xref: /onnv-gate/usr/src/uts/i86pc/os/intr.c (revision 989:2dafb1670a6e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/cpuvar.h>
300Sstevel@tonic-gate #include <sys/regset.h>
310Sstevel@tonic-gate #include <sys/psw.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/thread.h>
340Sstevel@tonic-gate #include <sys/systm.h>
350Sstevel@tonic-gate #include <sys/segments.h>
360Sstevel@tonic-gate #include <sys/pcb.h>
370Sstevel@tonic-gate #include <sys/trap.h>
380Sstevel@tonic-gate #include <sys/ftrace.h>
390Sstevel@tonic-gate #include <sys/traptrace.h>
400Sstevel@tonic-gate #include <sys/clock.h>
410Sstevel@tonic-gate #include <sys/panic.h>
420Sstevel@tonic-gate #include <sys/disp.h>
430Sstevel@tonic-gate #include <vm/seg_kp.h>
440Sstevel@tonic-gate #include <sys/stack.h>
450Sstevel@tonic-gate #include <sys/sysmacros.h>
460Sstevel@tonic-gate #include <sys/cmn_err.h>
470Sstevel@tonic-gate #include <sys/kstat.h>
480Sstevel@tonic-gate #include <sys/smp_impldefs.h>
490Sstevel@tonic-gate #include <sys/pool_pset.h>
500Sstevel@tonic-gate #include <sys/zone.h>
510Sstevel@tonic-gate #include <sys/bitmap.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #if defined(__amd64)
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #if defined(__lint)
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * atomic_btr32() is a gcc __inline__ function, defined in <asm/bitmap.h>
580Sstevel@tonic-gate  * For lint purposes, define it here.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate uint_t
610Sstevel@tonic-gate atomic_btr32(uint32_t *pending, uint_t pil)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	return (*pending &= ~(1 << pil));
640Sstevel@tonic-gate }
650Sstevel@tonic-gate #else
660Sstevel@tonic-gate 
670Sstevel@tonic-gate extern uint_t atomic_btr32(uint32_t *pending, uint_t pil);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #endif
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * This code is amd64-only for now, but as time permits, we should
730Sstevel@tonic-gate  * use this on i386 too.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * Some questions to ponder:
780Sstevel@tonic-gate  * -	in several of these routines, we make multiple calls to tsc_read()
790Sstevel@tonic-gate  *	without invoking functions .. couldn't we just reuse the same
800Sstevel@tonic-gate  *	timestamp sometimes?
810Sstevel@tonic-gate  * -	if we have the inline, we can probably make set_base_spl be a
820Sstevel@tonic-gate  *	C routine too.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static uint_t
860Sstevel@tonic-gate bsrw_insn(uint16_t mask)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	uint_t index = sizeof (mask) * NBBY - 1;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	ASSERT(mask != 0);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	while ((mask & (1 << index)) == 0)
930Sstevel@tonic-gate 		index--;
940Sstevel@tonic-gate 	return (index);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * Do all the work necessary to set up the cpu and thread structures
990Sstevel@tonic-gate  * to dispatch a high-level interrupt.
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  * Returns 0 if we're -not- already on the high-level interrupt stack,
1020Sstevel@tonic-gate  * (and *must* switch to it), non-zero if we are already on that stack.
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  * Called with interrupts masked.
1050Sstevel@tonic-gate  * The 'pil' is already set to the appropriate level for rp->r_trapno.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate int
1080Sstevel@tonic-gate hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
1110Sstevel@tonic-gate 	uint_t mask;
112590Sesolom 	hrtime_t intrtime;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	ASSERT(pil > LOCK_LEVEL);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (pil == CBE_HIGH_PIL) {
1170Sstevel@tonic-gate 		cpu->cpu_profile_pil = oldpil;
1180Sstevel@tonic-gate 		if (USERMODE(rp->r_cs)) {
1190Sstevel@tonic-gate 			cpu->cpu_profile_pc = 0;
1200Sstevel@tonic-gate 			cpu->cpu_profile_upc = rp->r_pc;
1210Sstevel@tonic-gate 		} else {
1220Sstevel@tonic-gate 			cpu->cpu_profile_pc = rp->r_pc;
1230Sstevel@tonic-gate 			cpu->cpu_profile_upc = 0;
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
1280Sstevel@tonic-gate 	if (mask != 0) {
1290Sstevel@tonic-gate 		int nestpil;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 		/*
1320Sstevel@tonic-gate 		 * We have interrupted another high-level interrupt.
1330Sstevel@tonic-gate 		 * Load starting timestamp, compute interval, update
1340Sstevel@tonic-gate 		 * cumulative counter.
1350Sstevel@tonic-gate 		 */
1360Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
1370Sstevel@tonic-gate 		ASSERT(nestpil < pil);
138590Sesolom 		intrtime = tsc_read() -
1390Sstevel@tonic-gate 		    mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)];
140916Sschwartz 		mcpu->intrstat[nestpil][0] += intrtime;
141590Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1420Sstevel@tonic-gate 		/*
1430Sstevel@tonic-gate 		 * Another high-level interrupt is active below this one, so
1440Sstevel@tonic-gate 		 * there is no need to check for an interrupt thread.  That
1450Sstevel@tonic-gate 		 * will be done by the lowest priority high-level interrupt
1460Sstevel@tonic-gate 		 * active.
1470Sstevel@tonic-gate 		 */
1480Sstevel@tonic-gate 	} else {
1490Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 		/*
1520Sstevel@tonic-gate 		 * See if we are interrupting a low-level interrupt thread.
1530Sstevel@tonic-gate 		 * If so, account for its time slice only if its time stamp
1540Sstevel@tonic-gate 		 * is non-zero.
1550Sstevel@tonic-gate 		 */
1560Sstevel@tonic-gate 		if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) {
157590Sesolom 			intrtime = tsc_read() - t->t_intr_start;
158916Sschwartz 			mcpu->intrstat[t->t_pil][0] += intrtime;
159590Sesolom 			cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1600Sstevel@tonic-gate 			t->t_intr_start = 0;
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	/*
1650Sstevel@tonic-gate 	 * Store starting timestamp in CPU structure for this PIL.
1660Sstevel@tonic-gate 	 */
1670Sstevel@tonic-gate 	mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = tsc_read();
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (pil == 15) {
1720Sstevel@tonic-gate 		/*
1730Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
1740Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
1750Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
1760Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
1770Sstevel@tonic-gate 		 */
1780Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
1790Sstevel@tonic-gate 		(*refcntp)++;
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * Does most of the work of returning from a high level interrupt.
1910Sstevel@tonic-gate  *
1920Sstevel@tonic-gate  * Returns 0 if there are no more high level interrupts (in which
1930Sstevel@tonic-gate  * case we must switch back to the interrupted thread stack) or
1940Sstevel@tonic-gate  * non-zero if there are more (in which case we should stay on it).
1950Sstevel@tonic-gate  *
1960Sstevel@tonic-gate  * Called with interrupts masked
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate int
1990Sstevel@tonic-gate hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
2020Sstevel@tonic-gate 	uint_t mask;
203590Sesolom 	hrtime_t intrtime;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	ASSERT(mcpu->mcpu_pri == pil);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (pil == 15) {
2120Sstevel@tonic-gate 		/*
2130Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
2140Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
2150Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
2160Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
2170Sstevel@tonic-gate 		 */
2180Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 		ASSERT(*refcntp > 0);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		if (--(*refcntp) == 0)
2230Sstevel@tonic-gate 			cpu->cpu_intr_actv &= ~(1 << pil);
2240Sstevel@tonic-gate 	} else {
2250Sstevel@tonic-gate 		cpu->cpu_intr_actv &= ~(1 << pil);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0);
2290Sstevel@tonic-gate 
230590Sesolom 	intrtime = tsc_read() - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)];
231916Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
232590Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * Check for lower-pil nested high-level interrupt beneath
2360Sstevel@tonic-gate 	 * current one.  If so, place a starting timestamp in its
2370Sstevel@tonic-gate 	 * pil_high_start entry.
2380Sstevel@tonic-gate 	 */
2390Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
2400Sstevel@tonic-gate 	if (mask != 0) {
2410Sstevel@tonic-gate 		int nestpil;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 		/*
2440Sstevel@tonic-gate 		 * find PIL of nested interrupt
2450Sstevel@tonic-gate 		 */
2460Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
2470Sstevel@tonic-gate 		ASSERT(nestpil < pil);
2480Sstevel@tonic-gate 		mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = tsc_read();
2490Sstevel@tonic-gate 		/*
2500Sstevel@tonic-gate 		 * (Another high-level interrupt is active below this one,
2510Sstevel@tonic-gate 		 * so there is no need to check for an interrupt
2520Sstevel@tonic-gate 		 * thread.  That will be done by the lowest priority
2530Sstevel@tonic-gate 		 * high-level interrupt active.)
2540Sstevel@tonic-gate 		 */
2550Sstevel@tonic-gate 	} else {
2560Sstevel@tonic-gate 		/*
2570Sstevel@tonic-gate 		 * Check to see if there is a low-level interrupt active.
2580Sstevel@tonic-gate 		 * If so, place a starting timestamp in the thread
2590Sstevel@tonic-gate 		 * structure.
2600Sstevel@tonic-gate 		 */
2610Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 		if (t->t_flag & T_INTR_THREAD)
2640Sstevel@tonic-gate 			t->t_intr_start = tsc_read();
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	mcpu->mcpu_pri = oldpil;
2680Sstevel@tonic-gate 	(void) (*setlvlx)(oldpil, vecnum);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * Set up the cpu, thread and interrupt thread structures for
2750Sstevel@tonic-gate  * executing an interrupt thread.  The new stack pointer of the
2760Sstevel@tonic-gate  * interrupt thread (which *must* be switched to) is returned.
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate caddr_t
2790Sstevel@tonic-gate intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
2820Sstevel@tonic-gate 	kthread_t *t, *volatile it;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	ASSERT(pil > 0);
2850Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
2860Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/*
2890Sstevel@tonic-gate 	 * Get set to run an interrupt thread.
2900Sstevel@tonic-gate 	 * There should always be an interrupt thread, since we
2910Sstevel@tonic-gate 	 * allocate one for each level on each CPU.
2920Sstevel@tonic-gate 	 *
293*989Sesolom 	 * t_intr_start could be zero due to cpu_intr_swtch_enter.
2940Sstevel@tonic-gate 	 */
2950Sstevel@tonic-gate 	t = cpu->cpu_thread;
296*989Sesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
297590Sesolom 		hrtime_t intrtime = tsc_read() - t->t_intr_start;
298916Sschwartz 		mcpu->intrstat[t->t_pil][0] += intrtime;
299590Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
3000Sstevel@tonic-gate 		t->t_intr_start = 0;
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;	/* mark stack in curthread for resume */
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	/*
3080Sstevel@tonic-gate 	 * unlink the interrupt thread off the cpu
309*989Sesolom 	 *
310*989Sesolom 	 * Note that the code in kcpc_overflow_intr -relies- on the
311*989Sesolom 	 * ordering of events here - in particular that t->t_lwp of
312*989Sesolom 	 * the interrupt thread is set to the pinned thread *before*
313*989Sesolom 	 * curthread is changed.
3140Sstevel@tonic-gate 	 */
3150Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
3160Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
3170Sstevel@tonic-gate 	it->t_intr = t;
3180Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * (threads on the interrupt thread free list could have state
3220Sstevel@tonic-gate 	 * preset to TS_ONPROC, but it helps in debugging if
3230Sstevel@tonic-gate 	 * they're TS_FREE.)
3240Sstevel@tonic-gate 	 */
3250Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	cpu->cpu_thread = it;		/* new curthread on this cpu */
3280Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
3290Sstevel@tonic-gate 	it->t_pri = intr_pri + (pri_t)pil;
3300Sstevel@tonic-gate 	it->t_intr_start = tsc_read();
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	return (it->t_stk);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate #ifdef DEBUG
3370Sstevel@tonic-gate int intr_thread_cnt;
3380Sstevel@tonic-gate #endif
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate  * Called with interrupts disabled
3420Sstevel@tonic-gate  */
3430Sstevel@tonic-gate void
3440Sstevel@tonic-gate intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
3470Sstevel@tonic-gate 	kthread_t *t;
3480Sstevel@tonic-gate 	kthread_t *it = cpu->cpu_thread;	/* curthread */
3490Sstevel@tonic-gate 	uint_t pil, basespl;
350590Sesolom 	hrtime_t intrtime;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	pil = it->t_pil;
3530Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	ASSERT(it->t_intr_start != 0);
356590Sesolom 	intrtime = tsc_read() - it->t_intr_start;
357916Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
358590Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
3610Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	/*
3640Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
3650Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
3660Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
3690Sstevel@tonic-gate 		/*
3700Sstevel@tonic-gate 		 * The interrupted thread is no longer pinned underneath
3710Sstevel@tonic-gate 		 * the interrupt thread.  This means the interrupt must
3720Sstevel@tonic-gate 		 * have blocked, and the interrupted thread has been
3730Sstevel@tonic-gate 		 * unpinned, and has probably been running around the
3740Sstevel@tonic-gate 		 * system for a while.
3750Sstevel@tonic-gate 		 *
3760Sstevel@tonic-gate 		 * Since there is no longer a thread under this one, put
3770Sstevel@tonic-gate 		 * this interrupt thread back on the CPU's free list and
3780Sstevel@tonic-gate 		 * resume the idle thread which will dispatch the next
3790Sstevel@tonic-gate 		 * thread to run.
3800Sstevel@tonic-gate 		 */
3810Sstevel@tonic-gate #ifdef DEBUG
3820Sstevel@tonic-gate 		intr_thread_cnt++;
3830Sstevel@tonic-gate #endif
3840Sstevel@tonic-gate 		cpu->cpu_stats.sys.intrblk++;
3850Sstevel@tonic-gate 		/*
3860Sstevel@tonic-gate 		 * Set CPU's base SPL based on active interrupts bitmask
3870Sstevel@tonic-gate 		 */
3880Sstevel@tonic-gate 		set_base_spl();
3890Sstevel@tonic-gate 		basespl = cpu->cpu_base_spl;
3900Sstevel@tonic-gate 		mcpu->mcpu_pri = basespl;
3910Sstevel@tonic-gate 		(*setlvlx)(basespl, vec);
3920Sstevel@tonic-gate 		(void) splhigh();
3930Sstevel@tonic-gate 		it->t_state = TS_FREE;
3940Sstevel@tonic-gate 		/*
3950Sstevel@tonic-gate 		 * Return interrupt thread to pool
3960Sstevel@tonic-gate 		 */
3970Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
3980Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
3990Sstevel@tonic-gate 		swtch();
4000Sstevel@tonic-gate 		/*NOTREACHED*/
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	/*
4040Sstevel@tonic-gate 	 * Return interrupt thread to the pool
4050Sstevel@tonic-gate 	 */
4060Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
4070Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
4080Sstevel@tonic-gate 	it->t_state = TS_FREE;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
4110Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
4120Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
4130Sstevel@tonic-gate 	(*setlvlx)(pil, vec);
4140Sstevel@tonic-gate 	t->t_intr_start = tsc_read();
4150Sstevel@tonic-gate 	cpu->cpu_thread = t;
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
418916Sschwartz /*
419916Sschwartz  * Called with interrupts disabled by an interrupt thread to determine
420916Sschwartz  * how much time has elapsed. See interrupt.s:intr_get_time() for detailed
421916Sschwartz  * theory of operation.
422916Sschwartz  */
423916Sschwartz uint64_t
424916Sschwartz intr_thread_get_time(struct cpu *cpu)
425916Sschwartz {
426916Sschwartz 	struct machcpu *mcpu = &cpu->cpu_m;
427916Sschwartz 	kthread_t *t = cpu->cpu_thread;
428916Sschwartz 	uint64_t time, delta, ret;
429916Sschwartz 	uint_t pil = t->t_pil;
430916Sschwartz 
431916Sschwartz 	ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0);
432916Sschwartz 	ASSERT(t->t_flag & T_INTR_THREAD);
433916Sschwartz 	ASSERT(pil != 0);
434916Sschwartz 	ASSERT(t->t_intr_start != 0);
435916Sschwartz 
436916Sschwartz 	time = tsc_read();
437916Sschwartz 	delta = time - t->t_intr_start;
438916Sschwartz 	t->t_intr_start = time;
439916Sschwartz 
440916Sschwartz 	time = mcpu->intrstat[pil][0] + delta;
441916Sschwartz 	ret = time - mcpu->intrstat[pil][1];
442916Sschwartz 	mcpu->intrstat[pil][0] = time;
443916Sschwartz 	mcpu->intrstat[pil][1] = time;
444916Sschwartz 
445916Sschwartz 	return (ret);
446916Sschwartz }
447916Sschwartz 
4480Sstevel@tonic-gate caddr_t
4490Sstevel@tonic-gate dosoftint_prolog(
4500Sstevel@tonic-gate 	struct cpu *cpu,
4510Sstevel@tonic-gate 	caddr_t stackptr,
4520Sstevel@tonic-gate 	uint32_t st_pending,
4530Sstevel@tonic-gate 	uint_t oldpil)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	kthread_t *t, *volatile it;
4560Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
4570Sstevel@tonic-gate 	uint_t pil;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate top:
4600Sstevel@tonic-gate 	ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	pil = bsrw_insn((uint16_t)st_pending);
4630Sstevel@tonic-gate 	if (pil <= oldpil || pil <= cpu->cpu_base_spl)
4640Sstevel@tonic-gate 		return (0);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/*
4670Sstevel@tonic-gate 	 * XX64	Sigh.
4680Sstevel@tonic-gate 	 *
4690Sstevel@tonic-gate 	 * This is a transliteration of the i386 assembler code for
4700Sstevel@tonic-gate 	 * soft interrupts.  One question is "why does this need
4710Sstevel@tonic-gate 	 * to be atomic?"  One possible race is -other- processors
4720Sstevel@tonic-gate 	 * posting soft interrupts to us in set_pending() i.e. the
4730Sstevel@tonic-gate 	 * CPU might get preempted just after the address computation,
4740Sstevel@tonic-gate 	 * but just before the atomic transaction, so another CPU would
4750Sstevel@tonic-gate 	 * actually set the original CPU's st_pending bit.  However,
4760Sstevel@tonic-gate 	 * it looks like it would be simpler to disable preemption there.
4770Sstevel@tonic-gate 	 * Are there other races for which preemption control doesn't work?
4780Sstevel@tonic-gate 	 *
4790Sstevel@tonic-gate 	 * The i386 assembler version -also- checks to see if the bit
4800Sstevel@tonic-gate 	 * being cleared was actually set; if it wasn't, it rechecks
4810Sstevel@tonic-gate 	 * for more.  This seems a bit strange, as the only code that
4820Sstevel@tonic-gate 	 * ever clears the bit is -this- code running with interrupts
4830Sstevel@tonic-gate 	 * disabled on -this- CPU.  This code would probably be cheaper:
4840Sstevel@tonic-gate 	 *
4850Sstevel@tonic-gate 	 * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending,
4860Sstevel@tonic-gate 	 *   ~(1 << pil));
4870Sstevel@tonic-gate 	 *
4880Sstevel@tonic-gate 	 * and t->t_preempt--/++ around set_pending() even cheaper,
4890Sstevel@tonic-gate 	 * but at this point, correctness is critical, so we slavishly
4900Sstevel@tonic-gate 	 * emulate the i386 port.
4910Sstevel@tonic-gate 	 */
4920Sstevel@tonic-gate 	if (atomic_btr32((uint32_t *)&mcpu->mcpu_softinfo.st_pending, pil)
4930Sstevel@tonic-gate 	    == 0) {
4940Sstevel@tonic-gate 		st_pending = mcpu->mcpu_softinfo.st_pending;
4950Sstevel@tonic-gate 		goto top;
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
4990Sstevel@tonic-gate 	(*setspl)(pil);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/*
5020Sstevel@tonic-gate 	 * Get set to run interrupt thread.
5030Sstevel@tonic-gate 	 * There should always be an interrupt thread since we
5040Sstevel@tonic-gate 	 * allocate one for each level on the CPU.
5050Sstevel@tonic-gate 	 */
5060Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
5070Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
5080Sstevel@tonic-gate 
509*989Sesolom 	/* t_intr_start could be zero due to cpu_intr_swtch_enter. */
510*989Sesolom 	t = cpu->cpu_thread;
511*989Sesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
512*989Sesolom 		hrtime_t intrtime = tsc_read() - t->t_intr_start;
513*989Sesolom 		mcpu->intrstat[pil][0] += intrtime;
514*989Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
515*989Sesolom 		t->t_intr_start = 0;
516*989Sesolom 	}
517*989Sesolom 
5180Sstevel@tonic-gate 	/*
5190Sstevel@tonic-gate 	 * Note that the code in kcpc_overflow_intr -relies- on the
5200Sstevel@tonic-gate 	 * ordering of events here - in particular that t->t_lwp of
5210Sstevel@tonic-gate 	 * the interrupt thread is set to the pinned thread *before*
522*989Sesolom 	 * curthread is changed.
5230Sstevel@tonic-gate 	 */
5240Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
5250Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/*
5280Sstevel@tonic-gate 	 * Push interrupted thread onto list from new thread.
5290Sstevel@tonic-gate 	 * Set the new thread as the current one.
5300Sstevel@tonic-gate 	 * Set interrupted thread's T_SP because if it is the idle thread,
5310Sstevel@tonic-gate 	 * resume() may use that stack between threads.
5320Sstevel@tonic-gate 	 */
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
5350Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	it->t_intr = t;
5380Sstevel@tonic-gate 	cpu->cpu_thread = it;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	/*
5410Sstevel@tonic-gate 	 * Set bit for this pil in CPU's interrupt active bitmask.
5420Sstevel@tonic-gate 	 */
5430Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
5440Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	/*
5470Sstevel@tonic-gate 	 * Initialize thread priority level from intr_pri
5480Sstevel@tonic-gate 	 */
5490Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
5500Sstevel@tonic-gate 	it->t_pri = (pri_t)pil + intr_pri;
5510Sstevel@tonic-gate 	it->t_intr_start = tsc_read();
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	return (it->t_stk);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate void
5570Sstevel@tonic-gate dosoftint_epilog(struct cpu *cpu, uint_t oldpil)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
5600Sstevel@tonic-gate 	kthread_t *t, *it;
5610Sstevel@tonic-gate 	uint_t pil, basespl;
562590Sesolom 	hrtime_t intrtime;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	it = cpu->cpu_thread;
5650Sstevel@tonic-gate 	pil = it->t_pil;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
5700Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
571590Sesolom 	intrtime = tsc_read() - it->t_intr_start;
572916Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
573590Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	/*
5760Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
5770Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
5780Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
5790Sstevel@tonic-gate 	 */
5800Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
5810Sstevel@tonic-gate 		/*
5820Sstevel@tonic-gate 		 * Put thread back on the interrupt thread list.
5830Sstevel@tonic-gate 		 * This was an interrupt thread, so set CPU's base SPL.
5840Sstevel@tonic-gate 		 */
5850Sstevel@tonic-gate 		set_base_spl();
5860Sstevel@tonic-gate 		it->t_state = TS_FREE;
5870Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
5880Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
5890Sstevel@tonic-gate 		(void) splhigh();
5900Sstevel@tonic-gate 		swtch();
5910Sstevel@tonic-gate 		/*NOTREACHED*/
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
5940Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
5950Sstevel@tonic-gate 	it->t_state = TS_FREE;
5960Sstevel@tonic-gate 	cpu->cpu_thread = t;
5970Sstevel@tonic-gate 	if (t->t_flag & T_INTR_THREAD)
5980Sstevel@tonic-gate 		t->t_intr_start = tsc_read();
5990Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
6000Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
6010Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
6020Sstevel@tonic-gate 	(*setspl)(pil);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate /*
6060Sstevel@tonic-gate  * Make the interrupted thread 'to' be runnable.
6070Sstevel@tonic-gate  *
6080Sstevel@tonic-gate  * Since t->t_sp has already been saved, t->t_pc is all
6090Sstevel@tonic-gate  * that needs to be set in this function.
6100Sstevel@tonic-gate  *
6110Sstevel@tonic-gate  * Returns the interrupt level of the interrupt thread.
6120Sstevel@tonic-gate  */
6130Sstevel@tonic-gate int
6140Sstevel@tonic-gate intr_passivate(
6150Sstevel@tonic-gate 	kthread_t *it,		/* interrupt thread */
6160Sstevel@tonic-gate 	kthread_t *t)		/* interrupted thread */
6170Sstevel@tonic-gate {
6180Sstevel@tonic-gate 	extern void _sys_rtt();
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	ASSERT(it->t_flag & T_INTR_THREAD);
6210Sstevel@tonic-gate 	ASSERT(SA(t->t_sp) == t->t_sp);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	t->t_pc = (uintptr_t)_sys_rtt;
6240Sstevel@tonic-gate 	return (it->t_pil);
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate #endif	/* __amd64 */
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate  * Allocate threads and stacks for interrupt handling.
6310Sstevel@tonic-gate  */
6320Sstevel@tonic-gate #define	NINTR_THREADS	(LOCK_LEVEL-1)	/* number of interrupt threads */
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate void
6350Sstevel@tonic-gate init_intr_threads(struct cpu *cp)
6360Sstevel@tonic-gate {
6370Sstevel@tonic-gate 	int i;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	for (i = 0; i < NINTR_THREADS; i++)
6400Sstevel@tonic-gate 		thread_create_intr(cp);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	cp->cpu_intr_stack = (caddr_t)segkp_get(segkp, INTR_STACK_SIZE,
6430Sstevel@tonic-gate 		KPD_HASREDZONE | KPD_NO_ANON | KPD_LOCKED) +
6440Sstevel@tonic-gate 		INTR_STACK_SIZE - SA(MINFRAME);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate /*
6480Sstevel@tonic-gate  * Create interrupt kstats for this CPU.
6490Sstevel@tonic-gate  */
6500Sstevel@tonic-gate void
6510Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	int		i;
6540Sstevel@tonic-gate 	kstat_t		*intr_ksp;
6550Sstevel@tonic-gate 	kstat_named_t	*knp;
6560Sstevel@tonic-gate 	char		name[KSTAT_STRLEN];
6570Sstevel@tonic-gate 	zoneid_t	zoneid;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	if (pool_pset_enabled())
6620Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
6630Sstevel@tonic-gate 	else
6640Sstevel@tonic-gate 		zoneid = ALL_ZONES;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
6670Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/*
6700Sstevel@tonic-gate 	 * Initialize each PIL's named kstat
6710Sstevel@tonic-gate 	 */
6720Sstevel@tonic-gate 	if (intr_ksp != NULL) {
6730Sstevel@tonic-gate 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
6740Sstevel@tonic-gate 		knp = (kstat_named_t *)intr_ksp->ks_data;
6750Sstevel@tonic-gate 		intr_ksp->ks_private = cp;
6760Sstevel@tonic-gate 		for (i = 0; i < PIL_MAX; i++) {
6770Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
6780Sstevel@tonic-gate 			    i + 1);
6790Sstevel@tonic-gate 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
6800Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
6810Sstevel@tonic-gate 			    i + 1);
6820Sstevel@tonic-gate 			kstat_named_init(&knp[(i * 2) + 1], name,
6830Sstevel@tonic-gate 			    KSTAT_DATA_UINT64);
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 		kstat_install(intr_ksp);
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate /*
6900Sstevel@tonic-gate  * Delete interrupt kstats for this CPU.
6910Sstevel@tonic-gate  */
6920Sstevel@tonic-gate void
6930Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate /*
6990Sstevel@tonic-gate  * Convert interrupt statistics from CPU ticks to nanoseconds and
7000Sstevel@tonic-gate  * update kstat.
7010Sstevel@tonic-gate  */
7020Sstevel@tonic-gate int
7030Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	kstat_named_t	*knp = ksp->ks_data;
7060Sstevel@tonic-gate 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
7070Sstevel@tonic-gate 	int		i;
7080Sstevel@tonic-gate 	hrtime_t	hrt;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7110Sstevel@tonic-gate 		return (EACCES);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
714916Sschwartz 		hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0];
7150Sstevel@tonic-gate 		tsc_scalehrtime(&hrt);
7160Sstevel@tonic-gate 		knp[i * 2].value.ui64 = (uint64_t)hrt;
7170Sstevel@tonic-gate 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	return (0);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate /*
7240Sstevel@tonic-gate  * An interrupt thread is ending a time slice, so compute the interval it
7250Sstevel@tonic-gate  * ran for and update the statistic for its PIL.
7260Sstevel@tonic-gate  */
7270Sstevel@tonic-gate void
7280Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	uint64_t	interval;
7310Sstevel@tonic-gate 	uint64_t	start;
732590Sesolom 	cpu_t		*cpu;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
7350Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	/*
7380Sstevel@tonic-gate 	 * We could be here with a zero timestamp. This could happen if:
7390Sstevel@tonic-gate 	 * an interrupt thread which no longer has a pinned thread underneath
7400Sstevel@tonic-gate 	 * it (i.e. it blocked at some point in its past) has finished running
7410Sstevel@tonic-gate 	 * its handler. intr_thread() updated the interrupt statistic for its
7420Sstevel@tonic-gate 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
7430Sstevel@tonic-gate 	 * return to, swtch() gets called and we end up here.
744590Sesolom 	 *
745590Sesolom 	 * Note that we use atomic ops below (cas64 and atomic_add_64), which
746590Sesolom 	 * we don't use in the functions above, because we're not called
747590Sesolom 	 * with interrupts blocked, but the epilog/prolog functions are.
7480Sstevel@tonic-gate 	 */
7490Sstevel@tonic-gate 	if (t->t_intr_start) {
7500Sstevel@tonic-gate 		do {
7510Sstevel@tonic-gate 			start = t->t_intr_start;
7520Sstevel@tonic-gate 			interval = tsc_read() - start;
7530Sstevel@tonic-gate 		} while (cas64(&t->t_intr_start, start, 0) != start);
754590Sesolom 		cpu = CPU;
755916Sschwartz 		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
756590Sesolom 
757590Sesolom 		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
758590Sesolom 		    interval);
7590Sstevel@tonic-gate 	} else
7600Sstevel@tonic-gate 		ASSERT(t->t_intr == NULL);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate /*
7640Sstevel@tonic-gate  * An interrupt thread is returning from swtch(). Place a starting timestamp
7650Sstevel@tonic-gate  * in its thread structure.
7660Sstevel@tonic-gate  */
7670Sstevel@tonic-gate void
7680Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t)
7690Sstevel@tonic-gate {
7700Sstevel@tonic-gate 	uint64_t ts;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
7730Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	do {
7760Sstevel@tonic-gate 		ts = t->t_intr_start;
7770Sstevel@tonic-gate 	} while (cas64(&t->t_intr_start, ts, tsc_read()) != ts);
7780Sstevel@tonic-gate }
779