1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (C) 1994, David Greenman 5 * Copyright (c) 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the University of Utah, and William Jolitz. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * from: @(#)trap.c 7.4 (Berkeley) 5/13/91 40 */ 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 /* 46 * AMD64 Trap and System call handling 47 */ 48 49 #include "opt_clock.h" 50 #include "opt_cpu.h" 51 #include "opt_hwpmc_hooks.h" 52 #include "opt_isa.h" 53 #include "opt_kdb.h" 54 #include "opt_stack.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 81 #include <vm/vm.h> 82 #include <vm/vm_param.h> 83 #include <vm/pmap.h> 84 #include <vm/vm_kern.h> 85 #include <vm/vm_map.h> 86 #include <vm/vm_page.h> 87 #include <vm/vm_extern.h> 88 89 #include <machine/cpu.h> 90 #include <machine/intr_machdep.h> 91 #include <x86/mca.h> 92 #include <machine/md_var.h> 93 #include <machine/pcb.h> 94 #ifdef SMP 95 #include <machine/smp.h> 96 #endif 97 #include <machine/stack.h> 98 #include <machine/tss.h> 99 100 #ifdef KDTRACE_HOOKS 101 #include <sys/dtrace_bsd.h> 102 #endif 103 104 void __noinline trap(struct trapframe *frame); 105 void trap_check(struct trapframe *frame); 106 void dblfault_handler(struct trapframe *frame); 107 108 static int trap_pfault(struct trapframe *, int); 109 static void trap_fatal(struct trapframe *, vm_offset_t); 110 111 #define MAX_TRAP_MSG 32 112 static char *trap_msg[] = { 113 "", /* 0 unused */ 114 "privileged instruction fault", /* 1 T_PRIVINFLT */ 115 "", /* 2 unused */ 116 "breakpoint instruction fault", /* 3 T_BPTFLT */ 117 "", /* 4 unused */ 118 "", /* 5 unused */ 119 "arithmetic trap", /* 6 T_ARITHTRAP */ 120 "", /* 7 unused */ 121 "", /* 8 unused */ 122 "general protection fault", /* 9 T_PROTFLT */ 123 "trace trap", /* 10 T_TRCTRAP */ 124 "", /* 11 unused */ 125 "page fault", /* 12 T_PAGEFLT */ 126 "", /* 13 unused */ 127 "alignment fault", /* 14 T_ALIGNFLT */ 128 "", /* 15 unused */ 129 "", /* 16 unused */ 130 "", /* 17 unused */ 131 "integer divide fault", /* 18 T_DIVIDE */ 132 "non-maskable interrupt trap", /* 19 T_NMI */ 133 "overflow trap", /* 20 T_OFLOW */ 134 "FPU bounds check fault", /* 21 T_BOUND */ 135 "FPU device not available", /* 22 T_DNA */ 136 "double fault", /* 23 T_DOUBLEFLT */ 137 "FPU operand fetch fault", /* 24 T_FPOPFLT */ 138 "invalid TSS fault", /* 25 T_TSSFLT */ 139 "segment not present fault", /* 26 T_SEGNPFLT */ 140 "stack fault", /* 27 T_STKFLT */ 141 "machine check trap", /* 28 T_MCHK */ 142 "SIMD floating-point exception", /* 29 T_XMMFLT */ 143 "reserved (unknown) fault", /* 30 T_RESERVED */ 144 "", /* 31 unused (reserved) */ 145 "DTrace pid return trap", /* 32 T_DTRACE_RET */ 146 }; 147 148 static int prot_fault_translation; 149 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN, 150 &prot_fault_translation, 0, 151 "Select signal to deliver on protection fault"); 152 static int uprintf_signal; 153 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RWTUN, 154 &uprintf_signal, 0, 155 "Print debugging information on trap signal to ctty"); 156 157 /* 158 * Exception, fault, and trap interface to the FreeBSD kernel. 159 * This common code is called from assembly language IDT gate entry 160 * routines that prepare a suitable stack frame, and restore this 161 * frame after the exception has been processed. 162 */ 163 164 void 165 trap(struct trapframe *frame) 166 { 167 #ifdef KDTRACE_HOOKS 168 struct reg regs; 169 #endif 170 ksiginfo_t ksi; 171 struct thread *td; 172 struct proc *p; 173 register_t addr; 174 #ifdef KDB 175 register_t dr6; 176 #endif 177 int signo, ucode; 178 u_int type; 179 180 td = curthread; 181 p = td->td_proc; 182 signo = 0; 183 ucode = 0; 184 addr = 0; 185 186 VM_CNT_INC(v_trap); 187 type = frame->tf_trapno; 188 189 #ifdef SMP 190 /* Handler for NMI IPIs used for stopping CPUs. */ 191 if (type == T_NMI && ipi_nmi_handler() == 0) 192 return; 193 #endif 194 195 #ifdef KDB 196 if (kdb_active) { 197 kdb_reenter(); 198 return; 199 } 200 #endif 201 202 if (type == T_RESERVED) { 203 trap_fatal(frame, 0); 204 return; 205 } 206 207 if (type == T_NMI) { 208 #ifdef HWPMC_HOOKS 209 /* 210 * CPU PMCs interrupt using an NMI. If the PMC module is 211 * active, pass the 'rip' value to the PMC module's interrupt 212 * handler. A non-zero return value from the handler means that 213 * the NMI was consumed by it and we can return immediately. 214 */ 215 if (pmc_intr != NULL && 216 (*pmc_intr)(PCPU_GET(cpuid), frame) != 0) 217 return; 218 #endif 219 220 #ifdef STACK 221 if (stack_nmi_handler(frame) != 0) 222 return; 223 #endif 224 } 225 226 if (type == T_MCHK) { 227 mca_intr(); 228 return; 229 } 230 231 if ((frame->tf_rflags & PSL_I) == 0) { 232 /* 233 * Buggy application or kernel code has disabled 234 * interrupts and then trapped. Enabling interrupts 235 * now is wrong, but it is better than running with 236 * interrupts disabled until they are accidentally 237 * enabled later. 238 */ 239 if (TRAPF_USERMODE(frame)) 240 uprintf( 241 "pid %ld (%s): trap %d with interrupts disabled\n", 242 (long)curproc->p_pid, curthread->td_name, type); 243 else if (type != T_NMI && type != T_BPTFLT && 244 type != T_TRCTRAP) { 245 /* 246 * XXX not quite right, since this may be for a 247 * multiple fault in user mode. 248 */ 249 printf("kernel trap %d with interrupts disabled\n", 250 type); 251 252 /* 253 * We shouldn't enable interrupts while holding a 254 * spin lock. 255 */ 256 if (td->td_md.md_spinlock_count == 0) 257 enable_intr(); 258 } 259 } 260 261 if (TRAPF_USERMODE(frame)) { 262 /* user trap */ 263 264 td->td_pticks = 0; 265 td->td_frame = frame; 266 addr = frame->tf_rip; 267 if (td->td_cowgen != p->p_cowgen) 268 thread_cow_update(td); 269 270 switch (type) { 271 case T_PRIVINFLT: /* privileged instruction fault */ 272 signo = SIGILL; 273 ucode = ILL_PRVOPC; 274 break; 275 276 case T_BPTFLT: /* bpt instruction fault */ 277 case T_TRCTRAP: /* trace trap */ 278 enable_intr(); 279 #ifdef KDTRACE_HOOKS 280 if (type == T_BPTFLT) { 281 fill_frame_regs(frame, ®s); 282 if (dtrace_pid_probe_ptr != NULL && 283 dtrace_pid_probe_ptr(®s) == 0) 284 return; 285 } 286 #endif 287 frame->tf_rflags &= ~PSL_T; 288 signo = SIGTRAP; 289 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT); 290 break; 291 292 case T_ARITHTRAP: /* arithmetic trap */ 293 ucode = fputrap_x87(); 294 if (ucode == -1) 295 return; 296 signo = SIGFPE; 297 break; 298 299 case T_PROTFLT: /* general protection fault */ 300 signo = SIGBUS; 301 ucode = BUS_OBJERR; 302 break; 303 case T_STKFLT: /* stack fault */ 304 case T_SEGNPFLT: /* segment not present fault */ 305 signo = SIGBUS; 306 ucode = BUS_ADRERR; 307 break; 308 case T_TSSFLT: /* invalid TSS fault */ 309 signo = SIGBUS; 310 ucode = BUS_OBJERR; 311 break; 312 case T_ALIGNFLT: 313 signo = SIGBUS; 314 ucode = BUS_ADRALN; 315 break; 316 case T_DOUBLEFLT: /* double fault */ 317 default: 318 signo = SIGBUS; 319 ucode = BUS_OBJERR; 320 break; 321 322 case T_PAGEFLT: /* page fault */ 323 /* 324 * Emulator can take care about this trap? 325 */ 326 if (*p->p_sysent->sv_trap != NULL && 327 (*p->p_sysent->sv_trap)(td) == 0) 328 return; 329 330 addr = frame->tf_addr; 331 signo = trap_pfault(frame, TRUE); 332 if (signo == -1) 333 return; 334 if (signo == 0) 335 goto userret; 336 if (signo == SIGSEGV) { 337 ucode = SEGV_MAPERR; 338 } else if (prot_fault_translation == 0) { 339 /* 340 * Autodetect. This check also covers 341 * the images without the ABI-tag ELF 342 * note. 343 */ 344 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD && 345 p->p_osrel >= P_OSREL_SIGSEGV) { 346 signo = SIGSEGV; 347 ucode = SEGV_ACCERR; 348 } else { 349 signo = SIGBUS; 350 ucode = BUS_PAGE_FAULT; 351 } 352 } else if (prot_fault_translation == 1) { 353 /* 354 * Always compat mode. 355 */ 356 signo = SIGBUS; 357 ucode = BUS_PAGE_FAULT; 358 } else { 359 /* 360 * Always SIGSEGV mode. 361 */ 362 signo = SIGSEGV; 363 ucode = SEGV_ACCERR; 364 } 365 break; 366 367 case T_DIVIDE: /* integer divide fault */ 368 ucode = FPE_INTDIV; 369 signo = SIGFPE; 370 break; 371 372 #ifdef DEV_ISA 373 case T_NMI: 374 nmi_handle_intr(type, frame); 375 return; 376 #endif 377 378 case T_OFLOW: /* integer overflow fault */ 379 ucode = FPE_INTOVF; 380 signo = SIGFPE; 381 break; 382 383 case T_BOUND: /* bounds check fault */ 384 ucode = FPE_FLTSUB; 385 signo = SIGFPE; 386 break; 387 388 case T_DNA: 389 /* transparent fault (due to context switch "late") */ 390 KASSERT(PCB_USER_FPU(td->td_pcb), 391 ("kernel FPU ctx has leaked")); 392 fpudna(); 393 return; 394 395 case T_FPOPFLT: /* FPU operand fetch fault */ 396 ucode = ILL_COPROC; 397 signo = SIGILL; 398 break; 399 400 case T_XMMFLT: /* SIMD floating-point exception */ 401 ucode = fputrap_sse(); 402 if (ucode == -1) 403 return; 404 signo = SIGFPE; 405 break; 406 #ifdef KDTRACE_HOOKS 407 case T_DTRACE_RET: 408 enable_intr(); 409 fill_frame_regs(frame, ®s); 410 if (dtrace_return_probe_ptr != NULL) 411 dtrace_return_probe_ptr(®s); 412 return; 413 #endif 414 } 415 } else { 416 /* kernel trap */ 417 418 KASSERT(cold || td->td_ucred != NULL, 419 ("kernel trap doesn't have ucred")); 420 switch (type) { 421 case T_PAGEFLT: /* page fault */ 422 (void) trap_pfault(frame, FALSE); 423 return; 424 425 case T_DNA: 426 if (PCB_USER_FPU(td->td_pcb)) 427 panic("Unregistered use of FPU in kernel"); 428 fpudna(); 429 return; 430 431 case T_ARITHTRAP: /* arithmetic trap */ 432 case T_XMMFLT: /* SIMD floating-point exception */ 433 case T_FPOPFLT: /* FPU operand fetch fault */ 434 /* 435 * For now, supporting kernel handler 436 * registration for FPU traps is overkill. 437 */ 438 trap_fatal(frame, 0); 439 return; 440 441 case T_STKFLT: /* stack fault */ 442 case T_PROTFLT: /* general protection fault */ 443 case T_SEGNPFLT: /* segment not present fault */ 444 if (td->td_intr_nesting_level != 0) 445 break; 446 447 /* 448 * Invalid segment selectors and out of bounds 449 * %rip's and %rsp's can be set up in user mode. 450 * This causes a fault in kernel mode when the 451 * kernel tries to return to user mode. We want 452 * to get this fault so that we can fix the 453 * problem here and not have to check all the 454 * selectors and pointers when the user changes 455 * them. 456 */ 457 if (frame->tf_rip == (long)doreti_iret) { 458 frame->tf_rip = (long)doreti_iret_fault; 459 return; 460 } 461 if (frame->tf_rip == (long)ld_ds) { 462 frame->tf_rip = (long)ds_load_fault; 463 return; 464 } 465 if (frame->tf_rip == (long)ld_es) { 466 frame->tf_rip = (long)es_load_fault; 467 return; 468 } 469 if (frame->tf_rip == (long)ld_fs) { 470 frame->tf_rip = (long)fs_load_fault; 471 return; 472 } 473 if (frame->tf_rip == (long)ld_gs) { 474 frame->tf_rip = (long)gs_load_fault; 475 return; 476 } 477 if (frame->tf_rip == (long)ld_gsbase) { 478 frame->tf_rip = (long)gsbase_load_fault; 479 return; 480 } 481 if (frame->tf_rip == (long)ld_fsbase) { 482 frame->tf_rip = (long)fsbase_load_fault; 483 return; 484 } 485 if (curpcb->pcb_onfault != NULL) { 486 frame->tf_rip = (long)curpcb->pcb_onfault; 487 return; 488 } 489 break; 490 491 case T_TSSFLT: 492 /* 493 * PSL_NT can be set in user mode and isn't cleared 494 * automatically when the kernel is entered. This 495 * causes a TSS fault when the kernel attempts to 496 * `iret' because the TSS link is uninitialized. We 497 * want to get this fault so that we can fix the 498 * problem here and not every time the kernel is 499 * entered. 500 */ 501 if (frame->tf_rflags & PSL_NT) { 502 frame->tf_rflags &= ~PSL_NT; 503 return; 504 } 505 break; 506 507 case T_TRCTRAP: /* trace trap */ 508 /* 509 * Ignore debug register trace traps due to 510 * accesses in the user's address space, which 511 * can happen under several conditions such as 512 * if a user sets a watchpoint on a buffer and 513 * then passes that buffer to a system call. 514 * We still want to get TRCTRAPS for addresses 515 * in kernel space because that is useful when 516 * debugging the kernel. 517 */ 518 if (user_dbreg_trap()) { 519 /* 520 * Reset breakpoint bits because the 521 * processor doesn't 522 */ 523 load_dr6(rdr6() & ~0xf); 524 return; 525 } 526 /* 527 * FALLTHROUGH (TRCTRAP kernel mode, kernel address) 528 */ 529 case T_BPTFLT: 530 /* 531 * If KDB is enabled, let it handle the debugger trap. 532 * Otherwise, debugger traps "can't happen". 533 */ 534 #ifdef KDB 535 /* XXX %dr6 is not quite reentrant. */ 536 dr6 = rdr6(); 537 load_dr6(dr6 & ~0x4000); 538 if (kdb_trap(type, dr6, frame)) 539 return; 540 #endif 541 break; 542 543 #ifdef DEV_ISA 544 case T_NMI: 545 nmi_handle_intr(type, frame); 546 return; 547 #endif 548 } 549 550 trap_fatal(frame, 0); 551 return; 552 } 553 554 /* Translate fault for emulators (e.g. Linux) */ 555 if (*p->p_sysent->sv_transtrap != NULL) 556 signo = (*p->p_sysent->sv_transtrap)(signo, type); 557 558 ksiginfo_init_trap(&ksi); 559 ksi.ksi_signo = signo; 560 ksi.ksi_code = ucode; 561 ksi.ksi_trapno = type; 562 ksi.ksi_addr = (void *)addr; 563 if (uprintf_signal) { 564 uprintf("pid %d comm %s: signal %d err %lx code %d type %d " 565 "addr 0x%lx rsp 0x%lx rip 0x%lx " 566 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n", 567 p->p_pid, p->p_comm, signo, frame->tf_err, ucode, type, 568 addr, frame->tf_rsp, frame->tf_rip, 569 fubyte((void *)(frame->tf_rip + 0)), 570 fubyte((void *)(frame->tf_rip + 1)), 571 fubyte((void *)(frame->tf_rip + 2)), 572 fubyte((void *)(frame->tf_rip + 3)), 573 fubyte((void *)(frame->tf_rip + 4)), 574 fubyte((void *)(frame->tf_rip + 5)), 575 fubyte((void *)(frame->tf_rip + 6)), 576 fubyte((void *)(frame->tf_rip + 7))); 577 } 578 KASSERT((read_rflags() & PSL_I) != 0, ("interrupts disabled")); 579 trapsignal(td, &ksi); 580 userret: 581 userret(td, frame); 582 KASSERT(PCB_USER_FPU(td->td_pcb), 583 ("Return from trap with kernel FPU ctx leaked")); 584 } 585 586 /* 587 * Ensure that we ignore any DTrace-induced faults. This function cannot 588 * be instrumented, so it cannot generate such faults itself. 589 */ 590 void 591 trap_check(struct trapframe *frame) 592 { 593 594 #ifdef KDTRACE_HOOKS 595 if (dtrace_trap_func != NULL && 596 (*dtrace_trap_func)(frame, frame->tf_trapno) != 0) 597 return; 598 #endif 599 trap(frame); 600 } 601 602 static int 603 trap_pfault(struct trapframe *frame, int usermode) 604 { 605 struct thread *td; 606 struct proc *p; 607 vm_map_t map; 608 vm_offset_t va; 609 int rv; 610 vm_prot_t ftype; 611 vm_offset_t eva; 612 613 td = curthread; 614 p = td->td_proc; 615 eva = frame->tf_addr; 616 rv = 0; 617 618 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) { 619 /* 620 * Due to both processor errata and lazy TLB invalidation when 621 * access restrictions are removed from virtual pages, memory 622 * accesses that are allowed by the physical mapping layer may 623 * nonetheless cause one spurious page fault per virtual page. 624 * When the thread is executing a "no faulting" section that 625 * is bracketed by vm_fault_{disable,enable}_pagefaults(), 626 * every page fault is treated as a spurious page fault, 627 * unless it accesses the same virtual address as the most 628 * recent page fault within the same "no faulting" section. 629 */ 630 if (td->td_md.md_spurflt_addr != eva || 631 (td->td_pflags & TDP_RESETSPUR) != 0) { 632 /* 633 * Do nothing to the TLB. A stale TLB entry is 634 * flushed automatically by a page fault. 635 */ 636 td->td_md.md_spurflt_addr = eva; 637 td->td_pflags &= ~TDP_RESETSPUR; 638 return (0); 639 } 640 } else { 641 /* 642 * If we get a page fault while in a critical section, then 643 * it is most likely a fatal kernel page fault. The kernel 644 * is already going to panic trying to get a sleep lock to 645 * do the VM lookup, so just consider it a fatal trap so the 646 * kernel can print out a useful trap message and even get 647 * to the debugger. 648 * 649 * If we get a page fault while holding a non-sleepable 650 * lock, then it is most likely a fatal kernel page fault. 651 * If WITNESS is enabled, then it's going to whine about 652 * bogus LORs with various VM locks, so just skip to the 653 * fatal trap handling directly. 654 */ 655 if (td->td_critnest != 0 || 656 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL, 657 "Kernel page fault") != 0) { 658 trap_fatal(frame, eva); 659 return (-1); 660 } 661 } 662 va = trunc_page(eva); 663 if (va >= VM_MIN_KERNEL_ADDRESS) { 664 /* 665 * Don't allow user-mode faults in kernel address space. 666 */ 667 if (usermode) 668 goto nogo; 669 670 map = kernel_map; 671 } else { 672 map = &p->p_vmspace->vm_map; 673 674 /* 675 * When accessing a usermode address, kernel must be 676 * ready to accept the page fault, and provide a 677 * handling routine. Since accessing the address 678 * without the handler is a bug, do not try to handle 679 * it normally, and panic immediately. 680 */ 681 if (!usermode && (td->td_intr_nesting_level != 0 || 682 curpcb->pcb_onfault == NULL)) { 683 trap_fatal(frame, eva); 684 return (-1); 685 } 686 } 687 688 /* 689 * If the trap was caused by errant bits in the PTE then panic. 690 */ 691 if (frame->tf_err & PGEX_RSV) { 692 trap_fatal(frame, eva); 693 return (-1); 694 } 695 696 /* 697 * PGEX_I is defined only if the execute disable bit capability is 698 * supported and enabled. 699 */ 700 if (frame->tf_err & PGEX_W) 701 ftype = VM_PROT_WRITE; 702 else if ((frame->tf_err & PGEX_I) && pg_nx != 0) 703 ftype = VM_PROT_EXECUTE; 704 else 705 ftype = VM_PROT_READ; 706 707 /* Fault in the page. */ 708 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 709 if (rv == KERN_SUCCESS) { 710 #ifdef HWPMC_HOOKS 711 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) { 712 PMC_SOFT_CALL_TF( , , page_fault, all, frame); 713 if (ftype == VM_PROT_READ) 714 PMC_SOFT_CALL_TF( , , page_fault, read, 715 frame); 716 else 717 PMC_SOFT_CALL_TF( , , page_fault, write, 718 frame); 719 } 720 #endif 721 return (0); 722 } 723 nogo: 724 if (!usermode) { 725 if (td->td_intr_nesting_level == 0 && 726 curpcb->pcb_onfault != NULL) { 727 frame->tf_rip = (long)curpcb->pcb_onfault; 728 return (0); 729 } 730 trap_fatal(frame, eva); 731 return (-1); 732 } 733 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV); 734 } 735 736 static void 737 trap_fatal(frame, eva) 738 struct trapframe *frame; 739 vm_offset_t eva; 740 { 741 int code, ss; 742 u_int type; 743 struct soft_segment_descriptor softseg; 744 char *msg; 745 746 code = frame->tf_err; 747 type = frame->tf_trapno; 748 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)], 749 &softseg); 750 751 if (type <= MAX_TRAP_MSG) 752 msg = trap_msg[type]; 753 else 754 msg = "UNKNOWN"; 755 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg, 756 TRAPF_USERMODE(frame) ? "user" : "kernel"); 757 #ifdef SMP 758 /* two separate prints in case of a trap on an unmapped page */ 759 printf("cpuid = %d; ", PCPU_GET(cpuid)); 760 printf("apic id = %02x\n", PCPU_GET(apic_id)); 761 #endif 762 if (type == T_PAGEFLT) { 763 printf("fault virtual address = 0x%lx\n", eva); 764 printf("fault code = %s %s %s, %s\n", 765 code & PGEX_U ? "user" : "supervisor", 766 code & PGEX_W ? "write" : "read", 767 code & PGEX_I ? "instruction" : "data", 768 code & PGEX_RSV ? "reserved bits in PTE" : 769 code & PGEX_P ? "protection violation" : "page not present"); 770 } 771 printf("instruction pointer = 0x%lx:0x%lx\n", 772 frame->tf_cs & 0xffff, frame->tf_rip); 773 ss = frame->tf_ss & 0xffff; 774 printf("stack pointer = 0x%x:0x%lx\n", ss, frame->tf_rsp); 775 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp); 776 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n", 777 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type); 778 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n", 779 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32, 780 softseg.ssd_gran); 781 printf("processor eflags = "); 782 if (frame->tf_rflags & PSL_T) 783 printf("trace trap, "); 784 if (frame->tf_rflags & PSL_I) 785 printf("interrupt enabled, "); 786 if (frame->tf_rflags & PSL_NT) 787 printf("nested task, "); 788 if (frame->tf_rflags & PSL_RF) 789 printf("resume, "); 790 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12); 791 printf("current process = %d (%s)\n", 792 curproc->p_pid, curthread->td_name); 793 794 #ifdef KDB 795 if (debugger_on_panic || kdb_active) 796 if (kdb_trap(type, 0, frame)) 797 return; 798 #endif 799 printf("trap number = %d\n", type); 800 if (type <= MAX_TRAP_MSG) 801 panic("%s", trap_msg[type]); 802 else 803 panic("unknown/reserved trap"); 804 } 805 806 /* 807 * Double fault handler. Called when a fault occurs while writing 808 * a frame for a trap/exception onto the stack. This usually occurs 809 * when the stack overflows (such is the case with infinite recursion, 810 * for example). 811 */ 812 void 813 dblfault_handler(struct trapframe *frame) 814 { 815 #ifdef KDTRACE_HOOKS 816 if (dtrace_doubletrap_func != NULL) 817 (*dtrace_doubletrap_func)(); 818 #endif 819 printf("\nFatal double fault\n" 820 "rip %#lx rsp %#lx rbp %#lx\n" 821 "rax %#lx rdx %#lx rbx %#lx\n" 822 "rcx %#lx rsi %#lx rdi %#lx\n" 823 "r8 %#lx r9 %#lx r10 %#lx\n" 824 "r11 %#lx r12 %#lx r13 %#lx\n" 825 "r14 %#lx r15 %#lx rflags %#lx\n" 826 "cs %#lx ss %#lx ds %#hx es %#hx fs %#hx gs %#hx\n" 827 "fsbase %#lx gsbase %#lx kgsbase %#lx\n", 828 frame->tf_rip, frame->tf_rsp, frame->tf_rbp, 829 frame->tf_rax, frame->tf_rdx, frame->tf_rbx, 830 frame->tf_rcx, frame->tf_rdi, frame->tf_rsi, 831 frame->tf_r8, frame->tf_r9, frame->tf_r10, 832 frame->tf_r11, frame->tf_r12, frame->tf_r13, 833 frame->tf_r14, frame->tf_r15, frame->tf_rflags, 834 frame->tf_cs, frame->tf_ss, frame->tf_ds, frame->tf_es, 835 frame->tf_fs, frame->tf_gs, 836 rdmsr(MSR_FSBASE), rdmsr(MSR_GSBASE), rdmsr(MSR_KGSBASE)); 837 #ifdef SMP 838 /* two separate prints in case of a trap on an unmapped page */ 839 printf("cpuid = %d; ", PCPU_GET(cpuid)); 840 printf("apic id = %02x\n", PCPU_GET(apic_id)); 841 #endif 842 panic("double fault"); 843 } 844 845 int 846 cpu_fetch_syscall_args(struct thread *td) 847 { 848 struct proc *p; 849 struct trapframe *frame; 850 register_t *argp; 851 struct syscall_args *sa; 852 caddr_t params; 853 int reg, regcnt, error; 854 855 p = td->td_proc; 856 frame = td->td_frame; 857 sa = &td->td_sa; 858 reg = 0; 859 regcnt = 6; 860 861 params = (caddr_t)frame->tf_rsp + sizeof(register_t); 862 sa->code = frame->tf_rax; 863 864 if (sa->code == SYS_syscall || sa->code == SYS___syscall) { 865 sa->code = frame->tf_rdi; 866 reg++; 867 regcnt--; 868 } 869 if (p->p_sysent->sv_mask) 870 sa->code &= p->p_sysent->sv_mask; 871 872 if (sa->code >= p->p_sysent->sv_size) 873 sa->callp = &p->p_sysent->sv_table[0]; 874 else 875 sa->callp = &p->p_sysent->sv_table[sa->code]; 876 877 sa->narg = sa->callp->sy_narg; 878 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]), 879 ("Too many syscall arguments!")); 880 error = 0; 881 argp = &frame->tf_rdi; 882 argp += reg; 883 bcopy(argp, sa->args, sizeof(sa->args[0]) * regcnt); 884 if (sa->narg > regcnt) { 885 KASSERT(params != NULL, ("copyin args with no params!")); 886 error = copyin(params, &sa->args[regcnt], 887 (sa->narg - regcnt) * sizeof(sa->args[0])); 888 } 889 890 if (error == 0) { 891 td->td_retval[0] = 0; 892 td->td_retval[1] = frame->tf_rdx; 893 } 894 895 return (error); 896 } 897 898 #include "../../kern/subr_syscall.c" 899 900 /* 901 * System call handler for native binaries. The trap frame is already 902 * set up by the assembler trampoline and a pointer to it is saved in 903 * td_frame. 904 */ 905 void 906 amd64_syscall(struct thread *td, int traced) 907 { 908 int error; 909 ksiginfo_t ksi; 910 911 #ifdef DIAGNOSTIC 912 if (!TRAPF_USERMODE(td->td_frame)) { 913 panic("syscall"); 914 /* NOT REACHED */ 915 } 916 #endif 917 error = syscallenter(td); 918 919 /* 920 * Traced syscall. 921 */ 922 if (__predict_false(traced)) { 923 td->td_frame->tf_rflags &= ~PSL_T; 924 ksiginfo_init_trap(&ksi); 925 ksi.ksi_signo = SIGTRAP; 926 ksi.ksi_code = TRAP_TRACE; 927 ksi.ksi_addr = (void *)td->td_frame->tf_rip; 928 trapsignal(td, &ksi); 929 } 930 931 KASSERT(PCB_USER_FPU(td->td_pcb), 932 ("System call %s returning with kernel FPU ctx leaked", 933 syscallname(td->td_proc, td->td_sa.code))); 934 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td), 935 ("System call %s returning with mangled pcb_save", 936 syscallname(td->td_proc, td->td_sa.code))); 937 KASSERT(td->td_md.md_invl_gen.gen == 0, 938 ("System call %s returning with leaked invl_gen %lu", 939 syscallname(td->td_proc, td->td_sa.code), 940 td->td_md.md_invl_gen.gen)); 941 942 syscallret(td, error); 943 944 /* 945 * If the user-supplied value of %rip is not a canonical 946 * address, then some CPUs will trigger a ring 0 #GP during 947 * the sysret instruction. However, the fault handler would 948 * execute in ring 0 with the user's %gs and %rsp which would 949 * not be safe. Instead, use the full return path which 950 * catches the problem safely. 951 */ 952 if (__predict_false(td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS)) 953 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 954 } 955