xref: /netbsd/sys/kern/kern_ntptime.c (revision 6183a165)
1 /*	$NetBSD: kern_ntptime.c,v 1.64 2022/10/26 23:23:52 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*-
30  ***********************************************************************
31  *								       *
32  * Copyright (c) David L. Mills 1993-2001			       *
33  *								       *
34  * Permission to use, copy, modify, and distribute this software and   *
35  * its documentation for any purpose and without fee is hereby	       *
36  * granted, provided that the above copyright notice appears in all    *
37  * copies and that both the copyright notice and this permission       *
38  * notice appear in supporting documentation, and that the name	       *
39  * University of Delaware not be used in advertising or publicity      *
40  * pertaining to distribution of the software without specific,	       *
41  * written prior permission. The University of Delaware makes no       *
42  * representations about the suitability this software for any	       *
43  * purpose. It is provided "as is" without express or implied	       *
44  * warranty.							       *
45  *								       *
46  **********************************************************************/
47 
48 /*
49  * Adapted from the original sources for FreeBSD and timecounters by:
50  * Poul-Henning Kamp <phk@FreeBSD.org>.
51  *
52  * The 32bit version of the "LP" macros seems a bit past its "sell by"
53  * date so I have retained only the 64bit version and included it directly
54  * in this file.
55  *
56  * Only minor changes done to interface with the timecounters over in
57  * sys/kern/kern_clock.c.   Some of the comments below may be (even more)
58  * confusing and/or plain wrong in that context.
59  */
60 
61 #include <sys/cdefs.h>
62 /* __FBSDID("$FreeBSD: src/sys/kern/kern_ntptime.c,v 1.59 2005/05/28 14:34:41 rwatson Exp $"); */
63 __KERNEL_RCSID(0, "$NetBSD: kern_ntptime.c,v 1.64 2022/10/26 23:23:52 riastradh Exp $");
64 
65 #ifdef _KERNEL_OPT
66 #include "opt_ntp.h"
67 #endif
68 
69 #include <sys/param.h>
70 #include <sys/resourcevar.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/proc.h>
74 #include <sys/sysctl.h>
75 #include <sys/timex.h>
76 #include <sys/vnode.h>
77 #include <sys/kauth.h>
78 #include <sys/mount.h>
79 #include <sys/syscallargs.h>
80 #include <sys/cpu.h>
81 
82 #include <compat/sys/timex.h>
83 
84 /*
85  * Single-precision macros for 64-bit machines
86  */
87 typedef int64_t l_fp;
88 #define L_ADD(v, u)	((v) += (u))
89 #define L_SUB(v, u)	((v) -= (u))
90 #define L_ADDHI(v, a)	((v) += (int64_t)(a) << 32)
91 #define L_NEG(v)	((v) = -(v))
92 #define L_RSHIFT(v, n) \
93 	do { \
94 		if ((v) < 0) \
95 			(v) = -(-(v) >> (n)); \
96 		else \
97 			(v) = (v) >> (n); \
98 	} while (0)
99 #define L_MPY(v, a)	((v) *= (a))
100 #define L_CLR(v)	((v) = 0)
101 #define L_ISNEG(v)	((v) < 0)
102 #define L_LINT(v, a)	((v) = (int64_t)((uint64_t)(a) << 32))
103 #define L_GINT(v)	((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
104 
105 #ifdef NTP
106 /*
107  * Generic NTP kernel interface
108  *
109  * These routines constitute the Network Time Protocol (NTP) interfaces
110  * for user and daemon application programs. The ntp_gettime() routine
111  * provides the time, maximum error (synch distance) and estimated error
112  * (dispersion) to client user application programs. The ntp_adjtime()
113  * routine is used by the NTP daemon to adjust the system clock to an
114  * externally derived time. The time offset and related variables set by
115  * this routine are used by other routines in this module to adjust the
116  * phase and frequency of the clock discipline loop which controls the
117  * system clock.
118  *
119  * When the kernel time is reckoned directly in nanoseconds (NTP_NANO
120  * defined), the time at each tick interrupt is derived directly from
121  * the kernel time variable. When the kernel time is reckoned in
122  * microseconds, (NTP_NANO undefined), the time is derived from the
123  * kernel time variable together with a variable representing the
124  * leftover nanoseconds at the last tick interrupt. In either case, the
125  * current nanosecond time is reckoned from these values plus an
126  * interpolated value derived by the clock routines in another
127  * architecture-specific module. The interpolation can use either a
128  * dedicated counter or a processor cycle counter (PCC) implemented in
129  * some architectures.
130  *
131  * Note that all routines must run at priority splclock or higher.
132  */
133 /*
134  * Phase/frequency-lock loop (PLL/FLL) definitions
135  *
136  * The nanosecond clock discipline uses two variable types, time
137  * variables and frequency variables. Both types are represented as 64-
138  * bit fixed-point quantities with the decimal point between two 32-bit
139  * halves. On a 32-bit machine, each half is represented as a single
140  * word and mathematical operations are done using multiple-precision
141  * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
142  * used.
143  *
144  * A time variable is a signed 64-bit fixed-point number in ns and
145  * fraction. It represents the remaining time offset to be amortized
146  * over succeeding tick interrupts. The maximum time offset is about
147  * 0.5 s and the resolution is about 2.3e-10 ns.
148  *
149  *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
150  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
151  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
152  * |s s s|			 ns				   |
153  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
154  * |			    fraction				   |
155  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
156  *
157  * A frequency variable is a signed 64-bit fixed-point number in ns/s
158  * and fraction. It represents the ns and fraction to be added to the
159  * kernel time variable at each second. The maximum frequency offset is
160  * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
161  *
162  *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
163  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
164  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
165  * |s s s s s s s s s s s s s|	          ns/s			   |
166  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167  * |			    fraction				   |
168  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169  */
170 /*
171  * The following variables establish the state of the PLL/FLL and the
172  * residual time and frequency offset of the local clock.
173  */
174 #define SHIFT_PLL	4		/* PLL loop gain (shift) */
175 #define SHIFT_FLL	2		/* FLL loop gain (shift) */
176 
177 static int time_state = TIME_OK;	/* clock state */
178 static int time_status = STA_UNSYNC;	/* clock status bits */
179 static long time_tai;			/* TAI offset (s) */
180 static long time_monitor;		/* last time offset scaled (ns) */
181 static long time_constant;		/* poll interval (shift) (s) */
182 static long time_precision = 1;		/* clock precision (ns) */
183 static long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */
184 static long time_esterror = MAXPHASE / 1000; /* estimated error (us) */
185 static time_t time_reftime;		/* time at last adjustment (s) */
186 static l_fp time_offset;		/* time offset (ns) */
187 static l_fp time_freq;			/* frequency offset (ns/s) */
188 #endif /* NTP */
189 
190 static l_fp time_adj;			/* tick adjust (ns/s) */
191 int64_t time_adjtime;		/* correction from adjtime(2) (usec) */
192 
193 #ifdef NTP
194 #ifdef PPS_SYNC
195 /*
196  * The following variables are used when a pulse-per-second (PPS) signal
197  * is available and connected via a modem control lead. They establish
198  * the engineering parameters of the clock discipline loop when
199  * controlled by the PPS signal.
200  */
201 #define PPS_FAVG	2		/* min freq avg interval (s) (shift) */
202 #define PPS_FAVGDEF	8		/* default freq avg int (s) (shift) */
203 #define PPS_FAVGMAX	15		/* max freq avg interval (s) (shift) */
204 #define PPS_PAVG	4		/* phase avg interval (s) (shift) */
205 #define PPS_VALID	120		/* PPS signal watchdog max (s) */
206 #define PPS_MAXWANDER	100000		/* max PPS wander (ns/s) */
207 #define PPS_POPCORN	2		/* popcorn spike threshold (shift) */
208 
209 static struct timespec pps_tf[3];	/* phase median filter */
210 static l_fp pps_freq;			/* scaled frequency offset (ns/s) */
211 static long pps_fcount;			/* frequency accumulator */
212 static long pps_jitter;			/* nominal jitter (ns) */
213 static long pps_stabil;			/* nominal stability (scaled ns/s) */
214 static long pps_lastsec;		/* time at last calibration (s) */
215 static int pps_valid;			/* signal watchdog counter */
216 static int pps_shift = PPS_FAVG;	/* interval duration (s) (shift) */
217 static int pps_shiftmax = PPS_FAVGDEF;	/* max interval duration (s) (shift) */
218 static int pps_intcnt;			/* wander counter */
219 
220 /*
221  * PPS signal quality monitors
222  */
223 static long pps_calcnt;			/* calibration intervals */
224 static long pps_jitcnt;			/* jitter limit exceeded */
225 static long pps_stbcnt;			/* stability limit exceeded */
226 static long pps_errcnt;			/* calibration errors */
227 #endif /* PPS_SYNC */
228 /*
229  * End of phase/frequency-lock loop (PLL/FLL) definitions
230  */
231 
232 static void hardupdate(long offset);
233 
234 /*
235  * ntp_gettime() - NTP user application interface
236  */
237 void
ntp_gettime(struct ntptimeval * ntv)238 ntp_gettime(struct ntptimeval *ntv)
239 {
240 	memset(ntv, 0, sizeof(*ntv));
241 
242 	mutex_spin_enter(&timecounter_lock);
243 	nanotime(&ntv->time);
244 	ntv->maxerror = time_maxerror;
245 	ntv->esterror = time_esterror;
246 	ntv->tai = time_tai;
247 	ntv->time_state = time_state;
248 	mutex_spin_exit(&timecounter_lock);
249 }
250 
251 /* ARGSUSED */
252 /*
253  * ntp_adjtime() - NTP daemon application interface
254  */
255 int
sys_ntp_adjtime(struct lwp * l,const struct sys_ntp_adjtime_args * uap,register_t * retval)256 sys_ntp_adjtime(struct lwp *l, const struct sys_ntp_adjtime_args *uap, register_t *retval)
257 {
258 	/* {
259 		syscallarg(struct timex *) tp;
260 	} */
261 	struct timex ntv;
262 	int error;
263 
264 	error = copyin((void *)SCARG(uap, tp), (void *)&ntv, sizeof(ntv));
265 	if (error != 0)
266 		return (error);
267 
268 	if (ntv.modes != 0 && (error = kauth_authorize_system(l->l_cred,
269 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_NTPADJTIME, NULL,
270 	    NULL, NULL)) != 0)
271 		return (error);
272 
273 	ntp_adjtime1(&ntv);
274 
275 	error = copyout((void *)&ntv, (void *)SCARG(uap, tp), sizeof(ntv));
276 	if (!error)
277 		*retval = ntp_timestatus();
278 
279 	return error;
280 }
281 
282 void
ntp_adjtime1(struct timex * ntv)283 ntp_adjtime1(struct timex *ntv)
284 {
285 	long freq;
286 	int modes;
287 
288 	/*
289 	 * Update selected clock variables - only the superuser can
290 	 * change anything. Note that there is no error checking here on
291 	 * the assumption the superuser should know what it is doing.
292 	 * Note that either the time constant or TAI offset are loaded
293 	 * from the ntv.constant member, depending on the mode bits. If
294 	 * the STA_PLL bit in the status word is cleared, the state and
295 	 * status words are reset to the initial values at boot.
296 	 */
297 	mutex_spin_enter(&timecounter_lock);
298 	modes = ntv->modes;
299 	if (modes != 0)
300 		/* We need to save the system time during shutdown */
301 		time_adjusted |= 2;
302 	if (modes & MOD_MAXERROR)
303 		time_maxerror = ntv->maxerror;
304 	if (modes & MOD_ESTERROR)
305 		time_esterror = ntv->esterror;
306 	if (modes & MOD_STATUS) {
307 		if (time_status & STA_PLL && !(ntv->status & STA_PLL)) {
308 			time_state = TIME_OK;
309 			time_status = STA_UNSYNC;
310 #ifdef PPS_SYNC
311 			pps_shift = PPS_FAVG;
312 #endif /* PPS_SYNC */
313 		}
314 		time_status &= STA_RONLY;
315 		time_status |= ntv->status & ~STA_RONLY;
316 	}
317 	if (modes & MOD_TIMECONST) {
318 		if (ntv->constant < 0)
319 			time_constant = 0;
320 		else if (ntv->constant > MAXTC)
321 			time_constant = MAXTC;
322 		else
323 			time_constant = ntv->constant;
324 	}
325 	if (modes & MOD_TAI) {
326 		if (ntv->constant > 0)	/* XXX zero & negative numbers ? */
327 			time_tai = ntv->constant;
328 	}
329 #ifdef PPS_SYNC
330 	if (modes & MOD_PPSMAX) {
331 		if (ntv->shift < PPS_FAVG)
332 			pps_shiftmax = PPS_FAVG;
333 		else if (ntv->shift > PPS_FAVGMAX)
334 			pps_shiftmax = PPS_FAVGMAX;
335 		else
336 			pps_shiftmax = ntv->shift;
337 	}
338 #endif /* PPS_SYNC */
339 	if (modes & MOD_NANO)
340 		time_status |= STA_NANO;
341 	if (modes & MOD_MICRO)
342 		time_status &= ~STA_NANO;
343 	if (modes & MOD_CLKB)
344 		time_status |= STA_CLK;
345 	if (modes & MOD_CLKA)
346 		time_status &= ~STA_CLK;
347 	if (modes & MOD_FREQUENCY) {
348 		freq = MIN(INT32_MAX, MAX(INT32_MIN, ntv->freq));
349 		freq = (freq * (int64_t)1000) >> 16;
350 		if (freq > MAXFREQ)
351 			L_LINT(time_freq, MAXFREQ);
352 		else if (freq < -MAXFREQ)
353 			L_LINT(time_freq, -MAXFREQ);
354 		else {
355 			/*
356 			 * ntv.freq is [PPM * 2^16] = [us/s * 2^16]
357 			 * time_freq is [ns/s * 2^32]
358 			 */
359 			time_freq = ntv->freq * 1000LL * 65536LL;
360 		}
361 #ifdef PPS_SYNC
362 		pps_freq = time_freq;
363 #endif /* PPS_SYNC */
364 	}
365 	if (modes & MOD_OFFSET) {
366 		if (time_status & STA_NANO) {
367 			hardupdate(ntv->offset);
368 		} else {
369 			long offset = ntv->offset;
370 			offset = MIN(offset, MAXPHASE/1000);
371 			offset = MAX(offset, -MAXPHASE/1000);
372 			hardupdate(offset * 1000);
373 		}
374 	}
375 
376 	/*
377 	 * Retrieve all clock variables. Note that the TAI offset is
378 	 * returned only by ntp_gettime();
379 	 */
380 	if (time_status & STA_NANO)
381 		ntv->offset = L_GINT(time_offset);
382 	else
383 		ntv->offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */
384 	if (time_freq < 0)
385 		ntv->freq = L_GINT(-((-time_freq / 1000LL) << 16));
386 	else
387 		ntv->freq = L_GINT((time_freq / 1000LL) << 16);
388 	ntv->maxerror = time_maxerror;
389 	ntv->esterror = time_esterror;
390 	ntv->status = time_status;
391 	ntv->constant = time_constant;
392 	if (time_status & STA_NANO)
393 		ntv->precision = time_precision;
394 	else
395 		ntv->precision = time_precision / 1000;
396 	ntv->tolerance = MAXFREQ * SCALE_PPM;
397 #ifdef PPS_SYNC
398 	ntv->shift = pps_shift;
399 	ntv->ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
400 	if (time_status & STA_NANO)
401 		ntv->jitter = pps_jitter;
402 	else
403 		ntv->jitter = pps_jitter / 1000;
404 	ntv->stabil = pps_stabil;
405 	ntv->calcnt = pps_calcnt;
406 	ntv->errcnt = pps_errcnt;
407 	ntv->jitcnt = pps_jitcnt;
408 	ntv->stbcnt = pps_stbcnt;
409 #endif /* PPS_SYNC */
410 	mutex_spin_exit(&timecounter_lock);
411 }
412 #endif /* NTP */
413 
414 /*
415  * second_overflow() - called after ntp_tick_adjust()
416  *
417  * This routine is ordinarily called immediately following the above
418  * routine ntp_tick_adjust(). While these two routines are normally
419  * combined, they are separated here only for the purposes of
420  * simulation.
421  */
422 void
ntp_update_second(int64_t * adjustment,time_t * newsec)423 ntp_update_second(int64_t *adjustment, time_t *newsec)
424 {
425 	int tickrate;
426 	l_fp ftemp;		/* 32/64-bit temporary */
427 
428 	KASSERT(mutex_owned(&timecounter_lock));
429 
430 #ifdef NTP
431 
432 	/*
433 	 * On rollover of the second both the nanosecond and microsecond
434 	 * clocks are updated and the state machine cranked as
435 	 * necessary. The phase adjustment to be used for the next
436 	 * second is calculated and the maximum error is increased by
437 	 * the tolerance.
438 	 */
439 	time_maxerror += MAXFREQ / 1000;
440 
441 	/*
442 	 * Leap second processing. If in leap-insert state at
443 	 * the end of the day, the system clock is set back one
444 	 * second; if in leap-delete state, the system clock is
445 	 * set ahead one second. The nano_time() routine or
446 	 * external clock driver will insure that reported time
447 	 * is always monotonic.
448 	 */
449 	switch (time_state) {
450 
451 		/*
452 		 * No warning.
453 		 */
454 		case TIME_OK:
455 		if (time_status & STA_INS)
456 			time_state = TIME_INS;
457 		else if (time_status & STA_DEL)
458 			time_state = TIME_DEL;
459 		break;
460 
461 		/*
462 		 * Insert second 23:59:60 following second
463 		 * 23:59:59.
464 		 */
465 		case TIME_INS:
466 		if (!(time_status & STA_INS))
467 			time_state = TIME_OK;
468 		else if ((*newsec) % 86400 == 0) {
469 			(*newsec)--;
470 			time_state = TIME_OOP;
471 			time_tai++;
472 		}
473 		break;
474 
475 		/*
476 		 * Delete second 23:59:59.
477 		 */
478 		case TIME_DEL:
479 		if (!(time_status & STA_DEL))
480 			time_state = TIME_OK;
481 		else if (((*newsec) + 1) % 86400 == 0) {
482 			(*newsec)++;
483 			time_tai--;
484 			time_state = TIME_WAIT;
485 		}
486 		break;
487 
488 		/*
489 		 * Insert second in progress.
490 		 */
491 		case TIME_OOP:
492 			time_state = TIME_WAIT;
493 		break;
494 
495 		/*
496 		 * Wait for status bits to clear.
497 		 */
498 		case TIME_WAIT:
499 		if (!(time_status & (STA_INS | STA_DEL)))
500 			time_state = TIME_OK;
501 	}
502 
503 	/*
504 	 * Compute the total time adjustment for the next second
505 	 * in ns. The offset is reduced by a factor depending on
506 	 * whether the PPS signal is operating. Note that the
507 	 * value is in effect scaled by the clock frequency,
508 	 * since the adjustment is added at each tick interrupt.
509 	 */
510 	ftemp = time_offset;
511 #ifdef PPS_SYNC
512 	/* XXX even if PPS signal dies we should finish adjustment ? */
513 	if (time_status & STA_PPSTIME && time_status &
514 	    STA_PPSSIGNAL)
515 		L_RSHIFT(ftemp, pps_shift);
516 	else
517 		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
518 #else
519 		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
520 #endif /* PPS_SYNC */
521 	time_adj = ftemp;
522 	L_SUB(time_offset, ftemp);
523 	L_ADD(time_adj, time_freq);
524 
525 #ifdef PPS_SYNC
526 	if (pps_valid > 0)
527 		pps_valid--;
528 	else
529 		time_status &= ~STA_PPSSIGNAL;
530 #endif /* PPS_SYNC */
531 #else  /* !NTP */
532 	L_CLR(time_adj);
533 #endif /* !NTP */
534 
535 	/*
536 	 * Apply any correction from adjtime(2).  If more than one second
537 	 * off we slew at a rate of 5ms/s (5000 PPM) else 500us/s (500PPM)
538 	 * until the last second is slewed the final < 500 usecs.
539 	 */
540 	if (time_adjtime != 0) {
541 		if (time_adjtime > 1000000)
542 			tickrate = 5000;
543 		else if (time_adjtime < -1000000)
544 			tickrate = -5000;
545 		else if (time_adjtime > 500)
546 			tickrate = 500;
547 		else if (time_adjtime < -500)
548 			tickrate = -500;
549 		else
550 			tickrate = time_adjtime;
551 		time_adjtime -= tickrate;
552 		L_LINT(ftemp, tickrate * 1000);
553 		L_ADD(time_adj, ftemp);
554 	}
555 	*adjustment = time_adj;
556 }
557 
558 /*
559  * ntp_init() - initialize variables and structures
560  *
561  * This routine must be called after the kernel variables hz and tick
562  * are set or changed and before the next tick interrupt. In this
563  * particular implementation, these values are assumed set elsewhere in
564  * the kernel. The design allows the clock frequency and tick interval
565  * to be changed while the system is running. So, this routine should
566  * probably be integrated with the code that does that.
567  */
568 void
ntp_init(void)569 ntp_init(void)
570 {
571 
572 	/*
573 	 * The following variables are initialized only at startup. Only
574 	 * those structures not cleared by the compiler need to be
575 	 * initialized, and these only in the simulator. In the actual
576 	 * kernel, any nonzero values here will quickly evaporate.
577 	 */
578 	L_CLR(time_adj);
579 #ifdef NTP
580 	L_CLR(time_offset);
581 	L_CLR(time_freq);
582 #ifdef PPS_SYNC
583 	pps_tf[0].tv_sec = pps_tf[0].tv_nsec = 0;
584 	pps_tf[1].tv_sec = pps_tf[1].tv_nsec = 0;
585 	pps_tf[2].tv_sec = pps_tf[2].tv_nsec = 0;
586 	pps_fcount = 0;
587 	L_CLR(pps_freq);
588 #endif /* PPS_SYNC */
589 #endif
590 }
591 
592 #ifdef NTP
593 /*
594  * hardupdate() - local clock update
595  *
596  * This routine is called by ntp_adjtime() to update the local clock
597  * phase and frequency. The implementation is of an adaptive-parameter,
598  * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
599  * time and frequency offset estimates for each call. If the kernel PPS
600  * discipline code is configured (PPS_SYNC), the PPS signal itself
601  * determines the new time offset, instead of the calling argument.
602  * Presumably, calls to ntp_adjtime() occur only when the caller
603  * believes the local clock is valid within some bound (+-128 ms with
604  * NTP). If the caller's time is far different than the PPS time, an
605  * argument will ensue, and it's not clear who will lose.
606  *
607  * For uncompensated quartz crystal oscillators and nominal update
608  * intervals less than 256 s, operation should be in phase-lock mode,
609  * where the loop is disciplined to phase. For update intervals greater
610  * than 1024 s, operation should be in frequency-lock mode, where the
611  * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
612  * is selected by the STA_MODE status bit.
613  *
614  * Note: splclock() is in effect.
615  */
616 void
hardupdate(long offset)617 hardupdate(long offset)
618 {
619 	long mtemp;
620 	l_fp ftemp;
621 
622 	KASSERT(mutex_owned(&timecounter_lock));
623 
624 	/*
625 	 * Select how the phase is to be controlled and from which
626 	 * source. If the PPS signal is present and enabled to
627 	 * discipline the time, the PPS offset is used; otherwise, the
628 	 * argument offset is used.
629 	 */
630 	if (!(time_status & STA_PLL))
631 		return;
632 	if (!(time_status & STA_PPSTIME && time_status &
633 	    STA_PPSSIGNAL)) {
634 		if (offset > MAXPHASE)
635 			time_monitor = MAXPHASE;
636 		else if (offset < -MAXPHASE)
637 			time_monitor = -MAXPHASE;
638 		else
639 			time_monitor = offset;
640 		L_LINT(time_offset, time_monitor);
641 	}
642 
643 	/*
644 	 * Select how the frequency is to be controlled and in which
645 	 * mode (PLL or FLL). If the PPS signal is present and enabled
646 	 * to discipline the frequency, the PPS frequency is used;
647 	 * otherwise, the argument offset is used to compute it.
648 	 */
649 	if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) {
650 		time_reftime = time_second;
651 		return;
652 	}
653 	if (time_status & STA_FREQHOLD || time_reftime == 0)
654 		time_reftime = time_second;
655 	mtemp = time_second - time_reftime;
656 	L_LINT(ftemp, time_monitor);
657 	L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
658 	L_MPY(ftemp, mtemp);
659 	L_ADD(time_freq, ftemp);
660 	time_status &= ~STA_MODE;
661 	if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp >
662 	    MAXSEC)) {
663 		L_LINT(ftemp, (time_monitor << 4) / mtemp);
664 		L_RSHIFT(ftemp, SHIFT_FLL + 4);
665 		L_ADD(time_freq, ftemp);
666 		time_status |= STA_MODE;
667 	}
668 	time_reftime = time_second;
669 	if (L_GINT(time_freq) > MAXFREQ)
670 		L_LINT(time_freq, MAXFREQ);
671 	else if (L_GINT(time_freq) < -MAXFREQ)
672 		L_LINT(time_freq, -MAXFREQ);
673 }
674 
675 #ifdef PPS_SYNC
676 /*
677  * hardpps() - discipline CPU clock oscillator to external PPS signal
678  *
679  * This routine is called at each PPS interrupt in order to discipline
680  * the CPU clock oscillator to the PPS signal. It measures the PPS phase
681  * and leaves it in a handy spot for the hardclock() routine. It
682  * integrates successive PPS phase differences and calculates the
683  * frequency offset. This is used in hardclock() to discipline the CPU
684  * clock oscillator so that intrinsic frequency error is cancelled out.
685  * The code requires the caller to capture the time and hardware counter
686  * value at the on-time PPS signal transition.
687  *
688  * Note that, on some Unix systems, this routine runs at an interrupt
689  * priority level higher than the timer interrupt routine hardclock().
690  * Therefore, the variables used are distinct from the hardclock()
691  * variables, except for certain exceptions: The PPS frequency pps_freq
692  * and phase pps_offset variables are determined by this routine and
693  * updated atomically. The time_tolerance variable can be considered a
694  * constant, since it is infrequently changed, and then only when the
695  * PPS signal is disabled. The watchdog counter pps_valid is updated
696  * once per second by hardclock() and is atomically cleared in this
697  * routine.
698  */
699 void
hardpps(struct timespec * tsp,long nsec)700 hardpps(struct timespec *tsp,		/* time at PPS */
701 	long nsec			/* hardware counter at PPS */)
702 {
703 	long u_sec, u_nsec, v_nsec; /* temps */
704 	l_fp ftemp;
705 
706 	KASSERT(mutex_owned(&timecounter_lock));
707 
708 	/*
709 	 * The signal is first processed by a range gate and frequency
710 	 * discriminator. The range gate rejects noise spikes outside
711 	 * the range +-500 us. The frequency discriminator rejects input
712 	 * signals with apparent frequency outside the range 1 +-500
713 	 * PPM. If two hits occur in the same second, we ignore the
714 	 * later hit; if not and a hit occurs outside the range gate,
715 	 * keep the later hit for later comparison, but do not process
716 	 * it.
717 	 */
718 	time_status |= STA_PPSSIGNAL | STA_PPSJITTER;
719 	time_status &= ~(STA_PPSWANDER | STA_PPSERROR);
720 	pps_valid = PPS_VALID;
721 	u_sec = tsp->tv_sec;
722 	u_nsec = tsp->tv_nsec;
723 	if (u_nsec >= (NANOSECOND >> 1)) {
724 		u_nsec -= NANOSECOND;
725 		u_sec++;
726 	}
727 	v_nsec = u_nsec - pps_tf[0].tv_nsec;
728 	if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND -
729 	    MAXFREQ)
730 		return;
731 	pps_tf[2] = pps_tf[1];
732 	pps_tf[1] = pps_tf[0];
733 	pps_tf[0].tv_sec = u_sec;
734 	pps_tf[0].tv_nsec = u_nsec;
735 
736 	/*
737 	 * Compute the difference between the current and previous
738 	 * counter values. If the difference exceeds 0.5 s, assume it
739 	 * has wrapped around, so correct 1.0 s. If the result exceeds
740 	 * the tick interval, the sample point has crossed a tick
741 	 * boundary during the last second, so correct the tick. Very
742 	 * intricate.
743 	 */
744 	u_nsec = nsec;
745 	if (u_nsec > (NANOSECOND >> 1))
746 		u_nsec -= NANOSECOND;
747 	else if (u_nsec < -(NANOSECOND >> 1))
748 		u_nsec += NANOSECOND;
749 	pps_fcount += u_nsec;
750 	if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ)
751 		return;
752 	time_status &= ~STA_PPSJITTER;
753 
754 	/*
755 	 * A three-stage median filter is used to help denoise the PPS
756 	 * time. The median sample becomes the time offset estimate; the
757 	 * difference between the other two samples becomes the time
758 	 * dispersion (jitter) estimate.
759 	 */
760 	if (pps_tf[0].tv_nsec > pps_tf[1].tv_nsec) {
761 		if (pps_tf[1].tv_nsec > pps_tf[2].tv_nsec) {
762 			v_nsec = pps_tf[1].tv_nsec;	/* 0 1 2 */
763 			u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec;
764 		} else if (pps_tf[2].tv_nsec > pps_tf[0].tv_nsec) {
765 			v_nsec = pps_tf[0].tv_nsec;	/* 2 0 1 */
766 			u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec;
767 		} else {
768 			v_nsec = pps_tf[2].tv_nsec;	/* 0 2 1 */
769 			u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec;
770 		}
771 	} else {
772 		if (pps_tf[1].tv_nsec < pps_tf[2].tv_nsec) {
773 			v_nsec = pps_tf[1].tv_nsec;	/* 2 1 0 */
774 			u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec;
775 		} else if (pps_tf[2].tv_nsec < pps_tf[0].tv_nsec) {
776 			v_nsec = pps_tf[0].tv_nsec;	/* 1 0 2 */
777 			u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec;
778 		} else {
779 			v_nsec = pps_tf[2].tv_nsec;	/* 1 2 0 */
780 			u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec;
781 		}
782 	}
783 
784 	/*
785 	 * Nominal jitter is due to PPS signal noise and interrupt
786 	 * latency. If it exceeds the popcorn threshold, the sample is
787 	 * discarded. otherwise, if so enabled, the time offset is
788 	 * updated. We can tolerate a modest loss of data here without
789 	 * much degrading time accuracy.
790 	 */
791 	if (u_nsec > (pps_jitter << PPS_POPCORN)) {
792 		time_status |= STA_PPSJITTER;
793 		pps_jitcnt++;
794 	} else if (time_status & STA_PPSTIME) {
795 		time_monitor = -v_nsec;
796 		L_LINT(time_offset, time_monitor);
797 	}
798 	pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
799 	u_sec = pps_tf[0].tv_sec - pps_lastsec;
800 	if (u_sec < (1 << pps_shift))
801 		return;
802 
803 	/*
804 	 * At the end of the calibration interval the difference between
805 	 * the first and last counter values becomes the scaled
806 	 * frequency. It will later be divided by the length of the
807 	 * interval to determine the frequency update. If the frequency
808 	 * exceeds a sanity threshold, or if the actual calibration
809 	 * interval is not equal to the expected length, the data are
810 	 * discarded. We can tolerate a modest loss of data here without
811 	 * much degrading frequency accuracy.
812 	 */
813 	pps_calcnt++;
814 	v_nsec = -pps_fcount;
815 	pps_lastsec = pps_tf[0].tv_sec;
816 	pps_fcount = 0;
817 	u_nsec = MAXFREQ << pps_shift;
818 	if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 <<
819 	    pps_shift)) {
820 		time_status |= STA_PPSERROR;
821 		pps_errcnt++;
822 		return;
823 	}
824 
825 	/*
826 	 * Here the raw frequency offset and wander (stability) is
827 	 * calculated. If the wander is less than the wander threshold
828 	 * for four consecutive averaging intervals, the interval is
829 	 * doubled; if it is greater than the threshold for four
830 	 * consecutive intervals, the interval is halved. The scaled
831 	 * frequency offset is converted to frequency offset. The
832 	 * stability metric is calculated as the average of recent
833 	 * frequency changes, but is used only for performance
834 	 * monitoring.
835 	 */
836 	L_LINT(ftemp, v_nsec);
837 	L_RSHIFT(ftemp, pps_shift);
838 	L_SUB(ftemp, pps_freq);
839 	u_nsec = L_GINT(ftemp);
840 	if (u_nsec > PPS_MAXWANDER) {
841 		L_LINT(ftemp, PPS_MAXWANDER);
842 		pps_intcnt--;
843 		time_status |= STA_PPSWANDER;
844 		pps_stbcnt++;
845 	} else if (u_nsec < -PPS_MAXWANDER) {
846 		L_LINT(ftemp, -PPS_MAXWANDER);
847 		pps_intcnt--;
848 		time_status |= STA_PPSWANDER;
849 		pps_stbcnt++;
850 	} else {
851 		pps_intcnt++;
852 	}
853 	if (pps_intcnt >= 4) {
854 		pps_intcnt = 4;
855 		if (pps_shift < pps_shiftmax) {
856 			pps_shift++;
857 			pps_intcnt = 0;
858 		}
859 	} else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) {
860 		pps_intcnt = -4;
861 		if (pps_shift > PPS_FAVG) {
862 			pps_shift--;
863 			pps_intcnt = 0;
864 		}
865 	}
866 	if (u_nsec < 0)
867 		u_nsec = -u_nsec;
868 	pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
869 
870 	/*
871 	 * The PPS frequency is recalculated and clamped to the maximum
872 	 * MAXFREQ. If enabled, the system clock frequency is updated as
873 	 * well.
874 	 */
875 	L_ADD(pps_freq, ftemp);
876 	u_nsec = L_GINT(pps_freq);
877 	if (u_nsec > MAXFREQ)
878 		L_LINT(pps_freq, MAXFREQ);
879 	else if (u_nsec < -MAXFREQ)
880 		L_LINT(pps_freq, -MAXFREQ);
881 	if (time_status & STA_PPSFREQ)
882 		time_freq = pps_freq;
883 }
884 #endif /* PPS_SYNC */
885 #endif /* NTP */
886 
887 #ifdef NTP
888 int
ntp_timestatus(void)889 ntp_timestatus(void)
890 {
891 	int rv;
892 
893 	/*
894 	 * Status word error decode. If any of these conditions
895 	 * occur, an error is returned, instead of the status
896 	 * word. Most applications will care only about the fact
897 	 * the system clock may not be trusted, not about the
898 	 * details.
899 	 *
900 	 * Hardware or software error
901 	 */
902 	mutex_spin_enter(&timecounter_lock);
903 	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
904 
905 	/*
906 	 * PPS signal lost when either time or frequency
907 	 * synchronization requested
908 	 */
909 	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
910 	     !(time_status & STA_PPSSIGNAL)) ||
911 
912 	/*
913 	 * PPS jitter exceeded when time synchronization
914 	 * requested
915 	 */
916 	    (time_status & STA_PPSTIME &&
917 	     time_status & STA_PPSJITTER) ||
918 
919 	/*
920 	 * PPS wander exceeded or calibration error when
921 	 * frequency synchronization requested
922 	 */
923 	    (time_status & STA_PPSFREQ &&
924 	     time_status & (STA_PPSWANDER | STA_PPSERROR)))
925 		rv = TIME_ERROR;
926 	else
927 		rv = time_state;
928 	mutex_spin_exit(&timecounter_lock);
929 
930 	return rv;
931 }
932 
933 /*ARGSUSED*/
934 /*
935  * ntp_gettime() - NTP user application interface
936  */
937 int
sys___ntp_gettime50(struct lwp * l,const struct sys___ntp_gettime50_args * uap,register_t * retval)938 sys___ntp_gettime50(struct lwp *l, const struct sys___ntp_gettime50_args *uap, register_t *retval)
939 {
940 	/* {
941 		syscallarg(struct ntptimeval *) ntvp;
942 	} */
943 	struct ntptimeval ntv;
944 	int error = 0;
945 
946 	if (SCARG(uap, ntvp)) {
947 		ntp_gettime(&ntv);
948 
949 		error = copyout((void *)&ntv, (void *)SCARG(uap, ntvp),
950 				sizeof(ntv));
951 	}
952 	if (!error) {
953 		*retval = ntp_timestatus();
954 	}
955 	return(error);
956 }
957 
958 /*
959  * return information about kernel precision timekeeping
960  */
961 static int
sysctl_kern_ntptime(SYSCTLFN_ARGS)962 sysctl_kern_ntptime(SYSCTLFN_ARGS)
963 {
964 	struct sysctlnode node;
965 	struct ntptimeval ntv;
966 
967 	ntp_gettime(&ntv);
968 
969 	node = *rnode;
970 	node.sysctl_data = &ntv;
971 	node.sysctl_size = sizeof(ntv);
972 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
973 }
974 
975 SYSCTL_SETUP(sysctl_kern_ntptime_setup, "sysctl kern.ntptime node setup")
976 {
977 
978 	sysctl_createv(clog, 0, NULL, NULL,
979 		       CTLFLAG_PERMANENT,
980 		       CTLTYPE_STRUCT, "ntptime",
981 		       SYSCTL_DESCR("Kernel clock values for NTP"),
982 		       sysctl_kern_ntptime, 0, NULL,
983 		       sizeof(struct ntptimeval),
984 		       CTL_KERN, KERN_NTPTIME, CTL_EOL);
985 }
986 #endif /* !NTP */
987