2 * Copyright (C) 1994, David Greenman
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the University of Utah, and William Jolitz.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: @(#)trap.c 7.4 (Berkeley) 5/13/91
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * AMD64 Trap and System call handling
47 #include "opt_clock.h"
49 #include "opt_hwpmc_hooks.h"
52 #include "opt_kdtrace.h"
54 #include <sys/param.h>
56 #include <sys/systm.h>
58 #include <sys/pioctl.h>
59 #include <sys/ptrace.h>
61 #include <sys/kernel.h>
64 #include <sys/mutex.h>
65 #include <sys/resourcevar.h>
66 #include <sys/signalvar.h>
67 #include <sys/syscall.h>
68 #include <sys/sysctl.h>
69 #include <sys/sysent.h>
71 #include <sys/vmmeter.h>
73 #include <sys/pmckern.h>
74 PMC_SOFT_DEFINE( , , page_fault, all);
75 PMC_SOFT_DEFINE( , , page_fault, read);
76 PMC_SOFT_DEFINE( , , page_fault, write);
80 #include <vm/vm_param.h>
82 #include <vm/vm_kern.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_extern.h>
87 #include <machine/cpu.h>
88 #include <machine/intr_machdep.h>
90 #include <machine/md_var.h>
91 #include <machine/pcb.h>
93 #include <machine/smp.h>
95 #include <machine/tss.h>
98 #include <sys/dtrace_bsd.h>
101 * This is a hook which is initialised by the dtrace module
102 * to handle traps which might occur during DTrace probe
105 dtrace_trap_func_t dtrace_trap_func;
107 dtrace_doubletrap_func_t dtrace_doubletrap_func;
110 * This is a hook which is initialised by the systrace module
111 * when it is loaded. This keeps the DTrace syscall provider
112 * implementation opaque.
114 systrace_probe_func_t systrace_probe_func;
117 * These hooks are necessary for the pid, usdt and fasttrap providers.
119 dtrace_fasttrap_probe_ptr_t dtrace_fasttrap_probe_ptr;
120 dtrace_pid_probe_ptr_t dtrace_pid_probe_ptr;
121 dtrace_return_probe_ptr_t dtrace_return_probe_ptr;
124 extern void trap(struct trapframe *frame);
125 extern void syscall(struct trapframe *frame);
126 void dblfault_handler(struct trapframe *frame);
128 static int trap_pfault(struct trapframe *, int);
129 static void trap_fatal(struct trapframe *, vm_offset_t);
131 #define MAX_TRAP_MSG 33
132 static char *trap_msg[] = {
134 "privileged instruction fault", /* 1 T_PRIVINFLT */
136 "breakpoint instruction fault", /* 3 T_BPTFLT */
139 "arithmetic trap", /* 6 T_ARITHTRAP */
142 "general protection fault", /* 9 T_PROTFLT */
143 "trace trap", /* 10 T_TRCTRAP */
145 "page fault", /* 12 T_PAGEFLT */
147 "alignment fault", /* 14 T_ALIGNFLT */
151 "integer divide fault", /* 18 T_DIVIDE */
152 "non-maskable interrupt trap", /* 19 T_NMI */
153 "overflow trap", /* 20 T_OFLOW */
154 "FPU bounds check fault", /* 21 T_BOUND */
155 "FPU device not available", /* 22 T_DNA */
156 "double fault", /* 23 T_DOUBLEFLT */
157 "FPU operand fetch fault", /* 24 T_FPOPFLT */
158 "invalid TSS fault", /* 25 T_TSSFLT */
159 "segment not present fault", /* 26 T_SEGNPFLT */
160 "stack fault", /* 27 T_STKFLT */
161 "machine check trap", /* 28 T_MCHK */
162 "SIMD floating-point exception", /* 29 T_XMMFLT */
163 "reserved (unknown) fault", /* 30 T_RESERVED */
164 "", /* 31 unused (reserved) */
165 "DTrace pid return trap", /* 32 T_DTRACE_RET */
166 "DTrace fasttrap probe trap", /* 33 T_DTRACE_PROBE */
170 static int kdb_on_nmi = 1;
171 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
172 &kdb_on_nmi, 0, "Go to KDB on NMI");
173 TUNABLE_INT("machdep.kdb_on_nmi", &kdb_on_nmi);
175 static int panic_on_nmi = 1;
176 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
177 &panic_on_nmi, 0, "Panic on NMI");
178 TUNABLE_INT("machdep.panic_on_nmi", &panic_on_nmi);
179 static int prot_fault_translation;
180 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
181 &prot_fault_translation, 0,
182 "Select signal to deliver on protection fault");
183 static int uprintf_signal;
184 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RW,
186 "Print debugging information on trap signal to ctty");
189 * Exception, fault, and trap interface to the FreeBSD kernel.
190 * This common code is called from assembly language IDT gate entry
191 * routines that prepare a suitable stack frame, and restore this
192 * frame after the exception has been processed.
196 trap(struct trapframe *frame)
198 struct thread *td = curthread;
199 struct proc *p = td->td_proc;
200 int i = 0, ucode = 0, code;
205 PCPU_INC(cnt.v_trap);
206 type = frame->tf_trapno;
209 /* Handler for NMI IPIs used for stopping CPUs. */
211 if (ipi_nmi_handler() == 0)
223 if (type == T_RESERVED) {
224 trap_fatal(frame, 0);
230 * CPU PMCs interrupt using an NMI. If the PMC module is
231 * active, pass the 'rip' value to the PMC module's interrupt
232 * handler. A return value of '1' from the handler means that
233 * the NMI was handled by it and we can return immediately.
235 if (type == T_NMI && pmc_intr &&
236 (*pmc_intr)(PCPU_GET(cpuid), frame))
240 if (type == T_MCHK) {
247 * A trap can occur while DTrace executes a probe. Before
248 * executing the probe, DTrace blocks re-scheduling and sets
249 * a flag in it's per-cpu flags to indicate that it doesn't
250 * want to fault. On returning from the probe, the no-fault
251 * flag is cleared and finally re-scheduling is enabled.
253 * If the DTrace kernel module has registered a trap handler,
254 * call it and if it returns non-zero, assume that it has
255 * handled the trap and modified the trap frame so that this
256 * function can return normally.
258 if (type == T_DTRACE_PROBE || type == T_DTRACE_RET ||
262 fill_frame_regs(frame, ®s);
263 if (type == T_DTRACE_PROBE &&
264 dtrace_fasttrap_probe_ptr != NULL &&
265 dtrace_fasttrap_probe_ptr(®s) == 0)
267 else if (type == T_BPTFLT &&
268 dtrace_pid_probe_ptr != NULL &&
269 dtrace_pid_probe_ptr(®s) == 0)
271 else if (type == T_DTRACE_RET &&
272 dtrace_return_probe_ptr != NULL &&
273 dtrace_return_probe_ptr(®s) == 0)
276 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
280 if ((frame->tf_rflags & PSL_I) == 0) {
282 * Buggy application or kernel code has disabled
283 * interrupts and then trapped. Enabling interrupts
284 * now is wrong, but it is better than running with
285 * interrupts disabled until they are accidentally
288 if (ISPL(frame->tf_cs) == SEL_UPL)
290 "pid %ld (%s): trap %d with interrupts disabled\n",
291 (long)curproc->p_pid, curthread->td_name, type);
292 else if (type != T_NMI && type != T_BPTFLT &&
295 * XXX not quite right, since this may be for a
296 * multiple fault in user mode.
298 printf("kernel trap %d with interrupts disabled\n",
302 * We shouldn't enable interrupts while holding a
305 if (td->td_md.md_spinlock_count == 0)
310 code = frame->tf_err;
312 if (ISPL(frame->tf_cs) == SEL_UPL) {
316 td->td_frame = frame;
317 addr = frame->tf_rip;
318 if (td->td_ucred != p->p_ucred)
319 cred_update_thread(td);
322 case T_PRIVINFLT: /* privileged instruction fault */
327 case T_BPTFLT: /* bpt instruction fault */
328 case T_TRCTRAP: /* trace trap */
330 frame->tf_rflags &= ~PSL_T;
332 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
335 case T_ARITHTRAP: /* arithmetic trap */
336 ucode = fputrap_x87();
342 case T_PROTFLT: /* general protection fault */
346 case T_STKFLT: /* stack fault */
347 case T_SEGNPFLT: /* segment not present fault */
351 case T_TSSFLT: /* invalid TSS fault */
355 case T_DOUBLEFLT: /* double fault */
361 case T_PAGEFLT: /* page fault */
362 addr = frame->tf_addr;
363 i = trap_pfault(frame, TRUE);
372 if (prot_fault_translation == 0) {
375 * This check also covers the images
376 * without the ABI-tag ELF note.
378 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
379 && p->p_osrel >= P_OSREL_SIGSEGV) {
384 ucode = BUS_PAGE_FAULT;
386 } else if (prot_fault_translation == 1) {
388 * Always compat mode.
391 ucode = BUS_PAGE_FAULT;
394 * Always SIGSEGV mode.
402 case T_DIVIDE: /* integer divide fault */
409 /* machine/parity/power fail/"kitchen sink" faults */
410 if (isa_nmi(code) == 0) {
413 * NMI can be hooked up to a pushbutton
417 printf ("NMI ... going to debugger\n");
418 kdb_trap(type, 0, frame);
422 } else if (panic_on_nmi)
423 panic("NMI indicates hardware failure");
427 case T_OFLOW: /* integer overflow fault */
432 case T_BOUND: /* bounds check fault */
438 /* transparent fault (due to context switch "late") */
439 KASSERT(PCB_USER_FPU(td->td_pcb),
440 ("kernel FPU ctx has leaked"));
444 case T_FPOPFLT: /* FPU operand fetch fault */
449 case T_XMMFLT: /* SIMD floating-point exception */
450 ucode = fputrap_sse();
459 KASSERT(cold || td->td_ucred != NULL,
460 ("kernel trap doesn't have ucred"));
462 case T_PAGEFLT: /* page fault */
463 (void) trap_pfault(frame, FALSE);
467 KASSERT(!PCB_USER_FPU(td->td_pcb),
468 ("Unregistered use of FPU in kernel"));
472 case T_ARITHTRAP: /* arithmetic trap */
473 case T_XMMFLT: /* SIMD floating-point exception */
474 case T_FPOPFLT: /* FPU operand fetch fault */
476 * XXXKIB for now disable any FPU traps in kernel
477 * handler registration seems to be overkill
479 trap_fatal(frame, 0);
482 case T_STKFLT: /* stack fault */
485 case T_PROTFLT: /* general protection fault */
486 case T_SEGNPFLT: /* segment not present fault */
487 if (td->td_intr_nesting_level != 0)
491 * Invalid segment selectors and out of bounds
492 * %rip's and %rsp's can be set up in user mode.
493 * This causes a fault in kernel mode when the
494 * kernel tries to return to user mode. We want
495 * to get this fault so that we can fix the
496 * problem here and not have to check all the
497 * selectors and pointers when the user changes
500 if (frame->tf_rip == (long)doreti_iret) {
501 frame->tf_rip = (long)doreti_iret_fault;
504 if (frame->tf_rip == (long)ld_ds) {
505 frame->tf_rip = (long)ds_load_fault;
508 if (frame->tf_rip == (long)ld_es) {
509 frame->tf_rip = (long)es_load_fault;
512 if (frame->tf_rip == (long)ld_fs) {
513 frame->tf_rip = (long)fs_load_fault;
516 if (frame->tf_rip == (long)ld_gs) {
517 frame->tf_rip = (long)gs_load_fault;
520 if (frame->tf_rip == (long)ld_gsbase) {
521 frame->tf_rip = (long)gsbase_load_fault;
524 if (frame->tf_rip == (long)ld_fsbase) {
525 frame->tf_rip = (long)fsbase_load_fault;
528 if (curpcb->pcb_onfault != NULL) {
529 frame->tf_rip = (long)curpcb->pcb_onfault;
536 * PSL_NT can be set in user mode and isn't cleared
537 * automatically when the kernel is entered. This
538 * causes a TSS fault when the kernel attempts to
539 * `iret' because the TSS link is uninitialized. We
540 * want to get this fault so that we can fix the
541 * problem here and not every time the kernel is
544 if (frame->tf_rflags & PSL_NT) {
545 frame->tf_rflags &= ~PSL_NT;
550 case T_TRCTRAP: /* trace trap */
552 * Ignore debug register trace traps due to
553 * accesses in the user's address space, which
554 * can happen under several conditions such as
555 * if a user sets a watchpoint on a buffer and
556 * then passes that buffer to a system call.
557 * We still want to get TRCTRAPS for addresses
558 * in kernel space because that is useful when
559 * debugging the kernel.
561 if (user_dbreg_trap()) {
563 * Reset breakpoint bits because the
566 /* XXX check upper bits here */
567 load_dr6(rdr6() & 0xfffffff0);
571 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
575 * If KDB is enabled, let it handle the debugger trap.
576 * Otherwise, debugger traps "can't happen".
579 if (kdb_trap(type, 0, frame))
586 /* machine/parity/power fail/"kitchen sink" faults */
587 if (isa_nmi(code) == 0) {
590 * NMI can be hooked up to a pushbutton
594 printf ("NMI ... going to debugger\n");
595 kdb_trap(type, 0, frame);
599 } else if (panic_on_nmi == 0)
605 trap_fatal(frame, 0);
609 /* Translate fault for emulators (e.g. Linux) */
610 if (*p->p_sysent->sv_transtrap)
611 i = (*p->p_sysent->sv_transtrap)(i, type);
613 ksiginfo_init_trap(&ksi);
615 ksi.ksi_code = ucode;
616 ksi.ksi_trapno = type;
617 ksi.ksi_addr = (void *)addr;
618 if (uprintf_signal) {
619 uprintf("pid %d comm %s: signal %d err %lx code %d type %d "
620 "addr 0x%lx rsp 0x%lx rip 0x%lx "
621 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
622 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr,
623 frame->tf_rsp, frame->tf_rip,
624 fubyte((void *)(frame->tf_rip + 0)),
625 fubyte((void *)(frame->tf_rip + 1)),
626 fubyte((void *)(frame->tf_rip + 2)),
627 fubyte((void *)(frame->tf_rip + 3)),
628 fubyte((void *)(frame->tf_rip + 4)),
629 fubyte((void *)(frame->tf_rip + 5)),
630 fubyte((void *)(frame->tf_rip + 6)),
631 fubyte((void *)(frame->tf_rip + 7)));
633 KASSERT((read_rflags() & PSL_I) != 0, ("interrupts disabled"));
634 trapsignal(td, &ksi);
638 KASSERT(PCB_USER_FPU(td->td_pcb),
639 ("Return from trap with kernel FPU ctx leaked"));
646 trap_pfault(frame, usermode)
647 struct trapframe *frame;
655 struct thread *td = curthread;
656 struct proc *p = td->td_proc;
657 vm_offset_t eva = frame->tf_addr;
659 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
661 * Due to both processor errata and lazy TLB invalidation when
662 * access restrictions are removed from virtual pages, memory
663 * accesses that are allowed by the physical mapping layer may
664 * nonetheless cause one spurious page fault per virtual page.
665 * When the thread is executing a "no faulting" section that
666 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
667 * every page fault is treated as a spurious page fault,
668 * unless it accesses the same virtual address as the most
669 * recent page fault within the same "no faulting" section.
671 if (td->td_md.md_spurflt_addr != eva ||
672 (td->td_pflags & TDP_RESETSPUR) != 0) {
674 * Do nothing to the TLB. A stale TLB entry is
675 * flushed automatically by a page fault.
677 td->td_md.md_spurflt_addr = eva;
678 td->td_pflags &= ~TDP_RESETSPUR;
683 * If we get a page fault while in a critical section, then
684 * it is most likely a fatal kernel page fault. The kernel
685 * is already going to panic trying to get a sleep lock to
686 * do the VM lookup, so just consider it a fatal trap so the
687 * kernel can print out a useful trap message and even get
690 * If we get a page fault while holding a non-sleepable
691 * lock, then it is most likely a fatal kernel page fault.
692 * If WITNESS is enabled, then it's going to whine about
693 * bogus LORs with various VM locks, so just skip to the
694 * fatal trap handling directly.
696 if (td->td_critnest != 0 ||
697 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
698 "Kernel page fault") != 0) {
699 trap_fatal(frame, eva);
703 va = trunc_page(eva);
704 if (va >= VM_MIN_KERNEL_ADDRESS) {
706 * Don't allow user-mode faults in kernel address space.
714 * This is a fault on non-kernel virtual memory. If either
715 * p or p->p_vmspace is NULL, then the fault is fatal.
717 if (p == NULL || (vm = p->p_vmspace) == NULL)
723 * When accessing a usermode address, kernel must be
724 * ready to accept the page fault, and provide a
725 * handling routine. Since accessing the address
726 * without the handler is a bug, do not try to handle
727 * it normally, and panic immediately.
729 if (!usermode && (td->td_intr_nesting_level != 0 ||
730 curpcb->pcb_onfault == NULL)) {
731 trap_fatal(frame, eva);
737 * If the trap was caused by errant bits in the PTE then panic.
739 if (frame->tf_err & PGEX_RSV) {
740 trap_fatal(frame, eva);
745 * PGEX_I is defined only if the execute disable bit capability is
746 * supported and enabled.
748 if (frame->tf_err & PGEX_W)
749 ftype = VM_PROT_WRITE;
750 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
751 ftype = VM_PROT_EXECUTE;
753 ftype = VM_PROT_READ;
755 if (map != kernel_map) {
757 * Keep swapout from messing with us during this
764 /* Fault in the user page: */
765 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
772 * Don't have to worry about process locking or stacks in the
775 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
777 if (rv == KERN_SUCCESS) {
779 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
780 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
781 if (ftype == VM_PROT_READ)
782 PMC_SOFT_CALL_TF( , , page_fault, read,
785 PMC_SOFT_CALL_TF( , , page_fault, write,
793 if (td->td_intr_nesting_level == 0 &&
794 curpcb->pcb_onfault != NULL) {
795 frame->tf_rip = (long)curpcb->pcb_onfault;
798 trap_fatal(frame, eva);
801 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
805 trap_fatal(frame, eva)
806 struct trapframe *frame;
812 struct soft_segment_descriptor softseg;
815 code = frame->tf_err;
816 type = frame->tf_trapno;
817 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
820 if (type <= MAX_TRAP_MSG)
821 msg = trap_msg[type];
824 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
825 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
827 /* two separate prints in case of a trap on an unmapped page */
828 printf("cpuid = %d; ", PCPU_GET(cpuid));
829 printf("apic id = %02x\n", PCPU_GET(apic_id));
831 if (type == T_PAGEFLT) {
832 printf("fault virtual address = 0x%lx\n", eva);
833 printf("fault code = %s %s %s%s, %s\n",
834 code & PGEX_U ? "user" : "supervisor",
835 code & PGEX_W ? "write" : "read",
836 code & PGEX_I ? "instruction" : "data",
837 code & PGEX_RSV ? " rsv" : "",
838 code & PGEX_P ? "protection violation" : "page not present");
840 printf("instruction pointer = 0x%lx:0x%lx\n",
841 frame->tf_cs & 0xffff, frame->tf_rip);
842 if (ISPL(frame->tf_cs) == SEL_UPL) {
843 ss = frame->tf_ss & 0xffff;
846 ss = GSEL(GDATA_SEL, SEL_KPL);
847 esp = (long)&frame->tf_rsp;
849 printf("stack pointer = 0x%x:0x%lx\n", ss, esp);
850 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
851 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
852 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
853 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
854 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
856 printf("processor eflags = ");
857 if (frame->tf_rflags & PSL_T)
858 printf("trace trap, ");
859 if (frame->tf_rflags & PSL_I)
860 printf("interrupt enabled, ");
861 if (frame->tf_rflags & PSL_NT)
862 printf("nested task, ");
863 if (frame->tf_rflags & PSL_RF)
865 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
866 printf("current process = ");
869 (u_long)curproc->p_pid, curthread->td_name ?
870 curthread->td_name : "");
876 if (debugger_on_panic || kdb_active)
877 if (kdb_trap(type, 0, frame))
880 printf("trap number = %d\n", type);
881 if (type <= MAX_TRAP_MSG)
882 panic("%s", trap_msg[type]);
884 panic("unknown/reserved trap");
888 * Double fault handler. Called when a fault occurs while writing
889 * a frame for a trap/exception onto the stack. This usually occurs
890 * when the stack overflows (such is the case with infinite recursion,
894 dblfault_handler(struct trapframe *frame)
897 if (dtrace_doubletrap_func != NULL)
898 (*dtrace_doubletrap_func)();
900 printf("\nFatal double fault\n");
901 printf("rip = 0x%lx\n", frame->tf_rip);
902 printf("rsp = 0x%lx\n", frame->tf_rsp);
903 printf("rbp = 0x%lx\n", frame->tf_rbp);
905 /* two separate prints in case of a trap on an unmapped page */
906 printf("cpuid = %d; ", PCPU_GET(cpuid));
907 printf("apic id = %02x\n", PCPU_GET(apic_id));
909 panic("double fault");
913 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
916 struct trapframe *frame;
919 int reg, regcnt, error;
922 frame = td->td_frame;
926 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
927 sa->code = frame->tf_rax;
929 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
930 sa->code = frame->tf_rdi;
934 if (p->p_sysent->sv_mask)
935 sa->code &= p->p_sysent->sv_mask;
937 if (sa->code >= p->p_sysent->sv_size)
938 sa->callp = &p->p_sysent->sv_table[0];
940 sa->callp = &p->p_sysent->sv_table[sa->code];
942 sa->narg = sa->callp->sy_narg;
943 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]),
944 ("Too many syscall arguments!"));
946 argp = &frame->tf_rdi;
948 bcopy(argp, sa->args, sizeof(sa->args[0]) * regcnt);
949 if (sa->narg > regcnt) {
950 KASSERT(params != NULL, ("copyin args with no params!"));
951 error = copyin(params, &sa->args[regcnt],
952 (sa->narg - regcnt) * sizeof(sa->args[0]));
956 td->td_retval[0] = 0;
957 td->td_retval[1] = frame->tf_rdx;
963 #include "../../kern/subr_syscall.c"
966 * System call handler for native binaries. The trap frame is already
967 * set up by the assembler trampoline and a pointer to it is saved in
971 amd64_syscall(struct thread *td, int traced)
973 struct syscall_args sa;
978 if (ISPL(td->td_frame->tf_cs) != SEL_UPL) {
983 error = syscallenter(td, &sa);
988 if (__predict_false(traced)) {
989 td->td_frame->tf_rflags &= ~PSL_T;
990 ksiginfo_init_trap(&ksi);
991 ksi.ksi_signo = SIGTRAP;
992 ksi.ksi_code = TRAP_TRACE;
993 ksi.ksi_addr = (void *)td->td_frame->tf_rip;
994 trapsignal(td, &ksi);
997 KASSERT(PCB_USER_FPU(td->td_pcb),
998 ("System call %s returing with kernel FPU ctx leaked",
999 syscallname(td->td_proc, sa.code)));
1000 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
1001 ("System call %s returning with mangled pcb_save",
1002 syscallname(td->td_proc, sa.code)));
1004 syscallret(td, error, &sa);
1007 * If the user-supplied value of %rip is not a canonical
1008 * address, then some CPUs will trigger a ring 0 #GP during
1009 * the sysret instruction. However, the fault handler would
1010 * execute in ring 0 with the user's %gs and %rsp which would
1011 * not be safe. Instead, use the full return path which
1012 * catches the problem safely.
1014 if (td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS)
1015 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);