kern_ntptime.c revision 1.13.2.3 1 /* $NetBSD: kern_ntptime.c,v 1.13.2.3 2001/11/14 19:16:36 nathanw Exp $ */
2
3 /******************************************************************************
4 * *
5 * Copyright (c) David L. Mills 1993, 1994 *
6 * *
7 * Permission to use, copy, modify, and distribute this software and its *
8 * documentation for any purpose and without fee is hereby granted, provided *
9 * that the above copyright notice appears in all copies and that both the *
10 * copyright notice and this permission notice appear in supporting *
11 * documentation, and that the name University of Delaware not be used in *
12 * advertising or publicity pertaining to distribution of the software *
13 * without specific, written prior permission. The University of Delaware *
14 * makes no representations about the suitability this software for any *
15 * purpose. It is provided "as is" without express or implied warranty. *
16 * *
17 ******************************************************************************/
18
19 /*
20 * Modification history kern_ntptime.c
21 *
22 * 24 Sep 94 David L. Mills
23 * Tightened code at exits.
24 *
25 * 24 Mar 94 David L. Mills
26 * Revised syscall interface to include new variables for PPS
27 * time discipline.
28 *
29 * 14 Feb 94 David L. Mills
30 * Added code for external clock
31 *
32 * 28 Nov 93 David L. Mills
33 * Revised frequency scaling to conform with adjusted parameters
34 *
35 * 17 Sep 93 David L. Mills
36 * Created file
37 */
38 /*
39 * ntp_gettime(), ntp_adjtime() - precision time interface for SunOS
40 * V4.1.1 and V4.1.3
41 *
42 * These routines consitute the Network Time Protocol (NTP) interfaces
43 * for user and daemon application programs. The ntp_gettime() routine
44 * provides the time, maximum error (synch distance) and estimated error
45 * (dispersion) to client user application programs. The ntp_adjtime()
46 * routine is used by the NTP daemon to adjust the system clock to an
47 * externally derived time. The time offset and related variables set by
48 * this routine are used by hardclock() to adjust the phase and
49 * frequency of the phase-lock loop which controls the system clock.
50 */
51
52 #include <sys/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD: kern_ntptime.c,v 1.13.2.3 2001/11/14 19:16:36 nathanw Exp $");
54
55 #include "opt_ntp.h"
56
57 #include <sys/param.h>
58 #include <sys/resourcevar.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61 #include <sys/lwp.h>
62 #include <sys/proc.h>
63 #include <sys/timex.h>
64 #include <sys/vnode.h>
65
66 #include <sys/mount.h>
67 #include <sys/syscallargs.h>
68
69 #include <machine/cpu.h>
70
71 #include <uvm/uvm_extern.h>
72 #include <sys/sysctl.h>
73
74 #ifdef NTP
75
76 /*
77 * The following variables are used by the hardclock() routine in the
78 * kern_clock.c module and are described in that module.
79 */
80 extern int time_state; /* clock state */
81 extern int time_status; /* clock status bits */
82 extern long time_offset; /* time adjustment (us) */
83 extern long time_freq; /* frequency offset (scaled ppm) */
84 extern long time_maxerror; /* maximum error (us) */
85 extern long time_esterror; /* estimated error (us) */
86 extern long time_constant; /* pll time constant */
87 extern long time_precision; /* clock precision (us) */
88 extern long time_tolerance; /* frequency tolerance (scaled ppm) */
89
90 #ifdef PPS_SYNC
91 /*
92 * The following variables are used only if the PPS signal discipline
93 * is configured in the kernel.
94 */
95 extern int pps_shift; /* interval duration (s) (shift) */
96 extern long pps_freq; /* pps frequency offset (scaled ppm) */
97 extern long pps_jitter; /* pps jitter (us) */
98 extern long pps_stabil; /* pps stability (scaled ppm) */
99 extern long pps_jitcnt; /* jitter limit exceeded */
100 extern long pps_calcnt; /* calibration intervals */
101 extern long pps_errcnt; /* calibration errors */
102 extern long pps_stbcnt; /* stability limit exceeded */
103 #endif /* PPS_SYNC */
104
105
106
107 /*ARGSUSED*/
108 /*
109 * ntp_gettime() - NTP user application interface
110 */
111 int
112 sys_ntp_gettime(l, v, retval)
113 struct lwp *l;
114 void *v;
115 register_t *retval;
116
117 {
118 struct sys_ntp_gettime_args /* {
119 syscallarg(struct ntptimeval *) ntvp;
120 } */ *uap = v;
121 struct timeval atv;
122 struct ntptimeval ntv;
123 int error = 0;
124 int s;
125
126 if (SCARG(uap, ntvp)) {
127 s = splclock();
128 #ifdef EXT_CLOCK
129 /*
130 * The microtime() external clock routine returns a
131 * status code. If less than zero, we declare an error
132 * in the clock status word and return the kernel
133 * (software) time variable. While there are other
134 * places that call microtime(), this is the only place
135 * that matters from an application point of view.
136 */
137 if (microtime(&atv) < 0) {
138 time_status |= STA_CLOCKERR;
139 ntv.time = time;
140 } else
141 time_status &= ~STA_CLOCKERR;
142 #else /* EXT_CLOCK */
143 microtime(&atv);
144 #endif /* EXT_CLOCK */
145 ntv.time = atv;
146 ntv.maxerror = time_maxerror;
147 ntv.esterror = time_esterror;
148 (void) splx(s);
149
150 error = copyout((caddr_t)&ntv, (caddr_t)SCARG(uap, ntvp),
151 sizeof(ntv));
152 }
153 if (!error) {
154
155 /*
156 * Status word error decode. If any of these conditions
157 * occur, an error is returned, instead of the status
158 * word. Most applications will care only about the fact
159 * the system clock may not be trusted, not about the
160 * details.
161 *
162 * Hardware or software error
163 */
164 if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
165
166 /*
167 * PPS signal lost when either time or frequency
168 * synchronization requested
169 */
170 (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
171 !(time_status & STA_PPSSIGNAL)) ||
172
173 /*
174 * PPS jitter exceeded when time synchronization
175 * requested
176 */
177 (time_status & STA_PPSTIME &&
178 time_status & STA_PPSJITTER) ||
179
180 /*
181 * PPS wander exceeded or calibration error when
182 * frequency synchronization requested
183 */
184 (time_status & STA_PPSFREQ &&
185 time_status & (STA_PPSWANDER | STA_PPSERROR)))
186 *retval = TIME_ERROR;
187 else
188 *retval = (register_t)time_state;
189 }
190 return(error);
191 }
192
193
194 /* ARGSUSED */
195 /*
196 * ntp_adjtime() - NTP daemon application interface
197 */
198 int
199 sys_ntp_adjtime(l, v, retval)
200 struct lwp *l;
201 void *v;
202 register_t *retval;
203 {
204 struct sys_ntp_adjtime_args /* {
205 syscallarg(struct timex *) tp;
206 } */ *uap = v;
207 struct proc *p = l->l_proc;
208 struct timex ntv;
209 int error = 0;
210
211 if ((error = copyin((caddr_t)SCARG(uap, tp), (caddr_t)&ntv,
212 sizeof(ntv))) != 0)
213 return (error);
214
215
216 if (ntv.modes != 0 && (error = suser(p->p_ucred, &p->p_acflag)) != 0)
217 return (error);
218
219 return (ntp_adjtime1(&ntv, v, retval));
220 }
221
222 int
223 ntp_adjtime1(ntv, v, retval)
224 struct timex *ntv;
225 void *v;
226 register_t *retval;
227 {
228 struct sys_ntp_adjtime_args /* {
229 syscallarg(struct timex *) tp;
230 } */ *uap = v;
231 int error = 0;
232 int modes;
233 int s;
234
235 /*
236 * Update selected clock variables. Note that there is no error
237 * checking here on the assumption the superuser should know
238 * what it is doing.
239 */
240 modes = ntv->modes;
241 s = splclock();
242 if (modes & MOD_FREQUENCY)
243 #ifdef PPS_SYNC
244 time_freq = ntv->freq - pps_freq;
245 #else /* PPS_SYNC */
246 time_freq = ntv->freq;
247 #endif /* PPS_SYNC */
248 if (modes & MOD_MAXERROR)
249 time_maxerror = ntv->maxerror;
250 if (modes & MOD_ESTERROR)
251 time_esterror = ntv->esterror;
252 if (modes & MOD_STATUS) {
253 time_status &= STA_RONLY;
254 time_status |= ntv->status & ~STA_RONLY;
255 }
256 if (modes & MOD_TIMECONST)
257 time_constant = ntv->constant;
258 if (modes & MOD_OFFSET)
259 hardupdate(ntv->offset);
260
261 /*
262 * Retrieve all clock variables
263 */
264 if (time_offset < 0)
265 ntv->offset = -(-time_offset >> SHIFT_UPDATE);
266 else
267 ntv->offset = time_offset >> SHIFT_UPDATE;
268 #ifdef PPS_SYNC
269 ntv->freq = time_freq + pps_freq;
270 #else /* PPS_SYNC */
271 ntv->freq = time_freq;
272 #endif /* PPS_SYNC */
273 ntv->maxerror = time_maxerror;
274 ntv->esterror = time_esterror;
275 ntv->status = time_status;
276 ntv->constant = time_constant;
277 ntv->precision = time_precision;
278 ntv->tolerance = time_tolerance;
279 #ifdef PPS_SYNC
280 ntv->shift = pps_shift;
281 ntv->ppsfreq = pps_freq;
282 ntv->jitter = pps_jitter >> PPS_AVG;
283 ntv->stabil = pps_stabil;
284 ntv->calcnt = pps_calcnt;
285 ntv->errcnt = pps_errcnt;
286 ntv->jitcnt = pps_jitcnt;
287 ntv->stbcnt = pps_stbcnt;
288 #endif /* PPS_SYNC */
289 (void)splx(s);
290
291 error = copyout((caddr_t)ntv, (caddr_t)SCARG(uap, tp), sizeof(*ntv));
292 if (!error) {
293
294 /*
295 * Status word error decode. See comments in
296 * ntp_gettime() routine.
297 */
298 if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
299 (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
300 !(time_status & STA_PPSSIGNAL)) ||
301 (time_status & STA_PPSTIME &&
302 time_status & STA_PPSJITTER) ||
303 (time_status & STA_PPSFREQ &&
304 time_status & (STA_PPSWANDER | STA_PPSERROR)))
305 *retval = TIME_ERROR;
306 else
307 *retval = (register_t)time_state;
308 }
309 return error;
310 }
311
312
313
314 /*
315 * return information about kernel precision timekeeping
316 */
317 int
318 sysctl_ntptime(where, sizep)
319 void *where;
320 size_t *sizep;
321 {
322 struct timeval atv;
323 struct ntptimeval ntv;
324 int s;
325
326 /*
327 * Construct ntp_timeval.
328 */
329
330 s = splclock();
331 #ifdef EXT_CLOCK
332 /*
333 * The microtime() external clock routine returns a
334 * status code. If less than zero, we declare an error
335 * in the clock status word and return the kernel
336 * (software) time variable. While there are other
337 * places that call microtime(), this is the only place
338 * that matters from an application point of view.
339 */
340 if (microtime(&atv) < 0) {
341 time_status |= STA_CLOCKERR;
342 ntv.time = time;
343 } else {
344 time_status &= ~STA_CLOCKERR;
345 }
346 #else /* EXT_CLOCK */
347 microtime(&atv);
348 #endif /* EXT_CLOCK */
349 ntv.time = atv;
350 ntv.maxerror = time_maxerror;
351 ntv.esterror = time_esterror;
352 splx(s);
353
354 #ifdef notyet
355 /*
356 * Status word error decode. If any of these conditions
357 * occur, an error is returned, instead of the status
358 * word. Most applications will care only about the fact
359 * the system clock may not be trusted, not about the
360 * details.
361 *
362 * Hardware or software error
363 */
364 if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
365 ntv.time_state = TIME_ERROR;
366
367 /*
368 * PPS signal lost when either time or frequency
369 * synchronization requested
370 */
371 (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
372 !(time_status & STA_PPSSIGNAL)) ||
373
374 /*
375 * PPS jitter exceeded when time synchronization
376 * requested
377 */
378 (time_status & STA_PPSTIME &&
379 time_status & STA_PPSJITTER) ||
380
381 /*
382 * PPS wander exceeded or calibration error when
383 * frequency synchronization requested
384 */
385 (time_status & STA_PPSFREQ &&
386 time_status & (STA_PPSWANDER | STA_PPSERROR)))
387 ntv.time_state = TIME_ERROR;
388 else
389 ntv.time_state = time_state;
390 #endif /* notyet */
391 return (sysctl_rdstruct(where, sizep, NULL, &ntv, sizeof(ntv)));
392 }
393
394 #else /* !NTP */
395
396 /* For some reason, raising SIGSYS (as sys_nosys would) is problematic. */
397
398 int
399 sys_ntp_gettime(l, v, retval)
400 struct lwp *l;
401 void *v;
402 register_t *retval;
403 {
404 return(ENOSYS);
405 }
406
407 #endif /* !NTP */
408