xref: /onnv-gate/usr/src/uts/i86pc/os/intr.c (revision 3446:5903aece022d)
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 /*
22*3446Smrj  * 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>
51*3446Smrj #include <sys/archsystm.h>
52*3446Smrj #include <sys/machsystm.h>
53*3446Smrj #include <sys/ontrap.h>
54*3446Smrj #include <sys/x86_archext.h>
55*3446Smrj #include <sys/promif.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
59*3446Smrj  * Set cpu's base SPL level to the highest active interrupt level
600Sstevel@tonic-gate  */
61*3446Smrj void
62*3446Smrj set_base_spl(void)
630Sstevel@tonic-gate {
64*3446Smrj 	struct cpu *cpu = CPU;
65*3446Smrj 	uint16_t active = (uint16_t)cpu->cpu_intr_actv;
660Sstevel@tonic-gate 
67*3446Smrj 	cpu->cpu_base_spl = active == 0 ? 0 : bsrw_insn(active);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * Do all the work necessary to set up the cpu and thread structures
720Sstevel@tonic-gate  * to dispatch a high-level interrupt.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * Returns 0 if we're -not- already on the high-level interrupt stack,
750Sstevel@tonic-gate  * (and *must* switch to it), non-zero if we are already on that stack.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * Called with interrupts masked.
780Sstevel@tonic-gate  * The 'pil' is already set to the appropriate level for rp->r_trapno.
790Sstevel@tonic-gate  */
80*3446Smrj static int
810Sstevel@tonic-gate hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
840Sstevel@tonic-gate 	uint_t mask;
85590Sesolom 	hrtime_t intrtime;
86*3446Smrj 	hrtime_t now = tsc_read();
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	ASSERT(pil > LOCK_LEVEL);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	if (pil == CBE_HIGH_PIL) {
910Sstevel@tonic-gate 		cpu->cpu_profile_pil = oldpil;
920Sstevel@tonic-gate 		if (USERMODE(rp->r_cs)) {
930Sstevel@tonic-gate 			cpu->cpu_profile_pc = 0;
940Sstevel@tonic-gate 			cpu->cpu_profile_upc = rp->r_pc;
950Sstevel@tonic-gate 		} else {
960Sstevel@tonic-gate 			cpu->cpu_profile_pc = rp->r_pc;
970Sstevel@tonic-gate 			cpu->cpu_profile_upc = 0;
980Sstevel@tonic-gate 		}
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
1020Sstevel@tonic-gate 	if (mask != 0) {
1030Sstevel@tonic-gate 		int nestpil;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 		/*
1060Sstevel@tonic-gate 		 * We have interrupted another high-level interrupt.
1070Sstevel@tonic-gate 		 * Load starting timestamp, compute interval, update
1080Sstevel@tonic-gate 		 * cumulative counter.
1090Sstevel@tonic-gate 		 */
1100Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
1110Sstevel@tonic-gate 		ASSERT(nestpil < pil);
112*3446Smrj 		intrtime = now -
1130Sstevel@tonic-gate 		    mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)];
114916Sschwartz 		mcpu->intrstat[nestpil][0] += intrtime;
115590Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1160Sstevel@tonic-gate 		/*
1170Sstevel@tonic-gate 		 * Another high-level interrupt is active below this one, so
1180Sstevel@tonic-gate 		 * there is no need to check for an interrupt thread.  That
1190Sstevel@tonic-gate 		 * will be done by the lowest priority high-level interrupt
1200Sstevel@tonic-gate 		 * active.
1210Sstevel@tonic-gate 		 */
1220Sstevel@tonic-gate 	} else {
1230Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 		/*
1260Sstevel@tonic-gate 		 * See if we are interrupting a low-level interrupt thread.
1270Sstevel@tonic-gate 		 * If so, account for its time slice only if its time stamp
1280Sstevel@tonic-gate 		 * is non-zero.
1290Sstevel@tonic-gate 		 */
1300Sstevel@tonic-gate 		if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) {
131*3446Smrj 			intrtime = now - t->t_intr_start;
132916Sschwartz 			mcpu->intrstat[t->t_pil][0] += intrtime;
133590Sesolom 			cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1340Sstevel@tonic-gate 			t->t_intr_start = 0;
1350Sstevel@tonic-gate 		}
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	/*
1390Sstevel@tonic-gate 	 * Store starting timestamp in CPU structure for this PIL.
1400Sstevel@tonic-gate 	 */
141*3446Smrj 	mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = now;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (pil == 15) {
1460Sstevel@tonic-gate 		/*
1470Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
1480Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
1490Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
1500Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
1510Sstevel@tonic-gate 		 */
1520Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
1530Sstevel@tonic-gate 		(*refcntp)++;
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * Does most of the work of returning from a high level interrupt.
1650Sstevel@tonic-gate  *
1660Sstevel@tonic-gate  * Returns 0 if there are no more high level interrupts (in which
1670Sstevel@tonic-gate  * case we must switch back to the interrupted thread stack) or
1680Sstevel@tonic-gate  * non-zero if there are more (in which case we should stay on it).
1690Sstevel@tonic-gate  *
1700Sstevel@tonic-gate  * Called with interrupts masked
1710Sstevel@tonic-gate  */
172*3446Smrj static int
1730Sstevel@tonic-gate hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
1760Sstevel@tonic-gate 	uint_t mask;
177590Sesolom 	hrtime_t intrtime;
178*3446Smrj 	hrtime_t now = tsc_read();
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	ASSERT(mcpu->mcpu_pri == pil);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (pil == 15) {
1870Sstevel@tonic-gate 		/*
1880Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
1890Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
1900Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
1910Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
1920Sstevel@tonic-gate 		 */
1930Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		ASSERT(*refcntp > 0);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		if (--(*refcntp) == 0)
1980Sstevel@tonic-gate 			cpu->cpu_intr_actv &= ~(1 << pil);
1990Sstevel@tonic-gate 	} else {
2000Sstevel@tonic-gate 		cpu->cpu_intr_actv &= ~(1 << pil);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0);
2040Sstevel@tonic-gate 
205*3446Smrj 	intrtime = now - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)];
206916Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
207590Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/*
2100Sstevel@tonic-gate 	 * Check for lower-pil nested high-level interrupt beneath
2110Sstevel@tonic-gate 	 * current one.  If so, place a starting timestamp in its
2120Sstevel@tonic-gate 	 * pil_high_start entry.
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
2150Sstevel@tonic-gate 	if (mask != 0) {
2160Sstevel@tonic-gate 		int nestpil;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 		/*
2190Sstevel@tonic-gate 		 * find PIL of nested interrupt
2200Sstevel@tonic-gate 		 */
2210Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
2220Sstevel@tonic-gate 		ASSERT(nestpil < pil);
223*3446Smrj 		mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = now;
2240Sstevel@tonic-gate 		/*
2250Sstevel@tonic-gate 		 * (Another high-level interrupt is active below this one,
2260Sstevel@tonic-gate 		 * so there is no need to check for an interrupt
2270Sstevel@tonic-gate 		 * thread.  That will be done by the lowest priority
2280Sstevel@tonic-gate 		 * high-level interrupt active.)
2290Sstevel@tonic-gate 		 */
2300Sstevel@tonic-gate 	} else {
2310Sstevel@tonic-gate 		/*
2320Sstevel@tonic-gate 		 * Check to see if there is a low-level interrupt active.
2330Sstevel@tonic-gate 		 * If so, place a starting timestamp in the thread
2340Sstevel@tonic-gate 		 * structure.
2350Sstevel@tonic-gate 		 */
2360Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		if (t->t_flag & T_INTR_THREAD)
239*3446Smrj 			t->t_intr_start = now;
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	mcpu->mcpu_pri = oldpil;
2430Sstevel@tonic-gate 	(void) (*setlvlx)(oldpil, vecnum);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  * Set up the cpu, thread and interrupt thread structures for
2500Sstevel@tonic-gate  * executing an interrupt thread.  The new stack pointer of the
2510Sstevel@tonic-gate  * interrupt thread (which *must* be switched to) is returned.
2520Sstevel@tonic-gate  */
253*3446Smrj static caddr_t
2540Sstevel@tonic-gate intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
2570Sstevel@tonic-gate 	kthread_t *t, *volatile it;
258*3446Smrj 	hrtime_t now = tsc_read();
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	ASSERT(pil > 0);
2610Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
2620Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/*
2650Sstevel@tonic-gate 	 * Get set to run an interrupt thread.
2660Sstevel@tonic-gate 	 * There should always be an interrupt thread, since we
2670Sstevel@tonic-gate 	 * allocate one for each level on each CPU.
2680Sstevel@tonic-gate 	 *
269989Sesolom 	 * t_intr_start could be zero due to cpu_intr_swtch_enter.
2700Sstevel@tonic-gate 	 */
2710Sstevel@tonic-gate 	t = cpu->cpu_thread;
272989Sesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
273*3446Smrj 		hrtime_t intrtime = now - t->t_intr_start;
274916Sschwartz 		mcpu->intrstat[t->t_pil][0] += intrtime;
275590Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
2760Sstevel@tonic-gate 		t->t_intr_start = 0;
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;	/* mark stack in curthread for resume */
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	/*
2840Sstevel@tonic-gate 	 * unlink the interrupt thread off the cpu
285989Sesolom 	 *
286989Sesolom 	 * Note that the code in kcpc_overflow_intr -relies- on the
287989Sesolom 	 * ordering of events here - in particular that t->t_lwp of
288989Sesolom 	 * the interrupt thread is set to the pinned thread *before*
289989Sesolom 	 * curthread is changed.
2900Sstevel@tonic-gate 	 */
2910Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
2920Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
2930Sstevel@tonic-gate 	it->t_intr = t;
2940Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	/*
2970Sstevel@tonic-gate 	 * (threads on the interrupt thread free list could have state
2980Sstevel@tonic-gate 	 * preset to TS_ONPROC, but it helps in debugging if
2990Sstevel@tonic-gate 	 * they're TS_FREE.)
3000Sstevel@tonic-gate 	 */
3010Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	cpu->cpu_thread = it;		/* new curthread on this cpu */
3040Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
3050Sstevel@tonic-gate 	it->t_pri = intr_pri + (pri_t)pil;
306*3446Smrj 	it->t_intr_start = now;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	return (it->t_stk);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate #ifdef DEBUG
3130Sstevel@tonic-gate int intr_thread_cnt;
3140Sstevel@tonic-gate #endif
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate  * Called with interrupts disabled
3180Sstevel@tonic-gate  */
319*3446Smrj static void
3200Sstevel@tonic-gate intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
3230Sstevel@tonic-gate 	kthread_t *t;
3240Sstevel@tonic-gate 	kthread_t *it = cpu->cpu_thread;	/* curthread */
3250Sstevel@tonic-gate 	uint_t pil, basespl;
326590Sesolom 	hrtime_t intrtime;
327*3446Smrj 	hrtime_t now = tsc_read();
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	pil = it->t_pil;
3300Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	ASSERT(it->t_intr_start != 0);
333*3446Smrj 	intrtime = now - it->t_intr_start;
334916Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
335590Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
3380Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/*
3410Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
3420Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
3430Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
3440Sstevel@tonic-gate 	 */
3450Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
3460Sstevel@tonic-gate 		/*
3470Sstevel@tonic-gate 		 * The interrupted thread is no longer pinned underneath
3480Sstevel@tonic-gate 		 * the interrupt thread.  This means the interrupt must
3490Sstevel@tonic-gate 		 * have blocked, and the interrupted thread has been
3500Sstevel@tonic-gate 		 * unpinned, and has probably been running around the
3510Sstevel@tonic-gate 		 * system for a while.
3520Sstevel@tonic-gate 		 *
3530Sstevel@tonic-gate 		 * Since there is no longer a thread under this one, put
3540Sstevel@tonic-gate 		 * this interrupt thread back on the CPU's free list and
3550Sstevel@tonic-gate 		 * resume the idle thread which will dispatch the next
3560Sstevel@tonic-gate 		 * thread to run.
3570Sstevel@tonic-gate 		 */
3580Sstevel@tonic-gate #ifdef DEBUG
3590Sstevel@tonic-gate 		intr_thread_cnt++;
3600Sstevel@tonic-gate #endif
3610Sstevel@tonic-gate 		cpu->cpu_stats.sys.intrblk++;
3620Sstevel@tonic-gate 		/*
3630Sstevel@tonic-gate 		 * Set CPU's base SPL based on active interrupts bitmask
3640Sstevel@tonic-gate 		 */
3650Sstevel@tonic-gate 		set_base_spl();
3660Sstevel@tonic-gate 		basespl = cpu->cpu_base_spl;
3670Sstevel@tonic-gate 		mcpu->mcpu_pri = basespl;
3680Sstevel@tonic-gate 		(*setlvlx)(basespl, vec);
3690Sstevel@tonic-gate 		(void) splhigh();
370*3446Smrj 		sti();
3710Sstevel@tonic-gate 		it->t_state = TS_FREE;
3720Sstevel@tonic-gate 		/*
3730Sstevel@tonic-gate 		 * Return interrupt thread to pool
3740Sstevel@tonic-gate 		 */
3750Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
3760Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
3770Sstevel@tonic-gate 		swtch();
378*3446Smrj 		panic("intr_thread_epilog: swtch returned");
3790Sstevel@tonic-gate 		/*NOTREACHED*/
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	/*
3830Sstevel@tonic-gate 	 * Return interrupt thread to the pool
3840Sstevel@tonic-gate 	 */
3850Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
3860Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
3870Sstevel@tonic-gate 	it->t_state = TS_FREE;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
3900Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
3910Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
3920Sstevel@tonic-gate 	(*setlvlx)(pil, vec);
393*3446Smrj 	t->t_intr_start = now;
3940Sstevel@tonic-gate 	cpu->cpu_thread = t;
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
397916Sschwartz /*
398*3446Smrj  * intr_get_time() is a resource for interrupt handlers to determine how
399*3446Smrj  * much time has been spent handling the current interrupt. Such a function
400*3446Smrj  * is needed because higher level interrupts can arrive during the
401*3446Smrj  * processing of an interrupt.  intr_get_time() only returns time spent in the
402*3446Smrj  * current interrupt handler.
403*3446Smrj  *
404*3446Smrj  * The caller must be calling from an interrupt handler running at a pil
405*3446Smrj  * below or at lock level. Timings are not provided for high-level
406*3446Smrj  * interrupts.
407*3446Smrj  *
408*3446Smrj  * The first time intr_get_time() is called while handling an interrupt,
409*3446Smrj  * it returns the time since the interrupt handler was invoked. Subsequent
410*3446Smrj  * calls will return the time since the prior call to intr_get_time(). Time
411*3446Smrj  * is returned as ticks. Use tsc_scalehrtime() to convert ticks to nsec.
412*3446Smrj  *
413*3446Smrj  * Theory Of Intrstat[][]:
414*3446Smrj  *
415*3446Smrj  * uint64_t intrstat[pil][0..1] is an array indexed by pil level, with two
416*3446Smrj  * uint64_ts per pil.
417*3446Smrj  *
418*3446Smrj  * intrstat[pil][0] is a cumulative count of the number of ticks spent
419*3446Smrj  * handling all interrupts at the specified pil on this CPU. It is
420*3446Smrj  * exported via kstats to the user.
421*3446Smrj  *
422*3446Smrj  * intrstat[pil][1] is always a count of ticks less than or equal to the
423*3446Smrj  * value in [0]. The difference between [1] and [0] is the value returned
424*3446Smrj  * by a call to intr_get_time(). At the start of interrupt processing,
425*3446Smrj  * [0] and [1] will be equal (or nearly so). As the interrupt consumes
426*3446Smrj  * time, [0] will increase, but [1] will remain the same. A call to
427*3446Smrj  * intr_get_time() will return the difference, then update [1] to be the
428*3446Smrj  * same as [0]. Future calls will return the time since the last call.
429*3446Smrj  * Finally, when the interrupt completes, [1] is updated to the same as [0].
430*3446Smrj  *
431*3446Smrj  * Implementation:
432*3446Smrj  *
433*3446Smrj  * intr_get_time() works much like a higher level interrupt arriving. It
434*3446Smrj  * "checkpoints" the timing information by incrementing intrstat[pil][0]
435*3446Smrj  * to include elapsed running time, and by setting t_intr_start to rdtsc.
436*3446Smrj  * It then sets the return value to intrstat[pil][0] - intrstat[pil][1],
437*3446Smrj  * and updates intrstat[pil][1] to be the same as the new value of
438*3446Smrj  * intrstat[pil][0].
439*3446Smrj  *
440*3446Smrj  * In the normal handling of interrupts, after an interrupt handler returns
441*3446Smrj  * and the code in intr_thread() updates intrstat[pil][0], it then sets
442*3446Smrj  * intrstat[pil][1] to the new value of intrstat[pil][0]. When [0] == [1],
443*3446Smrj  * the timings are reset, i.e. intr_get_time() will return [0] - [1] which
444*3446Smrj  * is 0.
445*3446Smrj  *
446*3446Smrj  * Whenever interrupts arrive on a CPU which is handling a lower pil
447*3446Smrj  * interrupt, they update the lower pil's [0] to show time spent in the
448*3446Smrj  * handler that they've interrupted. This results in a growing discrepancy
449*3446Smrj  * between [0] and [1], which is returned the next time intr_get_time() is
450*3446Smrj  * called. Time spent in the higher-pil interrupt will not be returned in
451*3446Smrj  * the next intr_get_time() call from the original interrupt, because
452*3446Smrj  * the higher-pil interrupt's time is accumulated in intrstat[higherpil][].
453916Sschwartz  */
454916Sschwartz uint64_t
455*3446Smrj intr_get_time(void)
456916Sschwartz {
457*3446Smrj 	struct cpu *cpu;
458*3446Smrj 	struct machcpu *mcpu;
459*3446Smrj 	kthread_t *t;
460916Sschwartz 	uint64_t time, delta, ret;
461*3446Smrj 	uint_t pil;
462916Sschwartz 
463*3446Smrj 	cli();
464*3446Smrj 	cpu = CPU;
465*3446Smrj 	mcpu = &cpu->cpu_m;
466*3446Smrj 	t = cpu->cpu_thread;
467*3446Smrj 	pil = t->t_pil;
468916Sschwartz 	ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0);
469916Sschwartz 	ASSERT(t->t_flag & T_INTR_THREAD);
470916Sschwartz 	ASSERT(pil != 0);
471916Sschwartz 	ASSERT(t->t_intr_start != 0);
472916Sschwartz 
473916Sschwartz 	time = tsc_read();
474916Sschwartz 	delta = time - t->t_intr_start;
475916Sschwartz 	t->t_intr_start = time;
476916Sschwartz 
477916Sschwartz 	time = mcpu->intrstat[pil][0] + delta;
478916Sschwartz 	ret = time - mcpu->intrstat[pil][1];
479916Sschwartz 	mcpu->intrstat[pil][0] = time;
480916Sschwartz 	mcpu->intrstat[pil][1] = time;
4811887Sjhaslam 	cpu->cpu_intracct[cpu->cpu_mstate] += delta;
482916Sschwartz 
483*3446Smrj 	sti();
484916Sschwartz 	return (ret);
485916Sschwartz }
486916Sschwartz 
487*3446Smrj static caddr_t
4880Sstevel@tonic-gate dosoftint_prolog(
4890Sstevel@tonic-gate 	struct cpu *cpu,
4900Sstevel@tonic-gate 	caddr_t stackptr,
4910Sstevel@tonic-gate 	uint32_t st_pending,
4920Sstevel@tonic-gate 	uint_t oldpil)
4930Sstevel@tonic-gate {
4940Sstevel@tonic-gate 	kthread_t *t, *volatile it;
4950Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
4960Sstevel@tonic-gate 	uint_t pil;
497*3446Smrj 	hrtime_t now;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate top:
5000Sstevel@tonic-gate 	ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	pil = bsrw_insn((uint16_t)st_pending);
5030Sstevel@tonic-gate 	if (pil <= oldpil || pil <= cpu->cpu_base_spl)
5040Sstevel@tonic-gate 		return (0);
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	/*
5070Sstevel@tonic-gate 	 * XX64	Sigh.
5080Sstevel@tonic-gate 	 *
5090Sstevel@tonic-gate 	 * This is a transliteration of the i386 assembler code for
5100Sstevel@tonic-gate 	 * soft interrupts.  One question is "why does this need
5110Sstevel@tonic-gate 	 * to be atomic?"  One possible race is -other- processors
5120Sstevel@tonic-gate 	 * posting soft interrupts to us in set_pending() i.e. the
5130Sstevel@tonic-gate 	 * CPU might get preempted just after the address computation,
5140Sstevel@tonic-gate 	 * but just before the atomic transaction, so another CPU would
5150Sstevel@tonic-gate 	 * actually set the original CPU's st_pending bit.  However,
5160Sstevel@tonic-gate 	 * it looks like it would be simpler to disable preemption there.
5170Sstevel@tonic-gate 	 * Are there other races for which preemption control doesn't work?
5180Sstevel@tonic-gate 	 *
5190Sstevel@tonic-gate 	 * The i386 assembler version -also- checks to see if the bit
5200Sstevel@tonic-gate 	 * being cleared was actually set; if it wasn't, it rechecks
5210Sstevel@tonic-gate 	 * for more.  This seems a bit strange, as the only code that
5220Sstevel@tonic-gate 	 * ever clears the bit is -this- code running with interrupts
5230Sstevel@tonic-gate 	 * disabled on -this- CPU.  This code would probably be cheaper:
5240Sstevel@tonic-gate 	 *
5250Sstevel@tonic-gate 	 * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending,
5260Sstevel@tonic-gate 	 *   ~(1 << pil));
5270Sstevel@tonic-gate 	 *
5280Sstevel@tonic-gate 	 * and t->t_preempt--/++ around set_pending() even cheaper,
5290Sstevel@tonic-gate 	 * but at this point, correctness is critical, so we slavishly
5300Sstevel@tonic-gate 	 * emulate the i386 port.
5310Sstevel@tonic-gate 	 */
532*3446Smrj 	if (atomic_btr32((uint32_t *)
533*3446Smrj 	    &mcpu->mcpu_softinfo.st_pending, pil) == 0) {
5340Sstevel@tonic-gate 		st_pending = mcpu->mcpu_softinfo.st_pending;
5350Sstevel@tonic-gate 		goto top;
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
5390Sstevel@tonic-gate 	(*setspl)(pil);
5400Sstevel@tonic-gate 
541*3446Smrj 	now = tsc_read();
542*3446Smrj 
5430Sstevel@tonic-gate 	/*
5440Sstevel@tonic-gate 	 * Get set to run interrupt thread.
5450Sstevel@tonic-gate 	 * There should always be an interrupt thread since we
5460Sstevel@tonic-gate 	 * allocate one for each level on the CPU.
5470Sstevel@tonic-gate 	 */
5480Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
5490Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
5500Sstevel@tonic-gate 
551989Sesolom 	/* t_intr_start could be zero due to cpu_intr_swtch_enter. */
552989Sesolom 	t = cpu->cpu_thread;
553989Sesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
554*3446Smrj 		hrtime_t intrtime = now - t->t_intr_start;
555989Sesolom 		mcpu->intrstat[pil][0] += intrtime;
556989Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
557989Sesolom 		t->t_intr_start = 0;
558989Sesolom 	}
559989Sesolom 
5600Sstevel@tonic-gate 	/*
5610Sstevel@tonic-gate 	 * Note that the code in kcpc_overflow_intr -relies- on the
5620Sstevel@tonic-gate 	 * ordering of events here - in particular that t->t_lwp of
5630Sstevel@tonic-gate 	 * the interrupt thread is set to the pinned thread *before*
564989Sesolom 	 * curthread is changed.
5650Sstevel@tonic-gate 	 */
5660Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
5670Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/*
5700Sstevel@tonic-gate 	 * Push interrupted thread onto list from new thread.
5710Sstevel@tonic-gate 	 * Set the new thread as the current one.
5720Sstevel@tonic-gate 	 * Set interrupted thread's T_SP because if it is the idle thread,
5730Sstevel@tonic-gate 	 * resume() may use that stack between threads.
5740Sstevel@tonic-gate 	 */
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
5770Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	it->t_intr = t;
5800Sstevel@tonic-gate 	cpu->cpu_thread = it;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	/*
5830Sstevel@tonic-gate 	 * Set bit for this pil in CPU's interrupt active bitmask.
5840Sstevel@tonic-gate 	 */
5850Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
5860Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/*
5890Sstevel@tonic-gate 	 * Initialize thread priority level from intr_pri
5900Sstevel@tonic-gate 	 */
5910Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
5920Sstevel@tonic-gate 	it->t_pri = (pri_t)pil + intr_pri;
593*3446Smrj 	it->t_intr_start = now;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	return (it->t_stk);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
598*3446Smrj static void
5990Sstevel@tonic-gate dosoftint_epilog(struct cpu *cpu, uint_t oldpil)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
6020Sstevel@tonic-gate 	kthread_t *t, *it;
6030Sstevel@tonic-gate 	uint_t pil, basespl;
604590Sesolom 	hrtime_t intrtime;
605*3446Smrj 	hrtime_t now = tsc_read();
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	it = cpu->cpu_thread;
6080Sstevel@tonic-gate 	pil = it->t_pil;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
6130Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
614*3446Smrj 	intrtime = now - it->t_intr_start;
615916Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
616590Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	/*
6190Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
6200Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
6210Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
6220Sstevel@tonic-gate 	 */
6230Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
6240Sstevel@tonic-gate 		/*
6250Sstevel@tonic-gate 		 * Put thread back on the interrupt thread list.
6260Sstevel@tonic-gate 		 * This was an interrupt thread, so set CPU's base SPL.
6270Sstevel@tonic-gate 		 */
6280Sstevel@tonic-gate 		set_base_spl();
6290Sstevel@tonic-gate 		it->t_state = TS_FREE;
6300Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
6310Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
6320Sstevel@tonic-gate 		(void) splhigh();
633*3446Smrj 		sti();
6340Sstevel@tonic-gate 		swtch();
6350Sstevel@tonic-gate 		/*NOTREACHED*/
636*3446Smrj 		panic("dosoftint_epilog: swtch returned");
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
6390Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
6400Sstevel@tonic-gate 	it->t_state = TS_FREE;
6410Sstevel@tonic-gate 	cpu->cpu_thread = t;
6420Sstevel@tonic-gate 	if (t->t_flag & T_INTR_THREAD)
643*3446Smrj 		t->t_intr_start = now;
6440Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
6450Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
6460Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
6470Sstevel@tonic-gate 	(*setspl)(pil);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
650*3446Smrj 
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate  * Make the interrupted thread 'to' be runnable.
6530Sstevel@tonic-gate  *
6540Sstevel@tonic-gate  * Since t->t_sp has already been saved, t->t_pc is all
6550Sstevel@tonic-gate  * that needs to be set in this function.
6560Sstevel@tonic-gate  *
6570Sstevel@tonic-gate  * Returns the interrupt level of the interrupt thread.
6580Sstevel@tonic-gate  */
6590Sstevel@tonic-gate int
6600Sstevel@tonic-gate intr_passivate(
6610Sstevel@tonic-gate 	kthread_t *it,		/* interrupt thread */
6620Sstevel@tonic-gate 	kthread_t *t)		/* interrupted thread */
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate 	extern void _sys_rtt();
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	ASSERT(it->t_flag & T_INTR_THREAD);
6670Sstevel@tonic-gate 	ASSERT(SA(t->t_sp) == t->t_sp);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	t->t_pc = (uintptr_t)_sys_rtt;
6700Sstevel@tonic-gate 	return (it->t_pil);
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate /*
6740Sstevel@tonic-gate  * Create interrupt kstats for this CPU.
6750Sstevel@tonic-gate  */
6760Sstevel@tonic-gate void
6770Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	int		i;
6800Sstevel@tonic-gate 	kstat_t		*intr_ksp;
6810Sstevel@tonic-gate 	kstat_named_t	*knp;
6820Sstevel@tonic-gate 	char		name[KSTAT_STRLEN];
6830Sstevel@tonic-gate 	zoneid_t	zoneid;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	if (pool_pset_enabled())
6880Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
6890Sstevel@tonic-gate 	else
6900Sstevel@tonic-gate 		zoneid = ALL_ZONES;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
6930Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/*
6960Sstevel@tonic-gate 	 * Initialize each PIL's named kstat
6970Sstevel@tonic-gate 	 */
6980Sstevel@tonic-gate 	if (intr_ksp != NULL) {
6990Sstevel@tonic-gate 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
7000Sstevel@tonic-gate 		knp = (kstat_named_t *)intr_ksp->ks_data;
7010Sstevel@tonic-gate 		intr_ksp->ks_private = cp;
7020Sstevel@tonic-gate 		for (i = 0; i < PIL_MAX; i++) {
7030Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
7040Sstevel@tonic-gate 			    i + 1);
7050Sstevel@tonic-gate 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
7060Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
7070Sstevel@tonic-gate 			    i + 1);
7080Sstevel@tonic-gate 			kstat_named_init(&knp[(i * 2) + 1], name,
7090Sstevel@tonic-gate 			    KSTAT_DATA_UINT64);
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate 		kstat_install(intr_ksp);
7120Sstevel@tonic-gate 	}
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate /*
7160Sstevel@tonic-gate  * Delete interrupt kstats for this CPU.
7170Sstevel@tonic-gate  */
7180Sstevel@tonic-gate void
7190Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp)
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate  * Convert interrupt statistics from CPU ticks to nanoseconds and
7260Sstevel@tonic-gate  * update kstat.
7270Sstevel@tonic-gate  */
7280Sstevel@tonic-gate int
7290Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate 	kstat_named_t	*knp = ksp->ks_data;
7320Sstevel@tonic-gate 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
7330Sstevel@tonic-gate 	int		i;
7340Sstevel@tonic-gate 	hrtime_t	hrt;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7370Sstevel@tonic-gate 		return (EACCES);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
740916Sschwartz 		hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0];
7410Sstevel@tonic-gate 		tsc_scalehrtime(&hrt);
7420Sstevel@tonic-gate 		knp[i * 2].value.ui64 = (uint64_t)hrt;
7430Sstevel@tonic-gate 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	return (0);
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate /*
7500Sstevel@tonic-gate  * An interrupt thread is ending a time slice, so compute the interval it
7510Sstevel@tonic-gate  * ran for and update the statistic for its PIL.
7520Sstevel@tonic-gate  */
7530Sstevel@tonic-gate void
7540Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	uint64_t	interval;
7570Sstevel@tonic-gate 	uint64_t	start;
758590Sesolom 	cpu_t		*cpu;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
7610Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	/*
7640Sstevel@tonic-gate 	 * We could be here with a zero timestamp. This could happen if:
7650Sstevel@tonic-gate 	 * an interrupt thread which no longer has a pinned thread underneath
7660Sstevel@tonic-gate 	 * it (i.e. it blocked at some point in its past) has finished running
7670Sstevel@tonic-gate 	 * its handler. intr_thread() updated the interrupt statistic for its
7680Sstevel@tonic-gate 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
7690Sstevel@tonic-gate 	 * return to, swtch() gets called and we end up here.
770590Sesolom 	 *
771590Sesolom 	 * Note that we use atomic ops below (cas64 and atomic_add_64), which
772590Sesolom 	 * we don't use in the functions above, because we're not called
773590Sesolom 	 * with interrupts blocked, but the epilog/prolog functions are.
7740Sstevel@tonic-gate 	 */
7750Sstevel@tonic-gate 	if (t->t_intr_start) {
7760Sstevel@tonic-gate 		do {
7770Sstevel@tonic-gate 			start = t->t_intr_start;
7780Sstevel@tonic-gate 			interval = tsc_read() - start;
7790Sstevel@tonic-gate 		} while (cas64(&t->t_intr_start, start, 0) != start);
780590Sesolom 		cpu = CPU;
781916Sschwartz 		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
782590Sesolom 
783590Sesolom 		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
784590Sesolom 		    interval);
7850Sstevel@tonic-gate 	} else
7860Sstevel@tonic-gate 		ASSERT(t->t_intr == NULL);
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate /*
7900Sstevel@tonic-gate  * An interrupt thread is returning from swtch(). Place a starting timestamp
7910Sstevel@tonic-gate  * in its thread structure.
7920Sstevel@tonic-gate  */
7930Sstevel@tonic-gate void
7940Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	uint64_t ts;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
7990Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	do {
8020Sstevel@tonic-gate 		ts = t->t_intr_start;
8030Sstevel@tonic-gate 	} while (cas64(&t->t_intr_start, ts, tsc_read()) != ts);
8040Sstevel@tonic-gate }
805*3446Smrj 
806*3446Smrj /*
807*3446Smrj  * Dispatch a hilevel interrupt (one above LOCK_LEVEL)
808*3446Smrj  */
809*3446Smrj /*ARGSUSED*/
810*3446Smrj static void
811*3446Smrj dispatch_hilevel(uint_t vector, uint_t arg2)
812*3446Smrj {
813*3446Smrj 	sti();
814*3446Smrj 	av_dispatch_autovect(vector);
815*3446Smrj 	cli();
816*3446Smrj }
817*3446Smrj 
818*3446Smrj /*
819*3446Smrj  * Dispatch a soft interrupt
820*3446Smrj  */
821*3446Smrj /*ARGSUSED*/
822*3446Smrj static void
823*3446Smrj dispatch_softint(uint_t oldpil, uint_t arg2)
824*3446Smrj {
825*3446Smrj 	struct cpu *cpu = CPU;
826*3446Smrj 
827*3446Smrj 	sti();
828*3446Smrj 	av_dispatch_softvect((int)cpu->cpu_thread->t_pil);
829*3446Smrj 	cli();
830*3446Smrj 
831*3446Smrj 	/*
832*3446Smrj 	 * Must run softint_epilog() on the interrupt thread stack, since
833*3446Smrj 	 * there may not be a return from it if the interrupt thread blocked.
834*3446Smrj 	 */
835*3446Smrj 	dosoftint_epilog(cpu, oldpil);
836*3446Smrj }
837*3446Smrj 
838*3446Smrj /*
839*3446Smrj  * Dispatch a normal interrupt
840*3446Smrj  */
841*3446Smrj static void
842*3446Smrj dispatch_hardint(uint_t vector, uint_t oldipl)
843*3446Smrj {
844*3446Smrj 	struct cpu *cpu = CPU;
845*3446Smrj 
846*3446Smrj 	sti();
847*3446Smrj 	av_dispatch_autovect(vector);
848*3446Smrj 	cli();
849*3446Smrj 
850*3446Smrj 	/*
851*3446Smrj 	 * Must run intr_thread_epilog() on the interrupt thread stack, since
852*3446Smrj 	 * there may not be a return from it if the interrupt thread blocked.
853*3446Smrj 	 */
854*3446Smrj 	intr_thread_epilog(cpu, vector, oldipl);
855*3446Smrj }
856*3446Smrj 
857*3446Smrj /*
858*3446Smrj  * Deliver any softints the current interrupt priority allows.
859*3446Smrj  * Called with interrupts disabled.
860*3446Smrj  */
861*3446Smrj void
862*3446Smrj dosoftint(struct regs *regs)
863*3446Smrj {
864*3446Smrj 	struct cpu *cpu = CPU;
865*3446Smrj 	int oldipl;
866*3446Smrj 	caddr_t newsp;
867*3446Smrj 
868*3446Smrj 	while (cpu->cpu_softinfo.st_pending) {
869*3446Smrj 		oldipl = cpu->cpu_pri;
870*3446Smrj 		newsp = dosoftint_prolog(cpu, (caddr_t)regs,
871*3446Smrj 			cpu->cpu_softinfo.st_pending, oldipl);
872*3446Smrj 		/*
873*3446Smrj 		 * If returned stack pointer is NULL, priority is too high
874*3446Smrj 		 * to run any of the pending softints now.
875*3446Smrj 		 * Break out and they will be run later.
876*3446Smrj 		 */
877*3446Smrj 		if (newsp == NULL)
878*3446Smrj 			break;
879*3446Smrj 		switch_sp_and_call(newsp, dispatch_softint, oldipl, 0);
880*3446Smrj 	}
881*3446Smrj }
882*3446Smrj 
883*3446Smrj /*
884*3446Smrj  * Interrupt service routine, called with interrupts disabled.
885*3446Smrj  */
886*3446Smrj /*ARGSUSED*/
887*3446Smrj void
888*3446Smrj do_interrupt(struct regs *rp, trap_trace_rec_t *ttp)
889*3446Smrj {
890*3446Smrj 	struct cpu *cpu = CPU;
891*3446Smrj 	int newipl, oldipl = cpu->cpu_pri;
892*3446Smrj 	uint_t vector;
893*3446Smrj 	caddr_t newsp;
894*3446Smrj 
895*3446Smrj #ifdef TRAPTRACE
896*3446Smrj 	ttp->ttr_marker = TT_INTERRUPT;
897*3446Smrj 	ttp->ttr_ipl = 0xff;
898*3446Smrj 	ttp->ttr_pri = oldipl;
899*3446Smrj 	ttp->ttr_spl = cpu->cpu_base_spl;
900*3446Smrj 	ttp->ttr_vector = 0xff;
901*3446Smrj #endif	/* TRAPTRACE */
902*3446Smrj 
903*3446Smrj 	/*
904*3446Smrj 	 * If it's a softint go do it now.
905*3446Smrj 	 */
906*3446Smrj 	if (rp->r_trapno == T_SOFTINT) {
907*3446Smrj 		dosoftint(rp);
908*3446Smrj 		ASSERT(!interrupts_enabled());
909*3446Smrj 		return;
910*3446Smrj 	}
911*3446Smrj 
912*3446Smrj 	/*
913*3446Smrj 	 * Raise the interrupt priority.
914*3446Smrj 	 */
915*3446Smrj 	newipl = (*setlvl)(oldipl, (int *)&rp->r_trapno);
916*3446Smrj #ifdef TRAPTRACE
917*3446Smrj 	ttp->ttr_ipl = newipl;
918*3446Smrj #endif	/* TRAPTRACE */
919*3446Smrj 
920*3446Smrj 	/*
921*3446Smrj 	 * Bail if it is a spurious interrupt
922*3446Smrj 	 */
923*3446Smrj 	if (newipl == -1)
924*3446Smrj 		return;
925*3446Smrj 	cpu->cpu_pri = newipl;
926*3446Smrj 	vector = rp->r_trapno;
927*3446Smrj #ifdef TRAPTRACE
928*3446Smrj 	ttp->ttr_vector = vector;
929*3446Smrj #endif	/* TRAPTRACE */
930*3446Smrj 	if (newipl > LOCK_LEVEL) {
931*3446Smrj 		/*
932*3446Smrj 		 * High priority interrupts run on this cpu's interrupt stack.
933*3446Smrj 		 */
934*3446Smrj 		if (hilevel_intr_prolog(cpu, newipl, oldipl, rp) == 0) {
935*3446Smrj 			newsp = cpu->cpu_intr_stack;
936*3446Smrj 			switch_sp_and_call(newsp, dispatch_hilevel, vector, 0);
937*3446Smrj 		} else { /* already on the interrupt stack */
938*3446Smrj 			dispatch_hilevel(vector, 0);
939*3446Smrj 		}
940*3446Smrj 		(void) hilevel_intr_epilog(cpu, newipl, oldipl, vector);
941*3446Smrj 	} else {
942*3446Smrj 		/*
943*3446Smrj 		 * Run this interrupt in a separate thread.
944*3446Smrj 		 */
945*3446Smrj 		newsp = intr_thread_prolog(cpu, (caddr_t)rp, newipl);
946*3446Smrj 		switch_sp_and_call(newsp, dispatch_hardint, vector, oldipl);
947*3446Smrj 	}
948*3446Smrj 
949*3446Smrj 	/*
950*3446Smrj 	 * Deliver any pending soft interrupts.
951*3446Smrj 	 */
952*3446Smrj 	if (cpu->cpu_softinfo.st_pending)
953*3446Smrj 		dosoftint(rp);
954*3446Smrj }
955*3446Smrj 
956*3446Smrj /*
957*3446Smrj  * Common tasks always done by _sys_rtt, called with interrupts disabled.
958*3446Smrj  * Returns 1 if returning to userland, 0 if returning to system mode.
959*3446Smrj  */
960*3446Smrj int
961*3446Smrj sys_rtt_common(struct regs *rp)
962*3446Smrj {
963*3446Smrj 	kthread_t *tp;
964*3446Smrj 	extern void mutex_exit_critical_start();
965*3446Smrj 	extern long mutex_exit_critical_size;
966*3446Smrj 
967*3446Smrj loop:
968*3446Smrj 
969*3446Smrj 	/*
970*3446Smrj 	 * Check if returning to user
971*3446Smrj 	 */
972*3446Smrj 	tp = CPU->cpu_thread;
973*3446Smrj 	if (USERMODE(rp->r_cs)) {
974*3446Smrj 		/*
975*3446Smrj 		 * Check if AST pending.
976*3446Smrj 		 */
977*3446Smrj 		if (tp->t_astflag) {
978*3446Smrj 			/*
979*3446Smrj 			 * Let trap() handle the AST
980*3446Smrj 			 */
981*3446Smrj 			sti();
982*3446Smrj 			rp->r_trapno = T_AST;
983*3446Smrj 			trap(rp, (caddr_t)0, CPU->cpu_id);
984*3446Smrj 			cli();
985*3446Smrj 			goto loop;
986*3446Smrj 		}
987*3446Smrj 
988*3446Smrj #if defined(__amd64)
989*3446Smrj 		/*
990*3446Smrj 		 * We are done if segment registers do not need updating.
991*3446Smrj 		 */
992*3446Smrj 		if ((tp->t_lwp->lwp_pcb.pcb_flags & RUPDATE_PENDING) == 0)
993*3446Smrj 			return (1);
994*3446Smrj 
995*3446Smrj 		if (update_sregs(rp, tp->t_lwp)) {
996*3446Smrj 			/*
997*3446Smrj 			 * 1 or more of the selectors is bad.
998*3446Smrj 			 * Deliver a SIGSEGV.
999*3446Smrj 			 */
1000*3446Smrj 			proc_t *p = ttoproc(tp);
1001*3446Smrj 
1002*3446Smrj 			sti();
1003*3446Smrj 			mutex_enter(&p->p_lock);
1004*3446Smrj 			tp->t_lwp->lwp_cursig = SIGSEGV;
1005*3446Smrj 			mutex_exit(&p->p_lock);
1006*3446Smrj 			psig();
1007*3446Smrj 			tp->t_sig_check = 1;
1008*3446Smrj 			cli();
1009*3446Smrj 		}
1010*3446Smrj 		tp->t_lwp->lwp_pcb.pcb_flags &= ~RUPDATE_PENDING;
1011*3446Smrj 
1012*3446Smrj #endif	/* __amd64 */
1013*3446Smrj 		return (1);
1014*3446Smrj 	}
1015*3446Smrj 
1016*3446Smrj 	/*
1017*3446Smrj 	 * Here if we are returning to supervisor mode.
1018*3446Smrj 	 * Check for a kernel preemption request.
1019*3446Smrj 	 */
1020*3446Smrj 	if (CPU->cpu_kprunrun && (rp->r_ps & PS_IE)) {
1021*3446Smrj 
1022*3446Smrj 		/*
1023*3446Smrj 		 * Do nothing if already in kpreempt
1024*3446Smrj 		 */
1025*3446Smrj 		if (!tp->t_preempt_lk) {
1026*3446Smrj 			tp->t_preempt_lk = 1;
1027*3446Smrj 			sti();
1028*3446Smrj 			kpreempt(1); /* asynchronous kpreempt call */
1029*3446Smrj 			cli();
1030*3446Smrj 			tp->t_preempt_lk = 0;
1031*3446Smrj 		}
1032*3446Smrj 	}
1033*3446Smrj 
1034*3446Smrj 	/*
1035*3446Smrj 	 * If we interrupted the mutex_exit() critical region we must
1036*3446Smrj 	 * reset the PC back to the beginning to prevent missed wakeups
1037*3446Smrj 	 * See the comments in mutex_exit() for details.
1038*3446Smrj 	 */
1039*3446Smrj 	if ((uintptr_t)rp->r_pc - (uintptr_t)mutex_exit_critical_start <
1040*3446Smrj 	    mutex_exit_critical_size) {
1041*3446Smrj 		rp->r_pc = (greg_t)mutex_exit_critical_start;
1042*3446Smrj 	}
1043*3446Smrj 	return (0);
1044*3446Smrj }
1045*3446Smrj 
1046*3446Smrj void
1047*3446Smrj send_dirint(int cpuid, int int_level)
1048*3446Smrj {
1049*3446Smrj 	(*send_dirintf)(cpuid, int_level);
1050*3446Smrj }
1051*3446Smrj 
1052*3446Smrj /*
1053*3446Smrj  * do_splx routine, takes new ipl to set
1054*3446Smrj  * returns the old ipl.
1055*3446Smrj  * We are careful not to set priority lower than CPU->cpu_base_pri,
1056*3446Smrj  * even though it seems we're raising the priority, it could be set
1057*3446Smrj  * higher at any time by an interrupt routine, so we must block interrupts
1058*3446Smrj  * and look at CPU->cpu_base_pri
1059*3446Smrj  */
1060*3446Smrj int
1061*3446Smrj do_splx(int newpri)
1062*3446Smrj {
1063*3446Smrj 	ulong_t	flag;
1064*3446Smrj 	cpu_t	*cpu;
1065*3446Smrj 	int	curpri, basepri;
1066*3446Smrj 
1067*3446Smrj 	flag = intr_clear();
1068*3446Smrj 	cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
1069*3446Smrj 	curpri = cpu->cpu_m.mcpu_pri;
1070*3446Smrj 	basepri = cpu->cpu_base_spl;
1071*3446Smrj 	if (newpri < basepri)
1072*3446Smrj 		newpri = basepri;
1073*3446Smrj 	cpu->cpu_m.mcpu_pri = newpri;
1074*3446Smrj 	(*setspl)(newpri);
1075*3446Smrj 	/*
1076*3446Smrj 	 * If we are going to reenable interrupts see if new priority level
1077*3446Smrj 	 * allows pending softint delivery.
1078*3446Smrj 	 */
1079*3446Smrj 	if ((flag & PS_IE) &&
1080*3446Smrj 	    bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > newpri)
1081*3446Smrj 		fakesoftint();
1082*3446Smrj 	ASSERT(!interrupts_enabled());
1083*3446Smrj 	intr_restore(flag);
1084*3446Smrj 	return (curpri);
1085*3446Smrj }
1086*3446Smrj 
1087*3446Smrj /*
1088*3446Smrj  * Common spl raise routine, takes new ipl to set
1089*3446Smrj  * returns the old ipl, will not lower ipl.
1090*3446Smrj  */
1091*3446Smrj int
1092*3446Smrj splr(int newpri)
1093*3446Smrj {
1094*3446Smrj 	ulong_t	flag;
1095*3446Smrj 	cpu_t	*cpu;
1096*3446Smrj 	int	curpri, basepri;
1097*3446Smrj 
1098*3446Smrj 	flag = intr_clear();
1099*3446Smrj 	cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
1100*3446Smrj 	curpri = cpu->cpu_m.mcpu_pri;
1101*3446Smrj 	/*
1102*3446Smrj 	 * Only do something if new priority is larger
1103*3446Smrj 	 */
1104*3446Smrj 	if (newpri > curpri) {
1105*3446Smrj 		basepri = cpu->cpu_base_spl;
1106*3446Smrj 		if (newpri < basepri)
1107*3446Smrj 			newpri = basepri;
1108*3446Smrj 		cpu->cpu_m.mcpu_pri = newpri;
1109*3446Smrj 		(*setspl)(newpri);
1110*3446Smrj 		/*
1111*3446Smrj 		 * See if new priority level allows pending softint delivery
1112*3446Smrj 		 */
1113*3446Smrj 		if ((flag & PS_IE) &&
1114*3446Smrj 		    bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > newpri)
1115*3446Smrj 			fakesoftint();
1116*3446Smrj 	}
1117*3446Smrj 	intr_restore(flag);
1118*3446Smrj 	return (curpri);
1119*3446Smrj }
1120*3446Smrj 
1121*3446Smrj int
1122*3446Smrj getpil(void)
1123*3446Smrj {
1124*3446Smrj 	return (CPU->cpu_m.mcpu_pri);
1125*3446Smrj }
1126*3446Smrj 
1127*3446Smrj int
1128*3446Smrj interrupts_enabled(void)
1129*3446Smrj {
1130*3446Smrj 	ulong_t	flag;
1131*3446Smrj 
1132*3446Smrj 	flag = getflags();
1133*3446Smrj 	return ((flag & PS_IE) == PS_IE);
1134*3446Smrj }
1135*3446Smrj 
1136*3446Smrj #ifdef DEBUG
1137*3446Smrj void
1138*3446Smrj assert_ints_enabled(void)
1139*3446Smrj {
1140*3446Smrj 	ASSERT(!interrupts_unleashed || interrupts_enabled());
1141*3446Smrj }
1142*3446Smrj #endif	/* DEBUG */
1143