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>
3432c20357SPoul-Henning Kamp #include "opt_ntp.h"
3532c20357SPoul-Henning Kamp
363f31c649SGarrett Wollman #include <sys/param.h>
373f31c649SGarrett Wollman #include <sys/systm.h>
38d2d3e875SBruce Evans #include <sys/sysproto.h>
395c7e270fSAndriy Gapon #include <sys/eventhandler.h>
403f31c649SGarrett Wollman #include <sys/kernel.h>
41acd3428bSRobert Watson #include <sys/priv.h>
423f31c649SGarrett Wollman #include <sys/proc.h>
436f1e8c18SMatthew Dillon #include <sys/lock.h>
446f1e8c18SMatthew Dillon #include <sys/mutex.h>
45c68996e2SPoul-Henning Kamp #include <sys/time.h>
463f31c649SGarrett Wollman #include <sys/timex.h>
4791266b96SPoul-Henning Kamp #include <sys/timetc.h>
48938ee3ceSPoul-Henning Kamp #include <sys/timepps.h>
49b88ec951SJohn Baldwin #include <sys/syscallsubr.h>
503f31c649SGarrett Wollman #include <sys/sysctl.h>
513f31c649SGarrett Wollman
52de5b1952SAlexander Leidinger #ifdef PPS_SYNC
53de5b1952SAlexander Leidinger FEATURE(pps_sync, "Support usage of external PPS signal by kernel PLL");
54de5b1952SAlexander Leidinger #endif
55de5b1952SAlexander Leidinger
563f31c649SGarrett Wollman /*
57c68996e2SPoul-Henning Kamp * Single-precision macros for 64-bit machines
583f31c649SGarrett Wollman */
59bcfe6d8bSPoul-Henning Kamp typedef int64_t l_fp;
60c68996e2SPoul-Henning Kamp #define L_ADD(v, u) ((v) += (u))
61c68996e2SPoul-Henning Kamp #define L_SUB(v, u) ((v) -= (u))
62bcfe6d8bSPoul-Henning Kamp #define L_ADDHI(v, a) ((v) += (int64_t)(a) << 32)
63c68996e2SPoul-Henning Kamp #define L_NEG(v) ((v) = -(v))
64c68996e2SPoul-Henning Kamp #define L_RSHIFT(v, n) \
65c68996e2SPoul-Henning Kamp do { \
66c68996e2SPoul-Henning Kamp if ((v) < 0) \
67c68996e2SPoul-Henning Kamp (v) = -(-(v) >> (n)); \
68c68996e2SPoul-Henning Kamp else \
69c68996e2SPoul-Henning Kamp (v) = (v) >> (n); \
70c68996e2SPoul-Henning Kamp } while (0)
71c68996e2SPoul-Henning Kamp #define L_MPY(v, a) ((v) *= (a))
72c68996e2SPoul-Henning Kamp #define L_CLR(v) ((v) = 0)
73c68996e2SPoul-Henning Kamp #define L_ISNEG(v) ((v) < 0)
74af9ce4e9SDmitriy Alexandrov #define L_LINT(v, a) \
75af9ce4e9SDmitriy Alexandrov do { \
76af9ce4e9SDmitriy Alexandrov if ((a) < 0) \
77af9ce4e9SDmitriy Alexandrov ((v) = -((int64_t)(-(a)) << 32)); \
78af9ce4e9SDmitriy Alexandrov else \
79af9ce4e9SDmitriy Alexandrov ((v) = (int64_t)(a) << 32); \
80af9ce4e9SDmitriy Alexandrov } while (0)
81c68996e2SPoul-Henning Kamp #define L_GINT(v) ((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
826f70df15SPoul-Henning Kamp
836f70df15SPoul-Henning Kamp /*
84c68996e2SPoul-Henning Kamp * Generic NTP kernel interface
856f70df15SPoul-Henning Kamp *
86c68996e2SPoul-Henning Kamp * These routines constitute the Network Time Protocol (NTP) interfaces
87c68996e2SPoul-Henning Kamp * for user and daemon application programs. The ntp_gettime() routine
88c68996e2SPoul-Henning Kamp * provides the time, maximum error (synch distance) and estimated error
89c68996e2SPoul-Henning Kamp * (dispersion) to client user application programs. The ntp_adjtime()
90c68996e2SPoul-Henning Kamp * routine is used by the NTP daemon to adjust the system clock to an
91c68996e2SPoul-Henning Kamp * externally derived time. The time offset and related variables set by
92c68996e2SPoul-Henning Kamp * this routine are used by other routines in this module to adjust the
93c68996e2SPoul-Henning Kamp * phase and frequency of the clock discipline loop which controls the
94c68996e2SPoul-Henning Kamp * system clock.
956f70df15SPoul-Henning Kamp *
96f425c1f6SPoul-Henning Kamp * When the kernel time is reckoned directly in nanoseconds (NTP_NANO
97c68996e2SPoul-Henning Kamp * defined), the time at each tick interrupt is derived directly from
98c68996e2SPoul-Henning Kamp * the kernel time variable. When the kernel time is reckoned in
99f425c1f6SPoul-Henning Kamp * microseconds, (NTP_NANO undefined), the time is derived from the
100f425c1f6SPoul-Henning Kamp * kernel time variable together with a variable representing the
101f425c1f6SPoul-Henning Kamp * leftover nanoseconds at the last tick interrupt. In either case, the
102f425c1f6SPoul-Henning Kamp * current nanosecond time is reckoned from these values plus an
103f425c1f6SPoul-Henning Kamp * interpolated value derived by the clock routines in another
104f425c1f6SPoul-Henning Kamp * architecture-specific module. The interpolation can use either a
105f425c1f6SPoul-Henning Kamp * dedicated counter or a processor cycle counter (PCC) implemented in
106f425c1f6SPoul-Henning Kamp * some architectures.
1076f70df15SPoul-Henning Kamp *
108c68996e2SPoul-Henning Kamp * Note that all routines must run at priority splclock or higher.
1096f70df15SPoul-Henning Kamp */
110c68996e2SPoul-Henning Kamp /*
111c68996e2SPoul-Henning Kamp * Phase/frequency-lock loop (PLL/FLL) definitions
112c68996e2SPoul-Henning Kamp *
113c68996e2SPoul-Henning Kamp * The nanosecond clock discipline uses two variable types, time
114c68996e2SPoul-Henning Kamp * variables and frequency variables. Both types are represented as 64-
115c68996e2SPoul-Henning Kamp * bit fixed-point quantities with the decimal point between two 32-bit
116c68996e2SPoul-Henning Kamp * halves. On a 32-bit machine, each half is represented as a single
117c68996e2SPoul-Henning Kamp * word and mathematical operations are done using multiple-precision
118c68996e2SPoul-Henning Kamp * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
119c68996e2SPoul-Henning Kamp * used.
120c68996e2SPoul-Henning Kamp *
121c68996e2SPoul-Henning Kamp * A time variable is a signed 64-bit fixed-point number in ns and
122c68996e2SPoul-Henning Kamp * fraction. It represents the remaining time offset to be amortized
123c68996e2SPoul-Henning Kamp * over succeeding tick interrupts. The maximum time offset is about
124f425c1f6SPoul-Henning Kamp * 0.5 s and the resolution is about 2.3e-10 ns.
125c68996e2SPoul-Henning Kamp *
126c68996e2SPoul-Henning Kamp * 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
127c68996e2SPoul-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
128c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129c68996e2SPoul-Henning Kamp * |s s s| ns |
130c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
131c68996e2SPoul-Henning Kamp * | fraction |
132c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133c68996e2SPoul-Henning Kamp *
134c68996e2SPoul-Henning Kamp * A frequency variable is a signed 64-bit fixed-point number in ns/s
135c68996e2SPoul-Henning Kamp * and fraction. It represents the ns and fraction to be added to the
136c68996e2SPoul-Henning Kamp * kernel time variable at each second. The maximum frequency offset is
137f425c1f6SPoul-Henning Kamp * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
138c68996e2SPoul-Henning Kamp *
139c68996e2SPoul-Henning Kamp * 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
140c68996e2SPoul-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
141c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142c68996e2SPoul-Henning Kamp * |s s s s s s s s s s s s s| ns/s |
143c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144c68996e2SPoul-Henning Kamp * | fraction |
145c68996e2SPoul-Henning Kamp * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146c68996e2SPoul-Henning Kamp */
147c68996e2SPoul-Henning Kamp /*
148c68996e2SPoul-Henning Kamp * The following variables establish the state of the PLL/FLL and the
149c68996e2SPoul-Henning Kamp * residual time and frequency offset of the local clock.
150c68996e2SPoul-Henning Kamp */
151c68996e2SPoul-Henning Kamp #define SHIFT_PLL 4 /* PLL loop gain (shift) */
152c68996e2SPoul-Henning Kamp #define SHIFT_FLL 2 /* FLL loop gain (shift) */
153c68996e2SPoul-Henning Kamp
154c68996e2SPoul-Henning Kamp static int time_state = TIME_OK; /* clock state */
1556cedd609SLawrence Stewart int time_status = STA_UNSYNC; /* clock status bits */
15697804a5cSPoul-Henning Kamp static long time_tai; /* TAI offset (s) */
15797804a5cSPoul-Henning Kamp static long time_monitor; /* last time offset scaled (ns) */
158c68996e2SPoul-Henning Kamp static long time_constant; /* poll interval (shift) (s) */
159c68996e2SPoul-Henning Kamp static long time_precision = 1; /* clock precision (ns) */
160c68996e2SPoul-Henning Kamp static long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */
1616cedd609SLawrence Stewart long time_esterror = MAXPHASE / 1000; /* estimated error (us) */
162969fc29eSIan Lepore static long time_reftime; /* uptime at last adjustment (s) */
163c68996e2SPoul-Henning Kamp static l_fp time_offset; /* time offset (ns) */
164c68996e2SPoul-Henning Kamp static l_fp time_freq; /* frequency offset (ns/s) */
16597804a5cSPoul-Henning Kamp static l_fp time_adj; /* tick adjust (ns/s) */
1663f31c649SGarrett Wollman
167e1d970f1SPoul-Henning Kamp static int64_t time_adjtime; /* correction from adjtime(2) (usec) */
168e1d970f1SPoul-Henning Kamp
1694493f659SKonstantin Belousov static struct mtx ntp_lock;
1704493f659SKonstantin Belousov MTX_SYSINIT(ntp, &ntp_lock, "ntp", MTX_SPIN);
171364c516cSKonstantin Belousov
1724493f659SKonstantin Belousov #define NTP_LOCK() mtx_lock_spin(&ntp_lock)
1734493f659SKonstantin Belousov #define NTP_UNLOCK() mtx_unlock_spin(&ntp_lock)
1744493f659SKonstantin Belousov #define NTP_ASSERT_LOCKED() mtx_assert(&ntp_lock, MA_OWNED)
175364c516cSKonstantin Belousov
1763f31c649SGarrett Wollman #ifdef PPS_SYNC
1773f31c649SGarrett Wollman /*
178c68996e2SPoul-Henning Kamp * The following variables are used when a pulse-per-second (PPS) signal
179c68996e2SPoul-Henning Kamp * is available and connected via a modem control lead. They establish
180c68996e2SPoul-Henning Kamp * the engineering parameters of the clock discipline loop when
181c68996e2SPoul-Henning Kamp * controlled by the PPS signal.
1823f31c649SGarrett Wollman */
183c68996e2SPoul-Henning Kamp #define PPS_FAVG 2 /* min freq avg interval (s) (shift) */
18424dbea46SJohn Hay #define PPS_FAVGDEF 8 /* default freq avg int (s) (shift) */
18582e84c5bSPoul-Henning Kamp #define PPS_FAVGMAX 15 /* max freq avg interval (s) (shift) */
186c68996e2SPoul-Henning Kamp #define PPS_PAVG 4 /* phase avg interval (s) (shift) */
187c68996e2SPoul-Henning Kamp #define PPS_VALID 120 /* PPS signal watchdog max (s) */
18882e84c5bSPoul-Henning Kamp #define PPS_MAXWANDER 100000 /* max PPS wander (ns/s) */
18982e84c5bSPoul-Henning Kamp #define PPS_POPCORN 2 /* popcorn spike threshold (shift) */
190c68996e2SPoul-Henning Kamp
19182e84c5bSPoul-Henning Kamp static struct timespec pps_tf[3]; /* phase median filter */
192c68996e2SPoul-Henning Kamp static l_fp pps_freq; /* scaled frequency offset (ns/s) */
193f425c1f6SPoul-Henning Kamp static long pps_fcount; /* frequency accumulator */
19482e84c5bSPoul-Henning Kamp static long pps_jitter; /* nominal jitter (ns) */
19582e84c5bSPoul-Henning Kamp static long pps_stabil; /* nominal stability (scaled ns/s) */
196*66145c38SSebastian Huber static time_t pps_lastsec; /* time at last calibration (s) */
197c68996e2SPoul-Henning Kamp static int pps_valid; /* signal watchdog counter */
198c68996e2SPoul-Henning Kamp static int pps_shift = PPS_FAVG; /* interval duration (s) (shift) */
19982e84c5bSPoul-Henning Kamp static int pps_shiftmax = PPS_FAVGDEF; /* max interval duration (s) (shift) */
200c68996e2SPoul-Henning Kamp static int pps_intcnt; /* wander counter */
2016f70df15SPoul-Henning Kamp
2026f70df15SPoul-Henning Kamp /*
2036f70df15SPoul-Henning Kamp * PPS signal quality monitors
2046f70df15SPoul-Henning Kamp */
205c68996e2SPoul-Henning Kamp static long pps_calcnt; /* calibration intervals */
206c68996e2SPoul-Henning Kamp static long pps_jitcnt; /* jitter limit exceeded */
207c68996e2SPoul-Henning Kamp static long pps_stbcnt; /* stability limit exceeded */
208c68996e2SPoul-Henning Kamp static long pps_errcnt; /* calibration errors */
2093f31c649SGarrett Wollman #endif /* PPS_SYNC */
210c68996e2SPoul-Henning Kamp /*
211c68996e2SPoul-Henning Kamp * End of phase/frequency-lock loop (PLL/FLL) definitions
212c68996e2SPoul-Henning Kamp */
2133f31c649SGarrett Wollman
214c68996e2SPoul-Henning Kamp static void hardupdate(long offset);
215932cfd41SMark Santcroos static void ntp_gettime1(struct ntptimeval *ntvp);
216364c516cSKonstantin Belousov static bool ntp_is_time_error(int tsl);
217c68996e2SPoul-Henning Kamp
218364c516cSKonstantin Belousov static bool
ntp_is_time_error(int tsl)219364c516cSKonstantin Belousov ntp_is_time_error(int tsl)
220c68996e2SPoul-Henning Kamp {
221364c516cSKonstantin Belousov
222c68996e2SPoul-Henning Kamp /*
223c68996e2SPoul-Henning Kamp * Status word error decode. If any of these conditions occur,
224c68996e2SPoul-Henning Kamp * an error is returned, instead of the status word. Most
225c68996e2SPoul-Henning Kamp * applications will care only about the fact the system clock
226c68996e2SPoul-Henning Kamp * may not be trusted, not about the details.
227c68996e2SPoul-Henning Kamp *
228c68996e2SPoul-Henning Kamp * Hardware or software error
229c68996e2SPoul-Henning Kamp */
230364c516cSKonstantin Belousov if ((tsl & (STA_UNSYNC | STA_CLOCKERR)) ||
231c68996e2SPoul-Henning Kamp
232c68996e2SPoul-Henning Kamp /*
233c68996e2SPoul-Henning Kamp * PPS signal lost when either time or frequency synchronization
234c68996e2SPoul-Henning Kamp * requested
235c68996e2SPoul-Henning Kamp */
236364c516cSKonstantin Belousov (tsl & (STA_PPSFREQ | STA_PPSTIME) &&
237364c516cSKonstantin Belousov !(tsl & STA_PPSSIGNAL)) ||
238c68996e2SPoul-Henning Kamp
239c68996e2SPoul-Henning Kamp /*
240c68996e2SPoul-Henning Kamp * PPS jitter exceeded when time synchronization requested
241c68996e2SPoul-Henning Kamp */
242364c516cSKonstantin Belousov (tsl & STA_PPSTIME && tsl & STA_PPSJITTER) ||
243c68996e2SPoul-Henning Kamp
244c68996e2SPoul-Henning Kamp /*
245c68996e2SPoul-Henning Kamp * PPS wander exceeded or calibration error when frequency
246c68996e2SPoul-Henning Kamp * synchronization requested
247c68996e2SPoul-Henning Kamp */
248364c516cSKonstantin Belousov (tsl & STA_PPSFREQ &&
249364c516cSKonstantin Belousov tsl & (STA_PPSWANDER | STA_PPSERROR)))
250364c516cSKonstantin Belousov return (true);
2519a9ae42aSAndriy Gapon
252364c516cSKonstantin Belousov return (false);
2539a9ae42aSAndriy Gapon }
2549a9ae42aSAndriy Gapon
2559a9ae42aSAndriy Gapon static void
ntp_gettime1(struct ntptimeval * ntvp)2569a9ae42aSAndriy Gapon ntp_gettime1(struct ntptimeval *ntvp)
2579a9ae42aSAndriy Gapon {
2589a9ae42aSAndriy Gapon struct timespec atv; /* nanosecond time */
2599a9ae42aSAndriy Gapon
2604493f659SKonstantin Belousov NTP_ASSERT_LOCKED();
2619a9ae42aSAndriy Gapon
2629a9ae42aSAndriy Gapon nanotime(&atv);
2639a9ae42aSAndriy Gapon ntvp->time.tv_sec = atv.tv_sec;
2649a9ae42aSAndriy Gapon ntvp->time.tv_nsec = atv.tv_nsec;
2659a9ae42aSAndriy Gapon ntvp->maxerror = time_maxerror;
2669a9ae42aSAndriy Gapon ntvp->esterror = time_esterror;
2679a9ae42aSAndriy Gapon ntvp->tai = time_tai;
2689a9ae42aSAndriy Gapon ntvp->time_state = time_state;
2699a9ae42aSAndriy Gapon
270364c516cSKonstantin Belousov if (ntp_is_time_error(time_status))
271932cfd41SMark Santcroos ntvp->time_state = TIME_ERROR;
272932cfd41SMark Santcroos }
273932cfd41SMark Santcroos
2749b7fe7e4SMark Santcroos /*
2759b7fe7e4SMark Santcroos * ntp_gettime() - NTP user application interface
2769b7fe7e4SMark Santcroos *
277873fbcd7SRobert Watson * See the timex.h header file for synopsis and API description. Note that
278873fbcd7SRobert Watson * the TAI offset is returned in the ntvtimeval.tai structure member.
2799b7fe7e4SMark Santcroos */
280932cfd41SMark Santcroos #ifndef _SYS_SYSPROTO_H_
281932cfd41SMark Santcroos struct ntp_gettime_args {
282932cfd41SMark Santcroos struct ntptimeval *ntvp;
283932cfd41SMark Santcroos };
284932cfd41SMark Santcroos #endif
285932cfd41SMark Santcroos /* ARGSUSED */
286932cfd41SMark Santcroos int
sys_ntp_gettime(struct thread * td,struct ntp_gettime_args * uap)2878451d0ddSKip Macy sys_ntp_gettime(struct thread *td, struct ntp_gettime_args *uap)
288932cfd41SMark Santcroos {
289932cfd41SMark Santcroos struct ntptimeval ntv;
290932cfd41SMark Santcroos
291fb441a88SKonstantin Belousov memset(&ntv, 0, sizeof(ntv));
292fb441a88SKonstantin Belousov
2934493f659SKonstantin Belousov NTP_LOCK();
294932cfd41SMark Santcroos ntp_gettime1(&ntv);
2954493f659SKonstantin Belousov NTP_UNLOCK();
296932cfd41SMark Santcroos
297fe18f385SWarner Losh td->td_retval[0] = ntv.time_state;
298932cfd41SMark Santcroos return (copyout(&ntv, uap->ntvp, sizeof(ntv)));
299932cfd41SMark Santcroos }
300932cfd41SMark Santcroos
301932cfd41SMark Santcroos static int
ntp_sysctl(SYSCTL_HANDLER_ARGS)302932cfd41SMark Santcroos ntp_sysctl(SYSCTL_HANDLER_ARGS)
303932cfd41SMark Santcroos {
304932cfd41SMark Santcroos struct ntptimeval ntv; /* temporary structure */
305932cfd41SMark Santcroos
306c7dc361dSMark Johnston memset(&ntv, 0, sizeof(ntv));
307c7dc361dSMark Johnston
3084493f659SKonstantin Belousov NTP_LOCK();
309932cfd41SMark Santcroos ntp_gettime1(&ntv);
3104493f659SKonstantin Belousov NTP_UNLOCK();
311932cfd41SMark Santcroos
312932cfd41SMark Santcroos return (sysctl_handle_opaque(oidp, &ntv, sizeof(ntv), req));
313c68996e2SPoul-Henning Kamp }
314c68996e2SPoul-Henning Kamp
3157029da5cSPawel Biernacki SYSCTL_NODE(_kern, OID_AUTO, ntp_pll, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3167029da5cSPawel Biernacki "");
317364c516cSKonstantin Belousov SYSCTL_PROC(_kern_ntp_pll, OID_AUTO, gettime, CTLTYPE_OPAQUE | CTLFLAG_RD |
318364c516cSKonstantin Belousov CTLFLAG_MPSAFE, 0, sizeof(struct ntptimeval) , ntp_sysctl, "S,ntptimeval",
319364c516cSKonstantin Belousov "");
320c68996e2SPoul-Henning Kamp
3215968e18bSPoul-Henning Kamp #ifdef PPS_SYNC
3223eb9ab52SEitan Adler SYSCTL_INT(_kern_ntp_pll, OID_AUTO, pps_shiftmax, CTLFLAG_RW,
3233eb9ab52SEitan Adler &pps_shiftmax, 0, "Max interval duration (sec) (shift)");
3243eb9ab52SEitan Adler SYSCTL_INT(_kern_ntp_pll, OID_AUTO, pps_shift, CTLFLAG_RW,
3253eb9ab52SEitan Adler &pps_shift, 0, "Interval duration (sec) (shift)");
326240577c2SMatthew D Fleming SYSCTL_LONG(_kern_ntp_pll, OID_AUTO, time_monitor, CTLFLAG_RD,
3273eb9ab52SEitan Adler &time_monitor, 0, "Last time offset scaled (ns)");
3287fd299cbSPoul-Henning Kamp
329364c516cSKonstantin Belousov SYSCTL_S64(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD | CTLFLAG_MPSAFE,
330364c516cSKonstantin Belousov &pps_freq, 0,
331364c516cSKonstantin Belousov "Scaled frequency offset (ns/sec)");
332364c516cSKonstantin Belousov SYSCTL_S64(_kern_ntp_pll, OID_AUTO, time_freq, CTLFLAG_RD | CTLFLAG_MPSAFE,
333364c516cSKonstantin Belousov &time_freq, 0,
334364c516cSKonstantin Belousov "Frequency offset (ns/sec)");
3355968e18bSPoul-Henning Kamp #endif
336873fbcd7SRobert Watson
337c68996e2SPoul-Henning Kamp /*
338c68996e2SPoul-Henning Kamp * ntp_adjtime() - NTP daemon application interface
339c68996e2SPoul-Henning Kamp *
340873fbcd7SRobert Watson * See the timex.h header file for synopsis and API description. Note that
341873fbcd7SRobert Watson * the timex.constant structure member has a dual purpose to set the time
342873fbcd7SRobert Watson * constant and to set the TAI offset.
343c68996e2SPoul-Henning Kamp */
344c68996e2SPoul-Henning Kamp int
kern_ntp_adjtime(struct thread * td,struct timex * ntv,int * retvalp)345be2535b0SKonstantin Belousov kern_ntp_adjtime(struct thread *td, struct timex *ntv, int *retvalp)
346c68996e2SPoul-Henning Kamp {
347f425c1f6SPoul-Henning Kamp long freq; /* frequency ns/s) */
348c68996e2SPoul-Henning Kamp int modes; /* mode bits from structure */
349364c516cSKonstantin Belousov int error, retval;
350c68996e2SPoul-Henning Kamp
351c68996e2SPoul-Henning Kamp /*
352c68996e2SPoul-Henning Kamp * Update selected clock variables - only the superuser can
353c68996e2SPoul-Henning Kamp * change anything. Note that there is no error checking here on
354c68996e2SPoul-Henning Kamp * the assumption the superuser should know what it is doing.
35597804a5cSPoul-Henning Kamp * Note that either the time constant or TAI offset are loaded
35624dbea46SJohn Hay * from the ntv.constant member, depending on the mode bits. If
35724dbea46SJohn Hay * the STA_PLL bit in the status word is cleared, the state and
35824dbea46SJohn Hay * status words are reset to the initial values at boot.
359c68996e2SPoul-Henning Kamp */
360be2535b0SKonstantin Belousov modes = ntv->modes;
361be2535b0SKonstantin Belousov error = 0;
362fafbe352SPoul-Henning Kamp if (modes)
363acd3428bSRobert Watson error = priv_check(td, PRIV_NTP_ADJTIME);
364364c516cSKonstantin Belousov if (error != 0)
365364c516cSKonstantin Belousov return (error);
3664493f659SKonstantin Belousov NTP_LOCK();
367c68996e2SPoul-Henning Kamp if (modes & MOD_MAXERROR)
368be2535b0SKonstantin Belousov time_maxerror = ntv->maxerror;
369c68996e2SPoul-Henning Kamp if (modes & MOD_ESTERROR)
370be2535b0SKonstantin Belousov time_esterror = ntv->esterror;
371c68996e2SPoul-Henning Kamp if (modes & MOD_STATUS) {
372be2535b0SKonstantin Belousov if (time_status & STA_PLL && !(ntv->status & STA_PLL)) {
37324dbea46SJohn Hay time_state = TIME_OK;
37424dbea46SJohn Hay time_status = STA_UNSYNC;
37524dbea46SJohn Hay #ifdef PPS_SYNC
37624dbea46SJohn Hay pps_shift = PPS_FAVG;
37724dbea46SJohn Hay #endif /* PPS_SYNC */
37824dbea46SJohn Hay }
379c68996e2SPoul-Henning Kamp time_status &= STA_RONLY;
380be2535b0SKonstantin Belousov time_status |= ntv->status & ~STA_RONLY;
381c68996e2SPoul-Henning Kamp }
382f425c1f6SPoul-Henning Kamp if (modes & MOD_TIMECONST) {
383be2535b0SKonstantin Belousov if (ntv->constant < 0)
384f425c1f6SPoul-Henning Kamp time_constant = 0;
385be2535b0SKonstantin Belousov else if (ntv->constant > MAXTC)
386f425c1f6SPoul-Henning Kamp time_constant = MAXTC;
387f425c1f6SPoul-Henning Kamp else
388be2535b0SKonstantin Belousov time_constant = ntv->constant;
389f425c1f6SPoul-Henning Kamp }
39097804a5cSPoul-Henning Kamp if (modes & MOD_TAI) {
391be2535b0SKonstantin Belousov if (ntv->constant > 0) /* XXX zero & negative numbers ? */
392be2535b0SKonstantin Belousov time_tai = ntv->constant;
39397804a5cSPoul-Henning Kamp }
39482e84c5bSPoul-Henning Kamp #ifdef PPS_SYNC
39582e84c5bSPoul-Henning Kamp if (modes & MOD_PPSMAX) {
396be2535b0SKonstantin Belousov if (ntv->shift < PPS_FAVG)
39782e84c5bSPoul-Henning Kamp pps_shiftmax = PPS_FAVG;
398be2535b0SKonstantin Belousov else if (ntv->shift > PPS_FAVGMAX)
39982e84c5bSPoul-Henning Kamp pps_shiftmax = PPS_FAVGMAX;
40082e84c5bSPoul-Henning Kamp else
401be2535b0SKonstantin Belousov pps_shiftmax = ntv->shift;
40282e84c5bSPoul-Henning Kamp }
40382e84c5bSPoul-Henning Kamp #endif /* PPS_SYNC */
404c68996e2SPoul-Henning Kamp if (modes & MOD_NANO)
405c68996e2SPoul-Henning Kamp time_status |= STA_NANO;
406c68996e2SPoul-Henning Kamp if (modes & MOD_MICRO)
407c68996e2SPoul-Henning Kamp time_status &= ~STA_NANO;
408c68996e2SPoul-Henning Kamp if (modes & MOD_CLKB)
409c68996e2SPoul-Henning Kamp time_status |= STA_CLK;
410c68996e2SPoul-Henning Kamp if (modes & MOD_CLKA)
411c68996e2SPoul-Henning Kamp time_status &= ~STA_CLK;
41224dbea46SJohn Hay if (modes & MOD_FREQUENCY) {
413be2535b0SKonstantin Belousov freq = (ntv->freq * 1000LL) >> 16;
41424dbea46SJohn Hay if (freq > MAXFREQ)
41524dbea46SJohn Hay L_LINT(time_freq, MAXFREQ);
41624dbea46SJohn Hay else if (freq < -MAXFREQ)
41724dbea46SJohn Hay L_LINT(time_freq, -MAXFREQ);
418bcfe6d8bSPoul-Henning Kamp else {
419bcfe6d8bSPoul-Henning Kamp /*
420be2535b0SKonstantin Belousov * ntv->freq is [PPM * 2^16] = [us/s * 2^16]
421bcfe6d8bSPoul-Henning Kamp * time_freq is [ns/s * 2^32]
422bcfe6d8bSPoul-Henning Kamp */
423be2535b0SKonstantin Belousov time_freq = ntv->freq * 1000LL * 65536LL;
424bcfe6d8bSPoul-Henning Kamp }
42524dbea46SJohn Hay #ifdef PPS_SYNC
42624dbea46SJohn Hay pps_freq = time_freq;
42724dbea46SJohn Hay #endif /* PPS_SYNC */
42824dbea46SJohn Hay }
429551260fcSPoul-Henning Kamp if (modes & MOD_OFFSET) {
430551260fcSPoul-Henning Kamp if (time_status & STA_NANO)
431be2535b0SKonstantin Belousov hardupdate(ntv->offset);
432551260fcSPoul-Henning Kamp else
433be2535b0SKonstantin Belousov hardupdate(ntv->offset * 1000);
434551260fcSPoul-Henning Kamp }
435c68996e2SPoul-Henning Kamp
436c68996e2SPoul-Henning Kamp /*
43797804a5cSPoul-Henning Kamp * Retrieve all clock variables. Note that the TAI offset is
43897804a5cSPoul-Henning Kamp * returned only by ntp_gettime();
439c68996e2SPoul-Henning Kamp */
440c68996e2SPoul-Henning Kamp if (time_status & STA_NANO)
441be2535b0SKonstantin Belousov ntv->offset = L_GINT(time_offset);
442c68996e2SPoul-Henning Kamp else
443be2535b0SKonstantin Belousov ntv->offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */
444be2535b0SKonstantin Belousov ntv->freq = L_GINT((time_freq / 1000LL) << 16);
445be2535b0SKonstantin Belousov ntv->maxerror = time_maxerror;
446be2535b0SKonstantin Belousov ntv->esterror = time_esterror;
447be2535b0SKonstantin Belousov ntv->status = time_status;
448be2535b0SKonstantin Belousov ntv->constant = time_constant;
449c68996e2SPoul-Henning Kamp if (time_status & STA_NANO)
450be2535b0SKonstantin Belousov ntv->precision = time_precision;
451c68996e2SPoul-Henning Kamp else
452be2535b0SKonstantin Belousov ntv->precision = time_precision / 1000;
453be2535b0SKonstantin Belousov ntv->tolerance = MAXFREQ * SCALE_PPM;
454c68996e2SPoul-Henning Kamp #ifdef PPS_SYNC
455be2535b0SKonstantin Belousov ntv->shift = pps_shift;
456be2535b0SKonstantin Belousov ntv->ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
457c68996e2SPoul-Henning Kamp if (time_status & STA_NANO)
458be2535b0SKonstantin Belousov ntv->jitter = pps_jitter;
459c68996e2SPoul-Henning Kamp else
460be2535b0SKonstantin Belousov ntv->jitter = pps_jitter / 1000;
461be2535b0SKonstantin Belousov ntv->stabil = pps_stabil;
462be2535b0SKonstantin Belousov ntv->calcnt = pps_calcnt;
463be2535b0SKonstantin Belousov ntv->errcnt = pps_errcnt;
464be2535b0SKonstantin Belousov ntv->jitcnt = pps_jitcnt;
465be2535b0SKonstantin Belousov ntv->stbcnt = pps_stbcnt;
466c68996e2SPoul-Henning Kamp #endif /* PPS_SYNC */
467364c516cSKonstantin Belousov retval = ntp_is_time_error(time_status) ? TIME_ERROR : time_state;
4684493f659SKonstantin Belousov NTP_UNLOCK();
469c68996e2SPoul-Henning Kamp
470be2535b0SKonstantin Belousov *retvalp = retval;
471be2535b0SKonstantin Belousov return (0);
472be2535b0SKonstantin Belousov }
473be2535b0SKonstantin Belousov
474be2535b0SKonstantin Belousov #ifndef _SYS_SYSPROTO_H_
475be2535b0SKonstantin Belousov struct ntp_adjtime_args {
476be2535b0SKonstantin Belousov struct timex *tp;
477be2535b0SKonstantin Belousov };
478be2535b0SKonstantin Belousov #endif
479be2535b0SKonstantin Belousov
480be2535b0SKonstantin Belousov int
sys_ntp_adjtime(struct thread * td,struct ntp_adjtime_args * uap)481be2535b0SKonstantin Belousov sys_ntp_adjtime(struct thread *td, struct ntp_adjtime_args *uap)
482be2535b0SKonstantin Belousov {
483be2535b0SKonstantin Belousov struct timex ntv;
484be2535b0SKonstantin Belousov int error, retval;
485be2535b0SKonstantin Belousov
486be2535b0SKonstantin Belousov error = copyin(uap->tp, &ntv, sizeof(ntv));
487be2535b0SKonstantin Belousov if (error == 0) {
488be2535b0SKonstantin Belousov error = kern_ntp_adjtime(td, &ntv, &retval);
489be2535b0SKonstantin Belousov if (error == 0) {
490be2535b0SKonstantin Belousov error = copyout(&ntv, uap->tp, sizeof(ntv));
491364c516cSKonstantin Belousov if (error == 0)
492364c516cSKonstantin Belousov td->td_retval[0] = retval;
493be2535b0SKonstantin Belousov }
494be2535b0SKonstantin Belousov }
495a5088017SPoul-Henning Kamp return (error);
496c68996e2SPoul-Henning Kamp }
497c68996e2SPoul-Henning Kamp
498c68996e2SPoul-Henning Kamp /*
499c68996e2SPoul-Henning Kamp * second_overflow() - called after ntp_tick_adjust()
500c68996e2SPoul-Henning Kamp *
501c68996e2SPoul-Henning Kamp * This routine is ordinarily called immediately following the above
502c68996e2SPoul-Henning Kamp * routine ntp_tick_adjust(). While these two routines are normally
503c68996e2SPoul-Henning Kamp * combined, they are separated here only for the purposes of
504c68996e2SPoul-Henning Kamp * simulation.
505c68996e2SPoul-Henning Kamp */
506c68996e2SPoul-Henning Kamp void
ntp_update_second(int64_t * adjustment,time_t * newsec)507b4a1d0deSPoul-Henning Kamp ntp_update_second(int64_t *adjustment, time_t *newsec)
508c68996e2SPoul-Henning Kamp {
509e1d970f1SPoul-Henning Kamp int tickrate;
51097804a5cSPoul-Henning Kamp l_fp ftemp; /* 32/64-bit temporary */
511c68996e2SPoul-Henning Kamp
5124493f659SKonstantin Belousov NTP_LOCK();
5134493f659SKonstantin Belousov
51482e84c5bSPoul-Henning Kamp /*
51582e84c5bSPoul-Henning Kamp * On rollover of the second both the nanosecond and microsecond
51682e84c5bSPoul-Henning Kamp * clocks are updated and the state machine cranked as
51782e84c5bSPoul-Henning Kamp * necessary. The phase adjustment to be used for the next
51882e84c5bSPoul-Henning Kamp * second is calculated and the maximum error is increased by
51982e84c5bSPoul-Henning Kamp * the tolerance.
52082e84c5bSPoul-Henning Kamp */
521c68996e2SPoul-Henning Kamp time_maxerror += MAXFREQ / 1000;
522c68996e2SPoul-Henning Kamp
523c68996e2SPoul-Henning Kamp /*
524c68996e2SPoul-Henning Kamp * Leap second processing. If in leap-insert state at
525c68996e2SPoul-Henning Kamp * the end of the day, the system clock is set back one
526c68996e2SPoul-Henning Kamp * second; if in leap-delete state, the system clock is
527c68996e2SPoul-Henning Kamp * set ahead one second. The nano_time() routine or
528c68996e2SPoul-Henning Kamp * external clock driver will insure that reported time
529c68996e2SPoul-Henning Kamp * is always monotonic.
530c68996e2SPoul-Henning Kamp */
531c68996e2SPoul-Henning Kamp switch (time_state) {
532c68996e2SPoul-Henning Kamp /*
533c68996e2SPoul-Henning Kamp * No warning.
534c68996e2SPoul-Henning Kamp */
535c68996e2SPoul-Henning Kamp case TIME_OK:
536c68996e2SPoul-Henning Kamp if (time_status & STA_INS)
537c68996e2SPoul-Henning Kamp time_state = TIME_INS;
538c68996e2SPoul-Henning Kamp else if (time_status & STA_DEL)
539c68996e2SPoul-Henning Kamp time_state = TIME_DEL;
540c68996e2SPoul-Henning Kamp break;
541c68996e2SPoul-Henning Kamp
542c68996e2SPoul-Henning Kamp /*
543c68996e2SPoul-Henning Kamp * Insert second 23:59:60 following second
544c68996e2SPoul-Henning Kamp * 23:59:59.
545c68996e2SPoul-Henning Kamp */
546c68996e2SPoul-Henning Kamp case TIME_INS:
547c68996e2SPoul-Henning Kamp if (!(time_status & STA_INS))
548c68996e2SPoul-Henning Kamp time_state = TIME_OK;
549c68996e2SPoul-Henning Kamp else if ((*newsec) % 86400 == 0) {
550c68996e2SPoul-Henning Kamp (*newsec)--;
551c68996e2SPoul-Henning Kamp time_state = TIME_OOP;
552eac3c62bSWarner Losh time_tai++;
553c68996e2SPoul-Henning Kamp }
554c68996e2SPoul-Henning Kamp break;
555c68996e2SPoul-Henning Kamp
556c68996e2SPoul-Henning Kamp /*
557c68996e2SPoul-Henning Kamp * Delete second 23:59:59.
558c68996e2SPoul-Henning Kamp */
559c68996e2SPoul-Henning Kamp case TIME_DEL:
560c68996e2SPoul-Henning Kamp if (!(time_status & STA_DEL))
561c68996e2SPoul-Henning Kamp time_state = TIME_OK;
562c68996e2SPoul-Henning Kamp else if (((*newsec) + 1) % 86400 == 0) {
563c68996e2SPoul-Henning Kamp (*newsec)++;
56497804a5cSPoul-Henning Kamp time_tai--;
565c68996e2SPoul-Henning Kamp time_state = TIME_WAIT;
566c68996e2SPoul-Henning Kamp }
567c68996e2SPoul-Henning Kamp break;
568c68996e2SPoul-Henning Kamp
569c68996e2SPoul-Henning Kamp /*
570c68996e2SPoul-Henning Kamp * Insert second in progress.
571c68996e2SPoul-Henning Kamp */
572c68996e2SPoul-Henning Kamp case TIME_OOP:
573c68996e2SPoul-Henning Kamp time_state = TIME_WAIT;
574c68996e2SPoul-Henning Kamp break;
575c68996e2SPoul-Henning Kamp
576c68996e2SPoul-Henning Kamp /*
577c68996e2SPoul-Henning Kamp * Wait for status bits to clear.
578c68996e2SPoul-Henning Kamp */
579c68996e2SPoul-Henning Kamp case TIME_WAIT:
580c68996e2SPoul-Henning Kamp if (!(time_status & (STA_INS | STA_DEL)))
581c68996e2SPoul-Henning Kamp time_state = TIME_OK;
582c68996e2SPoul-Henning Kamp }
583c68996e2SPoul-Henning Kamp
584c68996e2SPoul-Henning Kamp /*
58582e84c5bSPoul-Henning Kamp * Compute the total time adjustment for the next second
58682e84c5bSPoul-Henning Kamp * in ns. The offset is reduced by a factor depending on
58782e84c5bSPoul-Henning Kamp * whether the PPS signal is operating. Note that the
58882e84c5bSPoul-Henning Kamp * value is in effect scaled by the clock frequency,
58982e84c5bSPoul-Henning Kamp * since the adjustment is added at each tick interrupt.
590c68996e2SPoul-Henning Kamp */
59197804a5cSPoul-Henning Kamp ftemp = time_offset;
592c68996e2SPoul-Henning Kamp #ifdef PPS_SYNC
59397804a5cSPoul-Henning Kamp /* XXX even if PPS signal dies we should finish adjustment ? */
59497804a5cSPoul-Henning Kamp if (time_status & STA_PPSTIME && time_status &
59597804a5cSPoul-Henning Kamp STA_PPSSIGNAL)
59697804a5cSPoul-Henning Kamp L_RSHIFT(ftemp, pps_shift);
59797804a5cSPoul-Henning Kamp else
59897804a5cSPoul-Henning Kamp L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
59982e84c5bSPoul-Henning Kamp #else
60097804a5cSPoul-Henning Kamp L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
60182e84c5bSPoul-Henning Kamp #endif /* PPS_SYNC */
60297804a5cSPoul-Henning Kamp time_adj = ftemp;
60397804a5cSPoul-Henning Kamp L_SUB(time_offset, ftemp);
604c68996e2SPoul-Henning Kamp L_ADD(time_adj, time_freq);
605e1d970f1SPoul-Henning Kamp
606e1d970f1SPoul-Henning Kamp /*
607e1d970f1SPoul-Henning Kamp * Apply any correction from adjtime(2). If more than one second
608e1d970f1SPoul-Henning Kamp * off we slew at a rate of 5ms/s (5000 PPM) else 500us/s (500 PPM)
609e1d970f1SPoul-Henning Kamp * until the last second is slewed the final < 500 usecs.
610e1d970f1SPoul-Henning Kamp */
611e1d970f1SPoul-Henning Kamp if (time_adjtime != 0) {
612e1d970f1SPoul-Henning Kamp if (time_adjtime > 1000000)
613e1d970f1SPoul-Henning Kamp tickrate = 5000;
614e1d970f1SPoul-Henning Kamp else if (time_adjtime < -1000000)
615e1d970f1SPoul-Henning Kamp tickrate = -5000;
616e1d970f1SPoul-Henning Kamp else if (time_adjtime > 500)
617e1d970f1SPoul-Henning Kamp tickrate = 500;
618e1d970f1SPoul-Henning Kamp else if (time_adjtime < -500)
619e1d970f1SPoul-Henning Kamp tickrate = -500;
620e1d970f1SPoul-Henning Kamp else
621bcfe6d8bSPoul-Henning Kamp tickrate = time_adjtime;
622e1d970f1SPoul-Henning Kamp time_adjtime -= tickrate;
623e1d970f1SPoul-Henning Kamp L_LINT(ftemp, tickrate * 1000);
624e1d970f1SPoul-Henning Kamp L_ADD(time_adj, ftemp);
625e1d970f1SPoul-Henning Kamp }
626b4a1d0deSPoul-Henning Kamp *adjustment = time_adj;
627e1d970f1SPoul-Henning Kamp
628c68996e2SPoul-Henning Kamp #ifdef PPS_SYNC
629c68996e2SPoul-Henning Kamp if (pps_valid > 0)
630c68996e2SPoul-Henning Kamp pps_valid--;
631c68996e2SPoul-Henning Kamp else
63224dbea46SJohn Hay time_status &= ~STA_PPSSIGNAL;
633c68996e2SPoul-Henning Kamp #endif /* PPS_SYNC */
6344493f659SKonstantin Belousov
6354493f659SKonstantin Belousov NTP_UNLOCK();
636c68996e2SPoul-Henning Kamp }
637c68996e2SPoul-Henning Kamp
638c68996e2SPoul-Henning Kamp /*
6396f70df15SPoul-Henning Kamp * hardupdate() - local clock update
6406f70df15SPoul-Henning Kamp *
6416f70df15SPoul-Henning Kamp * This routine is called by ntp_adjtime() to update the local clock
6426f70df15SPoul-Henning Kamp * phase and frequency. The implementation is of an adaptive-parameter,
6436f70df15SPoul-Henning Kamp * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
6446f70df15SPoul-Henning Kamp * time and frequency offset estimates for each call. If the kernel PPS
6456f70df15SPoul-Henning Kamp * discipline code is configured (PPS_SYNC), the PPS signal itself
6466f70df15SPoul-Henning Kamp * determines the new time offset, instead of the calling argument.
6476f70df15SPoul-Henning Kamp * Presumably, calls to ntp_adjtime() occur only when the caller
6486f70df15SPoul-Henning Kamp * believes the local clock is valid within some bound (+-128 ms with
6496f70df15SPoul-Henning Kamp * NTP). If the caller's time is far different than the PPS time, an
6506f70df15SPoul-Henning Kamp * argument will ensue, and it's not clear who will lose.
6516f70df15SPoul-Henning Kamp *
652c68996e2SPoul-Henning Kamp * For uncompensated quartz crystal oscillators and nominal update
653c68996e2SPoul-Henning Kamp * intervals less than 256 s, operation should be in phase-lock mode,
654c68996e2SPoul-Henning Kamp * where the loop is disciplined to phase. For update intervals greater
655c68996e2SPoul-Henning Kamp * than 1024 s, operation should be in frequency-lock mode, where the
656c68996e2SPoul-Henning Kamp * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
657c68996e2SPoul-Henning Kamp * is selected by the STA_MODE status bit.
6586f70df15SPoul-Henning Kamp */
6596f70df15SPoul-Henning Kamp static void
hardupdate(long offset)660a066bba2SMateusz Guzik hardupdate(long offset /* clock offset (ns) */)
6616f70df15SPoul-Henning Kamp {
66297804a5cSPoul-Henning Kamp long mtemp;
663c68996e2SPoul-Henning Kamp l_fp ftemp;
6646f70df15SPoul-Henning Kamp
6654493f659SKonstantin Belousov NTP_ASSERT_LOCKED();
666364c516cSKonstantin Belousov
667c68996e2SPoul-Henning Kamp /*
668c68996e2SPoul-Henning Kamp * Select how the phase is to be controlled and from which
669c68996e2SPoul-Henning Kamp * source. If the PPS signal is present and enabled to
670c68996e2SPoul-Henning Kamp * discipline the time, the PPS offset is used; otherwise, the
671c68996e2SPoul-Henning Kamp * argument offset is used.
672c68996e2SPoul-Henning Kamp */
67382e84c5bSPoul-Henning Kamp if (!(time_status & STA_PLL))
67482e84c5bSPoul-Henning Kamp return;
67597804a5cSPoul-Henning Kamp if (!(time_status & STA_PPSTIME && time_status &
67697804a5cSPoul-Henning Kamp STA_PPSSIGNAL)) {
67797804a5cSPoul-Henning Kamp if (offset > MAXPHASE)
67897804a5cSPoul-Henning Kamp time_monitor = MAXPHASE;
67997804a5cSPoul-Henning Kamp else if (offset < -MAXPHASE)
68097804a5cSPoul-Henning Kamp time_monitor = -MAXPHASE;
68197804a5cSPoul-Henning Kamp else
68297804a5cSPoul-Henning Kamp time_monitor = offset;
68397804a5cSPoul-Henning Kamp L_LINT(time_offset, time_monitor);
68497804a5cSPoul-Henning Kamp }
6856f70df15SPoul-Henning Kamp
6866f70df15SPoul-Henning Kamp /*
687c68996e2SPoul-Henning Kamp * Select how the frequency is to be controlled and in which
688c68996e2SPoul-Henning Kamp * mode (PLL or FLL). If the PPS signal is present and enabled
689c68996e2SPoul-Henning Kamp * to discipline the frequency, the PPS frequency is used;
690c68996e2SPoul-Henning Kamp * otherwise, the argument offset is used to compute it.
6916f70df15SPoul-Henning Kamp */
692c68996e2SPoul-Henning Kamp if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) {
693969fc29eSIan Lepore time_reftime = time_uptime;
694c68996e2SPoul-Henning Kamp return;
695c68996e2SPoul-Henning Kamp }
6966f70df15SPoul-Henning Kamp if (time_status & STA_FREQHOLD || time_reftime == 0)
697969fc29eSIan Lepore time_reftime = time_uptime;
698969fc29eSIan Lepore mtemp = time_uptime - time_reftime;
69997804a5cSPoul-Henning Kamp L_LINT(ftemp, time_monitor);
700c68996e2SPoul-Henning Kamp L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
701c68996e2SPoul-Henning Kamp L_MPY(ftemp, mtemp);
702c68996e2SPoul-Henning Kamp L_ADD(time_freq, ftemp);
703c68996e2SPoul-Henning Kamp time_status &= ~STA_MODE;
70497804a5cSPoul-Henning Kamp if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp >
70597804a5cSPoul-Henning Kamp MAXSEC)) {
70697804a5cSPoul-Henning Kamp L_LINT(ftemp, (time_monitor << 4) / mtemp);
70782e84c5bSPoul-Henning Kamp L_RSHIFT(ftemp, SHIFT_FLL + 4);
70882e84c5bSPoul-Henning Kamp L_ADD(time_freq, ftemp);
70982e84c5bSPoul-Henning Kamp time_status |= STA_MODE;
710c68996e2SPoul-Henning Kamp }
711969fc29eSIan Lepore time_reftime = time_uptime;
712c68996e2SPoul-Henning Kamp if (L_GINT(time_freq) > MAXFREQ)
713c68996e2SPoul-Henning Kamp L_LINT(time_freq, MAXFREQ);
714c68996e2SPoul-Henning Kamp else if (L_GINT(time_freq) < -MAXFREQ)
715c68996e2SPoul-Henning Kamp L_LINT(time_freq, -MAXFREQ);
7163f31c649SGarrett Wollman }
7173f31c649SGarrett Wollman
7186f70df15SPoul-Henning Kamp #ifdef PPS_SYNC
7196f70df15SPoul-Henning Kamp /*
7206f70df15SPoul-Henning Kamp * hardpps() - discipline CPU clock oscillator to external PPS signal
7216f70df15SPoul-Henning Kamp *
7226f70df15SPoul-Henning Kamp * This routine is called at each PPS interrupt in order to discipline
72397804a5cSPoul-Henning Kamp * the CPU clock oscillator to the PPS signal. There are two independent
72497804a5cSPoul-Henning Kamp * first-order feedback loops, one for the phase, the other for the
72597804a5cSPoul-Henning Kamp * frequency. The phase loop measures and grooms the PPS phase offset
72697804a5cSPoul-Henning Kamp * and leaves it in a handy spot for the seconds overflow routine. The
72797804a5cSPoul-Henning Kamp * frequency loop averages successive PPS phase differences and
72897804a5cSPoul-Henning Kamp * calculates the PPS frequency offset, which is also processed by the
72997804a5cSPoul-Henning Kamp * seconds overflow routine. The code requires the caller to capture the
73097804a5cSPoul-Henning Kamp * time and architecture-dependent hardware counter values in
73197804a5cSPoul-Henning Kamp * nanoseconds at the on-time PPS signal transition.
7326f70df15SPoul-Henning Kamp *
733c68996e2SPoul-Henning Kamp * Note that, on some Unix systems this routine runs at an interrupt
7346f70df15SPoul-Henning Kamp * priority level higher than the timer interrupt routine hardclock().
7356f70df15SPoul-Henning Kamp * Therefore, the variables used are distinct from the hardclock()
736c68996e2SPoul-Henning Kamp * variables, except for the actual time and frequency variables, which
737c68996e2SPoul-Henning Kamp * are determined by this routine and updated atomically.
738f27ac8e2SEd Maste *
739c7c53e3cSSebastian Huber * tsp - time at current PPS event
740c7c53e3cSSebastian Huber * delta_nsec - time elapsed between the previous and current PPS event
7416f70df15SPoul-Henning Kamp */
7426f70df15SPoul-Henning Kamp void
hardpps(struct timespec * tsp,long delta_nsec)743c7c53e3cSSebastian Huber hardpps(struct timespec *tsp, long delta_nsec)
7446f70df15SPoul-Henning Kamp {
745*66145c38SSebastian Huber long u_nsec, v_nsec; /* temps */
746*66145c38SSebastian Huber time_t u_sec;
747c68996e2SPoul-Henning Kamp l_fp ftemp;
7486f70df15SPoul-Henning Kamp
7494493f659SKonstantin Belousov NTP_LOCK();
750364c516cSKonstantin Belousov
7516f70df15SPoul-Henning Kamp /*
75297804a5cSPoul-Henning Kamp * The signal is first processed by a range gate and frequency
75397804a5cSPoul-Henning Kamp * discriminator. The range gate rejects noise spikes outside
75497804a5cSPoul-Henning Kamp * the range +-500 us. The frequency discriminator rejects input
75597804a5cSPoul-Henning Kamp * signals with apparent frequency outside the range 1 +-500
75697804a5cSPoul-Henning Kamp * PPM. If two hits occur in the same second, we ignore the
75797804a5cSPoul-Henning Kamp * later hit; if not and a hit occurs outside the range gate,
75897804a5cSPoul-Henning Kamp * keep the later hit for later comparison, but do not process
75997804a5cSPoul-Henning Kamp * it.
7606f70df15SPoul-Henning Kamp */
761c68996e2SPoul-Henning Kamp time_status |= STA_PPSSIGNAL | STA_PPSJITTER;
762c68996e2SPoul-Henning Kamp time_status &= ~(STA_PPSWANDER | STA_PPSERROR);
763c68996e2SPoul-Henning Kamp pps_valid = PPS_VALID;
764c68996e2SPoul-Henning Kamp u_sec = tsp->tv_sec;
765c68996e2SPoul-Henning Kamp u_nsec = tsp->tv_nsec;
766c68996e2SPoul-Henning Kamp if (u_nsec >= (NANOSECOND >> 1)) {
767c68996e2SPoul-Henning Kamp u_nsec -= NANOSECOND;
768c68996e2SPoul-Henning Kamp u_sec++;
7696f70df15SPoul-Henning Kamp }
77082e84c5bSPoul-Henning Kamp v_nsec = u_nsec - pps_tf[0].tv_nsec;
771364c516cSKonstantin Belousov if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND - MAXFREQ)
772364c516cSKonstantin Belousov goto out;
773c68996e2SPoul-Henning Kamp pps_tf[2] = pps_tf[1];
774c68996e2SPoul-Henning Kamp pps_tf[1] = pps_tf[0];
77582e84c5bSPoul-Henning Kamp pps_tf[0].tv_sec = u_sec;
77682e84c5bSPoul-Henning Kamp pps_tf[0].tv_nsec = u_nsec;
7776f70df15SPoul-Henning Kamp
7786f70df15SPoul-Henning Kamp /*
7791e48d9d3SSebastian Huber * Update the frequency accumulator using the difference between the
7801e48d9d3SSebastian Huber * current and previous PPS event measured directly by the timecounter.
781c68996e2SPoul-Henning Kamp */
7821e48d9d3SSebastian Huber pps_fcount += delta_nsec - NANOSECOND;
78324dbea46SJohn Hay if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ)
784364c516cSKonstantin Belousov goto out;
785c68996e2SPoul-Henning Kamp time_status &= ~STA_PPSJITTER;
786c68996e2SPoul-Henning Kamp
787c68996e2SPoul-Henning Kamp /*
788c68996e2SPoul-Henning Kamp * A three-stage median filter is used to help denoise the PPS
7896f70df15SPoul-Henning Kamp * time. The median sample becomes the time offset estimate; the
7906f70df15SPoul-Henning Kamp * difference between the other two samples becomes the time
7916f70df15SPoul-Henning Kamp * dispersion (jitter) estimate.
7926f70df15SPoul-Henning Kamp */
79382e84c5bSPoul-Henning Kamp if (pps_tf[0].tv_nsec > pps_tf[1].tv_nsec) {
79482e84c5bSPoul-Henning Kamp if (pps_tf[1].tv_nsec > pps_tf[2].tv_nsec) {
79582e84c5bSPoul-Henning Kamp v_nsec = pps_tf[1].tv_nsec; /* 0 1 2 */
79682e84c5bSPoul-Henning Kamp u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec;
79782e84c5bSPoul-Henning Kamp } else if (pps_tf[2].tv_nsec > pps_tf[0].tv_nsec) {
79882e84c5bSPoul-Henning Kamp v_nsec = pps_tf[0].tv_nsec; /* 2 0 1 */
79982e84c5bSPoul-Henning Kamp u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec;
8006f70df15SPoul-Henning Kamp } else {
80182e84c5bSPoul-Henning Kamp v_nsec = pps_tf[2].tv_nsec; /* 0 2 1 */
80282e84c5bSPoul-Henning Kamp u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec;
803c68996e2SPoul-Henning Kamp }
804c68996e2SPoul-Henning Kamp } else {
80582e84c5bSPoul-Henning Kamp if (pps_tf[1].tv_nsec < pps_tf[2].tv_nsec) {
80682e84c5bSPoul-Henning Kamp v_nsec = pps_tf[1].tv_nsec; /* 2 1 0 */
80782e84c5bSPoul-Henning Kamp u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec;
80882e84c5bSPoul-Henning Kamp } else if (pps_tf[2].tv_nsec < pps_tf[0].tv_nsec) {
80982e84c5bSPoul-Henning Kamp v_nsec = pps_tf[0].tv_nsec; /* 1 0 2 */
81082e84c5bSPoul-Henning Kamp u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec;
811c68996e2SPoul-Henning Kamp } else {
81282e84c5bSPoul-Henning Kamp v_nsec = pps_tf[2].tv_nsec; /* 1 2 0 */
81382e84c5bSPoul-Henning Kamp u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec;
8146f70df15SPoul-Henning Kamp }
8156f70df15SPoul-Henning Kamp }
8166f70df15SPoul-Henning Kamp
8176f70df15SPoul-Henning Kamp /*
818c68996e2SPoul-Henning Kamp * Nominal jitter is due to PPS signal noise and interrupt
81997804a5cSPoul-Henning Kamp * latency. If it exceeds the popcorn threshold, the sample is
82097804a5cSPoul-Henning Kamp * discarded. otherwise, if so enabled, the time offset is
82197804a5cSPoul-Henning Kamp * updated. We can tolerate a modest loss of data here without
82297804a5cSPoul-Henning Kamp * much degrading time accuracy.
82379f1fdb8SWarner Losh *
82479f1fdb8SWarner Losh * The measurements being checked here were made with the system
82579f1fdb8SWarner Losh * timecounter, so the popcorn threshold is not allowed to fall below
82679f1fdb8SWarner Losh * the number of nanoseconds in two ticks of the timecounter. For a
82779f1fdb8SWarner Losh * timecounter running faster than 1 GHz the lower bound is 2ns, just
82879f1fdb8SWarner Losh * to avoid a nonsensical threshold of zero.
8296f70df15SPoul-Henning Kamp */
83079f1fdb8SWarner Losh if (u_nsec > lmax(pps_jitter << PPS_POPCORN,
83179f1fdb8SWarner Losh 2 * (NANOSECOND / (long)qmin(NANOSECOND, tc_getfrequency())))) {
832c68996e2SPoul-Henning Kamp time_status |= STA_PPSJITTER;
833c68996e2SPoul-Henning Kamp pps_jitcnt++;
834c68996e2SPoul-Henning Kamp } else if (time_status & STA_PPSTIME) {
83597804a5cSPoul-Henning Kamp time_monitor = -v_nsec;
83697804a5cSPoul-Henning Kamp L_LINT(time_offset, time_monitor);
837c68996e2SPoul-Henning Kamp }
838c68996e2SPoul-Henning Kamp pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
83982e84c5bSPoul-Henning Kamp u_sec = pps_tf[0].tv_sec - pps_lastsec;
840c68996e2SPoul-Henning Kamp if (u_sec < (1 << pps_shift))
841364c516cSKonstantin Belousov goto out;
842c68996e2SPoul-Henning Kamp
843c68996e2SPoul-Henning Kamp /*
844c68996e2SPoul-Henning Kamp * At the end of the calibration interval the difference between
845c68996e2SPoul-Henning Kamp * the first and last counter values becomes the scaled
846c68996e2SPoul-Henning Kamp * frequency. It will later be divided by the length of the
847c68996e2SPoul-Henning Kamp * interval to determine the frequency update. If the frequency
848c68996e2SPoul-Henning Kamp * exceeds a sanity threshold, or if the actual calibration
849c68996e2SPoul-Henning Kamp * interval is not equal to the expected length, the data are
850c68996e2SPoul-Henning Kamp * discarded. We can tolerate a modest loss of data here without
85197804a5cSPoul-Henning Kamp * much degrading frequency accuracy.
852c68996e2SPoul-Henning Kamp */
853c68996e2SPoul-Henning Kamp pps_calcnt++;
854884ab557SPoul-Henning Kamp v_nsec = -pps_fcount;
85582e84c5bSPoul-Henning Kamp pps_lastsec = pps_tf[0].tv_sec;
856884ab557SPoul-Henning Kamp pps_fcount = 0;
857c68996e2SPoul-Henning Kamp u_nsec = MAXFREQ << pps_shift;
858364c516cSKonstantin Belousov if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 << pps_shift)) {
859c68996e2SPoul-Henning Kamp time_status |= STA_PPSERROR;
860c68996e2SPoul-Henning Kamp pps_errcnt++;
861364c516cSKonstantin Belousov goto out;
862c68996e2SPoul-Henning Kamp }
863c68996e2SPoul-Henning Kamp
864c68996e2SPoul-Henning Kamp /*
86582e84c5bSPoul-Henning Kamp * Here the raw frequency offset and wander (stability) is
86682e84c5bSPoul-Henning Kamp * calculated. If the wander is less than the wander threshold
86782e84c5bSPoul-Henning Kamp * for four consecutive averaging intervals, the interval is
86882e84c5bSPoul-Henning Kamp * doubled; if it is greater than the threshold for four
86982e84c5bSPoul-Henning Kamp * consecutive intervals, the interval is halved. The scaled
87082e84c5bSPoul-Henning Kamp * frequency offset is converted to frequency offset. The
87182e84c5bSPoul-Henning Kamp * stability metric is calculated as the average of recent
87282e84c5bSPoul-Henning Kamp * frequency changes, but is used only for performance
873c68996e2SPoul-Henning Kamp * monitoring.
874c68996e2SPoul-Henning Kamp */
875c68996e2SPoul-Henning Kamp L_LINT(ftemp, v_nsec);
876c68996e2SPoul-Henning Kamp L_RSHIFT(ftemp, pps_shift);
877c68996e2SPoul-Henning Kamp L_SUB(ftemp, pps_freq);
878c68996e2SPoul-Henning Kamp u_nsec = L_GINT(ftemp);
87982e84c5bSPoul-Henning Kamp if (u_nsec > PPS_MAXWANDER) {
88082e84c5bSPoul-Henning Kamp L_LINT(ftemp, PPS_MAXWANDER);
881c68996e2SPoul-Henning Kamp pps_intcnt--;
882c68996e2SPoul-Henning Kamp time_status |= STA_PPSWANDER;
883c68996e2SPoul-Henning Kamp pps_stbcnt++;
88482e84c5bSPoul-Henning Kamp } else if (u_nsec < -PPS_MAXWANDER) {
88582e84c5bSPoul-Henning Kamp L_LINT(ftemp, -PPS_MAXWANDER);
886c68996e2SPoul-Henning Kamp pps_intcnt--;
887c68996e2SPoul-Henning Kamp time_status |= STA_PPSWANDER;
888c68996e2SPoul-Henning Kamp pps_stbcnt++;
889c68996e2SPoul-Henning Kamp } else {
8906f70df15SPoul-Henning Kamp pps_intcnt++;
8916f70df15SPoul-Henning Kamp }
89297804a5cSPoul-Henning Kamp if (pps_intcnt >= 4) {
893c68996e2SPoul-Henning Kamp pps_intcnt = 4;
89482e84c5bSPoul-Henning Kamp if (pps_shift < pps_shiftmax) {
895c68996e2SPoul-Henning Kamp pps_shift++;
896c68996e2SPoul-Henning Kamp pps_intcnt = 0;
897c68996e2SPoul-Henning Kamp }
89897804a5cSPoul-Henning Kamp } else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) {
899c68996e2SPoul-Henning Kamp pps_intcnt = -4;
900c68996e2SPoul-Henning Kamp if (pps_shift > PPS_FAVG) {
901c68996e2SPoul-Henning Kamp pps_shift--;
902c68996e2SPoul-Henning Kamp pps_intcnt = 0;
903c68996e2SPoul-Henning Kamp }
904c68996e2SPoul-Henning Kamp }
905c68996e2SPoul-Henning Kamp if (u_nsec < 0)
906c68996e2SPoul-Henning Kamp u_nsec = -u_nsec;
907c68996e2SPoul-Henning Kamp pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
9089ada5a50SPoul-Henning Kamp
909c68996e2SPoul-Henning Kamp /*
91082e84c5bSPoul-Henning Kamp * The PPS frequency is recalculated and clamped to the maximum
91182e84c5bSPoul-Henning Kamp * MAXFREQ. If enabled, the system clock frequency is updated as
91282e84c5bSPoul-Henning Kamp * well.
913c68996e2SPoul-Henning Kamp */
914c68996e2SPoul-Henning Kamp L_ADD(pps_freq, ftemp);
915c68996e2SPoul-Henning Kamp u_nsec = L_GINT(pps_freq);
916c68996e2SPoul-Henning Kamp if (u_nsec > MAXFREQ)
917c68996e2SPoul-Henning Kamp L_LINT(pps_freq, MAXFREQ);
918c68996e2SPoul-Henning Kamp else if (u_nsec < -MAXFREQ)
919c68996e2SPoul-Henning Kamp L_LINT(pps_freq, -MAXFREQ);
92097804a5cSPoul-Henning Kamp if (time_status & STA_PPSFREQ)
921c68996e2SPoul-Henning Kamp time_freq = pps_freq;
922364c516cSKonstantin Belousov
923364c516cSKonstantin Belousov out:
9244493f659SKonstantin Belousov NTP_UNLOCK();
925c68996e2SPoul-Henning Kamp }
9266f70df15SPoul-Henning Kamp #endif /* PPS_SYNC */
927e1d970f1SPoul-Henning Kamp
928e1d970f1SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
929e1d970f1SPoul-Henning Kamp struct adjtime_args {
930e1d970f1SPoul-Henning Kamp struct timeval *delta;
931e1d970f1SPoul-Henning Kamp struct timeval *olddelta;
932e1d970f1SPoul-Henning Kamp };
933e1d970f1SPoul-Henning Kamp #endif
934e1d970f1SPoul-Henning Kamp /* ARGSUSED */
935e1d970f1SPoul-Henning Kamp int
sys_adjtime(struct thread * td,struct adjtime_args * uap)9368451d0ddSKip Macy sys_adjtime(struct thread *td, struct adjtime_args *uap)
937e1d970f1SPoul-Henning Kamp {
938b88ec951SJohn Baldwin struct timeval delta, olddelta, *deltap;
939b88ec951SJohn Baldwin int error;
940b88ec951SJohn Baldwin
941b88ec951SJohn Baldwin if (uap->delta) {
942b88ec951SJohn Baldwin error = copyin(uap->delta, &delta, sizeof(delta));
943b88ec951SJohn Baldwin if (error)
944b88ec951SJohn Baldwin return (error);
945b88ec951SJohn Baldwin deltap = δ
946b88ec951SJohn Baldwin } else
947b88ec951SJohn Baldwin deltap = NULL;
948b88ec951SJohn Baldwin error = kern_adjtime(td, deltap, &olddelta);
949b88ec951SJohn Baldwin if (uap->olddelta && error == 0)
950b88ec951SJohn Baldwin error = copyout(&olddelta, uap->olddelta, sizeof(olddelta));
951b88ec951SJohn Baldwin return (error);
952b88ec951SJohn Baldwin }
953b88ec951SJohn Baldwin
954b88ec951SJohn Baldwin int
kern_adjtime(struct thread * td,struct timeval * delta,struct timeval * olddelta)955b88ec951SJohn Baldwin kern_adjtime(struct thread *td, struct timeval *delta, struct timeval *olddelta)
956b88ec951SJohn Baldwin {
957e1d970f1SPoul-Henning Kamp struct timeval atv;
958364c516cSKonstantin Belousov int64_t ltr, ltw;
959e1d970f1SPoul-Henning Kamp int error;
960e1d970f1SPoul-Henning Kamp
961364c516cSKonstantin Belousov if (delta != NULL) {
962364c516cSKonstantin Belousov error = priv_check(td, PRIV_ADJTIME);
963364c516cSKonstantin Belousov if (error != 0)
964364c516cSKonstantin Belousov return (error);
965364c516cSKonstantin Belousov ltw = (int64_t)delta->tv_sec * 1000000 + delta->tv_usec;
966364c516cSKonstantin Belousov }
9674493f659SKonstantin Belousov NTP_LOCK();
968364c516cSKonstantin Belousov ltr = time_adjtime;
969364c516cSKonstantin Belousov if (delta != NULL)
970364c516cSKonstantin Belousov time_adjtime = ltw;
9714493f659SKonstantin Belousov NTP_UNLOCK();
972364c516cSKonstantin Belousov if (olddelta != NULL) {
973364c516cSKonstantin Belousov atv.tv_sec = ltr / 1000000;
974364c516cSKonstantin Belousov atv.tv_usec = ltr % 1000000;
975e1d970f1SPoul-Henning Kamp if (atv.tv_usec < 0) {
976e1d970f1SPoul-Henning Kamp atv.tv_usec += 1000000;
977e1d970f1SPoul-Henning Kamp atv.tv_sec--;
978e1d970f1SPoul-Henning Kamp }
979b88ec951SJohn Baldwin *olddelta = atv;
980e1d970f1SPoul-Henning Kamp }
981b4be6ef2SRobert Watson return (0);
982b4be6ef2SRobert Watson }
983e1d970f1SPoul-Henning Kamp
9845c7e270fSAndriy Gapon static struct callout resettodr_callout;
9855c7e270fSAndriy Gapon static int resettodr_period = 1800;
9865c7e270fSAndriy Gapon
9875c7e270fSAndriy Gapon static void
periodic_resettodr(void * arg __unused)9885c7e270fSAndriy Gapon periodic_resettodr(void *arg __unused)
9895c7e270fSAndriy Gapon {
9905c7e270fSAndriy Gapon
991364c516cSKonstantin Belousov /*
992364c516cSKonstantin Belousov * Read of time_status is lock-less, which is fine since
993364c516cSKonstantin Belousov * ntp_is_time_error() operates on the consistent read value.
994364c516cSKonstantin Belousov */
995364c516cSKonstantin Belousov if (!ntp_is_time_error(time_status))
9965c7e270fSAndriy Gapon resettodr();
9975c7e270fSAndriy Gapon if (resettodr_period > 0)
9985c7e270fSAndriy Gapon callout_schedule(&resettodr_callout, resettodr_period * hz);
9995c7e270fSAndriy Gapon }
10005c7e270fSAndriy Gapon
10015c7e270fSAndriy Gapon static void
shutdown_resettodr(void * arg __unused,int howto __unused)10025c7e270fSAndriy Gapon shutdown_resettodr(void *arg __unused, int howto __unused)
10035c7e270fSAndriy Gapon {
10045c7e270fSAndriy Gapon
10055c7e270fSAndriy Gapon callout_drain(&resettodr_callout);
1006364c516cSKonstantin Belousov /* Another unlocked read of time_status */
1007364c516cSKonstantin Belousov if (resettodr_period > 0 && !ntp_is_time_error(time_status))
10085c7e270fSAndriy Gapon resettodr();
10095c7e270fSAndriy Gapon }
10105c7e270fSAndriy Gapon
10115c7e270fSAndriy Gapon static int
sysctl_resettodr_period(SYSCTL_HANDLER_ARGS)10125c7e270fSAndriy Gapon sysctl_resettodr_period(SYSCTL_HANDLER_ARGS)
10135c7e270fSAndriy Gapon {
10145c7e270fSAndriy Gapon int error;
10155c7e270fSAndriy Gapon
10165c7e270fSAndriy Gapon error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
10175c7e270fSAndriy Gapon if (error || !req->newptr)
10185c7e270fSAndriy Gapon return (error);
1019af3b2549SHans Petter Selasky if (cold)
1020af3b2549SHans Petter Selasky goto done;
10215c7e270fSAndriy Gapon if (resettodr_period == 0)
10225c7e270fSAndriy Gapon callout_stop(&resettodr_callout);
10235c7e270fSAndriy Gapon else
10245c7e270fSAndriy Gapon callout_reset(&resettodr_callout, resettodr_period * hz,
10255c7e270fSAndriy Gapon periodic_resettodr, NULL);
1026af3b2549SHans Petter Selasky done:
10275c7e270fSAndriy Gapon return (0);
10285c7e270fSAndriy Gapon }
10295c7e270fSAndriy Gapon
1030364c516cSKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, rtc_save_period, CTLTYPE_INT | CTLFLAG_RWTUN |
1031364c516cSKonstantin Belousov CTLFLAG_MPSAFE, &resettodr_period, 1800, sysctl_resettodr_period, "I",
10325c7e270fSAndriy Gapon "Save system time to RTC with this period (in seconds)");
10335c7e270fSAndriy Gapon
10345c7e270fSAndriy Gapon static void
start_periodic_resettodr(void * arg __unused)10355c7e270fSAndriy Gapon start_periodic_resettodr(void *arg __unused)
10365c7e270fSAndriy Gapon {
10375c7e270fSAndriy Gapon
10385c7e270fSAndriy Gapon EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_resettodr, NULL,
10395c7e270fSAndriy Gapon SHUTDOWN_PRI_FIRST);
10405c7e270fSAndriy Gapon callout_init(&resettodr_callout, 1);
10415c7e270fSAndriy Gapon if (resettodr_period == 0)
10425c7e270fSAndriy Gapon return;
10435c7e270fSAndriy Gapon callout_reset(&resettodr_callout, resettodr_period * hz,
10445c7e270fSAndriy Gapon periodic_resettodr, NULL);
10455c7e270fSAndriy Gapon }
10465c7e270fSAndriy Gapon
1047785797c3SAndriy Gapon SYSINIT(periodic_resettodr, SI_SUB_LAST, SI_ORDER_MIDDLE,
10485c7e270fSAndriy Gapon start_periodic_resettodr, NULL);
1049