This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / sys / kern / kern_ntptime.c
CommitLineData
486e5ce2
GW
1/******************************************************************************
2 * *
3 * Copyright (c) David L. Mills 1993, 1994 *
4 * *
5 * Permission to use, copy, modify, and distribute this software and its *
6 * documentation for any purpose and without fee is hereby granted, provided *
7 * that the above copyright notice appears in all copies and that both the *
8 * copyright notice and this permission notice appear in supporting *
9 * documentation, and that the name University of Delaware not be used in *
10 * advertising or publicity pertaining to distribution of the software *
11 * without specific, written prior permission. The University of Delaware *
12 * makes no representations about the suitability this software for any *
13 * purpose. It is provided "as is" without express or implied warranty. *
14 * *
15 ******************************************************************************/
16
17/*
18 * Modification history kern_ntptime.c
19 *
1b0bf0f1
GW
20 * 24 Mar 94 David L. Mills
21 * Revised syscall interface to include new variables for PPS
22 * time discipline.
23 *
486e5ce2
GW
24 * 14 Feb 94 David L. Mills
25 * Added code for external clock
26 *
27 * 28 Nov 93 David L. Mills
28 * Revised frequency scaling to conform with adjusted parameters
29 *
30 * 17 Sep 93 David L. Mills
31 * Created file
32 */
33/*
1b0bf0f1
GW
34 * ntp_gettime(), ntp_adjtime() - precision time interface for SunOS
35 * 4.1.1 and 4.1.3
486e5ce2
GW
36 *
37 * These routines consitute the Network Time Protocol (NTP) interfaces
38 * for user and daemon application programs. The ntp_gettime() routine
39 * provides the time, maximum error (synch distance) and estimated error
40 * (dispersion) to client user application programs. The ntp_adjtime()
41 * routine is used by the NTP daemon to adjust the system clock to an
42 * externally derived time. The time offset and related variables set by
43 * this routine are used by hardclock() to adjust the phase and
44 * frequency of the phase-lock loop which controls the system clock.
45 */
9e85cc83
GW
46#include "param.h"
47#include "systm.h"
48#include "kernel.h"
9e85cc83 49#include "proc.h"
1b0bf0f1 50#include "timex.h"
486e5ce2 51
1b0bf0f1
GW
52/*
53 * The following variables are used by the hardclock() routine in the
54 * kern_clock.c module and are described in that module.
55 */
56extern struct timeval time; /* kernel time variable */
57extern int time_state; /* clock state */
58extern int time_status; /* clock status bits */
59extern long time_offset; /* time adjustment (us) */
60extern long time_freq; /* frequency offset (scaled ppm) */
61extern long time_maxerror; /* maximum error (us) */
62extern long time_esterror; /* estimated error (us) */
63extern long time_constant; /* pll time constant */
64extern long time_precision; /* clock precision (us) */
65extern long time_tolerance; /* frequency tolerance (scaled ppm) */
486e5ce2 66
1b0bf0f1
GW
67#ifdef PPS_SYNC
68/*
69 * The following variables are used only if the PPS signal discipline
70 * is configured in the kernel.
71 */
72extern int pps_shift; /* interval duration (s) (shift) */
73extern long pps_freq; /* pps frequency offset (scaled ppm) */
74extern long pps_jitter; /* pps jitter (us) */
75extern long pps_stabil; /* pps stability (scaled ppm) */
76extern long pps_jitcnt; /* jitter limit exceeded */
77extern long pps_calcnt; /* calibration intervals */
78extern long pps_errcnt; /* calibration errors */
79extern long pps_stbcnt; /* stability limit exceeded */
80#endif /* PPS_SYNC */
486e5ce2
GW
81
82/*
83 * ntp_gettime() - NTP user application interface
84 */
9e85cc83 85struct ntp_gettime_args {
1b0bf0f1 86 struct ntptimeval *tp;
9e85cc83
GW
87};
88
89int
90ntp_gettime(struct proc *p, struct ntp_gettime_args *uap, int *retval)
486e5ce2 91{
486e5ce2
GW
92 struct timeval atv;
93 struct ntptimeval ntv;
94 int s;
1b0bf0f1 95 int error = 0;
486e5ce2
GW
96
97 if (uap->tp) {
486e5ce2 98 s = splclock();
1b0bf0f1
GW
99#ifdef EXT_CLOCK
100 /*
101 * The microtime() external clock routine returns a
102 * status code. If less than zero, we declare an error
103 * in the clock status word and return the kernel
104 * (software) time variable. While there are other
105 * places that call microtime(), this is the only place
106 * that matters from an application point of view.
107 */
108 if (microtime(&atv) < 0) {
109 time_status |= STA_CLOCKERR;
110 ntv.time = time;
111 } else
112 time_status &= ~STA_CLOCKERR;
113#else /* EXT_CLOCK */
114 microtime(&atv);
115#endif /* EXT_CLOCK */
116 ntv.time = atv;
117 ntv.maxerror = time_maxerror;
118 ntv.esterror = time_esterror;
486e5ce2
GW
119 (void) splx(s);
120
1b0bf0f1
GW
121 error = copyout((caddr_t)&ntv, (caddr_t)uap->tp,
122 sizeof (ntv));
486e5ce2 123 }
1b0bf0f1
GW
124 if (!error) {
125 *retval = time_state;
486e5ce2 126
1b0bf0f1
GW
127 /*
128 * Status word error decode. If any of these conditions
129 * occur, an error is returned, instead of the status
130 * word. Most applications will care only about the fact
131 * the system clock may not be trusted, not about the
132 * details.
133 *
134 * Hardware or software error
135 */
136 if (time_status & (STA_UNSYNC | STA_CLOCKERR))
137 *retval = TIME_ERROR;
138
139 /*
140 * PPS signal lost when either time or frequency
141 * synchronization requested
142 */
143 if (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
144 !(time_status & STA_PPSSIGNAL))
145 *retval = TIME_ERROR;
9e85cc83 146
1b0bf0f1
GW
147 /*
148 * PPS jitter exceeded when time synchronization
149 * requested
150 */
151 if (time_status & STA_PPSTIME &&
152 time_status & STA_PPSJITTER)
153 *retval = TIME_ERROR;
154
155 /*
156 * PPS wander exceeded or calibration error when
157 * frequency synchronization requested
158 */
159 if (time_status & STA_PPSFREQ &&
160 time_status & (STA_PPSWANDER | STA_PPSERROR))
161 *retval = TIME_ERROR;
162 }
163 return error;
164}
9e85cc83 165
486e5ce2
GW
166/*
167 * ntp_adjtime() - NTP daemon application interface
168 */
1b0bf0f1
GW
169struct ntp_adjtime_args {
170 struct timex *tp;
171};
172
9e85cc83 173int
1b0bf0f1
GW
174ntp_adjtime(struct proc *p, struct ntp_adjtime_args *uap, int *retval)
175{
486e5ce2 176 struct timex ntv;
1b0bf0f1
GW
177 int modes;
178 int s;
179 int error;
486e5ce2 180
9e85cc83
GW
181 error = copyin((caddr_t)uap->tp, (caddr_t)&ntv, sizeof(ntv));
182 if (error)
183 return error;
486e5ce2
GW
184
185 /*
186 * Update selected clock variables - only the superuser can
187 * change anything. Note that there is no error checking here on
188 * the assumption the superuser should know what it is doing.
189 */
1b0bf0f1
GW
190 modes = ntv.modes;
191 if ((modes != 0)
192 && (error = suser(p->p_cred->pc_ucred, &p->p_acflag)))
193 return error;
486e5ce2
GW
194
195 s = splclock();
1b0bf0f1 196 if (modes & MOD_FREQUENCY)
486e5ce2 197#ifdef PPS_SYNC
1b0bf0f1 198 time_freq = ntv.freq - pps_freq;
486e5ce2 199#else /* PPS_SYNC */
1b0bf0f1 200 time_freq = ntv.freq;
486e5ce2 201#endif /* PPS_SYNC */
1b0bf0f1
GW
202 if (modes & MOD_MAXERROR)
203 time_maxerror = ntv.maxerror;
204 if (modes & MOD_ESTERROR)
205 time_esterror = ntv.esterror;
206 if (modes & MOD_STATUS) {
207 time_status &= STA_RONLY;
208 time_status |= ntv.status & ~STA_RONLY;
209 }
210 if (modes & MOD_TIMECONST)
211 time_constant = ntv.constant;
212 if (modes & MOD_OFFSET)
213 hardupdate(ntv.offset);
486e5ce2
GW
214
215 /*
216 * Retrieve all clock variables
217 */
1b0bf0f1
GW
218 if (time_offset < 0)
219 ntv.offset = -(-time_offset >> SHIFT_UPDATE);
486e5ce2 220 else
1b0bf0f1 221 ntv.offset = time_offset >> SHIFT_UPDATE;
486e5ce2 222#ifdef PPS_SYNC
1b0bf0f1 223 ntv.freq = time_freq + pps_freq;
486e5ce2 224#else /* PPS_SYNC */
1b0bf0f1 225 ntv.freq = time_freq;
486e5ce2 226#endif /* PPS_SYNC */
1b0bf0f1
GW
227 ntv.maxerror = time_maxerror;
228 ntv.esterror = time_esterror;
229 ntv.status = time_status;
230 ntv.constant = time_constant;
231 ntv.precision = time_precision;
232 ntv.tolerance = time_tolerance;
486e5ce2 233#ifdef PPS_SYNC
1b0bf0f1
GW
234 ntv.shift = pps_shift;
235 ntv.ppsfreq = pps_freq;
236 ntv.jitter = pps_jitter >> PPS_AVG;
237 ntv.stabil = pps_stabil;
238 ntv.calcnt = pps_calcnt;
239 ntv.errcnt = pps_errcnt;
240 ntv.jitcnt = pps_jitcnt;
241 ntv.stbcnt = pps_stbcnt;
486e5ce2
GW
242#endif /* PPS_SYNC */
243 (void)splx(s);
244
9e85cc83 245 error = copyout((caddr_t)&ntv, (caddr_t)uap->tp, sizeof(ntv));
1b0bf0f1
GW
246 if (!error) {
247 /*
248 * Status word error decode. See comments in
249 * ntp_gettime() routine.
250 */
251 retval[0] = time_state;
252 if (time_status & (STA_UNSYNC | STA_CLOCKERR))
253 retval[0] = TIME_ERROR;
254 if (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
255 !(time_status & STA_PPSSIGNAL))
256 retval[0] = TIME_ERROR;
257 if (time_status & STA_PPSTIME &&
258 time_status & STA_PPSJITTER)
259 retval[0] = TIME_ERROR;
260 if (time_status & STA_PPSFREQ &&
261 time_status & (STA_PPSWANDER | STA_PPSERROR))
262 retval[0] = TIME_ERROR;
263 }
9e85cc83 264 return error;
486e5ce2 265}
9e85cc83 266
1b0bf0f1 267