xref: /freebsd/sys/kern/kern_tc.c (revision 80ff58b89dcacfe07fe20b045890df9db5ca0af0)
1 /*-
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * Copyright (c) 2011 The FreeBSD Foundation
10  * All rights reserved.
11  *
12  * Portions of this software were developed by Julien Ridoux at the University
13  * of Melbourne under sponsorship from the FreeBSD Foundation.
14  */
15 
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
18 
19 #include "opt_compat.h"
20 #include "opt_ntp.h"
21 #include "opt_ffclock.h"
22 
23 #include <sys/param.h>
24 #include <sys/kernel.h>
25 #ifdef FFCLOCK
26 #include <sys/lock.h>
27 #include <sys/mutex.h>
28 #endif
29 #include <sys/sysctl.h>
30 #include <sys/syslog.h>
31 #include <sys/systm.h>
32 #include <sys/timeffc.h>
33 #include <sys/timepps.h>
34 #include <sys/timetc.h>
35 #include <sys/timex.h>
36 #include <sys/vdso.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 /*
47  * Implement a dummy timecounter which we can use until we get a real one
48  * in the air.  This allows the console and other early stuff to use
49  * time services.
50  */
51 
52 static u_int
53 dummy_get_timecount(struct timecounter *tc)
54 {
55 	static u_int now;
56 
57 	return (++now);
58 }
59 
60 static struct timecounter dummy_timecounter = {
61 	dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
62 };
63 
64 struct timehands {
65 	/* These fields must be initialized by the driver. */
66 	struct timecounter	*th_counter;
67 	int64_t			th_adjustment;
68 	uint64_t		th_scale;
69 	u_int	 		th_offset_count;
70 	struct bintime		th_offset;
71 	struct timeval		th_microtime;
72 	struct timespec		th_nanotime;
73 	/* Fields not to be copied in tc_windup start with th_generation. */
74 	volatile u_int		th_generation;
75 	struct timehands	*th_next;
76 };
77 
78 static struct timehands th0;
79 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
80 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
81 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
82 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
83 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
84 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
85 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
86 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
87 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
88 static struct timehands th0 = {
89 	&dummy_timecounter,
90 	0,
91 	(uint64_t)-1 / 1000000,
92 	0,
93 	{1, 0},
94 	{0, 0},
95 	{0, 0},
96 	1,
97 	&th1
98 };
99 
100 static struct timehands *volatile timehands = &th0;
101 struct timecounter *timecounter = &dummy_timecounter;
102 static struct timecounter *timecounters = &dummy_timecounter;
103 
104 int tc_min_ticktock_freq = 1;
105 
106 volatile time_t time_second = 1;
107 volatile time_t time_uptime = 1;
108 
109 struct bintime boottimebin;
110 struct timeval boottime;
111 static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
112 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
113     NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
114 
115 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
116 static SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc, CTLFLAG_RW, 0, "");
117 
118 static int timestepwarnings;
119 SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
120     &timestepwarnings, 0, "Log time steps");
121 
122 static void tc_windup(void);
123 static void cpu_tick_calibrate(int);
124 
125 void dtrace_getnanotime(struct timespec *tsp);
126 
127 static int
128 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
129 {
130 #ifndef __mips__
131 #ifdef SCTL_MASK32
132 	int tv[2];
133 
134 	if (req->flags & SCTL_MASK32) {
135 		tv[0] = boottime.tv_sec;
136 		tv[1] = boottime.tv_usec;
137 		return SYSCTL_OUT(req, tv, sizeof(tv));
138 	} else
139 #endif
140 #endif
141 		return SYSCTL_OUT(req, &boottime, sizeof(boottime));
142 }
143 
144 static int
145 sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
146 {
147 	u_int ncount;
148 	struct timecounter *tc = arg1;
149 
150 	ncount = tc->tc_get_timecount(tc);
151 	return sysctl_handle_int(oidp, &ncount, 0, req);
152 }
153 
154 static int
155 sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
156 {
157 	uint64_t freq;
158 	struct timecounter *tc = arg1;
159 
160 	freq = tc->tc_frequency;
161 	return sysctl_handle_64(oidp, &freq, 0, req);
162 }
163 
164 /*
165  * Return the difference between the timehands' counter value now and what
166  * was when we copied it to the timehands' offset_count.
167  */
168 static __inline u_int
169 tc_delta(struct timehands *th)
170 {
171 	struct timecounter *tc;
172 
173 	tc = th->th_counter;
174 	return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
175 	    tc->tc_counter_mask);
176 }
177 
178 /*
179  * Functions for reading the time.  We have to loop until we are sure that
180  * the timehands that we operated on was not updated under our feet.  See
181  * the comment in <sys/time.h> for a description of these 12 functions.
182  */
183 
184 #ifdef FFCLOCK
185 void
186 fbclock_binuptime(struct bintime *bt)
187 {
188 	struct timehands *th;
189 	unsigned int gen;
190 
191 	do {
192 		th = timehands;
193 		gen = th->th_generation;
194 		*bt = th->th_offset;
195 		bintime_addx(bt, th->th_scale * tc_delta(th));
196 	} while (gen == 0 || gen != th->th_generation);
197 }
198 
199 void
200 fbclock_nanouptime(struct timespec *tsp)
201 {
202 	struct bintime bt;
203 
204 	fbclock_binuptime(&bt);
205 	bintime2timespec(&bt, tsp);
206 }
207 
208 void
209 fbclock_microuptime(struct timeval *tvp)
210 {
211 	struct bintime bt;
212 
213 	fbclock_binuptime(&bt);
214 	bintime2timeval(&bt, tvp);
215 }
216 
217 void
218 fbclock_bintime(struct bintime *bt)
219 {
220 
221 	fbclock_binuptime(bt);
222 	bintime_add(bt, &boottimebin);
223 }
224 
225 void
226 fbclock_nanotime(struct timespec *tsp)
227 {
228 	struct bintime bt;
229 
230 	fbclock_bintime(&bt);
231 	bintime2timespec(&bt, tsp);
232 }
233 
234 void
235 fbclock_microtime(struct timeval *tvp)
236 {
237 	struct bintime bt;
238 
239 	fbclock_bintime(&bt);
240 	bintime2timeval(&bt, tvp);
241 }
242 
243 void
244 fbclock_getbinuptime(struct bintime *bt)
245 {
246 	struct timehands *th;
247 	unsigned int gen;
248 
249 	do {
250 		th = timehands;
251 		gen = th->th_generation;
252 		*bt = th->th_offset;
253 	} while (gen == 0 || gen != th->th_generation);
254 }
255 
256 void
257 fbclock_getnanouptime(struct timespec *tsp)
258 {
259 	struct timehands *th;
260 	unsigned int gen;
261 
262 	do {
263 		th = timehands;
264 		gen = th->th_generation;
265 		bintime2timespec(&th->th_offset, tsp);
266 	} while (gen == 0 || gen != th->th_generation);
267 }
268 
269 void
270 fbclock_getmicrouptime(struct timeval *tvp)
271 {
272 	struct timehands *th;
273 	unsigned int gen;
274 
275 	do {
276 		th = timehands;
277 		gen = th->th_generation;
278 		bintime2timeval(&th->th_offset, tvp);
279 	} while (gen == 0 || gen != th->th_generation);
280 }
281 
282 void
283 fbclock_getbintime(struct bintime *bt)
284 {
285 	struct timehands *th;
286 	unsigned int gen;
287 
288 	do {
289 		th = timehands;
290 		gen = th->th_generation;
291 		*bt = th->th_offset;
292 	} while (gen == 0 || gen != th->th_generation);
293 	bintime_add(bt, &boottimebin);
294 }
295 
296 void
297 fbclock_getnanotime(struct timespec *tsp)
298 {
299 	struct timehands *th;
300 	unsigned int gen;
301 
302 	do {
303 		th = timehands;
304 		gen = th->th_generation;
305 		*tsp = th->th_nanotime;
306 	} while (gen == 0 || gen != th->th_generation);
307 }
308 
309 void
310 fbclock_getmicrotime(struct timeval *tvp)
311 {
312 	struct timehands *th;
313 	unsigned int gen;
314 
315 	do {
316 		th = timehands;
317 		gen = th->th_generation;
318 		*tvp = th->th_microtime;
319 	} while (gen == 0 || gen != th->th_generation);
320 }
321 #else /* !FFCLOCK */
322 void
323 binuptime(struct bintime *bt)
324 {
325 	struct timehands *th;
326 	u_int gen;
327 
328 	do {
329 		th = timehands;
330 		gen = th->th_generation;
331 		*bt = th->th_offset;
332 		bintime_addx(bt, th->th_scale * tc_delta(th));
333 	} while (gen == 0 || gen != th->th_generation);
334 }
335 
336 void
337 nanouptime(struct timespec *tsp)
338 {
339 	struct bintime bt;
340 
341 	binuptime(&bt);
342 	bintime2timespec(&bt, tsp);
343 }
344 
345 void
346 microuptime(struct timeval *tvp)
347 {
348 	struct bintime bt;
349 
350 	binuptime(&bt);
351 	bintime2timeval(&bt, tvp);
352 }
353 
354 void
355 bintime(struct bintime *bt)
356 {
357 
358 	binuptime(bt);
359 	bintime_add(bt, &boottimebin);
360 }
361 
362 void
363 nanotime(struct timespec *tsp)
364 {
365 	struct bintime bt;
366 
367 	bintime(&bt);
368 	bintime2timespec(&bt, tsp);
369 }
370 
371 void
372 microtime(struct timeval *tvp)
373 {
374 	struct bintime bt;
375 
376 	bintime(&bt);
377 	bintime2timeval(&bt, tvp);
378 }
379 
380 void
381 getbinuptime(struct bintime *bt)
382 {
383 	struct timehands *th;
384 	u_int gen;
385 
386 	do {
387 		th = timehands;
388 		gen = th->th_generation;
389 		*bt = th->th_offset;
390 	} while (gen == 0 || gen != th->th_generation);
391 }
392 
393 void
394 getnanouptime(struct timespec *tsp)
395 {
396 	struct timehands *th;
397 	u_int gen;
398 
399 	do {
400 		th = timehands;
401 		gen = th->th_generation;
402 		bintime2timespec(&th->th_offset, tsp);
403 	} while (gen == 0 || gen != th->th_generation);
404 }
405 
406 void
407 getmicrouptime(struct timeval *tvp)
408 {
409 	struct timehands *th;
410 	u_int gen;
411 
412 	do {
413 		th = timehands;
414 		gen = th->th_generation;
415 		bintime2timeval(&th->th_offset, tvp);
416 	} while (gen == 0 || gen != th->th_generation);
417 }
418 
419 void
420 getbintime(struct bintime *bt)
421 {
422 	struct timehands *th;
423 	u_int gen;
424 
425 	do {
426 		th = timehands;
427 		gen = th->th_generation;
428 		*bt = th->th_offset;
429 	} while (gen == 0 || gen != th->th_generation);
430 	bintime_add(bt, &boottimebin);
431 }
432 
433 void
434 getnanotime(struct timespec *tsp)
435 {
436 	struct timehands *th;
437 	u_int gen;
438 
439 	do {
440 		th = timehands;
441 		gen = th->th_generation;
442 		*tsp = th->th_nanotime;
443 	} while (gen == 0 || gen != th->th_generation);
444 }
445 
446 void
447 getmicrotime(struct timeval *tvp)
448 {
449 	struct timehands *th;
450 	u_int gen;
451 
452 	do {
453 		th = timehands;
454 		gen = th->th_generation;
455 		*tvp = th->th_microtime;
456 	} while (gen == 0 || gen != th->th_generation);
457 }
458 #endif /* FFCLOCK */
459 
460 #ifdef FFCLOCK
461 /*
462  * Support for feed-forward synchronization algorithms. This is heavily inspired
463  * by the timehands mechanism but kept independent from it. *_windup() functions
464  * have some connection to avoid accessing the timecounter hardware more than
465  * necessary.
466  */
467 
468 /* Feed-forward clock estimates kept updated by the synchronization daemon. */
469 struct ffclock_estimate ffclock_estimate;
470 struct bintime ffclock_boottime;	/* Feed-forward boot time estimate. */
471 uint32_t ffclock_status;		/* Feed-forward clock status. */
472 int8_t ffclock_updated;			/* New estimates are available. */
473 struct mtx ffclock_mtx;			/* Mutex on ffclock_estimate. */
474 
475 struct fftimehands {
476 	struct ffclock_estimate	cest;
477 	struct bintime		tick_time;
478 	struct bintime		tick_time_lerp;
479 	ffcounter		tick_ffcount;
480 	uint64_t		period_lerp;
481 	volatile uint8_t	gen;
482 	struct fftimehands	*next;
483 };
484 
485 #define	NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
486 
487 static struct fftimehands ffth[10];
488 static struct fftimehands *volatile fftimehands = ffth;
489 
490 static void
491 ffclock_init(void)
492 {
493 	struct fftimehands *cur;
494 	struct fftimehands *last;
495 
496 	memset(ffth, 0, sizeof(ffth));
497 
498 	last = ffth + NUM_ELEMENTS(ffth) - 1;
499 	for (cur = ffth; cur < last; cur++)
500 		cur->next = cur + 1;
501 	last->next = ffth;
502 
503 	ffclock_updated = 0;
504 	ffclock_status = FFCLOCK_STA_UNSYNC;
505 	mtx_init(&ffclock_mtx, "ffclock lock", NULL, MTX_DEF);
506 }
507 
508 /*
509  * Reset the feed-forward clock estimates. Called from inittodr() to get things
510  * kick started and uses the timecounter nominal frequency as a first period
511  * estimate. Note: this function may be called several time just after boot.
512  * Note: this is the only function that sets the value of boot time for the
513  * monotonic (i.e. uptime) version of the feed-forward clock.
514  */
515 void
516 ffclock_reset_clock(struct timespec *ts)
517 {
518 	struct timecounter *tc;
519 	struct ffclock_estimate cest;
520 
521 	tc = timehands->th_counter;
522 	memset(&cest, 0, sizeof(struct ffclock_estimate));
523 
524 	timespec2bintime(ts, &ffclock_boottime);
525 	timespec2bintime(ts, &(cest.update_time));
526 	ffclock_read_counter(&cest.update_ffcount);
527 	cest.leapsec_next = 0;
528 	cest.period = ((1ULL << 63) / tc->tc_frequency) << 1;
529 	cest.errb_abs = 0;
530 	cest.errb_rate = 0;
531 	cest.status = FFCLOCK_STA_UNSYNC;
532 	cest.leapsec_total = 0;
533 	cest.leapsec = 0;
534 
535 	mtx_lock(&ffclock_mtx);
536 	bcopy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
537 	ffclock_updated = INT8_MAX;
538 	mtx_unlock(&ffclock_mtx);
539 
540 	printf("ffclock reset: %s (%llu Hz), time = %ld.%09lu\n", tc->tc_name,
541 	    (unsigned long long)tc->tc_frequency, (long)ts->tv_sec,
542 	    (unsigned long)ts->tv_nsec);
543 }
544 
545 /*
546  * Sub-routine to convert a time interval measured in RAW counter units to time
547  * in seconds stored in bintime format.
548  * NOTE: bintime_mul requires u_int, but the value of the ffcounter may be
549  * larger than the max value of u_int (on 32 bit architecture). Loop to consume
550  * extra cycles.
551  */
552 static void
553 ffclock_convert_delta(ffcounter ffdelta, uint64_t period, struct bintime *bt)
554 {
555 	struct bintime bt2;
556 	ffcounter delta, delta_max;
557 
558 	delta_max = (1ULL << (8 * sizeof(unsigned int))) - 1;
559 	bintime_clear(bt);
560 	do {
561 		if (ffdelta > delta_max)
562 			delta = delta_max;
563 		else
564 			delta = ffdelta;
565 		bt2.sec = 0;
566 		bt2.frac = period;
567 		bintime_mul(&bt2, (unsigned int)delta);
568 		bintime_add(bt, &bt2);
569 		ffdelta -= delta;
570 	} while (ffdelta > 0);
571 }
572 
573 /*
574  * Update the fftimehands.
575  * Push the tick ffcount and time(s) forward based on current clock estimate.
576  * The conversion from ffcounter to bintime relies on the difference clock
577  * principle, whose accuracy relies on computing small time intervals. If a new
578  * clock estimate has been passed by the synchronisation daemon, make it
579  * current, and compute the linear interpolation for monotonic time if needed.
580  */
581 static void
582 ffclock_windup(unsigned int delta)
583 {
584 	struct ffclock_estimate *cest;
585 	struct fftimehands *ffth;
586 	struct bintime bt, gap_lerp;
587 	ffcounter ffdelta;
588 	uint64_t frac;
589 	unsigned int polling;
590 	uint8_t forward_jump, ogen;
591 
592 	/*
593 	 * Pick the next timehand, copy current ffclock estimates and move tick
594 	 * times and counter forward.
595 	 */
596 	forward_jump = 0;
597 	ffth = fftimehands->next;
598 	ogen = ffth->gen;
599 	ffth->gen = 0;
600 	cest = &ffth->cest;
601 	bcopy(&fftimehands->cest, cest, sizeof(struct ffclock_estimate));
602 	ffdelta = (ffcounter)delta;
603 	ffth->period_lerp = fftimehands->period_lerp;
604 
605 	ffth->tick_time = fftimehands->tick_time;
606 	ffclock_convert_delta(ffdelta, cest->period, &bt);
607 	bintime_add(&ffth->tick_time, &bt);
608 
609 	ffth->tick_time_lerp = fftimehands->tick_time_lerp;
610 	ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt);
611 	bintime_add(&ffth->tick_time_lerp, &bt);
612 
613 	ffth->tick_ffcount = fftimehands->tick_ffcount + ffdelta;
614 
615 	/*
616 	 * Assess the status of the clock, if the last update is too old, it is
617 	 * likely the synchronisation daemon is dead and the clock is free
618 	 * running.
619 	 */
620 	if (ffclock_updated == 0) {
621 		ffdelta = ffth->tick_ffcount - cest->update_ffcount;
622 		ffclock_convert_delta(ffdelta, cest->period, &bt);
623 		if (bt.sec > 2 * FFCLOCK_SKM_SCALE)
624 			ffclock_status |= FFCLOCK_STA_UNSYNC;
625 	}
626 
627 	/*
628 	 * If available, grab updated clock estimates and make them current.
629 	 * Recompute time at this tick using the updated estimates. The clock
630 	 * estimates passed the feed-forward synchronisation daemon may result
631 	 * in time conversion that is not monotonically increasing (just after
632 	 * the update). time_lerp is a particular linear interpolation over the
633 	 * synchronisation algo polling period that ensures monotonicity for the
634 	 * clock ids requesting it.
635 	 */
636 	if (ffclock_updated > 0) {
637 		bcopy(&ffclock_estimate, cest, sizeof(struct ffclock_estimate));
638 		ffdelta = ffth->tick_ffcount - cest->update_ffcount;
639 		ffth->tick_time = cest->update_time;
640 		ffclock_convert_delta(ffdelta, cest->period, &bt);
641 		bintime_add(&ffth->tick_time, &bt);
642 
643 		/* ffclock_reset sets ffclock_updated to INT8_MAX */
644 		if (ffclock_updated == INT8_MAX)
645 			ffth->tick_time_lerp = ffth->tick_time;
646 
647 		if (bintime_cmp(&ffth->tick_time, &ffth->tick_time_lerp, >))
648 			forward_jump = 1;
649 		else
650 			forward_jump = 0;
651 
652 		bintime_clear(&gap_lerp);
653 		if (forward_jump) {
654 			gap_lerp = ffth->tick_time;
655 			bintime_sub(&gap_lerp, &ffth->tick_time_lerp);
656 		} else {
657 			gap_lerp = ffth->tick_time_lerp;
658 			bintime_sub(&gap_lerp, &ffth->tick_time);
659 		}
660 
661 		/*
662 		 * The reset from the RTC clock may be far from accurate, and
663 		 * reducing the gap between real time and interpolated time
664 		 * could take a very long time if the interpolated clock insists
665 		 * on strict monotonicity. The clock is reset under very strict
666 		 * conditions (kernel time is known to be wrong and
667 		 * synchronization daemon has been restarted recently.
668 		 * ffclock_boottime absorbs the jump to ensure boot time is
669 		 * correct and uptime functions stay consistent.
670 		 */
671 		if (((ffclock_status & FFCLOCK_STA_UNSYNC) == FFCLOCK_STA_UNSYNC) &&
672 		    ((cest->status & FFCLOCK_STA_UNSYNC) == 0) &&
673 		    ((cest->status & FFCLOCK_STA_WARMUP) == FFCLOCK_STA_WARMUP)) {
674 			if (forward_jump)
675 				bintime_add(&ffclock_boottime, &gap_lerp);
676 			else
677 				bintime_sub(&ffclock_boottime, &gap_lerp);
678 			ffth->tick_time_lerp = ffth->tick_time;
679 			bintime_clear(&gap_lerp);
680 		}
681 
682 		ffclock_status = cest->status;
683 		ffth->period_lerp = cest->period;
684 
685 		/*
686 		 * Compute corrected period used for the linear interpolation of
687 		 * time. The rate of linear interpolation is capped to 5000PPM
688 		 * (5ms/s).
689 		 */
690 		if (bintime_isset(&gap_lerp)) {
691 			ffdelta = cest->update_ffcount;
692 			ffdelta -= fftimehands->cest.update_ffcount;
693 			ffclock_convert_delta(ffdelta, cest->period, &bt);
694 			polling = bt.sec;
695 			bt.sec = 0;
696 			bt.frac = 5000000 * (uint64_t)18446744073LL;
697 			bintime_mul(&bt, polling);
698 			if (bintime_cmp(&gap_lerp, &bt, >))
699 				gap_lerp = bt;
700 
701 			/* Approximate 1 sec by 1-(1/2^64) to ease arithmetic */
702 			frac = 0;
703 			if (gap_lerp.sec > 0) {
704 				frac -= 1;
705 				frac /= ffdelta / gap_lerp.sec;
706 			}
707 			frac += gap_lerp.frac / ffdelta;
708 
709 			if (forward_jump)
710 				ffth->period_lerp += frac;
711 			else
712 				ffth->period_lerp -= frac;
713 		}
714 
715 		ffclock_updated = 0;
716 	}
717 	if (++ogen == 0)
718 		ogen = 1;
719 	ffth->gen = ogen;
720 	fftimehands = ffth;
721 }
722 
723 /*
724  * Adjust the fftimehands when the timecounter is changed. Stating the obvious,
725  * the old and new hardware counter cannot be read simultaneously. tc_windup()
726  * does read the two counters 'back to back', but a few cycles are effectively
727  * lost, and not accumulated in tick_ffcount. This is a fairly radical
728  * operation for a feed-forward synchronization daemon, and it is its job to not
729  * pushing irrelevant data to the kernel. Because there is no locking here,
730  * simply force to ignore pending or next update to give daemon a chance to
731  * realize the counter has changed.
732  */
733 static void
734 ffclock_change_tc(struct timehands *th)
735 {
736 	struct fftimehands *ffth;
737 	struct ffclock_estimate *cest;
738 	struct timecounter *tc;
739 	uint8_t ogen;
740 
741 	tc = th->th_counter;
742 	ffth = fftimehands->next;
743 	ogen = ffth->gen;
744 	ffth->gen = 0;
745 
746 	cest = &ffth->cest;
747 	bcopy(&(fftimehands->cest), cest, sizeof(struct ffclock_estimate));
748 	cest->period = ((1ULL << 63) / tc->tc_frequency ) << 1;
749 	cest->errb_abs = 0;
750 	cest->errb_rate = 0;
751 	cest->status |= FFCLOCK_STA_UNSYNC;
752 
753 	ffth->tick_ffcount = fftimehands->tick_ffcount;
754 	ffth->tick_time_lerp = fftimehands->tick_time_lerp;
755 	ffth->tick_time = fftimehands->tick_time;
756 	ffth->period_lerp = cest->period;
757 
758 	/* Do not lock but ignore next update from synchronization daemon. */
759 	ffclock_updated--;
760 
761 	if (++ogen == 0)
762 		ogen = 1;
763 	ffth->gen = ogen;
764 	fftimehands = ffth;
765 }
766 
767 /*
768  * Retrieve feed-forward counter and time of last kernel tick.
769  */
770 void
771 ffclock_last_tick(ffcounter *ffcount, struct bintime *bt, uint32_t flags)
772 {
773 	struct fftimehands *ffth;
774 	uint8_t gen;
775 
776 	/*
777 	 * No locking but check generation has not changed. Also need to make
778 	 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
779 	 */
780 	do {
781 		ffth = fftimehands;
782 		gen = ffth->gen;
783 		if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP)
784 			*bt = ffth->tick_time_lerp;
785 		else
786 			*bt = ffth->tick_time;
787 		*ffcount = ffth->tick_ffcount;
788 	} while (gen == 0 || gen != ffth->gen);
789 }
790 
791 /*
792  * Absolute clock conversion. Low level function to convert ffcounter to
793  * bintime. The ffcounter is converted using the current ffclock period estimate
794  * or the "interpolated period" to ensure monotonicity.
795  * NOTE: this conversion may have been deferred, and the clock updated since the
796  * hardware counter has been read.
797  */
798 void
799 ffclock_convert_abs(ffcounter ffcount, struct bintime *bt, uint32_t flags)
800 {
801 	struct fftimehands *ffth;
802 	struct bintime bt2;
803 	ffcounter ffdelta;
804 	uint8_t gen;
805 
806 	/*
807 	 * No locking but check generation has not changed. Also need to make
808 	 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
809 	 */
810 	do {
811 		ffth = fftimehands;
812 		gen = ffth->gen;
813 		if (ffcount > ffth->tick_ffcount)
814 			ffdelta = ffcount - ffth->tick_ffcount;
815 		else
816 			ffdelta = ffth->tick_ffcount - ffcount;
817 
818 		if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP) {
819 			*bt = ffth->tick_time_lerp;
820 			ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt2);
821 		} else {
822 			*bt = ffth->tick_time;
823 			ffclock_convert_delta(ffdelta, ffth->cest.period, &bt2);
824 		}
825 
826 		if (ffcount > ffth->tick_ffcount)
827 			bintime_add(bt, &bt2);
828 		else
829 			bintime_sub(bt, &bt2);
830 	} while (gen == 0 || gen != ffth->gen);
831 }
832 
833 /*
834  * Difference clock conversion.
835  * Low level function to Convert a time interval measured in RAW counter units
836  * into bintime. The difference clock allows measuring small intervals much more
837  * reliably than the absolute clock.
838  */
839 void
840 ffclock_convert_diff(ffcounter ffdelta, struct bintime *bt)
841 {
842 	struct fftimehands *ffth;
843 	uint8_t gen;
844 
845 	/* No locking but check generation has not changed. */
846 	do {
847 		ffth = fftimehands;
848 		gen = ffth->gen;
849 		ffclock_convert_delta(ffdelta, ffth->cest.period, bt);
850 	} while (gen == 0 || gen != ffth->gen);
851 }
852 
853 /*
854  * Access to current ffcounter value.
855  */
856 void
857 ffclock_read_counter(ffcounter *ffcount)
858 {
859 	struct timehands *th;
860 	struct fftimehands *ffth;
861 	unsigned int gen, delta;
862 
863 	/*
864 	 * ffclock_windup() called from tc_windup(), safe to rely on
865 	 * th->th_generation only, for correct delta and ffcounter.
866 	 */
867 	do {
868 		th = timehands;
869 		gen = th->th_generation;
870 		ffth = fftimehands;
871 		delta = tc_delta(th);
872 		*ffcount = ffth->tick_ffcount;
873 	} while (gen == 0 || gen != th->th_generation);
874 
875 	*ffcount += delta;
876 }
877 
878 void
879 binuptime(struct bintime *bt)
880 {
881 
882 	binuptime_fromclock(bt, sysclock_active);
883 }
884 
885 void
886 nanouptime(struct timespec *tsp)
887 {
888 
889 	nanouptime_fromclock(tsp, sysclock_active);
890 }
891 
892 void
893 microuptime(struct timeval *tvp)
894 {
895 
896 	microuptime_fromclock(tvp, sysclock_active);
897 }
898 
899 void
900 bintime(struct bintime *bt)
901 {
902 
903 	bintime_fromclock(bt, sysclock_active);
904 }
905 
906 void
907 nanotime(struct timespec *tsp)
908 {
909 
910 	nanotime_fromclock(tsp, sysclock_active);
911 }
912 
913 void
914 microtime(struct timeval *tvp)
915 {
916 
917 	microtime_fromclock(tvp, sysclock_active);
918 }
919 
920 void
921 getbinuptime(struct bintime *bt)
922 {
923 
924 	getbinuptime_fromclock(bt, sysclock_active);
925 }
926 
927 void
928 getnanouptime(struct timespec *tsp)
929 {
930 
931 	getnanouptime_fromclock(tsp, sysclock_active);
932 }
933 
934 void
935 getmicrouptime(struct timeval *tvp)
936 {
937 
938 	getmicrouptime_fromclock(tvp, sysclock_active);
939 }
940 
941 void
942 getbintime(struct bintime *bt)
943 {
944 
945 	getbintime_fromclock(bt, sysclock_active);
946 }
947 
948 void
949 getnanotime(struct timespec *tsp)
950 {
951 
952 	getnanotime_fromclock(tsp, sysclock_active);
953 }
954 
955 void
956 getmicrotime(struct timeval *tvp)
957 {
958 
959 	getmicrouptime_fromclock(tvp, sysclock_active);
960 }
961 
962 #endif /* FFCLOCK */
963 
964 /*
965  * This is a clone of getnanotime and used for walltimestamps.
966  * The dtrace_ prefix prevents fbt from creating probes for
967  * it so walltimestamp can be safely used in all fbt probes.
968  */
969 void
970 dtrace_getnanotime(struct timespec *tsp)
971 {
972 	struct timehands *th;
973 	u_int gen;
974 
975 	do {
976 		th = timehands;
977 		gen = th->th_generation;
978 		*tsp = th->th_nanotime;
979 	} while (gen == 0 || gen != th->th_generation);
980 }
981 
982 /*
983  * System clock currently providing time to the system. Modifiable via sysctl
984  * when the FFCLOCK option is defined.
985  */
986 int sysclock_active = SYSCLOCK_FBCK;
987 
988 /* Internal NTP status and error estimates. */
989 extern int time_status;
990 extern long time_esterror;
991 
992 /*
993  * Take a snapshot of sysclock data which can be used to compare system clocks
994  * and generate timestamps after the fact.
995  */
996 void
997 sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast)
998 {
999 	struct fbclock_info *fbi;
1000 	struct timehands *th;
1001 	struct bintime bt;
1002 	unsigned int delta, gen;
1003 #ifdef FFCLOCK
1004 	ffcounter ffcount;
1005 	struct fftimehands *ffth;
1006 	struct ffclock_info *ffi;
1007 	struct ffclock_estimate cest;
1008 
1009 	ffi = &clock_snap->ff_info;
1010 #endif
1011 
1012 	fbi = &clock_snap->fb_info;
1013 	delta = 0;
1014 
1015 	do {
1016 		th = timehands;
1017 		gen = th->th_generation;
1018 		fbi->th_scale = th->th_scale;
1019 		fbi->tick_time = th->th_offset;
1020 #ifdef FFCLOCK
1021 		ffth = fftimehands;
1022 		ffi->tick_time = ffth->tick_time_lerp;
1023 		ffi->tick_time_lerp = ffth->tick_time_lerp;
1024 		ffi->period = ffth->cest.period;
1025 		ffi->period_lerp = ffth->period_lerp;
1026 		clock_snap->ffcount = ffth->tick_ffcount;
1027 		cest = ffth->cest;
1028 #endif
1029 		if (!fast)
1030 			delta = tc_delta(th);
1031 	} while (gen == 0 || gen != th->th_generation);
1032 
1033 	clock_snap->delta = delta;
1034 	clock_snap->sysclock_active = sysclock_active;
1035 
1036 	/* Record feedback clock status and error. */
1037 	clock_snap->fb_info.status = time_status;
1038 	/* XXX: Very crude estimate of feedback clock error. */
1039 	bt.sec = time_esterror / 1000000;
1040 	bt.frac = ((time_esterror - bt.sec) * 1000000) *
1041 	    (uint64_t)18446744073709ULL;
1042 	clock_snap->fb_info.error = bt;
1043 
1044 #ifdef FFCLOCK
1045 	if (!fast)
1046 		clock_snap->ffcount += delta;
1047 
1048 	/* Record feed-forward clock leap second adjustment. */
1049 	ffi->leapsec_adjustment = cest.leapsec_total;
1050 	if (clock_snap->ffcount > cest.leapsec_next)
1051 		ffi->leapsec_adjustment -= cest.leapsec;
1052 
1053 	/* Record feed-forward clock status and error. */
1054 	clock_snap->ff_info.status = cest.status;
1055 	ffcount = clock_snap->ffcount - cest.update_ffcount;
1056 	ffclock_convert_delta(ffcount, cest.period, &bt);
1057 	/* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s]. */
1058 	bintime_mul(&bt, cest.errb_rate * (uint64_t)18446744073709ULL);
1059 	/* 18446744073 = int(2^64 / 1e9), since err_abs in [ns]. */
1060 	bintime_addx(&bt, cest.errb_abs * (uint64_t)18446744073ULL);
1061 	clock_snap->ff_info.error = bt;
1062 #endif
1063 }
1064 
1065 /*
1066  * Convert a sysclock snapshot into a struct bintime based on the specified
1067  * clock source and flags.
1068  */
1069 int
1070 sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt,
1071     int whichclock, uint32_t flags)
1072 {
1073 #ifdef FFCLOCK
1074 	struct bintime bt2;
1075 	uint64_t period;
1076 #endif
1077 
1078 	switch (whichclock) {
1079 	case SYSCLOCK_FBCK:
1080 		*bt = cs->fb_info.tick_time;
1081 
1082 		/* If snapshot was created with !fast, delta will be >0. */
1083 		if (cs->delta > 0)
1084 			bintime_addx(bt, cs->fb_info.th_scale * cs->delta);
1085 
1086 		if ((flags & FBCLOCK_UPTIME) == 0)
1087 			bintime_add(bt, &boottimebin);
1088 		break;
1089 #ifdef FFCLOCK
1090 	case SYSCLOCK_FFWD:
1091 		if (flags & FFCLOCK_LERP) {
1092 			*bt = cs->ff_info.tick_time_lerp;
1093 			period = cs->ff_info.period_lerp;
1094 		} else {
1095 			*bt = cs->ff_info.tick_time;
1096 			period = cs->ff_info.period;
1097 		}
1098 
1099 		/* If snapshot was created with !fast, delta will be >0. */
1100 		if (cs->delta > 0) {
1101 			ffclock_convert_delta(cs->delta, period, &bt2);
1102 			bintime_add(bt, &bt2);
1103 		}
1104 
1105 		/* Leap second adjustment. */
1106 		if (flags & FFCLOCK_LEAPSEC)
1107 			bt->sec -= cs->ff_info.leapsec_adjustment;
1108 
1109 		/* Boot time adjustment, for uptime/monotonic clocks. */
1110 		if (flags & FFCLOCK_UPTIME)
1111 			bintime_sub(bt, &ffclock_boottime);
1112 		break;
1113 #endif
1114 	default:
1115 		return (EINVAL);
1116 		break;
1117 	}
1118 
1119 	return (0);
1120 }
1121 
1122 /*
1123  * Initialize a new timecounter and possibly use it.
1124  */
1125 void
1126 tc_init(struct timecounter *tc)
1127 {
1128 	u_int u;
1129 	struct sysctl_oid *tc_root;
1130 
1131 	u = tc->tc_frequency / tc->tc_counter_mask;
1132 	/* XXX: We need some margin here, 10% is a guess */
1133 	u *= 11;
1134 	u /= 10;
1135 	if (u > hz && tc->tc_quality >= 0) {
1136 		tc->tc_quality = -2000;
1137 		if (bootverbose) {
1138 			printf("Timecounter \"%s\" frequency %ju Hz",
1139 			    tc->tc_name, (uintmax_t)tc->tc_frequency);
1140 			printf(" -- Insufficient hz, needs at least %u\n", u);
1141 		}
1142 	} else if (tc->tc_quality >= 0 || bootverbose) {
1143 		printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
1144 		    tc->tc_name, (uintmax_t)tc->tc_frequency,
1145 		    tc->tc_quality);
1146 	}
1147 
1148 	tc->tc_next = timecounters;
1149 	timecounters = tc;
1150 	/*
1151 	 * Set up sysctl tree for this counter.
1152 	 */
1153 	tc_root = SYSCTL_ADD_NODE(NULL,
1154 	    SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
1155 	    CTLFLAG_RW, 0, "timecounter description");
1156 	SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1157 	    "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
1158 	    "mask for implemented bits");
1159 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1160 	    "counter", CTLTYPE_UINT | CTLFLAG_RD, tc, sizeof(*tc),
1161 	    sysctl_kern_timecounter_get, "IU", "current timecounter value");
1162 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1163 	    "frequency", CTLTYPE_U64 | CTLFLAG_RD, tc, sizeof(*tc),
1164 	     sysctl_kern_timecounter_freq, "QU", "timecounter frequency");
1165 	SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1166 	    "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
1167 	    "goodness of time counter");
1168 	/*
1169 	 * Never automatically use a timecounter with negative quality.
1170 	 * Even though we run on the dummy counter, switching here may be
1171 	 * worse since this timecounter may not be monotonous.
1172 	 */
1173 	if (tc->tc_quality < 0)
1174 		return;
1175 	if (tc->tc_quality < timecounter->tc_quality)
1176 		return;
1177 	if (tc->tc_quality == timecounter->tc_quality &&
1178 	    tc->tc_frequency < timecounter->tc_frequency)
1179 		return;
1180 	(void)tc->tc_get_timecount(tc);
1181 	(void)tc->tc_get_timecount(tc);
1182 	timecounter = tc;
1183 }
1184 
1185 /* Report the frequency of the current timecounter. */
1186 uint64_t
1187 tc_getfrequency(void)
1188 {
1189 
1190 	return (timehands->th_counter->tc_frequency);
1191 }
1192 
1193 /*
1194  * Step our concept of UTC.  This is done by modifying our estimate of
1195  * when we booted.
1196  * XXX: not locked.
1197  */
1198 void
1199 tc_setclock(struct timespec *ts)
1200 {
1201 	struct timespec tbef, taft;
1202 	struct bintime bt, bt2;
1203 
1204 	cpu_tick_calibrate(1);
1205 	nanotime(&tbef);
1206 	timespec2bintime(ts, &bt);
1207 	binuptime(&bt2);
1208 	bintime_sub(&bt, &bt2);
1209 	bintime_add(&bt2, &boottimebin);
1210 	boottimebin = bt;
1211 	bintime2timeval(&bt, &boottime);
1212 
1213 	/* XXX fiddle all the little crinkly bits around the fiords... */
1214 	tc_windup();
1215 	nanotime(&taft);
1216 	if (timestepwarnings) {
1217 		log(LOG_INFO,
1218 		    "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
1219 		    (intmax_t)tbef.tv_sec, tbef.tv_nsec,
1220 		    (intmax_t)taft.tv_sec, taft.tv_nsec,
1221 		    (intmax_t)ts->tv_sec, ts->tv_nsec);
1222 	}
1223 	cpu_tick_calibrate(1);
1224 }
1225 
1226 /*
1227  * Initialize the next struct timehands in the ring and make
1228  * it the active timehands.  Along the way we might switch to a different
1229  * timecounter and/or do seconds processing in NTP.  Slightly magic.
1230  */
1231 static void
1232 tc_windup(void)
1233 {
1234 	struct bintime bt;
1235 	struct timehands *th, *tho;
1236 	uint64_t scale;
1237 	u_int delta, ncount, ogen;
1238 	int i;
1239 	time_t t;
1240 
1241 	/*
1242 	 * Make the next timehands a copy of the current one, but do not
1243 	 * overwrite the generation or next pointer.  While we update
1244 	 * the contents, the generation must be zero.
1245 	 */
1246 	tho = timehands;
1247 	th = tho->th_next;
1248 	ogen = th->th_generation;
1249 	th->th_generation = 0;
1250 	bcopy(tho, th, offsetof(struct timehands, th_generation));
1251 
1252 	/*
1253 	 * Capture a timecounter delta on the current timecounter and if
1254 	 * changing timecounters, a counter value from the new timecounter.
1255 	 * Update the offset fields accordingly.
1256 	 */
1257 	delta = tc_delta(th);
1258 	if (th->th_counter != timecounter)
1259 		ncount = timecounter->tc_get_timecount(timecounter);
1260 	else
1261 		ncount = 0;
1262 #ifdef FFCLOCK
1263 	ffclock_windup(delta);
1264 #endif
1265 	th->th_offset_count += delta;
1266 	th->th_offset_count &= th->th_counter->tc_counter_mask;
1267 	while (delta > th->th_counter->tc_frequency) {
1268 		/* Eat complete unadjusted seconds. */
1269 		delta -= th->th_counter->tc_frequency;
1270 		th->th_offset.sec++;
1271 	}
1272 	if ((delta > th->th_counter->tc_frequency / 2) &&
1273 	    (th->th_scale * delta < ((uint64_t)1 << 63))) {
1274 		/* The product th_scale * delta just barely overflows. */
1275 		th->th_offset.sec++;
1276 	}
1277 	bintime_addx(&th->th_offset, th->th_scale * delta);
1278 
1279 	/*
1280 	 * Hardware latching timecounters may not generate interrupts on
1281 	 * PPS events, so instead we poll them.  There is a finite risk that
1282 	 * the hardware might capture a count which is later than the one we
1283 	 * got above, and therefore possibly in the next NTP second which might
1284 	 * have a different rate than the current NTP second.  It doesn't
1285 	 * matter in practice.
1286 	 */
1287 	if (tho->th_counter->tc_poll_pps)
1288 		tho->th_counter->tc_poll_pps(tho->th_counter);
1289 
1290 	/*
1291 	 * Deal with NTP second processing.  The for loop normally
1292 	 * iterates at most once, but in extreme situations it might
1293 	 * keep NTP sane if timeouts are not run for several seconds.
1294 	 * At boot, the time step can be large when the TOD hardware
1295 	 * has been read, so on really large steps, we call
1296 	 * ntp_update_second only twice.  We need to call it twice in
1297 	 * case we missed a leap second.
1298 	 */
1299 	bt = th->th_offset;
1300 	bintime_add(&bt, &boottimebin);
1301 	i = bt.sec - tho->th_microtime.tv_sec;
1302 	if (i > LARGE_STEP)
1303 		i = 2;
1304 	for (; i > 0; i--) {
1305 		t = bt.sec;
1306 		ntp_update_second(&th->th_adjustment, &bt.sec);
1307 		if (bt.sec != t)
1308 			boottimebin.sec += bt.sec - t;
1309 	}
1310 	/* Update the UTC timestamps used by the get*() functions. */
1311 	/* XXX shouldn't do this here.  Should force non-`get' versions. */
1312 	bintime2timeval(&bt, &th->th_microtime);
1313 	bintime2timespec(&bt, &th->th_nanotime);
1314 
1315 	/* Now is a good time to change timecounters. */
1316 	if (th->th_counter != timecounter) {
1317 #ifndef __arm__
1318 		if ((timecounter->tc_flags & TC_FLAGS_C3STOP) != 0)
1319 			cpu_disable_deep_sleep++;
1320 		if ((th->th_counter->tc_flags & TC_FLAGS_C3STOP) != 0)
1321 			cpu_disable_deep_sleep--;
1322 #endif
1323 		th->th_counter = timecounter;
1324 		th->th_offset_count = ncount;
1325 		tc_min_ticktock_freq = max(1, timecounter->tc_frequency /
1326 		    (((uint64_t)timecounter->tc_counter_mask + 1) / 3));
1327 #ifdef FFCLOCK
1328 		ffclock_change_tc(th);
1329 #endif
1330 	}
1331 
1332 	/*-
1333 	 * Recalculate the scaling factor.  We want the number of 1/2^64
1334 	 * fractions of a second per period of the hardware counter, taking
1335 	 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
1336 	 * processing provides us with.
1337 	 *
1338 	 * The th_adjustment is nanoseconds per second with 32 bit binary
1339 	 * fraction and we want 64 bit binary fraction of second:
1340 	 *
1341 	 *	 x = a * 2^32 / 10^9 = a * 4.294967296
1342 	 *
1343 	 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
1344 	 * we can only multiply by about 850 without overflowing, that
1345 	 * leaves no suitably precise fractions for multiply before divide.
1346 	 *
1347 	 * Divide before multiply with a fraction of 2199/512 results in a
1348 	 * systematic undercompensation of 10PPM of th_adjustment.  On a
1349 	 * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
1350  	 *
1351 	 * We happily sacrifice the lowest of the 64 bits of our result
1352 	 * to the goddess of code clarity.
1353 	 *
1354 	 */
1355 	scale = (uint64_t)1 << 63;
1356 	scale += (th->th_adjustment / 1024) * 2199;
1357 	scale /= th->th_counter->tc_frequency;
1358 	th->th_scale = scale * 2;
1359 
1360 	/*
1361 	 * Now that the struct timehands is again consistent, set the new
1362 	 * generation number, making sure to not make it zero.
1363 	 */
1364 	if (++ogen == 0)
1365 		ogen = 1;
1366 	th->th_generation = ogen;
1367 
1368 	/* Go live with the new struct timehands. */
1369 #ifdef FFCLOCK
1370 	switch (sysclock_active) {
1371 	case SYSCLOCK_FBCK:
1372 #endif
1373 		time_second = th->th_microtime.tv_sec;
1374 		time_uptime = th->th_offset.sec;
1375 #ifdef FFCLOCK
1376 		break;
1377 	case SYSCLOCK_FFWD:
1378 		time_second = fftimehands->tick_time_lerp.sec;
1379 		time_uptime = fftimehands->tick_time_lerp.sec - ffclock_boottime.sec;
1380 		break;
1381 	}
1382 #endif
1383 
1384 	timehands = th;
1385 	timekeep_push_vdso();
1386 }
1387 
1388 /* Report or change the active timecounter hardware. */
1389 static int
1390 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
1391 {
1392 	char newname[32];
1393 	struct timecounter *newtc, *tc;
1394 	int error;
1395 
1396 	tc = timecounter;
1397 	strlcpy(newname, tc->tc_name, sizeof(newname));
1398 
1399 	error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
1400 	if (error != 0 || req->newptr == NULL ||
1401 	    strcmp(newname, tc->tc_name) == 0)
1402 		return (error);
1403 	for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
1404 		if (strcmp(newname, newtc->tc_name) != 0)
1405 			continue;
1406 
1407 		/* Warm up new timecounter. */
1408 		(void)newtc->tc_get_timecount(newtc);
1409 		(void)newtc->tc_get_timecount(newtc);
1410 
1411 		timecounter = newtc;
1412 		timekeep_push_vdso();
1413 		return (0);
1414 	}
1415 	return (EINVAL);
1416 }
1417 
1418 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
1419     0, 0, sysctl_kern_timecounter_hardware, "A",
1420     "Timecounter hardware selected");
1421 
1422 
1423 /* Report or change the active timecounter hardware. */
1424 static int
1425 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
1426 {
1427 	char buf[32], *spc;
1428 	struct timecounter *tc;
1429 	int error;
1430 
1431 	spc = "";
1432 	error = 0;
1433 	for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
1434 		sprintf(buf, "%s%s(%d)",
1435 		    spc, tc->tc_name, tc->tc_quality);
1436 		error = SYSCTL_OUT(req, buf, strlen(buf));
1437 		spc = " ";
1438 	}
1439 	return (error);
1440 }
1441 
1442 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
1443     0, 0, sysctl_kern_timecounter_choice, "A", "Timecounter hardware detected");
1444 
1445 /*
1446  * RFC 2783 PPS-API implementation.
1447  */
1448 
1449 static int
1450 pps_fetch(struct pps_fetch_args *fapi, struct pps_state *pps)
1451 {
1452 	int err, timo;
1453 	pps_seq_t aseq, cseq;
1454 	struct timeval tv;
1455 
1456 	if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
1457 		return (EINVAL);
1458 
1459 	/*
1460 	 * If no timeout is requested, immediately return whatever values were
1461 	 * most recently captured.  If timeout seconds is -1, that's a request
1462 	 * to block without a timeout.  WITNESS won't let us sleep forever
1463 	 * without a lock (we really don't need a lock), so just repeatedly
1464 	 * sleep a long time.
1465 	 */
1466 	if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec) {
1467 		if (fapi->timeout.tv_sec == -1)
1468 			timo = 0x7fffffff;
1469 		else {
1470 			tv.tv_sec = fapi->timeout.tv_sec;
1471 			tv.tv_usec = fapi->timeout.tv_nsec / 1000;
1472 			timo = tvtohz(&tv);
1473 		}
1474 		aseq = pps->ppsinfo.assert_sequence;
1475 		cseq = pps->ppsinfo.clear_sequence;
1476 		while (aseq == pps->ppsinfo.assert_sequence &&
1477 		    cseq == pps->ppsinfo.clear_sequence) {
1478 			err = tsleep(pps, PCATCH, "ppsfch", timo);
1479 			if (err == EWOULDBLOCK && fapi->timeout.tv_sec == -1) {
1480 				continue;
1481 			} else if (err != 0) {
1482 				return (err);
1483 			}
1484 		}
1485 	}
1486 
1487 	pps->ppsinfo.current_mode = pps->ppsparam.mode;
1488 	fapi->pps_info_buf = pps->ppsinfo;
1489 
1490 	return (0);
1491 }
1492 
1493 int
1494 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
1495 {
1496 	pps_params_t *app;
1497 	struct pps_fetch_args *fapi;
1498 #ifdef FFCLOCK
1499 	struct pps_fetch_ffc_args *fapi_ffc;
1500 #endif
1501 #ifdef PPS_SYNC
1502 	struct pps_kcbind_args *kapi;
1503 #endif
1504 
1505 	KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
1506 	switch (cmd) {
1507 	case PPS_IOC_CREATE:
1508 		return (0);
1509 	case PPS_IOC_DESTROY:
1510 		return (0);
1511 	case PPS_IOC_SETPARAMS:
1512 		app = (pps_params_t *)data;
1513 		if (app->mode & ~pps->ppscap)
1514 			return (EINVAL);
1515 #ifdef FFCLOCK
1516 		/* Ensure only a single clock is selected for ffc timestamp. */
1517 		if ((app->mode & PPS_TSCLK_MASK) == PPS_TSCLK_MASK)
1518 			return (EINVAL);
1519 #endif
1520 		pps->ppsparam = *app;
1521 		return (0);
1522 	case PPS_IOC_GETPARAMS:
1523 		app = (pps_params_t *)data;
1524 		*app = pps->ppsparam;
1525 		app->api_version = PPS_API_VERS_1;
1526 		return (0);
1527 	case PPS_IOC_GETCAP:
1528 		*(int*)data = pps->ppscap;
1529 		return (0);
1530 	case PPS_IOC_FETCH:
1531 		fapi = (struct pps_fetch_args *)data;
1532 		return (pps_fetch(fapi, pps));
1533 #ifdef FFCLOCK
1534 	case PPS_IOC_FETCH_FFCOUNTER:
1535 		fapi_ffc = (struct pps_fetch_ffc_args *)data;
1536 		if (fapi_ffc->tsformat && fapi_ffc->tsformat !=
1537 		    PPS_TSFMT_TSPEC)
1538 			return (EINVAL);
1539 		if (fapi_ffc->timeout.tv_sec || fapi_ffc->timeout.tv_nsec)
1540 			return (EOPNOTSUPP);
1541 		pps->ppsinfo_ffc.current_mode = pps->ppsparam.mode;
1542 		fapi_ffc->pps_info_buf_ffc = pps->ppsinfo_ffc;
1543 		/* Overwrite timestamps if feedback clock selected. */
1544 		switch (pps->ppsparam.mode & PPS_TSCLK_MASK) {
1545 		case PPS_TSCLK_FBCK:
1546 			fapi_ffc->pps_info_buf_ffc.assert_timestamp =
1547 			    pps->ppsinfo.assert_timestamp;
1548 			fapi_ffc->pps_info_buf_ffc.clear_timestamp =
1549 			    pps->ppsinfo.clear_timestamp;
1550 			break;
1551 		case PPS_TSCLK_FFWD:
1552 			break;
1553 		default:
1554 			break;
1555 		}
1556 		return (0);
1557 #endif /* FFCLOCK */
1558 	case PPS_IOC_KCBIND:
1559 #ifdef PPS_SYNC
1560 		kapi = (struct pps_kcbind_args *)data;
1561 		/* XXX Only root should be able to do this */
1562 		if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
1563 			return (EINVAL);
1564 		if (kapi->kernel_consumer != PPS_KC_HARDPPS)
1565 			return (EINVAL);
1566 		if (kapi->edge & ~pps->ppscap)
1567 			return (EINVAL);
1568 		pps->kcmode = kapi->edge;
1569 		return (0);
1570 #else
1571 		return (EOPNOTSUPP);
1572 #endif
1573 	default:
1574 		return (ENOIOCTL);
1575 	}
1576 }
1577 
1578 void
1579 pps_init(struct pps_state *pps)
1580 {
1581 	pps->ppscap |= PPS_TSFMT_TSPEC | PPS_CANWAIT;
1582 	if (pps->ppscap & PPS_CAPTUREASSERT)
1583 		pps->ppscap |= PPS_OFFSETASSERT;
1584 	if (pps->ppscap & PPS_CAPTURECLEAR)
1585 		pps->ppscap |= PPS_OFFSETCLEAR;
1586 #ifdef FFCLOCK
1587 	pps->ppscap |= PPS_TSCLK_MASK;
1588 #endif
1589 }
1590 
1591 void
1592 pps_capture(struct pps_state *pps)
1593 {
1594 	struct timehands *th;
1595 
1596 	KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
1597 	th = timehands;
1598 	pps->capgen = th->th_generation;
1599 	pps->capth = th;
1600 #ifdef FFCLOCK
1601 	pps->capffth = fftimehands;
1602 #endif
1603 	pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
1604 	if (pps->capgen != th->th_generation)
1605 		pps->capgen = 0;
1606 }
1607 
1608 void
1609 pps_event(struct pps_state *pps, int event)
1610 {
1611 	struct bintime bt;
1612 	struct timespec ts, *tsp, *osp;
1613 	u_int tcount, *pcount;
1614 	int foff, fhard;
1615 	pps_seq_t *pseq;
1616 #ifdef FFCLOCK
1617 	struct timespec *tsp_ffc;
1618 	pps_seq_t *pseq_ffc;
1619 	ffcounter *ffcount;
1620 #endif
1621 
1622 	KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
1623 	/* If the timecounter was wound up underneath us, bail out. */
1624 	if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
1625 		return;
1626 
1627 	/* Things would be easier with arrays. */
1628 	if (event == PPS_CAPTUREASSERT) {
1629 		tsp = &pps->ppsinfo.assert_timestamp;
1630 		osp = &pps->ppsparam.assert_offset;
1631 		foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1632 		fhard = pps->kcmode & PPS_CAPTUREASSERT;
1633 		pcount = &pps->ppscount[0];
1634 		pseq = &pps->ppsinfo.assert_sequence;
1635 #ifdef FFCLOCK
1636 		ffcount = &pps->ppsinfo_ffc.assert_ffcount;
1637 		tsp_ffc = &pps->ppsinfo_ffc.assert_timestamp;
1638 		pseq_ffc = &pps->ppsinfo_ffc.assert_sequence;
1639 #endif
1640 	} else {
1641 		tsp = &pps->ppsinfo.clear_timestamp;
1642 		osp = &pps->ppsparam.clear_offset;
1643 		foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1644 		fhard = pps->kcmode & PPS_CAPTURECLEAR;
1645 		pcount = &pps->ppscount[1];
1646 		pseq = &pps->ppsinfo.clear_sequence;
1647 #ifdef FFCLOCK
1648 		ffcount = &pps->ppsinfo_ffc.clear_ffcount;
1649 		tsp_ffc = &pps->ppsinfo_ffc.clear_timestamp;
1650 		pseq_ffc = &pps->ppsinfo_ffc.clear_sequence;
1651 #endif
1652 	}
1653 
1654 	/*
1655 	 * If the timecounter changed, we cannot compare the count values, so
1656 	 * we have to drop the rest of the PPS-stuff until the next event.
1657 	 */
1658 	if (pps->ppstc != pps->capth->th_counter) {
1659 		pps->ppstc = pps->capth->th_counter;
1660 		*pcount = pps->capcount;
1661 		pps->ppscount[2] = pps->capcount;
1662 		return;
1663 	}
1664 
1665 	/* Convert the count to a timespec. */
1666 	tcount = pps->capcount - pps->capth->th_offset_count;
1667 	tcount &= pps->capth->th_counter->tc_counter_mask;
1668 	bt = pps->capth->th_offset;
1669 	bintime_addx(&bt, pps->capth->th_scale * tcount);
1670 	bintime_add(&bt, &boottimebin);
1671 	bintime2timespec(&bt, &ts);
1672 
1673 	/* If the timecounter was wound up underneath us, bail out. */
1674 	if (pps->capgen != pps->capth->th_generation)
1675 		return;
1676 
1677 	*pcount = pps->capcount;
1678 	(*pseq)++;
1679 	*tsp = ts;
1680 
1681 	if (foff) {
1682 		timespecadd(tsp, osp);
1683 		if (tsp->tv_nsec < 0) {
1684 			tsp->tv_nsec += 1000000000;
1685 			tsp->tv_sec -= 1;
1686 		}
1687 	}
1688 
1689 #ifdef FFCLOCK
1690 	*ffcount = pps->capffth->tick_ffcount + tcount;
1691 	bt = pps->capffth->tick_time;
1692 	ffclock_convert_delta(tcount, pps->capffth->cest.period, &bt);
1693 	bintime_add(&bt, &pps->capffth->tick_time);
1694 	bintime2timespec(&bt, &ts);
1695 	(*pseq_ffc)++;
1696 	*tsp_ffc = ts;
1697 #endif
1698 
1699 #ifdef PPS_SYNC
1700 	if (fhard) {
1701 		uint64_t scale;
1702 
1703 		/*
1704 		 * Feed the NTP PLL/FLL.
1705 		 * The FLL wants to know how many (hardware) nanoseconds
1706 		 * elapsed since the previous event.
1707 		 */
1708 		tcount = pps->capcount - pps->ppscount[2];
1709 		pps->ppscount[2] = pps->capcount;
1710 		tcount &= pps->capth->th_counter->tc_counter_mask;
1711 		scale = (uint64_t)1 << 63;
1712 		scale /= pps->capth->th_counter->tc_frequency;
1713 		scale *= 2;
1714 		bt.sec = 0;
1715 		bt.frac = 0;
1716 		bintime_addx(&bt, scale * tcount);
1717 		bintime2timespec(&bt, &ts);
1718 		hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
1719 	}
1720 #endif
1721 
1722 	/* Wakeup anyone sleeping in pps_fetch().  */
1723 	wakeup(pps);
1724 }
1725 
1726 /*
1727  * Timecounters need to be updated every so often to prevent the hardware
1728  * counter from overflowing.  Updating also recalculates the cached values
1729  * used by the get*() family of functions, so their precision depends on
1730  * the update frequency.
1731  */
1732 
1733 static int tc_tick;
1734 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0,
1735     "Approximate number of hardclock ticks in a millisecond");
1736 
1737 void
1738 tc_ticktock(int cnt)
1739 {
1740 	static int count;
1741 
1742 	count += cnt;
1743 	if (count < tc_tick)
1744 		return;
1745 	count = 0;
1746 	tc_windup();
1747 }
1748 
1749 static void
1750 inittimecounter(void *dummy)
1751 {
1752 	u_int p;
1753 
1754 	/*
1755 	 * Set the initial timeout to
1756 	 * max(1, <approx. number of hardclock ticks in a millisecond>).
1757 	 * People should probably not use the sysctl to set the timeout
1758 	 * to smaller than its inital value, since that value is the
1759 	 * smallest reasonable one.  If they want better timestamps they
1760 	 * should use the non-"get"* functions.
1761 	 */
1762 	if (hz > 1000)
1763 		tc_tick = (hz + 500) / 1000;
1764 	else
1765 		tc_tick = 1;
1766 	p = (tc_tick * 1000000) / hz;
1767 	printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
1768 
1769 #ifdef FFCLOCK
1770 	ffclock_init();
1771 #endif
1772 	/* warm up new timecounter (again) and get rolling. */
1773 	(void)timecounter->tc_get_timecount(timecounter);
1774 	(void)timecounter->tc_get_timecount(timecounter);
1775 	tc_windup();
1776 }
1777 
1778 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL);
1779 
1780 /* Cpu tick handling -------------------------------------------------*/
1781 
1782 static int cpu_tick_variable;
1783 static uint64_t	cpu_tick_frequency;
1784 
1785 static uint64_t
1786 tc_cpu_ticks(void)
1787 {
1788 	static uint64_t base;
1789 	static unsigned last;
1790 	unsigned u;
1791 	struct timecounter *tc;
1792 
1793 	tc = timehands->th_counter;
1794 	u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
1795 	if (u < last)
1796 		base += (uint64_t)tc->tc_counter_mask + 1;
1797 	last = u;
1798 	return (u + base);
1799 }
1800 
1801 void
1802 cpu_tick_calibration(void)
1803 {
1804 	static time_t last_calib;
1805 
1806 	if (time_uptime != last_calib && !(time_uptime & 0xf)) {
1807 		cpu_tick_calibrate(0);
1808 		last_calib = time_uptime;
1809 	}
1810 }
1811 
1812 /*
1813  * This function gets called every 16 seconds on only one designated
1814  * CPU in the system from hardclock() via cpu_tick_calibration()().
1815  *
1816  * Whenever the real time clock is stepped we get called with reset=1
1817  * to make sure we handle suspend/resume and similar events correctly.
1818  */
1819 
1820 static void
1821 cpu_tick_calibrate(int reset)
1822 {
1823 	static uint64_t c_last;
1824 	uint64_t c_this, c_delta;
1825 	static struct bintime  t_last;
1826 	struct bintime t_this, t_delta;
1827 	uint32_t divi;
1828 
1829 	if (reset) {
1830 		/* The clock was stepped, abort & reset */
1831 		t_last.sec = 0;
1832 		return;
1833 	}
1834 
1835 	/* we don't calibrate fixed rate cputicks */
1836 	if (!cpu_tick_variable)
1837 		return;
1838 
1839 	getbinuptime(&t_this);
1840 	c_this = cpu_ticks();
1841 	if (t_last.sec != 0) {
1842 		c_delta = c_this - c_last;
1843 		t_delta = t_this;
1844 		bintime_sub(&t_delta, &t_last);
1845 		/*
1846 		 * Headroom:
1847 		 * 	2^(64-20) / 16[s] =
1848 		 * 	2^(44) / 16[s] =
1849 		 * 	17.592.186.044.416 / 16 =
1850 		 * 	1.099.511.627.776 [Hz]
1851 		 */
1852 		divi = t_delta.sec << 20;
1853 		divi |= t_delta.frac >> (64 - 20);
1854 		c_delta <<= 20;
1855 		c_delta /= divi;
1856 		if (c_delta > cpu_tick_frequency) {
1857 			if (0 && bootverbose)
1858 				printf("cpu_tick increased to %ju Hz\n",
1859 				    c_delta);
1860 			cpu_tick_frequency = c_delta;
1861 		}
1862 	}
1863 	c_last = c_this;
1864 	t_last = t_this;
1865 }
1866 
1867 void
1868 set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
1869 {
1870 
1871 	if (func == NULL) {
1872 		cpu_ticks = tc_cpu_ticks;
1873 	} else {
1874 		cpu_tick_frequency = freq;
1875 		cpu_tick_variable = var;
1876 		cpu_ticks = func;
1877 	}
1878 }
1879 
1880 uint64_t
1881 cpu_tickrate(void)
1882 {
1883 
1884 	if (cpu_ticks == tc_cpu_ticks)
1885 		return (tc_getfrequency());
1886 	return (cpu_tick_frequency);
1887 }
1888 
1889 /*
1890  * We need to be slightly careful converting cputicks to microseconds.
1891  * There is plenty of margin in 64 bits of microseconds (half a million
1892  * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
1893  * before divide conversion (to retain precision) we find that the
1894  * margin shrinks to 1.5 hours (one millionth of 146y).
1895  * With a three prong approach we never lose significant bits, no
1896  * matter what the cputick rate and length of timeinterval is.
1897  */
1898 
1899 uint64_t
1900 cputick2usec(uint64_t tick)
1901 {
1902 
1903 	if (tick > 18446744073709551LL)		/* floor(2^64 / 1000) */
1904 		return (tick / (cpu_tickrate() / 1000000LL));
1905 	else if (tick > 18446744073709LL)	/* floor(2^64 / 1000000) */
1906 		return ((tick * 1000LL) / (cpu_tickrate() / 1000LL));
1907 	else
1908 		return ((tick * 1000000LL) / cpu_tickrate());
1909 }
1910 
1911 cpu_tick_f	*cpu_ticks = tc_cpu_ticks;
1912 
1913 static int vdso_th_enable = 1;
1914 static int
1915 sysctl_fast_gettime(SYSCTL_HANDLER_ARGS)
1916 {
1917 	int old_vdso_th_enable, error;
1918 
1919 	old_vdso_th_enable = vdso_th_enable;
1920 	error = sysctl_handle_int(oidp, &old_vdso_th_enable, 0, req);
1921 	if (error != 0)
1922 		return (error);
1923 	vdso_th_enable = old_vdso_th_enable;
1924 	timekeep_push_vdso();
1925 	return (0);
1926 }
1927 SYSCTL_PROC(_kern_timecounter, OID_AUTO, fast_gettime,
1928     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1929     NULL, 0, sysctl_fast_gettime, "I", "Enable fast time of day");
1930 
1931 uint32_t
1932 tc_fill_vdso_timehands(struct vdso_timehands *vdso_th)
1933 {
1934 	struct timehands *th;
1935 	uint32_t enabled;
1936 
1937 	th = timehands;
1938 	vdso_th->th_algo = VDSO_TH_ALGO_1;
1939 	vdso_th->th_scale = th->th_scale;
1940 	vdso_th->th_offset_count = th->th_offset_count;
1941 	vdso_th->th_counter_mask = th->th_counter->tc_counter_mask;
1942 	vdso_th->th_offset = th->th_offset;
1943 	vdso_th->th_boottime = boottimebin;
1944 	enabled = cpu_fill_vdso_timehands(vdso_th);
1945 	if (!vdso_th_enable)
1946 		enabled = 0;
1947 	return (enabled);
1948 }
1949 
1950 #ifdef COMPAT_FREEBSD32
1951 uint32_t
1952 tc_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32)
1953 {
1954 	struct timehands *th;
1955 	uint32_t enabled;
1956 
1957 	th = timehands;
1958 	vdso_th32->th_algo = VDSO_TH_ALGO_1;
1959 	*(uint64_t *)&vdso_th32->th_scale[0] = th->th_scale;
1960 	vdso_th32->th_offset_count = th->th_offset_count;
1961 	vdso_th32->th_counter_mask = th->th_counter->tc_counter_mask;
1962 	vdso_th32->th_offset.sec = th->th_offset.sec;
1963 	*(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac;
1964 	vdso_th32->th_boottime.sec = boottimebin.sec;
1965 	*(uint64_t *)&vdso_th32->th_boottime.frac[0] = boottimebin.frac;
1966 	enabled = cpu_fill_vdso_timehands32(vdso_th32);
1967 	if (!vdso_th_enable)
1968 		enabled = 0;
1969 	return (enabled);
1970 }
1971 #endif
1972