]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_ntptime.c
xnu-6153.41.3.tar.gz
[apple/xnu.git] / bsd / kern / kern_ntptime.c
CommitLineData
5ba3f43e
A
1/*-
2 ***********************************************************************
3 * *
4 * Copyright (c) David L. Mills 1993-2001 *
5 * *
6 * Permission to use, copy, modify, and distribute this software and *
7 * its documentation for any purpose and without fee is hereby *
8 * granted, provided that the above copyright notice appears in all *
9 * copies and that both the copyright notice and this permission *
10 * notice appear in supporting documentation, and that the name *
11 * University of Delaware not be used in advertising or publicity *
12 * pertaining to distribution of the software without specific, *
13 * written prior permission. The University of Delaware makes no *
14 * representations about the suitability this software for any *
15 * purpose. It is provided "as is" without express or implied *
16 * warranty. *
17 * *
18 **********************************************************************/
19
20
21/*
22 * Adapted from the original sources for FreeBSD and timecounters by:
23 * Poul-Henning Kamp <phk@FreeBSD.org>.
24 *
25 * The 32bit version of the "LP" macros seems a bit past its "sell by"
26 * date so I have retained only the 64bit version and included it directly
27 * in this file.
28 *
29 * Only minor changes done to interface with the timecounters over in
30 * sys/kern/kern_clock.c. Some of the comments below may be (even more)
31 * confusing and/or plain wrong in that context.
32 */
33
34/*
35 * Copyright (c) 2017 Apple Computer, Inc. All rights reserved.
36 *
37 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
38 *
39 * This file contains Original Code and/or Modifications of Original Code
40 * as defined in and that are subject to the Apple Public Source License
41 * Version 2.0 (the 'License'). You may not use this file except in
42 * compliance with the License. The rights granted to you under the License
43 * may not be used to create, or enable the creation or redistribution of,
44 * unlawful or unlicensed copies of an Apple operating system, or to
45 * circumvent, violate, or enable the circumvention or violation of, any
46 * terms of an Apple operating system software license agreement.
47 *
48 * Please obtain a copy of the License at
49 * http://www.opensource.apple.com/apsl/ and read it before using this file.
50 *
51 * The Original Code and all software distributed under the License are
52 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
53 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
54 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
55 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
56 * Please see the License for the specific language governing rights and
57 * limitations under the License.
58 *
59 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
60 */
61
62#include <sys/cdefs.h>
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/eventhandler.h>
66#include <sys/kernel.h>
67#include <sys/priv.h>
68#include <sys/proc.h>
69#include <sys/lock.h>
70#include <sys/time.h>
71#include <sys/timex.h>
72#include <kern/clock.h>
73#include <sys/sysctl.h>
74#include <sys/sysproto.h>
75#include <sys/kauth.h>
76#include <kern/thread_call.h>
77#include <kern/timer_call.h>
78#include <machine/machine_routines.h>
79#if CONFIG_MACF
80#include <security/mac_framework.h>
81#endif
82#include <IOKit/IOBSD.h>
cc8bc92a 83#include <os/log.h>
5ba3f43e
A
84
85typedef int64_t l_fp;
0a7de745
A
86#define L_ADD(v, u) ((v) += (u))
87#define L_SUB(v, u) ((v) -= (u))
88#define L_ADDHI(v, a) ((v) += (int64_t)(a) << 32)
89#define L_NEG(v) ((v) = -(v))
5ba3f43e
A
90#define L_RSHIFT(v, n) \
91 do { \
0a7de745
A
92 if ((v) < 0) \
93 (v) = -(-(v) >> (n)); \
94 else \
95 (v) = (v) >> (n); \
5ba3f43e 96 } while (0)
0a7de745
A
97#define L_MPY(v, a) ((v) *= (a))
98#define L_CLR(v) ((v) = 0)
99#define L_ISNEG(v) ((v) < 0)
5ba3f43e
A
100#define L_LINT(v, a) \
101 do { \
0a7de745
A
102 if ((a) > 0) \
103 ((v) = (int64_t)(a) << 32); \
104 else \
105 ((v) = -((int64_t)(-(a)) << 32)); \
5ba3f43e 106 } while (0)
0a7de745 107#define L_GINT(v) ((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
5ba3f43e
A
108
109/*
110 * Generic NTP kernel interface
111 *
112 * These routines constitute the Network Time Protocol (NTP) interfaces
113 * for user and daemon application programs. The ntp_gettime() routine
114 * provides the time, maximum error (synch distance) and estimated error
115 * (dispersion) to client user application programs. The ntp_adjtime()
116 * routine is used by the NTP daemon to adjust the calendar clock to an
117 * externally derived time. The time offset and related variables set by
118 * this routine are used by other routines in this module to adjust the
119 * phase and frequency of the clock discipline loop which controls the
120 * system clock.
121 *
122 * When the kernel time is reckoned directly in nanoseconds (NTP_NANO
123 * defined), the time at each tick interrupt is derived directly from
124 * the kernel time variable. When the kernel time is reckoned in
125 * microseconds, (NTP_NANO undefined), the time is derived from the
126 * kernel time variable together with a variable representing the
127 * leftover nanoseconds at the last tick interrupt. In either case, the
128 * current nanosecond time is reckoned from these values plus an
129 * interpolated value derived by the clock routines in another
130 * architecture-specific module. The interpolation can use either a
131 * dedicated counter or a processor cycle counter (PCC) implemented in
132 * some architectures.
133 *
134 */
135/*
136 * Phase/frequency-lock loop (PLL/FLL) definitions
137 *
138 * The nanosecond clock discipline uses two variable types, time
139 * variables and frequency variables. Both types are represented as 64-
140 * bit fixed-point quantities with the decimal point between two 32-bit
141 * halves. On a 32-bit machine, each half is represented as a single
142 * word and mathematical operations are done using multiple-precision
143 * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
144 * used.
145 *
146 * A time variable is a signed 64-bit fixed-point number in ns and
147 * fraction. It represents the remaining time offset to be amortized
148 * over succeeding tick interrupts. The maximum time offset is about
149 * 0.5 s and the resolution is about 2.3e-10 ns.
150 *
151 * 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
152 * 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
153 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
154 * |s s s| ns |
155 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
156 * | fraction |
157 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158 *
159 * A frequency variable is a signed 64-bit fixed-point number in ns/s
160 * and fraction. It represents the ns and fraction to be added to the
161 * kernel time variable at each second. The maximum frequency offset is
162 * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
163 *
164 * 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
165 * 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
166 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167 * |s s s s s s s s s s s s s| ns/s |
168 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169 * | fraction |
170 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
171 */
172
0a7de745
A
173#define SHIFT_PLL 4
174#define SHIFT_FLL 2
5ba3f43e
A
175
176static int time_state = TIME_OK;
177int time_status = STA_UNSYNC;
178static long time_tai;
179static long time_constant;
180static long time_precision = 1;
181static long time_maxerror = MAXPHASE / 1000;
182static unsigned long last_time_maxerror_update;
183long time_esterror = MAXPHASE / 1000;
184static long time_reftime;
185static l_fp time_offset;
186static l_fp time_freq;
187static int64_t time_adjtime;
188static int updated;
189
190static lck_spin_t * ntp_lock;
191static lck_grp_t * ntp_lock_grp;
192static lck_attr_t * ntp_lock_attr;
0a7de745 193static lck_grp_attr_t *ntp_lock_grp_attr;
5ba3f43e 194
0a7de745
A
195#define NTP_LOCK(enable) \
196 enable = ml_set_interrupts_enabled(FALSE); \
197 lck_spin_lock(ntp_lock);
5ba3f43e 198
0a7de745
A
199#define NTP_UNLOCK(enable) \
200 lck_spin_unlock(ntp_lock);\
201 ml_set_interrupts_enabled(enable);
5ba3f43e 202
0a7de745 203#define NTP_ASSERT_LOCKED() LCK_SPIN_ASSERT(ntp_lock, LCK_ASSERT_OWNED)
5ba3f43e
A
204
205static timer_call_data_t ntp_loop_update;
206static uint64_t ntp_loop_deadline;
207static uint32_t ntp_loop_active;
208static uint32_t ntp_loop_period;
209#define NTP_LOOP_PERIOD_INTERVAL (NSEC_PER_SEC) /*1 second interval*/
210
211void ntp_init(void);
212static void hardupdate(long offset);
213static void ntp_gettime1(struct ntptimeval *ntvp);
214static bool ntp_is_time_error(int tsl);
215
216static void ntp_loop_update_call(void);
217static void refresh_ntp_loop(void);
218static void start_ntp_loop(void);
219
cc8bc92a
A
220#if DEVELOPMENT || DEBUG
221uint32_t g_should_log_clock_adjustments = 0;
222SYSCTL_INT(_kern, OID_AUTO, log_clock_adjustments, CTLFLAG_RW | CTLFLAG_LOCKED, &g_should_log_clock_adjustments, 0, "enable kernel clock adjustment logging");
223#endif
224
5ba3f43e
A
225static bool
226ntp_is_time_error(int tsl)
227{
0a7de745
A
228 if (tsl & (STA_UNSYNC | STA_CLOCKERR)) {
229 return true;
230 }
5ba3f43e 231
0a7de745 232 return false;
5ba3f43e
A
233}
234
235static void
236ntp_gettime1(struct ntptimeval *ntvp)
237{
238 struct timespec atv;
239
240 NTP_ASSERT_LOCKED();
241
242 nanotime(&atv);
243 ntvp->time.tv_sec = atv.tv_sec;
244 ntvp->time.tv_nsec = atv.tv_nsec;
245 if ((unsigned long)atv.tv_sec > last_time_maxerror_update) {
0a7de745 246 time_maxerror += (MAXFREQ / 1000) * (atv.tv_sec - last_time_maxerror_update);
5ba3f43e
A
247 last_time_maxerror_update = atv.tv_sec;
248 }
249 ntvp->maxerror = time_maxerror;
250 ntvp->esterror = time_esterror;
251 ntvp->tai = time_tai;
252 ntvp->time_state = time_state;
253
0a7de745 254 if (ntp_is_time_error(time_status)) {
5ba3f43e 255 ntvp->time_state = TIME_ERROR;
0a7de745 256 }
5ba3f43e
A
257}
258
259int
260ntp_gettime(struct proc *p, struct ntp_gettime_args *uap, __unused int32_t *retval)
261{
262 struct ntptimeval ntv;
263 int error;
264 boolean_t enable;
265
266 NTP_LOCK(enable);
267 ntp_gettime1(&ntv);
268 NTP_UNLOCK(enable);
269
270 if (IS_64BIT_PROCESS(p)) {
527f9951 271 struct user64_ntptimeval user_ntv = {};
5ba3f43e
A
272 user_ntv.time.tv_sec = ntv.time.tv_sec;
273 user_ntv.time.tv_nsec = ntv.time.tv_nsec;
274 user_ntv.maxerror = ntv.maxerror;
275 user_ntv.esterror = ntv.esterror;
276 user_ntv.tai = ntv.tai;
277 user_ntv.time_state = ntv.time_state;
278 error = copyout(&user_ntv, uap->ntvp, sizeof(user_ntv));
279 } else {
527f9951 280 struct user32_ntptimeval user_ntv = {};
5ba3f43e
A
281 user_ntv.time.tv_sec = ntv.time.tv_sec;
282 user_ntv.time.tv_nsec = ntv.time.tv_nsec;
283 user_ntv.maxerror = ntv.maxerror;
284 user_ntv.esterror = ntv.esterror;
285 user_ntv.tai = ntv.tai;
286 user_ntv.time_state = ntv.time_state;
287 error = copyout(&user_ntv, uap->ntvp, sizeof(user_ntv));
288 }
289
0a7de745 290 if (error) {
5ba3f43e 291 return error;
0a7de745 292 }
5ba3f43e
A
293
294 return ntv.time_state;
295}
296
297int
d9a64523 298ntp_adjtime(struct proc *p, struct ntp_adjtime_args *uap, int32_t *retval)
5ba3f43e 299{
d9a64523 300 struct timex ntv = {};
5ba3f43e 301 long freq;
d9a64523 302 unsigned int modes;
5ba3f43e
A
303 int error, ret = 0;
304 clock_sec_t sec;
305 clock_usec_t microsecs;
306 boolean_t enable;
307
308 if (IS_64BIT_PROCESS(p)) {
309 struct user64_timex user_ntv;
310 error = copyin(uap->tp, &user_ntv, sizeof(user_ntv));
311 ntv.modes = user_ntv.modes;
312 ntv.offset = user_ntv.offset;
313 ntv.freq = user_ntv.freq;
314 ntv.maxerror = user_ntv.maxerror;
315 ntv.esterror = user_ntv.esterror;
316 ntv.status = user_ntv.status;
317 ntv.constant = user_ntv.constant;
318 ntv.precision = user_ntv.precision;
319 ntv.tolerance = user_ntv.tolerance;
5ba3f43e
A
320 } else {
321 struct user32_timex user_ntv;
322 error = copyin(uap->tp, &user_ntv, sizeof(user_ntv));
323 ntv.modes = user_ntv.modes;
324 ntv.offset = user_ntv.offset;
325 ntv.freq = user_ntv.freq;
326 ntv.maxerror = user_ntv.maxerror;
327 ntv.esterror = user_ntv.esterror;
328 ntv.status = user_ntv.status;
329 ntv.constant = user_ntv.constant;
330 ntv.precision = user_ntv.precision;
331 ntv.tolerance = user_ntv.tolerance;
332 }
0a7de745
A
333 if (error) {
334 return error;
335 }
5ba3f43e 336
cb323159 337#if DEVELOPMENT || DEBUG
cc8bc92a 338 if (g_should_log_clock_adjustments) {
d9a64523 339 os_log(OS_LOG_DEFAULT, "%s: BEFORE modes %u offset %ld freq %ld status %d constant %ld time_adjtime %lld\n",
0a7de745 340 __func__, ntv.modes, ntv.offset, ntv.freq, ntv.status, ntv.constant, time_adjtime);
cc8bc92a
A
341 }
342#endif
5ba3f43e
A
343 /*
344 * Update selected clock variables - only the superuser can
345 * change anything. Note that there is no error checking here on
346 * the assumption the superuser should know what it is doing.
347 * Note that either the time constant or TAI offset are loaded
348 * from the ntv.constant member, depending on the mode bits. If
349 * the STA_PLL bit in the status word is cleared, the state and
350 * status words are reset to the initial values at boot.
351 */
352 modes = ntv.modes;
353 if (modes) {
354 /* Check that this task is entitled to set the time or it is root */
355 if (!IOTaskHasEntitlement(current_task(), SETTIME_ENTITLEMENT)) {
356#if CONFIG_MACF
357 error = mac_system_check_settime(kauth_cred_get());
0a7de745
A
358 if (error) {
359 return error;
360 }
5ba3f43e 361#endif
0a7de745
A
362 if ((error = priv_check_cred(kauth_cred_get(), PRIV_ADJTIME, 0))) {
363 return error;
364 }
5ba3f43e
A
365 }
366 }
367
368 NTP_LOCK(enable);
369
370 if (modes & MOD_MAXERROR) {
371 clock_gettimeofday(&sec, &microsecs);
372 time_maxerror = ntv.maxerror;
373 last_time_maxerror_update = sec;
374 }
0a7de745 375 if (modes & MOD_ESTERROR) {
5ba3f43e 376 time_esterror = ntv.esterror;
0a7de745 377 }
5ba3f43e
A
378 if (modes & MOD_STATUS) {
379 if (time_status & STA_PLL && !(ntv.status & STA_PLL)) {
380 time_state = TIME_OK;
381 time_status = STA_UNSYNC;
382 }
383 time_status &= STA_RONLY;
384 time_status |= ntv.status & ~STA_RONLY;
385 /*
386 * Nor PPS or leaps seconds are supported.
387 * Filter out unsupported bits.
388 */
389 time_status &= STA_SUPPORTED;
390 }
391 if (modes & MOD_TIMECONST) {
0a7de745 392 if (ntv.constant < 0) {
5ba3f43e 393 time_constant = 0;
0a7de745 394 } else if (ntv.constant > MAXTC) {
5ba3f43e 395 time_constant = MAXTC;
0a7de745 396 } else {
5ba3f43e 397 time_constant = ntv.constant;
0a7de745 398 }
5ba3f43e
A
399 }
400 if (modes & MOD_TAI) {
0a7de745 401 if (ntv.constant > 0) {
5ba3f43e 402 time_tai = ntv.constant;
0a7de745 403 }
5ba3f43e 404 }
0a7de745 405 if (modes & MOD_NANO) {
5ba3f43e 406 time_status |= STA_NANO;
0a7de745
A
407 }
408 if (modes & MOD_MICRO) {
5ba3f43e 409 time_status &= ~STA_NANO;
0a7de745
A
410 }
411 if (modes & MOD_CLKB) {
5ba3f43e 412 time_status |= STA_CLK;
0a7de745
A
413 }
414 if (modes & MOD_CLKA) {
5ba3f43e 415 time_status &= ~STA_CLK;
0a7de745 416 }
5ba3f43e
A
417 if (modes & MOD_FREQUENCY) {
418 freq = (ntv.freq * 1000LL) >> 16;
0a7de745 419 if (freq > MAXFREQ) {
5ba3f43e 420 L_LINT(time_freq, MAXFREQ);
0a7de745 421 } else if (freq < -MAXFREQ) {
5ba3f43e 422 L_LINT(time_freq, -MAXFREQ);
0a7de745 423 } else {
5ba3f43e
A
424 /*
425 * ntv.freq is [PPM * 2^16] = [us/s * 2^16]
426 * time_freq is [ns/s * 2^32]
427 */
428 time_freq = ntv.freq * 1000LL * 65536LL;
429 }
430 }
431 if (modes & MOD_OFFSET) {
0a7de745 432 if (time_status & STA_NANO) {
5ba3f43e 433 hardupdate(ntv.offset);
0a7de745 434 } else {
5ba3f43e 435 hardupdate(ntv.offset * 1000);
0a7de745 436 }
5ba3f43e
A
437 }
438
439 ret = ntp_is_time_error(time_status) ? TIME_ERROR : time_state;
440
cb323159 441#if DEVELOPMENT || DEBUG
cc8bc92a 442 if (g_should_log_clock_adjustments) {
d9a64523 443 os_log(OS_LOG_DEFAULT, "%s: AFTER modes %u offset %lld freq %lld status %d constant %ld time_adjtime %lld\n",
0a7de745 444 __func__, modes, time_offset, time_freq, time_status, time_constant, time_adjtime);
cc8bc92a
A
445 }
446#endif
447
5ba3f43e
A
448 /*
449 * Retrieve all clock variables. Note that the TAI offset is
450 * returned only by ntp_gettime();
451 */
452 if (IS_64BIT_PROCESS(p)) {
527f9951 453 struct user64_timex user_ntv = {};
5ba3f43e 454
d9a64523 455 user_ntv.modes = modes;
0a7de745 456 if (time_status & STA_NANO) {
5ba3f43e 457 user_ntv.offset = L_GINT(time_offset);
0a7de745 458 } else {
5ba3f43e 459 user_ntv.offset = L_GINT(time_offset) / 1000;
0a7de745 460 }
5ba3f43e
A
461 user_ntv.freq = L_GINT((time_freq / 1000LL) << 16);
462 user_ntv.maxerror = time_maxerror;
463 user_ntv.esterror = time_esterror;
464 user_ntv.status = time_status;
465 user_ntv.constant = time_constant;
0a7de745 466 if (time_status & STA_NANO) {
5ba3f43e 467 user_ntv.precision = time_precision;
0a7de745 468 } else {
5ba3f43e 469 user_ntv.precision = time_precision / 1000;
0a7de745 470 }
5ba3f43e
A
471 user_ntv.tolerance = MAXFREQ * SCALE_PPM;
472
473 /* unlock before copyout */
474 NTP_UNLOCK(enable);
475
476 error = copyout(&user_ntv, uap->tp, sizeof(user_ntv));
0a7de745 477 } else {
527f9951 478 struct user32_timex user_ntv = {};
5ba3f43e 479
d9a64523 480 user_ntv.modes = modes;
0a7de745 481 if (time_status & STA_NANO) {
5ba3f43e 482 user_ntv.offset = L_GINT(time_offset);
0a7de745 483 } else {
5ba3f43e 484 user_ntv.offset = L_GINT(time_offset) / 1000;
0a7de745 485 }
5ba3f43e
A
486 user_ntv.freq = L_GINT((time_freq / 1000LL) << 16);
487 user_ntv.maxerror = time_maxerror;
488 user_ntv.esterror = time_esterror;
489 user_ntv.status = time_status;
490 user_ntv.constant = time_constant;
0a7de745 491 if (time_status & STA_NANO) {
5ba3f43e 492 user_ntv.precision = time_precision;
0a7de745 493 } else {
5ba3f43e 494 user_ntv.precision = time_precision / 1000;
0a7de745 495 }
5ba3f43e
A
496 user_ntv.tolerance = MAXFREQ * SCALE_PPM;
497
498 /* unlock before copyout */
499 NTP_UNLOCK(enable);
500
501 error = copyout(&user_ntv, uap->tp, sizeof(user_ntv));
502 }
503
0a7de745 504 if (modes) {
5ba3f43e 505 start_ntp_loop();
0a7de745 506 }
5ba3f43e 507
0a7de745 508 if (error == 0) {
5ba3f43e 509 *retval = ret;
0a7de745 510 }
5ba3f43e 511
0a7de745 512 return error;
5ba3f43e
A
513}
514
515int64_t
0a7de745
A
516ntp_get_freq(void)
517{
5ba3f43e
A
518 return time_freq;
519}
520
521/*
522 * Compute the adjustment to add to the next second.
523 */
524void
525ntp_update_second(int64_t *adjustment, clock_sec_t secs)
526{
527 int tickrate;
528 l_fp time_adj;
529 l_fp ftemp, old_time_adjtime, old_offset;
530
531 NTP_ASSERT_LOCKED();
532
533 if (secs > last_time_maxerror_update) {
0a7de745 534 time_maxerror += (MAXFREQ / 1000) * (secs - last_time_maxerror_update);
5ba3f43e
A
535 last_time_maxerror_update = secs;
536 }
537
538 old_offset = time_offset;
539 old_time_adjtime = time_adjtime;
540
541 ftemp = time_offset;
542 L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
543 time_adj = ftemp;
544 L_SUB(time_offset, ftemp);
545 L_ADD(time_adj, time_freq);
546
547 /*
548 * Apply any correction from adjtime. If more than one second
549 * off we slew at a rate of 5ms/s (5000 PPM) else 500us/s (500PPM)
550 * until the last second is slewed the final < 500 usecs.
551 */
552 if (time_adjtime != 0) {
0a7de745 553 if (time_adjtime > 1000000) {
5ba3f43e 554 tickrate = 5000;
0a7de745 555 } else if (time_adjtime < -1000000) {
5ba3f43e 556 tickrate = -5000;
0a7de745 557 } else if (time_adjtime > 500) {
5ba3f43e 558 tickrate = 500;
0a7de745 559 } else if (time_adjtime < -500) {
5ba3f43e 560 tickrate = -500;
0a7de745 561 } else {
5ba3f43e 562 tickrate = time_adjtime;
0a7de745 563 }
5ba3f43e
A
564 time_adjtime -= tickrate;
565 L_LINT(ftemp, tickrate * 1000);
566 L_ADD(time_adj, ftemp);
567 }
568
569 if (old_time_adjtime || ((time_offset || old_offset) && (time_offset != old_offset))) {
570 updated = 1;
0a7de745 571 } else {
5ba3f43e
A
572 updated = 0;
573 }
574
cb323159 575#if DEVELOPMENT || DEBUG
cc8bc92a 576 if (g_should_log_clock_adjustments) {
0a7de745
A
577 int64_t nano = (time_adj > 0)? time_adj >> 32 : -((-time_adj) >> 32);
578 int64_t frac = (time_adj > 0)? ((uint32_t) time_adj) : -((uint32_t) (-time_adj));
cc8bc92a
A
579
580 os_log(OS_LOG_DEFAULT, "%s:AFTER offset %lld (%lld) freq %lld status %d "
0a7de745
A
581 "constant %ld time_adjtime %lld nano %lld frac %lld adj %lld\n",
582 __func__, time_offset, (time_offset > 0)? time_offset >> 32 : -((-time_offset) >> 32),
583 time_freq, time_status, time_constant, time_adjtime, nano, frac, time_adj);
cc8bc92a
A
584 }
585#endif
586
5ba3f43e
A
587 *adjustment = time_adj;
588}
589
590/*
591 * hardupdate() - local clock update
592 *
593 * This routine is called by ntp_adjtime() when an offset is provided
594 * to update the local clock phase and frequency.
595 * The implementation is of an adaptive-parameter, hybrid
596 * phase/frequency-lock loop (PLL/FLL). The routine computes new
597 * time and frequency offset estimates for each call.
598 * Presumably, calls to ntp_adjtime() occur only when the caller
599 * believes the local clock is valid within some bound (+-128 ms with
600 * NTP).
601 *
602 * For uncompensated quartz crystal oscillators and nominal update
603 * intervals less than 256 s, operation should be in phase-lock mode,
604 * where the loop is disciplined to phase. For update intervals greater
605 * than 1024 s, operation should be in frequency-lock mode, where the
606 * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
607 * is selected by the STA_MODE status bit.
608 */
609static void
610hardupdate(offset)
0a7de745 611long offset;
5ba3f43e
A
612{
613 long mtemp = 0;
614 long time_monitor;
615 clock_sec_t time_uptime;
616 l_fp ftemp;
617
618 NTP_ASSERT_LOCKED();
619
0a7de745 620 if (!(time_status & STA_PLL)) {
5ba3f43e 621 return;
0a7de745 622 }
5ba3f43e 623
0a7de745 624 if (offset > MAXPHASE) {
5ba3f43e 625 time_monitor = MAXPHASE;
0a7de745 626 } else if (offset < -MAXPHASE) {
5ba3f43e 627 time_monitor = -MAXPHASE;
0a7de745 628 } else {
5ba3f43e 629 time_monitor = offset;
0a7de745 630 }
5ba3f43e
A
631 L_LINT(time_offset, time_monitor);
632
633 clock_get_calendar_uptime(&time_uptime);
634
635 if (time_status & STA_FREQHOLD || time_reftime == 0) {
636 time_reftime = time_uptime;
637 }
638
639 mtemp = time_uptime - time_reftime;
640 L_LINT(ftemp, time_monitor);
641 L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
642 L_MPY(ftemp, mtemp);
643 L_ADD(time_freq, ftemp);
644 time_status &= ~STA_MODE;
645 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp >
646 MAXSEC)) {
647 L_LINT(ftemp, (time_monitor << 4) / mtemp);
648 L_RSHIFT(ftemp, SHIFT_FLL + 4);
649 L_ADD(time_freq, ftemp);
650 time_status |= STA_MODE;
651 }
652 time_reftime = time_uptime;
653
0a7de745 654 if (L_GINT(time_freq) > MAXFREQ) {
5ba3f43e 655 L_LINT(time_freq, MAXFREQ);
0a7de745 656 } else if (L_GINT(time_freq) < -MAXFREQ) {
5ba3f43e 657 L_LINT(time_freq, -MAXFREQ);
0a7de745 658 }
5ba3f43e
A
659}
660
661
662static int
663kern_adjtime(struct timeval *delta)
664{
665 struct timeval atv;
666 int64_t ltr, ltw;
667 boolean_t enable;
668
0a7de745
A
669 if (delta == NULL) {
670 return EINVAL;
671 }
5ba3f43e
A
672
673 ltw = (int64_t)delta->tv_sec * (int64_t)USEC_PER_SEC + delta->tv_usec;
674
675 NTP_LOCK(enable);
676 ltr = time_adjtime;
677 time_adjtime = ltw;
cb323159 678#if DEVELOPMENT || DEBUG
cc8bc92a
A
679 if (g_should_log_clock_adjustments) {
680 os_log(OS_LOG_DEFAULT, "%s:AFTER offset %lld freq %lld status %d constant %ld time_adjtime %lld\n",
0a7de745 681 __func__, time_offset, time_freq, time_status, time_constant, time_adjtime);
cc8bc92a
A
682 }
683#endif
5ba3f43e
A
684 NTP_UNLOCK(enable);
685
686 atv.tv_sec = ltr / (int64_t)USEC_PER_SEC;
687 atv.tv_usec = ltr % (int64_t)USEC_PER_SEC;
688 if (atv.tv_usec < 0) {
689 atv.tv_usec += (suseconds_t)USEC_PER_SEC;
690 atv.tv_sec--;
691 }
692
693 *delta = atv;
694
695 start_ntp_loop();
696
0a7de745 697 return 0;
5ba3f43e
A
698}
699
700int
701adjtime(struct proc *p, struct adjtime_args *uap, __unused int32_t *retval)
702{
5ba3f43e
A
703 struct timeval atv;
704 int error;
705
706 /* Check that this task is entitled to set the time or it is root */
707 if (!IOTaskHasEntitlement(current_task(), SETTIME_ENTITLEMENT)) {
5ba3f43e
A
708#if CONFIG_MACF
709 error = mac_system_check_settime(kauth_cred_get());
0a7de745
A
710 if (error) {
711 return error;
712 }
5ba3f43e 713#endif
0a7de745
A
714 if ((error = priv_check_cred(kauth_cred_get(), PRIV_ADJTIME, 0))) {
715 return error;
716 }
5ba3f43e
A
717 }
718
719 if (IS_64BIT_PROCESS(p)) {
720 struct user64_timeval user_atv;
721 error = copyin(uap->delta, &user_atv, sizeof(user_atv));
722 atv.tv_sec = user_atv.tv_sec;
723 atv.tv_usec = user_atv.tv_usec;
724 } else {
725 struct user32_timeval user_atv;
726 error = copyin(uap->delta, &user_atv, sizeof(user_atv));
727 atv.tv_sec = user_atv.tv_sec;
728 atv.tv_usec = user_atv.tv_usec;
729 }
0a7de745
A
730 if (error) {
731 return error;
732 }
5ba3f43e
A
733
734 kern_adjtime(&atv);
735
736 if (uap->olddelta) {
737 if (IS_64BIT_PROCESS(p)) {
527f9951 738 struct user64_timeval user_atv = {};
5ba3f43e
A
739 user_atv.tv_sec = atv.tv_sec;
740 user_atv.tv_usec = atv.tv_usec;
741 error = copyout(&user_atv, uap->olddelta, sizeof(user_atv));
742 } else {
527f9951 743 struct user32_timeval user_atv = {};
5ba3f43e
A
744 user_atv.tv_sec = atv.tv_sec;
745 user_atv.tv_usec = atv.tv_usec;
746 error = copyout(&user_atv, uap->olddelta, sizeof(user_atv));
747 }
748 }
749
0a7de745 750 return error;
5ba3f43e
A
751}
752
753static void
754ntp_loop_update_call(void)
755{
756 boolean_t enable;
757
758 NTP_LOCK(enable);
759
760 /*
761 * Update the scale factor used by clock_calend.
762 * NOTE: clock_update_calendar will call ntp_update_second to compute the next adjustment.
763 */
764 clock_update_calendar();
765
766 refresh_ntp_loop();
767
768 NTP_UNLOCK(enable);
769}
770
771static void
772refresh_ntp_loop(void)
773{
5ba3f43e
A
774 NTP_ASSERT_LOCKED();
775 if (--ntp_loop_active == 0) {
776 /*
777 * Activate the timer only if the next second adjustment might change.
778 * ntp_update_second checks it and sets updated accordingly.
779 */
780 if (updated) {
781 clock_deadline_for_periodic_event(ntp_loop_period, mach_absolute_time(), &ntp_loop_deadline);
782
0a7de745
A
783 if (!timer_call_enter(&ntp_loop_update, ntp_loop_deadline, TIMER_CALL_SYS_CRITICAL)) {
784 ntp_loop_active++;
785 }
5ba3f43e
A
786 }
787 }
5ba3f43e
A
788}
789
790/*
791 * This function triggers a timer that each second will calculate the adjustment to
792 * provide to clock_calendar to scale the time (used by gettimeofday-family syscalls).
793 * The periodic timer will stop when the adjustment will reach a stable value.
794 */
795static void
796start_ntp_loop(void)
797{
798 boolean_t enable;
799
800 NTP_LOCK(enable);
801
802 ntp_loop_deadline = mach_absolute_time() + ntp_loop_period;
803
804 if (!timer_call_enter(&ntp_loop_update, ntp_loop_deadline, TIMER_CALL_SYS_CRITICAL)) {
0a7de745 805 ntp_loop_active++;
5ba3f43e
A
806 }
807
808 NTP_UNLOCK(enable);
809}
810
811
812static void
813init_ntp_loop(void)
814{
0a7de745 815 uint64_t abstime;
5ba3f43e
A
816
817 ntp_loop_active = 0;
818 nanoseconds_to_absolutetime(NTP_LOOP_PERIOD_INTERVAL, &abstime);
819 ntp_loop_period = (uint32_t)abstime;
820 timer_call_setup(&ntp_loop_update, (timer_call_func_t)ntp_loop_update_call, NULL);
821}
822
823void
824ntp_init(void)
825{
5ba3f43e
A
826 L_CLR(time_offset);
827 L_CLR(time_freq);
828
829 ntp_lock_grp_attr = lck_grp_attr_alloc_init();
830 ntp_lock_grp = lck_grp_alloc_init("ntp_lock", ntp_lock_grp_attr);
831 ntp_lock_attr = lck_attr_alloc_init();
832 ntp_lock = lck_spin_alloc_init(ntp_lock_grp, ntp_lock_attr);
833
834 updated = 0;
835
836 init_ntp_loop();
837}
838
839SYSINIT(ntpclocks, SI_SUB_CLOCKS, SI_ORDER_MIDDLE, ntp_init, NULL);