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