xref: /netbsd-src/sys/kern/kern_clock.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: kern_clock.c,v 1.114 2007/11/06 00:42:40 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2004, 2006, 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Charles M. Hannum.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the NetBSD
24  *	Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 /*-
43  * Copyright (c) 1982, 1986, 1991, 1993
44  *	The Regents of the University of California.  All rights reserved.
45  * (c) UNIX System Laboratories, Inc.
46  * All or some portions of this file are derived from material licensed
47  * to the University of California by American Telephone and Telegraph
48  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
49  * the permission of UNIX System Laboratories, Inc.
50  *
51  * Redistribution and use in source and binary forms, with or without
52  * modification, are permitted provided that the following conditions
53  * are met:
54  * 1. Redistributions of source code must retain the above copyright
55  *    notice, this list of conditions and the following disclaimer.
56  * 2. Redistributions in binary form must reproduce the above copyright
57  *    notice, this list of conditions and the following disclaimer in the
58  *    documentation and/or other materials provided with the distribution.
59  * 3. Neither the name of the University nor the names of its contributors
60  *    may be used to endorse or promote products derived from this software
61  *    without specific prior written permission.
62  *
63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73  * SUCH DAMAGE.
74  *
75  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
76  */
77 
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: kern_clock.c,v 1.114 2007/11/06 00:42:40 ad Exp $");
80 
81 #include "opt_ntp.h"
82 #include "opt_multiprocessor.h"
83 #include "opt_perfctrs.h"
84 
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/callout.h>
88 #include <sys/kernel.h>
89 #include <sys/proc.h>
90 #include <sys/resourcevar.h>
91 #include <sys/signalvar.h>
92 #include <sys/sysctl.h>
93 #include <sys/timex.h>
94 #include <sys/sched.h>
95 #include <sys/time.h>
96 #include <sys/timetc.h>
97 #include <sys/cpu.h>
98 
99 #ifdef GPROF
100 #include <sys/gmon.h>
101 #endif
102 
103 /*
104  * Clock handling routines.
105  *
106  * This code is written to operate with two timers that run independently of
107  * each other.  The main clock, running hz times per second, is used to keep
108  * track of real time.  The second timer handles kernel and user profiling,
109  * and does resource use estimation.  If the second timer is programmable,
110  * it is randomized to avoid aliasing between the two clocks.  For example,
111  * the randomization prevents an adversary from always giving up the CPU
112  * just before its quantum expires.  Otherwise, it would never accumulate
113  * CPU ticks.  The mean frequency of the second timer is stathz.
114  *
115  * If no second timer exists, stathz will be zero; in this case we drive
116  * profiling and statistics off the main clock.  This WILL NOT be accurate;
117  * do not do it unless absolutely necessary.
118  *
119  * The statistics clock may (or may not) be run at a higher rate while
120  * profiling.  This profile clock runs at profhz.  We require that profhz
121  * be an integral multiple of stathz.
122  *
123  * If the statistics clock is running fast, it must be divided by the ratio
124  * profhz/stathz for statistics.  (For profiling, every tick counts.)
125  */
126 
127 #ifndef __HAVE_TIMECOUNTER
128 #ifdef NTP	/* NTP phase-locked loop in kernel */
129 /*
130  * Phase/frequency-lock loop (PLL/FLL) definitions
131  *
132  * The following variables are read and set by the ntp_adjtime() system
133  * call.
134  *
135  * time_state shows the state of the system clock, with values defined
136  * in the timex.h header file.
137  *
138  * time_status shows the status of the system clock, with bits defined
139  * in the timex.h header file.
140  *
141  * time_offset is used by the PLL/FLL to adjust the system time in small
142  * increments.
143  *
144  * time_constant determines the bandwidth or "stiffness" of the PLL.
145  *
146  * time_tolerance determines maximum frequency error or tolerance of the
147  * CPU clock oscillator and is a property of the architecture; however,
148  * in principle it could change as result of the presence of external
149  * discipline signals, for instance.
150  *
151  * time_precision is usually equal to the kernel tick variable; however,
152  * in cases where a precision clock counter or external clock is
153  * available, the resolution can be much less than this and depend on
154  * whether the external clock is working or not.
155  *
156  * time_maxerror is initialized by a ntp_adjtime() call and increased by
157  * the kernel once each second to reflect the maximum error bound
158  * growth.
159  *
160  * time_esterror is set and read by the ntp_adjtime() call, but
161  * otherwise not used by the kernel.
162  */
163 int time_state = TIME_OK;	/* clock state */
164 int time_status = STA_UNSYNC;	/* clock status bits */
165 long time_offset = 0;		/* time offset (us) */
166 long time_constant = 0;		/* pll time constant */
167 long time_tolerance = MAXFREQ;	/* frequency tolerance (scaled ppm) */
168 long time_precision = 1;	/* clock precision (us) */
169 long time_maxerror = MAXPHASE;	/* maximum error (us) */
170 long time_esterror = MAXPHASE;	/* estimated error (us) */
171 
172 /*
173  * The following variables establish the state of the PLL/FLL and the
174  * residual time and frequency offset of the local clock. The scale
175  * factors are defined in the timex.h header file.
176  *
177  * time_phase and time_freq are the phase increment and the frequency
178  * increment, respectively, of the kernel time variable.
179  *
180  * time_freq is set via ntp_adjtime() from a value stored in a file when
181  * the synchronization daemon is first started. Its value is retrieved
182  * via ntp_adjtime() and written to the file about once per hour by the
183  * daemon.
184  *
185  * time_adj is the adjustment added to the value of tick at each timer
186  * interrupt and is recomputed from time_phase and time_freq at each
187  * seconds rollover.
188  *
189  * time_reftime is the second's portion of the system time at the last
190  * call to ntp_adjtime(). It is used to adjust the time_freq variable
191  * and to increase the time_maxerror as the time since last update
192  * increases.
193  */
194 long time_phase = 0;		/* phase offset (scaled us) */
195 long time_freq = 0;		/* frequency offset (scaled ppm) */
196 long time_adj = 0;		/* tick adjust (scaled 1 / hz) */
197 long time_reftime = 0;		/* time at last adjustment (s) */
198 
199 #ifdef PPS_SYNC
200 /*
201  * The following variables are used only if the kernel PPS discipline
202  * code is configured (PPS_SYNC). The scale factors are defined in the
203  * timex.h header file.
204  *
205  * pps_time contains the time at each calibration interval, as read by
206  * microtime(). pps_count counts the seconds of the calibration
207  * interval, the duration of which is nominally pps_shift in powers of
208  * two.
209  *
210  * pps_offset is the time offset produced by the time median filter
211  * pps_tf[], while pps_jitter is the dispersion (jitter) measured by
212  * this filter.
213  *
214  * pps_freq is the frequency offset produced by the frequency median
215  * filter pps_ff[], while pps_stabil is the dispersion (wander) measured
216  * by this filter.
217  *
218  * pps_usec is latched from a high resolution counter or external clock
219  * at pps_time. Here we want the hardware counter contents only, not the
220  * contents plus the time_tv.usec as usual.
221  *
222  * pps_valid counts the number of seconds since the last PPS update. It
223  * is used as a watchdog timer to disable the PPS discipline should the
224  * PPS signal be lost.
225  *
226  * pps_glitch counts the number of seconds since the beginning of an
227  * offset burst more than tick/2 from current nominal offset. It is used
228  * mainly to suppress error bursts due to priority conflicts between the
229  * PPS interrupt and timer interrupt.
230  *
231  * pps_intcnt counts the calibration intervals for use in the interval-
232  * adaptation algorithm. It's just too complicated for words.
233  *
234  * pps_kc_hardpps_source contains an arbitrary value that uniquely
235  * identifies the currently bound source of the PPS signal, or NULL
236  * if no source is bound.
237  *
238  * pps_kc_hardpps_mode indicates which transitions, if any, of the PPS
239  * signal should be reported.
240  */
241 struct timeval pps_time;	/* kernel time at last interval */
242 long pps_tf[] = {0, 0, 0};	/* pps time offset median filter (us) */
243 long pps_offset = 0;		/* pps time offset (us) */
244 long pps_jitter = MAXTIME;	/* time dispersion (jitter) (us) */
245 long pps_ff[] = {0, 0, 0};	/* pps frequency offset median filter */
246 long pps_freq = 0;		/* frequency offset (scaled ppm) */
247 long pps_stabil = MAXFREQ;	/* frequency dispersion (scaled ppm) */
248 long pps_usec = 0;		/* microsec counter at last interval */
249 long pps_valid = PPS_VALID;	/* pps signal watchdog counter */
250 int pps_glitch = 0;		/* pps signal glitch counter */
251 int pps_count = 0;		/* calibration interval counter (s) */
252 int pps_shift = PPS_SHIFT;	/* interval duration (s) (shift) */
253 int pps_intcnt = 0;		/* intervals at current duration */
254 void *pps_kc_hardpps_source = NULL; /* current PPS supplier's identifier */
255 int pps_kc_hardpps_mode = 0;	/* interesting edges of PPS signal */
256 
257 /*
258  * PPS signal quality monitors
259  *
260  * pps_jitcnt counts the seconds that have been discarded because the
261  * jitter measured by the time median filter exceeds the limit MAXTIME
262  * (100 us).
263  *
264  * pps_calcnt counts the frequency calibration intervals, which are
265  * variable from 4 s to 256 s.
266  *
267  * pps_errcnt counts the calibration intervals which have been discarded
268  * because the wander exceeds the limit MAXFREQ (100 ppm) or where the
269  * calibration interval jitter exceeds two ticks.
270  *
271  * pps_stbcnt counts the calibration intervals that have been discarded
272  * because the frequency wander exceeds the limit MAXFREQ / 4 (25 us).
273  */
274 long pps_jitcnt = 0;		/* jitter limit exceeded */
275 long pps_calcnt = 0;		/* calibration intervals */
276 long pps_errcnt = 0;		/* calibration errors */
277 long pps_stbcnt = 0;		/* stability limit exceeded */
278 #endif /* PPS_SYNC */
279 
280 #ifdef EXT_CLOCK
281 /*
282  * External clock definitions
283  *
284  * The following definitions and declarations are used only if an
285  * external clock is configured on the system.
286  */
287 #define CLOCK_INTERVAL 30	/* CPU clock update interval (s) */
288 
289 /*
290  * The clock_count variable is set to CLOCK_INTERVAL at each PPS
291  * interrupt and decremented once each second.
292  */
293 int clock_count = 0;		/* CPU clock counter */
294 
295 #endif /* EXT_CLOCK */
296 #endif /* NTP */
297 
298 /*
299  * Bump a timeval by a small number of usec's.
300  */
301 #define BUMPTIME(t, usec) { \
302 	volatile struct timeval *tp = (t); \
303 	long us; \
304  \
305 	tp->tv_usec = us = tp->tv_usec + (usec); \
306 	if (us >= 1000000) { \
307 		tp->tv_usec = us - 1000000; \
308 		tp->tv_sec++; \
309 	} \
310 }
311 #endif /* !__HAVE_TIMECOUNTER */
312 
313 int	stathz;
314 int	profhz;
315 int	profsrc;
316 int	schedhz;
317 int	profprocs;
318 int	hardclock_ticks;
319 static int hardscheddiv; /* hard => sched divider (used if schedhz == 0) */
320 static int psdiv;			/* prof => stat divider */
321 int	psratio;			/* ratio: prof / stat */
322 #ifndef __HAVE_TIMECOUNTER
323 int	tickfix, tickfixinterval;	/* used if tick not really integral */
324 #ifndef NTP
325 static int tickfixcnt;			/* accumulated fractional error */
326 #else
327 int	fixtick;			/* used by NTP for same */
328 int	shifthz;
329 #endif
330 
331 /*
332  * We might want ldd to load the both words from time at once.
333  * To succeed we need to be quadword aligned.
334  * The sparc already does that, and that it has worked so far is a fluke.
335  */
336 volatile struct	timeval time  __attribute__((__aligned__(__alignof__(quad_t))));
337 volatile struct	timeval mono_time;
338 #endif /* !__HAVE_TIMECOUNTER */
339 
340 #ifdef __HAVE_TIMECOUNTER
341 static u_int get_intr_timecount(struct timecounter *);
342 
343 static struct timecounter intr_timecounter = {
344 	get_intr_timecount,	/* get_timecount */
345 	0,			/* no poll_pps */
346 	~0u,			/* counter_mask */
347 	0,		        /* frequency */
348 	"clockinterrupt",	/* name */
349 	0,			/* quality - minimum implementation level for a clock */
350 	NULL,			/* prev */
351 	NULL,			/* next */
352 };
353 
354 static u_int
355 get_intr_timecount(struct timecounter *tc)
356 {
357 
358 	return (u_int)hardclock_ticks;
359 }
360 #endif
361 
362 /*
363  * Initialize clock frequencies and start both clocks running.
364  */
365 void
366 initclocks(void)
367 {
368 	int i;
369 
370 	/*
371 	 * Set divisors to 1 (normal case) and let the machine-specific
372 	 * code do its bit.
373 	 */
374 	psdiv = 1;
375 #ifdef __HAVE_TIMECOUNTER
376 	/*
377 	 * provide minimum default time counter
378 	 * will only run at interrupt resolution
379 	 */
380 	intr_timecounter.tc_frequency = hz;
381 	tc_init(&intr_timecounter);
382 #endif
383 	cpu_initclocks();
384 
385 	/*
386 	 * Compute profhz and stathz, fix profhz if needed.
387 	 */
388 	i = stathz ? stathz : hz;
389 	if (profhz == 0)
390 		profhz = i;
391 	psratio = profhz / i;
392 	if (schedhz == 0) {
393 		/* 16Hz is best */
394 		hardscheddiv = hz / 16;
395 		if (hardscheddiv <= 0)
396 			panic("hardscheddiv");
397 	}
398 
399 #ifndef __HAVE_TIMECOUNTER
400 #ifdef NTP
401 	switch (hz) {
402 	case 1:
403 		shifthz = SHIFT_SCALE - 0;
404 		break;
405 	case 2:
406 		shifthz = SHIFT_SCALE - 1;
407 		break;
408 	case 4:
409 		shifthz = SHIFT_SCALE - 2;
410 		break;
411 	case 8:
412 		shifthz = SHIFT_SCALE - 3;
413 		break;
414 	case 16:
415 		shifthz = SHIFT_SCALE - 4;
416 		break;
417 	case 32:
418 		shifthz = SHIFT_SCALE - 5;
419 		break;
420 	case 50:
421 	case 60:
422 	case 64:
423 		shifthz = SHIFT_SCALE - 6;
424 		break;
425 	case 96:
426 	case 100:
427 	case 128:
428 		shifthz = SHIFT_SCALE - 7;
429 		break;
430 	case 256:
431 		shifthz = SHIFT_SCALE - 8;
432 		break;
433 	case 512:
434 		shifthz = SHIFT_SCALE - 9;
435 		break;
436 	case 1000:
437 	case 1024:
438 		shifthz = SHIFT_SCALE - 10;
439 		break;
440 	case 1200:
441 	case 2048:
442 		shifthz = SHIFT_SCALE - 11;
443 		break;
444 	case 4096:
445 		shifthz = SHIFT_SCALE - 12;
446 		break;
447 	case 8192:
448 		shifthz = SHIFT_SCALE - 13;
449 		break;
450 	case 16384:
451 		shifthz = SHIFT_SCALE - 14;
452 		break;
453 	case 32768:
454 		shifthz = SHIFT_SCALE - 15;
455 		break;
456 	case 65536:
457 		shifthz = SHIFT_SCALE - 16;
458 		break;
459 	default:
460 		panic("weird hz");
461 	}
462 	if (fixtick == 0) {
463 		/*
464 		 * Give MD code a chance to set this to a better
465 		 * value; but, if it doesn't, we should.
466 		 */
467 		fixtick = (1000000 - (hz*tick));
468 	}
469 #endif /* NTP */
470 #endif /* !__HAVE_TIMECOUNTER */
471 }
472 
473 /*
474  * The real-time timer, interrupting hz times per second.
475  */
476 void
477 hardclock(struct clockframe *frame)
478 {
479 	struct lwp *l;
480 	struct proc *p;
481 	struct cpu_info *ci = curcpu();
482 	struct ptimer *pt;
483 #ifndef __HAVE_TIMECOUNTER
484 	int delta;
485 	extern int tickdelta;
486 	extern long timedelta;
487 #ifdef NTP
488 	int time_update;
489 	int ltemp;
490 #endif /* NTP */
491 #endif /* __HAVE_TIMECOUNTER */
492 
493 	l = ci->ci_data.cpu_onproc;
494 	if (!CURCPU_IDLE_P()) {
495 		p = l->l_proc;
496 		/*
497 		 * Run current process's virtual and profile time, as needed.
498 		 */
499 		if (CLKF_USERMODE(frame) && p->p_timers &&
500 		    (pt = LIST_FIRST(&p->p_timers->pts_virtual)) != NULL)
501 			if (itimerdecr(pt, tick) == 0)
502 				itimerfire(pt);
503 		if (p->p_timers &&
504 		    (pt = LIST_FIRST(&p->p_timers->pts_prof)) != NULL)
505 			if (itimerdecr(pt, tick) == 0)
506 				itimerfire(pt);
507 	}
508 
509 	/*
510 	 * If no separate statistics clock is available, run it from here.
511 	 */
512 	if (stathz == 0)
513 		statclock(frame);
514 	/*
515 	 * If no separate schedclock is provided, call it here
516 	 * at about 16 Hz.
517 	 */
518 	if (schedhz == 0) {
519 		if ((int)(--ci->ci_schedstate.spc_schedticks) <= 0) {
520 			schedclock(l);
521 			ci->ci_schedstate.spc_schedticks = hardscheddiv;
522 		}
523 	}
524 	if ((--ci->ci_schedstate.spc_ticks) <= 0)
525 		sched_tick(ci);
526 
527 #if defined(MULTIPROCESSOR)
528 	/*
529 	 * If we are not the primary CPU, we're not allowed to do
530 	 * any more work.
531 	 */
532 	if (CPU_IS_PRIMARY(ci) == 0)
533 		return;
534 #endif
535 
536 	hardclock_ticks++;
537 
538 #ifdef __HAVE_TIMECOUNTER
539 	tc_ticktock();
540 #else /* __HAVE_TIMECOUNTER */
541 	/*
542 	 * Increment the time-of-day.  The increment is normally just
543 	 * ``tick''.  If the machine is one which has a clock frequency
544 	 * such that ``hz'' would not divide the second evenly into
545 	 * milliseconds, a periodic adjustment must be applied.  Finally,
546 	 * if we are still adjusting the time (see adjtime()),
547 	 * ``tickdelta'' may also be added in.
548 	 */
549 	delta = tick;
550 
551 #ifndef NTP
552 	if (tickfix) {
553 		tickfixcnt += tickfix;
554 		if (tickfixcnt >= tickfixinterval) {
555 			delta++;
556 			tickfixcnt -= tickfixinterval;
557 		}
558 	}
559 #endif /* !NTP */
560 	/* Imprecise 4bsd adjtime() handling */
561 	if (timedelta != 0) {
562 		delta += tickdelta;
563 		timedelta -= tickdelta;
564 	}
565 
566 #ifdef notyet
567 	microset();
568 #endif
569 
570 #ifndef NTP
571 	BUMPTIME(&time, delta);		/* XXX Now done using NTP code below */
572 #endif
573 	BUMPTIME(&mono_time, delta);
574 
575 #ifdef NTP
576 	time_update = delta;
577 
578 	/*
579 	 * Compute the phase adjustment. If the low-order bits
580 	 * (time_phase) of the update overflow, bump the high-order bits
581 	 * (time_update).
582 	 */
583 	time_phase += time_adj;
584 	if (time_phase <= -FINEUSEC) {
585 		ltemp = -time_phase >> SHIFT_SCALE;
586 		time_phase += ltemp << SHIFT_SCALE;
587 		time_update -= ltemp;
588 	} else if (time_phase >= FINEUSEC) {
589 		ltemp = time_phase >> SHIFT_SCALE;
590 		time_phase -= ltemp << SHIFT_SCALE;
591 		time_update += ltemp;
592 	}
593 	time.tv_usec += time_update;
594 
595 	/*
596 	 * On rollover of the second the phase adjustment to be used for
597 	 * the next second is calculated. Also, the maximum error is
598 	 * increased by the tolerance. If the PPS frequency discipline
599 	 * code is present, the phase is increased to compensate for the
600 	 * CPU clock oscillator frequency error.
601 	 *
602  	 * On a 32-bit machine and given parameters in the timex.h
603 	 * header file, the maximum phase adjustment is +-512 ms and
604 	 * maximum frequency offset is a tad less than) +-512 ppm. On a
605 	 * 64-bit machine, you shouldn't need to ask.
606 	 */
607 	if (time.tv_usec >= 1000000) {
608 		time.tv_usec -= 1000000;
609 		time.tv_sec++;
610 		time_maxerror += time_tolerance >> SHIFT_USEC;
611 
612 		/*
613 		 * Leap second processing. If in leap-insert state at
614 		 * the end of the day, the system clock is set back one
615 		 * second; if in leap-delete state, the system clock is
616 		 * set ahead one second. The microtime() routine or
617 		 * external clock driver will insure that reported time
618 		 * is always monotonic. The ugly divides should be
619 		 * replaced.
620 		 */
621 		switch (time_state) {
622 		case TIME_OK:
623 			if (time_status & STA_INS)
624 				time_state = TIME_INS;
625 			else if (time_status & STA_DEL)
626 				time_state = TIME_DEL;
627 			break;
628 
629 		case TIME_INS:
630 			if (time.tv_sec % 86400 == 0) {
631 				time.tv_sec--;
632 				time_state = TIME_OOP;
633 			}
634 			break;
635 
636 		case TIME_DEL:
637 			if ((time.tv_sec + 1) % 86400 == 0) {
638 				time.tv_sec++;
639 				time_state = TIME_WAIT;
640 			}
641 			break;
642 
643 		case TIME_OOP:
644 			time_state = TIME_WAIT;
645 			break;
646 
647 		case TIME_WAIT:
648 			if (!(time_status & (STA_INS | STA_DEL)))
649 				time_state = TIME_OK;
650 			break;
651 		}
652 
653 		/*
654 		 * Compute the phase adjustment for the next second. In
655 		 * PLL mode, the offset is reduced by a fixed factor
656 		 * times the time constant. In FLL mode the offset is
657 		 * used directly. In either mode, the maximum phase
658 		 * adjustment for each second is clamped so as to spread
659 		 * the adjustment over not more than the number of
660 		 * seconds between updates.
661 		 */
662 		if (time_offset < 0) {
663 			ltemp = -time_offset;
664 			if (!(time_status & STA_FLL))
665 				ltemp >>= SHIFT_KG + time_constant;
666 			if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
667 				ltemp = (MAXPHASE / MINSEC) <<
668 				    SHIFT_UPDATE;
669 			time_offset += ltemp;
670 			time_adj = -ltemp << (shifthz - SHIFT_UPDATE);
671 		} else if (time_offset > 0) {
672 			ltemp = time_offset;
673 			if (!(time_status & STA_FLL))
674 				ltemp >>= SHIFT_KG + time_constant;
675 			if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
676 				ltemp = (MAXPHASE / MINSEC) <<
677 				    SHIFT_UPDATE;
678 			time_offset -= ltemp;
679 			time_adj = ltemp << (shifthz - SHIFT_UPDATE);
680 		} else
681 			time_adj = 0;
682 
683 		/*
684 		 * Compute the frequency estimate and additional phase
685 		 * adjustment due to frequency error for the next
686 		 * second. When the PPS signal is engaged, gnaw on the
687 		 * watchdog counter and update the frequency computed by
688 		 * the pll and the PPS signal.
689 		 */
690 #ifdef PPS_SYNC
691 		pps_valid++;
692 		if (pps_valid == PPS_VALID) {
693 			pps_jitter = MAXTIME;
694 			pps_stabil = MAXFREQ;
695 			time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
696 			    STA_PPSWANDER | STA_PPSERROR);
697 		}
698 		ltemp = time_freq + pps_freq;
699 #else
700 		ltemp = time_freq;
701 #endif /* PPS_SYNC */
702 
703 		if (ltemp < 0)
704 			time_adj -= -ltemp >> (SHIFT_USEC - shifthz);
705 		else
706 			time_adj += ltemp >> (SHIFT_USEC - shifthz);
707 		time_adj += (long)fixtick << shifthz;
708 
709 		/*
710 		 * When the CPU clock oscillator frequency is not a
711 		 * power of 2 in Hz, shifthz is only an approximate
712 		 * scale factor.
713 		 *
714 		 * To determine the adjustment, you can do the following:
715 		 *   bc -q
716 		 *   scale=24
717 		 *   obase=2
718 		 *   idealhz/realhz
719 		 * where `idealhz' is the next higher power of 2, and `realhz'
720 		 * is the actual value.  You may need to factor this result
721 		 * into a sequence of 2 multipliers to get better precision.
722 		 *
723 		 * Likewise, the error can be calculated with (e.g. for 100Hz):
724 		 *   bc -q
725 		 *   scale=24
726 		 *   ((1+2^-2+2^-5)*(1-2^-10)*realhz-idealhz)/idealhz
727 		 * (and then multiply by 1000000 to get ppm).
728 		 */
729 		switch (hz) {
730 		case 60:
731 			/* A factor of 1.000100010001 gives about 15ppm
732 			   error. */
733 			if (time_adj < 0) {
734 				time_adj -= (-time_adj >> 4);
735 				time_adj -= (-time_adj >> 8);
736 			} else {
737 				time_adj += (time_adj >> 4);
738 				time_adj += (time_adj >> 8);
739 			}
740 			break;
741 
742 		case 96:
743 			/* A factor of 1.0101010101 gives about 244ppm error. */
744 			if (time_adj < 0) {
745 				time_adj -= (-time_adj >> 2);
746 				time_adj -= (-time_adj >> 4) + (-time_adj >> 8);
747 			} else {
748 				time_adj += (time_adj >> 2);
749 				time_adj += (time_adj >> 4) + (time_adj >> 8);
750 			}
751 			break;
752 
753 		case 50:
754 		case 100:
755 			/* A factor of 1.010001111010111 gives about 1ppm
756 			   error. */
757 			if (time_adj < 0) {
758 				time_adj -= (-time_adj >> 2) + (-time_adj >> 5);
759 				time_adj += (-time_adj >> 10);
760 			} else {
761 				time_adj += (time_adj >> 2) + (time_adj >> 5);
762 				time_adj -= (time_adj >> 10);
763 			}
764 			break;
765 
766 		case 1000:
767 			/* A factor of 1.000001100010100001 gives about 50ppm
768 			   error. */
769 			if (time_adj < 0) {
770 				time_adj -= (-time_adj >> 6) + (-time_adj >> 11);
771 				time_adj -= (-time_adj >> 7);
772 			} else {
773 				time_adj += (time_adj >> 6) + (time_adj >> 11);
774 				time_adj += (time_adj >> 7);
775 			}
776 			break;
777 
778 		case 1200:
779 			/* A factor of 1.1011010011100001 gives about 64ppm
780 			   error. */
781 			if (time_adj < 0) {
782 				time_adj -= (-time_adj >> 1) + (-time_adj >> 6);
783 				time_adj -= (-time_adj >> 3) + (-time_adj >> 10);
784 			} else {
785 				time_adj += (time_adj >> 1) + (time_adj >> 6);
786 				time_adj += (time_adj >> 3) + (time_adj >> 10);
787 			}
788 			break;
789 		}
790 
791 #ifdef EXT_CLOCK
792 		/*
793 		 * If an external clock is present, it is necessary to
794 		 * discipline the kernel time variable anyway, since not
795 		 * all system components use the microtime() interface.
796 		 * Here, the time offset between the external clock and
797 		 * kernel time variable is computed every so often.
798 		 */
799 		clock_count++;
800 		if (clock_count > CLOCK_INTERVAL) {
801 			clock_count = 0;
802 			microtime(&clock_ext);
803 			delta.tv_sec = clock_ext.tv_sec - time.tv_sec;
804 			delta.tv_usec = clock_ext.tv_usec -
805 			    time.tv_usec;
806 			if (delta.tv_usec < 0)
807 				delta.tv_sec--;
808 			if (delta.tv_usec >= 500000) {
809 				delta.tv_usec -= 1000000;
810 				delta.tv_sec++;
811 			}
812 			if (delta.tv_usec < -500000) {
813 				delta.tv_usec += 1000000;
814 				delta.tv_sec--;
815 			}
816 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
817 			    delta.tv_usec > MAXPHASE) ||
818 			    delta.tv_sec < -1 || (delta.tv_sec == -1 &&
819 			    delta.tv_usec < -MAXPHASE)) {
820 				time = clock_ext;
821 				delta.tv_sec = 0;
822 				delta.tv_usec = 0;
823 			}
824 			hardupdate(delta.tv_usec);
825 		}
826 #endif /* EXT_CLOCK */
827 	}
828 
829 #endif /* NTP */
830 #endif /* !__HAVE_TIMECOUNTER */
831 
832 	/*
833 	 * Update real-time timeout queue.  Callouts are processed at a
834 	 * very low CPU priority, so we don't keep the relatively high
835 	 * clock interrupt priority any longer than necessary.
836 	 */
837 	callout_hardclock();
838 }
839 
840 /*
841  * Start profiling on a process.
842  *
843  * Kernel profiling passes proc0 which never exits and hence
844  * keeps the profile clock running constantly.
845  */
846 void
847 startprofclock(struct proc *p)
848 {
849 
850 	KASSERT(mutex_owned(&p->p_stmutex));
851 
852 	if ((p->p_stflag & PST_PROFIL) == 0) {
853 		p->p_stflag |= PST_PROFIL;
854 		/*
855 		 * This is only necessary if using the clock as the
856 		 * profiling source.
857 		 */
858 		if (++profprocs == 1 && stathz != 0)
859 			psdiv = psratio;
860 	}
861 }
862 
863 /*
864  * Stop profiling on a process.
865  */
866 void
867 stopprofclock(struct proc *p)
868 {
869 
870 	KASSERT(mutex_owned(&p->p_stmutex));
871 
872 	if (p->p_stflag & PST_PROFIL) {
873 		p->p_stflag &= ~PST_PROFIL;
874 		/*
875 		 * This is only necessary if using the clock as the
876 		 * profiling source.
877 		 */
878 		if (--profprocs == 0 && stathz != 0)
879 			psdiv = 1;
880 	}
881 }
882 
883 #if defined(PERFCTRS)
884 /*
885  * Independent profiling "tick" in case we're using a separate
886  * clock or profiling event source.  Currently, that's just
887  * performance counters--hence the wrapper.
888  */
889 void
890 proftick(struct clockframe *frame)
891 {
892 #ifdef GPROF
893         struct gmonparam *g;
894         intptr_t i;
895 #endif
896 	struct lwp *l;
897 	struct proc *p;
898 
899 	l = curcpu()->ci_data.cpu_onproc;
900 	p = (l ? l->l_proc : NULL);
901 	if (CLKF_USERMODE(frame)) {
902 		mutex_spin_enter(&p->p_stmutex);
903 		if (p->p_stflag & PST_PROFIL)
904 			addupc_intr(l, CLKF_PC(frame));
905 		mutex_spin_exit(&p->p_stmutex);
906 	} else {
907 #ifdef GPROF
908 		g = &_gmonparam;
909 		if (g->state == GMON_PROF_ON) {
910 			i = CLKF_PC(frame) - g->lowpc;
911 			if (i < g->textsize) {
912 				i /= HISTFRACTION * sizeof(*g->kcount);
913 				g->kcount[i]++;
914 			}
915 		}
916 #endif
917 #ifdef LWP_PC
918 		if (p != NULL && (p->p_stflag & PST_PROFIL) != 0)
919 			addupc_intr(l, LWP_PC(l));
920 #endif
921 	}
922 }
923 #endif
924 
925 void
926 schedclock(struct lwp *l)
927 {
928 
929 	if ((l->l_flag & LW_IDLE) != 0)
930 		return;
931 
932 	sched_schedclock(l);
933 }
934 
935 /*
936  * Statistics clock.  Grab profile sample, and if divider reaches 0,
937  * do process and kernel statistics.
938  */
939 void
940 statclock(struct clockframe *frame)
941 {
942 #ifdef GPROF
943 	struct gmonparam *g;
944 	intptr_t i;
945 #endif
946 	struct cpu_info *ci = curcpu();
947 	struct schedstate_percpu *spc = &ci->ci_schedstate;
948 	struct proc *p;
949 	struct lwp *l;
950 
951 	/*
952 	 * Notice changes in divisor frequency, and adjust clock
953 	 * frequency accordingly.
954 	 */
955 	if (spc->spc_psdiv != psdiv) {
956 		spc->spc_psdiv = psdiv;
957 		spc->spc_pscnt = psdiv;
958 		if (psdiv == 1) {
959 			setstatclockrate(stathz);
960 		} else {
961 			setstatclockrate(profhz);
962 		}
963 	}
964 	l = ci->ci_data.cpu_onproc;
965 	if ((l->l_flag & LW_IDLE) != 0) {
966 		/*
967 		 * don't account idle lwps as swapper.
968 		 */
969 		p = NULL;
970 	} else {
971 		p = l->l_proc;
972 		mutex_spin_enter(&p->p_stmutex);
973 	}
974 
975 	if (CLKF_USERMODE(frame)) {
976 		if ((p->p_stflag & PST_PROFIL) && profsrc == PROFSRC_CLOCK)
977 			addupc_intr(l, CLKF_PC(frame));
978 		if (--spc->spc_pscnt > 0) {
979 			mutex_spin_exit(&p->p_stmutex);
980 			return;
981 		}
982 
983 		/*
984 		 * Came from user mode; CPU was in user state.
985 		 * If this process is being profiled record the tick.
986 		 */
987 		p->p_uticks++;
988 		if (p->p_nice > NZERO)
989 			spc->spc_cp_time[CP_NICE]++;
990 		else
991 			spc->spc_cp_time[CP_USER]++;
992 	} else {
993 #ifdef GPROF
994 		/*
995 		 * Kernel statistics are just like addupc_intr, only easier.
996 		 */
997 		g = &_gmonparam;
998 		if (profsrc == PROFSRC_CLOCK && g->state == GMON_PROF_ON) {
999 			i = CLKF_PC(frame) - g->lowpc;
1000 			if (i < g->textsize) {
1001 				i /= HISTFRACTION * sizeof(*g->kcount);
1002 				g->kcount[i]++;
1003 			}
1004 		}
1005 #endif
1006 #ifdef LWP_PC
1007 		if (p != NULL && profsrc == PROFSRC_CLOCK &&
1008 		    (p->p_stflag & PST_PROFIL)) {
1009 			addupc_intr(l, LWP_PC(l));
1010 		}
1011 #endif
1012 		if (--spc->spc_pscnt > 0) {
1013 			if (p != NULL)
1014 				mutex_spin_exit(&p->p_stmutex);
1015 			return;
1016 		}
1017 		/*
1018 		 * Came from kernel mode, so we were:
1019 		 * - handling an interrupt,
1020 		 * - doing syscall or trap work on behalf of the current
1021 		 *   user process, or
1022 		 * - spinning in the idle loop.
1023 		 * Whichever it is, charge the time as appropriate.
1024 		 * Note that we charge interrupts to the current process,
1025 		 * regardless of whether they are ``for'' that process,
1026 		 * so that we know how much of its real time was spent
1027 		 * in ``non-process'' (i.e., interrupt) work.
1028 		 */
1029 		if (CLKF_INTR(frame) || (curlwp->l_pflag & LP_INTR) != 0) {
1030 			if (p != NULL) {
1031 				p->p_iticks++;
1032 			}
1033 			spc->spc_cp_time[CP_INTR]++;
1034 		} else if (p != NULL) {
1035 			p->p_sticks++;
1036 			spc->spc_cp_time[CP_SYS]++;
1037 		} else {
1038 			spc->spc_cp_time[CP_IDLE]++;
1039 		}
1040 	}
1041 	spc->spc_pscnt = psdiv;
1042 
1043 	if (p != NULL) {
1044 		++l->l_cpticks;
1045 		mutex_spin_exit(&p->p_stmutex);
1046 	}
1047 }
1048 
1049 #ifndef __HAVE_TIMECOUNTER
1050 #ifdef NTP	/* NTP phase-locked loop in kernel */
1051 /*
1052  * hardupdate() - local clock update
1053  *
1054  * This routine is called by ntp_adjtime() to update the local clock
1055  * phase and frequency. The implementation is of an adaptive-parameter,
1056  * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
1057  * time and frequency offset estimates for each call. If the kernel PPS
1058  * discipline code is configured (PPS_SYNC), the PPS signal itself
1059  * determines the new time offset, instead of the calling argument.
1060  * Presumably, calls to ntp_adjtime() occur only when the caller
1061  * believes the local clock is valid within some bound (+-128 ms with
1062  * NTP). If the caller's time is far different than the PPS time, an
1063  * argument will ensue, and it's not clear who will lose.
1064  *
1065  * For uncompensated quartz crystal oscillatores and nominal update
1066  * intervals less than 1024 s, operation should be in phase-lock mode
1067  * (STA_FLL = 0), where the loop is disciplined to phase. For update
1068  * intervals greater than thiss, operation should be in frequency-lock
1069  * mode (STA_FLL = 1), where the loop is disciplined to frequency.
1070  *
1071  * Note: splclock() is in effect.
1072  */
1073 void
1074 hardupdate(long offset)
1075 {
1076 	long ltemp, mtemp;
1077 
1078 	if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME))
1079 		return;
1080 	ltemp = offset;
1081 #ifdef PPS_SYNC
1082 	if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL)
1083 		ltemp = pps_offset;
1084 #endif /* PPS_SYNC */
1085 
1086 	/*
1087 	 * Scale the phase adjustment and clamp to the operating range.
1088 	 */
1089 	if (ltemp > MAXPHASE)
1090 		time_offset = MAXPHASE << SHIFT_UPDATE;
1091 	else if (ltemp < -MAXPHASE)
1092 		time_offset = -(MAXPHASE << SHIFT_UPDATE);
1093 	else
1094 		time_offset = ltemp << SHIFT_UPDATE;
1095 
1096 	/*
1097 	 * Select whether the frequency is to be controlled and in which
1098 	 * mode (PLL or FLL). Clamp to the operating range. Ugly
1099 	 * multiply/divide should be replaced someday.
1100 	 */
1101 	if (time_status & STA_FREQHOLD || time_reftime == 0)
1102 		time_reftime = time.tv_sec;
1103 	mtemp = time.tv_sec - time_reftime;
1104 	time_reftime = time.tv_sec;
1105 	if (time_status & STA_FLL) {
1106 		if (mtemp >= MINSEC) {
1107 			ltemp = ((time_offset / mtemp) << (SHIFT_USEC -
1108 			    SHIFT_UPDATE));
1109 			if (ltemp < 0)
1110 				time_freq -= -ltemp >> SHIFT_KH;
1111 			else
1112 				time_freq += ltemp >> SHIFT_KH;
1113 		}
1114 	} else {
1115 		if (mtemp < MAXSEC) {
1116 			ltemp *= mtemp;
1117 			if (ltemp < 0)
1118 				time_freq -= -ltemp >> (time_constant +
1119 				    time_constant + SHIFT_KF -
1120 				    SHIFT_USEC);
1121 			else
1122 				time_freq += ltemp >> (time_constant +
1123 				    time_constant + SHIFT_KF -
1124 				    SHIFT_USEC);
1125 		}
1126 	}
1127 	if (time_freq > time_tolerance)
1128 		time_freq = time_tolerance;
1129 	else if (time_freq < -time_tolerance)
1130 		time_freq = -time_tolerance;
1131 }
1132 
1133 #ifdef PPS_SYNC
1134 /*
1135  * hardpps() - discipline CPU clock oscillator to external PPS signal
1136  *
1137  * This routine is called at each PPS interrupt in order to discipline
1138  * the CPU clock oscillator to the PPS signal. It measures the PPS phase
1139  * and leaves it in a handy spot for the hardclock() routine. It
1140  * integrates successive PPS phase differences and calculates the
1141  * frequency offset. This is used in hardclock() to discipline the CPU
1142  * clock oscillator so that intrinsic frequency error is cancelled out.
1143  * The code requires the caller to capture the time and hardware counter
1144  * value at the on-time PPS signal transition.
1145  *
1146  * Note that, on some Unix systems, this routine runs at an interrupt
1147  * priority level higher than the timer interrupt routine hardclock().
1148  * Therefore, the variables used are distinct from the hardclock()
1149  * variables, except for certain exceptions: The PPS frequency pps_freq
1150  * and phase pps_offset variables are determined by this routine and
1151  * updated atomically. The time_tolerance variable can be considered a
1152  * constant, since it is infrequently changed, and then only when the
1153  * PPS signal is disabled. The watchdog counter pps_valid is updated
1154  * once per second by hardclock() and is atomically cleared in this
1155  * routine.
1156  */
1157 void
1158 hardpps(struct timeval *tvp,		/* time at PPS */
1159 	long usec			/* hardware counter at PPS */)
1160 {
1161 	long u_usec, v_usec, bigtick;
1162 	long cal_sec, cal_usec;
1163 
1164 	/*
1165 	 * An occasional glitch can be produced when the PPS interrupt
1166 	 * occurs in the hardclock() routine before the time variable is
1167 	 * updated. Here the offset is discarded when the difference
1168 	 * between it and the last one is greater than tick/2, but not
1169 	 * if the interval since the first discard exceeds 30 s.
1170 	 */
1171 	time_status |= STA_PPSSIGNAL;
1172 	time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
1173 	pps_valid = 0;
1174 	u_usec = -tvp->tv_usec;
1175 	if (u_usec < -500000)
1176 		u_usec += 1000000;
1177 	v_usec = pps_offset - u_usec;
1178 	if (v_usec < 0)
1179 		v_usec = -v_usec;
1180 	if (v_usec > (tick >> 1)) {
1181 		if (pps_glitch > MAXGLITCH) {
1182 			pps_glitch = 0;
1183 			pps_tf[2] = u_usec;
1184 			pps_tf[1] = u_usec;
1185 		} else {
1186 			pps_glitch++;
1187 			u_usec = pps_offset;
1188 		}
1189 	} else
1190 		pps_glitch = 0;
1191 
1192 	/*
1193 	 * A three-stage median filter is used to help deglitch the pps
1194 	 * time. The median sample becomes the time offset estimate; the
1195 	 * difference between the other two samples becomes the time
1196 	 * dispersion (jitter) estimate.
1197 	 */
1198 	pps_tf[2] = pps_tf[1];
1199 	pps_tf[1] = pps_tf[0];
1200 	pps_tf[0] = u_usec;
1201 	if (pps_tf[0] > pps_tf[1]) {
1202 		if (pps_tf[1] > pps_tf[2]) {
1203 			pps_offset = pps_tf[1];		/* 0 1 2 */
1204 			v_usec = pps_tf[0] - pps_tf[2];
1205 		} else if (pps_tf[2] > pps_tf[0]) {
1206 			pps_offset = pps_tf[0];		/* 2 0 1 */
1207 			v_usec = pps_tf[2] - pps_tf[1];
1208 		} else {
1209 			pps_offset = pps_tf[2];		/* 0 2 1 */
1210 			v_usec = pps_tf[0] - pps_tf[1];
1211 		}
1212 	} else {
1213 		if (pps_tf[1] < pps_tf[2]) {
1214 			pps_offset = pps_tf[1];		/* 2 1 0 */
1215 			v_usec = pps_tf[2] - pps_tf[0];
1216 		} else  if (pps_tf[2] < pps_tf[0]) {
1217 			pps_offset = pps_tf[0];		/* 1 0 2 */
1218 			v_usec = pps_tf[1] - pps_tf[2];
1219 		} else {
1220 			pps_offset = pps_tf[2];		/* 1 2 0 */
1221 			v_usec = pps_tf[1] - pps_tf[0];
1222 		}
1223 	}
1224 	if (v_usec > MAXTIME)
1225 		pps_jitcnt++;
1226 	v_usec = (v_usec << PPS_AVG) - pps_jitter;
1227 	if (v_usec < 0)
1228 		pps_jitter -= -v_usec >> PPS_AVG;
1229 	else
1230 		pps_jitter += v_usec >> PPS_AVG;
1231 	if (pps_jitter > (MAXTIME >> 1))
1232 		time_status |= STA_PPSJITTER;
1233 
1234 	/*
1235 	 * During the calibration interval adjust the starting time when
1236 	 * the tick overflows. At the end of the interval compute the
1237 	 * duration of the interval and the difference of the hardware
1238 	 * counters at the beginning and end of the interval. This code
1239 	 * is deliciously complicated by the fact valid differences may
1240 	 * exceed the value of tick when using long calibration
1241 	 * intervals and small ticks. Note that the counter can be
1242 	 * greater than tick if caught at just the wrong instant, but
1243 	 * the values returned and used here are correct.
1244 	 */
1245 	bigtick = (long)tick << SHIFT_USEC;
1246 	pps_usec -= pps_freq;
1247 	if (pps_usec >= bigtick)
1248 		pps_usec -= bigtick;
1249 	if (pps_usec < 0)
1250 		pps_usec += bigtick;
1251 	pps_time.tv_sec++;
1252 	pps_count++;
1253 	if (pps_count < (1 << pps_shift))
1254 		return;
1255 	pps_count = 0;
1256 	pps_calcnt++;
1257 	u_usec = usec << SHIFT_USEC;
1258 	v_usec = pps_usec - u_usec;
1259 	if (v_usec >= bigtick >> 1)
1260 		v_usec -= bigtick;
1261 	if (v_usec < -(bigtick >> 1))
1262 		v_usec += bigtick;
1263 	if (v_usec < 0)
1264 		v_usec = -(-v_usec >> pps_shift);
1265 	else
1266 		v_usec = v_usec >> pps_shift;
1267 	pps_usec = u_usec;
1268 	cal_sec = tvp->tv_sec;
1269 	cal_usec = tvp->tv_usec;
1270 	cal_sec -= pps_time.tv_sec;
1271 	cal_usec -= pps_time.tv_usec;
1272 	if (cal_usec < 0) {
1273 		cal_usec += 1000000;
1274 		cal_sec--;
1275 	}
1276 	pps_time = *tvp;
1277 
1278 	/*
1279 	 * Check for lost interrupts, noise, excessive jitter and
1280 	 * excessive frequency error. The number of timer ticks during
1281 	 * the interval may vary +-1 tick. Add to this a margin of one
1282 	 * tick for the PPS signal jitter and maximum frequency
1283 	 * deviation. If the limits are exceeded, the calibration
1284 	 * interval is reset to the minimum and we start over.
1285 	 */
1286 	u_usec = (long)tick << 1;
1287 	if (!((cal_sec == -1 && cal_usec > (1000000 - u_usec))
1288 	    || (cal_sec == 0 && cal_usec < u_usec))
1289 	    || v_usec > time_tolerance || v_usec < -time_tolerance) {
1290 		pps_errcnt++;
1291 		pps_shift = PPS_SHIFT;
1292 		pps_intcnt = 0;
1293 		time_status |= STA_PPSERROR;
1294 		return;
1295 	}
1296 
1297 	/*
1298 	 * A three-stage median filter is used to help deglitch the pps
1299 	 * frequency. The median sample becomes the frequency offset
1300 	 * estimate; the difference between the other two samples
1301 	 * becomes the frequency dispersion (stability) estimate.
1302 	 */
1303 	pps_ff[2] = pps_ff[1];
1304 	pps_ff[1] = pps_ff[0];
1305 	pps_ff[0] = v_usec;
1306 	if (pps_ff[0] > pps_ff[1]) {
1307 		if (pps_ff[1] > pps_ff[2]) {
1308 			u_usec = pps_ff[1];		/* 0 1 2 */
1309 			v_usec = pps_ff[0] - pps_ff[2];
1310 		} else if (pps_ff[2] > pps_ff[0]) {
1311 			u_usec = pps_ff[0];		/* 2 0 1 */
1312 			v_usec = pps_ff[2] - pps_ff[1];
1313 		} else {
1314 			u_usec = pps_ff[2];		/* 0 2 1 */
1315 			v_usec = pps_ff[0] - pps_ff[1];
1316 		}
1317 	} else {
1318 		if (pps_ff[1] < pps_ff[2]) {
1319 			u_usec = pps_ff[1];		/* 2 1 0 */
1320 			v_usec = pps_ff[2] - pps_ff[0];
1321 		} else  if (pps_ff[2] < pps_ff[0]) {
1322 			u_usec = pps_ff[0];		/* 1 0 2 */
1323 			v_usec = pps_ff[1] - pps_ff[2];
1324 		} else {
1325 			u_usec = pps_ff[2];		/* 1 2 0 */
1326 			v_usec = pps_ff[1] - pps_ff[0];
1327 		}
1328 	}
1329 
1330 	/*
1331 	 * Here the frequency dispersion (stability) is updated. If it
1332 	 * is less than one-fourth the maximum (MAXFREQ), the frequency
1333 	 * offset is updated as well, but clamped to the tolerance. It
1334 	 * will be processed later by the hardclock() routine.
1335 	 */
1336 	v_usec = (v_usec >> 1) - pps_stabil;
1337 	if (v_usec < 0)
1338 		pps_stabil -= -v_usec >> PPS_AVG;
1339 	else
1340 		pps_stabil += v_usec >> PPS_AVG;
1341 	if (pps_stabil > MAXFREQ >> 2) {
1342 		pps_stbcnt++;
1343 		time_status |= STA_PPSWANDER;
1344 		return;
1345 	}
1346 	if (time_status & STA_PPSFREQ) {
1347 		if (u_usec < 0) {
1348 			pps_freq -= -u_usec >> PPS_AVG;
1349 			if (pps_freq < -time_tolerance)
1350 				pps_freq = -time_tolerance;
1351 			u_usec = -u_usec;
1352 		} else {
1353 			pps_freq += u_usec >> PPS_AVG;
1354 			if (pps_freq > time_tolerance)
1355 				pps_freq = time_tolerance;
1356 		}
1357 	}
1358 
1359 	/*
1360 	 * Here the calibration interval is adjusted. If the maximum
1361 	 * time difference is greater than tick / 4, reduce the interval
1362 	 * by half. If this is not the case for four consecutive
1363 	 * intervals, double the interval.
1364 	 */
1365 	if (u_usec << pps_shift > bigtick >> 2) {
1366 		pps_intcnt = 0;
1367 		if (pps_shift > PPS_SHIFT)
1368 			pps_shift--;
1369 	} else if (pps_intcnt >= 4) {
1370 		pps_intcnt = 0;
1371 		if (pps_shift < PPS_SHIFTMAX)
1372 			pps_shift++;
1373 	} else
1374 		pps_intcnt++;
1375 }
1376 #endif /* PPS_SYNC */
1377 #endif /* NTP  */
1378 
1379 /* timecounter compat functions */
1380 void
1381 nanotime(struct timespec *ts)
1382 {
1383 	struct timeval tv;
1384 
1385 	microtime(&tv);
1386 	TIMEVAL_TO_TIMESPEC(&tv, ts);
1387 }
1388 
1389 void
1390 getbinuptime(struct bintime *bt)
1391 {
1392 	struct timeval tv;
1393 
1394 	microtime(&tv);
1395 	timeval2bintime(&tv, bt);
1396 }
1397 
1398 void
1399 nanouptime(struct timespec *tsp)
1400 {
1401 	int s;
1402 
1403 	s = splclock();
1404 	TIMEVAL_TO_TIMESPEC(&mono_time, tsp);
1405 	splx(s);
1406 }
1407 
1408 void
1409 getnanouptime(struct timespec *tsp)
1410 {
1411 	int s;
1412 
1413 	s = splclock();
1414 	TIMEVAL_TO_TIMESPEC(&mono_time, tsp);
1415 	splx(s);
1416 }
1417 
1418 void
1419 getmicrouptime(struct timeval *tvp)
1420 {
1421 	int s;
1422 
1423 	s = splclock();
1424 	*tvp = mono_time;
1425 	splx(s);
1426 }
1427 
1428 void
1429 getnanotime(struct timespec *tsp)
1430 {
1431 	int s;
1432 
1433 	s = splclock();
1434 	TIMEVAL_TO_TIMESPEC(&time, tsp);
1435 	splx(s);
1436 }
1437 
1438 void
1439 getmicrotime(struct timeval *tvp)
1440 {
1441 	int s;
1442 
1443 	s = splclock();
1444 	*tvp = time;
1445 	splx(s);
1446 }
1447 
1448 u_int64_t
1449 tc_getfrequency(void)
1450 {
1451 	return hz;
1452 }
1453 #endif /* !__HAVE_TIMECOUNTER */
1454