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.4 2003/06/21 07:54:56 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>
86 #include <i386/isa/intr_machdep.h>
89 #include <sys/syslog.h>
90 #include <machine/clock.h>
93 #include <machine/vm86.h>
100 int (*pmath_emulate) __P((struct trapframe *));
102 extern void trap __P((struct trapframe frame));
103 extern int trapwrite __P((unsigned addr));
104 extern void syscall2 __P((struct trapframe frame));
106 static int trap_pfault __P((struct trapframe *, int, vm_offset_t));
107 static void trap_fatal __P((struct trapframe *, vm_offset_t));
108 void dblfault_handler __P((void));
110 extern inthand_t IDTVEC(syscall);
112 #define MAX_TRAP_MSG 28
113 static char *trap_msg[] = {
115 "privileged instruction fault", /* 1 T_PRIVINFLT */
117 "breakpoint instruction fault", /* 3 T_BPTFLT */
120 "arithmetic trap", /* 6 T_ARITHTRAP */
121 "system forced exception", /* 7 T_ASTFLT */
123 "general protection fault", /* 9 T_PROTFLT */
124 "trace trap", /* 10 T_TRCTRAP */
126 "page fault", /* 12 T_PAGEFLT */
128 "alignment fault", /* 14 T_ALIGNFLT */
132 "integer divide fault", /* 18 T_DIVIDE */
133 "non-maskable interrupt trap", /* 19 T_NMI */
134 "overflow trap", /* 20 T_OFLOW */
135 "FPU bounds check fault", /* 21 T_BOUND */
136 "FPU device not available", /* 22 T_DNA */
137 "double fault", /* 23 T_DOUBLEFLT */
138 "FPU operand fetch fault", /* 24 T_FPOPFLT */
139 "invalid TSS fault", /* 25 T_TSSFLT */
140 "segment not present fault", /* 26 T_SEGNPFLT */
141 "stack fault", /* 27 T_STKFLT */
142 "machine check trap", /* 28 T_MCHK */
145 static __inline int userret __P((struct proc *p, struct trapframe *frame,
146 u_quad_t oticks, int have_mplock));
148 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
149 extern int has_f00f_bug;
153 static int ddb_on_nmi = 1;
154 SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
155 &ddb_on_nmi, 0, "Go to DDB on NMI");
157 static int panic_on_nmi = 1;
158 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
159 &panic_on_nmi, 0, "Panic on NMI");
162 userret(p, frame, oticks, have_mplock)
164 struct trapframe *frame;
170 while ((sig = CURSIG(p)) != 0) {
171 if (have_mplock == 0) {
178 p->p_priority = p->p_usrpri;
179 if (resched_wanted()) {
181 * Since we are curproc, clock will normally just change
182 * our priority without moving us from one queue to another
183 * (since the running process is not on a queue.)
184 * If that happened after we setrunqueue ourselves but before we
185 * mi_switch()'ed, we might not be on the queue indicated by
188 if (have_mplock == 0) {
194 p->p_stats->p_ru.ru_nivcsw++;
197 while ((sig = CURSIG(p)) != 0)
201 * Charge system time if profiling.
203 if (p->p_flag & P_PROFIL) {
204 if (have_mplock == 0) {
208 addupc_task(p, frame->tf_eip,
209 (u_int)(p->p_sticks - oticks) * psratio);
211 curpriority = p->p_priority;
215 #ifdef DEVICE_POLLING
216 extern u_int32_t poll_in_trap;
217 extern int ether_poll __P((int count));
218 #endif /* DEVICE_POLLING */
221 * Exception, fault, and trap interface to the FreeBSD kernel.
222 * This common code is called from assembly language IDT gate entry
223 * routines that prepare a suitable stack frame, and restore this
224 * frame after the exception has been processed.
229 struct trapframe frame;
231 struct proc *p = curproc;
233 int i = 0, ucode = 0, type, code;
238 eva = (frame.tf_trapno == T_PAGEFLT ? rcr2() : 0);
239 trap_fatal(&frame, eva);
244 if (!(frame.tf_eflags & PSL_I)) {
246 * Buggy application or kernel code has disabled interrupts
247 * and then trapped. Enabling interrupts now is wrong, but
248 * it is better than running with interrupts disabled until
249 * they are accidentally enabled later.
251 type = frame.tf_trapno;
252 if (ISPL(frame.tf_cs) == SEL_UPL || (frame.tf_eflags & PSL_VM))
254 "pid %ld (%s): trap %d with interrupts disabled\n",
255 (long)curproc->p_pid, curproc->p_comm, type);
256 else if (type != T_BPTFLT && type != T_TRCTRAP)
258 * XXX not quite right, since this may be for a
259 * multiple fault in user mode.
261 printf("kernel trap %d with interrupts disabled\n",
267 if (frame.tf_trapno == T_PAGEFLT) {
269 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
270 * This problem is worked around by using an interrupt
271 * gate for the pagefault handler. We are finally ready
272 * to read %cr2 and then must reenable interrupts.
274 * XXX this should be in the switch statement, but the
275 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
276 * flow of control too much for this to be obviously
283 #ifdef DEVICE_POLLING
285 ether_poll(poll_in_trap);
286 #endif /* DEVICE_POLLING */
288 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
291 type = frame.tf_trapno;
295 if (frame.tf_eflags & PSL_VM &&
296 (type == T_PROTFLT || type == T_STKFLT)) {
297 i = vm86_emulate((struct vm86frame *)&frame);
300 * returns to original process
302 vm86_trap((struct vm86frame *)&frame);
307 * these traps want either a process context, or
308 * assume a normal userspace trap.
312 trap_fatal(&frame, eva);
315 type = T_BPTFLT; /* kernel breakpoint */
318 goto kernel_trap; /* normal kernel trap handling */
321 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
324 sticks = p->p_sticks;
325 p->p_md.md_regs = &frame;
328 case T_PRIVINFLT: /* privileged instruction fault */
333 case T_BPTFLT: /* bpt instruction fault */
334 case T_TRCTRAP: /* trace trap */
335 frame.tf_eflags &= ~PSL_T;
339 case T_ARITHTRAP: /* arithmetic trap */
344 case T_ASTFLT: /* Allow process switch */
347 if (p->p_flag & P_OWEUPC) {
348 p->p_flag &= ~P_OWEUPC;
349 addupc_task(p, p->p_stats->p_prof.pr_addr,
350 p->p_stats->p_prof.pr_ticks);
355 * The following two traps can happen in
356 * vm86 mode, and, if so, we want to handle
359 case T_PROTFLT: /* general protection fault */
360 case T_STKFLT: /* stack fault */
361 if (frame.tf_eflags & PSL_VM) {
362 i = vm86_emulate((struct vm86frame *)&frame);
369 case T_SEGNPFLT: /* segment not present fault */
370 case T_TSSFLT: /* invalid TSS fault */
371 case T_DOUBLEFLT: /* double fault */
373 ucode = code + BUS_SEGM_FAULT ;
377 case T_PAGEFLT: /* page fault */
378 i = trap_pfault(&frame, TRUE, eva);
381 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
391 case T_DIVIDE: /* integer divide fault */
399 goto handle_powerfail;
400 #else /* !POWERFAIL_NMI */
401 /* machine/parity/power fail/"kitchen sink" faults */
402 if (isa_nmi(code) == 0) {
405 * NMI can be hooked up to a pushbutton
409 printf ("NMI ... going to debugger\n");
410 kdb_trap (type, 0, &frame);
414 } else if (panic_on_nmi)
415 panic("NMI indicates hardware failure");
417 #endif /* POWERFAIL_NMI */
418 #endif /* NISA > 0 */
420 case T_OFLOW: /* integer overflow fault */
425 case T_BOUND: /* bounds check fault */
432 /* if a transparent fault (due to context switch "late") */
436 if (!pmath_emulate) {
438 ucode = FPE_FPU_NP_TRAP;
441 i = (*pmath_emulate)(&frame);
443 if (!(frame.tf_eflags & PSL_T))
445 frame.tf_eflags &= ~PSL_T;
448 /* else ucode = emulator_only_knows() XXX */
451 case T_FPOPFLT: /* FPU operand fetch fault */
456 case T_XMMFLT: /* SIMD floating-point exception */
466 case T_PAGEFLT: /* page fault */
467 (void) trap_pfault(&frame, FALSE, eva);
473 * The kernel is apparently using npx for copying.
474 * XXX this should be fatal unless the kernel has
475 * registered such use.
482 case T_PROTFLT: /* general protection fault */
483 case T_SEGNPFLT: /* segment not present fault */
485 * Invalid segment selectors and out of bounds
486 * %eip's and %esp's can be set up in user mode.
487 * This causes a fault in kernel mode when the
488 * kernel tries to return to user mode. We want
489 * to get this fault so that we can fix the
490 * problem here and not have to check all the
491 * selectors and pointers when the user changes
494 #define MAYBE_DORETI_FAULT(where, whereto) \
496 if (frame.tf_eip == (int)where) { \
497 frame.tf_eip = (int)whereto; \
502 if (intr_nesting_level == 0) {
504 * Invalid %fs's and %gs's can be created using
505 * procfs or PT_SETREGS or by invalidating the
506 * underlying LDT entry. This causes a fault
507 * in kernel mode when the kernel attempts to
508 * switch contexts. Lose the bad context
509 * (XXX) so that we can continue, and generate
512 if (frame.tf_eip == (int)cpu_switch_load_gs) {
513 curthread->td_pcb->pcb_gs = 0;
517 MAYBE_DORETI_FAULT(doreti_iret,
519 MAYBE_DORETI_FAULT(doreti_popl_ds,
520 doreti_popl_ds_fault);
521 MAYBE_DORETI_FAULT(doreti_popl_es,
522 doreti_popl_es_fault);
523 MAYBE_DORETI_FAULT(doreti_popl_fs,
524 doreti_popl_fs_fault);
525 if (curthread->td_pcb->pcb_onfault) {
526 frame.tf_eip = (int)curthread->td_pcb->pcb_onfault;
534 * PSL_NT can be set in user mode and isn't cleared
535 * automatically when the kernel is entered. This
536 * causes a TSS fault when the kernel attempts to
537 * `iret' because the TSS link is uninitialized. We
538 * want to get this fault so that we can fix the
539 * problem here and not every time the kernel is
542 if (frame.tf_eflags & PSL_NT) {
543 frame.tf_eflags &= ~PSL_NT;
548 case T_TRCTRAP: /* trace trap */
549 if (frame.tf_eip == (int)IDTVEC(syscall)) {
551 * We've just entered system mode via the
552 * syscall lcall. Continue single stepping
553 * silently until the syscall handler has
558 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
560 * The syscall handler has now saved the
561 * flags. Stop single stepping it.
563 frame.tf_eflags &= ~PSL_T;
567 * Ignore debug register trace traps due to
568 * accesses in the user's address space, which
569 * can happen under several conditions such as
570 * if a user sets a watchpoint on a buffer and
571 * then passes that buffer to a system call.
572 * We still want to get TRCTRAPS for addresses
573 * in kernel space because that is useful when
574 * debugging the kernel.
576 if (user_dbreg_trap()) {
578 * Reset breakpoint bits because the
581 load_dr6(rdr6() & 0xfffffff0);
585 * Fall through (TRCTRAP kernel mode, kernel address)
589 * If DDB is enabled, let it handle the debugger trap.
590 * Otherwise, debugger traps "can't happen".
593 if (kdb_trap (type, 0, &frame))
602 # define TIMER_FREQ 1193182
606 static unsigned lastalert = 0;
608 if(time_second - lastalert > 10)
610 log(LOG_WARNING, "NMI: power fail\n");
611 sysbeep(TIMER_FREQ/880, hz);
612 lastalert = time_second;
616 #else /* !POWERFAIL_NMI */
617 /* machine/parity/power fail/"kitchen sink" faults */
618 if (isa_nmi(code) == 0) {
621 * NMI can be hooked up to a pushbutton
625 printf ("NMI ... going to debugger\n");
626 kdb_trap (type, 0, &frame);
630 } else if (panic_on_nmi == 0)
633 #endif /* POWERFAIL_NMI */
634 #endif /* NISA > 0 */
637 trap_fatal(&frame, eva);
641 /* Translate fault for emulators (e.g. Linux) */
642 if (*p->p_sysent->sv_transtrap)
643 i = (*p->p_sysent->sv_transtrap)(i, type);
645 trapsignal(p, i, ucode);
648 if (type <= MAX_TRAP_MSG) {
649 uprintf("fatal process exception: %s",
651 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
652 uprintf(", fault VA = 0x%lx", (u_long)eva);
658 userret(p, &frame, sticks, 1);
663 * This version doesn't allow a page fault to user space while
664 * in the kernel. The rest of the kernel needs to be made "safe"
665 * before this can be used. I think the only things remaining
666 * to be made safe are the iBCS2 code and the process tracing/
670 trap_pfault(frame, usermode, eva)
671 struct trapframe *frame;
676 struct vmspace *vm = NULL;
680 struct proc *p = curproc;
682 if (frame->tf_err & PGEX_W)
683 ftype = VM_PROT_WRITE;
685 ftype = VM_PROT_READ;
687 va = trunc_page(eva);
688 if (va < VM_MIN_KERNEL_ADDRESS) {
693 (!usermode && va < VM_MAXUSER_ADDRESS &&
694 (intr_nesting_level != 0 ||
695 curthread->td_pcb->pcb_onfault == NULL))) {
696 trap_fatal(frame, eva);
701 * This is a fault on non-kernel virtual memory.
702 * vm is initialized above to NULL. If curproc is NULL
703 * or curproc->p_vmspace is NULL the fault is fatal.
712 * Keep swapout from messing with us during this
718 * Grow the stack if necessary
720 /* grow_stack returns false only if va falls into
721 * a growable stack region and the stack growth
722 * fails. It returns true if va was not within
723 * a growable stack region, or if the stack
726 if (!grow_stack (p, va)) {
732 /* Fault in the user page: */
733 rv = vm_fault(map, va, ftype,
734 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
740 * Don't allow user-mode faults in kernel address space.
746 * Since we know that kernel virtual address addresses
747 * always have pte pages mapped, we just have to fault
750 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
753 if (rv == KERN_SUCCESS)
757 if (intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
758 frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
761 trap_fatal(frame, eva);
765 /* kludge to pass faulting virtual address to sendsig */
768 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
773 trap_pfault(frame, usermode, eva)
774 struct trapframe *frame;
779 struct vmspace *vm = NULL;
783 struct proc *p = curproc;
785 va = trunc_page(eva);
786 if (va >= KERNBASE) {
788 * Don't allow user-mode faults in kernel address space.
789 * An exception: if the faulting address is the invalid
790 * instruction entry in the IDT, then the Intel Pentium
791 * F00F bug workaround was triggered, and we need to
792 * treat it is as an illegal instruction, and not a page
795 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
796 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
797 frame->tf_trapno = T_PRIVINFLT;
807 * This is a fault on non-kernel virtual memory.
808 * vm is initialized above to NULL. If curproc is NULL
809 * or curproc->p_vmspace is NULL the fault is fatal.
820 if (frame->tf_err & PGEX_W)
821 ftype = VM_PROT_WRITE;
823 ftype = VM_PROT_READ;
825 if (map != kernel_map) {
827 * Keep swapout from messing with us during this
833 * Grow the stack if necessary
835 /* grow_stack returns false only if va falls into
836 * a growable stack region and the stack growth
837 * fails. It returns true if va was not within
838 * a growable stack region, or if the stack
841 if (!grow_stack (p, va)) {
847 /* Fault in the user page: */
848 rv = vm_fault(map, va, ftype,
849 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
855 * Don't have to worry about process locking or stacks in the kernel.
857 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
860 if (rv == KERN_SUCCESS)
864 if (intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
865 frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
868 trap_fatal(frame, eva);
872 /* kludge to pass faulting virtual address to sendsig */
875 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
879 trap_fatal(frame, eva)
880 struct trapframe *frame;
883 int code, type, ss, esp;
884 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 printf("\n\nFatal trap %d: %s while in %s mode\n",
892 type, trap_msg[type],
893 frame->tf_eflags & PSL_VM ? "vm86" :
894 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
896 /* three seperate prints in case of a trap on an unmapped page */
897 printf("mp_lock = %08x; ", mp_lock);
898 printf("cpuid = %d; ", cpuid);
899 printf("lapic.id = %08x\n", lapic.id);
901 if (type == T_PAGEFLT) {
902 printf("fault virtual address = 0x%x\n", eva);
903 printf("fault code = %s %s, %s\n",
904 code & PGEX_U ? "user" : "supervisor",
905 code & PGEX_W ? "write" : "read",
906 code & PGEX_P ? "protection violation" : "page not present");
908 printf("instruction pointer = 0x%x:0x%x\n",
909 frame->tf_cs & 0xffff, frame->tf_eip);
910 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
911 ss = frame->tf_ss & 0xffff;
914 ss = GSEL(GDATA_SEL, SEL_KPL);
915 esp = (int)&frame->tf_esp;
917 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
918 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
919 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
920 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
921 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
922 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
924 printf("processor eflags = ");
925 if (frame->tf_eflags & PSL_T)
926 printf("trace trap, ");
927 if (frame->tf_eflags & PSL_I)
928 printf("interrupt enabled, ");
929 if (frame->tf_eflags & PSL_NT)
930 printf("nested task, ");
931 if (frame->tf_eflags & PSL_RF)
933 if (frame->tf_eflags & PSL_VM)
935 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
936 printf("current process = ");
939 (u_long)curproc->p_pid, curproc->p_comm ?
940 curproc->p_comm : "");
944 printf("current thread = pri %d ", curthread->td_pri);
945 if (curthread->td_pri >= TDPRI_CRIT)
948 printf("interrupt mask = ");
949 if ((cpl & net_imask) == net_imask)
951 if ((cpl & tty_imask) == tty_imask)
953 if ((cpl & bio_imask) == bio_imask)
955 if ((cpl & cam_imask) == cam_imask)
962 * we probably SHOULD have stopped the other CPUs before now!
963 * another CPU COULD have been touching cpl at this moment...
965 printf(" <- SMP: XXX");
974 if ((debugger_on_panic || db_active) && kdb_trap(type, 0, frame))
977 printf("trap number = %d\n", type);
978 if (type <= MAX_TRAP_MSG)
979 panic("%s", trap_msg[type]);
981 panic("unknown/reserved trap");
985 * Double fault handler. Called when a fault occurs while writing
986 * a frame for a trap/exception onto the stack. This usually occurs
987 * when the stack overflows (such is the case with infinite recursion,
990 * XXX Note that the current PTD gets replaced by IdlePTD when the
991 * task switch occurs. This means that the stack that was active at
992 * the time of the double fault is not available at <kstack> unless
993 * the machine was idle when the double fault occurred. The downside
994 * of this is that "trace <ebp>" in ddb won't work.
999 printf("\nFatal double fault:\n");
1000 printf("eip = 0x%x\n", common_tss.tss_eip);
1001 printf("esp = 0x%x\n", common_tss.tss_esp);
1002 printf("ebp = 0x%x\n", common_tss.tss_ebp);
1004 /* three seperate prints in case of a trap on an unmapped page */
1005 printf("mp_lock = %08x; ", mp_lock);
1006 printf("cpuid = %d; ", cpuid);
1007 printf("lapic.id = %08x\n", lapic.id);
1009 panic("double fault");
1013 * Compensate for 386 brain damage (missing URKR).
1014 * This is a little simpler than the pagefault handler in trap() because
1015 * it the page tables have already been faulted in and high addresses
1016 * are thrown out early for other reasons.
1026 va = trunc_page((vm_offset_t)addr);
1028 * XXX - MAX is END. Changed > to >= for temp. fix.
1030 if (va >= VM_MAXUSER_ADDRESS)
1038 if (!grow_stack (p, va)) {
1044 * fault the data page
1046 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
1050 if (rv != KERN_SUCCESS)
1057 * syscall2 - MP aware system call request C handler
1059 * A system call is essentially treated as a trap except that the
1060 * MP lock is not held on entry or return. We are responsible for
1061 * obtaining the MP lock if necessary and for handling ASTs
1062 * (e.g. a task switch) prior to return.
1064 * In general, only simple access and manipulation of curproc and
1065 * the current stack is allowed without having to hold MP lock.
1069 struct trapframe frame;
1073 struct sysent *callp;
1074 struct proc *p = curproc;
1075 register_t orig_tf_eflags;
1080 int have_mplock = 0;
1084 if (ISPL(frame.tf_cs) != SEL_UPL) {
1092 * handle atomicy by looping since interrupts are enabled and the
1093 * MP lock is not held.
1095 sticks = ((volatile struct proc *)p)->p_sticks;
1096 while (sticks != ((volatile struct proc *)p)->p_sticks)
1097 sticks = ((volatile struct proc *)p)->p_sticks;
1099 p->p_md.md_regs = &frame;
1100 params = (caddr_t)frame.tf_esp + sizeof(int);
1101 code = frame.tf_eax;
1102 orig_tf_eflags = frame.tf_eflags;
1104 if (p->p_sysent->sv_prepsyscall) {
1106 * The prep code is not MP aware.
1109 (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms);
1113 * Need to check if this is a 32 bit or 64 bit syscall.
1114 * fuword is MP aware.
1116 if (code == SYS_syscall) {
1118 * Code is first argument, followed by actual args.
1120 code = fuword(params);
1121 params += sizeof(int);
1122 } else if (code == SYS___syscall) {
1124 * Like syscall, but code is a quad, so as to maintain
1125 * quad alignment for the rest of the arguments.
1127 code = fuword(params);
1128 params += sizeof(quad_t);
1132 if (p->p_sysent->sv_mask)
1133 code &= p->p_sysent->sv_mask;
1135 if (code >= p->p_sysent->sv_size)
1136 callp = &p->p_sysent->sv_table[0];
1138 callp = &p->p_sysent->sv_table[code];
1140 narg = callp->sy_narg & SYF_ARGMASK;
1143 * copyin is MP aware, but the tracing code is not
1145 if (params && (i = narg * sizeof(int)) &&
1146 (error = copyin(params, (caddr_t)args, (u_int)i))) {
1150 if (KTRPOINT(p, KTR_SYSCALL))
1151 ktrsyscall(p->p_tracep, code, narg, args);
1157 * Try to run the syscall without the MP lock if the syscall
1158 * is MP safe. We have to obtain the MP lock no matter what if
1161 if ((callp->sy_narg & SYF_MPSAFE) == 0) {
1167 if (KTRPOINT(p, KTR_SYSCALL)) {
1168 if (have_mplock == 0) {
1172 ktrsyscall(p->p_tracep, code, narg, args);
1176 p->p_retval[1] = frame.tf_edx;
1178 STOPEVENT(p, S_SCE, narg); /* MP aware */
1180 error = (*callp->sy_call)(p, args);
1183 * MP SAFE (we may or may not have the MP lock at this point)
1188 * Reinitialize proc pointer `p' as it may be different
1189 * if this is a child returning from fork syscall.
1192 frame.tf_eax = p->p_retval[0];
1193 frame.tf_edx = p->p_retval[1];
1194 frame.tf_eflags &= ~PSL_C;
1199 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1200 * int 0x80 is 2 bytes. We saved this in tf_err.
1202 frame.tf_eip -= frame.tf_err;
1210 if (p->p_sysent->sv_errsize) {
1211 if (error >= p->p_sysent->sv_errsize)
1212 error = -1; /* XXX */
1214 error = p->p_sysent->sv_errtbl[error];
1216 frame.tf_eax = error;
1217 frame.tf_eflags |= PSL_C;
1222 * Traced syscall. trapsignal() is not MP aware.
1224 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1225 if (have_mplock == 0) {
1229 frame.tf_eflags &= ~PSL_T;
1230 trapsignal(p, SIGTRAP, 0);
1234 * Handle reschedule and other end-of-syscall issues
1236 have_mplock = userret(p, &frame, sticks, have_mplock);
1239 if (KTRPOINT(p, KTR_SYSRET)) {
1240 if (have_mplock == 0) {
1244 ktrsysret(p->p_tracep, code, error, p->p_retval[0]);
1249 * This works because errno is findable through the
1250 * register set. If we ever support an emulation where this
1251 * is not the case, this code will need to be revisited.
1253 STOPEVENT(p, S_SCX, code);
1256 * Release the MP lock if we had to get it
1263 * Simplified back end of syscall(), used when returning from fork()
1264 * directly into user mode. MP lock is held on entry and should be
1268 fork_return(p, frame)
1270 struct trapframe frame;
1272 frame.tf_eax = 0; /* Child returns zero */
1273 frame.tf_eflags &= ~PSL_C; /* success */
1276 userret(p, &frame, 0, 1);
1278 if (KTRPOINT(p, KTR_SYSRET))
1279 ktrsysret(p->p_tracep, SYS_fork, 0, 0);