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_stack.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/stack.h>
98 #include <machine/tss.h>
99 #include <machine/vm86.h>
102 #include <sys/syslog.h>
103 #include <machine/clock.h>
107 #include <sys/dtrace_bsd.h>
110 extern void trap(struct trapframe *frame);
111 extern void syscall(struct trapframe *frame);
113 static int trap_pfault(struct trapframe *, int, vm_offset_t);
114 static void trap_fatal(struct trapframe *, vm_offset_t);
115 void dblfault_handler(void);
117 extern inthand_t IDTVEC(lcall_syscall);
119 #define MAX_TRAP_MSG 32
120 static char *trap_msg[] = {
122 "privileged instruction fault", /* 1 T_PRIVINFLT */
124 "breakpoint instruction fault", /* 3 T_BPTFLT */
127 "arithmetic trap", /* 6 T_ARITHTRAP */
130 "general protection fault", /* 9 T_PROTFLT */
131 "trace trap", /* 10 T_TRCTRAP */
133 "page fault", /* 12 T_PAGEFLT */
135 "alignment fault", /* 14 T_ALIGNFLT */
139 "integer divide fault", /* 18 T_DIVIDE */
140 "non-maskable interrupt trap", /* 19 T_NMI */
141 "overflow trap", /* 20 T_OFLOW */
142 "FPU bounds check fault", /* 21 T_BOUND */
143 "FPU device not available", /* 22 T_DNA */
144 "double fault", /* 23 T_DOUBLEFLT */
145 "FPU operand fetch fault", /* 24 T_FPOPFLT */
146 "invalid TSS fault", /* 25 T_TSSFLT */
147 "segment not present fault", /* 26 T_SEGNPFLT */
148 "stack fault", /* 27 T_STKFLT */
149 "machine check trap", /* 28 T_MCHK */
150 "SIMD floating-point exception", /* 29 T_XMMFLT */
151 "reserved (unknown) fault", /* 30 T_RESERVED */
152 "", /* 31 unused (reserved) */
153 "DTrace pid return trap", /* 32 T_DTRACE_RET */
156 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
157 int has_f00f_bug = 0; /* Initialized so that it can be patched. */
160 static int prot_fault_translation = 0;
161 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
162 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
163 static int uprintf_signal;
164 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RW,
166 "Print debugging information on trap signal to ctty");
169 * Exception, fault, and trap interface to the FreeBSD kernel.
170 * This common code is called from assembly language IDT gate entry
171 * routines that prepare a suitable stack frame, and restore this
172 * frame after the exception has been processed.
176 trap(struct trapframe *frame)
181 struct thread *td = curthread;
182 struct proc *p = td->td_proc;
186 int i = 0, ucode = 0;
192 static int lastalert = 0;
196 type = frame->tf_trapno;
199 /* Handler for NMI IPIs used for stopping CPUs. */
201 if (ipi_nmi_handler() == 0)
213 if (type == T_RESERVED) {
214 trap_fatal(frame, 0);
221 * CPU PMCs interrupt using an NMI so we check for that first.
222 * If the HWPMC module is active, 'pmc_hook' will point to
223 * the function to be called. A non-zero return value from the
224 * hook means that the NMI was consumed by it and that we can
225 * return immediately.
227 if (pmc_intr != NULL &&
228 (*pmc_intr)(PCPU_GET(cpuid), frame) != 0)
233 if (stack_nmi_handler(frame) != 0)
238 if (type == T_MCHK) {
245 * A trap can occur while DTrace executes a probe. Before
246 * executing the probe, DTrace blocks re-scheduling and sets
247 * a flag in its per-cpu flags to indicate that it doesn't
248 * want to fault. On returning from the probe, the no-fault
249 * flag is cleared and finally re-scheduling is enabled.
251 if ((type == T_PROTFLT || type == T_PAGEFLT) &&
252 dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
256 if ((frame->tf_eflags & PSL_I) == 0) {
258 * Buggy application or kernel code has disabled
259 * interrupts and then trapped. Enabling interrupts
260 * now is wrong, but it is better than running with
261 * interrupts disabled until they are accidentally
264 if (TRAPF_USERMODE(frame) &&
265 (curpcb->pcb_flags & PCB_VM86CALL) == 0)
267 "pid %ld (%s): trap %d with interrupts disabled\n",
268 (long)curproc->p_pid, curthread->td_name, type);
269 else if (type != T_NMI && type != T_BPTFLT &&
271 frame->tf_eip != (int)cpu_switch_load_gs) {
273 * XXX not quite right, since this may be for a
274 * multiple fault in user mode.
276 printf("kernel trap %d with interrupts disabled\n",
279 * Page faults need interrupts disabled until later,
280 * and we shouldn't enable interrupts while holding
283 if (type != T_PAGEFLT &&
284 td->td_md.md_spinlock_count == 0)
289 if (type == T_PAGEFLT) {
291 * For some Cyrix CPUs, %cr2 is clobbered by
292 * interrupts. This problem is worked around by using
293 * an interrupt gate for the pagefault handler. We
294 * are finally ready to read %cr2 and conditionally
295 * reenable interrupts. If we hold a spin lock, then
296 * we must not reenable interrupts. This might be a
297 * spurious page fault.
300 if (td->td_md.md_spinlock_count == 0)
304 if (TRAPF_USERMODE(frame) && (curpcb->pcb_flags & PCB_VM86CALL) == 0) {
308 td->td_frame = frame;
309 addr = frame->tf_eip;
310 if (td->td_cowgen != p->p_cowgen)
311 thread_cow_update(td);
314 case T_PRIVINFLT: /* privileged instruction fault */
319 case T_BPTFLT: /* bpt instruction fault */
320 case T_TRCTRAP: /* trace trap */
323 if (type == T_BPTFLT) {
324 fill_frame_regs(frame, ®s);
325 if (dtrace_pid_probe_ptr != NULL &&
326 dtrace_pid_probe_ptr(®s) == 0)
331 frame->tf_eflags &= ~PSL_T;
333 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
336 case T_ARITHTRAP: /* arithmetic trap */
337 ucode = npxtrap_x87();
344 * The following two traps can happen in
345 * vm86 mode, and, if so, we want to handle
348 case T_PROTFLT: /* general protection fault */
349 case T_STKFLT: /* stack fault */
350 if (frame->tf_eflags & PSL_VM) {
351 i = vm86_emulate((struct vm86frame *)frame);
354 load_dr6(rdr6() | 0x4000);
355 goto user_trctrap_out;
362 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
364 case T_SEGNPFLT: /* segment not present fault */
368 case T_TSSFLT: /* invalid TSS fault */
376 case T_DOUBLEFLT: /* double fault */
382 case T_PAGEFLT: /* page fault */
384 i = trap_pfault(frame, TRUE, eva);
385 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
388 * The f00f hack workaround has triggered, so
389 * treat the fault as an illegal instruction
390 * (T_PRIVINFLT) instead of a page fault.
392 type = frame->tf_trapno = T_PRIVINFLT;
394 /* Proceed as in that case. */
408 if (prot_fault_translation == 0) {
411 * This check also covers the images
412 * without the ABI-tag ELF note.
414 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
415 && p->p_osrel >= P_OSREL_SIGSEGV) {
420 ucode = BUS_PAGE_FAULT;
422 } else if (prot_fault_translation == 1) {
424 * Always compat mode.
427 ucode = BUS_PAGE_FAULT;
430 * Always SIGSEGV mode.
439 case T_DIVIDE: /* integer divide fault */
448 # define TIMER_FREQ 1193182
450 if (time_second - lastalert > 10) {
451 log(LOG_WARNING, "NMI: power fail\n");
453 lastalert = time_second;
456 #else /* !POWERFAIL_NMI */
457 nmi_handle_intr(type, frame);
459 #endif /* POWERFAIL_NMI */
462 case T_OFLOW: /* integer overflow fault */
467 case T_BOUND: /* bounds check fault */
473 KASSERT(PCB_USER_FPU(td->td_pcb),
474 ("kernel FPU ctx has leaked"));
475 /* transparent fault (due to context switch "late") */
478 uprintf("pid %d killed due to lack of floating point\n",
484 case T_FPOPFLT: /* FPU operand fetch fault */
489 case T_XMMFLT: /* SIMD floating-point exception */
490 ucode = npxtrap_sse();
498 fill_frame_regs(frame, ®s);
499 if (dtrace_return_probe_ptr != NULL &&
500 dtrace_return_probe_ptr(®s) == 0)
508 KASSERT(cold || td->td_ucred != NULL,
509 ("kernel trap doesn't have ucred"));
511 case T_PAGEFLT: /* page fault */
512 (void) trap_pfault(frame, FALSE, eva);
516 if (PCB_USER_FPU(td->td_pcb))
517 panic("Unregistered use of FPU in kernel");
522 case T_ARITHTRAP: /* arithmetic trap */
523 case T_XMMFLT: /* SIMD floating-point exception */
524 case T_FPOPFLT: /* FPU operand fetch fault */
526 * XXXKIB for now disable any FPU traps in kernel
527 * handler registration seems to be overkill
529 trap_fatal(frame, 0);
533 * The following two traps can happen in
534 * vm86 mode, and, if so, we want to handle
537 case T_PROTFLT: /* general protection fault */
538 case T_STKFLT: /* stack fault */
539 if (frame->tf_eflags & PSL_VM) {
540 i = vm86_emulate((struct vm86frame *)frame);
543 load_dr6(rdr6() | 0x4000);
548 * returns to original process
550 vm86_trap((struct vm86frame *)frame);
553 if (type == T_STKFLT)
558 case T_SEGNPFLT: /* segment not present fault */
559 if (curpcb->pcb_flags & PCB_VM86CALL)
563 * Invalid %fs's and %gs's can be created using
564 * procfs or PT_SETREGS or by invalidating the
565 * underlying LDT entry. This causes a fault
566 * in kernel mode when the kernel attempts to
567 * switch contexts. Lose the bad context
568 * (XXX) so that we can continue, and generate
571 if (frame->tf_eip == (int)cpu_switch_load_gs) {
575 kern_psignal(p, SIGBUS);
581 if (td->td_intr_nesting_level != 0)
585 * Invalid segment selectors and out of bounds
586 * %eip's and %esp's can be set up in user mode.
587 * This causes a fault in kernel mode when the
588 * kernel tries to return to user mode. We want
589 * to get this fault so that we can fix the
590 * problem here and not have to check all the
591 * selectors and pointers when the user changes
594 if (frame->tf_eip == (int)doreti_iret) {
595 frame->tf_eip = (int)doreti_iret_fault;
598 if (frame->tf_eip == (int)doreti_popl_ds) {
599 frame->tf_eip = (int)doreti_popl_ds_fault;
602 if (frame->tf_eip == (int)doreti_popl_es) {
603 frame->tf_eip = (int)doreti_popl_es_fault;
606 if (frame->tf_eip == (int)doreti_popl_fs) {
607 frame->tf_eip = (int)doreti_popl_fs_fault;
610 if (curpcb->pcb_onfault != NULL) {
612 (int)curpcb->pcb_onfault;
619 * PSL_NT can be set in user mode and isn't cleared
620 * automatically when the kernel is entered. This
621 * causes a TSS fault when the kernel attempts to
622 * `iret' because the TSS link is uninitialized. We
623 * want to get this fault so that we can fix the
624 * problem here and not every time the kernel is
627 if (frame->tf_eflags & PSL_NT) {
628 frame->tf_eflags &= ~PSL_NT;
633 case T_TRCTRAP: /* trace trap */
635 if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
637 * We've just entered system mode via the
638 * syscall lcall. Continue single stepping
639 * silently until the syscall handler has
644 if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
646 * The syscall handler has now saved the
647 * flags. Stop single stepping it.
649 frame->tf_eflags &= ~PSL_T;
653 * Ignore debug register trace traps due to
654 * accesses in the user's address space, which
655 * can happen under several conditions such as
656 * if a user sets a watchpoint on a buffer and
657 * then passes that buffer to a system call.
658 * We still want to get TRCTRAPS for addresses
659 * in kernel space because that is useful when
660 * debugging the kernel.
662 if (user_dbreg_trap() &&
663 !(curpcb->pcb_flags & PCB_VM86CALL)) {
665 * Reset breakpoint bits because the
668 load_dr6(rdr6() & ~0xf);
672 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
676 * If KDB is enabled, let it handle the debugger trap.
677 * Otherwise, debugger traps "can't happen".
680 /* XXX %dr6 is not quite reentrant. */
682 load_dr6(dr6 & ~0x4000);
683 if (kdb_trap(type, dr6, frame))
691 if (time_second - lastalert > 10) {
692 log(LOG_WARNING, "NMI: power fail\n");
694 lastalert = time_second;
697 #else /* !POWERFAIL_NMI */
698 nmi_handle_intr(type, frame);
700 #endif /* POWERFAIL_NMI */
704 trap_fatal(frame, eva);
708 /* Translate fault for emulators (e.g. Linux) */
709 if (*p->p_sysent->sv_transtrap)
710 i = (*p->p_sysent->sv_transtrap)(i, type);
712 ksiginfo_init_trap(&ksi);
714 ksi.ksi_code = ucode;
715 ksi.ksi_addr = (void *)addr;
716 ksi.ksi_trapno = type;
717 if (uprintf_signal) {
718 uprintf("pid %d comm %s: signal %d err %x code %d type %d "
719 "addr 0x%x esp 0x%08x eip 0x%08x "
720 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
721 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr,
722 frame->tf_esp, frame->tf_eip,
723 fubyte((void *)(frame->tf_eip + 0)),
724 fubyte((void *)(frame->tf_eip + 1)),
725 fubyte((void *)(frame->tf_eip + 2)),
726 fubyte((void *)(frame->tf_eip + 3)),
727 fubyte((void *)(frame->tf_eip + 4)),
728 fubyte((void *)(frame->tf_eip + 5)),
729 fubyte((void *)(frame->tf_eip + 6)),
730 fubyte((void *)(frame->tf_eip + 7)));
732 KASSERT((read_eflags() & PSL_I) != 0, ("interrupts disabled"));
733 trapsignal(td, &ksi);
736 if (type <= MAX_TRAP_MSG) {
737 uprintf("fatal process exception: %s",
739 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
740 uprintf(", fault VA = 0x%lx", (u_long)eva);
747 KASSERT(PCB_USER_FPU(td->td_pcb),
748 ("Return from trap with kernel FPU ctx leaked"));
755 trap_pfault(frame, usermode, eva)
756 struct trapframe *frame;
764 struct thread *td = curthread;
765 struct proc *p = td->td_proc;
767 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
769 * Due to both processor errata and lazy TLB invalidation when
770 * access restrictions are removed from virtual pages, memory
771 * accesses that are allowed by the physical mapping layer may
772 * nonetheless cause one spurious page fault per virtual page.
773 * When the thread is executing a "no faulting" section that
774 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
775 * every page fault is treated as a spurious page fault,
776 * unless it accesses the same virtual address as the most
777 * recent page fault within the same "no faulting" section.
779 if (td->td_md.md_spurflt_addr != eva ||
780 (td->td_pflags & TDP_RESETSPUR) != 0) {
782 * Do nothing to the TLB. A stale TLB entry is
783 * flushed automatically by a page fault.
785 td->td_md.md_spurflt_addr = eva;
786 td->td_pflags &= ~TDP_RESETSPUR;
791 * If we get a page fault while in a critical section, then
792 * it is most likely a fatal kernel page fault. The kernel
793 * is already going to panic trying to get a sleep lock to
794 * do the VM lookup, so just consider it a fatal trap so the
795 * kernel can print out a useful trap message and even get
798 * If we get a page fault while holding a non-sleepable
799 * lock, then it is most likely a fatal kernel page fault.
800 * If WITNESS is enabled, then it's going to whine about
801 * bogus LORs with various VM locks, so just skip to the
802 * fatal trap handling directly.
804 if (td->td_critnest != 0 ||
805 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
806 "Kernel page fault") != 0) {
807 trap_fatal(frame, eva);
811 va = trunc_page(eva);
812 if (va >= KERNBASE) {
814 * Don't allow user-mode faults in kernel address space.
815 * An exception: if the faulting address is the invalid
816 * instruction entry in the IDT, then the Intel Pentium
817 * F00F bug workaround was triggered, and we need to
818 * treat it is as an illegal instruction, and not a page
821 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
822 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
830 map = &p->p_vmspace->vm_map;
833 * When accessing a user-space address, kernel must be
834 * ready to accept the page fault, and provide a
835 * handling routine. Since accessing the address
836 * without the handler is a bug, do not try to handle
837 * it normally, and panic immediately.
839 if (!usermode && (td->td_intr_nesting_level != 0 ||
840 curpcb->pcb_onfault == NULL)) {
841 trap_fatal(frame, eva);
847 * If the trap was caused by errant bits in the PTE then panic.
849 if (frame->tf_err & PGEX_RSV) {
850 trap_fatal(frame, eva);
855 * PGEX_I is defined only if the execute disable bit capability is
856 * supported and enabled.
858 if (frame->tf_err & PGEX_W)
859 ftype = VM_PROT_WRITE;
860 #if defined(PAE) || defined(PAE_TABLES)
861 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
862 ftype = VM_PROT_EXECUTE;
865 ftype = VM_PROT_READ;
867 /* Fault in the page. */
868 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
869 if (rv == KERN_SUCCESS) {
871 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
872 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
873 if (ftype == VM_PROT_READ)
874 PMC_SOFT_CALL_TF( , , page_fault, read,
877 PMC_SOFT_CALL_TF( , , page_fault, write,
885 if (td->td_intr_nesting_level == 0 &&
886 curpcb->pcb_onfault != NULL) {
887 frame->tf_eip = (int)curpcb->pcb_onfault;
890 trap_fatal(frame, eva);
893 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
897 trap_fatal(frame, eva)
898 struct trapframe *frame;
903 struct soft_segment_descriptor softseg;
906 code = frame->tf_err;
907 type = frame->tf_trapno;
908 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
910 if (type <= MAX_TRAP_MSG)
911 msg = trap_msg[type];
914 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
915 frame->tf_eflags & PSL_VM ? "vm86" :
916 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
918 /* two separate prints in case of a trap on an unmapped page */
919 printf("cpuid = %d; ", PCPU_GET(cpuid));
920 printf("apic id = %02x\n", PCPU_GET(apic_id));
922 if (type == T_PAGEFLT) {
923 printf("fault virtual address = 0x%x\n", eva);
924 printf("fault code = %s %s%s, %s\n",
925 code & PGEX_U ? "user" : "supervisor",
926 code & PGEX_W ? "write" : "read",
927 #if defined(PAE) || defined(PAE_TABLES)
929 (code & PGEX_I ? " instruction" : " data") :
932 code & PGEX_RSV ? "reserved bits in PTE" :
933 code & PGEX_P ? "protection violation" : "page not present");
935 printf("instruction pointer = 0x%x:0x%x\n",
936 frame->tf_cs & 0xffff, frame->tf_eip);
937 if (TF_HAS_STACKREGS(frame)) {
938 ss = frame->tf_ss & 0xffff;
941 ss = GSEL(GDATA_SEL, SEL_KPL);
942 esp = (int)&frame->tf_esp;
944 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
945 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
946 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
947 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
948 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
949 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
951 printf("processor eflags = ");
952 if (frame->tf_eflags & PSL_T)
953 printf("trace trap, ");
954 if (frame->tf_eflags & PSL_I)
955 printf("interrupt enabled, ");
956 if (frame->tf_eflags & PSL_NT)
957 printf("nested task, ");
958 if (frame->tf_eflags & PSL_RF)
960 if (frame->tf_eflags & PSL_VM)
962 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
963 printf("current process = %d (%s)\n",
964 curproc->p_pid, curthread->td_name);
967 if (debugger_on_panic || kdb_active) {
968 frame->tf_err = eva; /* smuggle fault address to ddb */
969 if (kdb_trap(type, 0, frame)) {
970 frame->tf_err = code; /* restore error code */
973 frame->tf_err = code; /* restore error code */
976 printf("trap number = %d\n", type);
977 if (type <= MAX_TRAP_MSG)
978 panic("%s", trap_msg[type]);
980 panic("unknown/reserved trap");
984 * Double fault handler. Called when a fault occurs while writing
985 * a frame for a trap/exception onto the stack. This usually occurs
986 * when the stack overflows (such is the case with infinite recursion,
989 * XXX Note that the current PTD gets replaced by IdlePTD when the
990 * task switch occurs. This means that the stack that was active at
991 * the time of the double fault is not available at <kstack> unless
992 * the machine was idle when the double fault occurred. The downside
993 * of this is that "trace <ebp>" in ddb won't work.
999 if (dtrace_doubletrap_func != NULL)
1000 (*dtrace_doubletrap_func)();
1002 printf("\nFatal double fault:\n");
1003 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
1004 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
1005 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
1007 /* two separate prints in case of a trap on an unmapped page */
1008 printf("cpuid = %d; ", PCPU_GET(cpuid));
1009 printf("apic id = %02x\n", PCPU_GET(apic_id));
1011 panic("double fault");
1015 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
1018 struct trapframe *frame;
1024 frame = td->td_frame;
1026 params = (caddr_t)frame->tf_esp + sizeof(int);
1027 sa->code = frame->tf_eax;
1030 * Need to check if this is a 32 bit or 64 bit syscall.
1032 if (sa->code == SYS_syscall) {
1034 * Code is first argument, followed by actual args.
1036 error = fueword(params, &tmp);
1040 params += sizeof(int);
1041 } else if (sa->code == SYS___syscall) {
1043 * Like syscall, but code is a quad, so as to maintain
1044 * quad alignment for the rest of the arguments.
1046 error = fueword(params, &tmp);
1050 params += sizeof(quad_t);
1053 if (p->p_sysent->sv_mask)
1054 sa->code &= p->p_sysent->sv_mask;
1055 if (sa->code >= p->p_sysent->sv_size)
1056 sa->callp = &p->p_sysent->sv_table[0];
1058 sa->callp = &p->p_sysent->sv_table[sa->code];
1059 sa->narg = sa->callp->sy_narg;
1061 if (params != NULL && sa->narg != 0)
1062 error = copyin(params, (caddr_t)sa->args,
1063 (u_int)(sa->narg * sizeof(int)));
1068 td->td_retval[0] = 0;
1069 td->td_retval[1] = frame->tf_edx;
1075 #include "../../kern/subr_syscall.c"
1078 * syscall - system call request C handler. A system call is
1079 * essentially treated as a trap by reusing the frame layout.
1082 syscall(struct trapframe *frame)
1085 struct syscall_args sa;
1086 register_t orig_tf_eflags;
1091 if (!(TRAPF_USERMODE(frame) &&
1092 (curpcb->pcb_flags & PCB_VM86CALL) == 0)) {
1097 orig_tf_eflags = frame->tf_eflags;
1100 td->td_frame = frame;
1102 error = syscallenter(td, &sa);
1107 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1108 frame->tf_eflags &= ~PSL_T;
1109 ksiginfo_init_trap(&ksi);
1110 ksi.ksi_signo = SIGTRAP;
1111 ksi.ksi_code = TRAP_TRACE;
1112 ksi.ksi_addr = (void *)frame->tf_eip;
1113 trapsignal(td, &ksi);
1116 KASSERT(PCB_USER_FPU(td->td_pcb),
1117 ("System call %s returning with kernel FPU ctx leaked",
1118 syscallname(td->td_proc, sa.code)));
1119 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
1120 ("System call %s returning with mangled pcb_save",
1121 syscallname(td->td_proc, sa.code)));
1123 syscallret(td, error, &sa);