trap.c revision 287147
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: releng/9.3/sys/amd64/amd64/trap.c 287147 2015-08-25 20:49:05Z delphij $"); 42 43/* 44 * AMD64 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 54#include <sys/param.h> 55#include <sys/bus.h> 56#include <sys/systm.h> 57#include <sys/proc.h> 58#include <sys/pioctl.h> 59#include <sys/ptrace.h> 60#include <sys/kdb.h> 61#include <sys/kernel.h> 62#include <sys/ktr.h> 63#include <sys/lock.h> 64#include <sys/mutex.h> 65#include <sys/resourcevar.h> 66#include <sys/signalvar.h> 67#include <sys/syscall.h> 68#include <sys/sysctl.h> 69#include <sys/sysent.h> 70#include <sys/uio.h> 71#include <sys/vmmeter.h> 72#ifdef HWPMC_HOOKS 73#include <sys/pmckern.h> 74PMC_SOFT_DEFINE( , , page_fault, all); 75PMC_SOFT_DEFINE( , , page_fault, read); 76PMC_SOFT_DEFINE( , , page_fault, write); 77#endif 78 79#include <vm/vm.h> 80#include <vm/vm_param.h> 81#include <vm/pmap.h> 82#include <vm/vm_kern.h> 83#include <vm/vm_map.h> 84#include <vm/vm_page.h> 85#include <vm/vm_extern.h> 86 87#include <machine/cpu.h> 88#include <machine/intr_machdep.h> 89#include <x86/mca.h> 90#include <machine/md_var.h> 91#include <machine/pcb.h> 92#ifdef SMP 93#include <machine/smp.h> 94#endif 95#include <machine/tss.h> 96 97#ifdef KDTRACE_HOOKS 98#include <sys/dtrace_bsd.h> 99 100/* 101 * This is a hook which is initialised by the dtrace module 102 * to handle traps which might occur during DTrace probe 103 * execution. 104 */ 105dtrace_trap_func_t dtrace_trap_func; 106 107dtrace_doubletrap_func_t dtrace_doubletrap_func; 108 109/* 110 * This is a hook which is initialised by the systrace module 111 * when it is loaded. This keeps the DTrace syscall provider 112 * implementation opaque. 113 */ 114systrace_probe_func_t systrace_probe_func; 115 116/* 117 * These hooks are necessary for the pid and usdt providers. 118 */ 119dtrace_pid_probe_ptr_t dtrace_pid_probe_ptr; 120dtrace_return_probe_ptr_t dtrace_return_probe_ptr; 121#endif 122 123extern void trap(struct trapframe *frame); 124extern void syscall(struct trapframe *frame); 125void dblfault_handler(struct trapframe *frame); 126 127static int trap_pfault(struct trapframe *, int); 128static void trap_fatal(struct trapframe *, vm_offset_t); 129 130#define MAX_TRAP_MSG 32 131static char *trap_msg[] = { 132 "", /* 0 unused */ 133 "privileged instruction fault", /* 1 T_PRIVINFLT */ 134 "", /* 2 unused */ 135 "breakpoint instruction fault", /* 3 T_BPTFLT */ 136 "", /* 4 unused */ 137 "", /* 5 unused */ 138 "arithmetic trap", /* 6 T_ARITHTRAP */ 139 "", /* 7 unused */ 140 "", /* 8 unused */ 141 "general protection fault", /* 9 T_PROTFLT */ 142 "trace trap", /* 10 T_TRCTRAP */ 143 "", /* 11 unused */ 144 "page fault", /* 12 T_PAGEFLT */ 145 "", /* 13 unused */ 146 "alignment fault", /* 14 T_ALIGNFLT */ 147 "", /* 15 unused */ 148 "", /* 16 unused */ 149 "", /* 17 unused */ 150 "integer divide fault", /* 18 T_DIVIDE */ 151 "non-maskable interrupt trap", /* 19 T_NMI */ 152 "overflow trap", /* 20 T_OFLOW */ 153 "FPU bounds check fault", /* 21 T_BOUND */ 154 "FPU device not available", /* 22 T_DNA */ 155 "double fault", /* 23 T_DOUBLEFLT */ 156 "FPU operand fetch fault", /* 24 T_FPOPFLT */ 157 "invalid TSS fault", /* 25 T_TSSFLT */ 158 "segment not present fault", /* 26 T_SEGNPFLT */ 159 "stack fault", /* 27 T_STKFLT */ 160 "machine check trap", /* 28 T_MCHK */ 161 "SIMD floating-point exception", /* 29 T_XMMFLT */ 162 "reserved (unknown) fault", /* 30 T_RESERVED */ 163 "", /* 31 unused (reserved) */ 164 "DTrace pid return trap", /* 32 T_DTRACE_RET */ 165}; 166 167#ifdef KDB 168static int kdb_on_nmi = 1; 169SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW, 170 &kdb_on_nmi, 0, "Go to KDB on NMI"); 171TUNABLE_INT("machdep.kdb_on_nmi", &kdb_on_nmi); 172#endif 173static int panic_on_nmi = 1; 174SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW, 175 &panic_on_nmi, 0, "Panic on NMI"); 176TUNABLE_INT("machdep.panic_on_nmi", &panic_on_nmi); 177static int prot_fault_translation; 178SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW, 179 &prot_fault_translation, 0, 180 "Select signal to deliver on protection fault"); 181static int uprintf_signal; 182SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RW, 183 &uprintf_signal, 0, 184 "Print debugging information on trap signal to ctty"); 185 186/* 187 * Exception, fault, and trap interface to the FreeBSD kernel. 188 * This common code is called from assembly language IDT gate entry 189 * routines that prepare a suitable stack frame, and restore this 190 * frame after the exception has been processed. 191 */ 192 193void 194trap(struct trapframe *frame) 195{ 196 struct thread *td = curthread; 197 struct proc *p = td->td_proc; 198 int i = 0, ucode = 0, code; 199 u_int type; 200 register_t addr = 0; 201 ksiginfo_t ksi; 202 203 PCPU_INC(cnt.v_trap); 204 type = frame->tf_trapno; 205 206#ifdef SMP 207 /* Handler for NMI IPIs used for stopping CPUs. */ 208 if (type == T_NMI) { 209 if (ipi_nmi_handler() == 0) 210 goto out; 211 } 212#endif /* SMP */ 213 214#ifdef KDB 215 if (kdb_active) { 216 kdb_reenter(); 217 goto out; 218 } 219#endif 220 221 if (type == T_RESERVED) { 222 trap_fatal(frame, 0); 223 goto out; 224 } 225 226#ifdef HWPMC_HOOKS 227 /* 228 * CPU PMCs interrupt using an NMI. If the PMC module is 229 * active, pass the 'rip' value to the PMC module's interrupt 230 * handler. A return value of '1' from the handler means that 231 * the NMI was handled by it and we can return immediately. 232 */ 233 if (type == T_NMI && pmc_intr && 234 (*pmc_intr)(PCPU_GET(cpuid), frame)) 235 goto out; 236#endif 237 238 if (type == T_MCHK) { 239 mca_intr(); 240 goto out; 241 } 242 243#ifdef KDTRACE_HOOKS 244 /* 245 * A trap can occur while DTrace executes a probe. Before 246 * executing the probe, DTrace blocks re-scheduling and sets 247 * a flag in it's per-cpu flags to indicate that it doesn't 248 * want to fault. On returning from the probe, the no-fault 249 * flag is cleared and finally re-scheduling is enabled. 250 * 251 * If the DTrace kernel module has registered a trap handler, 252 * call it and if it returns non-zero, assume that it has 253 * handled the trap and modified the trap frame so that this 254 * function can return normally. 255 */ 256 if (type == T_DTRACE_RET || type == T_BPTFLT) { 257 struct reg regs; 258 259 fill_frame_regs(frame, ®s); 260 if (type == T_BPTFLT && 261 dtrace_pid_probe_ptr != NULL && 262 dtrace_pid_probe_ptr(®s) == 0) 263 goto out; 264 else if (type == T_DTRACE_RET && 265 dtrace_return_probe_ptr != NULL && 266 dtrace_return_probe_ptr(®s) == 0) 267 goto out; 268 } 269 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type)) 270 goto out; 271#endif 272 273 if ((frame->tf_rflags & PSL_I) == 0) { 274 /* 275 * Buggy application or kernel code has disabled 276 * interrupts and then trapped. Enabling interrupts 277 * now is wrong, but it is better than running with 278 * interrupts disabled until they are accidentally 279 * enabled later. 280 */ 281 if (ISPL(frame->tf_cs) == SEL_UPL) 282 uprintf( 283 "pid %ld (%s): trap %d with interrupts disabled\n", 284 (long)curproc->p_pid, curthread->td_name, type); 285 else if (type != T_NMI && type != T_BPTFLT && 286 type != T_TRCTRAP) { 287 /* 288 * XXX not quite right, since this may be for a 289 * multiple fault in user mode. 290 */ 291 printf("kernel trap %d with interrupts disabled\n", 292 type); 293 294 /* 295 * We shouldn't enable interrupts while holding a 296 * spin lock. 297 */ 298 if (td->td_md.md_spinlock_count == 0) 299 enable_intr(); 300 } 301 } 302 303 code = frame->tf_err; 304 305 if (ISPL(frame->tf_cs) == SEL_UPL) { 306 /* user trap */ 307 308 td->td_pticks = 0; 309 td->td_frame = frame; 310 addr = frame->tf_rip; 311 if (td->td_ucred != p->p_ucred) 312 cred_update_thread(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 frame->tf_rflags &= ~PSL_T; 324 i = SIGTRAP; 325 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT); 326 break; 327 328 case T_ARITHTRAP: /* arithmetic trap */ 329 ucode = fputrap_x87(); 330 if (ucode == -1) 331 goto userout; 332 i = SIGFPE; 333 break; 334 335 case T_PROTFLT: /* general protection fault */ 336 i = SIGBUS; 337 ucode = BUS_OBJERR; 338 break; 339 case T_STKFLT: /* stack fault */ 340 case T_SEGNPFLT: /* segment not present fault */ 341 i = SIGBUS; 342 ucode = BUS_ADRERR; 343 break; 344 case T_TSSFLT: /* invalid TSS fault */ 345 i = SIGBUS; 346 ucode = BUS_OBJERR; 347 break; 348 case T_DOUBLEFLT: /* double fault */ 349 default: 350 i = SIGBUS; 351 ucode = BUS_OBJERR; 352 break; 353 354 case T_PAGEFLT: /* page fault */ 355 addr = frame->tf_addr; 356 i = trap_pfault(frame, TRUE); 357 if (i == -1) 358 goto userout; 359 if (i == 0) 360 goto user; 361 362 if (i == SIGSEGV) 363 ucode = SEGV_MAPERR; 364 else { 365 if (prot_fault_translation == 0) { 366 /* 367 * Autodetect. 368 * This check also covers the images 369 * without the ABI-tag ELF note. 370 */ 371 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD 372 && p->p_osrel >= P_OSREL_SIGSEGV) { 373 i = SIGSEGV; 374 ucode = SEGV_ACCERR; 375 } else { 376 i = SIGBUS; 377 ucode = BUS_PAGE_FAULT; 378 } 379 } else if (prot_fault_translation == 1) { 380 /* 381 * Always compat mode. 382 */ 383 i = SIGBUS; 384 ucode = BUS_PAGE_FAULT; 385 } else { 386 /* 387 * Always SIGSEGV mode. 388 */ 389 i = SIGSEGV; 390 ucode = SEGV_ACCERR; 391 } 392 } 393 break; 394 395 case T_DIVIDE: /* integer divide fault */ 396 ucode = FPE_INTDIV; 397 i = SIGFPE; 398 break; 399 400#ifdef DEV_ISA 401 case T_NMI: 402 /* machine/parity/power fail/"kitchen sink" faults */ 403 if (isa_nmi(code) == 0) { 404#ifdef KDB 405 /* 406 * NMI can be hooked up to a pushbutton 407 * for debugging. 408 */ 409 if (kdb_on_nmi) { 410 printf ("NMI ... going to debugger\n"); 411 kdb_trap(type, 0, frame); 412 } 413#endif /* KDB */ 414 goto userout; 415 } else if (panic_on_nmi) 416 panic("NMI indicates hardware failure"); 417 break; 418#endif /* DEV_ISA */ 419 420 case T_OFLOW: /* integer overflow fault */ 421 ucode = FPE_INTOVF; 422 i = SIGFPE; 423 break; 424 425 case T_BOUND: /* bounds check fault */ 426 ucode = FPE_FLTSUB; 427 i = SIGFPE; 428 break; 429 430 case T_DNA: 431 /* transparent fault (due to context switch "late") */ 432 KASSERT(PCB_USER_FPU(td->td_pcb), 433 ("kernel FPU ctx has leaked")); 434 fpudna(); 435 goto userout; 436 437 case T_FPOPFLT: /* FPU operand fetch fault */ 438 ucode = ILL_COPROC; 439 i = SIGILL; 440 break; 441 442 case T_XMMFLT: /* SIMD floating-point exception */ 443 ucode = fputrap_sse(); 444 if (ucode == -1) 445 goto userout; 446 i = SIGFPE; 447 break; 448 } 449 } else { 450 /* kernel trap */ 451 452 KASSERT(cold || td->td_ucred != NULL, 453 ("kernel trap doesn't have ucred")); 454 switch (type) { 455 case T_PAGEFLT: /* page fault */ 456 (void) trap_pfault(frame, FALSE); 457 goto out; 458 459 case T_DNA: 460 KASSERT(!PCB_USER_FPU(td->td_pcb), 461 ("Unregistered use of FPU in kernel")); 462 fpudna(); 463 goto out; 464 465 case T_ARITHTRAP: /* arithmetic trap */ 466 case T_XMMFLT: /* SIMD floating-point exception */ 467 case T_FPOPFLT: /* FPU operand fetch fault */ 468 /* 469 * XXXKIB for now disable any FPU traps in kernel 470 * handler registration seems to be overkill 471 */ 472 trap_fatal(frame, 0); 473 goto out; 474 475 case T_STKFLT: /* stack fault */ 476 case T_PROTFLT: /* general protection fault */ 477 case T_SEGNPFLT: /* segment not present fault */ 478 if (td->td_intr_nesting_level != 0) 479 break; 480 481 /* 482 * Invalid segment selectors and out of bounds 483 * %rip's and %rsp's can be set up in user mode. 484 * This causes a fault in kernel mode when the 485 * kernel tries to return to user mode. We want 486 * to get this fault so that we can fix the 487 * problem here and not have to check all the 488 * selectors and pointers when the user changes 489 * them. 490 */ 491 if (frame->tf_rip == (long)doreti_iret) { 492 frame->tf_rip = (long)doreti_iret_fault; 493 goto out; 494 } 495 if (frame->tf_rip == (long)ld_ds) { 496 frame->tf_rip = (long)ds_load_fault; 497 goto out; 498 } 499 if (frame->tf_rip == (long)ld_es) { 500 frame->tf_rip = (long)es_load_fault; 501 goto out; 502 } 503 if (frame->tf_rip == (long)ld_fs) { 504 frame->tf_rip = (long)fs_load_fault; 505 goto out; 506 } 507 if (frame->tf_rip == (long)ld_gs) { 508 frame->tf_rip = (long)gs_load_fault; 509 goto out; 510 } 511 if (frame->tf_rip == (long)ld_gsbase) { 512 frame->tf_rip = (long)gsbase_load_fault; 513 goto out; 514 } 515 if (frame->tf_rip == (long)ld_fsbase) { 516 frame->tf_rip = (long)fsbase_load_fault; 517 goto out; 518 } 519 if (curpcb->pcb_onfault != NULL) { 520 frame->tf_rip = (long)curpcb->pcb_onfault; 521 goto out; 522 } 523 break; 524 525 case T_TSSFLT: 526 /* 527 * PSL_NT can be set in user mode and isn't cleared 528 * automatically when the kernel is entered. This 529 * causes a TSS fault when the kernel attempts to 530 * `iret' because the TSS link is uninitialized. We 531 * want to get this fault so that we can fix the 532 * problem here and not every time the kernel is 533 * entered. 534 */ 535 if (frame->tf_rflags & PSL_NT) { 536 frame->tf_rflags &= ~PSL_NT; 537 goto out; 538 } 539 break; 540 541 case T_TRCTRAP: /* trace trap */ 542 /* 543 * Ignore debug register trace traps due to 544 * accesses in the user's address space, which 545 * can happen under several conditions such as 546 * if a user sets a watchpoint on a buffer and 547 * then passes that buffer to a system call. 548 * We still want to get TRCTRAPS for addresses 549 * in kernel space because that is useful when 550 * debugging the kernel. 551 */ 552 if (user_dbreg_trap()) { 553 /* 554 * Reset breakpoint bits because the 555 * processor doesn't 556 */ 557 /* XXX check upper bits here */ 558 load_dr6(rdr6() & 0xfffffff0); 559 goto out; 560 } 561 /* 562 * FALLTHROUGH (TRCTRAP kernel mode, kernel address) 563 */ 564 case T_BPTFLT: 565 /* 566 * If KDB is enabled, let it handle the debugger trap. 567 * Otherwise, debugger traps "can't happen". 568 */ 569#ifdef KDB 570 if (kdb_trap(type, 0, frame)) 571 goto out; 572#endif 573 break; 574 575#ifdef DEV_ISA 576 case T_NMI: 577 /* machine/parity/power fail/"kitchen sink" faults */ 578 if (isa_nmi(code) == 0) { 579#ifdef KDB 580 /* 581 * NMI can be hooked up to a pushbutton 582 * for debugging. 583 */ 584 if (kdb_on_nmi) { 585 printf ("NMI ... going to debugger\n"); 586 kdb_trap(type, 0, frame); 587 } 588#endif /* KDB */ 589 goto out; 590 } else if (panic_on_nmi == 0) 591 goto out; 592 /* FALLTHROUGH */ 593#endif /* DEV_ISA */ 594 } 595 596 trap_fatal(frame, 0); 597 goto out; 598 } 599 600 /* Translate fault for emulators (e.g. Linux) */ 601 if (*p->p_sysent->sv_transtrap) 602 i = (*p->p_sysent->sv_transtrap)(i, type); 603 604 ksiginfo_init_trap(&ksi); 605 ksi.ksi_signo = i; 606 ksi.ksi_code = ucode; 607 ksi.ksi_trapno = type; 608 ksi.ksi_addr = (void *)addr; 609 if (uprintf_signal) { 610 uprintf("pid %d comm %s: signal %d err %lx code %d type %d " 611 "addr 0x%lx rsp 0x%lx rip 0x%lx " 612 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n", 613 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr, 614 frame->tf_rsp, frame->tf_rip, 615 fubyte((void *)(frame->tf_rip + 0)), 616 fubyte((void *)(frame->tf_rip + 1)), 617 fubyte((void *)(frame->tf_rip + 2)), 618 fubyte((void *)(frame->tf_rip + 3)), 619 fubyte((void *)(frame->tf_rip + 4)), 620 fubyte((void *)(frame->tf_rip + 5)), 621 fubyte((void *)(frame->tf_rip + 6)), 622 fubyte((void *)(frame->tf_rip + 7))); 623 } 624 KASSERT((read_rflags() & PSL_I) != 0, ("interrupts disabled")); 625 trapsignal(td, &ksi); 626 627user: 628 userret(td, frame); 629 mtx_assert(&Giant, MA_NOTOWNED); 630 KASSERT(PCB_USER_FPU(td->td_pcb), 631 ("Return from trap with kernel FPU ctx leaked")); 632userout: 633out: 634 return; 635} 636 637static int 638trap_pfault(frame, usermode) 639 struct trapframe *frame; 640 int usermode; 641{ 642 vm_offset_t va; 643 struct vmspace *vm = NULL; 644 vm_map_t map; 645 int rv = 0; 646 vm_prot_t ftype; 647 struct thread *td = curthread; 648 struct proc *p = td->td_proc; 649 vm_offset_t eva = frame->tf_addr; 650 651 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) { 652 /* 653 * Due to both processor errata and lazy TLB invalidation when 654 * access restrictions are removed from virtual pages, memory 655 * accesses that are allowed by the physical mapping layer may 656 * nonetheless cause one spurious page fault per virtual page. 657 * When the thread is executing a "no faulting" section that 658 * is bracketed by vm_fault_{disable,enable}_pagefaults(), 659 * every page fault is treated as a spurious page fault, 660 * unless it accesses the same virtual address as the most 661 * recent page fault within the same "no faulting" section. 662 */ 663 if (td->td_md.md_spurflt_addr != eva || 664 (td->td_pflags & TDP_RESETSPUR) != 0) { 665 /* 666 * Do nothing to the TLB. A stale TLB entry is 667 * flushed automatically by a page fault. 668 */ 669 td->td_md.md_spurflt_addr = eva; 670 td->td_pflags &= ~TDP_RESETSPUR; 671 return (0); 672 } 673 } else { 674 /* 675 * If we get a page fault while in a critical section, then 676 * it is most likely a fatal kernel page fault. The kernel 677 * is already going to panic trying to get a sleep lock to 678 * do the VM lookup, so just consider it a fatal trap so the 679 * kernel can print out a useful trap message and even get 680 * to the debugger. 681 * 682 * If we get a page fault while holding a non-sleepable 683 * lock, then it is most likely a fatal kernel page fault. 684 * If WITNESS is enabled, then it's going to whine about 685 * bogus LORs with various VM locks, so just skip to the 686 * fatal trap handling directly. 687 */ 688 if (td->td_critnest != 0 || 689 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL, 690 "Kernel page fault") != 0) { 691 trap_fatal(frame, eva); 692 return (-1); 693 } 694 } 695 va = trunc_page(eva); 696 if (va >= VM_MIN_KERNEL_ADDRESS) { 697 /* 698 * Don't allow user-mode faults in kernel address space. 699 */ 700 if (usermode) 701 goto nogo; 702 703 map = kernel_map; 704 } else { 705 /* 706 * This is a fault on non-kernel virtual memory. 707 * vm is initialized above to NULL. If curproc is NULL 708 * or curproc->p_vmspace is NULL the fault is fatal. 709 */ 710 if (p != NULL) 711 vm = p->p_vmspace; 712 713 if (vm == NULL) 714 goto nogo; 715 716 map = &vm->vm_map; 717 718 /* 719 * When accessing a usermode address, kernel must be 720 * ready to accept the page fault, and provide a 721 * handling routine. Since accessing the address 722 * without the handler is a bug, do not try to handle 723 * it normally, and panic immediately. 724 */ 725 if (!usermode && (td->td_intr_nesting_level != 0 || 726 curpcb->pcb_onfault == NULL)) { 727 trap_fatal(frame, eva); 728 return (-1); 729 } 730 } 731 732 /* 733 * PGEX_I is defined only if the execute disable bit capability is 734 * supported and enabled. 735 */ 736 if (frame->tf_err & PGEX_W) 737 ftype = VM_PROT_WRITE; 738 else if ((frame->tf_err & PGEX_I) && pg_nx != 0) 739 ftype = VM_PROT_EXECUTE; 740 else 741 ftype = VM_PROT_READ; 742 743 if (map != kernel_map) { 744 /* 745 * Keep swapout from messing with us during this 746 * critical time. 747 */ 748 PROC_LOCK(p); 749 ++p->p_lock; 750 PROC_UNLOCK(p); 751 752 /* Fault in the user page: */ 753 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 754 755 PROC_LOCK(p); 756 --p->p_lock; 757 PROC_UNLOCK(p); 758 } else { 759 /* 760 * Don't have to worry about process locking or stacks in the 761 * kernel. 762 */ 763 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 764 } 765 if (rv == KERN_SUCCESS) { 766#ifdef HWPMC_HOOKS 767 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) { 768 PMC_SOFT_CALL_TF( , , page_fault, all, frame); 769 if (ftype == VM_PROT_READ) 770 PMC_SOFT_CALL_TF( , , page_fault, read, 771 frame); 772 else 773 PMC_SOFT_CALL_TF( , , page_fault, write, 774 frame); 775 } 776#endif 777 return (0); 778 } 779nogo: 780 if (!usermode) { 781 if (td->td_intr_nesting_level == 0 && 782 curpcb->pcb_onfault != NULL) { 783 frame->tf_rip = (long)curpcb->pcb_onfault; 784 return (0); 785 } 786 if ((td->td_pflags & TDP_DEVMEMIO) != 0) { 787 KASSERT(curpcb->pcb_onfault != NULL, 788 ("/dev/mem without pcb_onfault")); 789 frame->tf_rip = (long)curpcb->pcb_onfault; 790 return (0); 791 } 792 trap_fatal(frame, eva); 793 return (-1); 794 } 795 796 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV); 797} 798 799static void 800trap_fatal(frame, eva) 801 struct trapframe *frame; 802 vm_offset_t eva; 803{ 804 int code, ss; 805 u_int type; 806 long esp; 807 struct soft_segment_descriptor softseg; 808 char *msg; 809 810 code = frame->tf_err; 811 type = frame->tf_trapno; 812 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)], 813 &softseg); 814 815 if (type <= MAX_TRAP_MSG) 816 msg = trap_msg[type]; 817 else 818 msg = "UNKNOWN"; 819 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg, 820 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel"); 821#ifdef SMP 822 /* two separate prints in case of a trap on an unmapped page */ 823 printf("cpuid = %d; ", PCPU_GET(cpuid)); 824 printf("apic id = %02x\n", PCPU_GET(apic_id)); 825#endif 826 if (type == T_PAGEFLT) { 827 printf("fault virtual address = 0x%lx\n", eva); 828 printf("fault code = %s %s %s, %s\n", 829 code & PGEX_U ? "user" : "supervisor", 830 code & PGEX_W ? "write" : "read", 831 code & PGEX_I ? "instruction" : "data", 832 code & PGEX_P ? "protection violation" : "page not present"); 833 } 834 printf("instruction pointer = 0x%lx:0x%lx\n", 835 frame->tf_cs & 0xffff, frame->tf_rip); 836 if (ISPL(frame->tf_cs) == SEL_UPL) { 837 ss = frame->tf_ss & 0xffff; 838 esp = frame->tf_rsp; 839 } else { 840 ss = GSEL(GDATA_SEL, SEL_KPL); 841 esp = (long)&frame->tf_rsp; 842 } 843 printf("stack pointer = 0x%x:0x%lx\n", ss, esp); 844 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp); 845 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n", 846 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type); 847 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n", 848 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32, 849 softseg.ssd_gran); 850 printf("processor eflags = "); 851 if (frame->tf_rflags & PSL_T) 852 printf("trace trap, "); 853 if (frame->tf_rflags & PSL_I) 854 printf("interrupt enabled, "); 855 if (frame->tf_rflags & PSL_NT) 856 printf("nested task, "); 857 if (frame->tf_rflags & PSL_RF) 858 printf("resume, "); 859 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12); 860 printf("current process = "); 861 if (curproc) { 862 printf("%lu (%s)\n", 863 (u_long)curproc->p_pid, curthread->td_name ? 864 curthread->td_name : ""); 865 } else { 866 printf("Idle\n"); 867 } 868 869#ifdef KDB 870 if (debugger_on_panic || kdb_active) 871 if (kdb_trap(type, 0, frame)) 872 return; 873#endif 874 printf("trap number = %d\n", type); 875 if (type <= MAX_TRAP_MSG) 876 panic("%s", trap_msg[type]); 877 else 878 panic("unknown/reserved trap"); 879} 880 881/* 882 * Double fault handler. Called when a fault occurs while writing 883 * a frame for a trap/exception onto the stack. This usually occurs 884 * when the stack overflows (such is the case with infinite recursion, 885 * for example). 886 */ 887void 888dblfault_handler(struct trapframe *frame) 889{ 890#ifdef KDTRACE_HOOKS 891 if (dtrace_doubletrap_func != NULL) 892 (*dtrace_doubletrap_func)(); 893#endif 894 printf("\nFatal double fault\n"); 895 printf("rip = 0x%lx\n", frame->tf_rip); 896 printf("rsp = 0x%lx\n", frame->tf_rsp); 897 printf("rbp = 0x%lx\n", frame->tf_rbp); 898#ifdef SMP 899 /* two separate prints in case of a trap on an unmapped page */ 900 printf("cpuid = %d; ", PCPU_GET(cpuid)); 901 printf("apic id = %02x\n", PCPU_GET(apic_id)); 902#endif 903 panic("double fault"); 904} 905 906int 907cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa) 908{ 909 struct proc *p; 910 struct trapframe *frame; 911 register_t *argp; 912 caddr_t params; 913 int reg, regcnt, error; 914 915 p = td->td_proc; 916 frame = td->td_frame; 917 reg = 0; 918 regcnt = 6; 919 920 params = (caddr_t)frame->tf_rsp + sizeof(register_t); 921 sa->code = frame->tf_rax; 922 923 if (sa->code == SYS_syscall || sa->code == SYS___syscall) { 924 sa->code = frame->tf_rdi; 925 reg++; 926 regcnt--; 927 } 928 if (p->p_sysent->sv_mask) 929 sa->code &= p->p_sysent->sv_mask; 930 931 if (sa->code >= p->p_sysent->sv_size) 932 sa->callp = &p->p_sysent->sv_table[0]; 933 else 934 sa->callp = &p->p_sysent->sv_table[sa->code]; 935 936 sa->narg = sa->callp->sy_narg; 937 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]), 938 ("Too many syscall arguments!")); 939 error = 0; 940 argp = &frame->tf_rdi; 941 argp += reg; 942 bcopy(argp, sa->args, sizeof(sa->args[0]) * regcnt); 943 if (sa->narg > regcnt) { 944 KASSERT(params != NULL, ("copyin args with no params!")); 945 error = copyin(params, &sa->args[regcnt], 946 (sa->narg - regcnt) * sizeof(sa->args[0])); 947 } 948 949 if (error == 0) { 950 td->td_retval[0] = 0; 951 td->td_retval[1] = frame->tf_rdx; 952 } 953 954 return (error); 955} 956 957#include "../../kern/subr_syscall.c" 958 959/* 960 * System call handler for native binaries. The trap frame is already 961 * set up by the assembler trampoline and a pointer to it is saved in 962 * td_frame. 963 */ 964void 965amd64_syscall(struct thread *td, int traced) 966{ 967 struct syscall_args sa; 968 int error; 969 ksiginfo_t ksi; 970 971#ifdef DIAGNOSTIC 972 if (ISPL(td->td_frame->tf_cs) != SEL_UPL) { 973 panic("syscall"); 974 /* NOT REACHED */ 975 } 976#endif 977 error = syscallenter(td, &sa); 978 979 /* 980 * Traced syscall. 981 */ 982 if (__predict_false(traced)) { 983 td->td_frame->tf_rflags &= ~PSL_T; 984 ksiginfo_init_trap(&ksi); 985 ksi.ksi_signo = SIGTRAP; 986 ksi.ksi_code = TRAP_TRACE; 987 ksi.ksi_addr = (void *)td->td_frame->tf_rip; 988 trapsignal(td, &ksi); 989 } 990 991 KASSERT(PCB_USER_FPU(td->td_pcb), 992 ("System call %s returing with kernel FPU ctx leaked", 993 syscallname(td->td_proc, sa.code))); 994 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td), 995 ("System call %s returning with mangled pcb_save", 996 syscallname(td->td_proc, sa.code))); 997 998 syscallret(td, error, &sa); 999 1000 /* 1001 * If the user-supplied value of %rip is not a canonical 1002 * address, then some CPUs will trigger a ring 0 #GP during 1003 * the sysret instruction. However, the fault handler would 1004 * execute in ring 0 with the user's %gs and %rsp which would 1005 * not be safe. Instead, use the full return path which 1006 * catches the problem safely. 1007 */ 1008 if (td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS) 1009 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 1010} 1011