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