xref: /dflybsd-src/sys/kern/kern_clock.c (revision 6617c2d1da399e5ee926c2d7db0a5ae49590b542)
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * Copyright (c) 1997, 1998 Poul-Henning Kamp <phk@FreeBSD.org>
35  * Copyright (c) 1982, 1986, 1991, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  * (c) UNIX System Laboratories, Inc.
38  * All or some portions of this file are derived from material licensed
39  * to the University of California by American Telephone and Telegraph
40  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41  * the permission of UNIX System Laboratories, Inc.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
68  * $FreeBSD: src/sys/kern/kern_clock.c,v 1.105.2.10 2002/10/17 13:19:40 maxim Exp $
69  */
70 
71 #include "opt_ntp.h"
72 #include "opt_ifpoll.h"
73 #include "opt_pctrack.h"
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/callout.h>
78 #include <sys/kernel.h>
79 #include <sys/kinfo.h>
80 #include <sys/proc.h>
81 #include <sys/malloc.h>
82 #include <sys/resource.h>
83 #include <sys/resourcevar.h>
84 #include <sys/signalvar.h>
85 #include <sys/priv.h>
86 #include <sys/timex.h>
87 #include <sys/timepps.h>
88 #include <sys/upmap.h>
89 #include <vm/vm.h>
90 #include <sys/lock.h>
91 #include <vm/pmap.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_extern.h>
94 #include <sys/sysctl.h>
95 
96 #include <sys/thread2.h>
97 #include <sys/spinlock2.h>
98 
99 #include <machine/cpu.h>
100 #include <machine/limits.h>
101 #include <machine/smp.h>
102 #include <machine/cpufunc.h>
103 #include <machine/specialreg.h>
104 #include <machine/clock.h>
105 
106 #ifdef GPROF
107 #include <sys/gmon.h>
108 #endif
109 
110 #ifdef IFPOLL_ENABLE
111 extern void ifpoll_init_pcpu(int);
112 #endif
113 
114 #ifdef DEBUG_PCTRACK
115 static void do_pctrack(struct intrframe *frame, int which);
116 #endif
117 
118 static void initclocks (void *dummy);
119 SYSINIT(clocks, SI_BOOT2_CLOCKS, SI_ORDER_FIRST, initclocks, NULL);
120 
121 /*
122  * Some of these don't belong here, but it's easiest to concentrate them.
123  * Note that cpu_time counts in microseconds, but most userland programs
124  * just compare relative times against the total by delta.
125  */
126 struct kinfo_cputime cputime_percpu[MAXCPU];
127 #ifdef DEBUG_PCTRACK
128 struct kinfo_pcheader cputime_pcheader = { PCTRACK_SIZE, PCTRACK_ARYSIZE };
129 struct kinfo_pctrack cputime_pctrack[MAXCPU][PCTRACK_SIZE];
130 #endif
131 
132 static int sniff_enable = 1;
133 static int sniff_target = -1;
134 SYSCTL_INT(_kern, OID_AUTO, sniff_enable, CTLFLAG_RW, &sniff_enable, 0 , "");
135 SYSCTL_INT(_kern, OID_AUTO, sniff_target, CTLFLAG_RW, &sniff_target, 0 , "");
136 
137 static int
138 sysctl_cputime(SYSCTL_HANDLER_ARGS)
139 {
140 	int cpu, error = 0;
141 	int root_error;
142 	size_t size = sizeof(struct kinfo_cputime);
143 	struct kinfo_cputime tmp;
144 
145 	/*
146 	 * NOTE: For security reasons, only root can sniff %rip
147 	 */
148 	root_error = priv_check_cred(curthread->td_ucred, PRIV_ROOT, 0);
149 
150 	for (cpu = 0; cpu < ncpus; ++cpu) {
151 		tmp = cputime_percpu[cpu];
152 		if (root_error == 0) {
153 			tmp.cp_sample_pc =
154 				(int64_t)globaldata_find(cpu)->gd_sample_pc;
155 			tmp.cp_sample_sp =
156 				(int64_t)globaldata_find(cpu)->gd_sample_sp;
157 		}
158 		if ((error = SYSCTL_OUT(req, &tmp, size)) != 0)
159 			break;
160 	}
161 
162 	if (root_error == 0) {
163 		if (sniff_enable) {
164 			int n = sniff_target;
165 			if (n < 0)
166 				smp_sniff();
167 			else if (n < ncpus)
168 				cpu_sniff(n);
169 		}
170 	}
171 
172 	return (error);
173 }
174 SYSCTL_PROC(_kern, OID_AUTO, cputime, (CTLTYPE_OPAQUE|CTLFLAG_RD), 0, 0,
175 	sysctl_cputime, "S,kinfo_cputime", "CPU time statistics");
176 
177 static int
178 sysctl_cp_time(SYSCTL_HANDLER_ARGS)
179 {
180 	long cpu_states[CPUSTATES] = {0};
181 	int cpu, error = 0;
182 	size_t size = sizeof(cpu_states);
183 
184 	for (cpu = 0; cpu < ncpus; ++cpu) {
185 		cpu_states[CP_USER] += cputime_percpu[cpu].cp_user;
186 		cpu_states[CP_NICE] += cputime_percpu[cpu].cp_nice;
187 		cpu_states[CP_SYS] += cputime_percpu[cpu].cp_sys;
188 		cpu_states[CP_INTR] += cputime_percpu[cpu].cp_intr;
189 		cpu_states[CP_IDLE] += cputime_percpu[cpu].cp_idle;
190 	}
191 
192 	error = SYSCTL_OUT(req, cpu_states, size);
193 
194 	return (error);
195 }
196 
197 SYSCTL_PROC(_kern, OID_AUTO, cp_time, (CTLTYPE_LONG|CTLFLAG_RD), 0, 0,
198     sysctl_cp_time, "LU", "CPU time statistics");
199 
200 static int
201 sysctl_cp_times(SYSCTL_HANDLER_ARGS)
202 {
203 	long cpu_states[CPUSTATES] = {0};
204 	int cpu, error;
205 	size_t size = sizeof(cpu_states);
206 
207 	for (error = 0, cpu = 0; error == 0 && cpu < ncpus; ++cpu) {
208 		cpu_states[CP_USER] = cputime_percpu[cpu].cp_user;
209 		cpu_states[CP_NICE] = cputime_percpu[cpu].cp_nice;
210 		cpu_states[CP_SYS] = cputime_percpu[cpu].cp_sys;
211 		cpu_states[CP_INTR] = cputime_percpu[cpu].cp_intr;
212 		cpu_states[CP_IDLE] = cputime_percpu[cpu].cp_idle;
213 		error = SYSCTL_OUT(req, cpu_states, size);
214 	}
215 
216 	return (error);
217 }
218 
219 SYSCTL_PROC(_kern, OID_AUTO, cp_times, (CTLTYPE_LONG|CTLFLAG_RD), 0, 0,
220     sysctl_cp_times, "LU", "per-CPU time statistics");
221 
222 /*
223  * boottime is used to calculate the 'real' uptime.  Do not confuse this with
224  * microuptime().  microtime() is not drift compensated.  The real uptime
225  * with compensation is nanotime() - bootime.  boottime is recalculated
226  * whenever the real time is set based on the compensated elapsed time
227  * in seconds (gd->gd_time_seconds).
228  *
229  * The gd_time_seconds and gd_cpuclock_base fields remain fairly monotonic.
230  * Slight adjustments to gd_cpuclock_base are made to phase-lock it to
231  * the real time.
232  *
233  * WARNING! time_second can backstep on time corrections. Also, unlike
234  *          time_second, time_uptime is not a "real" time_t (seconds
235  *          since the Epoch) but seconds since booting.
236  */
237 struct timespec boottime;	/* boot time (realtime) for reference only */
238 time_t time_second;		/* read-only 'passive' realtime in seconds */
239 time_t time_uptime;		/* read-only 'passive' uptime in seconds */
240 
241 /*
242  * basetime is used to calculate the compensated real time of day.  The
243  * basetime can be modified on a per-tick basis by the adjtime(),
244  * ntp_adjtime(), and sysctl-based time correction APIs.
245  *
246  * Note that frequency corrections can also be made by adjusting
247  * gd_cpuclock_base.
248  *
249  * basetime is a tail-chasing FIFO, updated only by cpu #0.  The FIFO is
250  * used on both SMP and UP systems to avoid MP races between cpu's and
251  * interrupt races on UP systems.
252  */
253 struct hardtime {
254 	__uint32_t time_second;
255 	sysclock_t cpuclock_base;
256 };
257 
258 #define BASETIME_ARYSIZE	16
259 #define BASETIME_ARYMASK	(BASETIME_ARYSIZE - 1)
260 static struct timespec basetime[BASETIME_ARYSIZE];
261 static struct hardtime hardtime[BASETIME_ARYSIZE];
262 static volatile int basetime_index;
263 
264 static int
265 sysctl_get_basetime(SYSCTL_HANDLER_ARGS)
266 {
267 	struct timespec *bt;
268 	int error;
269 	int index;
270 
271 	/*
272 	 * Because basetime data and index may be updated by another cpu,
273 	 * a load fence is required to ensure that the data we read has
274 	 * not been speculatively read relative to a possibly updated index.
275 	 */
276 	index = basetime_index;
277 	cpu_lfence();
278 	bt = &basetime[index];
279 	error = SYSCTL_OUT(req, bt, sizeof(*bt));
280 	return (error);
281 }
282 
283 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD,
284     &boottime, timespec, "System boottime");
285 SYSCTL_PROC(_kern, OID_AUTO, basetime, CTLTYPE_STRUCT|CTLFLAG_RD, 0, 0,
286     sysctl_get_basetime, "S,timespec", "System basetime");
287 
288 static void hardclock(systimer_t info, int, struct intrframe *frame);
289 static void statclock(systimer_t info, int, struct intrframe *frame);
290 static void schedclock(systimer_t info, int, struct intrframe *frame);
291 static void getnanotime_nbt(struct timespec *nbt, struct timespec *tsp);
292 
293 int	ticks;			/* system master ticks at hz */
294 int	clocks_running;		/* tsleep/timeout clocks operational */
295 int64_t	nsec_adj;		/* ntpd per-tick adjustment in nsec << 32 */
296 int64_t	nsec_acc;		/* accumulator */
297 int	sched_ticks;		/* global schedule clock ticks */
298 
299 /* NTPD time correction fields */
300 int64_t	ntp_tick_permanent;	/* per-tick adjustment in nsec << 32 */
301 int64_t	ntp_tick_acc;		/* accumulator for per-tick adjustment */
302 int64_t	ntp_delta;		/* one-time correction in nsec */
303 int64_t ntp_big_delta = 1000000000;
304 int32_t	ntp_tick_delta;		/* current adjustment rate */
305 int32_t	ntp_default_tick_delta;	/* adjustment rate for ntp_delta */
306 time_t	ntp_leap_second;	/* time of next leap second */
307 int	ntp_leap_insert;	/* whether to insert or remove a second */
308 struct spinlock ntp_spin;
309 
310 /*
311  * Finish initializing clock frequencies and start all clocks running.
312  */
313 /* ARGSUSED*/
314 static void
315 initclocks(void *dummy)
316 {
317 	/*psratio = profhz / stathz;*/
318 	spin_init(&ntp_spin, "ntp");
319 	initclocks_pcpu();
320 	clocks_running = 1;
321 	if (kpmap) {
322 	    kpmap->tsc_freq = (uint64_t)tsc_frequency;
323 	    kpmap->tick_freq = hz;
324 	}
325 }
326 
327 /*
328  * Called on a per-cpu basis from the idle thread bootstrap on each cpu
329  * during SMP initialization.
330  *
331  * This routine is called concurrently during low-level SMP initialization
332  * and may not block in any way.  Meaning, among other things, we can't
333  * acquire any tokens.
334  */
335 void
336 initclocks_pcpu(void)
337 {
338 	struct globaldata *gd = mycpu;
339 
340 	crit_enter();
341 	if (gd->gd_cpuid == 0) {
342 	    gd->gd_time_seconds = 1;
343 	    gd->gd_cpuclock_base = sys_cputimer->count();
344 	    hardtime[0].time_second = gd->gd_time_seconds;
345 	    hardtime[0].cpuclock_base = gd->gd_cpuclock_base;
346 	} else {
347 	    gd->gd_time_seconds = globaldata_find(0)->gd_time_seconds;
348 	    gd->gd_cpuclock_base = globaldata_find(0)->gd_cpuclock_base;
349 	}
350 
351 	systimer_intr_enable();
352 
353 	crit_exit();
354 }
355 
356 /*
357  * This routine is called on just the BSP, just after SMP initialization
358  * completes to * finish initializing any clocks that might contend/block
359  * (e.g. like on a token).  We can't do this in initclocks_pcpu() because
360  * that function is called from the idle thread bootstrap for each cpu and
361  * not allowed to block at all.
362  */
363 static
364 void
365 initclocks_other(void *dummy)
366 {
367 	struct globaldata *ogd = mycpu;
368 	struct globaldata *gd;
369 	int n;
370 
371 	for (n = 0; n < ncpus; ++n) {
372 		lwkt_setcpu_self(globaldata_find(n));
373 		gd = mycpu;
374 
375 		/*
376 		 * Use a non-queued periodic systimer to prevent multiple
377 		 * ticks from building up if the sysclock jumps forward
378 		 * (8254 gets reset).  The sysclock will never jump backwards.
379 		 * Our time sync is based on the actual sysclock, not the
380 		 * ticks count.
381 		 *
382 		 * Install statclock before hardclock to prevent statclock
383 		 * from misinterpreting gd_flags for tick assignment when
384 		 * they overlap.
385 		 */
386 		systimer_init_periodic_nq(&gd->gd_statclock, statclock,
387 					  NULL, stathz);
388 		systimer_init_periodic_nq(&gd->gd_hardclock, hardclock,
389 					  NULL, hz);
390 		/* XXX correct the frequency for scheduler / estcpu tests */
391 		systimer_init_periodic_nq(&gd->gd_schedclock, schedclock,
392 					  NULL, ESTCPUFREQ);
393 #ifdef IFPOLL_ENABLE
394 		ifpoll_init_pcpu(gd->gd_cpuid);
395 #endif
396 	}
397 	lwkt_setcpu_self(ogd);
398 }
399 SYSINIT(clocks2, SI_BOOT2_POST_SMP, SI_ORDER_ANY, initclocks_other, NULL);
400 
401 /*
402  * This sets the current real time of day.  Timespecs are in seconds and
403  * nanoseconds.  We do not mess with gd_time_seconds and gd_cpuclock_base,
404  * instead we adjust basetime so basetime + gd_* results in the current
405  * time of day.  This way the gd_* fields are guaranteed to represent
406  * a monotonically increasing 'uptime' value.
407  *
408  * When set_timeofday() is called from userland, the system call forces it
409  * onto cpu #0 since only cpu #0 can update basetime_index.
410  */
411 void
412 set_timeofday(struct timespec *ts)
413 {
414 	struct timespec *nbt;
415 	int ni;
416 
417 	/*
418 	 * XXX SMP / non-atomic basetime updates
419 	 */
420 	crit_enter();
421 	ni = (basetime_index + 1) & BASETIME_ARYMASK;
422 	cpu_lfence();
423 	nbt = &basetime[ni];
424 	nanouptime(nbt);
425 	nbt->tv_sec = ts->tv_sec - nbt->tv_sec;
426 	nbt->tv_nsec = ts->tv_nsec - nbt->tv_nsec;
427 	if (nbt->tv_nsec < 0) {
428 	    nbt->tv_nsec += 1000000000;
429 	    --nbt->tv_sec;
430 	}
431 
432 	/*
433 	 * Note that basetime diverges from boottime as the clock drift is
434 	 * compensated for, so we cannot do away with boottime.  When setting
435 	 * the absolute time of day the drift is 0 (for an instant) and we
436 	 * can simply assign boottime to basetime.
437 	 *
438 	 * Note that nanouptime() is based on gd_time_seconds which is drift
439 	 * compensated up to a point (it is guaranteed to remain monotonically
440 	 * increasing).  gd_time_seconds is thus our best uptime guess and
441 	 * suitable for use in the boottime calculation.  It is already taken
442 	 * into account in the basetime calculation above.
443 	 */
444 	spin_lock(&ntp_spin);
445 	boottime.tv_sec = nbt->tv_sec;
446 	ntp_delta = 0;
447 
448 	/*
449 	 * We now have a new basetime, make sure all other cpus have it,
450 	 * then update the index.
451 	 */
452 	cpu_sfence();
453 	basetime_index = ni;
454 	spin_unlock(&ntp_spin);
455 
456 	crit_exit();
457 }
458 
459 /*
460  * Each cpu has its own hardclock, but we only increments ticks and softticks
461  * on cpu #0.
462  *
463  * NOTE! systimer! the MP lock might not be held here.  We can only safely
464  * manipulate objects owned by the current cpu.
465  */
466 static void
467 hardclock(systimer_t info, int in_ipi, struct intrframe *frame)
468 {
469 	sysclock_t cputicks;
470 	struct proc *p;
471 	struct globaldata *gd = mycpu;
472 
473 	if ((gd->gd_reqflags & RQF_IPIQ) == 0 && lwkt_need_ipiq_process(gd)) {
474 		/* Defer to doreti on passive IPIQ processing */
475 		need_ipiq();
476 	}
477 
478 	/*
479 	 * We update the compensation base to calculate fine-grained time
480 	 * from the sys_cputimer on a per-cpu basis in order to avoid
481 	 * having to mess around with locks.  sys_cputimer is assumed to
482 	 * be consistent across all cpus.  CPU N copies the base state from
483 	 * CPU 0 using the same FIFO trick that we use for basetime (so we
484 	 * don't catch a CPU 0 update in the middle).
485 	 *
486 	 * Note that we never allow info->time (aka gd->gd_hardclock.time)
487 	 * to reverse index gd_cpuclock_base, but that it is possible for
488 	 * it to temporarily get behind in the seconds if something in the
489 	 * system locks interrupts for a long period of time.  Since periodic
490 	 * timers count events, though everything should resynch again
491 	 * immediately.
492 	 */
493 	if (gd->gd_cpuid == 0) {
494 		int ni;
495 
496 		cputicks = info->time - gd->gd_cpuclock_base;
497 		if (cputicks >= sys_cputimer->freq) {
498 			cputicks /= sys_cputimer->freq;
499 			if (cputicks != 0 && cputicks != 1)
500 				kprintf("Warning: hardclock missed > 1 sec\n");
501 			gd->gd_time_seconds += cputicks;
502 			gd->gd_cpuclock_base += sys_cputimer->freq * cputicks;
503 			/* uncorrected monotonic 1-sec gran */
504 			time_uptime += cputicks;
505 		}
506 		ni = (basetime_index + 1) & BASETIME_ARYMASK;
507 		hardtime[ni].time_second = gd->gd_time_seconds;
508 		hardtime[ni].cpuclock_base = gd->gd_cpuclock_base;
509 	} else {
510 		int ni;
511 
512 		ni = basetime_index;
513 		cpu_lfence();
514 		gd->gd_time_seconds = hardtime[ni].time_second;
515 		gd->gd_cpuclock_base = hardtime[ni].cpuclock_base;
516 	}
517 
518 	/*
519 	 * The system-wide ticks counter and NTP related timedelta/tickdelta
520 	 * adjustments only occur on cpu #0.  NTP adjustments are accomplished
521 	 * by updating basetime.
522 	 */
523 	if (gd->gd_cpuid == 0) {
524 	    struct timespec *nbt;
525 	    struct timespec nts;
526 	    int leap;
527 	    int ni;
528 
529 	    ++ticks;
530 
531 #if 0
532 	    if (tco->tc_poll_pps)
533 		tco->tc_poll_pps(tco);
534 #endif
535 
536 	    /*
537 	     * Calculate the new basetime index.  We are in a critical section
538 	     * on cpu #0 and can safely play with basetime_index.  Start
539 	     * with the current basetime and then make adjustments.
540 	     */
541 	    ni = (basetime_index + 1) & BASETIME_ARYMASK;
542 	    nbt = &basetime[ni];
543 	    *nbt = basetime[basetime_index];
544 
545 	    /*
546 	     * ntp adjustments only occur on cpu 0 and are protected by
547 	     * ntp_spin.  This spinlock virtually never conflicts.
548 	     */
549 	    spin_lock(&ntp_spin);
550 
551 	    /*
552 	     * Apply adjtime corrections.  (adjtime() API)
553 	     *
554 	     * adjtime() only runs on cpu #0 so our critical section is
555 	     * sufficient to access these variables.
556 	     */
557 	    if (ntp_delta != 0) {
558 		nbt->tv_nsec += ntp_tick_delta;
559 		ntp_delta -= ntp_tick_delta;
560 		if ((ntp_delta > 0 && ntp_delta < ntp_tick_delta) ||
561 		    (ntp_delta < 0 && ntp_delta > ntp_tick_delta)) {
562 			ntp_tick_delta = ntp_delta;
563  		}
564  	    }
565 
566 	    /*
567 	     * Apply permanent frequency corrections.  (sysctl API)
568 	     */
569 	    if (ntp_tick_permanent != 0) {
570 		ntp_tick_acc += ntp_tick_permanent;
571 		if (ntp_tick_acc >= (1LL << 32)) {
572 		    nbt->tv_nsec += ntp_tick_acc >> 32;
573 		    ntp_tick_acc -= (ntp_tick_acc >> 32) << 32;
574 		} else if (ntp_tick_acc <= -(1LL << 32)) {
575 		    /* Negate ntp_tick_acc to avoid shifting the sign bit. */
576 		    nbt->tv_nsec -= (-ntp_tick_acc) >> 32;
577 		    ntp_tick_acc += ((-ntp_tick_acc) >> 32) << 32;
578 		}
579  	    }
580 
581 	    if (nbt->tv_nsec >= 1000000000) {
582 		    nbt->tv_sec++;
583 		    nbt->tv_nsec -= 1000000000;
584 	    } else if (nbt->tv_nsec < 0) {
585 		    nbt->tv_sec--;
586 		    nbt->tv_nsec += 1000000000;
587 	    }
588 
589 	    /*
590 	     * Another per-tick compensation.  (for ntp_adjtime() API)
591 	     */
592 	    if (nsec_adj != 0) {
593 		nsec_acc += nsec_adj;
594 		if (nsec_acc >= 0x100000000LL) {
595 		    nbt->tv_nsec += nsec_acc >> 32;
596 		    nsec_acc = (nsec_acc & 0xFFFFFFFFLL);
597 		} else if (nsec_acc <= -0x100000000LL) {
598 		    nbt->tv_nsec -= -nsec_acc >> 32;
599 		    nsec_acc = -(-nsec_acc & 0xFFFFFFFFLL);
600 		}
601 		if (nbt->tv_nsec >= 1000000000) {
602 		    nbt->tv_nsec -= 1000000000;
603 		    ++nbt->tv_sec;
604 		} else if (nbt->tv_nsec < 0) {
605 		    nbt->tv_nsec += 1000000000;
606 		    --nbt->tv_sec;
607 		}
608 	    }
609 	    spin_unlock(&ntp_spin);
610 
611 	    /************************************************************
612 	     *			LEAP SECOND CORRECTION			*
613 	     ************************************************************
614 	     *
615 	     * Taking into account all the corrections made above, figure
616 	     * out the new real time.  If the seconds field has changed
617 	     * then apply any pending leap-second corrections.
618 	     */
619 	    getnanotime_nbt(nbt, &nts);
620 
621 	    if (time_second != nts.tv_sec) {
622 		/*
623 		 * Apply leap second (sysctl API).  Adjust nts for changes
624 		 * so we do not have to call getnanotime_nbt again.
625 		 */
626 		if (ntp_leap_second) {
627 		    if (ntp_leap_second == nts.tv_sec) {
628 			if (ntp_leap_insert) {
629 			    nbt->tv_sec++;
630 			    nts.tv_sec++;
631 			} else {
632 			    nbt->tv_sec--;
633 			    nts.tv_sec--;
634 			}
635 			ntp_leap_second--;
636 		    }
637 		}
638 
639 		/*
640 		 * Apply leap second (ntp_adjtime() API), calculate a new
641 		 * nsec_adj field.  ntp_update_second() returns nsec_adj
642 		 * as a per-second value but we need it as a per-tick value.
643 		 */
644 		leap = ntp_update_second(time_second, &nsec_adj);
645 		nsec_adj /= hz;
646 		nbt->tv_sec += leap;
647 		nts.tv_sec += leap;
648 
649 		/*
650 		 * Update the time_second 'approximate time' global.
651 		 */
652 		time_second = nts.tv_sec;
653 	    }
654 
655 	    /*
656 	     * Finally, our new basetime is ready to go live!
657 	     */
658 	    cpu_sfence();
659 	    basetime_index = ni;
660 
661 	    /*
662 	     * Update kpmap on each tick.  TS updates are integrated with
663 	     * fences and upticks allowing userland to read the data
664 	     * deterministically.
665 	     */
666 	    if (kpmap) {
667 		int w;
668 
669 		w = (kpmap->upticks + 1) & 1;
670 		getnanouptime(&kpmap->ts_uptime[w]);
671 		getnanotime(&kpmap->ts_realtime[w]);
672 		cpu_sfence();
673 		++kpmap->upticks;
674 		cpu_sfence();
675 	    }
676 	}
677 
678 	/*
679 	 * lwkt thread scheduler fair queueing
680 	 */
681 	lwkt_schedulerclock(curthread);
682 
683 	/*
684 	 * softticks are handled for all cpus
685 	 */
686 	hardclock_softtick(gd);
687 
688 	/*
689 	 * Rollup accumulated vmstats, copy-back for critical path checks.
690 	 */
691 	vmstats_rollup_cpu(gd);
692 	mycpu->gd_vmstats = vmstats;
693 
694 	/*
695 	 * ITimer handling is per-tick, per-cpu.
696 	 *
697 	 * We must acquire the per-process token in order for ksignal()
698 	 * to be non-blocking.  For the moment this requires an AST fault,
699 	 * the ksignal() cannot be safely issued from this hard interrupt.
700 	 *
701 	 * XXX Even the trytoken here isn't right, and itimer operation in
702 	 *     a multi threaded environment is going to be weird at the
703 	 *     very least.
704 	 */
705 	if ((p = curproc) != NULL && lwkt_trytoken(&p->p_token)) {
706 		crit_enter_hard();
707 		if (p->p_upmap)
708 			++p->p_upmap->runticks;
709 
710 		if (frame && CLKF_USERMODE(frame) &&
711 		    timevalisset(&p->p_timer[ITIMER_VIRTUAL].it_value) &&
712 		    itimerdecr(&p->p_timer[ITIMER_VIRTUAL], ustick) == 0) {
713 			p->p_flags |= P_SIGVTALRM;
714 			need_user_resched();
715 		}
716 		if (timevalisset(&p->p_timer[ITIMER_PROF].it_value) &&
717 		    itimerdecr(&p->p_timer[ITIMER_PROF], ustick) == 0) {
718 			p->p_flags |= P_SIGPROF;
719 			need_user_resched();
720 		}
721 		crit_exit_hard();
722 		lwkt_reltoken(&p->p_token);
723 	}
724 	setdelayed();
725 }
726 
727 /*
728  * The statistics clock typically runs at a 125Hz rate, and is intended
729  * to be frequency offset from the hardclock (typ 100Hz).  It is per-cpu.
730  *
731  * NOTE! systimer! the MP lock might not be held here.  We can only safely
732  * manipulate objects owned by the current cpu.
733  *
734  * The stats clock is responsible for grabbing a profiling sample.
735  * Most of the statistics are only used by user-level statistics programs.
736  * The main exceptions are p->p_uticks, p->p_sticks, p->p_iticks, and
737  * p->p_estcpu.
738  *
739  * Like the other clocks, the stat clock is called from what is effectively
740  * a fast interrupt, so the context should be the thread/process that got
741  * interrupted.
742  */
743 static void
744 statclock(systimer_t info, int in_ipi, struct intrframe *frame)
745 {
746 #ifdef GPROF
747 	struct gmonparam *g;
748 	int i;
749 #endif
750 	globaldata_t gd = mycpu;
751 	thread_t td;
752 	struct proc *p;
753 	int bump;
754 	sysclock_t cv;
755 	sysclock_t scv;
756 
757 	/*
758 	 * How big was our timeslice relative to the last time?  Calculate
759 	 * in microseconds.
760 	 *
761 	 * NOTE: Use of microuptime() is typically MPSAFE, but usually not
762 	 *	 during early boot.  Just use the systimer count to be nice
763 	 *	 to e.g. qemu.  The systimer has a better chance of being
764 	 *	 MPSAFE at early boot.
765 	 */
766 	cv = sys_cputimer->count();
767 	scv = gd->statint.gd_statcv;
768 	if (scv == 0) {
769 		bump = 1;
770 	} else {
771 		bump = (sys_cputimer->freq64_usec * (cv - scv)) >> 32;
772 		if (bump < 0)
773 			bump = 0;
774 		if (bump > 1000000)
775 			bump = 1000000;
776 	}
777 	gd->statint.gd_statcv = cv;
778 
779 #if 0
780 	stv = &gd->gd_stattv;
781 	if (stv->tv_sec == 0) {
782 	    bump = 1;
783 	} else {
784 	    bump = tv.tv_usec - stv->tv_usec +
785 		(tv.tv_sec - stv->tv_sec) * 1000000;
786 	    if (bump < 0)
787 		bump = 0;
788 	    if (bump > 1000000)
789 		bump = 1000000;
790 	}
791 	*stv = tv;
792 #endif
793 
794 	td = curthread;
795 	p = td->td_proc;
796 
797 	if (frame && CLKF_USERMODE(frame)) {
798 		/*
799 		 * Came from userland, handle user time and deal with
800 		 * possible process.
801 		 */
802 		if (p && (p->p_flags & P_PROFIL))
803 			addupc_intr(p, CLKF_PC(frame), 1);
804 		td->td_uticks += bump;
805 
806 		/*
807 		 * Charge the time as appropriate
808 		 */
809 		if (p && p->p_nice > NZERO)
810 			cpu_time.cp_nice += bump;
811 		else
812 			cpu_time.cp_user += bump;
813 	} else {
814 		int intr_nest = gd->gd_intr_nesting_level;
815 
816 		if (in_ipi) {
817 			/*
818 			 * IPI processing code will bump gd_intr_nesting_level
819 			 * up by one, which breaks following CLKF_INTR testing,
820 			 * so we subtract it by one here.
821 			 */
822 			--intr_nest;
823 		}
824 #ifdef GPROF
825 		/*
826 		 * Kernel statistics are just like addupc_intr, only easier.
827 		 */
828 		g = &_gmonparam;
829 		if (g->state == GMON_PROF_ON && frame) {
830 			i = CLKF_PC(frame) - g->lowpc;
831 			if (i < g->textsize) {
832 				i /= HISTFRACTION * sizeof(*g->kcount);
833 				g->kcount[i]++;
834 			}
835 		}
836 #endif
837 
838 #define IS_INTR_RUNNING	((frame && CLKF_INTR(intr_nest)) || CLKF_INTR_TD(td))
839 
840 		/*
841 		 * Came from kernel mode, so we were:
842 		 * - handling an interrupt,
843 		 * - doing syscall or trap work on behalf of the current
844 		 *   user process, or
845 		 * - spinning in the idle loop.
846 		 * Whichever it is, charge the time as appropriate.
847 		 * Note that we charge interrupts to the current process,
848 		 * regardless of whether they are ``for'' that process,
849 		 * so that we know how much of its real time was spent
850 		 * in ``non-process'' (i.e., interrupt) work.
851 		 *
852 		 * XXX assume system if frame is NULL.  A NULL frame
853 		 * can occur if ipi processing is done from a crit_exit().
854 		 */
855 		if (IS_INTR_RUNNING) {
856 			/*
857 			 * If we interrupted an interrupt thread, well,
858 			 * count it as interrupt time.
859 			 */
860 			td->td_iticks += bump;
861 #ifdef DEBUG_PCTRACK
862 			if (frame)
863 				do_pctrack(frame, PCTRACK_INT);
864 #endif
865 			cpu_time.cp_intr += bump;
866 		} else if (gd->gd_flags & GDF_VIRTUSER) {
867 			/*
868 			 * The vkernel doesn't do a good job providing trap
869 			 * frames that we can test.  If the GDF_VIRTUSER
870 			 * flag is set we probably interrupted user mode.
871 			 *
872 			 * We also use this flag on the host when entering
873 			 * VMM mode.
874 			 */
875 			td->td_uticks += bump;
876 
877 			/*
878 			 * Charge the time as appropriate
879 			 */
880 			if (p && p->p_nice > NZERO)
881 				cpu_time.cp_nice += bump;
882 			else
883 				cpu_time.cp_user += bump;
884 		} else {
885 			td->td_sticks += bump;
886 			if (td == &gd->gd_idlethread) {
887 				/*
888 				 * Token contention can cause us to mis-count
889 				 * a contended as idle, but it doesn't work
890 				 * properly for VKERNELs so just test on a
891 				 * real kernel.
892 				 */
893 #ifdef _KERNEL_VIRTUAL
894 				cpu_time.cp_idle += bump;
895 #else
896 				if (mycpu->gd_reqflags & RQF_IDLECHECK_WK_MASK)
897 					cpu_time.cp_sys += bump;
898 				else
899 					cpu_time.cp_idle += bump;
900 #endif
901 			} else {
902 				/*
903 				 * System thread was running.
904 				 */
905 #ifdef DEBUG_PCTRACK
906 				if (frame)
907 					do_pctrack(frame, PCTRACK_SYS);
908 #endif
909 				cpu_time.cp_sys += bump;
910 			}
911 		}
912 
913 #undef IS_INTR_RUNNING
914 	}
915 }
916 
917 #ifdef DEBUG_PCTRACK
918 /*
919  * Sample the PC when in the kernel or in an interrupt.  User code can
920  * retrieve the information and generate a histogram or other output.
921  */
922 
923 static void
924 do_pctrack(struct intrframe *frame, int which)
925 {
926 	struct kinfo_pctrack *pctrack;
927 
928 	pctrack = &cputime_pctrack[mycpu->gd_cpuid][which];
929 	pctrack->pc_array[pctrack->pc_index & PCTRACK_ARYMASK] =
930 		(void *)CLKF_PC(frame);
931 	++pctrack->pc_index;
932 }
933 
934 static int
935 sysctl_pctrack(SYSCTL_HANDLER_ARGS)
936 {
937 	struct kinfo_pcheader head;
938 	int error;
939 	int cpu;
940 	int ntrack;
941 
942 	head.pc_ntrack = PCTRACK_SIZE;
943 	head.pc_arysize = PCTRACK_ARYSIZE;
944 
945 	if ((error = SYSCTL_OUT(req, &head, sizeof(head))) != 0)
946 		return (error);
947 
948 	for (cpu = 0; cpu < ncpus; ++cpu) {
949 		for (ntrack = 0; ntrack < PCTRACK_SIZE; ++ntrack) {
950 			error = SYSCTL_OUT(req, &cputime_pctrack[cpu][ntrack],
951 					   sizeof(struct kinfo_pctrack));
952 			if (error)
953 				break;
954 		}
955 		if (error)
956 			break;
957 	}
958 	return (error);
959 }
960 SYSCTL_PROC(_kern, OID_AUTO, pctrack, (CTLTYPE_OPAQUE|CTLFLAG_RD), 0, 0,
961 	sysctl_pctrack, "S,kinfo_pcheader", "CPU PC tracking");
962 
963 #endif
964 
965 /*
966  * The scheduler clock typically runs at a 50Hz rate.  NOTE! systimer,
967  * the MP lock might not be held.  We can safely manipulate parts of curproc
968  * but that's about it.
969  *
970  * Each cpu has its own scheduler clock.
971  */
972 static void
973 schedclock(systimer_t info, int in_ipi __unused, struct intrframe *frame)
974 {
975 	struct lwp *lp;
976 	struct rusage *ru;
977 	struct vmspace *vm;
978 	long rss;
979 
980 	if ((lp = lwkt_preempted_proc()) != NULL) {
981 		/*
982 		 * Account for cpu time used and hit the scheduler.  Note
983 		 * that this call MUST BE MP SAFE, and the BGL IS NOT HELD
984 		 * HERE.
985 		 */
986 		++lp->lwp_cpticks;
987 		usched_schedulerclock(lp, info->periodic, info->time);
988 	} else {
989 		usched_schedulerclock(NULL, info->periodic, info->time);
990 	}
991 	if ((lp = curthread->td_lwp) != NULL) {
992 		/*
993 		 * Update resource usage integrals and maximums.
994 		 */
995 		if ((ru = &lp->lwp_proc->p_ru) &&
996 		    (vm = lp->lwp_proc->p_vmspace) != NULL) {
997 			ru->ru_ixrss += pgtok(vm->vm_tsize);
998 			ru->ru_idrss += pgtok(vm->vm_dsize);
999 			ru->ru_isrss += pgtok(vm->vm_ssize);
1000 			if (lwkt_trytoken(&vm->vm_map.token)) {
1001 				rss = pgtok(vmspace_resident_count(vm));
1002 				if (ru->ru_maxrss < rss)
1003 					ru->ru_maxrss = rss;
1004 				lwkt_reltoken(&vm->vm_map.token);
1005 			}
1006 		}
1007 	}
1008 	/* Increment the global sched_ticks */
1009 	if (mycpu->gd_cpuid == 0)
1010 		++sched_ticks;
1011 }
1012 
1013 /*
1014  * Compute number of ticks for the specified amount of time.  The
1015  * return value is intended to be used in a clock interrupt timed
1016  * operation and guaranteed to meet or exceed the requested time.
1017  * If the representation overflows, return INT_MAX.  The minimum return
1018  * value is 1 ticks and the function will average the calculation up.
1019  * If any value greater then 0 microseconds is supplied, a value
1020  * of at least 2 will be returned to ensure that a near-term clock
1021  * interrupt does not cause the timeout to occur (degenerately) early.
1022  *
1023  * Note that limit checks must take into account microseconds, which is
1024  * done simply by using the smaller signed long maximum instead of
1025  * the unsigned long maximum.
1026  *
1027  * If ints have 32 bits, then the maximum value for any timeout in
1028  * 10ms ticks is 248 days.
1029  */
1030 int
1031 tvtohz_high(struct timeval *tv)
1032 {
1033 	int ticks;
1034 	long sec, usec;
1035 
1036 	sec = tv->tv_sec;
1037 	usec = tv->tv_usec;
1038 	if (usec < 0) {
1039 		sec--;
1040 		usec += 1000000;
1041 	}
1042 	if (sec < 0) {
1043 #ifdef DIAGNOSTIC
1044 		if (usec > 0) {
1045 			sec++;
1046 			usec -= 1000000;
1047 		}
1048 		kprintf("tvtohz_high: negative time difference "
1049 			"%ld sec %ld usec\n",
1050 			sec, usec);
1051 #endif
1052 		ticks = 1;
1053 	} else if (sec <= INT_MAX / hz) {
1054 		ticks = (int)(sec * hz +
1055 			    ((u_long)usec + (ustick - 1)) / ustick) + 1;
1056 	} else {
1057 		ticks = INT_MAX;
1058 	}
1059 	return (ticks);
1060 }
1061 
1062 int
1063 tstohz_high(struct timespec *ts)
1064 {
1065 	int ticks;
1066 	long sec, nsec;
1067 
1068 	sec = ts->tv_sec;
1069 	nsec = ts->tv_nsec;
1070 	if (nsec < 0) {
1071 		sec--;
1072 		nsec += 1000000000;
1073 	}
1074 	if (sec < 0) {
1075 #ifdef DIAGNOSTIC
1076 		if (nsec > 0) {
1077 			sec++;
1078 			nsec -= 1000000000;
1079 		}
1080 		kprintf("tstohz_high: negative time difference "
1081 			"%ld sec %ld nsec\n",
1082 			sec, nsec);
1083 #endif
1084 		ticks = 1;
1085 	} else if (sec <= INT_MAX / hz) {
1086 		ticks = (int)(sec * hz +
1087 			    ((u_long)nsec + (nstick - 1)) / nstick) + 1;
1088 	} else {
1089 		ticks = INT_MAX;
1090 	}
1091 	return (ticks);
1092 }
1093 
1094 
1095 /*
1096  * Compute number of ticks for the specified amount of time, erroring on
1097  * the side of it being too low to ensure that sleeping the returned number
1098  * of ticks will not result in a late return.
1099  *
1100  * The supplied timeval may not be negative and should be normalized.  A
1101  * return value of 0 is possible if the timeval converts to less then
1102  * 1 tick.
1103  *
1104  * If ints have 32 bits, then the maximum value for any timeout in
1105  * 10ms ticks is 248 days.
1106  */
1107 int
1108 tvtohz_low(struct timeval *tv)
1109 {
1110 	int ticks;
1111 	long sec;
1112 
1113 	sec = tv->tv_sec;
1114 	if (sec <= INT_MAX / hz)
1115 		ticks = (int)(sec * hz + (u_long)tv->tv_usec / ustick);
1116 	else
1117 		ticks = INT_MAX;
1118 	return (ticks);
1119 }
1120 
1121 int
1122 tstohz_low(struct timespec *ts)
1123 {
1124 	int ticks;
1125 	long sec;
1126 
1127 	sec = ts->tv_sec;
1128 	if (sec <= INT_MAX / hz)
1129 		ticks = (int)(sec * hz + (u_long)ts->tv_nsec / nstick);
1130 	else
1131 		ticks = INT_MAX;
1132 	return (ticks);
1133 }
1134 
1135 /*
1136  * Start profiling on a process.
1137  *
1138  * Caller must hold p->p_token();
1139  *
1140  * Kernel profiling passes proc0 which never exits and hence
1141  * keeps the profile clock running constantly.
1142  */
1143 void
1144 startprofclock(struct proc *p)
1145 {
1146 	if ((p->p_flags & P_PROFIL) == 0) {
1147 		p->p_flags |= P_PROFIL;
1148 #if 0	/* XXX */
1149 		if (++profprocs == 1 && stathz != 0) {
1150 			crit_enter();
1151 			psdiv = psratio;
1152 			setstatclockrate(profhz);
1153 			crit_exit();
1154 		}
1155 #endif
1156 	}
1157 }
1158 
1159 /*
1160  * Stop profiling on a process.
1161  *
1162  * caller must hold p->p_token
1163  */
1164 void
1165 stopprofclock(struct proc *p)
1166 {
1167 	if (p->p_flags & P_PROFIL) {
1168 		p->p_flags &= ~P_PROFIL;
1169 #if 0	/* XXX */
1170 		if (--profprocs == 0 && stathz != 0) {
1171 			crit_enter();
1172 			psdiv = 1;
1173 			setstatclockrate(stathz);
1174 			crit_exit();
1175 		}
1176 #endif
1177 	}
1178 }
1179 
1180 /*
1181  * Return information about system clocks.
1182  */
1183 static int
1184 sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
1185 {
1186 	struct kinfo_clockinfo clkinfo;
1187 	/*
1188 	 * Construct clockinfo structure.
1189 	 */
1190 	clkinfo.ci_hz = hz;
1191 	clkinfo.ci_tick = ustick;
1192 	clkinfo.ci_tickadj = ntp_default_tick_delta / 1000;
1193 	clkinfo.ci_profhz = profhz;
1194 	clkinfo.ci_stathz = stathz ? stathz : hz;
1195 	return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
1196 }
1197 
1198 SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
1199 	0, 0, sysctl_kern_clockrate, "S,clockinfo","");
1200 
1201 /*
1202  * We have eight functions for looking at the clock, four for
1203  * microseconds and four for nanoseconds.  For each there is fast
1204  * but less precise version "get{nano|micro}[up]time" which will
1205  * return a time which is up to 1/HZ previous to the call, whereas
1206  * the raw version "{nano|micro}[up]time" will return a timestamp
1207  * which is as precise as possible.  The "up" variants return the
1208  * time relative to system boot, these are well suited for time
1209  * interval measurements.
1210  *
1211  * Each cpu independently maintains the current time of day, so all
1212  * we need to do to protect ourselves from changes is to do a loop
1213  * check on the seconds field changing out from under us.
1214  *
1215  * The system timer maintains a 32 bit count and due to various issues
1216  * it is possible for the calculated delta to occasionally exceed
1217  * sys_cputimer->freq.  If this occurs the sys_cputimer->freq64_nsec
1218  * multiplication can easily overflow, so we deal with the case.  For
1219  * uniformity we deal with the case in the usec case too.
1220  *
1221  * All the [get][micro,nano][time,uptime]() routines are MPSAFE.
1222  */
1223 void
1224 getmicrouptime(struct timeval *tvp)
1225 {
1226 	struct globaldata *gd = mycpu;
1227 	sysclock_t delta;
1228 
1229 	do {
1230 		tvp->tv_sec = gd->gd_time_seconds;
1231 		delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
1232 	} while (tvp->tv_sec != gd->gd_time_seconds);
1233 
1234 	if (delta >= sys_cputimer->freq) {
1235 		tvp->tv_sec += delta / sys_cputimer->freq;
1236 		delta %= sys_cputimer->freq;
1237 	}
1238 	tvp->tv_usec = (sys_cputimer->freq64_usec * delta) >> 32;
1239 	if (tvp->tv_usec >= 1000000) {
1240 		tvp->tv_usec -= 1000000;
1241 		++tvp->tv_sec;
1242 	}
1243 }
1244 
1245 void
1246 getnanouptime(struct timespec *tsp)
1247 {
1248 	struct globaldata *gd = mycpu;
1249 	sysclock_t delta;
1250 
1251 	do {
1252 		tsp->tv_sec = gd->gd_time_seconds;
1253 		delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
1254 	} while (tsp->tv_sec != gd->gd_time_seconds);
1255 
1256 	if (delta >= sys_cputimer->freq) {
1257 		tsp->tv_sec += delta / sys_cputimer->freq;
1258 		delta %= sys_cputimer->freq;
1259 	}
1260 	tsp->tv_nsec = (sys_cputimer->freq64_nsec * delta) >> 32;
1261 }
1262 
1263 void
1264 microuptime(struct timeval *tvp)
1265 {
1266 	struct globaldata *gd = mycpu;
1267 	sysclock_t delta;
1268 
1269 	do {
1270 		tvp->tv_sec = gd->gd_time_seconds;
1271 		delta = sys_cputimer->count() - gd->gd_cpuclock_base;
1272 	} while (tvp->tv_sec != gd->gd_time_seconds);
1273 
1274 	if (delta >= sys_cputimer->freq) {
1275 		tvp->tv_sec += delta / sys_cputimer->freq;
1276 		delta %= sys_cputimer->freq;
1277 	}
1278 	tvp->tv_usec = (sys_cputimer->freq64_usec * delta) >> 32;
1279 }
1280 
1281 void
1282 nanouptime(struct timespec *tsp)
1283 {
1284 	struct globaldata *gd = mycpu;
1285 	sysclock_t delta;
1286 
1287 	do {
1288 		tsp->tv_sec = gd->gd_time_seconds;
1289 		delta = sys_cputimer->count() - gd->gd_cpuclock_base;
1290 	} while (tsp->tv_sec != gd->gd_time_seconds);
1291 
1292 	if (delta >= sys_cputimer->freq) {
1293 		tsp->tv_sec += delta / sys_cputimer->freq;
1294 		delta %= sys_cputimer->freq;
1295 	}
1296 	tsp->tv_nsec = (sys_cputimer->freq64_nsec * delta) >> 32;
1297 }
1298 
1299 /*
1300  * realtime routines
1301  */
1302 void
1303 getmicrotime(struct timeval *tvp)
1304 {
1305 	struct globaldata *gd = mycpu;
1306 	struct timespec *bt;
1307 	sysclock_t delta;
1308 
1309 	do {
1310 		tvp->tv_sec = gd->gd_time_seconds;
1311 		delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
1312 	} while (tvp->tv_sec != gd->gd_time_seconds);
1313 
1314 	if (delta >= sys_cputimer->freq) {
1315 		tvp->tv_sec += delta / sys_cputimer->freq;
1316 		delta %= sys_cputimer->freq;
1317 	}
1318 	tvp->tv_usec = (sys_cputimer->freq64_usec * delta) >> 32;
1319 
1320 	bt = &basetime[basetime_index];
1321 	cpu_lfence();
1322 	tvp->tv_sec += bt->tv_sec;
1323 	tvp->tv_usec += bt->tv_nsec / 1000;
1324 	while (tvp->tv_usec >= 1000000) {
1325 		tvp->tv_usec -= 1000000;
1326 		++tvp->tv_sec;
1327 	}
1328 }
1329 
1330 void
1331 getnanotime(struct timespec *tsp)
1332 {
1333 	struct globaldata *gd = mycpu;
1334 	struct timespec *bt;
1335 	sysclock_t delta;
1336 
1337 	do {
1338 		tsp->tv_sec = gd->gd_time_seconds;
1339 		delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
1340 	} while (tsp->tv_sec != gd->gd_time_seconds);
1341 
1342 	if (delta >= sys_cputimer->freq) {
1343 		tsp->tv_sec += delta / sys_cputimer->freq;
1344 		delta %= sys_cputimer->freq;
1345 	}
1346 	tsp->tv_nsec = (sys_cputimer->freq64_nsec * delta) >> 32;
1347 
1348 	bt = &basetime[basetime_index];
1349 	cpu_lfence();
1350 	tsp->tv_sec += bt->tv_sec;
1351 	tsp->tv_nsec += bt->tv_nsec;
1352 	while (tsp->tv_nsec >= 1000000000) {
1353 		tsp->tv_nsec -= 1000000000;
1354 		++tsp->tv_sec;
1355 	}
1356 }
1357 
1358 static void
1359 getnanotime_nbt(struct timespec *nbt, struct timespec *tsp)
1360 {
1361 	struct globaldata *gd = mycpu;
1362 	sysclock_t delta;
1363 
1364 	do {
1365 		tsp->tv_sec = gd->gd_time_seconds;
1366 		delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
1367 	} while (tsp->tv_sec != gd->gd_time_seconds);
1368 
1369 	if (delta >= sys_cputimer->freq) {
1370 		tsp->tv_sec += delta / sys_cputimer->freq;
1371 		delta %= sys_cputimer->freq;
1372 	}
1373 	tsp->tv_nsec = (sys_cputimer->freq64_nsec * delta) >> 32;
1374 
1375 	tsp->tv_sec += nbt->tv_sec;
1376 	tsp->tv_nsec += nbt->tv_nsec;
1377 	while (tsp->tv_nsec >= 1000000000) {
1378 		tsp->tv_nsec -= 1000000000;
1379 		++tsp->tv_sec;
1380 	}
1381 }
1382 
1383 
1384 void
1385 microtime(struct timeval *tvp)
1386 {
1387 	struct globaldata *gd = mycpu;
1388 	struct timespec *bt;
1389 	sysclock_t delta;
1390 
1391 	do {
1392 		tvp->tv_sec = gd->gd_time_seconds;
1393 		delta = sys_cputimer->count() - gd->gd_cpuclock_base;
1394 	} while (tvp->tv_sec != gd->gd_time_seconds);
1395 
1396 	if (delta >= sys_cputimer->freq) {
1397 		tvp->tv_sec += delta / sys_cputimer->freq;
1398 		delta %= sys_cputimer->freq;
1399 	}
1400 	tvp->tv_usec = (sys_cputimer->freq64_usec * delta) >> 32;
1401 
1402 	bt = &basetime[basetime_index];
1403 	cpu_lfence();
1404 	tvp->tv_sec += bt->tv_sec;
1405 	tvp->tv_usec += bt->tv_nsec / 1000;
1406 	while (tvp->tv_usec >= 1000000) {
1407 		tvp->tv_usec -= 1000000;
1408 		++tvp->tv_sec;
1409 	}
1410 }
1411 
1412 void
1413 nanotime(struct timespec *tsp)
1414 {
1415 	struct globaldata *gd = mycpu;
1416 	struct timespec *bt;
1417 	sysclock_t delta;
1418 
1419 	do {
1420 		tsp->tv_sec = gd->gd_time_seconds;
1421 		delta = sys_cputimer->count() - gd->gd_cpuclock_base;
1422 	} while (tsp->tv_sec != gd->gd_time_seconds);
1423 
1424 	if (delta >= sys_cputimer->freq) {
1425 		tsp->tv_sec += delta / sys_cputimer->freq;
1426 		delta %= sys_cputimer->freq;
1427 	}
1428 	tsp->tv_nsec = (sys_cputimer->freq64_nsec * delta) >> 32;
1429 
1430 	bt = &basetime[basetime_index];
1431 	cpu_lfence();
1432 	tsp->tv_sec += bt->tv_sec;
1433 	tsp->tv_nsec += bt->tv_nsec;
1434 	while (tsp->tv_nsec >= 1000000000) {
1435 		tsp->tv_nsec -= 1000000000;
1436 		++tsp->tv_sec;
1437 	}
1438 }
1439 
1440 /*
1441  * Get an approximate time_t.  It does not have to be accurate.  This
1442  * function is called only from KTR and can be called with the system in
1443  * any state so do not use a critical section or other complex operation
1444  * here.
1445  *
1446  * NOTE: This is not exactly synchronized with real time.  To do that we
1447  *	 would have to do what microtime does and check for a nanoseconds
1448  *	 overflow.
1449  */
1450 time_t
1451 get_approximate_time_t(void)
1452 {
1453 	struct globaldata *gd = mycpu;
1454 	struct timespec *bt;
1455 
1456 	bt = &basetime[basetime_index];
1457 	return(gd->gd_time_seconds + bt->tv_sec);
1458 }
1459 
1460 int
1461 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
1462 {
1463 	pps_params_t *app;
1464 	struct pps_fetch_args *fapi;
1465 #ifdef PPS_SYNC
1466 	struct pps_kcbind_args *kapi;
1467 #endif
1468 
1469 	switch (cmd) {
1470 	case PPS_IOC_CREATE:
1471 		return (0);
1472 	case PPS_IOC_DESTROY:
1473 		return (0);
1474 	case PPS_IOC_SETPARAMS:
1475 		app = (pps_params_t *)data;
1476 		if (app->mode & ~pps->ppscap)
1477 			return (EINVAL);
1478 		pps->ppsparam = *app;
1479 		return (0);
1480 	case PPS_IOC_GETPARAMS:
1481 		app = (pps_params_t *)data;
1482 		*app = pps->ppsparam;
1483 		app->api_version = PPS_API_VERS_1;
1484 		return (0);
1485 	case PPS_IOC_GETCAP:
1486 		*(int*)data = pps->ppscap;
1487 		return (0);
1488 	case PPS_IOC_FETCH:
1489 		fapi = (struct pps_fetch_args *)data;
1490 		if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
1491 			return (EINVAL);
1492 		if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
1493 			return (EOPNOTSUPP);
1494 		pps->ppsinfo.current_mode = pps->ppsparam.mode;
1495 		fapi->pps_info_buf = pps->ppsinfo;
1496 		return (0);
1497 	case PPS_IOC_KCBIND:
1498 #ifdef PPS_SYNC
1499 		kapi = (struct pps_kcbind_args *)data;
1500 		/* XXX Only root should be able to do this */
1501 		if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
1502 			return (EINVAL);
1503 		if (kapi->kernel_consumer != PPS_KC_HARDPPS)
1504 			return (EINVAL);
1505 		if (kapi->edge & ~pps->ppscap)
1506 			return (EINVAL);
1507 		pps->kcmode = kapi->edge;
1508 		return (0);
1509 #else
1510 		return (EOPNOTSUPP);
1511 #endif
1512 	default:
1513 		return (ENOTTY);
1514 	}
1515 }
1516 
1517 void
1518 pps_init(struct pps_state *pps)
1519 {
1520 	pps->ppscap |= PPS_TSFMT_TSPEC;
1521 	if (pps->ppscap & PPS_CAPTUREASSERT)
1522 		pps->ppscap |= PPS_OFFSETASSERT;
1523 	if (pps->ppscap & PPS_CAPTURECLEAR)
1524 		pps->ppscap |= PPS_OFFSETCLEAR;
1525 }
1526 
1527 void
1528 pps_event(struct pps_state *pps, sysclock_t count, int event)
1529 {
1530 	struct globaldata *gd;
1531 	struct timespec *tsp;
1532 	struct timespec *osp;
1533 	struct timespec *bt;
1534 	struct timespec ts;
1535 	sysclock_t *pcount;
1536 #ifdef PPS_SYNC
1537 	sysclock_t tcount;
1538 #endif
1539 	sysclock_t delta;
1540 	pps_seq_t *pseq;
1541 	int foff;
1542 #ifdef PPS_SYNC
1543 	int fhard;
1544 #endif
1545 	int ni;
1546 
1547 	gd = mycpu;
1548 
1549 	/* Things would be easier with arrays... */
1550 	if (event == PPS_CAPTUREASSERT) {
1551 		tsp = &pps->ppsinfo.assert_timestamp;
1552 		osp = &pps->ppsparam.assert_offset;
1553 		foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1554 #ifdef PPS_SYNC
1555 		fhard = pps->kcmode & PPS_CAPTUREASSERT;
1556 #endif
1557 		pcount = &pps->ppscount[0];
1558 		pseq = &pps->ppsinfo.assert_sequence;
1559 	} else {
1560 		tsp = &pps->ppsinfo.clear_timestamp;
1561 		osp = &pps->ppsparam.clear_offset;
1562 		foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1563 #ifdef PPS_SYNC
1564 		fhard = pps->kcmode & PPS_CAPTURECLEAR;
1565 #endif
1566 		pcount = &pps->ppscount[1];
1567 		pseq = &pps->ppsinfo.clear_sequence;
1568 	}
1569 
1570 	/* Nothing really happened */
1571 	if (*pcount == count)
1572 		return;
1573 
1574 	*pcount = count;
1575 
1576 	do {
1577 		ts.tv_sec = gd->gd_time_seconds;
1578 		delta = count - gd->gd_cpuclock_base;
1579 	} while (ts.tv_sec != gd->gd_time_seconds);
1580 
1581 	if (delta >= sys_cputimer->freq) {
1582 		ts.tv_sec += delta / sys_cputimer->freq;
1583 		delta %= sys_cputimer->freq;
1584 	}
1585 	ts.tv_nsec = (sys_cputimer->freq64_nsec * delta) >> 32;
1586 	ni = basetime_index;
1587 	cpu_lfence();
1588 	bt = &basetime[ni];
1589 	ts.tv_sec += bt->tv_sec;
1590 	ts.tv_nsec += bt->tv_nsec;
1591 	while (ts.tv_nsec >= 1000000000) {
1592 		ts.tv_nsec -= 1000000000;
1593 		++ts.tv_sec;
1594 	}
1595 
1596 	(*pseq)++;
1597 	*tsp = ts;
1598 
1599 	if (foff) {
1600 		timespecadd(tsp, osp);
1601 		if (tsp->tv_nsec < 0) {
1602 			tsp->tv_nsec += 1000000000;
1603 			tsp->tv_sec -= 1;
1604 		}
1605 	}
1606 #ifdef PPS_SYNC
1607 	if (fhard) {
1608 		/* magic, at its best... */
1609 		tcount = count - pps->ppscount[2];
1610 		pps->ppscount[2] = count;
1611 		if (tcount >= sys_cputimer->freq) {
1612 			delta = (1000000000 * (tcount / sys_cputimer->freq) +
1613 				 sys_cputimer->freq64_nsec *
1614 				 (tcount % sys_cputimer->freq)) >> 32;
1615 		} else {
1616 			delta = (sys_cputimer->freq64_nsec * tcount) >> 32;
1617 		}
1618 		hardpps(tsp, delta);
1619 	}
1620 #endif
1621 }
1622 
1623 /*
1624  * Return the tsc target value for a delay of (ns).
1625  *
1626  * Returns -1 if the TSC is not supported.
1627  */
1628 int64_t
1629 tsc_get_target(int ns)
1630 {
1631 #if defined(_RDTSC_SUPPORTED_)
1632 	if (cpu_feature & CPUID_TSC) {
1633 		return (rdtsc() + tsc_frequency * ns / (int64_t)1000000000);
1634 	}
1635 #endif
1636 	return(-1);
1637 }
1638 
1639 /*
1640  * Compare the tsc against the passed target
1641  *
1642  * Returns +1 if the target has been reached
1643  * Returns  0 if the target has not yet been reached
1644  * Returns -1 if the TSC is not supported.
1645  *
1646  * Typical use:		while (tsc_test_target(target) == 0) { ...poll... }
1647  */
1648 int
1649 tsc_test_target(int64_t target)
1650 {
1651 #if defined(_RDTSC_SUPPORTED_)
1652 	if (cpu_feature & CPUID_TSC) {
1653 		if ((int64_t)(target - rdtsc()) <= 0)
1654 			return(1);
1655 		return(0);
1656 	}
1657 #endif
1658 	return(-1);
1659 }
1660 
1661 /*
1662  * Delay the specified number of nanoseconds using the tsc.  This function
1663  * returns immediately if the TSC is not supported.  At least one cpu_pause()
1664  * will be issued.
1665  */
1666 void
1667 tsc_delay(int ns)
1668 {
1669 	int64_t clk;
1670 
1671 	clk = tsc_get_target(ns);
1672 	cpu_pause();
1673 	while (tsc_test_target(clk) == 0)
1674 		cpu_pause();
1675 }
1676