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 (p->p_osrel >= 700004) {
417 ucode = BUS_PAGE_FAULT;
419 } else if (prot_fault_translation == 1) {
421 * Always compat mode.
424 ucode = BUS_PAGE_FAULT;
427 * Always SIGSEGV mode.
435 case T_DIVIDE: /* integer divide fault */
442 /* machine/parity/power fail/"kitchen sink" faults */
443 if (isa_nmi(code) == 0) {
446 * NMI can be hooked up to a pushbutton
450 printf ("NMI ... going to debugger\n");
451 kdb_trap(type, 0, frame);
455 } else if (panic_on_nmi)
456 panic("NMI indicates hardware failure");
460 case T_OFLOW: /* integer overflow fault */
465 case T_BOUND: /* bounds check fault */
471 /* transparent fault (due to context switch "late") */
475 case T_FPOPFLT: /* FPU operand fetch fault */
480 case T_XMMFLT: /* SIMD floating-point exception */
488 KASSERT(cold || td->td_ucred != NULL,
489 ("kernel trap doesn't have ucred"));
491 case T_PAGEFLT: /* page fault */
492 (void) trap_pfault(frame, FALSE);
497 * The kernel is apparently using fpu for copying.
498 * XXX this should be fatal unless the kernel has
499 * registered such use.
502 printf("fpudna in kernel mode!\n");
505 case T_STKFLT: /* stack fault */
508 case T_PROTFLT: /* general protection fault */
509 case T_SEGNPFLT: /* segment not present fault */
510 if (td->td_intr_nesting_level != 0)
514 * Invalid segment selectors and out of bounds
515 * %rip's and %rsp's can be set up in user mode.
516 * This causes a fault in kernel mode when the
517 * kernel tries to return to user mode. We want
518 * to get this fault so that we can fix the
519 * problem here and not have to check all the
520 * selectors and pointers when the user changes
523 if (frame->tf_rip == (long)doreti_iret) {
524 frame->tf_rip = (long)doreti_iret_fault;
527 if (frame->tf_rip == (long)ld_ds) {
529 report_seg_fault("ds", frame);
531 frame->tf_rip = (long)ds_load_fault;
532 frame->tf_ds = _udatasel;
535 if (frame->tf_rip == (long)ld_es) {
537 report_seg_fault("es", frame);
539 frame->tf_rip = (long)es_load_fault;
540 frame->tf_es = _udatasel;
543 if (frame->tf_rip == (long)ld_fs) {
545 report_seg_fault("fs", frame);
547 frame->tf_rip = (long)fs_load_fault;
548 frame->tf_fs = _ufssel;
551 if (frame->tf_rip == (long)ld_gs) {
553 report_seg_fault("gs", frame);
555 frame->tf_rip = (long)gs_load_fault;
556 frame->tf_gs = _ugssel;
559 if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
561 (long)PCPU_GET(curpcb)->pcb_onfault;
568 * PSL_NT can be set in user mode and isn't cleared
569 * automatically when the kernel is entered. This
570 * causes a TSS fault when the kernel attempts to
571 * `iret' because the TSS link is uninitialized. We
572 * want to get this fault so that we can fix the
573 * problem here and not every time the kernel is
576 if (frame->tf_rflags & PSL_NT) {
577 frame->tf_rflags &= ~PSL_NT;
582 case T_TRCTRAP: /* trace trap */
584 * Ignore debug register trace traps due to
585 * accesses in the user's address space, which
586 * can happen under several conditions such as
587 * if a user sets a watchpoint on a buffer and
588 * then passes that buffer to a system call.
589 * We still want to get TRCTRAPS for addresses
590 * in kernel space because that is useful when
591 * debugging the kernel.
593 if (user_dbreg_trap()) {
595 * Reset breakpoint bits because the
598 /* XXX check upper bits here */
599 load_dr6(rdr6() & 0xfffffff0);
603 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
607 * If KDB is enabled, let it handle the debugger trap.
608 * Otherwise, debugger traps "can't happen".
611 if (kdb_trap(type, 0, frame))
618 /* machine/parity/power fail/"kitchen sink" faults */
619 if (isa_nmi(code) == 0) {
622 * NMI can be hooked up to a pushbutton
626 printf ("NMI ... going to debugger\n");
627 kdb_trap(type, 0, frame);
631 } else if (panic_on_nmi == 0)
637 trap_fatal(frame, 0);
641 /* Translate fault for emulators (e.g. Linux) */
642 if (*p->p_sysent->sv_transtrap)
643 i = (*p->p_sysent->sv_transtrap)(i, type);
645 ksiginfo_init_trap(&ksi);
647 ksi.ksi_code = ucode;
648 ksi.ksi_trapno = type;
649 ksi.ksi_addr = (void *)addr;
650 trapsignal(td, &ksi);
654 register_t rg,rgk, rf;
656 if (type <= MAX_TRAP_MSG) {
657 uprintf("fatal process exception: %s",
659 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
660 uprintf(", fault VA = 0x%lx", frame->tf_addr);
663 rf = rdmsr(0xc0000100);
664 rg = rdmsr(0xc0000101);
665 rgk = rdmsr(0xc0000102);
666 uprintf("pid %d TRAP %d rip %lx err %lx addr %lx cs %lx ss %lx ds %x "
667 "es %x fs %x fsbase %lx %lx gs %x gsbase %lx %lx %lx\n",
668 curproc->p_pid, type, frame->tf_rip, frame->tf_err,
670 frame->tf_cs, frame->tf_ss, frame->tf_ds, frame->tf_es,
671 frame->tf_fs, td->td_pcb->pcb_fsbase, rf,
672 frame->tf_gs, td->td_pcb->pcb_gsbase, rg, rgk);
678 mtx_assert(&Giant, MA_NOTOWNED);
685 trap_pfault(frame, usermode)
686 struct trapframe *frame;
690 struct vmspace *vm = NULL;
694 struct thread *td = curthread;
695 struct proc *p = td->td_proc;
696 vm_offset_t eva = frame->tf_addr;
698 va = trunc_page(eva);
699 if (va >= VM_MIN_KERNEL_ADDRESS) {
701 * Don't allow user-mode faults in kernel address space.
709 * This is a fault on non-kernel virtual memory.
710 * vm is initialized above to NULL. If curproc is NULL
711 * or curproc->p_vmspace is NULL the fault is fatal.
723 * PGEX_I is defined only if the execute disable bit capability is
724 * supported and enabled.
726 if (frame->tf_err & PGEX_W)
727 ftype = VM_PROT_WRITE;
728 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
729 ftype = VM_PROT_EXECUTE;
731 ftype = VM_PROT_READ;
733 if (map != kernel_map) {
735 * Keep swapout from messing with us during this
742 /* Fault in the user page: */
743 rv = vm_fault(map, va, ftype,
744 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
752 * Don't have to worry about process locking or stacks in the
755 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
757 if (rv == KERN_SUCCESS)
761 if (td->td_intr_nesting_level == 0 &&
762 PCPU_GET(curpcb)->pcb_onfault != NULL) {
763 frame->tf_rip = (long)PCPU_GET(curpcb)->pcb_onfault;
766 trap_fatal(frame, eva);
770 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
774 trap_fatal(frame, eva)
775 struct trapframe *frame;
781 struct soft_segment_descriptor softseg;
784 code = frame->tf_err;
785 type = frame->tf_trapno;
786 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
789 if (type <= MAX_TRAP_MSG)
790 msg = trap_msg[type];
793 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
794 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
796 /* two separate prints in case of a trap on an unmapped page */
797 printf("cpuid = %d; ", PCPU_GET(cpuid));
798 printf("apic id = %02x\n", PCPU_GET(apic_id));
800 if (type == T_PAGEFLT) {
801 printf("fault virtual address = 0x%lx\n", eva);
802 printf("fault code = %s %s %s, %s\n",
803 code & PGEX_U ? "user" : "supervisor",
804 code & PGEX_W ? "write" : "read",
805 code & PGEX_I ? "instruction" : "data",
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 (ISPL(frame->tf_cs) == SEL_UPL) {
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 = ");
837 (u_long)curproc->p_pid, curthread->td_name ?
838 curthread->td_name : "");
844 if (debugger_on_panic || kdb_active)
845 if (kdb_trap(type, 0, frame))
848 printf("trap number = %d\n", type);
849 if (type <= MAX_TRAP_MSG)
850 panic("%s", trap_msg[type]);
852 panic("unknown/reserved trap");
856 * Double fault handler. Called when a fault occurs while writing
857 * a frame for a trap/exception onto the stack. This usually occurs
858 * when the stack overflows (such is the case with infinite recursion,
862 dblfault_handler(struct trapframe *frame)
865 if (dtrace_doubletrap_func != NULL)
866 (*dtrace_doubletrap_func)();
868 printf("\nFatal double fault\n");
869 printf("rip = 0x%lx\n", frame->tf_rip);
870 printf("rsp = 0x%lx\n", frame->tf_rsp);
871 printf("rbp = 0x%lx\n", frame->tf_rbp);
873 /* two separate prints in case of a trap on an unmapped page */
874 printf("cpuid = %d; ", PCPU_GET(cpuid));
875 printf("apic id = %02x\n", PCPU_GET(apic_id));
877 panic("double fault");
881 * syscall - system call request C handler
883 * A system call is essentially treated as a trap.
886 syscall(struct trapframe *frame)
889 struct sysent *callp;
890 struct thread *td = curthread;
891 struct proc *p = td->td_proc;
892 register_t orig_tf_rflags;
901 PCPU_INC(cnt.v_syscall);
904 if (ISPL(frame->tf_cs) != SEL_UPL) {
913 td->td_frame = frame;
914 if (td->td_ucred != p->p_ucred)
915 cred_update_thread(td);
916 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
917 code = frame->tf_rax;
918 orig_tf_rflags = frame->tf_rflags;
920 if (p->p_sysent->sv_prepsyscall) {
921 (*p->p_sysent->sv_prepsyscall)(frame, (int *)args, &code, ¶ms);
923 if (code == SYS_syscall || code == SYS___syscall) {
924 code = frame->tf_rdi;
930 if (p->p_sysent->sv_mask)
931 code &= p->p_sysent->sv_mask;
933 if (code >= p->p_sysent->sv_size)
934 callp = &p->p_sysent->sv_table[0];
936 callp = &p->p_sysent->sv_table[code];
938 narg = callp->sy_narg;
939 KASSERT(narg <= sizeof(args) / sizeof(args[0]),
940 ("Too many syscall arguments!"));
942 argp = &frame->tf_rdi;
944 bcopy(argp, args, sizeof(args[0]) * regcnt);
946 KASSERT(params != NULL, ("copyin args with no params!"));
947 error = copyin(params, &args[regcnt],
948 (narg - regcnt) * sizeof(args[0]));
953 if (KTRPOINT(td, KTR_SYSCALL))
954 ktrsyscall(code, narg, argp);
957 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
958 td->td_proc->p_pid, td->td_name, code);
963 td->td_retval[0] = 0;
964 td->td_retval[1] = frame->tf_rdx;
966 STOPEVENT(p, S_SCE, narg);
968 PTRACESTOP_SC(p, td, S_PT_SCE);
972 * If the systrace module has registered it's probe
973 * callback and if there is a probe active for the
974 * syscall 'entry', process the probe.
976 if (systrace_probe_func != NULL && callp->sy_entry != 0)
977 (*systrace_probe_func)(callp->sy_entry, code, callp,
981 AUDIT_SYSCALL_ENTER(code, td);
982 error = (*callp->sy_call)(td, argp);
983 AUDIT_SYSCALL_EXIT(error, td);
985 /* Save the latest error return value. */
986 td->td_errno = error;
990 * If the systrace module has registered it's probe
991 * callback and if there is a probe active for the
992 * syscall 'return', process the probe.
994 if (systrace_probe_func != NULL && callp->sy_return != 0)
995 (*systrace_probe_func)(callp->sy_return, code, callp,
1002 frame->tf_rax = td->td_retval[0];
1003 frame->tf_rdx = td->td_retval[1];
1004 frame->tf_rflags &= ~PSL_C;
1009 * Reconstruct pc, we know that 'syscall' is 2 bytes.
1010 * We have to do a full context restore so that %r10
1011 * (which was holding the value of %rcx) is restored for
1012 * the next iteration.
1014 frame->tf_rip -= frame->tf_err;
1015 frame->tf_r10 = frame->tf_rcx;
1016 td->td_pcb->pcb_flags |= PCB_FULLCTX;
1023 if (p->p_sysent->sv_errsize) {
1024 if (error >= p->p_sysent->sv_errsize)
1025 error = -1; /* XXX */
1027 error = p->p_sysent->sv_errtbl[error];
1029 frame->tf_rax = error;
1030 frame->tf_rflags |= PSL_C;
1037 if (orig_tf_rflags & PSL_T) {
1038 frame->tf_rflags &= ~PSL_T;
1039 ksiginfo_init_trap(&ksi);
1040 ksi.ksi_signo = SIGTRAP;
1041 ksi.ksi_code = TRAP_TRACE;
1042 ksi.ksi_addr = (void *)frame->tf_rip;
1043 trapsignal(td, &ksi);
1047 * Check for misbehavior.
1049 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
1050 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
1051 KASSERT(td->td_critnest == 0,
1052 ("System call %s returning in a critical section",
1053 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
1054 KASSERT(td->td_locks == 0,
1055 ("System call %s returning with %d locks held",
1056 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
1060 * Handle reschedule and other end-of-syscall issues
1064 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
1065 td->td_proc->p_pid, td->td_name, code);
1068 if (KTRPOINT(td, KTR_SYSRET))
1069 ktrsysret(code, error, td->td_retval[0]);
1073 * This works because errno is findable through the
1074 * register set. If we ever support an emulation where this
1075 * is not the case, this code will need to be revisited.
1077 STOPEVENT(p, S_SCX, code);
1079 PTRACESTOP_SC(p, td, S_PT_SCX);