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 $
42 * 386 Trap and System call handling
47 #include "opt_ktrace.h"
48 #include "opt_clock.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/pioctl.h>
55 #include <sys/kernel.h>
56 #include <sys/resourcevar.h>
57 #include <sys/signalvar.h>
58 #include <sys/syscall.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
62 #include <sys/vmmeter.h>
64 #include <sys/ktrace.h>
68 #include <vm/vm_param.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_extern.h>
76 #include <machine/cpu.h>
77 #include <machine/ipl.h>
78 #include <machine/md_var.h>
79 #include <machine/pcb.h>
81 #include <machine/smp.h>
83 #include <machine/tss.h>
85 #include <i386/isa/intr_machdep.h>
88 #include <sys/syslog.h>
89 #include <machine/clock.h>
92 #include <machine/vm86.h>
99 int (*pmath_emulate) __P((struct trapframe *));
101 extern void trap __P((struct trapframe frame));
102 extern int trapwrite __P((unsigned addr));
103 extern void syscall2 __P((struct trapframe frame));
105 static int trap_pfault __P((struct trapframe *, int, vm_offset_t));
106 static void trap_fatal __P((struct trapframe *, vm_offset_t));
107 void dblfault_handler __P((void));
109 extern inthand_t IDTVEC(syscall);
111 #define MAX_TRAP_MSG 28
112 static char *trap_msg[] = {
114 "privileged instruction fault", /* 1 T_PRIVINFLT */
116 "breakpoint instruction fault", /* 3 T_BPTFLT */
119 "arithmetic trap", /* 6 T_ARITHTRAP */
120 "system forced exception", /* 7 T_ASTFLT */
122 "general protection fault", /* 9 T_PROTFLT */
123 "trace trap", /* 10 T_TRCTRAP */
125 "page fault", /* 12 T_PAGEFLT */
127 "alignment fault", /* 14 T_ALIGNFLT */
131 "integer divide fault", /* 18 T_DIVIDE */
132 "non-maskable interrupt trap", /* 19 T_NMI */
133 "overflow trap", /* 20 T_OFLOW */
134 "FPU bounds check fault", /* 21 T_BOUND */
135 "FPU device not available", /* 22 T_DNA */
136 "double fault", /* 23 T_DOUBLEFLT */
137 "FPU operand fetch fault", /* 24 T_FPOPFLT */
138 "invalid TSS fault", /* 25 T_TSSFLT */
139 "segment not present fault", /* 26 T_SEGNPFLT */
140 "stack fault", /* 27 T_STKFLT */
141 "machine check trap", /* 28 T_MCHK */
144 static __inline int userret __P((struct proc *p, struct trapframe *frame,
145 u_quad_t oticks, int have_mplock));
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 userret(p, frame, oticks, have_mplock)
163 struct trapframe *frame;
169 while ((sig = CURSIG(p)) != 0) {
170 if (have_mplock == 0) {
177 p->p_priority = p->p_usrpri;
178 if (resched_wanted()) {
180 * Since we are curproc, clock will normally just change
181 * our priority without moving us from one queue to another
182 * (since the running process is not on a queue.)
183 * If that happened after we setrunqueue ourselves but before we
184 * mi_switch()'ed, we might not be on the queue indicated by
187 if (have_mplock == 0) {
193 p->p_stats->p_ru.ru_nivcsw++;
196 while ((sig = CURSIG(p)) != 0)
200 * Charge system time if profiling.
202 if (p->p_flag & P_PROFIL) {
203 if (have_mplock == 0) {
207 addupc_task(p, frame->tf_eip,
208 (u_int)(p->p_sticks - oticks) * psratio);
210 curpriority = p->p_priority;
214 #ifdef DEVICE_POLLING
215 extern u_int32_t poll_in_trap;
216 extern int ether_poll __P((int count));
217 #endif /* DEVICE_POLLING */
220 * Exception, fault, and trap interface to the FreeBSD kernel.
221 * This common code is called from assembly language IDT gate entry
222 * routines that prepare a suitable stack frame, and restore this
223 * frame after the exception has been processed.
228 struct trapframe frame;
230 struct proc *p = curproc;
232 int i = 0, ucode = 0, type, code;
237 eva = (frame.tf_trapno == T_PAGEFLT ? rcr2() : 0);
238 trap_fatal(&frame, eva);
243 if (!(frame.tf_eflags & PSL_I)) {
245 * Buggy application or kernel code has disabled interrupts
246 * and then trapped. Enabling interrupts now is wrong, but
247 * it is better than running with interrupts disabled until
248 * they are accidentally enabled later.
250 type = frame.tf_trapno;
251 if (ISPL(frame.tf_cs) == SEL_UPL || (frame.tf_eflags & PSL_VM))
253 "pid %ld (%s): trap %d with interrupts disabled\n",
254 (long)curproc->p_pid, curproc->p_comm, type);
255 else if (type != T_BPTFLT && type != T_TRCTRAP)
257 * XXX not quite right, since this may be for a
258 * multiple fault in user mode.
260 printf("kernel trap %d with interrupts disabled\n",
266 if (frame.tf_trapno == T_PAGEFLT) {
268 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
269 * This problem is worked around by using an interrupt
270 * gate for the pagefault handler. We are finally ready
271 * to read %cr2 and then must reenable interrupts.
273 * XXX this should be in the switch statement, but the
274 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
275 * flow of control too much for this to be obviously
282 #ifdef DEVICE_POLLING
284 ether_poll(poll_in_trap);
285 #endif /* DEVICE_POLLING */
287 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
290 type = frame.tf_trapno;
294 if (frame.tf_eflags & PSL_VM &&
295 (type == T_PROTFLT || type == T_STKFLT)) {
296 i = vm86_emulate((struct vm86frame *)&frame);
299 * returns to original process
301 vm86_trap((struct vm86frame *)&frame);
306 * these traps want either a process context, or
307 * assume a normal userspace trap.
311 trap_fatal(&frame, eva);
314 type = T_BPTFLT; /* kernel breakpoint */
317 goto kernel_trap; /* normal kernel trap handling */
320 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
323 sticks = p->p_sticks;
324 p->p_md.md_regs = &frame;
327 case T_PRIVINFLT: /* privileged instruction fault */
332 case T_BPTFLT: /* bpt instruction fault */
333 case T_TRCTRAP: /* trace trap */
334 frame.tf_eflags &= ~PSL_T;
338 case T_ARITHTRAP: /* arithmetic trap */
343 case T_ASTFLT: /* Allow process switch */
346 if (p->p_flag & P_OWEUPC) {
347 p->p_flag &= ~P_OWEUPC;
348 addupc_task(p, p->p_stats->p_prof.pr_addr,
349 p->p_stats->p_prof.pr_ticks);
354 * The following two traps can happen in
355 * vm86 mode, and, if so, we want to handle
358 case T_PROTFLT: /* general protection fault */
359 case T_STKFLT: /* stack fault */
360 if (frame.tf_eflags & PSL_VM) {
361 i = vm86_emulate((struct vm86frame *)&frame);
368 case T_SEGNPFLT: /* segment not present fault */
369 case T_TSSFLT: /* invalid TSS fault */
370 case T_DOUBLEFLT: /* double fault */
372 ucode = code + BUS_SEGM_FAULT ;
376 case T_PAGEFLT: /* page fault */
377 i = trap_pfault(&frame, TRUE, eva);
380 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
390 case T_DIVIDE: /* integer divide fault */
398 goto handle_powerfail;
399 #else /* !POWERFAIL_NMI */
400 /* machine/parity/power fail/"kitchen sink" faults */
401 if (isa_nmi(code) == 0) {
404 * NMI can be hooked up to a pushbutton
408 printf ("NMI ... going to debugger\n");
409 kdb_trap (type, 0, &frame);
413 } else if (panic_on_nmi)
414 panic("NMI indicates hardware failure");
416 #endif /* POWERFAIL_NMI */
417 #endif /* NISA > 0 */
419 case T_OFLOW: /* integer overflow fault */
424 case T_BOUND: /* bounds check fault */
431 /* if a transparent fault (due to context switch "late") */
435 if (!pmath_emulate) {
437 ucode = FPE_FPU_NP_TRAP;
440 i = (*pmath_emulate)(&frame);
442 if (!(frame.tf_eflags & PSL_T))
444 frame.tf_eflags &= ~PSL_T;
447 /* else ucode = emulator_only_knows() XXX */
450 case T_FPOPFLT: /* FPU operand fetch fault */
455 case T_XMMFLT: /* SIMD floating-point exception */
465 case T_PAGEFLT: /* page fault */
466 (void) trap_pfault(&frame, FALSE, eva);
472 * The kernel is apparently using npx for copying.
473 * XXX this should be fatal unless the kernel has
474 * registered such use.
481 case T_PROTFLT: /* general protection fault */
482 case T_SEGNPFLT: /* segment not present fault */
484 * Invalid segment selectors and out of bounds
485 * %eip's and %esp's can be set up in user mode.
486 * This causes a fault in kernel mode when the
487 * kernel tries to return to user mode. We want
488 * to get this fault so that we can fix the
489 * problem here and not have to check all the
490 * selectors and pointers when the user changes
493 #define MAYBE_DORETI_FAULT(where, whereto) \
495 if (frame.tf_eip == (int)where) { \
496 frame.tf_eip = (int)whereto; \
501 if (intr_nesting_level == 0) {
503 * Invalid %fs's and %gs's can be created using
504 * procfs or PT_SETREGS or by invalidating the
505 * underlying LDT entry. This causes a fault
506 * in kernel mode when the kernel attempts to
507 * switch contexts. Lose the bad context
508 * (XXX) so that we can continue, and generate
511 if (frame.tf_eip == (int)cpu_switch_load_gs) {
516 MAYBE_DORETI_FAULT(doreti_iret,
518 MAYBE_DORETI_FAULT(doreti_popl_ds,
519 doreti_popl_ds_fault);
520 MAYBE_DORETI_FAULT(doreti_popl_es,
521 doreti_popl_es_fault);
522 MAYBE_DORETI_FAULT(doreti_popl_fs,
523 doreti_popl_fs_fault);
524 if (curpcb && curpcb->pcb_onfault) {
525 frame.tf_eip = (int)curpcb->pcb_onfault;
533 * PSL_NT can be set in user mode and isn't cleared
534 * automatically when the kernel is entered. This
535 * causes a TSS fault when the kernel attempts to
536 * `iret' because the TSS link is uninitialized. We
537 * want to get this fault so that we can fix the
538 * problem here and not every time the kernel is
541 if (frame.tf_eflags & PSL_NT) {
542 frame.tf_eflags &= ~PSL_NT;
547 case T_TRCTRAP: /* trace trap */
548 if (frame.tf_eip == (int)IDTVEC(syscall)) {
550 * We've just entered system mode via the
551 * syscall lcall. Continue single stepping
552 * silently until the syscall handler has
557 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
559 * The syscall handler has now saved the
560 * flags. Stop single stepping it.
562 frame.tf_eflags &= ~PSL_T;
566 * Ignore debug register trace traps due to
567 * accesses in the user's address space, which
568 * can happen under several conditions such as
569 * if a user sets a watchpoint on a buffer and
570 * then passes that buffer to a system call.
571 * We still want to get TRCTRAPS for addresses
572 * in kernel space because that is useful when
573 * debugging the kernel.
575 if (user_dbreg_trap()) {
577 * Reset breakpoint bits because the
580 load_dr6(rdr6() & 0xfffffff0);
584 * Fall through (TRCTRAP kernel mode, kernel address)
588 * If DDB is enabled, let it handle the debugger trap.
589 * Otherwise, debugger traps "can't happen".
592 if (kdb_trap (type, 0, &frame))
601 # define TIMER_FREQ 1193182
605 static unsigned lastalert = 0;
607 if(time_second - lastalert > 10)
609 log(LOG_WARNING, "NMI: power fail\n");
610 sysbeep(TIMER_FREQ/880, hz);
611 lastalert = time_second;
615 #else /* !POWERFAIL_NMI */
616 /* machine/parity/power fail/"kitchen sink" faults */
617 if (isa_nmi(code) == 0) {
620 * NMI can be hooked up to a pushbutton
624 printf ("NMI ... going to debugger\n");
625 kdb_trap (type, 0, &frame);
629 } else if (panic_on_nmi == 0)
632 #endif /* POWERFAIL_NMI */
633 #endif /* NISA > 0 */
636 trap_fatal(&frame, eva);
640 /* Translate fault for emulators (e.g. Linux) */
641 if (*p->p_sysent->sv_transtrap)
642 i = (*p->p_sysent->sv_transtrap)(i, type);
644 trapsignal(p, i, ucode);
647 if (type <= MAX_TRAP_MSG) {
648 uprintf("fatal process exception: %s",
650 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
651 uprintf(", fault VA = 0x%lx", (u_long)eva);
657 userret(p, &frame, sticks, 1);
662 * This version doesn't allow a page fault to user space while
663 * in the kernel. The rest of the kernel needs to be made "safe"
664 * before this can be used. I think the only things remaining
665 * to be made safe are the iBCS2 code and the process tracing/
669 trap_pfault(frame, usermode, eva)
670 struct trapframe *frame;
675 struct vmspace *vm = NULL;
679 struct proc *p = curproc;
681 if (frame->tf_err & PGEX_W)
682 ftype = VM_PROT_WRITE;
684 ftype = VM_PROT_READ;
686 va = trunc_page(eva);
687 if (va < VM_MIN_KERNEL_ADDRESS) {
692 (!usermode && va < VM_MAXUSER_ADDRESS &&
693 (intr_nesting_level != 0 || curpcb == NULL ||
694 curpcb->pcb_onfault == NULL))) {
695 trap_fatal(frame, eva);
700 * This is a fault on non-kernel virtual memory.
701 * vm is initialized above to NULL. If curproc is NULL
702 * or curproc->p_vmspace is NULL the fault is fatal.
711 * Keep swapout from messing with us during this
717 * Grow the stack if necessary
719 /* grow_stack returns false only if va falls into
720 * a growable stack region and the stack growth
721 * fails. It returns true if va was not within
722 * a growable stack region, or if the stack
725 if (!grow_stack (p, va)) {
731 /* Fault in the user page: */
732 rv = vm_fault(map, va, ftype,
733 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
739 * Don't allow user-mode faults in kernel address space.
745 * Since we know that kernel virtual address addresses
746 * always have pte pages mapped, we just have to fault
749 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
752 if (rv == KERN_SUCCESS)
756 if (intr_nesting_level == 0 && curpcb && curpcb->pcb_onfault) {
757 frame->tf_eip = (int)curpcb->pcb_onfault;
760 trap_fatal(frame, eva);
764 /* kludge to pass faulting virtual address to sendsig */
767 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
772 trap_pfault(frame, usermode, eva)
773 struct trapframe *frame;
778 struct vmspace *vm = NULL;
782 struct proc *p = curproc;
784 va = trunc_page(eva);
785 if (va >= KERNBASE) {
787 * Don't allow user-mode faults in kernel address space.
788 * An exception: if the faulting address is the invalid
789 * instruction entry in the IDT, then the Intel Pentium
790 * F00F bug workaround was triggered, and we need to
791 * treat it is as an illegal instruction, and not a page
794 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
795 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
796 frame->tf_trapno = T_PRIVINFLT;
806 * This is a fault on non-kernel virtual memory.
807 * vm is initialized above to NULL. If curproc is NULL
808 * or curproc->p_vmspace is NULL the fault is fatal.
819 if (frame->tf_err & PGEX_W)
820 ftype = VM_PROT_WRITE;
822 ftype = VM_PROT_READ;
824 if (map != kernel_map) {
826 * Keep swapout from messing with us during this
832 * Grow the stack if necessary
834 /* grow_stack returns false only if va falls into
835 * a growable stack region and the stack growth
836 * fails. It returns true if va was not within
837 * a growable stack region, or if the stack
840 if (!grow_stack (p, va)) {
846 /* Fault in the user page: */
847 rv = vm_fault(map, va, ftype,
848 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
854 * Don't have to worry about process locking or stacks in the kernel.
856 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
859 if (rv == KERN_SUCCESS)
863 if (intr_nesting_level == 0 && curpcb && curpcb->pcb_onfault) {
864 frame->tf_eip = (int)curpcb->pcb_onfault;
867 trap_fatal(frame, eva);
871 /* kludge to pass faulting virtual address to sendsig */
874 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
878 trap_fatal(frame, eva)
879 struct trapframe *frame;
882 int code, type, ss, esp;
883 struct soft_segment_descriptor softseg;
885 code = frame->tf_err;
886 type = frame->tf_trapno;
887 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
889 if (type <= MAX_TRAP_MSG)
890 printf("\n\nFatal trap %d: %s while in %s mode\n",
891 type, trap_msg[type],
892 frame->tf_eflags & PSL_VM ? "vm86" :
893 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
895 /* three seperate prints in case of a trap on an unmapped page */
896 printf("mp_lock = %08x; ", mp_lock);
897 printf("cpuid = %d; ", cpuid);
898 printf("lapic.id = %08x\n", lapic.id);
900 if (type == T_PAGEFLT) {
901 printf("fault virtual address = 0x%x\n", eva);
902 printf("fault code = %s %s, %s\n",
903 code & PGEX_U ? "user" : "supervisor",
904 code & PGEX_W ? "write" : "read",
905 code & PGEX_P ? "protection violation" : "page not present");
907 printf("instruction pointer = 0x%x:0x%x\n",
908 frame->tf_cs & 0xffff, frame->tf_eip);
909 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
910 ss = frame->tf_ss & 0xffff;
913 ss = GSEL(GDATA_SEL, SEL_KPL);
914 esp = (int)&frame->tf_esp;
916 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
917 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
918 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
919 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
920 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
921 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
923 printf("processor eflags = ");
924 if (frame->tf_eflags & PSL_T)
925 printf("trace trap, ");
926 if (frame->tf_eflags & PSL_I)
927 printf("interrupt enabled, ");
928 if (frame->tf_eflags & PSL_NT)
929 printf("nested task, ");
930 if (frame->tf_eflags & PSL_RF)
932 if (frame->tf_eflags & PSL_VM)
934 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
935 printf("current process = ");
938 (u_long)curproc->p_pid, curproc->p_comm ?
939 curproc->p_comm : "");
943 printf("interrupt mask = ");
944 if ((cpl & net_imask) == net_imask)
946 if ((cpl & tty_imask) == tty_imask)
948 if ((cpl & bio_imask) == bio_imask)
950 if ((cpl & cam_imask) == cam_imask)
957 * we probably SHOULD have stopped the other CPUs before now!
958 * another CPU COULD have been touching cpl at this moment...
960 printf(" <- SMP: XXX");
969 if ((debugger_on_panic || db_active) && kdb_trap(type, 0, frame))
972 printf("trap number = %d\n", type);
973 if (type <= MAX_TRAP_MSG)
974 panic("%s", trap_msg[type]);
976 panic("unknown/reserved trap");
980 * Double fault handler. Called when a fault occurs while writing
981 * a frame for a trap/exception onto the stack. This usually occurs
982 * when the stack overflows (such is the case with infinite recursion,
985 * XXX Note that the current PTD gets replaced by IdlePTD when the
986 * task switch occurs. This means that the stack that was active at
987 * the time of the double fault is not available at <kstack> unless
988 * the machine was idle when the double fault occurred. The downside
989 * of this is that "trace <ebp>" in ddb won't work.
994 printf("\nFatal double fault:\n");
995 printf("eip = 0x%x\n", common_tss.tss_eip);
996 printf("esp = 0x%x\n", common_tss.tss_esp);
997 printf("ebp = 0x%x\n", common_tss.tss_ebp);
999 /* three seperate prints in case of a trap on an unmapped page */
1000 printf("mp_lock = %08x; ", mp_lock);
1001 printf("cpuid = %d; ", cpuid);
1002 printf("lapic.id = %08x\n", lapic.id);
1004 panic("double fault");
1008 * Compensate for 386 brain damage (missing URKR).
1009 * This is a little simpler than the pagefault handler in trap() because
1010 * it the page tables have already been faulted in and high addresses
1011 * are thrown out early for other reasons.
1021 va = trunc_page((vm_offset_t)addr);
1023 * XXX - MAX is END. Changed > to >= for temp. fix.
1025 if (va >= VM_MAXUSER_ADDRESS)
1033 if (!grow_stack (p, va)) {
1039 * fault the data page
1041 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
1045 if (rv != KERN_SUCCESS)
1052 * syscall2 - MP aware system call request C handler
1054 * A system call is essentially treated as a trap except that the
1055 * MP lock is not held on entry or return. We are responsible for
1056 * obtaining the MP lock if necessary and for handling ASTs
1057 * (e.g. a task switch) prior to return.
1059 * In general, only simple access and manipulation of curproc and
1060 * the current stack is allowed without having to hold MP lock.
1064 struct trapframe frame;
1068 struct sysent *callp;
1069 struct proc *p = curproc;
1070 register_t orig_tf_eflags;
1075 int have_mplock = 0;
1079 if (ISPL(frame.tf_cs) != SEL_UPL) {
1087 * handle atomicy by looping since interrupts are enabled and the
1088 * MP lock is not held.
1090 sticks = ((volatile struct proc *)p)->p_sticks;
1091 while (sticks != ((volatile struct proc *)p)->p_sticks)
1092 sticks = ((volatile struct proc *)p)->p_sticks;
1094 p->p_md.md_regs = &frame;
1095 params = (caddr_t)frame.tf_esp + sizeof(int);
1096 code = frame.tf_eax;
1097 orig_tf_eflags = frame.tf_eflags;
1099 if (p->p_sysent->sv_prepsyscall) {
1101 * The prep code is not MP aware.
1104 (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms);
1108 * Need to check if this is a 32 bit or 64 bit syscall.
1109 * fuword is MP aware.
1111 if (code == SYS_syscall) {
1113 * Code is first argument, followed by actual args.
1115 code = fuword(params);
1116 params += sizeof(int);
1117 } else if (code == SYS___syscall) {
1119 * Like syscall, but code is a quad, so as to maintain
1120 * quad alignment for the rest of the arguments.
1122 code = fuword(params);
1123 params += sizeof(quad_t);
1127 if (p->p_sysent->sv_mask)
1128 code &= p->p_sysent->sv_mask;
1130 if (code >= p->p_sysent->sv_size)
1131 callp = &p->p_sysent->sv_table[0];
1133 callp = &p->p_sysent->sv_table[code];
1135 narg = callp->sy_narg & SYF_ARGMASK;
1138 * copyin is MP aware, but the tracing code is not
1140 if (params && (i = narg * sizeof(int)) &&
1141 (error = copyin(params, (caddr_t)args, (u_int)i))) {
1145 if (KTRPOINT(p, KTR_SYSCALL))
1146 ktrsyscall(p->p_tracep, code, narg, args);
1152 * Try to run the syscall without the MP lock if the syscall
1153 * is MP safe. We have to obtain the MP lock no matter what if
1156 if ((callp->sy_narg & SYF_MPSAFE) == 0) {
1162 if (KTRPOINT(p, KTR_SYSCALL)) {
1163 if (have_mplock == 0) {
1167 ktrsyscall(p->p_tracep, code, narg, args);
1171 p->p_retval[1] = frame.tf_edx;
1173 STOPEVENT(p, S_SCE, narg); /* MP aware */
1175 error = (*callp->sy_call)(p, args);
1178 * MP SAFE (we may or may not have the MP lock at this point)
1183 * Reinitialize proc pointer `p' as it may be different
1184 * if this is a child returning from fork syscall.
1187 frame.tf_eax = p->p_retval[0];
1188 frame.tf_edx = p->p_retval[1];
1189 frame.tf_eflags &= ~PSL_C;
1194 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1195 * int 0x80 is 2 bytes. We saved this in tf_err.
1197 frame.tf_eip -= frame.tf_err;
1205 if (p->p_sysent->sv_errsize) {
1206 if (error >= p->p_sysent->sv_errsize)
1207 error = -1; /* XXX */
1209 error = p->p_sysent->sv_errtbl[error];
1211 frame.tf_eax = error;
1212 frame.tf_eflags |= PSL_C;
1217 * Traced syscall. trapsignal() is not MP aware.
1219 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1220 if (have_mplock == 0) {
1224 frame.tf_eflags &= ~PSL_T;
1225 trapsignal(p, SIGTRAP, 0);
1229 * Handle reschedule and other end-of-syscall issues
1231 have_mplock = userret(p, &frame, sticks, have_mplock);
1234 if (KTRPOINT(p, KTR_SYSRET)) {
1235 if (have_mplock == 0) {
1239 ktrsysret(p->p_tracep, code, error, p->p_retval[0]);
1244 * This works because errno is findable through the
1245 * register set. If we ever support an emulation where this
1246 * is not the case, this code will need to be revisited.
1248 STOPEVENT(p, S_SCX, code);
1251 * Release the MP lock if we had to get it
1258 * Simplified back end of syscall(), used when returning from fork()
1259 * directly into user mode. MP lock is held on entry and should be
1263 fork_return(p, frame)
1265 struct trapframe frame;
1267 frame.tf_eax = 0; /* Child returns zero */
1268 frame.tf_eflags &= ~PSL_C; /* success */
1271 userret(p, &frame, 0, 1);
1273 if (KTRPOINT(p, KTR_SYSRET))
1274 ktrsysret(p->p_tracep, SYS_fork, 0, 0);