kern_ntptime.c revision 1.13.2.4 1 /* $NetBSD: kern_ntptime.c,v 1.13.2.4 2002/01/08 00:32:33 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.4 2002/01/08 00:32:33 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 if (ntv.modes != 0 && (error = suser(p->p_ucred, &p->p_acflag)) != 0)
216 return (error);
217
218 return (ntp_adjtime1(&ntv, v, retval));
219 }
220
221 int
222 ntp_adjtime1(ntv, v, retval)
223 struct timex *ntv;
224 void *v;
225 register_t *retval;
226 {
227 struct sys_ntp_adjtime_args /* {
228 syscallarg(struct timex *) tp;
229 } */ *uap = v;
230 int error = 0;
231 int modes;
232 int s;
233
234 /*
235 * Update selected clock variables. Note that there is no error
236 * checking here on the assumption the superuser should know
237 * what it is doing.
238 */
239 modes = ntv->modes;
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
313 /*
314 * return information about kernel precision timekeeping
315 */
316 int
317 sysctl_ntptime(where, sizep)
318 void *where;
319 size_t *sizep;
320 {
321 struct timeval atv;
322 struct ntptimeval ntv;
323 int s;
324
325 /*
326 * Construct ntp_timeval.
327 */
328
329 s = splclock();
330 #ifdef EXT_CLOCK
331 /*
332 * The microtime() external clock routine returns a
333 * status code. If less than zero, we declare an error
334 * in the clock status word and return the kernel
335 * (software) time variable. While there are other
336 * places that call microtime(), this is the only place
337 * that matters from an application point of view.
338 */
339 if (microtime(&atv) < 0) {
340 time_status |= STA_CLOCKERR;
341 ntv.time = time;
342 } else {
343 time_status &= ~STA_CLOCKERR;
344 }
345 #else /* EXT_CLOCK */
346 microtime(&atv);
347 #endif /* EXT_CLOCK */
348 ntv.time = atv;
349 ntv.maxerror = time_maxerror;
350 ntv.esterror = time_esterror;
351 splx(s);
352
353 #ifdef notyet
354 /*
355 * Status word error decode. If any of these conditions
356 * occur, an error is returned, instead of the status
357 * word. Most applications will care only about the fact
358 * the system clock may not be trusted, not about the
359 * details.
360 *
361 * Hardware or software error
362 */
363 if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
364 ntv.time_state = TIME_ERROR;
365
366 /*
367 * PPS signal lost when either time or frequency
368 * synchronization requested
369 */
370 (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
371 !(time_status & STA_PPSSIGNAL)) ||
372
373 /*
374 * PPS jitter exceeded when time synchronization
375 * requested
376 */
377 (time_status & STA_PPSTIME &&
378 time_status & STA_PPSJITTER) ||
379
380 /*
381 * PPS wander exceeded or calibration error when
382 * frequency synchronization requested
383 */
384 (time_status & STA_PPSFREQ &&
385 time_status & (STA_PPSWANDER | STA_PPSERROR)))
386 ntv.time_state = TIME_ERROR;
387 else
388 ntv.time_state = time_state;
389 #endif /* notyet */
390 return (sysctl_rdstruct(where, sizep, NULL, &ntv, sizeof(ntv)));
391 }
392
393 #else /* !NTP */
394
395 /* For some reason, raising SIGSYS (as sys_nosys would) is problematic. */
396
397 int
398 sys_ntp_gettime(l, v, retval)
399 struct lwp *l;
400 void *v;
401 register_t *retval;
402 {
403 return(ENOSYS);
404 }
405
406 #endif /* !NTP */
407