Home | History | Annotate | Line # | Download | only in kern
kern_ntptime.c revision 1.57.18.1
      1 /*	$NetBSD: kern_ntptime.c,v 1.57.18.1 2019/06/10 22:09:03 christos 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 (at) 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.57.18.1 2019/06/10 22:09:03 christos 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 extern int time_adjusted;	/* ntp might have changed the system time */
    194 
    195 #ifdef NTP
    196 #ifdef PPS_SYNC
    197 /*
    198  * The following variables are used when a pulse-per-second (PPS) signal
    199  * is available and connected via a modem control lead. They establish
    200  * the engineering parameters of the clock discipline loop when
    201  * controlled by the PPS signal.
    202  */
    203 #define PPS_FAVG	2		/* min freq avg interval (s) (shift) */
    204 #define PPS_FAVGDEF	8		/* default freq avg int (s) (shift) */
    205 #define PPS_FAVGMAX	15		/* max freq avg interval (s) (shift) */
    206 #define PPS_PAVG	4		/* phase avg interval (s) (shift) */
    207 #define PPS_VALID	120		/* PPS signal watchdog max (s) */
    208 #define PPS_MAXWANDER	100000		/* max PPS wander (ns/s) */
    209 #define PPS_POPCORN	2		/* popcorn spike threshold (shift) */
    210 
    211 static struct timespec pps_tf[3];	/* phase median filter */
    212 static l_fp pps_freq;			/* scaled frequency offset (ns/s) */
    213 static long pps_fcount;			/* frequency accumulator */
    214 static long pps_jitter;			/* nominal jitter (ns) */
    215 static long pps_stabil;			/* nominal stability (scaled ns/s) */
    216 static long pps_lastsec;		/* time at last calibration (s) */
    217 static int pps_valid;			/* signal watchdog counter */
    218 static int pps_shift = PPS_FAVG;	/* interval duration (s) (shift) */
    219 static int pps_shiftmax = PPS_FAVGDEF;	/* max interval duration (s) (shift) */
    220 static int pps_intcnt;			/* wander counter */
    221 
    222 /*
    223  * PPS signal quality monitors
    224  */
    225 static long pps_calcnt;			/* calibration intervals */
    226 static long pps_jitcnt;			/* jitter limit exceeded */
    227 static long pps_stbcnt;			/* stability limit exceeded */
    228 static long pps_errcnt;			/* calibration errors */
    229 #endif /* PPS_SYNC */
    230 /*
    231  * End of phase/frequency-lock loop (PLL/FLL) definitions
    232  */
    233 
    234 static void hardupdate(long offset);
    235 
    236 /*
    237  * ntp_gettime() - NTP user application interface
    238  */
    239 void
    240 ntp_gettime(struct ntptimeval *ntv)
    241 {
    242 	memset(ntv, 0, sizeof(*ntv));
    243 
    244 	mutex_spin_enter(&timecounter_lock);
    245 	nanotime(&ntv->time);
    246 	ntv->maxerror = time_maxerror;
    247 	ntv->esterror = time_esterror;
    248 	ntv->tai = time_tai;
    249 	ntv->time_state = time_state;
    250 	mutex_spin_exit(&timecounter_lock);
    251 }
    252 
    253 /* ARGSUSED */
    254 /*
    255  * ntp_adjtime() - NTP daemon application interface
    256  */
    257 int
    258 sys_ntp_adjtime(struct lwp *l, const struct sys_ntp_adjtime_args *uap, register_t *retval)
    259 {
    260 	/* {
    261 		syscallarg(struct timex *) tp;
    262 	} */
    263 	struct timex ntv;
    264 	int error;
    265 
    266 	error = copyin((void *)SCARG(uap, tp), (void *)&ntv, sizeof(ntv));
    267 	if (error != 0)
    268 		return (error);
    269 
    270 	if (ntv.modes != 0 && (error = kauth_authorize_system(l->l_cred,
    271 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_NTPADJTIME, NULL,
    272 	    NULL, NULL)) != 0)
    273 		return (error);
    274 
    275 	ntp_adjtime1(&ntv);
    276 
    277 	error = copyout((void *)&ntv, (void *)SCARG(uap, tp), sizeof(ntv));
    278 	if (!error)
    279 		*retval = ntp_timestatus();
    280 
    281 	return error;
    282 }
    283 
    284 void
    285 ntp_adjtime1(struct timex *ntv)
    286 {
    287 	long freq;
    288 	int modes;
    289 
    290 	/*
    291 	 * Update selected clock variables - only the superuser can
    292 	 * change anything. Note that there is no error checking here on
    293 	 * the assumption the superuser should know what it is doing.
    294 	 * Note that either the time constant or TAI offset are loaded
    295 	 * from the ntv.constant member, depending on the mode bits. If
    296 	 * the STA_PLL bit in the status word is cleared, the state and
    297 	 * status words are reset to the initial values at boot.
    298 	 */
    299 	mutex_spin_enter(&timecounter_lock);
    300 	modes = ntv->modes;
    301 	if (modes != 0)
    302 		/* We need to save the system time during shutdown */
    303 		time_adjusted |= 2;
    304 	if (modes & MOD_MAXERROR)
    305 		time_maxerror = ntv->maxerror;
    306 	if (modes & MOD_ESTERROR)
    307 		time_esterror = ntv->esterror;
    308 	if (modes & MOD_STATUS) {
    309 		if (time_status & STA_PLL && !(ntv->status & STA_PLL)) {
    310 			time_state = TIME_OK;
    311 			time_status = STA_UNSYNC;
    312 #ifdef PPS_SYNC
    313 			pps_shift = PPS_FAVG;
    314 #endif /* PPS_SYNC */
    315 		}
    316 		time_status &= STA_RONLY;
    317 		time_status |= ntv->status & ~STA_RONLY;
    318 	}
    319 	if (modes & MOD_TIMECONST) {
    320 		if (ntv->constant < 0)
    321 			time_constant = 0;
    322 		else if (ntv->constant > MAXTC)
    323 			time_constant = MAXTC;
    324 		else
    325 			time_constant = ntv->constant;
    326 	}
    327 	if (modes & MOD_TAI) {
    328 		if (ntv->constant > 0)	/* XXX zero & negative numbers ? */
    329 			time_tai = ntv->constant;
    330 	}
    331 #ifdef PPS_SYNC
    332 	if (modes & MOD_PPSMAX) {
    333 		if (ntv->shift < PPS_FAVG)
    334 			pps_shiftmax = PPS_FAVG;
    335 		else if (ntv->shift > PPS_FAVGMAX)
    336 			pps_shiftmax = PPS_FAVGMAX;
    337 		else
    338 			pps_shiftmax = ntv->shift;
    339 	}
    340 #endif /* PPS_SYNC */
    341 	if (modes & MOD_NANO)
    342 		time_status |= STA_NANO;
    343 	if (modes & MOD_MICRO)
    344 		time_status &= ~STA_NANO;
    345 	if (modes & MOD_CLKB)
    346 		time_status |= STA_CLK;
    347 	if (modes & MOD_CLKA)
    348 		time_status &= ~STA_CLK;
    349 	if (modes & MOD_FREQUENCY) {
    350 		freq = (ntv->freq * 1000LL) >> 16;
    351 		if (freq > MAXFREQ)
    352 			L_LINT(time_freq, MAXFREQ);
    353 		else if (freq < -MAXFREQ)
    354 			L_LINT(time_freq, -MAXFREQ);
    355 		else {
    356 			/*
    357 			 * ntv.freq is [PPM * 2^16] = [us/s * 2^16]
    358 			 * time_freq is [ns/s * 2^32]
    359 			 */
    360 			time_freq = ntv->freq * 1000LL * 65536LL;
    361 		}
    362 #ifdef PPS_SYNC
    363 		pps_freq = time_freq;
    364 #endif /* PPS_SYNC */
    365 	}
    366 	if (modes & MOD_OFFSET) {
    367 		if (time_status & STA_NANO)
    368 			hardupdate(ntv->offset);
    369 		else
    370 			hardupdate(ntv->offset * 1000);
    371 	}
    372 
    373 	/*
    374 	 * Retrieve all clock variables. Note that the TAI offset is
    375 	 * returned only by ntp_gettime();
    376 	 */
    377 	if (time_status & STA_NANO)
    378 		ntv->offset = L_GINT(time_offset);
    379 	else
    380 		ntv->offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */
    381 	ntv->freq = L_GINT((time_freq / 1000LL) << 16);
    382 	ntv->maxerror = time_maxerror;
    383 	ntv->esterror = time_esterror;
    384 	ntv->status = time_status;
    385 	ntv->constant = time_constant;
    386 	if (time_status & STA_NANO)
    387 		ntv->precision = time_precision;
    388 	else
    389 		ntv->precision = time_precision / 1000;
    390 	ntv->tolerance = MAXFREQ * SCALE_PPM;
    391 #ifdef PPS_SYNC
    392 	ntv->shift = pps_shift;
    393 	ntv->ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
    394 	if (time_status & STA_NANO)
    395 		ntv->jitter = pps_jitter;
    396 	else
    397 		ntv->jitter = pps_jitter / 1000;
    398 	ntv->stabil = pps_stabil;
    399 	ntv->calcnt = pps_calcnt;
    400 	ntv->errcnt = pps_errcnt;
    401 	ntv->jitcnt = pps_jitcnt;
    402 	ntv->stbcnt = pps_stbcnt;
    403 #endif /* PPS_SYNC */
    404 	mutex_spin_exit(&timecounter_lock);
    405 }
    406 #endif /* NTP */
    407 
    408 /*
    409  * second_overflow() - called after ntp_tick_adjust()
    410  *
    411  * This routine is ordinarily called immediately following the above
    412  * routine ntp_tick_adjust(). While these two routines are normally
    413  * combined, they are separated here only for the purposes of
    414  * simulation.
    415  */
    416 void
    417 ntp_update_second(int64_t *adjustment, time_t *newsec)
    418 {
    419 	int tickrate;
    420 	l_fp ftemp;		/* 32/64-bit temporary */
    421 
    422 	KASSERT(mutex_owned(&timecounter_lock));
    423 
    424 #ifdef NTP
    425 
    426 	/*
    427 	 * On rollover of the second both the nanosecond and microsecond
    428 	 * clocks are updated and the state machine cranked as
    429 	 * necessary. The phase adjustment to be used for the next
    430 	 * second is calculated and the maximum error is increased by
    431 	 * the tolerance.
    432 	 */
    433 	time_maxerror += MAXFREQ / 1000;
    434 
    435 	/*
    436 	 * Leap second processing. If in leap-insert state at
    437 	 * the end of the day, the system clock is set back one
    438 	 * second; if in leap-delete state, the system clock is
    439 	 * set ahead one second. The nano_time() routine or
    440 	 * external clock driver will insure that reported time
    441 	 * is always monotonic.
    442 	 */
    443 	switch (time_state) {
    444 
    445 		/*
    446 		 * No warning.
    447 		 */
    448 		case TIME_OK:
    449 		if (time_status & STA_INS)
    450 			time_state = TIME_INS;
    451 		else if (time_status & STA_DEL)
    452 			time_state = TIME_DEL;
    453 		break;
    454 
    455 		/*
    456 		 * Insert second 23:59:60 following second
    457 		 * 23:59:59.
    458 		 */
    459 		case TIME_INS:
    460 		if (!(time_status & STA_INS))
    461 			time_state = TIME_OK;
    462 		else if ((*newsec) % 86400 == 0) {
    463 			(*newsec)--;
    464 			time_state = TIME_OOP;
    465 			time_tai++;
    466 		}
    467 		break;
    468 
    469 		/*
    470 		 * Delete second 23:59:59.
    471 		 */
    472 		case TIME_DEL:
    473 		if (!(time_status & STA_DEL))
    474 			time_state = TIME_OK;
    475 		else if (((*newsec) + 1) % 86400 == 0) {
    476 			(*newsec)++;
    477 			time_tai--;
    478 			time_state = TIME_WAIT;
    479 		}
    480 		break;
    481 
    482 		/*
    483 		 * Insert second in progress.
    484 		 */
    485 		case TIME_OOP:
    486 			time_state = TIME_WAIT;
    487 		break;
    488 
    489 		/*
    490 		 * Wait for status bits to clear.
    491 		 */
    492 		case TIME_WAIT:
    493 		if (!(time_status & (STA_INS | STA_DEL)))
    494 			time_state = TIME_OK;
    495 	}
    496 
    497 	/*
    498 	 * Compute the total time adjustment for the next second
    499 	 * in ns. The offset is reduced by a factor depending on
    500 	 * whether the PPS signal is operating. Note that the
    501 	 * value is in effect scaled by the clock frequency,
    502 	 * since the adjustment is added at each tick interrupt.
    503 	 */
    504 	ftemp = time_offset;
    505 #ifdef PPS_SYNC
    506 	/* XXX even if PPS signal dies we should finish adjustment ? */
    507 	if (time_status & STA_PPSTIME && time_status &
    508 	    STA_PPSSIGNAL)
    509 		L_RSHIFT(ftemp, pps_shift);
    510 	else
    511 		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
    512 #else
    513 		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
    514 #endif /* PPS_SYNC */
    515 	time_adj = ftemp;
    516 	L_SUB(time_offset, ftemp);
    517 	L_ADD(time_adj, time_freq);
    518 
    519 #ifdef PPS_SYNC
    520 	if (pps_valid > 0)
    521 		pps_valid--;
    522 	else
    523 		time_status &= ~STA_PPSSIGNAL;
    524 #endif /* PPS_SYNC */
    525 #else  /* !NTP */
    526 	L_CLR(time_adj);
    527 #endif /* !NTP */
    528 
    529 	/*
    530 	 * Apply any correction from adjtime(2).  If more than one second
    531 	 * off we slew at a rate of 5ms/s (5000 PPM) else 500us/s (500PPM)
    532 	 * until the last second is slewed the final < 500 usecs.
    533 	 */
    534 	if (time_adjtime != 0) {
    535 		if (time_adjtime > 1000000)
    536 			tickrate = 5000;
    537 		else if (time_adjtime < -1000000)
    538 			tickrate = -5000;
    539 		else if (time_adjtime > 500)
    540 			tickrate = 500;
    541 		else if (time_adjtime < -500)
    542 			tickrate = -500;
    543 		else
    544 			tickrate = time_adjtime;
    545 		time_adjtime -= tickrate;
    546 		L_LINT(ftemp, tickrate * 1000);
    547 		L_ADD(time_adj, ftemp);
    548 	}
    549 	*adjustment = time_adj;
    550 }
    551 
    552 /*
    553  * ntp_init() - initialize variables and structures
    554  *
    555  * This routine must be called after the kernel variables hz and tick
    556  * are set or changed and before the next tick interrupt. In this
    557  * particular implementation, these values are assumed set elsewhere in
    558  * the kernel. The design allows the clock frequency and tick interval
    559  * to be changed while the system is running. So, this routine should
    560  * probably be integrated with the code that does that.
    561  */
    562 void
    563 ntp_init(void)
    564 {
    565 
    566 	/*
    567 	 * The following variables are initialized only at startup. Only
    568 	 * those structures not cleared by the compiler need to be
    569 	 * initialized, and these only in the simulator. In the actual
    570 	 * kernel, any nonzero values here will quickly evaporate.
    571 	 */
    572 	L_CLR(time_adj);
    573 #ifdef NTP
    574 	L_CLR(time_offset);
    575 	L_CLR(time_freq);
    576 #ifdef PPS_SYNC
    577 	pps_tf[0].tv_sec = pps_tf[0].tv_nsec = 0;
    578 	pps_tf[1].tv_sec = pps_tf[1].tv_nsec = 0;
    579 	pps_tf[2].tv_sec = pps_tf[2].tv_nsec = 0;
    580 	pps_fcount = 0;
    581 	L_CLR(pps_freq);
    582 #endif /* PPS_SYNC */
    583 #endif
    584 }
    585 
    586 #ifdef NTP
    587 /*
    588  * hardupdate() - local clock update
    589  *
    590  * This routine is called by ntp_adjtime() to update the local clock
    591  * phase and frequency. The implementation is of an adaptive-parameter,
    592  * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
    593  * time and frequency offset estimates for each call. If the kernel PPS
    594  * discipline code is configured (PPS_SYNC), the PPS signal itself
    595  * determines the new time offset, instead of the calling argument.
    596  * Presumably, calls to ntp_adjtime() occur only when the caller
    597  * believes the local clock is valid within some bound (+-128 ms with
    598  * NTP). If the caller's time is far different than the PPS time, an
    599  * argument will ensue, and it's not clear who will lose.
    600  *
    601  * For uncompensated quartz crystal oscillators and nominal update
    602  * intervals less than 256 s, operation should be in phase-lock mode,
    603  * where the loop is disciplined to phase. For update intervals greater
    604  * than 1024 s, operation should be in frequency-lock mode, where the
    605  * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
    606  * is selected by the STA_MODE status bit.
    607  *
    608  * Note: splclock() is in effect.
    609  */
    610 void
    611 hardupdate(long offset)
    612 {
    613 	long mtemp;
    614 	l_fp ftemp;
    615 
    616 	KASSERT(mutex_owned(&timecounter_lock));
    617 
    618 	/*
    619 	 * Select how the phase is to be controlled and from which
    620 	 * source. If the PPS signal is present and enabled to
    621 	 * discipline the time, the PPS offset is used; otherwise, the
    622 	 * argument offset is used.
    623 	 */
    624 	if (!(time_status & STA_PLL))
    625 		return;
    626 	if (!(time_status & STA_PPSTIME && time_status &
    627 	    STA_PPSSIGNAL)) {
    628 		if (offset > MAXPHASE)
    629 			time_monitor = MAXPHASE;
    630 		else if (offset < -MAXPHASE)
    631 			time_monitor = -MAXPHASE;
    632 		else
    633 			time_monitor = offset;
    634 		L_LINT(time_offset, time_monitor);
    635 	}
    636 
    637 	/*
    638 	 * Select how the frequency is to be controlled and in which
    639 	 * mode (PLL or FLL). If the PPS signal is present and enabled
    640 	 * to discipline the frequency, the PPS frequency is used;
    641 	 * otherwise, the argument offset is used to compute it.
    642 	 */
    643 	if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) {
    644 		time_reftime = time_second;
    645 		return;
    646 	}
    647 	if (time_status & STA_FREQHOLD || time_reftime == 0)
    648 		time_reftime = time_second;
    649 	mtemp = time_second - time_reftime;
    650 	L_LINT(ftemp, time_monitor);
    651 	L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
    652 	L_MPY(ftemp, mtemp);
    653 	L_ADD(time_freq, ftemp);
    654 	time_status &= ~STA_MODE;
    655 	if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp >
    656 	    MAXSEC)) {
    657 		L_LINT(ftemp, (time_monitor << 4) / mtemp);
    658 		L_RSHIFT(ftemp, SHIFT_FLL + 4);
    659 		L_ADD(time_freq, ftemp);
    660 		time_status |= STA_MODE;
    661 	}
    662 	time_reftime = time_second;
    663 	if (L_GINT(time_freq) > MAXFREQ)
    664 		L_LINT(time_freq, MAXFREQ);
    665 	else if (L_GINT(time_freq) < -MAXFREQ)
    666 		L_LINT(time_freq, -MAXFREQ);
    667 }
    668 
    669 #ifdef PPS_SYNC
    670 /*
    671  * hardpps() - discipline CPU clock oscillator to external PPS signal
    672  *
    673  * This routine is called at each PPS interrupt in order to discipline
    674  * the CPU clock oscillator to the PPS signal. It measures the PPS phase
    675  * and leaves it in a handy spot for the hardclock() routine. It
    676  * integrates successive PPS phase differences and calculates the
    677  * frequency offset. This is used in hardclock() to discipline the CPU
    678  * clock oscillator so that intrinsic frequency error is cancelled out.
    679  * The code requires the caller to capture the time and hardware counter
    680  * value at the on-time PPS signal transition.
    681  *
    682  * Note that, on some Unix systems, this routine runs at an interrupt
    683  * priority level higher than the timer interrupt routine hardclock().
    684  * Therefore, the variables used are distinct from the hardclock()
    685  * variables, except for certain exceptions: The PPS frequency pps_freq
    686  * and phase pps_offset variables are determined by this routine and
    687  * updated atomically. The time_tolerance variable can be considered a
    688  * constant, since it is infrequently changed, and then only when the
    689  * PPS signal is disabled. The watchdog counter pps_valid is updated
    690  * once per second by hardclock() and is atomically cleared in this
    691  * routine.
    692  */
    693 void
    694 hardpps(struct timespec *tsp,		/* time at PPS */
    695 	long nsec			/* hardware counter at PPS */)
    696 {
    697 	long u_sec, u_nsec, v_nsec; /* temps */
    698 	l_fp ftemp;
    699 
    700 	KASSERT(mutex_owned(&timecounter_lock));
    701 
    702 	/*
    703 	 * The signal is first processed by a range gate and frequency
    704 	 * discriminator. The range gate rejects noise spikes outside
    705 	 * the range +-500 us. The frequency discriminator rejects input
    706 	 * signals with apparent frequency outside the range 1 +-500
    707 	 * PPM. If two hits occur in the same second, we ignore the
    708 	 * later hit; if not and a hit occurs outside the range gate,
    709 	 * keep the later hit for later comparison, but do not process
    710 	 * it.
    711 	 */
    712 	time_status |= STA_PPSSIGNAL | STA_PPSJITTER;
    713 	time_status &= ~(STA_PPSWANDER | STA_PPSERROR);
    714 	pps_valid = PPS_VALID;
    715 	u_sec = tsp->tv_sec;
    716 	u_nsec = tsp->tv_nsec;
    717 	if (u_nsec >= (NANOSECOND >> 1)) {
    718 		u_nsec -= NANOSECOND;
    719 		u_sec++;
    720 	}
    721 	v_nsec = u_nsec - pps_tf[0].tv_nsec;
    722 	if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND -
    723 	    MAXFREQ)
    724 		return;
    725 	pps_tf[2] = pps_tf[1];
    726 	pps_tf[1] = pps_tf[0];
    727 	pps_tf[0].tv_sec = u_sec;
    728 	pps_tf[0].tv_nsec = u_nsec;
    729 
    730 	/*
    731 	 * Compute the difference between the current and previous
    732 	 * counter values. If the difference exceeds 0.5 s, assume it
    733 	 * has wrapped around, so correct 1.0 s. If the result exceeds
    734 	 * the tick interval, the sample point has crossed a tick
    735 	 * boundary during the last second, so correct the tick. Very
    736 	 * intricate.
    737 	 */
    738 	u_nsec = nsec;
    739 	if (u_nsec > (NANOSECOND >> 1))
    740 		u_nsec -= NANOSECOND;
    741 	else if (u_nsec < -(NANOSECOND >> 1))
    742 		u_nsec += NANOSECOND;
    743 	pps_fcount += u_nsec;
    744 	if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ)
    745 		return;
    746 	time_status &= ~STA_PPSJITTER;
    747 
    748 	/*
    749 	 * A three-stage median filter is used to help denoise the PPS
    750 	 * time. The median sample becomes the time offset estimate; the
    751 	 * difference between the other two samples becomes the time
    752 	 * dispersion (jitter) estimate.
    753 	 */
    754 	if (pps_tf[0].tv_nsec > pps_tf[1].tv_nsec) {
    755 		if (pps_tf[1].tv_nsec > pps_tf[2].tv_nsec) {
    756 			v_nsec = pps_tf[1].tv_nsec;	/* 0 1 2 */
    757 			u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec;
    758 		} else if (pps_tf[2].tv_nsec > pps_tf[0].tv_nsec) {
    759 			v_nsec = pps_tf[0].tv_nsec;	/* 2 0 1 */
    760 			u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec;
    761 		} else {
    762 			v_nsec = pps_tf[2].tv_nsec;	/* 0 2 1 */
    763 			u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec;
    764 		}
    765 	} else {
    766 		if (pps_tf[1].tv_nsec < pps_tf[2].tv_nsec) {
    767 			v_nsec = pps_tf[1].tv_nsec;	/* 2 1 0 */
    768 			u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec;
    769 		} else if (pps_tf[2].tv_nsec < pps_tf[0].tv_nsec) {
    770 			v_nsec = pps_tf[0].tv_nsec;	/* 1 0 2 */
    771 			u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec;
    772 		} else {
    773 			v_nsec = pps_tf[2].tv_nsec;	/* 1 2 0 */
    774 			u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec;
    775 		}
    776 	}
    777 
    778 	/*
    779 	 * Nominal jitter is due to PPS signal noise and interrupt
    780 	 * latency. If it exceeds the popcorn threshold, the sample is
    781 	 * discarded. otherwise, if so enabled, the time offset is
    782 	 * updated. We can tolerate a modest loss of data here without
    783 	 * much degrading time accuracy.
    784 	 */
    785 	if (u_nsec > (pps_jitter << PPS_POPCORN)) {
    786 		time_status |= STA_PPSJITTER;
    787 		pps_jitcnt++;
    788 	} else if (time_status & STA_PPSTIME) {
    789 		time_monitor = -v_nsec;
    790 		L_LINT(time_offset, time_monitor);
    791 	}
    792 	pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
    793 	u_sec = pps_tf[0].tv_sec - pps_lastsec;
    794 	if (u_sec < (1 << pps_shift))
    795 		return;
    796 
    797 	/*
    798 	 * At the end of the calibration interval the difference between
    799 	 * the first and last counter values becomes the scaled
    800 	 * frequency. It will later be divided by the length of the
    801 	 * interval to determine the frequency update. If the frequency
    802 	 * exceeds a sanity threshold, or if the actual calibration
    803 	 * interval is not equal to the expected length, the data are
    804 	 * discarded. We can tolerate a modest loss of data here without
    805 	 * much degrading frequency accuracy.
    806 	 */
    807 	pps_calcnt++;
    808 	v_nsec = -pps_fcount;
    809 	pps_lastsec = pps_tf[0].tv_sec;
    810 	pps_fcount = 0;
    811 	u_nsec = MAXFREQ << pps_shift;
    812 	if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 <<
    813 	    pps_shift)) {
    814 		time_status |= STA_PPSERROR;
    815 		pps_errcnt++;
    816 		return;
    817 	}
    818 
    819 	/*
    820 	 * Here the raw frequency offset and wander (stability) is
    821 	 * calculated. If the wander is less than the wander threshold
    822 	 * for four consecutive averaging intervals, the interval is
    823 	 * doubled; if it is greater than the threshold for four
    824 	 * consecutive intervals, the interval is halved. The scaled
    825 	 * frequency offset is converted to frequency offset. The
    826 	 * stability metric is calculated as the average of recent
    827 	 * frequency changes, but is used only for performance
    828 	 * monitoring.
    829 	 */
    830 	L_LINT(ftemp, v_nsec);
    831 	L_RSHIFT(ftemp, pps_shift);
    832 	L_SUB(ftemp, pps_freq);
    833 	u_nsec = L_GINT(ftemp);
    834 	if (u_nsec > PPS_MAXWANDER) {
    835 		L_LINT(ftemp, PPS_MAXWANDER);
    836 		pps_intcnt--;
    837 		time_status |= STA_PPSWANDER;
    838 		pps_stbcnt++;
    839 	} else if (u_nsec < -PPS_MAXWANDER) {
    840 		L_LINT(ftemp, -PPS_MAXWANDER);
    841 		pps_intcnt--;
    842 		time_status |= STA_PPSWANDER;
    843 		pps_stbcnt++;
    844 	} else {
    845 		pps_intcnt++;
    846 	}
    847 	if (pps_intcnt >= 4) {
    848 		pps_intcnt = 4;
    849 		if (pps_shift < pps_shiftmax) {
    850 			pps_shift++;
    851 			pps_intcnt = 0;
    852 		}
    853 	} else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) {
    854 		pps_intcnt = -4;
    855 		if (pps_shift > PPS_FAVG) {
    856 			pps_shift--;
    857 			pps_intcnt = 0;
    858 		}
    859 	}
    860 	if (u_nsec < 0)
    861 		u_nsec = -u_nsec;
    862 	pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
    863 
    864 	/*
    865 	 * The PPS frequency is recalculated and clamped to the maximum
    866 	 * MAXFREQ. If enabled, the system clock frequency is updated as
    867 	 * well.
    868 	 */
    869 	L_ADD(pps_freq, ftemp);
    870 	u_nsec = L_GINT(pps_freq);
    871 	if (u_nsec > MAXFREQ)
    872 		L_LINT(pps_freq, MAXFREQ);
    873 	else if (u_nsec < -MAXFREQ)
    874 		L_LINT(pps_freq, -MAXFREQ);
    875 	if (time_status & STA_PPSFREQ)
    876 		time_freq = pps_freq;
    877 }
    878 #endif /* PPS_SYNC */
    879 #endif /* NTP */
    880 
    881 #ifdef NTP
    882 int
    883 ntp_timestatus(void)
    884 {
    885 	int rv;
    886 
    887 	/*
    888 	 * Status word error decode. If any of these conditions
    889 	 * occur, an error is returned, instead of the status
    890 	 * word. Most applications will care only about the fact
    891 	 * the system clock may not be trusted, not about the
    892 	 * details.
    893 	 *
    894 	 * Hardware or software error
    895 	 */
    896 	mutex_spin_enter(&timecounter_lock);
    897 	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
    898 
    899 	/*
    900 	 * PPS signal lost when either time or frequency
    901 	 * synchronization requested
    902 	 */
    903 	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
    904 	     !(time_status & STA_PPSSIGNAL)) ||
    905 
    906 	/*
    907 	 * PPS jitter exceeded when time synchronization
    908 	 * requested
    909 	 */
    910 	    (time_status & STA_PPSTIME &&
    911 	     time_status & STA_PPSJITTER) ||
    912 
    913 	/*
    914 	 * PPS wander exceeded or calibration error when
    915 	 * frequency synchronization requested
    916 	 */
    917 	    (time_status & STA_PPSFREQ &&
    918 	     time_status & (STA_PPSWANDER | STA_PPSERROR)))
    919 		rv = TIME_ERROR;
    920 	else
    921 		rv = time_state;
    922 	mutex_spin_exit(&timecounter_lock);
    923 
    924 	return rv;
    925 }
    926 
    927 /*ARGSUSED*/
    928 /*
    929  * ntp_gettime() - NTP user application interface
    930  */
    931 int
    932 sys___ntp_gettime50(struct lwp *l, const struct sys___ntp_gettime50_args *uap, register_t *retval)
    933 {
    934 	/* {
    935 		syscallarg(struct ntptimeval *) ntvp;
    936 	} */
    937 	struct ntptimeval ntv;
    938 	int error = 0;
    939 
    940 	if (SCARG(uap, ntvp)) {
    941 		ntp_gettime(&ntv);
    942 
    943 		error = copyout((void *)&ntv, (void *)SCARG(uap, ntvp),
    944 				sizeof(ntv));
    945 	}
    946 	if (!error) {
    947 		*retval = ntp_timestatus();
    948 	}
    949 	return(error);
    950 }
    951 
    952 /*
    953  * return information about kernel precision timekeeping
    954  */
    955 static int
    956 sysctl_kern_ntptime(SYSCTLFN_ARGS)
    957 {
    958 	struct sysctlnode node;
    959 	struct ntptimeval ntv;
    960 
    961 	ntp_gettime(&ntv);
    962 
    963 	node = *rnode;
    964 	node.sysctl_data = &ntv;
    965 	node.sysctl_size = sizeof(ntv);
    966 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    967 }
    968 
    969 SYSCTL_SETUP(sysctl_kern_ntptime_setup, "sysctl kern.ntptime node setup")
    970 {
    971 
    972 	sysctl_createv(clog, 0, NULL, NULL,
    973 		       CTLFLAG_PERMANENT,
    974 		       CTLTYPE_STRUCT, "ntptime",
    975 		       SYSCTL_DESCR("Kernel clock values for NTP"),
    976 		       sysctl_kern_ntptime, 0, NULL,
    977 		       sizeof(struct ntptimeval),
    978 		       CTL_KERN, KERN_NTPTIME, CTL_EOL);
    979 }
    980 #endif /* !NTP */
    981