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 and usdt providers.
119 dtrace_pid_probe_ptr_t dtrace_pid_probe_ptr;
120 dtrace_return_probe_ptr_t dtrace_return_probe_ptr;
123 extern void trap(struct trapframe *frame);
124 extern void syscall(struct trapframe *frame);
125 void dblfault_handler(struct trapframe *frame);
127 static int trap_pfault(struct trapframe *, int);
128 static void trap_fatal(struct trapframe *, vm_offset_t);
130 #define MAX_TRAP_MSG 32
131 static char *trap_msg[] = {
133 "privileged instruction fault", /* 1 T_PRIVINFLT */
135 "breakpoint instruction fault", /* 3 T_BPTFLT */
138 "arithmetic trap", /* 6 T_ARITHTRAP */
141 "general protection fault", /* 9 T_PROTFLT */
142 "trace trap", /* 10 T_TRCTRAP */
144 "page fault", /* 12 T_PAGEFLT */
146 "alignment fault", /* 14 T_ALIGNFLT */
150 "integer divide fault", /* 18 T_DIVIDE */
151 "non-maskable interrupt trap", /* 19 T_NMI */
152 "overflow trap", /* 20 T_OFLOW */
153 "FPU bounds check fault", /* 21 T_BOUND */
154 "FPU device not available", /* 22 T_DNA */
155 "double fault", /* 23 T_DOUBLEFLT */
156 "FPU operand fetch fault", /* 24 T_FPOPFLT */
157 "invalid TSS fault", /* 25 T_TSSFLT */
158 "segment not present fault", /* 26 T_SEGNPFLT */
159 "stack fault", /* 27 T_STKFLT */
160 "machine check trap", /* 28 T_MCHK */
161 "SIMD floating-point exception", /* 29 T_XMMFLT */
162 "reserved (unknown) fault", /* 30 T_RESERVED */
163 "", /* 31 unused (reserved) */
164 "DTrace pid return trap", /* 32 T_DTRACE_RET */
168 static int kdb_on_nmi = 1;
169 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
170 &kdb_on_nmi, 0, "Go to KDB on NMI");
171 TUNABLE_INT("machdep.kdb_on_nmi", &kdb_on_nmi);
173 static int panic_on_nmi = 1;
174 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
175 &panic_on_nmi, 0, "Panic on NMI");
176 TUNABLE_INT("machdep.panic_on_nmi", &panic_on_nmi);
177 static int prot_fault_translation;
178 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
179 &prot_fault_translation, 0,
180 "Select signal to deliver on protection fault");
181 static int uprintf_signal;
182 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RW,
184 "Print debugging information on trap signal to ctty");
187 * Exception, fault, and trap interface to the FreeBSD kernel.
188 * This common code is called from assembly language IDT gate entry
189 * routines that prepare a suitable stack frame, and restore this
190 * frame after the exception has been processed.
194 trap(struct trapframe *frame)
199 struct thread *td = curthread;
200 struct proc *p = td->td_proc;
201 int i = 0, ucode = 0, code;
206 PCPU_INC(cnt.v_trap);
207 type = frame->tf_trapno;
210 /* Handler for NMI IPIs used for stopping CPUs. */
212 if (ipi_nmi_handler() == 0)
224 if (type == T_RESERVED) {
225 trap_fatal(frame, 0);
231 * CPU PMCs interrupt using an NMI. If the PMC module is
232 * active, pass the 'rip' value to the PMC module's interrupt
233 * handler. A return value of '1' from the handler means that
234 * the NMI was handled by it and we can return immediately.
236 if (type == T_NMI && pmc_intr &&
237 (*pmc_intr)(PCPU_GET(cpuid), frame))
241 if (type == T_MCHK) {
248 * A trap can occur while DTrace executes a probe. Before
249 * executing the probe, DTrace blocks re-scheduling and sets
250 * a flag in its per-cpu flags to indicate that it doesn't
251 * want to fault. On returning from the probe, the no-fault
252 * flag is cleared and finally re-scheduling is enabled.
254 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
258 if ((frame->tf_rflags & PSL_I) == 0) {
260 * Buggy application or kernel code has disabled
261 * interrupts and then trapped. Enabling interrupts
262 * now is wrong, but it is better than running with
263 * interrupts disabled until they are accidentally
266 if (ISPL(frame->tf_cs) == SEL_UPL)
268 "pid %ld (%s): trap %d with interrupts disabled\n",
269 (long)curproc->p_pid, curthread->td_name, type);
270 else if (type != T_NMI && type != T_BPTFLT &&
273 * XXX not quite right, since this may be for a
274 * multiple fault in user mode.
276 printf("kernel trap %d with interrupts disabled\n",
280 * We shouldn't enable interrupts while holding a
283 if (td->td_md.md_spinlock_count == 0)
288 code = frame->tf_err;
290 if (ISPL(frame->tf_cs) == SEL_UPL) {
294 td->td_frame = frame;
295 addr = frame->tf_rip;
296 if (td->td_ucred != p->p_ucred)
297 cred_update_thread(td);
300 case T_PRIVINFLT: /* privileged instruction fault */
305 case T_BPTFLT: /* bpt instruction fault */
306 case T_TRCTRAP: /* trace trap */
309 if (type == T_BPTFLT) {
310 fill_frame_regs(frame, ®s);
311 if (dtrace_pid_probe_ptr != NULL &&
312 dtrace_pid_probe_ptr(®s) == 0)
316 frame->tf_rflags &= ~PSL_T;
318 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
321 case T_ARITHTRAP: /* arithmetic trap */
322 ucode = fputrap_x87();
328 case T_PROTFLT: /* general protection fault */
332 case T_STKFLT: /* stack fault */
333 case T_SEGNPFLT: /* segment not present fault */
337 case T_TSSFLT: /* invalid TSS fault */
345 case T_DOUBLEFLT: /* double fault */
351 case T_PAGEFLT: /* page fault */
352 addr = frame->tf_addr;
353 i = trap_pfault(frame, TRUE);
362 if (prot_fault_translation == 0) {
365 * This check also covers the images
366 * without the ABI-tag ELF note.
368 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
369 && p->p_osrel >= P_OSREL_SIGSEGV) {
374 ucode = BUS_PAGE_FAULT;
376 } else if (prot_fault_translation == 1) {
378 * Always compat mode.
381 ucode = BUS_PAGE_FAULT;
384 * Always SIGSEGV mode.
392 case T_DIVIDE: /* integer divide fault */
399 /* machine/parity/power fail/"kitchen sink" faults */
400 if (isa_nmi(code) == 0) {
403 * NMI can be hooked up to a pushbutton
407 printf ("NMI ... going to debugger\n");
408 kdb_trap(type, 0, frame);
412 } else if (panic_on_nmi)
413 panic("NMI indicates hardware failure");
417 case T_OFLOW: /* integer overflow fault */
422 case T_BOUND: /* bounds check fault */
428 /* transparent fault (due to context switch "late") */
429 KASSERT(PCB_USER_FPU(td->td_pcb),
430 ("kernel FPU ctx has leaked"));
434 case T_FPOPFLT: /* FPU operand fetch fault */
439 case T_XMMFLT: /* SIMD floating-point exception */
440 ucode = fputrap_sse();
448 fill_frame_regs(frame, ®s);
449 if (dtrace_return_probe_ptr != NULL &&
450 dtrace_return_probe_ptr(®s) == 0)
458 KASSERT(cold || td->td_ucred != NULL,
459 ("kernel trap doesn't have ucred"));
461 case T_PAGEFLT: /* page fault */
462 (void) trap_pfault(frame, FALSE);
466 KASSERT(!PCB_USER_FPU(td->td_pcb),
467 ("Unregistered use of FPU in kernel"));
471 case T_ARITHTRAP: /* arithmetic trap */
472 case T_XMMFLT: /* SIMD floating-point exception */
473 case T_FPOPFLT: /* FPU operand fetch fault */
475 * XXXKIB for now disable any FPU traps in kernel
476 * handler registration seems to be overkill
478 trap_fatal(frame, 0);
481 case T_STKFLT: /* stack fault */
482 case T_PROTFLT: /* general protection fault */
483 case T_SEGNPFLT: /* segment not present fault */
484 if (td->td_intr_nesting_level != 0)
488 * Invalid segment selectors and out of bounds
489 * %rip's and %rsp's can be set up in user mode.
490 * This causes a fault in kernel mode when the
491 * kernel tries to return to user mode. We want
492 * to get this fault so that we can fix the
493 * problem here and not have to check all the
494 * selectors and pointers when the user changes
497 if (frame->tf_rip == (long)doreti_iret) {
498 frame->tf_rip = (long)doreti_iret_fault;
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: /* trace trap */
549 * Ignore debug register trace traps due to
550 * accesses in the user's address space, which
551 * can happen under several conditions such as
552 * if a user sets a watchpoint on a buffer and
553 * then passes that buffer to a system call.
554 * We still want to get TRCTRAPS for addresses
555 * in kernel space because that is useful when
556 * debugging the kernel.
558 if (user_dbreg_trap()) {
560 * Reset breakpoint bits because the
563 /* XXX check upper bits here */
564 load_dr6(rdr6() & 0xfffffff0);
568 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
572 * If KDB is enabled, let it handle the debugger trap.
573 * Otherwise, debugger traps "can't happen".
576 if (kdb_trap(type, 0, frame))
583 /* machine/parity/power fail/"kitchen sink" faults */
584 if (isa_nmi(code) == 0) {
587 * NMI can be hooked up to a pushbutton
591 printf ("NMI ... going to debugger\n");
592 kdb_trap(type, 0, frame);
596 } else if (panic_on_nmi == 0)
602 trap_fatal(frame, 0);
606 /* Translate fault for emulators (e.g. Linux) */
607 if (*p->p_sysent->sv_transtrap)
608 i = (*p->p_sysent->sv_transtrap)(i, type);
610 ksiginfo_init_trap(&ksi);
612 ksi.ksi_code = ucode;
613 ksi.ksi_trapno = type;
614 ksi.ksi_addr = (void *)addr;
615 if (uprintf_signal) {
616 uprintf("pid %d comm %s: signal %d err %lx code %d type %d "
617 "addr 0x%lx rsp 0x%lx rip 0x%lx "
618 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
619 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr,
620 frame->tf_rsp, frame->tf_rip,
621 fubyte((void *)(frame->tf_rip + 0)),
622 fubyte((void *)(frame->tf_rip + 1)),
623 fubyte((void *)(frame->tf_rip + 2)),
624 fubyte((void *)(frame->tf_rip + 3)),
625 fubyte((void *)(frame->tf_rip + 4)),
626 fubyte((void *)(frame->tf_rip + 5)),
627 fubyte((void *)(frame->tf_rip + 6)),
628 fubyte((void *)(frame->tf_rip + 7)));
630 KASSERT((read_rflags() & PSL_I) != 0, ("interrupts disabled"));
631 trapsignal(td, &ksi);
635 mtx_assert(&Giant, MA_NOTOWNED);
636 KASSERT(PCB_USER_FPU(td->td_pcb),
637 ("Return from trap with kernel FPU ctx leaked"));
644 trap_pfault(frame, usermode)
645 struct trapframe *frame;
649 struct vmspace *vm = NULL;
653 struct thread *td = curthread;
654 struct proc *p = td->td_proc;
655 vm_offset_t eva = frame->tf_addr;
657 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
659 * Due to both processor errata and lazy TLB invalidation when
660 * access restrictions are removed from virtual pages, memory
661 * accesses that are allowed by the physical mapping layer may
662 * nonetheless cause one spurious page fault per virtual page.
663 * When the thread is executing a "no faulting" section that
664 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
665 * every page fault is treated as a spurious page fault,
666 * unless it accesses the same virtual address as the most
667 * recent page fault within the same "no faulting" section.
669 if (td->td_md.md_spurflt_addr != eva ||
670 (td->td_pflags & TDP_RESETSPUR) != 0) {
672 * Do nothing to the TLB. A stale TLB entry is
673 * flushed automatically by a page fault.
675 td->td_md.md_spurflt_addr = eva;
676 td->td_pflags &= ~TDP_RESETSPUR;
681 * If we get a page fault while in a critical section, then
682 * it is most likely a fatal kernel page fault. The kernel
683 * is already going to panic trying to get a sleep lock to
684 * do the VM lookup, so just consider it a fatal trap so the
685 * kernel can print out a useful trap message and even get
688 * If we get a page fault while holding a non-sleepable
689 * lock, then it is most likely a fatal kernel page fault.
690 * If WITNESS is enabled, then it's going to whine about
691 * bogus LORs with various VM locks, so just skip to the
692 * fatal trap handling directly.
694 if (td->td_critnest != 0 ||
695 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
696 "Kernel page fault") != 0) {
697 trap_fatal(frame, eva);
701 va = trunc_page(eva);
702 if (va >= VM_MIN_KERNEL_ADDRESS) {
704 * Don't allow user-mode faults in kernel address space.
712 * This is a fault on non-kernel virtual memory.
713 * vm is initialized above to NULL. If curproc is NULL
714 * or curproc->p_vmspace is NULL the fault is fatal.
725 * When accessing a usermode address, kernel must be
726 * ready to accept the page fault, and provide a
727 * handling routine. Since accessing the address
728 * without the handler is a bug, do not try to handle
729 * it normally, and panic immediately.
731 if (!usermode && (td->td_intr_nesting_level != 0 ||
732 curpcb->pcb_onfault == NULL)) {
733 trap_fatal(frame, eva);
739 * PGEX_I is defined only if the execute disable bit capability is
740 * supported and enabled.
742 if (frame->tf_err & PGEX_W)
743 ftype = VM_PROT_WRITE;
744 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
745 ftype = VM_PROT_EXECUTE;
747 ftype = VM_PROT_READ;
749 if (map != kernel_map) {
751 * Keep swapout from messing with us during this
758 /* Fault in the user page: */
759 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
766 * Don't have to worry about process locking or stacks in the
769 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
771 if (rv == KERN_SUCCESS) {
773 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
774 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
775 if (ftype == VM_PROT_READ)
776 PMC_SOFT_CALL_TF( , , page_fault, read,
779 PMC_SOFT_CALL_TF( , , page_fault, write,
787 if (td->td_intr_nesting_level == 0 &&
788 curpcb->pcb_onfault != NULL) {
789 frame->tf_rip = (long)curpcb->pcb_onfault;
792 if ((td->td_pflags & TDP_DEVMEMIO) != 0) {
793 KASSERT(curpcb->pcb_onfault != NULL,
794 ("/dev/mem without pcb_onfault"));
795 frame->tf_rip = (long)curpcb->pcb_onfault;
798 trap_fatal(frame, eva);
802 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
806 trap_fatal(frame, eva)
807 struct trapframe *frame;
813 struct soft_segment_descriptor softseg;
816 code = frame->tf_err;
817 type = frame->tf_trapno;
818 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
821 if (type <= MAX_TRAP_MSG)
822 msg = trap_msg[type];
825 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
826 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
828 /* two separate prints in case of a trap on an unmapped page */
829 printf("cpuid = %d; ", PCPU_GET(cpuid));
830 printf("apic id = %02x\n", PCPU_GET(apic_id));
832 if (type == T_PAGEFLT) {
833 printf("fault virtual address = 0x%lx\n", eva);
834 printf("fault code = %s %s %s, %s\n",
835 code & PGEX_U ? "user" : "supervisor",
836 code & PGEX_W ? "write" : "read",
837 code & PGEX_I ? "instruction" : "data",
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 = %d (%s)\n",
867 curproc->p_pid, curthread->td_name);
870 if (debugger_on_panic || kdb_active)
871 if (kdb_trap(type, 0, frame))
874 printf("trap number = %d\n", type);
875 if (type <= MAX_TRAP_MSG)
876 panic("%s", trap_msg[type]);
878 panic("unknown/reserved trap");
882 * Double fault handler. Called when a fault occurs while writing
883 * a frame for a trap/exception onto the stack. This usually occurs
884 * when the stack overflows (such is the case with infinite recursion,
888 dblfault_handler(struct trapframe *frame)
891 if (dtrace_doubletrap_func != NULL)
892 (*dtrace_doubletrap_func)();
894 printf("\nFatal double fault\n");
895 printf("rip = 0x%lx\n", frame->tf_rip);
896 printf("rsp = 0x%lx\n", frame->tf_rsp);
897 printf("rbp = 0x%lx\n", frame->tf_rbp);
899 /* two separate prints in case of a trap on an unmapped page */
900 printf("cpuid = %d; ", PCPU_GET(cpuid));
901 printf("apic id = %02x\n", PCPU_GET(apic_id));
903 panic("double fault");
907 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
910 struct trapframe *frame;
913 int reg, regcnt, error;
916 frame = td->td_frame;
920 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
921 sa->code = frame->tf_rax;
923 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
924 sa->code = frame->tf_rdi;
928 if (p->p_sysent->sv_mask)
929 sa->code &= p->p_sysent->sv_mask;
931 if (sa->code >= p->p_sysent->sv_size)
932 sa->callp = &p->p_sysent->sv_table[0];
934 sa->callp = &p->p_sysent->sv_table[sa->code];
936 sa->narg = sa->callp->sy_narg;
937 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]),
938 ("Too many syscall arguments!"));
940 argp = &frame->tf_rdi;
942 bcopy(argp, sa->args, sizeof(sa->args[0]) * regcnt);
943 if (sa->narg > regcnt) {
944 KASSERT(params != NULL, ("copyin args with no params!"));
945 error = copyin(params, &sa->args[regcnt],
946 (sa->narg - regcnt) * sizeof(sa->args[0]));
950 td->td_retval[0] = 0;
951 td->td_retval[1] = frame->tf_rdx;
957 #include "../../kern/subr_syscall.c"
960 * System call handler for native binaries. The trap frame is already
961 * set up by the assembler trampoline and a pointer to it is saved in
965 amd64_syscall(struct thread *td, int traced)
967 struct syscall_args sa;
972 if (ISPL(td->td_frame->tf_cs) != SEL_UPL) {
977 error = syscallenter(td, &sa);
982 if (__predict_false(traced)) {
983 td->td_frame->tf_rflags &= ~PSL_T;
984 ksiginfo_init_trap(&ksi);
985 ksi.ksi_signo = SIGTRAP;
986 ksi.ksi_code = TRAP_TRACE;
987 ksi.ksi_addr = (void *)td->td_frame->tf_rip;
988 trapsignal(td, &ksi);
991 KASSERT(PCB_USER_FPU(td->td_pcb),
992 ("System call %s returing with kernel FPU ctx leaked",
993 syscallname(td->td_proc, sa.code)));
994 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
995 ("System call %s returning with mangled pcb_save",
996 syscallname(td->td_proc, sa.code)));
998 syscallret(td, error, &sa);
1001 * If the user-supplied value of %rip is not a canonical
1002 * address, then some CPUs will trigger a ring 0 #GP during
1003 * the sysret instruction. However, the fault handler would
1004 * execute in ring 0 with the user's %gs and %rsp which would
1005 * not be safe. Instead, use the full return path which
1006 * catches the problem safely.
1008 if (td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS)
1009 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);