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.36 2003/10/17 07:30:43 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 * passive_release is called from within a critical section and the BGL will
179 * still be held. This function is NOT called for preemptions, only for
180 * switchouts. We only actually release our P_CURPROC designation when we
181 * are going to sleep, otherwise another process might be assigned P_CURPROC
185 passive_release(struct thread *td)
187 struct proc *p = td->td_proc;
189 if ((p->p_flag & P_CP_RELEASED) == 0) {
190 p->p_flag |= P_CP_RELEASED;
191 lwkt_setpri_self(TDPRI_KERN_USER);
193 if ((p->p_flag & P_CURPROC) && (td->td_flags & TDF_RUNQ) == 0) {
194 td->td_release = NULL;
200 * userenter() passively intercepts the thread switch function to increase
201 * the thread priority from a user priority to a kernel priority, reducing
202 * syscall and trap overhead for the case where no switch occurs.
206 userenter(struct thread *curtd)
208 curtd->td_release = passive_release;
212 userexit(struct proc *p)
214 struct thread *td = p->p_thread;
217 * Reacquire our P_CURPROC status and adjust the LWKT priority
218 * for our return to userland. We can fast path the case where
219 * td_release was not called by checking particular proc flags.
220 * Otherwise we do it the slow way.
222 * Lowering our priority may make other higher priority threads
223 * runnable. lwkt_setpri_self() does not switch away, so call
224 * lwkt_maybe_switch() to deal with it.
226 td->td_release = NULL;
227 if ((p->p_flag & (P_CP_RELEASED|P_CURPROC)) == P_CURPROC) {
233 switch(p->p_rtprio.type) {
235 lwkt_setpri_self(TDPRI_USER_IDLE);
237 case RTP_PRIO_REALTIME:
239 lwkt_setpri_self(TDPRI_USER_REAL);
242 lwkt_setpri_self(TDPRI_USER_NORM);
251 userret(struct proc *p, struct trapframe *frame, u_quad_t oticks)
256 * Post any pending signals
258 while ((sig = CURSIG(p)) != 0) {
263 * If a reschedule has been requested then we release the current
264 * process in order to shift our P_CURPROC designation to another
265 * user process. userexit() will reacquire P_CURPROC and block
268 if (resched_wanted()) {
269 p->p_thread->td_release = NULL;
270 if ((p->p_flag & P_CP_RELEASED) == 0) {
271 p->p_flag |= P_CP_RELEASED;
272 lwkt_setpri_self(TDPRI_KERN_USER);
274 if (p->p_flag & P_CURPROC) {
282 * Charge system time if profiling. Note: times are in microseconds.
284 if (p->p_flag & P_PROFIL) {
285 addupc_task(p, frame->tf_eip,
286 (u_int)(curthread->td_sticks - oticks));
290 * Post any pending signals XXX
292 while ((sig = CURSIG(p)) != 0)
296 #ifdef DEVICE_POLLING
297 extern u_int32_t poll_in_trap;
298 extern int ether_poll (int count);
299 #endif /* DEVICE_POLLING */
302 * Exception, fault, and trap interface to the FreeBSD kernel.
303 * This common code is called from assembly language IDT gate entry
304 * routines that prepare a suitable stack frame, and restore this
305 * frame after the exception has been processed.
307 * This function is also called from doreti in an interlock to handle ASTs.
308 * For example: hardwareint->INTROUTINE->(set ast)->doreti->trap
310 * NOTE! We have to retrieve the fault address prior to obtaining the
311 * MP lock because get_mplock() may switch out. YYY cr2 really ought
312 * to be retrieved by the assembly code, not here.
316 struct trapframe frame;
318 struct thread *td = curthread;
321 int i = 0, ucode = 0, type, code;
327 eva = (frame.tf_trapno == T_PAGEFLT ? rcr2() : 0);
329 trap_fatal(&frame, eva);
335 if (frame.tf_trapno == T_PAGEFLT) {
337 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
338 * This problem is worked around by using an interrupt
339 * gate for the pagefault handler. We are finally ready
340 * to read %cr2 and then must reenable interrupts.
342 * XXX this should be in the switch statement, but the
343 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
344 * flow of control too much for this to be obviously
354 * MP lock is held at this point
357 if (!(frame.tf_eflags & PSL_I)) {
359 * Buggy application or kernel code has disabled interrupts
360 * and then trapped. Enabling interrupts now is wrong, but
361 * it is better than running with interrupts disabled until
362 * they are accidentally enabled later.
364 type = frame.tf_trapno;
365 if (ISPL(frame.tf_cs)==SEL_UPL || (frame.tf_eflags & PSL_VM)) {
367 "pid %ld (%s): trap %d with interrupts disabled\n",
368 (long)curproc->p_pid, curproc->p_comm, type);
369 } else if (type != T_BPTFLT && type != T_TRCTRAP) {
371 * XXX not quite right, since this may be for a
372 * multiple fault in user mode.
374 printf("kernel trap %d with interrupts disabled\n",
381 #ifdef DEVICE_POLLING
383 ether_poll(poll_in_trap);
384 #endif /* DEVICE_POLLING */
386 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
389 type = frame.tf_trapno;
393 if (frame.tf_eflags & PSL_VM &&
394 (type == T_PROTFLT || type == T_STKFLT)) {
396 KKASSERT(curthread->td_mpcount > 0);
398 i = vm86_emulate((struct vm86frame *)&frame);
400 KKASSERT(curthread->td_mpcount > 0);
404 * returns to original process
406 vm86_trap((struct vm86frame *)&frame);
413 * these traps want either a process context, or
414 * assume a normal userspace trap.
418 trap_fatal(&frame, eva);
421 type = T_BPTFLT; /* kernel breakpoint */
424 goto kernel_trap; /* normal kernel trap handling */
427 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
432 sticks = curthread->td_sticks;
433 p->p_md.md_regs = &frame;
436 case T_PRIVINFLT: /* privileged instruction fault */
441 case T_BPTFLT: /* bpt instruction fault */
442 case T_TRCTRAP: /* trace trap */
443 frame.tf_eflags &= ~PSL_T;
447 case T_ARITHTRAP: /* arithmetic trap */
452 case T_ASTFLT: /* Allow process switch */
453 mycpu->gd_cnt.v_soft++;
454 if (mycpu->gd_reqflags & RQF_AST_OWEUPC) {
455 atomic_clear_int_nonlocked(&mycpu->gd_reqflags,
457 addupc_task(p, p->p_stats->p_prof.pr_addr,
458 p->p_stats->p_prof.pr_ticks);
463 * The following two traps can happen in
464 * vm86 mode, and, if so, we want to handle
467 case T_PROTFLT: /* general protection fault */
468 case T_STKFLT: /* stack fault */
469 if (frame.tf_eflags & PSL_VM) {
470 i = vm86_emulate((struct vm86frame *)&frame);
477 case T_SEGNPFLT: /* segment not present fault */
478 case T_TSSFLT: /* invalid TSS fault */
479 case T_DOUBLEFLT: /* double fault */
481 ucode = code + BUS_SEGM_FAULT ;
485 case T_PAGEFLT: /* page fault */
486 i = trap_pfault(&frame, TRUE, eva);
489 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
499 case T_DIVIDE: /* integer divide fault */
507 goto handle_powerfail;
508 #else /* !POWERFAIL_NMI */
509 /* machine/parity/power fail/"kitchen sink" faults */
510 if (isa_nmi(code) == 0) {
513 * NMI can be hooked up to a pushbutton
517 printf ("NMI ... going to debugger\n");
518 kdb_trap (type, 0, &frame);
522 } else if (panic_on_nmi)
523 panic("NMI indicates hardware failure");
525 #endif /* POWERFAIL_NMI */
526 #endif /* NISA > 0 */
528 case T_OFLOW: /* integer overflow fault */
533 case T_BOUND: /* bounds check fault */
540 /* if a transparent fault (due to context switch "late") */
544 if (!pmath_emulate) {
546 ucode = FPE_FPU_NP_TRAP;
549 i = (*pmath_emulate)(&frame);
551 if (!(frame.tf_eflags & PSL_T))
553 frame.tf_eflags &= ~PSL_T;
556 /* else ucode = emulator_only_knows() XXX */
559 case T_FPOPFLT: /* FPU operand fetch fault */
564 case T_XMMFLT: /* SIMD floating-point exception */
574 case T_PAGEFLT: /* page fault */
575 (void) trap_pfault(&frame, FALSE, eva);
581 * The kernel is apparently using npx for copying.
582 * XXX this should be fatal unless the kernel has
583 * registered such use.
590 case T_PROTFLT: /* general protection fault */
591 case T_SEGNPFLT: /* segment not present fault */
593 * Invalid segment selectors and out of bounds
594 * %eip's and %esp's can be set up in user mode.
595 * This causes a fault in kernel mode when the
596 * kernel tries to return to user mode. We want
597 * to get this fault so that we can fix the
598 * problem here and not have to check all the
599 * selectors and pointers when the user changes
602 #define MAYBE_DORETI_FAULT(where, whereto) \
604 if (frame.tf_eip == (int)where) { \
605 frame.tf_eip = (int)whereto; \
609 if (mycpu->gd_intr_nesting_level == 0) {
611 * Invalid %fs's and %gs's can be created using
612 * procfs or PT_SETREGS or by invalidating the
613 * underlying LDT entry. This causes a fault
614 * in kernel mode when the kernel attempts to
615 * switch contexts. Lose the bad context
616 * (XXX) so that we can continue, and generate
619 if (frame.tf_eip == (int)cpu_switch_load_gs) {
620 curthread->td_pcb->pcb_gs = 0;
624 MAYBE_DORETI_FAULT(doreti_iret,
626 MAYBE_DORETI_FAULT(doreti_popl_ds,
627 doreti_popl_ds_fault);
628 MAYBE_DORETI_FAULT(doreti_popl_es,
629 doreti_popl_es_fault);
630 MAYBE_DORETI_FAULT(doreti_popl_fs,
631 doreti_popl_fs_fault);
632 if (curthread->td_pcb->pcb_onfault) {
633 frame.tf_eip = (int)curthread->td_pcb->pcb_onfault;
641 * PSL_NT can be set in user mode and isn't cleared
642 * automatically when the kernel is entered. This
643 * causes a TSS fault when the kernel attempts to
644 * `iret' because the TSS link is uninitialized. We
645 * want to get this fault so that we can fix the
646 * problem here and not every time the kernel is
649 if (frame.tf_eflags & PSL_NT) {
650 frame.tf_eflags &= ~PSL_NT;
655 case T_TRCTRAP: /* trace trap */
656 if (frame.tf_eip == (int)IDTVEC(syscall)) {
658 * We've just entered system mode via the
659 * syscall lcall. Continue single stepping
660 * silently until the syscall handler has
665 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
667 * The syscall handler has now saved the
668 * flags. Stop single stepping it.
670 frame.tf_eflags &= ~PSL_T;
674 * Ignore debug register trace traps due to
675 * accesses in the user's address space, which
676 * can happen under several conditions such as
677 * if a user sets a watchpoint on a buffer and
678 * then passes that buffer to a system call.
679 * We still want to get TRCTRAPS for addresses
680 * in kernel space because that is useful when
681 * debugging the kernel.
683 if (user_dbreg_trap()) {
685 * Reset breakpoint bits because the
688 load_dr6(rdr6() & 0xfffffff0);
692 * Fall through (TRCTRAP kernel mode, kernel address)
696 * If DDB is enabled, let it handle the debugger trap.
697 * Otherwise, debugger traps "can't happen".
700 if (kdb_trap (type, 0, &frame))
709 # define TIMER_FREQ 1193182
713 static unsigned lastalert = 0;
715 if(time_second - lastalert > 10)
717 log(LOG_WARNING, "NMI: power fail\n");
718 sysbeep(TIMER_FREQ/880, hz);
719 lastalert = time_second;
724 #else /* !POWERFAIL_NMI */
725 /* machine/parity/power fail/"kitchen sink" faults */
726 if (isa_nmi(code) == 0) {
729 * NMI can be hooked up to a pushbutton
733 printf ("NMI ... going to debugger\n");
734 kdb_trap (type, 0, &frame);
738 } else if (panic_on_nmi == 0)
741 #endif /* POWERFAIL_NMI */
742 #endif /* NISA > 0 */
745 trap_fatal(&frame, eva);
749 /* Translate fault for emulators (e.g. Linux) */
750 if (*p->p_sysent->sv_transtrap)
751 i = (*p->p_sysent->sv_transtrap)(i, type);
753 trapsignal(p, i, ucode);
756 if (type <= MAX_TRAP_MSG) {
757 uprintf("fatal process exception: %s",
759 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
760 uprintf(", fault VA = 0x%lx", (u_long)eva);
767 if (ISPL(frame.tf_cs) == SEL_UPL)
768 KASSERT(curthread->td_mpcount == 1, ("badmpcount trap from %p", (void *)frame.tf_eip));
770 userret(p, &frame, sticks);
774 KKASSERT(curthread->td_mpcount > 0);
781 * This version doesn't allow a page fault to user space while
782 * in the kernel. The rest of the kernel needs to be made "safe"
783 * before this can be used. I think the only things remaining
784 * to be made safe are the iBCS2 code and the process tracing/
788 trap_pfault(frame, usermode, eva)
789 struct trapframe *frame;
794 struct vmspace *vm = NULL;
798 struct proc *p = curproc;
800 if (frame->tf_err & PGEX_W)
801 ftype = VM_PROT_WRITE;
803 ftype = VM_PROT_READ;
805 va = trunc_page(eva);
806 if (va < VM_MIN_KERNEL_ADDRESS) {
811 (!usermode && va < VM_MAXUSER_ADDRESS &&
812 (mycpu->gd_intr_nesting_level != 0 ||
813 curthread->td_pcb->pcb_onfault == NULL))) {
814 trap_fatal(frame, eva);
819 * This is a fault on non-kernel virtual memory.
820 * vm is initialized above to NULL. If curproc is NULL
821 * or curproc->p_vmspace is NULL the fault is fatal.
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 allow user-mode faults in kernel address space.
864 * Since we know that kernel virtual address addresses
865 * always have pte pages mapped, we just have to fault
868 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
871 if (rv == KERN_SUCCESS)
875 if (mycpu->gd_intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
876 frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
879 trap_fatal(frame, eva);
883 /* kludge to pass faulting virtual address to sendsig */
886 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
891 trap_pfault(frame, usermode, eva)
892 struct trapframe *frame;
897 struct vmspace *vm = NULL;
901 struct proc *p = curproc;
903 va = trunc_page(eva);
904 if (va >= KERNBASE) {
906 * Don't allow user-mode faults in kernel address space.
907 * An exception: if the faulting address is the invalid
908 * instruction entry in the IDT, then the Intel Pentium
909 * F00F bug workaround was triggered, and we need to
910 * treat it is as an illegal instruction, and not a page
913 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
914 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
915 frame->tf_trapno = T_PRIVINFLT;
925 * This is a fault on non-kernel virtual memory.
926 * vm is initialized above to NULL. If curproc is NULL
927 * or curproc->p_vmspace is NULL the fault is fatal.
938 if (frame->tf_err & PGEX_W)
939 ftype = VM_PROT_WRITE;
941 ftype = VM_PROT_READ;
943 if (map != kernel_map) {
945 * Keep swapout from messing with us during this
951 * Grow the stack if necessary
953 /* grow_stack returns false only if va falls into
954 * a growable stack region and the stack growth
955 * fails. It returns true if va was not within
956 * a growable stack region, or if the stack
959 if (!grow_stack (p, va)) {
965 /* Fault in the user page: */
966 rv = vm_fault(map, va, ftype,
967 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
973 * Don't have to worry about process locking or stacks in the kernel.
975 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
978 if (rv == KERN_SUCCESS)
982 if (mycpu->gd_intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
983 frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
986 trap_fatal(frame, eva);
990 /* kludge to pass faulting virtual address to sendsig */
993 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
997 trap_fatal(frame, eva)
998 struct trapframe *frame;
1001 int code, type, ss, esp;
1002 struct soft_segment_descriptor softseg;
1004 code = frame->tf_err;
1005 type = frame->tf_trapno;
1006 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
1008 if (type <= MAX_TRAP_MSG)
1009 printf("\n\nFatal trap %d: %s while in %s mode\n",
1010 type, trap_msg[type],
1011 frame->tf_eflags & PSL_VM ? "vm86" :
1012 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
1014 /* three seperate prints in case of a trap on an unmapped page */
1015 printf("mp_lock = %08x; ", mp_lock);
1016 printf("cpuid = %d; ", mycpu->gd_cpuid);
1017 printf("lapic.id = %08x\n", lapic.id);
1019 if (type == T_PAGEFLT) {
1020 printf("fault virtual address = 0x%x\n", eva);
1021 printf("fault code = %s %s, %s\n",
1022 code & PGEX_U ? "user" : "supervisor",
1023 code & PGEX_W ? "write" : "read",
1024 code & PGEX_P ? "protection violation" : "page not present");
1026 printf("instruction pointer = 0x%x:0x%x\n",
1027 frame->tf_cs & 0xffff, frame->tf_eip);
1028 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
1029 ss = frame->tf_ss & 0xffff;
1030 esp = frame->tf_esp;
1032 ss = GSEL(GDATA_SEL, SEL_KPL);
1033 esp = (int)&frame->tf_esp;
1035 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
1036 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
1037 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
1038 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
1039 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
1040 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
1042 printf("processor eflags = ");
1043 if (frame->tf_eflags & PSL_T)
1044 printf("trace trap, ");
1045 if (frame->tf_eflags & PSL_I)
1046 printf("interrupt enabled, ");
1047 if (frame->tf_eflags & PSL_NT)
1048 printf("nested task, ");
1049 if (frame->tf_eflags & PSL_RF)
1051 if (frame->tf_eflags & PSL_VM)
1053 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
1054 printf("current process = ");
1056 printf("%lu (%s)\n",
1057 (u_long)curproc->p_pid, curproc->p_comm ?
1058 curproc->p_comm : "");
1062 printf("current thread = pri %d ", curthread->td_pri);
1063 if (curthread->td_pri >= TDPRI_CRIT)
1066 printf("interrupt mask = ");
1067 if ((curthread->td_cpl & net_imask) == net_imask)
1069 if ((curthread->td_cpl & tty_imask) == tty_imask)
1071 if ((curthread->td_cpl & bio_imask) == bio_imask)
1073 if ((curthread->td_cpl & cam_imask) == cam_imask)
1075 if (curthread->td_cpl == 0)
1080 * we probably SHOULD have stopped the other CPUs before now!
1081 * another CPU COULD have been touching cpl at this moment...
1083 printf(" <- SMP: XXX");
1092 if ((debugger_on_panic || db_active) && kdb_trap(type, 0, frame))
1095 printf("trap number = %d\n", type);
1096 if (type <= MAX_TRAP_MSG)
1097 panic("%s", trap_msg[type]);
1099 panic("unknown/reserved trap");
1103 * Double fault handler. Called when a fault occurs while writing
1104 * a frame for a trap/exception onto the stack. This usually occurs
1105 * when the stack overflows (such is the case with infinite recursion,
1108 * XXX Note that the current PTD gets replaced by IdlePTD when the
1109 * task switch occurs. This means that the stack that was active at
1110 * the time of the double fault is not available at <kstack> unless
1111 * the machine was idle when the double fault occurred. The downside
1112 * of this is that "trace <ebp>" in ddb won't work.
1117 struct mdglobaldata *gd = mdcpu;
1119 printf("\nFatal double fault:\n");
1120 printf("eip = 0x%x\n", gd->gd_common_tss.tss_eip);
1121 printf("esp = 0x%x\n", gd->gd_common_tss.tss_esp);
1122 printf("ebp = 0x%x\n", gd->gd_common_tss.tss_ebp);
1124 /* three seperate prints in case of a trap on an unmapped page */
1125 printf("mp_lock = %08x; ", mp_lock);
1126 printf("cpuid = %d; ", mycpu->gd_cpuid);
1127 printf("lapic.id = %08x\n", lapic.id);
1129 panic("double fault");
1133 * Compensate for 386 brain damage (missing URKR).
1134 * This is a little simpler than the pagefault handler in trap() because
1135 * it the page tables have already been faulted in and high addresses
1136 * are thrown out early for other reasons.
1146 va = trunc_page((vm_offset_t)addr);
1148 * XXX - MAX is END. Changed > to >= for temp. fix.
1150 if (va >= VM_MAXUSER_ADDRESS)
1158 if (!grow_stack (p, va)) {
1164 * fault the data page
1166 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
1170 if (rv != KERN_SUCCESS)
1177 * syscall2 - MP aware system call request C handler
1179 * A system call is essentially treated as a trap except that the
1180 * MP lock is not held on entry or return. We are responsible for
1181 * obtaining the MP lock if necessary and for handling ASTs
1182 * (e.g. a task switch) prior to return.
1184 * In general, only simple access and manipulation of curproc and
1185 * the current stack is allowed without having to hold MP lock.
1188 syscall2(struct trapframe frame)
1190 struct thread *td = curthread;
1191 struct proc *p = td->td_proc;
1194 struct sysent *callp;
1195 register_t orig_tf_eflags;
1200 union sysunion args;
1203 if (ISPL(frame.tf_cs) != SEL_UPL) {
1211 KASSERT(curthread->td_mpcount == 0, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1215 * access non-atomic field from critical section. p_sticks is
1216 * updated by the clock interrupt. Also use this opportunity
1217 * to lazy-raise our LWKT priority.
1220 crit_enter_quick(td);
1221 sticks = curthread->td_sticks;
1222 crit_exit_quick(td);
1224 p->p_md.md_regs = &frame;
1225 params = (caddr_t)frame.tf_esp + sizeof(int);
1226 code = frame.tf_eax;
1227 orig_tf_eflags = frame.tf_eflags;
1229 if (p->p_sysent->sv_prepsyscall) {
1231 * The prep code is not MP aware.
1233 (*p->p_sysent->sv_prepsyscall)(&frame, (int *)(&args.nosys.usrmsg + 1), &code, ¶ms);
1236 * Need to check if this is a 32 bit or 64 bit syscall.
1237 * fuword is MP aware.
1239 if (code == SYS_syscall) {
1241 * Code is first argument, followed by actual args.
1243 code = fuword(params);
1244 params += sizeof(int);
1245 } else if (code == SYS___syscall) {
1247 * Like syscall, but code is a quad, so as to maintain
1248 * quad alignment for the rest of the arguments.
1250 code = fuword(params);
1251 params += sizeof(quad_t);
1255 if (p->p_sysent->sv_mask)
1256 code &= p->p_sysent->sv_mask;
1258 if (code >= p->p_sysent->sv_size)
1259 callp = &p->p_sysent->sv_table[0];
1261 callp = &p->p_sysent->sv_table[code];
1263 narg = callp->sy_narg & SYF_ARGMASK;
1266 * copyin is MP aware, but the tracing code is not
1268 if (params && (i = narg * sizeof(register_t)) &&
1269 (error = copyin(params, (caddr_t)(&args.nosys.usrmsg + 1), (u_int)i))) {
1271 if (KTRPOINT(td, KTR_SYSCALL))
1272 ktrsyscall(p->p_tracep, code, narg, (void *)(&args.nosys.usrmsg + 1));
1279 * Try to run the syscall without the MP lock if the syscall
1280 * is MP safe. We have to obtain the MP lock no matter what if
1283 if ((callp->sy_narg & SYF_MPSAFE) == 0) {
1290 if (KTRPOINT(td, KTR_SYSCALL)) {
1291 ktrsyscall(p->p_tracep, code, narg, (void *)(&args.nosys.usrmsg + 1));
1294 lwkt_initmsg(&args.lmsg, &td->td_msgport, code);
1295 args.sysmsg_fds[0] = 0;
1296 args.sysmsg_fds[1] = frame.tf_edx;
1298 STOPEVENT(p, S_SCE, narg); /* MP aware */
1300 error = (*callp->sy_call)(&args);
1303 * MP SAFE (we may or may not have the MP lock at this point)
1308 * Reinitialize proc pointer `p' as it may be different
1309 * if this is a child returning from fork syscall.
1312 frame.tf_eax = args.sysmsg_fds[0];
1313 frame.tf_edx = args.sysmsg_fds[1];
1314 frame.tf_eflags &= ~PSL_C;
1318 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1319 * int 0x80 is 2 bytes. We saved this in tf_err.
1321 frame.tf_eip -= frame.tf_err;
1326 panic("Unexpected EASYNC return value (for now)");
1329 if (p->p_sysent->sv_errsize) {
1330 if (error >= p->p_sysent->sv_errsize)
1331 error = -1; /* XXX */
1333 error = p->p_sysent->sv_errtbl[error];
1335 frame.tf_eax = error;
1336 frame.tf_eflags |= PSL_C;
1341 * Traced syscall. trapsignal() is not MP aware.
1343 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1344 frame.tf_eflags &= ~PSL_T;
1345 trapsignal(p, SIGTRAP, 0);
1349 * Handle reschedule and other end-of-syscall issues
1351 userret(p, &frame, sticks);
1354 if (KTRPOINT(td, KTR_SYSRET)) {
1355 ktrsysret(p->p_tracep, code, error, args.sysmsg_result);
1360 * This works because errno is findable through the
1361 * register set. If we ever support an emulation where this
1362 * is not the case, this code will need to be revisited.
1364 STOPEVENT(p, S_SCX, code);
1369 * Release the MP lock if we had to get it
1371 KASSERT(curthread->td_mpcount == 1, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1377 * sendsys2 - MP aware system message request C handler
1380 sendsys2(struct trapframe frame)
1382 struct globaldata *gd;
1383 struct thread *td = curthread;
1384 struct proc *p = td->td_proc;
1385 register_t orig_tf_eflags;
1386 struct sysent *callp;
1387 union sysunion *sysun;
1397 if (ISPL(frame.tf_cs) != SEL_UPL) {
1405 KASSERT(curthread->td_mpcount == 0, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1409 * access non-atomic field from critical section. p_sticks is
1410 * updated by the clock interrupt. Also use this opportunity
1411 * to lazy-raise our LWKT priority.
1414 crit_enter_quick(td);
1415 sticks = curthread->td_sticks;
1416 crit_exit_quick(td);
1418 p->p_md.md_regs = &frame;
1419 orig_tf_eflags = frame.tf_eflags;
1423 * Handle the waitport/waitmsg/checkport/checkmsg case
1425 * YYY MOVE THIS TO INT 0x82! We don't really need to combine it
1428 if ((msgsize = frame.tf_edx) <= 0) {
1430 printf("waitmsg/checkmsg not yet supported: %08x\n",
1436 printf("waitport/checkport only the default port is supported at the moment\n");
1443 * Wait on port for message
1445 sysun = lwkt_getport(&td->td_msgport);
1450 * Test port for message
1452 sysun = lwkt_getport(&td->td_msgport);
1460 umsg = sysun->lmsg.opaque.ms_umsg;
1461 frame.tf_eax = (register_t)umsg;
1462 if (sysun->lmsg.ms_cleanupmsg)
1463 sysun->lmsg.ms_cleanupmsg(&td->td_msgport, &sysun->lmsg);
1464 atomic_add_int_nonlocked(&td->td_msgport.mp_refs, -1);
1465 sysun->nosys.usrmsg.umsg.u.ms_fds[0] = sysun->lmsg.u.ms_fds[0];
1466 sysun->nosys.usrmsg.umsg.u.ms_fds[1] = sysun->lmsg.u.ms_fds[1];
1467 sysun->nosys.usrmsg.umsg.ms_error = sysun->lmsg.ms_error;
1468 error = sysun->lmsg.ms_error;
1469 result = sysun->lmsg.u.ms_fds[0]; /* for ktrace */
1470 if (error != 0 || code != SYS_execve) {
1472 &sysun->nosys.usrmsg.umsg.ms_copyout_start,
1473 &umsg->ms_copyout_start,
1476 crit_enter_quick(td);
1477 sysun->lmsg.opaque.ms_sysunnext = gd->gd_freesysun;
1478 gd->gd_freesysun = sysun;
1479 crit_exit_quick(td);
1490 * Extract the system call message. If msgsize is zero we are
1491 * blocking on a message and/or message port. If msgsize is -1
1492 * we are testing a message for completion or a message port for
1495 * The userland system call message size includes the size of the
1496 * userland lwkt_msg plus arguments. We load it into the userland
1497 * portion of our sysunion structure then we initialize the kerneland
1504 if (msgsize < sizeof(struct lwkt_msg) ||
1505 msgsize > sizeof(union sysunion) - sizeof(union sysmsg)
1512 * Obtain a sysun from our per-cpu cache or allocate a new one. Use
1513 * the opaque field to store the original (user) message pointer.
1514 * A critical section is necessary to interlock against interrupts
1515 * returning system messages to the thread cache.
1518 crit_enter_quick(td);
1519 if ((sysun = gd->gd_freesysun) != NULL) {
1520 gd->gd_freesysun = sysun->lmsg.opaque.ms_sysunnext;
1521 crit_exit_quick(td);
1523 crit_exit_quick(td);
1524 sysun = malloc(sizeof(union sysunion), M_SYSMSG, M_WAITOK);
1526 atomic_add_int_nonlocked(&td->td_msgport.mp_refs, 1);
1529 * Copy the user request into the kernel copy of the user request.
1531 umsg = (void *)frame.tf_ecx;
1532 error = copyin(umsg, &sysun->nosys.usrmsg, msgsize);
1535 if ((sysun->nosys.usrmsg.umsg.ms_flags & MSGF_ASYNC) &&
1536 (error = suser(td)) != 0
1542 * Initialize the kernel message from the copied-in data and
1543 * pull in appropriate flags from the userland message.
1545 lwkt_initmsg(&sysun->lmsg, &td->td_msgport,
1546 sysun->nosys.usrmsg.umsg.ms_cmd);
1547 sysun->lmsg.opaque.ms_umsg = umsg;
1548 sysun->lmsg.ms_flags |= sysun->nosys.usrmsg.umsg.ms_flags & MSGF_ASYNC;
1551 * Extract the system call number, lookup the system call, and
1552 * set the default return value.
1554 code = (u_int)sysun->lmsg.ms_cmd;
1555 if (code >= p->p_sysent->sv_size) {
1560 callp = &p->p_sysent->sv_table[code];
1562 narg = (msgsize - sizeof(struct lwkt_msg)) / sizeof(register_t);
1565 if (KTRPOINT(td, KTR_SYSCALL)) {
1566 ktrsyscall(p->p_tracep, code, narg, (void *)(&sysun->nosys.usrmsg + 1));
1569 sysun->lmsg.u.ms_fds[0] = 0;
1570 sysun->lmsg.u.ms_fds[1] = 0;
1572 STOPEVENT(p, S_SCE, narg); /* MP aware */
1575 * Make the system call. An error code is always returned, results
1576 * are copied back via ms_result32 or ms_result64. YYY temporary
1577 * stage copy p_retval[] into ms_result32/64
1579 * NOTE! XXX if this is a child returning from a fork curproc
1580 * might be different. YYY huh? a child returning from a fork
1581 * should never 'return' from this call, it should go right to the
1582 * fork_trampoline function.
1584 error = (*callp->sy_call)(sysun);
1585 gd = td->td_gd; /* RELOAD, might have switched cpus */
1589 * If a synchronous return copy p_retval to ms_result64 and return
1590 * the sysmsg to the free pool.
1592 * YYY Don't writeback message if execve() YYY
1594 if (error != EASYNC) {
1595 atomic_add_int_nonlocked(&td->td_msgport.mp_refs, -1);
1596 sysun->nosys.usrmsg.umsg.u.ms_fds[0] = sysun->lmsg.u.ms_fds[0];
1597 sysun->nosys.usrmsg.umsg.u.ms_fds[1] = sysun->lmsg.u.ms_fds[1];
1598 result = sysun->nosys.usrmsg.umsg.u.ms_fds[0]; /* for ktrace */
1599 if (error != 0 || code != SYS_execve) {
1601 error2 = copyout(&sysun->nosys.usrmsg.umsg.ms_copyout_start,
1602 &umsg->ms_copyout_start,
1607 crit_enter_quick(td);
1608 sysun->lmsg.opaque.ms_sysunnext = gd->gd_freesysun;
1609 gd->gd_freesysun = sysun;
1610 crit_exit_quick(td);
1613 frame.tf_eax = error;
1617 * Traced syscall. trapsignal() is not MP aware.
1619 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1620 frame.tf_eflags &= ~PSL_T;
1621 trapsignal(p, SIGTRAP, 0);
1625 * Handle reschedule and other end-of-syscall issues
1627 userret(p, &frame, sticks);
1630 if (KTRPOINT(td, KTR_SYSRET)) {
1631 ktrsysret(p->p_tracep, code, error, result);
1636 * This works because errno is findable through the
1637 * register set. If we ever support an emulation where this
1638 * is not the case, this code will need to be revisited.
1640 STOPEVENT(p, S_SCX, code);
1645 * Release the MP lock if we had to get it
1647 KASSERT(curthread->td_mpcount == 1, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1653 * Simplified back end of syscall(), used when returning from fork()
1654 * directly into user mode. MP lock is held on entry and should be
1655 * released on return. This code will return back into the fork
1656 * trampoline code which then runs doreti.
1659 fork_return(p, frame)
1661 struct trapframe frame;
1663 frame.tf_eax = 0; /* Child returns zero */
1664 frame.tf_eflags &= ~PSL_C; /* success */
1667 userret(p, &frame, 0);
1669 if (KTRPOINT(p->p_thread, KTR_SYSRET))
1670 ktrsysret(p->p_tracep, SYS_fork, 0, 0);
1672 p->p_flag |= P_PASSIVE_ACQ;
1674 p->p_flag &= ~P_PASSIVE_ACQ;
1676 KKASSERT(curthread->td_mpcount == 1);