xref: /onnv-gate/usr/src/uts/common/os/clock.c (revision 2036:6b118cfdcfda)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2036Swentaoy  * Common Development and Distribution License (the "License").
6*2036Swentaoy  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*2036Swentaoy  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/t_lock.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/tuneable.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/systm.h>
380Sstevel@tonic-gate #include <sys/cpuvar.h>
390Sstevel@tonic-gate #include <sys/lgrp.h>
400Sstevel@tonic-gate #include <sys/user.h>
410Sstevel@tonic-gate #include <sys/proc.h>
420Sstevel@tonic-gate #include <sys/callo.h>
430Sstevel@tonic-gate #include <sys/kmem.h>
440Sstevel@tonic-gate #include <sys/var.h>
450Sstevel@tonic-gate #include <sys/cmn_err.h>
460Sstevel@tonic-gate #include <sys/swap.h>
470Sstevel@tonic-gate #include <sys/vmsystm.h>
480Sstevel@tonic-gate #include <sys/class.h>
490Sstevel@tonic-gate #include <sys/time.h>
500Sstevel@tonic-gate #include <sys/debug.h>
510Sstevel@tonic-gate #include <sys/vtrace.h>
520Sstevel@tonic-gate #include <sys/spl.h>
530Sstevel@tonic-gate #include <sys/atomic.h>
540Sstevel@tonic-gate #include <sys/dumphdr.h>
550Sstevel@tonic-gate #include <sys/archsystm.h>
560Sstevel@tonic-gate #include <sys/fs/swapnode.h>
570Sstevel@tonic-gate #include <sys/panic.h>
580Sstevel@tonic-gate #include <sys/disp.h>
590Sstevel@tonic-gate #include <sys/msacct.h>
600Sstevel@tonic-gate #include <sys/mem_cage.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #include <vm/page.h>
630Sstevel@tonic-gate #include <vm/anon.h>
640Sstevel@tonic-gate #include <vm/rm.h>
650Sstevel@tonic-gate #include <sys/cyclic.h>
660Sstevel@tonic-gate #include <sys/cpupart.h>
670Sstevel@tonic-gate #include <sys/rctl.h>
680Sstevel@tonic-gate #include <sys/task.h>
690Sstevel@tonic-gate #include <sys/chip.h>
700Sstevel@tonic-gate #include <sys/sdt.h>
710Sstevel@tonic-gate 
72*2036Swentaoy #ifdef __sparc
73*2036Swentaoy #include <sys/wdt.h>
74*2036Swentaoy #endif
75*2036Swentaoy 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * for NTP support
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate #include <sys/timex.h>
800Sstevel@tonic-gate #include <sys/inttypes.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * clock is called straight from
840Sstevel@tonic-gate  * the real time clock interrupt.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  * Functions:
870Sstevel@tonic-gate  *	reprime clock
880Sstevel@tonic-gate  *	schedule callouts
890Sstevel@tonic-gate  *	maintain date
900Sstevel@tonic-gate  *	jab the scheduler
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate extern kcondvar_t	fsflush_cv;
940Sstevel@tonic-gate extern sysinfo_t	sysinfo;
950Sstevel@tonic-gate extern vminfo_t	vminfo;
960Sstevel@tonic-gate extern int	idleswtch;	/* flag set while idle in pswtch() */
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * high-precision avenrun values.  These are needed to make the
1000Sstevel@tonic-gate  * regular avenrun values accurate.
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate static uint64_t hp_avenrun[3];
1030Sstevel@tonic-gate int	avenrun[3];		/* FSCALED average run queue lengths */
1040Sstevel@tonic-gate time_t	time;	/* time in seconds since 1970 - for compatibility only */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static struct loadavg_s loadavg;
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * Phase/frequency-lock loop (PLL/FLL) definitions
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  * The following variables are read and set by the ntp_adjtime() system
1110Sstevel@tonic-gate  * call.
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  * time_state shows the state of the system clock, with values defined
1140Sstevel@tonic-gate  * in the timex.h header file.
1150Sstevel@tonic-gate  *
1160Sstevel@tonic-gate  * time_status shows the status of the system clock, with bits defined
1170Sstevel@tonic-gate  * in the timex.h header file.
1180Sstevel@tonic-gate  *
1190Sstevel@tonic-gate  * time_offset is used by the PLL/FLL to adjust the system time in small
1200Sstevel@tonic-gate  * increments.
1210Sstevel@tonic-gate  *
1220Sstevel@tonic-gate  * time_constant determines the bandwidth or "stiffness" of the PLL.
1230Sstevel@tonic-gate  *
1240Sstevel@tonic-gate  * time_tolerance determines maximum frequency error or tolerance of the
1250Sstevel@tonic-gate  * CPU clock oscillator and is a property of the architecture; however,
1260Sstevel@tonic-gate  * in principle it could change as result of the presence of external
1270Sstevel@tonic-gate  * discipline signals, for instance.
1280Sstevel@tonic-gate  *
1290Sstevel@tonic-gate  * time_precision is usually equal to the kernel tick variable; however,
1300Sstevel@tonic-gate  * in cases where a precision clock counter or external clock is
1310Sstevel@tonic-gate  * available, the resolution can be much less than this and depend on
1320Sstevel@tonic-gate  * whether the external clock is working or not.
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  * time_maxerror is initialized by a ntp_adjtime() call and increased by
1350Sstevel@tonic-gate  * the kernel once each second to reflect the maximum error bound
1360Sstevel@tonic-gate  * growth.
1370Sstevel@tonic-gate  *
1380Sstevel@tonic-gate  * time_esterror is set and read by the ntp_adjtime() call, but
1390Sstevel@tonic-gate  * otherwise not used by the kernel.
1400Sstevel@tonic-gate  */
1410Sstevel@tonic-gate int32_t time_state = TIME_OK;	/* clock state */
1420Sstevel@tonic-gate int32_t time_status = STA_UNSYNC;	/* clock status bits */
1430Sstevel@tonic-gate int32_t time_offset = 0;		/* time offset (us) */
1440Sstevel@tonic-gate int32_t time_constant = 0;		/* pll time constant */
1450Sstevel@tonic-gate int32_t time_tolerance = MAXFREQ;	/* frequency tolerance (scaled ppm) */
1460Sstevel@tonic-gate int32_t time_precision = 1;	/* clock precision (us) */
1470Sstevel@tonic-gate int32_t time_maxerror = MAXPHASE;	/* maximum error (us) */
1480Sstevel@tonic-gate int32_t time_esterror = MAXPHASE;	/* estimated error (us) */
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * The following variables establish the state of the PLL/FLL and the
1520Sstevel@tonic-gate  * residual time and frequency offset of the local clock. The scale
1530Sstevel@tonic-gate  * factors are defined in the timex.h header file.
1540Sstevel@tonic-gate  *
1550Sstevel@tonic-gate  * time_phase and time_freq are the phase increment and the frequency
1560Sstevel@tonic-gate  * increment, respectively, of the kernel time variable.
1570Sstevel@tonic-gate  *
1580Sstevel@tonic-gate  * time_freq is set via ntp_adjtime() from a value stored in a file when
1590Sstevel@tonic-gate  * the synchronization daemon is first started. Its value is retrieved
1600Sstevel@tonic-gate  * via ntp_adjtime() and written to the file about once per hour by the
1610Sstevel@tonic-gate  * daemon.
1620Sstevel@tonic-gate  *
1630Sstevel@tonic-gate  * time_adj is the adjustment added to the value of tick at each timer
1640Sstevel@tonic-gate  * interrupt and is recomputed from time_phase and time_freq at each
1650Sstevel@tonic-gate  * seconds rollover.
1660Sstevel@tonic-gate  *
1670Sstevel@tonic-gate  * time_reftime is the second's portion of the system time at the last
1680Sstevel@tonic-gate  * call to ntp_adjtime(). It is used to adjust the time_freq variable
1690Sstevel@tonic-gate  * and to increase the time_maxerror as the time since last update
1700Sstevel@tonic-gate  * increases.
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate int32_t time_phase = 0;		/* phase offset (scaled us) */
1730Sstevel@tonic-gate int32_t time_freq = 0;		/* frequency offset (scaled ppm) */
1740Sstevel@tonic-gate int32_t time_adj = 0;		/* tick adjust (scaled 1 / hz) */
1750Sstevel@tonic-gate int32_t time_reftime = 0;		/* time at last adjustment (s) */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate  * The scale factors of the following variables are defined in the
1790Sstevel@tonic-gate  * timex.h header file.
1800Sstevel@tonic-gate  *
1810Sstevel@tonic-gate  * pps_time contains the time at each calibration interval, as read by
1820Sstevel@tonic-gate  * microtime(). pps_count counts the seconds of the calibration
1830Sstevel@tonic-gate  * interval, the duration of which is nominally pps_shift in powers of
1840Sstevel@tonic-gate  * two.
1850Sstevel@tonic-gate  *
1860Sstevel@tonic-gate  * pps_offset is the time offset produced by the time median filter
1870Sstevel@tonic-gate  * pps_tf[], while pps_jitter is the dispersion (jitter) measured by
1880Sstevel@tonic-gate  * this filter.
1890Sstevel@tonic-gate  *
1900Sstevel@tonic-gate  * pps_freq is the frequency offset produced by the frequency median
1910Sstevel@tonic-gate  * filter pps_ff[], while pps_stabil is the dispersion (wander) measured
1920Sstevel@tonic-gate  * by this filter.
1930Sstevel@tonic-gate  *
1940Sstevel@tonic-gate  * pps_usec is latched from a high resolution counter or external clock
1950Sstevel@tonic-gate  * at pps_time. Here we want the hardware counter contents only, not the
1960Sstevel@tonic-gate  * contents plus the time_tv.usec as usual.
1970Sstevel@tonic-gate  *
1980Sstevel@tonic-gate  * pps_valid counts the number of seconds since the last PPS update. It
1990Sstevel@tonic-gate  * is used as a watchdog timer to disable the PPS discipline should the
2000Sstevel@tonic-gate  * PPS signal be lost.
2010Sstevel@tonic-gate  *
2020Sstevel@tonic-gate  * pps_glitch counts the number of seconds since the beginning of an
2030Sstevel@tonic-gate  * offset burst more than tick/2 from current nominal offset. It is used
2040Sstevel@tonic-gate  * mainly to suppress error bursts due to priority conflicts between the
2050Sstevel@tonic-gate  * PPS interrupt and timer interrupt.
2060Sstevel@tonic-gate  *
2070Sstevel@tonic-gate  * pps_intcnt counts the calibration intervals for use in the interval-
2080Sstevel@tonic-gate  * adaptation algorithm. It's just too complicated for words.
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate struct timeval pps_time;	/* kernel time at last interval */
2110Sstevel@tonic-gate int32_t pps_tf[] = {0, 0, 0};	/* pps time offset median filter (us) */
2120Sstevel@tonic-gate int32_t pps_offset = 0;		/* pps time offset (us) */
2130Sstevel@tonic-gate int32_t pps_jitter = MAXTIME;	/* time dispersion (jitter) (us) */
2140Sstevel@tonic-gate int32_t pps_ff[] = {0, 0, 0};	/* pps frequency offset median filter */
2150Sstevel@tonic-gate int32_t pps_freq = 0;		/* frequency offset (scaled ppm) */
2160Sstevel@tonic-gate int32_t pps_stabil = MAXFREQ;	/* frequency dispersion (scaled ppm) */
2170Sstevel@tonic-gate int32_t pps_usec = 0;		/* microsec counter at last interval */
2180Sstevel@tonic-gate int32_t pps_valid = PPS_VALID;	/* pps signal watchdog counter */
2190Sstevel@tonic-gate int32_t pps_glitch = 0;		/* pps signal glitch counter */
2200Sstevel@tonic-gate int32_t pps_count = 0;		/* calibration interval counter (s) */
2210Sstevel@tonic-gate int32_t pps_shift = PPS_SHIFT;	/* interval duration (s) (shift) */
2220Sstevel@tonic-gate int32_t pps_intcnt = 0;		/* intervals at current duration */
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate  * PPS signal quality monitors
2260Sstevel@tonic-gate  *
2270Sstevel@tonic-gate  * pps_jitcnt counts the seconds that have been discarded because the
2280Sstevel@tonic-gate  * jitter measured by the time median filter exceeds the limit MAXTIME
2290Sstevel@tonic-gate  * (100 us).
2300Sstevel@tonic-gate  *
2310Sstevel@tonic-gate  * pps_calcnt counts the frequency calibration intervals, which are
2320Sstevel@tonic-gate  * variable from 4 s to 256 s.
2330Sstevel@tonic-gate  *
2340Sstevel@tonic-gate  * pps_errcnt counts the calibration intervals which have been discarded
2350Sstevel@tonic-gate  * because the wander exceeds the limit MAXFREQ (100 ppm) or where the
2360Sstevel@tonic-gate  * calibration interval jitter exceeds two ticks.
2370Sstevel@tonic-gate  *
2380Sstevel@tonic-gate  * pps_stbcnt counts the calibration intervals that have been discarded
2390Sstevel@tonic-gate  * because the frequency wander exceeds the limit MAXFREQ / 4 (25 us).
2400Sstevel@tonic-gate  */
2410Sstevel@tonic-gate int32_t pps_jitcnt = 0;		/* jitter limit exceeded */
2420Sstevel@tonic-gate int32_t pps_calcnt = 0;		/* calibration intervals */
2430Sstevel@tonic-gate int32_t pps_errcnt = 0;		/* calibration errors */
2440Sstevel@tonic-gate int32_t pps_stbcnt = 0;		/* stability limit exceeded */
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /* The following variables require no explicit locking */
2470Sstevel@tonic-gate volatile clock_t lbolt;		/* time in Hz since last boot */
2480Sstevel@tonic-gate volatile int64_t lbolt64;	/* lbolt64 won't wrap for 2.9 billion yrs */
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate kcondvar_t lbolt_cv;
2510Sstevel@tonic-gate int one_sec = 1; /* turned on once every second */
2520Sstevel@tonic-gate static int fsflushcnt;	/* counter for t_fsflushr */
2530Sstevel@tonic-gate int	dosynctodr = 1;	/* patchable; enable/disable sync to TOD chip */
2540Sstevel@tonic-gate int	tod_needsync = 0;	/* need to sync tod chip with software time */
2550Sstevel@tonic-gate static int tod_broken = 0;	/* clock chip doesn't work */
2560Sstevel@tonic-gate time_t	boot_time = 0;		/* Boot time in seconds since 1970 */
2570Sstevel@tonic-gate cyclic_id_t clock_cyclic;	/* clock()'s cyclic_id */
2580Sstevel@tonic-gate cyclic_id_t deadman_cyclic;	/* deadman()'s cyclic_id */
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate static int lgrp_ticks;		/* counter to schedule lgrp load calcs */
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate  * rechoose_interval_history is used to detect when rechoose_interval's
2640Sstevel@tonic-gate  * value has changed (via hotpatching for example), so that the
2650Sstevel@tonic-gate  * cached values in the cpu structures may be updated.
2660Sstevel@tonic-gate  */
2670Sstevel@tonic-gate static int rechoose_interval_history = RECHOOSE_INTERVAL;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  * for tod fault detection
2710Sstevel@tonic-gate  */
2720Sstevel@tonic-gate #define	TOD_REF_FREQ		((longlong_t)(NANOSEC))
2730Sstevel@tonic-gate #define	TOD_STALL_THRESHOLD	(TOD_REF_FREQ * 3 / 2)
2740Sstevel@tonic-gate #define	TOD_JUMP_THRESHOLD	(TOD_REF_FREQ / 2)
2750Sstevel@tonic-gate #define	TOD_FILTER_N		4
2760Sstevel@tonic-gate #define	TOD_FILTER_SETTLE	(4 * TOD_FILTER_N)
2770Sstevel@tonic-gate static int tod_faulted = TOD_NOFAULT;
2780Sstevel@tonic-gate static int tod_fault_reset_flag = 0;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /* patchable via /etc/system */
2810Sstevel@tonic-gate int tod_validate_enable = 1;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate /*
284950Ssethg  * On non-SPARC systems, TOD validation must be deferred until gethrtime
285950Ssethg  * returns non-zero values (after mach_clkinit's execution).
286950Ssethg  * On SPARC systems, it must be deferred until after hrtime_base
287950Ssethg  * and hres_last_tick are set (in the first invocation of hres_tick).
288950Ssethg  * Since in both cases the prerequisites occur before the invocation of
289950Ssethg  * tod_get() in clock(), the deferment is lifted there.
290950Ssethg  */
291950Ssethg static boolean_t tod_validate_deferred = B_TRUE;
292950Ssethg 
293950Ssethg /*
2940Sstevel@tonic-gate  * tod_fault_table[] must be aligned with
2950Sstevel@tonic-gate  * enum tod_fault_type in systm.h
2960Sstevel@tonic-gate  */
2970Sstevel@tonic-gate static char *tod_fault_table[] = {
2980Sstevel@tonic-gate 	"Reversed",			/* TOD_REVERSED */
2990Sstevel@tonic-gate 	"Stalled",			/* TOD_STALLED */
3000Sstevel@tonic-gate 	"Jumped",			/* TOD_JUMPED */
3010Sstevel@tonic-gate 	"Changed in Clock Rate"		/* TOD_RATECHANGED */
3020Sstevel@tonic-gate 	/*
3030Sstevel@tonic-gate 	 * no strings needed for TOD_NOFAULT
3040Sstevel@tonic-gate 	 */
3050Sstevel@tonic-gate };
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate  * test hook for tod broken detection in tod_validate
3090Sstevel@tonic-gate  */
3100Sstevel@tonic-gate int tod_unit_test = 0;
3110Sstevel@tonic-gate time_t tod_test_injector;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate #define	CLOCK_ADJ_HIST_SIZE	4
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static int	adj_hist_entry;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate int64_t clock_adj_hist[CLOCK_ADJ_HIST_SIZE];
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate static void clock_tick(kthread_t *);
3200Sstevel@tonic-gate static void calcloadavg(int, uint64_t *);
3210Sstevel@tonic-gate static int genloadavg(struct loadavg_s *);
3220Sstevel@tonic-gate static void loadavg_update();
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate void (*cmm_clock_callout)() = NULL;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate #ifdef	KSLICE
3270Sstevel@tonic-gate int kslice = KSLICE;
3280Sstevel@tonic-gate #endif
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate static void
3310Sstevel@tonic-gate clock(void)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate 	kthread_t	*t;
3340Sstevel@tonic-gate 	kmutex_t	*plockp;	/* pointer to thread's process lock */
3350Sstevel@tonic-gate 	int	pinned_intr = 0;
3360Sstevel@tonic-gate 	uint_t	nrunnable, nrunning;
3370Sstevel@tonic-gate 	uint_t	w_io;
3380Sstevel@tonic-gate 	cpu_t	*cp;
3390Sstevel@tonic-gate 	cpupart_t *cpupart;
3400Sstevel@tonic-gate 	int	exiting;
3410Sstevel@tonic-gate 	extern void set_anoninfo();
3420Sstevel@tonic-gate 	extern	void	set_freemem();
3430Sstevel@tonic-gate 	void	(*funcp)();
3440Sstevel@tonic-gate 	int32_t ltemp;
3450Sstevel@tonic-gate 	int64_t lltemp;
3460Sstevel@tonic-gate 	int s;
3470Sstevel@tonic-gate 	int do_lgrp_load;
3480Sstevel@tonic-gate 	int rechoose_update = 0;
3490Sstevel@tonic-gate 	int rechoose;
3500Sstevel@tonic-gate 	int i;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	if (panicstr)
3530Sstevel@tonic-gate 		return;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	set_anoninfo();
3560Sstevel@tonic-gate 	/*
3570Sstevel@tonic-gate 	 * Make sure that 'freemem' do not drift too far from the truth
3580Sstevel@tonic-gate 	 */
3590Sstevel@tonic-gate 	set_freemem();
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	/*
3630Sstevel@tonic-gate 	 * Before the section which is repeated is executed, we do
3640Sstevel@tonic-gate 	 * the time delta processing which occurs every clock tick
3650Sstevel@tonic-gate 	 *
3660Sstevel@tonic-gate 	 * There is additional processing which happens every time
3670Sstevel@tonic-gate 	 * the nanosecond counter rolls over which is described
3680Sstevel@tonic-gate 	 * below - see the section which begins with : if (one_sec)
3690Sstevel@tonic-gate 	 *
3700Sstevel@tonic-gate 	 * This section marks the beginning of the precision-kernel
3710Sstevel@tonic-gate 	 * code fragment.
3720Sstevel@tonic-gate 	 *
3730Sstevel@tonic-gate 	 * First, compute the phase adjustment. If the low-order bits
3740Sstevel@tonic-gate 	 * (time_phase) of the update overflow, bump the higher order
3750Sstevel@tonic-gate 	 * bits (time_update).
3760Sstevel@tonic-gate 	 */
3770Sstevel@tonic-gate 	time_phase += time_adj;
3780Sstevel@tonic-gate 	if (time_phase <= -FINEUSEC) {
3790Sstevel@tonic-gate 		ltemp = -time_phase / SCALE_PHASE;
3800Sstevel@tonic-gate 		time_phase += ltemp * SCALE_PHASE;
3810Sstevel@tonic-gate 		s = hr_clock_lock();
3820Sstevel@tonic-gate 		timedelta -= ltemp * (NANOSEC/MICROSEC);
3830Sstevel@tonic-gate 		hr_clock_unlock(s);
3840Sstevel@tonic-gate 	} else if (time_phase >= FINEUSEC) {
3850Sstevel@tonic-gate 		ltemp = time_phase / SCALE_PHASE;
3860Sstevel@tonic-gate 		time_phase -= ltemp * SCALE_PHASE;
3870Sstevel@tonic-gate 		s = hr_clock_lock();
3880Sstevel@tonic-gate 		timedelta += ltemp * (NANOSEC/MICROSEC);
3890Sstevel@tonic-gate 		hr_clock_unlock(s);
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	/*
3930Sstevel@tonic-gate 	 * End of precision-kernel code fragment which is processed
3940Sstevel@tonic-gate 	 * every timer interrupt.
3950Sstevel@tonic-gate 	 *
3960Sstevel@tonic-gate 	 * Continue with the interrupt processing as scheduled.
3970Sstevel@tonic-gate 	 *
3980Sstevel@tonic-gate 	 * Did we pin another interrupt thread?  Need to check this before
3990Sstevel@tonic-gate 	 * grabbing any adaptive locks, since if we block on a lock the
4000Sstevel@tonic-gate 	 * pinned thread could escape.  Note that this is just a heuristic;
4010Sstevel@tonic-gate 	 * if we take multiple laps though clock() without returning from
4020Sstevel@tonic-gate 	 * the interrupt because we have another clock tick pending, then
4030Sstevel@tonic-gate 	 * the pinned interrupt could be released by one of the previous
4040Sstevel@tonic-gate 	 * laps.  The only consequence is that the CPU will be counted as
4050Sstevel@tonic-gate 	 * in idle (or wait) state once the pinned interrupt is released.
4060Sstevel@tonic-gate 	 * Since this accounting is inaccurate by nature, this isn't a big
4070Sstevel@tonic-gate 	 * deal --- but we should try to get it right in the common case
4080Sstevel@tonic-gate 	 * where we only call clock() once per interrupt.
4090Sstevel@tonic-gate 	 */
4100Sstevel@tonic-gate 	if (curthread->t_intr != NULL)
4110Sstevel@tonic-gate 		pinned_intr = (curthread->t_intr->t_flag & T_INTR_THREAD);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	/*
4140Sstevel@tonic-gate 	 * Count the number of runnable threads and the number waiting
4150Sstevel@tonic-gate 	 * for some form of I/O to complete -- gets added to
4160Sstevel@tonic-gate 	 * sysinfo.waiting.  To know the state of the system, must add
4170Sstevel@tonic-gate 	 * wait counts from all CPUs.  Also add up the per-partition
4180Sstevel@tonic-gate 	 * statistics.
4190Sstevel@tonic-gate 	 */
4200Sstevel@tonic-gate 	w_io = 0;
4210Sstevel@tonic-gate 	nrunnable = 0;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	/*
4240Sstevel@tonic-gate 	 * keep track of when to update lgrp/part loads
4250Sstevel@tonic-gate 	 */
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	do_lgrp_load = 0;
4280Sstevel@tonic-gate 	if (lgrp_ticks++ >= hz / 10) {
4290Sstevel@tonic-gate 		lgrp_ticks = 0;
4300Sstevel@tonic-gate 		do_lgrp_load = 1;
4310Sstevel@tonic-gate 	}
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	/*
4340Sstevel@tonic-gate 	 * The dispatcher tunable rechoose_interval may be hot-patched.
4350Sstevel@tonic-gate 	 * Note if it has a new value. If so, the effective rechoose_interval
4360Sstevel@tonic-gate 	 * cached in the cpu structures needs to be updated.
4370Sstevel@tonic-gate 	 * If needed we'll do this during the walk of the cpu_list below.
4380Sstevel@tonic-gate 	 */
4390Sstevel@tonic-gate 	if (rechoose_interval != rechoose_interval_history) {
4400Sstevel@tonic-gate 		rechoose_interval_history = rechoose_interval;
4410Sstevel@tonic-gate 		rechoose_update = 1;
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if (one_sec)
4450Sstevel@tonic-gate 		loadavg_update();
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * First count the threads waiting on kpreempt queues in each
4500Sstevel@tonic-gate 	 * CPU partition.
4510Sstevel@tonic-gate 	 */
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	cpupart = cp_list_head;
4540Sstevel@tonic-gate 	do {
4550Sstevel@tonic-gate 		uint_t cpupart_nrunnable = cpupart->cp_kp_queue.disp_nrunnable;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		cpupart->cp_updates++;
4580Sstevel@tonic-gate 		nrunnable += cpupart_nrunnable;
4590Sstevel@tonic-gate 		cpupart->cp_nrunnable_cum += cpupart_nrunnable;
4600Sstevel@tonic-gate 		if (one_sec) {
4610Sstevel@tonic-gate 			cpupart->cp_nrunning = 0;
4620Sstevel@tonic-gate 			cpupart->cp_nrunnable = cpupart_nrunnable;
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 	} while ((cpupart = cpupart->cp_next) != cp_list_head);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	/* Now count the per-CPU statistics. */
4680Sstevel@tonic-gate 	cp = cpu_list;
4690Sstevel@tonic-gate 	do {
4700Sstevel@tonic-gate 		uint_t cpu_nrunnable = cp->cpu_disp->disp_nrunnable;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 		nrunnable += cpu_nrunnable;
4730Sstevel@tonic-gate 		cpupart = cp->cpu_part;
4740Sstevel@tonic-gate 		cpupart->cp_nrunnable_cum += cpu_nrunnable;
4750Sstevel@tonic-gate 		if (one_sec)
4760Sstevel@tonic-gate 			cpupart->cp_nrunnable += cpu_nrunnable;
4770Sstevel@tonic-gate 		if (do_lgrp_load &&
4780Sstevel@tonic-gate 		    (cp->cpu_flags & CPU_EXISTS)) {
4790Sstevel@tonic-gate 			/*
4800Sstevel@tonic-gate 			 * When updating the lgroup's load average,
4810Sstevel@tonic-gate 			 * account for the thread running on the CPU.
4820Sstevel@tonic-gate 			 * If the CPU is the current one, then we need
4830Sstevel@tonic-gate 			 * to account for the underlying thread which
4840Sstevel@tonic-gate 			 * got the clock interrupt not the thread that is
4850Sstevel@tonic-gate 			 * handling the interrupt and caculating the load
4860Sstevel@tonic-gate 			 * average
4870Sstevel@tonic-gate 			 */
4880Sstevel@tonic-gate 			t = cp->cpu_thread;
4890Sstevel@tonic-gate 			if (CPU == cp)
4900Sstevel@tonic-gate 				t = t->t_intr;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 			/*
4930Sstevel@tonic-gate 			 * Account for the load average for this thread if
4940Sstevel@tonic-gate 			 * it isn't the idle thread or it is on the interrupt
4950Sstevel@tonic-gate 			 * stack and not the current CPU handling the clock
4960Sstevel@tonic-gate 			 * interrupt
4970Sstevel@tonic-gate 			 */
4980Sstevel@tonic-gate 			if ((t && t != cp->cpu_idle_thread) || (CPU != cp &&
4990Sstevel@tonic-gate 			    CPU_ON_INTR(cp))) {
5000Sstevel@tonic-gate 				if (t->t_lpl == cp->cpu_lpl) {
5010Sstevel@tonic-gate 					/* local thread */
5020Sstevel@tonic-gate 					cpu_nrunnable++;
5030Sstevel@tonic-gate 				} else {
5040Sstevel@tonic-gate 					/*
5050Sstevel@tonic-gate 					 * This is a remote thread, charge it
5060Sstevel@tonic-gate 					 * against its home lgroup.  Note that
5070Sstevel@tonic-gate 					 * we notice that a thread is remote
5080Sstevel@tonic-gate 					 * only if it's currently executing.
5090Sstevel@tonic-gate 					 * This is a reasonable approximation,
5100Sstevel@tonic-gate 					 * since queued remote threads are rare.
5110Sstevel@tonic-gate 					 * Note also that if we didn't charge
5120Sstevel@tonic-gate 					 * it to its home lgroup, remote
5130Sstevel@tonic-gate 					 * execution would often make a system
5140Sstevel@tonic-gate 					 * appear balanced even though it was
5150Sstevel@tonic-gate 					 * not, and thread placement/migration
5160Sstevel@tonic-gate 					 * would often not be done correctly.
5170Sstevel@tonic-gate 					 */
5180Sstevel@tonic-gate 					lgrp_loadavg(t->t_lpl,
5190Sstevel@tonic-gate 					    LGRP_LOADAVG_IN_THREAD_MAX, 0);
5200Sstevel@tonic-gate 				}
5210Sstevel@tonic-gate 			}
5220Sstevel@tonic-gate 			lgrp_loadavg(cp->cpu_lpl,
5230Sstevel@tonic-gate 			    cpu_nrunnable * LGRP_LOADAVG_IN_THREAD_MAX, 1);
5240Sstevel@tonic-gate 		}
5250Sstevel@tonic-gate 		/*
5260Sstevel@tonic-gate 		 * The platform may define a per physical processor
5270Sstevel@tonic-gate 		 * adjustment of rechoose_interval. The effective
5280Sstevel@tonic-gate 		 * (base + adjustment) rechoose_interval is cached
5290Sstevel@tonic-gate 		 * in the cpu structures for efficiency. Above we detect
5300Sstevel@tonic-gate 		 * if the cached values need updating, and here is where
5310Sstevel@tonic-gate 		 * the update happens.
5320Sstevel@tonic-gate 		 */
5330Sstevel@tonic-gate 		if (rechoose_update) {
5340Sstevel@tonic-gate 			rechoose = rechoose_interval +
5350Sstevel@tonic-gate 				cp->cpu_chip->chip_rechoose_adj;
5360Sstevel@tonic-gate 			cp->cpu_rechoose = (rechoose < 0) ? 0 : rechoose;
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	/*
5410Sstevel@tonic-gate 	 * Do tick processing for all the active threads running in
5420Sstevel@tonic-gate 	 * the system.
5430Sstevel@tonic-gate 	 */
5440Sstevel@tonic-gate 	cp = cpu_list;
5450Sstevel@tonic-gate 	nrunning = 0;
5460Sstevel@tonic-gate 	do {
5470Sstevel@tonic-gate 		klwp_id_t lwp;
5480Sstevel@tonic-gate 		int intr;
5490Sstevel@tonic-gate 		int thread_away;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 		/*
5520Sstevel@tonic-gate 		 * Don't do any tick processing on CPUs that
5530Sstevel@tonic-gate 		 * aren't even in the system or aren't up yet.
5540Sstevel@tonic-gate 		 */
5550Sstevel@tonic-gate 		if ((cp->cpu_flags & CPU_EXISTS) == 0) {
5560Sstevel@tonic-gate 			continue;
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 		/*
5600Sstevel@tonic-gate 		 * The locking here is rather tricky.  We use
5610Sstevel@tonic-gate 		 * thread_free_lock to keep the currently running
5620Sstevel@tonic-gate 		 * thread from being freed or recycled while we're
5630Sstevel@tonic-gate 		 * looking at it.  We can then check if the thread
5640Sstevel@tonic-gate 		 * is exiting and get the appropriate p_lock if it
5650Sstevel@tonic-gate 		 * is not.  We have to be careful, though, because
5660Sstevel@tonic-gate 		 * the _process_ can still be freed while we're
5670Sstevel@tonic-gate 		 * holding thread_free_lock.  To avoid touching the
5680Sstevel@tonic-gate 		 * proc structure we put a pointer to the p_lock in the
5690Sstevel@tonic-gate 		 * thread structure.  The p_lock is persistent so we
5700Sstevel@tonic-gate 		 * can acquire it even if the process is gone.  At that
5710Sstevel@tonic-gate 		 * point we can check (again) if the thread is exiting
5720Sstevel@tonic-gate 		 * and either drop the lock or do the tick processing.
5730Sstevel@tonic-gate 		 */
5740Sstevel@tonic-gate 		mutex_enter(&thread_free_lock);
5750Sstevel@tonic-gate 		/*
5760Sstevel@tonic-gate 		 * We cannot hold the cpu_lock to prevent the
5770Sstevel@tonic-gate 		 * cpu_list from changing in the clock interrupt.
5780Sstevel@tonic-gate 		 * As long as we don't block (or don't get pre-empted)
5790Sstevel@tonic-gate 		 * the cpu_list will not change (all threads are paused
5800Sstevel@tonic-gate 		 * before list modification). If the list does change
5810Sstevel@tonic-gate 		 * any deleted cpu structures will remain with cpu_next
5820Sstevel@tonic-gate 		 * set to NULL, hence the following test.
5830Sstevel@tonic-gate 		 */
5840Sstevel@tonic-gate 		if (cp->cpu_next == NULL) {
5850Sstevel@tonic-gate 			mutex_exit(&thread_free_lock);
5860Sstevel@tonic-gate 			break;
5870Sstevel@tonic-gate 		}
5880Sstevel@tonic-gate 		t = cp->cpu_thread;	/* Current running thread */
5890Sstevel@tonic-gate 		if (CPU == cp) {
5900Sstevel@tonic-gate 			/*
5910Sstevel@tonic-gate 			 * 't' will be the clock interrupt thread on this
5920Sstevel@tonic-gate 			 * CPU.  Use the pinned thread (if any) on this CPU
5930Sstevel@tonic-gate 			 * as the target of the clock tick.  If we pinned
5940Sstevel@tonic-gate 			 * an interrupt, though, just keep using the clock
5950Sstevel@tonic-gate 			 * interrupt thread since the formerly pinned one
5960Sstevel@tonic-gate 			 * may have gone away.  One interrupt thread is as
5970Sstevel@tonic-gate 			 * good as another, and this means we don't have
5980Sstevel@tonic-gate 			 * to continue to check pinned_intr in subsequent
5990Sstevel@tonic-gate 			 * code.
6000Sstevel@tonic-gate 			 */
6010Sstevel@tonic-gate 			ASSERT(t == curthread);
6020Sstevel@tonic-gate 			if (t->t_intr != NULL && !pinned_intr)
6030Sstevel@tonic-gate 				t = t->t_intr;
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 		intr = t->t_flag & T_INTR_THREAD;
6070Sstevel@tonic-gate 		lwp = ttolwp(t);
6080Sstevel@tonic-gate 		if (lwp == NULL || (t->t_proc_flag & TP_LWPEXIT) || intr) {
6090Sstevel@tonic-gate 			/*
6100Sstevel@tonic-gate 			 * Thread is exiting (or uninteresting) so don't
6110Sstevel@tonic-gate 			 * do tick processing or grab p_lock.  Once we
6120Sstevel@tonic-gate 			 * drop thread_free_lock we can't look inside the
6130Sstevel@tonic-gate 			 * thread or lwp structure, since the thread may
6140Sstevel@tonic-gate 			 * have gone away.
6150Sstevel@tonic-gate 			 */
6160Sstevel@tonic-gate 			exiting = 1;
6170Sstevel@tonic-gate 		} else {
6180Sstevel@tonic-gate 			/*
6190Sstevel@tonic-gate 			 * OK, try to grab the process lock.  See
6200Sstevel@tonic-gate 			 * comments above for why we're not using
6210Sstevel@tonic-gate 			 * ttoproc(t)->p_lockp here.
6220Sstevel@tonic-gate 			 */
6230Sstevel@tonic-gate 			plockp = t->t_plockp;
6240Sstevel@tonic-gate 			mutex_enter(plockp);
6250Sstevel@tonic-gate 			/* See above comment. */
6260Sstevel@tonic-gate 			if (cp->cpu_next == NULL) {
6270Sstevel@tonic-gate 				mutex_exit(plockp);
6280Sstevel@tonic-gate 				mutex_exit(&thread_free_lock);
6290Sstevel@tonic-gate 				break;
6300Sstevel@tonic-gate 			}
6310Sstevel@tonic-gate 			/*
6320Sstevel@tonic-gate 			 * The thread may have exited between when we
6330Sstevel@tonic-gate 			 * checked above, and when we got the p_lock.
6340Sstevel@tonic-gate 			 */
6350Sstevel@tonic-gate 			if (t->t_proc_flag & TP_LWPEXIT) {
6360Sstevel@tonic-gate 				mutex_exit(plockp);
6370Sstevel@tonic-gate 				exiting = 1;
6380Sstevel@tonic-gate 			} else {
6390Sstevel@tonic-gate 				exiting = 0;
6400Sstevel@tonic-gate 			}
6410Sstevel@tonic-gate 		}
6420Sstevel@tonic-gate 		/*
6430Sstevel@tonic-gate 		 * Either we have the p_lock for the thread's process,
6440Sstevel@tonic-gate 		 * or we don't care about the thread structure any more.
6450Sstevel@tonic-gate 		 * Either way we can drop thread_free_lock.
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		mutex_exit(&thread_free_lock);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		/*
6500Sstevel@tonic-gate 		 * Update user, system, and idle cpu times.
6510Sstevel@tonic-gate 		 */
6520Sstevel@tonic-gate 		if (one_sec) {
6530Sstevel@tonic-gate 			nrunning++;
6540Sstevel@tonic-gate 			cp->cpu_part->cp_nrunning++;
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 		/*
6570Sstevel@tonic-gate 		 * If we haven't done tick processing for this
6580Sstevel@tonic-gate 		 * lwp, then do it now. Since we don't hold the
6590Sstevel@tonic-gate 		 * lwp down on a CPU it can migrate and show up
6600Sstevel@tonic-gate 		 * more than once, hence the lbolt check.
6610Sstevel@tonic-gate 		 *
6620Sstevel@tonic-gate 		 * Also, make sure that it's okay to perform the
6630Sstevel@tonic-gate 		 * tick processing before calling clock_tick.
6640Sstevel@tonic-gate 		 * Setting thread_away to a TRUE value (ie. not 0)
6650Sstevel@tonic-gate 		 * results in tick processing not being performed for
6660Sstevel@tonic-gate 		 * that thread.  Or, in other words, keeps the thread
6670Sstevel@tonic-gate 		 * away from clock_tick processing.
6680Sstevel@tonic-gate 		 */
6690Sstevel@tonic-gate 		thread_away = ((cp->cpu_flags & CPU_QUIESCED) ||
6700Sstevel@tonic-gate 		    CPU_ON_INTR(cp) || intr ||
6710Sstevel@tonic-gate 		    (cp->cpu_dispthread == cp->cpu_idle_thread) || exiting);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		if ((!thread_away) && (lbolt - t->t_lbolt != 0)) {
6740Sstevel@tonic-gate 			t->t_lbolt = lbolt;
6750Sstevel@tonic-gate 			clock_tick(t);
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate #ifdef KSLICE
6790Sstevel@tonic-gate 		/*
6800Sstevel@tonic-gate 		 * Ah what the heck, give this kid a taste of the real
6810Sstevel@tonic-gate 		 * world and yank the rug out from under it.
6820Sstevel@tonic-gate 		 * But, only if we are running UniProcessor.
6830Sstevel@tonic-gate 		 */
6840Sstevel@tonic-gate 		if ((kslice) && (ncpus == 1)) {
6850Sstevel@tonic-gate 			aston(t);
6860Sstevel@tonic-gate 			cp->cpu_runrun = 1;
6870Sstevel@tonic-gate 			cp->cpu_kprunrun = 1;
6880Sstevel@tonic-gate 		}
6890Sstevel@tonic-gate #endif
6900Sstevel@tonic-gate 		if (!exiting)
6910Sstevel@tonic-gate 			mutex_exit(plockp);
6920Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	/*
6950Sstevel@tonic-gate 	 * bump time in ticks
6960Sstevel@tonic-gate 	 *
6970Sstevel@tonic-gate 	 * We rely on there being only one clock thread and hence
6980Sstevel@tonic-gate 	 * don't need a lock to protect lbolt.
6990Sstevel@tonic-gate 	 */
7000Sstevel@tonic-gate 	lbolt++;
7010Sstevel@tonic-gate 	atomic_add_64((uint64_t *)&lbolt64, (int64_t)1);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	/*
7040Sstevel@tonic-gate 	 * Check for a callout that needs be called from the clock
7050Sstevel@tonic-gate 	 * thread to support the membership protocol in a clustered
7060Sstevel@tonic-gate 	 * system.  Copy the function pointer so that we can reset
7070Sstevel@tonic-gate 	 * this to NULL if needed.
7080Sstevel@tonic-gate 	 */
7090Sstevel@tonic-gate 	if ((funcp = cmm_clock_callout) != NULL)
7100Sstevel@tonic-gate 		(*funcp)();
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	/*
7130Sstevel@tonic-gate 	 * Wakeup the cageout thread waiters once per second.
7140Sstevel@tonic-gate 	 */
7150Sstevel@tonic-gate 	if (one_sec)
7160Sstevel@tonic-gate 		kcage_tick();
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	/*
7190Sstevel@tonic-gate 	 * Schedule timeout() requests if any are due at this time.
7200Sstevel@tonic-gate 	 */
7210Sstevel@tonic-gate 	callout_schedule();
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	if (one_sec) {
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		int drift, absdrift;
7260Sstevel@tonic-gate 		timestruc_t tod;
7270Sstevel@tonic-gate 		int s;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		/*
7300Sstevel@tonic-gate 		 * Beginning of precision-kernel code fragment executed
7310Sstevel@tonic-gate 		 * every second.
7320Sstevel@tonic-gate 		 *
7330Sstevel@tonic-gate 		 * On rollover of the second the phase adjustment to be
7340Sstevel@tonic-gate 		 * used for the next second is calculated.  Also, the
7350Sstevel@tonic-gate 		 * maximum error is increased by the tolerance.  If the
7360Sstevel@tonic-gate 		 * PPS frequency discipline code is present, the phase is
7370Sstevel@tonic-gate 		 * increased to compensate for the CPU clock oscillator
7380Sstevel@tonic-gate 		 * frequency error.
7390Sstevel@tonic-gate 		 *
7400Sstevel@tonic-gate 		 * On a 32-bit machine and given parameters in the timex.h
7410Sstevel@tonic-gate 		 * header file, the maximum phase adjustment is +-512 ms
7420Sstevel@tonic-gate 		 * and maximum frequency offset is (a tad less than)
7430Sstevel@tonic-gate 		 * +-512 ppm. On a 64-bit machine, you shouldn't need to ask.
7440Sstevel@tonic-gate 		 */
7450Sstevel@tonic-gate 		time_maxerror += time_tolerance / SCALE_USEC;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		/*
7480Sstevel@tonic-gate 		 * Leap second processing. If in leap-insert state at
7490Sstevel@tonic-gate 		 * the end of the day, the system clock is set back one
7500Sstevel@tonic-gate 		 * second; if in leap-delete state, the system clock is
7510Sstevel@tonic-gate 		 * set ahead one second. The microtime() routine or
7520Sstevel@tonic-gate 		 * external clock driver will insure that reported time
7530Sstevel@tonic-gate 		 * is always monotonic. The ugly divides should be
7540Sstevel@tonic-gate 		 * replaced.
7550Sstevel@tonic-gate 		 */
7560Sstevel@tonic-gate 		switch (time_state) {
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 		case TIME_OK:
7590Sstevel@tonic-gate 			if (time_status & STA_INS)
7600Sstevel@tonic-gate 				time_state = TIME_INS;
7610Sstevel@tonic-gate 			else if (time_status & STA_DEL)
7620Sstevel@tonic-gate 				time_state = TIME_DEL;
7630Sstevel@tonic-gate 			break;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		case TIME_INS:
7660Sstevel@tonic-gate 			if (hrestime.tv_sec % 86400 == 0) {
7670Sstevel@tonic-gate 				s = hr_clock_lock();
7680Sstevel@tonic-gate 				hrestime.tv_sec--;
7690Sstevel@tonic-gate 				hr_clock_unlock(s);
7700Sstevel@tonic-gate 				time_state = TIME_OOP;
7710Sstevel@tonic-gate 			}
7720Sstevel@tonic-gate 			break;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 		case TIME_DEL:
7750Sstevel@tonic-gate 			if ((hrestime.tv_sec + 1) % 86400 == 0) {
7760Sstevel@tonic-gate 				s = hr_clock_lock();
7770Sstevel@tonic-gate 				hrestime.tv_sec++;
7780Sstevel@tonic-gate 				hr_clock_unlock(s);
7790Sstevel@tonic-gate 				time_state = TIME_WAIT;
7800Sstevel@tonic-gate 			}
7810Sstevel@tonic-gate 			break;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 		case TIME_OOP:
7840Sstevel@tonic-gate 			time_state = TIME_WAIT;
7850Sstevel@tonic-gate 			break;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		case TIME_WAIT:
7880Sstevel@tonic-gate 			if (!(time_status & (STA_INS | STA_DEL)))
7890Sstevel@tonic-gate 				time_state = TIME_OK;
7900Sstevel@tonic-gate 		default:
7910Sstevel@tonic-gate 			break;
7920Sstevel@tonic-gate 		}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 		/*
7950Sstevel@tonic-gate 		 * Compute the phase adjustment for the next second. In
7960Sstevel@tonic-gate 		 * PLL mode, the offset is reduced by a fixed factor
7970Sstevel@tonic-gate 		 * times the time constant. In FLL mode the offset is
7980Sstevel@tonic-gate 		 * used directly. In either mode, the maximum phase
7990Sstevel@tonic-gate 		 * adjustment for each second is clamped so as to spread
8000Sstevel@tonic-gate 		 * the adjustment over not more than the number of
8010Sstevel@tonic-gate 		 * seconds between updates.
8020Sstevel@tonic-gate 		 */
8030Sstevel@tonic-gate 		if (time_offset == 0)
8040Sstevel@tonic-gate 			time_adj = 0;
8050Sstevel@tonic-gate 		else if (time_offset < 0) {
8060Sstevel@tonic-gate 			lltemp = -time_offset;
8070Sstevel@tonic-gate 			if (!(time_status & STA_FLL)) {
8080Sstevel@tonic-gate 				if ((1 << time_constant) >= SCALE_KG)
8090Sstevel@tonic-gate 					lltemp *= (1 << time_constant) /
8100Sstevel@tonic-gate 					    SCALE_KG;
8110Sstevel@tonic-gate 				else
8120Sstevel@tonic-gate 					lltemp = (lltemp / SCALE_KG) >>
8130Sstevel@tonic-gate 					    time_constant;
8140Sstevel@tonic-gate 			}
8150Sstevel@tonic-gate 			if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
8160Sstevel@tonic-gate 				lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
8170Sstevel@tonic-gate 			time_offset += lltemp;
8180Sstevel@tonic-gate 			time_adj = -(lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
8190Sstevel@tonic-gate 		} else {
8200Sstevel@tonic-gate 			lltemp = time_offset;
8210Sstevel@tonic-gate 			if (!(time_status & STA_FLL)) {
8220Sstevel@tonic-gate 				if ((1 << time_constant) >= SCALE_KG)
8230Sstevel@tonic-gate 					lltemp *= (1 << time_constant) /
8240Sstevel@tonic-gate 					    SCALE_KG;
8250Sstevel@tonic-gate 				else
8260Sstevel@tonic-gate 					lltemp = (lltemp / SCALE_KG) >>
8270Sstevel@tonic-gate 					    time_constant;
8280Sstevel@tonic-gate 			}
8290Sstevel@tonic-gate 			if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
8300Sstevel@tonic-gate 				lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
8310Sstevel@tonic-gate 			time_offset -= lltemp;
8320Sstevel@tonic-gate 			time_adj = (lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
8330Sstevel@tonic-gate 		}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 		/*
8360Sstevel@tonic-gate 		 * Compute the frequency estimate and additional phase
8370Sstevel@tonic-gate 		 * adjustment due to frequency error for the next
8380Sstevel@tonic-gate 		 * second. When the PPS signal is engaged, gnaw on the
8390Sstevel@tonic-gate 		 * watchdog counter and update the frequency computed by
8400Sstevel@tonic-gate 		 * the pll and the PPS signal.
8410Sstevel@tonic-gate 		 */
8420Sstevel@tonic-gate 		pps_valid++;
8430Sstevel@tonic-gate 		if (pps_valid == PPS_VALID) {
8440Sstevel@tonic-gate 			pps_jitter = MAXTIME;
8450Sstevel@tonic-gate 			pps_stabil = MAXFREQ;
8460Sstevel@tonic-gate 			time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
8470Sstevel@tonic-gate 			    STA_PPSWANDER | STA_PPSERROR);
8480Sstevel@tonic-gate 		}
8490Sstevel@tonic-gate 		lltemp = time_freq + pps_freq;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 		if (lltemp)
8520Sstevel@tonic-gate 			time_adj += (lltemp * SCALE_PHASE) / (SCALE_USEC * hz);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 		/*
8550Sstevel@tonic-gate 		 * End of precision kernel-code fragment
8560Sstevel@tonic-gate 		 *
8570Sstevel@tonic-gate 		 * The section below should be modified if we are planning
8580Sstevel@tonic-gate 		 * to use NTP for synchronization.
8590Sstevel@tonic-gate 		 *
8600Sstevel@tonic-gate 		 * Note: the clock synchronization code now assumes
8610Sstevel@tonic-gate 		 * the following:
8620Sstevel@tonic-gate 		 *   - if dosynctodr is 1, then compute the drift between
8630Sstevel@tonic-gate 		 *	the tod chip and software time and adjust one or
8640Sstevel@tonic-gate 		 *	the other depending on the circumstances
8650Sstevel@tonic-gate 		 *
8660Sstevel@tonic-gate 		 *   - if dosynctodr is 0, then the tod chip is independent
8670Sstevel@tonic-gate 		 *	of the software clock and should not be adjusted,
8680Sstevel@tonic-gate 		 *	but allowed to free run.  this allows NTP to sync.
8690Sstevel@tonic-gate 		 *	hrestime without any interference from the tod chip.
8700Sstevel@tonic-gate 		 */
8710Sstevel@tonic-gate 
872950Ssethg 		tod_validate_deferred = B_FALSE;
8730Sstevel@tonic-gate 		mutex_enter(&tod_lock);
8740Sstevel@tonic-gate 		tod = tod_get();
8750Sstevel@tonic-gate 		drift = tod.tv_sec - hrestime.tv_sec;
8760Sstevel@tonic-gate 		absdrift = (drift >= 0) ? drift : -drift;
8770Sstevel@tonic-gate 		if (tod_needsync || absdrift > 1) {
8780Sstevel@tonic-gate 			int s;
8790Sstevel@tonic-gate 			if (absdrift > 2) {
8800Sstevel@tonic-gate 				if (!tod_broken && tod_faulted == TOD_NOFAULT) {
8810Sstevel@tonic-gate 					s = hr_clock_lock();
8820Sstevel@tonic-gate 					hrestime = tod;
8830Sstevel@tonic-gate 					membar_enter();	/* hrestime visible */
8840Sstevel@tonic-gate 					timedelta = 0;
8850Sstevel@tonic-gate 					timechanged++;
8860Sstevel@tonic-gate 					tod_needsync = 0;
8870Sstevel@tonic-gate 					hr_clock_unlock(s);
8880Sstevel@tonic-gate 				}
8890Sstevel@tonic-gate 			} else {
8900Sstevel@tonic-gate 				if (tod_needsync || !dosynctodr) {
8910Sstevel@tonic-gate 					gethrestime(&tod);
8920Sstevel@tonic-gate 					tod_set(tod);
8930Sstevel@tonic-gate 					s = hr_clock_lock();
8940Sstevel@tonic-gate 					if (timedelta == 0)
8950Sstevel@tonic-gate 						tod_needsync = 0;
8960Sstevel@tonic-gate 					hr_clock_unlock(s);
8970Sstevel@tonic-gate 				} else {
8980Sstevel@tonic-gate 					/*
8990Sstevel@tonic-gate 					 * If the drift is 2 seconds on the
9000Sstevel@tonic-gate 					 * money, then the TOD is adjusting
9010Sstevel@tonic-gate 					 * the clock;  record that.
9020Sstevel@tonic-gate 					 */
9030Sstevel@tonic-gate 					clock_adj_hist[adj_hist_entry++ %
9040Sstevel@tonic-gate 					    CLOCK_ADJ_HIST_SIZE] = lbolt64;
9050Sstevel@tonic-gate 					s = hr_clock_lock();
9060Sstevel@tonic-gate 					timedelta = (int64_t)drift*NANOSEC;
9070Sstevel@tonic-gate 					hr_clock_unlock(s);
9080Sstevel@tonic-gate 				}
9090Sstevel@tonic-gate 			}
9100Sstevel@tonic-gate 		}
9110Sstevel@tonic-gate 		one_sec = 0;
9120Sstevel@tonic-gate 		time = gethrestime_sec();  /* for crusty old kmem readers */
9130Sstevel@tonic-gate 		mutex_exit(&tod_lock);
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 		/*
9160Sstevel@tonic-gate 		 * Some drivers still depend on this... XXX
9170Sstevel@tonic-gate 		 */
9180Sstevel@tonic-gate 		cv_broadcast(&lbolt_cv);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		sysinfo.updates++;
9210Sstevel@tonic-gate 		vminfo.freemem += freemem;
9220Sstevel@tonic-gate 		{
9230Sstevel@tonic-gate 			pgcnt_t maxswap, resv, free;
9240Sstevel@tonic-gate 			pgcnt_t avail =
9250Sstevel@tonic-gate 			    MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 			maxswap = k_anoninfo.ani_mem_resv
9280Sstevel@tonic-gate 					+ k_anoninfo.ani_max +avail;
9290Sstevel@tonic-gate 			free = k_anoninfo.ani_free + avail;
9300Sstevel@tonic-gate 			resv = k_anoninfo.ani_phys_resv +
9310Sstevel@tonic-gate 			    k_anoninfo.ani_mem_resv;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 			vminfo.swap_resv += resv;
9340Sstevel@tonic-gate 			/* number of reserved and allocated pages */
9350Sstevel@tonic-gate #ifdef	DEBUG
9360Sstevel@tonic-gate 			if (maxswap < free)
9370Sstevel@tonic-gate 				cmn_err(CE_WARN, "clock: maxswap < free");
9380Sstevel@tonic-gate 			if (maxswap < resv)
9390Sstevel@tonic-gate 				cmn_err(CE_WARN, "clock: maxswap < resv");
9400Sstevel@tonic-gate #endif
9410Sstevel@tonic-gate 			vminfo.swap_alloc += maxswap - free;
9420Sstevel@tonic-gate 			vminfo.swap_avail += maxswap - resv;
9430Sstevel@tonic-gate 			vminfo.swap_free += free;
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 		if (nrunnable) {
9460Sstevel@tonic-gate 			sysinfo.runque += nrunnable;
9470Sstevel@tonic-gate 			sysinfo.runocc++;
9480Sstevel@tonic-gate 		}
9490Sstevel@tonic-gate 		if (nswapped) {
9500Sstevel@tonic-gate 			sysinfo.swpque += nswapped;
9510Sstevel@tonic-gate 			sysinfo.swpocc++;
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 		sysinfo.waiting += w_io;
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 		/*
9560Sstevel@tonic-gate 		 * Wake up fsflush to write out DELWRI
9570Sstevel@tonic-gate 		 * buffers, dirty pages and other cached
9580Sstevel@tonic-gate 		 * administrative data, e.g. inodes.
9590Sstevel@tonic-gate 		 */
9600Sstevel@tonic-gate 		if (--fsflushcnt <= 0) {
9610Sstevel@tonic-gate 			fsflushcnt = tune.t_fsflushr;
9620Sstevel@tonic-gate 			cv_signal(&fsflush_cv);
9630Sstevel@tonic-gate 		}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		vmmeter();
9660Sstevel@tonic-gate 		calcloadavg(genloadavg(&loadavg), hp_avenrun);
9670Sstevel@tonic-gate 		for (i = 0; i < 3; i++)
9680Sstevel@tonic-gate 			/*
9690Sstevel@tonic-gate 			 * At the moment avenrun[] can only hold 31
9700Sstevel@tonic-gate 			 * bits of load average as it is a signed
9710Sstevel@tonic-gate 			 * int in the API. We need to ensure that
9720Sstevel@tonic-gate 			 * hp_avenrun[i] >> (16 - FSHIFT) will not be
9730Sstevel@tonic-gate 			 * too large. If it is, we put the largest value
9740Sstevel@tonic-gate 			 * that we can use into avenrun[i]. This is
9750Sstevel@tonic-gate 			 * kludgey, but about all we can do until we
9760Sstevel@tonic-gate 			 * avenrun[] is declared as an array of uint64[]
9770Sstevel@tonic-gate 			 */
9780Sstevel@tonic-gate 			if (hp_avenrun[i] < ((uint64_t)1<<(31+16-FSHIFT)))
9790Sstevel@tonic-gate 				avenrun[i] = (int32_t)(hp_avenrun[i] >>
9800Sstevel@tonic-gate 				    (16 - FSHIFT));
9810Sstevel@tonic-gate 			else
9820Sstevel@tonic-gate 				avenrun[i] = 0x7fffffff;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 		cpupart = cp_list_head;
9850Sstevel@tonic-gate 		do {
9860Sstevel@tonic-gate 			calcloadavg(genloadavg(&cpupart->cp_loadavg),
9870Sstevel@tonic-gate 			    cpupart->cp_hp_avenrun);
9880Sstevel@tonic-gate 		} while ((cpupart = cpupart->cp_next) != cp_list_head);
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 		/*
9910Sstevel@tonic-gate 		 * Wake up the swapper thread if necessary.
9920Sstevel@tonic-gate 		 */
9930Sstevel@tonic-gate 		if (runin ||
9940Sstevel@tonic-gate 		    (runout && (avefree < desfree || wake_sched_sec))) {
9950Sstevel@tonic-gate 			t = &t0;
9960Sstevel@tonic-gate 			thread_lock(t);
9970Sstevel@tonic-gate 			if (t->t_state == TS_STOPPED) {
9980Sstevel@tonic-gate 				runin = runout = 0;
9990Sstevel@tonic-gate 				wake_sched_sec = 0;
10000Sstevel@tonic-gate 				t->t_whystop = 0;
10010Sstevel@tonic-gate 				t->t_whatstop = 0;
10020Sstevel@tonic-gate 				t->t_schedflag &= ~TS_ALLSTART;
10030Sstevel@tonic-gate 				THREAD_TRANSITION(t);
10040Sstevel@tonic-gate 				setfrontdq(t);
10050Sstevel@tonic-gate 			}
10060Sstevel@tonic-gate 			thread_unlock(t);
10070Sstevel@tonic-gate 		}
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	/*
10110Sstevel@tonic-gate 	 * Wake up the swapper if any high priority swapped-out threads
10120Sstevel@tonic-gate 	 * became runable during the last tick.
10130Sstevel@tonic-gate 	 */
10140Sstevel@tonic-gate 	if (wake_sched) {
10150Sstevel@tonic-gate 		t = &t0;
10160Sstevel@tonic-gate 		thread_lock(t);
10170Sstevel@tonic-gate 		if (t->t_state == TS_STOPPED) {
10180Sstevel@tonic-gate 			runin = runout = 0;
10190Sstevel@tonic-gate 			wake_sched = 0;
10200Sstevel@tonic-gate 			t->t_whystop = 0;
10210Sstevel@tonic-gate 			t->t_whatstop = 0;
10220Sstevel@tonic-gate 			t->t_schedflag &= ~TS_ALLSTART;
10230Sstevel@tonic-gate 			THREAD_TRANSITION(t);
10240Sstevel@tonic-gate 			setfrontdq(t);
10250Sstevel@tonic-gate 		}
10260Sstevel@tonic-gate 		thread_unlock(t);
10270Sstevel@tonic-gate 	}
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate void
10310Sstevel@tonic-gate clock_init(void)
10320Sstevel@tonic-gate {
10330Sstevel@tonic-gate 	cyc_handler_t hdlr;
10340Sstevel@tonic-gate 	cyc_time_t when;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	hdlr.cyh_func = (cyc_func_t)clock;
10370Sstevel@tonic-gate 	hdlr.cyh_level = CY_LOCK_LEVEL;
10380Sstevel@tonic-gate 	hdlr.cyh_arg = NULL;
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	when.cyt_when = 0;
10410Sstevel@tonic-gate 	when.cyt_interval = nsec_per_tick;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
10440Sstevel@tonic-gate 	clock_cyclic = cyclic_add(&hdlr, &when);
10450Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
10460Sstevel@tonic-gate }
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate /*
10490Sstevel@tonic-gate  * Called before calcloadavg to get 10-sec moving loadavg together
10500Sstevel@tonic-gate  */
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate static int
10530Sstevel@tonic-gate genloadavg(struct loadavg_s *avgs)
10540Sstevel@tonic-gate {
10550Sstevel@tonic-gate 	int avg;
10560Sstevel@tonic-gate 	int spos; /* starting position */
10570Sstevel@tonic-gate 	int cpos; /* moving current position */
10580Sstevel@tonic-gate 	int i;
10590Sstevel@tonic-gate 	int slen;
10600Sstevel@tonic-gate 	hrtime_t hr_avg;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	/* 10-second snapshot, calculate first positon */
10630Sstevel@tonic-gate 	if (avgs->lg_len == 0) {
10640Sstevel@tonic-gate 		return (0);
10650Sstevel@tonic-gate 	}
10660Sstevel@tonic-gate 	slen = avgs->lg_len < S_MOVAVG_SZ ? avgs->lg_len : S_MOVAVG_SZ;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	spos = (avgs->lg_cur - 1) >= 0 ? avgs->lg_cur - 1 :
10690Sstevel@tonic-gate 	    S_LOADAVG_SZ + (avgs->lg_cur - 1);
10700Sstevel@tonic-gate 	for (i = hr_avg = 0; i < slen; i++) {
10710Sstevel@tonic-gate 		cpos = (spos - i) >= 0 ? spos - i : S_LOADAVG_SZ + (spos - i);
10720Sstevel@tonic-gate 		hr_avg += avgs->lg_loads[cpos];
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	hr_avg = hr_avg / slen;
10760Sstevel@tonic-gate 	avg = hr_avg / (NANOSEC / LGRP_LOADAVG_IN_THREAD_MAX);
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	return (avg);
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate /*
10820Sstevel@tonic-gate  * Run every second from clock () to update the loadavg count available to the
10830Sstevel@tonic-gate  * system and cpu-partitions.
10840Sstevel@tonic-gate  *
10850Sstevel@tonic-gate  * This works by sampling the previous usr, sys, wait time elapsed,
10860Sstevel@tonic-gate  * computing a delta, and adding that delta to the elapsed usr, sys,
10870Sstevel@tonic-gate  * wait increase.
10880Sstevel@tonic-gate  */
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate static void
10910Sstevel@tonic-gate loadavg_update()
10920Sstevel@tonic-gate {
10930Sstevel@tonic-gate 	cpu_t *cp;
10940Sstevel@tonic-gate 	cpupart_t *cpupart;
10950Sstevel@tonic-gate 	hrtime_t cpu_total;
10960Sstevel@tonic-gate 	int prev;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	cp = cpu_list;
10990Sstevel@tonic-gate 	loadavg.lg_total = 0;
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	/*
11020Sstevel@tonic-gate 	 * first pass totals up per-cpu statistics for system and cpu
11030Sstevel@tonic-gate 	 * partitions
11040Sstevel@tonic-gate 	 */
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	do {
11070Sstevel@tonic-gate 		struct loadavg_s *lavg;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 		lavg = &cp->cpu_loadavg;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		cpu_total = cp->cpu_acct[CMS_USER] +
11120Sstevel@tonic-gate 		    cp->cpu_acct[CMS_SYSTEM] + cp->cpu_waitrq;
11130Sstevel@tonic-gate 		/* compute delta against last total */
11140Sstevel@tonic-gate 		scalehrtime(&cpu_total);
11150Sstevel@tonic-gate 		prev = (lavg->lg_cur - 1) >= 0 ? lavg->lg_cur - 1 :
11160Sstevel@tonic-gate 		    S_LOADAVG_SZ + (lavg->lg_cur - 1);
11170Sstevel@tonic-gate 		if (lavg->lg_loads[prev] <= 0) {
11180Sstevel@tonic-gate 			lavg->lg_loads[lavg->lg_cur] = cpu_total;
11190Sstevel@tonic-gate 			cpu_total = 0;
11200Sstevel@tonic-gate 		} else {
11210Sstevel@tonic-gate 			lavg->lg_loads[lavg->lg_cur] = cpu_total;
11220Sstevel@tonic-gate 			cpu_total = cpu_total - lavg->lg_loads[prev];
11230Sstevel@tonic-gate 			if (cpu_total < 0)
11240Sstevel@tonic-gate 				cpu_total = 0;
11250Sstevel@tonic-gate 		}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
11280Sstevel@tonic-gate 		lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
11290Sstevel@tonic-gate 		    lavg->lg_len + 1 : S_LOADAVG_SZ;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 		loadavg.lg_total += cpu_total;
11320Sstevel@tonic-gate 		cp->cpu_part->cp_loadavg.lg_total += cpu_total;
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	loadavg.lg_loads[loadavg.lg_cur] = loadavg.lg_total;
11370Sstevel@tonic-gate 	loadavg.lg_cur = (loadavg.lg_cur + 1) % S_LOADAVG_SZ;
11380Sstevel@tonic-gate 	loadavg.lg_len = (loadavg.lg_len + 1) < S_LOADAVG_SZ ?
11390Sstevel@tonic-gate 	    loadavg.lg_len + 1 : S_LOADAVG_SZ;
11400Sstevel@tonic-gate 	/*
11410Sstevel@tonic-gate 	 * Second pass updates counts
11420Sstevel@tonic-gate 	 */
11430Sstevel@tonic-gate 	cpupart = cp_list_head;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	do {
11460Sstevel@tonic-gate 		struct loadavg_s *lavg;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 		lavg = &cpupart->cp_loadavg;
11490Sstevel@tonic-gate 		lavg->lg_loads[lavg->lg_cur] = lavg->lg_total;
11500Sstevel@tonic-gate 		lavg->lg_total = 0;
11510Sstevel@tonic-gate 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
11520Sstevel@tonic-gate 		lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
11530Sstevel@tonic-gate 		    lavg->lg_len + 1 : S_LOADAVG_SZ;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	} while ((cpupart = cpupart->cp_next) != cp_list_head);
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate }
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate /*
11600Sstevel@tonic-gate  * clock_update() - local clock update
11610Sstevel@tonic-gate  *
11620Sstevel@tonic-gate  * This routine is called by ntp_adjtime() to update the local clock
11630Sstevel@tonic-gate  * phase and frequency. The implementation is of an
11640Sstevel@tonic-gate  * adaptive-parameter, hybrid phase/frequency-lock loop (PLL/FLL). The
11650Sstevel@tonic-gate  * routine computes new time and frequency offset estimates for each
11660Sstevel@tonic-gate  * call.  The PPS signal itself determines the new time offset,
11670Sstevel@tonic-gate  * instead of the calling argument.  Presumably, calls to
11680Sstevel@tonic-gate  * ntp_adjtime() occur only when the caller believes the local clock
11690Sstevel@tonic-gate  * is valid within some bound (+-128 ms with NTP). If the caller's
11700Sstevel@tonic-gate  * time is far different than the PPS time, an argument will ensue,
11710Sstevel@tonic-gate  * and it's not clear who will lose.
11720Sstevel@tonic-gate  *
11730Sstevel@tonic-gate  * For uncompensated quartz crystal oscillatores and nominal update
11740Sstevel@tonic-gate  * intervals less than 1024 s, operation should be in phase-lock mode
11750Sstevel@tonic-gate  * (STA_FLL = 0), where the loop is disciplined to phase. For update
11760Sstevel@tonic-gate  * intervals greater than this, operation should be in frequency-lock
11770Sstevel@tonic-gate  * mode (STA_FLL = 1), where the loop is disciplined to frequency.
11780Sstevel@tonic-gate  *
11790Sstevel@tonic-gate  * Note: mutex(&tod_lock) is in effect.
11800Sstevel@tonic-gate  */
11810Sstevel@tonic-gate void
11820Sstevel@tonic-gate clock_update(int offset)
11830Sstevel@tonic-gate {
11840Sstevel@tonic-gate 	int ltemp, mtemp, s;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME))
11890Sstevel@tonic-gate 		return;
11900Sstevel@tonic-gate 	ltemp = offset;
11910Sstevel@tonic-gate 	if ((time_status & STA_PPSTIME) && (time_status & STA_PPSSIGNAL))
11920Sstevel@tonic-gate 		ltemp = pps_offset;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	/*
11950Sstevel@tonic-gate 	 * Scale the phase adjustment and clamp to the operating range.
11960Sstevel@tonic-gate 	 */
11970Sstevel@tonic-gate 	if (ltemp > MAXPHASE)
11980Sstevel@tonic-gate 		time_offset = MAXPHASE * SCALE_UPDATE;
11990Sstevel@tonic-gate 	else if (ltemp < -MAXPHASE)
12000Sstevel@tonic-gate 		time_offset = -(MAXPHASE * SCALE_UPDATE);
12010Sstevel@tonic-gate 	else
12020Sstevel@tonic-gate 		time_offset = ltemp * SCALE_UPDATE;
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	/*
12050Sstevel@tonic-gate 	 * Select whether the frequency is to be controlled and in which
12060Sstevel@tonic-gate 	 * mode (PLL or FLL). Clamp to the operating range. Ugly
12070Sstevel@tonic-gate 	 * multiply/divide should be replaced someday.
12080Sstevel@tonic-gate 	 */
12090Sstevel@tonic-gate 	if (time_status & STA_FREQHOLD || time_reftime == 0)
12100Sstevel@tonic-gate 		time_reftime = hrestime.tv_sec;
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	mtemp = hrestime.tv_sec - time_reftime;
12130Sstevel@tonic-gate 	time_reftime = hrestime.tv_sec;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	if (time_status & STA_FLL) {
12160Sstevel@tonic-gate 		if (mtemp >= MINSEC) {
12170Sstevel@tonic-gate 			ltemp = ((time_offset / mtemp) * (SCALE_USEC /
12180Sstevel@tonic-gate 			    SCALE_UPDATE));
12190Sstevel@tonic-gate 			if (ltemp)
12200Sstevel@tonic-gate 				time_freq += ltemp / SCALE_KH;
12210Sstevel@tonic-gate 		}
12220Sstevel@tonic-gate 	} else {
12230Sstevel@tonic-gate 		if (mtemp < MAXSEC) {
12240Sstevel@tonic-gate 			ltemp *= mtemp;
12250Sstevel@tonic-gate 			if (ltemp)
12260Sstevel@tonic-gate 				time_freq += (int)(((int64_t)ltemp *
12270Sstevel@tonic-gate 				    SCALE_USEC) / SCALE_KF)
12280Sstevel@tonic-gate 				    / (1 << (time_constant * 2));
12290Sstevel@tonic-gate 		}
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 	if (time_freq > time_tolerance)
12320Sstevel@tonic-gate 		time_freq = time_tolerance;
12330Sstevel@tonic-gate 	else if (time_freq < -time_tolerance)
12340Sstevel@tonic-gate 		time_freq = -time_tolerance;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	s = hr_clock_lock();
12370Sstevel@tonic-gate 	tod_needsync = 1;
12380Sstevel@tonic-gate 	hr_clock_unlock(s);
12390Sstevel@tonic-gate }
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate /*
12420Sstevel@tonic-gate  * ddi_hardpps() - discipline CPU clock oscillator to external PPS signal
12430Sstevel@tonic-gate  *
12440Sstevel@tonic-gate  * This routine is called at each PPS interrupt in order to discipline
12450Sstevel@tonic-gate  * the CPU clock oscillator to the PPS signal. It measures the PPS phase
12460Sstevel@tonic-gate  * and leaves it in a handy spot for the clock() routine. It
12470Sstevel@tonic-gate  * integrates successive PPS phase differences and calculates the
12480Sstevel@tonic-gate  * frequency offset. This is used in clock() to discipline the CPU
12490Sstevel@tonic-gate  * clock oscillator so that intrinsic frequency error is cancelled out.
12500Sstevel@tonic-gate  * The code requires the caller to capture the time and hardware counter
12510Sstevel@tonic-gate  * value at the on-time PPS signal transition.
12520Sstevel@tonic-gate  *
12530Sstevel@tonic-gate  * Note that, on some Unix systems, this routine runs at an interrupt
12540Sstevel@tonic-gate  * priority level higher than the timer interrupt routine clock().
12550Sstevel@tonic-gate  * Therefore, the variables used are distinct from the clock()
12560Sstevel@tonic-gate  * variables, except for certain exceptions: The PPS frequency pps_freq
12570Sstevel@tonic-gate  * and phase pps_offset variables are determined by this routine and
12580Sstevel@tonic-gate  * updated atomically. The time_tolerance variable can be considered a
12590Sstevel@tonic-gate  * constant, since it is infrequently changed, and then only when the
12600Sstevel@tonic-gate  * PPS signal is disabled. The watchdog counter pps_valid is updated
12610Sstevel@tonic-gate  * once per second by clock() and is atomically cleared in this
12620Sstevel@tonic-gate  * routine.
12630Sstevel@tonic-gate  *
12640Sstevel@tonic-gate  * tvp is the time of the last tick; usec is a microsecond count since the
12650Sstevel@tonic-gate  * last tick.
12660Sstevel@tonic-gate  *
12670Sstevel@tonic-gate  * Note: In Solaris systems, the tick value is actually given by
12680Sstevel@tonic-gate  *       usec_per_tick.  This is called from the serial driver cdintr(),
12690Sstevel@tonic-gate  *	 or equivalent, at a high PIL.  Because the kernel keeps a
12700Sstevel@tonic-gate  *	 highresolution time, the following code can accept either
12710Sstevel@tonic-gate  *	 the traditional argument pair, or the current highres timestamp
12720Sstevel@tonic-gate  *       in tvp and zero in usec.
12730Sstevel@tonic-gate  */
12740Sstevel@tonic-gate void
12750Sstevel@tonic-gate ddi_hardpps(struct timeval *tvp, int usec)
12760Sstevel@tonic-gate {
12770Sstevel@tonic-gate 	int u_usec, v_usec, bigtick;
12780Sstevel@tonic-gate 	time_t cal_sec;
12790Sstevel@tonic-gate 	int cal_usec;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	/*
12820Sstevel@tonic-gate 	 * An occasional glitch can be produced when the PPS interrupt
12830Sstevel@tonic-gate 	 * occurs in the clock() routine before the time variable is
12840Sstevel@tonic-gate 	 * updated. Here the offset is discarded when the difference
12850Sstevel@tonic-gate 	 * between it and the last one is greater than tick/2, but not
12860Sstevel@tonic-gate 	 * if the interval since the first discard exceeds 30 s.
12870Sstevel@tonic-gate 	 */
12880Sstevel@tonic-gate 	time_status |= STA_PPSSIGNAL;
12890Sstevel@tonic-gate 	time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
12900Sstevel@tonic-gate 	pps_valid = 0;
12910Sstevel@tonic-gate 	u_usec = -tvp->tv_usec;
12920Sstevel@tonic-gate 	if (u_usec < -(MICROSEC/2))
12930Sstevel@tonic-gate 		u_usec += MICROSEC;
12940Sstevel@tonic-gate 	v_usec = pps_offset - u_usec;
12950Sstevel@tonic-gate 	if (v_usec < 0)
12960Sstevel@tonic-gate 		v_usec = -v_usec;
12970Sstevel@tonic-gate 	if (v_usec > (usec_per_tick >> 1)) {
12980Sstevel@tonic-gate 		if (pps_glitch > MAXGLITCH) {
12990Sstevel@tonic-gate 			pps_glitch = 0;
13000Sstevel@tonic-gate 			pps_tf[2] = u_usec;
13010Sstevel@tonic-gate 			pps_tf[1] = u_usec;
13020Sstevel@tonic-gate 		} else {
13030Sstevel@tonic-gate 			pps_glitch++;
13040Sstevel@tonic-gate 			u_usec = pps_offset;
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 	} else
13070Sstevel@tonic-gate 		pps_glitch = 0;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	/*
13100Sstevel@tonic-gate 	 * A three-stage median filter is used to help deglitch the pps
13110Sstevel@tonic-gate 	 * time. The median sample becomes the time offset estimate; the
13120Sstevel@tonic-gate 	 * difference between the other two samples becomes the time
13130Sstevel@tonic-gate 	 * dispersion (jitter) estimate.
13140Sstevel@tonic-gate 	 */
13150Sstevel@tonic-gate 	pps_tf[2] = pps_tf[1];
13160Sstevel@tonic-gate 	pps_tf[1] = pps_tf[0];
13170Sstevel@tonic-gate 	pps_tf[0] = u_usec;
13180Sstevel@tonic-gate 	if (pps_tf[0] > pps_tf[1]) {
13190Sstevel@tonic-gate 		if (pps_tf[1] > pps_tf[2]) {
13200Sstevel@tonic-gate 			pps_offset = pps_tf[1];		/* 0 1 2 */
13210Sstevel@tonic-gate 			v_usec = pps_tf[0] - pps_tf[2];
13220Sstevel@tonic-gate 		} else if (pps_tf[2] > pps_tf[0]) {
13230Sstevel@tonic-gate 			pps_offset = pps_tf[0];		/* 2 0 1 */
13240Sstevel@tonic-gate 			v_usec = pps_tf[2] - pps_tf[1];
13250Sstevel@tonic-gate 		} else {
13260Sstevel@tonic-gate 			pps_offset = pps_tf[2];		/* 0 2 1 */
13270Sstevel@tonic-gate 			v_usec = pps_tf[0] - pps_tf[1];
13280Sstevel@tonic-gate 		}
13290Sstevel@tonic-gate 	} else {
13300Sstevel@tonic-gate 		if (pps_tf[1] < pps_tf[2]) {
13310Sstevel@tonic-gate 			pps_offset = pps_tf[1];		/* 2 1 0 */
13320Sstevel@tonic-gate 			v_usec = pps_tf[2] - pps_tf[0];
13330Sstevel@tonic-gate 		} else  if (pps_tf[2] < pps_tf[0]) {
13340Sstevel@tonic-gate 			pps_offset = pps_tf[0];		/* 1 0 2 */
13350Sstevel@tonic-gate 			v_usec = pps_tf[1] - pps_tf[2];
13360Sstevel@tonic-gate 		} else {
13370Sstevel@tonic-gate 			pps_offset = pps_tf[2];		/* 1 2 0 */
13380Sstevel@tonic-gate 			v_usec = pps_tf[1] - pps_tf[0];
13390Sstevel@tonic-gate 		}
13400Sstevel@tonic-gate 	}
13410Sstevel@tonic-gate 	if (v_usec > MAXTIME)
13420Sstevel@tonic-gate 		pps_jitcnt++;
13430Sstevel@tonic-gate 	v_usec = (v_usec << PPS_AVG) - pps_jitter;
13440Sstevel@tonic-gate 	pps_jitter += v_usec / (1 << PPS_AVG);
13450Sstevel@tonic-gate 	if (pps_jitter > (MAXTIME >> 1))
13460Sstevel@tonic-gate 		time_status |= STA_PPSJITTER;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	/*
13490Sstevel@tonic-gate 	 * During the calibration interval adjust the starting time when
13500Sstevel@tonic-gate 	 * the tick overflows. At the end of the interval compute the
13510Sstevel@tonic-gate 	 * duration of the interval and the difference of the hardware
13520Sstevel@tonic-gate 	 * counters at the beginning and end of the interval. This code
13530Sstevel@tonic-gate 	 * is deliciously complicated by the fact valid differences may
13540Sstevel@tonic-gate 	 * exceed the value of tick when using long calibration
13550Sstevel@tonic-gate 	 * intervals and small ticks. Note that the counter can be
13560Sstevel@tonic-gate 	 * greater than tick if caught at just the wrong instant, but
13570Sstevel@tonic-gate 	 * the values returned and used here are correct.
13580Sstevel@tonic-gate 	 */
13590Sstevel@tonic-gate 	bigtick = (int)usec_per_tick * SCALE_USEC;
13600Sstevel@tonic-gate 	pps_usec -= pps_freq;
13610Sstevel@tonic-gate 	if (pps_usec >= bigtick)
13620Sstevel@tonic-gate 		pps_usec -= bigtick;
13630Sstevel@tonic-gate 	if (pps_usec < 0)
13640Sstevel@tonic-gate 		pps_usec += bigtick;
13650Sstevel@tonic-gate 	pps_time.tv_sec++;
13660Sstevel@tonic-gate 	pps_count++;
13670Sstevel@tonic-gate 	if (pps_count < (1 << pps_shift))
13680Sstevel@tonic-gate 		return;
13690Sstevel@tonic-gate 	pps_count = 0;
13700Sstevel@tonic-gate 	pps_calcnt++;
13710Sstevel@tonic-gate 	u_usec = usec * SCALE_USEC;
13720Sstevel@tonic-gate 	v_usec = pps_usec - u_usec;
13730Sstevel@tonic-gate 	if (v_usec >= bigtick >> 1)
13740Sstevel@tonic-gate 		v_usec -= bigtick;
13750Sstevel@tonic-gate 	if (v_usec < -(bigtick >> 1))
13760Sstevel@tonic-gate 		v_usec += bigtick;
13770Sstevel@tonic-gate 	if (v_usec < 0)
13780Sstevel@tonic-gate 		v_usec = -(-v_usec >> pps_shift);
13790Sstevel@tonic-gate 	else
13800Sstevel@tonic-gate 		v_usec = v_usec >> pps_shift;
13810Sstevel@tonic-gate 	pps_usec = u_usec;
13820Sstevel@tonic-gate 	cal_sec = tvp->tv_sec;
13830Sstevel@tonic-gate 	cal_usec = tvp->tv_usec;
13840Sstevel@tonic-gate 	cal_sec -= pps_time.tv_sec;
13850Sstevel@tonic-gate 	cal_usec -= pps_time.tv_usec;
13860Sstevel@tonic-gate 	if (cal_usec < 0) {
13870Sstevel@tonic-gate 		cal_usec += MICROSEC;
13880Sstevel@tonic-gate 		cal_sec--;
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 	pps_time = *tvp;
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	/*
13930Sstevel@tonic-gate 	 * Check for lost interrupts, noise, excessive jitter and
13940Sstevel@tonic-gate 	 * excessive frequency error. The number of timer ticks during
13950Sstevel@tonic-gate 	 * the interval may vary +-1 tick. Add to this a margin of one
13960Sstevel@tonic-gate 	 * tick for the PPS signal jitter and maximum frequency
13970Sstevel@tonic-gate 	 * deviation. If the limits are exceeded, the calibration
13980Sstevel@tonic-gate 	 * interval is reset to the minimum and we start over.
13990Sstevel@tonic-gate 	 */
14000Sstevel@tonic-gate 	u_usec = (int)usec_per_tick << 1;
14010Sstevel@tonic-gate 	if (!((cal_sec == -1 && cal_usec > (MICROSEC - u_usec)) ||
14020Sstevel@tonic-gate 	    (cal_sec == 0 && cal_usec < u_usec)) ||
14030Sstevel@tonic-gate 	    v_usec > time_tolerance || v_usec < -time_tolerance) {
14040Sstevel@tonic-gate 		pps_errcnt++;
14050Sstevel@tonic-gate 		pps_shift = PPS_SHIFT;
14060Sstevel@tonic-gate 		pps_intcnt = 0;
14070Sstevel@tonic-gate 		time_status |= STA_PPSERROR;
14080Sstevel@tonic-gate 		return;
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	/*
14120Sstevel@tonic-gate 	 * A three-stage median filter is used to help deglitch the pps
14130Sstevel@tonic-gate 	 * frequency. The median sample becomes the frequency offset
14140Sstevel@tonic-gate 	 * estimate; the difference between the other two samples
14150Sstevel@tonic-gate 	 * becomes the frequency dispersion (stability) estimate.
14160Sstevel@tonic-gate 	 */
14170Sstevel@tonic-gate 	pps_ff[2] = pps_ff[1];
14180Sstevel@tonic-gate 	pps_ff[1] = pps_ff[0];
14190Sstevel@tonic-gate 	pps_ff[0] = v_usec;
14200Sstevel@tonic-gate 	if (pps_ff[0] > pps_ff[1]) {
14210Sstevel@tonic-gate 		if (pps_ff[1] > pps_ff[2]) {
14220Sstevel@tonic-gate 			u_usec = pps_ff[1];		/* 0 1 2 */
14230Sstevel@tonic-gate 			v_usec = pps_ff[0] - pps_ff[2];
14240Sstevel@tonic-gate 		} else if (pps_ff[2] > pps_ff[0]) {
14250Sstevel@tonic-gate 			u_usec = pps_ff[0];		/* 2 0 1 */
14260Sstevel@tonic-gate 			v_usec = pps_ff[2] - pps_ff[1];
14270Sstevel@tonic-gate 		} else {
14280Sstevel@tonic-gate 			u_usec = pps_ff[2];		/* 0 2 1 */
14290Sstevel@tonic-gate 			v_usec = pps_ff[0] - pps_ff[1];
14300Sstevel@tonic-gate 		}
14310Sstevel@tonic-gate 	} else {
14320Sstevel@tonic-gate 		if (pps_ff[1] < pps_ff[2]) {
14330Sstevel@tonic-gate 			u_usec = pps_ff[1];		/* 2 1 0 */
14340Sstevel@tonic-gate 			v_usec = pps_ff[2] - pps_ff[0];
14350Sstevel@tonic-gate 		} else  if (pps_ff[2] < pps_ff[0]) {
14360Sstevel@tonic-gate 			u_usec = pps_ff[0];		/* 1 0 2 */
14370Sstevel@tonic-gate 			v_usec = pps_ff[1] - pps_ff[2];
14380Sstevel@tonic-gate 		} else {
14390Sstevel@tonic-gate 			u_usec = pps_ff[2];		/* 1 2 0 */
14400Sstevel@tonic-gate 			v_usec = pps_ff[1] - pps_ff[0];
14410Sstevel@tonic-gate 		}
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	/*
14450Sstevel@tonic-gate 	 * Here the frequency dispersion (stability) is updated. If it
14460Sstevel@tonic-gate 	 * is less than one-fourth the maximum (MAXFREQ), the frequency
14470Sstevel@tonic-gate 	 * offset is updated as well, but clamped to the tolerance. It
14480Sstevel@tonic-gate 	 * will be processed later by the clock() routine.
14490Sstevel@tonic-gate 	 */
14500Sstevel@tonic-gate 	v_usec = (v_usec >> 1) - pps_stabil;
14510Sstevel@tonic-gate 	if (v_usec < 0)
14520Sstevel@tonic-gate 		pps_stabil -= -v_usec >> PPS_AVG;
14530Sstevel@tonic-gate 	else
14540Sstevel@tonic-gate 		pps_stabil += v_usec >> PPS_AVG;
14550Sstevel@tonic-gate 	if (pps_stabil > MAXFREQ >> 2) {
14560Sstevel@tonic-gate 		pps_stbcnt++;
14570Sstevel@tonic-gate 		time_status |= STA_PPSWANDER;
14580Sstevel@tonic-gate 		return;
14590Sstevel@tonic-gate 	}
14600Sstevel@tonic-gate 	if (time_status & STA_PPSFREQ) {
14610Sstevel@tonic-gate 		if (u_usec < 0) {
14620Sstevel@tonic-gate 			pps_freq -= -u_usec >> PPS_AVG;
14630Sstevel@tonic-gate 			if (pps_freq < -time_tolerance)
14640Sstevel@tonic-gate 				pps_freq = -time_tolerance;
14650Sstevel@tonic-gate 			u_usec = -u_usec;
14660Sstevel@tonic-gate 		} else {
14670Sstevel@tonic-gate 			pps_freq += u_usec >> PPS_AVG;
14680Sstevel@tonic-gate 			if (pps_freq > time_tolerance)
14690Sstevel@tonic-gate 				pps_freq = time_tolerance;
14700Sstevel@tonic-gate 		}
14710Sstevel@tonic-gate 	}
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	/*
14740Sstevel@tonic-gate 	 * Here the calibration interval is adjusted. If the maximum
14750Sstevel@tonic-gate 	 * time difference is greater than tick / 4, reduce the interval
14760Sstevel@tonic-gate 	 * by half. If this is not the case for four consecutive
14770Sstevel@tonic-gate 	 * intervals, double the interval.
14780Sstevel@tonic-gate 	 */
14790Sstevel@tonic-gate 	if (u_usec << pps_shift > bigtick >> 2) {
14800Sstevel@tonic-gate 		pps_intcnt = 0;
14810Sstevel@tonic-gate 		if (pps_shift > PPS_SHIFT)
14820Sstevel@tonic-gate 			pps_shift--;
14830Sstevel@tonic-gate 	} else if (pps_intcnt >= 4) {
14840Sstevel@tonic-gate 		pps_intcnt = 0;
14850Sstevel@tonic-gate 		if (pps_shift < PPS_SHIFTMAX)
14860Sstevel@tonic-gate 			pps_shift++;
14870Sstevel@tonic-gate 	} else
14880Sstevel@tonic-gate 		pps_intcnt++;
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	/*
14910Sstevel@tonic-gate 	 * If recovering from kmdb, then make sure the tod chip gets resynced.
14920Sstevel@tonic-gate 	 * If we took an early exit above, then we don't yet have a stable
14930Sstevel@tonic-gate 	 * calibration signal to lock onto, so don't mark the tod for sync
14940Sstevel@tonic-gate 	 * until we get all the way here.
14950Sstevel@tonic-gate 	 */
14960Sstevel@tonic-gate 	{
14970Sstevel@tonic-gate 		int s = hr_clock_lock();
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 		tod_needsync = 1;
15000Sstevel@tonic-gate 		hr_clock_unlock(s);
15010Sstevel@tonic-gate 	}
15020Sstevel@tonic-gate }
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate /*
15050Sstevel@tonic-gate  * Handle clock tick processing for a thread.
15060Sstevel@tonic-gate  * Check for timer action, enforce CPU rlimit, do profiling etc.
15070Sstevel@tonic-gate  */
15080Sstevel@tonic-gate void
15090Sstevel@tonic-gate clock_tick(kthread_t *t)
15100Sstevel@tonic-gate {
15110Sstevel@tonic-gate 	struct proc *pp;
15120Sstevel@tonic-gate 	klwp_id_t    lwp;
15130Sstevel@tonic-gate 	struct as *as;
15140Sstevel@tonic-gate 	clock_t	utime;
15150Sstevel@tonic-gate 	clock_t	stime;
15160Sstevel@tonic-gate 	int	poke = 0;		/* notify another CPU */
15170Sstevel@tonic-gate 	int	user_mode;
15180Sstevel@tonic-gate 	size_t	 rss;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	/* Must be operating on a lwp/thread */
15210Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) == NULL) {
15220Sstevel@tonic-gate 		panic("clock_tick: no lwp");
15230Sstevel@tonic-gate 		/*NOTREACHED*/
15240Sstevel@tonic-gate 	}
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	CL_TICK(t);	/* Class specific tick processing */
15270Sstevel@tonic-gate 	DTRACE_SCHED1(tick, kthread_t *, t);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	pp = ttoproc(t);
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	/* pp->p_lock makes sure that the thread does not exit */
15320Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pp->p_lock));
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	user_mode = (lwp->lwp_state == LWP_USER);
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	/*
15370Sstevel@tonic-gate 	 * Update process times. Should use high res clock and state
15380Sstevel@tonic-gate 	 * changes instead of statistical sampling method. XXX
15390Sstevel@tonic-gate 	 */
15400Sstevel@tonic-gate 	if (user_mode) {
15410Sstevel@tonic-gate 		pp->p_utime++;
15420Sstevel@tonic-gate 		pp->p_task->tk_cpu_time++;
15430Sstevel@tonic-gate 	} else {
15440Sstevel@tonic-gate 		pp->p_stime++;
15450Sstevel@tonic-gate 		pp->p_task->tk_cpu_time++;
15460Sstevel@tonic-gate 	}
15470Sstevel@tonic-gate 	as = pp->p_as;
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	/*
15500Sstevel@tonic-gate 	 * Update user profiling statistics. Get the pc from the
15510Sstevel@tonic-gate 	 * lwp when the AST happens.
15520Sstevel@tonic-gate 	 */
15530Sstevel@tonic-gate 	if (pp->p_prof.pr_scale) {
15540Sstevel@tonic-gate 		atomic_add_32(&lwp->lwp_oweupc, 1);
15550Sstevel@tonic-gate 		if (user_mode) {
15560Sstevel@tonic-gate 			poke = 1;
15570Sstevel@tonic-gate 			aston(t);
15580Sstevel@tonic-gate 		}
15590Sstevel@tonic-gate 	}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	utime = pp->p_utime;
15620Sstevel@tonic-gate 	stime = pp->p_stime;
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	/*
15650Sstevel@tonic-gate 	 * If CPU was in user state, process lwp-virtual time
15660Sstevel@tonic-gate 	 * interval timer.
15670Sstevel@tonic-gate 	 */
15680Sstevel@tonic-gate 	if (user_mode &&
15690Sstevel@tonic-gate 	    timerisset(&lwp->lwp_timer[ITIMER_VIRTUAL].it_value) &&
15700Sstevel@tonic-gate 	    itimerdecr(&lwp->lwp_timer[ITIMER_VIRTUAL], usec_per_tick) == 0) {
15710Sstevel@tonic-gate 		poke = 1;
15720Sstevel@tonic-gate 		sigtoproc(pp, t, SIGVTALRM);
15730Sstevel@tonic-gate 	}
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	if (timerisset(&lwp->lwp_timer[ITIMER_PROF].it_value) &&
15760Sstevel@tonic-gate 	    itimerdecr(&lwp->lwp_timer[ITIMER_PROF], usec_per_tick) == 0) {
15770Sstevel@tonic-gate 		poke = 1;
15780Sstevel@tonic-gate 		sigtoproc(pp, t, SIGPROF);
15790Sstevel@tonic-gate 	}
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 	/*
15820Sstevel@tonic-gate 	 * Enforce CPU resource controls:
15830Sstevel@tonic-gate 	 *   (a) process.max-cpu-time resource control
15840Sstevel@tonic-gate 	 */
15850Sstevel@tonic-gate 	(void) rctl_test(rctlproc_legacy[RLIMIT_CPU], pp->p_rctls, pp,
15860Sstevel@tonic-gate 	    (utime + stime)/hz, RCA_UNSAFE_SIGINFO);
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	/*
15890Sstevel@tonic-gate 	 *   (b) task.max-cpu-time resource control
15900Sstevel@tonic-gate 	 */
15910Sstevel@tonic-gate 	(void) rctl_test(rc_task_cpu_time, pp->p_task->tk_rctls, pp, 1,
15920Sstevel@tonic-gate 	    RCA_UNSAFE_SIGINFO);
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	/*
15950Sstevel@tonic-gate 	 * Update memory usage for the currently running process.
15960Sstevel@tonic-gate 	 */
15970Sstevel@tonic-gate 	rss = rm_asrss(as);
15980Sstevel@tonic-gate 	PTOU(pp)->u_mem += rss;
15990Sstevel@tonic-gate 	if (rss > PTOU(pp)->u_mem_max)
16000Sstevel@tonic-gate 		PTOU(pp)->u_mem_max = rss;
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 	/*
16030Sstevel@tonic-gate 	 * Notify the CPU the thread is running on.
16040Sstevel@tonic-gate 	 */
16050Sstevel@tonic-gate 	if (poke && t->t_cpu != CPU)
16060Sstevel@tonic-gate 		poke_cpu(t->t_cpu->cpu_id);
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate void
16100Sstevel@tonic-gate profil_tick(uintptr_t upc)
16110Sstevel@tonic-gate {
16120Sstevel@tonic-gate 	int ticks;
16130Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
16140Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
16150Sstevel@tonic-gate 	struct prof *pr = &p->p_prof;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	do {
16180Sstevel@tonic-gate 		ticks = lwp->lwp_oweupc;
16190Sstevel@tonic-gate 	} while (cas32(&lwp->lwp_oweupc, ticks, 0) != ticks);
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	mutex_enter(&p->p_pflock);
16220Sstevel@tonic-gate 	if (pr->pr_scale >= 2 && upc >= pr->pr_off) {
16230Sstevel@tonic-gate 		/*
16240Sstevel@tonic-gate 		 * Old-style profiling
16250Sstevel@tonic-gate 		 */
16260Sstevel@tonic-gate 		uint16_t *slot = pr->pr_base;
16270Sstevel@tonic-gate 		uint16_t old, new;
16280Sstevel@tonic-gate 		if (pr->pr_scale != 2) {
16290Sstevel@tonic-gate 			uintptr_t delta = upc - pr->pr_off;
16300Sstevel@tonic-gate 			uintptr_t byteoff = ((delta >> 16) * pr->pr_scale) +
16310Sstevel@tonic-gate 			    (((delta & 0xffff) * pr->pr_scale) >> 16);
16320Sstevel@tonic-gate 			if (byteoff >= (uintptr_t)pr->pr_size) {
16330Sstevel@tonic-gate 				mutex_exit(&p->p_pflock);
16340Sstevel@tonic-gate 				return;
16350Sstevel@tonic-gate 			}
16360Sstevel@tonic-gate 			slot += byteoff / sizeof (uint16_t);
16370Sstevel@tonic-gate 		}
16380Sstevel@tonic-gate 		if (fuword16(slot, &old) < 0 ||
16390Sstevel@tonic-gate 		    (new = old + ticks) > SHRT_MAX ||
16400Sstevel@tonic-gate 		    suword16(slot, new) < 0) {
16410Sstevel@tonic-gate 			pr->pr_scale = 0;
16420Sstevel@tonic-gate 		}
16430Sstevel@tonic-gate 	} else if (pr->pr_scale == 1) {
16440Sstevel@tonic-gate 		/*
16450Sstevel@tonic-gate 		 * PC Sampling
16460Sstevel@tonic-gate 		 */
16470Sstevel@tonic-gate 		model_t model = lwp_getdatamodel(lwp);
16480Sstevel@tonic-gate 		int result;
16490Sstevel@tonic-gate #ifdef __lint
16500Sstevel@tonic-gate 		model = model;
16510Sstevel@tonic-gate #endif
16520Sstevel@tonic-gate 		while (ticks-- > 0) {
16530Sstevel@tonic-gate 			if (pr->pr_samples == pr->pr_size) {
16540Sstevel@tonic-gate 				/* buffer full, turn off sampling */
16550Sstevel@tonic-gate 				pr->pr_scale = 0;
16560Sstevel@tonic-gate 				break;
16570Sstevel@tonic-gate 			}
16580Sstevel@tonic-gate 			switch (SIZEOF_PTR(model)) {
16590Sstevel@tonic-gate 			case sizeof (uint32_t):
16600Sstevel@tonic-gate 				result = suword32(pr->pr_base, (uint32_t)upc);
16610Sstevel@tonic-gate 				break;
16620Sstevel@tonic-gate #ifdef _LP64
16630Sstevel@tonic-gate 			case sizeof (uint64_t):
16640Sstevel@tonic-gate 				result = suword64(pr->pr_base, (uint64_t)upc);
16650Sstevel@tonic-gate 				break;
16660Sstevel@tonic-gate #endif
16670Sstevel@tonic-gate 			default:
16680Sstevel@tonic-gate 				cmn_err(CE_WARN, "profil_tick: unexpected "
16690Sstevel@tonic-gate 				    "data model");
16700Sstevel@tonic-gate 				result = -1;
16710Sstevel@tonic-gate 				break;
16720Sstevel@tonic-gate 			}
16730Sstevel@tonic-gate 			if (result != 0) {
16740Sstevel@tonic-gate 				pr->pr_scale = 0;
16750Sstevel@tonic-gate 				break;
16760Sstevel@tonic-gate 			}
16770Sstevel@tonic-gate 			pr->pr_base = (caddr_t)pr->pr_base + SIZEOF_PTR(model);
16780Sstevel@tonic-gate 			pr->pr_samples++;
16790Sstevel@tonic-gate 		}
16800Sstevel@tonic-gate 	}
16810Sstevel@tonic-gate 	mutex_exit(&p->p_pflock);
16820Sstevel@tonic-gate }
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate static void
16850Sstevel@tonic-gate delay_wakeup(void *arg)
16860Sstevel@tonic-gate {
16870Sstevel@tonic-gate 	kthread_t *t = arg;
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 	mutex_enter(&t->t_delay_lock);
16900Sstevel@tonic-gate 	cv_signal(&t->t_delay_cv);
16910Sstevel@tonic-gate 	mutex_exit(&t->t_delay_lock);
16920Sstevel@tonic-gate }
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate void
16950Sstevel@tonic-gate delay(clock_t ticks)
16960Sstevel@tonic-gate {
16970Sstevel@tonic-gate 	kthread_t *t = curthread;
16980Sstevel@tonic-gate 	clock_t deadline = lbolt + ticks;
16990Sstevel@tonic-gate 	clock_t timeleft;
17000Sstevel@tonic-gate 	timeout_id_t id;
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	if (panicstr && ticks > 0) {
17030Sstevel@tonic-gate 		/*
17040Sstevel@tonic-gate 		 * Timeouts aren't running, so all we can do is spin.
17050Sstevel@tonic-gate 		 */
17060Sstevel@tonic-gate 		drv_usecwait(TICK_TO_USEC(ticks));
17070Sstevel@tonic-gate 		return;
17080Sstevel@tonic-gate 	}
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 	while ((timeleft = deadline - lbolt) > 0) {
17110Sstevel@tonic-gate 		mutex_enter(&t->t_delay_lock);
17120Sstevel@tonic-gate 		id = timeout(delay_wakeup, t, timeleft);
17130Sstevel@tonic-gate 		cv_wait(&t->t_delay_cv, &t->t_delay_lock);
17140Sstevel@tonic-gate 		mutex_exit(&t->t_delay_lock);
17150Sstevel@tonic-gate 		(void) untimeout(id);
17160Sstevel@tonic-gate 	}
17170Sstevel@tonic-gate }
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate /*
17200Sstevel@tonic-gate  * Like delay, but interruptible by a signal.
17210Sstevel@tonic-gate  */
17220Sstevel@tonic-gate int
17230Sstevel@tonic-gate delay_sig(clock_t ticks)
17240Sstevel@tonic-gate {
17250Sstevel@tonic-gate 	clock_t deadline = lbolt + ticks;
17260Sstevel@tonic-gate 	clock_t rc;
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	mutex_enter(&curthread->t_delay_lock);
17290Sstevel@tonic-gate 	do {
17300Sstevel@tonic-gate 		rc = cv_timedwait_sig(&curthread->t_delay_cv,
17310Sstevel@tonic-gate 		    &curthread->t_delay_lock, deadline);
17320Sstevel@tonic-gate 	} while (rc > 0);
17330Sstevel@tonic-gate 	mutex_exit(&curthread->t_delay_lock);
17340Sstevel@tonic-gate 	if (rc == 0)
17350Sstevel@tonic-gate 		return (EINTR);
17360Sstevel@tonic-gate 	return (0);
17370Sstevel@tonic-gate }
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate #define	SECONDS_PER_DAY 86400
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate /*
17420Sstevel@tonic-gate  * Initialize the system time based on the TOD chip.  approx is used as
17430Sstevel@tonic-gate  * an approximation of time (e.g. from the filesystem) in the event that
17440Sstevel@tonic-gate  * the TOD chip has been cleared or is unresponsive.  An approx of -1
17450Sstevel@tonic-gate  * means the filesystem doesn't keep time.
17460Sstevel@tonic-gate  */
17470Sstevel@tonic-gate void
17480Sstevel@tonic-gate clkset(time_t approx)
17490Sstevel@tonic-gate {
17500Sstevel@tonic-gate 	timestruc_t ts;
17510Sstevel@tonic-gate 	int spl;
17520Sstevel@tonic-gate 	int set_clock = 0;
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	mutex_enter(&tod_lock);
17550Sstevel@tonic-gate 	ts = tod_get();
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	if (ts.tv_sec > 365 * SECONDS_PER_DAY) {
17580Sstevel@tonic-gate 		/*
17590Sstevel@tonic-gate 		 * If the TOD chip is reporting some time after 1971,
17600Sstevel@tonic-gate 		 * then it probably didn't lose power or become otherwise
17610Sstevel@tonic-gate 		 * cleared in the recent past;  check to assure that
17620Sstevel@tonic-gate 		 * the time coming from the filesystem isn't in the future
17630Sstevel@tonic-gate 		 * according to the TOD chip.
17640Sstevel@tonic-gate 		 */
17650Sstevel@tonic-gate 		if (approx != -1 && approx > ts.tv_sec) {
17660Sstevel@tonic-gate 			cmn_err(CE_WARN, "Last shutdown is later "
17670Sstevel@tonic-gate 			    "than time on time-of-day chip; check date.");
17680Sstevel@tonic-gate 		}
17690Sstevel@tonic-gate 	} else {
17700Sstevel@tonic-gate 		/*
17710Sstevel@tonic-gate 		 * If the TOD chip isn't giving correct time, then set it to
17720Sstevel@tonic-gate 		 * the time that was passed in as a rough estimate.  If we
17730Sstevel@tonic-gate 		 * don't have an estimate, then set the clock back to a time
17740Sstevel@tonic-gate 		 * when Oliver North, ALF and Dire Straits were all on the
17750Sstevel@tonic-gate 		 * collective brain:  1987.
17760Sstevel@tonic-gate 		 */
17770Sstevel@tonic-gate 		timestruc_t tmp;
17780Sstevel@tonic-gate 		if (approx == -1)
17790Sstevel@tonic-gate 			ts.tv_sec = (1987 - 1970) * 365 * SECONDS_PER_DAY;
17800Sstevel@tonic-gate 		else
17810Sstevel@tonic-gate 			ts.tv_sec = approx;
17820Sstevel@tonic-gate 		ts.tv_nsec = 0;
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 		/*
17850Sstevel@tonic-gate 		 * Attempt to write the new time to the TOD chip.  Set spl high
17860Sstevel@tonic-gate 		 * to avoid getting preempted between the tod_set and tod_get.
17870Sstevel@tonic-gate 		 */
17880Sstevel@tonic-gate 		spl = splhi();
17890Sstevel@tonic-gate 		tod_set(ts);
17900Sstevel@tonic-gate 		tmp = tod_get();
17910Sstevel@tonic-gate 		splx(spl);
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 		if (tmp.tv_sec != ts.tv_sec && tmp.tv_sec != ts.tv_sec + 1) {
17940Sstevel@tonic-gate 			tod_broken = 1;
17950Sstevel@tonic-gate 			dosynctodr = 0;
17960Sstevel@tonic-gate 			cmn_err(CE_WARN, "Time-of-day chip unresponsive;"
17970Sstevel@tonic-gate 			    " dead batteries?");
17980Sstevel@tonic-gate 		} else {
17990Sstevel@tonic-gate 			cmn_err(CE_WARN, "Time-of-day chip had "
18000Sstevel@tonic-gate 			    "incorrect date; check and reset.");
18010Sstevel@tonic-gate 		}
18020Sstevel@tonic-gate 		set_clock = 1;
18030Sstevel@tonic-gate 	}
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 	if (!boot_time) {
18060Sstevel@tonic-gate 		boot_time = ts.tv_sec;
18070Sstevel@tonic-gate 		set_clock = 1;
18080Sstevel@tonic-gate 	}
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 	if (set_clock)
18110Sstevel@tonic-gate 		set_hrestime(&ts);
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	mutex_exit(&tod_lock);
18140Sstevel@tonic-gate }
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate int	timechanged;	/* for testing if the system time has been reset */
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate void
18190Sstevel@tonic-gate set_hrestime(timestruc_t *ts)
18200Sstevel@tonic-gate {
18210Sstevel@tonic-gate 	int spl = hr_clock_lock();
18220Sstevel@tonic-gate 	hrestime = *ts;
18230Sstevel@tonic-gate 	membar_enter();	/* hrestime must be visible before timechanged++ */
18240Sstevel@tonic-gate 	timedelta = 0;
18250Sstevel@tonic-gate 	timechanged++;
18260Sstevel@tonic-gate 	hr_clock_unlock(spl);
18270Sstevel@tonic-gate }
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate static uint_t deadman_seconds;
18300Sstevel@tonic-gate static uint32_t deadman_panics;
18310Sstevel@tonic-gate static int deadman_enabled = 0;
18320Sstevel@tonic-gate static int deadman_panic_timers = 1;
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate static void
18350Sstevel@tonic-gate deadman(void)
18360Sstevel@tonic-gate {
18370Sstevel@tonic-gate 	if (panicstr) {
18380Sstevel@tonic-gate 		/*
18390Sstevel@tonic-gate 		 * During panic, other CPUs besides the panic
18400Sstevel@tonic-gate 		 * master continue to handle cyclics and some other
18410Sstevel@tonic-gate 		 * interrupts.  The code below is intended to be
18420Sstevel@tonic-gate 		 * single threaded, so any CPU other than the master
18430Sstevel@tonic-gate 		 * must keep out.
18440Sstevel@tonic-gate 		 */
18450Sstevel@tonic-gate 		if (CPU->cpu_id != panic_cpu.cpu_id)
18460Sstevel@tonic-gate 			return;
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 		/*
18490Sstevel@tonic-gate 		 * If we're panicking, the deadman cyclic continues to increase
18500Sstevel@tonic-gate 		 * lbolt in case the dump device driver relies on this for
18510Sstevel@tonic-gate 		 * timeouts.  Note that we rely on deadman() being invoked once
18520Sstevel@tonic-gate 		 * per second, and credit lbolt and lbolt64 with hz ticks each.
18530Sstevel@tonic-gate 		 */
18540Sstevel@tonic-gate 		lbolt += hz;
18550Sstevel@tonic-gate 		lbolt64 += hz;
18560Sstevel@tonic-gate 
1857*2036Swentaoy #ifdef __sparc
1858*2036Swentaoy 		watchdog_pat();
1859*2036Swentaoy #endif
1860*2036Swentaoy 
18610Sstevel@tonic-gate 		if (!deadman_panic_timers)
18620Sstevel@tonic-gate 			return; /* allow all timers to be manually disabled */
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 		/*
18650Sstevel@tonic-gate 		 * If we are generating a crash dump or syncing filesystems and
18660Sstevel@tonic-gate 		 * the corresponding timer is set, decrement it and re-enter
18670Sstevel@tonic-gate 		 * the panic code to abort it and advance to the next state.
18680Sstevel@tonic-gate 		 * The panic states and triggers are explained in panic.c.
18690Sstevel@tonic-gate 		 */
18700Sstevel@tonic-gate 		if (panic_dump) {
18710Sstevel@tonic-gate 			if (dump_timeleft && (--dump_timeleft == 0)) {
18720Sstevel@tonic-gate 				panic("panic dump timeout");
18730Sstevel@tonic-gate 				/*NOTREACHED*/
18740Sstevel@tonic-gate 			}
18750Sstevel@tonic-gate 		} else if (panic_sync) {
18760Sstevel@tonic-gate 			if (sync_timeleft && (--sync_timeleft == 0)) {
18770Sstevel@tonic-gate 				panic("panic sync timeout");
18780Sstevel@tonic-gate 				/*NOTREACHED*/
18790Sstevel@tonic-gate 			}
18800Sstevel@tonic-gate 		}
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 		return;
18830Sstevel@tonic-gate 	}
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	if (lbolt != CPU->cpu_deadman_lbolt) {
18860Sstevel@tonic-gate 		CPU->cpu_deadman_lbolt = lbolt;
18870Sstevel@tonic-gate 		CPU->cpu_deadman_countdown = deadman_seconds;
18880Sstevel@tonic-gate 		return;
18890Sstevel@tonic-gate 	}
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 	if (CPU->cpu_deadman_countdown-- > 0)
18920Sstevel@tonic-gate 		return;
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	/*
18950Sstevel@tonic-gate 	 * Regardless of whether or not we actually bring the system down,
18960Sstevel@tonic-gate 	 * bump the deadman_panics variable.
18970Sstevel@tonic-gate 	 *
18980Sstevel@tonic-gate 	 * N.B. deadman_panics is incremented once for each CPU that
18990Sstevel@tonic-gate 	 * passes through here.  It's expected that all the CPUs will
19000Sstevel@tonic-gate 	 * detect this condition within one second of each other, so
19010Sstevel@tonic-gate 	 * when deadman_enabled is off, deadman_panics will
19020Sstevel@tonic-gate 	 * typically be a multiple of the total number of CPUs in
19030Sstevel@tonic-gate 	 * the system.
19040Sstevel@tonic-gate 	 */
19050Sstevel@tonic-gate 	atomic_add_32(&deadman_panics, 1);
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 	if (!deadman_enabled) {
19080Sstevel@tonic-gate 		CPU->cpu_deadman_countdown = deadman_seconds;
19090Sstevel@tonic-gate 		return;
19100Sstevel@tonic-gate 	}
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	/*
19130Sstevel@tonic-gate 	 * If we're here, we want to bring the system down.
19140Sstevel@tonic-gate 	 */
19150Sstevel@tonic-gate 	panic("deadman: timed out after %d seconds of clock "
19160Sstevel@tonic-gate 	    "inactivity", deadman_seconds);
19170Sstevel@tonic-gate 	/*NOTREACHED*/
19180Sstevel@tonic-gate }
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate /*ARGSUSED*/
19210Sstevel@tonic-gate static void
19220Sstevel@tonic-gate deadman_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
19230Sstevel@tonic-gate {
19240Sstevel@tonic-gate 	cpu->cpu_deadman_lbolt = 0;
19250Sstevel@tonic-gate 	cpu->cpu_deadman_countdown = deadman_seconds;
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 	hdlr->cyh_func = (cyc_func_t)deadman;
19280Sstevel@tonic-gate 	hdlr->cyh_level = CY_HIGH_LEVEL;
19290Sstevel@tonic-gate 	hdlr->cyh_arg = NULL;
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 	/*
19320Sstevel@tonic-gate 	 * Stagger the CPUs so that they don't all run deadman() at
19330Sstevel@tonic-gate 	 * the same time.  Simplest reason to do this is to make it
19340Sstevel@tonic-gate 	 * more likely that only one CPU will panic in case of a
19350Sstevel@tonic-gate 	 * timeout.  This is (strictly speaking) an aesthetic, not a
19360Sstevel@tonic-gate 	 * technical consideration.
19370Sstevel@tonic-gate 	 *
19380Sstevel@tonic-gate 	 * The interval must be one second in accordance with the
19390Sstevel@tonic-gate 	 * code in deadman() above to increase lbolt during panic.
19400Sstevel@tonic-gate 	 */
19410Sstevel@tonic-gate 	when->cyt_when = cpu->cpu_id * (NANOSEC / NCPU);
19420Sstevel@tonic-gate 	when->cyt_interval = NANOSEC;
19430Sstevel@tonic-gate }
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate void
19470Sstevel@tonic-gate deadman_init(void)
19480Sstevel@tonic-gate {
19490Sstevel@tonic-gate 	cyc_omni_handler_t hdlr;
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 	if (deadman_seconds == 0)
19520Sstevel@tonic-gate 		deadman_seconds = snoop_interval / MICROSEC;
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	if (snooping)
19550Sstevel@tonic-gate 		deadman_enabled = 1;
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	hdlr.cyo_online = deadman_online;
19580Sstevel@tonic-gate 	hdlr.cyo_offline = NULL;
19590Sstevel@tonic-gate 	hdlr.cyo_arg = NULL;
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
19620Sstevel@tonic-gate 	deadman_cyclic = cyclic_add_omni(&hdlr);
19630Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
19640Sstevel@tonic-gate }
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate /*
19670Sstevel@tonic-gate  * tod_fault() is for updating tod validate mechanism state:
19680Sstevel@tonic-gate  * (1) TOD_NOFAULT: for resetting the state to 'normal'.
19690Sstevel@tonic-gate  *     currently used for debugging only
19700Sstevel@tonic-gate  * (2) The following four cases detected by tod validate mechanism:
19710Sstevel@tonic-gate  *       TOD_REVERSED: current tod value is less than previous value.
19720Sstevel@tonic-gate  *       TOD_STALLED: current tod value hasn't advanced.
19730Sstevel@tonic-gate  *       TOD_JUMPED: current tod value advanced too far from previous value.
19740Sstevel@tonic-gate  *       TOD_RATECHANGED: the ratio between average tod delta and
19750Sstevel@tonic-gate  *       average tick delta has changed.
19760Sstevel@tonic-gate  */
19770Sstevel@tonic-gate enum tod_fault_type
19780Sstevel@tonic-gate tod_fault(enum tod_fault_type ftype, int off)
19790Sstevel@tonic-gate {
19800Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	if (tod_faulted != ftype) {
19830Sstevel@tonic-gate 		switch (ftype) {
19840Sstevel@tonic-gate 		case TOD_NOFAULT:
198578Sae112802 			plat_tod_fault(TOD_NOFAULT);
19860Sstevel@tonic-gate 			cmn_err(CE_NOTE, "Restarted tracking "
19870Sstevel@tonic-gate 					"Time of Day clock.");
19880Sstevel@tonic-gate 			tod_faulted = ftype;
19890Sstevel@tonic-gate 			break;
19900Sstevel@tonic-gate 		case TOD_REVERSED:
19910Sstevel@tonic-gate 		case TOD_JUMPED:
19920Sstevel@tonic-gate 			if (tod_faulted == TOD_NOFAULT) {
199378Sae112802 				plat_tod_fault(ftype);
19940Sstevel@tonic-gate 				cmn_err(CE_WARN, "Time of Day clock error: "
19950Sstevel@tonic-gate 				    "reason [%s by 0x%x]. -- "
19960Sstevel@tonic-gate 				    " Stopped tracking Time Of Day clock.",
19970Sstevel@tonic-gate 				    tod_fault_table[ftype], off);
19980Sstevel@tonic-gate 				tod_faulted = ftype;
19990Sstevel@tonic-gate 			}
20000Sstevel@tonic-gate 			break;
20010Sstevel@tonic-gate 		case TOD_STALLED:
20020Sstevel@tonic-gate 		case TOD_RATECHANGED:
20030Sstevel@tonic-gate 			if (tod_faulted == TOD_NOFAULT) {
200478Sae112802 				plat_tod_fault(ftype);
20050Sstevel@tonic-gate 				cmn_err(CE_WARN, "Time of Day clock error: "
20060Sstevel@tonic-gate 				    "reason [%s]. -- "
20070Sstevel@tonic-gate 				    " Stopped tracking Time Of Day clock.",
20080Sstevel@tonic-gate 				    tod_fault_table[ftype]);
20090Sstevel@tonic-gate 				tod_faulted = ftype;
20100Sstevel@tonic-gate 			}
20110Sstevel@tonic-gate 			break;
20120Sstevel@tonic-gate 		default:
20130Sstevel@tonic-gate 			break;
20140Sstevel@tonic-gate 		}
20150Sstevel@tonic-gate 	}
20160Sstevel@tonic-gate 	return (tod_faulted);
20170Sstevel@tonic-gate }
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate void
20200Sstevel@tonic-gate tod_fault_reset()
20210Sstevel@tonic-gate {
20220Sstevel@tonic-gate 	tod_fault_reset_flag = 1;
20230Sstevel@tonic-gate }
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate /*
20270Sstevel@tonic-gate  * tod_validate() is used for checking values returned by tod_get().
20280Sstevel@tonic-gate  * Four error cases can be detected by this routine:
20290Sstevel@tonic-gate  *   TOD_REVERSED: current tod value is less than previous.
20300Sstevel@tonic-gate  *   TOD_STALLED: current tod value hasn't advanced.
20310Sstevel@tonic-gate  *   TOD_JUMPED: current tod value advanced too far from previous value.
20320Sstevel@tonic-gate  *   TOD_RATECHANGED: the ratio between average tod delta and
20330Sstevel@tonic-gate  *   average tick delta has changed.
20340Sstevel@tonic-gate  */
20350Sstevel@tonic-gate time_t
20360Sstevel@tonic-gate tod_validate(time_t tod)
20370Sstevel@tonic-gate {
20380Sstevel@tonic-gate 	time_t diff_tod;
20390Sstevel@tonic-gate 	hrtime_t diff_tick;
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 	long dtick;
20420Sstevel@tonic-gate 	int dtick_delta;
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	int off = 0;
20450Sstevel@tonic-gate 	enum tod_fault_type tod_bad = TOD_NOFAULT;
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 	static int firsttime = 1;
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 	static time_t prev_tod = 0;
20500Sstevel@tonic-gate 	static hrtime_t prev_tick = 0;
20510Sstevel@tonic-gate 	static long dtick_avg = TOD_REF_FREQ;
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	hrtime_t tick = gethrtime();
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 	/*
20580Sstevel@tonic-gate 	 * tod_validate_enable is patchable via /etc/system.
2059950Ssethg 	 * If TOD is already faulted, or if TOD validation is deferred,
2060950Ssethg 	 * there is nothing to do.
20610Sstevel@tonic-gate 	 */
2062950Ssethg 	if ((tod_validate_enable == 0) || (tod_faulted != TOD_NOFAULT) ||
2063950Ssethg 	    tod_validate_deferred) {
20640Sstevel@tonic-gate 		return (tod);
20650Sstevel@tonic-gate 	}
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	/*
20680Sstevel@tonic-gate 	 * Update prev_tod and prev_tick values for first run
20690Sstevel@tonic-gate 	 */
20700Sstevel@tonic-gate 	if (firsttime) {
20710Sstevel@tonic-gate 		firsttime = 0;
20720Sstevel@tonic-gate 		prev_tod = tod;
20730Sstevel@tonic-gate 		prev_tick = tick;
20740Sstevel@tonic-gate 		return (tod);
20750Sstevel@tonic-gate 	}
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	/*
20780Sstevel@tonic-gate 	 * For either of these conditions, we need to reset ourself
20790Sstevel@tonic-gate 	 * and start validation from zero since each condition
20800Sstevel@tonic-gate 	 * indicates that the TOD will be updated with new value
20810Sstevel@tonic-gate 	 * Also, note that tod_needsync will be reset in clock()
20820Sstevel@tonic-gate 	 */
20830Sstevel@tonic-gate 	if (tod_needsync || tod_fault_reset_flag) {
20840Sstevel@tonic-gate 		firsttime = 1;
20850Sstevel@tonic-gate 		prev_tod = 0;
20860Sstevel@tonic-gate 		prev_tick = 0;
20870Sstevel@tonic-gate 		dtick_avg = TOD_REF_FREQ;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 		if (tod_fault_reset_flag)
20900Sstevel@tonic-gate 			tod_fault_reset_flag = 0;
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 		return (tod);
20930Sstevel@tonic-gate 	}
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 	/* test hook */
20960Sstevel@tonic-gate 	switch (tod_unit_test) {
20970Sstevel@tonic-gate 	case 1: /* for testing jumping tod */
20980Sstevel@tonic-gate 		tod += tod_test_injector;
20990Sstevel@tonic-gate 		tod_unit_test = 0;
21000Sstevel@tonic-gate 		break;
21010Sstevel@tonic-gate 	case 2:	/* for testing stuck tod bit */
21020Sstevel@tonic-gate 		tod |= 1 << tod_test_injector;
21030Sstevel@tonic-gate 		tod_unit_test = 0;
21040Sstevel@tonic-gate 		break;
21050Sstevel@tonic-gate 	case 3:	/* for testing stalled tod */
21060Sstevel@tonic-gate 		tod = prev_tod;
21070Sstevel@tonic-gate 		tod_unit_test = 0;
21080Sstevel@tonic-gate 		break;
21090Sstevel@tonic-gate 	case 4:	/* reset tod fault status */
21100Sstevel@tonic-gate 		(void) tod_fault(TOD_NOFAULT, 0);
21110Sstevel@tonic-gate 		tod_unit_test = 0;
21120Sstevel@tonic-gate 		break;
21130Sstevel@tonic-gate 	default:
21140Sstevel@tonic-gate 		break;
21150Sstevel@tonic-gate 	}
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	diff_tod = tod - prev_tod;
21180Sstevel@tonic-gate 	diff_tick = tick - prev_tick;
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 	ASSERT(diff_tick >= 0);
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	if (diff_tod < 0) {
21230Sstevel@tonic-gate 		/* ERROR - tod reversed */
21240Sstevel@tonic-gate 		tod_bad = TOD_REVERSED;
21250Sstevel@tonic-gate 		off = (int)(prev_tod - tod);
21260Sstevel@tonic-gate 	} else if (diff_tod == 0) {
21270Sstevel@tonic-gate 		/* tod did not advance */
21280Sstevel@tonic-gate 		if (diff_tick > TOD_STALL_THRESHOLD) {
21290Sstevel@tonic-gate 			/* ERROR - tod stalled */
21300Sstevel@tonic-gate 			tod_bad = TOD_STALLED;
21310Sstevel@tonic-gate 		} else {
21320Sstevel@tonic-gate 			/*
21330Sstevel@tonic-gate 			 * Make sure we don't update prev_tick
21340Sstevel@tonic-gate 			 * so that diff_tick is calculated since
21350Sstevel@tonic-gate 			 * the first diff_tod == 0
21360Sstevel@tonic-gate 			 */
21370Sstevel@tonic-gate 			return (tod);
21380Sstevel@tonic-gate 		}
21390Sstevel@tonic-gate 	} else {
21400Sstevel@tonic-gate 		/* calculate dtick */
21410Sstevel@tonic-gate 		dtick = diff_tick / diff_tod;
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 		/* update dtick averages */
21440Sstevel@tonic-gate 		dtick_avg += ((dtick - dtick_avg) / TOD_FILTER_N);
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 		/*
21470Sstevel@tonic-gate 		 * Calculate dtick_delta as
21480Sstevel@tonic-gate 		 * variation from reference freq in quartiles
21490Sstevel@tonic-gate 		 */
21500Sstevel@tonic-gate 		dtick_delta = (dtick_avg - TOD_REF_FREQ) /
21510Sstevel@tonic-gate 			(TOD_REF_FREQ >> 2);
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 		/*
21540Sstevel@tonic-gate 		 * Even with a perfectly functioning TOD device,
21550Sstevel@tonic-gate 		 * when the number of elapsed seconds is low the
21560Sstevel@tonic-gate 		 * algorithm can calculate a rate that is beyond
21570Sstevel@tonic-gate 		 * tolerance, causing an error.  The algorithm is
21580Sstevel@tonic-gate 		 * inaccurate when elapsed time is low (less than
21590Sstevel@tonic-gate 		 * 5 seconds).
21600Sstevel@tonic-gate 		 */
21610Sstevel@tonic-gate 		if (diff_tod > 4) {
21620Sstevel@tonic-gate 			if (dtick < TOD_JUMP_THRESHOLD) {
21630Sstevel@tonic-gate 				/* ERROR - tod jumped */
21640Sstevel@tonic-gate 				tod_bad = TOD_JUMPED;
21650Sstevel@tonic-gate 				off = (int)diff_tod;
21660Sstevel@tonic-gate 			} else if (dtick_delta) {
21670Sstevel@tonic-gate 				/* ERROR - change in clock rate */
21680Sstevel@tonic-gate 				tod_bad = TOD_RATECHANGED;
21690Sstevel@tonic-gate 			}
21700Sstevel@tonic-gate 		}
21710Sstevel@tonic-gate 	}
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 	if (tod_bad != TOD_NOFAULT) {
21740Sstevel@tonic-gate 		(void) tod_fault(tod_bad, off);
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 		/*
21770Sstevel@tonic-gate 		 * Disable dosynctodr since we are going to fault
21780Sstevel@tonic-gate 		 * the TOD chip anyway here
21790Sstevel@tonic-gate 		 */
21800Sstevel@tonic-gate 		dosynctodr = 0;
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 		/*
21830Sstevel@tonic-gate 		 * Set tod to the correct value from hrestime
21840Sstevel@tonic-gate 		 */
21850Sstevel@tonic-gate 		tod = hrestime.tv_sec;
21860Sstevel@tonic-gate 	}
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 	prev_tod = tod;
21890Sstevel@tonic-gate 	prev_tick = tick;
21900Sstevel@tonic-gate 	return (tod);
21910Sstevel@tonic-gate }
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate static void
21940Sstevel@tonic-gate calcloadavg(int nrun, uint64_t *hp_ave)
21950Sstevel@tonic-gate {
21960Sstevel@tonic-gate 	static int64_t f[3] = { 135, 27, 9 };
21970Sstevel@tonic-gate 	uint_t i;
21980Sstevel@tonic-gate 	int64_t q, r;
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 	/*
22010Sstevel@tonic-gate 	 * Compute load average over the last 1, 5, and 15 minutes
22020Sstevel@tonic-gate 	 * (60, 300, and 900 seconds).  The constants in f[3] are for
22030Sstevel@tonic-gate 	 * exponential decay:
22040Sstevel@tonic-gate 	 * (1 - exp(-1/60)) << 13 = 135,
22050Sstevel@tonic-gate 	 * (1 - exp(-1/300)) << 13 = 27,
22060Sstevel@tonic-gate 	 * (1 - exp(-1/900)) << 13 = 9.
22070Sstevel@tonic-gate 	 */
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 	/*
22100Sstevel@tonic-gate 	 * a little hoop-jumping to avoid integer overflow
22110Sstevel@tonic-gate 	 */
22120Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
22130Sstevel@tonic-gate 		q = (hp_ave[i]  >> 16) << 7;
22140Sstevel@tonic-gate 		r = (hp_ave[i]  & 0xffff) << 7;
22150Sstevel@tonic-gate 		hp_ave[i] += ((nrun - q) * f[i] - ((r * f[i]) >> 16)) >> 4;
22160Sstevel@tonic-gate 	}
22170Sstevel@tonic-gate }
2218