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 */ 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 /* 44 * 386 Trap and System call handling 45 */ 46 47 #include "opt_clock.h" 48 #include "opt_cpu.h" 49 #include "opt_hwpmc_hooks.h" 50 #include "opt_isa.h" 51 #include "opt_kdb.h" 52 #include "opt_npx.h" 53 #include "opt_stack.h" 54 #include "opt_trap.h" 55 56 #include <sys/param.h> 57 #include <sys/bus.h> 58 #include <sys/systm.h> 59 #include <sys/proc.h> 60 #include <sys/pioctl.h> 61 #include <sys/ptrace.h> 62 #include <sys/kdb.h> 63 #include <sys/kernel.h> 64 #include <sys/ktr.h> 65 #include <sys/lock.h> 66 #include <sys/mutex.h> 67 #include <sys/resourcevar.h> 68 #include <sys/signalvar.h> 69 #include <sys/syscall.h> 70 #include <sys/sysctl.h> 71 #include <sys/sysent.h> 72 #include <sys/uio.h> 73 #include <sys/vmmeter.h> 74 #ifdef HWPMC_HOOKS 75 #include <sys/pmckern.h> 76 PMC_SOFT_DEFINE( , , page_fault, all); 77 PMC_SOFT_DEFINE( , , page_fault, read); 78 PMC_SOFT_DEFINE( , , page_fault, write); 79 #endif 80 #include <security/audit/audit.h> 81 82 #include <vm/vm.h> 83 #include <vm/vm_param.h> 84 #include <vm/pmap.h> 85 #include <vm/vm_kern.h> 86 #include <vm/vm_map.h> 87 #include <vm/vm_page.h> 88 #include <vm/vm_extern.h> 89 90 #include <machine/cpu.h> 91 #include <machine/intr_machdep.h> 92 #include <x86/mca.h> 93 #include <machine/md_var.h> 94 #include <machine/pcb.h> 95 #ifdef SMP 96 #include <machine/smp.h> 97 #endif 98 #include <machine/stack.h> 99 #include <machine/tss.h> 100 #include <machine/vm86.h> 101 102 #ifdef POWERFAIL_NMI 103 #include <sys/syslog.h> 104 #include <machine/clock.h> 105 #endif 106 107 #ifdef KDTRACE_HOOKS 108 #include <sys/dtrace_bsd.h> 109 #endif 110 111 extern void trap(struct trapframe *frame); 112 extern void syscall(struct trapframe *frame); 113 114 static int trap_pfault(struct trapframe *, int, vm_offset_t); 115 static void trap_fatal(struct trapframe *, vm_offset_t); 116 void dblfault_handler(void); 117 118 extern inthand_t IDTVEC(lcall_syscall); 119 120 #define MAX_TRAP_MSG 32 121 static char *trap_msg[] = { 122 "", /* 0 unused */ 123 "privileged instruction fault", /* 1 T_PRIVINFLT */ 124 "", /* 2 unused */ 125 "breakpoint instruction fault", /* 3 T_BPTFLT */ 126 "", /* 4 unused */ 127 "", /* 5 unused */ 128 "arithmetic trap", /* 6 T_ARITHTRAP */ 129 "", /* 7 unused */ 130 "", /* 8 unused */ 131 "general protection fault", /* 9 T_PROTFLT */ 132 "trace trap", /* 10 T_TRCTRAP */ 133 "", /* 11 unused */ 134 "page fault", /* 12 T_PAGEFLT */ 135 "", /* 13 unused */ 136 "alignment fault", /* 14 T_ALIGNFLT */ 137 "", /* 15 unused */ 138 "", /* 16 unused */ 139 "", /* 17 unused */ 140 "integer divide fault", /* 18 T_DIVIDE */ 141 "non-maskable interrupt trap", /* 19 T_NMI */ 142 "overflow trap", /* 20 T_OFLOW */ 143 "FPU bounds check fault", /* 21 T_BOUND */ 144 "FPU device not available", /* 22 T_DNA */ 145 "double fault", /* 23 T_DOUBLEFLT */ 146 "FPU operand fetch fault", /* 24 T_FPOPFLT */ 147 "invalid TSS fault", /* 25 T_TSSFLT */ 148 "segment not present fault", /* 26 T_SEGNPFLT */ 149 "stack fault", /* 27 T_STKFLT */ 150 "machine check trap", /* 28 T_MCHK */ 151 "SIMD floating-point exception", /* 29 T_XMMFLT */ 152 "reserved (unknown) fault", /* 30 T_RESERVED */ 153 "", /* 31 unused (reserved) */ 154 "DTrace pid return trap", /* 32 T_DTRACE_RET */ 155 }; 156 157 #if defined(I586_CPU) && !defined(NO_F00F_HACK) 158 int has_f00f_bug = 0; /* Initialized so that it can be patched. */ 159 #endif 160 161 static int prot_fault_translation = 0; 162 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW, 163 &prot_fault_translation, 0, "Select signal to deliver on protection fault"); 164 static int uprintf_signal; 165 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RW, 166 &uprintf_signal, 0, 167 "Print debugging information on trap signal to ctty"); 168 169 /* 170 * Exception, fault, and trap interface to the FreeBSD kernel. 171 * This common code is called from assembly language IDT gate entry 172 * routines that prepare a suitable stack frame, and restore this 173 * frame after the exception has been processed. 174 */ 175 176 void 177 trap(struct trapframe *frame) 178 { 179 #ifdef KDTRACE_HOOKS 180 struct reg regs; 181 #endif 182 struct thread *td = curthread; 183 struct proc *p = td->td_proc; 184 #ifdef KDB 185 register_t dr6; 186 #endif 187 int i = 0, ucode = 0; 188 u_int type; 189 register_t addr = 0; 190 vm_offset_t eva; 191 ksiginfo_t ksi; 192 #ifdef POWERFAIL_NMI 193 static int lastalert = 0; 194 #endif 195 196 PCPU_INC(cnt.v_trap); 197 type = frame->tf_trapno; 198 199 #ifdef SMP 200 /* Handler for NMI IPIs used for stopping CPUs. */ 201 if (type == T_NMI) { 202 if (ipi_nmi_handler() == 0) 203 goto out; 204 } 205 #endif /* SMP */ 206 207 #ifdef KDB 208 if (kdb_active) { 209 kdb_reenter(); 210 goto out; 211 } 212 #endif 213 214 if (type == T_RESERVED) { 215 trap_fatal(frame, 0); 216 goto out; 217 } 218 219 if (type == T_NMI) { 220 #ifdef HWPMC_HOOKS 221 /* 222 * CPU PMCs interrupt using an NMI so we check for that first. 223 * If the HWPMC module is active, 'pmc_hook' will point to 224 * the function to be called. A non-zero return value from the 225 * hook means that the NMI was consumed by it and that we can 226 * return immediately. 227 */ 228 if (pmc_intr != NULL && 229 (*pmc_intr)(PCPU_GET(cpuid), frame) != 0) 230 goto out; 231 #endif 232 233 #ifdef STACK 234 if (stack_nmi_handler(frame) != 0) 235 goto out; 236 #endif 237 } 238 239 if (type == T_MCHK) { 240 mca_intr(); 241 goto out; 242 } 243 244 #ifdef KDTRACE_HOOKS 245 /* 246 * A trap can occur while DTrace executes a probe. Before 247 * executing the probe, DTrace blocks re-scheduling and sets 248 * a flag in its per-cpu flags to indicate that it doesn't 249 * want to fault. On returning from the probe, the no-fault 250 * flag is cleared and finally re-scheduling is enabled. 251 */ 252 if ((type == T_PROTFLT || type == T_PAGEFLT) && 253 dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type)) 254 goto out; 255 #endif 256 257 if ((frame->tf_eflags & PSL_I) == 0) { 258 /* 259 * Buggy application or kernel code has disabled 260 * interrupts and then trapped. Enabling interrupts 261 * now is wrong, but it is better than running with 262 * interrupts disabled until they are accidentally 263 * enabled later. 264 */ 265 if (TRAPF_USERMODE(frame) && 266 (curpcb->pcb_flags & PCB_VM86CALL) == 0) 267 uprintf( 268 "pid %ld (%s): trap %d with interrupts disabled\n", 269 (long)curproc->p_pid, curthread->td_name, type); 270 else if (type != T_NMI && type != T_BPTFLT && 271 type != T_TRCTRAP && 272 frame->tf_eip != (int)cpu_switch_load_gs) { 273 /* 274 * XXX not quite right, since this may be for a 275 * multiple fault in user mode. 276 */ 277 printf("kernel trap %d with interrupts disabled\n", 278 type); 279 /* 280 * Page faults need interrupts disabled until later, 281 * and we shouldn't enable interrupts while holding 282 * a spin lock. 283 */ 284 if (type != T_PAGEFLT && 285 td->td_md.md_spinlock_count == 0) 286 enable_intr(); 287 } 288 } 289 eva = 0; 290 if (type == T_PAGEFLT) { 291 /* 292 * For some Cyrix CPUs, %cr2 is clobbered by 293 * interrupts. This problem is worked around by using 294 * an interrupt gate for the pagefault handler. We 295 * are finally ready to read %cr2 and conditionally 296 * reenable interrupts. If we hold a spin lock, then 297 * we must not reenable interrupts. This might be a 298 * spurious page fault. 299 */ 300 eva = rcr2(); 301 if (td->td_md.md_spinlock_count == 0) 302 enable_intr(); 303 } 304 305 if (TRAPF_USERMODE(frame) && (curpcb->pcb_flags & PCB_VM86CALL) == 0) { 306 /* user trap */ 307 308 td->td_pticks = 0; 309 td->td_frame = frame; 310 addr = frame->tf_eip; 311 if (td->td_cowgen != p->p_cowgen) 312 thread_cow_update(td); 313 314 switch (type) { 315 case T_PRIVINFLT: /* privileged instruction fault */ 316 i = SIGILL; 317 ucode = ILL_PRVOPC; 318 break; 319 320 case T_BPTFLT: /* bpt instruction fault */ 321 case T_TRCTRAP: /* trace trap */ 322 enable_intr(); 323 #ifdef KDTRACE_HOOKS 324 if (type == T_BPTFLT) { 325 fill_frame_regs(frame, ®s); 326 if (dtrace_pid_probe_ptr != NULL && 327 dtrace_pid_probe_ptr(®s) == 0) 328 goto out; 329 } 330 #endif 331 user_trctrap_out: 332 frame->tf_eflags &= ~PSL_T; 333 i = SIGTRAP; 334 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT); 335 break; 336 337 case T_ARITHTRAP: /* arithmetic trap */ 338 #ifdef DEV_NPX 339 ucode = npxtrap_x87(); 340 if (ucode == -1) 341 goto userout; 342 #else 343 ucode = 0; 344 #endif 345 i = SIGFPE; 346 break; 347 348 /* 349 * The following two traps can happen in 350 * vm86 mode, and, if so, we want to handle 351 * them specially. 352 */ 353 case T_PROTFLT: /* general protection fault */ 354 case T_STKFLT: /* stack fault */ 355 if (frame->tf_eflags & PSL_VM) { 356 i = vm86_emulate((struct vm86frame *)frame); 357 if (i == SIGTRAP) { 358 type = T_TRCTRAP; 359 load_dr6(rdr6() | 0x4000); 360 goto user_trctrap_out; 361 } 362 if (i == 0) 363 goto user; 364 break; 365 } 366 i = SIGBUS; 367 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR; 368 break; 369 case T_SEGNPFLT: /* segment not present fault */ 370 i = SIGBUS; 371 ucode = BUS_ADRERR; 372 break; 373 case T_TSSFLT: /* invalid TSS fault */ 374 i = SIGBUS; 375 ucode = BUS_OBJERR; 376 break; 377 case T_ALIGNFLT: 378 i = SIGBUS; 379 ucode = BUS_ADRALN; 380 break; 381 case T_DOUBLEFLT: /* double fault */ 382 default: 383 i = SIGBUS; 384 ucode = BUS_OBJERR; 385 break; 386 387 case T_PAGEFLT: /* page fault */ 388 389 i = trap_pfault(frame, TRUE, eva); 390 #if defined(I586_CPU) && !defined(NO_F00F_HACK) 391 if (i == -2) { 392 /* 393 * The f00f hack workaround has triggered, so 394 * treat the fault as an illegal instruction 395 * (T_PRIVINFLT) instead of a page fault. 396 */ 397 type = frame->tf_trapno = T_PRIVINFLT; 398 399 /* Proceed as in that case. */ 400 ucode = ILL_PRVOPC; 401 i = SIGILL; 402 break; 403 } 404 #endif 405 if (i == -1) 406 goto userout; 407 if (i == 0) 408 goto user; 409 410 if (i == SIGSEGV) 411 ucode = SEGV_MAPERR; 412 else { 413 if (prot_fault_translation == 0) { 414 /* 415 * Autodetect. 416 * This check also covers the images 417 * without the ABI-tag ELF note. 418 */ 419 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD 420 && p->p_osrel >= P_OSREL_SIGSEGV) { 421 i = SIGSEGV; 422 ucode = SEGV_ACCERR; 423 } else { 424 i = SIGBUS; 425 ucode = BUS_PAGE_FAULT; 426 } 427 } else if (prot_fault_translation == 1) { 428 /* 429 * Always compat mode. 430 */ 431 i = SIGBUS; 432 ucode = BUS_PAGE_FAULT; 433 } else { 434 /* 435 * Always SIGSEGV mode. 436 */ 437 i = SIGSEGV; 438 ucode = SEGV_ACCERR; 439 } 440 } 441 addr = eva; 442 break; 443 444 case T_DIVIDE: /* integer divide fault */ 445 ucode = FPE_INTDIV; 446 i = SIGFPE; 447 break; 448 449 #ifdef DEV_ISA 450 case T_NMI: 451 #ifdef POWERFAIL_NMI 452 #ifndef TIMER_FREQ 453 # define TIMER_FREQ 1193182 454 #endif 455 if (time_second - lastalert > 10) { 456 log(LOG_WARNING, "NMI: power fail\n"); 457 sysbeep(880, hz); 458 lastalert = time_second; 459 } 460 goto userout; 461 #else /* !POWERFAIL_NMI */ 462 nmi_handle_intr(type, frame); 463 break; 464 #endif /* POWERFAIL_NMI */ 465 #endif /* DEV_ISA */ 466 467 case T_OFLOW: /* integer overflow fault */ 468 ucode = FPE_INTOVF; 469 i = SIGFPE; 470 break; 471 472 case T_BOUND: /* bounds check fault */ 473 ucode = FPE_FLTSUB; 474 i = SIGFPE; 475 break; 476 477 case T_DNA: 478 #ifdef DEV_NPX 479 KASSERT(PCB_USER_FPU(td->td_pcb), 480 ("kernel FPU ctx has leaked")); 481 /* transparent fault (due to context switch "late") */ 482 if (npxdna()) 483 goto userout; 484 #endif 485 uprintf("pid %d killed due to lack of floating point\n", 486 p->p_pid); 487 i = SIGKILL; 488 ucode = 0; 489 break; 490 491 case T_FPOPFLT: /* FPU operand fetch fault */ 492 ucode = ILL_COPROC; 493 i = SIGILL; 494 break; 495 496 case T_XMMFLT: /* SIMD floating-point exception */ 497 #if defined(DEV_NPX) && !defined(CPU_DISABLE_SSE) && defined(I686_CPU) 498 ucode = npxtrap_sse(); 499 if (ucode == -1) 500 goto userout; 501 #else 502 ucode = 0; 503 #endif 504 i = SIGFPE; 505 break; 506 #ifdef KDTRACE_HOOKS 507 case T_DTRACE_RET: 508 enable_intr(); 509 fill_frame_regs(frame, ®s); 510 if (dtrace_return_probe_ptr != NULL && 511 dtrace_return_probe_ptr(®s) == 0) 512 goto out; 513 break; 514 #endif 515 } 516 } else { 517 /* kernel trap */ 518 519 KASSERT(cold || td->td_ucred != NULL, 520 ("kernel trap doesn't have ucred")); 521 switch (type) { 522 case T_PAGEFLT: /* page fault */ 523 (void) trap_pfault(frame, FALSE, eva); 524 goto out; 525 526 case T_DNA: 527 #ifdef DEV_NPX 528 if (PCB_USER_FPU(td->td_pcb)) 529 panic("Unregistered use of FPU in kernel"); 530 if (npxdna()) 531 goto out; 532 #endif 533 break; 534 535 case T_ARITHTRAP: /* arithmetic trap */ 536 case T_XMMFLT: /* SIMD floating-point exception */ 537 case T_FPOPFLT: /* FPU operand fetch fault */ 538 /* 539 * XXXKIB for now disable any FPU traps in kernel 540 * handler registration seems to be overkill 541 */ 542 trap_fatal(frame, 0); 543 goto out; 544 545 /* 546 * The following two traps can happen in 547 * vm86 mode, and, if so, we want to handle 548 * them specially. 549 */ 550 case T_PROTFLT: /* general protection fault */ 551 case T_STKFLT: /* stack fault */ 552 if (frame->tf_eflags & PSL_VM) { 553 i = vm86_emulate((struct vm86frame *)frame); 554 if (i == SIGTRAP) { 555 type = T_TRCTRAP; 556 load_dr6(rdr6() | 0x4000); 557 goto kernel_trctrap; 558 } 559 if (i != 0) 560 /* 561 * returns to original process 562 */ 563 vm86_trap((struct vm86frame *)frame); 564 goto out; 565 } 566 if (type == T_STKFLT) 567 break; 568 569 /* FALL THROUGH */ 570 571 case T_SEGNPFLT: /* segment not present fault */ 572 if (curpcb->pcb_flags & PCB_VM86CALL) 573 break; 574 575 /* 576 * Invalid %fs's and %gs's can be created using 577 * procfs or PT_SETREGS or by invalidating the 578 * underlying LDT entry. This causes a fault 579 * in kernel mode when the kernel attempts to 580 * switch contexts. Lose the bad context 581 * (XXX) so that we can continue, and generate 582 * a signal. 583 */ 584 if (frame->tf_eip == (int)cpu_switch_load_gs) { 585 curpcb->pcb_gs = 0; 586 #if 0 587 PROC_LOCK(p); 588 kern_psignal(p, SIGBUS); 589 PROC_UNLOCK(p); 590 #endif 591 goto out; 592 } 593 594 if (td->td_intr_nesting_level != 0) 595 break; 596 597 /* 598 * Invalid segment selectors and out of bounds 599 * %eip's and %esp's can be set up in user mode. 600 * This causes a fault in kernel mode when the 601 * kernel tries to return to user mode. We want 602 * to get this fault so that we can fix the 603 * problem here and not have to check all the 604 * selectors and pointers when the user changes 605 * them. 606 */ 607 if (frame->tf_eip == (int)doreti_iret) { 608 frame->tf_eip = (int)doreti_iret_fault; 609 goto out; 610 } 611 if (frame->tf_eip == (int)doreti_popl_ds) { 612 frame->tf_eip = (int)doreti_popl_ds_fault; 613 goto out; 614 } 615 if (frame->tf_eip == (int)doreti_popl_es) { 616 frame->tf_eip = (int)doreti_popl_es_fault; 617 goto out; 618 } 619 if (frame->tf_eip == (int)doreti_popl_fs) { 620 frame->tf_eip = (int)doreti_popl_fs_fault; 621 goto out; 622 } 623 if (curpcb->pcb_onfault != NULL) { 624 frame->tf_eip = 625 (int)curpcb->pcb_onfault; 626 goto out; 627 } 628 break; 629 630 case T_TSSFLT: 631 /* 632 * PSL_NT can be set in user mode and isn't cleared 633 * automatically when the kernel is entered. This 634 * causes a TSS fault when the kernel attempts to 635 * `iret' because the TSS link is uninitialized. We 636 * want to get this fault so that we can fix the 637 * problem here and not every time the kernel is 638 * entered. 639 */ 640 if (frame->tf_eflags & PSL_NT) { 641 frame->tf_eflags &= ~PSL_NT; 642 goto out; 643 } 644 break; 645 646 case T_TRCTRAP: /* trace trap */ 647 kernel_trctrap: 648 if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) { 649 /* 650 * We've just entered system mode via the 651 * syscall lcall. Continue single stepping 652 * silently until the syscall handler has 653 * saved the flags. 654 */ 655 goto out; 656 } 657 if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) { 658 /* 659 * The syscall handler has now saved the 660 * flags. Stop single stepping it. 661 */ 662 frame->tf_eflags &= ~PSL_T; 663 goto out; 664 } 665 /* 666 * Ignore debug register trace traps due to 667 * accesses in the user's address space, which 668 * can happen under several conditions such as 669 * if a user sets a watchpoint on a buffer and 670 * then passes that buffer to a system call. 671 * We still want to get TRCTRAPS for addresses 672 * in kernel space because that is useful when 673 * debugging the kernel. 674 */ 675 if (user_dbreg_trap() && 676 !(curpcb->pcb_flags & PCB_VM86CALL)) { 677 /* 678 * Reset breakpoint bits because the 679 * processor doesn't 680 */ 681 load_dr6(rdr6() & ~0xf); 682 goto out; 683 } 684 /* 685 * FALLTHROUGH (TRCTRAP kernel mode, kernel address) 686 */ 687 case T_BPTFLT: 688 /* 689 * If KDB is enabled, let it handle the debugger trap. 690 * Otherwise, debugger traps "can't happen". 691 */ 692 #ifdef KDB 693 /* XXX %dr6 is not quite reentrant. */ 694 dr6 = rdr6(); 695 load_dr6(dr6 & ~0x4000); 696 if (kdb_trap(type, dr6, frame)) 697 goto out; 698 #endif 699 break; 700 701 #ifdef DEV_ISA 702 case T_NMI: 703 #ifdef POWERFAIL_NMI 704 if (time_second - lastalert > 10) { 705 log(LOG_WARNING, "NMI: power fail\n"); 706 sysbeep(880, hz); 707 lastalert = time_second; 708 } 709 goto out; 710 #else /* !POWERFAIL_NMI */ 711 nmi_handle_intr(type, frame); 712 goto out; 713 #endif /* POWERFAIL_NMI */ 714 #endif /* DEV_ISA */ 715 } 716 717 trap_fatal(frame, eva); 718 goto out; 719 } 720 721 /* Translate fault for emulators (e.g. Linux) */ 722 if (*p->p_sysent->sv_transtrap) 723 i = (*p->p_sysent->sv_transtrap)(i, type); 724 725 ksiginfo_init_trap(&ksi); 726 ksi.ksi_signo = i; 727 ksi.ksi_code = ucode; 728 ksi.ksi_addr = (void *)addr; 729 ksi.ksi_trapno = type; 730 if (uprintf_signal) { 731 uprintf("pid %d comm %s: signal %d err %x code %d type %d " 732 "addr 0x%x esp 0x%08x eip 0x%08x " 733 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n", 734 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr, 735 frame->tf_esp, frame->tf_eip, 736 fubyte((void *)(frame->tf_eip + 0)), 737 fubyte((void *)(frame->tf_eip + 1)), 738 fubyte((void *)(frame->tf_eip + 2)), 739 fubyte((void *)(frame->tf_eip + 3)), 740 fubyte((void *)(frame->tf_eip + 4)), 741 fubyte((void *)(frame->tf_eip + 5)), 742 fubyte((void *)(frame->tf_eip + 6)), 743 fubyte((void *)(frame->tf_eip + 7))); 744 } 745 KASSERT((read_eflags() & PSL_I) != 0, ("interrupts disabled")); 746 trapsignal(td, &ksi); 747 748 #ifdef DEBUG 749 if (type <= MAX_TRAP_MSG) { 750 uprintf("fatal process exception: %s", 751 trap_msg[type]); 752 if ((type == T_PAGEFLT) || (type == T_PROTFLT)) 753 uprintf(", fault VA = 0x%lx", (u_long)eva); 754 uprintf("\n"); 755 } 756 #endif 757 758 user: 759 userret(td, frame); 760 KASSERT(PCB_USER_FPU(td->td_pcb), 761 ("Return from trap with kernel FPU ctx leaked")); 762 userout: 763 out: 764 return; 765 } 766 767 static int 768 trap_pfault(frame, usermode, eva) 769 struct trapframe *frame; 770 int usermode; 771 vm_offset_t eva; 772 { 773 vm_offset_t va; 774 vm_map_t map; 775 int rv = 0; 776 vm_prot_t ftype; 777 struct thread *td = curthread; 778 struct proc *p = td->td_proc; 779 780 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) { 781 /* 782 * Due to both processor errata and lazy TLB invalidation when 783 * access restrictions are removed from virtual pages, memory 784 * accesses that are allowed by the physical mapping layer may 785 * nonetheless cause one spurious page fault per virtual page. 786 * When the thread is executing a "no faulting" section that 787 * is bracketed by vm_fault_{disable,enable}_pagefaults(), 788 * every page fault is treated as a spurious page fault, 789 * unless it accesses the same virtual address as the most 790 * recent page fault within the same "no faulting" section. 791 */ 792 if (td->td_md.md_spurflt_addr != eva || 793 (td->td_pflags & TDP_RESETSPUR) != 0) { 794 /* 795 * Do nothing to the TLB. A stale TLB entry is 796 * flushed automatically by a page fault. 797 */ 798 td->td_md.md_spurflt_addr = eva; 799 td->td_pflags &= ~TDP_RESETSPUR; 800 return (0); 801 } 802 } else { 803 /* 804 * If we get a page fault while in a critical section, then 805 * it is most likely a fatal kernel page fault. The kernel 806 * is already going to panic trying to get a sleep lock to 807 * do the VM lookup, so just consider it a fatal trap so the 808 * kernel can print out a useful trap message and even get 809 * to the debugger. 810 * 811 * If we get a page fault while holding a non-sleepable 812 * lock, then it is most likely a fatal kernel page fault. 813 * If WITNESS is enabled, then it's going to whine about 814 * bogus LORs with various VM locks, so just skip to the 815 * fatal trap handling directly. 816 */ 817 if (td->td_critnest != 0 || 818 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL, 819 "Kernel page fault") != 0) { 820 trap_fatal(frame, eva); 821 return (-1); 822 } 823 } 824 va = trunc_page(eva); 825 if (va >= KERNBASE) { 826 /* 827 * Don't allow user-mode faults in kernel address space. 828 * An exception: if the faulting address is the invalid 829 * instruction entry in the IDT, then the Intel Pentium 830 * F00F bug workaround was triggered, and we need to 831 * treat it is as an illegal instruction, and not a page 832 * fault. 833 */ 834 #if defined(I586_CPU) && !defined(NO_F00F_HACK) 835 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) 836 return (-2); 837 #endif 838 if (usermode) 839 goto nogo; 840 841 map = kernel_map; 842 } else { 843 map = &p->p_vmspace->vm_map; 844 845 /* 846 * When accessing a user-space address, kernel must be 847 * ready to accept the page fault, and provide a 848 * handling routine. Since accessing the address 849 * without the handler is a bug, do not try to handle 850 * it normally, and panic immediately. 851 */ 852 if (!usermode && (td->td_intr_nesting_level != 0 || 853 curpcb->pcb_onfault == NULL)) { 854 trap_fatal(frame, eva); 855 return (-1); 856 } 857 } 858 859 /* 860 * If the trap was caused by errant bits in the PTE then panic. 861 */ 862 if (frame->tf_err & PGEX_RSV) { 863 trap_fatal(frame, eva); 864 return (-1); 865 } 866 867 /* 868 * PGEX_I is defined only if the execute disable bit capability is 869 * supported and enabled. 870 */ 871 if (frame->tf_err & PGEX_W) 872 ftype = VM_PROT_WRITE; 873 #if defined(PAE) || defined(PAE_TABLES) 874 else if ((frame->tf_err & PGEX_I) && pg_nx != 0) 875 ftype = VM_PROT_EXECUTE; 876 #endif 877 else 878 ftype = VM_PROT_READ; 879 880 /* Fault in the page. */ 881 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 882 if (rv == KERN_SUCCESS) { 883 #ifdef HWPMC_HOOKS 884 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) { 885 PMC_SOFT_CALL_TF( , , page_fault, all, frame); 886 if (ftype == VM_PROT_READ) 887 PMC_SOFT_CALL_TF( , , page_fault, read, 888 frame); 889 else 890 PMC_SOFT_CALL_TF( , , page_fault, write, 891 frame); 892 } 893 #endif 894 return (0); 895 } 896 nogo: 897 if (!usermode) { 898 if (td->td_intr_nesting_level == 0 && 899 curpcb->pcb_onfault != NULL) { 900 frame->tf_eip = (int)curpcb->pcb_onfault; 901 return (0); 902 } 903 trap_fatal(frame, eva); 904 return (-1); 905 } 906 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV); 907 } 908 909 static void 910 trap_fatal(frame, eva) 911 struct trapframe *frame; 912 vm_offset_t eva; 913 { 914 int code, ss, esp; 915 u_int type; 916 struct soft_segment_descriptor softseg; 917 char *msg; 918 919 code = frame->tf_err; 920 type = frame->tf_trapno; 921 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg); 922 923 if (type <= MAX_TRAP_MSG) 924 msg = trap_msg[type]; 925 else 926 msg = "UNKNOWN"; 927 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg, 928 frame->tf_eflags & PSL_VM ? "vm86" : 929 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel"); 930 #ifdef SMP 931 /* two separate prints in case of a trap on an unmapped page */ 932 printf("cpuid = %d; ", PCPU_GET(cpuid)); 933 printf("apic id = %02x\n", PCPU_GET(apic_id)); 934 #endif 935 if (type == T_PAGEFLT) { 936 printf("fault virtual address = 0x%x\n", eva); 937 printf("fault code = %s %s%s, %s\n", 938 code & PGEX_U ? "user" : "supervisor", 939 code & PGEX_W ? "write" : "read", 940 #if defined(PAE) || defined(PAE_TABLES) 941 pg_nx != 0 ? 942 (code & PGEX_I ? " instruction" : " data") : 943 #endif 944 "", 945 code & PGEX_RSV ? "reserved bits in PTE" : 946 code & PGEX_P ? "protection violation" : "page not present"); 947 } 948 printf("instruction pointer = 0x%x:0x%x\n", 949 frame->tf_cs & 0xffff, frame->tf_eip); 950 if (TF_HAS_STACKREGS(frame)) { 951 ss = frame->tf_ss & 0xffff; 952 esp = frame->tf_esp; 953 } else { 954 ss = GSEL(GDATA_SEL, SEL_KPL); 955 esp = (int)&frame->tf_esp; 956 } 957 printf("stack pointer = 0x%x:0x%x\n", ss, esp); 958 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp); 959 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n", 960 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type); 961 printf(" = DPL %d, pres %d, def32 %d, gran %d\n", 962 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32, 963 softseg.ssd_gran); 964 printf("processor eflags = "); 965 if (frame->tf_eflags & PSL_T) 966 printf("trace trap, "); 967 if (frame->tf_eflags & PSL_I) 968 printf("interrupt enabled, "); 969 if (frame->tf_eflags & PSL_NT) 970 printf("nested task, "); 971 if (frame->tf_eflags & PSL_RF) 972 printf("resume, "); 973 if (frame->tf_eflags & PSL_VM) 974 printf("vm86, "); 975 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12); 976 printf("current process = %d (%s)\n", 977 curproc->p_pid, curthread->td_name); 978 979 #ifdef KDB 980 if (debugger_on_panic || kdb_active) { 981 frame->tf_err = eva; /* smuggle fault address to ddb */ 982 if (kdb_trap(type, 0, frame)) { 983 frame->tf_err = code; /* restore error code */ 984 return; 985 } 986 frame->tf_err = code; /* restore error code */ 987 } 988 #endif 989 printf("trap number = %d\n", type); 990 if (type <= MAX_TRAP_MSG) 991 panic("%s", trap_msg[type]); 992 else 993 panic("unknown/reserved trap"); 994 } 995 996 /* 997 * Double fault handler. Called when a fault occurs while writing 998 * a frame for a trap/exception onto the stack. This usually occurs 999 * when the stack overflows (such is the case with infinite recursion, 1000 * for example). 1001 * 1002 * XXX Note that the current PTD gets replaced by IdlePTD when the 1003 * task switch occurs. This means that the stack that was active at 1004 * the time of the double fault is not available at <kstack> unless 1005 * the machine was idle when the double fault occurred. The downside 1006 * of this is that "trace <ebp>" in ddb won't work. 1007 */ 1008 void 1009 dblfault_handler() 1010 { 1011 #ifdef KDTRACE_HOOKS 1012 if (dtrace_doubletrap_func != NULL) 1013 (*dtrace_doubletrap_func)(); 1014 #endif 1015 printf("\nFatal double fault:\n"); 1016 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip)); 1017 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp)); 1018 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp)); 1019 #ifdef SMP 1020 /* two separate prints in case of a trap on an unmapped page */ 1021 printf("cpuid = %d; ", PCPU_GET(cpuid)); 1022 printf("apic id = %02x\n", PCPU_GET(apic_id)); 1023 #endif 1024 panic("double fault"); 1025 } 1026 1027 int 1028 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa) 1029 { 1030 struct proc *p; 1031 struct trapframe *frame; 1032 caddr_t params; 1033 long tmp; 1034 int error; 1035 1036 p = td->td_proc; 1037 frame = td->td_frame; 1038 1039 params = (caddr_t)frame->tf_esp + sizeof(int); 1040 sa->code = frame->tf_eax; 1041 1042 /* 1043 * Need to check if this is a 32 bit or 64 bit syscall. 1044 */ 1045 if (sa->code == SYS_syscall) { 1046 /* 1047 * Code is first argument, followed by actual args. 1048 */ 1049 error = fueword(params, &tmp); 1050 if (error == -1) 1051 return (EFAULT); 1052 sa->code = tmp; 1053 params += sizeof(int); 1054 } else if (sa->code == SYS___syscall) { 1055 /* 1056 * Like syscall, but code is a quad, so as to maintain 1057 * quad alignment for the rest of the arguments. 1058 */ 1059 error = fueword(params, &tmp); 1060 if (error == -1) 1061 return (EFAULT); 1062 sa->code = tmp; 1063 params += sizeof(quad_t); 1064 } 1065 1066 if (p->p_sysent->sv_mask) 1067 sa->code &= p->p_sysent->sv_mask; 1068 if (sa->code >= p->p_sysent->sv_size) 1069 sa->callp = &p->p_sysent->sv_table[0]; 1070 else 1071 sa->callp = &p->p_sysent->sv_table[sa->code]; 1072 sa->narg = sa->callp->sy_narg; 1073 1074 if (params != NULL && sa->narg != 0) 1075 error = copyin(params, (caddr_t)sa->args, 1076 (u_int)(sa->narg * sizeof(int))); 1077 else 1078 error = 0; 1079 1080 if (error == 0) { 1081 td->td_retval[0] = 0; 1082 td->td_retval[1] = frame->tf_edx; 1083 } 1084 1085 return (error); 1086 } 1087 1088 #include "../../kern/subr_syscall.c" 1089 1090 /* 1091 * syscall - system call request C handler. A system call is 1092 * essentially treated as a trap by reusing the frame layout. 1093 */ 1094 void 1095 syscall(struct trapframe *frame) 1096 { 1097 struct thread *td; 1098 struct syscall_args sa; 1099 register_t orig_tf_eflags; 1100 int error; 1101 ksiginfo_t ksi; 1102 1103 #ifdef DIAGNOSTIC 1104 if (!(TRAPF_USERMODE(frame) && 1105 (curpcb->pcb_flags & PCB_VM86CALL) == 0)) { 1106 panic("syscall"); 1107 /* NOT REACHED */ 1108 } 1109 #endif 1110 orig_tf_eflags = frame->tf_eflags; 1111 1112 td = curthread; 1113 td->td_frame = frame; 1114 1115 error = syscallenter(td, &sa); 1116 1117 /* 1118 * Traced syscall. 1119 */ 1120 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) { 1121 frame->tf_eflags &= ~PSL_T; 1122 ksiginfo_init_trap(&ksi); 1123 ksi.ksi_signo = SIGTRAP; 1124 ksi.ksi_code = TRAP_TRACE; 1125 ksi.ksi_addr = (void *)frame->tf_eip; 1126 trapsignal(td, &ksi); 1127 } 1128 1129 KASSERT(PCB_USER_FPU(td->td_pcb), 1130 ("System call %s returning with kernel FPU ctx leaked", 1131 syscallname(td->td_proc, sa.code))); 1132 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td), 1133 ("System call %s returning with mangled pcb_save", 1134 syscallname(td->td_proc, sa.code))); 1135 1136 syscallret(td, error, &sa); 1137 } 1138