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_stack.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/stack.h>
96 #include <machine/tss.h>
99 #include <sys/dtrace_bsd.h>
102 extern void __noinline trap(struct trapframe *frame);
103 extern void trap_check(struct trapframe *frame);
104 extern void syscall(struct trapframe *frame);
105 void dblfault_handler(struct trapframe *frame);
107 static int trap_pfault(struct trapframe *, int);
108 static void trap_fatal(struct trapframe *, vm_offset_t);
110 #define MAX_TRAP_MSG 32
111 static char *trap_msg[] = {
113 "privileged instruction fault", /* 1 T_PRIVINFLT */
115 "breakpoint instruction fault", /* 3 T_BPTFLT */
118 "arithmetic trap", /* 6 T_ARITHTRAP */
121 "general protection fault", /* 9 T_PROTFLT */
122 "trace trap", /* 10 T_TRCTRAP */
124 "page fault", /* 12 T_PAGEFLT */
126 "alignment fault", /* 14 T_ALIGNFLT */
130 "integer divide fault", /* 18 T_DIVIDE */
131 "non-maskable interrupt trap", /* 19 T_NMI */
132 "overflow trap", /* 20 T_OFLOW */
133 "FPU bounds check fault", /* 21 T_BOUND */
134 "FPU device not available", /* 22 T_DNA */
135 "double fault", /* 23 T_DOUBLEFLT */
136 "FPU operand fetch fault", /* 24 T_FPOPFLT */
137 "invalid TSS fault", /* 25 T_TSSFLT */
138 "segment not present fault", /* 26 T_SEGNPFLT */
139 "stack fault", /* 27 T_STKFLT */
140 "machine check trap", /* 28 T_MCHK */
141 "SIMD floating-point exception", /* 29 T_XMMFLT */
142 "reserved (unknown) fault", /* 30 T_RESERVED */
143 "", /* 31 unused (reserved) */
144 "DTrace pid return trap", /* 32 T_DTRACE_RET */
148 static int kdb_on_nmi = 1;
149 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RWTUN,
150 &kdb_on_nmi, 0, "Go to KDB on NMI");
152 static int panic_on_nmi = 1;
153 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RWTUN,
154 &panic_on_nmi, 0, "Panic on NMI");
155 static int prot_fault_translation;
156 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN,
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_RWTUN,
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);
210 * CPU PMCs interrupt using an NMI. If the PMC module is
211 * active, pass the 'rip' value to the PMC module's interrupt
212 * handler. A non-zero return value from the handler means that
213 * the NMI was consumed by it and we can return immediately.
215 if (pmc_intr != NULL &&
216 (*pmc_intr)(PCPU_GET(cpuid), frame) != 0)
221 if (stack_nmi_handler(frame) != 0)
226 if (type == T_MCHK) {
231 if ((frame->tf_rflags & PSL_I) == 0) {
233 * Buggy application or kernel code has disabled
234 * interrupts and then trapped. Enabling interrupts
235 * now is wrong, but it is better than running with
236 * interrupts disabled until they are accidentally
239 if (ISPL(frame->tf_cs) == SEL_UPL)
241 "pid %ld (%s): trap %d with interrupts disabled\n",
242 (long)curproc->p_pid, curthread->td_name, type);
243 else if (type != T_NMI && type != T_BPTFLT &&
246 * XXX not quite right, since this may be for a
247 * multiple fault in user mode.
249 printf("kernel trap %d with interrupts disabled\n",
253 * We shouldn't enable interrupts while holding a
256 if (td->td_md.md_spinlock_count == 0)
261 code = frame->tf_err;
263 if (ISPL(frame->tf_cs) == SEL_UPL) {
267 td->td_frame = frame;
268 addr = frame->tf_rip;
269 if (td->td_cowgen != p->p_cowgen)
270 thread_cow_update(td);
273 case T_PRIVINFLT: /* privileged instruction fault */
278 case T_BPTFLT: /* bpt instruction fault */
279 case T_TRCTRAP: /* trace trap */
282 if (type == T_BPTFLT) {
283 fill_frame_regs(frame, ®s);
284 if (dtrace_pid_probe_ptr != NULL &&
285 dtrace_pid_probe_ptr(®s) == 0)
289 frame->tf_rflags &= ~PSL_T;
291 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
294 case T_ARITHTRAP: /* arithmetic trap */
295 ucode = fputrap_x87();
301 case T_PROTFLT: /* general protection fault */
305 case T_STKFLT: /* stack fault */
306 case T_SEGNPFLT: /* segment not present fault */
310 case T_TSSFLT: /* invalid TSS fault */
318 case T_DOUBLEFLT: /* double fault */
324 case T_PAGEFLT: /* page fault */
326 * Emulator can take care about this trap?
328 if (*p->p_sysent->sv_trap != NULL &&
329 (*p->p_sysent->sv_trap)(td) == 0)
332 addr = frame->tf_addr;
333 i = trap_pfault(frame, TRUE);
342 if (prot_fault_translation == 0) {
345 * This check also covers the images
346 * without the ABI-tag ELF note.
348 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
349 && p->p_osrel >= P_OSREL_SIGSEGV) {
354 ucode = BUS_PAGE_FAULT;
356 } else if (prot_fault_translation == 1) {
358 * Always compat mode.
361 ucode = BUS_PAGE_FAULT;
364 * Always SIGSEGV mode.
372 case T_DIVIDE: /* integer divide fault */
379 /* machine/parity/power fail/"kitchen sink" faults */
380 if (isa_nmi(code) == 0) {
383 * NMI can be hooked up to a pushbutton
387 printf ("NMI ... going to debugger\n");
388 kdb_trap(type, 0, frame);
392 } else if (panic_on_nmi)
393 panic("NMI indicates hardware failure");
397 case T_OFLOW: /* integer overflow fault */
402 case T_BOUND: /* bounds check fault */
408 /* transparent fault (due to context switch "late") */
409 KASSERT(PCB_USER_FPU(td->td_pcb),
410 ("kernel FPU ctx has leaked"));
414 case T_FPOPFLT: /* FPU operand fetch fault */
419 case T_XMMFLT: /* SIMD floating-point exception */
420 ucode = fputrap_sse();
428 fill_frame_regs(frame, ®s);
429 if (dtrace_return_probe_ptr != NULL &&
430 dtrace_return_probe_ptr(®s) == 0)
438 KASSERT(cold || td->td_ucred != NULL,
439 ("kernel trap doesn't have ucred"));
441 case T_PAGEFLT: /* page fault */
442 (void) trap_pfault(frame, FALSE);
446 KASSERT(!PCB_USER_FPU(td->td_pcb),
447 ("Unregistered use of FPU in kernel"));
451 case T_ARITHTRAP: /* arithmetic trap */
452 case T_XMMFLT: /* SIMD floating-point exception */
453 case T_FPOPFLT: /* FPU operand fetch fault */
455 * For now, supporting kernel handler
456 * registration for FPU traps is overkill.
458 trap_fatal(frame, 0);
461 case T_STKFLT: /* stack fault */
462 case T_PROTFLT: /* general protection fault */
463 case T_SEGNPFLT: /* segment not present fault */
464 if (td->td_intr_nesting_level != 0)
468 * Invalid segment selectors and out of bounds
469 * %rip's and %rsp's can be set up in user mode.
470 * This causes a fault in kernel mode when the
471 * kernel tries to return to user mode. We want
472 * to get this fault so that we can fix the
473 * problem here and not have to check all the
474 * selectors and pointers when the user changes
477 if (frame->tf_rip == (long)doreti_iret) {
478 frame->tf_rip = (long)doreti_iret_fault;
481 if (frame->tf_rip == (long)ld_ds) {
482 frame->tf_rip = (long)ds_load_fault;
485 if (frame->tf_rip == (long)ld_es) {
486 frame->tf_rip = (long)es_load_fault;
489 if (frame->tf_rip == (long)ld_fs) {
490 frame->tf_rip = (long)fs_load_fault;
493 if (frame->tf_rip == (long)ld_gs) {
494 frame->tf_rip = (long)gs_load_fault;
497 if (frame->tf_rip == (long)ld_gsbase) {
498 frame->tf_rip = (long)gsbase_load_fault;
501 if (frame->tf_rip == (long)ld_fsbase) {
502 frame->tf_rip = (long)fsbase_load_fault;
505 if (curpcb->pcb_onfault != NULL) {
506 frame->tf_rip = (long)curpcb->pcb_onfault;
513 * PSL_NT can be set in user mode and isn't cleared
514 * automatically when the kernel is entered. This
515 * causes a TSS fault when the kernel attempts to
516 * `iret' because the TSS link is uninitialized. We
517 * want to get this fault so that we can fix the
518 * problem here and not every time the kernel is
521 if (frame->tf_rflags & PSL_NT) {
522 frame->tf_rflags &= ~PSL_NT;
527 case T_TRCTRAP: /* trace trap */
529 * Ignore debug register trace traps due to
530 * accesses in the user's address space, which
531 * can happen under several conditions such as
532 * if a user sets a watchpoint on a buffer and
533 * then passes that buffer to a system call.
534 * We still want to get TRCTRAPS for addresses
535 * in kernel space because that is useful when
536 * debugging the kernel.
538 if (user_dbreg_trap()) {
540 * Reset breakpoint bits because the
543 /* XXX check upper bits here */
544 load_dr6(rdr6() & 0xfffffff0);
548 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
552 * If KDB is enabled, let it handle the debugger trap.
553 * Otherwise, debugger traps "can't happen".
556 if (kdb_trap(type, 0, frame))
563 /* machine/parity/power fail/"kitchen sink" faults */
564 if (isa_nmi(code) == 0) {
567 * NMI can be hooked up to a pushbutton
571 printf ("NMI ... going to debugger\n");
572 kdb_trap(type, 0, frame);
576 } else if (panic_on_nmi == 0)
582 trap_fatal(frame, 0);
586 /* Translate fault for emulators (e.g. Linux) */
587 if (*p->p_sysent->sv_transtrap)
588 i = (*p->p_sysent->sv_transtrap)(i, type);
590 ksiginfo_init_trap(&ksi);
592 ksi.ksi_code = ucode;
593 ksi.ksi_trapno = type;
594 ksi.ksi_addr = (void *)addr;
595 if (uprintf_signal) {
596 uprintf("pid %d comm %s: signal %d err %lx code %d type %d "
597 "addr 0x%lx rsp 0x%lx rip 0x%lx "
598 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
599 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr,
600 frame->tf_rsp, frame->tf_rip,
601 fubyte((void *)(frame->tf_rip + 0)),
602 fubyte((void *)(frame->tf_rip + 1)),
603 fubyte((void *)(frame->tf_rip + 2)),
604 fubyte((void *)(frame->tf_rip + 3)),
605 fubyte((void *)(frame->tf_rip + 4)),
606 fubyte((void *)(frame->tf_rip + 5)),
607 fubyte((void *)(frame->tf_rip + 6)),
608 fubyte((void *)(frame->tf_rip + 7)));
610 KASSERT((read_rflags() & PSL_I) != 0, ("interrupts disabled"));
611 trapsignal(td, &ksi);
615 KASSERT(PCB_USER_FPU(td->td_pcb),
616 ("Return from trap with kernel FPU ctx leaked"));
623 * Ensure that we ignore any DTrace-induced faults. This function cannot
624 * be instrumented, so it cannot generate such faults itself.
627 trap_check(struct trapframe *frame)
631 if (dtrace_trap_func != NULL &&
632 (*dtrace_trap_func)(frame, frame->tf_trapno) != 0)
639 trap_pfault(frame, usermode)
640 struct trapframe *frame;
647 struct thread *td = curthread;
648 struct proc *p = td->td_proc;
649 vm_offset_t eva = frame->tf_addr;
651 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
653 * Due to both processor errata and lazy TLB invalidation when
654 * access restrictions are removed from virtual pages, memory
655 * accesses that are allowed by the physical mapping layer may
656 * nonetheless cause one spurious page fault per virtual page.
657 * When the thread is executing a "no faulting" section that
658 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
659 * every page fault is treated as a spurious page fault,
660 * unless it accesses the same virtual address as the most
661 * recent page fault within the same "no faulting" section.
663 if (td->td_md.md_spurflt_addr != eva ||
664 (td->td_pflags & TDP_RESETSPUR) != 0) {
666 * Do nothing to the TLB. A stale TLB entry is
667 * flushed automatically by a page fault.
669 td->td_md.md_spurflt_addr = eva;
670 td->td_pflags &= ~TDP_RESETSPUR;
675 * If we get a page fault while in a critical section, then
676 * it is most likely a fatal kernel page fault. The kernel
677 * is already going to panic trying to get a sleep lock to
678 * do the VM lookup, so just consider it a fatal trap so the
679 * kernel can print out a useful trap message and even get
682 * If we get a page fault while holding a non-sleepable
683 * lock, then it is most likely a fatal kernel page fault.
684 * If WITNESS is enabled, then it's going to whine about
685 * bogus LORs with various VM locks, so just skip to the
686 * fatal trap handling directly.
688 if (td->td_critnest != 0 ||
689 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
690 "Kernel page fault") != 0) {
691 trap_fatal(frame, eva);
695 va = trunc_page(eva);
696 if (va >= VM_MIN_KERNEL_ADDRESS) {
698 * Don't allow user-mode faults in kernel address space.
705 map = &p->p_vmspace->vm_map;
708 * When accessing a usermode address, kernel must be
709 * ready to accept the page fault, and provide a
710 * handling routine. Since accessing the address
711 * without the handler is a bug, do not try to handle
712 * it normally, and panic immediately.
714 if (!usermode && (td->td_intr_nesting_level != 0 ||
715 curpcb->pcb_onfault == NULL)) {
716 trap_fatal(frame, eva);
722 * If the trap was caused by errant bits in the PTE then panic.
724 if (frame->tf_err & PGEX_RSV) {
725 trap_fatal(frame, eva);
730 * PGEX_I is defined only if the execute disable bit capability is
731 * supported and enabled.
733 if (frame->tf_err & PGEX_W)
734 ftype = VM_PROT_WRITE;
735 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
736 ftype = VM_PROT_EXECUTE;
738 ftype = VM_PROT_READ;
740 /* Fault in the page. */
741 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
742 if (rv == KERN_SUCCESS) {
744 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
745 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
746 if (ftype == VM_PROT_READ)
747 PMC_SOFT_CALL_TF( , , page_fault, read,
750 PMC_SOFT_CALL_TF( , , page_fault, write,
758 if (td->td_intr_nesting_level == 0 &&
759 curpcb->pcb_onfault != NULL) {
760 frame->tf_rip = (long)curpcb->pcb_onfault;
763 trap_fatal(frame, eva);
766 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
770 trap_fatal(frame, eva)
771 struct trapframe *frame;
777 struct soft_segment_descriptor softseg;
780 code = frame->tf_err;
781 type = frame->tf_trapno;
782 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
785 if (type <= MAX_TRAP_MSG)
786 msg = trap_msg[type];
789 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
790 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
792 /* two separate prints in case of a trap on an unmapped page */
793 printf("cpuid = %d; ", PCPU_GET(cpuid));
794 printf("apic id = %02x\n", PCPU_GET(apic_id));
796 if (type == T_PAGEFLT) {
797 printf("fault virtual address = 0x%lx\n", eva);
798 printf("fault code = %s %s %s%s, %s\n",
799 code & PGEX_U ? "user" : "supervisor",
800 code & PGEX_W ? "write" : "read",
801 code & PGEX_I ? "instruction" : "data",
802 code & PGEX_RSV ? " rsv" : "",
803 code & PGEX_P ? "protection violation" : "page not present");
805 printf("instruction pointer = 0x%lx:0x%lx\n",
806 frame->tf_cs & 0xffff, frame->tf_rip);
807 if (ISPL(frame->tf_cs) == SEL_UPL) {
808 ss = frame->tf_ss & 0xffff;
811 ss = GSEL(GDATA_SEL, SEL_KPL);
812 esp = (long)&frame->tf_rsp;
814 printf("stack pointer = 0x%x:0x%lx\n", ss, esp);
815 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
816 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
817 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
818 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
819 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
821 printf("processor eflags = ");
822 if (frame->tf_rflags & PSL_T)
823 printf("trace trap, ");
824 if (frame->tf_rflags & PSL_I)
825 printf("interrupt enabled, ");
826 if (frame->tf_rflags & PSL_NT)
827 printf("nested task, ");
828 if (frame->tf_rflags & PSL_RF)
830 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
831 printf("current process = %d (%s)\n",
832 curproc->p_pid, curthread->td_name);
835 if (debugger_on_panic || kdb_active)
836 if (kdb_trap(type, 0, frame))
839 printf("trap number = %d\n", type);
840 if (type <= MAX_TRAP_MSG)
841 panic("%s", trap_msg[type]);
843 panic("unknown/reserved trap");
847 * Double fault handler. Called when a fault occurs while writing
848 * a frame for a trap/exception onto the stack. This usually occurs
849 * when the stack overflows (such is the case with infinite recursion,
853 dblfault_handler(struct trapframe *frame)
856 if (dtrace_doubletrap_func != NULL)
857 (*dtrace_doubletrap_func)();
859 printf("\nFatal double fault\n");
860 printf("rip = 0x%lx\n", frame->tf_rip);
861 printf("rsp = 0x%lx\n", frame->tf_rsp);
862 printf("rbp = 0x%lx\n", frame->tf_rbp);
864 /* two separate prints in case of a trap on an unmapped page */
865 printf("cpuid = %d; ", PCPU_GET(cpuid));
866 printf("apic id = %02x\n", PCPU_GET(apic_id));
868 panic("double fault");
872 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
875 struct trapframe *frame;
878 int reg, regcnt, error;
881 frame = td->td_frame;
885 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
886 sa->code = frame->tf_rax;
888 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
889 sa->code = frame->tf_rdi;
893 if (p->p_sysent->sv_mask)
894 sa->code &= p->p_sysent->sv_mask;
896 if (sa->code >= p->p_sysent->sv_size)
897 sa->callp = &p->p_sysent->sv_table[0];
899 sa->callp = &p->p_sysent->sv_table[sa->code];
901 sa->narg = sa->callp->sy_narg;
902 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]),
903 ("Too many syscall arguments!"));
905 argp = &frame->tf_rdi;
907 bcopy(argp, sa->args, sizeof(sa->args[0]) * regcnt);
908 if (sa->narg > regcnt) {
909 KASSERT(params != NULL, ("copyin args with no params!"));
910 error = copyin(params, &sa->args[regcnt],
911 (sa->narg - regcnt) * sizeof(sa->args[0]));
915 td->td_retval[0] = 0;
916 td->td_retval[1] = frame->tf_rdx;
922 #include "../../kern/subr_syscall.c"
925 * System call handler for native binaries. The trap frame is already
926 * set up by the assembler trampoline and a pointer to it is saved in
930 amd64_syscall(struct thread *td, int traced)
932 struct syscall_args sa;
937 if (ISPL(td->td_frame->tf_cs) != SEL_UPL) {
942 error = syscallenter(td, &sa);
947 if (__predict_false(traced)) {
948 td->td_frame->tf_rflags &= ~PSL_T;
949 ksiginfo_init_trap(&ksi);
950 ksi.ksi_signo = SIGTRAP;
951 ksi.ksi_code = TRAP_TRACE;
952 ksi.ksi_addr = (void *)td->td_frame->tf_rip;
953 trapsignal(td, &ksi);
956 KASSERT(PCB_USER_FPU(td->td_pcb),
957 ("System call %s returing with kernel FPU ctx leaked",
958 syscallname(td->td_proc, sa.code)));
959 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
960 ("System call %s returning with mangled pcb_save",
961 syscallname(td->td_proc, sa.code)));
963 syscallret(td, error, &sa);
966 * If the user-supplied value of %rip is not a canonical
967 * address, then some CPUs will trigger a ring 0 #GP during
968 * the sysret instruction. However, the fault handler would
969 * execute in ring 0 with the user's %gs and %rsp which would
970 * not be safe. Instead, use the full return path which
971 * catches the problem safely.
973 if (td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS)
974 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);