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.6 2003/06/23 17:55:38 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;
171 while ((sig = CURSIG(p)) != 0) {
172 if (have_mplock == 0) {
179 p->p_priority = p->p_usrpri;
180 if (resched_wanted()) {
182 * Since we are curproc, clock will normally just change
183 * our priority without moving us from one queue to another
184 * (since the running process is not on a queue.)
185 * If that happened after we setrunqueue ourselves but before we
186 * mi_switch()'ed, we might not be on the queue indicated by
189 if (have_mplock == 0) {
195 p->p_stats->p_ru.ru_nivcsw++;
198 while ((sig = CURSIG(p)) != 0)
202 * Charge system time if profiling.
204 if (p->p_flag & P_PROFIL) {
205 if (have_mplock == 0) {
209 addupc_task(p, frame->tf_eip,
210 (u_int)(p->p_sticks - oticks) * psratio);
212 curpriority = p->p_priority;
216 #ifdef DEVICE_POLLING
217 extern u_int32_t poll_in_trap;
218 extern int ether_poll __P((int count));
219 #endif /* DEVICE_POLLING */
222 * Exception, fault, and trap interface to the FreeBSD kernel.
223 * This common code is called from assembly language IDT gate entry
224 * routines that prepare a suitable stack frame, and restore this
225 * frame after the exception has been processed.
230 struct trapframe frame;
232 struct proc *p = curproc;
234 int i = 0, ucode = 0, type, code;
239 eva = (frame.tf_trapno == T_PAGEFLT ? rcr2() : 0);
240 trap_fatal(&frame, eva);
245 if (!(frame.tf_eflags & PSL_I)) {
247 * Buggy application or kernel code has disabled interrupts
248 * and then trapped. Enabling interrupts now is wrong, but
249 * it is better than running with interrupts disabled until
250 * they are accidentally enabled later.
252 type = frame.tf_trapno;
253 if (ISPL(frame.tf_cs) == SEL_UPL || (frame.tf_eflags & PSL_VM))
255 "pid %ld (%s): trap %d with interrupts disabled\n",
256 (long)curproc->p_pid, curproc->p_comm, type);
257 else if (type != T_BPTFLT && type != T_TRCTRAP)
259 * XXX not quite right, since this may be for a
260 * multiple fault in user mode.
262 printf("kernel trap %d with interrupts disabled\n",
268 if (frame.tf_trapno == T_PAGEFLT) {
270 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
271 * This problem is worked around by using an interrupt
272 * gate for the pagefault handler. We are finally ready
273 * to read %cr2 and then must reenable interrupts.
275 * XXX this should be in the switch statement, but the
276 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
277 * flow of control too much for this to be obviously
284 #ifdef DEVICE_POLLING
286 ether_poll(poll_in_trap);
287 #endif /* DEVICE_POLLING */
289 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
292 type = frame.tf_trapno;
296 if (frame.tf_eflags & PSL_VM &&
297 (type == T_PROTFLT || type == T_STKFLT)) {
298 i = vm86_emulate((struct vm86frame *)&frame);
301 * returns to original process
303 vm86_trap((struct vm86frame *)&frame);
308 * these traps want either a process context, or
309 * assume a normal userspace trap.
313 trap_fatal(&frame, eva);
316 type = T_BPTFLT; /* kernel breakpoint */
319 goto kernel_trap; /* normal kernel trap handling */
322 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
325 sticks = p->p_sticks;
326 p->p_md.md_regs = &frame;
329 case T_PRIVINFLT: /* privileged instruction fault */
334 case T_BPTFLT: /* bpt instruction fault */
335 case T_TRCTRAP: /* trace trap */
336 frame.tf_eflags &= ~PSL_T;
340 case T_ARITHTRAP: /* arithmetic trap */
345 case T_ASTFLT: /* Allow process switch */
348 if (p->p_flag & P_OWEUPC) {
349 p->p_flag &= ~P_OWEUPC;
350 addupc_task(p, p->p_stats->p_prof.pr_addr,
351 p->p_stats->p_prof.pr_ticks);
356 * The following two traps can happen in
357 * vm86 mode, and, if so, we want to handle
360 case T_PROTFLT: /* general protection fault */
361 case T_STKFLT: /* stack fault */
362 if (frame.tf_eflags & PSL_VM) {
363 i = vm86_emulate((struct vm86frame *)&frame);
370 case T_SEGNPFLT: /* segment not present fault */
371 case T_TSSFLT: /* invalid TSS fault */
372 case T_DOUBLEFLT: /* double fault */
374 ucode = code + BUS_SEGM_FAULT ;
378 case T_PAGEFLT: /* page fault */
379 i = trap_pfault(&frame, TRUE, eva);
382 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
392 case T_DIVIDE: /* integer divide fault */
400 goto handle_powerfail;
401 #else /* !POWERFAIL_NMI */
402 /* machine/parity/power fail/"kitchen sink" faults */
403 if (isa_nmi(code) == 0) {
406 * NMI can be hooked up to a pushbutton
410 printf ("NMI ... going to debugger\n");
411 kdb_trap (type, 0, &frame);
415 } else if (panic_on_nmi)
416 panic("NMI indicates hardware failure");
418 #endif /* POWERFAIL_NMI */
419 #endif /* NISA > 0 */
421 case T_OFLOW: /* integer overflow fault */
426 case T_BOUND: /* bounds check fault */
433 /* if a transparent fault (due to context switch "late") */
437 if (!pmath_emulate) {
439 ucode = FPE_FPU_NP_TRAP;
442 i = (*pmath_emulate)(&frame);
444 if (!(frame.tf_eflags & PSL_T))
446 frame.tf_eflags &= ~PSL_T;
449 /* else ucode = emulator_only_knows() XXX */
452 case T_FPOPFLT: /* FPU operand fetch fault */
457 case T_XMMFLT: /* SIMD floating-point exception */
467 case T_PAGEFLT: /* page fault */
468 (void) trap_pfault(&frame, FALSE, eva);
474 * The kernel is apparently using npx for copying.
475 * XXX this should be fatal unless the kernel has
476 * registered such use.
483 case T_PROTFLT: /* general protection fault */
484 case T_SEGNPFLT: /* segment not present fault */
486 * Invalid segment selectors and out of bounds
487 * %eip's and %esp's can be set up in user mode.
488 * This causes a fault in kernel mode when the
489 * kernel tries to return to user mode. We want
490 * to get this fault so that we can fix the
491 * problem here and not have to check all the
492 * selectors and pointers when the user changes
495 #define MAYBE_DORETI_FAULT(where, whereto) \
497 if (frame.tf_eip == (int)where) { \
498 frame.tf_eip = (int)whereto; \
503 if (intr_nesting_level == 0) {
505 * Invalid %fs's and %gs's can be created using
506 * procfs or PT_SETREGS or by invalidating the
507 * underlying LDT entry. This causes a fault
508 * in kernel mode when the kernel attempts to
509 * switch contexts. Lose the bad context
510 * (XXX) so that we can continue, and generate
513 if (frame.tf_eip == (int)cpu_switch_load_gs) {
514 curthread->td_pcb->pcb_gs = 0;
518 MAYBE_DORETI_FAULT(doreti_iret,
520 MAYBE_DORETI_FAULT(doreti_popl_ds,
521 doreti_popl_ds_fault);
522 MAYBE_DORETI_FAULT(doreti_popl_es,
523 doreti_popl_es_fault);
524 MAYBE_DORETI_FAULT(doreti_popl_fs,
525 doreti_popl_fs_fault);
526 if (curthread->td_pcb->pcb_onfault) {
527 frame.tf_eip = (int)curthread->td_pcb->pcb_onfault;
535 * PSL_NT can be set in user mode and isn't cleared
536 * automatically when the kernel is entered. This
537 * causes a TSS fault when the kernel attempts to
538 * `iret' because the TSS link is uninitialized. We
539 * want to get this fault so that we can fix the
540 * problem here and not every time the kernel is
543 if (frame.tf_eflags & PSL_NT) {
544 frame.tf_eflags &= ~PSL_NT;
549 case T_TRCTRAP: /* trace trap */
550 if (frame.tf_eip == (int)IDTVEC(syscall)) {
552 * We've just entered system mode via the
553 * syscall lcall. Continue single stepping
554 * silently until the syscall handler has
559 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
561 * The syscall handler has now saved the
562 * flags. Stop single stepping it.
564 frame.tf_eflags &= ~PSL_T;
568 * Ignore debug register trace traps due to
569 * accesses in the user's address space, which
570 * can happen under several conditions such as
571 * if a user sets a watchpoint on a buffer and
572 * then passes that buffer to a system call.
573 * We still want to get TRCTRAPS for addresses
574 * in kernel space because that is useful when
575 * debugging the kernel.
577 if (user_dbreg_trap()) {
579 * Reset breakpoint bits because the
582 load_dr6(rdr6() & 0xfffffff0);
586 * Fall through (TRCTRAP kernel mode, kernel address)
590 * If DDB is enabled, let it handle the debugger trap.
591 * Otherwise, debugger traps "can't happen".
594 if (kdb_trap (type, 0, &frame))
603 # define TIMER_FREQ 1193182
607 static unsigned lastalert = 0;
609 if(time_second - lastalert > 10)
611 log(LOG_WARNING, "NMI: power fail\n");
612 sysbeep(TIMER_FREQ/880, hz);
613 lastalert = time_second;
617 #else /* !POWERFAIL_NMI */
618 /* machine/parity/power fail/"kitchen sink" faults */
619 if (isa_nmi(code) == 0) {
622 * NMI can be hooked up to a pushbutton
626 printf ("NMI ... going to debugger\n");
627 kdb_trap (type, 0, &frame);
631 } else if (panic_on_nmi == 0)
634 #endif /* POWERFAIL_NMI */
635 #endif /* NISA > 0 */
638 trap_fatal(&frame, eva);
642 /* Translate fault for emulators (e.g. Linux) */
643 if (*p->p_sysent->sv_transtrap)
644 i = (*p->p_sysent->sv_transtrap)(i, type);
646 trapsignal(p, i, ucode);
649 if (type <= MAX_TRAP_MSG) {
650 uprintf("fatal process exception: %s",
652 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
653 uprintf(", fault VA = 0x%lx", (u_long)eva);
659 userret(p, &frame, sticks, 1);
664 * This version doesn't allow a page fault to user space while
665 * in the kernel. The rest of the kernel needs to be made "safe"
666 * before this can be used. I think the only things remaining
667 * to be made safe are the iBCS2 code and the process tracing/
671 trap_pfault(frame, usermode, eva)
672 struct trapframe *frame;
677 struct vmspace *vm = NULL;
681 struct proc *p = curproc;
683 if (frame->tf_err & PGEX_W)
684 ftype = VM_PROT_WRITE;
686 ftype = VM_PROT_READ;
688 va = trunc_page(eva);
689 if (va < VM_MIN_KERNEL_ADDRESS) {
694 (!usermode && va < VM_MAXUSER_ADDRESS &&
695 (intr_nesting_level != 0 ||
696 curthread->td_pcb->pcb_onfault == NULL))) {
697 trap_fatal(frame, eva);
702 * This is a fault on non-kernel virtual memory.
703 * vm is initialized above to NULL. If curproc is NULL
704 * or curproc->p_vmspace is NULL the fault is fatal.
713 * Keep swapout from messing with us during this
719 * Grow the stack if necessary
721 /* grow_stack returns false only if va falls into
722 * a growable stack region and the stack growth
723 * fails. It returns true if va was not within
724 * a growable stack region, or if the stack
727 if (!grow_stack (p, va)) {
733 /* Fault in the user page: */
734 rv = vm_fault(map, va, ftype,
735 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
741 * Don't allow user-mode faults in kernel address space.
747 * Since we know that kernel virtual address addresses
748 * always have pte pages mapped, we just have to fault
751 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
754 if (rv == KERN_SUCCESS)
758 if (intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
759 frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
762 trap_fatal(frame, eva);
766 /* kludge to pass faulting virtual address to sendsig */
769 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
774 trap_pfault(frame, usermode, eva)
775 struct trapframe *frame;
780 struct vmspace *vm = NULL;
784 struct proc *p = curproc;
786 va = trunc_page(eva);
787 if (va >= KERNBASE) {
789 * Don't allow user-mode faults in kernel address space.
790 * An exception: if the faulting address is the invalid
791 * instruction entry in the IDT, then the Intel Pentium
792 * F00F bug workaround was triggered, and we need to
793 * treat it is as an illegal instruction, and not a page
796 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
797 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
798 frame->tf_trapno = T_PRIVINFLT;
808 * This is a fault on non-kernel virtual memory.
809 * vm is initialized above to NULL. If curproc is NULL
810 * or curproc->p_vmspace is NULL the fault is fatal.
821 if (frame->tf_err & PGEX_W)
822 ftype = VM_PROT_WRITE;
824 ftype = VM_PROT_READ;
826 if (map != kernel_map) {
828 * Keep swapout from messing with us during this
834 * Grow the stack if necessary
836 /* grow_stack returns false only if va falls into
837 * a growable stack region and the stack growth
838 * fails. It returns true if va was not within
839 * a growable stack region, or if the stack
842 if (!grow_stack (p, va)) {
848 /* Fault in the user page: */
849 rv = vm_fault(map, va, ftype,
850 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
856 * Don't have to worry about process locking or stacks in the kernel.
858 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
861 if (rv == KERN_SUCCESS)
865 if (intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
866 frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
869 trap_fatal(frame, eva);
873 /* kludge to pass faulting virtual address to sendsig */
876 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
880 trap_fatal(frame, eva)
881 struct trapframe *frame;
884 int code, type, ss, esp;
885 struct soft_segment_descriptor softseg;
887 code = frame->tf_err;
888 type = frame->tf_trapno;
889 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
891 if (type <= MAX_TRAP_MSG)
892 printf("\n\nFatal trap %d: %s while in %s mode\n",
893 type, trap_msg[type],
894 frame->tf_eflags & PSL_VM ? "vm86" :
895 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
897 /* three seperate prints in case of a trap on an unmapped page */
898 printf("mp_lock = %08x; ", mp_lock);
899 printf("cpuid = %d; ", cpuid);
900 printf("lapic.id = %08x\n", lapic.id);
902 if (type == T_PAGEFLT) {
903 printf("fault virtual address = 0x%x\n", eva);
904 printf("fault code = %s %s, %s\n",
905 code & PGEX_U ? "user" : "supervisor",
906 code & PGEX_W ? "write" : "read",
907 code & PGEX_P ? "protection violation" : "page not present");
909 printf("instruction pointer = 0x%x:0x%x\n",
910 frame->tf_cs & 0xffff, frame->tf_eip);
911 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
912 ss = frame->tf_ss & 0xffff;
915 ss = GSEL(GDATA_SEL, SEL_KPL);
916 esp = (int)&frame->tf_esp;
918 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
919 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
920 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
921 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
922 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
923 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
925 printf("processor eflags = ");
926 if (frame->tf_eflags & PSL_T)
927 printf("trace trap, ");
928 if (frame->tf_eflags & PSL_I)
929 printf("interrupt enabled, ");
930 if (frame->tf_eflags & PSL_NT)
931 printf("nested task, ");
932 if (frame->tf_eflags & PSL_RF)
934 if (frame->tf_eflags & PSL_VM)
936 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
937 printf("current process = ");
940 (u_long)curproc->p_pid, curproc->p_comm ?
941 curproc->p_comm : "");
945 printf("current thread = pri %d ", curthread->td_pri);
946 if (curthread->td_pri >= TDPRI_CRIT)
949 printf("interrupt mask = ");
950 if ((curthread->td_cpl & net_imask) == net_imask)
952 if ((curthread->td_cpl & tty_imask) == tty_imask)
954 if ((curthread->td_cpl & bio_imask) == bio_imask)
956 if ((curthread->td_cpl & cam_imask) == cam_imask)
958 if (curthread->td_cpl == 0)
963 * we probably SHOULD have stopped the other CPUs before now!
964 * another CPU COULD have been touching cpl at this moment...
966 printf(" <- SMP: XXX");
975 if ((debugger_on_panic || db_active) && kdb_trap(type, 0, frame))
978 printf("trap number = %d\n", type);
979 if (type <= MAX_TRAP_MSG)
980 panic("%s", trap_msg[type]);
982 panic("unknown/reserved trap");
986 * Double fault handler. Called when a fault occurs while writing
987 * a frame for a trap/exception onto the stack. This usually occurs
988 * when the stack overflows (such is the case with infinite recursion,
991 * XXX Note that the current PTD gets replaced by IdlePTD when the
992 * task switch occurs. This means that the stack that was active at
993 * the time of the double fault is not available at <kstack> unless
994 * the machine was idle when the double fault occurred. The downside
995 * of this is that "trace <ebp>" in ddb won't work.
1000 printf("\nFatal double fault:\n");
1001 printf("eip = 0x%x\n", common_tss.tss_eip);
1002 printf("esp = 0x%x\n", common_tss.tss_esp);
1003 printf("ebp = 0x%x\n", common_tss.tss_ebp);
1005 /* three seperate prints in case of a trap on an unmapped page */
1006 printf("mp_lock = %08x; ", mp_lock);
1007 printf("cpuid = %d; ", cpuid);
1008 printf("lapic.id = %08x\n", lapic.id);
1010 panic("double fault");
1014 * Compensate for 386 brain damage (missing URKR).
1015 * This is a little simpler than the pagefault handler in trap() because
1016 * it the page tables have already been faulted in and high addresses
1017 * are thrown out early for other reasons.
1027 va = trunc_page((vm_offset_t)addr);
1029 * XXX - MAX is END. Changed > to >= for temp. fix.
1031 if (va >= VM_MAXUSER_ADDRESS)
1039 if (!grow_stack (p, va)) {
1045 * fault the data page
1047 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
1051 if (rv != KERN_SUCCESS)
1058 * syscall2 - MP aware system call request C handler
1060 * A system call is essentially treated as a trap except that the
1061 * MP lock is not held on entry or return. We are responsible for
1062 * obtaining the MP lock if necessary and for handling ASTs
1063 * (e.g. a task switch) prior to return.
1065 * In general, only simple access and manipulation of curproc and
1066 * the current stack is allowed without having to hold MP lock.
1070 struct trapframe frame;
1074 struct sysent *callp;
1075 struct proc *p = curproc;
1076 register_t orig_tf_eflags;
1081 int have_mplock = 0;
1085 if (ISPL(frame.tf_cs) != SEL_UPL) {
1093 * access non-atomic field from critical section. p_sticks is
1094 * updated by the clock interrupt.
1097 sticks = ((volatile struct proc *)p)->p_sticks;
1100 p->p_md.md_regs = &frame;
1101 params = (caddr_t)frame.tf_esp + sizeof(int);
1102 code = frame.tf_eax;
1103 orig_tf_eflags = frame.tf_eflags;
1105 if (p->p_sysent->sv_prepsyscall) {
1107 * The prep code is not MP aware.
1110 (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms);
1114 * Need to check if this is a 32 bit or 64 bit syscall.
1115 * fuword is MP aware.
1117 if (code == SYS_syscall) {
1119 * Code is first argument, followed by actual args.
1121 code = fuword(params);
1122 params += sizeof(int);
1123 } else if (code == SYS___syscall) {
1125 * Like syscall, but code is a quad, so as to maintain
1126 * quad alignment for the rest of the arguments.
1128 code = fuword(params);
1129 params += sizeof(quad_t);
1133 if (p->p_sysent->sv_mask)
1134 code &= p->p_sysent->sv_mask;
1136 if (code >= p->p_sysent->sv_size)
1137 callp = &p->p_sysent->sv_table[0];
1139 callp = &p->p_sysent->sv_table[code];
1141 narg = callp->sy_narg & SYF_ARGMASK;
1144 * copyin is MP aware, but the tracing code is not
1146 if (params && (i = narg * sizeof(int)) &&
1147 (error = copyin(params, (caddr_t)args, (u_int)i))) {
1151 if (KTRPOINT(p, KTR_SYSCALL))
1152 ktrsyscall(p->p_tracep, code, narg, args);
1158 * Try to run the syscall without the MP lock if the syscall
1159 * is MP safe. We have to obtain the MP lock no matter what if
1162 if ((callp->sy_narg & SYF_MPSAFE) == 0) {
1168 if (KTRPOINT(p, KTR_SYSCALL)) {
1169 if (have_mplock == 0) {
1173 ktrsyscall(p->p_tracep, code, narg, args);
1177 p->p_retval[1] = frame.tf_edx;
1179 STOPEVENT(p, S_SCE, narg); /* MP aware */
1181 error = (*callp->sy_call)(args);
1184 * MP SAFE (we may or may not have the MP lock at this point)
1189 * Reinitialize proc pointer `p' as it may be different
1190 * if this is a child returning from fork syscall.
1193 frame.tf_eax = p->p_retval[0];
1194 frame.tf_edx = p->p_retval[1];
1195 frame.tf_eflags &= ~PSL_C;
1200 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1201 * int 0x80 is 2 bytes. We saved this in tf_err.
1203 frame.tf_eip -= frame.tf_err;
1211 if (p->p_sysent->sv_errsize) {
1212 if (error >= p->p_sysent->sv_errsize)
1213 error = -1; /* XXX */
1215 error = p->p_sysent->sv_errtbl[error];
1217 frame.tf_eax = error;
1218 frame.tf_eflags |= PSL_C;
1223 * Traced syscall. trapsignal() is not MP aware.
1225 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1226 if (have_mplock == 0) {
1230 frame.tf_eflags &= ~PSL_T;
1231 trapsignal(p, SIGTRAP, 0);
1235 * Handle reschedule and other end-of-syscall issues
1237 have_mplock = userret(p, &frame, sticks, have_mplock);
1240 if (KTRPOINT(p, KTR_SYSRET)) {
1241 if (have_mplock == 0) {
1245 ktrsysret(p->p_tracep, code, error, p->p_retval[0]);
1250 * This works because errno is findable through the
1251 * register set. If we ever support an emulation where this
1252 * is not the case, this code will need to be revisited.
1254 STOPEVENT(p, S_SCX, code);
1257 * Release the MP lock if we had to get it
1264 * Simplified back end of syscall(), used when returning from fork()
1265 * directly into user mode. MP lock is held on entry and should be
1269 fork_return(p, frame)
1271 struct trapframe frame;
1273 frame.tf_eax = 0; /* Child returns zero */
1274 frame.tf_eflags &= ~PSL_C; /* success */
1277 userret(p, &frame, 0, 1);
1279 if (KTRPOINT(p, KTR_SYSRET))
1280 ktrsysret(p->p_tracep, SYS_fork, 0, 0);