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_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 and we dispatch to handle the fault
250 Entry(idt64_gen_prot)
251 pushq $(HNDL_ALLTRAPS)
252 pushq $(T_GENERAL_PROTECTION)
253 jmp L_check_for_kern_flt
255 Entry(idt64_stack_fault)
256 pushq $(HNDL_ALLTRAPS)
257 pushq $(T_STACK_FAULT)
258 jmp L_check_for_kern_flt
260 L_check_for_kern_flt:
262 * If we took a #GP or #SS from the kernel, check if we took them
263 * from either ret32_iret or ret64_iret. If we did, we need to
264 * jump into L_dispatch at the swapgs so that the code in L_dispatch
265 * can proceed with the correct GSbase.
268 testb $3, 8+ISF64_CS(%rsp)
269 jnz L_dispatch_from_user_no_push_rax /* Fault from user, go straight to dispatch */
270 leaq EXT(ret32_iret)(%rip), %rax
271 cmpq %rax, 8+ISF64_RIP(%rsp)
273 leaq EXT(ret64_iret)(%rip), %rax
274 cmpq %rax, 8+ISF64_RIP(%rsp)
276 jmp L_dispatch_from_kernel_no_push_rax
278 * We hit the fault on iretq, so check the original return %cs. If
279 * it's a user %cs, fixup the stack and then jump to dispatch..
281 * With this type of fault, the stack is layed-out as follows:
284 * orig %ss saved_rsp+32
285 * orig %rsp saved_rsp+24
286 * orig %rflags saved_rsp+16
287 * orig %cs saved_rsp+8
288 * orig %rip saved_rsp
289 * ^^^^^^^^^ (maybe on another stack, since we switched to IST1)
298 * <saved %rax> <== %rsp -72
302 movq 16+ISF64_RSP(%rsp), %rbx
303 movq ISF64_CS-24(%rbx), %rax
304 testb $3, %al /* If the original return destination was to user */
307 jmp L_dispatch_from_kernel_no_push_rax /* Fault occurred when trying to return to kernel */
310 * Fix the stack so the original trap frame is current, then jump to dispatch
312 movq %rax, 16+ISF64_CS(%rsp)
314 movq ISF64_RSP-24(%rbx), %rax
315 movq %rax, 16+ISF64_RSP(%rsp)
317 movq ISF64_RIP-24(%rbx), %rax
318 movq %rax, 16+ISF64_RIP(%rsp)
320 movq ISF64_SS-24(%rbx), %rax
321 movq %rax, 16+ISF64_SS(%rsp)
323 movq ISF64_RFLAGS-24(%rbx), %rax
324 movq %rax, 16+ISF64_RFLAGS(%rsp)
327 jmp L_dispatch_from_user_no_push_rax
330 pushq $(HNDL_ALLTRAPS)
331 pushq $(T_SEGMENT_NOT_PRESENT)
335 * Fatal exception handlers:
337 Entry(idt64_db_task_dbl_fault)
338 pushq $(HNDL_DOUBLE_FAULT)
339 pushq $(T_DOUBLE_FAULT)
342 Entry(idt64_db_task_stk_fault)
343 pushq $(HNDL_DOUBLE_FAULT)
344 pushq $(T_STACK_FAULT)
348 push $(0) /* Error */
349 pushq $(HNDL_MACHINE_CHECK)
350 pushq $(T_MACHINE_CHECK)
355 * This may or may not be fatal but extreme care is required
356 * because it may fall when control was already in another trampoline.
358 * We get here on IST2 stack which is used exclusively for NMIs.
359 * Machine checks, doublefaults and similar use IST1
365 testb $3, ISF64_CS(%rsp)
368 /* From user-space: copy interrupt state to user PCB */
371 leaq EXT(idt64_hndl_table0)(%rip), %rax
372 mov 16(%rax), %rax /* Offset of per-CPU shadow */
373 mov %gs:CPU_SHADOWTASK_CR3(%rax), %rax
374 mov %rax, %cr3 /* note that SMAP is enabled in L_common_dispatch (on Broadwell+) */
376 mov %gs:CPU_UBER_ISF, %rcx /* PCB stack addr */
377 add $(ISF64_SIZE), %rcx /* adjust to base of ISF */
379 leaq TBL0_OFF_DISP_USER_WITH_POPRAX+EXT(idt64_hndl_table0)(%rip), %rax /* ks_dispatch_user_with_pop_rax */
380 jmp 4f /* Copy state to PCB */
385 * Determine whether the kernel or user GS is set.
386 * Sets the high 32 bits of the return CS to 1 to ensure that we'll swapgs back correctly at IRET.
388 mov $(MSR_IA32_GS_BASE), %ecx
389 rdmsr /* read kernel gsbase */
390 test $0x80000000, %edx /* test MSB of address */
393 movl $1, ISF64_CS+4(%rsp) /* and set flag in CS slot */
396 leaq EXT(idt64_hndl_table0)(%rip), %rax
397 mov 16(%rax), %rax /* Offset of per-CPU shadow */
399 mov %gs:CPU_SHADOWTASK_CR3(%rax), %rax
400 mov %rax, %cr3 /* Unconditionally switch to primary kernel pagetables */
403 * Determine whether we're on the kernel or interrupt stack
406 mov ISF64_RSP(%rsp), %rcx
407 mov %gs:CPU_KERNEL_STACK, %rax
409 movq TBL0_OFF_PTR_KERNEL_STACK_MASK+EXT(idt64_hndl_table0)(%rip), %rdx
410 mov (%rdx), %rdx /* Load kernel_stack_mask */
412 test %rax, %rax /* are we on the kernel stack? */
415 mov %gs:CPU_INT_STACK_TOP, %rax
416 cmp %rcx, %rax /* are we on the interrupt stack? */
418 leaq -INTSTACK_SIZE(%rax), %rax
422 mov %gs:CPU_KERNEL_STACK, %rcx
424 /* 16-byte-align kernel/interrupt stack for state push */
425 and $0xFFFFFFFFFFFFFFF0, %rcx
427 leaq TBL0_OFF_DISP_KERN_WITH_POPRAX+EXT(idt64_hndl_table0)(%rip), %rax /* ks_dispatch_kernel_with_pop_rax */
430 * Copy state from NMI stack (RSP) to the save area (RCX) which is
431 * the PCB for user or kernel/interrupt stack from kernel.
432 * ISF64_ERR(RSP) saved RAX
433 * ISF64_TRAPFN(RSP) saved RCX
434 * ISF64_TRAPNO(RSP) saved RDX
436 xchg %rsp, %rcx /* set for pushes */
439 push ISF64_RFLAGS(%rcx)
442 /* Synthesize common interrupt stack frame */
443 push $(0) /* error code 0 */
444 push $(HNDL_ALLINTRS) /* trapfn allintrs */
445 push $(T_NMI) /* trapno T_NMI */
446 push ISF64_ERR(%rcx) /* saved %rax is popped in ks_dispatch_{kernel|user}_with_pop_rax */
447 mov ISF64_TRAPNO(%rcx), %rdx
448 mov ISF64_TRAPFN(%rcx), %rcx
450 jmp *(%rax) /* ks_dispatch_{kernel|user}_with_pop_rax */
452 Entry(idt64_double_fault)
453 pushq $(HNDL_DOUBLE_FAULT)
454 pushq $(T_DOUBLE_FAULT)
460 /* Use RAX as a temporary by shifting its contents into R11[32:63]
461 * The systemcall number is defined to be a 32-bit quantity, as is
466 .globl EXT(dblsyscall_patch_point)
467 EXT(dblsyscall_patch_point):
468 // movabsq $0x12345678ABCDEFFFULL, %rax
469 /* Generate offset to the double-mapped per-CPU data shadow
472 leaq EXT(idt64_hndl_table0)(%rip), %rax
474 mov %rsp, %gs:CPU_UBER_TMP(%rax) /* save user stack */
475 mov %gs:CPU_ESTACK(%rax), %rsp /* switch stack to per-cpu estack */
476 sub $(ISF64_SIZE), %rsp
479 * Synthesize an ISF frame on the exception stack
481 movl $(USER_DS), ISF64_SS(%rsp)
482 mov %rcx, ISF64_RIP(%rsp) /* rip */
484 mov %gs:CPU_UBER_TMP(%rax), %rcx
485 mov %rcx, ISF64_RSP(%rsp) /* user stack --changed */
488 shrq $32, %rax /* Restore RAX */
489 mov %r11d, %r11d /* Clear r11[32:63] */
491 mov %r11, ISF64_RFLAGS(%rsp) /* rflags */
492 movl $(SYSCALL_CS), ISF64_CS(%rsp) /* cs - a pseudo-segment */
493 mov %rax, ISF64_ERR(%rsp) /* err/rax - syscall code */
494 movq $(HNDL_SYSCALL), ISF64_TRAPFN(%rsp)
495 movq $(T_SYSCALL), ISF64_TRAPNO(%rsp) /* trapno */
497 jmp L_dispatch /* this can only be 64-bit */
500 Entry(idt64_sysenter)
501 /* Synthesize an interrupt stack frame onto the
504 push $(USER_DS) /* ss */
508 * Clear, among others, the Nested Task (NT) flags bit;
509 * this is zeroed by INT, but not by SYSENTER.
513 push $(SYSENTER_CS) /* cs */
516 push %rax /* err/eax - syscall code */
517 pushq $(HNDL_SYSENTER)
519 orl $(EFL_IF), ISF64_RFLAGS(%rsp)
520 jmp L_u64bit_entry_check
523 * Common dispatch point.
524 * Determine what mode has been interrupted and save state accordingly.
526 * rsp from user-space: interrupt state in PCB, or
527 * from kernel-space: interrupt state in kernel or interrupt stack
528 * GSBASE from user-space: pthread area, or
529 * from kernel-space: cpu_data
534 testb $3, 8+ISF64_CS(%rsp)
536 L_dispatch_from_user_no_push_rax:
538 leaq EXT(idt64_hndl_table0)(%rip), %rax
541 mov %gs:CPU_SHADOWTASK_CR3(%rax), %rax
544 mov %rax, %gs:CPU_ENTRY_CR3
546 L_dispatch_from_kernel_no_push_rax:
548 leaq EXT(idt64_hndl_table0)(%rip), %rax
549 /* The text/data relationship here must be preserved in the doublemap, and the contents must be remapped */
550 /* Indirect branch to non-doublemapped trampolines */
552 /* User return: register restoration and address space switch sequence */
553 Entry(ks_64bit_return)
554 mov R64_R14(%r15), %r14
555 mov R64_R13(%r15), %r13
556 mov R64_R12(%r15), %r12
557 mov R64_R11(%r15), %r11
558 mov R64_R10(%r15), %r10
559 mov R64_R9(%r15), %r9
560 mov R64_R8(%r15), %r8
561 mov R64_RSI(%r15), %rsi
562 mov R64_RDI(%r15), %rdi
563 mov R64_RBP(%r15), %rbp
564 mov R64_RDX(%r15), %rdx
565 mov R64_RCX(%r15), %rcx
566 mov R64_RBX(%r15), %rbx
567 mov R64_RAX(%r15), %rax
568 /* Switch to per-CPU exception stack */
569 mov %gs:CPU_ESTACK, %rsp
571 /* Synthesize interrupt stack frame from PCB savearea to exception stack */
574 push R64_RFLAGS(%r15)
578 cmpq $(KERNEL64_CS), 8(%rsp)
579 jne 1f /* Returning to user (%r15 will be restored after the segment checks) */
580 mov R64_R15(%r15), %r15
581 jmp L_64b_kernel_return /* Returning to kernel */
585 movl %gs:CPU_NEED_SEGCHK, %eax
588 /* Returning to user */
589 cmpl $0, %gs:CPU_CURTASK_HAS_LDT /* If the current task has an LDT, check and restore segment regs */
590 jne L_64b_segops_island
593 * Restore %r15, since we're now done accessing saved state
594 * and (%r15) won't be accessible after the %cr3 load anyway.
595 * Note that %r15 is restored below for the segment-restore
596 * case, just after we no longer need to access register state
599 mov R64_R15(%r15), %r15
602 * Note that this %cr3 sequence is duplicated here to save
603 * [at least] a load and comparison that would be required if
604 * this block were shared.
606 /* Discover user cr3/ASID */
607 mov %gs:CPU_UCR3, %rax
609 mov %rax, %gs:CPU_EXIT_CR3
612 /* Continue execution on the shared/doublemapped trampoline */
616 pop %rax /* Matched to [B], above (segchk required) */
619 * At this point, the stack contains:
627 * | Saved RAX | <-- rsp
630 cmpl $(SYSCALL_CS), 16(%rsp) /* test for exit via SYSRET */
636 pop %rax /* Matched to [A], above */
639 .globl EXT(ret64_iret)
641 iretq /* return from interrupt */
648 pop %rax /* Matched to [A], above */
650 * Here to restore rcx/r11/rsp and perform the sysret back to user-space.
653 * rsp user stack pointer
659 sysretq /* return from system call */
664 pop %rax /* Matched to [A], above */
665 verw 40(%rsp) /* verw operates on the %ss value already on the stack */
671 pop %rax /* Matched to [A], above */
674 * Here to restore rcx/r11/rsp and perform the sysret back to user-space.
677 * rsp user stack pointer
682 verw 8(%rsp) /* verw operates on the %ss value already on the stack */
684 sysretq /* return from system call */
689 /* Validate CS/DS/ES/FS/GS segment selectors with the Load Access Rights instruction prior to restoration */
690 /* Exempt "known good" statically configured selectors, e.g. USER64_CS and 0 */
691 cmpl $(USER64_CS), R64_CS(%r15)
693 larw R64_CS(%r15), %ax
695 /* Ensure that the segment referenced by CS in the saved state is a code segment (bit 11 == 1) */
697 jz L_64_reset_cs /* Update stored %cs with known-good selector if ZF == 1 */
700 movl $(USER64_CS), R64_CS(%r15)
702 cmpl $0, R64_DS(%r15)
704 larw R64_DS(%r15), %ax
706 movl $0, R64_DS(%r15)
708 cmpl $0, R64_ES(%r15)
710 larw R64_ES(%r15), %ax
712 movl $0, R64_ES(%r15)
714 cmpl $0, R64_FS(%r15)
716 larw R64_FS(%r15), %ax
718 movl $0, R64_FS(%r15)
720 cmpl $0, R64_GS(%r15)
722 larw R64_GS(%r15), %ax
724 movl $0, R64_GS(%r15)
727 * Pack the segment registers in %rax since (%r15) will not
728 * be accessible after the %cr3 switch.
729 * Only restore %gs if cthread_self is zero, (indicate
730 * this to the code below with a value of 0xffff)
732 mov %gs:CPU_ACTIVE_THREAD, %rax /* Get the active thread */
733 cmpq $0, TH_CTH_SELF(%rax)
738 movw R64_GS(%r15), %ax
741 movw R64_FS(%r15), %ax
743 movw R64_ES(%r15), %ax
745 movw R64_DS(%r15), %ax
748 * Restore %r15, since we're done accessing saved state
749 * and (%r15) won't be accessible after the %cr3 switch.
751 mov R64_R15(%r15), %r15
753 /* Discover user cr3/ASID */
755 mov %gs:CPU_UCR3, %rax
757 mov %rax, %gs:CPU_EXIT_CR3
760 /* Continue execution on the shared/doublemapped trampoline */
765 * Returning to user; restore segment registers that might be used
766 * by compatibility-mode code in a 64-bit user process.
768 * Note that if we take a fault here, it's OK that we haven't yet
769 * popped %rax from the stack, because %rsp will be reset to
770 * the value pushed onto the exception stack (above).
782 * 0xFFFF is the sentinel set above that indicates we should
783 * not restore %gs (because GS.base was already set elsewhere
784 * (e.g.: in act_machine_set_pcb or machine_thread_set_tsd_base))
788 movw %ax, %gs /* Restore %gs to user-set value */
792 L_u64bit_entry_check:
794 * Check we're not a confused 64-bit user.
798 leaq EXT(idt64_hndl_table0)(%rip), %rax
801 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP(%rax)
802 jne L_64bit_entry_reject
805 L_64bit_entry_reject:
807 * Here for a 64-bit user attempting an invalid kernel entry.
809 movq $(HNDL_ALLTRAPS), 8+ISF64_TRAPFN(%rsp)
810 movq $(T_INVALID_OPCODE), 8+ISF64_TRAPNO(%rsp)
813 /* End of double-mapped TEXT */
818 cmpl $(KERNEL64_CS), ISF64_CS(%rsp)
819 je EXT(ks_dispatch_kernel)
821 mov %rax, %gs:CPU_UBER_TMP
822 mov %gs:CPU_UBER_ISF, %rax
823 add $(ISF64_SIZE), %rax
826 /* Memory to memory moves (aint x86 wonderful):
827 * Transfer the exception frame from the per-CPU exception stack to the
828 * 'PCB' stack programmed at cswitch.
832 push ISF64_RFLAGS(%rax)
836 push ISF64_TRAPFN(%rax)
837 push ISF64_TRAPNO(%rax)
838 mov %gs:CPU_UBER_TMP, %rax
839 jmp EXT(ks_dispatch_user)
841 Entry(ks_dispatch_user_with_pop_rax)
843 jmp EXT(ks_dispatch_user)
848 Entry(ks_dispatch_user)
849 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP
850 je L_dispatch_U32 /* 32-bit user task */
853 subq $(ISS64_OFFSET), %rsp
854 mov %r15, R64_R15(%rsp)
856 mov %gs:CPU_KERNEL_STACK, %rsp
859 Entry(ks_dispatch_kernel_with_pop_rax)
861 jmp EXT(ks_dispatch_kernel)
863 Entry(ks_dispatch_kernel)
864 subq $(ISS64_OFFSET), %rsp
865 mov %r15, R64_R15(%rsp)
869 * Here for 64-bit user task or kernel
872 movl $(SS_64), SS_FLAVOR(%r15)
875 * Save segment regs if a 64-bit task has
876 * installed customized segments in the LDT
878 cmpl $0, %gs:CPU_CURTASK_HAS_LDT
879 je L_skip_save_extra_segregs
881 mov %ds, R64_DS(%r15)
882 mov %es, R64_ES(%r15)
884 L_skip_save_extra_segregs:
885 mov %fs, R64_FS(%r15)
886 mov %gs, R64_GS(%r15)
889 /* Save general-purpose registers */
890 mov %rax, R64_RAX(%r15)
891 mov %rbx, R64_RBX(%r15)
892 mov %rcx, R64_RCX(%r15)
893 mov %rdx, R64_RDX(%r15)
894 mov %rbp, R64_RBP(%r15)
895 mov %rdi, R64_RDI(%r15)
896 mov %rsi, R64_RSI(%r15)
897 mov %r8, R64_R8(%r15)
898 mov %r9, R64_R9(%r15)
899 mov %r10, R64_R10(%r15)
900 mov %r11, R64_R11(%r15)
901 mov %r12, R64_R12(%r15)
902 mov %r13, R64_R13(%r15)
903 mov %r14, R64_R14(%r15)
905 /* Zero unused GPRs. BX/DX/SI are clobbered elsewhere across the exception handler, and are skipped. */
916 /* cr2 is significant only for page-faults */
918 mov %rax, R64_CR2(%r15)
920 L_dispatch_U64_after_fault:
921 mov R64_TRAPNO(%r15), %ebx /* %ebx := trapno for later */
922 mov R64_TRAPFN(%r15), %rdx /* %rdx := trapfn for later */
923 mov R64_CS(%r15), %esi /* %esi := cs for later */
925 jmp L_common_dispatch
927 L_dispatch_U32: /* 32-bit user task */
928 subq $(ISS64_OFFSET), %rsp
930 mov %gs:CPU_KERNEL_STACK, %rsp
931 movl $(SS_32), SS_FLAVOR(%r15)
936 mov %ds, R32_DS(%r15)
937 mov %es, R32_ES(%r15)
938 mov %fs, R32_FS(%r15)
939 mov %gs, R32_GS(%r15)
942 * Save general 32-bit registers
944 mov %eax, R32_EAX(%r15)
945 mov %ebx, R32_EBX(%r15)
946 mov %ecx, R32_ECX(%r15)
947 mov %edx, R32_EDX(%r15)
948 mov %ebp, R32_EBP(%r15)
949 mov %esi, R32_ESI(%r15)
950 mov %edi, R32_EDI(%r15)
952 /* Unconditionally save cr2; only meaningful on page faults */
954 mov %eax, R32_CR2(%r15)
955 /* Zero unused GPRs. BX/DX/SI/R15 are clobbered elsewhere across the exception handler, and are skipped. */
967 * Copy registers already saved in the machine state
968 * (in the interrupt stack frame) into the compat save area.
970 mov R64_RIP(%r15), %eax
971 mov %eax, R32_EIP(%r15)
972 mov R64_RFLAGS(%r15), %eax
973 mov %eax, R32_EFLAGS(%r15)
974 mov R64_RSP(%r15), %eax
975 mov %eax, R32_UESP(%r15)
976 mov R64_SS(%r15), %eax
977 mov %eax, R32_SS(%r15)
978 L_dispatch_U32_after_fault:
979 mov R64_CS(%r15), %esi /* %esi := %cs for later */
980 mov %esi, R32_CS(%r15)
981 mov R64_TRAPNO(%r15), %ebx /* %ebx := trapno for later */
982 mov %ebx, R32_TRAPNO(%r15)
983 mov R64_ERR(%r15), %eax
984 mov %eax, R32_ERR(%r15)
985 mov R64_TRAPFN(%r15), %rdx /* %rdx := trapfn for later */
988 cld /* Ensure the direction flag is clear in the kernel */
989 cmpl $0, EXT(pmap_smap_enabled)(%rip)
991 clac /* Clear EFLAGS.AC if SMAP is present/enabled */
994 * We mark the kernel's cr3 as "active" for TLB coherency evaluation
995 * For threads with a mapped pagezero (some WINE games) on non-SMAP platforms,
996 * we switch to the kernel's address space on entry. Also,
997 * if the global no_shared_cr3 is TRUE we do switch to the kernel's cr3
998 * so that illicit accesses to userspace can be trapped.
1000 mov %gs:CPU_KERNEL_CR3, %rcx
1001 mov %rcx, %gs:CPU_ACTIVE_CR3
1002 test $3, %esi /* CS: user/kernel? */
1003 jz 2f /* skip CR3 reload if from kernel */
1005 cmpl $0, %gs:CPU_PAGEZERO_MAPPED
1007 cmpl $0, EXT(no_shared_cr3)(%rip)
1011 movw %gs:CPU_KERNEL_PCID, %ax
1013 mov %rcx, %cr3 /* load kernel cr3 */
1016 /* Deferred processing of pending kernel address space TLB invalidations */
1017 mov %gs:CPU_ACTIVE_CR3+4, %rcx
1021 movl $0, %gs:CPU_TLB_INVALID
1022 cmpb $0, EXT(invpcid_enabled)(%rip)
1025 invpcid %gs:CPU_IP_DESC, %rcx
1028 mov %gs:CPU_ACTIVE_THREAD, %rcx /* Get the active thread */
1031 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling trap */
1032 cmpq $0, TH_PCB_IDS(%rcx) /* Is there a debug register state? */
1035 incl %gs:hwIntCnt(,%ebx,4) // Bump the trap/intr count
1036 /* Dispatch the designated handler */
1037 cmp EXT(dblmap_base)(%rip), %rsp
1039 cmp EXT(dblmap_max)(%rip), %rsp
1041 subq EXT(dblmap_dist)(%rip), %rsp
1042 subq EXT(dblmap_dist)(%rip), %r15
1044 leaq EXT(idt64_hndl_table1)(%rip), %rax
1045 jmp *(%rax, %rdx, 8)
1048 mov %cr4, %rcx /* RMWW CR4, for lack of an alternative*/
1049 and $(~CR4_PGE), %rcx
1055 xor %ecx, %ecx /* If so, reset DR7 (the control) */
1059 * Control is passed here to return to user.
1061 Entry(return_to_user)
1065 mov %gs:CPU_ACTIVE_THREAD, %rdx
1066 cmpq $0, TH_PCB_IDS(%rdx) /* Is there a debug register context? */
1067 jnz L_dr_restore_island
1070 * We now mark the task's address space as active for TLB coherency.
1071 * Handle special cases such as pagezero-less tasks here.
1073 mov %gs:CPU_TASK_CR3, %rcx
1074 mov %rcx, %gs:CPU_ACTIVE_CR3
1075 cmpl $0, %gs:CPU_PAGEZERO_MAPPED
1076 jnz L_cr3_switch_island
1077 movl EXT(no_shared_cr3)(%rip), %eax
1078 test %eax, %eax /* -no_shared_cr3 */
1079 jnz L_cr3_switch_island
1081 L_cr3_switch_return:
1082 mov %gs:CPU_DR7, %rax /* Is there a debug control register?*/
1085 mov %rax, %dr7 /* Set DR7 */
1086 movq $0, %gs:CPU_DR7
1088 cmpl $(SS_64), SS_FLAVOR(%r15) /* 64-bit state? */
1093 cmpl $(SS_32), SS_FLAVOR(%r15) /* 32-bit state? */
1097 CCALL1(panic_idt64, %r15)
1099 #endif /* DEBUG_IDT64 */
1102 * Restore registers into the machine state for iret.
1103 * Here on fault stack and PCB address in R11.
1105 movl R32_EIP(%r15), %eax
1106 movl %eax, R64_RIP(%r15)
1107 movl R32_EFLAGS(%r15), %eax
1108 movl %eax, R64_RFLAGS(%r15)
1109 movl R32_CS(%r15), %eax
1110 movl %eax, R64_CS(%r15)
1111 movl R32_UESP(%r15), %eax
1112 movl %eax, R64_RSP(%r15)
1113 movl R32_SS(%r15), %eax
1114 movl %eax, R64_SS(%r15)
1116 /* Validate CS/DS/ES/FS/GS segment selectors with the Load Access Rights instruction prior to restoration */
1117 /* Exempt "known good" statically configured selectors, e.g. USER_CS, USER_DS and 0 */
1118 cmpl $(USER_CS), R32_CS(%r15)
1120 larw R32_CS(%r15), %ax
1122 /* Ensure that the segment referenced by CS in the saved state is a code segment (bit 11 == 1) */
1124 jz L_32_reset_cs /* Update stored %cs with known-good selector if ZF == 1 */
1127 movl $(USER_CS), R32_CS(%r15)
1129 cmpl $(USER_DS), R32_DS(%r15)
1131 cmpl $0, R32_DS(%r15)
1133 larw R32_DS(%r15), %ax
1135 movl $(USER_DS), R32_DS(%r15)
1137 cmpl $(USER_DS), R32_ES(%r15)
1139 cmpl $0, R32_ES(%r15)
1141 larw R32_ES(%r15), %ax
1143 movl $(USER_DS), R32_ES(%r15)
1145 cmpl $(USER_DS), R32_FS(%r15)
1147 cmpl $0, R32_FS(%r15)
1149 larw R32_FS(%r15), %ax
1151 movl $(USER_DS), R32_FS(%r15)
1153 cmpl $(USER_CTHREAD), R32_GS(%r15)
1155 cmpl $0, R32_GS(%r15)
1157 larw R32_GS(%r15), %ax
1159 movl $(USER_CTHREAD), R32_GS(%r15)
1162 * Restore general 32-bit registers
1164 movl R32_EAX(%r15), %eax
1165 movl R32_EBX(%r15), %ebx
1166 movl R32_ECX(%r15), %ecx
1167 movl R32_EDX(%r15), %edx
1168 movl R32_EBP(%r15), %ebp
1169 movl R32_ESI(%r15), %esi
1170 movl R32_EDI(%r15), %edi
1173 * Restore segment registers. A segment exception taken here will
1174 * push state on the IST1 stack and will not affect the "PCB stack".
1176 mov %r15, %rsp /* Set the PCB as the stack */
1177 movl %gs:CPU_NEED_SEGCHK, %r14d /* %r14 will be restored below */
1180 /* Zero 64-bit-exclusive GPRs to prevent data leaks */
1189 movw R32_DS(%rsp), %ds
1190 movw R32_ES(%rsp), %es
1191 movw R32_FS(%rsp), %fs
1192 movw R32_GS(%rsp), %gs
1194 /* pop compat frame + trapno, trapfn and error */
1195 add $(ISS64_OFFSET)+8+8+8, %rsp
1198 * At this point, the stack contains:
1202 * | Return RSP | +24
1203 * | Return RFL | +16
1205 * | Return RIP | <-- rsp
1209 cmpl $(SYSENTER_CS), 8(%rsp)
1210 /* test for sysexit */
1211 je L_rtu_via_sysexit
1219 .globl EXT(ret32_iret)
1221 iretq /* return from interrupt */
1232 pop %rdx /* user return eip */
1233 pop %rcx /* pop and toss cs */
1234 andl $(~EFL_IF), (%rsp) /* clear interrupts enable, sti below */
1237 * %ss is now at 16(%rsp)
1244 popf /* flags - carry denotes failure */
1245 pop %rcx /* user return esp */
1248 sti /* interrupts enabled after sysexit */
1249 sysexitl /* 32-bit sysexit */
1251 L_dr_restore_island:
1252 movq TH_PCB_IDS(%rdx),%rax /* Obtain this thread's debug state */
1253 cmpl $(TASK_MAP_32BIT), %gs:CPU_TASK_MAP /* Are we a 32-bit task? */
1255 movl DS_DR0(%rax), %ecx /* If so, load the 32 bit DRs */
1257 movl DS_DR1(%rax), %ecx
1259 movl DS_DR2(%rax), %ecx
1261 movl DS_DR3(%rax), %ecx
1263 movl DS_DR7(%rax), %ecx
1264 movq %rcx, %gs:CPU_DR7
1267 mov DS64_DR0(%rax), %rcx /* Load the full width DRs*/
1269 mov DS64_DR1(%rax), %rcx
1271 mov DS64_DR2(%rax), %rcx
1273 mov DS64_DR3(%rax), %rcx
1275 mov DS64_DR7(%rax), %rcx
1276 mov %rcx, %gs:CPU_DR7
1278 jmp L_post_dr_restore
1279 L_cr3_switch_island:
1281 movw %gs:CPU_ACTIVE_PCID, %ax
1284 jmp L_cr3_switch_return
1288 cmpl $(SS_64), SS_FLAVOR(%r15) /* 64-bit state? */
1292 CCALL1(panic_idt64, %r15)
1295 cmpl $(KERNEL64_CS), R64_CS(%r15)
1297 CCALL1(panic_idt64, %r15)
1304 * Restore general 64-bit registers.
1305 * Here on fault stack and PCB address in R15.
1307 leaq EXT(idt64_hndl_table0)(%rip), %rax
1310 /* All 'exceptions' enter hndl_alltraps, with:
1311 * r15 x86_saved_state_t address
1312 * rsp kernel stack if user-space, otherwise interrupt or kernel stack
1315 * The rest of the state is set up as:
1316 * both rsp and r15 are 16-byte aligned
1317 * interrupts disabled
1318 * direction flag cleared
1320 Entry(hndl_alltraps)
1327 /* Check for active vtimers in the current task */
1328 mov %gs:CPU_ACTIVE_THREAD, %rcx
1329 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling trap/exception */
1330 mov TH_TASK(%rcx), %rbx
1331 TASK_VTIMER_CHECK(%rbx, %rcx)
1333 CCALL1(user_trap, %r15) /* call user trap routine */
1334 /* user_trap() unmasks interrupts */
1335 cli /* hold off intrs - critical section */
1336 xorl %ecx, %ecx /* don't check if we're in the PFZ */
1339 Entry(return_from_trap)
1340 movq %gs:CPU_ACTIVE_THREAD,%r15 /* Get current thread */
1341 movl $-1, TH_IOTIER_OVERRIDE(%r15) /* Reset IO tier override to -1 before returning to userspace */
1342 cmpl $0, TH_RWLOCK_COUNT(%r15) /* Check if current thread has pending RW locks held */
1344 xorq %rbp, %rbp /* clear framepointer */
1345 mov %r15, %rdi /* Set RDI to current thread */
1346 CCALL(lck_rw_clear_promotions_x86) /* Clear promotions if needed */
1348 movq TH_PCB_ISS(%r15), %r15 /* PCB stack */
1349 movl %gs:CPU_PENDING_AST,%eax
1351 je EXT(return_to_user) /* branch if no AST */
1353 L_return_from_trap_with_ast:
1354 testl %ecx, %ecx /* see if we need to check for an EIP in the PFZ */
1355 je 2f /* no, go handle the AST */
1356 cmpl $(SS_64), SS_FLAVOR(%r15) /* are we a 64-bit task? */
1358 /* no... 32-bit user mode */
1359 movl R32_EIP(%r15), %edi
1360 xorq %rbp, %rbp /* clear framepointer */
1361 CCALL(commpage_is_in_pfz32)
1363 je 2f /* not in the PFZ... go service AST */
1364 movl %eax, R32_EBX(%r15) /* let the PFZ know we've pended an AST */
1365 jmp EXT(return_to_user)
1367 movq R64_RIP(%r15), %rdi
1368 xorq %rbp, %rbp /* clear framepointer */
1369 CCALL(commpage_is_in_pfz64)
1371 je 2f /* not in the PFZ... go service AST */
1372 movl %eax, R64_RBX(%r15) /* let the PFZ know we've pended an AST */
1373 jmp EXT(return_to_user)
1376 xorq %rbp, %rbp /* clear framepointer */
1377 CCALL(ast_taken_user) /* handle all ASTs (enables interrupts, may return via continuation) */
1380 mov %rsp, %r15 /* AST changes stack, saved state */
1381 xorl %ecx, %ecx /* don't check if we're in the PFZ */
1382 jmp EXT(return_from_trap) /* and check again (rare) */
1385 * Trap from kernel mode. No need to switch stacks.
1386 * Interrupts must be off here - we will set them to state at time of trap
1387 * as soon as it's safe for us to do so and not recurse doing preemption
1391 movq %r15, %rdi /* saved state addr */
1392 pushq R64_RIP(%r15) /* Simulate a CALL from fault point */
1393 pushq %rbp /* Extend framepointer chain */
1395 CCALLWITHSP(kernel_trap) /* to kernel trap routine */
1398 mov %rsp, %r15 /* DTrace slides stack/saved-state */
1401 movl %gs:CPU_PENDING_AST,%eax /* get pending asts */
1402 testl $(AST_URGENT),%eax /* any urgent preemption? */
1403 je ret_to_kernel /* no, nothing to do */
1404 cmpl $(T_PREEMPT),R64_TRAPNO(%r15)
1405 je ret_to_kernel /* T_PREEMPT handled in kernel_trap() */
1406 testl $(EFL_IF),R64_RFLAGS(%r15) /* interrupts disabled? */
1408 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
1410 movq %gs:CPU_KERNEL_STACK,%rax
1413 andq EXT(kernel_stack_mask)(%rip),%rcx
1414 testq %rcx,%rcx /* are we on the kernel stack? */
1415 jne ret_to_kernel /* no, skip it */
1417 CCALL(ast_taken_kernel) /* take the AST */
1419 mov %rsp, %r15 /* AST changes stack, saved state */
1424 * All interrupts on all tasks enter here with:
1425 * r15 x86_saved_state_t
1426 * rsp kernel or interrupt stack
1429 * both rsp and r15 are 16-byte aligned
1430 * interrupts disabled
1431 * direction flag cleared
1433 Entry(hndl_allintrs)
1435 * test whether already on interrupt stack
1437 movq %gs:CPU_INT_STACK_TOP,%rcx
1440 leaq -INTSTACK_SIZE(%rcx),%rdx
1442 jb int_from_intstack
1444 xchgq %rcx,%rsp /* switch to interrupt stack */
1446 mov %cr0,%rax /* get cr0 */
1447 orl $(CR0_TS),%eax /* or in TS bit */
1448 mov %rax,%cr0 /* set cr0 */
1450 pushq %rcx /* save pointer to old stack */
1451 pushq %gs:CPU_INT_STATE /* save previous intr state */
1452 movq %r15,%gs:CPU_INT_STATE /* set intr state */
1454 TIME_INT_ENTRY /* do timing */
1456 /* Check for active vtimers in the current task */
1457 mov %gs:CPU_ACTIVE_THREAD, %rcx
1458 mov TH_TASK(%rcx), %rbx
1459 TASK_VTIMER_CHECK(%rbx, %rcx)
1461 incl %gs:CPU_PREEMPTION_LEVEL
1462 incl %gs:CPU_INTERRUPT_LEVEL
1464 CCALL1(interrupt, %r15) /* call generic interrupt routine */
1466 .globl EXT(return_to_iret)
1467 LEXT(return_to_iret) /* (label for kdb_kintr and hardclock) */
1469 decl %gs:CPU_INTERRUPT_LEVEL
1470 decl %gs:CPU_PREEMPTION_LEVEL
1472 TIME_INT_EXIT /* do timing */
1474 popq %gs:CPU_INT_STATE /* reset/clear intr state pointer */
1475 popq %rsp /* switch back to old stack */
1477 movq %gs:CPU_ACTIVE_THREAD,%rax
1478 movq TH_PCB_FPS(%rax),%rax /* get pcb's ifps */
1479 cmpq $0,%rax /* Is there a context */
1480 je 1f /* Branch if not */
1481 movl FP_VALID(%rax),%eax /* Load fp_valid */
1482 cmpl $0,%eax /* Check if valid */
1483 jne 1f /* Branch if valid */
1487 mov %cr0,%rax /* get cr0 */
1488 orl $(CR0_TS),%eax /* or in TS bit */
1489 mov %rax,%cr0 /* set cr0 */
1491 /* Load interrupted code segment into %eax */
1492 movl R32_CS(%r15),%eax /* assume 32-bit state */
1493 cmpl $(SS_64),SS_FLAVOR(%r15)/* 64-bit? */
1496 movl R64_CS(%r15),%eax /* 64-bit user mode */
1499 cmpl $(SS_32),SS_FLAVOR(%r15)
1502 CCALL1(panic_idt64, %r15)
1506 movl R64_CS(%r15),%eax /* 64-bit user mode */
1509 testb $3,%al /* user mode, */
1510 jnz ast_from_interrupt_user /* go handle potential ASTs */
1512 * we only want to handle preemption requests if
1513 * the interrupt fell in the kernel context
1514 * and preemption isn't disabled
1516 movl %gs:CPU_PENDING_AST,%eax
1517 testl $(AST_URGENT),%eax /* any urgent requests? */
1518 je ret_to_kernel /* no, nothing to do */
1520 cmpl $0,%gs:CPU_PREEMPTION_LEVEL /* preemption disabled? */
1521 jne ret_to_kernel /* yes, skip it */
1524 * Take an AST from kernel space. We don't need (and don't want)
1525 * to do as much as the case where the interrupt came from user
1528 CCALL(ast_taken_kernel)
1530 mov %rsp, %r15 /* AST changes stack, saved state */
1535 * nested int - simple path, can't preempt etc on way out
1538 incl %gs:CPU_PREEMPTION_LEVEL
1539 incl %gs:CPU_INTERRUPT_LEVEL
1540 incl %gs:CPU_NESTED_ISTACK
1542 push %gs:CPU_INT_STATE
1543 mov %r15, %gs:CPU_INT_STATE
1545 CCALL1(interrupt, %r15)
1547 pop %gs:CPU_INT_STATE
1549 decl %gs:CPU_INTERRUPT_LEVEL
1550 decl %gs:CPU_PREEMPTION_LEVEL
1551 decl %gs:CPU_NESTED_ISTACK
1556 * Take an AST from an interrupted user
1558 ast_from_interrupt_user:
1559 movl %gs:CPU_PENDING_AST,%eax
1560 testl %eax,%eax /* pending ASTs? */
1561 je EXT(ret_to_user) /* no, nothing to do */
1565 movl $1, %ecx /* check if we're in the PFZ */
1566 jmp L_return_from_trap_with_ast /* return */
1569 /* Syscall dispatch routines! */
1574 * System call entries via INTR_GATE or sysenter:
1576 * r15 x86_saved_state32_t
1579 * both rsp and r15 are 16-byte aligned
1580 * interrupts disabled
1581 * direction flag cleared
1584 Entry(hndl_sysenter)
1586 * We can be here either for a mach syscall or a unix syscall,
1587 * as indicated by the sign of the code:
1589 movl R32_EAX(%r15),%eax
1591 js EXT(hndl_mach_scall) /* < 0 => mach */
1594 Entry(hndl_unix_scall)
1598 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1599 movq TH_TASK(%rcx),%rbx /* point to current task */
1600 incl TH_SYSCALLS_UNIX(%rcx) /* increment call count */
1602 /* Check for active vtimers in the current task */
1603 TASK_VTIMER_CHECK(%rbx,%rcx)
1607 CCALL1(unix_syscall, %r15)
1609 * always returns through thread_exception_return
1613 Entry(hndl_mach_scall)
1616 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1617 movq TH_TASK(%rcx),%rbx /* point to current task */
1618 incl TH_SYSCALLS_MACH(%rcx) /* increment call count */
1620 /* Check for active vtimers in the current task */
1621 TASK_VTIMER_CHECK(%rbx,%rcx)
1625 CCALL1(mach_call_munger, %r15)
1627 * always returns through thread_exception_return
1631 Entry(hndl_mdep_scall)
1634 /* Check for active vtimers in the current task */
1635 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1636 movq TH_TASK(%rcx),%rbx /* point to current task */
1637 TASK_VTIMER_CHECK(%rbx,%rcx)
1641 CCALL1(machdep_syscall, %r15)
1643 * always returns through thread_exception_return
1648 * System call entries via syscall only:
1650 * r15 x86_saved_state64_t
1653 * both rsp and r15 are 16-byte aligned
1654 * interrupts disabled
1655 * direction flag cleared
1661 movq %gs:CPU_ACTIVE_THREAD,%rcx /* get current thread */
1662 movl $-1, TH_IOTIER_OVERRIDE(%rcx) /* Reset IO tier override to -1 before handling syscall */
1663 movq TH_TASK(%rcx),%rbx /* point to current task */
1665 /* Check for active vtimers in the current task */
1666 TASK_VTIMER_CHECK(%rbx,%rcx)
1669 * We can be here either for a mach, unix machdep or diag syscall,
1670 * as indicated by the syscall class:
1672 movl R64_RAX(%r15), %eax /* syscall number/class */
1674 andl $(SYSCALL_CLASS_MASK), %edx /* syscall class */
1675 cmpl $(SYSCALL_CLASS_MACH<<SYSCALL_CLASS_SHIFT), %edx
1676 je EXT(hndl_mach_scall64)
1677 cmpl $(SYSCALL_CLASS_UNIX<<SYSCALL_CLASS_SHIFT), %edx
1678 je EXT(hndl_unix_scall64)
1679 cmpl $(SYSCALL_CLASS_MDEP<<SYSCALL_CLASS_SHIFT), %edx
1680 je EXT(hndl_mdep_scall64)
1681 cmpl $(SYSCALL_CLASS_DIAG<<SYSCALL_CLASS_SHIFT), %edx
1682 je EXT(hndl_diag_scall64)
1684 /* Syscall class unknown */
1686 CCALL3(i386_exception, $(EXC_SYSCALL), %rax, $1)
1690 Entry(hndl_unix_scall64)
1691 incl TH_SYSCALLS_UNIX(%rcx) /* increment call count */
1694 CCALL1(unix_syscall64, %r15)
1696 * always returns through thread_exception_return
1700 Entry(hndl_mach_scall64)
1701 incl TH_SYSCALLS_MACH(%rcx) /* increment call count */
1704 CCALL1(mach_call_munger64, %r15)
1706 * always returns through thread_exception_return
1711 Entry(hndl_mdep_scall64)
1714 CCALL1(machdep_syscall64, %r15)
1716 * always returns through thread_exception_return
1719 Entry(hndl_diag_scall64)
1720 CCALL1(diagCall64, %r15) // Call diagnostics
1721 test %eax, %eax // What kind of return is this?
1722 je 1f // - branch if bad (zero)
1723 jmp EXT(return_to_user) // Normal return, do not check asts...
1726 CCALL3(i386_exception, $EXC_SYSCALL, $0x6000, $1)
1728 /* TODO assert at all 'C' entry points that we're never operating on the fault stack's alias mapping */
1729 Entry(hndl_machine_check)
1730 /* Adjust SP and savearea to their canonical, non-aliased addresses */
1731 CCALL1(panic_machine_check64, %r15)
1734 Entry(hndl_double_fault)
1735 CCALL1(panic_double_fault64, %r15)