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"
53 #include <sys/param.h>
55 #include <sys/systm.h>
57 #include <sys/pioctl.h>
58 #include <sys/ptrace.h>
60 #include <sys/kernel.h>
63 #include <sys/mutex.h>
64 #include <sys/resourcevar.h>
65 #include <sys/signalvar.h>
66 #include <sys/syscall.h>
67 #include <sys/sysctl.h>
68 #include <sys/sysent.h>
70 #include <sys/vmmeter.h>
72 #include <sys/pmckern.h>
73 PMC_SOFT_DEFINE( , , page_fault, all);
74 PMC_SOFT_DEFINE( , , page_fault, read);
75 PMC_SOFT_DEFINE( , , page_fault, write);
79 #include <vm/vm_param.h>
81 #include <vm/vm_kern.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_extern.h>
86 #include <machine/cpu.h>
87 #include <machine/intr_machdep.h>
89 #include <machine/md_var.h>
90 #include <machine/pcb.h>
92 #include <machine/smp.h>
94 #include <machine/tss.h>
97 #include <sys/dtrace_bsd.h>
100 extern void __noinline trap(struct trapframe *frame);
101 extern void trap_check(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_RWTUN,
148 &kdb_on_nmi, 0, "Go to KDB on NMI");
150 static int panic_on_nmi = 1;
151 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RWTUN,
152 &panic_on_nmi, 0, "Panic on NMI");
153 static int prot_fault_translation;
154 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN,
155 &prot_fault_translation, 0,
156 "Select signal to deliver on protection fault");
157 static int uprintf_signal;
158 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RWTUN,
160 "Print debugging information on trap signal to ctty");
163 * Exception, fault, and trap interface to the FreeBSD kernel.
164 * This common code is called from assembly language IDT gate entry
165 * routines that prepare a suitable stack frame, and restore this
166 * frame after the exception has been processed.
170 trap(struct trapframe *frame)
175 struct thread *td = curthread;
176 struct proc *p = td->td_proc;
177 int i = 0, ucode = 0, code;
182 PCPU_INC(cnt.v_trap);
183 type = frame->tf_trapno;
186 /* Handler for NMI IPIs used for stopping CPUs. */
188 if (ipi_nmi_handler() == 0)
200 if (type == T_RESERVED) {
201 trap_fatal(frame, 0);
207 * CPU PMCs interrupt using an NMI. If the PMC module is
208 * active, pass the 'rip' value to the PMC module's interrupt
209 * handler. A return value of '1' from the handler means that
210 * the NMI was handled by it and we can return immediately.
212 if (type == T_NMI && pmc_intr &&
213 (*pmc_intr)(PCPU_GET(cpuid), frame))
217 if (type == T_MCHK) {
222 if ((frame->tf_rflags & PSL_I) == 0) {
224 * Buggy application or kernel code has disabled
225 * interrupts and then trapped. Enabling interrupts
226 * now is wrong, but it is better than running with
227 * interrupts disabled until they are accidentally
230 if (ISPL(frame->tf_cs) == SEL_UPL)
232 "pid %ld (%s): trap %d with interrupts disabled\n",
233 (long)curproc->p_pid, curthread->td_name, type);
234 else if (type != T_NMI && type != T_BPTFLT &&
237 * XXX not quite right, since this may be for a
238 * multiple fault in user mode.
240 printf("kernel trap %d with interrupts disabled\n",
244 * We shouldn't enable interrupts while holding a
247 if (td->td_md.md_spinlock_count == 0)
252 code = frame->tf_err;
254 if (ISPL(frame->tf_cs) == SEL_UPL) {
258 td->td_frame = frame;
259 addr = frame->tf_rip;
260 if (td->td_ucred != p->p_ucred)
261 cred_update_thread(td);
264 case T_PRIVINFLT: /* privileged instruction fault */
269 case T_BPTFLT: /* bpt instruction fault */
270 case T_TRCTRAP: /* trace trap */
273 if (type == T_BPTFLT) {
274 fill_frame_regs(frame, ®s);
275 if (dtrace_pid_probe_ptr != NULL &&
276 dtrace_pid_probe_ptr(®s) == 0)
280 frame->tf_rflags &= ~PSL_T;
282 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
285 case T_ARITHTRAP: /* arithmetic trap */
286 ucode = fputrap_x87();
292 case T_PROTFLT: /* general protection fault */
296 case T_STKFLT: /* stack fault */
297 case T_SEGNPFLT: /* segment not present fault */
301 case T_TSSFLT: /* invalid TSS fault */
309 case T_DOUBLEFLT: /* double fault */
315 case T_PAGEFLT: /* page fault */
316 addr = frame->tf_addr;
317 i = trap_pfault(frame, TRUE);
326 if (prot_fault_translation == 0) {
329 * This check also covers the images
330 * without the ABI-tag ELF note.
332 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
333 && p->p_osrel >= P_OSREL_SIGSEGV) {
338 ucode = BUS_PAGE_FAULT;
340 } else if (prot_fault_translation == 1) {
342 * Always compat mode.
345 ucode = BUS_PAGE_FAULT;
348 * Always SIGSEGV mode.
356 case T_DIVIDE: /* integer divide fault */
363 /* machine/parity/power fail/"kitchen sink" faults */
364 if (isa_nmi(code) == 0) {
367 * NMI can be hooked up to a pushbutton
371 printf ("NMI ... going to debugger\n");
372 kdb_trap(type, 0, frame);
376 } else if (panic_on_nmi)
377 panic("NMI indicates hardware failure");
381 case T_OFLOW: /* integer overflow fault */
386 case T_BOUND: /* bounds check fault */
392 /* transparent fault (due to context switch "late") */
393 KASSERT(PCB_USER_FPU(td->td_pcb),
394 ("kernel FPU ctx has leaked"));
398 case T_FPOPFLT: /* FPU operand fetch fault */
403 case T_XMMFLT: /* SIMD floating-point exception */
404 ucode = fputrap_sse();
412 fill_frame_regs(frame, ®s);
413 if (dtrace_return_probe_ptr != NULL &&
414 dtrace_return_probe_ptr(®s) == 0)
422 KASSERT(cold || td->td_ucred != NULL,
423 ("kernel trap doesn't have ucred"));
425 case T_PAGEFLT: /* page fault */
426 (void) trap_pfault(frame, FALSE);
430 KASSERT(!PCB_USER_FPU(td->td_pcb),
431 ("Unregistered use of FPU in kernel"));
435 case T_ARITHTRAP: /* arithmetic trap */
436 case T_XMMFLT: /* SIMD floating-point exception */
437 case T_FPOPFLT: /* FPU operand fetch fault */
439 * For now, supporting kernel handler
440 * registration for FPU traps is overkill.
442 trap_fatal(frame, 0);
445 case T_STKFLT: /* stack fault */
446 case T_PROTFLT: /* general protection fault */
447 case T_SEGNPFLT: /* segment not present fault */
448 if (td->td_intr_nesting_level != 0)
452 * Invalid segment selectors and out of bounds
453 * %rip's and %rsp's can be set up in user mode.
454 * This causes a fault in kernel mode when the
455 * kernel tries to return to user mode. We want
456 * to get this fault so that we can fix the
457 * problem here and not have to check all the
458 * selectors and pointers when the user changes
461 if (frame->tf_rip == (long)doreti_iret) {
462 frame->tf_rip = (long)doreti_iret_fault;
465 if (frame->tf_rip == (long)ld_ds) {
466 frame->tf_rip = (long)ds_load_fault;
469 if (frame->tf_rip == (long)ld_es) {
470 frame->tf_rip = (long)es_load_fault;
473 if (frame->tf_rip == (long)ld_fs) {
474 frame->tf_rip = (long)fs_load_fault;
477 if (frame->tf_rip == (long)ld_gs) {
478 frame->tf_rip = (long)gs_load_fault;
481 if (frame->tf_rip == (long)ld_gsbase) {
482 frame->tf_rip = (long)gsbase_load_fault;
485 if (frame->tf_rip == (long)ld_fsbase) {
486 frame->tf_rip = (long)fsbase_load_fault;
489 if (curpcb->pcb_onfault != NULL) {
490 frame->tf_rip = (long)curpcb->pcb_onfault;
497 * PSL_NT can be set in user mode and isn't cleared
498 * automatically when the kernel is entered. This
499 * causes a TSS fault when the kernel attempts to
500 * `iret' because the TSS link is uninitialized. We
501 * want to get this fault so that we can fix the
502 * problem here and not every time the kernel is
505 if (frame->tf_rflags & PSL_NT) {
506 frame->tf_rflags &= ~PSL_NT;
511 case T_TRCTRAP: /* trace trap */
513 * Ignore debug register trace traps due to
514 * accesses in the user's address space, which
515 * can happen under several conditions such as
516 * if a user sets a watchpoint on a buffer and
517 * then passes that buffer to a system call.
518 * We still want to get TRCTRAPS for addresses
519 * in kernel space because that is useful when
520 * debugging the kernel.
522 if (user_dbreg_trap()) {
524 * Reset breakpoint bits because the
527 /* XXX check upper bits here */
528 load_dr6(rdr6() & 0xfffffff0);
532 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
536 * If KDB is enabled, let it handle the debugger trap.
537 * Otherwise, debugger traps "can't happen".
540 if (kdb_trap(type, 0, frame))
547 /* machine/parity/power fail/"kitchen sink" faults */
548 if (isa_nmi(code) == 0) {
551 * NMI can be hooked up to a pushbutton
555 printf ("NMI ... going to debugger\n");
556 kdb_trap(type, 0, frame);
560 } else if (panic_on_nmi == 0)
566 trap_fatal(frame, 0);
570 /* Translate fault for emulators (e.g. Linux) */
571 if (*p->p_sysent->sv_transtrap)
572 i = (*p->p_sysent->sv_transtrap)(i, type);
574 ksiginfo_init_trap(&ksi);
576 ksi.ksi_code = ucode;
577 ksi.ksi_trapno = type;
578 ksi.ksi_addr = (void *)addr;
579 if (uprintf_signal) {
580 uprintf("pid %d comm %s: signal %d err %lx code %d type %d "
581 "addr 0x%lx rsp 0x%lx rip 0x%lx "
582 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
583 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr,
584 frame->tf_rsp, frame->tf_rip,
585 fubyte((void *)(frame->tf_rip + 0)),
586 fubyte((void *)(frame->tf_rip + 1)),
587 fubyte((void *)(frame->tf_rip + 2)),
588 fubyte((void *)(frame->tf_rip + 3)),
589 fubyte((void *)(frame->tf_rip + 4)),
590 fubyte((void *)(frame->tf_rip + 5)),
591 fubyte((void *)(frame->tf_rip + 6)),
592 fubyte((void *)(frame->tf_rip + 7)));
594 KASSERT((read_rflags() & PSL_I) != 0, ("interrupts disabled"));
595 trapsignal(td, &ksi);
599 KASSERT(PCB_USER_FPU(td->td_pcb),
600 ("Return from trap with kernel FPU ctx leaked"));
607 * Ensure that we ignore any DTrace-induced faults. This function cannot
608 * be instrumented, so it cannot generate such faults itself.
611 trap_check(struct trapframe *frame)
615 if (dtrace_trap_func != NULL &&
616 (*dtrace_trap_func)(frame, frame->tf_trapno) != 0)
623 trap_pfault(frame, usermode)
624 struct trapframe *frame;
632 struct thread *td = curthread;
633 struct proc *p = td->td_proc;
634 vm_offset_t eva = frame->tf_addr;
636 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
638 * Due to both processor errata and lazy TLB invalidation when
639 * access restrictions are removed from virtual pages, memory
640 * accesses that are allowed by the physical mapping layer may
641 * nonetheless cause one spurious page fault per virtual page.
642 * When the thread is executing a "no faulting" section that
643 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
644 * every page fault is treated as a spurious page fault,
645 * unless it accesses the same virtual address as the most
646 * recent page fault within the same "no faulting" section.
648 if (td->td_md.md_spurflt_addr != eva ||
649 (td->td_pflags & TDP_RESETSPUR) != 0) {
651 * Do nothing to the TLB. A stale TLB entry is
652 * flushed automatically by a page fault.
654 td->td_md.md_spurflt_addr = eva;
655 td->td_pflags &= ~TDP_RESETSPUR;
660 * If we get a page fault while in a critical section, then
661 * it is most likely a fatal kernel page fault. The kernel
662 * is already going to panic trying to get a sleep lock to
663 * do the VM lookup, so just consider it a fatal trap so the
664 * kernel can print out a useful trap message and even get
667 * If we get a page fault while holding a non-sleepable
668 * lock, then it is most likely a fatal kernel page fault.
669 * If WITNESS is enabled, then it's going to whine about
670 * bogus LORs with various VM locks, so just skip to the
671 * fatal trap handling directly.
673 if (td->td_critnest != 0 ||
674 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
675 "Kernel page fault") != 0) {
676 trap_fatal(frame, eva);
680 va = trunc_page(eva);
681 if (va >= VM_MIN_KERNEL_ADDRESS) {
683 * Don't allow user-mode faults in kernel address space.
691 * This is a fault on non-kernel virtual memory. If either
692 * p or p->p_vmspace is NULL, then the fault is fatal.
694 if (p == NULL || (vm = p->p_vmspace) == NULL)
700 * When accessing a usermode address, kernel must be
701 * ready to accept the page fault, and provide a
702 * handling routine. Since accessing the address
703 * without the handler is a bug, do not try to handle
704 * it normally, and panic immediately.
706 if (!usermode && (td->td_intr_nesting_level != 0 ||
707 curpcb->pcb_onfault == NULL)) {
708 trap_fatal(frame, eva);
714 * If the trap was caused by errant bits in the PTE then panic.
716 if (frame->tf_err & PGEX_RSV) {
717 trap_fatal(frame, eva);
722 * PGEX_I is defined only if the execute disable bit capability is
723 * supported and enabled.
725 if (frame->tf_err & PGEX_W)
726 ftype = VM_PROT_WRITE;
727 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
728 ftype = VM_PROT_EXECUTE;
730 ftype = VM_PROT_READ;
732 if (map != kernel_map) {
734 * Keep swapout from messing with us during this
741 /* Fault in the user page: */
742 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
749 * Don't have to worry about process locking or stacks in the
752 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
754 if (rv == KERN_SUCCESS) {
756 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
757 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
758 if (ftype == VM_PROT_READ)
759 PMC_SOFT_CALL_TF( , , page_fault, read,
762 PMC_SOFT_CALL_TF( , , page_fault, write,
770 if (td->td_intr_nesting_level == 0 &&
771 curpcb->pcb_onfault != NULL) {
772 frame->tf_rip = (long)curpcb->pcb_onfault;
775 trap_fatal(frame, eva);
778 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
782 trap_fatal(frame, eva)
783 struct trapframe *frame;
789 struct soft_segment_descriptor softseg;
792 code = frame->tf_err;
793 type = frame->tf_trapno;
794 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
797 if (type <= MAX_TRAP_MSG)
798 msg = trap_msg[type];
801 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
802 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
804 /* two separate prints in case of a trap on an unmapped page */
805 printf("cpuid = %d; ", PCPU_GET(cpuid));
806 printf("apic id = %02x\n", PCPU_GET(apic_id));
808 if (type == T_PAGEFLT) {
809 printf("fault virtual address = 0x%lx\n", eva);
810 printf("fault code = %s %s %s%s, %s\n",
811 code & PGEX_U ? "user" : "supervisor",
812 code & PGEX_W ? "write" : "read",
813 code & PGEX_I ? "instruction" : "data",
814 code & PGEX_RSV ? " rsv" : "",
815 code & PGEX_P ? "protection violation" : "page not present");
817 printf("instruction pointer = 0x%lx:0x%lx\n",
818 frame->tf_cs & 0xffff, frame->tf_rip);
819 if (ISPL(frame->tf_cs) == SEL_UPL) {
820 ss = frame->tf_ss & 0xffff;
823 ss = GSEL(GDATA_SEL, SEL_KPL);
824 esp = (long)&frame->tf_rsp;
826 printf("stack pointer = 0x%x:0x%lx\n", ss, esp);
827 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
828 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
829 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
830 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
831 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
833 printf("processor eflags = ");
834 if (frame->tf_rflags & PSL_T)
835 printf("trace trap, ");
836 if (frame->tf_rflags & PSL_I)
837 printf("interrupt enabled, ");
838 if (frame->tf_rflags & PSL_NT)
839 printf("nested task, ");
840 if (frame->tf_rflags & PSL_RF)
842 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
843 printf("current process = ");
846 (u_long)curproc->p_pid, curthread->td_name ?
847 curthread->td_name : "");
853 if (debugger_on_panic || kdb_active)
854 if (kdb_trap(type, 0, frame))
857 printf("trap number = %d\n", type);
858 if (type <= MAX_TRAP_MSG)
859 panic("%s", trap_msg[type]);
861 panic("unknown/reserved trap");
865 * Double fault handler. Called when a fault occurs while writing
866 * a frame for a trap/exception onto the stack. This usually occurs
867 * when the stack overflows (such is the case with infinite recursion,
871 dblfault_handler(struct trapframe *frame)
874 if (dtrace_doubletrap_func != NULL)
875 (*dtrace_doubletrap_func)();
877 printf("\nFatal double fault\n");
878 printf("rip = 0x%lx\n", frame->tf_rip);
879 printf("rsp = 0x%lx\n", frame->tf_rsp);
880 printf("rbp = 0x%lx\n", frame->tf_rbp);
882 /* two separate prints in case of a trap on an unmapped page */
883 printf("cpuid = %d; ", PCPU_GET(cpuid));
884 printf("apic id = %02x\n", PCPU_GET(apic_id));
886 panic("double fault");
890 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
893 struct trapframe *frame;
896 int reg, regcnt, error;
899 frame = td->td_frame;
903 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
904 sa->code = frame->tf_rax;
906 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
907 sa->code = frame->tf_rdi;
911 if (p->p_sysent->sv_mask)
912 sa->code &= p->p_sysent->sv_mask;
914 if (sa->code >= p->p_sysent->sv_size)
915 sa->callp = &p->p_sysent->sv_table[0];
917 sa->callp = &p->p_sysent->sv_table[sa->code];
919 sa->narg = sa->callp->sy_narg;
920 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]),
921 ("Too many syscall arguments!"));
923 argp = &frame->tf_rdi;
925 bcopy(argp, sa->args, sizeof(sa->args[0]) * regcnt);
926 if (sa->narg > regcnt) {
927 KASSERT(params != NULL, ("copyin args with no params!"));
928 error = copyin(params, &sa->args[regcnt],
929 (sa->narg - regcnt) * sizeof(sa->args[0]));
933 td->td_retval[0] = 0;
934 td->td_retval[1] = frame->tf_rdx;
940 #include "../../kern/subr_syscall.c"
943 * System call handler for native binaries. The trap frame is already
944 * set up by the assembler trampoline and a pointer to it is saved in
948 amd64_syscall(struct thread *td, int traced)
950 struct syscall_args sa;
955 if (ISPL(td->td_frame->tf_cs) != SEL_UPL) {
960 error = syscallenter(td, &sa);
965 if (__predict_false(traced)) {
966 td->td_frame->tf_rflags &= ~PSL_T;
967 ksiginfo_init_trap(&ksi);
968 ksi.ksi_signo = SIGTRAP;
969 ksi.ksi_code = TRAP_TRACE;
970 ksi.ksi_addr = (void *)td->td_frame->tf_rip;
971 trapsignal(td, &ksi);
974 KASSERT(PCB_USER_FPU(td->td_pcb),
975 ("System call %s returing with kernel FPU ctx leaked",
976 syscallname(td->td_proc, sa.code)));
977 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
978 ("System call %s returning with mangled pcb_save",
979 syscallname(td->td_proc, sa.code)));
981 syscallret(td, error, &sa);
984 * If the user-supplied value of %rip is not a canonical
985 * address, then some CPUs will trigger a ring 0 #GP during
986 * the sysret instruction. However, the fault handler would
987 * execute in ring 0 with the user's %gs and %rsp which would
988 * not be safe. Instead, use the full return path which
989 * catches the problem safely.
991 if (td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS)
992 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);