xref: /onnv-gate/usr/src/uts/i86pc/os/intr.c (revision 4503:8cf697a3acff)
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
51455Sandrei  * Common Development and Distribution License (the "License").
61455Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/cpuvar.h>
290Sstevel@tonic-gate #include <sys/regset.h>
300Sstevel@tonic-gate #include <sys/psw.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/thread.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/segments.h>
350Sstevel@tonic-gate #include <sys/pcb.h>
360Sstevel@tonic-gate #include <sys/trap.h>
370Sstevel@tonic-gate #include <sys/ftrace.h>
380Sstevel@tonic-gate #include <sys/traptrace.h>
390Sstevel@tonic-gate #include <sys/clock.h>
400Sstevel@tonic-gate #include <sys/panic.h>
410Sstevel@tonic-gate #include <sys/disp.h>
420Sstevel@tonic-gate #include <vm/seg_kp.h>
430Sstevel@tonic-gate #include <sys/stack.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate #include <sys/cmn_err.h>
460Sstevel@tonic-gate #include <sys/kstat.h>
470Sstevel@tonic-gate #include <sys/smp_impldefs.h>
480Sstevel@tonic-gate #include <sys/pool_pset.h>
490Sstevel@tonic-gate #include <sys/zone.h>
500Sstevel@tonic-gate #include <sys/bitmap.h>
513446Smrj #include <sys/archsystm.h>
523446Smrj #include <sys/machsystm.h>
533446Smrj #include <sys/ontrap.h>
543446Smrj #include <sys/x86_archext.h>
553446Smrj #include <sys/promif.h>
564191Sjosephb #include <vm/hat_i86.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
603446Smrj  * Set cpu's base SPL level to the highest active interrupt level
610Sstevel@tonic-gate  */
623446Smrj void
633446Smrj set_base_spl(void)
640Sstevel@tonic-gate {
653446Smrj 	struct cpu *cpu = CPU;
663446Smrj 	uint16_t active = (uint16_t)cpu->cpu_intr_actv;
670Sstevel@tonic-gate 
683446Smrj 	cpu->cpu_base_spl = active == 0 ? 0 : bsrw_insn(active);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * Do all the work necessary to set up the cpu and thread structures
730Sstevel@tonic-gate  * to dispatch a high-level interrupt.
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  * Returns 0 if we're -not- already on the high-level interrupt stack,
760Sstevel@tonic-gate  * (and *must* switch to it), non-zero if we are already on that stack.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * Called with interrupts masked.
790Sstevel@tonic-gate  * The 'pil' is already set to the appropriate level for rp->r_trapno.
800Sstevel@tonic-gate  */
813446Smrj static int
820Sstevel@tonic-gate hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
850Sstevel@tonic-gate 	uint_t mask;
86590Sesolom 	hrtime_t intrtime;
873446Smrj 	hrtime_t now = tsc_read();
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	ASSERT(pil > LOCK_LEVEL);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (pil == CBE_HIGH_PIL) {
920Sstevel@tonic-gate 		cpu->cpu_profile_pil = oldpil;
930Sstevel@tonic-gate 		if (USERMODE(rp->r_cs)) {
940Sstevel@tonic-gate 			cpu->cpu_profile_pc = 0;
950Sstevel@tonic-gate 			cpu->cpu_profile_upc = rp->r_pc;
960Sstevel@tonic-gate 		} else {
970Sstevel@tonic-gate 			cpu->cpu_profile_pc = rp->r_pc;
980Sstevel@tonic-gate 			cpu->cpu_profile_upc = 0;
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 	}
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
1030Sstevel@tonic-gate 	if (mask != 0) {
1040Sstevel@tonic-gate 		int nestpil;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 		/*
1070Sstevel@tonic-gate 		 * We have interrupted another high-level interrupt.
1080Sstevel@tonic-gate 		 * Load starting timestamp, compute interval, update
1090Sstevel@tonic-gate 		 * cumulative counter.
1100Sstevel@tonic-gate 		 */
1110Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
1120Sstevel@tonic-gate 		ASSERT(nestpil < pil);
1133446Smrj 		intrtime = now -
1140Sstevel@tonic-gate 		    mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)];
115916Sschwartz 		mcpu->intrstat[nestpil][0] += intrtime;
116590Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1170Sstevel@tonic-gate 		/*
1180Sstevel@tonic-gate 		 * Another high-level interrupt is active below this one, so
1190Sstevel@tonic-gate 		 * there is no need to check for an interrupt thread.  That
1200Sstevel@tonic-gate 		 * will be done by the lowest priority high-level interrupt
1210Sstevel@tonic-gate 		 * active.
1220Sstevel@tonic-gate 		 */
1230Sstevel@tonic-gate 	} else {
1240Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 		/*
1270Sstevel@tonic-gate 		 * See if we are interrupting a low-level interrupt thread.
1280Sstevel@tonic-gate 		 * If so, account for its time slice only if its time stamp
1290Sstevel@tonic-gate 		 * is non-zero.
1300Sstevel@tonic-gate 		 */
1310Sstevel@tonic-gate 		if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) {
1323446Smrj 			intrtime = now - t->t_intr_start;
133916Sschwartz 			mcpu->intrstat[t->t_pil][0] += intrtime;
134590Sesolom 			cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1350Sstevel@tonic-gate 			t->t_intr_start = 0;
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	/*
1400Sstevel@tonic-gate 	 * Store starting timestamp in CPU structure for this PIL.
1410Sstevel@tonic-gate 	 */
1423446Smrj 	mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = now;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if (pil == 15) {
1470Sstevel@tonic-gate 		/*
1480Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
1490Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
1500Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
1510Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
1520Sstevel@tonic-gate 		 */
1530Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
1540Sstevel@tonic-gate 		(*refcntp)++;
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate  * Does most of the work of returning from a high level interrupt.
1660Sstevel@tonic-gate  *
1670Sstevel@tonic-gate  * Returns 0 if there are no more high level interrupts (in which
1680Sstevel@tonic-gate  * case we must switch back to the interrupted thread stack) or
1690Sstevel@tonic-gate  * non-zero if there are more (in which case we should stay on it).
1700Sstevel@tonic-gate  *
1710Sstevel@tonic-gate  * Called with interrupts masked
1720Sstevel@tonic-gate  */
1733446Smrj static int
1740Sstevel@tonic-gate hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
1770Sstevel@tonic-gate 	uint_t mask;
178590Sesolom 	hrtime_t intrtime;
1793446Smrj 	hrtime_t now = tsc_read();
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	ASSERT(mcpu->mcpu_pri == pil);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (pil == 15) {
1880Sstevel@tonic-gate 		/*
1890Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
1900Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
1910Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
1920Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
1930Sstevel@tonic-gate 		 */
1940Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 		ASSERT(*refcntp > 0);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		if (--(*refcntp) == 0)
1990Sstevel@tonic-gate 			cpu->cpu_intr_actv &= ~(1 << pil);
2000Sstevel@tonic-gate 	} else {
2010Sstevel@tonic-gate 		cpu->cpu_intr_actv &= ~(1 << pil);
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0);
2050Sstevel@tonic-gate 
2063446Smrj 	intrtime = now - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)];
207916Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
208590Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/*
2110Sstevel@tonic-gate 	 * Check for lower-pil nested high-level interrupt beneath
2120Sstevel@tonic-gate 	 * current one.  If so, place a starting timestamp in its
2130Sstevel@tonic-gate 	 * pil_high_start entry.
2140Sstevel@tonic-gate 	 */
2150Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
2160Sstevel@tonic-gate 	if (mask != 0) {
2170Sstevel@tonic-gate 		int nestpil;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		/*
2200Sstevel@tonic-gate 		 * find PIL of nested interrupt
2210Sstevel@tonic-gate 		 */
2220Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
2230Sstevel@tonic-gate 		ASSERT(nestpil < pil);
2243446Smrj 		mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = now;
2250Sstevel@tonic-gate 		/*
2260Sstevel@tonic-gate 		 * (Another high-level interrupt is active below this one,
2270Sstevel@tonic-gate 		 * so there is no need to check for an interrupt
2280Sstevel@tonic-gate 		 * thread.  That will be done by the lowest priority
2290Sstevel@tonic-gate 		 * high-level interrupt active.)
2300Sstevel@tonic-gate 		 */
2310Sstevel@tonic-gate 	} else {
2320Sstevel@tonic-gate 		/*
2330Sstevel@tonic-gate 		 * Check to see if there is a low-level interrupt active.
2340Sstevel@tonic-gate 		 * If so, place a starting timestamp in the thread
2350Sstevel@tonic-gate 		 * structure.
2360Sstevel@tonic-gate 		 */
2370Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		if (t->t_flag & T_INTR_THREAD)
2403446Smrj 			t->t_intr_start = now;
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	mcpu->mcpu_pri = oldpil;
2440Sstevel@tonic-gate 	(void) (*setlvlx)(oldpil, vecnum);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate  * Set up the cpu, thread and interrupt thread structures for
2510Sstevel@tonic-gate  * executing an interrupt thread.  The new stack pointer of the
2520Sstevel@tonic-gate  * interrupt thread (which *must* be switched to) is returned.
2530Sstevel@tonic-gate  */
2543446Smrj static caddr_t
2550Sstevel@tonic-gate intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
2580Sstevel@tonic-gate 	kthread_t *t, *volatile it;
2593446Smrj 	hrtime_t now = tsc_read();
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	ASSERT(pil > 0);
2620Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
2630Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	/*
2660Sstevel@tonic-gate 	 * Get set to run an interrupt thread.
2670Sstevel@tonic-gate 	 * There should always be an interrupt thread, since we
2680Sstevel@tonic-gate 	 * allocate one for each level on each CPU.
2690Sstevel@tonic-gate 	 *
270989Sesolom 	 * t_intr_start could be zero due to cpu_intr_swtch_enter.
2710Sstevel@tonic-gate 	 */
2720Sstevel@tonic-gate 	t = cpu->cpu_thread;
273989Sesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
2743446Smrj 		hrtime_t intrtime = now - t->t_intr_start;
275916Sschwartz 		mcpu->intrstat[t->t_pil][0] += intrtime;
276590Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
2770Sstevel@tonic-gate 		t->t_intr_start = 0;
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;	/* mark stack in curthread for resume */
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	/*
2850Sstevel@tonic-gate 	 * unlink the interrupt thread off the cpu
286989Sesolom 	 *
287989Sesolom 	 * Note that the code in kcpc_overflow_intr -relies- on the
288989Sesolom 	 * ordering of events here - in particular that t->t_lwp of
289989Sesolom 	 * the interrupt thread is set to the pinned thread *before*
290989Sesolom 	 * curthread is changed.
2910Sstevel@tonic-gate 	 */
2920Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
2930Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
2940Sstevel@tonic-gate 	it->t_intr = t;
2950Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/*
2980Sstevel@tonic-gate 	 * (threads on the interrupt thread free list could have state
2990Sstevel@tonic-gate 	 * preset to TS_ONPROC, but it helps in debugging if
3000Sstevel@tonic-gate 	 * they're TS_FREE.)
3010Sstevel@tonic-gate 	 */
3020Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	cpu->cpu_thread = it;		/* new curthread on this cpu */
3050Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
3060Sstevel@tonic-gate 	it->t_pri = intr_pri + (pri_t)pil;
3073446Smrj 	it->t_intr_start = now;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	return (it->t_stk);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate #ifdef DEBUG
3140Sstevel@tonic-gate int intr_thread_cnt;
3150Sstevel@tonic-gate #endif
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate /*
3180Sstevel@tonic-gate  * Called with interrupts disabled
3190Sstevel@tonic-gate  */
3203446Smrj static void
3210Sstevel@tonic-gate intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
3240Sstevel@tonic-gate 	kthread_t *t;
3250Sstevel@tonic-gate 	kthread_t *it = cpu->cpu_thread;	/* curthread */
3260Sstevel@tonic-gate 	uint_t pil, basespl;
327590Sesolom 	hrtime_t intrtime;
3283446Smrj 	hrtime_t now = tsc_read();
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	pil = it->t_pil;
3310Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	ASSERT(it->t_intr_start != 0);
3343446Smrj 	intrtime = now - it->t_intr_start;
335916Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
336590Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
3390Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/*
3420Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
3430Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
3440Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
3470Sstevel@tonic-gate 		/*
3480Sstevel@tonic-gate 		 * The interrupted thread is no longer pinned underneath
3490Sstevel@tonic-gate 		 * the interrupt thread.  This means the interrupt must
3500Sstevel@tonic-gate 		 * have blocked, and the interrupted thread has been
3510Sstevel@tonic-gate 		 * unpinned, and has probably been running around the
3520Sstevel@tonic-gate 		 * system for a while.
3530Sstevel@tonic-gate 		 *
3540Sstevel@tonic-gate 		 * Since there is no longer a thread under this one, put
3550Sstevel@tonic-gate 		 * this interrupt thread back on the CPU's free list and
3560Sstevel@tonic-gate 		 * resume the idle thread which will dispatch the next
3570Sstevel@tonic-gate 		 * thread to run.
3580Sstevel@tonic-gate 		 */
3590Sstevel@tonic-gate #ifdef DEBUG
3600Sstevel@tonic-gate 		intr_thread_cnt++;
3610Sstevel@tonic-gate #endif
3620Sstevel@tonic-gate 		cpu->cpu_stats.sys.intrblk++;
3630Sstevel@tonic-gate 		/*
3640Sstevel@tonic-gate 		 * Set CPU's base SPL based on active interrupts bitmask
3650Sstevel@tonic-gate 		 */
3660Sstevel@tonic-gate 		set_base_spl();
3670Sstevel@tonic-gate 		basespl = cpu->cpu_base_spl;
3680Sstevel@tonic-gate 		mcpu->mcpu_pri = basespl;
3690Sstevel@tonic-gate 		(*setlvlx)(basespl, vec);
3700Sstevel@tonic-gate 		(void) splhigh();
3713446Smrj 		sti();
3720Sstevel@tonic-gate 		it->t_state = TS_FREE;
3730Sstevel@tonic-gate 		/*
3740Sstevel@tonic-gate 		 * Return interrupt thread to pool
3750Sstevel@tonic-gate 		 */
3760Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
3770Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
3780Sstevel@tonic-gate 		swtch();
3793446Smrj 		panic("intr_thread_epilog: swtch returned");
3800Sstevel@tonic-gate 		/*NOTREACHED*/
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	/*
3840Sstevel@tonic-gate 	 * Return interrupt thread to the pool
3850Sstevel@tonic-gate 	 */
3860Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
3870Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
3880Sstevel@tonic-gate 	it->t_state = TS_FREE;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
3910Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
3920Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
3930Sstevel@tonic-gate 	(*setlvlx)(pil, vec);
3943446Smrj 	t->t_intr_start = now;
3950Sstevel@tonic-gate 	cpu->cpu_thread = t;
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate 
398916Sschwartz /*
3993446Smrj  * intr_get_time() is a resource for interrupt handlers to determine how
4003446Smrj  * much time has been spent handling the current interrupt. Such a function
4013446Smrj  * is needed because higher level interrupts can arrive during the
4023446Smrj  * processing of an interrupt.  intr_get_time() only returns time spent in the
4033446Smrj  * current interrupt handler.
4043446Smrj  *
4053446Smrj  * The caller must be calling from an interrupt handler running at a pil
4063446Smrj  * below or at lock level. Timings are not provided for high-level
4073446Smrj  * interrupts.
4083446Smrj  *
4093446Smrj  * The first time intr_get_time() is called while handling an interrupt,
4103446Smrj  * it returns the time since the interrupt handler was invoked. Subsequent
4113446Smrj  * calls will return the time since the prior call to intr_get_time(). Time
4123446Smrj  * is returned as ticks. Use tsc_scalehrtime() to convert ticks to nsec.
4133446Smrj  *
4143446Smrj  * Theory Of Intrstat[][]:
4153446Smrj  *
4163446Smrj  * uint64_t intrstat[pil][0..1] is an array indexed by pil level, with two
4173446Smrj  * uint64_ts per pil.
4183446Smrj  *
4193446Smrj  * intrstat[pil][0] is a cumulative count of the number of ticks spent
4203446Smrj  * handling all interrupts at the specified pil on this CPU. It is
4213446Smrj  * exported via kstats to the user.
4223446Smrj  *
4233446Smrj  * intrstat[pil][1] is always a count of ticks less than or equal to the
4243446Smrj  * value in [0]. The difference between [1] and [0] is the value returned
4253446Smrj  * by a call to intr_get_time(). At the start of interrupt processing,
4263446Smrj  * [0] and [1] will be equal (or nearly so). As the interrupt consumes
4273446Smrj  * time, [0] will increase, but [1] will remain the same. A call to
4283446Smrj  * intr_get_time() will return the difference, then update [1] to be the
4293446Smrj  * same as [0]. Future calls will return the time since the last call.
4303446Smrj  * Finally, when the interrupt completes, [1] is updated to the same as [0].
4313446Smrj  *
4323446Smrj  * Implementation:
4333446Smrj  *
4343446Smrj  * intr_get_time() works much like a higher level interrupt arriving. It
4353446Smrj  * "checkpoints" the timing information by incrementing intrstat[pil][0]
4363446Smrj  * to include elapsed running time, and by setting t_intr_start to rdtsc.
4373446Smrj  * It then sets the return value to intrstat[pil][0] - intrstat[pil][1],
4383446Smrj  * and updates intrstat[pil][1] to be the same as the new value of
4393446Smrj  * intrstat[pil][0].
4403446Smrj  *
4413446Smrj  * In the normal handling of interrupts, after an interrupt handler returns
4423446Smrj  * and the code in intr_thread() updates intrstat[pil][0], it then sets
4433446Smrj  * intrstat[pil][1] to the new value of intrstat[pil][0]. When [0] == [1],
4443446Smrj  * the timings are reset, i.e. intr_get_time() will return [0] - [1] which
4453446Smrj  * is 0.
4463446Smrj  *
4473446Smrj  * Whenever interrupts arrive on a CPU which is handling a lower pil
4483446Smrj  * interrupt, they update the lower pil's [0] to show time spent in the
4493446Smrj  * handler that they've interrupted. This results in a growing discrepancy
4503446Smrj  * between [0] and [1], which is returned the next time intr_get_time() is
4513446Smrj  * called. Time spent in the higher-pil interrupt will not be returned in
4523446Smrj  * the next intr_get_time() call from the original interrupt, because
4533446Smrj  * the higher-pil interrupt's time is accumulated in intrstat[higherpil][].
454916Sschwartz  */
455916Sschwartz uint64_t
4563446Smrj intr_get_time(void)
457916Sschwartz {
4583446Smrj 	struct cpu *cpu;
4593446Smrj 	struct machcpu *mcpu;
4603446Smrj 	kthread_t *t;
461916Sschwartz 	uint64_t time, delta, ret;
4623446Smrj 	uint_t pil;
463916Sschwartz 
4643446Smrj 	cli();
4653446Smrj 	cpu = CPU;
4663446Smrj 	mcpu = &cpu->cpu_m;
4673446Smrj 	t = cpu->cpu_thread;
4683446Smrj 	pil = t->t_pil;
469916Sschwartz 	ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0);
470916Sschwartz 	ASSERT(t->t_flag & T_INTR_THREAD);
471916Sschwartz 	ASSERT(pil != 0);
472916Sschwartz 	ASSERT(t->t_intr_start != 0);
473916Sschwartz 
474916Sschwartz 	time = tsc_read();
475916Sschwartz 	delta = time - t->t_intr_start;
476916Sschwartz 	t->t_intr_start = time;
477916Sschwartz 
478916Sschwartz 	time = mcpu->intrstat[pil][0] + delta;
479916Sschwartz 	ret = time - mcpu->intrstat[pil][1];
480916Sschwartz 	mcpu->intrstat[pil][0] = time;
481916Sschwartz 	mcpu->intrstat[pil][1] = time;
4821887Sjhaslam 	cpu->cpu_intracct[cpu->cpu_mstate] += delta;
483916Sschwartz 
4843446Smrj 	sti();
485916Sschwartz 	return (ret);
486916Sschwartz }
487916Sschwartz 
4883446Smrj static caddr_t
4890Sstevel@tonic-gate dosoftint_prolog(
4900Sstevel@tonic-gate 	struct cpu *cpu,
4910Sstevel@tonic-gate 	caddr_t stackptr,
4920Sstevel@tonic-gate 	uint32_t st_pending,
4930Sstevel@tonic-gate 	uint_t oldpil)
4940Sstevel@tonic-gate {
4950Sstevel@tonic-gate 	kthread_t *t, *volatile it;
4960Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
4970Sstevel@tonic-gate 	uint_t pil;
4983446Smrj 	hrtime_t now;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate top:
5010Sstevel@tonic-gate 	ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	pil = bsrw_insn((uint16_t)st_pending);
5040Sstevel@tonic-gate 	if (pil <= oldpil || pil <= cpu->cpu_base_spl)
5050Sstevel@tonic-gate 		return (0);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * XX64	Sigh.
5090Sstevel@tonic-gate 	 *
5100Sstevel@tonic-gate 	 * This is a transliteration of the i386 assembler code for
5110Sstevel@tonic-gate 	 * soft interrupts.  One question is "why does this need
5120Sstevel@tonic-gate 	 * to be atomic?"  One possible race is -other- processors
5130Sstevel@tonic-gate 	 * posting soft interrupts to us in set_pending() i.e. the
5140Sstevel@tonic-gate 	 * CPU might get preempted just after the address computation,
5150Sstevel@tonic-gate 	 * but just before the atomic transaction, so another CPU would
5160Sstevel@tonic-gate 	 * actually set the original CPU's st_pending bit.  However,
5170Sstevel@tonic-gate 	 * it looks like it would be simpler to disable preemption there.
5180Sstevel@tonic-gate 	 * Are there other races for which preemption control doesn't work?
5190Sstevel@tonic-gate 	 *
5200Sstevel@tonic-gate 	 * The i386 assembler version -also- checks to see if the bit
5210Sstevel@tonic-gate 	 * being cleared was actually set; if it wasn't, it rechecks
5220Sstevel@tonic-gate 	 * for more.  This seems a bit strange, as the only code that
5230Sstevel@tonic-gate 	 * ever clears the bit is -this- code running with interrupts
5240Sstevel@tonic-gate 	 * disabled on -this- CPU.  This code would probably be cheaper:
5250Sstevel@tonic-gate 	 *
5260Sstevel@tonic-gate 	 * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending,
5270Sstevel@tonic-gate 	 *   ~(1 << pil));
5280Sstevel@tonic-gate 	 *
5290Sstevel@tonic-gate 	 * and t->t_preempt--/++ around set_pending() even cheaper,
5300Sstevel@tonic-gate 	 * but at this point, correctness is critical, so we slavishly
5310Sstevel@tonic-gate 	 * emulate the i386 port.
5320Sstevel@tonic-gate 	 */
5333446Smrj 	if (atomic_btr32((uint32_t *)
5343446Smrj 	    &mcpu->mcpu_softinfo.st_pending, pil) == 0) {
5350Sstevel@tonic-gate 		st_pending = mcpu->mcpu_softinfo.st_pending;
5360Sstevel@tonic-gate 		goto top;
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
5400Sstevel@tonic-gate 	(*setspl)(pil);
5410Sstevel@tonic-gate 
5423446Smrj 	now = tsc_read();
5433446Smrj 
5440Sstevel@tonic-gate 	/*
5450Sstevel@tonic-gate 	 * Get set to run interrupt thread.
5460Sstevel@tonic-gate 	 * There should always be an interrupt thread since we
5470Sstevel@tonic-gate 	 * allocate one for each level on the CPU.
5480Sstevel@tonic-gate 	 */
5490Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
5500Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
5510Sstevel@tonic-gate 
552989Sesolom 	/* t_intr_start could be zero due to cpu_intr_swtch_enter. */
553989Sesolom 	t = cpu->cpu_thread;
554989Sesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
5553446Smrj 		hrtime_t intrtime = now - t->t_intr_start;
556989Sesolom 		mcpu->intrstat[pil][0] += intrtime;
557989Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
558989Sesolom 		t->t_intr_start = 0;
559989Sesolom 	}
560989Sesolom 
5610Sstevel@tonic-gate 	/*
5620Sstevel@tonic-gate 	 * Note that the code in kcpc_overflow_intr -relies- on the
5630Sstevel@tonic-gate 	 * ordering of events here - in particular that t->t_lwp of
5640Sstevel@tonic-gate 	 * the interrupt thread is set to the pinned thread *before*
565989Sesolom 	 * curthread is changed.
5660Sstevel@tonic-gate 	 */
5670Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
5680Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	/*
5710Sstevel@tonic-gate 	 * Push interrupted thread onto list from new thread.
5720Sstevel@tonic-gate 	 * Set the new thread as the current one.
5730Sstevel@tonic-gate 	 * Set interrupted thread's T_SP because if it is the idle thread,
5740Sstevel@tonic-gate 	 * resume() may use that stack between threads.
5750Sstevel@tonic-gate 	 */
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
5780Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	it->t_intr = t;
5810Sstevel@tonic-gate 	cpu->cpu_thread = it;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/*
5840Sstevel@tonic-gate 	 * Set bit for this pil in CPU's interrupt active bitmask.
5850Sstevel@tonic-gate 	 */
5860Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
5870Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	/*
5900Sstevel@tonic-gate 	 * Initialize thread priority level from intr_pri
5910Sstevel@tonic-gate 	 */
5920Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
5930Sstevel@tonic-gate 	it->t_pri = (pri_t)pil + intr_pri;
5943446Smrj 	it->t_intr_start = now;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	return (it->t_stk);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5993446Smrj static void
6000Sstevel@tonic-gate dosoftint_epilog(struct cpu *cpu, uint_t oldpil)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
6030Sstevel@tonic-gate 	kthread_t *t, *it;
6040Sstevel@tonic-gate 	uint_t pil, basespl;
605590Sesolom 	hrtime_t intrtime;
6063446Smrj 	hrtime_t now = tsc_read();
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	it = cpu->cpu_thread;
6090Sstevel@tonic-gate 	pil = it->t_pil;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
6140Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
6153446Smrj 	intrtime = now - it->t_intr_start;
616916Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
617590Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	/*
6200Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
6210Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
6220Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
6230Sstevel@tonic-gate 	 */
6240Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
6250Sstevel@tonic-gate 		/*
6260Sstevel@tonic-gate 		 * Put thread back on the interrupt thread list.
6270Sstevel@tonic-gate 		 * This was an interrupt thread, so set CPU's base SPL.
6280Sstevel@tonic-gate 		 */
6290Sstevel@tonic-gate 		set_base_spl();
6300Sstevel@tonic-gate 		it->t_state = TS_FREE;
6310Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
6320Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
6330Sstevel@tonic-gate 		(void) splhigh();
6343446Smrj 		sti();
6350Sstevel@tonic-gate 		swtch();
6360Sstevel@tonic-gate 		/*NOTREACHED*/
6373446Smrj 		panic("dosoftint_epilog: swtch returned");
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
6400Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
6410Sstevel@tonic-gate 	it->t_state = TS_FREE;
6420Sstevel@tonic-gate 	cpu->cpu_thread = t;
6430Sstevel@tonic-gate 	if (t->t_flag & T_INTR_THREAD)
6443446Smrj 		t->t_intr_start = now;
6450Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
6460Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
6470Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
6480Sstevel@tonic-gate 	(*setspl)(pil);
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate 
6513446Smrj 
6520Sstevel@tonic-gate /*
6530Sstevel@tonic-gate  * Make the interrupted thread 'to' be runnable.
6540Sstevel@tonic-gate  *
6550Sstevel@tonic-gate  * Since t->t_sp has already been saved, t->t_pc is all
6560Sstevel@tonic-gate  * that needs to be set in this function.
6570Sstevel@tonic-gate  *
6580Sstevel@tonic-gate  * Returns the interrupt level of the interrupt thread.
6590Sstevel@tonic-gate  */
6600Sstevel@tonic-gate int
6610Sstevel@tonic-gate intr_passivate(
6620Sstevel@tonic-gate 	kthread_t *it,		/* interrupt thread */
6630Sstevel@tonic-gate 	kthread_t *t)		/* interrupted thread */
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	extern void _sys_rtt();
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	ASSERT(it->t_flag & T_INTR_THREAD);
6680Sstevel@tonic-gate 	ASSERT(SA(t->t_sp) == t->t_sp);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	t->t_pc = (uintptr_t)_sys_rtt;
6710Sstevel@tonic-gate 	return (it->t_pil);
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate /*
6750Sstevel@tonic-gate  * Create interrupt kstats for this CPU.
6760Sstevel@tonic-gate  */
6770Sstevel@tonic-gate void
6780Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp)
6790Sstevel@tonic-gate {
6800Sstevel@tonic-gate 	int		i;
6810Sstevel@tonic-gate 	kstat_t		*intr_ksp;
6820Sstevel@tonic-gate 	kstat_named_t	*knp;
6830Sstevel@tonic-gate 	char		name[KSTAT_STRLEN];
6840Sstevel@tonic-gate 	zoneid_t	zoneid;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	if (pool_pset_enabled())
6890Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
6900Sstevel@tonic-gate 	else
6910Sstevel@tonic-gate 		zoneid = ALL_ZONES;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
6940Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	/*
6970Sstevel@tonic-gate 	 * Initialize each PIL's named kstat
6980Sstevel@tonic-gate 	 */
6990Sstevel@tonic-gate 	if (intr_ksp != NULL) {
7000Sstevel@tonic-gate 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
7010Sstevel@tonic-gate 		knp = (kstat_named_t *)intr_ksp->ks_data;
7020Sstevel@tonic-gate 		intr_ksp->ks_private = cp;
7030Sstevel@tonic-gate 		for (i = 0; i < PIL_MAX; i++) {
7040Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
7050Sstevel@tonic-gate 			    i + 1);
7060Sstevel@tonic-gate 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
7070Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
7080Sstevel@tonic-gate 			    i + 1);
7090Sstevel@tonic-gate 			kstat_named_init(&knp[(i * 2) + 1], name,
7100Sstevel@tonic-gate 			    KSTAT_DATA_UINT64);
7110Sstevel@tonic-gate 		}
7120Sstevel@tonic-gate 		kstat_install(intr_ksp);
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate /*
7170Sstevel@tonic-gate  * Delete interrupt kstats for this CPU.
7180Sstevel@tonic-gate  */
7190Sstevel@tonic-gate void
7200Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp)
7210Sstevel@tonic-gate {
7220Sstevel@tonic-gate 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate  * Convert interrupt statistics from CPU ticks to nanoseconds and
7270Sstevel@tonic-gate  * update kstat.
7280Sstevel@tonic-gate  */
7290Sstevel@tonic-gate int
7300Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
7310Sstevel@tonic-gate {
7320Sstevel@tonic-gate 	kstat_named_t	*knp = ksp->ks_data;
7330Sstevel@tonic-gate 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
7340Sstevel@tonic-gate 	int		i;
7350Sstevel@tonic-gate 	hrtime_t	hrt;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7380Sstevel@tonic-gate 		return (EACCES);
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
741916Sschwartz 		hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0];
7420Sstevel@tonic-gate 		tsc_scalehrtime(&hrt);
7430Sstevel@tonic-gate 		knp[i * 2].value.ui64 = (uint64_t)hrt;
7440Sstevel@tonic-gate 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	return (0);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate /*
7510Sstevel@tonic-gate  * An interrupt thread is ending a time slice, so compute the interval it
7520Sstevel@tonic-gate  * ran for and update the statistic for its PIL.
7530Sstevel@tonic-gate  */
7540Sstevel@tonic-gate void
7550Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t)
7560Sstevel@tonic-gate {
7570Sstevel@tonic-gate 	uint64_t	interval;
7580Sstevel@tonic-gate 	uint64_t	start;
759590Sesolom 	cpu_t		*cpu;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
7620Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	/*
7650Sstevel@tonic-gate 	 * We could be here with a zero timestamp. This could happen if:
7660Sstevel@tonic-gate 	 * an interrupt thread which no longer has a pinned thread underneath
7670Sstevel@tonic-gate 	 * it (i.e. it blocked at some point in its past) has finished running
7680Sstevel@tonic-gate 	 * its handler. intr_thread() updated the interrupt statistic for its
7690Sstevel@tonic-gate 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
7700Sstevel@tonic-gate 	 * return to, swtch() gets called and we end up here.
771590Sesolom 	 *
772590Sesolom 	 * Note that we use atomic ops below (cas64 and atomic_add_64), which
773590Sesolom 	 * we don't use in the functions above, because we're not called
774590Sesolom 	 * with interrupts blocked, but the epilog/prolog functions are.
7750Sstevel@tonic-gate 	 */
7760Sstevel@tonic-gate 	if (t->t_intr_start) {
7770Sstevel@tonic-gate 		do {
7780Sstevel@tonic-gate 			start = t->t_intr_start;
7790Sstevel@tonic-gate 			interval = tsc_read() - start;
7800Sstevel@tonic-gate 		} while (cas64(&t->t_intr_start, start, 0) != start);
781590Sesolom 		cpu = CPU;
782916Sschwartz 		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
783590Sesolom 
784590Sesolom 		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
785590Sesolom 		    interval);
7860Sstevel@tonic-gate 	} else
7870Sstevel@tonic-gate 		ASSERT(t->t_intr == NULL);
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate /*
7910Sstevel@tonic-gate  * An interrupt thread is returning from swtch(). Place a starting timestamp
7920Sstevel@tonic-gate  * in its thread structure.
7930Sstevel@tonic-gate  */
7940Sstevel@tonic-gate void
7950Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	uint64_t ts;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
8000Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	do {
8030Sstevel@tonic-gate 		ts = t->t_intr_start;
8040Sstevel@tonic-gate 	} while (cas64(&t->t_intr_start, ts, tsc_read()) != ts);
8050Sstevel@tonic-gate }
8063446Smrj 
8073446Smrj /*
8083446Smrj  * Dispatch a hilevel interrupt (one above LOCK_LEVEL)
8093446Smrj  */
8103446Smrj /*ARGSUSED*/
8113446Smrj static void
8123446Smrj dispatch_hilevel(uint_t vector, uint_t arg2)
8133446Smrj {
8143446Smrj 	sti();
8153446Smrj 	av_dispatch_autovect(vector);
8163446Smrj 	cli();
8173446Smrj }
8183446Smrj 
8193446Smrj /*
8203446Smrj  * Dispatch a soft interrupt
8213446Smrj  */
8223446Smrj /*ARGSUSED*/
8233446Smrj static void
8243446Smrj dispatch_softint(uint_t oldpil, uint_t arg2)
8253446Smrj {
8263446Smrj 	struct cpu *cpu = CPU;
8273446Smrj 
8283446Smrj 	sti();
8293446Smrj 	av_dispatch_softvect((int)cpu->cpu_thread->t_pil);
8303446Smrj 	cli();
8313446Smrj 
8323446Smrj 	/*
8333446Smrj 	 * Must run softint_epilog() on the interrupt thread stack, since
8343446Smrj 	 * there may not be a return from it if the interrupt thread blocked.
8353446Smrj 	 */
8363446Smrj 	dosoftint_epilog(cpu, oldpil);
8373446Smrj }
8383446Smrj 
8393446Smrj /*
8403446Smrj  * Dispatch a normal interrupt
8413446Smrj  */
8423446Smrj static void
8433446Smrj dispatch_hardint(uint_t vector, uint_t oldipl)
8443446Smrj {
8453446Smrj 	struct cpu *cpu = CPU;
8463446Smrj 
8473446Smrj 	sti();
8483446Smrj 	av_dispatch_autovect(vector);
8493446Smrj 	cli();
8503446Smrj 
8513446Smrj 	/*
8523446Smrj 	 * Must run intr_thread_epilog() on the interrupt thread stack, since
8533446Smrj 	 * there may not be a return from it if the interrupt thread blocked.
8543446Smrj 	 */
8553446Smrj 	intr_thread_epilog(cpu, vector, oldipl);
8563446Smrj }
8573446Smrj 
8583446Smrj /*
8593446Smrj  * Deliver any softints the current interrupt priority allows.
8603446Smrj  * Called with interrupts disabled.
8613446Smrj  */
8623446Smrj void
8633446Smrj dosoftint(struct regs *regs)
8643446Smrj {
8653446Smrj 	struct cpu *cpu = CPU;
8663446Smrj 	int oldipl;
8673446Smrj 	caddr_t newsp;
8683446Smrj 
8693446Smrj 	while (cpu->cpu_softinfo.st_pending) {
8703446Smrj 		oldipl = cpu->cpu_pri;
8713446Smrj 		newsp = dosoftint_prolog(cpu, (caddr_t)regs,
8723446Smrj 			cpu->cpu_softinfo.st_pending, oldipl);
8733446Smrj 		/*
8743446Smrj 		 * If returned stack pointer is NULL, priority is too high
8753446Smrj 		 * to run any of the pending softints now.
8763446Smrj 		 * Break out and they will be run later.
8773446Smrj 		 */
8783446Smrj 		if (newsp == NULL)
8793446Smrj 			break;
8803446Smrj 		switch_sp_and_call(newsp, dispatch_softint, oldipl, 0);
8813446Smrj 	}
8823446Smrj }
8833446Smrj 
8843446Smrj /*
8853446Smrj  * Interrupt service routine, called with interrupts disabled.
8863446Smrj  */
8873446Smrj /*ARGSUSED*/
8883446Smrj void
8893446Smrj do_interrupt(struct regs *rp, trap_trace_rec_t *ttp)
8903446Smrj {
8913446Smrj 	struct cpu *cpu = CPU;
8923446Smrj 	int newipl, oldipl = cpu->cpu_pri;
8933446Smrj 	uint_t vector;
8943446Smrj 	caddr_t newsp;
8953446Smrj 
8963446Smrj #ifdef TRAPTRACE
8973446Smrj 	ttp->ttr_marker = TT_INTERRUPT;
8983446Smrj 	ttp->ttr_ipl = 0xff;
8993446Smrj 	ttp->ttr_pri = oldipl;
9003446Smrj 	ttp->ttr_spl = cpu->cpu_base_spl;
9013446Smrj 	ttp->ttr_vector = 0xff;
9023446Smrj #endif	/* TRAPTRACE */
9033446Smrj 
9043446Smrj 	/*
9054191Sjosephb 	 * Handle any pending TLB flushing
9064191Sjosephb 	 */
9074191Sjosephb 	tlb_service();
9084191Sjosephb 
9094191Sjosephb 	/*
9103446Smrj 	 * If it's a softint go do it now.
9113446Smrj 	 */
9123446Smrj 	if (rp->r_trapno == T_SOFTINT) {
9133446Smrj 		dosoftint(rp);
9143446Smrj 		ASSERT(!interrupts_enabled());
9153446Smrj 		return;
9163446Smrj 	}
9173446Smrj 
9183446Smrj 	/*
9193446Smrj 	 * Raise the interrupt priority.
9203446Smrj 	 */
9213446Smrj 	newipl = (*setlvl)(oldipl, (int *)&rp->r_trapno);
9223446Smrj #ifdef TRAPTRACE
9233446Smrj 	ttp->ttr_ipl = newipl;
9243446Smrj #endif	/* TRAPTRACE */
9253446Smrj 
9263446Smrj 	/*
9273446Smrj 	 * Bail if it is a spurious interrupt
9283446Smrj 	 */
9293446Smrj 	if (newipl == -1)
9303446Smrj 		return;
9313446Smrj 	cpu->cpu_pri = newipl;
9323446Smrj 	vector = rp->r_trapno;
9333446Smrj #ifdef TRAPTRACE
9343446Smrj 	ttp->ttr_vector = vector;
9353446Smrj #endif	/* TRAPTRACE */
9363446Smrj 	if (newipl > LOCK_LEVEL) {
9373446Smrj 		/*
9383446Smrj 		 * High priority interrupts run on this cpu's interrupt stack.
9393446Smrj 		 */
9403446Smrj 		if (hilevel_intr_prolog(cpu, newipl, oldipl, rp) == 0) {
9413446Smrj 			newsp = cpu->cpu_intr_stack;
9423446Smrj 			switch_sp_and_call(newsp, dispatch_hilevel, vector, 0);
9433446Smrj 		} else { /* already on the interrupt stack */
9443446Smrj 			dispatch_hilevel(vector, 0);
9453446Smrj 		}
9463446Smrj 		(void) hilevel_intr_epilog(cpu, newipl, oldipl, vector);
9473446Smrj 	} else {
9483446Smrj 		/*
9493446Smrj 		 * Run this interrupt in a separate thread.
9503446Smrj 		 */
9513446Smrj 		newsp = intr_thread_prolog(cpu, (caddr_t)rp, newipl);
9523446Smrj 		switch_sp_and_call(newsp, dispatch_hardint, vector, oldipl);
9533446Smrj 	}
9543446Smrj 
9553446Smrj 	/*
9563446Smrj 	 * Deliver any pending soft interrupts.
9573446Smrj 	 */
9583446Smrj 	if (cpu->cpu_softinfo.st_pending)
9593446Smrj 		dosoftint(rp);
9603446Smrj }
9613446Smrj 
9623446Smrj /*
9633446Smrj  * Common tasks always done by _sys_rtt, called with interrupts disabled.
9643446Smrj  * Returns 1 if returning to userland, 0 if returning to system mode.
9653446Smrj  */
9663446Smrj int
9673446Smrj sys_rtt_common(struct regs *rp)
9683446Smrj {
9693446Smrj 	kthread_t *tp;
9703446Smrj 	extern void mutex_exit_critical_start();
9713446Smrj 	extern long mutex_exit_critical_size;
9723446Smrj 
9733446Smrj loop:
9743446Smrj 
9753446Smrj 	/*
9763446Smrj 	 * Check if returning to user
9773446Smrj 	 */
9783446Smrj 	tp = CPU->cpu_thread;
9793446Smrj 	if (USERMODE(rp->r_cs)) {
9803446Smrj 		/*
9813446Smrj 		 * Check if AST pending.
9823446Smrj 		 */
9833446Smrj 		if (tp->t_astflag) {
9843446Smrj 			/*
9853446Smrj 			 * Let trap() handle the AST
9863446Smrj 			 */
9873446Smrj 			sti();
9883446Smrj 			rp->r_trapno = T_AST;
9893446Smrj 			trap(rp, (caddr_t)0, CPU->cpu_id);
9903446Smrj 			cli();
9913446Smrj 			goto loop;
9923446Smrj 		}
9933446Smrj 
9943446Smrj #if defined(__amd64)
9953446Smrj 		/*
9963446Smrj 		 * We are done if segment registers do not need updating.
9973446Smrj 		 */
998*4503Ssudheer 		if (tp->t_lwp->lwp_pcb.pcb_rupdate == 0)
9993446Smrj 			return (1);
10003446Smrj 
10013446Smrj 		if (update_sregs(rp, tp->t_lwp)) {
10023446Smrj 			/*
10033446Smrj 			 * 1 or more of the selectors is bad.
10043446Smrj 			 * Deliver a SIGSEGV.
10053446Smrj 			 */
10063446Smrj 			proc_t *p = ttoproc(tp);
10073446Smrj 
10083446Smrj 			sti();
10093446Smrj 			mutex_enter(&p->p_lock);
10103446Smrj 			tp->t_lwp->lwp_cursig = SIGSEGV;
10113446Smrj 			mutex_exit(&p->p_lock);
10123446Smrj 			psig();
10133446Smrj 			tp->t_sig_check = 1;
10143446Smrj 			cli();
10153446Smrj 		}
1016*4503Ssudheer 		tp->t_lwp->lwp_pcb.pcb_rupdate = 0;
10173446Smrj 
10183446Smrj #endif	/* __amd64 */
10193446Smrj 		return (1);
10203446Smrj 	}
10213446Smrj 
10223446Smrj 	/*
10233446Smrj 	 * Here if we are returning to supervisor mode.
10243446Smrj 	 * Check for a kernel preemption request.
10253446Smrj 	 */
10263446Smrj 	if (CPU->cpu_kprunrun && (rp->r_ps & PS_IE)) {
10273446Smrj 
10283446Smrj 		/*
10293446Smrj 		 * Do nothing if already in kpreempt
10303446Smrj 		 */
10313446Smrj 		if (!tp->t_preempt_lk) {
10323446Smrj 			tp->t_preempt_lk = 1;
10333446Smrj 			sti();
10343446Smrj 			kpreempt(1); /* asynchronous kpreempt call */
10353446Smrj 			cli();
10363446Smrj 			tp->t_preempt_lk = 0;
10373446Smrj 		}
10383446Smrj 	}
10393446Smrj 
10403446Smrj 	/*
10413446Smrj 	 * If we interrupted the mutex_exit() critical region we must
10423446Smrj 	 * reset the PC back to the beginning to prevent missed wakeups
10433446Smrj 	 * See the comments in mutex_exit() for details.
10443446Smrj 	 */
10453446Smrj 	if ((uintptr_t)rp->r_pc - (uintptr_t)mutex_exit_critical_start <
10463446Smrj 	    mutex_exit_critical_size) {
10473446Smrj 		rp->r_pc = (greg_t)mutex_exit_critical_start;
10483446Smrj 	}
10493446Smrj 	return (0);
10503446Smrj }
10513446Smrj 
10523446Smrj void
10533446Smrj send_dirint(int cpuid, int int_level)
10543446Smrj {
10553446Smrj 	(*send_dirintf)(cpuid, int_level);
10563446Smrj }
10573446Smrj 
10583446Smrj /*
10593446Smrj  * do_splx routine, takes new ipl to set
10603446Smrj  * returns the old ipl.
10613446Smrj  * We are careful not to set priority lower than CPU->cpu_base_pri,
10623446Smrj  * even though it seems we're raising the priority, it could be set
10633446Smrj  * higher at any time by an interrupt routine, so we must block interrupts
10643446Smrj  * and look at CPU->cpu_base_pri
10653446Smrj  */
10663446Smrj int
10673446Smrj do_splx(int newpri)
10683446Smrj {
10693446Smrj 	ulong_t	flag;
10703446Smrj 	cpu_t	*cpu;
10713446Smrj 	int	curpri, basepri;
10723446Smrj 
10733446Smrj 	flag = intr_clear();
10743446Smrj 	cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
10753446Smrj 	curpri = cpu->cpu_m.mcpu_pri;
10763446Smrj 	basepri = cpu->cpu_base_spl;
10773446Smrj 	if (newpri < basepri)
10783446Smrj 		newpri = basepri;
10793446Smrj 	cpu->cpu_m.mcpu_pri = newpri;
10803446Smrj 	(*setspl)(newpri);
10813446Smrj 	/*
10823446Smrj 	 * If we are going to reenable interrupts see if new priority level
10833446Smrj 	 * allows pending softint delivery.
10843446Smrj 	 */
10853446Smrj 	if ((flag & PS_IE) &&
10863446Smrj 	    bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > newpri)
10873446Smrj 		fakesoftint();
10883446Smrj 	ASSERT(!interrupts_enabled());
10893446Smrj 	intr_restore(flag);
10903446Smrj 	return (curpri);
10913446Smrj }
10923446Smrj 
10933446Smrj /*
10943446Smrj  * Common spl raise routine, takes new ipl to set
10953446Smrj  * returns the old ipl, will not lower ipl.
10963446Smrj  */
10973446Smrj int
10983446Smrj splr(int newpri)
10993446Smrj {
11003446Smrj 	ulong_t	flag;
11013446Smrj 	cpu_t	*cpu;
11023446Smrj 	int	curpri, basepri;
11033446Smrj 
11043446Smrj 	flag = intr_clear();
11053446Smrj 	cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
11063446Smrj 	curpri = cpu->cpu_m.mcpu_pri;
11073446Smrj 	/*
11083446Smrj 	 * Only do something if new priority is larger
11093446Smrj 	 */
11103446Smrj 	if (newpri > curpri) {
11113446Smrj 		basepri = cpu->cpu_base_spl;
11123446Smrj 		if (newpri < basepri)
11133446Smrj 			newpri = basepri;
11143446Smrj 		cpu->cpu_m.mcpu_pri = newpri;
11153446Smrj 		(*setspl)(newpri);
11163446Smrj 		/*
11173446Smrj 		 * See if new priority level allows pending softint delivery
11183446Smrj 		 */
11193446Smrj 		if ((flag & PS_IE) &&
11203446Smrj 		    bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > newpri)
11213446Smrj 			fakesoftint();
11223446Smrj 	}
11233446Smrj 	intr_restore(flag);
11243446Smrj 	return (curpri);
11253446Smrj }
11263446Smrj 
11273446Smrj int
11283446Smrj getpil(void)
11293446Smrj {
11303446Smrj 	return (CPU->cpu_m.mcpu_pri);
11313446Smrj }
11323446Smrj 
11333446Smrj int
11343446Smrj interrupts_enabled(void)
11353446Smrj {
11363446Smrj 	ulong_t	flag;
11373446Smrj 
11383446Smrj 	flag = getflags();
11393446Smrj 	return ((flag & PS_IE) == PS_IE);
11403446Smrj }
11413446Smrj 
11423446Smrj #ifdef DEBUG
11433446Smrj void
11443446Smrj assert_ints_enabled(void)
11453446Smrj {
11463446Smrj 	ASSERT(!interrupts_unleashed || interrupts_enabled());
11473446Smrj }
11483446Smrj #endif	/* DEBUG */
1149