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.35 2003/10/16 22:26:35 dillon Exp $
43 * 386 Trap and System call handling
51 #include "opt_ktrace.h"
52 #include "opt_clock.h"
55 #include <sys/param.h>
56 #include <sys/systm.h>
58 #include <sys/pioctl.h>
59 #include <sys/kernel.h>
60 #include <sys/resourcevar.h>
61 #include <sys/signalvar.h>
62 #include <sys/syscall.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
66 #include <sys/vmmeter.h>
67 #include <sys/malloc.h>
69 #include <sys/ktrace.h>
71 #include <sys/sysproto.h>
72 #include <sys/sysunion.h>
75 #include <vm/vm_param.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_extern.h>
83 #include <machine/cpu.h>
84 #include <machine/ipl.h>
85 #include <machine/md_var.h>
86 #include <machine/pcb.h>
88 #include <machine/smp.h>
90 #include <machine/tss.h>
91 #include <machine/globaldata.h>
93 #include <i386/isa/intr_machdep.h>
96 #include <sys/syslog.h>
97 #include <machine/clock.h>
100 #include <machine/vm86.h>
103 #include <sys/msgport2.h>
104 #include <sys/thread2.h>
106 int (*pmath_emulate) (struct trapframe *);
108 extern void trap (struct trapframe frame);
109 extern int trapwrite (unsigned addr);
110 extern void syscall2 (struct trapframe frame);
111 extern void sendsys2 (struct trapframe frame);
113 static int trap_pfault (struct trapframe *, int, vm_offset_t);
114 static void trap_fatal (struct trapframe *, vm_offset_t);
115 void dblfault_handler (void);
117 extern inthand_t IDTVEC(syscall);
119 #define MAX_TRAP_MSG 28
120 static char *trap_msg[] = {
122 "privileged instruction fault", /* 1 T_PRIVINFLT */
124 "breakpoint instruction fault", /* 3 T_BPTFLT */
127 "arithmetic trap", /* 6 T_ARITHTRAP */
128 "system forced exception", /* 7 T_ASTFLT */
130 "general protection fault", /* 9 T_PROTFLT */
131 "trace trap", /* 10 T_TRCTRAP */
133 "page fault", /* 12 T_PAGEFLT */
135 "alignment fault", /* 14 T_ALIGNFLT */
139 "integer divide fault", /* 18 T_DIVIDE */
140 "non-maskable interrupt trap", /* 19 T_NMI */
141 "overflow trap", /* 20 T_OFLOW */
142 "FPU bounds check fault", /* 21 T_BOUND */
143 "FPU device not available", /* 22 T_DNA */
144 "double fault", /* 23 T_DOUBLEFLT */
145 "FPU operand fetch fault", /* 24 T_FPOPFLT */
146 "invalid TSS fault", /* 25 T_TSSFLT */
147 "segment not present fault", /* 26 T_SEGNPFLT */
148 "stack fault", /* 27 T_STKFLT */
149 "machine check trap", /* 28 T_MCHK */
152 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
153 extern int has_f00f_bug;
157 static int ddb_on_nmi = 1;
158 SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
159 &ddb_on_nmi, 0, "Go to DDB on NMI");
161 static int panic_on_nmi = 1;
162 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
163 &panic_on_nmi, 0, "Panic on NMI");
164 static int fast_release;
165 SYSCTL_INT(_machdep, OID_AUTO, fast_release, CTLFLAG_RW,
166 &fast_release, 0, "Passive Release was optimal");
167 static int slow_release;
168 SYSCTL_INT(_machdep, OID_AUTO, slow_release, CTLFLAG_RW,
169 &slow_release, 0, "Passive Release was nonoptimal");
171 MALLOC_DEFINE(M_SYSMSG, "sysmsg", "sysmsg structure");
174 * USER->KERNEL transition. Do not transition us out of userland from the
175 * point of view of the userland scheduler unless we actually have to
178 * usertdsw is called from within a critical section and the BGL will still
179 * be held. This function is NOT called for preemptions, only for switchouts.
182 passive_release(struct thread *td)
184 struct proc *p = td->td_proc;
186 lwkt_setpri_self(TDPRI_KERN_USER);
187 if (p->p_flag & P_CURPROC)
188 release_curproc(p, (td->td_flags & TDF_RUNQ) == 0);
189 if ((p->p_flag & P_CURPROC) == 0)
190 td->td_release = NULL;
194 * userenter() passively intercepts the thread switch function to increase
195 * the thread priority from a user priority to a kernel priority, reducing
196 * syscall and trap overhead for the case where no switch occurs.
200 userenter(struct thread *curtd)
202 curtd->td_release = passive_release;
206 userexit(struct proc *p)
208 struct thread *td = p->p_thread;
211 * Reacquire our P_CURPROC status and adjust the LWKT priority
212 * for our return to userland. We can fast path the case where
213 * td_release was not called and P_CURPROC is still set, otherwise
214 * do it the slow way.
216 * Lowering our priority may make other higher priority threads
217 * runnable. lwkt_setpri_self() does not switch away, so call
218 * lwkt_maybe_switch() to deal with it.
220 if (td->td_release && (p->p_flag & P_CURPROC)) {
222 td->td_release = NULL;
226 switch(p->p_rtprio.type) {
228 lwkt_setpri_self(TDPRI_USER_IDLE);
230 case RTP_PRIO_REALTIME:
232 lwkt_setpri_self(TDPRI_USER_REAL);
235 lwkt_setpri_self(TDPRI_USER_NORM);
244 userret(struct proc *p, struct trapframe *frame, u_quad_t oticks)
249 * Post any pending signals
251 while ((sig = CURSIG(p)) != 0) {
256 * If a reschedule has been requested then the easiest solution
257 * is to run our passive release function which will possibly
258 * shift our P_CURPROC designation to another user process.
260 * A reschedule can also occur due to a higher priority LWKT thread
261 * becoming runable, we have to call lwkt_maybe_switch() to deal
264 if (resched_wanted()) {
265 if (curthread->td_release)
266 passive_release(curthread);
271 * Charge system time if profiling. Note: times are in microseconds.
273 if (p->p_flag & P_PROFIL) {
274 addupc_task(p, frame->tf_eip,
275 (u_int)(curthread->td_sticks - oticks));
279 * Post any pending signals XXX
281 while ((sig = CURSIG(p)) != 0)
285 #ifdef DEVICE_POLLING
286 extern u_int32_t poll_in_trap;
287 extern int ether_poll (int count);
288 #endif /* DEVICE_POLLING */
291 * Exception, fault, and trap interface to the FreeBSD kernel.
292 * This common code is called from assembly language IDT gate entry
293 * routines that prepare a suitable stack frame, and restore this
294 * frame after the exception has been processed.
296 * This function is also called from doreti in an interlock to handle ASTs.
297 * For example: hardwareint->INTROUTINE->(set ast)->doreti->trap
299 * NOTE! We have to retrieve the fault address prior to obtaining the
300 * MP lock because get_mplock() may switch out. YYY cr2 really ought
301 * to be retrieved by the assembly code, not here.
305 struct trapframe frame;
307 struct thread *td = curthread;
310 int i = 0, ucode = 0, type, code;
316 eva = (frame.tf_trapno == T_PAGEFLT ? rcr2() : 0);
318 trap_fatal(&frame, eva);
324 if (frame.tf_trapno == T_PAGEFLT) {
326 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
327 * This problem is worked around by using an interrupt
328 * gate for the pagefault handler. We are finally ready
329 * to read %cr2 and then must reenable interrupts.
331 * XXX this should be in the switch statement, but the
332 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
333 * flow of control too much for this to be obviously
343 * MP lock is held at this point
346 if (!(frame.tf_eflags & PSL_I)) {
348 * Buggy application or kernel code has disabled interrupts
349 * and then trapped. Enabling interrupts now is wrong, but
350 * it is better than running with interrupts disabled until
351 * they are accidentally enabled later.
353 type = frame.tf_trapno;
354 if (ISPL(frame.tf_cs)==SEL_UPL || (frame.tf_eflags & PSL_VM)) {
356 "pid %ld (%s): trap %d with interrupts disabled\n",
357 (long)curproc->p_pid, curproc->p_comm, type);
358 } else if (type != T_BPTFLT && type != T_TRCTRAP) {
360 * XXX not quite right, since this may be for a
361 * multiple fault in user mode.
363 printf("kernel trap %d with interrupts disabled\n",
370 #ifdef DEVICE_POLLING
372 ether_poll(poll_in_trap);
373 #endif /* DEVICE_POLLING */
375 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
378 type = frame.tf_trapno;
382 if (frame.tf_eflags & PSL_VM &&
383 (type == T_PROTFLT || type == T_STKFLT)) {
385 KKASSERT(curthread->td_mpcount > 0);
387 i = vm86_emulate((struct vm86frame *)&frame);
389 KKASSERT(curthread->td_mpcount > 0);
393 * returns to original process
395 vm86_trap((struct vm86frame *)&frame);
402 * these traps want either a process context, or
403 * assume a normal userspace trap.
407 trap_fatal(&frame, eva);
410 type = T_BPTFLT; /* kernel breakpoint */
413 goto kernel_trap; /* normal kernel trap handling */
416 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
421 sticks = curthread->td_sticks;
422 p->p_md.md_regs = &frame;
425 case T_PRIVINFLT: /* privileged instruction fault */
430 case T_BPTFLT: /* bpt instruction fault */
431 case T_TRCTRAP: /* trace trap */
432 frame.tf_eflags &= ~PSL_T;
436 case T_ARITHTRAP: /* arithmetic trap */
441 case T_ASTFLT: /* Allow process switch */
442 mycpu->gd_cnt.v_soft++;
443 if (mycpu->gd_reqflags & RQF_AST_OWEUPC) {
444 atomic_clear_int_nonlocked(&mycpu->gd_reqflags,
446 addupc_task(p, p->p_stats->p_prof.pr_addr,
447 p->p_stats->p_prof.pr_ticks);
452 * The following two traps can happen in
453 * vm86 mode, and, if so, we want to handle
456 case T_PROTFLT: /* general protection fault */
457 case T_STKFLT: /* stack fault */
458 if (frame.tf_eflags & PSL_VM) {
459 i = vm86_emulate((struct vm86frame *)&frame);
466 case T_SEGNPFLT: /* segment not present fault */
467 case T_TSSFLT: /* invalid TSS fault */
468 case T_DOUBLEFLT: /* double fault */
470 ucode = code + BUS_SEGM_FAULT ;
474 case T_PAGEFLT: /* page fault */
475 i = trap_pfault(&frame, TRUE, eva);
478 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
488 case T_DIVIDE: /* integer divide fault */
496 goto handle_powerfail;
497 #else /* !POWERFAIL_NMI */
498 /* machine/parity/power fail/"kitchen sink" faults */
499 if (isa_nmi(code) == 0) {
502 * NMI can be hooked up to a pushbutton
506 printf ("NMI ... going to debugger\n");
507 kdb_trap (type, 0, &frame);
511 } else if (panic_on_nmi)
512 panic("NMI indicates hardware failure");
514 #endif /* POWERFAIL_NMI */
515 #endif /* NISA > 0 */
517 case T_OFLOW: /* integer overflow fault */
522 case T_BOUND: /* bounds check fault */
529 /* if a transparent fault (due to context switch "late") */
533 if (!pmath_emulate) {
535 ucode = FPE_FPU_NP_TRAP;
538 i = (*pmath_emulate)(&frame);
540 if (!(frame.tf_eflags & PSL_T))
542 frame.tf_eflags &= ~PSL_T;
545 /* else ucode = emulator_only_knows() XXX */
548 case T_FPOPFLT: /* FPU operand fetch fault */
553 case T_XMMFLT: /* SIMD floating-point exception */
563 case T_PAGEFLT: /* page fault */
564 (void) trap_pfault(&frame, FALSE, eva);
570 * The kernel is apparently using npx for copying.
571 * XXX this should be fatal unless the kernel has
572 * registered such use.
579 case T_PROTFLT: /* general protection fault */
580 case T_SEGNPFLT: /* segment not present fault */
582 * Invalid segment selectors and out of bounds
583 * %eip's and %esp's can be set up in user mode.
584 * This causes a fault in kernel mode when the
585 * kernel tries to return to user mode. We want
586 * to get this fault so that we can fix the
587 * problem here and not have to check all the
588 * selectors and pointers when the user changes
591 #define MAYBE_DORETI_FAULT(where, whereto) \
593 if (frame.tf_eip == (int)where) { \
594 frame.tf_eip = (int)whereto; \
598 if (mycpu->gd_intr_nesting_level == 0) {
600 * Invalid %fs's and %gs's can be created using
601 * procfs or PT_SETREGS or by invalidating the
602 * underlying LDT entry. This causes a fault
603 * in kernel mode when the kernel attempts to
604 * switch contexts. Lose the bad context
605 * (XXX) so that we can continue, and generate
608 if (frame.tf_eip == (int)cpu_switch_load_gs) {
609 curthread->td_pcb->pcb_gs = 0;
613 MAYBE_DORETI_FAULT(doreti_iret,
615 MAYBE_DORETI_FAULT(doreti_popl_ds,
616 doreti_popl_ds_fault);
617 MAYBE_DORETI_FAULT(doreti_popl_es,
618 doreti_popl_es_fault);
619 MAYBE_DORETI_FAULT(doreti_popl_fs,
620 doreti_popl_fs_fault);
621 if (curthread->td_pcb->pcb_onfault) {
622 frame.tf_eip = (int)curthread->td_pcb->pcb_onfault;
630 * PSL_NT can be set in user mode and isn't cleared
631 * automatically when the kernel is entered. This
632 * causes a TSS fault when the kernel attempts to
633 * `iret' because the TSS link is uninitialized. We
634 * want to get this fault so that we can fix the
635 * problem here and not every time the kernel is
638 if (frame.tf_eflags & PSL_NT) {
639 frame.tf_eflags &= ~PSL_NT;
644 case T_TRCTRAP: /* trace trap */
645 if (frame.tf_eip == (int)IDTVEC(syscall)) {
647 * We've just entered system mode via the
648 * syscall lcall. Continue single stepping
649 * silently until the syscall handler has
654 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
656 * The syscall handler has now saved the
657 * flags. Stop single stepping it.
659 frame.tf_eflags &= ~PSL_T;
663 * Ignore debug register trace traps due to
664 * accesses in the user's address space, which
665 * can happen under several conditions such as
666 * if a user sets a watchpoint on a buffer and
667 * then passes that buffer to a system call.
668 * We still want to get TRCTRAPS for addresses
669 * in kernel space because that is useful when
670 * debugging the kernel.
672 if (user_dbreg_trap()) {
674 * Reset breakpoint bits because the
677 load_dr6(rdr6() & 0xfffffff0);
681 * Fall through (TRCTRAP kernel mode, kernel address)
685 * If DDB is enabled, let it handle the debugger trap.
686 * Otherwise, debugger traps "can't happen".
689 if (kdb_trap (type, 0, &frame))
698 # define TIMER_FREQ 1193182
702 static unsigned lastalert = 0;
704 if(time_second - lastalert > 10)
706 log(LOG_WARNING, "NMI: power fail\n");
707 sysbeep(TIMER_FREQ/880, hz);
708 lastalert = time_second;
713 #else /* !POWERFAIL_NMI */
714 /* machine/parity/power fail/"kitchen sink" faults */
715 if (isa_nmi(code) == 0) {
718 * NMI can be hooked up to a pushbutton
722 printf ("NMI ... going to debugger\n");
723 kdb_trap (type, 0, &frame);
727 } else if (panic_on_nmi == 0)
730 #endif /* POWERFAIL_NMI */
731 #endif /* NISA > 0 */
734 trap_fatal(&frame, eva);
738 /* Translate fault for emulators (e.g. Linux) */
739 if (*p->p_sysent->sv_transtrap)
740 i = (*p->p_sysent->sv_transtrap)(i, type);
742 trapsignal(p, i, ucode);
745 if (type <= MAX_TRAP_MSG) {
746 uprintf("fatal process exception: %s",
748 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
749 uprintf(", fault VA = 0x%lx", (u_long)eva);
756 if (ISPL(frame.tf_cs) == SEL_UPL)
757 KASSERT(curthread->td_mpcount == 1, ("badmpcount trap from %p", (void *)frame.tf_eip));
759 userret(p, &frame, sticks);
763 KKASSERT(curthread->td_mpcount > 0);
770 * This version doesn't allow a page fault to user space while
771 * in the kernel. The rest of the kernel needs to be made "safe"
772 * before this can be used. I think the only things remaining
773 * to be made safe are the iBCS2 code and the process tracing/
777 trap_pfault(frame, usermode, eva)
778 struct trapframe *frame;
783 struct vmspace *vm = NULL;
787 struct proc *p = curproc;
789 if (frame->tf_err & PGEX_W)
790 ftype = VM_PROT_WRITE;
792 ftype = VM_PROT_READ;
794 va = trunc_page(eva);
795 if (va < VM_MIN_KERNEL_ADDRESS) {
800 (!usermode && va < VM_MAXUSER_ADDRESS &&
801 (mycpu->gd_intr_nesting_level != 0 ||
802 curthread->td_pcb->pcb_onfault == NULL))) {
803 trap_fatal(frame, eva);
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.
819 * Keep swapout from messing with us during this
825 * Grow the stack if necessary
827 /* grow_stack returns false only if va falls into
828 * a growable stack region and the stack growth
829 * fails. It returns true if va was not within
830 * a growable stack region, or if the stack
833 if (!grow_stack (p, va)) {
839 /* Fault in the user page: */
840 rv = vm_fault(map, va, ftype,
841 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
847 * Don't allow user-mode faults in kernel address space.
853 * Since we know that kernel virtual address addresses
854 * always have pte pages mapped, we just have to fault
857 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
860 if (rv == KERN_SUCCESS)
864 if (mycpu->gd_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);
880 trap_pfault(frame, usermode, eva)
881 struct trapframe *frame;
886 struct vmspace *vm = NULL;
890 struct proc *p = curproc;
892 va = trunc_page(eva);
893 if (va >= KERNBASE) {
895 * Don't allow user-mode faults in kernel address space.
896 * An exception: if the faulting address is the invalid
897 * instruction entry in the IDT, then the Intel Pentium
898 * F00F bug workaround was triggered, and we need to
899 * treat it is as an illegal instruction, and not a page
902 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
903 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
904 frame->tf_trapno = T_PRIVINFLT;
914 * This is a fault on non-kernel virtual memory.
915 * vm is initialized above to NULL. If curproc is NULL
916 * or curproc->p_vmspace is NULL the fault is fatal.
927 if (frame->tf_err & PGEX_W)
928 ftype = VM_PROT_WRITE;
930 ftype = VM_PROT_READ;
932 if (map != kernel_map) {
934 * Keep swapout from messing with us during this
940 * Grow the stack if necessary
942 /* grow_stack returns false only if va falls into
943 * a growable stack region and the stack growth
944 * fails. It returns true if va was not within
945 * a growable stack region, or if the stack
948 if (!grow_stack (p, va)) {
954 /* Fault in the user page: */
955 rv = vm_fault(map, va, ftype,
956 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
962 * Don't have to worry about process locking or stacks in the kernel.
964 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
967 if (rv == KERN_SUCCESS)
971 if (mycpu->gd_intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
972 frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
975 trap_fatal(frame, eva);
979 /* kludge to pass faulting virtual address to sendsig */
982 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
986 trap_fatal(frame, eva)
987 struct trapframe *frame;
990 int code, type, ss, esp;
991 struct soft_segment_descriptor softseg;
993 code = frame->tf_err;
994 type = frame->tf_trapno;
995 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
997 if (type <= MAX_TRAP_MSG)
998 printf("\n\nFatal trap %d: %s while in %s mode\n",
999 type, trap_msg[type],
1000 frame->tf_eflags & PSL_VM ? "vm86" :
1001 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
1003 /* three seperate prints in case of a trap on an unmapped page */
1004 printf("mp_lock = %08x; ", mp_lock);
1005 printf("cpuid = %d; ", mycpu->gd_cpuid);
1006 printf("lapic.id = %08x\n", lapic.id);
1008 if (type == T_PAGEFLT) {
1009 printf("fault virtual address = 0x%x\n", eva);
1010 printf("fault code = %s %s, %s\n",
1011 code & PGEX_U ? "user" : "supervisor",
1012 code & PGEX_W ? "write" : "read",
1013 code & PGEX_P ? "protection violation" : "page not present");
1015 printf("instruction pointer = 0x%x:0x%x\n",
1016 frame->tf_cs & 0xffff, frame->tf_eip);
1017 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
1018 ss = frame->tf_ss & 0xffff;
1019 esp = frame->tf_esp;
1021 ss = GSEL(GDATA_SEL, SEL_KPL);
1022 esp = (int)&frame->tf_esp;
1024 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
1025 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
1026 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
1027 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
1028 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
1029 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
1031 printf("processor eflags = ");
1032 if (frame->tf_eflags & PSL_T)
1033 printf("trace trap, ");
1034 if (frame->tf_eflags & PSL_I)
1035 printf("interrupt enabled, ");
1036 if (frame->tf_eflags & PSL_NT)
1037 printf("nested task, ");
1038 if (frame->tf_eflags & PSL_RF)
1040 if (frame->tf_eflags & PSL_VM)
1042 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
1043 printf("current process = ");
1045 printf("%lu (%s)\n",
1046 (u_long)curproc->p_pid, curproc->p_comm ?
1047 curproc->p_comm : "");
1051 printf("current thread = pri %d ", curthread->td_pri);
1052 if (curthread->td_pri >= TDPRI_CRIT)
1055 printf("interrupt mask = ");
1056 if ((curthread->td_cpl & net_imask) == net_imask)
1058 if ((curthread->td_cpl & tty_imask) == tty_imask)
1060 if ((curthread->td_cpl & bio_imask) == bio_imask)
1062 if ((curthread->td_cpl & cam_imask) == cam_imask)
1064 if (curthread->td_cpl == 0)
1069 * we probably SHOULD have stopped the other CPUs before now!
1070 * another CPU COULD have been touching cpl at this moment...
1072 printf(" <- SMP: XXX");
1081 if ((debugger_on_panic || db_active) && kdb_trap(type, 0, frame))
1084 printf("trap number = %d\n", type);
1085 if (type <= MAX_TRAP_MSG)
1086 panic("%s", trap_msg[type]);
1088 panic("unknown/reserved trap");
1092 * Double fault handler. Called when a fault occurs while writing
1093 * a frame for a trap/exception onto the stack. This usually occurs
1094 * when the stack overflows (such is the case with infinite recursion,
1097 * XXX Note that the current PTD gets replaced by IdlePTD when the
1098 * task switch occurs. This means that the stack that was active at
1099 * the time of the double fault is not available at <kstack> unless
1100 * the machine was idle when the double fault occurred. The downside
1101 * of this is that "trace <ebp>" in ddb won't work.
1106 struct mdglobaldata *gd = mdcpu;
1108 printf("\nFatal double fault:\n");
1109 printf("eip = 0x%x\n", gd->gd_common_tss.tss_eip);
1110 printf("esp = 0x%x\n", gd->gd_common_tss.tss_esp);
1111 printf("ebp = 0x%x\n", gd->gd_common_tss.tss_ebp);
1113 /* three seperate prints in case of a trap on an unmapped page */
1114 printf("mp_lock = %08x; ", mp_lock);
1115 printf("cpuid = %d; ", mycpu->gd_cpuid);
1116 printf("lapic.id = %08x\n", lapic.id);
1118 panic("double fault");
1122 * Compensate for 386 brain damage (missing URKR).
1123 * This is a little simpler than the pagefault handler in trap() because
1124 * it the page tables have already been faulted in and high addresses
1125 * are thrown out early for other reasons.
1135 va = trunc_page((vm_offset_t)addr);
1137 * XXX - MAX is END. Changed > to >= for temp. fix.
1139 if (va >= VM_MAXUSER_ADDRESS)
1147 if (!grow_stack (p, va)) {
1153 * fault the data page
1155 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
1159 if (rv != KERN_SUCCESS)
1166 * syscall2 - MP aware system call request C handler
1168 * A system call is essentially treated as a trap except that the
1169 * MP lock is not held on entry or return. We are responsible for
1170 * obtaining the MP lock if necessary and for handling ASTs
1171 * (e.g. a task switch) prior to return.
1173 * In general, only simple access and manipulation of curproc and
1174 * the current stack is allowed without having to hold MP lock.
1177 syscall2(struct trapframe frame)
1179 struct thread *td = curthread;
1180 struct proc *p = td->td_proc;
1183 struct sysent *callp;
1184 register_t orig_tf_eflags;
1189 union sysunion args;
1192 if (ISPL(frame.tf_cs) != SEL_UPL) {
1200 KASSERT(curthread->td_mpcount == 0, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1204 * access non-atomic field from critical section. p_sticks is
1205 * updated by the clock interrupt. Also use this opportunity
1206 * to lazy-raise our LWKT priority.
1209 crit_enter_quick(td);
1210 sticks = curthread->td_sticks;
1211 crit_exit_quick(td);
1213 p->p_md.md_regs = &frame;
1214 params = (caddr_t)frame.tf_esp + sizeof(int);
1215 code = frame.tf_eax;
1216 orig_tf_eflags = frame.tf_eflags;
1218 if (p->p_sysent->sv_prepsyscall) {
1220 * The prep code is not MP aware.
1222 (*p->p_sysent->sv_prepsyscall)(&frame, (int *)(&args.nosys.usrmsg + 1), &code, ¶ms);
1225 * Need to check if this is a 32 bit or 64 bit syscall.
1226 * fuword is MP aware.
1228 if (code == SYS_syscall) {
1230 * Code is first argument, followed by actual args.
1232 code = fuword(params);
1233 params += sizeof(int);
1234 } else if (code == SYS___syscall) {
1236 * Like syscall, but code is a quad, so as to maintain
1237 * quad alignment for the rest of the arguments.
1239 code = fuword(params);
1240 params += sizeof(quad_t);
1244 if (p->p_sysent->sv_mask)
1245 code &= p->p_sysent->sv_mask;
1247 if (code >= p->p_sysent->sv_size)
1248 callp = &p->p_sysent->sv_table[0];
1250 callp = &p->p_sysent->sv_table[code];
1252 narg = callp->sy_narg & SYF_ARGMASK;
1255 * copyin is MP aware, but the tracing code is not
1257 if (params && (i = narg * sizeof(register_t)) &&
1258 (error = copyin(params, (caddr_t)(&args.nosys.usrmsg + 1), (u_int)i))) {
1260 if (KTRPOINT(td, KTR_SYSCALL))
1261 ktrsyscall(p->p_tracep, code, narg, (void *)(&args.nosys.usrmsg + 1));
1268 * Try to run the syscall without the MP lock if the syscall
1269 * is MP safe. We have to obtain the MP lock no matter what if
1272 if ((callp->sy_narg & SYF_MPSAFE) == 0) {
1279 if (KTRPOINT(td, KTR_SYSCALL)) {
1280 ktrsyscall(p->p_tracep, code, narg, (void *)(&args.nosys.usrmsg + 1));
1283 lwkt_initmsg(&args.lmsg, &td->td_msgport, code);
1284 args.sysmsg_fds[0] = 0;
1285 args.sysmsg_fds[1] = frame.tf_edx;
1287 STOPEVENT(p, S_SCE, narg); /* MP aware */
1289 error = (*callp->sy_call)(&args);
1292 * MP SAFE (we may or may not have the MP lock at this point)
1297 * Reinitialize proc pointer `p' as it may be different
1298 * if this is a child returning from fork syscall.
1301 frame.tf_eax = args.sysmsg_fds[0];
1302 frame.tf_edx = args.sysmsg_fds[1];
1303 frame.tf_eflags &= ~PSL_C;
1307 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1308 * int 0x80 is 2 bytes. We saved this in tf_err.
1310 frame.tf_eip -= frame.tf_err;
1315 panic("Unexpected EASYNC return value (for now)");
1318 if (p->p_sysent->sv_errsize) {
1319 if (error >= p->p_sysent->sv_errsize)
1320 error = -1; /* XXX */
1322 error = p->p_sysent->sv_errtbl[error];
1324 frame.tf_eax = error;
1325 frame.tf_eflags |= PSL_C;
1330 * Traced syscall. trapsignal() is not MP aware.
1332 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1333 frame.tf_eflags &= ~PSL_T;
1334 trapsignal(p, SIGTRAP, 0);
1338 * Handle reschedule and other end-of-syscall issues
1340 userret(p, &frame, sticks);
1343 if (KTRPOINT(td, KTR_SYSRET)) {
1344 ktrsysret(p->p_tracep, code, error, args.sysmsg_result);
1349 * This works because errno is findable through the
1350 * register set. If we ever support an emulation where this
1351 * is not the case, this code will need to be revisited.
1353 STOPEVENT(p, S_SCX, code);
1358 * Release the MP lock if we had to get it
1360 KASSERT(curthread->td_mpcount == 1, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1366 * sendsys2 - MP aware system message request C handler
1369 sendsys2(struct trapframe frame)
1371 struct globaldata *gd;
1372 struct thread *td = curthread;
1373 struct proc *p = td->td_proc;
1374 register_t orig_tf_eflags;
1375 struct sysent *callp;
1376 union sysunion *sysun;
1386 if (ISPL(frame.tf_cs) != SEL_UPL) {
1394 KASSERT(curthread->td_mpcount == 0, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1398 * access non-atomic field from critical section. p_sticks is
1399 * updated by the clock interrupt. Also use this opportunity
1400 * to lazy-raise our LWKT priority.
1403 crit_enter_quick(td);
1404 sticks = curthread->td_sticks;
1405 crit_exit_quick(td);
1407 p->p_md.md_regs = &frame;
1408 orig_tf_eflags = frame.tf_eflags;
1412 * Handle the waitport/waitmsg/checkport/checkmsg case
1414 * YYY MOVE THIS TO INT 0x82! We don't really need to combine it
1417 if ((msgsize = frame.tf_edx) <= 0) {
1419 printf("waitmsg/checkmsg not yet supported: %08x\n",
1425 printf("waitport/checkport only the default port is supported at the moment\n");
1432 * Wait on port for message
1434 sysun = lwkt_getport(&td->td_msgport);
1439 * Test port for message
1441 sysun = lwkt_getport(&td->td_msgport);
1449 umsg = sysun->lmsg.opaque.ms_umsg;
1450 frame.tf_eax = (register_t)umsg;
1451 if (sysun->lmsg.ms_cleanupmsg)
1452 sysun->lmsg.ms_cleanupmsg(&td->td_msgport, &sysun->lmsg);
1453 atomic_add_int_nonlocked(&td->td_msgport.mp_refs, -1);
1454 sysun->nosys.usrmsg.umsg.u.ms_fds[0] = sysun->lmsg.u.ms_fds[0];
1455 sysun->nosys.usrmsg.umsg.u.ms_fds[1] = sysun->lmsg.u.ms_fds[1];
1456 sysun->nosys.usrmsg.umsg.ms_error = sysun->lmsg.ms_error;
1457 error = sysun->lmsg.ms_error;
1458 result = sysun->lmsg.u.ms_fds[0]; /* for ktrace */
1459 if (error != 0 || code != SYS_execve) {
1461 &sysun->nosys.usrmsg.umsg.ms_copyout_start,
1462 &umsg->ms_copyout_start,
1465 crit_enter_quick(td);
1466 sysun->lmsg.opaque.ms_sysunnext = gd->gd_freesysun;
1467 gd->gd_freesysun = sysun;
1468 crit_exit_quick(td);
1479 * Extract the system call message. If msgsize is zero we are
1480 * blocking on a message and/or message port. If msgsize is -1
1481 * we are testing a message for completion or a message port for
1484 * The userland system call message size includes the size of the
1485 * userland lwkt_msg plus arguments. We load it into the userland
1486 * portion of our sysunion structure then we initialize the kerneland
1493 if (msgsize < sizeof(struct lwkt_msg) ||
1494 msgsize > sizeof(union sysunion) - sizeof(union sysmsg)
1501 * Obtain a sysun from our per-cpu cache or allocate a new one. Use
1502 * the opaque field to store the original (user) message pointer.
1503 * A critical section is necessary to interlock against interrupts
1504 * returning system messages to the thread cache.
1507 crit_enter_quick(td);
1508 if ((sysun = gd->gd_freesysun) != NULL) {
1509 gd->gd_freesysun = sysun->lmsg.opaque.ms_sysunnext;
1510 crit_exit_quick(td);
1512 crit_exit_quick(td);
1513 sysun = malloc(sizeof(union sysunion), M_SYSMSG, M_WAITOK);
1515 atomic_add_int_nonlocked(&td->td_msgport.mp_refs, 1);
1518 * Copy the user request into the kernel copy of the user request.
1520 umsg = (void *)frame.tf_ecx;
1521 error = copyin(umsg, &sysun->nosys.usrmsg, msgsize);
1524 if ((sysun->nosys.usrmsg.umsg.ms_flags & MSGF_ASYNC) &&
1525 (error = suser(td)) != 0
1531 * Initialize the kernel message from the copied-in data and
1532 * pull in appropriate flags from the userland message.
1534 lwkt_initmsg(&sysun->lmsg, &td->td_msgport,
1535 sysun->nosys.usrmsg.umsg.ms_cmd);
1536 sysun->lmsg.opaque.ms_umsg = umsg;
1537 sysun->lmsg.ms_flags |= sysun->nosys.usrmsg.umsg.ms_flags & MSGF_ASYNC;
1540 * Extract the system call number, lookup the system call, and
1541 * set the default return value.
1543 code = (u_int)sysun->lmsg.ms_cmd;
1544 if (code >= p->p_sysent->sv_size) {
1549 callp = &p->p_sysent->sv_table[code];
1551 narg = (msgsize - sizeof(struct lwkt_msg)) / sizeof(register_t);
1554 if (KTRPOINT(td, KTR_SYSCALL)) {
1555 ktrsyscall(p->p_tracep, code, narg, (void *)(&sysun->nosys.usrmsg + 1));
1558 sysun->lmsg.u.ms_fds[0] = 0;
1559 sysun->lmsg.u.ms_fds[1] = 0;
1561 STOPEVENT(p, S_SCE, narg); /* MP aware */
1564 * Make the system call. An error code is always returned, results
1565 * are copied back via ms_result32 or ms_result64. YYY temporary
1566 * stage copy p_retval[] into ms_result32/64
1568 * NOTE! XXX if this is a child returning from a fork curproc
1569 * might be different. YYY huh? a child returning from a fork
1570 * should never 'return' from this call, it should go right to the
1571 * fork_trampoline function.
1573 error = (*callp->sy_call)(sysun);
1574 gd = td->td_gd; /* RELOAD, might have switched cpus */
1578 * If a synchronous return copy p_retval to ms_result64 and return
1579 * the sysmsg to the free pool.
1581 * YYY Don't writeback message if execve() YYY
1583 if (error != EASYNC) {
1584 atomic_add_int_nonlocked(&td->td_msgport.mp_refs, -1);
1585 sysun->nosys.usrmsg.umsg.u.ms_fds[0] = sysun->lmsg.u.ms_fds[0];
1586 sysun->nosys.usrmsg.umsg.u.ms_fds[1] = sysun->lmsg.u.ms_fds[1];
1587 result = sysun->nosys.usrmsg.umsg.u.ms_fds[0]; /* for ktrace */
1588 if (error != 0 || code != SYS_execve) {
1590 error2 = copyout(&sysun->nosys.usrmsg.umsg.ms_copyout_start,
1591 &umsg->ms_copyout_start,
1596 crit_enter_quick(td);
1597 sysun->lmsg.opaque.ms_sysunnext = gd->gd_freesysun;
1598 gd->gd_freesysun = sysun;
1599 crit_exit_quick(td);
1602 frame.tf_eax = error;
1606 * Traced syscall. trapsignal() is not MP aware.
1608 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1609 frame.tf_eflags &= ~PSL_T;
1610 trapsignal(p, SIGTRAP, 0);
1614 * Handle reschedule and other end-of-syscall issues
1616 userret(p, &frame, sticks);
1619 if (KTRPOINT(td, KTR_SYSRET)) {
1620 ktrsysret(p->p_tracep, code, error, result);
1625 * This works because errno is findable through the
1626 * register set. If we ever support an emulation where this
1627 * is not the case, this code will need to be revisited.
1629 STOPEVENT(p, S_SCX, code);
1634 * Release the MP lock if we had to get it
1636 KASSERT(curthread->td_mpcount == 1, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1642 * Simplified back end of syscall(), used when returning from fork()
1643 * directly into user mode. MP lock is held on entry and should be
1644 * released on return. This code will return back into the fork
1645 * trampoline code which then runs doreti.
1648 fork_return(p, frame)
1650 struct trapframe frame;
1652 frame.tf_eax = 0; /* Child returns zero */
1653 frame.tf_eflags &= ~PSL_C; /* success */
1656 userret(p, &frame, 0);
1658 if (KTRPOINT(p->p_thread, KTR_SYSRET))
1659 ktrsysret(p->p_tracep, SYS_fork, 0, 0);
1661 p->p_flag |= P_PASSIVE_ACQ;
1663 p->p_flag &= ~P_PASSIVE_ACQ;
1665 KKASSERT(curthread->td_mpcount == 1);