kern_ntptime.c revision 1.28.4.7 1 1.28.4.7 yamt /* $NetBSD: kern_ntptime.c,v 1.28.4.7 2008/03/17 09:15:33 yamt Exp $ */
2 1.28.4.1 yamt
3 1.28.4.1 yamt /*-
4 1.28.4.1 yamt ***********************************************************************
5 1.28.4.1 yamt * *
6 1.28.4.1 yamt * Copyright (c) David L. Mills 1993-2001 *
7 1.28.4.1 yamt * *
8 1.28.4.1 yamt * Permission to use, copy, modify, and distribute this software and *
9 1.28.4.1 yamt * its documentation for any purpose and without fee is hereby *
10 1.28.4.1 yamt * granted, provided that the above copyright notice appears in all *
11 1.28.4.1 yamt * copies and that both the copyright notice and this permission *
12 1.28.4.1 yamt * notice appear in supporting documentation, and that the name *
13 1.28.4.1 yamt * University of Delaware not be used in advertising or publicity *
14 1.28.4.1 yamt * pertaining to distribution of the software without specific, *
15 1.28.4.1 yamt * written prior permission. The University of Delaware makes no *
16 1.28.4.1 yamt * representations about the suitability this software for any *
17 1.28.4.1 yamt * purpose. It is provided "as is" without express or implied *
18 1.28.4.1 yamt * warranty. *
19 1.28.4.1 yamt * *
20 1.28.4.1 yamt **********************************************************************/
21 1.1 jonathan
22 1.28.4.1 yamt /*
23 1.28.4.1 yamt * Adapted from the original sources for FreeBSD and timecounters by:
24 1.28.4.1 yamt * Poul-Henning Kamp <phk (at) FreeBSD.org>.
25 1.28.4.1 yamt *
26 1.28.4.1 yamt * The 32bit version of the "LP" macros seems a bit past its "sell by"
27 1.28.4.1 yamt * date so I have retained only the 64bit version and included it directly
28 1.28.4.1 yamt * in this file.
29 1.28.4.1 yamt *
30 1.28.4.1 yamt * Only minor changes done to interface with the timecounters over in
31 1.28.4.1 yamt * sys/kern/kern_clock.c. Some of the comments below may be (even more)
32 1.28.4.1 yamt * confusing and/or plain wrong in that context.
33 1.28.4.1 yamt */
34 1.28.4.1 yamt
35 1.28.4.1 yamt #include <sys/cdefs.h>
36 1.28.4.1 yamt /* __FBSDID("$FreeBSD: src/sys/kern/kern_ntptime.c,v 1.59 2005/05/28 14:34:41 rwatson Exp $"); */
37 1.28.4.7 yamt __KERNEL_RCSID(0, "$NetBSD: kern_ntptime.c,v 1.28.4.7 2008/03/17 09:15:33 yamt Exp $");
38 1.28.4.1 yamt
39 1.28.4.1 yamt #include "opt_ntp.h"
40 1.28.4.1 yamt #include "opt_compat_netbsd.h"
41 1.28.4.1 yamt
42 1.28.4.1 yamt #include <sys/param.h>
43 1.28.4.1 yamt #include <sys/resourcevar.h>
44 1.28.4.1 yamt #include <sys/systm.h>
45 1.28.4.1 yamt #include <sys/kernel.h>
46 1.28.4.1 yamt #include <sys/proc.h>
47 1.28.4.1 yamt #include <sys/sysctl.h>
48 1.28.4.1 yamt #include <sys/timex.h>
49 1.28.4.1 yamt #ifdef COMPAT_30
50 1.28.4.1 yamt #include <compat/sys/timex.h>
51 1.28.4.1 yamt #endif
52 1.28.4.1 yamt #include <sys/vnode.h>
53 1.28.4.1 yamt #include <sys/kauth.h>
54 1.28.4.1 yamt
55 1.28.4.1 yamt #include <sys/mount.h>
56 1.28.4.1 yamt #include <sys/syscallargs.h>
57 1.28.4.1 yamt
58 1.28.4.5 yamt #include <sys/cpu.h>
59 1.28.4.1 yamt
60 1.28.4.1 yamt /*
61 1.28.4.1 yamt * Single-precision macros for 64-bit machines
62 1.28.4.1 yamt */
63 1.28.4.1 yamt typedef int64_t l_fp;
64 1.28.4.1 yamt #define L_ADD(v, u) ((v) += (u))
65 1.28.4.1 yamt #define L_SUB(v, u) ((v) -= (u))
66 1.28.4.1 yamt #define L_ADDHI(v, a) ((v) += (int64_t)(a) << 32)
67 1.28.4.1 yamt #define L_NEG(v) ((v) = -(v))
68 1.28.4.1 yamt #define L_RSHIFT(v, n) \
69 1.28.4.1 yamt do { \
70 1.28.4.1 yamt if ((v) < 0) \
71 1.28.4.1 yamt (v) = -(-(v) >> (n)); \
72 1.28.4.1 yamt else \
73 1.28.4.1 yamt (v) = (v) >> (n); \
74 1.28.4.1 yamt } while (0)
75 1.28.4.1 yamt #define L_MPY(v, a) ((v) *= (a))
76 1.28.4.1 yamt #define L_CLR(v) ((v) = 0)
77 1.28.4.1 yamt #define L_ISNEG(v) ((v) < 0)
78 1.28.4.1 yamt #define L_LINT(v, a) ((v) = (int64_t)(a) << 32)
79 1.28.4.1 yamt #define L_GINT(v) ((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
80 1.28.4.1 yamt
81 1.28.4.1 yamt #ifdef NTP
82 1.28.4.1 yamt /*
83 1.28.4.1 yamt * Generic NTP kernel interface
84 1.28.4.1 yamt *
85 1.28.4.1 yamt * These routines constitute the Network Time Protocol (NTP) interfaces
86 1.28.4.1 yamt * for user and daemon application programs. The ntp_gettime() routine
87 1.28.4.1 yamt * provides the time, maximum error (synch distance) and estimated error
88 1.28.4.1 yamt * (dispersion) to client user application programs. The ntp_adjtime()
89 1.28.4.1 yamt * routine is used by the NTP daemon to adjust the system clock to an
90 1.28.4.1 yamt * externally derived time. The time offset and related variables set by
91 1.28.4.1 yamt * this routine are used by other routines in this module to adjust the
92 1.28.4.1 yamt * phase and frequency of the clock discipline loop which controls the
93 1.28.4.1 yamt * system clock.
94 1.28.4.1 yamt *
95 1.28.4.1 yamt * When the kernel time is reckoned directly in nanoseconds (NTP_NANO
96 1.28.4.1 yamt * defined), the time at each tick interrupt is derived directly from
97 1.28.4.1 yamt * the kernel time variable. When the kernel time is reckoned in
98 1.28.4.1 yamt * microseconds, (NTP_NANO undefined), the time is derived from the
99 1.28.4.1 yamt * kernel time variable together with a variable representing the
100 1.28.4.1 yamt * leftover nanoseconds at the last tick interrupt. In either case, the
101 1.28.4.1 yamt * current nanosecond time is reckoned from these values plus an
102 1.28.4.1 yamt * interpolated value derived by the clock routines in another
103 1.28.4.1 yamt * architecture-specific module. The interpolation can use either a
104 1.28.4.1 yamt * dedicated counter or a processor cycle counter (PCC) implemented in
105 1.28.4.1 yamt * some architectures.
106 1.28.4.1 yamt *
107 1.28.4.1 yamt * Note that all routines must run at priority splclock or higher.
108 1.28.4.1 yamt */
109 1.28.4.1 yamt /*
110 1.28.4.1 yamt * Phase/frequency-lock loop (PLL/FLL) definitions
111 1.28.4.1 yamt *
112 1.28.4.1 yamt * The nanosecond clock discipline uses two variable types, time
113 1.28.4.1 yamt * variables and frequency variables. Both types are represented as 64-
114 1.28.4.1 yamt * bit fixed-point quantities with the decimal point between two 32-bit
115 1.28.4.1 yamt * halves. On a 32-bit machine, each half is represented as a single
116 1.28.4.1 yamt * word and mathematical operations are done using multiple-precision
117 1.28.4.1 yamt * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
118 1.28.4.1 yamt * used.
119 1.28.4.1 yamt *
120 1.28.4.1 yamt * A time variable is a signed 64-bit fixed-point number in ns and
121 1.28.4.1 yamt * fraction. It represents the remaining time offset to be amortized
122 1.28.4.1 yamt * over succeeding tick interrupts. The maximum time offset is about
123 1.28.4.1 yamt * 0.5 s and the resolution is about 2.3e-10 ns.
124 1.28.4.1 yamt *
125 1.28.4.1 yamt * 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
126 1.28.4.1 yamt * 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
127 1.28.4.1 yamt * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128 1.28.4.1 yamt * |s s s| ns |
129 1.28.4.1 yamt * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 1.28.4.1 yamt * | fraction |
131 1.28.4.1 yamt * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132 1.28.4.1 yamt *
133 1.28.4.1 yamt * A frequency variable is a signed 64-bit fixed-point number in ns/s
134 1.28.4.1 yamt * and fraction. It represents the ns and fraction to be added to the
135 1.28.4.1 yamt * kernel time variable at each second. The maximum frequency offset is
136 1.28.4.1 yamt * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
137 1.28.4.1 yamt *
138 1.28.4.1 yamt * 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
139 1.28.4.1 yamt * 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
140 1.28.4.1 yamt * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 1.28.4.1 yamt * |s s s s s s s s s s s s s| ns/s |
142 1.28.4.1 yamt * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 1.28.4.1 yamt * | fraction |
144 1.28.4.1 yamt * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 1.28.4.1 yamt */
146 1.28.4.1 yamt /*
147 1.28.4.1 yamt * The following variables establish the state of the PLL/FLL and the
148 1.28.4.1 yamt * residual time and frequency offset of the local clock.
149 1.28.4.1 yamt */
150 1.28.4.1 yamt #define SHIFT_PLL 4 /* PLL loop gain (shift) */
151 1.28.4.1 yamt #define SHIFT_FLL 2 /* FLL loop gain (shift) */
152 1.28.4.1 yamt
153 1.28.4.1 yamt static int time_state = TIME_OK; /* clock state */
154 1.28.4.1 yamt static int time_status = STA_UNSYNC; /* clock status bits */
155 1.28.4.1 yamt static long time_tai; /* TAI offset (s) */
156 1.28.4.1 yamt static long time_monitor; /* last time offset scaled (ns) */
157 1.28.4.1 yamt static long time_constant; /* poll interval (shift) (s) */
158 1.28.4.1 yamt static long time_precision = 1; /* clock precision (ns) */
159 1.28.4.1 yamt static long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */
160 1.28.4.1 yamt static long time_esterror = MAXPHASE / 1000; /* estimated error (us) */
161 1.28.4.1 yamt static long time_reftime; /* time at last adjustment (s) */
162 1.28.4.1 yamt static l_fp time_offset; /* time offset (ns) */
163 1.28.4.1 yamt static l_fp time_freq; /* frequency offset (ns/s) */
164 1.28.4.1 yamt #endif /* NTP */
165 1.28.4.1 yamt
166 1.28.4.1 yamt static l_fp time_adj; /* tick adjust (ns/s) */
167 1.28.4.1 yamt int64_t time_adjtime; /* correction from adjtime(2) (usec) */
168 1.28.4.1 yamt
169 1.28.4.1 yamt extern int time_adjusted; /* ntp might have changed the system time */
170 1.28.4.1 yamt
171 1.28.4.1 yamt #ifdef NTP
172 1.28.4.1 yamt #ifdef PPS_SYNC
173 1.28.4.1 yamt /*
174 1.28.4.1 yamt * The following variables are used when a pulse-per-second (PPS) signal
175 1.28.4.1 yamt * is available and connected via a modem control lead. They establish
176 1.28.4.1 yamt * the engineering parameters of the clock discipline loop when
177 1.28.4.1 yamt * controlled by the PPS signal.
178 1.28.4.1 yamt */
179 1.28.4.1 yamt #define PPS_FAVG 2 /* min freq avg interval (s) (shift) */
180 1.28.4.1 yamt #define PPS_FAVGDEF 8 /* default freq avg int (s) (shift) */
181 1.28.4.1 yamt #define PPS_FAVGMAX 15 /* max freq avg interval (s) (shift) */
182 1.28.4.1 yamt #define PPS_PAVG 4 /* phase avg interval (s) (shift) */
183 1.28.4.1 yamt #define PPS_VALID 120 /* PPS signal watchdog max (s) */
184 1.28.4.1 yamt #define PPS_MAXWANDER 100000 /* max PPS wander (ns/s) */
185 1.28.4.1 yamt #define PPS_POPCORN 2 /* popcorn spike threshold (shift) */
186 1.28.4.1 yamt
187 1.28.4.1 yamt static struct timespec pps_tf[3]; /* phase median filter */
188 1.28.4.1 yamt static l_fp pps_freq; /* scaled frequency offset (ns/s) */
189 1.28.4.1 yamt static long pps_fcount; /* frequency accumulator */
190 1.28.4.1 yamt static long pps_jitter; /* nominal jitter (ns) */
191 1.28.4.1 yamt static long pps_stabil; /* nominal stability (scaled ns/s) */
192 1.28.4.1 yamt static long pps_lastsec; /* time at last calibration (s) */
193 1.28.4.1 yamt static int pps_valid; /* signal watchdog counter */
194 1.28.4.1 yamt static int pps_shift = PPS_FAVG; /* interval duration (s) (shift) */
195 1.28.4.1 yamt static int pps_shiftmax = PPS_FAVGDEF; /* max interval duration (s) (shift) */
196 1.28.4.1 yamt static int pps_intcnt; /* wander counter */
197 1.28.4.1 yamt
198 1.28.4.1 yamt /*
199 1.28.4.1 yamt * PPS signal quality monitors
200 1.28.4.1 yamt */
201 1.28.4.1 yamt static long pps_calcnt; /* calibration intervals */
202 1.28.4.1 yamt static long pps_jitcnt; /* jitter limit exceeded */
203 1.28.4.1 yamt static long pps_stbcnt; /* stability limit exceeded */
204 1.28.4.1 yamt static long pps_errcnt; /* calibration errors */
205 1.28.4.1 yamt #endif /* PPS_SYNC */
206 1.28.4.1 yamt /*
207 1.28.4.1 yamt * End of phase/frequency-lock loop (PLL/FLL) definitions
208 1.28.4.1 yamt */
209 1.28.4.1 yamt
210 1.28.4.1 yamt static void hardupdate(long offset);
211 1.28.4.1 yamt
212 1.28.4.1 yamt /*
213 1.28.4.1 yamt * ntp_gettime() - NTP user application interface
214 1.28.4.1 yamt */
215 1.28.4.1 yamt void
216 1.28.4.6 yamt ntp_gettime(struct ntptimeval *ntv)
217 1.28.4.1 yamt {
218 1.28.4.1 yamt nanotime(&ntv->time);
219 1.28.4.1 yamt ntv->maxerror = time_maxerror;
220 1.28.4.1 yamt ntv->esterror = time_esterror;
221 1.28.4.1 yamt ntv->tai = time_tai;
222 1.28.4.1 yamt ntv->time_state = time_state;
223 1.28.4.1 yamt }
224 1.28.4.1 yamt
225 1.28.4.1 yamt /* ARGSUSED */
226 1.28.4.1 yamt /*
227 1.28.4.1 yamt * ntp_adjtime() - NTP daemon application interface
228 1.28.4.1 yamt */
229 1.28.4.1 yamt int
230 1.28.4.6 yamt sys_ntp_adjtime(struct lwp *l, const struct sys_ntp_adjtime_args *uap, register_t *retval)
231 1.28.4.1 yamt {
232 1.28.4.6 yamt /* {
233 1.28.4.1 yamt syscallarg(struct timex *) tp;
234 1.28.4.6 yamt } */
235 1.28.4.1 yamt struct timex ntv;
236 1.28.4.1 yamt int error = 0;
237 1.28.4.1 yamt
238 1.28.4.4 yamt error = copyin((void *)SCARG(uap, tp), (void *)&ntv, sizeof(ntv));
239 1.28.4.2 yamt if (error != 0)
240 1.28.4.1 yamt return (error);
241 1.28.4.1 yamt
242 1.28.4.2 yamt if (ntv.modes != 0 && (error = kauth_authorize_system(l->l_cred,
243 1.28.4.2 yamt KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_NTPADJTIME, NULL,
244 1.28.4.2 yamt NULL, NULL)) != 0)
245 1.28.4.1 yamt return (error);
246 1.28.4.1 yamt
247 1.28.4.1 yamt ntp_adjtime1(&ntv);
248 1.28.4.1 yamt
249 1.28.4.4 yamt error = copyout((void *)&ntv, (void *)SCARG(uap, tp), sizeof(ntv));
250 1.28.4.2 yamt if (!error)
251 1.28.4.1 yamt *retval = ntp_timestatus();
252 1.28.4.2 yamt
253 1.28.4.1 yamt return error;
254 1.28.4.1 yamt }
255 1.28.4.1 yamt
256 1.28.4.1 yamt void
257 1.28.4.6 yamt ntp_adjtime1(struct timex *ntv)
258 1.28.4.1 yamt {
259 1.28.4.1 yamt long freq;
260 1.28.4.1 yamt int modes;
261 1.28.4.1 yamt int s;
262 1.28.4.1 yamt
263 1.28.4.1 yamt /*
264 1.28.4.1 yamt * Update selected clock variables - only the superuser can
265 1.28.4.1 yamt * change anything. Note that there is no error checking here on
266 1.28.4.1 yamt * the assumption the superuser should know what it is doing.
267 1.28.4.1 yamt * Note that either the time constant or TAI offset are loaded
268 1.28.4.1 yamt * from the ntv.constant member, depending on the mode bits. If
269 1.28.4.1 yamt * the STA_PLL bit in the status word is cleared, the state and
270 1.28.4.1 yamt * status words are reset to the initial values at boot.
271 1.28.4.1 yamt */
272 1.28.4.1 yamt modes = ntv->modes;
273 1.28.4.1 yamt if (modes != 0)
274 1.28.4.1 yamt /* We need to save the system time during shutdown */
275 1.28.4.1 yamt time_adjusted |= 2;
276 1.28.4.1 yamt s = splclock();
277 1.28.4.1 yamt if (modes & MOD_MAXERROR)
278 1.28.4.1 yamt time_maxerror = ntv->maxerror;
279 1.28.4.1 yamt if (modes & MOD_ESTERROR)
280 1.28.4.1 yamt time_esterror = ntv->esterror;
281 1.28.4.1 yamt if (modes & MOD_STATUS) {
282 1.28.4.1 yamt if (time_status & STA_PLL && !(ntv->status & STA_PLL)) {
283 1.28.4.1 yamt time_state = TIME_OK;
284 1.28.4.1 yamt time_status = STA_UNSYNC;
285 1.28.4.1 yamt #ifdef PPS_SYNC
286 1.28.4.1 yamt pps_shift = PPS_FAVG;
287 1.28.4.1 yamt #endif /* PPS_SYNC */
288 1.28.4.1 yamt }
289 1.28.4.1 yamt time_status &= STA_RONLY;
290 1.28.4.1 yamt time_status |= ntv->status & ~STA_RONLY;
291 1.28.4.1 yamt }
292 1.28.4.1 yamt if (modes & MOD_TIMECONST) {
293 1.28.4.1 yamt if (ntv->constant < 0)
294 1.28.4.1 yamt time_constant = 0;
295 1.28.4.1 yamt else if (ntv->constant > MAXTC)
296 1.28.4.1 yamt time_constant = MAXTC;
297 1.28.4.1 yamt else
298 1.28.4.1 yamt time_constant = ntv->constant;
299 1.28.4.1 yamt }
300 1.28.4.1 yamt if (modes & MOD_TAI) {
301 1.28.4.1 yamt if (ntv->constant > 0) /* XXX zero & negative numbers ? */
302 1.28.4.1 yamt time_tai = ntv->constant;
303 1.28.4.1 yamt }
304 1.28.4.1 yamt #ifdef PPS_SYNC
305 1.28.4.1 yamt if (modes & MOD_PPSMAX) {
306 1.28.4.1 yamt if (ntv->shift < PPS_FAVG)
307 1.28.4.1 yamt pps_shiftmax = PPS_FAVG;
308 1.28.4.1 yamt else if (ntv->shift > PPS_FAVGMAX)
309 1.28.4.1 yamt pps_shiftmax = PPS_FAVGMAX;
310 1.28.4.1 yamt else
311 1.28.4.1 yamt pps_shiftmax = ntv->shift;
312 1.28.4.1 yamt }
313 1.28.4.1 yamt #endif /* PPS_SYNC */
314 1.28.4.1 yamt if (modes & MOD_NANO)
315 1.28.4.1 yamt time_status |= STA_NANO;
316 1.28.4.1 yamt if (modes & MOD_MICRO)
317 1.28.4.1 yamt time_status &= ~STA_NANO;
318 1.28.4.1 yamt if (modes & MOD_CLKB)
319 1.28.4.1 yamt time_status |= STA_CLK;
320 1.28.4.1 yamt if (modes & MOD_CLKA)
321 1.28.4.1 yamt time_status &= ~STA_CLK;
322 1.28.4.1 yamt if (modes & MOD_FREQUENCY) {
323 1.28.4.1 yamt freq = (ntv->freq * 1000LL) >> 16;
324 1.28.4.1 yamt if (freq > MAXFREQ)
325 1.28.4.1 yamt L_LINT(time_freq, MAXFREQ);
326 1.28.4.1 yamt else if (freq < -MAXFREQ)
327 1.28.4.1 yamt L_LINT(time_freq, -MAXFREQ);
328 1.28.4.1 yamt else {
329 1.28.4.1 yamt /*
330 1.28.4.1 yamt * ntv.freq is [PPM * 2^16] = [us/s * 2^16]
331 1.28.4.1 yamt * time_freq is [ns/s * 2^32]
332 1.28.4.1 yamt */
333 1.28.4.1 yamt time_freq = ntv->freq * 1000LL * 65536LL;
334 1.28.4.1 yamt }
335 1.28.4.1 yamt #ifdef PPS_SYNC
336 1.28.4.1 yamt pps_freq = time_freq;
337 1.28.4.1 yamt #endif /* PPS_SYNC */
338 1.28.4.1 yamt }
339 1.28.4.1 yamt if (modes & MOD_OFFSET) {
340 1.28.4.1 yamt if (time_status & STA_NANO)
341 1.28.4.1 yamt hardupdate(ntv->offset);
342 1.28.4.1 yamt else
343 1.28.4.1 yamt hardupdate(ntv->offset * 1000);
344 1.28.4.1 yamt }
345 1.28.4.1 yamt
346 1.28.4.1 yamt /*
347 1.28.4.1 yamt * Retrieve all clock variables. Note that the TAI offset is
348 1.28.4.1 yamt * returned only by ntp_gettime();
349 1.28.4.1 yamt */
350 1.28.4.1 yamt if (time_status & STA_NANO)
351 1.28.4.1 yamt ntv->offset = L_GINT(time_offset);
352 1.28.4.1 yamt else
353 1.28.4.1 yamt ntv->offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */
354 1.28.4.1 yamt ntv->freq = L_GINT((time_freq / 1000LL) << 16);
355 1.28.4.1 yamt ntv->maxerror = time_maxerror;
356 1.28.4.1 yamt ntv->esterror = time_esterror;
357 1.28.4.1 yamt ntv->status = time_status;
358 1.28.4.1 yamt ntv->constant = time_constant;
359 1.28.4.1 yamt if (time_status & STA_NANO)
360 1.28.4.1 yamt ntv->precision = time_precision;
361 1.28.4.1 yamt else
362 1.28.4.1 yamt ntv->precision = time_precision / 1000;
363 1.28.4.1 yamt ntv->tolerance = MAXFREQ * SCALE_PPM;
364 1.28.4.1 yamt #ifdef PPS_SYNC
365 1.28.4.1 yamt ntv->shift = pps_shift;
366 1.28.4.1 yamt ntv->ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
367 1.28.4.1 yamt if (time_status & STA_NANO)
368 1.28.4.1 yamt ntv->jitter = pps_jitter;
369 1.28.4.1 yamt else
370 1.28.4.1 yamt ntv->jitter = pps_jitter / 1000;
371 1.28.4.1 yamt ntv->stabil = pps_stabil;
372 1.28.4.1 yamt ntv->calcnt = pps_calcnt;
373 1.28.4.1 yamt ntv->errcnt = pps_errcnt;
374 1.28.4.1 yamt ntv->jitcnt = pps_jitcnt;
375 1.28.4.1 yamt ntv->stbcnt = pps_stbcnt;
376 1.28.4.1 yamt #endif /* PPS_SYNC */
377 1.28.4.1 yamt splx(s);
378 1.28.4.1 yamt }
379 1.28.4.1 yamt #endif /* NTP */
380 1.28.4.1 yamt
381 1.28.4.1 yamt /*
382 1.28.4.1 yamt * second_overflow() - called after ntp_tick_adjust()
383 1.28.4.1 yamt *
384 1.28.4.1 yamt * This routine is ordinarily called immediately following the above
385 1.28.4.1 yamt * routine ntp_tick_adjust(). While these two routines are normally
386 1.28.4.1 yamt * combined, they are separated here only for the purposes of
387 1.28.4.1 yamt * simulation.
388 1.28.4.1 yamt */
389 1.28.4.1 yamt void
390 1.28.4.1 yamt ntp_update_second(int64_t *adjustment, time_t *newsec)
391 1.28.4.1 yamt {
392 1.28.4.1 yamt int tickrate;
393 1.28.4.1 yamt l_fp ftemp; /* 32/64-bit temporary */
394 1.28.4.1 yamt
395 1.28.4.1 yamt #ifdef NTP
396 1.28.4.1 yamt
397 1.28.4.1 yamt /*
398 1.28.4.1 yamt * On rollover of the second both the nanosecond and microsecond
399 1.28.4.1 yamt * clocks are updated and the state machine cranked as
400 1.28.4.1 yamt * necessary. The phase adjustment to be used for the next
401 1.28.4.1 yamt * second is calculated and the maximum error is increased by
402 1.28.4.1 yamt * the tolerance.
403 1.28.4.1 yamt */
404 1.28.4.1 yamt time_maxerror += MAXFREQ / 1000;
405 1.28.4.1 yamt
406 1.28.4.1 yamt /*
407 1.28.4.1 yamt * Leap second processing. If in leap-insert state at
408 1.28.4.1 yamt * the end of the day, the system clock is set back one
409 1.28.4.1 yamt * second; if in leap-delete state, the system clock is
410 1.28.4.1 yamt * set ahead one second. The nano_time() routine or
411 1.28.4.1 yamt * external clock driver will insure that reported time
412 1.28.4.1 yamt * is always monotonic.
413 1.28.4.1 yamt */
414 1.28.4.1 yamt switch (time_state) {
415 1.28.4.1 yamt
416 1.28.4.1 yamt /*
417 1.28.4.1 yamt * No warning.
418 1.28.4.1 yamt */
419 1.28.4.1 yamt case TIME_OK:
420 1.28.4.1 yamt if (time_status & STA_INS)
421 1.28.4.1 yamt time_state = TIME_INS;
422 1.28.4.1 yamt else if (time_status & STA_DEL)
423 1.28.4.1 yamt time_state = TIME_DEL;
424 1.28.4.1 yamt break;
425 1.28.4.1 yamt
426 1.28.4.1 yamt /*
427 1.28.4.1 yamt * Insert second 23:59:60 following second
428 1.28.4.1 yamt * 23:59:59.
429 1.28.4.1 yamt */
430 1.28.4.1 yamt case TIME_INS:
431 1.28.4.1 yamt if (!(time_status & STA_INS))
432 1.28.4.1 yamt time_state = TIME_OK;
433 1.28.4.1 yamt else if ((*newsec) % 86400 == 0) {
434 1.28.4.1 yamt (*newsec)--;
435 1.28.4.1 yamt time_state = TIME_OOP;
436 1.28.4.1 yamt time_tai++;
437 1.28.4.1 yamt }
438 1.28.4.1 yamt break;
439 1.28.4.1 yamt
440 1.28.4.1 yamt /*
441 1.28.4.1 yamt * Delete second 23:59:59.
442 1.28.4.1 yamt */
443 1.28.4.1 yamt case TIME_DEL:
444 1.28.4.1 yamt if (!(time_status & STA_DEL))
445 1.28.4.1 yamt time_state = TIME_OK;
446 1.28.4.1 yamt else if (((*newsec) + 1) % 86400 == 0) {
447 1.28.4.1 yamt (*newsec)++;
448 1.28.4.1 yamt time_tai--;
449 1.28.4.1 yamt time_state = TIME_WAIT;
450 1.28.4.1 yamt }
451 1.28.4.1 yamt break;
452 1.28.4.1 yamt
453 1.28.4.1 yamt /*
454 1.28.4.1 yamt * Insert second in progress.
455 1.28.4.1 yamt */
456 1.28.4.1 yamt case TIME_OOP:
457 1.28.4.1 yamt time_state = TIME_WAIT;
458 1.28.4.1 yamt break;
459 1.28.4.1 yamt
460 1.28.4.1 yamt /*
461 1.28.4.1 yamt * Wait for status bits to clear.
462 1.28.4.1 yamt */
463 1.28.4.1 yamt case TIME_WAIT:
464 1.28.4.1 yamt if (!(time_status & (STA_INS | STA_DEL)))
465 1.28.4.1 yamt time_state = TIME_OK;
466 1.28.4.1 yamt }
467 1.28.4.1 yamt
468 1.28.4.1 yamt /*
469 1.28.4.1 yamt * Compute the total time adjustment for the next second
470 1.28.4.1 yamt * in ns. The offset is reduced by a factor depending on
471 1.28.4.1 yamt * whether the PPS signal is operating. Note that the
472 1.28.4.1 yamt * value is in effect scaled by the clock frequency,
473 1.28.4.1 yamt * since the adjustment is added at each tick interrupt.
474 1.28.4.1 yamt */
475 1.28.4.1 yamt ftemp = time_offset;
476 1.28.4.1 yamt #ifdef PPS_SYNC
477 1.28.4.1 yamt /* XXX even if PPS signal dies we should finish adjustment ? */
478 1.28.4.1 yamt if (time_status & STA_PPSTIME && time_status &
479 1.28.4.1 yamt STA_PPSSIGNAL)
480 1.28.4.1 yamt L_RSHIFT(ftemp, pps_shift);
481 1.28.4.1 yamt else
482 1.28.4.1 yamt L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
483 1.28.4.1 yamt #else
484 1.28.4.1 yamt L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
485 1.28.4.1 yamt #endif /* PPS_SYNC */
486 1.28.4.1 yamt time_adj = ftemp;
487 1.28.4.1 yamt L_SUB(time_offset, ftemp);
488 1.28.4.1 yamt L_ADD(time_adj, time_freq);
489 1.28.4.1 yamt
490 1.28.4.1 yamt #ifdef PPS_SYNC
491 1.28.4.1 yamt if (pps_valid > 0)
492 1.28.4.1 yamt pps_valid--;
493 1.28.4.1 yamt else
494 1.28.4.1 yamt time_status &= ~STA_PPSSIGNAL;
495 1.28.4.1 yamt #endif /* PPS_SYNC */
496 1.28.4.2 yamt #else /* !NTP */
497 1.28.4.2 yamt L_CLR(time_adj);
498 1.28.4.2 yamt #endif /* !NTP */
499 1.28.4.1 yamt
500 1.28.4.1 yamt /*
501 1.28.4.1 yamt * Apply any correction from adjtime(2). If more than one second
502 1.28.4.1 yamt * off we slew at a rate of 5ms/s (5000 PPM) else 500us/s (500PPM)
503 1.28.4.1 yamt * until the last second is slewed the final < 500 usecs.
504 1.28.4.1 yamt */
505 1.28.4.1 yamt if (time_adjtime != 0) {
506 1.28.4.1 yamt if (time_adjtime > 1000000)
507 1.28.4.1 yamt tickrate = 5000;
508 1.28.4.1 yamt else if (time_adjtime < -1000000)
509 1.28.4.1 yamt tickrate = -5000;
510 1.28.4.1 yamt else if (time_adjtime > 500)
511 1.28.4.1 yamt tickrate = 500;
512 1.28.4.1 yamt else if (time_adjtime < -500)
513 1.28.4.1 yamt tickrate = -500;
514 1.28.4.1 yamt else
515 1.28.4.1 yamt tickrate = time_adjtime;
516 1.28.4.1 yamt time_adjtime -= tickrate;
517 1.28.4.1 yamt L_LINT(ftemp, tickrate * 1000);
518 1.28.4.1 yamt L_ADD(time_adj, ftemp);
519 1.28.4.1 yamt }
520 1.28.4.1 yamt *adjustment = time_adj;
521 1.28.4.1 yamt }
522 1.28.4.1 yamt
523 1.28.4.1 yamt /*
524 1.28.4.1 yamt * ntp_init() - initialize variables and structures
525 1.28.4.1 yamt *
526 1.28.4.1 yamt * This routine must be called after the kernel variables hz and tick
527 1.28.4.1 yamt * are set or changed and before the next tick interrupt. In this
528 1.28.4.1 yamt * particular implementation, these values are assumed set elsewhere in
529 1.28.4.1 yamt * the kernel. The design allows the clock frequency and tick interval
530 1.28.4.1 yamt * to be changed while the system is running. So, this routine should
531 1.28.4.1 yamt * probably be integrated with the code that does that.
532 1.28.4.1 yamt */
533 1.28.4.1 yamt void
534 1.28.4.1 yamt ntp_init(void)
535 1.28.4.1 yamt {
536 1.28.4.1 yamt
537 1.28.4.1 yamt /*
538 1.28.4.1 yamt * The following variables are initialized only at startup. Only
539 1.28.4.1 yamt * those structures not cleared by the compiler need to be
540 1.28.4.1 yamt * initialized, and these only in the simulator. In the actual
541 1.28.4.1 yamt * kernel, any nonzero values here will quickly evaporate.
542 1.28.4.1 yamt */
543 1.28.4.1 yamt L_CLR(time_adj);
544 1.28.4.1 yamt #ifdef NTP
545 1.28.4.1 yamt L_CLR(time_offset);
546 1.28.4.1 yamt L_CLR(time_freq);
547 1.28.4.1 yamt #ifdef PPS_SYNC
548 1.28.4.1 yamt pps_tf[0].tv_sec = pps_tf[0].tv_nsec = 0;
549 1.28.4.1 yamt pps_tf[1].tv_sec = pps_tf[1].tv_nsec = 0;
550 1.28.4.1 yamt pps_tf[2].tv_sec = pps_tf[2].tv_nsec = 0;
551 1.28.4.1 yamt pps_fcount = 0;
552 1.28.4.1 yamt L_CLR(pps_freq);
553 1.28.4.1 yamt #endif /* PPS_SYNC */
554 1.28.4.1 yamt #endif
555 1.28.4.1 yamt }
556 1.28.4.1 yamt
557 1.28.4.1 yamt #ifdef NTP
558 1.28.4.1 yamt /*
559 1.28.4.1 yamt * hardupdate() - local clock update
560 1.28.4.1 yamt *
561 1.28.4.1 yamt * This routine is called by ntp_adjtime() to update the local clock
562 1.28.4.1 yamt * phase and frequency. The implementation is of an adaptive-parameter,
563 1.28.4.1 yamt * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
564 1.28.4.1 yamt * time and frequency offset estimates for each call. If the kernel PPS
565 1.28.4.1 yamt * discipline code is configured (PPS_SYNC), the PPS signal itself
566 1.28.4.1 yamt * determines the new time offset, instead of the calling argument.
567 1.28.4.1 yamt * Presumably, calls to ntp_adjtime() occur only when the caller
568 1.28.4.1 yamt * believes the local clock is valid within some bound (+-128 ms with
569 1.28.4.1 yamt * NTP). If the caller's time is far different than the PPS time, an
570 1.28.4.1 yamt * argument will ensue, and it's not clear who will lose.
571 1.28.4.1 yamt *
572 1.28.4.1 yamt * For uncompensated quartz crystal oscillators and nominal update
573 1.28.4.1 yamt * intervals less than 256 s, operation should be in phase-lock mode,
574 1.28.4.1 yamt * where the loop is disciplined to phase. For update intervals greater
575 1.28.4.1 yamt * than 1024 s, operation should be in frequency-lock mode, where the
576 1.28.4.1 yamt * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
577 1.28.4.1 yamt * is selected by the STA_MODE status bit.
578 1.28.4.1 yamt *
579 1.28.4.1 yamt * Note: splclock() is in effect.
580 1.28.4.1 yamt */
581 1.28.4.1 yamt void
582 1.28.4.1 yamt hardupdate(long offset)
583 1.28.4.1 yamt {
584 1.28.4.1 yamt long mtemp;
585 1.28.4.1 yamt l_fp ftemp;
586 1.28.4.1 yamt
587 1.28.4.1 yamt /*
588 1.28.4.1 yamt * Select how the phase is to be controlled and from which
589 1.28.4.1 yamt * source. If the PPS signal is present and enabled to
590 1.28.4.1 yamt * discipline the time, the PPS offset is used; otherwise, the
591 1.28.4.1 yamt * argument offset is used.
592 1.28.4.1 yamt */
593 1.28.4.1 yamt if (!(time_status & STA_PLL))
594 1.28.4.1 yamt return;
595 1.28.4.1 yamt if (!(time_status & STA_PPSTIME && time_status &
596 1.28.4.1 yamt STA_PPSSIGNAL)) {
597 1.28.4.1 yamt if (offset > MAXPHASE)
598 1.28.4.1 yamt time_monitor = MAXPHASE;
599 1.28.4.1 yamt else if (offset < -MAXPHASE)
600 1.28.4.1 yamt time_monitor = -MAXPHASE;
601 1.28.4.1 yamt else
602 1.28.4.1 yamt time_monitor = offset;
603 1.28.4.1 yamt L_LINT(time_offset, time_monitor);
604 1.28.4.1 yamt }
605 1.28.4.1 yamt
606 1.28.4.1 yamt /*
607 1.28.4.1 yamt * Select how the frequency is to be controlled and in which
608 1.28.4.1 yamt * mode (PLL or FLL). If the PPS signal is present and enabled
609 1.28.4.1 yamt * to discipline the frequency, the PPS frequency is used;
610 1.28.4.1 yamt * otherwise, the argument offset is used to compute it.
611 1.28.4.1 yamt */
612 1.28.4.1 yamt if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) {
613 1.28.4.1 yamt time_reftime = time_second;
614 1.28.4.1 yamt return;
615 1.28.4.1 yamt }
616 1.28.4.1 yamt if (time_status & STA_FREQHOLD || time_reftime == 0)
617 1.28.4.1 yamt time_reftime = time_second;
618 1.28.4.1 yamt mtemp = time_second - time_reftime;
619 1.28.4.1 yamt L_LINT(ftemp, time_monitor);
620 1.28.4.1 yamt L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
621 1.28.4.1 yamt L_MPY(ftemp, mtemp);
622 1.28.4.1 yamt L_ADD(time_freq, ftemp);
623 1.28.4.1 yamt time_status &= ~STA_MODE;
624 1.28.4.1 yamt if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp >
625 1.28.4.1 yamt MAXSEC)) {
626 1.28.4.1 yamt L_LINT(ftemp, (time_monitor << 4) / mtemp);
627 1.28.4.1 yamt L_RSHIFT(ftemp, SHIFT_FLL + 4);
628 1.28.4.1 yamt L_ADD(time_freq, ftemp);
629 1.28.4.1 yamt time_status |= STA_MODE;
630 1.28.4.1 yamt }
631 1.28.4.1 yamt time_reftime = time_second;
632 1.28.4.1 yamt if (L_GINT(time_freq) > MAXFREQ)
633 1.28.4.1 yamt L_LINT(time_freq, MAXFREQ);
634 1.28.4.1 yamt else if (L_GINT(time_freq) < -MAXFREQ)
635 1.28.4.1 yamt L_LINT(time_freq, -MAXFREQ);
636 1.28.4.1 yamt }
637 1.28.4.1 yamt
638 1.28.4.1 yamt #ifdef PPS_SYNC
639 1.28.4.1 yamt /*
640 1.28.4.1 yamt * hardpps() - discipline CPU clock oscillator to external PPS signal
641 1.28.4.1 yamt *
642 1.28.4.1 yamt * This routine is called at each PPS interrupt in order to discipline
643 1.28.4.1 yamt * the CPU clock oscillator to the PPS signal. It measures the PPS phase
644 1.28.4.1 yamt * and leaves it in a handy spot for the hardclock() routine. It
645 1.28.4.1 yamt * integrates successive PPS phase differences and calculates the
646 1.28.4.1 yamt * frequency offset. This is used in hardclock() to discipline the CPU
647 1.28.4.1 yamt * clock oscillator so that intrinsic frequency error is cancelled out.
648 1.28.4.1 yamt * The code requires the caller to capture the time and hardware counter
649 1.28.4.1 yamt * value at the on-time PPS signal transition.
650 1.28.4.1 yamt *
651 1.28.4.1 yamt * Note that, on some Unix systems, this routine runs at an interrupt
652 1.28.4.1 yamt * priority level higher than the timer interrupt routine hardclock().
653 1.28.4.1 yamt * Therefore, the variables used are distinct from the hardclock()
654 1.28.4.1 yamt * variables, except for certain exceptions: The PPS frequency pps_freq
655 1.28.4.1 yamt * and phase pps_offset variables are determined by this routine and
656 1.28.4.1 yamt * updated atomically. The time_tolerance variable can be considered a
657 1.28.4.1 yamt * constant, since it is infrequently changed, and then only when the
658 1.28.4.1 yamt * PPS signal is disabled. The watchdog counter pps_valid is updated
659 1.28.4.1 yamt * once per second by hardclock() and is atomically cleared in this
660 1.28.4.1 yamt * routine.
661 1.28.4.1 yamt */
662 1.28.4.1 yamt void
663 1.28.4.1 yamt hardpps(struct timespec *tsp, /* time at PPS */
664 1.28.4.1 yamt long nsec /* hardware counter at PPS */)
665 1.28.4.1 yamt {
666 1.28.4.1 yamt long u_sec, u_nsec, v_nsec; /* temps */
667 1.28.4.1 yamt l_fp ftemp;
668 1.28.4.1 yamt
669 1.28.4.1 yamt /*
670 1.28.4.1 yamt * The signal is first processed by a range gate and frequency
671 1.28.4.1 yamt * discriminator. The range gate rejects noise spikes outside
672 1.28.4.1 yamt * the range +-500 us. The frequency discriminator rejects input
673 1.28.4.1 yamt * signals with apparent frequency outside the range 1 +-500
674 1.28.4.1 yamt * PPM. If two hits occur in the same second, we ignore the
675 1.28.4.1 yamt * later hit; if not and a hit occurs outside the range gate,
676 1.28.4.1 yamt * keep the later hit for later comparison, but do not process
677 1.28.4.1 yamt * it.
678 1.28.4.1 yamt */
679 1.28.4.1 yamt time_status |= STA_PPSSIGNAL | STA_PPSJITTER;
680 1.28.4.1 yamt time_status &= ~(STA_PPSWANDER | STA_PPSERROR);
681 1.28.4.1 yamt pps_valid = PPS_VALID;
682 1.28.4.1 yamt u_sec = tsp->tv_sec;
683 1.28.4.1 yamt u_nsec = tsp->tv_nsec;
684 1.28.4.1 yamt if (u_nsec >= (NANOSECOND >> 1)) {
685 1.28.4.1 yamt u_nsec -= NANOSECOND;
686 1.28.4.1 yamt u_sec++;
687 1.28.4.1 yamt }
688 1.28.4.1 yamt v_nsec = u_nsec - pps_tf[0].tv_nsec;
689 1.28.4.1 yamt if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND -
690 1.28.4.1 yamt MAXFREQ)
691 1.28.4.1 yamt return;
692 1.28.4.1 yamt pps_tf[2] = pps_tf[1];
693 1.28.4.1 yamt pps_tf[1] = pps_tf[0];
694 1.28.4.1 yamt pps_tf[0].tv_sec = u_sec;
695 1.28.4.1 yamt pps_tf[0].tv_nsec = u_nsec;
696 1.28.4.1 yamt
697 1.28.4.1 yamt /*
698 1.28.4.1 yamt * Compute the difference between the current and previous
699 1.28.4.1 yamt * counter values. If the difference exceeds 0.5 s, assume it
700 1.28.4.1 yamt * has wrapped around, so correct 1.0 s. If the result exceeds
701 1.28.4.1 yamt * the tick interval, the sample point has crossed a tick
702 1.28.4.1 yamt * boundary during the last second, so correct the tick. Very
703 1.28.4.1 yamt * intricate.
704 1.28.4.1 yamt */
705 1.28.4.1 yamt u_nsec = nsec;
706 1.28.4.1 yamt if (u_nsec > (NANOSECOND >> 1))
707 1.28.4.1 yamt u_nsec -= NANOSECOND;
708 1.28.4.1 yamt else if (u_nsec < -(NANOSECOND >> 1))
709 1.28.4.1 yamt u_nsec += NANOSECOND;
710 1.28.4.1 yamt pps_fcount += u_nsec;
711 1.28.4.1 yamt if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ)
712 1.28.4.1 yamt return;
713 1.28.4.1 yamt time_status &= ~STA_PPSJITTER;
714 1.28.4.1 yamt
715 1.28.4.1 yamt /*
716 1.28.4.1 yamt * A three-stage median filter is used to help denoise the PPS
717 1.28.4.1 yamt * time. The median sample becomes the time offset estimate; the
718 1.28.4.1 yamt * difference between the other two samples becomes the time
719 1.28.4.1 yamt * dispersion (jitter) estimate.
720 1.28.4.1 yamt */
721 1.28.4.1 yamt if (pps_tf[0].tv_nsec > pps_tf[1].tv_nsec) {
722 1.28.4.1 yamt if (pps_tf[1].tv_nsec > pps_tf[2].tv_nsec) {
723 1.28.4.1 yamt v_nsec = pps_tf[1].tv_nsec; /* 0 1 2 */
724 1.28.4.1 yamt u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec;
725 1.28.4.1 yamt } else if (pps_tf[2].tv_nsec > pps_tf[0].tv_nsec) {
726 1.28.4.1 yamt v_nsec = pps_tf[0].tv_nsec; /* 2 0 1 */
727 1.28.4.1 yamt u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec;
728 1.28.4.1 yamt } else {
729 1.28.4.1 yamt v_nsec = pps_tf[2].tv_nsec; /* 0 2 1 */
730 1.28.4.1 yamt u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec;
731 1.28.4.1 yamt }
732 1.28.4.1 yamt } else {
733 1.28.4.1 yamt if (pps_tf[1].tv_nsec < pps_tf[2].tv_nsec) {
734 1.28.4.1 yamt v_nsec = pps_tf[1].tv_nsec; /* 2 1 0 */
735 1.28.4.1 yamt u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec;
736 1.28.4.1 yamt } else if (pps_tf[2].tv_nsec < pps_tf[0].tv_nsec) {
737 1.28.4.1 yamt v_nsec = pps_tf[0].tv_nsec; /* 1 0 2 */
738 1.28.4.1 yamt u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec;
739 1.28.4.1 yamt } else {
740 1.28.4.1 yamt v_nsec = pps_tf[2].tv_nsec; /* 1 2 0 */
741 1.28.4.1 yamt u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec;
742 1.28.4.1 yamt }
743 1.28.4.1 yamt }
744 1.28.4.1 yamt
745 1.28.4.1 yamt /*
746 1.28.4.1 yamt * Nominal jitter is due to PPS signal noise and interrupt
747 1.28.4.1 yamt * latency. If it exceeds the popcorn threshold, the sample is
748 1.28.4.1 yamt * discarded. otherwise, if so enabled, the time offset is
749 1.28.4.1 yamt * updated. We can tolerate a modest loss of data here without
750 1.28.4.1 yamt * much degrading time accuracy.
751 1.28.4.1 yamt */
752 1.28.4.1 yamt if (u_nsec > (pps_jitter << PPS_POPCORN)) {
753 1.28.4.1 yamt time_status |= STA_PPSJITTER;
754 1.28.4.1 yamt pps_jitcnt++;
755 1.28.4.1 yamt } else if (time_status & STA_PPSTIME) {
756 1.28.4.1 yamt time_monitor = -v_nsec;
757 1.28.4.1 yamt L_LINT(time_offset, time_monitor);
758 1.28.4.1 yamt }
759 1.28.4.1 yamt pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
760 1.28.4.1 yamt u_sec = pps_tf[0].tv_sec - pps_lastsec;
761 1.28.4.1 yamt if (u_sec < (1 << pps_shift))
762 1.28.4.1 yamt return;
763 1.28.4.1 yamt
764 1.28.4.1 yamt /*
765 1.28.4.1 yamt * At the end of the calibration interval the difference between
766 1.28.4.1 yamt * the first and last counter values becomes the scaled
767 1.28.4.1 yamt * frequency. It will later be divided by the length of the
768 1.28.4.1 yamt * interval to determine the frequency update. If the frequency
769 1.28.4.1 yamt * exceeds a sanity threshold, or if the actual calibration
770 1.28.4.1 yamt * interval is not equal to the expected length, the data are
771 1.28.4.1 yamt * discarded. We can tolerate a modest loss of data here without
772 1.28.4.1 yamt * much degrading frequency accuracy.
773 1.28.4.1 yamt */
774 1.28.4.1 yamt pps_calcnt++;
775 1.28.4.1 yamt v_nsec = -pps_fcount;
776 1.28.4.1 yamt pps_lastsec = pps_tf[0].tv_sec;
777 1.28.4.1 yamt pps_fcount = 0;
778 1.28.4.1 yamt u_nsec = MAXFREQ << pps_shift;
779 1.28.4.1 yamt if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 <<
780 1.28.4.1 yamt pps_shift)) {
781 1.28.4.1 yamt time_status |= STA_PPSERROR;
782 1.28.4.1 yamt pps_errcnt++;
783 1.28.4.1 yamt return;
784 1.28.4.1 yamt }
785 1.28.4.1 yamt
786 1.28.4.1 yamt /*
787 1.28.4.1 yamt * Here the raw frequency offset and wander (stability) is
788 1.28.4.1 yamt * calculated. If the wander is less than the wander threshold
789 1.28.4.1 yamt * for four consecutive averaging intervals, the interval is
790 1.28.4.1 yamt * doubled; if it is greater than the threshold for four
791 1.28.4.1 yamt * consecutive intervals, the interval is halved. The scaled
792 1.28.4.1 yamt * frequency offset is converted to frequency offset. The
793 1.28.4.1 yamt * stability metric is calculated as the average of recent
794 1.28.4.1 yamt * frequency changes, but is used only for performance
795 1.28.4.1 yamt * monitoring.
796 1.28.4.1 yamt */
797 1.28.4.1 yamt L_LINT(ftemp, v_nsec);
798 1.28.4.1 yamt L_RSHIFT(ftemp, pps_shift);
799 1.28.4.1 yamt L_SUB(ftemp, pps_freq);
800 1.28.4.1 yamt u_nsec = L_GINT(ftemp);
801 1.28.4.1 yamt if (u_nsec > PPS_MAXWANDER) {
802 1.28.4.1 yamt L_LINT(ftemp, PPS_MAXWANDER);
803 1.28.4.1 yamt pps_intcnt--;
804 1.28.4.1 yamt time_status |= STA_PPSWANDER;
805 1.28.4.1 yamt pps_stbcnt++;
806 1.28.4.1 yamt } else if (u_nsec < -PPS_MAXWANDER) {
807 1.28.4.1 yamt L_LINT(ftemp, -PPS_MAXWANDER);
808 1.28.4.1 yamt pps_intcnt--;
809 1.28.4.1 yamt time_status |= STA_PPSWANDER;
810 1.28.4.1 yamt pps_stbcnt++;
811 1.28.4.1 yamt } else {
812 1.28.4.1 yamt pps_intcnt++;
813 1.28.4.1 yamt }
814 1.28.4.1 yamt if (pps_intcnt >= 4) {
815 1.28.4.1 yamt pps_intcnt = 4;
816 1.28.4.1 yamt if (pps_shift < pps_shiftmax) {
817 1.28.4.1 yamt pps_shift++;
818 1.28.4.1 yamt pps_intcnt = 0;
819 1.28.4.1 yamt }
820 1.28.4.1 yamt } else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) {
821 1.28.4.1 yamt pps_intcnt = -4;
822 1.28.4.1 yamt if (pps_shift > PPS_FAVG) {
823 1.28.4.1 yamt pps_shift--;
824 1.28.4.1 yamt pps_intcnt = 0;
825 1.28.4.1 yamt }
826 1.28.4.1 yamt }
827 1.28.4.1 yamt if (u_nsec < 0)
828 1.28.4.1 yamt u_nsec = -u_nsec;
829 1.28.4.1 yamt pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
830 1.28.4.1 yamt
831 1.28.4.1 yamt /*
832 1.28.4.1 yamt * The PPS frequency is recalculated and clamped to the maximum
833 1.28.4.1 yamt * MAXFREQ. If enabled, the system clock frequency is updated as
834 1.28.4.1 yamt * well.
835 1.28.4.1 yamt */
836 1.28.4.1 yamt L_ADD(pps_freq, ftemp);
837 1.28.4.1 yamt u_nsec = L_GINT(pps_freq);
838 1.28.4.1 yamt if (u_nsec > MAXFREQ)
839 1.28.4.1 yamt L_LINT(pps_freq, MAXFREQ);
840 1.28.4.1 yamt else if (u_nsec < -MAXFREQ)
841 1.28.4.1 yamt L_LINT(pps_freq, -MAXFREQ);
842 1.28.4.1 yamt if (time_status & STA_PPSFREQ)
843 1.28.4.1 yamt time_freq = pps_freq;
844 1.28.4.1 yamt }
845 1.28.4.1 yamt #endif /* PPS_SYNC */
846 1.28.4.1 yamt #endif /* NTP */
847 1.1 jonathan
848 1.28.4.1 yamt #ifdef NTP
849 1.28.4.1 yamt int
850 1.28.4.7 yamt ntp_timestatus(void)
851 1.1 jonathan {
852 1.1 jonathan /*
853 1.1 jonathan * Status word error decode. If any of these conditions
854 1.1 jonathan * occur, an error is returned, instead of the status
855 1.1 jonathan * word. Most applications will care only about the fact
856 1.1 jonathan * the system clock may not be trusted, not about the
857 1.1 jonathan * details.
858 1.1 jonathan *
859 1.1 jonathan * Hardware or software error
860 1.1 jonathan */
861 1.1 jonathan if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
862 1.1 jonathan
863 1.1 jonathan /*
864 1.1 jonathan * PPS signal lost when either time or frequency
865 1.1 jonathan * synchronization requested
866 1.1 jonathan */
867 1.28.4.1 yamt (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
868 1.28.4.1 yamt !(time_status & STA_PPSSIGNAL)) ||
869 1.1 jonathan
870 1.1 jonathan /*
871 1.1 jonathan * PPS jitter exceeded when time synchronization
872 1.1 jonathan * requested
873 1.1 jonathan */
874 1.28.4.1 yamt (time_status & STA_PPSTIME &&
875 1.28.4.1 yamt time_status & STA_PPSJITTER) ||
876 1.1 jonathan
877 1.1 jonathan /*
878 1.1 jonathan * PPS wander exceeded or calibration error when
879 1.1 jonathan * frequency synchronization requested
880 1.1 jonathan */
881 1.28.4.1 yamt (time_status & STA_PPSFREQ &&
882 1.28.4.1 yamt time_status & (STA_PPSWANDER | STA_PPSERROR)))
883 1.28.4.1 yamt return (TIME_ERROR);
884 1.1 jonathan else
885 1.28.4.1 yamt return (time_state);
886 1.28.4.1 yamt }
887 1.28.4.1 yamt
888 1.28.4.1 yamt /*ARGSUSED*/
889 1.28.4.1 yamt /*
890 1.28.4.1 yamt * ntp_gettime() - NTP user application interface
891 1.28.4.1 yamt */
892 1.28.4.1 yamt int
893 1.28.4.6 yamt sys___ntp_gettime30(struct lwp *l, const struct sys___ntp_gettime30_args *uap, register_t *retval)
894 1.28.4.1 yamt {
895 1.28.4.6 yamt /* {
896 1.28.4.1 yamt syscallarg(struct ntptimeval *) ntvp;
897 1.28.4.6 yamt } */
898 1.28.4.1 yamt struct ntptimeval ntv;
899 1.28.4.1 yamt int error = 0;
900 1.28.4.1 yamt
901 1.28.4.1 yamt if (SCARG(uap, ntvp)) {
902 1.28.4.1 yamt ntp_gettime(&ntv);
903 1.28.4.1 yamt
904 1.28.4.4 yamt error = copyout((void *)&ntv, (void *)SCARG(uap, ntvp),
905 1.28.4.1 yamt sizeof(ntv));
906 1.28.4.1 yamt }
907 1.28.4.1 yamt if (!error) {
908 1.28.4.1 yamt *retval = ntp_timestatus();
909 1.28.4.1 yamt }
910 1.28.4.1 yamt return(error);
911 1.28.4.1 yamt }
912 1.28.4.1 yamt
913 1.28.4.1 yamt #ifdef COMPAT_30
914 1.28.4.1 yamt int
915 1.28.4.6 yamt compat_30_sys_ntp_gettime(struct lwp *l, const struct compat_30_sys_ntp_gettime_args *uap, register_t *retval)
916 1.28.4.1 yamt {
917 1.28.4.6 yamt /* {
918 1.28.4.1 yamt syscallarg(struct ntptimeval30 *) ontvp;
919 1.28.4.6 yamt } */
920 1.28.4.1 yamt struct ntptimeval ntv;
921 1.28.4.1 yamt struct ntptimeval30 ontv;
922 1.28.4.1 yamt int error = 0;
923 1.28.4.1 yamt
924 1.28.4.1 yamt if (SCARG(uap, ntvp)) {
925 1.28.4.1 yamt ntp_gettime(&ntv);
926 1.28.4.1 yamt TIMESPEC_TO_TIMEVAL(&ontv.time, &ntv.time);
927 1.28.4.1 yamt ontv.maxerror = ntv.maxerror;
928 1.28.4.1 yamt ontv.esterror = ntv.esterror;
929 1.28.4.1 yamt
930 1.28.4.4 yamt error = copyout((void *)&ontv, (void *)SCARG(uap, ntvp),
931 1.28.4.1 yamt sizeof(ontv));
932 1.28.4.1 yamt }
933 1.28.4.1 yamt if (!error)
934 1.28.4.1 yamt *retval = ntp_timestatus();
935 1.28.4.1 yamt
936 1.28.4.1 yamt return (error);
937 1.28.4.1 yamt }
938 1.28.4.1 yamt #endif
939 1.28.4.1 yamt
940 1.28.4.1 yamt /*
941 1.28.4.1 yamt * return information about kernel precision timekeeping
942 1.28.4.1 yamt */
943 1.28.4.1 yamt static int
944 1.28.4.1 yamt sysctl_kern_ntptime(SYSCTLFN_ARGS)
945 1.28.4.1 yamt {
946 1.28.4.1 yamt struct sysctlnode node;
947 1.28.4.1 yamt struct ntptimeval ntv;
948 1.28.4.1 yamt
949 1.28.4.1 yamt ntp_gettime(&ntv);
950 1.25 atatat
951 1.25 atatat node = *rnode;
952 1.25 atatat node.sysctl_data = &ntv;
953 1.25 atatat node.sysctl_size = sizeof(ntv);
954 1.25 atatat return (sysctl_lookup(SYSCTLFN_CALL(&node)));
955 1.25 atatat }
956 1.25 atatat
957 1.25 atatat SYSCTL_SETUP(sysctl_kern_ntptime_setup, "sysctl kern.ntptime node setup")
958 1.25 atatat {
959 1.25 atatat
960 1.26 atatat sysctl_createv(clog, 0, NULL, NULL,
961 1.26 atatat CTLFLAG_PERMANENT,
962 1.25 atatat CTLTYPE_NODE, "kern", NULL,
963 1.25 atatat NULL, 0, NULL, 0,
964 1.25 atatat CTL_KERN, CTL_EOL);
965 1.25 atatat
966 1.26 atatat sysctl_createv(clog, 0, NULL, NULL,
967 1.26 atatat CTLFLAG_PERMANENT,
968 1.27 atatat CTLTYPE_STRUCT, "ntptime",
969 1.27 atatat SYSCTL_DESCR("Kernel clock values for NTP"),
970 1.25 atatat sysctl_kern_ntptime, 0, NULL,
971 1.25 atatat sizeof(struct ntptimeval),
972 1.25 atatat CTL_KERN, KERN_NTPTIME, CTL_EOL);
973 1.1 jonathan }
974 1.4 thorpej #else /* !NTP */
975 1.13 bjh21 /* For some reason, raising SIGSYS (as sys_nosys would) is problematic. */
976 1.13 bjh21
977 1.4 thorpej int
978 1.28.4.6 yamt sys___ntp_gettime30(struct lwp *l, const struct sys___ntp_gettime30_args *uap, register_t *retval)
979 1.4 thorpej {
980 1.19 simonb
981 1.4 thorpej return(ENOSYS);
982 1.4 thorpej }
983 1.28.4.1 yamt
984 1.28.4.1 yamt #ifdef COMPAT_30
985 1.28.4.1 yamt int
986 1.28.4.6 yamt compat_30_sys_ntp_gettime(struct lwp *l, const struct compat_30_sys_ntp_gettime_args *uap, register_t *retval)
987 1.28.4.1 yamt {
988 1.28.4.1 yamt
989 1.28.4.1 yamt return(ENOSYS);
990 1.28.4.1 yamt }
991 1.28.4.1 yamt #endif
992 1.13 bjh21 #endif /* !NTP */
993