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