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 * 386 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"
57 #include <sys/param.h>
59 #include <sys/systm.h>
61 #include <sys/pioctl.h>
62 #include <sys/ptrace.h>
64 #include <sys/kernel.h>
67 #include <sys/mutex.h>
68 #include <sys/resourcevar.h>
69 #include <sys/signalvar.h>
70 #include <sys/syscall.h>
71 #include <sys/sysctl.h>
72 #include <sys/sysent.h>
74 #include <sys/vmmeter.h>
76 #include <sys/ktrace.h>
79 #include <sys/pmckern.h>
81 #include <security/audit/audit.h>
84 #include <vm/vm_param.h>
86 #include <vm/vm_kern.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_extern.h>
91 #include <machine/cpu.h>
92 #include <machine/intr_machdep.h>
93 #include <machine/mca.h>
94 #include <machine/md_var.h>
95 #include <machine/pcb.h>
97 #include <machine/smp.h>
99 #include <machine/tss.h>
100 #include <machine/vm86.h>
103 #include <sys/syslog.h>
104 #include <machine/clock.h>
108 #include <sys/dtrace_bsd.h>
111 * This is a hook which is initialised by the dtrace module
112 * to handle traps which might occur during DTrace probe
115 dtrace_trap_func_t dtrace_trap_func;
117 dtrace_doubletrap_func_t dtrace_doubletrap_func;
120 * This is a hook which is initialised by the systrace module
121 * when it is loaded. This keeps the DTrace syscall provider
122 * implementation opaque.
124 systrace_probe_func_t systrace_probe_func;
127 * These hooks are necessary for the pid, usdt and fasttrap providers.
129 dtrace_fasttrap_probe_ptr_t dtrace_fasttrap_probe_ptr;
130 dtrace_pid_probe_ptr_t dtrace_pid_probe_ptr;
131 dtrace_return_probe_ptr_t dtrace_return_probe_ptr;
134 extern void trap(struct trapframe *frame);
135 extern void syscall(struct trapframe *frame);
137 static int trap_pfault(struct trapframe *, int, vm_offset_t);
138 static void trap_fatal(struct trapframe *, vm_offset_t);
139 void dblfault_handler(void);
141 extern inthand_t IDTVEC(lcall_syscall);
143 #define MAX_TRAP_MSG 33
144 static char *trap_msg[] = {
146 "privileged instruction fault", /* 1 T_PRIVINFLT */
148 "breakpoint instruction fault", /* 3 T_BPTFLT */
151 "arithmetic trap", /* 6 T_ARITHTRAP */
154 "general protection fault", /* 9 T_PROTFLT */
155 "trace trap", /* 10 T_TRCTRAP */
157 "page fault", /* 12 T_PAGEFLT */
159 "alignment fault", /* 14 T_ALIGNFLT */
163 "integer divide fault", /* 18 T_DIVIDE */
164 "non-maskable interrupt trap", /* 19 T_NMI */
165 "overflow trap", /* 20 T_OFLOW */
166 "FPU bounds check fault", /* 21 T_BOUND */
167 "FPU device not available", /* 22 T_DNA */
168 "double fault", /* 23 T_DOUBLEFLT */
169 "FPU operand fetch fault", /* 24 T_FPOPFLT */
170 "invalid TSS fault", /* 25 T_TSSFLT */
171 "segment not present fault", /* 26 T_SEGNPFLT */
172 "stack fault", /* 27 T_STKFLT */
173 "machine check trap", /* 28 T_MCHK */
174 "SIMD floating-point exception", /* 29 T_XMMFLT */
175 "reserved (unknown) fault", /* 30 T_RESERVED */
176 "", /* 31 unused (reserved) */
177 "DTrace pid return trap", /* 32 T_DTRACE_RET */
178 "DTrace fasttrap probe trap", /* 33 T_DTRACE_PROBE */
182 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
183 extern int has_f00f_bug;
187 static int kdb_on_nmi = 1;
188 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
189 &kdb_on_nmi, 0, "Go to KDB on NMI");
190 TUNABLE_INT("machdep.kdb_on_nmi", &kdb_on_nmi);
192 static int panic_on_nmi = 1;
193 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
194 &panic_on_nmi, 0, "Panic on NMI");
195 TUNABLE_INT("machdep.panic_on_nmi", &panic_on_nmi);
196 static int prot_fault_translation = 0;
197 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
198 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
201 * Exception, fault, and trap interface to the FreeBSD kernel.
202 * This common code is called from assembly language IDT gate entry
203 * routines that prepare a suitable stack frame, and restore this
204 * frame after the exception has been processed.
208 trap(struct trapframe *frame)
210 struct thread *td = curthread;
211 struct proc *p = td->td_proc;
212 int i = 0, ucode = 0, code;
218 static int lastalert = 0;
221 PCPU_INC(cnt.v_trap);
222 type = frame->tf_trapno;
225 /* Handler for NMI IPIs used for stopping CPUs. */
227 if (ipi_nmi_handler() == 0)
239 if (type == T_RESERVED) {
240 trap_fatal(frame, 0);
246 * CPU PMCs interrupt using an NMI so we check for that first.
247 * If the HWPMC module is active, 'pmc_hook' will point to
248 * the function to be called. A return value of '1' from the
249 * hook means that the NMI was handled by it and that we can
250 * return immediately.
252 if (type == T_NMI && pmc_intr &&
253 (*pmc_intr)(PCPU_GET(cpuid), frame))
257 if (type == T_MCHK) {
264 * A trap can occur while DTrace executes a probe. Before
265 * executing the probe, DTrace blocks re-scheduling and sets
266 * a flag in it's per-cpu flags to indicate that it doesn't
267 * want to fault. On returning from the probe, the no-fault
268 * flag is cleared and finally re-scheduling is enabled.
270 * If the DTrace kernel module has registered a trap handler,
271 * call it and if it returns non-zero, assume that it has
272 * handled the trap and modified the trap frame so that this
273 * function can return normally.
275 if (type == T_DTRACE_PROBE || type == T_DTRACE_RET ||
279 fill_frame_regs(frame, ®s);
280 if (type == T_DTRACE_PROBE &&
281 dtrace_fasttrap_probe_ptr != NULL &&
282 dtrace_fasttrap_probe_ptr(®s) == 0)
284 if (type == T_BPTFLT &&
285 dtrace_pid_probe_ptr != NULL &&
286 dtrace_pid_probe_ptr(®s) == 0)
288 if (type == T_DTRACE_RET &&
289 dtrace_return_probe_ptr != NULL &&
290 dtrace_return_probe_ptr(®s) == 0)
293 if ((type == T_PROTFLT || type == T_PAGEFLT) &&
294 dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
298 if ((frame->tf_eflags & PSL_I) == 0) {
300 * Buggy application or kernel code has disabled
301 * interrupts and then trapped. Enabling interrupts
302 * now is wrong, but it is better than running with
303 * interrupts disabled until they are accidentally
306 if (ISPL(frame->tf_cs) == SEL_UPL || (frame->tf_eflags & PSL_VM))
308 "pid %ld (%s): trap %d with interrupts disabled\n",
309 (long)curproc->p_pid, curthread->td_name, type);
310 else if (type != T_BPTFLT && type != T_TRCTRAP &&
311 frame->tf_eip != (int)cpu_switch_load_gs) {
313 * XXX not quite right, since this may be for a
314 * multiple fault in user mode.
316 printf("kernel trap %d with interrupts disabled\n",
319 * Page faults need interrupts disabled until later,
320 * and we shouldn't enable interrupts while holding
321 * a spin lock or if servicing an NMI.
323 if (type != T_NMI && type != T_PAGEFLT &&
324 td->td_md.md_spinlock_count == 0)
329 code = frame->tf_err;
330 if (type == T_PAGEFLT) {
332 * For some Cyrix CPUs, %cr2 is clobbered by
333 * interrupts. This problem is worked around by using
334 * an interrupt gate for the pagefault handler. We
335 * are finally ready to read %cr2 and conditionally
336 * reenable interrupts. If we hold a spin lock, then
337 * we must not reenable interrupts. This might be a
338 * spurious page fault.
341 if (td->td_md.md_spinlock_count == 0)
345 if ((ISPL(frame->tf_cs) == SEL_UPL) ||
346 ((frame->tf_eflags & PSL_VM) &&
347 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL))) {
351 td->td_frame = frame;
352 addr = frame->tf_eip;
353 if (td->td_ucred != p->p_ucred)
354 cred_update_thread(td);
357 case T_PRIVINFLT: /* privileged instruction fault */
362 case T_BPTFLT: /* bpt instruction fault */
363 case T_TRCTRAP: /* trace trap */
365 frame->tf_eflags &= ~PSL_T;
367 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
370 case T_ARITHTRAP: /* arithmetic trap */
382 * The following two traps can happen in
383 * vm86 mode, and, if so, we want to handle
386 case T_PROTFLT: /* general protection fault */
387 case T_STKFLT: /* stack fault */
388 if (frame->tf_eflags & PSL_VM) {
389 i = vm86_emulate((struct vm86frame *)frame);
395 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
397 case T_SEGNPFLT: /* segment not present fault */
401 case T_TSSFLT: /* invalid TSS fault */
405 case T_DOUBLEFLT: /* double fault */
411 case T_PAGEFLT: /* page fault */
413 i = trap_pfault(frame, TRUE, eva);
414 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
417 * The f00f hack workaround has triggered, so
418 * treat the fault as an illegal instruction
419 * (T_PRIVINFLT) instead of a page fault.
421 type = frame->tf_trapno = T_PRIVINFLT;
423 /* Proceed as in that case. */
437 if (prot_fault_translation == 0) {
440 * This check also covers the images
441 * without the ABI-tag ELF note.
443 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
444 && p->p_osrel >= P_OSREL_SIGSEGV) {
449 ucode = BUS_PAGE_FAULT;
451 } else if (prot_fault_translation == 1) {
453 * Always compat mode.
456 ucode = BUS_PAGE_FAULT;
459 * Always SIGSEGV mode.
468 case T_DIVIDE: /* integer divide fault */
477 # define TIMER_FREQ 1193182
479 if (time_second - lastalert > 10) {
480 log(LOG_WARNING, "NMI: power fail\n");
482 lastalert = time_second;
485 #else /* !POWERFAIL_NMI */
486 /* machine/parity/power fail/"kitchen sink" faults */
487 if (isa_nmi(code) == 0) {
490 * NMI can be hooked up to a pushbutton
494 printf ("NMI ... going to debugger\n");
495 kdb_trap(type, 0, frame);
499 } else if (panic_on_nmi)
500 panic("NMI indicates hardware failure");
502 #endif /* POWERFAIL_NMI */
505 case T_OFLOW: /* integer overflow fault */
510 case T_BOUND: /* bounds check fault */
517 KASSERT(PCB_USER_FPU(td->td_pcb),
518 ("kernel FPU ctx has leaked"));
519 /* transparent fault (due to context switch "late") */
523 uprintf("pid %d killed due to lack of floating point\n",
529 case T_FPOPFLT: /* FPU operand fetch fault */
534 case T_XMMFLT: /* SIMD floating-point exception */
542 KASSERT(cold || td->td_ucred != NULL,
543 ("kernel trap doesn't have ucred"));
545 case T_PAGEFLT: /* page fault */
546 (void) trap_pfault(frame, FALSE, eva);
551 KASSERT(!PCB_USER_FPU(td->td_pcb),
552 ("Unregistered use of FPU in kernel"));
558 case T_ARITHTRAP: /* arithmetic trap */
559 case T_XMMFLT: /* SIMD floating-point exception */
560 case T_FPOPFLT: /* FPU operand fetch fault */
562 * XXXKIB for now disable any FPU traps in kernel
563 * handler registration seems to be overkill
565 trap_fatal(frame, 0);
569 * The following two traps can happen in
570 * vm86 mode, and, if so, we want to handle
573 case T_PROTFLT: /* general protection fault */
574 case T_STKFLT: /* stack fault */
575 if (frame->tf_eflags & PSL_VM) {
576 i = vm86_emulate((struct vm86frame *)frame);
579 * returns to original process
581 vm86_trap((struct vm86frame *)frame);
584 if (type == T_STKFLT)
589 case T_SEGNPFLT: /* segment not present fault */
590 if (PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)
594 * Invalid %fs's and %gs's can be created using
595 * procfs or PT_SETREGS or by invalidating the
596 * underlying LDT entry. This causes a fault
597 * in kernel mode when the kernel attempts to
598 * switch contexts. Lose the bad context
599 * (XXX) so that we can continue, and generate
602 if (frame->tf_eip == (int)cpu_switch_load_gs) {
603 PCPU_GET(curpcb)->pcb_gs = 0;
612 if (td->td_intr_nesting_level != 0)
616 * Invalid segment selectors and out of bounds
617 * %eip's and %esp's can be set up in user mode.
618 * This causes a fault in kernel mode when the
619 * kernel tries to return to user mode. We want
620 * to get this fault so that we can fix the
621 * problem here and not have to check all the
622 * selectors and pointers when the user changes
625 if (frame->tf_eip == (int)doreti_iret) {
626 frame->tf_eip = (int)doreti_iret_fault;
629 if (frame->tf_eip == (int)doreti_popl_ds) {
630 frame->tf_eip = (int)doreti_popl_ds_fault;
633 if (frame->tf_eip == (int)doreti_popl_es) {
634 frame->tf_eip = (int)doreti_popl_es_fault;
637 if (frame->tf_eip == (int)doreti_popl_fs) {
638 frame->tf_eip = (int)doreti_popl_fs_fault;
641 if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
643 (int)PCPU_GET(curpcb)->pcb_onfault;
650 * PSL_NT can be set in user mode and isn't cleared
651 * automatically when the kernel is entered. This
652 * causes a TSS fault when the kernel attempts to
653 * `iret' because the TSS link is uninitialized. We
654 * want to get this fault so that we can fix the
655 * problem here and not every time the kernel is
658 if (frame->tf_eflags & PSL_NT) {
659 frame->tf_eflags &= ~PSL_NT;
664 case T_TRCTRAP: /* trace trap */
665 if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
667 * We've just entered system mode via the
668 * syscall lcall. Continue single stepping
669 * silently until the syscall handler has
674 if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
676 * The syscall handler has now saved the
677 * flags. Stop single stepping it.
679 frame->tf_eflags &= ~PSL_T;
683 * Ignore debug register trace traps due to
684 * accesses in the user's address space, which
685 * can happen under several conditions such as
686 * if a user sets a watchpoint on a buffer and
687 * then passes that buffer to a system call.
688 * We still want to get TRCTRAPS for addresses
689 * in kernel space because that is useful when
690 * debugging the kernel.
692 if (user_dbreg_trap() &&
693 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)) {
695 * Reset breakpoint bits because the
698 load_dr6(rdr6() & 0xfffffff0);
702 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
706 * If KDB is enabled, let it handle the debugger trap.
707 * Otherwise, debugger traps "can't happen".
710 if (kdb_trap(type, 0, frame))
718 if (time_second - lastalert > 10) {
719 log(LOG_WARNING, "NMI: power fail\n");
721 lastalert = time_second;
724 #else /* !POWERFAIL_NMI */
725 /* machine/parity/power fail/"kitchen sink" faults */
726 if (isa_nmi(code) == 0) {
729 * NMI can be hooked up to a pushbutton
733 printf ("NMI ... going to debugger\n");
734 kdb_trap(type, 0, frame);
738 } else if (panic_on_nmi == 0)
741 #endif /* POWERFAIL_NMI */
745 trap_fatal(frame, eva);
749 /* Translate fault for emulators (e.g. Linux) */
750 if (*p->p_sysent->sv_transtrap)
751 i = (*p->p_sysent->sv_transtrap)(i, type);
753 ksiginfo_init_trap(&ksi);
755 ksi.ksi_code = ucode;
756 ksi.ksi_addr = (void *)addr;
757 ksi.ksi_trapno = type;
758 KASSERT((read_eflags() & PSL_I) != 0, ("interrupts disabled"));
759 trapsignal(td, &ksi);
762 if (type <= MAX_TRAP_MSG) {
763 uprintf("fatal process exception: %s",
765 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
766 uprintf(", fault VA = 0x%lx", (u_long)eva);
773 mtx_assert(&Giant, MA_NOTOWNED);
774 KASSERT(PCB_USER_FPU(td->td_pcb),
775 ("Return from trap with kernel FPU ctx leaked"));
782 trap_pfault(frame, usermode, eva)
783 struct trapframe *frame;
788 struct vmspace *vm = NULL;
792 struct thread *td = curthread;
793 struct proc *p = td->td_proc;
795 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
797 * Due to both processor errata and lazy TLB invalidation when
798 * access restrictions are removed from virtual pages, memory
799 * accesses that are allowed by the physical mapping layer may
800 * nonetheless cause one spurious page fault per virtual page.
801 * When the thread is executing a "no faulting" section that
802 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
803 * every page fault is treated as a spurious page fault,
804 * unless it accesses the same virtual address as the most
805 * recent page fault within the same "no faulting" section.
807 if (td->td_md.md_spurflt_addr != eva ||
808 (td->td_pflags & TDP_RESETSPUR) != 0) {
810 * Do nothing to the TLB. A stale TLB entry is
811 * flushed automatically by a page fault.
813 td->td_md.md_spurflt_addr = eva;
814 td->td_pflags &= ~TDP_RESETSPUR;
819 * If we get a page fault while in a critical section, then
820 * it is most likely a fatal kernel page fault. The kernel
821 * is already going to panic trying to get a sleep lock to
822 * do the VM lookup, so just consider it a fatal trap so the
823 * kernel can print out a useful trap message and even get
826 * If we get a page fault while holding a non-sleepable
827 * lock, then it is most likely a fatal kernel page fault.
828 * If WITNESS is enabled, then it's going to whine about
829 * bogus LORs with various VM locks, so just skip to the
830 * fatal trap handling directly.
832 if (td->td_critnest != 0 ||
833 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
834 "Kernel page fault") != 0) {
835 trap_fatal(frame, eva);
839 va = trunc_page(eva);
840 if (va >= KERNBASE) {
842 * Don't allow user-mode faults in kernel address space.
843 * An exception: if the faulting address is the invalid
844 * instruction entry in the IDT, then the Intel Pentium
845 * F00F bug workaround was triggered, and we need to
846 * treat it is as an illegal instruction, and not a page
849 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
850 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
859 * This is a fault on non-kernel virtual memory.
860 * vm is initialized above to NULL. If curproc is NULL
861 * or curproc->p_vmspace is NULL the fault is fatal.
870 if (!usermode && (td->td_intr_nesting_level != 0 ||
871 PCPU_GET(curpcb)->pcb_onfault == NULL)) {
872 trap_fatal(frame, eva);
878 * PGEX_I is defined only if the execute disable bit capability is
879 * supported and enabled.
881 if (frame->tf_err & PGEX_W)
882 ftype = VM_PROT_WRITE;
884 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
885 ftype = VM_PROT_EXECUTE;
888 ftype = VM_PROT_READ;
890 if (map != kernel_map) {
892 * Keep swapout from messing with us during this
899 /* Fault in the user page: */
900 rv = vm_fault(map, va, ftype,
901 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
909 * Don't have to worry about process locking or stacks in the
912 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
914 if (rv == KERN_SUCCESS)
918 if (td->td_intr_nesting_level == 0 &&
919 PCPU_GET(curpcb)->pcb_onfault != NULL) {
920 frame->tf_eip = (int)PCPU_GET(curpcb)->pcb_onfault;
923 trap_fatal(frame, eva);
927 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
931 trap_fatal(frame, eva)
932 struct trapframe *frame;
937 struct soft_segment_descriptor softseg;
940 code = frame->tf_err;
941 type = frame->tf_trapno;
942 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
944 if (type <= MAX_TRAP_MSG)
945 msg = trap_msg[type];
948 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
949 frame->tf_eflags & PSL_VM ? "vm86" :
950 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
952 /* two separate prints in case of a trap on an unmapped page */
953 printf("cpuid = %d; ", PCPU_GET(cpuid));
954 printf("apic id = %02x\n", PCPU_GET(apic_id));
956 if (type == T_PAGEFLT) {
957 printf("fault virtual address = 0x%x\n", eva);
958 printf("fault code = %s %s, %s\n",
959 code & PGEX_U ? "user" : "supervisor",
960 code & PGEX_W ? "write" : "read",
961 code & PGEX_P ? "protection violation" : "page not present");
963 printf("instruction pointer = 0x%x:0x%x\n",
964 frame->tf_cs & 0xffff, frame->tf_eip);
965 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
966 ss = frame->tf_ss & 0xffff;
969 ss = GSEL(GDATA_SEL, SEL_KPL);
970 esp = (int)&frame->tf_esp;
972 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
973 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
974 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
975 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
976 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
977 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
979 printf("processor eflags = ");
980 if (frame->tf_eflags & PSL_T)
981 printf("trace trap, ");
982 if (frame->tf_eflags & PSL_I)
983 printf("interrupt enabled, ");
984 if (frame->tf_eflags & PSL_NT)
985 printf("nested task, ");
986 if (frame->tf_eflags & PSL_RF)
988 if (frame->tf_eflags & PSL_VM)
990 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
991 printf("current process = ");
993 printf("%lu (%s)\n", (u_long)curproc->p_pid, curthread->td_name);
999 if (debugger_on_panic || kdb_active) {
1000 frame->tf_err = eva; /* smuggle fault address to ddb */
1001 if (kdb_trap(type, 0, frame)) {
1002 frame->tf_err = code; /* restore error code */
1005 frame->tf_err = code; /* restore error code */
1008 printf("trap number = %d\n", type);
1009 if (type <= MAX_TRAP_MSG)
1010 panic("%s", trap_msg[type]);
1012 panic("unknown/reserved trap");
1016 * Double fault handler. Called when a fault occurs while writing
1017 * a frame for a trap/exception onto the stack. This usually occurs
1018 * when the stack overflows (such is the case with infinite recursion,
1021 * XXX Note that the current PTD gets replaced by IdlePTD when the
1022 * task switch occurs. This means that the stack that was active at
1023 * the time of the double fault is not available at <kstack> unless
1024 * the machine was idle when the double fault occurred. The downside
1025 * of this is that "trace <ebp>" in ddb won't work.
1030 #ifdef KDTRACE_HOOKS
1031 if (dtrace_doubletrap_func != NULL)
1032 (*dtrace_doubletrap_func)();
1034 printf("\nFatal double fault:\n");
1035 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
1036 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
1037 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
1039 /* two separate prints in case of a trap on an unmapped page */
1040 printf("cpuid = %d; ", PCPU_GET(cpuid));
1041 printf("apic id = %02x\n", PCPU_GET(apic_id));
1043 panic("double fault");
1047 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
1050 struct trapframe *frame;
1055 frame = td->td_frame;
1057 params = (caddr_t)frame->tf_esp + sizeof(int);
1058 sa->code = frame->tf_eax;
1061 * Need to check if this is a 32 bit or 64 bit syscall.
1063 if (sa->code == SYS_syscall) {
1065 * Code is first argument, followed by actual args.
1067 sa->code = fuword(params);
1068 params += sizeof(int);
1069 } else if (sa->code == SYS___syscall) {
1071 * Like syscall, but code is a quad, so as to maintain
1072 * quad alignment for the rest of the arguments.
1074 sa->code = fuword(params);
1075 params += sizeof(quad_t);
1078 if (p->p_sysent->sv_mask)
1079 sa->code &= p->p_sysent->sv_mask;
1080 if (sa->code >= p->p_sysent->sv_size)
1081 sa->callp = &p->p_sysent->sv_table[0];
1083 sa->callp = &p->p_sysent->sv_table[sa->code];
1084 sa->narg = sa->callp->sy_narg;
1086 if (params != NULL && sa->narg != 0)
1087 error = copyin(params, (caddr_t)sa->args,
1088 (u_int)(sa->narg * sizeof(int)));
1093 td->td_retval[0] = 0;
1094 td->td_retval[1] = frame->tf_edx;
1100 #include "../../kern/subr_syscall.c"
1103 * syscall - system call request C handler
1105 * A system call is essentially treated as a trap.
1108 syscall(struct trapframe *frame)
1111 struct syscall_args sa;
1112 register_t orig_tf_eflags;
1117 if (ISPL(frame->tf_cs) != SEL_UPL) {
1122 orig_tf_eflags = frame->tf_eflags;
1125 td->td_frame = frame;
1127 error = syscallenter(td, &sa);
1132 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1133 frame->tf_eflags &= ~PSL_T;
1134 ksiginfo_init_trap(&ksi);
1135 ksi.ksi_signo = SIGTRAP;
1136 ksi.ksi_code = TRAP_TRACE;
1137 ksi.ksi_addr = (void *)frame->tf_eip;
1138 trapsignal(td, &ksi);
1141 KASSERT(PCB_USER_FPU(td->td_pcb),
1142 ("System call %s returning with kernel FPU ctx leaked",
1143 syscallname(td->td_proc, sa.code)));
1144 KASSERT(td->td_pcb->pcb_save == &td->td_pcb->pcb_user_save,
1145 ("System call %s returning with mangled pcb_save",
1146 syscallname(td->td_proc, sa.code)));
1148 syscallret(td, error, &sa);