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"
54 #include <sys/param.h>
56 #include <sys/systm.h>
58 #include <sys/pioctl.h>
59 #include <sys/ptrace.h>
61 #include <sys/kernel.h>
64 #include <sys/mutex.h>
65 #include <sys/resourcevar.h>
66 #include <sys/signalvar.h>
67 #include <sys/syscall.h>
68 #include <sys/sysctl.h>
69 #include <sys/sysent.h>
71 #include <sys/vmmeter.h>
73 #include <sys/pmckern.h>
77 #include <vm/vm_param.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_extern.h>
84 #include <machine/cpu.h>
85 #include <machine/intr_machdep.h>
87 #include <machine/md_var.h>
88 #include <machine/pcb.h>
90 #include <machine/smp.h>
92 #include <machine/tss.h>
95 #include <sys/dtrace_bsd.h>
98 * This is a hook which is initialised by the dtrace module
99 * to handle traps which might occur during DTrace probe
102 dtrace_trap_func_t dtrace_trap_func;
104 dtrace_doubletrap_func_t dtrace_doubletrap_func;
107 * This is a hook which is initialised by the systrace module
108 * when it is loaded. This keeps the DTrace syscall provider
109 * implementation opaque.
111 systrace_probe_func_t systrace_probe_func;
114 * These hooks are necessary for the pid, usdt and fasttrap providers.
116 dtrace_fasttrap_probe_ptr_t dtrace_fasttrap_probe_ptr;
117 dtrace_pid_probe_ptr_t dtrace_pid_probe_ptr;
118 dtrace_return_probe_ptr_t dtrace_return_probe_ptr;
121 extern void trap(struct trapframe *frame);
122 extern void syscall(struct trapframe *frame);
123 void dblfault_handler(struct trapframe *frame);
125 static int trap_pfault(struct trapframe *, int);
126 static void trap_fatal(struct trapframe *, vm_offset_t);
128 #define MAX_TRAP_MSG 33
129 static char *trap_msg[] = {
131 "privileged instruction fault", /* 1 T_PRIVINFLT */
133 "breakpoint instruction fault", /* 3 T_BPTFLT */
136 "arithmetic trap", /* 6 T_ARITHTRAP */
139 "general protection fault", /* 9 T_PROTFLT */
140 "trace trap", /* 10 T_TRCTRAP */
142 "page fault", /* 12 T_PAGEFLT */
144 "alignment fault", /* 14 T_ALIGNFLT */
148 "integer divide fault", /* 18 T_DIVIDE */
149 "non-maskable interrupt trap", /* 19 T_NMI */
150 "overflow trap", /* 20 T_OFLOW */
151 "FPU bounds check fault", /* 21 T_BOUND */
152 "FPU device not available", /* 22 T_DNA */
153 "double fault", /* 23 T_DOUBLEFLT */
154 "FPU operand fetch fault", /* 24 T_FPOPFLT */
155 "invalid TSS fault", /* 25 T_TSSFLT */
156 "segment not present fault", /* 26 T_SEGNPFLT */
157 "stack fault", /* 27 T_STKFLT */
158 "machine check trap", /* 28 T_MCHK */
159 "SIMD floating-point exception", /* 29 T_XMMFLT */
160 "reserved (unknown) fault", /* 30 T_RESERVED */
161 "", /* 31 unused (reserved) */
162 "DTrace pid return trap", /* 32 T_DTRACE_RET */
163 "DTrace fasttrap probe trap", /* 33 T_DTRACE_PROBE */
167 static int kdb_on_nmi = 1;
168 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
169 &kdb_on_nmi, 0, "Go to KDB on NMI");
170 TUNABLE_INT("machdep.kdb_on_nmi", &kdb_on_nmi);
172 static int panic_on_nmi = 1;
173 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
174 &panic_on_nmi, 0, "Panic on NMI");
175 TUNABLE_INT("machdep.panic_on_nmi", &panic_on_nmi);
176 static int prot_fault_translation = 0;
177 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
178 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
181 * Exception, fault, and trap interface to the FreeBSD kernel.
182 * This common code is called from assembly language IDT gate entry
183 * routines that prepare a suitable stack frame, and restore this
184 * frame after the exception has been processed.
188 trap(struct trapframe *frame)
190 struct thread *td = curthread;
191 struct proc *p = td->td_proc;
192 int i = 0, ucode = 0, code;
197 PCPU_INC(cnt.v_trap);
198 type = frame->tf_trapno;
201 /* Handler for NMI IPIs used for stopping CPUs. */
203 if (ipi_nmi_handler() == 0)
215 if (type == T_RESERVED) {
216 trap_fatal(frame, 0);
222 * CPU PMCs interrupt using an NMI. If the PMC module is
223 * active, pass the 'rip' value to the PMC module's interrupt
224 * handler. A return value of '1' from the handler means that
225 * the NMI was handled by it and we can return immediately.
227 if (type == T_NMI && pmc_intr &&
228 (*pmc_intr)(PCPU_GET(cpuid), frame))
232 if (type == T_MCHK) {
234 trap_fatal(frame, 0);
240 * A trap can occur while DTrace executes a probe. Before
241 * executing the probe, DTrace blocks re-scheduling and sets
242 * a flag in it's per-cpu flags to indicate that it doesn't
243 * want to fault. On returning from the probe, the no-fault
244 * flag is cleared and finally re-scheduling is enabled.
246 * If the DTrace kernel module has registered a trap handler,
247 * call it and if it returns non-zero, assume that it has
248 * handled the trap and modified the trap frame so that this
249 * function can return normally.
251 if (type == T_DTRACE_PROBE || type == T_DTRACE_RET ||
255 fill_frame_regs(frame, ®s);
256 if (type == T_DTRACE_PROBE &&
257 dtrace_fasttrap_probe_ptr != NULL &&
258 dtrace_fasttrap_probe_ptr(®s) == 0)
260 else if (type == T_BPTFLT &&
261 dtrace_pid_probe_ptr != NULL &&
262 dtrace_pid_probe_ptr(®s) == 0)
264 else if (type == T_DTRACE_RET &&
265 dtrace_return_probe_ptr != NULL &&
266 dtrace_return_probe_ptr(®s) == 0)
269 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
273 if ((frame->tf_rflags & PSL_I) == 0) {
275 * Buggy application or kernel code has disabled
276 * interrupts and then trapped. Enabling interrupts
277 * now is wrong, but it is better than running with
278 * interrupts disabled until they are accidentally
281 if (ISPL(frame->tf_cs) == SEL_UPL)
283 "pid %ld (%s): trap %d with interrupts disabled\n",
284 (long)curproc->p_pid, curthread->td_name, type);
285 else if (type != T_NMI && type != T_BPTFLT &&
288 * XXX not quite right, since this may be for a
289 * multiple fault in user mode.
291 printf("kernel trap %d with interrupts disabled\n",
295 * We shouldn't enable interrupts while holding a
298 if (td->td_md.md_spinlock_count == 0)
303 code = frame->tf_err;
304 if (type == T_PAGEFLT) {
306 * If we get a page fault while in a critical section, then
307 * it is most likely a fatal kernel page fault. The kernel
308 * is already going to panic trying to get a sleep lock to
309 * do the VM lookup, so just consider it a fatal trap so the
310 * kernel can print out a useful trap message and even get
313 * If we get a page fault while holding a non-sleepable
314 * lock, then it is most likely a fatal kernel page fault.
315 * If WITNESS is enabled, then it's going to whine about
316 * bogus LORs with various VM locks, so just skip to the
317 * fatal trap handling directly.
319 if (td->td_critnest != 0 ||
320 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
321 "Kernel page fault") != 0)
322 trap_fatal(frame, frame->tf_addr);
325 if (ISPL(frame->tf_cs) == SEL_UPL) {
329 td->td_frame = frame;
330 addr = frame->tf_rip;
331 if (td->td_ucred != p->p_ucred)
332 cred_update_thread(td);
335 case T_PRIVINFLT: /* privileged instruction fault */
340 case T_BPTFLT: /* bpt instruction fault */
341 case T_TRCTRAP: /* trace trap */
343 frame->tf_rflags &= ~PSL_T;
345 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
348 case T_ARITHTRAP: /* arithmetic trap */
355 case T_PROTFLT: /* general protection fault */
359 case T_STKFLT: /* stack fault */
360 case T_SEGNPFLT: /* segment not present fault */
364 case T_TSSFLT: /* invalid TSS fault */
368 case T_DOUBLEFLT: /* double fault */
374 case T_PAGEFLT: /* page fault */
375 addr = frame->tf_addr;
376 i = trap_pfault(frame, TRUE);
385 if (prot_fault_translation == 0) {
388 * This check also covers the images
389 * without the ABI-tag ELF note.
391 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
392 && p->p_osrel >= P_OSREL_SIGSEGV) {
397 ucode = BUS_PAGE_FAULT;
399 } else if (prot_fault_translation == 1) {
401 * Always compat mode.
404 ucode = BUS_PAGE_FAULT;
407 * Always SIGSEGV mode.
415 case T_DIVIDE: /* integer divide fault */
422 /* machine/parity/power fail/"kitchen sink" faults */
423 if (isa_nmi(code) == 0) {
426 * NMI can be hooked up to a pushbutton
430 printf ("NMI ... going to debugger\n");
431 kdb_trap(type, 0, frame);
435 } else if (panic_on_nmi)
436 panic("NMI indicates hardware failure");
440 case T_OFLOW: /* integer overflow fault */
445 case T_BOUND: /* bounds check fault */
451 /* transparent fault (due to context switch "late") */
452 KASSERT(PCB_USER_FPU(td->td_pcb),
453 ("kernel FPU ctx has leaked"));
457 case T_FPOPFLT: /* FPU operand fetch fault */
462 case T_XMMFLT: /* SIMD floating-point exception */
470 KASSERT(cold || td->td_ucred != NULL,
471 ("kernel trap doesn't have ucred"));
473 case T_PAGEFLT: /* page fault */
474 (void) trap_pfault(frame, FALSE);
478 KASSERT(!PCB_USER_FPU(td->td_pcb),
479 ("Unregistered use of FPU in kernel"));
483 case T_ARITHTRAP: /* arithmetic trap */
484 case T_XMMFLT: /* SIMD floating-point exception */
485 case T_FPOPFLT: /* FPU operand fetch fault */
487 * XXXKIB for now disable any FPU traps in kernel
488 * handler registration seems to be overkill
490 trap_fatal(frame, 0);
493 case T_STKFLT: /* stack fault */
496 case T_PROTFLT: /* general protection fault */
497 case T_SEGNPFLT: /* segment not present fault */
498 if (td->td_intr_nesting_level != 0)
502 * Invalid segment selectors and out of bounds
503 * %rip's and %rsp's can be set up in user mode.
504 * This causes a fault in kernel mode when the
505 * kernel tries to return to user mode. We want
506 * to get this fault so that we can fix the
507 * problem here and not have to check all the
508 * selectors and pointers when the user changes
511 if (frame->tf_rip == (long)doreti_iret) {
512 frame->tf_rip = (long)doreti_iret_fault;
515 if (frame->tf_rip == (long)ld_ds) {
516 frame->tf_rip = (long)ds_load_fault;
519 if (frame->tf_rip == (long)ld_es) {
520 frame->tf_rip = (long)es_load_fault;
523 if (frame->tf_rip == (long)ld_fs) {
524 frame->tf_rip = (long)fs_load_fault;
527 if (frame->tf_rip == (long)ld_gs) {
528 frame->tf_rip = (long)gs_load_fault;
531 if (frame->tf_rip == (long)ld_gsbase) {
532 frame->tf_rip = (long)gsbase_load_fault;
535 if (frame->tf_rip == (long)ld_fsbase) {
536 frame->tf_rip = (long)fsbase_load_fault;
539 if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
541 (long)PCPU_GET(curpcb)->pcb_onfault;
548 * PSL_NT can be set in user mode and isn't cleared
549 * automatically when the kernel is entered. This
550 * causes a TSS fault when the kernel attempts to
551 * `iret' because the TSS link is uninitialized. We
552 * want to get this fault so that we can fix the
553 * problem here and not every time the kernel is
556 if (frame->tf_rflags & PSL_NT) {
557 frame->tf_rflags &= ~PSL_NT;
562 case T_TRCTRAP: /* trace trap */
564 * Ignore debug register trace traps due to
565 * accesses in the user's address space, which
566 * can happen under several conditions such as
567 * if a user sets a watchpoint on a buffer and
568 * then passes that buffer to a system call.
569 * We still want to get TRCTRAPS for addresses
570 * in kernel space because that is useful when
571 * debugging the kernel.
573 if (user_dbreg_trap()) {
575 * Reset breakpoint bits because the
578 /* XXX check upper bits here */
579 load_dr6(rdr6() & 0xfffffff0);
583 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
587 * If KDB is enabled, let it handle the debugger trap.
588 * Otherwise, debugger traps "can't happen".
591 if (kdb_trap(type, 0, frame))
598 /* machine/parity/power fail/"kitchen sink" faults */
599 if (isa_nmi(code) == 0) {
602 * NMI can be hooked up to a pushbutton
606 printf ("NMI ... going to debugger\n");
607 kdb_trap(type, 0, frame);
611 } else if (panic_on_nmi == 0)
617 trap_fatal(frame, 0);
621 /* Translate fault for emulators (e.g. Linux) */
622 if (*p->p_sysent->sv_transtrap)
623 i = (*p->p_sysent->sv_transtrap)(i, type);
625 ksiginfo_init_trap(&ksi);
627 ksi.ksi_code = ucode;
628 ksi.ksi_trapno = type;
629 ksi.ksi_addr = (void *)addr;
630 trapsignal(td, &ksi);
634 mtx_assert(&Giant, MA_NOTOWNED);
635 KASSERT(PCB_USER_FPU(td->td_pcb),
636 ("Return from trap with kernel FPU ctx leaked"));
643 trap_pfault(frame, usermode)
644 struct trapframe *frame;
648 struct vmspace *vm = NULL;
652 struct thread *td = curthread;
653 struct proc *p = td->td_proc;
654 vm_offset_t eva = frame->tf_addr;
656 va = trunc_page(eva);
657 if (va >= VM_MIN_KERNEL_ADDRESS) {
659 * Don't allow user-mode faults in kernel address space.
667 * This is a fault on non-kernel virtual memory.
668 * vm is initialized above to NULL. If curproc is NULL
669 * or curproc->p_vmspace is NULL the fault is fatal.
680 * When accessing a usermode address, kernel must be
681 * ready to accept the page fault, and provide a
682 * handling routine. Since accessing the address
683 * without the handler is a bug, do not try to handle
684 * it normally, and panic immediately.
686 if (!usermode && (td->td_intr_nesting_level != 0 ||
687 PCPU_GET(curpcb)->pcb_onfault == NULL)) {
688 trap_fatal(frame, eva);
694 * PGEX_I is defined only if the execute disable bit capability is
695 * supported and enabled.
697 if (frame->tf_err & PGEX_W)
698 ftype = VM_PROT_WRITE;
699 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
700 ftype = VM_PROT_EXECUTE;
702 ftype = VM_PROT_READ;
704 if (map != kernel_map) {
706 * Keep swapout from messing with us during this
713 /* Fault in the user page: */
714 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
721 * Don't have to worry about process locking or stacks in the
724 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
726 if (rv == KERN_SUCCESS)
730 if (td->td_intr_nesting_level == 0 &&
731 PCPU_GET(curpcb)->pcb_onfault != NULL) {
732 frame->tf_rip = (long)PCPU_GET(curpcb)->pcb_onfault;
735 trap_fatal(frame, eva);
739 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
743 trap_fatal(frame, eva)
744 struct trapframe *frame;
750 struct soft_segment_descriptor softseg;
753 code = frame->tf_err;
754 type = frame->tf_trapno;
755 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
758 if (type <= MAX_TRAP_MSG)
759 msg = trap_msg[type];
762 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
763 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
765 /* two separate prints in case of a trap on an unmapped page */
766 printf("cpuid = %d; ", PCPU_GET(cpuid));
767 printf("apic id = %02x\n", PCPU_GET(apic_id));
769 if (type == T_PAGEFLT) {
770 printf("fault virtual address = 0x%lx\n", eva);
771 printf("fault code = %s %s %s, %s\n",
772 code & PGEX_U ? "user" : "supervisor",
773 code & PGEX_W ? "write" : "read",
774 code & PGEX_I ? "instruction" : "data",
775 code & PGEX_P ? "protection violation" : "page not present");
777 printf("instruction pointer = 0x%lx:0x%lx\n",
778 frame->tf_cs & 0xffff, frame->tf_rip);
779 if (ISPL(frame->tf_cs) == SEL_UPL) {
780 ss = frame->tf_ss & 0xffff;
783 ss = GSEL(GDATA_SEL, SEL_KPL);
784 esp = (long)&frame->tf_rsp;
786 printf("stack pointer = 0x%x:0x%lx\n", ss, esp);
787 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
788 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
789 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
790 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
791 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
793 printf("processor eflags = ");
794 if (frame->tf_rflags & PSL_T)
795 printf("trace trap, ");
796 if (frame->tf_rflags & PSL_I)
797 printf("interrupt enabled, ");
798 if (frame->tf_rflags & PSL_NT)
799 printf("nested task, ");
800 if (frame->tf_rflags & PSL_RF)
802 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
803 printf("current process = ");
806 (u_long)curproc->p_pid, curthread->td_name ?
807 curthread->td_name : "");
813 if (debugger_on_panic || kdb_active)
814 if (kdb_trap(type, 0, frame))
817 printf("trap number = %d\n", type);
818 if (type <= MAX_TRAP_MSG)
819 panic("%s", trap_msg[type]);
821 panic("unknown/reserved trap");
825 * Double fault handler. Called when a fault occurs while writing
826 * a frame for a trap/exception onto the stack. This usually occurs
827 * when the stack overflows (such is the case with infinite recursion,
831 dblfault_handler(struct trapframe *frame)
834 if (dtrace_doubletrap_func != NULL)
835 (*dtrace_doubletrap_func)();
837 printf("\nFatal double fault\n");
838 printf("rip = 0x%lx\n", frame->tf_rip);
839 printf("rsp = 0x%lx\n", frame->tf_rsp);
840 printf("rbp = 0x%lx\n", frame->tf_rbp);
842 /* two separate prints in case of a trap on an unmapped page */
843 printf("cpuid = %d; ", PCPU_GET(cpuid));
844 printf("apic id = %02x\n", PCPU_GET(apic_id));
846 panic("double fault");
850 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
853 struct trapframe *frame;
856 int reg, regcnt, error;
859 frame = td->td_frame;
863 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
864 sa->code = frame->tf_rax;
866 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
867 sa->code = frame->tf_rdi;
871 if (p->p_sysent->sv_mask)
872 sa->code &= p->p_sysent->sv_mask;
874 if (sa->code >= p->p_sysent->sv_size)
875 sa->callp = &p->p_sysent->sv_table[0];
877 sa->callp = &p->p_sysent->sv_table[sa->code];
879 sa->narg = sa->callp->sy_narg;
880 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]),
881 ("Too many syscall arguments!"));
883 argp = &frame->tf_rdi;
885 bcopy(argp, sa->args, sizeof(sa->args[0]) * regcnt);
886 if (sa->narg > regcnt) {
887 KASSERT(params != NULL, ("copyin args with no params!"));
888 error = copyin(params, &sa->args[regcnt],
889 (sa->narg - regcnt) * sizeof(sa->args[0]));
893 td->td_retval[0] = 0;
894 td->td_retval[1] = frame->tf_rdx;
900 #include "../../kern/subr_syscall.c"
903 * syscall - system call request C handler
905 * A system call is essentially treated as a trap.
908 amd64_syscall(struct thread *td, int traced)
910 struct syscall_args sa;
915 if (ISPL(td->td_frame->tf_cs) != SEL_UPL) {
920 error = syscallenter(td, &sa);
925 if (__predict_false(traced)) {
926 td->td_frame->tf_rflags &= ~PSL_T;
927 ksiginfo_init_trap(&ksi);
928 ksi.ksi_signo = SIGTRAP;
929 ksi.ksi_code = TRAP_TRACE;
930 ksi.ksi_addr = (void *)td->td_frame->tf_rip;
931 trapsignal(td, &ksi);
934 KASSERT(PCB_USER_FPU(td->td_pcb),
935 ("System call %s returing with kernel FPU ctx leaked",
936 syscallname(td->td_proc, sa.code)));
937 KASSERT(td->td_pcb->pcb_save == &td->td_pcb->pcb_user_save,
938 ("System call %s returning with mangled pcb_save",
939 syscallname(td->td_proc, sa.code)));
941 syscallret(td, error, &sa);
944 * If the user-supplied value of %rip is not a canonical
945 * address, then some CPUs will trigger a ring 0 #GP during
946 * the sysret instruction. However, the fault handler would
947 * execute with the user's %gs and %rsp in ring 0 which would
948 * not be safe. Instead, preemptively kill the thread with a
951 if (td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS) {
952 ksiginfo_init_trap(&ksi);
953 ksi.ksi_signo = SIGBUS;
954 ksi.ksi_code = BUS_OBJERR;
955 ksi.ksi_trapno = T_PROTFLT;
956 ksi.ksi_addr = (void *)td->td_frame->tf_rip;
957 trapsignal(td, &ksi);