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"
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/pmckern.h>
75 PMC_SOFT_DEFINE( , , page_fault, all);
76 PMC_SOFT_DEFINE( , , page_fault, read);
77 PMC_SOFT_DEFINE( , , page_fault, write);
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>
92 #include <machine/md_var.h>
93 #include <machine/pcb.h>
95 #include <machine/smp.h>
97 #include <machine/tss.h>
98 #include <machine/vm86.h>
101 #include <sys/syslog.h>
102 #include <machine/clock.h>
106 #include <sys/dtrace_bsd.h>
109 extern void trap(struct trapframe *frame);
110 extern void syscall(struct trapframe *frame);
112 static int trap_pfault(struct trapframe *, int, vm_offset_t);
113 static void trap_fatal(struct trapframe *, vm_offset_t);
114 void dblfault_handler(void);
116 extern inthand_t IDTVEC(lcall_syscall);
118 #define MAX_TRAP_MSG 32
119 static char *trap_msg[] = {
121 "privileged instruction fault", /* 1 T_PRIVINFLT */
123 "breakpoint instruction fault", /* 3 T_BPTFLT */
126 "arithmetic trap", /* 6 T_ARITHTRAP */
129 "general protection fault", /* 9 T_PROTFLT */
130 "trace trap", /* 10 T_TRCTRAP */
132 "page fault", /* 12 T_PAGEFLT */
134 "alignment fault", /* 14 T_ALIGNFLT */
138 "integer divide fault", /* 18 T_DIVIDE */
139 "non-maskable interrupt trap", /* 19 T_NMI */
140 "overflow trap", /* 20 T_OFLOW */
141 "FPU bounds check fault", /* 21 T_BOUND */
142 "FPU device not available", /* 22 T_DNA */
143 "double fault", /* 23 T_DOUBLEFLT */
144 "FPU operand fetch fault", /* 24 T_FPOPFLT */
145 "invalid TSS fault", /* 25 T_TSSFLT */
146 "segment not present fault", /* 26 T_SEGNPFLT */
147 "stack fault", /* 27 T_STKFLT */
148 "machine check trap", /* 28 T_MCHK */
149 "SIMD floating-point exception", /* 29 T_XMMFLT */
150 "reserved (unknown) fault", /* 30 T_RESERVED */
151 "", /* 31 unused (reserved) */
152 "DTrace pid return trap", /* 32 T_DTRACE_RET */
155 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
156 extern int has_f00f_bug;
160 static int kdb_on_nmi = 1;
161 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RWTUN,
162 &kdb_on_nmi, 0, "Go to KDB on NMI");
164 static int panic_on_nmi = 1;
165 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RWTUN,
166 &panic_on_nmi, 0, "Panic on NMI");
167 static int prot_fault_translation = 0;
168 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
169 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
170 static int uprintf_signal;
171 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RW,
173 "Print debugging information on trap signal to ctty");
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)
188 struct thread *td = curthread;
189 struct proc *p = td->td_proc;
190 int i = 0, ucode = 0, code;
196 static int lastalert = 0;
199 PCPU_INC(cnt.v_trap);
200 type = frame->tf_trapno;
203 /* Handler for NMI IPIs used for stopping CPUs. */
205 if (ipi_nmi_handler() == 0)
217 if (type == T_RESERVED) {
218 trap_fatal(frame, 0);
224 * CPU PMCs interrupt using an NMI so we check for that first.
225 * If the HWPMC module is active, 'pmc_hook' will point to
226 * the function to be called. A return value of '1' from the
227 * hook means that the NMI was handled by it and that we can
228 * return immediately.
230 if (type == T_NMI && pmc_intr &&
231 (*pmc_intr)(PCPU_GET(cpuid), frame))
235 if (type == T_MCHK) {
242 * A trap can occur while DTrace executes a probe. Before
243 * executing the probe, DTrace blocks re-scheduling and sets
244 * a flag in its per-cpu flags to indicate that it doesn't
245 * want to fault. On returning from the probe, the no-fault
246 * flag is cleared and finally re-scheduling is enabled.
248 if ((type == T_PROTFLT || type == T_PAGEFLT) &&
249 dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
253 if ((frame->tf_eflags & PSL_I) == 0) {
255 * Buggy application or kernel code has disabled
256 * interrupts and then trapped. Enabling interrupts
257 * now is wrong, but it is better than running with
258 * interrupts disabled until they are accidentally
261 if (ISPL(frame->tf_cs) == SEL_UPL || (frame->tf_eflags & PSL_VM))
263 "pid %ld (%s): trap %d with interrupts disabled\n",
264 (long)curproc->p_pid, curthread->td_name, type);
265 else if (type != T_NMI && type != T_BPTFLT &&
267 frame->tf_eip != (int)cpu_switch_load_gs) {
269 * XXX not quite right, since this may be for a
270 * multiple fault in user mode.
272 printf("kernel trap %d with interrupts disabled\n",
275 * Page faults need interrupts disabled until later,
276 * and we shouldn't enable interrupts while holding
279 if (type != T_PAGEFLT &&
280 td->td_md.md_spinlock_count == 0)
285 code = frame->tf_err;
286 if (type == T_PAGEFLT) {
288 * For some Cyrix CPUs, %cr2 is clobbered by
289 * interrupts. This problem is worked around by using
290 * an interrupt gate for the pagefault handler. We
291 * are finally ready to read %cr2 and conditionally
292 * reenable interrupts. If we hold a spin lock, then
293 * we must not reenable interrupts. This might be a
294 * spurious page fault.
297 if (td->td_md.md_spinlock_count == 0)
301 if ((ISPL(frame->tf_cs) == SEL_UPL) ||
302 ((frame->tf_eflags & PSL_VM) &&
303 !(curpcb->pcb_flags & PCB_VM86CALL))) {
307 td->td_frame = frame;
308 addr = frame->tf_eip;
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 */
322 if (type == T_BPTFLT) {
323 fill_frame_regs(frame, ®s);
324 if (dtrace_pid_probe_ptr != NULL &&
325 dtrace_pid_probe_ptr(®s) == 0)
329 frame->tf_eflags &= ~PSL_T;
331 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
334 case T_ARITHTRAP: /* arithmetic trap */
336 ucode = npxtrap_x87();
346 * The following two traps can happen in
347 * vm86 mode, and, if so, we want to handle
350 case T_PROTFLT: /* general protection fault */
351 case T_STKFLT: /* stack fault */
352 if (frame->tf_eflags & PSL_VM) {
353 i = vm86_emulate((struct vm86frame *)frame);
359 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
361 case T_SEGNPFLT: /* segment not present fault */
365 case T_TSSFLT: /* invalid TSS fault */
369 case T_DOUBLEFLT: /* double fault */
375 case T_PAGEFLT: /* page fault */
377 i = trap_pfault(frame, TRUE, eva);
378 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
381 * The f00f hack workaround has triggered, so
382 * treat the fault as an illegal instruction
383 * (T_PRIVINFLT) instead of a page fault.
385 type = frame->tf_trapno = T_PRIVINFLT;
387 /* Proceed as in that case. */
401 if (prot_fault_translation == 0) {
404 * This check also covers the images
405 * without the ABI-tag ELF note.
407 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
408 && p->p_osrel >= P_OSREL_SIGSEGV) {
413 ucode = BUS_PAGE_FAULT;
415 } else if (prot_fault_translation == 1) {
417 * Always compat mode.
420 ucode = BUS_PAGE_FAULT;
423 * Always SIGSEGV mode.
432 case T_DIVIDE: /* integer divide fault */
441 # define TIMER_FREQ 1193182
443 if (time_second - lastalert > 10) {
444 log(LOG_WARNING, "NMI: power fail\n");
446 lastalert = time_second;
449 #else /* !POWERFAIL_NMI */
450 /* machine/parity/power fail/"kitchen sink" faults */
451 if (isa_nmi(code) == 0) {
454 * NMI can be hooked up to a pushbutton
458 printf ("NMI ... going to debugger\n");
459 kdb_trap(type, 0, frame);
463 } else if (panic_on_nmi)
464 panic("NMI indicates hardware failure");
466 #endif /* POWERFAIL_NMI */
469 case T_OFLOW: /* integer overflow fault */
474 case T_BOUND: /* bounds check fault */
481 KASSERT(PCB_USER_FPU(td->td_pcb),
482 ("kernel FPU ctx has leaked"));
483 /* transparent fault (due to context switch "late") */
487 uprintf("pid %d killed due to lack of floating point\n",
493 case T_FPOPFLT: /* FPU operand fetch fault */
498 case T_XMMFLT: /* SIMD floating-point exception */
499 #if defined(DEV_NPX) && !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
500 ucode = npxtrap_sse();
511 fill_frame_regs(frame, ®s);
512 if (dtrace_return_probe_ptr != NULL &&
513 dtrace_return_probe_ptr(®s) == 0)
521 KASSERT(cold || td->td_ucred != NULL,
522 ("kernel trap doesn't have ucred"));
524 case T_PAGEFLT: /* page fault */
525 (void) trap_pfault(frame, FALSE, eva);
530 KASSERT(!PCB_USER_FPU(td->td_pcb),
531 ("Unregistered use of FPU in kernel"));
537 case T_ARITHTRAP: /* arithmetic trap */
538 case T_XMMFLT: /* SIMD floating-point exception */
539 case T_FPOPFLT: /* FPU operand fetch fault */
541 * XXXKIB for now disable any FPU traps in kernel
542 * handler registration seems to be overkill
544 trap_fatal(frame, 0);
548 * The following two traps can happen in
549 * vm86 mode, and, if so, we want to handle
552 case T_PROTFLT: /* general protection fault */
553 case T_STKFLT: /* stack fault */
554 if (frame->tf_eflags & PSL_VM) {
555 i = vm86_emulate((struct vm86frame *)frame);
558 * returns to original process
560 vm86_trap((struct vm86frame *)frame);
563 if (type == T_STKFLT)
568 case T_SEGNPFLT: /* segment not present fault */
569 if (curpcb->pcb_flags & PCB_VM86CALL)
573 * Invalid %fs's and %gs's can be created using
574 * procfs or PT_SETREGS or by invalidating the
575 * underlying LDT entry. This causes a fault
576 * in kernel mode when the kernel attempts to
577 * switch contexts. Lose the bad context
578 * (XXX) so that we can continue, and generate
581 if (frame->tf_eip == (int)cpu_switch_load_gs) {
585 kern_psignal(p, SIGBUS);
591 if (td->td_intr_nesting_level != 0)
595 * Invalid segment selectors and out of bounds
596 * %eip's and %esp's can be set up in user mode.
597 * This causes a fault in kernel mode when the
598 * kernel tries to return to user mode. We want
599 * to get this fault so that we can fix the
600 * problem here and not have to check all the
601 * selectors and pointers when the user changes
604 if (frame->tf_eip == (int)doreti_iret) {
605 frame->tf_eip = (int)doreti_iret_fault;
608 if (frame->tf_eip == (int)doreti_popl_ds) {
609 frame->tf_eip = (int)doreti_popl_ds_fault;
612 if (frame->tf_eip == (int)doreti_popl_es) {
613 frame->tf_eip = (int)doreti_popl_es_fault;
616 if (frame->tf_eip == (int)doreti_popl_fs) {
617 frame->tf_eip = (int)doreti_popl_fs_fault;
620 if (curpcb->pcb_onfault != NULL) {
622 (int)curpcb->pcb_onfault;
629 * PSL_NT can be set in user mode and isn't cleared
630 * automatically when the kernel is entered. This
631 * causes a TSS fault when the kernel attempts to
632 * `iret' because the TSS link is uninitialized. We
633 * want to get this fault so that we can fix the
634 * problem here and not every time the kernel is
637 if (frame->tf_eflags & PSL_NT) {
638 frame->tf_eflags &= ~PSL_NT;
643 case T_TRCTRAP: /* trace trap */
644 if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
646 * We've just entered system mode via the
647 * syscall lcall. Continue single stepping
648 * silently until the syscall handler has
653 if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
655 * The syscall handler has now saved the
656 * flags. Stop single stepping it.
658 frame->tf_eflags &= ~PSL_T;
662 * Ignore debug register trace traps due to
663 * accesses in the user's address space, which
664 * can happen under several conditions such as
665 * if a user sets a watchpoint on a buffer and
666 * then passes that buffer to a system call.
667 * We still want to get TRCTRAPS for addresses
668 * in kernel space because that is useful when
669 * debugging the kernel.
671 if (user_dbreg_trap() &&
672 !(curpcb->pcb_flags & PCB_VM86CALL)) {
674 * Reset breakpoint bits because the
677 load_dr6(rdr6() & 0xfffffff0);
681 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
685 * If KDB is enabled, let it handle the debugger trap.
686 * Otherwise, debugger traps "can't happen".
689 if (kdb_trap(type, 0, frame))
697 if (time_second - lastalert > 10) {
698 log(LOG_WARNING, "NMI: power fail\n");
700 lastalert = time_second;
703 #else /* !POWERFAIL_NMI */
704 /* machine/parity/power fail/"kitchen sink" faults */
705 if (isa_nmi(code) == 0) {
708 * NMI can be hooked up to a pushbutton
712 printf ("NMI ... going to debugger\n");
713 kdb_trap(type, 0, frame);
717 } else if (panic_on_nmi == 0)
720 #endif /* POWERFAIL_NMI */
724 trap_fatal(frame, eva);
728 /* Translate fault for emulators (e.g. Linux) */
729 if (*p->p_sysent->sv_transtrap)
730 i = (*p->p_sysent->sv_transtrap)(i, type);
732 ksiginfo_init_trap(&ksi);
734 ksi.ksi_code = ucode;
735 ksi.ksi_addr = (void *)addr;
736 ksi.ksi_trapno = type;
737 if (uprintf_signal) {
738 uprintf("pid %d comm %s: signal %d err %x code %d type %d "
739 "addr 0x%x esp 0x%08x eip 0x%08x "
740 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
741 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr,
742 frame->tf_esp, frame->tf_eip,
743 fubyte((void *)(frame->tf_eip + 0)),
744 fubyte((void *)(frame->tf_eip + 1)),
745 fubyte((void *)(frame->tf_eip + 2)),
746 fubyte((void *)(frame->tf_eip + 3)),
747 fubyte((void *)(frame->tf_eip + 4)),
748 fubyte((void *)(frame->tf_eip + 5)),
749 fubyte((void *)(frame->tf_eip + 6)),
750 fubyte((void *)(frame->tf_eip + 7)));
752 KASSERT((read_eflags() & PSL_I) != 0, ("interrupts disabled"));
753 trapsignal(td, &ksi);
756 if (type <= MAX_TRAP_MSG) {
757 uprintf("fatal process exception: %s",
759 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
760 uprintf(", fault VA = 0x%lx", (u_long)eva);
767 KASSERT(PCB_USER_FPU(td->td_pcb),
768 ("Return from trap with kernel FPU ctx leaked"));
775 trap_pfault(frame, usermode, eva)
776 struct trapframe *frame;
785 struct thread *td = curthread;
786 struct proc *p = td->td_proc;
788 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
790 * Due to both processor errata and lazy TLB invalidation when
791 * access restrictions are removed from virtual pages, memory
792 * accesses that are allowed by the physical mapping layer may
793 * nonetheless cause one spurious page fault per virtual page.
794 * When the thread is executing a "no faulting" section that
795 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
796 * every page fault is treated as a spurious page fault,
797 * unless it accesses the same virtual address as the most
798 * recent page fault within the same "no faulting" section.
800 if (td->td_md.md_spurflt_addr != eva ||
801 (td->td_pflags & TDP_RESETSPUR) != 0) {
803 * Do nothing to the TLB. A stale TLB entry is
804 * flushed automatically by a page fault.
806 td->td_md.md_spurflt_addr = eva;
807 td->td_pflags &= ~TDP_RESETSPUR;
812 * If we get a page fault while in a critical section, then
813 * it is most likely a fatal kernel page fault. The kernel
814 * is already going to panic trying to get a sleep lock to
815 * do the VM lookup, so just consider it a fatal trap so the
816 * kernel can print out a useful trap message and even get
819 * If we get a page fault while holding a non-sleepable
820 * lock, then it is most likely a fatal kernel page fault.
821 * If WITNESS is enabled, then it's going to whine about
822 * bogus LORs with various VM locks, so just skip to the
823 * fatal trap handling directly.
825 if (td->td_critnest != 0 ||
826 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
827 "Kernel page fault") != 0) {
828 trap_fatal(frame, eva);
832 va = trunc_page(eva);
833 if (va >= KERNBASE) {
835 * Don't allow user-mode faults in kernel address space.
836 * An exception: if the faulting address is the invalid
837 * instruction entry in the IDT, then the Intel Pentium
838 * F00F bug workaround was triggered, and we need to
839 * treat it is as an illegal instruction, and not a page
842 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
843 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
852 * This is a fault on non-kernel virtual memory. If either
853 * p or p->p_vmspace is NULL, then the fault is fatal.
855 if (p == NULL || (vm = p->p_vmspace) == NULL)
861 * When accessing a user-space address, kernel must be
862 * ready to accept the page fault, and provide a
863 * handling routine. Since accessing the address
864 * without the handler is a bug, do not try to handle
865 * it normally, and panic immediately.
867 if (!usermode && (td->td_intr_nesting_level != 0 ||
868 curpcb->pcb_onfault == NULL)) {
869 trap_fatal(frame, eva);
875 * PGEX_I is defined only if the execute disable bit capability is
876 * supported and enabled.
878 if (frame->tf_err & PGEX_W)
879 ftype = VM_PROT_WRITE;
881 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
882 ftype = VM_PROT_EXECUTE;
885 ftype = VM_PROT_READ;
887 if (map != kernel_map) {
889 * Keep swapout from messing with us during this
896 /* Fault in the user page: */
897 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
904 * Don't have to worry about process locking or stacks in the
907 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
909 if (rv == KERN_SUCCESS) {
911 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
912 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
913 if (ftype == VM_PROT_READ)
914 PMC_SOFT_CALL_TF( , , page_fault, read,
917 PMC_SOFT_CALL_TF( , , page_fault, write,
925 if (td->td_intr_nesting_level == 0 &&
926 curpcb->pcb_onfault != NULL) {
927 frame->tf_eip = (int)curpcb->pcb_onfault;
930 trap_fatal(frame, eva);
933 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
937 trap_fatal(frame, eva)
938 struct trapframe *frame;
943 struct soft_segment_descriptor softseg;
946 code = frame->tf_err;
947 type = frame->tf_trapno;
948 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
950 if (type <= MAX_TRAP_MSG)
951 msg = trap_msg[type];
954 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
955 frame->tf_eflags & PSL_VM ? "vm86" :
956 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
958 /* two separate prints in case of a trap on an unmapped page */
959 printf("cpuid = %d; ", PCPU_GET(cpuid));
960 printf("apic id = %02x\n", PCPU_GET(apic_id));
962 if (type == T_PAGEFLT) {
963 printf("fault virtual address = 0x%x\n", eva);
964 printf("fault code = %s %s, %s\n",
965 code & PGEX_U ? "user" : "supervisor",
966 code & PGEX_W ? "write" : "read",
967 code & PGEX_P ? "protection violation" : "page not present");
969 printf("instruction pointer = 0x%x:0x%x\n",
970 frame->tf_cs & 0xffff, frame->tf_eip);
971 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
972 ss = frame->tf_ss & 0xffff;
975 ss = GSEL(GDATA_SEL, SEL_KPL);
976 esp = (int)&frame->tf_esp;
978 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
979 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
980 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
981 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
982 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
983 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
985 printf("processor eflags = ");
986 if (frame->tf_eflags & PSL_T)
987 printf("trace trap, ");
988 if (frame->tf_eflags & PSL_I)
989 printf("interrupt enabled, ");
990 if (frame->tf_eflags & PSL_NT)
991 printf("nested task, ");
992 if (frame->tf_eflags & PSL_RF)
994 if (frame->tf_eflags & PSL_VM)
996 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
997 printf("current process = ");
999 printf("%lu (%s)\n", (u_long)curproc->p_pid, curthread->td_name);
1005 if (debugger_on_panic || kdb_active) {
1006 frame->tf_err = eva; /* smuggle fault address to ddb */
1007 if (kdb_trap(type, 0, frame)) {
1008 frame->tf_err = code; /* restore error code */
1011 frame->tf_err = code; /* restore error code */
1014 printf("trap number = %d\n", type);
1015 if (type <= MAX_TRAP_MSG)
1016 panic("%s", trap_msg[type]);
1018 panic("unknown/reserved trap");
1022 * Double fault handler. Called when a fault occurs while writing
1023 * a frame for a trap/exception onto the stack. This usually occurs
1024 * when the stack overflows (such is the case with infinite recursion,
1027 * XXX Note that the current PTD gets replaced by IdlePTD when the
1028 * task switch occurs. This means that the stack that was active at
1029 * the time of the double fault is not available at <kstack> unless
1030 * the machine was idle when the double fault occurred. The downside
1031 * of this is that "trace <ebp>" in ddb won't work.
1036 #ifdef KDTRACE_HOOKS
1037 if (dtrace_doubletrap_func != NULL)
1038 (*dtrace_doubletrap_func)();
1040 printf("\nFatal double fault:\n");
1041 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
1042 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
1043 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
1045 /* two separate prints in case of a trap on an unmapped page */
1046 printf("cpuid = %d; ", PCPU_GET(cpuid));
1047 printf("apic id = %02x\n", PCPU_GET(apic_id));
1049 panic("double fault");
1053 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
1056 struct trapframe *frame;
1061 frame = td->td_frame;
1063 params = (caddr_t)frame->tf_esp + sizeof(int);
1064 sa->code = frame->tf_eax;
1067 * Need to check if this is a 32 bit or 64 bit syscall.
1069 if (sa->code == SYS_syscall) {
1071 * Code is first argument, followed by actual args.
1073 sa->code = fuword(params);
1074 params += sizeof(int);
1075 } else if (sa->code == SYS___syscall) {
1077 * Like syscall, but code is a quad, so as to maintain
1078 * quad alignment for the rest of the arguments.
1080 sa->code = fuword(params);
1081 params += sizeof(quad_t);
1084 if (p->p_sysent->sv_mask)
1085 sa->code &= p->p_sysent->sv_mask;
1086 if (sa->code >= p->p_sysent->sv_size)
1087 sa->callp = &p->p_sysent->sv_table[0];
1089 sa->callp = &p->p_sysent->sv_table[sa->code];
1090 sa->narg = sa->callp->sy_narg;
1092 if (params != NULL && sa->narg != 0)
1093 error = copyin(params, (caddr_t)sa->args,
1094 (u_int)(sa->narg * sizeof(int)));
1099 td->td_retval[0] = 0;
1100 td->td_retval[1] = frame->tf_edx;
1106 #include "../../kern/subr_syscall.c"
1109 * syscall - system call request C handler. A system call is
1110 * essentially treated as a trap by reusing the frame layout.
1113 syscall(struct trapframe *frame)
1116 struct syscall_args sa;
1117 register_t orig_tf_eflags;
1122 if (ISPL(frame->tf_cs) != SEL_UPL) {
1127 orig_tf_eflags = frame->tf_eflags;
1130 td->td_frame = frame;
1132 error = syscallenter(td, &sa);
1137 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1138 frame->tf_eflags &= ~PSL_T;
1139 ksiginfo_init_trap(&ksi);
1140 ksi.ksi_signo = SIGTRAP;
1141 ksi.ksi_code = TRAP_TRACE;
1142 ksi.ksi_addr = (void *)frame->tf_eip;
1143 trapsignal(td, &ksi);
1146 KASSERT(PCB_USER_FPU(td->td_pcb),
1147 ("System call %s returning with kernel FPU ctx leaked",
1148 syscallname(td->td_proc, sa.code)));
1149 KASSERT(td->td_pcb->pcb_save == &td->td_pcb->pcb_user_save,
1150 ("System call %s returning with mangled pcb_save",
1151 syscallname(td->td_proc, sa.code)));
1153 syscallret(td, error, &sa);