AMD64 - Correct wire_count, statistics, and other pmap fixes and cleanups
[dragonfly.git] / sys / platform / pc64 / amd64 / ipl.s
blobe81c10e25a9d0ffe8791e041ce1efb243c2bbb1c
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 * ---
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
41 * William Jolitz.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
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
69 * SUCH DAMAGE.
71 * @(#)ipl.s
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>
84 #include "assym.s"
87 * AT/386
88 * Vector interrupt control section
90 * fpending - Pending interrupts (set when a masked interrupt occurs)
91 * spending - Pending software interrupts
93 .data
94 ALIGN_DATA
96 .globl fastunpend_count
97 fastunpend_count: .long 0
99 .text
100 SUPERALIGN_TEXT
103 * GENERAL NOTES
105 * - fast interrupts are always called with a critical section
106 * held
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
120 * processing.
124 * DORETI
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.
135 SUPERALIGN_TEXT
136 .globl doreti
137 .type doreti,@function
138 doreti:
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 */
144 je 5f
145 cmpl $TDPRI_CRIT,TD_PRI(%rbx) /* can't unpend if in critical sec */
146 jge 5f
147 addl $TDPRI_CRIT,TD_PRI(%rbx) /* force all ints to pending */
148 doreti_next:
149 sti /* allow new interrupts */
150 movl %eax,%ecx /* irq mask unavailable due to BGL */
151 notl %ecx
152 cli /* disallow YYY remove */
153 #ifdef SMP
154 testl $RQF_IPIQ,PCPU(reqflags)
155 jnz doreti_ipiq
156 testl $RQF_TIMER,PCPU(reqflags)
157 jnz doreti_timer
158 #endif
159 testl PCPU(fpending),%ecx /* check for an unmasked fast int */
160 jnz doreti_fast
162 movl PCPU(spending),%ecx /* check for a pending software int */
163 cmpl $0,%ecx
164 jnz doreti_soft
166 testl $RQF_AST_MASK,PCPU(reqflags) /* any pending ASTs? */
167 jz 2f
169 /* ASTs are only applicable when returning to userland */
170 testb $SEL_RPL_MASK,TF_CS(%rsp)
171 jnz doreti_ast
174 * Nothing left to do, finish up. Interrupts are still disabled.
175 * %eax contains the mask of IRQ's that are not available due to
176 * BGL requirements. We can only clear RQF_INTPEND if *ALL* pending
177 * interrupts have been processed.
179 subl $TDPRI_CRIT,TD_PRI(%rbx) /* interlocked with cli */
180 testl %eax,%eax
181 jnz 5f
182 andl $~RQF_INTPEND,PCPU(reqflags)
184 MEXITCOUNT
187 * Restore register and iret. iret can fault on %rip (which is
188 * really stupid). If this occurs we re-fault and vector to
189 * doreti_iret_fault().
191 * ...
192 * can be set from user mode, this can result in a kernel mode
193 * exception. The trap code will revector to the *_fault code
194 * which then sets up a T_PROTFLT signal. If the signal is
195 * sent to userland, sendsig() will automatically clean up all
196 * the segment registers to avoid a loop.
198 .globl doreti_iret
199 .globl doreti_syscall_ret
200 doreti_syscall_ret:
201 POP_FRAME /* registers and %gs (+cli) */
202 /* special global also used by exception.S */
203 doreti_iret:
204 iretq
207 * doreti_iret_fault. Alternative return code for
208 * the case where we get a fault in the doreti_exit code
209 * above. trap() (sys/platform/pc64/amd64/trap.c) catches this specific
210 * case, sends the process a signal and continues in the
211 * corresponding place in the code below.
213 ALIGN_TEXT
214 .globl doreti_iret_fault
215 doreti_iret_fault:
216 PUSH_FRAME_NOSWAP
217 testq $PSL_I,TF_RFLAGS(%rsp)
218 jz 2f
221 movq $T_PROTFLT,TF_TRAPNO(%rsp)
222 movq $0,TF_ERR(%rsp) /* XXX should be the error code */
223 movq $0,TF_ADDR(%rsp)
224 FAKE_MCOUNT(TF_RIP(%rsp))
225 jmp calltrap
228 * FAST interrupt pending. NOTE: stack context holds frame structure
229 * for fast interrupt procedure, do not do random pushes or pops!
231 ALIGN_TEXT
232 doreti_fast:
233 andl PCPU(fpending),%ecx /* only check fast ints */
234 bsfl %ecx, %ecx /* locate the next dispatchable int */
235 btrl %ecx, PCPU(fpending) /* is it really still pending? */
236 jnc doreti_next
237 pushq %rax /* save IRQ mask unavailable for BGL */
238 /* NOTE: is also CPL in frame */
239 #if 0
240 #ifdef SMP
241 pushq %rcx /* save ecx */
242 call try_mplock
243 popq %rcx
244 testl %eax,%eax
245 jz 1f
246 /* MP lock successful */
247 #endif
248 #endif
249 call dofastunpend /* unpend fast intr %ecx */
250 #if 0
251 #ifdef SMP
252 call rel_mplock
253 #endif
254 #endif
255 popq %rax
256 jmp doreti_next
258 btsl %ecx, PCPU(fpending) /* oops, couldn't get the MP lock */
259 popq %rax /* add to temp. cpl mask to ignore */
260 orl PCPU(fpending),%eax
261 jmp doreti_next
264 * SOFT interrupt pending
266 * Temporarily back-out our critical section to allow an interrupt
267 * preempt us when we schedule it. Bump intr_nesting_level to
268 * prevent the switch code from recursing via splz too deeply.
270 ALIGN_TEXT
271 doreti_soft:
272 bsfl %ecx,%ecx /* locate the next pending softint */
273 btrl %ecx,PCPU(spending) /* make sure its still pending */
274 jnc doreti_next
275 addl $FIRST_SOFTINT,%ecx /* actual intr number */
276 pushq %rax
277 movl %ecx,%edi /* argument to C call */
278 incl TD_NEST_COUNT(%rbx) /* prevent doreti/splz nesting */
279 subl $TDPRI_CRIT,TD_PRI(%rbx) /* so we can preempt */
280 call sched_ithd /* YYY must pull in imasks */
281 addl $TDPRI_CRIT,TD_PRI(%rbx)
282 decl TD_NEST_COUNT(%rbx)
283 popq %rax
284 jmp doreti_next
287 * AST pending. We clear RQF_AST_SIGNAL automatically, the others
288 * are cleared by the trap as they are processed.
290 * Temporarily back-out our critical section because trap() can be
291 * a long-winded call, and we want to be more syscall-like.
293 * YYY theoretically we can call lwkt_switch directly if all we need
294 * to do is a reschedule.
296 doreti_ast:
297 andl $~(RQF_AST_SIGNAL|RQF_AST_UPCALL),PCPU(reqflags)
299 movl %eax,%r12d /* save cpl (can't use stack) */
300 movl $T_ASTFLT,TF_TRAPNO(%rsp)
301 movq %rsp,%rdi /* pass frame by ref (%edi = C arg) */
302 subl $TDPRI_CRIT,TD_PRI(%rbx)
303 call trap
304 addl $TDPRI_CRIT,TD_PRI(%rbx)
305 movl %r12d,%eax /* restore cpl for loop */
306 jmp doreti_next
308 #ifdef SMP
310 * IPIQ message pending. We clear RQF_IPIQ automatically.
312 doreti_ipiq:
313 movl %eax,%r12d /* save cpl (can't use stack) */
314 incl PCPU(intr_nesting_level)
315 andl $~RQF_IPIQ,PCPU(reqflags)
316 subq $8,%rsp /* trapframe->intrframe */
317 movq %rsp,%rdi /* pass frame by ref (C arg) */
318 call lwkt_process_ipiq_frame
319 addq $8,%rsp /* intrframe->trapframe */
320 decl PCPU(intr_nesting_level)
321 movl %r12d,%eax /* restore cpl for loop */
322 jmp doreti_next
324 doreti_timer:
325 movl %eax,%r12d /* save cpl (can't use stack) */
326 incl PCPU(intr_nesting_level)
327 andl $~RQF_TIMER,PCPU(reqflags)
328 subq $8,%rsp /* trapframe->intrframe */
329 movq %rsp,%rdi /* pass frame by ref (C arg) */
330 call lapic_timer_process_frame
331 addq $8,%rsp /* intrframe->trapframe */
332 decl PCPU(intr_nesting_level)
333 movl %r12d,%eax /* restore cpl for loop */
334 jmp doreti_next
336 #endif
339 * SPLZ() a C callable procedure to dispatch any unmasked pending
340 * interrupts regardless of critical section nesting. ASTs
341 * are not dispatched.
343 * Use %eax to track those IRQs that could not be processed
344 * due to BGL requirements.
346 SUPERALIGN_TEXT
348 ENTRY(splz)
349 pushfq
350 pushq %rbx
351 movq PCPU(curthread),%rbx
352 addl $TDPRI_CRIT,TD_PRI(%rbx)
353 movl $0,%eax
355 splz_next:
357 movl %eax,%ecx /* ecx = ~CPL */
358 notl %ecx
359 #ifdef SMP
360 testl $RQF_IPIQ,PCPU(reqflags)
361 jnz splz_ipiq
362 testl $RQF_TIMER,PCPU(reqflags)
363 jnz splz_timer
364 #endif
365 testl PCPU(fpending),%ecx /* check for an unmasked fast int */
366 jnz splz_fast
368 movl PCPU(spending),%ecx
369 cmpl $0,%ecx
370 jnz splz_soft
372 subl $TDPRI_CRIT,TD_PRI(%rbx)
375 * Nothing left to do, finish up. Interrupts are still disabled.
376 * If our mask of IRQs we couldn't process due to BGL requirements
377 * is 0 then there are no pending interrupt sources left and we
378 * can clear RQF_INTPEND.
380 testl %eax,%eax
381 jnz 5f
382 andl $~RQF_INTPEND,PCPU(reqflags)
384 popq %rbx
385 popfq
389 * FAST interrupt pending
391 ALIGN_TEXT
392 splz_fast:
393 andl PCPU(fpending),%ecx /* only check fast ints */
394 bsfl %ecx, %ecx /* locate the next dispatchable int */
395 btrl %ecx, PCPU(fpending) /* is it really still pending? */
396 jnc splz_next
397 pushq %rax
398 #if 0
399 #ifdef SMP
400 movl %ecx,%edi /* argument to try_mplock */
401 call try_mplock
402 testl %eax,%eax
403 jz 1f
404 #endif
405 #endif
406 call dofastunpend /* unpend fast intr %ecx */
407 #if 0
408 #ifdef SMP
409 call rel_mplock
410 #endif
411 #endif
412 popq %rax
413 jmp splz_next
415 btsl %ecx, PCPU(fpending) /* oops, couldn't get the MP lock */
416 popq %rax
417 orl PCPU(fpending),%eax
418 jmp splz_next
421 * SOFT interrupt pending
423 * Temporarily back-out our critical section to allow the interrupt
424 * preempt us.
426 ALIGN_TEXT
427 splz_soft:
428 bsfl %ecx,%ecx /* locate the next pending softint */
429 btrl %ecx,PCPU(spending) /* make sure its still pending */
430 jnc splz_next
431 addl $FIRST_SOFTINT,%ecx /* actual intr number */
433 pushq %rax
434 movl %ecx,%edi /* C argument */
435 subl $TDPRI_CRIT,TD_PRI(%rbx)
436 incl TD_NEST_COUNT(%rbx) /* prevent doreti/splz nesting */
437 call sched_ithd /* YYY must pull in imasks */
438 addl $TDPRI_CRIT,TD_PRI(%rbx)
439 decl TD_NEST_COUNT(%rbx) /* prevent doreti/splz nesting */
440 popq %rax
441 jmp splz_next
443 #ifdef SMP
444 splz_ipiq:
445 andl $~RQF_IPIQ,PCPU(reqflags)
446 pushq %rax
447 call lwkt_process_ipiq
448 popq %rax
449 jmp splz_next
451 splz_timer:
452 andl $~RQF_TIMER,PCPU(reqflags)
453 pushq %rax
454 call lapic_timer_process
455 popq %rax
456 jmp splz_next
457 #endif
460 * dofastunpend(%ecx:intr)
462 * A FAST interrupt previously made pending can now be run,
463 * execute it by pushing a dummy interrupt frame and
464 * calling ithread_fast_handler to execute or schedule it.
466 * ithread_fast_handler() returns 0 if it wants us to unmask
467 * further interrupts.
469 #define PUSH_DUMMY \
470 pushfq ; /* phys int frame / flags */ \
471 movl %cs,%eax ; \
472 pushq %rax ; /* phys int frame / cs */ \
473 pushq 3*8(%rsp) ; /* original caller eip */ \
474 subq $TF_RIP,%rsp ; /* trap frame */ \
475 movq $0,TF_XFLAGS(%rsp) ; /* extras */ \
476 movq $0,TF_TRAPNO(%rsp) ; /* extras */ \
477 movq $0,TF_ADDR(%rsp) ; /* extras */ \
478 movq $0,TF_FLAGS(%rsp) ; /* extras */ \
479 movq $0,TF_ERR(%rsp) ; /* extras */ \
481 #define POP_DUMMY \
482 addq $TF_RIP+(3*8),%rsp ; \
484 dofastunpend:
485 pushq %rbp /* frame for backtrace */
486 movq %rsp,%rbp
487 PUSH_DUMMY
488 pushq %rcx /* last part of intrframe = intr */
489 incl fastunpend_count
490 movq %rsp,%rdi /* pass frame by reference C arg */
491 call ithread_fast_handler /* returns 0 to unmask */
492 popq %rdi /* intrframe->trapframe */
493 /* + also rdi C arg to next call */
494 cmpl $0,%eax
495 jnz 1f
496 movq MachIntrABI + MACHINTR_INTREN, %rax
497 callq *%rax /* MachIntrABI.intren(intr) */
499 POP_DUMMY
500 popq %rbp