2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (C) 1994, David Greenman
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the University of Utah, and William Jolitz.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * from: @(#)trap.c 7.4 (Berkeley) 5/13/91
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * AMD64 Trap and System call handling
49 #include "opt_clock.h"
50 #include "opt_compat.h"
52 #include "opt_hwpmc_hooks.h"
55 #include "opt_stack.h"
57 #include <sys/param.h>
59 #include <sys/systm.h>
61 #include <sys/pioctl.h>
62 #include <sys/ptrace.h>
64 #include <sys/kernel.h>
67 #include <sys/mutex.h>
68 #include <sys/resourcevar.h>
69 #include <sys/signalvar.h>
70 #include <sys/syscall.h>
71 #include <sys/sysctl.h>
72 #include <sys/sysent.h>
74 #include <sys/vmmeter.h>
76 #include <sys/pmckern.h>
77 PMC_SOFT_DEFINE( , , page_fault, all);
78 PMC_SOFT_DEFINE( , , page_fault, read);
79 PMC_SOFT_DEFINE( , , page_fault, write);
83 #include <vm/vm_param.h>
85 #include <vm/vm_kern.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_extern.h>
90 #include <machine/cpu.h>
91 #include <machine/intr_machdep.h>
93 #include <machine/md_var.h>
94 #include <machine/pcb.h>
96 #include <machine/smp.h>
98 #include <machine/stack.h>
99 #include <machine/trap.h>
100 #include <machine/tss.h>
103 #include <sys/dtrace_bsd.h>
106 extern inthand_t IDTVEC(bpt), IDTVEC(bpt_pti), IDTVEC(dbg),
107 IDTVEC(fast_syscall), IDTVEC(fast_syscall_pti), IDTVEC(fast_syscall32),
108 IDTVEC(int0x80_syscall_pti), IDTVEC(int0x80_syscall);
110 void __noinline trap(struct trapframe *frame);
111 void trap_check(struct trapframe *frame);
112 void dblfault_handler(struct trapframe *frame);
114 static int trap_pfault(struct trapframe *, int);
115 static void trap_fatal(struct trapframe *, vm_offset_t);
117 #define MAX_TRAP_MSG 32
118 static char *trap_msg[] = {
120 "privileged instruction fault", /* 1 T_PRIVINFLT */
122 "breakpoint instruction fault", /* 3 T_BPTFLT */
125 "arithmetic trap", /* 6 T_ARITHTRAP */
128 "general protection fault", /* 9 T_PROTFLT */
129 "debug exception", /* 10 T_TRCTRAP */
131 "page fault", /* 12 T_PAGEFLT */
133 "alignment fault", /* 14 T_ALIGNFLT */
137 "integer divide fault", /* 18 T_DIVIDE */
138 "non-maskable interrupt trap", /* 19 T_NMI */
139 "overflow trap", /* 20 T_OFLOW */
140 "FPU bounds check fault", /* 21 T_BOUND */
141 "FPU device not available", /* 22 T_DNA */
142 "double fault", /* 23 T_DOUBLEFLT */
143 "FPU operand fetch fault", /* 24 T_FPOPFLT */
144 "invalid TSS fault", /* 25 T_TSSFLT */
145 "segment not present fault", /* 26 T_SEGNPFLT */
146 "stack fault", /* 27 T_STKFLT */
147 "machine check trap", /* 28 T_MCHK */
148 "SIMD floating-point exception", /* 29 T_XMMFLT */
149 "reserved (unknown) fault", /* 30 T_RESERVED */
150 "", /* 31 unused (reserved) */
151 "DTrace pid return trap", /* 32 T_DTRACE_RET */
154 static int prot_fault_translation;
155 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN,
156 &prot_fault_translation, 0,
157 "Select signal to deliver on protection fault");
158 static int uprintf_signal;
159 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RWTUN,
161 "Print debugging information on trap signal to ctty");
164 * Control L1D flush on return from NMI.
166 * Tunable can be set to the following values:
167 * 0 - only enable flush on return from NMI if required by vmm.ko (default)
168 * >1 - always flush on return from NMI.
170 * Post-boot, the sysctl indicates if flushing is currently enabled.
172 int nmi_flush_l1d_sw;
173 SYSCTL_INT(_machdep, OID_AUTO, nmi_flush_l1d_sw, CTLFLAG_RWTUN,
174 &nmi_flush_l1d_sw, 0,
175 "Flush L1 Data Cache on NMI exit, software bhyve L1TF mitigation assist");
178 * Exception, fault, and trap interface to the FreeBSD kernel.
179 * This common code is called from assembly language IDT gate entry
180 * routines that prepare a suitable stack frame, and restore this
181 * frame after the exception has been processed.
185 trap(struct trapframe *frame)
190 register_t addr, dr6;
202 type = frame->tf_trapno;
205 /* Handler for NMI IPIs used for stopping CPUs. */
206 if (type == T_NMI && ipi_nmi_handler() == 0)
217 if (type == T_RESERVED) {
218 trap_fatal(frame, 0);
225 * CPU PMCs interrupt using an NMI. If the PMC module is
226 * active, pass the 'rip' value to the PMC module's interrupt
227 * handler. A non-zero return value from the handler means that
228 * the NMI was consumed by it and we can return immediately.
230 if (pmc_intr != NULL &&
231 (*pmc_intr)(frame) != 0)
236 if (stack_nmi_handler(frame) != 0)
241 if ((frame->tf_rflags & PSL_I) == 0) {
243 * Buggy application or kernel code has disabled
244 * interrupts and then trapped. Enabling interrupts
245 * now is wrong, but it is better than running with
246 * interrupts disabled until they are accidentally
249 if (TRAPF_USERMODE(frame))
251 "pid %ld (%s): trap %d with interrupts disabled\n",
252 (long)curproc->p_pid, curthread->td_name, type);
253 else if (type != T_NMI && type != T_BPTFLT &&
256 * XXX not quite right, since this may be for a
257 * multiple fault in user mode.
259 printf("kernel trap %d with interrupts disabled\n",
263 * We shouldn't enable interrupts while holding a
266 if (td->td_md.md_spinlock_count == 0)
271 if (TRAPF_USERMODE(frame)) {
275 td->td_frame = frame;
276 addr = frame->tf_rip;
277 if (td->td_cowgen != p->p_cowgen)
278 thread_cow_update(td);
281 case T_PRIVINFLT: /* privileged instruction fault */
286 case T_BPTFLT: /* bpt instruction fault */
289 if (dtrace_pid_probe_ptr != NULL &&
290 dtrace_pid_probe_ptr(frame) == 0)
297 case T_TRCTRAP: /* debug exception */
302 if ((dr6 & DBREG_DR6_BS) != 0) {
303 PROC_LOCK(td->td_proc);
304 if ((td->td_dbgflags & TDB_STEP) != 0) {
305 td->td_frame->tf_rflags &= ~PSL_T;
306 td->td_dbgflags &= ~TDB_STEP;
308 PROC_UNLOCK(td->td_proc);
312 case T_ARITHTRAP: /* arithmetic trap */
313 ucode = fputrap_x87();
319 case T_PROTFLT: /* general protection fault */
323 case T_STKFLT: /* stack fault */
324 case T_SEGNPFLT: /* segment not present fault */
328 case T_TSSFLT: /* invalid TSS fault */
336 case T_DOUBLEFLT: /* double fault */
342 case T_PAGEFLT: /* page fault */
344 * Emulator can take care about this trap?
346 if (*p->p_sysent->sv_trap != NULL &&
347 (*p->p_sysent->sv_trap)(td) == 0)
350 addr = frame->tf_addr;
351 signo = trap_pfault(frame, TRUE);
356 if (signo == SIGSEGV) {
358 } else if (prot_fault_translation == 0) {
360 * Autodetect. This check also covers
361 * the images without the ABI-tag ELF
364 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD &&
365 p->p_osrel >= P_OSREL_SIGSEGV) {
372 } else if (prot_fault_translation == 1) {
374 * Always compat mode.
380 * Always SIGSEGV mode.
387 case T_DIVIDE: /* integer divide fault */
394 nmi_handle_intr(type, frame);
398 case T_OFLOW: /* integer overflow fault */
403 case T_BOUND: /* bounds check fault */
409 /* transparent fault (due to context switch "late") */
410 KASSERT(PCB_USER_FPU(td->td_pcb),
411 ("kernel FPU ctx has leaked"));
415 case T_FPOPFLT: /* FPU operand fetch fault */
420 case T_XMMFLT: /* SIMD floating-point exception */
421 ucode = fputrap_sse();
429 if (dtrace_return_probe_ptr != NULL)
430 dtrace_return_probe_ptr(frame);
437 KASSERT(cold || td->td_ucred != NULL,
438 ("kernel trap doesn't have ucred"));
440 case T_PAGEFLT: /* page fault */
441 (void) trap_pfault(frame, FALSE);
445 if (PCB_USER_FPU(td->td_pcb))
446 panic("Unregistered use of FPU in kernel");
450 case T_ARITHTRAP: /* arithmetic trap */
451 case T_XMMFLT: /* SIMD floating-point exception */
452 case T_FPOPFLT: /* FPU operand fetch fault */
454 * For now, supporting kernel handler
455 * registration for FPU traps is overkill.
457 trap_fatal(frame, 0);
460 case T_STKFLT: /* stack fault */
461 case T_PROTFLT: /* general protection fault */
462 case T_SEGNPFLT: /* segment not present fault */
463 if (td->td_intr_nesting_level != 0)
467 * Invalid segment selectors and out of bounds
468 * %rip's and %rsp's can be set up in user mode.
469 * This causes a fault in kernel mode when the
470 * kernel tries to return to user mode. We want
471 * to get this fault so that we can fix the
472 * problem here and not have to check all the
473 * selectors and pointers when the user changes
476 * In case of PTI, the IRETQ faulted while the
477 * kernel used the pti stack, and exception
478 * frame records %rsp value pointing to that
479 * stack. If we return normally to
480 * doreti_iret_fault, the trapframe is
481 * reconstructed on pti stack, and calltrap()
482 * called on it as well. Due to the very
483 * limited pti stack size, kernel does not
484 * survive for too long. Switch to the normal
485 * thread stack for the trap handling.
487 * Magic '5' is the number of qwords occupied by
488 * the hardware trap frame.
490 if (frame->tf_rip == (long)doreti_iret) {
491 frame->tf_rip = (long)doreti_iret_fault;
492 if ((PCPU_GET(curpmap)->pm_ucr3 !=
494 (frame->tf_rsp == (uintptr_t)PCPU_GET(
495 pti_rsp0) - 5 * sizeof(register_t))) {
496 frame->tf_rsp = PCPU_GET(rsp0) - 5 *
501 if (frame->tf_rip == (long)ld_ds) {
502 frame->tf_rip = (long)ds_load_fault;
505 if (frame->tf_rip == (long)ld_es) {
506 frame->tf_rip = (long)es_load_fault;
509 if (frame->tf_rip == (long)ld_fs) {
510 frame->tf_rip = (long)fs_load_fault;
513 if (frame->tf_rip == (long)ld_gs) {
514 frame->tf_rip = (long)gs_load_fault;
517 if (frame->tf_rip == (long)ld_gsbase) {
518 frame->tf_rip = (long)gsbase_load_fault;
521 if (frame->tf_rip == (long)ld_fsbase) {
522 frame->tf_rip = (long)fsbase_load_fault;
525 if (curpcb->pcb_onfault != NULL) {
526 frame->tf_rip = (long)curpcb->pcb_onfault;
533 * PSL_NT can be set in user mode and isn't cleared
534 * automatically when the kernel is entered. This
535 * causes a TSS fault when the kernel attempts to
536 * `iret' because the TSS link is uninitialized. We
537 * want to get this fault so that we can fix the
538 * problem here and not every time the kernel is
541 if (frame->tf_rflags & PSL_NT) {
542 frame->tf_rflags &= ~PSL_NT;
547 case T_TRCTRAP: /* debug exception */
548 /* Clear any pending debug events. */
553 * Ignore debug register exceptions due to
554 * accesses in the user's address space, which
555 * can happen under several conditions such as
556 * if a user sets a watchpoint on a buffer and
557 * then passes that buffer to a system call.
558 * We still want to get TRCTRAPS for addresses
559 * in kernel space because that is useful when
560 * debugging the kernel.
562 if (user_dbreg_trap(dr6))
566 * Malicious user code can configure a debug
567 * register watchpoint to trap on data access
568 * to the top of stack and then execute 'pop
569 * %ss; int 3'. Due to exception deferral for
570 * 'pop %ss', the CPU will not interrupt 'int
571 * 3' to raise the DB# exception for the debug
572 * register but will postpone the DB# until
573 * execution of the first instruction of the
574 * BP# handler (in kernel mode). Normally the
575 * previous check would ignore DB# exceptions
576 * for watchpoints on user addresses raised in
577 * kernel mode. However, some CPU errata
578 * include cases where DB# exceptions do not
579 * properly set bits in %dr6, e.g. Haswell
580 * HSD23 and Skylake-X SKZ24.
582 * A deferred DB# can also be raised on the
583 * first instructions of system call entry
584 * points or single-step traps via similar use
585 * of 'pop %ss' or 'mov xxx, %ss'.
589 (uintptr_t)IDTVEC(fast_syscall_pti) ||
590 #ifdef COMPAT_FREEBSD32
592 (uintptr_t)IDTVEC(int0x80_syscall_pti) ||
594 frame->tf_rip == (uintptr_t)IDTVEC(bpt_pti))
598 (uintptr_t)IDTVEC(fast_syscall) ||
599 #ifdef COMPAT_FREEBSD32
601 (uintptr_t)IDTVEC(int0x80_syscall) ||
603 frame->tf_rip == (uintptr_t)IDTVEC(bpt))
606 if (frame->tf_rip == (uintptr_t)IDTVEC(dbg) ||
607 /* Needed for AMD. */
608 frame->tf_rip == (uintptr_t)IDTVEC(fast_syscall32))
611 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
615 * If KDB is enabled, let it handle the debugger trap.
616 * Otherwise, debugger traps "can't happen".
619 if (kdb_trap(type, dr6, frame))
626 nmi_handle_intr(type, frame);
631 trap_fatal(frame, 0);
635 /* Translate fault for emulators (e.g. Linux) */
636 if (*p->p_sysent->sv_transtrap != NULL)
637 signo = (*p->p_sysent->sv_transtrap)(signo, type);
639 ksiginfo_init_trap(&ksi);
640 ksi.ksi_signo = signo;
641 ksi.ksi_code = ucode;
642 ksi.ksi_trapno = type;
643 ksi.ksi_addr = (void *)addr;
644 if (uprintf_signal) {
645 uprintf("pid %d comm %s: signal %d err %lx code %d type %d "
646 "addr 0x%lx rsp 0x%lx rip 0x%lx "
647 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
648 p->p_pid, p->p_comm, signo, frame->tf_err, ucode, type,
649 addr, frame->tf_rsp, frame->tf_rip,
650 fubyte((void *)(frame->tf_rip + 0)),
651 fubyte((void *)(frame->tf_rip + 1)),
652 fubyte((void *)(frame->tf_rip + 2)),
653 fubyte((void *)(frame->tf_rip + 3)),
654 fubyte((void *)(frame->tf_rip + 4)),
655 fubyte((void *)(frame->tf_rip + 5)),
656 fubyte((void *)(frame->tf_rip + 6)),
657 fubyte((void *)(frame->tf_rip + 7)));
659 KASSERT((read_rflags() & PSL_I) != 0, ("interrupts disabled"));
660 trapsignal(td, &ksi);
664 KASSERT(PCB_USER_FPU(td->td_pcb),
665 ("Return from trap with kernel FPU ctx leaked"));
669 * Ensure that we ignore any DTrace-induced faults. This function cannot
670 * be instrumented, so it cannot generate such faults itself.
673 trap_check(struct trapframe *frame)
677 if (dtrace_trap_func != NULL &&
678 (*dtrace_trap_func)(frame, frame->tf_trapno) != 0)
685 trap_is_smap(struct trapframe *frame)
689 * A page fault on a userspace address is classified as
691 * - SMAP is supported;
692 * - kernel mode accessed present data page;
693 * - rflags.AC was cleared.
694 * Kernel must never access user space with rflags.AC cleared
695 * if SMAP is enabled.
697 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 &&
698 (frame->tf_err & (PGEX_P | PGEX_U | PGEX_I | PGEX_RSV)) ==
699 PGEX_P && (frame->tf_rflags & PSL_AC) == 0);
703 trap_is_pti(struct trapframe *frame)
706 return (PCPU_GET(curpmap)->pm_ucr3 != PMAP_NO_CR3 &&
707 pg_nx != 0 && (frame->tf_err & (PGEX_P | PGEX_W |
708 PGEX_U | PGEX_I)) == (PGEX_P | PGEX_U | PGEX_I) &&
709 (curpcb->pcb_saved_ucr3 & ~CR3_PCID_MASK) ==
710 (PCPU_GET(curpmap)->pm_cr3 & ~CR3_PCID_MASK));
714 trap_pfault(struct trapframe *frame, int usermode)
726 eva = frame->tf_addr;
728 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
730 * Due to both processor errata and lazy TLB invalidation when
731 * access restrictions are removed from virtual pages, memory
732 * accesses that are allowed by the physical mapping layer may
733 * nonetheless cause one spurious page fault per virtual page.
734 * When the thread is executing a "no faulting" section that
735 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
736 * every page fault is treated as a spurious page fault,
737 * unless it accesses the same virtual address as the most
738 * recent page fault within the same "no faulting" section.
740 if (td->td_md.md_spurflt_addr != eva ||
741 (td->td_pflags & TDP_RESETSPUR) != 0) {
743 * Do nothing to the TLB. A stale TLB entry is
744 * flushed automatically by a page fault.
746 td->td_md.md_spurflt_addr = eva;
747 td->td_pflags &= ~TDP_RESETSPUR;
752 * If we get a page fault while in a critical section, then
753 * it is most likely a fatal kernel page fault. The kernel
754 * is already going to panic trying to get a sleep lock to
755 * do the VM lookup, so just consider it a fatal trap so the
756 * kernel can print out a useful trap message and even get
759 * If we get a page fault while holding a non-sleepable
760 * lock, then it is most likely a fatal kernel page fault.
761 * If WITNESS is enabled, then it's going to whine about
762 * bogus LORs with various VM locks, so just skip to the
763 * fatal trap handling directly.
765 if (td->td_critnest != 0 ||
766 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
767 "Kernel page fault") != 0) {
768 trap_fatal(frame, eva);
772 va = trunc_page(eva);
773 if (va >= VM_MIN_KERNEL_ADDRESS) {
775 * Don't allow user-mode faults in kernel address space.
782 map = &p->p_vmspace->vm_map;
785 * When accessing a usermode address, kernel must be
786 * ready to accept the page fault, and provide a
787 * handling routine. Since accessing the address
788 * without the handler is a bug, do not try to handle
789 * it normally, and panic immediately.
791 * If SMAP is enabled, filter SMAP faults also,
792 * because illegal access might occur to the mapped
793 * user address, causing infinite loop.
795 if (!usermode && (td->td_intr_nesting_level != 0 ||
796 trap_is_smap(frame) || curpcb->pcb_onfault == NULL)) {
797 trap_fatal(frame, eva);
803 * If the trap was caused by errant bits in the PTE then panic.
805 if (frame->tf_err & PGEX_RSV) {
806 trap_fatal(frame, eva);
811 * User-mode protection key violation (PKU). May happen
812 * either from usermode or from kernel if copyin accessed
813 * key-protected mapping.
815 if ((frame->tf_err & PGEX_PK) != 0) {
816 if (eva > VM_MAXUSER_ADDRESS) {
817 trap_fatal(frame, eva);
820 rv = KERN_PROTECTION_FAILURE;
825 * If nx protection of the usermode portion of kernel page
826 * tables caused trap, panic.
828 if (usermode && trap_is_pti(frame))
829 panic("PTI: pid %d comm %s tf_err %#lx", p->p_pid,
830 p->p_comm, frame->tf_err);
833 * PGEX_I is defined only if the execute disable bit capability is
834 * supported and enabled.
836 if (frame->tf_err & PGEX_W)
837 ftype = VM_PROT_WRITE;
838 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
839 ftype = VM_PROT_EXECUTE;
841 ftype = VM_PROT_READ;
843 /* Fault in the page. */
844 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
845 if (rv == KERN_SUCCESS) {
847 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
848 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
849 if (ftype == VM_PROT_READ)
850 PMC_SOFT_CALL_TF( , , page_fault, read,
853 PMC_SOFT_CALL_TF( , , page_fault, write,
861 if (td->td_intr_nesting_level == 0 &&
862 curpcb->pcb_onfault != NULL) {
863 frame->tf_rip = (long)curpcb->pcb_onfault;
866 trap_fatal(frame, eva);
869 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
873 trap_fatal(frame, eva)
874 struct trapframe *frame;
879 struct soft_segment_descriptor softseg;
885 code = frame->tf_err;
886 type = frame->tf_trapno;
887 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
890 if (type <= MAX_TRAP_MSG)
891 msg = trap_msg[type];
894 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
895 TRAPF_USERMODE(frame) ? "user" : "kernel");
897 /* two separate prints in case of a trap on an unmapped page */
898 printf("cpuid = %d; ", PCPU_GET(cpuid));
899 printf("apic id = %02x\n", PCPU_GET(apic_id));
901 if (type == T_PAGEFLT) {
902 printf("fault virtual address = 0x%lx\n", eva);
903 printf("fault code = %s %s %s%s%s, %s\n",
904 code & PGEX_U ? "user" : "supervisor",
905 code & PGEX_W ? "write" : "read",
906 code & PGEX_I ? "instruction" : "data",
907 code & PGEX_PK ? " prot key" : " ",
908 code & PGEX_SGX ? " SGX" : " ",
909 code & PGEX_RSV ? "reserved bits in PTE" :
910 code & PGEX_P ? "protection violation" : "page not present");
912 printf("instruction pointer = 0x%lx:0x%lx\n",
913 frame->tf_cs & 0xffff, frame->tf_rip);
914 ss = frame->tf_ss & 0xffff;
915 printf("stack pointer = 0x%x:0x%lx\n", ss, frame->tf_rsp);
916 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
917 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
918 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
919 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
920 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
922 printf("processor eflags = ");
923 if (frame->tf_rflags & PSL_T)
924 printf("trace trap, ");
925 if (frame->tf_rflags & PSL_I)
926 printf("interrupt enabled, ");
927 if (frame->tf_rflags & PSL_NT)
928 printf("nested task, ");
929 if (frame->tf_rflags & PSL_RF)
931 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
932 printf("current process = %d (%s)\n",
933 curproc->p_pid, curthread->td_name);
936 if (debugger_on_trap) {
937 kdb_why = KDB_WHY_TRAP;
938 handled = kdb_trap(type, 0, frame);
939 kdb_why = KDB_WHY_UNSET;
944 printf("trap number = %d\n", type);
945 if (type <= MAX_TRAP_MSG)
946 panic("%s", trap_msg[type]);
948 panic("unknown/reserved trap");
952 * Double fault handler. Called when a fault occurs while writing
953 * a frame for a trap/exception onto the stack. This usually occurs
954 * when the stack overflows (such is the case with infinite recursion,
958 dblfault_handler(struct trapframe *frame)
961 if (dtrace_doubletrap_func != NULL)
962 (*dtrace_doubletrap_func)();
964 printf("\nFatal double fault\n"
965 "rip %#lx rsp %#lx rbp %#lx\n"
966 "rax %#lx rdx %#lx rbx %#lx\n"
967 "rcx %#lx rsi %#lx rdi %#lx\n"
968 "r8 %#lx r9 %#lx r10 %#lx\n"
969 "r11 %#lx r12 %#lx r13 %#lx\n"
970 "r14 %#lx r15 %#lx rflags %#lx\n"
971 "cs %#lx ss %#lx ds %#hx es %#hx fs %#hx gs %#hx\n"
972 "fsbase %#lx gsbase %#lx kgsbase %#lx\n",
973 frame->tf_rip, frame->tf_rsp, frame->tf_rbp,
974 frame->tf_rax, frame->tf_rdx, frame->tf_rbx,
975 frame->tf_rcx, frame->tf_rdi, frame->tf_rsi,
976 frame->tf_r8, frame->tf_r9, frame->tf_r10,
977 frame->tf_r11, frame->tf_r12, frame->tf_r13,
978 frame->tf_r14, frame->tf_r15, frame->tf_rflags,
979 frame->tf_cs, frame->tf_ss, frame->tf_ds, frame->tf_es,
980 frame->tf_fs, frame->tf_gs,
981 rdmsr(MSR_FSBASE), rdmsr(MSR_GSBASE), rdmsr(MSR_KGSBASE));
983 /* two separate prints in case of a trap on an unmapped page */
984 printf("cpuid = %d; ", PCPU_GET(cpuid));
985 printf("apic id = %02x\n", PCPU_GET(apic_id));
987 panic("double fault");
990 static int __noinline
991 cpu_fetch_syscall_args_fallback(struct thread *td, struct syscall_args *sa)
994 struct trapframe *frame;
997 int reg, regcnt, error;
1000 frame = td->td_frame;
1004 sa->code = frame->tf_rax;
1006 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
1007 sa->code = frame->tf_rdi;
1012 if (sa->code >= p->p_sysent->sv_size)
1013 sa->callp = &p->p_sysent->sv_table[0];
1015 sa->callp = &p->p_sysent->sv_table[sa->code];
1017 sa->narg = sa->callp->sy_narg;
1018 KASSERT(sa->narg <= nitems(sa->args), ("Too many syscall arguments!"));
1019 argp = &frame->tf_rdi;
1021 memcpy(sa->args, argp, sizeof(sa->args[0]) * NARGREGS);
1022 if (sa->narg > regcnt) {
1023 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
1024 error = copyin(params, &sa->args[regcnt],
1025 (sa->narg - regcnt) * sizeof(sa->args[0]));
1026 if (__predict_false(error != 0))
1030 td->td_retval[0] = 0;
1031 td->td_retval[1] = frame->tf_rdx;
1037 cpu_fetch_syscall_args(struct thread *td)
1040 struct trapframe *frame;
1041 struct syscall_args *sa;
1044 frame = td->td_frame;
1047 sa->code = frame->tf_rax;
1049 if (__predict_false(sa->code == SYS_syscall ||
1050 sa->code == SYS___syscall ||
1051 sa->code >= p->p_sysent->sv_size))
1052 return (cpu_fetch_syscall_args_fallback(td, sa));
1054 sa->callp = &p->p_sysent->sv_table[sa->code];
1055 sa->narg = sa->callp->sy_narg;
1056 KASSERT(sa->narg <= nitems(sa->args), ("Too many syscall arguments!"));
1058 if (__predict_false(sa->narg > NARGREGS))
1059 return (cpu_fetch_syscall_args_fallback(td, sa));
1061 memcpy(sa->args, &frame->tf_rdi, sizeof(sa->args[0]) * NARGREGS);
1063 td->td_retval[0] = 0;
1064 td->td_retval[1] = frame->tf_rdx;
1069 #include "../../kern/subr_syscall.c"
1071 static void (*syscall_ret_l1d_flush)(void);
1072 int syscall_ret_l1d_flush_mode;
1078 wrmsr(MSR_IA32_FLUSH_CMD, IA32_FLUSH_CMD_L1D);
1081 static void __inline
1082 amd64_syscall_ret_flush_l1d_inline(int error)
1086 if (error != 0 && error != EEXIST && error != EAGAIN &&
1087 error != EXDEV && error != ENOENT && error != ENOTCONN &&
1088 error != EINPROGRESS) {
1089 p = syscall_ret_l1d_flush;
1096 amd64_syscall_ret_flush_l1d(int error)
1099 amd64_syscall_ret_flush_l1d_inline(error);
1103 amd64_syscall_ret_flush_l1d_recalc(void)
1107 l1d_hw = (cpu_stdext_feature3 & CPUID_STDEXT3_L1D_FLUSH) != 0;
1109 switch (syscall_ret_l1d_flush_mode) {
1111 syscall_ret_l1d_flush = NULL;
1114 syscall_ret_l1d_flush = l1d_hw ? flush_l1d_hw :
1118 syscall_ret_l1d_flush = l1d_hw ? flush_l1d_hw : NULL;
1121 syscall_ret_l1d_flush = flush_l1d_sw_abi;
1124 syscall_ret_l1d_flush_mode = 1;
1130 machdep_syscall_ret_flush_l1d(SYSCTL_HANDLER_ARGS)
1134 val = syscall_ret_l1d_flush_mode;
1135 error = sysctl_handle_int(oidp, &val, 0, req);
1136 if (error != 0 || req->newptr == NULL)
1138 syscall_ret_l1d_flush_mode = val;
1139 amd64_syscall_ret_flush_l1d_recalc();
1142 SYSCTL_PROC(_machdep, OID_AUTO, syscall_ret_flush_l1d, CTLTYPE_INT |
1143 CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
1144 machdep_syscall_ret_flush_l1d, "I",
1145 "Flush L1D on syscall return with error (0 - off, 1 - on, "
1146 "2 - use hw only, 3 - use sw only");
1150 * System call handler for native binaries. The trap frame is already
1151 * set up by the assembler trampoline and a pointer to it is saved in
1155 amd64_syscall(struct thread *td, int traced)
1161 if (!TRAPF_USERMODE(td->td_frame)) {
1166 error = syscallenter(td);
1171 if (__predict_false(traced)) {
1172 td->td_frame->tf_rflags &= ~PSL_T;
1173 ksiginfo_init_trap(&ksi);
1174 ksi.ksi_signo = SIGTRAP;
1175 ksi.ksi_code = TRAP_TRACE;
1176 ksi.ksi_addr = (void *)td->td_frame->tf_rip;
1177 trapsignal(td, &ksi);
1180 KASSERT(PCB_USER_FPU(td->td_pcb),
1181 ("System call %s returning with kernel FPU ctx leaked",
1182 syscallname(td->td_proc, td->td_sa.code)));
1183 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
1184 ("System call %s returning with mangled pcb_save",
1185 syscallname(td->td_proc, td->td_sa.code)));
1186 KASSERT(td->td_md.md_invl_gen.gen == 0,
1187 ("System call %s returning with leaked invl_gen %lu",
1188 syscallname(td->td_proc, td->td_sa.code),
1189 td->td_md.md_invl_gen.gen));
1191 syscallret(td, error);
1194 * If the user-supplied value of %rip is not a canonical
1195 * address, then some CPUs will trigger a ring 0 #GP during
1196 * the sysret instruction. However, the fault handler would
1197 * execute in ring 0 with the user's %gs and %rsp which would
1198 * not be safe. Instead, use the full return path which
1199 * catches the problem safely.
1201 if (__predict_false(td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS))
1202 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
1204 amd64_syscall_ret_flush_l1d_inline(error);