2 * Copyright (c) 1989, 1990 William F. Jolitz.
3 * Copyright (c) 1990 The Regents of the University of California.
4 * Copyright (c) 2007-2018 The FreeBSD Foundation
7 * Portions of this software were developed by A. Joseph Koshy under
8 * sponsorship from the FreeBSD Foundation and Google, Inc.
10 * Portions of this software were developed by
11 * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from
12 * the FreeBSD Foundation.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include "opt_atpic.h"
42 #include "opt_hwpmc_hooks.h"
46 #include <machine/psl.h>
47 #include <machine/asmacros.h>
48 #include <machine/trap.h>
49 #include <machine/specialreg.h>
53 .globl dtrace_invop_jump_addr
55 .type dtrace_invop_jump_addr,@object
56 .size dtrace_invop_jump_addr,8
57 dtrace_invop_jump_addr:
59 .globl dtrace_invop_calltrap_addr
61 .type dtrace_invop_calltrap_addr,@object
62 .size dtrace_invop_calltrap_addr,8
63 dtrace_invop_calltrap_addr:
68 ENTRY(start_exceptions)
71 /*****************************************************************************/
73 /*****************************************************************************/
75 * Trap and fault vector routines.
77 * All traps are 'interrupt gates', SDT_SYSIGT. An interrupt gate pushes
78 * state on the stack but also disables interrupts. This is important for
79 * us for the use of the swapgs instruction. We cannot be interrupted
80 * until the GS.base value is correct. For most traps, we automatically
81 * then enable interrupts if the interrupted context had them enabled.
82 * This is equivalent to the i386 port's use of SDT_SYS386TGT.
84 * The cpu will push a certain amount of state onto the kernel stack for
85 * the current process. See amd64/include/frame.h.
86 * This includes the current RFLAGS (status register, which includes
87 * the interrupt disable state prior to the trap), the code segment register,
88 * and the return instruction pointer are pushed by the cpu. The cpu
89 * will also push an 'error' code for certain traps. We push a dummy
90 * error code for those traps where the cpu doesn't in order to maintain
91 * a consistent frame. We also push a contrived 'trap number'.
93 * The CPU does not push the general registers, so we must do that, and we
94 * must restore them prior to calling 'iret'. The CPU adjusts %cs and %ss
95 * but does not mess with %ds, %es, %gs or %fs. We swap the %gs base for
96 * for the kernel mode operation shortly, without changes to the selector
97 * loaded. Since superuser long mode works with any selectors loaded into
98 * segment registers other then %cs, which makes them mostly unused in long
99 * mode, and kernel does not reference %fs, leave them alone. The segment
100 * registers are reloaded on return to the usermode.
106 /* Traps that we leave interrupts disabled for. */
107 .macro TRAP_NOEN l, trapno
111 X\l: subq $TF_RIP,%rsp
112 movl $\trapno,TF_TRAPNO(%rsp)
113 movq $0,TF_ADDR(%rsp)
118 TRAP_NOEN bpt, T_BPTFLT
120 TRAP_NOEN dtrace_ret, T_DTRACE_RET
123 /* Regular traps; The cpu does not supply tf_err for these. */
124 .macro TRAP l, trapno
130 movl $\trapno,TF_TRAPNO(%rsp)
131 movq $0,TF_ADDR(%rsp)
139 TRAP ill, T_PRIVINFLT
141 TRAP fpusegm, T_FPOPFLT
142 TRAP rsvd, T_RESERVED
143 TRAP fpu, T_ARITHTRAP
146 /* This group of traps have tf_err already pushed by the cpu. */
147 .macro TRAP_ERR l, trapno
148 PTI_ENTRY \l,X\l,has_err=1
153 movl $\trapno,TF_TRAPNO(%rsp)
154 movq $0,TF_ADDR(%rsp)
158 TRAP_ERR tss, T_TSSFLT
159 TRAP_ERR align, T_ALIGNFLT
162 * alltraps entry point. Use swapgs if this is the first time in the
163 * kernel from userland. Reenable interrupts if they were enabled
164 * before the trap. This approximates SDT_SYS386TGT on the i386 port.
168 .type alltraps,@function
170 movq %rdi,TF_RDI(%rsp)
171 testb $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
172 jz 1f /* already running with kernel GS.base */
174 movq PCPU(CURPCB),%rdi
175 andl $~PCB_FULL_IRET,PCB_FLAGS(%rdi)
177 movq %rdx,TF_RDX(%rsp)
178 movq %rax,TF_RAX(%rsp)
179 movq %rcx,TF_RCX(%rsp)
180 testb $SEL_RPL_MASK,TF_CS(%rsp)
182 call handle_ibrs_entry
183 2: testl $PSL_I,TF_RFLAGS(%rsp)
184 jz alltraps_pushregs_no_rax
186 alltraps_pushregs_no_rax:
187 movq %rsi,TF_RSI(%rsp)
190 movq %rbx,TF_RBX(%rsp)
191 movq %rbp,TF_RBP(%rsp)
192 movq %r10,TF_R10(%rsp)
193 movq %r11,TF_R11(%rsp)
194 movq %r12,TF_R12(%rsp)
195 movq %r13,TF_R13(%rsp)
196 movq %r14,TF_R14(%rsp)
197 movq %r15,TF_R15(%rsp)
198 movl $TF_HASSEGS,TF_FLAGS(%rsp)
200 andq $~(PSL_D | PSL_AC),(%rsp)
202 FAKE_MCOUNT(TF_RIP(%rsp))
205 * DTrace Function Boundary Trace (fbt) probes are triggered
206 * by int3 (0xcc) which causes the #BP (T_BPTFLT) breakpoint
207 * interrupt. For all other trap types, just handle them in
210 testb $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
211 jnz calltrap /* ignore userland traps */
212 cmpl $T_BPTFLT,TF_TRAPNO(%rsp)
215 /* Check if there is no DTrace hook registered. */
216 cmpq $0,dtrace_invop_jump_addr
220 * Set our jump address for the jump back in the event that
221 * the breakpoint wasn't caused by DTrace at all.
223 movq $calltrap,dtrace_invop_calltrap_addr(%rip)
225 /* Jump to the code hooked in by DTrace. */
226 jmpq *dtrace_invop_jump_addr
229 .type calltrap,@function
234 jmp doreti /* Handle any pending ASTs */
237 * alltraps_noen entry point. Unlike alltraps above, we want to
238 * leave the interrupts disabled. This corresponds to
239 * SDT_SYS386IGT on the i386 port.
243 .type alltraps_noen,@function
245 movq %rdi,TF_RDI(%rsp)
246 testb $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
247 jz 1f /* already running with kernel GS.base */
249 movq PCPU(CURPCB),%rdi
250 andl $~PCB_FULL_IRET,PCB_FLAGS(%rdi)
252 movq %rdx,TF_RDX(%rsp)
253 movq %rax,TF_RAX(%rsp)
254 movq %rcx,TF_RCX(%rsp)
255 testb $SEL_RPL_MASK,TF_CS(%rsp)
256 jz alltraps_pushregs_no_rax
257 call handle_ibrs_entry
258 jmp alltraps_pushregs_no_rax
262 movl $T_DOUBLEFLT,TF_TRAPNO(%rsp)
263 movq $0,TF_ADDR(%rsp)
265 movq %rdi,TF_RDI(%rsp)
266 movq %rsi,TF_RSI(%rsp)
267 movq %rdx,TF_RDX(%rsp)
268 movq %rcx,TF_RCX(%rsp)
271 movq %rax,TF_RAX(%rsp)
272 movq %rbx,TF_RBX(%rsp)
273 movq %rbp,TF_RBP(%rsp)
274 movq %r10,TF_R10(%rsp)
275 movq %r11,TF_R11(%rsp)
276 movq %r12,TF_R12(%rsp)
277 movq %r13,TF_R13(%rsp)
278 movq %r14,TF_R14(%rsp)
279 movq %r15,TF_R15(%rsp)
281 movl $TF_HASSEGS,TF_FLAGS(%rsp)
283 andq $~(PSL_D | PSL_AC),(%rsp)
285 testb $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
286 jz 1f /* already running with kernel GS.base */
294 call dblfault_handler
300 testb $SEL_RPL_MASK,PTI_CS-2*8(%rsp)
305 movq %rax,PCPU(SAVED_UCR3)
311 PTI_UUENTRY has_err=1
313 movq %rdi,TF_RDI(%rsp)
314 movq %rax,TF_RAX(%rsp)
315 movq %rdx,TF_RDX(%rsp)
316 movq %rcx,TF_RCX(%rsp)
320 movq %rdi,TF_RDI(%rsp) /* free up GP registers */
321 movq %rax,TF_RAX(%rsp)
322 movq %rdx,TF_RDX(%rsp)
323 movq %rcx,TF_RCX(%rsp)
324 testb $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
325 jz page_cr2 /* already running with kernel GS.base */
327 page_u: movq PCPU(CURPCB),%rdi
328 andl $~PCB_FULL_IRET,PCB_FLAGS(%rdi)
329 movq PCPU(SAVED_UCR3),%rax
330 movq %rax,PCB_SAVED_UCR3(%rdi)
331 call handle_ibrs_entry
333 movq %cr2,%rdi /* preserve %cr2 before .. */
334 movq %rdi,TF_ADDR(%rsp) /* enabling interrupts. */
336 movl $T_PAGEFLT,TF_TRAPNO(%rsp)
337 testl $PSL_I,TF_RFLAGS(%rsp)
338 jz alltraps_pushregs_no_rax
340 jmp alltraps_pushregs_no_rax
343 * We have to special-case this one. If we get a trap in doreti() at
344 * the iretq stage, we'll reenter with the wrong gs state. We'll have
345 * to do a special the swapgs in this case even coming from the kernel.
346 * XXX linux has a trap handler for their equivalent of load_gs().
348 * On the stack, we have the hardware interrupt frame to return
349 * to usermode (faulted) and another frame with error code, for
350 * fault. For PTI, copy both frames to the main thread stack.
351 * Handle the potential 16-byte alignment adjustment incurred
352 * during the second fault by copying both frames independently
353 * while unwinding the stack in between.
355 .macro PROTF_ENTRY name,trapno
365 subq $2*PTI_SIZE-3*8,%rax /* no err, %rax, %rdx in faulted frame */
366 MOVE_STACKS (PTI_SIZE / 8)
368 movq PTI_RSP(%rsp),%rsp
369 MOVE_STACKS (PTI_SIZE / 8 - 3)
377 cmpq $doreti_iret,PTI_RIP-2*8(%rsp)
378 je \name\()_pti_doreti
379 testb $SEL_RPL_MASK,PTI_CS-2*8(%rsp) /* %rax, %rdx not yet pushed */
385 movl $\trapno,TF_TRAPNO(%rsp)
389 PROTF_ENTRY missing, T_SEGNPFLT
390 PROTF_ENTRY stk, T_STKFLT
391 PROTF_ENTRY prot, T_PROTFLT
394 movq $0,TF_ADDR(%rsp)
395 movq %rdi,TF_RDI(%rsp) /* free up a GP register */
396 movq %rax,TF_RAX(%rsp)
397 movq %rdx,TF_RDX(%rsp)
398 movq %rcx,TF_RCX(%rsp)
401 leaq doreti_iret(%rip),%rdi
402 cmpq %rdi,TF_RIP(%rsp)
403 je 5f /* kernel but with user gsbase!! */
404 testb $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
405 jz 6f /* already running with kernel GS.base */
406 testb $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
408 cmpw $KUF32SEL,TF_FS(%rsp)
411 1: cmpw $KUG32SEL,TF_GS(%rsp)
415 movq PCPU(CURPCB),%rdi
416 testb $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
418 cmpw $KUF32SEL,TF_FS(%rsp)
420 movq %rax,PCB_FSBASE(%rdi)
421 3: cmpw $KUG32SEL,TF_GS(%rsp)
423 movq %rdx,PCB_GSBASE(%rdi)
424 4: call handle_ibrs_entry
425 orl $PCB_FULL_IRET,PCB_FLAGS(%rdi) /* always full iret from GPF */
428 testl $PSL_I,TF_RFLAGS(%rsp)
429 jz alltraps_pushregs_no_rax
431 jmp alltraps_pushregs_no_rax
434 6: movq PCPU(CURPCB),%rdi
438 * Fast syscall entry point. We enter here with just our new %cs/%ss set,
439 * and the new privilige level. We are still running on the old user stack
440 * pointer. We have to juggle a few things around to find our stack etc.
441 * swapgs gives us access to our PCPU space only.
443 * We do not support invoking this from a custom segment registers,
444 * esp. %cs, %ss, %fs, %gs, e.g. using entries from an LDT.
447 IDTVEC(fast_syscall_pti)
449 movq %rax,PCPU(SCRATCH_RAX)
451 je fast_syscall_common
454 jmp fast_syscall_common
458 movq %rax,PCPU(SCRATCH_RAX)
460 movq %rsp,PCPU(SCRATCH_RSP)
462 /* Now emulate a trapframe. Make the 8 byte alignment odd for call. */
464 /* defer TF_RSP till we have a spare register */
465 movq %r11,TF_RFLAGS(%rsp)
466 movq %rcx,TF_RIP(%rsp) /* %rcx original value is in %r10 */
467 movq PCPU(SCRATCH_RSP),%r11 /* %r11 already saved */
468 movq %r11,TF_RSP(%rsp) /* user stack pointer */
469 movq PCPU(SCRATCH_RAX),%rax
471 * Save a few arg registers early to free them for use in
472 * handle_ibrs_entry(). %r10 is especially tricky. It is not an
473 * arg register, but it holds the arg register %rcx. Profiling
474 * preserves %rcx, but may clobber %r10. Profiling may also
475 * clobber %r11, but %r11 (original %eflags) has been saved.
477 movq %rax,TF_RAX(%rsp) /* syscall number */
478 movq %rdx,TF_RDX(%rsp) /* arg 3 */
479 movq %r10,TF_RCX(%rsp) /* arg 4 */
481 call handle_ibrs_entry
482 movq PCPU(CURPCB),%r11
483 andl $~PCB_FULL_IRET,PCB_FLAGS(%r11)
485 movq $KUDSEL,TF_SS(%rsp)
486 movq $KUCSEL,TF_CS(%rsp)
488 movq %rdi,TF_RDI(%rsp) /* arg 1 */
489 movq %rsi,TF_RSI(%rsp) /* arg 2 */
490 movq %r8,TF_R8(%rsp) /* arg 5 */
491 movq %r9,TF_R9(%rsp) /* arg 6 */
492 movq %rbx,TF_RBX(%rsp) /* C preserved */
493 movq %rbp,TF_RBP(%rsp) /* C preserved */
494 movq %r12,TF_R12(%rsp) /* C preserved */
495 movq %r13,TF_R13(%rsp) /* C preserved */
496 movq %r14,TF_R14(%rsp) /* C preserved */
497 movq %r15,TF_R15(%rsp) /* C preserved */
498 movl $TF_HASSEGS,TF_FLAGS(%rsp)
499 FAKE_MCOUNT(TF_RIP(%rsp))
500 movq PCPU(CURTHREAD),%rdi
501 movq %rsp,TD_FRAME(%rdi)
502 movl TF_RFLAGS(%rsp),%esi
505 1: movq PCPU(CURPCB),%rax
506 /* Disable interrupts before testing PCB_FULL_IRET. */
508 testl $PCB_FULL_IRET,PCB_FLAGS(%rax)
510 /* Check for and handle AST's on return to userland. */
511 movq PCPU(CURTHREAD),%rax
512 testl $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%rax)
514 call handle_ibrs_exit
516 /* Restore preserved registers. */
518 movq TF_RDI(%rsp),%rdi /* bonus; preserve arg 1 */
519 movq TF_RSI(%rsp),%rsi /* bonus: preserve arg 2 */
520 movq TF_RDX(%rsp),%rdx /* return value 2 */
521 movq TF_RAX(%rsp),%rax /* return value 1 */
522 movq TF_RFLAGS(%rsp),%r11 /* original %rflags */
523 movq TF_RIP(%rsp),%rcx /* original %rip */
524 movq TF_RSP(%rsp),%rsp /* user stack pointer */
525 xorl %r8d,%r8d /* zero the rest of GPRs */
535 3: /* AST scheduled. */
541 4: /* Requested full context restore, use doreti for that. */
546 * Here for CYA insurance, in case a "syscall" instruction gets
547 * issued from 32 bit compatibility mode. MSR_CSTAR has to point
548 * to *something* if EFER_SCE is enabled.
550 IDTVEC(fast_syscall32)
554 * DB# handler is very similar to NM#, because 'mov/pop %ss' delay
555 * generation of exception until the next instruction is executed,
556 * which might be a kernel entry. So we must execute the handler
557 * on IST stack and be ready for non-kernel GSBASE.
561 movl $(T_TRCTRAP),TF_TRAPNO(%rsp)
562 movq $0,TF_ADDR(%rsp)
564 movq %rdi,TF_RDI(%rsp)
565 movq %rsi,TF_RSI(%rsp)
566 movq %rdx,TF_RDX(%rsp)
567 movq %rcx,TF_RCX(%rsp)
570 movq %rax,TF_RAX(%rsp)
571 movq %rbx,TF_RBX(%rsp)
572 movq %rbp,TF_RBP(%rsp)
573 movq %r10,TF_R10(%rsp)
574 movq %r11,TF_R11(%rsp)
575 movq %r12,TF_R12(%rsp)
576 movq %r13,TF_R13(%rsp)
577 movq %r14,TF_R14(%rsp)
578 movq %r15,TF_R15(%rsp)
580 movl $TF_HASSEGS,TF_FLAGS(%rsp)
582 andq $~(PSL_D | PSL_AC),(%rsp)
584 testb $SEL_RPL_MASK,TF_CS(%rsp)
585 jnz dbg_fromuserspace
587 * We've interrupted the kernel. Preserve GS.base in %r12,
588 * %cr3 in %r13, and possibly lower half of MSR_IA32_SPEC_CTL in %r14d.
590 movl $MSR_GSBASE,%ecx
595 /* Retrieve and load the canonical value for GS.base. */
596 movq TF_SIZE(%rsp),%rdx
605 1: testl $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
607 movl $MSR_IA32_SPEC_CTRL,%ecx
610 call handle_ibrs_entry
611 2: FAKE_MCOUNT(TF_RIP(%rsp))
615 testl $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
619 movl $MSR_IA32_SPEC_CTRL,%ecx
622 * Put back the preserved MSR_GSBASE value.
624 3: movl $MSR_GSBASE,%ecx
635 * Switch to kernel GSBASE and kernel page table, and copy frame
636 * from the IST stack to the normal kernel stack, since trap()
637 * re-enables interrupts, and since we might trap on DB# while
645 1: movq PCPU(RSP0),%rax
652 call handle_ibrs_entry
653 movq PCPU(CURPCB),%rdi
654 orl $PCB_FULL_IRET,PCB_FLAGS(%rdi)
655 testb $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
657 cmpw $KUF32SEL,TF_FS(%rsp)
660 movq %rax,PCB_FSBASE(%rdi)
661 2: cmpw $KUG32SEL,TF_GS(%rsp)
663 movl $MSR_KGSBASE,%ecx
667 movq %rax,PCB_GSBASE(%rdi)
671 * NMI handling is special.
673 * First, NMIs do not respect the state of the processor's RFLAGS.IF
674 * bit. The NMI handler may be entered at any time, including when
675 * the processor is in a critical section with RFLAGS.IF == 0.
676 * The processor's GS.base value could be invalid on entry to the
679 * Second, the processor treats NMIs specially, blocking further NMIs
680 * until an 'iretq' instruction is executed. We thus need to execute
681 * the NMI handler with interrupts disabled, to prevent a nested interrupt
682 * from executing an 'iretq' instruction and inadvertently taking the
683 * processor out of NMI mode.
685 * Third, the NMI handler runs on its own stack (tss_ist2). The canonical
686 * GS.base value for the processor is stored just above the bottom of its
687 * NMI stack. For NMIs taken from kernel mode, the current value in
688 * the processor's GS.base is saved at entry to C-preserved register %r12,
689 * the canonical value for GS.base is then loaded into the processor, and
690 * the saved value is restored at exit time. For NMIs taken from user mode,
691 * the cheaper 'SWAPGS' instructions are used for swapping GS.base.
696 movl $(T_NMI),TF_TRAPNO(%rsp)
697 movq $0,TF_ADDR(%rsp)
699 movq %rdi,TF_RDI(%rsp)
700 movq %rsi,TF_RSI(%rsp)
701 movq %rdx,TF_RDX(%rsp)
702 movq %rcx,TF_RCX(%rsp)
705 movq %rax,TF_RAX(%rsp)
706 movq %rbx,TF_RBX(%rsp)
707 movq %rbp,TF_RBP(%rsp)
708 movq %r10,TF_R10(%rsp)
709 movq %r11,TF_R11(%rsp)
710 movq %r12,TF_R12(%rsp)
711 movq %r13,TF_R13(%rsp)
712 movq %r14,TF_R14(%rsp)
713 movq %r15,TF_R15(%rsp)
715 movl $TF_HASSEGS,TF_FLAGS(%rsp)
717 andq $~(PSL_D | PSL_AC),(%rsp)
720 testb $SEL_RPL_MASK,TF_CS(%rsp)
721 jnz nmi_fromuserspace
723 * We've interrupted the kernel. Preserve GS.base in %r12,
724 * %cr3 in %r13, and possibly lower half of MSR_IA32_SPEC_CTL in %r14d.
726 movl $MSR_GSBASE,%ecx
731 /* Retrieve and load the canonical value for GS.base. */
732 movq TF_SIZE(%rsp),%rdx
741 1: testl $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
743 movl $MSR_IA32_SPEC_CTRL,%ecx
746 call handle_ibrs_entry
756 1: call handle_ibrs_entry
757 movq PCPU(CURPCB),%rdi
760 orl $PCB_FULL_IRET,PCB_FLAGS(%rdi)
761 testb $CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
763 cmpw $KUF32SEL,TF_FS(%rsp)
766 movq %rax,PCB_FSBASE(%rdi)
767 2: cmpw $KUG32SEL,TF_GS(%rsp)
769 movl $MSR_KGSBASE,%ecx
773 movq %rax,PCB_GSBASE(%rdi)
775 /* Note: this label is also used by ddb and gdb: */
777 FAKE_MCOUNT(TF_RIP(%rsp))
783 * Capture a userspace callchain if needed.
785 * - Check if the current trap was from user mode.
786 * - Check if the current thread is valid.
787 * - Check if the thread requires a user call chain to be
790 * We are still in NMI mode at this point.
793 jz nocallchain /* not from userspace */
794 movq PCPU(CURTHREAD),%rax
795 orq %rax,%rax /* curthread present? */
798 * Move execution to the regular kernel stack, because we
799 * committed to return through doreti.
801 movq %rsp,%rsi /* source stack pointer */
805 movq %rdx,%rdi /* destination stack pointer */
806 shrq $3,%rcx /* trap frame size in long words */
808 andq $~(PSL_D | PSL_AC),(%rsp)
811 movsq /* copy trapframe */
812 movq %rdx,%rsp /* we are on the regular kstack */
814 testl $TDP_CALLCHAIN,TD_PFLAGS(%rax) /* flagged for capture? */
817 * A user callchain is to be captured, so:
818 * - Take the processor out of "NMI" mode by faking an "iret",
819 * to allow for nested NMI interrupts.
820 * - Enable interrupts, so that copyin() can work.
823 pushq %rax /* tf_ss */
824 pushq %rdx /* tf_rsp (on kernel stack) */
825 pushfq /* tf_rflags */
827 pushq %rax /* tf_cs */
828 pushq $outofnmi /* tf_rip */
832 * At this point the processor has exited NMI mode and is running
833 * with interrupts turned off on the normal kernel stack.
835 * If a pending NMI gets recognized at or after this point, it
836 * will cause a kernel callchain to be traced.
838 * We turn interrupts back on, and call the user callchain capture hook.
843 movq PCPU(CURTHREAD),%rdi /* thread */
844 movq $PMC_FN_USER_CALLCHAIN,%rsi /* command */
845 movq %rsp,%rdx /* frame */
851 testl %ebx,%ebx /* %ebx == 0 => return to userland */
854 * Restore speculation control MSR, if preserved.
856 testl $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
860 movl $MSR_IA32_SPEC_CTRL,%ecx
863 * Put back the preserved MSR_GSBASE value.
865 1: movl $MSR_GSBASE,%ecx
870 cmpb $0, nmi_flush_l1d_sw(%rip)
872 call flush_l1d_sw /* bhyve L1TF assist */
879 * MC# handling is similar to NMI.
881 * As with NMIs, machine check exceptions do not respect RFLAGS.IF and
882 * can occur at any time with a GS.base value that does not correspond
883 * to the privilege level in CS.
885 * Machine checks are not unblocked by iretq, but it is best to run
886 * the handler with interrupts disabled since the exception may have
887 * interrupted a critical section.
889 * The MC# handler runs on its own stack (tss_ist3). The canonical
890 * GS.base value for the processor is stored just above the bottom of
891 * its MC# stack. For exceptions taken from kernel mode, the current
892 * value in the processor's GS.base is saved at entry to C-preserved
893 * register %r12, the canonical value for GS.base is then loaded into
894 * the processor, and the saved value is restored at exit time. For
895 * exceptions taken from user mode, the cheaper 'SWAPGS' instructions
896 * are used for swapping GS.base.
901 movl $(T_MCHK),TF_TRAPNO(%rsp)
902 movq $0,TF_ADDR(%rsp)
904 movq %rdi,TF_RDI(%rsp)
905 movq %rsi,TF_RSI(%rsp)
906 movq %rdx,TF_RDX(%rsp)
907 movq %rcx,TF_RCX(%rsp)
910 movq %rax,TF_RAX(%rsp)
911 movq %rbx,TF_RBX(%rsp)
912 movq %rbp,TF_RBP(%rsp)
913 movq %r10,TF_R10(%rsp)
914 movq %r11,TF_R11(%rsp)
915 movq %r12,TF_R12(%rsp)
916 movq %r13,TF_R13(%rsp)
917 movq %r14,TF_R14(%rsp)
918 movq %r15,TF_R15(%rsp)
920 movl $TF_HASSEGS,TF_FLAGS(%rsp)
922 andq $~(PSL_D | PSL_AC),(%rsp)
925 testb $SEL_RPL_MASK,TF_CS(%rsp)
926 jnz mchk_fromuserspace
928 * We've interrupted the kernel. Preserve GS.base in %r12,
929 * %cr3 in %r13, and possibly lower half of MSR_IA32_SPEC_CTL in %r14d.
931 movl $MSR_GSBASE,%ecx
936 /* Retrieve and load the canonical value for GS.base. */
937 movq TF_SIZE(%rsp),%rdx
946 1: testl $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
948 movl $MSR_IA32_SPEC_CTRL,%ecx
951 call handle_ibrs_entry
961 1: call handle_ibrs_entry
962 /* Note: this label is also used by ddb and gdb: */
964 FAKE_MCOUNT(TF_RIP(%rsp))
968 testl %ebx,%ebx /* %ebx == 0 => return to userland */
971 * Restore speculation control MSR, if preserved.
973 testl $CPUID_STDEXT3_IBPB,cpu_stdext_feature3(%rip)
977 movl $MSR_IA32_SPEC_CTRL,%ecx
980 * Put back the preserved MSR_GSBASE value.
982 1: movl $MSR_GSBASE,%ecx
992 ENTRY(fork_trampoline)
993 movq %r12,%rdi /* function */
994 movq %rbx,%rsi /* arg1 */
995 movq %rsp,%rdx /* trapframe pointer */
998 jmp doreti /* Handle any ASTs */
1001 * To efficiently implement classification of trap and interrupt handlers
1002 * for profiling, there must be only trap handlers between the labels btrap
1003 * and bintr, and only interrupt handlers between the labels bintr and
1004 * eintr. This is implemented (partly) by including files that contain
1005 * some of the handlers. Before including the files, set up a normal asm
1006 * environment so that the included files doen't need to know that they are
1010 #ifdef COMPAT_FREEBSD32
1016 #include <amd64/ia32/ia32_exception.S>
1025 #include <amd64/amd64/apic_vector.S>
1033 #include <amd64/amd64/atpic_vector.S>
1040 * void doreti(struct trapframe)
1042 * Handle return from interrupts, traps and syscalls.
1046 .type doreti,@function
1049 FAKE_MCOUNT($bintr) /* init "from" bintr -> doreti */
1051 * Check if ASTs can be handled now.
1053 testb $SEL_RPL_MASK,TF_CS(%rsp) /* are we returning to user mode? */
1054 jz doreti_exit /* can't handle ASTs now if not */
1058 * Check for ASTs atomically with returning. Disabling CPU
1059 * interrupts provides sufficient locking even in the SMP case,
1060 * since we will be informed of any new ASTs by an IPI.
1063 movq PCPU(CURTHREAD),%rax
1064 testl $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%rax)
1067 movq %rsp,%rdi /* pass a pointer to the trapframe */
1072 * doreti_exit: pop registers, iret.
1074 * The segment register pop is a special case, since it may
1075 * fault if (for example) a sigreturn specifies bad segment
1076 * registers. The fault is handled in trap.c.
1080 movq PCPU(CURPCB),%r8
1083 * Do not reload segment registers for kernel.
1084 * Since we do not reload segments registers with sane
1085 * values on kernel entry, descriptors referenced by
1086 * segments registers might be not valid. This is fatal
1087 * for user mode, but is not a problem for the kernel.
1089 testb $SEL_RPL_MASK,TF_CS(%rsp)
1091 testl $PCB_FULL_IRET,PCB_FLAGS(%r8)
1093 andl $~PCB_FULL_IRET,PCB_FLAGS(%r8)
1094 testl $TF_HASSEGS,TF_FLAGS(%rsp)
1098 /* Restore %fs and fsbase */
1099 movw TF_FS(%rsp),%ax
1105 movl $MSR_FSBASE,%ecx
1106 movl PCB_FSBASE(%r8),%eax
1107 movl PCB_FSBASE+4(%r8),%edx
1112 /* Restore %gs and gsbase */
1113 movw TF_GS(%rsp),%si
1116 movl $MSR_GSBASE,%ecx
1117 /* Save current kernel %gs base into %r12d:%r13d */
1124 /* Save user %gs base into %r14d:%r15d */
1128 /* Restore kernel %gs base */
1134 * Restore user %gs base, either from PCB if used for TLS, or
1135 * from the previously saved msr read.
1137 movl $MSR_KGSBASE,%ecx
1140 movl PCB_GSBASE(%r8),%eax
1141 movl PCB_GSBASE+4(%r8),%edx
1148 wrmsr /* May trap if non-canonical, but only for TLS. */
1151 movw TF_ES(%rsp),%es
1154 movw TF_DS(%rsp),%ds
1157 testb $SEL_RPL_MASK,TF_CS(%rsp) /* Did we come from kernel? */
1158 jz 2f /* keep running with kernel GS.base */
1160 call handle_ibrs_exit_rs
1165 movq PCPU(PTI_RSP0),%rdx
1167 movq %rax,PTI_RAX(%rdx)
1169 movq %rax,PTI_RDX(%rdx)
1170 movq TF_RIP(%rsp),%rax
1171 movq %rax,PTI_RIP(%rdx)
1172 movq TF_CS(%rsp),%rax
1173 movq %rax,PTI_CS(%rdx)
1174 movq TF_RFLAGS(%rsp),%rax
1175 movq %rax,PTI_RFLAGS(%rdx)
1176 movq TF_RSP(%rsp),%rax
1177 movq %rax,PTI_RSP(%rdx)
1178 movq TF_SS(%rsp),%rax
1179 movq %rax,PTI_SS(%rdx)
1180 movq PCPU(UCR3),%rax
1189 2: addq $TF_RIP,%rsp
1196 movw %ax,TF_DS(%rsp)
1197 movw %ax,TF_ES(%rsp)
1198 movw $KUF32SEL,TF_FS(%rsp)
1199 movw $KUG32SEL,TF_GS(%rsp)
1203 * doreti_iret_fault. Alternative return code for
1204 * the case where we get a fault in the doreti_exit code
1205 * above. trap() (amd64/amd64/trap.c) catches this specific
1206 * case, sends the process a signal and continues in the
1207 * corresponding place in the code below.
1210 .globl doreti_iret_fault
1212 subq $TF_RIP,%rsp /* space including tf_err, tf_trapno */
1213 movq %rax,TF_RAX(%rsp)
1214 movq %rdx,TF_RDX(%rsp)
1215 movq %rcx,TF_RCX(%rsp)
1216 call handle_ibrs_entry
1217 testb $SEL_RPL_MASK,TF_CS(%rsp)
1222 movl $TF_HASSEGS,TF_FLAGS(%rsp)
1223 movq %rdi,TF_RDI(%rsp)
1224 movq %rsi,TF_RSI(%rsp)
1225 movq %r8,TF_R8(%rsp)
1226 movq %r9,TF_R9(%rsp)
1227 movq %rbx,TF_RBX(%rsp)
1228 movq %rbp,TF_RBP(%rsp)
1229 movq %r10,TF_R10(%rsp)
1230 movq %r11,TF_R11(%rsp)
1231 movq %r12,TF_R12(%rsp)
1232 movq %r13,TF_R13(%rsp)
1233 movq %r14,TF_R14(%rsp)
1234 movq %r15,TF_R15(%rsp)
1235 movl $T_PROTFLT,TF_TRAPNO(%rsp)
1236 movq $0,TF_ERR(%rsp) /* XXX should be the error code */
1237 movq $0,TF_ADDR(%rsp)
1238 FAKE_MCOUNT(TF_RIP(%rsp))
1242 .globl ds_load_fault
1244 movl $T_PROTFLT,TF_TRAPNO(%rsp)
1245 testb $SEL_RPL_MASK,TF_CS(%rsp)
1251 movw $KUDSEL,TF_DS(%rsp)
1255 .globl es_load_fault
1257 movl $T_PROTFLT,TF_TRAPNO(%rsp)
1258 testl $PSL_I,TF_RFLAGS(%rsp)
1264 movw $KUDSEL,TF_ES(%rsp)
1268 .globl fs_load_fault
1270 testl $PSL_I,TF_RFLAGS(%rsp)
1274 movl $T_PROTFLT,TF_TRAPNO(%rsp)
1277 movw $KUF32SEL,TF_FS(%rsp)
1281 .globl gs_load_fault
1284 movl $T_PROTFLT,TF_TRAPNO(%rsp)
1285 testl $PSL_I,TF_RFLAGS(%rsp)
1291 movw $KUG32SEL,TF_GS(%rsp)
1295 .globl fsbase_load_fault
1297 movl $T_PROTFLT,TF_TRAPNO(%rsp)
1298 testl $PSL_I,TF_RFLAGS(%rsp)
1304 movq PCPU(CURTHREAD),%r8
1305 movq TD_PCB(%r8),%r8
1306 movq $0,PCB_FSBASE(%r8)
1310 .globl gsbase_load_fault
1312 movl $T_PROTFLT,TF_TRAPNO(%rsp)
1313 testl $PSL_I,TF_RFLAGS(%rsp)
1319 movq PCPU(CURTHREAD),%r8
1320 movq TD_PCB(%r8),%r8
1321 movq $0,PCB_GSBASE(%r8)
1325 ENTRY(end_exceptions)