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
38 * $FreeBSD: src/sys/i386/i386/trap.c,v 1.147.2.11 2003/02/27 19:09:59 luoqi Exp $
39 * $DragonFly: src/sys/platform/pc32/i386/trap.c,v 1.14 2003/07/01 18:49:52 dillon Exp $
43 * 386 Trap and System call handling
48 #include "opt_ktrace.h"
49 #include "opt_clock.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
55 #include <sys/pioctl.h>
56 #include <sys/kernel.h>
57 #include <sys/resourcevar.h>
58 #include <sys/signalvar.h>
59 #include <sys/syscall.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysent.h>
63 #include <sys/vmmeter.h>
65 #include <sys/ktrace.h>
69 #include <vm/vm_param.h>
72 #include <vm/vm_kern.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_page.h>
75 #include <vm/vm_extern.h>
77 #include <machine/cpu.h>
78 #include <machine/ipl.h>
79 #include <machine/md_var.h>
80 #include <machine/pcb.h>
82 #include <machine/smp.h>
84 #include <machine/tss.h>
85 #include <machine/globaldata.h>
87 #include <i386/isa/intr_machdep.h>
90 #include <sys/syslog.h>
91 #include <machine/clock.h>
94 #include <machine/vm86.h>
97 #include <sys/thread2.h>
102 int (*pmath_emulate) __P((struct trapframe *));
104 extern void trap __P((struct trapframe frame));
105 extern int trapwrite __P((unsigned addr));
106 extern void syscall2 __P((struct trapframe frame));
108 static int trap_pfault __P((struct trapframe *, int, vm_offset_t));
109 static void trap_fatal __P((struct trapframe *, vm_offset_t));
110 void dblfault_handler __P((void));
112 extern inthand_t IDTVEC(syscall);
114 #define MAX_TRAP_MSG 28
115 static char *trap_msg[] = {
117 "privileged instruction fault", /* 1 T_PRIVINFLT */
119 "breakpoint instruction fault", /* 3 T_BPTFLT */
122 "arithmetic trap", /* 6 T_ARITHTRAP */
123 "system forced exception", /* 7 T_ASTFLT */
125 "general protection fault", /* 9 T_PROTFLT */
126 "trace trap", /* 10 T_TRCTRAP */
128 "page fault", /* 12 T_PAGEFLT */
130 "alignment fault", /* 14 T_ALIGNFLT */
134 "integer divide fault", /* 18 T_DIVIDE */
135 "non-maskable interrupt trap", /* 19 T_NMI */
136 "overflow trap", /* 20 T_OFLOW */
137 "FPU bounds check fault", /* 21 T_BOUND */
138 "FPU device not available", /* 22 T_DNA */
139 "double fault", /* 23 T_DOUBLEFLT */
140 "FPU operand fetch fault", /* 24 T_FPOPFLT */
141 "invalid TSS fault", /* 25 T_TSSFLT */
142 "segment not present fault", /* 26 T_SEGNPFLT */
143 "stack fault", /* 27 T_STKFLT */
144 "machine check trap", /* 28 T_MCHK */
147 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
148 extern int has_f00f_bug;
152 static int ddb_on_nmi = 1;
153 SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
154 &ddb_on_nmi, 0, "Go to DDB on NMI");
156 static int panic_on_nmi = 1;
157 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
158 &panic_on_nmi, 0, "Panic on NMI");
161 * USER->KERNEL transition. Do not transition us out of userland from the
162 * point of view of the userland scheduler unless we actually have to
165 * usertdsw is called from within a critical section.
168 usertdsw(struct thread *ntd)
170 struct thread *td = curthread;
172 td->td_switch = cpu_heavy_switch;
173 lwkt_setpri_self(TDPRI_KERN_USER);
176 * This is where we might want to catch the P_CURPROC designation
177 * and fix it for *any* switchout rather then just an mi_switch()
178 * switchout (move from mi_switch()?) YYY
180 if (p->p_flag & P_CURPROC) {
188 * Note that userenter() may be re-entered several times due to AST
194 struct thread *td = curthread;
196 KASSERT(td->td_switch == cpu_heavy_switch || td->td_switch == usertdsw,
197 ("userenter: bad td_switch = %p", td->td_switch));
198 td->td_switch = usertdsw;
202 userret(struct proc *p, struct trapframe *frame,
203 u_quad_t oticks, int have_mplock)
206 struct thread *td = curthread;
209 * Post any pending signals
212 while ((sig = CURSIG(p)) != 0) {
213 if (have_mplock == 0) {
223 * Set our priority properly and restore our switch function. If
224 * we did not hit our lazy switch function in the first place we
225 * do not need to restore anything.
227 if (td->td_switch == cpu_heavy_switch) {
228 switch(p->p_rtprio.type) {
230 lwkt_setpri_self(TDPRI_USER_IDLE);
232 case RTP_PRIO_REALTIME:
234 lwkt_setpri_self(TDPRI_USER_REAL);
237 lwkt_setpri_self(TDPRI_USER_NORM);
241 KKASSERT(td->td_switch == usertdsw);
242 td->td_switch = cpu_heavy_switch;
247 * If a reschedule has been requested we call chooseproc() to locate
248 * the next runnable process. When we wakeup from that we check
249 * for pending signals again.
251 if (resched_wanted()) {
253 if (have_mplock == 0) {
257 while ((sig = CURSIG(p)) != 0)
262 * Charge system time if profiling.
264 if (p->p_flag & P_PROFIL) {
265 if (have_mplock == 0) {
269 addupc_task(p, frame->tf_eip,
270 (u_int)(curthread->td_sticks - oticks) * psratio);
274 * In order to return to userland we need to be the designated
275 * current (user) process on this cpu. We have to wait for
276 * the userland scheduler to schedule as P_CURPROC.
279 while ((p->p_flag & P_CURPROC) == 0) {
280 p->p_stats->p_ru.ru_nivcsw++;
281 lwkt_deschedule_self();
285 KKASSERT(mycpu->gd_uprocscheduled == 1);
290 #ifdef DEVICE_POLLING
291 extern u_int32_t poll_in_trap;
292 extern int ether_poll __P((int count));
293 #endif /* DEVICE_POLLING */
296 * Exception, fault, and trap interface to the FreeBSD kernel.
297 * This common code is called from assembly language IDT gate entry
298 * routines that prepare a suitable stack frame, and restore this
299 * frame after the exception has been processed.
304 struct trapframe frame;
306 struct proc *p = curproc;
308 int i = 0, ucode = 0, type, code;
313 eva = (frame.tf_trapno == T_PAGEFLT ? rcr2() : 0);
314 trap_fatal(&frame, eva);
319 if (!(frame.tf_eflags & PSL_I)) {
321 * Buggy application or kernel code has disabled interrupts
322 * and then trapped. Enabling interrupts now is wrong, but
323 * it is better than running with interrupts disabled until
324 * they are accidentally enabled later.
326 type = frame.tf_trapno;
327 if (ISPL(frame.tf_cs) == SEL_UPL || (frame.tf_eflags & PSL_VM))
329 "pid %ld (%s): trap %d with interrupts disabled\n",
330 (long)curproc->p_pid, curproc->p_comm, type);
331 else if (type != T_BPTFLT && type != T_TRCTRAP)
333 * XXX not quite right, since this may be for a
334 * multiple fault in user mode.
336 printf("kernel trap %d with interrupts disabled\n",
342 if (frame.tf_trapno == T_PAGEFLT) {
344 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
345 * This problem is worked around by using an interrupt
346 * gate for the pagefault handler. We are finally ready
347 * to read %cr2 and then must reenable interrupts.
349 * XXX this should be in the switch statement, but the
350 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
351 * flow of control too much for this to be obviously
358 #ifdef DEVICE_POLLING
360 ether_poll(poll_in_trap);
361 #endif /* DEVICE_POLLING */
363 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
366 type = frame.tf_trapno;
370 if (frame.tf_eflags & PSL_VM &&
371 (type == T_PROTFLT || type == T_STKFLT)) {
372 i = vm86_emulate((struct vm86frame *)&frame);
375 * returns to original process
377 vm86_trap((struct vm86frame *)&frame);
382 * these traps want either a process context, or
383 * assume a normal userspace trap.
387 trap_fatal(&frame, eva);
390 type = T_BPTFLT; /* kernel breakpoint */
393 goto kernel_trap; /* normal kernel trap handling */
396 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
401 sticks = curthread->td_sticks;
402 p->p_md.md_regs = &frame;
405 case T_PRIVINFLT: /* privileged instruction fault */
410 case T_BPTFLT: /* bpt instruction fault */
411 case T_TRCTRAP: /* trace trap */
412 frame.tf_eflags &= ~PSL_T;
416 case T_ARITHTRAP: /* arithmetic trap */
421 case T_ASTFLT: /* Allow process switch */
424 if (p->p_flag & P_OWEUPC) {
425 p->p_flag &= ~P_OWEUPC;
426 addupc_task(p, p->p_stats->p_prof.pr_addr,
427 p->p_stats->p_prof.pr_ticks);
432 * The following two traps can happen in
433 * vm86 mode, and, if so, we want to handle
436 case T_PROTFLT: /* general protection fault */
437 case T_STKFLT: /* stack fault */
438 if (frame.tf_eflags & PSL_VM) {
439 i = vm86_emulate((struct vm86frame *)&frame);
446 case T_SEGNPFLT: /* segment not present fault */
447 case T_TSSFLT: /* invalid TSS fault */
448 case T_DOUBLEFLT: /* double fault */
450 ucode = code + BUS_SEGM_FAULT ;
454 case T_PAGEFLT: /* page fault */
455 i = trap_pfault(&frame, TRUE, eva);
458 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
468 case T_DIVIDE: /* integer divide fault */
476 goto handle_powerfail;
477 #else /* !POWERFAIL_NMI */
478 /* machine/parity/power fail/"kitchen sink" faults */
479 if (isa_nmi(code) == 0) {
482 * NMI can be hooked up to a pushbutton
486 printf ("NMI ... going to debugger\n");
487 kdb_trap (type, 0, &frame);
491 } else if (panic_on_nmi)
492 panic("NMI indicates hardware failure");
494 #endif /* POWERFAIL_NMI */
495 #endif /* NISA > 0 */
497 case T_OFLOW: /* integer overflow fault */
502 case T_BOUND: /* bounds check fault */
509 /* if a transparent fault (due to context switch "late") */
513 if (!pmath_emulate) {
515 ucode = FPE_FPU_NP_TRAP;
518 i = (*pmath_emulate)(&frame);
520 if (!(frame.tf_eflags & PSL_T))
522 frame.tf_eflags &= ~PSL_T;
525 /* else ucode = emulator_only_knows() XXX */
528 case T_FPOPFLT: /* FPU operand fetch fault */
533 case T_XMMFLT: /* SIMD floating-point exception */
543 case T_PAGEFLT: /* page fault */
544 (void) trap_pfault(&frame, FALSE, eva);
550 * The kernel is apparently using npx for copying.
551 * XXX this should be fatal unless the kernel has
552 * registered such use.
559 case T_PROTFLT: /* general protection fault */
560 case T_SEGNPFLT: /* segment not present fault */
562 * Invalid segment selectors and out of bounds
563 * %eip's and %esp's can be set up in user mode.
564 * This causes a fault in kernel mode when the
565 * kernel tries to return to user mode. We want
566 * to get this fault so that we can fix the
567 * problem here and not have to check all the
568 * selectors and pointers when the user changes
571 #define MAYBE_DORETI_FAULT(where, whereto) \
573 if (frame.tf_eip == (int)where) { \
574 frame.tf_eip = (int)whereto; \
579 if (mycpu->gd_intr_nesting_level == 0) {
581 * Invalid %fs's and %gs's can be created using
582 * procfs or PT_SETREGS or by invalidating the
583 * underlying LDT entry. This causes a fault
584 * in kernel mode when the kernel attempts to
585 * switch contexts. Lose the bad context
586 * (XXX) so that we can continue, and generate
589 if (frame.tf_eip == (int)cpu_switch_load_gs) {
590 curthread->td_pcb->pcb_gs = 0;
594 MAYBE_DORETI_FAULT(doreti_iret,
596 MAYBE_DORETI_FAULT(doreti_popl_ds,
597 doreti_popl_ds_fault);
598 MAYBE_DORETI_FAULT(doreti_popl_es,
599 doreti_popl_es_fault);
600 MAYBE_DORETI_FAULT(doreti_popl_fs,
601 doreti_popl_fs_fault);
602 if (curthread->td_pcb->pcb_onfault) {
603 frame.tf_eip = (int)curthread->td_pcb->pcb_onfault;
611 * PSL_NT can be set in user mode and isn't cleared
612 * automatically when the kernel is entered. This
613 * causes a TSS fault when the kernel attempts to
614 * `iret' because the TSS link is uninitialized. We
615 * want to get this fault so that we can fix the
616 * problem here and not every time the kernel is
619 if (frame.tf_eflags & PSL_NT) {
620 frame.tf_eflags &= ~PSL_NT;
625 case T_TRCTRAP: /* trace trap */
626 if (frame.tf_eip == (int)IDTVEC(syscall)) {
628 * We've just entered system mode via the
629 * syscall lcall. Continue single stepping
630 * silently until the syscall handler has
635 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
637 * The syscall handler has now saved the
638 * flags. Stop single stepping it.
640 frame.tf_eflags &= ~PSL_T;
644 * Ignore debug register trace traps due to
645 * accesses in the user's address space, which
646 * can happen under several conditions such as
647 * if a user sets a watchpoint on a buffer and
648 * then passes that buffer to a system call.
649 * We still want to get TRCTRAPS for addresses
650 * in kernel space because that is useful when
651 * debugging the kernel.
653 if (user_dbreg_trap()) {
655 * Reset breakpoint bits because the
658 load_dr6(rdr6() & 0xfffffff0);
662 * Fall through (TRCTRAP kernel mode, kernel address)
666 * If DDB is enabled, let it handle the debugger trap.
667 * Otherwise, debugger traps "can't happen".
670 if (kdb_trap (type, 0, &frame))
679 # define TIMER_FREQ 1193182
683 static unsigned lastalert = 0;
685 if(time_second - lastalert > 10)
687 log(LOG_WARNING, "NMI: power fail\n");
688 sysbeep(TIMER_FREQ/880, hz);
689 lastalert = time_second;
693 #else /* !POWERFAIL_NMI */
694 /* machine/parity/power fail/"kitchen sink" faults */
695 if (isa_nmi(code) == 0) {
698 * NMI can be hooked up to a pushbutton
702 printf ("NMI ... going to debugger\n");
703 kdb_trap (type, 0, &frame);
707 } else if (panic_on_nmi == 0)
710 #endif /* POWERFAIL_NMI */
711 #endif /* NISA > 0 */
714 trap_fatal(&frame, eva);
718 /* Translate fault for emulators (e.g. Linux) */
719 if (*p->p_sysent->sv_transtrap)
720 i = (*p->p_sysent->sv_transtrap)(i, type);
722 trapsignal(p, i, ucode);
725 if (type <= MAX_TRAP_MSG) {
726 uprintf("fatal process exception: %s",
728 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
729 uprintf(", fault VA = 0x%lx", (u_long)eva);
735 userret(p, &frame, sticks, 1);
740 * This version doesn't allow a page fault to user space while
741 * in the kernel. The rest of the kernel needs to be made "safe"
742 * before this can be used. I think the only things remaining
743 * to be made safe are the iBCS2 code and the process tracing/
747 trap_pfault(frame, usermode, eva)
748 struct trapframe *frame;
753 struct vmspace *vm = NULL;
757 struct proc *p = curproc;
759 if (frame->tf_err & PGEX_W)
760 ftype = VM_PROT_WRITE;
762 ftype = VM_PROT_READ;
764 va = trunc_page(eva);
765 if (va < VM_MIN_KERNEL_ADDRESS) {
770 (!usermode && va < VM_MAXUSER_ADDRESS &&
771 (mycpu->gd_intr_nesting_level != 0 ||
772 curthread->td_pcb->pcb_onfault == NULL))) {
773 trap_fatal(frame, eva);
778 * This is a fault on non-kernel virtual memory.
779 * vm is initialized above to NULL. If curproc is NULL
780 * or curproc->p_vmspace is NULL the fault is fatal.
789 * Keep swapout from messing with us during this
795 * Grow the stack if necessary
797 /* grow_stack returns false only if va falls into
798 * a growable stack region and the stack growth
799 * fails. It returns true if va was not within
800 * a growable stack region, or if the stack
803 if (!grow_stack (p, va)) {
809 /* Fault in the user page: */
810 rv = vm_fault(map, va, ftype,
811 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
817 * Don't allow user-mode faults in kernel address space.
823 * Since we know that kernel virtual address addresses
824 * always have pte pages mapped, we just have to fault
827 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
830 if (rv == KERN_SUCCESS)
834 if (mycpu->gd_intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
835 frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
838 trap_fatal(frame, eva);
842 /* kludge to pass faulting virtual address to sendsig */
845 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
850 trap_pfault(frame, usermode, eva)
851 struct trapframe *frame;
856 struct vmspace *vm = NULL;
860 struct proc *p = curproc;
862 va = trunc_page(eva);
863 if (va >= KERNBASE) {
865 * Don't allow user-mode faults in kernel address space.
866 * An exception: if the faulting address is the invalid
867 * instruction entry in the IDT, then the Intel Pentium
868 * F00F bug workaround was triggered, and we need to
869 * treat it is as an illegal instruction, and not a page
872 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
873 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
874 frame->tf_trapno = T_PRIVINFLT;
884 * This is a fault on non-kernel virtual memory.
885 * vm is initialized above to NULL. If curproc is NULL
886 * or curproc->p_vmspace is NULL the fault is fatal.
897 if (frame->tf_err & PGEX_W)
898 ftype = VM_PROT_WRITE;
900 ftype = VM_PROT_READ;
902 if (map != kernel_map) {
904 * Keep swapout from messing with us during this
910 * Grow the stack if necessary
912 /* grow_stack returns false only if va falls into
913 * a growable stack region and the stack growth
914 * fails. It returns true if va was not within
915 * a growable stack region, or if the stack
918 if (!grow_stack (p, va)) {
924 /* Fault in the user page: */
925 rv = vm_fault(map, va, ftype,
926 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
932 * Don't have to worry about process locking or stacks in the kernel.
934 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
937 if (rv == KERN_SUCCESS)
941 if (mycpu->gd_intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
942 frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
945 trap_fatal(frame, eva);
949 /* kludge to pass faulting virtual address to sendsig */
952 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
956 trap_fatal(frame, eva)
957 struct trapframe *frame;
960 int code, type, ss, esp;
961 struct soft_segment_descriptor softseg;
963 code = frame->tf_err;
964 type = frame->tf_trapno;
965 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
967 if (type <= MAX_TRAP_MSG)
968 printf("\n\nFatal trap %d: %s while in %s mode\n",
969 type, trap_msg[type],
970 frame->tf_eflags & PSL_VM ? "vm86" :
971 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
973 /* three seperate prints in case of a trap on an unmapped page */
974 printf("mp_lock = %08x; ", mp_lock);
975 printf("cpuid = %d; ", cpuid);
976 printf("lapic.id = %08x\n", lapic.id);
978 if (type == T_PAGEFLT) {
979 printf("fault virtual address = 0x%x\n", eva);
980 printf("fault code = %s %s, %s\n",
981 code & PGEX_U ? "user" : "supervisor",
982 code & PGEX_W ? "write" : "read",
983 code & PGEX_P ? "protection violation" : "page not present");
985 printf("instruction pointer = 0x%x:0x%x\n",
986 frame->tf_cs & 0xffff, frame->tf_eip);
987 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
988 ss = frame->tf_ss & 0xffff;
991 ss = GSEL(GDATA_SEL, SEL_KPL);
992 esp = (int)&frame->tf_esp;
994 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
995 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
996 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
997 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
998 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
999 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
1001 printf("processor eflags = ");
1002 if (frame->tf_eflags & PSL_T)
1003 printf("trace trap, ");
1004 if (frame->tf_eflags & PSL_I)
1005 printf("interrupt enabled, ");
1006 if (frame->tf_eflags & PSL_NT)
1007 printf("nested task, ");
1008 if (frame->tf_eflags & PSL_RF)
1010 if (frame->tf_eflags & PSL_VM)
1012 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
1013 printf("current process = ");
1015 printf("%lu (%s)\n",
1016 (u_long)curproc->p_pid, curproc->p_comm ?
1017 curproc->p_comm : "");
1021 printf("current thread = pri %d ", curthread->td_pri);
1022 if (curthread->td_pri >= TDPRI_CRIT)
1025 printf("interrupt mask = ");
1026 if ((curthread->td_cpl & net_imask) == net_imask)
1028 if ((curthread->td_cpl & tty_imask) == tty_imask)
1030 if ((curthread->td_cpl & bio_imask) == bio_imask)
1032 if ((curthread->td_cpl & cam_imask) == cam_imask)
1034 if (curthread->td_cpl == 0)
1039 * we probably SHOULD have stopped the other CPUs before now!
1040 * another CPU COULD have been touching cpl at this moment...
1042 printf(" <- SMP: XXX");
1051 if ((debugger_on_panic || db_active) && kdb_trap(type, 0, frame))
1054 printf("trap number = %d\n", type);
1055 if (type <= MAX_TRAP_MSG)
1056 panic("%s", trap_msg[type]);
1058 panic("unknown/reserved trap");
1062 * Double fault handler. Called when a fault occurs while writing
1063 * a frame for a trap/exception onto the stack. This usually occurs
1064 * when the stack overflows (such is the case with infinite recursion,
1067 * XXX Note that the current PTD gets replaced by IdlePTD when the
1068 * task switch occurs. This means that the stack that was active at
1069 * the time of the double fault is not available at <kstack> unless
1070 * the machine was idle when the double fault occurred. The downside
1071 * of this is that "trace <ebp>" in ddb won't work.
1076 struct mdglobaldata *gd = mdcpu;
1078 printf("\nFatal double fault:\n");
1079 printf("eip = 0x%x\n", gd->gd_common_tss.tss_eip);
1080 printf("esp = 0x%x\n", gd->gd_common_tss.tss_esp);
1081 printf("ebp = 0x%x\n", gd->gd_common_tss.tss_ebp);
1083 /* three seperate prints in case of a trap on an unmapped page */
1084 printf("mp_lock = %08x; ", mp_lock);
1085 printf("cpuid = %d; ", cpuid);
1086 printf("lapic.id = %08x\n", lapic.id);
1088 panic("double fault");
1092 * Compensate for 386 brain damage (missing URKR).
1093 * This is a little simpler than the pagefault handler in trap() because
1094 * it the page tables have already been faulted in and high addresses
1095 * are thrown out early for other reasons.
1105 va = trunc_page((vm_offset_t)addr);
1107 * XXX - MAX is END. Changed > to >= for temp. fix.
1109 if (va >= VM_MAXUSER_ADDRESS)
1117 if (!grow_stack (p, va)) {
1123 * fault the data page
1125 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
1129 if (rv != KERN_SUCCESS)
1136 * syscall2 - MP aware system call request C handler
1138 * A system call is essentially treated as a trap except that the
1139 * MP lock is not held on entry or return. We are responsible for
1140 * obtaining the MP lock if necessary and for handling ASTs
1141 * (e.g. a task switch) prior to return.
1143 * In general, only simple access and manipulation of curproc and
1144 * the current stack is allowed without having to hold MP lock.
1148 struct trapframe frame;
1150 struct thread *td = curthread;
1151 struct proc *p = td->td_proc;
1154 struct sysent *callp;
1155 register_t orig_tf_eflags;
1160 int have_mplock = 0;
1164 if (ISPL(frame.tf_cs) != SEL_UPL) {
1172 * access non-atomic field from critical section. p_sticks is
1173 * updated by the clock interrupt. Also use this opportunity
1174 * to raise our LWKT priority.
1178 sticks = curthread->td_sticks;
1181 p->p_md.md_regs = &frame;
1182 params = (caddr_t)frame.tf_esp + sizeof(int);
1183 code = frame.tf_eax;
1184 orig_tf_eflags = frame.tf_eflags;
1186 if (p->p_sysent->sv_prepsyscall) {
1188 * The prep code is not MP aware.
1191 (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms);
1195 * Need to check if this is a 32 bit or 64 bit syscall.
1196 * fuword is MP aware.
1198 if (code == SYS_syscall) {
1200 * Code is first argument, followed by actual args.
1202 code = fuword(params);
1203 params += sizeof(int);
1204 } else if (code == SYS___syscall) {
1206 * Like syscall, but code is a quad, so as to maintain
1207 * quad alignment for the rest of the arguments.
1209 code = fuword(params);
1210 params += sizeof(quad_t);
1214 if (p->p_sysent->sv_mask)
1215 code &= p->p_sysent->sv_mask;
1217 if (code >= p->p_sysent->sv_size)
1218 callp = &p->p_sysent->sv_table[0];
1220 callp = &p->p_sysent->sv_table[code];
1222 narg = callp->sy_narg & SYF_ARGMASK;
1225 * copyin is MP aware, but the tracing code is not
1227 if (params && (i = narg * sizeof(int)) &&
1228 (error = copyin(params, (caddr_t)args, (u_int)i))) {
1232 if (KTRPOINT(td, KTR_SYSCALL))
1233 ktrsyscall(p->p_tracep, code, narg, args);
1239 * Try to run the syscall without the MP lock if the syscall
1240 * is MP safe. We have to obtain the MP lock no matter what if
1243 if ((callp->sy_narg & SYF_MPSAFE) == 0) {
1249 if (KTRPOINT(td, KTR_SYSCALL)) {
1250 if (have_mplock == 0) {
1254 ktrsyscall(p->p_tracep, code, narg, args);
1258 p->p_retval[1] = frame.tf_edx;
1260 STOPEVENT(p, S_SCE, narg); /* MP aware */
1262 error = (*callp->sy_call)(args);
1265 * MP SAFE (we may or may not have the MP lock at this point)
1270 * Reinitialize proc pointer `p' as it may be different
1271 * if this is a child returning from fork syscall.
1274 frame.tf_eax = p->p_retval[0];
1275 frame.tf_edx = p->p_retval[1];
1276 frame.tf_eflags &= ~PSL_C;
1281 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1282 * int 0x80 is 2 bytes. We saved this in tf_err.
1284 frame.tf_eip -= frame.tf_err;
1292 if (p->p_sysent->sv_errsize) {
1293 if (error >= p->p_sysent->sv_errsize)
1294 error = -1; /* XXX */
1296 error = p->p_sysent->sv_errtbl[error];
1298 frame.tf_eax = error;
1299 frame.tf_eflags |= PSL_C;
1304 * Traced syscall. trapsignal() is not MP aware.
1306 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1307 if (have_mplock == 0) {
1311 frame.tf_eflags &= ~PSL_T;
1312 trapsignal(p, SIGTRAP, 0);
1316 * Handle reschedule and other end-of-syscall issues
1318 have_mplock = userret(p, &frame, sticks, have_mplock);
1321 if (KTRPOINT(td, KTR_SYSRET)) {
1322 if (have_mplock == 0) {
1326 ktrsysret(p->p_tracep, code, error, p->p_retval[0]);
1331 * This works because errno is findable through the
1332 * register set. If we ever support an emulation where this
1333 * is not the case, this code will need to be revisited.
1335 STOPEVENT(p, S_SCX, code);
1338 * Release the MP lock if we had to get it
1345 * Simplified back end of syscall(), used when returning from fork()
1346 * directly into user mode. MP lock is held on entry and should be
1350 fork_return(p, frame)
1352 struct trapframe frame;
1354 frame.tf_eax = 0; /* Child returns zero */
1355 frame.tf_eflags &= ~PSL_C; /* success */
1358 userret(p, &frame, 0, 1);
1360 if (KTRPOINT(p->p_thread, KTR_SYSRET))
1361 ktrsysret(p->p_tracep, SYS_fork, 0, 0);