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