2 * Copyright (c) 2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 #include <i386/eflags.h>
32 #include <i386/rtclock_asm.h>
33 #include <i386/trap.h>
34 #define _ARCH_I386_ASM_HELP_H_ /* Prevent inclusion of user header */
35 #include <mach/i386/syscall_sw.h>
36 #include <i386/postcode.h>
37 #include <i386/proc_reg.h>
38 #include <mach/exception_types.h>
45 * This is the low-level trap and interrupt handling code associated with
46 * the IDT. It also includes system call handlers for sysenter/syscall.
47 * The IDT itself is defined in mp_desc.c.
49 * Code here is structured as follows:
51 * stubs Code called directly from an IDT vector.
52 * All entry points have the "idt64_" prefix and they are built
53 * using macros expanded by the inclusion of idt_table.h.
54 * This code performs vector-dependent identification and jumps
55 * into the dispatch code.
57 * dispatch The dispatch code is responsible for saving the thread state
58 * (which is either 64-bit or 32-bit) and then jumping to the
59 * class handler identified by the stub.
61 * returns Code to restore state and return to the previous context.
63 * handlers There are several classes of handlers:
64 * interrupt - asynchronous events typically from external devices
65 * trap - synchronous events due to thread execution
66 * syscall - synchronous system call request
73 #define HNDL_ALLINTRS EXT(hndl_allintrs)
74 #define HNDL_ALLTRAPS EXT(hndl_alltraps)
75 #define HNDL_SYSENTER EXT(hndl_sysenter)
76 #define HNDL_SYSCALL EXT(hndl_syscall)
77 #define HNDL_UNIX_SCALL EXT(hndl_unix_scall)
78 #define HNDL_MACH_SCALL EXT(hndl_mach_scall)
79 #define HNDL_MDEP_SCALL EXT(hndl_mdep_scall)
80 #define HNDL_DOUBLE_FAULT EXT(hndl_double_fault)
81 #define HNDL_MACHINE_CHECK EXT(hndl_machine_check)
85 #define PUSH_FUNCTION(func) \
88 leaq func(%rip), %rax ;\
92 #define PUSH_FUNCTION(func) pushq func
95 /* The wrapper for all non-special traps/interrupts */
96 /* Everything up to PUSH_FUNCTION is just to output
97 * the interrupt number out to the postcode display
100 #define IDT_ENTRY_WRAPPER(n, f) \
102 POSTCODE2(0x6400+n) ;\
108 #define IDT_ENTRY_WRAPPER(n, f) \
114 /* A trap that comes with an error code already on the stack */
115 #define TRAP_ERR(n, f) \
117 IDT_ENTRY_WRAPPER(n, HNDL_ALLTRAPS)
123 IDT_ENTRY_WRAPPER(n, HNDL_ALLTRAPS)
125 #define USER_TRAP TRAP
128 #define INTERRUPT(n) \
129 Entry(_intr_ ## n) ;\
131 IDT_ENTRY_WRAPPER(n, HNDL_ALLINTRS)
133 /* A trap with a special-case handler, hence we don't need to define anything */
134 #define TRAP_SPC(n, f)
135 #define TRAP_IST1(n, f)
136 #define TRAP_IST2(n, f)
137 #define USER_TRAP_SPC(n, f)
139 /* Generate all the stubs */
140 #include "idt_table.h"
143 * Common dispatch point.
144 * Determine what mode has been interrupted and save state accordingly.
146 * rsp from user-space: interrupt state in PCB, or
147 * from kernel-space: interrupt state in kernel or interrupt stack
148 * GSBASE from user-space: pthread area, or
149 * from kernel-space: cpu_data
152 cmpl $(KERNEL64_CS), ISF64_CS(%rsp)
158 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP
159 je L_dispatch_U32 /* 32-bit user task */
162 subq $(ISS64_OFFSET), %rsp
163 mov %r15, R64_R15(%rsp)
165 mov %gs:CPU_KERNEL_STACK, %rsp
169 subq $(ISS64_OFFSET), %rsp
170 mov %r15, R64_R15(%rsp)
174 * Here for 64-bit user task or kernel
177 movl $(SS_64), SS_FLAVOR(%r15)
180 * Save segment regs - for completeness since theyre not used.
182 movl %fs, R64_FS(%r15)
183 movl %gs, R64_GS(%r15)
185 /* Save general-purpose registers */
186 mov %rax, R64_RAX(%r15)
187 mov %rbx, R64_RBX(%r15)
188 mov %rcx, R64_RCX(%r15)
189 mov %rdx, R64_RDX(%r15)
190 mov %rbp, R64_RBP(%r15)
191 mov %rdi, R64_RDI(%r15)
192 mov %rsi, R64_RSI(%r15)
193 mov %r8, R64_R8(%r15)
194 mov %r9, R64_R9(%r15)
195 mov %r10, R64_R10(%r15)
196 mov %r11, R64_R11(%r15)
197 mov %r12, R64_R12(%r15)
198 mov %r13, R64_R13(%r15)
199 mov %r14, R64_R14(%r15)
201 /* cr2 is significant only for page-faults */
203 mov %rax, R64_CR2(%r15)
205 mov R64_TRAPNO(%r15), %ebx /* %ebx := trapno for later */
206 mov R64_TRAPFN(%r15), %rdx /* %rdx := trapfn for later */
207 mov R64_CS(%r15), %esi /* %esi := cs for later */
209 jmp L_common_dispatch
211 L_64bit_entry_reject:
213 * Here for a 64-bit user attempting an invalid kernel entry.
216 leaq HNDL_ALLTRAPS(%rip), %rax
217 movq %rax, ISF64_TRAPFN+8(%rsp)
219 movq $(T_INVALID_OPCODE), ISF64_TRAPNO(%rsp)
224 * Check we're not a confused 64-bit user.
226 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP
227 jne L_64bit_entry_reject
228 /* fall through to 32-bit handler: */
230 L_dispatch_U32: /* 32-bit user task */
231 subq $(ISS64_OFFSET), %rsp
233 mov %gs:CPU_KERNEL_STACK, %rsp
234 movl $(SS_32), SS_FLAVOR(%r15)
239 movl %ds, R32_DS(%r15)
240 movl %es, R32_ES(%r15)
241 movl %fs, R32_FS(%r15)
242 movl %gs, R32_GS(%r15)
245 * Save general 32-bit registers
247 mov %eax, R32_EAX(%r15)
248 mov %ebx, R32_EBX(%r15)
249 mov %ecx, R32_ECX(%r15)
250 mov %edx, R32_EDX(%r15)
251 mov %ebp, R32_EBP(%r15)
252 mov %esi, R32_ESI(%r15)
253 mov %edi, R32_EDI(%r15)
255 /* Unconditionally save cr2; only meaningful on page faults */
257 mov %eax, R32_CR2(%r15)
260 * Copy registers already saved in the machine state
261 * (in the interrupt stack frame) into the compat save area.
263 mov R64_RIP(%r15), %eax
264 mov %eax, R32_EIP(%r15)
265 mov R64_RFLAGS(%r15), %eax
266 mov %eax, R32_EFLAGS(%r15)
267 mov R64_RSP(%r15), %eax
268 mov %eax, R32_UESP(%r15)
269 mov R64_SS(%r15), %eax
270 mov %eax, R32_SS(%r15)
271 L_dispatch_U32_after_fault:
272 mov R64_CS(%r15), %esi /* %esi := %cs for later */
273 mov %esi, R32_CS(%r15)
274 mov R64_TRAPNO(%r15), %ebx /* %ebx := trapno for later */
275 mov %ebx, R32_TRAPNO(%r15)
276 mov R64_ERR(%r15), %eax
277 mov %eax, R32_ERR(%r15)
278 mov R64_TRAPFN(%r15), %rdx /* %rdx := trapfn for later */
281 cld /* Ensure the direction flag is clear in the kernel */
282 cmpl $0, EXT(pmap_smap_enabled)(%rip)
284 clac /* Clear EFLAGS.AC if SMAP is present/enabled */
287 * On entering the kernel, we don't need to switch cr3
288 * because the kernel shares the user's address space.
289 * But we mark the kernel's cr3 as "active".
290 * If, however, the invalid cr3 flag is set, we have to flush tlbs
291 * since the kernel's mapping was changed while we were in userspace.
293 * But: if global no_shared_cr3 is TRUE we do switch to the kernel's cr3
294 * so that illicit accesses to userspace can be trapped.
296 mov %gs:CPU_KERNEL_CR3, %rcx
297 mov %rcx, %gs:CPU_ACTIVE_CR3
298 test $3, %esi /* user/kernel? */
299 jz 2f /* skip cr3 reload from kernel */
301 cmpl $0, EXT(no_shared_cr3)(%rip)
303 mov %rcx, %cr3 /* load kernel cr3 */
304 jmp 4f /* and skip tlb flush test */
306 mov %gs:CPU_ACTIVE_CR3+4, %rcx
310 movl $0, %gs:CPU_TLB_INVALID
311 testl $(1<<16), %ecx /* Global? */
313 mov %cr4, %rcx /* RMWW CR4, for lack of an alternative*/
314 and $(~CR4_PGE), %rcx
323 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Get the active thread */
324 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling trap */
325 cmpq $0, TH_PCB_IDS(%rcx) /* Is there a debug register state? */
327 xor %ecx, %ecx /* If so, reset DR7 (the control) */
330 incl %gs:hwIntCnt(,%ebx,4) // Bump the trap/intr count
331 /* Dispatch the designated handler */
335 * Control is passed here to return to user.
337 Entry(return_to_user)
341 // XXX 'Be nice to tidy up this debug register restore sequence...
342 mov %gs:CPU_ACTIVE_THREAD, %rdx
343 movq TH_PCB_IDS(%rdx),%rax /* Obtain this thread's debug state */
345 test %rax, %rax /* Is there a debug register context? */
346 je 2f /* branch if not */
347 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP /* Are we a 32-bit task? */
349 movl DS_DR0(%rax), %ecx /* If so, load the 32 bit DRs */
351 movl DS_DR1(%rax), %ecx
353 movl DS_DR2(%rax), %ecx
355 movl DS_DR3(%rax), %ecx
357 movl DS_DR7(%rax), %ecx
358 movq %rcx, %gs:CPU_DR7
361 mov DS64_DR0(%rax), %rcx /* Load the full width DRs*/
363 mov DS64_DR1(%rax), %rcx
365 mov DS64_DR2(%rax), %rcx
367 mov DS64_DR3(%rax), %rcx
369 mov DS64_DR7(%rax), %rcx
370 mov %rcx, %gs:CPU_DR7
373 * On exiting the kernel there's no need to switch cr3 since we're
374 * already running in the user's address space which includes the
375 * kernel. Nevertheless, we now mark the task's cr3 as active.
376 * But, if no_shared_cr3 is set, we do need to switch cr3 at this point.
378 mov %gs:CPU_TASK_CR3, %rcx
379 mov %rcx, %gs:CPU_ACTIVE_CR3
380 movl EXT(no_shared_cr3)(%rip), %eax
381 test %eax, %eax /* -no_shared_cr3 */
385 mov %gs:CPU_DR7, %rax /* Is there a debug control register?*/
388 mov %rax, %dr7 /* Set DR7 */
391 cmpl $(SS_64), SS_FLAVOR(%r15) /* 64-bit state? */
396 cmpl $(SS_32), SS_FLAVOR(%r15) /* 32-bit state? */
400 CCALL1(panic_idt64, %r15)
402 #endif /* DEBUG_IDT64 */
405 * Restore registers into the machine state for iret.
406 * Here on fault stack and PCB address in R11.
408 movl R32_EIP(%r15), %eax
409 movl %eax, R64_RIP(%r15)
410 movl R32_EFLAGS(%r15), %eax
411 movl %eax, R64_RFLAGS(%r15)
412 movl R32_CS(%r15), %eax
413 movl %eax, R64_CS(%r15)
414 movl R32_UESP(%r15), %eax
415 movl %eax, R64_RSP(%r15)
416 movl R32_SS(%r15), %eax
417 movl %eax, R64_SS(%r15)
420 * Restore general 32-bit registers
422 movl R32_EAX(%r15), %eax
423 movl R32_EBX(%r15), %ebx
424 movl R32_ECX(%r15), %ecx
425 movl R32_EDX(%r15), %edx
426 movl R32_EBP(%r15), %ebp
427 movl R32_ESI(%r15), %esi
428 movl R32_EDI(%r15), %edi
431 * Restore segment registers. A segment exception taken here will
432 * push state on the IST1 stack and will not affect the "PCB stack".
434 mov %r15, %rsp /* Set the PCB as the stack */
437 movl R32_DS(%rsp), %ds
439 movl R32_ES(%rsp), %es
441 movl R32_FS(%rsp), %fs
443 movl R32_GS(%rsp), %gs
445 /* pop compat frame + trapno, trapfn and error */
446 add $(ISS64_OFFSET)+8+8+8, %rsp
447 cmpl $(SYSENTER_CS),ISF64_CS-8-8-8(%rsp)
448 /* test for fast entry/exit */
451 iretq /* return from interrupt */
454 pop %rdx /* user return eip */
455 pop %rcx /* pop and toss cs */
456 andl $(~EFL_IF), (%rsp) /* clear interrupts enable, sti below */
457 popf /* flags - carry denotes failure */
458 pop %rcx /* user return esp */
459 sti /* interrupts enabled after sysexit */
460 sysexitl /* 32-bit sysexit */
464 cmpl $(SS_64), SS_FLAVOR(%r15) /* 64-bit state? */
468 CCALL1(panic_idt64, %r15)
471 cmpl $(KERNEL64_CS), R64_CS(%r15)
473 CCALL1(panic_idt64, %r15)
480 * Restore general 64-bit registers.
481 * Here on fault stack and PCB address in R15.
483 mov R64_R14(%r15), %r14
484 mov R64_R13(%r15), %r13
485 mov R64_R12(%r15), %r12
486 mov R64_R11(%r15), %r11
487 mov R64_R10(%r15), %r10
488 mov R64_R9(%r15), %r9
489 mov R64_R8(%r15), %r8
490 mov R64_RSI(%r15), %rsi
491 mov R64_RDI(%r15), %rdi
492 mov R64_RBP(%r15), %rbp
493 mov R64_RDX(%r15), %rdx
494 mov R64_RCX(%r15), %rcx
495 mov R64_RBX(%r15), %rbx
496 mov R64_RAX(%r15), %rax
499 * We must swap GS base if we're returning to user-space,
500 * or we're returning from an NMI that occurred in a trampoline
501 * before the user GS had been swapped. In the latter case, the NMI
502 * handler will have flagged the high-order 32-bits of the CS.
504 cmpq $(KERNEL64_CS), R64_CS(%r15)
508 mov R64_R15(%r15), %rsp
510 add $(ISS64_OFFSET)+24, %rsp /* pop saved state */
511 /* + trapno/trapfn/error */
512 cmpl $(SYSCALL_CS),ISF64_CS-24(%rsp)
513 /* test for fast entry/exit */
517 iretq /* return from interrupt */
521 * Here to load rcx/r11/rsp and perform the sysret back to user-space.
524 * rsp user stack pointer
526 mov ISF64_RIP-24(%rsp), %rcx
527 mov ISF64_RFLAGS-24(%rsp), %r11
528 mov ISF64_RSP-24(%rsp), %rsp
529 sysretq /* return from systen call */
534 * System call handlers.
535 * These are entered via a syscall interrupt. The system call number in %rax
536 * is saved to the error code slot in the stack frame. We then branch to the
537 * common state saving code.
541 #error NO UNIX INT!!!
543 Entry(idt64_unix_scall)
544 swapgs /* switch to kernel gs (cpu_data) */
545 pushq %rax /* save system call number */
546 PUSH_FUNCTION(HNDL_UNIX_SCALL)
548 jmp L_32bit_entry_check
551 Entry(idt64_mach_scall)
552 swapgs /* switch to kernel gs (cpu_data) */
553 pushq %rax /* save system call number */
554 PUSH_FUNCTION(HNDL_MACH_SCALL)
556 jmp L_32bit_entry_check
559 Entry(idt64_mdep_scall)
560 swapgs /* switch to kernel gs (cpu_data) */
561 pushq %rax /* save system call number */
562 PUSH_FUNCTION(HNDL_MDEP_SCALL)
564 jmp L_32bit_entry_check
566 /* Programmed into MSR_IA32_LSTAR by mp_desc.c */
570 swapgs /* Kapow! get per-cpu data area */
571 mov %rsp, %gs:CPU_UBER_TMP /* save user stack */
572 mov %gs:CPU_UBER_ISF, %rsp /* switch stack to pcb */
575 * Save values in the ISF frame in the PCB
576 * to cons up the saved machine state.
578 movl $(USER_DS), ISF64_SS(%rsp)
579 movl $(SYSCALL_CS), ISF64_CS(%rsp) /* cs - a pseudo-segment */
580 mov %r11, ISF64_RFLAGS(%rsp) /* rflags */
581 mov %rcx, ISF64_RIP(%rsp) /* rip */
582 mov %gs:CPU_UBER_TMP, %rcx
583 mov %rcx, ISF64_RSP(%rsp) /* user stack */
584 mov %rax, ISF64_ERR(%rsp) /* err/rax - syscall code */
585 movq $(T_SYSCALL), ISF64_TRAPNO(%rsp) /* trapno */
586 leaq HNDL_SYSCALL(%rip), %r11;
587 movq %r11, ISF64_TRAPFN(%rsp)
588 mov ISF64_RFLAGS(%rsp), %r11 /* Avoid leak, restore R11 */
589 jmp L_dispatch_U64 /* this can only be 64-bit */
592 * sysenter entry point
593 * Requires user code to set up:
594 * edx: user instruction pointer (return address)
595 * ecx: user stack pointer
596 * on which is pushed stub ret addr and saved ebx
597 * Return to user-space is made using sysexit.
598 * Note: sysenter/sysexit cannot be used for calls returning a value in edx,
599 * or requiring ecx to be preserved.
602 Entry(idt64_sysenter)
605 * Push values on to the PCB stack
606 * to cons up the saved machine state.
608 push $(USER_DS) /* ss */
612 * Clear, among others, the Nested Task (NT) flags bit;
613 * this is zeroed by INT, but not by SYSENTER.
617 push $(SYSENTER_CS) /* cs */
619 swapgs /* switch to kernel gs (cpu_data) */
621 push %rax /* err/eax - syscall code */
622 PUSH_FUNCTION(HNDL_SYSENTER)
624 orl $(EFL_IF), ISF64_RFLAGS(%rsp)
625 jmp L_32bit_entry_check
628 Entry(idt64_page_fault)
629 PUSH_FUNCTION(HNDL_ALLTRAPS)
631 push %rax /* save %rax temporarily */
632 testb $3, 8+ISF64_CS(%rsp) /* was trap from kernel? */
633 jz L_kernel_trap /* - yes, handle with care */
634 pop %rax /* restore %rax, swapgs, and continue */
640 * Debug trap. Check for single-stepping across system call into
641 * kernel. If this is the case, taking the debug trap has turned
642 * off single-stepping - save the flags register with the trace
646 push $0 /* error code */
647 PUSH_FUNCTION(HNDL_ALLTRAPS)
650 testb $3, ISF64_CS(%rsp)
654 * trap came from kernel mode
657 push %rax /* save %rax temporarily */
658 lea EXT(idt64_sysenter)(%rip), %rax
659 cmp %rax, ISF64_RIP+8(%rsp)
663 * Interrupt stack frame has been pushed on the temporary stack.
664 * We have to switch to pcb stack and patch up the saved state.
666 mov %rcx, ISF64_ERR(%rsp) /* save %rcx in error slot */
667 mov ISF64_SS+8(%rsp), %rcx /* top of temp stack -> pcb stack */
668 xchg %rcx,%rsp /* switch to pcb stack */
669 push $(USER_DS) /* ss */
670 push ISF64_ERR(%rcx) /* saved %rcx into rsp slot */
671 push ISF64_RFLAGS(%rcx) /* rflags */
672 push $(SYSENTER_TF_CS) /* cs - not SYSENTER_CS for iret path */
673 mov ISF64_ERR(%rcx),%rcx /* restore %rcx */
674 jmp L_sysenter_continue /* continue sysenter entry */
677 Entry(idt64_double_fault)
678 PUSH_FUNCTION(HNDL_DOUBLE_FAULT)
679 pushq $(T_DOUBLE_FAULT)
682 leaq EXT(idt64_syscall)(%rip), %rax
683 cmp %rax, ISF64_RIP+8(%rsp)
685 jne L_dispatch_kernel
687 mov ISF64_RSP(%rsp), %rsp
688 jmp L_syscall_continue
692 * For GP/NP/SS faults, we use the IST1 stack.
693 * For faults from user-space, we have to copy the machine state to the
694 * PCB stack and then dispatch as normal.
695 * For faults in kernel-space, we need to scrub for kernel exit faults and
696 * treat these as user-space faults. But for all other kernel-space faults
697 * we continue to run on the IST1 stack and we dispatch to handle the fault
700 Entry(idt64_gen_prot)
701 PUSH_FUNCTION(HNDL_ALLTRAPS)
702 pushq $(T_GENERAL_PROTECTION)
703 jmp trap_check_kernel_exit /* check for kernel exit sequence */
705 Entry(idt64_stack_fault)
706 PUSH_FUNCTION(HNDL_ALLTRAPS)
707 pushq $(T_STACK_FAULT)
708 jmp trap_check_kernel_exit /* check for kernel exit sequence */
711 PUSH_FUNCTION(HNDL_ALLTRAPS)
712 pushq $(T_SEGMENT_NOT_PRESENT)
713 /* indicate fault type */
714 trap_check_kernel_exit:
715 testb $3,ISF64_CS(%rsp)
718 /* Here for fault from user-space. Copy interrupt state to PCB. */
721 mov %rcx, %gs:CPU_UBER_TMP /* save user RCX */
722 mov %gs:CPU_UBER_ISF, %rcx /* PCB stack addr */
723 mov ISF64_SS+8(%rsp), %rax
724 mov %rax, ISF64_SS(%rcx)
725 mov ISF64_RSP+8(%rsp), %rax
726 mov %rax, ISF64_RSP(%rcx)
727 mov ISF64_RFLAGS+8(%rsp), %rax
728 mov %rax, ISF64_RFLAGS(%rcx)
729 mov ISF64_CS+8(%rsp), %rax
730 mov %rax, ISF64_CS(%rcx)
731 mov ISF64_RIP+8(%rsp), %rax
732 mov %rax, ISF64_RIP(%rcx)
733 mov ISF64_ERR+8(%rsp), %rax
734 mov %rax, ISF64_ERR(%rcx)
735 mov ISF64_TRAPFN+8(%rsp), %rax
736 mov %rax, ISF64_TRAPFN(%rcx)
737 mov ISF64_TRAPNO+8(%rsp), %rax
738 mov %rax, ISF64_TRAPNO(%rcx)
740 mov %gs:CPU_UBER_TMP, %rsp /* user RCX into RSP */
741 xchg %rcx, %rsp /* to PCB stack with user RCX */
745 /* Here for GPF from kernel_space. Check for recoverable cases. */
747 leaq EXT(ret32_iret)(%rip), %rax
748 cmp %rax, 8+ISF64_RIP(%rsp)
750 leaq EXT(ret64_iret)(%rip), %rax
751 cmp %rax, 8+ISF64_RIP(%rsp)
753 leaq EXT(ret32_set_ds)(%rip), %rax
754 cmp %rax, 8+ISF64_RIP(%rsp)
755 je L_32bit_fault_set_seg
756 leaq EXT(ret32_set_es)(%rip), %rax
757 cmp %rax, 8+ISF64_RIP(%rsp)
758 je L_32bit_fault_set_seg
759 leaq EXT(ret32_set_fs)(%rip), %rax
760 cmp %rax, 8+ISF64_RIP(%rsp)
761 je L_32bit_fault_set_seg
762 leaq EXT(ret32_set_gs)(%rip), %rax
763 cmp %rax, 8+ISF64_RIP(%rsp)
764 je L_32bit_fault_set_seg
770 * Here after taking an unexpected trap from kernel mode - perhaps
771 * while running in the trampolines hereabouts.
772 * Note: %rax has been pushed on stack.
773 * Make sure we're not on the PCB stack, if so move to the kernel stack.
774 * This is likely a fatal condition.
775 * But first, ensure we have the kernel gs base active...
779 mov $(MSR_IA32_GS_BASE), %ecx
780 rdmsr /* read kernel gsbase */
781 test $0x80000000, %edx /* test MSB of address */
788 movq %gs:CPU_UBER_ISF, %rax /* PCB stack addr */
790 cmpq $(PAGE_SIZE), %rax /* current stack in PCB? */
791 jb 2f /* - yes, deal with it */
792 pop %rax /* - no, restore %rax */
793 jmp L_dispatch_kernel
796 * Here if %rsp is in the PCB
797 * Copy the interrupt stack frame from PCB stack to kernel stack
799 movq %gs:CPU_KERNEL_STACK, %rax
801 pushq 8+ISF64_SS(%rax)
802 pushq 8+ISF64_RSP(%rax)
803 pushq 8+ISF64_RFLAGS(%rax)
804 pushq 8+ISF64_CS(%rax)
805 pushq 8+ISF64_RIP(%rax)
806 pushq 8+ISF64_ERR(%rax)
807 pushq 8+ISF64_TRAPFN(%rax)
808 pushq 8+ISF64_TRAPNO(%rax)
810 jmp L_dispatch_kernel
814 * GP/NP fault on IRET: CS or SS is in error.
815 * User GSBASE is active.
816 * On IST1 stack containing:
817 * (rax saved above, which is immediately popped)
818 * 0 ISF64_TRAPNO: trap code (NP or GP)
819 * 8 ISF64_TRAPFN: trap function
820 * 16 ISF64_ERR: segment number in error (error code)
821 * 24 ISF64_RIP: kernel RIP
822 * 32 ISF64_CS: kernel CS
823 * 40 ISF64_RFLAGS: kernel RFLAGS
824 * 48 ISF64_RSP: kernel RSP
825 * 56 ISF64_SS: kernel SS
826 * On the PCB stack, pointed to by the kernel's RSP is:
833 * We need to move the kernel's TRAPNO, TRAPFN and ERR to the PCB and handle
834 * as a user fault with:
835 * 0 ISF64_TRAPNO: trap code (NP or GP)
836 * 8 ISF64_TRAPFN: trap function
837 * 16 ISF64_ERR: segment number in error (error code)
845 pop %rax /* recover saved %rax */
846 mov %rax, ISF64_RIP(%rsp) /* save rax (we don`t need saved rip) */
847 mov ISF64_RSP(%rsp), %rax
848 xchg %rax, %rsp /* switch to PCB stack */
850 push ISF64_TRAPFN(%rax)
851 push ISF64_TRAPNO(%rax)
852 mov ISF64_RIP(%rax), %rax /* restore rax */
853 /* now treat as fault from user */
857 * Fault restoring a segment register. All of the saved state is still
858 * on the stack untouched since we haven't yet moved the stack pointer.
859 * On IST1 stack containing:
860 * (rax saved above, which is immediately popped)
861 * 0 ISF64_TRAPNO: trap code (NP or GP)
862 * 8 ISF64_TRAPFN: trap function
863 * 16 ISF64_ERR: segment number in error (error code)
864 * 24 ISF64_RIP: kernel RIP
865 * 32 ISF64_CS: kernel CS
866 * 40 ISF64_RFLAGS: kernel RFLAGS
867 * 48 ISF64_RSP: kernel RSP
868 * 56 ISF64_SS: kernel SS
869 * On the PCB stack, pointed to by the kernel's RSP is:
871 * 8 user trap function
879 L_32bit_fault_set_seg:
881 pop %rax /* toss saved %rax from stack */
882 mov ISF64_TRAPNO(%rsp), %rax
883 mov ISF64_TRAPFN(%rsp), %rcx
884 mov ISF64_ERR(%rsp), %rdx
885 mov ISF64_RSP(%rsp), %rsp /* reset stack to saved state */
886 mov %rax,R64_TRAPNO(%rsp)
887 mov %rcx,R64_TRAPFN(%rsp)
888 mov %rdx,R64_ERR(%rsp)
889 /* now treat as fault from user */
890 /* except that all the state is */
891 /* already saved - we just have to */
892 /* move the trapno and error into */
893 /* the compatibility frame */
894 jmp L_dispatch_U32_after_fault
897 * Fatal exception handlers:
899 Entry(idt64_db_task_dbl_fault)
900 PUSH_FUNCTION(HNDL_DOUBLE_FAULT)
901 pushq $(T_DOUBLE_FAULT)
904 Entry(idt64_db_task_stk_fault)
905 PUSH_FUNCTION(HNDL_DOUBLE_FAULT)
906 pushq $(T_STACK_FAULT)
910 push $(0) /* Error */
911 PUSH_FUNCTION(HNDL_MACHINE_CHECK)
912 pushq $(T_MACHINE_CHECK)
917 * This may or may not be fatal but extreme care is required
918 * because it may fall when control was already in another trampoline.
920 * We get here on IST2 stack which is used for NMIs only.
921 * We must be aware of the interrupted state:
922 * - from user-space, we
923 * - copy state to the PCB and continue;
924 * - from kernel-space, we
925 * - copy state to the kernel stack and continue, but
926 * - check what GSBASE was active, set the kernel base and
927 * - ensure that the active state is restored when the NMI is dismissed.
930 push %rax /* save RAX to ISF64_ERR */
931 push %rcx /* save RCX to ISF64_TRAPFN */
932 push %rdx /* save RDX to ISF64_TRAPNO */
933 testb $3, ISF64_CS(%rsp) /* NMI from user-space? */
936 /* From user-space: copy interrupt state to user PCB */
938 mov %gs:CPU_UBER_ISF, %rcx /* PCB stack addr */
939 add $(ISF64_SIZE), %rcx /* adjust to base of ISF */
940 swapgs /* swap back for L_dispatch */
941 jmp 4f /* Copy state to PCB */
946 * Determine whether the kernel or user GS is set.
947 * Set the kernel and ensure that we'll swap back correctly at IRET.
949 mov $(MSR_IA32_GS_BASE), %ecx
950 rdmsr /* read kernel gsbase */
951 test $0x80000000, %edx /* test MSB of address */
954 movl $1, ISF64_CS+4(%rsp) /* and set flag in CS slot */
957 * Determine whether we're on the kernel or interrupt stack
960 mov ISF64_RSP(%rsp), %rcx
961 mov %gs:CPU_KERNEL_STACK, %rax
963 and EXT(kernel_stack_mask)(%rip), %rax
964 test %rax, %rax /* are we on the kernel stack? */
967 mov %gs:CPU_INT_STACK_TOP, %rax
968 dec %rax /* intr stack top is byte above max */
970 and EXT(kernel_stack_mask)(%rip), %rax
971 test %rax, %rax /* are we on the interrupt stack? */
974 mov %gs:CPU_KERNEL_STACK, %rcx
976 /* 16-byte-align kernel/interrupt stack for state push */
977 and $0xFFFFFFFFFFFFFFF0, %rcx
981 * Copy state from NMI stack (RSP) to the save area (RCX) which is
982 * the PCB for user or kernel/interrupt stack from kernel.
983 * ISF64_ERR(RSP) saved RAX
984 * ISF64_TRAPFN(RSP) saved RCX
985 * ISF64_TRAPNO(RSP) saved RDX
987 xchg %rsp, %rcx /* set for pushes */
990 push ISF64_RFLAGS(%rcx)
993 push $(0) /* error code 0 */
994 lea HNDL_ALLINTRS(%rip), %rax
995 push %rax /* trapfn allintrs */
996 push $(T_NMI) /* trapno T_NMI */
997 mov ISF64_ERR(%rcx), %rax
998 mov ISF64_TRAPNO(%rcx), %rdx
999 mov ISF64_TRAPFN(%rcx), %rcx
1003 /* All 'exceptions' enter hndl_alltraps, with:
1004 * r15 x86_saved_state_t address
1005 * rsp kernel stack if user-space, otherwise interrupt or kernel stack
1008 * The rest of the state is set up as:
1009 * both rsp and r15 are 16-byte aligned
1010 * interrupts disabled
1011 * direction flag cleared
1013 Entry(hndl_alltraps)
1020 /* Check for active vtimers in the current task */
1021 mov %gs:CPU_ACTIVE_THREAD, %rcx
1022 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling trap/exception */
1023 mov TH_TASK(%rcx), %rbx
1024 TASK_VTIMER_CHECK(%rbx, %rcx)
1026 CCALL1(user_trap, %r15) /* call user trap routine */
1027 /* user_trap() unmasks interrupts */
1028 cli /* hold off intrs - critical section */
1029 xorl %ecx, %ecx /* don't check if we're in the PFZ */
1034 Entry(return_from_trap)
1035 movq %gs:CPU_ACTIVE_THREAD,%r15 /* Get current thread */
1036 movl $-1, TH_IOTIER_OVERRIDE(%r15) /* Reset IO tier override to -1 before returning to userspace */
1037 cmpl $0, TH_RWLOCK_COUNT(%r15) /* Check if current thread has pending RW locks held */
1039 xorq %rbp, %rbp /* clear framepointer */
1040 mov %r15, %rdi /* Set RDI to current thread */
1041 CCALL(lck_rw_clear_promotions_x86) /* Clear promotions if needed */
1043 movq TH_PCB_ISS(%r15), %r15 /* PCB stack */
1044 movl %gs:CPU_PENDING_AST,%eax
1046 je EXT(return_to_user) /* branch if no AST */
1048 L_return_from_trap_with_ast:
1049 testl %ecx, %ecx /* see if we need to check for an EIP in the PFZ */
1050 je 2f /* no, go handle the AST */
1051 cmpl $(SS_64), SS_FLAVOR(%r15) /* are we a 64-bit task? */
1053 /* no... 32-bit user mode */
1054 movl R32_EIP(%r15), %edi
1055 xorq %rbp, %rbp /* clear framepointer */
1056 CCALL(commpage_is_in_pfz32)
1058 je 2f /* not in the PFZ... go service AST */
1059 movl %eax, R32_EBX(%r15) /* let the PFZ know we've pended an AST */
1060 jmp EXT(return_to_user)
1062 movq R64_RIP(%r15), %rdi
1063 xorq %rbp, %rbp /* clear framepointer */
1064 CCALL(commpage_is_in_pfz64)
1066 je 2f /* not in the PFZ... go service AST */
1067 movl %eax, R64_RBX(%r15) /* let the PFZ know we've pended an AST */
1068 jmp EXT(return_to_user)
1070 STI /* interrupts always enabled on return to user mode */
1072 xor %edi, %edi /* zero %rdi */
1073 xorq %rbp, %rbp /* clear framepointer */
1074 CCALL(i386_astintr) /* take the AST */
1077 mov %rsp, %r15 /* AST changes stack, saved state */
1078 xorl %ecx, %ecx /* don't check if we're in the PFZ */
1079 jmp EXT(return_from_trap) /* and check again (rare) */
1082 * Trap from kernel mode. No need to switch stacks.
1083 * Interrupts must be off here - we will set them to state at time of trap
1084 * as soon as it's safe for us to do so and not recurse doing preemption
1088 movq %r15, %rdi /* saved state addr */
1089 pushq R64_RIP(%r15) /* Simulate a CALL from fault point */
1090 pushq %rbp /* Extend framepointer chain */
1092 CCALLWITHSP(kernel_trap) /* to kernel trap routine */
1095 mov %rsp, %r15 /* DTrace slides stack/saved-state */
1098 movl %gs:CPU_PENDING_AST,%eax /* get pending asts */
1099 testl $(AST_URGENT),%eax /* any urgent preemption? */
1100 je ret_to_kernel /* no, nothing to do */
1101 cmpl $(T_PREEMPT),R64_TRAPNO(%r15)
1102 je ret_to_kernel /* T_PREEMPT handled in kernel_trap() */
1103 testl $(EFL_IF),R64_RFLAGS(%r15) /* interrupts disabled? */
1105 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
1107 movq %gs:CPU_KERNEL_STACK,%rax
1110 andq EXT(kernel_stack_mask)(%rip),%rcx
1111 testq %rcx,%rcx /* are we on the kernel stack? */
1112 jne ret_to_kernel /* no, skip it */
1114 CCALL1(i386_astintr, $1) /* take the AST */
1116 mov %rsp, %r15 /* AST changes stack, saved state */
1121 * All interrupts on all tasks enter here with:
1122 * r15 x86_saved_state_t
1123 * rsp kernel or interrupt stack
1126 * both rsp and r15 are 16-byte aligned
1127 * interrupts disabled
1128 * direction flag cleared
1130 Entry(hndl_allintrs)
1132 * test whether already on interrupt stack
1134 movq %gs:CPU_INT_STACK_TOP,%rcx
1137 leaq -INTSTACK_SIZE(%rcx),%rdx
1139 jb int_from_intstack
1141 xchgq %rcx,%rsp /* switch to interrupt stack */
1143 mov %cr0,%rax /* get cr0 */
1144 orl $(CR0_TS),%eax /* or in TS bit */
1145 mov %rax,%cr0 /* set cr0 */
1147 pushq %rcx /* save pointer to old stack */
1148 pushq %gs:CPU_INT_STATE /* save previous intr state */
1149 movq %r15,%gs:CPU_INT_STATE /* set intr state */
1151 TIME_INT_ENTRY /* do timing */
1153 /* Check for active vtimers in the current task */
1154 mov %gs:CPU_ACTIVE_THREAD, %rcx
1155 mov TH_TASK(%rcx), %rbx
1156 TASK_VTIMER_CHECK(%rbx, %rcx)
1158 incl %gs:CPU_PREEMPTION_LEVEL
1159 incl %gs:CPU_INTERRUPT_LEVEL
1161 CCALL1(interrupt, %r15) /* call generic interrupt routine */
1163 cli /* just in case we returned with intrs enabled */
1165 .globl EXT(return_to_iret)
1166 LEXT(return_to_iret) /* (label for kdb_kintr and hardclock) */
1168 decl %gs:CPU_INTERRUPT_LEVEL
1169 decl %gs:CPU_PREEMPTION_LEVEL
1171 TIME_INT_EXIT /* do timing */
1173 popq %gs:CPU_INT_STATE /* reset/clear intr state pointer */
1174 popq %rsp /* switch back to old stack */
1176 movq %gs:CPU_ACTIVE_THREAD,%rax
1177 movq TH_PCB_FPS(%rax),%rax /* get pcb's ifps */
1178 cmpq $0,%rax /* Is there a context */
1179 je 1f /* Branch if not */
1180 movl FP_VALID(%rax),%eax /* Load fp_valid */
1181 cmpl $0,%eax /* Check if valid */
1182 jne 1f /* Branch if valid */
1186 mov %cr0,%rax /* get cr0 */
1187 orl $(CR0_TS),%eax /* or in TS bit */
1188 mov %rax,%cr0 /* set cr0 */
1190 /* Load interrupted code segment into %eax */
1191 movl R32_CS(%r15),%eax /* assume 32-bit state */
1192 cmpl $(SS_64),SS_FLAVOR(%r15)/* 64-bit? */
1195 movl R64_CS(%r15),%eax /* 64-bit user mode */
1198 cmpl $(SS_32),SS_FLAVOR(%r15)
1201 CCALL1(panic_idt64, %r15)
1205 movl R64_CS(%r15),%eax /* 64-bit user mode */
1208 testb $3,%al /* user mode, */
1209 jnz ast_from_interrupt_user /* go handle potential ASTs */
1211 * we only want to handle preemption requests if
1212 * the interrupt fell in the kernel context
1213 * and preemption isn't disabled
1215 movl %gs:CPU_PENDING_AST,%eax
1216 testl $(AST_URGENT),%eax /* any urgent requests? */
1217 je ret_to_kernel /* no, nothing to do */
1219 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
1220 jne ret_to_kernel /* yes, skip it */
1223 * Take an AST from kernel space. We don't need (and don't want)
1224 * to do as much as the case where the interrupt came from user
1227 CCALL1(i386_astintr, $1)
1229 mov %rsp, %r15 /* AST changes stack, saved state */
1234 * nested int - simple path, can't preempt etc on way out
1237 incl %gs:CPU_PREEMPTION_LEVEL
1238 incl %gs:CPU_INTERRUPT_LEVEL
1239 incl %gs:CPU_NESTED_ISTACK
1241 push %gs:CPU_INT_STATE
1242 mov %r15, %gs:CPU_INT_STATE
1244 CCALL1(interrupt, %r15)
1246 pop %gs:CPU_INT_STATE
1248 decl %gs:CPU_INTERRUPT_LEVEL
1249 decl %gs:CPU_PREEMPTION_LEVEL
1250 decl %gs:CPU_NESTED_ISTACK
1255 * Take an AST from an interrupted user
1257 ast_from_interrupt_user:
1258 movl %gs:CPU_PENDING_AST,%eax
1259 testl %eax,%eax /* pending ASTs? */
1260 je EXT(ret_to_user) /* no, nothing to do */
1264 movl $1, %ecx /* check if we're in the PFZ */
1265 jmp L_return_from_trap_with_ast /* return */
1268 /* Syscall dispatch routines! */
1273 * System call entries via INTR_GATE or sysenter:
1275 * r15 x86_saved_state32_t
1278 * both rsp and r15 are 16-byte aligned
1279 * interrupts disabled
1280 * direction flag cleared
1283 Entry(hndl_sysenter)
1285 * We can be here either for a mach syscall or a unix syscall,
1286 * as indicated by the sign of the code:
1288 movl R32_EAX(%r15),%eax
1290 js EXT(hndl_mach_scall) /* < 0 => mach */
1293 Entry(hndl_unix_scall)
1297 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1298 movq TH_TASK(%rcx),%rbx /* point to current task */
1299 incl TH_SYSCALLS_UNIX(%rcx) /* increment call count */
1301 /* Check for active vtimers in the current task */
1302 TASK_VTIMER_CHECK(%rbx,%rcx)
1306 CCALL1(unix_syscall, %r15)
1308 * always returns through thread_exception_return
1312 Entry(hndl_mach_scall)
1315 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1316 movq TH_TASK(%rcx),%rbx /* point to current task */
1317 incl TH_SYSCALLS_MACH(%rcx) /* increment call count */
1319 /* Check for active vtimers in the current task */
1320 TASK_VTIMER_CHECK(%rbx,%rcx)
1324 CCALL1(mach_call_munger, %r15)
1326 * always returns through thread_exception_return
1330 Entry(hndl_mdep_scall)
1333 /* Check for active vtimers in the current task */
1334 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1335 movq TH_TASK(%rcx),%rbx /* point to current task */
1336 TASK_VTIMER_CHECK(%rbx,%rcx)
1340 CCALL1(machdep_syscall, %r15)
1342 * always returns through thread_exception_return
1347 * System call entries via syscall only:
1349 * r15 x86_saved_state64_t
1352 * both rsp and r15 are 16-byte aligned
1353 * interrupts disabled
1354 * direction flag cleared
1360 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1361 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling syscall */
1362 movq TH_TASK(%rcx),%rbx /* point to current task */
1364 /* Check for active vtimers in the current task */
1365 TASK_VTIMER_CHECK(%rbx,%rcx)
1368 * We can be here either for a mach, unix machdep or diag syscall,
1369 * as indicated by the syscall class:
1371 movl R64_RAX(%r15), %eax /* syscall number/class */
1373 andl $(SYSCALL_CLASS_MASK), %edx /* syscall class */
1374 cmpl $(SYSCALL_CLASS_MACH<<SYSCALL_CLASS_SHIFT), %edx
1375 je EXT(hndl_mach_scall64)
1376 cmpl $(SYSCALL_CLASS_UNIX<<SYSCALL_CLASS_SHIFT), %edx
1377 je EXT(hndl_unix_scall64)
1378 cmpl $(SYSCALL_CLASS_MDEP<<SYSCALL_CLASS_SHIFT), %edx
1379 je EXT(hndl_mdep_scall64)
1380 cmpl $(SYSCALL_CLASS_DIAG<<SYSCALL_CLASS_SHIFT), %edx
1381 je EXT(hndl_diag_scall64)
1383 /* Syscall class unknown */
1385 CCALL3(i386_exception, $(EXC_SYSCALL), %rax, $1)
1389 Entry(hndl_unix_scall64)
1390 incl TH_SYSCALLS_UNIX(%rcx) /* increment call count */
1393 CCALL1(unix_syscall64, %r15)
1395 * always returns through thread_exception_return
1399 Entry(hndl_mach_scall64)
1400 incl TH_SYSCALLS_MACH(%rcx) /* increment call count */
1403 CCALL1(mach_call_munger64, %r15)
1405 * always returns through thread_exception_return
1410 Entry(hndl_mdep_scall64)
1413 CCALL1(machdep_syscall64, %r15)
1415 * always returns through thread_exception_return
1418 Entry(hndl_diag_scall64)
1419 CCALL1(diagCall64, %r15) // Call diagnostics
1420 cli // Disable interruptions just in case
1421 test %eax, %eax // What kind of return is this?
1422 je 1f // - branch if bad (zero)
1423 jmp EXT(return_to_user) // Normal return, do not check asts...
1426 CCALL3(i386_exception, $EXC_SYSCALL, $0x6000, $1)
1429 Entry(hndl_machine_check)
1430 CCALL1(panic_machine_check64, %r15)
1433 Entry(hndl_double_fault)
1434 CCALL1(panic_double_fault64, %r15)