2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (C) 1994, David Greenman
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the University of Utah, and William Jolitz.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * from: @(#)trap.c 7.4 (Berkeley) 5/13/91
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * 386 Trap and System call handling
49 #include "opt_clock.h"
51 #include "opt_hwpmc_hooks.h"
54 #include "opt_stack.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/pmckern.h>
77 PMC_SOFT_DEFINE( , , page_fault, all);
78 PMC_SOFT_DEFINE( , , page_fault, read);
79 PMC_SOFT_DEFINE( , , page_fault, write);
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>
94 #include <machine/md_var.h>
95 #include <machine/pcb.h>
97 #include <machine/smp.h>
99 #include <machine/stack.h>
100 #include <machine/tss.h>
101 #include <machine/vm86.h>
104 #include <sys/syslog.h>
105 #include <machine/clock.h>
109 #include <sys/dtrace_bsd.h>
112 void trap(struct trapframe *frame);
113 void syscall(struct trapframe *frame);
115 static int trap_pfault(struct trapframe *, int, vm_offset_t);
116 static void trap_fatal(struct trapframe *, vm_offset_t);
117 void dblfault_handler(void);
119 extern inthand_t IDTVEC(lcall_syscall);
121 #define MAX_TRAP_MSG 32
122 static char *trap_msg[] = {
124 "privileged instruction fault", /* 1 T_PRIVINFLT */
126 "breakpoint instruction fault", /* 3 T_BPTFLT */
129 "arithmetic trap", /* 6 T_ARITHTRAP */
132 "general protection fault", /* 9 T_PROTFLT */
133 "trace trap", /* 10 T_TRCTRAP */
135 "page fault", /* 12 T_PAGEFLT */
137 "alignment fault", /* 14 T_ALIGNFLT */
141 "integer divide fault", /* 18 T_DIVIDE */
142 "non-maskable interrupt trap", /* 19 T_NMI */
143 "overflow trap", /* 20 T_OFLOW */
144 "FPU bounds check fault", /* 21 T_BOUND */
145 "FPU device not available", /* 22 T_DNA */
146 "double fault", /* 23 T_DOUBLEFLT */
147 "FPU operand fetch fault", /* 24 T_FPOPFLT */
148 "invalid TSS fault", /* 25 T_TSSFLT */
149 "segment not present fault", /* 26 T_SEGNPFLT */
150 "stack fault", /* 27 T_STKFLT */
151 "machine check trap", /* 28 T_MCHK */
152 "SIMD floating-point exception", /* 29 T_XMMFLT */
153 "reserved (unknown) fault", /* 30 T_RESERVED */
154 "", /* 31 unused (reserved) */
155 "DTrace pid return trap", /* 32 T_DTRACE_RET */
158 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
159 int has_f00f_bug = 0; /* Initialized so that it can be patched. */
162 static int prot_fault_translation = 0;
163 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
164 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
165 static int uprintf_signal;
166 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RW,
168 "Print debugging information on trap signal to ctty");
171 * Exception, fault, and trap interface to the FreeBSD kernel.
172 * This common code is called from assembly language IDT gate entry
173 * routines that prepare a suitable stack frame, and restore this
174 * frame after the exception has been processed.
178 trap(struct trapframe *frame)
191 static int lastalert = 0;
201 type = frame->tf_trapno;
204 /* Handler for NMI IPIs used for stopping CPUs. */
205 if (type == T_NMI && ipi_nmi_handler() == 0)
216 if (type == T_RESERVED) {
217 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 non-zero return value from the
227 * hook means that the NMI was consumed by it and that we can
228 * return immediately.
230 if (pmc_intr != NULL &&
231 (*pmc_intr)(PCPU_GET(cpuid), frame) != 0)
236 if (stack_nmi_handler(frame) != 0)
241 if (type == T_MCHK) {
248 * A trap can occur while DTrace executes a probe. Before
249 * executing the probe, DTrace blocks re-scheduling and sets
250 * a flag in its per-cpu flags to indicate that it doesn't
251 * want to fault. On returning from the probe, the no-fault
252 * flag is cleared and finally re-scheduling is enabled.
254 if ((type == T_PROTFLT || type == T_PAGEFLT) &&
255 dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
259 if ((frame->tf_eflags & PSL_I) == 0) {
261 * Buggy application or kernel code has disabled
262 * interrupts and then trapped. Enabling interrupts
263 * now is wrong, but it is better than running with
264 * interrupts disabled until they are accidentally
267 if (TRAPF_USERMODE(frame) &&
268 (curpcb->pcb_flags & PCB_VM86CALL) == 0)
270 "pid %ld (%s): trap %d with interrupts disabled\n",
271 (long)curproc->p_pid, curthread->td_name, type);
272 else if (type != T_NMI && type != T_BPTFLT &&
274 frame->tf_eip != (int)cpu_switch_load_gs) {
276 * XXX not quite right, since this may be for a
277 * multiple fault in user mode.
279 printf("kernel trap %d with interrupts disabled\n",
282 * Page faults need interrupts disabled until later,
283 * and we shouldn't enable interrupts while holding
286 if (type != T_PAGEFLT &&
287 td->td_md.md_spinlock_count == 0)
292 if (type == T_PAGEFLT) {
294 * For some Cyrix CPUs, %cr2 is clobbered by
295 * interrupts. This problem is worked around by using
296 * an interrupt gate for the pagefault handler. We
297 * are finally ready to read %cr2 and conditionally
298 * reenable interrupts. If we hold a spin lock, then
299 * we must not reenable interrupts. This might be a
300 * spurious page fault.
303 if (td->td_md.md_spinlock_count == 0)
307 if (TRAPF_USERMODE(frame) && (curpcb->pcb_flags & PCB_VM86CALL) == 0) {
311 td->td_frame = frame;
312 addr = frame->tf_eip;
313 if (td->td_cowgen != p->p_cowgen)
314 thread_cow_update(td);
317 case T_PRIVINFLT: /* privileged instruction fault */
322 case T_BPTFLT: /* bpt instruction fault */
323 case T_TRCTRAP: /* trace trap */
326 if (type == T_BPTFLT) {
327 if (dtrace_pid_probe_ptr != NULL &&
328 dtrace_pid_probe_ptr(frame) == 0)
333 frame->tf_eflags &= ~PSL_T;
335 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
338 case T_ARITHTRAP: /* arithmetic trap */
339 ucode = npxtrap_x87();
346 * The following two traps can happen in vm86 mode,
347 * and, if so, we want to handle them specially.
349 case T_PROTFLT: /* general protection fault */
350 case T_STKFLT: /* stack fault */
351 if (frame->tf_eflags & PSL_VM) {
352 signo = vm86_emulate((struct vm86frame *)frame);
353 if (signo == SIGTRAP) {
355 load_dr6(rdr6() | 0x4000);
356 goto user_trctrap_out;
363 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
365 case T_SEGNPFLT: /* segment not present fault */
369 case T_TSSFLT: /* invalid TSS fault */
377 case T_DOUBLEFLT: /* double fault */
383 case T_PAGEFLT: /* page fault */
384 signo = 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. */
405 if (signo == SIGSEGV)
407 else if (prot_fault_translation == 0) {
409 * Autodetect. This check also covers
410 * the images without the ABI-tag ELF
413 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD &&
414 p->p_osrel >= P_OSREL_SIGSEGV) {
419 ucode = BUS_PAGE_FAULT;
421 } else if (prot_fault_translation == 1) {
423 * Always compat mode.
426 ucode = BUS_PAGE_FAULT;
429 * Always SIGSEGV mode.
437 case T_DIVIDE: /* integer divide fault */
446 # define TIMER_FREQ 1193182
448 if (time_second - lastalert > 10) {
449 log(LOG_WARNING, "NMI: power fail\n");
451 lastalert = time_second;
454 #else /* !POWERFAIL_NMI */
455 nmi_handle_intr(type, frame);
457 #endif /* POWERFAIL_NMI */
460 case T_OFLOW: /* integer overflow fault */
465 case T_BOUND: /* bounds check fault */
471 KASSERT(PCB_USER_FPU(td->td_pcb),
472 ("kernel FPU ctx has leaked"));
473 /* transparent fault (due to context switch "late") */
476 uprintf("pid %d killed due to lack of floating point\n",
482 case T_FPOPFLT: /* FPU operand fetch fault */
487 case T_XMMFLT: /* SIMD floating-point exception */
488 ucode = npxtrap_sse();
496 if (dtrace_return_probe_ptr != NULL)
497 dtrace_return_probe_ptr(frame);
504 KASSERT(cold || td->td_ucred != NULL,
505 ("kernel trap doesn't have ucred"));
507 case T_PAGEFLT: /* page fault */
508 (void) trap_pfault(frame, FALSE, eva);
512 if (PCB_USER_FPU(td->td_pcb))
513 panic("Unregistered use of FPU in kernel");
518 case T_ARITHTRAP: /* arithmetic trap */
519 case T_XMMFLT: /* SIMD floating-point exception */
520 case T_FPOPFLT: /* FPU operand fetch fault */
522 * XXXKIB for now disable any FPU traps in kernel
523 * handler registration seems to be overkill
525 trap_fatal(frame, 0);
529 * The following two traps can happen in
530 * vm86 mode, and, if so, we want to handle
533 case T_PROTFLT: /* general protection fault */
534 case T_STKFLT: /* stack fault */
535 if (frame->tf_eflags & PSL_VM) {
536 signo = vm86_emulate((struct vm86frame *)frame);
537 if (signo == SIGTRAP) {
539 load_dr6(rdr6() | 0x4000);
544 * returns to original process
546 vm86_trap((struct vm86frame *)frame);
550 case T_SEGNPFLT: /* segment not present fault */
551 if (curpcb->pcb_flags & PCB_VM86CALL)
555 * Invalid %fs's and %gs's can be created using
556 * procfs or PT_SETREGS or by invalidating the
557 * underlying LDT entry. This causes a fault
558 * in kernel mode when the kernel attempts to
559 * switch contexts. Lose the bad context
560 * (XXX) so that we can continue, and generate
563 if (frame->tf_eip == (int)cpu_switch_load_gs) {
567 kern_psignal(p, SIGBUS);
573 if (td->td_intr_nesting_level != 0)
577 * Invalid segment selectors and out of bounds
578 * %eip's and %esp's can be set up in user mode.
579 * This causes a fault in kernel mode when the
580 * kernel tries to return to user mode. We want
581 * to get this fault so that we can fix the
582 * problem here and not have to check all the
583 * selectors and pointers when the user changes
586 if (frame->tf_eip == (int)doreti_iret) {
587 frame->tf_eip = (int)doreti_iret_fault;
590 if (type == T_STKFLT)
593 if (frame->tf_eip == (int)doreti_popl_ds) {
594 frame->tf_eip = (int)doreti_popl_ds_fault;
597 if (frame->tf_eip == (int)doreti_popl_es) {
598 frame->tf_eip = (int)doreti_popl_es_fault;
601 if (frame->tf_eip == (int)doreti_popl_fs) {
602 frame->tf_eip = (int)doreti_popl_fs_fault;
605 if (curpcb->pcb_onfault != NULL) {
606 frame->tf_eip = (int)curpcb->pcb_onfault;
613 * PSL_NT can be set in user mode and isn't cleared
614 * automatically when the kernel is entered. This
615 * causes a TSS fault when the kernel attempts to
616 * `iret' because the TSS link is uninitialized. We
617 * want to get this fault so that we can fix the
618 * problem here and not every time the kernel is
621 if (frame->tf_eflags & PSL_NT) {
622 frame->tf_eflags &= ~PSL_NT;
627 case T_TRCTRAP: /* trace trap */
629 if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
631 * We've just entered system mode via the
632 * syscall lcall. Continue single stepping
633 * silently until the syscall handler has
638 if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
640 * The syscall handler has now saved the
641 * flags. Stop single stepping it.
643 frame->tf_eflags &= ~PSL_T;
647 * Ignore debug register trace traps due to
648 * accesses in the user's address space, which
649 * can happen under several conditions such as
650 * if a user sets a watchpoint on a buffer and
651 * then passes that buffer to a system call.
652 * We still want to get TRCTRAPS for addresses
653 * in kernel space because that is useful when
654 * debugging the kernel.
656 if (user_dbreg_trap() &&
657 !(curpcb->pcb_flags & PCB_VM86CALL)) {
659 * Reset breakpoint bits because the
662 load_dr6(rdr6() & ~0xf);
666 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
670 * If KDB is enabled, let it handle the debugger trap.
671 * Otherwise, debugger traps "can't happen".
674 /* XXX %dr6 is not quite reentrant. */
676 load_dr6(dr6 & ~0x4000);
677 if (kdb_trap(type, dr6, frame))
685 if (time_second - lastalert > 10) {
686 log(LOG_WARNING, "NMI: power fail\n");
688 lastalert = time_second;
691 #else /* !POWERFAIL_NMI */
692 nmi_handle_intr(type, frame);
694 #endif /* POWERFAIL_NMI */
698 trap_fatal(frame, eva);
702 /* Translate fault for emulators (e.g. Linux) */
703 if (*p->p_sysent->sv_transtrap != NULL)
704 signo = (*p->p_sysent->sv_transtrap)(signo, type);
706 ksiginfo_init_trap(&ksi);
707 ksi.ksi_signo = signo;
708 ksi.ksi_code = ucode;
709 ksi.ksi_addr = (void *)addr;
710 ksi.ksi_trapno = type;
711 if (uprintf_signal) {
712 uprintf("pid %d comm %s: signal %d err %x code %d type %d "
713 "addr 0x%x esp 0x%08x eip 0x%08x "
714 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
715 p->p_pid, p->p_comm, signo, frame->tf_err, ucode, type,
716 addr, frame->tf_esp, frame->tf_eip,
717 fubyte((void *)(frame->tf_eip + 0)),
718 fubyte((void *)(frame->tf_eip + 1)),
719 fubyte((void *)(frame->tf_eip + 2)),
720 fubyte((void *)(frame->tf_eip + 3)),
721 fubyte((void *)(frame->tf_eip + 4)),
722 fubyte((void *)(frame->tf_eip + 5)),
723 fubyte((void *)(frame->tf_eip + 6)),
724 fubyte((void *)(frame->tf_eip + 7)));
726 KASSERT((read_eflags() & PSL_I) != 0, ("interrupts disabled"));
727 trapsignal(td, &ksi);
731 KASSERT(PCB_USER_FPU(td->td_pcb),
732 ("Return from trap with kernel FPU ctx leaked"));
736 trap_pfault(struct trapframe *frame, int usermode, vm_offset_t eva)
748 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
750 * Due to both processor errata and lazy TLB invalidation when
751 * access restrictions are removed from virtual pages, memory
752 * accesses that are allowed by the physical mapping layer may
753 * nonetheless cause one spurious page fault per virtual page.
754 * When the thread is executing a "no faulting" section that
755 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
756 * every page fault is treated as a spurious page fault,
757 * unless it accesses the same virtual address as the most
758 * recent page fault within the same "no faulting" section.
760 if (td->td_md.md_spurflt_addr != eva ||
761 (td->td_pflags & TDP_RESETSPUR) != 0) {
763 * Do nothing to the TLB. A stale TLB entry is
764 * flushed automatically by a page fault.
766 td->td_md.md_spurflt_addr = eva;
767 td->td_pflags &= ~TDP_RESETSPUR;
772 * If we get a page fault while in a critical section, then
773 * it is most likely a fatal kernel page fault. The kernel
774 * is already going to panic trying to get a sleep lock to
775 * do the VM lookup, so just consider it a fatal trap so the
776 * kernel can print out a useful trap message and even get
779 * If we get a page fault while holding a non-sleepable
780 * lock, then it is most likely a fatal kernel page fault.
781 * If WITNESS is enabled, then it's going to whine about
782 * bogus LORs with various VM locks, so just skip to the
783 * fatal trap handling directly.
785 if (td->td_critnest != 0 ||
786 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
787 "Kernel page fault") != 0) {
788 trap_fatal(frame, eva);
792 va = trunc_page(eva);
793 if (va >= KERNBASE) {
795 * Don't allow user-mode faults in kernel address space.
796 * An exception: if the faulting address is the invalid
797 * instruction entry in the IDT, then the Intel Pentium
798 * F00F bug workaround was triggered, and we need to
799 * treat it is as an illegal instruction, and not a page
802 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
803 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
811 map = &p->p_vmspace->vm_map;
814 * When accessing a user-space address, kernel must be
815 * ready to accept the page fault, and provide a
816 * handling routine. Since accessing the address
817 * without the handler is a bug, do not try to handle
818 * it normally, and panic immediately.
820 if (!usermode && (td->td_intr_nesting_level != 0 ||
821 curpcb->pcb_onfault == NULL)) {
822 trap_fatal(frame, eva);
828 * If the trap was caused by errant bits in the PTE then panic.
830 if (frame->tf_err & PGEX_RSV) {
831 trap_fatal(frame, eva);
836 * PGEX_I is defined only if the execute disable bit capability is
837 * supported and enabled.
839 if (frame->tf_err & PGEX_W)
840 ftype = VM_PROT_WRITE;
841 #if defined(PAE) || defined(PAE_TABLES)
842 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
843 ftype = VM_PROT_EXECUTE;
846 ftype = VM_PROT_READ;
848 /* Fault in the page. */
849 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
850 if (rv == KERN_SUCCESS) {
852 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
853 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
854 if (ftype == VM_PROT_READ)
855 PMC_SOFT_CALL_TF( , , page_fault, read,
858 PMC_SOFT_CALL_TF( , , page_fault, write,
865 if (td->td_intr_nesting_level == 0 &&
866 curpcb->pcb_onfault != NULL) {
867 frame->tf_eip = (int)curpcb->pcb_onfault;
870 trap_fatal(frame, eva);
873 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
877 trap_fatal(frame, eva)
878 struct trapframe *frame;
883 struct soft_segment_descriptor softseg;
886 code = frame->tf_err;
887 type = frame->tf_trapno;
888 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
890 if (type <= MAX_TRAP_MSG)
891 msg = trap_msg[type];
894 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
895 frame->tf_eflags & PSL_VM ? "vm86" :
896 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
898 /* two separate prints in case of a trap on an unmapped page */
899 printf("cpuid = %d; ", PCPU_GET(cpuid));
900 printf("apic id = %02x\n", PCPU_GET(apic_id));
902 if (type == T_PAGEFLT) {
903 printf("fault virtual address = 0x%x\n", eva);
904 printf("fault code = %s %s%s, %s\n",
905 code & PGEX_U ? "user" : "supervisor",
906 code & PGEX_W ? "write" : "read",
907 #if defined(PAE) || defined(PAE_TABLES)
909 (code & PGEX_I ? " instruction" : " data") :
912 code & PGEX_RSV ? "reserved bits in PTE" :
913 code & PGEX_P ? "protection violation" : "page not present");
915 printf("instruction pointer = 0x%x:0x%x\n",
916 frame->tf_cs & 0xffff, frame->tf_eip);
917 if (TF_HAS_STACKREGS(frame)) {
918 ss = frame->tf_ss & 0xffff;
921 ss = GSEL(GDATA_SEL, SEL_KPL);
922 esp = (int)&frame->tf_esp;
924 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
925 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
926 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
927 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
928 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
929 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
931 printf("processor eflags = ");
932 if (frame->tf_eflags & PSL_T)
933 printf("trace trap, ");
934 if (frame->tf_eflags & PSL_I)
935 printf("interrupt enabled, ");
936 if (frame->tf_eflags & PSL_NT)
937 printf("nested task, ");
938 if (frame->tf_eflags & PSL_RF)
940 if (frame->tf_eflags & PSL_VM)
942 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
943 printf("current process = %d (%s)\n",
944 curproc->p_pid, curthread->td_name);
947 if (debugger_on_panic || kdb_active) {
948 frame->tf_err = eva; /* smuggle fault address to ddb */
949 if (kdb_trap(type, 0, frame)) {
950 frame->tf_err = code; /* restore error code */
953 frame->tf_err = code; /* restore error code */
956 printf("trap number = %d\n", type);
957 if (type <= MAX_TRAP_MSG)
958 panic("%s", trap_msg[type]);
960 panic("unknown/reserved trap");
964 * Double fault handler. Called when a fault occurs while writing
965 * a frame for a trap/exception onto the stack. This usually occurs
966 * when the stack overflows (such is the case with infinite recursion,
969 * XXX Note that the current PTD gets replaced by IdlePTD when the
970 * task switch occurs. This means that the stack that was active at
971 * the time of the double fault is not available at <kstack> unless
972 * the machine was idle when the double fault occurred. The downside
973 * of this is that "trace <ebp>" in ddb won't work.
979 if (dtrace_doubletrap_func != NULL)
980 (*dtrace_doubletrap_func)();
982 printf("\nFatal double fault:\n");
983 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
984 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
985 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
987 /* two separate prints in case of a trap on an unmapped page */
988 printf("cpuid = %d; ", PCPU_GET(cpuid));
989 printf("apic id = %02x\n", PCPU_GET(apic_id));
991 panic("double fault");
995 cpu_fetch_syscall_args(struct thread *td)
998 struct trapframe *frame;
999 struct syscall_args *sa;
1005 frame = td->td_frame;
1008 params = (caddr_t)frame->tf_esp + sizeof(int);
1009 sa->code = frame->tf_eax;
1012 * Need to check if this is a 32 bit or 64 bit syscall.
1014 if (sa->code == SYS_syscall) {
1016 * Code is first argument, followed by actual args.
1018 error = fueword(params, &tmp);
1022 params += sizeof(int);
1023 } else if (sa->code == SYS___syscall) {
1025 * Like syscall, but code is a quad, so as to maintain
1026 * quad alignment for the rest of the arguments.
1028 error = fueword(params, &tmp);
1032 params += sizeof(quad_t);
1035 if (p->p_sysent->sv_mask)
1036 sa->code &= p->p_sysent->sv_mask;
1037 if (sa->code >= p->p_sysent->sv_size)
1038 sa->callp = &p->p_sysent->sv_table[0];
1040 sa->callp = &p->p_sysent->sv_table[sa->code];
1041 sa->narg = sa->callp->sy_narg;
1043 if (params != NULL && sa->narg != 0)
1044 error = copyin(params, (caddr_t)sa->args,
1045 (u_int)(sa->narg * sizeof(int)));
1050 td->td_retval[0] = 0;
1051 td->td_retval[1] = frame->tf_edx;
1057 #include "../../kern/subr_syscall.c"
1060 * syscall - system call request C handler. A system call is
1061 * essentially treated as a trap by reusing the frame layout.
1064 syscall(struct trapframe *frame)
1067 register_t orig_tf_eflags;
1072 if (!(TRAPF_USERMODE(frame) &&
1073 (curpcb->pcb_flags & PCB_VM86CALL) == 0)) {
1078 orig_tf_eflags = frame->tf_eflags;
1081 td->td_frame = frame;
1083 error = syscallenter(td);
1088 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1089 frame->tf_eflags &= ~PSL_T;
1090 ksiginfo_init_trap(&ksi);
1091 ksi.ksi_signo = SIGTRAP;
1092 ksi.ksi_code = TRAP_TRACE;
1093 ksi.ksi_addr = (void *)frame->tf_eip;
1094 trapsignal(td, &ksi);
1097 KASSERT(PCB_USER_FPU(td->td_pcb),
1098 ("System call %s returning with kernel FPU ctx leaked",
1099 syscallname(td->td_proc, td->td_sa.code)));
1100 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
1101 ("System call %s returning with mangled pcb_save",
1102 syscallname(td->td_proc, td->td_sa.code)));
1104 syscallret(td, error);