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