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