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;
180 int i = 0, ucode = 0, code;
185 PCPU_INC(cnt.v_trap);
186 type = frame->tf_trapno;
189 /* Handler for NMI IPIs used for stopping CPUs. */
191 if (ipi_nmi_handler() == 0)
203 if (type == T_RESERVED) {
204 trap_fatal(frame, 0);
211 * CPU PMCs interrupt using an NMI. If the PMC module is
212 * active, pass the 'rip' value to the PMC module's interrupt
213 * handler. A non-zero return value from the handler means that
214 * the NMI was consumed by it and we can return immediately.
216 if (pmc_intr != NULL &&
217 (*pmc_intr)(PCPU_GET(cpuid), frame) != 0)
222 if (stack_nmi_handler(frame) != 0)
227 if (type == T_MCHK) {
232 if ((frame->tf_rflags & PSL_I) == 0) {
234 * Buggy application or kernel code has disabled
235 * interrupts and then trapped. Enabling interrupts
236 * now is wrong, but it is better than running with
237 * interrupts disabled until they are accidentally
240 if (TRAPF_USERMODE(frame))
242 "pid %ld (%s): trap %d with interrupts disabled\n",
243 (long)curproc->p_pid, curthread->td_name, type);
244 else if (type != T_NMI && type != T_BPTFLT &&
247 * XXX not quite right, since this may be for a
248 * multiple fault in user mode.
250 printf("kernel trap %d with interrupts disabled\n",
254 * We shouldn't enable interrupts while holding a
257 if (td->td_md.md_spinlock_count == 0)
262 code = frame->tf_err;
264 if (TRAPF_USERMODE(frame)) {
268 td->td_frame = frame;
269 addr = frame->tf_rip;
270 if (td->td_cowgen != p->p_cowgen)
271 thread_cow_update(td);
274 case T_PRIVINFLT: /* privileged instruction fault */
279 case T_BPTFLT: /* bpt instruction fault */
280 case T_TRCTRAP: /* trace trap */
283 if (type == T_BPTFLT) {
284 fill_frame_regs(frame, ®s);
285 if (dtrace_pid_probe_ptr != NULL &&
286 dtrace_pid_probe_ptr(®s) == 0)
290 frame->tf_rflags &= ~PSL_T;
292 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
295 case T_ARITHTRAP: /* arithmetic trap */
296 ucode = fputrap_x87();
302 case T_PROTFLT: /* general protection fault */
306 case T_STKFLT: /* stack fault */
307 case T_SEGNPFLT: /* segment not present fault */
311 case T_TSSFLT: /* invalid TSS fault */
319 case T_DOUBLEFLT: /* double fault */
325 case T_PAGEFLT: /* page fault */
327 * Emulator can take care about this trap?
329 if (*p->p_sysent->sv_trap != NULL &&
330 (*p->p_sysent->sv_trap)(td) == 0)
333 addr = frame->tf_addr;
334 i = trap_pfault(frame, TRUE);
343 if (prot_fault_translation == 0) {
346 * This check also covers the images
347 * without the ABI-tag ELF note.
349 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
350 && p->p_osrel >= P_OSREL_SIGSEGV) {
355 ucode = BUS_PAGE_FAULT;
357 } else if (prot_fault_translation == 1) {
359 * Always compat mode.
362 ucode = BUS_PAGE_FAULT;
365 * Always SIGSEGV mode.
373 case T_DIVIDE: /* integer divide fault */
380 /* machine/parity/power fail/"kitchen sink" faults */
381 if (isa_nmi(code) == 0) {
384 * NMI can be hooked up to a pushbutton
388 printf ("NMI ... going to debugger\n");
389 kdb_trap(type, 0, frame);
393 } else if (panic_on_nmi)
394 panic("NMI indicates hardware failure");
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 fill_frame_regs(frame, ®s);
430 if (dtrace_return_probe_ptr != NULL &&
431 dtrace_return_probe_ptr(®s) == 0)
439 KASSERT(cold || td->td_ucred != NULL,
440 ("kernel trap doesn't have ucred"));
442 case T_PAGEFLT: /* page fault */
443 (void) trap_pfault(frame, FALSE);
447 if (PCB_USER_FPU(td->td_pcb))
448 panic("Unregistered use of FPU in kernel");
452 case T_ARITHTRAP: /* arithmetic trap */
453 case T_XMMFLT: /* SIMD floating-point exception */
454 case T_FPOPFLT: /* FPU operand fetch fault */
456 * For now, supporting kernel handler
457 * registration for FPU traps is overkill.
459 trap_fatal(frame, 0);
462 case T_STKFLT: /* stack fault */
463 case T_PROTFLT: /* general protection fault */
464 case T_SEGNPFLT: /* segment not present fault */
465 if (td->td_intr_nesting_level != 0)
469 * Invalid segment selectors and out of bounds
470 * %rip's and %rsp's can be set up in user mode.
471 * This causes a fault in kernel mode when the
472 * kernel tries to return to user mode. We want
473 * to get this fault so that we can fix the
474 * problem here and not have to check all the
475 * selectors and pointers when the user changes
478 if (frame->tf_rip == (long)doreti_iret) {
479 frame->tf_rip = (long)doreti_iret_fault;
482 if (frame->tf_rip == (long)ld_ds) {
483 frame->tf_rip = (long)ds_load_fault;
486 if (frame->tf_rip == (long)ld_es) {
487 frame->tf_rip = (long)es_load_fault;
490 if (frame->tf_rip == (long)ld_fs) {
491 frame->tf_rip = (long)fs_load_fault;
494 if (frame->tf_rip == (long)ld_gs) {
495 frame->tf_rip = (long)gs_load_fault;
498 if (frame->tf_rip == (long)ld_gsbase) {
499 frame->tf_rip = (long)gsbase_load_fault;
502 if (frame->tf_rip == (long)ld_fsbase) {
503 frame->tf_rip = (long)fsbase_load_fault;
506 if (curpcb->pcb_onfault != NULL) {
507 frame->tf_rip = (long)curpcb->pcb_onfault;
514 * PSL_NT can be set in user mode and isn't cleared
515 * automatically when the kernel is entered. This
516 * causes a TSS fault when the kernel attempts to
517 * `iret' because the TSS link is uninitialized. We
518 * want to get this fault so that we can fix the
519 * problem here and not every time the kernel is
522 if (frame->tf_rflags & PSL_NT) {
523 frame->tf_rflags &= ~PSL_NT;
528 case T_TRCTRAP: /* trace trap */
530 * Ignore debug register trace traps due to
531 * accesses in the user's address space, which
532 * can happen under several conditions such as
533 * if a user sets a watchpoint on a buffer and
534 * then passes that buffer to a system call.
535 * We still want to get TRCTRAPS for addresses
536 * in kernel space because that is useful when
537 * debugging the kernel.
539 if (user_dbreg_trap()) {
541 * Reset breakpoint bits because the
544 load_dr6(rdr6() & ~0xf);
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 /* XXX %dr6 is not quite reentrant. */
558 load_dr6(dr6 & ~0x4000);
559 if (kdb_trap(type, dr6, frame))
566 /* machine/parity/power fail/"kitchen sink" faults */
567 if (isa_nmi(code) == 0) {
570 * NMI can be hooked up to a pushbutton
574 printf ("NMI ... going to debugger\n");
575 kdb_trap(type, 0, frame);
579 } else if (panic_on_nmi == 0)
585 trap_fatal(frame, 0);
589 /* Translate fault for emulators (e.g. Linux) */
590 if (*p->p_sysent->sv_transtrap)
591 i = (*p->p_sysent->sv_transtrap)(i, type);
593 ksiginfo_init_trap(&ksi);
595 ksi.ksi_code = ucode;
596 ksi.ksi_trapno = type;
597 ksi.ksi_addr = (void *)addr;
598 if (uprintf_signal) {
599 uprintf("pid %d comm %s: signal %d err %lx code %d type %d "
600 "addr 0x%lx rsp 0x%lx rip 0x%lx "
601 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
602 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr,
603 frame->tf_rsp, frame->tf_rip,
604 fubyte((void *)(frame->tf_rip + 0)),
605 fubyte((void *)(frame->tf_rip + 1)),
606 fubyte((void *)(frame->tf_rip + 2)),
607 fubyte((void *)(frame->tf_rip + 3)),
608 fubyte((void *)(frame->tf_rip + 4)),
609 fubyte((void *)(frame->tf_rip + 5)),
610 fubyte((void *)(frame->tf_rip + 6)),
611 fubyte((void *)(frame->tf_rip + 7)));
613 KASSERT((read_rflags() & PSL_I) != 0, ("interrupts disabled"));
614 trapsignal(td, &ksi);
618 KASSERT(PCB_USER_FPU(td->td_pcb),
619 ("Return from trap with kernel FPU ctx leaked"));
626 * Ensure that we ignore any DTrace-induced faults. This function cannot
627 * be instrumented, so it cannot generate such faults itself.
630 trap_check(struct trapframe *frame)
634 if (dtrace_trap_func != NULL &&
635 (*dtrace_trap_func)(frame, frame->tf_trapno) != 0)
642 trap_pfault(frame, usermode)
643 struct trapframe *frame;
650 struct thread *td = curthread;
651 struct proc *p = td->td_proc;
652 vm_offset_t eva = frame->tf_addr;
654 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
656 * Due to both processor errata and lazy TLB invalidation when
657 * access restrictions are removed from virtual pages, memory
658 * accesses that are allowed by the physical mapping layer may
659 * nonetheless cause one spurious page fault per virtual page.
660 * When the thread is executing a "no faulting" section that
661 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
662 * every page fault is treated as a spurious page fault,
663 * unless it accesses the same virtual address as the most
664 * recent page fault within the same "no faulting" section.
666 if (td->td_md.md_spurflt_addr != eva ||
667 (td->td_pflags & TDP_RESETSPUR) != 0) {
669 * Do nothing to the TLB. A stale TLB entry is
670 * flushed automatically by a page fault.
672 td->td_md.md_spurflt_addr = eva;
673 td->td_pflags &= ~TDP_RESETSPUR;
678 * If we get a page fault while in a critical section, then
679 * it is most likely a fatal kernel page fault. The kernel
680 * is already going to panic trying to get a sleep lock to
681 * do the VM lookup, so just consider it a fatal trap so the
682 * kernel can print out a useful trap message and even get
685 * If we get a page fault while holding a non-sleepable
686 * lock, then it is most likely a fatal kernel page fault.
687 * If WITNESS is enabled, then it's going to whine about
688 * bogus LORs with various VM locks, so just skip to the
689 * fatal trap handling directly.
691 if (td->td_critnest != 0 ||
692 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
693 "Kernel page fault") != 0) {
694 trap_fatal(frame, eva);
698 va = trunc_page(eva);
699 if (va >= VM_MIN_KERNEL_ADDRESS) {
701 * Don't allow user-mode faults in kernel address space.
708 map = &p->p_vmspace->vm_map;
711 * When accessing a usermode address, kernel must be
712 * ready to accept the page fault, and provide a
713 * handling routine. Since accessing the address
714 * without the handler is a bug, do not try to handle
715 * it normally, and panic immediately.
717 if (!usermode && (td->td_intr_nesting_level != 0 ||
718 curpcb->pcb_onfault == NULL)) {
719 trap_fatal(frame, eva);
725 * If the trap was caused by errant bits in the PTE then panic.
727 if (frame->tf_err & PGEX_RSV) {
728 trap_fatal(frame, eva);
733 * PGEX_I is defined only if the execute disable bit capability is
734 * supported and enabled.
736 if (frame->tf_err & PGEX_W)
737 ftype = VM_PROT_WRITE;
738 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
739 ftype = VM_PROT_EXECUTE;
741 ftype = VM_PROT_READ;
743 /* Fault in the page. */
744 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
745 if (rv == KERN_SUCCESS) {
747 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
748 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
749 if (ftype == VM_PROT_READ)
750 PMC_SOFT_CALL_TF( , , page_fault, read,
753 PMC_SOFT_CALL_TF( , , page_fault, write,
761 if (td->td_intr_nesting_level == 0 &&
762 curpcb->pcb_onfault != NULL) {
763 frame->tf_rip = (long)curpcb->pcb_onfault;
766 trap_fatal(frame, eva);
769 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
773 trap_fatal(frame, eva)
774 struct trapframe *frame;
780 struct soft_segment_descriptor softseg;
783 code = frame->tf_err;
784 type = frame->tf_trapno;
785 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
788 if (type <= MAX_TRAP_MSG)
789 msg = trap_msg[type];
792 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
793 TRAPF_USERMODE(frame) ? "user" : "kernel");
795 /* two separate prints in case of a trap on an unmapped page */
796 printf("cpuid = %d; ", PCPU_GET(cpuid));
797 printf("apic id = %02x\n", PCPU_GET(apic_id));
799 if (type == T_PAGEFLT) {
800 printf("fault virtual address = 0x%lx\n", eva);
801 printf("fault code = %s %s %s%s, %s\n",
802 code & PGEX_U ? "user" : "supervisor",
803 code & PGEX_W ? "write" : "read",
804 code & PGEX_I ? "instruction" : "data",
805 code & PGEX_RSV ? " rsv" : "",
806 code & PGEX_P ? "protection violation" : "page not present");
808 printf("instruction pointer = 0x%lx:0x%lx\n",
809 frame->tf_cs & 0xffff, frame->tf_rip);
810 if (TF_HAS_STACKREGS(frame)) {
811 ss = frame->tf_ss & 0xffff;
814 ss = GSEL(GDATA_SEL, SEL_KPL);
815 esp = (long)&frame->tf_rsp;
817 printf("stack pointer = 0x%x:0x%lx\n", ss, esp);
818 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
819 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
820 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
821 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
822 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
824 printf("processor eflags = ");
825 if (frame->tf_rflags & PSL_T)
826 printf("trace trap, ");
827 if (frame->tf_rflags & PSL_I)
828 printf("interrupt enabled, ");
829 if (frame->tf_rflags & PSL_NT)
830 printf("nested task, ");
831 if (frame->tf_rflags & PSL_RF)
833 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
834 printf("current process = %d (%s)\n",
835 curproc->p_pid, curthread->td_name);
838 if (debugger_on_panic || kdb_active)
839 if (kdb_trap(type, 0, frame))
842 printf("trap number = %d\n", type);
843 if (type <= MAX_TRAP_MSG)
844 panic("%s", trap_msg[type]);
846 panic("unknown/reserved trap");
850 * Double fault handler. Called when a fault occurs while writing
851 * a frame for a trap/exception onto the stack. This usually occurs
852 * when the stack overflows (such is the case with infinite recursion,
856 dblfault_handler(struct trapframe *frame)
859 if (dtrace_doubletrap_func != NULL)
860 (*dtrace_doubletrap_func)();
862 printf("\nFatal double fault\n");
863 printf("rip = 0x%lx\n", frame->tf_rip);
864 printf("rsp = 0x%lx\n", frame->tf_rsp);
865 printf("rbp = 0x%lx\n", frame->tf_rbp);
867 /* two separate prints in case of a trap on an unmapped page */
868 printf("cpuid = %d; ", PCPU_GET(cpuid));
869 printf("apic id = %02x\n", PCPU_GET(apic_id));
871 panic("double fault");
875 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
878 struct trapframe *frame;
881 int reg, regcnt, error;
884 frame = td->td_frame;
888 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
889 sa->code = frame->tf_rax;
891 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
892 sa->code = frame->tf_rdi;
896 if (p->p_sysent->sv_mask)
897 sa->code &= p->p_sysent->sv_mask;
899 if (sa->code >= p->p_sysent->sv_size)
900 sa->callp = &p->p_sysent->sv_table[0];
902 sa->callp = &p->p_sysent->sv_table[sa->code];
904 sa->narg = sa->callp->sy_narg;
905 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]),
906 ("Too many syscall arguments!"));
908 argp = &frame->tf_rdi;
910 bcopy(argp, sa->args, sizeof(sa->args[0]) * regcnt);
911 if (sa->narg > regcnt) {
912 KASSERT(params != NULL, ("copyin args with no params!"));
913 error = copyin(params, &sa->args[regcnt],
914 (sa->narg - regcnt) * sizeof(sa->args[0]));
918 td->td_retval[0] = 0;
919 td->td_retval[1] = frame->tf_rdx;
925 #include "../../kern/subr_syscall.c"
928 * System call handler for native binaries. The trap frame is already
929 * set up by the assembler trampoline and a pointer to it is saved in
933 amd64_syscall(struct thread *td, int traced)
935 struct syscall_args sa;
940 if (!TRAPF_USERMODE(td->td_frame)) {
945 error = syscallenter(td, &sa);
950 if (__predict_false(traced)) {
951 td->td_frame->tf_rflags &= ~PSL_T;
952 ksiginfo_init_trap(&ksi);
953 ksi.ksi_signo = SIGTRAP;
954 ksi.ksi_code = TRAP_TRACE;
955 ksi.ksi_addr = (void *)td->td_frame->tf_rip;
956 trapsignal(td, &ksi);
959 KASSERT(PCB_USER_FPU(td->td_pcb),
960 ("System call %s returing with kernel FPU ctx leaked",
961 syscallname(td->td_proc, sa.code)));
962 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
963 ("System call %s returning with mangled pcb_save",
964 syscallname(td->td_proc, sa.code)));
965 KASSERT(td->td_md.md_invl_gen.gen == 0,
966 ("System call %s returning with leaked invl_gen %lu",
967 syscallname(td->td_proc, sa.code), td->td_md.md_invl_gen.gen));
970 syscallret(td, error, &sa);
973 * If the user-supplied value of %rip is not a canonical
974 * address, then some CPUs will trigger a ring 0 #GP during
975 * the sysret instruction. However, the fault handler would
976 * execute in ring 0 with the user's %gs and %rsp which would
977 * not be safe. Instead, use the full return path which
978 * catches the problem safely.
980 if (td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS)
981 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);