xref: /titanic_44/usr/src/uts/i86pc/os/intr.c (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5100b72f4Sandrei  * Common Development and Distribution License (the "License").
6100b72f4Sandrei  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
297c478bd9Sstevel@tonic-gate #include <sys/regset.h>
307c478bd9Sstevel@tonic-gate #include <sys/psw.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/thread.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/segments.h>
357c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
367c478bd9Sstevel@tonic-gate #include <sys/trap.h>
377c478bd9Sstevel@tonic-gate #include <sys/ftrace.h>
387c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
397c478bd9Sstevel@tonic-gate #include <sys/clock.h>
407c478bd9Sstevel@tonic-gate #include <sys/panic.h>
417c478bd9Sstevel@tonic-gate #include <sys/disp.h>
427c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
437c478bd9Sstevel@tonic-gate #include <sys/stack.h>
447c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
457c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
467c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
477c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
487c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
497c478bd9Sstevel@tonic-gate #include <sys/zone.h>
507c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
51*ae115bc7Smrj #include <sys/archsystm.h>
52*ae115bc7Smrj #include <sys/machsystm.h>
53*ae115bc7Smrj #include <sys/ontrap.h>
54*ae115bc7Smrj #include <sys/x86_archext.h>
55*ae115bc7Smrj #include <sys/promif.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
59*ae115bc7Smrj  * Set cpu's base SPL level to the highest active interrupt level
607c478bd9Sstevel@tonic-gate  */
61*ae115bc7Smrj void
62*ae115bc7Smrj set_base_spl(void)
637c478bd9Sstevel@tonic-gate {
64*ae115bc7Smrj 	struct cpu *cpu = CPU;
65*ae115bc7Smrj 	uint16_t active = (uint16_t)cpu->cpu_intr_actv;
667c478bd9Sstevel@tonic-gate 
67*ae115bc7Smrj 	cpu->cpu_base_spl = active == 0 ? 0 : bsrw_insn(active);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * Do all the work necessary to set up the cpu and thread structures
727c478bd9Sstevel@tonic-gate  * to dispatch a high-level interrupt.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * Returns 0 if we're -not- already on the high-level interrupt stack,
757c478bd9Sstevel@tonic-gate  * (and *must* switch to it), non-zero if we are already on that stack.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * Called with interrupts masked.
787c478bd9Sstevel@tonic-gate  * The 'pil' is already set to the appropriate level for rp->r_trapno.
797c478bd9Sstevel@tonic-gate  */
80*ae115bc7Smrj static int
817c478bd9Sstevel@tonic-gate hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
847c478bd9Sstevel@tonic-gate 	uint_t mask;
85eda89462Sesolom 	hrtime_t intrtime;
86*ae115bc7Smrj 	hrtime_t now = tsc_read();
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	ASSERT(pil > LOCK_LEVEL);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (pil == CBE_HIGH_PIL) {
917c478bd9Sstevel@tonic-gate 		cpu->cpu_profile_pil = oldpil;
927c478bd9Sstevel@tonic-gate 		if (USERMODE(rp->r_cs)) {
937c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_pc = 0;
947c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_upc = rp->r_pc;
957c478bd9Sstevel@tonic-gate 		} else {
967c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_pc = rp->r_pc;
977c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_upc = 0;
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
1027c478bd9Sstevel@tonic-gate 	if (mask != 0) {
1037c478bd9Sstevel@tonic-gate 		int nestpil;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		/*
1067c478bd9Sstevel@tonic-gate 		 * We have interrupted another high-level interrupt.
1077c478bd9Sstevel@tonic-gate 		 * Load starting timestamp, compute interval, update
1087c478bd9Sstevel@tonic-gate 		 * cumulative counter.
1097c478bd9Sstevel@tonic-gate 		 */
1107c478bd9Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
1117c478bd9Sstevel@tonic-gate 		ASSERT(nestpil < pil);
112*ae115bc7Smrj 		intrtime = now -
1137c478bd9Sstevel@tonic-gate 		    mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)];
1147a364d25Sschwartz 		mcpu->intrstat[nestpil][0] += intrtime;
115eda89462Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1167c478bd9Sstevel@tonic-gate 		/*
1177c478bd9Sstevel@tonic-gate 		 * Another high-level interrupt is active below this one, so
1187c478bd9Sstevel@tonic-gate 		 * there is no need to check for an interrupt thread.  That
1197c478bd9Sstevel@tonic-gate 		 * will be done by the lowest priority high-level interrupt
1207c478bd9Sstevel@tonic-gate 		 * active.
1217c478bd9Sstevel@tonic-gate 		 */
1227c478bd9Sstevel@tonic-gate 	} else {
1237c478bd9Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		/*
1267c478bd9Sstevel@tonic-gate 		 * See if we are interrupting a low-level interrupt thread.
1277c478bd9Sstevel@tonic-gate 		 * If so, account for its time slice only if its time stamp
1287c478bd9Sstevel@tonic-gate 		 * is non-zero.
1297c478bd9Sstevel@tonic-gate 		 */
1307c478bd9Sstevel@tonic-gate 		if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) {
131*ae115bc7Smrj 			intrtime = now - t->t_intr_start;
1327a364d25Sschwartz 			mcpu->intrstat[t->t_pil][0] += intrtime;
133eda89462Sesolom 			cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1347c478bd9Sstevel@tonic-gate 			t->t_intr_start = 0;
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/*
1397c478bd9Sstevel@tonic-gate 	 * Store starting timestamp in CPU structure for this PIL.
1407c478bd9Sstevel@tonic-gate 	 */
141*ae115bc7Smrj 	mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = now;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if (pil == 15) {
1467c478bd9Sstevel@tonic-gate 		/*
1477c478bd9Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
1487c478bd9Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
1497c478bd9Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
1507c478bd9Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
1517c478bd9Sstevel@tonic-gate 		 */
1527c478bd9Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
1537c478bd9Sstevel@tonic-gate 		(*refcntp)++;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * Does most of the work of returning from a high level interrupt.
1657c478bd9Sstevel@tonic-gate  *
1667c478bd9Sstevel@tonic-gate  * Returns 0 if there are no more high level interrupts (in which
1677c478bd9Sstevel@tonic-gate  * case we must switch back to the interrupted thread stack) or
1687c478bd9Sstevel@tonic-gate  * non-zero if there are more (in which case we should stay on it).
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  * Called with interrupts masked
1717c478bd9Sstevel@tonic-gate  */
172*ae115bc7Smrj static int
1737c478bd9Sstevel@tonic-gate hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
1767c478bd9Sstevel@tonic-gate 	uint_t mask;
177eda89462Sesolom 	hrtime_t intrtime;
178*ae115bc7Smrj 	hrtime_t now = tsc_read();
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	ASSERT(mcpu->mcpu_pri == pil);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (pil == 15) {
1877c478bd9Sstevel@tonic-gate 		/*
1887c478bd9Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
1897c478bd9Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
1907c478bd9Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
1917c478bd9Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
1927c478bd9Sstevel@tonic-gate 		 */
1937c478bd9Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		ASSERT(*refcntp > 0);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		if (--(*refcntp) == 0)
1987c478bd9Sstevel@tonic-gate 			cpu->cpu_intr_actv &= ~(1 << pil);
1997c478bd9Sstevel@tonic-gate 	} else {
2007c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_actv &= ~(1 << pil);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0);
2047c478bd9Sstevel@tonic-gate 
205*ae115bc7Smrj 	intrtime = now - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)];
2067a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
207eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * Check for lower-pil nested high-level interrupt beneath
2117c478bd9Sstevel@tonic-gate 	 * current one.  If so, place a starting timestamp in its
2127c478bd9Sstevel@tonic-gate 	 * pil_high_start entry.
2137c478bd9Sstevel@tonic-gate 	 */
2147c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
2157c478bd9Sstevel@tonic-gate 	if (mask != 0) {
2167c478bd9Sstevel@tonic-gate 		int nestpil;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		/*
2197c478bd9Sstevel@tonic-gate 		 * find PIL of nested interrupt
2207c478bd9Sstevel@tonic-gate 		 */
2217c478bd9Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
2227c478bd9Sstevel@tonic-gate 		ASSERT(nestpil < pil);
223*ae115bc7Smrj 		mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = now;
2247c478bd9Sstevel@tonic-gate 		/*
2257c478bd9Sstevel@tonic-gate 		 * (Another high-level interrupt is active below this one,
2267c478bd9Sstevel@tonic-gate 		 * so there is no need to check for an interrupt
2277c478bd9Sstevel@tonic-gate 		 * thread.  That will be done by the lowest priority
2287c478bd9Sstevel@tonic-gate 		 * high-level interrupt active.)
2297c478bd9Sstevel@tonic-gate 		 */
2307c478bd9Sstevel@tonic-gate 	} else {
2317c478bd9Sstevel@tonic-gate 		/*
2327c478bd9Sstevel@tonic-gate 		 * Check to see if there is a low-level interrupt active.
2337c478bd9Sstevel@tonic-gate 		 * If so, place a starting timestamp in the thread
2347c478bd9Sstevel@tonic-gate 		 * structure.
2357c478bd9Sstevel@tonic-gate 		 */
2367c478bd9Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		if (t->t_flag & T_INTR_THREAD)
239*ae115bc7Smrj 			t->t_intr_start = now;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = oldpil;
2437c478bd9Sstevel@tonic-gate 	(void) (*setlvlx)(oldpil, vecnum);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate  * Set up the cpu, thread and interrupt thread structures for
2507c478bd9Sstevel@tonic-gate  * executing an interrupt thread.  The new stack pointer of the
2517c478bd9Sstevel@tonic-gate  * interrupt thread (which *must* be switched to) is returned.
2527c478bd9Sstevel@tonic-gate  */
253*ae115bc7Smrj static caddr_t
2547c478bd9Sstevel@tonic-gate intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
2577c478bd9Sstevel@tonic-gate 	kthread_t *t, *volatile it;
258*ae115bc7Smrj 	hrtime_t now = tsc_read();
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	ASSERT(pil > 0);
2617c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
2627c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * Get set to run an interrupt thread.
2667c478bd9Sstevel@tonic-gate 	 * There should always be an interrupt thread, since we
2677c478bd9Sstevel@tonic-gate 	 * allocate one for each level on each CPU.
2687c478bd9Sstevel@tonic-gate 	 *
269fd71cd2fSesolom 	 * t_intr_start could be zero due to cpu_intr_swtch_enter.
2707c478bd9Sstevel@tonic-gate 	 */
2717c478bd9Sstevel@tonic-gate 	t = cpu->cpu_thread;
272fd71cd2fSesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
273*ae115bc7Smrj 		hrtime_t intrtime = now - t->t_intr_start;
2747a364d25Sschwartz 		mcpu->intrstat[t->t_pil][0] += intrtime;
275eda89462Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
2767c478bd9Sstevel@tonic-gate 		t->t_intr_start = 0;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;	/* mark stack in curthread for resume */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	/*
2847c478bd9Sstevel@tonic-gate 	 * unlink the interrupt thread off the cpu
285fd71cd2fSesolom 	 *
286fd71cd2fSesolom 	 * Note that the code in kcpc_overflow_intr -relies- on the
287fd71cd2fSesolom 	 * ordering of events here - in particular that t->t_lwp of
288fd71cd2fSesolom 	 * the interrupt thread is set to the pinned thread *before*
289fd71cd2fSesolom 	 * curthread is changed.
2907c478bd9Sstevel@tonic-gate 	 */
2917c478bd9Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
2927c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
2937c478bd9Sstevel@tonic-gate 	it->t_intr = t;
2947c478bd9Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/*
2977c478bd9Sstevel@tonic-gate 	 * (threads on the interrupt thread free list could have state
2987c478bd9Sstevel@tonic-gate 	 * preset to TS_ONPROC, but it helps in debugging if
2997c478bd9Sstevel@tonic-gate 	 * they're TS_FREE.)
3007c478bd9Sstevel@tonic-gate 	 */
3017c478bd9Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = it;		/* new curthread on this cpu */
3047c478bd9Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
3057c478bd9Sstevel@tonic-gate 	it->t_pri = intr_pri + (pri_t)pil;
306*ae115bc7Smrj 	it->t_intr_start = now;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	return (it->t_stk);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate #ifdef DEBUG
3137c478bd9Sstevel@tonic-gate int intr_thread_cnt;
3147c478bd9Sstevel@tonic-gate #endif
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate  * Called with interrupts disabled
3187c478bd9Sstevel@tonic-gate  */
319*ae115bc7Smrj static void
3207c478bd9Sstevel@tonic-gate intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
3237c478bd9Sstevel@tonic-gate 	kthread_t *t;
3247c478bd9Sstevel@tonic-gate 	kthread_t *it = cpu->cpu_thread;	/* curthread */
3257c478bd9Sstevel@tonic-gate 	uint_t pil, basespl;
326eda89462Sesolom 	hrtime_t intrtime;
327*ae115bc7Smrj 	hrtime_t now = tsc_read();
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	pil = it->t_pil;
3307c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	ASSERT(it->t_intr_start != 0);
333*ae115bc7Smrj 	intrtime = now - it->t_intr_start;
3347a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
335eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
3387c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/*
3417c478bd9Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
3427c478bd9Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
3437c478bd9Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
3447c478bd9Sstevel@tonic-gate 	 */
3457c478bd9Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
3467c478bd9Sstevel@tonic-gate 		/*
3477c478bd9Sstevel@tonic-gate 		 * The interrupted thread is no longer pinned underneath
3487c478bd9Sstevel@tonic-gate 		 * the interrupt thread.  This means the interrupt must
3497c478bd9Sstevel@tonic-gate 		 * have blocked, and the interrupted thread has been
3507c478bd9Sstevel@tonic-gate 		 * unpinned, and has probably been running around the
3517c478bd9Sstevel@tonic-gate 		 * system for a while.
3527c478bd9Sstevel@tonic-gate 		 *
3537c478bd9Sstevel@tonic-gate 		 * Since there is no longer a thread under this one, put
3547c478bd9Sstevel@tonic-gate 		 * this interrupt thread back on the CPU's free list and
3557c478bd9Sstevel@tonic-gate 		 * resume the idle thread which will dispatch the next
3567c478bd9Sstevel@tonic-gate 		 * thread to run.
3577c478bd9Sstevel@tonic-gate 		 */
3587c478bd9Sstevel@tonic-gate #ifdef DEBUG
3597c478bd9Sstevel@tonic-gate 		intr_thread_cnt++;
3607c478bd9Sstevel@tonic-gate #endif
3617c478bd9Sstevel@tonic-gate 		cpu->cpu_stats.sys.intrblk++;
3627c478bd9Sstevel@tonic-gate 		/*
3637c478bd9Sstevel@tonic-gate 		 * Set CPU's base SPL based on active interrupts bitmask
3647c478bd9Sstevel@tonic-gate 		 */
3657c478bd9Sstevel@tonic-gate 		set_base_spl();
3667c478bd9Sstevel@tonic-gate 		basespl = cpu->cpu_base_spl;
3677c478bd9Sstevel@tonic-gate 		mcpu->mcpu_pri = basespl;
3687c478bd9Sstevel@tonic-gate 		(*setlvlx)(basespl, vec);
3697c478bd9Sstevel@tonic-gate 		(void) splhigh();
370*ae115bc7Smrj 		sti();
3717c478bd9Sstevel@tonic-gate 		it->t_state = TS_FREE;
3727c478bd9Sstevel@tonic-gate 		/*
3737c478bd9Sstevel@tonic-gate 		 * Return interrupt thread to pool
3747c478bd9Sstevel@tonic-gate 		 */
3757c478bd9Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
3767c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
3777c478bd9Sstevel@tonic-gate 		swtch();
378*ae115bc7Smrj 		panic("intr_thread_epilog: swtch returned");
3797c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Return interrupt thread to the pool
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
3867c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
3877c478bd9Sstevel@tonic-gate 	it->t_state = TS_FREE;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
3907c478bd9Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
3917c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
3927c478bd9Sstevel@tonic-gate 	(*setlvlx)(pil, vec);
393*ae115bc7Smrj 	t->t_intr_start = now;
3947c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = t;
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977a364d25Sschwartz /*
398*ae115bc7Smrj  * intr_get_time() is a resource for interrupt handlers to determine how
399*ae115bc7Smrj  * much time has been spent handling the current interrupt. Such a function
400*ae115bc7Smrj  * is needed because higher level interrupts can arrive during the
401*ae115bc7Smrj  * processing of an interrupt.  intr_get_time() only returns time spent in the
402*ae115bc7Smrj  * current interrupt handler.
403*ae115bc7Smrj  *
404*ae115bc7Smrj  * The caller must be calling from an interrupt handler running at a pil
405*ae115bc7Smrj  * below or at lock level. Timings are not provided for high-level
406*ae115bc7Smrj  * interrupts.
407*ae115bc7Smrj  *
408*ae115bc7Smrj  * The first time intr_get_time() is called while handling an interrupt,
409*ae115bc7Smrj  * it returns the time since the interrupt handler was invoked. Subsequent
410*ae115bc7Smrj  * calls will return the time since the prior call to intr_get_time(). Time
411*ae115bc7Smrj  * is returned as ticks. Use tsc_scalehrtime() to convert ticks to nsec.
412*ae115bc7Smrj  *
413*ae115bc7Smrj  * Theory Of Intrstat[][]:
414*ae115bc7Smrj  *
415*ae115bc7Smrj  * uint64_t intrstat[pil][0..1] is an array indexed by pil level, with two
416*ae115bc7Smrj  * uint64_ts per pil.
417*ae115bc7Smrj  *
418*ae115bc7Smrj  * intrstat[pil][0] is a cumulative count of the number of ticks spent
419*ae115bc7Smrj  * handling all interrupts at the specified pil on this CPU. It is
420*ae115bc7Smrj  * exported via kstats to the user.
421*ae115bc7Smrj  *
422*ae115bc7Smrj  * intrstat[pil][1] is always a count of ticks less than or equal to the
423*ae115bc7Smrj  * value in [0]. The difference between [1] and [0] is the value returned
424*ae115bc7Smrj  * by a call to intr_get_time(). At the start of interrupt processing,
425*ae115bc7Smrj  * [0] and [1] will be equal (or nearly so). As the interrupt consumes
426*ae115bc7Smrj  * time, [0] will increase, but [1] will remain the same. A call to
427*ae115bc7Smrj  * intr_get_time() will return the difference, then update [1] to be the
428*ae115bc7Smrj  * same as [0]. Future calls will return the time since the last call.
429*ae115bc7Smrj  * Finally, when the interrupt completes, [1] is updated to the same as [0].
430*ae115bc7Smrj  *
431*ae115bc7Smrj  * Implementation:
432*ae115bc7Smrj  *
433*ae115bc7Smrj  * intr_get_time() works much like a higher level interrupt arriving. It
434*ae115bc7Smrj  * "checkpoints" the timing information by incrementing intrstat[pil][0]
435*ae115bc7Smrj  * to include elapsed running time, and by setting t_intr_start to rdtsc.
436*ae115bc7Smrj  * It then sets the return value to intrstat[pil][0] - intrstat[pil][1],
437*ae115bc7Smrj  * and updates intrstat[pil][1] to be the same as the new value of
438*ae115bc7Smrj  * intrstat[pil][0].
439*ae115bc7Smrj  *
440*ae115bc7Smrj  * In the normal handling of interrupts, after an interrupt handler returns
441*ae115bc7Smrj  * and the code in intr_thread() updates intrstat[pil][0], it then sets
442*ae115bc7Smrj  * intrstat[pil][1] to the new value of intrstat[pil][0]. When [0] == [1],
443*ae115bc7Smrj  * the timings are reset, i.e. intr_get_time() will return [0] - [1] which
444*ae115bc7Smrj  * is 0.
445*ae115bc7Smrj  *
446*ae115bc7Smrj  * Whenever interrupts arrive on a CPU which is handling a lower pil
447*ae115bc7Smrj  * interrupt, they update the lower pil's [0] to show time spent in the
448*ae115bc7Smrj  * handler that they've interrupted. This results in a growing discrepancy
449*ae115bc7Smrj  * between [0] and [1], which is returned the next time intr_get_time() is
450*ae115bc7Smrj  * called. Time spent in the higher-pil interrupt will not be returned in
451*ae115bc7Smrj  * the next intr_get_time() call from the original interrupt, because
452*ae115bc7Smrj  * the higher-pil interrupt's time is accumulated in intrstat[higherpil][].
4537a364d25Sschwartz  */
4547a364d25Sschwartz uint64_t
455*ae115bc7Smrj intr_get_time(void)
4567a364d25Sschwartz {
457*ae115bc7Smrj 	struct cpu *cpu;
458*ae115bc7Smrj 	struct machcpu *mcpu;
459*ae115bc7Smrj 	kthread_t *t;
4607a364d25Sschwartz 	uint64_t time, delta, ret;
461*ae115bc7Smrj 	uint_t pil;
4627a364d25Sschwartz 
463*ae115bc7Smrj 	cli();
464*ae115bc7Smrj 	cpu = CPU;
465*ae115bc7Smrj 	mcpu = &cpu->cpu_m;
466*ae115bc7Smrj 	t = cpu->cpu_thread;
467*ae115bc7Smrj 	pil = t->t_pil;
4687a364d25Sschwartz 	ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0);
4697a364d25Sschwartz 	ASSERT(t->t_flag & T_INTR_THREAD);
4707a364d25Sschwartz 	ASSERT(pil != 0);
4717a364d25Sschwartz 	ASSERT(t->t_intr_start != 0);
4727a364d25Sschwartz 
4737a364d25Sschwartz 	time = tsc_read();
4747a364d25Sschwartz 	delta = time - t->t_intr_start;
4757a364d25Sschwartz 	t->t_intr_start = time;
4767a364d25Sschwartz 
4777a364d25Sschwartz 	time = mcpu->intrstat[pil][0] + delta;
4787a364d25Sschwartz 	ret = time - mcpu->intrstat[pil][1];
4797a364d25Sschwartz 	mcpu->intrstat[pil][0] = time;
4807a364d25Sschwartz 	mcpu->intrstat[pil][1] = time;
481c81508f4Sjhaslam 	cpu->cpu_intracct[cpu->cpu_mstate] += delta;
4827a364d25Sschwartz 
483*ae115bc7Smrj 	sti();
4847a364d25Sschwartz 	return (ret);
4857a364d25Sschwartz }
4867a364d25Sschwartz 
487*ae115bc7Smrj static caddr_t
4887c478bd9Sstevel@tonic-gate dosoftint_prolog(
4897c478bd9Sstevel@tonic-gate 	struct cpu *cpu,
4907c478bd9Sstevel@tonic-gate 	caddr_t stackptr,
4917c478bd9Sstevel@tonic-gate 	uint32_t st_pending,
4927c478bd9Sstevel@tonic-gate 	uint_t oldpil)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	kthread_t *t, *volatile it;
4957c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
4967c478bd9Sstevel@tonic-gate 	uint_t pil;
497*ae115bc7Smrj 	hrtime_t now;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate top:
5007c478bd9Sstevel@tonic-gate 	ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	pil = bsrw_insn((uint16_t)st_pending);
5037c478bd9Sstevel@tonic-gate 	if (pil <= oldpil || pil <= cpu->cpu_base_spl)
5047c478bd9Sstevel@tonic-gate 		return (0);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	/*
5077c478bd9Sstevel@tonic-gate 	 * XX64	Sigh.
5087c478bd9Sstevel@tonic-gate 	 *
5097c478bd9Sstevel@tonic-gate 	 * This is a transliteration of the i386 assembler code for
5107c478bd9Sstevel@tonic-gate 	 * soft interrupts.  One question is "why does this need
5117c478bd9Sstevel@tonic-gate 	 * to be atomic?"  One possible race is -other- processors
5127c478bd9Sstevel@tonic-gate 	 * posting soft interrupts to us in set_pending() i.e. the
5137c478bd9Sstevel@tonic-gate 	 * CPU might get preempted just after the address computation,
5147c478bd9Sstevel@tonic-gate 	 * but just before the atomic transaction, so another CPU would
5157c478bd9Sstevel@tonic-gate 	 * actually set the original CPU's st_pending bit.  However,
5167c478bd9Sstevel@tonic-gate 	 * it looks like it would be simpler to disable preemption there.
5177c478bd9Sstevel@tonic-gate 	 * Are there other races for which preemption control doesn't work?
5187c478bd9Sstevel@tonic-gate 	 *
5197c478bd9Sstevel@tonic-gate 	 * The i386 assembler version -also- checks to see if the bit
5207c478bd9Sstevel@tonic-gate 	 * being cleared was actually set; if it wasn't, it rechecks
5217c478bd9Sstevel@tonic-gate 	 * for more.  This seems a bit strange, as the only code that
5227c478bd9Sstevel@tonic-gate 	 * ever clears the bit is -this- code running with interrupts
5237c478bd9Sstevel@tonic-gate 	 * disabled on -this- CPU.  This code would probably be cheaper:
5247c478bd9Sstevel@tonic-gate 	 *
5257c478bd9Sstevel@tonic-gate 	 * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending,
5267c478bd9Sstevel@tonic-gate 	 *   ~(1 << pil));
5277c478bd9Sstevel@tonic-gate 	 *
5287c478bd9Sstevel@tonic-gate 	 * and t->t_preempt--/++ around set_pending() even cheaper,
5297c478bd9Sstevel@tonic-gate 	 * but at this point, correctness is critical, so we slavishly
5307c478bd9Sstevel@tonic-gate 	 * emulate the i386 port.
5317c478bd9Sstevel@tonic-gate 	 */
532*ae115bc7Smrj 	if (atomic_btr32((uint32_t *)
533*ae115bc7Smrj 	    &mcpu->mcpu_softinfo.st_pending, pil) == 0) {
5347c478bd9Sstevel@tonic-gate 		st_pending = mcpu->mcpu_softinfo.st_pending;
5357c478bd9Sstevel@tonic-gate 		goto top;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
5397c478bd9Sstevel@tonic-gate 	(*setspl)(pil);
5407c478bd9Sstevel@tonic-gate 
541*ae115bc7Smrj 	now = tsc_read();
542*ae115bc7Smrj 
5437c478bd9Sstevel@tonic-gate 	/*
5447c478bd9Sstevel@tonic-gate 	 * Get set to run interrupt thread.
5457c478bd9Sstevel@tonic-gate 	 * There should always be an interrupt thread since we
5467c478bd9Sstevel@tonic-gate 	 * allocate one for each level on the CPU.
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
5497c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
5507c478bd9Sstevel@tonic-gate 
551fd71cd2fSesolom 	/* t_intr_start could be zero due to cpu_intr_swtch_enter. */
552fd71cd2fSesolom 	t = cpu->cpu_thread;
553fd71cd2fSesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
554*ae115bc7Smrj 		hrtime_t intrtime = now - t->t_intr_start;
555fd71cd2fSesolom 		mcpu->intrstat[pil][0] += intrtime;
556fd71cd2fSesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
557fd71cd2fSesolom 		t->t_intr_start = 0;
558fd71cd2fSesolom 	}
559fd71cd2fSesolom 
5607c478bd9Sstevel@tonic-gate 	/*
5617c478bd9Sstevel@tonic-gate 	 * Note that the code in kcpc_overflow_intr -relies- on the
5627c478bd9Sstevel@tonic-gate 	 * ordering of events here - in particular that t->t_lwp of
5637c478bd9Sstevel@tonic-gate 	 * the interrupt thread is set to the pinned thread *before*
564fd71cd2fSesolom 	 * curthread is changed.
5657c478bd9Sstevel@tonic-gate 	 */
5667c478bd9Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
5677c478bd9Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/*
5707c478bd9Sstevel@tonic-gate 	 * Push interrupted thread onto list from new thread.
5717c478bd9Sstevel@tonic-gate 	 * Set the new thread as the current one.
5727c478bd9Sstevel@tonic-gate 	 * Set interrupted thread's T_SP because if it is the idle thread,
5737c478bd9Sstevel@tonic-gate 	 * resume() may use that stack between threads.
5747c478bd9Sstevel@tonic-gate 	 */
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
5777c478bd9Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	it->t_intr = t;
5807c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = it;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	/*
5837c478bd9Sstevel@tonic-gate 	 * Set bit for this pil in CPU's interrupt active bitmask.
5847c478bd9Sstevel@tonic-gate 	 */
5857c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
5867c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	/*
5897c478bd9Sstevel@tonic-gate 	 * Initialize thread priority level from intr_pri
5907c478bd9Sstevel@tonic-gate 	 */
5917c478bd9Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
5927c478bd9Sstevel@tonic-gate 	it->t_pri = (pri_t)pil + intr_pri;
593*ae115bc7Smrj 	it->t_intr_start = now;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	return (it->t_stk);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
598*ae115bc7Smrj static void
5997c478bd9Sstevel@tonic-gate dosoftint_epilog(struct cpu *cpu, uint_t oldpil)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
6027c478bd9Sstevel@tonic-gate 	kthread_t *t, *it;
6037c478bd9Sstevel@tonic-gate 	uint_t pil, basespl;
604eda89462Sesolom 	hrtime_t intrtime;
605*ae115bc7Smrj 	hrtime_t now = tsc_read();
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	it = cpu->cpu_thread;
6087c478bd9Sstevel@tonic-gate 	pil = it->t_pil;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
6137c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
614*ae115bc7Smrj 	intrtime = now - it->t_intr_start;
6157a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
616eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	/*
6197c478bd9Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
6207c478bd9Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
6217c478bd9Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
6227c478bd9Sstevel@tonic-gate 	 */
6237c478bd9Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
6247c478bd9Sstevel@tonic-gate 		/*
6257c478bd9Sstevel@tonic-gate 		 * Put thread back on the interrupt thread list.
6267c478bd9Sstevel@tonic-gate 		 * This was an interrupt thread, so set CPU's base SPL.
6277c478bd9Sstevel@tonic-gate 		 */
6287c478bd9Sstevel@tonic-gate 		set_base_spl();
6297c478bd9Sstevel@tonic-gate 		it->t_state = TS_FREE;
6307c478bd9Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
6317c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
6327c478bd9Sstevel@tonic-gate 		(void) splhigh();
633*ae115bc7Smrj 		sti();
6347c478bd9Sstevel@tonic-gate 		swtch();
6357c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
636*ae115bc7Smrj 		panic("dosoftint_epilog: swtch returned");
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
6397c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
6407c478bd9Sstevel@tonic-gate 	it->t_state = TS_FREE;
6417c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = t;
6427c478bd9Sstevel@tonic-gate 	if (t->t_flag & T_INTR_THREAD)
643*ae115bc7Smrj 		t->t_intr_start = now;
6447c478bd9Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
6457c478bd9Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
6467c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
6477c478bd9Sstevel@tonic-gate 	(*setspl)(pil);
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
650*ae115bc7Smrj 
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate  * Make the interrupted thread 'to' be runnable.
6537c478bd9Sstevel@tonic-gate  *
6547c478bd9Sstevel@tonic-gate  * Since t->t_sp has already been saved, t->t_pc is all
6557c478bd9Sstevel@tonic-gate  * that needs to be set in this function.
6567c478bd9Sstevel@tonic-gate  *
6577c478bd9Sstevel@tonic-gate  * Returns the interrupt level of the interrupt thread.
6587c478bd9Sstevel@tonic-gate  */
6597c478bd9Sstevel@tonic-gate int
6607c478bd9Sstevel@tonic-gate intr_passivate(
6617c478bd9Sstevel@tonic-gate 	kthread_t *it,		/* interrupt thread */
6627c478bd9Sstevel@tonic-gate 	kthread_t *t)		/* interrupted thread */
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	extern void _sys_rtt();
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	ASSERT(it->t_flag & T_INTR_THREAD);
6677c478bd9Sstevel@tonic-gate 	ASSERT(SA(t->t_sp) == t->t_sp);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	t->t_pc = (uintptr_t)_sys_rtt;
6707c478bd9Sstevel@tonic-gate 	return (it->t_pil);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * Create interrupt kstats for this CPU.
6757c478bd9Sstevel@tonic-gate  */
6767c478bd9Sstevel@tonic-gate void
6777c478bd9Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	int		i;
6807c478bd9Sstevel@tonic-gate 	kstat_t		*intr_ksp;
6817c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp;
6827c478bd9Sstevel@tonic-gate 	char		name[KSTAT_STRLEN];
6837c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	if (pool_pset_enabled())
6887c478bd9Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
6897c478bd9Sstevel@tonic-gate 	else
6907c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
6937c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	/*
6967c478bd9Sstevel@tonic-gate 	 * Initialize each PIL's named kstat
6977c478bd9Sstevel@tonic-gate 	 */
6987c478bd9Sstevel@tonic-gate 	if (intr_ksp != NULL) {
6997c478bd9Sstevel@tonic-gate 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
7007c478bd9Sstevel@tonic-gate 		knp = (kstat_named_t *)intr_ksp->ks_data;
7017c478bd9Sstevel@tonic-gate 		intr_ksp->ks_private = cp;
7027c478bd9Sstevel@tonic-gate 		for (i = 0; i < PIL_MAX; i++) {
7037c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
7047c478bd9Sstevel@tonic-gate 			    i + 1);
7057c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
7067c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
7077c478bd9Sstevel@tonic-gate 			    i + 1);
7087c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[(i * 2) + 1], name,
7097c478bd9Sstevel@tonic-gate 			    KSTAT_DATA_UINT64);
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 		kstat_install(intr_ksp);
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate /*
7167c478bd9Sstevel@tonic-gate  * Delete interrupt kstats for this CPU.
7177c478bd9Sstevel@tonic-gate  */
7187c478bd9Sstevel@tonic-gate void
7197c478bd9Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp)
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate /*
7257c478bd9Sstevel@tonic-gate  * Convert interrupt statistics from CPU ticks to nanoseconds and
7267c478bd9Sstevel@tonic-gate  * update kstat.
7277c478bd9Sstevel@tonic-gate  */
7287c478bd9Sstevel@tonic-gate int
7297c478bd9Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
7307c478bd9Sstevel@tonic-gate {
7317c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp = ksp->ks_data;
7327c478bd9Sstevel@tonic-gate 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
7337c478bd9Sstevel@tonic-gate 	int		i;
7347c478bd9Sstevel@tonic-gate 	hrtime_t	hrt;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7377c478bd9Sstevel@tonic-gate 		return (EACCES);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
7407a364d25Sschwartz 		hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0];
7417c478bd9Sstevel@tonic-gate 		tsc_scalehrtime(&hrt);
7427c478bd9Sstevel@tonic-gate 		knp[i * 2].value.ui64 = (uint64_t)hrt;
7437c478bd9Sstevel@tonic-gate 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	return (0);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate /*
7507c478bd9Sstevel@tonic-gate  * An interrupt thread is ending a time slice, so compute the interval it
7517c478bd9Sstevel@tonic-gate  * ran for and update the statistic for its PIL.
7527c478bd9Sstevel@tonic-gate  */
7537c478bd9Sstevel@tonic-gate void
7547c478bd9Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	uint64_t	interval;
7577c478bd9Sstevel@tonic-gate 	uint64_t	start;
758eda89462Sesolom 	cpu_t		*cpu;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
7617c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/*
7647c478bd9Sstevel@tonic-gate 	 * We could be here with a zero timestamp. This could happen if:
7657c478bd9Sstevel@tonic-gate 	 * an interrupt thread which no longer has a pinned thread underneath
7667c478bd9Sstevel@tonic-gate 	 * it (i.e. it blocked at some point in its past) has finished running
7677c478bd9Sstevel@tonic-gate 	 * its handler. intr_thread() updated the interrupt statistic for its
7687c478bd9Sstevel@tonic-gate 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
7697c478bd9Sstevel@tonic-gate 	 * return to, swtch() gets called and we end up here.
770eda89462Sesolom 	 *
771eda89462Sesolom 	 * Note that we use atomic ops below (cas64 and atomic_add_64), which
772eda89462Sesolom 	 * we don't use in the functions above, because we're not called
773eda89462Sesolom 	 * with interrupts blocked, but the epilog/prolog functions are.
7747c478bd9Sstevel@tonic-gate 	 */
7757c478bd9Sstevel@tonic-gate 	if (t->t_intr_start) {
7767c478bd9Sstevel@tonic-gate 		do {
7777c478bd9Sstevel@tonic-gate 			start = t->t_intr_start;
7787c478bd9Sstevel@tonic-gate 			interval = tsc_read() - start;
7797c478bd9Sstevel@tonic-gate 		} while (cas64(&t->t_intr_start, start, 0) != start);
780eda89462Sesolom 		cpu = CPU;
7817a364d25Sschwartz 		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
782eda89462Sesolom 
783eda89462Sesolom 		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
784eda89462Sesolom 		    interval);
7857c478bd9Sstevel@tonic-gate 	} else
7867c478bd9Sstevel@tonic-gate 		ASSERT(t->t_intr == NULL);
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * An interrupt thread is returning from swtch(). Place a starting timestamp
7917c478bd9Sstevel@tonic-gate  * in its thread structure.
7927c478bd9Sstevel@tonic-gate  */
7937c478bd9Sstevel@tonic-gate void
7947c478bd9Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	uint64_t ts;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
7997c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	do {
8027c478bd9Sstevel@tonic-gate 		ts = t->t_intr_start;
8037c478bd9Sstevel@tonic-gate 	} while (cas64(&t->t_intr_start, ts, tsc_read()) != ts);
8047c478bd9Sstevel@tonic-gate }
805*ae115bc7Smrj 
806*ae115bc7Smrj /*
807*ae115bc7Smrj  * Dispatch a hilevel interrupt (one above LOCK_LEVEL)
808*ae115bc7Smrj  */
809*ae115bc7Smrj /*ARGSUSED*/
810*ae115bc7Smrj static void
811*ae115bc7Smrj dispatch_hilevel(uint_t vector, uint_t arg2)
812*ae115bc7Smrj {
813*ae115bc7Smrj 	sti();
814*ae115bc7Smrj 	av_dispatch_autovect(vector);
815*ae115bc7Smrj 	cli();
816*ae115bc7Smrj }
817*ae115bc7Smrj 
818*ae115bc7Smrj /*
819*ae115bc7Smrj  * Dispatch a soft interrupt
820*ae115bc7Smrj  */
821*ae115bc7Smrj /*ARGSUSED*/
822*ae115bc7Smrj static void
823*ae115bc7Smrj dispatch_softint(uint_t oldpil, uint_t arg2)
824*ae115bc7Smrj {
825*ae115bc7Smrj 	struct cpu *cpu = CPU;
826*ae115bc7Smrj 
827*ae115bc7Smrj 	sti();
828*ae115bc7Smrj 	av_dispatch_softvect((int)cpu->cpu_thread->t_pil);
829*ae115bc7Smrj 	cli();
830*ae115bc7Smrj 
831*ae115bc7Smrj 	/*
832*ae115bc7Smrj 	 * Must run softint_epilog() on the interrupt thread stack, since
833*ae115bc7Smrj 	 * there may not be a return from it if the interrupt thread blocked.
834*ae115bc7Smrj 	 */
835*ae115bc7Smrj 	dosoftint_epilog(cpu, oldpil);
836*ae115bc7Smrj }
837*ae115bc7Smrj 
838*ae115bc7Smrj /*
839*ae115bc7Smrj  * Dispatch a normal interrupt
840*ae115bc7Smrj  */
841*ae115bc7Smrj static void
842*ae115bc7Smrj dispatch_hardint(uint_t vector, uint_t oldipl)
843*ae115bc7Smrj {
844*ae115bc7Smrj 	struct cpu *cpu = CPU;
845*ae115bc7Smrj 
846*ae115bc7Smrj 	sti();
847*ae115bc7Smrj 	av_dispatch_autovect(vector);
848*ae115bc7Smrj 	cli();
849*ae115bc7Smrj 
850*ae115bc7Smrj 	/*
851*ae115bc7Smrj 	 * Must run intr_thread_epilog() on the interrupt thread stack, since
852*ae115bc7Smrj 	 * there may not be a return from it if the interrupt thread blocked.
853*ae115bc7Smrj 	 */
854*ae115bc7Smrj 	intr_thread_epilog(cpu, vector, oldipl);
855*ae115bc7Smrj }
856*ae115bc7Smrj 
857*ae115bc7Smrj /*
858*ae115bc7Smrj  * Deliver any softints the current interrupt priority allows.
859*ae115bc7Smrj  * Called with interrupts disabled.
860*ae115bc7Smrj  */
861*ae115bc7Smrj void
862*ae115bc7Smrj dosoftint(struct regs *regs)
863*ae115bc7Smrj {
864*ae115bc7Smrj 	struct cpu *cpu = CPU;
865*ae115bc7Smrj 	int oldipl;
866*ae115bc7Smrj 	caddr_t newsp;
867*ae115bc7Smrj 
868*ae115bc7Smrj 	while (cpu->cpu_softinfo.st_pending) {
869*ae115bc7Smrj 		oldipl = cpu->cpu_pri;
870*ae115bc7Smrj 		newsp = dosoftint_prolog(cpu, (caddr_t)regs,
871*ae115bc7Smrj 			cpu->cpu_softinfo.st_pending, oldipl);
872*ae115bc7Smrj 		/*
873*ae115bc7Smrj 		 * If returned stack pointer is NULL, priority is too high
874*ae115bc7Smrj 		 * to run any of the pending softints now.
875*ae115bc7Smrj 		 * Break out and they will be run later.
876*ae115bc7Smrj 		 */
877*ae115bc7Smrj 		if (newsp == NULL)
878*ae115bc7Smrj 			break;
879*ae115bc7Smrj 		switch_sp_and_call(newsp, dispatch_softint, oldipl, 0);
880*ae115bc7Smrj 	}
881*ae115bc7Smrj }
882*ae115bc7Smrj 
883*ae115bc7Smrj /*
884*ae115bc7Smrj  * Interrupt service routine, called with interrupts disabled.
885*ae115bc7Smrj  */
886*ae115bc7Smrj /*ARGSUSED*/
887*ae115bc7Smrj void
888*ae115bc7Smrj do_interrupt(struct regs *rp, trap_trace_rec_t *ttp)
889*ae115bc7Smrj {
890*ae115bc7Smrj 	struct cpu *cpu = CPU;
891*ae115bc7Smrj 	int newipl, oldipl = cpu->cpu_pri;
892*ae115bc7Smrj 	uint_t vector;
893*ae115bc7Smrj 	caddr_t newsp;
894*ae115bc7Smrj 
895*ae115bc7Smrj #ifdef TRAPTRACE
896*ae115bc7Smrj 	ttp->ttr_marker = TT_INTERRUPT;
897*ae115bc7Smrj 	ttp->ttr_ipl = 0xff;
898*ae115bc7Smrj 	ttp->ttr_pri = oldipl;
899*ae115bc7Smrj 	ttp->ttr_spl = cpu->cpu_base_spl;
900*ae115bc7Smrj 	ttp->ttr_vector = 0xff;
901*ae115bc7Smrj #endif	/* TRAPTRACE */
902*ae115bc7Smrj 
903*ae115bc7Smrj 	/*
904*ae115bc7Smrj 	 * If it's a softint go do it now.
905*ae115bc7Smrj 	 */
906*ae115bc7Smrj 	if (rp->r_trapno == T_SOFTINT) {
907*ae115bc7Smrj 		dosoftint(rp);
908*ae115bc7Smrj 		ASSERT(!interrupts_enabled());
909*ae115bc7Smrj 		return;
910*ae115bc7Smrj 	}
911*ae115bc7Smrj 
912*ae115bc7Smrj 	/*
913*ae115bc7Smrj 	 * Raise the interrupt priority.
914*ae115bc7Smrj 	 */
915*ae115bc7Smrj 	newipl = (*setlvl)(oldipl, (int *)&rp->r_trapno);
916*ae115bc7Smrj #ifdef TRAPTRACE
917*ae115bc7Smrj 	ttp->ttr_ipl = newipl;
918*ae115bc7Smrj #endif	/* TRAPTRACE */
919*ae115bc7Smrj 
920*ae115bc7Smrj 	/*
921*ae115bc7Smrj 	 * Bail if it is a spurious interrupt
922*ae115bc7Smrj 	 */
923*ae115bc7Smrj 	if (newipl == -1)
924*ae115bc7Smrj 		return;
925*ae115bc7Smrj 	cpu->cpu_pri = newipl;
926*ae115bc7Smrj 	vector = rp->r_trapno;
927*ae115bc7Smrj #ifdef TRAPTRACE
928*ae115bc7Smrj 	ttp->ttr_vector = vector;
929*ae115bc7Smrj #endif	/* TRAPTRACE */
930*ae115bc7Smrj 	if (newipl > LOCK_LEVEL) {
931*ae115bc7Smrj 		/*
932*ae115bc7Smrj 		 * High priority interrupts run on this cpu's interrupt stack.
933*ae115bc7Smrj 		 */
934*ae115bc7Smrj 		if (hilevel_intr_prolog(cpu, newipl, oldipl, rp) == 0) {
935*ae115bc7Smrj 			newsp = cpu->cpu_intr_stack;
936*ae115bc7Smrj 			switch_sp_and_call(newsp, dispatch_hilevel, vector, 0);
937*ae115bc7Smrj 		} else { /* already on the interrupt stack */
938*ae115bc7Smrj 			dispatch_hilevel(vector, 0);
939*ae115bc7Smrj 		}
940*ae115bc7Smrj 		(void) hilevel_intr_epilog(cpu, newipl, oldipl, vector);
941*ae115bc7Smrj 	} else {
942*ae115bc7Smrj 		/*
943*ae115bc7Smrj 		 * Run this interrupt in a separate thread.
944*ae115bc7Smrj 		 */
945*ae115bc7Smrj 		newsp = intr_thread_prolog(cpu, (caddr_t)rp, newipl);
946*ae115bc7Smrj 		switch_sp_and_call(newsp, dispatch_hardint, vector, oldipl);
947*ae115bc7Smrj 	}
948*ae115bc7Smrj 
949*ae115bc7Smrj 	/*
950*ae115bc7Smrj 	 * Deliver any pending soft interrupts.
951*ae115bc7Smrj 	 */
952*ae115bc7Smrj 	if (cpu->cpu_softinfo.st_pending)
953*ae115bc7Smrj 		dosoftint(rp);
954*ae115bc7Smrj }
955*ae115bc7Smrj 
956*ae115bc7Smrj /*
957*ae115bc7Smrj  * Common tasks always done by _sys_rtt, called with interrupts disabled.
958*ae115bc7Smrj  * Returns 1 if returning to userland, 0 if returning to system mode.
959*ae115bc7Smrj  */
960*ae115bc7Smrj int
961*ae115bc7Smrj sys_rtt_common(struct regs *rp)
962*ae115bc7Smrj {
963*ae115bc7Smrj 	kthread_t *tp;
964*ae115bc7Smrj 	extern void mutex_exit_critical_start();
965*ae115bc7Smrj 	extern long mutex_exit_critical_size;
966*ae115bc7Smrj 
967*ae115bc7Smrj loop:
968*ae115bc7Smrj 
969*ae115bc7Smrj 	/*
970*ae115bc7Smrj 	 * Check if returning to user
971*ae115bc7Smrj 	 */
972*ae115bc7Smrj 	tp = CPU->cpu_thread;
973*ae115bc7Smrj 	if (USERMODE(rp->r_cs)) {
974*ae115bc7Smrj 		/*
975*ae115bc7Smrj 		 * Check if AST pending.
976*ae115bc7Smrj 		 */
977*ae115bc7Smrj 		if (tp->t_astflag) {
978*ae115bc7Smrj 			/*
979*ae115bc7Smrj 			 * Let trap() handle the AST
980*ae115bc7Smrj 			 */
981*ae115bc7Smrj 			sti();
982*ae115bc7Smrj 			rp->r_trapno = T_AST;
983*ae115bc7Smrj 			trap(rp, (caddr_t)0, CPU->cpu_id);
984*ae115bc7Smrj 			cli();
985*ae115bc7Smrj 			goto loop;
986*ae115bc7Smrj 		}
987*ae115bc7Smrj 
988*ae115bc7Smrj #if defined(__amd64)
989*ae115bc7Smrj 		/*
990*ae115bc7Smrj 		 * We are done if segment registers do not need updating.
991*ae115bc7Smrj 		 */
992*ae115bc7Smrj 		if ((tp->t_lwp->lwp_pcb.pcb_flags & RUPDATE_PENDING) == 0)
993*ae115bc7Smrj 			return (1);
994*ae115bc7Smrj 
995*ae115bc7Smrj 		if (update_sregs(rp, tp->t_lwp)) {
996*ae115bc7Smrj 			/*
997*ae115bc7Smrj 			 * 1 or more of the selectors is bad.
998*ae115bc7Smrj 			 * Deliver a SIGSEGV.
999*ae115bc7Smrj 			 */
1000*ae115bc7Smrj 			proc_t *p = ttoproc(tp);
1001*ae115bc7Smrj 
1002*ae115bc7Smrj 			sti();
1003*ae115bc7Smrj 			mutex_enter(&p->p_lock);
1004*ae115bc7Smrj 			tp->t_lwp->lwp_cursig = SIGSEGV;
1005*ae115bc7Smrj 			mutex_exit(&p->p_lock);
1006*ae115bc7Smrj 			psig();
1007*ae115bc7Smrj 			tp->t_sig_check = 1;
1008*ae115bc7Smrj 			cli();
1009*ae115bc7Smrj 		}
1010*ae115bc7Smrj 		tp->t_lwp->lwp_pcb.pcb_flags &= ~RUPDATE_PENDING;
1011*ae115bc7Smrj 
1012*ae115bc7Smrj #endif	/* __amd64 */
1013*ae115bc7Smrj 		return (1);
1014*ae115bc7Smrj 	}
1015*ae115bc7Smrj 
1016*ae115bc7Smrj 	/*
1017*ae115bc7Smrj 	 * Here if we are returning to supervisor mode.
1018*ae115bc7Smrj 	 * Check for a kernel preemption request.
1019*ae115bc7Smrj 	 */
1020*ae115bc7Smrj 	if (CPU->cpu_kprunrun && (rp->r_ps & PS_IE)) {
1021*ae115bc7Smrj 
1022*ae115bc7Smrj 		/*
1023*ae115bc7Smrj 		 * Do nothing if already in kpreempt
1024*ae115bc7Smrj 		 */
1025*ae115bc7Smrj 		if (!tp->t_preempt_lk) {
1026*ae115bc7Smrj 			tp->t_preempt_lk = 1;
1027*ae115bc7Smrj 			sti();
1028*ae115bc7Smrj 			kpreempt(1); /* asynchronous kpreempt call */
1029*ae115bc7Smrj 			cli();
1030*ae115bc7Smrj 			tp->t_preempt_lk = 0;
1031*ae115bc7Smrj 		}
1032*ae115bc7Smrj 	}
1033*ae115bc7Smrj 
1034*ae115bc7Smrj 	/*
1035*ae115bc7Smrj 	 * If we interrupted the mutex_exit() critical region we must
1036*ae115bc7Smrj 	 * reset the PC back to the beginning to prevent missed wakeups
1037*ae115bc7Smrj 	 * See the comments in mutex_exit() for details.
1038*ae115bc7Smrj 	 */
1039*ae115bc7Smrj 	if ((uintptr_t)rp->r_pc - (uintptr_t)mutex_exit_critical_start <
1040*ae115bc7Smrj 	    mutex_exit_critical_size) {
1041*ae115bc7Smrj 		rp->r_pc = (greg_t)mutex_exit_critical_start;
1042*ae115bc7Smrj 	}
1043*ae115bc7Smrj 	return (0);
1044*ae115bc7Smrj }
1045*ae115bc7Smrj 
1046*ae115bc7Smrj void
1047*ae115bc7Smrj send_dirint(int cpuid, int int_level)
1048*ae115bc7Smrj {
1049*ae115bc7Smrj 	(*send_dirintf)(cpuid, int_level);
1050*ae115bc7Smrj }
1051*ae115bc7Smrj 
1052*ae115bc7Smrj /*
1053*ae115bc7Smrj  * do_splx routine, takes new ipl to set
1054*ae115bc7Smrj  * returns the old ipl.
1055*ae115bc7Smrj  * We are careful not to set priority lower than CPU->cpu_base_pri,
1056*ae115bc7Smrj  * even though it seems we're raising the priority, it could be set
1057*ae115bc7Smrj  * higher at any time by an interrupt routine, so we must block interrupts
1058*ae115bc7Smrj  * and look at CPU->cpu_base_pri
1059*ae115bc7Smrj  */
1060*ae115bc7Smrj int
1061*ae115bc7Smrj do_splx(int newpri)
1062*ae115bc7Smrj {
1063*ae115bc7Smrj 	ulong_t	flag;
1064*ae115bc7Smrj 	cpu_t	*cpu;
1065*ae115bc7Smrj 	int	curpri, basepri;
1066*ae115bc7Smrj 
1067*ae115bc7Smrj 	flag = intr_clear();
1068*ae115bc7Smrj 	cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
1069*ae115bc7Smrj 	curpri = cpu->cpu_m.mcpu_pri;
1070*ae115bc7Smrj 	basepri = cpu->cpu_base_spl;
1071*ae115bc7Smrj 	if (newpri < basepri)
1072*ae115bc7Smrj 		newpri = basepri;
1073*ae115bc7Smrj 	cpu->cpu_m.mcpu_pri = newpri;
1074*ae115bc7Smrj 	(*setspl)(newpri);
1075*ae115bc7Smrj 	/*
1076*ae115bc7Smrj 	 * If we are going to reenable interrupts see if new priority level
1077*ae115bc7Smrj 	 * allows pending softint delivery.
1078*ae115bc7Smrj 	 */
1079*ae115bc7Smrj 	if ((flag & PS_IE) &&
1080*ae115bc7Smrj 	    bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > newpri)
1081*ae115bc7Smrj 		fakesoftint();
1082*ae115bc7Smrj 	ASSERT(!interrupts_enabled());
1083*ae115bc7Smrj 	intr_restore(flag);
1084*ae115bc7Smrj 	return (curpri);
1085*ae115bc7Smrj }
1086*ae115bc7Smrj 
1087*ae115bc7Smrj /*
1088*ae115bc7Smrj  * Common spl raise routine, takes new ipl to set
1089*ae115bc7Smrj  * returns the old ipl, will not lower ipl.
1090*ae115bc7Smrj  */
1091*ae115bc7Smrj int
1092*ae115bc7Smrj splr(int newpri)
1093*ae115bc7Smrj {
1094*ae115bc7Smrj 	ulong_t	flag;
1095*ae115bc7Smrj 	cpu_t	*cpu;
1096*ae115bc7Smrj 	int	curpri, basepri;
1097*ae115bc7Smrj 
1098*ae115bc7Smrj 	flag = intr_clear();
1099*ae115bc7Smrj 	cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
1100*ae115bc7Smrj 	curpri = cpu->cpu_m.mcpu_pri;
1101*ae115bc7Smrj 	/*
1102*ae115bc7Smrj 	 * Only do something if new priority is larger
1103*ae115bc7Smrj 	 */
1104*ae115bc7Smrj 	if (newpri > curpri) {
1105*ae115bc7Smrj 		basepri = cpu->cpu_base_spl;
1106*ae115bc7Smrj 		if (newpri < basepri)
1107*ae115bc7Smrj 			newpri = basepri;
1108*ae115bc7Smrj 		cpu->cpu_m.mcpu_pri = newpri;
1109*ae115bc7Smrj 		(*setspl)(newpri);
1110*ae115bc7Smrj 		/*
1111*ae115bc7Smrj 		 * See if new priority level allows pending softint delivery
1112*ae115bc7Smrj 		 */
1113*ae115bc7Smrj 		if ((flag & PS_IE) &&
1114*ae115bc7Smrj 		    bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > newpri)
1115*ae115bc7Smrj 			fakesoftint();
1116*ae115bc7Smrj 	}
1117*ae115bc7Smrj 	intr_restore(flag);
1118*ae115bc7Smrj 	return (curpri);
1119*ae115bc7Smrj }
1120*ae115bc7Smrj 
1121*ae115bc7Smrj int
1122*ae115bc7Smrj getpil(void)
1123*ae115bc7Smrj {
1124*ae115bc7Smrj 	return (CPU->cpu_m.mcpu_pri);
1125*ae115bc7Smrj }
1126*ae115bc7Smrj 
1127*ae115bc7Smrj int
1128*ae115bc7Smrj interrupts_enabled(void)
1129*ae115bc7Smrj {
1130*ae115bc7Smrj 	ulong_t	flag;
1131*ae115bc7Smrj 
1132*ae115bc7Smrj 	flag = getflags();
1133*ae115bc7Smrj 	return ((flag & PS_IE) == PS_IE);
1134*ae115bc7Smrj }
1135*ae115bc7Smrj 
1136*ae115bc7Smrj #ifdef DEBUG
1137*ae115bc7Smrj void
1138*ae115bc7Smrj assert_ints_enabled(void)
1139*ae115bc7Smrj {
1140*ae115bc7Smrj 	ASSERT(!interrupts_unleashed || interrupts_enabled());
1141*ae115bc7Smrj }
1142*ae115bc7Smrj #endif	/* DEBUG */
1143