xref: /titanic_44/usr/src/uts/i86pc/os/intr.c (revision 7ff178cd8db129d385d3177eb20744d3b6efc59b)
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  */
21843e1988Sjohnlev 
227c478bd9Sstevel@tonic-gate /*
23*7ff178cdSJimmy Vetayases  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
27fb2caebeSRandy Fishel #include <sys/cpu_event.h>
287c478bd9Sstevel@tonic-gate #include <sys/regset.h>
297c478bd9Sstevel@tonic-gate #include <sys/psw.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/thread.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/segments.h>
347c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
357c478bd9Sstevel@tonic-gate #include <sys/trap.h>
367c478bd9Sstevel@tonic-gate #include <sys/ftrace.h>
377c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
387c478bd9Sstevel@tonic-gate #include <sys/clock.h>
397c478bd9Sstevel@tonic-gate #include <sys/panic.h>
407c478bd9Sstevel@tonic-gate #include <sys/disp.h>
417c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
427c478bd9Sstevel@tonic-gate #include <sys/stack.h>
437c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
467c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
477c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
487c478bd9Sstevel@tonic-gate #include <sys/zone.h>
497c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
50ae115bc7Smrj #include <sys/archsystm.h>
51ae115bc7Smrj #include <sys/machsystm.h>
52ae115bc7Smrj #include <sys/ontrap.h>
53ae115bc7Smrj #include <sys/x86_archext.h>
54ae115bc7Smrj #include <sys/promif.h>
5595c0a3c8Sjosephb #include <vm/hat_i86.h>
56843e1988Sjohnlev #if defined(__xpv)
57843e1988Sjohnlev #include <sys/hypervisor.h>
58843e1988Sjohnlev #endif
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 
61843e1988Sjohnlev #if defined(__xpv) && defined(DEBUG)
62843e1988Sjohnlev 
63843e1988Sjohnlev /*
64843e1988Sjohnlev  * This panic message is intended as an aid to interrupt debugging.
65843e1988Sjohnlev  *
66843e1988Sjohnlev  * The associated assertion tests the condition of enabling
67843e1988Sjohnlev  * events when events are already enabled.  The implication
68843e1988Sjohnlev  * being that whatever code the programmer thought was
69843e1988Sjohnlev  * protected by having events disabled until the second
70843e1988Sjohnlev  * enable happened really wasn't protected at all ..
71843e1988Sjohnlev  */
72843e1988Sjohnlev 
73843e1988Sjohnlev int stistipanic = 1;	/* controls the debug panic check */
74843e1988Sjohnlev const char *stistimsg = "stisti";
75843e1988Sjohnlev ulong_t laststi[NCPU];
76843e1988Sjohnlev 
77843e1988Sjohnlev /*
78843e1988Sjohnlev  * This variable tracks the last place events were disabled on each cpu
79fb2caebeSRandy Fishel  * it assists in debugging when asserts that interrupts are enabled trip.
80843e1988Sjohnlev  */
81843e1988Sjohnlev ulong_t lastcli[NCPU];
82843e1988Sjohnlev 
83843e1988Sjohnlev #endif
84843e1988Sjohnlev 
85*7ff178cdSJimmy Vetayases void do_interrupt(struct regs *rp, trap_trace_rec_t *ttp);
86*7ff178cdSJimmy Vetayases 
87*7ff178cdSJimmy Vetayases void (*do_interrupt_common)(struct regs *, trap_trace_rec_t *) = do_interrupt;
88*7ff178cdSJimmy Vetayases uintptr_t (*get_intr_handler)(int, short) = NULL;
89*7ff178cdSJimmy Vetayases 
907c478bd9Sstevel@tonic-gate /*
91ae115bc7Smrj  * Set cpu's base SPL level to the highest active interrupt level
927c478bd9Sstevel@tonic-gate  */
93ae115bc7Smrj void
94ae115bc7Smrj set_base_spl(void)
957c478bd9Sstevel@tonic-gate {
96ae115bc7Smrj 	struct cpu *cpu = CPU;
97ae115bc7Smrj 	uint16_t active = (uint16_t)cpu->cpu_intr_actv;
987c478bd9Sstevel@tonic-gate 
99ae115bc7Smrj 	cpu->cpu_base_spl = active == 0 ? 0 : bsrw_insn(active);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * Do all the work necessary to set up the cpu and thread structures
1047c478bd9Sstevel@tonic-gate  * to dispatch a high-level interrupt.
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * Returns 0 if we're -not- already on the high-level interrupt stack,
1077c478bd9Sstevel@tonic-gate  * (and *must* switch to it), non-zero if we are already on that stack.
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  * Called with interrupts masked.
1107c478bd9Sstevel@tonic-gate  * The 'pil' is already set to the appropriate level for rp->r_trapno.
1117c478bd9Sstevel@tonic-gate  */
112ae115bc7Smrj static int
1137c478bd9Sstevel@tonic-gate hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
1167c478bd9Sstevel@tonic-gate 	uint_t mask;
117eda89462Sesolom 	hrtime_t intrtime;
118ae115bc7Smrj 	hrtime_t now = tsc_read();
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	ASSERT(pil > LOCK_LEVEL);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (pil == CBE_HIGH_PIL) {
1237c478bd9Sstevel@tonic-gate 		cpu->cpu_profile_pil = oldpil;
1247c478bd9Sstevel@tonic-gate 		if (USERMODE(rp->r_cs)) {
1257c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_pc = 0;
1267c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_upc = rp->r_pc;
127b9e93c10SJonathan Haslam 			cpu->cpu_cpcprofile_pc = 0;
128b9e93c10SJonathan Haslam 			cpu->cpu_cpcprofile_upc = rp->r_pc;
1297c478bd9Sstevel@tonic-gate 		} else {
1307c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_pc = rp->r_pc;
1317c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_upc = 0;
132b9e93c10SJonathan Haslam 			cpu->cpu_cpcprofile_pc = rp->r_pc;
133b9e93c10SJonathan Haslam 			cpu->cpu_cpcprofile_upc = 0;
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
1387c478bd9Sstevel@tonic-gate 	if (mask != 0) {
1397c478bd9Sstevel@tonic-gate 		int nestpil;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		/*
1427c478bd9Sstevel@tonic-gate 		 * We have interrupted another high-level interrupt.
1437c478bd9Sstevel@tonic-gate 		 * Load starting timestamp, compute interval, update
1447c478bd9Sstevel@tonic-gate 		 * cumulative counter.
1457c478bd9Sstevel@tonic-gate 		 */
1467c478bd9Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
1477c478bd9Sstevel@tonic-gate 		ASSERT(nestpil < pil);
148ae115bc7Smrj 		intrtime = now -
1497c478bd9Sstevel@tonic-gate 		    mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)];
1507a364d25Sschwartz 		mcpu->intrstat[nestpil][0] += intrtime;
151eda89462Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1527c478bd9Sstevel@tonic-gate 		/*
1537c478bd9Sstevel@tonic-gate 		 * Another high-level interrupt is active below this one, so
1547c478bd9Sstevel@tonic-gate 		 * there is no need to check for an interrupt thread.  That
1557c478bd9Sstevel@tonic-gate 		 * will be done by the lowest priority high-level interrupt
1567c478bd9Sstevel@tonic-gate 		 * active.
1577c478bd9Sstevel@tonic-gate 		 */
1587c478bd9Sstevel@tonic-gate 	} else {
1597c478bd9Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		/*
1627c478bd9Sstevel@tonic-gate 		 * See if we are interrupting a low-level interrupt thread.
1637c478bd9Sstevel@tonic-gate 		 * If so, account for its time slice only if its time stamp
1647c478bd9Sstevel@tonic-gate 		 * is non-zero.
1657c478bd9Sstevel@tonic-gate 		 */
1667c478bd9Sstevel@tonic-gate 		if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) {
167ae115bc7Smrj 			intrtime = now - t->t_intr_start;
1687a364d25Sschwartz 			mcpu->intrstat[t->t_pil][0] += intrtime;
169eda89462Sesolom 			cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1707c478bd9Sstevel@tonic-gate 			t->t_intr_start = 0;
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/*
1757c478bd9Sstevel@tonic-gate 	 * Store starting timestamp in CPU structure for this PIL.
1767c478bd9Sstevel@tonic-gate 	 */
177ae115bc7Smrj 	mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = now;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (pil == 15) {
1827c478bd9Sstevel@tonic-gate 		/*
1837c478bd9Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
1847c478bd9Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
1857c478bd9Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
1867c478bd9Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
1877c478bd9Sstevel@tonic-gate 		 */
1887c478bd9Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
1897c478bd9Sstevel@tonic-gate 		(*refcntp)++;
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * Does most of the work of returning from a high level interrupt.
2017c478bd9Sstevel@tonic-gate  *
2027c478bd9Sstevel@tonic-gate  * Returns 0 if there are no more high level interrupts (in which
2037c478bd9Sstevel@tonic-gate  * case we must switch back to the interrupted thread stack) or
2047c478bd9Sstevel@tonic-gate  * non-zero if there are more (in which case we should stay on it).
2057c478bd9Sstevel@tonic-gate  *
2067c478bd9Sstevel@tonic-gate  * Called with interrupts masked
2077c478bd9Sstevel@tonic-gate  */
208ae115bc7Smrj static int
2097c478bd9Sstevel@tonic-gate hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
2127c478bd9Sstevel@tonic-gate 	uint_t mask;
213eda89462Sesolom 	hrtime_t intrtime;
214ae115bc7Smrj 	hrtime_t now = tsc_read();
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	ASSERT(mcpu->mcpu_pri == pil);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if (pil == 15) {
2237c478bd9Sstevel@tonic-gate 		/*
2247c478bd9Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
2257c478bd9Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
2267c478bd9Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
2277c478bd9Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
2287c478bd9Sstevel@tonic-gate 		 */
2297c478bd9Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		ASSERT(*refcntp > 0);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		if (--(*refcntp) == 0)
2347c478bd9Sstevel@tonic-gate 			cpu->cpu_intr_actv &= ~(1 << pil);
2357c478bd9Sstevel@tonic-gate 	} else {
2367c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_actv &= ~(1 << pil);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0);
2407c478bd9Sstevel@tonic-gate 
241ae115bc7Smrj 	intrtime = now - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)];
2427a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
243eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/*
2467c478bd9Sstevel@tonic-gate 	 * Check for lower-pil nested high-level interrupt beneath
2477c478bd9Sstevel@tonic-gate 	 * current one.  If so, place a starting timestamp in its
2487c478bd9Sstevel@tonic-gate 	 * pil_high_start entry.
2497c478bd9Sstevel@tonic-gate 	 */
2507c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
2517c478bd9Sstevel@tonic-gate 	if (mask != 0) {
2527c478bd9Sstevel@tonic-gate 		int nestpil;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		/*
2557c478bd9Sstevel@tonic-gate 		 * find PIL of nested interrupt
2567c478bd9Sstevel@tonic-gate 		 */
2577c478bd9Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
2587c478bd9Sstevel@tonic-gate 		ASSERT(nestpil < pil);
259ae115bc7Smrj 		mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = now;
2607c478bd9Sstevel@tonic-gate 		/*
2617c478bd9Sstevel@tonic-gate 		 * (Another high-level interrupt is active below this one,
2627c478bd9Sstevel@tonic-gate 		 * so there is no need to check for an interrupt
2637c478bd9Sstevel@tonic-gate 		 * thread.  That will be done by the lowest priority
2647c478bd9Sstevel@tonic-gate 		 * high-level interrupt active.)
2657c478bd9Sstevel@tonic-gate 		 */
2667c478bd9Sstevel@tonic-gate 	} else {
2677c478bd9Sstevel@tonic-gate 		/*
2687c478bd9Sstevel@tonic-gate 		 * Check to see if there is a low-level interrupt active.
2697c478bd9Sstevel@tonic-gate 		 * If so, place a starting timestamp in the thread
2707c478bd9Sstevel@tonic-gate 		 * structure.
2717c478bd9Sstevel@tonic-gate 		 */
2727c478bd9Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		if (t->t_flag & T_INTR_THREAD)
275ae115bc7Smrj 			t->t_intr_start = now;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = oldpil;
2797c478bd9Sstevel@tonic-gate 	(void) (*setlvlx)(oldpil, vecnum);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * Set up the cpu, thread and interrupt thread structures for
2867c478bd9Sstevel@tonic-gate  * executing an interrupt thread.  The new stack pointer of the
2877c478bd9Sstevel@tonic-gate  * interrupt thread (which *must* be switched to) is returned.
2887c478bd9Sstevel@tonic-gate  */
289ae115bc7Smrj static caddr_t
2907c478bd9Sstevel@tonic-gate intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
2937c478bd9Sstevel@tonic-gate 	kthread_t *t, *volatile it;
294ae115bc7Smrj 	hrtime_t now = tsc_read();
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	ASSERT(pil > 0);
2977c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
2987c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/*
3017c478bd9Sstevel@tonic-gate 	 * Get set to run an interrupt thread.
3027c478bd9Sstevel@tonic-gate 	 * There should always be an interrupt thread, since we
3037c478bd9Sstevel@tonic-gate 	 * allocate one for each level on each CPU.
3047c478bd9Sstevel@tonic-gate 	 *
305fd71cd2fSesolom 	 * t_intr_start could be zero due to cpu_intr_swtch_enter.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	t = cpu->cpu_thread;
308fd71cd2fSesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
309ae115bc7Smrj 		hrtime_t intrtime = now - t->t_intr_start;
3107a364d25Sschwartz 		mcpu->intrstat[t->t_pil][0] += intrtime;
311eda89462Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
3127c478bd9Sstevel@tonic-gate 		t->t_intr_start = 0;
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;	/* mark stack in curthread for resume */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 * unlink the interrupt thread off the cpu
321fd71cd2fSesolom 	 *
322fd71cd2fSesolom 	 * Note that the code in kcpc_overflow_intr -relies- on the
323fd71cd2fSesolom 	 * ordering of events here - in particular that t->t_lwp of
324fd71cd2fSesolom 	 * the interrupt thread is set to the pinned thread *before*
325fd71cd2fSesolom 	 * curthread is changed.
3267c478bd9Sstevel@tonic-gate 	 */
3277c478bd9Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
3287c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
3297c478bd9Sstevel@tonic-gate 	it->t_intr = t;
3307c478bd9Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * (threads on the interrupt thread free list could have state
3347c478bd9Sstevel@tonic-gate 	 * preset to TS_ONPROC, but it helps in debugging if
3357c478bd9Sstevel@tonic-gate 	 * they're TS_FREE.)
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = it;		/* new curthread on this cpu */
3407c478bd9Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
3417c478bd9Sstevel@tonic-gate 	it->t_pri = intr_pri + (pri_t)pil;
342ae115bc7Smrj 	it->t_intr_start = now;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	return (it->t_stk);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate #ifdef DEBUG
3497c478bd9Sstevel@tonic-gate int intr_thread_cnt;
3507c478bd9Sstevel@tonic-gate #endif
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate  * Called with interrupts disabled
3547c478bd9Sstevel@tonic-gate  */
355ae115bc7Smrj static void
3567c478bd9Sstevel@tonic-gate intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
3597c478bd9Sstevel@tonic-gate 	kthread_t *t;
3607c478bd9Sstevel@tonic-gate 	kthread_t *it = cpu->cpu_thread;	/* curthread */
3617c478bd9Sstevel@tonic-gate 	uint_t pil, basespl;
362eda89462Sesolom 	hrtime_t intrtime;
363ae115bc7Smrj 	hrtime_t now = tsc_read();
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	pil = it->t_pil;
3667c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	ASSERT(it->t_intr_start != 0);
369ae115bc7Smrj 	intrtime = now - it->t_intr_start;
3707a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
371eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
3747c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
3787c478bd9Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
3797c478bd9Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
3807c478bd9Sstevel@tonic-gate 	 */
3817c478bd9Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
3827c478bd9Sstevel@tonic-gate 		/*
3837c478bd9Sstevel@tonic-gate 		 * The interrupted thread is no longer pinned underneath
3847c478bd9Sstevel@tonic-gate 		 * the interrupt thread.  This means the interrupt must
3857c478bd9Sstevel@tonic-gate 		 * have blocked, and the interrupted thread has been
3867c478bd9Sstevel@tonic-gate 		 * unpinned, and has probably been running around the
3877c478bd9Sstevel@tonic-gate 		 * system for a while.
3887c478bd9Sstevel@tonic-gate 		 *
3897c478bd9Sstevel@tonic-gate 		 * Since there is no longer a thread under this one, put
3907c478bd9Sstevel@tonic-gate 		 * this interrupt thread back on the CPU's free list and
3917c478bd9Sstevel@tonic-gate 		 * resume the idle thread which will dispatch the next
3927c478bd9Sstevel@tonic-gate 		 * thread to run.
3937c478bd9Sstevel@tonic-gate 		 */
3947c478bd9Sstevel@tonic-gate #ifdef DEBUG
3957c478bd9Sstevel@tonic-gate 		intr_thread_cnt++;
3967c478bd9Sstevel@tonic-gate #endif
3977c478bd9Sstevel@tonic-gate 		cpu->cpu_stats.sys.intrblk++;
3987c478bd9Sstevel@tonic-gate 		/*
3997c478bd9Sstevel@tonic-gate 		 * Set CPU's base SPL based on active interrupts bitmask
4007c478bd9Sstevel@tonic-gate 		 */
4017c478bd9Sstevel@tonic-gate 		set_base_spl();
4027c478bd9Sstevel@tonic-gate 		basespl = cpu->cpu_base_spl;
4037c478bd9Sstevel@tonic-gate 		mcpu->mcpu_pri = basespl;
4047c478bd9Sstevel@tonic-gate 		(*setlvlx)(basespl, vec);
4057c478bd9Sstevel@tonic-gate 		(void) splhigh();
406ae115bc7Smrj 		sti();
4077c478bd9Sstevel@tonic-gate 		it->t_state = TS_FREE;
4087c478bd9Sstevel@tonic-gate 		/*
4097c478bd9Sstevel@tonic-gate 		 * Return interrupt thread to pool
4107c478bd9Sstevel@tonic-gate 		 */
4117c478bd9Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
4127c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
4137c478bd9Sstevel@tonic-gate 		swtch();
414ae115bc7Smrj 		panic("intr_thread_epilog: swtch returned");
4157c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * Return interrupt thread to the pool
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
4227c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
4237c478bd9Sstevel@tonic-gate 	it->t_state = TS_FREE;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
4267c478bd9Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
4277c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
4287c478bd9Sstevel@tonic-gate 	(*setlvlx)(pil, vec);
429ae115bc7Smrj 	t->t_intr_start = now;
4307c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = t;
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337a364d25Sschwartz /*
434ae115bc7Smrj  * intr_get_time() is a resource for interrupt handlers to determine how
435ae115bc7Smrj  * much time has been spent handling the current interrupt. Such a function
436ae115bc7Smrj  * is needed because higher level interrupts can arrive during the
437ae115bc7Smrj  * processing of an interrupt.  intr_get_time() only returns time spent in the
438ae115bc7Smrj  * current interrupt handler.
439ae115bc7Smrj  *
440ae115bc7Smrj  * The caller must be calling from an interrupt handler running at a pil
441ae115bc7Smrj  * below or at lock level. Timings are not provided for high-level
442ae115bc7Smrj  * interrupts.
443ae115bc7Smrj  *
444ae115bc7Smrj  * The first time intr_get_time() is called while handling an interrupt,
445ae115bc7Smrj  * it returns the time since the interrupt handler was invoked. Subsequent
446ae115bc7Smrj  * calls will return the time since the prior call to intr_get_time(). Time
447843e1988Sjohnlev  * is returned as ticks. Use scalehrtimef() to convert ticks to nsec.
448ae115bc7Smrj  *
449ae115bc7Smrj  * Theory Of Intrstat[][]:
450ae115bc7Smrj  *
451ae115bc7Smrj  * uint64_t intrstat[pil][0..1] is an array indexed by pil level, with two
452ae115bc7Smrj  * uint64_ts per pil.
453ae115bc7Smrj  *
454ae115bc7Smrj  * intrstat[pil][0] is a cumulative count of the number of ticks spent
455ae115bc7Smrj  * handling all interrupts at the specified pil on this CPU. It is
456ae115bc7Smrj  * exported via kstats to the user.
457ae115bc7Smrj  *
458ae115bc7Smrj  * intrstat[pil][1] is always a count of ticks less than or equal to the
459ae115bc7Smrj  * value in [0]. The difference between [1] and [0] is the value returned
460ae115bc7Smrj  * by a call to intr_get_time(). At the start of interrupt processing,
461ae115bc7Smrj  * [0] and [1] will be equal (or nearly so). As the interrupt consumes
462ae115bc7Smrj  * time, [0] will increase, but [1] will remain the same. A call to
463ae115bc7Smrj  * intr_get_time() will return the difference, then update [1] to be the
464ae115bc7Smrj  * same as [0]. Future calls will return the time since the last call.
465ae115bc7Smrj  * Finally, when the interrupt completes, [1] is updated to the same as [0].
466ae115bc7Smrj  *
467ae115bc7Smrj  * Implementation:
468ae115bc7Smrj  *
469ae115bc7Smrj  * intr_get_time() works much like a higher level interrupt arriving. It
470ae115bc7Smrj  * "checkpoints" the timing information by incrementing intrstat[pil][0]
471ae115bc7Smrj  * to include elapsed running time, and by setting t_intr_start to rdtsc.
472ae115bc7Smrj  * It then sets the return value to intrstat[pil][0] - intrstat[pil][1],
473ae115bc7Smrj  * and updates intrstat[pil][1] to be the same as the new value of
474ae115bc7Smrj  * intrstat[pil][0].
475ae115bc7Smrj  *
476ae115bc7Smrj  * In the normal handling of interrupts, after an interrupt handler returns
477ae115bc7Smrj  * and the code in intr_thread() updates intrstat[pil][0], it then sets
478ae115bc7Smrj  * intrstat[pil][1] to the new value of intrstat[pil][0]. When [0] == [1],
479ae115bc7Smrj  * the timings are reset, i.e. intr_get_time() will return [0] - [1] which
480ae115bc7Smrj  * is 0.
481ae115bc7Smrj  *
482ae115bc7Smrj  * Whenever interrupts arrive on a CPU which is handling a lower pil
483ae115bc7Smrj  * interrupt, they update the lower pil's [0] to show time spent in the
484ae115bc7Smrj  * handler that they've interrupted. This results in a growing discrepancy
485ae115bc7Smrj  * between [0] and [1], which is returned the next time intr_get_time() is
486ae115bc7Smrj  * called. Time spent in the higher-pil interrupt will not be returned in
487ae115bc7Smrj  * the next intr_get_time() call from the original interrupt, because
488ae115bc7Smrj  * the higher-pil interrupt's time is accumulated in intrstat[higherpil][].
4897a364d25Sschwartz  */
4907a364d25Sschwartz uint64_t
491ae115bc7Smrj intr_get_time(void)
4927a364d25Sschwartz {
493ae115bc7Smrj 	struct cpu *cpu;
494ae115bc7Smrj 	struct machcpu *mcpu;
495ae115bc7Smrj 	kthread_t *t;
4967a364d25Sschwartz 	uint64_t time, delta, ret;
497ae115bc7Smrj 	uint_t pil;
4987a364d25Sschwartz 
499ae115bc7Smrj 	cli();
500ae115bc7Smrj 	cpu = CPU;
501ae115bc7Smrj 	mcpu = &cpu->cpu_m;
502ae115bc7Smrj 	t = cpu->cpu_thread;
503ae115bc7Smrj 	pil = t->t_pil;
5047a364d25Sschwartz 	ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0);
5057a364d25Sschwartz 	ASSERT(t->t_flag & T_INTR_THREAD);
5067a364d25Sschwartz 	ASSERT(pil != 0);
5077a364d25Sschwartz 	ASSERT(t->t_intr_start != 0);
5087a364d25Sschwartz 
5097a364d25Sschwartz 	time = tsc_read();
5107a364d25Sschwartz 	delta = time - t->t_intr_start;
5117a364d25Sschwartz 	t->t_intr_start = time;
5127a364d25Sschwartz 
5137a364d25Sschwartz 	time = mcpu->intrstat[pil][0] + delta;
5147a364d25Sschwartz 	ret = time - mcpu->intrstat[pil][1];
5157a364d25Sschwartz 	mcpu->intrstat[pil][0] = time;
5167a364d25Sschwartz 	mcpu->intrstat[pil][1] = time;
517c81508f4Sjhaslam 	cpu->cpu_intracct[cpu->cpu_mstate] += delta;
5187a364d25Sschwartz 
519ae115bc7Smrj 	sti();
5207a364d25Sschwartz 	return (ret);
5217a364d25Sschwartz }
5227a364d25Sschwartz 
523ae115bc7Smrj static caddr_t
5247c478bd9Sstevel@tonic-gate dosoftint_prolog(
5257c478bd9Sstevel@tonic-gate 	struct cpu *cpu,
5267c478bd9Sstevel@tonic-gate 	caddr_t stackptr,
5277c478bd9Sstevel@tonic-gate 	uint32_t st_pending,
5287c478bd9Sstevel@tonic-gate 	uint_t oldpil)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	kthread_t *t, *volatile it;
5317c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
5327c478bd9Sstevel@tonic-gate 	uint_t pil;
533ae115bc7Smrj 	hrtime_t now;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate top:
5367c478bd9Sstevel@tonic-gate 	ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	pil = bsrw_insn((uint16_t)st_pending);
5397c478bd9Sstevel@tonic-gate 	if (pil <= oldpil || pil <= cpu->cpu_base_spl)
5407c478bd9Sstevel@tonic-gate 		return (0);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * XX64	Sigh.
5447c478bd9Sstevel@tonic-gate 	 *
5457c478bd9Sstevel@tonic-gate 	 * This is a transliteration of the i386 assembler code for
5467c478bd9Sstevel@tonic-gate 	 * soft interrupts.  One question is "why does this need
5477c478bd9Sstevel@tonic-gate 	 * to be atomic?"  One possible race is -other- processors
5487c478bd9Sstevel@tonic-gate 	 * posting soft interrupts to us in set_pending() i.e. the
5497c478bd9Sstevel@tonic-gate 	 * CPU might get preempted just after the address computation,
5507c478bd9Sstevel@tonic-gate 	 * but just before the atomic transaction, so another CPU would
5517c478bd9Sstevel@tonic-gate 	 * actually set the original CPU's st_pending bit.  However,
5527c478bd9Sstevel@tonic-gate 	 * it looks like it would be simpler to disable preemption there.
5537c478bd9Sstevel@tonic-gate 	 * Are there other races for which preemption control doesn't work?
5547c478bd9Sstevel@tonic-gate 	 *
5557c478bd9Sstevel@tonic-gate 	 * The i386 assembler version -also- checks to see if the bit
5567c478bd9Sstevel@tonic-gate 	 * being cleared was actually set; if it wasn't, it rechecks
5577c478bd9Sstevel@tonic-gate 	 * for more.  This seems a bit strange, as the only code that
5587c478bd9Sstevel@tonic-gate 	 * ever clears the bit is -this- code running with interrupts
5597c478bd9Sstevel@tonic-gate 	 * disabled on -this- CPU.  This code would probably be cheaper:
5607c478bd9Sstevel@tonic-gate 	 *
5617c478bd9Sstevel@tonic-gate 	 * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending,
5627c478bd9Sstevel@tonic-gate 	 *   ~(1 << pil));
5637c478bd9Sstevel@tonic-gate 	 *
5647c478bd9Sstevel@tonic-gate 	 * and t->t_preempt--/++ around set_pending() even cheaper,
5657c478bd9Sstevel@tonic-gate 	 * but at this point, correctness is critical, so we slavishly
5667c478bd9Sstevel@tonic-gate 	 * emulate the i386 port.
5677c478bd9Sstevel@tonic-gate 	 */
568ae115bc7Smrj 	if (atomic_btr32((uint32_t *)
569ae115bc7Smrj 	    &mcpu->mcpu_softinfo.st_pending, pil) == 0) {
5707c478bd9Sstevel@tonic-gate 		st_pending = mcpu->mcpu_softinfo.st_pending;
5717c478bd9Sstevel@tonic-gate 		goto top;
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
5757c478bd9Sstevel@tonic-gate 	(*setspl)(pil);
5767c478bd9Sstevel@tonic-gate 
577ae115bc7Smrj 	now = tsc_read();
578ae115bc7Smrj 
5797c478bd9Sstevel@tonic-gate 	/*
5807c478bd9Sstevel@tonic-gate 	 * Get set to run interrupt thread.
5817c478bd9Sstevel@tonic-gate 	 * There should always be an interrupt thread since we
5827c478bd9Sstevel@tonic-gate 	 * allocate one for each level on the CPU.
5837c478bd9Sstevel@tonic-gate 	 */
5847c478bd9Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
5857c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
5867c478bd9Sstevel@tonic-gate 
587fd71cd2fSesolom 	/* t_intr_start could be zero due to cpu_intr_swtch_enter. */
588fd71cd2fSesolom 	t = cpu->cpu_thread;
589fd71cd2fSesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
590ae115bc7Smrj 		hrtime_t intrtime = now - t->t_intr_start;
591fd71cd2fSesolom 		mcpu->intrstat[pil][0] += intrtime;
592fd71cd2fSesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
593fd71cd2fSesolom 		t->t_intr_start = 0;
594fd71cd2fSesolom 	}
595fd71cd2fSesolom 
5967c478bd9Sstevel@tonic-gate 	/*
5977c478bd9Sstevel@tonic-gate 	 * Note that the code in kcpc_overflow_intr -relies- on the
5987c478bd9Sstevel@tonic-gate 	 * ordering of events here - in particular that t->t_lwp of
5997c478bd9Sstevel@tonic-gate 	 * the interrupt thread is set to the pinned thread *before*
600fd71cd2fSesolom 	 * curthread is changed.
6017c478bd9Sstevel@tonic-gate 	 */
6027c478bd9Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
6037c478bd9Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/*
6067c478bd9Sstevel@tonic-gate 	 * Push interrupted thread onto list from new thread.
6077c478bd9Sstevel@tonic-gate 	 * Set the new thread as the current one.
6087c478bd9Sstevel@tonic-gate 	 * Set interrupted thread's T_SP because if it is the idle thread,
6097c478bd9Sstevel@tonic-gate 	 * resume() may use that stack between threads.
6107c478bd9Sstevel@tonic-gate 	 */
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
6137c478bd9Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	it->t_intr = t;
6167c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = it;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	/*
6197c478bd9Sstevel@tonic-gate 	 * Set bit for this pil in CPU's interrupt active bitmask.
6207c478bd9Sstevel@tonic-gate 	 */
6217c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
6227c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	/*
6257c478bd9Sstevel@tonic-gate 	 * Initialize thread priority level from intr_pri
6267c478bd9Sstevel@tonic-gate 	 */
6277c478bd9Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
6287c478bd9Sstevel@tonic-gate 	it->t_pri = (pri_t)pil + intr_pri;
629ae115bc7Smrj 	it->t_intr_start = now;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	return (it->t_stk);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
634ae115bc7Smrj static void
6357c478bd9Sstevel@tonic-gate dosoftint_epilog(struct cpu *cpu, uint_t oldpil)
6367c478bd9Sstevel@tonic-gate {
6377c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
6387c478bd9Sstevel@tonic-gate 	kthread_t *t, *it;
6397c478bd9Sstevel@tonic-gate 	uint_t pil, basespl;
640eda89462Sesolom 	hrtime_t intrtime;
641ae115bc7Smrj 	hrtime_t now = tsc_read();
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	it = cpu->cpu_thread;
6447c478bd9Sstevel@tonic-gate 	pil = it->t_pil;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
6497c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
650ae115bc7Smrj 	intrtime = now - it->t_intr_start;
6517a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
652eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
6567c478bd9Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
6577c478bd9Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
6587c478bd9Sstevel@tonic-gate 	 */
6597c478bd9Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
6607c478bd9Sstevel@tonic-gate 		/*
6617c478bd9Sstevel@tonic-gate 		 * Put thread back on the interrupt thread list.
6627c478bd9Sstevel@tonic-gate 		 * This was an interrupt thread, so set CPU's base SPL.
6637c478bd9Sstevel@tonic-gate 		 */
6647c478bd9Sstevel@tonic-gate 		set_base_spl();
6657c478bd9Sstevel@tonic-gate 		it->t_state = TS_FREE;
6667c478bd9Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
6677c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
6687c478bd9Sstevel@tonic-gate 		(void) splhigh();
669ae115bc7Smrj 		sti();
6707c478bd9Sstevel@tonic-gate 		swtch();
6717c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
672ae115bc7Smrj 		panic("dosoftint_epilog: swtch returned");
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
6757c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
6767c478bd9Sstevel@tonic-gate 	it->t_state = TS_FREE;
6777c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = t;
6787c478bd9Sstevel@tonic-gate 	if (t->t_flag & T_INTR_THREAD)
679ae115bc7Smrj 		t->t_intr_start = now;
6807c478bd9Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
6817c478bd9Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
6827c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
6837c478bd9Sstevel@tonic-gate 	(*setspl)(pil);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
686ae115bc7Smrj 
6877c478bd9Sstevel@tonic-gate /*
6887c478bd9Sstevel@tonic-gate  * Make the interrupted thread 'to' be runnable.
6897c478bd9Sstevel@tonic-gate  *
6907c478bd9Sstevel@tonic-gate  * Since t->t_sp has already been saved, t->t_pc is all
6917c478bd9Sstevel@tonic-gate  * that needs to be set in this function.
6927c478bd9Sstevel@tonic-gate  *
6937c478bd9Sstevel@tonic-gate  * Returns the interrupt level of the interrupt thread.
6947c478bd9Sstevel@tonic-gate  */
6957c478bd9Sstevel@tonic-gate int
6967c478bd9Sstevel@tonic-gate intr_passivate(
6977c478bd9Sstevel@tonic-gate 	kthread_t *it,		/* interrupt thread */
6987c478bd9Sstevel@tonic-gate 	kthread_t *t)		/* interrupted thread */
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 	extern void _sys_rtt();
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	ASSERT(it->t_flag & T_INTR_THREAD);
7037c478bd9Sstevel@tonic-gate 	ASSERT(SA(t->t_sp) == t->t_sp);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	t->t_pc = (uintptr_t)_sys_rtt;
7067c478bd9Sstevel@tonic-gate 	return (it->t_pil);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate /*
7107c478bd9Sstevel@tonic-gate  * Create interrupt kstats for this CPU.
7117c478bd9Sstevel@tonic-gate  */
7127c478bd9Sstevel@tonic-gate void
7137c478bd9Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp)
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 	int		i;
7167c478bd9Sstevel@tonic-gate 	kstat_t		*intr_ksp;
7177c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp;
7187c478bd9Sstevel@tonic-gate 	char		name[KSTAT_STRLEN];
7197c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (pool_pset_enabled())
7247c478bd9Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
7257c478bd9Sstevel@tonic-gate 	else
7267c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
7297c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	/*
7327c478bd9Sstevel@tonic-gate 	 * Initialize each PIL's named kstat
7337c478bd9Sstevel@tonic-gate 	 */
7347c478bd9Sstevel@tonic-gate 	if (intr_ksp != NULL) {
7357c478bd9Sstevel@tonic-gate 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
7367c478bd9Sstevel@tonic-gate 		knp = (kstat_named_t *)intr_ksp->ks_data;
7377c478bd9Sstevel@tonic-gate 		intr_ksp->ks_private = cp;
7387c478bd9Sstevel@tonic-gate 		for (i = 0; i < PIL_MAX; i++) {
7397c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
7407c478bd9Sstevel@tonic-gate 			    i + 1);
7417c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
7427c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
7437c478bd9Sstevel@tonic-gate 			    i + 1);
7447c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[(i * 2) + 1], name,
7457c478bd9Sstevel@tonic-gate 			    KSTAT_DATA_UINT64);
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 		kstat_install(intr_ksp);
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate /*
7527c478bd9Sstevel@tonic-gate  * Delete interrupt kstats for this CPU.
7537c478bd9Sstevel@tonic-gate  */
7547c478bd9Sstevel@tonic-gate void
7557c478bd9Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp)
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate /*
7617c478bd9Sstevel@tonic-gate  * Convert interrupt statistics from CPU ticks to nanoseconds and
7627c478bd9Sstevel@tonic-gate  * update kstat.
7637c478bd9Sstevel@tonic-gate  */
7647c478bd9Sstevel@tonic-gate int
7657c478bd9Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp = ksp->ks_data;
7687c478bd9Sstevel@tonic-gate 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
7697c478bd9Sstevel@tonic-gate 	int		i;
7707c478bd9Sstevel@tonic-gate 	hrtime_t	hrt;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7737c478bd9Sstevel@tonic-gate 		return (EACCES);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
7767a364d25Sschwartz 		hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0];
777843e1988Sjohnlev 		scalehrtimef(&hrt);
7787c478bd9Sstevel@tonic-gate 		knp[i * 2].value.ui64 = (uint64_t)hrt;
7797c478bd9Sstevel@tonic-gate 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	return (0);
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate /*
7867c478bd9Sstevel@tonic-gate  * An interrupt thread is ending a time slice, so compute the interval it
7877c478bd9Sstevel@tonic-gate  * ran for and update the statistic for its PIL.
7887c478bd9Sstevel@tonic-gate  */
7897c478bd9Sstevel@tonic-gate void
7907c478bd9Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t)
7917c478bd9Sstevel@tonic-gate {
7927c478bd9Sstevel@tonic-gate 	uint64_t	interval;
7937c478bd9Sstevel@tonic-gate 	uint64_t	start;
794eda89462Sesolom 	cpu_t		*cpu;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
7977c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	/*
8007c478bd9Sstevel@tonic-gate 	 * We could be here with a zero timestamp. This could happen if:
8017c478bd9Sstevel@tonic-gate 	 * an interrupt thread which no longer has a pinned thread underneath
8027c478bd9Sstevel@tonic-gate 	 * it (i.e. it blocked at some point in its past) has finished running
8037c478bd9Sstevel@tonic-gate 	 * its handler. intr_thread() updated the interrupt statistic for its
8047c478bd9Sstevel@tonic-gate 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
8057c478bd9Sstevel@tonic-gate 	 * return to, swtch() gets called and we end up here.
806eda89462Sesolom 	 *
807eda89462Sesolom 	 * Note that we use atomic ops below (cas64 and atomic_add_64), which
808eda89462Sesolom 	 * we don't use in the functions above, because we're not called
809eda89462Sesolom 	 * with interrupts blocked, but the epilog/prolog functions are.
8107c478bd9Sstevel@tonic-gate 	 */
8117c478bd9Sstevel@tonic-gate 	if (t->t_intr_start) {
8127c478bd9Sstevel@tonic-gate 		do {
8137c478bd9Sstevel@tonic-gate 			start = t->t_intr_start;
8147c478bd9Sstevel@tonic-gate 			interval = tsc_read() - start;
8157c478bd9Sstevel@tonic-gate 		} while (cas64(&t->t_intr_start, start, 0) != start);
816eda89462Sesolom 		cpu = CPU;
8177a364d25Sschwartz 		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
818eda89462Sesolom 
819eda89462Sesolom 		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
820eda89462Sesolom 		    interval);
8217c478bd9Sstevel@tonic-gate 	} else
8227c478bd9Sstevel@tonic-gate 		ASSERT(t->t_intr == NULL);
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate /*
8267c478bd9Sstevel@tonic-gate  * An interrupt thread is returning from swtch(). Place a starting timestamp
8277c478bd9Sstevel@tonic-gate  * in its thread structure.
8287c478bd9Sstevel@tonic-gate  */
8297c478bd9Sstevel@tonic-gate void
8307c478bd9Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t)
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	uint64_t ts;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
8357c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	do {
8387c478bd9Sstevel@tonic-gate 		ts = t->t_intr_start;
8397c478bd9Sstevel@tonic-gate 	} while (cas64(&t->t_intr_start, ts, tsc_read()) != ts);
8407c478bd9Sstevel@tonic-gate }
841ae115bc7Smrj 
842ae115bc7Smrj /*
843ae115bc7Smrj  * Dispatch a hilevel interrupt (one above LOCK_LEVEL)
844ae115bc7Smrj  */
845ae115bc7Smrj /*ARGSUSED*/
846ae115bc7Smrj static void
847ae115bc7Smrj dispatch_hilevel(uint_t vector, uint_t arg2)
848ae115bc7Smrj {
849ae115bc7Smrj 	sti();
850ae115bc7Smrj 	av_dispatch_autovect(vector);
851ae115bc7Smrj 	cli();
852ae115bc7Smrj }
853ae115bc7Smrj 
854ae115bc7Smrj /*
855ae115bc7Smrj  * Dispatch a soft interrupt
856ae115bc7Smrj  */
857ae115bc7Smrj /*ARGSUSED*/
858ae115bc7Smrj static void
859ae115bc7Smrj dispatch_softint(uint_t oldpil, uint_t arg2)
860ae115bc7Smrj {
861ae115bc7Smrj 	struct cpu *cpu = CPU;
862ae115bc7Smrj 
863ae115bc7Smrj 	sti();
864ae115bc7Smrj 	av_dispatch_softvect((int)cpu->cpu_thread->t_pil);
865ae115bc7Smrj 	cli();
866ae115bc7Smrj 
867ae115bc7Smrj 	/*
868ae115bc7Smrj 	 * Must run softint_epilog() on the interrupt thread stack, since
869ae115bc7Smrj 	 * there may not be a return from it if the interrupt thread blocked.
870ae115bc7Smrj 	 */
871ae115bc7Smrj 	dosoftint_epilog(cpu, oldpil);
872ae115bc7Smrj }
873ae115bc7Smrj 
874ae115bc7Smrj /*
875ae115bc7Smrj  * Dispatch a normal interrupt
876ae115bc7Smrj  */
877ae115bc7Smrj static void
878ae115bc7Smrj dispatch_hardint(uint_t vector, uint_t oldipl)
879ae115bc7Smrj {
880ae115bc7Smrj 	struct cpu *cpu = CPU;
881ae115bc7Smrj 
882ae115bc7Smrj 	sti();
883ae115bc7Smrj 	av_dispatch_autovect(vector);
884ae115bc7Smrj 	cli();
885ae115bc7Smrj 
886ae115bc7Smrj 	/*
887ae115bc7Smrj 	 * Must run intr_thread_epilog() on the interrupt thread stack, since
888ae115bc7Smrj 	 * there may not be a return from it if the interrupt thread blocked.
889ae115bc7Smrj 	 */
890ae115bc7Smrj 	intr_thread_epilog(cpu, vector, oldipl);
891ae115bc7Smrj }
892ae115bc7Smrj 
893ae115bc7Smrj /*
894ae115bc7Smrj  * Deliver any softints the current interrupt priority allows.
895ae115bc7Smrj  * Called with interrupts disabled.
896ae115bc7Smrj  */
897ae115bc7Smrj void
898ae115bc7Smrj dosoftint(struct regs *regs)
899ae115bc7Smrj {
900ae115bc7Smrj 	struct cpu *cpu = CPU;
901ae115bc7Smrj 	int oldipl;
902ae115bc7Smrj 	caddr_t newsp;
903ae115bc7Smrj 
904ae115bc7Smrj 	while (cpu->cpu_softinfo.st_pending) {
905ae115bc7Smrj 		oldipl = cpu->cpu_pri;
906ae115bc7Smrj 		newsp = dosoftint_prolog(cpu, (caddr_t)regs,
907ae115bc7Smrj 		    cpu->cpu_softinfo.st_pending, oldipl);
908ae115bc7Smrj 		/*
909ae115bc7Smrj 		 * If returned stack pointer is NULL, priority is too high
910ae115bc7Smrj 		 * to run any of the pending softints now.
911ae115bc7Smrj 		 * Break out and they will be run later.
912ae115bc7Smrj 		 */
913ae115bc7Smrj 		if (newsp == NULL)
914ae115bc7Smrj 			break;
915ae115bc7Smrj 		switch_sp_and_call(newsp, dispatch_softint, oldipl, 0);
916ae115bc7Smrj 	}
917ae115bc7Smrj }
918ae115bc7Smrj 
919ae115bc7Smrj /*
920ae115bc7Smrj  * Interrupt service routine, called with interrupts disabled.
921ae115bc7Smrj  */
922ae115bc7Smrj /*ARGSUSED*/
923ae115bc7Smrj void
924ae115bc7Smrj do_interrupt(struct regs *rp, trap_trace_rec_t *ttp)
925ae115bc7Smrj {
926ae115bc7Smrj 	struct cpu *cpu = CPU;
927ae115bc7Smrj 	int newipl, oldipl = cpu->cpu_pri;
928ae115bc7Smrj 	uint_t vector;
929ae115bc7Smrj 	caddr_t newsp;
930ae115bc7Smrj 
931ae115bc7Smrj #ifdef TRAPTRACE
932ae115bc7Smrj 	ttp->ttr_marker = TT_INTERRUPT;
933ae115bc7Smrj 	ttp->ttr_ipl = 0xff;
934ae115bc7Smrj 	ttp->ttr_pri = oldipl;
935ae115bc7Smrj 	ttp->ttr_spl = cpu->cpu_base_spl;
936ae115bc7Smrj 	ttp->ttr_vector = 0xff;
937ae115bc7Smrj #endif	/* TRAPTRACE */
938ae115bc7Smrj 
939fb2caebeSRandy Fishel 	cpu_idle_exit(CPU_IDLE_CB_FLAG_INTR);
94095c0a3c8Sjosephb 
9413006ae82SFrank Van Der Linden 	++*(uint16_t *)&cpu->cpu_m.mcpu_istamp;
9423006ae82SFrank Van Der Linden 
94395c0a3c8Sjosephb 	/*
944ae115bc7Smrj 	 * If it's a softint go do it now.
945ae115bc7Smrj 	 */
946ae115bc7Smrj 	if (rp->r_trapno == T_SOFTINT) {
947ae115bc7Smrj 		dosoftint(rp);
948ae115bc7Smrj 		ASSERT(!interrupts_enabled());
949ae115bc7Smrj 		return;
950ae115bc7Smrj 	}
951ae115bc7Smrj 
952ae115bc7Smrj 	/*
953ae115bc7Smrj 	 * Raise the interrupt priority.
954ae115bc7Smrj 	 */
955ae115bc7Smrj 	newipl = (*setlvl)(oldipl, (int *)&rp->r_trapno);
956ae115bc7Smrj #ifdef TRAPTRACE
957ae115bc7Smrj 	ttp->ttr_ipl = newipl;
958ae115bc7Smrj #endif	/* TRAPTRACE */
959ae115bc7Smrj 
960ae115bc7Smrj 	/*
961ae115bc7Smrj 	 * Bail if it is a spurious interrupt
962ae115bc7Smrj 	 */
963ae115bc7Smrj 	if (newipl == -1)
964ae115bc7Smrj 		return;
965ae115bc7Smrj 	cpu->cpu_pri = newipl;
966ae115bc7Smrj 	vector = rp->r_trapno;
967ae115bc7Smrj #ifdef TRAPTRACE
968ae115bc7Smrj 	ttp->ttr_vector = vector;
969ae115bc7Smrj #endif	/* TRAPTRACE */
970ae115bc7Smrj 	if (newipl > LOCK_LEVEL) {
971ae115bc7Smrj 		/*
972ae115bc7Smrj 		 * High priority interrupts run on this cpu's interrupt stack.
973ae115bc7Smrj 		 */
974ae115bc7Smrj 		if (hilevel_intr_prolog(cpu, newipl, oldipl, rp) == 0) {
975ae115bc7Smrj 			newsp = cpu->cpu_intr_stack;
976ae115bc7Smrj 			switch_sp_and_call(newsp, dispatch_hilevel, vector, 0);
977ae115bc7Smrj 		} else { /* already on the interrupt stack */
978ae115bc7Smrj 			dispatch_hilevel(vector, 0);
979ae115bc7Smrj 		}
980ae115bc7Smrj 		(void) hilevel_intr_epilog(cpu, newipl, oldipl, vector);
981ae115bc7Smrj 	} else {
982ae115bc7Smrj 		/*
983ae115bc7Smrj 		 * Run this interrupt in a separate thread.
984ae115bc7Smrj 		 */
985ae115bc7Smrj 		newsp = intr_thread_prolog(cpu, (caddr_t)rp, newipl);
986ae115bc7Smrj 		switch_sp_and_call(newsp, dispatch_hardint, vector, oldipl);
987ae115bc7Smrj 	}
988ae115bc7Smrj 
989349b53ddSStuart Maybee #if !defined(__xpv)
990ae115bc7Smrj 	/*
991ae115bc7Smrj 	 * Deliver any pending soft interrupts.
992ae115bc7Smrj 	 */
993ae115bc7Smrj 	if (cpu->cpu_softinfo.st_pending)
994ae115bc7Smrj 		dosoftint(rp);
995349b53ddSStuart Maybee #endif	/* !__xpv */
996ae115bc7Smrj }
997ae115bc7Smrj 
998349b53ddSStuart Maybee 
999ae115bc7Smrj /*
1000ae115bc7Smrj  * Common tasks always done by _sys_rtt, called with interrupts disabled.
1001ae115bc7Smrj  * Returns 1 if returning to userland, 0 if returning to system mode.
1002ae115bc7Smrj  */
1003ae115bc7Smrj int
1004ae115bc7Smrj sys_rtt_common(struct regs *rp)
1005ae115bc7Smrj {
1006ae115bc7Smrj 	kthread_t *tp;
1007ae115bc7Smrj 	extern void mutex_exit_critical_start();
1008ae115bc7Smrj 	extern long mutex_exit_critical_size;
1009575a7426Spt157919 	extern void mutex_owner_running_critical_start();
1010575a7426Spt157919 	extern long mutex_owner_running_critical_size;
1011ae115bc7Smrj 
1012ae115bc7Smrj loop:
1013ae115bc7Smrj 
1014ae115bc7Smrj 	/*
1015ae115bc7Smrj 	 * Check if returning to user
1016ae115bc7Smrj 	 */
1017ae115bc7Smrj 	tp = CPU->cpu_thread;
1018ae115bc7Smrj 	if (USERMODE(rp->r_cs)) {
1019ae115bc7Smrj 		/*
1020ae115bc7Smrj 		 * Check if AST pending.
1021ae115bc7Smrj 		 */
1022ae115bc7Smrj 		if (tp->t_astflag) {
1023ae115bc7Smrj 			/*
1024ae115bc7Smrj 			 * Let trap() handle the AST
1025ae115bc7Smrj 			 */
1026ae115bc7Smrj 			sti();
1027ae115bc7Smrj 			rp->r_trapno = T_AST;
1028ae115bc7Smrj 			trap(rp, (caddr_t)0, CPU->cpu_id);
1029ae115bc7Smrj 			cli();
1030ae115bc7Smrj 			goto loop;
1031ae115bc7Smrj 		}
1032ae115bc7Smrj 
1033ae115bc7Smrj #if defined(__amd64)
1034ae115bc7Smrj 		/*
1035ae115bc7Smrj 		 * We are done if segment registers do not need updating.
1036ae115bc7Smrj 		 */
10377712e92cSsudheer 		if (tp->t_lwp->lwp_pcb.pcb_rupdate == 0)
1038ae115bc7Smrj 			return (1);
1039ae115bc7Smrj 
1040ae115bc7Smrj 		if (update_sregs(rp, tp->t_lwp)) {
1041ae115bc7Smrj 			/*
1042ae115bc7Smrj 			 * 1 or more of the selectors is bad.
1043ae115bc7Smrj 			 * Deliver a SIGSEGV.
1044ae115bc7Smrj 			 */
1045ae115bc7Smrj 			proc_t *p = ttoproc(tp);
1046ae115bc7Smrj 
1047ae115bc7Smrj 			sti();
1048ae115bc7Smrj 			mutex_enter(&p->p_lock);
1049ae115bc7Smrj 			tp->t_lwp->lwp_cursig = SIGSEGV;
1050ae115bc7Smrj 			mutex_exit(&p->p_lock);
1051ae115bc7Smrj 			psig();
1052ae115bc7Smrj 			tp->t_sig_check = 1;
1053ae115bc7Smrj 			cli();
1054ae115bc7Smrj 		}
10557712e92cSsudheer 		tp->t_lwp->lwp_pcb.pcb_rupdate = 0;
1056ae115bc7Smrj 
1057ae115bc7Smrj #endif	/* __amd64 */
1058ae115bc7Smrj 		return (1);
1059ae115bc7Smrj 	}
1060ae115bc7Smrj 
1061ae115bc7Smrj 	/*
1062ae115bc7Smrj 	 * Here if we are returning to supervisor mode.
1063ae115bc7Smrj 	 * Check for a kernel preemption request.
1064ae115bc7Smrj 	 */
1065ae115bc7Smrj 	if (CPU->cpu_kprunrun && (rp->r_ps & PS_IE)) {
1066ae115bc7Smrj 
1067ae115bc7Smrj 		/*
1068ae115bc7Smrj 		 * Do nothing if already in kpreempt
1069ae115bc7Smrj 		 */
1070ae115bc7Smrj 		if (!tp->t_preempt_lk) {
1071ae115bc7Smrj 			tp->t_preempt_lk = 1;
1072ae115bc7Smrj 			sti();
1073ae115bc7Smrj 			kpreempt(1); /* asynchronous kpreempt call */
1074ae115bc7Smrj 			cli();
1075ae115bc7Smrj 			tp->t_preempt_lk = 0;
1076ae115bc7Smrj 		}
1077ae115bc7Smrj 	}
1078ae115bc7Smrj 
1079ae115bc7Smrj 	/*
1080ae115bc7Smrj 	 * If we interrupted the mutex_exit() critical region we must
1081ae115bc7Smrj 	 * reset the PC back to the beginning to prevent missed wakeups
1082ae115bc7Smrj 	 * See the comments in mutex_exit() for details.
1083ae115bc7Smrj 	 */
1084ae115bc7Smrj 	if ((uintptr_t)rp->r_pc - (uintptr_t)mutex_exit_critical_start <
1085ae115bc7Smrj 	    mutex_exit_critical_size) {
1086ae115bc7Smrj 		rp->r_pc = (greg_t)mutex_exit_critical_start;
1087ae115bc7Smrj 	}
1088575a7426Spt157919 
1089575a7426Spt157919 	/*
1090575a7426Spt157919 	 * If we interrupted the mutex_owner_running() critical region we
1091575a7426Spt157919 	 * must reset the PC back to the beginning to prevent dereferencing
1092575a7426Spt157919 	 * of a freed thread pointer. See the comments in mutex_owner_running
1093575a7426Spt157919 	 * for details.
1094575a7426Spt157919 	 */
1095575a7426Spt157919 	if ((uintptr_t)rp->r_pc -
1096575a7426Spt157919 	    (uintptr_t)mutex_owner_running_critical_start <
1097575a7426Spt157919 	    mutex_owner_running_critical_size) {
1098575a7426Spt157919 		rp->r_pc = (greg_t)mutex_owner_running_critical_start;
1099575a7426Spt157919 	}
1100575a7426Spt157919 
1101ae115bc7Smrj 	return (0);
1102ae115bc7Smrj }
1103ae115bc7Smrj 
1104ae115bc7Smrj void
1105ae115bc7Smrj send_dirint(int cpuid, int int_level)
1106ae115bc7Smrj {
1107ae115bc7Smrj 	(*send_dirintf)(cpuid, int_level);
1108ae115bc7Smrj }
1109ae115bc7Smrj 
1110*7ff178cdSJimmy Vetayases #define	IS_FAKE_SOFTINT(flag, newpri)		\
1111*7ff178cdSJimmy Vetayases 	(((flag) & PS_IE) &&				\
1112*7ff178cdSJimmy Vetayases 	    (((*get_pending_spl)() > (newpri)) ||	\
1113*7ff178cdSJimmy Vetayases 	    bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > (newpri)))
1114*7ff178cdSJimmy Vetayases 
1115ae115bc7Smrj /*
1116ae115bc7Smrj  * do_splx routine, takes new ipl to set
1117ae115bc7Smrj  * returns the old ipl.
1118ae115bc7Smrj  * We are careful not to set priority lower than CPU->cpu_base_pri,
1119ae115bc7Smrj  * even though it seems we're raising the priority, it could be set
1120ae115bc7Smrj  * higher at any time by an interrupt routine, so we must block interrupts
1121ae115bc7Smrj  * and look at CPU->cpu_base_pri
1122ae115bc7Smrj  */
1123ae115bc7Smrj int
1124ae115bc7Smrj do_splx(int newpri)
1125ae115bc7Smrj {
1126ae115bc7Smrj 	ulong_t	flag;
1127ae115bc7Smrj 	cpu_t	*cpu;
1128ae115bc7Smrj 	int	curpri, basepri;
1129ae115bc7Smrj 
1130ae115bc7Smrj 	flag = intr_clear();
1131ae115bc7Smrj 	cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
1132ae115bc7Smrj 	curpri = cpu->cpu_m.mcpu_pri;
1133ae115bc7Smrj 	basepri = cpu->cpu_base_spl;
1134ae115bc7Smrj 	if (newpri < basepri)
1135ae115bc7Smrj 		newpri = basepri;
1136ae115bc7Smrj 	cpu->cpu_m.mcpu_pri = newpri;
1137ae115bc7Smrj 	(*setspl)(newpri);
1138ae115bc7Smrj 	/*
1139ae115bc7Smrj 	 * If we are going to reenable interrupts see if new priority level
1140ae115bc7Smrj 	 * allows pending softint delivery.
1141ae115bc7Smrj 	 */
1142*7ff178cdSJimmy Vetayases 	if (IS_FAKE_SOFTINT(flag, newpri))
1143ae115bc7Smrj 		fakesoftint();
1144ae115bc7Smrj 	ASSERT(!interrupts_enabled());
1145ae115bc7Smrj 	intr_restore(flag);
1146ae115bc7Smrj 	return (curpri);
1147ae115bc7Smrj }
1148ae115bc7Smrj 
1149ae115bc7Smrj /*
1150ae115bc7Smrj  * Common spl raise routine, takes new ipl to set
1151ae115bc7Smrj  * returns the old ipl, will not lower ipl.
1152ae115bc7Smrj  */
1153ae115bc7Smrj int
1154ae115bc7Smrj splr(int newpri)
1155ae115bc7Smrj {
1156ae115bc7Smrj 	ulong_t	flag;
1157ae115bc7Smrj 	cpu_t	*cpu;
1158ae115bc7Smrj 	int	curpri, basepri;
1159ae115bc7Smrj 
1160ae115bc7Smrj 	flag = intr_clear();
1161ae115bc7Smrj 	cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
1162ae115bc7Smrj 	curpri = cpu->cpu_m.mcpu_pri;
1163ae115bc7Smrj 	/*
1164ae115bc7Smrj 	 * Only do something if new priority is larger
1165ae115bc7Smrj 	 */
1166ae115bc7Smrj 	if (newpri > curpri) {
1167ae115bc7Smrj 		basepri = cpu->cpu_base_spl;
1168ae115bc7Smrj 		if (newpri < basepri)
1169ae115bc7Smrj 			newpri = basepri;
1170ae115bc7Smrj 		cpu->cpu_m.mcpu_pri = newpri;
1171ae115bc7Smrj 		(*setspl)(newpri);
1172ae115bc7Smrj 		/*
1173ae115bc7Smrj 		 * See if new priority level allows pending softint delivery
1174ae115bc7Smrj 		 */
1175*7ff178cdSJimmy Vetayases 		if (IS_FAKE_SOFTINT(flag, newpri))
1176ae115bc7Smrj 			fakesoftint();
1177ae115bc7Smrj 	}
1178ae115bc7Smrj 	intr_restore(flag);
1179ae115bc7Smrj 	return (curpri);
1180ae115bc7Smrj }
1181ae115bc7Smrj 
1182ae115bc7Smrj int
1183ae115bc7Smrj getpil(void)
1184ae115bc7Smrj {
1185ae115bc7Smrj 	return (CPU->cpu_m.mcpu_pri);
1186ae115bc7Smrj }
1187ae115bc7Smrj 
1188ae115bc7Smrj int
1189b885580bSAlexander Kolbasov spl_xcall(void)
1190b885580bSAlexander Kolbasov {
1191b885580bSAlexander Kolbasov 	return (splr(ipltospl(XCALL_PIL)));
1192b885580bSAlexander Kolbasov }
1193b885580bSAlexander Kolbasov 
1194b885580bSAlexander Kolbasov int
1195ae115bc7Smrj interrupts_enabled(void)
1196ae115bc7Smrj {
1197ae115bc7Smrj 	ulong_t	flag;
1198ae115bc7Smrj 
1199ae115bc7Smrj 	flag = getflags();
1200ae115bc7Smrj 	return ((flag & PS_IE) == PS_IE);
1201ae115bc7Smrj }
1202ae115bc7Smrj 
1203ae115bc7Smrj #ifdef DEBUG
1204ae115bc7Smrj void
1205ae115bc7Smrj assert_ints_enabled(void)
1206ae115bc7Smrj {
1207ae115bc7Smrj 	ASSERT(!interrupts_unleashed || interrupts_enabled());
1208ae115bc7Smrj }
1209ae115bc7Smrj #endif	/* DEBUG */
1210