xref: /illumos-gate/usr/src/uts/common/os/clock.c (revision 9a1586db)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53c431bb5Swentaoy  * Common Development and Distribution License (the "License").
63c431bb5Swentaoy  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
25777222b7SRafael Vanoni  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
26a288e5a9SJoshua M. Clulow  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
273f11de9dSSara Hartse  * Copyright (c) 2016 by Delphix. All rights reserved.
28*9a1586dbSAndy Fiddaman  * Copyright 2023 Oxide Computer Company
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/tuneable.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
387c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/proc.h>
417c478bd9Sstevel@tonic-gate #include <sys/callo.h>
427c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
437c478bd9Sstevel@tonic-gate #include <sys/var.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/swap.h>
467c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
477c478bd9Sstevel@tonic-gate #include <sys/class.h>
487c478bd9Sstevel@tonic-gate #include <sys/time.h>
497c478bd9Sstevel@tonic-gate #include <sys/debug.h>
507c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
517c478bd9Sstevel@tonic-gate #include <sys/spl.h>
527c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
537c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
547c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
557c478bd9Sstevel@tonic-gate #include <sys/fs/swapnode.h>
567c478bd9Sstevel@tonic-gate #include <sys/panic.h>
577c478bd9Sstevel@tonic-gate #include <sys/disp.h>
587c478bd9Sstevel@tonic-gate #include <sys/msacct.h>
597c478bd9Sstevel@tonic-gate #include <sys/mem_cage.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <vm/page.h>
627c478bd9Sstevel@tonic-gate #include <vm/anon.h>
637c478bd9Sstevel@tonic-gate #include <vm/rm.h>
647c478bd9Sstevel@tonic-gate #include <sys/cyclic.h>
657c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
667c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
677c478bd9Sstevel@tonic-gate #include <sys/task.h>
687c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
69a288e5a9SJoshua M. Clulow #include <sys/ddi_periodic.h>
704c06356bSdh #include <sys/random.h>
714c06356bSdh #include <sys/modctl.h>
7281d43577SJerry Jelinek #include <sys/zone.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * for NTP support
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate #include <sys/timex.h>
787c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
797c478bd9Sstevel@tonic-gate 
80d3d50737SRafael Vanoni #include <sys/sunddi.h>
81d3d50737SRafael Vanoni #include <sys/clock_impl.h>
82d3d50737SRafael Vanoni 
837c478bd9Sstevel@tonic-gate /*
84c97ad5cdSakolb  * clock() is called straight from the clock cyclic; see clock_init().
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  * Functions:
877c478bd9Sstevel@tonic-gate  *	reprime clock
887c478bd9Sstevel@tonic-gate  *	maintain date
897c478bd9Sstevel@tonic-gate  *	jab the scheduler
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate extern kcondvar_t	fsflush_cv;
937c478bd9Sstevel@tonic-gate extern sysinfo_t	sysinfo;
947c478bd9Sstevel@tonic-gate extern vminfo_t	vminfo;
957c478bd9Sstevel@tonic-gate extern int	idleswtch;	/* flag set while idle in pswtch() */
964c06356bSdh extern hrtime_t volatile devinfo_freeze;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * high-precision avenrun values.  These are needed to make the
1007c478bd9Sstevel@tonic-gate  * regular avenrun values accurate.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate static uint64_t hp_avenrun[3];
1037c478bd9Sstevel@tonic-gate int	avenrun[3];		/* FSCALED average run queue lengths */
1047c478bd9Sstevel@tonic-gate time_t	time;	/* time in seconds since 1970 - for compatibility only */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static struct loadavg_s loadavg;
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Phase/frequency-lock loop (PLL/FLL) definitions
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * The following variables are read and set by the ntp_adjtime() system
1117c478bd9Sstevel@tonic-gate  * call.
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * time_state shows the state of the system clock, with values defined
1147c478bd9Sstevel@tonic-gate  * in the timex.h header file.
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  * time_status shows the status of the system clock, with bits defined
1177c478bd9Sstevel@tonic-gate  * in the timex.h header file.
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * time_offset is used by the PLL/FLL to adjust the system time in small
1207c478bd9Sstevel@tonic-gate  * increments.
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  * time_constant determines the bandwidth or "stiffness" of the PLL.
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  * time_tolerance determines maximum frequency error or tolerance of the
1257c478bd9Sstevel@tonic-gate  * CPU clock oscillator and is a property of the architecture; however,
1267c478bd9Sstevel@tonic-gate  * in principle it could change as result of the presence of external
1277c478bd9Sstevel@tonic-gate  * discipline signals, for instance.
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  * time_precision is usually equal to the kernel tick variable; however,
1307c478bd9Sstevel@tonic-gate  * in cases where a precision clock counter or external clock is
1317c478bd9Sstevel@tonic-gate  * available, the resolution can be much less than this and depend on
1327c478bd9Sstevel@tonic-gate  * whether the external clock is working or not.
1337c478bd9Sstevel@tonic-gate  *
1347c478bd9Sstevel@tonic-gate  * time_maxerror is initialized by a ntp_adjtime() call and increased by
1357c478bd9Sstevel@tonic-gate  * the kernel once each second to reflect the maximum error bound
1367c478bd9Sstevel@tonic-gate  * growth.
1377c478bd9Sstevel@tonic-gate  *
1387c478bd9Sstevel@tonic-gate  * time_esterror is set and read by the ntp_adjtime() call, but
1397c478bd9Sstevel@tonic-gate  * otherwise not used by the kernel.
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate int32_t time_state = TIME_OK;	/* clock state */
1427c478bd9Sstevel@tonic-gate int32_t time_status = STA_UNSYNC;	/* clock status bits */
1437c478bd9Sstevel@tonic-gate int32_t time_offset = 0;		/* time offset (us) */
1447c478bd9Sstevel@tonic-gate int32_t time_constant = 0;		/* pll time constant */
1457c478bd9Sstevel@tonic-gate int32_t time_tolerance = MAXFREQ;	/* frequency tolerance (scaled ppm) */
1467c478bd9Sstevel@tonic-gate int32_t time_precision = 1;	/* clock precision (us) */
1477c478bd9Sstevel@tonic-gate int32_t time_maxerror = MAXPHASE;	/* maximum error (us) */
1487c478bd9Sstevel@tonic-gate int32_t time_esterror = MAXPHASE;	/* estimated error (us) */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * The following variables establish the state of the PLL/FLL and the
1527c478bd9Sstevel@tonic-gate  * residual time and frequency offset of the local clock. The scale
1537c478bd9Sstevel@tonic-gate  * factors are defined in the timex.h header file.
1547c478bd9Sstevel@tonic-gate  *
1557c478bd9Sstevel@tonic-gate  * time_phase and time_freq are the phase increment and the frequency
1567c478bd9Sstevel@tonic-gate  * increment, respectively, of the kernel time variable.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * time_freq is set via ntp_adjtime() from a value stored in a file when
1597c478bd9Sstevel@tonic-gate  * the synchronization daemon is first started. Its value is retrieved
1607c478bd9Sstevel@tonic-gate  * via ntp_adjtime() and written to the file about once per hour by the
1617c478bd9Sstevel@tonic-gate  * daemon.
1627c478bd9Sstevel@tonic-gate  *
1637c478bd9Sstevel@tonic-gate  * time_adj is the adjustment added to the value of tick at each timer
1647c478bd9Sstevel@tonic-gate  * interrupt and is recomputed from time_phase and time_freq at each
1657c478bd9Sstevel@tonic-gate  * seconds rollover.
1667c478bd9Sstevel@tonic-gate  *
1677c478bd9Sstevel@tonic-gate  * time_reftime is the second's portion of the system time at the last
1687c478bd9Sstevel@tonic-gate  * call to ntp_adjtime(). It is used to adjust the time_freq variable
1697c478bd9Sstevel@tonic-gate  * and to increase the time_maxerror as the time since last update
1707c478bd9Sstevel@tonic-gate  * increases.
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate int32_t time_phase = 0;		/* phase offset (scaled us) */
1737c478bd9Sstevel@tonic-gate int32_t time_freq = 0;		/* frequency offset (scaled ppm) */
1747c478bd9Sstevel@tonic-gate int32_t time_adj = 0;		/* tick adjust (scaled 1 / hz) */
1757c478bd9Sstevel@tonic-gate int32_t time_reftime = 0;		/* time at last adjustment (s) */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * The scale factors of the following variables are defined in the
1797c478bd9Sstevel@tonic-gate  * timex.h header file.
1807c478bd9Sstevel@tonic-gate  *
1817c478bd9Sstevel@tonic-gate  * pps_time contains the time at each calibration interval, as read by
1827c478bd9Sstevel@tonic-gate  * microtime(). pps_count counts the seconds of the calibration
1837c478bd9Sstevel@tonic-gate  * interval, the duration of which is nominally pps_shift in powers of
1847c478bd9Sstevel@tonic-gate  * two.
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  * pps_offset is the time offset produced by the time median filter
1877c478bd9Sstevel@tonic-gate  * pps_tf[], while pps_jitter is the dispersion (jitter) measured by
1887c478bd9Sstevel@tonic-gate  * this filter.
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  * pps_freq is the frequency offset produced by the frequency median
1917c478bd9Sstevel@tonic-gate  * filter pps_ff[], while pps_stabil is the dispersion (wander) measured
1927c478bd9Sstevel@tonic-gate  * by this filter.
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  * pps_usec is latched from a high resolution counter or external clock
1957c478bd9Sstevel@tonic-gate  * at pps_time. Here we want the hardware counter contents only, not the
1967c478bd9Sstevel@tonic-gate  * contents plus the time_tv.usec as usual.
1977c478bd9Sstevel@tonic-gate  *
1987c478bd9Sstevel@tonic-gate  * pps_valid counts the number of seconds since the last PPS update. It
1997c478bd9Sstevel@tonic-gate  * is used as a watchdog timer to disable the PPS discipline should the
2007c478bd9Sstevel@tonic-gate  * PPS signal be lost.
2017c478bd9Sstevel@tonic-gate  *
2027c478bd9Sstevel@tonic-gate  * pps_glitch counts the number of seconds since the beginning of an
2037c478bd9Sstevel@tonic-gate  * offset burst more than tick/2 from current nominal offset. It is used
2047c478bd9Sstevel@tonic-gate  * mainly to suppress error bursts due to priority conflicts between the
2057c478bd9Sstevel@tonic-gate  * PPS interrupt and timer interrupt.
2067c478bd9Sstevel@tonic-gate  *
2077c478bd9Sstevel@tonic-gate  * pps_intcnt counts the calibration intervals for use in the interval-
2087c478bd9Sstevel@tonic-gate  * adaptation algorithm. It's just too complicated for words.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate struct timeval pps_time;	/* kernel time at last interval */
2117c478bd9Sstevel@tonic-gate int32_t pps_tf[] = {0, 0, 0};	/* pps time offset median filter (us) */
2127c478bd9Sstevel@tonic-gate int32_t pps_offset = 0;		/* pps time offset (us) */
2137c478bd9Sstevel@tonic-gate int32_t pps_jitter = MAXTIME;	/* time dispersion (jitter) (us) */
2147c478bd9Sstevel@tonic-gate int32_t pps_ff[] = {0, 0, 0};	/* pps frequency offset median filter */
2157c478bd9Sstevel@tonic-gate int32_t pps_freq = 0;		/* frequency offset (scaled ppm) */
2167c478bd9Sstevel@tonic-gate int32_t pps_stabil = MAXFREQ;	/* frequency dispersion (scaled ppm) */
2177c478bd9Sstevel@tonic-gate int32_t pps_usec = 0;		/* microsec counter at last interval */
2187c478bd9Sstevel@tonic-gate int32_t pps_valid = PPS_VALID;	/* pps signal watchdog counter */
2197c478bd9Sstevel@tonic-gate int32_t pps_glitch = 0;		/* pps signal glitch counter */
2207c478bd9Sstevel@tonic-gate int32_t pps_count = 0;		/* calibration interval counter (s) */
2217c478bd9Sstevel@tonic-gate int32_t pps_shift = PPS_SHIFT;	/* interval duration (s) (shift) */
2227c478bd9Sstevel@tonic-gate int32_t pps_intcnt = 0;		/* intervals at current duration */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * PPS signal quality monitors
2267c478bd9Sstevel@tonic-gate  *
2277c478bd9Sstevel@tonic-gate  * pps_jitcnt counts the seconds that have been discarded because the
2287c478bd9Sstevel@tonic-gate  * jitter measured by the time median filter exceeds the limit MAXTIME
2297c478bd9Sstevel@tonic-gate  * (100 us).
2307c478bd9Sstevel@tonic-gate  *
2317c478bd9Sstevel@tonic-gate  * pps_calcnt counts the frequency calibration intervals, which are
2327c478bd9Sstevel@tonic-gate  * variable from 4 s to 256 s.
2337c478bd9Sstevel@tonic-gate  *
2347c478bd9Sstevel@tonic-gate  * pps_errcnt counts the calibration intervals which have been discarded
2357c478bd9Sstevel@tonic-gate  * because the wander exceeds the limit MAXFREQ (100 ppm) or where the
2367c478bd9Sstevel@tonic-gate  * calibration interval jitter exceeds two ticks.
2377c478bd9Sstevel@tonic-gate  *
2387c478bd9Sstevel@tonic-gate  * pps_stbcnt counts the calibration intervals that have been discarded
2397c478bd9Sstevel@tonic-gate  * because the frequency wander exceeds the limit MAXFREQ / 4 (25 us).
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate int32_t pps_jitcnt = 0;		/* jitter limit exceeded */
2427c478bd9Sstevel@tonic-gate int32_t pps_calcnt = 0;		/* calibration intervals */
2437c478bd9Sstevel@tonic-gate int32_t pps_errcnt = 0;		/* calibration errors */
2447c478bd9Sstevel@tonic-gate int32_t pps_stbcnt = 0;		/* stability limit exceeded */
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate kcondvar_t lbolt_cv;
247d3d50737SRafael Vanoni 
248d3d50737SRafael Vanoni /*
249d3d50737SRafael Vanoni  * Hybrid lbolt implementation:
250d3d50737SRafael Vanoni  *
251d3d50737SRafael Vanoni  * The service historically provided by the lbolt and lbolt64 variables has
252d3d50737SRafael Vanoni  * been replaced by the ddi_get_lbolt() and ddi_get_lbolt64() routines, and the
253d3d50737SRafael Vanoni  * original symbols removed from the system. The once clock driven variables are
254d3d50737SRafael Vanoni  * now implemented in an event driven fashion, backed by gethrtime() coarsed to
255d3d50737SRafael Vanoni  * the appropriate clock resolution. The default event driven implementation is
256d3d50737SRafael Vanoni  * complemented by a cyclic driven one, active only during periods of intense
257d3d50737SRafael Vanoni  * activity around the DDI lbolt routines, when a lbolt specific cyclic is
258d3d50737SRafael Vanoni  * reprogramed to fire at a clock tick interval to serve consumers of lbolt who
259d3d50737SRafael Vanoni  * rely on the original low cost of consulting a memory position.
260d3d50737SRafael Vanoni  *
261d3d50737SRafael Vanoni  * The implementation uses the number of calls to these routines and the
262d3d50737SRafael Vanoni  * frequency of these to determine when to transition from event to cyclic
263d3d50737SRafael Vanoni  * driven and vice-versa. These values are kept on a per CPU basis for
264d3d50737SRafael Vanoni  * scalability reasons and to prevent CPUs from constantly invalidating a single
265d3d50737SRafael Vanoni  * cache line when modifying a global variable. The transition from event to
266d3d50737SRafael Vanoni  * cyclic mode happens once the thresholds are crossed, and activity on any CPU
267d3d50737SRafael Vanoni  * can cause such transition.
268d3d50737SRafael Vanoni  *
269d3d50737SRafael Vanoni  * The lbolt_hybrid function pointer is called by ddi_get_lbolt() and
270d3d50737SRafael Vanoni  * ddi_get_lbolt64(), and will point to lbolt_event_driven() or
271d3d50737SRafael Vanoni  * lbolt_cyclic_driven() according to the current mode. When the thresholds
272d3d50737SRafael Vanoni  * are exceeded, lbolt_event_driven() will reprogram the lbolt cyclic to
273d3d50737SRafael Vanoni  * fire at a nsec_per_tick interval and increment an internal variable at
274d3d50737SRafael Vanoni  * each firing. lbolt_hybrid will then point to lbolt_cyclic_driven(), which
275d3d50737SRafael Vanoni  * will simply return the value of such variable. lbolt_cyclic() will attempt
276d3d50737SRafael Vanoni  * to shut itself off at each threshold interval (sampling period for calls
277d3d50737SRafael Vanoni  * to the DDI lbolt routines), and return to the event driven mode, but will
278d3d50737SRafael Vanoni  * be prevented from doing so if lbolt_cyclic_driven() is being heavily used.
279d3d50737SRafael Vanoni  *
280d3d50737SRafael Vanoni  * lbolt_bootstrap is used during boot to serve lbolt consumers who don't wait
281d3d50737SRafael Vanoni  * for the cyclic subsystem to be intialized.
282d3d50737SRafael Vanoni  *
283d3d50737SRafael Vanoni  */
28483e866b9SRafael Vanoni int64_t lbolt_bootstrap(void);
285d3d50737SRafael Vanoni int64_t lbolt_event_driven(void);
286d3d50737SRafael Vanoni int64_t lbolt_cyclic_driven(void);
287d3d50737SRafael Vanoni int64_t (*lbolt_hybrid)(void) = lbolt_bootstrap;
288d3d50737SRafael Vanoni uint_t lbolt_ev_to_cyclic(caddr_t, caddr_t);
289d3d50737SRafael Vanoni 
290d3d50737SRafael Vanoni /*
291d3d50737SRafael Vanoni  * lbolt's cyclic, installed by clock_init().
292d3d50737SRafael Vanoni  */
293d3d50737SRafael Vanoni static void lbolt_cyclic(void);
294d3d50737SRafael Vanoni 
295d3d50737SRafael Vanoni /*
296d3d50737SRafael Vanoni  * Tunable to keep lbolt in cyclic driven mode. This will prevent the system
297d3d50737SRafael Vanoni  * from switching back to event driven, once it reaches cyclic mode.
298d3d50737SRafael Vanoni  */
299d3d50737SRafael Vanoni static boolean_t lbolt_cyc_only = B_FALSE;
300d3d50737SRafael Vanoni 
301d3d50737SRafael Vanoni /*
302d3d50737SRafael Vanoni  * Cache aligned, per CPU structure with lbolt usage statistics.
303d3d50737SRafael Vanoni  */
304d3d50737SRafael Vanoni static lbolt_cpu_t *lb_cpu;
305d3d50737SRafael Vanoni 
306d3d50737SRafael Vanoni /*
307d3d50737SRafael Vanoni  * Single, cache aligned, structure with all the information required by
308d3d50737SRafael Vanoni  * the lbolt implementation.
309d3d50737SRafael Vanoni  */
310d3d50737SRafael Vanoni lbolt_info_t *lb_info;
311d3d50737SRafael Vanoni 
312d3d50737SRafael Vanoni 
3137c478bd9Sstevel@tonic-gate int one_sec = 1; /* turned on once every second */
3147c478bd9Sstevel@tonic-gate static int fsflushcnt;	/* counter for t_fsflushr */
3157c478bd9Sstevel@tonic-gate int	dosynctodr = 1;	/* patchable; enable/disable sync to TOD chip */
3167c478bd9Sstevel@tonic-gate int	tod_needsync = 0;	/* need to sync tod chip with software time */
3177c478bd9Sstevel@tonic-gate static int tod_broken = 0;	/* clock chip doesn't work */
3187c478bd9Sstevel@tonic-gate time_t	boot_time = 0;		/* Boot time in seconds since 1970 */
3197c478bd9Sstevel@tonic-gate cyclic_id_t clock_cyclic;	/* clock()'s cyclic_id */
3207c478bd9Sstevel@tonic-gate cyclic_id_t deadman_cyclic;	/* deadman()'s cyclic_id */
3217c478bd9Sstevel@tonic-gate 
322727737b4SJoshua M. Clulow extern void clock_tick_schedule(int);
323727737b4SJoshua M. Clulow extern void set_freemem(void);
324727737b4SJoshua M. Clulow extern void pageout_deadman(void);
3252850d85bSmv 
3267c478bd9Sstevel@tonic-gate static int lgrp_ticks;		/* counter to schedule lgrp load calcs */
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * for tod fault detection
3307c478bd9Sstevel@tonic-gate  */
3317c478bd9Sstevel@tonic-gate #define	TOD_REF_FREQ		((longlong_t)(NANOSEC))
3327c478bd9Sstevel@tonic-gate #define	TOD_STALL_THRESHOLD	(TOD_REF_FREQ * 3 / 2)
3337c478bd9Sstevel@tonic-gate #define	TOD_JUMP_THRESHOLD	(TOD_REF_FREQ / 2)
3347c478bd9Sstevel@tonic-gate #define	TOD_FILTER_N		4
3357c478bd9Sstevel@tonic-gate #define	TOD_FILTER_SETTLE	(4 * TOD_FILTER_N)
3364cde9a1eSJosef 'Jeff' Sipek static enum tod_fault_type tod_faulted = TOD_NOFAULT;
3378fc99e42STrevor Thompson 
3388fc99e42STrevor Thompson static int tod_status_flag = 0;		/* used by tod_validate() */
3398fc99e42STrevor Thompson 
3408fc99e42STrevor Thompson static hrtime_t prev_set_tick = 0;	/* gethrtime() prior to tod_set() */
3418fc99e42STrevor Thompson static time_t prev_set_tod = 0;		/* tv_sec value passed to tod_set() */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /* patchable via /etc/system */
3447c478bd9Sstevel@tonic-gate int tod_validate_enable = 1;
3457c478bd9Sstevel@tonic-gate 
3464c06356bSdh /* Diagnose/Limit messages about delay(9F) called from interrupt context */
3474c06356bSdh int			delay_from_interrupt_diagnose = 0;
3484c06356bSdh volatile uint32_t	delay_from_interrupt_msg = 20;
3494c06356bSdh 
3502c891a87Ssethg /*
3512c891a87Ssethg  * On non-SPARC systems, TOD validation must be deferred until gethrtime
3522c891a87Ssethg  * returns non-zero values (after mach_clkinit's execution).
3532c891a87Ssethg  * On SPARC systems, it must be deferred until after hrtime_base
3542c891a87Ssethg  * and hres_last_tick are set (in the first invocation of hres_tick).
3552c891a87Ssethg  * Since in both cases the prerequisites occur before the invocation of
3562c891a87Ssethg  * tod_get() in clock(), the deferment is lifted there.
3572c891a87Ssethg  */
3582c891a87Ssethg static boolean_t tod_validate_deferred = B_TRUE;
3592c891a87Ssethg 
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate  * tod_fault_table[] must be aligned with
3627c478bd9Sstevel@tonic-gate  * enum tod_fault_type in systm.h
3637c478bd9Sstevel@tonic-gate  */
3647c478bd9Sstevel@tonic-gate static char *tod_fault_table[] = {
3657c478bd9Sstevel@tonic-gate 	"Reversed",			/* TOD_REVERSED */
3667c478bd9Sstevel@tonic-gate 	"Stalled",			/* TOD_STALLED */
3677c478bd9Sstevel@tonic-gate 	"Jumped",			/* TOD_JUMPED */
368843e1988Sjohnlev 	"Changed in Clock Rate",	/* TOD_RATECHANGED */
369843e1988Sjohnlev 	"Is Read-Only"			/* TOD_RDONLY */
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * no strings needed for TOD_NOFAULT
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate };
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate  * test hook for tod broken detection in tod_validate
3777c478bd9Sstevel@tonic-gate  */
3787c478bd9Sstevel@tonic-gate int tod_unit_test = 0;
3797c478bd9Sstevel@tonic-gate time_t tod_test_injector;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate #define	CLOCK_ADJ_HIST_SIZE	4
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate static int	adj_hist_entry;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate int64_t clock_adj_hist[CLOCK_ADJ_HIST_SIZE];
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate static void calcloadavg(int, uint64_t *);
3887c478bd9Sstevel@tonic-gate static int genloadavg(struct loadavg_s *);
3897c478bd9Sstevel@tonic-gate static void loadavg_update();
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate void (*cmm_clock_callout)() = NULL;
392c97ad5cdSakolb void (*cpucaps_clock_callout)() = NULL;
3937c478bd9Sstevel@tonic-gate 
3942850d85bSmv extern clock_t clock_tick_proc_max;
3952850d85bSmv 
396d3d50737SRafael Vanoni static int64_t deadman_counter = 0;
397d3d50737SRafael Vanoni 
3987c478bd9Sstevel@tonic-gate static void
clock(void)3997c478bd9Sstevel@tonic-gate clock(void)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	kthread_t	*t;
4022850d85bSmv 	uint_t	nrunnable;
4037c478bd9Sstevel@tonic-gate 	uint_t	w_io;
4047c478bd9Sstevel@tonic-gate 	cpu_t	*cp;
4057c478bd9Sstevel@tonic-gate 	cpupart_t *cpupart;
4067c478bd9Sstevel@tonic-gate 	void	(*funcp)();
4077c478bd9Sstevel@tonic-gate 	int32_t ltemp;
4087c478bd9Sstevel@tonic-gate 	int64_t lltemp;
4097c478bd9Sstevel@tonic-gate 	int s;
4107c478bd9Sstevel@tonic-gate 	int do_lgrp_load;
4117c478bd9Sstevel@tonic-gate 	int i;
412d3d50737SRafael Vanoni 	clock_t now = LBOLT_NO_ACCOUNT;	/* current tick */
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (panicstr)
4157c478bd9Sstevel@tonic-gate 		return;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * Make sure that 'freemem' do not drift too far from the truth
4197c478bd9Sstevel@tonic-gate 	 */
4207c478bd9Sstevel@tonic-gate 	set_freemem();
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	/*
4247c478bd9Sstevel@tonic-gate 	 * Before the section which is repeated is executed, we do
4257c478bd9Sstevel@tonic-gate 	 * the time delta processing which occurs every clock tick
4267c478bd9Sstevel@tonic-gate 	 *
4277c478bd9Sstevel@tonic-gate 	 * There is additional processing which happens every time
4287c478bd9Sstevel@tonic-gate 	 * the nanosecond counter rolls over which is described
4297c478bd9Sstevel@tonic-gate 	 * below - see the section which begins with : if (one_sec)
4307c478bd9Sstevel@tonic-gate 	 *
4317c478bd9Sstevel@tonic-gate 	 * This section marks the beginning of the precision-kernel
4327c478bd9Sstevel@tonic-gate 	 * code fragment.
4337c478bd9Sstevel@tonic-gate 	 *
4347c478bd9Sstevel@tonic-gate 	 * First, compute the phase adjustment. If the low-order bits
4357c478bd9Sstevel@tonic-gate 	 * (time_phase) of the update overflow, bump the higher order
4367c478bd9Sstevel@tonic-gate 	 * bits (time_update).
4377c478bd9Sstevel@tonic-gate 	 */
4387c478bd9Sstevel@tonic-gate 	time_phase += time_adj;
4397c478bd9Sstevel@tonic-gate 	if (time_phase <= -FINEUSEC) {
4407c478bd9Sstevel@tonic-gate 		ltemp = -time_phase / SCALE_PHASE;
4417c478bd9Sstevel@tonic-gate 		time_phase += ltemp * SCALE_PHASE;
4427c478bd9Sstevel@tonic-gate 		s = hr_clock_lock();
4437c478bd9Sstevel@tonic-gate 		timedelta -= ltemp * (NANOSEC/MICROSEC);
4447c478bd9Sstevel@tonic-gate 		hr_clock_unlock(s);
4457c478bd9Sstevel@tonic-gate 	} else if (time_phase >= FINEUSEC) {
4467c478bd9Sstevel@tonic-gate 		ltemp = time_phase / SCALE_PHASE;
4477c478bd9Sstevel@tonic-gate 		time_phase -= ltemp * SCALE_PHASE;
4487c478bd9Sstevel@tonic-gate 		s = hr_clock_lock();
4497c478bd9Sstevel@tonic-gate 		timedelta += ltemp * (NANOSEC/MICROSEC);
4507c478bd9Sstevel@tonic-gate 		hr_clock_unlock(s);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/*
4547c478bd9Sstevel@tonic-gate 	 * End of precision-kernel code fragment which is processed
4557c478bd9Sstevel@tonic-gate 	 * every timer interrupt.
4567c478bd9Sstevel@tonic-gate 	 *
4577c478bd9Sstevel@tonic-gate 	 * Continue with the interrupt processing as scheduled.
4587c478bd9Sstevel@tonic-gate 	 */
4597c478bd9Sstevel@tonic-gate 	/*
4607c478bd9Sstevel@tonic-gate 	 * Count the number of runnable threads and the number waiting
4617c478bd9Sstevel@tonic-gate 	 * for some form of I/O to complete -- gets added to
4627c478bd9Sstevel@tonic-gate 	 * sysinfo.waiting.  To know the state of the system, must add
4637c478bd9Sstevel@tonic-gate 	 * wait counts from all CPUs.  Also add up the per-partition
4647c478bd9Sstevel@tonic-gate 	 * statistics.
4657c478bd9Sstevel@tonic-gate 	 */
4667c478bd9Sstevel@tonic-gate 	w_io = 0;
4677c478bd9Sstevel@tonic-gate 	nrunnable = 0;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * keep track of when to update lgrp/part loads
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	do_lgrp_load = 0;
4747c478bd9Sstevel@tonic-gate 	if (lgrp_ticks++ >= hz / 10) {
4757c478bd9Sstevel@tonic-gate 		lgrp_ticks = 0;
4767c478bd9Sstevel@tonic-gate 		do_lgrp_load = 1;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
479d3d50737SRafael Vanoni 	if (one_sec) {
4807c478bd9Sstevel@tonic-gate 		loadavg_update();
481d3d50737SRafael Vanoni 		deadman_counter++;
482727737b4SJoshua M. Clulow 		pageout_deadman();
483d3d50737SRafael Vanoni 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * First count the threads waiting on kpreempt queues in each
4877c478bd9Sstevel@tonic-gate 	 * CPU partition.
4887c478bd9Sstevel@tonic-gate 	 */
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	cpupart = cp_list_head;
4917c478bd9Sstevel@tonic-gate 	do {
4927c478bd9Sstevel@tonic-gate 		uint_t cpupart_nrunnable = cpupart->cp_kp_queue.disp_nrunnable;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		cpupart->cp_updates++;
4957c478bd9Sstevel@tonic-gate 		nrunnable += cpupart_nrunnable;
4967c478bd9Sstevel@tonic-gate 		cpupart->cp_nrunnable_cum += cpupart_nrunnable;
4977c478bd9Sstevel@tonic-gate 		if (one_sec) {
4987c478bd9Sstevel@tonic-gate 			cpupart->cp_nrunning = 0;
4997c478bd9Sstevel@tonic-gate 			cpupart->cp_nrunnable = cpupart_nrunnable;
5007c478bd9Sstevel@tonic-gate 		}
5017c478bd9Sstevel@tonic-gate 	} while ((cpupart = cpupart->cp_next) != cp_list_head);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/* Now count the per-CPU statistics. */
5057c478bd9Sstevel@tonic-gate 	cp = cpu_list;
5067c478bd9Sstevel@tonic-gate 	do {
5077c478bd9Sstevel@tonic-gate 		uint_t cpu_nrunnable = cp->cpu_disp->disp_nrunnable;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		nrunnable += cpu_nrunnable;
5107c478bd9Sstevel@tonic-gate 		cpupart = cp->cpu_part;
5117c478bd9Sstevel@tonic-gate 		cpupart->cp_nrunnable_cum += cpu_nrunnable;
512ae115bc7Smrj 		if (one_sec) {
5137c478bd9Sstevel@tonic-gate 			cpupart->cp_nrunnable += cpu_nrunnable;
5142850d85bSmv 			/*
5152850d85bSmv 			 * Update user, system, and idle cpu times.
5162850d85bSmv 			 */
5172850d85bSmv 			cpupart->cp_nrunning++;
518ae115bc7Smrj 			/*
519ae115bc7Smrj 			 * w_io is used to update sysinfo.waiting during
520ae115bc7Smrj 			 * one_second processing below.  Only gather w_io
521ae115bc7Smrj 			 * information when we walk the list of cpus if we're
522ae115bc7Smrj 			 * going to perform one_second processing.
523ae115bc7Smrj 			 */
524ae115bc7Smrj 			w_io += CPU_STATS(cp, sys.iowait);
5253aedfe0bSmishra 		}
5263aedfe0bSmishra 
5273aedfe0bSmishra 		if (one_sec && (cp->cpu_flags & CPU_EXISTS)) {
5283aedfe0bSmishra 			int i, load, change;
5293aedfe0bSmishra 			hrtime_t intracct, intrused;
5303aedfe0bSmishra 			const hrtime_t maxnsec = 1000000000;
5313aedfe0bSmishra 			const int precision = 100;
5323aedfe0bSmishra 
5333aedfe0bSmishra 			/*
5343aedfe0bSmishra 			 * Estimate interrupt load on this cpu each second.
5353aedfe0bSmishra 			 * Computes cpu_intrload as %utilization (0-99).
5363aedfe0bSmishra 			 */
537ae115bc7Smrj 
5383aedfe0bSmishra 			/* add up interrupt time from all micro states */
5393aedfe0bSmishra 			for (intracct = 0, i = 0; i < NCMSTATES; i++)
5403aedfe0bSmishra 				intracct += cp->cpu_intracct[i];
5413aedfe0bSmishra 			scalehrtime(&intracct);
5423aedfe0bSmishra 
5433aedfe0bSmishra 			/* compute nsec used in the past second */
5443aedfe0bSmishra 			intrused = intracct - cp->cpu_intrlast;
5453aedfe0bSmishra 			cp->cpu_intrlast = intracct;
5463aedfe0bSmishra 
5473aedfe0bSmishra 			/* limit the value for safety (and the first pass) */
5483aedfe0bSmishra 			if (intrused >= maxnsec)
5493aedfe0bSmishra 				intrused = maxnsec - 1;
5503aedfe0bSmishra 
5513aedfe0bSmishra 			/* calculate %time in interrupt */
5523aedfe0bSmishra 			load = (precision * intrused) / maxnsec;
5533aedfe0bSmishra 			ASSERT(load >= 0 && load < precision);
5543aedfe0bSmishra 			change = cp->cpu_intrload - load;
5553aedfe0bSmishra 
5563aedfe0bSmishra 			/* jump to new max, or decay the old max */
5573aedfe0bSmishra 			if (change < 0)
5583aedfe0bSmishra 				cp->cpu_intrload = load;
5593aedfe0bSmishra 			else if (change > 0)
5603aedfe0bSmishra 				cp->cpu_intrload -= (change + 3) / 4;
5613aedfe0bSmishra 
5623aedfe0bSmishra 			DTRACE_PROBE3(cpu_intrload,
5633aedfe0bSmishra 			    cpu_t *, cp,
5643aedfe0bSmishra 			    hrtime_t, intracct,
5653aedfe0bSmishra 			    hrtime_t, intrused);
566ae115bc7Smrj 		}
5673aedfe0bSmishra 
5687c478bd9Sstevel@tonic-gate 		if (do_lgrp_load &&
5697c478bd9Sstevel@tonic-gate 		    (cp->cpu_flags & CPU_EXISTS)) {
5707c478bd9Sstevel@tonic-gate 			/*
5717c478bd9Sstevel@tonic-gate 			 * When updating the lgroup's load average,
5727c478bd9Sstevel@tonic-gate 			 * account for the thread running on the CPU.
5737c478bd9Sstevel@tonic-gate 			 * If the CPU is the current one, then we need
5747c478bd9Sstevel@tonic-gate 			 * to account for the underlying thread which
5757c478bd9Sstevel@tonic-gate 			 * got the clock interrupt not the thread that is
5767c478bd9Sstevel@tonic-gate 			 * handling the interrupt and caculating the load
5777c478bd9Sstevel@tonic-gate 			 * average
5787c478bd9Sstevel@tonic-gate 			 */
5797c478bd9Sstevel@tonic-gate 			t = cp->cpu_thread;
5807c478bd9Sstevel@tonic-gate 			if (CPU == cp)
5817c478bd9Sstevel@tonic-gate 				t = t->t_intr;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 			/*
5847c478bd9Sstevel@tonic-gate 			 * Account for the load average for this thread if
5857c478bd9Sstevel@tonic-gate 			 * it isn't the idle thread or it is on the interrupt
5867c478bd9Sstevel@tonic-gate 			 * stack and not the current CPU handling the clock
5877c478bd9Sstevel@tonic-gate 			 * interrupt
5887c478bd9Sstevel@tonic-gate 			 */
5897c478bd9Sstevel@tonic-gate 			if ((t && t != cp->cpu_idle_thread) || (CPU != cp &&
5907c478bd9Sstevel@tonic-gate 			    CPU_ON_INTR(cp))) {
5917c478bd9Sstevel@tonic-gate 				if (t->t_lpl == cp->cpu_lpl) {
5927c478bd9Sstevel@tonic-gate 					/* local thread */
5937c478bd9Sstevel@tonic-gate 					cpu_nrunnable++;
5947c478bd9Sstevel@tonic-gate 				} else {
5957c478bd9Sstevel@tonic-gate 					/*
5967c478bd9Sstevel@tonic-gate 					 * This is a remote thread, charge it
5977c478bd9Sstevel@tonic-gate 					 * against its home lgroup.  Note that
5987c478bd9Sstevel@tonic-gate 					 * we notice that a thread is remote
5997c478bd9Sstevel@tonic-gate 					 * only if it's currently executing.
6007c478bd9Sstevel@tonic-gate 					 * This is a reasonable approximation,
6017c478bd9Sstevel@tonic-gate 					 * since queued remote threads are rare.
6027c478bd9Sstevel@tonic-gate 					 * Note also that if we didn't charge
6037c478bd9Sstevel@tonic-gate 					 * it to its home lgroup, remote
6047c478bd9Sstevel@tonic-gate 					 * execution would often make a system
6057c478bd9Sstevel@tonic-gate 					 * appear balanced even though it was
6067c478bd9Sstevel@tonic-gate 					 * not, and thread placement/migration
6077c478bd9Sstevel@tonic-gate 					 * would often not be done correctly.
6087c478bd9Sstevel@tonic-gate 					 */
6097c478bd9Sstevel@tonic-gate 					lgrp_loadavg(t->t_lpl,
6107c478bd9Sstevel@tonic-gate 					    LGRP_LOADAVG_IN_THREAD_MAX, 0);
6117c478bd9Sstevel@tonic-gate 				}
6127c478bd9Sstevel@tonic-gate 			}
6137c478bd9Sstevel@tonic-gate 			lgrp_loadavg(cp->cpu_lpl,
6147c478bd9Sstevel@tonic-gate 			    cpu_nrunnable * LGRP_LOADAVG_IN_THREAD_MAX, 1);
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
6177c478bd9Sstevel@tonic-gate 
6182850d85bSmv 	clock_tick_schedule(one_sec);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/*
6217c478bd9Sstevel@tonic-gate 	 * Check for a callout that needs be called from the clock
6227c478bd9Sstevel@tonic-gate 	 * thread to support the membership protocol in a clustered
6237c478bd9Sstevel@tonic-gate 	 * system.  Copy the function pointer so that we can reset
6247c478bd9Sstevel@tonic-gate 	 * this to NULL if needed.
6257c478bd9Sstevel@tonic-gate 	 */
6267c478bd9Sstevel@tonic-gate 	if ((funcp = cmm_clock_callout) != NULL)
6277c478bd9Sstevel@tonic-gate 		(*funcp)();
6287c478bd9Sstevel@tonic-gate 
629c97ad5cdSakolb 	if ((funcp = cpucaps_clock_callout) != NULL)
630c97ad5cdSakolb 		(*funcp)();
631c97ad5cdSakolb 
6327c478bd9Sstevel@tonic-gate 	/*
6337c478bd9Sstevel@tonic-gate 	 * Wakeup the cageout thread waiters once per second.
6347c478bd9Sstevel@tonic-gate 	 */
635d94ffb28Sjmcp 	if (one_sec)
636d94ffb28Sjmcp 		kcage_tick();
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	if (one_sec) {
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		int drift, absdrift;
6417c478bd9Sstevel@tonic-gate 		timestruc_t tod;
6427c478bd9Sstevel@tonic-gate 		int s;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		/*
6457c478bd9Sstevel@tonic-gate 		 * Beginning of precision-kernel code fragment executed
6467c478bd9Sstevel@tonic-gate 		 * every second.
6477c478bd9Sstevel@tonic-gate 		 *
6487c478bd9Sstevel@tonic-gate 		 * On rollover of the second the phase adjustment to be
6497c478bd9Sstevel@tonic-gate 		 * used for the next second is calculated.  Also, the
6507c478bd9Sstevel@tonic-gate 		 * maximum error is increased by the tolerance.  If the
6517c478bd9Sstevel@tonic-gate 		 * PPS frequency discipline code is present, the phase is
6527c478bd9Sstevel@tonic-gate 		 * increased to compensate for the CPU clock oscillator
6537c478bd9Sstevel@tonic-gate 		 * frequency error.
6547c478bd9Sstevel@tonic-gate 		 *
6557c478bd9Sstevel@tonic-gate 		 * On a 32-bit machine and given parameters in the timex.h
6567c478bd9Sstevel@tonic-gate 		 * header file, the maximum phase adjustment is +-512 ms
6577c478bd9Sstevel@tonic-gate 		 * and maximum frequency offset is (a tad less than)
6587c478bd9Sstevel@tonic-gate 		 * +-512 ppm. On a 64-bit machine, you shouldn't need to ask.
6597c478bd9Sstevel@tonic-gate 		 */
6607c478bd9Sstevel@tonic-gate 		time_maxerror += time_tolerance / SCALE_USEC;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		/*
6637c478bd9Sstevel@tonic-gate 		 * Leap second processing. If in leap-insert state at
6647c478bd9Sstevel@tonic-gate 		 * the end of the day, the system clock is set back one
6657c478bd9Sstevel@tonic-gate 		 * second; if in leap-delete state, the system clock is
6667c478bd9Sstevel@tonic-gate 		 * set ahead one second. The microtime() routine or
6677c478bd9Sstevel@tonic-gate 		 * external clock driver will insure that reported time
6687c478bd9Sstevel@tonic-gate 		 * is always monotonic. The ugly divides should be
6697c478bd9Sstevel@tonic-gate 		 * replaced.
6707c478bd9Sstevel@tonic-gate 		 */
6717c478bd9Sstevel@tonic-gate 		switch (time_state) {
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		case TIME_OK:
6747c478bd9Sstevel@tonic-gate 			if (time_status & STA_INS)
6757c478bd9Sstevel@tonic-gate 				time_state = TIME_INS;
6767c478bd9Sstevel@tonic-gate 			else if (time_status & STA_DEL)
6777c478bd9Sstevel@tonic-gate 				time_state = TIME_DEL;
6787c478bd9Sstevel@tonic-gate 			break;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		case TIME_INS:
6817c478bd9Sstevel@tonic-gate 			if (hrestime.tv_sec % 86400 == 0) {
6827c478bd9Sstevel@tonic-gate 				s = hr_clock_lock();
6837c478bd9Sstevel@tonic-gate 				hrestime.tv_sec--;
6847c478bd9Sstevel@tonic-gate 				hr_clock_unlock(s);
6857c478bd9Sstevel@tonic-gate 				time_state = TIME_OOP;
6867c478bd9Sstevel@tonic-gate 			}
6877c478bd9Sstevel@tonic-gate 			break;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		case TIME_DEL:
6907c478bd9Sstevel@tonic-gate 			if ((hrestime.tv_sec + 1) % 86400 == 0) {
6917c478bd9Sstevel@tonic-gate 				s = hr_clock_lock();
6927c478bd9Sstevel@tonic-gate 				hrestime.tv_sec++;
6937c478bd9Sstevel@tonic-gate 				hr_clock_unlock(s);
6947c478bd9Sstevel@tonic-gate 				time_state = TIME_WAIT;
6957c478bd9Sstevel@tonic-gate 			}
6967c478bd9Sstevel@tonic-gate 			break;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		case TIME_OOP:
6997c478bd9Sstevel@tonic-gate 			time_state = TIME_WAIT;
7007c478bd9Sstevel@tonic-gate 			break;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		case TIME_WAIT:
7037c478bd9Sstevel@tonic-gate 			if (!(time_status & (STA_INS | STA_DEL)))
7047c478bd9Sstevel@tonic-gate 				time_state = TIME_OK;
7057c478bd9Sstevel@tonic-gate 		default:
7067c478bd9Sstevel@tonic-gate 			break;
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		/*
7107c478bd9Sstevel@tonic-gate 		 * Compute the phase adjustment for the next second. In
7117c478bd9Sstevel@tonic-gate 		 * PLL mode, the offset is reduced by a fixed factor
7127c478bd9Sstevel@tonic-gate 		 * times the time constant. In FLL mode the offset is
7137c478bd9Sstevel@tonic-gate 		 * used directly. In either mode, the maximum phase
7147c478bd9Sstevel@tonic-gate 		 * adjustment for each second is clamped so as to spread
7157c478bd9Sstevel@tonic-gate 		 * the adjustment over not more than the number of
7167c478bd9Sstevel@tonic-gate 		 * seconds between updates.
7177c478bd9Sstevel@tonic-gate 		 */
7187c478bd9Sstevel@tonic-gate 		if (time_offset == 0)
7197c478bd9Sstevel@tonic-gate 			time_adj = 0;
7207c478bd9Sstevel@tonic-gate 		else if (time_offset < 0) {
7217c478bd9Sstevel@tonic-gate 			lltemp = -time_offset;
7227c478bd9Sstevel@tonic-gate 			if (!(time_status & STA_FLL)) {
7237c478bd9Sstevel@tonic-gate 				if ((1 << time_constant) >= SCALE_KG)
7247c478bd9Sstevel@tonic-gate 					lltemp *= (1 << time_constant) /
7257c478bd9Sstevel@tonic-gate 					    SCALE_KG;
7267c478bd9Sstevel@tonic-gate 				else
7277c478bd9Sstevel@tonic-gate 					lltemp = (lltemp / SCALE_KG) >>
7287c478bd9Sstevel@tonic-gate 					    time_constant;
7297c478bd9Sstevel@tonic-gate 			}
7307c478bd9Sstevel@tonic-gate 			if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
7317c478bd9Sstevel@tonic-gate 				lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
7327c478bd9Sstevel@tonic-gate 			time_offset += lltemp;
7337c478bd9Sstevel@tonic-gate 			time_adj = -(lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
7347c478bd9Sstevel@tonic-gate 		} else {
7357c478bd9Sstevel@tonic-gate 			lltemp = time_offset;
7367c478bd9Sstevel@tonic-gate 			if (!(time_status & STA_FLL)) {
7377c478bd9Sstevel@tonic-gate 				if ((1 << time_constant) >= SCALE_KG)
7387c478bd9Sstevel@tonic-gate 					lltemp *= (1 << time_constant) /
7397c478bd9Sstevel@tonic-gate 					    SCALE_KG;
7407c478bd9Sstevel@tonic-gate 				else
7417c478bd9Sstevel@tonic-gate 					lltemp = (lltemp / SCALE_KG) >>
7427c478bd9Sstevel@tonic-gate 					    time_constant;
7437c478bd9Sstevel@tonic-gate 			}
7447c478bd9Sstevel@tonic-gate 			if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
7457c478bd9Sstevel@tonic-gate 				lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
7467c478bd9Sstevel@tonic-gate 			time_offset -= lltemp;
7477c478bd9Sstevel@tonic-gate 			time_adj = (lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		/*
7517c478bd9Sstevel@tonic-gate 		 * Compute the frequency estimate and additional phase
7527c478bd9Sstevel@tonic-gate 		 * adjustment due to frequency error for the next
7537c478bd9Sstevel@tonic-gate 		 * second. When the PPS signal is engaged, gnaw on the
7547c478bd9Sstevel@tonic-gate 		 * watchdog counter and update the frequency computed by
7557c478bd9Sstevel@tonic-gate 		 * the pll and the PPS signal.
7567c478bd9Sstevel@tonic-gate 		 */
7577c478bd9Sstevel@tonic-gate 		pps_valid++;
7587c478bd9Sstevel@tonic-gate 		if (pps_valid == PPS_VALID) {
7597c478bd9Sstevel@tonic-gate 			pps_jitter = MAXTIME;
7607c478bd9Sstevel@tonic-gate 			pps_stabil = MAXFREQ;
7617c478bd9Sstevel@tonic-gate 			time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
7627c478bd9Sstevel@tonic-gate 			    STA_PPSWANDER | STA_PPSERROR);
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate 		lltemp = time_freq + pps_freq;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		if (lltemp)
7677c478bd9Sstevel@tonic-gate 			time_adj += (lltemp * SCALE_PHASE) / (SCALE_USEC * hz);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 		/*
7707c478bd9Sstevel@tonic-gate 		 * End of precision kernel-code fragment
7717c478bd9Sstevel@tonic-gate 		 *
7727c478bd9Sstevel@tonic-gate 		 * The section below should be modified if we are planning
7737c478bd9Sstevel@tonic-gate 		 * to use NTP for synchronization.
7747c478bd9Sstevel@tonic-gate 		 *
7757c478bd9Sstevel@tonic-gate 		 * Note: the clock synchronization code now assumes
7767c478bd9Sstevel@tonic-gate 		 * the following:
7777c478bd9Sstevel@tonic-gate 		 *   - if dosynctodr is 1, then compute the drift between
7787c478bd9Sstevel@tonic-gate 		 *	the tod chip and software time and adjust one or
7797c478bd9Sstevel@tonic-gate 		 *	the other depending on the circumstances
7807c478bd9Sstevel@tonic-gate 		 *
7817c478bd9Sstevel@tonic-gate 		 *   - if dosynctodr is 0, then the tod chip is independent
7827c478bd9Sstevel@tonic-gate 		 *	of the software clock and should not be adjusted,
7837c478bd9Sstevel@tonic-gate 		 *	but allowed to free run.  this allows NTP to sync.
7847c478bd9Sstevel@tonic-gate 		 *	hrestime without any interference from the tod chip.
7857c478bd9Sstevel@tonic-gate 		 */
7867c478bd9Sstevel@tonic-gate 
7872c891a87Ssethg 		tod_validate_deferred = B_FALSE;
7887c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
7897c478bd9Sstevel@tonic-gate 		tod = tod_get();
7907c478bd9Sstevel@tonic-gate 		drift = tod.tv_sec - hrestime.tv_sec;
7917c478bd9Sstevel@tonic-gate 		absdrift = (drift >= 0) ? drift : -drift;
7927c478bd9Sstevel@tonic-gate 		if (tod_needsync || absdrift > 1) {
7937c478bd9Sstevel@tonic-gate 			int s;
7947c478bd9Sstevel@tonic-gate 			if (absdrift > 2) {
7957c478bd9Sstevel@tonic-gate 				if (!tod_broken && tod_faulted == TOD_NOFAULT) {
7967c478bd9Sstevel@tonic-gate 					s = hr_clock_lock();
7977c478bd9Sstevel@tonic-gate 					hrestime = tod;
7987c478bd9Sstevel@tonic-gate 					membar_enter();	/* hrestime visible */
7997c478bd9Sstevel@tonic-gate 					timedelta = 0;
8003348528fSdm 					timechanged++;
8017c478bd9Sstevel@tonic-gate 					tod_needsync = 0;
8027c478bd9Sstevel@tonic-gate 					hr_clock_unlock(s);
80387a18d3fSMadhavan Venkataraman 					callout_hrestime();
80487a18d3fSMadhavan Venkataraman 
8057c478bd9Sstevel@tonic-gate 				}
8067c478bd9Sstevel@tonic-gate 			} else {
8077c478bd9Sstevel@tonic-gate 				if (tod_needsync || !dosynctodr) {
8087c478bd9Sstevel@tonic-gate 					gethrestime(&tod);
8097c478bd9Sstevel@tonic-gate 					tod_set(tod);
8107c478bd9Sstevel@tonic-gate 					s = hr_clock_lock();
8117c478bd9Sstevel@tonic-gate 					if (timedelta == 0)
8127c478bd9Sstevel@tonic-gate 						tod_needsync = 0;
8137c478bd9Sstevel@tonic-gate 					hr_clock_unlock(s);
8147c478bd9Sstevel@tonic-gate 				} else {
8157c478bd9Sstevel@tonic-gate 					/*
8167c478bd9Sstevel@tonic-gate 					 * If the drift is 2 seconds on the
8177c478bd9Sstevel@tonic-gate 					 * money, then the TOD is adjusting
8187c478bd9Sstevel@tonic-gate 					 * the clock;  record that.
8197c478bd9Sstevel@tonic-gate 					 */
8207c478bd9Sstevel@tonic-gate 					clock_adj_hist[adj_hist_entry++ %
821d3d50737SRafael Vanoni 					    CLOCK_ADJ_HIST_SIZE] = now;
8227c478bd9Sstevel@tonic-gate 					s = hr_clock_lock();
8237c478bd9Sstevel@tonic-gate 					timedelta = (int64_t)drift*NANOSEC;
8247c478bd9Sstevel@tonic-gate 					hr_clock_unlock(s);
8257c478bd9Sstevel@tonic-gate 				}
8267c478bd9Sstevel@tonic-gate 			}
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 		one_sec = 0;
8297c478bd9Sstevel@tonic-gate 		time = gethrestime_sec();  /* for crusty old kmem readers */
8307c478bd9Sstevel@tonic-gate 		mutex_exit(&tod_lock);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		/*
8337c478bd9Sstevel@tonic-gate 		 * Some drivers still depend on this... XXX
8347c478bd9Sstevel@tonic-gate 		 */
8357c478bd9Sstevel@tonic-gate 		cv_broadcast(&lbolt_cv);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		vminfo.freemem += freemem;
8387c478bd9Sstevel@tonic-gate 		{
8397c478bd9Sstevel@tonic-gate 			pgcnt_t maxswap, resv, free;
8407c478bd9Sstevel@tonic-gate 			pgcnt_t avail =
8417c478bd9Sstevel@tonic-gate 			    MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
8427c478bd9Sstevel@tonic-gate 
8433aedfe0bSmishra 			maxswap = k_anoninfo.ani_mem_resv +
8443aedfe0bSmishra 			    k_anoninfo.ani_max +avail;
845b52a336eSPavel Tatashin 			/* Update ani_free */
846b52a336eSPavel Tatashin 			set_anoninfo();
8477c478bd9Sstevel@tonic-gate 			free = k_anoninfo.ani_free + avail;
8487c478bd9Sstevel@tonic-gate 			resv = k_anoninfo.ani_phys_resv +
8497c478bd9Sstevel@tonic-gate 			    k_anoninfo.ani_mem_resv;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 			vminfo.swap_resv += resv;
8527c478bd9Sstevel@tonic-gate 			/* number of reserved and allocated pages */
8537c478bd9Sstevel@tonic-gate #ifdef	DEBUG
8547c478bd9Sstevel@tonic-gate 			if (maxswap < free)
8557c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "clock: maxswap < free");
8567c478bd9Sstevel@tonic-gate 			if (maxswap < resv)
8577c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "clock: maxswap < resv");
8587c478bd9Sstevel@tonic-gate #endif
8597c478bd9Sstevel@tonic-gate 			vminfo.swap_alloc += maxswap - free;
8607c478bd9Sstevel@tonic-gate 			vminfo.swap_avail += maxswap - resv;
8617c478bd9Sstevel@tonic-gate 			vminfo.swap_free += free;
8627c478bd9Sstevel@tonic-gate 		}
863c65c9cdcSDonghai Qiao 		vminfo.updates++;
8647c478bd9Sstevel@tonic-gate 		if (nrunnable) {
8657c478bd9Sstevel@tonic-gate 			sysinfo.runque += nrunnable;
8667c478bd9Sstevel@tonic-gate 			sysinfo.runocc++;
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 		if (nswapped) {
8697c478bd9Sstevel@tonic-gate 			sysinfo.swpque += nswapped;
8707c478bd9Sstevel@tonic-gate 			sysinfo.swpocc++;
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 		sysinfo.waiting += w_io;
873c65c9cdcSDonghai Qiao 		sysinfo.updates++;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 		/*
8767c478bd9Sstevel@tonic-gate 		 * Wake up fsflush to write out DELWRI
8777c478bd9Sstevel@tonic-gate 		 * buffers, dirty pages and other cached
8787c478bd9Sstevel@tonic-gate 		 * administrative data, e.g. inodes.
8797c478bd9Sstevel@tonic-gate 		 */
8807c478bd9Sstevel@tonic-gate 		if (--fsflushcnt <= 0) {
8817c478bd9Sstevel@tonic-gate 			fsflushcnt = tune.t_fsflushr;
8827c478bd9Sstevel@tonic-gate 			cv_signal(&fsflush_cv);
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		vmmeter();
8867c478bd9Sstevel@tonic-gate 		calcloadavg(genloadavg(&loadavg), hp_avenrun);
8877c478bd9Sstevel@tonic-gate 		for (i = 0; i < 3; i++)
8887c478bd9Sstevel@tonic-gate 			/*
8897c478bd9Sstevel@tonic-gate 			 * At the moment avenrun[] can only hold 31
8907c478bd9Sstevel@tonic-gate 			 * bits of load average as it is a signed
8917c478bd9Sstevel@tonic-gate 			 * int in the API. We need to ensure that
8927c478bd9Sstevel@tonic-gate 			 * hp_avenrun[i] >> (16 - FSHIFT) will not be
8937c478bd9Sstevel@tonic-gate 			 * too large. If it is, we put the largest value
8947c478bd9Sstevel@tonic-gate 			 * that we can use into avenrun[i]. This is
8957c478bd9Sstevel@tonic-gate 			 * kludgey, but about all we can do until we
8967c478bd9Sstevel@tonic-gate 			 * avenrun[] is declared as an array of uint64[]
8977c478bd9Sstevel@tonic-gate 			 */
8987c478bd9Sstevel@tonic-gate 			if (hp_avenrun[i] < ((uint64_t)1<<(31+16-FSHIFT)))
8997c478bd9Sstevel@tonic-gate 				avenrun[i] = (int32_t)(hp_avenrun[i] >>
9007c478bd9Sstevel@tonic-gate 				    (16 - FSHIFT));
9017c478bd9Sstevel@tonic-gate 			else
9027c478bd9Sstevel@tonic-gate 				avenrun[i] = 0x7fffffff;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 		cpupart = cp_list_head;
9057c478bd9Sstevel@tonic-gate 		do {
9067c478bd9Sstevel@tonic-gate 			calcloadavg(genloadavg(&cpupart->cp_loadavg),
9077c478bd9Sstevel@tonic-gate 			    cpupart->cp_hp_avenrun);
9087c478bd9Sstevel@tonic-gate 		} while ((cpupart = cpupart->cp_next) != cp_list_head);
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 		/*
9117c478bd9Sstevel@tonic-gate 		 * Wake up the swapper thread if necessary.
9127c478bd9Sstevel@tonic-gate 		 */
9137c478bd9Sstevel@tonic-gate 		if (runin ||
9147c478bd9Sstevel@tonic-gate 		    (runout && (avefree < desfree || wake_sched_sec))) {
9157c478bd9Sstevel@tonic-gate 			t = &t0;
9167c478bd9Sstevel@tonic-gate 			thread_lock(t);
9177c478bd9Sstevel@tonic-gate 			if (t->t_state == TS_STOPPED) {
9187c478bd9Sstevel@tonic-gate 				runin = runout = 0;
9197c478bd9Sstevel@tonic-gate 				wake_sched_sec = 0;
9207c478bd9Sstevel@tonic-gate 				t->t_whystop = 0;
9217c478bd9Sstevel@tonic-gate 				t->t_whatstop = 0;
9227c478bd9Sstevel@tonic-gate 				t->t_schedflag &= ~TS_ALLSTART;
9237c478bd9Sstevel@tonic-gate 				THREAD_TRANSITION(t);
9247c478bd9Sstevel@tonic-gate 				setfrontdq(t);
9257c478bd9Sstevel@tonic-gate 			}
9267c478bd9Sstevel@tonic-gate 			thread_unlock(t);
9277c478bd9Sstevel@tonic-gate 		}
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	/*
9317c478bd9Sstevel@tonic-gate 	 * Wake up the swapper if any high priority swapped-out threads
9327c478bd9Sstevel@tonic-gate 	 * became runable during the last tick.
9337c478bd9Sstevel@tonic-gate 	 */
9347c478bd9Sstevel@tonic-gate 	if (wake_sched) {
9357c478bd9Sstevel@tonic-gate 		t = &t0;
9367c478bd9Sstevel@tonic-gate 		thread_lock(t);
9377c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_STOPPED) {
9387c478bd9Sstevel@tonic-gate 			runin = runout = 0;
9397c478bd9Sstevel@tonic-gate 			wake_sched = 0;
9407c478bd9Sstevel@tonic-gate 			t->t_whystop = 0;
9417c478bd9Sstevel@tonic-gate 			t->t_whatstop = 0;
9427c478bd9Sstevel@tonic-gate 			t->t_schedflag &= ~TS_ALLSTART;
9437c478bd9Sstevel@tonic-gate 			THREAD_TRANSITION(t);
9447c478bd9Sstevel@tonic-gate 			setfrontdq(t);
9457c478bd9Sstevel@tonic-gate 		}
9467c478bd9Sstevel@tonic-gate 		thread_unlock(t);
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate void
clock_init(void)9517c478bd9Sstevel@tonic-gate clock_init(void)
9527c478bd9Sstevel@tonic-gate {
953a288e5a9SJoshua M. Clulow 	cyc_handler_t clk_hdlr, lbolt_hdlr;
954d3d50737SRafael Vanoni 	cyc_time_t clk_when, lbolt_when;
955d3d50737SRafael Vanoni 	int i, sz;
956d3d50737SRafael Vanoni 	intptr_t buf;
9577c478bd9Sstevel@tonic-gate 
958d3d50737SRafael Vanoni 	/*
959d3d50737SRafael Vanoni 	 * Setup handler and timer for the clock cyclic.
960d3d50737SRafael Vanoni 	 */
961d3d50737SRafael Vanoni 	clk_hdlr.cyh_func = (cyc_func_t)clock;
962d3d50737SRafael Vanoni 	clk_hdlr.cyh_level = CY_LOCK_LEVEL;
963d3d50737SRafael Vanoni 	clk_hdlr.cyh_arg = NULL;
9647c478bd9Sstevel@tonic-gate 
965d3d50737SRafael Vanoni 	clk_when.cyt_when = 0;
966d3d50737SRafael Vanoni 	clk_when.cyt_interval = nsec_per_tick;
967dd4eeefdSeota 
968d3d50737SRafael Vanoni 	/*
96983e866b9SRafael Vanoni 	 * The lbolt cyclic will be reprogramed to fire at a nsec_per_tick
97083e866b9SRafael Vanoni 	 * interval to satisfy performance needs of the DDI lbolt consumers.
97183e866b9SRafael Vanoni 	 * It is off by default.
972d3d50737SRafael Vanoni 	 */
973d3d50737SRafael Vanoni 	lbolt_hdlr.cyh_func = (cyc_func_t)lbolt_cyclic;
974d3d50737SRafael Vanoni 	lbolt_hdlr.cyh_level = CY_LOCK_LEVEL;
975d3d50737SRafael Vanoni 	lbolt_hdlr.cyh_arg = NULL;
976d3d50737SRafael Vanoni 
977d3d50737SRafael Vanoni 	lbolt_when.cyt_interval = nsec_per_tick;
978d3d50737SRafael Vanoni 
979d3d50737SRafael Vanoni 	/*
980d3d50737SRafael Vanoni 	 * Allocate cache line aligned space for the per CPU lbolt data and
9811b7f7204SRafael Vanoni 	 * lbolt info structures, and initialize them with their default
9821b7f7204SRafael Vanoni 	 * values. Note that these structures are also cache line sized.
983d3d50737SRafael Vanoni 	 */
984d3d50737SRafael Vanoni 	sz = sizeof (lbolt_info_t) + CPU_CACHE_COHERENCE_SIZE;
985d3d50737SRafael Vanoni 	buf = (intptr_t)kmem_zalloc(sz, KM_SLEEP);
986d3d50737SRafael Vanoni 	lb_info = (lbolt_info_t *)P2ROUNDUP(buf, CPU_CACHE_COHERENCE_SIZE);
987d3d50737SRafael Vanoni 
988d3d50737SRafael Vanoni 	if (hz != HZ_DEFAULT)
989d3d50737SRafael Vanoni 		lb_info->lbi_thresh_interval = LBOLT_THRESH_INTERVAL *
990d3d50737SRafael Vanoni 		    hz/HZ_DEFAULT;
991d3d50737SRafael Vanoni 	else
992d3d50737SRafael Vanoni 		lb_info->lbi_thresh_interval = LBOLT_THRESH_INTERVAL;
993d3d50737SRafael Vanoni 
994d3d50737SRafael Vanoni 	lb_info->lbi_thresh_calls = LBOLT_THRESH_CALLS;
995d3d50737SRafael Vanoni 
9961b7f7204SRafael Vanoni 	sz = (sizeof (lbolt_cpu_t) * max_ncpus) + CPU_CACHE_COHERENCE_SIZE;
997d3d50737SRafael Vanoni 	buf = (intptr_t)kmem_zalloc(sz, KM_SLEEP);
998d3d50737SRafael Vanoni 	lb_cpu = (lbolt_cpu_t *)P2ROUNDUP(buf, CPU_CACHE_COHERENCE_SIZE);
999d3d50737SRafael Vanoni 
1000d3d50737SRafael Vanoni 	for (i = 0; i < max_ncpus; i++)
1001d3d50737SRafael Vanoni 		lb_cpu[i].lbc_counter = lb_info->lbi_thresh_calls;
1002d3d50737SRafael Vanoni 
100383e866b9SRafael Vanoni 	/*
100483e866b9SRafael Vanoni 	 * Install the softint used to switch between event and cyclic driven
100583e866b9SRafael Vanoni 	 * lbolt. We use a soft interrupt to make sure the context of the
100683e866b9SRafael Vanoni 	 * cyclic reprogram call is safe.
100783e866b9SRafael Vanoni 	 */
1008d3d50737SRafael Vanoni 	lbolt_softint_add();
1009d3d50737SRafael Vanoni 
101083e866b9SRafael Vanoni 	/*
101183e866b9SRafael Vanoni 	 * Since the hybrid lbolt implementation is based on a hardware counter
101283e866b9SRafael Vanoni 	 * that is reset at every hardware reboot and that we'd like to have
101383e866b9SRafael Vanoni 	 * the lbolt value starting at zero after both a hardware and a fast
101483e866b9SRafael Vanoni 	 * reboot, we calculate the number of clock ticks the system's been up
101583e866b9SRafael Vanoni 	 * and store it in the lbi_debug_time field of the lbolt info structure.
101683e866b9SRafael Vanoni 	 * The value of this field will be subtracted from lbolt before
101783e866b9SRafael Vanoni 	 * returning it.
101883e866b9SRafael Vanoni 	 */
101983e866b9SRafael Vanoni 	lb_info->lbi_internal = lb_info->lbi_debug_time =
102083e866b9SRafael Vanoni 	    (gethrtime()/nsec_per_tick);
102183e866b9SRafael Vanoni 
102283e866b9SRafael Vanoni 	/*
102383e866b9SRafael Vanoni 	 * lbolt_hybrid points at lbolt_bootstrap until now. The LBOLT_* macros
102483e866b9SRafael Vanoni 	 * and lbolt_debug_{enter,return} use this value as an indication that
102583e866b9SRafael Vanoni 	 * the initializaion above hasn't been completed. Setting lbolt_hybrid
102683e866b9SRafael Vanoni 	 * to either lbolt_{cyclic,event}_driven here signals those code paths
102783e866b9SRafael Vanoni 	 * that the lbolt related structures can be used.
102883e866b9SRafael Vanoni 	 */
10293bf27077SRafael Vanoni 	if (lbolt_cyc_only) {
10303bf27077SRafael Vanoni 		lbolt_when.cyt_when = 0;
10313bf27077SRafael Vanoni 		lbolt_hybrid = lbolt_cyclic_driven;
10323bf27077SRafael Vanoni 	} else {
10333bf27077SRafael Vanoni 		lbolt_when.cyt_when = CY_INFINITY;
10343bf27077SRafael Vanoni 		lbolt_hybrid = lbolt_event_driven;
10353bf27077SRafael Vanoni 	}
10363bf27077SRafael Vanoni 
1037d3d50737SRafael Vanoni 	/*
1038d3d50737SRafael Vanoni 	 * Grab cpu_lock and install all three cyclics.
1039d3d50737SRafael Vanoni 	 */
1040dd4eeefdSeota 	mutex_enter(&cpu_lock);
1041d3d50737SRafael Vanoni 
1042d3d50737SRafael Vanoni 	clock_cyclic = cyclic_add(&clk_hdlr, &clk_when);
1043579a6895SRafael Vanoni 	lb_info->id.lbi_cyclic_id = cyclic_add(&lbolt_hdlr, &lbolt_when);
1044d3d50737SRafael Vanoni 
1045dd4eeefdSeota 	mutex_exit(&cpu_lock);
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate /*
10497c478bd9Sstevel@tonic-gate  * Called before calcloadavg to get 10-sec moving loadavg together
10507c478bd9Sstevel@tonic-gate  */
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate static int
genloadavg(struct loadavg_s * avgs)10537c478bd9Sstevel@tonic-gate genloadavg(struct loadavg_s *avgs)
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate 	int avg;
10567c478bd9Sstevel@tonic-gate 	int spos; /* starting position */
10577c478bd9Sstevel@tonic-gate 	int cpos; /* moving current position */
10587c478bd9Sstevel@tonic-gate 	int i;
10597c478bd9Sstevel@tonic-gate 	int slen;
10607c478bd9Sstevel@tonic-gate 	hrtime_t hr_avg;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	/* 10-second snapshot, calculate first positon */
10637c478bd9Sstevel@tonic-gate 	if (avgs->lg_len == 0) {
10647c478bd9Sstevel@tonic-gate 		return (0);
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 	slen = avgs->lg_len < S_MOVAVG_SZ ? avgs->lg_len : S_MOVAVG_SZ;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	spos = (avgs->lg_cur - 1) >= 0 ? avgs->lg_cur - 1 :
10697c478bd9Sstevel@tonic-gate 	    S_LOADAVG_SZ + (avgs->lg_cur - 1);
10707c478bd9Sstevel@tonic-gate 	for (i = hr_avg = 0; i < slen; i++) {
10717c478bd9Sstevel@tonic-gate 		cpos = (spos - i) >= 0 ? spos - i : S_LOADAVG_SZ + (spos - i);
10727c478bd9Sstevel@tonic-gate 		hr_avg += avgs->lg_loads[cpos];
10737c478bd9Sstevel@tonic-gate 	}
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	hr_avg = hr_avg / slen;
10767c478bd9Sstevel@tonic-gate 	avg = hr_avg / (NANOSEC / LGRP_LOADAVG_IN_THREAD_MAX);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	return (avg);
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate /*
10827c478bd9Sstevel@tonic-gate  * Run every second from clock () to update the loadavg count available to the
10837c478bd9Sstevel@tonic-gate  * system and cpu-partitions.
10847c478bd9Sstevel@tonic-gate  *
10857c478bd9Sstevel@tonic-gate  * This works by sampling the previous usr, sys, wait time elapsed,
10867c478bd9Sstevel@tonic-gate  * computing a delta, and adding that delta to the elapsed usr, sys,
10877c478bd9Sstevel@tonic-gate  * wait increase.
10887c478bd9Sstevel@tonic-gate  */
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate static void
loadavg_update()10917c478bd9Sstevel@tonic-gate loadavg_update()
10927c478bd9Sstevel@tonic-gate {
10937c478bd9Sstevel@tonic-gate 	cpu_t *cp;
10947c478bd9Sstevel@tonic-gate 	cpupart_t *cpupart;
10957c478bd9Sstevel@tonic-gate 	hrtime_t cpu_total;
10967c478bd9Sstevel@tonic-gate 	int prev;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	cp = cpu_list;
10997c478bd9Sstevel@tonic-gate 	loadavg.lg_total = 0;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	/*
11027c478bd9Sstevel@tonic-gate 	 * first pass totals up per-cpu statistics for system and cpu
11037c478bd9Sstevel@tonic-gate 	 * partitions
11047c478bd9Sstevel@tonic-gate 	 */
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	do {
11077c478bd9Sstevel@tonic-gate 		struct loadavg_s *lavg;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 		lavg = &cp->cpu_loadavg;
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 		cpu_total = cp->cpu_acct[CMS_USER] +
11127c478bd9Sstevel@tonic-gate 		    cp->cpu_acct[CMS_SYSTEM] + cp->cpu_waitrq;
11137c478bd9Sstevel@tonic-gate 		/* compute delta against last total */
11147c478bd9Sstevel@tonic-gate 		scalehrtime(&cpu_total);
11157c478bd9Sstevel@tonic-gate 		prev = (lavg->lg_cur - 1) >= 0 ? lavg->lg_cur - 1 :
11167c478bd9Sstevel@tonic-gate 		    S_LOADAVG_SZ + (lavg->lg_cur - 1);
11177c478bd9Sstevel@tonic-gate 		if (lavg->lg_loads[prev] <= 0) {
11187c478bd9Sstevel@tonic-gate 			lavg->lg_loads[lavg->lg_cur] = cpu_total;
11197c478bd9Sstevel@tonic-gate 			cpu_total = 0;
11207c478bd9Sstevel@tonic-gate 		} else {
11217c478bd9Sstevel@tonic-gate 			lavg->lg_loads[lavg->lg_cur] = cpu_total;
11227c478bd9Sstevel@tonic-gate 			cpu_total = cpu_total - lavg->lg_loads[prev];
11237c478bd9Sstevel@tonic-gate 			if (cpu_total < 0)
11247c478bd9Sstevel@tonic-gate 				cpu_total = 0;
11257c478bd9Sstevel@tonic-gate 		}
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
11287c478bd9Sstevel@tonic-gate 		lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
11297c478bd9Sstevel@tonic-gate 		    lavg->lg_len + 1 : S_LOADAVG_SZ;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 		loadavg.lg_total += cpu_total;
11327c478bd9Sstevel@tonic-gate 		cp->cpu_part->cp_loadavg.lg_total += cpu_total;
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	} while ((cp = cp->cpu_next) != cpu_list);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	loadavg.lg_loads[loadavg.lg_cur] = loadavg.lg_total;
11377c478bd9Sstevel@tonic-gate 	loadavg.lg_cur = (loadavg.lg_cur + 1) % S_LOADAVG_SZ;
11387c478bd9Sstevel@tonic-gate 	loadavg.lg_len = (loadavg.lg_len + 1) < S_LOADAVG_SZ ?
11397c478bd9Sstevel@tonic-gate 	    loadavg.lg_len + 1 : S_LOADAVG_SZ;
11407c478bd9Sstevel@tonic-gate 	/*
11417c478bd9Sstevel@tonic-gate 	 * Second pass updates counts
11427c478bd9Sstevel@tonic-gate 	 */
11437c478bd9Sstevel@tonic-gate 	cpupart = cp_list_head;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	do {
11467c478bd9Sstevel@tonic-gate 		struct loadavg_s *lavg;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 		lavg = &cpupart->cp_loadavg;
11497c478bd9Sstevel@tonic-gate 		lavg->lg_loads[lavg->lg_cur] = lavg->lg_total;
11507c478bd9Sstevel@tonic-gate 		lavg->lg_total = 0;
11517c478bd9Sstevel@tonic-gate 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
11527c478bd9Sstevel@tonic-gate 		lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
11537c478bd9Sstevel@tonic-gate 		    lavg->lg_len + 1 : S_LOADAVG_SZ;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	} while ((cpupart = cpupart->cp_next) != cp_list_head);
11567c478bd9Sstevel@tonic-gate 
115781d43577SJerry Jelinek 	/*
115881d43577SJerry Jelinek 	 * Third pass totals up per-zone statistics.
115981d43577SJerry Jelinek 	 */
116081d43577SJerry Jelinek 	zone_loadavg_update();
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate /*
11647c478bd9Sstevel@tonic-gate  * clock_update() - local clock update
11657c478bd9Sstevel@tonic-gate  *
11667c478bd9Sstevel@tonic-gate  * This routine is called by ntp_adjtime() to update the local clock
11677c478bd9Sstevel@tonic-gate  * phase and frequency. The implementation is of an
11687c478bd9Sstevel@tonic-gate  * adaptive-parameter, hybrid phase/frequency-lock loop (PLL/FLL). The
11697c478bd9Sstevel@tonic-gate  * routine computes new time and frequency offset estimates for each
11707c478bd9Sstevel@tonic-gate  * call.  The PPS signal itself determines the new time offset,
11717c478bd9Sstevel@tonic-gate  * instead of the calling argument.  Presumably, calls to
11727c478bd9Sstevel@tonic-gate  * ntp_adjtime() occur only when the caller believes the local clock
11737c478bd9Sstevel@tonic-gate  * is valid within some bound (+-128 ms with NTP). If the caller's
11747c478bd9Sstevel@tonic-gate  * time is far different than the PPS time, an argument will ensue,
11757c478bd9Sstevel@tonic-gate  * and it's not clear who will lose.
11767c478bd9Sstevel@tonic-gate  *
11777c478bd9Sstevel@tonic-gate  * For uncompensated quartz crystal oscillatores and nominal update
11787c478bd9Sstevel@tonic-gate  * intervals less than 1024 s, operation should be in phase-lock mode
11797c478bd9Sstevel@tonic-gate  * (STA_FLL = 0), where the loop is disciplined to phase. For update
11807c478bd9Sstevel@tonic-gate  * intervals greater than this, operation should be in frequency-lock
11817c478bd9Sstevel@tonic-gate  * mode (STA_FLL = 1), where the loop is disciplined to frequency.
11827c478bd9Sstevel@tonic-gate  *
11837c478bd9Sstevel@tonic-gate  * Note: mutex(&tod_lock) is in effect.
11847c478bd9Sstevel@tonic-gate  */
11857c478bd9Sstevel@tonic-gate void
clock_update(int offset)11867c478bd9Sstevel@tonic-gate clock_update(int offset)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	int ltemp, mtemp, s;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME))
11937c478bd9Sstevel@tonic-gate 		return;
11947c478bd9Sstevel@tonic-gate 	ltemp = offset;
11957c478bd9Sstevel@tonic-gate 	if ((time_status & STA_PPSTIME) && (time_status & STA_PPSSIGNAL))
11967c478bd9Sstevel@tonic-gate 		ltemp = pps_offset;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	/*
11997c478bd9Sstevel@tonic-gate 	 * Scale the phase adjustment and clamp to the operating range.
12007c478bd9Sstevel@tonic-gate 	 */
12017c478bd9Sstevel@tonic-gate 	if (ltemp > MAXPHASE)
12027c478bd9Sstevel@tonic-gate 		time_offset = MAXPHASE * SCALE_UPDATE;
12037c478bd9Sstevel@tonic-gate 	else if (ltemp < -MAXPHASE)
12047c478bd9Sstevel@tonic-gate 		time_offset = -(MAXPHASE * SCALE_UPDATE);
12057c478bd9Sstevel@tonic-gate 	else
12067c478bd9Sstevel@tonic-gate 		time_offset = ltemp * SCALE_UPDATE;
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	/*
12097c478bd9Sstevel@tonic-gate 	 * Select whether the frequency is to be controlled and in which
12107c478bd9Sstevel@tonic-gate 	 * mode (PLL or FLL). Clamp to the operating range. Ugly
12117c478bd9Sstevel@tonic-gate 	 * multiply/divide should be replaced someday.
12127c478bd9Sstevel@tonic-gate 	 */
12137c478bd9Sstevel@tonic-gate 	if (time_status & STA_FREQHOLD || time_reftime == 0)
12147c478bd9Sstevel@tonic-gate 		time_reftime = hrestime.tv_sec;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	mtemp = hrestime.tv_sec - time_reftime;
12177c478bd9Sstevel@tonic-gate 	time_reftime = hrestime.tv_sec;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	if (time_status & STA_FLL) {
12207c478bd9Sstevel@tonic-gate 		if (mtemp >= MINSEC) {
12217c478bd9Sstevel@tonic-gate 			ltemp = ((time_offset / mtemp) * (SCALE_USEC /
12227c478bd9Sstevel@tonic-gate 			    SCALE_UPDATE));
12237c478bd9Sstevel@tonic-gate 			if (ltemp)
12247c478bd9Sstevel@tonic-gate 				time_freq += ltemp / SCALE_KH;
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 	} else {
12277c478bd9Sstevel@tonic-gate 		if (mtemp < MAXSEC) {
12287c478bd9Sstevel@tonic-gate 			ltemp *= mtemp;
12297c478bd9Sstevel@tonic-gate 			if (ltemp)
12307c478bd9Sstevel@tonic-gate 				time_freq += (int)(((int64_t)ltemp *
12317c478bd9Sstevel@tonic-gate 				    SCALE_USEC) / SCALE_KF)
12327c478bd9Sstevel@tonic-gate 				    / (1 << (time_constant * 2));
12337c478bd9Sstevel@tonic-gate 		}
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 	if (time_freq > time_tolerance)
12367c478bd9Sstevel@tonic-gate 		time_freq = time_tolerance;
12377c478bd9Sstevel@tonic-gate 	else if (time_freq < -time_tolerance)
12387c478bd9Sstevel@tonic-gate 		time_freq = -time_tolerance;
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	s = hr_clock_lock();
12417c478bd9Sstevel@tonic-gate 	tod_needsync = 1;
12427c478bd9Sstevel@tonic-gate 	hr_clock_unlock(s);
12437c478bd9Sstevel@tonic-gate }
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate /*
12467c478bd9Sstevel@tonic-gate  * ddi_hardpps() - discipline CPU clock oscillator to external PPS signal
12477c478bd9Sstevel@tonic-gate  *
12487c478bd9Sstevel@tonic-gate  * This routine is called at each PPS interrupt in order to discipline
12497c478bd9Sstevel@tonic-gate  * the CPU clock oscillator to the PPS signal. It measures the PPS phase
12507c478bd9Sstevel@tonic-gate  * and leaves it in a handy spot for the clock() routine. It
12517c478bd9Sstevel@tonic-gate  * integrates successive PPS phase differences and calculates the
12527c478bd9Sstevel@tonic-gate  * frequency offset. This is used in clock() to discipline the CPU
12537c478bd9Sstevel@tonic-gate  * clock oscillator so that intrinsic frequency error is cancelled out.
12547c478bd9Sstevel@tonic-gate  * The code requires the caller to capture the time and hardware counter
12557c478bd9Sstevel@tonic-gate  * value at the on-time PPS signal transition.
12567c478bd9Sstevel@tonic-gate  *
12577c478bd9Sstevel@tonic-gate  * Note that, on some Unix systems, this routine runs at an interrupt
12587c478bd9Sstevel@tonic-gate  * priority level higher than the timer interrupt routine clock().
12597c478bd9Sstevel@tonic-gate  * Therefore, the variables used are distinct from the clock()
12607c478bd9Sstevel@tonic-gate  * variables, except for certain exceptions: The PPS frequency pps_freq
12617c478bd9Sstevel@tonic-gate  * and phase pps_offset variables are determined by this routine and
12627c478bd9Sstevel@tonic-gate  * updated atomically. The time_tolerance variable can be considered a
12637c478bd9Sstevel@tonic-gate  * constant, since it is infrequently changed, and then only when the
12647c478bd9Sstevel@tonic-gate  * PPS signal is disabled. The watchdog counter pps_valid is updated
12657c478bd9Sstevel@tonic-gate  * once per second by clock() and is atomically cleared in this
12667c478bd9Sstevel@tonic-gate  * routine.
12677c478bd9Sstevel@tonic-gate  *
12687c478bd9Sstevel@tonic-gate  * tvp is the time of the last tick; usec is a microsecond count since the
12697c478bd9Sstevel@tonic-gate  * last tick.
12707c478bd9Sstevel@tonic-gate  *
12717c478bd9Sstevel@tonic-gate  * Note: In Solaris systems, the tick value is actually given by
12727c478bd9Sstevel@tonic-gate  *       usec_per_tick.  This is called from the serial driver cdintr(),
12737c478bd9Sstevel@tonic-gate  *	 or equivalent, at a high PIL.  Because the kernel keeps a
12747c478bd9Sstevel@tonic-gate  *	 highresolution time, the following code can accept either
12757c478bd9Sstevel@tonic-gate  *	 the traditional argument pair, or the current highres timestamp
12767c478bd9Sstevel@tonic-gate  *       in tvp and zero in usec.
12777c478bd9Sstevel@tonic-gate  */
12787c478bd9Sstevel@tonic-gate void
ddi_hardpps(struct timeval * tvp,int usec)12797c478bd9Sstevel@tonic-gate ddi_hardpps(struct timeval *tvp, int usec)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate 	int u_usec, v_usec, bigtick;
12827c478bd9Sstevel@tonic-gate 	time_t cal_sec;
12837c478bd9Sstevel@tonic-gate 	int cal_usec;
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	/*
12867c478bd9Sstevel@tonic-gate 	 * An occasional glitch can be produced when the PPS interrupt
12877c478bd9Sstevel@tonic-gate 	 * occurs in the clock() routine before the time variable is
12887c478bd9Sstevel@tonic-gate 	 * updated. Here the offset is discarded when the difference
12897c478bd9Sstevel@tonic-gate 	 * between it and the last one is greater than tick/2, but not
12907c478bd9Sstevel@tonic-gate 	 * if the interval since the first discard exceeds 30 s.
12917c478bd9Sstevel@tonic-gate 	 */
12927c478bd9Sstevel@tonic-gate 	time_status |= STA_PPSSIGNAL;
12937c478bd9Sstevel@tonic-gate 	time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
12947c478bd9Sstevel@tonic-gate 	pps_valid = 0;
12957c478bd9Sstevel@tonic-gate 	u_usec = -tvp->tv_usec;
12967c478bd9Sstevel@tonic-gate 	if (u_usec < -(MICROSEC/2))
12977c478bd9Sstevel@tonic-gate 		u_usec += MICROSEC;
12987c478bd9Sstevel@tonic-gate 	v_usec = pps_offset - u_usec;
12997c478bd9Sstevel@tonic-gate 	if (v_usec < 0)
13007c478bd9Sstevel@tonic-gate 		v_usec = -v_usec;
13017c478bd9Sstevel@tonic-gate 	if (v_usec > (usec_per_tick >> 1)) {
13027c478bd9Sstevel@tonic-gate 		if (pps_glitch > MAXGLITCH) {
13037c478bd9Sstevel@tonic-gate 			pps_glitch = 0;
13047c478bd9Sstevel@tonic-gate 			pps_tf[2] = u_usec;
13057c478bd9Sstevel@tonic-gate 			pps_tf[1] = u_usec;
13067c478bd9Sstevel@tonic-gate 		} else {
13077c478bd9Sstevel@tonic-gate 			pps_glitch++;
13087c478bd9Sstevel@tonic-gate 			u_usec = pps_offset;
13097c478bd9Sstevel@tonic-gate 		}
13107c478bd9Sstevel@tonic-gate 	} else
13117c478bd9Sstevel@tonic-gate 		pps_glitch = 0;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/*
13147c478bd9Sstevel@tonic-gate 	 * A three-stage median filter is used to help deglitch the pps
13157c478bd9Sstevel@tonic-gate 	 * time. The median sample becomes the time offset estimate; the
13167c478bd9Sstevel@tonic-gate 	 * difference between the other two samples becomes the time
13177c478bd9Sstevel@tonic-gate 	 * dispersion (jitter) estimate.
13187c478bd9Sstevel@tonic-gate 	 */
13197c478bd9Sstevel@tonic-gate 	pps_tf[2] = pps_tf[1];
13207c478bd9Sstevel@tonic-gate 	pps_tf[1] = pps_tf[0];
13217c478bd9Sstevel@tonic-gate 	pps_tf[0] = u_usec;
13227c478bd9Sstevel@tonic-gate 	if (pps_tf[0] > pps_tf[1]) {
13237c478bd9Sstevel@tonic-gate 		if (pps_tf[1] > pps_tf[2]) {
13247c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[1];		/* 0 1 2 */
13257c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[0] - pps_tf[2];
13267c478bd9Sstevel@tonic-gate 		} else if (pps_tf[2] > pps_tf[0]) {
13277c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[0];		/* 2 0 1 */
13287c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[2] - pps_tf[1];
13297c478bd9Sstevel@tonic-gate 		} else {
13307c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[2];		/* 0 2 1 */
13317c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[0] - pps_tf[1];
13327c478bd9Sstevel@tonic-gate 		}
13337c478bd9Sstevel@tonic-gate 	} else {
13347c478bd9Sstevel@tonic-gate 		if (pps_tf[1] < pps_tf[2]) {
13357c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[1];		/* 2 1 0 */
13367c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[2] - pps_tf[0];
13377c478bd9Sstevel@tonic-gate 		} else  if (pps_tf[2] < pps_tf[0]) {
13387c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[0];		/* 1 0 2 */
13397c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[1] - pps_tf[2];
13407c478bd9Sstevel@tonic-gate 		} else {
13417c478bd9Sstevel@tonic-gate 			pps_offset = pps_tf[2];		/* 1 2 0 */
13427c478bd9Sstevel@tonic-gate 			v_usec = pps_tf[1] - pps_tf[0];
13437c478bd9Sstevel@tonic-gate 		}
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 	if (v_usec > MAXTIME)
13467c478bd9Sstevel@tonic-gate 		pps_jitcnt++;
13477c478bd9Sstevel@tonic-gate 	v_usec = (v_usec << PPS_AVG) - pps_jitter;
13487c478bd9Sstevel@tonic-gate 	pps_jitter += v_usec / (1 << PPS_AVG);
13497c478bd9Sstevel@tonic-gate 	if (pps_jitter > (MAXTIME >> 1))
13507c478bd9Sstevel@tonic-gate 		time_status |= STA_PPSJITTER;
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	/*
13537c478bd9Sstevel@tonic-gate 	 * During the calibration interval adjust the starting time when
13547c478bd9Sstevel@tonic-gate 	 * the tick overflows. At the end of the interval compute the
13557c478bd9Sstevel@tonic-gate 	 * duration of the interval and the difference of the hardware
13567c478bd9Sstevel@tonic-gate 	 * counters at the beginning and end of the interval. This code
13577c478bd9Sstevel@tonic-gate 	 * is deliciously complicated by the fact valid differences may
13587c478bd9Sstevel@tonic-gate 	 * exceed the value of tick when using long calibration
13597c478bd9Sstevel@tonic-gate 	 * intervals and small ticks. Note that the counter can be
13607c478bd9Sstevel@tonic-gate 	 * greater than tick if caught at just the wrong instant, but
13617c478bd9Sstevel@tonic-gate 	 * the values returned and used here are correct.
13627c478bd9Sstevel@tonic-gate 	 */
13637c478bd9Sstevel@tonic-gate 	bigtick = (int)usec_per_tick * SCALE_USEC;
13647c478bd9Sstevel@tonic-gate 	pps_usec -= pps_freq;
13657c478bd9Sstevel@tonic-gate 	if (pps_usec >= bigtick)
13667c478bd9Sstevel@tonic-gate 		pps_usec -= bigtick;
13677c478bd9Sstevel@tonic-gate 	if (pps_usec < 0)
13687c478bd9Sstevel@tonic-gate 		pps_usec += bigtick;
13697c478bd9Sstevel@tonic-gate 	pps_time.tv_sec++;
13707c478bd9Sstevel@tonic-gate 	pps_count++;
13717c478bd9Sstevel@tonic-gate 	if (pps_count < (1 << pps_shift))
13727c478bd9Sstevel@tonic-gate 		return;
13737c478bd9Sstevel@tonic-gate 	pps_count = 0;
13747c478bd9Sstevel@tonic-gate 	pps_calcnt++;
13757c478bd9Sstevel@tonic-gate 	u_usec = usec * SCALE_USEC;
13767c478bd9Sstevel@tonic-gate 	v_usec = pps_usec - u_usec;
13777c478bd9Sstevel@tonic-gate 	if (v_usec >= bigtick >> 1)
13787c478bd9Sstevel@tonic-gate 		v_usec -= bigtick;
13797c478bd9Sstevel@tonic-gate 	if (v_usec < -(bigtick >> 1))
13807c478bd9Sstevel@tonic-gate 		v_usec += bigtick;
13817c478bd9Sstevel@tonic-gate 	if (v_usec < 0)
13827c478bd9Sstevel@tonic-gate 		v_usec = -(-v_usec >> pps_shift);
13837c478bd9Sstevel@tonic-gate 	else
13847c478bd9Sstevel@tonic-gate 		v_usec = v_usec >> pps_shift;
13857c478bd9Sstevel@tonic-gate 	pps_usec = u_usec;
13867c478bd9Sstevel@tonic-gate 	cal_sec = tvp->tv_sec;
13877c478bd9Sstevel@tonic-gate 	cal_usec = tvp->tv_usec;
13887c478bd9Sstevel@tonic-gate 	cal_sec -= pps_time.tv_sec;
13897c478bd9Sstevel@tonic-gate 	cal_usec -= pps_time.tv_usec;
13907c478bd9Sstevel@tonic-gate 	if (cal_usec < 0) {
13917c478bd9Sstevel@tonic-gate 		cal_usec += MICROSEC;
13927c478bd9Sstevel@tonic-gate 		cal_sec--;
13937c478bd9Sstevel@tonic-gate 	}
13947c478bd9Sstevel@tonic-gate 	pps_time = *tvp;
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	/*
13977c478bd9Sstevel@tonic-gate 	 * Check for lost interrupts, noise, excessive jitter and
13987c478bd9Sstevel@tonic-gate 	 * excessive frequency error. The number of timer ticks during
13997c478bd9Sstevel@tonic-gate 	 * the interval may vary +-1 tick. Add to this a margin of one
14007c478bd9Sstevel@tonic-gate 	 * tick for the PPS signal jitter and maximum frequency
14017c478bd9Sstevel@tonic-gate 	 * deviation. If the limits are exceeded, the calibration
14027c478bd9Sstevel@tonic-gate 	 * interval is reset to the minimum and we start over.
14037c478bd9Sstevel@tonic-gate 	 */
14047c478bd9Sstevel@tonic-gate 	u_usec = (int)usec_per_tick << 1;
14057c478bd9Sstevel@tonic-gate 	if (!((cal_sec == -1 && cal_usec > (MICROSEC - u_usec)) ||
14067c478bd9Sstevel@tonic-gate 	    (cal_sec == 0 && cal_usec < u_usec)) ||
14077c478bd9Sstevel@tonic-gate 	    v_usec > time_tolerance || v_usec < -time_tolerance) {
14087c478bd9Sstevel@tonic-gate 		pps_errcnt++;
14097c478bd9Sstevel@tonic-gate 		pps_shift = PPS_SHIFT;
14107c478bd9Sstevel@tonic-gate 		pps_intcnt = 0;
14117c478bd9Sstevel@tonic-gate 		time_status |= STA_PPSERROR;
14127c478bd9Sstevel@tonic-gate 		return;
14137c478bd9Sstevel@tonic-gate 	}
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	/*
14167c478bd9Sstevel@tonic-gate 	 * A three-stage median filter is used to help deglitch the pps
14177c478bd9Sstevel@tonic-gate 	 * frequency. The median sample becomes the frequency offset
14187c478bd9Sstevel@tonic-gate 	 * estimate; the difference between the other two samples
14197c478bd9Sstevel@tonic-gate 	 * becomes the frequency dispersion (stability) estimate.
14207c478bd9Sstevel@tonic-gate 	 */
14217c478bd9Sstevel@tonic-gate 	pps_ff[2] = pps_ff[1];
14227c478bd9Sstevel@tonic-gate 	pps_ff[1] = pps_ff[0];
14237c478bd9Sstevel@tonic-gate 	pps_ff[0] = v_usec;
14247c478bd9Sstevel@tonic-gate 	if (pps_ff[0] > pps_ff[1]) {
14257c478bd9Sstevel@tonic-gate 		if (pps_ff[1] > pps_ff[2]) {
14267c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[1];		/* 0 1 2 */
14277c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[0] - pps_ff[2];
14287c478bd9Sstevel@tonic-gate 		} else if (pps_ff[2] > pps_ff[0]) {
14297c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[0];		/* 2 0 1 */
14307c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[2] - pps_ff[1];
14317c478bd9Sstevel@tonic-gate 		} else {
14327c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[2];		/* 0 2 1 */
14337c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[0] - pps_ff[1];
14347c478bd9Sstevel@tonic-gate 		}
14357c478bd9Sstevel@tonic-gate 	} else {
14367c478bd9Sstevel@tonic-gate 		if (pps_ff[1] < pps_ff[2]) {
14377c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[1];		/* 2 1 0 */
14387c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[2] - pps_ff[0];
14397c478bd9Sstevel@tonic-gate 		} else  if (pps_ff[2] < pps_ff[0]) {
14407c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[0];		/* 1 0 2 */
14417c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[1] - pps_ff[2];
14427c478bd9Sstevel@tonic-gate 		} else {
14437c478bd9Sstevel@tonic-gate 			u_usec = pps_ff[2];		/* 1 2 0 */
14447c478bd9Sstevel@tonic-gate 			v_usec = pps_ff[1] - pps_ff[0];
14457c478bd9Sstevel@tonic-gate 		}
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	/*
14497c478bd9Sstevel@tonic-gate 	 * Here the frequency dispersion (stability) is updated. If it
14507c478bd9Sstevel@tonic-gate 	 * is less than one-fourth the maximum (MAXFREQ), the frequency
14517c478bd9Sstevel@tonic-gate 	 * offset is updated as well, but clamped to the tolerance. It
14527c478bd9Sstevel@tonic-gate 	 * will be processed later by the clock() routine.
14537c478bd9Sstevel@tonic-gate 	 */
14547c478bd9Sstevel@tonic-gate 	v_usec = (v_usec >> 1) - pps_stabil;
14557c478bd9Sstevel@tonic-gate 	if (v_usec < 0)
14567c478bd9Sstevel@tonic-gate 		pps_stabil -= -v_usec >> PPS_AVG;
14577c478bd9Sstevel@tonic-gate 	else
14587c478bd9Sstevel@tonic-gate 		pps_stabil += v_usec >> PPS_AVG;
14597c478bd9Sstevel@tonic-gate 	if (pps_stabil > MAXFREQ >> 2) {
14607c478bd9Sstevel@tonic-gate 		pps_stbcnt++;
14617c478bd9Sstevel@tonic-gate 		time_status |= STA_PPSWANDER;
14627c478bd9Sstevel@tonic-gate 		return;
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 	if (time_status & STA_PPSFREQ) {
14657c478bd9Sstevel@tonic-gate 		if (u_usec < 0) {
14667c478bd9Sstevel@tonic-gate 			pps_freq -= -u_usec >> PPS_AVG;
14677c478bd9Sstevel@tonic-gate 			if (pps_freq < -time_tolerance)
14687c478bd9Sstevel@tonic-gate 				pps_freq = -time_tolerance;
14697c478bd9Sstevel@tonic-gate 			u_usec = -u_usec;
14707c478bd9Sstevel@tonic-gate 		} else {
14717c478bd9Sstevel@tonic-gate 			pps_freq += u_usec >> PPS_AVG;
14727c478bd9Sstevel@tonic-gate 			if (pps_freq > time_tolerance)
14737c478bd9Sstevel@tonic-gate 				pps_freq = time_tolerance;
14747c478bd9Sstevel@tonic-gate 		}
14757c478bd9Sstevel@tonic-gate 	}
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	/*
14787c478bd9Sstevel@tonic-gate 	 * Here the calibration interval is adjusted. If the maximum
14797c478bd9Sstevel@tonic-gate 	 * time difference is greater than tick / 4, reduce the interval
14807c478bd9Sstevel@tonic-gate 	 * by half. If this is not the case for four consecutive
14817c478bd9Sstevel@tonic-gate 	 * intervals, double the interval.
14827c478bd9Sstevel@tonic-gate 	 */
14837c478bd9Sstevel@tonic-gate 	if (u_usec << pps_shift > bigtick >> 2) {
14847c478bd9Sstevel@tonic-gate 		pps_intcnt = 0;
14857c478bd9Sstevel@tonic-gate 		if (pps_shift > PPS_SHIFT)
14867c478bd9Sstevel@tonic-gate 			pps_shift--;
14877c478bd9Sstevel@tonic-gate 	} else if (pps_intcnt >= 4) {
14887c478bd9Sstevel@tonic-gate 		pps_intcnt = 0;
14897c478bd9Sstevel@tonic-gate 		if (pps_shift < PPS_SHIFTMAX)
14907c478bd9Sstevel@tonic-gate 			pps_shift++;
14917c478bd9Sstevel@tonic-gate 	} else
14927c478bd9Sstevel@tonic-gate 		pps_intcnt++;
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	/*
14957c478bd9Sstevel@tonic-gate 	 * If recovering from kmdb, then make sure the tod chip gets resynced.
14967c478bd9Sstevel@tonic-gate 	 * If we took an early exit above, then we don't yet have a stable
14977c478bd9Sstevel@tonic-gate 	 * calibration signal to lock onto, so don't mark the tod for sync
14987c478bd9Sstevel@tonic-gate 	 * until we get all the way here.
14997c478bd9Sstevel@tonic-gate 	 */
15007c478bd9Sstevel@tonic-gate 	{
15017c478bd9Sstevel@tonic-gate 		int s = hr_clock_lock();
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 		tod_needsync = 1;
15047c478bd9Sstevel@tonic-gate 		hr_clock_unlock(s);
15057c478bd9Sstevel@tonic-gate 	}
15067c478bd9Sstevel@tonic-gate }
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate /*
15097c478bd9Sstevel@tonic-gate  * Handle clock tick processing for a thread.
15107c478bd9Sstevel@tonic-gate  * Check for timer action, enforce CPU rlimit, do profiling etc.
15117c478bd9Sstevel@tonic-gate  */
15127c478bd9Sstevel@tonic-gate void
clock_tick(kthread_t * t,int pending)15132850d85bSmv clock_tick(kthread_t *t, int pending)
15147c478bd9Sstevel@tonic-gate {
15157c478bd9Sstevel@tonic-gate 	struct proc *pp;
15167c478bd9Sstevel@tonic-gate 	klwp_id_t    lwp;
15177c478bd9Sstevel@tonic-gate 	struct as *as;
15182850d85bSmv 	clock_t	ticks;
15197c478bd9Sstevel@tonic-gate 	int	poke = 0;		/* notify another CPU */
15207c478bd9Sstevel@tonic-gate 	int	user_mode;
15217c478bd9Sstevel@tonic-gate 	size_t	 rss;
15222850d85bSmv 	int i, total_usec, usec;
15232850d85bSmv 	rctl_qty_t secs;
15242850d85bSmv 
15252850d85bSmv 	ASSERT(pending > 0);
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	/* Must be operating on a lwp/thread */
15287c478bd9Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) == NULL) {
15297c478bd9Sstevel@tonic-gate 		panic("clock_tick: no lwp");
15307c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 
15332850d85bSmv 	for (i = 0; i < pending; i++) {
15342850d85bSmv 		CL_TICK(t);	/* Class specific tick processing */
15352850d85bSmv 		DTRACE_SCHED1(tick, kthread_t *, t);
15362850d85bSmv 	}
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	pp = ttoproc(t);
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	/* pp->p_lock makes sure that the thread does not exit */
15417c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pp->p_lock));
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	user_mode = (lwp->lwp_state == LWP_USER);
15447c478bd9Sstevel@tonic-gate 
15452850d85bSmv 	ticks = (pp->p_utime + pp->p_stime) % hz;
15467c478bd9Sstevel@tonic-gate 	/*
15477c478bd9Sstevel@tonic-gate 	 * Update process times. Should use high res clock and state
15487c478bd9Sstevel@tonic-gate 	 * changes instead of statistical sampling method. XXX
15497c478bd9Sstevel@tonic-gate 	 */
15507c478bd9Sstevel@tonic-gate 	if (user_mode) {
15512850d85bSmv 		pp->p_utime += pending;
15527c478bd9Sstevel@tonic-gate 	} else {
15532850d85bSmv 		pp->p_stime += pending;
15547c478bd9Sstevel@tonic-gate 	}
15552850d85bSmv 
15562850d85bSmv 	pp->p_ttime += pending;
15577c478bd9Sstevel@tonic-gate 	as = pp->p_as;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	/*
15607c478bd9Sstevel@tonic-gate 	 * Update user profiling statistics. Get the pc from the
15617c478bd9Sstevel@tonic-gate 	 * lwp when the AST happens.
15627c478bd9Sstevel@tonic-gate 	 */
15637c478bd9Sstevel@tonic-gate 	if (pp->p_prof.pr_scale) {
15642850d85bSmv 		atomic_add_32(&lwp->lwp_oweupc, (int32_t)pending);
15657c478bd9Sstevel@tonic-gate 		if (user_mode) {
15667c478bd9Sstevel@tonic-gate 			poke = 1;
15677c478bd9Sstevel@tonic-gate 			aston(t);
15687c478bd9Sstevel@tonic-gate 		}
15697c478bd9Sstevel@tonic-gate 	}
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	/*
15727c478bd9Sstevel@tonic-gate 	 * If CPU was in user state, process lwp-virtual time
15732850d85bSmv 	 * interval timer. The value passed to itimerdecr() has to be
15742850d85bSmv 	 * in microseconds and has to be less than one second. Hence
15752850d85bSmv 	 * this loop.
15767c478bd9Sstevel@tonic-gate 	 */
15772850d85bSmv 	total_usec = usec_per_tick * pending;
15782850d85bSmv 	while (total_usec > 0) {
15792850d85bSmv 		usec = MIN(total_usec, (MICROSEC - 1));
15802850d85bSmv 		if (user_mode &&
15812850d85bSmv 		    timerisset(&lwp->lwp_timer[ITIMER_VIRTUAL].it_value) &&
15822850d85bSmv 		    itimerdecr(&lwp->lwp_timer[ITIMER_VIRTUAL], usec) == 0) {
15832850d85bSmv 			poke = 1;
15842850d85bSmv 			sigtoproc(pp, t, SIGVTALRM);
15852850d85bSmv 		}
15862850d85bSmv 		total_usec -= usec;
15877c478bd9Sstevel@tonic-gate 	}
15887c478bd9Sstevel@tonic-gate 
15892850d85bSmv 	/*
15902850d85bSmv 	 * If CPU was in user state, process lwp-profile
15912850d85bSmv 	 * interval timer.
15922850d85bSmv 	 */
15932850d85bSmv 	total_usec = usec_per_tick * pending;
15942850d85bSmv 	while (total_usec > 0) {
15952850d85bSmv 		usec = MIN(total_usec, (MICROSEC - 1));
15962850d85bSmv 		if (timerisset(&lwp->lwp_timer[ITIMER_PROF].it_value) &&
15972850d85bSmv 		    itimerdecr(&lwp->lwp_timer[ITIMER_PROF], usec) == 0) {
15982850d85bSmv 			poke = 1;
15992850d85bSmv 			sigtoproc(pp, t, SIGPROF);
16002850d85bSmv 		}
16012850d85bSmv 		total_usec -= usec;
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	/*
16057c478bd9Sstevel@tonic-gate 	 * Enforce CPU resource controls:
16067c478bd9Sstevel@tonic-gate 	 *   (a) process.max-cpu-time resource control
16072850d85bSmv 	 *
16082850d85bSmv 	 * Perform the check only if we have accumulated more a second.
16097c478bd9Sstevel@tonic-gate 	 */
16102850d85bSmv 	if ((ticks + pending) >= hz) {
16112850d85bSmv 		(void) rctl_test(rctlproc_legacy[RLIMIT_CPU], pp->p_rctls, pp,
16122850d85bSmv 		    (pp->p_utime + pp->p_stime)/hz, RCA_UNSAFE_SIGINFO);
16132850d85bSmv 	}
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	/*
16167c478bd9Sstevel@tonic-gate 	 *   (b) task.max-cpu-time resource control
16172850d85bSmv 	 *
16182850d85bSmv 	 * If we have accumulated enough ticks, increment the task CPU
16192850d85bSmv 	 * time usage and test for the resource limit. This minimizes the
16202850d85bSmv 	 * number of calls to the rct_test(). The task CPU time mutex
16212850d85bSmv 	 * is highly contentious as many processes can be sharing a task.
16227c478bd9Sstevel@tonic-gate 	 */
16232850d85bSmv 	if (pp->p_ttime >= clock_tick_proc_max) {
16242850d85bSmv 		secs = task_cpu_time_incr(pp->p_task, pp->p_ttime);
16252850d85bSmv 		pp->p_ttime = 0;
16262850d85bSmv 		if (secs) {
16272850d85bSmv 			(void) rctl_test(rc_task_cpu_time, pp->p_task->tk_rctls,
16282850d85bSmv 			    pp, secs, RCA_UNSAFE_SIGINFO);
16292850d85bSmv 		}
16302850d85bSmv 	}
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	/*
16337c478bd9Sstevel@tonic-gate 	 * Update memory usage for the currently running process.
16347c478bd9Sstevel@tonic-gate 	 */
16357c478bd9Sstevel@tonic-gate 	rss = rm_asrss(as);
16367c478bd9Sstevel@tonic-gate 	PTOU(pp)->u_mem += rss;
16377c478bd9Sstevel@tonic-gate 	if (rss > PTOU(pp)->u_mem_max)
16387c478bd9Sstevel@tonic-gate 		PTOU(pp)->u_mem_max = rss;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	/*
16417c478bd9Sstevel@tonic-gate 	 * Notify the CPU the thread is running on.
16427c478bd9Sstevel@tonic-gate 	 */
16437c478bd9Sstevel@tonic-gate 	if (poke && t->t_cpu != CPU)
16447c478bd9Sstevel@tonic-gate 		poke_cpu(t->t_cpu->cpu_id);
16457c478bd9Sstevel@tonic-gate }
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate void
profil_tick(uintptr_t upc)16487c478bd9Sstevel@tonic-gate profil_tick(uintptr_t upc)
16497c478bd9Sstevel@tonic-gate {
16507c478bd9Sstevel@tonic-gate 	int ticks;
16517c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
16527c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
16537c478bd9Sstevel@tonic-gate 	struct prof *pr = &p->p_prof;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	do {
16567c478bd9Sstevel@tonic-gate 		ticks = lwp->lwp_oweupc;
165775d94465SJosef 'Jeff' Sipek 	} while (atomic_cas_32(&lwp->lwp_oweupc, ticks, 0) != ticks);
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_pflock);
16607c478bd9Sstevel@tonic-gate 	if (pr->pr_scale >= 2 && upc >= pr->pr_off) {
16617c478bd9Sstevel@tonic-gate 		/*
16627c478bd9Sstevel@tonic-gate 		 * Old-style profiling
16637c478bd9Sstevel@tonic-gate 		 */
16647c478bd9Sstevel@tonic-gate 		uint16_t *slot = pr->pr_base;
16657c478bd9Sstevel@tonic-gate 		uint16_t old, new;
16667c478bd9Sstevel@tonic-gate 		if (pr->pr_scale != 2) {
16677c478bd9Sstevel@tonic-gate 			uintptr_t delta = upc - pr->pr_off;
16687c478bd9Sstevel@tonic-gate 			uintptr_t byteoff = ((delta >> 16) * pr->pr_scale) +
16697c478bd9Sstevel@tonic-gate 			    (((delta & 0xffff) * pr->pr_scale) >> 16);
16707c478bd9Sstevel@tonic-gate 			if (byteoff >= (uintptr_t)pr->pr_size) {
16717c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_pflock);
16727c478bd9Sstevel@tonic-gate 				return;
16737c478bd9Sstevel@tonic-gate 			}
16747c478bd9Sstevel@tonic-gate 			slot += byteoff / sizeof (uint16_t);
16757c478bd9Sstevel@tonic-gate 		}
16767c478bd9Sstevel@tonic-gate 		if (fuword16(slot, &old) < 0 ||
16777c478bd9Sstevel@tonic-gate 		    (new = old + ticks) > SHRT_MAX ||
16787c478bd9Sstevel@tonic-gate 		    suword16(slot, new) < 0) {
16797c478bd9Sstevel@tonic-gate 			pr->pr_scale = 0;
16807c478bd9Sstevel@tonic-gate 		}
16817c478bd9Sstevel@tonic-gate 	} else if (pr->pr_scale == 1) {
16827c478bd9Sstevel@tonic-gate 		/*
16837c478bd9Sstevel@tonic-gate 		 * PC Sampling
16847c478bd9Sstevel@tonic-gate 		 */
16857c478bd9Sstevel@tonic-gate 		model_t model = lwp_getdatamodel(lwp);
16867c478bd9Sstevel@tonic-gate 		int result;
16877c478bd9Sstevel@tonic-gate #ifdef __lint
16887c478bd9Sstevel@tonic-gate 		model = model;
16897c478bd9Sstevel@tonic-gate #endif
16907c478bd9Sstevel@tonic-gate 		while (ticks-- > 0) {
16917c478bd9Sstevel@tonic-gate 			if (pr->pr_samples == pr->pr_size) {
16927c478bd9Sstevel@tonic-gate 				/* buffer full, turn off sampling */
16937c478bd9Sstevel@tonic-gate 				pr->pr_scale = 0;
16947c478bd9Sstevel@tonic-gate 				break;
16957c478bd9Sstevel@tonic-gate 			}
16967c478bd9Sstevel@tonic-gate 			switch (SIZEOF_PTR(model)) {
16977c478bd9Sstevel@tonic-gate 			case sizeof (uint32_t):
16987c478bd9Sstevel@tonic-gate 				result = suword32(pr->pr_base, (uint32_t)upc);
16997c478bd9Sstevel@tonic-gate 				break;
17007c478bd9Sstevel@tonic-gate #ifdef _LP64
17017c478bd9Sstevel@tonic-gate 			case sizeof (uint64_t):
17027c478bd9Sstevel@tonic-gate 				result = suword64(pr->pr_base, (uint64_t)upc);
17037c478bd9Sstevel@tonic-gate 				break;
17047c478bd9Sstevel@tonic-gate #endif
17057c478bd9Sstevel@tonic-gate 			default:
17067c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "profil_tick: unexpected "
17077c478bd9Sstevel@tonic-gate 				    "data model");
17087c478bd9Sstevel@tonic-gate 				result = -1;
17097c478bd9Sstevel@tonic-gate 				break;
17107c478bd9Sstevel@tonic-gate 			}
17117c478bd9Sstevel@tonic-gate 			if (result != 0) {
17127c478bd9Sstevel@tonic-gate 				pr->pr_scale = 0;
17137c478bd9Sstevel@tonic-gate 				break;
17147c478bd9Sstevel@tonic-gate 			}
17157c478bd9Sstevel@tonic-gate 			pr->pr_base = (caddr_t)pr->pr_base + SIZEOF_PTR(model);
17167c478bd9Sstevel@tonic-gate 			pr->pr_samples++;
17177c478bd9Sstevel@tonic-gate 		}
17187c478bd9Sstevel@tonic-gate 	}
17197c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_pflock);
17207c478bd9Sstevel@tonic-gate }
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate static void
delay_wakeup(void * arg)17237c478bd9Sstevel@tonic-gate delay_wakeup(void *arg)
17247c478bd9Sstevel@tonic-gate {
17254c06356bSdh 	kthread_t	*t = arg;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	mutex_enter(&t->t_delay_lock);
17287c478bd9Sstevel@tonic-gate 	cv_signal(&t->t_delay_cv);
17297c478bd9Sstevel@tonic-gate 	mutex_exit(&t->t_delay_lock);
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate 
17324c06356bSdh /*
17334c06356bSdh  * The delay(9F) man page indicates that it can only be called from user or
17344c06356bSdh  * kernel context - detect and diagnose bad calls. The following macro will
17354c06356bSdh  * produce a limited number of messages identifying bad callers.  This is done
17364c06356bSdh  * in a macro so that caller() is meaningful. When a bad caller is identified,
17374c06356bSdh  * switching to 'drv_usecwait(TICK_TO_USEC(ticks));' may be appropriate.
17384c06356bSdh  */
17394c06356bSdh #define	DELAY_CONTEXT_CHECK()	{					\
17404c06356bSdh 	uint32_t	m;						\
17414c06356bSdh 	char		*f;						\
17424c06356bSdh 	ulong_t		off;						\
17434c06356bSdh 									\
17444c06356bSdh 	m = delay_from_interrupt_msg;					\
17454c06356bSdh 	if (delay_from_interrupt_diagnose && servicing_interrupt() &&	\
17464c06356bSdh 	    !panicstr && !devinfo_freeze &&				\
17474c06356bSdh 	    atomic_cas_32(&delay_from_interrupt_msg, m ? m : 1, m-1)) {	\
17484c06356bSdh 		f = modgetsymname((uintptr_t)caller(), &off);		\
17494c06356bSdh 		cmn_err(CE_WARN, "delay(9F) called from "		\
17504c06356bSdh 		    "interrupt context: %s`%s",				\
17514c06356bSdh 		    mod_containing_pc(caller()), f ? f : "...");	\
17524c06356bSdh 	}								\
17534c06356bSdh }
17547c478bd9Sstevel@tonic-gate 
17554c06356bSdh /*
17564c06356bSdh  * delay_common: common delay code.
17574c06356bSdh  */
17584c06356bSdh static void
delay_common(clock_t ticks)17594c06356bSdh delay_common(clock_t ticks)
17604c06356bSdh {
17614c06356bSdh 	kthread_t	*t = curthread;
17624c06356bSdh 	clock_t		deadline;
17634c06356bSdh 	clock_t		timeleft;
17644c06356bSdh 	callout_id_t	id;
17654c06356bSdh 
17664c06356bSdh 	/* If timeouts aren't running all we can do is spin. */
17674c06356bSdh 	if (panicstr || devinfo_freeze) {
17684c06356bSdh 		/* Convert delay(9F) call into drv_usecwait(9F) call. */
17694c06356bSdh 		if (ticks > 0)
17704c06356bSdh 			drv_usecwait(TICK_TO_USEC(ticks));
17717c478bd9Sstevel@tonic-gate 		return;
17727c478bd9Sstevel@tonic-gate 	}
17737c478bd9Sstevel@tonic-gate 
1774d3d50737SRafael Vanoni 	deadline = ddi_get_lbolt() + ticks;
1775d3d50737SRafael Vanoni 	while ((timeleft = deadline - ddi_get_lbolt()) > 0) {
17767c478bd9Sstevel@tonic-gate 		mutex_enter(&t->t_delay_lock);
17774c06356bSdh 		id = timeout_default(delay_wakeup, t, timeleft);
17787c478bd9Sstevel@tonic-gate 		cv_wait(&t->t_delay_cv, &t->t_delay_lock);
17797c478bd9Sstevel@tonic-gate 		mutex_exit(&t->t_delay_lock);
17804c06356bSdh 		(void) untimeout_default(id, 0);
17817c478bd9Sstevel@tonic-gate 	}
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate 
17844c06356bSdh /*
17854c06356bSdh  * Delay specified number of clock ticks.
17864c06356bSdh  */
17874c06356bSdh void
delay(clock_t ticks)17884c06356bSdh delay(clock_t ticks)
17894c06356bSdh {
17904c06356bSdh 	DELAY_CONTEXT_CHECK();
17914c06356bSdh 
17924c06356bSdh 	delay_common(ticks);
17934c06356bSdh }
17944c06356bSdh 
17954c06356bSdh /*
17964c06356bSdh  * Delay a random number of clock ticks between 1 and ticks.
17974c06356bSdh  */
17984c06356bSdh void
delay_random(clock_t ticks)17994c06356bSdh delay_random(clock_t ticks)
18004c06356bSdh {
18014c06356bSdh 	int	r;
18024c06356bSdh 
18034c06356bSdh 	DELAY_CONTEXT_CHECK();
18044c06356bSdh 
18054c06356bSdh 	(void) random_get_pseudo_bytes((void *)&r, sizeof (r));
18064c06356bSdh 	if (ticks == 0)
18074c06356bSdh 		ticks = 1;
18084c06356bSdh 	ticks = (r % ticks) + 1;
18094c06356bSdh 	delay_common(ticks);
18104c06356bSdh }
18114c06356bSdh 
18127c478bd9Sstevel@tonic-gate /*
18137c478bd9Sstevel@tonic-gate  * Like delay, but interruptible by a signal.
18147c478bd9Sstevel@tonic-gate  */
18157c478bd9Sstevel@tonic-gate int
delay_sig(clock_t ticks)18167c478bd9Sstevel@tonic-gate delay_sig(clock_t ticks)
18177c478bd9Sstevel@tonic-gate {
18184c06356bSdh 	kthread_t	*t = curthread;
18194c06356bSdh 	clock_t		deadline;
18204c06356bSdh 	clock_t		rc;
18214c06356bSdh 
18224c06356bSdh 	/* If timeouts aren't running all we can do is spin. */
18234c06356bSdh 	if (panicstr || devinfo_freeze) {
18244c06356bSdh 		if (ticks > 0)
18254c06356bSdh 			drv_usecwait(TICK_TO_USEC(ticks));
18264c06356bSdh 		return (0);
18274c06356bSdh 	}
18287c478bd9Sstevel@tonic-gate 
1829d3d50737SRafael Vanoni 	deadline = ddi_get_lbolt() + ticks;
18304c06356bSdh 	mutex_enter(&t->t_delay_lock);
18317c478bd9Sstevel@tonic-gate 	do {
18324c06356bSdh 		rc = cv_timedwait_sig(&t->t_delay_cv,
18334c06356bSdh 		    &t->t_delay_lock, deadline);
18344c06356bSdh 		/* loop until past deadline or signaled */
18357c478bd9Sstevel@tonic-gate 	} while (rc > 0);
18364c06356bSdh 	mutex_exit(&t->t_delay_lock);
18377c478bd9Sstevel@tonic-gate 	if (rc == 0)
18387c478bd9Sstevel@tonic-gate 		return (EINTR);
18397c478bd9Sstevel@tonic-gate 	return (0);
18407c478bd9Sstevel@tonic-gate }
18417c478bd9Sstevel@tonic-gate 
18424c06356bSdh 
18437c478bd9Sstevel@tonic-gate #define	SECONDS_PER_DAY 86400
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate /*
18467c478bd9Sstevel@tonic-gate  * Initialize the system time based on the TOD chip.  approx is used as
18477c478bd9Sstevel@tonic-gate  * an approximation of time (e.g. from the filesystem) in the event that
18487c478bd9Sstevel@tonic-gate  * the TOD chip has been cleared or is unresponsive.  An approx of -1
18497c478bd9Sstevel@tonic-gate  * means the filesystem doesn't keep time.
18507c478bd9Sstevel@tonic-gate  */
18517c478bd9Sstevel@tonic-gate void
clkset(time_t approx)18527c478bd9Sstevel@tonic-gate clkset(time_t approx)
18537c478bd9Sstevel@tonic-gate {
18547c478bd9Sstevel@tonic-gate 	timestruc_t ts;
18557c478bd9Sstevel@tonic-gate 	int spl;
18567c478bd9Sstevel@tonic-gate 	int set_clock = 0;
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	mutex_enter(&tod_lock);
18597c478bd9Sstevel@tonic-gate 	ts = tod_get();
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	if (ts.tv_sec > 365 * SECONDS_PER_DAY) {
18627c478bd9Sstevel@tonic-gate 		/*
18637c478bd9Sstevel@tonic-gate 		 * If the TOD chip is reporting some time after 1971,
18647c478bd9Sstevel@tonic-gate 		 * then it probably didn't lose power or become otherwise
18657c478bd9Sstevel@tonic-gate 		 * cleared in the recent past;  check to assure that
18667c478bd9Sstevel@tonic-gate 		 * the time coming from the filesystem isn't in the future
18677c478bd9Sstevel@tonic-gate 		 * according to the TOD chip.
18687c478bd9Sstevel@tonic-gate 		 */
18697c478bd9Sstevel@tonic-gate 		if (approx != -1 && approx > ts.tv_sec) {
18707c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Last shutdown is later "
18717c478bd9Sstevel@tonic-gate 			    "than time on time-of-day chip; check date.");
18727c478bd9Sstevel@tonic-gate 		}
18737c478bd9Sstevel@tonic-gate 	} else {
18747c478bd9Sstevel@tonic-gate 		/*
1875a07a2431SKrishnendu Sadhukhan - Sun Microsystems 		 * If the TOD chip isn't giving correct time, set it to the
1876a07a2431SKrishnendu Sadhukhan - Sun Microsystems 		 * greater of i) approx and ii) 1987. That way if approx
1877a07a2431SKrishnendu Sadhukhan - Sun Microsystems 		 * is negative or is earlier than 1987, we set the clock
1878a07a2431SKrishnendu Sadhukhan - Sun Microsystems 		 * back to a time when Oliver North, ALF and Dire Straits
1879a07a2431SKrishnendu Sadhukhan - Sun Microsystems 		 * were all on the collective brain:  1987.
18807c478bd9Sstevel@tonic-gate 		 */
18817c478bd9Sstevel@tonic-gate 		timestruc_t tmp;
1882a07a2431SKrishnendu Sadhukhan - Sun Microsystems 		time_t diagnose_date = (1987 - 1970) * 365 * SECONDS_PER_DAY;
1883a07a2431SKrishnendu Sadhukhan - Sun Microsystems 		ts.tv_sec = (approx > diagnose_date ? approx : diagnose_date);
18847c478bd9Sstevel@tonic-gate 		ts.tv_nsec = 0;
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 		/*
18877c478bd9Sstevel@tonic-gate 		 * Attempt to write the new time to the TOD chip.  Set spl high
18887c478bd9Sstevel@tonic-gate 		 * to avoid getting preempted between the tod_set and tod_get.
18897c478bd9Sstevel@tonic-gate 		 */
18907c478bd9Sstevel@tonic-gate 		spl = splhi();
18917c478bd9Sstevel@tonic-gate 		tod_set(ts);
18927c478bd9Sstevel@tonic-gate 		tmp = tod_get();
18937c478bd9Sstevel@tonic-gate 		splx(spl);
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 		if (tmp.tv_sec != ts.tv_sec && tmp.tv_sec != ts.tv_sec + 1) {
18967c478bd9Sstevel@tonic-gate 			tod_broken = 1;
18977c478bd9Sstevel@tonic-gate 			dosynctodr = 0;
1898a07a2431SKrishnendu Sadhukhan - Sun Microsystems 			cmn_err(CE_WARN, "Time-of-day chip unresponsive.");
18997c478bd9Sstevel@tonic-gate 		} else {
19007c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Time-of-day chip had "
19017c478bd9Sstevel@tonic-gate 			    "incorrect date; check and reset.");
19027c478bd9Sstevel@tonic-gate 		}
19037c478bd9Sstevel@tonic-gate 		set_clock = 1;
19047c478bd9Sstevel@tonic-gate 	}
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	if (!boot_time) {
19077c478bd9Sstevel@tonic-gate 		boot_time = ts.tv_sec;
1908*9a1586dbSAndy Fiddaman 		global_zone->zone_boot_time = ts.tv_sec;
19097c478bd9Sstevel@tonic-gate 		set_clock = 1;
19107c478bd9Sstevel@tonic-gate 	}
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 	if (set_clock)
19137c478bd9Sstevel@tonic-gate 		set_hrestime(&ts);
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	mutex_exit(&tod_lock);
19167c478bd9Sstevel@tonic-gate }
19177c478bd9Sstevel@tonic-gate 
19183348528fSdm int	timechanged;	/* for testing if the system time has been reset */
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate void
set_hrestime(timestruc_t * ts)19217c478bd9Sstevel@tonic-gate set_hrestime(timestruc_t *ts)
19227c478bd9Sstevel@tonic-gate {
19237c478bd9Sstevel@tonic-gate 	int spl = hr_clock_lock();
19247c478bd9Sstevel@tonic-gate 	hrestime = *ts;
19253348528fSdm 	membar_enter();	/* hrestime must be visible before timechanged++ */
19267c478bd9Sstevel@tonic-gate 	timedelta = 0;
19273348528fSdm 	timechanged++;
19287c478bd9Sstevel@tonic-gate 	hr_clock_unlock(spl);
192987a18d3fSMadhavan Venkataraman 	callout_hrestime();
19307c478bd9Sstevel@tonic-gate }
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate static uint_t deadman_seconds;
19337c478bd9Sstevel@tonic-gate static uint32_t deadman_panics;
19347c478bd9Sstevel@tonic-gate static int deadman_enabled = 0;
19357c478bd9Sstevel@tonic-gate static int deadman_panic_timers = 1;
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate static void
deadman(void)19387c478bd9Sstevel@tonic-gate deadman(void)
19397c478bd9Sstevel@tonic-gate {
19407c478bd9Sstevel@tonic-gate 	if (panicstr) {
19417c478bd9Sstevel@tonic-gate 		/*
19427c478bd9Sstevel@tonic-gate 		 * During panic, other CPUs besides the panic
19437c478bd9Sstevel@tonic-gate 		 * master continue to handle cyclics and some other
19447c478bd9Sstevel@tonic-gate 		 * interrupts.  The code below is intended to be
19457c478bd9Sstevel@tonic-gate 		 * single threaded, so any CPU other than the master
19467c478bd9Sstevel@tonic-gate 		 * must keep out.
19477c478bd9Sstevel@tonic-gate 		 */
19487c478bd9Sstevel@tonic-gate 		if (CPU->cpu_id != panic_cpu.cpu_id)
19497c478bd9Sstevel@tonic-gate 			return;
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 		if (!deadman_panic_timers)
19527c478bd9Sstevel@tonic-gate 			return; /* allow all timers to be manually disabled */
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 		/*
19557c478bd9Sstevel@tonic-gate 		 * If we are generating a crash dump or syncing filesystems and
19567c478bd9Sstevel@tonic-gate 		 * the corresponding timer is set, decrement it and re-enter
19577c478bd9Sstevel@tonic-gate 		 * the panic code to abort it and advance to the next state.
19587c478bd9Sstevel@tonic-gate 		 * The panic states and triggers are explained in panic.c.
19597c478bd9Sstevel@tonic-gate 		 */
19607c478bd9Sstevel@tonic-gate 		if (panic_dump) {
19617c478bd9Sstevel@tonic-gate 			if (dump_timeleft && (--dump_timeleft == 0)) {
19627c478bd9Sstevel@tonic-gate 				panic("panic dump timeout");
19637c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
19647c478bd9Sstevel@tonic-gate 			}
19657c478bd9Sstevel@tonic-gate 		}
19667c478bd9Sstevel@tonic-gate 		return;
19677c478bd9Sstevel@tonic-gate 	}
19687c478bd9Sstevel@tonic-gate 
1969d3d50737SRafael Vanoni 	if (deadman_counter != CPU->cpu_deadman_counter) {
1970d3d50737SRafael Vanoni 		CPU->cpu_deadman_counter = deadman_counter;
19717c478bd9Sstevel@tonic-gate 		CPU->cpu_deadman_countdown = deadman_seconds;
19727c478bd9Sstevel@tonic-gate 		return;
19737c478bd9Sstevel@tonic-gate 	}
19747c478bd9Sstevel@tonic-gate 
197571368153Svb 	if (--CPU->cpu_deadman_countdown > 0)
19767c478bd9Sstevel@tonic-gate 		return;
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 	/*
19797c478bd9Sstevel@tonic-gate 	 * Regardless of whether or not we actually bring the system down,
19807c478bd9Sstevel@tonic-gate 	 * bump the deadman_panics variable.
19817c478bd9Sstevel@tonic-gate 	 *
19827c478bd9Sstevel@tonic-gate 	 * N.B. deadman_panics is incremented once for each CPU that
19837c478bd9Sstevel@tonic-gate 	 * passes through here.  It's expected that all the CPUs will
19847c478bd9Sstevel@tonic-gate 	 * detect this condition within one second of each other, so
19857c478bd9Sstevel@tonic-gate 	 * when deadman_enabled is off, deadman_panics will
19867c478bd9Sstevel@tonic-gate 	 * typically be a multiple of the total number of CPUs in
19877c478bd9Sstevel@tonic-gate 	 * the system.
19887c478bd9Sstevel@tonic-gate 	 */
19891a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&deadman_panics);
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	if (!deadman_enabled) {
19927c478bd9Sstevel@tonic-gate 		CPU->cpu_deadman_countdown = deadman_seconds;
19937c478bd9Sstevel@tonic-gate 		return;
19947c478bd9Sstevel@tonic-gate 	}
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	/*
19977c478bd9Sstevel@tonic-gate 	 * If we're here, we want to bring the system down.
19987c478bd9Sstevel@tonic-gate 	 */
19997c478bd9Sstevel@tonic-gate 	panic("deadman: timed out after %d seconds of clock "
20007c478bd9Sstevel@tonic-gate 	    "inactivity", deadman_seconds);
20017c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
20027c478bd9Sstevel@tonic-gate }
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20057c478bd9Sstevel@tonic-gate static void
deadman_online(void * arg,cpu_t * cpu,cyc_handler_t * hdlr,cyc_time_t * when)20067c478bd9Sstevel@tonic-gate deadman_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
20077c478bd9Sstevel@tonic-gate {
2008d3d50737SRafael Vanoni 	cpu->cpu_deadman_counter = 0;
20097c478bd9Sstevel@tonic-gate 	cpu->cpu_deadman_countdown = deadman_seconds;
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 	hdlr->cyh_func = (cyc_func_t)deadman;
20127c478bd9Sstevel@tonic-gate 	hdlr->cyh_level = CY_HIGH_LEVEL;
20137c478bd9Sstevel@tonic-gate 	hdlr->cyh_arg = NULL;
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 	/*
20167c478bd9Sstevel@tonic-gate 	 * Stagger the CPUs so that they don't all run deadman() at
20177c478bd9Sstevel@tonic-gate 	 * the same time.  Simplest reason to do this is to make it
20187c478bd9Sstevel@tonic-gate 	 * more likely that only one CPU will panic in case of a
20197c478bd9Sstevel@tonic-gate 	 * timeout.  This is (strictly speaking) an aesthetic, not a
20207c478bd9Sstevel@tonic-gate 	 * technical consideration.
20217c478bd9Sstevel@tonic-gate 	 */
20227c478bd9Sstevel@tonic-gate 	when->cyt_when = cpu->cpu_id * (NANOSEC / NCPU);
20237c478bd9Sstevel@tonic-gate 	when->cyt_interval = NANOSEC;
20247c478bd9Sstevel@tonic-gate }
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate void
deadman_init(void)20287c478bd9Sstevel@tonic-gate deadman_init(void)
20297c478bd9Sstevel@tonic-gate {
20307c478bd9Sstevel@tonic-gate 	cyc_omni_handler_t hdlr;
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	if (deadman_seconds == 0)
20337c478bd9Sstevel@tonic-gate 		deadman_seconds = snoop_interval / MICROSEC;
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	if (snooping)
20367c478bd9Sstevel@tonic-gate 		deadman_enabled = 1;
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	hdlr.cyo_online = deadman_online;
20397c478bd9Sstevel@tonic-gate 	hdlr.cyo_offline = NULL;
20407c478bd9Sstevel@tonic-gate 	hdlr.cyo_arg = NULL;
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
20437c478bd9Sstevel@tonic-gate 	deadman_cyclic = cyclic_add_omni(&hdlr);
20447c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
20457c478bd9Sstevel@tonic-gate }
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate /*
20487c478bd9Sstevel@tonic-gate  * tod_fault() is for updating tod validate mechanism state:
20497c478bd9Sstevel@tonic-gate  * (1) TOD_NOFAULT: for resetting the state to 'normal'.
20507c478bd9Sstevel@tonic-gate  *     currently used for debugging only
20517c478bd9Sstevel@tonic-gate  * (2) The following four cases detected by tod validate mechanism:
20527c478bd9Sstevel@tonic-gate  *       TOD_REVERSED: current tod value is less than previous value.
20537c478bd9Sstevel@tonic-gate  *       TOD_STALLED: current tod value hasn't advanced.
20547c478bd9Sstevel@tonic-gate  *       TOD_JUMPED: current tod value advanced too far from previous value.
20557c478bd9Sstevel@tonic-gate  *       TOD_RATECHANGED: the ratio between average tod delta and
20567c478bd9Sstevel@tonic-gate  *       average tick delta has changed.
2057843e1988Sjohnlev  * (3) TOD_RDONLY: when the TOD clock is not writeable e.g. because it is
2058843e1988Sjohnlev  *     a virtual TOD provided by a hypervisor.
20597c478bd9Sstevel@tonic-gate  */
20607c478bd9Sstevel@tonic-gate enum tod_fault_type
tod_fault(enum tod_fault_type ftype,int off)20617c478bd9Sstevel@tonic-gate tod_fault(enum tod_fault_type ftype, int off)
20627c478bd9Sstevel@tonic-gate {
20637c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	if (tod_faulted != ftype) {
20667c478bd9Sstevel@tonic-gate 		switch (ftype) {
20677c478bd9Sstevel@tonic-gate 		case TOD_NOFAULT:
206818a0bfdeSae 			plat_tod_fault(TOD_NOFAULT);
20697c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "Restarted tracking "
20703aedfe0bSmishra 			    "Time of Day clock.");
20717c478bd9Sstevel@tonic-gate 			tod_faulted = ftype;
20727c478bd9Sstevel@tonic-gate 			break;
20737c478bd9Sstevel@tonic-gate 		case TOD_REVERSED:
20747c478bd9Sstevel@tonic-gate 		case TOD_JUMPED:
20757c478bd9Sstevel@tonic-gate 			if (tod_faulted == TOD_NOFAULT) {
207618a0bfdeSae 				plat_tod_fault(ftype);
20777c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "Time of Day clock error: "
20787c478bd9Sstevel@tonic-gate 				    "reason [%s by 0x%x]. -- "
20797c478bd9Sstevel@tonic-gate 				    " Stopped tracking Time Of Day clock.",
20807c478bd9Sstevel@tonic-gate 				    tod_fault_table[ftype], off);
20817c478bd9Sstevel@tonic-gate 				tod_faulted = ftype;
20827c478bd9Sstevel@tonic-gate 			}
20837c478bd9Sstevel@tonic-gate 			break;
20847c478bd9Sstevel@tonic-gate 		case TOD_STALLED:
20857c478bd9Sstevel@tonic-gate 		case TOD_RATECHANGED:
20867c478bd9Sstevel@tonic-gate 			if (tod_faulted == TOD_NOFAULT) {
208718a0bfdeSae 				plat_tod_fault(ftype);
20887c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "Time of Day clock error: "
20897c478bd9Sstevel@tonic-gate 				    "reason [%s]. -- "
20907c478bd9Sstevel@tonic-gate 				    " Stopped tracking Time Of Day clock.",
20917c478bd9Sstevel@tonic-gate 				    tod_fault_table[ftype]);
20927c478bd9Sstevel@tonic-gate 				tod_faulted = ftype;
20937c478bd9Sstevel@tonic-gate 			}
20947c478bd9Sstevel@tonic-gate 			break;
2095843e1988Sjohnlev 		case TOD_RDONLY:
2096843e1988Sjohnlev 			if (tod_faulted == TOD_NOFAULT) {
2097843e1988Sjohnlev 				plat_tod_fault(ftype);
2098843e1988Sjohnlev 				cmn_err(CE_NOTE, "!Time of Day clock is "
2099843e1988Sjohnlev 				    "Read-Only; set of Date/Time will not "
2100843e1988Sjohnlev 				    "persist across reboot.");
2101843e1988Sjohnlev 				tod_faulted = ftype;
2102843e1988Sjohnlev 			}
2103843e1988Sjohnlev 			break;
21047c478bd9Sstevel@tonic-gate 		default:
21057c478bd9Sstevel@tonic-gate 			break;
21067c478bd9Sstevel@tonic-gate 		}
21077c478bd9Sstevel@tonic-gate 	}
21087c478bd9Sstevel@tonic-gate 	return (tod_faulted);
21097c478bd9Sstevel@tonic-gate }
21107c478bd9Sstevel@tonic-gate 
21118fc99e42STrevor Thompson /*
21128fc99e42STrevor Thompson  * Two functions that allow tod_status_flag to be manipulated by functions
21138fc99e42STrevor Thompson  * external to this file.
21148fc99e42STrevor Thompson  */
21158fc99e42STrevor Thompson 
21167c478bd9Sstevel@tonic-gate void
tod_status_set(int tod_flag)21178fc99e42STrevor Thompson tod_status_set(int tod_flag)
21187c478bd9Sstevel@tonic-gate {
21198fc99e42STrevor Thompson 	tod_status_flag |= tod_flag;
21207c478bd9Sstevel@tonic-gate }
21217c478bd9Sstevel@tonic-gate 
21228fc99e42STrevor Thompson void
tod_status_clear(int tod_flag)21238fc99e42STrevor Thompson tod_status_clear(int tod_flag)
21248fc99e42STrevor Thompson {
21258fc99e42STrevor Thompson 	tod_status_flag &= ~tod_flag;
21268fc99e42STrevor Thompson }
21278fc99e42STrevor Thompson 
21288fc99e42STrevor Thompson /*
21298fc99e42STrevor Thompson  * Record a timestamp and the value passed to tod_set().  The next call to
21308fc99e42STrevor Thompson  * tod_validate() can use these values, prev_set_tick and prev_set_tod,
21318fc99e42STrevor Thompson  * when checking the timestruc_t returned by tod_get().  Ordinarily,
21328fc99e42STrevor Thompson  * tod_validate() will use prev_tick and prev_tod for this task but these
21338fc99e42STrevor Thompson  * become obsolete, and will be re-assigned with the prev_set_* values,
21348fc99e42STrevor Thompson  * in the case when the TOD is re-written.
21358fc99e42STrevor Thompson  */
21368fc99e42STrevor Thompson void
tod_set_prev(timestruc_t ts)21378fc99e42STrevor Thompson tod_set_prev(timestruc_t ts)
21388fc99e42STrevor Thompson {
21398fc99e42STrevor Thompson 	if ((tod_validate_enable == 0) || (tod_faulted != TOD_NOFAULT) ||
21408fc99e42STrevor Thompson 	    tod_validate_deferred) {
21418fc99e42STrevor Thompson 		return;
21428fc99e42STrevor Thompson 	}
21438fc99e42STrevor Thompson 	prev_set_tick = gethrtime();
21448fc99e42STrevor Thompson 	/*
21458fc99e42STrevor Thompson 	 * A negative value will be set to zero in utc_to_tod() so we fake
21468fc99e42STrevor Thompson 	 * a zero here in such a case.  This would need to change if the
21478fc99e42STrevor Thompson 	 * behavior of utc_to_tod() changes.
21488fc99e42STrevor Thompson 	 */
21498fc99e42STrevor Thompson 	prev_set_tod = ts.tv_sec < 0 ? 0 : ts.tv_sec;
21508fc99e42STrevor Thompson }
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate /*
21537c478bd9Sstevel@tonic-gate  * tod_validate() is used for checking values returned by tod_get().
21547c478bd9Sstevel@tonic-gate  * Four error cases can be detected by this routine:
21557c478bd9Sstevel@tonic-gate  *   TOD_REVERSED: current tod value is less than previous.
21567c478bd9Sstevel@tonic-gate  *   TOD_STALLED: current tod value hasn't advanced.
21577c478bd9Sstevel@tonic-gate  *   TOD_JUMPED: current tod value advanced too far from previous value.
21587c478bd9Sstevel@tonic-gate  *   TOD_RATECHANGED: the ratio between average tod delta and
21597c478bd9Sstevel@tonic-gate  *   average tick delta has changed.
21607c478bd9Sstevel@tonic-gate  */
21617c478bd9Sstevel@tonic-gate time_t
tod_validate(time_t tod)21627c478bd9Sstevel@tonic-gate tod_validate(time_t tod)
21637c478bd9Sstevel@tonic-gate {
21647c478bd9Sstevel@tonic-gate 	time_t diff_tod;
21657c478bd9Sstevel@tonic-gate 	hrtime_t diff_tick;
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	long dtick;
21687c478bd9Sstevel@tonic-gate 	int dtick_delta;
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	int off = 0;
21717c478bd9Sstevel@tonic-gate 	enum tod_fault_type tod_bad = TOD_NOFAULT;
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 	static int firsttime = 1;
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	static time_t prev_tod = 0;
21767c478bd9Sstevel@tonic-gate 	static hrtime_t prev_tick = 0;
21777c478bd9Sstevel@tonic-gate 	static long dtick_avg = TOD_REF_FREQ;
21787c478bd9Sstevel@tonic-gate 
21798fc99e42STrevor Thompson 	int cpr_resume_done = 0;
21808fc99e42STrevor Thompson 	int dr_resume_done = 0;
21818fc99e42STrevor Thompson 
21827c478bd9Sstevel@tonic-gate 	hrtime_t tick = gethrtime();
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	/*
21877c478bd9Sstevel@tonic-gate 	 * tod_validate_enable is patchable via /etc/system.
21882c891a87Ssethg 	 * If TOD is already faulted, or if TOD validation is deferred,
21892c891a87Ssethg 	 * there is nothing to do.
21907c478bd9Sstevel@tonic-gate 	 */
21912c891a87Ssethg 	if ((tod_validate_enable == 0) || (tod_faulted != TOD_NOFAULT) ||
21922c891a87Ssethg 	    tod_validate_deferred) {
21937c478bd9Sstevel@tonic-gate 		return (tod);
21947c478bd9Sstevel@tonic-gate 	}
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	/*
21978fc99e42STrevor Thompson 	 * If this is the first time through, we just need to save the tod
21988fc99e42STrevor Thompson 	 * we were called with and hrtime so we can use them next time to
21998fc99e42STrevor Thompson 	 * validate tod_get().
22007c478bd9Sstevel@tonic-gate 	 */
22017c478bd9Sstevel@tonic-gate 	if (firsttime) {
22027c478bd9Sstevel@tonic-gate 		firsttime = 0;
22037c478bd9Sstevel@tonic-gate 		prev_tod = tod;
22047c478bd9Sstevel@tonic-gate 		prev_tick = tick;
22057c478bd9Sstevel@tonic-gate 		return (tod);
22067c478bd9Sstevel@tonic-gate 	}
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/*
22098fc99e42STrevor Thompson 	 * Handle any flags that have been turned on by tod_status_set().
22108fc99e42STrevor Thompson 	 * In the case where a tod_set() is done and then a subsequent
22118fc99e42STrevor Thompson 	 * tod_get() fails (ie, both TOD_SET_DONE and TOD_GET_FAILED are
22128fc99e42STrevor Thompson 	 * true), we treat the TOD_GET_FAILED with precedence by switching
22138fc99e42STrevor Thompson 	 * off the flag, returning tod and leaving TOD_SET_DONE asserted
22148fc99e42STrevor Thompson 	 * until such time as tod_get() completes successfully.
22157c478bd9Sstevel@tonic-gate 	 */
22168fc99e42STrevor Thompson 	if (tod_status_flag & TOD_GET_FAILED) {
22178fc99e42STrevor Thompson 		/*
22188fc99e42STrevor Thompson 		 * tod_get() has encountered an issue, possibly transitory,
22198fc99e42STrevor Thompson 		 * when reading TOD.  We'll just return the incoming tod
22208fc99e42STrevor Thompson 		 * value (which is actually hrestime.tv_sec in this case)
22218fc99e42STrevor Thompson 		 * and when we get a genuine tod, following a successful
22228fc99e42STrevor Thompson 		 * tod_get(), we can validate using prev_tod and prev_tick.
22238fc99e42STrevor Thompson 		 */
22248fc99e42STrevor Thompson 		tod_status_flag &= ~TOD_GET_FAILED;
22257c478bd9Sstevel@tonic-gate 		return (tod);
22268fc99e42STrevor Thompson 	} else if (tod_status_flag & TOD_SET_DONE) {
22278fc99e42STrevor Thompson 		/*
22288fc99e42STrevor Thompson 		 * TOD has been modified.  Just before the TOD was written,
22298fc99e42STrevor Thompson 		 * tod_set_prev() saved tod and hrtime; we can now use
22308fc99e42STrevor Thompson 		 * those values, prev_set_tod and prev_set_tick, to validate
22318fc99e42STrevor Thompson 		 * the incoming tod that's just been read.
22328fc99e42STrevor Thompson 		 */
22338fc99e42STrevor Thompson 		prev_tod = prev_set_tod;
22348fc99e42STrevor Thompson 		prev_tick = prev_set_tick;
22358fc99e42STrevor Thompson 		dtick_avg = TOD_REF_FREQ;
22368fc99e42STrevor Thompson 		tod_status_flag &= ~TOD_SET_DONE;
22378fc99e42STrevor Thompson 		/*
22388fc99e42STrevor Thompson 		 * If a tod_set() preceded a cpr_suspend() without an
22398fc99e42STrevor Thompson 		 * intervening tod_validate(), we need to ensure that a
22408fc99e42STrevor Thompson 		 * TOD_JUMPED condition is ignored.
22418fc99e42STrevor Thompson 		 * Note this isn't a concern in the case of DR as we've
22428fc99e42STrevor Thompson 		 * just reassigned dtick_avg, above.
22438fc99e42STrevor Thompson 		 */
22448fc99e42STrevor Thompson 		if (tod_status_flag & TOD_CPR_RESUME_DONE) {
22458fc99e42STrevor Thompson 			cpr_resume_done = 1;
22468fc99e42STrevor Thompson 			tod_status_flag &= ~TOD_CPR_RESUME_DONE;
22478fc99e42STrevor Thompson 		}
22488fc99e42STrevor Thompson 	} else if (tod_status_flag & TOD_CPR_RESUME_DONE) {
22498fc99e42STrevor Thompson 		/*
22508fc99e42STrevor Thompson 		 * The system's coming back from a checkpoint resume.
22518fc99e42STrevor Thompson 		 */
22528fc99e42STrevor Thompson 		cpr_resume_done = 1;
22538fc99e42STrevor Thompson 		tod_status_flag &= ~TOD_CPR_RESUME_DONE;
22548fc99e42STrevor Thompson 		/*
22558fc99e42STrevor Thompson 		 * We need to handle the possibility of a CPR suspend
22568fc99e42STrevor Thompson 		 * operation having been initiated whilst a DR event was
22578fc99e42STrevor Thompson 		 * in-flight.
22588fc99e42STrevor Thompson 		 */
22598fc99e42STrevor Thompson 		if (tod_status_flag & TOD_DR_RESUME_DONE) {
22608fc99e42STrevor Thompson 			dr_resume_done = 1;
22618fc99e42STrevor Thompson 			tod_status_flag &= ~TOD_DR_RESUME_DONE;
22628fc99e42STrevor Thompson 		}
22638fc99e42STrevor Thompson 	} else if (tod_status_flag & TOD_DR_RESUME_DONE) {
22648fc99e42STrevor Thompson 		/*
22658fc99e42STrevor Thompson 		 * A Dynamic Reconfiguration event has taken place.
22668fc99e42STrevor Thompson 		 */
22678fc99e42STrevor Thompson 		dr_resume_done = 1;
22688fc99e42STrevor Thompson 		tod_status_flag &= ~TOD_DR_RESUME_DONE;
22697c478bd9Sstevel@tonic-gate 	}
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 	/* test hook */
22727c478bd9Sstevel@tonic-gate 	switch (tod_unit_test) {
22737c478bd9Sstevel@tonic-gate 	case 1: /* for testing jumping tod */
22747c478bd9Sstevel@tonic-gate 		tod += tod_test_injector;
22757c478bd9Sstevel@tonic-gate 		tod_unit_test = 0;
22767c478bd9Sstevel@tonic-gate 		break;
22777c478bd9Sstevel@tonic-gate 	case 2:	/* for testing stuck tod bit */
22787c478bd9Sstevel@tonic-gate 		tod |= 1 << tod_test_injector;
22797c478bd9Sstevel@tonic-gate 		tod_unit_test = 0;
22807c478bd9Sstevel@tonic-gate 		break;
22817c478bd9Sstevel@tonic-gate 	case 3:	/* for testing stalled tod */
22827c478bd9Sstevel@tonic-gate 		tod = prev_tod;
22837c478bd9Sstevel@tonic-gate 		tod_unit_test = 0;
22847c478bd9Sstevel@tonic-gate 		break;
22857c478bd9Sstevel@tonic-gate 	case 4:	/* reset tod fault status */
22867c478bd9Sstevel@tonic-gate 		(void) tod_fault(TOD_NOFAULT, 0);
22877c478bd9Sstevel@tonic-gate 		tod_unit_test = 0;
22887c478bd9Sstevel@tonic-gate 		break;
22897c478bd9Sstevel@tonic-gate 	default:
22907c478bd9Sstevel@tonic-gate 		break;
22917c478bd9Sstevel@tonic-gate 	}
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 	diff_tod = tod - prev_tod;
22947c478bd9Sstevel@tonic-gate 	diff_tick = tick - prev_tick;
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	ASSERT(diff_tick >= 0);
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	if (diff_tod < 0) {
22997c478bd9Sstevel@tonic-gate 		/* ERROR - tod reversed */
23007c478bd9Sstevel@tonic-gate 		tod_bad = TOD_REVERSED;
23017c478bd9Sstevel@tonic-gate 		off = (int)(prev_tod - tod);
23027c478bd9Sstevel@tonic-gate 	} else if (diff_tod == 0) {
23037c478bd9Sstevel@tonic-gate 		/* tod did not advance */
23047c478bd9Sstevel@tonic-gate 		if (diff_tick > TOD_STALL_THRESHOLD) {
23057c478bd9Sstevel@tonic-gate 			/* ERROR - tod stalled */
23067c478bd9Sstevel@tonic-gate 			tod_bad = TOD_STALLED;
23077c478bd9Sstevel@tonic-gate 		} else {
23087c478bd9Sstevel@tonic-gate 			/*
23097c478bd9Sstevel@tonic-gate 			 * Make sure we don't update prev_tick
23107c478bd9Sstevel@tonic-gate 			 * so that diff_tick is calculated since
23117c478bd9Sstevel@tonic-gate 			 * the first diff_tod == 0
23127c478bd9Sstevel@tonic-gate 			 */
23137c478bd9Sstevel@tonic-gate 			return (tod);
23147c478bd9Sstevel@tonic-gate 		}
23157c478bd9Sstevel@tonic-gate 	} else {
23167c478bd9Sstevel@tonic-gate 		/* calculate dtick */
23177c478bd9Sstevel@tonic-gate 		dtick = diff_tick / diff_tod;
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 		/* update dtick averages */
23207c478bd9Sstevel@tonic-gate 		dtick_avg += ((dtick - dtick_avg) / TOD_FILTER_N);
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 		/*
23237c478bd9Sstevel@tonic-gate 		 * Calculate dtick_delta as
23247c478bd9Sstevel@tonic-gate 		 * variation from reference freq in quartiles
23257c478bd9Sstevel@tonic-gate 		 */
23267c478bd9Sstevel@tonic-gate 		dtick_delta = (dtick_avg - TOD_REF_FREQ) /
23273aedfe0bSmishra 		    (TOD_REF_FREQ >> 2);
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 		/*
23307c478bd9Sstevel@tonic-gate 		 * Even with a perfectly functioning TOD device,
23317c478bd9Sstevel@tonic-gate 		 * when the number of elapsed seconds is low the
23327c478bd9Sstevel@tonic-gate 		 * algorithm can calculate a rate that is beyond
23337c478bd9Sstevel@tonic-gate 		 * tolerance, causing an error.  The algorithm is
23347c478bd9Sstevel@tonic-gate 		 * inaccurate when elapsed time is low (less than
23357c478bd9Sstevel@tonic-gate 		 * 5 seconds).
23367c478bd9Sstevel@tonic-gate 		 */
23377c478bd9Sstevel@tonic-gate 		if (diff_tod > 4) {
23387c478bd9Sstevel@tonic-gate 			if (dtick < TOD_JUMP_THRESHOLD) {
23398fc99e42STrevor Thompson 				/*
23408fc99e42STrevor Thompson 				 * If we've just done a CPR resume, we detect
23418fc99e42STrevor Thompson 				 * a jump in the TOD but, actually, what's
23428fc99e42STrevor Thompson 				 * happened is that the TOD has been increasing
23438fc99e42STrevor Thompson 				 * whilst the system was suspended and the tick
23448fc99e42STrevor Thompson 				 * count hasn't kept up.  We consider the first
23458fc99e42STrevor Thompson 				 * occurrence of this after a resume as normal
23468fc99e42STrevor Thompson 				 * and ignore it; otherwise, in a non-resume
23478fc99e42STrevor Thompson 				 * case, we regard it as a TOD problem.
23488fc99e42STrevor Thompson 				 */
23498fc99e42STrevor Thompson 				if (!cpr_resume_done) {
23508fc99e42STrevor Thompson 					/* ERROR - tod jumped */
23518fc99e42STrevor Thompson 					tod_bad = TOD_JUMPED;
23528fc99e42STrevor Thompson 					off = (int)diff_tod;
23538fc99e42STrevor Thompson 				}
23548fc99e42STrevor Thompson 			}
23558fc99e42STrevor Thompson 			if (dtick_delta) {
23568fc99e42STrevor Thompson 				/*
23578fc99e42STrevor Thompson 				 * If we've just done a DR resume, dtick_avg
23588fc99e42STrevor Thompson 				 * can go a bit askew so we reset it and carry
23598fc99e42STrevor Thompson 				 * on; otherwise, the TOD is in error.
23608fc99e42STrevor Thompson 				 */
23618fc99e42STrevor Thompson 				if (dr_resume_done) {
23628fc99e42STrevor Thompson 					dtick_avg = TOD_REF_FREQ;
23638fc99e42STrevor Thompson 				} else {
23648fc99e42STrevor Thompson 					/* ERROR - change in clock rate */
23658fc99e42STrevor Thompson 					tod_bad = TOD_RATECHANGED;
23668fc99e42STrevor Thompson 				}
23677c478bd9Sstevel@tonic-gate 			}
23687c478bd9Sstevel@tonic-gate 		}
23697c478bd9Sstevel@tonic-gate 	}
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	if (tod_bad != TOD_NOFAULT) {
23727c478bd9Sstevel@tonic-gate 		(void) tod_fault(tod_bad, off);
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 		/*
23757c478bd9Sstevel@tonic-gate 		 * Disable dosynctodr since we are going to fault
23767c478bd9Sstevel@tonic-gate 		 * the TOD chip anyway here
23777c478bd9Sstevel@tonic-gate 		 */
23787c478bd9Sstevel@tonic-gate 		dosynctodr = 0;
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 		/*
23817c478bd9Sstevel@tonic-gate 		 * Set tod to the correct value from hrestime
23827c478bd9Sstevel@tonic-gate 		 */
23837c478bd9Sstevel@tonic-gate 		tod = hrestime.tv_sec;
23847c478bd9Sstevel@tonic-gate 	}
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 	prev_tod = tod;
23877c478bd9Sstevel@tonic-gate 	prev_tick = tick;
23887c478bd9Sstevel@tonic-gate 	return (tod);
23897c478bd9Sstevel@tonic-gate }
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate static void
calcloadavg(int nrun,uint64_t * hp_ave)23927c478bd9Sstevel@tonic-gate calcloadavg(int nrun, uint64_t *hp_ave)
23937c478bd9Sstevel@tonic-gate {
23947c478bd9Sstevel@tonic-gate 	static int64_t f[3] = { 135, 27, 9 };
23957c478bd9Sstevel@tonic-gate 	uint_t i;
23967c478bd9Sstevel@tonic-gate 	int64_t q, r;
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	/*
23997c478bd9Sstevel@tonic-gate 	 * Compute load average over the last 1, 5, and 15 minutes
24007c478bd9Sstevel@tonic-gate 	 * (60, 300, and 900 seconds).  The constants in f[3] are for
24017c478bd9Sstevel@tonic-gate 	 * exponential decay:
24027c478bd9Sstevel@tonic-gate 	 * (1 - exp(-1/60)) << 13 = 135,
24037c478bd9Sstevel@tonic-gate 	 * (1 - exp(-1/300)) << 13 = 27,
24047c478bd9Sstevel@tonic-gate 	 * (1 - exp(-1/900)) << 13 = 9.
24057c478bd9Sstevel@tonic-gate 	 */
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	/*
24087c478bd9Sstevel@tonic-gate 	 * a little hoop-jumping to avoid integer overflow
24097c478bd9Sstevel@tonic-gate 	 */
24107c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
24117c478bd9Sstevel@tonic-gate 		q = (hp_ave[i]  >> 16) << 7;
24127c478bd9Sstevel@tonic-gate 		r = (hp_ave[i]  & 0xffff) << 7;
24137c478bd9Sstevel@tonic-gate 		hp_ave[i] += ((nrun - q) * f[i] - ((r * f[i]) >> 16)) >> 4;
24147c478bd9Sstevel@tonic-gate 	}
24157c478bd9Sstevel@tonic-gate }
2416d3d50737SRafael Vanoni 
2417d3d50737SRafael Vanoni /*
2418d3d50737SRafael Vanoni  * lbolt_hybrid() is used by ddi_get_lbolt() and ddi_get_lbolt64() to
2419d3d50737SRafael Vanoni  * calculate the value of lbolt according to the current mode. In the event
2420d3d50737SRafael Vanoni  * driven mode (the default), lbolt is calculated by dividing the current hires
2421d3d50737SRafael Vanoni  * time by the number of nanoseconds per clock tick. In the cyclic driven mode
2422d3d50737SRafael Vanoni  * an internal variable is incremented at each firing of the lbolt cyclic
2423d3d50737SRafael Vanoni  * and returned by lbolt_cyclic_driven().
2424d3d50737SRafael Vanoni  *
2425d3d50737SRafael Vanoni  * The system will transition from event to cyclic driven mode when the number
2426d3d50737SRafael Vanoni  * of calls to lbolt_event_driven() exceeds the (per CPU) threshold within a
2427d3d50737SRafael Vanoni  * window of time. It does so by reprograming lbolt_cyclic from CY_INFINITY to
2428d3d50737SRafael Vanoni  * nsec_per_tick. The lbolt cyclic will remain ON while at least one CPU is
2429d3d50737SRafael Vanoni  * causing enough activity to cross the thresholds.
2430d3d50737SRafael Vanoni  */
243183e866b9SRafael Vanoni int64_t
lbolt_bootstrap(void)2432d3d50737SRafael Vanoni lbolt_bootstrap(void)
2433d3d50737SRafael Vanoni {
2434d3d50737SRafael Vanoni 	return (0);
2435d3d50737SRafael Vanoni }
2436d3d50737SRafael Vanoni 
2437d3d50737SRafael Vanoni /* ARGSUSED */
2438d3d50737SRafael Vanoni uint_t
lbolt_ev_to_cyclic(caddr_t arg1,caddr_t arg2)2439d3d50737SRafael Vanoni lbolt_ev_to_cyclic(caddr_t arg1, caddr_t arg2)
2440d3d50737SRafael Vanoni {
2441d3d50737SRafael Vanoni 	hrtime_t ts, exp;
2442d3d50737SRafael Vanoni 	int ret;
2443d3d50737SRafael Vanoni 
2444d3d50737SRafael Vanoni 	ASSERT(lbolt_hybrid != lbolt_cyclic_driven);
2445d3d50737SRafael Vanoni 
2446d3d50737SRafael Vanoni 	kpreempt_disable();
2447d3d50737SRafael Vanoni 
2448d3d50737SRafael Vanoni 	ts = gethrtime();
2449d3d50737SRafael Vanoni 	lb_info->lbi_internal = (ts/nsec_per_tick);
2450d3d50737SRafael Vanoni 
2451d3d50737SRafael Vanoni 	/*
2452d3d50737SRafael Vanoni 	 * Align the next expiration to a clock tick boundary.
2453d3d50737SRafael Vanoni 	 */
2454d3d50737SRafael Vanoni 	exp = ts + nsec_per_tick - 1;
2455d3d50737SRafael Vanoni 	exp = (exp/nsec_per_tick) * nsec_per_tick;
2456d3d50737SRafael Vanoni 
2457579a6895SRafael Vanoni 	ret = cyclic_reprogram(lb_info->id.lbi_cyclic_id, exp);
2458d3d50737SRafael Vanoni 	ASSERT(ret);
2459d3d50737SRafael Vanoni 
2460d3d50737SRafael Vanoni 	lbolt_hybrid = lbolt_cyclic_driven;
2461d3d50737SRafael Vanoni 	lb_info->lbi_cyc_deactivate = B_FALSE;
2462d3d50737SRafael Vanoni 	lb_info->lbi_cyc_deac_start = lb_info->lbi_internal;
2463d3d50737SRafael Vanoni 
2464d3d50737SRafael Vanoni 	kpreempt_enable();
2465d3d50737SRafael Vanoni 
2466d3d50737SRafael Vanoni 	ret = atomic_dec_32_nv(&lb_info->lbi_token);
2467d3d50737SRafael Vanoni 	ASSERT(ret == 0);
2468d3d50737SRafael Vanoni 
2469d3d50737SRafael Vanoni 	return (1);
2470d3d50737SRafael Vanoni }
2471d3d50737SRafael Vanoni 
2472d3d50737SRafael Vanoni int64_t
lbolt_event_driven(void)2473d3d50737SRafael Vanoni lbolt_event_driven(void)
2474d3d50737SRafael Vanoni {
2475d3d50737SRafael Vanoni 	hrtime_t ts;
2476d3d50737SRafael Vanoni 	int64_t lb;
2477d3d50737SRafael Vanoni 	int ret, cpu = CPU->cpu_seqid;
2478d3d50737SRafael Vanoni 
2479d3d50737SRafael Vanoni 	ts = gethrtime();
2480d3d50737SRafael Vanoni 	ASSERT(ts > 0);
2481d3d50737SRafael Vanoni 
2482d3d50737SRafael Vanoni 	ASSERT(nsec_per_tick > 0);
2483d3d50737SRafael Vanoni 	lb = (ts/nsec_per_tick);
2484d3d50737SRafael Vanoni 
2485d3d50737SRafael Vanoni 	/*
2486d3d50737SRafael Vanoni 	 * Switch to cyclic mode if the number of calls to this routine
2487d3d50737SRafael Vanoni 	 * has reached the threshold within the interval.
2488d3d50737SRafael Vanoni 	 */
2489d3d50737SRafael Vanoni 	if ((lb - lb_cpu[cpu].lbc_cnt_start) < lb_info->lbi_thresh_interval) {
2490d3d50737SRafael Vanoni 
2491d3d50737SRafael Vanoni 		if (--lb_cpu[cpu].lbc_counter == 0) {
2492d3d50737SRafael Vanoni 			/*
2493d3d50737SRafael Vanoni 			 * Reached the threshold within the interval, reset
2494d3d50737SRafael Vanoni 			 * the usage statistics.
2495d3d50737SRafael Vanoni 			 */
2496d3d50737SRafael Vanoni 			lb_cpu[cpu].lbc_counter = lb_info->lbi_thresh_calls;
2497d3d50737SRafael Vanoni 			lb_cpu[cpu].lbc_cnt_start = lb;
2498d3d50737SRafael Vanoni 
2499d3d50737SRafael Vanoni 			/*
2500d3d50737SRafael Vanoni 			 * Make sure only one thread reprograms the
2501d3d50737SRafael Vanoni 			 * lbolt cyclic and changes the mode.
2502d3d50737SRafael Vanoni 			 */
2503d3d50737SRafael Vanoni 			if (panicstr == NULL &&
2504d3d50737SRafael Vanoni 			    atomic_cas_32(&lb_info->lbi_token, 0, 1) == 0) {
2505d3d50737SRafael Vanoni 
2506d3d50737SRafael Vanoni 				if (lbolt_hybrid == lbolt_cyclic_driven) {
2507d3d50737SRafael Vanoni 					ret = atomic_dec_32_nv(
2508d3d50737SRafael Vanoni 					    &lb_info->lbi_token);
2509d3d50737SRafael Vanoni 					ASSERT(ret == 0);
251073cd038fSRafael Vanoni 				} else {
251173cd038fSRafael Vanoni 					lbolt_softint_post();
2512d3d50737SRafael Vanoni 				}
2513d3d50737SRafael Vanoni 			}
2514d3d50737SRafael Vanoni 		}
2515d3d50737SRafael Vanoni 	} else {
2516d3d50737SRafael Vanoni 		/*
2517d3d50737SRafael Vanoni 		 * Exceeded the interval, reset the usage statistics.
2518d3d50737SRafael Vanoni 		 */
2519d3d50737SRafael Vanoni 		lb_cpu[cpu].lbc_counter = lb_info->lbi_thresh_calls;
2520d3d50737SRafael Vanoni 		lb_cpu[cpu].lbc_cnt_start = lb;
2521d3d50737SRafael Vanoni 	}
2522d3d50737SRafael Vanoni 
2523d3d50737SRafael Vanoni 	ASSERT(lb >= lb_info->lbi_debug_time);
2524d3d50737SRafael Vanoni 
2525d3d50737SRafael Vanoni 	return (lb - lb_info->lbi_debug_time);
2526d3d50737SRafael Vanoni }
2527d3d50737SRafael Vanoni 
2528d3d50737SRafael Vanoni int64_t
lbolt_cyclic_driven(void)2529d3d50737SRafael Vanoni lbolt_cyclic_driven(void)
2530d3d50737SRafael Vanoni {
2531d3d50737SRafael Vanoni 	int64_t lb = lb_info->lbi_internal;
2532777222b7SRafael Vanoni 	int cpu;
2533d3d50737SRafael Vanoni 
2534777222b7SRafael Vanoni 	/*
2535777222b7SRafael Vanoni 	 * If a CPU has already prevented the lbolt cyclic from deactivating
2536777222b7SRafael Vanoni 	 * itself, don't bother tracking the usage. Otherwise check if we're
2537777222b7SRafael Vanoni 	 * within the interval and how the per CPU counter is doing.
2538777222b7SRafael Vanoni 	 */
2539777222b7SRafael Vanoni 	if (lb_info->lbi_cyc_deactivate) {
2540777222b7SRafael Vanoni 		cpu = CPU->cpu_seqid;
2541777222b7SRafael Vanoni 		if ((lb - lb_cpu[cpu].lbc_cnt_start) <
2542777222b7SRafael Vanoni 		    lb_info->lbi_thresh_interval) {
2543d3d50737SRafael Vanoni 
2544777222b7SRafael Vanoni 			if (lb_cpu[cpu].lbc_counter == 0)
2545777222b7SRafael Vanoni 				/*
2546777222b7SRafael Vanoni 				 * Reached the threshold within the interval,
2547777222b7SRafael Vanoni 				 * prevent the lbolt cyclic from turning itself
2548777222b7SRafael Vanoni 				 * off.
2549777222b7SRafael Vanoni 				 */
2550777222b7SRafael Vanoni 				lb_info->lbi_cyc_deactivate = B_FALSE;
2551777222b7SRafael Vanoni 			else
2552777222b7SRafael Vanoni 				lb_cpu[cpu].lbc_counter--;
2553777222b7SRafael Vanoni 		} else {
2554d3d50737SRafael Vanoni 			/*
2555777222b7SRafael Vanoni 			 * Only reset the usage statistics when we have
2556777222b7SRafael Vanoni 			 * exceeded the interval.
2557d3d50737SRafael Vanoni 			 */
2558777222b7SRafael Vanoni 			lb_cpu[cpu].lbc_counter = lb_info->lbi_thresh_calls;
2559777222b7SRafael Vanoni 			lb_cpu[cpu].lbc_cnt_start = lb;
2560777222b7SRafael Vanoni 		}
2561d3d50737SRafael Vanoni 	}
2562d3d50737SRafael Vanoni 
2563d3d50737SRafael Vanoni 	ASSERT(lb >= lb_info->lbi_debug_time);
2564d3d50737SRafael Vanoni 
2565d3d50737SRafael Vanoni 	return (lb - lb_info->lbi_debug_time);
2566d3d50737SRafael Vanoni }
2567d3d50737SRafael Vanoni 
2568d3d50737SRafael Vanoni /*
256983e866b9SRafael Vanoni  * The lbolt_cyclic() routine will fire at a nsec_per_tick interval to satisfy
2570d3d50737SRafael Vanoni  * performance needs of ddi_get_lbolt() and ddi_get_lbolt64() consumers.
2571d3d50737SRafael Vanoni  * It is inactive by default, and will be activated when switching from event
2572d3d50737SRafael Vanoni  * to cyclic driven lbolt. The cyclic will turn itself off unless signaled
2573d3d50737SRafael Vanoni  * by lbolt_cyclic_driven().
2574d3d50737SRafael Vanoni  */
2575d3d50737SRafael Vanoni static void
lbolt_cyclic(void)2576d3d50737SRafael Vanoni lbolt_cyclic(void)
2577d3d50737SRafael Vanoni {
2578d3d50737SRafael Vanoni 	int ret;
2579d3d50737SRafael Vanoni 
2580d3d50737SRafael Vanoni 	lb_info->lbi_internal++;
2581d3d50737SRafael Vanoni 
2582d3d50737SRafael Vanoni 	if (!lbolt_cyc_only) {
2583d3d50737SRafael Vanoni 
2584d3d50737SRafael Vanoni 		if (lb_info->lbi_cyc_deactivate) {
2585d3d50737SRafael Vanoni 			/*
2586d3d50737SRafael Vanoni 			 * Switching from cyclic to event driven mode.
2587d3d50737SRafael Vanoni 			 */
258890aaf355SRafael Vanoni 			if (panicstr == NULL &&
258990aaf355SRafael Vanoni 			    atomic_cas_32(&lb_info->lbi_token, 0, 1) == 0) {
2590d3d50737SRafael Vanoni 
2591d3d50737SRafael Vanoni 				if (lbolt_hybrid == lbolt_event_driven) {
2592d3d50737SRafael Vanoni 					ret = atomic_dec_32_nv(
2593d3d50737SRafael Vanoni 					    &lb_info->lbi_token);
2594d3d50737SRafael Vanoni 					ASSERT(ret == 0);
2595d3d50737SRafael Vanoni 					return;
2596d3d50737SRafael Vanoni 				}
2597d3d50737SRafael Vanoni 
2598d3d50737SRafael Vanoni 				kpreempt_disable();
2599d3d50737SRafael Vanoni 
2600d3d50737SRafael Vanoni 				lbolt_hybrid = lbolt_event_driven;
2601579a6895SRafael Vanoni 				ret = cyclic_reprogram(
2602579a6895SRafael Vanoni 				    lb_info->id.lbi_cyclic_id,
2603d3d50737SRafael Vanoni 				    CY_INFINITY);
2604d3d50737SRafael Vanoni 				ASSERT(ret);
2605d3d50737SRafael Vanoni 
2606d3d50737SRafael Vanoni 				kpreempt_enable();
2607d3d50737SRafael Vanoni 
2608d3d50737SRafael Vanoni 				ret = atomic_dec_32_nv(&lb_info->lbi_token);
2609d3d50737SRafael Vanoni 				ASSERT(ret == 0);
2610d3d50737SRafael Vanoni 			}
2611d3d50737SRafael Vanoni 		}
2612d3d50737SRafael Vanoni 
2613d3d50737SRafael Vanoni 		/*
2614d3d50737SRafael Vanoni 		 * The lbolt cyclic should not try to deactivate itself before
2615d3d50737SRafael Vanoni 		 * the sampling period has elapsed.
2616d3d50737SRafael Vanoni 		 */
2617d3d50737SRafael Vanoni 		if (lb_info->lbi_internal - lb_info->lbi_cyc_deac_start >=
2618d3d50737SRafael Vanoni 		    lb_info->lbi_thresh_interval) {
2619d3d50737SRafael Vanoni 			lb_info->lbi_cyc_deactivate = B_TRUE;
2620d3d50737SRafael Vanoni 			lb_info->lbi_cyc_deac_start = lb_info->lbi_internal;
2621d3d50737SRafael Vanoni 		}
2622d3d50737SRafael Vanoni 	}
2623d3d50737SRafael Vanoni }
2624d3d50737SRafael Vanoni 
2625d3d50737SRafael Vanoni /*
2626d3d50737SRafael Vanoni  * Since the lbolt service was historically cyclic driven, it must be 'stopped'
2627d3d50737SRafael Vanoni  * when the system drops into the kernel debugger. lbolt_debug_entry() is
2628d3d50737SRafael Vanoni  * called by the KDI system claim callbacks to record a hires timestamp at
2629d3d50737SRafael Vanoni  * debug enter time. lbolt_debug_return() is called by the sistem release
2630d3d50737SRafael Vanoni  * callbacks to account for the time spent in the debugger. The value is then
2631d3d50737SRafael Vanoni  * accumulated in the lb_info structure and used by lbolt_event_driven() and
2632d3d50737SRafael Vanoni  * lbolt_cyclic_driven(), as well as the mdb_get_lbolt() routine.
2633d3d50737SRafael Vanoni  */
2634d3d50737SRafael Vanoni void
lbolt_debug_entry(void)2635d3d50737SRafael Vanoni lbolt_debug_entry(void)
2636d3d50737SRafael Vanoni {
26373bf27077SRafael Vanoni 	if (lbolt_hybrid != lbolt_bootstrap) {
26383bf27077SRafael Vanoni 		ASSERT(lb_info != NULL);
26393bf27077SRafael Vanoni 		lb_info->lbi_debug_ts = gethrtime();
26403bf27077SRafael Vanoni 	}
2641d3d50737SRafael Vanoni }
2642d3d50737SRafael Vanoni 
2643579a6895SRafael Vanoni /*
2644579a6895SRafael Vanoni  * Calculate the time spent in the debugger and add it to the lbolt info
2645579a6895SRafael Vanoni  * structure. We also update the internal lbolt value in case we were in
2646579a6895SRafael Vanoni  * cyclic driven mode going in.
2647579a6895SRafael Vanoni  */
2648d3d50737SRafael Vanoni void
lbolt_debug_return(void)2649d3d50737SRafael Vanoni lbolt_debug_return(void)
2650d3d50737SRafael Vanoni {
2651579a6895SRafael Vanoni 	hrtime_t ts;
2652579a6895SRafael Vanoni 
26533bf27077SRafael Vanoni 	if (lbolt_hybrid != lbolt_bootstrap) {
26543bf27077SRafael Vanoni 		ASSERT(lb_info != NULL);
26553bf27077SRafael Vanoni 		ASSERT(nsec_per_tick > 0);
2656579a6895SRafael Vanoni 
26573bf27077SRafael Vanoni 		ts = gethrtime();
2658579a6895SRafael Vanoni 		lb_info->lbi_internal = (ts/nsec_per_tick);
2659d3d50737SRafael Vanoni 		lb_info->lbi_debug_time +=
2660579a6895SRafael Vanoni 		    ((ts - lb_info->lbi_debug_ts)/nsec_per_tick);
2661d3d50737SRafael Vanoni 
26623bf27077SRafael Vanoni 		lb_info->lbi_debug_ts = 0;
26633bf27077SRafael Vanoni 	}
2664d3d50737SRafael Vanoni }
2665