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