xref: /onnv-gate/usr/src/uts/common/os/clock.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/param.h>
34*0Sstevel@tonic-gate #include <sys/t_lock.h>
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <sys/tuneable.h>
37*0Sstevel@tonic-gate #include <sys/sysmacros.h>
38*0Sstevel@tonic-gate #include <sys/systm.h>
39*0Sstevel@tonic-gate #include <sys/cpuvar.h>
40*0Sstevel@tonic-gate #include <sys/lgrp.h>
41*0Sstevel@tonic-gate #include <sys/user.h>
42*0Sstevel@tonic-gate #include <sys/proc.h>
43*0Sstevel@tonic-gate #include <sys/callo.h>
44*0Sstevel@tonic-gate #include <sys/kmem.h>
45*0Sstevel@tonic-gate #include <sys/var.h>
46*0Sstevel@tonic-gate #include <sys/cmn_err.h>
47*0Sstevel@tonic-gate #include <sys/swap.h>
48*0Sstevel@tonic-gate #include <sys/vmsystm.h>
49*0Sstevel@tonic-gate #include <sys/class.h>
50*0Sstevel@tonic-gate #include <sys/time.h>
51*0Sstevel@tonic-gate #include <sys/debug.h>
52*0Sstevel@tonic-gate #include <sys/vtrace.h>
53*0Sstevel@tonic-gate #include <sys/spl.h>
54*0Sstevel@tonic-gate #include <sys/atomic.h>
55*0Sstevel@tonic-gate #include <sys/dumphdr.h>
56*0Sstevel@tonic-gate #include <sys/archsystm.h>
57*0Sstevel@tonic-gate #include <sys/fs/swapnode.h>
58*0Sstevel@tonic-gate #include <sys/panic.h>
59*0Sstevel@tonic-gate #include <sys/disp.h>
60*0Sstevel@tonic-gate #include <sys/msacct.h>
61*0Sstevel@tonic-gate #include <sys/mem_cage.h>
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate #include <vm/page.h>
64*0Sstevel@tonic-gate #include <vm/anon.h>
65*0Sstevel@tonic-gate #include <vm/rm.h>
66*0Sstevel@tonic-gate #include <sys/cyclic.h>
67*0Sstevel@tonic-gate #include <sys/cpupart.h>
68*0Sstevel@tonic-gate #include <sys/rctl.h>
69*0Sstevel@tonic-gate #include <sys/task.h>
70*0Sstevel@tonic-gate #include <sys/chip.h>
71*0Sstevel@tonic-gate #include <sys/sdt.h>
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * for NTP support
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate #include <sys/timex.h>
77*0Sstevel@tonic-gate #include <sys/inttypes.h>
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate  * clock is called straight from
81*0Sstevel@tonic-gate  * the real time clock interrupt.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  * Functions:
84*0Sstevel@tonic-gate  *	reprime clock
85*0Sstevel@tonic-gate  *	schedule callouts
86*0Sstevel@tonic-gate  *	maintain date
87*0Sstevel@tonic-gate  *	jab the scheduler
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate extern kcondvar_t	fsflush_cv;
91*0Sstevel@tonic-gate extern sysinfo_t	sysinfo;
92*0Sstevel@tonic-gate extern vminfo_t	vminfo;
93*0Sstevel@tonic-gate extern int	idleswtch;	/* flag set while idle in pswtch() */
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  * high-precision avenrun values.  These are needed to make the
97*0Sstevel@tonic-gate  * regular avenrun values accurate.
98*0Sstevel@tonic-gate  */
99*0Sstevel@tonic-gate static uint64_t hp_avenrun[3];
100*0Sstevel@tonic-gate int	avenrun[3];		/* FSCALED average run queue lengths */
101*0Sstevel@tonic-gate time_t	time;	/* time in seconds since 1970 - for compatibility only */
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate static struct loadavg_s loadavg;
104*0Sstevel@tonic-gate /*
105*0Sstevel@tonic-gate  * Phase/frequency-lock loop (PLL/FLL) definitions
106*0Sstevel@tonic-gate  *
107*0Sstevel@tonic-gate  * The following variables are read and set by the ntp_adjtime() system
108*0Sstevel@tonic-gate  * call.
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  * time_state shows the state of the system clock, with values defined
111*0Sstevel@tonic-gate  * in the timex.h header file.
112*0Sstevel@tonic-gate  *
113*0Sstevel@tonic-gate  * time_status shows the status of the system clock, with bits defined
114*0Sstevel@tonic-gate  * in the timex.h header file.
115*0Sstevel@tonic-gate  *
116*0Sstevel@tonic-gate  * time_offset is used by the PLL/FLL to adjust the system time in small
117*0Sstevel@tonic-gate  * increments.
118*0Sstevel@tonic-gate  *
119*0Sstevel@tonic-gate  * time_constant determines the bandwidth or "stiffness" of the PLL.
120*0Sstevel@tonic-gate  *
121*0Sstevel@tonic-gate  * time_tolerance determines maximum frequency error or tolerance of the
122*0Sstevel@tonic-gate  * CPU clock oscillator and is a property of the architecture; however,
123*0Sstevel@tonic-gate  * in principle it could change as result of the presence of external
124*0Sstevel@tonic-gate  * discipline signals, for instance.
125*0Sstevel@tonic-gate  *
126*0Sstevel@tonic-gate  * time_precision is usually equal to the kernel tick variable; however,
127*0Sstevel@tonic-gate  * in cases where a precision clock counter or external clock is
128*0Sstevel@tonic-gate  * available, the resolution can be much less than this and depend on
129*0Sstevel@tonic-gate  * whether the external clock is working or not.
130*0Sstevel@tonic-gate  *
131*0Sstevel@tonic-gate  * time_maxerror is initialized by a ntp_adjtime() call and increased by
132*0Sstevel@tonic-gate  * the kernel once each second to reflect the maximum error bound
133*0Sstevel@tonic-gate  * growth.
134*0Sstevel@tonic-gate  *
135*0Sstevel@tonic-gate  * time_esterror is set and read by the ntp_adjtime() call, but
136*0Sstevel@tonic-gate  * otherwise not used by the kernel.
137*0Sstevel@tonic-gate  */
138*0Sstevel@tonic-gate int32_t time_state = TIME_OK;	/* clock state */
139*0Sstevel@tonic-gate int32_t time_status = STA_UNSYNC;	/* clock status bits */
140*0Sstevel@tonic-gate int32_t time_offset = 0;		/* time offset (us) */
141*0Sstevel@tonic-gate int32_t time_constant = 0;		/* pll time constant */
142*0Sstevel@tonic-gate int32_t time_tolerance = MAXFREQ;	/* frequency tolerance (scaled ppm) */
143*0Sstevel@tonic-gate int32_t time_precision = 1;	/* clock precision (us) */
144*0Sstevel@tonic-gate int32_t time_maxerror = MAXPHASE;	/* maximum error (us) */
145*0Sstevel@tonic-gate int32_t time_esterror = MAXPHASE;	/* estimated error (us) */
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate  * The following variables establish the state of the PLL/FLL and the
149*0Sstevel@tonic-gate  * residual time and frequency offset of the local clock. The scale
150*0Sstevel@tonic-gate  * factors are defined in the timex.h header file.
151*0Sstevel@tonic-gate  *
152*0Sstevel@tonic-gate  * time_phase and time_freq are the phase increment and the frequency
153*0Sstevel@tonic-gate  * increment, respectively, of the kernel time variable.
154*0Sstevel@tonic-gate  *
155*0Sstevel@tonic-gate  * time_freq is set via ntp_adjtime() from a value stored in a file when
156*0Sstevel@tonic-gate  * the synchronization daemon is first started. Its value is retrieved
157*0Sstevel@tonic-gate  * via ntp_adjtime() and written to the file about once per hour by the
158*0Sstevel@tonic-gate  * daemon.
159*0Sstevel@tonic-gate  *
160*0Sstevel@tonic-gate  * time_adj is the adjustment added to the value of tick at each timer
161*0Sstevel@tonic-gate  * interrupt and is recomputed from time_phase and time_freq at each
162*0Sstevel@tonic-gate  * seconds rollover.
163*0Sstevel@tonic-gate  *
164*0Sstevel@tonic-gate  * time_reftime is the second's portion of the system time at the last
165*0Sstevel@tonic-gate  * call to ntp_adjtime(). It is used to adjust the time_freq variable
166*0Sstevel@tonic-gate  * and to increase the time_maxerror as the time since last update
167*0Sstevel@tonic-gate  * increases.
168*0Sstevel@tonic-gate  */
169*0Sstevel@tonic-gate int32_t time_phase = 0;		/* phase offset (scaled us) */
170*0Sstevel@tonic-gate int32_t time_freq = 0;		/* frequency offset (scaled ppm) */
171*0Sstevel@tonic-gate int32_t time_adj = 0;		/* tick adjust (scaled 1 / hz) */
172*0Sstevel@tonic-gate int32_t time_reftime = 0;		/* time at last adjustment (s) */
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate /*
175*0Sstevel@tonic-gate  * The scale factors of the following variables are defined in the
176*0Sstevel@tonic-gate  * timex.h header file.
177*0Sstevel@tonic-gate  *
178*0Sstevel@tonic-gate  * pps_time contains the time at each calibration interval, as read by
179*0Sstevel@tonic-gate  * microtime(). pps_count counts the seconds of the calibration
180*0Sstevel@tonic-gate  * interval, the duration of which is nominally pps_shift in powers of
181*0Sstevel@tonic-gate  * two.
182*0Sstevel@tonic-gate  *
183*0Sstevel@tonic-gate  * pps_offset is the time offset produced by the time median filter
184*0Sstevel@tonic-gate  * pps_tf[], while pps_jitter is the dispersion (jitter) measured by
185*0Sstevel@tonic-gate  * this filter.
186*0Sstevel@tonic-gate  *
187*0Sstevel@tonic-gate  * pps_freq is the frequency offset produced by the frequency median
188*0Sstevel@tonic-gate  * filter pps_ff[], while pps_stabil is the dispersion (wander) measured
189*0Sstevel@tonic-gate  * by this filter.
190*0Sstevel@tonic-gate  *
191*0Sstevel@tonic-gate  * pps_usec is latched from a high resolution counter or external clock
192*0Sstevel@tonic-gate  * at pps_time. Here we want the hardware counter contents only, not the
193*0Sstevel@tonic-gate  * contents plus the time_tv.usec as usual.
194*0Sstevel@tonic-gate  *
195*0Sstevel@tonic-gate  * pps_valid counts the number of seconds since the last PPS update. It
196*0Sstevel@tonic-gate  * is used as a watchdog timer to disable the PPS discipline should the
197*0Sstevel@tonic-gate  * PPS signal be lost.
198*0Sstevel@tonic-gate  *
199*0Sstevel@tonic-gate  * pps_glitch counts the number of seconds since the beginning of an
200*0Sstevel@tonic-gate  * offset burst more than tick/2 from current nominal offset. It is used
201*0Sstevel@tonic-gate  * mainly to suppress error bursts due to priority conflicts between the
202*0Sstevel@tonic-gate  * PPS interrupt and timer interrupt.
203*0Sstevel@tonic-gate  *
204*0Sstevel@tonic-gate  * pps_intcnt counts the calibration intervals for use in the interval-
205*0Sstevel@tonic-gate  * adaptation algorithm. It's just too complicated for words.
206*0Sstevel@tonic-gate  */
207*0Sstevel@tonic-gate struct timeval pps_time;	/* kernel time at last interval */
208*0Sstevel@tonic-gate int32_t pps_tf[] = {0, 0, 0};	/* pps time offset median filter (us) */
209*0Sstevel@tonic-gate int32_t pps_offset = 0;		/* pps time offset (us) */
210*0Sstevel@tonic-gate int32_t pps_jitter = MAXTIME;	/* time dispersion (jitter) (us) */
211*0Sstevel@tonic-gate int32_t pps_ff[] = {0, 0, 0};	/* pps frequency offset median filter */
212*0Sstevel@tonic-gate int32_t pps_freq = 0;		/* frequency offset (scaled ppm) */
213*0Sstevel@tonic-gate int32_t pps_stabil = MAXFREQ;	/* frequency dispersion (scaled ppm) */
214*0Sstevel@tonic-gate int32_t pps_usec = 0;		/* microsec counter at last interval */
215*0Sstevel@tonic-gate int32_t pps_valid = PPS_VALID;	/* pps signal watchdog counter */
216*0Sstevel@tonic-gate int32_t pps_glitch = 0;		/* pps signal glitch counter */
217*0Sstevel@tonic-gate int32_t pps_count = 0;		/* calibration interval counter (s) */
218*0Sstevel@tonic-gate int32_t pps_shift = PPS_SHIFT;	/* interval duration (s) (shift) */
219*0Sstevel@tonic-gate int32_t pps_intcnt = 0;		/* intervals at current duration */
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate /*
222*0Sstevel@tonic-gate  * PPS signal quality monitors
223*0Sstevel@tonic-gate  *
224*0Sstevel@tonic-gate  * pps_jitcnt counts the seconds that have been discarded because the
225*0Sstevel@tonic-gate  * jitter measured by the time median filter exceeds the limit MAXTIME
226*0Sstevel@tonic-gate  * (100 us).
227*0Sstevel@tonic-gate  *
228*0Sstevel@tonic-gate  * pps_calcnt counts the frequency calibration intervals, which are
229*0Sstevel@tonic-gate  * variable from 4 s to 256 s.
230*0Sstevel@tonic-gate  *
231*0Sstevel@tonic-gate  * pps_errcnt counts the calibration intervals which have been discarded
232*0Sstevel@tonic-gate  * because the wander exceeds the limit MAXFREQ (100 ppm) or where the
233*0Sstevel@tonic-gate  * calibration interval jitter exceeds two ticks.
234*0Sstevel@tonic-gate  *
235*0Sstevel@tonic-gate  * pps_stbcnt counts the calibration intervals that have been discarded
236*0Sstevel@tonic-gate  * because the frequency wander exceeds the limit MAXFREQ / 4 (25 us).
237*0Sstevel@tonic-gate  */
238*0Sstevel@tonic-gate int32_t pps_jitcnt = 0;		/* jitter limit exceeded */
239*0Sstevel@tonic-gate int32_t pps_calcnt = 0;		/* calibration intervals */
240*0Sstevel@tonic-gate int32_t pps_errcnt = 0;		/* calibration errors */
241*0Sstevel@tonic-gate int32_t pps_stbcnt = 0;		/* stability limit exceeded */
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate /* The following variables require no explicit locking */
244*0Sstevel@tonic-gate volatile clock_t lbolt;		/* time in Hz since last boot */
245*0Sstevel@tonic-gate volatile int64_t lbolt64;	/* lbolt64 won't wrap for 2.9 billion yrs */
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate kcondvar_t lbolt_cv;
248*0Sstevel@tonic-gate int one_sec = 1; /* turned on once every second */
249*0Sstevel@tonic-gate static int fsflushcnt;	/* counter for t_fsflushr */
250*0Sstevel@tonic-gate int	dosynctodr = 1;	/* patchable; enable/disable sync to TOD chip */
251*0Sstevel@tonic-gate int	tod_needsync = 0;	/* need to sync tod chip with software time */
252*0Sstevel@tonic-gate static int tod_broken = 0;	/* clock chip doesn't work */
253*0Sstevel@tonic-gate time_t	boot_time = 0;		/* Boot time in seconds since 1970 */
254*0Sstevel@tonic-gate cyclic_id_t clock_cyclic;	/* clock()'s cyclic_id */
255*0Sstevel@tonic-gate cyclic_id_t deadman_cyclic;	/* deadman()'s cyclic_id */
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate static int lgrp_ticks;		/* counter to schedule lgrp load calcs */
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate /*
260*0Sstevel@tonic-gate  * rechoose_interval_history is used to detect when rechoose_interval's
261*0Sstevel@tonic-gate  * value has changed (via hotpatching for example), so that the
262*0Sstevel@tonic-gate  * cached values in the cpu structures may be updated.
263*0Sstevel@tonic-gate  */
264*0Sstevel@tonic-gate static int rechoose_interval_history = RECHOOSE_INTERVAL;
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate /*
267*0Sstevel@tonic-gate  * for tod fault detection
268*0Sstevel@tonic-gate  */
269*0Sstevel@tonic-gate #define	TOD_REF_FREQ		((longlong_t)(NANOSEC))
270*0Sstevel@tonic-gate #define	TOD_STALL_THRESHOLD	(TOD_REF_FREQ * 3 / 2)
271*0Sstevel@tonic-gate #define	TOD_JUMP_THRESHOLD	(TOD_REF_FREQ / 2)
272*0Sstevel@tonic-gate #define	TOD_FILTER_N		4
273*0Sstevel@tonic-gate #define	TOD_FILTER_SETTLE	(4 * TOD_FILTER_N)
274*0Sstevel@tonic-gate static int tod_faulted = TOD_NOFAULT;
275*0Sstevel@tonic-gate static int tod_fault_reset_flag = 0;
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate /* patchable via /etc/system */
278*0Sstevel@tonic-gate int tod_validate_enable = 1;
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate /*
281*0Sstevel@tonic-gate  * tod_fault_table[] must be aligned with
282*0Sstevel@tonic-gate  * enum tod_fault_type in systm.h
283*0Sstevel@tonic-gate  */
284*0Sstevel@tonic-gate static char *tod_fault_table[] = {
285*0Sstevel@tonic-gate 	"Reversed",			/* TOD_REVERSED */
286*0Sstevel@tonic-gate 	"Stalled",			/* TOD_STALLED */
287*0Sstevel@tonic-gate 	"Jumped",			/* TOD_JUMPED */
288*0Sstevel@tonic-gate 	"Changed in Clock Rate"		/* TOD_RATECHANGED */
289*0Sstevel@tonic-gate 	/*
290*0Sstevel@tonic-gate 	 * no strings needed for TOD_NOFAULT
291*0Sstevel@tonic-gate 	 */
292*0Sstevel@tonic-gate };
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate /*
295*0Sstevel@tonic-gate  * test hook for tod broken detection in tod_validate
296*0Sstevel@tonic-gate  */
297*0Sstevel@tonic-gate int tod_unit_test = 0;
298*0Sstevel@tonic-gate time_t tod_test_injector;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate #define	CLOCK_ADJ_HIST_SIZE	4
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate static int	adj_hist_entry;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate int64_t clock_adj_hist[CLOCK_ADJ_HIST_SIZE];
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate static void clock_tick(kthread_t *);
307*0Sstevel@tonic-gate static void calcloadavg(int, uint64_t *);
308*0Sstevel@tonic-gate static int genloadavg(struct loadavg_s *);
309*0Sstevel@tonic-gate static void loadavg_update();
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate void (*cmm_clock_callout)() = NULL;
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate #ifdef	KSLICE
314*0Sstevel@tonic-gate int kslice = KSLICE;
315*0Sstevel@tonic-gate #endif
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate static void
318*0Sstevel@tonic-gate clock(void)
319*0Sstevel@tonic-gate {
320*0Sstevel@tonic-gate 	kthread_t	*t;
321*0Sstevel@tonic-gate 	kmutex_t	*plockp;	/* pointer to thread's process lock */
322*0Sstevel@tonic-gate 	int	pinned_intr = 0;
323*0Sstevel@tonic-gate 	uint_t	nrunnable, nrunning;
324*0Sstevel@tonic-gate 	uint_t	w_io;
325*0Sstevel@tonic-gate 	cpu_t	*cp;
326*0Sstevel@tonic-gate 	cpupart_t *cpupart;
327*0Sstevel@tonic-gate 	int	exiting;
328*0Sstevel@tonic-gate 	extern void set_anoninfo();
329*0Sstevel@tonic-gate 	extern	void	set_freemem();
330*0Sstevel@tonic-gate 	void	(*funcp)();
331*0Sstevel@tonic-gate 	int32_t ltemp;
332*0Sstevel@tonic-gate 	int64_t lltemp;
333*0Sstevel@tonic-gate 	int s;
334*0Sstevel@tonic-gate 	int do_lgrp_load;
335*0Sstevel@tonic-gate 	int rechoose_update = 0;
336*0Sstevel@tonic-gate 	int rechoose;
337*0Sstevel@tonic-gate 	int i;
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 	if (panicstr)
340*0Sstevel@tonic-gate 		return;
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	set_anoninfo();
343*0Sstevel@tonic-gate 	/*
344*0Sstevel@tonic-gate 	 * Make sure that 'freemem' do not drift too far from the truth
345*0Sstevel@tonic-gate 	 */
346*0Sstevel@tonic-gate 	set_freemem();
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	/*
350*0Sstevel@tonic-gate 	 * Before the section which is repeated is executed, we do
351*0Sstevel@tonic-gate 	 * the time delta processing which occurs every clock tick
352*0Sstevel@tonic-gate 	 *
353*0Sstevel@tonic-gate 	 * There is additional processing which happens every time
354*0Sstevel@tonic-gate 	 * the nanosecond counter rolls over which is described
355*0Sstevel@tonic-gate 	 * below - see the section which begins with : if (one_sec)
356*0Sstevel@tonic-gate 	 *
357*0Sstevel@tonic-gate 	 * This section marks the beginning of the precision-kernel
358*0Sstevel@tonic-gate 	 * code fragment.
359*0Sstevel@tonic-gate 	 *
360*0Sstevel@tonic-gate 	 * First, compute the phase adjustment. If the low-order bits
361*0Sstevel@tonic-gate 	 * (time_phase) of the update overflow, bump the higher order
362*0Sstevel@tonic-gate 	 * bits (time_update).
363*0Sstevel@tonic-gate 	 */
364*0Sstevel@tonic-gate 	time_phase += time_adj;
365*0Sstevel@tonic-gate 	if (time_phase <= -FINEUSEC) {
366*0Sstevel@tonic-gate 		ltemp = -time_phase / SCALE_PHASE;
367*0Sstevel@tonic-gate 		time_phase += ltemp * SCALE_PHASE;
368*0Sstevel@tonic-gate 		s = hr_clock_lock();
369*0Sstevel@tonic-gate 		timedelta -= ltemp * (NANOSEC/MICROSEC);
370*0Sstevel@tonic-gate 		hr_clock_unlock(s);
371*0Sstevel@tonic-gate 	} else if (time_phase >= FINEUSEC) {
372*0Sstevel@tonic-gate 		ltemp = time_phase / SCALE_PHASE;
373*0Sstevel@tonic-gate 		time_phase -= ltemp * SCALE_PHASE;
374*0Sstevel@tonic-gate 		s = hr_clock_lock();
375*0Sstevel@tonic-gate 		timedelta += ltemp * (NANOSEC/MICROSEC);
376*0Sstevel@tonic-gate 		hr_clock_unlock(s);
377*0Sstevel@tonic-gate 	}
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	/*
380*0Sstevel@tonic-gate 	 * End of precision-kernel code fragment which is processed
381*0Sstevel@tonic-gate 	 * every timer interrupt.
382*0Sstevel@tonic-gate 	 *
383*0Sstevel@tonic-gate 	 * Continue with the interrupt processing as scheduled.
384*0Sstevel@tonic-gate 	 *
385*0Sstevel@tonic-gate 	 * Did we pin another interrupt thread?  Need to check this before
386*0Sstevel@tonic-gate 	 * grabbing any adaptive locks, since if we block on a lock the
387*0Sstevel@tonic-gate 	 * pinned thread could escape.  Note that this is just a heuristic;
388*0Sstevel@tonic-gate 	 * if we take multiple laps though clock() without returning from
389*0Sstevel@tonic-gate 	 * the interrupt because we have another clock tick pending, then
390*0Sstevel@tonic-gate 	 * the pinned interrupt could be released by one of the previous
391*0Sstevel@tonic-gate 	 * laps.  The only consequence is that the CPU will be counted as
392*0Sstevel@tonic-gate 	 * in idle (or wait) state once the pinned interrupt is released.
393*0Sstevel@tonic-gate 	 * Since this accounting is inaccurate by nature, this isn't a big
394*0Sstevel@tonic-gate 	 * deal --- but we should try to get it right in the common case
395*0Sstevel@tonic-gate 	 * where we only call clock() once per interrupt.
396*0Sstevel@tonic-gate 	 */
397*0Sstevel@tonic-gate 	if (curthread->t_intr != NULL)
398*0Sstevel@tonic-gate 		pinned_intr = (curthread->t_intr->t_flag & T_INTR_THREAD);
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	/*
401*0Sstevel@tonic-gate 	 * Count the number of runnable threads and the number waiting
402*0Sstevel@tonic-gate 	 * for some form of I/O to complete -- gets added to
403*0Sstevel@tonic-gate 	 * sysinfo.waiting.  To know the state of the system, must add
404*0Sstevel@tonic-gate 	 * wait counts from all CPUs.  Also add up the per-partition
405*0Sstevel@tonic-gate 	 * statistics.
406*0Sstevel@tonic-gate 	 */
407*0Sstevel@tonic-gate 	w_io = 0;
408*0Sstevel@tonic-gate 	nrunnable = 0;
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	/*
411*0Sstevel@tonic-gate 	 * keep track of when to update lgrp/part loads
412*0Sstevel@tonic-gate 	 */
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	do_lgrp_load = 0;
415*0Sstevel@tonic-gate 	if (lgrp_ticks++ >= hz / 10) {
416*0Sstevel@tonic-gate 		lgrp_ticks = 0;
417*0Sstevel@tonic-gate 		do_lgrp_load = 1;
418*0Sstevel@tonic-gate 	}
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	/*
421*0Sstevel@tonic-gate 	 * The dispatcher tunable rechoose_interval may be hot-patched.
422*0Sstevel@tonic-gate 	 * Note if it has a new value. If so, the effective rechoose_interval
423*0Sstevel@tonic-gate 	 * cached in the cpu structures needs to be updated.
424*0Sstevel@tonic-gate 	 * If needed we'll do this during the walk of the cpu_list below.
425*0Sstevel@tonic-gate 	 */
426*0Sstevel@tonic-gate 	if (rechoose_interval != rechoose_interval_history) {
427*0Sstevel@tonic-gate 		rechoose_interval_history = rechoose_interval;
428*0Sstevel@tonic-gate 		rechoose_update = 1;
429*0Sstevel@tonic-gate 	}
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	if (one_sec)
432*0Sstevel@tonic-gate 		loadavg_update();
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	/*
436*0Sstevel@tonic-gate 	 * First count the threads waiting on kpreempt queues in each
437*0Sstevel@tonic-gate 	 * CPU partition.
438*0Sstevel@tonic-gate 	 */
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	cpupart = cp_list_head;
441*0Sstevel@tonic-gate 	do {
442*0Sstevel@tonic-gate 		uint_t cpupart_nrunnable = cpupart->cp_kp_queue.disp_nrunnable;
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 		cpupart->cp_updates++;
445*0Sstevel@tonic-gate 		nrunnable += cpupart_nrunnable;
446*0Sstevel@tonic-gate 		cpupart->cp_nrunnable_cum += cpupart_nrunnable;
447*0Sstevel@tonic-gate 		if (one_sec) {
448*0Sstevel@tonic-gate 			cpupart->cp_nrunning = 0;
449*0Sstevel@tonic-gate 			cpupart->cp_nrunnable = cpupart_nrunnable;
450*0Sstevel@tonic-gate 		}
451*0Sstevel@tonic-gate 	} while ((cpupart = cpupart->cp_next) != cp_list_head);
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	/* Now count the per-CPU statistics. */
455*0Sstevel@tonic-gate 	cp = cpu_list;
456*0Sstevel@tonic-gate 	do {
457*0Sstevel@tonic-gate 		uint_t cpu_nrunnable = cp->cpu_disp->disp_nrunnable;
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 		nrunnable += cpu_nrunnable;
460*0Sstevel@tonic-gate 		cpupart = cp->cpu_part;
461*0Sstevel@tonic-gate 		cpupart->cp_nrunnable_cum += cpu_nrunnable;
462*0Sstevel@tonic-gate 		if (one_sec)
463*0Sstevel@tonic-gate 			cpupart->cp_nrunnable += cpu_nrunnable;
464*0Sstevel@tonic-gate 		if (do_lgrp_load &&
465*0Sstevel@tonic-gate 		    (cp->cpu_flags & CPU_EXISTS)) {
466*0Sstevel@tonic-gate 			/*
467*0Sstevel@tonic-gate 			 * When updating the lgroup's load average,
468*0Sstevel@tonic-gate 			 * account for the thread running on the CPU.
469*0Sstevel@tonic-gate 			 * If the CPU is the current one, then we need
470*0Sstevel@tonic-gate 			 * to account for the underlying thread which
471*0Sstevel@tonic-gate 			 * got the clock interrupt not the thread that is
472*0Sstevel@tonic-gate 			 * handling the interrupt and caculating the load
473*0Sstevel@tonic-gate 			 * average
474*0Sstevel@tonic-gate 			 */
475*0Sstevel@tonic-gate 			t = cp->cpu_thread;
476*0Sstevel@tonic-gate 			if (CPU == cp)
477*0Sstevel@tonic-gate 				t = t->t_intr;
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 			/*
480*0Sstevel@tonic-gate 			 * Account for the load average for this thread if
481*0Sstevel@tonic-gate 			 * it isn't the idle thread or it is on the interrupt
482*0Sstevel@tonic-gate 			 * stack and not the current CPU handling the clock
483*0Sstevel@tonic-gate 			 * interrupt
484*0Sstevel@tonic-gate 			 */
485*0Sstevel@tonic-gate 			if ((t && t != cp->cpu_idle_thread) || (CPU != cp &&
486*0Sstevel@tonic-gate 			    CPU_ON_INTR(cp))) {
487*0Sstevel@tonic-gate 				if (t->t_lpl == cp->cpu_lpl) {
488*0Sstevel@tonic-gate 					/* local thread */
489*0Sstevel@tonic-gate 					cpu_nrunnable++;
490*0Sstevel@tonic-gate 				} else {
491*0Sstevel@tonic-gate 					/*
492*0Sstevel@tonic-gate 					 * This is a remote thread, charge it
493*0Sstevel@tonic-gate 					 * against its home lgroup.  Note that
494*0Sstevel@tonic-gate 					 * we notice that a thread is remote
495*0Sstevel@tonic-gate 					 * only if it's currently executing.
496*0Sstevel@tonic-gate 					 * This is a reasonable approximation,
497*0Sstevel@tonic-gate 					 * since queued remote threads are rare.
498*0Sstevel@tonic-gate 					 * Note also that if we didn't charge
499*0Sstevel@tonic-gate 					 * it to its home lgroup, remote
500*0Sstevel@tonic-gate 					 * execution would often make a system
501*0Sstevel@tonic-gate 					 * appear balanced even though it was
502*0Sstevel@tonic-gate 					 * not, and thread placement/migration
503*0Sstevel@tonic-gate 					 * would often not be done correctly.
504*0Sstevel@tonic-gate 					 */
505*0Sstevel@tonic-gate 					lgrp_loadavg(t->t_lpl,
506*0Sstevel@tonic-gate 					    LGRP_LOADAVG_IN_THREAD_MAX, 0);
507*0Sstevel@tonic-gate 				}
508*0Sstevel@tonic-gate 			}
509*0Sstevel@tonic-gate 			lgrp_loadavg(cp->cpu_lpl,
510*0Sstevel@tonic-gate 			    cpu_nrunnable * LGRP_LOADAVG_IN_THREAD_MAX, 1);
511*0Sstevel@tonic-gate 		}
512*0Sstevel@tonic-gate 		/*
513*0Sstevel@tonic-gate 		 * The platform may define a per physical processor
514*0Sstevel@tonic-gate 		 * adjustment of rechoose_interval. The effective
515*0Sstevel@tonic-gate 		 * (base + adjustment) rechoose_interval is cached
516*0Sstevel@tonic-gate 		 * in the cpu structures for efficiency. Above we detect
517*0Sstevel@tonic-gate 		 * if the cached values need updating, and here is where
518*0Sstevel@tonic-gate 		 * the update happens.
519*0Sstevel@tonic-gate 		 */
520*0Sstevel@tonic-gate 		if (rechoose_update) {
521*0Sstevel@tonic-gate 			rechoose = rechoose_interval +
522*0Sstevel@tonic-gate 				cp->cpu_chip->chip_rechoose_adj;
523*0Sstevel@tonic-gate 			cp->cpu_rechoose = (rechoose < 0) ? 0 : rechoose;
524*0Sstevel@tonic-gate 		}
525*0Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate 	/*
528*0Sstevel@tonic-gate 	 * Do tick processing for all the active threads running in
529*0Sstevel@tonic-gate 	 * the system.
530*0Sstevel@tonic-gate 	 */
531*0Sstevel@tonic-gate 	cp = cpu_list;
532*0Sstevel@tonic-gate 	nrunning = 0;
533*0Sstevel@tonic-gate 	do {
534*0Sstevel@tonic-gate 		klwp_id_t lwp;
535*0Sstevel@tonic-gate 		int intr;
536*0Sstevel@tonic-gate 		int thread_away;
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 		/*
539*0Sstevel@tonic-gate 		 * Don't do any tick processing on CPUs that
540*0Sstevel@tonic-gate 		 * aren't even in the system or aren't up yet.
541*0Sstevel@tonic-gate 		 */
542*0Sstevel@tonic-gate 		if ((cp->cpu_flags & CPU_EXISTS) == 0) {
543*0Sstevel@tonic-gate 			continue;
544*0Sstevel@tonic-gate 		}
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 		/*
547*0Sstevel@tonic-gate 		 * The locking here is rather tricky.  We use
548*0Sstevel@tonic-gate 		 * thread_free_lock to keep the currently running
549*0Sstevel@tonic-gate 		 * thread from being freed or recycled while we're
550*0Sstevel@tonic-gate 		 * looking at it.  We can then check if the thread
551*0Sstevel@tonic-gate 		 * is exiting and get the appropriate p_lock if it
552*0Sstevel@tonic-gate 		 * is not.  We have to be careful, though, because
553*0Sstevel@tonic-gate 		 * the _process_ can still be freed while we're
554*0Sstevel@tonic-gate 		 * holding thread_free_lock.  To avoid touching the
555*0Sstevel@tonic-gate 		 * proc structure we put a pointer to the p_lock in the
556*0Sstevel@tonic-gate 		 * thread structure.  The p_lock is persistent so we
557*0Sstevel@tonic-gate 		 * can acquire it even if the process is gone.  At that
558*0Sstevel@tonic-gate 		 * point we can check (again) if the thread is exiting
559*0Sstevel@tonic-gate 		 * and either drop the lock or do the tick processing.
560*0Sstevel@tonic-gate 		 */
561*0Sstevel@tonic-gate 		mutex_enter(&thread_free_lock);
562*0Sstevel@tonic-gate 		/*
563*0Sstevel@tonic-gate 		 * We cannot hold the cpu_lock to prevent the
564*0Sstevel@tonic-gate 		 * cpu_list from changing in the clock interrupt.
565*0Sstevel@tonic-gate 		 * As long as we don't block (or don't get pre-empted)
566*0Sstevel@tonic-gate 		 * the cpu_list will not change (all threads are paused
567*0Sstevel@tonic-gate 		 * before list modification). If the list does change
568*0Sstevel@tonic-gate 		 * any deleted cpu structures will remain with cpu_next
569*0Sstevel@tonic-gate 		 * set to NULL, hence the following test.
570*0Sstevel@tonic-gate 		 */
571*0Sstevel@tonic-gate 		if (cp->cpu_next == NULL) {
572*0Sstevel@tonic-gate 			mutex_exit(&thread_free_lock);
573*0Sstevel@tonic-gate 			break;
574*0Sstevel@tonic-gate 		}
575*0Sstevel@tonic-gate 		t = cp->cpu_thread;	/* Current running thread */
576*0Sstevel@tonic-gate 		if (CPU == cp) {
577*0Sstevel@tonic-gate 			/*
578*0Sstevel@tonic-gate 			 * 't' will be the clock interrupt thread on this
579*0Sstevel@tonic-gate 			 * CPU.  Use the pinned thread (if any) on this CPU
580*0Sstevel@tonic-gate 			 * as the target of the clock tick.  If we pinned
581*0Sstevel@tonic-gate 			 * an interrupt, though, just keep using the clock
582*0Sstevel@tonic-gate 			 * interrupt thread since the formerly pinned one
583*0Sstevel@tonic-gate 			 * may have gone away.  One interrupt thread is as
584*0Sstevel@tonic-gate 			 * good as another, and this means we don't have
585*0Sstevel@tonic-gate 			 * to continue to check pinned_intr in subsequent
586*0Sstevel@tonic-gate 			 * code.
587*0Sstevel@tonic-gate 			 */
588*0Sstevel@tonic-gate 			ASSERT(t == curthread);
589*0Sstevel@tonic-gate 			if (t->t_intr != NULL && !pinned_intr)
590*0Sstevel@tonic-gate 				t = t->t_intr;
591*0Sstevel@tonic-gate 		}
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 		intr = t->t_flag & T_INTR_THREAD;
594*0Sstevel@tonic-gate 		lwp = ttolwp(t);
595*0Sstevel@tonic-gate 		if (lwp == NULL || (t->t_proc_flag & TP_LWPEXIT) || intr) {
596*0Sstevel@tonic-gate 			/*
597*0Sstevel@tonic-gate 			 * Thread is exiting (or uninteresting) so don't
598*0Sstevel@tonic-gate 			 * do tick processing or grab p_lock.  Once we
599*0Sstevel@tonic-gate 			 * drop thread_free_lock we can't look inside the
600*0Sstevel@tonic-gate 			 * thread or lwp structure, since the thread may
601*0Sstevel@tonic-gate 			 * have gone away.
602*0Sstevel@tonic-gate 			 */
603*0Sstevel@tonic-gate 			exiting = 1;
604*0Sstevel@tonic-gate 		} else {
605*0Sstevel@tonic-gate 			/*
606*0Sstevel@tonic-gate 			 * OK, try to grab the process lock.  See
607*0Sstevel@tonic-gate 			 * comments above for why we're not using
608*0Sstevel@tonic-gate 			 * ttoproc(t)->p_lockp here.
609*0Sstevel@tonic-gate 			 */
610*0Sstevel@tonic-gate 			plockp = t->t_plockp;
611*0Sstevel@tonic-gate 			mutex_enter(plockp);
612*0Sstevel@tonic-gate 			/* See above comment. */
613*0Sstevel@tonic-gate 			if (cp->cpu_next == NULL) {
614*0Sstevel@tonic-gate 				mutex_exit(plockp);
615*0Sstevel@tonic-gate 				mutex_exit(&thread_free_lock);
616*0Sstevel@tonic-gate 				break;
617*0Sstevel@tonic-gate 			}
618*0Sstevel@tonic-gate 			/*
619*0Sstevel@tonic-gate 			 * The thread may have exited between when we
620*0Sstevel@tonic-gate 			 * checked above, and when we got the p_lock.
621*0Sstevel@tonic-gate 			 */
622*0Sstevel@tonic-gate 			if (t->t_proc_flag & TP_LWPEXIT) {
623*0Sstevel@tonic-gate 				mutex_exit(plockp);
624*0Sstevel@tonic-gate 				exiting = 1;
625*0Sstevel@tonic-gate 			} else {
626*0Sstevel@tonic-gate 				exiting = 0;
627*0Sstevel@tonic-gate 			}
628*0Sstevel@tonic-gate 		}
629*0Sstevel@tonic-gate 		/*
630*0Sstevel@tonic-gate 		 * Either we have the p_lock for the thread's process,
631*0Sstevel@tonic-gate 		 * or we don't care about the thread structure any more.
632*0Sstevel@tonic-gate 		 * Either way we can drop thread_free_lock.
633*0Sstevel@tonic-gate 		 */
634*0Sstevel@tonic-gate 		mutex_exit(&thread_free_lock);
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 		/*
637*0Sstevel@tonic-gate 		 * Update user, system, and idle cpu times.
638*0Sstevel@tonic-gate 		 */
639*0Sstevel@tonic-gate 		if (one_sec) {
640*0Sstevel@tonic-gate 			nrunning++;
641*0Sstevel@tonic-gate 			cp->cpu_part->cp_nrunning++;
642*0Sstevel@tonic-gate 		}
643*0Sstevel@tonic-gate 		/*
644*0Sstevel@tonic-gate 		 * If we haven't done tick processing for this
645*0Sstevel@tonic-gate 		 * lwp, then do it now. Since we don't hold the
646*0Sstevel@tonic-gate 		 * lwp down on a CPU it can migrate and show up
647*0Sstevel@tonic-gate 		 * more than once, hence the lbolt check.
648*0Sstevel@tonic-gate 		 *
649*0Sstevel@tonic-gate 		 * Also, make sure that it's okay to perform the
650*0Sstevel@tonic-gate 		 * tick processing before calling clock_tick.
651*0Sstevel@tonic-gate 		 * Setting thread_away to a TRUE value (ie. not 0)
652*0Sstevel@tonic-gate 		 * results in tick processing not being performed for
653*0Sstevel@tonic-gate 		 * that thread.  Or, in other words, keeps the thread
654*0Sstevel@tonic-gate 		 * away from clock_tick processing.
655*0Sstevel@tonic-gate 		 */
656*0Sstevel@tonic-gate 		thread_away = ((cp->cpu_flags & CPU_QUIESCED) ||
657*0Sstevel@tonic-gate 		    CPU_ON_INTR(cp) || intr ||
658*0Sstevel@tonic-gate 		    (cp->cpu_dispthread == cp->cpu_idle_thread) || exiting);
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 		if ((!thread_away) && (lbolt - t->t_lbolt != 0)) {
661*0Sstevel@tonic-gate 			t->t_lbolt = lbolt;
662*0Sstevel@tonic-gate 			clock_tick(t);
663*0Sstevel@tonic-gate 		}
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate #ifdef KSLICE
666*0Sstevel@tonic-gate 		/*
667*0Sstevel@tonic-gate 		 * Ah what the heck, give this kid a taste of the real
668*0Sstevel@tonic-gate 		 * world and yank the rug out from under it.
669*0Sstevel@tonic-gate 		 * But, only if we are running UniProcessor.
670*0Sstevel@tonic-gate 		 */
671*0Sstevel@tonic-gate 		if ((kslice) && (ncpus == 1)) {
672*0Sstevel@tonic-gate 			aston(t);
673*0Sstevel@tonic-gate 			cp->cpu_runrun = 1;
674*0Sstevel@tonic-gate 			cp->cpu_kprunrun = 1;
675*0Sstevel@tonic-gate 		}
676*0Sstevel@tonic-gate #endif
677*0Sstevel@tonic-gate 		if (!exiting)
678*0Sstevel@tonic-gate 			mutex_exit(plockp);
679*0Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	/*
682*0Sstevel@tonic-gate 	 * bump time in ticks
683*0Sstevel@tonic-gate 	 *
684*0Sstevel@tonic-gate 	 * We rely on there being only one clock thread and hence
685*0Sstevel@tonic-gate 	 * don't need a lock to protect lbolt.
686*0Sstevel@tonic-gate 	 */
687*0Sstevel@tonic-gate 	lbolt++;
688*0Sstevel@tonic-gate 	atomic_add_64((uint64_t *)&lbolt64, (int64_t)1);
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 	/*
691*0Sstevel@tonic-gate 	 * Check for a callout that needs be called from the clock
692*0Sstevel@tonic-gate 	 * thread to support the membership protocol in a clustered
693*0Sstevel@tonic-gate 	 * system.  Copy the function pointer so that we can reset
694*0Sstevel@tonic-gate 	 * this to NULL if needed.
695*0Sstevel@tonic-gate 	 */
696*0Sstevel@tonic-gate 	if ((funcp = cmm_clock_callout) != NULL)
697*0Sstevel@tonic-gate 		(*funcp)();
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate 	/*
700*0Sstevel@tonic-gate 	 * Wakeup the cageout thread waiters once per second.
701*0Sstevel@tonic-gate 	 */
702*0Sstevel@tonic-gate 	if (one_sec)
703*0Sstevel@tonic-gate 		kcage_tick();
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 	/*
706*0Sstevel@tonic-gate 	 * Schedule timeout() requests if any are due at this time.
707*0Sstevel@tonic-gate 	 */
708*0Sstevel@tonic-gate 	callout_schedule();
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate 	if (one_sec) {
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 		int drift, absdrift;
713*0Sstevel@tonic-gate 		timestruc_t tod;
714*0Sstevel@tonic-gate 		int s;
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate 		/*
717*0Sstevel@tonic-gate 		 * Beginning of precision-kernel code fragment executed
718*0Sstevel@tonic-gate 		 * every second.
719*0Sstevel@tonic-gate 		 *
720*0Sstevel@tonic-gate 		 * On rollover of the second the phase adjustment to be
721*0Sstevel@tonic-gate 		 * used for the next second is calculated.  Also, the
722*0Sstevel@tonic-gate 		 * maximum error is increased by the tolerance.  If the
723*0Sstevel@tonic-gate 		 * PPS frequency discipline code is present, the phase is
724*0Sstevel@tonic-gate 		 * increased to compensate for the CPU clock oscillator
725*0Sstevel@tonic-gate 		 * frequency error.
726*0Sstevel@tonic-gate 		 *
727*0Sstevel@tonic-gate 		 * On a 32-bit machine and given parameters in the timex.h
728*0Sstevel@tonic-gate 		 * header file, the maximum phase adjustment is +-512 ms
729*0Sstevel@tonic-gate 		 * and maximum frequency offset is (a tad less than)
730*0Sstevel@tonic-gate 		 * +-512 ppm. On a 64-bit machine, you shouldn't need to ask.
731*0Sstevel@tonic-gate 		 */
732*0Sstevel@tonic-gate 		time_maxerror += time_tolerance / SCALE_USEC;
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 		/*
735*0Sstevel@tonic-gate 		 * Leap second processing. If in leap-insert state at
736*0Sstevel@tonic-gate 		 * the end of the day, the system clock is set back one
737*0Sstevel@tonic-gate 		 * second; if in leap-delete state, the system clock is
738*0Sstevel@tonic-gate 		 * set ahead one second. The microtime() routine or
739*0Sstevel@tonic-gate 		 * external clock driver will insure that reported time
740*0Sstevel@tonic-gate 		 * is always monotonic. The ugly divides should be
741*0Sstevel@tonic-gate 		 * replaced.
742*0Sstevel@tonic-gate 		 */
743*0Sstevel@tonic-gate 		switch (time_state) {
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 		case TIME_OK:
746*0Sstevel@tonic-gate 			if (time_status & STA_INS)
747*0Sstevel@tonic-gate 				time_state = TIME_INS;
748*0Sstevel@tonic-gate 			else if (time_status & STA_DEL)
749*0Sstevel@tonic-gate 				time_state = TIME_DEL;
750*0Sstevel@tonic-gate 			break;
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 		case TIME_INS:
753*0Sstevel@tonic-gate 			if (hrestime.tv_sec % 86400 == 0) {
754*0Sstevel@tonic-gate 				s = hr_clock_lock();
755*0Sstevel@tonic-gate 				hrestime.tv_sec--;
756*0Sstevel@tonic-gate 				hr_clock_unlock(s);
757*0Sstevel@tonic-gate 				time_state = TIME_OOP;
758*0Sstevel@tonic-gate 			}
759*0Sstevel@tonic-gate 			break;
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 		case TIME_DEL:
762*0Sstevel@tonic-gate 			if ((hrestime.tv_sec + 1) % 86400 == 0) {
763*0Sstevel@tonic-gate 				s = hr_clock_lock();
764*0Sstevel@tonic-gate 				hrestime.tv_sec++;
765*0Sstevel@tonic-gate 				hr_clock_unlock(s);
766*0Sstevel@tonic-gate 				time_state = TIME_WAIT;
767*0Sstevel@tonic-gate 			}
768*0Sstevel@tonic-gate 			break;
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 		case TIME_OOP:
771*0Sstevel@tonic-gate 			time_state = TIME_WAIT;
772*0Sstevel@tonic-gate 			break;
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate 		case TIME_WAIT:
775*0Sstevel@tonic-gate 			if (!(time_status & (STA_INS | STA_DEL)))
776*0Sstevel@tonic-gate 				time_state = TIME_OK;
777*0Sstevel@tonic-gate 		default:
778*0Sstevel@tonic-gate 			break;
779*0Sstevel@tonic-gate 		}
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 		/*
782*0Sstevel@tonic-gate 		 * Compute the phase adjustment for the next second. In
783*0Sstevel@tonic-gate 		 * PLL mode, the offset is reduced by a fixed factor
784*0Sstevel@tonic-gate 		 * times the time constant. In FLL mode the offset is
785*0Sstevel@tonic-gate 		 * used directly. In either mode, the maximum phase
786*0Sstevel@tonic-gate 		 * adjustment for each second is clamped so as to spread
787*0Sstevel@tonic-gate 		 * the adjustment over not more than the number of
788*0Sstevel@tonic-gate 		 * seconds between updates.
789*0Sstevel@tonic-gate 		 */
790*0Sstevel@tonic-gate 		if (time_offset == 0)
791*0Sstevel@tonic-gate 			time_adj = 0;
792*0Sstevel@tonic-gate 		else if (time_offset < 0) {
793*0Sstevel@tonic-gate 			lltemp = -time_offset;
794*0Sstevel@tonic-gate 			if (!(time_status & STA_FLL)) {
795*0Sstevel@tonic-gate 				if ((1 << time_constant) >= SCALE_KG)
796*0Sstevel@tonic-gate 					lltemp *= (1 << time_constant) /
797*0Sstevel@tonic-gate 					    SCALE_KG;
798*0Sstevel@tonic-gate 				else
799*0Sstevel@tonic-gate 					lltemp = (lltemp / SCALE_KG) >>
800*0Sstevel@tonic-gate 					    time_constant;
801*0Sstevel@tonic-gate 			}
802*0Sstevel@tonic-gate 			if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
803*0Sstevel@tonic-gate 				lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
804*0Sstevel@tonic-gate 			time_offset += lltemp;
805*0Sstevel@tonic-gate 			time_adj = -(lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
806*0Sstevel@tonic-gate 		} else {
807*0Sstevel@tonic-gate 			lltemp = time_offset;
808*0Sstevel@tonic-gate 			if (!(time_status & STA_FLL)) {
809*0Sstevel@tonic-gate 				if ((1 << time_constant) >= SCALE_KG)
810*0Sstevel@tonic-gate 					lltemp *= (1 << time_constant) /
811*0Sstevel@tonic-gate 					    SCALE_KG;
812*0Sstevel@tonic-gate 				else
813*0Sstevel@tonic-gate 					lltemp = (lltemp / SCALE_KG) >>
814*0Sstevel@tonic-gate 					    time_constant;
815*0Sstevel@tonic-gate 			}
816*0Sstevel@tonic-gate 			if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
817*0Sstevel@tonic-gate 				lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
818*0Sstevel@tonic-gate 			time_offset -= lltemp;
819*0Sstevel@tonic-gate 			time_adj = (lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
820*0Sstevel@tonic-gate 		}
821*0Sstevel@tonic-gate 
822*0Sstevel@tonic-gate 		/*
823*0Sstevel@tonic-gate 		 * Compute the frequency estimate and additional phase
824*0Sstevel@tonic-gate 		 * adjustment due to frequency error for the next
825*0Sstevel@tonic-gate 		 * second. When the PPS signal is engaged, gnaw on the
826*0Sstevel@tonic-gate 		 * watchdog counter and update the frequency computed by
827*0Sstevel@tonic-gate 		 * the pll and the PPS signal.
828*0Sstevel@tonic-gate 		 */
829*0Sstevel@tonic-gate 		pps_valid++;
830*0Sstevel@tonic-gate 		if (pps_valid == PPS_VALID) {
831*0Sstevel@tonic-gate 			pps_jitter = MAXTIME;
832*0Sstevel@tonic-gate 			pps_stabil = MAXFREQ;
833*0Sstevel@tonic-gate 			time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
834*0Sstevel@tonic-gate 			    STA_PPSWANDER | STA_PPSERROR);
835*0Sstevel@tonic-gate 		}
836*0Sstevel@tonic-gate 		lltemp = time_freq + pps_freq;
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 		if (lltemp)
839*0Sstevel@tonic-gate 			time_adj += (lltemp * SCALE_PHASE) / (SCALE_USEC * hz);
840*0Sstevel@tonic-gate 
841*0Sstevel@tonic-gate 		/*
842*0Sstevel@tonic-gate 		 * End of precision kernel-code fragment
843*0Sstevel@tonic-gate 		 *
844*0Sstevel@tonic-gate 		 * The section below should be modified if we are planning
845*0Sstevel@tonic-gate 		 * to use NTP for synchronization.
846*0Sstevel@tonic-gate 		 *
847*0Sstevel@tonic-gate 		 * Note: the clock synchronization code now assumes
848*0Sstevel@tonic-gate 		 * the following:
849*0Sstevel@tonic-gate 		 *   - if dosynctodr is 1, then compute the drift between
850*0Sstevel@tonic-gate 		 *	the tod chip and software time and adjust one or
851*0Sstevel@tonic-gate 		 *	the other depending on the circumstances
852*0Sstevel@tonic-gate 		 *
853*0Sstevel@tonic-gate 		 *   - if dosynctodr is 0, then the tod chip is independent
854*0Sstevel@tonic-gate 		 *	of the software clock and should not be adjusted,
855*0Sstevel@tonic-gate 		 *	but allowed to free run.  this allows NTP to sync.
856*0Sstevel@tonic-gate 		 *	hrestime without any interference from the tod chip.
857*0Sstevel@tonic-gate 		 */
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 		mutex_enter(&tod_lock);
860*0Sstevel@tonic-gate 		tod = tod_get();
861*0Sstevel@tonic-gate 		drift = tod.tv_sec - hrestime.tv_sec;
862*0Sstevel@tonic-gate 		absdrift = (drift >= 0) ? drift : -drift;
863*0Sstevel@tonic-gate 		if (tod_needsync || absdrift > 1) {
864*0Sstevel@tonic-gate 			int s;
865*0Sstevel@tonic-gate 			if (absdrift > 2) {
866*0Sstevel@tonic-gate 				if (!tod_broken && tod_faulted == TOD_NOFAULT) {
867*0Sstevel@tonic-gate 					s = hr_clock_lock();
868*0Sstevel@tonic-gate 					hrestime = tod;
869*0Sstevel@tonic-gate 					membar_enter();	/* hrestime visible */
870*0Sstevel@tonic-gate 					timedelta = 0;
871*0Sstevel@tonic-gate 					timechanged++;
872*0Sstevel@tonic-gate 					tod_needsync = 0;
873*0Sstevel@tonic-gate 					hr_clock_unlock(s);
874*0Sstevel@tonic-gate 				}
875*0Sstevel@tonic-gate 			} else {
876*0Sstevel@tonic-gate 				if (tod_needsync || !dosynctodr) {
877*0Sstevel@tonic-gate 					gethrestime(&tod);
878*0Sstevel@tonic-gate 					tod_set(tod);
879*0Sstevel@tonic-gate 					s = hr_clock_lock();
880*0Sstevel@tonic-gate 					if (timedelta == 0)
881*0Sstevel@tonic-gate 						tod_needsync = 0;
882*0Sstevel@tonic-gate 					hr_clock_unlock(s);
883*0Sstevel@tonic-gate 				} else {
884*0Sstevel@tonic-gate 					/*
885*0Sstevel@tonic-gate 					 * If the drift is 2 seconds on the
886*0Sstevel@tonic-gate 					 * money, then the TOD is adjusting
887*0Sstevel@tonic-gate 					 * the clock;  record that.
888*0Sstevel@tonic-gate 					 */
889*0Sstevel@tonic-gate 					clock_adj_hist[adj_hist_entry++ %
890*0Sstevel@tonic-gate 					    CLOCK_ADJ_HIST_SIZE] = lbolt64;
891*0Sstevel@tonic-gate 					s = hr_clock_lock();
892*0Sstevel@tonic-gate 					timedelta = (int64_t)drift*NANOSEC;
893*0Sstevel@tonic-gate 					hr_clock_unlock(s);
894*0Sstevel@tonic-gate 				}
895*0Sstevel@tonic-gate 			}
896*0Sstevel@tonic-gate 		}
897*0Sstevel@tonic-gate 		one_sec = 0;
898*0Sstevel@tonic-gate 		time = gethrestime_sec();  /* for crusty old kmem readers */
899*0Sstevel@tonic-gate 		mutex_exit(&tod_lock);
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 		/*
902*0Sstevel@tonic-gate 		 * Some drivers still depend on this... XXX
903*0Sstevel@tonic-gate 		 */
904*0Sstevel@tonic-gate 		cv_broadcast(&lbolt_cv);
905*0Sstevel@tonic-gate 
906*0Sstevel@tonic-gate 		sysinfo.updates++;
907*0Sstevel@tonic-gate 		vminfo.freemem += freemem;
908*0Sstevel@tonic-gate 		{
909*0Sstevel@tonic-gate 			pgcnt_t maxswap, resv, free;
910*0Sstevel@tonic-gate 			pgcnt_t avail =
911*0Sstevel@tonic-gate 			    MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate 			maxswap = k_anoninfo.ani_mem_resv
914*0Sstevel@tonic-gate 					+ k_anoninfo.ani_max +avail;
915*0Sstevel@tonic-gate 			free = k_anoninfo.ani_free + avail;
916*0Sstevel@tonic-gate 			resv = k_anoninfo.ani_phys_resv +
917*0Sstevel@tonic-gate 			    k_anoninfo.ani_mem_resv;
918*0Sstevel@tonic-gate 
919*0Sstevel@tonic-gate 			vminfo.swap_resv += resv;
920*0Sstevel@tonic-gate 			/* number of reserved and allocated pages */
921*0Sstevel@tonic-gate #ifdef	DEBUG
922*0Sstevel@tonic-gate 			if (maxswap < free)
923*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "clock: maxswap < free");
924*0Sstevel@tonic-gate 			if (maxswap < resv)
925*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "clock: maxswap < resv");
926*0Sstevel@tonic-gate #endif
927*0Sstevel@tonic-gate 			vminfo.swap_alloc += maxswap - free;
928*0Sstevel@tonic-gate 			vminfo.swap_avail += maxswap - resv;
929*0Sstevel@tonic-gate 			vminfo.swap_free += free;
930*0Sstevel@tonic-gate 		}
931*0Sstevel@tonic-gate 		if (nrunnable) {
932*0Sstevel@tonic-gate 			sysinfo.runque += nrunnable;
933*0Sstevel@tonic-gate 			sysinfo.runocc++;
934*0Sstevel@tonic-gate 		}
935*0Sstevel@tonic-gate 		if (nswapped) {
936*0Sstevel@tonic-gate 			sysinfo.swpque += nswapped;
937*0Sstevel@tonic-gate 			sysinfo.swpocc++;
938*0Sstevel@tonic-gate 		}
939*0Sstevel@tonic-gate 		sysinfo.waiting += w_io;
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate 		/*
942*0Sstevel@tonic-gate 		 * Wake up fsflush to write out DELWRI
943*0Sstevel@tonic-gate 		 * buffers, dirty pages and other cached
944*0Sstevel@tonic-gate 		 * administrative data, e.g. inodes.
945*0Sstevel@tonic-gate 		 */
946*0Sstevel@tonic-gate 		if (--fsflushcnt <= 0) {
947*0Sstevel@tonic-gate 			fsflushcnt = tune.t_fsflushr;
948*0Sstevel@tonic-gate 			cv_signal(&fsflush_cv);
949*0Sstevel@tonic-gate 		}
950*0Sstevel@tonic-gate 
951*0Sstevel@tonic-gate 		vmmeter();
952*0Sstevel@tonic-gate 		calcloadavg(genloadavg(&loadavg), hp_avenrun);
953*0Sstevel@tonic-gate 		for (i = 0; i < 3; i++)
954*0Sstevel@tonic-gate 			/*
955*0Sstevel@tonic-gate 			 * At the moment avenrun[] can only hold 31
956*0Sstevel@tonic-gate 			 * bits of load average as it is a signed
957*0Sstevel@tonic-gate 			 * int in the API. We need to ensure that
958*0Sstevel@tonic-gate 			 * hp_avenrun[i] >> (16 - FSHIFT) will not be
959*0Sstevel@tonic-gate 			 * too large. If it is, we put the largest value
960*0Sstevel@tonic-gate 			 * that we can use into avenrun[i]. This is
961*0Sstevel@tonic-gate 			 * kludgey, but about all we can do until we
962*0Sstevel@tonic-gate 			 * avenrun[] is declared as an array of uint64[]
963*0Sstevel@tonic-gate 			 */
964*0Sstevel@tonic-gate 			if (hp_avenrun[i] < ((uint64_t)1<<(31+16-FSHIFT)))
965*0Sstevel@tonic-gate 				avenrun[i] = (int32_t)(hp_avenrun[i] >>
966*0Sstevel@tonic-gate 				    (16 - FSHIFT));
967*0Sstevel@tonic-gate 			else
968*0Sstevel@tonic-gate 				avenrun[i] = 0x7fffffff;
969*0Sstevel@tonic-gate 
970*0Sstevel@tonic-gate 		cpupart = cp_list_head;
971*0Sstevel@tonic-gate 		do {
972*0Sstevel@tonic-gate 			calcloadavg(genloadavg(&cpupart->cp_loadavg),
973*0Sstevel@tonic-gate 			    cpupart->cp_hp_avenrun);
974*0Sstevel@tonic-gate 		} while ((cpupart = cpupart->cp_next) != cp_list_head);
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate 		/*
977*0Sstevel@tonic-gate 		 * Wake up the swapper thread if necessary.
978*0Sstevel@tonic-gate 		 */
979*0Sstevel@tonic-gate 		if (runin ||
980*0Sstevel@tonic-gate 		    (runout && (avefree < desfree || wake_sched_sec))) {
981*0Sstevel@tonic-gate 			t = &t0;
982*0Sstevel@tonic-gate 			thread_lock(t);
983*0Sstevel@tonic-gate 			if (t->t_state == TS_STOPPED) {
984*0Sstevel@tonic-gate 				runin = runout = 0;
985*0Sstevel@tonic-gate 				wake_sched_sec = 0;
986*0Sstevel@tonic-gate 				t->t_whystop = 0;
987*0Sstevel@tonic-gate 				t->t_whatstop = 0;
988*0Sstevel@tonic-gate 				t->t_schedflag &= ~TS_ALLSTART;
989*0Sstevel@tonic-gate 				THREAD_TRANSITION(t);
990*0Sstevel@tonic-gate 				setfrontdq(t);
991*0Sstevel@tonic-gate 			}
992*0Sstevel@tonic-gate 			thread_unlock(t);
993*0Sstevel@tonic-gate 		}
994*0Sstevel@tonic-gate 	}
995*0Sstevel@tonic-gate 
996*0Sstevel@tonic-gate 	/*
997*0Sstevel@tonic-gate 	 * Wake up the swapper if any high priority swapped-out threads
998*0Sstevel@tonic-gate 	 * became runable during the last tick.
999*0Sstevel@tonic-gate 	 */
1000*0Sstevel@tonic-gate 	if (wake_sched) {
1001*0Sstevel@tonic-gate 		t = &t0;
1002*0Sstevel@tonic-gate 		thread_lock(t);
1003*0Sstevel@tonic-gate 		if (t->t_state == TS_STOPPED) {
1004*0Sstevel@tonic-gate 			runin = runout = 0;
1005*0Sstevel@tonic-gate 			wake_sched = 0;
1006*0Sstevel@tonic-gate 			t->t_whystop = 0;
1007*0Sstevel@tonic-gate 			t->t_whatstop = 0;
1008*0Sstevel@tonic-gate 			t->t_schedflag &= ~TS_ALLSTART;
1009*0Sstevel@tonic-gate 			THREAD_TRANSITION(t);
1010*0Sstevel@tonic-gate 			setfrontdq(t);
1011*0Sstevel@tonic-gate 		}
1012*0Sstevel@tonic-gate 		thread_unlock(t);
1013*0Sstevel@tonic-gate 	}
1014*0Sstevel@tonic-gate }
1015*0Sstevel@tonic-gate 
1016*0Sstevel@tonic-gate void
1017*0Sstevel@tonic-gate clock_init(void)
1018*0Sstevel@tonic-gate {
1019*0Sstevel@tonic-gate 	cyc_handler_t hdlr;
1020*0Sstevel@tonic-gate 	cyc_time_t when;
1021*0Sstevel@tonic-gate 
1022*0Sstevel@tonic-gate 	hdlr.cyh_func = (cyc_func_t)clock;
1023*0Sstevel@tonic-gate 	hdlr.cyh_level = CY_LOCK_LEVEL;
1024*0Sstevel@tonic-gate 	hdlr.cyh_arg = NULL;
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 	when.cyt_when = 0;
1027*0Sstevel@tonic-gate 	when.cyt_interval = nsec_per_tick;
1028*0Sstevel@tonic-gate 
1029*0Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1030*0Sstevel@tonic-gate 	clock_cyclic = cyclic_add(&hdlr, &when);
1031*0Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1032*0Sstevel@tonic-gate }
1033*0Sstevel@tonic-gate 
1034*0Sstevel@tonic-gate /*
1035*0Sstevel@tonic-gate  * Called before calcloadavg to get 10-sec moving loadavg together
1036*0Sstevel@tonic-gate  */
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate static int
1039*0Sstevel@tonic-gate genloadavg(struct loadavg_s *avgs)
1040*0Sstevel@tonic-gate {
1041*0Sstevel@tonic-gate 	int avg;
1042*0Sstevel@tonic-gate 	int spos; /* starting position */
1043*0Sstevel@tonic-gate 	int cpos; /* moving current position */
1044*0Sstevel@tonic-gate 	int i;
1045*0Sstevel@tonic-gate 	int slen;
1046*0Sstevel@tonic-gate 	hrtime_t hr_avg;
1047*0Sstevel@tonic-gate 
1048*0Sstevel@tonic-gate 	/* 10-second snapshot, calculate first positon */
1049*0Sstevel@tonic-gate 	if (avgs->lg_len == 0) {
1050*0Sstevel@tonic-gate 		return (0);
1051*0Sstevel@tonic-gate 	}
1052*0Sstevel@tonic-gate 	slen = avgs->lg_len < S_MOVAVG_SZ ? avgs->lg_len : S_MOVAVG_SZ;
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate 	spos = (avgs->lg_cur - 1) >= 0 ? avgs->lg_cur - 1 :
1055*0Sstevel@tonic-gate 	    S_LOADAVG_SZ + (avgs->lg_cur - 1);
1056*0Sstevel@tonic-gate 	for (i = hr_avg = 0; i < slen; i++) {
1057*0Sstevel@tonic-gate 		cpos = (spos - i) >= 0 ? spos - i : S_LOADAVG_SZ + (spos - i);
1058*0Sstevel@tonic-gate 		hr_avg += avgs->lg_loads[cpos];
1059*0Sstevel@tonic-gate 	}
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate 	hr_avg = hr_avg / slen;
1062*0Sstevel@tonic-gate 	avg = hr_avg / (NANOSEC / LGRP_LOADAVG_IN_THREAD_MAX);
1063*0Sstevel@tonic-gate 
1064*0Sstevel@tonic-gate 	return (avg);
1065*0Sstevel@tonic-gate }
1066*0Sstevel@tonic-gate 
1067*0Sstevel@tonic-gate /*
1068*0Sstevel@tonic-gate  * Run every second from clock () to update the loadavg count available to the
1069*0Sstevel@tonic-gate  * system and cpu-partitions.
1070*0Sstevel@tonic-gate  *
1071*0Sstevel@tonic-gate  * This works by sampling the previous usr, sys, wait time elapsed,
1072*0Sstevel@tonic-gate  * computing a delta, and adding that delta to the elapsed usr, sys,
1073*0Sstevel@tonic-gate  * wait increase.
1074*0Sstevel@tonic-gate  */
1075*0Sstevel@tonic-gate 
1076*0Sstevel@tonic-gate static void
1077*0Sstevel@tonic-gate loadavg_update()
1078*0Sstevel@tonic-gate {
1079*0Sstevel@tonic-gate 	cpu_t *cp;
1080*0Sstevel@tonic-gate 	cpupart_t *cpupart;
1081*0Sstevel@tonic-gate 	hrtime_t cpu_total;
1082*0Sstevel@tonic-gate 	int prev;
1083*0Sstevel@tonic-gate 
1084*0Sstevel@tonic-gate 	cp = cpu_list;
1085*0Sstevel@tonic-gate 	loadavg.lg_total = 0;
1086*0Sstevel@tonic-gate 
1087*0Sstevel@tonic-gate 	/*
1088*0Sstevel@tonic-gate 	 * first pass totals up per-cpu statistics for system and cpu
1089*0Sstevel@tonic-gate 	 * partitions
1090*0Sstevel@tonic-gate 	 */
1091*0Sstevel@tonic-gate 
1092*0Sstevel@tonic-gate 	do {
1093*0Sstevel@tonic-gate 		struct loadavg_s *lavg;
1094*0Sstevel@tonic-gate 
1095*0Sstevel@tonic-gate 		lavg = &cp->cpu_loadavg;
1096*0Sstevel@tonic-gate 
1097*0Sstevel@tonic-gate 		cpu_total = cp->cpu_acct[CMS_USER] +
1098*0Sstevel@tonic-gate 		    cp->cpu_acct[CMS_SYSTEM] + cp->cpu_waitrq;
1099*0Sstevel@tonic-gate 		/* compute delta against last total */
1100*0Sstevel@tonic-gate 		scalehrtime(&cpu_total);
1101*0Sstevel@tonic-gate 		prev = (lavg->lg_cur - 1) >= 0 ? lavg->lg_cur - 1 :
1102*0Sstevel@tonic-gate 		    S_LOADAVG_SZ + (lavg->lg_cur - 1);
1103*0Sstevel@tonic-gate 		if (lavg->lg_loads[prev] <= 0) {
1104*0Sstevel@tonic-gate 			lavg->lg_loads[lavg->lg_cur] = cpu_total;
1105*0Sstevel@tonic-gate 			cpu_total = 0;
1106*0Sstevel@tonic-gate 		} else {
1107*0Sstevel@tonic-gate 			lavg->lg_loads[lavg->lg_cur] = cpu_total;
1108*0Sstevel@tonic-gate 			cpu_total = cpu_total - lavg->lg_loads[prev];
1109*0Sstevel@tonic-gate 			if (cpu_total < 0)
1110*0Sstevel@tonic-gate 				cpu_total = 0;
1111*0Sstevel@tonic-gate 		}
1112*0Sstevel@tonic-gate 
1113*0Sstevel@tonic-gate 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
1114*0Sstevel@tonic-gate 		lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
1115*0Sstevel@tonic-gate 		    lavg->lg_len + 1 : S_LOADAVG_SZ;
1116*0Sstevel@tonic-gate 
1117*0Sstevel@tonic-gate 		loadavg.lg_total += cpu_total;
1118*0Sstevel@tonic-gate 		cp->cpu_part->cp_loadavg.lg_total += cpu_total;
1119*0Sstevel@tonic-gate 
1120*0Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
1121*0Sstevel@tonic-gate 
1122*0Sstevel@tonic-gate 	loadavg.lg_loads[loadavg.lg_cur] = loadavg.lg_total;
1123*0Sstevel@tonic-gate 	loadavg.lg_cur = (loadavg.lg_cur + 1) % S_LOADAVG_SZ;
1124*0Sstevel@tonic-gate 	loadavg.lg_len = (loadavg.lg_len + 1) < S_LOADAVG_SZ ?
1125*0Sstevel@tonic-gate 	    loadavg.lg_len + 1 : S_LOADAVG_SZ;
1126*0Sstevel@tonic-gate 	/*
1127*0Sstevel@tonic-gate 	 * Second pass updates counts
1128*0Sstevel@tonic-gate 	 */
1129*0Sstevel@tonic-gate 	cpupart = cp_list_head;
1130*0Sstevel@tonic-gate 
1131*0Sstevel@tonic-gate 	do {
1132*0Sstevel@tonic-gate 		struct loadavg_s *lavg;
1133*0Sstevel@tonic-gate 
1134*0Sstevel@tonic-gate 		lavg = &cpupart->cp_loadavg;
1135*0Sstevel@tonic-gate 		lavg->lg_loads[lavg->lg_cur] = lavg->lg_total;
1136*0Sstevel@tonic-gate 		lavg->lg_total = 0;
1137*0Sstevel@tonic-gate 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
1138*0Sstevel@tonic-gate 		lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
1139*0Sstevel@tonic-gate 		    lavg->lg_len + 1 : S_LOADAVG_SZ;
1140*0Sstevel@tonic-gate 
1141*0Sstevel@tonic-gate 	} while ((cpupart = cpupart->cp_next) != cp_list_head);
1142*0Sstevel@tonic-gate 
1143*0Sstevel@tonic-gate }
1144*0Sstevel@tonic-gate 
1145*0Sstevel@tonic-gate /*
1146*0Sstevel@tonic-gate  * clock_update() - local clock update
1147*0Sstevel@tonic-gate  *
1148*0Sstevel@tonic-gate  * This routine is called by ntp_adjtime() to update the local clock
1149*0Sstevel@tonic-gate  * phase and frequency. The implementation is of an
1150*0Sstevel@tonic-gate  * adaptive-parameter, hybrid phase/frequency-lock loop (PLL/FLL). The
1151*0Sstevel@tonic-gate  * routine computes new time and frequency offset estimates for each
1152*0Sstevel@tonic-gate  * call.  The PPS signal itself determines the new time offset,
1153*0Sstevel@tonic-gate  * instead of the calling argument.  Presumably, calls to
1154*0Sstevel@tonic-gate  * ntp_adjtime() occur only when the caller believes the local clock
1155*0Sstevel@tonic-gate  * is valid within some bound (+-128 ms with NTP). If the caller's
1156*0Sstevel@tonic-gate  * time is far different than the PPS time, an argument will ensue,
1157*0Sstevel@tonic-gate  * and it's not clear who will lose.
1158*0Sstevel@tonic-gate  *
1159*0Sstevel@tonic-gate  * For uncompensated quartz crystal oscillatores and nominal update
1160*0Sstevel@tonic-gate  * intervals less than 1024 s, operation should be in phase-lock mode
1161*0Sstevel@tonic-gate  * (STA_FLL = 0), where the loop is disciplined to phase. For update
1162*0Sstevel@tonic-gate  * intervals greater than this, operation should be in frequency-lock
1163*0Sstevel@tonic-gate  * mode (STA_FLL = 1), where the loop is disciplined to frequency.
1164*0Sstevel@tonic-gate  *
1165*0Sstevel@tonic-gate  * Note: mutex(&tod_lock) is in effect.
1166*0Sstevel@tonic-gate  */
1167*0Sstevel@tonic-gate void
1168*0Sstevel@tonic-gate clock_update(int offset)
1169*0Sstevel@tonic-gate {
1170*0Sstevel@tonic-gate 	int ltemp, mtemp, s;
1171*0Sstevel@tonic-gate 
1172*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
1173*0Sstevel@tonic-gate 
1174*0Sstevel@tonic-gate 	if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME))
1175*0Sstevel@tonic-gate 		return;
1176*0Sstevel@tonic-gate 	ltemp = offset;
1177*0Sstevel@tonic-gate 	if ((time_status & STA_PPSTIME) && (time_status & STA_PPSSIGNAL))
1178*0Sstevel@tonic-gate 		ltemp = pps_offset;
1179*0Sstevel@tonic-gate 
1180*0Sstevel@tonic-gate 	/*
1181*0Sstevel@tonic-gate 	 * Scale the phase adjustment and clamp to the operating range.
1182*0Sstevel@tonic-gate 	 */
1183*0Sstevel@tonic-gate 	if (ltemp > MAXPHASE)
1184*0Sstevel@tonic-gate 		time_offset = MAXPHASE * SCALE_UPDATE;
1185*0Sstevel@tonic-gate 	else if (ltemp < -MAXPHASE)
1186*0Sstevel@tonic-gate 		time_offset = -(MAXPHASE * SCALE_UPDATE);
1187*0Sstevel@tonic-gate 	else
1188*0Sstevel@tonic-gate 		time_offset = ltemp * SCALE_UPDATE;
1189*0Sstevel@tonic-gate 
1190*0Sstevel@tonic-gate 	/*
1191*0Sstevel@tonic-gate 	 * Select whether the frequency is to be controlled and in which
1192*0Sstevel@tonic-gate 	 * mode (PLL or FLL). Clamp to the operating range. Ugly
1193*0Sstevel@tonic-gate 	 * multiply/divide should be replaced someday.
1194*0Sstevel@tonic-gate 	 */
1195*0Sstevel@tonic-gate 	if (time_status & STA_FREQHOLD || time_reftime == 0)
1196*0Sstevel@tonic-gate 		time_reftime = hrestime.tv_sec;
1197*0Sstevel@tonic-gate 
1198*0Sstevel@tonic-gate 	mtemp = hrestime.tv_sec - time_reftime;
1199*0Sstevel@tonic-gate 	time_reftime = hrestime.tv_sec;
1200*0Sstevel@tonic-gate 
1201*0Sstevel@tonic-gate 	if (time_status & STA_FLL) {
1202*0Sstevel@tonic-gate 		if (mtemp >= MINSEC) {
1203*0Sstevel@tonic-gate 			ltemp = ((time_offset / mtemp) * (SCALE_USEC /
1204*0Sstevel@tonic-gate 			    SCALE_UPDATE));
1205*0Sstevel@tonic-gate 			if (ltemp)
1206*0Sstevel@tonic-gate 				time_freq += ltemp / SCALE_KH;
1207*0Sstevel@tonic-gate 		}
1208*0Sstevel@tonic-gate 	} else {
1209*0Sstevel@tonic-gate 		if (mtemp < MAXSEC) {
1210*0Sstevel@tonic-gate 			ltemp *= mtemp;
1211*0Sstevel@tonic-gate 			if (ltemp)
1212*0Sstevel@tonic-gate 				time_freq += (int)(((int64_t)ltemp *
1213*0Sstevel@tonic-gate 				    SCALE_USEC) / SCALE_KF)
1214*0Sstevel@tonic-gate 				    / (1 << (time_constant * 2));
1215*0Sstevel@tonic-gate 		}
1216*0Sstevel@tonic-gate 	}
1217*0Sstevel@tonic-gate 	if (time_freq > time_tolerance)
1218*0Sstevel@tonic-gate 		time_freq = time_tolerance;
1219*0Sstevel@tonic-gate 	else if (time_freq < -time_tolerance)
1220*0Sstevel@tonic-gate 		time_freq = -time_tolerance;
1221*0Sstevel@tonic-gate 
1222*0Sstevel@tonic-gate 	s = hr_clock_lock();
1223*0Sstevel@tonic-gate 	tod_needsync = 1;
1224*0Sstevel@tonic-gate 	hr_clock_unlock(s);
1225*0Sstevel@tonic-gate }
1226*0Sstevel@tonic-gate 
1227*0Sstevel@tonic-gate /*
1228*0Sstevel@tonic-gate  * ddi_hardpps() - discipline CPU clock oscillator to external PPS signal
1229*0Sstevel@tonic-gate  *
1230*0Sstevel@tonic-gate  * This routine is called at each PPS interrupt in order to discipline
1231*0Sstevel@tonic-gate  * the CPU clock oscillator to the PPS signal. It measures the PPS phase
1232*0Sstevel@tonic-gate  * and leaves it in a handy spot for the clock() routine. It
1233*0Sstevel@tonic-gate  * integrates successive PPS phase differences and calculates the
1234*0Sstevel@tonic-gate  * frequency offset. This is used in clock() to discipline the CPU
1235*0Sstevel@tonic-gate  * clock oscillator so that intrinsic frequency error is cancelled out.
1236*0Sstevel@tonic-gate  * The code requires the caller to capture the time and hardware counter
1237*0Sstevel@tonic-gate  * value at the on-time PPS signal transition.
1238*0Sstevel@tonic-gate  *
1239*0Sstevel@tonic-gate  * Note that, on some Unix systems, this routine runs at an interrupt
1240*0Sstevel@tonic-gate  * priority level higher than the timer interrupt routine clock().
1241*0Sstevel@tonic-gate  * Therefore, the variables used are distinct from the clock()
1242*0Sstevel@tonic-gate  * variables, except for certain exceptions: The PPS frequency pps_freq
1243*0Sstevel@tonic-gate  * and phase pps_offset variables are determined by this routine and
1244*0Sstevel@tonic-gate  * updated atomically. The time_tolerance variable can be considered a
1245*0Sstevel@tonic-gate  * constant, since it is infrequently changed, and then only when the
1246*0Sstevel@tonic-gate  * PPS signal is disabled. The watchdog counter pps_valid is updated
1247*0Sstevel@tonic-gate  * once per second by clock() and is atomically cleared in this
1248*0Sstevel@tonic-gate  * routine.
1249*0Sstevel@tonic-gate  *
1250*0Sstevel@tonic-gate  * tvp is the time of the last tick; usec is a microsecond count since the
1251*0Sstevel@tonic-gate  * last tick.
1252*0Sstevel@tonic-gate  *
1253*0Sstevel@tonic-gate  * Note: In Solaris systems, the tick value is actually given by
1254*0Sstevel@tonic-gate  *       usec_per_tick.  This is called from the serial driver cdintr(),
1255*0Sstevel@tonic-gate  *	 or equivalent, at a high PIL.  Because the kernel keeps a
1256*0Sstevel@tonic-gate  *	 highresolution time, the following code can accept either
1257*0Sstevel@tonic-gate  *	 the traditional argument pair, or the current highres timestamp
1258*0Sstevel@tonic-gate  *       in tvp and zero in usec.
1259*0Sstevel@tonic-gate  */
1260*0Sstevel@tonic-gate void
1261*0Sstevel@tonic-gate ddi_hardpps(struct timeval *tvp, int usec)
1262*0Sstevel@tonic-gate {
1263*0Sstevel@tonic-gate 	int u_usec, v_usec, bigtick;
1264*0Sstevel@tonic-gate 	time_t cal_sec;
1265*0Sstevel@tonic-gate 	int cal_usec;
1266*0Sstevel@tonic-gate 
1267*0Sstevel@tonic-gate 	/*
1268*0Sstevel@tonic-gate 	 * An occasional glitch can be produced when the PPS interrupt
1269*0Sstevel@tonic-gate 	 * occurs in the clock() routine before the time variable is
1270*0Sstevel@tonic-gate 	 * updated. Here the offset is discarded when the difference
1271*0Sstevel@tonic-gate 	 * between it and the last one is greater than tick/2, but not
1272*0Sstevel@tonic-gate 	 * if the interval since the first discard exceeds 30 s.
1273*0Sstevel@tonic-gate 	 */
1274*0Sstevel@tonic-gate 	time_status |= STA_PPSSIGNAL;
1275*0Sstevel@tonic-gate 	time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
1276*0Sstevel@tonic-gate 	pps_valid = 0;
1277*0Sstevel@tonic-gate 	u_usec = -tvp->tv_usec;
1278*0Sstevel@tonic-gate 	if (u_usec < -(MICROSEC/2))
1279*0Sstevel@tonic-gate 		u_usec += MICROSEC;
1280*0Sstevel@tonic-gate 	v_usec = pps_offset - u_usec;
1281*0Sstevel@tonic-gate 	if (v_usec < 0)
1282*0Sstevel@tonic-gate 		v_usec = -v_usec;
1283*0Sstevel@tonic-gate 	if (v_usec > (usec_per_tick >> 1)) {
1284*0Sstevel@tonic-gate 		if (pps_glitch > MAXGLITCH) {
1285*0Sstevel@tonic-gate 			pps_glitch = 0;
1286*0Sstevel@tonic-gate 			pps_tf[2] = u_usec;
1287*0Sstevel@tonic-gate 			pps_tf[1] = u_usec;
1288*0Sstevel@tonic-gate 		} else {
1289*0Sstevel@tonic-gate 			pps_glitch++;
1290*0Sstevel@tonic-gate 			u_usec = pps_offset;
1291*0Sstevel@tonic-gate 		}
1292*0Sstevel@tonic-gate 	} else
1293*0Sstevel@tonic-gate 		pps_glitch = 0;
1294*0Sstevel@tonic-gate 
1295*0Sstevel@tonic-gate 	/*
1296*0Sstevel@tonic-gate 	 * A three-stage median filter is used to help deglitch the pps
1297*0Sstevel@tonic-gate 	 * time. The median sample becomes the time offset estimate; the
1298*0Sstevel@tonic-gate 	 * difference between the other two samples becomes the time
1299*0Sstevel@tonic-gate 	 * dispersion (jitter) estimate.
1300*0Sstevel@tonic-gate 	 */
1301*0Sstevel@tonic-gate 	pps_tf[2] = pps_tf[1];
1302*0Sstevel@tonic-gate 	pps_tf[1] = pps_tf[0];
1303*0Sstevel@tonic-gate 	pps_tf[0] = u_usec;
1304*0Sstevel@tonic-gate 	if (pps_tf[0] > pps_tf[1]) {
1305*0Sstevel@tonic-gate 		if (pps_tf[1] > pps_tf[2]) {
1306*0Sstevel@tonic-gate 			pps_offset = pps_tf[1];		/* 0 1 2 */
1307*0Sstevel@tonic-gate 			v_usec = pps_tf[0] - pps_tf[2];
1308*0Sstevel@tonic-gate 		} else if (pps_tf[2] > pps_tf[0]) {
1309*0Sstevel@tonic-gate 			pps_offset = pps_tf[0];		/* 2 0 1 */
1310*0Sstevel@tonic-gate 			v_usec = pps_tf[2] - pps_tf[1];
1311*0Sstevel@tonic-gate 		} else {
1312*0Sstevel@tonic-gate 			pps_offset = pps_tf[2];		/* 0 2 1 */
1313*0Sstevel@tonic-gate 			v_usec = pps_tf[0] - pps_tf[1];
1314*0Sstevel@tonic-gate 		}
1315*0Sstevel@tonic-gate 	} else {
1316*0Sstevel@tonic-gate 		if (pps_tf[1] < pps_tf[2]) {
1317*0Sstevel@tonic-gate 			pps_offset = pps_tf[1];		/* 2 1 0 */
1318*0Sstevel@tonic-gate 			v_usec = pps_tf[2] - pps_tf[0];
1319*0Sstevel@tonic-gate 		} else  if (pps_tf[2] < pps_tf[0]) {
1320*0Sstevel@tonic-gate 			pps_offset = pps_tf[0];		/* 1 0 2 */
1321*0Sstevel@tonic-gate 			v_usec = pps_tf[1] - pps_tf[2];
1322*0Sstevel@tonic-gate 		} else {
1323*0Sstevel@tonic-gate 			pps_offset = pps_tf[2];		/* 1 2 0 */
1324*0Sstevel@tonic-gate 			v_usec = pps_tf[1] - pps_tf[0];
1325*0Sstevel@tonic-gate 		}
1326*0Sstevel@tonic-gate 	}
1327*0Sstevel@tonic-gate 	if (v_usec > MAXTIME)
1328*0Sstevel@tonic-gate 		pps_jitcnt++;
1329*0Sstevel@tonic-gate 	v_usec = (v_usec << PPS_AVG) - pps_jitter;
1330*0Sstevel@tonic-gate 	pps_jitter += v_usec / (1 << PPS_AVG);
1331*0Sstevel@tonic-gate 	if (pps_jitter > (MAXTIME >> 1))
1332*0Sstevel@tonic-gate 		time_status |= STA_PPSJITTER;
1333*0Sstevel@tonic-gate 
1334*0Sstevel@tonic-gate 	/*
1335*0Sstevel@tonic-gate 	 * During the calibration interval adjust the starting time when
1336*0Sstevel@tonic-gate 	 * the tick overflows. At the end of the interval compute the
1337*0Sstevel@tonic-gate 	 * duration of the interval and the difference of the hardware
1338*0Sstevel@tonic-gate 	 * counters at the beginning and end of the interval. This code
1339*0Sstevel@tonic-gate 	 * is deliciously complicated by the fact valid differences may
1340*0Sstevel@tonic-gate 	 * exceed the value of tick when using long calibration
1341*0Sstevel@tonic-gate 	 * intervals and small ticks. Note that the counter can be
1342*0Sstevel@tonic-gate 	 * greater than tick if caught at just the wrong instant, but
1343*0Sstevel@tonic-gate 	 * the values returned and used here are correct.
1344*0Sstevel@tonic-gate 	 */
1345*0Sstevel@tonic-gate 	bigtick = (int)usec_per_tick * SCALE_USEC;
1346*0Sstevel@tonic-gate 	pps_usec -= pps_freq;
1347*0Sstevel@tonic-gate 	if (pps_usec >= bigtick)
1348*0Sstevel@tonic-gate 		pps_usec -= bigtick;
1349*0Sstevel@tonic-gate 	if (pps_usec < 0)
1350*0Sstevel@tonic-gate 		pps_usec += bigtick;
1351*0Sstevel@tonic-gate 	pps_time.tv_sec++;
1352*0Sstevel@tonic-gate 	pps_count++;
1353*0Sstevel@tonic-gate 	if (pps_count < (1 << pps_shift))
1354*0Sstevel@tonic-gate 		return;
1355*0Sstevel@tonic-gate 	pps_count = 0;
1356*0Sstevel@tonic-gate 	pps_calcnt++;
1357*0Sstevel@tonic-gate 	u_usec = usec * SCALE_USEC;
1358*0Sstevel@tonic-gate 	v_usec = pps_usec - u_usec;
1359*0Sstevel@tonic-gate 	if (v_usec >= bigtick >> 1)
1360*0Sstevel@tonic-gate 		v_usec -= bigtick;
1361*0Sstevel@tonic-gate 	if (v_usec < -(bigtick >> 1))
1362*0Sstevel@tonic-gate 		v_usec += bigtick;
1363*0Sstevel@tonic-gate 	if (v_usec < 0)
1364*0Sstevel@tonic-gate 		v_usec = -(-v_usec >> pps_shift);
1365*0Sstevel@tonic-gate 	else
1366*0Sstevel@tonic-gate 		v_usec = v_usec >> pps_shift;
1367*0Sstevel@tonic-gate 	pps_usec = u_usec;
1368*0Sstevel@tonic-gate 	cal_sec = tvp->tv_sec;
1369*0Sstevel@tonic-gate 	cal_usec = tvp->tv_usec;
1370*0Sstevel@tonic-gate 	cal_sec -= pps_time.tv_sec;
1371*0Sstevel@tonic-gate 	cal_usec -= pps_time.tv_usec;
1372*0Sstevel@tonic-gate 	if (cal_usec < 0) {
1373*0Sstevel@tonic-gate 		cal_usec += MICROSEC;
1374*0Sstevel@tonic-gate 		cal_sec--;
1375*0Sstevel@tonic-gate 	}
1376*0Sstevel@tonic-gate 	pps_time = *tvp;
1377*0Sstevel@tonic-gate 
1378*0Sstevel@tonic-gate 	/*
1379*0Sstevel@tonic-gate 	 * Check for lost interrupts, noise, excessive jitter and
1380*0Sstevel@tonic-gate 	 * excessive frequency error. The number of timer ticks during
1381*0Sstevel@tonic-gate 	 * the interval may vary +-1 tick. Add to this a margin of one
1382*0Sstevel@tonic-gate 	 * tick for the PPS signal jitter and maximum frequency
1383*0Sstevel@tonic-gate 	 * deviation. If the limits are exceeded, the calibration
1384*0Sstevel@tonic-gate 	 * interval is reset to the minimum and we start over.
1385*0Sstevel@tonic-gate 	 */
1386*0Sstevel@tonic-gate 	u_usec = (int)usec_per_tick << 1;
1387*0Sstevel@tonic-gate 	if (!((cal_sec == -1 && cal_usec > (MICROSEC - u_usec)) ||
1388*0Sstevel@tonic-gate 	    (cal_sec == 0 && cal_usec < u_usec)) ||
1389*0Sstevel@tonic-gate 	    v_usec > time_tolerance || v_usec < -time_tolerance) {
1390*0Sstevel@tonic-gate 		pps_errcnt++;
1391*0Sstevel@tonic-gate 		pps_shift = PPS_SHIFT;
1392*0Sstevel@tonic-gate 		pps_intcnt = 0;
1393*0Sstevel@tonic-gate 		time_status |= STA_PPSERROR;
1394*0Sstevel@tonic-gate 		return;
1395*0Sstevel@tonic-gate 	}
1396*0Sstevel@tonic-gate 
1397*0Sstevel@tonic-gate 	/*
1398*0Sstevel@tonic-gate 	 * A three-stage median filter is used to help deglitch the pps
1399*0Sstevel@tonic-gate 	 * frequency. The median sample becomes the frequency offset
1400*0Sstevel@tonic-gate 	 * estimate; the difference between the other two samples
1401*0Sstevel@tonic-gate 	 * becomes the frequency dispersion (stability) estimate.
1402*0Sstevel@tonic-gate 	 */
1403*0Sstevel@tonic-gate 	pps_ff[2] = pps_ff[1];
1404*0Sstevel@tonic-gate 	pps_ff[1] = pps_ff[0];
1405*0Sstevel@tonic-gate 	pps_ff[0] = v_usec;
1406*0Sstevel@tonic-gate 	if (pps_ff[0] > pps_ff[1]) {
1407*0Sstevel@tonic-gate 		if (pps_ff[1] > pps_ff[2]) {
1408*0Sstevel@tonic-gate 			u_usec = pps_ff[1];		/* 0 1 2 */
1409*0Sstevel@tonic-gate 			v_usec = pps_ff[0] - pps_ff[2];
1410*0Sstevel@tonic-gate 		} else if (pps_ff[2] > pps_ff[0]) {
1411*0Sstevel@tonic-gate 			u_usec = pps_ff[0];		/* 2 0 1 */
1412*0Sstevel@tonic-gate 			v_usec = pps_ff[2] - pps_ff[1];
1413*0Sstevel@tonic-gate 		} else {
1414*0Sstevel@tonic-gate 			u_usec = pps_ff[2];		/* 0 2 1 */
1415*0Sstevel@tonic-gate 			v_usec = pps_ff[0] - pps_ff[1];
1416*0Sstevel@tonic-gate 		}
1417*0Sstevel@tonic-gate 	} else {
1418*0Sstevel@tonic-gate 		if (pps_ff[1] < pps_ff[2]) {
1419*0Sstevel@tonic-gate 			u_usec = pps_ff[1];		/* 2 1 0 */
1420*0Sstevel@tonic-gate 			v_usec = pps_ff[2] - pps_ff[0];
1421*0Sstevel@tonic-gate 		} else  if (pps_ff[2] < pps_ff[0]) {
1422*0Sstevel@tonic-gate 			u_usec = pps_ff[0];		/* 1 0 2 */
1423*0Sstevel@tonic-gate 			v_usec = pps_ff[1] - pps_ff[2];
1424*0Sstevel@tonic-gate 		} else {
1425*0Sstevel@tonic-gate 			u_usec = pps_ff[2];		/* 1 2 0 */
1426*0Sstevel@tonic-gate 			v_usec = pps_ff[1] - pps_ff[0];
1427*0Sstevel@tonic-gate 		}
1428*0Sstevel@tonic-gate 	}
1429*0Sstevel@tonic-gate 
1430*0Sstevel@tonic-gate 	/*
1431*0Sstevel@tonic-gate 	 * Here the frequency dispersion (stability) is updated. If it
1432*0Sstevel@tonic-gate 	 * is less than one-fourth the maximum (MAXFREQ), the frequency
1433*0Sstevel@tonic-gate 	 * offset is updated as well, but clamped to the tolerance. It
1434*0Sstevel@tonic-gate 	 * will be processed later by the clock() routine.
1435*0Sstevel@tonic-gate 	 */
1436*0Sstevel@tonic-gate 	v_usec = (v_usec >> 1) - pps_stabil;
1437*0Sstevel@tonic-gate 	if (v_usec < 0)
1438*0Sstevel@tonic-gate 		pps_stabil -= -v_usec >> PPS_AVG;
1439*0Sstevel@tonic-gate 	else
1440*0Sstevel@tonic-gate 		pps_stabil += v_usec >> PPS_AVG;
1441*0Sstevel@tonic-gate 	if (pps_stabil > MAXFREQ >> 2) {
1442*0Sstevel@tonic-gate 		pps_stbcnt++;
1443*0Sstevel@tonic-gate 		time_status |= STA_PPSWANDER;
1444*0Sstevel@tonic-gate 		return;
1445*0Sstevel@tonic-gate 	}
1446*0Sstevel@tonic-gate 	if (time_status & STA_PPSFREQ) {
1447*0Sstevel@tonic-gate 		if (u_usec < 0) {
1448*0Sstevel@tonic-gate 			pps_freq -= -u_usec >> PPS_AVG;
1449*0Sstevel@tonic-gate 			if (pps_freq < -time_tolerance)
1450*0Sstevel@tonic-gate 				pps_freq = -time_tolerance;
1451*0Sstevel@tonic-gate 			u_usec = -u_usec;
1452*0Sstevel@tonic-gate 		} else {
1453*0Sstevel@tonic-gate 			pps_freq += u_usec >> PPS_AVG;
1454*0Sstevel@tonic-gate 			if (pps_freq > time_tolerance)
1455*0Sstevel@tonic-gate 				pps_freq = time_tolerance;
1456*0Sstevel@tonic-gate 		}
1457*0Sstevel@tonic-gate 	}
1458*0Sstevel@tonic-gate 
1459*0Sstevel@tonic-gate 	/*
1460*0Sstevel@tonic-gate 	 * Here the calibration interval is adjusted. If the maximum
1461*0Sstevel@tonic-gate 	 * time difference is greater than tick / 4, reduce the interval
1462*0Sstevel@tonic-gate 	 * by half. If this is not the case for four consecutive
1463*0Sstevel@tonic-gate 	 * intervals, double the interval.
1464*0Sstevel@tonic-gate 	 */
1465*0Sstevel@tonic-gate 	if (u_usec << pps_shift > bigtick >> 2) {
1466*0Sstevel@tonic-gate 		pps_intcnt = 0;
1467*0Sstevel@tonic-gate 		if (pps_shift > PPS_SHIFT)
1468*0Sstevel@tonic-gate 			pps_shift--;
1469*0Sstevel@tonic-gate 	} else if (pps_intcnt >= 4) {
1470*0Sstevel@tonic-gate 		pps_intcnt = 0;
1471*0Sstevel@tonic-gate 		if (pps_shift < PPS_SHIFTMAX)
1472*0Sstevel@tonic-gate 			pps_shift++;
1473*0Sstevel@tonic-gate 	} else
1474*0Sstevel@tonic-gate 		pps_intcnt++;
1475*0Sstevel@tonic-gate 
1476*0Sstevel@tonic-gate 	/*
1477*0Sstevel@tonic-gate 	 * If recovering from kmdb, then make sure the tod chip gets resynced.
1478*0Sstevel@tonic-gate 	 * If we took an early exit above, then we don't yet have a stable
1479*0Sstevel@tonic-gate 	 * calibration signal to lock onto, so don't mark the tod for sync
1480*0Sstevel@tonic-gate 	 * until we get all the way here.
1481*0Sstevel@tonic-gate 	 */
1482*0Sstevel@tonic-gate 	{
1483*0Sstevel@tonic-gate 		int s = hr_clock_lock();
1484*0Sstevel@tonic-gate 
1485*0Sstevel@tonic-gate 		tod_needsync = 1;
1486*0Sstevel@tonic-gate 		hr_clock_unlock(s);
1487*0Sstevel@tonic-gate 	}
1488*0Sstevel@tonic-gate }
1489*0Sstevel@tonic-gate 
1490*0Sstevel@tonic-gate /*
1491*0Sstevel@tonic-gate  * Handle clock tick processing for a thread.
1492*0Sstevel@tonic-gate  * Check for timer action, enforce CPU rlimit, do profiling etc.
1493*0Sstevel@tonic-gate  */
1494*0Sstevel@tonic-gate void
1495*0Sstevel@tonic-gate clock_tick(kthread_t *t)
1496*0Sstevel@tonic-gate {
1497*0Sstevel@tonic-gate 	struct proc *pp;
1498*0Sstevel@tonic-gate 	klwp_id_t    lwp;
1499*0Sstevel@tonic-gate 	struct as *as;
1500*0Sstevel@tonic-gate 	clock_t	utime;
1501*0Sstevel@tonic-gate 	clock_t	stime;
1502*0Sstevel@tonic-gate 	int	poke = 0;		/* notify another CPU */
1503*0Sstevel@tonic-gate 	int	user_mode;
1504*0Sstevel@tonic-gate 	size_t	 rss;
1505*0Sstevel@tonic-gate 
1506*0Sstevel@tonic-gate 	/* Must be operating on a lwp/thread */
1507*0Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) == NULL) {
1508*0Sstevel@tonic-gate 		panic("clock_tick: no lwp");
1509*0Sstevel@tonic-gate 		/*NOTREACHED*/
1510*0Sstevel@tonic-gate 	}
1511*0Sstevel@tonic-gate 
1512*0Sstevel@tonic-gate 	CL_TICK(t);	/* Class specific tick processing */
1513*0Sstevel@tonic-gate 	DTRACE_SCHED1(tick, kthread_t *, t);
1514*0Sstevel@tonic-gate 
1515*0Sstevel@tonic-gate 	pp = ttoproc(t);
1516*0Sstevel@tonic-gate 
1517*0Sstevel@tonic-gate 	/* pp->p_lock makes sure that the thread does not exit */
1518*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pp->p_lock));
1519*0Sstevel@tonic-gate 
1520*0Sstevel@tonic-gate 	user_mode = (lwp->lwp_state == LWP_USER);
1521*0Sstevel@tonic-gate 
1522*0Sstevel@tonic-gate 	/*
1523*0Sstevel@tonic-gate 	 * Update process times. Should use high res clock and state
1524*0Sstevel@tonic-gate 	 * changes instead of statistical sampling method. XXX
1525*0Sstevel@tonic-gate 	 */
1526*0Sstevel@tonic-gate 	if (user_mode) {
1527*0Sstevel@tonic-gate 		pp->p_utime++;
1528*0Sstevel@tonic-gate 		pp->p_task->tk_cpu_time++;
1529*0Sstevel@tonic-gate 	} else {
1530*0Sstevel@tonic-gate 		pp->p_stime++;
1531*0Sstevel@tonic-gate 		pp->p_task->tk_cpu_time++;
1532*0Sstevel@tonic-gate 	}
1533*0Sstevel@tonic-gate 	as = pp->p_as;
1534*0Sstevel@tonic-gate 
1535*0Sstevel@tonic-gate 	/*
1536*0Sstevel@tonic-gate 	 * Update user profiling statistics. Get the pc from the
1537*0Sstevel@tonic-gate 	 * lwp when the AST happens.
1538*0Sstevel@tonic-gate 	 */
1539*0Sstevel@tonic-gate 	if (pp->p_prof.pr_scale) {
1540*0Sstevel@tonic-gate 		atomic_add_32(&lwp->lwp_oweupc, 1);
1541*0Sstevel@tonic-gate 		if (user_mode) {
1542*0Sstevel@tonic-gate 			poke = 1;
1543*0Sstevel@tonic-gate 			aston(t);
1544*0Sstevel@tonic-gate 		}
1545*0Sstevel@tonic-gate 	}
1546*0Sstevel@tonic-gate 
1547*0Sstevel@tonic-gate 	utime = pp->p_utime;
1548*0Sstevel@tonic-gate 	stime = pp->p_stime;
1549*0Sstevel@tonic-gate 
1550*0Sstevel@tonic-gate 	/*
1551*0Sstevel@tonic-gate 	 * If CPU was in user state, process lwp-virtual time
1552*0Sstevel@tonic-gate 	 * interval timer.
1553*0Sstevel@tonic-gate 	 */
1554*0Sstevel@tonic-gate 	if (user_mode &&
1555*0Sstevel@tonic-gate 	    timerisset(&lwp->lwp_timer[ITIMER_VIRTUAL].it_value) &&
1556*0Sstevel@tonic-gate 	    itimerdecr(&lwp->lwp_timer[ITIMER_VIRTUAL], usec_per_tick) == 0) {
1557*0Sstevel@tonic-gate 		poke = 1;
1558*0Sstevel@tonic-gate 		sigtoproc(pp, t, SIGVTALRM);
1559*0Sstevel@tonic-gate 	}
1560*0Sstevel@tonic-gate 
1561*0Sstevel@tonic-gate 	if (timerisset(&lwp->lwp_timer[ITIMER_PROF].it_value) &&
1562*0Sstevel@tonic-gate 	    itimerdecr(&lwp->lwp_timer[ITIMER_PROF], usec_per_tick) == 0) {
1563*0Sstevel@tonic-gate 		poke = 1;
1564*0Sstevel@tonic-gate 		sigtoproc(pp, t, SIGPROF);
1565*0Sstevel@tonic-gate 	}
1566*0Sstevel@tonic-gate 
1567*0Sstevel@tonic-gate 	/*
1568*0Sstevel@tonic-gate 	 * Enforce CPU resource controls:
1569*0Sstevel@tonic-gate 	 *   (a) process.max-cpu-time resource control
1570*0Sstevel@tonic-gate 	 */
1571*0Sstevel@tonic-gate 	(void) rctl_test(rctlproc_legacy[RLIMIT_CPU], pp->p_rctls, pp,
1572*0Sstevel@tonic-gate 	    (utime + stime)/hz, RCA_UNSAFE_SIGINFO);
1573*0Sstevel@tonic-gate 
1574*0Sstevel@tonic-gate 	/*
1575*0Sstevel@tonic-gate 	 *   (b) task.max-cpu-time resource control
1576*0Sstevel@tonic-gate 	 */
1577*0Sstevel@tonic-gate 	(void) rctl_test(rc_task_cpu_time, pp->p_task->tk_rctls, pp, 1,
1578*0Sstevel@tonic-gate 	    RCA_UNSAFE_SIGINFO);
1579*0Sstevel@tonic-gate 
1580*0Sstevel@tonic-gate 	/*
1581*0Sstevel@tonic-gate 	 * Update memory usage for the currently running process.
1582*0Sstevel@tonic-gate 	 */
1583*0Sstevel@tonic-gate 	rss = rm_asrss(as);
1584*0Sstevel@tonic-gate 	PTOU(pp)->u_mem += rss;
1585*0Sstevel@tonic-gate 	if (rss > PTOU(pp)->u_mem_max)
1586*0Sstevel@tonic-gate 		PTOU(pp)->u_mem_max = rss;
1587*0Sstevel@tonic-gate 
1588*0Sstevel@tonic-gate 	/*
1589*0Sstevel@tonic-gate 	 * Notify the CPU the thread is running on.
1590*0Sstevel@tonic-gate 	 */
1591*0Sstevel@tonic-gate 	if (poke && t->t_cpu != CPU)
1592*0Sstevel@tonic-gate 		poke_cpu(t->t_cpu->cpu_id);
1593*0Sstevel@tonic-gate }
1594*0Sstevel@tonic-gate 
1595*0Sstevel@tonic-gate void
1596*0Sstevel@tonic-gate profil_tick(uintptr_t upc)
1597*0Sstevel@tonic-gate {
1598*0Sstevel@tonic-gate 	int ticks;
1599*0Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1600*0Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
1601*0Sstevel@tonic-gate 	struct prof *pr = &p->p_prof;
1602*0Sstevel@tonic-gate 
1603*0Sstevel@tonic-gate 	do {
1604*0Sstevel@tonic-gate 		ticks = lwp->lwp_oweupc;
1605*0Sstevel@tonic-gate 	} while (cas32(&lwp->lwp_oweupc, ticks, 0) != ticks);
1606*0Sstevel@tonic-gate 
1607*0Sstevel@tonic-gate 	mutex_enter(&p->p_pflock);
1608*0Sstevel@tonic-gate 	if (pr->pr_scale >= 2 && upc >= pr->pr_off) {
1609*0Sstevel@tonic-gate 		/*
1610*0Sstevel@tonic-gate 		 * Old-style profiling
1611*0Sstevel@tonic-gate 		 */
1612*0Sstevel@tonic-gate 		uint16_t *slot = pr->pr_base;
1613*0Sstevel@tonic-gate 		uint16_t old, new;
1614*0Sstevel@tonic-gate 		if (pr->pr_scale != 2) {
1615*0Sstevel@tonic-gate 			uintptr_t delta = upc - pr->pr_off;
1616*0Sstevel@tonic-gate 			uintptr_t byteoff = ((delta >> 16) * pr->pr_scale) +
1617*0Sstevel@tonic-gate 			    (((delta & 0xffff) * pr->pr_scale) >> 16);
1618*0Sstevel@tonic-gate 			if (byteoff >= (uintptr_t)pr->pr_size) {
1619*0Sstevel@tonic-gate 				mutex_exit(&p->p_pflock);
1620*0Sstevel@tonic-gate 				return;
1621*0Sstevel@tonic-gate 			}
1622*0Sstevel@tonic-gate 			slot += byteoff / sizeof (uint16_t);
1623*0Sstevel@tonic-gate 		}
1624*0Sstevel@tonic-gate 		if (fuword16(slot, &old) < 0 ||
1625*0Sstevel@tonic-gate 		    (new = old + ticks) > SHRT_MAX ||
1626*0Sstevel@tonic-gate 		    suword16(slot, new) < 0) {
1627*0Sstevel@tonic-gate 			pr->pr_scale = 0;
1628*0Sstevel@tonic-gate 		}
1629*0Sstevel@tonic-gate 	} else if (pr->pr_scale == 1) {
1630*0Sstevel@tonic-gate 		/*
1631*0Sstevel@tonic-gate 		 * PC Sampling
1632*0Sstevel@tonic-gate 		 */
1633*0Sstevel@tonic-gate 		model_t model = lwp_getdatamodel(lwp);
1634*0Sstevel@tonic-gate 		int result;
1635*0Sstevel@tonic-gate #ifdef __lint
1636*0Sstevel@tonic-gate 		model = model;
1637*0Sstevel@tonic-gate #endif
1638*0Sstevel@tonic-gate 		while (ticks-- > 0) {
1639*0Sstevel@tonic-gate 			if (pr->pr_samples == pr->pr_size) {
1640*0Sstevel@tonic-gate 				/* buffer full, turn off sampling */
1641*0Sstevel@tonic-gate 				pr->pr_scale = 0;
1642*0Sstevel@tonic-gate 				break;
1643*0Sstevel@tonic-gate 			}
1644*0Sstevel@tonic-gate 			switch (SIZEOF_PTR(model)) {
1645*0Sstevel@tonic-gate 			case sizeof (uint32_t):
1646*0Sstevel@tonic-gate 				result = suword32(pr->pr_base, (uint32_t)upc);
1647*0Sstevel@tonic-gate 				break;
1648*0Sstevel@tonic-gate #ifdef _LP64
1649*0Sstevel@tonic-gate 			case sizeof (uint64_t):
1650*0Sstevel@tonic-gate 				result = suword64(pr->pr_base, (uint64_t)upc);
1651*0Sstevel@tonic-gate 				break;
1652*0Sstevel@tonic-gate #endif
1653*0Sstevel@tonic-gate 			default:
1654*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "profil_tick: unexpected "
1655*0Sstevel@tonic-gate 				    "data model");
1656*0Sstevel@tonic-gate 				result = -1;
1657*0Sstevel@tonic-gate 				break;
1658*0Sstevel@tonic-gate 			}
1659*0Sstevel@tonic-gate 			if (result != 0) {
1660*0Sstevel@tonic-gate 				pr->pr_scale = 0;
1661*0Sstevel@tonic-gate 				break;
1662*0Sstevel@tonic-gate 			}
1663*0Sstevel@tonic-gate 			pr->pr_base = (caddr_t)pr->pr_base + SIZEOF_PTR(model);
1664*0Sstevel@tonic-gate 			pr->pr_samples++;
1665*0Sstevel@tonic-gate 		}
1666*0Sstevel@tonic-gate 	}
1667*0Sstevel@tonic-gate 	mutex_exit(&p->p_pflock);
1668*0Sstevel@tonic-gate }
1669*0Sstevel@tonic-gate 
1670*0Sstevel@tonic-gate static void
1671*0Sstevel@tonic-gate delay_wakeup(void *arg)
1672*0Sstevel@tonic-gate {
1673*0Sstevel@tonic-gate 	kthread_t *t = arg;
1674*0Sstevel@tonic-gate 
1675*0Sstevel@tonic-gate 	mutex_enter(&t->t_delay_lock);
1676*0Sstevel@tonic-gate 	cv_signal(&t->t_delay_cv);
1677*0Sstevel@tonic-gate 	mutex_exit(&t->t_delay_lock);
1678*0Sstevel@tonic-gate }
1679*0Sstevel@tonic-gate 
1680*0Sstevel@tonic-gate void
1681*0Sstevel@tonic-gate delay(clock_t ticks)
1682*0Sstevel@tonic-gate {
1683*0Sstevel@tonic-gate 	kthread_t *t = curthread;
1684*0Sstevel@tonic-gate 	clock_t deadline = lbolt + ticks;
1685*0Sstevel@tonic-gate 	clock_t timeleft;
1686*0Sstevel@tonic-gate 	timeout_id_t id;
1687*0Sstevel@tonic-gate 
1688*0Sstevel@tonic-gate 	if (panicstr && ticks > 0) {
1689*0Sstevel@tonic-gate 		/*
1690*0Sstevel@tonic-gate 		 * Timeouts aren't running, so all we can do is spin.
1691*0Sstevel@tonic-gate 		 */
1692*0Sstevel@tonic-gate 		drv_usecwait(TICK_TO_USEC(ticks));
1693*0Sstevel@tonic-gate 		return;
1694*0Sstevel@tonic-gate 	}
1695*0Sstevel@tonic-gate 
1696*0Sstevel@tonic-gate 	while ((timeleft = deadline - lbolt) > 0) {
1697*0Sstevel@tonic-gate 		mutex_enter(&t->t_delay_lock);
1698*0Sstevel@tonic-gate 		id = timeout(delay_wakeup, t, timeleft);
1699*0Sstevel@tonic-gate 		cv_wait(&t->t_delay_cv, &t->t_delay_lock);
1700*0Sstevel@tonic-gate 		mutex_exit(&t->t_delay_lock);
1701*0Sstevel@tonic-gate 		(void) untimeout(id);
1702*0Sstevel@tonic-gate 	}
1703*0Sstevel@tonic-gate }
1704*0Sstevel@tonic-gate 
1705*0Sstevel@tonic-gate /*
1706*0Sstevel@tonic-gate  * Like delay, but interruptible by a signal.
1707*0Sstevel@tonic-gate  */
1708*0Sstevel@tonic-gate int
1709*0Sstevel@tonic-gate delay_sig(clock_t ticks)
1710*0Sstevel@tonic-gate {
1711*0Sstevel@tonic-gate 	clock_t deadline = lbolt + ticks;
1712*0Sstevel@tonic-gate 	clock_t rc;
1713*0Sstevel@tonic-gate 
1714*0Sstevel@tonic-gate 	mutex_enter(&curthread->t_delay_lock);
1715*0Sstevel@tonic-gate 	do {
1716*0Sstevel@tonic-gate 		rc = cv_timedwait_sig(&curthread->t_delay_cv,
1717*0Sstevel@tonic-gate 		    &curthread->t_delay_lock, deadline);
1718*0Sstevel@tonic-gate 	} while (rc > 0);
1719*0Sstevel@tonic-gate 	mutex_exit(&curthread->t_delay_lock);
1720*0Sstevel@tonic-gate 	if (rc == 0)
1721*0Sstevel@tonic-gate 		return (EINTR);
1722*0Sstevel@tonic-gate 	return (0);
1723*0Sstevel@tonic-gate }
1724*0Sstevel@tonic-gate 
1725*0Sstevel@tonic-gate #define	SECONDS_PER_DAY 86400
1726*0Sstevel@tonic-gate 
1727*0Sstevel@tonic-gate /*
1728*0Sstevel@tonic-gate  * Initialize the system time based on the TOD chip.  approx is used as
1729*0Sstevel@tonic-gate  * an approximation of time (e.g. from the filesystem) in the event that
1730*0Sstevel@tonic-gate  * the TOD chip has been cleared or is unresponsive.  An approx of -1
1731*0Sstevel@tonic-gate  * means the filesystem doesn't keep time.
1732*0Sstevel@tonic-gate  */
1733*0Sstevel@tonic-gate void
1734*0Sstevel@tonic-gate clkset(time_t approx)
1735*0Sstevel@tonic-gate {
1736*0Sstevel@tonic-gate 	timestruc_t ts;
1737*0Sstevel@tonic-gate 	int spl;
1738*0Sstevel@tonic-gate 	int set_clock = 0;
1739*0Sstevel@tonic-gate 
1740*0Sstevel@tonic-gate 	mutex_enter(&tod_lock);
1741*0Sstevel@tonic-gate 	ts = tod_get();
1742*0Sstevel@tonic-gate 
1743*0Sstevel@tonic-gate 	if (ts.tv_sec > 365 * SECONDS_PER_DAY) {
1744*0Sstevel@tonic-gate 		/*
1745*0Sstevel@tonic-gate 		 * If the TOD chip is reporting some time after 1971,
1746*0Sstevel@tonic-gate 		 * then it probably didn't lose power or become otherwise
1747*0Sstevel@tonic-gate 		 * cleared in the recent past;  check to assure that
1748*0Sstevel@tonic-gate 		 * the time coming from the filesystem isn't in the future
1749*0Sstevel@tonic-gate 		 * according to the TOD chip.
1750*0Sstevel@tonic-gate 		 */
1751*0Sstevel@tonic-gate 		if (approx != -1 && approx > ts.tv_sec) {
1752*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "Last shutdown is later "
1753*0Sstevel@tonic-gate 			    "than time on time-of-day chip; check date.");
1754*0Sstevel@tonic-gate 		}
1755*0Sstevel@tonic-gate 	} else {
1756*0Sstevel@tonic-gate 		/*
1757*0Sstevel@tonic-gate 		 * If the TOD chip isn't giving correct time, then set it to
1758*0Sstevel@tonic-gate 		 * the time that was passed in as a rough estimate.  If we
1759*0Sstevel@tonic-gate 		 * don't have an estimate, then set the clock back to a time
1760*0Sstevel@tonic-gate 		 * when Oliver North, ALF and Dire Straits were all on the
1761*0Sstevel@tonic-gate 		 * collective brain:  1987.
1762*0Sstevel@tonic-gate 		 */
1763*0Sstevel@tonic-gate 		timestruc_t tmp;
1764*0Sstevel@tonic-gate 		if (approx == -1)
1765*0Sstevel@tonic-gate 			ts.tv_sec = (1987 - 1970) * 365 * SECONDS_PER_DAY;
1766*0Sstevel@tonic-gate 		else
1767*0Sstevel@tonic-gate 			ts.tv_sec = approx;
1768*0Sstevel@tonic-gate 		ts.tv_nsec = 0;
1769*0Sstevel@tonic-gate 
1770*0Sstevel@tonic-gate 		/*
1771*0Sstevel@tonic-gate 		 * Attempt to write the new time to the TOD chip.  Set spl high
1772*0Sstevel@tonic-gate 		 * to avoid getting preempted between the tod_set and tod_get.
1773*0Sstevel@tonic-gate 		 */
1774*0Sstevel@tonic-gate 		spl = splhi();
1775*0Sstevel@tonic-gate 		tod_set(ts);
1776*0Sstevel@tonic-gate 		tmp = tod_get();
1777*0Sstevel@tonic-gate 		splx(spl);
1778*0Sstevel@tonic-gate 
1779*0Sstevel@tonic-gate 		if (tmp.tv_sec != ts.tv_sec && tmp.tv_sec != ts.tv_sec + 1) {
1780*0Sstevel@tonic-gate 			tod_broken = 1;
1781*0Sstevel@tonic-gate 			dosynctodr = 0;
1782*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "Time-of-day chip unresponsive;"
1783*0Sstevel@tonic-gate 			    " dead batteries?");
1784*0Sstevel@tonic-gate 		} else {
1785*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "Time-of-day chip had "
1786*0Sstevel@tonic-gate 			    "incorrect date; check and reset.");
1787*0Sstevel@tonic-gate 		}
1788*0Sstevel@tonic-gate 		set_clock = 1;
1789*0Sstevel@tonic-gate 	}
1790*0Sstevel@tonic-gate 
1791*0Sstevel@tonic-gate 	if (!boot_time) {
1792*0Sstevel@tonic-gate 		boot_time = ts.tv_sec;
1793*0Sstevel@tonic-gate 		set_clock = 1;
1794*0Sstevel@tonic-gate 	}
1795*0Sstevel@tonic-gate 
1796*0Sstevel@tonic-gate 	if (set_clock)
1797*0Sstevel@tonic-gate 		set_hrestime(&ts);
1798*0Sstevel@tonic-gate 
1799*0Sstevel@tonic-gate 	mutex_exit(&tod_lock);
1800*0Sstevel@tonic-gate }
1801*0Sstevel@tonic-gate 
1802*0Sstevel@tonic-gate int	timechanged;	/* for testing if the system time has been reset */
1803*0Sstevel@tonic-gate 
1804*0Sstevel@tonic-gate void
1805*0Sstevel@tonic-gate set_hrestime(timestruc_t *ts)
1806*0Sstevel@tonic-gate {
1807*0Sstevel@tonic-gate 	int spl = hr_clock_lock();
1808*0Sstevel@tonic-gate 	hrestime = *ts;
1809*0Sstevel@tonic-gate 	membar_enter();	/* hrestime must be visible before timechanged++ */
1810*0Sstevel@tonic-gate 	timedelta = 0;
1811*0Sstevel@tonic-gate 	timechanged++;
1812*0Sstevel@tonic-gate 	hr_clock_unlock(spl);
1813*0Sstevel@tonic-gate }
1814*0Sstevel@tonic-gate 
1815*0Sstevel@tonic-gate static uint_t deadman_seconds;
1816*0Sstevel@tonic-gate static uint32_t deadman_panics;
1817*0Sstevel@tonic-gate static int deadman_enabled = 0;
1818*0Sstevel@tonic-gate static int deadman_panic_timers = 1;
1819*0Sstevel@tonic-gate 
1820*0Sstevel@tonic-gate static void
1821*0Sstevel@tonic-gate deadman(void)
1822*0Sstevel@tonic-gate {
1823*0Sstevel@tonic-gate 	if (panicstr) {
1824*0Sstevel@tonic-gate 		/*
1825*0Sstevel@tonic-gate 		 * During panic, other CPUs besides the panic
1826*0Sstevel@tonic-gate 		 * master continue to handle cyclics and some other
1827*0Sstevel@tonic-gate 		 * interrupts.  The code below is intended to be
1828*0Sstevel@tonic-gate 		 * single threaded, so any CPU other than the master
1829*0Sstevel@tonic-gate 		 * must keep out.
1830*0Sstevel@tonic-gate 		 */
1831*0Sstevel@tonic-gate 		if (CPU->cpu_id != panic_cpu.cpu_id)
1832*0Sstevel@tonic-gate 			return;
1833*0Sstevel@tonic-gate 
1834*0Sstevel@tonic-gate 		/*
1835*0Sstevel@tonic-gate 		 * If we're panicking, the deadman cyclic continues to increase
1836*0Sstevel@tonic-gate 		 * lbolt in case the dump device driver relies on this for
1837*0Sstevel@tonic-gate 		 * timeouts.  Note that we rely on deadman() being invoked once
1838*0Sstevel@tonic-gate 		 * per second, and credit lbolt and lbolt64 with hz ticks each.
1839*0Sstevel@tonic-gate 		 */
1840*0Sstevel@tonic-gate 		lbolt += hz;
1841*0Sstevel@tonic-gate 		lbolt64 += hz;
1842*0Sstevel@tonic-gate 
1843*0Sstevel@tonic-gate 		if (!deadman_panic_timers)
1844*0Sstevel@tonic-gate 			return; /* allow all timers to be manually disabled */
1845*0Sstevel@tonic-gate 
1846*0Sstevel@tonic-gate 		/*
1847*0Sstevel@tonic-gate 		 * If we are generating a crash dump or syncing filesystems and
1848*0Sstevel@tonic-gate 		 * the corresponding timer is set, decrement it and re-enter
1849*0Sstevel@tonic-gate 		 * the panic code to abort it and advance to the next state.
1850*0Sstevel@tonic-gate 		 * The panic states and triggers are explained in panic.c.
1851*0Sstevel@tonic-gate 		 */
1852*0Sstevel@tonic-gate 		if (panic_dump) {
1853*0Sstevel@tonic-gate 			if (dump_timeleft && (--dump_timeleft == 0)) {
1854*0Sstevel@tonic-gate 				panic("panic dump timeout");
1855*0Sstevel@tonic-gate 				/*NOTREACHED*/
1856*0Sstevel@tonic-gate 			}
1857*0Sstevel@tonic-gate 		} else if (panic_sync) {
1858*0Sstevel@tonic-gate 			if (sync_timeleft && (--sync_timeleft == 0)) {
1859*0Sstevel@tonic-gate 				panic("panic sync timeout");
1860*0Sstevel@tonic-gate 				/*NOTREACHED*/
1861*0Sstevel@tonic-gate 			}
1862*0Sstevel@tonic-gate 		}
1863*0Sstevel@tonic-gate 
1864*0Sstevel@tonic-gate 		return;
1865*0Sstevel@tonic-gate 	}
1866*0Sstevel@tonic-gate 
1867*0Sstevel@tonic-gate 	if (lbolt != CPU->cpu_deadman_lbolt) {
1868*0Sstevel@tonic-gate 		CPU->cpu_deadman_lbolt = lbolt;
1869*0Sstevel@tonic-gate 		CPU->cpu_deadman_countdown = deadman_seconds;
1870*0Sstevel@tonic-gate 		return;
1871*0Sstevel@tonic-gate 	}
1872*0Sstevel@tonic-gate 
1873*0Sstevel@tonic-gate 	if (CPU->cpu_deadman_countdown-- > 0)
1874*0Sstevel@tonic-gate 		return;
1875*0Sstevel@tonic-gate 
1876*0Sstevel@tonic-gate 	/*
1877*0Sstevel@tonic-gate 	 * Regardless of whether or not we actually bring the system down,
1878*0Sstevel@tonic-gate 	 * bump the deadman_panics variable.
1879*0Sstevel@tonic-gate 	 *
1880*0Sstevel@tonic-gate 	 * N.B. deadman_panics is incremented once for each CPU that
1881*0Sstevel@tonic-gate 	 * passes through here.  It's expected that all the CPUs will
1882*0Sstevel@tonic-gate 	 * detect this condition within one second of each other, so
1883*0Sstevel@tonic-gate 	 * when deadman_enabled is off, deadman_panics will
1884*0Sstevel@tonic-gate 	 * typically be a multiple of the total number of CPUs in
1885*0Sstevel@tonic-gate 	 * the system.
1886*0Sstevel@tonic-gate 	 */
1887*0Sstevel@tonic-gate 	atomic_add_32(&deadman_panics, 1);
1888*0Sstevel@tonic-gate 
1889*0Sstevel@tonic-gate 	if (!deadman_enabled) {
1890*0Sstevel@tonic-gate 		CPU->cpu_deadman_countdown = deadman_seconds;
1891*0Sstevel@tonic-gate 		return;
1892*0Sstevel@tonic-gate 	}
1893*0Sstevel@tonic-gate 
1894*0Sstevel@tonic-gate 	/*
1895*0Sstevel@tonic-gate 	 * If we're here, we want to bring the system down.
1896*0Sstevel@tonic-gate 	 */
1897*0Sstevel@tonic-gate 	panic("deadman: timed out after %d seconds of clock "
1898*0Sstevel@tonic-gate 	    "inactivity", deadman_seconds);
1899*0Sstevel@tonic-gate 	/*NOTREACHED*/
1900*0Sstevel@tonic-gate }
1901*0Sstevel@tonic-gate 
1902*0Sstevel@tonic-gate /*ARGSUSED*/
1903*0Sstevel@tonic-gate static void
1904*0Sstevel@tonic-gate deadman_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
1905*0Sstevel@tonic-gate {
1906*0Sstevel@tonic-gate 	cpu->cpu_deadman_lbolt = 0;
1907*0Sstevel@tonic-gate 	cpu->cpu_deadman_countdown = deadman_seconds;
1908*0Sstevel@tonic-gate 
1909*0Sstevel@tonic-gate 	hdlr->cyh_func = (cyc_func_t)deadman;
1910*0Sstevel@tonic-gate 	hdlr->cyh_level = CY_HIGH_LEVEL;
1911*0Sstevel@tonic-gate 	hdlr->cyh_arg = NULL;
1912*0Sstevel@tonic-gate 
1913*0Sstevel@tonic-gate 	/*
1914*0Sstevel@tonic-gate 	 * Stagger the CPUs so that they don't all run deadman() at
1915*0Sstevel@tonic-gate 	 * the same time.  Simplest reason to do this is to make it
1916*0Sstevel@tonic-gate 	 * more likely that only one CPU will panic in case of a
1917*0Sstevel@tonic-gate 	 * timeout.  This is (strictly speaking) an aesthetic, not a
1918*0Sstevel@tonic-gate 	 * technical consideration.
1919*0Sstevel@tonic-gate 	 *
1920*0Sstevel@tonic-gate 	 * The interval must be one second in accordance with the
1921*0Sstevel@tonic-gate 	 * code in deadman() above to increase lbolt during panic.
1922*0Sstevel@tonic-gate 	 */
1923*0Sstevel@tonic-gate 	when->cyt_when = cpu->cpu_id * (NANOSEC / NCPU);
1924*0Sstevel@tonic-gate 	when->cyt_interval = NANOSEC;
1925*0Sstevel@tonic-gate }
1926*0Sstevel@tonic-gate 
1927*0Sstevel@tonic-gate 
1928*0Sstevel@tonic-gate void
1929*0Sstevel@tonic-gate deadman_init(void)
1930*0Sstevel@tonic-gate {
1931*0Sstevel@tonic-gate 	cyc_omni_handler_t hdlr;
1932*0Sstevel@tonic-gate 
1933*0Sstevel@tonic-gate 	if (deadman_seconds == 0)
1934*0Sstevel@tonic-gate 		deadman_seconds = snoop_interval / MICROSEC;
1935*0Sstevel@tonic-gate 
1936*0Sstevel@tonic-gate 	if (snooping)
1937*0Sstevel@tonic-gate 		deadman_enabled = 1;
1938*0Sstevel@tonic-gate 
1939*0Sstevel@tonic-gate 	hdlr.cyo_online = deadman_online;
1940*0Sstevel@tonic-gate 	hdlr.cyo_offline = NULL;
1941*0Sstevel@tonic-gate 	hdlr.cyo_arg = NULL;
1942*0Sstevel@tonic-gate 
1943*0Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1944*0Sstevel@tonic-gate 	deadman_cyclic = cyclic_add_omni(&hdlr);
1945*0Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1946*0Sstevel@tonic-gate }
1947*0Sstevel@tonic-gate 
1948*0Sstevel@tonic-gate /*
1949*0Sstevel@tonic-gate  * tod_fault() is for updating tod validate mechanism state:
1950*0Sstevel@tonic-gate  * (1) TOD_NOFAULT: for resetting the state to 'normal'.
1951*0Sstevel@tonic-gate  *     currently used for debugging only
1952*0Sstevel@tonic-gate  * (2) The following four cases detected by tod validate mechanism:
1953*0Sstevel@tonic-gate  *       TOD_REVERSED: current tod value is less than previous value.
1954*0Sstevel@tonic-gate  *       TOD_STALLED: current tod value hasn't advanced.
1955*0Sstevel@tonic-gate  *       TOD_JUMPED: current tod value advanced too far from previous value.
1956*0Sstevel@tonic-gate  *       TOD_RATECHANGED: the ratio between average tod delta and
1957*0Sstevel@tonic-gate  *       average tick delta has changed.
1958*0Sstevel@tonic-gate  */
1959*0Sstevel@tonic-gate enum tod_fault_type
1960*0Sstevel@tonic-gate tod_fault(enum tod_fault_type ftype, int off)
1961*0Sstevel@tonic-gate {
1962*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
1963*0Sstevel@tonic-gate 
1964*0Sstevel@tonic-gate 	if (tod_faulted != ftype) {
1965*0Sstevel@tonic-gate 		switch (ftype) {
1966*0Sstevel@tonic-gate 		case TOD_NOFAULT:
1967*0Sstevel@tonic-gate 			if (&plat_tod_fault)
1968*0Sstevel@tonic-gate 				plat_tod_fault(TOD_NOFAULT);
1969*0Sstevel@tonic-gate 			cmn_err(CE_NOTE, "Restarted tracking "
1970*0Sstevel@tonic-gate 					"Time of Day clock.");
1971*0Sstevel@tonic-gate 			tod_faulted = ftype;
1972*0Sstevel@tonic-gate 			break;
1973*0Sstevel@tonic-gate 		case TOD_REVERSED:
1974*0Sstevel@tonic-gate 		case TOD_JUMPED:
1975*0Sstevel@tonic-gate 			if (tod_faulted == TOD_NOFAULT) {
1976*0Sstevel@tonic-gate 				if (&plat_tod_fault)
1977*0Sstevel@tonic-gate 					plat_tod_fault(ftype);
1978*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "Time of Day clock error: "
1979*0Sstevel@tonic-gate 				    "reason [%s by 0x%x]. -- "
1980*0Sstevel@tonic-gate 				    " Stopped tracking Time Of Day clock.",
1981*0Sstevel@tonic-gate 				    tod_fault_table[ftype], off);
1982*0Sstevel@tonic-gate 				tod_faulted = ftype;
1983*0Sstevel@tonic-gate 			}
1984*0Sstevel@tonic-gate 			break;
1985*0Sstevel@tonic-gate 		case TOD_STALLED:
1986*0Sstevel@tonic-gate 		case TOD_RATECHANGED:
1987*0Sstevel@tonic-gate 			if (tod_faulted == TOD_NOFAULT) {
1988*0Sstevel@tonic-gate 				if (&plat_tod_fault)
1989*0Sstevel@tonic-gate 					plat_tod_fault(ftype);
1990*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "Time of Day clock error: "
1991*0Sstevel@tonic-gate 				    "reason [%s]. -- "
1992*0Sstevel@tonic-gate 				    " Stopped tracking Time Of Day clock.",
1993*0Sstevel@tonic-gate 				    tod_fault_table[ftype]);
1994*0Sstevel@tonic-gate 				tod_faulted = ftype;
1995*0Sstevel@tonic-gate 			}
1996*0Sstevel@tonic-gate 			break;
1997*0Sstevel@tonic-gate 		default:
1998*0Sstevel@tonic-gate 			break;
1999*0Sstevel@tonic-gate 		}
2000*0Sstevel@tonic-gate 	}
2001*0Sstevel@tonic-gate 	return (tod_faulted);
2002*0Sstevel@tonic-gate }
2003*0Sstevel@tonic-gate 
2004*0Sstevel@tonic-gate void
2005*0Sstevel@tonic-gate tod_fault_reset()
2006*0Sstevel@tonic-gate {
2007*0Sstevel@tonic-gate 	tod_fault_reset_flag = 1;
2008*0Sstevel@tonic-gate }
2009*0Sstevel@tonic-gate 
2010*0Sstevel@tonic-gate 
2011*0Sstevel@tonic-gate /*
2012*0Sstevel@tonic-gate  * tod_validate() is used for checking values returned by tod_get().
2013*0Sstevel@tonic-gate  * Four error cases can be detected by this routine:
2014*0Sstevel@tonic-gate  *   TOD_REVERSED: current tod value is less than previous.
2015*0Sstevel@tonic-gate  *   TOD_STALLED: current tod value hasn't advanced.
2016*0Sstevel@tonic-gate  *   TOD_JUMPED: current tod value advanced too far from previous value.
2017*0Sstevel@tonic-gate  *   TOD_RATECHANGED: the ratio between average tod delta and
2018*0Sstevel@tonic-gate  *   average tick delta has changed.
2019*0Sstevel@tonic-gate  */
2020*0Sstevel@tonic-gate time_t
2021*0Sstevel@tonic-gate tod_validate(time_t tod)
2022*0Sstevel@tonic-gate {
2023*0Sstevel@tonic-gate 	time_t diff_tod;
2024*0Sstevel@tonic-gate 	hrtime_t diff_tick;
2025*0Sstevel@tonic-gate 
2026*0Sstevel@tonic-gate 	long dtick;
2027*0Sstevel@tonic-gate 	int dtick_delta;
2028*0Sstevel@tonic-gate 
2029*0Sstevel@tonic-gate 	int off = 0;
2030*0Sstevel@tonic-gate 	enum tod_fault_type tod_bad = TOD_NOFAULT;
2031*0Sstevel@tonic-gate 
2032*0Sstevel@tonic-gate 	static int firsttime = 1;
2033*0Sstevel@tonic-gate 
2034*0Sstevel@tonic-gate 	static time_t prev_tod = 0;
2035*0Sstevel@tonic-gate 	static hrtime_t prev_tick = 0;
2036*0Sstevel@tonic-gate 	static long dtick_avg = TOD_REF_FREQ;
2037*0Sstevel@tonic-gate 
2038*0Sstevel@tonic-gate 	hrtime_t tick = gethrtime();
2039*0Sstevel@tonic-gate 
2040*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
2041*0Sstevel@tonic-gate 
2042*0Sstevel@tonic-gate 	/*
2043*0Sstevel@tonic-gate 	 * tod_validate_enable is patchable via /etc/system.
2044*0Sstevel@tonic-gate 	 * If TOD is already faulted, there is nothing to do
2045*0Sstevel@tonic-gate 	 */
2046*0Sstevel@tonic-gate 	if ((tod_validate_enable == 0) || (tod_faulted != TOD_NOFAULT)) {
2047*0Sstevel@tonic-gate 		return (tod);
2048*0Sstevel@tonic-gate 	}
2049*0Sstevel@tonic-gate 
2050*0Sstevel@tonic-gate 	/*
2051*0Sstevel@tonic-gate 	 * Update prev_tod and prev_tick values for first run
2052*0Sstevel@tonic-gate 	 */
2053*0Sstevel@tonic-gate 	if (firsttime) {
2054*0Sstevel@tonic-gate 		firsttime = 0;
2055*0Sstevel@tonic-gate 		prev_tod = tod;
2056*0Sstevel@tonic-gate 		prev_tick = tick;
2057*0Sstevel@tonic-gate 		return (tod);
2058*0Sstevel@tonic-gate 	}
2059*0Sstevel@tonic-gate 
2060*0Sstevel@tonic-gate 	/*
2061*0Sstevel@tonic-gate 	 * For either of these conditions, we need to reset ourself
2062*0Sstevel@tonic-gate 	 * and start validation from zero since each condition
2063*0Sstevel@tonic-gate 	 * indicates that the TOD will be updated with new value
2064*0Sstevel@tonic-gate 	 * Also, note that tod_needsync will be reset in clock()
2065*0Sstevel@tonic-gate 	 */
2066*0Sstevel@tonic-gate 	if (tod_needsync || tod_fault_reset_flag) {
2067*0Sstevel@tonic-gate 		firsttime = 1;
2068*0Sstevel@tonic-gate 		prev_tod = 0;
2069*0Sstevel@tonic-gate 		prev_tick = 0;
2070*0Sstevel@tonic-gate 		dtick_avg = TOD_REF_FREQ;
2071*0Sstevel@tonic-gate 
2072*0Sstevel@tonic-gate 		if (tod_fault_reset_flag)
2073*0Sstevel@tonic-gate 			tod_fault_reset_flag = 0;
2074*0Sstevel@tonic-gate 
2075*0Sstevel@tonic-gate 		return (tod);
2076*0Sstevel@tonic-gate 	}
2077*0Sstevel@tonic-gate 
2078*0Sstevel@tonic-gate 	/* test hook */
2079*0Sstevel@tonic-gate 	switch (tod_unit_test) {
2080*0Sstevel@tonic-gate 	case 1: /* for testing jumping tod */
2081*0Sstevel@tonic-gate 		tod += tod_test_injector;
2082*0Sstevel@tonic-gate 		tod_unit_test = 0;
2083*0Sstevel@tonic-gate 		break;
2084*0Sstevel@tonic-gate 	case 2:	/* for testing stuck tod bit */
2085*0Sstevel@tonic-gate 		tod |= 1 << tod_test_injector;
2086*0Sstevel@tonic-gate 		tod_unit_test = 0;
2087*0Sstevel@tonic-gate 		break;
2088*0Sstevel@tonic-gate 	case 3:	/* for testing stalled tod */
2089*0Sstevel@tonic-gate 		tod = prev_tod;
2090*0Sstevel@tonic-gate 		tod_unit_test = 0;
2091*0Sstevel@tonic-gate 		break;
2092*0Sstevel@tonic-gate 	case 4:	/* reset tod fault status */
2093*0Sstevel@tonic-gate 		(void) tod_fault(TOD_NOFAULT, 0);
2094*0Sstevel@tonic-gate 		tod_unit_test = 0;
2095*0Sstevel@tonic-gate 		break;
2096*0Sstevel@tonic-gate 	default:
2097*0Sstevel@tonic-gate 		break;
2098*0Sstevel@tonic-gate 	}
2099*0Sstevel@tonic-gate 
2100*0Sstevel@tonic-gate 	diff_tod = tod - prev_tod;
2101*0Sstevel@tonic-gate 	diff_tick = tick - prev_tick;
2102*0Sstevel@tonic-gate 
2103*0Sstevel@tonic-gate 	ASSERT(diff_tick >= 0);
2104*0Sstevel@tonic-gate 
2105*0Sstevel@tonic-gate 	if (diff_tod < 0) {
2106*0Sstevel@tonic-gate 		/* ERROR - tod reversed */
2107*0Sstevel@tonic-gate 		tod_bad = TOD_REVERSED;
2108*0Sstevel@tonic-gate 		off = (int)(prev_tod - tod);
2109*0Sstevel@tonic-gate 	} else if (diff_tod == 0) {
2110*0Sstevel@tonic-gate 		/* tod did not advance */
2111*0Sstevel@tonic-gate 		if (diff_tick > TOD_STALL_THRESHOLD) {
2112*0Sstevel@tonic-gate 			/* ERROR - tod stalled */
2113*0Sstevel@tonic-gate 			tod_bad = TOD_STALLED;
2114*0Sstevel@tonic-gate 		} else {
2115*0Sstevel@tonic-gate 			/*
2116*0Sstevel@tonic-gate 			 * Make sure we don't update prev_tick
2117*0Sstevel@tonic-gate 			 * so that diff_tick is calculated since
2118*0Sstevel@tonic-gate 			 * the first diff_tod == 0
2119*0Sstevel@tonic-gate 			 */
2120*0Sstevel@tonic-gate 			return (tod);
2121*0Sstevel@tonic-gate 		}
2122*0Sstevel@tonic-gate 	} else {
2123*0Sstevel@tonic-gate 		/* calculate dtick */
2124*0Sstevel@tonic-gate 		dtick = diff_tick / diff_tod;
2125*0Sstevel@tonic-gate 
2126*0Sstevel@tonic-gate 		/* update dtick averages */
2127*0Sstevel@tonic-gate 		dtick_avg += ((dtick - dtick_avg) / TOD_FILTER_N);
2128*0Sstevel@tonic-gate 
2129*0Sstevel@tonic-gate 		/*
2130*0Sstevel@tonic-gate 		 * Calculate dtick_delta as
2131*0Sstevel@tonic-gate 		 * variation from reference freq in quartiles
2132*0Sstevel@tonic-gate 		 */
2133*0Sstevel@tonic-gate 		dtick_delta = (dtick_avg - TOD_REF_FREQ) /
2134*0Sstevel@tonic-gate 			(TOD_REF_FREQ >> 2);
2135*0Sstevel@tonic-gate 
2136*0Sstevel@tonic-gate 		/*
2137*0Sstevel@tonic-gate 		 * Even with a perfectly functioning TOD device,
2138*0Sstevel@tonic-gate 		 * when the number of elapsed seconds is low the
2139*0Sstevel@tonic-gate 		 * algorithm can calculate a rate that is beyond
2140*0Sstevel@tonic-gate 		 * tolerance, causing an error.  The algorithm is
2141*0Sstevel@tonic-gate 		 * inaccurate when elapsed time is low (less than
2142*0Sstevel@tonic-gate 		 * 5 seconds).
2143*0Sstevel@tonic-gate 		 */
2144*0Sstevel@tonic-gate 		if (diff_tod > 4) {
2145*0Sstevel@tonic-gate 			if (dtick < TOD_JUMP_THRESHOLD) {
2146*0Sstevel@tonic-gate 				/* ERROR - tod jumped */
2147*0Sstevel@tonic-gate 				tod_bad = TOD_JUMPED;
2148*0Sstevel@tonic-gate 				off = (int)diff_tod;
2149*0Sstevel@tonic-gate 			} else if (dtick_delta) {
2150*0Sstevel@tonic-gate 				/* ERROR - change in clock rate */
2151*0Sstevel@tonic-gate 				tod_bad = TOD_RATECHANGED;
2152*0Sstevel@tonic-gate 			}
2153*0Sstevel@tonic-gate 		}
2154*0Sstevel@tonic-gate 	}
2155*0Sstevel@tonic-gate 
2156*0Sstevel@tonic-gate 	if (tod_bad != TOD_NOFAULT) {
2157*0Sstevel@tonic-gate 		(void) tod_fault(tod_bad, off);
2158*0Sstevel@tonic-gate 
2159*0Sstevel@tonic-gate 		/*
2160*0Sstevel@tonic-gate 		 * Disable dosynctodr since we are going to fault
2161*0Sstevel@tonic-gate 		 * the TOD chip anyway here
2162*0Sstevel@tonic-gate 		 */
2163*0Sstevel@tonic-gate 		dosynctodr = 0;
2164*0Sstevel@tonic-gate 
2165*0Sstevel@tonic-gate 		/*
2166*0Sstevel@tonic-gate 		 * Set tod to the correct value from hrestime
2167*0Sstevel@tonic-gate 		 */
2168*0Sstevel@tonic-gate 		tod = hrestime.tv_sec;
2169*0Sstevel@tonic-gate 	}
2170*0Sstevel@tonic-gate 
2171*0Sstevel@tonic-gate 	prev_tod = tod;
2172*0Sstevel@tonic-gate 	prev_tick = tick;
2173*0Sstevel@tonic-gate 	return (tod);
2174*0Sstevel@tonic-gate }
2175*0Sstevel@tonic-gate 
2176*0Sstevel@tonic-gate static void
2177*0Sstevel@tonic-gate calcloadavg(int nrun, uint64_t *hp_ave)
2178*0Sstevel@tonic-gate {
2179*0Sstevel@tonic-gate 	static int64_t f[3] = { 135, 27, 9 };
2180*0Sstevel@tonic-gate 	uint_t i;
2181*0Sstevel@tonic-gate 	int64_t q, r;
2182*0Sstevel@tonic-gate 
2183*0Sstevel@tonic-gate 	/*
2184*0Sstevel@tonic-gate 	 * Compute load average over the last 1, 5, and 15 minutes
2185*0Sstevel@tonic-gate 	 * (60, 300, and 900 seconds).  The constants in f[3] are for
2186*0Sstevel@tonic-gate 	 * exponential decay:
2187*0Sstevel@tonic-gate 	 * (1 - exp(-1/60)) << 13 = 135,
2188*0Sstevel@tonic-gate 	 * (1 - exp(-1/300)) << 13 = 27,
2189*0Sstevel@tonic-gate 	 * (1 - exp(-1/900)) << 13 = 9.
2190*0Sstevel@tonic-gate 	 */
2191*0Sstevel@tonic-gate 
2192*0Sstevel@tonic-gate 	/*
2193*0Sstevel@tonic-gate 	 * a little hoop-jumping to avoid integer overflow
2194*0Sstevel@tonic-gate 	 */
2195*0Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
2196*0Sstevel@tonic-gate 		q = (hp_ave[i]  >> 16) << 7;
2197*0Sstevel@tonic-gate 		r = (hp_ave[i]  & 0xffff) << 7;
2198*0Sstevel@tonic-gate 		hp_ave[i] += ((nrun - q) * f[i] - ((r * f[i]) >> 16)) >> 4;
2199*0Sstevel@tonic-gate 	}
2200*0Sstevel@tonic-gate }
2201