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