2 * Copyright (C) 1994, David Greenman
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the University of Utah, and William Jolitz.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: @(#)trap.c 7.4 (Berkeley) 5/13/91
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * 386 Trap and System call handling
47 #include "opt_clock.h"
49 #include "opt_hwpmc_hooks.h"
52 #include "opt_kdtrace.h"
56 #include <sys/param.h>
58 #include <sys/systm.h>
60 #include <sys/pioctl.h>
61 #include <sys/ptrace.h>
63 #include <sys/kernel.h>
66 #include <sys/mutex.h>
67 #include <sys/resourcevar.h>
68 #include <sys/signalvar.h>
69 #include <sys/syscall.h>
70 #include <sys/sysctl.h>
71 #include <sys/sysent.h>
73 #include <sys/vmmeter.h>
75 #include <sys/pmckern.h>
76 PMC_SOFT_DEFINE( , , page_fault, all);
77 PMC_SOFT_DEFINE( , , page_fault, read);
78 PMC_SOFT_DEFINE( , , page_fault, write);
80 #include <security/audit/audit.h>
83 #include <vm/vm_param.h>
85 #include <vm/vm_kern.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_extern.h>
90 #include <machine/cpu.h>
91 #include <machine/intr_machdep.h>
93 #include <machine/md_var.h>
94 #include <machine/pcb.h>
96 #include <machine/smp.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 * This is a hook which is initialised by the dtrace module
111 * to handle traps which might occur during DTrace probe
114 dtrace_trap_func_t dtrace_trap_func;
116 dtrace_doubletrap_func_t dtrace_doubletrap_func;
119 * This is a hook which is initialised by the systrace module
120 * when it is loaded. This keeps the DTrace syscall provider
121 * implementation opaque.
123 systrace_probe_func_t systrace_probe_func;
126 * These hooks are necessary for the pid, usdt and fasttrap providers.
128 dtrace_fasttrap_probe_ptr_t dtrace_fasttrap_probe_ptr;
129 dtrace_pid_probe_ptr_t dtrace_pid_probe_ptr;
130 dtrace_return_probe_ptr_t dtrace_return_probe_ptr;
133 extern void trap(struct trapframe *frame);
134 extern void syscall(struct trapframe *frame);
136 static int trap_pfault(struct trapframe *, int, vm_offset_t);
137 static void trap_fatal(struct trapframe *, vm_offset_t);
138 void dblfault_handler(void);
140 extern inthand_t IDTVEC(lcall_syscall);
142 #define MAX_TRAP_MSG 33
143 static char *trap_msg[] = {
145 "privileged instruction fault", /* 1 T_PRIVINFLT */
147 "breakpoint instruction fault", /* 3 T_BPTFLT */
150 "arithmetic trap", /* 6 T_ARITHTRAP */
153 "general protection fault", /* 9 T_PROTFLT */
154 "trace trap", /* 10 T_TRCTRAP */
156 "page fault", /* 12 T_PAGEFLT */
158 "alignment fault", /* 14 T_ALIGNFLT */
162 "integer divide fault", /* 18 T_DIVIDE */
163 "non-maskable interrupt trap", /* 19 T_NMI */
164 "overflow trap", /* 20 T_OFLOW */
165 "FPU bounds check fault", /* 21 T_BOUND */
166 "FPU device not available", /* 22 T_DNA */
167 "double fault", /* 23 T_DOUBLEFLT */
168 "FPU operand fetch fault", /* 24 T_FPOPFLT */
169 "invalid TSS fault", /* 25 T_TSSFLT */
170 "segment not present fault", /* 26 T_SEGNPFLT */
171 "stack fault", /* 27 T_STKFLT */
172 "machine check trap", /* 28 T_MCHK */
173 "SIMD floating-point exception", /* 29 T_XMMFLT */
174 "reserved (unknown) fault", /* 30 T_RESERVED */
175 "", /* 31 unused (reserved) */
176 "DTrace pid return trap", /* 32 T_DTRACE_RET */
177 "DTrace fasttrap probe trap", /* 33 T_DTRACE_PROBE */
181 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
182 extern int has_f00f_bug;
186 static int kdb_on_nmi = 1;
187 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
188 &kdb_on_nmi, 0, "Go to KDB on NMI");
189 TUNABLE_INT("machdep.kdb_on_nmi", &kdb_on_nmi);
191 static int panic_on_nmi = 1;
192 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
193 &panic_on_nmi, 0, "Panic on NMI");
194 TUNABLE_INT("machdep.panic_on_nmi", &panic_on_nmi);
195 static int prot_fault_translation = 0;
196 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
197 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
198 static int uprintf_signal;
199 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RW,
201 "Print debugging information on trap signal to ctty");
204 * Exception, fault, and trap interface to the FreeBSD kernel.
205 * This common code is called from assembly language IDT gate entry
206 * routines that prepare a suitable stack frame, and restore this
207 * frame after the exception has been processed.
211 trap(struct trapframe *frame)
213 struct thread *td = curthread;
214 struct proc *p = td->td_proc;
215 int i = 0, ucode = 0, code;
221 static int lastalert = 0;
224 PCPU_INC(cnt.v_trap);
225 type = frame->tf_trapno;
228 /* Handler for NMI IPIs used for stopping CPUs. */
230 if (ipi_nmi_handler() == 0)
242 if (type == T_RESERVED) {
243 trap_fatal(frame, 0);
249 * CPU PMCs interrupt using an NMI so we check for that first.
250 * If the HWPMC module is active, 'pmc_hook' will point to
251 * the function to be called. A return value of '1' from the
252 * hook means that the NMI was handled by it and that we can
253 * return immediately.
255 if (type == T_NMI && pmc_intr &&
256 (*pmc_intr)(PCPU_GET(cpuid), frame))
260 if (type == T_MCHK) {
267 * A trap can occur while DTrace executes a probe. Before
268 * executing the probe, DTrace blocks re-scheduling and sets
269 * a flag in it's per-cpu flags to indicate that it doesn't
270 * want to fault. On returning from the probe, the no-fault
271 * flag is cleared and finally re-scheduling is enabled.
273 * If the DTrace kernel module has registered a trap handler,
274 * call it and if it returns non-zero, assume that it has
275 * handled the trap and modified the trap frame so that this
276 * function can return normally.
278 if (type == T_DTRACE_PROBE || type == T_DTRACE_RET ||
282 fill_frame_regs(frame, ®s);
283 if (type == T_DTRACE_PROBE &&
284 dtrace_fasttrap_probe_ptr != NULL &&
285 dtrace_fasttrap_probe_ptr(®s) == 0)
287 if (type == T_BPTFLT &&
288 dtrace_pid_probe_ptr != NULL &&
289 dtrace_pid_probe_ptr(®s) == 0)
291 if (type == T_DTRACE_RET &&
292 dtrace_return_probe_ptr != NULL &&
293 dtrace_return_probe_ptr(®s) == 0)
296 if ((type == T_PROTFLT || type == T_PAGEFLT) &&
297 dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
301 if ((frame->tf_eflags & PSL_I) == 0) {
303 * Buggy application or kernel code has disabled
304 * interrupts and then trapped. Enabling interrupts
305 * now is wrong, but it is better than running with
306 * interrupts disabled until they are accidentally
309 if (ISPL(frame->tf_cs) == SEL_UPL || (frame->tf_eflags & PSL_VM))
311 "pid %ld (%s): trap %d with interrupts disabled\n",
312 (long)curproc->p_pid, curthread->td_name, type);
313 else if (type != T_BPTFLT && type != T_TRCTRAP &&
314 frame->tf_eip != (int)cpu_switch_load_gs) {
316 * XXX not quite right, since this may be for a
317 * multiple fault in user mode.
319 printf("kernel trap %d with interrupts disabled\n",
322 * Page faults need interrupts disabled until later,
323 * and we shouldn't enable interrupts while holding
324 * a spin lock or if servicing an NMI.
326 if (type != T_NMI && type != T_PAGEFLT &&
327 td->td_md.md_spinlock_count == 0)
332 code = frame->tf_err;
333 if (type == T_PAGEFLT) {
335 * For some Cyrix CPUs, %cr2 is clobbered by
336 * interrupts. This problem is worked around by using
337 * an interrupt gate for the pagefault handler. We
338 * are finally ready to read %cr2 and conditionally
339 * reenable interrupts. If we hold a spin lock, then
340 * we must not reenable interrupts. This might be a
341 * spurious page fault.
344 if (td->td_md.md_spinlock_count == 0)
348 if ((ISPL(frame->tf_cs) == SEL_UPL) ||
349 ((frame->tf_eflags & PSL_VM) &&
350 !(curpcb->pcb_flags & PCB_VM86CALL))) {
354 td->td_frame = frame;
355 addr = frame->tf_eip;
356 if (td->td_ucred != p->p_ucred)
357 cred_update_thread(td);
360 case T_PRIVINFLT: /* privileged instruction fault */
365 case T_BPTFLT: /* bpt instruction fault */
366 case T_TRCTRAP: /* trace trap */
368 frame->tf_eflags &= ~PSL_T;
370 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
373 case T_ARITHTRAP: /* arithmetic trap */
375 ucode = npxtrap_x87();
385 * The following two traps can happen in
386 * vm86 mode, and, if so, we want to handle
389 case T_PROTFLT: /* general protection fault */
390 case T_STKFLT: /* stack fault */
391 if (frame->tf_eflags & PSL_VM) {
392 i = vm86_emulate((struct vm86frame *)frame);
398 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
400 case T_SEGNPFLT: /* segment not present fault */
404 case T_TSSFLT: /* invalid TSS fault */
408 case T_DOUBLEFLT: /* double fault */
414 case T_PAGEFLT: /* page fault */
416 i = trap_pfault(frame, TRUE, eva);
417 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
420 * The f00f hack workaround has triggered, so
421 * treat the fault as an illegal instruction
422 * (T_PRIVINFLT) instead of a page fault.
424 type = frame->tf_trapno = T_PRIVINFLT;
426 /* Proceed as in that case. */
440 if (prot_fault_translation == 0) {
443 * This check also covers the images
444 * without the ABI-tag ELF note.
446 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
447 && p->p_osrel >= P_OSREL_SIGSEGV) {
452 ucode = BUS_PAGE_FAULT;
454 } else if (prot_fault_translation == 1) {
456 * Always compat mode.
459 ucode = BUS_PAGE_FAULT;
462 * Always SIGSEGV mode.
471 case T_DIVIDE: /* integer divide fault */
480 # define TIMER_FREQ 1193182
482 if (time_second - lastalert > 10) {
483 log(LOG_WARNING, "NMI: power fail\n");
485 lastalert = time_second;
488 #else /* !POWERFAIL_NMI */
489 /* machine/parity/power fail/"kitchen sink" faults */
490 if (isa_nmi(code) == 0) {
493 * NMI can be hooked up to a pushbutton
497 printf ("NMI ... going to debugger\n");
498 kdb_trap(type, 0, frame);
502 } else if (panic_on_nmi)
503 panic("NMI indicates hardware failure");
505 #endif /* POWERFAIL_NMI */
508 case T_OFLOW: /* integer overflow fault */
513 case T_BOUND: /* bounds check fault */
520 KASSERT(PCB_USER_FPU(td->td_pcb),
521 ("kernel FPU ctx has leaked"));
522 /* transparent fault (due to context switch "late") */
526 uprintf("pid %d killed due to lack of floating point\n",
532 case T_FPOPFLT: /* FPU operand fetch fault */
537 case T_XMMFLT: /* SIMD floating-point exception */
538 #if defined(DEV_NPX) && !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
539 ucode = npxtrap_sse();
551 KASSERT(cold || td->td_ucred != NULL,
552 ("kernel trap doesn't have ucred"));
554 case T_PAGEFLT: /* page fault */
555 (void) trap_pfault(frame, FALSE, eva);
560 KASSERT(!PCB_USER_FPU(td->td_pcb),
561 ("Unregistered use of FPU in kernel"));
567 case T_ARITHTRAP: /* arithmetic trap */
568 case T_XMMFLT: /* SIMD floating-point exception */
569 case T_FPOPFLT: /* FPU operand fetch fault */
571 * XXXKIB for now disable any FPU traps in kernel
572 * handler registration seems to be overkill
574 trap_fatal(frame, 0);
578 * The following two traps can happen in
579 * vm86 mode, and, if so, we want to handle
582 case T_PROTFLT: /* general protection fault */
583 case T_STKFLT: /* stack fault */
584 if (frame->tf_eflags & PSL_VM) {
585 i = vm86_emulate((struct vm86frame *)frame);
588 * returns to original process
590 vm86_trap((struct vm86frame *)frame);
593 if (type == T_STKFLT)
598 case T_SEGNPFLT: /* segment not present fault */
599 if (curpcb->pcb_flags & PCB_VM86CALL)
603 * Invalid %fs's and %gs's can be created using
604 * procfs or PT_SETREGS or by invalidating the
605 * underlying LDT entry. This causes a fault
606 * in kernel mode when the kernel attempts to
607 * switch contexts. Lose the bad context
608 * (XXX) so that we can continue, and generate
611 if (frame->tf_eip == (int)cpu_switch_load_gs) {
615 kern_psignal(p, SIGBUS);
621 if (td->td_intr_nesting_level != 0)
625 * Invalid segment selectors and out of bounds
626 * %eip's and %esp's can be set up in user mode.
627 * This causes a fault in kernel mode when the
628 * kernel tries to return to user mode. We want
629 * to get this fault so that we can fix the
630 * problem here and not have to check all the
631 * selectors and pointers when the user changes
634 if (frame->tf_eip == (int)doreti_iret) {
635 frame->tf_eip = (int)doreti_iret_fault;
638 if (frame->tf_eip == (int)doreti_popl_ds) {
639 frame->tf_eip = (int)doreti_popl_ds_fault;
642 if (frame->tf_eip == (int)doreti_popl_es) {
643 frame->tf_eip = (int)doreti_popl_es_fault;
646 if (frame->tf_eip == (int)doreti_popl_fs) {
647 frame->tf_eip = (int)doreti_popl_fs_fault;
650 if (curpcb->pcb_onfault != NULL) {
652 (int)curpcb->pcb_onfault;
659 * PSL_NT can be set in user mode and isn't cleared
660 * automatically when the kernel is entered. This
661 * causes a TSS fault when the kernel attempts to
662 * `iret' because the TSS link is uninitialized. We
663 * want to get this fault so that we can fix the
664 * problem here and not every time the kernel is
667 if (frame->tf_eflags & PSL_NT) {
668 frame->tf_eflags &= ~PSL_NT;
673 case T_TRCTRAP: /* trace trap */
674 if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
676 * We've just entered system mode via the
677 * syscall lcall. Continue single stepping
678 * silently until the syscall handler has
683 if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
685 * The syscall handler has now saved the
686 * flags. Stop single stepping it.
688 frame->tf_eflags &= ~PSL_T;
692 * Ignore debug register trace traps due to
693 * accesses in the user's address space, which
694 * can happen under several conditions such as
695 * if a user sets a watchpoint on a buffer and
696 * then passes that buffer to a system call.
697 * We still want to get TRCTRAPS for addresses
698 * in kernel space because that is useful when
699 * debugging the kernel.
701 if (user_dbreg_trap() &&
702 !(curpcb->pcb_flags & PCB_VM86CALL)) {
704 * Reset breakpoint bits because the
707 load_dr6(rdr6() & 0xfffffff0);
711 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
715 * If KDB is enabled, let it handle the debugger trap.
716 * Otherwise, debugger traps "can't happen".
719 if (kdb_trap(type, 0, frame))
727 if (time_second - lastalert > 10) {
728 log(LOG_WARNING, "NMI: power fail\n");
730 lastalert = time_second;
733 #else /* !POWERFAIL_NMI */
734 /* machine/parity/power fail/"kitchen sink" faults */
735 if (isa_nmi(code) == 0) {
738 * NMI can be hooked up to a pushbutton
742 printf ("NMI ... going to debugger\n");
743 kdb_trap(type, 0, frame);
747 } else if (panic_on_nmi == 0)
750 #endif /* POWERFAIL_NMI */
754 trap_fatal(frame, eva);
758 /* Translate fault for emulators (e.g. Linux) */
759 if (*p->p_sysent->sv_transtrap)
760 i = (*p->p_sysent->sv_transtrap)(i, type);
762 ksiginfo_init_trap(&ksi);
764 ksi.ksi_code = ucode;
765 ksi.ksi_addr = (void *)addr;
766 ksi.ksi_trapno = type;
767 if (uprintf_signal) {
768 uprintf("pid %d comm %s: signal %d err %x code %d type %d "
769 "addr 0x%x esp 0x%08x eip 0x%08x "
770 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
771 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr,
772 frame->tf_esp, frame->tf_eip,
773 fubyte((void *)(frame->tf_eip + 0)),
774 fubyte((void *)(frame->tf_eip + 1)),
775 fubyte((void *)(frame->tf_eip + 2)),
776 fubyte((void *)(frame->tf_eip + 3)),
777 fubyte((void *)(frame->tf_eip + 4)),
778 fubyte((void *)(frame->tf_eip + 5)),
779 fubyte((void *)(frame->tf_eip + 6)),
780 fubyte((void *)(frame->tf_eip + 7)));
782 KASSERT((read_eflags() & PSL_I) != 0, ("interrupts disabled"));
783 trapsignal(td, &ksi);
786 if (type <= MAX_TRAP_MSG) {
787 uprintf("fatal process exception: %s",
789 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
790 uprintf(", fault VA = 0x%lx", (u_long)eva);
797 mtx_assert(&Giant, MA_NOTOWNED);
798 KASSERT(PCB_USER_FPU(td->td_pcb),
799 ("Return from trap with kernel FPU ctx leaked"));
806 trap_pfault(frame, usermode, eva)
807 struct trapframe *frame;
812 struct vmspace *vm = NULL;
816 struct thread *td = curthread;
817 struct proc *p = td->td_proc;
819 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
821 * Due to both processor errata and lazy TLB invalidation when
822 * access restrictions are removed from virtual pages, memory
823 * accesses that are allowed by the physical mapping layer may
824 * nonetheless cause one spurious page fault per virtual page.
825 * When the thread is executing a "no faulting" section that
826 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
827 * every page fault is treated as a spurious page fault,
828 * unless it accesses the same virtual address as the most
829 * recent page fault within the same "no faulting" section.
831 if (td->td_md.md_spurflt_addr != eva ||
832 (td->td_pflags & TDP_RESETSPUR) != 0) {
834 * Do nothing to the TLB. A stale TLB entry is
835 * flushed automatically by a page fault.
837 td->td_md.md_spurflt_addr = eva;
838 td->td_pflags &= ~TDP_RESETSPUR;
843 * If we get a page fault while in a critical section, then
844 * it is most likely a fatal kernel page fault. The kernel
845 * is already going to panic trying to get a sleep lock to
846 * do the VM lookup, so just consider it a fatal trap so the
847 * kernel can print out a useful trap message and even get
850 * If we get a page fault while holding a non-sleepable
851 * lock, then it is most likely a fatal kernel page fault.
852 * If WITNESS is enabled, then it's going to whine about
853 * bogus LORs with various VM locks, so just skip to the
854 * fatal trap handling directly.
856 if (td->td_critnest != 0 ||
857 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
858 "Kernel page fault") != 0) {
859 trap_fatal(frame, eva);
863 va = trunc_page(eva);
864 if (va >= KERNBASE) {
866 * Don't allow user-mode faults in kernel address space.
867 * An exception: if the faulting address is the invalid
868 * instruction entry in the IDT, then the Intel Pentium
869 * F00F bug workaround was triggered, and we need to
870 * treat it is as an illegal instruction, and not a page
873 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
874 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
883 * This is a fault on non-kernel virtual memory.
884 * vm is initialized above to NULL. If curproc is NULL
885 * or curproc->p_vmspace is NULL the fault is fatal.
894 if (!usermode && (td->td_intr_nesting_level != 0 ||
895 curpcb->pcb_onfault == NULL)) {
896 trap_fatal(frame, eva);
902 * PGEX_I is defined only if the execute disable bit capability is
903 * supported and enabled.
905 if (frame->tf_err & PGEX_W)
906 ftype = VM_PROT_WRITE;
908 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
909 ftype = VM_PROT_EXECUTE;
912 ftype = VM_PROT_READ;
914 if (map != kernel_map) {
916 * Keep swapout from messing with us during this
923 /* Fault in the user page: */
924 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
931 * Don't have to worry about process locking or stacks in the
934 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
936 if (rv == KERN_SUCCESS) {
938 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
939 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
940 if (ftype == VM_PROT_READ)
941 PMC_SOFT_CALL_TF( , , page_fault, read,
944 PMC_SOFT_CALL_TF( , , page_fault, write,
952 if (td->td_intr_nesting_level == 0 &&
953 curpcb->pcb_onfault != NULL) {
954 frame->tf_eip = (int)curpcb->pcb_onfault;
957 trap_fatal(frame, eva);
961 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
965 trap_fatal(frame, eva)
966 struct trapframe *frame;
971 struct soft_segment_descriptor softseg;
974 code = frame->tf_err;
975 type = frame->tf_trapno;
976 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
978 if (type <= MAX_TRAP_MSG)
979 msg = trap_msg[type];
982 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
983 frame->tf_eflags & PSL_VM ? "vm86" :
984 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
986 /* two separate prints in case of a trap on an unmapped page */
987 printf("cpuid = %d; ", PCPU_GET(cpuid));
988 printf("apic id = %02x\n", PCPU_GET(apic_id));
990 if (type == T_PAGEFLT) {
991 printf("fault virtual address = 0x%x\n", eva);
992 printf("fault code = %s %s, %s\n",
993 code & PGEX_U ? "user" : "supervisor",
994 code & PGEX_W ? "write" : "read",
995 code & PGEX_P ? "protection violation" : "page not present");
997 printf("instruction pointer = 0x%x:0x%x\n",
998 frame->tf_cs & 0xffff, frame->tf_eip);
999 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
1000 ss = frame->tf_ss & 0xffff;
1001 esp = frame->tf_esp;
1003 ss = GSEL(GDATA_SEL, SEL_KPL);
1004 esp = (int)&frame->tf_esp;
1006 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
1007 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
1008 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
1009 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
1010 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
1011 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
1013 printf("processor eflags = ");
1014 if (frame->tf_eflags & PSL_T)
1015 printf("trace trap, ");
1016 if (frame->tf_eflags & PSL_I)
1017 printf("interrupt enabled, ");
1018 if (frame->tf_eflags & PSL_NT)
1019 printf("nested task, ");
1020 if (frame->tf_eflags & PSL_RF)
1022 if (frame->tf_eflags & PSL_VM)
1024 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
1025 printf("current process = ");
1027 printf("%lu (%s)\n", (u_long)curproc->p_pid, curthread->td_name);
1033 if (debugger_on_panic || kdb_active) {
1034 frame->tf_err = eva; /* smuggle fault address to ddb */
1035 if (kdb_trap(type, 0, frame)) {
1036 frame->tf_err = code; /* restore error code */
1039 frame->tf_err = code; /* restore error code */
1042 printf("trap number = %d\n", type);
1043 if (type <= MAX_TRAP_MSG)
1044 panic("%s", trap_msg[type]);
1046 panic("unknown/reserved trap");
1050 * Double fault handler. Called when a fault occurs while writing
1051 * a frame for a trap/exception onto the stack. This usually occurs
1052 * when the stack overflows (such is the case with infinite recursion,
1055 * XXX Note that the current PTD gets replaced by IdlePTD when the
1056 * task switch occurs. This means that the stack that was active at
1057 * the time of the double fault is not available at <kstack> unless
1058 * the machine was idle when the double fault occurred. The downside
1059 * of this is that "trace <ebp>" in ddb won't work.
1064 #ifdef KDTRACE_HOOKS
1065 if (dtrace_doubletrap_func != NULL)
1066 (*dtrace_doubletrap_func)();
1068 printf("\nFatal double fault:\n");
1069 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
1070 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
1071 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
1073 /* two separate prints in case of a trap on an unmapped page */
1074 printf("cpuid = %d; ", PCPU_GET(cpuid));
1075 printf("apic id = %02x\n", PCPU_GET(apic_id));
1077 panic("double fault");
1081 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
1084 struct trapframe *frame;
1089 frame = td->td_frame;
1091 params = (caddr_t)frame->tf_esp + sizeof(int);
1092 sa->code = frame->tf_eax;
1095 * Need to check if this is a 32 bit or 64 bit syscall.
1097 if (sa->code == SYS_syscall) {
1099 * Code is first argument, followed by actual args.
1101 sa->code = fuword(params);
1102 params += sizeof(int);
1103 } else if (sa->code == SYS___syscall) {
1105 * Like syscall, but code is a quad, so as to maintain
1106 * quad alignment for the rest of the arguments.
1108 sa->code = fuword(params);
1109 params += sizeof(quad_t);
1112 if (p->p_sysent->sv_mask)
1113 sa->code &= p->p_sysent->sv_mask;
1114 if (sa->code >= p->p_sysent->sv_size)
1115 sa->callp = &p->p_sysent->sv_table[0];
1117 sa->callp = &p->p_sysent->sv_table[sa->code];
1118 sa->narg = sa->callp->sy_narg;
1120 if (params != NULL && sa->narg != 0)
1121 error = copyin(params, (caddr_t)sa->args,
1122 (u_int)(sa->narg * sizeof(int)));
1127 td->td_retval[0] = 0;
1128 td->td_retval[1] = frame->tf_edx;
1134 #include "../../kern/subr_syscall.c"
1137 * syscall - system call request C handler. A system call is
1138 * essentially treated as a trap by reusing the frame layout.
1141 syscall(struct trapframe *frame)
1144 struct syscall_args sa;
1145 register_t orig_tf_eflags;
1150 if (ISPL(frame->tf_cs) != SEL_UPL) {
1155 orig_tf_eflags = frame->tf_eflags;
1158 td->td_frame = frame;
1160 error = syscallenter(td, &sa);
1165 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1166 frame->tf_eflags &= ~PSL_T;
1167 ksiginfo_init_trap(&ksi);
1168 ksi.ksi_signo = SIGTRAP;
1169 ksi.ksi_code = TRAP_TRACE;
1170 ksi.ksi_addr = (void *)frame->tf_eip;
1171 trapsignal(td, &ksi);
1174 KASSERT(PCB_USER_FPU(td->td_pcb),
1175 ("System call %s returning with kernel FPU ctx leaked",
1176 syscallname(td->td_proc, sa.code)));
1177 KASSERT(td->td_pcb->pcb_save == &td->td_pcb->pcb_user_save,
1178 ("System call %s returning with mangled pcb_save",
1179 syscallname(td->td_proc, sa.code)));
1181 syscallret(td, error, &sa);