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/i386/i386/Attic/trap.c,v 1.8 2003/06/25 03:55:53 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>
96 #include <sys/thread2.h>
101 int (*pmath_emulate) __P((struct trapframe *));
103 extern void trap __P((struct trapframe frame));
104 extern int trapwrite __P((unsigned addr));
105 extern void syscall2 __P((struct trapframe frame));
107 static int trap_pfault __P((struct trapframe *, int, vm_offset_t));
108 static void trap_fatal __P((struct trapframe *, vm_offset_t));
109 void dblfault_handler __P((void));
111 extern inthand_t IDTVEC(syscall);
113 #define MAX_TRAP_MSG 28
114 static char *trap_msg[] = {
116 "privileged instruction fault", /* 1 T_PRIVINFLT */
118 "breakpoint instruction fault", /* 3 T_BPTFLT */
121 "arithmetic trap", /* 6 T_ARITHTRAP */
122 "system forced exception", /* 7 T_ASTFLT */
124 "general protection fault", /* 9 T_PROTFLT */
125 "trace trap", /* 10 T_TRCTRAP */
127 "page fault", /* 12 T_PAGEFLT */
129 "alignment fault", /* 14 T_ALIGNFLT */
133 "integer divide fault", /* 18 T_DIVIDE */
134 "non-maskable interrupt trap", /* 19 T_NMI */
135 "overflow trap", /* 20 T_OFLOW */
136 "FPU bounds check fault", /* 21 T_BOUND */
137 "FPU device not available", /* 22 T_DNA */
138 "double fault", /* 23 T_DOUBLEFLT */
139 "FPU operand fetch fault", /* 24 T_FPOPFLT */
140 "invalid TSS fault", /* 25 T_TSSFLT */
141 "segment not present fault", /* 26 T_SEGNPFLT */
142 "stack fault", /* 27 T_STKFLT */
143 "machine check trap", /* 28 T_MCHK */
146 static __inline int userret __P((struct proc *p, struct trapframe *frame,
147 u_quad_t oticks, int have_mplock));
149 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
150 extern int has_f00f_bug;
154 static int ddb_on_nmi = 1;
155 SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
156 &ddb_on_nmi, 0, "Go to DDB on NMI");
158 static int panic_on_nmi = 1;
159 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
160 &panic_on_nmi, 0, "Panic on NMI");
163 userret(p, frame, oticks, have_mplock)
165 struct trapframe *frame;
172 while ((sig = CURSIG(p)) != 0) {
173 if (have_mplock == 0) {
180 p->p_priority = p->p_usrpri;
181 if (resched_wanted()) {
183 * Since we are curproc, clock will normally just change
184 * our priority without moving us from one queue to another
185 * (since the running process is not on a queue.)
186 * If that happened after we setrunqueue ourselves but before we
187 * mi_switch()'ed, we might not be on the queue indicated by
190 if (have_mplock == 0) {
196 p->p_stats->p_ru.ru_nivcsw++;
199 while ((sig = CURSIG(p)) != 0)
203 * Charge system time if profiling.
205 if (p->p_flag & P_PROFIL) {
206 if (have_mplock == 0) {
211 addupc_task(p, frame->tf_eip,
212 (u_int)(td->td_sticks - oticks) * psratio);
214 curpriority = p->p_priority;
218 #ifdef DEVICE_POLLING
219 extern u_int32_t poll_in_trap;
220 extern int ether_poll __P((int count));
221 #endif /* DEVICE_POLLING */
224 * Exception, fault, and trap interface to the FreeBSD kernel.
225 * This common code is called from assembly language IDT gate entry
226 * routines that prepare a suitable stack frame, and restore this
227 * frame after the exception has been processed.
232 struct trapframe frame;
234 struct proc *p = curproc;
236 int i = 0, ucode = 0, type, code;
241 eva = (frame.tf_trapno == T_PAGEFLT ? rcr2() : 0);
242 trap_fatal(&frame, eva);
247 if (!(frame.tf_eflags & PSL_I)) {
249 * Buggy application or kernel code has disabled interrupts
250 * and then trapped. Enabling interrupts now is wrong, but
251 * it is better than running with interrupts disabled until
252 * they are accidentally enabled later.
254 type = frame.tf_trapno;
255 if (ISPL(frame.tf_cs) == SEL_UPL || (frame.tf_eflags & PSL_VM))
257 "pid %ld (%s): trap %d with interrupts disabled\n",
258 (long)curproc->p_pid, curproc->p_comm, type);
259 else if (type != T_BPTFLT && type != T_TRCTRAP)
261 * XXX not quite right, since this may be for a
262 * multiple fault in user mode.
264 printf("kernel trap %d with interrupts disabled\n",
270 if (frame.tf_trapno == T_PAGEFLT) {
272 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
273 * This problem is worked around by using an interrupt
274 * gate for the pagefault handler. We are finally ready
275 * to read %cr2 and then must reenable interrupts.
277 * XXX this should be in the switch statement, but the
278 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
279 * flow of control too much for this to be obviously
286 #ifdef DEVICE_POLLING
288 ether_poll(poll_in_trap);
289 #endif /* DEVICE_POLLING */
291 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
294 type = frame.tf_trapno;
298 if (frame.tf_eflags & PSL_VM &&
299 (type == T_PROTFLT || type == T_STKFLT)) {
300 i = vm86_emulate((struct vm86frame *)&frame);
303 * returns to original process
305 vm86_trap((struct vm86frame *)&frame);
310 * these traps want either a process context, or
311 * assume a normal userspace trap.
315 trap_fatal(&frame, eva);
318 type = T_BPTFLT; /* kernel breakpoint */
321 goto kernel_trap; /* normal kernel trap handling */
324 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
327 sticks = curthread->td_sticks;
328 p->p_md.md_regs = &frame;
331 case T_PRIVINFLT: /* privileged instruction fault */
336 case T_BPTFLT: /* bpt instruction fault */
337 case T_TRCTRAP: /* trace trap */
338 frame.tf_eflags &= ~PSL_T;
342 case T_ARITHTRAP: /* arithmetic trap */
347 case T_ASTFLT: /* Allow process switch */
350 if (p->p_flag & P_OWEUPC) {
351 p->p_flag &= ~P_OWEUPC;
352 addupc_task(p, p->p_stats->p_prof.pr_addr,
353 p->p_stats->p_prof.pr_ticks);
358 * The following two traps can happen in
359 * vm86 mode, and, if so, we want to handle
362 case T_PROTFLT: /* general protection fault */
363 case T_STKFLT: /* stack fault */
364 if (frame.tf_eflags & PSL_VM) {
365 i = vm86_emulate((struct vm86frame *)&frame);
372 case T_SEGNPFLT: /* segment not present fault */
373 case T_TSSFLT: /* invalid TSS fault */
374 case T_DOUBLEFLT: /* double fault */
376 ucode = code + BUS_SEGM_FAULT ;
380 case T_PAGEFLT: /* page fault */
381 i = trap_pfault(&frame, TRUE, eva);
384 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
394 case T_DIVIDE: /* integer divide fault */
402 goto handle_powerfail;
403 #else /* !POWERFAIL_NMI */
404 /* machine/parity/power fail/"kitchen sink" faults */
405 if (isa_nmi(code) == 0) {
408 * NMI can be hooked up to a pushbutton
412 printf ("NMI ... going to debugger\n");
413 kdb_trap (type, 0, &frame);
417 } else if (panic_on_nmi)
418 panic("NMI indicates hardware failure");
420 #endif /* POWERFAIL_NMI */
421 #endif /* NISA > 0 */
423 case T_OFLOW: /* integer overflow fault */
428 case T_BOUND: /* bounds check fault */
435 /* if a transparent fault (due to context switch "late") */
439 if (!pmath_emulate) {
441 ucode = FPE_FPU_NP_TRAP;
444 i = (*pmath_emulate)(&frame);
446 if (!(frame.tf_eflags & PSL_T))
448 frame.tf_eflags &= ~PSL_T;
451 /* else ucode = emulator_only_knows() XXX */
454 case T_FPOPFLT: /* FPU operand fetch fault */
459 case T_XMMFLT: /* SIMD floating-point exception */
469 case T_PAGEFLT: /* page fault */
470 (void) trap_pfault(&frame, FALSE, eva);
476 * The kernel is apparently using npx for copying.
477 * XXX this should be fatal unless the kernel has
478 * registered such use.
485 case T_PROTFLT: /* general protection fault */
486 case T_SEGNPFLT: /* segment not present fault */
488 * Invalid segment selectors and out of bounds
489 * %eip's and %esp's can be set up in user mode.
490 * This causes a fault in kernel mode when the
491 * kernel tries to return to user mode. We want
492 * to get this fault so that we can fix the
493 * problem here and not have to check all the
494 * selectors and pointers when the user changes
497 #define MAYBE_DORETI_FAULT(where, whereto) \
499 if (frame.tf_eip == (int)where) { \
500 frame.tf_eip = (int)whereto; \
505 if (intr_nesting_level == 0) {
507 * Invalid %fs's and %gs's can be created using
508 * procfs or PT_SETREGS or by invalidating the
509 * underlying LDT entry. This causes a fault
510 * in kernel mode when the kernel attempts to
511 * switch contexts. Lose the bad context
512 * (XXX) so that we can continue, and generate
515 if (frame.tf_eip == (int)cpu_switch_load_gs) {
516 curthread->td_pcb->pcb_gs = 0;
520 MAYBE_DORETI_FAULT(doreti_iret,
522 MAYBE_DORETI_FAULT(doreti_popl_ds,
523 doreti_popl_ds_fault);
524 MAYBE_DORETI_FAULT(doreti_popl_es,
525 doreti_popl_es_fault);
526 MAYBE_DORETI_FAULT(doreti_popl_fs,
527 doreti_popl_fs_fault);
528 if (curthread->td_pcb->pcb_onfault) {
529 frame.tf_eip = (int)curthread->td_pcb->pcb_onfault;
537 * PSL_NT can be set in user mode and isn't cleared
538 * automatically when the kernel is entered. This
539 * causes a TSS fault when the kernel attempts to
540 * `iret' because the TSS link is uninitialized. We
541 * want to get this fault so that we can fix the
542 * problem here and not every time the kernel is
545 if (frame.tf_eflags & PSL_NT) {
546 frame.tf_eflags &= ~PSL_NT;
551 case T_TRCTRAP: /* trace trap */
552 if (frame.tf_eip == (int)IDTVEC(syscall)) {
554 * We've just entered system mode via the
555 * syscall lcall. Continue single stepping
556 * silently until the syscall handler has
561 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
563 * The syscall handler has now saved the
564 * flags. Stop single stepping it.
566 frame.tf_eflags &= ~PSL_T;
570 * Ignore debug register trace traps due to
571 * accesses in the user's address space, which
572 * can happen under several conditions such as
573 * if a user sets a watchpoint on a buffer and
574 * then passes that buffer to a system call.
575 * We still want to get TRCTRAPS for addresses
576 * in kernel space because that is useful when
577 * debugging the kernel.
579 if (user_dbreg_trap()) {
581 * Reset breakpoint bits because the
584 load_dr6(rdr6() & 0xfffffff0);
588 * Fall through (TRCTRAP kernel mode, kernel address)
592 * If DDB is enabled, let it handle the debugger trap.
593 * Otherwise, debugger traps "can't happen".
596 if (kdb_trap (type, 0, &frame))
605 # define TIMER_FREQ 1193182
609 static unsigned lastalert = 0;
611 if(time_second - lastalert > 10)
613 log(LOG_WARNING, "NMI: power fail\n");
614 sysbeep(TIMER_FREQ/880, hz);
615 lastalert = time_second;
619 #else /* !POWERFAIL_NMI */
620 /* machine/parity/power fail/"kitchen sink" faults */
621 if (isa_nmi(code) == 0) {
624 * NMI can be hooked up to a pushbutton
628 printf ("NMI ... going to debugger\n");
629 kdb_trap (type, 0, &frame);
633 } else if (panic_on_nmi == 0)
636 #endif /* POWERFAIL_NMI */
637 #endif /* NISA > 0 */
640 trap_fatal(&frame, eva);
644 /* Translate fault for emulators (e.g. Linux) */
645 if (*p->p_sysent->sv_transtrap)
646 i = (*p->p_sysent->sv_transtrap)(i, type);
648 trapsignal(p, i, ucode);
651 if (type <= MAX_TRAP_MSG) {
652 uprintf("fatal process exception: %s",
654 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
655 uprintf(", fault VA = 0x%lx", (u_long)eva);
661 userret(p, &frame, sticks, 1);
666 * This version doesn't allow a page fault to user space while
667 * in the kernel. The rest of the kernel needs to be made "safe"
668 * before this can be used. I think the only things remaining
669 * to be made safe are the iBCS2 code and the process tracing/
673 trap_pfault(frame, usermode, eva)
674 struct trapframe *frame;
679 struct vmspace *vm = NULL;
683 struct proc *p = curproc;
685 if (frame->tf_err & PGEX_W)
686 ftype = VM_PROT_WRITE;
688 ftype = VM_PROT_READ;
690 va = trunc_page(eva);
691 if (va < VM_MIN_KERNEL_ADDRESS) {
696 (!usermode && va < VM_MAXUSER_ADDRESS &&
697 (intr_nesting_level != 0 ||
698 curthread->td_pcb->pcb_onfault == NULL))) {
699 trap_fatal(frame, eva);
704 * This is a fault on non-kernel virtual memory.
705 * vm is initialized above to NULL. If curproc is NULL
706 * or curproc->p_vmspace is NULL the fault is fatal.
715 * Keep swapout from messing with us during this
721 * Grow the stack if necessary
723 /* grow_stack returns false only if va falls into
724 * a growable stack region and the stack growth
725 * fails. It returns true if va was not within
726 * a growable stack region, or if the stack
729 if (!grow_stack (p, va)) {
735 /* Fault in the user page: */
736 rv = vm_fault(map, va, ftype,
737 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
743 * Don't allow user-mode faults in kernel address space.
749 * Since we know that kernel virtual address addresses
750 * always have pte pages mapped, we just have to fault
753 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
756 if (rv == KERN_SUCCESS)
760 if (intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
761 frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
764 trap_fatal(frame, eva);
768 /* kludge to pass faulting virtual address to sendsig */
771 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
776 trap_pfault(frame, usermode, eva)
777 struct trapframe *frame;
782 struct vmspace *vm = NULL;
786 struct proc *p = curproc;
788 va = trunc_page(eva);
789 if (va >= KERNBASE) {
791 * Don't allow user-mode faults in kernel address space.
792 * An exception: if the faulting address is the invalid
793 * instruction entry in the IDT, then the Intel Pentium
794 * F00F bug workaround was triggered, and we need to
795 * treat it is as an illegal instruction, and not a page
798 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
799 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
800 frame->tf_trapno = T_PRIVINFLT;
810 * This is a fault on non-kernel virtual memory.
811 * vm is initialized above to NULL. If curproc is NULL
812 * or curproc->p_vmspace is NULL the fault is fatal.
823 if (frame->tf_err & PGEX_W)
824 ftype = VM_PROT_WRITE;
826 ftype = VM_PROT_READ;
828 if (map != kernel_map) {
830 * Keep swapout from messing with us during this
836 * Grow the stack if necessary
838 /* grow_stack returns false only if va falls into
839 * a growable stack region and the stack growth
840 * fails. It returns true if va was not within
841 * a growable stack region, or if the stack
844 if (!grow_stack (p, va)) {
850 /* Fault in the user page: */
851 rv = vm_fault(map, va, ftype,
852 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
858 * Don't have to worry about process locking or stacks in the kernel.
860 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
863 if (rv == KERN_SUCCESS)
867 if (intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
868 frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
871 trap_fatal(frame, eva);
875 /* kludge to pass faulting virtual address to sendsig */
878 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
882 trap_fatal(frame, eva)
883 struct trapframe *frame;
886 int code, type, ss, esp;
887 struct soft_segment_descriptor softseg;
889 code = frame->tf_err;
890 type = frame->tf_trapno;
891 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
893 if (type <= MAX_TRAP_MSG)
894 printf("\n\nFatal trap %d: %s while in %s mode\n",
895 type, trap_msg[type],
896 frame->tf_eflags & PSL_VM ? "vm86" :
897 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
899 /* three seperate prints in case of a trap on an unmapped page */
900 printf("mp_lock = %08x; ", mp_lock);
901 printf("cpuid = %d; ", cpuid);
902 printf("lapic.id = %08x\n", lapic.id);
904 if (type == T_PAGEFLT) {
905 printf("fault virtual address = 0x%x\n", eva);
906 printf("fault code = %s %s, %s\n",
907 code & PGEX_U ? "user" : "supervisor",
908 code & PGEX_W ? "write" : "read",
909 code & PGEX_P ? "protection violation" : "page not present");
911 printf("instruction pointer = 0x%x:0x%x\n",
912 frame->tf_cs & 0xffff, frame->tf_eip);
913 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
914 ss = frame->tf_ss & 0xffff;
917 ss = GSEL(GDATA_SEL, SEL_KPL);
918 esp = (int)&frame->tf_esp;
920 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
921 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
922 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
923 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
924 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
925 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
927 printf("processor eflags = ");
928 if (frame->tf_eflags & PSL_T)
929 printf("trace trap, ");
930 if (frame->tf_eflags & PSL_I)
931 printf("interrupt enabled, ");
932 if (frame->tf_eflags & PSL_NT)
933 printf("nested task, ");
934 if (frame->tf_eflags & PSL_RF)
936 if (frame->tf_eflags & PSL_VM)
938 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
939 printf("current process = ");
942 (u_long)curproc->p_pid, curproc->p_comm ?
943 curproc->p_comm : "");
947 printf("current thread = pri %d ", curthread->td_pri);
948 if (curthread->td_pri >= TDPRI_CRIT)
951 printf("interrupt mask = ");
952 if ((curthread->td_cpl & net_imask) == net_imask)
954 if ((curthread->td_cpl & tty_imask) == tty_imask)
956 if ((curthread->td_cpl & bio_imask) == bio_imask)
958 if ((curthread->td_cpl & cam_imask) == cam_imask)
960 if (curthread->td_cpl == 0)
965 * we probably SHOULD have stopped the other CPUs before now!
966 * another CPU COULD have been touching cpl at this moment...
968 printf(" <- SMP: XXX");
977 if ((debugger_on_panic || db_active) && kdb_trap(type, 0, frame))
980 printf("trap number = %d\n", type);
981 if (type <= MAX_TRAP_MSG)
982 panic("%s", trap_msg[type]);
984 panic("unknown/reserved trap");
988 * Double fault handler. Called when a fault occurs while writing
989 * a frame for a trap/exception onto the stack. This usually occurs
990 * when the stack overflows (such is the case with infinite recursion,
993 * XXX Note that the current PTD gets replaced by IdlePTD when the
994 * task switch occurs. This means that the stack that was active at
995 * the time of the double fault is not available at <kstack> unless
996 * the machine was idle when the double fault occurred. The downside
997 * of this is that "trace <ebp>" in ddb won't work.
1002 printf("\nFatal double fault:\n");
1003 printf("eip = 0x%x\n", common_tss.tss_eip);
1004 printf("esp = 0x%x\n", common_tss.tss_esp);
1005 printf("ebp = 0x%x\n", common_tss.tss_ebp);
1007 /* three seperate prints in case of a trap on an unmapped page */
1008 printf("mp_lock = %08x; ", mp_lock);
1009 printf("cpuid = %d; ", cpuid);
1010 printf("lapic.id = %08x\n", lapic.id);
1012 panic("double fault");
1016 * Compensate for 386 brain damage (missing URKR).
1017 * This is a little simpler than the pagefault handler in trap() because
1018 * it the page tables have already been faulted in and high addresses
1019 * are thrown out early for other reasons.
1029 va = trunc_page((vm_offset_t)addr);
1031 * XXX - MAX is END. Changed > to >= for temp. fix.
1033 if (va >= VM_MAXUSER_ADDRESS)
1041 if (!grow_stack (p, va)) {
1047 * fault the data page
1049 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
1053 if (rv != KERN_SUCCESS)
1060 * syscall2 - MP aware system call request C handler
1062 * A system call is essentially treated as a trap except that the
1063 * MP lock is not held on entry or return. We are responsible for
1064 * obtaining the MP lock if necessary and for handling ASTs
1065 * (e.g. a task switch) prior to return.
1067 * In general, only simple access and manipulation of curproc and
1068 * the current stack is allowed without having to hold MP lock.
1072 struct trapframe frame;
1074 struct thread *td = curthread;
1075 struct proc *p = td->td_proc;
1078 struct sysent *callp;
1079 register_t orig_tf_eflags;
1084 int have_mplock = 0;
1088 if (ISPL(frame.tf_cs) != SEL_UPL) {
1096 * access non-atomic field from critical section. p_sticks is
1097 * updated by the clock interrupt.
1100 sticks = curthread->td_sticks;
1103 p->p_md.md_regs = &frame;
1104 params = (caddr_t)frame.tf_esp + sizeof(int);
1105 code = frame.tf_eax;
1106 orig_tf_eflags = frame.tf_eflags;
1108 if (p->p_sysent->sv_prepsyscall) {
1110 * The prep code is not MP aware.
1113 (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms);
1117 * Need to check if this is a 32 bit or 64 bit syscall.
1118 * fuword is MP aware.
1120 if (code == SYS_syscall) {
1122 * Code is first argument, followed by actual args.
1124 code = fuword(params);
1125 params += sizeof(int);
1126 } else if (code == SYS___syscall) {
1128 * Like syscall, but code is a quad, so as to maintain
1129 * quad alignment for the rest of the arguments.
1131 code = fuword(params);
1132 params += sizeof(quad_t);
1136 if (p->p_sysent->sv_mask)
1137 code &= p->p_sysent->sv_mask;
1139 if (code >= p->p_sysent->sv_size)
1140 callp = &p->p_sysent->sv_table[0];
1142 callp = &p->p_sysent->sv_table[code];
1144 narg = callp->sy_narg & SYF_ARGMASK;
1147 * copyin is MP aware, but the tracing code is not
1149 if (params && (i = narg * sizeof(int)) &&
1150 (error = copyin(params, (caddr_t)args, (u_int)i))) {
1154 if (KTRPOINT(td, KTR_SYSCALL))
1155 ktrsyscall(p->p_tracep, code, narg, args);
1161 * Try to run the syscall without the MP lock if the syscall
1162 * is MP safe. We have to obtain the MP lock no matter what if
1165 if ((callp->sy_narg & SYF_MPSAFE) == 0) {
1171 if (KTRPOINT(td, KTR_SYSCALL)) {
1172 if (have_mplock == 0) {
1176 ktrsyscall(p->p_tracep, code, narg, args);
1180 p->p_retval[1] = frame.tf_edx;
1182 STOPEVENT(p, S_SCE, narg); /* MP aware */
1184 error = (*callp->sy_call)(args);
1187 * MP SAFE (we may or may not have the MP lock at this point)
1192 * Reinitialize proc pointer `p' as it may be different
1193 * if this is a child returning from fork syscall.
1196 frame.tf_eax = p->p_retval[0];
1197 frame.tf_edx = p->p_retval[1];
1198 frame.tf_eflags &= ~PSL_C;
1203 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1204 * int 0x80 is 2 bytes. We saved this in tf_err.
1206 frame.tf_eip -= frame.tf_err;
1214 if (p->p_sysent->sv_errsize) {
1215 if (error >= p->p_sysent->sv_errsize)
1216 error = -1; /* XXX */
1218 error = p->p_sysent->sv_errtbl[error];
1220 frame.tf_eax = error;
1221 frame.tf_eflags |= PSL_C;
1226 * Traced syscall. trapsignal() is not MP aware.
1228 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1229 if (have_mplock == 0) {
1233 frame.tf_eflags &= ~PSL_T;
1234 trapsignal(p, SIGTRAP, 0);
1238 * Handle reschedule and other end-of-syscall issues
1240 have_mplock = userret(p, &frame, sticks, have_mplock);
1243 if (KTRPOINT(td, KTR_SYSRET)) {
1244 if (have_mplock == 0) {
1248 ktrsysret(p->p_tracep, code, error, p->p_retval[0]);
1253 * This works because errno is findable through the
1254 * register set. If we ever support an emulation where this
1255 * is not the case, this code will need to be revisited.
1257 STOPEVENT(p, S_SCX, code);
1260 * Release the MP lock if we had to get it
1267 * Simplified back end of syscall(), used when returning from fork()
1268 * directly into user mode. MP lock is held on entry and should be
1272 fork_return(p, frame)
1274 struct trapframe frame;
1276 frame.tf_eax = 0; /* Child returns zero */
1277 frame.tf_eflags &= ~PSL_C; /* success */
1280 userret(p, &frame, 0, 1);
1282 if (KTRPOINT(p->p_thread, KTR_SYSRET))
1283 ktrsysret(p->p_tracep, SYS_fork, 0, 0);