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 void trap(struct trapframe *frame);
111 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)
192 static int lastalert = 0;
202 type = frame->tf_trapno;
205 /* Handler for NMI IPIs used for stopping CPUs. */
206 if (type == T_NMI && ipi_nmi_handler() == 0)
217 if (type == T_RESERVED) {
218 trap_fatal(frame, 0);
225 * CPU PMCs interrupt using an NMI so we check for that first.
226 * If the HWPMC module is active, 'pmc_hook' will point to
227 * the function to be called. A non-zero return value from the
228 * hook means that the NMI was consumed by it and that we can
229 * return immediately.
231 if (pmc_intr != NULL &&
232 (*pmc_intr)(PCPU_GET(cpuid), frame) != 0)
237 if (stack_nmi_handler(frame) != 0)
242 if (type == T_MCHK) {
249 * A trap can occur while DTrace executes a probe. Before
250 * executing the probe, DTrace blocks re-scheduling and sets
251 * a flag in its per-cpu flags to indicate that it doesn't
252 * want to fault. On returning from the probe, the no-fault
253 * flag is cleared and finally re-scheduling is enabled.
255 if ((type == T_PROTFLT || type == T_PAGEFLT) &&
256 dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
260 if ((frame->tf_eflags & PSL_I) == 0) {
262 * Buggy application or kernel code has disabled
263 * interrupts and then trapped. Enabling interrupts
264 * now is wrong, but it is better than running with
265 * interrupts disabled until they are accidentally
268 if (TRAPF_USERMODE(frame) &&
269 (curpcb->pcb_flags & PCB_VM86CALL) == 0)
271 "pid %ld (%s): trap %d with interrupts disabled\n",
272 (long)curproc->p_pid, curthread->td_name, type);
273 else if (type != T_NMI && type != T_BPTFLT &&
275 frame->tf_eip != (int)cpu_switch_load_gs) {
277 * XXX not quite right, since this may be for a
278 * multiple fault in user mode.
280 printf("kernel trap %d with interrupts disabled\n",
283 * Page faults need interrupts disabled until later,
284 * and we shouldn't enable interrupts while holding
287 if (type != T_PAGEFLT &&
288 td->td_md.md_spinlock_count == 0)
293 if (type == T_PAGEFLT) {
295 * For some Cyrix CPUs, %cr2 is clobbered by
296 * interrupts. This problem is worked around by using
297 * an interrupt gate for the pagefault handler. We
298 * are finally ready to read %cr2 and conditionally
299 * reenable interrupts. If we hold a spin lock, then
300 * we must not reenable interrupts. This might be a
301 * spurious page fault.
304 if (td->td_md.md_spinlock_count == 0)
308 if (TRAPF_USERMODE(frame) && (curpcb->pcb_flags & PCB_VM86CALL) == 0) {
312 td->td_frame = frame;
313 addr = frame->tf_eip;
314 if (td->td_cowgen != p->p_cowgen)
315 thread_cow_update(td);
318 case T_PRIVINFLT: /* privileged instruction fault */
323 case T_BPTFLT: /* bpt instruction fault */
324 case T_TRCTRAP: /* trace trap */
327 if (type == T_BPTFLT) {
328 fill_frame_regs(frame, ®s);
329 if (dtrace_pid_probe_ptr != NULL &&
330 dtrace_pid_probe_ptr(®s) == 0)
335 frame->tf_eflags &= ~PSL_T;
337 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
340 case T_ARITHTRAP: /* arithmetic trap */
341 ucode = npxtrap_x87();
348 * The following two traps can happen in vm86 mode,
349 * and, if so, we want to handle them specially.
351 case T_PROTFLT: /* general protection fault */
352 case T_STKFLT: /* stack fault */
353 if (frame->tf_eflags & PSL_VM) {
354 signo = vm86_emulate((struct vm86frame *)frame);
355 if (signo == SIGTRAP) {
357 load_dr6(rdr6() | 0x4000);
358 goto user_trctrap_out;
365 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
367 case T_SEGNPFLT: /* segment not present fault */
371 case T_TSSFLT: /* invalid TSS fault */
379 case T_DOUBLEFLT: /* double fault */
385 case T_PAGEFLT: /* page fault */
386 signo = trap_pfault(frame, TRUE, eva);
387 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
390 * The f00f hack workaround has triggered, so
391 * treat the fault as an illegal instruction
392 * (T_PRIVINFLT) instead of a page fault.
394 type = frame->tf_trapno = T_PRIVINFLT;
396 /* Proceed as in that case. */
407 if (signo == SIGSEGV)
409 else if (prot_fault_translation == 0) {
411 * Autodetect. This check also covers
412 * the images without the ABI-tag ELF
415 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD &&
416 p->p_osrel >= P_OSREL_SIGSEGV) {
421 ucode = BUS_PAGE_FAULT;
423 } else if (prot_fault_translation == 1) {
425 * Always compat mode.
428 ucode = BUS_PAGE_FAULT;
431 * 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);
507 KASSERT(cold || td->td_ucred != NULL,
508 ("kernel trap doesn't have ucred"));
510 case T_PAGEFLT: /* page fault */
511 (void) trap_pfault(frame, FALSE, eva);
515 if (PCB_USER_FPU(td->td_pcb))
516 panic("Unregistered use of FPU in kernel");
521 case T_ARITHTRAP: /* arithmetic trap */
522 case T_XMMFLT: /* SIMD floating-point exception */
523 case T_FPOPFLT: /* FPU operand fetch fault */
525 * XXXKIB for now disable any FPU traps in kernel
526 * handler registration seems to be overkill
528 trap_fatal(frame, 0);
532 * The following two traps can happen in
533 * vm86 mode, and, if so, we want to handle
536 case T_PROTFLT: /* general protection fault */
537 case T_STKFLT: /* stack fault */
538 if (frame->tf_eflags & PSL_VM) {
539 signo = vm86_emulate((struct vm86frame *)frame);
540 if (signo == SIGTRAP) {
542 load_dr6(rdr6() | 0x4000);
547 * returns to original process
549 vm86_trap((struct vm86frame *)frame);
553 case T_SEGNPFLT: /* segment not present fault */
554 if (curpcb->pcb_flags & PCB_VM86CALL)
558 * Invalid %fs's and %gs's can be created using
559 * procfs or PT_SETREGS or by invalidating the
560 * underlying LDT entry. This causes a fault
561 * in kernel mode when the kernel attempts to
562 * switch contexts. Lose the bad context
563 * (XXX) so that we can continue, and generate
566 if (frame->tf_eip == (int)cpu_switch_load_gs) {
570 kern_psignal(p, SIGBUS);
576 if (td->td_intr_nesting_level != 0)
580 * Invalid segment selectors and out of bounds
581 * %eip's and %esp's can be set up in user mode.
582 * This causes a fault in kernel mode when the
583 * kernel tries to return to user mode. We want
584 * to get this fault so that we can fix the
585 * problem here and not have to check all the
586 * selectors and pointers when the user changes
589 if (frame->tf_eip == (int)doreti_iret) {
590 frame->tf_eip = (int)doreti_iret_fault;
593 if (type == T_STKFLT)
596 if (frame->tf_eip == (int)doreti_popl_ds) {
597 frame->tf_eip = (int)doreti_popl_ds_fault;
600 if (frame->tf_eip == (int)doreti_popl_es) {
601 frame->tf_eip = (int)doreti_popl_es_fault;
604 if (frame->tf_eip == (int)doreti_popl_fs) {
605 frame->tf_eip = (int)doreti_popl_fs_fault;
608 if (curpcb->pcb_onfault != NULL) {
609 frame->tf_eip = (int)curpcb->pcb_onfault;
616 * PSL_NT can be set in user mode and isn't cleared
617 * automatically when the kernel is entered. This
618 * causes a TSS fault when the kernel attempts to
619 * `iret' because the TSS link is uninitialized. We
620 * want to get this fault so that we can fix the
621 * problem here and not every time the kernel is
624 if (frame->tf_eflags & PSL_NT) {
625 frame->tf_eflags &= ~PSL_NT;
630 case T_TRCTRAP: /* trace trap */
632 if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
634 * We've just entered system mode via the
635 * syscall lcall. Continue single stepping
636 * silently until the syscall handler has
641 if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
643 * The syscall handler has now saved the
644 * flags. Stop single stepping it.
646 frame->tf_eflags &= ~PSL_T;
650 * Ignore debug register trace traps due to
651 * accesses in the user's address space, which
652 * can happen under several conditions such as
653 * if a user sets a watchpoint on a buffer and
654 * then passes that buffer to a system call.
655 * We still want to get TRCTRAPS for addresses
656 * in kernel space because that is useful when
657 * debugging the kernel.
659 if (user_dbreg_trap() &&
660 !(curpcb->pcb_flags & PCB_VM86CALL)) {
662 * Reset breakpoint bits because the
665 load_dr6(rdr6() & ~0xf);
669 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
673 * If KDB is enabled, let it handle the debugger trap.
674 * Otherwise, debugger traps "can't happen".
677 /* XXX %dr6 is not quite reentrant. */
679 load_dr6(dr6 & ~0x4000);
680 if (kdb_trap(type, dr6, frame))
688 if (time_second - lastalert > 10) {
689 log(LOG_WARNING, "NMI: power fail\n");
691 lastalert = time_second;
694 #else /* !POWERFAIL_NMI */
695 nmi_handle_intr(type, frame);
697 #endif /* POWERFAIL_NMI */
701 trap_fatal(frame, eva);
705 /* Translate fault for emulators (e.g. Linux) */
706 if (*p->p_sysent->sv_transtrap != NULL)
707 signo = (*p->p_sysent->sv_transtrap)(signo, type);
709 ksiginfo_init_trap(&ksi);
710 ksi.ksi_signo = signo;
711 ksi.ksi_code = ucode;
712 ksi.ksi_addr = (void *)addr;
713 ksi.ksi_trapno = type;
714 if (uprintf_signal) {
715 uprintf("pid %d comm %s: signal %d err %x code %d type %d "
716 "addr 0x%x esp 0x%08x eip 0x%08x "
717 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
718 p->p_pid, p->p_comm, signo, frame->tf_err, ucode, type,
719 addr, frame->tf_esp, frame->tf_eip,
720 fubyte((void *)(frame->tf_eip + 0)),
721 fubyte((void *)(frame->tf_eip + 1)),
722 fubyte((void *)(frame->tf_eip + 2)),
723 fubyte((void *)(frame->tf_eip + 3)),
724 fubyte((void *)(frame->tf_eip + 4)),
725 fubyte((void *)(frame->tf_eip + 5)),
726 fubyte((void *)(frame->tf_eip + 6)),
727 fubyte((void *)(frame->tf_eip + 7)));
729 KASSERT((read_eflags() & PSL_I) != 0, ("interrupts disabled"));
730 trapsignal(td, &ksi);
734 KASSERT(PCB_USER_FPU(td->td_pcb),
735 ("Return from trap with kernel FPU ctx leaked"));
739 trap_pfault(struct trapframe *frame, int usermode, vm_offset_t eva)
752 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
754 * Due to both processor errata and lazy TLB invalidation when
755 * access restrictions are removed from virtual pages, memory
756 * accesses that are allowed by the physical mapping layer may
757 * nonetheless cause one spurious page fault per virtual page.
758 * When the thread is executing a "no faulting" section that
759 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
760 * every page fault is treated as a spurious page fault,
761 * unless it accesses the same virtual address as the most
762 * recent page fault within the same "no faulting" section.
764 if (td->td_md.md_spurflt_addr != eva ||
765 (td->td_pflags & TDP_RESETSPUR) != 0) {
767 * Do nothing to the TLB. A stale TLB entry is
768 * flushed automatically by a page fault.
770 td->td_md.md_spurflt_addr = eva;
771 td->td_pflags &= ~TDP_RESETSPUR;
776 * If we get a page fault while in a critical section, then
777 * it is most likely a fatal kernel page fault. The kernel
778 * is already going to panic trying to get a sleep lock to
779 * do the VM lookup, so just consider it a fatal trap so the
780 * kernel can print out a useful trap message and even get
783 * If we get a page fault while holding a non-sleepable
784 * lock, then it is most likely a fatal kernel page fault.
785 * If WITNESS is enabled, then it's going to whine about
786 * bogus LORs with various VM locks, so just skip to the
787 * fatal trap handling directly.
789 if (td->td_critnest != 0 ||
790 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
791 "Kernel page fault") != 0) {
792 trap_fatal(frame, eva);
796 va = trunc_page(eva);
797 if (va >= KERNBASE) {
799 * Don't allow user-mode faults in kernel address space.
800 * An exception: if the faulting address is the invalid
801 * instruction entry in the IDT, then the Intel Pentium
802 * F00F bug workaround was triggered, and we need to
803 * treat it is as an illegal instruction, and not a page
806 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
807 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
815 map = &p->p_vmspace->vm_map;
818 * When accessing a user-space address, kernel must be
819 * ready to accept the page fault, and provide a
820 * handling routine. Since accessing the address
821 * without the handler is a bug, do not try to handle
822 * it normally, and panic immediately.
824 if (!usermode && (td->td_intr_nesting_level != 0 ||
825 curpcb->pcb_onfault == NULL)) {
826 trap_fatal(frame, eva);
832 * If the trap was caused by errant bits in the PTE then panic.
834 if (frame->tf_err & PGEX_RSV) {
835 trap_fatal(frame, eva);
840 * PGEX_I is defined only if the execute disable bit capability is
841 * supported and enabled.
843 if (frame->tf_err & PGEX_W)
844 ftype = VM_PROT_WRITE;
845 #if defined(PAE) || defined(PAE_TABLES)
846 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
847 ftype = VM_PROT_EXECUTE;
850 ftype = VM_PROT_READ;
852 /* Fault in the page. */
853 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
854 if (rv == KERN_SUCCESS) {
856 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
857 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
858 if (ftype == VM_PROT_READ)
859 PMC_SOFT_CALL_TF( , , page_fault, read,
862 PMC_SOFT_CALL_TF( , , page_fault, write,
870 if (td->td_intr_nesting_level == 0 &&
871 curpcb->pcb_onfault != NULL) {
872 frame->tf_eip = (int)curpcb->pcb_onfault;
875 trap_fatal(frame, eva);
878 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
882 trap_fatal(frame, eva)
883 struct trapframe *frame;
888 struct soft_segment_descriptor softseg;
891 code = frame->tf_err;
892 type = frame->tf_trapno;
893 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
895 if (type <= MAX_TRAP_MSG)
896 msg = trap_msg[type];
899 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
900 frame->tf_eflags & PSL_VM ? "vm86" :
901 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
903 /* two separate prints in case of a trap on an unmapped page */
904 printf("cpuid = %d; ", PCPU_GET(cpuid));
905 printf("apic id = %02x\n", PCPU_GET(apic_id));
907 if (type == T_PAGEFLT) {
908 printf("fault virtual address = 0x%x\n", eva);
909 printf("fault code = %s %s%s, %s\n",
910 code & PGEX_U ? "user" : "supervisor",
911 code & PGEX_W ? "write" : "read",
912 #if defined(PAE) || defined(PAE_TABLES)
914 (code & PGEX_I ? " instruction" : " data") :
917 code & PGEX_RSV ? "reserved bits in PTE" :
918 code & PGEX_P ? "protection violation" : "page not present");
920 printf("instruction pointer = 0x%x:0x%x\n",
921 frame->tf_cs & 0xffff, frame->tf_eip);
922 if (TF_HAS_STACKREGS(frame)) {
923 ss = frame->tf_ss & 0xffff;
926 ss = GSEL(GDATA_SEL, SEL_KPL);
927 esp = (int)&frame->tf_esp;
929 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
930 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
931 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
932 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
933 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
934 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
936 printf("processor eflags = ");
937 if (frame->tf_eflags & PSL_T)
938 printf("trace trap, ");
939 if (frame->tf_eflags & PSL_I)
940 printf("interrupt enabled, ");
941 if (frame->tf_eflags & PSL_NT)
942 printf("nested task, ");
943 if (frame->tf_eflags & PSL_RF)
945 if (frame->tf_eflags & PSL_VM)
947 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
948 printf("current process = %d (%s)\n",
949 curproc->p_pid, curthread->td_name);
952 if (debugger_on_panic || kdb_active) {
953 frame->tf_err = eva; /* smuggle fault address to ddb */
954 if (kdb_trap(type, 0, frame)) {
955 frame->tf_err = code; /* restore error code */
958 frame->tf_err = code; /* restore error code */
961 printf("trap number = %d\n", type);
962 if (type <= MAX_TRAP_MSG)
963 panic("%s", trap_msg[type]);
965 panic("unknown/reserved trap");
969 * Double fault handler. Called when a fault occurs while writing
970 * a frame for a trap/exception onto the stack. This usually occurs
971 * when the stack overflows (such is the case with infinite recursion,
974 * XXX Note that the current PTD gets replaced by IdlePTD when the
975 * task switch occurs. This means that the stack that was active at
976 * the time of the double fault is not available at <kstack> unless
977 * the machine was idle when the double fault occurred. The downside
978 * of this is that "trace <ebp>" in ddb won't work.
984 if (dtrace_doubletrap_func != NULL)
985 (*dtrace_doubletrap_func)();
987 printf("\nFatal double fault:\n");
988 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
989 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
990 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
992 /* two separate prints in case of a trap on an unmapped page */
993 printf("cpuid = %d; ", PCPU_GET(cpuid));
994 printf("apic id = %02x\n", PCPU_GET(apic_id));
996 panic("double fault");
1000 cpu_fetch_syscall_args(struct thread *td)
1003 struct trapframe *frame;
1004 struct syscall_args *sa;
1010 frame = td->td_frame;
1013 params = (caddr_t)frame->tf_esp + sizeof(int);
1014 sa->code = frame->tf_eax;
1017 * Need to check if this is a 32 bit or 64 bit syscall.
1019 if (sa->code == SYS_syscall) {
1021 * Code is first argument, followed by actual args.
1023 error = fueword(params, &tmp);
1027 params += sizeof(int);
1028 } else if (sa->code == SYS___syscall) {
1030 * Like syscall, but code is a quad, so as to maintain
1031 * quad alignment for the rest of the arguments.
1033 error = fueword(params, &tmp);
1037 params += sizeof(quad_t);
1040 if (p->p_sysent->sv_mask)
1041 sa->code &= p->p_sysent->sv_mask;
1042 if (sa->code >= p->p_sysent->sv_size)
1043 sa->callp = &p->p_sysent->sv_table[0];
1045 sa->callp = &p->p_sysent->sv_table[sa->code];
1046 sa->narg = sa->callp->sy_narg;
1048 if (params != NULL && sa->narg != 0)
1049 error = copyin(params, (caddr_t)sa->args,
1050 (u_int)(sa->narg * sizeof(int)));
1055 td->td_retval[0] = 0;
1056 td->td_retval[1] = frame->tf_edx;
1062 #include "../../kern/subr_syscall.c"
1065 * syscall - system call request C handler. A system call is
1066 * essentially treated as a trap by reusing the frame layout.
1069 syscall(struct trapframe *frame)
1072 register_t orig_tf_eflags;
1077 if (!(TRAPF_USERMODE(frame) &&
1078 (curpcb->pcb_flags & PCB_VM86CALL) == 0)) {
1083 orig_tf_eflags = frame->tf_eflags;
1086 td->td_frame = frame;
1088 error = syscallenter(td);
1093 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1094 frame->tf_eflags &= ~PSL_T;
1095 ksiginfo_init_trap(&ksi);
1096 ksi.ksi_signo = SIGTRAP;
1097 ksi.ksi_code = TRAP_TRACE;
1098 ksi.ksi_addr = (void *)frame->tf_eip;
1099 trapsignal(td, &ksi);
1102 KASSERT(PCB_USER_FPU(td->td_pcb),
1103 ("System call %s returning with kernel FPU ctx leaked",
1104 syscallname(td->td_proc, td->td_sa.code)));
1105 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
1106 ("System call %s returning with mangled pcb_save",
1107 syscallname(td->td_proc, td->td_sa.code)));
1109 syscallret(td, error);