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 * x86_64 Trap and System call handling
48 #include "opt_ktrace.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
53 #include <sys/pioctl.h>
54 #include <sys/kernel.h>
55 #include <sys/resourcevar.h>
56 #include <sys/signalvar.h>
57 #include <sys/signal2.h>
58 #include <sys/syscall.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
62 #include <sys/vmmeter.h>
63 #include <sys/malloc.h>
65 #include <sys/ktrace.h>
68 #include <sys/vkernel.h>
69 #include <sys/sysproto.h>
70 #include <sys/sysunion.h>
71 #include <sys/vmspace.h>
74 #include <vm/vm_param.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_extern.h>
82 #include <machine/cpu.h>
83 #include <machine/md_var.h>
84 #include <machine/pcb.h>
85 #include <machine/smp.h>
86 #include <machine/tss.h>
87 #include <machine/globaldata.h>
91 #include <sys/msgport2.h>
92 #include <sys/thread2.h>
93 #include <sys/mplock2.h>
95 int (*pmath_emulate) (struct trapframe *);
97 static int trap_pfault (struct trapframe *, int, vm_offset_t);
98 static void trap_fatal (struct trapframe *, int, vm_offset_t);
99 void dblfault_handler (void);
100 extern int vmm_enabled;
103 extern inthand_t IDTVEC(syscall);
106 #define MAX_TRAP_MSG 30
107 static char *trap_msg[] = {
109 "privileged instruction fault", /* 1 T_PRIVINFLT */
111 "breakpoint instruction fault", /* 3 T_BPTFLT */
114 "arithmetic trap", /* 6 T_ARITHTRAP */
115 "system forced exception", /* 7 T_ASTFLT */
117 "general protection fault", /* 9 T_PROTFLT */
118 "trace trap", /* 10 T_TRCTRAP */
120 "page fault", /* 12 T_PAGEFLT */
122 "alignment fault", /* 14 T_ALIGNFLT */
126 "integer divide fault", /* 18 T_DIVIDE */
127 "non-maskable interrupt trap", /* 19 T_NMI */
128 "overflow trap", /* 20 T_OFLOW */
129 "FPU bounds check fault", /* 21 T_BOUND */
130 "FPU device not available", /* 22 T_DNA */
131 "double fault", /* 23 T_DOUBLEFLT */
132 "FPU operand fetch fault", /* 24 T_FPOPFLT */
133 "invalid TSS fault", /* 25 T_TSSFLT */
134 "segment not present fault", /* 26 T_SEGNPFLT */
135 "stack fault", /* 27 T_STKFLT */
136 "machine check trap", /* 28 T_MCHK */
137 "SIMD floating-point exception", /* 29 T_XMMFLT */
138 "reserved (unknown) fault", /* 30 T_RESERVED */
142 static int ddb_on_nmi = 1;
143 SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
144 &ddb_on_nmi, 0, "Go to DDB on NMI");
146 static int panic_on_nmi = 1;
147 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
148 &panic_on_nmi, 0, "Panic on NMI");
149 static int fast_release;
150 SYSCTL_INT(_machdep, OID_AUTO, fast_release, CTLFLAG_RW,
151 &fast_release, 0, "Passive Release was optimal");
152 static int slow_release;
153 SYSCTL_INT(_machdep, OID_AUTO, slow_release, CTLFLAG_RW,
154 &slow_release, 0, "Passive Release was nonoptimal");
157 * Passively intercepts the thread switch function to increase
158 * the thread priority from a user priority to a kernel priority, reducing
159 * syscall and trap overhead for the case where no switch occurs.
161 * Synchronizes td_ucred with p_ucred. This is used by system calls,
162 * signal handling, faults, AST traps, and anything else that enters the
163 * kernel from userland and provides the kernel with a stable read-only
164 * copy of the process ucred.
167 userenter(struct thread *curtd, struct proc *curp)
172 curtd->td_release = lwkt_passive_release;
174 if (curtd->td_ucred != curp->p_ucred) {
175 ncred = crhold(curp->p_ucred);
176 ocred = curtd->td_ucred;
177 curtd->td_ucred = ncred;
184 * Handle signals, profiling, and other AST's and/or tasks that
185 * must be completed before we can return to or try to return to userland.
187 * Note that td_sticks is a 64 bit quantity, but there's no point doing 64
188 * arithmatic on the delta calculation so the absolute tick values are
189 * truncated to an integer.
192 userret(struct lwp *lp, struct trapframe *frame, int sticks)
194 struct proc *p = lp->lwp_proc;
199 * Charge system time if profiling. Note: times are in microseconds.
200 * This may do a copyout and block, so do it first even though it
201 * means some system time will be charged as user time.
203 if (p->p_flags & P_PROFIL) {
204 addupc_task(p, frame->tf_rip,
205 (u_int)((int)lp->lwp_thread->td_sticks - sticks));
210 * Specific on-return-to-usermode checks (LWP_MP_WEXIT,
211 * LWP_MP_VNLRU, etc).
213 if (lp->lwp_mpflags & LWP_MP_URETMASK)
217 * Block here if we are in a stopped state.
219 if (STOPLWP(p, lp)) {
220 lwkt_gettoken(&p->p_token);
222 lwkt_reltoken(&p->p_token);
227 * Post any pending upcalls. If running a virtual kernel be sure
228 * to restore the virtual kernel's vmspace before posting the upcall.
230 if (p->p_flags & (P_SIGVTALRM | P_SIGPROF)) {
231 lwkt_gettoken(&p->p_token);
232 if (p->p_flags & P_SIGVTALRM) {
233 p->p_flags &= ~P_SIGVTALRM;
234 ksignal(p, SIGVTALRM);
236 if (p->p_flags & P_SIGPROF) {
237 p->p_flags &= ~P_SIGPROF;
240 lwkt_reltoken(&p->p_token);
245 * Post any pending signals
247 * WARNING! postsig() can exit and not return.
249 if ((sig = CURSIG_LCK_TRACE(lp, &ptok)) != 0) {
255 * block here if we are swapped out, but still process signals
256 * (such as SIGKILL). proc0 (the swapin scheduler) is already
257 * aware of our situation, we do not have to wake it up.
259 if (p->p_flags & P_SWAPPEDOUT) {
260 lwkt_gettoken(&p->p_token);
262 p->p_flags |= P_SWAPWAIT;
264 if (p->p_flags & P_SWAPWAIT)
265 tsleep(p, PCATCH, "SWOUT", 0);
266 p->p_flags &= ~P_SWAPWAIT;
268 lwkt_reltoken(&p->p_token);
273 * In a multi-threaded program it is possible for a thread to change
274 * signal state during a system call which temporarily changes the
275 * signal mask. In this case postsig() might not be run and we
276 * have to restore the mask ourselves.
278 if (lp->lwp_flags & LWP_OLDMASK) {
279 lp->lwp_flags &= ~LWP_OLDMASK;
280 lp->lwp_sigmask = lp->lwp_oldsigmask;
286 * Cleanup from userenter and any passive release that might have occured.
287 * We must reclaim the current-process designation before we can return
288 * to usermode. We also handle both LWKT and USER reschedule requests.
291 userexit(struct lwp *lp)
293 struct thread *td = lp->lwp_thread;
294 /* globaldata_t gd = td->td_gd; */
297 * Handle stop requests at kernel priority. Any requests queued
298 * after this loop will generate another AST.
300 while (STOPLWP(lp->lwp_proc, lp)) {
301 lwkt_gettoken(&lp->lwp_proc->p_token);
303 lwkt_reltoken(&lp->lwp_proc->p_token);
307 * Reduce our priority in preparation for a return to userland. If
308 * our passive release function was still in place, our priority was
309 * never raised and does not need to be reduced.
311 lwkt_passive_recover(td);
314 * Become the current user scheduled process if we aren't already,
315 * and deal with reschedule requests and other factors.
317 lp->lwp_proc->p_usched->acquire_curproc(lp);
318 /* WARNING: we may have migrated cpu's */
319 /* gd = td->td_gd; */
322 #if !defined(KTR_KERNENTRY)
323 #define KTR_KERNENTRY KTR_ALL
325 KTR_INFO_MASTER(kernentry);
326 KTR_INFO(KTR_KERNENTRY, kernentry, trap, 0,
327 "TRAP(pid %hd, tid %hd, trapno %ld, eva %lu)",
328 pid_t pid, lwpid_t tid, register_t trapno, vm_offset_t eva);
329 KTR_INFO(KTR_KERNENTRY, kernentry, trap_ret, 0, "TRAP_RET(pid %hd, tid %hd)",
330 pid_t pid, lwpid_t tid);
331 KTR_INFO(KTR_KERNENTRY, kernentry, syscall, 0, "SYSC(pid %hd, tid %hd, nr %ld)",
332 pid_t pid, lwpid_t tid, register_t trapno);
333 KTR_INFO(KTR_KERNENTRY, kernentry, syscall_ret, 0, "SYSRET(pid %hd, tid %hd, err %d)",
334 pid_t pid, lwpid_t tid, int err);
335 KTR_INFO(KTR_KERNENTRY, kernentry, fork_ret, 0, "FORKRET(pid %hd, tid %hd)",
336 pid_t pid, lwpid_t tid);
339 * Exception, fault, and trap interface to the kernel.
340 * This common code is called from assembly language IDT gate entry
341 * routines that prepare a suitable stack frame, and restore this
342 * frame after the exception has been processed.
344 * This function is also called from doreti in an interlock to handle ASTs.
345 * For example: hardwareint->INTROUTINE->(set ast)->doreti->trap
347 * NOTE! We have to retrieve the fault address prior to obtaining the
348 * MP lock because get_mplock() may switch out. YYY cr2 really ought
349 * to be retrieved by the assembly code, not here.
351 * XXX gd_trap_nesting_level currently prevents lwkt_switch() from panicing
352 * if an attempt is made to switch from a fast interrupt or IPI. This is
353 * necessary to properly take fatal kernel traps on SMP machines if
354 * get_mplock() has to block.
358 user_trap(struct trapframe *frame)
360 struct globaldata *gd = mycpu;
361 struct thread *td = gd->gd_curthread;
362 struct lwp *lp = td->td_lwp;
365 int i = 0, ucode = 0, type, code;
367 int crit_count = td->td_critcount;
368 lwkt_tokref_t curstop = td->td_toks_stop;
374 if (frame->tf_trapno == T_PAGEFLT)
375 eva = frame->tf_addr;
379 kprintf("USER_TRAP AT %08lx xflags %ld trapno %ld eva %08lx\n",
380 frame->tf_rip, frame->tf_xflags, frame->tf_trapno, eva);
384 * Everything coming from user mode runs through user_trap,
385 * including system calls.
387 if (frame->tf_trapno == T_FAST_SYSCALL) {
392 KTR_LOG(kernentry_trap, lp->lwp_proc->p_pid, lp->lwp_tid,
393 frame->tf_trapno, eva);
397 eva = (frame->tf_trapno == T_PAGEFLT ? rcr2() : 0);
398 ++gd->gd_trap_nesting_level;
399 trap_fatal(frame, TRUE, eva);
400 --gd->gd_trap_nesting_level;
405 type = frame->tf_trapno;
406 code = frame->tf_err;
410 sticks = (int)td->td_sticks;
411 lp->lwp_md.md_regs = frame;
414 case T_PRIVINFLT: /* privileged instruction fault */
419 case T_BPTFLT: /* bpt instruction fault */
420 case T_TRCTRAP: /* trace trap */
421 frame->tf_rflags &= ~PSL_T;
423 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
426 case T_ARITHTRAP: /* arithmetic trap */
431 case T_ASTFLT: /* Allow process switch */
432 mycpu->gd_cnt.v_soft++;
433 if (mycpu->gd_reqflags & RQF_AST_OWEUPC) {
434 atomic_clear_int(&mycpu->gd_reqflags, RQF_AST_OWEUPC);
435 addupc_task(p, p->p_prof.pr_addr, p->p_prof.pr_ticks);
440 * The following two traps can happen in
441 * vm86 mode, and, if so, we want to handle
444 case T_PROTFLT: /* general protection fault */
445 case T_STKFLT: /* stack fault */
447 if (frame->tf_eflags & PSL_VM) {
448 i = vm86_emulate((struct vm86frame *)frame);
456 case T_SEGNPFLT: /* segment not present fault */
457 case T_TSSFLT: /* invalid TSS fault */
458 case T_DOUBLEFLT: /* double fault */
461 ucode = code + BUS_SEGM_FAULT ;
464 case T_PAGEFLT: /* page fault */
465 i = trap_pfault(frame, TRUE, eva);
466 if (i == -1 || i == 0)
478 case T_DIVIDE: /* integer divide fault */
485 /* machine/parity/power fail/"kitchen sink" faults */
486 if (isa_nmi(code) == 0) {
489 * NMI can be hooked up to a pushbutton
493 kprintf ("NMI ... going to debugger\n");
494 kdb_trap(type, 0, frame);
498 } else if (panic_on_nmi)
499 panic("NMI indicates hardware failure");
501 #endif /* NISA > 0 */
503 case T_OFLOW: /* integer overflow fault */
508 case T_BOUND: /* bounds check fault */
515 * Virtual kernel intercept - pass the DNA exception
516 * to the (emulated) virtual kernel if it asked to handle
517 * it. This occurs when the virtual kernel is holding
518 * onto the FP context for a different emulated
519 * process then the one currently running.
521 * We must still call npxdna() since we may have
522 * saved FP state that the (emulated) virtual kernel
523 * needs to hand over to a different emulated process.
525 if (lp->lwp_vkernel && lp->lwp_vkernel->ve &&
526 (td->td_pcb->pcb_flags & FP_VIRTFP)
533 * The kernel may have switched out the FP unit's
534 * state, causing the user process to take a fault
535 * when it tries to use the FP unit. Restore the
540 if (!pmath_emulate) {
542 ucode = FPE_FPU_NP_TRAP;
545 i = (*pmath_emulate)(frame);
547 if (!(frame->tf_rflags & PSL_T))
549 frame->tf_rflags &= ~PSL_T;
552 /* else ucode = emulator_only_knows() XXX */
555 case T_FPOPFLT: /* FPU operand fetch fault */
560 case T_XMMFLT: /* SIMD floating-point exception */
567 * Virtual kernel intercept - if the fault is directly related to a
568 * VM context managed by a virtual kernel then let the virtual kernel
571 if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
572 vkernel_trap(lp, frame);
577 * Translate fault for emulators (e.g. Linux)
579 if (*p->p_sysent->sv_transtrap)
580 i = (*p->p_sysent->sv_transtrap)(i, type);
582 trapsignal(lp, i, ucode);
585 if (type <= MAX_TRAP_MSG) {
586 uprintf("fatal process exception: %s",
588 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
589 uprintf(", fault VA = 0x%lx", (u_long)eva);
595 userret(lp, frame, sticks);
598 KTR_LOG(kernentry_trap_ret, lp->lwp_proc->p_pid, lp->lwp_tid);
600 KASSERT(crit_count == td->td_critcount,
601 ("trap: critical section count mismatch! %d/%d",
602 crit_count, td->td_pri));
603 KASSERT(curstop == td->td_toks_stop,
604 ("trap: extra tokens held after trap! %ld/%ld",
605 curstop - &td->td_toks_base,
606 td->td_toks_stop - &td->td_toks_base));
611 kern_trap(struct trapframe *frame)
613 struct globaldata *gd = mycpu;
614 struct thread *td = gd->gd_curthread;
617 int i = 0, ucode = 0, type, code;
619 int crit_count = td->td_critcount;
620 lwkt_tokref_t curstop = td->td_toks_stop;
627 if (frame->tf_trapno == T_PAGEFLT)
628 eva = frame->tf_addr;
634 ++gd->gd_trap_nesting_level;
635 trap_fatal(frame, FALSE, eva);
636 --gd->gd_trap_nesting_level;
641 type = frame->tf_trapno;
642 code = frame->tf_err;
650 case T_PAGEFLT: /* page fault */
651 trap_pfault(frame, FALSE, eva);
656 * The kernel may be using npx for copying or other
659 panic("kernel NPX should not happen");
664 case T_PROTFLT: /* general protection fault */
665 case T_SEGNPFLT: /* segment not present fault */
667 * Invalid segment selectors and out of bounds
668 * %eip's and %esp's can be set up in user mode.
669 * This causes a fault in kernel mode when the
670 * kernel tries to return to user mode. We want
671 * to get this fault so that we can fix the
672 * problem here and not have to check all the
673 * selectors and pointers when the user changes
676 if (mycpu->gd_intr_nesting_level == 0) {
677 if (td->td_pcb->pcb_onfault) {
679 (register_t)td->td_pcb->pcb_onfault;
687 * PSL_NT can be set in user mode and isn't cleared
688 * automatically when the kernel is entered. This
689 * causes a TSS fault when the kernel attempts to
690 * `iret' because the TSS link is uninitialized. We
691 * want to get this fault so that we can fix the
692 * problem here and not every time the kernel is
695 if (frame->tf_rflags & PSL_NT) {
696 frame->tf_rflags &= ~PSL_NT;
701 case T_TRCTRAP: /* trace trap */
703 if (frame->tf_eip == (int)IDTVEC(syscall)) {
705 * We've just entered system mode via the
706 * syscall lcall. Continue single stepping
707 * silently until the syscall handler has
712 if (frame->tf_eip == (int)IDTVEC(syscall) + 1) {
714 * The syscall handler has now saved the
715 * flags. Stop single stepping it.
717 frame->tf_eflags &= ~PSL_T;
723 * Ignore debug register trace traps due to
724 * accesses in the user's address space, which
725 * can happen under several conditions such as
726 * if a user sets a watchpoint on a buffer and
727 * then passes that buffer to a system call.
728 * We still want to get TRCTRAPS for addresses
729 * in kernel space because that is useful when
730 * debugging the kernel.
732 if (user_dbreg_trap()) {
734 * Reset breakpoint bits because the
737 load_dr6(rdr6() & 0xfffffff0);
742 * Fall through (TRCTRAP kernel mode, kernel address)
746 * If DDB is enabled, let it handle the debugger trap.
747 * Otherwise, debugger traps "can't happen".
750 if (kdb_trap (type, 0, frame))
755 trap_fatal(frame, FALSE, eva);
758 trap_fatal(frame, FALSE, eva);
763 * Ignore this trap generated from a spurious SIGTRAP.
765 * single stepping in / syscalls leads to spurious / SIGTRAP
768 * Haiku (c) 2007 Simon 'corecode' Schubert
774 * Translate fault for emulators (e.g. Linux)
776 if (*p->p_sysent->sv_transtrap)
777 i = (*p->p_sysent->sv_transtrap)(i, type);
779 trapsignal(lp, i, ucode);
782 if (type <= MAX_TRAP_MSG) {
783 uprintf("fatal process exception: %s",
785 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
786 uprintf(", fault VA = 0x%lx", (u_long)eva);
794 KASSERT(crit_count == td->td_critcount,
795 ("trap: critical section count mismatch! %d/%d",
796 crit_count, td->td_pri));
797 KASSERT(curstop == td->td_toks_stop,
798 ("trap: extra tokens held after trap! %ld/%ld",
799 curstop - &td->td_toks_base,
800 td->td_toks_stop - &td->td_toks_base));
805 trap_pfault(struct trapframe *frame, int usermode, vm_offset_t eva)
808 struct vmspace *vm = NULL;
812 thread_t td = curthread;
813 struct lwp *lp = td->td_lwp;
816 va = trunc_page(eva);
817 if (usermode == FALSE) {
819 * This is a fault on kernel virtual memory.
824 * This is a fault on non-kernel virtual memory.
825 * vm is initialized above to NULL. If curproc is NULL
826 * or curproc->p_vmspace is NULL the fault is fatal.
829 vm = lp->lwp_vmspace;
837 if (frame->tf_err & PGEX_W)
838 ftype = VM_PROT_READ | VM_PROT_WRITE;
840 ftype = VM_PROT_READ;
842 if (map != &kernel_map) {
844 * Keep swapout from messing with us during this
851 * Grow the stack if necessary
853 /* grow_stack returns false only if va falls into
854 * a growable stack region and the stack growth
855 * fails. It returns true if va was not within
856 * a growable stack region, or if the stack
859 if (!grow_stack (map, va)) {
868 fault_flags |= VM_FAULT_BURST | VM_FAULT_USERMODE;
869 if (ftype & VM_PROT_WRITE)
870 fault_flags |= VM_FAULT_DIRTY;
872 fault_flags |= VM_FAULT_NORMAL;
873 rv = vm_fault(map, va, ftype, fault_flags);
878 * Don't have to worry about process locking or stacks in the kernel.
880 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
883 if (rv == KERN_SUCCESS)
887 if (td->td_gd->gd_intr_nesting_level == 0 &&
888 td->td_pcb->pcb_onfault) {
889 frame->tf_rip = (register_t)td->td_pcb->pcb_onfault;
892 trap_fatal(frame, usermode, eva);
897 * NOTE: on x86_64 we have a tf_addr field in the trapframe, no
898 * kludge is needed to pass the fault address to signal handlers.
900 struct proc *p = td->td_proc;
901 kprintf("seg-fault accessing address %p rip=%p pid=%d p_comm=%s\n",
902 (void *)va, (void *)frame->tf_rip, p->p_pid, p->p_comm);
903 /* Debugger("seg-fault"); */
905 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
909 trap_fatal(struct trapframe *frame, int usermode, vm_offset_t eva)
914 code = frame->tf_xflags;
915 type = frame->tf_trapno;
917 if (type <= MAX_TRAP_MSG) {
918 kprintf("\n\nFatal trap %d: %s while in %s mode\n",
919 type, trap_msg[type],
920 (usermode ? "user" : "kernel"));
922 /* two separate prints in case of a trap on an unmapped page */
923 kprintf("cpuid = %d\n", mycpu->gd_cpuid);
924 if (type == T_PAGEFLT) {
925 kprintf("fault virtual address = %p\n", (void *)eva);
926 kprintf("fault code = %s %s, %s\n",
927 usermode ? "user" : "supervisor",
928 code & PGEX_W ? "write" : "read",
929 code & PGEX_P ? "protection violation" : "page not present");
931 kprintf("instruction pointer = 0x%lx:0x%lx\n",
932 frame->tf_cs & 0xffff, frame->tf_rip);
934 ss = frame->tf_ss & 0xffff;
937 ss = GSEL(GDATA_SEL, SEL_KPL);
938 rsp = (long)&frame->tf_rsp;
940 kprintf("stack pointer = 0x%x:0x%lx\n", ss, rsp);
941 kprintf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
942 kprintf("processor eflags = ");
943 if (frame->tf_rflags & PSL_T)
944 kprintf("trace trap, ");
945 if (frame->tf_rflags & PSL_I)
946 kprintf("interrupt enabled, ");
947 if (frame->tf_rflags & PSL_NT)
948 kprintf("nested task, ");
949 if (frame->tf_rflags & PSL_RF)
952 if (frame->tf_eflags & PSL_VM)
955 kprintf("IOPL = %jd\n", (intmax_t)((frame->tf_rflags & PSL_IOPL) >> 12));
956 kprintf("current process = ");
958 kprintf("%lu (%s)\n",
959 (u_long)curproc->p_pid, curproc->p_comm ?
960 curproc->p_comm : "");
964 kprintf("current thread = pri %d ", curthread->td_pri);
965 if (curthread->td_critcount)
970 * we probably SHOULD have stopped the other CPUs before now!
971 * another CPU COULD have been touching cpl at this moment...
973 kprintf(" <- SMP: XXX");
981 if ((debugger_on_panic || db_active) && kdb_trap(type, code, frame))
984 kprintf("trap number = %d\n", type);
985 if (type <= MAX_TRAP_MSG)
986 panic("%s", trap_msg[type]);
988 panic("unknown/reserved trap");
992 * Double fault handler. Called when a fault occurs while writing
993 * a frame for a trap/exception onto the stack. This usually occurs
994 * when the stack overflows (such is the case with infinite recursion,
997 * XXX Note that the current PTD gets replaced by IdlePTD when the
998 * task switch occurs. This means that the stack that was active at
999 * the time of the double fault is not available at <kstack> unless
1000 * the machine was idle when the double fault occurred. The downside
1001 * of this is that "trace <ebp>" in ddb won't work.
1004 dblfault_handler(void)
1007 struct mdglobaldata *gd = mdcpu;
1010 kprintf("\nFatal double fault:\n");
1012 kprintf("rip = 0x%lx\n", gd->gd_common_tss.tss_rip);
1013 kprintf("rsp = 0x%lx\n", gd->gd_common_tss.tss_rsp);
1014 kprintf("rbp = 0x%lx\n", gd->gd_common_tss.tss_rbp);
1016 /* two separate prints in case of a trap on an unmapped page */
1017 kprintf("cpuid = %d\n", mycpu->gd_cpuid);
1018 panic("double fault");
1022 * syscall2 - MP aware system call request C handler
1024 * A system call is essentially treated as a trap except that the
1025 * MP lock is not held on entry or return. We are responsible for
1026 * obtaining the MP lock if necessary and for handling ASTs
1027 * (e.g. a task switch) prior to return.
1029 * In general, only simple access and manipulation of curproc and
1030 * the current stack is allowed without having to hold MP lock.
1032 * MPSAFE - note that large sections of this routine are run without
1036 syscall2(struct trapframe *frame)
1038 struct thread *td = curthread;
1039 struct proc *p = td->td_proc;
1040 struct lwp *lp = td->td_lwp;
1042 struct sysent *callp;
1043 register_t orig_tf_rflags;
1048 int crit_count = td->td_critcount;
1049 lwkt_tokref_t curstop = td->td_toks_stop;
1054 union sysunion args;
1055 register_t *argsdst;
1057 mycpu->gd_cnt.v_syscall++;
1059 KTR_LOG(kernentry_syscall, lp->lwp_proc->p_pid, lp->lwp_tid,
1062 userenter(td, p); /* lazy raise our priority */
1069 sticks = (int)td->td_sticks;
1070 orig_tf_rflags = frame->tf_rflags;
1073 * Virtual kernel intercept - if a VM context managed by a virtual
1074 * kernel issues a system call the virtual kernel handles it, not us.
1075 * Restore the virtual kernel context and return from its system
1076 * call. The current frame is copied out to the virtual kernel.
1078 if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
1079 vkernel_trap(lp, frame);
1080 error = EJUSTRETURN;
1085 * Get the system call parameters and account for time
1087 lp->lwp_md.md_regs = frame;
1088 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
1089 code = frame->tf_rax;
1091 if (p->p_sysent->sv_prepsyscall) {
1092 (*p->p_sysent->sv_prepsyscall)(
1093 frame, (int *)(&args.nosys.sysmsg + 1),
1096 if (code == SYS_syscall || code == SYS___syscall) {
1097 code = frame->tf_rdi;
1103 if (p->p_sysent->sv_mask)
1104 code &= p->p_sysent->sv_mask;
1106 if (code >= p->p_sysent->sv_size)
1107 callp = &p->p_sysent->sv_table[0];
1109 callp = &p->p_sysent->sv_table[code];
1111 narg = callp->sy_narg & SYF_ARGMASK;
1114 * On x86_64 we get up to six arguments in registers. The rest are
1115 * on the stack. The first six members of 'struct trapframe' happen
1116 * to be the registers used to pass arguments, in exactly the right
1119 argp = &frame->tf_rdi;
1121 argsdst = (register_t *)(&args.nosys.sysmsg + 1);
1123 * JG can we overflow the space pointed to by 'argsdst'
1124 * either with 'bcopy' or with 'copyin'?
1126 bcopy(argp, argsdst, sizeof(register_t) * regcnt);
1128 * copyin is MP aware, but the tracing code is not
1130 if (narg > regcnt) {
1131 KASSERT(params != NULL, ("copyin args with no params!"));
1132 error = copyin(params, &argsdst[regcnt],
1133 (narg - regcnt) * sizeof(register_t));
1136 if (KTRPOINT(td, KTR_SYSCALL)) {
1137 ktrsyscall(lp, code, narg,
1138 (void *)(&args.nosys.sysmsg + 1));
1146 if (KTRPOINT(td, KTR_SYSCALL)) {
1147 ktrsyscall(lp, code, narg, (void *)(&args.nosys.sysmsg + 1));
1152 * Default return value is 0 (will be copied to %rax). Double-value
1153 * returns use %rax and %rdx. %rdx is left unchanged for system
1154 * calls which return only one result.
1156 args.sysmsg_fds[0] = 0;
1157 args.sysmsg_fds[1] = frame->tf_rdx;
1160 * The syscall might manipulate the trap frame. If it does it
1161 * will probably return EJUSTRETURN.
1163 args.sysmsg_frame = frame;
1165 STOPEVENT(p, S_SCE, narg); /* MP aware */
1168 * NOTE: All system calls run MPSAFE now. The system call itself
1169 * is responsible for getting the MP lock.
1171 error = (*callp->sy_call)(&args);
1174 kprintf("system call %d returned %d\n", code, error);
1179 * MP SAFE (we may or may not have the MP lock at this point)
1184 * Reinitialize proc pointer `p' as it may be different
1185 * if this is a child returning from fork syscall.
1188 lp = curthread->td_lwp;
1189 frame->tf_rax = args.sysmsg_fds[0];
1190 frame->tf_rdx = args.sysmsg_fds[1];
1191 frame->tf_rflags &= ~PSL_C;
1195 * Reconstruct pc, we know that 'syscall' is 2 bytes.
1196 * We have to do a full context restore so that %r10
1197 * (which was holding the value of %rcx) is restored for
1198 * the next iteration.
1200 frame->tf_rip -= frame->tf_err;
1201 frame->tf_r10 = frame->tf_rcx;
1206 panic("Unexpected EASYNC return value (for now)");
1209 if (p->p_sysent->sv_errsize) {
1210 if (error >= p->p_sysent->sv_errsize)
1211 error = -1; /* XXX */
1213 error = p->p_sysent->sv_errtbl[error];
1215 frame->tf_rax = error;
1216 frame->tf_rflags |= PSL_C;
1221 * Traced syscall. trapsignal() is not MP aware.
1223 if (orig_tf_rflags & PSL_T) {
1224 frame->tf_rflags &= ~PSL_T;
1225 trapsignal(lp, SIGTRAP, 0);
1229 * Handle reschedule and other end-of-syscall issues
1231 userret(lp, frame, sticks);
1234 if (KTRPOINT(td, KTR_SYSRET)) {
1235 ktrsysret(lp, code, error, args.sysmsg_result);
1240 * This works because errno is findable through the
1241 * register set. If we ever support an emulation where this
1242 * is not the case, this code will need to be revisited.
1244 STOPEVENT(p, S_SCX, code);
1247 KTR_LOG(kernentry_syscall_ret, lp->lwp_proc->p_pid, lp->lwp_tid, error);
1249 KASSERT(&td->td_toks_base == td->td_toks_stop,
1250 ("syscall: critical section count mismatch! %d/%d",
1251 crit_count, td->td_pri));
1252 KASSERT(curstop == td->td_toks_stop,
1253 ("syscall: extra tokens held after trap! %ld",
1254 td->td_toks_stop - &td->td_toks_base));
1259 * NOTE: mplock not held at any point
1262 fork_return(struct lwp *lp, struct trapframe *frame)
1264 frame->tf_rax = 0; /* Child returns zero */
1265 frame->tf_rflags &= ~PSL_C; /* success */
1268 generic_lwp_return(lp, frame);
1269 KTR_LOG(kernentry_fork_ret, lp->lwp_proc->p_pid, lp->lwp_tid);
1273 * Simplified back end of syscall(), used when returning from fork()
1274 * directly into user mode.
1276 * This code will return back into the fork trampoline code which then
1279 * NOTE: The mplock is not held at any point.
1282 generic_lwp_return(struct lwp *lp, struct trapframe *frame)
1284 struct proc *p = lp->lwp_proc;
1287 * Check for exit-race. If one lwp exits the process concurrent with
1288 * another lwp creating a new thread, the two operations may cross
1289 * each other resulting in the newly-created lwp not receiving a
1292 if (p->p_flags & P_WEXIT) {
1293 lwpsignal(p, lp, SIGKILL);
1297 * Newly forked processes are given a kernel priority. We have to
1298 * adjust the priority to a normal user priority and fake entry
1299 * into the kernel (call userenter()) to install a passive release
1300 * function just in case userret() decides to stop the process. This
1301 * can occur when ^Z races a fork. If we do not install the passive
1302 * release function the current process designation will not be
1303 * released when the thread goes to sleep.
1305 lwkt_setpri_self(TDPRI_USER_NORM);
1306 userenter(lp->lwp_thread, p);
1307 userret(lp, frame, 0);
1309 if (KTRPOINT(lp->lwp_thread, KTR_SYSRET))
1310 ktrsysret(lp, SYS_fork, 0, 0);
1312 lp->lwp_flags |= LWP_PASSIVE_ACQ;
1314 lp->lwp_flags &= ~LWP_PASSIVE_ACQ;
1318 * doreti has turned into this. The frame is directly on the stack. We
1319 * pull everything else we need (fpu and tls context) from the current
1322 * Note on fpu interactions: In a virtual kernel, the fpu context for
1323 * an emulated user mode process is not shared with the virtual kernel's
1324 * fpu context, so we only have to 'stack' fpu contexts within the virtual
1325 * kernel itself, and not even then since the signal() contexts that we care
1326 * about save and restore the FPU state (I think anyhow).
1328 * vmspace_ctl() returns an error only if it had problems instaling the
1329 * context we supplied or problems copying data to/from our VM space.
1332 go_user(struct intrframe *frame)
1334 struct trapframe *tf = (void *)&frame->if_rdi;
1340 * Interrupts may be disabled on entry, make sure all signals
1341 * can be received before beginning our loop.
1346 * Switch to the current simulated user process, then call
1347 * user_trap() when we break out of it (usually due to a signal).
1352 * Always make the FPU state correct. This should generally
1353 * be faster because the cost of taking a #NM fault through
1354 * the vkernel to the real kernel is astronomical.
1357 tf->tf_xflags &= ~PGEX_FPFAULT;
1358 if (mdcpu->gd_npxthread != curthread) {
1359 if (mdcpu->gd_npxthread)
1360 npxsave(mdcpu->gd_npxthread->td_savefpu);
1365 * Tell the real kernel whether it is ok to use the FP
1366 * unit or not, allowing us to take a T_DNA exception
1367 * if the context tries to use the FP.
1369 if (mdcpu->gd_npxthread == curthread) {
1370 tf->tf_xflags &= ~PGEX_FPFAULT;
1372 tf->tf_xflags |= PGEX_FPFAULT;
1377 * Run emulated user process context. This call interlocks
1378 * with new mailbox signals.
1380 * Set PGEX_U unconditionally, indicating a user frame (the
1381 * bit is normally set only by T_PAGEFLT).
1384 id = (void *)vtophys(curproc->p_vmspace->vm_pmap.pm_pml4);
1386 id = &curproc->p_vmspace->vm_pmap;
1389 * The GDF_VIRTUSER hack helps statclock() figure out who
1390 * the tick belongs to.
1393 gd->gd_flags |= GDF_VIRTUSER;
1394 r = vmspace_ctl(id, VMSPACE_CTL_RUN, tf,
1395 &curthread->td_savevext);
1396 gd->gd_flags &= ~GDF_VIRTUSER;
1398 frame->if_xflags |= PGEX_U;
1401 * Immediately save the user FPU state. The vkernel is a
1402 * user program and libraries like libc will use the FP
1405 if (mdcpu->gd_npxthread == curthread) {
1406 npxsave(mdcpu->gd_npxthread->td_savefpu);
1410 kprintf("GO USER %d trap %ld EVA %08lx RIP %08lx RSP %08lx XFLAGS %02lx/%02lx\n",
1411 r, tf->tf_trapno, tf->tf_addr, tf->tf_rip, tf->tf_rsp,
1412 tf->tf_xflags, frame->if_xflags);
1416 panic("vmspace_ctl failed error %d", errno);
1418 if (tf->tf_trapno) {
1422 if (mycpu->gd_reqflags & RQF_AST_MASK) {
1423 tf->tf_trapno = T_ASTFLT;
1431 * If PGEX_FPFAULT is set then set FP_VIRTFP in the PCB to force a T_DNA
1432 * fault (which is then passed back to the virtual kernel) if an attempt is
1433 * made to use the FP unit.
1435 * XXX this is a fairly big hack.
1438 set_vkernel_fp(struct trapframe *frame)
1440 struct thread *td = curthread;
1442 if (frame->tf_xflags & PGEX_FPFAULT) {
1443 td->td_pcb->pcb_flags |= FP_VIRTFP;
1444 if (mdcpu->gd_npxthread == td)
1447 td->td_pcb->pcb_flags &= ~FP_VIRTFP;
1452 * Called from vkernel_trap() to fixup the vkernel's syscall
1453 * frame for vmspace_ctl() return.
1456 cpu_vkernel_trap(struct trapframe *frame, int error)
1458 frame->tf_rax = error;
1460 frame->tf_rflags |= PSL_C;
1462 frame->tf_rflags &= ~PSL_C;