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 extern void trap(struct trapframe *frame);
102 extern void syscall(struct trapframe *frame);
103 void dblfault_handler(struct trapframe *frame);
105 static int trap_pfault(struct trapframe *, int);
106 static void trap_fatal(struct trapframe *, vm_offset_t);
108 #define MAX_TRAP_MSG 32
109 static char *trap_msg[] = {
111 "privileged instruction fault", /* 1 T_PRIVINFLT */
113 "breakpoint instruction fault", /* 3 T_BPTFLT */
116 "arithmetic trap", /* 6 T_ARITHTRAP */
119 "general protection fault", /* 9 T_PROTFLT */
120 "trace trap", /* 10 T_TRCTRAP */
122 "page fault", /* 12 T_PAGEFLT */
124 "alignment fault", /* 14 T_ALIGNFLT */
128 "integer divide fault", /* 18 T_DIVIDE */
129 "non-maskable interrupt trap", /* 19 T_NMI */
130 "overflow trap", /* 20 T_OFLOW */
131 "FPU bounds check fault", /* 21 T_BOUND */
132 "FPU device not available", /* 22 T_DNA */
133 "double fault", /* 23 T_DOUBLEFLT */
134 "FPU operand fetch fault", /* 24 T_FPOPFLT */
135 "invalid TSS fault", /* 25 T_TSSFLT */
136 "segment not present fault", /* 26 T_SEGNPFLT */
137 "stack fault", /* 27 T_STKFLT */
138 "machine check trap", /* 28 T_MCHK */
139 "SIMD floating-point exception", /* 29 T_XMMFLT */
140 "reserved (unknown) fault", /* 30 T_RESERVED */
141 "", /* 31 unused (reserved) */
142 "DTrace pid return trap", /* 32 T_DTRACE_RET */
146 static int kdb_on_nmi = 1;
147 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
148 &kdb_on_nmi, 0, "Go to KDB on NMI");
149 TUNABLE_INT("machdep.kdb_on_nmi", &kdb_on_nmi);
151 static int panic_on_nmi = 1;
152 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
153 &panic_on_nmi, 0, "Panic on NMI");
154 TUNABLE_INT("machdep.panic_on_nmi", &panic_on_nmi);
155 static int prot_fault_translation;
156 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
157 &prot_fault_translation, 0,
158 "Select signal to deliver on protection fault");
159 static int uprintf_signal;
160 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RW,
162 "Print debugging information on trap signal to ctty");
165 * Exception, fault, and trap interface to the FreeBSD kernel.
166 * This common code is called from assembly language IDT gate entry
167 * routines that prepare a suitable stack frame, and restore this
168 * frame after the exception has been processed.
172 trap(struct trapframe *frame)
177 struct thread *td = curthread;
178 struct proc *p = td->td_proc;
179 int i = 0, ucode = 0, code;
184 PCPU_INC(cnt.v_trap);
185 type = frame->tf_trapno;
188 /* Handler for NMI IPIs used for stopping CPUs. */
190 if (ipi_nmi_handler() == 0)
202 if (type == T_RESERVED) {
203 trap_fatal(frame, 0);
209 * CPU PMCs interrupt using an NMI. If the PMC module is
210 * active, pass the 'rip' value to the PMC module's interrupt
211 * handler. A return value of '1' from the handler means that
212 * the NMI was handled by it and we can return immediately.
214 if (type == T_NMI && pmc_intr &&
215 (*pmc_intr)(PCPU_GET(cpuid), frame))
219 if (type == T_MCHK) {
226 * A trap can occur while DTrace executes a probe. Before
227 * executing the probe, DTrace blocks re-scheduling and sets
228 * a flag in its per-cpu flags to indicate that it doesn't
229 * want to fault. On returning from the probe, the no-fault
230 * flag is cleared and finally re-scheduling is enabled.
232 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
236 if ((frame->tf_rflags & PSL_I) == 0) {
238 * Buggy application or kernel code has disabled
239 * interrupts and then trapped. Enabling interrupts
240 * now is wrong, but it is better than running with
241 * interrupts disabled until they are accidentally
244 if (ISPL(frame->tf_cs) == SEL_UPL)
246 "pid %ld (%s): trap %d with interrupts disabled\n",
247 (long)curproc->p_pid, curthread->td_name, type);
248 else if (type != T_NMI && type != T_BPTFLT &&
251 * XXX not quite right, since this may be for a
252 * multiple fault in user mode.
254 printf("kernel trap %d with interrupts disabled\n",
258 * We shouldn't enable interrupts while holding a
261 if (td->td_md.md_spinlock_count == 0)
266 code = frame->tf_err;
268 if (ISPL(frame->tf_cs) == SEL_UPL) {
272 td->td_frame = frame;
273 addr = frame->tf_rip;
274 if (td->td_ucred != p->p_ucred)
275 cred_update_thread(td);
278 case T_PRIVINFLT: /* privileged instruction fault */
283 case T_BPTFLT: /* bpt instruction fault */
284 case T_TRCTRAP: /* trace trap */
287 if (type == T_BPTFLT) {
288 fill_frame_regs(frame, ®s);
289 if (dtrace_pid_probe_ptr != NULL &&
290 dtrace_pid_probe_ptr(®s) == 0)
294 frame->tf_rflags &= ~PSL_T;
296 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
299 case T_ARITHTRAP: /* arithmetic trap */
300 ucode = fputrap_x87();
306 case T_PROTFLT: /* general protection fault */
310 case T_STKFLT: /* stack fault */
311 case T_SEGNPFLT: /* segment not present fault */
315 case T_TSSFLT: /* invalid TSS fault */
323 case T_DOUBLEFLT: /* double fault */
329 case T_PAGEFLT: /* page fault */
330 addr = frame->tf_addr;
331 i = trap_pfault(frame, TRUE);
340 if (prot_fault_translation == 0) {
343 * This check also covers the images
344 * without the ABI-tag ELF note.
346 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
347 && p->p_osrel >= P_OSREL_SIGSEGV) {
352 ucode = BUS_PAGE_FAULT;
354 } else if (prot_fault_translation == 1) {
356 * Always compat mode.
359 ucode = BUS_PAGE_FAULT;
362 * Always SIGSEGV mode.
370 case T_DIVIDE: /* integer divide fault */
377 /* machine/parity/power fail/"kitchen sink" faults */
378 if (isa_nmi(code) == 0) {
381 * NMI can be hooked up to a pushbutton
385 printf ("NMI ... going to debugger\n");
386 kdb_trap(type, 0, frame);
390 } else if (panic_on_nmi)
391 panic("NMI indicates hardware failure");
395 case T_OFLOW: /* integer overflow fault */
400 case T_BOUND: /* bounds check fault */
406 /* transparent fault (due to context switch "late") */
407 KASSERT(PCB_USER_FPU(td->td_pcb),
408 ("kernel FPU ctx has leaked"));
412 case T_FPOPFLT: /* FPU operand fetch fault */
417 case T_XMMFLT: /* SIMD floating-point exception */
418 ucode = fputrap_sse();
426 fill_frame_regs(frame, ®s);
427 if (dtrace_return_probe_ptr != NULL &&
428 dtrace_return_probe_ptr(®s) == 0)
436 KASSERT(cold || td->td_ucred != NULL,
437 ("kernel trap doesn't have ucred"));
439 case T_PAGEFLT: /* page fault */
440 (void) trap_pfault(frame, FALSE);
444 KASSERT(!PCB_USER_FPU(td->td_pcb),
445 ("Unregistered use of FPU in kernel"));
449 case T_ARITHTRAP: /* arithmetic trap */
450 case T_XMMFLT: /* SIMD floating-point exception */
451 case T_FPOPFLT: /* FPU operand fetch fault */
453 * XXXKIB for now disable any FPU traps in kernel
454 * handler registration seems to be overkill
456 trap_fatal(frame, 0);
459 case T_STKFLT: /* stack fault */
460 case T_PROTFLT: /* general protection fault */
461 case T_SEGNPFLT: /* segment not present fault */
462 if (td->td_intr_nesting_level != 0)
466 * Invalid segment selectors and out of bounds
467 * %rip's and %rsp's can be set up in user mode.
468 * This causes a fault in kernel mode when the
469 * kernel tries to return to user mode. We want
470 * to get this fault so that we can fix the
471 * problem here and not have to check all the
472 * selectors and pointers when the user changes
475 if (frame->tf_rip == (long)doreti_iret) {
476 frame->tf_rip = (long)doreti_iret_fault;
479 if (frame->tf_rip == (long)ld_ds) {
480 frame->tf_rip = (long)ds_load_fault;
483 if (frame->tf_rip == (long)ld_es) {
484 frame->tf_rip = (long)es_load_fault;
487 if (frame->tf_rip == (long)ld_fs) {
488 frame->tf_rip = (long)fs_load_fault;
491 if (frame->tf_rip == (long)ld_gs) {
492 frame->tf_rip = (long)gs_load_fault;
495 if (frame->tf_rip == (long)ld_gsbase) {
496 frame->tf_rip = (long)gsbase_load_fault;
499 if (frame->tf_rip == (long)ld_fsbase) {
500 frame->tf_rip = (long)fsbase_load_fault;
503 if (curpcb->pcb_onfault != NULL) {
504 frame->tf_rip = (long)curpcb->pcb_onfault;
511 * PSL_NT can be set in user mode and isn't cleared
512 * automatically when the kernel is entered. This
513 * causes a TSS fault when the kernel attempts to
514 * `iret' because the TSS link is uninitialized. We
515 * want to get this fault so that we can fix the
516 * problem here and not every time the kernel is
519 if (frame->tf_rflags & PSL_NT) {
520 frame->tf_rflags &= ~PSL_NT;
525 case T_TRCTRAP: /* trace trap */
527 * Ignore debug register trace traps due to
528 * accesses in the user's address space, which
529 * can happen under several conditions such as
530 * if a user sets a watchpoint on a buffer and
531 * then passes that buffer to a system call.
532 * We still want to get TRCTRAPS for addresses
533 * in kernel space because that is useful when
534 * debugging the kernel.
536 if (user_dbreg_trap()) {
538 * Reset breakpoint bits because the
541 /* XXX check upper bits here */
542 load_dr6(rdr6() & 0xfffffff0);
546 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
550 * If KDB is enabled, let it handle the debugger trap.
551 * Otherwise, debugger traps "can't happen".
554 if (kdb_trap(type, 0, frame))
561 /* machine/parity/power fail/"kitchen sink" faults */
562 if (isa_nmi(code) == 0) {
565 * NMI can be hooked up to a pushbutton
569 printf ("NMI ... going to debugger\n");
570 kdb_trap(type, 0, frame);
574 } else if (panic_on_nmi == 0)
580 trap_fatal(frame, 0);
584 /* Translate fault for emulators (e.g. Linux) */
585 if (*p->p_sysent->sv_transtrap)
586 i = (*p->p_sysent->sv_transtrap)(i, type);
588 ksiginfo_init_trap(&ksi);
590 ksi.ksi_code = ucode;
591 ksi.ksi_trapno = type;
592 ksi.ksi_addr = (void *)addr;
593 if (uprintf_signal) {
594 uprintf("pid %d comm %s: signal %d err %lx code %d type %d "
595 "addr 0x%lx rsp 0x%lx rip 0x%lx "
596 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
597 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr,
598 frame->tf_rsp, frame->tf_rip,
599 fubyte((void *)(frame->tf_rip + 0)),
600 fubyte((void *)(frame->tf_rip + 1)),
601 fubyte((void *)(frame->tf_rip + 2)),
602 fubyte((void *)(frame->tf_rip + 3)),
603 fubyte((void *)(frame->tf_rip + 4)),
604 fubyte((void *)(frame->tf_rip + 5)),
605 fubyte((void *)(frame->tf_rip + 6)),
606 fubyte((void *)(frame->tf_rip + 7)));
608 KASSERT((read_rflags() & PSL_I) != 0, ("interrupts disabled"));
609 trapsignal(td, &ksi);
613 KASSERT(PCB_USER_FPU(td->td_pcb),
614 ("Return from trap with kernel FPU ctx leaked"));
621 trap_pfault(frame, usermode)
622 struct trapframe *frame;
630 struct thread *td = curthread;
631 struct proc *p = td->td_proc;
632 vm_offset_t eva = frame->tf_addr;
634 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
636 * Due to both processor errata and lazy TLB invalidation when
637 * access restrictions are removed from virtual pages, memory
638 * accesses that are allowed by the physical mapping layer may
639 * nonetheless cause one spurious page fault per virtual page.
640 * When the thread is executing a "no faulting" section that
641 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
642 * every page fault is treated as a spurious page fault,
643 * unless it accesses the same virtual address as the most
644 * recent page fault within the same "no faulting" section.
646 if (td->td_md.md_spurflt_addr != eva ||
647 (td->td_pflags & TDP_RESETSPUR) != 0) {
649 * Do nothing to the TLB. A stale TLB entry is
650 * flushed automatically by a page fault.
652 td->td_md.md_spurflt_addr = eva;
653 td->td_pflags &= ~TDP_RESETSPUR;
658 * If we get a page fault while in a critical section, then
659 * it is most likely a fatal kernel page fault. The kernel
660 * is already going to panic trying to get a sleep lock to
661 * do the VM lookup, so just consider it a fatal trap so the
662 * kernel can print out a useful trap message and even get
665 * If we get a page fault while holding a non-sleepable
666 * lock, then it is most likely a fatal kernel page fault.
667 * If WITNESS is enabled, then it's going to whine about
668 * bogus LORs with various VM locks, so just skip to the
669 * fatal trap handling directly.
671 if (td->td_critnest != 0 ||
672 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
673 "Kernel page fault") != 0) {
674 trap_fatal(frame, eva);
678 va = trunc_page(eva);
679 if (va >= VM_MIN_KERNEL_ADDRESS) {
681 * Don't allow user-mode faults in kernel address space.
689 * This is a fault on non-kernel virtual memory. If either
690 * p or p->p_vmspace is NULL, then the fault is fatal.
692 if (p == NULL || (vm = p->p_vmspace) == NULL)
698 * When accessing a usermode address, kernel must be
699 * ready to accept the page fault, and provide a
700 * handling routine. Since accessing the address
701 * without the handler is a bug, do not try to handle
702 * it normally, and panic immediately.
704 if (!usermode && (td->td_intr_nesting_level != 0 ||
705 curpcb->pcb_onfault == NULL)) {
706 trap_fatal(frame, eva);
712 * If the trap was caused by errant bits in the PTE then panic.
714 if (frame->tf_err & PGEX_RSV) {
715 trap_fatal(frame, eva);
720 * PGEX_I is defined only if the execute disable bit capability is
721 * supported and enabled.
723 if (frame->tf_err & PGEX_W)
724 ftype = VM_PROT_WRITE;
725 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
726 ftype = VM_PROT_EXECUTE;
728 ftype = VM_PROT_READ;
730 if (map != kernel_map) {
732 * Keep swapout from messing with us during this
739 /* Fault in the user page: */
740 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
747 * Don't have to worry about process locking or stacks in the
750 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
752 if (rv == KERN_SUCCESS) {
754 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
755 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
756 if (ftype == VM_PROT_READ)
757 PMC_SOFT_CALL_TF( , , page_fault, read,
760 PMC_SOFT_CALL_TF( , , page_fault, write,
768 if (td->td_intr_nesting_level == 0 &&
769 curpcb->pcb_onfault != NULL) {
770 frame->tf_rip = (long)curpcb->pcb_onfault;
773 if ((td->td_pflags & TDP_DEVMEMIO) != 0) {
774 KASSERT(curpcb->pcb_onfault != NULL,
775 ("/dev/mem without pcb_onfault"));
776 frame->tf_rip = (long)curpcb->pcb_onfault;
779 trap_fatal(frame, eva);
782 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
786 trap_fatal(frame, eva)
787 struct trapframe *frame;
793 struct soft_segment_descriptor softseg;
796 code = frame->tf_err;
797 type = frame->tf_trapno;
798 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
801 if (type <= MAX_TRAP_MSG)
802 msg = trap_msg[type];
805 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
806 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
808 /* two separate prints in case of a trap on an unmapped page */
809 printf("cpuid = %d; ", PCPU_GET(cpuid));
810 printf("apic id = %02x\n", PCPU_GET(apic_id));
812 if (type == T_PAGEFLT) {
813 printf("fault virtual address = 0x%lx\n", eva);
814 printf("fault code = %s %s %s%s, %s\n",
815 code & PGEX_U ? "user" : "supervisor",
816 code & PGEX_W ? "write" : "read",
817 code & PGEX_I ? "instruction" : "data",
818 code & PGEX_RSV ? " rsv" : "",
819 code & PGEX_P ? "protection violation" : "page not present");
821 printf("instruction pointer = 0x%lx:0x%lx\n",
822 frame->tf_cs & 0xffff, frame->tf_rip);
823 if (ISPL(frame->tf_cs) == SEL_UPL) {
824 ss = frame->tf_ss & 0xffff;
827 ss = GSEL(GDATA_SEL, SEL_KPL);
828 esp = (long)&frame->tf_rsp;
830 printf("stack pointer = 0x%x:0x%lx\n", ss, esp);
831 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
832 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
833 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
834 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
835 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
837 printf("processor eflags = ");
838 if (frame->tf_rflags & PSL_T)
839 printf("trace trap, ");
840 if (frame->tf_rflags & PSL_I)
841 printf("interrupt enabled, ");
842 if (frame->tf_rflags & PSL_NT)
843 printf("nested task, ");
844 if (frame->tf_rflags & PSL_RF)
846 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
847 printf("current process = ");
850 (u_long)curproc->p_pid, curthread->td_name ?
851 curthread->td_name : "");
857 if (debugger_on_panic || kdb_active)
858 if (kdb_trap(type, 0, frame))
861 printf("trap number = %d\n", type);
862 if (type <= MAX_TRAP_MSG)
863 panic("%s", trap_msg[type]);
865 panic("unknown/reserved trap");
869 * Double fault handler. Called when a fault occurs while writing
870 * a frame for a trap/exception onto the stack. This usually occurs
871 * when the stack overflows (such is the case with infinite recursion,
875 dblfault_handler(struct trapframe *frame)
878 if (dtrace_doubletrap_func != NULL)
879 (*dtrace_doubletrap_func)();
881 printf("\nFatal double fault\n");
882 printf("rip = 0x%lx\n", frame->tf_rip);
883 printf("rsp = 0x%lx\n", frame->tf_rsp);
884 printf("rbp = 0x%lx\n", frame->tf_rbp);
886 /* two separate prints in case of a trap on an unmapped page */
887 printf("cpuid = %d; ", PCPU_GET(cpuid));
888 printf("apic id = %02x\n", PCPU_GET(apic_id));
890 panic("double fault");
894 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
897 struct trapframe *frame;
900 int reg, regcnt, error;
903 frame = td->td_frame;
907 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
908 sa->code = frame->tf_rax;
910 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
911 sa->code = frame->tf_rdi;
915 if (p->p_sysent->sv_mask)
916 sa->code &= p->p_sysent->sv_mask;
918 if (sa->code >= p->p_sysent->sv_size)
919 sa->callp = &p->p_sysent->sv_table[0];
921 sa->callp = &p->p_sysent->sv_table[sa->code];
923 sa->narg = sa->callp->sy_narg;
924 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]),
925 ("Too many syscall arguments!"));
927 argp = &frame->tf_rdi;
929 bcopy(argp, sa->args, sizeof(sa->args[0]) * regcnt);
930 if (sa->narg > regcnt) {
931 KASSERT(params != NULL, ("copyin args with no params!"));
932 error = copyin(params, &sa->args[regcnt],
933 (sa->narg - regcnt) * sizeof(sa->args[0]));
937 td->td_retval[0] = 0;
938 td->td_retval[1] = frame->tf_rdx;
944 #include "../../kern/subr_syscall.c"
947 * System call handler for native binaries. The trap frame is already
948 * set up by the assembler trampoline and a pointer to it is saved in
952 amd64_syscall(struct thread *td, int traced)
954 struct syscall_args sa;
959 if (ISPL(td->td_frame->tf_cs) != SEL_UPL) {
964 error = syscallenter(td, &sa);
969 if (__predict_false(traced)) {
970 td->td_frame->tf_rflags &= ~PSL_T;
971 ksiginfo_init_trap(&ksi);
972 ksi.ksi_signo = SIGTRAP;
973 ksi.ksi_code = TRAP_TRACE;
974 ksi.ksi_addr = (void *)td->td_frame->tf_rip;
975 trapsignal(td, &ksi);
978 KASSERT(PCB_USER_FPU(td->td_pcb),
979 ("System call %s returing with kernel FPU ctx leaked",
980 syscallname(td->td_proc, sa.code)));
981 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
982 ("System call %s returning with mangled pcb_save",
983 syscallname(td->td_proc, sa.code)));
985 syscallret(td, error, &sa);
988 * If the user-supplied value of %rip is not a canonical
989 * address, then some CPUs will trigger a ring 0 #GP during
990 * the sysret instruction. However, the fault handler would
991 * execute in ring 0 with the user's %gs and %rsp which would
992 * not be safe. Instead, use the full return path which
993 * catches the problem safely.
995 if (td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS)
996 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);