10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51455Sandrei * Common Development and Distribution License (the "License"). 61455Sandrei * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 215084Sjohnlev 220Sstevel@tonic-gate /* 23*8803SJonathan.Haslam@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include <sys/cpuvar.h> 280Sstevel@tonic-gate #include <sys/regset.h> 290Sstevel@tonic-gate #include <sys/psw.h> 300Sstevel@tonic-gate #include <sys/types.h> 310Sstevel@tonic-gate #include <sys/thread.h> 320Sstevel@tonic-gate #include <sys/systm.h> 330Sstevel@tonic-gate #include <sys/segments.h> 340Sstevel@tonic-gate #include <sys/pcb.h> 350Sstevel@tonic-gate #include <sys/trap.h> 360Sstevel@tonic-gate #include <sys/ftrace.h> 370Sstevel@tonic-gate #include <sys/traptrace.h> 380Sstevel@tonic-gate #include <sys/clock.h> 390Sstevel@tonic-gate #include <sys/panic.h> 400Sstevel@tonic-gate #include <sys/disp.h> 410Sstevel@tonic-gate #include <vm/seg_kp.h> 420Sstevel@tonic-gate #include <sys/stack.h> 430Sstevel@tonic-gate #include <sys/sysmacros.h> 440Sstevel@tonic-gate #include <sys/cmn_err.h> 450Sstevel@tonic-gate #include <sys/kstat.h> 460Sstevel@tonic-gate #include <sys/smp_impldefs.h> 470Sstevel@tonic-gate #include <sys/pool_pset.h> 480Sstevel@tonic-gate #include <sys/zone.h> 490Sstevel@tonic-gate #include <sys/bitmap.h> 503446Smrj #include <sys/archsystm.h> 513446Smrj #include <sys/machsystm.h> 523446Smrj #include <sys/ontrap.h> 533446Smrj #include <sys/x86_archext.h> 543446Smrj #include <sys/promif.h> 554191Sjosephb #include <vm/hat_i86.h> 565084Sjohnlev #if defined(__xpv) 575084Sjohnlev #include <sys/hypervisor.h> 585084Sjohnlev #endif 590Sstevel@tonic-gate 600Sstevel@tonic-gate 615084Sjohnlev #if defined(__xpv) && defined(DEBUG) 625084Sjohnlev 635084Sjohnlev /* 645084Sjohnlev * This panic message is intended as an aid to interrupt debugging. 655084Sjohnlev * 665084Sjohnlev * The associated assertion tests the condition of enabling 675084Sjohnlev * events when events are already enabled. The implication 685084Sjohnlev * being that whatever code the programmer thought was 695084Sjohnlev * protected by having events disabled until the second 705084Sjohnlev * enable happened really wasn't protected at all .. 715084Sjohnlev */ 725084Sjohnlev 735084Sjohnlev int stistipanic = 1; /* controls the debug panic check */ 745084Sjohnlev const char *stistimsg = "stisti"; 755084Sjohnlev ulong_t laststi[NCPU]; 765084Sjohnlev 775084Sjohnlev /* 785084Sjohnlev * This variable tracks the last place events were disabled on each cpu 795084Sjohnlev * it assists in debugging when asserts that interupts are enabled trip. 805084Sjohnlev */ 815084Sjohnlev ulong_t lastcli[NCPU]; 825084Sjohnlev 835084Sjohnlev #endif 845084Sjohnlev 850Sstevel@tonic-gate /* 863446Smrj * Set cpu's base SPL level to the highest active interrupt level 870Sstevel@tonic-gate */ 883446Smrj void 893446Smrj set_base_spl(void) 900Sstevel@tonic-gate { 913446Smrj struct cpu *cpu = CPU; 923446Smrj uint16_t active = (uint16_t)cpu->cpu_intr_actv; 930Sstevel@tonic-gate 943446Smrj cpu->cpu_base_spl = active == 0 ? 0 : bsrw_insn(active); 950Sstevel@tonic-gate } 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* 980Sstevel@tonic-gate * Do all the work necessary to set up the cpu and thread structures 990Sstevel@tonic-gate * to dispatch a high-level interrupt. 1000Sstevel@tonic-gate * 1010Sstevel@tonic-gate * Returns 0 if we're -not- already on the high-level interrupt stack, 1020Sstevel@tonic-gate * (and *must* switch to it), non-zero if we are already on that stack. 1030Sstevel@tonic-gate * 1040Sstevel@tonic-gate * Called with interrupts masked. 1050Sstevel@tonic-gate * The 'pil' is already set to the appropriate level for rp->r_trapno. 1060Sstevel@tonic-gate */ 1073446Smrj static int 1080Sstevel@tonic-gate hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp) 1090Sstevel@tonic-gate { 1100Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 1110Sstevel@tonic-gate uint_t mask; 112590Sesolom hrtime_t intrtime; 1133446Smrj hrtime_t now = tsc_read(); 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate ASSERT(pil > LOCK_LEVEL); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate if (pil == CBE_HIGH_PIL) { 1180Sstevel@tonic-gate cpu->cpu_profile_pil = oldpil; 1190Sstevel@tonic-gate if (USERMODE(rp->r_cs)) { 1200Sstevel@tonic-gate cpu->cpu_profile_pc = 0; 1210Sstevel@tonic-gate cpu->cpu_profile_upc = rp->r_pc; 122*8803SJonathan.Haslam@Sun.COM cpu->cpu_cpcprofile_pc = 0; 123*8803SJonathan.Haslam@Sun.COM cpu->cpu_cpcprofile_upc = rp->r_pc; 1240Sstevel@tonic-gate } else { 1250Sstevel@tonic-gate cpu->cpu_profile_pc = rp->r_pc; 1260Sstevel@tonic-gate cpu->cpu_profile_upc = 0; 127*8803SJonathan.Haslam@Sun.COM cpu->cpu_cpcprofile_pc = rp->r_pc; 128*8803SJonathan.Haslam@Sun.COM cpu->cpu_cpcprofile_upc = 0; 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK; 1330Sstevel@tonic-gate if (mask != 0) { 1340Sstevel@tonic-gate int nestpil; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* 1370Sstevel@tonic-gate * We have interrupted another high-level interrupt. 1380Sstevel@tonic-gate * Load starting timestamp, compute interval, update 1390Sstevel@tonic-gate * cumulative counter. 1400Sstevel@tonic-gate */ 1410Sstevel@tonic-gate nestpil = bsrw_insn((uint16_t)mask); 1420Sstevel@tonic-gate ASSERT(nestpil < pil); 1433446Smrj intrtime = now - 1440Sstevel@tonic-gate mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)]; 145916Sschwartz mcpu->intrstat[nestpil][0] += intrtime; 146590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 1470Sstevel@tonic-gate /* 1480Sstevel@tonic-gate * Another high-level interrupt is active below this one, so 1490Sstevel@tonic-gate * there is no need to check for an interrupt thread. That 1500Sstevel@tonic-gate * will be done by the lowest priority high-level interrupt 1510Sstevel@tonic-gate * active. 1520Sstevel@tonic-gate */ 1530Sstevel@tonic-gate } else { 1540Sstevel@tonic-gate kthread_t *t = cpu->cpu_thread; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate /* 1570Sstevel@tonic-gate * See if we are interrupting a low-level interrupt thread. 1580Sstevel@tonic-gate * If so, account for its time slice only if its time stamp 1590Sstevel@tonic-gate * is non-zero. 1600Sstevel@tonic-gate */ 1610Sstevel@tonic-gate if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) { 1623446Smrj intrtime = now - t->t_intr_start; 163916Sschwartz mcpu->intrstat[t->t_pil][0] += intrtime; 164590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 1650Sstevel@tonic-gate t->t_intr_start = 0; 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate /* 1700Sstevel@tonic-gate * Store starting timestamp in CPU structure for this PIL. 1710Sstevel@tonic-gate */ 1723446Smrj mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = now; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate if (pil == 15) { 1770Sstevel@tonic-gate /* 1780Sstevel@tonic-gate * To support reentrant level 15 interrupts, we maintain a 1790Sstevel@tonic-gate * recursion count in the top half of cpu_intr_actv. Only 1800Sstevel@tonic-gate * when this count hits zero do we clear the PIL 15 bit from 1810Sstevel@tonic-gate * the lower half of cpu_intr_actv. 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1; 1840Sstevel@tonic-gate (*refcntp)++; 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate mask = cpu->cpu_intr_actv; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil); 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK); 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate /* 1950Sstevel@tonic-gate * Does most of the work of returning from a high level interrupt. 1960Sstevel@tonic-gate * 1970Sstevel@tonic-gate * Returns 0 if there are no more high level interrupts (in which 1980Sstevel@tonic-gate * case we must switch back to the interrupted thread stack) or 1990Sstevel@tonic-gate * non-zero if there are more (in which case we should stay on it). 2000Sstevel@tonic-gate * 2010Sstevel@tonic-gate * Called with interrupts masked 2020Sstevel@tonic-gate */ 2033446Smrj static int 2040Sstevel@tonic-gate hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum) 2050Sstevel@tonic-gate { 2060Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 2070Sstevel@tonic-gate uint_t mask; 208590Sesolom hrtime_t intrtime; 2093446Smrj hrtime_t now = tsc_read(); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate ASSERT(mcpu->mcpu_pri == pil); 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++; 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil)); 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate if (pil == 15) { 2180Sstevel@tonic-gate /* 2190Sstevel@tonic-gate * To support reentrant level 15 interrupts, we maintain a 2200Sstevel@tonic-gate * recursion count in the top half of cpu_intr_actv. Only 2210Sstevel@tonic-gate * when this count hits zero do we clear the PIL 15 bit from 2220Sstevel@tonic-gate * the lower half of cpu_intr_actv. 2230Sstevel@tonic-gate */ 2240Sstevel@tonic-gate uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1; 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate ASSERT(*refcntp > 0); 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate if (--(*refcntp) == 0) 2290Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 2300Sstevel@tonic-gate } else { 2310Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0); 2350Sstevel@tonic-gate 2363446Smrj intrtime = now - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)]; 237916Sschwartz mcpu->intrstat[pil][0] += intrtime; 238590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate /* 2410Sstevel@tonic-gate * Check for lower-pil nested high-level interrupt beneath 2420Sstevel@tonic-gate * current one. If so, place a starting timestamp in its 2430Sstevel@tonic-gate * pil_high_start entry. 2440Sstevel@tonic-gate */ 2450Sstevel@tonic-gate mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK; 2460Sstevel@tonic-gate if (mask != 0) { 2470Sstevel@tonic-gate int nestpil; 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* 2500Sstevel@tonic-gate * find PIL of nested interrupt 2510Sstevel@tonic-gate */ 2520Sstevel@tonic-gate nestpil = bsrw_insn((uint16_t)mask); 2530Sstevel@tonic-gate ASSERT(nestpil < pil); 2543446Smrj mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = now; 2550Sstevel@tonic-gate /* 2560Sstevel@tonic-gate * (Another high-level interrupt is active below this one, 2570Sstevel@tonic-gate * so there is no need to check for an interrupt 2580Sstevel@tonic-gate * thread. That will be done by the lowest priority 2590Sstevel@tonic-gate * high-level interrupt active.) 2600Sstevel@tonic-gate */ 2610Sstevel@tonic-gate } else { 2620Sstevel@tonic-gate /* 2630Sstevel@tonic-gate * Check to see if there is a low-level interrupt active. 2640Sstevel@tonic-gate * If so, place a starting timestamp in the thread 2650Sstevel@tonic-gate * structure. 2660Sstevel@tonic-gate */ 2670Sstevel@tonic-gate kthread_t *t = cpu->cpu_thread; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate if (t->t_flag & T_INTR_THREAD) 2703446Smrj t->t_intr_start = now; 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate mcpu->mcpu_pri = oldpil; 2740Sstevel@tonic-gate (void) (*setlvlx)(oldpil, vecnum); 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate /* 2800Sstevel@tonic-gate * Set up the cpu, thread and interrupt thread structures for 2810Sstevel@tonic-gate * executing an interrupt thread. The new stack pointer of the 2820Sstevel@tonic-gate * interrupt thread (which *must* be switched to) is returned. 2830Sstevel@tonic-gate */ 2843446Smrj static caddr_t 2850Sstevel@tonic-gate intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil) 2860Sstevel@tonic-gate { 2870Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 2880Sstevel@tonic-gate kthread_t *t, *volatile it; 2893446Smrj hrtime_t now = tsc_read(); 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate ASSERT(pil > 0); 2920Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0); 2930Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil); 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate /* 2960Sstevel@tonic-gate * Get set to run an interrupt thread. 2970Sstevel@tonic-gate * There should always be an interrupt thread, since we 2980Sstevel@tonic-gate * allocate one for each level on each CPU. 2990Sstevel@tonic-gate * 300989Sesolom * t_intr_start could be zero due to cpu_intr_swtch_enter. 3010Sstevel@tonic-gate */ 3020Sstevel@tonic-gate t = cpu->cpu_thread; 303989Sesolom if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) { 3043446Smrj hrtime_t intrtime = now - t->t_intr_start; 305916Sschwartz mcpu->intrstat[t->t_pil][0] += intrtime; 306590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 3070Sstevel@tonic-gate t->t_intr_start = 0; 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr); 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate t->t_sp = (uintptr_t)stackptr; /* mark stack in curthread for resume */ 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate /* 3150Sstevel@tonic-gate * unlink the interrupt thread off the cpu 316989Sesolom * 317989Sesolom * Note that the code in kcpc_overflow_intr -relies- on the 318989Sesolom * ordering of events here - in particular that t->t_lwp of 319989Sesolom * the interrupt thread is set to the pinned thread *before* 320989Sesolom * curthread is changed. 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate it = cpu->cpu_intr_thread; 3230Sstevel@tonic-gate cpu->cpu_intr_thread = it->t_link; 3240Sstevel@tonic-gate it->t_intr = t; 3250Sstevel@tonic-gate it->t_lwp = t->t_lwp; 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate /* 3280Sstevel@tonic-gate * (threads on the interrupt thread free list could have state 3290Sstevel@tonic-gate * preset to TS_ONPROC, but it helps in debugging if 3300Sstevel@tonic-gate * they're TS_FREE.) 3310Sstevel@tonic-gate */ 3320Sstevel@tonic-gate it->t_state = TS_ONPROC; 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate cpu->cpu_thread = it; /* new curthread on this cpu */ 3350Sstevel@tonic-gate it->t_pil = (uchar_t)pil; 3360Sstevel@tonic-gate it->t_pri = intr_pri + (pri_t)pil; 3373446Smrj it->t_intr_start = now; 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate return (it->t_stk); 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate #ifdef DEBUG 3440Sstevel@tonic-gate int intr_thread_cnt; 3450Sstevel@tonic-gate #endif 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate /* 3480Sstevel@tonic-gate * Called with interrupts disabled 3490Sstevel@tonic-gate */ 3503446Smrj static void 3510Sstevel@tonic-gate intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil) 3520Sstevel@tonic-gate { 3530Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 3540Sstevel@tonic-gate kthread_t *t; 3550Sstevel@tonic-gate kthread_t *it = cpu->cpu_thread; /* curthread */ 3560Sstevel@tonic-gate uint_t pil, basespl; 357590Sesolom hrtime_t intrtime; 3583446Smrj hrtime_t now = tsc_read(); 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate pil = it->t_pil; 3610Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++; 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate ASSERT(it->t_intr_start != 0); 3643446Smrj intrtime = now - it->t_intr_start; 365916Sschwartz mcpu->intrstat[pil][0] += intrtime; 366590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil)); 3690Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * If there is still an interrupted thread underneath this one 3730Sstevel@tonic-gate * then the interrupt was never blocked and the return is 3740Sstevel@tonic-gate * fairly simple. Otherwise it isn't. 3750Sstevel@tonic-gate */ 3760Sstevel@tonic-gate if ((t = it->t_intr) == NULL) { 3770Sstevel@tonic-gate /* 3780Sstevel@tonic-gate * The interrupted thread is no longer pinned underneath 3790Sstevel@tonic-gate * the interrupt thread. This means the interrupt must 3800Sstevel@tonic-gate * have blocked, and the interrupted thread has been 3810Sstevel@tonic-gate * unpinned, and has probably been running around the 3820Sstevel@tonic-gate * system for a while. 3830Sstevel@tonic-gate * 3840Sstevel@tonic-gate * Since there is no longer a thread under this one, put 3850Sstevel@tonic-gate * this interrupt thread back on the CPU's free list and 3860Sstevel@tonic-gate * resume the idle thread which will dispatch the next 3870Sstevel@tonic-gate * thread to run. 3880Sstevel@tonic-gate */ 3890Sstevel@tonic-gate #ifdef DEBUG 3900Sstevel@tonic-gate intr_thread_cnt++; 3910Sstevel@tonic-gate #endif 3920Sstevel@tonic-gate cpu->cpu_stats.sys.intrblk++; 3930Sstevel@tonic-gate /* 3940Sstevel@tonic-gate * Set CPU's base SPL based on active interrupts bitmask 3950Sstevel@tonic-gate */ 3960Sstevel@tonic-gate set_base_spl(); 3970Sstevel@tonic-gate basespl = cpu->cpu_base_spl; 3980Sstevel@tonic-gate mcpu->mcpu_pri = basespl; 3990Sstevel@tonic-gate (*setlvlx)(basespl, vec); 4000Sstevel@tonic-gate (void) splhigh(); 4013446Smrj sti(); 4020Sstevel@tonic-gate it->t_state = TS_FREE; 4030Sstevel@tonic-gate /* 4040Sstevel@tonic-gate * Return interrupt thread to pool 4050Sstevel@tonic-gate */ 4060Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 4070Sstevel@tonic-gate cpu->cpu_intr_thread = it; 4080Sstevel@tonic-gate swtch(); 4093446Smrj panic("intr_thread_epilog: swtch returned"); 4100Sstevel@tonic-gate /*NOTREACHED*/ 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate /* 4140Sstevel@tonic-gate * Return interrupt thread to the pool 4150Sstevel@tonic-gate */ 4160Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 4170Sstevel@tonic-gate cpu->cpu_intr_thread = it; 4180Sstevel@tonic-gate it->t_state = TS_FREE; 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate basespl = cpu->cpu_base_spl; 4210Sstevel@tonic-gate pil = MAX(oldpil, basespl); 4220Sstevel@tonic-gate mcpu->mcpu_pri = pil; 4230Sstevel@tonic-gate (*setlvlx)(pil, vec); 4243446Smrj t->t_intr_start = now; 4250Sstevel@tonic-gate cpu->cpu_thread = t; 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate 428916Sschwartz /* 4293446Smrj * intr_get_time() is a resource for interrupt handlers to determine how 4303446Smrj * much time has been spent handling the current interrupt. Such a function 4313446Smrj * is needed because higher level interrupts can arrive during the 4323446Smrj * processing of an interrupt. intr_get_time() only returns time spent in the 4333446Smrj * current interrupt handler. 4343446Smrj * 4353446Smrj * The caller must be calling from an interrupt handler running at a pil 4363446Smrj * below or at lock level. Timings are not provided for high-level 4373446Smrj * interrupts. 4383446Smrj * 4393446Smrj * The first time intr_get_time() is called while handling an interrupt, 4403446Smrj * it returns the time since the interrupt handler was invoked. Subsequent 4413446Smrj * calls will return the time since the prior call to intr_get_time(). Time 4425084Sjohnlev * is returned as ticks. Use scalehrtimef() to convert ticks to nsec. 4433446Smrj * 4443446Smrj * Theory Of Intrstat[][]: 4453446Smrj * 4463446Smrj * uint64_t intrstat[pil][0..1] is an array indexed by pil level, with two 4473446Smrj * uint64_ts per pil. 4483446Smrj * 4493446Smrj * intrstat[pil][0] is a cumulative count of the number of ticks spent 4503446Smrj * handling all interrupts at the specified pil on this CPU. It is 4513446Smrj * exported via kstats to the user. 4523446Smrj * 4533446Smrj * intrstat[pil][1] is always a count of ticks less than or equal to the 4543446Smrj * value in [0]. The difference between [1] and [0] is the value returned 4553446Smrj * by a call to intr_get_time(). At the start of interrupt processing, 4563446Smrj * [0] and [1] will be equal (or nearly so). As the interrupt consumes 4573446Smrj * time, [0] will increase, but [1] will remain the same. A call to 4583446Smrj * intr_get_time() will return the difference, then update [1] to be the 4593446Smrj * same as [0]. Future calls will return the time since the last call. 4603446Smrj * Finally, when the interrupt completes, [1] is updated to the same as [0]. 4613446Smrj * 4623446Smrj * Implementation: 4633446Smrj * 4643446Smrj * intr_get_time() works much like a higher level interrupt arriving. It 4653446Smrj * "checkpoints" the timing information by incrementing intrstat[pil][0] 4663446Smrj * to include elapsed running time, and by setting t_intr_start to rdtsc. 4673446Smrj * It then sets the return value to intrstat[pil][0] - intrstat[pil][1], 4683446Smrj * and updates intrstat[pil][1] to be the same as the new value of 4693446Smrj * intrstat[pil][0]. 4703446Smrj * 4713446Smrj * In the normal handling of interrupts, after an interrupt handler returns 4723446Smrj * and the code in intr_thread() updates intrstat[pil][0], it then sets 4733446Smrj * intrstat[pil][1] to the new value of intrstat[pil][0]. When [0] == [1], 4743446Smrj * the timings are reset, i.e. intr_get_time() will return [0] - [1] which 4753446Smrj * is 0. 4763446Smrj * 4773446Smrj * Whenever interrupts arrive on a CPU which is handling a lower pil 4783446Smrj * interrupt, they update the lower pil's [0] to show time spent in the 4793446Smrj * handler that they've interrupted. This results in a growing discrepancy 4803446Smrj * between [0] and [1], which is returned the next time intr_get_time() is 4813446Smrj * called. Time spent in the higher-pil interrupt will not be returned in 4823446Smrj * the next intr_get_time() call from the original interrupt, because 4833446Smrj * the higher-pil interrupt's time is accumulated in intrstat[higherpil][]. 484916Sschwartz */ 485916Sschwartz uint64_t 4863446Smrj intr_get_time(void) 487916Sschwartz { 4883446Smrj struct cpu *cpu; 4893446Smrj struct machcpu *mcpu; 4903446Smrj kthread_t *t; 491916Sschwartz uint64_t time, delta, ret; 4923446Smrj uint_t pil; 493916Sschwartz 4943446Smrj cli(); 4953446Smrj cpu = CPU; 4963446Smrj mcpu = &cpu->cpu_m; 4973446Smrj t = cpu->cpu_thread; 4983446Smrj pil = t->t_pil; 499916Sschwartz ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0); 500916Sschwartz ASSERT(t->t_flag & T_INTR_THREAD); 501916Sschwartz ASSERT(pil != 0); 502916Sschwartz ASSERT(t->t_intr_start != 0); 503916Sschwartz 504916Sschwartz time = tsc_read(); 505916Sschwartz delta = time - t->t_intr_start; 506916Sschwartz t->t_intr_start = time; 507916Sschwartz 508916Sschwartz time = mcpu->intrstat[pil][0] + delta; 509916Sschwartz ret = time - mcpu->intrstat[pil][1]; 510916Sschwartz mcpu->intrstat[pil][0] = time; 511916Sschwartz mcpu->intrstat[pil][1] = time; 5121887Sjhaslam cpu->cpu_intracct[cpu->cpu_mstate] += delta; 513916Sschwartz 5143446Smrj sti(); 515916Sschwartz return (ret); 516916Sschwartz } 517916Sschwartz 5183446Smrj static caddr_t 5190Sstevel@tonic-gate dosoftint_prolog( 5200Sstevel@tonic-gate struct cpu *cpu, 5210Sstevel@tonic-gate caddr_t stackptr, 5220Sstevel@tonic-gate uint32_t st_pending, 5230Sstevel@tonic-gate uint_t oldpil) 5240Sstevel@tonic-gate { 5250Sstevel@tonic-gate kthread_t *t, *volatile it; 5260Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 5270Sstevel@tonic-gate uint_t pil; 5283446Smrj hrtime_t now; 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate top: 5310Sstevel@tonic-gate ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending); 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate pil = bsrw_insn((uint16_t)st_pending); 5340Sstevel@tonic-gate if (pil <= oldpil || pil <= cpu->cpu_base_spl) 5350Sstevel@tonic-gate return (0); 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate /* 5380Sstevel@tonic-gate * XX64 Sigh. 5390Sstevel@tonic-gate * 5400Sstevel@tonic-gate * This is a transliteration of the i386 assembler code for 5410Sstevel@tonic-gate * soft interrupts. One question is "why does this need 5420Sstevel@tonic-gate * to be atomic?" One possible race is -other- processors 5430Sstevel@tonic-gate * posting soft interrupts to us in set_pending() i.e. the 5440Sstevel@tonic-gate * CPU might get preempted just after the address computation, 5450Sstevel@tonic-gate * but just before the atomic transaction, so another CPU would 5460Sstevel@tonic-gate * actually set the original CPU's st_pending bit. However, 5470Sstevel@tonic-gate * it looks like it would be simpler to disable preemption there. 5480Sstevel@tonic-gate * Are there other races for which preemption control doesn't work? 5490Sstevel@tonic-gate * 5500Sstevel@tonic-gate * The i386 assembler version -also- checks to see if the bit 5510Sstevel@tonic-gate * being cleared was actually set; if it wasn't, it rechecks 5520Sstevel@tonic-gate * for more. This seems a bit strange, as the only code that 5530Sstevel@tonic-gate * ever clears the bit is -this- code running with interrupts 5540Sstevel@tonic-gate * disabled on -this- CPU. This code would probably be cheaper: 5550Sstevel@tonic-gate * 5560Sstevel@tonic-gate * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending, 5570Sstevel@tonic-gate * ~(1 << pil)); 5580Sstevel@tonic-gate * 5590Sstevel@tonic-gate * and t->t_preempt--/++ around set_pending() even cheaper, 5600Sstevel@tonic-gate * but at this point, correctness is critical, so we slavishly 5610Sstevel@tonic-gate * emulate the i386 port. 5620Sstevel@tonic-gate */ 5633446Smrj if (atomic_btr32((uint32_t *) 5643446Smrj &mcpu->mcpu_softinfo.st_pending, pil) == 0) { 5650Sstevel@tonic-gate st_pending = mcpu->mcpu_softinfo.st_pending; 5660Sstevel@tonic-gate goto top; 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate mcpu->mcpu_pri = pil; 5700Sstevel@tonic-gate (*setspl)(pil); 5710Sstevel@tonic-gate 5723446Smrj now = tsc_read(); 5733446Smrj 5740Sstevel@tonic-gate /* 5750Sstevel@tonic-gate * Get set to run interrupt thread. 5760Sstevel@tonic-gate * There should always be an interrupt thread since we 5770Sstevel@tonic-gate * allocate one for each level on the CPU. 5780Sstevel@tonic-gate */ 5790Sstevel@tonic-gate it = cpu->cpu_intr_thread; 5800Sstevel@tonic-gate cpu->cpu_intr_thread = it->t_link; 5810Sstevel@tonic-gate 582989Sesolom /* t_intr_start could be zero due to cpu_intr_swtch_enter. */ 583989Sesolom t = cpu->cpu_thread; 584989Sesolom if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) { 5853446Smrj hrtime_t intrtime = now - t->t_intr_start; 586989Sesolom mcpu->intrstat[pil][0] += intrtime; 587989Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 588989Sesolom t->t_intr_start = 0; 589989Sesolom } 590989Sesolom 5910Sstevel@tonic-gate /* 5920Sstevel@tonic-gate * Note that the code in kcpc_overflow_intr -relies- on the 5930Sstevel@tonic-gate * ordering of events here - in particular that t->t_lwp of 5940Sstevel@tonic-gate * the interrupt thread is set to the pinned thread *before* 595989Sesolom * curthread is changed. 5960Sstevel@tonic-gate */ 5970Sstevel@tonic-gate it->t_lwp = t->t_lwp; 5980Sstevel@tonic-gate it->t_state = TS_ONPROC; 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate /* 6010Sstevel@tonic-gate * Push interrupted thread onto list from new thread. 6020Sstevel@tonic-gate * Set the new thread as the current one. 6030Sstevel@tonic-gate * Set interrupted thread's T_SP because if it is the idle thread, 6040Sstevel@tonic-gate * resume() may use that stack between threads. 6050Sstevel@tonic-gate */ 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr); 6080Sstevel@tonic-gate t->t_sp = (uintptr_t)stackptr; 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate it->t_intr = t; 6110Sstevel@tonic-gate cpu->cpu_thread = it; 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate /* 6140Sstevel@tonic-gate * Set bit for this pil in CPU's interrupt active bitmask. 6150Sstevel@tonic-gate */ 6160Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0); 6170Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil); 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate /* 6200Sstevel@tonic-gate * Initialize thread priority level from intr_pri 6210Sstevel@tonic-gate */ 6220Sstevel@tonic-gate it->t_pil = (uchar_t)pil; 6230Sstevel@tonic-gate it->t_pri = (pri_t)pil + intr_pri; 6243446Smrj it->t_intr_start = now; 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate return (it->t_stk); 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate 6293446Smrj static void 6300Sstevel@tonic-gate dosoftint_epilog(struct cpu *cpu, uint_t oldpil) 6310Sstevel@tonic-gate { 6320Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 6330Sstevel@tonic-gate kthread_t *t, *it; 6340Sstevel@tonic-gate uint_t pil, basespl; 635590Sesolom hrtime_t intrtime; 6363446Smrj hrtime_t now = tsc_read(); 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate it = cpu->cpu_thread; 6390Sstevel@tonic-gate pil = it->t_pil; 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++; 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil)); 6440Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 6453446Smrj intrtime = now - it->t_intr_start; 646916Sschwartz mcpu->intrstat[pil][0] += intrtime; 647590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate /* 6500Sstevel@tonic-gate * If there is still an interrupted thread underneath this one 6510Sstevel@tonic-gate * then the interrupt was never blocked and the return is 6520Sstevel@tonic-gate * fairly simple. Otherwise it isn't. 6530Sstevel@tonic-gate */ 6540Sstevel@tonic-gate if ((t = it->t_intr) == NULL) { 6550Sstevel@tonic-gate /* 6560Sstevel@tonic-gate * Put thread back on the interrupt thread list. 6570Sstevel@tonic-gate * This was an interrupt thread, so set CPU's base SPL. 6580Sstevel@tonic-gate */ 6590Sstevel@tonic-gate set_base_spl(); 6600Sstevel@tonic-gate it->t_state = TS_FREE; 6610Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 6620Sstevel@tonic-gate cpu->cpu_intr_thread = it; 6630Sstevel@tonic-gate (void) splhigh(); 6643446Smrj sti(); 6650Sstevel@tonic-gate swtch(); 6660Sstevel@tonic-gate /*NOTREACHED*/ 6673446Smrj panic("dosoftint_epilog: swtch returned"); 6680Sstevel@tonic-gate } 6690Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 6700Sstevel@tonic-gate cpu->cpu_intr_thread = it; 6710Sstevel@tonic-gate it->t_state = TS_FREE; 6720Sstevel@tonic-gate cpu->cpu_thread = t; 6730Sstevel@tonic-gate if (t->t_flag & T_INTR_THREAD) 6743446Smrj t->t_intr_start = now; 6750Sstevel@tonic-gate basespl = cpu->cpu_base_spl; 6760Sstevel@tonic-gate pil = MAX(oldpil, basespl); 6770Sstevel@tonic-gate mcpu->mcpu_pri = pil; 6780Sstevel@tonic-gate (*setspl)(pil); 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate 6813446Smrj 6820Sstevel@tonic-gate /* 6830Sstevel@tonic-gate * Make the interrupted thread 'to' be runnable. 6840Sstevel@tonic-gate * 6850Sstevel@tonic-gate * Since t->t_sp has already been saved, t->t_pc is all 6860Sstevel@tonic-gate * that needs to be set in this function. 6870Sstevel@tonic-gate * 6880Sstevel@tonic-gate * Returns the interrupt level of the interrupt thread. 6890Sstevel@tonic-gate */ 6900Sstevel@tonic-gate int 6910Sstevel@tonic-gate intr_passivate( 6920Sstevel@tonic-gate kthread_t *it, /* interrupt thread */ 6930Sstevel@tonic-gate kthread_t *t) /* interrupted thread */ 6940Sstevel@tonic-gate { 6950Sstevel@tonic-gate extern void _sys_rtt(); 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate ASSERT(it->t_flag & T_INTR_THREAD); 6980Sstevel@tonic-gate ASSERT(SA(t->t_sp) == t->t_sp); 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate t->t_pc = (uintptr_t)_sys_rtt; 7010Sstevel@tonic-gate return (it->t_pil); 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate /* 7050Sstevel@tonic-gate * Create interrupt kstats for this CPU. 7060Sstevel@tonic-gate */ 7070Sstevel@tonic-gate void 7080Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp) 7090Sstevel@tonic-gate { 7100Sstevel@tonic-gate int i; 7110Sstevel@tonic-gate kstat_t *intr_ksp; 7120Sstevel@tonic-gate kstat_named_t *knp; 7130Sstevel@tonic-gate char name[KSTAT_STRLEN]; 7140Sstevel@tonic-gate zoneid_t zoneid; 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate if (pool_pset_enabled()) 7190Sstevel@tonic-gate zoneid = GLOBAL_ZONEID; 7200Sstevel@tonic-gate else 7210Sstevel@tonic-gate zoneid = ALL_ZONES; 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc", 7240Sstevel@tonic-gate KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid); 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate /* 7270Sstevel@tonic-gate * Initialize each PIL's named kstat 7280Sstevel@tonic-gate */ 7290Sstevel@tonic-gate if (intr_ksp != NULL) { 7300Sstevel@tonic-gate intr_ksp->ks_update = cpu_kstat_intrstat_update; 7310Sstevel@tonic-gate knp = (kstat_named_t *)intr_ksp->ks_data; 7320Sstevel@tonic-gate intr_ksp->ks_private = cp; 7330Sstevel@tonic-gate for (i = 0; i < PIL_MAX; i++) { 7340Sstevel@tonic-gate (void) snprintf(name, KSTAT_STRLEN, "level-%d-time", 7350Sstevel@tonic-gate i + 1); 7360Sstevel@tonic-gate kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64); 7370Sstevel@tonic-gate (void) snprintf(name, KSTAT_STRLEN, "level-%d-count", 7380Sstevel@tonic-gate i + 1); 7390Sstevel@tonic-gate kstat_named_init(&knp[(i * 2) + 1], name, 7400Sstevel@tonic-gate KSTAT_DATA_UINT64); 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate kstat_install(intr_ksp); 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate } 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate /* 7470Sstevel@tonic-gate * Delete interrupt kstats for this CPU. 7480Sstevel@tonic-gate */ 7490Sstevel@tonic-gate void 7500Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp) 7510Sstevel@tonic-gate { 7520Sstevel@tonic-gate kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES); 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate /* 7560Sstevel@tonic-gate * Convert interrupt statistics from CPU ticks to nanoseconds and 7570Sstevel@tonic-gate * update kstat. 7580Sstevel@tonic-gate */ 7590Sstevel@tonic-gate int 7600Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw) 7610Sstevel@tonic-gate { 7620Sstevel@tonic-gate kstat_named_t *knp = ksp->ks_data; 7630Sstevel@tonic-gate cpu_t *cpup = (cpu_t *)ksp->ks_private; 7640Sstevel@tonic-gate int i; 7650Sstevel@tonic-gate hrtime_t hrt; 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate if (rw == KSTAT_WRITE) 7680Sstevel@tonic-gate return (EACCES); 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate for (i = 0; i < PIL_MAX; i++) { 771916Sschwartz hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0]; 7725084Sjohnlev scalehrtimef(&hrt); 7730Sstevel@tonic-gate knp[i * 2].value.ui64 = (uint64_t)hrt; 7740Sstevel@tonic-gate knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i]; 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate return (0); 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate /* 7810Sstevel@tonic-gate * An interrupt thread is ending a time slice, so compute the interval it 7820Sstevel@tonic-gate * ran for and update the statistic for its PIL. 7830Sstevel@tonic-gate */ 7840Sstevel@tonic-gate void 7850Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t) 7860Sstevel@tonic-gate { 7870Sstevel@tonic-gate uint64_t interval; 7880Sstevel@tonic-gate uint64_t start; 789590Sesolom cpu_t *cpu; 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate ASSERT((t->t_flag & T_INTR_THREAD) != 0); 7920Sstevel@tonic-gate ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL); 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate /* 7950Sstevel@tonic-gate * We could be here with a zero timestamp. This could happen if: 7960Sstevel@tonic-gate * an interrupt thread which no longer has a pinned thread underneath 7970Sstevel@tonic-gate * it (i.e. it blocked at some point in its past) has finished running 7980Sstevel@tonic-gate * its handler. intr_thread() updated the interrupt statistic for its 7990Sstevel@tonic-gate * PIL and zeroed its timestamp. Since there was no pinned thread to 8000Sstevel@tonic-gate * return to, swtch() gets called and we end up here. 801590Sesolom * 802590Sesolom * Note that we use atomic ops below (cas64 and atomic_add_64), which 803590Sesolom * we don't use in the functions above, because we're not called 804590Sesolom * with interrupts blocked, but the epilog/prolog functions are. 8050Sstevel@tonic-gate */ 8060Sstevel@tonic-gate if (t->t_intr_start) { 8070Sstevel@tonic-gate do { 8080Sstevel@tonic-gate start = t->t_intr_start; 8090Sstevel@tonic-gate interval = tsc_read() - start; 8100Sstevel@tonic-gate } while (cas64(&t->t_intr_start, start, 0) != start); 811590Sesolom cpu = CPU; 812916Sschwartz cpu->cpu_m.intrstat[t->t_pil][0] += interval; 813590Sesolom 814590Sesolom atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate], 815590Sesolom interval); 8160Sstevel@tonic-gate } else 8170Sstevel@tonic-gate ASSERT(t->t_intr == NULL); 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate /* 8210Sstevel@tonic-gate * An interrupt thread is returning from swtch(). Place a starting timestamp 8220Sstevel@tonic-gate * in its thread structure. 8230Sstevel@tonic-gate */ 8240Sstevel@tonic-gate void 8250Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t) 8260Sstevel@tonic-gate { 8270Sstevel@tonic-gate uint64_t ts; 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate ASSERT((t->t_flag & T_INTR_THREAD) != 0); 8300Sstevel@tonic-gate ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL); 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate do { 8330Sstevel@tonic-gate ts = t->t_intr_start; 8340Sstevel@tonic-gate } while (cas64(&t->t_intr_start, ts, tsc_read()) != ts); 8350Sstevel@tonic-gate } 8363446Smrj 8373446Smrj /* 8383446Smrj * Dispatch a hilevel interrupt (one above LOCK_LEVEL) 8393446Smrj */ 8403446Smrj /*ARGSUSED*/ 8413446Smrj static void 8423446Smrj dispatch_hilevel(uint_t vector, uint_t arg2) 8433446Smrj { 8443446Smrj sti(); 8453446Smrj av_dispatch_autovect(vector); 8463446Smrj cli(); 8473446Smrj } 8483446Smrj 8493446Smrj /* 8503446Smrj * Dispatch a soft interrupt 8513446Smrj */ 8523446Smrj /*ARGSUSED*/ 8533446Smrj static void 8543446Smrj dispatch_softint(uint_t oldpil, uint_t arg2) 8553446Smrj { 8563446Smrj struct cpu *cpu = CPU; 8573446Smrj 8583446Smrj sti(); 8593446Smrj av_dispatch_softvect((int)cpu->cpu_thread->t_pil); 8603446Smrj cli(); 8613446Smrj 8623446Smrj /* 8633446Smrj * Must run softint_epilog() on the interrupt thread stack, since 8643446Smrj * there may not be a return from it if the interrupt thread blocked. 8653446Smrj */ 8663446Smrj dosoftint_epilog(cpu, oldpil); 8673446Smrj } 8683446Smrj 8693446Smrj /* 8703446Smrj * Dispatch a normal interrupt 8713446Smrj */ 8723446Smrj static void 8733446Smrj dispatch_hardint(uint_t vector, uint_t oldipl) 8743446Smrj { 8753446Smrj struct cpu *cpu = CPU; 8763446Smrj 8773446Smrj sti(); 8783446Smrj av_dispatch_autovect(vector); 8793446Smrj cli(); 8803446Smrj 8813446Smrj /* 8823446Smrj * Must run intr_thread_epilog() on the interrupt thread stack, since 8833446Smrj * there may not be a return from it if the interrupt thread blocked. 8843446Smrj */ 8853446Smrj intr_thread_epilog(cpu, vector, oldipl); 8863446Smrj } 8873446Smrj 8883446Smrj /* 8893446Smrj * Deliver any softints the current interrupt priority allows. 8903446Smrj * Called with interrupts disabled. 8913446Smrj */ 8923446Smrj void 8933446Smrj dosoftint(struct regs *regs) 8943446Smrj { 8953446Smrj struct cpu *cpu = CPU; 8963446Smrj int oldipl; 8973446Smrj caddr_t newsp; 8983446Smrj 8993446Smrj while (cpu->cpu_softinfo.st_pending) { 9003446Smrj oldipl = cpu->cpu_pri; 9013446Smrj newsp = dosoftint_prolog(cpu, (caddr_t)regs, 9025084Sjohnlev cpu->cpu_softinfo.st_pending, oldipl); 9033446Smrj /* 9043446Smrj * If returned stack pointer is NULL, priority is too high 9053446Smrj * to run any of the pending softints now. 9063446Smrj * Break out and they will be run later. 9073446Smrj */ 9083446Smrj if (newsp == NULL) 9093446Smrj break; 9103446Smrj switch_sp_and_call(newsp, dispatch_softint, oldipl, 0); 9113446Smrj } 9123446Smrj } 9133446Smrj 9143446Smrj /* 9153446Smrj * Interrupt service routine, called with interrupts disabled. 9163446Smrj */ 9173446Smrj /*ARGSUSED*/ 9183446Smrj void 9193446Smrj do_interrupt(struct regs *rp, trap_trace_rec_t *ttp) 9203446Smrj { 9213446Smrj struct cpu *cpu = CPU; 9223446Smrj int newipl, oldipl = cpu->cpu_pri; 9233446Smrj uint_t vector; 9243446Smrj caddr_t newsp; 9253446Smrj 9263446Smrj #ifdef TRAPTRACE 9273446Smrj ttp->ttr_marker = TT_INTERRUPT; 9283446Smrj ttp->ttr_ipl = 0xff; 9293446Smrj ttp->ttr_pri = oldipl; 9303446Smrj ttp->ttr_spl = cpu->cpu_base_spl; 9313446Smrj ttp->ttr_vector = 0xff; 9323446Smrj #endif /* TRAPTRACE */ 9333446Smrj 9345084Sjohnlev #if !defined(__xpv) 9353446Smrj /* 9364191Sjosephb * Handle any pending TLB flushing 9374191Sjosephb */ 9384191Sjosephb tlb_service(); 9395084Sjohnlev #endif 9404191Sjosephb 9414191Sjosephb /* 9423446Smrj * If it's a softint go do it now. 9433446Smrj */ 9443446Smrj if (rp->r_trapno == T_SOFTINT) { 9453446Smrj dosoftint(rp); 9463446Smrj ASSERT(!interrupts_enabled()); 9473446Smrj return; 9483446Smrj } 9493446Smrj 9503446Smrj /* 9513446Smrj * Raise the interrupt priority. 9523446Smrj */ 9533446Smrj newipl = (*setlvl)(oldipl, (int *)&rp->r_trapno); 9543446Smrj #ifdef TRAPTRACE 9553446Smrj ttp->ttr_ipl = newipl; 9563446Smrj #endif /* TRAPTRACE */ 9573446Smrj 9583446Smrj /* 9593446Smrj * Bail if it is a spurious interrupt 9603446Smrj */ 9613446Smrj if (newipl == -1) 9623446Smrj return; 9633446Smrj cpu->cpu_pri = newipl; 9643446Smrj vector = rp->r_trapno; 9653446Smrj #ifdef TRAPTRACE 9663446Smrj ttp->ttr_vector = vector; 9673446Smrj #endif /* TRAPTRACE */ 9683446Smrj if (newipl > LOCK_LEVEL) { 9693446Smrj /* 9703446Smrj * High priority interrupts run on this cpu's interrupt stack. 9713446Smrj */ 9723446Smrj if (hilevel_intr_prolog(cpu, newipl, oldipl, rp) == 0) { 9733446Smrj newsp = cpu->cpu_intr_stack; 9743446Smrj switch_sp_and_call(newsp, dispatch_hilevel, vector, 0); 9753446Smrj } else { /* already on the interrupt stack */ 9763446Smrj dispatch_hilevel(vector, 0); 9773446Smrj } 9783446Smrj (void) hilevel_intr_epilog(cpu, newipl, oldipl, vector); 9793446Smrj } else { 9803446Smrj /* 9813446Smrj * Run this interrupt in a separate thread. 9823446Smrj */ 9833446Smrj newsp = intr_thread_prolog(cpu, (caddr_t)rp, newipl); 9843446Smrj switch_sp_and_call(newsp, dispatch_hardint, vector, oldipl); 9853446Smrj } 9863446Smrj 9873446Smrj /* 9883446Smrj * Deliver any pending soft interrupts. 9893446Smrj */ 9903446Smrj if (cpu->cpu_softinfo.st_pending) 9913446Smrj dosoftint(rp); 9923446Smrj } 9933446Smrj 9943446Smrj /* 9953446Smrj * Common tasks always done by _sys_rtt, called with interrupts disabled. 9963446Smrj * Returns 1 if returning to userland, 0 if returning to system mode. 9973446Smrj */ 9983446Smrj int 9993446Smrj sys_rtt_common(struct regs *rp) 10003446Smrj { 10013446Smrj kthread_t *tp; 10023446Smrj extern void mutex_exit_critical_start(); 10033446Smrj extern long mutex_exit_critical_size; 10045834Spt157919 extern void mutex_owner_running_critical_start(); 10055834Spt157919 extern long mutex_owner_running_critical_size; 10063446Smrj 10073446Smrj loop: 10083446Smrj 10093446Smrj /* 10103446Smrj * Check if returning to user 10113446Smrj */ 10123446Smrj tp = CPU->cpu_thread; 10133446Smrj if (USERMODE(rp->r_cs)) { 10143446Smrj /* 10153446Smrj * Check if AST pending. 10163446Smrj */ 10173446Smrj if (tp->t_astflag) { 10183446Smrj /* 10193446Smrj * Let trap() handle the AST 10203446Smrj */ 10213446Smrj sti(); 10223446Smrj rp->r_trapno = T_AST; 10233446Smrj trap(rp, (caddr_t)0, CPU->cpu_id); 10243446Smrj cli(); 10253446Smrj goto loop; 10263446Smrj } 10273446Smrj 10283446Smrj #if defined(__amd64) 10293446Smrj /* 10303446Smrj * We are done if segment registers do not need updating. 10313446Smrj */ 10324503Ssudheer if (tp->t_lwp->lwp_pcb.pcb_rupdate == 0) 10333446Smrj return (1); 10343446Smrj 10353446Smrj if (update_sregs(rp, tp->t_lwp)) { 10363446Smrj /* 10373446Smrj * 1 or more of the selectors is bad. 10383446Smrj * Deliver a SIGSEGV. 10393446Smrj */ 10403446Smrj proc_t *p = ttoproc(tp); 10413446Smrj 10423446Smrj sti(); 10433446Smrj mutex_enter(&p->p_lock); 10443446Smrj tp->t_lwp->lwp_cursig = SIGSEGV; 10453446Smrj mutex_exit(&p->p_lock); 10463446Smrj psig(); 10473446Smrj tp->t_sig_check = 1; 10483446Smrj cli(); 10493446Smrj } 10504503Ssudheer tp->t_lwp->lwp_pcb.pcb_rupdate = 0; 10513446Smrj 10523446Smrj #endif /* __amd64 */ 10533446Smrj return (1); 10543446Smrj } 10553446Smrj 10563446Smrj /* 10573446Smrj * Here if we are returning to supervisor mode. 10583446Smrj * Check for a kernel preemption request. 10593446Smrj */ 10603446Smrj if (CPU->cpu_kprunrun && (rp->r_ps & PS_IE)) { 10613446Smrj 10623446Smrj /* 10633446Smrj * Do nothing if already in kpreempt 10643446Smrj */ 10653446Smrj if (!tp->t_preempt_lk) { 10663446Smrj tp->t_preempt_lk = 1; 10673446Smrj sti(); 10683446Smrj kpreempt(1); /* asynchronous kpreempt call */ 10693446Smrj cli(); 10703446Smrj tp->t_preempt_lk = 0; 10713446Smrj } 10723446Smrj } 10733446Smrj 10743446Smrj /* 10753446Smrj * If we interrupted the mutex_exit() critical region we must 10763446Smrj * reset the PC back to the beginning to prevent missed wakeups 10773446Smrj * See the comments in mutex_exit() for details. 10783446Smrj */ 10793446Smrj if ((uintptr_t)rp->r_pc - (uintptr_t)mutex_exit_critical_start < 10803446Smrj mutex_exit_critical_size) { 10813446Smrj rp->r_pc = (greg_t)mutex_exit_critical_start; 10823446Smrj } 10835834Spt157919 10845834Spt157919 /* 10855834Spt157919 * If we interrupted the mutex_owner_running() critical region we 10865834Spt157919 * must reset the PC back to the beginning to prevent dereferencing 10875834Spt157919 * of a freed thread pointer. See the comments in mutex_owner_running 10885834Spt157919 * for details. 10895834Spt157919 */ 10905834Spt157919 if ((uintptr_t)rp->r_pc - 10915834Spt157919 (uintptr_t)mutex_owner_running_critical_start < 10925834Spt157919 mutex_owner_running_critical_size) { 10935834Spt157919 rp->r_pc = (greg_t)mutex_owner_running_critical_start; 10945834Spt157919 } 10955834Spt157919 10963446Smrj return (0); 10973446Smrj } 10983446Smrj 10993446Smrj void 11003446Smrj send_dirint(int cpuid, int int_level) 11013446Smrj { 11023446Smrj (*send_dirintf)(cpuid, int_level); 11033446Smrj } 11043446Smrj 11053446Smrj /* 11063446Smrj * do_splx routine, takes new ipl to set 11073446Smrj * returns the old ipl. 11083446Smrj * We are careful not to set priority lower than CPU->cpu_base_pri, 11093446Smrj * even though it seems we're raising the priority, it could be set 11103446Smrj * higher at any time by an interrupt routine, so we must block interrupts 11113446Smrj * and look at CPU->cpu_base_pri 11123446Smrj */ 11133446Smrj int 11143446Smrj do_splx(int newpri) 11153446Smrj { 11163446Smrj ulong_t flag; 11173446Smrj cpu_t *cpu; 11183446Smrj int curpri, basepri; 11193446Smrj 11203446Smrj flag = intr_clear(); 11213446Smrj cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */ 11223446Smrj curpri = cpu->cpu_m.mcpu_pri; 11233446Smrj basepri = cpu->cpu_base_spl; 11243446Smrj if (newpri < basepri) 11253446Smrj newpri = basepri; 11263446Smrj cpu->cpu_m.mcpu_pri = newpri; 11273446Smrj (*setspl)(newpri); 11283446Smrj /* 11293446Smrj * If we are going to reenable interrupts see if new priority level 11303446Smrj * allows pending softint delivery. 11313446Smrj */ 11323446Smrj if ((flag & PS_IE) && 11333446Smrj bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > newpri) 11343446Smrj fakesoftint(); 11353446Smrj ASSERT(!interrupts_enabled()); 11363446Smrj intr_restore(flag); 11373446Smrj return (curpri); 11383446Smrj } 11393446Smrj 11403446Smrj /* 11413446Smrj * Common spl raise routine, takes new ipl to set 11423446Smrj * returns the old ipl, will not lower ipl. 11433446Smrj */ 11443446Smrj int 11453446Smrj splr(int newpri) 11463446Smrj { 11473446Smrj ulong_t flag; 11483446Smrj cpu_t *cpu; 11493446Smrj int curpri, basepri; 11503446Smrj 11513446Smrj flag = intr_clear(); 11523446Smrj cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */ 11533446Smrj curpri = cpu->cpu_m.mcpu_pri; 11543446Smrj /* 11553446Smrj * Only do something if new priority is larger 11563446Smrj */ 11573446Smrj if (newpri > curpri) { 11583446Smrj basepri = cpu->cpu_base_spl; 11593446Smrj if (newpri < basepri) 11603446Smrj newpri = basepri; 11613446Smrj cpu->cpu_m.mcpu_pri = newpri; 11623446Smrj (*setspl)(newpri); 11633446Smrj /* 11643446Smrj * See if new priority level allows pending softint delivery 11653446Smrj */ 11663446Smrj if ((flag & PS_IE) && 11673446Smrj bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > newpri) 11683446Smrj fakesoftint(); 11693446Smrj } 11703446Smrj intr_restore(flag); 11713446Smrj return (curpri); 11723446Smrj } 11733446Smrj 11743446Smrj int 11753446Smrj getpil(void) 11763446Smrj { 11773446Smrj return (CPU->cpu_m.mcpu_pri); 11783446Smrj } 11793446Smrj 11803446Smrj int 11813446Smrj interrupts_enabled(void) 11823446Smrj { 11833446Smrj ulong_t flag; 11843446Smrj 11853446Smrj flag = getflags(); 11863446Smrj return ((flag & PS_IE) == PS_IE); 11873446Smrj } 11883446Smrj 11893446Smrj #ifdef DEBUG 11903446Smrj void 11913446Smrj assert_ints_enabled(void) 11923446Smrj { 11933446Smrj ASSERT(!interrupts_unleashed || interrupts_enabled()); 11943446Smrj } 11953446Smrj #endif /* DEBUG */ 1196