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