| Commit | Line | Data |
|---|---|---|
| 6f7b98e0 MD |
1 | /*- |
| 2 | * Copyright (C) 1994, David Greenman | |
| 3 | * Copyright (c) 1990, 1993 | |
| 4 | * The Regents of the University of California. All rights reserved. | |
| 5 | * | |
| 6 | * This code is derived from software contributed to Berkeley by | |
| 7 | * the University of Utah, and William Jolitz. | |
| 8 | * | |
| 9 | * Redistribution and use in source and binary forms, with or without | |
| 10 | * modification, are permitted provided that the following conditions | |
| 11 | * are met: | |
| 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. | |
| 24 | * | |
| 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 | |
| 35 | * SUCH DAMAGE. | |
| 36 | * | |
| 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 $ | |
| 6f7b98e0 MD |
39 | */ |
| 40 | ||
| 41 | /* | |
| 42 | * 386 Trap and System call handling | |
| 43 | */ | |
| 44 | ||
| 45 | #include "use_isa.h" | |
| 46 | #include "use_npx.h" | |
| 47 | ||
| 48 | #include "opt_ddb.h" | |
| 49 | #include "opt_ktrace.h" | |
| 50 | ||
| 51 | #include <sys/param.h> | |
| 52 | #include <sys/systm.h> | |
| 53 | #include <sys/proc.h> | |
| 54 | #include <sys/pioctl.h> | |
| 55 | #include <sys/kernel.h> | |
| 56 | #include <sys/resourcevar.h> | |
| 57 | #include <sys/signalvar.h> | |
| b1b4e5a6 | 58 | #include <sys/signal2.h> |
| 6f7b98e0 MD |
59 | #include <sys/syscall.h> |
| 60 | #include <sys/sysctl.h> | |
| 61 | #include <sys/sysent.h> | |
| 62 | #include <sys/uio.h> | |
| 63 | #include <sys/vmmeter.h> | |
| 64 | #include <sys/malloc.h> | |
| 65 | #ifdef KTRACE | |
| 66 | #include <sys/ktrace.h> | |
| 67 | #endif | |
| 80bbc910 | 68 | #include <sys/ktr.h> |
| 6f7b98e0 MD |
69 | #include <sys/vkernel.h> |
| 70 | #include <sys/sysproto.h> | |
| 71 | #include <sys/sysunion.h> | |
| 135d7199 | 72 | #include <sys/vmspace.h> |
| 6f7b98e0 MD |
73 | |
| 74 | #include <vm/vm.h> | |
| 75 | #include <vm/vm_param.h> | |
| 76 | #include <sys/lock.h> | |
| 77 | #include <vm/pmap.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> | |
| 82 | ||
| 83 | #include <machine/cpu.h> | |
| 84 | #include <machine/md_var.h> | |
| 85 | #include <machine/pcb.h> | |
| 86 | #include <machine/smp.h> | |
| 87 | #include <machine/tss.h> | |
| 88 | #include <machine/globaldata.h> | |
| 89 | ||
| 90 | #include <machine/vm86.h> | |
| 91 | ||
| 92 | #include <ddb/ddb.h> | |
| 684a93c4 | 93 | |
| 6f7b98e0 MD |
94 | #include <sys/msgport2.h> |
| 95 | #include <sys/thread2.h> | |
| 684a93c4 | 96 | #include <sys/mplock2.h> |
| 6f7b98e0 | 97 | |
| 6f7b98e0 MD |
98 | #define MAKEMPSAFE(have_mplock) \ |
| 99 | if (have_mplock == 0) { \ | |
| 100 | get_mplock(); \ | |
| 101 | have_mplock = 1; \ | |
| 102 | } | |
| 103 | ||
| 6f7b98e0 MD |
104 | int (*pmath_emulate) (struct trapframe *); |
| 105 | ||
| 6f7b98e0 | 106 | static int trap_pfault (struct trapframe *, int, vm_offset_t); |
| 6092278a | 107 | static void trap_fatal (struct trapframe *, int, vm_offset_t); |
| 6f7b98e0 MD |
108 | void dblfault_handler (void); |
| 109 | ||
| 110 | #if 0 | |
| 111 | extern inthand_t IDTVEC(syscall); | |
| 112 | #endif | |
| 113 | ||
| 114 | #define MAX_TRAP_MSG 28 | |
| 115 | static char *trap_msg[] = { | |
| 116 | "", /* 0 unused */ | |
| 117 | "privileged instruction fault", /* 1 T_PRIVINFLT */ | |
| 118 | "", /* 2 unused */ | |
| 119 | "breakpoint instruction fault", /* 3 T_BPTFLT */ | |
| 120 | "", /* 4 unused */ | |
| 121 | "", /* 5 unused */ | |
| 122 | "arithmetic trap", /* 6 T_ARITHTRAP */ | |
| 123 | "system forced exception", /* 7 T_ASTFLT */ | |
| 124 | "", /* 8 unused */ | |
| 125 | "general protection fault", /* 9 T_PROTFLT */ | |
| 126 | "trace trap", /* 10 T_TRCTRAP */ | |
| 127 | "", /* 11 unused */ | |
| 128 | "page fault", /* 12 T_PAGEFLT */ | |
| 129 | "", /* 13 unused */ | |
| 130 | "alignment fault", /* 14 T_ALIGNFLT */ | |
| 131 | "", /* 15 unused */ | |
| 132 | "", /* 16 unused */ | |
| 133 | "", /* 17 unused */ | |
| 134 | "integer divide fault", /* 18 T_DIVIDE */ | |
| 135 | "non-maskable interrupt trap", /* 19 T_NMI */ | |
| 136 | "overflow trap", /* 20 T_OFLOW */ | |
| 137 | "FPU bounds check fault", /* 21 T_BOUND */ | |
| 138 | "FPU device not available", /* 22 T_DNA */ | |
| 139 | "double fault", /* 23 T_DOUBLEFLT */ | |
| 140 | "FPU operand fetch fault", /* 24 T_FPOPFLT */ | |
| 141 | "invalid TSS fault", /* 25 T_TSSFLT */ | |
| 142 | "segment not present fault", /* 26 T_SEGNPFLT */ | |
| 143 | "stack fault", /* 27 T_STKFLT */ | |
| 144 | "machine check trap", /* 28 T_MCHK */ | |
| 145 | }; | |
| 146 | ||
| 147 | #ifdef DDB | |
| 148 | static int ddb_on_nmi = 1; | |
| 149 | SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW, | |
| 150 | &ddb_on_nmi, 0, "Go to DDB on NMI"); | |
| 151 | #endif | |
| 152 | static int panic_on_nmi = 1; | |
| 153 | SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW, | |
| 154 | &panic_on_nmi, 0, "Panic on NMI"); | |
| 155 | static int fast_release; | |
| 156 | SYSCTL_INT(_machdep, OID_AUTO, fast_release, CTLFLAG_RW, | |
| 157 | &fast_release, 0, "Passive Release was optimal"); | |
| 158 | static int slow_release; | |
| 159 | SYSCTL_INT(_machdep, OID_AUTO, slow_release, CTLFLAG_RW, | |
| 160 | &slow_release, 0, "Passive Release was nonoptimal"); | |
| 6f7b98e0 MD |
161 | |
| 162 | MALLOC_DEFINE(M_SYSMSG, "sysmsg", "sysmsg structure"); | |
| 163 | extern int max_sysmsg; | |
| 164 | ||
| 165 | /* | |
| bab69519 JM |
166 | * Passively intercepts the thread switch function to increase the thread |
| 167 | * priority from a user priority to a kernel priority, reducing | |
| 6f7b98e0 | 168 | * syscall and trap overhead for the case where no switch occurs. |
| 7bd858e5 | 169 | * |
| d86a23e0 | 170 | * Synchronizes td_ucred with p_ucred. This is used by system calls, |
| 7bd858e5 MD |
171 | * signal handling, faults, AST traps, and anything else that enters the |
| 172 | * kernel from userland and provides the kernel with a stable read-only | |
| 173 | * copy of the process ucred. | |
| 6f7b98e0 | 174 | */ |
| 6f7b98e0 | 175 | static __inline void |
| d86a23e0 | 176 | userenter(struct thread *curtd, struct proc *curp) |
| 6f7b98e0 | 177 | { |
| 7bd858e5 MD |
178 | struct ucred *ocred; |
| 179 | struct ucred *ncred; | |
| 180 | ||
| 3824f392 | 181 | curtd->td_release = lwkt_passive_release; |
| 7bd858e5 | 182 | |
| d86a23e0 MD |
183 | if (curtd->td_ucred != curp->p_ucred) { |
| 184 | ncred = crhold(curp->p_ucred); | |
| 185 | ocred = curtd->td_ucred; | |
| 186 | curtd->td_ucred = ncred; | |
| 7bd858e5 MD |
187 | if (ocred) |
| 188 | crfree(ocred); | |
| 189 | } | |
| bab69519 | 190 | |
| 6f7b98e0 MD |
191 | } |
| 192 | ||
| 193 | /* | |
| 7adb15b6 | 194 | * Handle signals, profiling, and other AST's and/or tasks that |
| 6f7b98e0 MD |
195 | * must be completed before we can return to or try to return to userland. |
| 196 | * | |
| 197 | * Note that td_sticks is a 64 bit quantity, but there's no point doing 64 | |
| 198 | * arithmatic on the delta calculation so the absolute tick values are | |
| 199 | * truncated to an integer. | |
| 200 | */ | |
| 201 | static void | |
| 202 | userret(struct lwp *lp, struct trapframe *frame, int sticks) | |
| 203 | { | |
| 204 | struct proc *p = lp->lwp_proc; | |
| 205 | int sig; | |
| 206 | ||
| 207 | /* | |
| 208 | * Charge system time if profiling. Note: times are in microseconds. | |
| 209 | * This may do a copyout and block, so do it first even though it | |
| 210 | * means some system time will be charged as user time. | |
| 211 | */ | |
| 4643740a | 212 | if (p->p_flags & P_PROFIL) { |
| 6f7b98e0 MD |
213 | addupc_task(p, frame->tf_eip, |
| 214 | (u_int)((int)lp->lwp_thread->td_sticks - sticks)); | |
| 215 | } | |
| 216 | ||
| 217 | recheck: | |
| 218 | /* | |
| 62ae46c9 MD |
219 | * Specific on-return-to-usermode checks (LWP_MP_WEXIT, |
| 220 | * LWP_MP_VNLRU, etc). | |
| 70d3d461 | 221 | */ |
| 62ae46c9 MD |
222 | if (lp->lwp_mpflags & LWP_MP_URETMASK) |
| 223 | lwpuserret(lp); | |
| 70d3d461 SS |
224 | |
| 225 | /* | |
| 6f7b98e0 MD |
226 | * Block here if we are in a stopped state. |
| 227 | */ | |
| 164b8401 | 228 | if (p->p_stat == SSTOP) { |
| 4643740a | 229 | lwkt_gettoken(&p->p_token); |
| 9a379a4a | 230 | tstop(); |
| 4643740a | 231 | lwkt_reltoken(&p->p_token); |
| 6f7b98e0 MD |
232 | goto recheck; |
| 233 | } | |
| 234 | ||
| 235 | /* | |
| 898e34b3 MD |
236 | * Post any pending upcalls. If running a virtual kernel be sure |
| 237 | * to restore the virtual kernel's vmspace before posting the upcall. | |
| 6f7b98e0 | 238 | */ |
| 7adb15b6 | 239 | if (p->p_flags & (P_SIGVTALRM | P_SIGPROF)) { |
| fc509460 | 240 | lwkt_gettoken(&p->p_token); |
| 4643740a MD |
241 | if (p->p_flags & P_SIGVTALRM) { |
| 242 | p->p_flags &= ~P_SIGVTALRM; | |
| 898e34b3 MD |
243 | ksignal(p, SIGVTALRM); |
| 244 | } | |
| 4643740a MD |
245 | if (p->p_flags & P_SIGPROF) { |
| 246 | p->p_flags &= ~P_SIGPROF; | |
| 898e34b3 MD |
247 | ksignal(p, SIGPROF); |
| 248 | } | |
| fc509460 | 249 | lwkt_reltoken(&p->p_token); |
| 6f7b98e0 MD |
250 | goto recheck; |
| 251 | } | |
| 252 | ||
| 253 | /* | |
| 254 | * Post any pending signals | |
| 2883d2d8 MD |
255 | * |
| 256 | * WARNING! postsig() can exit and not return. | |
| 6f7b98e0 | 257 | */ |
| e473f776 | 258 | if ((sig = CURSIG_TRACE(lp)) != 0) { |
| fc509460 | 259 | lwkt_gettoken(&p->p_token); |
| 6f7b98e0 | 260 | postsig(sig); |
| fc509460 | 261 | lwkt_reltoken(&p->p_token); |
| 6f7b98e0 MD |
262 | goto recheck; |
| 263 | } | |
| 264 | ||
| 265 | /* | |
| 266 | * block here if we are swapped out, but still process signals | |
| 267 | * (such as SIGKILL). proc0 (the swapin scheduler) is already | |
| 268 | * aware of our situation, we do not have to wake it up. | |
| 269 | */ | |
| 4643740a | 270 | if (p->p_flags & P_SWAPPEDOUT) { |
| 616516c8 | 271 | lwkt_gettoken(&p->p_token); |
| 6f7b98e0 | 272 | get_mplock(); |
| 4643740a | 273 | p->p_flags |= P_SWAPWAIT; |
| 6f7b98e0 | 274 | swapin_request(); |
| 4643740a | 275 | if (p->p_flags & P_SWAPWAIT) |
| 6f7b98e0 | 276 | tsleep(p, PCATCH, "SWOUT", 0); |
| 4643740a | 277 | p->p_flags &= ~P_SWAPWAIT; |
| 6f7b98e0 | 278 | rel_mplock(); |
| 616516c8 | 279 | lwkt_reltoken(&p->p_token); |
| 6f7b98e0 MD |
280 | goto recheck; |
| 281 | } | |
| ae7cb1b5 NT |
282 | |
| 283 | /* | |
| 6562e2d8 MD |
284 | * In a multi-threaded program it is possible for a thread to change |
| 285 | * signal state during a system call which temporarily changes the | |
| 286 | * signal mask. In this case postsig() might not be run and we | |
| 287 | * have to restore the mask ourselves. | |
| ae7cb1b5 | 288 | */ |
| 6562e2d8 MD |
289 | if (lp->lwp_flags & LWP_OLDMASK) { |
| 290 | lp->lwp_flags &= ~LWP_OLDMASK; | |
| 291 | lp->lwp_sigmask = lp->lwp_oldsigmask; | |
| 292 | goto recheck; | |
| 293 | } | |
| 6f7b98e0 MD |
294 | } |
| 295 | ||
| 296 | /* | |
| 297 | * Cleanup from userenter and any passive release that might have occured. | |
| 298 | * We must reclaim the current-process designation before we can return | |
| 299 | * to usermode. We also handle both LWKT and USER reschedule requests. | |
| 300 | */ | |
| 301 | static __inline void | |
| 302 | userexit(struct lwp *lp) | |
| 303 | { | |
| 304 | struct thread *td = lp->lwp_thread; | |
| f5df782c | 305 | /* globaldata_t gd = td->td_gd; */ |
| 6f7b98e0 | 306 | |
| 6f7b98e0 | 307 | /* |
| b9eb1c19 MD |
308 | * Handle stop requests at kernel priority. Any requests queued |
| 309 | * after this loop will generate another AST. | |
| 6f7b98e0 | 310 | */ |
| b9eb1c19 | 311 | while (lp->lwp_proc->p_stat == SSTOP) { |
| 4643740a | 312 | lwkt_gettoken(&lp->lwp_proc->p_token); |
| b9eb1c19 | 313 | tstop(); |
| 4643740a | 314 | lwkt_reltoken(&lp->lwp_proc->p_token); |
| fede0c7f NT |
315 | } |
| 316 | ||
| 6f7b98e0 | 317 | /* |
| b9eb1c19 MD |
318 | * Become the current user scheduled process if we aren't already, |
| 319 | * and deal with reschedule requests and other factors. | |
| 320 | */ | |
| 321 | lp->lwp_proc->p_usched->acquire_curproc(lp); | |
| 322 | /* WARNING: we may have migrated cpu's */ | |
| 323 | /* gd = td->td_gd; */ | |
| e3e6be1f MD |
324 | |
| 325 | /* | |
| 326 | * Reduce our priority in preparation for a return to userland. If | |
| 327 | * our passive release function was still in place, our priority was | |
| 328 | * never raised and does not need to be reduced. | |
| 329 | */ | |
| 330 | lwkt_passive_recover(td); | |
| b9eb1c19 | 331 | } |
| 80bbc910 SS |
332 | |
| 333 | #if !defined(KTR_KERNENTRY) | |
| 334 | #define KTR_KERNENTRY KTR_ALL | |
| 335 | #endif | |
| 336 | KTR_INFO_MASTER(kernentry); | |
| cacf1050 VS |
337 | KTR_INFO(KTR_KERNENTRY, kernentry, trap, 0, |
| 338 | "TRAP(pid %d, tid %d, trapno %d, eva %lu)", | |
| 339 | pid_t pid, lwpid_t tid, register_t trapno, vm_offset_t eva); | |
| 340 | KTR_INFO(KTR_KERNENTRY, kernentry, trap_ret, 0, "TRAP_RET(pid %d, tid %d)", | |
| 341 | pid_t pid, lwpid_t tid); | |
| 342 | KTR_INFO(KTR_KERNENTRY, kernentry, syscall, 0, "SYSC(pid %d, tid %d, nr %d)", | |
| 343 | pid_t pid, lwpid_t tid, register_t trapno); | |
| 344 | KTR_INFO(KTR_KERNENTRY, kernentry, syscall_ret, 0, "SYSRET(pid %d, tid %d, err %d)", | |
| 345 | pid_t pid, lwpid_t tid, int err); | |
| 346 | KTR_INFO(KTR_KERNENTRY, kernentry, fork_ret, 0, "FORKRET(pid %d, tid %d)", | |
| 347 | pid_t pid, lwpid_t tid); | |
| 80bbc910 | 348 | |
| 6f7b98e0 MD |
349 | /* |
| 350 | * Exception, fault, and trap interface to the kernel. | |
| 351 | * This common code is called from assembly language IDT gate entry | |
| 352 | * routines that prepare a suitable stack frame, and restore this | |
| 353 | * frame after the exception has been processed. | |
| 354 | * | |
| 355 | * This function is also called from doreti in an interlock to handle ASTs. | |
| 356 | * For example: hardwareint->INTROUTINE->(set ast)->doreti->trap | |
| 357 | * | |
| 358 | * NOTE! We have to retrieve the fault address prior to obtaining the | |
| 359 | * MP lock because get_mplock() may switch out. YYY cr2 really ought | |
| 360 | * to be retrieved by the assembly code, not here. | |
| 361 | * | |
| 362 | * XXX gd_trap_nesting_level currently prevents lwkt_switch() from panicing | |
| 363 | * if an attempt is made to switch from a fast interrupt or IPI. This is | |
| 364 | * necessary to properly take fatal kernel traps on SMP machines if | |
| 365 | * get_mplock() has to block. | |
| 366 | */ | |
| 367 | ||
| 368 | void | |
| 6092278a | 369 | user_trap(struct trapframe *frame) |
| 6f7b98e0 MD |
370 | { |
| 371 | struct globaldata *gd = mycpu; | |
| 372 | struct thread *td = gd->gd_curthread; | |
| 373 | struct lwp *lp = td->td_lwp; | |
| 374 | struct proc *p; | |
| 375 | int sticks = 0; | |
| 376 | int i = 0, ucode = 0, type, code; | |
| 6f7b98e0 | 377 | int have_mplock = 0; |
| 6f7b98e0 | 378 | #ifdef INVARIANTS |
| f9235b6d | 379 | int crit_count = td->td_critcount; |
| 3933a3ab | 380 | lwkt_tokref_t curstop = td->td_toks_stop; |
| 6f7b98e0 MD |
381 | #endif |
| 382 | vm_offset_t eva; | |
| 383 | ||
| 384 | p = td->td_proc; | |
| 135d7199 MD |
385 | |
| 386 | /* | |
| 387 | * This is a bad kludge to avoid changing the various trapframe | |
| 388 | * structures. Because we are enabled as a virtual kernel, | |
| 389 | * the original tf_err field will be passed to us shifted 16 | |
| 390 | * over in the tf_trapno field for T_PAGEFLT. | |
| 391 | */ | |
| 4e7c41c5 | 392 | if (frame->tf_trapno == T_PAGEFLT) |
| 135d7199 | 393 | eva = frame->tf_err; |
| 4e7c41c5 | 394 | else |
| 135d7199 | 395 | eva = 0; |
| 4e7c41c5 MD |
396 | #if 0 |
| 397 | kprintf("USER_TRAP AT %08x xflags %d trapno %d eva %08x\n", | |
| 398 | frame->tf_eip, frame->tf_xflags, frame->tf_trapno, eva); | |
| 399 | #endif | |
| 135d7199 MD |
400 | |
| 401 | /* | |
| 402 | * Everything coming from user mode runs through user_trap, | |
| 403 | * including system calls. | |
| 404 | */ | |
| 405 | if (frame->tf_trapno == T_SYSCALL80) { | |
| 406 | syscall2(frame); | |
| 407 | return; | |
| 408 | } | |
| 409 | ||
| 80bbc910 SS |
410 | KTR_LOG(kernentry_trap, lp->lwp_proc->p_pid, lp->lwp_tid, |
| 411 | frame->tf_trapno, eva); | |
| 412 | ||
| 6f7b98e0 MD |
413 | #ifdef DDB |
| 414 | if (db_active) { | |
| 6092278a | 415 | eva = (frame->tf_trapno == T_PAGEFLT ? rcr2() : 0); |
| 6f7b98e0 MD |
416 | ++gd->gd_trap_nesting_level; |
| 417 | MAKEMPSAFE(have_mplock); | |
| 6092278a | 418 | trap_fatal(frame, TRUE, eva); |
| 6f7b98e0 MD |
419 | --gd->gd_trap_nesting_level; |
| 420 | goto out2; | |
| 421 | } | |
| 422 | #endif | |
| 423 | ||
| 6f7b98e0 MD |
424 | #if defined(I586_CPU) && !defined(NO_F00F_HACK) |
| 425 | restart: | |
| 426 | #endif | |
| 6092278a MD |
427 | type = frame->tf_trapno; |
| 428 | code = frame->tf_err; | |
| 6f7b98e0 | 429 | |
| d86a23e0 | 430 | userenter(td, p); |
| 6f7b98e0 | 431 | |
| 6092278a MD |
432 | sticks = (int)td->td_sticks; |
| 433 | lp->lwp_md.md_regs = frame; | |
| 6f7b98e0 | 434 | |
| 6092278a MD |
435 | switch (type) { |
| 436 | case T_PRIVINFLT: /* privileged instruction fault */ | |
| 6092278a | 437 | i = SIGILL; |
| bab69519 | 438 | ucode = ILL_PRVOPC; |
| 6092278a | 439 | break; |
| 6f7b98e0 | 440 | |
| 6092278a MD |
441 | case T_BPTFLT: /* bpt instruction fault */ |
| 442 | case T_TRCTRAP: /* trace trap */ | |
| 443 | frame->tf_eflags &= ~PSL_T; | |
| 444 | i = SIGTRAP; | |
| bab69519 | 445 | ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT); |
| 6092278a | 446 | break; |
| 6f7b98e0 | 447 | |
| 6092278a MD |
448 | case T_ARITHTRAP: /* arithmetic trap */ |
| 449 | ucode = code; | |
| 450 | i = SIGFPE; | |
| 451 | break; | |
| 6f7b98e0 | 452 | |
| 6092278a MD |
453 | case T_ASTFLT: /* Allow process switch */ |
| 454 | mycpu->gd_cnt.v_soft++; | |
| 455 | if (mycpu->gd_reqflags & RQF_AST_OWEUPC) { | |
| 2a418930 | 456 | atomic_clear_int(&mycpu->gd_reqflags, |
| 6092278a MD |
457 | RQF_AST_OWEUPC); |
| 458 | addupc_task(p, p->p_prof.pr_addr, | |
| 459 | p->p_prof.pr_ticks); | |
| 460 | } | |
| 461 | goto out; | |
| 6f7b98e0 | 462 | |
| 6092278a MD |
463 | /* |
| 464 | * The following two traps can happen in | |
| 465 | * vm86 mode, and, if so, we want to handle | |
| 466 | * them specially. | |
| 467 | */ | |
| 468 | case T_PROTFLT: /* general protection fault */ | |
| 469 | case T_STKFLT: /* stack fault */ | |
| 6f7b98e0 | 470 | #if 0 |
| 6092278a MD |
471 | if (frame->tf_eflags & PSL_VM) { |
| 472 | i = vm86_emulate((struct vm86frame *)frame); | |
| 473 | if (i == 0) | |
| 474 | goto out; | |
| 6f7b98e0 | 475 | break; |
| 6092278a MD |
476 | } |
| 477 | #endif | |
| f2642d5a AH |
478 | i = SIGBUS; |
| 479 | ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR; | |
| 480 | break; | |
| 6092278a | 481 | case T_SEGNPFLT: /* segment not present fault */ |
| f2642d5a AH |
482 | i = SIGBUS; |
| 483 | ucode = BUS_ADRERR; | |
| 484 | break; | |
| 6092278a MD |
485 | case T_TSSFLT: /* invalid TSS fault */ |
| 486 | case T_DOUBLEFLT: /* double fault */ | |
| 487 | default: | |
| 6092278a | 488 | i = SIGBUS; |
| bab69519 | 489 | ucode = BUS_OBJERR; |
| 6092278a MD |
490 | break; |
| 491 | ||
| 492 | case T_PAGEFLT: /* page fault */ | |
| 493 | MAKEMPSAFE(have_mplock); | |
| 494 | i = trap_pfault(frame, TRUE, eva); | |
| 495 | if (i == -1) | |
| 496 | goto out; | |
| 6f7b98e0 | 497 | #if defined(I586_CPU) && !defined(NO_F00F_HACK) |
| 6092278a MD |
498 | if (i == -2) |
| 499 | goto restart; | |
| 6f7b98e0 | 500 | #endif |
| 6092278a MD |
501 | if (i == 0) |
| 502 | goto out; | |
| 6f7b98e0 | 503 | |
| f2642d5a AH |
504 | if (i == SIGSEGV) |
| 505 | ucode = SEGV_MAPERR; | |
| bab69519 JM |
506 | else { |
| 507 | i = SIGSEGV; | |
| 508 | ucode = SEGV_ACCERR; | |
| 509 | } | |
| 6092278a | 510 | break; |
| 6f7b98e0 | 511 | |
| 6092278a MD |
512 | case T_DIVIDE: /* integer divide fault */ |
| 513 | ucode = FPE_INTDIV; | |
| 514 | i = SIGFPE; | |
| 515 | break; | |
| 6f7b98e0 MD |
516 | |
| 517 | #if NISA > 0 | |
| 6092278a MD |
518 | case T_NMI: |
| 519 | MAKEMPSAFE(have_mplock); | |
| 520 | /* machine/parity/power fail/"kitchen sink" faults */ | |
| 521 | if (isa_nmi(code) == 0) { | |
| 6f7b98e0 | 522 | #ifdef DDB |
| 6092278a MD |
523 | /* |
| 524 | * NMI can be hooked up to a pushbutton | |
| 525 | * for debugging. | |
| 526 | */ | |
| 527 | if (ddb_on_nmi) { | |
| 528 | kprintf ("NMI ... going to debugger\n"); | |
| 529 | kdb_trap (type, 0, frame); | |
| 530 | } | |
| 6f7b98e0 | 531 | #endif /* DDB */ |
| 6092278a MD |
532 | goto out2; |
| 533 | } else if (panic_on_nmi) | |
| 534 | panic("NMI indicates hardware failure"); | |
| 535 | break; | |
| 6f7b98e0 MD |
536 | #endif /* NISA > 0 */ |
| 537 | ||
| 6092278a MD |
538 | case T_OFLOW: /* integer overflow fault */ |
| 539 | ucode = FPE_INTOVF; | |
| 540 | i = SIGFPE; | |
| 541 | break; | |
| 6f7b98e0 | 542 | |
| 6092278a MD |
543 | case T_BOUND: /* bounds check fault */ |
| 544 | ucode = FPE_FLTSUB; | |
| 545 | i = SIGFPE; | |
| 546 | break; | |
| 6f7b98e0 | 547 | |
| 6092278a | 548 | case T_DNA: |
| 431d0fef MD |
549 | /* |
| 550 | * Virtual kernel intercept - pass the DNA exception | |
| 551 | * to the (emulated) virtual kernel if it asked to handle | |
| 552 | * it. This occurs when the virtual kernel is holding | |
| 553 | * onto the FP context for a different emulated | |
| 554 | * process then the one currently running. | |
| 555 | * | |
| 556 | * We must still call npxdna() since we may have | |
| 557 | * saved FP state that the (emulated) virtual kernel | |
| 558 | * needs to hand over to a different emulated process. | |
| 559 | */ | |
| 39005e16 | 560 | if (lp->lwp_vkernel && lp->lwp_vkernel->ve && |
| 431d0fef MD |
561 | (td->td_pcb->pcb_flags & FP_VIRTFP) |
| 562 | ) { | |
| 563 | npxdna(frame); | |
| 564 | break; | |
| 565 | } | |
| bab69519 | 566 | |
| 6f7b98e0 | 567 | #if NNPX > 0 |
| 6092278a MD |
568 | /* |
| 569 | * The kernel may have switched out the FP unit's | |
| 570 | * state, causing the user process to take a fault | |
| 571 | * when it tries to use the FP unit. Restore the | |
| 572 | * state here | |
| 573 | */ | |
| 431d0fef | 574 | if (npxdna(frame)) |
| 6092278a | 575 | goto out; |
| 6f7b98e0 | 576 | #endif |
| 6092278a | 577 | if (!pmath_emulate) { |
| 6f7b98e0 | 578 | i = SIGFPE; |
| 6092278a | 579 | ucode = FPE_FPU_NP_TRAP; |
| 6f7b98e0 MD |
580 | break; |
| 581 | } | |
| 6092278a MD |
582 | i = (*pmath_emulate)(frame); |
| 583 | if (i == 0) { | |
| 584 | if (!(frame->tf_eflags & PSL_T)) | |
| 6f7b98e0 | 585 | goto out2; |
| 6092278a MD |
586 | frame->tf_eflags &= ~PSL_T; |
| 587 | i = SIGTRAP; | |
| 6f7b98e0 | 588 | } |
| 6092278a MD |
589 | /* else ucode = emulator_only_knows() XXX */ |
| 590 | break; | |
| 6f7b98e0 | 591 | |
| 6092278a | 592 | case T_FPOPFLT: /* FPU operand fetch fault */ |
| f2642d5a | 593 | ucode = ILL_COPROC; |
| 6092278a MD |
594 | i = SIGILL; |
| 595 | break; | |
| 596 | ||
| 597 | case T_XMMFLT: /* SIMD floating-point exception */ | |
| 598 | ucode = 0; /* XXX */ | |
| 599 | i = SIGFPE; | |
| 600 | break; | |
| 6f7b98e0 MD |
601 | } |
| 602 | ||
| 603 | /* | |
| 604 | * Virtual kernel intercept - if the fault is directly related to a | |
| 605 | * VM context managed by a virtual kernel then let the virtual kernel | |
| 606 | * handle it. | |
| 607 | */ | |
| 39005e16 | 608 | if (lp->lwp_vkernel && lp->lwp_vkernel->ve) { |
| 287ebb09 | 609 | vkernel_trap(lp, frame); |
| 6f7b98e0 MD |
610 | goto out; |
| 611 | } | |
| 612 | ||
| 613 | /* | |
| 614 | * Translate fault for emulators (e.g. Linux) | |
| 615 | */ | |
| 616 | if (*p->p_sysent->sv_transtrap) | |
| 617 | i = (*p->p_sysent->sv_transtrap)(i, type); | |
| 618 | ||
| 619 | MAKEMPSAFE(have_mplock); | |
| 08f2f1bb | 620 | trapsignal(lp, i, ucode); |
| 6f7b98e0 MD |
621 | |
| 622 | #ifdef DEBUG | |
| 623 | if (type <= MAX_TRAP_MSG) { | |
| 624 | uprintf("fatal process exception: %s", | |
| 625 | trap_msg[type]); | |
| 626 | if ((type == T_PAGEFLT) || (type == T_PROTFLT)) | |
| 627 | uprintf(", fault VA = 0x%lx", (u_long)eva); | |
| 628 | uprintf("\n"); | |
| 629 | } | |
| 630 | #endif | |
| 631 | ||
| 632 | out: | |
| 6092278a | 633 | userret(lp, frame, sticks); |
| 6f7b98e0 MD |
634 | userexit(lp); |
| 635 | out2: ; | |
| 6f7b98e0 MD |
636 | if (have_mplock) |
| 637 | rel_mplock(); | |
| 80bbc910 | 638 | KTR_LOG(kernentry_trap_ret, lp->lwp_proc->p_pid, lp->lwp_tid); |
| 6f7b98e0 | 639 | #ifdef INVARIANTS |
| f9235b6d | 640 | KASSERT(crit_count == td->td_critcount, |
| 3933a3ab | 641 | ("trap: critical section count mismatch! %d/%d", |
| f9235b6d | 642 | crit_count, td->td_pri)); |
| 3933a3ab MD |
643 | KASSERT(curstop == td->td_toks_stop, |
| 644 | ("trap: extra tokens held after trap! %zd/%zd", | |
| 645 | curstop - &td->td_toks_base, | |
| 646 | td->td_toks_stop - &td->td_toks_base)); | |
| 6f7b98e0 MD |
647 | #endif |
| 648 | } | |
| 649 | ||
| 6092278a MD |
650 | void |
| 651 | kern_trap(struct trapframe *frame) | |
| 6f7b98e0 | 652 | { |
| 6092278a MD |
653 | struct globaldata *gd = mycpu; |
| 654 | struct thread *td = gd->gd_curthread; | |
| 08f2f1bb | 655 | struct lwp *lp; |
| 6092278a MD |
656 | struct proc *p; |
| 657 | int i = 0, ucode = 0, type, code; | |
| 6092278a | 658 | int have_mplock = 0; |
| 6092278a | 659 | #ifdef INVARIANTS |
| f9235b6d | 660 | int crit_count = td->td_critcount; |
| 3933a3ab | 661 | lwkt_tokref_t curstop = td->td_toks_stop; |
| 6092278a MD |
662 | #endif |
| 663 | vm_offset_t eva; | |
| 6f7b98e0 | 664 | |
| 08f2f1bb | 665 | lp = td->td_lwp; |
| 6092278a | 666 | p = td->td_proc; |
| 6f7b98e0 | 667 | |
| 4e7c41c5 | 668 | if (frame->tf_trapno == T_PAGEFLT) |
| 6092278a | 669 | eva = frame->tf_err; |
| 4e7c41c5 | 670 | else |
| 135d7199 | 671 | eva = 0; |
| 135d7199 MD |
672 | |
| 673 | #ifdef DDB | |
| 674 | if (db_active) { | |
| 675 | ++gd->gd_trap_nesting_level; | |
| 676 | MAKEMPSAFE(have_mplock); | |
| 677 | trap_fatal(frame, FALSE, eva); | |
| 678 | --gd->gd_trap_nesting_level; | |
| 679 | goto out2; | |
| 680 | } | |
| 681 | #endif | |
| 6092278a MD |
682 | type = frame->tf_trapno; |
| 683 | code = frame->tf_err; | |
| 684 | ||
| 685 | #if 0 | |
| 686 | kernel_trap: | |
| 687 | #endif | |
| 688 | /* kernel trap */ | |
| 689 | ||
| 690 | switch (type) { | |
| 691 | case T_PAGEFLT: /* page fault */ | |
| 692 | MAKEMPSAFE(have_mplock); | |
| 693 | trap_pfault(frame, FALSE, eva); | |
| 694 | goto out2; | |
| 6f7b98e0 | 695 | |
| 6092278a MD |
696 | case T_DNA: |
| 697 | #if NNPX > 0 | |
| 6f7b98e0 | 698 | /* |
| 6092278a MD |
699 | * The kernel may be using npx for copying or other |
| 700 | * purposes. | |
| 6f7b98e0 | 701 | */ |
| 431d0fef MD |
702 | panic("kernel NPX should not happen"); |
| 703 | if (npxdna(frame)) | |
| 6092278a MD |
704 | goto out2; |
| 705 | #endif | |
| 706 | break; | |
| 6f7b98e0 | 707 | |
| 6092278a MD |
708 | case T_PROTFLT: /* general protection fault */ |
| 709 | case T_SEGNPFLT: /* segment not present fault */ | |
| 6f7b98e0 | 710 | /* |
| 6092278a MD |
711 | * Invalid segment selectors and out of bounds |
| 712 | * %eip's and %esp's can be set up in user mode. | |
| 713 | * This causes a fault in kernel mode when the | |
| 714 | * kernel tries to return to user mode. We want | |
| 715 | * to get this fault so that we can fix the | |
| 716 | * problem here and not have to check all the | |
| 717 | * selectors and pointers when the user changes | |
| 718 | * them. | |
| 6f7b98e0 | 719 | */ |
| 6092278a MD |
720 | if (mycpu->gd_intr_nesting_level == 0) { |
| 721 | if (td->td_pcb->pcb_onfault) { | |
| 722 | frame->tf_eip = | |
| 723 | (register_t)td->td_pcb->pcb_onfault; | |
| 724 | goto out2; | |
| 725 | } | |
| 6f7b98e0 | 726 | } |
| 6092278a | 727 | break; |
| 6f7b98e0 | 728 | |
| 6092278a | 729 | case T_TSSFLT: |
| 6f7b98e0 | 730 | /* |
| 6092278a MD |
731 | * PSL_NT can be set in user mode and isn't cleared |
| 732 | * automatically when the kernel is entered. This | |
| 733 | * causes a TSS fault when the kernel attempts to | |
| 734 | * `iret' because the TSS link is uninitialized. We | |
| 735 | * want to get this fault so that we can fix the | |
| 736 | * problem here and not every time the kernel is | |
| 737 | * entered. | |
| 6f7b98e0 | 738 | */ |
| 6092278a MD |
739 | if (frame->tf_eflags & PSL_NT) { |
| 740 | frame->tf_eflags &= ~PSL_NT; | |
| 741 | goto out2; | |
| 742 | } | |
| 743 | break; | |
| 6f7b98e0 | 744 | |
| 6092278a MD |
745 | case T_TRCTRAP: /* trace trap */ |
| 746 | #if 0 | |
| 747 | if (frame->tf_eip == (int)IDTVEC(syscall)) { | |
| 748 | /* | |
| 749 | * We've just entered system mode via the | |
| 750 | * syscall lcall. Continue single stepping | |
| 751 | * silently until the syscall handler has | |
| 752 | * saved the flags. | |
| 753 | */ | |
| 754 | goto out2; | |
| 755 | } | |
| 756 | if (frame->tf_eip == (int)IDTVEC(syscall) + 1) { | |
| 757 | /* | |
| 758 | * The syscall handler has now saved the | |
| 759 | * flags. Stop single stepping it. | |
| 760 | */ | |
| 761 | frame->tf_eflags &= ~PSL_T; | |
| 762 | goto out2; | |
| 763 | } | |
| 764 | #endif | |
| 765 | #if 0 | |
| 6f7b98e0 | 766 | /* |
| 6092278a MD |
767 | * Ignore debug register trace traps due to |
| 768 | * accesses in the user's address space, which | |
| 769 | * can happen under several conditions such as | |
| 770 | * if a user sets a watchpoint on a buffer and | |
| 771 | * then passes that buffer to a system call. | |
| 772 | * We still want to get TRCTRAPS for addresses | |
| 773 | * in kernel space because that is useful when | |
| 774 | * debugging the kernel. | |
| 6f7b98e0 | 775 | */ |
| 6092278a MD |
776 | if (user_dbreg_trap()) { |
| 777 | /* | |
| 778 | * Reset breakpoint bits because the | |
| 779 | * processor doesn't | |
| 780 | */ | |
| 781 | load_dr6(rdr6() & 0xfffffff0); | |
| 782 | goto out2; | |
| 6f7b98e0 | 783 | } |
| 6092278a MD |
784 | #endif |
| 785 | /* | |
| bab69519 | 786 | * FALLTHROUGH (TRCTRAP kernel mode, kernel address) |
| 6092278a MD |
787 | */ |
| 788 | case T_BPTFLT: | |
| 789 | /* | |
| 790 | * If DDB is enabled, let it handle the debugger trap. | |
| 791 | * Otherwise, debugger traps "can't happen". | |
| 792 | */ | |
| 793 | #ifdef DDB | |
| 794 | MAKEMPSAFE(have_mplock); | |
| 795 | if (kdb_trap (type, 0, frame)) | |
| 796 | goto out2; | |
| 797 | #endif | |
| 798 | break; | |
| 58e0cae5 MD |
799 | case T_DIVIDE: |
| 800 | MAKEMPSAFE(have_mplock); | |
| 801 | trap_fatal(frame, FALSE, eva); | |
| 802 | goto out2; | |
| 6092278a MD |
803 | case T_NMI: |
| 804 | MAKEMPSAFE(have_mplock); | |
| 805 | trap_fatal(frame, FALSE, eva); | |
| 806 | goto out2; | |
| 2612135b TS |
807 | case T_SYSCALL80: |
| 808 | /* | |
| 809 | * Ignore this trap generated from a spurious SIGTRAP. | |
| 810 | * | |
| 811 | * single stepping in / syscalls leads to spurious / SIGTRAP | |
| 812 | * so ignore | |
| 813 | * | |
| 814 | * Haiku (c) 2007 Simon 'corecode' Schubert | |
| 815 | */ | |
| 816 | goto out2; | |
| 6f7b98e0 MD |
817 | } |
| 818 | ||
| 6092278a MD |
819 | /* |
| 820 | * Translate fault for emulators (e.g. Linux) | |
| 821 | */ | |
| 822 | if (*p->p_sysent->sv_transtrap) | |
| 823 | i = (*p->p_sysent->sv_transtrap)(i, type); | |
| 6f7b98e0 | 824 | |
| 6092278a | 825 | MAKEMPSAFE(have_mplock); |
| 08f2f1bb | 826 | trapsignal(lp, i, ucode); |
| 6092278a MD |
827 | |
| 828 | #ifdef DEBUG | |
| 829 | if (type <= MAX_TRAP_MSG) { | |
| 830 | uprintf("fatal process exception: %s", | |
| 831 | trap_msg[type]); | |
| 832 | if ((type == T_PAGEFLT) || (type == T_PROTFLT)) | |
| 833 | uprintf(", fault VA = 0x%lx", (u_long)eva); | |
| 834 | uprintf("\n"); | |
| 835 | } | |
| 836 | #endif | |
| 837 | ||
| 838 | out2: | |
| 839 | ; | |
| 6092278a MD |
840 | if (have_mplock) |
| 841 | rel_mplock(); | |
| 6092278a | 842 | #ifdef INVARIANTS |
| f9235b6d | 843 | KASSERT(crit_count == td->td_critcount, |
| 3933a3ab | 844 | ("trap: critical section count mismatch! %d/%d", |
| f9235b6d | 845 | crit_count, td->td_pri)); |
| 3933a3ab MD |
846 | KASSERT(curstop == td->td_toks_stop, |
| 847 | ("trap: extra tokens held after trap! %zd/%zd", | |
| 848 | curstop - &td->td_toks_base, | |
| 849 | td->td_toks_stop - &td->td_toks_base)); | |
| 6f7b98e0 | 850 | #endif |
| 6092278a | 851 | } |
| 6f7b98e0 MD |
852 | |
| 853 | int | |
| 854 | trap_pfault(struct trapframe *frame, int usermode, vm_offset_t eva) | |
| 855 | { | |
| 856 | vm_offset_t va; | |
| 857 | struct vmspace *vm = NULL; | |
| 858 | vm_map_t map = 0; | |
| 859 | int rv = 0; | |
| 1b9d3514 | 860 | int fault_flags; |
| 6f7b98e0 MD |
861 | vm_prot_t ftype; |
| 862 | thread_t td = curthread; | |
| 287ebb09 | 863 | struct lwp *lp = td->td_lwp; |
| 6f7b98e0 MD |
864 | |
| 865 | va = trunc_page(eva); | |
| 6092278a | 866 | if (usermode == FALSE) { |
| 6f7b98e0 | 867 | /* |
| 6092278a | 868 | * This is a fault on kernel virtual memory. |
| 6f7b98e0 | 869 | */ |
| 6f7b98e0 MD |
870 | map = &kernel_map; |
| 871 | } else { | |
| 872 | /* | |
| 873 | * This is a fault on non-kernel virtual memory. | |
| 874 | * vm is initialized above to NULL. If curproc is NULL | |
| 875 | * or curproc->p_vmspace is NULL the fault is fatal. | |
| 876 | */ | |
| 287ebb09 MD |
877 | if (lp != NULL) |
| 878 | vm = lp->lwp_vmspace; | |
| 6f7b98e0 MD |
879 | |
| 880 | if (vm == NULL) | |
| 881 | goto nogo; | |
| 882 | ||
| 883 | map = &vm->vm_map; | |
| 884 | } | |
| 885 | ||
| 4e7c41c5 | 886 | if (frame->tf_xflags & PGEX_W) |
| 0035dca9 | 887 | ftype = VM_PROT_READ | VM_PROT_WRITE; |
| 6f7b98e0 MD |
888 | else |
| 889 | ftype = VM_PROT_READ; | |
| 890 | ||
| 891 | if (map != &kernel_map) { | |
| 892 | /* | |
| 893 | * Keep swapout from messing with us during this | |
| 894 | * critical time. | |
| 895 | */ | |
| 287ebb09 | 896 | PHOLD(lp->lwp_proc); |
| 6f7b98e0 MD |
897 | |
| 898 | /* | |
| 1b9d3514 MD |
899 | * Issue fault |
| 900 | */ | |
| 901 | fault_flags = 0; | |
| 902 | if (usermode) | |
| 903 | fault_flags |= VM_FAULT_BURST; | |
| 904 | if (ftype & VM_PROT_WRITE) | |
| 905 | fault_flags |= VM_FAULT_DIRTY; | |
| 906 | else | |
| 907 | fault_flags |= VM_FAULT_NORMAL; | |
| 908 | rv = vm_fault(map, va, ftype, fault_flags); | |
| 287ebb09 | 909 | PRELE(lp->lwp_proc); |
| 6f7b98e0 MD |
910 | } else { |
| 911 | /* | |
| 912 | * Don't have to worry about process locking or stacks in the kernel. | |
| 913 | */ | |
| 914 | rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL); | |
| 915 | } | |
| 6f7b98e0 MD |
916 | if (rv == KERN_SUCCESS) |
| 917 | return (0); | |
| 918 | nogo: | |
| 919 | if (!usermode) { | |
| 920 | if (td->td_gd->gd_intr_nesting_level == 0 && | |
| 921 | td->td_pcb->pcb_onfault) { | |
| 922 | frame->tf_eip = (register_t)td->td_pcb->pcb_onfault; | |
| 923 | return (0); | |
| 924 | } | |
| 6092278a | 925 | trap_fatal(frame, usermode, eva); |
| 6f7b98e0 MD |
926 | return (-1); |
| 927 | } | |
| 6f7b98e0 MD |
928 | return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV); |
| 929 | } | |
| 930 | ||
| 931 | static void | |
| 6092278a | 932 | trap_fatal(struct trapframe *frame, int usermode, vm_offset_t eva) |
| 6f7b98e0 MD |
933 | { |
| 934 | int code, type, ss, esp; | |
| 935 | ||
| 4e7c41c5 | 936 | code = frame->tf_xflags; |
| 6f7b98e0 MD |
937 | type = frame->tf_trapno; |
| 938 | ||
| 9097944f | 939 | if (type <= MAX_TRAP_MSG) { |
| 6f7b98e0 MD |
940 | kprintf("\n\nFatal trap %d: %s while in %s mode\n", |
| 941 | type, trap_msg[type], | |
| 9097944f MD |
942 | (usermode ? "user" : "kernel")); |
| 943 | } | |
| 6a8aa90e | 944 | /* two separate prints in case of a trap on an unmapped page */ |
| 39005e16 | 945 | kprintf("cpuid = %d\n", mycpu->gd_cpuid); |
| 6f7b98e0 | 946 | if (type == T_PAGEFLT) { |
| d557216f | 947 | kprintf("fault virtual address = %p\n", (void *)eva); |
| 6f7b98e0 | 948 | kprintf("fault code = %s %s, %s\n", |
| 6092278a | 949 | usermode ? "user" : "supervisor", |
| 6f7b98e0 MD |
950 | code & PGEX_W ? "write" : "read", |
| 951 | code & PGEX_P ? "protection violation" : "page not present"); | |
| 952 | } | |
| 953 | kprintf("instruction pointer = 0x%x:0x%x\n", | |
| 954 | frame->tf_cs & 0xffff, frame->tf_eip); | |
| 9097944f | 955 | if (usermode) { |
| 6f7b98e0 MD |
956 | ss = frame->tf_ss & 0xffff; |
| 957 | esp = frame->tf_esp; | |
| 958 | } else { | |
| 959 | ss = GSEL(GDATA_SEL, SEL_KPL); | |
| 960 | esp = (int)&frame->tf_esp; | |
| 961 | } | |
| 962 | kprintf("stack pointer = 0x%x:0x%x\n", ss, esp); | |
| 963 | kprintf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp); | |
| 964 | kprintf("processor eflags = "); | |
| 965 | if (frame->tf_eflags & PSL_T) | |
| 966 | kprintf("trace trap, "); | |
| 967 | if (frame->tf_eflags & PSL_I) | |
| 968 | kprintf("interrupt enabled, "); | |
| 969 | if (frame->tf_eflags & PSL_NT) | |
| 970 | kprintf("nested task, "); | |
| 971 | if (frame->tf_eflags & PSL_RF) | |
| 972 | kprintf("resume, "); | |
| 973 | #if 0 | |
| 974 | if (frame->tf_eflags & PSL_VM) | |
| 975 | kprintf("vm86, "); | |
| 976 | #endif | |
| 977 | kprintf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12); | |
| 978 | kprintf("current process = "); | |
| 979 | if (curproc) { | |
| 980 | kprintf("%lu (%s)\n", | |
| 981 | (u_long)curproc->p_pid, curproc->p_comm ? | |
| 982 | curproc->p_comm : ""); | |
| 983 | } else { | |
| 984 | kprintf("Idle\n"); | |
| 985 | } | |
| 986 | kprintf("current thread = pri %d ", curthread->td_pri); | |
| f9235b6d | 987 | if (curthread->td_critcount) |
| 6f7b98e0 MD |
988 | kprintf("(CRIT)"); |
| 989 | kprintf("\n"); | |
| 6f7b98e0 MD |
990 | /** |
| 991 | * XXX FIXME: | |
| 992 | * we probably SHOULD have stopped the other CPUs before now! | |
| 993 | * another CPU COULD have been touching cpl at this moment... | |
| 994 | */ | |
| 995 | kprintf(" <- SMP: XXX"); | |
| 6f7b98e0 MD |
996 | kprintf("\n"); |
| 997 | ||
| 998 | #ifdef KDB | |
| 999 | if (kdb_trap(&psl)) | |
| 1000 | return; | |
| 1001 | #endif | |
| 1002 | #ifdef DDB | |
| 1003 | if ((debugger_on_panic || db_active) && kdb_trap(type, code, frame)) | |
| 1004 | return; | |
| 1005 | #endif | |
| 1006 | kprintf("trap number = %d\n", type); | |
| 1007 | if (type <= MAX_TRAP_MSG) | |
| 1008 | panic("%s", trap_msg[type]); | |
| 1009 | else | |
| 1010 | panic("unknown/reserved trap"); | |
| 1011 | } | |
| 1012 | ||
| 1013 | /* | |
| 1014 | * Double fault handler. Called when a fault occurs while writing | |
| 1015 | * a frame for a trap/exception onto the stack. This usually occurs | |
| 1016 | * when the stack overflows (such is the case with infinite recursion, | |
| 1017 | * for example). | |
| 1018 | * | |
| 1019 | * XXX Note that the current PTD gets replaced by IdlePTD when the | |
| 1020 | * task switch occurs. This means that the stack that was active at | |
| 1021 | * the time of the double fault is not available at <kstack> unless | |
| 1022 | * the machine was idle when the double fault occurred. The downside | |
| 1023 | * of this is that "trace <ebp>" in ddb won't work. | |
| 1024 | */ | |
| 1025 | void | |
| 1026 | dblfault_handler(void) | |
| 1027 | { | |
| 1028 | struct mdglobaldata *gd = mdcpu; | |
| 1029 | ||
| 1030 | kprintf("\nFatal double fault:\n"); | |
| 1031 | kprintf("eip = 0x%x\n", gd->gd_common_tss.tss_eip); | |
| 1032 | kprintf("esp = 0x%x\n", gd->gd_common_tss.tss_esp); | |
| 1033 | kprintf("ebp = 0x%x\n", gd->gd_common_tss.tss_ebp); | |
| 6a8aa90e | 1034 | /* two separate prints in case of a trap on an unmapped page */ |
| 39005e16 | 1035 | kprintf("cpuid = %d\n", mycpu->gd_cpuid); |
| 6f7b98e0 MD |
1036 | panic("double fault"); |
| 1037 | } | |
| 1038 | ||
| 1039 | /* | |
| 7bd858e5 | 1040 | * syscall2 - MP aware system call request C handler |
| 6f7b98e0 | 1041 | * |
| 7bd858e5 MD |
1042 | * A system call is essentially treated as a trap except that the |
| 1043 | * MP lock is not held on entry or return. We are responsible for | |
| 1044 | * obtaining the MP lock if necessary and for handling ASTs | |
| 1045 | * (e.g. a task switch) prior to return. | |
| 6f7b98e0 | 1046 | * |
| 7bd858e5 | 1047 | * MPSAFE |
| 6f7b98e0 | 1048 | */ |
| 6f7b98e0 | 1049 | void |
| 6092278a | 1050 | syscall2(struct trapframe *frame) |
| 6f7b98e0 MD |
1051 | { |
| 1052 | struct thread *td = curthread; | |
| 1053 | struct proc *p = td->td_proc; | |
| 1054 | struct lwp *lp = td->td_lwp; | |
| 1055 | caddr_t params; | |
| 1056 | struct sysent *callp; | |
| 1057 | register_t orig_tf_eflags; | |
| 1058 | int sticks; | |
| 1059 | int error; | |
| 1060 | int narg; | |
| 1061 | #ifdef INVARIANTS | |
| f9235b6d | 1062 | int crit_count = td->td_critcount; |
| 6f7b98e0 | 1063 | #endif |
| 6f7b98e0 | 1064 | int have_mplock = 0; |
| 6f7b98e0 MD |
1065 | u_int code; |
| 1066 | union sysunion args; | |
| 1067 | ||
| 80bbc910 SS |
1068 | KTR_LOG(kernentry_syscall, lp->lwp_proc->p_pid, lp->lwp_tid, |
| 1069 | frame->tf_eax); | |
| 1070 | ||
| d86a23e0 | 1071 | userenter(td, p); /* lazy raise our priority */ |
| 6f7b98e0 MD |
1072 | |
| 1073 | /* | |
| 1074 | * Misc | |
| 1075 | */ | |
| 1076 | sticks = (int)td->td_sticks; | |
| 6092278a | 1077 | orig_tf_eflags = frame->tf_eflags; |
| 6f7b98e0 MD |
1078 | |
| 1079 | /* | |
| 1080 | * Virtual kernel intercept - if a VM context managed by a virtual | |
| 1081 | * kernel issues a system call the virtual kernel handles it, not us. | |
| 1082 | * Restore the virtual kernel context and return from its system | |
| 1083 | * call. The current frame is copied out to the virtual kernel. | |
| 1084 | */ | |
| 39005e16 | 1085 | if (lp->lwp_vkernel && lp->lwp_vkernel->ve) { |
| bb47c072 | 1086 | vkernel_trap(lp, frame); |
| 6f7b98e0 MD |
1087 | error = EJUSTRETURN; |
| 1088 | goto out; | |
| 1089 | } | |
| 1090 | ||
| 1091 | /* | |
| 1092 | * Get the system call parameters and account for time | |
| 1093 | */ | |
| 6092278a MD |
1094 | lp->lwp_md.md_regs = frame; |
| 1095 | params = (caddr_t)frame->tf_esp + sizeof(int); | |
| 1096 | code = frame->tf_eax; | |
| 6f7b98e0 MD |
1097 | |
| 1098 | if (p->p_sysent->sv_prepsyscall) { | |
| 1099 | (*p->p_sysent->sv_prepsyscall)( | |
| 6092278a | 1100 | frame, (int *)(&args.nosys.sysmsg + 1), |
| 6f7b98e0 MD |
1101 | &code, ¶ms); |
| 1102 | } else { | |
| 1103 | /* | |
| 1104 | * Need to check if this is a 32 bit or 64 bit syscall. | |
| 1105 | * fuword is MP aware. | |
| 1106 | */ | |
| 1107 | if (code == SYS_syscall) { | |
| 1108 | /* | |
| 1109 | * Code is first argument, followed by actual args. | |
| 1110 | */ | |
| 1111 | code = fuword(params); | |
| 1112 | params += sizeof(int); | |
| 1113 | } else if (code == SYS___syscall) { | |
| 1114 | /* | |
| 1115 | * Like syscall, but code is a quad, so as to maintain | |
| 1116 | * quad alignment for the rest of the arguments. | |
| 1117 | */ | |
| 1118 | code = fuword(params); | |
| 1119 | params += sizeof(quad_t); | |
| 1120 | } | |
| 1121 | } | |
| 1122 | ||
| 1123 | code &= p->p_sysent->sv_mask; | |
| 1124 | if (code >= p->p_sysent->sv_size) | |
| 1125 | callp = &p->p_sysent->sv_table[0]; | |
| 1126 | else | |
| 1127 | callp = &p->p_sysent->sv_table[code]; | |
| 1128 | ||
| 1129 | narg = callp->sy_narg & SYF_ARGMASK; | |
| 1130 | ||
| 1131 | /* | |
| 1132 | * copyin is MP aware, but the tracing code is not | |
| 1133 | */ | |
| 1134 | if (narg && params) { | |
| 1135 | error = copyin(params, (caddr_t)(&args.nosys.sysmsg + 1), | |
| 1136 | narg * sizeof(register_t)); | |
| 1137 | if (error) { | |
| 1138 | #ifdef KTRACE | |
| 1139 | if (KTRPOINT(td, KTR_SYSCALL)) { | |
| 1140 | MAKEMPSAFE(have_mplock); | |
| 1141 | ||
| 9fb04d14 | 1142 | ktrsyscall(lp, code, narg, |
| 6f7b98e0 MD |
1143 | (void *)(&args.nosys.sysmsg + 1)); |
| 1144 | } | |
| 1145 | #endif | |
| 1146 | goto bad; | |
| 1147 | } | |
| 1148 | } | |
| 1149 | ||
| 1150 | #ifdef KTRACE | |
| 1151 | if (KTRPOINT(td, KTR_SYSCALL)) { | |
| 1152 | MAKEMPSAFE(have_mplock); | |
| 9fb04d14 | 1153 | ktrsyscall(lp, code, narg, (void *)(&args.nosys.sysmsg + 1)); |
| 6f7b98e0 MD |
1154 | } |
| 1155 | #endif | |
| 1156 | ||
| 1157 | /* | |
| 1158 | * For traditional syscall code edx is left untouched when 32 bit | |
| 1159 | * results are returned. Since edx is loaded from fds[1] when the | |
| 1160 | * system call returns we pre-set it here. | |
| 1161 | */ | |
| 1162 | args.sysmsg_fds[0] = 0; | |
| 6092278a | 1163 | args.sysmsg_fds[1] = frame->tf_edx; |
| 6f7b98e0 MD |
1164 | |
| 1165 | /* | |
| 1166 | * The syscall might manipulate the trap frame. If it does it | |
| 1167 | * will probably return EJUSTRETURN. | |
| 1168 | */ | |
| 6092278a | 1169 | args.sysmsg_frame = frame; |
| 6f7b98e0 MD |
1170 | |
| 1171 | STOPEVENT(p, S_SCE, narg); /* MP aware */ | |
| 1172 | ||
| 6f7b98e0 | 1173 | /* |
| 3919ced0 MD |
1174 | * NOTE: All system calls run MPSAFE now. The system call itself |
| 1175 | * is responsible for getting the MP lock. | |
| 6f7b98e0 | 1176 | */ |
| 6f7b98e0 MD |
1177 | error = (*callp->sy_call)(&args); |
| 1178 | ||
| aaf8b91f MD |
1179 | #if 0 |
| 1180 | kprintf("system call %d returned %d\n", code, error); | |
| 1181 | #endif | |
| 1182 | ||
| 6f7b98e0 MD |
1183 | out: |
| 1184 | /* | |
| 1185 | * MP SAFE (we may or may not have the MP lock at this point) | |
| 1186 | */ | |
| 1187 | switch (error) { | |
| 1188 | case 0: | |
| 1189 | /* | |
| 1190 | * Reinitialize proc pointer `p' as it may be different | |
| 1191 | * if this is a child returning from fork syscall. | |
| 1192 | */ | |
| 1193 | p = curproc; | |
| 1194 | lp = curthread->td_lwp; | |
| 6092278a MD |
1195 | frame->tf_eax = args.sysmsg_fds[0]; |
| 1196 | frame->tf_edx = args.sysmsg_fds[1]; | |
| 1197 | frame->tf_eflags &= ~PSL_C; | |
| 6f7b98e0 MD |
1198 | break; |
| 1199 | case ERESTART: | |
| 1200 | /* | |
| 1201 | * Reconstruct pc, assuming lcall $X,y is 7 bytes, | |
| 1202 | * int 0x80 is 2 bytes. We saved this in tf_err. | |
| 1203 | */ | |
| 6092278a | 1204 | frame->tf_eip -= frame->tf_err; |
| 6f7b98e0 MD |
1205 | break; |
| 1206 | case EJUSTRETURN: | |
| 1207 | break; | |
| 1208 | case EASYNC: | |
| 1209 | panic("Unexpected EASYNC return value (for now)"); | |
| 1210 | default: | |
| 1211 | bad: | |
| 1212 | if (p->p_sysent->sv_errsize) { | |
| 1213 | if (error >= p->p_sysent->sv_errsize) | |
| 1214 | error = -1; /* XXX */ | |
| 1215 | else | |
| 1216 | error = p->p_sysent->sv_errtbl[error]; | |
| 1217 | } | |
| 6092278a MD |
1218 | frame->tf_eax = error; |
| 1219 | frame->tf_eflags |= PSL_C; | |
| 6f7b98e0 MD |
1220 | break; |
| 1221 | } | |
| 1222 | ||
| 1223 | /* | |
| 1224 | * Traced syscall. trapsignal() is not MP aware. | |
| 1225 | */ | |
| 1226 | if ((orig_tf_eflags & PSL_T) /*&& !(orig_tf_eflags & PSL_VM)*/) { | |
| 1227 | MAKEMPSAFE(have_mplock); | |
| 6092278a | 1228 | frame->tf_eflags &= ~PSL_T; |
| f2642d5a | 1229 | trapsignal(lp, SIGTRAP, TRAP_TRACE); |
| 6f7b98e0 MD |
1230 | } |
| 1231 | ||
| 1232 | /* | |
| 1233 | * Handle reschedule and other end-of-syscall issues | |
| 1234 | */ | |
| 6092278a | 1235 | userret(lp, frame, sticks); |
| 6f7b98e0 MD |
1236 | |
| 1237 | #ifdef KTRACE | |
| 1238 | if (KTRPOINT(td, KTR_SYSRET)) { | |
| 1239 | MAKEMPSAFE(have_mplock); | |
| 9fb04d14 | 1240 | ktrsysret(lp, code, error, args.sysmsg_result); |
| 6f7b98e0 MD |
1241 | } |
| 1242 | #endif | |
| 1243 | ||
| 1244 | /* | |
| 1245 | * This works because errno is findable through the | |
| 1246 | * register set. If we ever support an emulation where this | |
| 1247 | * is not the case, this code will need to be revisited. | |
| 1248 | */ | |
| 1249 | STOPEVENT(p, S_SCX, code); | |
| 1250 | ||
| 1251 | userexit(lp); | |
| 6f7b98e0 MD |
1252 | /* |
| 1253 | * Release the MP lock if we had to get it | |
| 1254 | */ | |
| 6f7b98e0 MD |
1255 | if (have_mplock) |
| 1256 | rel_mplock(); | |
| 80bbc910 | 1257 | KTR_LOG(kernentry_syscall_ret, lp->lwp_proc->p_pid, lp->lwp_tid, error); |
| 6f7b98e0 | 1258 | #ifdef INVARIANTS |
| f9235b6d | 1259 | KASSERT(crit_count == td->td_critcount, |
| 6f7b98e0 | 1260 | ("syscall: critical section count mismatch! %d/%d", |
| f9235b6d | 1261 | crit_count, td->td_pri)); |
| 3933a3ab MD |
1262 | KASSERT(&td->td_toks_base == td->td_toks_stop, |
| 1263 | ("syscall: extra tokens held after trap! %zd", | |
| 1264 | td->td_toks_stop - &td->td_toks_base)); | |
| 6f7b98e0 MD |
1265 | #endif |
| 1266 | } | |
| 1267 | ||
| 2b0bd8aa MD |
1268 | /* |
| 1269 | * NOTE: mplock not held at any point | |
| 1270 | */ | |
| 91bd9c1e SS |
1271 | void |
| 1272 | fork_return(struct lwp *lp, struct trapframe *frame) | |
| 1273 | { | |
| 1274 | frame->tf_eax = 0; /* Child returns zero */ | |
| 1275 | frame->tf_eflags &= ~PSL_C; /* success */ | |
| 1276 | frame->tf_edx = 1; | |
| 1277 | ||
| 1278 | generic_lwp_return(lp, frame); | |
| 80bbc910 | 1279 | KTR_LOG(kernentry_fork_ret, lp->lwp_proc->p_pid, lp->lwp_tid); |
| 91bd9c1e SS |
1280 | } |
| 1281 | ||
| 6f7b98e0 MD |
1282 | /* |
| 1283 | * Simplified back end of syscall(), used when returning from fork() | |
| 2b0bd8aa MD |
1284 | * directly into user mode. |
| 1285 | * | |
| 1286 | * This code will return back into the fork trampoline code which then | |
| 1287 | * runs doreti. | |
| 1288 | * | |
| 1289 | * NOTE: The mplock is not held at any point. | |
| 6f7b98e0 MD |
1290 | */ |
| 1291 | void | |
| 91bd9c1e | 1292 | generic_lwp_return(struct lwp *lp, struct trapframe *frame) |
| 6f7b98e0 MD |
1293 | { |
| 1294 | struct proc *p = lp->lwp_proc; | |
| 1295 | ||
| 6f7b98e0 MD |
1296 | /* |
| 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. | |
| 1304 | */ | |
| 1305 | lwkt_setpri_self(TDPRI_USER_NORM); | |
| d86a23e0 | 1306 | userenter(lp->lwp_thread, p); |
| c7eb0589 | 1307 | userret(lp, frame, 0); |
| 6f7b98e0 MD |
1308 | #ifdef KTRACE |
| 1309 | if (KTRPOINT(lp->lwp_thread, KTR_SYSRET)) | |
| 9fb04d14 | 1310 | ktrsysret(lp, SYS_fork, 0, 0); |
| 6f7b98e0 | 1311 | #endif |
| 4643740a | 1312 | lp->lwp_flags |= LWP_PASSIVE_ACQ; |
| 6f7b98e0 | 1313 | userexit(lp); |
| 4643740a | 1314 | lp->lwp_flags &= ~LWP_PASSIVE_ACQ; |
| 6f7b98e0 | 1315 | } |
| 9ad680a3 | 1316 | |
| 4e7c41c5 MD |
1317 | /* |
| 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 | |
| 1320 | * thread. | |
| 1321 | * | |
| 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). | |
| 1327 | * | |
| 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. | |
| 1330 | */ | |
| 9ad680a3 | 1331 | void |
| c7eb0589 | 1332 | go_user(struct intrframe *frame) |
| 9ad680a3 | 1333 | { |
| c7eb0589 | 1334 | struct trapframe *tf = (void *)&frame->if_gs; |
| 4e7c41c5 MD |
1335 | int r; |
| 1336 | ||
| 1fdc022f MD |
1337 | /* |
| 1338 | * Interrupts may be disabled on entry, make sure all signals | |
| 1339 | * can be received before beginning our loop. | |
| 1340 | */ | |
| 1341 | sigsetmask(0); | |
| 1342 | ||
| 1343 | /* | |
| 1344 | * Switch to the current simulated user process, then call | |
| 1345 | * user_trap() when we break out of it (usually due to a signal). | |
| 1346 | */ | |
| 135d7199 | 1347 | for (;;) { |
| 431d0fef MD |
1348 | /* |
| 1349 | * Tell the real kernel whether it is ok to use the FP | |
| 1350 | * unit or not. | |
| eaaa6d60 MD |
1351 | * |
| 1352 | * The critical section is required to prevent an interrupt | |
| 1353 | * from causing a preemptive task switch and changing | |
| 1354 | * the FP state. | |
| 431d0fef | 1355 | */ |
| eaaa6d60 | 1356 | crit_enter(); |
| 431d0fef | 1357 | if (mdcpu->gd_npxthread == curthread) { |
| 4b486183 | 1358 | tf->tf_xflags &= ~PGEX_FPFAULT; |
| 431d0fef | 1359 | } else { |
| 4b486183 | 1360 | tf->tf_xflags |= PGEX_FPFAULT; |
| 431d0fef MD |
1361 | } |
| 1362 | ||
| 1363 | /* | |
| 7c1212ec MD |
1364 | * Run emulated user process context. This call interlocks |
| 1365 | * with new mailbox signals. | |
| 4b486183 MD |
1366 | * |
| 1367 | * Set PGEX_U unconditionally, indicating a user frame (the | |
| 1368 | * bit is normally set only by T_PAGEFLT). | |
| 7c1212ec MD |
1369 | */ |
| 1370 | r = vmspace_ctl(&curproc->p_vmspace->vm_pmap, VMSPACE_CTL_RUN, | |
| 1371 | tf, &curthread->td_savevext); | |
| eaaa6d60 | 1372 | crit_exit(); |
| c7eb0589 | 1373 | frame->if_xflags |= PGEX_U; |
| 4b486183 MD |
1374 | #if 0 |
| 1375 | kprintf("GO USER %d trap %d EVA %08x EIP %08x ESP %08x XFLAGS %02x/%02x\n", | |
| 1376 | r, tf->tf_trapno, tf->tf_err, tf->tf_eip, tf->tf_esp, | |
| c7eb0589 | 1377 | tf->tf_xflags, frame->if_xflags); |
| 4b486183 | 1378 | #endif |
| 7c1212ec | 1379 | if (r < 0) { |
| 24eb47e0 | 1380 | if (errno != EINTR) |
| bb47c072 | 1381 | panic("vmspace_ctl failed error %d", errno); |
| aaf8b91f | 1382 | } else { |
| 7c1212ec MD |
1383 | if (tf->tf_trapno) { |
| 1384 | user_trap(tf); | |
| 7c1212ec | 1385 | } |
| aaf8b91f | 1386 | } |
| 6179d4f3 MD |
1387 | if (mycpu->gd_reqflags & RQF_AST_MASK) { |
| 1388 | tf->tf_trapno = T_ASTFLT; | |
| 1389 | user_trap(tf); | |
| 1390 | } | |
| 1391 | tf->tf_trapno = 0; | |
| 135d7199 | 1392 | } |
| 9ad680a3 | 1393 | } |
| 135d7199 | 1394 | |
| 431d0fef | 1395 | /* |
| 4b486183 | 1396 | * If PGEX_FPFAULT is set then set FP_VIRTFP in the PCB to force a T_DNA |
| 431d0fef MD |
1397 | * fault (which is then passed back to the virtual kernel) if an attempt is |
| 1398 | * made to use the FP unit. | |
| bab69519 | 1399 | * |
| 431d0fef MD |
1400 | * XXX this is a fairly big hack. |
| 1401 | */ | |
| 1402 | void | |
| 1403 | set_vkernel_fp(struct trapframe *frame) | |
| 1404 | { | |
| 1405 | struct thread *td = curthread; | |
| 1406 | ||
| 4b486183 | 1407 | if (frame->tf_xflags & PGEX_FPFAULT) { |
| 431d0fef MD |
1408 | td->td_pcb->pcb_flags |= FP_VIRTFP; |
| 1409 | if (mdcpu->gd_npxthread == td) | |
| 1410 | npxexit(); | |
| 1411 | } else { | |
| 1412 | td->td_pcb->pcb_flags &= ~FP_VIRTFP; | |
| 1413 | } | |
| 1414 | } | |
| 1415 | ||
| bb47c072 MD |
1416 | /* |
| 1417 | * Called from vkernel_trap() to fixup the vkernel's syscall | |
| 1418 | * frame for vmspace_ctl() return. | |
| 1419 | */ | |
| 1420 | void | |
| 1421 | cpu_vkernel_trap(struct trapframe *frame, int error) | |
| 1422 | { | |
| 1423 | frame->tf_eax = error; | |
| 1424 | if (error) | |
| 1425 | frame->tf_eflags |= PSL_C; | |
| 1426 | else | |
| 1427 | frame->tf_eflags &= ~PSL_C; | |
| 1428 | } |