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