2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (C) 1994, David Greenman
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the University of Utah, and William Jolitz.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * from: @(#)trap.c 7.4 (Berkeley) 5/13/91
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * AMD64 Trap and System call handling
49 #include "opt_clock.h"
51 #include "opt_hwpmc_hooks.h"
54 #include "opt_stack.h"
56 #include <sys/param.h>
58 #include <sys/systm.h>
60 #include <sys/pioctl.h>
61 #include <sys/ptrace.h>
63 #include <sys/kernel.h>
66 #include <sys/mutex.h>
67 #include <sys/resourcevar.h>
68 #include <sys/signalvar.h>
69 #include <sys/syscall.h>
70 #include <sys/sysctl.h>
71 #include <sys/sysent.h>
73 #include <sys/vmmeter.h>
75 #include <sys/pmckern.h>
76 PMC_SOFT_DEFINE( , , page_fault, all);
77 PMC_SOFT_DEFINE( , , page_fault, read);
78 PMC_SOFT_DEFINE( , , page_fault, write);
82 #include <vm/vm_param.h>
84 #include <vm/vm_kern.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_extern.h>
89 #include <machine/cpu.h>
90 #include <machine/intr_machdep.h>
92 #include <machine/md_var.h>
93 #include <machine/pcb.h>
95 #include <machine/smp.h>
97 #include <machine/stack.h>
98 #include <machine/tss.h>
101 #include <sys/dtrace_bsd.h>
104 void __noinline trap(struct trapframe *frame);
105 void trap_check(struct trapframe *frame);
106 void dblfault_handler(struct trapframe *frame);
108 static int trap_pfault(struct trapframe *, int);
109 static void trap_fatal(struct trapframe *, vm_offset_t);
111 #define MAX_TRAP_MSG 32
112 static char *trap_msg[] = {
114 "privileged instruction fault", /* 1 T_PRIVINFLT */
116 "breakpoint instruction fault", /* 3 T_BPTFLT */
119 "arithmetic trap", /* 6 T_ARITHTRAP */
122 "general protection fault", /* 9 T_PROTFLT */
123 "trace trap", /* 10 T_TRCTRAP */
125 "page fault", /* 12 T_PAGEFLT */
127 "alignment fault", /* 14 T_ALIGNFLT */
131 "integer divide fault", /* 18 T_DIVIDE */
132 "non-maskable interrupt trap", /* 19 T_NMI */
133 "overflow trap", /* 20 T_OFLOW */
134 "FPU bounds check fault", /* 21 T_BOUND */
135 "FPU device not available", /* 22 T_DNA */
136 "double fault", /* 23 T_DOUBLEFLT */
137 "FPU operand fetch fault", /* 24 T_FPOPFLT */
138 "invalid TSS fault", /* 25 T_TSSFLT */
139 "segment not present fault", /* 26 T_SEGNPFLT */
140 "stack fault", /* 27 T_STKFLT */
141 "machine check trap", /* 28 T_MCHK */
142 "SIMD floating-point exception", /* 29 T_XMMFLT */
143 "reserved (unknown) fault", /* 30 T_RESERVED */
144 "", /* 31 unused (reserved) */
145 "DTrace pid return trap", /* 32 T_DTRACE_RET */
148 static int prot_fault_translation;
149 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN,
150 &prot_fault_translation, 0,
151 "Select signal to deliver on protection fault");
152 static int uprintf_signal;
153 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RWTUN,
155 "Print debugging information on trap signal to ctty");
158 * Exception, fault, and trap interface to the FreeBSD kernel.
159 * This common code is called from assembly language IDT gate entry
160 * routines that prepare a suitable stack frame, and restore this
161 * frame after the exception has been processed.
165 trap(struct trapframe *frame)
184 type = frame->tf_trapno;
187 /* Handler for NMI IPIs used for stopping CPUs. */
188 if (type == T_NMI && ipi_nmi_handler() == 0)
199 if (type == T_RESERVED) {
200 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 non-zero return value from the handler means that
210 * the NMI was consumed by it and we can return immediately.
212 if (pmc_intr != NULL &&
213 (*pmc_intr)(PCPU_GET(cpuid), frame) != 0)
218 if (stack_nmi_handler(frame) != 0)
223 if (type == T_MCHK) {
228 if ((frame->tf_rflags & PSL_I) == 0) {
230 * Buggy application or kernel code has disabled
231 * interrupts and then trapped. Enabling interrupts
232 * now is wrong, but it is better than running with
233 * interrupts disabled until they are accidentally
236 if (TRAPF_USERMODE(frame))
238 "pid %ld (%s): trap %d with interrupts disabled\n",
239 (long)curproc->p_pid, curthread->td_name, type);
240 else if (type != T_NMI && type != T_BPTFLT &&
243 * XXX not quite right, since this may be for a
244 * multiple fault in user mode.
246 printf("kernel trap %d with interrupts disabled\n",
250 * We shouldn't enable interrupts while holding a
253 if (td->td_md.md_spinlock_count == 0)
258 if (TRAPF_USERMODE(frame)) {
262 td->td_frame = frame;
263 addr = frame->tf_rip;
264 if (td->td_cowgen != p->p_cowgen)
265 thread_cow_update(td);
268 case T_PRIVINFLT: /* privileged instruction fault */
273 case T_BPTFLT: /* bpt instruction fault */
274 case T_TRCTRAP: /* trace trap */
277 if (type == T_BPTFLT) {
278 if (dtrace_pid_probe_ptr != NULL &&
279 dtrace_pid_probe_ptr(frame) == 0)
283 frame->tf_rflags &= ~PSL_T;
285 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
288 case T_ARITHTRAP: /* arithmetic trap */
289 ucode = fputrap_x87();
295 case T_PROTFLT: /* general protection fault */
299 case T_STKFLT: /* stack fault */
300 case T_SEGNPFLT: /* segment not present fault */
304 case T_TSSFLT: /* invalid TSS fault */
312 case T_DOUBLEFLT: /* double fault */
318 case T_PAGEFLT: /* page fault */
320 * Emulator can take care about this trap?
322 if (*p->p_sysent->sv_trap != NULL &&
323 (*p->p_sysent->sv_trap)(td) == 0)
326 addr = frame->tf_addr;
327 signo = trap_pfault(frame, TRUE);
332 if (signo == SIGSEGV) {
334 } else if (prot_fault_translation == 0) {
336 * Autodetect. This check also covers
337 * the images without the ABI-tag ELF
340 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD &&
341 p->p_osrel >= P_OSREL_SIGSEGV) {
346 ucode = BUS_PAGE_FAULT;
348 } else if (prot_fault_translation == 1) {
350 * Always compat mode.
353 ucode = BUS_PAGE_FAULT;
356 * Always SIGSEGV mode.
363 case T_DIVIDE: /* integer divide fault */
370 nmi_handle_intr(type, frame);
374 case T_OFLOW: /* integer overflow fault */
379 case T_BOUND: /* bounds check fault */
385 /* transparent fault (due to context switch "late") */
386 KASSERT(PCB_USER_FPU(td->td_pcb),
387 ("kernel FPU ctx has leaked"));
391 case T_FPOPFLT: /* FPU operand fetch fault */
396 case T_XMMFLT: /* SIMD floating-point exception */
397 ucode = fputrap_sse();
405 if (dtrace_return_probe_ptr != NULL)
406 dtrace_return_probe_ptr(frame);
413 KASSERT(cold || td->td_ucred != NULL,
414 ("kernel trap doesn't have ucred"));
416 case T_PAGEFLT: /* page fault */
417 (void) trap_pfault(frame, FALSE);
421 if (PCB_USER_FPU(td->td_pcb))
422 panic("Unregistered use of FPU in kernel");
426 case T_ARITHTRAP: /* arithmetic trap */
427 case T_XMMFLT: /* SIMD floating-point exception */
428 case T_FPOPFLT: /* FPU operand fetch fault */
430 * For now, supporting kernel handler
431 * registration for FPU traps is overkill.
433 trap_fatal(frame, 0);
436 case T_STKFLT: /* stack fault */
437 case T_PROTFLT: /* general protection fault */
438 case T_SEGNPFLT: /* segment not present fault */
439 if (td->td_intr_nesting_level != 0)
443 * Invalid segment selectors and out of bounds
444 * %rip's and %rsp's can be set up in user mode.
445 * This causes a fault in kernel mode when the
446 * kernel tries to return to user mode. We want
447 * to get this fault so that we can fix the
448 * problem here and not have to check all the
449 * selectors and pointers when the user changes
452 if (frame->tf_rip == (long)doreti_iret) {
453 frame->tf_rip = (long)doreti_iret_fault;
456 if (frame->tf_rip == (long)ld_ds) {
457 frame->tf_rip = (long)ds_load_fault;
460 if (frame->tf_rip == (long)ld_es) {
461 frame->tf_rip = (long)es_load_fault;
464 if (frame->tf_rip == (long)ld_fs) {
465 frame->tf_rip = (long)fs_load_fault;
468 if (frame->tf_rip == (long)ld_gs) {
469 frame->tf_rip = (long)gs_load_fault;
472 if (frame->tf_rip == (long)ld_gsbase) {
473 frame->tf_rip = (long)gsbase_load_fault;
476 if (frame->tf_rip == (long)ld_fsbase) {
477 frame->tf_rip = (long)fsbase_load_fault;
480 if (curpcb->pcb_onfault != NULL) {
481 frame->tf_rip = (long)curpcb->pcb_onfault;
488 * PSL_NT can be set in user mode and isn't cleared
489 * automatically when the kernel is entered. This
490 * causes a TSS fault when the kernel attempts to
491 * `iret' because the TSS link is uninitialized. We
492 * want to get this fault so that we can fix the
493 * problem here and not every time the kernel is
496 if (frame->tf_rflags & PSL_NT) {
497 frame->tf_rflags &= ~PSL_NT;
502 case T_TRCTRAP: /* trace trap */
504 * Ignore debug register trace traps due to
505 * accesses in the user's address space, which
506 * can happen under several conditions such as
507 * if a user sets a watchpoint on a buffer and
508 * then passes that buffer to a system call.
509 * We still want to get TRCTRAPS for addresses
510 * in kernel space because that is useful when
511 * debugging the kernel.
513 if (user_dbreg_trap()) {
515 * Reset breakpoint bits because the
518 load_dr6(rdr6() & ~0xf);
522 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
526 * If KDB is enabled, let it handle the debugger trap.
527 * Otherwise, debugger traps "can't happen".
530 /* XXX %dr6 is not quite reentrant. */
532 load_dr6(dr6 & ~0x4000);
533 if (kdb_trap(type, dr6, frame))
540 nmi_handle_intr(type, frame);
545 trap_fatal(frame, 0);
549 /* Translate fault for emulators (e.g. Linux) */
550 if (*p->p_sysent->sv_transtrap != NULL)
551 signo = (*p->p_sysent->sv_transtrap)(signo, type);
553 ksiginfo_init_trap(&ksi);
554 ksi.ksi_signo = signo;
555 ksi.ksi_code = ucode;
556 ksi.ksi_trapno = type;
557 ksi.ksi_addr = (void *)addr;
558 if (uprintf_signal) {
559 uprintf("pid %d comm %s: signal %d err %lx code %d type %d "
560 "addr 0x%lx rsp 0x%lx rip 0x%lx "
561 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
562 p->p_pid, p->p_comm, signo, frame->tf_err, ucode, type,
563 addr, frame->tf_rsp, frame->tf_rip,
564 fubyte((void *)(frame->tf_rip + 0)),
565 fubyte((void *)(frame->tf_rip + 1)),
566 fubyte((void *)(frame->tf_rip + 2)),
567 fubyte((void *)(frame->tf_rip + 3)),
568 fubyte((void *)(frame->tf_rip + 4)),
569 fubyte((void *)(frame->tf_rip + 5)),
570 fubyte((void *)(frame->tf_rip + 6)),
571 fubyte((void *)(frame->tf_rip + 7)));
573 KASSERT((read_rflags() & PSL_I) != 0, ("interrupts disabled"));
574 trapsignal(td, &ksi);
577 KASSERT(PCB_USER_FPU(td->td_pcb),
578 ("Return from trap with kernel FPU ctx leaked"));
582 * Ensure that we ignore any DTrace-induced faults. This function cannot
583 * be instrumented, so it cannot generate such faults itself.
586 trap_check(struct trapframe *frame)
590 if (dtrace_trap_func != NULL &&
591 (*dtrace_trap_func)(frame, frame->tf_trapno) != 0)
598 trap_pfault(struct trapframe *frame, int usermode)
610 eva = frame->tf_addr;
612 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
614 * Due to both processor errata and lazy TLB invalidation when
615 * access restrictions are removed from virtual pages, memory
616 * accesses that are allowed by the physical mapping layer may
617 * nonetheless cause one spurious page fault per virtual page.
618 * When the thread is executing a "no faulting" section that
619 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
620 * every page fault is treated as a spurious page fault,
621 * unless it accesses the same virtual address as the most
622 * recent page fault within the same "no faulting" section.
624 if (td->td_md.md_spurflt_addr != eva ||
625 (td->td_pflags & TDP_RESETSPUR) != 0) {
627 * Do nothing to the TLB. A stale TLB entry is
628 * flushed automatically by a page fault.
630 td->td_md.md_spurflt_addr = eva;
631 td->td_pflags &= ~TDP_RESETSPUR;
636 * If we get a page fault while in a critical section, then
637 * it is most likely a fatal kernel page fault. The kernel
638 * is already going to panic trying to get a sleep lock to
639 * do the VM lookup, so just consider it a fatal trap so the
640 * kernel can print out a useful trap message and even get
643 * If we get a page fault while holding a non-sleepable
644 * lock, then it is most likely a fatal kernel page fault.
645 * If WITNESS is enabled, then it's going to whine about
646 * bogus LORs with various VM locks, so just skip to the
647 * fatal trap handling directly.
649 if (td->td_critnest != 0 ||
650 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
651 "Kernel page fault") != 0) {
652 trap_fatal(frame, eva);
656 va = trunc_page(eva);
657 if (va >= VM_MIN_KERNEL_ADDRESS) {
659 * Don't allow user-mode faults in kernel address space.
666 map = &p->p_vmspace->vm_map;
669 * When accessing a usermode address, kernel must be
670 * ready to accept the page fault, and provide a
671 * handling routine. Since accessing the address
672 * without the handler is a bug, do not try to handle
673 * it normally, and panic immediately.
675 if (!usermode && (td->td_intr_nesting_level != 0 ||
676 curpcb->pcb_onfault == NULL)) {
677 trap_fatal(frame, eva);
683 * If the trap was caused by errant bits in the PTE then panic.
685 if (frame->tf_err & PGEX_RSV) {
686 trap_fatal(frame, eva);
691 * PGEX_I is defined only if the execute disable bit capability is
692 * supported and enabled.
694 if (frame->tf_err & PGEX_W)
695 ftype = VM_PROT_WRITE;
696 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
697 ftype = VM_PROT_EXECUTE;
699 ftype = VM_PROT_READ;
701 /* Fault in the page. */
702 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
703 if (rv == KERN_SUCCESS) {
705 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
706 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
707 if (ftype == VM_PROT_READ)
708 PMC_SOFT_CALL_TF( , , page_fault, read,
711 PMC_SOFT_CALL_TF( , , page_fault, write,
718 if (td->td_intr_nesting_level == 0 &&
719 curpcb->pcb_onfault != NULL) {
720 frame->tf_rip = (long)curpcb->pcb_onfault;
723 trap_fatal(frame, eva);
726 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
730 trap_fatal(frame, eva)
731 struct trapframe *frame;
736 struct soft_segment_descriptor softseg;
739 code = frame->tf_err;
740 type = frame->tf_trapno;
741 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
744 if (type <= MAX_TRAP_MSG)
745 msg = trap_msg[type];
748 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
749 TRAPF_USERMODE(frame) ? "user" : "kernel");
751 /* two separate prints in case of a trap on an unmapped page */
752 printf("cpuid = %d; ", PCPU_GET(cpuid));
753 printf("apic id = %02x\n", PCPU_GET(apic_id));
755 if (type == T_PAGEFLT) {
756 printf("fault virtual address = 0x%lx\n", eva);
757 printf("fault code = %s %s %s, %s\n",
758 code & PGEX_U ? "user" : "supervisor",
759 code & PGEX_W ? "write" : "read",
760 code & PGEX_I ? "instruction" : "data",
761 code & PGEX_RSV ? "reserved bits in PTE" :
762 code & PGEX_P ? "protection violation" : "page not present");
764 printf("instruction pointer = 0x%lx:0x%lx\n",
765 frame->tf_cs & 0xffff, frame->tf_rip);
766 ss = frame->tf_ss & 0xffff;
767 printf("stack pointer = 0x%x:0x%lx\n", ss, frame->tf_rsp);
768 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
769 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
770 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
771 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
772 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
774 printf("processor eflags = ");
775 if (frame->tf_rflags & PSL_T)
776 printf("trace trap, ");
777 if (frame->tf_rflags & PSL_I)
778 printf("interrupt enabled, ");
779 if (frame->tf_rflags & PSL_NT)
780 printf("nested task, ");
781 if (frame->tf_rflags & PSL_RF)
783 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
784 printf("current process = %d (%s)\n",
785 curproc->p_pid, curthread->td_name);
788 if (debugger_on_panic || kdb_active)
789 if (kdb_trap(type, 0, frame))
792 printf("trap number = %d\n", type);
793 if (type <= MAX_TRAP_MSG)
794 panic("%s", trap_msg[type]);
796 panic("unknown/reserved trap");
800 * Double fault handler. Called when a fault occurs while writing
801 * a frame for a trap/exception onto the stack. This usually occurs
802 * when the stack overflows (such is the case with infinite recursion,
806 dblfault_handler(struct trapframe *frame)
809 if (dtrace_doubletrap_func != NULL)
810 (*dtrace_doubletrap_func)();
812 printf("\nFatal double fault\n"
813 "rip %#lx rsp %#lx rbp %#lx\n"
814 "rax %#lx rdx %#lx rbx %#lx\n"
815 "rcx %#lx rsi %#lx rdi %#lx\n"
816 "r8 %#lx r9 %#lx r10 %#lx\n"
817 "r11 %#lx r12 %#lx r13 %#lx\n"
818 "r14 %#lx r15 %#lx rflags %#lx\n"
819 "cs %#lx ss %#lx ds %#hx es %#hx fs %#hx gs %#hx\n"
820 "fsbase %#lx gsbase %#lx kgsbase %#lx\n",
821 frame->tf_rip, frame->tf_rsp, frame->tf_rbp,
822 frame->tf_rax, frame->tf_rdx, frame->tf_rbx,
823 frame->tf_rcx, frame->tf_rdi, frame->tf_rsi,
824 frame->tf_r8, frame->tf_r9, frame->tf_r10,
825 frame->tf_r11, frame->tf_r12, frame->tf_r13,
826 frame->tf_r14, frame->tf_r15, frame->tf_rflags,
827 frame->tf_cs, frame->tf_ss, frame->tf_ds, frame->tf_es,
828 frame->tf_fs, frame->tf_gs,
829 rdmsr(MSR_FSBASE), rdmsr(MSR_GSBASE), rdmsr(MSR_KGSBASE));
831 /* two separate prints in case of a trap on an unmapped page */
832 printf("cpuid = %d; ", PCPU_GET(cpuid));
833 printf("apic id = %02x\n", PCPU_GET(apic_id));
835 panic("double fault");
839 cpu_fetch_syscall_args(struct thread *td)
842 struct trapframe *frame;
844 struct syscall_args *sa;
846 int reg, regcnt, error;
849 frame = td->td_frame;
854 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
855 sa->code = frame->tf_rax;
857 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
858 sa->code = frame->tf_rdi;
862 if (p->p_sysent->sv_mask)
863 sa->code &= p->p_sysent->sv_mask;
865 if (sa->code >= p->p_sysent->sv_size)
866 sa->callp = &p->p_sysent->sv_table[0];
868 sa->callp = &p->p_sysent->sv_table[sa->code];
870 sa->narg = sa->callp->sy_narg;
871 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]),
872 ("Too many syscall arguments!"));
874 argp = &frame->tf_rdi;
876 bcopy(argp, sa->args, sizeof(sa->args[0]) * regcnt);
877 if (sa->narg > regcnt) {
878 KASSERT(params != NULL, ("copyin args with no params!"));
879 error = copyin(params, &sa->args[regcnt],
880 (sa->narg - regcnt) * sizeof(sa->args[0]));
884 td->td_retval[0] = 0;
885 td->td_retval[1] = frame->tf_rdx;
891 #include "../../kern/subr_syscall.c"
894 * System call handler for native binaries. The trap frame is already
895 * set up by the assembler trampoline and a pointer to it is saved in
899 amd64_syscall(struct thread *td, int traced)
905 if (!TRAPF_USERMODE(td->td_frame)) {
910 error = syscallenter(td);
915 if (__predict_false(traced)) {
916 td->td_frame->tf_rflags &= ~PSL_T;
917 ksiginfo_init_trap(&ksi);
918 ksi.ksi_signo = SIGTRAP;
919 ksi.ksi_code = TRAP_TRACE;
920 ksi.ksi_addr = (void *)td->td_frame->tf_rip;
921 trapsignal(td, &ksi);
924 KASSERT(PCB_USER_FPU(td->td_pcb),
925 ("System call %s returning with kernel FPU ctx leaked",
926 syscallname(td->td_proc, td->td_sa.code)));
927 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
928 ("System call %s returning with mangled pcb_save",
929 syscallname(td->td_proc, td->td_sa.code)));
930 KASSERT(td->td_md.md_invl_gen.gen == 0,
931 ("System call %s returning with leaked invl_gen %lu",
932 syscallname(td->td_proc, td->td_sa.code),
933 td->td_md.md_invl_gen.gen));
935 syscallret(td, error);
938 * If the user-supplied value of %rip is not a canonical
939 * address, then some CPUs will trigger a ring 0 #GP during
940 * the sysret instruction. However, the fault handler would
941 * execute in ring 0 with the user's %gs and %rsp which would
942 * not be safe. Instead, use the full return path which
943 * catches the problem safely.
945 if (__predict_false(td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS))
946 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);