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