kern_ntptime.c revision 1.30 1 /* $NetBSD: kern_ntptime.c,v 1.30 2006/05/14 21:15:11 elad 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.30 2006/05/14 21:15:11 elad 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/proc.h>
62 #include <sys/sysctl.h>
63 #include <sys/timex.h>
64 #include <sys/vnode.h>
65 #include <sys/kauth.h>
66
67 #include <sys/mount.h>
68 #include <sys/sa.h>
69 #include <sys/syscallargs.h>
70
71 #include <machine/cpu.h>
72
73 #ifdef NTP
74 /*
75 * The following variables are used by the hardclock() routine in the
76 * kern_clock.c module and are described in that module.
77 */
78 extern int time_state; /* clock state */
79 extern int time_status; /* clock status bits */
80 extern long time_offset; /* time adjustment (us) */
81 extern long time_freq; /* frequency offset (scaled ppm) */
82 extern long time_maxerror; /* maximum error (us) */
83 extern long time_esterror; /* estimated error (us) */
84 extern long time_constant; /* pll time constant */
85 extern long time_precision; /* clock precision (us) */
86 extern long time_tolerance; /* frequency tolerance (scaled ppm) */
87 extern int time_adjusted; /* ntp might have changed the system time */
88
89 #ifdef PPS_SYNC
90 /*
91 * The following variables are used only if the PPS signal discipline
92 * is configured in the kernel.
93 */
94 extern int pps_shift; /* interval duration (s) (shift) */
95 extern long pps_freq; /* pps frequency offset (scaled ppm) */
96 extern long pps_jitter; /* pps jitter (us) */
97 extern long pps_stabil; /* pps stability (scaled ppm) */
98 extern long pps_jitcnt; /* jitter limit exceeded */
99 extern long pps_calcnt; /* calibration intervals */
100 extern long pps_errcnt; /* calibration errors */
101 extern long pps_stbcnt; /* stability limit exceeded */
102 #endif /* PPS_SYNC */
103
104 /*ARGSUSED*/
105 /*
106 * ntp_gettime() - NTP user application interface
107 */
108 int
109 sys_ntp_gettime(l, v, retval)
110 struct lwp *l;
111 void *v;
112 register_t *retval;
113
114 {
115 struct sys_ntp_gettime_args /* {
116 syscallarg(struct ntptimeval *) ntvp;
117 } */ *uap = v;
118 struct timeval atv;
119 struct ntptimeval ntv;
120 int error = 0;
121 int s;
122
123 if (SCARG(uap, ntvp)) {
124 s = splclock();
125 #ifdef EXT_CLOCK
126 /*
127 * The microtime() external clock routine returns a
128 * status code. If less than zero, we declare an error
129 * in the clock status word and return the kernel
130 * (software) time variable. While there are other
131 * places that call microtime(), this is the only place
132 * that matters from an application point of view.
133 */
134 if (microtime(&atv) < 0) {
135 time_status |= STA_CLOCKERR;
136 ntv.time = time;
137 } else
138 time_status &= ~STA_CLOCKERR;
139 #else /* EXT_CLOCK */
140 microtime(&atv);
141 #endif /* EXT_CLOCK */
142 ntv.time = atv;
143 ntv.maxerror = time_maxerror;
144 ntv.esterror = time_esterror;
145 (void) splx(s);
146
147 error = copyout((caddr_t)&ntv, (caddr_t)SCARG(uap, ntvp),
148 sizeof(ntv));
149 }
150 if (!error) {
151
152 /*
153 * Status word error decode. If any of these conditions
154 * occur, an error is returned, instead of the status
155 * word. Most applications will care only about the fact
156 * the system clock may not be trusted, not about the
157 * details.
158 *
159 * Hardware or software error
160 */
161 if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
162
163 /*
164 * PPS signal lost when either time or frequency
165 * synchronization requested
166 */
167 (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
168 !(time_status & STA_PPSSIGNAL)) ||
169
170 /*
171 * PPS jitter exceeded when time synchronization
172 * requested
173 */
174 (time_status & STA_PPSTIME &&
175 time_status & STA_PPSJITTER) ||
176
177 /*
178 * PPS wander exceeded or calibration error when
179 * frequency synchronization requested
180 */
181 (time_status & STA_PPSFREQ &&
182 time_status & (STA_PPSWANDER | STA_PPSERROR)))
183 *retval = TIME_ERROR;
184 else
185 *retval = (register_t)time_state;
186 }
187 return(error);
188 }
189
190 /* ARGSUSED */
191 /*
192 * ntp_adjtime() - NTP daemon application interface
193 */
194 int
195 sys_ntp_adjtime(l, v, retval)
196 struct lwp *l;
197 void *v;
198 register_t *retval;
199 {
200 struct sys_ntp_adjtime_args /* {
201 syscallarg(struct timex *) tp;
202 } */ *uap = v;
203 struct proc *p = l->l_proc;
204 struct timex ntv;
205 int error = 0;
206
207 if ((error = copyin((caddr_t)SCARG(uap, tp), (caddr_t)&ntv,
208 sizeof(ntv))) != 0)
209 return (error);
210
211 if (ntv.modes != 0 && (error = kauth_authorize_generic(p->p_cred,
212 KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
213 return (error);
214
215 return (ntp_adjtime1(&ntv, v, retval));
216 }
217
218 int
219 ntp_adjtime1(ntv, v, retval)
220 struct timex *ntv;
221 void *v;
222 register_t *retval;
223 {
224 struct sys_ntp_adjtime_args /* {
225 syscallarg(struct timex *) tp;
226 } */ *uap = v;
227 int error = 0;
228 int modes;
229 int s;
230
231 /*
232 * Update selected clock variables. Note that there is no error
233 * checking here on the assumption the superuser should know
234 * what it is doing.
235 */
236 modes = ntv->modes;
237 if (modes != 0)
238 /* We need to save the system time during shutdown */
239 time_adjusted |= 2;
240 s = splclock();
241 if (modes & MOD_FREQUENCY)
242 #ifdef PPS_SYNC
243 time_freq = ntv->freq - pps_freq;
244 #else /* PPS_SYNC */
245 time_freq = ntv->freq;
246 #endif /* PPS_SYNC */
247 if (modes & MOD_MAXERROR)
248 time_maxerror = ntv->maxerror;
249 if (modes & MOD_ESTERROR)
250 time_esterror = ntv->esterror;
251 if (modes & MOD_STATUS) {
252 time_status &= STA_RONLY;
253 time_status |= ntv->status & ~STA_RONLY;
254 }
255 if (modes & MOD_TIMECONST)
256 time_constant = ntv->constant;
257 if (modes & MOD_OFFSET)
258 hardupdate(ntv->offset);
259
260 /*
261 * Retrieve all clock variables
262 */
263 if (time_offset < 0)
264 ntv->offset = -(-time_offset >> SHIFT_UPDATE);
265 else
266 ntv->offset = time_offset >> SHIFT_UPDATE;
267 #ifdef PPS_SYNC
268 ntv->freq = time_freq + pps_freq;
269 #else /* PPS_SYNC */
270 ntv->freq = time_freq;
271 #endif /* PPS_SYNC */
272 ntv->maxerror = time_maxerror;
273 ntv->esterror = time_esterror;
274 ntv->status = time_status;
275 ntv->constant = time_constant;
276 ntv->precision = time_precision;
277 ntv->tolerance = time_tolerance;
278 #ifdef PPS_SYNC
279 ntv->shift = pps_shift;
280 ntv->ppsfreq = pps_freq;
281 ntv->jitter = pps_jitter >> PPS_AVG;
282 ntv->stabil = pps_stabil;
283 ntv->calcnt = pps_calcnt;
284 ntv->errcnt = pps_errcnt;
285 ntv->jitcnt = pps_jitcnt;
286 ntv->stbcnt = pps_stbcnt;
287 #endif /* PPS_SYNC */
288 (void)splx(s);
289
290 error = copyout((caddr_t)ntv, (caddr_t)SCARG(uap, tp), sizeof(*ntv));
291 if (!error) {
292
293 /*
294 * Status word error decode. See comments in
295 * ntp_gettime() routine.
296 */
297 if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
298 (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
299 !(time_status & STA_PPSSIGNAL)) ||
300 (time_status & STA_PPSTIME &&
301 time_status & STA_PPSJITTER) ||
302 (time_status & STA_PPSFREQ &&
303 time_status & (STA_PPSWANDER | STA_PPSERROR)))
304 *retval = TIME_ERROR;
305 else
306 *retval = (register_t)time_state;
307 }
308 return error;
309 }
310
311 /*
312 * return information about kernel precision timekeeping
313 */
314 static int
315 sysctl_kern_ntptime(SYSCTLFN_ARGS)
316 {
317 struct sysctlnode node;
318 struct timeval atv;
319 struct ntptimeval ntv;
320 int s;
321
322 /*
323 * Construct ntp_timeval.
324 */
325
326 s = splclock();
327 #ifdef EXT_CLOCK
328 /*
329 * The microtime() external clock routine returns a
330 * status code. If less than zero, we declare an error
331 * in the clock status word and return the kernel
332 * (software) time variable. While there are other
333 * places that call microtime(), this is the only place
334 * that matters from an application point of view.
335 */
336 if (microtime(&atv) < 0) {
337 time_status |= STA_CLOCKERR;
338 ntv.time = time;
339 } else {
340 time_status &= ~STA_CLOCKERR;
341 }
342 #else /* EXT_CLOCK */
343 microtime(&atv);
344 #endif /* EXT_CLOCK */
345 ntv.time = atv;
346 ntv.maxerror = time_maxerror;
347 ntv.esterror = time_esterror;
348 splx(s);
349
350 #ifdef notyet
351 /*
352 * Status word error decode. If any of these conditions
353 * occur, an error is returned, instead of the status
354 * word. Most applications will care only about the fact
355 * the system clock may not be trusted, not about the
356 * details.
357 *
358 * Hardware or software error
359 */
360 if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
361 ntv.time_state = TIME_ERROR;
362
363 /*
364 * PPS signal lost when either time or frequency
365 * synchronization requested
366 */
367 (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
368 !(time_status & STA_PPSSIGNAL)) ||
369
370 /*
371 * PPS jitter exceeded when time synchronization
372 * requested
373 */
374 (time_status & STA_PPSTIME &&
375 time_status & STA_PPSJITTER) ||
376
377 /*
378 * PPS wander exceeded or calibration error when
379 * frequency synchronization requested
380 */
381 (time_status & STA_PPSFREQ &&
382 time_status & (STA_PPSWANDER | STA_PPSERROR)))
383 ntv.time_state = TIME_ERROR;
384 else
385 ntv.time_state = time_state;
386 #endif /* notyet */
387
388 node = *rnode;
389 node.sysctl_data = &ntv;
390 node.sysctl_size = sizeof(ntv);
391 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
392 }
393
394 SYSCTL_SETUP(sysctl_kern_ntptime_setup, "sysctl kern.ntptime node setup")
395 {
396
397 sysctl_createv(clog, 0, NULL, NULL,
398 CTLFLAG_PERMANENT,
399 CTLTYPE_NODE, "kern", NULL,
400 NULL, 0, NULL, 0,
401 CTL_KERN, CTL_EOL);
402
403 sysctl_createv(clog, 0, NULL, NULL,
404 CTLFLAG_PERMANENT,
405 CTLTYPE_STRUCT, "ntptime",
406 SYSCTL_DESCR("Kernel clock values for NTP"),
407 sysctl_kern_ntptime, 0, NULL,
408 sizeof(struct ntptimeval),
409 CTL_KERN, KERN_NTPTIME, CTL_EOL);
410 }
411 #else /* !NTP */
412 /* For some reason, raising SIGSYS (as sys_nosys would) is problematic. */
413
414 int
415 sys_ntp_gettime(l, v, retval)
416 struct lwp *l;
417 void *v;
418 register_t *retval;
419 {
420
421 return(ENOSYS);
422 }
423 #endif /* !NTP */
424