xref: /titanic_52/usr/src/uts/common/os/clock.c (revision c97ad5cdc75eb73e3cc38542ca3ba783574b0a7a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53c431bb5Swentaoy  * Common Development and Distribution License (the "License").
63c431bb5Swentaoy  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26fb2f18f8Sesaxe  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/tuneable.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
397c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
407c478bd9Sstevel@tonic-gate #include <sys/user.h>
417c478bd9Sstevel@tonic-gate #include <sys/proc.h>
427c478bd9Sstevel@tonic-gate #include <sys/callo.h>
437c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
447c478bd9Sstevel@tonic-gate #include <sys/var.h>
457c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
467c478bd9Sstevel@tonic-gate #include <sys/swap.h>
477c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
487c478bd9Sstevel@tonic-gate #include <sys/class.h>
497c478bd9Sstevel@tonic-gate #include <sys/time.h>
507c478bd9Sstevel@tonic-gate #include <sys/debug.h>
517c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
527c478bd9Sstevel@tonic-gate #include <sys/spl.h>
537c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
547c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
557c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
567c478bd9Sstevel@tonic-gate #include <sys/fs/swapnode.h>
577c478bd9Sstevel@tonic-gate #include <sys/panic.h>
587c478bd9Sstevel@tonic-gate #include <sys/disp.h>
597c478bd9Sstevel@tonic-gate #include <sys/msacct.h>
607c478bd9Sstevel@tonic-gate #include <sys/mem_cage.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <vm/page.h>
637c478bd9Sstevel@tonic-gate #include <vm/anon.h>
647c478bd9Sstevel@tonic-gate #include <vm/rm.h>
657c478bd9Sstevel@tonic-gate #include <sys/cyclic.h>
667c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
677c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
687c478bd9Sstevel@tonic-gate #include <sys/task.h>
697c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
707c478bd9Sstevel@tonic-gate 
713c431bb5Swentaoy #ifdef __sparc
723c431bb5Swentaoy #include <sys/wdt.h>
733c431bb5Swentaoy #endif
743c431bb5Swentaoy 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * for NTP support
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate #include <sys/timex.h>
797c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
82*c97ad5cdSakolb  * clock() is called straight from the clock cyclic; see clock_init().
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  * Functions:
857c478bd9Sstevel@tonic-gate  *	reprime clock
867c478bd9Sstevel@tonic-gate  *	schedule callouts
877c478bd9Sstevel@tonic-gate  *	maintain date
887c478bd9Sstevel@tonic-gate  *	jab the scheduler
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate extern kcondvar_t	fsflush_cv;
927c478bd9Sstevel@tonic-gate extern sysinfo_t	sysinfo;
937c478bd9Sstevel@tonic-gate extern vminfo_t	vminfo;
947c478bd9Sstevel@tonic-gate extern int	idleswtch;	/* flag set while idle in pswtch() */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * high-precision avenrun values.  These are needed to make the
987c478bd9Sstevel@tonic-gate  * regular avenrun values accurate.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate static uint64_t hp_avenrun[3];
1017c478bd9Sstevel@tonic-gate int	avenrun[3];		/* FSCALED average run queue lengths */
1027c478bd9Sstevel@tonic-gate time_t	time;	/* time in seconds since 1970 - for compatibility only */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static struct loadavg_s loadavg;
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Phase/frequency-lock loop (PLL/FLL) definitions
1077c478bd9Sstevel@tonic-gate  *
1087c478bd9Sstevel@tonic-gate  * The following variables are read and set by the ntp_adjtime() system
1097c478bd9Sstevel@tonic-gate  * call.
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  * time_state shows the state of the system clock, with values defined
1127c478bd9Sstevel@tonic-gate  * in the timex.h header file.
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * time_status shows the status of the system clock, with bits defined
1157c478bd9Sstevel@tonic-gate  * in the timex.h header file.
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * time_offset is used by the PLL/FLL to adjust the system time in small
1187c478bd9Sstevel@tonic-gate  * increments.
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  * time_constant determines the bandwidth or "stiffness" of the PLL.
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  * time_tolerance determines maximum frequency error or tolerance of the
1237c478bd9Sstevel@tonic-gate  * CPU clock oscillator and is a property of the architecture; however,
1247c478bd9Sstevel@tonic-gate  * in principle it could change as result of the presence of external
1257c478bd9Sstevel@tonic-gate  * discipline signals, for instance.
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  * time_precision is usually equal to the kernel tick variable; however,
1287c478bd9Sstevel@tonic-gate  * in cases where a precision clock counter or external clock is
1297c478bd9Sstevel@tonic-gate  * available, the resolution can be much less than this and depend on
1307c478bd9Sstevel@tonic-gate  * whether the external clock is working or not.
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  * time_maxerror is initialized by a ntp_adjtime() call and increased by
1337c478bd9Sstevel@tonic-gate  * the kernel once each second to reflect the maximum error bound
1347c478bd9Sstevel@tonic-gate  * growth.
1357c478bd9Sstevel@tonic-gate  *
1367c478bd9Sstevel@tonic-gate  * time_esterror is set and read by the ntp_adjtime() call, but
1377c478bd9Sstevel@tonic-gate  * otherwise not used by the kernel.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate int32_t time_state = TIME_OK;	/* clock state */
1407c478bd9Sstevel@tonic-gate int32_t time_status = STA_UNSYNC;	/* clock status bits */
1417c478bd9Sstevel@tonic-gate int32_t time_offset = 0;		/* time offset (us) */
1427c478bd9Sstevel@tonic-gate int32_t time_constant = 0;		/* pll time constant */
1437c478bd9Sstevel@tonic-gate int32_t time_tolerance = MAXFREQ;	/* frequency tolerance (scaled ppm) */
1447c478bd9Sstevel@tonic-gate int32_t time_precision = 1;	/* clock precision (us) */
1457c478bd9Sstevel@tonic-gate int32_t time_maxerror = MAXPHASE;	/* maximum error (us) */
1467c478bd9Sstevel@tonic-gate int32_t time_esterror = MAXPHASE;	/* estimated error (us) */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate  * The following variables establish the state of the PLL/FLL and the
1507c478bd9Sstevel@tonic-gate  * residual time and frequency offset of the local clock. The scale
1517c478bd9Sstevel@tonic-gate  * factors are defined in the timex.h header file.
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  * time_phase and time_freq are the phase increment and the frequency
1547c478bd9Sstevel@tonic-gate  * increment, respectively, of the kernel time variable.
1557c478bd9Sstevel@tonic-gate  *
1567c478bd9Sstevel@tonic-gate  * time_freq is set via ntp_adjtime() from a value stored in a file when
1577c478bd9Sstevel@tonic-gate  * the synchronization daemon is first started. Its value is retrieved
1587c478bd9Sstevel@tonic-gate  * via ntp_adjtime() and written to the file about once per hour by the
1597c478bd9Sstevel@tonic-gate  * daemon.
1607c478bd9Sstevel@tonic-gate  *
1617c478bd9Sstevel@tonic-gate  * time_adj is the adjustment added to the value of tick at each timer
1627c478bd9Sstevel@tonic-gate  * interrupt and is recomputed from time_phase and time_freq at each
1637c478bd9Sstevel@tonic-gate  * seconds rollover.
1647c478bd9Sstevel@tonic-gate  *
1657c478bd9Sstevel@tonic-gate  * time_reftime is the second's portion of the system time at the last
1667c478bd9Sstevel@tonic-gate  * call to ntp_adjtime(). It is used to adjust the time_freq variable
1677c478bd9Sstevel@tonic-gate  * and to increase the time_maxerror as the time since last update
1687c478bd9Sstevel@tonic-gate  * increases.
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate int32_t time_phase = 0;		/* phase offset (scaled us) */
1717c478bd9Sstevel@tonic-gate int32_t time_freq = 0;		/* frequency offset (scaled ppm) */
1727c478bd9Sstevel@tonic-gate int32_t time_adj = 0;		/* tick adjust (scaled 1 / hz) */
1737c478bd9Sstevel@tonic-gate int32_t time_reftime = 0;		/* time at last adjustment (s) */
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * The scale factors of the following variables are defined in the
1777c478bd9Sstevel@tonic-gate  * timex.h header file.
1787c478bd9Sstevel@tonic-gate  *
1797c478bd9Sstevel@tonic-gate  * pps_time contains the time at each calibration interval, as read by
1807c478bd9Sstevel@tonic-gate  * microtime(). pps_count counts the seconds of the calibration
1817c478bd9Sstevel@tonic-gate  * interval, the duration of which is nominally pps_shift in powers of
1827c478bd9Sstevel@tonic-gate  * two.
1837c478bd9Sstevel@tonic-gate  *
1847c478bd9Sstevel@tonic-gate  * pps_offset is the time offset produced by the time median filter
1857c478bd9Sstevel@tonic-gate  * pps_tf[], while pps_jitter is the dispersion (jitter) measured by
1867c478bd9Sstevel@tonic-gate  * this filter.
1877c478bd9Sstevel@tonic-gate  *
1887c478bd9Sstevel@tonic-gate  * pps_freq is the frequency offset produced by the frequency median
1897c478bd9Sstevel@tonic-gate  * filter pps_ff[], while pps_stabil is the dispersion (wander) measured
1907c478bd9Sstevel@tonic-gate  * by this filter.
1917c478bd9Sstevel@tonic-gate  *
1927c478bd9Sstevel@tonic-gate  * pps_usec is latched from a high resolution counter or external clock
1937c478bd9Sstevel@tonic-gate  * at pps_time. Here we want the hardware counter contents only, not the
1947c478bd9Sstevel@tonic-gate  * contents plus the time_tv.usec as usual.
1957c478bd9Sstevel@tonic-gate  *
1967c478bd9Sstevel@tonic-gate  * pps_valid counts the number of seconds since the last PPS update. It
1977c478bd9Sstevel@tonic-gate  * is used as a watchdog timer to disable the PPS discipline should the
1987c478bd9Sstevel@tonic-gate  * PPS signal be lost.
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * pps_glitch counts the number of seconds since the beginning of an
2017c478bd9Sstevel@tonic-gate  * offset burst more than tick/2 from current nominal offset. It is used
2027c478bd9Sstevel@tonic-gate  * mainly to suppress error bursts due to priority conflicts between the
2037c478bd9Sstevel@tonic-gate  * PPS interrupt and timer interrupt.
2047c478bd9Sstevel@tonic-gate  *
2057c478bd9Sstevel@tonic-gate  * pps_intcnt counts the calibration intervals for use in the interval-
2067c478bd9Sstevel@tonic-gate  * adaptation algorithm. It's just too complicated for words.
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate struct timeval pps_time;	/* kernel time at last interval */
2097c478bd9Sstevel@tonic-gate int32_t pps_tf[] = {0, 0, 0};	/* pps time offset median filter (us) */
2107c478bd9Sstevel@tonic-gate int32_t pps_offset = 0;		/* pps time offset (us) */
2117c478bd9Sstevel@tonic-gate int32_t pps_jitter = MAXTIME;	/* time dispersion (jitter) (us) */
2127c478bd9Sstevel@tonic-gate int32_t pps_ff[] = {0, 0, 0};	/* pps frequency offset median filter */
2137c478bd9Sstevel@tonic-gate int32_t pps_freq = 0;		/* frequency offset (scaled ppm) */
2147c478bd9Sstevel@tonic-gate int32_t pps_stabil = MAXFREQ;	/* frequency dispersion (scaled ppm) */
2157c478bd9Sstevel@tonic-gate int32_t pps_usec = 0;		/* microsec counter at last interval */
2167c478bd9Sstevel@tonic-gate int32_t pps_valid = PPS_VALID;	/* pps signal watchdog counter */
2177c478bd9Sstevel@tonic-gate int32_t pps_glitch = 0;		/* pps signal glitch counter */
2187c478bd9Sstevel@tonic-gate int32_t pps_count = 0;		/* calibration interval counter (s) */
2197c478bd9Sstevel@tonic-gate int32_t pps_shift = PPS_SHIFT;	/* interval duration (s) (shift) */
2207c478bd9Sstevel@tonic-gate int32_t pps_intcnt = 0;		/* intervals at current duration */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * PPS signal quality monitors
2247c478bd9Sstevel@tonic-gate  *
2257c478bd9Sstevel@tonic-gate  * pps_jitcnt counts the seconds that have been discarded because the
2267c478bd9Sstevel@tonic-gate  * jitter measured by the time median filter exceeds the limit MAXTIME
2277c478bd9Sstevel@tonic-gate  * (100 us).
2287c478bd9Sstevel@tonic-gate  *
2297c478bd9Sstevel@tonic-gate  * pps_calcnt counts the frequency calibration intervals, which are
2307c478bd9Sstevel@tonic-gate  * variable from 4 s to 256 s.
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  * pps_errcnt counts the calibration intervals which have been discarded
2337c478bd9Sstevel@tonic-gate  * because the wander exceeds the limit MAXFREQ (100 ppm) or where the
2347c478bd9Sstevel@tonic-gate  * calibration interval jitter exceeds two ticks.
2357c478bd9Sstevel@tonic-gate  *
2367c478bd9Sstevel@tonic-gate  * pps_stbcnt counts the calibration intervals that have been discarded
2377c478bd9Sstevel@tonic-gate  * because the frequency wander exceeds the limit MAXFREQ / 4 (25 us).
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate int32_t pps_jitcnt = 0;		/* jitter limit exceeded */
2407c478bd9Sstevel@tonic-gate int32_t pps_calcnt = 0;		/* calibration intervals */
2417c478bd9Sstevel@tonic-gate int32_t pps_errcnt = 0;		/* calibration errors */
2427c478bd9Sstevel@tonic-gate int32_t pps_stbcnt = 0;		/* stability limit exceeded */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /* The following variables require no explicit locking */
2457c478bd9Sstevel@tonic-gate volatile clock_t lbolt;		/* time in Hz since last boot */
2467c478bd9Sstevel@tonic-gate volatile int64_t lbolt64;	/* lbolt64 won't wrap for 2.9 billion yrs */
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate kcondvar_t lbolt_cv;
2497c478bd9Sstevel@tonic-gate int one_sec = 1; /* turned on once every second */
2507c478bd9Sstevel@tonic-gate static int fsflushcnt;	/* counter for t_fsflushr */
2517c478bd9Sstevel@tonic-gate int	dosynctodr = 1;	/* patchable; enable/disable sync to TOD chip */
2527c478bd9Sstevel@tonic-gate int	tod_needsync = 0;	/* need to sync tod chip with software time */
2537c478bd9Sstevel@tonic-gate static int tod_broken = 0;	/* clock chip doesn't work */
2547c478bd9Sstevel@tonic-gate time_t	boot_time = 0;		/* Boot time in seconds since 1970 */
2557c478bd9Sstevel@tonic-gate cyclic_id_t clock_cyclic;	/* clock()'s cyclic_id */
2567c478bd9Sstevel@tonic-gate cyclic_id_t deadman_cyclic;	/* deadman()'s cyclic_id */
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate static int lgrp_ticks;		/* counter to schedule lgrp load calcs */
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * for tod fault detection
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate #define	TOD_REF_FREQ		((longlong_t)(NANOSEC))
2647c478bd9Sstevel@tonic-gate #define	TOD_STALL_THRESHOLD	(TOD_REF_FREQ * 3 / 2)
2657c478bd9Sstevel@tonic-gate #define	TOD_JUMP_THRESHOLD	(TOD_REF_FREQ / 2)
2667c478bd9Sstevel@tonic-gate #define	TOD_FILTER_N		4
2677c478bd9Sstevel@tonic-gate #define	TOD_FILTER_SETTLE	(4 * TOD_FILTER_N)
2687c478bd9Sstevel@tonic-gate static int tod_faulted = TOD_NOFAULT;
2697c478bd9Sstevel@tonic-gate static int tod_fault_reset_flag = 0;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /* patchable via /etc/system */
2727c478bd9Sstevel@tonic-gate int tod_validate_enable = 1;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2752c891a87Ssethg  * On non-SPARC systems, TOD validation must be deferred until gethrtime
2762c891a87Ssethg  * returns non-zero values (after mach_clkinit's execution).
2772c891a87Ssethg  * On SPARC systems, it must be deferred until after hrtime_base
2782c891a87Ssethg  * and hres_last_tick are set (in the first invocation of hres_tick).
2792c891a87Ssethg  * Since in both cases the prerequisites occur before the invocation of
2802c891a87Ssethg  * tod_get() in clock(), the deferment is lifted there.
2812c891a87Ssethg  */
2822c891a87Ssethg static boolean_t tod_validate_deferred = B_TRUE;
2832c891a87Ssethg 
2842c891a87Ssethg /*
2857c478bd9Sstevel@tonic-gate  * tod_fault_table[] must be aligned with
2867c478bd9Sstevel@tonic-gate  * enum tod_fault_type in systm.h
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate static char *tod_fault_table[] = {
2897c478bd9Sstevel@tonic-gate 	"Reversed",			/* TOD_REVERSED */
2907c478bd9Sstevel@tonic-gate 	"Stalled",			/* TOD_STALLED */
2917c478bd9Sstevel@tonic-gate 	"Jumped",			/* TOD_JUMPED */
2927c478bd9Sstevel@tonic-gate 	"Changed in Clock Rate"		/* TOD_RATECHANGED */
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * no strings needed for TOD_NOFAULT
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate };
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate /*
2997c478bd9Sstevel@tonic-gate  * test hook for tod broken detection in tod_validate
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate int tod_unit_test = 0;
3027c478bd9Sstevel@tonic-gate time_t tod_test_injector;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate #define	CLOCK_ADJ_HIST_SIZE	4
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static int	adj_hist_entry;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate int64_t clock_adj_hist[CLOCK_ADJ_HIST_SIZE];
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate static void clock_tick(kthread_t *);
3117c478bd9Sstevel@tonic-gate static void calcloadavg(int, uint64_t *);
3127c478bd9Sstevel@tonic-gate static int genloadavg(struct loadavg_s *);
3137c478bd9Sstevel@tonic-gate static void loadavg_update();
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate void (*cmm_clock_callout)() = NULL;
316*c97ad5cdSakolb void (*cpucaps_clock_callout)() = NULL;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static void
3197c478bd9Sstevel@tonic-gate clock(void)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	kthread_t	*t;
3227c478bd9Sstevel@tonic-gate 	kmutex_t	*plockp;	/* pointer to thread's process lock */
3237c478bd9Sstevel@tonic-gate 	int	pinned_intr = 0;
3247c478bd9Sstevel@tonic-gate 	uint_t	nrunnable, nrunning;
3257c478bd9Sstevel@tonic-gate 	uint_t	w_io;
3267c478bd9Sstevel@tonic-gate 	cpu_t	*cp;
3277c478bd9Sstevel@tonic-gate 	cpupart_t *cpupart;
3287c478bd9Sstevel@tonic-gate 	int	exiting;
3297c478bd9Sstevel@tonic-gate 	extern void set_anoninfo();
3307c478bd9Sstevel@tonic-gate 	extern	void	set_freemem();
3317c478bd9Sstevel@tonic-gate 	void	(*funcp)();
3327c478bd9Sstevel@tonic-gate 	int32_t ltemp;
3337c478bd9Sstevel@tonic-gate 	int64_t lltemp;
3347c478bd9Sstevel@tonic-gate 	int s;
3357c478bd9Sstevel@tonic-gate 	int do_lgrp_load;
3367c478bd9Sstevel@tonic-gate 	int i;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if (panicstr)
3397c478bd9Sstevel@tonic-gate 		return;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	set_anoninfo();
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * Make sure that 'freemem' do not drift too far from the truth
3447c478bd9Sstevel@tonic-gate 	 */
3457c478bd9Sstevel@tonic-gate 	set_freemem();
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * Before the section which is repeated is executed, we do
3507c478bd9Sstevel@tonic-gate 	 * the time delta processing which occurs every clock tick
3517c478bd9Sstevel@tonic-gate 	 *
3527c478bd9Sstevel@tonic-gate 	 * There is additional processing which happens every time
3537c478bd9Sstevel@tonic-gate 	 * the nanosecond counter rolls over which is described
3547c478bd9Sstevel@tonic-gate 	 * below - see the section which begins with : if (one_sec)
3557c478bd9Sstevel@tonic-gate 	 *
3567c478bd9Sstevel@tonic-gate 	 * This section marks the beginning of the precision-kernel
3577c478bd9Sstevel@tonic-gate 	 * code fragment.
3587c478bd9Sstevel@tonic-gate 	 *
3597c478bd9Sstevel@tonic-gate 	 * First, compute the phase adjustment. If the low-order bits
3607c478bd9Sstevel@tonic-gate 	 * (time_phase) of the update overflow, bump the higher order
3617c478bd9Sstevel@tonic-gate 	 * bits (time_update).
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 	time_phase += time_adj;
3647c478bd9Sstevel@tonic-gate 	if (time_phase <= -FINEUSEC) {
3657c478bd9Sstevel@tonic-gate 		ltemp = -time_phase / SCALE_PHASE;
3667c478bd9Sstevel@tonic-gate 		time_phase += ltemp * SCALE_PHASE;
3677c478bd9Sstevel@tonic-gate 		s = hr_clock_lock();
3687c478bd9Sstevel@tonic-gate 		timedelta -= ltemp * (NANOSEC/MICROSEC);
3697c478bd9Sstevel@tonic-gate 		hr_clock_unlock(s);
3707c478bd9Sstevel@tonic-gate 	} else if (time_phase >= FINEUSEC) {
3717c478bd9Sstevel@tonic-gate 		ltemp = time_phase / SCALE_PHASE;
3727c478bd9Sstevel@tonic-gate 		time_phase -= ltemp * SCALE_PHASE;
3737c478bd9Sstevel@tonic-gate 		s = hr_clock_lock();
3747c478bd9Sstevel@tonic-gate 		timedelta += ltemp * (NANOSEC/MICROSEC);
3757c478bd9Sstevel@tonic-gate 		hr_clock_unlock(s);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * End of precision-kernel code fragment which is processed
3807c478bd9Sstevel@tonic-gate 	 * every timer interrupt.
3817c478bd9Sstevel@tonic-gate 	 *
3827c478bd9Sstevel@tonic-gate 	 * Continue with the interrupt processing as scheduled.
3837c478bd9Sstevel@tonic-gate 	 *
3847c478bd9Sstevel@tonic-gate 	 * Did we pin another interrupt thread?  Need to check this before
3857c478bd9Sstevel@tonic-gate 	 * grabbing any adaptive locks, since if we block on a lock the
3867c478bd9Sstevel@tonic-gate 	 * pinned thread could escape.  Note that this is just a heuristic;
3877c478bd9Sstevel@tonic-gate 	 * if we take multiple laps though clock() without returning from
3887c478bd9Sstevel@tonic-gate 	 * the interrupt because we have another clock tick pending, then
3897c478bd9Sstevel@tonic-gate 	 * the pinned interrupt could be released by one of the previous
3907c478bd9Sstevel@tonic-gate 	 * laps.  The only consequence is that the CPU will be counted as
3917c478bd9Sstevel@tonic-gate 	 * in idle (or wait) state once the pinned interrupt is released.
3927c478bd9Sstevel@tonic-gate 	 * Since this accounting is inaccurate by nature, this isn't a big
3937c478bd9Sstevel@tonic-gate 	 * deal --- but we should try to get it right in the common case
3947c478bd9Sstevel@tonic-gate 	 * where we only call clock() once per interrupt.
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate 	if (curthread->t_intr != NULL)
3977c478bd9Sstevel@tonic-gate 		pinned_intr = (curthread->t_intr->t_flag & T_INTR_THREAD);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	/*
4007c478bd9Sstevel@tonic-gate 	 * Count the number of runnable threads and the number waiting
4017c478bd9Sstevel@tonic-gate 	 * for some form of I/O to complete -- gets added to
4027c478bd9Sstevel@tonic-gate 	 * sysinfo.waiting.  To know the state of the system, must add
4037c478bd9Sstevel@tonic-gate 	 * wait counts from all CPUs.  Also add up the per-partition
4047c478bd9Sstevel@tonic-gate 	 * statistics.
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	w_io = 0;
4077c478bd9Sstevel@tonic-gate 	nrunnable = 0;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	/*
4107c478bd9Sstevel@tonic-gate 	 * keep track of when to update lgrp/part loads
4117c478bd9Sstevel@tonic-gate 	 */
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	do_lgrp_load = 0;
4147c478bd9Sstevel@tonic-gate 	if (lgrp_ticks++ >= hz / 10) {
4157c478bd9Sstevel@tonic-gate 		lgrp_ticks = 0;
4167c478bd9Sstevel@tonic-gate 		do_lgrp_load = 1;
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if (one_sec)
4207c478bd9Sstevel@tonic-gate 		loadavg_update();
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/*
4237c478bd9Sstevel@tonic-gate 	 * First count the threads waiting on kpreempt queues in each
4247c478bd9Sstevel@tonic-gate 	 * CPU partition.
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	cpupart = cp_list_head;
4287c478bd9Sstevel@tonic-gate 	do {
4297c478bd9Sstevel@tonic-gate 		uint_t cpupart_nrunnable = cpupart->cp_kp_queue.disp_nrunnable;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		cpupart->cp_updates++;
4327c478bd9Sstevel@tonic-gate 		nrunnable += cpupart_nrunnable;
4337c478bd9Sstevel@tonic-gate 		cpupart->cp_nrunnable_cum += cpupart_nrunnable;
4347c478bd9Sstevel@tonic-gate 		if (one_sec) {
4357c478bd9Sstevel@tonic-gate 			cpupart->cp_nrunning = 0;
4367c478bd9Sstevel@tonic-gate 			cpupart->cp_nrunnable = cpupart_nrunnable;
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 	} while ((cpupart = cpupart->cp_next) != cp_list_head);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	/* Now count the per-CPU statistics. */
4427c478bd9Sstevel@tonic-gate 	cp = cpu_list;
4437c478bd9Sstevel@tonic-gate 	do {
4447c478bd9Sstevel@tonic-gate 		uint_t cpu_nrunnable = cp->cpu_disp->disp_nrunnable;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		nrunnable += cpu_nrunnable;
4477c478bd9Sstevel@tonic-gate 		cpupart = cp->cpu_part;
4487c478bd9Sstevel@tonic-gate 		cpupart->cp_nrunnable_cum += cpu_nrunnable;
449ae115bc7Smrj 		if (one_sec) {
4507c478bd9Sstevel@tonic-gate 			cpupart->cp_nrunnable += cpu_nrunnable;
451ae115bc7Smrj 			/*
452ae115bc7Smrj 			 * w_io is used to update sysinfo.waiting during
453ae115bc7Smrj 			 * one_second processing below.  Only gather w_io
454ae115bc7Smrj 			 * information when we walk the list of cpus if we're
455ae115bc7Smrj 			 * going to perform one_second processing.
456ae115bc7Smrj 			 */
457ae115bc7Smrj 			w_io += CPU_STATS(cp, sys.iowait);
458ae115bc7Smrj 
459ae115bc7Smrj 		}
4607c478bd9Sstevel@tonic-gate 		if (do_lgrp_load &&
4617c478bd9Sstevel@tonic-gate 		    (cp->cpu_flags & CPU_EXISTS)) {
4627c478bd9Sstevel@tonic-gate 			/*
4637c478bd9Sstevel@tonic-gate 			 * When updating the lgroup's load average,
4647c478bd9Sstevel@tonic-gate 			 * account for the thread running on the CPU.
4657c478bd9Sstevel@tonic-gate 			 * If the CPU is the current one, then we need
4667c478bd9Sstevel@tonic-gate 			 * to account for the underlying thread which
4677c478bd9Sstevel@tonic-gate 			 * got the clock interrupt not the thread that is
4687c478bd9Sstevel@tonic-gate 			 * handling the interrupt and caculating the load
4697c478bd9Sstevel@tonic-gate 			 * average
4707c478bd9Sstevel@tonic-gate 			 */
4717c478bd9Sstevel@tonic-gate 			t = cp->cpu_thread;
4727c478bd9Sstevel@tonic-gate 			if (CPU == cp)
4737c478bd9Sstevel@tonic-gate 				t = t->t_intr;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 			/*
4767c478bd9Sstevel@tonic-gate 			 * Account for the load average for this thread if
4777c478bd9Sstevel@tonic-gate 			 * it isn't the idle thread or it is on the interrupt
4787c478bd9Sstevel@tonic-gate 			 * stack and not the current CPU handling the clock
4797c478bd9Sstevel@tonic-gate 			 * interrupt
4807c478bd9Sstevel@tonic-gate 			 */
4817c478bd9Sstevel@tonic-gate 			if ((t && t != cp->cpu_idle_thread) || (CPU != cp &&
4827c478bd9Sstevel@tonic-gate 			    CPU_ON_INTR(cp))) {
4837c478bd9Sstevel@tonic-gate 				if (t->t_lpl == cp->cpu_lpl) {
4847c478bd9Sstevel@tonic-gate 					/* local thread */
4857c478bd9Sstevel@tonic-gate 					cpu_nrunnable++;
4867c478bd9Sstevel@tonic-gate 				} else {
4877c478bd9Sstevel@tonic-gate 					/*
4887c478bd9Sstevel@tonic-gate 					 * This is a remote thread, charge it
4897c478bd9Sstevel@tonic-gate 					 * against its home lgroup.  Note that
4907c478bd9Sstevel@tonic-gate 					 * we notice that a thread is remote
4917c478bd9Sstevel@tonic-gate 					 * only if it's currently executing.
4927c478bd9Sstevel@tonic-gate 					 * This is a reasonable approximation,
4937c478bd9Sstevel@tonic-gate 					 * since queued remote threads are rare.
4947c478bd9Sstevel@tonic-gate 					 * Note also that if we didn't charge
4957c478bd9Sstevel@tonic-gate 					 * it to its home lgroup, remote
4967c478bd9Sstevel@tonic-gate 					 * execution would often make a system
4977c478bd9Sstevel@tonic-gate 					 * appear balanced even though it was
4987c478bd9Sstevel@tonic-gate 					 * not, and thread placement/migration
4997c478bd9Sstevel@tonic-gate 					 * would often not be done correctly.
5007c478bd9Sstevel@tonic-gate 					 */
5017c478bd9Sstevel@tonic-gate 					lgrp_loadavg(t->t_lpl,
5027c478bd9Sstevel@tonic-gate 					    LGRP_LOADAVG_IN_THREAD_MAX, 0);
5037c478bd9Sstevel@tonic-gate 				}
5047c478bd9Sstevel@tonic-gate 			}
5057c478bd9Sstevel@tonic-gate 			lgrp_loadavg(cp->cpu_lpl,
5067c478bd9Sstevel@tonic-gate 			    cpu_nrunnable * LGRP_LOADAVG_IN_THREAD_MAX, 1);
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/*
5117c478bd9Sstevel@tonic-gate 	 * Do tick processing for all the active threads running in
512*c97ad5cdSakolb 	 * the system.  We're trying to be more fair by walking the
513*c97ad5cdSakolb 	 * list of CPUs starting from a different CPUs each time.
5147c478bd9Sstevel@tonic-gate 	 */
515*c97ad5cdSakolb 	cp = clock_cpu_list;
5167c478bd9Sstevel@tonic-gate 	nrunning = 0;
5177c478bd9Sstevel@tonic-gate 	do {
5187c478bd9Sstevel@tonic-gate 		klwp_id_t lwp;
5197c478bd9Sstevel@tonic-gate 		int intr;
5207c478bd9Sstevel@tonic-gate 		int thread_away;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 		/*
5237c478bd9Sstevel@tonic-gate 		 * Don't do any tick processing on CPUs that
5247c478bd9Sstevel@tonic-gate 		 * aren't even in the system or aren't up yet.
5257c478bd9Sstevel@tonic-gate 		 */
5267c478bd9Sstevel@tonic-gate 		if ((cp->cpu_flags & CPU_EXISTS) == 0) {
5277c478bd9Sstevel@tonic-gate 			continue;
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 		/*
5317c478bd9Sstevel@tonic-gate 		 * The locking here is rather tricky.  We use
5327c478bd9Sstevel@tonic-gate 		 * thread_free_lock to keep the currently running
5337c478bd9Sstevel@tonic-gate 		 * thread from being freed or recycled while we're
5347c478bd9Sstevel@tonic-gate 		 * looking at it.  We can then check if the thread
5357c478bd9Sstevel@tonic-gate 		 * is exiting and get the appropriate p_lock if it
5367c478bd9Sstevel@tonic-gate 		 * is not.  We have to be careful, though, because
5377c478bd9Sstevel@tonic-gate 		 * the _process_ can still be freed while we're
5387c478bd9Sstevel@tonic-gate 		 * holding thread_free_lock.  To avoid touching the
5397c478bd9Sstevel@tonic-gate 		 * proc structure we put a pointer to the p_lock in the
5407c478bd9Sstevel@tonic-gate 		 * thread structure.  The p_lock is persistent so we
5417c478bd9Sstevel@tonic-gate 		 * can acquire it even if the process is gone.  At that
5427c478bd9Sstevel@tonic-gate 		 * point we can check (again) if the thread is exiting
5437c478bd9Sstevel@tonic-gate 		 * and either drop the lock or do the tick processing.
5447c478bd9Sstevel@tonic-gate 		 */
5457c478bd9Sstevel@tonic-gate 		mutex_enter(&thread_free_lock);
5467c478bd9Sstevel@tonic-gate 		/*
5477c478bd9Sstevel@tonic-gate 		 * We cannot hold the cpu_lock to prevent the
5487c478bd9Sstevel@tonic-gate 		 * cpu_list from changing in the clock interrupt.
5497c478bd9Sstevel@tonic-gate 		 * As long as we don't block (or don't get pre-empted)
5507c478bd9Sstevel@tonic-gate 		 * the cpu_list will not change (all threads are paused
5517c478bd9Sstevel@tonic-gate 		 * before list modification). If the list does change
5527c478bd9Sstevel@tonic-gate 		 * any deleted cpu structures will remain with cpu_next
5537c478bd9Sstevel@tonic-gate 		 * set to NULL, hence the following test.
5547c478bd9Sstevel@tonic-gate 		 */
5557c478bd9Sstevel@tonic-gate 		if (cp->cpu_next == NULL) {
5567c478bd9Sstevel@tonic-gate 			mutex_exit(&thread_free_lock);
5577c478bd9Sstevel@tonic-gate 			break;
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 		t = cp->cpu_thread;	/* Current running thread */
5607c478bd9Sstevel@tonic-gate 		if (CPU == cp) {
5617c478bd9Sstevel@tonic-gate 			/*
5627c478bd9Sstevel@tonic-gate 			 * 't' will be the clock interrupt thread on this
5637c478bd9Sstevel@tonic-gate 			 * CPU.  Use the pinned thread (if any) on this CPU
5647c478bd9Sstevel@tonic-gate 			 * as the target of the clock tick.  If we pinned
5657c478bd9Sstevel@tonic-gate 			 * an interrupt, though, just keep using the clock
5667c478bd9Sstevel@tonic-gate 			 * interrupt thread since the formerly pinned one
5677c478bd9Sstevel@tonic-gate 			 * may have gone away.  One interrupt thread is as
5687c478bd9Sstevel@tonic-gate 			 * good as another, and this means we don't have
5697c478bd9Sstevel@tonic-gate 			 * to continue to check pinned_intr in subsequent
5707c478bd9Sstevel@tonic-gate 			 * code.
5717c478bd9Sstevel@tonic-gate 			 */
5727c478bd9Sstevel@tonic-gate 			ASSERT(t == curthread);
5737c478bd9Sstevel@tonic-gate 			if (t->t_intr != NULL && !pinned_intr)
5747c478bd9Sstevel@tonic-gate 				t = t->t_intr;
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		intr = t->t_flag & T_INTR_THREAD;
5787c478bd9Sstevel@tonic-gate 		lwp = ttolwp(t);
5797c478bd9Sstevel@tonic-gate 		if (lwp == NULL || (t->t_proc_flag & TP_LWPEXIT) || intr) {
5807c478bd9Sstevel@tonic-gate 			/*
5817c478bd9Sstevel@tonic-gate 			 * Thread is exiting (or uninteresting) so don't
5827c478bd9Sstevel@tonic-gate 			 * do tick processing or grab p_lock.  Once we
5837c478bd9Sstevel@tonic-gate 			 * drop thread_free_lock we can't look inside the
5847c478bd9Sstevel@tonic-gate 			 * thread or lwp structure, since the thread may
5857c478bd9Sstevel@tonic-gate 			 * have gone away.
5867c478bd9Sstevel@tonic-gate 			 */
5877c478bd9Sstevel@tonic-gate 			exiting = 1;
5887c478bd9Sstevel@tonic-gate 		} else {
5897c478bd9Sstevel@tonic-gate 			/*
5907c478bd9Sstevel@tonic-gate 			 * OK, try to grab the process lock.  See
5917c478bd9Sstevel@tonic-gate 			 * comments above for why we're not using
5927c478bd9Sstevel@tonic-gate 			 * ttoproc(t)->p_lockp here.
5937c478bd9Sstevel@tonic-gate 			 */
5947c478bd9Sstevel@tonic-gate 			plockp = t->t_plockp;
5957c478bd9Sstevel@tonic-gate 			mutex_enter(plockp);
5967c478bd9Sstevel@tonic-gate 			/* See above comment. */
5977c478bd9Sstevel@tonic-gate 			if (cp->cpu_next == NULL) {
5987c478bd9Sstevel@tonic-gate 				mutex_exit(plockp);
5997c478bd9Sstevel@tonic-gate 				mutex_exit(&thread_free_lock);
6007c478bd9Sstevel@tonic-gate 				break;
6017c478bd9Sstevel@tonic-gate 			}
6027c478bd9Sstevel@tonic-gate 			/*
6037c478bd9Sstevel@tonic-gate 			 * The thread may have exited between when we
6047c478bd9Sstevel@tonic-gate 			 * checked above, and when we got the p_lock.
6057c478bd9Sstevel@tonic-gate 			 */
6067c478bd9Sstevel@tonic-gate 			if (t->t_proc_flag & TP_LWPEXIT) {
6077c478bd9Sstevel@tonic-gate 				mutex_exit(plockp);
6087c478bd9Sstevel@tonic-gate 				exiting = 1;
6097c478bd9Sstevel@tonic-gate 			} else {
6107c478bd9Sstevel@tonic-gate 				exiting = 0;
6117c478bd9Sstevel@tonic-gate 			}
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 		/*
6147c478bd9Sstevel@tonic-gate 		 * Either we have the p_lock for the thread's process,
6157c478bd9Sstevel@tonic-gate 		 * or we don't care about the thread structure any more.
6167c478bd9Sstevel@tonic-gate 		 * Either way we can drop thread_free_lock.
6177c478bd9Sstevel@tonic-gate 		 */
6187c478bd9Sstevel@tonic-gate 		mutex_exit(&thread_free_lock);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		/*
6217c478bd9Sstevel@tonic-gate 		 * Update user, system, and idle cpu times.
6227c478bd9Sstevel@tonic-gate 		 */
6237c478bd9Sstevel@tonic-gate 		if (one_sec) {
6247c478bd9Sstevel@tonic-gate 			nrunning++;
6257c478bd9Sstevel@tonic-gate 			cp->cpu_part->cp_nrunning++;
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 		/*
6287c478bd9Sstevel@tonic-gate 		 * If we haven't done tick processing for this
6297c478bd9Sstevel@tonic-gate 		 * lwp, then do it now. Since we don't hold the
6307c478bd9Sstevel@tonic-gate 		 * lwp down on a CPU it can migrate and show up
6317c478bd9Sstevel@tonic-gate 		 * more than once, hence the lbolt check.
6327c478bd9Sstevel@tonic-gate 		 *
6337c478bd9Sstevel@tonic-gate 		 * Also, make sure that it's okay to perform the
6347c478bd9Sstevel@tonic-gate 		 * tick processing before calling clock_tick.
6357c478bd9Sstevel@tonic-gate 		 * Setting thread_away to a TRUE value (ie. not 0)
6367c478bd9Sstevel@tonic-gate 		 * results in tick processing not being performed for
6377c478bd9Sstevel@tonic-gate 		 * that thread.  Or, in other words, keeps the thread
6387c478bd9Sstevel@tonic-gate 		 * away from clock_tick processing.
6397c478bd9Sstevel@tonic-gate 		 */
6407c478bd9Sstevel@tonic-gate 		thread_away = ((cp->cpu_flags & CPU_QUIESCED) ||
6417c478bd9Sstevel@tonic-gate 		    CPU_ON_INTR(cp) || intr ||
6427c478bd9Sstevel@tonic-gate 		    (cp->cpu_dispthread == cp->cpu_idle_thread) || exiting);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		if ((!thread_away) && (lbolt - t->t_lbolt != 0)) {
6457c478bd9Sstevel@tonic-gate 			t->t_lbolt = lbolt;
6467c478bd9Sstevel@tonic-gate 			clock_tick(t);
6477c478bd9Sstevel@tonic-gate 		}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		if (!exiting)
6507c478bd9Sstevel@tonic-gate 			mutex_exit(plockp);
651*c97ad5cdSakolb 	} while ((cp = cp->cpu_next) != clock_cpu_list);
652*c97ad5cdSakolb 
653*c97ad5cdSakolb 	clock_cpu_list = clock_cpu_list->cpu_next;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	/*
6567c478bd9Sstevel@tonic-gate 	 * bump time in ticks
6577c478bd9Sstevel@tonic-gate 	 *
6587c478bd9Sstevel@tonic-gate 	 * We rely on there being only one clock thread and hence
6597c478bd9Sstevel@tonic-gate 	 * don't need a lock to protect lbolt.
6607c478bd9Sstevel@tonic-gate 	 */
6617c478bd9Sstevel@tonic-gate 	lbolt++;
6627c478bd9Sstevel@tonic-gate 	atomic_add_64((uint64_t *)&lbolt64, (int64_t)1);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/*
6657c478bd9Sstevel@tonic-gate 	 * Check for a callout that needs be called from the clock
6667c478bd9Sstevel@tonic-gate 	 * thread to support the membership protocol in a clustered
6677c478bd9Sstevel@tonic-gate 	 * system.  Copy the function pointer so that we can reset
6687c478bd9Sstevel@tonic-gate 	 * this to NULL if needed.
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	if ((funcp = cmm_clock_callout) != NULL)
6717c478bd9Sstevel@tonic-gate 		(*funcp)();
6727c478bd9Sstevel@tonic-gate 
673*c97ad5cdSakolb 	if ((funcp = cpucaps_clock_callout) != NULL)
674*c97ad5cdSakolb 		(*funcp)();
675*c97ad5cdSakolb 
6767c478bd9Sstevel@tonic-gate 	/*
6777c478bd9Sstevel@tonic-gate 	 * Wakeup the cageout thread waiters once per second.
6787c478bd9Sstevel@tonic-gate 	 */
6797c478bd9Sstevel@tonic-gate 	if (one_sec)
6807c478bd9Sstevel@tonic-gate 		kcage_tick();
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/*
6837c478bd9Sstevel@tonic-gate 	 * Schedule timeout() requests if any are due at this time.
6847c478bd9Sstevel@tonic-gate 	 */
6857c478bd9Sstevel@tonic-gate 	callout_schedule();
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	if (one_sec) {
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		int drift, absdrift;
6907c478bd9Sstevel@tonic-gate 		timestruc_t tod;
6917c478bd9Sstevel@tonic-gate 		int s;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		/*
6947c478bd9Sstevel@tonic-gate 		 * Beginning of precision-kernel code fragment executed
6957c478bd9Sstevel@tonic-gate 		 * every second.
6967c478bd9Sstevel@tonic-gate 		 *
6977c478bd9Sstevel@tonic-gate 		 * On rollover of the second the phase adjustment to be
6987c478bd9Sstevel@tonic-gate 		 * used for the next second is calculated.  Also, the
6997c478bd9Sstevel@tonic-gate 		 * maximum error is increased by the tolerance.  If the
7007c478bd9Sstevel@tonic-gate 		 * PPS frequency discipline code is present, the phase is
7017c478bd9Sstevel@tonic-gate 		 * increased to compensate for the CPU clock oscillator
7027c478bd9Sstevel@tonic-gate 		 * frequency error.
7037c478bd9Sstevel@tonic-gate 		 *
7047c478bd9Sstevel@tonic-gate 		 * On a 32-bit machine and given parameters in the timex.h
7057c478bd9Sstevel@tonic-gate 		 * header file, the maximum phase adjustment is +-512 ms
7067c478bd9Sstevel@tonic-gate 		 * and maximum frequency offset is (a tad less than)
7077c478bd9Sstevel@tonic-gate 		 * +-512 ppm. On a 64-bit machine, you shouldn't need to ask.
7087c478bd9Sstevel@tonic-gate 		 */
7097c478bd9Sstevel@tonic-gate 		time_maxerror += time_tolerance / SCALE_USEC;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		/*
7127c478bd9Sstevel@tonic-gate 		 * Leap second processing. If in leap-insert state at
7137c478bd9Sstevel@tonic-gate 		 * the end of the day, the system clock is set back one
7147c478bd9Sstevel@tonic-gate 		 * second; if in leap-delete state, the system clock is
7157c478bd9Sstevel@tonic-gate 		 * set ahead one second. The microtime() routine or
7167c478bd9Sstevel@tonic-gate 		 * external clock driver will insure that reported time
7177c478bd9Sstevel@tonic-gate 		 * is always monotonic. The ugly divides should be
7187c478bd9Sstevel@tonic-gate 		 * replaced.
7197c478bd9Sstevel@tonic-gate 		 */
7207c478bd9Sstevel@tonic-gate 		switch (time_state) {
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 		case TIME_OK:
7237c478bd9Sstevel@tonic-gate 			if (time_status & STA_INS)
7247c478bd9Sstevel@tonic-gate 				time_state = TIME_INS;
7257c478bd9Sstevel@tonic-gate 			else if (time_status & STA_DEL)
7267c478bd9Sstevel@tonic-gate 				time_state = TIME_DEL;
7277c478bd9Sstevel@tonic-gate 			break;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		case TIME_INS:
7307c478bd9Sstevel@tonic-gate 			if (hrestime.tv_sec % 86400 == 0) {
7317c478bd9Sstevel@tonic-gate 				s = hr_clock_lock();
7327c478bd9Sstevel@tonic-gate 				hrestime.tv_sec--;
7337c478bd9Sstevel@tonic-gate 				hr_clock_unlock(s);
7347c478bd9Sstevel@tonic-gate 				time_state = TIME_OOP;
7357c478bd9Sstevel@tonic-gate 			}
7367c478bd9Sstevel@tonic-gate 			break;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 		case TIME_DEL:
7397c478bd9Sstevel@tonic-gate 			if ((hrestime.tv_sec + 1) % 86400 == 0) {
7407c478bd9Sstevel@tonic-gate 				s = hr_clock_lock();
7417c478bd9Sstevel@tonic-gate 				hrestime.tv_sec++;
7427c478bd9Sstevel@tonic-gate 				hr_clock_unlock(s);
7437c478bd9Sstevel@tonic-gate 				time_state = TIME_WAIT;
7447c478bd9Sstevel@tonic-gate 			}
7457c478bd9Sstevel@tonic-gate 			break;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		case TIME_OOP:
7487c478bd9Sstevel@tonic-gate 			time_state = TIME_WAIT;
7497c478bd9Sstevel@tonic-gate 			break;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		case TIME_WAIT:
7527c478bd9Sstevel@tonic-gate 			if (!(time_status & (STA_INS | STA_DEL)))
7537c478bd9Sstevel@tonic-gate 				time_state = TIME_OK;
7547c478bd9Sstevel@tonic-gate 		default:
7557c478bd9Sstevel@tonic-gate 			break;
7567c478bd9Sstevel@tonic-gate 		}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 		/*
7597c478bd9Sstevel@tonic-gate 		 * Compute the phase adjustment for the next second. In
7607c478bd9Sstevel@tonic-gate 		 * PLL mode, the offset is reduced by a fixed factor
7617c478bd9Sstevel@tonic-gate 		 * times the time constant. In FLL mode the offset is
7627c478bd9Sstevel@tonic-gate 		 * used directly. In either mode, the maximum phase
7637c478bd9Sstevel@tonic-gate 		 * adjustment for each second is clamped so as to spread
7647c478bd9Sstevel@tonic-gate 		 * the adjustment over not more than the number of
7657c478bd9Sstevel@tonic-gate 		 * seconds between updates.
7667c478bd9Sstevel@tonic-gate 		 */
7677c478bd9Sstevel@tonic-gate 		if (time_offset == 0)
7687c478bd9Sstevel@tonic-gate 			time_adj = 0;
7697c478bd9Sstevel@tonic-gate 		else if (time_offset < 0) {
7707c478bd9Sstevel@tonic-gate 			lltemp = -time_offset;
7717c478bd9Sstevel@tonic-gate 			if (!(time_status & STA_FLL)) {
7727c478bd9Sstevel@tonic-gate 				if ((1 << time_constant) >= SCALE_KG)
7737c478bd9Sstevel@tonic-gate 					lltemp *= (1 << time_constant) /
7747c478bd9Sstevel@tonic-gate 					    SCALE_KG;
7757c478bd9Sstevel@tonic-gate 				else
7767c478bd9Sstevel@tonic-gate 					lltemp = (lltemp / SCALE_KG) >>
7777c478bd9Sstevel@tonic-gate 					    time_constant;
7787c478bd9Sstevel@tonic-gate 			}
7797c478bd9Sstevel@tonic-gate 			if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
7807c478bd9Sstevel@tonic-gate 				lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
7817c478bd9Sstevel@tonic-gate 			time_offset += lltemp;
7827c478bd9Sstevel@tonic-gate 			time_adj = -(lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
7837c478bd9Sstevel@tonic-gate 		} else {
7847c478bd9Sstevel@tonic-gate 			lltemp = time_offset;
7857c478bd9Sstevel@tonic-gate 			if (!(time_status & STA_FLL)) {
7867c478bd9Sstevel@tonic-gate 				if ((1 << time_constant) >= SCALE_KG)
7877c478bd9Sstevel@tonic-gate 					lltemp *= (1 << time_constant) /
7887c478bd9Sstevel@tonic-gate 					    SCALE_KG;
7897c478bd9Sstevel@tonic-gate 				else
7907c478bd9Sstevel@tonic-gate 					lltemp = (lltemp / SCALE_KG) >>
7917c478bd9Sstevel@tonic-gate 					    time_constant;
7927c478bd9Sstevel@tonic-gate 			}
7937c478bd9Sstevel@tonic-gate 			if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
7947c478bd9Sstevel@tonic-gate 				lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
7957c478bd9Sstevel@tonic-gate 			time_offset -= lltemp;
7967c478bd9Sstevel@tonic-gate 			time_adj = (lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
7977c478bd9Sstevel@tonic-gate 		}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 		/*
8007c478bd9Sstevel@tonic-gate 		 * Compute the frequency estimate and additional phase
8017c478bd9Sstevel@tonic-gate 		 * adjustment due to frequency error for the next
8027c478bd9Sstevel@tonic-gate 		 * second. When the PPS signal is engaged, gnaw on the
8037c478bd9Sstevel@tonic-gate 		 * watchdog counter and update the frequency computed by
8047c478bd9Sstevel@tonic-gate 		 * the pll and the PPS signal.
8057c478bd9Sstevel@tonic-gate 		 */
8067c478bd9Sstevel@tonic-gate 		pps_valid++;
8077c478bd9Sstevel@tonic-gate 		if (pps_valid == PPS_VALID) {
8087c478bd9Sstevel@tonic-gate 			pps_jitter = MAXTIME;
8097c478bd9Sstevel@tonic-gate 			pps_stabil = MAXFREQ;
8107c478bd9Sstevel@tonic-gate 			time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
8117c478bd9Sstevel@tonic-gate 			    STA_PPSWANDER | STA_PPSERROR);
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		lltemp = time_freq + pps_freq;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 		if (lltemp)
8167c478bd9Sstevel@tonic-gate 			time_adj += (lltemp * SCALE_PHASE) / (SCALE_USEC * hz);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 		/*
8197c478bd9Sstevel@tonic-gate 		 * End of precision kernel-code fragment
8207c478bd9Sstevel@tonic-gate 		 *
8217c478bd9Sstevel@tonic-gate 		 * The section below should be modified if we are planning
8227c478bd9Sstevel@tonic-gate 		 * to use NTP for synchronization.
8237c478bd9Sstevel@tonic-gate 		 *
8247c478bd9Sstevel@tonic-gate 		 * Note: the clock synchronization code now assumes
8257c478bd9Sstevel@tonic-gate 		 * the following:
8267c478bd9Sstevel@tonic-gate 		 *   - if dosynctodr is 1, then compute the drift between
8277c478bd9Sstevel@tonic-gate 		 *	the tod chip and software time and adjust one or
8287c478bd9Sstevel@tonic-gate 		 *	the other depending on the circumstances
8297c478bd9Sstevel@tonic-gate 		 *
8307c478bd9Sstevel@tonic-gate 		 *   - if dosynctodr is 0, then the tod chip is independent
8317c478bd9Sstevel@tonic-gate 		 *	of the software clock and should not be adjusted,
8327c478bd9Sstevel@tonic-gate 		 *	but allowed to free run.  this allows NTP to sync.
8337c478bd9Sstevel@tonic-gate 		 *	hrestime without any interference from the tod chip.
8347c478bd9Sstevel@tonic-gate 		 */
8357c478bd9Sstevel@tonic-gate 
8362c891a87Ssethg 		tod_validate_deferred = B_FALSE;
8377c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
8387c478bd9Sstevel@tonic-gate 		tod = tod_get();
8397c478bd9Sstevel@tonic-gate 		drift = tod.tv_sec - hrestime.tv_sec;
8407c478bd9Sstevel@tonic-gate 		absdrift = (drift >= 0) ? drift : -drift;
8417c478bd9Sstevel@tonic-gate 		if (tod_needsync || absdrift > 1) {
8427c478bd9Sstevel@tonic-gate 			int s;
8437c478bd9Sstevel@tonic-gate 			if (absdrift > 2) {
8447c478bd9Sstevel@tonic-gate 				if (!tod_broken && tod_faulted == TOD_NOFAULT) {
8457c478bd9Sstevel@tonic-gate 					s = hr_clock_lock();
8467c478bd9Sstevel@tonic-gate 					hrestime = tod;
8477c478bd9Sstevel@tonic-gate 					membar_enter();	/* hrestime visible */
8487c478bd9Sstevel@tonic-gate 					timedelta = 0;
849a913396dSqiao 					hrestime_isvalid = 1;
8507c478bd9Sstevel@tonic-gate 					tod_needsync = 0;
8517c478bd9Sstevel@tonic-gate 					hr_clock_unlock(s);
8527c478bd9Sstevel@tonic-gate 				}
8537c478bd9Sstevel@tonic-gate 			} else {
8547c478bd9Sstevel@tonic-gate 				if (tod_needsync || !dosynctodr) {
8557c478bd9Sstevel@tonic-gate 					gethrestime(&tod);
8567c478bd9Sstevel@tonic-gate 					tod_set(tod);
8577c478bd9Sstevel@tonic-gate 					s = hr_clock_lock();
8587c478bd9Sstevel@tonic-gate 					if (timedelta == 0)
8597c478bd9Sstevel@tonic-gate 						tod_needsync = 0;
8607c478bd9Sstevel@tonic-gate 					hr_clock_unlock(s);
8617c478bd9Sstevel@tonic-gate 				} else {
8627c478bd9Sstevel@tonic-gate 					/*
8637c478bd9Sstevel@tonic-gate 					 * If the drift is 2 seconds on the
8647c478bd9Sstevel@tonic-gate 					 * money, then the TOD is adjusting
8657c478bd9Sstevel@tonic-gate 					 * the clock;  record that.
8667c478bd9Sstevel@tonic-gate 					 */
8677c478bd9Sstevel@tonic-gate 					clock_adj_hist[adj_hist_entry++ %
8687c478bd9Sstevel@tonic-gate 					    CLOCK_ADJ_HIST_SIZE] = lbolt64;
8697c478bd9Sstevel@tonic-gate 					s = hr_clock_lock();
8707c478bd9Sstevel@tonic-gate 					timedelta = (int64_t)drift*NANOSEC;
8717c478bd9Sstevel@tonic-gate 					hr_clock_unlock(s);
8727c478bd9Sstevel@tonic-gate 				}
8737c478bd9Sstevel@tonic-gate 			}
8747c478bd9Sstevel@tonic-gate 		}
8757c478bd9Sstevel@tonic-gate 		one_sec = 0;
8767c478bd9Sstevel@tonic-gate 		time = gethrestime_sec();  /* for crusty old kmem readers */
8777c478bd9Sstevel@tonic-gate 		mutex_exit(&tod_lock);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		/*
8807c478bd9Sstevel@tonic-gate 		 * Some drivers still depend on this... XXX
8817c478bd9Sstevel@tonic-gate 		 */
8827c478bd9Sstevel@tonic-gate 		cv_broadcast(&lbolt_cv);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		sysinfo.updates++;
8857c478bd9Sstevel@tonic-gate 		vminfo.freemem += freemem;
8867c478bd9Sstevel@tonic-gate 		{
8877c478bd9Sstevel@tonic-gate 			pgcnt_t maxswap, resv, free;
8887c478bd9Sstevel@tonic-gate 			pgcnt_t avail =
8897c478bd9Sstevel@tonic-gate 			    MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 			maxswap = k_anoninfo.ani_mem_resv
8927c478bd9Sstevel@tonic-gate 					+ k_anoninfo.ani_max +avail;
8937c478bd9Sstevel@tonic-gate 			free = k_anoninfo.ani_free + avail;
8947c478bd9Sstevel@tonic-gate 			resv = k_anoninfo.ani_phys_resv +
8957c478bd9Sstevel@tonic-gate 			    k_anoninfo.ani_mem_resv;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 			vminfo.swap_resv += resv;
8987c478bd9Sstevel@tonic-gate 			/* number of reserved and allocated pages */
8997c478bd9Sstevel@tonic-gate #ifdef	DEBUG
9007c478bd9Sstevel@tonic-gate 			if (maxswap < free)
9017c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "clock: maxswap < free");
9027c478bd9Sstevel@tonic-gate 			if (maxswap < resv)
9037c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "clock: maxswap < resv");
9047c478bd9Sstevel@tonic-gate #endif
9057c478bd9Sstevel@tonic-gate 			vminfo.swap_alloc += maxswap - free;
9067c478bd9Sstevel@tonic-gate 			vminfo.swap_avail += maxswap - resv;
9077c478bd9Sstevel@tonic-gate 			vminfo.swap_free += free;
9087c478bd9Sstevel@tonic-gate 		}
9097c478bd9Sstevel@tonic-gate 		if (nrunnable) {
9107c478bd9Sstevel@tonic-gate 			sysinfo.runque += nrunnable;
9117c478bd9Sstevel@tonic-gate 			sysinfo.runocc++;
9127c478bd9Sstevel@tonic-gate 		}
9137c478bd9Sstevel@tonic-gate 		if (nswapped) {
9147c478bd9Sstevel@tonic-gate 			sysinfo.swpque += nswapped;
9157c478bd9Sstevel@tonic-gate 			sysinfo.swpocc++;
9167c478bd9Sstevel@tonic-gate 		}
9177c478bd9Sstevel@tonic-gate 		sysinfo.waiting += w_io;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 		/*
9207c478bd9Sstevel@tonic-gate 		 * Wake up fsflush to write out DELWRI
9217c478bd9Sstevel@tonic-gate 		 * buffers, dirty pages and other cached
9227c478bd9Sstevel@tonic-gate 		 * administrative data, e.g. inodes.
9237c478bd9Sstevel@tonic-gate 		 */
9247c478bd9Sstevel@tonic-gate 		if (--fsflushcnt <= 0) {
9257c478bd9Sstevel@tonic-gate 			fsflushcnt = tune.t_fsflushr;
9267c478bd9Sstevel@tonic-gate 			cv_signal(&fsflush_cv);
9277c478bd9Sstevel@tonic-gate 		}
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 		vmmeter();
9307c478bd9Sstevel@tonic-gate 		calcloadavg(genloadavg(&loadavg), hp_avenrun);
9317c478bd9Sstevel@tonic-gate 		for (i = 0; i < 3; i++)
9327c478bd9Sstevel@tonic-gate 			/*
9337c478bd9Sstevel@tonic-gate 			 * At the moment avenrun[] can only hold 31
9347c478bd9Sstevel@tonic-gate 			 * bits of load average as it is a signed
9357c478bd9Sstevel@tonic-gate 			 * int in the API. We need to ensure that
9367c478bd9Sstevel@tonic-gate 			 * hp_avenrun[i] >> (16 - FSHIFT) will not be
9377c478bd9Sstevel@tonic-gate 			 * too large. If it is, we put the largest value
9387c478bd9Sstevel@tonic-gate 			 * that we can use into avenrun[i]. This is
9397c478bd9Sstevel@tonic-gate 			 * kludgey, but about all we can do until we
9407c478bd9Sstevel@tonic-gate 			 * avenrun[] is declared as an array of uint64[]
9417c478bd9Sstevel@tonic-gate 			 */
9427c478bd9Sstevel@tonic-gate 			if (hp_avenrun[i] < ((uint64_t)1<<(31+16-FSHIFT)))
9437c478bd9Sstevel@tonic-gate 				avenrun[i] = (int32_t)(hp_avenrun[i] >>
9447c478bd9Sstevel@tonic-gate 				    (16 - FSHIFT));
9457c478bd9Sstevel@tonic-gate 			else
9467c478bd9Sstevel@tonic-gate 				avenrun[i] = 0x7fffffff;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 		cpupart = cp_list_head;
9497c478bd9Sstevel@tonic-gate 		do {
9507c478bd9Sstevel@tonic-gate 			calcloadavg(genloadavg(&cpupart->cp_loadavg),
9517c478bd9Sstevel@tonic-gate 			    cpupart->cp_hp_avenrun);
9527c478bd9Sstevel@tonic-gate 		} while ((cpupart = cpupart->cp_next) != cp_list_head);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 		/*
9557c478bd9Sstevel@tonic-gate 		 * Wake up the swapper thread if necessary.
9567c478bd9Sstevel@tonic-gate 		 */
9577c478bd9Sstevel@tonic-gate 		if (runin ||
9587c478bd9Sstevel@tonic-gate 		    (runout && (avefree < desfree || wake_sched_sec))) {
9597c478bd9Sstevel@tonic-gate 			t = &t0;
9607c478bd9Sstevel@tonic-gate 			thread_lock(t);
9617c478bd9Sstevel@tonic-gate 			if (t->t_state == TS_STOPPED) {
9627c478bd9Sstevel@tonic-gate 				runin = runout = 0;
9637c478bd9Sstevel@tonic-gate 				wake_sched_sec = 0;
9647c478bd9Sstevel@tonic-gate 				t->t_whystop = 0;
9657c478bd9Sstevel@tonic-gate 				t->t_whatstop = 0;
9667c478bd9Sstevel@tonic-gate 				t->t_schedflag &= ~TS_ALLSTART;
9677c478bd9Sstevel@tonic-gate 				THREAD_TRANSITION(t);
9687c478bd9Sstevel@tonic-gate 				setfrontdq(t);
9697c478bd9Sstevel@tonic-gate 			}
9707c478bd9Sstevel@tonic-gate 			thread_unlock(t);
9717c478bd9Sstevel@tonic-gate 		}
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	/*
9757c478bd9Sstevel@tonic-gate 	 * Wake up the swapper if any high priority swapped-out threads
9767c478bd9Sstevel@tonic-gate 	 * became runable during the last tick.
9777c478bd9Sstevel@tonic-gate 	 */
9787c478bd9Sstevel@tonic-gate 	if (wake_sched) {
9797c478bd9Sstevel@tonic-gate 		t = &t0;
9807c478bd9Sstevel@tonic-gate 		thread_lock(t);
9817c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_STOPPED) {
9827c478bd9Sstevel@tonic-gate 			runin = runout = 0;
9837c478bd9Sstevel@tonic-gate 			wake_sched = 0;
9847c478bd9Sstevel@tonic-gate 			t->t_whystop = 0;
9857c478bd9Sstevel@tonic-gate 			t->t_whatstop = 0;
9867c478bd9Sstevel@tonic-gate 			t->t_schedflag &= ~TS_ALLSTART;
9877c478bd9Sstevel@tonic-gate 			THREAD_TRANSITION(t);
9887c478bd9Sstevel@tonic-gate 			setfrontdq(t);
9897c478bd9Sstevel@tonic-gate 		}
9907c478bd9Sstevel@tonic-gate 		thread_unlock(t);
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate void
9957c478bd9Sstevel@tonic-gate clock_init(void)
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate 	cyc_handler_t hdlr;
9987c478bd9Sstevel@tonic-gate 	cyc_time_t when;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	hdlr.cyh_func = (cyc_func_t)clock;
10017c478bd9Sstevel@tonic-gate 	hdlr.cyh_level = CY_LOCK_LEVEL;
10027c478bd9Sstevel@tonic-gate 	hdlr.cyh_arg = NULL;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	when.cyt_when = 0;
10057c478bd9Sstevel@tonic-gate 	when.cyt_interval = nsec_per_tick;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
10087c478bd9Sstevel@tonic-gate 	clock_cyclic = cyclic_add(&hdlr, &when);
10097c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate /*
10137c478bd9Sstevel@tonic-gate  * Called before calcloadavg to get 10-sec moving loadavg together
10147c478bd9Sstevel@tonic-gate  */
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate static int
10177c478bd9Sstevel@tonic-gate genloadavg(struct loadavg_s *avgs)
10187c478bd9Sstevel@tonic-gate {
10197c478bd9Sstevel@tonic-gate 	int avg;
10207c478bd9Sstevel@tonic-gate 	int spos; /* starting position */
10217c478bd9Sstevel@tonic-gate 	int cpos; /* moving current position */
10227c478bd9Sstevel@tonic-gate 	int i;
10237c478bd9Sstevel@tonic-gate 	int slen;
10247c478bd9Sstevel@tonic-gate 	hrtime_t hr_avg;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	/* 10-second snapshot, calculate first positon */
10277c478bd9Sstevel@tonic-gate 	if (avgs->lg_len == 0) {
10287c478bd9Sstevel@tonic-gate 		return (0);
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 	slen = avgs->lg_len < S_MOVAVG_SZ ? avgs->lg_len : S_MOVAVG_SZ;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	spos = (avgs->lg_cur - 1) >= 0 ? avgs->lg_cur - 1 :
10337c478bd9Sstevel@tonic-gate 	    S_LOADAVG_SZ + (avgs->lg_cur - 1);
10347c478bd9Sstevel@tonic-gate 	for (i = hr_avg = 0; i < slen; i++) {
10357c478bd9Sstevel@tonic-gate 		cpos = (spos - i) >= 0 ? spos - i : S_LOADAVG_SZ + (spos - i);
10367c478bd9Sstevel@tonic-gate 		hr_avg += avgs->lg_loads[cpos];
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	hr_avg = hr_avg / slen;
10407c478bd9Sstevel@tonic-gate 	avg = hr_avg / (NANOSEC / LGRP_LOADAVG_IN_THREAD_MAX);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	return (avg);
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate /*
10467c478bd9Sstevel@tonic-gate  * Run every second from clock () to update the loadavg count available to the
10477c478bd9Sstevel@tonic-gate  * system and cpu-partitions.
10487c478bd9Sstevel@tonic-gate  *
10497c478bd9Sstevel@tonic-gate  * This works by sampling the previous usr, sys, wait time elapsed,
10507c478bd9Sstevel@tonic-gate  * computing a delta, and adding that delta to the elapsed usr, sys,
10517c478bd9Sstevel@tonic-gate  * wait increase.
10527c478bd9Sstevel@tonic-gate  */
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate static void
10557c478bd9Sstevel@tonic-gate loadavg_update()
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	cpu_t *cp;
10587c478bd9Sstevel@tonic-gate 	cpupart_t *cpupart;
10597c478bd9Sstevel@tonic-gate 	hrtime_t cpu_total;
10607c478bd9Sstevel@tonic-gate 	int prev;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	cp = cpu_list;
10637c478bd9Sstevel@tonic-gate 	loadavg.lg_total = 0;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	/*
10667c478bd9Sstevel@tonic-gate 	 * first pass totals up per-cpu statistics for system and cpu
10677c478bd9Sstevel@tonic-gate 	 * partitions
10687c478bd9Sstevel@tonic-gate 	 */
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	do {
10717c478bd9Sstevel@tonic-gate 		struct loadavg_s *lavg;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 		lavg = &cp->cpu_loadavg;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 		cpu_total = cp->cpu_acct[CMS_USER] +
10767c478bd9Sstevel@tonic-gate 		    cp->cpu_acct[CMS_SYSTEM] + cp->cpu_waitrq;
10777c478bd9Sstevel@tonic-gate 		/* compute delta against last total */
10787c478bd9Sstevel@tonic-gate 		scalehrtime(&cpu_total);
10797c478bd9Sstevel@tonic-gate 		prev = (lavg->lg_cur - 1) >= 0 ? lavg->lg_cur - 1 :
10807c478bd9Sstevel@tonic-gate 		    S_LOADAVG_SZ + (lavg->lg_cur - 1);
10817c478bd9Sstevel@tonic-gate 		if (lavg->lg_loads[prev] <= 0) {
10827c478bd9Sstevel@tonic-gate 			lavg->lg_loads[lavg->lg_cur] = cpu_total;
10837c478bd9Sstevel@tonic-gate 			cpu_total = 0;
10847c478bd9Sstevel@tonic-gate 		} else {
10857c478bd9Sstevel@tonic-gate 			lavg->lg_loads[lavg->lg_cur] = cpu_total;
10867c478bd9Sstevel@tonic-gate 			cpu_total = cpu_total - lavg->lg_loads[prev];
10877c478bd9Sstevel@tonic-gate 			if (cpu_total < 0)
10887c478bd9Sstevel@tonic-gate 				cpu_total = 0;
10897c478bd9Sstevel@tonic-gate 		}
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
10927c478bd9Sstevel@tonic-gate 		lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
10937c478bd9Sstevel@tonic-gate 		    lavg->lg_len + 1 : S_LOADAVG_SZ;
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 		loadavg.lg_total += cpu_total;
10967c478bd9Sstevel@tonic-gate 		cp->cpu_part->cp_loadavg.lg_total += cpu_total;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	loadavg.lg_loads[loadavg.lg_cur] = loadavg.lg_total;
11017c478bd9Sstevel@tonic-gate 	loadavg.lg_cur = (loadavg.lg_cur + 1) % S_LOADAVG_SZ;
11027c478bd9Sstevel@tonic-gate 	loadavg.lg_len = (loadavg.lg_len + 1) < S_LOADAVG_SZ ?
11037c478bd9Sstevel@tonic-gate 	    loadavg.lg_len + 1 : S_LOADAVG_SZ;
11047c478bd9Sstevel@tonic-gate 	/*
11057c478bd9Sstevel@tonic-gate 	 * Second pass updates counts
11067c478bd9Sstevel@tonic-gate 	 */
11077c478bd9Sstevel@tonic-gate 	cpupart = cp_list_head;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	do {
11107c478bd9Sstevel@tonic-gate 		struct loadavg_s *lavg;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 		lavg = &cpupart->cp_loadavg;
11137c478bd9Sstevel@tonic-gate 		lavg->lg_loads[lavg->lg_cur] = lavg->lg_total;
11147c478bd9Sstevel@tonic-gate 		lavg->lg_total = 0;
11157c478bd9Sstevel@tonic-gate 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
11167c478bd9Sstevel@tonic-gate 		lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
11177c478bd9Sstevel@tonic-gate 		    lavg->lg_len + 1 : S_LOADAVG_SZ;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	} while ((cpupart = cpupart->cp_next) != cp_list_head);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate /*
11247c478bd9Sstevel@tonic-gate  * clock_update() - local clock update
11257c478bd9Sstevel@tonic-gate  *
11267c478bd9Sstevel@tonic-gate  * This routine is called by ntp_adjtime() to update the local clock
11277c478bd9Sstevel@tonic-gate  * phase and frequency. The implementation is of an
11287c478bd9Sstevel@tonic-gate  * adaptive-parameter, hybrid phase/frequency-lock loop (PLL/FLL). The
11297c478bd9Sstevel@tonic-gate  * routine computes new time and frequency offset estimates for each
11307c478bd9Sstevel@tonic-gate  * call.  The PPS signal itself determines the new time offset,
11317c478bd9Sstevel@tonic-gate  * instead of the calling argument.  Presumably, calls to
11327c478bd9Sstevel@tonic-gate  * ntp_adjtime() occur only when the caller believes the local clock
11337c478bd9Sstevel@tonic-gate  * is valid within some bound (+-128 ms with NTP). If the caller's
11347c478bd9Sstevel@tonic-gate  * time is far different than the PPS time, an argument will ensue,
11357c478bd9Sstevel@tonic-gate  * and it's not clear who will lose.
11367c478bd9Sstevel@tonic-gate  *
11377c478bd9Sstevel@tonic-gate  * For uncompensated quartz crystal oscillatores and nominal update
11387c478bd9Sstevel@tonic-gate  * intervals less than 1024 s, operation should be in phase-lock mode
11397c478bd9Sstevel@tonic-gate  * (STA_FLL = 0), where the loop is disciplined to phase. For update
11407c478bd9Sstevel@tonic-gate  * intervals greater than this, operation should be in frequency-lock
11417c478bd9Sstevel@tonic-gate  * mode (STA_FLL = 1), where the loop is disciplined to frequency.
11427c478bd9Sstevel@tonic-gate  *
11437c478bd9Sstevel@tonic-gate  * Note: mutex(&tod_lock) is in effect.
11447c478bd9Sstevel@tonic-gate  */
11457c478bd9Sstevel@tonic-gate void
11467c478bd9Sstevel@tonic-gate clock_update(int offset)
11477c478bd9Sstevel@tonic-gate {
11487c478bd9Sstevel@tonic-gate 	int ltemp, mtemp, s;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME))
11537c478bd9Sstevel@tonic-gate 		return;
11547c478bd9Sstevel@tonic-gate 	ltemp = offset;
11557c478bd9Sstevel@tonic-gate 	if ((time_status & STA_PPSTIME) && (time_status & STA_PPSSIGNAL))
11567c478bd9Sstevel@tonic-gate 		ltemp = pps_offset;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	/*
11597c478bd9Sstevel@tonic-gate 	 * Scale the phase adjustment and clamp to the operating range.
11607c478bd9Sstevel@tonic-gate 	 */
11617c478bd9Sstevel@tonic-gate 	if (ltemp > MAXPHASE)
11627c478bd9Sstevel@tonic-gate 		time_offset = MAXPHASE * SCALE_UPDATE;
11637c478bd9Sstevel@tonic-gate 	else if (ltemp < -MAXPHASE)
11647c478bd9Sstevel@tonic-gate 		time_offset = -(MAXPHASE * SCALE_UPDATE);
11657c478bd9Sstevel@tonic-gate 	else
11667c478bd9Sstevel@tonic-gate 		time_offset = ltemp * SCALE_UPDATE;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	/*
11697c478bd9Sstevel@tonic-gate 	 * Select whether the frequency is to be controlled and in which
11707c478bd9Sstevel@tonic-gate 	 * mode (PLL or FLL). Clamp to the operating range. Ugly
11717c478bd9Sstevel@tonic-gate 	 * multiply/divide should be replaced someday.
11727c478bd9Sstevel@tonic-gate 	 */
11737c478bd9Sstevel@tonic-gate 	if (time_status & STA_FREQHOLD || time_reftime == 0)
11747c478bd9Sstevel@tonic-gate 		time_reftime = hrestime.tv_sec;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	mtemp = hrestime.tv_sec - time_reftime;
11777c478bd9Sstevel@tonic-gate 	time_reftime = hrestime.tv_sec;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	if (time_status & STA_FLL) {
11807c478bd9Sstevel@tonic-gate 		if (mtemp >= MINSEC) {
11817c478bd9Sstevel@tonic-gate 			ltemp = ((time_offset / mtemp) * (SCALE_USEC /
11827c478bd9Sstevel@tonic-gate 			    SCALE_UPDATE));
11837c478bd9Sstevel@tonic-gate 			if (ltemp)
11847c478bd9Sstevel@tonic-gate 				time_freq += ltemp / SCALE_KH;
11857c478bd9Sstevel@tonic-gate 		}
11867c478bd9Sstevel@tonic-gate 	} else {
11877c478bd9Sstevel@tonic-gate 		if (mtemp < MAXSEC) {
11887c478bd9Sstevel@tonic-gate 			ltemp *= mtemp;
11897c478bd9Sstevel@tonic-gate 			if (ltemp)
11907c478bd9Sstevel@tonic-gate 				time_freq += (int)(((int64_t)ltemp *
11917c478bd9Sstevel@tonic-gate 				    SCALE_USEC) / SCALE_KF)
11927c478bd9Sstevel@tonic-gate 				    / (1 << (time_constant * 2));
11937c478bd9Sstevel@tonic-gate 		}
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate 	if (time_freq > time_tolerance)
11967c478bd9Sstevel@tonic-gate 		time_freq = time_tolerance;
11977c478bd9Sstevel@tonic-gate 	else if (time_freq < -time_tolerance)
11987c478bd9Sstevel@tonic-gate 		time_freq = -time_tolerance;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	s = hr_clock_lock();
12017c478bd9Sstevel@tonic-gate 	tod_needsync = 1;
12027c478bd9Sstevel@tonic-gate 	hr_clock_unlock(s);
12037c478bd9Sstevel@tonic-gate }
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate /*
12067c478bd9Sstevel@tonic-gate  * ddi_hardpps() - discipline CPU clock oscillator to external PPS signal
12077c478bd9Sstevel@tonic-gate  *
12087c478bd9Sstevel@tonic-gate  * This routine is called at each PPS interrupt in order to discipline
12097c478bd9Sstevel@tonic-gate  * the CPU clock oscillator to the PPS signal. It measures the PPS phase
12107c478bd9Sstevel@tonic-gate  * and leaves it in a handy spot for the clock() routine. It
12117c478bd9Sstevel@tonic-gate  * integrates successive PPS phase differences and calculates the
12127c478bd9Sstevel@tonic-gate  * frequency offset. This is used in clock() to discipline the CPU
12137c478bd9Sstevel@tonic-gate  * clock oscillator so that intrinsic frequency error is cancelled out.
12147c478bd9Sstevel@tonic-gate  * The code requires the caller to capture the time and hardware counter
12157c478bd9Sstevel@tonic-gate  * value at the on-time PPS signal transition.
12167c478bd9Sstevel@tonic-gate  *
12177c478bd9Sstevel@tonic-gate  * Note that, on some Unix systems, this routine runs at an interrupt
12187c478bd9Sstevel@tonic-gate  * priority level higher than the timer interrupt routine clock().
12197c478bd9Sstevel@tonic-gate  * Therefore, the variables used are distinct from the clock()
12207c478bd9Sstevel@tonic-gate  * variables, except for certain exceptions: The PPS frequency pps_freq
12217c478bd9Sstevel@tonic-gate  * and phase pps_offset variables are determined by this routine and
12227c478bd9Sstevel@tonic-gate  * updated atomically. The time_tolerance variable can be considered a
12237c478bd9Sstevel@tonic-gate  * constant, since it is infrequently changed, and then only when the
12247c478bd9Sstevel@tonic-gate  * PPS signal is disabled. The watchdog counter pps_valid is updated
12257c478bd9Sstevel@tonic-gate  * once per second by clock() and is atomically cleared in this
12267c478bd9Sstevel@tonic-gate  * routine.
12277c478bd9Sstevel@tonic-gate  *
12287c478bd9Sstevel@tonic-gate  * tvp is the time of the last tick; usec is a microsecond count since the
12297c478bd9Sstevel@tonic-gate  * last tick.
12307c478bd9Sstevel@tonic-gate  *
12317c478bd9Sstevel@tonic-gate  * Note: In Solaris systems, the tick value is actually given by
12327c478bd9Sstevel@tonic-gate  *       usec_per_tick.  This is called from the serial driver cdintr(),
12337c478bd9Sstevel@tonic-gate  *	 or equivalent, at a high PIL.  Because the kernel keeps a
12347c478bd9Sstevel@tonic-gate  *	 highresolution time, the following code can accept either
12357c478bd9Sstevel@tonic-gate  *	 the traditional argument pair, or the current highres timestamp
12367c478bd9Sstevel@tonic-gate  *       in tvp and zero in usec.
12377c478bd9Sstevel@tonic-gate  */
12387c478bd9Sstevel@tonic-gate void
12397c478bd9Sstevel@tonic-gate ddi_hardpps(struct timeval *tvp, int usec)
12407c478bd9Sstevel@tonic-gate {
12417c478bd9Sstevel@tonic-gate 	int u_usec, v_usec, bigtick;
12427c478bd9Sstevel@tonic-gate 	time_t cal_sec;
12437c478bd9Sstevel@tonic-gate 	int cal_usec;
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	/*
12467c478bd9Sstevel@tonic-gate 	 * An occasional glitch can be produced when the PPS interrupt
12477c478bd9Sstevel@tonic-gate 	 * occurs in the clock() routine before the time variable is
12487c478bd9Sstevel@tonic-gate 	 * updated. Here the offset is discarded when the difference
12497c478bd9Sstevel@tonic-gate 	 * between it and the last one is greater than tick/2, but not
12507c478bd9Sstevel@tonic-gate 	 * if the interval since the first discard exceeds 30 s.
12517c478bd9Sstevel@tonic-gate 	 */
12527c478bd9Sstevel@tonic-gate 	time_status |= STA_PPSSIGNAL;
12537c478bd9Sstevel@tonic-gate 	time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
12547c478bd9Sstevel@tonic-gate 	pps_valid = 0;
12557c478bd9Sstevel@tonic-gate 	u_usec = -tvp->tv_usec;
12567c478bd9Sstevel@tonic-gate 	if (u_usec < -(MICROSEC/2))
12577c478bd9Sstevel@tonic-gate 		u_usec += MICROSEC;
12587c478bd9Sstevel@tonic-gate 	v_usec = pps_offset - u_usec;
12597c478bd9Sstevel@tonic-gate 	if (v_usec < 0)
12607c478bd9Sstevel@tonic-gate 		v_usec = -v_usec;
12617c478bd9Sstevel@tonic-gate 	if (v_usec > (usec_per_tick >> 1)) {
12627c478bd9Sstevel@tonic-gate 		if (pps_glitch > MAXGLITCH) {
12637c478bd9Sstevel@tonic-gate 			pps_glitch = 0;
12647c478bd9Sstevel@tonic-gate 			pps_tf[2] = u_usec;
12657c478bd9Sstevel@tonic-gate 			pps_tf[1] = u_usec;
12667c478bd9Sstevel@tonic-gate 		} else {
12677c478bd9Sstevel@tonic-gate 			pps_glitch++;
12687c478bd9Sstevel@tonic-gate 			u_usec = pps_offset;
12697c478bd9Sstevel@tonic-gate 		}
12707c478bd9Sstevel@tonic-gate 	} else
12717c478bd9Sstevel@tonic-gate 		pps_glitch = 0;
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	/*
12747c478bd9Sstevel@tonic-gate 	 * A three-stage median filter is used to help deglitch the pps
12757c478bd9Sstevel@tonic-gate 	 * time. The median sample becomes the time offset estimate; the
12767c478bd9Sstevel@tonic-gate 	 * difference between the other two samples becomes the time
12777c478bd9Sstevel@tonic-gate 	 * dispersion (jitter) estimate.
12787c478bd9Sstevel@tonic-gate 	 */
12797c478bd9Sstevel@tonic-gate 	pps_tf[2] = pps_tf[1];
12807c478bd9Sstevel@tonic-gate 	pps_tf[1] = pps_tf[0];
12817c478bd9Sstevel@tonic-gate 	pps_tf[0] = u_usec;
12827c478bd9Sstevel@tonic-gate 	if (pps_tf[0] > pps_tf[1]) {
12837c478bd9Sstevel@tonic-gate 		if (pps_tf[1] > pps_tf[2]) {
12847c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[1];		/* 0 1 2 */
12857c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[0] - pps_tf[2];
12867c478bd9Sstevel@tonic-gate 		} else if (pps_tf[2] > pps_tf[0]) {
12877c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[0];		/* 2 0 1 */
12887c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[2] - pps_tf[1];
12897c478bd9Sstevel@tonic-gate 		} else {
12907c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[2];		/* 0 2 1 */
12917c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[0] - pps_tf[1];
12927c478bd9Sstevel@tonic-gate 		}
12937c478bd9Sstevel@tonic-gate 	} else {
12947c478bd9Sstevel@tonic-gate 		if (pps_tf[1] < pps_tf[2]) {
12957c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[1];		/* 2 1 0 */
12967c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[2] - pps_tf[0];
12977c478bd9Sstevel@tonic-gate 		} else  if (pps_tf[2] < pps_tf[0]) {
12987c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[0];		/* 1 0 2 */
12997c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[1] - pps_tf[2];
13007c478bd9Sstevel@tonic-gate 		} else {
13017c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[2];		/* 1 2 0 */
13027c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[1] - pps_tf[0];
13037c478bd9Sstevel@tonic-gate 		}
13047c478bd9Sstevel@tonic-gate 	}
13057c478bd9Sstevel@tonic-gate 	if (v_usec > MAXTIME)
13067c478bd9Sstevel@tonic-gate 		pps_jitcnt++;
13077c478bd9Sstevel@tonic-gate 	v_usec = (v_usec << PPS_AVG) - pps_jitter;
13087c478bd9Sstevel@tonic-gate 	pps_jitter += v_usec / (1 << PPS_AVG);
13097c478bd9Sstevel@tonic-gate 	if (pps_jitter > (MAXTIME >> 1))
13107c478bd9Sstevel@tonic-gate 		time_status |= STA_PPSJITTER;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	/*
13137c478bd9Sstevel@tonic-gate 	 * During the calibration interval adjust the starting time when
13147c478bd9Sstevel@tonic-gate 	 * the tick overflows. At the end of the interval compute the
13157c478bd9Sstevel@tonic-gate 	 * duration of the interval and the difference of the hardware
13167c478bd9Sstevel@tonic-gate 	 * counters at the beginning and end of the interval. This code
13177c478bd9Sstevel@tonic-gate 	 * is deliciously complicated by the fact valid differences may
13187c478bd9Sstevel@tonic-gate 	 * exceed the value of tick when using long calibration
13197c478bd9Sstevel@tonic-gate 	 * intervals and small ticks. Note that the counter can be
13207c478bd9Sstevel@tonic-gate 	 * greater than tick if caught at just the wrong instant, but
13217c478bd9Sstevel@tonic-gate 	 * the values returned and used here are correct.
13227c478bd9Sstevel@tonic-gate 	 */
13237c478bd9Sstevel@tonic-gate 	bigtick = (int)usec_per_tick * SCALE_USEC;
13247c478bd9Sstevel@tonic-gate 	pps_usec -= pps_freq;
13257c478bd9Sstevel@tonic-gate 	if (pps_usec >= bigtick)
13267c478bd9Sstevel@tonic-gate 		pps_usec -= bigtick;
13277c478bd9Sstevel@tonic-gate 	if (pps_usec < 0)
13287c478bd9Sstevel@tonic-gate 		pps_usec += bigtick;
13297c478bd9Sstevel@tonic-gate 	pps_time.tv_sec++;
13307c478bd9Sstevel@tonic-gate 	pps_count++;
13317c478bd9Sstevel@tonic-gate 	if (pps_count < (1 << pps_shift))
13327c478bd9Sstevel@tonic-gate 		return;
13337c478bd9Sstevel@tonic-gate 	pps_count = 0;
13347c478bd9Sstevel@tonic-gate 	pps_calcnt++;
13357c478bd9Sstevel@tonic-gate 	u_usec = usec * SCALE_USEC;
13367c478bd9Sstevel@tonic-gate 	v_usec = pps_usec - u_usec;
13377c478bd9Sstevel@tonic-gate 	if (v_usec >= bigtick >> 1)
13387c478bd9Sstevel@tonic-gate 		v_usec -= bigtick;
13397c478bd9Sstevel@tonic-gate 	if (v_usec < -(bigtick >> 1))
13407c478bd9Sstevel@tonic-gate 		v_usec += bigtick;
13417c478bd9Sstevel@tonic-gate 	if (v_usec < 0)
13427c478bd9Sstevel@tonic-gate 		v_usec = -(-v_usec >> pps_shift);
13437c478bd9Sstevel@tonic-gate 	else
13447c478bd9Sstevel@tonic-gate 		v_usec = v_usec >> pps_shift;
13457c478bd9Sstevel@tonic-gate 	pps_usec = u_usec;
13467c478bd9Sstevel@tonic-gate 	cal_sec = tvp->tv_sec;
13477c478bd9Sstevel@tonic-gate 	cal_usec = tvp->tv_usec;
13487c478bd9Sstevel@tonic-gate 	cal_sec -= pps_time.tv_sec;
13497c478bd9Sstevel@tonic-gate 	cal_usec -= pps_time.tv_usec;
13507c478bd9Sstevel@tonic-gate 	if (cal_usec < 0) {
13517c478bd9Sstevel@tonic-gate 		cal_usec += MICROSEC;
13527c478bd9Sstevel@tonic-gate 		cal_sec--;
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 	pps_time = *tvp;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	/*
13577c478bd9Sstevel@tonic-gate 	 * Check for lost interrupts, noise, excessive jitter and
13587c478bd9Sstevel@tonic-gate 	 * excessive frequency error. The number of timer ticks during
13597c478bd9Sstevel@tonic-gate 	 * the interval may vary +-1 tick. Add to this a margin of one
13607c478bd9Sstevel@tonic-gate 	 * tick for the PPS signal jitter and maximum frequency
13617c478bd9Sstevel@tonic-gate 	 * deviation. If the limits are exceeded, the calibration
13627c478bd9Sstevel@tonic-gate 	 * interval is reset to the minimum and we start over.
13637c478bd9Sstevel@tonic-gate 	 */
13647c478bd9Sstevel@tonic-gate 	u_usec = (int)usec_per_tick << 1;
13657c478bd9Sstevel@tonic-gate 	if (!((cal_sec == -1 && cal_usec > (MICROSEC - u_usec)) ||
13667c478bd9Sstevel@tonic-gate 	    (cal_sec == 0 && cal_usec < u_usec)) ||
13677c478bd9Sstevel@tonic-gate 	    v_usec > time_tolerance || v_usec < -time_tolerance) {
13687c478bd9Sstevel@tonic-gate 		pps_errcnt++;
13697c478bd9Sstevel@tonic-gate 		pps_shift = PPS_SHIFT;
13707c478bd9Sstevel@tonic-gate 		pps_intcnt = 0;
13717c478bd9Sstevel@tonic-gate 		time_status |= STA_PPSERROR;
13727c478bd9Sstevel@tonic-gate 		return;
13737c478bd9Sstevel@tonic-gate 	}
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	/*
13767c478bd9Sstevel@tonic-gate 	 * A three-stage median filter is used to help deglitch the pps
13777c478bd9Sstevel@tonic-gate 	 * frequency. The median sample becomes the frequency offset
13787c478bd9Sstevel@tonic-gate 	 * estimate; the difference between the other two samples
13797c478bd9Sstevel@tonic-gate 	 * becomes the frequency dispersion (stability) estimate.
13807c478bd9Sstevel@tonic-gate 	 */
13817c478bd9Sstevel@tonic-gate 	pps_ff[2] = pps_ff[1];
13827c478bd9Sstevel@tonic-gate 	pps_ff[1] = pps_ff[0];
13837c478bd9Sstevel@tonic-gate 	pps_ff[0] = v_usec;
13847c478bd9Sstevel@tonic-gate 	if (pps_ff[0] > pps_ff[1]) {
13857c478bd9Sstevel@tonic-gate 		if (pps_ff[1] > pps_ff[2]) {
13867c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[1];		/* 0 1 2 */
13877c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[0] - pps_ff[2];
13887c478bd9Sstevel@tonic-gate 		} else if (pps_ff[2] > pps_ff[0]) {
13897c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[0];		/* 2 0 1 */
13907c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[2] - pps_ff[1];
13917c478bd9Sstevel@tonic-gate 		} else {
13927c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[2];		/* 0 2 1 */
13937c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[0] - pps_ff[1];
13947c478bd9Sstevel@tonic-gate 		}
13957c478bd9Sstevel@tonic-gate 	} else {
13967c478bd9Sstevel@tonic-gate 		if (pps_ff[1] < pps_ff[2]) {
13977c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[1];		/* 2 1 0 */
13987c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[2] - pps_ff[0];
13997c478bd9Sstevel@tonic-gate 		} else  if (pps_ff[2] < pps_ff[0]) {
14007c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[0];		/* 1 0 2 */
14017c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[1] - pps_ff[2];
14027c478bd9Sstevel@tonic-gate 		} else {
14037c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[2];		/* 1 2 0 */
14047c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[1] - pps_ff[0];
14057c478bd9Sstevel@tonic-gate 		}
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	/*
14097c478bd9Sstevel@tonic-gate 	 * Here the frequency dispersion (stability) is updated. If it
14107c478bd9Sstevel@tonic-gate 	 * is less than one-fourth the maximum (MAXFREQ), the frequency
14117c478bd9Sstevel@tonic-gate 	 * offset is updated as well, but clamped to the tolerance. It
14127c478bd9Sstevel@tonic-gate 	 * will be processed later by the clock() routine.
14137c478bd9Sstevel@tonic-gate 	 */
14147c478bd9Sstevel@tonic-gate 	v_usec = (v_usec >> 1) - pps_stabil;
14157c478bd9Sstevel@tonic-gate 	if (v_usec < 0)
14167c478bd9Sstevel@tonic-gate 		pps_stabil -= -v_usec >> PPS_AVG;
14177c478bd9Sstevel@tonic-gate 	else
14187c478bd9Sstevel@tonic-gate 		pps_stabil += v_usec >> PPS_AVG;
14197c478bd9Sstevel@tonic-gate 	if (pps_stabil > MAXFREQ >> 2) {
14207c478bd9Sstevel@tonic-gate 		pps_stbcnt++;
14217c478bd9Sstevel@tonic-gate 		time_status |= STA_PPSWANDER;
14227c478bd9Sstevel@tonic-gate 		return;
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 	if (time_status & STA_PPSFREQ) {
14257c478bd9Sstevel@tonic-gate 		if (u_usec < 0) {
14267c478bd9Sstevel@tonic-gate 			pps_freq -= -u_usec >> PPS_AVG;
14277c478bd9Sstevel@tonic-gate 			if (pps_freq < -time_tolerance)
14287c478bd9Sstevel@tonic-gate 				pps_freq = -time_tolerance;
14297c478bd9Sstevel@tonic-gate 			u_usec = -u_usec;
14307c478bd9Sstevel@tonic-gate 		} else {
14317c478bd9Sstevel@tonic-gate 			pps_freq += u_usec >> PPS_AVG;
14327c478bd9Sstevel@tonic-gate 			if (pps_freq > time_tolerance)
14337c478bd9Sstevel@tonic-gate 				pps_freq = time_tolerance;
14347c478bd9Sstevel@tonic-gate 		}
14357c478bd9Sstevel@tonic-gate 	}
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	/*
14387c478bd9Sstevel@tonic-gate 	 * Here the calibration interval is adjusted. If the maximum
14397c478bd9Sstevel@tonic-gate 	 * time difference is greater than tick / 4, reduce the interval
14407c478bd9Sstevel@tonic-gate 	 * by half. If this is not the case for four consecutive
14417c478bd9Sstevel@tonic-gate 	 * intervals, double the interval.
14427c478bd9Sstevel@tonic-gate 	 */
14437c478bd9Sstevel@tonic-gate 	if (u_usec << pps_shift > bigtick >> 2) {
14447c478bd9Sstevel@tonic-gate 		pps_intcnt = 0;
14457c478bd9Sstevel@tonic-gate 		if (pps_shift > PPS_SHIFT)
14467c478bd9Sstevel@tonic-gate 			pps_shift--;
14477c478bd9Sstevel@tonic-gate 	} else if (pps_intcnt >= 4) {
14487c478bd9Sstevel@tonic-gate 		pps_intcnt = 0;
14497c478bd9Sstevel@tonic-gate 		if (pps_shift < PPS_SHIFTMAX)
14507c478bd9Sstevel@tonic-gate 			pps_shift++;
14517c478bd9Sstevel@tonic-gate 	} else
14527c478bd9Sstevel@tonic-gate 		pps_intcnt++;
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	/*
14557c478bd9Sstevel@tonic-gate 	 * If recovering from kmdb, then make sure the tod chip gets resynced.
14567c478bd9Sstevel@tonic-gate 	 * If we took an early exit above, then we don't yet have a stable
14577c478bd9Sstevel@tonic-gate 	 * calibration signal to lock onto, so don't mark the tod for sync
14587c478bd9Sstevel@tonic-gate 	 * until we get all the way here.
14597c478bd9Sstevel@tonic-gate 	 */
14607c478bd9Sstevel@tonic-gate 	{
14617c478bd9Sstevel@tonic-gate 		int s = hr_clock_lock();
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 		tod_needsync = 1;
14647c478bd9Sstevel@tonic-gate 		hr_clock_unlock(s);
14657c478bd9Sstevel@tonic-gate 	}
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate /*
14697c478bd9Sstevel@tonic-gate  * Handle clock tick processing for a thread.
14707c478bd9Sstevel@tonic-gate  * Check for timer action, enforce CPU rlimit, do profiling etc.
14717c478bd9Sstevel@tonic-gate  */
14727c478bd9Sstevel@tonic-gate void
14737c478bd9Sstevel@tonic-gate clock_tick(kthread_t *t)
14747c478bd9Sstevel@tonic-gate {
14757c478bd9Sstevel@tonic-gate 	struct proc *pp;
14767c478bd9Sstevel@tonic-gate 	klwp_id_t    lwp;
14777c478bd9Sstevel@tonic-gate 	struct as *as;
14787c478bd9Sstevel@tonic-gate 	clock_t	utime;
14797c478bd9Sstevel@tonic-gate 	clock_t	stime;
14807c478bd9Sstevel@tonic-gate 	int	poke = 0;		/* notify another CPU */
14817c478bd9Sstevel@tonic-gate 	int	user_mode;
14827c478bd9Sstevel@tonic-gate 	size_t	 rss;
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	/* Must be operating on a lwp/thread */
14857c478bd9Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) == NULL) {
14867c478bd9Sstevel@tonic-gate 		panic("clock_tick: no lwp");
14877c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	CL_TICK(t);	/* Class specific tick processing */
14917c478bd9Sstevel@tonic-gate 	DTRACE_SCHED1(tick, kthread_t *, t);
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	pp = ttoproc(t);
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	/* pp->p_lock makes sure that the thread does not exit */
14967c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pp->p_lock));
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	user_mode = (lwp->lwp_state == LWP_USER);
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	/*
15017c478bd9Sstevel@tonic-gate 	 * Update process times. Should use high res clock and state
15027c478bd9Sstevel@tonic-gate 	 * changes instead of statistical sampling method. XXX
15037c478bd9Sstevel@tonic-gate 	 */
15047c478bd9Sstevel@tonic-gate 	if (user_mode) {
15057c478bd9Sstevel@tonic-gate 		pp->p_utime++;
15067c478bd9Sstevel@tonic-gate 		pp->p_task->tk_cpu_time++;
15077c478bd9Sstevel@tonic-gate 	} else {
15087c478bd9Sstevel@tonic-gate 		pp->p_stime++;
15097c478bd9Sstevel@tonic-gate 		pp->p_task->tk_cpu_time++;
15107c478bd9Sstevel@tonic-gate 	}
15117c478bd9Sstevel@tonic-gate 	as = pp->p_as;
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	/*
15147c478bd9Sstevel@tonic-gate 	 * Update user profiling statistics. Get the pc from the
15157c478bd9Sstevel@tonic-gate 	 * lwp when the AST happens.
15167c478bd9Sstevel@tonic-gate 	 */
15177c478bd9Sstevel@tonic-gate 	if (pp->p_prof.pr_scale) {
15187c478bd9Sstevel@tonic-gate 		atomic_add_32(&lwp->lwp_oweupc, 1);
15197c478bd9Sstevel@tonic-gate 		if (user_mode) {
15207c478bd9Sstevel@tonic-gate 			poke = 1;
15217c478bd9Sstevel@tonic-gate 			aston(t);
15227c478bd9Sstevel@tonic-gate 		}
15237c478bd9Sstevel@tonic-gate 	}
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	utime = pp->p_utime;
15267c478bd9Sstevel@tonic-gate 	stime = pp->p_stime;
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	/*
15297c478bd9Sstevel@tonic-gate 	 * If CPU was in user state, process lwp-virtual time
15307c478bd9Sstevel@tonic-gate 	 * interval timer.
15317c478bd9Sstevel@tonic-gate 	 */
15327c478bd9Sstevel@tonic-gate 	if (user_mode &&
15337c478bd9Sstevel@tonic-gate 	    timerisset(&lwp->lwp_timer[ITIMER_VIRTUAL].it_value) &&
15347c478bd9Sstevel@tonic-gate 	    itimerdecr(&lwp->lwp_timer[ITIMER_VIRTUAL], usec_per_tick) == 0) {
15357c478bd9Sstevel@tonic-gate 		poke = 1;
15367c478bd9Sstevel@tonic-gate 		sigtoproc(pp, t, SIGVTALRM);
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	if (timerisset(&lwp->lwp_timer[ITIMER_PROF].it_value) &&
15407c478bd9Sstevel@tonic-gate 	    itimerdecr(&lwp->lwp_timer[ITIMER_PROF], usec_per_tick) == 0) {
15417c478bd9Sstevel@tonic-gate 		poke = 1;
15427c478bd9Sstevel@tonic-gate 		sigtoproc(pp, t, SIGPROF);
15437c478bd9Sstevel@tonic-gate 	}
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	/*
15467c478bd9Sstevel@tonic-gate 	 * Enforce CPU resource controls:
15477c478bd9Sstevel@tonic-gate 	 *   (a) process.max-cpu-time resource control
15487c478bd9Sstevel@tonic-gate 	 */
15497c478bd9Sstevel@tonic-gate 	(void) rctl_test(rctlproc_legacy[RLIMIT_CPU], pp->p_rctls, pp,
15507c478bd9Sstevel@tonic-gate 	    (utime + stime)/hz, RCA_UNSAFE_SIGINFO);
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	/*
15537c478bd9Sstevel@tonic-gate 	 *   (b) task.max-cpu-time resource control
15547c478bd9Sstevel@tonic-gate 	 */
15557c478bd9Sstevel@tonic-gate 	(void) rctl_test(rc_task_cpu_time, pp->p_task->tk_rctls, pp, 1,
15567c478bd9Sstevel@tonic-gate 	    RCA_UNSAFE_SIGINFO);
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	/*
15597c478bd9Sstevel@tonic-gate 	 * Update memory usage for the currently running process.
15607c478bd9Sstevel@tonic-gate 	 */
15617c478bd9Sstevel@tonic-gate 	rss = rm_asrss(as);
15627c478bd9Sstevel@tonic-gate 	PTOU(pp)->u_mem += rss;
15637c478bd9Sstevel@tonic-gate 	if (rss > PTOU(pp)->u_mem_max)
15647c478bd9Sstevel@tonic-gate 		PTOU(pp)->u_mem_max = rss;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	/*
15677c478bd9Sstevel@tonic-gate 	 * Notify the CPU the thread is running on.
15687c478bd9Sstevel@tonic-gate 	 */
15697c478bd9Sstevel@tonic-gate 	if (poke && t->t_cpu != CPU)
15707c478bd9Sstevel@tonic-gate 		poke_cpu(t->t_cpu->cpu_id);
15717c478bd9Sstevel@tonic-gate }
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate void
15747c478bd9Sstevel@tonic-gate profil_tick(uintptr_t upc)
15757c478bd9Sstevel@tonic-gate {
15767c478bd9Sstevel@tonic-gate 	int ticks;
15777c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
15787c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
15797c478bd9Sstevel@tonic-gate 	struct prof *pr = &p->p_prof;
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	do {
15827c478bd9Sstevel@tonic-gate 		ticks = lwp->lwp_oweupc;
15837c478bd9Sstevel@tonic-gate 	} while (cas32(&lwp->lwp_oweupc, ticks, 0) != ticks);
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_pflock);
15867c478bd9Sstevel@tonic-gate 	if (pr->pr_scale >= 2 && upc >= pr->pr_off) {
15877c478bd9Sstevel@tonic-gate 		/*
15887c478bd9Sstevel@tonic-gate 		 * Old-style profiling
15897c478bd9Sstevel@tonic-gate 		 */
15907c478bd9Sstevel@tonic-gate 		uint16_t *slot = pr->pr_base;
15917c478bd9Sstevel@tonic-gate 		uint16_t old, new;
15927c478bd9Sstevel@tonic-gate 		if (pr->pr_scale != 2) {
15937c478bd9Sstevel@tonic-gate 			uintptr_t delta = upc - pr->pr_off;
15947c478bd9Sstevel@tonic-gate 			uintptr_t byteoff = ((delta >> 16) * pr->pr_scale) +
15957c478bd9Sstevel@tonic-gate 			    (((delta & 0xffff) * pr->pr_scale) >> 16);
15967c478bd9Sstevel@tonic-gate 			if (byteoff >= (uintptr_t)pr->pr_size) {
15977c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_pflock);
15987c478bd9Sstevel@tonic-gate 				return;
15997c478bd9Sstevel@tonic-gate 			}
16007c478bd9Sstevel@tonic-gate 			slot += byteoff / sizeof (uint16_t);
16017c478bd9Sstevel@tonic-gate 		}
16027c478bd9Sstevel@tonic-gate 		if (fuword16(slot, &old) < 0 ||
16037c478bd9Sstevel@tonic-gate 		    (new = old + ticks) > SHRT_MAX ||
16047c478bd9Sstevel@tonic-gate 		    suword16(slot, new) < 0) {
16057c478bd9Sstevel@tonic-gate 			pr->pr_scale = 0;
16067c478bd9Sstevel@tonic-gate 		}
16077c478bd9Sstevel@tonic-gate 	} else if (pr->pr_scale == 1) {
16087c478bd9Sstevel@tonic-gate 		/*
16097c478bd9Sstevel@tonic-gate 		 * PC Sampling
16107c478bd9Sstevel@tonic-gate 		 */
16117c478bd9Sstevel@tonic-gate 		model_t model = lwp_getdatamodel(lwp);
16127c478bd9Sstevel@tonic-gate 		int result;
16137c478bd9Sstevel@tonic-gate #ifdef __lint
16147c478bd9Sstevel@tonic-gate 		model = model;
16157c478bd9Sstevel@tonic-gate #endif
16167c478bd9Sstevel@tonic-gate 		while (ticks-- > 0) {
16177c478bd9Sstevel@tonic-gate 			if (pr->pr_samples == pr->pr_size) {
16187c478bd9Sstevel@tonic-gate 				/* buffer full, turn off sampling */
16197c478bd9Sstevel@tonic-gate 				pr->pr_scale = 0;
16207c478bd9Sstevel@tonic-gate 				break;
16217c478bd9Sstevel@tonic-gate 			}
16227c478bd9Sstevel@tonic-gate 			switch (SIZEOF_PTR(model)) {
16237c478bd9Sstevel@tonic-gate 			case sizeof (uint32_t):
16247c478bd9Sstevel@tonic-gate 				result = suword32(pr->pr_base, (uint32_t)upc);
16257c478bd9Sstevel@tonic-gate 				break;
16267c478bd9Sstevel@tonic-gate #ifdef _LP64
16277c478bd9Sstevel@tonic-gate 			case sizeof (uint64_t):
16287c478bd9Sstevel@tonic-gate 				result = suword64(pr->pr_base, (uint64_t)upc);
16297c478bd9Sstevel@tonic-gate 				break;
16307c478bd9Sstevel@tonic-gate #endif
16317c478bd9Sstevel@tonic-gate 			default:
16327c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "profil_tick: unexpected "
16337c478bd9Sstevel@tonic-gate 				    "data model");
16347c478bd9Sstevel@tonic-gate 				result = -1;
16357c478bd9Sstevel@tonic-gate 				break;
16367c478bd9Sstevel@tonic-gate 			}
16377c478bd9Sstevel@tonic-gate 			if (result != 0) {
16387c478bd9Sstevel@tonic-gate 				pr->pr_scale = 0;
16397c478bd9Sstevel@tonic-gate 				break;
16407c478bd9Sstevel@tonic-gate 			}
16417c478bd9Sstevel@tonic-gate 			pr->pr_base = (caddr_t)pr->pr_base + SIZEOF_PTR(model);
16427c478bd9Sstevel@tonic-gate 			pr->pr_samples++;
16437c478bd9Sstevel@tonic-gate 		}
16447c478bd9Sstevel@tonic-gate 	}
16457c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_pflock);
16467c478bd9Sstevel@tonic-gate }
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate static void
16497c478bd9Sstevel@tonic-gate delay_wakeup(void *arg)
16507c478bd9Sstevel@tonic-gate {
16517c478bd9Sstevel@tonic-gate 	kthread_t *t = arg;
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	mutex_enter(&t->t_delay_lock);
16547c478bd9Sstevel@tonic-gate 	cv_signal(&t->t_delay_cv);
16557c478bd9Sstevel@tonic-gate 	mutex_exit(&t->t_delay_lock);
16567c478bd9Sstevel@tonic-gate }
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate void
16597c478bd9Sstevel@tonic-gate delay(clock_t ticks)
16607c478bd9Sstevel@tonic-gate {
16617c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
16627c478bd9Sstevel@tonic-gate 	clock_t deadline = lbolt + ticks;
16637c478bd9Sstevel@tonic-gate 	clock_t timeleft;
16647c478bd9Sstevel@tonic-gate 	timeout_id_t id;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if (panicstr && ticks > 0) {
16677c478bd9Sstevel@tonic-gate 		/*
16687c478bd9Sstevel@tonic-gate 		 * Timeouts aren't running, so all we can do is spin.
16697c478bd9Sstevel@tonic-gate 		 */
16707c478bd9Sstevel@tonic-gate 		drv_usecwait(TICK_TO_USEC(ticks));
16717c478bd9Sstevel@tonic-gate 		return;
16727c478bd9Sstevel@tonic-gate 	}
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	while ((timeleft = deadline - lbolt) > 0) {
16757c478bd9Sstevel@tonic-gate 		mutex_enter(&t->t_delay_lock);
16767c478bd9Sstevel@tonic-gate 		id = timeout(delay_wakeup, t, timeleft);
16777c478bd9Sstevel@tonic-gate 		cv_wait(&t->t_delay_cv, &t->t_delay_lock);
16787c478bd9Sstevel@tonic-gate 		mutex_exit(&t->t_delay_lock);
16797c478bd9Sstevel@tonic-gate 		(void) untimeout(id);
16807c478bd9Sstevel@tonic-gate 	}
16817c478bd9Sstevel@tonic-gate }
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate /*
16847c478bd9Sstevel@tonic-gate  * Like delay, but interruptible by a signal.
16857c478bd9Sstevel@tonic-gate  */
16867c478bd9Sstevel@tonic-gate int
16877c478bd9Sstevel@tonic-gate delay_sig(clock_t ticks)
16887c478bd9Sstevel@tonic-gate {
16897c478bd9Sstevel@tonic-gate 	clock_t deadline = lbolt + ticks;
16907c478bd9Sstevel@tonic-gate 	clock_t rc;
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	mutex_enter(&curthread->t_delay_lock);
16937c478bd9Sstevel@tonic-gate 	do {
16947c478bd9Sstevel@tonic-gate 		rc = cv_timedwait_sig(&curthread->t_delay_cv,
16957c478bd9Sstevel@tonic-gate 		    &curthread->t_delay_lock, deadline);
16967c478bd9Sstevel@tonic-gate 	} while (rc > 0);
16977c478bd9Sstevel@tonic-gate 	mutex_exit(&curthread->t_delay_lock);
16987c478bd9Sstevel@tonic-gate 	if (rc == 0)
16997c478bd9Sstevel@tonic-gate 		return (EINTR);
17007c478bd9Sstevel@tonic-gate 	return (0);
17017c478bd9Sstevel@tonic-gate }
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate #define	SECONDS_PER_DAY 86400
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate /*
17067c478bd9Sstevel@tonic-gate  * Initialize the system time based on the TOD chip.  approx is used as
17077c478bd9Sstevel@tonic-gate  * an approximation of time (e.g. from the filesystem) in the event that
17087c478bd9Sstevel@tonic-gate  * the TOD chip has been cleared or is unresponsive.  An approx of -1
17097c478bd9Sstevel@tonic-gate  * means the filesystem doesn't keep time.
17107c478bd9Sstevel@tonic-gate  */
17117c478bd9Sstevel@tonic-gate void
17127c478bd9Sstevel@tonic-gate clkset(time_t approx)
17137c478bd9Sstevel@tonic-gate {
17147c478bd9Sstevel@tonic-gate 	timestruc_t ts;
17157c478bd9Sstevel@tonic-gate 	int spl;
17167c478bd9Sstevel@tonic-gate 	int set_clock = 0;
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	mutex_enter(&tod_lock);
17197c478bd9Sstevel@tonic-gate 	ts = tod_get();
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	if (ts.tv_sec > 365 * SECONDS_PER_DAY) {
17227c478bd9Sstevel@tonic-gate 		/*
17237c478bd9Sstevel@tonic-gate 		 * If the TOD chip is reporting some time after 1971,
17247c478bd9Sstevel@tonic-gate 		 * then it probably didn't lose power or become otherwise
17257c478bd9Sstevel@tonic-gate 		 * cleared in the recent past;  check to assure that
17267c478bd9Sstevel@tonic-gate 		 * the time coming from the filesystem isn't in the future
17277c478bd9Sstevel@tonic-gate 		 * according to the TOD chip.
17287c478bd9Sstevel@tonic-gate 		 */
17297c478bd9Sstevel@tonic-gate 		if (approx != -1 && approx > ts.tv_sec) {
17307c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Last shutdown is later "
17317c478bd9Sstevel@tonic-gate 			    "than time on time-of-day chip; check date.");
17327c478bd9Sstevel@tonic-gate 		}
17337c478bd9Sstevel@tonic-gate 	} else {
17347c478bd9Sstevel@tonic-gate 		/*
17357c478bd9Sstevel@tonic-gate 		 * If the TOD chip isn't giving correct time, then set it to
17367c478bd9Sstevel@tonic-gate 		 * the time that was passed in as a rough estimate.  If we
17377c478bd9Sstevel@tonic-gate 		 * don't have an estimate, then set the clock back to a time
17387c478bd9Sstevel@tonic-gate 		 * when Oliver North, ALF and Dire Straits were all on the
17397c478bd9Sstevel@tonic-gate 		 * collective brain:  1987.
17407c478bd9Sstevel@tonic-gate 		 */
17417c478bd9Sstevel@tonic-gate 		timestruc_t tmp;
17427c478bd9Sstevel@tonic-gate 		if (approx == -1)
17437c478bd9Sstevel@tonic-gate 			ts.tv_sec = (1987 - 1970) * 365 * SECONDS_PER_DAY;
17447c478bd9Sstevel@tonic-gate 		else
17457c478bd9Sstevel@tonic-gate 			ts.tv_sec = approx;
17467c478bd9Sstevel@tonic-gate 		ts.tv_nsec = 0;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 		/*
17497c478bd9Sstevel@tonic-gate 		 * Attempt to write the new time to the TOD chip.  Set spl high
17507c478bd9Sstevel@tonic-gate 		 * to avoid getting preempted between the tod_set and tod_get.
17517c478bd9Sstevel@tonic-gate 		 */
17527c478bd9Sstevel@tonic-gate 		spl = splhi();
17537c478bd9Sstevel@tonic-gate 		tod_set(ts);
17547c478bd9Sstevel@tonic-gate 		tmp = tod_get();
17557c478bd9Sstevel@tonic-gate 		splx(spl);
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 		if (tmp.tv_sec != ts.tv_sec && tmp.tv_sec != ts.tv_sec + 1) {
17587c478bd9Sstevel@tonic-gate 			tod_broken = 1;
17597c478bd9Sstevel@tonic-gate 			dosynctodr = 0;
17607c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Time-of-day chip unresponsive;"
17617c478bd9Sstevel@tonic-gate 			    " dead batteries?");
17627c478bd9Sstevel@tonic-gate 		} else {
17637c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Time-of-day chip had "
17647c478bd9Sstevel@tonic-gate 			    "incorrect date; check and reset.");
17657c478bd9Sstevel@tonic-gate 		}
17667c478bd9Sstevel@tonic-gate 		set_clock = 1;
17677c478bd9Sstevel@tonic-gate 	}
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	if (!boot_time) {
17707c478bd9Sstevel@tonic-gate 		boot_time = ts.tv_sec;
17717c478bd9Sstevel@tonic-gate 		set_clock = 1;
17727c478bd9Sstevel@tonic-gate 	}
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	if (set_clock)
17757c478bd9Sstevel@tonic-gate 		set_hrestime(&ts);
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	mutex_exit(&tod_lock);
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate 
1780a913396dSqiao int	hrestime_isvalid = 0;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate void
17837c478bd9Sstevel@tonic-gate set_hrestime(timestruc_t *ts)
17847c478bd9Sstevel@tonic-gate {
17857c478bd9Sstevel@tonic-gate 	int spl = hr_clock_lock();
17867c478bd9Sstevel@tonic-gate 	hrestime = *ts;
1787a913396dSqiao 	/*
1788a913396dSqiao 	 * hrestime must be visible before hrestime_isvalid
1789a913396dSqiao 	 * is set to 1
1790a913396dSqiao 	 */
1791a913396dSqiao 	membar_enter();
17927c478bd9Sstevel@tonic-gate 	timedelta = 0;
1793a913396dSqiao 	hrestime_isvalid = 1;
17947c478bd9Sstevel@tonic-gate 	hr_clock_unlock(spl);
17957c478bd9Sstevel@tonic-gate }
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate static uint_t deadman_seconds;
17987c478bd9Sstevel@tonic-gate static uint32_t deadman_panics;
17997c478bd9Sstevel@tonic-gate static int deadman_enabled = 0;
18007c478bd9Sstevel@tonic-gate static int deadman_panic_timers = 1;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate static void
18037c478bd9Sstevel@tonic-gate deadman(void)
18047c478bd9Sstevel@tonic-gate {
18057c478bd9Sstevel@tonic-gate 	if (panicstr) {
18067c478bd9Sstevel@tonic-gate 		/*
18077c478bd9Sstevel@tonic-gate 		 * During panic, other CPUs besides the panic
18087c478bd9Sstevel@tonic-gate 		 * master continue to handle cyclics and some other
18097c478bd9Sstevel@tonic-gate 		 * interrupts.  The code below is intended to be
18107c478bd9Sstevel@tonic-gate 		 * single threaded, so any CPU other than the master
18117c478bd9Sstevel@tonic-gate 		 * must keep out.
18127c478bd9Sstevel@tonic-gate 		 */
18137c478bd9Sstevel@tonic-gate 		if (CPU->cpu_id != panic_cpu.cpu_id)
18147c478bd9Sstevel@tonic-gate 			return;
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 		/*
18177c478bd9Sstevel@tonic-gate 		 * If we're panicking, the deadman cyclic continues to increase
18187c478bd9Sstevel@tonic-gate 		 * lbolt in case the dump device driver relies on this for
18197c478bd9Sstevel@tonic-gate 		 * timeouts.  Note that we rely on deadman() being invoked once
18207c478bd9Sstevel@tonic-gate 		 * per second, and credit lbolt and lbolt64 with hz ticks each.
18217c478bd9Sstevel@tonic-gate 		 */
18227c478bd9Sstevel@tonic-gate 		lbolt += hz;
18237c478bd9Sstevel@tonic-gate 		lbolt64 += hz;
18247c478bd9Sstevel@tonic-gate 
18253c431bb5Swentaoy #ifdef __sparc
18263c431bb5Swentaoy 		watchdog_pat();
18273c431bb5Swentaoy #endif
18283c431bb5Swentaoy 
18297c478bd9Sstevel@tonic-gate 		if (!deadman_panic_timers)
18307c478bd9Sstevel@tonic-gate 			return; /* allow all timers to be manually disabled */
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 		/*
18337c478bd9Sstevel@tonic-gate 		 * If we are generating a crash dump or syncing filesystems and
18347c478bd9Sstevel@tonic-gate 		 * the corresponding timer is set, decrement it and re-enter
18357c478bd9Sstevel@tonic-gate 		 * the panic code to abort it and advance to the next state.
18367c478bd9Sstevel@tonic-gate 		 * The panic states and triggers are explained in panic.c.
18377c478bd9Sstevel@tonic-gate 		 */
18387c478bd9Sstevel@tonic-gate 		if (panic_dump) {
18397c478bd9Sstevel@tonic-gate 			if (dump_timeleft && (--dump_timeleft == 0)) {
18407c478bd9Sstevel@tonic-gate 				panic("panic dump timeout");
18417c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
18427c478bd9Sstevel@tonic-gate 			}
18437c478bd9Sstevel@tonic-gate 		} else if (panic_sync) {
18447c478bd9Sstevel@tonic-gate 			if (sync_timeleft && (--sync_timeleft == 0)) {
18457c478bd9Sstevel@tonic-gate 				panic("panic sync timeout");
18467c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
18477c478bd9Sstevel@tonic-gate 			}
18487c478bd9Sstevel@tonic-gate 		}
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 		return;
18517c478bd9Sstevel@tonic-gate 	}
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (lbolt != CPU->cpu_deadman_lbolt) {
18547c478bd9Sstevel@tonic-gate 		CPU->cpu_deadman_lbolt = lbolt;
18557c478bd9Sstevel@tonic-gate 		CPU->cpu_deadman_countdown = deadman_seconds;
18567c478bd9Sstevel@tonic-gate 		return;
18577c478bd9Sstevel@tonic-gate 	}
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	if (CPU->cpu_deadman_countdown-- > 0)
18607c478bd9Sstevel@tonic-gate 		return;
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	/*
18637c478bd9Sstevel@tonic-gate 	 * Regardless of whether or not we actually bring the system down,
18647c478bd9Sstevel@tonic-gate 	 * bump the deadman_panics variable.
18657c478bd9Sstevel@tonic-gate 	 *
18667c478bd9Sstevel@tonic-gate 	 * N.B. deadman_panics is incremented once for each CPU that
18677c478bd9Sstevel@tonic-gate 	 * passes through here.  It's expected that all the CPUs will
18687c478bd9Sstevel@tonic-gate 	 * detect this condition within one second of each other, so
18697c478bd9Sstevel@tonic-gate 	 * when deadman_enabled is off, deadman_panics will
18707c478bd9Sstevel@tonic-gate 	 * typically be a multiple of the total number of CPUs in
18717c478bd9Sstevel@tonic-gate 	 * the system.
18727c478bd9Sstevel@tonic-gate 	 */
18737c478bd9Sstevel@tonic-gate 	atomic_add_32(&deadman_panics, 1);
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	if (!deadman_enabled) {
18767c478bd9Sstevel@tonic-gate 		CPU->cpu_deadman_countdown = deadman_seconds;
18777c478bd9Sstevel@tonic-gate 		return;
18787c478bd9Sstevel@tonic-gate 	}
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	/*
18817c478bd9Sstevel@tonic-gate 	 * If we're here, we want to bring the system down.
18827c478bd9Sstevel@tonic-gate 	 */
18837c478bd9Sstevel@tonic-gate 	panic("deadman: timed out after %d seconds of clock "
18847c478bd9Sstevel@tonic-gate 	    "inactivity", deadman_seconds);
18857c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
18867c478bd9Sstevel@tonic-gate }
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18897c478bd9Sstevel@tonic-gate static void
18907c478bd9Sstevel@tonic-gate deadman_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
18917c478bd9Sstevel@tonic-gate {
18927c478bd9Sstevel@tonic-gate 	cpu->cpu_deadman_lbolt = 0;
18937c478bd9Sstevel@tonic-gate 	cpu->cpu_deadman_countdown = deadman_seconds;
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	hdlr->cyh_func = (cyc_func_t)deadman;
18967c478bd9Sstevel@tonic-gate 	hdlr->cyh_level = CY_HIGH_LEVEL;
18977c478bd9Sstevel@tonic-gate 	hdlr->cyh_arg = NULL;
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	/*
19007c478bd9Sstevel@tonic-gate 	 * Stagger the CPUs so that they don't all run deadman() at
19017c478bd9Sstevel@tonic-gate 	 * the same time.  Simplest reason to do this is to make it
19027c478bd9Sstevel@tonic-gate 	 * more likely that only one CPU will panic in case of a
19037c478bd9Sstevel@tonic-gate 	 * timeout.  This is (strictly speaking) an aesthetic, not a
19047c478bd9Sstevel@tonic-gate 	 * technical consideration.
19057c478bd9Sstevel@tonic-gate 	 *
19067c478bd9Sstevel@tonic-gate 	 * The interval must be one second in accordance with the
19077c478bd9Sstevel@tonic-gate 	 * code in deadman() above to increase lbolt during panic.
19087c478bd9Sstevel@tonic-gate 	 */
19097c478bd9Sstevel@tonic-gate 	when->cyt_when = cpu->cpu_id * (NANOSEC / NCPU);
19107c478bd9Sstevel@tonic-gate 	when->cyt_interval = NANOSEC;
19117c478bd9Sstevel@tonic-gate }
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate void
19157c478bd9Sstevel@tonic-gate deadman_init(void)
19167c478bd9Sstevel@tonic-gate {
19177c478bd9Sstevel@tonic-gate 	cyc_omni_handler_t hdlr;
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	if (deadman_seconds == 0)
19207c478bd9Sstevel@tonic-gate 		deadman_seconds = snoop_interval / MICROSEC;
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	if (snooping)
19237c478bd9Sstevel@tonic-gate 		deadman_enabled = 1;
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	hdlr.cyo_online = deadman_online;
19267c478bd9Sstevel@tonic-gate 	hdlr.cyo_offline = NULL;
19277c478bd9Sstevel@tonic-gate 	hdlr.cyo_arg = NULL;
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
19307c478bd9Sstevel@tonic-gate 	deadman_cyclic = cyclic_add_omni(&hdlr);
19317c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
19327c478bd9Sstevel@tonic-gate }
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate /*
19357c478bd9Sstevel@tonic-gate  * tod_fault() is for updating tod validate mechanism state:
19367c478bd9Sstevel@tonic-gate  * (1) TOD_NOFAULT: for resetting the state to 'normal'.
19377c478bd9Sstevel@tonic-gate  *     currently used for debugging only
19387c478bd9Sstevel@tonic-gate  * (2) The following four cases detected by tod validate mechanism:
19397c478bd9Sstevel@tonic-gate  *       TOD_REVERSED: current tod value is less than previous value.
19407c478bd9Sstevel@tonic-gate  *       TOD_STALLED: current tod value hasn't advanced.
19417c478bd9Sstevel@tonic-gate  *       TOD_JUMPED: current tod value advanced too far from previous value.
19427c478bd9Sstevel@tonic-gate  *       TOD_RATECHANGED: the ratio between average tod delta and
19437c478bd9Sstevel@tonic-gate  *       average tick delta has changed.
19447c478bd9Sstevel@tonic-gate  */
19457c478bd9Sstevel@tonic-gate enum tod_fault_type
19467c478bd9Sstevel@tonic-gate tod_fault(enum tod_fault_type ftype, int off)
19477c478bd9Sstevel@tonic-gate {
19487c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	if (tod_faulted != ftype) {
19517c478bd9Sstevel@tonic-gate 		switch (ftype) {
19527c478bd9Sstevel@tonic-gate 		case TOD_NOFAULT:
19537c478bd9Sstevel@tonic-gate 			plat_tod_fault(TOD_NOFAULT);
19547c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "Restarted tracking "
19557c478bd9Sstevel@tonic-gate 					"Time of Day clock.");
19567c478bd9Sstevel@tonic-gate 			tod_faulted = ftype;
19577c478bd9Sstevel@tonic-gate 			break;
19587c478bd9Sstevel@tonic-gate 		case TOD_REVERSED:
19597c478bd9Sstevel@tonic-gate 		case TOD_JUMPED:
19607c478bd9Sstevel@tonic-gate 			if (tod_faulted == TOD_NOFAULT) {
19617c478bd9Sstevel@tonic-gate 				plat_tod_fault(ftype);
19627c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "Time of Day clock error: "
19637c478bd9Sstevel@tonic-gate 				    "reason [%s by 0x%x]. -- "
19647c478bd9Sstevel@tonic-gate 				    " Stopped tracking Time Of Day clock.",
19657c478bd9Sstevel@tonic-gate 				    tod_fault_table[ftype], off);
19667c478bd9Sstevel@tonic-gate 				tod_faulted = ftype;
19677c478bd9Sstevel@tonic-gate 			}
19687c478bd9Sstevel@tonic-gate 			break;
19697c478bd9Sstevel@tonic-gate 		case TOD_STALLED:
19707c478bd9Sstevel@tonic-gate 		case TOD_RATECHANGED:
19717c478bd9Sstevel@tonic-gate 			if (tod_faulted == TOD_NOFAULT) {
19727c478bd9Sstevel@tonic-gate 				plat_tod_fault(ftype);
19737c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "Time of Day clock error: "
19747c478bd9Sstevel@tonic-gate 				    "reason [%s]. -- "
19757c478bd9Sstevel@tonic-gate 				    " Stopped tracking Time Of Day clock.",
19767c478bd9Sstevel@tonic-gate 				    tod_fault_table[ftype]);
19777c478bd9Sstevel@tonic-gate 				tod_faulted = ftype;
19787c478bd9Sstevel@tonic-gate 			}
19797c478bd9Sstevel@tonic-gate 			break;
19807c478bd9Sstevel@tonic-gate 		default:
19817c478bd9Sstevel@tonic-gate 			break;
19827c478bd9Sstevel@tonic-gate 		}
19837c478bd9Sstevel@tonic-gate 	}
19847c478bd9Sstevel@tonic-gate 	return (tod_faulted);
19857c478bd9Sstevel@tonic-gate }
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate void
19887c478bd9Sstevel@tonic-gate tod_fault_reset()
19897c478bd9Sstevel@tonic-gate {
19907c478bd9Sstevel@tonic-gate 	tod_fault_reset_flag = 1;
19917c478bd9Sstevel@tonic-gate }
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate /*
19957c478bd9Sstevel@tonic-gate  * tod_validate() is used for checking values returned by tod_get().
19967c478bd9Sstevel@tonic-gate  * Four error cases can be detected by this routine:
19977c478bd9Sstevel@tonic-gate  *   TOD_REVERSED: current tod value is less than previous.
19987c478bd9Sstevel@tonic-gate  *   TOD_STALLED: current tod value hasn't advanced.
19997c478bd9Sstevel@tonic-gate  *   TOD_JUMPED: current tod value advanced too far from previous value.
20007c478bd9Sstevel@tonic-gate  *   TOD_RATECHANGED: the ratio between average tod delta and
20017c478bd9Sstevel@tonic-gate  *   average tick delta has changed.
20027c478bd9Sstevel@tonic-gate  */
20037c478bd9Sstevel@tonic-gate time_t
20047c478bd9Sstevel@tonic-gate tod_validate(time_t tod)
20057c478bd9Sstevel@tonic-gate {
20067c478bd9Sstevel@tonic-gate 	time_t diff_tod;
20077c478bd9Sstevel@tonic-gate 	hrtime_t diff_tick;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	long dtick;
20107c478bd9Sstevel@tonic-gate 	int dtick_delta;
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	int off = 0;
20137c478bd9Sstevel@tonic-gate 	enum tod_fault_type tod_bad = TOD_NOFAULT;
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 	static int firsttime = 1;
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	static time_t prev_tod = 0;
20187c478bd9Sstevel@tonic-gate 	static hrtime_t prev_tick = 0;
20197c478bd9Sstevel@tonic-gate 	static long dtick_avg = TOD_REF_FREQ;
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	hrtime_t tick = gethrtime();
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 	/*
20267c478bd9Sstevel@tonic-gate 	 * tod_validate_enable is patchable via /etc/system.
20272c891a87Ssethg 	 * If TOD is already faulted, or if TOD validation is deferred,
20282c891a87Ssethg 	 * there is nothing to do.
20297c478bd9Sstevel@tonic-gate 	 */
20302c891a87Ssethg 	if ((tod_validate_enable == 0) || (tod_faulted != TOD_NOFAULT) ||
20312c891a87Ssethg 	    tod_validate_deferred) {
20327c478bd9Sstevel@tonic-gate 		return (tod);
20337c478bd9Sstevel@tonic-gate 	}
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	/*
20367c478bd9Sstevel@tonic-gate 	 * Update prev_tod and prev_tick values for first run
20377c478bd9Sstevel@tonic-gate 	 */
20387c478bd9Sstevel@tonic-gate 	if (firsttime) {
20397c478bd9Sstevel@tonic-gate 		firsttime = 0;
20407c478bd9Sstevel@tonic-gate 		prev_tod = tod;
20417c478bd9Sstevel@tonic-gate 		prev_tick = tick;
20427c478bd9Sstevel@tonic-gate 		return (tod);
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	/*
20467c478bd9Sstevel@tonic-gate 	 * For either of these conditions, we need to reset ourself
20477c478bd9Sstevel@tonic-gate 	 * and start validation from zero since each condition
20487c478bd9Sstevel@tonic-gate 	 * indicates that the TOD will be updated with new value
20497c478bd9Sstevel@tonic-gate 	 * Also, note that tod_needsync will be reset in clock()
20507c478bd9Sstevel@tonic-gate 	 */
20517c478bd9Sstevel@tonic-gate 	if (tod_needsync || tod_fault_reset_flag) {
20527c478bd9Sstevel@tonic-gate 		firsttime = 1;
20537c478bd9Sstevel@tonic-gate 		prev_tod = 0;
20547c478bd9Sstevel@tonic-gate 		prev_tick = 0;
20557c478bd9Sstevel@tonic-gate 		dtick_avg = TOD_REF_FREQ;
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 		if (tod_fault_reset_flag)
20587c478bd9Sstevel@tonic-gate 			tod_fault_reset_flag = 0;
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 		return (tod);
20617c478bd9Sstevel@tonic-gate 	}
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	/* test hook */
20647c478bd9Sstevel@tonic-gate 	switch (tod_unit_test) {
20657c478bd9Sstevel@tonic-gate 	case 1: /* for testing jumping tod */
20667c478bd9Sstevel@tonic-gate 		tod += tod_test_injector;
20677c478bd9Sstevel@tonic-gate 		tod_unit_test = 0;
20687c478bd9Sstevel@tonic-gate 		break;
20697c478bd9Sstevel@tonic-gate 	case 2:	/* for testing stuck tod bit */
20707c478bd9Sstevel@tonic-gate 		tod |= 1 << tod_test_injector;
20717c478bd9Sstevel@tonic-gate 		tod_unit_test = 0;
20727c478bd9Sstevel@tonic-gate 		break;
20737c478bd9Sstevel@tonic-gate 	case 3:	/* for testing stalled tod */
20747c478bd9Sstevel@tonic-gate 		tod = prev_tod;
20757c478bd9Sstevel@tonic-gate 		tod_unit_test = 0;
20767c478bd9Sstevel@tonic-gate 		break;
20777c478bd9Sstevel@tonic-gate 	case 4:	/* reset tod fault status */
20787c478bd9Sstevel@tonic-gate 		(void) tod_fault(TOD_NOFAULT, 0);
20797c478bd9Sstevel@tonic-gate 		tod_unit_test = 0;
20807c478bd9Sstevel@tonic-gate 		break;
20817c478bd9Sstevel@tonic-gate 	default:
20827c478bd9Sstevel@tonic-gate 		break;
20837c478bd9Sstevel@tonic-gate 	}
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	diff_tod = tod - prev_tod;
20867c478bd9Sstevel@tonic-gate 	diff_tick = tick - prev_tick;
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	ASSERT(diff_tick >= 0);
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	if (diff_tod < 0) {
20917c478bd9Sstevel@tonic-gate 		/* ERROR - tod reversed */
20927c478bd9Sstevel@tonic-gate 		tod_bad = TOD_REVERSED;
20937c478bd9Sstevel@tonic-gate 		off = (int)(prev_tod - tod);
20947c478bd9Sstevel@tonic-gate 	} else if (diff_tod == 0) {
20957c478bd9Sstevel@tonic-gate 		/* tod did not advance */
20967c478bd9Sstevel@tonic-gate 		if (diff_tick > TOD_STALL_THRESHOLD) {
20977c478bd9Sstevel@tonic-gate 			/* ERROR - tod stalled */
20987c478bd9Sstevel@tonic-gate 			tod_bad = TOD_STALLED;
20997c478bd9Sstevel@tonic-gate 		} else {
21007c478bd9Sstevel@tonic-gate 			/*
21017c478bd9Sstevel@tonic-gate 			 * Make sure we don't update prev_tick
21027c478bd9Sstevel@tonic-gate 			 * so that diff_tick is calculated since
21037c478bd9Sstevel@tonic-gate 			 * the first diff_tod == 0
21047c478bd9Sstevel@tonic-gate 			 */
21057c478bd9Sstevel@tonic-gate 			return (tod);
21067c478bd9Sstevel@tonic-gate 		}
21077c478bd9Sstevel@tonic-gate 	} else {
21087c478bd9Sstevel@tonic-gate 		/* calculate dtick */
21097c478bd9Sstevel@tonic-gate 		dtick = diff_tick / diff_tod;
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 		/* update dtick averages */
21127c478bd9Sstevel@tonic-gate 		dtick_avg += ((dtick - dtick_avg) / TOD_FILTER_N);
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 		/*
21157c478bd9Sstevel@tonic-gate 		 * Calculate dtick_delta as
21167c478bd9Sstevel@tonic-gate 		 * variation from reference freq in quartiles
21177c478bd9Sstevel@tonic-gate 		 */
21187c478bd9Sstevel@tonic-gate 		dtick_delta = (dtick_avg - TOD_REF_FREQ) /
21197c478bd9Sstevel@tonic-gate 			(TOD_REF_FREQ >> 2);
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 		/*
21227c478bd9Sstevel@tonic-gate 		 * Even with a perfectly functioning TOD device,
21237c478bd9Sstevel@tonic-gate 		 * when the number of elapsed seconds is low the
21247c478bd9Sstevel@tonic-gate 		 * algorithm can calculate a rate that is beyond
21257c478bd9Sstevel@tonic-gate 		 * tolerance, causing an error.  The algorithm is
21267c478bd9Sstevel@tonic-gate 		 * inaccurate when elapsed time is low (less than
21277c478bd9Sstevel@tonic-gate 		 * 5 seconds).
21287c478bd9Sstevel@tonic-gate 		 */
21297c478bd9Sstevel@tonic-gate 		if (diff_tod > 4) {
21307c478bd9Sstevel@tonic-gate 			if (dtick < TOD_JUMP_THRESHOLD) {
21317c478bd9Sstevel@tonic-gate 				/* ERROR - tod jumped */
21327c478bd9Sstevel@tonic-gate 				tod_bad = TOD_JUMPED;
21337c478bd9Sstevel@tonic-gate 				off = (int)diff_tod;
21347c478bd9Sstevel@tonic-gate 			} else if (dtick_delta) {
21357c478bd9Sstevel@tonic-gate 				/* ERROR - change in clock rate */
21367c478bd9Sstevel@tonic-gate 				tod_bad = TOD_RATECHANGED;
21377c478bd9Sstevel@tonic-gate 			}
21387c478bd9Sstevel@tonic-gate 		}
21397c478bd9Sstevel@tonic-gate 	}
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	if (tod_bad != TOD_NOFAULT) {
21427c478bd9Sstevel@tonic-gate 		(void) tod_fault(tod_bad, off);
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 		/*
21457c478bd9Sstevel@tonic-gate 		 * Disable dosynctodr since we are going to fault
21467c478bd9Sstevel@tonic-gate 		 * the TOD chip anyway here
21477c478bd9Sstevel@tonic-gate 		 */
21487c478bd9Sstevel@tonic-gate 		dosynctodr = 0;
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 		/*
21517c478bd9Sstevel@tonic-gate 		 * Set tod to the correct value from hrestime
21527c478bd9Sstevel@tonic-gate 		 */
21537c478bd9Sstevel@tonic-gate 		tod = hrestime.tv_sec;
21547c478bd9Sstevel@tonic-gate 	}
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	prev_tod = tod;
21577c478bd9Sstevel@tonic-gate 	prev_tick = tick;
21587c478bd9Sstevel@tonic-gate 	return (tod);
21597c478bd9Sstevel@tonic-gate }
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate static void
21627c478bd9Sstevel@tonic-gate calcloadavg(int nrun, uint64_t *hp_ave)
21637c478bd9Sstevel@tonic-gate {
21647c478bd9Sstevel@tonic-gate 	static int64_t f[3] = { 135, 27, 9 };
21657c478bd9Sstevel@tonic-gate 	uint_t i;
21667c478bd9Sstevel@tonic-gate 	int64_t q, r;
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 	/*
21697c478bd9Sstevel@tonic-gate 	 * Compute load average over the last 1, 5, and 15 minutes
21707c478bd9Sstevel@tonic-gate 	 * (60, 300, and 900 seconds).  The constants in f[3] are for
21717c478bd9Sstevel@tonic-gate 	 * exponential decay:
21727c478bd9Sstevel@tonic-gate 	 * (1 - exp(-1/60)) << 13 = 135,
21737c478bd9Sstevel@tonic-gate 	 * (1 - exp(-1/300)) << 13 = 27,
21747c478bd9Sstevel@tonic-gate 	 * (1 - exp(-1/900)) << 13 = 9.
21757c478bd9Sstevel@tonic-gate 	 */
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	/*
21787c478bd9Sstevel@tonic-gate 	 * a little hoop-jumping to avoid integer overflow
21797c478bd9Sstevel@tonic-gate 	 */
21807c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
21817c478bd9Sstevel@tonic-gate 		q = (hp_ave[i]  >> 16) << 7;
21827c478bd9Sstevel@tonic-gate 		r = (hp_ave[i]  & 0xffff) << 7;
21837c478bd9Sstevel@tonic-gate 		hp_ave[i] += ((nrun - q) * f[i] - ((r * f[i]) >> 16)) >> 4;
21847c478bd9Sstevel@tonic-gate 	}
21857c478bd9Sstevel@tonic-gate }
2186