Home | History | Annotate | Line # | Download | only in kern
kern_clock.c revision 1.90.2.1
      1 /*	$NetBSD: kern_clock.c,v 1.90.2.1 2004/09/16 03:23:27 jmc Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 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  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*-
     41  * Copyright (c) 1982, 1986, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  * (c) UNIX System Laboratories, Inc.
     44  * All or some portions of this file are derived from material licensed
     45  * to the University of California by American Telephone and Telegraph
     46  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     47  * the permission of UNIX System Laboratories, Inc.
     48  *
     49  * Redistribution and use in source and binary forms, with or without
     50  * modification, are permitted provided that the following conditions
     51  * are met:
     52  * 1. Redistributions of source code must retain the above copyright
     53  *    notice, this list of conditions and the following disclaimer.
     54  * 2. Redistributions in binary form must reproduce the above copyright
     55  *    notice, this list of conditions and the following disclaimer in the
     56  *    documentation and/or other materials provided with the distribution.
     57  * 3. Neither the name of the University nor the names of its contributors
     58  *    may be used to endorse or promote products derived from this software
     59  *    without specific prior written permission.
     60  *
     61  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71  * SUCH DAMAGE.
     72  *
     73  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
     74  */
     75 
     76 #include <sys/cdefs.h>
     77 __KERNEL_RCSID(0, "$NetBSD: kern_clock.c,v 1.90.2.1 2004/09/16 03:23:27 jmc Exp $");
     78 
     79 #include "opt_ntp.h"
     80 #include "opt_multiprocessor.h"
     81 #include "opt_perfctrs.h"
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/callout.h>
     86 #include <sys/kernel.h>
     87 #include <sys/proc.h>
     88 #include <sys/resourcevar.h>
     89 #include <sys/signalvar.h>
     90 #include <sys/sysctl.h>
     91 #include <sys/timex.h>
     92 #include <sys/sched.h>
     93 #include <sys/time.h>
     94 
     95 #include <machine/cpu.h>
     96 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
     97 #include <machine/intr.h>
     98 #endif
     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 #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 #ifdef HIGHBALL
    296 /*
    297  * The clock_offset and clock_cpu variables are used by the HIGHBALL
    298  * interface. The clock_offset variable defines the offset between
    299  * system time and the HIGBALL counters. The clock_cpu variable contains
    300  * the offset between the system clock and the HIGHBALL clock for use in
    301  * disciplining the kernel time variable.
    302  */
    303 extern struct timeval clock_offset; /* Highball clock offset */
    304 long clock_cpu = 0;		/* CPU clock adjust */
    305 #endif /* HIGHBALL */
    306 #endif /* EXT_CLOCK */
    307 #endif /* NTP */
    308 
    309 
    310 /*
    311  * Bump a timeval by a small number of usec's.
    312  */
    313 #define BUMPTIME(t, usec) { \
    314 	volatile struct timeval *tp = (t); \
    315 	long us; \
    316  \
    317 	tp->tv_usec = us = tp->tv_usec + (usec); \
    318 	if (us >= 1000000) { \
    319 		tp->tv_usec = us - 1000000; \
    320 		tp->tv_sec++; \
    321 	} \
    322 }
    323 
    324 int	stathz;
    325 int	profhz;
    326 int	profsrc;
    327 int	schedhz;
    328 int	profprocs;
    329 int	hardclock_ticks;
    330 static int psdiv;			/* prof => stat divider */
    331 int	psratio;			/* ratio: prof / stat */
    332 int	tickfix, tickfixinterval;	/* used if tick not really integral */
    333 #ifndef NTP
    334 static int tickfixcnt;			/* accumulated fractional error */
    335 #else
    336 int	fixtick;			/* used by NTP for same */
    337 int	shifthz;
    338 #endif
    339 
    340 /*
    341  * We might want ldd to load the both words from time at once.
    342  * To succeed we need to be quadword aligned.
    343  * The sparc already does that, and that it has worked so far is a fluke.
    344  */
    345 volatile struct	timeval time  __attribute__((__aligned__(__alignof__(quad_t))));
    346 volatile struct	timeval mono_time;
    347 
    348 void	*softclock_si;
    349 
    350 /*
    351  * Initialize clock frequencies and start both clocks running.
    352  */
    353 void
    354 initclocks(void)
    355 {
    356 	int i;
    357 
    358 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    359 	softclock_si = softintr_establish(IPL_SOFTCLOCK, softclock, NULL);
    360 	if (softclock_si == NULL)
    361 		panic("initclocks: unable to register softclock intr");
    362 #endif
    363 
    364 	/*
    365 	 * Set divisors to 1 (normal case) and let the machine-specific
    366 	 * code do its bit.
    367 	 */
    368 	psdiv = 1;
    369 	cpu_initclocks();
    370 
    371 	/*
    372 	 * Compute profhz/stathz/rrticks, and fix profhz if needed.
    373 	 */
    374 	i = stathz ? stathz : hz;
    375 	if (profhz == 0)
    376 		profhz = i;
    377 	psratio = profhz / i;
    378 	rrticks = hz / 10;
    379 
    380 #ifdef NTP
    381 	switch (hz) {
    382 	case 1:
    383 		shifthz = SHIFT_SCALE - 0;
    384 		break;
    385 	case 2:
    386 		shifthz = SHIFT_SCALE - 1;
    387 		break;
    388 	case 4:
    389 		shifthz = SHIFT_SCALE - 2;
    390 		break;
    391 	case 8:
    392 		shifthz = SHIFT_SCALE - 3;
    393 		break;
    394 	case 16:
    395 		shifthz = SHIFT_SCALE - 4;
    396 		break;
    397 	case 32:
    398 		shifthz = SHIFT_SCALE - 5;
    399 		break;
    400 	case 50:
    401 	case 60:
    402 	case 64:
    403 		shifthz = SHIFT_SCALE - 6;
    404 		break;
    405 	case 96:
    406 	case 100:
    407 	case 128:
    408 		shifthz = SHIFT_SCALE - 7;
    409 		break;
    410 	case 256:
    411 		shifthz = SHIFT_SCALE - 8;
    412 		break;
    413 	case 512:
    414 		shifthz = SHIFT_SCALE - 9;
    415 		break;
    416 	case 1000:
    417 	case 1024:
    418 		shifthz = SHIFT_SCALE - 10;
    419 		break;
    420 	case 1200:
    421 	case 2048:
    422 		shifthz = SHIFT_SCALE - 11;
    423 		break;
    424 	case 4096:
    425 		shifthz = SHIFT_SCALE - 12;
    426 		break;
    427 	case 8192:
    428 		shifthz = SHIFT_SCALE - 13;
    429 		break;
    430 	case 16384:
    431 		shifthz = SHIFT_SCALE - 14;
    432 		break;
    433 	case 32768:
    434 		shifthz = SHIFT_SCALE - 15;
    435 		break;
    436 	case 65536:
    437 		shifthz = SHIFT_SCALE - 16;
    438 		break;
    439 	default:
    440 		panic("weird hz");
    441 	}
    442 	if (fixtick == 0) {
    443 		/*
    444 		 * Give MD code a chance to set this to a better
    445 		 * value; but, if it doesn't, we should.
    446 		 */
    447 		fixtick = (1000000 - (hz*tick));
    448 	}
    449 #endif
    450 }
    451 
    452 /*
    453  * The real-time timer, interrupting hz times per second.
    454  */
    455 void
    456 hardclock(struct clockframe *frame)
    457 {
    458 	struct lwp *l;
    459 	struct proc *p;
    460 	int delta;
    461 	extern int tickdelta;
    462 	extern long timedelta;
    463 	struct cpu_info *ci = curcpu();
    464 	struct ptimer *pt;
    465 #ifdef NTP
    466 	int time_update;
    467 	int ltemp;
    468 #endif
    469 
    470 	l = curlwp;
    471 	if (l) {
    472 		p = l->l_proc;
    473 		/*
    474 		 * Run current process's virtual and profile time, as needed.
    475 		 */
    476 		if (CLKF_USERMODE(frame) && p->p_timers &&
    477 		    (pt = LIST_FIRST(&p->p_timers->pts_virtual)) != NULL)
    478 			if (itimerdecr(pt, tick) == 0)
    479 				itimerfire(pt);
    480 		if (p->p_timers &&
    481 		    (pt = LIST_FIRST(&p->p_timers->pts_prof)) != NULL)
    482 			if (itimerdecr(pt, tick) == 0)
    483 				itimerfire(pt);
    484 	}
    485 
    486 	/*
    487 	 * If no separate statistics clock is available, run it from here.
    488 	 */
    489 	if (stathz == 0)
    490 		statclock(frame);
    491 	if ((--ci->ci_schedstate.spc_rrticks) <= 0)
    492 		roundrobin(ci);
    493 
    494 #if defined(MULTIPROCESSOR)
    495 	/*
    496 	 * If we are not the primary CPU, we're not allowed to do
    497 	 * any more work.
    498 	 */
    499 	if (CPU_IS_PRIMARY(ci) == 0)
    500 		return;
    501 #endif
    502 
    503 	/*
    504 	 * Increment the time-of-day.  The increment is normally just
    505 	 * ``tick''.  If the machine is one which has a clock frequency
    506 	 * such that ``hz'' would not divide the second evenly into
    507 	 * milliseconds, a periodic adjustment must be applied.  Finally,
    508 	 * if we are still adjusting the time (see adjtime()),
    509 	 * ``tickdelta'' may also be added in.
    510 	 */
    511 	hardclock_ticks++;
    512 	delta = tick;
    513 
    514 #ifndef NTP
    515 	if (tickfix) {
    516 		tickfixcnt += tickfix;
    517 		if (tickfixcnt >= tickfixinterval) {
    518 			delta++;
    519 			tickfixcnt -= tickfixinterval;
    520 		}
    521 	}
    522 #endif /* !NTP */
    523 	/* Imprecise 4bsd adjtime() handling */
    524 	if (timedelta != 0) {
    525 		delta += tickdelta;
    526 		timedelta -= tickdelta;
    527 	}
    528 
    529 #ifdef notyet
    530 	microset();
    531 #endif
    532 
    533 #ifndef NTP
    534 	BUMPTIME(&time, delta);		/* XXX Now done using NTP code below */
    535 #endif
    536 	BUMPTIME(&mono_time, delta);
    537 
    538 #ifdef NTP
    539 	time_update = delta;
    540 
    541 	/*
    542 	 * Compute the phase adjustment. If the low-order bits
    543 	 * (time_phase) of the update overflow, bump the high-order bits
    544 	 * (time_update).
    545 	 */
    546 	time_phase += time_adj;
    547 	if (time_phase <= -FINEUSEC) {
    548 		ltemp = -time_phase >> SHIFT_SCALE;
    549 		time_phase += ltemp << SHIFT_SCALE;
    550 		time_update -= ltemp;
    551 	} else if (time_phase >= FINEUSEC) {
    552 		ltemp = time_phase >> SHIFT_SCALE;
    553 		time_phase -= ltemp << SHIFT_SCALE;
    554 		time_update += ltemp;
    555 	}
    556 
    557 #ifdef HIGHBALL
    558 	/*
    559 	 * If the HIGHBALL board is installed, we need to adjust the
    560 	 * external clock offset in order to close the hardware feedback
    561 	 * loop. This will adjust the external clock phase and frequency
    562 	 * in small amounts. The additional phase noise and frequency
    563 	 * wander this causes should be minimal. We also need to
    564 	 * discipline the kernel time variable, since the PLL is used to
    565 	 * discipline the external clock. If the Highball board is not
    566 	 * present, we discipline kernel time with the PLL as usual. We
    567 	 * assume that the external clock phase adjustment (time_update)
    568 	 * and kernel phase adjustment (clock_cpu) are less than the
    569 	 * value of tick.
    570 	 */
    571 	clock_offset.tv_usec += time_update;
    572 	if (clock_offset.tv_usec >= 1000000) {
    573 		clock_offset.tv_sec++;
    574 		clock_offset.tv_usec -= 1000000;
    575 	}
    576 	if (clock_offset.tv_usec < 0) {
    577 		clock_offset.tv_sec--;
    578 		clock_offset.tv_usec += 1000000;
    579 	}
    580 	time.tv_usec += clock_cpu;
    581 	clock_cpu = 0;
    582 #else
    583 	time.tv_usec += time_update;
    584 #endif /* HIGHBALL */
    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 #ifdef HIGHBALL
    816 			clock_cpu = delta.tv_usec;
    817 #else /* HIGHBALL */
    818 			hardupdate(delta.tv_usec);
    819 #endif /* HIGHBALL */
    820 		}
    821 #endif /* EXT_CLOCK */
    822 	}
    823 
    824 #endif /* NTP */
    825 
    826 	/*
    827 	 * Update real-time timeout queue.
    828 	 * Process callouts at a very low CPU priority, so we don't keep the
    829 	 * relatively high clock interrupt priority any longer than necessary.
    830 	 */
    831 	if (callout_hardclock()) {
    832 		if (CLKF_BASEPRI(frame)) {
    833 			/*
    834 			 * Save the overhead of a software interrupt;
    835 			 * it will happen as soon as we return, so do
    836 			 * it now.
    837 			 */
    838 			spllowersoftclock();
    839 			KERNEL_LOCK(LK_CANRECURSE|LK_EXCLUSIVE);
    840 			softclock(NULL);
    841 			KERNEL_UNLOCK();
    842 		} else {
    843 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    844 			softintr_schedule(softclock_si);
    845 #else
    846 			setsoftclock();
    847 #endif
    848 		}
    849 	}
    850 }
    851 
    852 /*
    853  * Compute number of hz until specified time.  Used to compute second
    854  * argument to callout_reset() from an absolute time.
    855  */
    856 int
    857 hzto(struct timeval *tv)
    858 {
    859 	unsigned long ticks;
    860 	long sec, usec;
    861 	int s;
    862 
    863 	/*
    864 	 * If the number of usecs in the whole seconds part of the time
    865 	 * difference fits in a long, then the total number of usecs will
    866 	 * fit in an unsigned long.  Compute the total and convert it to
    867 	 * ticks, rounding up and adding 1 to allow for the current tick
    868 	 * to expire.  Rounding also depends on unsigned long arithmetic
    869 	 * to avoid overflow.
    870 	 *
    871 	 * Otherwise, if the number of ticks in the whole seconds part of
    872 	 * the time difference fits in a long, then convert the parts to
    873 	 * ticks separately and add, using similar rounding methods and
    874 	 * overflow avoidance.  This method would work in the previous
    875 	 * case, but it is slightly slower and assume that hz is integral.
    876 	 *
    877 	 * Otherwise, round the time difference down to the maximum
    878 	 * representable value.
    879 	 *
    880 	 * If ints are 32-bit, then the maximum value for any timeout in
    881 	 * 10ms ticks is 248 days.
    882 	 */
    883 	s = splclock();
    884 	sec = tv->tv_sec - time.tv_sec;
    885 	usec = tv->tv_usec - time.tv_usec;
    886 	splx(s);
    887 
    888 	if (usec < 0) {
    889 		sec--;
    890 		usec += 1000000;
    891 	}
    892 
    893 	if (sec < 0 || (sec == 0 && usec <= 0)) {
    894 		/*
    895 		 * Would expire now or in the past.  Return 0 ticks.
    896 		 * This is different from the legacy hzto() interface,
    897 		 * and callers need to check for it.
    898 		 */
    899 		ticks = 0;
    900 	} else if (sec <= (LONG_MAX / 1000000))
    901 		ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
    902 		    / tick) + 1;
    903 	else if (sec <= (LONG_MAX / hz))
    904 		ticks = (sec * hz) +
    905 		    (((unsigned long)usec + (tick - 1)) / tick) + 1;
    906 	else
    907 		ticks = LONG_MAX;
    908 
    909 	if (ticks > INT_MAX)
    910 		ticks = INT_MAX;
    911 
    912 	return ((int)ticks);
    913 }
    914 
    915 /*
    916  * Start profiling on a process.
    917  *
    918  * Kernel profiling passes proc0 which never exits and hence
    919  * keeps the profile clock running constantly.
    920  */
    921 void
    922 startprofclock(struct proc *p)
    923 {
    924 
    925 	if ((p->p_flag & P_PROFIL) == 0) {
    926 		p->p_flag |= P_PROFIL;
    927 		/*
    928 		 * This is only necessary if using the clock as the
    929 		 * profiling source.
    930 		 */
    931 		if (++profprocs == 1 && stathz != 0)
    932 			psdiv = psratio;
    933 	}
    934 }
    935 
    936 /*
    937  * Stop profiling on a process.
    938  */
    939 void
    940 stopprofclock(struct proc *p)
    941 {
    942 
    943 	if (p->p_flag & P_PROFIL) {
    944 		p->p_flag &= ~P_PROFIL;
    945 		/*
    946 		 * This is only necessary if using the clock as the
    947 		 * profiling source.
    948 		 */
    949 		if (--profprocs == 0 && stathz != 0)
    950 			psdiv = 1;
    951 	}
    952 }
    953 
    954 #if defined(PERFCTRS)
    955 /*
    956  * Independent profiling "tick" in case we're using a separate
    957  * clock or profiling event source.  Currently, that's just
    958  * performance counters--hence the wrapper.
    959  */
    960 void
    961 proftick(struct clockframe *frame)
    962 {
    963 #ifdef GPROF
    964         struct gmonparam *g;
    965         intptr_t i;
    966 #endif
    967 	struct proc *p;
    968 
    969 	p = curproc;
    970 	if (CLKF_USERMODE(frame)) {
    971 		if (p->p_flag & P_PROFIL)
    972 			addupc_intr(p, CLKF_PC(frame));
    973 	} else {
    974 #ifdef GPROF
    975 		g = &_gmonparam;
    976 		if (g->state == GMON_PROF_ON) {
    977 			i = CLKF_PC(frame) - g->lowpc;
    978 			if (i < g->textsize) {
    979 				i /= HISTFRACTION * sizeof(*g->kcount);
    980 				g->kcount[i]++;
    981 			}
    982 		}
    983 #endif
    984 #ifdef PROC_PC
    985                 if (p && p->p_flag & P_PROFIL)
    986                         addupc_intr(p, PROC_PC(p));
    987 #endif
    988 	}
    989 }
    990 #endif
    991 
    992 /*
    993  * Statistics clock.  Grab profile sample, and if divider reaches 0,
    994  * do process and kernel statistics.
    995  */
    996 void
    997 statclock(struct clockframe *frame)
    998 {
    999 #ifdef GPROF
   1000 	struct gmonparam *g;
   1001 	intptr_t i;
   1002 #endif
   1003 	struct cpu_info *ci = curcpu();
   1004 	struct schedstate_percpu *spc = &ci->ci_schedstate;
   1005 	struct lwp *l;
   1006 	struct proc *p;
   1007 
   1008 	/*
   1009 	 * Notice changes in divisor frequency, and adjust clock
   1010 	 * frequency accordingly.
   1011 	 */
   1012 	if (spc->spc_psdiv != psdiv) {
   1013 		spc->spc_psdiv = psdiv;
   1014 		spc->spc_pscnt = psdiv;
   1015 		if (psdiv == 1) {
   1016 			setstatclockrate(stathz);
   1017 		} else {
   1018 			setstatclockrate(profhz);
   1019 		}
   1020 	}
   1021 	l = curlwp;
   1022 	p = (l ? l->l_proc : 0);
   1023 	if (CLKF_USERMODE(frame)) {
   1024 		if (p->p_flag & P_PROFIL && profsrc == PROFSRC_CLOCK)
   1025 			addupc_intr(p, CLKF_PC(frame));
   1026 		if (--spc->spc_pscnt > 0)
   1027 			return;
   1028 		/*
   1029 		 * Came from user mode; CPU was in user state.
   1030 		 * If this process is being profiled record the tick.
   1031 		 */
   1032 		p->p_uticks++;
   1033 		if (p->p_nice > NZERO)
   1034 			spc->spc_cp_time[CP_NICE]++;
   1035 		else
   1036 			spc->spc_cp_time[CP_USER]++;
   1037 	} else {
   1038 #ifdef GPROF
   1039 		/*
   1040 		 * Kernel statistics are just like addupc_intr, only easier.
   1041 		 */
   1042 		g = &_gmonparam;
   1043 		if (profsrc == PROFSRC_CLOCK && g->state == GMON_PROF_ON) {
   1044 			i = CLKF_PC(frame) - g->lowpc;
   1045 			if (i < g->textsize) {
   1046 				i /= HISTFRACTION * sizeof(*g->kcount);
   1047 				g->kcount[i]++;
   1048 			}
   1049 		}
   1050 #endif
   1051 #ifdef LWP_PC
   1052 		if (p && profsrc == PROFSRC_CLOCK && p->p_flag & P_PROFIL)
   1053 			addupc_intr(p, LWP_PC(l));
   1054 #endif
   1055 		if (--spc->spc_pscnt > 0)
   1056 			return;
   1057 		/*
   1058 		 * Came from kernel mode, so we were:
   1059 		 * - handling an interrupt,
   1060 		 * - doing syscall or trap work on behalf of the current
   1061 		 *   user process, or
   1062 		 * - spinning in the idle loop.
   1063 		 * Whichever it is, charge the time as appropriate.
   1064 		 * Note that we charge interrupts to the current process,
   1065 		 * regardless of whether they are ``for'' that process,
   1066 		 * so that we know how much of its real time was spent
   1067 		 * in ``non-process'' (i.e., interrupt) work.
   1068 		 */
   1069 		if (CLKF_INTR(frame)) {
   1070 			if (p != NULL)
   1071 				p->p_iticks++;
   1072 			spc->spc_cp_time[CP_INTR]++;
   1073 		} else if (p != NULL) {
   1074 			p->p_sticks++;
   1075 			spc->spc_cp_time[CP_SYS]++;
   1076 		} else
   1077 			spc->spc_cp_time[CP_IDLE]++;
   1078 	}
   1079 	spc->spc_pscnt = psdiv;
   1080 
   1081 	if (l != NULL) {
   1082 		++p->p_cpticks;
   1083 		/*
   1084 		 * If no separate schedclock is provided, call it here
   1085 		 * at ~~12-25 Hz, ~~16 Hz is best
   1086 		 */
   1087 		if (schedhz == 0)
   1088 			if ((++ci->ci_schedstate.spc_schedticks & 3) == 0)
   1089 				schedclock(l);
   1090 	}
   1091 }
   1092 
   1093 
   1094 #ifdef NTP	/* NTP phase-locked loop in kernel */
   1095 
   1096 /*
   1097  * hardupdate() - local clock update
   1098  *
   1099  * This routine is called by ntp_adjtime() to update the local clock
   1100  * phase and frequency. The implementation is of an adaptive-parameter,
   1101  * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
   1102  * time and frequency offset estimates for each call. If the kernel PPS
   1103  * discipline code is configured (PPS_SYNC), the PPS signal itself
   1104  * determines the new time offset, instead of the calling argument.
   1105  * Presumably, calls to ntp_adjtime() occur only when the caller
   1106  * believes the local clock is valid within some bound (+-128 ms with
   1107  * NTP). If the caller's time is far different than the PPS time, an
   1108  * argument will ensue, and it's not clear who will lose.
   1109  *
   1110  * For uncompensated quartz crystal oscillatores and nominal update
   1111  * intervals less than 1024 s, operation should be in phase-lock mode
   1112  * (STA_FLL = 0), where the loop is disciplined to phase. For update
   1113  * intervals greater than thiss, operation should be in frequency-lock
   1114  * mode (STA_FLL = 1), where the loop is disciplined to frequency.
   1115  *
   1116  * Note: splclock() is in effect.
   1117  */
   1118 void
   1119 hardupdate(long offset)
   1120 {
   1121 	long ltemp, mtemp;
   1122 
   1123 	if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME))
   1124 		return;
   1125 	ltemp = offset;
   1126 #ifdef PPS_SYNC
   1127 	if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL)
   1128 		ltemp = pps_offset;
   1129 #endif /* PPS_SYNC */
   1130 
   1131 	/*
   1132 	 * Scale the phase adjustment and clamp to the operating range.
   1133 	 */
   1134 	if (ltemp > MAXPHASE)
   1135 		time_offset = MAXPHASE << SHIFT_UPDATE;
   1136 	else if (ltemp < -MAXPHASE)
   1137 		time_offset = -(MAXPHASE << SHIFT_UPDATE);
   1138 	else
   1139 		time_offset = ltemp << SHIFT_UPDATE;
   1140 
   1141 	/*
   1142 	 * Select whether the frequency is to be controlled and in which
   1143 	 * mode (PLL or FLL). Clamp to the operating range. Ugly
   1144 	 * multiply/divide should be replaced someday.
   1145 	 */
   1146 	if (time_status & STA_FREQHOLD || time_reftime == 0)
   1147 		time_reftime = time.tv_sec;
   1148 	mtemp = time.tv_sec - time_reftime;
   1149 	time_reftime = time.tv_sec;
   1150 	if (time_status & STA_FLL) {
   1151 		if (mtemp >= MINSEC) {
   1152 			ltemp = ((time_offset / mtemp) << (SHIFT_USEC -
   1153 			    SHIFT_UPDATE));
   1154 			if (ltemp < 0)
   1155 				time_freq -= -ltemp >> SHIFT_KH;
   1156 			else
   1157 				time_freq += ltemp >> SHIFT_KH;
   1158 		}
   1159 	} else {
   1160 		if (mtemp < MAXSEC) {
   1161 			ltemp *= mtemp;
   1162 			if (ltemp < 0)
   1163 				time_freq -= -ltemp >> (time_constant +
   1164 				    time_constant + SHIFT_KF -
   1165 				    SHIFT_USEC);
   1166 			else
   1167 				time_freq += ltemp >> (time_constant +
   1168 				    time_constant + SHIFT_KF -
   1169 				    SHIFT_USEC);
   1170 		}
   1171 	}
   1172 	if (time_freq > time_tolerance)
   1173 		time_freq = time_tolerance;
   1174 	else if (time_freq < -time_tolerance)
   1175 		time_freq = -time_tolerance;
   1176 }
   1177 
   1178 #ifdef PPS_SYNC
   1179 /*
   1180  * hardpps() - discipline CPU clock oscillator to external PPS signal
   1181  *
   1182  * This routine is called at each PPS interrupt in order to discipline
   1183  * the CPU clock oscillator to the PPS signal. It measures the PPS phase
   1184  * and leaves it in a handy spot for the hardclock() routine. It
   1185  * integrates successive PPS phase differences and calculates the
   1186  * frequency offset. This is used in hardclock() to discipline the CPU
   1187  * clock oscillator so that intrinsic frequency error is cancelled out.
   1188  * The code requires the caller to capture the time and hardware counter
   1189  * value at the on-time PPS signal transition.
   1190  *
   1191  * Note that, on some Unix systems, this routine runs at an interrupt
   1192  * priority level higher than the timer interrupt routine hardclock().
   1193  * Therefore, the variables used are distinct from the hardclock()
   1194  * variables, except for certain exceptions: The PPS frequency pps_freq
   1195  * and phase pps_offset variables are determined by this routine and
   1196  * updated atomically. The time_tolerance variable can be considered a
   1197  * constant, since it is infrequently changed, and then only when the
   1198  * PPS signal is disabled. The watchdog counter pps_valid is updated
   1199  * once per second by hardclock() and is atomically cleared in this
   1200  * routine.
   1201  */
   1202 void
   1203 hardpps(struct timeval *tvp,		/* time at PPS */
   1204 	long usec			/* hardware counter at PPS */)
   1205 {
   1206 	long u_usec, v_usec, bigtick;
   1207 	long cal_sec, cal_usec;
   1208 
   1209 	/*
   1210 	 * An occasional glitch can be produced when the PPS interrupt
   1211 	 * occurs in the hardclock() routine before the time variable is
   1212 	 * updated. Here the offset is discarded when the difference
   1213 	 * between it and the last one is greater than tick/2, but not
   1214 	 * if the interval since the first discard exceeds 30 s.
   1215 	 */
   1216 	time_status |= STA_PPSSIGNAL;
   1217 	time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
   1218 	pps_valid = 0;
   1219 	u_usec = -tvp->tv_usec;
   1220 	if (u_usec < -500000)
   1221 		u_usec += 1000000;
   1222 	v_usec = pps_offset - u_usec;
   1223 	if (v_usec < 0)
   1224 		v_usec = -v_usec;
   1225 	if (v_usec > (tick >> 1)) {
   1226 		if (pps_glitch > MAXGLITCH) {
   1227 			pps_glitch = 0;
   1228 			pps_tf[2] = u_usec;
   1229 			pps_tf[1] = u_usec;
   1230 		} else {
   1231 			pps_glitch++;
   1232 			u_usec = pps_offset;
   1233 		}
   1234 	} else
   1235 		pps_glitch = 0;
   1236 
   1237 	/*
   1238 	 * A three-stage median filter is used to help deglitch the pps
   1239 	 * time. The median sample becomes the time offset estimate; the
   1240 	 * difference between the other two samples becomes the time
   1241 	 * dispersion (jitter) estimate.
   1242 	 */
   1243 	pps_tf[2] = pps_tf[1];
   1244 	pps_tf[1] = pps_tf[0];
   1245 	pps_tf[0] = u_usec;
   1246 	if (pps_tf[0] > pps_tf[1]) {
   1247 		if (pps_tf[1] > pps_tf[2]) {
   1248 			pps_offset = pps_tf[1];		/* 0 1 2 */
   1249 			v_usec = pps_tf[0] - pps_tf[2];
   1250 		} else if (pps_tf[2] > pps_tf[0]) {
   1251 			pps_offset = pps_tf[0];		/* 2 0 1 */
   1252 			v_usec = pps_tf[2] - pps_tf[1];
   1253 		} else {
   1254 			pps_offset = pps_tf[2];		/* 0 2 1 */
   1255 			v_usec = pps_tf[0] - pps_tf[1];
   1256 		}
   1257 	} else {
   1258 		if (pps_tf[1] < pps_tf[2]) {
   1259 			pps_offset = pps_tf[1];		/* 2 1 0 */
   1260 			v_usec = pps_tf[2] - pps_tf[0];
   1261 		} else  if (pps_tf[2] < pps_tf[0]) {
   1262 			pps_offset = pps_tf[0];		/* 1 0 2 */
   1263 			v_usec = pps_tf[1] - pps_tf[2];
   1264 		} else {
   1265 			pps_offset = pps_tf[2];		/* 1 2 0 */
   1266 			v_usec = pps_tf[1] - pps_tf[0];
   1267 		}
   1268 	}
   1269 	if (v_usec > MAXTIME)
   1270 		pps_jitcnt++;
   1271 	v_usec = (v_usec << PPS_AVG) - pps_jitter;
   1272 	if (v_usec < 0)
   1273 		pps_jitter -= -v_usec >> PPS_AVG;
   1274 	else
   1275 		pps_jitter += v_usec >> PPS_AVG;
   1276 	if (pps_jitter > (MAXTIME >> 1))
   1277 		time_status |= STA_PPSJITTER;
   1278 
   1279 	/*
   1280 	 * During the calibration interval adjust the starting time when
   1281 	 * the tick overflows. At the end of the interval compute the
   1282 	 * duration of the interval and the difference of the hardware
   1283 	 * counters at the beginning and end of the interval. This code
   1284 	 * is deliciously complicated by the fact valid differences may
   1285 	 * exceed the value of tick when using long calibration
   1286 	 * intervals and small ticks. Note that the counter can be
   1287 	 * greater than tick if caught at just the wrong instant, but
   1288 	 * the values returned and used here are correct.
   1289 	 */
   1290 	bigtick = (long)tick << SHIFT_USEC;
   1291 	pps_usec -= pps_freq;
   1292 	if (pps_usec >= bigtick)
   1293 		pps_usec -= bigtick;
   1294 	if (pps_usec < 0)
   1295 		pps_usec += bigtick;
   1296 	pps_time.tv_sec++;
   1297 	pps_count++;
   1298 	if (pps_count < (1 << pps_shift))
   1299 		return;
   1300 	pps_count = 0;
   1301 	pps_calcnt++;
   1302 	u_usec = usec << SHIFT_USEC;
   1303 	v_usec = pps_usec - u_usec;
   1304 	if (v_usec >= bigtick >> 1)
   1305 		v_usec -= bigtick;
   1306 	if (v_usec < -(bigtick >> 1))
   1307 		v_usec += bigtick;
   1308 	if (v_usec < 0)
   1309 		v_usec = -(-v_usec >> pps_shift);
   1310 	else
   1311 		v_usec = v_usec >> pps_shift;
   1312 	pps_usec = u_usec;
   1313 	cal_sec = tvp->tv_sec;
   1314 	cal_usec = tvp->tv_usec;
   1315 	cal_sec -= pps_time.tv_sec;
   1316 	cal_usec -= pps_time.tv_usec;
   1317 	if (cal_usec < 0) {
   1318 		cal_usec += 1000000;
   1319 		cal_sec--;
   1320 	}
   1321 	pps_time = *tvp;
   1322 
   1323 	/*
   1324 	 * Check for lost interrupts, noise, excessive jitter and
   1325 	 * excessive frequency error. The number of timer ticks during
   1326 	 * the interval may vary +-1 tick. Add to this a margin of one
   1327 	 * tick for the PPS signal jitter and maximum frequency
   1328 	 * deviation. If the limits are exceeded, the calibration
   1329 	 * interval is reset to the minimum and we start over.
   1330 	 */
   1331 	u_usec = (long)tick << 1;
   1332 	if (!((cal_sec == -1 && cal_usec > (1000000 - u_usec))
   1333 	    || (cal_sec == 0 && cal_usec < u_usec))
   1334 	    || v_usec > time_tolerance || v_usec < -time_tolerance) {
   1335 		pps_errcnt++;
   1336 		pps_shift = PPS_SHIFT;
   1337 		pps_intcnt = 0;
   1338 		time_status |= STA_PPSERROR;
   1339 		return;
   1340 	}
   1341 
   1342 	/*
   1343 	 * A three-stage median filter is used to help deglitch the pps
   1344 	 * frequency. The median sample becomes the frequency offset
   1345 	 * estimate; the difference between the other two samples
   1346 	 * becomes the frequency dispersion (stability) estimate.
   1347 	 */
   1348 	pps_ff[2] = pps_ff[1];
   1349 	pps_ff[1] = pps_ff[0];
   1350 	pps_ff[0] = v_usec;
   1351 	if (pps_ff[0] > pps_ff[1]) {
   1352 		if (pps_ff[1] > pps_ff[2]) {
   1353 			u_usec = pps_ff[1];		/* 0 1 2 */
   1354 			v_usec = pps_ff[0] - pps_ff[2];
   1355 		} else if (pps_ff[2] > pps_ff[0]) {
   1356 			u_usec = pps_ff[0];		/* 2 0 1 */
   1357 			v_usec = pps_ff[2] - pps_ff[1];
   1358 		} else {
   1359 			u_usec = pps_ff[2];		/* 0 2 1 */
   1360 			v_usec = pps_ff[0] - pps_ff[1];
   1361 		}
   1362 	} else {
   1363 		if (pps_ff[1] < pps_ff[2]) {
   1364 			u_usec = pps_ff[1];		/* 2 1 0 */
   1365 			v_usec = pps_ff[2] - pps_ff[0];
   1366 		} else  if (pps_ff[2] < pps_ff[0]) {
   1367 			u_usec = pps_ff[0];		/* 1 0 2 */
   1368 			v_usec = pps_ff[1] - pps_ff[2];
   1369 		} else {
   1370 			u_usec = pps_ff[2];		/* 1 2 0 */
   1371 			v_usec = pps_ff[1] - pps_ff[0];
   1372 		}
   1373 	}
   1374 
   1375 	/*
   1376 	 * Here the frequency dispersion (stability) is updated. If it
   1377 	 * is less than one-fourth the maximum (MAXFREQ), the frequency
   1378 	 * offset is updated as well, but clamped to the tolerance. It
   1379 	 * will be processed later by the hardclock() routine.
   1380 	 */
   1381 	v_usec = (v_usec >> 1) - pps_stabil;
   1382 	if (v_usec < 0)
   1383 		pps_stabil -= -v_usec >> PPS_AVG;
   1384 	else
   1385 		pps_stabil += v_usec >> PPS_AVG;
   1386 	if (pps_stabil > MAXFREQ >> 2) {
   1387 		pps_stbcnt++;
   1388 		time_status |= STA_PPSWANDER;
   1389 		return;
   1390 	}
   1391 	if (time_status & STA_PPSFREQ) {
   1392 		if (u_usec < 0) {
   1393 			pps_freq -= -u_usec >> PPS_AVG;
   1394 			if (pps_freq < -time_tolerance)
   1395 				pps_freq = -time_tolerance;
   1396 			u_usec = -u_usec;
   1397 		} else {
   1398 			pps_freq += u_usec >> PPS_AVG;
   1399 			if (pps_freq > time_tolerance)
   1400 				pps_freq = time_tolerance;
   1401 		}
   1402 	}
   1403 
   1404 	/*
   1405 	 * Here the calibration interval is adjusted. If the maximum
   1406 	 * time difference is greater than tick / 4, reduce the interval
   1407 	 * by half. If this is not the case for four consecutive
   1408 	 * intervals, double the interval.
   1409 	 */
   1410 	if (u_usec << pps_shift > bigtick >> 2) {
   1411 		pps_intcnt = 0;
   1412 		if (pps_shift > PPS_SHIFT)
   1413 			pps_shift--;
   1414 	} else if (pps_intcnt >= 4) {
   1415 		pps_intcnt = 0;
   1416 		if (pps_shift < PPS_SHIFTMAX)
   1417 			pps_shift++;
   1418 	} else
   1419 		pps_intcnt++;
   1420 }
   1421 #endif /* PPS_SYNC */
   1422 #endif /* NTP  */
   1423