17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 307c478bd9Sstevel@tonic-gate #include <sys/regset.h> 317c478bd9Sstevel@tonic-gate #include <sys/psw.h> 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <sys/thread.h> 347c478bd9Sstevel@tonic-gate #include <sys/systm.h> 357c478bd9Sstevel@tonic-gate #include <sys/segments.h> 367c478bd9Sstevel@tonic-gate #include <sys/pcb.h> 377c478bd9Sstevel@tonic-gate #include <sys/trap.h> 387c478bd9Sstevel@tonic-gate #include <sys/ftrace.h> 397c478bd9Sstevel@tonic-gate #include <sys/traptrace.h> 407c478bd9Sstevel@tonic-gate #include <sys/clock.h> 417c478bd9Sstevel@tonic-gate #include <sys/panic.h> 427c478bd9Sstevel@tonic-gate #include <sys/disp.h> 437c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h> 447c478bd9Sstevel@tonic-gate #include <sys/stack.h> 457c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 467c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 477c478bd9Sstevel@tonic-gate #include <sys/kstat.h> 487c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h> 497c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h> 507c478bd9Sstevel@tonic-gate #include <sys/zone.h> 517c478bd9Sstevel@tonic-gate #include <sys/bitmap.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #if defined(__amd64) 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate #if defined(__lint) 567c478bd9Sstevel@tonic-gate /* 577c478bd9Sstevel@tonic-gate * atomic_btr32() is a gcc __inline__ function, defined in <asm/bitmap.h> 587c478bd9Sstevel@tonic-gate * For lint purposes, define it here. 597c478bd9Sstevel@tonic-gate */ 607c478bd9Sstevel@tonic-gate uint_t 617c478bd9Sstevel@tonic-gate atomic_btr32(uint32_t *pending, uint_t pil) 627c478bd9Sstevel@tonic-gate { 637c478bd9Sstevel@tonic-gate return (*pending &= ~(1 << pil)); 647c478bd9Sstevel@tonic-gate } 657c478bd9Sstevel@tonic-gate #else 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate extern uint_t atomic_btr32(uint32_t *pending, uint_t pil); 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate #endif 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* 727c478bd9Sstevel@tonic-gate * This code is amd64-only for now, but as time permits, we should 737c478bd9Sstevel@tonic-gate * use this on i386 too. 747c478bd9Sstevel@tonic-gate */ 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * Some questions to ponder: 787c478bd9Sstevel@tonic-gate * - in several of these routines, we make multiple calls to tsc_read() 797c478bd9Sstevel@tonic-gate * without invoking functions .. couldn't we just reuse the same 807c478bd9Sstevel@tonic-gate * timestamp sometimes? 817c478bd9Sstevel@tonic-gate * - if we have the inline, we can probably make set_base_spl be a 827c478bd9Sstevel@tonic-gate * C routine too. 837c478bd9Sstevel@tonic-gate */ 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate static uint_t 867c478bd9Sstevel@tonic-gate bsrw_insn(uint16_t mask) 877c478bd9Sstevel@tonic-gate { 887c478bd9Sstevel@tonic-gate uint_t index = sizeof (mask) * NBBY - 1; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate ASSERT(mask != 0); 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate while ((mask & (1 << index)) == 0) 937c478bd9Sstevel@tonic-gate index--; 947c478bd9Sstevel@tonic-gate return (index); 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * Do all the work necessary to set up the cpu and thread structures 997c478bd9Sstevel@tonic-gate * to dispatch a high-level interrupt. 1007c478bd9Sstevel@tonic-gate * 1017c478bd9Sstevel@tonic-gate * Returns 0 if we're -not- already on the high-level interrupt stack, 1027c478bd9Sstevel@tonic-gate * (and *must* switch to it), non-zero if we are already on that stack. 1037c478bd9Sstevel@tonic-gate * 1047c478bd9Sstevel@tonic-gate * Called with interrupts masked. 1057c478bd9Sstevel@tonic-gate * The 'pil' is already set to the appropriate level for rp->r_trapno. 1067c478bd9Sstevel@tonic-gate */ 1077c478bd9Sstevel@tonic-gate int 1087c478bd9Sstevel@tonic-gate hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp) 1097c478bd9Sstevel@tonic-gate { 1107c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 1117c478bd9Sstevel@tonic-gate uint_t mask; 112eda89462Sesolom hrtime_t intrtime; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate ASSERT(pil > LOCK_LEVEL); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate if (pil == CBE_HIGH_PIL) { 1177c478bd9Sstevel@tonic-gate cpu->cpu_profile_pil = oldpil; 1187c478bd9Sstevel@tonic-gate if (USERMODE(rp->r_cs)) { 1197c478bd9Sstevel@tonic-gate cpu->cpu_profile_pc = 0; 1207c478bd9Sstevel@tonic-gate cpu->cpu_profile_upc = rp->r_pc; 1217c478bd9Sstevel@tonic-gate } else { 1227c478bd9Sstevel@tonic-gate cpu->cpu_profile_pc = rp->r_pc; 1237c478bd9Sstevel@tonic-gate cpu->cpu_profile_upc = 0; 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK; 1287c478bd9Sstevel@tonic-gate if (mask != 0) { 1297c478bd9Sstevel@tonic-gate int nestpil; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate /* 1327c478bd9Sstevel@tonic-gate * We have interrupted another high-level interrupt. 1337c478bd9Sstevel@tonic-gate * Load starting timestamp, compute interval, update 1347c478bd9Sstevel@tonic-gate * cumulative counter. 1357c478bd9Sstevel@tonic-gate */ 1367c478bd9Sstevel@tonic-gate nestpil = bsrw_insn((uint16_t)mask); 1377c478bd9Sstevel@tonic-gate ASSERT(nestpil < pil); 138eda89462Sesolom intrtime = tsc_read() - 1397c478bd9Sstevel@tonic-gate mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)]; 1407a364d25Sschwartz mcpu->intrstat[nestpil][0] += intrtime; 141eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 1427c478bd9Sstevel@tonic-gate /* 1437c478bd9Sstevel@tonic-gate * Another high-level interrupt is active below this one, so 1447c478bd9Sstevel@tonic-gate * there is no need to check for an interrupt thread. That 1457c478bd9Sstevel@tonic-gate * will be done by the lowest priority high-level interrupt 1467c478bd9Sstevel@tonic-gate * active. 1477c478bd9Sstevel@tonic-gate */ 1487c478bd9Sstevel@tonic-gate } else { 1497c478bd9Sstevel@tonic-gate kthread_t *t = cpu->cpu_thread; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* 1527c478bd9Sstevel@tonic-gate * See if we are interrupting a low-level interrupt thread. 1537c478bd9Sstevel@tonic-gate * If so, account for its time slice only if its time stamp 1547c478bd9Sstevel@tonic-gate * is non-zero. 1557c478bd9Sstevel@tonic-gate */ 1567c478bd9Sstevel@tonic-gate if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) { 157eda89462Sesolom intrtime = tsc_read() - t->t_intr_start; 1587a364d25Sschwartz mcpu->intrstat[t->t_pil][0] += intrtime; 159eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 1607c478bd9Sstevel@tonic-gate t->t_intr_start = 0; 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate * Store starting timestamp in CPU structure for this PIL. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = tsc_read(); 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0); 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate if (pil == 15) { 1727c478bd9Sstevel@tonic-gate /* 1737c478bd9Sstevel@tonic-gate * To support reentrant level 15 interrupts, we maintain a 1747c478bd9Sstevel@tonic-gate * recursion count in the top half of cpu_intr_actv. Only 1757c478bd9Sstevel@tonic-gate * when this count hits zero do we clear the PIL 15 bit from 1767c478bd9Sstevel@tonic-gate * the lower half of cpu_intr_actv. 1777c478bd9Sstevel@tonic-gate */ 1787c478bd9Sstevel@tonic-gate uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1; 1797c478bd9Sstevel@tonic-gate (*refcntp)++; 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate mask = cpu->cpu_intr_actv; 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil); 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK); 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate /* 1907c478bd9Sstevel@tonic-gate * Does most of the work of returning from a high level interrupt. 1917c478bd9Sstevel@tonic-gate * 1927c478bd9Sstevel@tonic-gate * Returns 0 if there are no more high level interrupts (in which 1937c478bd9Sstevel@tonic-gate * case we must switch back to the interrupted thread stack) or 1947c478bd9Sstevel@tonic-gate * non-zero if there are more (in which case we should stay on it). 1957c478bd9Sstevel@tonic-gate * 1967c478bd9Sstevel@tonic-gate * Called with interrupts masked 1977c478bd9Sstevel@tonic-gate */ 1987c478bd9Sstevel@tonic-gate int 1997c478bd9Sstevel@tonic-gate hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum) 2007c478bd9Sstevel@tonic-gate { 2017c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 2027c478bd9Sstevel@tonic-gate uint_t mask; 203eda89462Sesolom hrtime_t intrtime; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate ASSERT(mcpu->mcpu_pri == pil); 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++; 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil)); 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate if (pil == 15) { 2127c478bd9Sstevel@tonic-gate /* 2137c478bd9Sstevel@tonic-gate * To support reentrant level 15 interrupts, we maintain a 2147c478bd9Sstevel@tonic-gate * recursion count in the top half of cpu_intr_actv. Only 2157c478bd9Sstevel@tonic-gate * when this count hits zero do we clear the PIL 15 bit from 2167c478bd9Sstevel@tonic-gate * the lower half of cpu_intr_actv. 2177c478bd9Sstevel@tonic-gate */ 2187c478bd9Sstevel@tonic-gate uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate ASSERT(*refcntp > 0); 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate if (--(*refcntp) == 0) 2237c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 2247c478bd9Sstevel@tonic-gate } else { 2257c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0); 2297c478bd9Sstevel@tonic-gate 230eda89462Sesolom intrtime = tsc_read() - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)]; 2317a364d25Sschwartz mcpu->intrstat[pil][0] += intrtime; 232eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate /* 2357c478bd9Sstevel@tonic-gate * Check for lower-pil nested high-level interrupt beneath 2367c478bd9Sstevel@tonic-gate * current one. If so, place a starting timestamp in its 2377c478bd9Sstevel@tonic-gate * pil_high_start entry. 2387c478bd9Sstevel@tonic-gate */ 2397c478bd9Sstevel@tonic-gate mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK; 2407c478bd9Sstevel@tonic-gate if (mask != 0) { 2417c478bd9Sstevel@tonic-gate int nestpil; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate /* 2447c478bd9Sstevel@tonic-gate * find PIL of nested interrupt 2457c478bd9Sstevel@tonic-gate */ 2467c478bd9Sstevel@tonic-gate nestpil = bsrw_insn((uint16_t)mask); 2477c478bd9Sstevel@tonic-gate ASSERT(nestpil < pil); 2487c478bd9Sstevel@tonic-gate mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = tsc_read(); 2497c478bd9Sstevel@tonic-gate /* 2507c478bd9Sstevel@tonic-gate * (Another high-level interrupt is active below this one, 2517c478bd9Sstevel@tonic-gate * so there is no need to check for an interrupt 2527c478bd9Sstevel@tonic-gate * thread. That will be done by the lowest priority 2537c478bd9Sstevel@tonic-gate * high-level interrupt active.) 2547c478bd9Sstevel@tonic-gate */ 2557c478bd9Sstevel@tonic-gate } else { 2567c478bd9Sstevel@tonic-gate /* 2577c478bd9Sstevel@tonic-gate * Check to see if there is a low-level interrupt active. 2587c478bd9Sstevel@tonic-gate * If so, place a starting timestamp in the thread 2597c478bd9Sstevel@tonic-gate * structure. 2607c478bd9Sstevel@tonic-gate */ 2617c478bd9Sstevel@tonic-gate kthread_t *t = cpu->cpu_thread; 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate if (t->t_flag & T_INTR_THREAD) 2647c478bd9Sstevel@tonic-gate t->t_intr_start = tsc_read(); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate mcpu->mcpu_pri = oldpil; 2687c478bd9Sstevel@tonic-gate (void) (*setlvlx)(oldpil, vecnum); 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * Set up the cpu, thread and interrupt thread structures for 2757c478bd9Sstevel@tonic-gate * executing an interrupt thread. The new stack pointer of the 2767c478bd9Sstevel@tonic-gate * interrupt thread (which *must* be switched to) is returned. 2777c478bd9Sstevel@tonic-gate */ 2787c478bd9Sstevel@tonic-gate caddr_t 2797c478bd9Sstevel@tonic-gate intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil) 2807c478bd9Sstevel@tonic-gate { 2817c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 2827c478bd9Sstevel@tonic-gate kthread_t *t, *volatile it; 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate ASSERT(pil > 0); 2857c478bd9Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0); 2867c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil); 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate /* 2897c478bd9Sstevel@tonic-gate * Get set to run an interrupt thread. 2907c478bd9Sstevel@tonic-gate * There should always be an interrupt thread, since we 2917c478bd9Sstevel@tonic-gate * allocate one for each level on each CPU. 2927c478bd9Sstevel@tonic-gate * 293*fd71cd2fSesolom * t_intr_start could be zero due to cpu_intr_swtch_enter. 2947c478bd9Sstevel@tonic-gate */ 2957c478bd9Sstevel@tonic-gate t = cpu->cpu_thread; 296*fd71cd2fSesolom if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) { 297eda89462Sesolom hrtime_t intrtime = tsc_read() - t->t_intr_start; 2987a364d25Sschwartz mcpu->intrstat[t->t_pil][0] += intrtime; 299eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 3007c478bd9Sstevel@tonic-gate t->t_intr_start = 0; 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr); 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate t->t_sp = (uintptr_t)stackptr; /* mark stack in curthread for resume */ 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate /* 3087c478bd9Sstevel@tonic-gate * unlink the interrupt thread off the cpu 309*fd71cd2fSesolom * 310*fd71cd2fSesolom * Note that the code in kcpc_overflow_intr -relies- on the 311*fd71cd2fSesolom * ordering of events here - in particular that t->t_lwp of 312*fd71cd2fSesolom * the interrupt thread is set to the pinned thread *before* 313*fd71cd2fSesolom * curthread is changed. 3147c478bd9Sstevel@tonic-gate */ 3157c478bd9Sstevel@tonic-gate it = cpu->cpu_intr_thread; 3167c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it->t_link; 3177c478bd9Sstevel@tonic-gate it->t_intr = t; 3187c478bd9Sstevel@tonic-gate it->t_lwp = t->t_lwp; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate /* 3217c478bd9Sstevel@tonic-gate * (threads on the interrupt thread free list could have state 3227c478bd9Sstevel@tonic-gate * preset to TS_ONPROC, but it helps in debugging if 3237c478bd9Sstevel@tonic-gate * they're TS_FREE.) 3247c478bd9Sstevel@tonic-gate */ 3257c478bd9Sstevel@tonic-gate it->t_state = TS_ONPROC; 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate cpu->cpu_thread = it; /* new curthread on this cpu */ 3287c478bd9Sstevel@tonic-gate it->t_pil = (uchar_t)pil; 3297c478bd9Sstevel@tonic-gate it->t_pri = intr_pri + (pri_t)pil; 3307c478bd9Sstevel@tonic-gate it->t_intr_start = tsc_read(); 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate return (it->t_stk); 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate #ifdef DEBUG 3377c478bd9Sstevel@tonic-gate int intr_thread_cnt; 3387c478bd9Sstevel@tonic-gate #endif 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate /* 3417c478bd9Sstevel@tonic-gate * Called with interrupts disabled 3427c478bd9Sstevel@tonic-gate */ 3437c478bd9Sstevel@tonic-gate void 3447c478bd9Sstevel@tonic-gate intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil) 3457c478bd9Sstevel@tonic-gate { 3467c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 3477c478bd9Sstevel@tonic-gate kthread_t *t; 3487c478bd9Sstevel@tonic-gate kthread_t *it = cpu->cpu_thread; /* curthread */ 3497c478bd9Sstevel@tonic-gate uint_t pil, basespl; 350eda89462Sesolom hrtime_t intrtime; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate pil = it->t_pil; 3537c478bd9Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++; 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate ASSERT(it->t_intr_start != 0); 356eda89462Sesolom intrtime = tsc_read() - it->t_intr_start; 3577a364d25Sschwartz mcpu->intrstat[pil][0] += intrtime; 358eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil)); 3617c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate /* 3647c478bd9Sstevel@tonic-gate * If there is still an interrupted thread underneath this one 3657c478bd9Sstevel@tonic-gate * then the interrupt was never blocked and the return is 3667c478bd9Sstevel@tonic-gate * fairly simple. Otherwise it isn't. 3677c478bd9Sstevel@tonic-gate */ 3687c478bd9Sstevel@tonic-gate if ((t = it->t_intr) == NULL) { 3697c478bd9Sstevel@tonic-gate /* 3707c478bd9Sstevel@tonic-gate * The interrupted thread is no longer pinned underneath 3717c478bd9Sstevel@tonic-gate * the interrupt thread. This means the interrupt must 3727c478bd9Sstevel@tonic-gate * have blocked, and the interrupted thread has been 3737c478bd9Sstevel@tonic-gate * unpinned, and has probably been running around the 3747c478bd9Sstevel@tonic-gate * system for a while. 3757c478bd9Sstevel@tonic-gate * 3767c478bd9Sstevel@tonic-gate * Since there is no longer a thread under this one, put 3777c478bd9Sstevel@tonic-gate * this interrupt thread back on the CPU's free list and 3787c478bd9Sstevel@tonic-gate * resume the idle thread which will dispatch the next 3797c478bd9Sstevel@tonic-gate * thread to run. 3807c478bd9Sstevel@tonic-gate */ 3817c478bd9Sstevel@tonic-gate #ifdef DEBUG 3827c478bd9Sstevel@tonic-gate intr_thread_cnt++; 3837c478bd9Sstevel@tonic-gate #endif 3847c478bd9Sstevel@tonic-gate cpu->cpu_stats.sys.intrblk++; 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * Set CPU's base SPL based on active interrupts bitmask 3877c478bd9Sstevel@tonic-gate */ 3887c478bd9Sstevel@tonic-gate set_base_spl(); 3897c478bd9Sstevel@tonic-gate basespl = cpu->cpu_base_spl; 3907c478bd9Sstevel@tonic-gate mcpu->mcpu_pri = basespl; 3917c478bd9Sstevel@tonic-gate (*setlvlx)(basespl, vec); 3927c478bd9Sstevel@tonic-gate (void) splhigh(); 3937c478bd9Sstevel@tonic-gate it->t_state = TS_FREE; 3947c478bd9Sstevel@tonic-gate /* 3957c478bd9Sstevel@tonic-gate * Return interrupt thread to pool 3967c478bd9Sstevel@tonic-gate */ 3977c478bd9Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 3987c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it; 3997c478bd9Sstevel@tonic-gate swtch(); 4007c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate /* 4047c478bd9Sstevel@tonic-gate * Return interrupt thread to the pool 4057c478bd9Sstevel@tonic-gate */ 4067c478bd9Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 4077c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it; 4087c478bd9Sstevel@tonic-gate it->t_state = TS_FREE; 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate basespl = cpu->cpu_base_spl; 4117c478bd9Sstevel@tonic-gate pil = MAX(oldpil, basespl); 4127c478bd9Sstevel@tonic-gate mcpu->mcpu_pri = pil; 4137c478bd9Sstevel@tonic-gate (*setlvlx)(pil, vec); 4147c478bd9Sstevel@tonic-gate t->t_intr_start = tsc_read(); 4157c478bd9Sstevel@tonic-gate cpu->cpu_thread = t; 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate 4187a364d25Sschwartz /* 4197a364d25Sschwartz * Called with interrupts disabled by an interrupt thread to determine 4207a364d25Sschwartz * how much time has elapsed. See interrupt.s:intr_get_time() for detailed 4217a364d25Sschwartz * theory of operation. 4227a364d25Sschwartz */ 4237a364d25Sschwartz uint64_t 4247a364d25Sschwartz intr_thread_get_time(struct cpu *cpu) 4257a364d25Sschwartz { 4267a364d25Sschwartz struct machcpu *mcpu = &cpu->cpu_m; 4277a364d25Sschwartz kthread_t *t = cpu->cpu_thread; 4287a364d25Sschwartz uint64_t time, delta, ret; 4297a364d25Sschwartz uint_t pil = t->t_pil; 4307a364d25Sschwartz 4317a364d25Sschwartz ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0); 4327a364d25Sschwartz ASSERT(t->t_flag & T_INTR_THREAD); 4337a364d25Sschwartz ASSERT(pil != 0); 4347a364d25Sschwartz ASSERT(t->t_intr_start != 0); 4357a364d25Sschwartz 4367a364d25Sschwartz time = tsc_read(); 4377a364d25Sschwartz delta = time - t->t_intr_start; 4387a364d25Sschwartz t->t_intr_start = time; 4397a364d25Sschwartz 4407a364d25Sschwartz time = mcpu->intrstat[pil][0] + delta; 4417a364d25Sschwartz ret = time - mcpu->intrstat[pil][1]; 4427a364d25Sschwartz mcpu->intrstat[pil][0] = time; 4437a364d25Sschwartz mcpu->intrstat[pil][1] = time; 4447a364d25Sschwartz 4457a364d25Sschwartz return (ret); 4467a364d25Sschwartz } 4477a364d25Sschwartz 4487c478bd9Sstevel@tonic-gate caddr_t 4497c478bd9Sstevel@tonic-gate dosoftint_prolog( 4507c478bd9Sstevel@tonic-gate struct cpu *cpu, 4517c478bd9Sstevel@tonic-gate caddr_t stackptr, 4527c478bd9Sstevel@tonic-gate uint32_t st_pending, 4537c478bd9Sstevel@tonic-gate uint_t oldpil) 4547c478bd9Sstevel@tonic-gate { 4557c478bd9Sstevel@tonic-gate kthread_t *t, *volatile it; 4567c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 4577c478bd9Sstevel@tonic-gate uint_t pil; 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate top: 4607c478bd9Sstevel@tonic-gate ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending); 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate pil = bsrw_insn((uint16_t)st_pending); 4637c478bd9Sstevel@tonic-gate if (pil <= oldpil || pil <= cpu->cpu_base_spl) 4647c478bd9Sstevel@tonic-gate return (0); 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate /* 4677c478bd9Sstevel@tonic-gate * XX64 Sigh. 4687c478bd9Sstevel@tonic-gate * 4697c478bd9Sstevel@tonic-gate * This is a transliteration of the i386 assembler code for 4707c478bd9Sstevel@tonic-gate * soft interrupts. One question is "why does this need 4717c478bd9Sstevel@tonic-gate * to be atomic?" One possible race is -other- processors 4727c478bd9Sstevel@tonic-gate * posting soft interrupts to us in set_pending() i.e. the 4737c478bd9Sstevel@tonic-gate * CPU might get preempted just after the address computation, 4747c478bd9Sstevel@tonic-gate * but just before the atomic transaction, so another CPU would 4757c478bd9Sstevel@tonic-gate * actually set the original CPU's st_pending bit. However, 4767c478bd9Sstevel@tonic-gate * it looks like it would be simpler to disable preemption there. 4777c478bd9Sstevel@tonic-gate * Are there other races for which preemption control doesn't work? 4787c478bd9Sstevel@tonic-gate * 4797c478bd9Sstevel@tonic-gate * The i386 assembler version -also- checks to see if the bit 4807c478bd9Sstevel@tonic-gate * being cleared was actually set; if it wasn't, it rechecks 4817c478bd9Sstevel@tonic-gate * for more. This seems a bit strange, as the only code that 4827c478bd9Sstevel@tonic-gate * ever clears the bit is -this- code running with interrupts 4837c478bd9Sstevel@tonic-gate * disabled on -this- CPU. This code would probably be cheaper: 4847c478bd9Sstevel@tonic-gate * 4857c478bd9Sstevel@tonic-gate * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending, 4867c478bd9Sstevel@tonic-gate * ~(1 << pil)); 4877c478bd9Sstevel@tonic-gate * 4887c478bd9Sstevel@tonic-gate * and t->t_preempt--/++ around set_pending() even cheaper, 4897c478bd9Sstevel@tonic-gate * but at this point, correctness is critical, so we slavishly 4907c478bd9Sstevel@tonic-gate * emulate the i386 port. 4917c478bd9Sstevel@tonic-gate */ 4927c478bd9Sstevel@tonic-gate if (atomic_btr32((uint32_t *)&mcpu->mcpu_softinfo.st_pending, pil) 4937c478bd9Sstevel@tonic-gate == 0) { 4947c478bd9Sstevel@tonic-gate st_pending = mcpu->mcpu_softinfo.st_pending; 4957c478bd9Sstevel@tonic-gate goto top; 4967c478bd9Sstevel@tonic-gate } 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate mcpu->mcpu_pri = pil; 4997c478bd9Sstevel@tonic-gate (*setspl)(pil); 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate /* 5027c478bd9Sstevel@tonic-gate * Get set to run interrupt thread. 5037c478bd9Sstevel@tonic-gate * There should always be an interrupt thread since we 5047c478bd9Sstevel@tonic-gate * allocate one for each level on the CPU. 5057c478bd9Sstevel@tonic-gate */ 5067c478bd9Sstevel@tonic-gate it = cpu->cpu_intr_thread; 5077c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it->t_link; 5087c478bd9Sstevel@tonic-gate 509*fd71cd2fSesolom /* t_intr_start could be zero due to cpu_intr_swtch_enter. */ 510*fd71cd2fSesolom t = cpu->cpu_thread; 511*fd71cd2fSesolom if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) { 512*fd71cd2fSesolom hrtime_t intrtime = tsc_read() - t->t_intr_start; 513*fd71cd2fSesolom mcpu->intrstat[pil][0] += intrtime; 514*fd71cd2fSesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 515*fd71cd2fSesolom t->t_intr_start = 0; 516*fd71cd2fSesolom } 517*fd71cd2fSesolom 5187c478bd9Sstevel@tonic-gate /* 5197c478bd9Sstevel@tonic-gate * Note that the code in kcpc_overflow_intr -relies- on the 5207c478bd9Sstevel@tonic-gate * ordering of events here - in particular that t->t_lwp of 5217c478bd9Sstevel@tonic-gate * the interrupt thread is set to the pinned thread *before* 522*fd71cd2fSesolom * curthread is changed. 5237c478bd9Sstevel@tonic-gate */ 5247c478bd9Sstevel@tonic-gate it->t_lwp = t->t_lwp; 5257c478bd9Sstevel@tonic-gate it->t_state = TS_ONPROC; 5267c478bd9Sstevel@tonic-gate 5277c478bd9Sstevel@tonic-gate /* 5287c478bd9Sstevel@tonic-gate * Push interrupted thread onto list from new thread. 5297c478bd9Sstevel@tonic-gate * Set the new thread as the current one. 5307c478bd9Sstevel@tonic-gate * Set interrupted thread's T_SP because if it is the idle thread, 5317c478bd9Sstevel@tonic-gate * resume() may use that stack between threads. 5327c478bd9Sstevel@tonic-gate */ 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr); 5357c478bd9Sstevel@tonic-gate t->t_sp = (uintptr_t)stackptr; 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate it->t_intr = t; 5387c478bd9Sstevel@tonic-gate cpu->cpu_thread = it; 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate /* 5417c478bd9Sstevel@tonic-gate * Set bit for this pil in CPU's interrupt active bitmask. 5427c478bd9Sstevel@tonic-gate */ 5437c478bd9Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0); 5447c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil); 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate /* 5477c478bd9Sstevel@tonic-gate * Initialize thread priority level from intr_pri 5487c478bd9Sstevel@tonic-gate */ 5497c478bd9Sstevel@tonic-gate it->t_pil = (uchar_t)pil; 5507c478bd9Sstevel@tonic-gate it->t_pri = (pri_t)pil + intr_pri; 5517c478bd9Sstevel@tonic-gate it->t_intr_start = tsc_read(); 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate return (it->t_stk); 5547c478bd9Sstevel@tonic-gate } 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate void 5577c478bd9Sstevel@tonic-gate dosoftint_epilog(struct cpu *cpu, uint_t oldpil) 5587c478bd9Sstevel@tonic-gate { 5597c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m; 5607c478bd9Sstevel@tonic-gate kthread_t *t, *it; 5617c478bd9Sstevel@tonic-gate uint_t pil, basespl; 562eda89462Sesolom hrtime_t intrtime; 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate it = cpu->cpu_thread; 5657c478bd9Sstevel@tonic-gate pil = it->t_pil; 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++; 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil)); 5707c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil); 571eda89462Sesolom intrtime = tsc_read() - it->t_intr_start; 5727a364d25Sschwartz mcpu->intrstat[pil][0] += intrtime; 573eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime; 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate /* 5767c478bd9Sstevel@tonic-gate * If there is still an interrupted thread underneath this one 5777c478bd9Sstevel@tonic-gate * then the interrupt was never blocked and the return is 5787c478bd9Sstevel@tonic-gate * fairly simple. Otherwise it isn't. 5797c478bd9Sstevel@tonic-gate */ 5807c478bd9Sstevel@tonic-gate if ((t = it->t_intr) == NULL) { 5817c478bd9Sstevel@tonic-gate /* 5827c478bd9Sstevel@tonic-gate * Put thread back on the interrupt thread list. 5837c478bd9Sstevel@tonic-gate * This was an interrupt thread, so set CPU's base SPL. 5847c478bd9Sstevel@tonic-gate */ 5857c478bd9Sstevel@tonic-gate set_base_spl(); 5867c478bd9Sstevel@tonic-gate it->t_state = TS_FREE; 5877c478bd9Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 5887c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it; 5897c478bd9Sstevel@tonic-gate (void) splhigh(); 5907c478bd9Sstevel@tonic-gate swtch(); 5917c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread; 5947c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it; 5957c478bd9Sstevel@tonic-gate it->t_state = TS_FREE; 5967c478bd9Sstevel@tonic-gate cpu->cpu_thread = t; 5977c478bd9Sstevel@tonic-gate if (t->t_flag & T_INTR_THREAD) 5987c478bd9Sstevel@tonic-gate t->t_intr_start = tsc_read(); 5997c478bd9Sstevel@tonic-gate basespl = cpu->cpu_base_spl; 6007c478bd9Sstevel@tonic-gate pil = MAX(oldpil, basespl); 6017c478bd9Sstevel@tonic-gate mcpu->mcpu_pri = pil; 6027c478bd9Sstevel@tonic-gate (*setspl)(pil); 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate /* 6067c478bd9Sstevel@tonic-gate * Make the interrupted thread 'to' be runnable. 6077c478bd9Sstevel@tonic-gate * 6087c478bd9Sstevel@tonic-gate * Since t->t_sp has already been saved, t->t_pc is all 6097c478bd9Sstevel@tonic-gate * that needs to be set in this function. 6107c478bd9Sstevel@tonic-gate * 6117c478bd9Sstevel@tonic-gate * Returns the interrupt level of the interrupt thread. 6127c478bd9Sstevel@tonic-gate */ 6137c478bd9Sstevel@tonic-gate int 6147c478bd9Sstevel@tonic-gate intr_passivate( 6157c478bd9Sstevel@tonic-gate kthread_t *it, /* interrupt thread */ 6167c478bd9Sstevel@tonic-gate kthread_t *t) /* interrupted thread */ 6177c478bd9Sstevel@tonic-gate { 6187c478bd9Sstevel@tonic-gate extern void _sys_rtt(); 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate ASSERT(it->t_flag & T_INTR_THREAD); 6217c478bd9Sstevel@tonic-gate ASSERT(SA(t->t_sp) == t->t_sp); 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate t->t_pc = (uintptr_t)_sys_rtt; 6247c478bd9Sstevel@tonic-gate return (it->t_pil); 6257c478bd9Sstevel@tonic-gate } 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate #endif /* __amd64 */ 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate /* 6307c478bd9Sstevel@tonic-gate * Allocate threads and stacks for interrupt handling. 6317c478bd9Sstevel@tonic-gate */ 6327c478bd9Sstevel@tonic-gate #define NINTR_THREADS (LOCK_LEVEL-1) /* number of interrupt threads */ 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate void 6357c478bd9Sstevel@tonic-gate init_intr_threads(struct cpu *cp) 6367c478bd9Sstevel@tonic-gate { 6377c478bd9Sstevel@tonic-gate int i; 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate for (i = 0; i < NINTR_THREADS; i++) 6407c478bd9Sstevel@tonic-gate thread_create_intr(cp); 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate cp->cpu_intr_stack = (caddr_t)segkp_get(segkp, INTR_STACK_SIZE, 6437c478bd9Sstevel@tonic-gate KPD_HASREDZONE | KPD_NO_ANON | KPD_LOCKED) + 6447c478bd9Sstevel@tonic-gate INTR_STACK_SIZE - SA(MINFRAME); 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate /* 6487c478bd9Sstevel@tonic-gate * Create interrupt kstats for this CPU. 6497c478bd9Sstevel@tonic-gate */ 6507c478bd9Sstevel@tonic-gate void 6517c478bd9Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp) 6527c478bd9Sstevel@tonic-gate { 6537c478bd9Sstevel@tonic-gate int i; 6547c478bd9Sstevel@tonic-gate kstat_t *intr_ksp; 6557c478bd9Sstevel@tonic-gate kstat_named_t *knp; 6567c478bd9Sstevel@tonic-gate char name[KSTAT_STRLEN]; 6577c478bd9Sstevel@tonic-gate zoneid_t zoneid; 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate if (pool_pset_enabled()) 6627c478bd9Sstevel@tonic-gate zoneid = GLOBAL_ZONEID; 6637c478bd9Sstevel@tonic-gate else 6647c478bd9Sstevel@tonic-gate zoneid = ALL_ZONES; 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc", 6677c478bd9Sstevel@tonic-gate KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid); 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate /* 6707c478bd9Sstevel@tonic-gate * Initialize each PIL's named kstat 6717c478bd9Sstevel@tonic-gate */ 6727c478bd9Sstevel@tonic-gate if (intr_ksp != NULL) { 6737c478bd9Sstevel@tonic-gate intr_ksp->ks_update = cpu_kstat_intrstat_update; 6747c478bd9Sstevel@tonic-gate knp = (kstat_named_t *)intr_ksp->ks_data; 6757c478bd9Sstevel@tonic-gate intr_ksp->ks_private = cp; 6767c478bd9Sstevel@tonic-gate for (i = 0; i < PIL_MAX; i++) { 6777c478bd9Sstevel@tonic-gate (void) snprintf(name, KSTAT_STRLEN, "level-%d-time", 6787c478bd9Sstevel@tonic-gate i + 1); 6797c478bd9Sstevel@tonic-gate kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64); 6807c478bd9Sstevel@tonic-gate (void) snprintf(name, KSTAT_STRLEN, "level-%d-count", 6817c478bd9Sstevel@tonic-gate i + 1); 6827c478bd9Sstevel@tonic-gate kstat_named_init(&knp[(i * 2) + 1], name, 6837c478bd9Sstevel@tonic-gate KSTAT_DATA_UINT64); 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate kstat_install(intr_ksp); 6867c478bd9Sstevel@tonic-gate } 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate /* 6907c478bd9Sstevel@tonic-gate * Delete interrupt kstats for this CPU. 6917c478bd9Sstevel@tonic-gate */ 6927c478bd9Sstevel@tonic-gate void 6937c478bd9Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp) 6947c478bd9Sstevel@tonic-gate { 6957c478bd9Sstevel@tonic-gate kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES); 6967c478bd9Sstevel@tonic-gate } 6977c478bd9Sstevel@tonic-gate 6987c478bd9Sstevel@tonic-gate /* 6997c478bd9Sstevel@tonic-gate * Convert interrupt statistics from CPU ticks to nanoseconds and 7007c478bd9Sstevel@tonic-gate * update kstat. 7017c478bd9Sstevel@tonic-gate */ 7027c478bd9Sstevel@tonic-gate int 7037c478bd9Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw) 7047c478bd9Sstevel@tonic-gate { 7057c478bd9Sstevel@tonic-gate kstat_named_t *knp = ksp->ks_data; 7067c478bd9Sstevel@tonic-gate cpu_t *cpup = (cpu_t *)ksp->ks_private; 7077c478bd9Sstevel@tonic-gate int i; 7087c478bd9Sstevel@tonic-gate hrtime_t hrt; 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate if (rw == KSTAT_WRITE) 7117c478bd9Sstevel@tonic-gate return (EACCES); 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate for (i = 0; i < PIL_MAX; i++) { 7147a364d25Sschwartz hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0]; 7157c478bd9Sstevel@tonic-gate tsc_scalehrtime(&hrt); 7167c478bd9Sstevel@tonic-gate knp[i * 2].value.ui64 = (uint64_t)hrt; 7177c478bd9Sstevel@tonic-gate knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i]; 7187c478bd9Sstevel@tonic-gate } 7197c478bd9Sstevel@tonic-gate 7207c478bd9Sstevel@tonic-gate return (0); 7217c478bd9Sstevel@tonic-gate } 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate /* 7247c478bd9Sstevel@tonic-gate * An interrupt thread is ending a time slice, so compute the interval it 7257c478bd9Sstevel@tonic-gate * ran for and update the statistic for its PIL. 7267c478bd9Sstevel@tonic-gate */ 7277c478bd9Sstevel@tonic-gate void 7287c478bd9Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t) 7297c478bd9Sstevel@tonic-gate { 7307c478bd9Sstevel@tonic-gate uint64_t interval; 7317c478bd9Sstevel@tonic-gate uint64_t start; 732eda89462Sesolom cpu_t *cpu; 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate ASSERT((t->t_flag & T_INTR_THREAD) != 0); 7357c478bd9Sstevel@tonic-gate ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL); 7367c478bd9Sstevel@tonic-gate 7377c478bd9Sstevel@tonic-gate /* 7387c478bd9Sstevel@tonic-gate * We could be here with a zero timestamp. This could happen if: 7397c478bd9Sstevel@tonic-gate * an interrupt thread which no longer has a pinned thread underneath 7407c478bd9Sstevel@tonic-gate * it (i.e. it blocked at some point in its past) has finished running 7417c478bd9Sstevel@tonic-gate * its handler. intr_thread() updated the interrupt statistic for its 7427c478bd9Sstevel@tonic-gate * PIL and zeroed its timestamp. Since there was no pinned thread to 7437c478bd9Sstevel@tonic-gate * return to, swtch() gets called and we end up here. 744eda89462Sesolom * 745eda89462Sesolom * Note that we use atomic ops below (cas64 and atomic_add_64), which 746eda89462Sesolom * we don't use in the functions above, because we're not called 747eda89462Sesolom * with interrupts blocked, but the epilog/prolog functions are. 7487c478bd9Sstevel@tonic-gate */ 7497c478bd9Sstevel@tonic-gate if (t->t_intr_start) { 7507c478bd9Sstevel@tonic-gate do { 7517c478bd9Sstevel@tonic-gate start = t->t_intr_start; 7527c478bd9Sstevel@tonic-gate interval = tsc_read() - start; 7537c478bd9Sstevel@tonic-gate } while (cas64(&t->t_intr_start, start, 0) != start); 754eda89462Sesolom cpu = CPU; 7557a364d25Sschwartz cpu->cpu_m.intrstat[t->t_pil][0] += interval; 756eda89462Sesolom 757eda89462Sesolom atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate], 758eda89462Sesolom interval); 7597c478bd9Sstevel@tonic-gate } else 7607c478bd9Sstevel@tonic-gate ASSERT(t->t_intr == NULL); 7617c478bd9Sstevel@tonic-gate } 7627c478bd9Sstevel@tonic-gate 7637c478bd9Sstevel@tonic-gate /* 7647c478bd9Sstevel@tonic-gate * An interrupt thread is returning from swtch(). Place a starting timestamp 7657c478bd9Sstevel@tonic-gate * in its thread structure. 7667c478bd9Sstevel@tonic-gate */ 7677c478bd9Sstevel@tonic-gate void 7687c478bd9Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t) 7697c478bd9Sstevel@tonic-gate { 7707c478bd9Sstevel@tonic-gate uint64_t ts; 7717c478bd9Sstevel@tonic-gate 7727c478bd9Sstevel@tonic-gate ASSERT((t->t_flag & T_INTR_THREAD) != 0); 7737c478bd9Sstevel@tonic-gate ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL); 7747c478bd9Sstevel@tonic-gate 7757c478bd9Sstevel@tonic-gate do { 7767c478bd9Sstevel@tonic-gate ts = t->t_intr_start; 7777c478bd9Sstevel@tonic-gate } while (cas64(&t->t_intr_start, ts, tsc_read()) != ts); 7787c478bd9Sstevel@tonic-gate } 779