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