19454b2d8SWarner Losh /*- 29454b2d8SWarner Losh *********************************************************************** 33f31c649SGarrett Wollman * * 424dbea46SJohn Hay * Copyright (c) David L. Mills 1993-2001 * 53f31c649SGarrett Wollman * * 6c68996e2SPoul-Henning Kamp * Permission to use, copy, modify, and distribute this software and * 7c68996e2SPoul-Henning Kamp * its documentation for any purpose and without fee is hereby * 8c68996e2SPoul-Henning Kamp * granted, provided that the above copyright notice appears in all * 9c68996e2SPoul-Henning Kamp * copies and that both the copyright notice and this permission * 10c68996e2SPoul-Henning Kamp * notice appear in supporting documentation, and that the name * 11c68996e2SPoul-Henning Kamp * University of Delaware not be used in advertising or publicity * 12c68996e2SPoul-Henning Kamp * pertaining to distribution of the software without specific, * 13c68996e2SPoul-Henning Kamp * written prior permission. The University of Delaware makes no * 14c68996e2SPoul-Henning Kamp * representations about the suitability this software for any * 15c68996e2SPoul-Henning Kamp * purpose. It is provided "as is" without express or implied * 16c68996e2SPoul-Henning Kamp * warranty. * 173f31c649SGarrett Wollman * * 18c68996e2SPoul-Henning Kamp **********************************************************************/ 193f31c649SGarrett Wollman 203f31c649SGarrett Wollman /* 21c68996e2SPoul-Henning Kamp * Adapted from the original sources for FreeBSD and timecounters by: 2232c20357SPoul-Henning Kamp * Poul-Henning Kamp <phk@FreeBSD.org>. 233f31c649SGarrett Wollman * 24c68996e2SPoul-Henning Kamp * The 32bit version of the "LP" macros seems a bit past its "sell by" 25c68996e2SPoul-Henning Kamp * date so I have retained only the 64bit version and included it directly 26c68996e2SPoul-Henning Kamp * in this file. 27885bd8e4SJohn Hay * 28c68996e2SPoul-Henning Kamp * Only minor changes done to interface with the timecounters over in 29c68996e2SPoul-Henning Kamp * sys/kern/kern_clock.c. Some of the comments below may be (even more) 30c68996e2SPoul-Henning Kamp * confusing and/or plain wrong in that context. 313f31c649SGarrett Wollman */ 32e0d781f3SEivind Eklund 33677b542eSDavid E. O'Brien #include <sys/cdefs.h> 34677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 35677b542eSDavid E. O'Brien 3632c20357SPoul-Henning Kamp #include "opt_ntp.h" 3732c20357SPoul-Henning Kamp 383f31c649SGarrett Wollman #include <sys/param.h> 393f31c649SGarrett Wollman #include <sys/systm.h> 40d2d3e875SBruce Evans #include <sys/sysproto.h> 415c7e270fSAndriy Gapon #include <sys/eventhandler.h> 423f31c649SGarrett Wollman #include <sys/kernel.h> 43acd3428bSRobert Watson #include <sys/priv.h> 443f31c649SGarrett Wollman #include <sys/proc.h> 456f1e8c18SMatthew Dillon #include <sys/lock.h> 466f1e8c18SMatthew Dillon #include <sys/mutex.h> 47c68996e2SPoul-Henning Kamp #include <sys/time.h> 483f31c649SGarrett Wollman #include <sys/timex.h> 4991266b96SPoul-Henning Kamp #include <sys/timetc.h> 50938ee3ceSPoul-Henning Kamp #include <sys/timepps.h> 51b88ec951SJohn Baldwin #include <sys/syscallsubr.h> 523f31c649SGarrett Wollman #include <sys/sysctl.h> 533f31c649SGarrett Wollman 54de5b1952SAlexander Leidinger #ifdef PPS_SYNC 55de5b1952SAlexander Leidinger FEATURE(pps_sync, "Support usage of external PPS signal by kernel PLL"); 56de5b1952SAlexander Leidinger #endif 57de5b1952SAlexander Leidinger 583f31c649SGarrett Wollman /* 59c68996e2SPoul-Henning Kamp * Single-precision macros for 64-bit machines 603f31c649SGarrett Wollman */ 61bcfe6d8bSPoul-Henning Kamp typedef int64_t l_fp; 62c68996e2SPoul-Henning Kamp #define L_ADD(v, u) ((v) += (u)) 63c68996e2SPoul-Henning Kamp #define L_SUB(v, u) ((v) -= (u)) 64bcfe6d8bSPoul-Henning Kamp #define L_ADDHI(v, a) ((v) += (int64_t)(a) << 32) 65c68996e2SPoul-Henning Kamp #define L_NEG(v) ((v) = -(v)) 66c68996e2SPoul-Henning Kamp #define L_RSHIFT(v, n) \ 67c68996e2SPoul-Henning Kamp do { \ 68c68996e2SPoul-Henning Kamp if ((v) < 0) \ 69c68996e2SPoul-Henning Kamp (v) = -(-(v) >> (n)); \ 70c68996e2SPoul-Henning Kamp else \ 71c68996e2SPoul-Henning Kamp (v) = (v) >> (n); \ 72c68996e2SPoul-Henning Kamp } while (0) 73c68996e2SPoul-Henning Kamp #define L_MPY(v, a) ((v) *= (a)) 74c68996e2SPoul-Henning Kamp #define L_CLR(v) ((v) = 0) 75c68996e2SPoul-Henning Kamp #define L_ISNEG(v) ((v) < 0) 76bcfe6d8bSPoul-Henning Kamp #define L_LINT(v, a) ((v) = (int64_t)(a) << 32) 77c68996e2SPoul-Henning Kamp #define L_GINT(v) ((v) < 0 ? -(-(v) >> 32) : (v) >> 32) 786f70df15SPoul-Henning Kamp 796f70df15SPoul-Henning Kamp /* 80c68996e2SPoul-Henning Kamp * Generic NTP kernel interface 816f70df15SPoul-Henning Kamp * 82c68996e2SPoul-Henning Kamp * These routines constitute the Network Time Protocol (NTP) interfaces 83c68996e2SPoul-Henning Kamp * for user and daemon application programs. The ntp_gettime() routine 84c68996e2SPoul-Henning Kamp * provides the time, maximum error (synch distance) and estimated error 85c68996e2SPoul-Henning Kamp * (dispersion) to client user application programs. The ntp_adjtime() 86c68996e2SPoul-Henning Kamp * routine is used by the NTP daemon to adjust the system clock to an 87c68996e2SPoul-Henning Kamp * externally derived time. The time offset and related variables set by 88c68996e2SPoul-Henning Kamp * this routine are used by other routines in this module to adjust the 89c68996e2SPoul-Henning Kamp * phase and frequency of the clock discipline loop which controls the 90c68996e2SPoul-Henning Kamp * system clock. 916f70df15SPoul-Henning Kamp * 92f425c1f6SPoul-Henning Kamp * When the kernel time is reckoned directly in nanoseconds (NTP_NANO 93c68996e2SPoul-Henning Kamp * defined), the time at each tick interrupt is derived directly from 94c68996e2SPoul-Henning Kamp * the kernel time variable. When the kernel time is reckoned in 95f425c1f6SPoul-Henning Kamp * microseconds, (NTP_NANO undefined), the time is derived from the 96f425c1f6SPoul-Henning Kamp * kernel time variable together with a variable representing the 97f425c1f6SPoul-Henning Kamp * leftover nanoseconds at the last tick interrupt. In either case, the 98f425c1f6SPoul-Henning Kamp * current nanosecond time is reckoned from these values plus an 99f425c1f6SPoul-Henning Kamp * interpolated value derived by the clock routines in another 100f425c1f6SPoul-Henning Kamp * architecture-specific module. The interpolation can use either a 101f425c1f6SPoul-Henning Kamp * dedicated counter or a processor cycle counter (PCC) implemented in 102f425c1f6SPoul-Henning Kamp * some architectures. 1036f70df15SPoul-Henning Kamp * 104c68996e2SPoul-Henning Kamp * Note that all routines must run at priority splclock or higher. 1056f70df15SPoul-Henning Kamp */ 106c68996e2SPoul-Henning Kamp /* 107c68996e2SPoul-Henning Kamp * Phase/frequency-lock loop (PLL/FLL) definitions 108c68996e2SPoul-Henning Kamp * 109c68996e2SPoul-Henning Kamp * The nanosecond clock discipline uses two variable types, time 110c68996e2SPoul-Henning Kamp * variables and frequency variables. Both types are represented as 64- 111c68996e2SPoul-Henning Kamp * bit fixed-point quantities with the decimal point between two 32-bit 112c68996e2SPoul-Henning Kamp * halves. On a 32-bit machine, each half is represented as a single 113c68996e2SPoul-Henning Kamp * word and mathematical operations are done using multiple-precision 114c68996e2SPoul-Henning Kamp * arithmetic. On a 64-bit machine, ordinary computer arithmetic is 115c68996e2SPoul-Henning Kamp * used. 116c68996e2SPoul-Henning Kamp * 117c68996e2SPoul-Henning Kamp * A time variable is a signed 64-bit fixed-point number in ns and 118c68996e2SPoul-Henning Kamp * fraction. It represents the remaining time offset to be amortized 119c68996e2SPoul-Henning Kamp * over succeeding tick interrupts. The maximum time offset is about 120f425c1f6SPoul-Henning Kamp * 0.5 s and the resolution is about 2.3e-10 ns. 121c68996e2SPoul-Henning Kamp * 122c68996e2SPoul-Henning Kamp * 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 123c68996e2SPoul-Henning Kamp * 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 124c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 125c68996e2SPoul-Henning Kamp * |s s s| ns | 126c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 127c68996e2SPoul-Henning Kamp * | fraction | 128c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 129c68996e2SPoul-Henning Kamp * 130c68996e2SPoul-Henning Kamp * A frequency variable is a signed 64-bit fixed-point number in ns/s 131c68996e2SPoul-Henning Kamp * and fraction. It represents the ns and fraction to be added to the 132c68996e2SPoul-Henning Kamp * kernel time variable at each second. The maximum frequency offset is 133f425c1f6SPoul-Henning Kamp * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s. 134c68996e2SPoul-Henning Kamp * 135c68996e2SPoul-Henning Kamp * 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 136c68996e2SPoul-Henning Kamp * 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 137c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 138c68996e2SPoul-Henning Kamp * |s s s s s s s s s s s s s| ns/s | 139c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 140c68996e2SPoul-Henning Kamp * | fraction | 141c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 142c68996e2SPoul-Henning Kamp */ 143c68996e2SPoul-Henning Kamp /* 144c68996e2SPoul-Henning Kamp * The following variables establish the state of the PLL/FLL and the 145c68996e2SPoul-Henning Kamp * residual time and frequency offset of the local clock. 146c68996e2SPoul-Henning Kamp */ 147c68996e2SPoul-Henning Kamp #define SHIFT_PLL 4 /* PLL loop gain (shift) */ 148c68996e2SPoul-Henning Kamp #define SHIFT_FLL 2 /* FLL loop gain (shift) */ 149c68996e2SPoul-Henning Kamp 150c68996e2SPoul-Henning Kamp static int time_state = TIME_OK; /* clock state */ 1516cedd609SLawrence Stewart int time_status = STA_UNSYNC; /* clock status bits */ 15297804a5cSPoul-Henning Kamp static long time_tai; /* TAI offset (s) */ 15397804a5cSPoul-Henning Kamp static long time_monitor; /* last time offset scaled (ns) */ 154c68996e2SPoul-Henning Kamp static long time_constant; /* poll interval (shift) (s) */ 155c68996e2SPoul-Henning Kamp static long time_precision = 1; /* clock precision (ns) */ 156c68996e2SPoul-Henning Kamp static long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */ 1576cedd609SLawrence Stewart long time_esterror = MAXPHASE / 1000; /* estimated error (us) */ 158969fc29eSIan Lepore static long time_reftime; /* uptime at last adjustment (s) */ 159c68996e2SPoul-Henning Kamp static l_fp time_offset; /* time offset (ns) */ 160c68996e2SPoul-Henning Kamp static l_fp time_freq; /* frequency offset (ns/s) */ 16197804a5cSPoul-Henning Kamp static l_fp time_adj; /* tick adjust (ns/s) */ 1623f31c649SGarrett Wollman 163e1d970f1SPoul-Henning Kamp static int64_t time_adjtime; /* correction from adjtime(2) (usec) */ 164e1d970f1SPoul-Henning Kamp 165*364c516cSKonstantin Belousov static struct mtx ntpadj_lock; 166*364c516cSKonstantin Belousov MTX_SYSINIT(ntpadj, &ntpadj_lock, "ntpadj", 167*364c516cSKonstantin Belousov #ifdef PPS_SYNC 168*364c516cSKonstantin Belousov MTX_SPIN 169*364c516cSKonstantin Belousov #else 170*364c516cSKonstantin Belousov MTX_DEF 171*364c516cSKonstantin Belousov #endif 172*364c516cSKonstantin Belousov ); 173*364c516cSKonstantin Belousov 174*364c516cSKonstantin Belousov /* 175*364c516cSKonstantin Belousov * When PPS_SYNC is defined, hardpps() function is provided which can 176*364c516cSKonstantin Belousov * be legitimately called from interrupt filters. Due to this, use 177*364c516cSKonstantin Belousov * spinlock for ntptime state protection, otherwise sleepable mutex is 178*364c516cSKonstantin Belousov * adequate. 179*364c516cSKonstantin Belousov */ 180*364c516cSKonstantin Belousov #ifdef PPS_SYNC 181*364c516cSKonstantin Belousov #define NTPADJ_LOCK() mtx_lock_spin(&ntpadj_lock) 182*364c516cSKonstantin Belousov #define NTPADJ_UNLOCK() mtx_unlock_spin(&ntpadj_lock) 183*364c516cSKonstantin Belousov #else 184*364c516cSKonstantin Belousov #define NTPADJ_LOCK() mtx_lock(&ntpadj_lock) 185*364c516cSKonstantin Belousov #define NTPADJ_UNLOCK() mtx_unlock(&ntpadj_lock) 186*364c516cSKonstantin Belousov #endif 187*364c516cSKonstantin Belousov #define NTPADJ_ASSERT_LOCKED() mtx_assert(&ntpadj_lock, MA_OWNED) 188*364c516cSKonstantin Belousov 1893f31c649SGarrett Wollman #ifdef PPS_SYNC 1903f31c649SGarrett Wollman /* 191c68996e2SPoul-Henning Kamp * The following variables are used when a pulse-per-second (PPS) signal 192c68996e2SPoul-Henning Kamp * is available and connected via a modem control lead. They establish 193c68996e2SPoul-Henning Kamp * the engineering parameters of the clock discipline loop when 194c68996e2SPoul-Henning Kamp * controlled by the PPS signal. 1953f31c649SGarrett Wollman */ 196c68996e2SPoul-Henning Kamp #define PPS_FAVG 2 /* min freq avg interval (s) (shift) */ 19724dbea46SJohn Hay #define PPS_FAVGDEF 8 /* default freq avg int (s) (shift) */ 19882e84c5bSPoul-Henning Kamp #define PPS_FAVGMAX 15 /* max freq avg interval (s) (shift) */ 199c68996e2SPoul-Henning Kamp #define PPS_PAVG 4 /* phase avg interval (s) (shift) */ 200c68996e2SPoul-Henning Kamp #define PPS_VALID 120 /* PPS signal watchdog max (s) */ 20182e84c5bSPoul-Henning Kamp #define PPS_MAXWANDER 100000 /* max PPS wander (ns/s) */ 20282e84c5bSPoul-Henning Kamp #define PPS_POPCORN 2 /* popcorn spike threshold (shift) */ 203c68996e2SPoul-Henning Kamp 20482e84c5bSPoul-Henning Kamp static struct timespec pps_tf[3]; /* phase median filter */ 205c68996e2SPoul-Henning Kamp static l_fp pps_freq; /* scaled frequency offset (ns/s) */ 206f425c1f6SPoul-Henning Kamp static long pps_fcount; /* frequency accumulator */ 20782e84c5bSPoul-Henning Kamp static long pps_jitter; /* nominal jitter (ns) */ 20882e84c5bSPoul-Henning Kamp static long pps_stabil; /* nominal stability (scaled ns/s) */ 209c68996e2SPoul-Henning Kamp static long pps_lastsec; /* time at last calibration (s) */ 210c68996e2SPoul-Henning Kamp static int pps_valid; /* signal watchdog counter */ 211c68996e2SPoul-Henning Kamp static int pps_shift = PPS_FAVG; /* interval duration (s) (shift) */ 21282e84c5bSPoul-Henning Kamp static int pps_shiftmax = PPS_FAVGDEF; /* max interval duration (s) (shift) */ 213c68996e2SPoul-Henning Kamp static int pps_intcnt; /* wander counter */ 2146f70df15SPoul-Henning Kamp 2156f70df15SPoul-Henning Kamp /* 2166f70df15SPoul-Henning Kamp * PPS signal quality monitors 2176f70df15SPoul-Henning Kamp */ 218c68996e2SPoul-Henning Kamp static long pps_calcnt; /* calibration intervals */ 219c68996e2SPoul-Henning Kamp static long pps_jitcnt; /* jitter limit exceeded */ 220c68996e2SPoul-Henning Kamp static long pps_stbcnt; /* stability limit exceeded */ 221c68996e2SPoul-Henning Kamp static long pps_errcnt; /* calibration errors */ 2223f31c649SGarrett Wollman #endif /* PPS_SYNC */ 223c68996e2SPoul-Henning Kamp /* 224c68996e2SPoul-Henning Kamp * End of phase/frequency-lock loop (PLL/FLL) definitions 225c68996e2SPoul-Henning Kamp */ 2263f31c649SGarrett Wollman 227c68996e2SPoul-Henning Kamp static void ntp_init(void); 228c68996e2SPoul-Henning Kamp static void hardupdate(long offset); 229932cfd41SMark Santcroos static void ntp_gettime1(struct ntptimeval *ntvp); 230*364c516cSKonstantin Belousov static bool ntp_is_time_error(int tsl); 231c68996e2SPoul-Henning Kamp 232*364c516cSKonstantin Belousov static bool 233*364c516cSKonstantin Belousov ntp_is_time_error(int tsl) 234c68996e2SPoul-Henning Kamp { 235*364c516cSKonstantin Belousov 236c68996e2SPoul-Henning Kamp /* 237c68996e2SPoul-Henning Kamp * Status word error decode. If any of these conditions occur, 238c68996e2SPoul-Henning Kamp * an error is returned, instead of the status word. Most 239c68996e2SPoul-Henning Kamp * applications will care only about the fact the system clock 240c68996e2SPoul-Henning Kamp * may not be trusted, not about the details. 241c68996e2SPoul-Henning Kamp * 242c68996e2SPoul-Henning Kamp * Hardware or software error 243c68996e2SPoul-Henning Kamp */ 244*364c516cSKonstantin Belousov if ((tsl & (STA_UNSYNC | STA_CLOCKERR)) || 245c68996e2SPoul-Henning Kamp 246c68996e2SPoul-Henning Kamp /* 247c68996e2SPoul-Henning Kamp * PPS signal lost when either time or frequency synchronization 248c68996e2SPoul-Henning Kamp * requested 249c68996e2SPoul-Henning Kamp */ 250*364c516cSKonstantin Belousov (tsl & (STA_PPSFREQ | STA_PPSTIME) && 251*364c516cSKonstantin Belousov !(tsl & STA_PPSSIGNAL)) || 252c68996e2SPoul-Henning Kamp 253c68996e2SPoul-Henning Kamp /* 254c68996e2SPoul-Henning Kamp * PPS jitter exceeded when time synchronization requested 255c68996e2SPoul-Henning Kamp */ 256*364c516cSKonstantin Belousov (tsl & STA_PPSTIME && tsl & STA_PPSJITTER) || 257c68996e2SPoul-Henning Kamp 258c68996e2SPoul-Henning Kamp /* 259c68996e2SPoul-Henning Kamp * PPS wander exceeded or calibration error when frequency 260c68996e2SPoul-Henning Kamp * synchronization requested 261c68996e2SPoul-Henning Kamp */ 262*364c516cSKonstantin Belousov (tsl & STA_PPSFREQ && 263*364c516cSKonstantin Belousov tsl & (STA_PPSWANDER | STA_PPSERROR))) 264*364c516cSKonstantin Belousov return (true); 2659a9ae42aSAndriy Gapon 266*364c516cSKonstantin Belousov return (false); 2679a9ae42aSAndriy Gapon } 2689a9ae42aSAndriy Gapon 2699a9ae42aSAndriy Gapon static void 2709a9ae42aSAndriy Gapon ntp_gettime1(struct ntptimeval *ntvp) 2719a9ae42aSAndriy Gapon { 2729a9ae42aSAndriy Gapon struct timespec atv; /* nanosecond time */ 2739a9ae42aSAndriy Gapon 274*364c516cSKonstantin Belousov NTPADJ_ASSERT_LOCKED(); 2759a9ae42aSAndriy Gapon 2769a9ae42aSAndriy Gapon nanotime(&atv); 2779a9ae42aSAndriy Gapon ntvp->time.tv_sec = atv.tv_sec; 2789a9ae42aSAndriy Gapon ntvp->time.tv_nsec = atv.tv_nsec; 2799a9ae42aSAndriy Gapon ntvp->maxerror = time_maxerror; 2809a9ae42aSAndriy Gapon ntvp->esterror = time_esterror; 2819a9ae42aSAndriy Gapon ntvp->tai = time_tai; 2829a9ae42aSAndriy Gapon ntvp->time_state = time_state; 2839a9ae42aSAndriy Gapon 284*364c516cSKonstantin Belousov if (ntp_is_time_error(time_status)) 285932cfd41SMark Santcroos ntvp->time_state = TIME_ERROR; 286932cfd41SMark Santcroos } 287932cfd41SMark Santcroos 2889b7fe7e4SMark Santcroos /* 2899b7fe7e4SMark Santcroos * ntp_gettime() - NTP user application interface 2909b7fe7e4SMark Santcroos * 291873fbcd7SRobert Watson * See the timex.h header file for synopsis and API description. Note that 292873fbcd7SRobert Watson * the TAI offset is returned in the ntvtimeval.tai structure member. 2939b7fe7e4SMark Santcroos */ 294932cfd41SMark Santcroos #ifndef _SYS_SYSPROTO_H_ 295932cfd41SMark Santcroos struct ntp_gettime_args { 296932cfd41SMark Santcroos struct ntptimeval *ntvp; 297932cfd41SMark Santcroos }; 298932cfd41SMark Santcroos #endif 299932cfd41SMark Santcroos /* ARGSUSED */ 300932cfd41SMark Santcroos int 3018451d0ddSKip Macy sys_ntp_gettime(struct thread *td, struct ntp_gettime_args *uap) 302932cfd41SMark Santcroos { 303932cfd41SMark Santcroos struct ntptimeval ntv; 304932cfd41SMark Santcroos 305*364c516cSKonstantin Belousov NTPADJ_LOCK(); 306932cfd41SMark Santcroos ntp_gettime1(&ntv); 307*364c516cSKonstantin Belousov NTPADJ_UNLOCK(); 308932cfd41SMark Santcroos 309fe18f385SWarner Losh td->td_retval[0] = ntv.time_state; 310932cfd41SMark Santcroos return (copyout(&ntv, uap->ntvp, sizeof(ntv))); 311932cfd41SMark Santcroos } 312932cfd41SMark Santcroos 313932cfd41SMark Santcroos static int 314932cfd41SMark Santcroos ntp_sysctl(SYSCTL_HANDLER_ARGS) 315932cfd41SMark Santcroos { 316932cfd41SMark Santcroos struct ntptimeval ntv; /* temporary structure */ 317932cfd41SMark Santcroos 318*364c516cSKonstantin Belousov NTPADJ_LOCK(); 319932cfd41SMark Santcroos ntp_gettime1(&ntv); 320*364c516cSKonstantin Belousov NTPADJ_UNLOCK(); 321932cfd41SMark Santcroos 322932cfd41SMark Santcroos return (sysctl_handle_opaque(oidp, &ntv, sizeof(ntv), req)); 323c68996e2SPoul-Henning Kamp } 324c68996e2SPoul-Henning Kamp 325c68996e2SPoul-Henning Kamp SYSCTL_NODE(_kern, OID_AUTO, ntp_pll, CTLFLAG_RW, 0, ""); 326*364c516cSKonstantin Belousov SYSCTL_PROC(_kern_ntp_pll, OID_AUTO, gettime, CTLTYPE_OPAQUE | CTLFLAG_RD | 327*364c516cSKonstantin Belousov CTLFLAG_MPSAFE, 0, sizeof(struct ntptimeval) , ntp_sysctl, "S,ntptimeval", 328*364c516cSKonstantin Belousov ""); 329c68996e2SPoul-Henning Kamp 3305968e18bSPoul-Henning Kamp #ifdef PPS_SYNC 3313eb9ab52SEitan Adler SYSCTL_INT(_kern_ntp_pll, OID_AUTO, pps_shiftmax, CTLFLAG_RW, 3323eb9ab52SEitan Adler &pps_shiftmax, 0, "Max interval duration (sec) (shift)"); 3333eb9ab52SEitan Adler SYSCTL_INT(_kern_ntp_pll, OID_AUTO, pps_shift, CTLFLAG_RW, 3343eb9ab52SEitan Adler &pps_shift, 0, "Interval duration (sec) (shift)"); 335240577c2SMatthew D Fleming SYSCTL_LONG(_kern_ntp_pll, OID_AUTO, time_monitor, CTLFLAG_RD, 3363eb9ab52SEitan Adler &time_monitor, 0, "Last time offset scaled (ns)"); 3377fd299cbSPoul-Henning Kamp 338*364c516cSKonstantin Belousov SYSCTL_S64(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD | CTLFLAG_MPSAFE, 339*364c516cSKonstantin Belousov &pps_freq, 0, 340*364c516cSKonstantin Belousov "Scaled frequency offset (ns/sec)"); 341*364c516cSKonstantin Belousov SYSCTL_S64(_kern_ntp_pll, OID_AUTO, time_freq, CTLFLAG_RD | CTLFLAG_MPSAFE, 342*364c516cSKonstantin Belousov &time_freq, 0, 343*364c516cSKonstantin Belousov "Frequency offset (ns/sec)"); 3445968e18bSPoul-Henning Kamp #endif 345873fbcd7SRobert Watson 346c68996e2SPoul-Henning Kamp /* 347c68996e2SPoul-Henning Kamp * ntp_adjtime() - NTP daemon application interface 348c68996e2SPoul-Henning Kamp * 349873fbcd7SRobert Watson * See the timex.h header file for synopsis and API description. Note that 350873fbcd7SRobert Watson * the timex.constant structure member has a dual purpose to set the time 351873fbcd7SRobert Watson * constant and to set the TAI offset. 352c68996e2SPoul-Henning Kamp */ 353c68996e2SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 354c68996e2SPoul-Henning Kamp struct ntp_adjtime_args { 355c68996e2SPoul-Henning Kamp struct timex *tp; 356c68996e2SPoul-Henning Kamp }; 357c68996e2SPoul-Henning Kamp #endif 358c68996e2SPoul-Henning Kamp 359c68996e2SPoul-Henning Kamp int 3608451d0ddSKip Macy sys_ntp_adjtime(struct thread *td, struct ntp_adjtime_args *uap) 361c68996e2SPoul-Henning Kamp { 362c68996e2SPoul-Henning Kamp struct timex ntv; /* temporary structure */ 363f425c1f6SPoul-Henning Kamp long freq; /* frequency ns/s) */ 364c68996e2SPoul-Henning Kamp int modes; /* mode bits from structure */ 365*364c516cSKonstantin Belousov int error, retval; 366c68996e2SPoul-Henning Kamp 367c68996e2SPoul-Henning Kamp error = copyin((caddr_t)uap->tp, (caddr_t)&ntv, sizeof(ntv)); 368c68996e2SPoul-Henning Kamp if (error) 369c68996e2SPoul-Henning Kamp return (error); 370c68996e2SPoul-Henning Kamp 371c68996e2SPoul-Henning Kamp /* 372c68996e2SPoul-Henning Kamp * Update selected clock variables - only the superuser can 373c68996e2SPoul-Henning Kamp * change anything. Note that there is no error checking here on 374c68996e2SPoul-Henning Kamp * the assumption the superuser should know what it is doing. 37597804a5cSPoul-Henning Kamp * Note that either the time constant or TAI offset are loaded 37624dbea46SJohn Hay * from the ntv.constant member, depending on the mode bits. If 37724dbea46SJohn Hay * the STA_PLL bit in the status word is cleared, the state and 37824dbea46SJohn Hay * status words are reset to the initial values at boot. 379c68996e2SPoul-Henning Kamp */ 380c68996e2SPoul-Henning Kamp modes = ntv.modes; 381fafbe352SPoul-Henning Kamp if (modes) 382acd3428bSRobert Watson error = priv_check(td, PRIV_NTP_ADJTIME); 383*364c516cSKonstantin Belousov if (error != 0) 384*364c516cSKonstantin Belousov return (error); 385*364c516cSKonstantin Belousov NTPADJ_LOCK(); 386c68996e2SPoul-Henning Kamp if (modes & MOD_MAXERROR) 387c68996e2SPoul-Henning Kamp time_maxerror = ntv.maxerror; 388c68996e2SPoul-Henning Kamp if (modes & MOD_ESTERROR) 389c68996e2SPoul-Henning Kamp time_esterror = ntv.esterror; 390c68996e2SPoul-Henning Kamp if (modes & MOD_STATUS) { 39124dbea46SJohn Hay if (time_status & STA_PLL && !(ntv.status & STA_PLL)) { 39224dbea46SJohn Hay time_state = TIME_OK; 39324dbea46SJohn Hay time_status = STA_UNSYNC; 39424dbea46SJohn Hay #ifdef PPS_SYNC 39524dbea46SJohn Hay pps_shift = PPS_FAVG; 39624dbea46SJohn Hay #endif /* PPS_SYNC */ 39724dbea46SJohn Hay } 398c68996e2SPoul-Henning Kamp time_status &= STA_RONLY; 399c68996e2SPoul-Henning Kamp time_status |= ntv.status & ~STA_RONLY; 400c68996e2SPoul-Henning Kamp } 401f425c1f6SPoul-Henning Kamp if (modes & MOD_TIMECONST) { 402f425c1f6SPoul-Henning Kamp if (ntv.constant < 0) 403f425c1f6SPoul-Henning Kamp time_constant = 0; 404f425c1f6SPoul-Henning Kamp else if (ntv.constant > MAXTC) 405f425c1f6SPoul-Henning Kamp time_constant = MAXTC; 406f425c1f6SPoul-Henning Kamp else 407c68996e2SPoul-Henning Kamp time_constant = ntv.constant; 408f425c1f6SPoul-Henning Kamp } 40997804a5cSPoul-Henning Kamp if (modes & MOD_TAI) { 41097804a5cSPoul-Henning Kamp if (ntv.constant > 0) /* XXX zero & negative numbers ? */ 41197804a5cSPoul-Henning Kamp time_tai = ntv.constant; 41297804a5cSPoul-Henning Kamp } 41382e84c5bSPoul-Henning Kamp #ifdef PPS_SYNC 41482e84c5bSPoul-Henning Kamp if (modes & MOD_PPSMAX) { 41582e84c5bSPoul-Henning Kamp if (ntv.shift < PPS_FAVG) 41682e84c5bSPoul-Henning Kamp pps_shiftmax = PPS_FAVG; 41782e84c5bSPoul-Henning Kamp else if (ntv.shift > PPS_FAVGMAX) 41882e84c5bSPoul-Henning Kamp pps_shiftmax = PPS_FAVGMAX; 41982e84c5bSPoul-Henning Kamp else 42082e84c5bSPoul-Henning Kamp pps_shiftmax = ntv.shift; 42182e84c5bSPoul-Henning Kamp } 42282e84c5bSPoul-Henning Kamp #endif /* PPS_SYNC */ 423c68996e2SPoul-Henning Kamp if (modes & MOD_NANO) 424c68996e2SPoul-Henning Kamp time_status |= STA_NANO; 425c68996e2SPoul-Henning Kamp if (modes & MOD_MICRO) 426c68996e2SPoul-Henning Kamp time_status &= ~STA_NANO; 427c68996e2SPoul-Henning Kamp if (modes & MOD_CLKB) 428c68996e2SPoul-Henning Kamp time_status |= STA_CLK; 429c68996e2SPoul-Henning Kamp if (modes & MOD_CLKA) 430c68996e2SPoul-Henning Kamp time_status &= ~STA_CLK; 43124dbea46SJohn Hay if (modes & MOD_FREQUENCY) { 43224dbea46SJohn Hay freq = (ntv.freq * 1000LL) >> 16; 43324dbea46SJohn Hay if (freq > MAXFREQ) 43424dbea46SJohn Hay L_LINT(time_freq, MAXFREQ); 43524dbea46SJohn Hay else if (freq < -MAXFREQ) 43624dbea46SJohn Hay L_LINT(time_freq, -MAXFREQ); 437bcfe6d8bSPoul-Henning Kamp else { 438bcfe6d8bSPoul-Henning Kamp /* 439bcfe6d8bSPoul-Henning Kamp * ntv.freq is [PPM * 2^16] = [us/s * 2^16] 440bcfe6d8bSPoul-Henning Kamp * time_freq is [ns/s * 2^32] 441bcfe6d8bSPoul-Henning Kamp */ 442bcfe6d8bSPoul-Henning Kamp time_freq = ntv.freq * 1000LL * 65536LL; 443bcfe6d8bSPoul-Henning Kamp } 44424dbea46SJohn Hay #ifdef PPS_SYNC 44524dbea46SJohn Hay pps_freq = time_freq; 44624dbea46SJohn Hay #endif /* PPS_SYNC */ 44724dbea46SJohn Hay } 448551260fcSPoul-Henning Kamp if (modes & MOD_OFFSET) { 449551260fcSPoul-Henning Kamp if (time_status & STA_NANO) 450551260fcSPoul-Henning Kamp hardupdate(ntv.offset); 451551260fcSPoul-Henning Kamp else 452551260fcSPoul-Henning Kamp hardupdate(ntv.offset * 1000); 453551260fcSPoul-Henning Kamp } 454c68996e2SPoul-Henning Kamp 455c68996e2SPoul-Henning Kamp /* 45697804a5cSPoul-Henning Kamp * Retrieve all clock variables. Note that the TAI offset is 45797804a5cSPoul-Henning Kamp * returned only by ntp_gettime(); 458c68996e2SPoul-Henning Kamp */ 459c68996e2SPoul-Henning Kamp if (time_status & STA_NANO) 460b9c6e8bdSPoul-Henning Kamp ntv.offset = L_GINT(time_offset); 461c68996e2SPoul-Henning Kamp else 462b9c6e8bdSPoul-Henning Kamp ntv.offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */ 46334cffbe3SPoul-Henning Kamp ntv.freq = L_GINT((time_freq / 1000LL) << 16); 464c68996e2SPoul-Henning Kamp ntv.maxerror = time_maxerror; 465c68996e2SPoul-Henning Kamp ntv.esterror = time_esterror; 466c68996e2SPoul-Henning Kamp ntv.status = time_status; 467f425c1f6SPoul-Henning Kamp ntv.constant = time_constant; 468c68996e2SPoul-Henning Kamp if (time_status & STA_NANO) 469c68996e2SPoul-Henning Kamp ntv.precision = time_precision; 470c68996e2SPoul-Henning Kamp else 471c68996e2SPoul-Henning Kamp ntv.precision = time_precision / 1000; 472c68996e2SPoul-Henning Kamp ntv.tolerance = MAXFREQ * SCALE_PPM; 473c68996e2SPoul-Henning Kamp #ifdef PPS_SYNC 474c68996e2SPoul-Henning Kamp ntv.shift = pps_shift; 47534cffbe3SPoul-Henning Kamp ntv.ppsfreq = L_GINT((pps_freq / 1000LL) << 16); 476c68996e2SPoul-Henning Kamp if (time_status & STA_NANO) 477c68996e2SPoul-Henning Kamp ntv.jitter = pps_jitter; 478c68996e2SPoul-Henning Kamp else 479c68996e2SPoul-Henning Kamp ntv.jitter = pps_jitter / 1000; 480c68996e2SPoul-Henning Kamp ntv.stabil = pps_stabil; 481c68996e2SPoul-Henning Kamp ntv.calcnt = pps_calcnt; 482c68996e2SPoul-Henning Kamp ntv.errcnt = pps_errcnt; 483c68996e2SPoul-Henning Kamp ntv.jitcnt = pps_jitcnt; 484c68996e2SPoul-Henning Kamp ntv.stbcnt = pps_stbcnt; 485c68996e2SPoul-Henning Kamp #endif /* PPS_SYNC */ 486*364c516cSKonstantin Belousov retval = ntp_is_time_error(time_status) ? TIME_ERROR : time_state; 487*364c516cSKonstantin Belousov NTPADJ_UNLOCK(); 488c68996e2SPoul-Henning Kamp 489c68996e2SPoul-Henning Kamp error = copyout((caddr_t)&ntv, (caddr_t)uap->tp, sizeof(ntv)); 490*364c516cSKonstantin Belousov if (error == 0) 491*364c516cSKonstantin Belousov td->td_retval[0] = retval; 492a5088017SPoul-Henning Kamp return (error); 493c68996e2SPoul-Henning Kamp } 494c68996e2SPoul-Henning Kamp 495c68996e2SPoul-Henning Kamp /* 496c68996e2SPoul-Henning Kamp * second_overflow() - called after ntp_tick_adjust() 497c68996e2SPoul-Henning Kamp * 498c68996e2SPoul-Henning Kamp * This routine is ordinarily called immediately following the above 499c68996e2SPoul-Henning Kamp * routine ntp_tick_adjust(). While these two routines are normally 500c68996e2SPoul-Henning Kamp * combined, they are separated here only for the purposes of 501c68996e2SPoul-Henning Kamp * simulation. 502c68996e2SPoul-Henning Kamp */ 503c68996e2SPoul-Henning Kamp void 504b4a1d0deSPoul-Henning Kamp ntp_update_second(int64_t *adjustment, time_t *newsec) 505c68996e2SPoul-Henning Kamp { 506e1d970f1SPoul-Henning Kamp int tickrate; 50797804a5cSPoul-Henning Kamp l_fp ftemp; /* 32/64-bit temporary */ 508c68996e2SPoul-Henning Kamp 50982e84c5bSPoul-Henning Kamp /* 51082e84c5bSPoul-Henning Kamp * On rollover of the second both the nanosecond and microsecond 51182e84c5bSPoul-Henning Kamp * clocks are updated and the state machine cranked as 51282e84c5bSPoul-Henning Kamp * necessary. The phase adjustment to be used for the next 51382e84c5bSPoul-Henning Kamp * second is calculated and the maximum error is increased by 51482e84c5bSPoul-Henning Kamp * the tolerance. 51582e84c5bSPoul-Henning Kamp */ 516c68996e2SPoul-Henning Kamp time_maxerror += MAXFREQ / 1000; 517c68996e2SPoul-Henning Kamp 518c68996e2SPoul-Henning Kamp /* 519c68996e2SPoul-Henning Kamp * Leap second processing. If in leap-insert state at 520c68996e2SPoul-Henning Kamp * the end of the day, the system clock is set back one 521c68996e2SPoul-Henning Kamp * second; if in leap-delete state, the system clock is 522c68996e2SPoul-Henning Kamp * set ahead one second. The nano_time() routine or 523c68996e2SPoul-Henning Kamp * external clock driver will insure that reported time 524c68996e2SPoul-Henning Kamp * is always monotonic. 525c68996e2SPoul-Henning Kamp */ 526c68996e2SPoul-Henning Kamp switch (time_state) { 527c68996e2SPoul-Henning Kamp 528c68996e2SPoul-Henning Kamp /* 529c68996e2SPoul-Henning Kamp * No warning. 530c68996e2SPoul-Henning Kamp */ 531c68996e2SPoul-Henning Kamp case TIME_OK: 532c68996e2SPoul-Henning Kamp if (time_status & STA_INS) 533c68996e2SPoul-Henning Kamp time_state = TIME_INS; 534c68996e2SPoul-Henning Kamp else if (time_status & STA_DEL) 535c68996e2SPoul-Henning Kamp time_state = TIME_DEL; 536c68996e2SPoul-Henning Kamp break; 537c68996e2SPoul-Henning Kamp 538c68996e2SPoul-Henning Kamp /* 539c68996e2SPoul-Henning Kamp * Insert second 23:59:60 following second 540c68996e2SPoul-Henning Kamp * 23:59:59. 541c68996e2SPoul-Henning Kamp */ 542c68996e2SPoul-Henning Kamp case TIME_INS: 543c68996e2SPoul-Henning Kamp if (!(time_status & STA_INS)) 544c68996e2SPoul-Henning Kamp time_state = TIME_OK; 545c68996e2SPoul-Henning Kamp else if ((*newsec) % 86400 == 0) { 546c68996e2SPoul-Henning Kamp (*newsec)--; 547c68996e2SPoul-Henning Kamp time_state = TIME_OOP; 548eac3c62bSWarner Losh time_tai++; 549c68996e2SPoul-Henning Kamp } 550c68996e2SPoul-Henning Kamp break; 551c68996e2SPoul-Henning Kamp 552c68996e2SPoul-Henning Kamp /* 553c68996e2SPoul-Henning Kamp * Delete second 23:59:59. 554c68996e2SPoul-Henning Kamp */ 555c68996e2SPoul-Henning Kamp case TIME_DEL: 556c68996e2SPoul-Henning Kamp if (!(time_status & STA_DEL)) 557c68996e2SPoul-Henning Kamp time_state = TIME_OK; 558c68996e2SPoul-Henning Kamp else if (((*newsec) + 1) % 86400 == 0) { 559c68996e2SPoul-Henning Kamp (*newsec)++; 56097804a5cSPoul-Henning Kamp time_tai--; 561c68996e2SPoul-Henning Kamp time_state = TIME_WAIT; 562c68996e2SPoul-Henning Kamp } 563c68996e2SPoul-Henning Kamp break; 564c68996e2SPoul-Henning Kamp 565c68996e2SPoul-Henning Kamp /* 566c68996e2SPoul-Henning Kamp * Insert second in progress. 567c68996e2SPoul-Henning Kamp */ 568c68996e2SPoul-Henning Kamp case TIME_OOP: 569c68996e2SPoul-Henning Kamp time_state = TIME_WAIT; 570c68996e2SPoul-Henning Kamp break; 571c68996e2SPoul-Henning Kamp 572c68996e2SPoul-Henning Kamp /* 573c68996e2SPoul-Henning Kamp * Wait for status bits to clear. 574c68996e2SPoul-Henning Kamp */ 575c68996e2SPoul-Henning Kamp case TIME_WAIT: 576c68996e2SPoul-Henning Kamp if (!(time_status & (STA_INS | STA_DEL))) 577c68996e2SPoul-Henning Kamp time_state = TIME_OK; 578c68996e2SPoul-Henning Kamp } 579c68996e2SPoul-Henning Kamp 580c68996e2SPoul-Henning Kamp /* 58182e84c5bSPoul-Henning Kamp * Compute the total time adjustment for the next second 58282e84c5bSPoul-Henning Kamp * in ns. The offset is reduced by a factor depending on 58382e84c5bSPoul-Henning Kamp * whether the PPS signal is operating. Note that the 58482e84c5bSPoul-Henning Kamp * value is in effect scaled by the clock frequency, 58582e84c5bSPoul-Henning Kamp * since the adjustment is added at each tick interrupt. 586c68996e2SPoul-Henning Kamp */ 58797804a5cSPoul-Henning Kamp ftemp = time_offset; 588c68996e2SPoul-Henning Kamp #ifdef PPS_SYNC 58997804a5cSPoul-Henning Kamp /* XXX even if PPS signal dies we should finish adjustment ? */ 59097804a5cSPoul-Henning Kamp if (time_status & STA_PPSTIME && time_status & 59197804a5cSPoul-Henning Kamp STA_PPSSIGNAL) 59297804a5cSPoul-Henning Kamp L_RSHIFT(ftemp, pps_shift); 59397804a5cSPoul-Henning Kamp else 59497804a5cSPoul-Henning Kamp L_RSHIFT(ftemp, SHIFT_PLL + time_constant); 59582e84c5bSPoul-Henning Kamp #else 59697804a5cSPoul-Henning Kamp L_RSHIFT(ftemp, SHIFT_PLL + time_constant); 59782e84c5bSPoul-Henning Kamp #endif /* PPS_SYNC */ 59897804a5cSPoul-Henning Kamp time_adj = ftemp; 59997804a5cSPoul-Henning Kamp L_SUB(time_offset, ftemp); 600c68996e2SPoul-Henning Kamp L_ADD(time_adj, time_freq); 601e1d970f1SPoul-Henning Kamp 602e1d970f1SPoul-Henning Kamp /* 603e1d970f1SPoul-Henning Kamp * Apply any correction from adjtime(2). If more than one second 604e1d970f1SPoul-Henning Kamp * off we slew at a rate of 5ms/s (5000 PPM) else 500us/s (500PPM) 605e1d970f1SPoul-Henning Kamp * until the last second is slewed the final < 500 usecs. 606e1d970f1SPoul-Henning Kamp */ 607e1d970f1SPoul-Henning Kamp if (time_adjtime != 0) { 608e1d970f1SPoul-Henning Kamp if (time_adjtime > 1000000) 609e1d970f1SPoul-Henning Kamp tickrate = 5000; 610e1d970f1SPoul-Henning Kamp else if (time_adjtime < -1000000) 611e1d970f1SPoul-Henning Kamp tickrate = -5000; 612e1d970f1SPoul-Henning Kamp else if (time_adjtime > 500) 613e1d970f1SPoul-Henning Kamp tickrate = 500; 614e1d970f1SPoul-Henning Kamp else if (time_adjtime < -500) 615e1d970f1SPoul-Henning Kamp tickrate = -500; 616e1d970f1SPoul-Henning Kamp else 617bcfe6d8bSPoul-Henning Kamp tickrate = time_adjtime; 618e1d970f1SPoul-Henning Kamp time_adjtime -= tickrate; 619e1d970f1SPoul-Henning Kamp L_LINT(ftemp, tickrate * 1000); 620e1d970f1SPoul-Henning Kamp L_ADD(time_adj, ftemp); 621e1d970f1SPoul-Henning Kamp } 622b4a1d0deSPoul-Henning Kamp *adjustment = time_adj; 623e1d970f1SPoul-Henning Kamp 624c68996e2SPoul-Henning Kamp #ifdef PPS_SYNC 625c68996e2SPoul-Henning Kamp if (pps_valid > 0) 626c68996e2SPoul-Henning Kamp pps_valid--; 627c68996e2SPoul-Henning Kamp else 62824dbea46SJohn Hay time_status &= ~STA_PPSSIGNAL; 629c68996e2SPoul-Henning Kamp #endif /* PPS_SYNC */ 630c68996e2SPoul-Henning Kamp } 631c68996e2SPoul-Henning Kamp 632c68996e2SPoul-Henning Kamp /* 633c68996e2SPoul-Henning Kamp * ntp_init() - initialize variables and structures 634c68996e2SPoul-Henning Kamp * 635c68996e2SPoul-Henning Kamp * This routine must be called after the kernel variables hz and tick 636c68996e2SPoul-Henning Kamp * are set or changed and before the next tick interrupt. In this 637c68996e2SPoul-Henning Kamp * particular implementation, these values are assumed set elsewhere in 638c68996e2SPoul-Henning Kamp * the kernel. The design allows the clock frequency and tick interval 639c68996e2SPoul-Henning Kamp * to be changed while the system is running. So, this routine should 640c68996e2SPoul-Henning Kamp * probably be integrated with the code that does that. 641c68996e2SPoul-Henning Kamp */ 642c68996e2SPoul-Henning Kamp static void 643*364c516cSKonstantin Belousov ntp_init(void) 644c68996e2SPoul-Henning Kamp { 645c68996e2SPoul-Henning Kamp 646c68996e2SPoul-Henning Kamp /* 647c68996e2SPoul-Henning Kamp * The following variables are initialized only at startup. Only 648c68996e2SPoul-Henning Kamp * those structures not cleared by the compiler need to be 649c68996e2SPoul-Henning Kamp * initialized, and these only in the simulator. In the actual 650c68996e2SPoul-Henning Kamp * kernel, any nonzero values here will quickly evaporate. 651c68996e2SPoul-Henning Kamp */ 652c68996e2SPoul-Henning Kamp L_CLR(time_offset); 653c68996e2SPoul-Henning Kamp L_CLR(time_freq); 654c68996e2SPoul-Henning Kamp #ifdef PPS_SYNC 65582e84c5bSPoul-Henning Kamp pps_tf[0].tv_sec = pps_tf[0].tv_nsec = 0; 65682e84c5bSPoul-Henning Kamp pps_tf[1].tv_sec = pps_tf[1].tv_nsec = 0; 65782e84c5bSPoul-Henning Kamp pps_tf[2].tv_sec = pps_tf[2].tv_nsec = 0; 658f425c1f6SPoul-Henning Kamp pps_fcount = 0; 659c68996e2SPoul-Henning Kamp L_CLR(pps_freq); 660c68996e2SPoul-Henning Kamp #endif /* PPS_SYNC */ 661c68996e2SPoul-Henning Kamp } 662c68996e2SPoul-Henning Kamp 663237fdd78SRobert Watson SYSINIT(ntpclocks, SI_SUB_CLOCKS, SI_ORDER_MIDDLE, ntp_init, NULL); 6646f70df15SPoul-Henning Kamp 6656f70df15SPoul-Henning Kamp /* 6666f70df15SPoul-Henning Kamp * hardupdate() - local clock update 6676f70df15SPoul-Henning Kamp * 6686f70df15SPoul-Henning Kamp * This routine is called by ntp_adjtime() to update the local clock 6696f70df15SPoul-Henning Kamp * phase and frequency. The implementation is of an adaptive-parameter, 6706f70df15SPoul-Henning Kamp * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new 6716f70df15SPoul-Henning Kamp * time and frequency offset estimates for each call. If the kernel PPS 6726f70df15SPoul-Henning Kamp * discipline code is configured (PPS_SYNC), the PPS signal itself 6736f70df15SPoul-Henning Kamp * determines the new time offset, instead of the calling argument. 6746f70df15SPoul-Henning Kamp * Presumably, calls to ntp_adjtime() occur only when the caller 6756f70df15SPoul-Henning Kamp * believes the local clock is valid within some bound (+-128 ms with 6766f70df15SPoul-Henning Kamp * NTP). If the caller's time is far different than the PPS time, an 6776f70df15SPoul-Henning Kamp * argument will ensue, and it's not clear who will lose. 6786f70df15SPoul-Henning Kamp * 679c68996e2SPoul-Henning Kamp * For uncompensated quartz crystal oscillators and nominal update 680c68996e2SPoul-Henning Kamp * intervals less than 256 s, operation should be in phase-lock mode, 681c68996e2SPoul-Henning Kamp * where the loop is disciplined to phase. For update intervals greater 682c68996e2SPoul-Henning Kamp * than 1024 s, operation should be in frequency-lock mode, where the 683c68996e2SPoul-Henning Kamp * loop is disciplined to frequency. Between 256 s and 1024 s, the mode 684c68996e2SPoul-Henning Kamp * is selected by the STA_MODE status bit. 6856f70df15SPoul-Henning Kamp */ 6866f70df15SPoul-Henning Kamp static void 687c68996e2SPoul-Henning Kamp hardupdate(offset) 688c68996e2SPoul-Henning Kamp long offset; /* clock offset (ns) */ 6896f70df15SPoul-Henning Kamp { 69097804a5cSPoul-Henning Kamp long mtemp; 691c68996e2SPoul-Henning Kamp l_fp ftemp; 6926f70df15SPoul-Henning Kamp 693*364c516cSKonstantin Belousov NTPADJ_ASSERT_LOCKED(); 694*364c516cSKonstantin Belousov 695c68996e2SPoul-Henning Kamp /* 696c68996e2SPoul-Henning Kamp * Select how the phase is to be controlled and from which 697c68996e2SPoul-Henning Kamp * source. If the PPS signal is present and enabled to 698c68996e2SPoul-Henning Kamp * discipline the time, the PPS offset is used; otherwise, the 699c68996e2SPoul-Henning Kamp * argument offset is used. 700c68996e2SPoul-Henning Kamp */ 70182e84c5bSPoul-Henning Kamp if (!(time_status & STA_PLL)) 70282e84c5bSPoul-Henning Kamp return; 70397804a5cSPoul-Henning Kamp if (!(time_status & STA_PPSTIME && time_status & 70497804a5cSPoul-Henning Kamp STA_PPSSIGNAL)) { 70597804a5cSPoul-Henning Kamp if (offset > MAXPHASE) 70697804a5cSPoul-Henning Kamp time_monitor = MAXPHASE; 70797804a5cSPoul-Henning Kamp else if (offset < -MAXPHASE) 70897804a5cSPoul-Henning Kamp time_monitor = -MAXPHASE; 70997804a5cSPoul-Henning Kamp else 71097804a5cSPoul-Henning Kamp time_monitor = offset; 71197804a5cSPoul-Henning Kamp L_LINT(time_offset, time_monitor); 71297804a5cSPoul-Henning Kamp } 7136f70df15SPoul-Henning Kamp 7146f70df15SPoul-Henning Kamp /* 715c68996e2SPoul-Henning Kamp * Select how the frequency is to be controlled and in which 716c68996e2SPoul-Henning Kamp * mode (PLL or FLL). If the PPS signal is present and enabled 717c68996e2SPoul-Henning Kamp * to discipline the frequency, the PPS frequency is used; 718c68996e2SPoul-Henning Kamp * otherwise, the argument offset is used to compute it. 7196f70df15SPoul-Henning Kamp */ 720c68996e2SPoul-Henning Kamp if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) { 721969fc29eSIan Lepore time_reftime = time_uptime; 722c68996e2SPoul-Henning Kamp return; 723c68996e2SPoul-Henning Kamp } 7246f70df15SPoul-Henning Kamp if (time_status & STA_FREQHOLD || time_reftime == 0) 725969fc29eSIan Lepore time_reftime = time_uptime; 726969fc29eSIan Lepore mtemp = time_uptime - time_reftime; 72797804a5cSPoul-Henning Kamp L_LINT(ftemp, time_monitor); 728c68996e2SPoul-Henning Kamp L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1); 729c68996e2SPoul-Henning Kamp L_MPY(ftemp, mtemp); 730c68996e2SPoul-Henning Kamp L_ADD(time_freq, ftemp); 731c68996e2SPoul-Henning Kamp time_status &= ~STA_MODE; 73297804a5cSPoul-Henning Kamp if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > 73397804a5cSPoul-Henning Kamp MAXSEC)) { 73497804a5cSPoul-Henning Kamp L_LINT(ftemp, (time_monitor << 4) / mtemp); 73582e84c5bSPoul-Henning Kamp L_RSHIFT(ftemp, SHIFT_FLL + 4); 73682e84c5bSPoul-Henning Kamp L_ADD(time_freq, ftemp); 73782e84c5bSPoul-Henning Kamp time_status |= STA_MODE; 738c68996e2SPoul-Henning Kamp } 739969fc29eSIan Lepore time_reftime = time_uptime; 740c68996e2SPoul-Henning Kamp if (L_GINT(time_freq) > MAXFREQ) 741c68996e2SPoul-Henning Kamp L_LINT(time_freq, MAXFREQ); 742c68996e2SPoul-Henning Kamp else if (L_GINT(time_freq) < -MAXFREQ) 743c68996e2SPoul-Henning Kamp L_LINT(time_freq, -MAXFREQ); 7443f31c649SGarrett Wollman } 7453f31c649SGarrett Wollman 7466f70df15SPoul-Henning Kamp #ifdef PPS_SYNC 7476f70df15SPoul-Henning Kamp /* 7486f70df15SPoul-Henning Kamp * hardpps() - discipline CPU clock oscillator to external PPS signal 7496f70df15SPoul-Henning Kamp * 7506f70df15SPoul-Henning Kamp * This routine is called at each PPS interrupt in order to discipline 75197804a5cSPoul-Henning Kamp * the CPU clock oscillator to the PPS signal. There are two independent 75297804a5cSPoul-Henning Kamp * first-order feedback loops, one for the phase, the other for the 75397804a5cSPoul-Henning Kamp * frequency. The phase loop measures and grooms the PPS phase offset 75497804a5cSPoul-Henning Kamp * and leaves it in a handy spot for the seconds overflow routine. The 75597804a5cSPoul-Henning Kamp * frequency loop averages successive PPS phase differences and 75697804a5cSPoul-Henning Kamp * calculates the PPS frequency offset, which is also processed by the 75797804a5cSPoul-Henning Kamp * seconds overflow routine. The code requires the caller to capture the 75897804a5cSPoul-Henning Kamp * time and architecture-dependent hardware counter values in 75997804a5cSPoul-Henning Kamp * nanoseconds at the on-time PPS signal transition. 7606f70df15SPoul-Henning Kamp * 761c68996e2SPoul-Henning Kamp * Note that, on some Unix systems this routine runs at an interrupt 7626f70df15SPoul-Henning Kamp * priority level higher than the timer interrupt routine hardclock(). 7636f70df15SPoul-Henning Kamp * Therefore, the variables used are distinct from the hardclock() 764c68996e2SPoul-Henning Kamp * variables, except for the actual time and frequency variables, which 765c68996e2SPoul-Henning Kamp * are determined by this routine and updated atomically. 7666f70df15SPoul-Henning Kamp */ 7676f70df15SPoul-Henning Kamp void 768c68996e2SPoul-Henning Kamp hardpps(tsp, nsec) 769c68996e2SPoul-Henning Kamp struct timespec *tsp; /* time at PPS */ 770c68996e2SPoul-Henning Kamp long nsec; /* hardware counter at PPS */ 7716f70df15SPoul-Henning Kamp { 77297804a5cSPoul-Henning Kamp long u_sec, u_nsec, v_nsec; /* temps */ 773c68996e2SPoul-Henning Kamp l_fp ftemp; 7746f70df15SPoul-Henning Kamp 775*364c516cSKonstantin Belousov NTPADJ_LOCK(); 776*364c516cSKonstantin Belousov 7776f70df15SPoul-Henning Kamp /* 77897804a5cSPoul-Henning Kamp * The signal is first processed by a range gate and frequency 77997804a5cSPoul-Henning Kamp * discriminator. The range gate rejects noise spikes outside 78097804a5cSPoul-Henning Kamp * the range +-500 us. The frequency discriminator rejects input 78197804a5cSPoul-Henning Kamp * signals with apparent frequency outside the range 1 +-500 78297804a5cSPoul-Henning Kamp * PPM. If two hits occur in the same second, we ignore the 78397804a5cSPoul-Henning Kamp * later hit; if not and a hit occurs outside the range gate, 78497804a5cSPoul-Henning Kamp * keep the later hit for later comparison, but do not process 78597804a5cSPoul-Henning Kamp * it. 7866f70df15SPoul-Henning Kamp */ 787c68996e2SPoul-Henning Kamp time_status |= STA_PPSSIGNAL | STA_PPSJITTER; 788c68996e2SPoul-Henning Kamp time_status &= ~(STA_PPSWANDER | STA_PPSERROR); 789c68996e2SPoul-Henning Kamp pps_valid = PPS_VALID; 790c68996e2SPoul-Henning Kamp u_sec = tsp->tv_sec; 791c68996e2SPoul-Henning Kamp u_nsec = tsp->tv_nsec; 792c68996e2SPoul-Henning Kamp if (u_nsec >= (NANOSECOND >> 1)) { 793c68996e2SPoul-Henning Kamp u_nsec -= NANOSECOND; 794c68996e2SPoul-Henning Kamp u_sec++; 7956f70df15SPoul-Henning Kamp } 79682e84c5bSPoul-Henning Kamp v_nsec = u_nsec - pps_tf[0].tv_nsec; 797*364c516cSKonstantin Belousov if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND - MAXFREQ) 798*364c516cSKonstantin Belousov goto out; 799c68996e2SPoul-Henning Kamp pps_tf[2] = pps_tf[1]; 800c68996e2SPoul-Henning Kamp pps_tf[1] = pps_tf[0]; 80182e84c5bSPoul-Henning Kamp pps_tf[0].tv_sec = u_sec; 80282e84c5bSPoul-Henning Kamp pps_tf[0].tv_nsec = u_nsec; 8036f70df15SPoul-Henning Kamp 8046f70df15SPoul-Henning Kamp /* 805c68996e2SPoul-Henning Kamp * Compute the difference between the current and previous 806c68996e2SPoul-Henning Kamp * counter values. If the difference exceeds 0.5 s, assume it 807c68996e2SPoul-Henning Kamp * has wrapped around, so correct 1.0 s. If the result exceeds 808c68996e2SPoul-Henning Kamp * the tick interval, the sample point has crossed a tick 809c68996e2SPoul-Henning Kamp * boundary during the last second, so correct the tick. Very 810c68996e2SPoul-Henning Kamp * intricate. 811c68996e2SPoul-Henning Kamp */ 81232c20357SPoul-Henning Kamp u_nsec = nsec; 813c68996e2SPoul-Henning Kamp if (u_nsec > (NANOSECOND >> 1)) 814c68996e2SPoul-Henning Kamp u_nsec -= NANOSECOND; 815c68996e2SPoul-Henning Kamp else if (u_nsec < -(NANOSECOND >> 1)) 816c68996e2SPoul-Henning Kamp u_nsec += NANOSECOND; 817884ab557SPoul-Henning Kamp pps_fcount += u_nsec; 81824dbea46SJohn Hay if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ) 819*364c516cSKonstantin Belousov goto out; 820c68996e2SPoul-Henning Kamp time_status &= ~STA_PPSJITTER; 821c68996e2SPoul-Henning Kamp 822c68996e2SPoul-Henning Kamp /* 823c68996e2SPoul-Henning Kamp * A three-stage median filter is used to help denoise the PPS 8246f70df15SPoul-Henning Kamp * time. The median sample becomes the time offset estimate; the 8256f70df15SPoul-Henning Kamp * difference between the other two samples becomes the time 8266f70df15SPoul-Henning Kamp * dispersion (jitter) estimate. 8276f70df15SPoul-Henning Kamp */ 82882e84c5bSPoul-Henning Kamp if (pps_tf[0].tv_nsec > pps_tf[1].tv_nsec) { 82982e84c5bSPoul-Henning Kamp if (pps_tf[1].tv_nsec > pps_tf[2].tv_nsec) { 83082e84c5bSPoul-Henning Kamp v_nsec = pps_tf[1].tv_nsec; /* 0 1 2 */ 83182e84c5bSPoul-Henning Kamp u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec; 83282e84c5bSPoul-Henning Kamp } else if (pps_tf[2].tv_nsec > pps_tf[0].tv_nsec) { 83382e84c5bSPoul-Henning Kamp v_nsec = pps_tf[0].tv_nsec; /* 2 0 1 */ 83482e84c5bSPoul-Henning Kamp u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec; 8356f70df15SPoul-Henning Kamp } else { 83682e84c5bSPoul-Henning Kamp v_nsec = pps_tf[2].tv_nsec; /* 0 2 1 */ 83782e84c5bSPoul-Henning Kamp u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec; 838c68996e2SPoul-Henning Kamp } 839c68996e2SPoul-Henning Kamp } else { 84082e84c5bSPoul-Henning Kamp if (pps_tf[1].tv_nsec < pps_tf[2].tv_nsec) { 84182e84c5bSPoul-Henning Kamp v_nsec = pps_tf[1].tv_nsec; /* 2 1 0 */ 84282e84c5bSPoul-Henning Kamp u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec; 84382e84c5bSPoul-Henning Kamp } else if (pps_tf[2].tv_nsec < pps_tf[0].tv_nsec) { 84482e84c5bSPoul-Henning Kamp v_nsec = pps_tf[0].tv_nsec; /* 1 0 2 */ 84582e84c5bSPoul-Henning Kamp u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec; 846c68996e2SPoul-Henning Kamp } else { 84782e84c5bSPoul-Henning Kamp v_nsec = pps_tf[2].tv_nsec; /* 1 2 0 */ 84882e84c5bSPoul-Henning Kamp u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec; 8496f70df15SPoul-Henning Kamp } 8506f70df15SPoul-Henning Kamp } 8516f70df15SPoul-Henning Kamp 8526f70df15SPoul-Henning Kamp /* 853c68996e2SPoul-Henning Kamp * Nominal jitter is due to PPS signal noise and interrupt 85497804a5cSPoul-Henning Kamp * latency. If it exceeds the popcorn threshold, the sample is 85597804a5cSPoul-Henning Kamp * discarded. otherwise, if so enabled, the time offset is 85697804a5cSPoul-Henning Kamp * updated. We can tolerate a modest loss of data here without 85797804a5cSPoul-Henning Kamp * much degrading time accuracy. 85879f1fdb8SWarner Losh * 85979f1fdb8SWarner Losh * The measurements being checked here were made with the system 86079f1fdb8SWarner Losh * timecounter, so the popcorn threshold is not allowed to fall below 86179f1fdb8SWarner Losh * the number of nanoseconds in two ticks of the timecounter. For a 86279f1fdb8SWarner Losh * timecounter running faster than 1 GHz the lower bound is 2ns, just 86379f1fdb8SWarner Losh * to avoid a nonsensical threshold of zero. 8646f70df15SPoul-Henning Kamp */ 86579f1fdb8SWarner Losh if (u_nsec > lmax(pps_jitter << PPS_POPCORN, 86679f1fdb8SWarner Losh 2 * (NANOSECOND / (long)qmin(NANOSECOND, tc_getfrequency())))) { 867c68996e2SPoul-Henning Kamp time_status |= STA_PPSJITTER; 868c68996e2SPoul-Henning Kamp pps_jitcnt++; 869c68996e2SPoul-Henning Kamp } else if (time_status & STA_PPSTIME) { 87097804a5cSPoul-Henning Kamp time_monitor = -v_nsec; 87197804a5cSPoul-Henning Kamp L_LINT(time_offset, time_monitor); 872c68996e2SPoul-Henning Kamp } 873c68996e2SPoul-Henning Kamp pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG; 87482e84c5bSPoul-Henning Kamp u_sec = pps_tf[0].tv_sec - pps_lastsec; 875c68996e2SPoul-Henning Kamp if (u_sec < (1 << pps_shift)) 876*364c516cSKonstantin Belousov goto out; 877c68996e2SPoul-Henning Kamp 878c68996e2SPoul-Henning Kamp /* 879c68996e2SPoul-Henning Kamp * At the end of the calibration interval the difference between 880c68996e2SPoul-Henning Kamp * the first and last counter values becomes the scaled 881c68996e2SPoul-Henning Kamp * frequency. It will later be divided by the length of the 882c68996e2SPoul-Henning Kamp * interval to determine the frequency update. If the frequency 883c68996e2SPoul-Henning Kamp * exceeds a sanity threshold, or if the actual calibration 884c68996e2SPoul-Henning Kamp * interval is not equal to the expected length, the data are 885c68996e2SPoul-Henning Kamp * discarded. We can tolerate a modest loss of data here without 88697804a5cSPoul-Henning Kamp * much degrading frequency accuracy. 887c68996e2SPoul-Henning Kamp */ 888c68996e2SPoul-Henning Kamp pps_calcnt++; 889884ab557SPoul-Henning Kamp v_nsec = -pps_fcount; 89082e84c5bSPoul-Henning Kamp pps_lastsec = pps_tf[0].tv_sec; 891884ab557SPoul-Henning Kamp pps_fcount = 0; 892c68996e2SPoul-Henning Kamp u_nsec = MAXFREQ << pps_shift; 893*364c516cSKonstantin Belousov if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 << pps_shift)) { 894c68996e2SPoul-Henning Kamp time_status |= STA_PPSERROR; 895c68996e2SPoul-Henning Kamp pps_errcnt++; 896*364c516cSKonstantin Belousov goto out; 897c68996e2SPoul-Henning Kamp } 898c68996e2SPoul-Henning Kamp 899c68996e2SPoul-Henning Kamp /* 90082e84c5bSPoul-Henning Kamp * Here the raw frequency offset and wander (stability) is 90182e84c5bSPoul-Henning Kamp * calculated. If the wander is less than the wander threshold 90282e84c5bSPoul-Henning Kamp * for four consecutive averaging intervals, the interval is 90382e84c5bSPoul-Henning Kamp * doubled; if it is greater than the threshold for four 90482e84c5bSPoul-Henning Kamp * consecutive intervals, the interval is halved. The scaled 90582e84c5bSPoul-Henning Kamp * frequency offset is converted to frequency offset. The 90682e84c5bSPoul-Henning Kamp * stability metric is calculated as the average of recent 90782e84c5bSPoul-Henning Kamp * frequency changes, but is used only for performance 908c68996e2SPoul-Henning Kamp * monitoring. 909c68996e2SPoul-Henning Kamp */ 910c68996e2SPoul-Henning Kamp L_LINT(ftemp, v_nsec); 911c68996e2SPoul-Henning Kamp L_RSHIFT(ftemp, pps_shift); 912c68996e2SPoul-Henning Kamp L_SUB(ftemp, pps_freq); 913c68996e2SPoul-Henning Kamp u_nsec = L_GINT(ftemp); 91482e84c5bSPoul-Henning Kamp if (u_nsec > PPS_MAXWANDER) { 91582e84c5bSPoul-Henning Kamp L_LINT(ftemp, PPS_MAXWANDER); 916c68996e2SPoul-Henning Kamp pps_intcnt--; 917c68996e2SPoul-Henning Kamp time_status |= STA_PPSWANDER; 918c68996e2SPoul-Henning Kamp pps_stbcnt++; 91982e84c5bSPoul-Henning Kamp } else if (u_nsec < -PPS_MAXWANDER) { 92082e84c5bSPoul-Henning Kamp L_LINT(ftemp, -PPS_MAXWANDER); 921c68996e2SPoul-Henning Kamp pps_intcnt--; 922c68996e2SPoul-Henning Kamp time_status |= STA_PPSWANDER; 923c68996e2SPoul-Henning Kamp pps_stbcnt++; 924c68996e2SPoul-Henning Kamp } else { 9256f70df15SPoul-Henning Kamp pps_intcnt++; 9266f70df15SPoul-Henning Kamp } 92797804a5cSPoul-Henning Kamp if (pps_intcnt >= 4) { 928c68996e2SPoul-Henning Kamp pps_intcnt = 4; 92982e84c5bSPoul-Henning Kamp if (pps_shift < pps_shiftmax) { 930c68996e2SPoul-Henning Kamp pps_shift++; 931c68996e2SPoul-Henning Kamp pps_intcnt = 0; 932c68996e2SPoul-Henning Kamp } 93397804a5cSPoul-Henning Kamp } else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) { 934c68996e2SPoul-Henning Kamp pps_intcnt = -4; 935c68996e2SPoul-Henning Kamp if (pps_shift > PPS_FAVG) { 936c68996e2SPoul-Henning Kamp pps_shift--; 937c68996e2SPoul-Henning Kamp pps_intcnt = 0; 938c68996e2SPoul-Henning Kamp } 939c68996e2SPoul-Henning Kamp } 940c68996e2SPoul-Henning Kamp if (u_nsec < 0) 941c68996e2SPoul-Henning Kamp u_nsec = -u_nsec; 942c68996e2SPoul-Henning Kamp pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG; 9439ada5a50SPoul-Henning Kamp 944c68996e2SPoul-Henning Kamp /* 94582e84c5bSPoul-Henning Kamp * The PPS frequency is recalculated and clamped to the maximum 94682e84c5bSPoul-Henning Kamp * MAXFREQ. If enabled, the system clock frequency is updated as 94782e84c5bSPoul-Henning Kamp * well. 948c68996e2SPoul-Henning Kamp */ 949c68996e2SPoul-Henning Kamp L_ADD(pps_freq, ftemp); 950c68996e2SPoul-Henning Kamp u_nsec = L_GINT(pps_freq); 951c68996e2SPoul-Henning Kamp if (u_nsec > MAXFREQ) 952c68996e2SPoul-Henning Kamp L_LINT(pps_freq, MAXFREQ); 953c68996e2SPoul-Henning Kamp else if (u_nsec < -MAXFREQ) 954c68996e2SPoul-Henning Kamp L_LINT(pps_freq, -MAXFREQ); 95597804a5cSPoul-Henning Kamp if (time_status & STA_PPSFREQ) 956c68996e2SPoul-Henning Kamp time_freq = pps_freq; 957*364c516cSKonstantin Belousov 958*364c516cSKonstantin Belousov out: 959*364c516cSKonstantin Belousov NTPADJ_UNLOCK(); 960c68996e2SPoul-Henning Kamp } 9616f70df15SPoul-Henning Kamp #endif /* PPS_SYNC */ 962e1d970f1SPoul-Henning Kamp 963e1d970f1SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 964e1d970f1SPoul-Henning Kamp struct adjtime_args { 965e1d970f1SPoul-Henning Kamp struct timeval *delta; 966e1d970f1SPoul-Henning Kamp struct timeval *olddelta; 967e1d970f1SPoul-Henning Kamp }; 968e1d970f1SPoul-Henning Kamp #endif 969e1d970f1SPoul-Henning Kamp /* ARGSUSED */ 970e1d970f1SPoul-Henning Kamp int 9718451d0ddSKip Macy sys_adjtime(struct thread *td, struct adjtime_args *uap) 972e1d970f1SPoul-Henning Kamp { 973b88ec951SJohn Baldwin struct timeval delta, olddelta, *deltap; 974b88ec951SJohn Baldwin int error; 975b88ec951SJohn Baldwin 976b88ec951SJohn Baldwin if (uap->delta) { 977b88ec951SJohn Baldwin error = copyin(uap->delta, &delta, sizeof(delta)); 978b88ec951SJohn Baldwin if (error) 979b88ec951SJohn Baldwin return (error); 980b88ec951SJohn Baldwin deltap = δ 981b88ec951SJohn Baldwin } else 982b88ec951SJohn Baldwin deltap = NULL; 983b88ec951SJohn Baldwin error = kern_adjtime(td, deltap, &olddelta); 984b88ec951SJohn Baldwin if (uap->olddelta && error == 0) 985b88ec951SJohn Baldwin error = copyout(&olddelta, uap->olddelta, sizeof(olddelta)); 986b88ec951SJohn Baldwin return (error); 987b88ec951SJohn Baldwin } 988b88ec951SJohn Baldwin 989b88ec951SJohn Baldwin int 990b88ec951SJohn Baldwin kern_adjtime(struct thread *td, struct timeval *delta, struct timeval *olddelta) 991b88ec951SJohn Baldwin { 992e1d970f1SPoul-Henning Kamp struct timeval atv; 993*364c516cSKonstantin Belousov int64_t ltr, ltw; 994e1d970f1SPoul-Henning Kamp int error; 995e1d970f1SPoul-Henning Kamp 996*364c516cSKonstantin Belousov if (delta != NULL) { 997*364c516cSKonstantin Belousov error = priv_check(td, PRIV_ADJTIME); 998*364c516cSKonstantin Belousov if (error != 0) 999*364c516cSKonstantin Belousov return (error); 1000*364c516cSKonstantin Belousov ltw = (int64_t)delta->tv_sec * 1000000 + delta->tv_usec; 1001*364c516cSKonstantin Belousov } 1002*364c516cSKonstantin Belousov NTPADJ_LOCK(); 1003*364c516cSKonstantin Belousov ltr = time_adjtime; 1004*364c516cSKonstantin Belousov if (delta != NULL) 1005*364c516cSKonstantin Belousov time_adjtime = ltw; 1006*364c516cSKonstantin Belousov NTPADJ_UNLOCK(); 1007*364c516cSKonstantin Belousov if (olddelta != NULL) { 1008*364c516cSKonstantin Belousov atv.tv_sec = ltr / 1000000; 1009*364c516cSKonstantin Belousov atv.tv_usec = ltr % 1000000; 1010e1d970f1SPoul-Henning Kamp if (atv.tv_usec < 0) { 1011e1d970f1SPoul-Henning Kamp atv.tv_usec += 1000000; 1012e1d970f1SPoul-Henning Kamp atv.tv_sec--; 1013e1d970f1SPoul-Henning Kamp } 1014b88ec951SJohn Baldwin *olddelta = atv; 1015e1d970f1SPoul-Henning Kamp } 1016b4be6ef2SRobert Watson return (0); 1017b4be6ef2SRobert Watson } 1018e1d970f1SPoul-Henning Kamp 10195c7e270fSAndriy Gapon static struct callout resettodr_callout; 10205c7e270fSAndriy Gapon static int resettodr_period = 1800; 10215c7e270fSAndriy Gapon 10225c7e270fSAndriy Gapon static void 10235c7e270fSAndriy Gapon periodic_resettodr(void *arg __unused) 10245c7e270fSAndriy Gapon { 10255c7e270fSAndriy Gapon 1026*364c516cSKonstantin Belousov /* 1027*364c516cSKonstantin Belousov * Read of time_status is lock-less, which is fine since 1028*364c516cSKonstantin Belousov * ntp_is_time_error() operates on the consistent read value. 1029*364c516cSKonstantin Belousov */ 1030*364c516cSKonstantin Belousov if (!ntp_is_time_error(time_status)) 10315c7e270fSAndriy Gapon resettodr(); 10325c7e270fSAndriy Gapon if (resettodr_period > 0) 10335c7e270fSAndriy Gapon callout_schedule(&resettodr_callout, resettodr_period * hz); 10345c7e270fSAndriy Gapon } 10355c7e270fSAndriy Gapon 10365c7e270fSAndriy Gapon static void 10375c7e270fSAndriy Gapon shutdown_resettodr(void *arg __unused, int howto __unused) 10385c7e270fSAndriy Gapon { 10395c7e270fSAndriy Gapon 10405c7e270fSAndriy Gapon callout_drain(&resettodr_callout); 1041*364c516cSKonstantin Belousov /* Another unlocked read of time_status */ 1042*364c516cSKonstantin Belousov if (resettodr_period > 0 && !ntp_is_time_error(time_status)) 10435c7e270fSAndriy Gapon resettodr(); 10445c7e270fSAndriy Gapon } 10455c7e270fSAndriy Gapon 10465c7e270fSAndriy Gapon static int 10475c7e270fSAndriy Gapon sysctl_resettodr_period(SYSCTL_HANDLER_ARGS) 10485c7e270fSAndriy Gapon { 10495c7e270fSAndriy Gapon int error; 10505c7e270fSAndriy Gapon 10515c7e270fSAndriy Gapon error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 10525c7e270fSAndriy Gapon if (error || !req->newptr) 10535c7e270fSAndriy Gapon return (error); 1054af3b2549SHans Petter Selasky if (cold) 1055af3b2549SHans Petter Selasky goto done; 10565c7e270fSAndriy Gapon if (resettodr_period == 0) 10575c7e270fSAndriy Gapon callout_stop(&resettodr_callout); 10585c7e270fSAndriy Gapon else 10595c7e270fSAndriy Gapon callout_reset(&resettodr_callout, resettodr_period * hz, 10605c7e270fSAndriy Gapon periodic_resettodr, NULL); 1061af3b2549SHans Petter Selasky done: 10625c7e270fSAndriy Gapon return (0); 10635c7e270fSAndriy Gapon } 10645c7e270fSAndriy Gapon 1065*364c516cSKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, rtc_save_period, CTLTYPE_INT | CTLFLAG_RWTUN | 1066*364c516cSKonstantin Belousov CTLFLAG_MPSAFE, &resettodr_period, 1800, sysctl_resettodr_period, "I", 10675c7e270fSAndriy Gapon "Save system time to RTC with this period (in seconds)"); 10685c7e270fSAndriy Gapon 10695c7e270fSAndriy Gapon static void 10705c7e270fSAndriy Gapon start_periodic_resettodr(void *arg __unused) 10715c7e270fSAndriy Gapon { 10725c7e270fSAndriy Gapon 10735c7e270fSAndriy Gapon EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_resettodr, NULL, 10745c7e270fSAndriy Gapon SHUTDOWN_PRI_FIRST); 10755c7e270fSAndriy Gapon callout_init(&resettodr_callout, 1); 10765c7e270fSAndriy Gapon if (resettodr_period == 0) 10775c7e270fSAndriy Gapon return; 10785c7e270fSAndriy Gapon callout_reset(&resettodr_callout, resettodr_period * hz, 10795c7e270fSAndriy Gapon periodic_resettodr, NULL); 10805c7e270fSAndriy Gapon } 10815c7e270fSAndriy Gapon 1082785797c3SAndriy Gapon SYSINIT(periodic_resettodr, SI_SUB_LAST, SI_ORDER_MIDDLE, 10835c7e270fSAndriy Gapon start_periodic_resettodr, NULL); 1084