xref: /illumos-gate/usr/src/uts/i86pc/os/intr.c (revision c3377ee9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5100b72f4Sandrei  * Common Development and Distribution License (the "License").
6100b72f4Sandrei  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21843e1988Sjohnlev 
227c478bd9Sstevel@tonic-gate /*
235cd376e8SJimmy Vetayases  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24*c3377ee9SJohn Levon  * Copyright 2019 Joyent, Inc.
25636dfb4bSJerry Jelinek  */
26636dfb4bSJerry Jelinek 
27636dfb4bSJerry Jelinek /*
28636dfb4bSJerry Jelinek  * To understand the present state of interrupt handling on i86pc, we must
29636dfb4bSJerry Jelinek  * first consider the history of interrupt controllers and our way of handling
30636dfb4bSJerry Jelinek  * interrupts.
31636dfb4bSJerry Jelinek  *
32636dfb4bSJerry Jelinek  * History of Interrupt Controllers on i86pc
33636dfb4bSJerry Jelinek  * -----------------------------------------
34636dfb4bSJerry Jelinek  *
35636dfb4bSJerry Jelinek  *    Intel 8259 and 8259A
36636dfb4bSJerry Jelinek  *
37636dfb4bSJerry Jelinek  * The first interrupt controller that attained widespread use on i86pc was
38636dfb4bSJerry Jelinek  * the Intel 8259(A) Programmable Interrupt Controller that first saw use with
39636dfb4bSJerry Jelinek  * the 8086. It took up to 8 interrupt sources and combined them into one
40636dfb4bSJerry Jelinek  * output wire. Up to 8 8259s could be slaved together providing up to 64 IRQs.
41636dfb4bSJerry Jelinek  * With the switch to the 8259A, level mode interrupts became possible. For a
42636dfb4bSJerry Jelinek  * long time on i86pc the 8259A was the only way to handle interrupts and it
43636dfb4bSJerry Jelinek  * had its own set of quirks. The 8259A and its corresponding interval timer
44636dfb4bSJerry Jelinek  * the 8254 are programmed using outb and inb instructions.
45636dfb4bSJerry Jelinek  *
46636dfb4bSJerry Jelinek  *    Intel Advanced Programmable Interrupt Controller (APIC)
47636dfb4bSJerry Jelinek  *
48636dfb4bSJerry Jelinek  * Starting around the time of the introduction of the P6 family
49636dfb4bSJerry Jelinek  * microarchitecture (i686) Intel introduced a new interrupt controller.
50636dfb4bSJerry Jelinek  * Instead of having the series of slaved 8259A devices, Intel opted to outfit
51636dfb4bSJerry Jelinek  * each processor with a Local APIC (lapic) and to outfit the system with at
52636dfb4bSJerry Jelinek  * least one, but potentially more, I/O APICs (ioapic). The lapics and ioapics
53636dfb4bSJerry Jelinek  * initially communicated over a dedicated bus, but this has since been
54636dfb4bSJerry Jelinek  * replaced. Each physical core and even hyperthread currently contains its
55636dfb4bSJerry Jelinek  * own local apic, which is not shared. There are a few exceptions for
56636dfb4bSJerry Jelinek  * hyperthreads, but that does not usually concern us.
57636dfb4bSJerry Jelinek  *
58636dfb4bSJerry Jelinek  * Instead of talking directly to 8259 for status, sending End Of Interrupt
59636dfb4bSJerry Jelinek  * (EOI), etc. a microprocessor now communicates directly to the lapic. This
60636dfb4bSJerry Jelinek  * also allows for each microprocessor to be able to have independent controls.
61636dfb4bSJerry Jelinek  * The programming method is different from the 8259. Consumers map the lapic
62636dfb4bSJerry Jelinek  * registers into uncacheable memory to read and manipulate the state.
63636dfb4bSJerry Jelinek  *
64636dfb4bSJerry Jelinek  * The number of addressable interrupt vectors was increased to 256. However
65636dfb4bSJerry Jelinek  * vectors 0-31 are reserved for the processor exception handling, leaving the
66636dfb4bSJerry Jelinek  * remaining vectors for general use. In addition to hardware generated
67636dfb4bSJerry Jelinek  * interrupts, the lapic provides a way for generating inter-processor
68636dfb4bSJerry Jelinek  * interrupts (IPI) which are the basis for CPU cross calls and CPU pokes.
69636dfb4bSJerry Jelinek  *
70636dfb4bSJerry Jelinek  * AMD ended up implementing the Intel APIC architecture in lieu of their work
71636dfb4bSJerry Jelinek  * with Cyrix.
72636dfb4bSJerry Jelinek  *
73636dfb4bSJerry Jelinek  *    Intel x2apic
74636dfb4bSJerry Jelinek  *
75636dfb4bSJerry Jelinek  * The x2apic is an extension to the lapic which started showing up around the
76636dfb4bSJerry Jelinek  * same time as the Sandy Bridge chipsets. It provides a new programming mode
77636dfb4bSJerry Jelinek  * as well as new features. The goal of the x2apic is to solve a few problems
78636dfb4bSJerry Jelinek  * with the previous generation of lapic and the x2apic is backwards compatible
79636dfb4bSJerry Jelinek  * with the previous programming and model. The only downsides to using the
80636dfb4bSJerry Jelinek  * backwards compatibility is that you are not able to take advantage of the new
81636dfb4bSJerry Jelinek  * x2apic features.
82636dfb4bSJerry Jelinek  *
83636dfb4bSJerry Jelinek  *    o The APIC ID is increased from an 8-bit value to a 32-bit value. This
84636dfb4bSJerry Jelinek  *    increases the maximum number of addressable physical processors beyond
85636dfb4bSJerry Jelinek  *    256. This new ID is assembled in a similar manner as the information that
86636dfb4bSJerry Jelinek  *    is obtainable by the extended cpuid topology leaves.
87636dfb4bSJerry Jelinek  *
88636dfb4bSJerry Jelinek  *    o A new means of generating IPIs was introduced.
89636dfb4bSJerry Jelinek  *
90636dfb4bSJerry Jelinek  *    o Instead of memory mapping the registers, the x2apic only allows for
91636dfb4bSJerry Jelinek  *    programming it through a series of wrmsrs. This has important semantic
92636dfb4bSJerry Jelinek  *    side effects. Recall that the registers were previously all mapped to
93636dfb4bSJerry Jelinek  *    uncachable memory which meant that all operations to the local apic were
94636dfb4bSJerry Jelinek  *    serializing instructions. With the switch to using wrmsrs this has been
95636dfb4bSJerry Jelinek  *    relaxed and these operations can no longer be assumed to be serializing
96636dfb4bSJerry Jelinek  *    instructions.
97636dfb4bSJerry Jelinek  *
98636dfb4bSJerry Jelinek  * Note for the rest of this we are only going to concern ourselves with the
99636dfb4bSJerry Jelinek  * apic and x2apic which practically all of i86pc has been using now for
100636dfb4bSJerry Jelinek  * quite some time.
101636dfb4bSJerry Jelinek  *
102636dfb4bSJerry Jelinek  * Interrupt Priority Levels
103636dfb4bSJerry Jelinek  * -------------------------
104636dfb4bSJerry Jelinek  *
105636dfb4bSJerry Jelinek  * On i86pc systems there are a total of fifteen interrupt priority levels
106636dfb4bSJerry Jelinek  * (ipls) which range from 1-15. Level 0 is for normal processing and
107636dfb4bSJerry Jelinek  * non-interrupt processing. To manipulate these values the family of spl
108636dfb4bSJerry Jelinek  * functions (which date back to UNIX on the PDP-11) are used. Specifically,
109636dfb4bSJerry Jelinek  * splr() to raise the priority level and splx() to lower it. One should not
110636dfb4bSJerry Jelinek  * generally call setspl() directly.
111636dfb4bSJerry Jelinek  *
112636dfb4bSJerry Jelinek  * Both i86pc and the supported SPARC platforms honor the same conventions for
113636dfb4bSJerry Jelinek  * the meaning behind these IPLs. The most important IPL is the platform's
114636dfb4bSJerry Jelinek  * LOCK_LEVEL (0xa on i86pc). If a thread is above LOCK_LEVEL it _must_ not
115636dfb4bSJerry Jelinek  * sleep on any synchronization object. The only allowed synchronization
116636dfb4bSJerry Jelinek  * primitive is a mutex that has been specifically initialized to be a spin
117636dfb4bSJerry Jelinek  * lock (see mutex_init(9F)). Another important level is DISP_LEVEL (0xb on
118636dfb4bSJerry Jelinek  * i86pc). You must be at DISP_LEVEL if you want to control the dispatcher.
119636dfb4bSJerry Jelinek  * The XC_HI_PIL is the highest level (0xf) and is used during cross-calls.
120636dfb4bSJerry Jelinek  *
121636dfb4bSJerry Jelinek  * Each interrupt that is registered in the system fires at a specific IPL.
122636dfb4bSJerry Jelinek  * Generally most interrupts fire below LOCK_LEVEL.
123636dfb4bSJerry Jelinek  *
124636dfb4bSJerry Jelinek  * PSM Drivers
125636dfb4bSJerry Jelinek  * -----------
126636dfb4bSJerry Jelinek  *
127636dfb4bSJerry Jelinek  * We currently have three sets of PSM (platform specific module) drivers
128636dfb4bSJerry Jelinek  * available. uppc, pcplusmp, and apix. uppc (uni-processor PC) is the original
129636dfb4bSJerry Jelinek  * driver that interacts with the 8259A and 8254. In general, it is not used
130636dfb4bSJerry Jelinek  * anymore given the prevalence of the apic.
131636dfb4bSJerry Jelinek  *
132636dfb4bSJerry Jelinek  * The system prefers to use the apix driver over the pcplusmp driver. The apix
133636dfb4bSJerry Jelinek  * driver requires HW support for an x2apic. If there is no x2apic HW, apix
134636dfb4bSJerry Jelinek  * will not be used. In general we prefer using the apix driver over the
135636dfb4bSJerry Jelinek  * pcplusmp driver because it gives us much more flexibility with respect to
136636dfb4bSJerry Jelinek  * interrupts. In the apix driver each local apic has its own independent set
137636dfb4bSJerry Jelinek  * of  interrupts, whereas the pcplusmp driver only has a single global set of
138636dfb4bSJerry Jelinek  * interrupts. This is why pcplusmp only supports a finite number of interrupts
139636dfb4bSJerry Jelinek  * per IPL -- generally 16, often less. The apix driver supports using either
140636dfb4bSJerry Jelinek  * the x2apic or the local apic programing modes. The programming mode does not
141636dfb4bSJerry Jelinek  * change the number of interrupts available, just the number of processors
142636dfb4bSJerry Jelinek  * that we can address. For the apix driver, the x2apic mode is enabled if the
143636dfb4bSJerry Jelinek  * system supports interrupt re-mapping, otherwise the module manages the
144636dfb4bSJerry Jelinek  * x2apic in local mode.
145636dfb4bSJerry Jelinek  *
146636dfb4bSJerry Jelinek  * When there is no x2apic present, we default back to the pcplusmp PSM driver.
147636dfb4bSJerry Jelinek  * In general, this is not problematic unless you have more than 256
148636dfb4bSJerry Jelinek  * processors in the machine or you do not have enough interrupts available.
149636dfb4bSJerry Jelinek  *
150636dfb4bSJerry Jelinek  * Controlling Interrupt Generation on i86pc
151636dfb4bSJerry Jelinek  * -----------------------------------------
152636dfb4bSJerry Jelinek  *
153636dfb4bSJerry Jelinek  * There are two different ways to manipulate which interrupts will be
154636dfb4bSJerry Jelinek  * generated on i86pc. Each offers different degrees of control.
155636dfb4bSJerry Jelinek  *
156636dfb4bSJerry Jelinek  * The first is through the flags register (eflags and rflags on i386 and amd64
157636dfb4bSJerry Jelinek  * respectively). The IF bit determines whether or not interrupts are enabled
158636dfb4bSJerry Jelinek  * or disabled. This is manipulated in one of several ways. The most common way
159636dfb4bSJerry Jelinek  * is through the cli and sti instructions. These clear the IF flag and set it,
160636dfb4bSJerry Jelinek  * respectively, for the current processor. The other common way is through the
161636dfb4bSJerry Jelinek  * use of the intr_clear and intr_restore functions.
162636dfb4bSJerry Jelinek  *
163636dfb4bSJerry Jelinek  * Assuming interrupts are not blocked by the IF flag, then the second form is
164636dfb4bSJerry Jelinek  * through the Processor-Priority Register (PPR). The PPR is used to determine
165636dfb4bSJerry Jelinek  * whether or not a pending interrupt should be delivered. If the ipl of the
166636dfb4bSJerry Jelinek  * new interrupt is higher than the current value in the PPR, then the lapic
167636dfb4bSJerry Jelinek  * will either deliver it immediately (if interrupts are not in progress) or it
168636dfb4bSJerry Jelinek  * will deliver it once the current interrupt processing has issued an EOI. The
169636dfb4bSJerry Jelinek  * highest unmasked interrupt will be the one delivered.
170636dfb4bSJerry Jelinek  *
171636dfb4bSJerry Jelinek  * The PPR register is based upon the max of the following two registers in the
172636dfb4bSJerry Jelinek  * lapic, the TPR register (also known as CR8 on amd64) that can be used to
173636dfb4bSJerry Jelinek  * mask interrupt levels, and the current vector. Because the pcplusmp module
174636dfb4bSJerry Jelinek  * always sets TPR appropriately early in the do_interrupt path, we can usually
175636dfb4bSJerry Jelinek  * just think that the PPR is the TPR. The pcplusmp module also issues an EOI
176636dfb4bSJerry Jelinek  * once it has set the TPR, so higher priority interrupts can come in while
177636dfb4bSJerry Jelinek  * we're servicing a lower priority interrupt.
178636dfb4bSJerry Jelinek  *
179636dfb4bSJerry Jelinek  * Handling Interrupts
180636dfb4bSJerry Jelinek  * -------------------
181636dfb4bSJerry Jelinek  *
182636dfb4bSJerry Jelinek  * Interrupts can be broken down into three categories based on priority and
183636dfb4bSJerry Jelinek  * source:
184636dfb4bSJerry Jelinek  *
185636dfb4bSJerry Jelinek  *   o High level interrupts
186636dfb4bSJerry Jelinek  *   o Low level hardware interrupts
187636dfb4bSJerry Jelinek  *   o Low level software interrupts
188636dfb4bSJerry Jelinek  *
189636dfb4bSJerry Jelinek  *   High Level Interrupts
190636dfb4bSJerry Jelinek  *
191636dfb4bSJerry Jelinek  * High level interrupts encompasses both hardware-sourced and software-sourced
192636dfb4bSJerry Jelinek  * interrupts. Examples of high level hardware interrupts include the serial
193636dfb4bSJerry Jelinek  * console. High level software-sourced interrupts are still delivered through
194636dfb4bSJerry Jelinek  * the local apic through IPIs. This is primarily cross calls.
195636dfb4bSJerry Jelinek  *
196636dfb4bSJerry Jelinek  * When a high level interrupt comes in, we will raise the SPL and then pin the
197636dfb4bSJerry Jelinek  * current lwp to the processor. We will use its lwp, but our own interrupt
198636dfb4bSJerry Jelinek  * stack and process the high level interrupt in-situ. These handlers are
199636dfb4bSJerry Jelinek  * designed to be very short in nature and cannot go to sleep, only block on a
200636dfb4bSJerry Jelinek  * spin lock. If the interrupt has a lot of work to do, it must generate a
201636dfb4bSJerry Jelinek  * low-priority software interrupt that will be processed later.
202636dfb4bSJerry Jelinek  *
203636dfb4bSJerry Jelinek  *   Low level hardware interrupts
204636dfb4bSJerry Jelinek  *
205636dfb4bSJerry Jelinek  * Low level hardware interrupts start off like their high-level cousins. The
206636dfb4bSJerry Jelinek  * current CPU contains a number of kernel threads (kthread_t) that can be used
207636dfb4bSJerry Jelinek  * to process low level interrupts. These are shared between both low level
208636dfb4bSJerry Jelinek  * hardware and software interrupts. Note that while we run with our
209636dfb4bSJerry Jelinek  * kthread_t, we borrow the pinned threads lwp_t until such a time as we hit a
210636dfb4bSJerry Jelinek  * synchronization object. If we hit one and need to sleep, then the scheduler
211636dfb4bSJerry Jelinek  * will instead create the rest of what we need.
212636dfb4bSJerry Jelinek  *
213636dfb4bSJerry Jelinek  *   Low level software interrupts
214636dfb4bSJerry Jelinek  *
215636dfb4bSJerry Jelinek  * Low level software interrupts are handled in a similar way as hardware
216636dfb4bSJerry Jelinek  * interrupts, but the notification vector is different. Each CPU has a bitmask
217636dfb4bSJerry Jelinek  * of pending software interrupts. We can notify a CPU to process software
218636dfb4bSJerry Jelinek  * interrupts through a specific trap vector as well as through several
219636dfb4bSJerry Jelinek  * checks that are performed throughout the code. These checks will look at
220636dfb4bSJerry Jelinek  * processing software interrupts as we lower our spl.
221636dfb4bSJerry Jelinek  *
222636dfb4bSJerry Jelinek  * We attempt to process the highest pending software interrupt that we can
223636dfb4bSJerry Jelinek  * which is greater than our current IPL. If none currently exist, then we move
224636dfb4bSJerry Jelinek  * on. We process a software interrupt in a similar fashion to a hardware
225636dfb4bSJerry Jelinek  * interrupt.
226636dfb4bSJerry Jelinek  *
227636dfb4bSJerry Jelinek  * Traditional Interrupt Flow
228636dfb4bSJerry Jelinek  * --------------------------
229636dfb4bSJerry Jelinek  *
230636dfb4bSJerry Jelinek  * The following diagram tracks the flow of the traditional uppc and pcplusmp
231636dfb4bSJerry Jelinek  * interrupt handlers. The apix driver has its own version of do_interrupt().
232636dfb4bSJerry Jelinek  * We come into the interrupt handler with all interrupts masked by the IF
233636dfb4bSJerry Jelinek  * flag. This is because we set up the handler using an interrupt-gate, which
234636dfb4bSJerry Jelinek  * is defined architecturally to have cleared the IF flag for us.
235636dfb4bSJerry Jelinek  *
236636dfb4bSJerry Jelinek  * +--------------+    +----------------+    +-----------+
237636dfb4bSJerry Jelinek  * | _interrupt() |--->| do_interrupt() |--->| *setlvl() |
238636dfb4bSJerry Jelinek  * +--------------+    +----------------+    +-----------+
239636dfb4bSJerry Jelinek  *                       |      |     |
240636dfb4bSJerry Jelinek  *                       |      |     |
241636dfb4bSJerry Jelinek  *              low-level|      |     | softint
242636dfb4bSJerry Jelinek  *                HW int |      |     +---------------------------------------+
243636dfb4bSJerry Jelinek  * +--------------+      |      |                                             |
244636dfb4bSJerry Jelinek  * | intr_thread_ |<-----+      | hi-level int                                |
245636dfb4bSJerry Jelinek  * | prolog()     |             |    +----------+                             |
246636dfb4bSJerry Jelinek  * +--------------+             +--->| hilevel_ |      Not on intr stack      |
247636dfb4bSJerry Jelinek  *       |                           | intr_    |-----------------+           |
248636dfb4bSJerry Jelinek  *       |                           | prolog() |                 |           |
249636dfb4bSJerry Jelinek  * +------------+                    +----------+                 |           |
250636dfb4bSJerry Jelinek  * | switch_sp_ |                        | On intr                v           |
251636dfb4bSJerry Jelinek  * | and_call() |                        | Stack          +------------+      |
252636dfb4bSJerry Jelinek  * +------------+                        |                | switch_sp_ |      |
253636dfb4bSJerry Jelinek  *       |                               v                | and_call() |      |
254636dfb4bSJerry Jelinek  *       v                             +-----------+      +------------+      |
255636dfb4bSJerry Jelinek  * +-----------+                       | dispatch_ |             |            |
256636dfb4bSJerry Jelinek  * | dispatch_ |   +-------------------| hilevel() |<------------+            |
257636dfb4bSJerry Jelinek  * | hardint() |   |                   +-----------+                          |
258636dfb4bSJerry Jelinek  * +-----------+   |                                                          |
259636dfb4bSJerry Jelinek  *       |         v                                                          |
260636dfb4bSJerry Jelinek  *       |     +-----+  +----------------------+  +-----+  hi-level           |
261636dfb4bSJerry Jelinek  *       +---->| sti |->| av_dispatch_autovect |->| cli |---------+           |
262636dfb4bSJerry Jelinek  *             +-----+  +----------------------+  +-----+         |           |
263636dfb4bSJerry Jelinek  *                                |                |              |           |
264636dfb4bSJerry Jelinek  *                                v                |              |           |
265636dfb4bSJerry Jelinek  *                         +----------+            |              |           |
266636dfb4bSJerry Jelinek  *                         | for each |            |              |           |
267636dfb4bSJerry Jelinek  *                         | handler  |            |              |           |
268636dfb4bSJerry Jelinek  *                         |  *intr() |            |              v           |
269636dfb4bSJerry Jelinek  * +--------------+        +----------+            |      +----------------+  |
270636dfb4bSJerry Jelinek  * | intr_thread_ |                      low-level |      | hilevel_intr_  |  |
271636dfb4bSJerry Jelinek  * | epilog()     |<-------------------------------+      | epilog()       |  |
272636dfb4bSJerry Jelinek  * +--------------+                                       +----------------+  |
273636dfb4bSJerry Jelinek  *   |       |                                                   |            |
274636dfb4bSJerry Jelinek  *   |       +----------------------v      v---------------------+            |
275636dfb4bSJerry Jelinek  *   |                           +------------+                               |
276636dfb4bSJerry Jelinek  *   |   +---------------------->| *setlvlx() |                               |
277636dfb4bSJerry Jelinek  *   |   |                       +------------+                               |
278636dfb4bSJerry Jelinek  *   |   |                              |                                     |
279636dfb4bSJerry Jelinek  *   |   |                              v                                     |
280636dfb4bSJerry Jelinek  *   |   |      +--------+     +------------------+      +-------------+      |
281636dfb4bSJerry Jelinek  *   |   |      | return |<----| softint pending? |----->| dosoftint() |<-----+
282636dfb4bSJerry Jelinek  *   |   |      +--------+  no +------------------+ yes  +-------------+
283636dfb4bSJerry Jelinek  *   |   |           ^                                      |     |
284636dfb4bSJerry Jelinek  *   |   |           |  softint pil too low                 |     |
285636dfb4bSJerry Jelinek  *   |   |           +--------------------------------------+     |
286636dfb4bSJerry Jelinek  *   |   |                                                        v
287636dfb4bSJerry Jelinek  *   |   |    +-----------+      +------------+          +-----------+
288636dfb4bSJerry Jelinek  *   |   |    | dispatch_ |<-----| switch_sp_ |<---------| *setspl() |
289636dfb4bSJerry Jelinek  *   |   |    | softint() |      | and_call() |          +-----------+
290636dfb4bSJerry Jelinek  *   |   |    +-----------+      +------------+
291636dfb4bSJerry Jelinek  *   |   |        |
292636dfb4bSJerry Jelinek  *   |   |        v
293636dfb4bSJerry Jelinek  *   |   |      +-----+  +----------------------+  +-----+  +------------+
294636dfb4bSJerry Jelinek  *   |   |      | sti |->| av_dispatch_autovect |->| cli |->| dosoftint_ |
295636dfb4bSJerry Jelinek  *   |   |      +-----+  +----------------------+  +-----+  | epilog()   |
296636dfb4bSJerry Jelinek  *   |   |                                                  +------------+
297636dfb4bSJerry Jelinek  *   |   |                                                    |     |
298636dfb4bSJerry Jelinek  *   |   +----------------------------------------------------+     |
299636dfb4bSJerry Jelinek  *   v                                                              |
300636dfb4bSJerry Jelinek  * +-----------+                                                    |
301636dfb4bSJerry Jelinek  * | interrupt |                                                    |
302636dfb4bSJerry Jelinek  * | thread    |<---------------------------------------------------+
303636dfb4bSJerry Jelinek  * | blocked   |
304636dfb4bSJerry Jelinek  * +-----------+
305636dfb4bSJerry Jelinek  *      |
306636dfb4bSJerry Jelinek  *      v
307636dfb4bSJerry Jelinek  *  +----------------+  +------------+  +-----------+  +-------+  +---------+
308636dfb4bSJerry Jelinek  *  | set_base_spl() |->| *setlvlx() |->| splhigh() |->| sti() |->| swtch() |
309636dfb4bSJerry Jelinek  *  +----------------+  +------------+  +-----------+  +-------+  +---------+
310636dfb4bSJerry Jelinek  *
311636dfb4bSJerry Jelinek  *    Calls made on Interrupt Stacks and Epilogue routines
312636dfb4bSJerry Jelinek  *
313636dfb4bSJerry Jelinek  * We use the switch_sp_and_call() assembly routine to switch our sp to the
314636dfb4bSJerry Jelinek  * interrupt stacks and then call the appropriate dispatch function. In the
315636dfb4bSJerry Jelinek  * case of interrupts which may block, softints and hardints, we always ensure
316636dfb4bSJerry Jelinek  * that we are still on the interrupt thread when we call the epilog routine.
317636dfb4bSJerry Jelinek  * This is not just important, it's necessary. If the interrupt thread blocked,
318636dfb4bSJerry Jelinek  * we won't return from our switch_sp_and_call() function and instead we'll go
319636dfb4bSJerry Jelinek  * through and set ourselves up to swtch() directly.
320636dfb4bSJerry Jelinek  *
321636dfb4bSJerry Jelinek  * New Interrupt Flow
322636dfb4bSJerry Jelinek  * ------------------
323636dfb4bSJerry Jelinek  *
324636dfb4bSJerry Jelinek  * The apix module has its own interrupt path. This is done for various
325636dfb4bSJerry Jelinek  * reasons. The first is that rather than having global interrupt vectors, we
326636dfb4bSJerry Jelinek  * now have per-cpu vectors.
327636dfb4bSJerry Jelinek  *
328636dfb4bSJerry Jelinek  * The other substantial change is that the apix design does not use the TPR to
329636dfb4bSJerry Jelinek  * mask interrupts below the current level. In fact, except for one special
330636dfb4bSJerry Jelinek  * case, it does not use the TPR at all. Instead, it only uses the IF flag
331636dfb4bSJerry Jelinek  * (cli/sti) to either block all interrupts or allow any interrupts to come in.
332636dfb4bSJerry Jelinek  * The design is such that when interrupts are allowed to come in, if we are
333636dfb4bSJerry Jelinek  * currently servicing a higher priority interupt, the new interrupt is treated
334636dfb4bSJerry Jelinek  * as pending and serviced later. Specifically, in the pcplusmp module's
335636dfb4bSJerry Jelinek  * apic_intr_enter() the code masks interrupts at or below the current
336636dfb4bSJerry Jelinek  * IPL using the TPR before sending EOI, whereas the apix module's
337636dfb4bSJerry Jelinek  * apix_intr_enter() simply sends EOI.
338636dfb4bSJerry Jelinek  *
339636dfb4bSJerry Jelinek  * The one special case where the apix code uses the TPR is when it calls
340636dfb4bSJerry Jelinek  * through the apic_reg_ops function pointer apic_write_task_reg in
341636dfb4bSJerry Jelinek  * apix_init_intr() to initially mask all levels and then finally to enable all
342636dfb4bSJerry Jelinek  * levels.
343636dfb4bSJerry Jelinek  *
344636dfb4bSJerry Jelinek  * Recall that we come into the interrupt handler with all interrupts masked
345636dfb4bSJerry Jelinek  * by the IF flag. This is because we set up the handler using an
346636dfb4bSJerry Jelinek  * interrupt-gate which is defined architecturally to have cleared the IF flag
347636dfb4bSJerry Jelinek  * for us.
348636dfb4bSJerry Jelinek  *
349636dfb4bSJerry Jelinek  * +--------------+    +---------------------+
350636dfb4bSJerry Jelinek  * | _interrupt() |--->| apix_do_interrupt() |
351636dfb4bSJerry Jelinek  * +--------------+    +---------------------+
352636dfb4bSJerry Jelinek  *                               |
353636dfb4bSJerry Jelinek  *                hard int? +----+--------+ softint?
354636dfb4bSJerry Jelinek  *                          |             | (but no low-level looping)
355636dfb4bSJerry Jelinek  *                   +-----------+        |
356636dfb4bSJerry Jelinek  *                   | *setlvl() |        |
357636dfb4bSJerry Jelinek  * +---------+       +-----------+        +----------------------------------+
358636dfb4bSJerry Jelinek  * |apix_add_|    check IPL |                                                |
359636dfb4bSJerry Jelinek  * |pending_ |<-------------+------+----------------------+                  |
360636dfb4bSJerry Jelinek  * |hardint()|        low-level int|          hi-level int|                  |
361636dfb4bSJerry Jelinek  * +---------+                     v                      v                  |
362636dfb4bSJerry Jelinek  *     | check IPL       +-----------------+     +---------------+           |
363636dfb4bSJerry Jelinek  *  +--+-----+           | apix_intr_      |     | apix_hilevel_ |           |
364636dfb4bSJerry Jelinek  *  |        |           | thread_prolog() |     | intr_prolog() |           |
365636dfb4bSJerry Jelinek  *  |      return        +-----------------+     +---------------+           |
366636dfb4bSJerry Jelinek  *  |                         |                    | On intr                 |
367636dfb4bSJerry Jelinek  *  |                   +------------+             | stack?  +------------+  |
368636dfb4bSJerry Jelinek  *  |                   | switch_sp_ |             +---------| switch_sp_ |  |
369636dfb4bSJerry Jelinek  *  |                   | and_call() |             |         | and_call() |  |
370636dfb4bSJerry Jelinek  *  |                   +------------+             |         +------------+  |
371636dfb4bSJerry Jelinek  *  |                         |                    |          |              |
372636dfb4bSJerry Jelinek  *  |                   +----------------+     +----------------+            |
373636dfb4bSJerry Jelinek  *  |                   | apix_dispatch_ |     | apix_dispatch_ |            |
374636dfb4bSJerry Jelinek  *  |                   | lowlevel()     |     | hilevel()      |            |
375636dfb4bSJerry Jelinek  *  |                   +----------------+     +----------------+            |
376636dfb4bSJerry Jelinek  *  |                                |             |                         |
377636dfb4bSJerry Jelinek  *  |                                v             v                         |
378636dfb4bSJerry Jelinek  *  |                       +-------------------------+                      |
379636dfb4bSJerry Jelinek  *  |                       |apix_dispatch_by_vector()|----+                 |
380636dfb4bSJerry Jelinek  *  |                       +-------------------------+    |                 |
381636dfb4bSJerry Jelinek  *  |               !XC_HI_PIL|         |         |        |                 |
382636dfb4bSJerry Jelinek  *  |                       +---+   +-------+   +---+      |                 |
383636dfb4bSJerry Jelinek  *  |                       |sti|   |*intr()|   |cli|      |                 |
384636dfb4bSJerry Jelinek  *  |                       +---+   +-------+   +---+      |  hi-level?      |
385636dfb4bSJerry Jelinek  *  |                          +---------------------------+----+            |
386636dfb4bSJerry Jelinek  *  |                          v                low-level?      v            |
387636dfb4bSJerry Jelinek  *  |                  +----------------+               +----------------+   |
388636dfb4bSJerry Jelinek  *  |                  | apix_intr_     |               | apix_hilevel_  |   |
389636dfb4bSJerry Jelinek  *  |                  | thread_epilog()|               | intr_epilog()  |   |
390636dfb4bSJerry Jelinek  *  |                  +----------------+               +----------------+   |
391636dfb4bSJerry Jelinek  *  |                          |                                |            |
392636dfb4bSJerry Jelinek  *  |        v-----------------+--------------------------------+            |
393636dfb4bSJerry Jelinek  *  |  +------------+                                                        |
394636dfb4bSJerry Jelinek  *  |  | *setlvlx() |   +----------------------------------------------------+
395636dfb4bSJerry Jelinek  *  |  +------------+   |
396636dfb4bSJerry Jelinek  *  |      |            |            +--------------------------------+ low
397636dfb4bSJerry Jelinek  *  v      v     v------+            v                                | level
398636dfb4bSJerry Jelinek  * +------------------+      +------------------+      +-----------+  | pending?
399636dfb4bSJerry Jelinek  * | apix_do_pending_ |----->| apix_do_pending_ |----->| apix_do_  |--+
400636dfb4bSJerry Jelinek  * | hilevel()        |      | hardint()        |      | softint() |  |
401636dfb4bSJerry Jelinek  * +------------------+      +------------------+      +-----------+    return
402636dfb4bSJerry Jelinek  *     |                       |                         |
403636dfb4bSJerry Jelinek  *     | while pending         | while pending           | while pending
404636dfb4bSJerry Jelinek  *     | hi-level              | low-level               | softint
405636dfb4bSJerry Jelinek  *     |                       |                         |
406636dfb4bSJerry Jelinek  *  +---------------+        +-----------------+       +-----------------+
407636dfb4bSJerry Jelinek  *  | apix_hilevel_ |        | apix_intr_      |       | apix_do_        |
408636dfb4bSJerry Jelinek  *  | intr_prolog() |        | thread_prolog() |       | softint_prolog()|
409636dfb4bSJerry Jelinek  *  +---------------+        +-----------------+       +-----------------+
410636dfb4bSJerry Jelinek  *     | On intr                       |                      |
411636dfb4bSJerry Jelinek  *     | stack? +------------+    +------------+        +------------+
412636dfb4bSJerry Jelinek  *     +--------| switch_sp_ |    | switch_sp_ |        | switch_sp_ |
413636dfb4bSJerry Jelinek  *     |        | and_call() |    | and_call() |        | and_call() |
414636dfb4bSJerry Jelinek  *     |        +------------+    +------------+        +------------+
415636dfb4bSJerry Jelinek  *     |           |                   |                      |
416636dfb4bSJerry Jelinek  *  +------------------+   +------------------+   +------------------------+
417636dfb4bSJerry Jelinek  *  | apix_dispatch_   |   | apix_dispatch_   |   | apix_dispatch_softint()|
418636dfb4bSJerry Jelinek  *  | pending_hilevel()|   | pending_hardint()|   +------------------------+
419636dfb4bSJerry Jelinek  *  +------------------+   +------------------+      |    |      |      |
420636dfb4bSJerry Jelinek  *    |         |           |         |              |    |      |      |
421636dfb4bSJerry Jelinek  *    | +----------------+  | +----------------+     |    |      |      |
422636dfb4bSJerry Jelinek  *    | | apix_hilevel_  |  | | apix_intr_     |     |    |      |      |
423636dfb4bSJerry Jelinek  *    | | intr_epilog()  |  | | thread_epilog()|     |    |      |      |
424636dfb4bSJerry Jelinek  *    | +----------------+  | +----------------+     |    |      |      |
425636dfb4bSJerry Jelinek  *    |         |           |       |                |    |      |      |
426636dfb4bSJerry Jelinek  *    |   +------------+    |  +----------+   +------+    |      |      |
427636dfb4bSJerry Jelinek  *    |   | *setlvlx() |    |  |*setlvlx()|   |           |      |      |
428636dfb4bSJerry Jelinek  *    |   +------------+    |  +----------+   |   +----------+   |   +---------+
429636dfb4bSJerry Jelinek  *    |                     |               +---+ |av_       | +---+ |apix_do_ |
430636dfb4bSJerry Jelinek  * +---------------------------------+      |sti| |dispatch_ | |cli| |softint_ |
431636dfb4bSJerry Jelinek  * | apix_dispatch_pending_autovect()|      +---+ |softvect()| +---+ |epilog() |
432636dfb4bSJerry Jelinek  * +---------------------------------+            +----------+       +---------+
433636dfb4bSJerry Jelinek  *  |!XC_HI_PIL  |       |         |                    |
434636dfb4bSJerry Jelinek  * +---+  +-------+    +---+  +----------+          +-------+
435636dfb4bSJerry Jelinek  * |sti|  |*intr()|    |cli|  |apix_post_|          |*intr()|
436636dfb4bSJerry Jelinek  * +---+  +-------+    +---+  |hardint() |          +-------+
437636dfb4bSJerry Jelinek  *                            +----------+
4387c478bd9Sstevel@tonic-gate  */
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
441fb2caebeSRandy Fishel #include <sys/cpu_event.h>
4427c478bd9Sstevel@tonic-gate #include <sys/regset.h>
4437c478bd9Sstevel@tonic-gate #include <sys/psw.h>
4447c478bd9Sstevel@tonic-gate #include <sys/types.h>
4457c478bd9Sstevel@tonic-gate #include <sys/thread.h>
4467c478bd9Sstevel@tonic-gate #include <sys/systm.h>
4477c478bd9Sstevel@tonic-gate #include <sys/segments.h>
4487c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
4497c478bd9Sstevel@tonic-gate #include <sys/trap.h>
4507c478bd9Sstevel@tonic-gate #include <sys/ftrace.h>
4517c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
4527c478bd9Sstevel@tonic-gate #include <sys/clock.h>
4537c478bd9Sstevel@tonic-gate #include <sys/panic.h>
4547c478bd9Sstevel@tonic-gate #include <sys/disp.h>
4557c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
4567c478bd9Sstevel@tonic-gate #include <sys/stack.h>
4577c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
4587c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
4597c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
4607c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
4617c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
4627c478bd9Sstevel@tonic-gate #include <sys/zone.h>
4637c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
464ae115bc7Smrj #include <sys/archsystm.h>
465ae115bc7Smrj #include <sys/machsystm.h>
466ae115bc7Smrj #include <sys/ontrap.h>
467ae115bc7Smrj #include <sys/x86_archext.h>
468ae115bc7Smrj #include <sys/promif.h>
469*c3377ee9SJohn Levon #include <sys/smt.h>
47095c0a3c8Sjosephb #include <vm/hat_i86.h>
471843e1988Sjohnlev #if defined(__xpv)
472843e1988Sjohnlev #include <sys/hypervisor.h>
473843e1988Sjohnlev #endif
474843e1988Sjohnlev 
475455e370cSJohn Levon /* If these fail, then the padding numbers in machcpuvar.h are wrong. */
476455e370cSJohn Levon #if !defined(__xpv)
477455e370cSJohn Levon #define	MCOFF(member)	\
478455e370cSJohn Levon 	(offsetof(cpu_t, cpu_m) + offsetof(struct machcpu, member))
479455e370cSJohn Levon CTASSERT(MCOFF(mcpu_pad) == MACHCPU_SIZE);
480455e370cSJohn Levon CTASSERT(MCOFF(mcpu_pad2) == MMU_PAGESIZE);
481455e370cSJohn Levon CTASSERT((MCOFF(mcpu_kpti) & 0xF) == 0);
48274ecdb51SJohn Levon CTASSERT(((sizeof (struct kpti_frame)) & 0xF) == 0);
48374ecdb51SJohn Levon CTASSERT((offsetof(struct kpti_frame, kf_tr_rsp) & 0xF) == 0);
484455e370cSJohn Levon CTASSERT(MCOFF(mcpu_pad3) < 2 * MMU_PAGESIZE);
48574ecdb51SJohn Levon #endif
486843e1988Sjohnlev 
487843e1988Sjohnlev #if defined(__xpv) && defined(DEBUG)
488843e1988Sjohnlev 
489843e1988Sjohnlev /*
490843e1988Sjohnlev  * This panic message is intended as an aid to interrupt debugging.
491843e1988Sjohnlev  *
492843e1988Sjohnlev  * The associated assertion tests the condition of enabling
493843e1988Sjohnlev  * events when events are already enabled.  The implication
494843e1988Sjohnlev  * being that whatever code the programmer thought was
495843e1988Sjohnlev  * protected by having events disabled until the second
496843e1988Sjohnlev  * enable happened really wasn't protected at all ..
497843e1988Sjohnlev  */
4987c478bd9Sstevel@tonic-gate 
499843e1988Sjohnlev int stistipanic = 1;	/* controls the debug panic check */
500843e1988Sjohnlev const char *stistimsg = "stisti";
501843e1988Sjohnlev ulong_t laststi[NCPU];
502843e1988Sjohnlev 
503843e1988Sjohnlev /*
504843e1988Sjohnlev  * This variable tracks the last place events were disabled on each cpu
505fb2caebeSRandy Fishel  * it assists in debugging when asserts that interrupts are enabled trip.
506843e1988Sjohnlev  */
507843e1988Sjohnlev ulong_t lastcli[NCPU];
508843e1988Sjohnlev 
509843e1988Sjohnlev #endif
5107c478bd9Sstevel@tonic-gate 
5117ff178cdSJimmy Vetayases void do_interrupt(struct regs *rp, trap_trace_rec_t *ttp);
5127ff178cdSJimmy Vetayases 
5137ff178cdSJimmy Vetayases void (*do_interrupt_common)(struct regs *, trap_trace_rec_t *) = do_interrupt;
5147ff178cdSJimmy Vetayases uintptr_t (*get_intr_handler)(int, short) = NULL;
5157ff178cdSJimmy Vetayases 
5167c478bd9Sstevel@tonic-gate /*
517ae115bc7Smrj  * Set cpu's base SPL level to the highest active interrupt level
5187c478bd9Sstevel@tonic-gate  */
519ae115bc7Smrj void
set_base_spl(void)520ae115bc7Smrj set_base_spl(void)
5217c478bd9Sstevel@tonic-gate {
522ae115bc7Smrj 	struct cpu *cpu = CPU;
523ae115bc7Smrj 	uint16_t active = (uint16_t)cpu->cpu_intr_actv;
5247c478bd9Sstevel@tonic-gate 
525ae115bc7Smrj 	cpu->cpu_base_spl = active == 0 ? 0 : bsrw_insn(active);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate  * Do all the work necessary to set up the cpu and thread structures
5307c478bd9Sstevel@tonic-gate  * to dispatch a high-level interrupt.
5317c478bd9Sstevel@tonic-gate  *
5327c478bd9Sstevel@tonic-gate  * Returns 0 if we're -not- already on the high-level interrupt stack,
5337c478bd9Sstevel@tonic-gate  * (and *must* switch to it), non-zero if we are already on that stack.
5347c478bd9Sstevel@tonic-gate  *
5357c478bd9Sstevel@tonic-gate  * Called with interrupts masked.
5367c478bd9Sstevel@tonic-gate  * The 'pil' is already set to the appropriate level for rp->r_trapno.
5377c478bd9Sstevel@tonic-gate  */
538ae115bc7Smrj static int
hilevel_intr_prolog(struct cpu * cpu,uint_t pil,uint_t oldpil,struct regs * rp)5397c478bd9Sstevel@tonic-gate hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp)
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
5427c478bd9Sstevel@tonic-gate 	uint_t mask;
543eda89462Sesolom 	hrtime_t intrtime;
544ae115bc7Smrj 	hrtime_t now = tsc_read();
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	ASSERT(pil > LOCK_LEVEL);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	if (pil == CBE_HIGH_PIL) {
5497c478bd9Sstevel@tonic-gate 		cpu->cpu_profile_pil = oldpil;
5507c478bd9Sstevel@tonic-gate 		if (USERMODE(rp->r_cs)) {
5517c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_pc = 0;
5527c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_upc = rp->r_pc;
553b9e93c10SJonathan Haslam 			cpu->cpu_cpcprofile_pc = 0;
554b9e93c10SJonathan Haslam 			cpu->cpu_cpcprofile_upc = rp->r_pc;
5557c478bd9Sstevel@tonic-gate 		} else {
5567c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_pc = rp->r_pc;
5577c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_upc = 0;
558b9e93c10SJonathan Haslam 			cpu->cpu_cpcprofile_pc = rp->r_pc;
559b9e93c10SJonathan Haslam 			cpu->cpu_cpcprofile_upc = 0;
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
5647c478bd9Sstevel@tonic-gate 	if (mask != 0) {
5657c478bd9Sstevel@tonic-gate 		int nestpil;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		/*
5687c478bd9Sstevel@tonic-gate 		 * We have interrupted another high-level interrupt.
5697c478bd9Sstevel@tonic-gate 		 * Load starting timestamp, compute interval, update
5707c478bd9Sstevel@tonic-gate 		 * cumulative counter.
5717c478bd9Sstevel@tonic-gate 		 */
5727c478bd9Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
5737c478bd9Sstevel@tonic-gate 		ASSERT(nestpil < pil);
574ae115bc7Smrj 		intrtime = now -
5757c478bd9Sstevel@tonic-gate 		    mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)];
5767a364d25Sschwartz 		mcpu->intrstat[nestpil][0] += intrtime;
577eda89462Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
5787c478bd9Sstevel@tonic-gate 		/*
5797c478bd9Sstevel@tonic-gate 		 * Another high-level interrupt is active below this one, so
5807c478bd9Sstevel@tonic-gate 		 * there is no need to check for an interrupt thread.  That
5817c478bd9Sstevel@tonic-gate 		 * will be done by the lowest priority high-level interrupt
5827c478bd9Sstevel@tonic-gate 		 * active.
5837c478bd9Sstevel@tonic-gate 		 */
5847c478bd9Sstevel@tonic-gate 	} else {
5857c478bd9Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		/*
5887c478bd9Sstevel@tonic-gate 		 * See if we are interrupting a low-level interrupt thread.
5897c478bd9Sstevel@tonic-gate 		 * If so, account for its time slice only if its time stamp
5907c478bd9Sstevel@tonic-gate 		 * is non-zero.
5917c478bd9Sstevel@tonic-gate 		 */
5927c478bd9Sstevel@tonic-gate 		if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) {
593ae115bc7Smrj 			intrtime = now - t->t_intr_start;
5947a364d25Sschwartz 			mcpu->intrstat[t->t_pil][0] += intrtime;
595eda89462Sesolom 			cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
5967c478bd9Sstevel@tonic-gate 			t->t_intr_start = 0;
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
600*c3377ee9SJohn Levon 	smt_begin_intr(pil);
601455e370cSJohn Levon 
6027c478bd9Sstevel@tonic-gate 	/*
6037c478bd9Sstevel@tonic-gate 	 * Store starting timestamp in CPU structure for this PIL.
6047c478bd9Sstevel@tonic-gate 	 */
605ae115bc7Smrj 	mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = now;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (pil == 15) {
6107c478bd9Sstevel@tonic-gate 		/*
6117c478bd9Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
6127c478bd9Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
6137c478bd9Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
6147c478bd9Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
6157c478bd9Sstevel@tonic-gate 		 */
6167c478bd9Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
6177c478bd9Sstevel@tonic-gate 		(*refcntp)++;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate /*
6287c478bd9Sstevel@tonic-gate  * Does most of the work of returning from a high level interrupt.
6297c478bd9Sstevel@tonic-gate  *
6307c478bd9Sstevel@tonic-gate  * Returns 0 if there are no more high level interrupts (in which
6317c478bd9Sstevel@tonic-gate  * case we must switch back to the interrupted thread stack) or
6327c478bd9Sstevel@tonic-gate  * non-zero if there are more (in which case we should stay on it).
6337c478bd9Sstevel@tonic-gate  *
6347c478bd9Sstevel@tonic-gate  * Called with interrupts masked
6357c478bd9Sstevel@tonic-gate  */
636ae115bc7Smrj static int
hilevel_intr_epilog(struct cpu * cpu,uint_t pil,uint_t oldpil,uint_t vecnum)6377c478bd9Sstevel@tonic-gate hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
6407c478bd9Sstevel@tonic-gate 	uint_t mask;
641eda89462Sesolom 	hrtime_t intrtime;
642ae115bc7Smrj 	hrtime_t now = tsc_read();
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	ASSERT(mcpu->mcpu_pri == pil);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (pil == 15) {
6517c478bd9Sstevel@tonic-gate 		/*
6527c478bd9Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
6537c478bd9Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
6547c478bd9Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
6557c478bd9Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
6567c478bd9Sstevel@tonic-gate 		 */
6577c478bd9Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		ASSERT(*refcntp > 0);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 		if (--(*refcntp) == 0)
6627c478bd9Sstevel@tonic-gate 			cpu->cpu_intr_actv &= ~(1 << pil);
6637c478bd9Sstevel@tonic-gate 	} else {
6647c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_actv &= ~(1 << pil);
6657c478bd9Sstevel@tonic-gate 	}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0);
6687c478bd9Sstevel@tonic-gate 
669ae115bc7Smrj 	intrtime = now - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)];
6707a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
671eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/*
6747c478bd9Sstevel@tonic-gate 	 * Check for lower-pil nested high-level interrupt beneath
6757c478bd9Sstevel@tonic-gate 	 * current one.  If so, place a starting timestamp in its
6767c478bd9Sstevel@tonic-gate 	 * pil_high_start entry.
6777c478bd9Sstevel@tonic-gate 	 */
6787c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
6797c478bd9Sstevel@tonic-gate 	if (mask != 0) {
6807c478bd9Sstevel@tonic-gate 		int nestpil;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		/*
6837c478bd9Sstevel@tonic-gate 		 * find PIL of nested interrupt
6847c478bd9Sstevel@tonic-gate 		 */
6857c478bd9Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
6867c478bd9Sstevel@tonic-gate 		ASSERT(nestpil < pil);
687ae115bc7Smrj 		mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = now;
6887c478bd9Sstevel@tonic-gate 		/*
6897c478bd9Sstevel@tonic-gate 		 * (Another high-level interrupt is active below this one,
6907c478bd9Sstevel@tonic-gate 		 * so there is no need to check for an interrupt
6917c478bd9Sstevel@tonic-gate 		 * thread.  That will be done by the lowest priority
6927c478bd9Sstevel@tonic-gate 		 * high-level interrupt active.)
6937c478bd9Sstevel@tonic-gate 		 */
6947c478bd9Sstevel@tonic-gate 	} else {
6957c478bd9Sstevel@tonic-gate 		/*
6967c478bd9Sstevel@tonic-gate 		 * Check to see if there is a low-level interrupt active.
6977c478bd9Sstevel@tonic-gate 		 * If so, place a starting timestamp in the thread
6987c478bd9Sstevel@tonic-gate 		 * structure.
6997c478bd9Sstevel@tonic-gate 		 */
7007c478bd9Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		if (t->t_flag & T_INTR_THREAD)
703ae115bc7Smrj 			t->t_intr_start = now;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
706*c3377ee9SJohn Levon 	smt_end_intr();
707455e370cSJohn Levon 
7087c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = oldpil;
7097c478bd9Sstevel@tonic-gate 	(void) (*setlvlx)(oldpil, vecnum);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate /*
7157c478bd9Sstevel@tonic-gate  * Set up the cpu, thread and interrupt thread structures for
7167c478bd9Sstevel@tonic-gate  * executing an interrupt thread.  The new stack pointer of the
7177c478bd9Sstevel@tonic-gate  * interrupt thread (which *must* be switched to) is returned.
7187c478bd9Sstevel@tonic-gate  */
719ae115bc7Smrj static caddr_t
intr_thread_prolog(struct cpu * cpu,caddr_t stackptr,uint_t pil)7207c478bd9Sstevel@tonic-gate intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
7237c478bd9Sstevel@tonic-gate 	kthread_t *t, *volatile it;
724ae115bc7Smrj 	hrtime_t now = tsc_read();
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	ASSERT(pil > 0);
7277c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
7287c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	/*
7317c478bd9Sstevel@tonic-gate 	 * Get set to run an interrupt thread.
7327c478bd9Sstevel@tonic-gate 	 * There should always be an interrupt thread, since we
7337c478bd9Sstevel@tonic-gate 	 * allocate one for each level on each CPU.
7347c478bd9Sstevel@tonic-gate 	 *
735fd71cd2fSesolom 	 * t_intr_start could be zero due to cpu_intr_swtch_enter.
7367c478bd9Sstevel@tonic-gate 	 */
7377c478bd9Sstevel@tonic-gate 	t = cpu->cpu_thread;
738fd71cd2fSesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
739ae115bc7Smrj 		hrtime_t intrtime = now - t->t_intr_start;
7407a364d25Sschwartz 		mcpu->intrstat[t->t_pil][0] += intrtime;
741eda89462Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
7427c478bd9Sstevel@tonic-gate 		t->t_intr_start = 0;
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;	/* mark stack in curthread for resume */
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/*
7507c478bd9Sstevel@tonic-gate 	 * unlink the interrupt thread off the cpu
751fd71cd2fSesolom 	 *
752fd71cd2fSesolom 	 * Note that the code in kcpc_overflow_intr -relies- on the
753fd71cd2fSesolom 	 * ordering of events here - in particular that t->t_lwp of
754fd71cd2fSesolom 	 * the interrupt thread is set to the pinned thread *before*
755fd71cd2fSesolom 	 * curthread is changed.
7567c478bd9Sstevel@tonic-gate 	 */
7577c478bd9Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
7587c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
7597c478bd9Sstevel@tonic-gate 	it->t_intr = t;
7607c478bd9Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/*
7637c478bd9Sstevel@tonic-gate 	 * (threads on the interrupt thread free list could have state
7647c478bd9Sstevel@tonic-gate 	 * preset to TS_ONPROC, but it helps in debugging if
7657c478bd9Sstevel@tonic-gate 	 * they're TS_FREE.)
7667c478bd9Sstevel@tonic-gate 	 */
7677c478bd9Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = it;		/* new curthread on this cpu */
770*c3377ee9SJohn Levon 	smt_begin_intr(pil);
771455e370cSJohn Levon 
7727c478bd9Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
7737c478bd9Sstevel@tonic-gate 	it->t_pri = intr_pri + (pri_t)pil;
774ae115bc7Smrj 	it->t_intr_start = now;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	return (it->t_stk);
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate #ifdef DEBUG
7817c478bd9Sstevel@tonic-gate int intr_thread_cnt;
7827c478bd9Sstevel@tonic-gate #endif
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate /*
7857c478bd9Sstevel@tonic-gate  * Called with interrupts disabled
7867c478bd9Sstevel@tonic-gate  */
787ae115bc7Smrj static void
intr_thread_epilog(struct cpu * cpu,uint_t vec,uint_t oldpil)7887c478bd9Sstevel@tonic-gate intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil)
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
7917c478bd9Sstevel@tonic-gate 	kthread_t *t;
7927c478bd9Sstevel@tonic-gate 	kthread_t *it = cpu->cpu_thread;	/* curthread */
7937c478bd9Sstevel@tonic-gate 	uint_t pil, basespl;
794eda89462Sesolom 	hrtime_t intrtime;
795ae115bc7Smrj 	hrtime_t now = tsc_read();
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	pil = it->t_pil;
7987c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	ASSERT(it->t_intr_start != 0);
801ae115bc7Smrj 	intrtime = now - it->t_intr_start;
8027a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
803eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
8067c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	/*
8097c478bd9Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
8107c478bd9Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
8117c478bd9Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
8127c478bd9Sstevel@tonic-gate 	 */
8137c478bd9Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
8147c478bd9Sstevel@tonic-gate 		/*
8157c478bd9Sstevel@tonic-gate 		 * The interrupted thread is no longer pinned underneath
8167c478bd9Sstevel@tonic-gate 		 * the interrupt thread.  This means the interrupt must
8177c478bd9Sstevel@tonic-gate 		 * have blocked, and the interrupted thread has been
8187c478bd9Sstevel@tonic-gate 		 * unpinned, and has probably been running around the
8197c478bd9Sstevel@tonic-gate 		 * system for a while.
8207c478bd9Sstevel@tonic-gate 		 *
8217c478bd9Sstevel@tonic-gate 		 * Since there is no longer a thread under this one, put
8227c478bd9Sstevel@tonic-gate 		 * this interrupt thread back on the CPU's free list and
8237c478bd9Sstevel@tonic-gate 		 * resume the idle thread which will dispatch the next
8247c478bd9Sstevel@tonic-gate 		 * thread to run.
8257c478bd9Sstevel@tonic-gate 		 */
8267c478bd9Sstevel@tonic-gate #ifdef DEBUG
8277c478bd9Sstevel@tonic-gate 		intr_thread_cnt++;
8287c478bd9Sstevel@tonic-gate #endif
8297c478bd9Sstevel@tonic-gate 		cpu->cpu_stats.sys.intrblk++;
8307c478bd9Sstevel@tonic-gate 		/*
8317c478bd9Sstevel@tonic-gate 		 * Set CPU's base SPL based on active interrupts bitmask
8327c478bd9Sstevel@tonic-gate 		 */
8337c478bd9Sstevel@tonic-gate 		set_base_spl();
8347c478bd9Sstevel@tonic-gate 		basespl = cpu->cpu_base_spl;
8357c478bd9Sstevel@tonic-gate 		mcpu->mcpu_pri = basespl;
8367c478bd9Sstevel@tonic-gate 		(*setlvlx)(basespl, vec);
8377c478bd9Sstevel@tonic-gate 		(void) splhigh();
838ae115bc7Smrj 		sti();
8397c478bd9Sstevel@tonic-gate 		it->t_state = TS_FREE;
8407c478bd9Sstevel@tonic-gate 		/*
8417c478bd9Sstevel@tonic-gate 		 * Return interrupt thread to pool
8427c478bd9Sstevel@tonic-gate 		 */
8437c478bd9Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
8447c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
8457c478bd9Sstevel@tonic-gate 		swtch();
846ae115bc7Smrj 		panic("intr_thread_epilog: swtch returned");
8477c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/*
8517c478bd9Sstevel@tonic-gate 	 * Return interrupt thread to the pool
8527c478bd9Sstevel@tonic-gate 	 */
8537c478bd9Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
8547c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
8557c478bd9Sstevel@tonic-gate 	it->t_state = TS_FREE;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
8587c478bd9Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
8597c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
8607c478bd9Sstevel@tonic-gate 	(*setlvlx)(pil, vec);
861ae115bc7Smrj 	t->t_intr_start = now;
862*c3377ee9SJohn Levon 	smt_end_intr();
8637c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = t;
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate 
8667a364d25Sschwartz /*
867ae115bc7Smrj  * intr_get_time() is a resource for interrupt handlers to determine how
868ae115bc7Smrj  * much time has been spent handling the current interrupt. Such a function
869ae115bc7Smrj  * is needed because higher level interrupts can arrive during the
870ae115bc7Smrj  * processing of an interrupt.  intr_get_time() only returns time spent in the
871ae115bc7Smrj  * current interrupt handler.
872ae115bc7Smrj  *
873ae115bc7Smrj  * The caller must be calling from an interrupt handler running at a pil
874ae115bc7Smrj  * below or at lock level. Timings are not provided for high-level
875ae115bc7Smrj  * interrupts.
876ae115bc7Smrj  *
877ae115bc7Smrj  * The first time intr_get_time() is called while handling an interrupt,
878ae115bc7Smrj  * it returns the time since the interrupt handler was invoked. Subsequent
879ae115bc7Smrj  * calls will return the time since the prior call to intr_get_time(). Time
880843e1988Sjohnlev  * is returned as ticks. Use scalehrtimef() to convert ticks to nsec.
881ae115bc7Smrj  *
882ae115bc7Smrj  * Theory Of Intrstat[][]:
883ae115bc7Smrj  *
884ae115bc7Smrj  * uint64_t intrstat[pil][0..1] is an array indexed by pil level, with two
885ae115bc7Smrj  * uint64_ts per pil.
886ae115bc7Smrj  *
887ae115bc7Smrj  * intrstat[pil][0] is a cumulative count of the number of ticks spent
888ae115bc7Smrj  * handling all interrupts at the specified pil on this CPU. It is
889ae115bc7Smrj  * exported via kstats to the user.
890ae115bc7Smrj  *
891ae115bc7Smrj  * intrstat[pil][1] is always a count of ticks less than or equal to the
892ae115bc7Smrj  * value in [0]. The difference between [1] and [0] is the value returned
893ae115bc7Smrj  * by a call to intr_get_time(). At the start of interrupt processing,
894ae115bc7Smrj  * [0] and [1] will be equal (or nearly so). As the interrupt consumes
895ae115bc7Smrj  * time, [0] will increase, but [1] will remain the same. A call to
896ae115bc7Smrj  * intr_get_time() will return the difference, then update [1] to be the
897ae115bc7Smrj  * same as [0]. Future calls will return the time since the last call.
898ae115bc7Smrj  * Finally, when the interrupt completes, [1] is updated to the same as [0].
899ae115bc7Smrj  *
900ae115bc7Smrj  * Implementation:
901ae115bc7Smrj  *
902ae115bc7Smrj  * intr_get_time() works much like a higher level interrupt arriving. It
903ae115bc7Smrj  * "checkpoints" the timing information by incrementing intrstat[pil][0]
904ae115bc7Smrj  * to include elapsed running time, and by setting t_intr_start to rdtsc.
905ae115bc7Smrj  * It then sets the return value to intrstat[pil][0] - intrstat[pil][1],
906ae115bc7Smrj  * and updates intrstat[pil][1] to be the same as the new value of
907ae115bc7Smrj  * intrstat[pil][0].
908ae115bc7Smrj  *
909ae115bc7Smrj  * In the normal handling of interrupts, after an interrupt handler returns
910ae115bc7Smrj  * and the code in intr_thread() updates intrstat[pil][0], it then sets
911ae115bc7Smrj  * intrstat[pil][1] to the new value of intrstat[pil][0]. When [0] == [1],
912ae115bc7Smrj  * the timings are reset, i.e. intr_get_time() will return [0] - [1] which
913ae115bc7Smrj  * is 0.
914ae115bc7Smrj  *
915ae115bc7Smrj  * Whenever interrupts arrive on a CPU which is handling a lower pil
916ae115bc7Smrj  * interrupt, they update the lower pil's [0] to show time spent in the
917ae115bc7Smrj  * handler that they've interrupted. This results in a growing discrepancy
918ae115bc7Smrj  * between [0] and [1], which is returned the next time intr_get_time() is
919ae115bc7Smrj  * called. Time spent in the higher-pil interrupt will not be returned in
920ae115bc7Smrj  * the next intr_get_time() call from the original interrupt, because
921ae115bc7Smrj  * the higher-pil interrupt's time is accumulated in intrstat[higherpil][].
9227a364d25Sschwartz  */
9237a364d25Sschwartz uint64_t
intr_get_time(void)924ae115bc7Smrj intr_get_time(void)
9257a364d25Sschwartz {
926ae115bc7Smrj 	struct cpu *cpu;
927ae115bc7Smrj 	struct machcpu *mcpu;
928ae115bc7Smrj 	kthread_t *t;
9297a364d25Sschwartz 	uint64_t time, delta, ret;
930ae115bc7Smrj 	uint_t pil;
9317a364d25Sschwartz 
932ae115bc7Smrj 	cli();
933ae115bc7Smrj 	cpu = CPU;
934ae115bc7Smrj 	mcpu = &cpu->cpu_m;
935ae115bc7Smrj 	t = cpu->cpu_thread;
936ae115bc7Smrj 	pil = t->t_pil;
9377a364d25Sschwartz 	ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0);
9387a364d25Sschwartz 	ASSERT(t->t_flag & T_INTR_THREAD);
9397a364d25Sschwartz 	ASSERT(pil != 0);
9407a364d25Sschwartz 	ASSERT(t->t_intr_start != 0);
9417a364d25Sschwartz 
9427a364d25Sschwartz 	time = tsc_read();
9437a364d25Sschwartz 	delta = time - t->t_intr_start;
9447a364d25Sschwartz 	t->t_intr_start = time;
9457a364d25Sschwartz 
9467a364d25Sschwartz 	time = mcpu->intrstat[pil][0] + delta;
9477a364d25Sschwartz 	ret = time - mcpu->intrstat[pil][1];
9487a364d25Sschwartz 	mcpu->intrstat[pil][0] = time;
9497a364d25Sschwartz 	mcpu->intrstat[pil][1] = time;
950c81508f4Sjhaslam 	cpu->cpu_intracct[cpu->cpu_mstate] += delta;
9517a364d25Sschwartz 
952ae115bc7Smrj 	sti();
9537a364d25Sschwartz 	return (ret);
9547a364d25Sschwartz }
9557a364d25Sschwartz 
956ae115bc7Smrj static caddr_t
dosoftint_prolog(struct cpu * cpu,caddr_t stackptr,uint32_t st_pending,uint_t oldpil)9577c478bd9Sstevel@tonic-gate dosoftint_prolog(
9587c478bd9Sstevel@tonic-gate 	struct cpu *cpu,
9597c478bd9Sstevel@tonic-gate 	caddr_t stackptr,
9607c478bd9Sstevel@tonic-gate 	uint32_t st_pending,
9617c478bd9Sstevel@tonic-gate 	uint_t oldpil)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate 	kthread_t *t, *volatile it;
9647c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
9657c478bd9Sstevel@tonic-gate 	uint_t pil;
966ae115bc7Smrj 	hrtime_t now;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate top:
9697c478bd9Sstevel@tonic-gate 	ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	pil = bsrw_insn((uint16_t)st_pending);
9727c478bd9Sstevel@tonic-gate 	if (pil <= oldpil || pil <= cpu->cpu_base_spl)
9737c478bd9Sstevel@tonic-gate 		return (0);
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	/*
9767c478bd9Sstevel@tonic-gate 	 * XX64	Sigh.
9777c478bd9Sstevel@tonic-gate 	 *
9787c478bd9Sstevel@tonic-gate 	 * This is a transliteration of the i386 assembler code for
9797c478bd9Sstevel@tonic-gate 	 * soft interrupts.  One question is "why does this need
9807c478bd9Sstevel@tonic-gate 	 * to be atomic?"  One possible race is -other- processors
9817c478bd9Sstevel@tonic-gate 	 * posting soft interrupts to us in set_pending() i.e. the
9827c478bd9Sstevel@tonic-gate 	 * CPU might get preempted just after the address computation,
9837c478bd9Sstevel@tonic-gate 	 * but just before the atomic transaction, so another CPU would
9847c478bd9Sstevel@tonic-gate 	 * actually set the original CPU's st_pending bit.  However,
9857c478bd9Sstevel@tonic-gate 	 * it looks like it would be simpler to disable preemption there.
9867c478bd9Sstevel@tonic-gate 	 * Are there other races for which preemption control doesn't work?
9877c478bd9Sstevel@tonic-gate 	 *
9887c478bd9Sstevel@tonic-gate 	 * The i386 assembler version -also- checks to see if the bit
9897c478bd9Sstevel@tonic-gate 	 * being cleared was actually set; if it wasn't, it rechecks
9907c478bd9Sstevel@tonic-gate 	 * for more.  This seems a bit strange, as the only code that
9917c478bd9Sstevel@tonic-gate 	 * ever clears the bit is -this- code running with interrupts
9927c478bd9Sstevel@tonic-gate 	 * disabled on -this- CPU.  This code would probably be cheaper:
9937c478bd9Sstevel@tonic-gate 	 *
9947c478bd9Sstevel@tonic-gate 	 * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending,
9957c478bd9Sstevel@tonic-gate 	 *   ~(1 << pil));
9967c478bd9Sstevel@tonic-gate 	 *
9977c478bd9Sstevel@tonic-gate 	 * and t->t_preempt--/++ around set_pending() even cheaper,
9987c478bd9Sstevel@tonic-gate 	 * but at this point, correctness is critical, so we slavishly
9997c478bd9Sstevel@tonic-gate 	 * emulate the i386 port.
10007c478bd9Sstevel@tonic-gate 	 */
1001ae115bc7Smrj 	if (atomic_btr32((uint32_t *)
1002ae115bc7Smrj 	    &mcpu->mcpu_softinfo.st_pending, pil) == 0) {
10037c478bd9Sstevel@tonic-gate 		st_pending = mcpu->mcpu_softinfo.st_pending;
10047c478bd9Sstevel@tonic-gate 		goto top;
10057c478bd9Sstevel@tonic-gate 	}
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
10087c478bd9Sstevel@tonic-gate 	(*setspl)(pil);
10097c478bd9Sstevel@tonic-gate 
1010ae115bc7Smrj 	now = tsc_read();
1011ae115bc7Smrj 
10127c478bd9Sstevel@tonic-gate 	/*
10137c478bd9Sstevel@tonic-gate 	 * Get set to run interrupt thread.
10147c478bd9Sstevel@tonic-gate 	 * There should always be an interrupt thread since we
10157c478bd9Sstevel@tonic-gate 	 * allocate one for each level on the CPU.
10167c478bd9Sstevel@tonic-gate 	 */
10177c478bd9Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
10187c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
10197c478bd9Sstevel@tonic-gate 
1020fd71cd2fSesolom 	/* t_intr_start could be zero due to cpu_intr_swtch_enter. */
10217c478bd9Sstevel@tonic-gate 	t = cpu->cpu_thread;
1022fd71cd2fSesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
1023ae115bc7Smrj 		hrtime_t intrtime = now - t->t_intr_start;
10247a364d25Sschwartz 		mcpu->intrstat[pil][0] += intrtime;
1025eda89462Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1026fd71cd2fSesolom 		t->t_intr_start = 0;
1027eda89462Sesolom 	}
1028fd71cd2fSesolom 
1029fd71cd2fSesolom 	/*
1030fd71cd2fSesolom 	 * Note that the code in kcpc_overflow_intr -relies- on the
1031fd71cd2fSesolom 	 * ordering of events here - in particular that t->t_lwp of
1032fd71cd2fSesolom 	 * the interrupt thread is set to the pinned thread *before*
1033fd71cd2fSesolom 	 * curthread is changed.
1034fd71cd2fSesolom 	 */
10357c478bd9Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
10367c478bd9Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	/*
10397c478bd9Sstevel@tonic-gate 	 * Push interrupted thread onto list from new thread.
10407c478bd9Sstevel@tonic-gate 	 * Set the new thread as the current one.
10417c478bd9Sstevel@tonic-gate 	 * Set interrupted thread's T_SP because if it is the idle thread,
10427c478bd9Sstevel@tonic-gate 	 * resume() may use that stack between threads.
10437c478bd9Sstevel@tonic-gate 	 */
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
10467c478bd9Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	it->t_intr = t;
10497c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = it;
1050*c3377ee9SJohn Levon 	smt_begin_intr(pil);
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	/*
10537c478bd9Sstevel@tonic-gate 	 * Set bit for this pil in CPU's interrupt active bitmask.
10547c478bd9Sstevel@tonic-gate 	 */
10557c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
10567c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	/*
10597c478bd9Sstevel@tonic-gate 	 * Initialize thread priority level from intr_pri
10607c478bd9Sstevel@tonic-gate 	 */
10617c478bd9Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
10627c478bd9Sstevel@tonic-gate 	it->t_pri = (pri_t)pil + intr_pri;
1063ae115bc7Smrj 	it->t_intr_start = now;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	return (it->t_stk);
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate 
1068ae115bc7Smrj static void
dosoftint_epilog(struct cpu * cpu,uint_t oldpil)10697c478bd9Sstevel@tonic-gate dosoftint_epilog(struct cpu *cpu, uint_t oldpil)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
10727c478bd9Sstevel@tonic-gate 	kthread_t *t, *it;
10737c478bd9Sstevel@tonic-gate 	uint_t pil, basespl;
1074eda89462Sesolom 	hrtime_t intrtime;
1075ae115bc7Smrj 	hrtime_t now = tsc_read();
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	it = cpu->cpu_thread;
10787c478bd9Sstevel@tonic-gate 	pil = it->t_pil;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
10837c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
1084ae115bc7Smrj 	intrtime = now - it->t_intr_start;
10857a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
1086eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	/*
10897c478bd9Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
10907c478bd9Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
10917c478bd9Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
10927c478bd9Sstevel@tonic-gate 	 */
10937c478bd9Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
10947c478bd9Sstevel@tonic-gate 		/*
10957c478bd9Sstevel@tonic-gate 		 * Put thread back on the interrupt thread list.
10967c478bd9Sstevel@tonic-gate 		 * This was an interrupt thread, so set CPU's base SPL.
10977c478bd9Sstevel@tonic-gate 		 */
10987c478bd9Sstevel@tonic-gate 		set_base_spl();
10997c478bd9Sstevel@tonic-gate 		it->t_state = TS_FREE;
11007c478bd9Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
11017c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
11027c478bd9Sstevel@tonic-gate 		(void) splhigh();
1103ae115bc7Smrj 		sti();
11047c478bd9Sstevel@tonic-gate 		swtch();
11057c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1106ae115bc7Smrj 		panic("dosoftint_epilog: swtch returned");
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
11097c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
11107c478bd9Sstevel@tonic-gate 	it->t_state = TS_FREE;
1111*c3377ee9SJohn Levon 	smt_end_intr();
11127c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = t;
1113455e370cSJohn Levon 
11147c478bd9Sstevel@tonic-gate 	if (t->t_flag & T_INTR_THREAD)
1115ae115bc7Smrj 		t->t_intr_start = now;
11167c478bd9Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
11177c478bd9Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
11187c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
11197c478bd9Sstevel@tonic-gate 	(*setspl)(pil);
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate 
1122ae115bc7Smrj 
11237c478bd9Sstevel@tonic-gate /*
11247c478bd9Sstevel@tonic-gate  * Make the interrupted thread 'to' be runnable.
11257c478bd9Sstevel@tonic-gate  *
11267c478bd9Sstevel@tonic-gate  * Since t->t_sp has already been saved, t->t_pc is all
11277c478bd9Sstevel@tonic-gate  * that needs to be set in this function.
11287c478bd9Sstevel@tonic-gate  *
11297c478bd9Sstevel@tonic-gate  * Returns the interrupt level of the interrupt thread.
11307c478bd9Sstevel@tonic-gate  */
11317c478bd9Sstevel@tonic-gate int
intr_passivate(kthread_t * it,kthread_t * t)11327c478bd9Sstevel@tonic-gate intr_passivate(
11337c478bd9Sstevel@tonic-gate 	kthread_t *it,		/* interrupt thread */
11347c478bd9Sstevel@tonic-gate 	kthread_t *t)		/* interrupted thread */
11357c478bd9Sstevel@tonic-gate {
11367c478bd9Sstevel@tonic-gate 	extern void _sys_rtt();
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	ASSERT(it->t_flag & T_INTR_THREAD);
11397c478bd9Sstevel@tonic-gate 	ASSERT(SA(t->t_sp) == t->t_sp);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	t->t_pc = (uintptr_t)_sys_rtt;
11427c478bd9Sstevel@tonic-gate 	return (it->t_pil);
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate /*
11467c478bd9Sstevel@tonic-gate  * Create interrupt kstats for this CPU.
11477c478bd9Sstevel@tonic-gate  */
11487c478bd9Sstevel@tonic-gate void
cpu_create_intrstat(cpu_t * cp)11497c478bd9Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp)
11507c478bd9Sstevel@tonic-gate {
11517c478bd9Sstevel@tonic-gate 	int		i;
11527c478bd9Sstevel@tonic-gate 	kstat_t		*intr_ksp;
11537c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp;
11547c478bd9Sstevel@tonic-gate 	char		name[KSTAT_STRLEN];
11557c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	if (pool_pset_enabled())
11607c478bd9Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
11617c478bd9Sstevel@tonic-gate 	else
11627c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
11654da99751SToomas Soome 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, 0, zoneid);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	/*
11687c478bd9Sstevel@tonic-gate 	 * Initialize each PIL's named kstat
11697c478bd9Sstevel@tonic-gate 	 */
11707c478bd9Sstevel@tonic-gate 	if (intr_ksp != NULL) {
11717c478bd9Sstevel@tonic-gate 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
11727c478bd9Sstevel@tonic-gate 		knp = (kstat_named_t *)intr_ksp->ks_data;
11737c478bd9Sstevel@tonic-gate 		intr_ksp->ks_private = cp;
11747c478bd9Sstevel@tonic-gate 		for (i = 0; i < PIL_MAX; i++) {
11757c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
11767c478bd9Sstevel@tonic-gate 			    i + 1);
11777c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
11787c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
11797c478bd9Sstevel@tonic-gate 			    i + 1);
11807c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[(i * 2) + 1], name,
11817c478bd9Sstevel@tonic-gate 			    KSTAT_DATA_UINT64);
11827c478bd9Sstevel@tonic-gate 		}
11837c478bd9Sstevel@tonic-gate 		kstat_install(intr_ksp);
11847c478bd9Sstevel@tonic-gate 	}
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate /*
11887c478bd9Sstevel@tonic-gate  * Delete interrupt kstats for this CPU.
11897c478bd9Sstevel@tonic-gate  */
11907c478bd9Sstevel@tonic-gate void
cpu_delete_intrstat(cpu_t * cp)11917c478bd9Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp)
11927c478bd9Sstevel@tonic-gate {
11937c478bd9Sstevel@tonic-gate 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate /*
11977c478bd9Sstevel@tonic-gate  * Convert interrupt statistics from CPU ticks to nanoseconds and
11987c478bd9Sstevel@tonic-gate  * update kstat.
11997c478bd9Sstevel@tonic-gate  */
12007c478bd9Sstevel@tonic-gate int
cpu_kstat_intrstat_update(kstat_t * ksp,int rw)12017c478bd9Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
12027c478bd9Sstevel@tonic-gate {
12037c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp = ksp->ks_data;
12047c478bd9Sstevel@tonic-gate 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
12057c478bd9Sstevel@tonic-gate 	int		i;
12067c478bd9Sstevel@tonic-gate 	hrtime_t	hrt;
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
12097c478bd9Sstevel@tonic-gate 		return (EACCES);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
12127a364d25Sschwartz 		hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0];
1213843e1988Sjohnlev 		scalehrtimef(&hrt);
12147c478bd9Sstevel@tonic-gate 		knp[i * 2].value.ui64 = (uint64_t)hrt;
12157c478bd9Sstevel@tonic-gate 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
12167c478bd9Sstevel@tonic-gate 	}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	return (0);
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate /*
12227c478bd9Sstevel@tonic-gate  * An interrupt thread is ending a time slice, so compute the interval it
12237c478bd9Sstevel@tonic-gate  * ran for and update the statistic for its PIL.
12247c478bd9Sstevel@tonic-gate  */
12257c478bd9Sstevel@tonic-gate void
cpu_intr_swtch_enter(kthread_id_t t)12267c478bd9Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t)
12277c478bd9Sstevel@tonic-gate {
12287c478bd9Sstevel@tonic-gate 	uint64_t	interval;
12297c478bd9Sstevel@tonic-gate 	uint64_t	start;
1230eda89462Sesolom 	cpu_t		*cpu;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
12337c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	/*
12367c478bd9Sstevel@tonic-gate 	 * We could be here with a zero timestamp. This could happen if:
12377c478bd9Sstevel@tonic-gate 	 * an interrupt thread which no longer has a pinned thread underneath
12387c478bd9Sstevel@tonic-gate 	 * it (i.e. it blocked at some point in its past) has finished running
12397c478bd9Sstevel@tonic-gate 	 * its handler. intr_thread() updated the interrupt statistic for its
12407c478bd9Sstevel@tonic-gate 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
12417c478bd9Sstevel@tonic-gate 	 * return to, swtch() gets called and we end up here.
1242eda89462Sesolom 	 *
124375d94465SJosef 'Jeff' Sipek 	 * Note that we use atomic ops below (atomic_cas_64 and
124475d94465SJosef 'Jeff' Sipek 	 * atomic_add_64), which we don't use in the functions above,
124575d94465SJosef 'Jeff' Sipek 	 * because we're not called with interrupts blocked, but the
124675d94465SJosef 'Jeff' Sipek 	 * epilog/prolog functions are.
12477c478bd9Sstevel@tonic-gate 	 */
12487c478bd9Sstevel@tonic-gate 	if (t->t_intr_start) {
12497c478bd9Sstevel@tonic-gate 		do {
12507c478bd9Sstevel@tonic-gate 			start = t->t_intr_start;
12517c478bd9Sstevel@tonic-gate 			interval = tsc_read() - start;
125275d94465SJosef 'Jeff' Sipek 		} while (atomic_cas_64(&t->t_intr_start, start, 0) != start);
1253eda89462Sesolom 		cpu = CPU;
12547a364d25Sschwartz 		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
1255eda89462Sesolom 
1256eda89462Sesolom 		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
1257eda89462Sesolom 		    interval);
12587c478bd9Sstevel@tonic-gate 	} else
12597c478bd9Sstevel@tonic-gate 		ASSERT(t->t_intr == NULL);
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate /*
12637c478bd9Sstevel@tonic-gate  * An interrupt thread is returning from swtch(). Place a starting timestamp
12647c478bd9Sstevel@tonic-gate  * in its thread structure.
12657c478bd9Sstevel@tonic-gate  */
12667c478bd9Sstevel@tonic-gate void
cpu_intr_swtch_exit(kthread_id_t t)12677c478bd9Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t)
12687c478bd9Sstevel@tonic-gate {
12697c478bd9Sstevel@tonic-gate 	uint64_t ts;
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
12727c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	do {
12757c478bd9Sstevel@tonic-gate 		ts = t->t_intr_start;
127675d94465SJosef 'Jeff' Sipek 	} while (atomic_cas_64(&t->t_intr_start, ts, tsc_read()) != ts);
12777c478bd9Sstevel@tonic-gate }
1278ae115bc7Smrj 
1279ae115bc7Smrj /*
1280ae115bc7Smrj  * Dispatch a hilevel interrupt (one above LOCK_LEVEL)
1281ae115bc7Smrj  */
1282ae115bc7Smrj /*ARGSUSED*/
1283ae115bc7Smrj static void
dispatch_hilevel(uint_t vector,uint_t arg2)1284ae115bc7Smrj dispatch_hilevel(uint_t vector, uint_t arg2)
1285ae115bc7Smrj {
1286ae115bc7Smrj 	sti();
1287ae115bc7Smrj 	av_dispatch_autovect(vector);
1288ae115bc7Smrj 	cli();
1289ae115bc7Smrj }
1290ae115bc7Smrj 
1291ae115bc7Smrj /*
1292ae115bc7Smrj  * Dispatch a soft interrupt
1293ae115bc7Smrj  */
1294ae115bc7Smrj /*ARGSUSED*/
1295ae115bc7Smrj static void
dispatch_softint(uint_t oldpil,uint_t arg2)1296ae115bc7Smrj dispatch_softint(uint_t oldpil, uint_t arg2)
1297ae115bc7Smrj {
1298ae115bc7Smrj 	struct cpu *cpu = CPU;
1299ae115bc7Smrj 
1300ae115bc7Smrj 	sti();
1301ae115bc7Smrj 	av_dispatch_softvect((int)cpu->cpu_thread->t_pil);
1302ae115bc7Smrj 	cli();
1303ae115bc7Smrj 
1304ae115bc7Smrj 	/*
1305ae115bc7Smrj 	 * Must run softint_epilog() on the interrupt thread stack, since
1306ae115bc7Smrj 	 * there may not be a return from it if the interrupt thread blocked.
1307ae115bc7Smrj 	 */
1308ae115bc7Smrj 	dosoftint_epilog(cpu, oldpil);
1309ae115bc7Smrj }
1310ae115bc7Smrj 
1311ae115bc7Smrj /*
1312ae115bc7Smrj  * Dispatch a normal interrupt
1313ae115bc7Smrj  */
1314ae115bc7Smrj static void
dispatch_hardint(uint_t vector,uint_t oldipl)1315ae115bc7Smrj dispatch_hardint(uint_t vector, uint_t oldipl)
1316ae115bc7Smrj {
1317ae115bc7Smrj 	struct cpu *cpu = CPU;
1318ae115bc7Smrj 
1319ae115bc7Smrj 	sti();
1320ae115bc7Smrj 	av_dispatch_autovect(vector);
1321ae115bc7Smrj 	cli();
1322ae115bc7Smrj 
1323ae115bc7Smrj 	/*
1324ae115bc7Smrj 	 * Must run intr_thread_epilog() on the interrupt thread stack, since
1325ae115bc7Smrj 	 * there may not be a return from it if the interrupt thread blocked.
1326ae115bc7Smrj 	 */
1327ae115bc7Smrj 	intr_thread_epilog(cpu, vector, oldipl);
1328ae115bc7Smrj }
1329ae115bc7Smrj 
1330ae115bc7Smrj /*
1331ae115bc7Smrj  * Deliver any softints the current interrupt priority allows.
1332ae115bc7Smrj  * Called with interrupts disabled.
1333ae115bc7Smrj  */
1334ae115bc7Smrj void
dosoftint(struct regs * regs)1335ae115bc7Smrj dosoftint(struct regs *regs)
1336ae115bc7Smrj {
1337ae115bc7Smrj 	struct cpu *cpu = CPU;
1338ae115bc7Smrj 	int oldipl;
1339ae115bc7Smrj 	caddr_t newsp;
1340ae115bc7Smrj 
1341ae115bc7Smrj 	while (cpu->cpu_softinfo.st_pending) {
1342ae115bc7Smrj 		oldipl = cpu->cpu_pri;
1343ae115bc7Smrj 		newsp = dosoftint_prolog(cpu, (caddr_t)regs,
1344843e1988Sjohnlev 		    cpu->cpu_softinfo.st_pending, oldipl);
1345ae115bc7Smrj 		/*
1346ae115bc7Smrj 		 * If returned stack pointer is NULL, priority is too high
1347ae115bc7Smrj 		 * to run any of the pending softints now.
1348ae115bc7Smrj 		 * Break out and they will be run later.
1349ae115bc7Smrj 		 */
1350ae115bc7Smrj 		if (newsp == NULL)
1351ae115bc7Smrj 			break;
1352ae115bc7Smrj 		switch_sp_and_call(newsp, dispatch_softint, oldipl, 0);
1353ae115bc7Smrj 	}
1354ae115bc7Smrj }
1355ae115bc7Smrj 
1356ae115bc7Smrj /*
1357ae115bc7Smrj  * Interrupt service routine, called with interrupts disabled.
1358ae115bc7Smrj  */
1359ae115bc7Smrj /*ARGSUSED*/
1360ae115bc7Smrj void
do_interrupt(struct regs * rp,trap_trace_rec_t * ttp)1361ae115bc7Smrj do_interrupt(struct regs *rp, trap_trace_rec_t *ttp)
1362ae115bc7Smrj {
1363ae115bc7Smrj 	struct cpu *cpu = CPU;
1364ae115bc7Smrj 	int newipl, oldipl = cpu->cpu_pri;
1365ae115bc7Smrj 	uint_t vector;
1366ae115bc7Smrj 	caddr_t newsp;
1367ae115bc7Smrj 
1368ae115bc7Smrj #ifdef TRAPTRACE
1369ae115bc7Smrj 	ttp->ttr_marker = TT_INTERRUPT;
1370ae115bc7Smrj 	ttp->ttr_ipl = 0xff;
1371ae115bc7Smrj 	ttp->ttr_pri = oldipl;
1372ae115bc7Smrj 	ttp->ttr_spl = cpu->cpu_base_spl;
1373ae115bc7Smrj 	ttp->ttr_vector = 0xff;
1374ae115bc7Smrj #endif	/* TRAPTRACE */
1375ae115bc7Smrj 
1376fb2caebeSRandy Fishel 	cpu_idle_exit(CPU_IDLE_CB_FLAG_INTR);
137795c0a3c8Sjosephb 
13783006ae82SFrank Van Der Linden 	++*(uint16_t *)&cpu->cpu_m.mcpu_istamp;
13793006ae82SFrank Van Der Linden 
1380ae115bc7Smrj 	/*
1381ae115bc7Smrj 	 * If it's a softint go do it now.
1382ae115bc7Smrj 	 */
1383ae115bc7Smrj 	if (rp->r_trapno == T_SOFTINT) {
1384ae115bc7Smrj 		dosoftint(rp);
1385ae115bc7Smrj 		ASSERT(!interrupts_enabled());
1386ae115bc7Smrj 		return;
1387ae115bc7Smrj 	}
1388ae115bc7Smrj 
1389ae115bc7Smrj 	/*
1390ae115bc7Smrj 	 * Raise the interrupt priority.
1391ae115bc7Smrj 	 */
1392ae115bc7Smrj 	newipl = (*setlvl)(oldipl, (int *)&rp->r_trapno);
1393ae115bc7Smrj #ifdef TRAPTRACE
1394ae115bc7Smrj 	ttp->ttr_ipl = newipl;
1395ae115bc7Smrj #endif	/* TRAPTRACE */
1396ae115bc7Smrj 
1397ae115bc7Smrj 	/*
1398ae115bc7Smrj 	 * Bail if it is a spurious interrupt
1399ae115bc7Smrj 	 */
1400ae115bc7Smrj 	if (newipl == -1)
1401ae115bc7Smrj 		return;
1402ae115bc7Smrj 	cpu->cpu_pri = newipl;
1403ae115bc7Smrj 	vector = rp->r_trapno;
1404ae115bc7Smrj #ifdef TRAPTRACE
1405ae115bc7Smrj 	ttp->ttr_vector = vector;
1406ae115bc7Smrj #endif	/* TRAPTRACE */
1407ae115bc7Smrj 	if (newipl > LOCK_LEVEL) {
1408ae115bc7Smrj 		/*
1409ae115bc7Smrj 		 * High priority interrupts run on this cpu's interrupt stack.
1410ae115bc7Smrj 		 */
1411ae115bc7Smrj 		if (hilevel_intr_prolog(cpu, newipl, oldipl, rp) == 0) {
1412ae115bc7Smrj 			newsp = cpu->cpu_intr_stack;
1413ae115bc7Smrj 			switch_sp_and_call(newsp, dispatch_hilevel, vector, 0);
1414ae115bc7Smrj 		} else { /* already on the interrupt stack */
1415ae115bc7Smrj 			dispatch_hilevel(vector, 0);
1416ae115bc7Smrj 		}
1417ae115bc7Smrj 		(void) hilevel_intr_epilog(cpu, newipl, oldipl, vector);
1418ae115bc7Smrj 	} else {
1419ae115bc7Smrj 		/*
1420ae115bc7Smrj 		 * Run this interrupt in a separate thread.
1421ae115bc7Smrj 		 */
1422ae115bc7Smrj 		newsp = intr_thread_prolog(cpu, (caddr_t)rp, newipl);
1423ae115bc7Smrj 		switch_sp_and_call(newsp, dispatch_hardint, vector, oldipl);
1424ae115bc7Smrj 	}
1425ae115bc7Smrj 
1426349b53ddSStuart Maybee #if !defined(__xpv)
1427ae115bc7Smrj 	/*
1428ae115bc7Smrj 	 * Deliver any pending soft interrupts.
1429ae115bc7Smrj 	 */
1430ae115bc7Smrj 	if (cpu->cpu_softinfo.st_pending)
1431ae115bc7Smrj 		dosoftint(rp);
1432349b53ddSStuart Maybee #endif	/* !__xpv */
1433ae115bc7Smrj }
1434ae115bc7Smrj 
1435349b53ddSStuart Maybee 
1436ae115bc7Smrj /*
1437ae115bc7Smrj  * Common tasks always done by _sys_rtt, called with interrupts disabled.
1438ae115bc7Smrj  * Returns 1 if returning to userland, 0 if returning to system mode.
1439ae115bc7Smrj  */
1440ae115bc7Smrj int
sys_rtt_common(struct regs * rp)1441ae115bc7Smrj sys_rtt_common(struct regs *rp)
1442ae115bc7Smrj {
1443ae115bc7Smrj 	kthread_t *tp;
1444ae115bc7Smrj 	extern void mutex_exit_critical_start();
1445ae115bc7Smrj 	extern long mutex_exit_critical_size;
1446575a7426Spt 	extern void mutex_owner_running_critical_start();
1447575a7426Spt 	extern long mutex_owner_running_critical_size;
1448ae115bc7Smrj 
1449ae115bc7Smrj loop:
1450ae115bc7Smrj 
1451ae115bc7Smrj 	/*
1452ae115bc7Smrj 	 * Check if returning to user
1453ae115bc7Smrj 	 */
1454ae115bc7Smrj 	tp = CPU->cpu_thread;
1455ae115bc7Smrj 	if (USERMODE(rp->r_cs)) {
14564c28a617SRobert Mustacchi 		pcb_t *pcb;
14574c28a617SRobert Mustacchi 
1458ae115bc7Smrj 		/*
1459ae115bc7Smrj 		 * Check if AST pending.
1460ae115bc7Smrj 		 */
1461ae115bc7Smrj 		if (tp->t_astflag) {
1462ae115bc7Smrj 			/*
1463ae115bc7Smrj 			 * Let trap() handle the AST
1464ae115bc7Smrj 			 */
1465ae115bc7Smrj 			sti();
1466ae115bc7Smrj 			rp->r_trapno = T_AST;
1467ae115bc7Smrj 			trap(rp, (caddr_t)0, CPU->cpu_id);
1468ae115bc7Smrj 			cli();
1469ae115bc7Smrj 			goto loop;
1470ae115bc7Smrj 		}
1471ae115bc7Smrj 
14724c28a617SRobert Mustacchi 		pcb = &tp->t_lwp->lwp_pcb;
14734c28a617SRobert Mustacchi 
14744c28a617SRobert Mustacchi 		/*
14754c28a617SRobert Mustacchi 		 * Check to see if we need to initialize the FPU for this
14764c28a617SRobert Mustacchi 		 * thread. This should be an uncommon occurrence, but may happen
14774c28a617SRobert Mustacchi 		 * in the case where the system creates an lwp through an
14784c28a617SRobert Mustacchi 		 * abnormal path such as the agent lwp. Make sure that we still
14794c28a617SRobert Mustacchi 		 * happen to have the FPU in a good state.
14804c28a617SRobert Mustacchi 		 */
14814c28a617SRobert Mustacchi 		if ((pcb->pcb_fpu.fpu_flags & FPU_EN) == 0) {
14824c28a617SRobert Mustacchi 			kpreempt_disable();
14834c28a617SRobert Mustacchi 			fp_seed();
14844c28a617SRobert Mustacchi 			kpreempt_enable();
14854c28a617SRobert Mustacchi 			PCB_SET_UPDATE_FPU(pcb);
14864c28a617SRobert Mustacchi 		}
14874c28a617SRobert Mustacchi 
1488ae115bc7Smrj 		/*
1489ae115bc7Smrj 		 * We are done if segment registers do not need updating.
1490ae115bc7Smrj 		 */
14914c28a617SRobert Mustacchi 		if (!PCB_NEED_UPDATE(pcb))
1492ae115bc7Smrj 			return (1);
1493ae115bc7Smrj 
14944c28a617SRobert Mustacchi 		if (PCB_NEED_UPDATE_SEGS(pcb) && update_sregs(rp, tp->t_lwp)) {
1495ae115bc7Smrj 			/*
1496ae115bc7Smrj 			 * 1 or more of the selectors is bad.
1497ae115bc7Smrj 			 * Deliver a SIGSEGV.
1498ae115bc7Smrj 			 */
1499ae115bc7Smrj 			proc_t *p = ttoproc(tp);
1500ae115bc7Smrj 
1501ae115bc7Smrj 			sti();
1502ae115bc7Smrj 			mutex_enter(&p->p_lock);
1503ae115bc7Smrj 			tp->t_lwp->lwp_cursig = SIGSEGV;
1504ae115bc7Smrj 			mutex_exit(&p->p_lock);
1505ae115bc7Smrj 			psig();
1506ae115bc7Smrj 			tp->t_sig_check = 1;
1507ae115bc7Smrj 			cli();
1508ae115bc7Smrj 		}
15094c28a617SRobert Mustacchi 		PCB_CLEAR_UPDATE_SEGS(pcb);
15104c28a617SRobert Mustacchi 
15114c28a617SRobert Mustacchi 		if (PCB_NEED_UPDATE_FPU(pcb)) {
15124c28a617SRobert Mustacchi 			fprestore_ctxt(&pcb->pcb_fpu);
15134c28a617SRobert Mustacchi 		}
15144c28a617SRobert Mustacchi 		PCB_CLEAR_UPDATE_FPU(pcb);
15154c28a617SRobert Mustacchi 
15164c28a617SRobert Mustacchi 		ASSERT0(PCB_NEED_UPDATE(pcb));
1517ae115bc7Smrj 
1518ae115bc7Smrj 		return (1);
1519ae115bc7Smrj 	}
1520ae115bc7Smrj 
152174ecdb51SJohn Levon #if !defined(__xpv)
152274ecdb51SJohn Levon 	/*
152374ecdb51SJohn Levon 	 * Assert that we're not trying to return into the syscall return
152474ecdb51SJohn Levon 	 * trampolines. Things will go baaaaad if we try to do that.
152574ecdb51SJohn Levon 	 *
152674ecdb51SJohn Levon 	 * Note that none of these run with interrupts on, so this should
152774ecdb51SJohn Levon 	 * never happen (even in the sysexit case the STI doesn't take effect
152874ecdb51SJohn Levon 	 * until after sysexit finishes).
152974ecdb51SJohn Levon 	 */
153074ecdb51SJohn Levon 	extern void tr_sysc_ret_start();
153174ecdb51SJohn Levon 	extern void tr_sysc_ret_end();
153274ecdb51SJohn Levon 	ASSERT(!(rp->r_pc >= (uintptr_t)tr_sysc_ret_start &&
153374ecdb51SJohn Levon 	    rp->r_pc <= (uintptr_t)tr_sysc_ret_end));
153474ecdb51SJohn Levon #endif
153574ecdb51SJohn Levon 
1536ae115bc7Smrj 	/*
1537ae115bc7Smrj 	 * Here if we are returning to supervisor mode.
1538ae115bc7Smrj 	 * Check for a kernel preemption request.
1539ae115bc7Smrj 	 */
1540ae115bc7Smrj 	if (CPU->cpu_kprunrun && (rp->r_ps & PS_IE)) {
1541ae115bc7Smrj 
1542ae115bc7Smrj 		/*
1543ae115bc7Smrj 		 * Do nothing if already in kpreempt
1544ae115bc7Smrj 		 */
1545ae115bc7Smrj 		if (!tp->t_preempt_lk) {
1546ae115bc7Smrj 			tp->t_preempt_lk = 1;
1547ae115bc7Smrj 			sti();
1548ae115bc7Smrj 			kpreempt(1); /* asynchronous kpreempt call */
1549ae115bc7Smrj 			cli();
1550ae115bc7Smrj 			tp->t_preempt_lk = 0;
1551ae115bc7Smrj 		}
1552ae115bc7Smrj 	}
1553ae115bc7Smrj 
1554ae115bc7Smrj 	/*
1555ae115bc7Smrj 	 * If we interrupted the mutex_exit() critical region we must
1556ae115bc7Smrj 	 * reset the PC back to the beginning to prevent missed wakeups
1557ae115bc7Smrj 	 * See the comments in mutex_exit() for details.
1558ae115bc7Smrj 	 */
1559ae115bc7Smrj 	if ((uintptr_t)rp->r_pc - (uintptr_t)mutex_exit_critical_start <
1560ae115bc7Smrj 	    mutex_exit_critical_size) {
1561ae115bc7Smrj 		rp->r_pc = (greg_t)mutex_exit_critical_start;
1562ae115bc7Smrj 	}
1563575a7426Spt 
1564575a7426Spt 	/*
1565575a7426Spt 	 * If we interrupted the mutex_owner_running() critical region we
1566575a7426Spt 	 * must reset the PC back to the beginning to prevent dereferencing
1567575a7426Spt 	 * of a freed thread pointer. See the comments in mutex_owner_running
1568575a7426Spt 	 * for details.
1569575a7426Spt 	 */
1570575a7426Spt 	if ((uintptr_t)rp->r_pc -
1571575a7426Spt 	    (uintptr_t)mutex_owner_running_critical_start <
1572575a7426Spt 	    mutex_owner_running_critical_size) {
1573575a7426Spt 		rp->r_pc = (greg_t)mutex_owner_running_critical_start;
1574575a7426Spt 	}
1575575a7426Spt 
1576ae115bc7Smrj 	return (0);
1577ae115bc7Smrj }
1578ae115bc7Smrj 
1579ae115bc7Smrj void
send_dirint(int cpuid,int int_level)1580ae115bc7Smrj send_dirint(int cpuid, int int_level)
1581ae115bc7Smrj {
1582ae115bc7Smrj 	(*send_dirintf)(cpuid, int_level);
1583ae115bc7Smrj }
1584ae115bc7Smrj 
15857ff178cdSJimmy Vetayases #define	IS_FAKE_SOFTINT(flag, newpri)		\
15867ff178cdSJimmy Vetayases 	(((flag) & PS_IE) &&				\
15877ff178cdSJimmy Vetayases 	    (((*get_pending_spl)() > (newpri)) ||	\
15887ff178cdSJimmy Vetayases 	    bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > (newpri)))
15897ff178cdSJimmy Vetayases 
1590ae115bc7Smrj /*
1591ae115bc7Smrj  * do_splx routine, takes new ipl to set
1592ae115bc7Smrj  * returns the old ipl.
1593ae115bc7Smrj  * We are careful not to set priority lower than CPU->cpu_base_pri,
1594ae115bc7Smrj  * even though it seems we're raising the priority, it could be set
1595ae115bc7Smrj  * higher at any time by an interrupt routine, so we must block interrupts
1596ae115bc7Smrj  * and look at CPU->cpu_base_pri
1597ae115bc7Smrj  */
1598ae115bc7Smrj int
do_splx(int newpri)1599ae115bc7Smrj do_splx(int newpri)
1600ae115bc7Smrj {
1601ae115bc7Smrj 	ulong_t	flag;
1602ae115bc7Smrj 	cpu_t	*cpu;
1603ae115bc7Smrj 	int	curpri, basepri;
1604ae115bc7Smrj 
1605ae115bc7Smrj 	flag = intr_clear();
1606ae115bc7Smrj 	cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
1607ae115bc7Smrj 	curpri = cpu->cpu_m.mcpu_pri;
1608ae115bc7Smrj 	basepri = cpu->cpu_base_spl;
1609ae115bc7Smrj 	if (newpri < basepri)
1610ae115bc7Smrj 		newpri = basepri;
1611ae115bc7Smrj 	cpu->cpu_m.mcpu_pri = newpri;
1612ae115bc7Smrj 	(*setspl)(newpri);
1613ae115bc7Smrj 	/*
1614ae115bc7Smrj 	 * If we are going to reenable interrupts see if new priority level
1615ae115bc7Smrj 	 * allows pending softint delivery.
1616ae115bc7Smrj 	 */
16177ff178cdSJimmy Vetayases 	if (IS_FAKE_SOFTINT(flag, newpri))
1618ae115bc7Smrj 		fakesoftint();
1619ae115bc7Smrj 	ASSERT(!interrupts_enabled());
1620ae115bc7Smrj 	intr_restore(flag);
1621ae115bc7Smrj 	return (curpri);
1622ae115bc7Smrj }
1623ae115bc7Smrj 
1624ae115bc7Smrj /*
1625ae115bc7Smrj  * Common spl raise routine, takes new ipl to set
1626ae115bc7Smrj  * returns the old ipl, will not lower ipl.
1627ae115bc7Smrj  */
1628ae115bc7Smrj int
splr(int newpri)1629ae115bc7Smrj splr(int newpri)
1630ae115bc7Smrj {
1631ae115bc7Smrj 	ulong_t	flag;
1632ae115bc7Smrj 	cpu_t	*cpu;
1633ae115bc7Smrj 	int	curpri, basepri;
1634ae115bc7Smrj 
1635ae115bc7Smrj 	flag = intr_clear();
1636ae115bc7Smrj 	cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
1637ae115bc7Smrj 	curpri = cpu->cpu_m.mcpu_pri;
1638ae115bc7Smrj 	/*
1639ae115bc7Smrj 	 * Only do something if new priority is larger
1640ae115bc7Smrj 	 */
1641ae115bc7Smrj 	if (newpri > curpri) {
1642ae115bc7Smrj 		basepri = cpu->cpu_base_spl;
1643ae115bc7Smrj 		if (newpri < basepri)
1644ae115bc7Smrj 			newpri = basepri;
1645ae115bc7Smrj 		cpu->cpu_m.mcpu_pri = newpri;
1646ae115bc7Smrj 		(*setspl)(newpri);
1647ae115bc7Smrj 		/*
1648ae115bc7Smrj 		 * See if new priority level allows pending softint delivery
1649ae115bc7Smrj 		 */
16507ff178cdSJimmy Vetayases 		if (IS_FAKE_SOFTINT(flag, newpri))
1651ae115bc7Smrj 			fakesoftint();
1652ae115bc7Smrj 	}
1653ae115bc7Smrj 	intr_restore(flag);
1654ae115bc7Smrj 	return (curpri);
1655ae115bc7Smrj }
1656ae115bc7Smrj 
1657ae115bc7Smrj int
getpil(void)1658ae115bc7Smrj getpil(void)
1659ae115bc7Smrj {
1660ae115bc7Smrj 	return (CPU->cpu_m.mcpu_pri);
1661ae115bc7Smrj }
1662ae115bc7Smrj 
1663b885580bSAlexander Kolbasov int
spl_xcall(void)1664b885580bSAlexander Kolbasov spl_xcall(void)
1665b885580bSAlexander Kolbasov {
1666b885580bSAlexander Kolbasov 	return (splr(ipltospl(XCALL_PIL)));
1667b885580bSAlexander Kolbasov }
1668b885580bSAlexander Kolbasov 
1669ae115bc7Smrj int
interrupts_enabled(void)1670ae115bc7Smrj interrupts_enabled(void)
1671ae115bc7Smrj {
1672ae115bc7Smrj 	ulong_t	flag;
1673ae115bc7Smrj 
1674ae115bc7Smrj 	flag = getflags();
1675ae115bc7Smrj 	return ((flag & PS_IE) == PS_IE);
1676ae115bc7Smrj }
1677ae115bc7Smrj 
1678ae115bc7Smrj #ifdef DEBUG
1679ae115bc7Smrj void
assert_ints_enabled(void)1680ae115bc7Smrj assert_ints_enabled(void)
1681ae115bc7Smrj {
1682ae115bc7Smrj 	ASSERT(!interrupts_unleashed || interrupts_enabled());
1683ae115bc7Smrj }
1684ae115bc7Smrj #endif	/* DEBUG */
1685