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*5834Spt157919 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/cpuvar.h> 300Sstevel@tonic-gate #include <sys/regset.h> 310Sstevel@tonic-gate #include <sys/psw.h> 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/thread.h> 340Sstevel@tonic-gate #include <sys/systm.h> 350Sstevel@tonic-gate #include <sys/segments.h> 360Sstevel@tonic-gate #include <sys/pcb.h> 370Sstevel@tonic-gate #include <sys/trap.h> 380Sstevel@tonic-gate #include <sys/ftrace.h> 390Sstevel@tonic-gate #include <sys/traptrace.h> 400Sstevel@tonic-gate #include <sys/clock.h> 410Sstevel@tonic-gate #include <sys/panic.h> 420Sstevel@tonic-gate #include <sys/disp.h> 430Sstevel@tonic-gate #include <vm/seg_kp.h> 440Sstevel@tonic-gate #include <sys/stack.h> 450Sstevel@tonic-gate #include <sys/sysmacros.h> 460Sstevel@tonic-gate #include <sys/cmn_err.h> 470Sstevel@tonic-gate #include <sys/kstat.h> 480Sstevel@tonic-gate #include <sys/smp_impldefs.h> 490Sstevel@tonic-gate #include <sys/pool_pset.h> 500Sstevel@tonic-gate #include <sys/zone.h> 510Sstevel@tonic-gate #include <sys/bitmap.h> 523446Smrj #include <sys/archsystm.h> 533446Smrj #include <sys/machsystm.h> 543446Smrj #include <sys/ontrap.h> 553446Smrj #include <sys/x86_archext.h> 563446Smrj #include <sys/promif.h> 574191Sjosephb #include <vm/hat_i86.h> 585084Sjohnlev #if defined(__xpv) 595084Sjohnlev #include <sys/hypervisor.h> 605084Sjohnlev #endif 610Sstevel@tonic-gate 620Sstevel@tonic-gate 635084Sjohnlev #if defined(__xpv) && defined(DEBUG) 645084Sjohnlev 655084Sjohnlev /* 665084Sjohnlev * This panic message is intended as an aid to interrupt debugging. 675084Sjohnlev * 685084Sjohnlev * The associated assertion tests the condition of enabling 695084Sjohnlev * events when events are already enabled. The implication 705084Sjohnlev * being that whatever code the programmer thought was 715084Sjohnlev * protected by having events disabled until the second 725084Sjohnlev * enable happened really wasn't protected at all .. 735084Sjohnlev */ 745084Sjohnlev 755084Sjohnlev int stistipanic = 1; /* controls the debug panic check */ 765084Sjohnlev const char *stistimsg = "stisti"; 775084Sjohnlev ulong_t laststi[NCPU]; 785084Sjohnlev 795084Sjohnlev /* 805084Sjohnlev * This variable tracks the last place events were disabled on each cpu 815084Sjohnlev * it assists in debugging when asserts that interupts are enabled trip. 825084Sjohnlev */ 835084Sjohnlev ulong_t lastcli[NCPU]; 845084Sjohnlev 855084Sjohnlev #endif 865084Sjohnlev 870Sstevel@tonic-gate /* 883446Smrj * Set cpu's base SPL level to the highest active interrupt level 890Sstevel@tonic-gate */ 903446Smrj void 913446Smrj set_base_spl(void) 920Sstevel@tonic-gate { 933446Smrj struct cpu *cpu = CPU; 943446Smrj uint16_t active = (uint16_t)cpu->cpu_intr_actv; 950Sstevel@tonic-gate 963446Smrj cpu->cpu_base_spl = active == 0 ? 0 : bsrw_insn(active); 970Sstevel@tonic-gate } 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * Do all the work necessary to set up the cpu and thread structures 1010Sstevel@tonic-gate * to dispatch a high-level interrupt. 1020Sstevel@tonic-gate * 1030Sstevel@tonic-gate * Returns 0 if we're -not- already on the high-level interrupt stack, 1040Sstevel@tonic-gate * (and *must* switch to it), non-zero if we are already on that stack. 1050Sstevel@tonic-gate * 1060Sstevel@tonic-gate * Called with interrupts masked. 1070Sstevel@tonic-gate * The 'pil' is already set to the appropriate level for rp->r_trapno. 1080Sstevel@tonic-gate */ 1093446Smrj static int 1100Sstevel@tonic-gate hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp) 1110Sstevel@tonic-gate { 1120Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 1130Sstevel@tonic-gate uint_t mask; 114590Sesolom hrtime_t intrtime; 1153446Smrj hrtime_t now = tsc_read(); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate ASSERT(pil > LOCK_LEVEL); 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate if (pil == CBE_HIGH_PIL) { 1200Sstevel@tonic-gate cpu->cpu_profile_pil = oldpil; 1210Sstevel@tonic-gate if (USERMODE(rp->r_cs)) { 1220Sstevel@tonic-gate cpu->cpu_profile_pc = 0; 1230Sstevel@tonic-gate cpu->cpu_profile_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; 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK; 1310Sstevel@tonic-gate if (mask != 0) { 1320Sstevel@tonic-gate int nestpil; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate /* 1350Sstevel@tonic-gate * We have interrupted another high-level interrupt. 1360Sstevel@tonic-gate * Load starting timestamp, compute interval, update 1370Sstevel@tonic-gate * cumulative counter. 1380Sstevel@tonic-gate */ 1390Sstevel@tonic-gate nestpil = bsrw_insn((uint16_t)mask); 1400Sstevel@tonic-gate ASSERT(nestpil < pil); 1413446Smrj intrtime = now - 1420Sstevel@tonic-gate mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)]; 143916Sschwartz mcpu->intrstat[nestpil][0] += intrtime; 144590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 1450Sstevel@tonic-gate /* 1460Sstevel@tonic-gate * Another high-level interrupt is active below this one, so 1470Sstevel@tonic-gate * there is no need to check for an interrupt thread. That 1480Sstevel@tonic-gate * will be done by the lowest priority high-level interrupt 1490Sstevel@tonic-gate * active. 1500Sstevel@tonic-gate */ 1510Sstevel@tonic-gate } else { 1520Sstevel@tonic-gate kthread_t *t = cpu->cpu_thread; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* 1550Sstevel@tonic-gate * See if we are interrupting a low-level interrupt thread. 1560Sstevel@tonic-gate * If so, account for its time slice only if its time stamp 1570Sstevel@tonic-gate * is non-zero. 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) { 1603446Smrj intrtime = now - t->t_intr_start; 161916Sschwartz mcpu->intrstat[t->t_pil][0] += intrtime; 162590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 1630Sstevel@tonic-gate t->t_intr_start = 0; 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * Store starting timestamp in CPU structure for this PIL. 1690Sstevel@tonic-gate */ 1703446Smrj mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = now; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0); 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate if (pil == 15) { 1750Sstevel@tonic-gate /* 1760Sstevel@tonic-gate * To support reentrant level 15 interrupts, we maintain a 1770Sstevel@tonic-gate * recursion count in the top half of cpu_intr_actv. Only 1780Sstevel@tonic-gate * when this count hits zero do we clear the PIL 15 bit from 1790Sstevel@tonic-gate * the lower half of cpu_intr_actv. 1800Sstevel@tonic-gate */ 1810Sstevel@tonic-gate uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1; 1820Sstevel@tonic-gate (*refcntp)++; 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate mask = cpu->cpu_intr_actv; 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * Does most of the work of returning from a high level interrupt. 1940Sstevel@tonic-gate * 1950Sstevel@tonic-gate * Returns 0 if there are no more high level interrupts (in which 1960Sstevel@tonic-gate * case we must switch back to the interrupted thread stack) or 1970Sstevel@tonic-gate * non-zero if there are more (in which case we should stay on it). 1980Sstevel@tonic-gate * 1990Sstevel@tonic-gate * Called with interrupts masked 2000Sstevel@tonic-gate */ 2013446Smrj static int 2020Sstevel@tonic-gate hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum) 2030Sstevel@tonic-gate { 2040Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 2050Sstevel@tonic-gate uint_t mask; 206590Sesolom hrtime_t intrtime; 2073446Smrj hrtime_t now = tsc_read(); 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate ASSERT(mcpu->mcpu_pri == pil); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++; 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil)); 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate if (pil == 15) { 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * To support reentrant level 15 interrupts, we maintain a 2180Sstevel@tonic-gate * recursion count in the top half of cpu_intr_actv. Only 2190Sstevel@tonic-gate * when this count hits zero do we clear the PIL 15 bit from 2200Sstevel@tonic-gate * the lower half of cpu_intr_actv. 2210Sstevel@tonic-gate */ 2220Sstevel@tonic-gate uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate ASSERT(*refcntp > 0); 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate if (--(*refcntp) == 0) 2270Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 2280Sstevel@tonic-gate } else { 2290Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0); 2330Sstevel@tonic-gate 2343446Smrj intrtime = now - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)]; 235916Sschwartz mcpu->intrstat[pil][0] += intrtime; 236590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate /* 2390Sstevel@tonic-gate * Check for lower-pil nested high-level interrupt beneath 2400Sstevel@tonic-gate * current one. If so, place a starting timestamp in its 2410Sstevel@tonic-gate * pil_high_start entry. 2420Sstevel@tonic-gate */ 2430Sstevel@tonic-gate mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK; 2440Sstevel@tonic-gate if (mask != 0) { 2450Sstevel@tonic-gate int nestpil; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * find PIL of nested interrupt 2490Sstevel@tonic-gate */ 2500Sstevel@tonic-gate nestpil = bsrw_insn((uint16_t)mask); 2510Sstevel@tonic-gate ASSERT(nestpil < pil); 2523446Smrj mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = now; 2530Sstevel@tonic-gate /* 2540Sstevel@tonic-gate * (Another high-level interrupt is active below this one, 2550Sstevel@tonic-gate * so there is no need to check for an interrupt 2560Sstevel@tonic-gate * thread. That will be done by the lowest priority 2570Sstevel@tonic-gate * high-level interrupt active.) 2580Sstevel@tonic-gate */ 2590Sstevel@tonic-gate } else { 2600Sstevel@tonic-gate /* 2610Sstevel@tonic-gate * Check to see if there is a low-level interrupt active. 2620Sstevel@tonic-gate * If so, place a starting timestamp in the thread 2630Sstevel@tonic-gate * structure. 2640Sstevel@tonic-gate */ 2650Sstevel@tonic-gate kthread_t *t = cpu->cpu_thread; 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate if (t->t_flag & T_INTR_THREAD) 2683446Smrj t->t_intr_start = now; 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate mcpu->mcpu_pri = oldpil; 2720Sstevel@tonic-gate (void) (*setlvlx)(oldpil, vecnum); 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK); 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * Set up the cpu, thread and interrupt thread structures for 2790Sstevel@tonic-gate * executing an interrupt thread. The new stack pointer of the 2800Sstevel@tonic-gate * interrupt thread (which *must* be switched to) is returned. 2810Sstevel@tonic-gate */ 2823446Smrj static caddr_t 2830Sstevel@tonic-gate intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil) 2840Sstevel@tonic-gate { 2850Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 2860Sstevel@tonic-gate kthread_t *t, *volatile it; 2873446Smrj hrtime_t now = tsc_read(); 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate ASSERT(pil > 0); 2900Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0); 2910Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil); 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate /* 2940Sstevel@tonic-gate * Get set to run an interrupt thread. 2950Sstevel@tonic-gate * There should always be an interrupt thread, since we 2960Sstevel@tonic-gate * allocate one for each level on each CPU. 2970Sstevel@tonic-gate * 298989Sesolom * t_intr_start could be zero due to cpu_intr_swtch_enter. 2990Sstevel@tonic-gate */ 3000Sstevel@tonic-gate t = cpu->cpu_thread; 301989Sesolom if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) { 3023446Smrj hrtime_t intrtime = now - t->t_intr_start; 303916Sschwartz mcpu->intrstat[t->t_pil][0] += intrtime; 304590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 3050Sstevel@tonic-gate t->t_intr_start = 0; 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr); 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate t->t_sp = (uintptr_t)stackptr; /* mark stack in curthread for resume */ 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* 3130Sstevel@tonic-gate * unlink the interrupt thread off the cpu 314989Sesolom * 315989Sesolom * Note that the code in kcpc_overflow_intr -relies- on the 316989Sesolom * ordering of events here - in particular that t->t_lwp of 317989Sesolom * the interrupt thread is set to the pinned thread *before* 318989Sesolom * curthread is changed. 3190Sstevel@tonic-gate */ 3200Sstevel@tonic-gate it = cpu->cpu_intr_thread; 3210Sstevel@tonic-gate cpu->cpu_intr_thread = it->t_link; 3220Sstevel@tonic-gate it->t_intr = t; 3230Sstevel@tonic-gate it->t_lwp = t->t_lwp; 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * (threads on the interrupt thread free list could have state 3270Sstevel@tonic-gate * preset to TS_ONPROC, but it helps in debugging if 3280Sstevel@tonic-gate * they're TS_FREE.) 3290Sstevel@tonic-gate */ 3300Sstevel@tonic-gate it->t_state = TS_ONPROC; 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate cpu->cpu_thread = it; /* new curthread on this cpu */ 3330Sstevel@tonic-gate it->t_pil = (uchar_t)pil; 3340Sstevel@tonic-gate it->t_pri = intr_pri + (pri_t)pil; 3353446Smrj it->t_intr_start = now; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate return (it->t_stk); 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate #ifdef DEBUG 3420Sstevel@tonic-gate int intr_thread_cnt; 3430Sstevel@tonic-gate #endif 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate /* 3460Sstevel@tonic-gate * Called with interrupts disabled 3470Sstevel@tonic-gate */ 3483446Smrj static void 3490Sstevel@tonic-gate intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil) 3500Sstevel@tonic-gate { 3510Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 3520Sstevel@tonic-gate kthread_t *t; 3530Sstevel@tonic-gate kthread_t *it = cpu->cpu_thread; /* curthread */ 3540Sstevel@tonic-gate uint_t pil, basespl; 355590Sesolom hrtime_t intrtime; 3563446Smrj hrtime_t now = tsc_read(); 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate pil = it->t_pil; 3590Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++; 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate ASSERT(it->t_intr_start != 0); 3623446Smrj intrtime = now - it->t_intr_start; 363916Sschwartz mcpu->intrstat[pil][0] += intrtime; 364590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil)); 3670Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate /* 3700Sstevel@tonic-gate * If there is still an interrupted thread underneath this one 3710Sstevel@tonic-gate * then the interrupt was never blocked and the return is 3720Sstevel@tonic-gate * fairly simple. Otherwise it isn't. 3730Sstevel@tonic-gate */ 3740Sstevel@tonic-gate if ((t = it->t_intr) == NULL) { 3750Sstevel@tonic-gate /* 3760Sstevel@tonic-gate * The interrupted thread is no longer pinned underneath 3770Sstevel@tonic-gate * the interrupt thread. This means the interrupt must 3780Sstevel@tonic-gate * have blocked, and the interrupted thread has been 3790Sstevel@tonic-gate * unpinned, and has probably been running around the 3800Sstevel@tonic-gate * system for a while. 3810Sstevel@tonic-gate * 3820Sstevel@tonic-gate * Since there is no longer a thread under this one, put 3830Sstevel@tonic-gate * this interrupt thread back on the CPU's free list and 3840Sstevel@tonic-gate * resume the idle thread which will dispatch the next 3850Sstevel@tonic-gate * thread to run. 3860Sstevel@tonic-gate */ 3870Sstevel@tonic-gate #ifdef DEBUG 3880Sstevel@tonic-gate intr_thread_cnt++; 3890Sstevel@tonic-gate #endif 3900Sstevel@tonic-gate cpu->cpu_stats.sys.intrblk++; 3910Sstevel@tonic-gate /* 3920Sstevel@tonic-gate * Set CPU's base SPL based on active interrupts bitmask 3930Sstevel@tonic-gate */ 3940Sstevel@tonic-gate set_base_spl(); 3950Sstevel@tonic-gate basespl = cpu->cpu_base_spl; 3960Sstevel@tonic-gate mcpu->mcpu_pri = basespl; 3970Sstevel@tonic-gate (*setlvlx)(basespl, vec); 3980Sstevel@tonic-gate (void) splhigh(); 3993446Smrj sti(); 4000Sstevel@tonic-gate it->t_state = TS_FREE; 4010Sstevel@tonic-gate /* 4020Sstevel@tonic-gate * Return interrupt thread to pool 4030Sstevel@tonic-gate */ 4040Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 4050Sstevel@tonic-gate cpu->cpu_intr_thread = it; 4060Sstevel@tonic-gate swtch(); 4073446Smrj panic("intr_thread_epilog: swtch returned"); 4080Sstevel@tonic-gate /*NOTREACHED*/ 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate /* 4120Sstevel@tonic-gate * Return interrupt thread to the pool 4130Sstevel@tonic-gate */ 4140Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 4150Sstevel@tonic-gate cpu->cpu_intr_thread = it; 4160Sstevel@tonic-gate it->t_state = TS_FREE; 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate basespl = cpu->cpu_base_spl; 4190Sstevel@tonic-gate pil = MAX(oldpil, basespl); 4200Sstevel@tonic-gate mcpu->mcpu_pri = pil; 4210Sstevel@tonic-gate (*setlvlx)(pil, vec); 4223446Smrj t->t_intr_start = now; 4230Sstevel@tonic-gate cpu->cpu_thread = t; 4240Sstevel@tonic-gate } 4250Sstevel@tonic-gate 426916Sschwartz /* 4273446Smrj * intr_get_time() is a resource for interrupt handlers to determine how 4283446Smrj * much time has been spent handling the current interrupt. Such a function 4293446Smrj * is needed because higher level interrupts can arrive during the 4303446Smrj * processing of an interrupt. intr_get_time() only returns time spent in the 4313446Smrj * current interrupt handler. 4323446Smrj * 4333446Smrj * The caller must be calling from an interrupt handler running at a pil 4343446Smrj * below or at lock level. Timings are not provided for high-level 4353446Smrj * interrupts. 4363446Smrj * 4373446Smrj * The first time intr_get_time() is called while handling an interrupt, 4383446Smrj * it returns the time since the interrupt handler was invoked. Subsequent 4393446Smrj * calls will return the time since the prior call to intr_get_time(). Time 4405084Sjohnlev * is returned as ticks. Use scalehrtimef() to convert ticks to nsec. 4413446Smrj * 4423446Smrj * Theory Of Intrstat[][]: 4433446Smrj * 4443446Smrj * uint64_t intrstat[pil][0..1] is an array indexed by pil level, with two 4453446Smrj * uint64_ts per pil. 4463446Smrj * 4473446Smrj * intrstat[pil][0] is a cumulative count of the number of ticks spent 4483446Smrj * handling all interrupts at the specified pil on this CPU. It is 4493446Smrj * exported via kstats to the user. 4503446Smrj * 4513446Smrj * intrstat[pil][1] is always a count of ticks less than or equal to the 4523446Smrj * value in [0]. The difference between [1] and [0] is the value returned 4533446Smrj * by a call to intr_get_time(). At the start of interrupt processing, 4543446Smrj * [0] and [1] will be equal (or nearly so). As the interrupt consumes 4553446Smrj * time, [0] will increase, but [1] will remain the same. A call to 4563446Smrj * intr_get_time() will return the difference, then update [1] to be the 4573446Smrj * same as [0]. Future calls will return the time since the last call. 4583446Smrj * Finally, when the interrupt completes, [1] is updated to the same as [0]. 4593446Smrj * 4603446Smrj * Implementation: 4613446Smrj * 4623446Smrj * intr_get_time() works much like a higher level interrupt arriving. It 4633446Smrj * "checkpoints" the timing information by incrementing intrstat[pil][0] 4643446Smrj * to include elapsed running time, and by setting t_intr_start to rdtsc. 4653446Smrj * It then sets the return value to intrstat[pil][0] - intrstat[pil][1], 4663446Smrj * and updates intrstat[pil][1] to be the same as the new value of 4673446Smrj * intrstat[pil][0]. 4683446Smrj * 4693446Smrj * In the normal handling of interrupts, after an interrupt handler returns 4703446Smrj * and the code in intr_thread() updates intrstat[pil][0], it then sets 4713446Smrj * intrstat[pil][1] to the new value of intrstat[pil][0]. When [0] == [1], 4723446Smrj * the timings are reset, i.e. intr_get_time() will return [0] - [1] which 4733446Smrj * is 0. 4743446Smrj * 4753446Smrj * Whenever interrupts arrive on a CPU which is handling a lower pil 4763446Smrj * interrupt, they update the lower pil's [0] to show time spent in the 4773446Smrj * handler that they've interrupted. This results in a growing discrepancy 4783446Smrj * between [0] and [1], which is returned the next time intr_get_time() is 4793446Smrj * called. Time spent in the higher-pil interrupt will not be returned in 4803446Smrj * the next intr_get_time() call from the original interrupt, because 4813446Smrj * the higher-pil interrupt's time is accumulated in intrstat[higherpil][]. 482916Sschwartz */ 483916Sschwartz uint64_t 4843446Smrj intr_get_time(void) 485916Sschwartz { 4863446Smrj struct cpu *cpu; 4873446Smrj struct machcpu *mcpu; 4883446Smrj kthread_t *t; 489916Sschwartz uint64_t time, delta, ret; 4903446Smrj uint_t pil; 491916Sschwartz 4923446Smrj cli(); 4933446Smrj cpu = CPU; 4943446Smrj mcpu = &cpu->cpu_m; 4953446Smrj t = cpu->cpu_thread; 4963446Smrj pil = t->t_pil; 497916Sschwartz ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0); 498916Sschwartz ASSERT(t->t_flag & T_INTR_THREAD); 499916Sschwartz ASSERT(pil != 0); 500916Sschwartz ASSERT(t->t_intr_start != 0); 501916Sschwartz 502916Sschwartz time = tsc_read(); 503916Sschwartz delta = time - t->t_intr_start; 504916Sschwartz t->t_intr_start = time; 505916Sschwartz 506916Sschwartz time = mcpu->intrstat[pil][0] + delta; 507916Sschwartz ret = time - mcpu->intrstat[pil][1]; 508916Sschwartz mcpu->intrstat[pil][0] = time; 509916Sschwartz mcpu->intrstat[pil][1] = time; 5101887Sjhaslam cpu->cpu_intracct[cpu->cpu_mstate] += delta; 511916Sschwartz 5123446Smrj sti(); 513916Sschwartz return (ret); 514916Sschwartz } 515916Sschwartz 5163446Smrj static caddr_t 5170Sstevel@tonic-gate dosoftint_prolog( 5180Sstevel@tonic-gate struct cpu *cpu, 5190Sstevel@tonic-gate caddr_t stackptr, 5200Sstevel@tonic-gate uint32_t st_pending, 5210Sstevel@tonic-gate uint_t oldpil) 5220Sstevel@tonic-gate { 5230Sstevel@tonic-gate kthread_t *t, *volatile it; 5240Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 5250Sstevel@tonic-gate uint_t pil; 5263446Smrj hrtime_t now; 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate top: 5290Sstevel@tonic-gate ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending); 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate pil = bsrw_insn((uint16_t)st_pending); 5320Sstevel@tonic-gate if (pil <= oldpil || pil <= cpu->cpu_base_spl) 5330Sstevel@tonic-gate return (0); 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate /* 5360Sstevel@tonic-gate * XX64 Sigh. 5370Sstevel@tonic-gate * 5380Sstevel@tonic-gate * This is a transliteration of the i386 assembler code for 5390Sstevel@tonic-gate * soft interrupts. One question is "why does this need 5400Sstevel@tonic-gate * to be atomic?" One possible race is -other- processors 5410Sstevel@tonic-gate * posting soft interrupts to us in set_pending() i.e. the 5420Sstevel@tonic-gate * CPU might get preempted just after the address computation, 5430Sstevel@tonic-gate * but just before the atomic transaction, so another CPU would 5440Sstevel@tonic-gate * actually set the original CPU's st_pending bit. However, 5450Sstevel@tonic-gate * it looks like it would be simpler to disable preemption there. 5460Sstevel@tonic-gate * Are there other races for which preemption control doesn't work? 5470Sstevel@tonic-gate * 5480Sstevel@tonic-gate * The i386 assembler version -also- checks to see if the bit 5490Sstevel@tonic-gate * being cleared was actually set; if it wasn't, it rechecks 5500Sstevel@tonic-gate * for more. This seems a bit strange, as the only code that 5510Sstevel@tonic-gate * ever clears the bit is -this- code running with interrupts 5520Sstevel@tonic-gate * disabled on -this- CPU. This code would probably be cheaper: 5530Sstevel@tonic-gate * 5540Sstevel@tonic-gate * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending, 5550Sstevel@tonic-gate * ~(1 << pil)); 5560Sstevel@tonic-gate * 5570Sstevel@tonic-gate * and t->t_preempt--/++ around set_pending() even cheaper, 5580Sstevel@tonic-gate * but at this point, correctness is critical, so we slavishly 5590Sstevel@tonic-gate * emulate the i386 port. 5600Sstevel@tonic-gate */ 5613446Smrj if (atomic_btr32((uint32_t *) 5623446Smrj &mcpu->mcpu_softinfo.st_pending, pil) == 0) { 5630Sstevel@tonic-gate st_pending = mcpu->mcpu_softinfo.st_pending; 5640Sstevel@tonic-gate goto top; 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate mcpu->mcpu_pri = pil; 5680Sstevel@tonic-gate (*setspl)(pil); 5690Sstevel@tonic-gate 5703446Smrj now = tsc_read(); 5713446Smrj 5720Sstevel@tonic-gate /* 5730Sstevel@tonic-gate * Get set to run interrupt thread. 5740Sstevel@tonic-gate * There should always be an interrupt thread since we 5750Sstevel@tonic-gate * allocate one for each level on the CPU. 5760Sstevel@tonic-gate */ 5770Sstevel@tonic-gate it = cpu->cpu_intr_thread; 5780Sstevel@tonic-gate cpu->cpu_intr_thread = it->t_link; 5790Sstevel@tonic-gate 580989Sesolom /* t_intr_start could be zero due to cpu_intr_swtch_enter. */ 581989Sesolom t = cpu->cpu_thread; 582989Sesolom if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) { 5833446Smrj hrtime_t intrtime = now - t->t_intr_start; 584989Sesolom mcpu->intrstat[pil][0] += intrtime; 585989Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 586989Sesolom t->t_intr_start = 0; 587989Sesolom } 588989Sesolom 5890Sstevel@tonic-gate /* 5900Sstevel@tonic-gate * Note that the code in kcpc_overflow_intr -relies- on the 5910Sstevel@tonic-gate * ordering of events here - in particular that t->t_lwp of 5920Sstevel@tonic-gate * the interrupt thread is set to the pinned thread *before* 593989Sesolom * curthread is changed. 5940Sstevel@tonic-gate */ 5950Sstevel@tonic-gate it->t_lwp = t->t_lwp; 5960Sstevel@tonic-gate it->t_state = TS_ONPROC; 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate /* 5990Sstevel@tonic-gate * Push interrupted thread onto list from new thread. 6000Sstevel@tonic-gate * Set the new thread as the current one. 6010Sstevel@tonic-gate * Set interrupted thread's T_SP because if it is the idle thread, 6020Sstevel@tonic-gate * resume() may use that stack between threads. 6030Sstevel@tonic-gate */ 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr); 6060Sstevel@tonic-gate t->t_sp = (uintptr_t)stackptr; 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate it->t_intr = t; 6090Sstevel@tonic-gate cpu->cpu_thread = it; 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate /* 6120Sstevel@tonic-gate * Set bit for this pil in CPU's interrupt active bitmask. 6130Sstevel@tonic-gate */ 6140Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0); 6150Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil); 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate /* 6180Sstevel@tonic-gate * Initialize thread priority level from intr_pri 6190Sstevel@tonic-gate */ 6200Sstevel@tonic-gate it->t_pil = (uchar_t)pil; 6210Sstevel@tonic-gate it->t_pri = (pri_t)pil + intr_pri; 6223446Smrj it->t_intr_start = now; 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate return (it->t_stk); 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate 6273446Smrj static void 6280Sstevel@tonic-gate dosoftint_epilog(struct cpu *cpu, uint_t oldpil) 6290Sstevel@tonic-gate { 6300Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 6310Sstevel@tonic-gate kthread_t *t, *it; 6320Sstevel@tonic-gate uint_t pil, basespl; 633590Sesolom hrtime_t intrtime; 6343446Smrj hrtime_t now = tsc_read(); 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate it = cpu->cpu_thread; 6370Sstevel@tonic-gate pil = it->t_pil; 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++; 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil)); 6420Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 6433446Smrj intrtime = now - it->t_intr_start; 644916Sschwartz mcpu->intrstat[pil][0] += intrtime; 645590Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate /* 6480Sstevel@tonic-gate * If there is still an interrupted thread underneath this one 6490Sstevel@tonic-gate * then the interrupt was never blocked and the return is 6500Sstevel@tonic-gate * fairly simple. Otherwise it isn't. 6510Sstevel@tonic-gate */ 6520Sstevel@tonic-gate if ((t = it->t_intr) == NULL) { 6530Sstevel@tonic-gate /* 6540Sstevel@tonic-gate * Put thread back on the interrupt thread list. 6550Sstevel@tonic-gate * This was an interrupt thread, so set CPU's base SPL. 6560Sstevel@tonic-gate */ 6570Sstevel@tonic-gate set_base_spl(); 6580Sstevel@tonic-gate it->t_state = TS_FREE; 6590Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 6600Sstevel@tonic-gate cpu->cpu_intr_thread = it; 6610Sstevel@tonic-gate (void) splhigh(); 6623446Smrj sti(); 6630Sstevel@tonic-gate swtch(); 6640Sstevel@tonic-gate /*NOTREACHED*/ 6653446Smrj panic("dosoftint_epilog: swtch returned"); 6660Sstevel@tonic-gate } 6670Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 6680Sstevel@tonic-gate cpu->cpu_intr_thread = it; 6690Sstevel@tonic-gate it->t_state = TS_FREE; 6700Sstevel@tonic-gate cpu->cpu_thread = t; 6710Sstevel@tonic-gate if (t->t_flag & T_INTR_THREAD) 6723446Smrj t->t_intr_start = now; 6730Sstevel@tonic-gate basespl = cpu->cpu_base_spl; 6740Sstevel@tonic-gate pil = MAX(oldpil, basespl); 6750Sstevel@tonic-gate mcpu->mcpu_pri = pil; 6760Sstevel@tonic-gate (*setspl)(pil); 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate 6793446Smrj 6800Sstevel@tonic-gate /* 6810Sstevel@tonic-gate * Make the interrupted thread 'to' be runnable. 6820Sstevel@tonic-gate * 6830Sstevel@tonic-gate * Since t->t_sp has already been saved, t->t_pc is all 6840Sstevel@tonic-gate * that needs to be set in this function. 6850Sstevel@tonic-gate * 6860Sstevel@tonic-gate * Returns the interrupt level of the interrupt thread. 6870Sstevel@tonic-gate */ 6880Sstevel@tonic-gate int 6890Sstevel@tonic-gate intr_passivate( 6900Sstevel@tonic-gate kthread_t *it, /* interrupt thread */ 6910Sstevel@tonic-gate kthread_t *t) /* interrupted thread */ 6920Sstevel@tonic-gate { 6930Sstevel@tonic-gate extern void _sys_rtt(); 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate ASSERT(it->t_flag & T_INTR_THREAD); 6960Sstevel@tonic-gate ASSERT(SA(t->t_sp) == t->t_sp); 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate t->t_pc = (uintptr_t)_sys_rtt; 6990Sstevel@tonic-gate return (it->t_pil); 7000Sstevel@tonic-gate } 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate /* 7030Sstevel@tonic-gate * Create interrupt kstats for this CPU. 7040Sstevel@tonic-gate */ 7050Sstevel@tonic-gate void 7060Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp) 7070Sstevel@tonic-gate { 7080Sstevel@tonic-gate int i; 7090Sstevel@tonic-gate kstat_t *intr_ksp; 7100Sstevel@tonic-gate kstat_named_t *knp; 7110Sstevel@tonic-gate char name[KSTAT_STRLEN]; 7120Sstevel@tonic-gate zoneid_t zoneid; 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate if (pool_pset_enabled()) 7170Sstevel@tonic-gate zoneid = GLOBAL_ZONEID; 7180Sstevel@tonic-gate else 7190Sstevel@tonic-gate zoneid = ALL_ZONES; 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc", 7220Sstevel@tonic-gate KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid); 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate /* 7250Sstevel@tonic-gate * Initialize each PIL's named kstat 7260Sstevel@tonic-gate */ 7270Sstevel@tonic-gate if (intr_ksp != NULL) { 7280Sstevel@tonic-gate intr_ksp->ks_update = cpu_kstat_intrstat_update; 7290Sstevel@tonic-gate knp = (kstat_named_t *)intr_ksp->ks_data; 7300Sstevel@tonic-gate intr_ksp->ks_private = cp; 7310Sstevel@tonic-gate for (i = 0; i < PIL_MAX; i++) { 7320Sstevel@tonic-gate (void) snprintf(name, KSTAT_STRLEN, "level-%d-time", 7330Sstevel@tonic-gate i + 1); 7340Sstevel@tonic-gate kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64); 7350Sstevel@tonic-gate (void) snprintf(name, KSTAT_STRLEN, "level-%d-count", 7360Sstevel@tonic-gate i + 1); 7370Sstevel@tonic-gate kstat_named_init(&knp[(i * 2) + 1], name, 7380Sstevel@tonic-gate KSTAT_DATA_UINT64); 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate kstat_install(intr_ksp); 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * Delete interrupt kstats for this CPU. 7460Sstevel@tonic-gate */ 7470Sstevel@tonic-gate void 7480Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp) 7490Sstevel@tonic-gate { 7500Sstevel@tonic-gate kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES); 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate /* 7540Sstevel@tonic-gate * Convert interrupt statistics from CPU ticks to nanoseconds and 7550Sstevel@tonic-gate * update kstat. 7560Sstevel@tonic-gate */ 7570Sstevel@tonic-gate int 7580Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw) 7590Sstevel@tonic-gate { 7600Sstevel@tonic-gate kstat_named_t *knp = ksp->ks_data; 7610Sstevel@tonic-gate cpu_t *cpup = (cpu_t *)ksp->ks_private; 7620Sstevel@tonic-gate int i; 7630Sstevel@tonic-gate hrtime_t hrt; 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate if (rw == KSTAT_WRITE) 7660Sstevel@tonic-gate return (EACCES); 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate for (i = 0; i < PIL_MAX; i++) { 769916Sschwartz hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0]; 7705084Sjohnlev scalehrtimef(&hrt); 7710Sstevel@tonic-gate knp[i * 2].value.ui64 = (uint64_t)hrt; 7720Sstevel@tonic-gate knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i]; 7730Sstevel@tonic-gate } 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate return (0); 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate /* 7790Sstevel@tonic-gate * An interrupt thread is ending a time slice, so compute the interval it 7800Sstevel@tonic-gate * ran for and update the statistic for its PIL. 7810Sstevel@tonic-gate */ 7820Sstevel@tonic-gate void 7830Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t) 7840Sstevel@tonic-gate { 7850Sstevel@tonic-gate uint64_t interval; 7860Sstevel@tonic-gate uint64_t start; 787590Sesolom cpu_t *cpu; 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate ASSERT((t->t_flag & T_INTR_THREAD) != 0); 7900Sstevel@tonic-gate ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL); 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate /* 7930Sstevel@tonic-gate * We could be here with a zero timestamp. This could happen if: 7940Sstevel@tonic-gate * an interrupt thread which no longer has a pinned thread underneath 7950Sstevel@tonic-gate * it (i.e. it blocked at some point in its past) has finished running 7960Sstevel@tonic-gate * its handler. intr_thread() updated the interrupt statistic for its 7970Sstevel@tonic-gate * PIL and zeroed its timestamp. Since there was no pinned thread to 7980Sstevel@tonic-gate * return to, swtch() gets called and we end up here. 799590Sesolom * 800590Sesolom * Note that we use atomic ops below (cas64 and atomic_add_64), which 801590Sesolom * we don't use in the functions above, because we're not called 802590Sesolom * with interrupts blocked, but the epilog/prolog functions are. 8030Sstevel@tonic-gate */ 8040Sstevel@tonic-gate if (t->t_intr_start) { 8050Sstevel@tonic-gate do { 8060Sstevel@tonic-gate start = t->t_intr_start; 8070Sstevel@tonic-gate interval = tsc_read() - start; 8080Sstevel@tonic-gate } while (cas64(&t->t_intr_start, start, 0) != start); 809590Sesolom cpu = CPU; 810916Sschwartz cpu->cpu_m.intrstat[t->t_pil][0] += interval; 811590Sesolom 812590Sesolom atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate], 813590Sesolom interval); 8140Sstevel@tonic-gate } else 8150Sstevel@tonic-gate ASSERT(t->t_intr == NULL); 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate /* 8190Sstevel@tonic-gate * An interrupt thread is returning from swtch(). Place a starting timestamp 8200Sstevel@tonic-gate * in its thread structure. 8210Sstevel@tonic-gate */ 8220Sstevel@tonic-gate void 8230Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t) 8240Sstevel@tonic-gate { 8250Sstevel@tonic-gate uint64_t ts; 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate ASSERT((t->t_flag & T_INTR_THREAD) != 0); 8280Sstevel@tonic-gate ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL); 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate do { 8310Sstevel@tonic-gate ts = t->t_intr_start; 8320Sstevel@tonic-gate } while (cas64(&t->t_intr_start, ts, tsc_read()) != ts); 8330Sstevel@tonic-gate } 8343446Smrj 8353446Smrj /* 8363446Smrj * Dispatch a hilevel interrupt (one above LOCK_LEVEL) 8373446Smrj */ 8383446Smrj /*ARGSUSED*/ 8393446Smrj static void 8403446Smrj dispatch_hilevel(uint_t vector, uint_t arg2) 8413446Smrj { 8423446Smrj sti(); 8433446Smrj av_dispatch_autovect(vector); 8443446Smrj cli(); 8453446Smrj } 8463446Smrj 8473446Smrj /* 8483446Smrj * Dispatch a soft interrupt 8493446Smrj */ 8503446Smrj /*ARGSUSED*/ 8513446Smrj static void 8523446Smrj dispatch_softint(uint_t oldpil, uint_t arg2) 8533446Smrj { 8543446Smrj struct cpu *cpu = CPU; 8553446Smrj 8563446Smrj sti(); 8573446Smrj av_dispatch_softvect((int)cpu->cpu_thread->t_pil); 8583446Smrj cli(); 8593446Smrj 8603446Smrj /* 8613446Smrj * Must run softint_epilog() on the interrupt thread stack, since 8623446Smrj * there may not be a return from it if the interrupt thread blocked. 8633446Smrj */ 8643446Smrj dosoftint_epilog(cpu, oldpil); 8653446Smrj } 8663446Smrj 8673446Smrj /* 8683446Smrj * Dispatch a normal interrupt 8693446Smrj */ 8703446Smrj static void 8713446Smrj dispatch_hardint(uint_t vector, uint_t oldipl) 8723446Smrj { 8733446Smrj struct cpu *cpu = CPU; 8743446Smrj 8753446Smrj sti(); 8763446Smrj av_dispatch_autovect(vector); 8773446Smrj cli(); 8783446Smrj 8793446Smrj /* 8803446Smrj * Must run intr_thread_epilog() on the interrupt thread stack, since 8813446Smrj * there may not be a return from it if the interrupt thread blocked. 8823446Smrj */ 8833446Smrj intr_thread_epilog(cpu, vector, oldipl); 8843446Smrj } 8853446Smrj 8863446Smrj /* 8873446Smrj * Deliver any softints the current interrupt priority allows. 8883446Smrj * Called with interrupts disabled. 8893446Smrj */ 8903446Smrj void 8913446Smrj dosoftint(struct regs *regs) 8923446Smrj { 8933446Smrj struct cpu *cpu = CPU; 8943446Smrj int oldipl; 8953446Smrj caddr_t newsp; 8963446Smrj 8973446Smrj while (cpu->cpu_softinfo.st_pending) { 8983446Smrj oldipl = cpu->cpu_pri; 8993446Smrj newsp = dosoftint_prolog(cpu, (caddr_t)regs, 9005084Sjohnlev cpu->cpu_softinfo.st_pending, oldipl); 9013446Smrj /* 9023446Smrj * If returned stack pointer is NULL, priority is too high 9033446Smrj * to run any of the pending softints now. 9043446Smrj * Break out and they will be run later. 9053446Smrj */ 9063446Smrj if (newsp == NULL) 9073446Smrj break; 9083446Smrj switch_sp_and_call(newsp, dispatch_softint, oldipl, 0); 9093446Smrj } 9103446Smrj } 9113446Smrj 9123446Smrj /* 9133446Smrj * Interrupt service routine, called with interrupts disabled. 9143446Smrj */ 9153446Smrj /*ARGSUSED*/ 9163446Smrj void 9173446Smrj do_interrupt(struct regs *rp, trap_trace_rec_t *ttp) 9183446Smrj { 9193446Smrj struct cpu *cpu = CPU; 9203446Smrj int newipl, oldipl = cpu->cpu_pri; 9213446Smrj uint_t vector; 9223446Smrj caddr_t newsp; 9233446Smrj 9243446Smrj #ifdef TRAPTRACE 9253446Smrj ttp->ttr_marker = TT_INTERRUPT; 9263446Smrj ttp->ttr_ipl = 0xff; 9273446Smrj ttp->ttr_pri = oldipl; 9283446Smrj ttp->ttr_spl = cpu->cpu_base_spl; 9293446Smrj ttp->ttr_vector = 0xff; 9303446Smrj #endif /* TRAPTRACE */ 9313446Smrj 9325084Sjohnlev #if !defined(__xpv) 9333446Smrj /* 9344191Sjosephb * Handle any pending TLB flushing 9354191Sjosephb */ 9364191Sjosephb tlb_service(); 9375084Sjohnlev #endif 9384191Sjosephb 9394191Sjosephb /* 9403446Smrj * If it's a softint go do it now. 9413446Smrj */ 9423446Smrj if (rp->r_trapno == T_SOFTINT) { 9433446Smrj dosoftint(rp); 9443446Smrj ASSERT(!interrupts_enabled()); 9453446Smrj return; 9463446Smrj } 9473446Smrj 9483446Smrj /* 9493446Smrj * Raise the interrupt priority. 9503446Smrj */ 9513446Smrj newipl = (*setlvl)(oldipl, (int *)&rp->r_trapno); 9523446Smrj #ifdef TRAPTRACE 9533446Smrj ttp->ttr_ipl = newipl; 9543446Smrj #endif /* TRAPTRACE */ 9553446Smrj 9563446Smrj /* 9573446Smrj * Bail if it is a spurious interrupt 9583446Smrj */ 9593446Smrj if (newipl == -1) 9603446Smrj return; 9613446Smrj cpu->cpu_pri = newipl; 9623446Smrj vector = rp->r_trapno; 9633446Smrj #ifdef TRAPTRACE 9643446Smrj ttp->ttr_vector = vector; 9653446Smrj #endif /* TRAPTRACE */ 9663446Smrj if (newipl > LOCK_LEVEL) { 9673446Smrj /* 9683446Smrj * High priority interrupts run on this cpu's interrupt stack. 9693446Smrj */ 9703446Smrj if (hilevel_intr_prolog(cpu, newipl, oldipl, rp) == 0) { 9713446Smrj newsp = cpu->cpu_intr_stack; 9723446Smrj switch_sp_and_call(newsp, dispatch_hilevel, vector, 0); 9733446Smrj } else { /* already on the interrupt stack */ 9743446Smrj dispatch_hilevel(vector, 0); 9753446Smrj } 9763446Smrj (void) hilevel_intr_epilog(cpu, newipl, oldipl, vector); 9773446Smrj } else { 9783446Smrj /* 9793446Smrj * Run this interrupt in a separate thread. 9803446Smrj */ 9813446Smrj newsp = intr_thread_prolog(cpu, (caddr_t)rp, newipl); 9823446Smrj switch_sp_and_call(newsp, dispatch_hardint, vector, oldipl); 9833446Smrj } 9843446Smrj 9853446Smrj /* 9863446Smrj * Deliver any pending soft interrupts. 9873446Smrj */ 9883446Smrj if (cpu->cpu_softinfo.st_pending) 9893446Smrj dosoftint(rp); 9903446Smrj } 9913446Smrj 9923446Smrj /* 9933446Smrj * Common tasks always done by _sys_rtt, called with interrupts disabled. 9943446Smrj * Returns 1 if returning to userland, 0 if returning to system mode. 9953446Smrj */ 9963446Smrj int 9973446Smrj sys_rtt_common(struct regs *rp) 9983446Smrj { 9993446Smrj kthread_t *tp; 10003446Smrj extern void mutex_exit_critical_start(); 10013446Smrj extern long mutex_exit_critical_size; 1002*5834Spt157919 extern void mutex_owner_running_critical_start(); 1003*5834Spt157919 extern long mutex_owner_running_critical_size; 10043446Smrj 10053446Smrj loop: 10063446Smrj 10073446Smrj /* 10083446Smrj * Check if returning to user 10093446Smrj */ 10103446Smrj tp = CPU->cpu_thread; 10113446Smrj if (USERMODE(rp->r_cs)) { 10123446Smrj /* 10133446Smrj * Check if AST pending. 10143446Smrj */ 10153446Smrj if (tp->t_astflag) { 10163446Smrj /* 10173446Smrj * Let trap() handle the AST 10183446Smrj */ 10193446Smrj sti(); 10203446Smrj rp->r_trapno = T_AST; 10213446Smrj trap(rp, (caddr_t)0, CPU->cpu_id); 10223446Smrj cli(); 10233446Smrj goto loop; 10243446Smrj } 10253446Smrj 10263446Smrj #if defined(__amd64) 10273446Smrj /* 10283446Smrj * We are done if segment registers do not need updating. 10293446Smrj */ 10304503Ssudheer if (tp->t_lwp->lwp_pcb.pcb_rupdate == 0) 10313446Smrj return (1); 10323446Smrj 10333446Smrj if (update_sregs(rp, tp->t_lwp)) { 10343446Smrj /* 10353446Smrj * 1 or more of the selectors is bad. 10363446Smrj * Deliver a SIGSEGV. 10373446Smrj */ 10383446Smrj proc_t *p = ttoproc(tp); 10393446Smrj 10403446Smrj sti(); 10413446Smrj mutex_enter(&p->p_lock); 10423446Smrj tp->t_lwp->lwp_cursig = SIGSEGV; 10433446Smrj mutex_exit(&p->p_lock); 10443446Smrj psig(); 10453446Smrj tp->t_sig_check = 1; 10463446Smrj cli(); 10473446Smrj } 10484503Ssudheer tp->t_lwp->lwp_pcb.pcb_rupdate = 0; 10493446Smrj 10503446Smrj #endif /* __amd64 */ 10513446Smrj return (1); 10523446Smrj } 10533446Smrj 10543446Smrj /* 10553446Smrj * Here if we are returning to supervisor mode. 10563446Smrj * Check for a kernel preemption request. 10573446Smrj */ 10583446Smrj if (CPU->cpu_kprunrun && (rp->r_ps & PS_IE)) { 10593446Smrj 10603446Smrj /* 10613446Smrj * Do nothing if already in kpreempt 10623446Smrj */ 10633446Smrj if (!tp->t_preempt_lk) { 10643446Smrj tp->t_preempt_lk = 1; 10653446Smrj sti(); 10663446Smrj kpreempt(1); /* asynchronous kpreempt call */ 10673446Smrj cli(); 10683446Smrj tp->t_preempt_lk = 0; 10693446Smrj } 10703446Smrj } 10713446Smrj 10723446Smrj /* 10733446Smrj * If we interrupted the mutex_exit() critical region we must 10743446Smrj * reset the PC back to the beginning to prevent missed wakeups 10753446Smrj * See the comments in mutex_exit() for details. 10763446Smrj */ 10773446Smrj if ((uintptr_t)rp->r_pc - (uintptr_t)mutex_exit_critical_start < 10783446Smrj mutex_exit_critical_size) { 10793446Smrj rp->r_pc = (greg_t)mutex_exit_critical_start; 10803446Smrj } 1081*5834Spt157919 1082*5834Spt157919 /* 1083*5834Spt157919 * If we interrupted the mutex_owner_running() critical region we 1084*5834Spt157919 * must reset the PC back to the beginning to prevent dereferencing 1085*5834Spt157919 * of a freed thread pointer. See the comments in mutex_owner_running 1086*5834Spt157919 * for details. 1087*5834Spt157919 */ 1088*5834Spt157919 if ((uintptr_t)rp->r_pc - 1089*5834Spt157919 (uintptr_t)mutex_owner_running_critical_start < 1090*5834Spt157919 mutex_owner_running_critical_size) { 1091*5834Spt157919 rp->r_pc = (greg_t)mutex_owner_running_critical_start; 1092*5834Spt157919 } 1093*5834Spt157919 10943446Smrj return (0); 10953446Smrj } 10963446Smrj 10973446Smrj void 10983446Smrj send_dirint(int cpuid, int int_level) 10993446Smrj { 11003446Smrj (*send_dirintf)(cpuid, int_level); 11013446Smrj } 11023446Smrj 11033446Smrj /* 11043446Smrj * do_splx routine, takes new ipl to set 11053446Smrj * returns the old ipl. 11063446Smrj * We are careful not to set priority lower than CPU->cpu_base_pri, 11073446Smrj * even though it seems we're raising the priority, it could be set 11083446Smrj * higher at any time by an interrupt routine, so we must block interrupts 11093446Smrj * and look at CPU->cpu_base_pri 11103446Smrj */ 11113446Smrj int 11123446Smrj do_splx(int newpri) 11133446Smrj { 11143446Smrj ulong_t flag; 11153446Smrj cpu_t *cpu; 11163446Smrj int curpri, basepri; 11173446Smrj 11183446Smrj flag = intr_clear(); 11193446Smrj cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */ 11203446Smrj curpri = cpu->cpu_m.mcpu_pri; 11213446Smrj basepri = cpu->cpu_base_spl; 11223446Smrj if (newpri < basepri) 11233446Smrj newpri = basepri; 11243446Smrj cpu->cpu_m.mcpu_pri = newpri; 11253446Smrj (*setspl)(newpri); 11263446Smrj /* 11273446Smrj * If we are going to reenable interrupts see if new priority level 11283446Smrj * allows pending softint delivery. 11293446Smrj */ 11303446Smrj if ((flag & PS_IE) && 11313446Smrj bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > newpri) 11323446Smrj fakesoftint(); 11333446Smrj ASSERT(!interrupts_enabled()); 11343446Smrj intr_restore(flag); 11353446Smrj return (curpri); 11363446Smrj } 11373446Smrj 11383446Smrj /* 11393446Smrj * Common spl raise routine, takes new ipl to set 11403446Smrj * returns the old ipl, will not lower ipl. 11413446Smrj */ 11423446Smrj int 11433446Smrj splr(int newpri) 11443446Smrj { 11453446Smrj ulong_t flag; 11463446Smrj cpu_t *cpu; 11473446Smrj int curpri, basepri; 11483446Smrj 11493446Smrj flag = intr_clear(); 11503446Smrj cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */ 11513446Smrj curpri = cpu->cpu_m.mcpu_pri; 11523446Smrj /* 11533446Smrj * Only do something if new priority is larger 11543446Smrj */ 11553446Smrj if (newpri > curpri) { 11563446Smrj basepri = cpu->cpu_base_spl; 11573446Smrj if (newpri < basepri) 11583446Smrj newpri = basepri; 11593446Smrj cpu->cpu_m.mcpu_pri = newpri; 11603446Smrj (*setspl)(newpri); 11613446Smrj /* 11623446Smrj * See if new priority level allows pending softint delivery 11633446Smrj */ 11643446Smrj if ((flag & PS_IE) && 11653446Smrj bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > newpri) 11663446Smrj fakesoftint(); 11673446Smrj } 11683446Smrj intr_restore(flag); 11693446Smrj return (curpri); 11703446Smrj } 11713446Smrj 11723446Smrj int 11733446Smrj getpil(void) 11743446Smrj { 11753446Smrj return (CPU->cpu_m.mcpu_pri); 11763446Smrj } 11773446Smrj 11783446Smrj int 11793446Smrj interrupts_enabled(void) 11803446Smrj { 11813446Smrj ulong_t flag; 11823446Smrj 11833446Smrj flag = getflags(); 11843446Smrj return ((flag & PS_IE) == PS_IE); 11853446Smrj } 11863446Smrj 11873446Smrj #ifdef DEBUG 11883446Smrj void 11893446Smrj assert_ints_enabled(void) 11903446Smrj { 11913446Smrj ASSERT(!interrupts_unleashed || interrupts_enabled()); 11923446Smrj } 11933446Smrj #endif /* DEBUG */ 1194