2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1989, 1990 William F. Jolitz.
37 * Copyright (c) 1990 The Regents of the University of California.
38 * All rights reserved.
40 * This code is derived from software contributed to Berkeley by
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * $FreeBSD: src/sys/i386/isa/ipl.s,v 1.32.2.3 2002/05/16 16:03:56 bde Exp $
74 * $DragonFly: src/sys/platform/pc64/amd64/ipl.s,v 1.1 2008/08/29 17:07:10 dillon Exp $
77 #include <machine/asmacros.h>
78 #include <machine/segments.h>
79 #include <machine/ipl.h>
80 #include <machine/lock.h>
81 #include <machine/psl.h>
82 #include <machine/trap.h>
88 * Vector interrupt control section
90 * ipending - Pending interrupts (set when a masked interrupt occurs)
91 * spending - Pending software interrupts
96 .globl fastunpend_count
97 fastunpend_count: .long 0
105 * - fast interrupts are always called with a critical section
108 * - we release our critical section when scheduling interrupt
109 * or softinterrupt threads in order so they can preempt
110 * (unless we are called manually from a critical section, in
111 * which case there will still be a critical section and
112 * they won't preempt anyway).
114 * - TD_NEST_COUNT prevents splz from nesting too deeply within
115 * itself. It is *not* actually an interrupt nesting count.
116 * PCPU(intr_nesting_level) is an interrupt nesting count.
118 * - We have to be careful in regards to local interrupts
119 * occuring simultaniously with our doreti and splz
126 * Handle return from interrupts, traps and syscalls. This function
127 * checks the cpl for unmasked pending interrupts (fast, normal, or
128 * soft) and schedules them if appropriate, then irets.
130 * If we are in a critical section we cannot run any pending ints
131 * nor can be play with mp_lock.
133 * The stack contains a trapframe at the start of doreti.
137 .type doreti,@function
139 FAKE_MCOUNT(bintr) /* init "from" bintr -> doreti */
140 movq $0,%rax /* irq mask unavailable due to BGL */
141 movq PCPU(curthread),%rbx
142 cli /* interlock with TDPRI_CRIT */
143 cmpl $0,PCPU(reqflags) /* short cut if nothing to do */
145 cmpl $TDPRI_CRIT,TD_PRI(%rbx) /* can't unpend if in critical sec */
147 addl $TDPRI_CRIT,TD_PRI(%rbx) /* force all ints to pending */
149 sti /* allow new interrupts */
150 movl %eax,%ecx /* irq mask unavailable due to BGL */
152 cli /* disallow YYY remove */
154 testl $RQF_IPIQ,PCPU(reqflags)
156 testl $RQF_TIMER,PCPU(reqflags)
159 testl PCPU(fpending),%ecx /* check for an unmasked fast int */
162 testl PCPU(ipending),%ecx /* check for an unmasked slow int */
165 movl PCPU(spending),%ecx /* check for a pending software int */
169 testl $RQF_AST_MASK,PCPU(reqflags) /* any pending ASTs? */
172 /* ASTs are only applicable when returning to userland */
173 testb $SEL_RPL_MASK,TF_CS(%rsp)
177 * Nothing left to do, finish up. Interrupts are still disabled.
178 * %eax contains the mask of IRQ's that are not available due to
179 * BGL requirements. We can only clear RQF_INTPEND if *ALL* pending
180 * interrupts have been processed.
182 subl $TDPRI_CRIT,TD_PRI(%rbx) /* interlocked with cli */
185 andl $~RQF_INTPEND,PCPU(reqflags)
190 * Restore register and iret. iret can fault on %rip (which is
191 * really stupid). If this occurs we re-fault and vector to
192 * doreti_iret_fault().
195 * can be set from user mode, this can result in a kernel mode
196 * exception. The trap code will revector to the *_fault code
197 * which then sets up a T_PROTFLT signal. If the signal is
198 * sent to userland, sendsig() will automatically clean up all
199 * the segment registers to avoid a loop.
202 .globl doreti_syscall_ret
204 POP_FRAME /* registers and %gs (+cli) */
205 /* special global also used by exception.S */
210 * doreti_iret_fault. Alternative return code for
211 * the case where we get a fault in the doreti_exit code
212 * above. trap() (sys/platform/pc64/amd64/trap.c) catches this specific
213 * case, sends the process a signal and continues in the
214 * corresponding place in the code below.
217 .globl doreti_iret_fault
220 testq $PSL_I,TF_RFLAGS(%rsp)
224 movq $T_PROTFLT,TF_TRAPNO(%rsp)
225 movq $0,TF_ERR(%rsp) /* XXX should be the error code */
226 movq $0,TF_ADDR(%rsp)
227 FAKE_MCOUNT(TF_RIP(%rsp))
231 * FAST interrupt pending. NOTE: stack context holds frame structure
232 * for fast interrupt procedure, do not do random pushes or pops!
236 andl PCPU(fpending),%ecx /* only check fast ints */
237 bsfl %ecx, %ecx /* locate the next dispatchable int */
238 btrl %ecx, PCPU(fpending) /* is it really still pending? */
240 pushq %rax /* save IRQ mask unavailable for BGL */
241 /* NOTE: is also CPL in frame */
244 pushq %rcx /* save ecx */
249 /* MP lock successful */
252 incl PCPU(intr_nesting_level)
253 call dofastunpend /* unpend fast intr %ecx */
254 decl PCPU(intr_nesting_level)
263 btsl %ecx, PCPU(fpending) /* oops, couldn't get the MP lock */
264 popq %rax /* add to temp. cpl mask to ignore */
265 orl PCPU(fpending),%eax
269 * INTR interrupt pending
271 * Temporarily back-out our critical section to allow an interrupt
272 * preempt us when we schedule it. Bump intr_nesting_level to
273 * prevent the switch code from recursing via splz too deeply.
277 andl PCPU(ipending),%ecx /* only check normal ints */
278 bsfl %ecx, %ecx /* locate the next dispatchable int */
279 btrl %ecx, PCPU(ipending) /* is it really still pending? */
282 movl %ecx,%edi /* argument to C function */
283 incl TD_NEST_COUNT(%rbx) /* prevent doreti/splz nesting */
284 subl $TDPRI_CRIT,TD_PRI(%rbx) /* so we can preempt */
285 call sched_ithd /* YYY must pull in imasks */
286 addl $TDPRI_CRIT,TD_PRI(%rbx)
287 decl TD_NEST_COUNT(%rbx)
292 * SOFT interrupt pending
294 * Temporarily back-out our critical section to allow an interrupt
295 * preempt us when we schedule it. Bump intr_nesting_level to
296 * prevent the switch code from recursing via splz too deeply.
300 bsfl %ecx,%ecx /* locate the next pending softint */
301 btrl %ecx,PCPU(spending) /* make sure its still pending */
303 addl $FIRST_SOFTINT,%ecx /* actual intr number */
305 movl %ecx,%edi /* argument to C call */
306 incl TD_NEST_COUNT(%rbx) /* prevent doreti/splz nesting */
307 subl $TDPRI_CRIT,TD_PRI(%rbx) /* so we can preempt */
308 call sched_ithd /* YYY must pull in imasks */
309 addl $TDPRI_CRIT,TD_PRI(%rbx)
310 decl TD_NEST_COUNT(%rbx)
315 * AST pending. We clear RQF_AST_SIGNAL automatically, the others
316 * are cleared by the trap as they are processed.
318 * Temporarily back-out our critical section because trap() can be
319 * a long-winded call, and we want to be more syscall-like.
321 * YYY theoretically we can call lwkt_switch directly if all we need
322 * to do is a reschedule.
325 andl $~(RQF_AST_SIGNAL|RQF_AST_UPCALL),PCPU(reqflags)
327 movl %eax,%r12d /* save cpl (can't use stack) */
328 movl $T_ASTFLT,TF_TRAPNO(%rsp)
329 movq %rsp,%rdi /* pass frame by ref (%edi = C arg) */
330 subl $TDPRI_CRIT,TD_PRI(%rbx)
332 addl $TDPRI_CRIT,TD_PRI(%rbx)
333 movl %r12d,%eax /* restore cpl for loop */
338 * IPIQ message pending. We clear RQF_IPIQ automatically.
341 movl %eax,%r12d /* save cpl (can't use stack) */
342 incl PCPU(intr_nesting_level)
343 andl $~RQF_IPIQ,PCPU(reqflags)
344 subq $8,%rsp /* trapframe->intrframe */
345 movq %rsp,%rdi /* pass frame by ref (C arg) */
346 call lwkt_process_ipiq_frame
348 decl PCPU(intr_nesting_level)
349 movl %r12d,%eax /* restore cpl for loop */
353 movl %eax,%r12d /* save cpl (can't use stack) */
354 incl PCPU(intr_nesting_level)
355 andl $~RQF_TIMER,PCPU(reqflags)
356 subq $16,%rsp /* add dummy vec and ppl */
357 movq %rsp,%rdi /* pass frame by ref (C arg) */
358 call lapic_timer_process_frame
360 decl PCPU(intr_nesting_level)
361 movl %r12d,%eax /* restore cpl for loop */
367 * SPLZ() a C callable procedure to dispatch any unmasked pending
368 * interrupts regardless of critical section nesting. ASTs
369 * are not dispatched.
371 * Use %eax to track those IRQs that could not be processed
372 * due to BGL requirements.
379 movq PCPU(curthread),%rbx
380 addl $TDPRI_CRIT,TD_PRI(%rbx)
385 movl %eax,%ecx /* ecx = ~CPL */
388 testl $RQF_IPIQ,PCPU(reqflags)
390 testl $RQF_TIMER,PCPU(reqflags)
393 testl PCPU(fpending),%ecx /* check for an unmasked fast int */
396 testl PCPU(ipending),%ecx
399 movl PCPU(spending),%ecx
403 subl $TDPRI_CRIT,TD_PRI(%rbx)
406 * Nothing left to do, finish up. Interrupts are still disabled.
407 * If our mask of IRQs we couldn't process due to BGL requirements
408 * is 0 then there are no pending interrupt sources left and we
409 * can clear RQF_INTPEND.
413 andl $~RQF_INTPEND,PCPU(reqflags)
420 * FAST interrupt pending
424 andl PCPU(fpending),%ecx /* only check fast ints */
425 bsfl %ecx, %ecx /* locate the next dispatchable int */
426 btrl %ecx, PCPU(fpending) /* is it really still pending? */
431 movl %ecx,%edi /* argument to try_mplock */
437 incl PCPU(intr_nesting_level)
438 call dofastunpend /* unpend fast intr %ecx */
439 decl PCPU(intr_nesting_level)
448 btsl %ecx, PCPU(fpending) /* oops, couldn't get the MP lock */
450 orl PCPU(fpending),%eax
454 * INTR interrupt pending
456 * Temporarily back-out our critical section to allow the interrupt
461 andl PCPU(ipending),%ecx /* only check normal ints */
462 bsfl %ecx, %ecx /* locate the next dispatchable int */
463 btrl %ecx, PCPU(ipending) /* is it really still pending? */
467 movl %ecx,%edi /* C argument */
468 subl $TDPRI_CRIT,TD_PRI(%rbx)
469 incl TD_NEST_COUNT(%rbx) /* prevent doreti/splz nesting */
470 call sched_ithd /* YYY must pull in imasks */
471 addl $TDPRI_CRIT,TD_PRI(%rbx)
472 decl TD_NEST_COUNT(%rbx) /* prevent doreti/splz nesting */
477 * SOFT interrupt pending
479 * Temporarily back-out our critical section to allow the interrupt
484 bsfl %ecx,%ecx /* locate the next pending softint */
485 btrl %ecx,PCPU(spending) /* make sure its still pending */
487 addl $FIRST_SOFTINT,%ecx /* actual intr number */
490 movl %ecx,%edi /* C argument */
491 subl $TDPRI_CRIT,TD_PRI(%rbx)
492 incl TD_NEST_COUNT(%rbx) /* prevent doreti/splz nesting */
493 call sched_ithd /* YYY must pull in imasks */
494 addl $TDPRI_CRIT,TD_PRI(%rbx)
495 decl TD_NEST_COUNT(%rbx) /* prevent doreti/splz nesting */
501 andl $~RQF_IPIQ,PCPU(reqflags)
503 call lwkt_process_ipiq
508 andl $~RQF_TIMER,PCPU(reqflags)
510 call lapic_timer_process
516 * dofastunpend(%ecx:intr)
518 * A FAST interrupt previously made pending can now be run,
519 * execute it by pushing a dummy interrupt frame and
520 * calling ithread_fast_handler to execute or schedule it.
522 * ithread_fast_handler() returns 0 if it wants us to unmask
523 * further interrupts.
526 pushfq ; /* phys int frame / flags */ \
528 pushq %rax ; /* phys int frame / cs */ \
529 pushq 3*8(%rsp) ; /* original caller eip */ \
530 subq $TF_RIP,%rsp ; /* trap frame */ \
531 movq $0,TF_XFLAGS(%rsp) ; /* extras */ \
532 movq $0,TF_TRAPNO(%rsp) ; /* extras */ \
533 movq $0,TF_ADDR(%rsp) ; /* extras */ \
534 movq $0,TF_FLAGS(%rsp) ; /* extras */ \
535 movq $0,TF_ERR(%rsp) ; /* extras */ \
538 addq $TF_RIP+(3*8),%rsp ; \
541 pushq %rbp /* frame for backtrace */
544 pushq %rcx /* last part of intrframe = intr */
545 incl fastunpend_count
546 movq %rsp,%rdi /* pass frame by reference C arg */
547 call ithread_fast_handler /* returns 0 to unmask */
548 popq %rdi /* intrframe->trapframe */
549 /* + also rdi C arg to next call */
552 movq MachIntrABI + MACHINTR_INTREN, %rax
553 callq *%rax /* MachIntrABI.intren(intr) */