kern_ntptime.c revision 116838
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. 302858Swollman */ 3132925Seivind 32116182Sobrien#include <sys/cdefs.h> 33116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_ntptime.c 116838 2003-06-25 20:56:40Z imp $"); 34116182Sobrien 3544666Sphk#include "opt_ntp.h" 3644666Sphk 372858Swollman#include <sys/param.h> 382858Swollman#include <sys/systm.h> 3912221Sbde#include <sys/sysproto.h> 402858Swollman#include <sys/kernel.h> 412858Swollman#include <sys/proc.h> 4282717Sdillon#include <sys/lock.h> 4382717Sdillon#include <sys/mutex.h> 4444574Sphk#include <sys/time.h> 452858Swollman#include <sys/timex.h> 4658377Sphk#include <sys/timetc.h> 4736941Sphk#include <sys/timepps.h> 482858Swollman#include <sys/sysctl.h> 492858Swollman 502858Swollman/* 5144574Sphk * Single-precision macros for 64-bit machines 5244574Sphk */ 5344574Sphktypedef long long l_fp; 5444574Sphk#define L_ADD(v, u) ((v) += (u)) 5544574Sphk#define L_SUB(v, u) ((v) -= (u)) 5644574Sphk#define L_ADDHI(v, a) ((v) += (long long)(a) << 32) 5744574Sphk#define L_NEG(v) ((v) = -(v)) 5844574Sphk#define L_RSHIFT(v, n) \ 5944574Sphk do { \ 6044574Sphk if ((v) < 0) \ 6144574Sphk (v) = -(-(v) >> (n)); \ 6244574Sphk else \ 6344574Sphk (v) = (v) >> (n); \ 6444574Sphk } while (0) 6544574Sphk#define L_MPY(v, a) ((v) *= (a)) 6644574Sphk#define L_CLR(v) ((v) = 0) 6744574Sphk#define L_ISNEG(v) ((v) < 0) 6844574Sphk#define L_LINT(v, a) ((v) = (long long)(a) << 32) 6944574Sphk#define L_GINT(v) ((v) < 0 ? -(-(v) >> 32) : (v) >> 32) 7044574Sphk 7144574Sphk/* 7244574Sphk * Generic NTP kernel interface 7332513Sphk * 7444574Sphk * These routines constitute the Network Time Protocol (NTP) interfaces 7544574Sphk * for user and daemon application programs. The ntp_gettime() routine 7644574Sphk * provides the time, maximum error (synch distance) and estimated error 7744574Sphk * (dispersion) to client user application programs. The ntp_adjtime() 7844574Sphk * routine is used by the NTP daemon to adjust the system clock to an 7944574Sphk * externally derived time. The time offset and related variables set by 8044574Sphk * this routine are used by other routines in this module to adjust the 8144574Sphk * phase and frequency of the clock discipline loop which controls the 8244574Sphk * system clock. 8332513Sphk * 8445294Sphk * When the kernel time is reckoned directly in nanoseconds (NTP_NANO 8544574Sphk * defined), the time at each tick interrupt is derived directly from 8644574Sphk * the kernel time variable. When the kernel time is reckoned in 8745294Sphk * microseconds, (NTP_NANO undefined), the time is derived from the 8845294Sphk * kernel time variable together with a variable representing the 8945294Sphk * leftover nanoseconds at the last tick interrupt. In either case, the 9045294Sphk * current nanosecond time is reckoned from these values plus an 9145294Sphk * interpolated value derived by the clock routines in another 9245294Sphk * architecture-specific module. The interpolation can use either a 9345294Sphk * dedicated counter or a processor cycle counter (PCC) implemented in 9445294Sphk * some architectures. 9532513Sphk * 9644574Sphk * Note that all routines must run at priority splclock or higher. 9744574Sphk */ 9844574Sphk/* 9944574Sphk * Phase/frequency-lock loop (PLL/FLL) definitions 10032513Sphk * 10144574Sphk * The nanosecond clock discipline uses two variable types, time 10244574Sphk * variables and frequency variables. Both types are represented as 64- 10344574Sphk * bit fixed-point quantities with the decimal point between two 32-bit 10444574Sphk * halves. On a 32-bit machine, each half is represented as a single 10544574Sphk * word and mathematical operations are done using multiple-precision 10644574Sphk * arithmetic. On a 64-bit machine, ordinary computer arithmetic is 10744574Sphk * used. 10832513Sphk * 10944574Sphk * A time variable is a signed 64-bit fixed-point number in ns and 11044574Sphk * fraction. It represents the remaining time offset to be amortized 11144574Sphk * over succeeding tick interrupts. The maximum time offset is about 11245294Sphk * 0.5 s and the resolution is about 2.3e-10 ns. 11332513Sphk * 11444574Sphk * 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 11544574Sphk * 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 11644574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 11744574Sphk * |s s s| ns | 11844574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 11944574Sphk * | fraction | 12044574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 12132513Sphk * 12244574Sphk * A frequency variable is a signed 64-bit fixed-point number in ns/s 12344574Sphk * and fraction. It represents the ns and fraction to be added to the 12444574Sphk * kernel time variable at each second. The maximum frequency offset is 12545294Sphk * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s. 12632513Sphk * 12744574Sphk * 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 12844574Sphk * 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 12944574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 13044574Sphk * |s s s s s s s s s s s s s| ns/s | 13144574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 13244574Sphk * | fraction | 13344574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 1342858Swollman */ 13532513Sphk/* 13632513Sphk * The following variables establish the state of the PLL/FLL and the 13744574Sphk * residual time and frequency offset of the local clock. 13832513Sphk */ 13944574Sphk#define SHIFT_PLL 4 /* PLL loop gain (shift) */ 14044574Sphk#define SHIFT_FLL 2 /* FLL loop gain (shift) */ 14132513Sphk 14244574Sphkstatic int time_state = TIME_OK; /* clock state */ 14344574Sphkstatic int time_status = STA_UNSYNC; /* clock status bits */ 14465432Sphkstatic long time_tai; /* TAI offset (s) */ 14565432Sphkstatic long time_monitor; /* last time offset scaled (ns) */ 14644574Sphkstatic long time_constant; /* poll interval (shift) (s) */ 14744574Sphkstatic long time_precision = 1; /* clock precision (ns) */ 14844574Sphkstatic long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */ 14944574Sphkstatic long time_esterror = MAXPHASE / 1000; /* estimated error (us) */ 15044574Sphkstatic long time_reftime; /* time at last adjustment (s) */ 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 43995529Sphkntp_update_second(int64_t *adjustment, time_t *newsec) 44044574Sphk{ 44194754Sphk int tickrate; 44265432Sphk l_fp ftemp; /* 32/64-bit temporary */ 44344574Sphk 44450656Sphk /* 44550656Sphk * On rollover of the second both the nanosecond and microsecond 44650656Sphk * clocks are updated and the state machine cranked as 44750656Sphk * necessary. The phase adjustment to be used for the next 44850656Sphk * second is calculated and the maximum error is increased by 44950656Sphk * the tolerance. 45050656Sphk */ 45144574Sphk time_maxerror += MAXFREQ / 1000; 45244574Sphk 45344574Sphk /* 45444574Sphk * Leap second processing. If in leap-insert state at 45544574Sphk * the end of the day, the system clock is set back one 45644574Sphk * second; if in leap-delete state, the system clock is 45744574Sphk * set ahead one second. The nano_time() routine or 45844574Sphk * external clock driver will insure that reported time 45944574Sphk * is always monotonic. 46044574Sphk */ 46144574Sphk switch (time_state) { 46244574Sphk 4632858Swollman /* 46444574Sphk * No warning. 4652858Swollman */ 46644574Sphk case TIME_OK: 46744574Sphk if (time_status & STA_INS) 46844574Sphk time_state = TIME_INS; 46944574Sphk else if (time_status & STA_DEL) 47044574Sphk time_state = TIME_DEL; 47144574Sphk break; 47244574Sphk 47344574Sphk /* 47444574Sphk * Insert second 23:59:60 following second 47544574Sphk * 23:59:59. 47644574Sphk */ 47744574Sphk case TIME_INS: 47844574Sphk if (!(time_status & STA_INS)) 47944574Sphk time_state = TIME_OK; 48044574Sphk else if ((*newsec) % 86400 == 0) { 48144574Sphk (*newsec)--; 48244574Sphk time_state = TIME_OOP; 483116838Simp time_tai++; 48444574Sphk } 48544574Sphk break; 48644574Sphk 48744574Sphk /* 48844574Sphk * Delete second 23:59:59. 48944574Sphk */ 49044574Sphk case TIME_DEL: 49144574Sphk if (!(time_status & STA_DEL)) 49244574Sphk time_state = TIME_OK; 49344574Sphk else if (((*newsec) + 1) % 86400 == 0) { 49444574Sphk (*newsec)++; 49565432Sphk time_tai--; 49644574Sphk time_state = TIME_WAIT; 49744574Sphk } 49844574Sphk break; 49944574Sphk 50044574Sphk /* 50144574Sphk * Insert second in progress. 50244574Sphk */ 50344574Sphk case TIME_OOP: 50465432Sphk time_state = TIME_WAIT; 50544574Sphk break; 50644574Sphk 50744574Sphk /* 50844574Sphk * Wait for status bits to clear. 50944574Sphk */ 51044574Sphk case TIME_WAIT: 51144574Sphk if (!(time_status & (STA_INS | STA_DEL))) 51244574Sphk time_state = TIME_OK; 5132858Swollman } 51444574Sphk 51544574Sphk /* 51650656Sphk * Compute the total time adjustment for the next second 51750656Sphk * in ns. The offset is reduced by a factor depending on 51850656Sphk * whether the PPS signal is operating. Note that the 51950656Sphk * value is in effect scaled by the clock frequency, 52050656Sphk * since the adjustment is added at each tick interrupt. 52144574Sphk */ 52265432Sphk ftemp = time_offset; 52344574Sphk#ifdef PPS_SYNC 52465432Sphk /* XXX even if PPS signal dies we should finish adjustment ? */ 52565432Sphk if (time_status & STA_PPSTIME && time_status & 52665673Sphk STA_PPSSIGNAL) 52765432Sphk L_RSHIFT(ftemp, pps_shift); 52865432Sphk else 52965432Sphk L_RSHIFT(ftemp, SHIFT_PLL + time_constant); 53044574Sphk#else 53165432Sphk L_RSHIFT(ftemp, SHIFT_PLL + time_constant); 53244574Sphk#endif /* PPS_SYNC */ 53365432Sphk time_adj = ftemp; 53465432Sphk L_SUB(time_offset, ftemp); 53544574Sphk L_ADD(time_adj, time_freq); 53694754Sphk 53794754Sphk /* 53894754Sphk * Apply any correction from adjtime(2). If more than one second 53994754Sphk * off we slew at a rate of 5ms/s (5000 PPM) else 500us/s (500PPM) 54094754Sphk * until the last second is slewed the final < 500 usecs. 54194754Sphk */ 54294754Sphk if (time_adjtime != 0) { 54394754Sphk if (time_adjtime > 1000000) 54494754Sphk tickrate = 5000; 54594754Sphk else if (time_adjtime < -1000000) 54694754Sphk tickrate = -5000; 54794754Sphk else if (time_adjtime > 500) 54894754Sphk tickrate = 500; 54994754Sphk else if (time_adjtime < -500) 55094754Sphk tickrate = -500; 55194754Sphk else if (time_adjtime != 0) 55294754Sphk tickrate = time_adjtime; 55394754Sphk else 55494754Sphk tickrate = 0; /* GCC sucks! */ 55594754Sphk time_adjtime -= tickrate; 55694754Sphk L_LINT(ftemp, tickrate * 1000); 55794754Sphk L_ADD(time_adj, ftemp); 55894754Sphk } 55995529Sphk *adjustment = time_adj; 56094754Sphk 56144574Sphk#ifdef PPS_SYNC 56244574Sphk if (pps_valid > 0) 56344574Sphk pps_valid--; 56444574Sphk else 56575540Sjhay time_status &= ~STA_PPSSIGNAL; 56644574Sphk#endif /* PPS_SYNC */ 5672858Swollman} 5682858Swollman 56944574Sphk/* 57044574Sphk * ntp_init() - initialize variables and structures 57144574Sphk * 57244574Sphk * This routine must be called after the kernel variables hz and tick 57344574Sphk * are set or changed and before the next tick interrupt. In this 57444574Sphk * particular implementation, these values are assumed set elsewhere in 57544574Sphk * the kernel. The design allows the clock frequency and tick interval 57644574Sphk * to be changed while the system is running. So, this routine should 57744574Sphk * probably be integrated with the code that does that. 57844574Sphk */ 57944574Sphkstatic void 58044574Sphkntp_init() 58144574Sphk{ 58244574Sphk 58344574Sphk /* 58444574Sphk * The following variables are initialized only at startup. Only 58544574Sphk * those structures not cleared by the compiler need to be 58644574Sphk * initialized, and these only in the simulator. In the actual 58744574Sphk * kernel, any nonzero values here will quickly evaporate. 58844574Sphk */ 58944574Sphk L_CLR(time_offset); 59044574Sphk L_CLR(time_freq); 59132513Sphk#ifdef PPS_SYNC 59250656Sphk pps_tf[0].tv_sec = pps_tf[0].tv_nsec = 0; 59350656Sphk pps_tf[1].tv_sec = pps_tf[1].tv_nsec = 0; 59450656Sphk pps_tf[2].tv_sec = pps_tf[2].tv_nsec = 0; 59544794Sphk pps_fcount = 0; 59644574Sphk L_CLR(pps_freq); 59744574Sphk#endif /* PPS_SYNC */ 59844574Sphk} 5992858Swollman 600108755SpeterSYSINIT(ntpclocks, SI_SUB_CLOCKS, SI_ORDER_MIDDLE, ntp_init, NULL) 60132513Sphk 60244574Sphk/* 60344574Sphk * hardupdate() - local clock update 60444574Sphk * 60544574Sphk * This routine is called by ntp_adjtime() to update the local clock 60644574Sphk * phase and frequency. The implementation is of an adaptive-parameter, 60744574Sphk * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new 60844574Sphk * time and frequency offset estimates for each call. If the kernel PPS 60944574Sphk * discipline code is configured (PPS_SYNC), the PPS signal itself 61044574Sphk * determines the new time offset, instead of the calling argument. 61144574Sphk * Presumably, calls to ntp_adjtime() occur only when the caller 61244574Sphk * believes the local clock is valid within some bound (+-128 ms with 61344574Sphk * NTP). If the caller's time is far different than the PPS time, an 61444574Sphk * argument will ensue, and it's not clear who will lose. 61544574Sphk * 61644574Sphk * For uncompensated quartz crystal oscillators and nominal update 61744574Sphk * intervals less than 256 s, operation should be in phase-lock mode, 61844574Sphk * where the loop is disciplined to phase. For update intervals greater 61944574Sphk * than 1024 s, operation should be in frequency-lock mode, where the 62044574Sphk * loop is disciplined to frequency. Between 256 s and 1024 s, the mode 62144574Sphk * is selected by the STA_MODE status bit. 62244574Sphk */ 62344574Sphkstatic void 62444574Sphkhardupdate(offset) 62544574Sphk long offset; /* clock offset (ns) */ 62644574Sphk{ 62765432Sphk long mtemp; 62844574Sphk l_fp ftemp; 62932513Sphk 63044574Sphk /* 63144574Sphk * Select how the phase is to be controlled and from which 63244574Sphk * source. If the PPS signal is present and enabled to 63344574Sphk * discipline the time, the PPS offset is used; otherwise, the 63444574Sphk * argument offset is used. 63544574Sphk */ 63650656Sphk if (!(time_status & STA_PLL)) 63750656Sphk return; 63865432Sphk if (!(time_status & STA_PPSTIME && time_status & 63965432Sphk STA_PPSSIGNAL)) { 64065432Sphk if (offset > MAXPHASE) 64165432Sphk time_monitor = MAXPHASE; 64265432Sphk else if (offset < -MAXPHASE) 64365432Sphk time_monitor = -MAXPHASE; 64465432Sphk else 64565432Sphk time_monitor = offset; 64665432Sphk L_LINT(time_offset, time_monitor); 64765432Sphk } 64832513Sphk 64944574Sphk /* 65044574Sphk * Select how the frequency is to be controlled and in which 65144574Sphk * mode (PLL or FLL). If the PPS signal is present and enabled 65244574Sphk * to discipline the frequency, the PPS frequency is used; 65344574Sphk * otherwise, the argument offset is used to compute it. 65444574Sphk */ 65544574Sphk if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) { 65644574Sphk time_reftime = time_second; 65744574Sphk return; 65844574Sphk } 65944574Sphk if (time_status & STA_FREQHOLD || time_reftime == 0) 66044574Sphk time_reftime = time_second; 66144574Sphk mtemp = time_second - time_reftime; 66265432Sphk L_LINT(ftemp, time_monitor); 66350656Sphk L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1); 66450656Sphk L_MPY(ftemp, mtemp); 66550656Sphk L_ADD(time_freq, ftemp); 66650656Sphk time_status &= ~STA_MODE; 66765432Sphk if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > 66865432Sphk MAXSEC)) { 66965432Sphk L_LINT(ftemp, (time_monitor << 4) / mtemp); 67044574Sphk L_RSHIFT(ftemp, SHIFT_FLL + 4); 67144574Sphk L_ADD(time_freq, ftemp); 67244574Sphk time_status |= STA_MODE; 67344574Sphk } 67444574Sphk time_reftime = time_second; 67544574Sphk if (L_GINT(time_freq) > MAXFREQ) 67644574Sphk L_LINT(time_freq, MAXFREQ); 67744574Sphk else if (L_GINT(time_freq) < -MAXFREQ) 67844574Sphk L_LINT(time_freq, -MAXFREQ); 67944574Sphk} 68044574Sphk 68144574Sphk#ifdef PPS_SYNC 68232513Sphk/* 68332513Sphk * hardpps() - discipline CPU clock oscillator to external PPS signal 68432513Sphk * 68532513Sphk * This routine is called at each PPS interrupt in order to discipline 68665432Sphk * the CPU clock oscillator to the PPS signal. There are two independent 68765432Sphk * first-order feedback loops, one for the phase, the other for the 68865432Sphk * frequency. The phase loop measures and grooms the PPS phase offset 68965432Sphk * and leaves it in a handy spot for the seconds overflow routine. The 69065432Sphk * frequency loop averages successive PPS phase differences and 69165432Sphk * calculates the PPS frequency offset, which is also processed by the 69265432Sphk * seconds overflow routine. The code requires the caller to capture the 69365432Sphk * time and architecture-dependent hardware counter values in 69465432Sphk * nanoseconds at the on-time PPS signal transition. 69532513Sphk * 69644574Sphk * Note that, on some Unix systems this routine runs at an interrupt 69732513Sphk * priority level higher than the timer interrupt routine hardclock(). 69832513Sphk * Therefore, the variables used are distinct from the hardclock() 69944574Sphk * variables, except for the actual time and frequency variables, which 70044574Sphk * are determined by this routine and updated atomically. 70132513Sphk */ 70232513Sphkvoid 70344574Sphkhardpps(tsp, nsec) 70444574Sphk struct timespec *tsp; /* time at PPS */ 70544574Sphk long nsec; /* hardware counter at PPS */ 70632513Sphk{ 70765432Sphk long u_sec, u_nsec, v_nsec; /* temps */ 70844574Sphk l_fp ftemp; 70932513Sphk 71032513Sphk /* 71165432Sphk * The signal is first processed by a range gate and frequency 71265432Sphk * discriminator. The range gate rejects noise spikes outside 71365432Sphk * the range +-500 us. The frequency discriminator rejects input 71465432Sphk * signals with apparent frequency outside the range 1 +-500 71565432Sphk * PPM. If two hits occur in the same second, we ignore the 71665432Sphk * later hit; if not and a hit occurs outside the range gate, 71765432Sphk * keep the later hit for later comparison, but do not process 71865432Sphk * it. 71932513Sphk */ 72044574Sphk time_status |= STA_PPSSIGNAL | STA_PPSJITTER; 72144574Sphk time_status &= ~(STA_PPSWANDER | STA_PPSERROR); 72244574Sphk pps_valid = PPS_VALID; 72344574Sphk u_sec = tsp->tv_sec; 72444574Sphk u_nsec = tsp->tv_nsec; 72544574Sphk if (u_nsec >= (NANOSECOND >> 1)) { 72644574Sphk u_nsec -= NANOSECOND; 72744574Sphk u_sec++; 72844574Sphk } 72950656Sphk v_nsec = u_nsec - pps_tf[0].tv_nsec; 73075540Sjhay if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND - 73175540Sjhay MAXFREQ) 73244574Sphk return; 73344574Sphk pps_tf[2] = pps_tf[1]; 73444574Sphk pps_tf[1] = pps_tf[0]; 73550656Sphk pps_tf[0].tv_sec = u_sec; 73650656Sphk pps_tf[0].tv_nsec = u_nsec; 73732513Sphk 73832513Sphk /* 73944574Sphk * Compute the difference between the current and previous 74044574Sphk * counter values. If the difference exceeds 0.5 s, assume it 74144574Sphk * has wrapped around, so correct 1.0 s. If the result exceeds 74244574Sphk * the tick interval, the sample point has crossed a tick 74344574Sphk * boundary during the last second, so correct the tick. Very 74444574Sphk * intricate. 74544574Sphk */ 74644666Sphk u_nsec = nsec; 74744574Sphk if (u_nsec > (NANOSECOND >> 1)) 74844574Sphk u_nsec -= NANOSECOND; 74944574Sphk else if (u_nsec < -(NANOSECOND >> 1)) 75044574Sphk u_nsec += NANOSECOND; 75144794Sphk pps_fcount += u_nsec; 75275540Sjhay if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ) 75344574Sphk return; 75444574Sphk time_status &= ~STA_PPSJITTER; 75544574Sphk 75644574Sphk /* 75744574Sphk * A three-stage median filter is used to help denoise the PPS 75832513Sphk * time. The median sample becomes the time offset estimate; the 75932513Sphk * difference between the other two samples becomes the time 76032513Sphk * dispersion (jitter) estimate. 76132513Sphk */ 76250656Sphk if (pps_tf[0].tv_nsec > pps_tf[1].tv_nsec) { 76350656Sphk if (pps_tf[1].tv_nsec > pps_tf[2].tv_nsec) { 76450656Sphk v_nsec = pps_tf[1].tv_nsec; /* 0 1 2 */ 76550656Sphk u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec; 76650656Sphk } else if (pps_tf[2].tv_nsec > pps_tf[0].tv_nsec) { 76750656Sphk v_nsec = pps_tf[0].tv_nsec; /* 2 0 1 */ 76850656Sphk u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec; 76944574Sphk } else { 77050656Sphk v_nsec = pps_tf[2].tv_nsec; /* 0 2 1 */ 77150656Sphk u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec; 77244574Sphk } 77344574Sphk } else { 77450656Sphk if (pps_tf[1].tv_nsec < pps_tf[2].tv_nsec) { 77550656Sphk v_nsec = pps_tf[1].tv_nsec; /* 2 1 0 */ 77650656Sphk u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec; 77765673Sphk } else if (pps_tf[2].tv_nsec < pps_tf[0].tv_nsec) { 77850656Sphk v_nsec = pps_tf[0].tv_nsec; /* 1 0 2 */ 77950656Sphk u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec; 78044574Sphk } else { 78150656Sphk v_nsec = pps_tf[2].tv_nsec; /* 1 2 0 */ 78250656Sphk u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec; 78344574Sphk } 78444574Sphk } 78532513Sphk 78632513Sphk /* 78765673Sphk * Nominal jitter is due to PPS signal noise and interrupt 78865432Sphk * latency. If it exceeds the popcorn threshold, the sample is 78965432Sphk * discarded. otherwise, if so enabled, the time offset is 79065432Sphk * updated. We can tolerate a modest loss of data here without 79165432Sphk * much degrading time accuracy. 79232513Sphk */ 79350656Sphk if (u_nsec > (pps_jitter << PPS_POPCORN)) { 79444574Sphk time_status |= STA_PPSJITTER; 79544574Sphk pps_jitcnt++; 79644574Sphk } else if (time_status & STA_PPSTIME) { 79765432Sphk time_monitor = -v_nsec; 79865432Sphk L_LINT(time_offset, time_monitor); 79932513Sphk } 80044574Sphk pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG; 80150656Sphk u_sec = pps_tf[0].tv_sec - pps_lastsec; 80244574Sphk if (u_sec < (1 << pps_shift)) 80344574Sphk return; 80444574Sphk 80532513Sphk /* 80644574Sphk * At the end of the calibration interval the difference between 80744574Sphk * the first and last counter values becomes the scaled 80844574Sphk * frequency. It will later be divided by the length of the 80944574Sphk * interval to determine the frequency update. If the frequency 81044574Sphk * exceeds a sanity threshold, or if the actual calibration 81144574Sphk * interval is not equal to the expected length, the data are 81244574Sphk * discarded. We can tolerate a modest loss of data here without 81365432Sphk * much degrading frequency accuracy. 81432513Sphk */ 81544574Sphk pps_calcnt++; 81644794Sphk v_nsec = -pps_fcount; 81750656Sphk pps_lastsec = pps_tf[0].tv_sec; 81844794Sphk pps_fcount = 0; 81944574Sphk u_nsec = MAXFREQ << pps_shift; 82044574Sphk if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 << 82144574Sphk pps_shift)) { 82244574Sphk time_status |= STA_PPSERROR; 82332513Sphk pps_errcnt++; 82432513Sphk return; 82532513Sphk } 82632513Sphk 82732513Sphk /* 82850656Sphk * Here the raw frequency offset and wander (stability) is 82950656Sphk * calculated. If the wander is less than the wander threshold 83050656Sphk * for four consecutive averaging intervals, the interval is 83150656Sphk * doubled; if it is greater than the threshold for four 83250656Sphk * consecutive intervals, the interval is halved. The scaled 83350656Sphk * frequency offset is converted to frequency offset. The 83450656Sphk * stability metric is calculated as the average of recent 83550656Sphk * frequency changes, but is used only for performance 83644574Sphk * monitoring. 83732513Sphk */ 83844574Sphk L_LINT(ftemp, v_nsec); 83944574Sphk L_RSHIFT(ftemp, pps_shift); 84044574Sphk L_SUB(ftemp, pps_freq); 84144574Sphk u_nsec = L_GINT(ftemp); 84250656Sphk if (u_nsec > PPS_MAXWANDER) { 84350656Sphk L_LINT(ftemp, PPS_MAXWANDER); 84444574Sphk pps_intcnt--; 84544574Sphk time_status |= STA_PPSWANDER; 84632513Sphk pps_stbcnt++; 84750656Sphk } else if (u_nsec < -PPS_MAXWANDER) { 84850656Sphk L_LINT(ftemp, -PPS_MAXWANDER); 84944574Sphk pps_intcnt--; 85032513Sphk time_status |= STA_PPSWANDER; 85144574Sphk pps_stbcnt++; 85244574Sphk } else { 85344574Sphk pps_intcnt++; 85432513Sphk } 85565432Sphk if (pps_intcnt >= 4) { 85644574Sphk pps_intcnt = 4; 85750656Sphk if (pps_shift < pps_shiftmax) { 85844574Sphk pps_shift++; 85944574Sphk pps_intcnt = 0; 86032513Sphk } 86165432Sphk } else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) { 86244574Sphk pps_intcnt = -4; 86344574Sphk if (pps_shift > PPS_FAVG) { 86444574Sphk pps_shift--; 86544574Sphk pps_intcnt = 0; 86644574Sphk } 86732513Sphk } 86844574Sphk if (u_nsec < 0) 86944574Sphk u_nsec = -u_nsec; 87044574Sphk pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG; 87132513Sphk 87232513Sphk /* 87350656Sphk * The PPS frequency is recalculated and clamped to the maximum 87450656Sphk * MAXFREQ. If enabled, the system clock frequency is updated as 87550656Sphk * well. 87632513Sphk */ 87744574Sphk L_ADD(pps_freq, ftemp); 87844574Sphk u_nsec = L_GINT(pps_freq); 87944574Sphk if (u_nsec > MAXFREQ) 88044574Sphk L_LINT(pps_freq, MAXFREQ); 88144574Sphk else if (u_nsec < -MAXFREQ) 88244574Sphk L_LINT(pps_freq, -MAXFREQ); 88365432Sphk if (time_status & STA_PPSFREQ) 88444574Sphk time_freq = pps_freq; 88532513Sphk} 88632513Sphk#endif /* PPS_SYNC */ 88794754Sphk 88894754Sphk#ifndef _SYS_SYSPROTO_H_ 88994754Sphkstruct adjtime_args { 89094754Sphk struct timeval *delta; 89194754Sphk struct timeval *olddelta; 89294754Sphk}; 89394754Sphk#endif 89494754Sphk/* 89594754Sphk * MPSAFE 89694754Sphk */ 89794754Sphk/* ARGSUSED */ 89894754Sphkint 89994754Sphkadjtime(struct thread *td, struct adjtime_args *uap) 90094754Sphk{ 90194754Sphk struct timeval atv; 90294754Sphk int error; 90394754Sphk 90495036Sphk if ((error = suser(td))) 90595036Sphk return (error); 90695036Sphk 90794754Sphk mtx_lock(&Giant); 90894754Sphk if (uap->olddelta) { 90994754Sphk atv.tv_sec = time_adjtime / 1000000; 91094754Sphk atv.tv_usec = time_adjtime % 1000000; 91194754Sphk if (atv.tv_usec < 0) { 91294754Sphk atv.tv_usec += 1000000; 91394754Sphk atv.tv_sec--; 91494754Sphk } 91594754Sphk error = copyout(&atv, uap->olddelta, sizeof(atv)); 91694754Sphk if (error) 91794754Sphk goto done2; 91894754Sphk } 91994754Sphk if (uap->delta) { 92094754Sphk error = copyin(uap->delta, &atv, sizeof(atv)); 92194754Sphk if (error) 92294754Sphk goto done2; 92394754Sphk time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec; 92494754Sphk } 92594754Sphkdone2: 92694754Sphk mtx_unlock(&Giant); 92794754Sphk return (error); 92894754Sphk} 92994754Sphk 930