2 * from: vector.s, 386BSD 0.1 unknown origin
3 * $FreeBSD: src/sys/i386/isa/apic_vector.s,v 1.47.2.5 2001/09/01 22:33:38 tegge Exp $
7 #include "opt_auto_eoi.h"
10 #include <machine/asmacros.h>
11 #include <machine/lock.h>
12 #include <machine/psl.h>
13 #include <machine/trap.h>
14 #include <machine/segments.h>
16 #include <machine_base/icu/icu.h>
17 #include <bus/isa/isa.h>
22 #include <machine_base/apic/ioapic_ipl.h>
23 #include <machine/intr_machdep.h>
26 /* convert an absolute IRQ# into bitmask */
27 #define IRQ_LBIT(irq_num) (1UL << (irq_num & 0x3f))
30 #define IRQ_SBITS(irq_num) ((irq_num) & 0x3f)
32 /* convert an absolute IRQ# into gd_ipending index */
33 #define IRQ_LIDX(irq_num) ((irq_num) >> 6)
35 #define MPLOCKED lock ;
37 #define APIC_PUSH_FRAME_TFRIP \
38 PUSH_FRAME_TFRIP ;
/* 15 regs + space for 5 extras */ \
39 movq $
0,TF_XFLAGS
(%rsp
) ; \
40 movq $
0,TF_TRAPNO
(%rsp
) ; \
41 movq $
0,TF_ADDR
(%rsp
) ; \
42 movq $
0,TF_FLAGS
(%rsp
) ; \
43 movq $
0,TF_ERR
(%rsp
) ; \
47 * JG stale? Warning: POP_FRAME can only be used if there is no chance of a
48 * segment register being changed (e.g. by procfs), which is why syscalls
51 #define APIC_POP_FRAME(lastinsn) \
54 #define IOAPICADDR(irq_num) \
55 CNAME
(ioapic_irqs
) + IOAPIC_IRQI_SIZE
* (irq_num
) + IOAPIC_IRQI_ADDR
56 #define REDIRIDX(irq_num) \
57 CNAME
(ioapic_irqs
) + IOAPIC_IRQI_SIZE
* (irq_num
) + IOAPIC_IRQI_IDX
58 #define IOAPICFLAGS(irq_num) \
59 CNAME
(ioapic_irqs
) + IOAPIC_IRQI_SIZE
* (irq_num
) + IOAPIC_IRQI_FLAGS
61 #define MASK_IRQ(irq_num) \
62 IOAPIC_IMASK_LOCK ;
/* into critical reg */ \
63 testl $IOAPIC_IRQI_FLAG_MASKED
, IOAPICFLAGS
(irq_num
) ; \
64 jne
7f ;
/* masked, don't mask */ \
65 orl $IOAPIC_IRQI_FLAG_MASKED
, IOAPICFLAGS
(irq_num
) ; \
66 /* set the mask bit */ \
67 movq IOAPICADDR
(irq_num
), %rcx ;
/* ioapic addr */ \
68 movl REDIRIDX
(irq_num
), %eax ;
/* get the index */ \
69 movl
%eax
, (%rcx
) ;
/* write the index */ \
70 orl $IOART_INTMASK
,IOAPIC_WINDOW
(%rcx
) ;
/* set the mask */ \
71 7: ;
/* already masked */ \
72 IOAPIC_IMASK_UNLOCK ; \
75 * Test to see whether we are handling an edge or level triggered INT.
76 * Level-triggered INTs must still be masked as we don't clear the source,
77 * and the EOI cycle would cause redundant INTs to occur.
79 #define MASK_LEVEL_IRQ(irq_num) \
80 testl $IOAPIC_IRQI_FLAG_LEVEL
, IOAPICFLAGS
(irq_num
) ; \
81 jz
9f ;
/* edge, don't mask */ \
86 * Test to see if the source is currntly masked, clear if so.
88 #define UNMASK_IRQ(irq_num) \
91 IOAPIC_IMASK_LOCK ;
/* into critical reg */ \
92 testl $IOAPIC_IRQI_FLAG_MASKED
, IOAPICFLAGS
(irq_num
) ; \
93 je
7f ;
/* bit clear, not masked */ \
94 andl $~IOAPIC_IRQI_FLAG_MASKED
, IOAPICFLAGS
(irq_num
) ; \
95 /* clear mask bit */ \
96 movq IOAPICADDR
(irq_num
),%rcx ;
/* ioapic addr */ \
97 movl REDIRIDX
(irq_num
), %eax ;
/* get the index */ \
98 movl
%eax
,(%rcx
) ;
/* write the index */ \
99 andl $~IOART_INTMASK
,IOAPIC_WINDOW
(%rcx
) ;
/* clear the mask */ \
101 IOAPIC_IMASK_UNLOCK ; \
105 * Interrupt call handlers run in the following sequence:
107 * - Push the trap frame required by doreti
108 * - Mask the interrupt and reenable its source
109 * - If we cannot take the interrupt set its ipending bit and
111 * - If we can take the interrupt clear its ipending bit,
112 * call the handler, then unmask and doreti.
114 * YYY can cache gd base opitner instead of using hidden %fs prefixes.
117 #define INTR_HANDLER(irq_num) \
120 IDTVEC
(ioapic_intr
##irq_num) ; \
121 APIC_PUSH_FRAME_TFRIP ; \
122 FAKE_MCOUNT
(TF_RIP
(%rsp
)) ; \
123 MASK_LEVEL_IRQ
(irq_num
) ; \
124 movq lapic_eoi
, %rax ; \
126 movq PCPU
(curthread
),%rbx ; \
127 testl $
-1,TD_NEST_COUNT
(%rbx
) ; \
129 testl $
-1,TD_CRITCOUNT
(%rbx
) ; \
132 /* in critical section, make interrupt pending */ \
133 /* set the pending bit and return, leave interrupt masked */ \
135 shlq $IRQ_SBITS
(irq_num
),%rcx ; \
136 movq $IRQ_LIDX
(irq_num
),%rdx ; \
137 orq
%rcx
,PCPU_E8
(ipending
,%rdx
) ; \
138 orl $RQF_INTPEND
,PCPU
(reqflags
) ; \
141 /* clear pending bit, run handler */ \
143 shlq $IRQ_SBITS
(irq_num
),%rcx ; \
145 movq $IRQ_LIDX
(irq_num
),%rdx ; \
146 andq
%rcx
,PCPU_E8
(ipending
,%rdx
) ; \
147 pushq $irq_num ;
/* trapframe -> intrframe */ \
148 movq
%rsp
, %rdi ;
/* pass frame by reference */ \
149 incl TD_CRITCOUNT
(%rbx
) ; \
151 call ithread_fast_handler ;
/* returns 0 to unmask */ \
152 cli ;
/* interlock avoid stacking */ \
153 decl TD_CRITCOUNT
(%rbx
) ; \
154 addq $
8, %rsp ;
/* intrframe -> trapframe */ \
155 UNMASK_IRQ
(irq_num
) ; \
161 * Handle "spurious INTerrupts".
163 * NOTE: This is different than the "spurious INTerrupt" generated by an
164 * 8259 PIC for missing INTs. See the APIC documentation for details.
165 * This routine should NOT do an 'EOI' cycle.
167 * NOTE: Even though we don't do anything here we must still swapgs if
168 * coming from a user frame in case the iretq faults... just use
169 * the nominal APIC_PUSH_FRAME sequence to get it done.
175 APIC_PUSH_FRAME_TFRIP
176 /* No EOI cycle used here */
177 FAKE_MCOUNT
(TF_RIP
(%rsp
))
179 APIC_POP_FRAME
(jmp doreti_iret
)
182 * Handle TLB shootdowns.
184 * NOTE: interrupts are left disabled.
190 APIC_PUSH_FRAME_TFRIP
192 callq
*%rax
/* End Of Interrupt to APIC */
193 FAKE_MCOUNT
(TF_RIP
(%rsp
))
194 incl PCPU
(cnt
) + V_IPI
195 movq PCPU
(curthread
),%rbx
196 incl PCPU
(intr_nesting_level
)
197 incl TD_CRITCOUNT
(%rbx
)
198 subq $
8,%rsp
/* make same as interrupt frame */
199 movq
%rsp
,%rdi
/* pass frame by reference */
200 call smp_inval_intr
/* called w/interrupts disabled */
201 addq $
8,%rsp
/* turn into trapframe */
202 decl TD_CRITCOUNT
(%rbx
)
203 decl PCPU
(intr_nesting_level
)
206 jmp doreti
/* doreti b/c intrs enabled */
209 * Handle sniffs - sniff %rip and %rsp.
215 APIC_PUSH_FRAME_TFRIP
217 callq
*%rax
/* End Of Interrupt to APIC */
218 FAKE_MCOUNT
(TF_RIP
(%rsp
))
219 incl PCPU
(cnt
) + V_IPI
220 movq TF_RIP
(%rsp
),%rax
221 movq
%rax
,PCPU
(sample_pc
)
222 movq TF_RSP
(%rsp
),%rax
223 movq
%rax
,PCPU
(sample_sp
)
225 APIC_POP_FRAME
(jmp doreti_iret
)
228 * Executed by a CPU when it receives an Xcpustop IPI from another CPU,
230 * - We cannot call doreti
231 * - Signals its receipt.
232 * - Waits for permission to restart.
233 * - Processing pending IPIQ events while waiting.
234 * - Signals its restart.
241 APIC_PUSH_FRAME_TFRIP
243 callq
*%rax
/* End Of Interrupt to APIC */
245 movl PCPU
(cpuid
), %eax
246 imull $PCB_SIZE
, %eax
247 leaq CNAME
(stoppcbs
), %rdi
249 call CNAME
(savectx
) /* Save process context */
252 * Indicate that we have stopped and loop waiting for permission
253 * to start again. We must still process IPI events while in a
256 * Interrupts must remain enabled for non-IPI'd per-cpu interrupts
257 * (e.g. Xtimer, Xinvltlb).
259 #if CPUMASK_ELEMENTS != 4
260 #error "assembly incompatible with cpumask_t"
262 movq PCPU
(cpumask
)+0,%rax
/* stopped_cpus |= 1 << cpuid */
263 MPLOCKED orq
%rax
, stopped_cpus+
0
264 movq PCPU
(cpumask
)+8,%rax
265 MPLOCKED orq
%rax
, stopped_cpus+
8
266 movq PCPU
(cpumask
)+16,%rax
267 MPLOCKED orq
%rax
, stopped_cpus+
16
268 movq PCPU
(cpumask
)+24,%rax
269 MPLOCKED orq
%rax
, stopped_cpus+
24
271 movq PCPU
(curthread
),%rbx
272 incl PCPU
(intr_nesting_level
)
273 incl TD_CRITCOUNT
(%rbx
)
276 andl $~RQF_IPIQ
,PCPU
(reqflags
)
277 call lwkt_smp_stopped
281 movq started_cpus+
0,%rax
/* while (!(started_cpus & (1<<id))) */
282 andq PCPU
(cpumask
)+0,%rax
284 movq started_cpus+
8,%rax
285 andq PCPU
(cpumask
)+8,%rax
287 movq started_cpus+
16,%rax
288 andq PCPU
(cpumask
)+16,%rax
290 movq started_cpus+
24,%rax
291 andq PCPU
(cpumask
)+24,%rax
296 movq PCPU
(other_cpus
)+0,%rax
/* started_cpus &= ~(1 << cpuid) */
297 MPLOCKED andq
%rax
, started_cpus+
0
298 movq PCPU
(other_cpus
)+8,%rax
299 MPLOCKED andq
%rax
, started_cpus+
8
300 movq PCPU
(other_cpus
)+16,%rax
301 MPLOCKED andq
%rax
, started_cpus+
16
302 movq PCPU
(other_cpus
)+24,%rax
303 MPLOCKED andq
%rax
, started_cpus+
24
305 movq PCPU
(other_cpus
)+0,%rax
/* stopped_cpus &= ~(1 << cpuid) */
306 MPLOCKED andq
%rax
, stopped_cpus+
0
307 movq PCPU
(other_cpus
)+8,%rax
308 MPLOCKED andq
%rax
, stopped_cpus+
8
309 movq PCPU
(other_cpus
)+16,%rax
310 MPLOCKED andq
%rax
, stopped_cpus+
16
311 movq PCPU
(other_cpus
)+24,%rax
312 MPLOCKED andq
%rax
, stopped_cpus+
24
317 movq CNAME
(cpustop_restartfunc
), %rax
320 movq $
0, CNAME
(cpustop_restartfunc
) /* One-shot */
324 decl TD_CRITCOUNT
(%rbx
)
325 decl PCPU
(intr_nesting_level
)
331 * For now just have one ipiq IPI, but what we really want is
332 * to have one for each source cpu to the APICs don't get stalled
333 * backlogging the requests.
339 APIC_PUSH_FRAME_TFRIP
341 callq
*%rax
/* End Of Interrupt to APIC */
342 FAKE_MCOUNT
(TF_RIP
(%rsp
))
344 incl PCPU
(cnt
) + V_IPI
345 movq PCPU
(curthread
),%rbx
346 testl $
-1,TD_CRITCOUNT
(%rbx
)
348 subq $
8,%rsp
/* make same as interrupt frame */
349 movq
%rsp
,%rdi
/* pass frame by reference */
350 incl PCPU
(intr_nesting_level
)
351 incl TD_CRITCOUNT
(%rbx
)
354 xchgl
%eax
,PCPU
(npoll
) /* (atomic op) allow another Xipi */
355 call lwkt_process_ipiq_frame
356 cli /* interlock avoid stacking */
357 decl TD_CRITCOUNT
(%rbx
)
358 decl PCPU
(intr_nesting_level
)
359 addq $
8,%rsp
/* turn into trapframe */
363 orl $RQF_IPIQ
,PCPU
(reqflags
)
365 APIC_POP_FRAME
(jmp doreti_iret
)
371 APIC_PUSH_FRAME_TFRIP
373 callq
*%rax
/* End Of Interrupt to APIC */
374 FAKE_MCOUNT
(TF_RIP
(%rsp
))
376 subq $
8,%rsp
/* make same as interrupt frame */
377 movq
%rsp
,%rdi
/* pass frame by reference */
378 call pcpu_timer_always
379 addq $
8,%rsp
/* turn into trapframe */
381 incl PCPU
(cnt
) + V_TIMER
382 movq TF_RIP
(%rsp
),%rbx
/* sample addr before checking crit */
383 movq
%rbx
,PCPU
(sample_pc
)
384 movq PCPU
(curthread
),%rbx
385 testl $
-1,TD_CRITCOUNT
(%rbx
)
387 testl $
-1,TD_NEST_COUNT
(%rbx
)
389 subq $
8,%rsp
/* make same as interrupt frame */
390 movq
%rsp
,%rdi
/* pass frame by reference */
391 incl PCPU
(intr_nesting_level
)
392 incl TD_CRITCOUNT
(%rbx
)
394 call pcpu_timer_process_frame
395 cli /* interlock avoid stacking */
396 decl TD_CRITCOUNT
(%rbx
)
397 decl PCPU
(intr_nesting_level
)
398 addq $
8,%rsp
/* turn into trapframe */
402 orl $RQF_TIMER
,PCPU
(reqflags
)
404 APIC_POP_FRAME
(jmp doreti_iret
)
603 #if CPUMASK_ELEMENTS != 4
604 #error "assembly incompatible with cpumask_t"
606 /* variables used by stop_cpus()/restart_cpus()/Xcpustop */
607 .globl stopped_cpus, started_cpus
619 .globl CNAME(cpustop_restartfunc)
620 CNAME
(cpustop_restartfunc
):