2 * Copyright (c) 2010-2019 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
70 * Indices of handlers for each exception type.
72 #define HNDL_ALLINTRS 0
73 #define HNDL_ALLTRAPS 1
74 #define HNDL_SYSENTER 2
75 #define HNDL_SYSCALL 3
76 #define HNDL_UNIX_SCALL 4
77 #define HNDL_MACH_SCALL 5
78 #define HNDL_MDEP_SCALL 6
79 #define HNDL_DOUBLE_FAULT 7
80 #define HNDL_MACHINE_CHECK 8
82 /* Begin double-mapped descriptor section */
84 .section __HIB, __desc
85 .globl EXT(idt64_hndl_table0)
86 EXT(idt64_hndl_table0):
87 /* 0x00 */ .quad EXT(ks_dispatch)
88 /* 0x08 */ .quad EXT(ks_64bit_return)
89 /* 0x10 */ .quad 0 /* Populated with CPU shadow displacement*/
90 /* 0x18 */ .quad EXT(ks_32bit_return)
91 #define TBL0_OFF_DISP_USER_WITH_POPRAX 0x20
92 /* 0x20 */ .quad EXT(ks_dispatch_user_with_pop_rax)
93 #define TBL0_OFF_DISP_KERN_WITH_POPRAX 0x28
94 /* 0x28 */ .quad EXT(ks_dispatch_kernel_with_pop_rax)
95 #define TBL0_OFF_PTR_KERNEL_STACK_MASK 0x30
96 /* 0x30 */ .quad 0 /* &kernel_stack_mask */
98 EXT(idt64_hndl_table1):
99 .quad EXT(hndl_allintrs)
100 .quad EXT(hndl_alltraps)
101 .quad EXT(hndl_sysenter)
102 .quad EXT(hndl_syscall)
103 .quad EXT(hndl_unix_scall)
104 .quad EXT(hndl_mach_scall)
105 .quad EXT(hndl_mdep_scall)
106 .quad EXT(hndl_double_fault)
107 .quad EXT(hndl_machine_check)
111 /* The wrapper for all non-special traps/interrupts */
112 /* Everything up to PUSH_FUNCTION is just to output
113 * the interrupt number out to the postcode display
116 #define IDT_ENTRY_WRAPPER(n, f) \
118 POSTCODE2(0x6400+n) ;\
124 #define IDT_ENTRY_WRAPPER(n, f) \
130 /* A trap that comes with an error code already on the stack */
131 #define TRAP_ERR(n, f) \
133 IDT_ENTRY_WRAPPER(n, HNDL_ALLTRAPS)
139 IDT_ENTRY_WRAPPER(n, HNDL_ALLTRAPS)
141 #define USER_TRAP TRAP
144 #define INTERRUPT(n) \
145 Entry(_intr_ ## n) ;\
147 IDT_ENTRY_WRAPPER(n, HNDL_ALLINTRS)
149 /* A trap with a special-case handler, hence we don't need to define anything */
150 #define TRAP_SPC(n, f)
151 #define TRAP_IST1(n, f)
152 #define TRAP_IST2(n, f)
153 #define USER_TRAP_SPC(n, f)
155 /* Begin double-mapped text section */
156 .section __HIB, __text
157 /* Generate all the stubs */
158 #include "idt_table.h"
160 Entry(idt64_page_fault)
161 pushq $(HNDL_ALLTRAPS)
166 * #DB handler, which runs on IST1, will treat as spurious any #DB received while executing in the
167 * kernel while not on the kernel's gsbase.
170 /* Synthesize common interrupt stack frame */
171 push $0 /* error code */
172 pushq $(HNDL_ALLTRAPS)
174 /* Spill prior to RDMSR */
178 mov $(MSR_IA32_GS_BASE), %ecx
179 rdmsr /* Check contents of GSBASE MSR */
180 test $0x80000000, %edx /* MSB set? Already swapped to kernel's */
184 * If we're not already swapped to the kernel's gsbase AND this #DB originated from kernel space,
185 * it must have happened within the very small window on entry or exit before or after (respectively)
186 * swapgs occurred. In those cases, consider the #DB spurious and immediately return.
188 testb $3, 8+8+8+ISF64_CS(%rsp)
193 addq $0x18, %rsp /* Remove synthesized interrupt stack frame */
196 swapgs /* direct from user */
200 leaq EXT(idt64_hndl_table0)(%rip), %rax
201 mov 16(%rax), %rax /* Offset of per-CPU shadow */
203 mov %gs:CPU_SHADOWTASK_CR3(%rax), %rax
208 /* Note that %rax will be popped from the stack in ks_dispatch, below */
210 leaq EXT(idt64_hndl_table0)(%rip), %rax
214 * Legacy interrupt gate System call handlers.
215 * These are entered via a syscall interrupt. The system call number in %rax
216 * is saved to the error code slot in the stack frame. We then branch to the
217 * common state saving code.
221 #error NO UNIX INT!!!
223 Entry(idt64_unix_scall)
224 pushq %rax /* save system call number */
225 pushq $(HNDL_UNIX_SCALL)
227 jmp L_u64bit_entry_check
229 Entry(idt64_mach_scall)
230 pushq %rax /* save system call number */
231 pushq $(HNDL_MACH_SCALL)
233 jmp L_u64bit_entry_check
235 Entry(idt64_mdep_scall)
236 pushq %rax /* save system call number */
237 pushq $(HNDL_MDEP_SCALL)
239 jmp L_u64bit_entry_check
242 * For GP/NP/SS faults, we use the IST1 stack.
243 * For faults from user-space, we have to copy the machine state to the
244 * PCB stack and then dispatch as normal.
245 * For faults in kernel-space, we need to scrub for kernel exit faults and
246 * treat these as user-space faults. But for all other kernel-space faults
247 * we continue to run on the IST1 stack as we dispatch to handle the fault
251 pushq $(HNDL_ALLTRAPS)
252 pushq $(T_SEGMENT_NOT_PRESENT)
253 jmp L_check_for_kern_flt
255 Entry(idt64_gen_prot)
256 pushq $(HNDL_ALLTRAPS)
257 pushq $(T_GENERAL_PROTECTION)
258 jmp L_check_for_kern_flt
260 Entry(idt64_stack_fault)
261 pushq $(HNDL_ALLTRAPS)
262 pushq $(T_STACK_FAULT)
263 jmp L_check_for_kern_flt
265 L_check_for_kern_flt:
267 * If we took a #GP or #SS from the kernel, check if we took them
268 * from either ret32_iret or ret64_iret. If we did, we need to
269 * jump into L_dispatch at the swapgs so that the code in L_dispatch
270 * can proceed with the correct GSbase.
273 testb $3, 8+ISF64_CS(%rsp)
274 jnz L_dispatch_from_user_no_push_rax /* Fault from user, go straight to dispatch */
276 /* Check if the fault occurred in the 32-bit segment restoration window (which executes with user gsb) */
277 leaq L_32bit_seg_restore_begin(%rip), %rax
278 cmpq %rax, 8+ISF64_RIP(%rsp)
279 jb L_not_32bit_segrestores
280 leaq L_32bit_seg_restore_done(%rip), %rax
281 cmpq %rax, 8+ISF64_RIP(%rsp)
282 jae L_not_32bit_segrestores
284 L_not_32bit_segrestores:
285 leaq EXT(ret32_iret)(%rip), %rax
286 cmpq %rax, 8+ISF64_RIP(%rsp)
288 leaq EXT(ret64_iret)(%rip), %rax
289 cmpq %rax, 8+ISF64_RIP(%rsp)
291 jmp L_dispatch_from_kernel_no_push_rax
293 * We hit the fault on iretq, so check the original return %cs. If
294 * it's a user %cs, fixup the stack and then jump to dispatch..
296 * With this type of fault, the stack is layed-out as follows:
299 * orig %ss saved_rsp+32
300 * orig %rsp saved_rsp+24
301 * orig %rflags saved_rsp+16
302 * orig %cs saved_rsp+8
303 * orig %rip saved_rsp
304 * ^^^^^^^^^ (maybe on another stack, since we switched to IST1)
313 * <saved %rax> <== %rsp -72
317 movq 16+ISF64_RSP(%rsp), %rbx
318 movq ISF64_CS-24(%rbx), %rax
319 testb $3, %al /* If the original return destination was to user */
322 jmp L_dispatch_from_kernel_no_push_rax /* Fault occurred when trying to return to kernel */
325 * Fix the stack so the original trap frame is current, then jump to dispatch
328 movq %rax, 16+ISF64_CS(%rsp)
330 movq ISF64_RSP-24(%rbx), %rax
331 movq %rax, 16+ISF64_RSP(%rsp)
333 movq ISF64_RIP-24(%rbx), %rax
334 movq %rax, 16+ISF64_RIP(%rsp)
336 movq ISF64_SS-24(%rbx), %rax
337 movq %rax, 16+ISF64_SS(%rsp)
339 movq ISF64_RFLAGS-24(%rbx), %rax
340 movq %rax, 16+ISF64_RFLAGS(%rsp)
343 jmp L_dispatch_from_user_no_push_rax
347 * Fatal exception handlers:
349 Entry(idt64_db_task_dbl_fault)
350 pushq $(HNDL_DOUBLE_FAULT)
351 pushq $(T_DOUBLE_FAULT)
354 Entry(idt64_db_task_stk_fault)
355 pushq $(HNDL_DOUBLE_FAULT)
356 pushq $(T_STACK_FAULT)
360 push $(0) /* Error */
361 pushq $(HNDL_MACHINE_CHECK)
362 pushq $(T_MACHINE_CHECK)
367 * This may or may not be fatal but extreme care is required
368 * because it may fall when control was already in another trampoline.
370 * We get here on IST2 stack which is used exclusively for NMIs.
371 * Machine checks, doublefaults and similar use IST1
377 testb $3, ISF64_CS(%rsp)
380 /* From user-space: copy interrupt state to user PCB */
383 leaq EXT(idt64_hndl_table0)(%rip), %rax
384 mov 16(%rax), %rax /* Offset of per-CPU shadow */
385 mov %gs:CPU_SHADOWTASK_CR3(%rax), %rax
386 mov %rax, %cr3 /* note that SMAP is enabled in L_common_dispatch (on Broadwell+) */
388 mov %gs:CPU_UBER_ISF, %rcx /* PCB stack addr */
389 add $(ISF64_SIZE), %rcx /* adjust to base of ISF */
391 leaq TBL0_OFF_DISP_USER_WITH_POPRAX+EXT(idt64_hndl_table0)(%rip), %rax /* ks_dispatch_user_with_pop_rax */
392 jmp 4f /* Copy state to PCB */
397 * Determine whether the kernel or user GS is set.
398 * Sets the high 32 bits of the return CS to 1 to ensure that we'll swapgs back correctly at IRET.
400 mov $(MSR_IA32_GS_BASE), %ecx
401 rdmsr /* read kernel gsbase */
402 test $0x80000000, %edx /* test MSB of address */
405 movl $1, ISF64_CS+4(%rsp) /* and set flag in CS slot */
408 leaq EXT(idt64_hndl_table0)(%rip), %rax
409 mov 16(%rax), %rax /* Offset of per-CPU shadow */
411 mov %gs:CPU_SHADOWTASK_CR3(%rax), %rax
412 mov %rax, %cr3 /* Unconditionally switch to primary kernel pagetables */
415 * Determine whether we're on the kernel or interrupt stack
418 mov ISF64_RSP(%rsp), %rcx
419 mov %gs:CPU_KERNEL_STACK, %rax
421 movq TBL0_OFF_PTR_KERNEL_STACK_MASK+EXT(idt64_hndl_table0)(%rip), %rdx
422 mov (%rdx), %rdx /* Load kernel_stack_mask */
424 test %rax, %rax /* are we on the kernel stack? */
427 mov %gs:CPU_INT_STACK_TOP, %rax
428 cmp %rcx, %rax /* are we on the interrupt stack? */
430 leaq -INTSTACK_SIZE(%rax), %rax
434 mov %gs:CPU_KERNEL_STACK, %rcx
436 /* 16-byte-align kernel/interrupt stack for state push */
437 and $0xFFFFFFFFFFFFFFF0, %rcx
439 leaq TBL0_OFF_DISP_KERN_WITH_POPRAX+EXT(idt64_hndl_table0)(%rip), %rax /* ks_dispatch_kernel_with_pop_rax */
442 * Copy state from NMI stack (RSP) to the save area (RCX) which is
443 * the PCB for user or kernel/interrupt stack from kernel.
444 * ISF64_ERR(RSP) saved RAX
445 * ISF64_TRAPFN(RSP) saved RCX
446 * ISF64_TRAPNO(RSP) saved RDX
448 xchg %rsp, %rcx /* set for pushes */
451 push ISF64_RFLAGS(%rcx)
454 /* Synthesize common interrupt stack frame */
455 push $(0) /* error code 0 */
456 push $(HNDL_ALLINTRS) /* trapfn allintrs */
457 push $(T_NMI) /* trapno T_NMI */
458 push ISF64_ERR(%rcx) /* saved %rax is popped in ks_dispatch_{kernel|user}_with_pop_rax */
459 mov ISF64_TRAPNO(%rcx), %rdx
460 mov ISF64_TRAPFN(%rcx), %rcx
462 jmp *(%rax) /* ks_dispatch_{kernel|user}_with_pop_rax */
464 Entry(idt64_double_fault)
465 pushq $(HNDL_DOUBLE_FAULT)
466 pushq $(T_DOUBLE_FAULT)
472 /* Use RAX as a temporary by shifting its contents into R11[32:63]
473 * The systemcall number is defined to be a 32-bit quantity, as is
478 .globl EXT(dblsyscall_patch_point)
479 EXT(dblsyscall_patch_point):
480 // movabsq $0x12345678ABCDEFFFULL, %rax
481 /* Generate offset to the double-mapped per-CPU data shadow
484 leaq EXT(idt64_hndl_table0)(%rip), %rax
486 mov %rsp, %gs:CPU_UBER_TMP(%rax) /* save user stack */
487 mov %gs:CPU_ESTACK(%rax), %rsp /* switch stack to per-cpu estack */
488 sub $(ISF64_SIZE), %rsp
491 * Synthesize an ISF frame on the exception stack
493 movl $(USER_DS), ISF64_SS(%rsp)
494 mov %rcx, ISF64_RIP(%rsp) /* rip */
496 mov %gs:CPU_UBER_TMP(%rax), %rcx
497 mov %rcx, ISF64_RSP(%rsp) /* user stack --changed */
500 shrq $32, %rax /* Restore RAX */
501 mov %r11d, %r11d /* Clear r11[32:63] */
503 mov %r11, ISF64_RFLAGS(%rsp) /* rflags */
504 movl $(SYSCALL_CS), ISF64_CS(%rsp) /* cs - a pseudo-segment */
505 mov %rax, ISF64_ERR(%rsp) /* err/rax - syscall code */
506 movq $(HNDL_SYSCALL), ISF64_TRAPFN(%rsp)
507 movq $(T_SYSCALL), ISF64_TRAPNO(%rsp) /* trapno */
509 jmp L_dispatch /* this can only be 64-bit */
512 Entry(idt64_sysenter)
513 /* Synthesize an interrupt stack frame onto the
516 push $(USER_DS) /* ss */
520 * Clear, among others, the Nested Task (NT) flags bit;
521 * this is zeroed by INT, but not by SYSENTER.
525 push $(SYSENTER_CS) /* cs */
528 push %rax /* err/eax - syscall code */
529 pushq $(HNDL_SYSENTER)
531 orl $(EFL_IF), ISF64_RFLAGS(%rsp)
532 jmp L_u64bit_entry_check
535 * Common dispatch point.
536 * Determine what mode has been interrupted and save state accordingly.
538 * rsp from user-space: interrupt state in PCB, or
539 * from kernel-space: interrupt state in kernel or interrupt stack
540 * GSBASE from user-space: pthread area, or
541 * from kernel-space: cpu_data
546 testb $3, 8+ISF64_CS(%rsp)
548 L_dispatch_from_user_no_push_rax:
550 leaq EXT(idt64_hndl_table0)(%rip), %rax
553 mov %gs:CPU_SHADOWTASK_CR3(%rax), %rax
556 mov %rax, %gs:CPU_ENTRY_CR3
558 L_dispatch_from_kernel_no_push_rax:
560 leaq EXT(idt64_hndl_table0)(%rip), %rax
561 /* The text/data relationship here must be preserved in the doublemap, and the contents must be remapped */
562 /* Indirect branch to non-doublemapped trampolines */
564 /* User return: register restoration and address space switch sequence */
565 Entry(ks_64bit_return)
567 mov R64_R14(%r15), %r14
568 mov R64_R13(%r15), %r13
569 mov R64_R12(%r15), %r12
570 mov R64_R11(%r15), %r11
571 mov R64_R10(%r15), %r10
572 mov R64_R9(%r15), %r9
573 mov R64_R8(%r15), %r8
574 mov R64_RSI(%r15), %rsi
575 mov R64_RDI(%r15), %rdi
576 mov R64_RBP(%r15), %rbp
577 mov R64_RDX(%r15), %rdx
578 mov R64_RCX(%r15), %rcx
579 mov R64_RBX(%r15), %rbx
580 mov R64_RAX(%r15), %rax
581 /* Switch to per-CPU exception stack */
582 mov %gs:CPU_ESTACK, %rsp
584 /* Synthesize interrupt stack frame from PCB savearea to exception stack */
587 push R64_RFLAGS(%r15)
591 cmpq $(KERNEL64_CS), 8(%rsp)
592 jne 1f /* Returning to user (%r15 will be restored after the segment checks) */
593 mov R64_R15(%r15), %r15
594 jmp L_64b_kernel_return /* Returning to kernel */
598 movl %gs:CPU_NEED_SEGCHK, %eax
601 /* Returning to user */
602 cmpl $0, %gs:CPU_CURTASK_HAS_LDT /* If the current task has an LDT, check and restore segment regs */
603 jne L_64b_segops_island
606 * Restore %r15, since we're now done accessing saved state
607 * and (%r15) won't be accessible after the %cr3 load anyway.
608 * Note that %r15 is restored below for the segment-restore
609 * case, just after we no longer need to access register state
612 mov R64_R15(%r15), %r15
615 * Note that this %cr3 sequence is duplicated here to save
616 * [at least] a load and comparison that would be required if
617 * this block were shared.
619 /* Discover user cr3/ASID */
620 mov %gs:CPU_UCR3, %rax
622 mov %rax, %gs:CPU_EXIT_CR3
625 /* Continue execution on the shared/doublemapped trampoline */
629 pop %rax /* Matched to [B], above (segchk required) */
632 * At this point, the stack contains:
640 * | Saved RAX | <-- rsp
643 cmpl $(SYSCALL_CS), 16(%rsp) /* test for exit via SYSRET */
649 pop %rax /* Matched to [A], above */
652 .globl EXT(ret64_iret)
654 iretq /* return from interrupt */
661 pop %rax /* Matched to [A], above */
663 * Here to restore rcx/r11/rsp and perform the sysret back to user-space.
666 * rsp user stack pointer
672 sysretq /* return from system call */
677 pop %rax /* Matched to [A], above */
678 verw 40(%rsp) /* verw operates on the %ss value already on the stack */
684 pop %rax /* Matched to [A], above */
687 * Here to restore rcx/r11/rsp and perform the sysret back to user-space.
690 * rsp user stack pointer
695 verw 8(%rsp) /* verw operates on the %ss value already on the stack */
697 sysretq /* return from system call */
702 /* Validate CS/DS/ES/FS/GS segment selectors with the Load Access Rights instruction prior to restoration */
703 /* Exempt "known good" statically configured selectors, e.g. USER64_CS and 0 */
704 cmpl $(USER64_CS), R64_CS(%r15)
706 larw R64_CS(%r15), %ax
708 /* Ensure that the segment referenced by CS in the saved state is a code segment (bit 11 == 1) */
710 jz L_64_reset_cs /* Update stored %cs with known-good selector if ZF == 1 */
713 movl $(USER64_CS), R64_CS(%r15)
715 cmpl $0, R64_DS(%r15)
717 larw R64_DS(%r15), %ax
719 movl $0, R64_DS(%r15)
721 cmpl $0, R64_ES(%r15)
723 larw R64_ES(%r15), %ax
725 movl $0, R64_ES(%r15)
727 cmpl $0, R64_FS(%r15)
729 larw R64_FS(%r15), %ax
731 movl $0, R64_FS(%r15)
733 cmpl $0, R64_GS(%r15)
735 larw R64_GS(%r15), %ax
737 movl $0, R64_GS(%r15)
740 * Pack the segment registers in %rax since (%r15) will not
741 * be accessible after the %cr3 switch.
742 * Only restore %gs if cthread_self is zero, (indicate
743 * this to the code below with a value of 0xffff)
745 mov %gs:CPU_ACTIVE_THREAD, %rax /* Get the active thread */
746 cmpq $0, TH_CTH_SELF(%rax)
751 movw R64_GS(%r15), %ax
754 movw R64_FS(%r15), %ax
756 movw R64_ES(%r15), %ax
758 movw R64_DS(%r15), %ax
761 * Restore %r15, since we're done accessing saved state
762 * and (%r15) won't be accessible after the %cr3 switch.
764 mov R64_R15(%r15), %r15
766 /* Discover user cr3/ASID */
768 mov %gs:CPU_UCR3, %rax
770 mov %rax, %gs:CPU_EXIT_CR3
773 /* Continue execution on the shared/doublemapped trampoline */
778 * Returning to user; restore segment registers that might be used
779 * by compatibility-mode code in a 64-bit user process.
781 * Note that if we take a fault here, it's OK that we haven't yet
782 * popped %rax from the stack, because %rsp will be reset to
783 * the value pushed onto the exception stack (above).
795 * 0xFFFF is the sentinel set above that indicates we should
796 * not restore %gs (because GS.base was already set elsewhere
797 * (e.g.: in act_machine_set_pcb or machine_thread_set_tsd_base))
801 movw %ax, %gs /* Restore %gs to user-set value */
805 L_u64bit_entry_check:
807 * Check we're not a confused 64-bit user.
811 leaq EXT(idt64_hndl_table0)(%rip), %rax
814 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP(%rax)
815 jne L_64bit_entry_reject
818 L_64bit_entry_reject:
820 * Here for a 64-bit user attempting an invalid kernel entry.
822 movq $(HNDL_ALLTRAPS), 8+ISF64_TRAPFN(%rsp)
823 movq $(T_INVALID_OPCODE), 8+ISF64_TRAPNO(%rsp)
826 Entry(ks_32bit_return)
828 /* Validate CS/DS/ES/FS/GS segment selectors with the Load Access Rights instruction prior to restoration */
829 /* Exempt "known good" statically configured selectors, e.g. USER_CS, USER_DS and 0 */
830 cmpl $(USER_CS), R32_CS(%r15)
832 larw R32_CS(%r15), %ax
834 /* Ensure that the segment referenced by CS in the saved state is a code segment (bit 11 == 1) */
836 jz L_32_reset_cs /* Update stored %cs with known-good selector if ZF == 1 */
839 movl $(USER_CS), R32_CS(%r15)
841 cmpl $(USER_DS), R32_DS(%r15)
843 cmpl $0, R32_DS(%r15)
845 larw R32_DS(%r15), %ax
847 movl $(USER_DS), R32_DS(%r15)
849 cmpl $(USER_DS), R32_ES(%r15)
851 cmpl $0, R32_ES(%r15)
853 larw R32_ES(%r15), %ax
855 movl $(USER_DS), R32_ES(%r15)
857 cmpl $(USER_DS), R32_FS(%r15)
859 cmpl $0, R32_FS(%r15)
861 larw R32_FS(%r15), %ax
863 movl $(USER_DS), R32_FS(%r15)
865 cmpl $(USER_CTHREAD), R32_GS(%r15)
867 cmpl $0, R32_GS(%r15)
869 larw R32_GS(%r15), %ax
871 movl $(USER_CTHREAD), R32_GS(%r15)
875 * Restore general 32-bit registers
877 movl R32_EAX(%r15), %eax
878 movl R32_EBX(%r15), %ebx
879 movl R32_ECX(%r15), %ecx
880 movl R32_EDX(%r15), %edx
881 movl R32_EBP(%r15), %ebp
882 movl R32_ESI(%r15), %esi
883 movl R32_EDI(%r15), %edi
884 movl R32_DS(%r15), %r8d
885 movl R32_ES(%r15), %r9d
886 movl R32_FS(%r15), %r10d
887 movl R32_GS(%r15), %r11d
889 /* Switch to the per-cpu (doublemapped) exception stack */
890 mov %gs:CPU_ESTACK, %rsp
892 /* Now transfer the ISF to the exception stack in preparation for iret, below */
893 movl R32_SS(%r15), %r12d
895 movl R32_UESP(%r15), %r12d
897 movl R32_EFLAGS(%r15), %r12d
899 movl R32_CS(%r15), %r12d
901 movl R32_EIP(%r15), %r12d
904 movl %gs:CPU_NEED_SEGCHK, %r14d /* %r14 will be zeroed just before we return */
907 * Finally, switch to the user pagetables. After this, all %gs-relative
908 * accesses MUST be to cpu shadow data ONLY. Note that after we restore %gs
909 * (after the swapgs), no %gs-relative accesses should be performed.
911 /* Discover user cr3/ASID */
912 mov %gs:CPU_UCR3, %r13
914 mov %r13, %gs:CPU_EXIT_CR3
921 * Restore segment registers. A #GP taken here will push state onto IST1,
922 * not the exception stack. Note that the placement of the labels here
923 * corresponds to the fault address-detection logic (so do not change them
924 * without also changing that code).
926 L_32bit_seg_restore_begin:
931 L_32bit_seg_restore_done:
933 /* Zero 64-bit-exclusive GPRs to prevent data leaks */
943 * At this point, the stack contains:
950 * | Return RIP | <-- rsp
954 cmpl $(SYSENTER_CS), 8(%rsp)
955 /* test for sysexit */
964 .globl EXT(ret32_iret)
966 iretq /* return from interrupt */
977 pop %rdx /* user return eip */
978 pop %rcx /* pop and toss cs */
979 andl $(~EFL_IF), (%rsp) /* clear interrupts enable, sti below */
982 * %ss is now at 16(%rsp)
989 popf /* flags - carry denotes failure */
990 pop %rcx /* user return esp */
993 sti /* interrupts enabled after sysexit */
994 sysexitl /* 32-bit sysexit */
996 /* End of double-mapped TEXT */
1001 cmpl $(KERNEL64_CS), ISF64_CS(%rsp)
1002 je EXT(ks_dispatch_kernel)
1004 mov %rax, %gs:CPU_UBER_TMP
1005 mov %gs:CPU_UBER_ISF, %rax
1006 add $(ISF64_SIZE), %rax
1009 /* Memory to memory moves (aint x86 wonderful):
1010 * Transfer the exception frame from the per-CPU exception stack to the
1011 * 'PCB' stack programmed at cswitch.
1014 push ISF64_RSP(%rax)
1015 push ISF64_RFLAGS(%rax)
1017 push ISF64_RIP(%rax)
1018 push ISF64_ERR(%rax)
1019 push ISF64_TRAPFN(%rax)
1020 push ISF64_TRAPNO(%rax)
1021 mov %gs:CPU_UBER_TMP, %rax
1022 jmp EXT(ks_dispatch_user)
1024 Entry(ks_dispatch_user_with_pop_rax)
1026 jmp EXT(ks_dispatch_user)
1028 Entry(ks_dispatch_user)
1029 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP
1030 je L_dispatch_U32 /* 32-bit user task */
1033 subq $(ISS64_OFFSET), %rsp
1034 mov %r15, R64_R15(%rsp)
1036 mov %gs:CPU_KERNEL_STACK, %rsp
1037 jmp L_dispatch_64bit
1039 Entry(ks_dispatch_kernel_with_pop_rax)
1041 jmp EXT(ks_dispatch_kernel)
1043 Entry(ks_dispatch_kernel)
1044 subq $(ISS64_OFFSET), %rsp
1045 mov %r15, R64_R15(%rsp)
1049 * Here for 64-bit user task or kernel
1052 movl $(SS_64), SS_FLAVOR(%r15)
1055 * Save segment regs if a 64-bit task has
1056 * installed customized segments in the LDT
1058 cmpl $0, %gs:CPU_CURTASK_HAS_LDT
1059 je L_skip_save_extra_segregs
1061 mov %ds, R64_DS(%r15)
1062 mov %es, R64_ES(%r15)
1064 L_skip_save_extra_segregs:
1065 mov %fs, R64_FS(%r15)
1066 mov %gs, R64_GS(%r15)
1069 /* Save general-purpose registers */
1070 mov %rax, R64_RAX(%r15)
1071 mov %rbx, R64_RBX(%r15)
1072 mov %rcx, R64_RCX(%r15)
1073 mov %rdx, R64_RDX(%r15)
1074 mov %rbp, R64_RBP(%r15)
1075 mov %rdi, R64_RDI(%r15)
1076 mov %rsi, R64_RSI(%r15)
1077 mov %r8, R64_R8(%r15)
1078 mov %r9, R64_R9(%r15)
1079 mov %r10, R64_R10(%r15)
1080 mov %r11, R64_R11(%r15)
1081 mov %r12, R64_R12(%r15)
1082 mov %r13, R64_R13(%r15)
1083 mov %r14, R64_R14(%r15)
1085 /* Zero unused GPRs. BX/DX/SI are clobbered elsewhere across the exception handler, and are skipped. */
1096 /* cr2 is significant only for page-faults */
1098 mov %rax, R64_CR2(%r15)
1100 L_dispatch_U64_after_fault:
1101 mov R64_TRAPNO(%r15), %ebx /* %ebx := trapno for later */
1102 mov R64_TRAPFN(%r15), %rdx /* %rdx := trapfn for later */
1103 mov R64_CS(%r15), %esi /* %esi := cs for later */
1105 jmp L_common_dispatch
1107 L_dispatch_U32: /* 32-bit user task */
1108 subq $(ISS64_OFFSET), %rsp
1110 mov %gs:CPU_KERNEL_STACK, %rsp
1111 movl $(SS_32), SS_FLAVOR(%r15)
1116 mov %ds, R32_DS(%r15)
1117 mov %es, R32_ES(%r15)
1118 mov %fs, R32_FS(%r15)
1119 mov %gs, R32_GS(%r15)
1122 * Save general 32-bit registers
1124 mov %eax, R32_EAX(%r15)
1125 mov %ebx, R32_EBX(%r15)
1126 mov %ecx, R32_ECX(%r15)
1127 mov %edx, R32_EDX(%r15)
1128 mov %ebp, R32_EBP(%r15)
1129 mov %esi, R32_ESI(%r15)
1130 mov %edi, R32_EDI(%r15)
1132 /* Unconditionally save cr2; only meaningful on page faults */
1134 mov %eax, R32_CR2(%r15)
1135 /* Zero unused GPRs. BX/DX/SI/R15 are clobbered elsewhere across the exception handler, and are skipped. */
1147 * Copy registers already saved in the machine state
1148 * (in the interrupt stack frame) into the compat save area.
1150 mov R64_RIP(%r15), %eax
1151 mov %eax, R32_EIP(%r15)
1152 mov R64_RFLAGS(%r15), %eax
1153 mov %eax, R32_EFLAGS(%r15)
1154 mov R64_RSP(%r15), %eax
1155 mov %eax, R32_UESP(%r15)
1156 mov R64_SS(%r15), %eax
1157 mov %eax, R32_SS(%r15)
1158 L_dispatch_U32_after_fault:
1159 mov R64_CS(%r15), %esi /* %esi := %cs for later */
1160 mov %esi, R32_CS(%r15)
1161 mov R64_TRAPNO(%r15), %ebx /* %ebx := trapno for later */
1162 mov %ebx, R32_TRAPNO(%r15)
1163 mov R64_ERR(%r15), %eax
1164 mov %eax, R32_ERR(%r15)
1165 mov R64_TRAPFN(%r15), %rdx /* %rdx := trapfn for later */
1168 cld /* Ensure the direction flag is clear in the kernel */
1169 cmpl $0, EXT(pmap_smap_enabled)(%rip)
1171 clac /* Clear EFLAGS.AC if SMAP is present/enabled */
1174 * We mark the kernel's cr3 as "active" for TLB coherency evaluation
1175 * For threads with a mapped pagezero (some WINE games) on non-SMAP platforms,
1176 * we switch to the kernel's address space on entry. Also,
1177 * if the global no_shared_cr3 is TRUE we do switch to the kernel's cr3
1178 * so that illicit accesses to userspace can be trapped.
1180 mov %gs:CPU_KERNEL_CR3, %rcx
1181 mov %rcx, %gs:CPU_ACTIVE_CR3
1182 test $3, %esi /* CS: user/kernel? */
1183 jz 2f /* skip CR3 reload if from kernel */
1185 cmpl $0, %gs:CPU_PAGEZERO_MAPPED
1187 cmpl $0, EXT(no_shared_cr3)(%rip)
1191 movw %gs:CPU_KERNEL_PCID, %ax
1193 mov %rcx, %cr3 /* load kernel cr3 */
1196 /* Deferred processing of pending kernel address space TLB invalidations */
1197 mov %gs:CPU_ACTIVE_CR3+4, %rcx
1201 movl $0, %gs:CPU_TLB_INVALID
1202 cmpb $0, EXT(invpcid_enabled)(%rip)
1205 invpcid %gs:CPU_IP_DESC, %rcx
1208 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Get the active thread */
1211 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling trap */
1212 cmpq $0, TH_PCB_IDS(%rcx) /* Is there a debug register state? */
1215 incl %gs:hwIntCnt(,%ebx,4) // Bump the trap/intr count
1216 /* Dispatch the designated handler */
1217 cmp EXT(dblmap_base)(%rip), %rsp
1219 cmp EXT(dblmap_max)(%rip), %rsp
1221 subq EXT(dblmap_dist)(%rip), %rsp
1222 subq EXT(dblmap_dist)(%rip), %r15
1224 leaq EXT(idt64_hndl_table1)(%rip), %rax
1225 jmp *(%rax, %rdx, 8)
1228 mov %cr4, %rcx /* RMWW CR4, for lack of an alternative*/
1229 and $(~CR4_PGE), %rcx
1235 xor %ecx, %ecx /* If so, reset DR7 (the control) */
1239 * Control is passed here to return to user.
1241 Entry(return_to_user)
1245 mov %gs:CPU_ACTIVE_THREAD, %rdx
1246 cmpq $0, TH_PCB_IDS(%rdx) /* Is there a debug register context? */
1247 jnz L_dr_restore_island
1250 * We now mark the task's address space as active for TLB coherency.
1251 * Handle special cases such as pagezero-less tasks here.
1253 mov %gs:CPU_TASK_CR3, %rcx
1254 mov %rcx, %gs:CPU_ACTIVE_CR3
1255 cmpl $0, %gs:CPU_PAGEZERO_MAPPED
1256 jnz L_cr3_switch_island
1257 movl EXT(no_shared_cr3)(%rip), %eax
1258 test %eax, %eax /* -no_shared_cr3 */
1259 jnz L_cr3_switch_island
1261 L_cr3_switch_return:
1262 mov %gs:CPU_DR7, %rax /* Is there a debug control register?*/
1265 mov %rax, %dr7 /* Set DR7 */
1266 movq $0, %gs:CPU_DR7
1268 cmpl $(SS_64), SS_FLAVOR(%r15) /* 64-bit state? */
1272 * Restore general 64-bit registers.
1273 * Here on fault stack and PCB address in R15.
1275 leaq EXT(idt64_hndl_table0)(%rip), %rax
1281 cmpl $(SS_32), SS_FLAVOR(%r15) /* 32-bit state? */
1285 CCALL1(panic_idt64, %r15)
1287 #endif /* DEBUG_IDT64 */
1289 leaq EXT(idt64_hndl_table0)(%rip), %rax
1293 L_dr_restore_island:
1294 movq TH_PCB_IDS(%rdx),%rax /* Obtain this thread's debug state */
1295 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP /* Are we a 32-bit task? */
1297 movl DS_DR0(%rax), %ecx /* If so, load the 32 bit DRs */
1299 movl DS_DR1(%rax), %ecx
1301 movl DS_DR2(%rax), %ecx
1303 movl DS_DR3(%rax), %ecx
1305 movl DS_DR7(%rax), %ecx
1306 movq %rcx, %gs:CPU_DR7
1309 mov DS64_DR0(%rax), %rcx /* Load the full width DRs*/
1311 mov DS64_DR1(%rax), %rcx
1313 mov DS64_DR2(%rax), %rcx
1315 mov DS64_DR3(%rax), %rcx
1317 mov DS64_DR7(%rax), %rcx
1318 mov %rcx, %gs:CPU_DR7
1320 jmp L_post_dr_restore
1321 L_cr3_switch_island:
1323 movw %gs:CPU_ACTIVE_PCID, %ax
1326 jmp L_cr3_switch_return
1330 cmpl $(SS_64), SS_FLAVOR(%r15) /* 64-bit state? */
1334 CCALL1(panic_idt64, %r15)
1337 cmpl $(KERNEL64_CS), R64_CS(%r15)
1339 CCALL1(panic_idt64, %r15)
1344 * Restore general 64-bit registers.
1345 * Here on fault stack and PCB address in R15.
1347 leaq EXT(idt64_hndl_table0)(%rip), %rax
1350 /* All 'exceptions' enter hndl_alltraps, with:
1351 * r15 x86_saved_state_t address
1352 * rsp kernel stack if user-space, otherwise interrupt or kernel stack
1355 * The rest of the state is set up as:
1356 * both rsp and r15 are 16-byte aligned
1357 * interrupts disabled
1358 * direction flag cleared
1360 Entry(hndl_alltraps)
1367 /* Check for active vtimers in the current task */
1368 mov %gs:CPU_ACTIVE_THREAD, %rcx
1369 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling trap/exception */
1370 mov TH_TASK(%rcx), %rbx
1371 TASK_VTIMER_CHECK(%rbx, %rcx)
1373 CCALL1(user_trap, %r15) /* call user trap routine */
1374 /* user_trap() unmasks interrupts */
1375 cli /* hold off intrs - critical section */
1376 xorl %ecx, %ecx /* don't check if we're in the PFZ */
1379 Entry(return_from_trap)
1380 movq %gs:CPU_ACTIVE_THREAD,%r15 /* Get current thread */
1381 movl $-1, TH_IOTIER_OVERRIDE(%r15) /* Reset IO tier override to -1 before returning to userspace */
1382 cmpl $0, TH_RWLOCK_COUNT(%r15) /* Check if current thread has pending RW locks held */
1384 xorq %rbp, %rbp /* clear framepointer */
1385 mov %r15, %rdi /* Set RDI to current thread */
1386 CCALL(lck_rw_clear_promotions_x86) /* Clear promotions if needed */
1388 movq TH_PCB_ISS(%r15), %r15 /* PCB stack */
1389 movl %gs:CPU_PENDING_AST,%eax
1391 je EXT(return_to_user) /* branch if no AST */
1393 L_return_from_trap_with_ast:
1394 testl %ecx, %ecx /* see if we need to check for an EIP in the PFZ */
1395 je 2f /* no, go handle the AST */
1396 cmpl $(SS_64), SS_FLAVOR(%r15) /* are we a 64-bit task? */
1398 /* no... 32-bit user mode */
1399 movl R32_EIP(%r15), %edi
1400 xorq %rbp, %rbp /* clear framepointer */
1401 CCALL(commpage_is_in_pfz32)
1403 je 2f /* not in the PFZ... go service AST */
1404 movl %eax, R32_EBX(%r15) /* let the PFZ know we've pended an AST */
1405 jmp EXT(return_to_user)
1407 movq R64_RIP(%r15), %rdi
1408 xorq %rbp, %rbp /* clear framepointer */
1409 CCALL(commpage_is_in_pfz64)
1411 je 2f /* not in the PFZ... go service AST */
1412 movl %eax, R64_RBX(%r15) /* let the PFZ know we've pended an AST */
1413 jmp EXT(return_to_user)
1416 xorq %rbp, %rbp /* clear framepointer */
1417 CCALL(ast_taken_user) /* handle all ASTs (enables interrupts, may return via continuation) */
1420 mov %rsp, %r15 /* AST changes stack, saved state */
1421 xorl %ecx, %ecx /* don't check if we're in the PFZ */
1422 jmp EXT(return_from_trap) /* and check again (rare) */
1425 * Trap from kernel mode. No need to switch stacks.
1426 * Interrupts must be off here - we will set them to state at time of trap
1427 * as soon as it's safe for us to do so and not recurse doing preemption
1431 movq %r15, %rdi /* saved state addr */
1432 pushq R64_RIP(%r15) /* Simulate a CALL from fault point */
1433 pushq %rbp /* Extend framepointer chain */
1435 CCALLWITHSP(kernel_trap) /* to kernel trap routine */
1438 mov %rsp, %r15 /* DTrace slides stack/saved-state */
1441 movl %gs:CPU_PENDING_AST,%eax /* get pending asts */
1442 testl $(AST_URGENT),%eax /* any urgent preemption? */
1443 je ret_to_kernel /* no, nothing to do */
1444 cmpl $(T_PREEMPT),R64_TRAPNO(%r15)
1445 je ret_to_kernel /* T_PREEMPT handled in kernel_trap() */
1446 testl $(EFL_IF),R64_RFLAGS(%r15) /* interrupts disabled? */
1448 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
1450 movq %gs:CPU_KERNEL_STACK,%rax
1453 andq EXT(kernel_stack_mask)(%rip),%rcx
1454 testq %rcx,%rcx /* are we on the kernel stack? */
1455 jne ret_to_kernel /* no, skip it */
1457 CCALL(ast_taken_kernel) /* take the AST */
1459 mov %rsp, %r15 /* AST changes stack, saved state */
1464 * All interrupts on all tasks enter here with:
1465 * r15 x86_saved_state_t
1466 * rsp kernel or interrupt stack
1469 * both rsp and r15 are 16-byte aligned
1470 * interrupts disabled
1471 * direction flag cleared
1473 Entry(hndl_allintrs)
1475 * test whether already on interrupt stack
1477 movq %gs:CPU_INT_STACK_TOP,%rcx
1480 leaq -INTSTACK_SIZE(%rcx),%rdx
1482 jb int_from_intstack
1484 xchgq %rcx,%rsp /* switch to interrupt stack */
1486 mov %cr0,%rax /* get cr0 */
1487 orl $(CR0_TS),%eax /* or in TS bit */
1488 mov %rax,%cr0 /* set cr0 */
1490 pushq %rcx /* save pointer to old stack */
1491 pushq %gs:CPU_INT_STATE /* save previous intr state */
1492 movq %r15,%gs:CPU_INT_STATE /* set intr state */
1494 TIME_INT_ENTRY /* do timing */
1496 /* Check for active vtimers in the current task */
1497 mov %gs:CPU_ACTIVE_THREAD, %rcx
1498 mov TH_TASK(%rcx), %rbx
1499 TASK_VTIMER_CHECK(%rbx, %rcx)
1501 incl %gs:CPU_PREEMPTION_LEVEL
1502 incl %gs:CPU_INTERRUPT_LEVEL
1504 CCALL1(interrupt, %r15) /* call generic interrupt routine */
1506 .globl EXT(return_to_iret)
1507 LEXT(return_to_iret) /* (label for kdb_kintr and hardclock) */
1509 decl %gs:CPU_INTERRUPT_LEVEL
1510 decl %gs:CPU_PREEMPTION_LEVEL
1512 TIME_INT_EXIT /* do timing */
1514 popq %gs:CPU_INT_STATE /* reset/clear intr state pointer */
1515 popq %rsp /* switch back to old stack */
1517 movq %gs:CPU_ACTIVE_THREAD,%rax
1518 movq TH_PCB_FPS(%rax),%rax /* get pcb's ifps */
1519 cmpq $0,%rax /* Is there a context */
1520 je 1f /* Branch if not */
1521 movl FP_VALID(%rax),%eax /* Load fp_valid */
1522 cmpl $0,%eax /* Check if valid */
1523 jne 1f /* Branch if valid */
1527 mov %cr0,%rax /* get cr0 */
1528 orl $(CR0_TS),%eax /* or in TS bit */
1529 mov %rax,%cr0 /* set cr0 */
1531 /* Load interrupted code segment into %eax */
1532 movl R32_CS(%r15),%eax /* assume 32-bit state */
1533 cmpl $(SS_64),SS_FLAVOR(%r15)/* 64-bit? */
1536 movl R64_CS(%r15),%eax /* 64-bit user mode */
1539 cmpl $(SS_32),SS_FLAVOR(%r15)
1542 CCALL1(panic_idt64, %r15)
1546 movl R64_CS(%r15),%eax /* 64-bit user mode */
1549 testb $3,%al /* user mode, */
1550 jnz ast_from_interrupt_user /* go handle potential ASTs */
1552 * we only want to handle preemption requests if
1553 * the interrupt fell in the kernel context
1554 * and preemption isn't disabled
1556 movl %gs:CPU_PENDING_AST,%eax
1557 testl $(AST_URGENT),%eax /* any urgent requests? */
1558 je ret_to_kernel /* no, nothing to do */
1560 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
1561 jne ret_to_kernel /* yes, skip it */
1564 * Take an AST from kernel space. We don't need (and don't want)
1565 * to do as much as the case where the interrupt came from user
1568 CCALL(ast_taken_kernel)
1570 mov %rsp, %r15 /* AST changes stack, saved state */
1575 * nested int - simple path, can't preempt etc on way out
1578 incl %gs:CPU_PREEMPTION_LEVEL
1579 incl %gs:CPU_INTERRUPT_LEVEL
1580 incl %gs:CPU_NESTED_ISTACK
1582 push %gs:CPU_INT_STATE
1583 mov %r15, %gs:CPU_INT_STATE
1585 CCALL1(interrupt, %r15)
1587 pop %gs:CPU_INT_STATE
1589 decl %gs:CPU_INTERRUPT_LEVEL
1590 decl %gs:CPU_PREEMPTION_LEVEL
1591 decl %gs:CPU_NESTED_ISTACK
1596 * Take an AST from an interrupted user
1598 ast_from_interrupt_user:
1599 movl %gs:CPU_PENDING_AST,%eax
1600 testl %eax,%eax /* pending ASTs? */
1601 je EXT(ret_to_user) /* no, nothing to do */
1605 movl $1, %ecx /* check if we're in the PFZ */
1606 jmp L_return_from_trap_with_ast /* return */
1609 /* Syscall dispatch routines! */
1614 * System call entries via INTR_GATE or sysenter:
1616 * r15 x86_saved_state32_t
1619 * both rsp and r15 are 16-byte aligned
1620 * interrupts disabled
1621 * direction flag cleared
1624 Entry(hndl_sysenter)
1626 * We can be here either for a mach syscall or a unix syscall,
1627 * as indicated by the sign of the code:
1629 movl R32_EAX(%r15),%eax
1631 js EXT(hndl_mach_scall) /* < 0 => mach */
1634 Entry(hndl_unix_scall)
1638 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1639 movq TH_TASK(%rcx),%rbx /* point to current task */
1640 incl TH_SYSCALLS_UNIX(%rcx) /* increment call count */
1642 /* Check for active vtimers in the current task */
1643 TASK_VTIMER_CHECK(%rbx,%rcx)
1647 CCALL1(unix_syscall, %r15)
1649 * always returns through thread_exception_return
1653 Entry(hndl_mach_scall)
1656 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1657 movq TH_TASK(%rcx),%rbx /* point to current task */
1658 incl TH_SYSCALLS_MACH(%rcx) /* increment call count */
1660 /* Check for active vtimers in the current task */
1661 TASK_VTIMER_CHECK(%rbx,%rcx)
1665 CCALL1(mach_call_munger, %r15)
1667 * always returns through thread_exception_return
1671 Entry(hndl_mdep_scall)
1674 /* Check for active vtimers in the current task */
1675 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1676 movq TH_TASK(%rcx),%rbx /* point to current task */
1677 TASK_VTIMER_CHECK(%rbx,%rcx)
1681 CCALL1(machdep_syscall, %r15)
1683 * always returns through thread_exception_return
1688 * System call entries via syscall only:
1690 * r15 x86_saved_state64_t
1693 * both rsp and r15 are 16-byte aligned
1694 * interrupts disabled
1695 * direction flag cleared
1701 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1702 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling syscall */
1703 movq TH_TASK(%rcx),%rbx /* point to current task */
1705 /* Check for active vtimers in the current task */
1706 TASK_VTIMER_CHECK(%rbx,%rcx)
1709 * We can be here either for a mach, unix machdep or diag syscall,
1710 * as indicated by the syscall class:
1712 movl R64_RAX(%r15), %eax /* syscall number/class */
1714 andl $(SYSCALL_CLASS_MASK), %edx /* syscall class */
1715 cmpl $(SYSCALL_CLASS_MACH<<SYSCALL_CLASS_SHIFT), %edx
1716 je EXT(hndl_mach_scall64)
1717 cmpl $(SYSCALL_CLASS_UNIX<<SYSCALL_CLASS_SHIFT), %edx
1718 je EXT(hndl_unix_scall64)
1719 cmpl $(SYSCALL_CLASS_MDEP<<SYSCALL_CLASS_SHIFT), %edx
1720 je EXT(hndl_mdep_scall64)
1721 cmpl $(SYSCALL_CLASS_DIAG<<SYSCALL_CLASS_SHIFT), %edx
1722 je EXT(hndl_diag_scall64)
1724 /* Syscall class unknown */
1726 CCALL3(i386_exception, $(EXC_SYSCALL), %rax, $1)
1730 Entry(hndl_unix_scall64)
1731 incl TH_SYSCALLS_UNIX(%rcx) /* increment call count */
1734 CCALL1(unix_syscall64, %r15)
1736 * always returns through thread_exception_return
1740 Entry(hndl_mach_scall64)
1741 incl TH_SYSCALLS_MACH(%rcx) /* increment call count */
1744 CCALL1(mach_call_munger64, %r15)
1746 * always returns through thread_exception_return
1751 Entry(hndl_mdep_scall64)
1754 CCALL1(machdep_syscall64, %r15)
1756 * always returns through thread_exception_return
1759 Entry(hndl_diag_scall64)
1760 CCALL1(diagCall64, %r15) // Call diagnostics
1761 test %eax, %eax // What kind of return is this?
1762 je 1f // - branch if bad (zero)
1763 jmp EXT(return_to_user) // Normal return, do not check asts...
1766 CCALL3(i386_exception, $EXC_SYSCALL, $0x6000, $1)
1768 /* TODO assert at all 'C' entry points that we're never operating on the fault stack's alias mapping */
1769 Entry(hndl_machine_check)
1770 /* Adjust SP and savearea to their canonical, non-aliased addresses */
1771 CCALL1(panic_machine_check64, %r15)
1774 Entry(hndl_double_fault)
1775 CCALL1(panic_double_fault64, %r15)