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.
24636dfb4bSJerry Jelinek * Copyright (c) 2012, Joyent, Inc. All rights reserverd.
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>
46995c0a3c8Sjosephb #include <vm/hat_i86.h>
470843e1988Sjohnlev #if defined(__xpv)
471843e1988Sjohnlev #include <sys/hypervisor.h>
472843e1988Sjohnlev #endif
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate
475843e1988Sjohnlev #if defined(__xpv) && defined(DEBUG)
476843e1988Sjohnlev
477843e1988Sjohnlev /*
478843e1988Sjohnlev * This panic message is intended as an aid to interrupt debugging.
479843e1988Sjohnlev *
480843e1988Sjohnlev * The associated assertion tests the condition of enabling
481843e1988Sjohnlev * events when events are already enabled. The implication
482843e1988Sjohnlev * being that whatever code the programmer thought was
483843e1988Sjohnlev * protected by having events disabled until the second
484843e1988Sjohnlev * enable happened really wasn't protected at all ..
485843e1988Sjohnlev */
486843e1988Sjohnlev
487843e1988Sjohnlev int stistipanic = 1; /* controls the debug panic check */
488843e1988Sjohnlev const char *stistimsg = "stisti";
489843e1988Sjohnlev ulong_t laststi[NCPU];
490843e1988Sjohnlev
491843e1988Sjohnlev /*
492843e1988Sjohnlev * This variable tracks the last place events were disabled on each cpu
493fb2caebeSRandy Fishel * it assists in debugging when asserts that interrupts are enabled trip.
494843e1988Sjohnlev */
495843e1988Sjohnlev ulong_t lastcli[NCPU];
496843e1988Sjohnlev
497843e1988Sjohnlev #endif
498843e1988Sjohnlev
4997ff178cdSJimmy Vetayases void do_interrupt(struct regs *rp, trap_trace_rec_t *ttp);
5007ff178cdSJimmy Vetayases
5017ff178cdSJimmy Vetayases void (*do_interrupt_common)(struct regs *, trap_trace_rec_t *) = do_interrupt;
5027ff178cdSJimmy Vetayases uintptr_t (*get_intr_handler)(int, short) = NULL;
5037ff178cdSJimmy Vetayases
5047c478bd9Sstevel@tonic-gate /*
505ae115bc7Smrj * Set cpu's base SPL level to the highest active interrupt level
5067c478bd9Sstevel@tonic-gate */
507ae115bc7Smrj void
set_base_spl(void)508ae115bc7Smrj set_base_spl(void)
5097c478bd9Sstevel@tonic-gate {
510ae115bc7Smrj struct cpu *cpu = CPU;
511ae115bc7Smrj uint16_t active = (uint16_t)cpu->cpu_intr_actv;
5127c478bd9Sstevel@tonic-gate
513ae115bc7Smrj cpu->cpu_base_spl = active == 0 ? 0 : bsrw_insn(active);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate
5167c478bd9Sstevel@tonic-gate /*
5177c478bd9Sstevel@tonic-gate * Do all the work necessary to set up the cpu and thread structures
5187c478bd9Sstevel@tonic-gate * to dispatch a high-level interrupt.
5197c478bd9Sstevel@tonic-gate *
5207c478bd9Sstevel@tonic-gate * Returns 0 if we're -not- already on the high-level interrupt stack,
5217c478bd9Sstevel@tonic-gate * (and *must* switch to it), non-zero if we are already on that stack.
5227c478bd9Sstevel@tonic-gate *
5237c478bd9Sstevel@tonic-gate * Called with interrupts masked.
5247c478bd9Sstevel@tonic-gate * The 'pil' is already set to the appropriate level for rp->r_trapno.
5257c478bd9Sstevel@tonic-gate */
526ae115bc7Smrj static int
hilevel_intr_prolog(struct cpu * cpu,uint_t pil,uint_t oldpil,struct regs * rp)5277c478bd9Sstevel@tonic-gate hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m;
5307c478bd9Sstevel@tonic-gate uint_t mask;
531eda89462Sesolom hrtime_t intrtime;
532ae115bc7Smrj hrtime_t now = tsc_read();
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate ASSERT(pil > LOCK_LEVEL);
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate if (pil == CBE_HIGH_PIL) {
5377c478bd9Sstevel@tonic-gate cpu->cpu_profile_pil = oldpil;
5387c478bd9Sstevel@tonic-gate if (USERMODE(rp->r_cs)) {
5397c478bd9Sstevel@tonic-gate cpu->cpu_profile_pc = 0;
5407c478bd9Sstevel@tonic-gate cpu->cpu_profile_upc = rp->r_pc;
541b9e93c10SJonathan Haslam cpu->cpu_cpcprofile_pc = 0;
542b9e93c10SJonathan Haslam cpu->cpu_cpcprofile_upc = rp->r_pc;
5437c478bd9Sstevel@tonic-gate } else {
5447c478bd9Sstevel@tonic-gate cpu->cpu_profile_pc = rp->r_pc;
5457c478bd9Sstevel@tonic-gate cpu->cpu_profile_upc = 0;
546b9e93c10SJonathan Haslam cpu->cpu_cpcprofile_pc = rp->r_pc;
547b9e93c10SJonathan Haslam cpu->cpu_cpcprofile_upc = 0;
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
5527c478bd9Sstevel@tonic-gate if (mask != 0) {
5537c478bd9Sstevel@tonic-gate int nestpil;
5547c478bd9Sstevel@tonic-gate
5557c478bd9Sstevel@tonic-gate /*
5567c478bd9Sstevel@tonic-gate * We have interrupted another high-level interrupt.
5577c478bd9Sstevel@tonic-gate * Load starting timestamp, compute interval, update
5587c478bd9Sstevel@tonic-gate * cumulative counter.
5597c478bd9Sstevel@tonic-gate */
5607c478bd9Sstevel@tonic-gate nestpil = bsrw_insn((uint16_t)mask);
5617c478bd9Sstevel@tonic-gate ASSERT(nestpil < pil);
562ae115bc7Smrj intrtime = now -
5637c478bd9Sstevel@tonic-gate mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)];
5647a364d25Sschwartz mcpu->intrstat[nestpil][0] += intrtime;
565eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate * Another high-level interrupt is active below this one, so
5687c478bd9Sstevel@tonic-gate * there is no need to check for an interrupt thread. That
5697c478bd9Sstevel@tonic-gate * will be done by the lowest priority high-level interrupt
5707c478bd9Sstevel@tonic-gate * active.
5717c478bd9Sstevel@tonic-gate */
5727c478bd9Sstevel@tonic-gate } else {
5737c478bd9Sstevel@tonic-gate kthread_t *t = cpu->cpu_thread;
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate * See if we are interrupting a low-level interrupt thread.
5777c478bd9Sstevel@tonic-gate * If so, account for its time slice only if its time stamp
5787c478bd9Sstevel@tonic-gate * is non-zero.
5797c478bd9Sstevel@tonic-gate */
5807c478bd9Sstevel@tonic-gate if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) {
581ae115bc7Smrj intrtime = now - t->t_intr_start;
5827a364d25Sschwartz mcpu->intrstat[t->t_pil][0] += intrtime;
583eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
5847c478bd9Sstevel@tonic-gate t->t_intr_start = 0;
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate
5887c478bd9Sstevel@tonic-gate /*
5897c478bd9Sstevel@tonic-gate * Store starting timestamp in CPU structure for this PIL.
5907c478bd9Sstevel@tonic-gate */
591ae115bc7Smrj mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = now;
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
5947c478bd9Sstevel@tonic-gate
5957c478bd9Sstevel@tonic-gate if (pil == 15) {
5967c478bd9Sstevel@tonic-gate /*
5977c478bd9Sstevel@tonic-gate * To support reentrant level 15 interrupts, we maintain a
5987c478bd9Sstevel@tonic-gate * recursion count in the top half of cpu_intr_actv. Only
5997c478bd9Sstevel@tonic-gate * when this count hits zero do we clear the PIL 15 bit from
6007c478bd9Sstevel@tonic-gate * the lower half of cpu_intr_actv.
6017c478bd9Sstevel@tonic-gate */
6027c478bd9Sstevel@tonic-gate uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
6037c478bd9Sstevel@tonic-gate (*refcntp)++;
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate
6067c478bd9Sstevel@tonic-gate mask = cpu->cpu_intr_actv;
6077c478bd9Sstevel@tonic-gate
6087c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil);
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate * Does most of the work of returning from a high level interrupt.
6157c478bd9Sstevel@tonic-gate *
6167c478bd9Sstevel@tonic-gate * Returns 0 if there are no more high level interrupts (in which
6177c478bd9Sstevel@tonic-gate * case we must switch back to the interrupted thread stack) or
6187c478bd9Sstevel@tonic-gate * non-zero if there are more (in which case we should stay on it).
6197c478bd9Sstevel@tonic-gate *
6207c478bd9Sstevel@tonic-gate * Called with interrupts masked
6217c478bd9Sstevel@tonic-gate */
622ae115bc7Smrj static int
hilevel_intr_epilog(struct cpu * cpu,uint_t pil,uint_t oldpil,uint_t vecnum)6237c478bd9Sstevel@tonic-gate hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m;
6267c478bd9Sstevel@tonic-gate uint_t mask;
627eda89462Sesolom hrtime_t intrtime;
628ae115bc7Smrj hrtime_t now = tsc_read();
6297c478bd9Sstevel@tonic-gate
6307c478bd9Sstevel@tonic-gate ASSERT(mcpu->mcpu_pri == pil);
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++;
6337c478bd9Sstevel@tonic-gate
6347c478bd9Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil));
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate if (pil == 15) {
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate * To support reentrant level 15 interrupts, we maintain a
6397c478bd9Sstevel@tonic-gate * recursion count in the top half of cpu_intr_actv. Only
6407c478bd9Sstevel@tonic-gate * when this count hits zero do we clear the PIL 15 bit from
6417c478bd9Sstevel@tonic-gate * the lower half of cpu_intr_actv.
6427c478bd9Sstevel@tonic-gate */
6437c478bd9Sstevel@tonic-gate uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
6447c478bd9Sstevel@tonic-gate
6457c478bd9Sstevel@tonic-gate ASSERT(*refcntp > 0);
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate if (--(*refcntp) == 0)
6487c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil);
6497c478bd9Sstevel@tonic-gate } else {
6507c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0);
6547c478bd9Sstevel@tonic-gate
655ae115bc7Smrj intrtime = now - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)];
6567a364d25Sschwartz mcpu->intrstat[pil][0] += intrtime;
657eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate * Check for lower-pil nested high-level interrupt beneath
6617c478bd9Sstevel@tonic-gate * current one. If so, place a starting timestamp in its
6627c478bd9Sstevel@tonic-gate * pil_high_start entry.
6637c478bd9Sstevel@tonic-gate */
6647c478bd9Sstevel@tonic-gate mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
6657c478bd9Sstevel@tonic-gate if (mask != 0) {
6667c478bd9Sstevel@tonic-gate int nestpil;
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate /*
6697c478bd9Sstevel@tonic-gate * find PIL of nested interrupt
6707c478bd9Sstevel@tonic-gate */
6717c478bd9Sstevel@tonic-gate nestpil = bsrw_insn((uint16_t)mask);
6727c478bd9Sstevel@tonic-gate ASSERT(nestpil < pil);
673ae115bc7Smrj mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = now;
6747c478bd9Sstevel@tonic-gate /*
6757c478bd9Sstevel@tonic-gate * (Another high-level interrupt is active below this one,
6767c478bd9Sstevel@tonic-gate * so there is no need to check for an interrupt
6777c478bd9Sstevel@tonic-gate * thread. That will be done by the lowest priority
6787c478bd9Sstevel@tonic-gate * high-level interrupt active.)
6797c478bd9Sstevel@tonic-gate */
6807c478bd9Sstevel@tonic-gate } else {
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate * Check to see if there is a low-level interrupt active.
6837c478bd9Sstevel@tonic-gate * If so, place a starting timestamp in the thread
6847c478bd9Sstevel@tonic-gate * structure.
6857c478bd9Sstevel@tonic-gate */
6867c478bd9Sstevel@tonic-gate kthread_t *t = cpu->cpu_thread;
6877c478bd9Sstevel@tonic-gate
6887c478bd9Sstevel@tonic-gate if (t->t_flag & T_INTR_THREAD)
689ae115bc7Smrj t->t_intr_start = now;
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate
6927c478bd9Sstevel@tonic-gate mcpu->mcpu_pri = oldpil;
6937c478bd9Sstevel@tonic-gate (void) (*setlvlx)(oldpil, vecnum);
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate
6987c478bd9Sstevel@tonic-gate /*
6997c478bd9Sstevel@tonic-gate * Set up the cpu, thread and interrupt thread structures for
7007c478bd9Sstevel@tonic-gate * executing an interrupt thread. The new stack pointer of the
7017c478bd9Sstevel@tonic-gate * interrupt thread (which *must* be switched to) is returned.
7027c478bd9Sstevel@tonic-gate */
703ae115bc7Smrj static caddr_t
intr_thread_prolog(struct cpu * cpu,caddr_t stackptr,uint_t pil)7047c478bd9Sstevel@tonic-gate intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m;
7077c478bd9Sstevel@tonic-gate kthread_t *t, *volatile it;
708ae115bc7Smrj hrtime_t now = tsc_read();
7097c478bd9Sstevel@tonic-gate
7107c478bd9Sstevel@tonic-gate ASSERT(pil > 0);
7117c478bd9Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
7127c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil);
7137c478bd9Sstevel@tonic-gate
7147c478bd9Sstevel@tonic-gate /*
7157c478bd9Sstevel@tonic-gate * Get set to run an interrupt thread.
7167c478bd9Sstevel@tonic-gate * There should always be an interrupt thread, since we
7177c478bd9Sstevel@tonic-gate * allocate one for each level on each CPU.
7187c478bd9Sstevel@tonic-gate *
719fd71cd2fSesolom * t_intr_start could be zero due to cpu_intr_swtch_enter.
7207c478bd9Sstevel@tonic-gate */
7217c478bd9Sstevel@tonic-gate t = cpu->cpu_thread;
722fd71cd2fSesolom if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
723ae115bc7Smrj hrtime_t intrtime = now - t->t_intr_start;
7247a364d25Sschwartz mcpu->intrstat[t->t_pil][0] += intrtime;
725eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
7267c478bd9Sstevel@tonic-gate t->t_intr_start = 0;
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate
7297c478bd9Sstevel@tonic-gate ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
7307c478bd9Sstevel@tonic-gate
7317c478bd9Sstevel@tonic-gate t->t_sp = (uintptr_t)stackptr; /* mark stack in curthread for resume */
7327c478bd9Sstevel@tonic-gate
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate * unlink the interrupt thread off the cpu
735fd71cd2fSesolom *
736fd71cd2fSesolom * Note that the code in kcpc_overflow_intr -relies- on the
737fd71cd2fSesolom * ordering of events here - in particular that t->t_lwp of
738fd71cd2fSesolom * the interrupt thread is set to the pinned thread *before*
739fd71cd2fSesolom * curthread is changed.
7407c478bd9Sstevel@tonic-gate */
7417c478bd9Sstevel@tonic-gate it = cpu->cpu_intr_thread;
7427c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it->t_link;
7437c478bd9Sstevel@tonic-gate it->t_intr = t;
7447c478bd9Sstevel@tonic-gate it->t_lwp = t->t_lwp;
7457c478bd9Sstevel@tonic-gate
7467c478bd9Sstevel@tonic-gate /*
7477c478bd9Sstevel@tonic-gate * (threads on the interrupt thread free list could have state
7487c478bd9Sstevel@tonic-gate * preset to TS_ONPROC, but it helps in debugging if
7497c478bd9Sstevel@tonic-gate * they're TS_FREE.)
7507c478bd9Sstevel@tonic-gate */
7517c478bd9Sstevel@tonic-gate it->t_state = TS_ONPROC;
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate cpu->cpu_thread = it; /* new curthread on this cpu */
7547c478bd9Sstevel@tonic-gate it->t_pil = (uchar_t)pil;
7557c478bd9Sstevel@tonic-gate it->t_pri = intr_pri + (pri_t)pil;
756ae115bc7Smrj it->t_intr_start = now;
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate return (it->t_stk);
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate
7617c478bd9Sstevel@tonic-gate
7627c478bd9Sstevel@tonic-gate #ifdef DEBUG
7637c478bd9Sstevel@tonic-gate int intr_thread_cnt;
7647c478bd9Sstevel@tonic-gate #endif
7657c478bd9Sstevel@tonic-gate
7667c478bd9Sstevel@tonic-gate /*
7677c478bd9Sstevel@tonic-gate * Called with interrupts disabled
7687c478bd9Sstevel@tonic-gate */
769ae115bc7Smrj static void
intr_thread_epilog(struct cpu * cpu,uint_t vec,uint_t oldpil)7707c478bd9Sstevel@tonic-gate intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil)
7717c478bd9Sstevel@tonic-gate {
7727c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m;
7737c478bd9Sstevel@tonic-gate kthread_t *t;
7747c478bd9Sstevel@tonic-gate kthread_t *it = cpu->cpu_thread; /* curthread */
7757c478bd9Sstevel@tonic-gate uint_t pil, basespl;
776eda89462Sesolom hrtime_t intrtime;
777ae115bc7Smrj hrtime_t now = tsc_read();
7787c478bd9Sstevel@tonic-gate
7797c478bd9Sstevel@tonic-gate pil = it->t_pil;
7807c478bd9Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++;
7817c478bd9Sstevel@tonic-gate
7827c478bd9Sstevel@tonic-gate ASSERT(it->t_intr_start != 0);
783ae115bc7Smrj intrtime = now - it->t_intr_start;
7847a364d25Sschwartz mcpu->intrstat[pil][0] += intrtime;
785eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
7867c478bd9Sstevel@tonic-gate
7877c478bd9Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil));
7887c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil);
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate * If there is still an interrupted thread underneath this one
7927c478bd9Sstevel@tonic-gate * then the interrupt was never blocked and the return is
7937c478bd9Sstevel@tonic-gate * fairly simple. Otherwise it isn't.
7947c478bd9Sstevel@tonic-gate */
7957c478bd9Sstevel@tonic-gate if ((t = it->t_intr) == NULL) {
7967c478bd9Sstevel@tonic-gate /*
7977c478bd9Sstevel@tonic-gate * The interrupted thread is no longer pinned underneath
7987c478bd9Sstevel@tonic-gate * the interrupt thread. This means the interrupt must
7997c478bd9Sstevel@tonic-gate * have blocked, and the interrupted thread has been
8007c478bd9Sstevel@tonic-gate * unpinned, and has probably been running around the
8017c478bd9Sstevel@tonic-gate * system for a while.
8027c478bd9Sstevel@tonic-gate *
8037c478bd9Sstevel@tonic-gate * Since there is no longer a thread under this one, put
8047c478bd9Sstevel@tonic-gate * this interrupt thread back on the CPU's free list and
8057c478bd9Sstevel@tonic-gate * resume the idle thread which will dispatch the next
8067c478bd9Sstevel@tonic-gate * thread to run.
8077c478bd9Sstevel@tonic-gate */
8087c478bd9Sstevel@tonic-gate #ifdef DEBUG
8097c478bd9Sstevel@tonic-gate intr_thread_cnt++;
8107c478bd9Sstevel@tonic-gate #endif
8117c478bd9Sstevel@tonic-gate cpu->cpu_stats.sys.intrblk++;
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate * Set CPU's base SPL based on active interrupts bitmask
8147c478bd9Sstevel@tonic-gate */
8157c478bd9Sstevel@tonic-gate set_base_spl();
8167c478bd9Sstevel@tonic-gate basespl = cpu->cpu_base_spl;
8177c478bd9Sstevel@tonic-gate mcpu->mcpu_pri = basespl;
8187c478bd9Sstevel@tonic-gate (*setlvlx)(basespl, vec);
8197c478bd9Sstevel@tonic-gate (void) splhigh();
820ae115bc7Smrj sti();
8217c478bd9Sstevel@tonic-gate it->t_state = TS_FREE;
8227c478bd9Sstevel@tonic-gate /*
8237c478bd9Sstevel@tonic-gate * Return interrupt thread to pool
8247c478bd9Sstevel@tonic-gate */
8257c478bd9Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread;
8267c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it;
8277c478bd9Sstevel@tonic-gate swtch();
828ae115bc7Smrj panic("intr_thread_epilog: swtch returned");
8297c478bd9Sstevel@tonic-gate /*NOTREACHED*/
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate
8327c478bd9Sstevel@tonic-gate /*
8337c478bd9Sstevel@tonic-gate * Return interrupt thread to the pool
8347c478bd9Sstevel@tonic-gate */
8357c478bd9Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread;
8367c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it;
8377c478bd9Sstevel@tonic-gate it->t_state = TS_FREE;
8387c478bd9Sstevel@tonic-gate
8397c478bd9Sstevel@tonic-gate basespl = cpu->cpu_base_spl;
8407c478bd9Sstevel@tonic-gate pil = MAX(oldpil, basespl);
8417c478bd9Sstevel@tonic-gate mcpu->mcpu_pri = pil;
8427c478bd9Sstevel@tonic-gate (*setlvlx)(pil, vec);
843ae115bc7Smrj t->t_intr_start = now;
8447c478bd9Sstevel@tonic-gate cpu->cpu_thread = t;
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate
8477a364d25Sschwartz /*
848ae115bc7Smrj * intr_get_time() is a resource for interrupt handlers to determine how
849ae115bc7Smrj * much time has been spent handling the current interrupt. Such a function
850ae115bc7Smrj * is needed because higher level interrupts can arrive during the
851ae115bc7Smrj * processing of an interrupt. intr_get_time() only returns time spent in the
852ae115bc7Smrj * current interrupt handler.
853ae115bc7Smrj *
854ae115bc7Smrj * The caller must be calling from an interrupt handler running at a pil
855ae115bc7Smrj * below or at lock level. Timings are not provided for high-level
856ae115bc7Smrj * interrupts.
857ae115bc7Smrj *
858ae115bc7Smrj * The first time intr_get_time() is called while handling an interrupt,
859ae115bc7Smrj * it returns the time since the interrupt handler was invoked. Subsequent
860ae115bc7Smrj * calls will return the time since the prior call to intr_get_time(). Time
861843e1988Sjohnlev * is returned as ticks. Use scalehrtimef() to convert ticks to nsec.
862ae115bc7Smrj *
863ae115bc7Smrj * Theory Of Intrstat[][]:
864ae115bc7Smrj *
865ae115bc7Smrj * uint64_t intrstat[pil][0..1] is an array indexed by pil level, with two
866ae115bc7Smrj * uint64_ts per pil.
867ae115bc7Smrj *
868ae115bc7Smrj * intrstat[pil][0] is a cumulative count of the number of ticks spent
869ae115bc7Smrj * handling all interrupts at the specified pil on this CPU. It is
870ae115bc7Smrj * exported via kstats to the user.
871ae115bc7Smrj *
872ae115bc7Smrj * intrstat[pil][1] is always a count of ticks less than or equal to the
873ae115bc7Smrj * value in [0]. The difference between [1] and [0] is the value returned
874ae115bc7Smrj * by a call to intr_get_time(). At the start of interrupt processing,
875ae115bc7Smrj * [0] and [1] will be equal (or nearly so). As the interrupt consumes
876ae115bc7Smrj * time, [0] will increase, but [1] will remain the same. A call to
877ae115bc7Smrj * intr_get_time() will return the difference, then update [1] to be the
878ae115bc7Smrj * same as [0]. Future calls will return the time since the last call.
879ae115bc7Smrj * Finally, when the interrupt completes, [1] is updated to the same as [0].
880ae115bc7Smrj *
881ae115bc7Smrj * Implementation:
882ae115bc7Smrj *
883ae115bc7Smrj * intr_get_time() works much like a higher level interrupt arriving. It
884ae115bc7Smrj * "checkpoints" the timing information by incrementing intrstat[pil][0]
885ae115bc7Smrj * to include elapsed running time, and by setting t_intr_start to rdtsc.
886ae115bc7Smrj * It then sets the return value to intrstat[pil][0] - intrstat[pil][1],
887ae115bc7Smrj * and updates intrstat[pil][1] to be the same as the new value of
888ae115bc7Smrj * intrstat[pil][0].
889ae115bc7Smrj *
890ae115bc7Smrj * In the normal handling of interrupts, after an interrupt handler returns
891ae115bc7Smrj * and the code in intr_thread() updates intrstat[pil][0], it then sets
892ae115bc7Smrj * intrstat[pil][1] to the new value of intrstat[pil][0]. When [0] == [1],
893ae115bc7Smrj * the timings are reset, i.e. intr_get_time() will return [0] - [1] which
894ae115bc7Smrj * is 0.
895ae115bc7Smrj *
896ae115bc7Smrj * Whenever interrupts arrive on a CPU which is handling a lower pil
897ae115bc7Smrj * interrupt, they update the lower pil's [0] to show time spent in the
898ae115bc7Smrj * handler that they've interrupted. This results in a growing discrepancy
899ae115bc7Smrj * between [0] and [1], which is returned the next time intr_get_time() is
900ae115bc7Smrj * called. Time spent in the higher-pil interrupt will not be returned in
901ae115bc7Smrj * the next intr_get_time() call from the original interrupt, because
902ae115bc7Smrj * the higher-pil interrupt's time is accumulated in intrstat[higherpil][].
9037a364d25Sschwartz */
9047a364d25Sschwartz uint64_t
intr_get_time(void)905ae115bc7Smrj intr_get_time(void)
9067a364d25Sschwartz {
907ae115bc7Smrj struct cpu *cpu;
908ae115bc7Smrj struct machcpu *mcpu;
909ae115bc7Smrj kthread_t *t;
9107a364d25Sschwartz uint64_t time, delta, ret;
911ae115bc7Smrj uint_t pil;
9127a364d25Sschwartz
913ae115bc7Smrj cli();
914ae115bc7Smrj cpu = CPU;
915ae115bc7Smrj mcpu = &cpu->cpu_m;
916ae115bc7Smrj t = cpu->cpu_thread;
917ae115bc7Smrj pil = t->t_pil;
9187a364d25Sschwartz ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0);
9197a364d25Sschwartz ASSERT(t->t_flag & T_INTR_THREAD);
9207a364d25Sschwartz ASSERT(pil != 0);
9217a364d25Sschwartz ASSERT(t->t_intr_start != 0);
9227a364d25Sschwartz
9237a364d25Sschwartz time = tsc_read();
9247a364d25Sschwartz delta = time - t->t_intr_start;
9257a364d25Sschwartz t->t_intr_start = time;
9267a364d25Sschwartz
9277a364d25Sschwartz time = mcpu->intrstat[pil][0] + delta;
9287a364d25Sschwartz ret = time - mcpu->intrstat[pil][1];
9297a364d25Sschwartz mcpu->intrstat[pil][0] = time;
9307a364d25Sschwartz mcpu->intrstat[pil][1] = time;
931c81508f4Sjhaslam cpu->cpu_intracct[cpu->cpu_mstate] += delta;
9327a364d25Sschwartz
933ae115bc7Smrj sti();
9347a364d25Sschwartz return (ret);
9357a364d25Sschwartz }
9367a364d25Sschwartz
937ae115bc7Smrj static caddr_t
dosoftint_prolog(struct cpu * cpu,caddr_t stackptr,uint32_t st_pending,uint_t oldpil)9387c478bd9Sstevel@tonic-gate dosoftint_prolog(
9397c478bd9Sstevel@tonic-gate struct cpu *cpu,
9407c478bd9Sstevel@tonic-gate caddr_t stackptr,
9417c478bd9Sstevel@tonic-gate uint32_t st_pending,
9427c478bd9Sstevel@tonic-gate uint_t oldpil)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate kthread_t *t, *volatile it;
9457c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m;
9467c478bd9Sstevel@tonic-gate uint_t pil;
947ae115bc7Smrj hrtime_t now;
9487c478bd9Sstevel@tonic-gate
9497c478bd9Sstevel@tonic-gate top:
9507c478bd9Sstevel@tonic-gate ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending);
9517c478bd9Sstevel@tonic-gate
9527c478bd9Sstevel@tonic-gate pil = bsrw_insn((uint16_t)st_pending);
9537c478bd9Sstevel@tonic-gate if (pil <= oldpil || pil <= cpu->cpu_base_spl)
9547c478bd9Sstevel@tonic-gate return (0);
9557c478bd9Sstevel@tonic-gate
9567c478bd9Sstevel@tonic-gate /*
9577c478bd9Sstevel@tonic-gate * XX64 Sigh.
9587c478bd9Sstevel@tonic-gate *
9597c478bd9Sstevel@tonic-gate * This is a transliteration of the i386 assembler code for
9607c478bd9Sstevel@tonic-gate * soft interrupts. One question is "why does this need
9617c478bd9Sstevel@tonic-gate * to be atomic?" One possible race is -other- processors
9627c478bd9Sstevel@tonic-gate * posting soft interrupts to us in set_pending() i.e. the
9637c478bd9Sstevel@tonic-gate * CPU might get preempted just after the address computation,
9647c478bd9Sstevel@tonic-gate * but just before the atomic transaction, so another CPU would
9657c478bd9Sstevel@tonic-gate * actually set the original CPU's st_pending bit. However,
9667c478bd9Sstevel@tonic-gate * it looks like it would be simpler to disable preemption there.
9677c478bd9Sstevel@tonic-gate * Are there other races for which preemption control doesn't work?
9687c478bd9Sstevel@tonic-gate *
9697c478bd9Sstevel@tonic-gate * The i386 assembler version -also- checks to see if the bit
9707c478bd9Sstevel@tonic-gate * being cleared was actually set; if it wasn't, it rechecks
9717c478bd9Sstevel@tonic-gate * for more. This seems a bit strange, as the only code that
9727c478bd9Sstevel@tonic-gate * ever clears the bit is -this- code running with interrupts
9737c478bd9Sstevel@tonic-gate * disabled on -this- CPU. This code would probably be cheaper:
9747c478bd9Sstevel@tonic-gate *
9757c478bd9Sstevel@tonic-gate * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending,
9767c478bd9Sstevel@tonic-gate * ~(1 << pil));
9777c478bd9Sstevel@tonic-gate *
9787c478bd9Sstevel@tonic-gate * and t->t_preempt--/++ around set_pending() even cheaper,
9797c478bd9Sstevel@tonic-gate * but at this point, correctness is critical, so we slavishly
9807c478bd9Sstevel@tonic-gate * emulate the i386 port.
9817c478bd9Sstevel@tonic-gate */
982ae115bc7Smrj if (atomic_btr32((uint32_t *)
983ae115bc7Smrj &mcpu->mcpu_softinfo.st_pending, pil) == 0) {
9847c478bd9Sstevel@tonic-gate st_pending = mcpu->mcpu_softinfo.st_pending;
9857c478bd9Sstevel@tonic-gate goto top;
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate mcpu->mcpu_pri = pil;
9897c478bd9Sstevel@tonic-gate (*setspl)(pil);
9907c478bd9Sstevel@tonic-gate
991ae115bc7Smrj now = tsc_read();
992ae115bc7Smrj
9937c478bd9Sstevel@tonic-gate /*
9947c478bd9Sstevel@tonic-gate * Get set to run interrupt thread.
9957c478bd9Sstevel@tonic-gate * There should always be an interrupt thread since we
9967c478bd9Sstevel@tonic-gate * allocate one for each level on the CPU.
9977c478bd9Sstevel@tonic-gate */
9987c478bd9Sstevel@tonic-gate it = cpu->cpu_intr_thread;
9997c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it->t_link;
10007c478bd9Sstevel@tonic-gate
1001fd71cd2fSesolom /* t_intr_start could be zero due to cpu_intr_swtch_enter. */
1002fd71cd2fSesolom t = cpu->cpu_thread;
1003fd71cd2fSesolom if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
1004ae115bc7Smrj hrtime_t intrtime = now - t->t_intr_start;
1005fd71cd2fSesolom mcpu->intrstat[pil][0] += intrtime;
1006fd71cd2fSesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1007fd71cd2fSesolom t->t_intr_start = 0;
1008fd71cd2fSesolom }
1009fd71cd2fSesolom
10107c478bd9Sstevel@tonic-gate /*
10117c478bd9Sstevel@tonic-gate * Note that the code in kcpc_overflow_intr -relies- on the
10127c478bd9Sstevel@tonic-gate * ordering of events here - in particular that t->t_lwp of
10137c478bd9Sstevel@tonic-gate * the interrupt thread is set to the pinned thread *before*
1014fd71cd2fSesolom * curthread is changed.
10157c478bd9Sstevel@tonic-gate */
10167c478bd9Sstevel@tonic-gate it->t_lwp = t->t_lwp;
10177c478bd9Sstevel@tonic-gate it->t_state = TS_ONPROC;
10187c478bd9Sstevel@tonic-gate
10197c478bd9Sstevel@tonic-gate /*
10207c478bd9Sstevel@tonic-gate * Push interrupted thread onto list from new thread.
10217c478bd9Sstevel@tonic-gate * Set the new thread as the current one.
10227c478bd9Sstevel@tonic-gate * Set interrupted thread's T_SP because if it is the idle thread,
10237c478bd9Sstevel@tonic-gate * resume() may use that stack between threads.
10247c478bd9Sstevel@tonic-gate */
10257c478bd9Sstevel@tonic-gate
10267c478bd9Sstevel@tonic-gate ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
10277c478bd9Sstevel@tonic-gate t->t_sp = (uintptr_t)stackptr;
10287c478bd9Sstevel@tonic-gate
10297c478bd9Sstevel@tonic-gate it->t_intr = t;
10307c478bd9Sstevel@tonic-gate cpu->cpu_thread = it;
10317c478bd9Sstevel@tonic-gate
10327c478bd9Sstevel@tonic-gate /*
10337c478bd9Sstevel@tonic-gate * Set bit for this pil in CPU's interrupt active bitmask.
10347c478bd9Sstevel@tonic-gate */
10357c478bd9Sstevel@tonic-gate ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
10367c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv |= (1 << pil);
10377c478bd9Sstevel@tonic-gate
10387c478bd9Sstevel@tonic-gate /*
10397c478bd9Sstevel@tonic-gate * Initialize thread priority level from intr_pri
10407c478bd9Sstevel@tonic-gate */
10417c478bd9Sstevel@tonic-gate it->t_pil = (uchar_t)pil;
10427c478bd9Sstevel@tonic-gate it->t_pri = (pri_t)pil + intr_pri;
1043ae115bc7Smrj it->t_intr_start = now;
10447c478bd9Sstevel@tonic-gate
10457c478bd9Sstevel@tonic-gate return (it->t_stk);
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate
1048ae115bc7Smrj static void
dosoftint_epilog(struct cpu * cpu,uint_t oldpil)10497c478bd9Sstevel@tonic-gate dosoftint_epilog(struct cpu *cpu, uint_t oldpil)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate struct machcpu *mcpu = &cpu->cpu_m;
10527c478bd9Sstevel@tonic-gate kthread_t *t, *it;
10537c478bd9Sstevel@tonic-gate uint_t pil, basespl;
1054eda89462Sesolom hrtime_t intrtime;
1055ae115bc7Smrj hrtime_t now = tsc_read();
10567c478bd9Sstevel@tonic-gate
10577c478bd9Sstevel@tonic-gate it = cpu->cpu_thread;
10587c478bd9Sstevel@tonic-gate pil = it->t_pil;
10597c478bd9Sstevel@tonic-gate
10607c478bd9Sstevel@tonic-gate cpu->cpu_stats.sys.intr[pil - 1]++;
10617c478bd9Sstevel@tonic-gate
10627c478bd9Sstevel@tonic-gate ASSERT(cpu->cpu_intr_actv & (1 << pil));
10637c478bd9Sstevel@tonic-gate cpu->cpu_intr_actv &= ~(1 << pil);
1064ae115bc7Smrj intrtime = now - it->t_intr_start;
10657a364d25Sschwartz mcpu->intrstat[pil][0] += intrtime;
1066eda89462Sesolom cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
10677c478bd9Sstevel@tonic-gate
10687c478bd9Sstevel@tonic-gate /*
10697c478bd9Sstevel@tonic-gate * If there is still an interrupted thread underneath this one
10707c478bd9Sstevel@tonic-gate * then the interrupt was never blocked and the return is
10717c478bd9Sstevel@tonic-gate * fairly simple. Otherwise it isn't.
10727c478bd9Sstevel@tonic-gate */
10737c478bd9Sstevel@tonic-gate if ((t = it->t_intr) == NULL) {
10747c478bd9Sstevel@tonic-gate /*
10757c478bd9Sstevel@tonic-gate * Put thread back on the interrupt thread list.
10767c478bd9Sstevel@tonic-gate * This was an interrupt thread, so set CPU's base SPL.
10777c478bd9Sstevel@tonic-gate */
10787c478bd9Sstevel@tonic-gate set_base_spl();
10797c478bd9Sstevel@tonic-gate it->t_state = TS_FREE;
10807c478bd9Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread;
10817c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it;
10827c478bd9Sstevel@tonic-gate (void) splhigh();
1083ae115bc7Smrj sti();
10847c478bd9Sstevel@tonic-gate swtch();
10857c478bd9Sstevel@tonic-gate /*NOTREACHED*/
1086ae115bc7Smrj panic("dosoftint_epilog: swtch returned");
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate it->t_link = cpu->cpu_intr_thread;
10897c478bd9Sstevel@tonic-gate cpu->cpu_intr_thread = it;
10907c478bd9Sstevel@tonic-gate it->t_state = TS_FREE;
10917c478bd9Sstevel@tonic-gate cpu->cpu_thread = t;
10927c478bd9Sstevel@tonic-gate if (t->t_flag & T_INTR_THREAD)
1093ae115bc7Smrj t->t_intr_start = now;
10947c478bd9Sstevel@tonic-gate basespl = cpu->cpu_base_spl;
10957c478bd9Sstevel@tonic-gate pil = MAX(oldpil, basespl);
10967c478bd9Sstevel@tonic-gate mcpu->mcpu_pri = pil;
10977c478bd9Sstevel@tonic-gate (*setspl)(pil);
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate
1100ae115bc7Smrj
11017c478bd9Sstevel@tonic-gate /*
11027c478bd9Sstevel@tonic-gate * Make the interrupted thread 'to' be runnable.
11037c478bd9Sstevel@tonic-gate *
11047c478bd9Sstevel@tonic-gate * Since t->t_sp has already been saved, t->t_pc is all
11057c478bd9Sstevel@tonic-gate * that needs to be set in this function.
11067c478bd9Sstevel@tonic-gate *
11077c478bd9Sstevel@tonic-gate * Returns the interrupt level of the interrupt thread.
11087c478bd9Sstevel@tonic-gate */
11097c478bd9Sstevel@tonic-gate int
intr_passivate(kthread_t * it,kthread_t * t)11107c478bd9Sstevel@tonic-gate intr_passivate(
11117c478bd9Sstevel@tonic-gate kthread_t *it, /* interrupt thread */
11127c478bd9Sstevel@tonic-gate kthread_t *t) /* interrupted thread */
11137c478bd9Sstevel@tonic-gate {
11147c478bd9Sstevel@tonic-gate extern void _sys_rtt();
11157c478bd9Sstevel@tonic-gate
11167c478bd9Sstevel@tonic-gate ASSERT(it->t_flag & T_INTR_THREAD);
11177c478bd9Sstevel@tonic-gate ASSERT(SA(t->t_sp) == t->t_sp);
11187c478bd9Sstevel@tonic-gate
11197c478bd9Sstevel@tonic-gate t->t_pc = (uintptr_t)_sys_rtt;
11207c478bd9Sstevel@tonic-gate return (it->t_pil);
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate
11237c478bd9Sstevel@tonic-gate /*
11247c478bd9Sstevel@tonic-gate * Create interrupt kstats for this CPU.
11257c478bd9Sstevel@tonic-gate */
11267c478bd9Sstevel@tonic-gate void
cpu_create_intrstat(cpu_t * cp)11277c478bd9Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp)
11287c478bd9Sstevel@tonic-gate {
11297c478bd9Sstevel@tonic-gate int i;
11307c478bd9Sstevel@tonic-gate kstat_t *intr_ksp;
11317c478bd9Sstevel@tonic-gate kstat_named_t *knp;
11327c478bd9Sstevel@tonic-gate char name[KSTAT_STRLEN];
11337c478bd9Sstevel@tonic-gate zoneid_t zoneid;
11347c478bd9Sstevel@tonic-gate
11357c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
11367c478bd9Sstevel@tonic-gate
11377c478bd9Sstevel@tonic-gate if (pool_pset_enabled())
11387c478bd9Sstevel@tonic-gate zoneid = GLOBAL_ZONEID;
11397c478bd9Sstevel@tonic-gate else
11407c478bd9Sstevel@tonic-gate zoneid = ALL_ZONES;
11417c478bd9Sstevel@tonic-gate
11427c478bd9Sstevel@tonic-gate intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
11437c478bd9Sstevel@tonic-gate KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
11447c478bd9Sstevel@tonic-gate
11457c478bd9Sstevel@tonic-gate /*
11467c478bd9Sstevel@tonic-gate * Initialize each PIL's named kstat
11477c478bd9Sstevel@tonic-gate */
11487c478bd9Sstevel@tonic-gate if (intr_ksp != NULL) {
11497c478bd9Sstevel@tonic-gate intr_ksp->ks_update = cpu_kstat_intrstat_update;
11507c478bd9Sstevel@tonic-gate knp = (kstat_named_t *)intr_ksp->ks_data;
11517c478bd9Sstevel@tonic-gate intr_ksp->ks_private = cp;
11527c478bd9Sstevel@tonic-gate for (i = 0; i < PIL_MAX; i++) {
11537c478bd9Sstevel@tonic-gate (void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
11547c478bd9Sstevel@tonic-gate i + 1);
11557c478bd9Sstevel@tonic-gate kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
11567c478bd9Sstevel@tonic-gate (void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
11577c478bd9Sstevel@tonic-gate i + 1);
11587c478bd9Sstevel@tonic-gate kstat_named_init(&knp[(i * 2) + 1], name,
11597c478bd9Sstevel@tonic-gate KSTAT_DATA_UINT64);
11607c478bd9Sstevel@tonic-gate }
11617c478bd9Sstevel@tonic-gate kstat_install(intr_ksp);
11627c478bd9Sstevel@tonic-gate }
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate
11657c478bd9Sstevel@tonic-gate /*
11667c478bd9Sstevel@tonic-gate * Delete interrupt kstats for this CPU.
11677c478bd9Sstevel@tonic-gate */
11687c478bd9Sstevel@tonic-gate void
cpu_delete_intrstat(cpu_t * cp)11697c478bd9Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp)
11707c478bd9Sstevel@tonic-gate {
11717c478bd9Sstevel@tonic-gate kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate
11747c478bd9Sstevel@tonic-gate /*
11757c478bd9Sstevel@tonic-gate * Convert interrupt statistics from CPU ticks to nanoseconds and
11767c478bd9Sstevel@tonic-gate * update kstat.
11777c478bd9Sstevel@tonic-gate */
11787c478bd9Sstevel@tonic-gate int
cpu_kstat_intrstat_update(kstat_t * ksp,int rw)11797c478bd9Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
11807c478bd9Sstevel@tonic-gate {
11817c478bd9Sstevel@tonic-gate kstat_named_t *knp = ksp->ks_data;
11827c478bd9Sstevel@tonic-gate cpu_t *cpup = (cpu_t *)ksp->ks_private;
11837c478bd9Sstevel@tonic-gate int i;
11847c478bd9Sstevel@tonic-gate hrtime_t hrt;
11857c478bd9Sstevel@tonic-gate
11867c478bd9Sstevel@tonic-gate if (rw == KSTAT_WRITE)
11877c478bd9Sstevel@tonic-gate return (EACCES);
11887c478bd9Sstevel@tonic-gate
11897c478bd9Sstevel@tonic-gate for (i = 0; i < PIL_MAX; i++) {
11907a364d25Sschwartz hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0];
1191843e1988Sjohnlev scalehrtimef(&hrt);
11927c478bd9Sstevel@tonic-gate knp[i * 2].value.ui64 = (uint64_t)hrt;
11937c478bd9Sstevel@tonic-gate knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate
11967c478bd9Sstevel@tonic-gate return (0);
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate
11997c478bd9Sstevel@tonic-gate /*
12007c478bd9Sstevel@tonic-gate * An interrupt thread is ending a time slice, so compute the interval it
12017c478bd9Sstevel@tonic-gate * ran for and update the statistic for its PIL.
12027c478bd9Sstevel@tonic-gate */
12037c478bd9Sstevel@tonic-gate void
cpu_intr_swtch_enter(kthread_id_t t)12047c478bd9Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t)
12057c478bd9Sstevel@tonic-gate {
12067c478bd9Sstevel@tonic-gate uint64_t interval;
12077c478bd9Sstevel@tonic-gate uint64_t start;
1208eda89462Sesolom cpu_t *cpu;
12097c478bd9Sstevel@tonic-gate
12107c478bd9Sstevel@tonic-gate ASSERT((t->t_flag & T_INTR_THREAD) != 0);
12117c478bd9Sstevel@tonic-gate ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
12127c478bd9Sstevel@tonic-gate
12137c478bd9Sstevel@tonic-gate /*
12147c478bd9Sstevel@tonic-gate * We could be here with a zero timestamp. This could happen if:
12157c478bd9Sstevel@tonic-gate * an interrupt thread which no longer has a pinned thread underneath
12167c478bd9Sstevel@tonic-gate * it (i.e. it blocked at some point in its past) has finished running
12177c478bd9Sstevel@tonic-gate * its handler. intr_thread() updated the interrupt statistic for its
12187c478bd9Sstevel@tonic-gate * PIL and zeroed its timestamp. Since there was no pinned thread to
12197c478bd9Sstevel@tonic-gate * return to, swtch() gets called and we end up here.
1220eda89462Sesolom *
1221*8b6220d7SJosef 'Jeff' Sipek * Note that we use atomic ops below (atomic_cas_64 and
1222*8b6220d7SJosef 'Jeff' Sipek * atomic_add_64), which we don't use in the functions above,
1223*8b6220d7SJosef 'Jeff' Sipek * because we're not called with interrupts blocked, but the
1224*8b6220d7SJosef 'Jeff' Sipek * epilog/prolog functions are.
12257c478bd9Sstevel@tonic-gate */
12267c478bd9Sstevel@tonic-gate if (t->t_intr_start) {
12277c478bd9Sstevel@tonic-gate do {
12287c478bd9Sstevel@tonic-gate start = t->t_intr_start;
12297c478bd9Sstevel@tonic-gate interval = tsc_read() - start;
1230*8b6220d7SJosef 'Jeff' Sipek } while (atomic_cas_64(&t->t_intr_start, start, 0) != start);
1231eda89462Sesolom cpu = CPU;
12327a364d25Sschwartz cpu->cpu_m.intrstat[t->t_pil][0] += interval;
1233eda89462Sesolom
1234eda89462Sesolom atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
1235eda89462Sesolom interval);
12367c478bd9Sstevel@tonic-gate } else
12377c478bd9Sstevel@tonic-gate ASSERT(t->t_intr == NULL);
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate
12407c478bd9Sstevel@tonic-gate /*
12417c478bd9Sstevel@tonic-gate * An interrupt thread is returning from swtch(). Place a starting timestamp
12427c478bd9Sstevel@tonic-gate * in its thread structure.
12437c478bd9Sstevel@tonic-gate */
12447c478bd9Sstevel@tonic-gate void
cpu_intr_swtch_exit(kthread_id_t t)12457c478bd9Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t)
12467c478bd9Sstevel@tonic-gate {
12477c478bd9Sstevel@tonic-gate uint64_t ts;
12487c478bd9Sstevel@tonic-gate
12497c478bd9Sstevel@tonic-gate ASSERT((t->t_flag & T_INTR_THREAD) != 0);
12507c478bd9Sstevel@tonic-gate ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
12517c478bd9Sstevel@tonic-gate
12527c478bd9Sstevel@tonic-gate do {
12537c478bd9Sstevel@tonic-gate ts = t->t_intr_start;
1254*8b6220d7SJosef 'Jeff' Sipek } while (atomic_cas_64(&t->t_intr_start, ts, tsc_read()) != ts);
12557c478bd9Sstevel@tonic-gate }
1256ae115bc7Smrj
1257ae115bc7Smrj /*
1258ae115bc7Smrj * Dispatch a hilevel interrupt (one above LOCK_LEVEL)
1259ae115bc7Smrj */
1260ae115bc7Smrj /*ARGSUSED*/
1261ae115bc7Smrj static void
dispatch_hilevel(uint_t vector,uint_t arg2)1262ae115bc7Smrj dispatch_hilevel(uint_t vector, uint_t arg2)
1263ae115bc7Smrj {
1264ae115bc7Smrj sti();
1265ae115bc7Smrj av_dispatch_autovect(vector);
1266ae115bc7Smrj cli();
1267ae115bc7Smrj }
1268ae115bc7Smrj
1269ae115bc7Smrj /*
1270ae115bc7Smrj * Dispatch a soft interrupt
1271ae115bc7Smrj */
1272ae115bc7Smrj /*ARGSUSED*/
1273ae115bc7Smrj static void
dispatch_softint(uint_t oldpil,uint_t arg2)1274ae115bc7Smrj dispatch_softint(uint_t oldpil, uint_t arg2)
1275ae115bc7Smrj {
1276ae115bc7Smrj struct cpu *cpu = CPU;
1277ae115bc7Smrj
1278ae115bc7Smrj sti();
1279ae115bc7Smrj av_dispatch_softvect((int)cpu->cpu_thread->t_pil);
1280ae115bc7Smrj cli();
1281ae115bc7Smrj
1282ae115bc7Smrj /*
1283ae115bc7Smrj * Must run softint_epilog() on the interrupt thread stack, since
1284ae115bc7Smrj * there may not be a return from it if the interrupt thread blocked.
1285ae115bc7Smrj */
1286ae115bc7Smrj dosoftint_epilog(cpu, oldpil);
1287ae115bc7Smrj }
1288ae115bc7Smrj
1289ae115bc7Smrj /*
1290ae115bc7Smrj * Dispatch a normal interrupt
1291ae115bc7Smrj */
1292ae115bc7Smrj static void
dispatch_hardint(uint_t vector,uint_t oldipl)1293ae115bc7Smrj dispatch_hardint(uint_t vector, uint_t oldipl)
1294ae115bc7Smrj {
1295ae115bc7Smrj struct cpu *cpu = CPU;
1296ae115bc7Smrj
1297ae115bc7Smrj sti();
1298ae115bc7Smrj av_dispatch_autovect(vector);
1299ae115bc7Smrj cli();
1300ae115bc7Smrj
1301ae115bc7Smrj /*
1302ae115bc7Smrj * Must run intr_thread_epilog() on the interrupt thread stack, since
1303ae115bc7Smrj * there may not be a return from it if the interrupt thread blocked.
1304ae115bc7Smrj */
1305ae115bc7Smrj intr_thread_epilog(cpu, vector, oldipl);
1306ae115bc7Smrj }
1307ae115bc7Smrj
1308ae115bc7Smrj /*
1309ae115bc7Smrj * Deliver any softints the current interrupt priority allows.
1310ae115bc7Smrj * Called with interrupts disabled.
1311ae115bc7Smrj */
1312ae115bc7Smrj void
dosoftint(struct regs * regs)1313ae115bc7Smrj dosoftint(struct regs *regs)
1314ae115bc7Smrj {
1315ae115bc7Smrj struct cpu *cpu = CPU;
1316ae115bc7Smrj int oldipl;
1317ae115bc7Smrj caddr_t newsp;
1318ae115bc7Smrj
1319ae115bc7Smrj while (cpu->cpu_softinfo.st_pending) {
1320ae115bc7Smrj oldipl = cpu->cpu_pri;
1321ae115bc7Smrj newsp = dosoftint_prolog(cpu, (caddr_t)regs,
1322ae115bc7Smrj cpu->cpu_softinfo.st_pending, oldipl);
1323ae115bc7Smrj /*
1324ae115bc7Smrj * If returned stack pointer is NULL, priority is too high
1325ae115bc7Smrj * to run any of the pending softints now.
1326ae115bc7Smrj * Break out and they will be run later.
1327ae115bc7Smrj */
1328ae115bc7Smrj if (newsp == NULL)
1329ae115bc7Smrj break;
1330ae115bc7Smrj switch_sp_and_call(newsp, dispatch_softint, oldipl, 0);
1331ae115bc7Smrj }
1332ae115bc7Smrj }
1333ae115bc7Smrj
1334ae115bc7Smrj /*
1335ae115bc7Smrj * Interrupt service routine, called with interrupts disabled.
1336ae115bc7Smrj */
1337ae115bc7Smrj /*ARGSUSED*/
1338ae115bc7Smrj void
do_interrupt(struct regs * rp,trap_trace_rec_t * ttp)1339ae115bc7Smrj do_interrupt(struct regs *rp, trap_trace_rec_t *ttp)
1340ae115bc7Smrj {
1341ae115bc7Smrj struct cpu *cpu = CPU;
1342ae115bc7Smrj int newipl, oldipl = cpu->cpu_pri;
1343ae115bc7Smrj uint_t vector;
1344ae115bc7Smrj caddr_t newsp;
1345ae115bc7Smrj
1346ae115bc7Smrj #ifdef TRAPTRACE
1347ae115bc7Smrj ttp->ttr_marker = TT_INTERRUPT;
1348ae115bc7Smrj ttp->ttr_ipl = 0xff;
1349ae115bc7Smrj ttp->ttr_pri = oldipl;
1350ae115bc7Smrj ttp->ttr_spl = cpu->cpu_base_spl;
1351ae115bc7Smrj ttp->ttr_vector = 0xff;
1352ae115bc7Smrj #endif /* TRAPTRACE */
1353ae115bc7Smrj
1354fb2caebeSRandy Fishel cpu_idle_exit(CPU_IDLE_CB_FLAG_INTR);
135595c0a3c8Sjosephb
13563006ae82SFrank Van Der Linden ++*(uint16_t *)&cpu->cpu_m.mcpu_istamp;
13573006ae82SFrank Van Der Linden
135895c0a3c8Sjosephb /*
1359ae115bc7Smrj * If it's a softint go do it now.
1360ae115bc7Smrj */
1361ae115bc7Smrj if (rp->r_trapno == T_SOFTINT) {
1362ae115bc7Smrj dosoftint(rp);
1363ae115bc7Smrj ASSERT(!interrupts_enabled());
1364ae115bc7Smrj return;
1365ae115bc7Smrj }
1366ae115bc7Smrj
1367ae115bc7Smrj /*
1368ae115bc7Smrj * Raise the interrupt priority.
1369ae115bc7Smrj */
1370ae115bc7Smrj newipl = (*setlvl)(oldipl, (int *)&rp->r_trapno);
1371ae115bc7Smrj #ifdef TRAPTRACE
1372ae115bc7Smrj ttp->ttr_ipl = newipl;
1373ae115bc7Smrj #endif /* TRAPTRACE */
1374ae115bc7Smrj
1375ae115bc7Smrj /*
1376ae115bc7Smrj * Bail if it is a spurious interrupt
1377ae115bc7Smrj */
1378ae115bc7Smrj if (newipl == -1)
1379ae115bc7Smrj return;
1380ae115bc7Smrj cpu->cpu_pri = newipl;
1381ae115bc7Smrj vector = rp->r_trapno;
1382ae115bc7Smrj #ifdef TRAPTRACE
1383ae115bc7Smrj ttp->ttr_vector = vector;
1384ae115bc7Smrj #endif /* TRAPTRACE */
1385ae115bc7Smrj if (newipl > LOCK_LEVEL) {
1386ae115bc7Smrj /*
1387ae115bc7Smrj * High priority interrupts run on this cpu's interrupt stack.
1388ae115bc7Smrj */
1389ae115bc7Smrj if (hilevel_intr_prolog(cpu, newipl, oldipl, rp) == 0) {
1390ae115bc7Smrj newsp = cpu->cpu_intr_stack;
1391ae115bc7Smrj switch_sp_and_call(newsp, dispatch_hilevel, vector, 0);
1392ae115bc7Smrj } else { /* already on the interrupt stack */
1393ae115bc7Smrj dispatch_hilevel(vector, 0);
1394ae115bc7Smrj }
1395ae115bc7Smrj (void) hilevel_intr_epilog(cpu, newipl, oldipl, vector);
1396ae115bc7Smrj } else {
1397ae115bc7Smrj /*
1398ae115bc7Smrj * Run this interrupt in a separate thread.
1399ae115bc7Smrj */
1400ae115bc7Smrj newsp = intr_thread_prolog(cpu, (caddr_t)rp, newipl);
1401ae115bc7Smrj switch_sp_and_call(newsp, dispatch_hardint, vector, oldipl);
1402ae115bc7Smrj }
1403ae115bc7Smrj
1404349b53ddSStuart Maybee #if !defined(__xpv)
1405ae115bc7Smrj /*
1406ae115bc7Smrj * Deliver any pending soft interrupts.
1407ae115bc7Smrj */
1408ae115bc7Smrj if (cpu->cpu_softinfo.st_pending)
1409ae115bc7Smrj dosoftint(rp);
1410349b53ddSStuart Maybee #endif /* !__xpv */
1411ae115bc7Smrj }
1412ae115bc7Smrj
1413349b53ddSStuart Maybee
1414ae115bc7Smrj /*
1415ae115bc7Smrj * Common tasks always done by _sys_rtt, called with interrupts disabled.
1416ae115bc7Smrj * Returns 1 if returning to userland, 0 if returning to system mode.
1417ae115bc7Smrj */
1418ae115bc7Smrj int
sys_rtt_common(struct regs * rp)1419ae115bc7Smrj sys_rtt_common(struct regs *rp)
1420ae115bc7Smrj {
1421ae115bc7Smrj kthread_t *tp;
1422ae115bc7Smrj extern void mutex_exit_critical_start();
1423ae115bc7Smrj extern long mutex_exit_critical_size;
1424575a7426Spt157919 extern void mutex_owner_running_critical_start();
1425575a7426Spt157919 extern long mutex_owner_running_critical_size;
1426ae115bc7Smrj
1427ae115bc7Smrj loop:
1428ae115bc7Smrj
1429ae115bc7Smrj /*
1430ae115bc7Smrj * Check if returning to user
1431ae115bc7Smrj */
1432ae115bc7Smrj tp = CPU->cpu_thread;
1433ae115bc7Smrj if (USERMODE(rp->r_cs)) {
1434ae115bc7Smrj /*
1435ae115bc7Smrj * Check if AST pending.
1436ae115bc7Smrj */
1437ae115bc7Smrj if (tp->t_astflag) {
1438ae115bc7Smrj /*
1439ae115bc7Smrj * Let trap() handle the AST
1440ae115bc7Smrj */
1441ae115bc7Smrj sti();
1442ae115bc7Smrj rp->r_trapno = T_AST;
1443ae115bc7Smrj trap(rp, (caddr_t)0, CPU->cpu_id);
1444ae115bc7Smrj cli();
1445ae115bc7Smrj goto loop;
1446ae115bc7Smrj }
1447ae115bc7Smrj
1448ae115bc7Smrj #if defined(__amd64)
1449ae115bc7Smrj /*
1450ae115bc7Smrj * We are done if segment registers do not need updating.
1451ae115bc7Smrj */
14527712e92cSsudheer if (tp->t_lwp->lwp_pcb.pcb_rupdate == 0)
1453ae115bc7Smrj return (1);
1454ae115bc7Smrj
1455ae115bc7Smrj if (update_sregs(rp, tp->t_lwp)) {
1456ae115bc7Smrj /*
1457ae115bc7Smrj * 1 or more of the selectors is bad.
1458ae115bc7Smrj * Deliver a SIGSEGV.
1459ae115bc7Smrj */
1460ae115bc7Smrj proc_t *p = ttoproc(tp);
1461ae115bc7Smrj
1462ae115bc7Smrj sti();
1463ae115bc7Smrj mutex_enter(&p->p_lock);
1464ae115bc7Smrj tp->t_lwp->lwp_cursig = SIGSEGV;
1465ae115bc7Smrj mutex_exit(&p->p_lock);
1466ae115bc7Smrj psig();
1467ae115bc7Smrj tp->t_sig_check = 1;
1468ae115bc7Smrj cli();
1469ae115bc7Smrj }
14707712e92cSsudheer tp->t_lwp->lwp_pcb.pcb_rupdate = 0;
1471ae115bc7Smrj
1472ae115bc7Smrj #endif /* __amd64 */
1473ae115bc7Smrj return (1);
1474ae115bc7Smrj }
1475ae115bc7Smrj
1476ae115bc7Smrj /*
1477ae115bc7Smrj * Here if we are returning to supervisor mode.
1478ae115bc7Smrj * Check for a kernel preemption request.
1479ae115bc7Smrj */
1480ae115bc7Smrj if (CPU->cpu_kprunrun && (rp->r_ps & PS_IE)) {
1481ae115bc7Smrj
1482ae115bc7Smrj /*
1483ae115bc7Smrj * Do nothing if already in kpreempt
1484ae115bc7Smrj */
1485ae115bc7Smrj if (!tp->t_preempt_lk) {
1486ae115bc7Smrj tp->t_preempt_lk = 1;
1487ae115bc7Smrj sti();
1488ae115bc7Smrj kpreempt(1); /* asynchronous kpreempt call */
1489ae115bc7Smrj cli();
1490ae115bc7Smrj tp->t_preempt_lk = 0;
1491ae115bc7Smrj }
1492ae115bc7Smrj }
1493ae115bc7Smrj
1494ae115bc7Smrj /*
1495ae115bc7Smrj * If we interrupted the mutex_exit() critical region we must
1496ae115bc7Smrj * reset the PC back to the beginning to prevent missed wakeups
1497ae115bc7Smrj * See the comments in mutex_exit() for details.
1498ae115bc7Smrj */
1499ae115bc7Smrj if ((uintptr_t)rp->r_pc - (uintptr_t)mutex_exit_critical_start <
1500ae115bc7Smrj mutex_exit_critical_size) {
1501ae115bc7Smrj rp->r_pc = (greg_t)mutex_exit_critical_start;
1502ae115bc7Smrj }
1503575a7426Spt157919
1504575a7426Spt157919 /*
1505575a7426Spt157919 * If we interrupted the mutex_owner_running() critical region we
1506575a7426Spt157919 * must reset the PC back to the beginning to prevent dereferencing
1507575a7426Spt157919 * of a freed thread pointer. See the comments in mutex_owner_running
1508575a7426Spt157919 * for details.
1509575a7426Spt157919 */
1510575a7426Spt157919 if ((uintptr_t)rp->r_pc -
1511575a7426Spt157919 (uintptr_t)mutex_owner_running_critical_start <
1512575a7426Spt157919 mutex_owner_running_critical_size) {
1513575a7426Spt157919 rp->r_pc = (greg_t)mutex_owner_running_critical_start;
1514575a7426Spt157919 }
1515575a7426Spt157919
1516ae115bc7Smrj return (0);
1517ae115bc7Smrj }
1518ae115bc7Smrj
1519ae115bc7Smrj void
send_dirint(int cpuid,int int_level)1520ae115bc7Smrj send_dirint(int cpuid, int int_level)
1521ae115bc7Smrj {
1522ae115bc7Smrj (*send_dirintf)(cpuid, int_level);
1523ae115bc7Smrj }
1524ae115bc7Smrj
15257ff178cdSJimmy Vetayases #define IS_FAKE_SOFTINT(flag, newpri) \
15267ff178cdSJimmy Vetayases (((flag) & PS_IE) && \
15277ff178cdSJimmy Vetayases (((*get_pending_spl)() > (newpri)) || \
15287ff178cdSJimmy Vetayases bsrw_insn((uint16_t)cpu->cpu_softinfo.st_pending) > (newpri)))
15297ff178cdSJimmy Vetayases
1530ae115bc7Smrj /*
1531ae115bc7Smrj * do_splx routine, takes new ipl to set
1532ae115bc7Smrj * returns the old ipl.
1533ae115bc7Smrj * We are careful not to set priority lower than CPU->cpu_base_pri,
1534ae115bc7Smrj * even though it seems we're raising the priority, it could be set
1535ae115bc7Smrj * higher at any time by an interrupt routine, so we must block interrupts
1536ae115bc7Smrj * and look at CPU->cpu_base_pri
1537ae115bc7Smrj */
1538ae115bc7Smrj int
do_splx(int newpri)1539ae115bc7Smrj do_splx(int newpri)
1540ae115bc7Smrj {
1541ae115bc7Smrj ulong_t flag;
1542ae115bc7Smrj cpu_t *cpu;
1543ae115bc7Smrj int curpri, basepri;
1544ae115bc7Smrj
1545ae115bc7Smrj flag = intr_clear();
1546ae115bc7Smrj cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
1547ae115bc7Smrj curpri = cpu->cpu_m.mcpu_pri;
1548ae115bc7Smrj basepri = cpu->cpu_base_spl;
1549ae115bc7Smrj if (newpri < basepri)
1550ae115bc7Smrj newpri = basepri;
1551ae115bc7Smrj cpu->cpu_m.mcpu_pri = newpri;
1552ae115bc7Smrj (*setspl)(newpri);
1553ae115bc7Smrj /*
1554ae115bc7Smrj * If we are going to reenable interrupts see if new priority level
1555ae115bc7Smrj * allows pending softint delivery.
1556ae115bc7Smrj */
15577ff178cdSJimmy Vetayases if (IS_FAKE_SOFTINT(flag, newpri))
1558ae115bc7Smrj fakesoftint();
1559ae115bc7Smrj ASSERT(!interrupts_enabled());
1560ae115bc7Smrj intr_restore(flag);
1561ae115bc7Smrj return (curpri);
1562ae115bc7Smrj }
1563ae115bc7Smrj
1564ae115bc7Smrj /*
1565ae115bc7Smrj * Common spl raise routine, takes new ipl to set
1566ae115bc7Smrj * returns the old ipl, will not lower ipl.
1567ae115bc7Smrj */
1568ae115bc7Smrj int
splr(int newpri)1569ae115bc7Smrj splr(int newpri)
1570ae115bc7Smrj {
1571ae115bc7Smrj ulong_t flag;
1572ae115bc7Smrj cpu_t *cpu;
1573ae115bc7Smrj int curpri, basepri;
1574ae115bc7Smrj
1575ae115bc7Smrj flag = intr_clear();
1576ae115bc7Smrj cpu = CPU; /* ints are disabled, now safe to cache cpu ptr */
1577ae115bc7Smrj curpri = cpu->cpu_m.mcpu_pri;
1578ae115bc7Smrj /*
1579ae115bc7Smrj * Only do something if new priority is larger
1580ae115bc7Smrj */
1581ae115bc7Smrj if (newpri > curpri) {
1582ae115bc7Smrj basepri = cpu->cpu_base_spl;
1583ae115bc7Smrj if (newpri < basepri)
1584ae115bc7Smrj newpri = basepri;
1585ae115bc7Smrj cpu->cpu_m.mcpu_pri = newpri;
1586ae115bc7Smrj (*setspl)(newpri);
1587ae115bc7Smrj /*
1588ae115bc7Smrj * See if new priority level allows pending softint delivery
1589ae115bc7Smrj */
15907ff178cdSJimmy Vetayases if (IS_FAKE_SOFTINT(flag, newpri))
1591ae115bc7Smrj fakesoftint();
1592ae115bc7Smrj }
1593ae115bc7Smrj intr_restore(flag);
1594ae115bc7Smrj return (curpri);
1595ae115bc7Smrj }
1596ae115bc7Smrj
1597ae115bc7Smrj int
getpil(void)1598ae115bc7Smrj getpil(void)
1599ae115bc7Smrj {
1600ae115bc7Smrj return (CPU->cpu_m.mcpu_pri);
1601ae115bc7Smrj }
1602ae115bc7Smrj
1603ae115bc7Smrj int
spl_xcall(void)1604b885580bSAlexander Kolbasov spl_xcall(void)
1605b885580bSAlexander Kolbasov {
1606b885580bSAlexander Kolbasov return (splr(ipltospl(XCALL_PIL)));
1607b885580bSAlexander Kolbasov }
1608b885580bSAlexander Kolbasov
1609b885580bSAlexander Kolbasov int
interrupts_enabled(void)1610ae115bc7Smrj interrupts_enabled(void)
1611ae115bc7Smrj {
1612ae115bc7Smrj ulong_t flag;
1613ae115bc7Smrj
1614ae115bc7Smrj flag = getflags();
1615ae115bc7Smrj return ((flag & PS_IE) == PS_IE);
1616ae115bc7Smrj }
1617ae115bc7Smrj
1618ae115bc7Smrj #ifdef DEBUG
1619ae115bc7Smrj void
assert_ints_enabled(void)1620ae115bc7Smrj assert_ints_enabled(void)
1621ae115bc7Smrj {
1622ae115bc7Smrj ASSERT(!interrupts_unleashed || interrupts_enabled());
1623ae115bc7Smrj }
1624ae115bc7Smrj #endif /* DEBUG */
1625