xref: /openbsd-src/sys/kern/kern_tc.c (revision 1fb8cdb7c769191a92d23555c8001c7507fd9257)
1 /*	$OpenBSD: kern_tc.c,v 1.66 2020/07/20 21:43:02 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 bintime boottime, old_utc, uptime, utc;
444 	struct timespec tmp;
445 	int64_t zero = 0;
446 
447 	TIMESPEC_TO_BINTIME(ts, &utc);
448 
449 	rw_enter_write(&tc_lock);
450 	mtx_enter(&windup_mtx);
451 
452 	binuptime(&uptime);
453 	bintimesub(&utc, &uptime, &boottime);
454 	bintimeadd(&timehands->th_boottime, &uptime, &old_utc);
455 	/* XXX fiddle all the little crinkly bits around the fiords... */
456 	tc_windup(&boottime, NULL, &zero);
457 
458 	mtx_leave(&windup_mtx);
459 	rw_exit_write(&tc_lock);
460 
461 	enqueue_randomness(ts->tv_sec);
462 
463 	if (timestepwarnings) {
464 		BINTIME_TO_TIMESPEC(&old_utc, &tmp);
465 		log(LOG_INFO, "Time stepped from %lld.%09ld to %lld.%09ld\n",
466 		    (long long)tmp.tv_sec, tmp.tv_nsec,
467 		    (long long)ts->tv_sec, ts->tv_nsec);
468 	}
469 }
470 
471 /*
472  * Step the monotonic and realtime clocks, triggering any timeouts that
473  * should have occurred across the interval.
474  */
475 void
476 tc_setclock(const struct timespec *ts)
477 {
478 	struct bintime elapsed, naptime, old_naptime, uptime, utc;
479 	struct timespec tmp;
480 	static int first = 1;
481 #ifndef SMALL_KERNEL
482 	long long adj_ticks;
483 #endif
484 
485 	/*
486 	 * When we're called for the first time, during boot when
487 	 * the root partition is mounted, we need to set boottime.
488 	 */
489 	if (first) {
490 		tc_setrealtimeclock(ts);
491 		first = 0;
492 		return;
493 	}
494 
495 	enqueue_randomness(ts->tv_sec);
496 
497 	TIMESPEC_TO_BINTIME(ts, &utc);
498 
499 	mtx_enter(&windup_mtx);
500 
501 	bintimesub(&utc, &timehands->th_boottime, &uptime);
502 	old_naptime = timehands->th_naptime;
503 	/* XXX fiddle all the little crinkly bits around the fiords... */
504 	tc_windup(NULL, &uptime, NULL);
505 	naptime = timehands->th_naptime;
506 
507 	mtx_leave(&windup_mtx);
508 
509 	if (bintimecmp(&old_naptime, &naptime, ==)) {
510 		BINTIME_TO_TIMESPEC(&uptime, &tmp);
511 		printf("%s: cannot rewind uptime to %lld.%09ld\n",
512 		    __func__, (long long)tmp.tv_sec, tmp.tv_nsec);
513 	}
514 
515 #ifndef SMALL_KERNEL
516 	/* convert the bintime to ticks */
517 	bintimesub(&naptime, &old_naptime, &elapsed);
518 	adj_ticks = (uint64_t)hz * elapsed.sec +
519 	    (((uint64_t)1000000 * (uint32_t)(elapsed.frac >> 32)) >> 32) / tick;
520 	if (adj_ticks > 0) {
521 		if (adj_ticks > INT_MAX)
522 			adj_ticks = INT_MAX;
523 		timeout_adjust_ticks(adj_ticks);
524 	}
525 #endif
526 }
527 
528 void
529 tc_update_timekeep(void)
530 {
531 	static struct timecounter *last_tc = NULL;
532 	struct timehands *th;
533 
534 	if (timekeep == NULL)
535 		return;
536 
537 	th = timehands;
538 	timekeep->tk_generation = 0;
539 	membar_producer();
540 	timekeep->tk_scale = th->th_scale;
541 	timekeep->tk_offset_count = th->th_offset_count;
542 	timekeep->tk_offset = th->th_offset;
543 	timekeep->tk_naptime = th->th_naptime;
544 	timekeep->tk_boottime = th->th_boottime;
545 	if (last_tc != th->th_counter) {
546 		timekeep->tk_counter_mask = th->th_counter->tc_counter_mask;
547 		timekeep->tk_user = th->th_counter->tc_user;
548 		last_tc = th->th_counter;
549 	}
550 	membar_producer();
551 	timekeep->tk_generation = th->th_generation;
552 
553 	return;
554 }
555 
556 /*
557  * Initialize the next struct timehands in the ring and make
558  * it the active timehands.  Along the way we might switch to a different
559  * timecounter and/or do seconds processing in NTP.  Slightly magic.
560  */
561 void
562 tc_windup(struct bintime *new_boottime, struct bintime *new_offset,
563     int64_t *new_adjtimedelta)
564 {
565 	struct bintime bt;
566 	struct timecounter *active_tc;
567 	struct timehands *th, *tho;
568 	u_int64_t scale;
569 	u_int delta, ncount, ogen;
570 
571 	if (new_boottime != NULL || new_adjtimedelta != NULL)
572 		rw_assert_wrlock(&tc_lock);
573 	MUTEX_ASSERT_LOCKED(&windup_mtx);
574 
575 	active_tc = timecounter;
576 
577 	/*
578 	 * Make the next timehands a copy of the current one, but do not
579 	 * overwrite the generation or next pointer.  While we update
580 	 * the contents, the generation must be zero.
581 	 */
582 	tho = timehands;
583 	ogen = tho->th_generation;
584 	th = tho->th_next;
585 	th->th_generation = 0;
586 	membar_producer();
587 	memcpy(th, tho, offsetof(struct timehands, th_generation));
588 
589 	/*
590 	 * Capture a timecounter delta on the current timecounter and if
591 	 * changing timecounters, a counter value from the new timecounter.
592 	 * Update the offset fields accordingly.
593 	 */
594 	delta = tc_delta(th);
595 	if (th->th_counter != active_tc)
596 		ncount = active_tc->tc_get_timecount(active_tc);
597 	else
598 		ncount = 0;
599 	th->th_offset_count += delta;
600 	th->th_offset_count &= th->th_counter->tc_counter_mask;
601 	bintimeaddfrac(&th->th_offset, th->th_scale * delta, &th->th_offset);
602 
603 	/*
604 	 * Ignore new offsets that predate the current offset.
605 	 * If changing the offset, first increase the naptime
606 	 * accordingly.
607 	 */
608 	if (new_offset != NULL && bintimecmp(&th->th_offset, new_offset, <)) {
609 		bintimesub(new_offset, &th->th_offset, &bt);
610 		bintimeadd(&th->th_naptime, &bt, &th->th_naptime);
611 		th->th_offset = *new_offset;
612 	}
613 
614 #ifdef notyet
615 	/*
616 	 * Hardware latching timecounters may not generate interrupts on
617 	 * PPS events, so instead we poll them.  There is a finite risk that
618 	 * the hardware might capture a count which is later than the one we
619 	 * got above, and therefore possibly in the next NTP second which might
620 	 * have a different rate than the current NTP second.  It doesn't
621 	 * matter in practice.
622 	 */
623 	if (tho->th_counter->tc_poll_pps)
624 		tho->th_counter->tc_poll_pps(tho->th_counter);
625 #endif
626 
627 	/*
628 	 * If changing the boot time or clock adjustment, do so before
629 	 * NTP processing.
630 	 */
631 	if (new_boottime != NULL)
632 		th->th_boottime = *new_boottime;
633 	if (new_adjtimedelta != NULL) {
634 		th->th_adjtimedelta = *new_adjtimedelta;
635 		/* Reset the NTP update period. */
636 		bintimesub(&th->th_offset, &th->th_naptime,
637 		    &th->th_next_ntp_update);
638 	}
639 
640 	/*
641 	 * Deal with NTP second processing.  The while-loop normally
642 	 * iterates at most once, but in extreme situations it might
643 	 * keep NTP sane if tc_windup() is not run for several seconds.
644 	 */
645 	bintimesub(&th->th_offset, &th->th_naptime, &bt);
646 	while (bintimecmp(&th->th_next_ntp_update, &bt, <=)) {
647 		ntp_update_second(th);
648 		th->th_next_ntp_update.sec++;
649 	}
650 
651 	/* Update the UTC timestamps used by the get*() functions. */
652 	bintimeadd(&th->th_boottime, &th->th_offset, &bt);
653 	BINTIME_TO_TIMEVAL(&bt, &th->th_microtime);
654 	BINTIME_TO_TIMESPEC(&bt, &th->th_nanotime);
655 
656 	/* Now is a good time to change timecounters. */
657 	if (th->th_counter != active_tc) {
658 		th->th_counter = active_tc;
659 		th->th_offset_count = ncount;
660 	}
661 
662 	/*-
663 	 * Recalculate the scaling factor.  We want the number of 1/2^64
664 	 * fractions of a second per period of the hardware counter, taking
665 	 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
666 	 * processing provides us with.
667 	 *
668 	 * The th_adjustment is nanoseconds per second with 32 bit binary
669 	 * fraction and we want 64 bit binary fraction of second:
670 	 *
671 	 *	 x = a * 2^32 / 10^9 = a * 4.294967296
672 	 *
673 	 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
674 	 * we can only multiply by about 850 without overflowing, but that
675 	 * leaves suitably precise fractions for multiply before divide.
676 	 *
677 	 * Divide before multiply with a fraction of 2199/512 results in a
678 	 * systematic undercompensation of 10PPM of th_adjustment.  On a
679 	 * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
680  	 *
681 	 * We happily sacrifice the lowest of the 64 bits of our result
682 	 * to the goddess of code clarity.
683 	 *
684 	 */
685 	scale = (u_int64_t)1 << 63;
686 	scale += \
687 	    ((th->th_adjustment + th->th_counter->tc_freq_adj) / 1024) * 2199;
688 	scale /= th->th_counter->tc_frequency;
689 	th->th_scale = scale * 2;
690 
691 	/*
692 	 * Now that the struct timehands is again consistent, set the new
693 	 * generation number, making sure to not make it zero.
694 	 */
695 	if (++ogen == 0)
696 		ogen = 1;
697 	membar_producer();
698 	th->th_generation = ogen;
699 
700 	/* Go live with the new struct timehands. */
701 	time_second = th->th_microtime.tv_sec;
702 	time_uptime = th->th_offset.sec;
703 	membar_producer();
704 	timehands = th;
705 
706 	tc_update_timekeep();
707 }
708 
709 /* Report or change the active timecounter hardware. */
710 int
711 sysctl_tc_hardware(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
712 {
713 	char newname[32];
714 	struct timecounter *newtc, *tc;
715 	int error;
716 
717 	tc = timecounter;
718 	strlcpy(newname, tc->tc_name, sizeof(newname));
719 
720 	error = sysctl_string(oldp, oldlenp, newp, newlen, newname, sizeof(newname));
721 	if (error != 0 || strcmp(newname, tc->tc_name) == 0)
722 		return (error);
723 	SLIST_FOREACH(newtc, &tc_list, tc_next) {
724 		if (strcmp(newname, newtc->tc_name) != 0)
725 			continue;
726 
727 		/* Warm up new timecounter. */
728 		(void)newtc->tc_get_timecount(newtc);
729 		(void)newtc->tc_get_timecount(newtc);
730 
731 		rw_enter_write(&tc_lock);
732 		timecounter = newtc;
733 		rw_exit_write(&tc_lock);
734 
735 		return (0);
736 	}
737 	return (EINVAL);
738 }
739 
740 /* Report or change the active timecounter hardware. */
741 int
742 sysctl_tc_choice(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
743 {
744 	char buf[32], *spc, *choices;
745 	struct timecounter *tc;
746 	int error, maxlen;
747 
748 	if (SLIST_EMPTY(&tc_list))
749 		return (sysctl_rdstring(oldp, oldlenp, newp, ""));
750 
751 	spc = "";
752 	maxlen = 0;
753 	SLIST_FOREACH(tc, &tc_list, tc_next)
754 		maxlen += sizeof(buf);
755 	choices = malloc(maxlen, M_TEMP, M_WAITOK);
756 	*choices = '\0';
757 	SLIST_FOREACH(tc, &tc_list, tc_next) {
758 		snprintf(buf, sizeof(buf), "%s%s(%d)",
759 		    spc, tc->tc_name, tc->tc_quality);
760 		spc = " ";
761 		strlcat(choices, buf, maxlen);
762 	}
763 	error = sysctl_rdstring(oldp, oldlenp, newp, choices);
764 	free(choices, M_TEMP, maxlen);
765 	return (error);
766 }
767 
768 /*
769  * Timecounters need to be updated every so often to prevent the hardware
770  * counter from overflowing.  Updating also recalculates the cached values
771  * used by the get*() family of functions, so their precision depends on
772  * the update frequency.
773  */
774 static int tc_tick;
775 
776 void
777 tc_ticktock(void)
778 {
779 	static int count;
780 
781 	if (++count < tc_tick)
782 		return;
783 	if (!mtx_enter_try(&windup_mtx))
784 		return;
785 	count = 0;
786 	tc_windup(NULL, NULL, NULL);
787 	mtx_leave(&windup_mtx);
788 }
789 
790 void
791 inittimecounter(void)
792 {
793 #ifdef DEBUG
794 	u_int p;
795 #endif
796 
797 	/*
798 	 * Set the initial timeout to
799 	 * max(1, <approx. number of hardclock ticks in a millisecond>).
800 	 * People should probably not use the sysctl to set the timeout
801 	 * to smaller than its initial value, since that value is the
802 	 * smallest reasonable one.  If they want better timestamps they
803 	 * should use the non-"get"* functions.
804 	 */
805 	if (hz > 1000)
806 		tc_tick = (hz + 500) / 1000;
807 	else
808 		tc_tick = 1;
809 #ifdef DEBUG
810 	p = (tc_tick * 1000000) / hz;
811 	printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
812 #endif
813 
814 	/* warm up new timecounter (again) and get rolling. */
815 	(void)timecounter->tc_get_timecount(timecounter);
816 	(void)timecounter->tc_get_timecount(timecounter);
817 }
818 
819 /*
820  * Return timecounter-related information.
821  */
822 int
823 sysctl_tc(int *name, u_int namelen, void *oldp, size_t *oldlenp,
824     void *newp, size_t newlen)
825 {
826 	if (namelen != 1)
827 		return (ENOTDIR);
828 
829 	switch (name[0]) {
830 	case KERN_TIMECOUNTER_TICK:
831 		return (sysctl_rdint(oldp, oldlenp, newp, tc_tick));
832 	case KERN_TIMECOUNTER_TIMESTEPWARNINGS:
833 		return (sysctl_int(oldp, oldlenp, newp, newlen,
834 		    &timestepwarnings));
835 	case KERN_TIMECOUNTER_HARDWARE:
836 		return (sysctl_tc_hardware(oldp, oldlenp, newp, newlen));
837 	case KERN_TIMECOUNTER_CHOICE:
838 		return (sysctl_tc_choice(oldp, oldlenp, newp, newlen));
839 	default:
840 		return (EOPNOTSUPP);
841 	}
842 	/* NOTREACHED */
843 }
844 
845 /*
846  * Skew the timehands according to any adjtime(2) adjustment.
847  */
848 void
849 ntp_update_second(struct timehands *th)
850 {
851 	int64_t adj;
852 
853 	MUTEX_ASSERT_LOCKED(&windup_mtx);
854 
855 	if (th->th_adjtimedelta > 0)
856 		adj = MIN(5000, th->th_adjtimedelta);
857 	else
858 		adj = MAX(-5000, th->th_adjtimedelta);
859 	th->th_adjtimedelta -= adj;
860 	th->th_adjustment = (adj * 1000) << 32;
861 }
862 
863 void
864 tc_adjfreq(int64_t *old, int64_t *new)
865 {
866 	if (old != NULL) {
867 		rw_assert_anylock(&tc_lock);
868 		*old = timecounter->tc_freq_adj;
869 	}
870 	if (new != NULL) {
871 		rw_assert_wrlock(&tc_lock);
872 		mtx_enter(&windup_mtx);
873 		timecounter->tc_freq_adj = *new;
874 		tc_windup(NULL, NULL, NULL);
875 		mtx_leave(&windup_mtx);
876 	}
877 }
878 
879 void
880 tc_adjtime(int64_t *old, int64_t *new)
881 {
882 	struct timehands *th;
883 	u_int gen;
884 
885 	if (old != NULL) {
886 		do {
887 			th = timehands;
888 			gen = th->th_generation;
889 			membar_consumer();
890 			*old = th->th_adjtimedelta;
891 			membar_consumer();
892 		} while (gen == 0 || gen != th->th_generation);
893 	}
894 	if (new != NULL) {
895 		rw_assert_wrlock(&tc_lock);
896 		mtx_enter(&windup_mtx);
897 		tc_windup(NULL, NULL, new);
898 		mtx_leave(&windup_mtx);
899 	}
900 }
901