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