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[];
176 * Exception, fault, and trap interface to the FreeBSD kernel.
177 * This common code is called from assembly language IDT gate entry
178 * routines that prepare a suitable stack frame, and restore this
179 * frame after the exception has been processed.
183 trap(struct trapframe *frame)
185 struct thread *td = curthread;
186 struct proc *p = td->td_proc;
187 int i = 0, ucode = 0, code;
192 PCPU_INC(cnt.v_trap);
193 type = frame->tf_trapno;
196 /* Handler for NMI IPIs used for stopping CPUs. */
198 if (ipi_nmi_handler() == 0)
210 if (type == T_RESERVED) {
211 trap_fatal(frame, 0);
217 * CPU PMCs interrupt using an NMI. If the PMC module is
218 * active, pass the 'rip' value to the PMC module's interrupt
219 * handler. A return value of '1' from the handler means that
220 * the NMI was handled by it and we can return immediately.
222 if (type == T_NMI && pmc_intr &&
223 (*pmc_intr)(PCPU_GET(cpuid), frame))
227 if (type == T_MCHK) {
229 trap_fatal(frame, 0);
235 * A trap can occur while DTrace executes a probe. Before
236 * executing the probe, DTrace blocks re-scheduling and sets
237 * a flag in it's per-cpu flags to indicate that it doesn't
238 * want to fault. On returning from the the probe, the no-fault
239 * flag is cleared and finally re-scheduling is enabled.
241 * If the DTrace kernel module has registered a trap handler,
242 * call it and if it returns non-zero, assume that it has
243 * handled the trap and modified the trap frame so that this
244 * function can return normally.
246 if (dtrace_trap_func != NULL)
247 if ((*dtrace_trap_func)(frame, type))
251 if ((frame->tf_rflags & PSL_I) == 0) {
253 * Buggy application or kernel code has disabled
254 * interrupts and then trapped. Enabling interrupts
255 * now is wrong, but it is better than running with
256 * interrupts disabled until they are accidentally
259 if (ISPL(frame->tf_cs) == SEL_UPL)
261 "pid %ld (%s): trap %d with interrupts disabled\n",
262 (long)curproc->p_pid, curthread->td_name, type);
263 else if (type != T_NMI && type != T_BPTFLT &&
266 * XXX not quite right, since this may be for a
267 * multiple fault in user mode.
269 printf("kernel trap %d with interrupts disabled\n",
273 * We shouldn't enable interrupts while holding a
274 * spin lock or servicing an NMI.
276 if (type != T_NMI && td->td_md.md_spinlock_count == 0)
281 code = frame->tf_err;
282 if (type == T_PAGEFLT) {
284 * If we get a page fault while in a critical section, then
285 * it is most likely a fatal kernel page fault. The kernel
286 * is already going to panic trying to get a sleep lock to
287 * do the VM lookup, so just consider it a fatal trap so the
288 * kernel can print out a useful trap message and even get
291 * If we get a page fault while holding a non-sleepable
292 * lock, then it is most likely a fatal kernel page fault.
293 * If WITNESS is enabled, then it's going to whine about
294 * bogus LORs with various VM locks, so just skip to the
295 * fatal trap handling directly.
297 if (td->td_critnest != 0 ||
298 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
299 "Kernel page fault") != 0)
300 trap_fatal(frame, frame->tf_addr);
303 if (ISPL(frame->tf_cs) == SEL_UPL) {
307 td->td_frame = frame;
308 addr = frame->tf_rip;
309 if (td->td_ucred != p->p_ucred)
310 cred_update_thread(td);
313 case T_PRIVINFLT: /* privileged instruction fault */
318 case T_BPTFLT: /* bpt instruction fault */
319 case T_TRCTRAP: /* trace trap */
321 frame->tf_rflags &= ~PSL_T;
323 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
326 case T_ARITHTRAP: /* arithmetic trap */
333 case T_PROTFLT: /* general protection fault */
337 case T_STKFLT: /* stack fault */
338 case T_SEGNPFLT: /* segment not present fault */
342 case T_TSSFLT: /* invalid TSS fault */
346 case T_DOUBLEFLT: /* double fault */
352 case T_PAGEFLT: /* page fault */
353 addr = frame->tf_addr;
354 i = trap_pfault(frame, TRUE);
363 if (prot_fault_translation == 0) {
366 * This check also covers the images
367 * without the ABI-tag ELF note.
369 if (SV_CURPROC_ABI() ==
371 p->p_osrel >= 700004) {
376 ucode = BUS_PAGE_FAULT;
378 } else if (prot_fault_translation == 1) {
380 * Always compat mode.
383 ucode = BUS_PAGE_FAULT;
386 * Always SIGSEGV mode.
394 case T_DIVIDE: /* integer divide fault */
401 /* machine/parity/power fail/"kitchen sink" faults */
402 if (isa_nmi(code) == 0) {
405 * NMI can be hooked up to a pushbutton
409 printf ("NMI ... going to debugger\n");
410 kdb_trap(type, 0, frame);
414 } else if (panic_on_nmi)
415 panic("NMI indicates hardware failure");
419 case T_OFLOW: /* integer overflow fault */
424 case T_BOUND: /* bounds check fault */
430 /* transparent fault (due to context switch "late") */
434 case T_FPOPFLT: /* FPU operand fetch fault */
439 case T_XMMFLT: /* SIMD floating-point exception */
447 KASSERT(cold || td->td_ucred != NULL,
448 ("kernel trap doesn't have ucred"));
450 case T_PAGEFLT: /* page fault */
451 (void) trap_pfault(frame, FALSE);
456 * The kernel is apparently using fpu for copying.
457 * XXX this should be fatal unless the kernel has
458 * registered such use.
461 printf("fpudna in kernel mode!\n");
464 case T_STKFLT: /* stack fault */
467 case T_PROTFLT: /* general protection fault */
468 case T_SEGNPFLT: /* segment not present fault */
469 if (td->td_intr_nesting_level != 0)
473 * Invalid segment selectors and out of bounds
474 * %rip's and %rsp's can be set up in user mode.
475 * This causes a fault in kernel mode when the
476 * kernel tries to return to user mode. We want
477 * to get this fault so that we can fix the
478 * problem here and not have to check all the
479 * selectors and pointers when the user changes
482 if (frame->tf_rip == (long)doreti_iret) {
483 frame->tf_rip = (long)doreti_iret_fault;
486 if (frame->tf_rip == (long)ld_ds) {
487 frame->tf_rip = (long)ds_load_fault;
488 frame->tf_ds = _udatasel;
491 if (frame->tf_rip == (long)ld_es) {
492 frame->tf_rip = (long)es_load_fault;
493 frame->tf_es = _udatasel;
496 if (frame->tf_rip == (long)ld_fs) {
497 frame->tf_rip = (long)fs_load_fault;
498 frame->tf_fs = _ufssel;
501 if (frame->tf_rip == (long)ld_gs) {
502 frame->tf_rip = (long)gs_load_fault;
503 frame->tf_gs = _ugssel;
506 if (frame->tf_rip == (long)ld_gsbase) {
507 frame->tf_rip = (long)gsbase_load_fault;
510 if (frame->tf_rip == (long)ld_fsbase) {
511 frame->tf_rip = (long)fsbase_load_fault;
514 if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
516 (long)PCPU_GET(curpcb)->pcb_onfault;
523 * PSL_NT can be set in user mode and isn't cleared
524 * automatically when the kernel is entered. This
525 * causes a TSS fault when the kernel attempts to
526 * `iret' because the TSS link is uninitialized. We
527 * want to get this fault so that we can fix the
528 * problem here and not every time the kernel is
531 if (frame->tf_rflags & PSL_NT) {
532 frame->tf_rflags &= ~PSL_NT;
537 case T_TRCTRAP: /* trace trap */
539 * Ignore debug register trace traps due to
540 * accesses in the user's address space, which
541 * can happen under several conditions such as
542 * if a user sets a watchpoint on a buffer and
543 * then passes that buffer to a system call.
544 * We still want to get TRCTRAPS for addresses
545 * in kernel space because that is useful when
546 * debugging the kernel.
548 if (user_dbreg_trap()) {
550 * Reset breakpoint bits because the
553 /* XXX check upper bits here */
554 load_dr6(rdr6() & 0xfffffff0);
558 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
562 * If KDB is enabled, let it handle the debugger trap.
563 * Otherwise, debugger traps "can't happen".
566 if (kdb_trap(type, 0, frame))
573 /* machine/parity/power fail/"kitchen sink" faults */
574 if (isa_nmi(code) == 0) {
577 * NMI can be hooked up to a pushbutton
581 printf ("NMI ... going to debugger\n");
582 kdb_trap(type, 0, frame);
586 } else if (panic_on_nmi == 0)
592 trap_fatal(frame, 0);
596 /* Translate fault for emulators (e.g. Linux) */
597 if (*p->p_sysent->sv_transtrap)
598 i = (*p->p_sysent->sv_transtrap)(i, type);
600 ksiginfo_init_trap(&ksi);
602 ksi.ksi_code = ucode;
603 ksi.ksi_trapno = type;
604 ksi.ksi_addr = (void *)addr;
605 trapsignal(td, &ksi);
609 mtx_assert(&Giant, MA_NOTOWNED);
616 trap_pfault(frame, usermode)
617 struct trapframe *frame;
621 struct vmspace *vm = NULL;
625 struct thread *td = curthread;
626 struct proc *p = td->td_proc;
627 vm_offset_t eva = frame->tf_addr;
629 va = trunc_page(eva);
630 if (va >= VM_MIN_KERNEL_ADDRESS) {
632 * Don't allow user-mode faults in kernel address space.
640 * This is a fault on non-kernel virtual memory.
641 * vm is initialized above to NULL. If curproc is NULL
642 * or curproc->p_vmspace is NULL the fault is fatal.
654 * PGEX_I is defined only if the execute disable bit capability is
655 * supported and enabled.
657 if (frame->tf_err & PGEX_W)
658 ftype = VM_PROT_WRITE;
659 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
660 ftype = VM_PROT_EXECUTE;
662 ftype = VM_PROT_READ;
664 if (map != kernel_map) {
666 * Keep swapout from messing with us during this
673 /* Fault in the user page: */
674 rv = vm_fault(map, va, ftype,
675 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
683 * Don't have to worry about process locking or stacks in the
686 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
688 if (rv == KERN_SUCCESS)
692 if (td->td_intr_nesting_level == 0 &&
693 PCPU_GET(curpcb)->pcb_onfault != NULL) {
694 frame->tf_rip = (long)PCPU_GET(curpcb)->pcb_onfault;
697 trap_fatal(frame, eva);
701 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
705 trap_fatal(frame, eva)
706 struct trapframe *frame;
712 struct soft_segment_descriptor softseg;
715 code = frame->tf_err;
716 type = frame->tf_trapno;
717 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
720 if (type <= MAX_TRAP_MSG)
721 msg = trap_msg[type];
724 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
725 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
727 /* two separate prints in case of a trap on an unmapped page */
728 printf("cpuid = %d; ", PCPU_GET(cpuid));
729 printf("apic id = %02x\n", PCPU_GET(apic_id));
731 if (type == T_PAGEFLT) {
732 printf("fault virtual address = 0x%lx\n", eva);
733 printf("fault code = %s %s %s, %s\n",
734 code & PGEX_U ? "user" : "supervisor",
735 code & PGEX_W ? "write" : "read",
736 code & PGEX_I ? "instruction" : "data",
737 code & PGEX_P ? "protection violation" : "page not present");
739 printf("instruction pointer = 0x%lx:0x%lx\n",
740 frame->tf_cs & 0xffff, frame->tf_rip);
741 if (ISPL(frame->tf_cs) == SEL_UPL) {
742 ss = frame->tf_ss & 0xffff;
745 ss = GSEL(GDATA_SEL, SEL_KPL);
746 esp = (long)&frame->tf_rsp;
748 printf("stack pointer = 0x%x:0x%lx\n", ss, esp);
749 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
750 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
751 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
752 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
753 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
755 printf("processor eflags = ");
756 if (frame->tf_rflags & PSL_T)
757 printf("trace trap, ");
758 if (frame->tf_rflags & PSL_I)
759 printf("interrupt enabled, ");
760 if (frame->tf_rflags & PSL_NT)
761 printf("nested task, ");
762 if (frame->tf_rflags & PSL_RF)
764 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
765 printf("current process = ");
768 (u_long)curproc->p_pid, curthread->td_name ?
769 curthread->td_name : "");
775 if (debugger_on_panic || kdb_active)
776 if (kdb_trap(type, 0, frame))
779 printf("trap number = %d\n", type);
780 if (type <= MAX_TRAP_MSG)
781 panic("%s", trap_msg[type]);
783 panic("unknown/reserved trap");
787 * Double fault handler. Called when a fault occurs while writing
788 * a frame for a trap/exception onto the stack. This usually occurs
789 * when the stack overflows (such is the case with infinite recursion,
793 dblfault_handler(struct trapframe *frame)
796 if (dtrace_doubletrap_func != NULL)
797 (*dtrace_doubletrap_func)();
799 printf("\nFatal double fault\n");
800 printf("rip = 0x%lx\n", frame->tf_rip);
801 printf("rsp = 0x%lx\n", frame->tf_rsp);
802 printf("rbp = 0x%lx\n", frame->tf_rbp);
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 panic("double fault");
811 struct syscall_args {
813 struct sysent *callp;
820 fetch_syscall_args(struct thread *td, struct syscall_args *sa)
823 struct trapframe *frame;
825 int reg, regcnt, error;
828 frame = td->td_frame;
832 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
833 sa->code = frame->tf_rax;
835 if (p->p_sysent->sv_prepsyscall) {
836 (*p->p_sysent->sv_prepsyscall)(frame, (int *)sa->args,
839 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
840 sa->code = frame->tf_rdi;
845 if (p->p_sysent->sv_mask)
846 sa->code &= p->p_sysent->sv_mask;
848 if (sa->code >= p->p_sysent->sv_size)
849 sa->callp = &p->p_sysent->sv_table[0];
851 sa->callp = &p->p_sysent->sv_table[sa->code];
853 sa->narg = sa->callp->sy_narg;
854 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]),
855 ("Too many syscall arguments!"));
857 sa->argp = &frame->tf_rdi;
859 bcopy(sa->argp, sa->args, sizeof(sa->args[0]) * regcnt);
860 if (sa->narg > regcnt) {
861 KASSERT(params != NULL, ("copyin args with no params!"));
862 error = copyin(params, &sa->args[regcnt],
863 (sa->narg - regcnt) * sizeof(sa->args[0]));
865 sa->argp = &sa->args[0];
868 * This may result in two records if debugger modified
869 * registers or memory during sleep at stop/ptrace point.
872 if (KTRPOINT(td, KTR_SYSCALL))
873 ktrsyscall(sa->code, sa->narg, sa->argp);
879 * syscall - system call request C handler
881 * A system call is essentially treated as a trap.
884 syscall(struct trapframe *frame)
888 struct syscall_args sa;
889 register_t orig_tf_rflags;
893 PCPU_INC(cnt.v_syscall);
899 if (ISPL(frame->tf_cs) != SEL_UPL) {
906 td->td_frame = frame;
907 if (td->td_ucred != p->p_ucred)
908 cred_update_thread(td);
909 orig_tf_rflags = frame->tf_rflags;
910 if (p->p_flag & P_TRACED) {
912 td->td_dbgflags &= ~TDB_USERWR;
915 error = fetch_syscall_args(td, &sa);
917 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
918 td->td_proc->p_pid, td->td_name, sa.code);
921 td->td_retval[0] = 0;
922 td->td_retval[1] = frame->tf_rdx;
924 STOPEVENT(p, S_SCE, sa.narg);
925 PTRACESTOP_SC(p, td, S_PT_SCE);
926 if (td->td_dbgflags & TDB_USERWR) {
928 * Reread syscall number and arguments if
929 * debugger modified registers or memory.
931 error = fetch_syscall_args(td, &sa);
934 td->td_retval[1] = frame->tf_rdx;
939 * If the systrace module has registered it's probe
940 * callback and if there is a probe active for the
941 * syscall 'entry', process the probe.
943 if (systrace_probe_func != NULL && sa.callp->sy_entry != 0)
944 (*systrace_probe_func)(sa.callp->sy_entry, sa.code,
948 AUDIT_SYSCALL_ENTER(sa.code, td);
949 error = (*sa.callp->sy_call)(td, sa.argp);
950 AUDIT_SYSCALL_EXIT(error, td);
952 /* Save the latest error return value. */
953 td->td_errno = error;
957 * If the systrace module has registered it's probe
958 * callback and if there is a probe active for the
959 * syscall 'return', process the probe.
961 if (systrace_probe_func != NULL && sa.callp->sy_return != 0)
962 (*systrace_probe_func)(sa.callp->sy_return, sa.code,
967 cpu_set_syscall_retval(td, error);
972 if (orig_tf_rflags & PSL_T) {
973 frame->tf_rflags &= ~PSL_T;
974 ksiginfo_init_trap(&ksi);
975 ksi.ksi_signo = SIGTRAP;
976 ksi.ksi_code = TRAP_TRACE;
977 ksi.ksi_addr = (void *)frame->tf_rip;
978 trapsignal(td, &ksi);
982 * Check for misbehavior.
984 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
985 (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
986 syscallnames[sa.code] : "???");
987 KASSERT(td->td_critnest == 0,
988 ("System call %s returning in a critical section",
989 (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
990 syscallnames[sa.code] : "???"));
991 KASSERT(td->td_locks == 0,
992 ("System call %s returning with %d locks held",
993 (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
994 syscallnames[sa.code] : "???", td->td_locks));
997 * Handle reschedule and other end-of-syscall issues
1001 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
1002 td->td_proc->p_pid, td->td_name, sa.code);
1005 if (KTRPOINT(td, KTR_SYSRET))
1006 ktrsysret(sa.code, error, td->td_retval[0]);
1010 * This works because errno is findable through the
1011 * register set. If we ever support an emulation where this
1012 * is not the case, this code will need to be revisited.
1014 STOPEVENT(p, S_SCX, sa.code);
1016 PTRACESTOP_SC(p, td, S_PT_SCX);