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"
53 #include "opt_ktrace.h"
55 #include <sys/param.h>
57 #include <sys/systm.h>
59 #include <sys/pioctl.h>
60 #include <sys/ptrace.h>
62 #include <sys/kernel.h>
65 #include <sys/mutex.h>
66 #include <sys/resourcevar.h>
67 #include <sys/signalvar.h>
68 #include <sys/syscall.h>
69 #include <sys/sysctl.h>
70 #include <sys/sysent.h>
72 #include <sys/vmmeter.h>
74 #include <sys/ktrace.h>
77 #include <sys/pmckern.h>
79 #include <security/audit/audit.h>
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>
91 #include <machine/mca.h>
92 #include <machine/md_var.h>
93 #include <machine/pcb.h>
95 #include <machine/smp.h>
97 #include <machine/tss.h>
100 #include <sys/dtrace_bsd.h>
103 * This is a hook which is initialised by the dtrace module
104 * to handle traps which might occur during DTrace probe
107 dtrace_trap_func_t dtrace_trap_func;
109 dtrace_doubletrap_func_t dtrace_doubletrap_func;
112 * This is a hook which is initialised by the systrace module
113 * when it is loaded. This keeps the DTrace syscall provider
114 * implementation opaque.
116 systrace_probe_func_t systrace_probe_func;
119 extern void trap(struct trapframe *frame);
120 extern void syscall(struct trapframe *frame);
121 void dblfault_handler(struct trapframe *frame);
123 static int trap_pfault(struct trapframe *, int);
124 static void trap_fatal(struct trapframe *, vm_offset_t);
126 #define MAX_TRAP_MSG 30
127 static char *trap_msg[] = {
129 "privileged instruction fault", /* 1 T_PRIVINFLT */
131 "breakpoint instruction fault", /* 3 T_BPTFLT */
134 "arithmetic trap", /* 6 T_ARITHTRAP */
137 "general protection fault", /* 9 T_PROTFLT */
138 "trace trap", /* 10 T_TRCTRAP */
140 "page fault", /* 12 T_PAGEFLT */
142 "alignment fault", /* 14 T_ALIGNFLT */
146 "integer divide fault", /* 18 T_DIVIDE */
147 "non-maskable interrupt trap", /* 19 T_NMI */
148 "overflow trap", /* 20 T_OFLOW */
149 "FPU bounds check fault", /* 21 T_BOUND */
150 "FPU device not available", /* 22 T_DNA */
151 "double fault", /* 23 T_DOUBLEFLT */
152 "FPU operand fetch fault", /* 24 T_FPOPFLT */
153 "invalid TSS fault", /* 25 T_TSSFLT */
154 "segment not present fault", /* 26 T_SEGNPFLT */
155 "stack fault", /* 27 T_STKFLT */
156 "machine check trap", /* 28 T_MCHK */
157 "SIMD floating-point exception", /* 29 T_XMMFLT */
158 "reserved (unknown) fault", /* 30 T_RESERVED */
162 static int kdb_on_nmi = 1;
163 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
164 &kdb_on_nmi, 0, "Go to KDB on NMI");
166 static int panic_on_nmi = 1;
167 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
168 &panic_on_nmi, 0, "Panic on NMI");
169 static int prot_fault_translation = 0;
170 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
171 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
173 extern char *syscallnames[];
175 /* #define DEBUG 1 */
178 report_seg_fault(const char *segn, struct trapframe *frame)
180 struct proc_ldt *pldt;
181 struct trapframe *pf;
183 pldt = curproc->p_md.md_ldt;
184 printf("%d: %s load fault %lx %p %d\n",
185 curproc->p_pid, segn, frame->tf_err,
186 pldt != NULL ? pldt->ldt_base : NULL,
187 pldt != NULL ? pldt->ldt_refcnt : 0);
189 pf = (struct trapframe *)frame->tf_rsp;
190 printf("rdi %lx\n", pf->tf_rdi);
191 printf("rsi %lx\n", pf->tf_rsi);
192 printf("rdx %lx\n", pf->tf_rdx);
193 printf("rcx %lx\n", pf->tf_rcx);
194 printf("r8 %lx\n", pf->tf_r8);
195 printf("r9 %lx\n", pf->tf_r9);
196 printf("rax %lx\n", pf->tf_rax);
197 printf("rbx %lx\n", pf->tf_rbx);
198 printf("rbp %lx\n", pf->tf_rbp);
199 printf("r10 %lx\n", pf->tf_r10);
200 printf("r11 %lx\n", pf->tf_r11);
201 printf("r12 %lx\n", pf->tf_r12);
202 printf("r13 %lx\n", pf->tf_r13);
203 printf("r14 %lx\n", pf->tf_r14);
204 printf("r15 %lx\n", pf->tf_r15);
205 printf("fs %x\n", pf->tf_fs);
206 printf("gs %x\n", pf->tf_gs);
207 printf("es %x\n", pf->tf_es);
208 printf("ds %x\n", pf->tf_ds);
209 printf("tno %x\n", pf->tf_trapno);
210 printf("adr %lx\n", pf->tf_addr);
211 printf("flg %x\n", pf->tf_flags);
212 printf("err %lx\n", pf->tf_err);
213 printf("rip %lx\n", pf->tf_rip);
214 printf("cs %lx\n", pf->tf_cs);
215 printf("rfl %lx\n", pf->tf_rflags);
216 printf("rsp %lx\n", pf->tf_rsp);
217 printf("ss %lx\n", pf->tf_ss);
222 * Exception, fault, and trap interface to the FreeBSD kernel.
223 * This common code is called from assembly language IDT gate entry
224 * routines that prepare a suitable stack frame, and restore this
225 * frame after the exception has been processed.
229 trap(struct trapframe *frame)
231 struct thread *td = curthread;
232 struct proc *p = td->td_proc;
233 int i = 0, ucode = 0, code;
238 PCPU_INC(cnt.v_trap);
239 type = frame->tf_trapno;
242 /* Handler for NMI IPIs used for stopping CPUs. */
244 if (ipi_nmi_handler() == 0)
258 * CPU PMCs interrupt using an NMI. If the PMC module is
259 * active, pass the 'rip' value to the PMC module's interrupt
260 * handler. A return value of '1' from the handler means that
261 * the NMI was handled by it and we can return immediately.
263 if (type == T_NMI && pmc_intr &&
264 (*pmc_intr)(PCPU_GET(cpuid), frame))
268 if (type == T_MCHK) {
270 trap_fatal(frame, 0);
276 * A trap can occur while DTrace executes a probe. Before
277 * executing the probe, DTrace blocks re-scheduling and sets
278 * a flag in it's per-cpu flags to indicate that it doesn't
279 * want to fault. On returning from the the probe, the no-fault
280 * flag is cleared and finally re-scheduling is enabled.
282 * If the DTrace kernel module has registered a trap handler,
283 * call it and if it returns non-zero, assume that it has
284 * handled the trap and modified the trap frame so that this
285 * function can return normally.
287 if (dtrace_trap_func != NULL)
288 if ((*dtrace_trap_func)(frame, type))
292 if ((frame->tf_rflags & PSL_I) == 0) {
294 * Buggy application or kernel code has disabled
295 * interrupts and then trapped. Enabling interrupts
296 * now is wrong, but it is better than running with
297 * interrupts disabled until they are accidentally
300 if (ISPL(frame->tf_cs) == SEL_UPL)
302 "pid %ld (%s): trap %d with interrupts disabled\n",
303 (long)curproc->p_pid, curthread->td_name, type);
304 else if (type != T_NMI && type != T_BPTFLT &&
307 * XXX not quite right, since this may be for a
308 * multiple fault in user mode.
310 printf("kernel trap %d with interrupts disabled\n",
313 report_seg_fault("hlt", frame);
316 * We shouldn't enable interrupts while holding a
317 * spin lock or servicing an NMI.
319 if (type != T_NMI && td->td_md.md_spinlock_count == 0)
324 code = frame->tf_err;
325 if (type == T_PAGEFLT) {
327 * If we get a page fault while in a critical section, then
328 * it is most likely a fatal kernel page fault. The kernel
329 * is already going to panic trying to get a sleep lock to
330 * do the VM lookup, so just consider it a fatal trap so the
331 * kernel can print out a useful trap message and even get
334 * If we get a page fault while holding a non-sleepable
335 * lock, then it is most likely a fatal kernel page fault.
336 * If WITNESS is enabled, then it's going to whine about
337 * bogus LORs with various VM locks, so just skip to the
338 * fatal trap handling directly.
340 if (td->td_critnest != 0 ||
341 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
342 "Kernel page fault") != 0)
343 trap_fatal(frame, frame->tf_addr);
346 if (ISPL(frame->tf_cs) == SEL_UPL) {
350 td->td_frame = frame;
351 addr = frame->tf_rip;
352 if (td->td_ucred != p->p_ucred)
353 cred_update_thread(td);
356 case T_PRIVINFLT: /* privileged instruction fault */
361 case T_BPTFLT: /* bpt instruction fault */
362 case T_TRCTRAP: /* trace trap */
364 frame->tf_rflags &= ~PSL_T;
366 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
369 case T_ARITHTRAP: /* arithmetic trap */
376 case T_PROTFLT: /* general protection fault */
380 case T_STKFLT: /* stack fault */
381 case T_SEGNPFLT: /* segment not present fault */
385 case T_TSSFLT: /* invalid TSS fault */
389 case T_DOUBLEFLT: /* double fault */
395 case T_PAGEFLT: /* page fault */
396 addr = frame->tf_addr;
397 i = trap_pfault(frame, TRUE);
406 if (prot_fault_translation == 0) {
409 * This check also covers the images
410 * without the ABI-tag ELF note.
412 if (SV_CURPROC_ABI() ==
414 p->p_osrel >= 700004) {
419 ucode = BUS_PAGE_FAULT;
421 } else if (prot_fault_translation == 1) {
423 * Always compat mode.
426 ucode = BUS_PAGE_FAULT;
429 * Always SIGSEGV mode.
437 case T_DIVIDE: /* integer divide fault */
444 /* machine/parity/power fail/"kitchen sink" faults */
445 if (isa_nmi(code) == 0) {
448 * NMI can be hooked up to a pushbutton
452 printf ("NMI ... going to debugger\n");
453 kdb_trap(type, 0, frame);
457 } else if (panic_on_nmi)
458 panic("NMI indicates hardware failure");
462 case T_OFLOW: /* integer overflow fault */
467 case T_BOUND: /* bounds check fault */
473 /* transparent fault (due to context switch "late") */
477 case T_FPOPFLT: /* FPU operand fetch fault */
482 case T_XMMFLT: /* SIMD floating-point exception */
490 KASSERT(cold || td->td_ucred != NULL,
491 ("kernel trap doesn't have ucred"));
493 case T_PAGEFLT: /* page fault */
494 (void) trap_pfault(frame, FALSE);
499 * The kernel is apparently using fpu for copying.
500 * XXX this should be fatal unless the kernel has
501 * registered such use.
504 printf("fpudna in kernel mode!\n");
507 case T_STKFLT: /* stack fault */
510 case T_PROTFLT: /* general protection fault */
511 case T_SEGNPFLT: /* segment not present fault */
512 if (td->td_intr_nesting_level != 0)
516 * Invalid segment selectors and out of bounds
517 * %rip's and %rsp's can be set up in user mode.
518 * This causes a fault in kernel mode when the
519 * kernel tries to return to user mode. We want
520 * to get this fault so that we can fix the
521 * problem here and not have to check all the
522 * selectors and pointers when the user changes
525 if (frame->tf_rip == (long)doreti_iret) {
526 frame->tf_rip = (long)doreti_iret_fault;
529 if (frame->tf_rip == (long)ld_ds) {
531 report_seg_fault("ds", frame);
533 frame->tf_rip = (long)ds_load_fault;
534 frame->tf_ds = _udatasel;
537 if (frame->tf_rip == (long)ld_es) {
539 report_seg_fault("es", frame);
541 frame->tf_rip = (long)es_load_fault;
542 frame->tf_es = _udatasel;
545 if (frame->tf_rip == (long)ld_fs) {
547 report_seg_fault("fs", frame);
549 frame->tf_rip = (long)fs_load_fault;
550 frame->tf_fs = _ufssel;
553 if (frame->tf_rip == (long)ld_gs) {
555 report_seg_fault("gs", frame);
557 frame->tf_rip = (long)gs_load_fault;
558 frame->tf_gs = _ugssel;
561 if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
563 (long)PCPU_GET(curpcb)->pcb_onfault;
570 * PSL_NT can be set in user mode and isn't cleared
571 * automatically when the kernel is entered. This
572 * causes a TSS fault when the kernel attempts to
573 * `iret' because the TSS link is uninitialized. We
574 * want to get this fault so that we can fix the
575 * problem here and not every time the kernel is
578 if (frame->tf_rflags & PSL_NT) {
579 frame->tf_rflags &= ~PSL_NT;
584 case T_TRCTRAP: /* trace trap */
586 * Ignore debug register trace traps due to
587 * accesses in the user's address space, which
588 * can happen under several conditions such as
589 * if a user sets a watchpoint on a buffer and
590 * then passes that buffer to a system call.
591 * We still want to get TRCTRAPS for addresses
592 * in kernel space because that is useful when
593 * debugging the kernel.
595 if (user_dbreg_trap()) {
597 * Reset breakpoint bits because the
600 /* XXX check upper bits here */
601 load_dr6(rdr6() & 0xfffffff0);
605 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
609 * If KDB is enabled, let it handle the debugger trap.
610 * Otherwise, debugger traps "can't happen".
613 if (kdb_trap(type, 0, frame))
620 /* machine/parity/power fail/"kitchen sink" faults */
621 if (isa_nmi(code) == 0) {
624 * NMI can be hooked up to a pushbutton
628 printf ("NMI ... going to debugger\n");
629 kdb_trap(type, 0, frame);
633 } else if (panic_on_nmi == 0)
639 trap_fatal(frame, 0);
643 /* Translate fault for emulators (e.g. Linux) */
644 if (*p->p_sysent->sv_transtrap)
645 i = (*p->p_sysent->sv_transtrap)(i, type);
647 ksiginfo_init_trap(&ksi);
649 ksi.ksi_code = ucode;
650 ksi.ksi_trapno = type;
651 ksi.ksi_addr = (void *)addr;
652 trapsignal(td, &ksi);
656 register_t rg,rgk, rf;
658 if (type <= MAX_TRAP_MSG) {
659 uprintf("fatal process exception: %s",
661 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
662 uprintf(", fault VA = 0x%lx", frame->tf_addr);
665 rf = rdmsr(0xc0000100);
666 rg = rdmsr(0xc0000101);
667 rgk = rdmsr(0xc0000102);
668 uprintf("pid %d TRAP %d rip %lx err %lx addr %lx cs %lx ss %lx ds %x "
669 "es %x fs %x fsbase %lx %lx gs %x gsbase %lx %lx %lx\n",
670 curproc->p_pid, type, frame->tf_rip, frame->tf_err,
672 frame->tf_cs, frame->tf_ss, frame->tf_ds, frame->tf_es,
673 frame->tf_fs, td->td_pcb->pcb_fsbase, rf,
674 frame->tf_gs, td->td_pcb->pcb_gsbase, rg, rgk);
680 mtx_assert(&Giant, MA_NOTOWNED);
687 trap_pfault(frame, usermode)
688 struct trapframe *frame;
692 struct vmspace *vm = NULL;
696 struct thread *td = curthread;
697 struct proc *p = td->td_proc;
698 vm_offset_t eva = frame->tf_addr;
700 va = trunc_page(eva);
701 if (va >= VM_MIN_KERNEL_ADDRESS) {
703 * Don't allow user-mode faults in kernel address space.
711 * This is a fault on non-kernel virtual memory.
712 * vm is initialized above to NULL. If curproc is NULL
713 * or curproc->p_vmspace is NULL the fault is fatal.
725 * PGEX_I is defined only if the execute disable bit capability is
726 * supported and enabled.
728 if (frame->tf_err & PGEX_W)
729 ftype = VM_PROT_WRITE;
730 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
731 ftype = VM_PROT_EXECUTE;
733 ftype = VM_PROT_READ;
735 if (map != kernel_map) {
737 * Keep swapout from messing with us during this
744 /* Fault in the user page: */
745 rv = vm_fault(map, va, ftype,
746 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
754 * Don't have to worry about process locking or stacks in the
757 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
759 if (rv == KERN_SUCCESS)
763 if (td->td_intr_nesting_level == 0 &&
764 PCPU_GET(curpcb)->pcb_onfault != NULL) {
765 frame->tf_rip = (long)PCPU_GET(curpcb)->pcb_onfault;
768 trap_fatal(frame, eva);
772 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
776 trap_fatal(frame, eva)
777 struct trapframe *frame;
783 struct soft_segment_descriptor softseg;
786 code = frame->tf_err;
787 type = frame->tf_trapno;
788 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
791 if (type <= MAX_TRAP_MSG)
792 msg = trap_msg[type];
795 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
796 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
798 /* two separate prints in case of a trap on an unmapped page */
799 printf("cpuid = %d; ", PCPU_GET(cpuid));
800 printf("apic id = %02x\n", PCPU_GET(apic_id));
802 if (type == T_PAGEFLT) {
803 printf("fault virtual address = 0x%lx\n", eva);
804 printf("fault code = %s %s %s, %s\n",
805 code & PGEX_U ? "user" : "supervisor",
806 code & PGEX_W ? "write" : "read",
807 code & PGEX_I ? "instruction" : "data",
808 code & PGEX_P ? "protection violation" : "page not present");
810 printf("instruction pointer = 0x%lx:0x%lx\n",
811 frame->tf_cs & 0xffff, frame->tf_rip);
812 if (ISPL(frame->tf_cs) == SEL_UPL) {
813 ss = frame->tf_ss & 0xffff;
816 ss = GSEL(GDATA_SEL, SEL_KPL);
817 esp = (long)&frame->tf_rsp;
819 printf("stack pointer = 0x%x:0x%lx\n", ss, esp);
820 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
821 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
822 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
823 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
824 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
826 printf("processor eflags = ");
827 if (frame->tf_rflags & PSL_T)
828 printf("trace trap, ");
829 if (frame->tf_rflags & PSL_I)
830 printf("interrupt enabled, ");
831 if (frame->tf_rflags & PSL_NT)
832 printf("nested task, ");
833 if (frame->tf_rflags & PSL_RF)
835 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
836 printf("current process = ");
839 (u_long)curproc->p_pid, curthread->td_name ?
840 curthread->td_name : "");
846 if (debugger_on_panic || kdb_active)
847 if (kdb_trap(type, 0, frame))
850 printf("trap number = %d\n", type);
851 if (type <= MAX_TRAP_MSG)
852 panic("%s", trap_msg[type]);
854 panic("unknown/reserved trap");
858 * Double fault handler. Called when a fault occurs while writing
859 * a frame for a trap/exception onto the stack. This usually occurs
860 * when the stack overflows (such is the case with infinite recursion,
864 dblfault_handler(struct trapframe *frame)
867 if (dtrace_doubletrap_func != NULL)
868 (*dtrace_doubletrap_func)();
870 printf("\nFatal double fault\n");
871 printf("rip = 0x%lx\n", frame->tf_rip);
872 printf("rsp = 0x%lx\n", frame->tf_rsp);
873 printf("rbp = 0x%lx\n", frame->tf_rbp);
875 /* two separate prints in case of a trap on an unmapped page */
876 printf("cpuid = %d; ", PCPU_GET(cpuid));
877 printf("apic id = %02x\n", PCPU_GET(apic_id));
879 panic("double fault");
883 * syscall - system call request C handler
885 * A system call is essentially treated as a trap.
888 syscall(struct trapframe *frame)
891 struct sysent *callp;
892 struct thread *td = curthread;
893 struct proc *p = td->td_proc;
894 register_t orig_tf_rflags;
903 PCPU_INC(cnt.v_syscall);
906 if (ISPL(frame->tf_cs) != SEL_UPL) {
915 td->td_frame = frame;
916 if (td->td_ucred != p->p_ucred)
917 cred_update_thread(td);
918 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
919 code = frame->tf_rax;
920 orig_tf_rflags = frame->tf_rflags;
922 if (p->p_sysent->sv_prepsyscall) {
923 (*p->p_sysent->sv_prepsyscall)(frame, (int *)args, &code, ¶ms);
925 if (code == SYS_syscall || code == SYS___syscall) {
926 code = frame->tf_rdi;
932 if (p->p_sysent->sv_mask)
933 code &= p->p_sysent->sv_mask;
935 if (code >= p->p_sysent->sv_size)
936 callp = &p->p_sysent->sv_table[0];
938 callp = &p->p_sysent->sv_table[code];
940 narg = callp->sy_narg;
941 KASSERT(narg <= sizeof(args) / sizeof(args[0]),
942 ("Too many syscall arguments!"));
944 argp = &frame->tf_rdi;
946 bcopy(argp, args, sizeof(args[0]) * regcnt);
948 KASSERT(params != NULL, ("copyin args with no params!"));
949 error = copyin(params, &args[regcnt],
950 (narg - regcnt) * sizeof(args[0]));
955 if (KTRPOINT(td, KTR_SYSCALL))
956 ktrsyscall(code, narg, argp);
959 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
960 td->td_proc->p_pid, td->td_name, code);
965 td->td_retval[0] = 0;
966 td->td_retval[1] = frame->tf_rdx;
968 STOPEVENT(p, S_SCE, narg);
970 PTRACESTOP_SC(p, td, S_PT_SCE);
974 * If the systrace module has registered it's probe
975 * callback and if there is a probe active for the
976 * syscall 'entry', process the probe.
978 if (systrace_probe_func != NULL && callp->sy_entry != 0)
979 (*systrace_probe_func)(callp->sy_entry, code, callp,
983 AUDIT_SYSCALL_ENTER(code, td);
984 error = (*callp->sy_call)(td, argp);
985 AUDIT_SYSCALL_EXIT(error, td);
987 /* Save the latest error return value. */
988 td->td_errno = error;
992 * If the systrace module has registered it's probe
993 * callback and if there is a probe active for the
994 * syscall 'return', process the probe.
996 if (systrace_probe_func != NULL && callp->sy_return != 0)
997 (*systrace_probe_func)(callp->sy_return, code, callp,
1004 frame->tf_rax = td->td_retval[0];
1005 frame->tf_rdx = td->td_retval[1];
1006 frame->tf_rflags &= ~PSL_C;
1011 * Reconstruct pc, we know that 'syscall' is 2 bytes.
1012 * We have to do a full context restore so that %r10
1013 * (which was holding the value of %rcx) is restored for
1014 * the next iteration.
1016 frame->tf_rip -= frame->tf_err;
1017 frame->tf_r10 = frame->tf_rcx;
1018 td->td_pcb->pcb_flags |= PCB_FULLCTX;
1025 if (p->p_sysent->sv_errsize) {
1026 if (error >= p->p_sysent->sv_errsize)
1027 error = -1; /* XXX */
1029 error = p->p_sysent->sv_errtbl[error];
1031 frame->tf_rax = error;
1032 frame->tf_rflags |= PSL_C;
1039 if (orig_tf_rflags & PSL_T) {
1040 frame->tf_rflags &= ~PSL_T;
1041 ksiginfo_init_trap(&ksi);
1042 ksi.ksi_signo = SIGTRAP;
1043 ksi.ksi_code = TRAP_TRACE;
1044 ksi.ksi_addr = (void *)frame->tf_rip;
1045 trapsignal(td, &ksi);
1049 * Check for misbehavior.
1051 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
1052 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
1053 KASSERT(td->td_critnest == 0,
1054 ("System call %s returning in a critical section",
1055 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
1056 KASSERT(td->td_locks == 0,
1057 ("System call %s returning with %d locks held",
1058 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
1062 * Handle reschedule and other end-of-syscall issues
1066 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
1067 td->td_proc->p_pid, td->td_name, code);
1070 if (KTRPOINT(td, KTR_SYSRET))
1071 ktrsysret(code, error, td->td_retval[0]);
1075 * This works because errno is findable through the
1076 * register set. If we ever support an emulation where this
1077 * is not the case, this code will need to be revisited.
1079 STOPEVENT(p, S_SCX, code);
1081 PTRACESTOP_SC(p, td, S_PT_SCX);