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.70 2005/11/22 01:52:25 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/upcall.h>
72 #include <sys/sysproto.h>
73 #include <sys/sysunion.h>
76 #include <vm/vm_param.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_extern.h>
84 #include <machine/cpu.h>
85 #include <machine/ipl.h>
86 #include <machine/md_var.h>
87 #include <machine/pcb.h>
88 #include <machine/smp.h>
89 #include <machine/tss.h>
90 #include <machine/globaldata.h>
92 #include <i386/isa/intr_machdep.h>
95 #include <sys/syslog.h>
96 #include <machine/clock.h>
99 #include <machine/vm86.h>
102 #include <sys/msgport2.h>
103 #include <sys/thread2.h>
107 #define MAKEMPSAFE(have_mplock) \
108 if (have_mplock == 0) { \
115 #define MAKEMPSAFE(have_mplock)
119 int (*pmath_emulate) (struct trapframe *);
121 extern void trap (struct trapframe frame);
122 extern int trapwrite (unsigned addr);
123 extern void syscall2 (struct trapframe frame);
124 extern void sendsys2 (struct trapframe frame);
125 extern void waitsys2 (struct trapframe frame);
127 static int trap_pfault (struct trapframe *, int, vm_offset_t);
128 static void trap_fatal (struct trapframe *, vm_offset_t);
129 void dblfault_handler (void);
131 extern inthand_t IDTVEC(syscall);
133 #define MAX_TRAP_MSG 28
134 static char *trap_msg[] = {
136 "privileged instruction fault", /* 1 T_PRIVINFLT */
138 "breakpoint instruction fault", /* 3 T_BPTFLT */
141 "arithmetic trap", /* 6 T_ARITHTRAP */
142 "system forced exception", /* 7 T_ASTFLT */
144 "general protection fault", /* 9 T_PROTFLT */
145 "trace trap", /* 10 T_TRCTRAP */
147 "page fault", /* 12 T_PAGEFLT */
149 "alignment fault", /* 14 T_ALIGNFLT */
153 "integer divide fault", /* 18 T_DIVIDE */
154 "non-maskable interrupt trap", /* 19 T_NMI */
155 "overflow trap", /* 20 T_OFLOW */
156 "FPU bounds check fault", /* 21 T_BOUND */
157 "FPU device not available", /* 22 T_DNA */
158 "double fault", /* 23 T_DOUBLEFLT */
159 "FPU operand fetch fault", /* 24 T_FPOPFLT */
160 "invalid TSS fault", /* 25 T_TSSFLT */
161 "segment not present fault", /* 26 T_SEGNPFLT */
162 "stack fault", /* 27 T_STKFLT */
163 "machine check trap", /* 28 T_MCHK */
166 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
167 extern int has_f00f_bug;
171 static int ddb_on_nmi = 1;
172 SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
173 &ddb_on_nmi, 0, "Go to DDB on NMI");
175 static int panic_on_nmi = 1;
176 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
177 &panic_on_nmi, 0, "Panic on NMI");
178 static int fast_release;
179 SYSCTL_INT(_machdep, OID_AUTO, fast_release, CTLFLAG_RW,
180 &fast_release, 0, "Passive Release was optimal");
181 static int slow_release;
182 SYSCTL_INT(_machdep, OID_AUTO, slow_release, CTLFLAG_RW,
183 &slow_release, 0, "Passive Release was nonoptimal");
185 static int syscall_mpsafe = 0;
186 SYSCTL_INT(_kern, OID_AUTO, syscall_mpsafe, CTLFLAG_RW,
187 &syscall_mpsafe, 0, "Allow MPSAFE marked syscalls to run without BGL");
188 TUNABLE_INT("kern.syscall_mpsafe", &syscall_mpsafe);
189 static int trap_mpsafe = 0;
190 SYSCTL_INT(_kern, OID_AUTO, trap_mpsafe, CTLFLAG_RW,
191 &trap_mpsafe, 0, "Allow traps to mostly run without the BGL");
192 TUNABLE_INT("kern.trap_mpsafe", &trap_mpsafe);
195 MALLOC_DEFINE(M_SYSMSG, "sysmsg", "sysmsg structure");
196 extern int max_sysmsg;
199 * Passive USER->KERNEL transition. This only occurs if we block in the
200 * kernel while still holding our userland priority. We have to fixup our
201 * priority in order to avoid potential deadlocks before we allow the system
202 * to switch us to another thread.
205 passive_release(struct thread *td)
207 struct lwp *lp = td->td_lwp;
209 td->td_release = NULL;
210 lwkt_setpri_self(TDPRI_KERN_USER);
211 lp->lwp_proc->p_usched->release_curproc(lp);
215 * userenter() passively intercepts the thread switch function to increase
216 * the thread priority from a user priority to a kernel priority, reducing
217 * syscall and trap overhead for the case where no switch occurs.
221 userenter(struct thread *curtd)
223 curtd->td_release = passive_release;
227 * Handle signals, upcalls, profiling, and other AST's and/or tasks that
228 * must be completed before we can return to or try to return to userland.
230 * Note that td_sticks is a 64 bit quantity, but there's no point doing 64
231 * arithmatic on the delta calculation so the absolute tick values are
232 * truncated to an integer.
235 userret(struct lwp *lp, struct trapframe *frame, int sticks)
237 struct proc *p = lp->lwp_proc;
241 * Charge system time if profiling. Note: times are in microseconds.
242 * This may do a copyout and block, so do it first even though it
243 * means some system time will be charged as user time.
245 if (p->p_flag & P_PROFIL) {
246 addupc_task(p, frame->tf_eip,
247 (u_int)((int)p->p_thread->td_sticks - sticks));
252 * Block here if we are in a stopped state.
254 if (p->p_flag & P_STOPPED) {
262 * Post any pending upcalls
264 if (p->p_flag & P_UPCALLPEND) {
265 p->p_flag &= ~P_UPCALLPEND;
273 * Post any pending signals
275 if ((sig = CURSIG(p)) != 0) {
283 * block here if we are swapped out, but still process signals
284 * (such as SIGKILL). proc0 (the swapin scheduler) is already
285 * aware of our situation, we do not have to wake it up.
287 if (p->p_flag & P_SWAPPEDOUT) {
289 p->p_flag |= P_SWAPWAIT;
291 if (p->p_flag & P_SWAPWAIT)
292 tsleep(p, PCATCH, "SWOUT", 0);
293 p->p_flag &= ~P_SWAPWAIT;
300 * Cleanup from userenter and any passive release that might have occured.
301 * We must reclaim the current-process designation before we can return
302 * to usermode. We also handle both LWKT and USER reschedule requests.
305 userexit(struct lwp *lp)
307 struct thread *td = lp->lwp_thread;
308 globaldata_t gd = td->td_gd;
312 * If a user reschedule is requested force a new process to be
313 * chosen by releasing the current process. Our process will only
314 * be chosen again if it has a considerably better priority.
316 if (user_resched_wanted())
317 lp->lwp_proc->p_usched->release_curproc(lp);
322 * Handle a LWKT reschedule request first. Since our passive release
323 * is still in place we do not have to do anything special.
325 if (lwkt_resched_wanted())
329 * Acquire the current process designation if we do not own it.
330 * Note that acquire_curproc() does not reset the user reschedule
331 * bit on purpose, because we may need to accumulate over several
332 * threads waking up at the same time.
334 * NOTE: userland scheduler cruft: because processes are removed
335 * from the userland scheduler's queue we run through loops to try
336 * to figure out which is the best of [ existing, waking-up ]
339 if (lp != gd->gd_uschedcp) {
341 lp->lwp_proc->p_usched->acquire_curproc(lp);
342 /* We may have switched cpus on acquisition */
349 * Reduce our priority in preparation for a return to userland. If
350 * our passive release function was still in place, our priority was
351 * never raised and does not need to be reduced.
353 if (td->td_release == NULL)
354 lwkt_setpri_self(TDPRI_USER_NORM);
355 td->td_release = NULL;
358 * After reducing our priority there might be other kernel-level
359 * LWKTs that now have a greater priority. Run them as necessary.
360 * We don't have to worry about losing cpu to userland because
361 * we still control the current-process designation and we no longer
362 * have a passive release function installed.
364 if (lwkt_checkpri_self())
368 * If a userland reschedule is [still] pending we may not be the best
369 * selected process. Select a better one. If another LWKT resched
370 * is pending the trap will be re-entered.
372 if (user_resched_wanted()) {
373 lp->lwp_proc->p_usched->select_curproc(gd);
374 if (lp != gd->gd_uschedcp) {
375 lwkt_setpri_self(TDPRI_KERN_USER);
382 * Exception, fault, and trap interface to the kernel.
383 * This common code is called from assembly language IDT gate entry
384 * routines that prepare a suitable stack frame, and restore this
385 * frame after the exception has been processed.
387 * This function is also called from doreti in an interlock to handle ASTs.
388 * For example: hardwareint->INTROUTINE->(set ast)->doreti->trap
390 * NOTE! We have to retrieve the fault address prior to obtaining the
391 * MP lock because get_mplock() may switch out. YYY cr2 really ought
392 * to be retrieved by the assembly code, not here.
394 * XXX gd_trap_nesting_level currently prevents lwkt_switch() from panicing
395 * if an attempt is made to switch from a fast interrupt or IPI. This is
396 * necessary to properly take fatal kernel traps on SMP machines if
397 * get_mplock() has to block.
402 struct trapframe frame;
404 struct globaldata *gd = mycpu;
405 struct thread *td = gd->gd_curthread;
406 struct lwp *lp = td->td_lwp;
409 int i = 0, ucode = 0, type, code;
418 eva = (frame.tf_trapno == T_PAGEFLT ? rcr2() : 0);
419 ++gd->gd_trap_nesting_level;
420 MAKEMPSAFE(have_mplock);
421 trap_fatal(&frame, eva);
422 --gd->gd_trap_nesting_level;
428 ++gd->gd_trap_nesting_level;
429 if (frame.tf_trapno == T_PAGEFLT) {
431 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
432 * This problem is worked around by using an interrupt
433 * gate for the pagefault handler. We are finally ready
434 * to read %cr2 and then must reenable interrupts.
436 * XXX this should be in the switch statement, but the
437 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
438 * flow of control too much for this to be obviously
445 if (trap_mpsafe == 0)
446 MAKEMPSAFE(have_mplock);
449 --gd->gd_trap_nesting_level;
451 if (!(frame.tf_eflags & PSL_I)) {
453 * Buggy application or kernel code has disabled interrupts
454 * and then trapped. Enabling interrupts now is wrong, but
455 * it is better than running with interrupts disabled until
456 * they are accidentally enabled later.
458 type = frame.tf_trapno;
459 if (ISPL(frame.tf_cs)==SEL_UPL || (frame.tf_eflags & PSL_VM)) {
460 MAKEMPSAFE(have_mplock);
462 "pid %ld (%s): trap %d with interrupts disabled\n",
463 (long)curproc->p_pid, curproc->p_comm, type);
464 } else if (type != T_BPTFLT && type != T_TRCTRAP) {
466 * XXX not quite right, since this may be for a
467 * multiple fault in user mode.
469 MAKEMPSAFE(have_mplock);
470 printf("kernel trap %d with interrupts disabled\n",
476 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
479 type = frame.tf_trapno;
483 ASSERT_MP_LOCK_HELD(curthread);
484 if (frame.tf_eflags & PSL_VM &&
485 (type == T_PROTFLT || type == T_STKFLT)) {
487 KKASSERT(td->td_mpcount > 0);
489 i = vm86_emulate((struct vm86frame *)&frame);
491 KKASSERT(td->td_mpcount > 0);
495 * returns to original process
497 vm86_trap((struct vm86frame *)&frame);
504 * these traps want either a process context, or
505 * assume a normal userspace trap.
509 trap_fatal(&frame, eva);
512 type = T_BPTFLT; /* kernel breakpoint */
515 goto kernel_trap; /* normal kernel trap handling */
518 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
523 sticks = (int)td->td_sticks;
524 lp->lwp_md.md_regs = &frame;
527 case T_PRIVINFLT: /* privileged instruction fault */
532 case T_BPTFLT: /* bpt instruction fault */
533 case T_TRCTRAP: /* trace trap */
534 frame.tf_eflags &= ~PSL_T;
538 case T_ARITHTRAP: /* arithmetic trap */
543 case T_ASTFLT: /* Allow process switch */
544 mycpu->gd_cnt.v_soft++;
545 if (mycpu->gd_reqflags & RQF_AST_OWEUPC) {
546 atomic_clear_int_nonlocked(&mycpu->gd_reqflags,
548 addupc_task(p, p->p_prof.pr_addr,
554 * The following two traps can happen in
555 * vm86 mode, and, if so, we want to handle
558 case T_PROTFLT: /* general protection fault */
559 case T_STKFLT: /* stack fault */
560 if (frame.tf_eflags & PSL_VM) {
561 i = vm86_emulate((struct vm86frame *)&frame);
568 case T_SEGNPFLT: /* segment not present fault */
569 case T_TSSFLT: /* invalid TSS fault */
570 case T_DOUBLEFLT: /* double fault */
572 ucode = code + BUS_SEGM_FAULT ;
576 case T_PAGEFLT: /* page fault */
577 MAKEMPSAFE(have_mplock);
578 i = trap_pfault(&frame, TRUE, eva);
581 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
591 case T_DIVIDE: /* integer divide fault */
598 MAKEMPSAFE(have_mplock);
600 goto handle_powerfail;
601 #else /* !POWERFAIL_NMI */
602 /* machine/parity/power fail/"kitchen sink" faults */
603 if (isa_nmi(code) == 0) {
606 * NMI can be hooked up to a pushbutton
610 printf ("NMI ... going to debugger\n");
611 kdb_trap (type, 0, &frame);
615 } else if (panic_on_nmi)
616 panic("NMI indicates hardware failure");
618 #endif /* POWERFAIL_NMI */
619 #endif /* NISA > 0 */
621 case T_OFLOW: /* integer overflow fault */
626 case T_BOUND: /* bounds check fault */
634 * The kernel may have switched out the FP unit's
635 * state, causing the user process to take a fault
636 * when it tries to use the FP unit. Restore the
642 if (!pmath_emulate) {
644 ucode = FPE_FPU_NP_TRAP;
647 i = (*pmath_emulate)(&frame);
649 if (!(frame.tf_eflags & PSL_T))
651 frame.tf_eflags &= ~PSL_T;
654 /* else ucode = emulator_only_knows() XXX */
657 case T_FPOPFLT: /* FPU operand fetch fault */
662 case T_XMMFLT: /* SIMD floating-point exception */
672 case T_PAGEFLT: /* page fault */
673 MAKEMPSAFE(have_mplock);
674 (void) trap_pfault(&frame, FALSE, eva);
680 * The kernel may be using npx for copying or other
688 case T_PROTFLT: /* general protection fault */
689 case T_SEGNPFLT: /* segment not present fault */
691 * Invalid segment selectors and out of bounds
692 * %eip's and %esp's can be set up in user mode.
693 * This causes a fault in kernel mode when the
694 * kernel tries to return to user mode. We want
695 * to get this fault so that we can fix the
696 * problem here and not have to check all the
697 * selectors and pointers when the user changes
700 #define MAYBE_DORETI_FAULT(where, whereto) \
702 if (frame.tf_eip == (int)where) { \
703 frame.tf_eip = (int)whereto; \
708 * Since we don't save %gs across an interrupt
709 * frame this check must occur outside the intr
710 * nesting level check.
712 if (frame.tf_eip == (int)cpu_switch_load_gs) {
713 td->td_pcb->pcb_gs = 0;
714 MAKEMPSAFE(have_mplock);
718 if (mycpu->gd_intr_nesting_level == 0) {
720 * Invalid %fs's and %gs's can be created using
721 * procfs or PT_SETREGS or by invalidating the
722 * underlying LDT entry. This causes a fault
723 * in kernel mode when the kernel attempts to
724 * switch contexts. Lose the bad context
725 * (XXX) so that we can continue, and generate
728 MAYBE_DORETI_FAULT(doreti_iret,
730 MAYBE_DORETI_FAULT(doreti_popl_ds,
731 doreti_popl_ds_fault);
732 MAYBE_DORETI_FAULT(doreti_popl_es,
733 doreti_popl_es_fault);
734 MAYBE_DORETI_FAULT(doreti_popl_fs,
735 doreti_popl_fs_fault);
736 if (td->td_pcb->pcb_onfault) {
738 (register_t)td->td_pcb->pcb_onfault;
746 * PSL_NT can be set in user mode and isn't cleared
747 * automatically when the kernel is entered. This
748 * causes a TSS fault when the kernel attempts to
749 * `iret' because the TSS link is uninitialized. We
750 * want to get this fault so that we can fix the
751 * problem here and not every time the kernel is
754 if (frame.tf_eflags & PSL_NT) {
755 frame.tf_eflags &= ~PSL_NT;
760 case T_TRCTRAP: /* trace trap */
761 if (frame.tf_eip == (int)IDTVEC(syscall)) {
763 * We've just entered system mode via the
764 * syscall lcall. Continue single stepping
765 * silently until the syscall handler has
770 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
772 * The syscall handler has now saved the
773 * flags. Stop single stepping it.
775 frame.tf_eflags &= ~PSL_T;
779 * Ignore debug register trace traps due to
780 * accesses in the user's address space, which
781 * can happen under several conditions such as
782 * if a user sets a watchpoint on a buffer and
783 * then passes that buffer to a system call.
784 * We still want to get TRCTRAPS for addresses
785 * in kernel space because that is useful when
786 * debugging the kernel.
788 if (user_dbreg_trap()) {
790 * Reset breakpoint bits because the
793 load_dr6(rdr6() & 0xfffffff0);
797 * Fall through (TRCTRAP kernel mode, kernel address)
801 * If DDB is enabled, let it handle the debugger trap.
802 * Otherwise, debugger traps "can't happen".
805 MAKEMPSAFE(have_mplock);
806 if (kdb_trap (type, 0, &frame))
813 MAKEMPSAFE(have_mplock);
816 # define TIMER_FREQ 1193182
820 static unsigned lastalert = 0;
822 if(time_second - lastalert > 10)
824 log(LOG_WARNING, "NMI: power fail\n");
825 sysbeep(TIMER_FREQ/880, hz);
826 lastalert = time_second;
831 #else /* !POWERFAIL_NMI */
832 /* machine/parity/power fail/"kitchen sink" faults */
833 if (isa_nmi(code) == 0) {
836 * NMI can be hooked up to a pushbutton
840 printf ("NMI ... going to debugger\n");
841 kdb_trap (type, 0, &frame);
845 } else if (panic_on_nmi == 0)
848 #endif /* POWERFAIL_NMI */
849 #endif /* NISA > 0 */
852 MAKEMPSAFE(have_mplock);
853 trap_fatal(&frame, eva);
857 /* Translate fault for emulators (e.g. Linux) */
858 if (*p->p_sysent->sv_transtrap)
859 i = (*p->p_sysent->sv_transtrap)(i, type);
861 MAKEMPSAFE(have_mplock);
862 trapsignal(p, i, ucode);
865 if (type <= MAX_TRAP_MSG) {
866 uprintf("fatal process exception: %s",
868 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
869 uprintf(", fault VA = 0x%lx", (u_long)eva);
876 if (ISPL(frame.tf_cs) == SEL_UPL)
877 KASSERT(td->td_mpcount == have_mplock, ("badmpcount trap/end from %p", (void *)frame.tf_eip));
879 userret(lp, &frame, sticks);
883 KKASSERT(td->td_mpcount >= have_mplock);
891 * This version doesn't allow a page fault to user space while
892 * in the kernel. The rest of the kernel needs to be made "safe"
893 * before this can be used. I think the only things remaining
894 * to be made safe are the iBCS2 code and the process tracing/
898 trap_pfault(frame, usermode, eva)
899 struct trapframe *frame;
904 struct vmspace *vm = NULL;
908 thread_t td = curthread;
909 struct proc *p = td->td_proc; /* may be NULL */
911 if (frame->tf_err & PGEX_W)
912 ftype = VM_PROT_WRITE;
914 ftype = VM_PROT_READ;
916 va = trunc_page(eva);
917 if (va < VM_MIN_KERNEL_ADDRESS) {
922 (!usermode && va < VM_MAXUSER_ADDRESS &&
923 (td->td_gd->gd_intr_nesting_level != 0 ||
924 td->td_pcb->pcb_onfault == NULL))) {
925 trap_fatal(frame, eva);
930 * This is a fault on non-kernel virtual memory.
931 * vm is initialized above to NULL. If curproc is NULL
932 * or curproc->p_vmspace is NULL the fault is fatal.
941 * Keep swapout from messing with us during this
947 * Grow the stack if necessary
949 /* grow_stack returns false only if va falls into
950 * a growable stack region and the stack growth
951 * fails. It returns true if va was not within
952 * a growable stack region, or if the stack
955 if (!grow_stack (p, va)) {
961 /* Fault in the user page: */
962 rv = vm_fault(map, va, ftype,
963 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
969 * Don't allow user-mode faults in kernel address space.
975 * Since we know that kernel virtual address addresses
976 * always have pte pages mapped, we just have to fault
979 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
982 if (rv == KERN_SUCCESS)
986 if (mtd->td_gd->gd_intr_nesting_level == 0 &&
987 td->td_pcb->pcb_onfault) {
988 frame->tf_eip = (register_t)td->td_pcb->pcb_onfault;
991 trap_fatal(frame, eva);
995 /* kludge to pass faulting virtual address to sendsig */
998 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
1003 trap_pfault(frame, usermode, eva)
1004 struct trapframe *frame;
1009 struct vmspace *vm = NULL;
1013 thread_t td = curthread;
1014 struct proc *p = td->td_proc;
1016 va = trunc_page(eva);
1017 if (va >= KERNBASE) {
1019 * Don't allow user-mode faults in kernel address space.
1020 * An exception: if the faulting address is the invalid
1021 * instruction entry in the IDT, then the Intel Pentium
1022 * F00F bug workaround was triggered, and we need to
1023 * treat it is as an illegal instruction, and not a page
1026 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1027 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
1028 frame->tf_trapno = T_PRIVINFLT;
1038 * This is a fault on non-kernel virtual memory.
1039 * vm is initialized above to NULL. If curproc is NULL
1040 * or curproc->p_vmspace is NULL the fault is fatal.
1051 if (frame->tf_err & PGEX_W)
1052 ftype = VM_PROT_WRITE;
1054 ftype = VM_PROT_READ;
1056 if (map != kernel_map) {
1058 * Keep swapout from messing with us during this
1064 * Grow the stack if necessary
1066 /* grow_stack returns false only if va falls into
1067 * a growable stack region and the stack growth
1068 * fails. It returns true if va was not within
1069 * a growable stack region, or if the stack
1072 if (!grow_stack (p, va)) {
1078 /* Fault in the user page: */
1079 rv = vm_fault(map, va, ftype,
1080 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
1086 * Don't have to worry about process locking or stacks in the kernel.
1088 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
1091 if (rv == KERN_SUCCESS)
1095 if (td->td_gd->gd_intr_nesting_level == 0 &&
1096 td->td_pcb->pcb_onfault) {
1097 frame->tf_eip = (register_t)td->td_pcb->pcb_onfault;
1100 trap_fatal(frame, eva);
1104 /* kludge to pass faulting virtual address to sendsig */
1105 frame->tf_err = eva;
1107 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
1111 trap_fatal(frame, eva)
1112 struct trapframe *frame;
1115 int code, type, ss, esp;
1116 struct soft_segment_descriptor softseg;
1118 code = frame->tf_err;
1119 type = frame->tf_trapno;
1120 sdtossd(&gdt[mycpu->gd_cpuid * NGDT + IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
1122 if (type <= MAX_TRAP_MSG)
1123 printf("\n\nFatal trap %d: %s while in %s mode\n",
1124 type, trap_msg[type],
1125 frame->tf_eflags & PSL_VM ? "vm86" :
1126 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
1128 /* three separate prints in case of a trap on an unmapped page */
1129 printf("mp_lock = %08x; ", mp_lock);
1130 printf("cpuid = %d; ", mycpu->gd_cpuid);
1131 printf("lapic.id = %08x\n", lapic.id);
1133 if (type == T_PAGEFLT) {
1134 printf("fault virtual address = 0x%x\n", eva);
1135 printf("fault code = %s %s, %s\n",
1136 code & PGEX_U ? "user" : "supervisor",
1137 code & PGEX_W ? "write" : "read",
1138 code & PGEX_P ? "protection violation" : "page not present");
1140 printf("instruction pointer = 0x%x:0x%x\n",
1141 frame->tf_cs & 0xffff, frame->tf_eip);
1142 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
1143 ss = frame->tf_ss & 0xffff;
1144 esp = frame->tf_esp;
1146 ss = GSEL(GDATA_SEL, SEL_KPL);
1147 esp = (int)&frame->tf_esp;
1149 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
1150 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
1151 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
1152 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
1153 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
1154 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
1156 printf("processor eflags = ");
1157 if (frame->tf_eflags & PSL_T)
1158 printf("trace trap, ");
1159 if (frame->tf_eflags & PSL_I)
1160 printf("interrupt enabled, ");
1161 if (frame->tf_eflags & PSL_NT)
1162 printf("nested task, ");
1163 if (frame->tf_eflags & PSL_RF)
1165 if (frame->tf_eflags & PSL_VM)
1167 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
1168 printf("current process = ");
1170 printf("%lu (%s)\n",
1171 (u_long)curproc->p_pid, curproc->p_comm ?
1172 curproc->p_comm : "");
1176 printf("current thread = pri %d ", curthread->td_pri);
1177 if (curthread->td_pri >= TDPRI_CRIT)
1183 * we probably SHOULD have stopped the other CPUs before now!
1184 * another CPU COULD have been touching cpl at this moment...
1186 printf(" <- SMP: XXX");
1195 if ((debugger_on_panic || db_active) && kdb_trap(type, code, frame))
1198 printf("trap number = %d\n", type);
1199 if (type <= MAX_TRAP_MSG)
1200 panic("%s", trap_msg[type]);
1202 panic("unknown/reserved trap");
1206 * Double fault handler. Called when a fault occurs while writing
1207 * a frame for a trap/exception onto the stack. This usually occurs
1208 * when the stack overflows (such is the case with infinite recursion,
1211 * XXX Note that the current PTD gets replaced by IdlePTD when the
1212 * task switch occurs. This means that the stack that was active at
1213 * the time of the double fault is not available at <kstack> unless
1214 * the machine was idle when the double fault occurred. The downside
1215 * of this is that "trace <ebp>" in ddb won't work.
1220 struct mdglobaldata *gd = mdcpu;
1222 printf("\nFatal double fault:\n");
1223 printf("eip = 0x%x\n", gd->gd_common_tss.tss_eip);
1224 printf("esp = 0x%x\n", gd->gd_common_tss.tss_esp);
1225 printf("ebp = 0x%x\n", gd->gd_common_tss.tss_ebp);
1227 /* three separate prints in case of a trap on an unmapped page */
1228 printf("mp_lock = %08x; ", mp_lock);
1229 printf("cpuid = %d; ", mycpu->gd_cpuid);
1230 printf("lapic.id = %08x\n", lapic.id);
1232 panic("double fault");
1236 * Compensate for 386 brain damage (missing URKR).
1237 * This is a little simpler than the pagefault handler in trap() because
1238 * it the page tables have already been faulted in and high addresses
1239 * are thrown out early for other reasons.
1249 va = trunc_page((vm_offset_t)addr);
1251 * XXX - MAX is END. Changed > to >= for temp. fix.
1253 if (va >= VM_MAXUSER_ADDRESS)
1261 if (!grow_stack (p, va)) {
1267 * fault the data page
1269 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
1273 if (rv != KERN_SUCCESS)
1280 * syscall2 - MP aware system call request C handler
1282 * A system call is essentially treated as a trap except that the
1283 * MP lock is not held on entry or return. We are responsible for
1284 * obtaining the MP lock if necessary and for handling ASTs
1285 * (e.g. a task switch) prior to return.
1287 * In general, only simple access and manipulation of curproc and
1288 * the current stack is allowed without having to hold MP lock.
1290 * MPSAFE - note that large sections of this routine are run without
1295 syscall2(struct trapframe frame)
1297 struct thread *td = curthread;
1298 struct proc *p = td->td_proc;
1299 struct lwp *lp = td->td_lwp;
1301 struct sysent *callp;
1302 register_t orig_tf_eflags;
1307 int have_mplock = 0;
1310 union sysunion args;
1313 if (ISPL(frame.tf_cs) != SEL_UPL) {
1321 KASSERT(td->td_mpcount == 0, ("badmpcount syscall2 from %p", (void *)frame.tf_eip));
1322 if (syscall_mpsafe == 0)
1323 MAKEMPSAFE(have_mplock);
1325 userenter(td); /* lazy raise our priority */
1327 sticks = (int)td->td_sticks;
1329 lp->lwp_md.md_regs = &frame;
1330 params = (caddr_t)frame.tf_esp + sizeof(int);
1331 code = frame.tf_eax;
1332 orig_tf_eflags = frame.tf_eflags;
1334 if (p->p_sysent->sv_prepsyscall) {
1335 (*p->p_sysent->sv_prepsyscall)(
1336 &frame, (int *)(&args.nosys.usrmsg + 1),
1340 * Need to check if this is a 32 bit or 64 bit syscall.
1341 * fuword is MP aware.
1343 if (code == SYS_syscall) {
1345 * Code is first argument, followed by actual args.
1347 code = fuword(params);
1348 params += sizeof(int);
1349 } else if (code == SYS___syscall) {
1351 * Like syscall, but code is a quad, so as to maintain
1352 * quad alignment for the rest of the arguments.
1354 code = fuword(params);
1355 params += sizeof(quad_t);
1359 code &= p->p_sysent->sv_mask;
1360 if (code >= p->p_sysent->sv_size)
1361 callp = &p->p_sysent->sv_table[0];
1363 callp = &p->p_sysent->sv_table[code];
1365 narg = callp->sy_narg & SYF_ARGMASK;
1368 * copyin is MP aware, but the tracing code is not
1370 if (narg && params) {
1371 error = copyin(params, (caddr_t)(&args.nosys.usrmsg + 1),
1372 narg * sizeof(register_t));
1375 if (KTRPOINT(td, KTR_SYSCALL)) {
1376 MAKEMPSAFE(have_mplock);
1378 ktrsyscall(p->p_tracep, code, narg,
1379 (void *)(&args.nosys.usrmsg + 1));
1387 if (KTRPOINT(td, KTR_SYSCALL)) {
1388 MAKEMPSAFE(have_mplock);
1389 ktrsyscall(p->p_tracep, code, narg, (void *)(&args.nosys.usrmsg + 1));
1394 * For traditional syscall code edx is left untouched when 32 bit
1395 * results are returned. Since edx is loaded from fds[1] when the
1396 * system call returns we pre-set it here.
1398 lwkt_initmsg(&args.lmsg, &td->td_msgport, 0,
1399 lwkt_cmd_op(code), lwkt_cmd_op_none);
1400 args.sysmsg_copyout = NULL;
1401 args.sysmsg_fds[0] = 0;
1402 args.sysmsg_fds[1] = frame.tf_edx;
1404 STOPEVENT(p, S_SCE, narg); /* MP aware */
1408 * Try to run the syscall without the MP lock if the syscall
1409 * is MP safe. We have to obtain the MP lock no matter what if
1412 if ((callp->sy_narg & SYF_MPSAFE) == 0)
1413 MAKEMPSAFE(have_mplock);
1416 error = (*callp->sy_call)(&args);
1419 * MP SAFE (we may or may not have the MP lock at this point)
1424 * Reinitialize proc pointer `p' as it may be different
1425 * if this is a child returning from fork syscall.
1428 lp = curthread->td_lwp;
1429 frame.tf_eax = args.sysmsg_fds[0];
1430 frame.tf_edx = args.sysmsg_fds[1];
1431 frame.tf_eflags &= ~PSL_C;
1435 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1436 * int 0x80 is 2 bytes. We saved this in tf_err.
1438 frame.tf_eip -= frame.tf_err;
1443 panic("Unexpected EASYNC return value (for now)");
1446 if (p->p_sysent->sv_errsize) {
1447 if (error >= p->p_sysent->sv_errsize)
1448 error = -1; /* XXX */
1450 error = p->p_sysent->sv_errtbl[error];
1452 frame.tf_eax = error;
1453 frame.tf_eflags |= PSL_C;
1458 * Traced syscall. trapsignal() is not MP aware.
1460 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1461 MAKEMPSAFE(have_mplock);
1462 frame.tf_eflags &= ~PSL_T;
1463 trapsignal(p, SIGTRAP, 0);
1467 * Handle reschedule and other end-of-syscall issues
1469 userret(lp, &frame, sticks);
1472 if (KTRPOINT(td, KTR_SYSRET)) {
1473 MAKEMPSAFE(have_mplock);
1474 ktrsysret(p->p_tracep, code, error, args.sysmsg_result);
1479 * This works because errno is findable through the
1480 * register set. If we ever support an emulation where this
1481 * is not the case, this code will need to be revisited.
1483 STOPEVENT(p, S_SCX, code);
1488 * Release the MP lock if we had to get it
1490 KASSERT(td->td_mpcount == have_mplock,
1491 ("badmpcount syscall2/end from %p", (void *)frame.tf_eip));
1498 * free_sysun - Put an unused sysun on the free list.
1500 static __inline void
1501 free_sysun(struct thread *td, union sysunion *sysun)
1503 struct globaldata *gd = td->td_gd;
1505 crit_enter_quick(td);
1506 sysun->lmsg.opaque.ms_sysunnext = gd->gd_freesysun;
1507 gd->gd_freesysun = sysun;
1508 crit_exit_quick(td);
1512 * sendsys2 - MP aware system message request C handler
1515 sendsys2(struct trapframe frame)
1517 struct globaldata *gd;
1518 struct thread *td = curthread;
1519 struct proc *p = td->td_proc;
1520 struct lwp *lp = td->td_lwp;
1521 register_t orig_tf_eflags;
1522 struct sysent *callp;
1523 union sysunion *sysun = NULL;
1526 int have_mplock = 0;
1536 if (ISPL(frame.tf_cs) != SEL_UPL) {
1544 KASSERT(td->td_mpcount == 0,
1545 ("badmpcount sendsys2 from %p", (void *)frame.tf_eip));
1546 if (syscall_mpsafe == 0)
1547 MAKEMPSAFE(have_mplock);
1550 * access non-atomic field from critical section. p_sticks is
1551 * updated by the clock interrupt. Also use this opportunity
1552 * to lazy-raise our LWKT priority.
1555 sticks = td->td_sticks;
1557 lp->lwp_md.md_regs = &frame;
1558 orig_tf_eflags = frame.tf_eflags;
1562 * Extract the system call message. If msgsize is zero we are
1563 * blocking on a message and/or message port. If msgsize is -1
1564 * we are testing a message for completion or a message port for
1567 * The userland system call message size includes the size of the
1568 * userland lwkt_msg plus arguments. We load it into the userland
1569 * portion of our sysunion structure then we initialize the kerneland
1576 if ((msgsize = frame.tf_edx) < sizeof(struct lwkt_msg) ||
1577 msgsize > sizeof(union sysunion) - sizeof(struct sysmsg)) {
1583 * Obtain a sysun from our per-cpu cache or allocate a new one. Use
1584 * the opaque field to store the original (user) message pointer.
1585 * A critical section is necessary to interlock against interrupts
1586 * returning system messages to the thread cache.
1589 crit_enter_quick(td);
1590 if ((sysun = gd->gd_freesysun) != NULL)
1591 gd->gd_freesysun = sysun->lmsg.opaque.ms_sysunnext;
1593 sysun = malloc(sizeof(union sysunion), M_SYSMSG, M_WAITOK);
1594 crit_exit_quick(td);
1597 * Copy the user request into the kernel copy of the user request.
1599 umsg = (void *)frame.tf_ecx;
1600 error = copyin(umsg, &sysun->nosys.usrmsg, msgsize);
1603 if ((sysun->nosys.usrmsg.umsg.ms_flags & MSGF_ASYNC)) {
1608 if (max_sysmsg > 0 && lp->lwp_nsysmsg >= max_sysmsg) {
1615 * Initialize the kernel message from the copied-in data and
1616 * pull in appropriate flags from the userland message.
1618 * ms_abort_port is usually initialized in sendmsg/domsg, but since
1619 * we are not calling those functions (yet), we have to do it manually.
1621 lwkt_initmsg(&sysun->lmsg, &td->td_msgport, 0,
1622 sysun->nosys.usrmsg.umsg.ms_cmd,
1624 sysun->lmsg.ms_abort_port = sysun->lmsg.ms_reply_port;
1625 sysun->sysmsg_copyout = NULL;
1626 sysun->lmsg.opaque.ms_umsg = umsg;
1627 sysun->lmsg.ms_flags |= sysun->nosys.usrmsg.umsg.ms_flags & MSGF_ASYNC;
1630 * Extract the system call number, lookup the system call, and
1631 * set the default return value.
1633 code = (u_int)sysun->lmsg.ms_cmd.cm_op;
1634 /* We don't handle the syscall() syscall yet */
1637 free_sysun(td, sysun);
1640 if (code >= p->p_sysent->sv_size) {
1642 free_sysun(td, sysun);
1646 callp = &p->p_sysent->sv_table[code];
1648 narg = (msgsize - sizeof(struct lwkt_msg)) / sizeof(register_t);
1651 if (KTRPOINT(td, KTR_SYSCALL)) {
1652 MAKEMPSAFE(have_mplock);
1653 ktrsyscall(p->p_tracep, code, narg, (void *)(&sysun->nosys.usrmsg + 1));
1656 sysun->lmsg.u.ms_fds[0] = 0;
1657 sysun->lmsg.u.ms_fds[1] = 0;
1659 STOPEVENT(p, S_SCE, narg); /* MP aware */
1662 * Make the system call. An error code is always returned, results
1663 * are copied back via ms_result32 or ms_result64. YYY temporary
1664 * stage copy p_retval[] into ms_result32/64
1666 * NOTE! XXX if this is a child returning from a fork curproc
1667 * might be different. YYY huh? a child returning from a fork
1668 * should never 'return' from this call, it should go right to the
1669 * fork_trampoline function.
1671 * Obtain the MP lock if necessary.
1674 if ((callp->sy_narg & SYF_MPSAFE) == 0)
1675 MAKEMPSAFE(have_mplock);
1677 error = (*callp->sy_call)(sysun);
1678 gd = td->td_gd; /* RELOAD, might have switched cpus */
1682 * If a synchronous return copy p_retval to ms_result64 and return
1683 * the sysmsg to the free pool.
1685 * YYY Don't writeback message if execve() YYY
1687 sysun->nosys.usrmsg.umsg.ms_error = error;
1688 sysun->nosys.usrmsg.umsg.u.ms_fds[0] = sysun->lmsg.u.ms_fds[0];
1689 sysun->nosys.usrmsg.umsg.u.ms_fds[1] = sysun->lmsg.u.ms_fds[1];
1690 result = sysun->nosys.usrmsg.umsg.u.ms_fds[0]; /* for ktrace */
1691 if (error != 0 || code != SYS_execve) {
1693 error2 = copyout(&sysun->nosys.usrmsg.umsg.ms_copyout_start,
1694 &umsg->ms_copyout_start,
1699 if (error == EASYNC) {
1701 * Since only the current process ever messes with msgq,
1702 * we can safely manipulate it in parallel with the async
1705 TAILQ_INSERT_TAIL(&lp->lwp_sysmsgq, &sysun->sysmsg, msgq);
1707 error = (int)&sysun->sysmsg;
1709 free_sysun(td, sysun);
1712 frame.tf_eax = (register_t)error;
1715 * Traced syscall. trapsignal() is not MP aware.
1717 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1718 MAKEMPSAFE(have_mplock);
1719 frame.tf_eflags &= ~PSL_T;
1720 trapsignal(p, SIGTRAP, 0);
1724 * Handle reschedule and other end-of-syscall issues
1726 userret(lp, &frame, sticks);
1729 if (KTRPOINT(td, KTR_SYSRET)) {
1730 MAKEMPSAFE(have_mplock);
1731 ktrsysret(p->p_tracep, code, error, result);
1736 * This works because errno is findable through the
1737 * register set. If we ever support an emulation where this
1738 * is not the case, this code will need to be revisited.
1740 STOPEVENT(p, S_SCX, code);
1745 * Release the MP lock if we had to get it
1747 KASSERT(td->td_mpcount == have_mplock,
1748 ("badmpcount sendsys2/end from %p", (void *)frame.tf_eip));
1755 * waitsys2 - MP aware system message wait C handler
1758 waitsys2(struct trapframe frame)
1760 struct globaldata *gd;
1761 struct thread *td = curthread;
1762 struct proc *p = td->td_proc;
1763 struct lwp *lp = td->td_lwp;
1764 union sysunion *sysun = NULL;
1766 register_t orig_tf_eflags;
1767 int error = 0, result, sticks;
1769 int have_mplock = 0;
1774 if (ISPL(frame.tf_cs) != SEL_UPL) {
1782 KASSERT(td->td_mpcount == 0, ("badmpcount waitsys from %p",
1783 (void *)frame.tf_eip));
1784 if (syscall_mpsafe == 0)
1785 MAKEMPSAFE(have_mplock);
1789 * access non-atomic field from critical section. p_sticks is
1790 * updated by the clock interrupt. Also use this opportunity
1791 * to lazy-raise our LWKT priority.
1794 sticks = td->td_sticks;
1796 lp->lwp_md.md_regs = &frame;
1797 orig_tf_eflags = frame.tf_eflags;
1803 TAILQ_FOREACH(ptr, &lp->lwp_sysmsgq, msgq) {
1804 if ((void *)ptr == (void *)frame.tf_ecx) {
1805 sysun = (void *)sysmsg_wait(lp,
1806 (void *)frame.tf_ecx, 1);
1816 else if (frame.tf_eax) {
1817 printf("waitport/checkport only the default port is supported at the moment\n");
1822 switch(frame.tf_edx) {
1824 sysun = (void *)sysmsg_wait(lp, NULL, 0);
1827 sysun = (void *)sysmsg_wait(lp, NULL, 1);
1836 umsg = sysun->lmsg.opaque.ms_umsg;
1837 frame.tf_eax = (register_t)sysun;
1838 sysun->nosys.usrmsg.umsg.u.ms_fds[0] = sysun->lmsg.u.ms_fds[0];
1839 sysun->nosys.usrmsg.umsg.u.ms_fds[1] = sysun->lmsg.u.ms_fds[1];
1840 sysun->nosys.usrmsg.umsg.ms_error = sysun->lmsg.ms_error;
1841 error = sysun->lmsg.ms_error;
1842 result = sysun->lmsg.u.ms_fds[0]; /* for ktrace */
1843 error = copyout(&sysun->nosys.usrmsg.umsg.ms_copyout_start,
1844 &umsg->ms_copyout_start, ms_copyout_size);
1845 free_sysun(td, sysun);
1847 code = (u_int)sysun->lmsg.ms_cmd.cm_op;
1851 frame.tf_eax = error;
1853 * Traced syscall. trapsignal() is not MP aware.
1855 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1856 MAKEMPSAFE(have_mplock);
1857 frame.tf_eflags &= ~PSL_T;
1858 trapsignal(p, SIGTRAP, 0);
1862 * Handle reschedule and other end-of-syscall issues
1864 userret(lp, &frame, sticks);
1867 if (KTRPOINT(td, KTR_SYSRET)) {
1868 MAKEMPSAFE(have_mplock);
1869 ktrsysret(p->p_tracep, code, error, result);
1874 * This works because errno is findable through the
1875 * register set. If we ever support an emulation where this
1876 * is not the case, this code will need to be revisited.
1878 STOPEVENT(p, S_SCX, code);
1882 KASSERT(td->td_mpcount == 1, ("badmpcount waitsys/end from %p",
1883 (void *)frame.tf_eip));
1890 * Simplified back end of syscall(), used when returning from fork()
1891 * directly into user mode. MP lock is held on entry and should be
1892 * released on return. This code will return back into the fork
1893 * trampoline code which then runs doreti.
1896 fork_return(p, frame)
1898 struct trapframe frame;
1902 KKASSERT(p->p_nthreads == 1);
1904 lp = LIST_FIRST(&p->p_lwps);
1906 frame.tf_eax = 0; /* Child returns zero */
1907 frame.tf_eflags &= ~PSL_C; /* success */
1911 * Newly forked processes are given a kernel priority. We have to
1912 * adjust the priority to a normal user priority and fake entry
1913 * into the kernel (call userenter()) to install a passive release
1914 * function just in case userret() decides to stop the process. This
1915 * can occur when ^Z races a fork. If we do not install the passive
1916 * release function the current process designation will not be
1917 * released when the thread goes to sleep.
1919 lwkt_setpri_self(TDPRI_USER_NORM);
1920 userenter(lp->lwp_thread);
1921 userret(lp, &frame, 0);
1923 if (KTRPOINT(lp->lwp_thread, KTR_SYSRET))
1924 ktrsysret(p->p_tracep, SYS_fork, 0, 0);
1926 p->p_flag |= P_PASSIVE_ACQ;
1928 p->p_flag &= ~P_PASSIVE_ACQ;
1930 KKASSERT(lp->lwp_thread->td_mpcount == 1);