xref: /openbsd-src/sys/kern/kern_tc.c (revision 4ea724981d715fa6d48d9259aab2fa5feb58d23d)
1 /*	$OpenBSD: kern_tc.c,v 1.73 2021/06/15 05:24:46 dlg Exp $ */
2 
3 /*
4  * Copyright (c) 2000 Poul-Henning Kamp <phk@FreeBSD.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * If we meet some day, and you think this stuff is worth it, you
21  * can buy me a beer in return. Poul-Henning Kamp
22  */
23 
24 #include <sys/param.h>
25 #include <sys/atomic.h>
26 #include <sys/kernel.h>
27 #include <sys/mutex.h>
28 #include <sys/rwlock.h>
29 #include <sys/stdint.h>
30 #include <sys/timeout.h>
31 #include <sys/sysctl.h>
32 #include <sys/syslog.h>
33 #include <sys/systm.h>
34 #include <sys/timetc.h>
35 #include <sys/queue.h>
36 #include <sys/malloc.h>
37 
38 u_int dummy_get_timecount(struct timecounter *);
39 
40 int sysctl_tc_hardware(void *, size_t *, void *, size_t);
41 int sysctl_tc_choice(void *, size_t *, void *, size_t);
42 
43 /*
44  * Implement a dummy timecounter which we can use until we get a real one
45  * in the air.  This allows the console and other early stuff to use
46  * time services.
47  */
48 
49 u_int
50 dummy_get_timecount(struct timecounter *tc)
51 {
52 	static u_int now;
53 
54 	return atomic_inc_int_nv(&now);
55 }
56 
57 static struct timecounter dummy_timecounter = {
58 	.tc_get_timecount = dummy_get_timecount,
59 	.tc_poll_pps = 0,
60 	.tc_counter_mask = ~0u,
61 	.tc_frequency = 1000000,
62 	.tc_name = "dummy",
63 	.tc_quality = -1000000,
64 	.tc_priv = NULL,
65 	.tc_user = 0,
66 };
67 
68 /*
69  * Locks used to protect struct members, global variables in this file:
70  *	I	immutable after initialization
71  *	T	tc_lock
72  *	W	windup_mtx
73  */
74 
75 struct timehands {
76 	/* These fields must be initialized by the driver. */
77 	struct timecounter	*th_counter;		/* [W] */
78 	int64_t			th_adjtimedelta;	/* [T,W] */
79 	struct bintime		th_next_ntp_update;	/* [T,W] */
80 	int64_t			th_adjustment;		/* [W] */
81 	u_int64_t		th_scale;		/* [W] */
82 	u_int	 		th_offset_count;	/* [W] */
83 	struct bintime		th_boottime;		/* [T,W] */
84 	struct bintime		th_offset;		/* [W] */
85 	struct bintime		th_naptime;		/* [W] */
86 	struct timeval		th_microtime;		/* [W] */
87 	struct timespec		th_nanotime;		/* [W] */
88 	/* Fields not to be copied in tc_windup start with th_generation. */
89 	volatile u_int		th_generation;		/* [W] */
90 	struct timehands	*th_next;		/* [I] */
91 };
92 
93 static struct timehands th0;
94 static struct timehands th1 = {
95 	.th_next = &th0
96 };
97 static struct timehands th0 = {
98 	.th_counter = &dummy_timecounter,
99 	.th_scale = UINT64_MAX / 1000000,
100 	.th_offset = { .sec = 1, .frac = 0 },
101 	.th_generation = 1,
102 	.th_next = &th1
103 };
104 
105 struct rwlock tc_lock = RWLOCK_INITIALIZER("tc_lock");
106 
107 /*
108  * tc_windup() must be called before leaving this mutex.
109  */
110 struct mutex windup_mtx = MUTEX_INITIALIZER(IPL_CLOCK);
111 
112 static struct timehands *volatile timehands = &th0;		/* [W] */
113 struct timecounter *timecounter = &dummy_timecounter;		/* [T] */
114 static SLIST_HEAD(, timecounter) tc_list = SLIST_HEAD_INITIALIZER(tc_list);
115 
116 /*
117  * These are updated from tc_windup().  They are useful when
118  * examining kernel core dumps.
119  */
120 volatile time_t naptime = 0;
121 volatile time_t time_second = 1;
122 volatile time_t time_uptime = 0;
123 
124 static int timestepwarnings;
125 
126 void ntp_update_second(struct timehands *);
127 void tc_windup(struct bintime *, struct bintime *, int64_t *);
128 
129 /*
130  * Return the difference between the timehands' counter value now and what
131  * was when we copied it to the timehands' offset_count.
132  */
133 static __inline u_int
134 tc_delta(struct timehands *th)
135 {
136 	struct timecounter *tc;
137 
138 	tc = th->th_counter;
139 	return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
140 	    tc->tc_counter_mask);
141 }
142 
143 /*
144  * Functions for reading the time.  We have to loop until we are sure that
145  * the timehands that we operated on was not updated under our feet.  See
146  * the comment in <sys/time.h> for a description of these functions.
147  */
148 
149 void
150 binboottime(struct bintime *bt)
151 {
152 	struct timehands *th;
153 	u_int gen;
154 
155 	do {
156 		th = timehands;
157 		gen = th->th_generation;
158 		membar_consumer();
159 		*bt = th->th_boottime;
160 		membar_consumer();
161 	} while (gen == 0 || gen != th->th_generation);
162 }
163 
164 void
165 microboottime(struct timeval *tvp)
166 {
167 	struct bintime bt;
168 
169 	binboottime(&bt);
170 	BINTIME_TO_TIMEVAL(&bt, tvp);
171 }
172 
173 void
174 nanoboottime(struct timespec *tsp)
175 {
176 	struct bintime bt;
177 
178 	binboottime(&bt);
179 	BINTIME_TO_TIMESPEC(&bt, tsp);
180 }
181 
182 void
183 binuptime(struct bintime *bt)
184 {
185 	struct timehands *th;
186 	u_int gen;
187 
188 	do {
189 		th = timehands;
190 		gen = th->th_generation;
191 		membar_consumer();
192 		*bt = th->th_offset;
193 		bintimeaddfrac(bt, th->th_scale * tc_delta(th), bt);
194 		membar_consumer();
195 	} while (gen == 0 || gen != th->th_generation);
196 }
197 
198 void
199 getbinuptime(struct bintime *bt)
200 {
201 	struct timehands *th;
202 	u_int gen;
203 
204 	do {
205 		th = timehands;
206 		gen = th->th_generation;
207 		membar_consumer();
208 		*bt = th->th_offset;
209 		membar_consumer();
210 	} while (gen == 0 || gen != th->th_generation);
211 }
212 
213 void
214 nanouptime(struct timespec *tsp)
215 {
216 	struct bintime bt;
217 
218 	binuptime(&bt);
219 	BINTIME_TO_TIMESPEC(&bt, tsp);
220 }
221 
222 void
223 microuptime(struct timeval *tvp)
224 {
225 	struct bintime bt;
226 
227 	binuptime(&bt);
228 	BINTIME_TO_TIMEVAL(&bt, tvp);
229 }
230 
231 time_t
232 getuptime(void)
233 {
234 #if defined(__LP64__)
235 	return time_uptime;	/* atomic */
236 #else
237 	time_t now;
238 	struct timehands *th;
239 	u_int gen;
240 
241 	do {
242 		th = timehands;
243 		gen = th->th_generation;
244 		membar_consumer();
245 		now = th->th_offset.sec;
246 		membar_consumer();
247 	} while (gen == 0 || gen != th->th_generation);
248 
249 	return now;
250 #endif
251 }
252 
253 uint64_t
254 nsecuptime(void)
255 {
256 	struct bintime bt;
257 	uint64_t nsec;
258 
259 	binuptime(&bt);
260 
261 	nsec = (1000000000ULL * (bt.frac >> 32)) >> 32;
262 	nsec += bt.sec * 1000000000ULL;
263 
264 	return (nsec);
265 }
266 
267 uint64_t
268 getnsecuptime(void)
269 {
270 	struct bintime bt;
271 	uint64_t nsec;
272 
273 	getbinuptime(&bt);
274 
275 	nsec = (1000000000ULL * (bt.frac >> 32)) >> 32;
276 	nsec += bt.sec * 1000000000ULL;
277 
278 	return (nsec);
279 }
280 
281 void
282 binruntime(struct bintime *bt)
283 {
284 	struct timehands *th;
285 	u_int gen;
286 
287 	do {
288 		th = timehands;
289 		gen = th->th_generation;
290 		membar_consumer();
291 		bintimeaddfrac(&th->th_offset, th->th_scale * tc_delta(th), bt);
292 		bintimesub(bt, &th->th_naptime, bt);
293 		membar_consumer();
294 	} while (gen == 0 || gen != th->th_generation);
295 }
296 
297 void
298 nanoruntime(struct timespec *ts)
299 {
300 	struct bintime bt;
301 
302 	binruntime(&bt);
303 	BINTIME_TO_TIMESPEC(&bt, ts);
304 }
305 
306 void
307 bintime(struct bintime *bt)
308 {
309 	struct timehands *th;
310 	u_int gen;
311 
312 	do {
313 		th = timehands;
314 		gen = th->th_generation;
315 		membar_consumer();
316 		*bt = th->th_offset;
317 		bintimeaddfrac(bt, th->th_scale * tc_delta(th), bt);
318 		bintimeadd(bt, &th->th_boottime, bt);
319 		membar_consumer();
320 	} while (gen == 0 || gen != th->th_generation);
321 }
322 
323 void
324 nanotime(struct timespec *tsp)
325 {
326 	struct bintime bt;
327 
328 	bintime(&bt);
329 	BINTIME_TO_TIMESPEC(&bt, tsp);
330 }
331 
332 void
333 microtime(struct timeval *tvp)
334 {
335 	struct bintime bt;
336 
337 	bintime(&bt);
338 	BINTIME_TO_TIMEVAL(&bt, tvp);
339 }
340 
341 time_t
342 gettime(void)
343 {
344 #if defined(__LP64__)
345 	return time_second;	/* atomic */
346 #else
347 	time_t now;
348 	struct timehands *th;
349 	u_int gen;
350 
351 	do {
352 		th = timehands;
353 		gen = th->th_generation;
354 		membar_consumer();
355 		now = th->th_microtime.tv_sec;
356 		membar_consumer();
357 	} while (gen == 0 || gen != th->th_generation);
358 
359 	return now;
360 #endif
361 }
362 
363 void
364 getnanouptime(struct timespec *tsp)
365 {
366 	struct timehands *th;
367 	u_int gen;
368 
369 	do {
370 		th = timehands;
371 		gen = th->th_generation;
372 		membar_consumer();
373 		BINTIME_TO_TIMESPEC(&th->th_offset, tsp);
374 		membar_consumer();
375 	} while (gen == 0 || gen != th->th_generation);
376 }
377 
378 void
379 getmicrouptime(struct timeval *tvp)
380 {
381 	struct timehands *th;
382 	u_int gen;
383 
384 	do {
385 		th = timehands;
386 		gen = th->th_generation;
387 		membar_consumer();
388 		BINTIME_TO_TIMEVAL(&th->th_offset, tvp);
389 		membar_consumer();
390 	} while (gen == 0 || gen != th->th_generation);
391 }
392 
393 void
394 getnanotime(struct timespec *tsp)
395 {
396 	struct timehands *th;
397 	u_int gen;
398 
399 	do {
400 		th = timehands;
401 		gen = th->th_generation;
402 		membar_consumer();
403 		*tsp = th->th_nanotime;
404 		membar_consumer();
405 	} while (gen == 0 || gen != th->th_generation);
406 }
407 
408 void
409 getmicrotime(struct timeval *tvp)
410 {
411 	struct timehands *th;
412 	u_int gen;
413 
414 	do {
415 		th = timehands;
416 		gen = th->th_generation;
417 		membar_consumer();
418 		*tvp = th->th_microtime;
419 		membar_consumer();
420 	} while (gen == 0 || gen != th->th_generation);
421 }
422 
423 /*
424  * Initialize a new timecounter and possibly use it.
425  */
426 void
427 tc_init(struct timecounter *tc)
428 {
429 	u_int64_t tmp;
430 	u_int u;
431 
432 	u = tc->tc_frequency / tc->tc_counter_mask;
433 	/* XXX: We need some margin here, 10% is a guess */
434 	u *= 11;
435 	u /= 10;
436 	if (tc->tc_quality >= 0) {
437 		if (u > hz) {
438 			tc->tc_quality = -2000;
439 			printf("Timecounter \"%s\" frequency %lu Hz",
440 			    tc->tc_name, (unsigned long)tc->tc_frequency);
441 			printf(" -- Insufficient hz, needs at least %u\n", u);
442 		}
443 	}
444 
445 	/* Determine the counter's precision. */
446 	for (tmp = 1; (tmp & tc->tc_counter_mask) == 0; tmp <<= 1)
447 		continue;
448 	tc->tc_precision = tmp;
449 
450 	SLIST_INSERT_HEAD(&tc_list, tc, tc_next);
451 
452 	/*
453 	 * Never automatically use a timecounter with negative quality.
454 	 * Even though we run on the dummy counter, switching here may be
455 	 * worse since this timecounter may not be monotonic.
456 	 */
457 	if (tc->tc_quality < 0)
458 		return;
459 	if (tc->tc_quality < timecounter->tc_quality)
460 		return;
461 	if (tc->tc_quality == timecounter->tc_quality &&
462 	    tc->tc_frequency < timecounter->tc_frequency)
463 		return;
464 	(void)tc->tc_get_timecount(tc);
465 	enqueue_randomness(tc->tc_get_timecount(tc));
466 
467 	timecounter = tc;
468 }
469 
470 /* Report the frequency of the current timecounter. */
471 u_int64_t
472 tc_getfrequency(void)
473 {
474 	return (timehands->th_counter->tc_frequency);
475 }
476 
477 /* Report the precision of the current timecounter. */
478 u_int64_t
479 tc_getprecision(void)
480 {
481 	return (timehands->th_counter->tc_precision);
482 }
483 
484 /*
485  * Step our concept of UTC, aka the realtime clock.
486  * This is done by modifying our estimate of when we booted.
487  *
488  * Any ongoing adjustment is meaningless after a clock jump,
489  * so we zero adjtimedelta here as well.
490  */
491 void
492 tc_setrealtimeclock(const struct timespec *ts)
493 {
494 	struct bintime boottime, old_utc, uptime, utc;
495 	struct timespec tmp;
496 	int64_t zero = 0;
497 
498 	TIMESPEC_TO_BINTIME(ts, &utc);
499 
500 	rw_enter_write(&tc_lock);
501 	mtx_enter(&windup_mtx);
502 
503 	binuptime(&uptime);
504 	bintimesub(&utc, &uptime, &boottime);
505 	bintimeadd(&timehands->th_boottime, &uptime, &old_utc);
506 	/* XXX fiddle all the little crinkly bits around the fiords... */
507 	tc_windup(&boottime, NULL, &zero);
508 
509 	mtx_leave(&windup_mtx);
510 	rw_exit_write(&tc_lock);
511 
512 	enqueue_randomness(ts->tv_sec);
513 
514 	if (timestepwarnings) {
515 		BINTIME_TO_TIMESPEC(&old_utc, &tmp);
516 		log(LOG_INFO, "Time stepped from %lld.%09ld to %lld.%09ld\n",
517 		    (long long)tmp.tv_sec, tmp.tv_nsec,
518 		    (long long)ts->tv_sec, ts->tv_nsec);
519 	}
520 }
521 
522 /*
523  * Step the monotonic and realtime clocks, triggering any timeouts that
524  * should have occurred across the interval.
525  */
526 void
527 tc_setclock(const struct timespec *ts)
528 {
529 	struct bintime new_naptime, old_naptime, uptime, utc;
530 	struct timespec tmp;
531 	static int first = 1;
532 #ifndef SMALL_KERNEL
533 	struct bintime elapsed;
534 	long long adj_ticks;
535 #endif
536 
537 	/*
538 	 * When we're called for the first time, during boot when
539 	 * the root partition is mounted, we need to set boottime.
540 	 */
541 	if (first) {
542 		tc_setrealtimeclock(ts);
543 		first = 0;
544 		return;
545 	}
546 
547 	enqueue_randomness(ts->tv_sec);
548 
549 	TIMESPEC_TO_BINTIME(ts, &utc);
550 
551 	mtx_enter(&windup_mtx);
552 
553 	bintimesub(&utc, &timehands->th_boottime, &uptime);
554 	old_naptime = timehands->th_naptime;
555 	/* XXX fiddle all the little crinkly bits around the fiords... */
556 	tc_windup(NULL, &uptime, NULL);
557 	new_naptime = timehands->th_naptime;
558 
559 	mtx_leave(&windup_mtx);
560 
561 	if (bintimecmp(&old_naptime, &new_naptime, ==)) {
562 		BINTIME_TO_TIMESPEC(&uptime, &tmp);
563 		printf("%s: cannot rewind uptime to %lld.%09ld\n",
564 		    __func__, (long long)tmp.tv_sec, tmp.tv_nsec);
565 	}
566 
567 #ifndef SMALL_KERNEL
568 	/* convert the bintime to ticks */
569 	bintimesub(&new_naptime, &old_naptime, &elapsed);
570 	adj_ticks = (uint64_t)hz * elapsed.sec +
571 	    (((uint64_t)1000000 * (uint32_t)(elapsed.frac >> 32)) >> 32) / tick;
572 	if (adj_ticks > 0) {
573 		if (adj_ticks > INT_MAX)
574 			adj_ticks = INT_MAX;
575 		timeout_adjust_ticks(adj_ticks);
576 	}
577 #endif
578 }
579 
580 void
581 tc_update_timekeep(void)
582 {
583 	static struct timecounter *last_tc = NULL;
584 	struct timehands *th;
585 
586 	MUTEX_ASSERT_LOCKED(&windup_mtx);
587 
588 	if (timekeep == NULL)
589 		return;
590 
591 	th = timehands;
592 	timekeep->tk_generation = 0;
593 	membar_producer();
594 	timekeep->tk_scale = th->th_scale;
595 	timekeep->tk_offset_count = th->th_offset_count;
596 	timekeep->tk_offset = th->th_offset;
597 	timekeep->tk_naptime = th->th_naptime;
598 	timekeep->tk_boottime = th->th_boottime;
599 	if (last_tc != th->th_counter) {
600 		timekeep->tk_counter_mask = th->th_counter->tc_counter_mask;
601 		timekeep->tk_user = th->th_counter->tc_user;
602 		last_tc = th->th_counter;
603 	}
604 	membar_producer();
605 	timekeep->tk_generation = th->th_generation;
606 
607 	return;
608 }
609 
610 /*
611  * Initialize the next struct timehands in the ring and make
612  * it the active timehands.  Along the way we might switch to a different
613  * timecounter and/or do seconds processing in NTP.  Slightly magic.
614  */
615 void
616 tc_windup(struct bintime *new_boottime, struct bintime *new_offset,
617     int64_t *new_adjtimedelta)
618 {
619 	struct bintime bt;
620 	struct timecounter *active_tc;
621 	struct timehands *th, *tho;
622 	u_int64_t scale;
623 	u_int delta, ncount, ogen;
624 
625 	if (new_boottime != NULL || new_adjtimedelta != NULL)
626 		rw_assert_wrlock(&tc_lock);
627 	MUTEX_ASSERT_LOCKED(&windup_mtx);
628 
629 	active_tc = timecounter;
630 
631 	/*
632 	 * Make the next timehands a copy of the current one, but do not
633 	 * overwrite the generation or next pointer.  While we update
634 	 * the contents, the generation must be zero.
635 	 */
636 	tho = timehands;
637 	ogen = tho->th_generation;
638 	th = tho->th_next;
639 	th->th_generation = 0;
640 	membar_producer();
641 	memcpy(th, tho, offsetof(struct timehands, th_generation));
642 
643 	/*
644 	 * Capture a timecounter delta on the current timecounter and if
645 	 * changing timecounters, a counter value from the new timecounter.
646 	 * Update the offset fields accordingly.
647 	 */
648 	delta = tc_delta(th);
649 	if (th->th_counter != active_tc)
650 		ncount = active_tc->tc_get_timecount(active_tc);
651 	else
652 		ncount = 0;
653 	th->th_offset_count += delta;
654 	th->th_offset_count &= th->th_counter->tc_counter_mask;
655 	bintimeaddfrac(&th->th_offset, th->th_scale * delta, &th->th_offset);
656 
657 	/*
658 	 * Ignore new offsets that predate the current offset.
659 	 * If changing the offset, first increase the naptime
660 	 * accordingly.
661 	 */
662 	if (new_offset != NULL && bintimecmp(&th->th_offset, new_offset, <)) {
663 		bintimesub(new_offset, &th->th_offset, &bt);
664 		bintimeadd(&th->th_naptime, &bt, &th->th_naptime);
665 		naptime = th->th_naptime.sec;
666 		th->th_offset = *new_offset;
667 	}
668 
669 #ifdef notyet
670 	/*
671 	 * Hardware latching timecounters may not generate interrupts on
672 	 * PPS events, so instead we poll them.  There is a finite risk that
673 	 * the hardware might capture a count which is later than the one we
674 	 * got above, and therefore possibly in the next NTP second which might
675 	 * have a different rate than the current NTP second.  It doesn't
676 	 * matter in practice.
677 	 */
678 	if (tho->th_counter->tc_poll_pps)
679 		tho->th_counter->tc_poll_pps(tho->th_counter);
680 #endif
681 
682 	/*
683 	 * If changing the boot time or clock adjustment, do so before
684 	 * NTP processing.
685 	 */
686 	if (new_boottime != NULL)
687 		th->th_boottime = *new_boottime;
688 	if (new_adjtimedelta != NULL) {
689 		th->th_adjtimedelta = *new_adjtimedelta;
690 		/* Reset the NTP update period. */
691 		bintimesub(&th->th_offset, &th->th_naptime,
692 		    &th->th_next_ntp_update);
693 	}
694 
695 	/*
696 	 * Deal with NTP second processing.  The while-loop normally
697 	 * iterates at most once, but in extreme situations it might
698 	 * keep NTP sane if tc_windup() is not run for several seconds.
699 	 */
700 	bintimesub(&th->th_offset, &th->th_naptime, &bt);
701 	while (bintimecmp(&th->th_next_ntp_update, &bt, <=)) {
702 		ntp_update_second(th);
703 		th->th_next_ntp_update.sec++;
704 	}
705 
706 	/* Update the UTC timestamps used by the get*() functions. */
707 	bintimeadd(&th->th_boottime, &th->th_offset, &bt);
708 	BINTIME_TO_TIMEVAL(&bt, &th->th_microtime);
709 	BINTIME_TO_TIMESPEC(&bt, &th->th_nanotime);
710 
711 	/* Now is a good time to change timecounters. */
712 	if (th->th_counter != active_tc) {
713 		th->th_counter = active_tc;
714 		th->th_offset_count = ncount;
715 	}
716 
717 	/*-
718 	 * Recalculate the scaling factor.  We want the number of 1/2^64
719 	 * fractions of a second per period of the hardware counter, taking
720 	 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
721 	 * processing provides us with.
722 	 *
723 	 * The th_adjustment is nanoseconds per second with 32 bit binary
724 	 * fraction and we want 64 bit binary fraction of second:
725 	 *
726 	 *	 x = a * 2^32 / 10^9 = a * 4.294967296
727 	 *
728 	 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
729 	 * we can only multiply by about 850 without overflowing, but that
730 	 * leaves suitably precise fractions for multiply before divide.
731 	 *
732 	 * Divide before multiply with a fraction of 2199/512 results in a
733 	 * systematic undercompensation of 10PPM of th_adjustment.  On a
734 	 * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
735  	 *
736 	 * We happily sacrifice the lowest of the 64 bits of our result
737 	 * to the goddess of code clarity.
738 	 *
739 	 */
740 	scale = (u_int64_t)1 << 63;
741 	scale += \
742 	    ((th->th_adjustment + th->th_counter->tc_freq_adj) / 1024) * 2199;
743 	scale /= th->th_counter->tc_frequency;
744 	th->th_scale = scale * 2;
745 
746 	/*
747 	 * Now that the struct timehands is again consistent, set the new
748 	 * generation number, making sure to not make it zero.
749 	 */
750 	if (++ogen == 0)
751 		ogen = 1;
752 	membar_producer();
753 	th->th_generation = ogen;
754 
755 	/* Go live with the new struct timehands. */
756 	time_second = th->th_microtime.tv_sec;
757 	time_uptime = th->th_offset.sec;
758 	membar_producer();
759 	timehands = th;
760 
761 	tc_update_timekeep();
762 }
763 
764 /* Report or change the active timecounter hardware. */
765 int
766 sysctl_tc_hardware(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
767 {
768 	char newname[32];
769 	struct timecounter *newtc, *tc;
770 	int error;
771 
772 	tc = timecounter;
773 	strlcpy(newname, tc->tc_name, sizeof(newname));
774 
775 	error = sysctl_string(oldp, oldlenp, newp, newlen, newname, sizeof(newname));
776 	if (error != 0 || strcmp(newname, tc->tc_name) == 0)
777 		return (error);
778 	SLIST_FOREACH(newtc, &tc_list, tc_next) {
779 		if (strcmp(newname, newtc->tc_name) != 0)
780 			continue;
781 
782 		/* Warm up new timecounter. */
783 		(void)newtc->tc_get_timecount(newtc);
784 		(void)newtc->tc_get_timecount(newtc);
785 
786 		rw_enter_write(&tc_lock);
787 		timecounter = newtc;
788 		rw_exit_write(&tc_lock);
789 
790 		return (0);
791 	}
792 	return (EINVAL);
793 }
794 
795 /* Report or change the active timecounter hardware. */
796 int
797 sysctl_tc_choice(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
798 {
799 	char buf[32], *spc, *choices;
800 	struct timecounter *tc;
801 	int error, maxlen;
802 
803 	if (SLIST_EMPTY(&tc_list))
804 		return (sysctl_rdstring(oldp, oldlenp, newp, ""));
805 
806 	spc = "";
807 	maxlen = 0;
808 	SLIST_FOREACH(tc, &tc_list, tc_next)
809 		maxlen += sizeof(buf);
810 	choices = malloc(maxlen, M_TEMP, M_WAITOK);
811 	*choices = '\0';
812 	SLIST_FOREACH(tc, &tc_list, tc_next) {
813 		snprintf(buf, sizeof(buf), "%s%s(%d)",
814 		    spc, tc->tc_name, tc->tc_quality);
815 		spc = " ";
816 		strlcat(choices, buf, maxlen);
817 	}
818 	error = sysctl_rdstring(oldp, oldlenp, newp, choices);
819 	free(choices, M_TEMP, maxlen);
820 	return (error);
821 }
822 
823 /*
824  * Timecounters need to be updated every so often to prevent the hardware
825  * counter from overflowing.  Updating also recalculates the cached values
826  * used by the get*() family of functions, so their precision depends on
827  * the update frequency.
828  */
829 static int tc_tick;
830 
831 void
832 tc_ticktock(void)
833 {
834 	static int count;
835 
836 	if (++count < tc_tick)
837 		return;
838 	if (!mtx_enter_try(&windup_mtx))
839 		return;
840 	count = 0;
841 	tc_windup(NULL, NULL, NULL);
842 	mtx_leave(&windup_mtx);
843 }
844 
845 void
846 inittimecounter(void)
847 {
848 #ifdef DEBUG
849 	u_int p;
850 #endif
851 
852 	/*
853 	 * Set the initial timeout to
854 	 * max(1, <approx. number of hardclock ticks in a millisecond>).
855 	 * People should probably not use the sysctl to set the timeout
856 	 * to smaller than its initial value, since that value is the
857 	 * smallest reasonable one.  If they want better timestamps they
858 	 * should use the non-"get"* functions.
859 	 */
860 	if (hz > 1000)
861 		tc_tick = (hz + 500) / 1000;
862 	else
863 		tc_tick = 1;
864 #ifdef DEBUG
865 	p = (tc_tick * 1000000) / hz;
866 	printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
867 #endif
868 
869 	/* warm up new timecounter (again) and get rolling. */
870 	(void)timecounter->tc_get_timecount(timecounter);
871 	(void)timecounter->tc_get_timecount(timecounter);
872 }
873 
874 const struct sysctl_bounded_args tc_vars[] = {
875 	{ KERN_TIMECOUNTER_TICK, &tc_tick, SYSCTL_INT_READONLY },
876 	{ KERN_TIMECOUNTER_TIMESTEPWARNINGS, &timestepwarnings, 0, 1 },
877 };
878 
879 /*
880  * Return timecounter-related information.
881  */
882 int
883 sysctl_tc(int *name, u_int namelen, void *oldp, size_t *oldlenp,
884     void *newp, size_t newlen)
885 {
886 	if (namelen != 1)
887 		return (ENOTDIR);
888 
889 	switch (name[0]) {
890 	case KERN_TIMECOUNTER_HARDWARE:
891 		return (sysctl_tc_hardware(oldp, oldlenp, newp, newlen));
892 	case KERN_TIMECOUNTER_CHOICE:
893 		return (sysctl_tc_choice(oldp, oldlenp, newp, newlen));
894 	default:
895 		return (sysctl_bounded_arr(tc_vars, nitems(tc_vars), name,
896 		    namelen, oldp, oldlenp, newp, newlen));
897 	}
898 	/* NOTREACHED */
899 }
900 
901 /*
902  * Skew the timehands according to any adjtime(2) adjustment.
903  */
904 void
905 ntp_update_second(struct timehands *th)
906 {
907 	int64_t adj;
908 
909 	MUTEX_ASSERT_LOCKED(&windup_mtx);
910 
911 	if (th->th_adjtimedelta > 0)
912 		adj = MIN(5000, th->th_adjtimedelta);
913 	else
914 		adj = MAX(-5000, th->th_adjtimedelta);
915 	th->th_adjtimedelta -= adj;
916 	th->th_adjustment = (adj * 1000) << 32;
917 }
918 
919 void
920 tc_adjfreq(int64_t *old, int64_t *new)
921 {
922 	if (old != NULL) {
923 		rw_assert_anylock(&tc_lock);
924 		*old = timecounter->tc_freq_adj;
925 	}
926 	if (new != NULL) {
927 		rw_assert_wrlock(&tc_lock);
928 		mtx_enter(&windup_mtx);
929 		timecounter->tc_freq_adj = *new;
930 		tc_windup(NULL, NULL, NULL);
931 		mtx_leave(&windup_mtx);
932 	}
933 }
934 
935 void
936 tc_adjtime(int64_t *old, int64_t *new)
937 {
938 	struct timehands *th;
939 	u_int gen;
940 
941 	if (old != NULL) {
942 		do {
943 			th = timehands;
944 			gen = th->th_generation;
945 			membar_consumer();
946 			*old = th->th_adjtimedelta;
947 			membar_consumer();
948 		} while (gen == 0 || gen != th->th_generation);
949 	}
950 	if (new != NULL) {
951 		rw_assert_wrlock(&tc_lock);
952 		mtx_enter(&windup_mtx);
953 		tc_windup(NULL, NULL, new);
954 		mtx_leave(&windup_mtx);
955 	}
956 }
957