kern_ntptime.c revision 95036
144574Sphk/***********************************************************************
244574Sphk *								       *
375540Sjhay * Copyright (c) David L. Mills 1993-2001			       *
444574Sphk *								       *
544574Sphk * Permission to use, copy, modify, and distribute this software and   *
644574Sphk * its documentation for any purpose and without fee is hereby	       *
744574Sphk * granted, provided that the above copyright notice appears in all    *
844574Sphk * copies and that both the copyright notice and this permission       *
944574Sphk * notice appear in supporting documentation, and that the name	       *
1044574Sphk * University of Delaware not be used in advertising or publicity      *
1144574Sphk * pertaining to distribution of the software without specific,	       *
1244574Sphk * written prior permission. The University of Delaware makes no       *
1344574Sphk * representations about the suitability this software for any	       *
1444574Sphk * purpose. It is provided "as is" without express or implied	       *
1544574Sphk * warranty.							       *
1644574Sphk *								       *
1744574Sphk **********************************************************************/
182858Swollman
192858Swollman/*
2044574Sphk * Adapted from the original sources for FreeBSD and timecounters by:
2144666Sphk * Poul-Henning Kamp <phk@FreeBSD.org>.
222858Swollman *
2344574Sphk * The 32bit version of the "LP" macros seems a bit past its "sell by"
2444574Sphk * date so I have retained only the 64bit version and included it directly
2544574Sphk * in this file.
2621101Sjhay *
2744574Sphk * Only minor changes done to interface with the timecounters over in
2844574Sphk * sys/kern/kern_clock.c.   Some of the comments below may be (even more)
2944574Sphk * confusing and/or plain wrong in that context.
3055219Sphk *
3155219Sphk * $FreeBSD: head/sys/kern/kern_ntptime.c 95036 2002-04-19 09:20:13Z phk $
322858Swollman */
3332925Seivind
3444666Sphk#include "opt_ntp.h"
3544666Sphk
362858Swollman#include <sys/param.h>
372858Swollman#include <sys/systm.h>
3812221Sbde#include <sys/sysproto.h>
392858Swollman#include <sys/kernel.h>
402858Swollman#include <sys/proc.h>
4182717Sdillon#include <sys/lock.h>
4282717Sdillon#include <sys/mutex.h>
4344574Sphk#include <sys/time.h>
442858Swollman#include <sys/timex.h>
4558377Sphk#include <sys/timetc.h>
4636941Sphk#include <sys/timepps.h>
472858Swollman#include <sys/sysctl.h>
482858Swollman
492858Swollman/*
5044574Sphk * Single-precision macros for 64-bit machines
5144574Sphk */
5244574Sphktypedef long long l_fp;
5344574Sphk#define L_ADD(v, u)	((v) += (u))
5444574Sphk#define L_SUB(v, u)	((v) -= (u))
5544574Sphk#define L_ADDHI(v, a)	((v) += (long long)(a) << 32)
5644574Sphk#define L_NEG(v)	((v) = -(v))
5744574Sphk#define L_RSHIFT(v, n) \
5844574Sphk	do { \
5944574Sphk		if ((v) < 0) \
6044574Sphk			(v) = -(-(v) >> (n)); \
6144574Sphk		else \
6244574Sphk			(v) = (v) >> (n); \
6344574Sphk	} while (0)
6444574Sphk#define L_MPY(v, a)	((v) *= (a))
6544574Sphk#define L_CLR(v)	((v) = 0)
6644574Sphk#define L_ISNEG(v)	((v) < 0)
6744574Sphk#define L_LINT(v, a)	((v) = (long long)(a) << 32)
6844574Sphk#define L_GINT(v)	((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
6944574Sphk
7044574Sphk/*
7144574Sphk * Generic NTP kernel interface
7232513Sphk *
7344574Sphk * These routines constitute the Network Time Protocol (NTP) interfaces
7444574Sphk * for user and daemon application programs. The ntp_gettime() routine
7544574Sphk * provides the time, maximum error (synch distance) and estimated error
7644574Sphk * (dispersion) to client user application programs. The ntp_adjtime()
7744574Sphk * routine is used by the NTP daemon to adjust the system clock to an
7844574Sphk * externally derived time. The time offset and related variables set by
7944574Sphk * this routine are used by other routines in this module to adjust the
8044574Sphk * phase and frequency of the clock discipline loop which controls the
8144574Sphk * system clock.
8232513Sphk *
8345294Sphk * When the kernel time is reckoned directly in nanoseconds (NTP_NANO
8444574Sphk * defined), the time at each tick interrupt is derived directly from
8544574Sphk * the kernel time variable. When the kernel time is reckoned in
8645294Sphk * microseconds, (NTP_NANO undefined), the time is derived from the
8745294Sphk * kernel time variable together with a variable representing the
8845294Sphk * leftover nanoseconds at the last tick interrupt. In either case, the
8945294Sphk * current nanosecond time is reckoned from these values plus an
9045294Sphk * interpolated value derived by the clock routines in another
9145294Sphk * architecture-specific module. The interpolation can use either a
9245294Sphk * dedicated counter or a processor cycle counter (PCC) implemented in
9345294Sphk * some architectures.
9432513Sphk *
9544574Sphk * Note that all routines must run at priority splclock or higher.
9644574Sphk */
9744574Sphk/*
9844574Sphk * Phase/frequency-lock loop (PLL/FLL) definitions
9932513Sphk *
10044574Sphk * The nanosecond clock discipline uses two variable types, time
10144574Sphk * variables and frequency variables. Both types are represented as 64-
10244574Sphk * bit fixed-point quantities with the decimal point between two 32-bit
10344574Sphk * halves. On a 32-bit machine, each half is represented as a single
10444574Sphk * word and mathematical operations are done using multiple-precision
10544574Sphk * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
10644574Sphk * used.
10732513Sphk *
10844574Sphk * A time variable is a signed 64-bit fixed-point number in ns and
10944574Sphk * fraction. It represents the remaining time offset to be amortized
11044574Sphk * over succeeding tick interrupts. The maximum time offset is about
11145294Sphk * 0.5 s and the resolution is about 2.3e-10 ns.
11232513Sphk *
11344574Sphk *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
11444574Sphk *  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
11544574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11644574Sphk * |s s s|			 ns				   |
11744574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11844574Sphk * |			    fraction				   |
11944574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12032513Sphk *
12144574Sphk * A frequency variable is a signed 64-bit fixed-point number in ns/s
12244574Sphk * and fraction. It represents the ns and fraction to be added to the
12344574Sphk * kernel time variable at each second. The maximum frequency offset is
12445294Sphk * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
12532513Sphk *
12644574Sphk *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
12744574Sphk *  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
12844574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12944574Sphk * |s s s s s s s s s s s s s|	          ns/s			   |
13044574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13144574Sphk * |			    fraction				   |
13244574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1332858Swollman */
13432513Sphk/*
13532513Sphk * The following variables establish the state of the PLL/FLL and the
13644574Sphk * residual time and frequency offset of the local clock.
13732513Sphk */
13844574Sphk#define SHIFT_PLL	4		/* PLL loop gain (shift) */
13944574Sphk#define SHIFT_FLL	2		/* FLL loop gain (shift) */
14032513Sphk
14144574Sphkstatic int time_state = TIME_OK;	/* clock state */
14244574Sphkstatic int time_status = STA_UNSYNC;	/* clock status bits */
14365432Sphkstatic long time_tai;			/* TAI offset (s) */
14465432Sphkstatic long time_monitor;		/* last time offset scaled (ns) */
14544574Sphkstatic long time_constant;		/* poll interval (shift) (s) */
14644574Sphkstatic long time_precision = 1;		/* clock precision (ns) */
14744574Sphkstatic long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */
14844574Sphkstatic long time_esterror = MAXPHASE / 1000; /* estimated error (us) */
14944574Sphkstatic long time_reftime;		/* time at last adjustment (s) */
15044574Sphkstatic long time_tick;			/* nanoseconds per tick (ns) */
15144574Sphkstatic l_fp time_offset;		/* time offset (ns) */
15244574Sphkstatic l_fp time_freq;			/* frequency offset (ns/s) */
15365432Sphkstatic l_fp time_adj;			/* tick adjust (ns/s) */
15444574Sphk
15594754Sphkstatic int64_t time_adjtime;		/* correction from adjtime(2) (usec) */
15694754Sphk
1572858Swollman#ifdef PPS_SYNC
1582858Swollman/*
15944574Sphk * The following variables are used when a pulse-per-second (PPS) signal
16044574Sphk * is available and connected via a modem control lead. They establish
16144574Sphk * the engineering parameters of the clock discipline loop when
16244574Sphk * controlled by the PPS signal.
1632858Swollman */
16444574Sphk#define PPS_FAVG	2		/* min freq avg interval (s) (shift) */
16575540Sjhay#define PPS_FAVGDEF	8		/* default freq avg int (s) (shift) */
16650656Sphk#define PPS_FAVGMAX	15		/* max freq avg interval (s) (shift) */
16744574Sphk#define PPS_PAVG	4		/* phase avg interval (s) (shift) */
16844574Sphk#define PPS_VALID	120		/* PPS signal watchdog max (s) */
16950656Sphk#define PPS_MAXWANDER	100000		/* max PPS wander (ns/s) */
17050656Sphk#define PPS_POPCORN	2		/* popcorn spike threshold (shift) */
17132513Sphk
17250656Sphkstatic struct timespec pps_tf[3];	/* phase median filter */
17344574Sphkstatic l_fp pps_freq;			/* scaled frequency offset (ns/s) */
17445294Sphkstatic long pps_fcount;			/* frequency accumulator */
17550656Sphkstatic long pps_jitter;			/* nominal jitter (ns) */
17650656Sphkstatic long pps_stabil;			/* nominal stability (scaled ns/s) */
17744574Sphkstatic long pps_lastsec;		/* time at last calibration (s) */
17844574Sphkstatic int pps_valid;			/* signal watchdog counter */
17944574Sphkstatic int pps_shift = PPS_FAVG;	/* interval duration (s) (shift) */
18050656Sphkstatic int pps_shiftmax = PPS_FAVGDEF;	/* max interval duration (s) (shift) */
18144574Sphkstatic int pps_intcnt;			/* wander counter */
18244574Sphk
18332513Sphk/*
18432513Sphk * PPS signal quality monitors
18532513Sphk */
18644574Sphkstatic long pps_calcnt;			/* calibration intervals */
18744574Sphkstatic long pps_jitcnt;			/* jitter limit exceeded */
18844574Sphkstatic long pps_stbcnt;			/* stability limit exceeded */
18944574Sphkstatic long pps_errcnt;			/* calibration errors */
1902858Swollman#endif /* PPS_SYNC */
19132513Sphk/*
19244574Sphk * End of phase/frequency-lock loop (PLL/FLL) definitions
19332513Sphk */
19432513Sphk
19544574Sphkstatic void ntp_init(void);
19644574Sphkstatic void hardupdate(long offset);
19732513Sphk
19833690Sphk/*
19944574Sphk * ntp_gettime() - NTP user application interface
20033690Sphk *
20165432Sphk * See the timex.h header file for synopsis and API description. Note
20265432Sphk * that the TAI offset is returned in the ntvtimeval.tai structure
20365432Sphk * member.
20433690Sphk */
20512279Sphkstatic int
20662573Sphkntp_sysctl(SYSCTL_HANDLER_ARGS)
2072858Swollman{
20844574Sphk	struct ntptimeval ntv;	/* temporary structure */
20944574Sphk	struct timespec atv;	/* nanosecond time */
2102858Swollman
21144574Sphk	nanotime(&atv);
21244574Sphk	ntv.time.tv_sec = atv.tv_sec;
21344574Sphk	ntv.time.tv_nsec = atv.tv_nsec;
2142858Swollman	ntv.maxerror = time_maxerror;
2152858Swollman	ntv.esterror = time_esterror;
21665432Sphk	ntv.tai = time_tai;
21765673Sphk	ntv.time_state = time_state;
2182858Swollman
2192858Swollman	/*
22044574Sphk	 * Status word error decode. If any of these conditions occur,
22144574Sphk	 * an error is returned, instead of the status word. Most
22244574Sphk	 * applications will care only about the fact the system clock
22344574Sphk	 * may not be trusted, not about the details.
2242858Swollman	 *
2252858Swollman	 * Hardware or software error
2262858Swollman	 */
22744574Sphk	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
2282858Swollman
2292858Swollman	/*
23044574Sphk	 * PPS signal lost when either time or frequency synchronization
23144574Sphk	 * requested
2322858Swollman	 */
23344574Sphk	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
23444574Sphk	    !(time_status & STA_PPSSIGNAL)) ||
2352858Swollman
2362858Swollman	/*
23744574Sphk	 * PPS jitter exceeded when time synchronization requested
2382858Swollman	 */
23944574Sphk	    (time_status & STA_PPSTIME &&
24044574Sphk	    time_status & STA_PPSJITTER) ||
2412858Swollman
2422858Swollman	/*
24344574Sphk	 * PPS wander exceeded or calibration error when frequency
24444574Sphk	 * synchronization requested
2452858Swollman	 */
24644574Sphk	    (time_status & STA_PPSFREQ &&
24744574Sphk	    time_status & (STA_PPSWANDER | STA_PPSERROR)))
2482858Swollman		ntv.time_state = TIME_ERROR;
24912279Sphk	return (sysctl_handle_opaque(oidp, &ntv, sizeof ntv, req));
2502858Swollman}
2512858Swollman
25244574SphkSYSCTL_NODE(_kern, OID_AUTO, ntp_pll, CTLFLAG_RW, 0, "");
25344574SphkSYSCTL_PROC(_kern_ntp_pll, OID_AUTO, gettime, CTLTYPE_OPAQUE|CTLFLAG_RD,
25412623Sphk	0, sizeof(struct ntptimeval) , ntp_sysctl, "S,ntptimeval", "");
25512279Sphk
25650663Sphk#ifdef PPS_SYNC
25750656SphkSYSCTL_INT(_kern_ntp_pll, OID_AUTO, pps_shiftmax, CTLFLAG_RW, &pps_shiftmax, 0, "");
25855413SphkSYSCTL_INT(_kern_ntp_pll, OID_AUTO, pps_shift, CTLFLAG_RW, &pps_shift, 0, "");
25965673SphkSYSCTL_INT(_kern_ntp_pll, OID_AUTO, time_monitor, CTLFLAG_RD, &time_monitor, 0, "");
26056458Sphk
26156458SphkSYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD, &pps_freq, sizeof(pps_freq), "I", "");
26256458SphkSYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, time_freq, CTLFLAG_RD, &time_freq, sizeof(time_freq), "I", "");
26350663Sphk#endif
2642858Swollman/*
2652858Swollman * ntp_adjtime() - NTP daemon application interface
26644574Sphk *
26765432Sphk * See the timex.h header file for synopsis and API description. Note
26865432Sphk * that the timex.constant structure member has a dual purpose to set
26965432Sphk * the time constant and to set the TAI offset.
2702858Swollman */
27112221Sbde#ifndef _SYS_SYSPROTO_H_
2722858Swollmanstruct ntp_adjtime_args {
27344574Sphk	struct timex *tp;
2742858Swollman};
27512221Sbde#endif
2762858Swollman
27782717Sdillon/*
27882717Sdillon * MPSAFE
27982717Sdillon */
2802858Swollmanint
28183366Sjulianntp_adjtime(struct thread *td, struct ntp_adjtime_args *uap)
2822858Swollman{
28344574Sphk	struct timex ntv;	/* temporary structure */
28445294Sphk	long freq;		/* frequency ns/s) */
28544574Sphk	int modes;		/* mode bits from structure */
28644574Sphk	int s;			/* caller priority */
2872858Swollman	int error;
2882858Swollman
2892858Swollman	error = copyin((caddr_t)uap->tp, (caddr_t)&ntv, sizeof(ntv));
2902858Swollman	if (error)
29144574Sphk		return(error);
2922858Swollman
2932858Swollman	/*
2942858Swollman	 * Update selected clock variables - only the superuser can
2952858Swollman	 * change anything. Note that there is no error checking here on
2962858Swollman	 * the assumption the superuser should know what it is doing.
29765432Sphk	 * Note that either the time constant or TAI offset are loaded
29875540Sjhay	 * from the ntv.constant member, depending on the mode bits. If
29975540Sjhay	 * the STA_PLL bit in the status word is cleared, the state and
30075540Sjhay	 * status words are reset to the initial values at boot.
3012858Swollman	 */
30282717Sdillon	mtx_lock(&Giant);
3032858Swollman	modes = ntv.modes;
30444776Sphk	if (modes)
30593593Sjhb		error = suser(td);
30644574Sphk	if (error)
30782717Sdillon		goto done2;
3082858Swollman	s = splclock();
3092858Swollman	if (modes & MOD_MAXERROR)
3102858Swollman		time_maxerror = ntv.maxerror;
3112858Swollman	if (modes & MOD_ESTERROR)
3122858Swollman		time_esterror = ntv.esterror;
3132858Swollman	if (modes & MOD_STATUS) {
31475540Sjhay		if (time_status & STA_PLL && !(ntv.status & STA_PLL)) {
31575540Sjhay			time_state = TIME_OK;
31675540Sjhay			time_status = STA_UNSYNC;
31775540Sjhay#ifdef PPS_SYNC
31875540Sjhay			pps_shift = PPS_FAVG;
31975540Sjhay#endif /* PPS_SYNC */
32075540Sjhay		}
3212858Swollman		time_status &= STA_RONLY;
3222858Swollman		time_status |= ntv.status & ~STA_RONLY;
3232858Swollman	}
32445294Sphk	if (modes & MOD_TIMECONST) {
32545294Sphk		if (ntv.constant < 0)
32645294Sphk			time_constant = 0;
32745294Sphk		else if (ntv.constant > MAXTC)
32845294Sphk			time_constant = MAXTC;
32945294Sphk		else
33045294Sphk			time_constant = ntv.constant;
33145294Sphk	}
33265432Sphk	if (modes & MOD_TAI) {
33365432Sphk		if (ntv.constant > 0) /* XXX zero & negative numbers ? */
33465432Sphk			time_tai = ntv.constant;
33565432Sphk	}
33650656Sphk#ifdef PPS_SYNC
33750656Sphk	if (modes & MOD_PPSMAX) {
33850656Sphk		if (ntv.shift < PPS_FAVG)
33950656Sphk			pps_shiftmax = PPS_FAVG;
34050656Sphk		else if (ntv.shift > PPS_FAVGMAX)
34150656Sphk			pps_shiftmax = PPS_FAVGMAX;
34250656Sphk		else
34350656Sphk			pps_shiftmax = ntv.shift;
34450656Sphk	}
34550656Sphk#endif /* PPS_SYNC */
34644574Sphk	if (modes & MOD_NANO)
34744574Sphk		time_status |= STA_NANO;
34844574Sphk	if (modes & MOD_MICRO)
34944574Sphk		time_status &= ~STA_NANO;
35044574Sphk	if (modes & MOD_CLKB)
35144574Sphk		time_status |= STA_CLK;
35244574Sphk	if (modes & MOD_CLKA)
35344574Sphk		time_status &= ~STA_CLK;
35444574Sphk	if (modes & MOD_OFFSET) {
35544574Sphk		if (time_status & STA_NANO)
35644574Sphk			hardupdate(ntv.offset);
35744574Sphk		else
35844574Sphk			hardupdate(ntv.offset * 1000);
35944574Sphk	}
36075540Sjhay	if (modes & MOD_FREQUENCY) {
36175540Sjhay		freq = (ntv.freq * 1000LL) >> 16;
36275540Sjhay		if (freq > MAXFREQ)
36375540Sjhay			L_LINT(time_freq, MAXFREQ);
36475540Sjhay		else if (freq < -MAXFREQ)
36575540Sjhay			L_LINT(time_freq, -MAXFREQ);
36675540Sjhay		else
36775540Sjhay			L_LINT(time_freq, freq);
36875540Sjhay#ifdef PPS_SYNC
36975540Sjhay		pps_freq = time_freq;
37075540Sjhay#endif /* PPS_SYNC */
37175540Sjhay	}
3722858Swollman
3732858Swollman	/*
37465432Sphk	 * Retrieve all clock variables. Note that the TAI offset is
37565432Sphk	 * returned only by ntp_gettime();
3762858Swollman	 */
37744574Sphk	if (time_status & STA_NANO)
37894740Sphk		ntv.offset = L_GINT(time_offset);
3792858Swollman	else
38094740Sphk		ntv.offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */
38145295Sphk	ntv.freq = L_GINT((time_freq / 1000LL) << 16);
3822858Swollman	ntv.maxerror = time_maxerror;
3832858Swollman	ntv.esterror = time_esterror;
3842858Swollman	ntv.status = time_status;
38545294Sphk	ntv.constant = time_constant;
38644574Sphk	if (time_status & STA_NANO)
38744574Sphk		ntv.precision = time_precision;
38844574Sphk	else
38944574Sphk		ntv.precision = time_precision / 1000;
39044574Sphk	ntv.tolerance = MAXFREQ * SCALE_PPM;
3912858Swollman#ifdef PPS_SYNC
3922858Swollman	ntv.shift = pps_shift;
39345295Sphk	ntv.ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
39444574Sphk	if (time_status & STA_NANO)
39544574Sphk		ntv.jitter = pps_jitter;
39644574Sphk	else
39744574Sphk		ntv.jitter = pps_jitter / 1000;
3982858Swollman	ntv.stabil = pps_stabil;
3992858Swollman	ntv.calcnt = pps_calcnt;
4002858Swollman	ntv.errcnt = pps_errcnt;
4012858Swollman	ntv.jitcnt = pps_jitcnt;
4022858Swollman	ntv.stbcnt = pps_stbcnt;
4032858Swollman#endif /* PPS_SYNC */
40444574Sphk	splx(s);
4052858Swollman
4062858Swollman	error = copyout((caddr_t)&ntv, (caddr_t)uap->tp, sizeof(ntv));
40744574Sphk	if (error)
40882717Sdillon		goto done2;
40944574Sphk
41044574Sphk	/*
41144574Sphk	 * Status word error decode. See comments in
41244574Sphk	 * ntp_gettime() routine.
41344574Sphk	 */
41444574Sphk	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
41544574Sphk	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
41644574Sphk	    !(time_status & STA_PPSSIGNAL)) ||
41744574Sphk	    (time_status & STA_PPSTIME &&
41844574Sphk	    time_status & STA_PPSJITTER) ||
41944574Sphk	    (time_status & STA_PPSFREQ &&
42082717Sdillon	    time_status & (STA_PPSWANDER | STA_PPSERROR))) {
42183366Sjulian		td->td_retval[0] = TIME_ERROR;
42282717Sdillon	} else {
42383366Sjulian		td->td_retval[0] = time_state;
42482717Sdillon	}
42582717Sdillondone2:
42682717Sdillon	mtx_unlock(&Giant);
42745302Sphk	return (error);
42844574Sphk}
42944574Sphk
43044574Sphk/*
43144574Sphk * second_overflow() - called after ntp_tick_adjust()
43244574Sphk *
43344574Sphk * This routine is ordinarily called immediately following the above
43444574Sphk * routine ntp_tick_adjust(). While these two routines are normally
43544574Sphk * combined, they are separated here only for the purposes of
43644574Sphk * simulation.
43744574Sphk */
43844574Sphkvoid
43944574Sphkntp_update_second(struct timecounter *tcp)
44044574Sphk{
44144574Sphk	u_int32_t *newsec;
44294754Sphk	int tickrate;
44365432Sphk	l_fp ftemp;		/* 32/64-bit temporary */
44444574Sphk
44590362Sphk	newsec = &tcp->tc_offset.sec;
44650656Sphk	/*
44750656Sphk	 * On rollover of the second both the nanosecond and microsecond
44850656Sphk	 * clocks are updated and the state machine cranked as
44950656Sphk	 * necessary. The phase adjustment to be used for the next
45050656Sphk	 * second is calculated and the maximum error is increased by
45150656Sphk	 * the tolerance.
45250656Sphk	 */
45344574Sphk	time_maxerror += MAXFREQ / 1000;
45444574Sphk
45544574Sphk	/*
45644574Sphk	 * Leap second processing. If in leap-insert state at
45744574Sphk	 * the end of the day, the system clock is set back one
45844574Sphk	 * second; if in leap-delete state, the system clock is
45944574Sphk	 * set ahead one second. The nano_time() routine or
46044574Sphk	 * external clock driver will insure that reported time
46144574Sphk	 * is always monotonic.
46244574Sphk	 */
46344574Sphk	switch (time_state) {
46444574Sphk
4652858Swollman		/*
46644574Sphk		 * No warning.
4672858Swollman		 */
46844574Sphk		case TIME_OK:
46944574Sphk		if (time_status & STA_INS)
47044574Sphk			time_state = TIME_INS;
47144574Sphk		else if (time_status & STA_DEL)
47244574Sphk			time_state = TIME_DEL;
47344574Sphk		break;
47444574Sphk
47544574Sphk		/*
47644574Sphk		 * Insert second 23:59:60 following second
47744574Sphk		 * 23:59:59.
47844574Sphk		 */
47944574Sphk		case TIME_INS:
48044574Sphk		if (!(time_status & STA_INS))
48144574Sphk			time_state = TIME_OK;
48244574Sphk		else if ((*newsec) % 86400 == 0) {
48344574Sphk			(*newsec)--;
48444574Sphk			time_state = TIME_OOP;
48544574Sphk		}
48644574Sphk		break;
48744574Sphk
48844574Sphk		/*
48944574Sphk		 * Delete second 23:59:59.
49044574Sphk		 */
49144574Sphk		case TIME_DEL:
49244574Sphk		if (!(time_status & STA_DEL))
49344574Sphk			time_state = TIME_OK;
49444574Sphk		else if (((*newsec) + 1) % 86400 == 0) {
49544574Sphk			(*newsec)++;
49665432Sphk			time_tai--;
49744574Sphk			time_state = TIME_WAIT;
49844574Sphk		}
49944574Sphk		break;
50044574Sphk
50144574Sphk		/*
50244574Sphk		 * Insert second in progress.
50344574Sphk		 */
50444574Sphk		case TIME_OOP:
50565432Sphk			time_tai++;
50665432Sphk			time_state = TIME_WAIT;
50744574Sphk		break;
50844574Sphk
50944574Sphk		/*
51044574Sphk		 * Wait for status bits to clear.
51144574Sphk		 */
51244574Sphk		case TIME_WAIT:
51344574Sphk		if (!(time_status & (STA_INS | STA_DEL)))
51444574Sphk			time_state = TIME_OK;
5152858Swollman	}
51644574Sphk
51744574Sphk	/*
51850656Sphk	 * Compute the total time adjustment for the next second
51950656Sphk	 * in ns. The offset is reduced by a factor depending on
52050656Sphk	 * whether the PPS signal is operating. Note that the
52150656Sphk	 * value is in effect scaled by the clock frequency,
52250656Sphk	 * since the adjustment is added at each tick interrupt.
52344574Sphk	 */
52465432Sphk	ftemp = time_offset;
52544574Sphk#ifdef PPS_SYNC
52665432Sphk	/* XXX even if PPS signal dies we should finish adjustment ? */
52765432Sphk	if (time_status & STA_PPSTIME && time_status &
52865673Sphk	    STA_PPSSIGNAL)
52965432Sphk		L_RSHIFT(ftemp, pps_shift);
53065432Sphk	else
53165432Sphk		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
53244574Sphk#else
53365432Sphk		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
53444574Sphk#endif /* PPS_SYNC */
53565432Sphk	time_adj = ftemp;
53665432Sphk	L_SUB(time_offset, ftemp);
53744574Sphk	L_ADD(time_adj, time_freq);
53894754Sphk
53994754Sphk	/*
54094754Sphk	 * Apply any correction from adjtime(2).  If more than one second
54194754Sphk	 * off we slew at a rate of 5ms/s (5000 PPM) else 500us/s (500PPM)
54294754Sphk	 * until the last second is slewed the final < 500 usecs.
54394754Sphk	 */
54494754Sphk	if (time_adjtime != 0) {
54594754Sphk		if (time_adjtime > 1000000)
54694754Sphk			tickrate = 5000;
54794754Sphk		else if (time_adjtime < -1000000)
54894754Sphk			tickrate = -5000;
54994754Sphk		else if (time_adjtime > 500)
55094754Sphk			tickrate = 500;
55194754Sphk		else if (time_adjtime < -500)
55294754Sphk			tickrate = -500;
55394754Sphk		else if (time_adjtime != 0)
55494754Sphk			tickrate = time_adjtime;
55594754Sphk		else
55694754Sphk			tickrate = 0;	/* GCC sucks! */
55794754Sphk		time_adjtime -= tickrate;
55894754Sphk		L_LINT(ftemp, tickrate * 1000);
55994754Sphk		L_ADD(time_adj, ftemp);
56094754Sphk	}
56144574Sphk	tcp->tc_adjustment = time_adj;
56294754Sphk
56344574Sphk#ifdef PPS_SYNC
56444574Sphk	if (pps_valid > 0)
56544574Sphk		pps_valid--;
56644574Sphk	else
56775540Sjhay		time_status &= ~STA_PPSSIGNAL;
56844574Sphk#endif /* PPS_SYNC */
5692858Swollman}
5702858Swollman
57144574Sphk/*
57244574Sphk * ntp_init() - initialize variables and structures
57344574Sphk *
57444574Sphk * This routine must be called after the kernel variables hz and tick
57544574Sphk * are set or changed and before the next tick interrupt. In this
57644574Sphk * particular implementation, these values are assumed set elsewhere in
57744574Sphk * the kernel. The design allows the clock frequency and tick interval
57844574Sphk * to be changed while the system is running. So, this routine should
57944574Sphk * probably be integrated with the code that does that.
58044574Sphk */
58144574Sphkstatic void
58244574Sphkntp_init()
58344574Sphk{
58444574Sphk
58544574Sphk	/*
58644574Sphk	 * The following variable must be initialized any time the
58744574Sphk	 * kernel variable hz is changed.
58844574Sphk	 */
58944574Sphk	time_tick = NANOSECOND / hz;
59044574Sphk
59144574Sphk	/*
59244574Sphk	 * The following variables are initialized only at startup. Only
59344574Sphk	 * those structures not cleared by the compiler need to be
59444574Sphk	 * initialized, and these only in the simulator. In the actual
59544574Sphk	 * kernel, any nonzero values here will quickly evaporate.
59644574Sphk	 */
59744574Sphk	L_CLR(time_offset);
59844574Sphk	L_CLR(time_freq);
59932513Sphk#ifdef PPS_SYNC
60050656Sphk	pps_tf[0].tv_sec = pps_tf[0].tv_nsec = 0;
60150656Sphk	pps_tf[1].tv_sec = pps_tf[1].tv_nsec = 0;
60250656Sphk	pps_tf[2].tv_sec = pps_tf[2].tv_nsec = 0;
60344794Sphk	pps_fcount = 0;
60444574Sphk	L_CLR(pps_freq);
60544574Sphk#endif /* PPS_SYNC */
60644574Sphk}
6072858Swollman
60844574SphkSYSINIT(ntpclocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, ntp_init, NULL)
60932513Sphk
61044574Sphk/*
61144574Sphk * hardupdate() - local clock update
61244574Sphk *
61344574Sphk * This routine is called by ntp_adjtime() to update the local clock
61444574Sphk * phase and frequency. The implementation is of an adaptive-parameter,
61544574Sphk * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
61644574Sphk * time and frequency offset estimates for each call. If the kernel PPS
61744574Sphk * discipline code is configured (PPS_SYNC), the PPS signal itself
61844574Sphk * determines the new time offset, instead of the calling argument.
61944574Sphk * Presumably, calls to ntp_adjtime() occur only when the caller
62044574Sphk * believes the local clock is valid within some bound (+-128 ms with
62144574Sphk * NTP). If the caller's time is far different than the PPS time, an
62244574Sphk * argument will ensue, and it's not clear who will lose.
62344574Sphk *
62444574Sphk * For uncompensated quartz crystal oscillators and nominal update
62544574Sphk * intervals less than 256 s, operation should be in phase-lock mode,
62644574Sphk * where the loop is disciplined to phase. For update intervals greater
62744574Sphk * than 1024 s, operation should be in frequency-lock mode, where the
62844574Sphk * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
62944574Sphk * is selected by the STA_MODE status bit.
63044574Sphk */
63144574Sphkstatic void
63244574Sphkhardupdate(offset)
63344574Sphk	long offset;		/* clock offset (ns) */
63444574Sphk{
63565432Sphk	long mtemp;
63644574Sphk	l_fp ftemp;
63732513Sphk
63844574Sphk	/*
63944574Sphk	 * Select how the phase is to be controlled and from which
64044574Sphk	 * source. If the PPS signal is present and enabled to
64144574Sphk	 * discipline the time, the PPS offset is used; otherwise, the
64244574Sphk	 * argument offset is used.
64344574Sphk	 */
64450656Sphk	if (!(time_status & STA_PLL))
64550656Sphk		return;
64665432Sphk	if (!(time_status & STA_PPSTIME && time_status &
64765432Sphk	    STA_PPSSIGNAL)) {
64865432Sphk		if (offset > MAXPHASE)
64965432Sphk			time_monitor = MAXPHASE;
65065432Sphk		else if (offset < -MAXPHASE)
65165432Sphk			time_monitor = -MAXPHASE;
65265432Sphk		else
65365432Sphk			time_monitor = offset;
65465432Sphk		L_LINT(time_offset, time_monitor);
65565432Sphk	}
65632513Sphk
65744574Sphk	/*
65844574Sphk	 * Select how the frequency is to be controlled and in which
65944574Sphk	 * mode (PLL or FLL). If the PPS signal is present and enabled
66044574Sphk	 * to discipline the frequency, the PPS frequency is used;
66144574Sphk	 * otherwise, the argument offset is used to compute it.
66244574Sphk	 */
66344574Sphk	if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) {
66444574Sphk		time_reftime = time_second;
66544574Sphk		return;
66644574Sphk	}
66744574Sphk	if (time_status & STA_FREQHOLD || time_reftime == 0)
66844574Sphk		time_reftime = time_second;
66944574Sphk	mtemp = time_second - time_reftime;
67065432Sphk	L_LINT(ftemp, time_monitor);
67150656Sphk	L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
67250656Sphk	L_MPY(ftemp, mtemp);
67350656Sphk	L_ADD(time_freq, ftemp);
67450656Sphk	time_status &= ~STA_MODE;
67565432Sphk	if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp >
67665432Sphk	    MAXSEC)) {
67765432Sphk		L_LINT(ftemp, (time_monitor << 4) / mtemp);
67844574Sphk		L_RSHIFT(ftemp, SHIFT_FLL + 4);
67944574Sphk		L_ADD(time_freq, ftemp);
68044574Sphk		time_status |= STA_MODE;
68144574Sphk	}
68244574Sphk	time_reftime = time_second;
68344574Sphk	if (L_GINT(time_freq) > MAXFREQ)
68444574Sphk		L_LINT(time_freq, MAXFREQ);
68544574Sphk	else if (L_GINT(time_freq) < -MAXFREQ)
68644574Sphk		L_LINT(time_freq, -MAXFREQ);
68744574Sphk}
68844574Sphk
68944574Sphk#ifdef PPS_SYNC
69032513Sphk/*
69132513Sphk * hardpps() - discipline CPU clock oscillator to external PPS signal
69232513Sphk *
69332513Sphk * This routine is called at each PPS interrupt in order to discipline
69465432Sphk * the CPU clock oscillator to the PPS signal. There are two independent
69565432Sphk * first-order feedback loops, one for the phase, the other for the
69665432Sphk * frequency. The phase loop measures and grooms the PPS phase offset
69765432Sphk * and leaves it in a handy spot for the seconds overflow routine. The
69865432Sphk * frequency loop averages successive PPS phase differences and
69965432Sphk * calculates the PPS frequency offset, which is also processed by the
70065432Sphk * seconds overflow routine. The code requires the caller to capture the
70165432Sphk * time and architecture-dependent hardware counter values in
70265432Sphk * nanoseconds at the on-time PPS signal transition.
70332513Sphk *
70444574Sphk * Note that, on some Unix systems this routine runs at an interrupt
70532513Sphk * priority level higher than the timer interrupt routine hardclock().
70632513Sphk * Therefore, the variables used are distinct from the hardclock()
70744574Sphk * variables, except for the actual time and frequency variables, which
70844574Sphk * are determined by this routine and updated atomically.
70932513Sphk */
71032513Sphkvoid
71144574Sphkhardpps(tsp, nsec)
71244574Sphk	struct timespec *tsp;	/* time at PPS */
71344574Sphk	long nsec;		/* hardware counter at PPS */
71432513Sphk{
71565432Sphk	long u_sec, u_nsec, v_nsec; /* temps */
71644574Sphk	l_fp ftemp;
71732513Sphk
71832513Sphk	/*
71965432Sphk	 * The signal is first processed by a range gate and frequency
72065432Sphk	 * discriminator. The range gate rejects noise spikes outside
72165432Sphk	 * the range +-500 us. The frequency discriminator rejects input
72265432Sphk	 * signals with apparent frequency outside the range 1 +-500
72365432Sphk	 * PPM. If two hits occur in the same second, we ignore the
72465432Sphk	 * later hit; if not and a hit occurs outside the range gate,
72565432Sphk	 * keep the later hit for later comparison, but do not process
72665432Sphk	 * it.
72732513Sphk	 */
72844574Sphk	time_status |= STA_PPSSIGNAL | STA_PPSJITTER;
72944574Sphk	time_status &= ~(STA_PPSWANDER | STA_PPSERROR);
73044574Sphk	pps_valid = PPS_VALID;
73144574Sphk	u_sec = tsp->tv_sec;
73244574Sphk	u_nsec = tsp->tv_nsec;
73344574Sphk	if (u_nsec >= (NANOSECOND >> 1)) {
73444574Sphk		u_nsec -= NANOSECOND;
73544574Sphk		u_sec++;
73644574Sphk	}
73750656Sphk	v_nsec = u_nsec - pps_tf[0].tv_nsec;
73875540Sjhay	if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND -
73975540Sjhay	    MAXFREQ)
74044574Sphk		return;
74144574Sphk	pps_tf[2] = pps_tf[1];
74244574Sphk	pps_tf[1] = pps_tf[0];
74350656Sphk	pps_tf[0].tv_sec = u_sec;
74450656Sphk	pps_tf[0].tv_nsec = u_nsec;
74532513Sphk
74632513Sphk	/*
74744574Sphk	 * Compute the difference between the current and previous
74844574Sphk	 * counter values. If the difference exceeds 0.5 s, assume it
74944574Sphk	 * has wrapped around, so correct 1.0 s. If the result exceeds
75044574Sphk	 * the tick interval, the sample point has crossed a tick
75144574Sphk	 * boundary during the last second, so correct the tick. Very
75244574Sphk	 * intricate.
75344574Sphk	 */
75444666Sphk	u_nsec = nsec;
75544574Sphk	if (u_nsec > (NANOSECOND >> 1))
75644574Sphk		u_nsec -= NANOSECOND;
75744574Sphk	else if (u_nsec < -(NANOSECOND >> 1))
75844574Sphk		u_nsec += NANOSECOND;
75944794Sphk	pps_fcount += u_nsec;
76075540Sjhay	if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ)
76144574Sphk		return;
76244574Sphk	time_status &= ~STA_PPSJITTER;
76344574Sphk
76444574Sphk	/*
76544574Sphk	 * A three-stage median filter is used to help denoise the PPS
76632513Sphk	 * time. The median sample becomes the time offset estimate; the
76732513Sphk	 * difference between the other two samples becomes the time
76832513Sphk	 * dispersion (jitter) estimate.
76932513Sphk	 */
77050656Sphk	if (pps_tf[0].tv_nsec > pps_tf[1].tv_nsec) {
77150656Sphk		if (pps_tf[1].tv_nsec > pps_tf[2].tv_nsec) {
77250656Sphk			v_nsec = pps_tf[1].tv_nsec;	/* 0 1 2 */
77350656Sphk			u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec;
77450656Sphk		} else if (pps_tf[2].tv_nsec > pps_tf[0].tv_nsec) {
77550656Sphk			v_nsec = pps_tf[0].tv_nsec;	/* 2 0 1 */
77650656Sphk			u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec;
77744574Sphk		} else {
77850656Sphk			v_nsec = pps_tf[2].tv_nsec;	/* 0 2 1 */
77950656Sphk			u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec;
78044574Sphk		}
78144574Sphk	} else {
78250656Sphk		if (pps_tf[1].tv_nsec < pps_tf[2].tv_nsec) {
78350656Sphk			v_nsec = pps_tf[1].tv_nsec;	/* 2 1 0 */
78450656Sphk			u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec;
78565673Sphk		} else if (pps_tf[2].tv_nsec < pps_tf[0].tv_nsec) {
78650656Sphk			v_nsec = pps_tf[0].tv_nsec;	/* 1 0 2 */
78750656Sphk			u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec;
78844574Sphk		} else {
78950656Sphk			v_nsec = pps_tf[2].tv_nsec;	/* 1 2 0 */
79050656Sphk			u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec;
79144574Sphk		}
79244574Sphk	}
79332513Sphk
79432513Sphk	/*
79565673Sphk	 * Nominal jitter is due to PPS signal noise and interrupt
79665432Sphk	 * latency. If it exceeds the popcorn threshold, the sample is
79765432Sphk	 * discarded. otherwise, if so enabled, the time offset is
79865432Sphk	 * updated. We can tolerate a modest loss of data here without
79965432Sphk	 * much degrading time accuracy.
80032513Sphk	 */
80150656Sphk	if (u_nsec > (pps_jitter << PPS_POPCORN)) {
80244574Sphk		time_status |= STA_PPSJITTER;
80344574Sphk		pps_jitcnt++;
80444574Sphk	} else if (time_status & STA_PPSTIME) {
80565432Sphk		time_monitor = -v_nsec;
80665432Sphk		L_LINT(time_offset, time_monitor);
80732513Sphk	}
80844574Sphk	pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
80950656Sphk	u_sec = pps_tf[0].tv_sec - pps_lastsec;
81044574Sphk	if (u_sec < (1 << pps_shift))
81144574Sphk		return;
81244574Sphk
81332513Sphk	/*
81444574Sphk	 * At the end of the calibration interval the difference between
81544574Sphk	 * the first and last counter values becomes the scaled
81644574Sphk	 * frequency. It will later be divided by the length of the
81744574Sphk	 * interval to determine the frequency update. If the frequency
81844574Sphk	 * exceeds a sanity threshold, or if the actual calibration
81944574Sphk	 * interval is not equal to the expected length, the data are
82044574Sphk	 * discarded. We can tolerate a modest loss of data here without
82165432Sphk	 * much degrading frequency accuracy.
82232513Sphk	 */
82344574Sphk	pps_calcnt++;
82444794Sphk	v_nsec = -pps_fcount;
82550656Sphk	pps_lastsec = pps_tf[0].tv_sec;
82644794Sphk	pps_fcount = 0;
82744574Sphk	u_nsec = MAXFREQ << pps_shift;
82844574Sphk	if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 <<
82944574Sphk	    pps_shift)) {
83044574Sphk		time_status |= STA_PPSERROR;
83132513Sphk		pps_errcnt++;
83232513Sphk		return;
83332513Sphk	}
83432513Sphk
83532513Sphk	/*
83650656Sphk	 * Here the raw frequency offset and wander (stability) is
83750656Sphk	 * calculated. If the wander is less than the wander threshold
83850656Sphk	 * for four consecutive averaging intervals, the interval is
83950656Sphk	 * doubled; if it is greater than the threshold for four
84050656Sphk	 * consecutive intervals, the interval is halved. The scaled
84150656Sphk	 * frequency offset is converted to frequency offset. The
84250656Sphk	 * stability metric is calculated as the average of recent
84350656Sphk	 * frequency changes, but is used only for performance
84444574Sphk	 * monitoring.
84532513Sphk	 */
84644574Sphk	L_LINT(ftemp, v_nsec);
84744574Sphk	L_RSHIFT(ftemp, pps_shift);
84844574Sphk	L_SUB(ftemp, pps_freq);
84944574Sphk	u_nsec = L_GINT(ftemp);
85050656Sphk	if (u_nsec > PPS_MAXWANDER) {
85150656Sphk		L_LINT(ftemp, PPS_MAXWANDER);
85244574Sphk		pps_intcnt--;
85344574Sphk		time_status |= STA_PPSWANDER;
85432513Sphk		pps_stbcnt++;
85550656Sphk	} else if (u_nsec < -PPS_MAXWANDER) {
85650656Sphk		L_LINT(ftemp, -PPS_MAXWANDER);
85744574Sphk		pps_intcnt--;
85832513Sphk		time_status |= STA_PPSWANDER;
85944574Sphk		pps_stbcnt++;
86044574Sphk	} else {
86144574Sphk		pps_intcnt++;
86232513Sphk	}
86365432Sphk	if (pps_intcnt >= 4) {
86444574Sphk		pps_intcnt = 4;
86550656Sphk		if (pps_shift < pps_shiftmax) {
86644574Sphk			pps_shift++;
86744574Sphk			pps_intcnt = 0;
86832513Sphk		}
86965432Sphk	} else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) {
87044574Sphk		pps_intcnt = -4;
87144574Sphk		if (pps_shift > PPS_FAVG) {
87244574Sphk			pps_shift--;
87344574Sphk			pps_intcnt = 0;
87444574Sphk		}
87532513Sphk	}
87644574Sphk	if (u_nsec < 0)
87744574Sphk		u_nsec = -u_nsec;
87844574Sphk	pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
87932513Sphk
88032513Sphk	/*
88150656Sphk	 * The PPS frequency is recalculated and clamped to the maximum
88250656Sphk	 * MAXFREQ. If enabled, the system clock frequency is updated as
88350656Sphk	 * well.
88432513Sphk	 */
88544574Sphk	L_ADD(pps_freq, ftemp);
88644574Sphk	u_nsec = L_GINT(pps_freq);
88744574Sphk	if (u_nsec > MAXFREQ)
88844574Sphk		L_LINT(pps_freq, MAXFREQ);
88944574Sphk	else if (u_nsec < -MAXFREQ)
89044574Sphk		L_LINT(pps_freq, -MAXFREQ);
89165432Sphk	if (time_status & STA_PPSFREQ)
89244574Sphk		time_freq = pps_freq;
89332513Sphk}
89432513Sphk#endif /* PPS_SYNC */
89594754Sphk
89694754Sphk#ifndef _SYS_SYSPROTO_H_
89794754Sphkstruct adjtime_args {
89894754Sphk	struct timeval *delta;
89994754Sphk	struct timeval *olddelta;
90094754Sphk};
90194754Sphk#endif
90294754Sphk/*
90394754Sphk * MPSAFE
90494754Sphk */
90594754Sphk/* ARGSUSED */
90694754Sphkint
90794754Sphkadjtime(struct thread *td, struct adjtime_args *uap)
90894754Sphk{
90994754Sphk	struct timeval atv;
91094754Sphk	int error;
91194754Sphk
91295036Sphk	if ((error = suser(td)))
91395036Sphk		return (error);
91495036Sphk
91594754Sphk	mtx_lock(&Giant);
91694754Sphk	if (uap->olddelta) {
91794754Sphk		atv.tv_sec = time_adjtime / 1000000;
91894754Sphk		atv.tv_usec = time_adjtime % 1000000;
91994754Sphk		if (atv.tv_usec < 0) {
92094754Sphk			atv.tv_usec += 1000000;
92194754Sphk			atv.tv_sec--;
92294754Sphk		}
92394754Sphk		error = copyout(&atv, uap->olddelta, sizeof(atv));
92494754Sphk		if (error)
92594754Sphk			goto done2;
92694754Sphk	}
92794754Sphk	if (uap->delta) {
92894754Sphk		error = copyin(uap->delta, &atv, sizeof(atv));
92994754Sphk		if (error)
93094754Sphk			goto done2;
93194754Sphk		time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec;
93294754Sphk	}
93394754Sphkdone2:
93494754Sphk	mtx_unlock(&Giant);
93594754Sphk	return (error);
93694754Sphk}
93794754Sphk
938