Home | History | Annotate | Line # | Download | only in kern
kern_ntptime.c revision 1.35.4.2
      1  1.35.4.2        ad /*	$NetBSD: kern_ntptime.c,v 1.35.4.2 2007/01/30 13:51:40 ad Exp $	*/
      2      1.33    kardel #include <sys/types.h> 	/* XXX to get __HAVE_TIMECOUNTER, remove
      3      1.33    kardel 			   after all ports are converted. */
      4      1.33    kardel #ifdef __HAVE_TIMECOUNTER
      5      1.33    kardel 
      6      1.33    kardel /*-
      7      1.33    kardel  ***********************************************************************
      8      1.33    kardel  *								       *
      9      1.33    kardel  * Copyright (c) David L. Mills 1993-2001			       *
     10      1.33    kardel  *								       *
     11      1.33    kardel  * Permission to use, copy, modify, and distribute this software and   *
     12      1.33    kardel  * its documentation for any purpose and without fee is hereby	       *
     13      1.33    kardel  * granted, provided that the above copyright notice appears in all    *
     14      1.33    kardel  * copies and that both the copyright notice and this permission       *
     15      1.33    kardel  * notice appear in supporting documentation, and that the name	       *
     16      1.33    kardel  * University of Delaware not be used in advertising or publicity      *
     17      1.33    kardel  * pertaining to distribution of the software without specific,	       *
     18      1.33    kardel  * written prior permission. The University of Delaware makes no       *
     19      1.33    kardel  * representations about the suitability this software for any	       *
     20      1.33    kardel  * purpose. It is provided "as is" without express or implied	       *
     21      1.33    kardel  * warranty.							       *
     22      1.33    kardel  *								       *
     23      1.33    kardel  **********************************************************************/
     24       1.1  jonathan 
     25      1.33    kardel /*
     26      1.33    kardel  * Adapted from the original sources for FreeBSD and timecounters by:
     27      1.33    kardel  * Poul-Henning Kamp <phk (at) FreeBSD.org>.
     28      1.33    kardel  *
     29      1.33    kardel  * The 32bit version of the "LP" macros seems a bit past its "sell by"
     30      1.33    kardel  * date so I have retained only the 64bit version and included it directly
     31      1.33    kardel  * in this file.
     32      1.33    kardel  *
     33      1.33    kardel  * Only minor changes done to interface with the timecounters over in
     34      1.33    kardel  * sys/kern/kern_clock.c.   Some of the comments below may be (even more)
     35      1.33    kardel  * confusing and/or plain wrong in that context.
     36      1.33    kardel  */
     37      1.33    kardel 
     38      1.33    kardel #include <sys/cdefs.h>
     39      1.33    kardel /* __FBSDID("$FreeBSD: src/sys/kern/kern_ntptime.c,v 1.59 2005/05/28 14:34:41 rwatson Exp $"); */
     40  1.35.4.2        ad __KERNEL_RCSID(0, "$NetBSD: kern_ntptime.c,v 1.35.4.2 2007/01/30 13:51:40 ad Exp $");
     41      1.33    kardel 
     42      1.33    kardel #include "opt_ntp.h"
     43      1.33    kardel #include "opt_compat_netbsd.h"
     44      1.33    kardel 
     45      1.33    kardel #include <sys/param.h>
     46      1.33    kardel #include <sys/resourcevar.h>
     47      1.33    kardel #include <sys/systm.h>
     48      1.33    kardel #include <sys/kernel.h>
     49      1.33    kardel #include <sys/proc.h>
     50      1.33    kardel #include <sys/sysctl.h>
     51      1.33    kardel #include <sys/timex.h>
     52      1.33    kardel #ifdef COMPAT_30
     53      1.33    kardel #include <compat/sys/timex.h>
     54      1.33    kardel #endif
     55      1.33    kardel #include <sys/vnode.h>
     56      1.33    kardel #include <sys/kauth.h>
     57      1.33    kardel 
     58      1.33    kardel #include <sys/mount.h>
     59      1.33    kardel #include <sys/syscallargs.h>
     60      1.33    kardel 
     61      1.33    kardel #include <machine/cpu.h>
     62      1.33    kardel 
     63      1.33    kardel /*
     64      1.33    kardel  * Single-precision macros for 64-bit machines
     65      1.33    kardel  */
     66      1.33    kardel typedef int64_t l_fp;
     67      1.33    kardel #define L_ADD(v, u)	((v) += (u))
     68      1.33    kardel #define L_SUB(v, u)	((v) -= (u))
     69      1.33    kardel #define L_ADDHI(v, a)	((v) += (int64_t)(a) << 32)
     70      1.33    kardel #define L_NEG(v)	((v) = -(v))
     71      1.33    kardel #define L_RSHIFT(v, n) \
     72      1.33    kardel 	do { \
     73      1.33    kardel 		if ((v) < 0) \
     74      1.33    kardel 			(v) = -(-(v) >> (n)); \
     75      1.33    kardel 		else \
     76      1.33    kardel 			(v) = (v) >> (n); \
     77      1.33    kardel 	} while (0)
     78      1.33    kardel #define L_MPY(v, a)	((v) *= (a))
     79      1.33    kardel #define L_CLR(v)	((v) = 0)
     80      1.33    kardel #define L_ISNEG(v)	((v) < 0)
     81      1.33    kardel #define L_LINT(v, a)	((v) = (int64_t)(a) << 32)
     82      1.33    kardel #define L_GINT(v)	((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
     83      1.33    kardel 
     84      1.33    kardel #ifdef NTP
     85      1.33    kardel /*
     86      1.33    kardel  * Generic NTP kernel interface
     87      1.33    kardel  *
     88      1.33    kardel  * These routines constitute the Network Time Protocol (NTP) interfaces
     89      1.33    kardel  * for user and daemon application programs. The ntp_gettime() routine
     90      1.33    kardel  * provides the time, maximum error (synch distance) and estimated error
     91      1.33    kardel  * (dispersion) to client user application programs. The ntp_adjtime()
     92      1.33    kardel  * routine is used by the NTP daemon to adjust the system clock to an
     93      1.33    kardel  * externally derived time. The time offset and related variables set by
     94      1.33    kardel  * this routine are used by other routines in this module to adjust the
     95      1.33    kardel  * phase and frequency of the clock discipline loop which controls the
     96      1.33    kardel  * system clock.
     97      1.33    kardel  *
     98      1.33    kardel  * When the kernel time is reckoned directly in nanoseconds (NTP_NANO
     99      1.33    kardel  * defined), the time at each tick interrupt is derived directly from
    100      1.33    kardel  * the kernel time variable. When the kernel time is reckoned in
    101      1.33    kardel  * microseconds, (NTP_NANO undefined), the time is derived from the
    102      1.33    kardel  * kernel time variable together with a variable representing the
    103      1.33    kardel  * leftover nanoseconds at the last tick interrupt. In either case, the
    104      1.33    kardel  * current nanosecond time is reckoned from these values plus an
    105      1.33    kardel  * interpolated value derived by the clock routines in another
    106      1.33    kardel  * architecture-specific module. The interpolation can use either a
    107      1.33    kardel  * dedicated counter or a processor cycle counter (PCC) implemented in
    108      1.33    kardel  * some architectures.
    109      1.33    kardel  *
    110      1.33    kardel  * Note that all routines must run at priority splclock or higher.
    111      1.33    kardel  */
    112      1.33    kardel /*
    113      1.33    kardel  * Phase/frequency-lock loop (PLL/FLL) definitions
    114      1.33    kardel  *
    115      1.33    kardel  * The nanosecond clock discipline uses two variable types, time
    116      1.33    kardel  * variables and frequency variables. Both types are represented as 64-
    117      1.33    kardel  * bit fixed-point quantities with the decimal point between two 32-bit
    118      1.33    kardel  * halves. On a 32-bit machine, each half is represented as a single
    119      1.33    kardel  * word and mathematical operations are done using multiple-precision
    120      1.33    kardel  * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
    121      1.33    kardel  * used.
    122      1.33    kardel  *
    123      1.33    kardel  * A time variable is a signed 64-bit fixed-point number in ns and
    124      1.33    kardel  * fraction. It represents the remaining time offset to be amortized
    125      1.33    kardel  * over succeeding tick interrupts. The maximum time offset is about
    126      1.33    kardel  * 0.5 s and the resolution is about 2.3e-10 ns.
    127      1.33    kardel  *
    128      1.33    kardel  *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
    129      1.33    kardel  *  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
    130      1.33    kardel  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    131      1.33    kardel  * |s s s|			 ns				   |
    132      1.33    kardel  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    133      1.33    kardel  * |			    fraction				   |
    134      1.33    kardel  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    135      1.33    kardel  *
    136      1.33    kardel  * A frequency variable is a signed 64-bit fixed-point number in ns/s
    137      1.33    kardel  * and fraction. It represents the ns and fraction to be added to the
    138      1.33    kardel  * kernel time variable at each second. The maximum frequency offset is
    139      1.33    kardel  * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
    140      1.33    kardel  *
    141      1.33    kardel  *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
    142      1.33    kardel  *  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
    143      1.33    kardel  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    144      1.33    kardel  * |s s s s s s s s s s s s s|	          ns/s			   |
    145      1.33    kardel  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    146      1.33    kardel  * |			    fraction				   |
    147      1.33    kardel  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    148      1.33    kardel  */
    149      1.33    kardel /*
    150      1.33    kardel  * The following variables establish the state of the PLL/FLL and the
    151      1.33    kardel  * residual time and frequency offset of the local clock.
    152      1.33    kardel  */
    153      1.33    kardel #define SHIFT_PLL	4		/* PLL loop gain (shift) */
    154      1.33    kardel #define SHIFT_FLL	2		/* FLL loop gain (shift) */
    155      1.33    kardel 
    156      1.33    kardel static int time_state = TIME_OK;	/* clock state */
    157      1.33    kardel static int time_status = STA_UNSYNC;	/* clock status bits */
    158      1.33    kardel static long time_tai;			/* TAI offset (s) */
    159      1.33    kardel static long time_monitor;		/* last time offset scaled (ns) */
    160      1.33    kardel static long time_constant;		/* poll interval (shift) (s) */
    161      1.33    kardel static long time_precision = 1;		/* clock precision (ns) */
    162      1.33    kardel static long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */
    163      1.33    kardel static long time_esterror = MAXPHASE / 1000; /* estimated error (us) */
    164      1.33    kardel static long time_reftime;		/* time at last adjustment (s) */
    165      1.33    kardel static l_fp time_offset;		/* time offset (ns) */
    166      1.33    kardel static l_fp time_freq;			/* frequency offset (ns/s) */
    167      1.33    kardel #endif /* NTP */
    168      1.33    kardel 
    169      1.33    kardel static l_fp time_adj;			/* tick adjust (ns/s) */
    170      1.33    kardel int64_t time_adjtime;		/* correction from adjtime(2) (usec) */
    171      1.33    kardel 
    172      1.33    kardel extern int time_adjusted;	/* ntp might have changed the system time */
    173      1.33    kardel 
    174      1.33    kardel #ifdef NTP
    175      1.33    kardel #ifdef PPS_SYNC
    176      1.33    kardel /*
    177      1.33    kardel  * The following variables are used when a pulse-per-second (PPS) signal
    178      1.33    kardel  * is available and connected via a modem control lead. They establish
    179      1.33    kardel  * the engineering parameters of the clock discipline loop when
    180      1.33    kardel  * controlled by the PPS signal.
    181      1.33    kardel  */
    182      1.33    kardel #define PPS_FAVG	2		/* min freq avg interval (s) (shift) */
    183      1.33    kardel #define PPS_FAVGDEF	8		/* default freq avg int (s) (shift) */
    184      1.33    kardel #define PPS_FAVGMAX	15		/* max freq avg interval (s) (shift) */
    185      1.33    kardel #define PPS_PAVG	4		/* phase avg interval (s) (shift) */
    186      1.33    kardel #define PPS_VALID	120		/* PPS signal watchdog max (s) */
    187      1.33    kardel #define PPS_MAXWANDER	100000		/* max PPS wander (ns/s) */
    188      1.33    kardel #define PPS_POPCORN	2		/* popcorn spike threshold (shift) */
    189      1.33    kardel 
    190      1.33    kardel static struct timespec pps_tf[3];	/* phase median filter */
    191      1.33    kardel static l_fp pps_freq;			/* scaled frequency offset (ns/s) */
    192      1.33    kardel static long pps_fcount;			/* frequency accumulator */
    193      1.33    kardel static long pps_jitter;			/* nominal jitter (ns) */
    194      1.33    kardel static long pps_stabil;			/* nominal stability (scaled ns/s) */
    195      1.33    kardel static long pps_lastsec;		/* time at last calibration (s) */
    196      1.33    kardel static int pps_valid;			/* signal watchdog counter */
    197      1.33    kardel static int pps_shift = PPS_FAVG;	/* interval duration (s) (shift) */
    198      1.33    kardel static int pps_shiftmax = PPS_FAVGDEF;	/* max interval duration (s) (shift) */
    199      1.33    kardel static int pps_intcnt;			/* wander counter */
    200      1.33    kardel 
    201      1.33    kardel /*
    202      1.33    kardel  * PPS signal quality monitors
    203      1.33    kardel  */
    204      1.33    kardel static long pps_calcnt;			/* calibration intervals */
    205      1.33    kardel static long pps_jitcnt;			/* jitter limit exceeded */
    206      1.33    kardel static long pps_stbcnt;			/* stability limit exceeded */
    207      1.33    kardel static long pps_errcnt;			/* calibration errors */
    208      1.33    kardel #endif /* PPS_SYNC */
    209      1.33    kardel /*
    210      1.33    kardel  * End of phase/frequency-lock loop (PLL/FLL) definitions
    211      1.33    kardel  */
    212      1.33    kardel 
    213      1.33    kardel static void hardupdate(long offset);
    214      1.33    kardel 
    215      1.33    kardel /*
    216      1.33    kardel  * ntp_gettime() - NTP user application interface
    217      1.33    kardel  */
    218      1.33    kardel void
    219      1.33    kardel ntp_gettime(ntv)
    220      1.33    kardel 	struct ntptimeval *ntv;
    221      1.33    kardel {
    222      1.33    kardel 	nanotime(&ntv->time);
    223      1.33    kardel 	ntv->maxerror = time_maxerror;
    224      1.33    kardel 	ntv->esterror = time_esterror;
    225      1.33    kardel 	ntv->tai = time_tai;
    226      1.33    kardel 	ntv->time_state = time_state;
    227      1.33    kardel }
    228      1.33    kardel 
    229      1.33    kardel /* ARGSUSED */
    230      1.33    kardel /*
    231      1.33    kardel  * ntp_adjtime() - NTP daemon application interface
    232      1.33    kardel  */
    233      1.33    kardel int
    234      1.33    kardel sys_ntp_adjtime(l, v, retval)
    235      1.33    kardel 	struct lwp *l;
    236      1.33    kardel 	void *v;
    237      1.33    kardel 	register_t *retval;
    238      1.33    kardel {
    239      1.33    kardel 	struct sys_ntp_adjtime_args /* {
    240      1.33    kardel 		syscallarg(struct timex *) tp;
    241      1.33    kardel 	} */ *uap = v;
    242      1.33    kardel 	struct timex ntv;
    243      1.33    kardel 	int error = 0;
    244      1.33    kardel 
    245      1.35        ad 	error = copyin((caddr_t)SCARG(uap, tp), (caddr_t)&ntv, sizeof(ntv));
    246      1.35        ad 	if (error != 0)
    247      1.33    kardel 		return (error);
    248      1.33    kardel 
    249  1.35.4.1        ad 	if (ntv.modes != 0 && (error = kauth_authorize_system(l->l_cred,
    250  1.35.4.1        ad 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_NTPADJTIME, NULL,
    251  1.35.4.1        ad 	    NULL, NULL)) != 0)
    252      1.33    kardel 		return (error);
    253      1.33    kardel 
    254      1.33    kardel 	ntp_adjtime1(&ntv);
    255      1.33    kardel 
    256      1.33    kardel 	error = copyout((caddr_t)&ntv, (caddr_t)SCARG(uap, tp), sizeof(ntv));
    257      1.35        ad 	if (!error)
    258      1.33    kardel 		*retval = ntp_timestatus();
    259      1.35        ad 
    260      1.33    kardel 	return error;
    261      1.33    kardel }
    262      1.33    kardel 
    263      1.33    kardel void
    264      1.33    kardel ntp_adjtime1(ntv)
    265      1.33    kardel 	struct timex *ntv;
    266      1.33    kardel {
    267      1.33    kardel 	long freq;
    268      1.33    kardel 	int modes;
    269      1.33    kardel 	int s;
    270      1.33    kardel 
    271      1.33    kardel 	/*
    272      1.33    kardel 	 * Update selected clock variables - only the superuser can
    273      1.33    kardel 	 * change anything. Note that there is no error checking here on
    274      1.33    kardel 	 * the assumption the superuser should know what it is doing.
    275      1.33    kardel 	 * Note that either the time constant or TAI offset are loaded
    276      1.33    kardel 	 * from the ntv.constant member, depending on the mode bits. If
    277      1.33    kardel 	 * the STA_PLL bit in the status word is cleared, the state and
    278      1.33    kardel 	 * status words are reset to the initial values at boot.
    279      1.33    kardel 	 */
    280      1.33    kardel 	modes = ntv->modes;
    281      1.33    kardel 	if (modes != 0)
    282      1.33    kardel 		/* We need to save the system time during shutdown */
    283      1.33    kardel 		time_adjusted |= 2;
    284      1.33    kardel 	s = splclock();
    285      1.33    kardel 	if (modes & MOD_MAXERROR)
    286      1.33    kardel 		time_maxerror = ntv->maxerror;
    287      1.33    kardel 	if (modes & MOD_ESTERROR)
    288      1.33    kardel 		time_esterror = ntv->esterror;
    289      1.33    kardel 	if (modes & MOD_STATUS) {
    290      1.33    kardel 		if (time_status & STA_PLL && !(ntv->status & STA_PLL)) {
    291      1.33    kardel 			time_state = TIME_OK;
    292      1.33    kardel 			time_status = STA_UNSYNC;
    293      1.33    kardel #ifdef PPS_SYNC
    294      1.33    kardel 			pps_shift = PPS_FAVG;
    295      1.33    kardel #endif /* PPS_SYNC */
    296      1.33    kardel 		}
    297      1.33    kardel 		time_status &= STA_RONLY;
    298      1.33    kardel 		time_status |= ntv->status & ~STA_RONLY;
    299      1.33    kardel 	}
    300      1.33    kardel 	if (modes & MOD_TIMECONST) {
    301      1.33    kardel 		if (ntv->constant < 0)
    302      1.33    kardel 			time_constant = 0;
    303      1.33    kardel 		else if (ntv->constant > MAXTC)
    304      1.33    kardel 			time_constant = MAXTC;
    305      1.33    kardel 		else
    306      1.33    kardel 			time_constant = ntv->constant;
    307      1.33    kardel 	}
    308      1.33    kardel 	if (modes & MOD_TAI) {
    309      1.33    kardel 		if (ntv->constant > 0)	/* XXX zero & negative numbers ? */
    310      1.33    kardel 			time_tai = ntv->constant;
    311      1.33    kardel 	}
    312      1.33    kardel #ifdef PPS_SYNC
    313      1.33    kardel 	if (modes & MOD_PPSMAX) {
    314      1.33    kardel 		if (ntv->shift < PPS_FAVG)
    315      1.33    kardel 			pps_shiftmax = PPS_FAVG;
    316      1.33    kardel 		else if (ntv->shift > PPS_FAVGMAX)
    317      1.33    kardel 			pps_shiftmax = PPS_FAVGMAX;
    318      1.33    kardel 		else
    319      1.33    kardel 			pps_shiftmax = ntv->shift;
    320      1.33    kardel 	}
    321      1.33    kardel #endif /* PPS_SYNC */
    322      1.33    kardel 	if (modes & MOD_NANO)
    323      1.33    kardel 		time_status |= STA_NANO;
    324      1.33    kardel 	if (modes & MOD_MICRO)
    325      1.33    kardel 		time_status &= ~STA_NANO;
    326      1.33    kardel 	if (modes & MOD_CLKB)
    327      1.33    kardel 		time_status |= STA_CLK;
    328      1.33    kardel 	if (modes & MOD_CLKA)
    329      1.33    kardel 		time_status &= ~STA_CLK;
    330      1.33    kardel 	if (modes & MOD_FREQUENCY) {
    331      1.33    kardel 		freq = (ntv->freq * 1000LL) >> 16;
    332      1.33    kardel 		if (freq > MAXFREQ)
    333      1.33    kardel 			L_LINT(time_freq, MAXFREQ);
    334      1.33    kardel 		else if (freq < -MAXFREQ)
    335      1.33    kardel 			L_LINT(time_freq, -MAXFREQ);
    336      1.33    kardel 		else {
    337      1.33    kardel 			/*
    338      1.33    kardel 			 * ntv.freq is [PPM * 2^16] = [us/s * 2^16]
    339      1.33    kardel 			 * time_freq is [ns/s * 2^32]
    340      1.33    kardel 			 */
    341      1.33    kardel 			time_freq = ntv->freq * 1000LL * 65536LL;
    342      1.33    kardel 		}
    343      1.33    kardel #ifdef PPS_SYNC
    344      1.33    kardel 		pps_freq = time_freq;
    345      1.33    kardel #endif /* PPS_SYNC */
    346      1.33    kardel 	}
    347      1.33    kardel 	if (modes & MOD_OFFSET) {
    348      1.33    kardel 		if (time_status & STA_NANO)
    349      1.33    kardel 			hardupdate(ntv->offset);
    350      1.33    kardel 		else
    351      1.33    kardel 			hardupdate(ntv->offset * 1000);
    352      1.33    kardel 	}
    353      1.33    kardel 
    354      1.33    kardel 	/*
    355      1.33    kardel 	 * Retrieve all clock variables. Note that the TAI offset is
    356      1.33    kardel 	 * returned only by ntp_gettime();
    357      1.33    kardel 	 */
    358      1.33    kardel 	if (time_status & STA_NANO)
    359      1.33    kardel 		ntv->offset = L_GINT(time_offset);
    360      1.33    kardel 	else
    361      1.33    kardel 		ntv->offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */
    362      1.33    kardel 	ntv->freq = L_GINT((time_freq / 1000LL) << 16);
    363      1.33    kardel 	ntv->maxerror = time_maxerror;
    364      1.33    kardel 	ntv->esterror = time_esterror;
    365      1.33    kardel 	ntv->status = time_status;
    366      1.33    kardel 	ntv->constant = time_constant;
    367      1.33    kardel 	if (time_status & STA_NANO)
    368      1.33    kardel 		ntv->precision = time_precision;
    369      1.33    kardel 	else
    370      1.33    kardel 		ntv->precision = time_precision / 1000;
    371      1.33    kardel 	ntv->tolerance = MAXFREQ * SCALE_PPM;
    372      1.33    kardel #ifdef PPS_SYNC
    373      1.33    kardel 	ntv->shift = pps_shift;
    374      1.33    kardel 	ntv->ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
    375      1.33    kardel 	if (time_status & STA_NANO)
    376      1.33    kardel 		ntv->jitter = pps_jitter;
    377      1.33    kardel 	else
    378      1.33    kardel 		ntv->jitter = pps_jitter / 1000;
    379      1.33    kardel 	ntv->stabil = pps_stabil;
    380      1.33    kardel 	ntv->calcnt = pps_calcnt;
    381      1.33    kardel 	ntv->errcnt = pps_errcnt;
    382      1.33    kardel 	ntv->jitcnt = pps_jitcnt;
    383      1.33    kardel 	ntv->stbcnt = pps_stbcnt;
    384      1.33    kardel #endif /* PPS_SYNC */
    385      1.33    kardel 	splx(s);
    386      1.33    kardel }
    387      1.33    kardel #endif /* NTP */
    388      1.33    kardel 
    389      1.33    kardel /*
    390      1.33    kardel  * second_overflow() - called after ntp_tick_adjust()
    391      1.33    kardel  *
    392      1.33    kardel  * This routine is ordinarily called immediately following the above
    393      1.33    kardel  * routine ntp_tick_adjust(). While these two routines are normally
    394      1.33    kardel  * combined, they are separated here only for the purposes of
    395      1.33    kardel  * simulation.
    396      1.33    kardel  */
    397      1.33    kardel void
    398      1.33    kardel ntp_update_second(int64_t *adjustment, time_t *newsec)
    399      1.33    kardel {
    400      1.33    kardel 	int tickrate;
    401      1.33    kardel 	l_fp ftemp;		/* 32/64-bit temporary */
    402      1.33    kardel 
    403      1.33    kardel #ifdef NTP
    404      1.33    kardel 
    405      1.33    kardel 	/*
    406      1.33    kardel 	 * On rollover of the second both the nanosecond and microsecond
    407      1.33    kardel 	 * clocks are updated and the state machine cranked as
    408      1.33    kardel 	 * necessary. The phase adjustment to be used for the next
    409      1.33    kardel 	 * second is calculated and the maximum error is increased by
    410      1.33    kardel 	 * the tolerance.
    411      1.33    kardel 	 */
    412      1.33    kardel 	time_maxerror += MAXFREQ / 1000;
    413      1.33    kardel 
    414      1.33    kardel 	/*
    415      1.33    kardel 	 * Leap second processing. If in leap-insert state at
    416      1.33    kardel 	 * the end of the day, the system clock is set back one
    417      1.33    kardel 	 * second; if in leap-delete state, the system clock is
    418      1.33    kardel 	 * set ahead one second. The nano_time() routine or
    419      1.33    kardel 	 * external clock driver will insure that reported time
    420      1.33    kardel 	 * is always monotonic.
    421      1.33    kardel 	 */
    422      1.33    kardel 	switch (time_state) {
    423      1.33    kardel 
    424      1.33    kardel 		/*
    425      1.33    kardel 		 * No warning.
    426      1.33    kardel 		 */
    427      1.33    kardel 		case TIME_OK:
    428      1.33    kardel 		if (time_status & STA_INS)
    429      1.33    kardel 			time_state = TIME_INS;
    430      1.33    kardel 		else if (time_status & STA_DEL)
    431      1.33    kardel 			time_state = TIME_DEL;
    432      1.33    kardel 		break;
    433      1.33    kardel 
    434      1.33    kardel 		/*
    435      1.33    kardel 		 * Insert second 23:59:60 following second
    436      1.33    kardel 		 * 23:59:59.
    437      1.33    kardel 		 */
    438      1.33    kardel 		case TIME_INS:
    439      1.33    kardel 		if (!(time_status & STA_INS))
    440      1.33    kardel 			time_state = TIME_OK;
    441      1.33    kardel 		else if ((*newsec) % 86400 == 0) {
    442      1.33    kardel 			(*newsec)--;
    443      1.33    kardel 			time_state = TIME_OOP;
    444      1.33    kardel 			time_tai++;
    445      1.33    kardel 		}
    446      1.33    kardel 		break;
    447      1.33    kardel 
    448      1.33    kardel 		/*
    449      1.33    kardel 		 * Delete second 23:59:59.
    450      1.33    kardel 		 */
    451      1.33    kardel 		case TIME_DEL:
    452      1.33    kardel 		if (!(time_status & STA_DEL))
    453      1.33    kardel 			time_state = TIME_OK;
    454      1.33    kardel 		else if (((*newsec) + 1) % 86400 == 0) {
    455      1.33    kardel 			(*newsec)++;
    456      1.33    kardel 			time_tai--;
    457      1.33    kardel 			time_state = TIME_WAIT;
    458      1.33    kardel 		}
    459      1.33    kardel 		break;
    460      1.33    kardel 
    461      1.33    kardel 		/*
    462      1.33    kardel 		 * Insert second in progress.
    463      1.33    kardel 		 */
    464      1.33    kardel 		case TIME_OOP:
    465      1.33    kardel 			time_state = TIME_WAIT;
    466      1.33    kardel 		break;
    467      1.33    kardel 
    468      1.33    kardel 		/*
    469      1.33    kardel 		 * Wait for status bits to clear.
    470      1.33    kardel 		 */
    471      1.33    kardel 		case TIME_WAIT:
    472      1.33    kardel 		if (!(time_status & (STA_INS | STA_DEL)))
    473      1.33    kardel 			time_state = TIME_OK;
    474      1.33    kardel 	}
    475      1.33    kardel 
    476      1.33    kardel 	/*
    477      1.33    kardel 	 * Compute the total time adjustment for the next second
    478      1.33    kardel 	 * in ns. The offset is reduced by a factor depending on
    479      1.33    kardel 	 * whether the PPS signal is operating. Note that the
    480      1.33    kardel 	 * value is in effect scaled by the clock frequency,
    481      1.33    kardel 	 * since the adjustment is added at each tick interrupt.
    482      1.33    kardel 	 */
    483      1.33    kardel 	ftemp = time_offset;
    484      1.33    kardel #ifdef PPS_SYNC
    485      1.33    kardel 	/* XXX even if PPS signal dies we should finish adjustment ? */
    486      1.33    kardel 	if (time_status & STA_PPSTIME && time_status &
    487      1.33    kardel 	    STA_PPSSIGNAL)
    488      1.33    kardel 		L_RSHIFT(ftemp, pps_shift);
    489      1.33    kardel 	else
    490      1.33    kardel 		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
    491      1.33    kardel #else
    492      1.33    kardel 		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
    493      1.33    kardel #endif /* PPS_SYNC */
    494      1.33    kardel 	time_adj = ftemp;
    495      1.33    kardel 	L_SUB(time_offset, ftemp);
    496      1.33    kardel 	L_ADD(time_adj, time_freq);
    497      1.33    kardel 
    498      1.33    kardel #ifdef PPS_SYNC
    499      1.33    kardel 	if (pps_valid > 0)
    500      1.33    kardel 		pps_valid--;
    501      1.33    kardel 	else
    502      1.33    kardel 		time_status &= ~STA_PPSSIGNAL;
    503      1.33    kardel #endif /* PPS_SYNC */
    504      1.34    kardel #else  /* !NTP */
    505      1.34    kardel 	L_CLR(time_adj);
    506      1.34    kardel #endif /* !NTP */
    507      1.33    kardel 
    508      1.33    kardel 	/*
    509      1.33    kardel 	 * Apply any correction from adjtime(2).  If more than one second
    510      1.33    kardel 	 * off we slew at a rate of 5ms/s (5000 PPM) else 500us/s (500PPM)
    511      1.33    kardel 	 * until the last second is slewed the final < 500 usecs.
    512      1.33    kardel 	 */
    513      1.33    kardel 	if (time_adjtime != 0) {
    514      1.33    kardel 		if (time_adjtime > 1000000)
    515      1.33    kardel 			tickrate = 5000;
    516      1.33    kardel 		else if (time_adjtime < -1000000)
    517      1.33    kardel 			tickrate = -5000;
    518      1.33    kardel 		else if (time_adjtime > 500)
    519      1.33    kardel 			tickrate = 500;
    520      1.33    kardel 		else if (time_adjtime < -500)
    521      1.33    kardel 			tickrate = -500;
    522      1.33    kardel 		else
    523      1.33    kardel 			tickrate = time_adjtime;
    524      1.33    kardel 		time_adjtime -= tickrate;
    525      1.33    kardel 		L_LINT(ftemp, tickrate * 1000);
    526      1.33    kardel 		L_ADD(time_adj, ftemp);
    527      1.33    kardel 	}
    528      1.33    kardel 	*adjustment = time_adj;
    529      1.33    kardel }
    530      1.33    kardel 
    531      1.33    kardel /*
    532      1.33    kardel  * ntp_init() - initialize variables and structures
    533      1.33    kardel  *
    534      1.33    kardel  * This routine must be called after the kernel variables hz and tick
    535      1.33    kardel  * are set or changed and before the next tick interrupt. In this
    536      1.33    kardel  * particular implementation, these values are assumed set elsewhere in
    537      1.33    kardel  * the kernel. The design allows the clock frequency and tick interval
    538      1.33    kardel  * to be changed while the system is running. So, this routine should
    539      1.33    kardel  * probably be integrated with the code that does that.
    540      1.33    kardel  */
    541      1.33    kardel void
    542      1.33    kardel ntp_init(void)
    543      1.33    kardel {
    544      1.33    kardel 
    545      1.33    kardel 	/*
    546      1.33    kardel 	 * The following variables are initialized only at startup. Only
    547      1.33    kardel 	 * those structures not cleared by the compiler need to be
    548      1.33    kardel 	 * initialized, and these only in the simulator. In the actual
    549      1.33    kardel 	 * kernel, any nonzero values here will quickly evaporate.
    550      1.33    kardel 	 */
    551      1.33    kardel 	L_CLR(time_adj);
    552      1.33    kardel #ifdef NTP
    553      1.33    kardel 	L_CLR(time_offset);
    554      1.33    kardel 	L_CLR(time_freq);
    555      1.33    kardel #ifdef PPS_SYNC
    556      1.33    kardel 	pps_tf[0].tv_sec = pps_tf[0].tv_nsec = 0;
    557      1.33    kardel 	pps_tf[1].tv_sec = pps_tf[1].tv_nsec = 0;
    558      1.33    kardel 	pps_tf[2].tv_sec = pps_tf[2].tv_nsec = 0;
    559      1.33    kardel 	pps_fcount = 0;
    560      1.33    kardel 	L_CLR(pps_freq);
    561      1.33    kardel #endif /* PPS_SYNC */
    562      1.33    kardel #endif
    563      1.33    kardel }
    564      1.33    kardel 
    565      1.33    kardel #ifdef NTP
    566      1.33    kardel /*
    567      1.33    kardel  * hardupdate() - local clock update
    568      1.33    kardel  *
    569      1.33    kardel  * This routine is called by ntp_adjtime() to update the local clock
    570      1.33    kardel  * phase and frequency. The implementation is of an adaptive-parameter,
    571      1.33    kardel  * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
    572      1.33    kardel  * time and frequency offset estimates for each call. If the kernel PPS
    573      1.33    kardel  * discipline code is configured (PPS_SYNC), the PPS signal itself
    574      1.33    kardel  * determines the new time offset, instead of the calling argument.
    575      1.33    kardel  * Presumably, calls to ntp_adjtime() occur only when the caller
    576      1.33    kardel  * believes the local clock is valid within some bound (+-128 ms with
    577      1.33    kardel  * NTP). If the caller's time is far different than the PPS time, an
    578      1.33    kardel  * argument will ensue, and it's not clear who will lose.
    579      1.33    kardel  *
    580      1.33    kardel  * For uncompensated quartz crystal oscillators and nominal update
    581      1.33    kardel  * intervals less than 256 s, operation should be in phase-lock mode,
    582      1.33    kardel  * where the loop is disciplined to phase. For update intervals greater
    583      1.33    kardel  * than 1024 s, operation should be in frequency-lock mode, where the
    584      1.33    kardel  * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
    585      1.33    kardel  * is selected by the STA_MODE status bit.
    586      1.33    kardel  *
    587      1.33    kardel  * Note: splclock() is in effect.
    588      1.33    kardel  */
    589      1.33    kardel void
    590      1.33    kardel hardupdate(long offset)
    591      1.33    kardel {
    592      1.33    kardel 	long mtemp;
    593      1.33    kardel 	l_fp ftemp;
    594      1.33    kardel 
    595      1.33    kardel 	/*
    596      1.33    kardel 	 * Select how the phase is to be controlled and from which
    597      1.33    kardel 	 * source. If the PPS signal is present and enabled to
    598      1.33    kardel 	 * discipline the time, the PPS offset is used; otherwise, the
    599      1.33    kardel 	 * argument offset is used.
    600      1.33    kardel 	 */
    601      1.33    kardel 	if (!(time_status & STA_PLL))
    602      1.33    kardel 		return;
    603      1.33    kardel 	if (!(time_status & STA_PPSTIME && time_status &
    604      1.33    kardel 	    STA_PPSSIGNAL)) {
    605      1.33    kardel 		if (offset > MAXPHASE)
    606      1.33    kardel 			time_monitor = MAXPHASE;
    607      1.33    kardel 		else if (offset < -MAXPHASE)
    608      1.33    kardel 			time_monitor = -MAXPHASE;
    609      1.33    kardel 		else
    610      1.33    kardel 			time_monitor = offset;
    611      1.33    kardel 		L_LINT(time_offset, time_monitor);
    612      1.33    kardel 	}
    613      1.33    kardel 
    614      1.33    kardel 	/*
    615      1.33    kardel 	 * Select how the frequency is to be controlled and in which
    616      1.33    kardel 	 * mode (PLL or FLL). If the PPS signal is present and enabled
    617      1.33    kardel 	 * to discipline the frequency, the PPS frequency is used;
    618      1.33    kardel 	 * otherwise, the argument offset is used to compute it.
    619      1.33    kardel 	 */
    620      1.33    kardel 	if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) {
    621      1.33    kardel 		time_reftime = time_second;
    622      1.33    kardel 		return;
    623      1.33    kardel 	}
    624      1.33    kardel 	if (time_status & STA_FREQHOLD || time_reftime == 0)
    625      1.33    kardel 		time_reftime = time_second;
    626      1.33    kardel 	mtemp = time_second - time_reftime;
    627      1.33    kardel 	L_LINT(ftemp, time_monitor);
    628      1.33    kardel 	L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
    629      1.33    kardel 	L_MPY(ftemp, mtemp);
    630      1.33    kardel 	L_ADD(time_freq, ftemp);
    631      1.33    kardel 	time_status &= ~STA_MODE;
    632      1.33    kardel 	if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp >
    633      1.33    kardel 	    MAXSEC)) {
    634      1.33    kardel 		L_LINT(ftemp, (time_monitor << 4) / mtemp);
    635      1.33    kardel 		L_RSHIFT(ftemp, SHIFT_FLL + 4);
    636      1.33    kardel 		L_ADD(time_freq, ftemp);
    637      1.33    kardel 		time_status |= STA_MODE;
    638      1.33    kardel 	}
    639      1.33    kardel 	time_reftime = time_second;
    640      1.33    kardel 	if (L_GINT(time_freq) > MAXFREQ)
    641      1.33    kardel 		L_LINT(time_freq, MAXFREQ);
    642      1.33    kardel 	else if (L_GINT(time_freq) < -MAXFREQ)
    643      1.33    kardel 		L_LINT(time_freq, -MAXFREQ);
    644      1.33    kardel }
    645      1.33    kardel 
    646      1.33    kardel #ifdef PPS_SYNC
    647      1.33    kardel /*
    648      1.33    kardel  * hardpps() - discipline CPU clock oscillator to external PPS signal
    649      1.33    kardel  *
    650      1.33    kardel  * This routine is called at each PPS interrupt in order to discipline
    651      1.33    kardel  * the CPU clock oscillator to the PPS signal. It measures the PPS phase
    652      1.33    kardel  * and leaves it in a handy spot for the hardclock() routine. It
    653      1.33    kardel  * integrates successive PPS phase differences and calculates the
    654      1.33    kardel  * frequency offset. This is used in hardclock() to discipline the CPU
    655      1.33    kardel  * clock oscillator so that intrinsic frequency error is cancelled out.
    656      1.33    kardel  * The code requires the caller to capture the time and hardware counter
    657      1.33    kardel  * value at the on-time PPS signal transition.
    658      1.33    kardel  *
    659      1.33    kardel  * Note that, on some Unix systems, this routine runs at an interrupt
    660      1.33    kardel  * priority level higher than the timer interrupt routine hardclock().
    661      1.33    kardel  * Therefore, the variables used are distinct from the hardclock()
    662      1.33    kardel  * variables, except for certain exceptions: The PPS frequency pps_freq
    663      1.33    kardel  * and phase pps_offset variables are determined by this routine and
    664      1.33    kardel  * updated atomically. The time_tolerance variable can be considered a
    665      1.33    kardel  * constant, since it is infrequently changed, and then only when the
    666      1.33    kardel  * PPS signal is disabled. The watchdog counter pps_valid is updated
    667      1.33    kardel  * once per second by hardclock() and is atomically cleared in this
    668      1.33    kardel  * routine.
    669      1.33    kardel  */
    670      1.33    kardel void
    671      1.33    kardel hardpps(struct timespec *tsp,		/* time at PPS */
    672      1.33    kardel 	long nsec			/* hardware counter at PPS */)
    673      1.33    kardel {
    674      1.33    kardel 	long u_sec, u_nsec, v_nsec; /* temps */
    675      1.33    kardel 	l_fp ftemp;
    676      1.33    kardel 
    677      1.33    kardel 	/*
    678      1.33    kardel 	 * The signal is first processed by a range gate and frequency
    679      1.33    kardel 	 * discriminator. The range gate rejects noise spikes outside
    680      1.33    kardel 	 * the range +-500 us. The frequency discriminator rejects input
    681      1.33    kardel 	 * signals with apparent frequency outside the range 1 +-500
    682      1.33    kardel 	 * PPM. If two hits occur in the same second, we ignore the
    683      1.33    kardel 	 * later hit; if not and a hit occurs outside the range gate,
    684      1.33    kardel 	 * keep the later hit for later comparison, but do not process
    685      1.33    kardel 	 * it.
    686      1.33    kardel 	 */
    687      1.33    kardel 	time_status |= STA_PPSSIGNAL | STA_PPSJITTER;
    688      1.33    kardel 	time_status &= ~(STA_PPSWANDER | STA_PPSERROR);
    689      1.33    kardel 	pps_valid = PPS_VALID;
    690      1.33    kardel 	u_sec = tsp->tv_sec;
    691      1.33    kardel 	u_nsec = tsp->tv_nsec;
    692      1.33    kardel 	if (u_nsec >= (NANOSECOND >> 1)) {
    693      1.33    kardel 		u_nsec -= NANOSECOND;
    694      1.33    kardel 		u_sec++;
    695      1.33    kardel 	}
    696      1.33    kardel 	v_nsec = u_nsec - pps_tf[0].tv_nsec;
    697      1.33    kardel 	if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND -
    698      1.33    kardel 	    MAXFREQ)
    699      1.33    kardel 		return;
    700      1.33    kardel 	pps_tf[2] = pps_tf[1];
    701      1.33    kardel 	pps_tf[1] = pps_tf[0];
    702      1.33    kardel 	pps_tf[0].tv_sec = u_sec;
    703      1.33    kardel 	pps_tf[0].tv_nsec = u_nsec;
    704      1.33    kardel 
    705      1.33    kardel 	/*
    706      1.33    kardel 	 * Compute the difference between the current and previous
    707      1.33    kardel 	 * counter values. If the difference exceeds 0.5 s, assume it
    708      1.33    kardel 	 * has wrapped around, so correct 1.0 s. If the result exceeds
    709      1.33    kardel 	 * the tick interval, the sample point has crossed a tick
    710      1.33    kardel 	 * boundary during the last second, so correct the tick. Very
    711      1.33    kardel 	 * intricate.
    712      1.33    kardel 	 */
    713      1.33    kardel 	u_nsec = nsec;
    714      1.33    kardel 	if (u_nsec > (NANOSECOND >> 1))
    715      1.33    kardel 		u_nsec -= NANOSECOND;
    716      1.33    kardel 	else if (u_nsec < -(NANOSECOND >> 1))
    717      1.33    kardel 		u_nsec += NANOSECOND;
    718      1.33    kardel 	pps_fcount += u_nsec;
    719      1.33    kardel 	if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ)
    720      1.33    kardel 		return;
    721      1.33    kardel 	time_status &= ~STA_PPSJITTER;
    722      1.33    kardel 
    723      1.33    kardel 	/*
    724      1.33    kardel 	 * A three-stage median filter is used to help denoise the PPS
    725      1.33    kardel 	 * time. The median sample becomes the time offset estimate; the
    726      1.33    kardel 	 * difference between the other two samples becomes the time
    727      1.33    kardel 	 * dispersion (jitter) estimate.
    728      1.33    kardel 	 */
    729      1.33    kardel 	if (pps_tf[0].tv_nsec > pps_tf[1].tv_nsec) {
    730      1.33    kardel 		if (pps_tf[1].tv_nsec > pps_tf[2].tv_nsec) {
    731      1.33    kardel 			v_nsec = pps_tf[1].tv_nsec;	/* 0 1 2 */
    732      1.33    kardel 			u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec;
    733      1.33    kardel 		} else if (pps_tf[2].tv_nsec > pps_tf[0].tv_nsec) {
    734      1.33    kardel 			v_nsec = pps_tf[0].tv_nsec;	/* 2 0 1 */
    735      1.33    kardel 			u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec;
    736      1.33    kardel 		} else {
    737      1.33    kardel 			v_nsec = pps_tf[2].tv_nsec;	/* 0 2 1 */
    738      1.33    kardel 			u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec;
    739      1.33    kardel 		}
    740      1.33    kardel 	} else {
    741      1.33    kardel 		if (pps_tf[1].tv_nsec < pps_tf[2].tv_nsec) {
    742      1.33    kardel 			v_nsec = pps_tf[1].tv_nsec;	/* 2 1 0 */
    743      1.33    kardel 			u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec;
    744      1.33    kardel 		} else if (pps_tf[2].tv_nsec < pps_tf[0].tv_nsec) {
    745      1.33    kardel 			v_nsec = pps_tf[0].tv_nsec;	/* 1 0 2 */
    746      1.33    kardel 			u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec;
    747      1.33    kardel 		} else {
    748      1.33    kardel 			v_nsec = pps_tf[2].tv_nsec;	/* 1 2 0 */
    749      1.33    kardel 			u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec;
    750      1.33    kardel 		}
    751      1.33    kardel 	}
    752      1.33    kardel 
    753      1.33    kardel 	/*
    754      1.33    kardel 	 * Nominal jitter is due to PPS signal noise and interrupt
    755      1.33    kardel 	 * latency. If it exceeds the popcorn threshold, the sample is
    756      1.33    kardel 	 * discarded. otherwise, if so enabled, the time offset is
    757      1.33    kardel 	 * updated. We can tolerate a modest loss of data here without
    758      1.33    kardel 	 * much degrading time accuracy.
    759      1.33    kardel 	 */
    760      1.33    kardel 	if (u_nsec > (pps_jitter << PPS_POPCORN)) {
    761      1.33    kardel 		time_status |= STA_PPSJITTER;
    762      1.33    kardel 		pps_jitcnt++;
    763      1.33    kardel 	} else if (time_status & STA_PPSTIME) {
    764      1.33    kardel 		time_monitor = -v_nsec;
    765      1.33    kardel 		L_LINT(time_offset, time_monitor);
    766      1.33    kardel 	}
    767      1.33    kardel 	pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
    768      1.33    kardel 	u_sec = pps_tf[0].tv_sec - pps_lastsec;
    769      1.33    kardel 	if (u_sec < (1 << pps_shift))
    770      1.33    kardel 		return;
    771      1.33    kardel 
    772      1.33    kardel 	/*
    773      1.33    kardel 	 * At the end of the calibration interval the difference between
    774      1.33    kardel 	 * the first and last counter values becomes the scaled
    775      1.33    kardel 	 * frequency. It will later be divided by the length of the
    776      1.33    kardel 	 * interval to determine the frequency update. If the frequency
    777      1.33    kardel 	 * exceeds a sanity threshold, or if the actual calibration
    778      1.33    kardel 	 * interval is not equal to the expected length, the data are
    779      1.33    kardel 	 * discarded. We can tolerate a modest loss of data here without
    780      1.33    kardel 	 * much degrading frequency accuracy.
    781      1.33    kardel 	 */
    782      1.33    kardel 	pps_calcnt++;
    783      1.33    kardel 	v_nsec = -pps_fcount;
    784      1.33    kardel 	pps_lastsec = pps_tf[0].tv_sec;
    785      1.33    kardel 	pps_fcount = 0;
    786      1.33    kardel 	u_nsec = MAXFREQ << pps_shift;
    787      1.33    kardel 	if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 <<
    788      1.33    kardel 	    pps_shift)) {
    789      1.33    kardel 		time_status |= STA_PPSERROR;
    790      1.33    kardel 		pps_errcnt++;
    791      1.33    kardel 		return;
    792      1.33    kardel 	}
    793      1.33    kardel 
    794      1.33    kardel 	/*
    795      1.33    kardel 	 * Here the raw frequency offset and wander (stability) is
    796      1.33    kardel 	 * calculated. If the wander is less than the wander threshold
    797      1.33    kardel 	 * for four consecutive averaging intervals, the interval is
    798      1.33    kardel 	 * doubled; if it is greater than the threshold for four
    799      1.33    kardel 	 * consecutive intervals, the interval is halved. The scaled
    800      1.33    kardel 	 * frequency offset is converted to frequency offset. The
    801      1.33    kardel 	 * stability metric is calculated as the average of recent
    802      1.33    kardel 	 * frequency changes, but is used only for performance
    803      1.33    kardel 	 * monitoring.
    804      1.33    kardel 	 */
    805      1.33    kardel 	L_LINT(ftemp, v_nsec);
    806      1.33    kardel 	L_RSHIFT(ftemp, pps_shift);
    807      1.33    kardel 	L_SUB(ftemp, pps_freq);
    808      1.33    kardel 	u_nsec = L_GINT(ftemp);
    809      1.33    kardel 	if (u_nsec > PPS_MAXWANDER) {
    810      1.33    kardel 		L_LINT(ftemp, PPS_MAXWANDER);
    811      1.33    kardel 		pps_intcnt--;
    812      1.33    kardel 		time_status |= STA_PPSWANDER;
    813      1.33    kardel 		pps_stbcnt++;
    814      1.33    kardel 	} else if (u_nsec < -PPS_MAXWANDER) {
    815      1.33    kardel 		L_LINT(ftemp, -PPS_MAXWANDER);
    816      1.33    kardel 		pps_intcnt--;
    817      1.33    kardel 		time_status |= STA_PPSWANDER;
    818      1.33    kardel 		pps_stbcnt++;
    819      1.33    kardel 	} else {
    820      1.33    kardel 		pps_intcnt++;
    821      1.33    kardel 	}
    822      1.33    kardel 	if (pps_intcnt >= 4) {
    823      1.33    kardel 		pps_intcnt = 4;
    824      1.33    kardel 		if (pps_shift < pps_shiftmax) {
    825      1.33    kardel 			pps_shift++;
    826      1.33    kardel 			pps_intcnt = 0;
    827      1.33    kardel 		}
    828      1.33    kardel 	} else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) {
    829      1.33    kardel 		pps_intcnt = -4;
    830      1.33    kardel 		if (pps_shift > PPS_FAVG) {
    831      1.33    kardel 			pps_shift--;
    832      1.33    kardel 			pps_intcnt = 0;
    833      1.33    kardel 		}
    834      1.33    kardel 	}
    835      1.33    kardel 	if (u_nsec < 0)
    836      1.33    kardel 		u_nsec = -u_nsec;
    837      1.33    kardel 	pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
    838      1.33    kardel 
    839      1.33    kardel 	/*
    840      1.33    kardel 	 * The PPS frequency is recalculated and clamped to the maximum
    841      1.33    kardel 	 * MAXFREQ. If enabled, the system clock frequency is updated as
    842      1.33    kardel 	 * well.
    843      1.33    kardel 	 */
    844      1.33    kardel 	L_ADD(pps_freq, ftemp);
    845      1.33    kardel 	u_nsec = L_GINT(pps_freq);
    846      1.33    kardel 	if (u_nsec > MAXFREQ)
    847      1.33    kardel 		L_LINT(pps_freq, MAXFREQ);
    848      1.33    kardel 	else if (u_nsec < -MAXFREQ)
    849      1.33    kardel 		L_LINT(pps_freq, -MAXFREQ);
    850      1.33    kardel 	if (time_status & STA_PPSFREQ)
    851      1.33    kardel 		time_freq = pps_freq;
    852      1.33    kardel }
    853      1.33    kardel #endif /* PPS_SYNC */
    854      1.33    kardel #endif /* NTP */
    855      1.33    kardel #else /* !__HAVE_TIMECOUNTER */
    856       1.1  jonathan /******************************************************************************
    857       1.1  jonathan  *                                                                            *
    858       1.1  jonathan  * Copyright (c) David L. Mills 1993, 1994                                    *
    859       1.1  jonathan  *                                                                            *
    860       1.1  jonathan  * Permission to use, copy, modify, and distribute this software and its      *
    861       1.1  jonathan  * documentation for any purpose and without fee is hereby granted, provided  *
    862       1.1  jonathan  * that the above copyright notice appears in all copies and that both the    *
    863       1.1  jonathan  * copyright notice and this permission notice appear in supporting           *
    864       1.1  jonathan  * documentation, and that the name University of Delaware not be used in     *
    865       1.1  jonathan  * advertising or publicity pertaining to distribution of the software        *
    866       1.1  jonathan  * without specific, written prior permission.  The University of Delaware    *
    867       1.1  jonathan  * makes no representations about the suitability this software for any       *
    868       1.1  jonathan  * purpose.  It is provided "as is" without express or implied warranty.      *
    869       1.1  jonathan  *                                                                            *
    870       1.1  jonathan  ******************************************************************************/
    871       1.1  jonathan 
    872       1.1  jonathan /*
    873       1.1  jonathan  * Modification history kern_ntptime.c
    874       1.1  jonathan  *
    875       1.1  jonathan  * 24 Sep 94	David L. Mills
    876       1.1  jonathan  *	Tightened code at exits.
    877       1.1  jonathan  *
    878       1.1  jonathan  * 24 Mar 94	David L. Mills
    879       1.1  jonathan  *	Revised syscall interface to include new variables for PPS
    880       1.1  jonathan  *	time discipline.
    881       1.1  jonathan  *
    882       1.1  jonathan  * 14 Feb 94	David L. Mills
    883       1.1  jonathan  *	Added code for external clock
    884       1.1  jonathan  *
    885       1.1  jonathan  * 28 Nov 93	David L. Mills
    886       1.1  jonathan  *	Revised frequency scaling to conform with adjusted parameters
    887       1.1  jonathan  *
    888       1.1  jonathan  * 17 Sep 93	David L. Mills
    889       1.1  jonathan  *	Created file
    890       1.1  jonathan  */
    891       1.1  jonathan /*
    892       1.1  jonathan  * ntp_gettime(), ntp_adjtime() - precision time interface for SunOS
    893       1.1  jonathan  * V4.1.1 and V4.1.3
    894       1.1  jonathan  *
    895       1.1  jonathan  * These routines consitute the Network Time Protocol (NTP) interfaces
    896       1.1  jonathan  * for user and daemon application programs. The ntp_gettime() routine
    897       1.1  jonathan  * provides the time, maximum error (synch distance) and estimated error
    898       1.1  jonathan  * (dispersion) to client user application programs. The ntp_adjtime()
    899       1.1  jonathan  * routine is used by the NTP daemon to adjust the system clock to an
    900       1.1  jonathan  * externally derived time. The time offset and related variables set by
    901       1.1  jonathan  * this routine are used by hardclock() to adjust the phase and
    902       1.1  jonathan  * frequency of the phase-lock loop which controls the system clock.
    903       1.1  jonathan  */
    904      1.16     lukem 
    905      1.16     lukem #include <sys/cdefs.h>
    906  1.35.4.2        ad __KERNEL_RCSID(0, "$NetBSD: kern_ntptime.c,v 1.35.4.2 2007/01/30 13:51:40 ad Exp $");
    907      1.16     lukem 
    908       1.6  jonathan #include "opt_ntp.h"
    909      1.31  drochner #include "opt_compat_netbsd.h"
    910       1.6  jonathan 
    911       1.1  jonathan #include <sys/param.h>
    912       1.1  jonathan #include <sys/resourcevar.h>
    913       1.1  jonathan #include <sys/systm.h>
    914       1.1  jonathan #include <sys/kernel.h>
    915       1.1  jonathan #include <sys/proc.h>
    916      1.18    simonb #include <sys/sysctl.h>
    917       1.1  jonathan #include <sys/timex.h>
    918      1.31  drochner #ifdef COMPAT_30
    919      1.31  drochner #include <compat/sys/timex.h>
    920      1.31  drochner #endif
    921       1.1  jonathan #include <sys/vnode.h>
    922      1.30      elad #include <sys/kauth.h>
    923       1.1  jonathan 
    924       1.1  jonathan #include <sys/mount.h>
    925       1.1  jonathan #include <sys/syscallargs.h>
    926       1.1  jonathan 
    927       1.1  jonathan #include <machine/cpu.h>
    928       1.2  christos 
    929       1.4   thorpej #ifdef NTP
    930       1.1  jonathan /*
    931       1.1  jonathan  * The following variables are used by the hardclock() routine in the
    932      1.28     perry  * kern_clock.c module and are described in that module.
    933       1.1  jonathan  */
    934       1.1  jonathan extern int time_state;		/* clock state */
    935       1.1  jonathan extern int time_status;		/* clock status bits */
    936       1.1  jonathan extern long time_offset;	/* time adjustment (us) */
    937       1.1  jonathan extern long time_freq;		/* frequency offset (scaled ppm) */
    938       1.1  jonathan extern long time_maxerror;	/* maximum error (us) */
    939       1.1  jonathan extern long time_esterror;	/* estimated error (us) */
    940       1.1  jonathan extern long time_constant;	/* pll time constant */
    941       1.1  jonathan extern long time_precision;	/* clock precision (us) */
    942       1.1  jonathan extern long time_tolerance;	/* frequency tolerance (scaled ppm) */
    943      1.24  drochner extern int time_adjusted;	/* ntp might have changed the system time */
    944       1.1  jonathan 
    945       1.1  jonathan #ifdef PPS_SYNC
    946       1.1  jonathan /*
    947       1.1  jonathan  * The following variables are used only if the PPS signal discipline
    948       1.1  jonathan  * is configured in the kernel.
    949       1.1  jonathan  */
    950       1.1  jonathan extern int pps_shift;		/* interval duration (s) (shift) */
    951       1.1  jonathan extern long pps_freq;		/* pps frequency offset (scaled ppm) */
    952       1.1  jonathan extern long pps_jitter;		/* pps jitter (us) */
    953       1.1  jonathan extern long pps_stabil;		/* pps stability (scaled ppm) */
    954       1.1  jonathan extern long pps_jitcnt;		/* jitter limit exceeded */
    955       1.1  jonathan extern long pps_calcnt;		/* calibration intervals */
    956       1.1  jonathan extern long pps_errcnt;		/* calibration errors */
    957       1.1  jonathan extern long pps_stbcnt;		/* stability limit exceeded */
    958       1.1  jonathan #endif /* PPS_SYNC */
    959       1.1  jonathan 
    960       1.1  jonathan /*ARGSUSED*/
    961       1.1  jonathan /*
    962       1.1  jonathan  * ntp_gettime() - NTP user application interface
    963       1.1  jonathan  */
    964      1.32  drochner void
    965      1.31  drochner ntp_gettime(ntvp)
    966      1.31  drochner 	struct ntptimeval *ntvp;
    967      1.31  drochner {
    968      1.31  drochner 	struct timeval atv;
    969      1.31  drochner 	int s;
    970      1.31  drochner 
    971      1.31  drochner 	memset(ntvp, 0, sizeof(struct ntptimeval));
    972      1.31  drochner 
    973      1.31  drochner 	s = splclock();
    974      1.31  drochner #ifdef EXT_CLOCK
    975      1.31  drochner 	/*
    976      1.31  drochner 	 * The microtime() external clock routine returns a
    977      1.31  drochner 	 * status code. If less than zero, we declare an error
    978      1.31  drochner 	 * in the clock status word and return the kernel
    979      1.31  drochner 	 * (software) time variable. While there are other
    980      1.31  drochner 	 * places that call microtime(), this is the only place
    981      1.31  drochner 	 * that matters from an application point of view.
    982      1.31  drochner 	 */
    983      1.31  drochner 	if (microtime(&atv) < 0) {
    984      1.31  drochner 		time_status |= STA_CLOCKERR;
    985      1.31  drochner 		ntvp->time = time;
    986      1.31  drochner 	} else
    987      1.31  drochner 		time_status &= ~STA_CLOCKERR;
    988      1.31  drochner #else /* EXT_CLOCK */
    989      1.31  drochner 	microtime(&atv);
    990      1.31  drochner #endif /* EXT_CLOCK */
    991      1.31  drochner 	ntvp->maxerror = time_maxerror;
    992      1.31  drochner 	ntvp->esterror = time_esterror;
    993      1.31  drochner 	(void) splx(s);
    994      1.31  drochner 	TIMEVAL_TO_TIMESPEC(&atv, &ntvp->time);
    995      1.31  drochner }
    996      1.33    kardel 
    997       1.1  jonathan 
    998       1.1  jonathan /* ARGSUSED */
    999       1.1  jonathan /*
   1000       1.1  jonathan  * ntp_adjtime() - NTP daemon application interface
   1001       1.1  jonathan  */
   1002       1.1  jonathan int
   1003      1.22   thorpej sys_ntp_adjtime(l, v, retval)
   1004      1.22   thorpej 	struct lwp *l;
   1005       1.1  jonathan 	void *v;
   1006       1.1  jonathan 	register_t *retval;
   1007       1.1  jonathan {
   1008       1.3   thorpej 	struct sys_ntp_adjtime_args /* {
   1009       1.1  jonathan 		syscallarg(struct timex *) tp;
   1010       1.1  jonathan 	} */ *uap = v;
   1011       1.1  jonathan 	struct timex ntv;
   1012       1.1  jonathan 	int error = 0;
   1013       1.1  jonathan 
   1014      1.35        ad 	error = copyin((caddr_t)SCARG(uap, tp), (caddr_t)&ntv, sizeof(ntv));
   1015      1.35        ad 	if (error != 0)
   1016      1.14      manu 		return (error);
   1017      1.14      manu 
   1018  1.35.4.1        ad 	if (ntv.modes != 0 && (error = kauth_authorize_system(l->l_cred,
   1019  1.35.4.1        ad 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_NTPADJTIME, NULL,
   1020  1.35.4.1        ad 	    NULL, NULL)) != 0)
   1021       1.1  jonathan 		return (error);
   1022       1.1  jonathan 
   1023      1.33    kardel 	ntp_adjtime1(&ntv);
   1024      1.33    kardel 
   1025      1.33    kardel 	error = copyout((caddr_t)&ntv, (caddr_t)SCARG(uap, tp), sizeof(ntv));
   1026      1.35        ad 	if (error == 0)
   1027      1.33    kardel 		*retval = ntp_timestatus();
   1028      1.33    kardel 
   1029      1.33    kardel 	return error;
   1030      1.14      manu }
   1031      1.14      manu 
   1032      1.33    kardel void
   1033      1.33    kardel ntp_adjtime1(ntv)
   1034      1.14      manu 	struct timex *ntv;
   1035      1.14      manu {
   1036      1.14      manu 	int modes;
   1037      1.14      manu 	int s;
   1038      1.14      manu 
   1039       1.1  jonathan 	/*
   1040      1.28     perry 	 * Update selected clock variables. Note that there is no error
   1041      1.28     perry 	 * checking here on the assumption the superuser should know
   1042      1.14      manu 	 * what it is doing.
   1043       1.1  jonathan 	 */
   1044      1.15       jmc 	modes = ntv->modes;
   1045      1.23       dsl 	if (modes != 0)
   1046      1.23       dsl 		/* We need to save the system time during shutdown */
   1047      1.23       dsl 		time_adjusted |= 2;
   1048       1.1  jonathan 	s = splclock();
   1049       1.1  jonathan 	if (modes & MOD_FREQUENCY)
   1050       1.1  jonathan #ifdef PPS_SYNC
   1051      1.15       jmc 		time_freq = ntv->freq - pps_freq;
   1052       1.1  jonathan #else /* PPS_SYNC */
   1053      1.15       jmc 		time_freq = ntv->freq;
   1054       1.1  jonathan #endif /* PPS_SYNC */
   1055       1.1  jonathan 	if (modes & MOD_MAXERROR)
   1056      1.15       jmc 		time_maxerror = ntv->maxerror;
   1057       1.1  jonathan 	if (modes & MOD_ESTERROR)
   1058      1.15       jmc 		time_esterror = ntv->esterror;
   1059       1.1  jonathan 	if (modes & MOD_STATUS) {
   1060       1.1  jonathan 		time_status &= STA_RONLY;
   1061      1.15       jmc 		time_status |= ntv->status & ~STA_RONLY;
   1062       1.1  jonathan 	}
   1063       1.1  jonathan 	if (modes & MOD_TIMECONST)
   1064      1.15       jmc 		time_constant = ntv->constant;
   1065       1.1  jonathan 	if (modes & MOD_OFFSET)
   1066      1.15       jmc 		hardupdate(ntv->offset);
   1067       1.1  jonathan 
   1068       1.1  jonathan 	/*
   1069       1.1  jonathan 	 * Retrieve all clock variables
   1070       1.1  jonathan 	 */
   1071       1.1  jonathan 	if (time_offset < 0)
   1072      1.15       jmc 		ntv->offset = -(-time_offset >> SHIFT_UPDATE);
   1073       1.1  jonathan 	else
   1074      1.15       jmc 		ntv->offset = time_offset >> SHIFT_UPDATE;
   1075       1.1  jonathan #ifdef PPS_SYNC
   1076      1.15       jmc 	ntv->freq = time_freq + pps_freq;
   1077       1.1  jonathan #else /* PPS_SYNC */
   1078      1.15       jmc 	ntv->freq = time_freq;
   1079       1.1  jonathan #endif /* PPS_SYNC */
   1080      1.15       jmc 	ntv->maxerror = time_maxerror;
   1081      1.15       jmc 	ntv->esterror = time_esterror;
   1082      1.15       jmc 	ntv->status = time_status;
   1083      1.15       jmc 	ntv->constant = time_constant;
   1084      1.15       jmc 	ntv->precision = time_precision;
   1085      1.15       jmc 	ntv->tolerance = time_tolerance;
   1086       1.1  jonathan #ifdef PPS_SYNC
   1087      1.15       jmc 	ntv->shift = pps_shift;
   1088      1.15       jmc 	ntv->ppsfreq = pps_freq;
   1089      1.15       jmc 	ntv->jitter = pps_jitter >> PPS_AVG;
   1090      1.15       jmc 	ntv->stabil = pps_stabil;
   1091      1.15       jmc 	ntv->calcnt = pps_calcnt;
   1092      1.15       jmc 	ntv->errcnt = pps_errcnt;
   1093      1.15       jmc 	ntv->jitcnt = pps_jitcnt;
   1094      1.15       jmc 	ntv->stbcnt = pps_stbcnt;
   1095       1.1  jonathan #endif /* PPS_SYNC */
   1096       1.1  jonathan 	(void)splx(s);
   1097      1.33    kardel }
   1098      1.33    kardel #endif /* NTP */
   1099      1.33    kardel #endif /* !__HAVE_TIMECOUNTER */
   1100      1.33    kardel 
   1101      1.33    kardel #ifdef NTP
   1102      1.33    kardel int
   1103      1.33    kardel ntp_timestatus()
   1104      1.33    kardel {
   1105      1.33    kardel 	/*
   1106      1.33    kardel 	 * Status word error decode. If any of these conditions
   1107      1.33    kardel 	 * occur, an error is returned, instead of the status
   1108      1.33    kardel 	 * word. Most applications will care only about the fact
   1109      1.33    kardel 	 * the system clock may not be trusted, not about the
   1110      1.33    kardel 	 * details.
   1111      1.33    kardel 	 *
   1112      1.33    kardel 	 * Hardware or software error
   1113      1.33    kardel 	 */
   1114      1.33    kardel 	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
   1115      1.33    kardel 
   1116      1.33    kardel 	/*
   1117      1.33    kardel 	 * PPS signal lost when either time or frequency
   1118      1.33    kardel 	 * synchronization requested
   1119      1.33    kardel 	 */
   1120      1.33    kardel 	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
   1121      1.33    kardel 	     !(time_status & STA_PPSSIGNAL)) ||
   1122      1.33    kardel 
   1123      1.33    kardel 	/*
   1124      1.33    kardel 	 * PPS jitter exceeded when time synchronization
   1125      1.33    kardel 	 * requested
   1126      1.33    kardel 	 */
   1127      1.33    kardel 	    (time_status & STA_PPSTIME &&
   1128      1.33    kardel 	     time_status & STA_PPSJITTER) ||
   1129      1.33    kardel 
   1130      1.33    kardel 	/*
   1131      1.33    kardel 	 * PPS wander exceeded or calibration error when
   1132      1.33    kardel 	 * frequency synchronization requested
   1133      1.33    kardel 	 */
   1134      1.33    kardel 	    (time_status & STA_PPSFREQ &&
   1135      1.33    kardel 	     time_status & (STA_PPSWANDER | STA_PPSERROR)))
   1136      1.33    kardel 		return (TIME_ERROR);
   1137      1.33    kardel 	else
   1138      1.33    kardel 		return (time_state);
   1139      1.33    kardel }
   1140       1.1  jonathan 
   1141      1.33    kardel /*ARGSUSED*/
   1142      1.33    kardel /*
   1143      1.33    kardel  * ntp_gettime() - NTP user application interface
   1144      1.33    kardel  */
   1145      1.33    kardel int
   1146  1.35.4.1        ad sys___ntp_gettime30(struct lwp *l, void *v, register_t *retval)
   1147      1.33    kardel {
   1148      1.33    kardel 	struct sys___ntp_gettime30_args /* {
   1149      1.33    kardel 		syscallarg(struct ntptimeval *) ntvp;
   1150      1.33    kardel 	} */ *uap = v;
   1151      1.33    kardel 	struct ntptimeval ntv;
   1152      1.33    kardel 	int error = 0;
   1153      1.33    kardel 
   1154      1.33    kardel 	if (SCARG(uap, ntvp)) {
   1155      1.33    kardel 		ntp_gettime(&ntv);
   1156      1.33    kardel 
   1157      1.33    kardel 		error = copyout((caddr_t)&ntv, (caddr_t)SCARG(uap, ntvp),
   1158      1.33    kardel 				sizeof(ntv));
   1159      1.33    kardel 	}
   1160       1.1  jonathan 	if (!error) {
   1161      1.33    kardel 		*retval = ntp_timestatus();
   1162      1.33    kardel 	}
   1163      1.33    kardel 	return(error);
   1164      1.33    kardel }
   1165       1.1  jonathan 
   1166      1.33    kardel #ifdef COMPAT_30
   1167      1.33    kardel int
   1168  1.35.4.1        ad compat_30_sys_ntp_gettime(struct lwp *l, void *v, register_t *retval)
   1169      1.33    kardel {
   1170      1.33    kardel 	struct compat_30_sys_ntp_gettime_args /* {
   1171      1.33    kardel 		syscallarg(struct ntptimeval30 *) ontvp;
   1172      1.33    kardel 	} */ *uap = v;
   1173      1.33    kardel 	struct ntptimeval ntv;
   1174      1.33    kardel 	struct ntptimeval30 ontv;
   1175      1.33    kardel 	int error = 0;
   1176      1.33    kardel 
   1177      1.33    kardel 	if (SCARG(uap, ntvp)) {
   1178      1.33    kardel 		ntp_gettime(&ntv);
   1179      1.33    kardel 		TIMESPEC_TO_TIMEVAL(&ontv.time, &ntv.time);
   1180      1.33    kardel 		ontv.maxerror = ntv.maxerror;
   1181      1.33    kardel 		ontv.esterror = ntv.esterror;
   1182      1.33    kardel 
   1183      1.33    kardel 		error = copyout((caddr_t)&ontv, (caddr_t)SCARG(uap, ntvp),
   1184      1.33    kardel 				sizeof(ontv));
   1185      1.33    kardel  	}
   1186      1.33    kardel 	if (!error)
   1187      1.33    kardel 		*retval = ntp_timestatus();
   1188      1.33    kardel 
   1189      1.33    kardel 	return (error);
   1190       1.1  jonathan }
   1191      1.33    kardel #endif
   1192       1.1  jonathan 
   1193       1.1  jonathan /*
   1194       1.1  jonathan  * return information about kernel precision timekeeping
   1195       1.1  jonathan  */
   1196      1.25    atatat static int
   1197      1.25    atatat sysctl_kern_ntptime(SYSCTLFN_ARGS)
   1198       1.1  jonathan {
   1199      1.25    atatat 	struct sysctlnode node;
   1200       1.1  jonathan 	struct ntptimeval ntv;
   1201       1.1  jonathan 
   1202      1.31  drochner 	ntp_gettime(&ntv);
   1203      1.25    atatat 
   1204      1.25    atatat 	node = *rnode;
   1205      1.25    atatat 	node.sysctl_data = &ntv;
   1206      1.25    atatat 	node.sysctl_size = sizeof(ntv);
   1207      1.25    atatat 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1208      1.25    atatat }
   1209      1.25    atatat 
   1210      1.25    atatat SYSCTL_SETUP(sysctl_kern_ntptime_setup, "sysctl kern.ntptime node setup")
   1211      1.25    atatat {
   1212      1.25    atatat 
   1213      1.26    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1214      1.26    atatat 		       CTLFLAG_PERMANENT,
   1215      1.25    atatat 		       CTLTYPE_NODE, "kern", NULL,
   1216      1.25    atatat 		       NULL, 0, NULL, 0,
   1217      1.25    atatat 		       CTL_KERN, CTL_EOL);
   1218      1.25    atatat 
   1219      1.26    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1220      1.26    atatat 		       CTLFLAG_PERMANENT,
   1221      1.27    atatat 		       CTLTYPE_STRUCT, "ntptime",
   1222      1.27    atatat 		       SYSCTL_DESCR("Kernel clock values for NTP"),
   1223      1.25    atatat 		       sysctl_kern_ntptime, 0, NULL,
   1224      1.25    atatat 		       sizeof(struct ntptimeval),
   1225      1.25    atatat 		       CTL_KERN, KERN_NTPTIME, CTL_EOL);
   1226       1.1  jonathan }
   1227       1.4   thorpej #else /* !NTP */
   1228      1.13     bjh21 /* For some reason, raising SIGSYS (as sys_nosys would) is problematic. */
   1229      1.13     bjh21 
   1230       1.4   thorpej int
   1231  1.35.4.1        ad sys___ntp_gettime30(struct lwp *l, void *v, register_t *retval)
   1232      1.31  drochner {
   1233      1.31  drochner 
   1234      1.31  drochner 	return(ENOSYS);
   1235      1.31  drochner }
   1236      1.31  drochner 
   1237      1.31  drochner #ifdef COMPAT_30
   1238      1.31  drochner int
   1239  1.35.4.1        ad compat_30_sys_ntp_gettime(struct lwp *l, void *v, register_t *retval)
   1240       1.4   thorpej {
   1241      1.19    simonb 
   1242      1.33    kardel  	return(ENOSYS);
   1243       1.4   thorpej }
   1244      1.31  drochner #endif
   1245      1.13     bjh21 #endif /* !NTP */
   1246