1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 1995 Linus Torvalds 4 * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs. 5 * Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar 6 */ 7 #include <linux/sched.h> /* test_thread_flag(), ... */ 8 #include <linux/sched/task_stack.h> /* task_stack_*(), ... */ 9 #include <linux/kdebug.h> /* oops_begin/end, ... */ 10 #include <linux/extable.h> /* search_exception_tables */ 11 #include <linux/memblock.h> /* max_low_pfn */ 12 #include <linux/kprobes.h> /* NOKPROBE_SYMBOL, ... */ 13 #include <linux/mmiotrace.h> /* kmmio_handler, ... */ 14 #include <linux/perf_event.h> /* perf_sw_event */ 15 #include <linux/hugetlb.h> /* hstate_index_to_shift */ 16 #include <linux/prefetch.h> /* prefetchw */ 17 #include <linux/context_tracking.h> /* exception_enter(), ... */ 18 #include <linux/uaccess.h> /* faulthandler_disabled() */ 19 #include <linux/efi.h> /* efi_recover_from_page_fault()*/ 20 #include <linux/mm_types.h> 21 22 #include <asm/cpufeature.h> /* boot_cpu_has, ... */ 23 #include <asm/traps.h> /* dotraplinkage, ... */ 24 #include <asm/fixmap.h> /* VSYSCALL_ADDR */ 25 #include <asm/vsyscall.h> /* emulate_vsyscall */ 26 #include <asm/vm86.h> /* struct vm86 */ 27 #include <asm/mmu_context.h> /* vma_pkey() */ 28 #include <asm/efi.h> /* efi_recover_from_page_fault()*/ 29 #include <asm/desc.h> /* store_idt(), ... */ 30 #include <asm/cpu_entry_area.h> /* exception stack */ 31 #include <asm/pgtable_areas.h> /* VMALLOC_START, ... */ 32 #include <asm/kvm_para.h> /* kvm_handle_async_pf */ 33 #include <asm/vdso.h> /* fixup_vdso_exception() */ 34 35 #define CREATE_TRACE_POINTS 36 #include <asm/trace/exceptions.h> 37 38 /* 39 * Returns 0 if mmiotrace is disabled, or if the fault is not 40 * handled by mmiotrace: 41 */ 42 static nokprobe_inline int 43 kmmio_fault(struct pt_regs *regs, unsigned long addr) 44 { 45 if (unlikely(is_kmmio_active())) 46 if (kmmio_handler(regs, addr) == 1) 47 return -1; 48 return 0; 49 } 50 51 /* 52 * Prefetch quirks: 53 * 54 * 32-bit mode: 55 * 56 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch. 57 * Check that here and ignore it. This is AMD erratum #91. 58 * 59 * 64-bit mode: 60 * 61 * Sometimes the CPU reports invalid exceptions on prefetch. 62 * Check that here and ignore it. 63 * 64 * Opcode checker based on code by Richard Brunner. 65 */ 66 static inline int 67 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr, 68 unsigned char opcode, int *prefetch) 69 { 70 unsigned char instr_hi = opcode & 0xf0; 71 unsigned char instr_lo = opcode & 0x0f; 72 73 switch (instr_hi) { 74 case 0x20: 75 case 0x30: 76 /* 77 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes. 78 * In X86_64 long mode, the CPU will signal invalid 79 * opcode if some of these prefixes are present so 80 * X86_64 will never get here anyway 81 */ 82 return ((instr_lo & 7) == 0x6); 83 #ifdef CONFIG_X86_64 84 case 0x40: 85 /* 86 * In 64-bit mode 0x40..0x4F are valid REX prefixes 87 */ 88 return (!user_mode(regs) || user_64bit_mode(regs)); 89 #endif 90 case 0x60: 91 /* 0x64 thru 0x67 are valid prefixes in all modes. */ 92 return (instr_lo & 0xC) == 0x4; 93 case 0xF0: 94 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */ 95 return !instr_lo || (instr_lo>>1) == 1; 96 case 0x00: 97 /* Prefetch instruction is 0x0F0D or 0x0F18 */ 98 if (get_kernel_nofault(opcode, instr)) 99 return 0; 100 101 *prefetch = (instr_lo == 0xF) && 102 (opcode == 0x0D || opcode == 0x18); 103 return 0; 104 default: 105 return 0; 106 } 107 } 108 109 static bool is_amd_k8_pre_npt(void) 110 { 111 struct cpuinfo_x86 *c = &boot_cpu_data; 112 113 return unlikely(IS_ENABLED(CONFIG_CPU_SUP_AMD) && 114 c->x86_vendor == X86_VENDOR_AMD && 115 c->x86 == 0xf && c->x86_model < 0x40); 116 } 117 118 static int 119 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr) 120 { 121 unsigned char *max_instr; 122 unsigned char *instr; 123 int prefetch = 0; 124 125 /* Erratum #91 affects AMD K8, pre-NPT CPUs */ 126 if (!is_amd_k8_pre_npt()) 127 return 0; 128 129 /* 130 * If it was a exec (instruction fetch) fault on NX page, then 131 * do not ignore the fault: 132 */ 133 if (error_code & X86_PF_INSTR) 134 return 0; 135 136 instr = (void *)convert_ip_to_linear(current, regs); 137 max_instr = instr + 15; 138 139 /* 140 * This code has historically always bailed out if IP points to a 141 * not-present page (e.g. due to a race). No one has ever 142 * complained about this. 143 */ 144 pagefault_disable(); 145 146 while (instr < max_instr) { 147 unsigned char opcode; 148 149 if (user_mode(regs)) { 150 if (get_user(opcode, instr)) 151 break; 152 } else { 153 if (get_kernel_nofault(opcode, instr)) 154 break; 155 } 156 157 instr++; 158 159 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch)) 160 break; 161 } 162 163 pagefault_enable(); 164 return prefetch; 165 } 166 167 DEFINE_SPINLOCK(pgd_lock); 168 LIST_HEAD(pgd_list); 169 170 #ifdef CONFIG_X86_32 171 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address) 172 { 173 unsigned index = pgd_index(address); 174 pgd_t *pgd_k; 175 p4d_t *p4d, *p4d_k; 176 pud_t *pud, *pud_k; 177 pmd_t *pmd, *pmd_k; 178 179 pgd += index; 180 pgd_k = init_mm.pgd + index; 181 182 if (!pgd_present(*pgd_k)) 183 return NULL; 184 185 /* 186 * set_pgd(pgd, *pgd_k); here would be useless on PAE 187 * and redundant with the set_pmd() on non-PAE. As would 188 * set_p4d/set_pud. 189 */ 190 p4d = p4d_offset(pgd, address); 191 p4d_k = p4d_offset(pgd_k, address); 192 if (!p4d_present(*p4d_k)) 193 return NULL; 194 195 pud = pud_offset(p4d, address); 196 pud_k = pud_offset(p4d_k, address); 197 if (!pud_present(*pud_k)) 198 return NULL; 199 200 pmd = pmd_offset(pud, address); 201 pmd_k = pmd_offset(pud_k, address); 202 203 if (pmd_present(*pmd) != pmd_present(*pmd_k)) 204 set_pmd(pmd, *pmd_k); 205 206 if (!pmd_present(*pmd_k)) 207 return NULL; 208 else 209 BUG_ON(pmd_pfn(*pmd) != pmd_pfn(*pmd_k)); 210 211 return pmd_k; 212 } 213 214 /* 215 * Handle a fault on the vmalloc or module mapping area 216 * 217 * This is needed because there is a race condition between the time 218 * when the vmalloc mapping code updates the PMD to the point in time 219 * where it synchronizes this update with the other page-tables in the 220 * system. 221 * 222 * In this race window another thread/CPU can map an area on the same 223 * PMD, finds it already present and does not synchronize it with the 224 * rest of the system yet. As a result v[mz]alloc might return areas 225 * which are not mapped in every page-table in the system, causing an 226 * unhandled page-fault when they are accessed. 227 */ 228 static noinline int vmalloc_fault(unsigned long address) 229 { 230 unsigned long pgd_paddr; 231 pmd_t *pmd_k; 232 pte_t *pte_k; 233 234 /* Make sure we are in vmalloc area: */ 235 if (!(address >= VMALLOC_START && address < VMALLOC_END)) 236 return -1; 237 238 /* 239 * Synchronize this task's top level page-table 240 * with the 'reference' page table. 241 * 242 * Do _not_ use "current" here. We might be inside 243 * an interrupt in the middle of a task switch.. 244 */ 245 pgd_paddr = read_cr3_pa(); 246 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address); 247 if (!pmd_k) 248 return -1; 249 250 if (pmd_large(*pmd_k)) 251 return 0; 252 253 pte_k = pte_offset_kernel(pmd_k, address); 254 if (!pte_present(*pte_k)) 255 return -1; 256 257 return 0; 258 } 259 NOKPROBE_SYMBOL(vmalloc_fault); 260 261 void arch_sync_kernel_mappings(unsigned long start, unsigned long end) 262 { 263 unsigned long addr; 264 265 for (addr = start & PMD_MASK; 266 addr >= TASK_SIZE_MAX && addr < VMALLOC_END; 267 addr += PMD_SIZE) { 268 struct page *page; 269 270 spin_lock(&pgd_lock); 271 list_for_each_entry(page, &pgd_list, lru) { 272 spinlock_t *pgt_lock; 273 274 /* the pgt_lock only for Xen */ 275 pgt_lock = &pgd_page_get_mm(page)->page_table_lock; 276 277 spin_lock(pgt_lock); 278 vmalloc_sync_one(page_address(page), addr); 279 spin_unlock(pgt_lock); 280 } 281 spin_unlock(&pgd_lock); 282 } 283 } 284 285 /* 286 * Did it hit the DOS screen memory VA from vm86 mode? 287 */ 288 static inline void 289 check_v8086_mode(struct pt_regs *regs, unsigned long address, 290 struct task_struct *tsk) 291 { 292 #ifdef CONFIG_VM86 293 unsigned long bit; 294 295 if (!v8086_mode(regs) || !tsk->thread.vm86) 296 return; 297 298 bit = (address - 0xA0000) >> PAGE_SHIFT; 299 if (bit < 32) 300 tsk->thread.vm86->screen_bitmap |= 1 << bit; 301 #endif 302 } 303 304 static bool low_pfn(unsigned long pfn) 305 { 306 return pfn < max_low_pfn; 307 } 308 309 static void dump_pagetable(unsigned long address) 310 { 311 pgd_t *base = __va(read_cr3_pa()); 312 pgd_t *pgd = &base[pgd_index(address)]; 313 p4d_t *p4d; 314 pud_t *pud; 315 pmd_t *pmd; 316 pte_t *pte; 317 318 #ifdef CONFIG_X86_PAE 319 pr_info("*pdpt = %016Lx ", pgd_val(*pgd)); 320 if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd)) 321 goto out; 322 #define pr_pde pr_cont 323 #else 324 #define pr_pde pr_info 325 #endif 326 p4d = p4d_offset(pgd, address); 327 pud = pud_offset(p4d, address); 328 pmd = pmd_offset(pud, address); 329 pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd)); 330 #undef pr_pde 331 332 /* 333 * We must not directly access the pte in the highpte 334 * case if the page table is located in highmem. 335 * And let's rather not kmap-atomic the pte, just in case 336 * it's allocated already: 337 */ 338 if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd)) 339 goto out; 340 341 pte = pte_offset_kernel(pmd, address); 342 pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte)); 343 out: 344 pr_cont("\n"); 345 } 346 347 #else /* CONFIG_X86_64: */ 348 349 #ifdef CONFIG_CPU_SUP_AMD 350 static const char errata93_warning[] = 351 KERN_ERR 352 "******* Your BIOS seems to not contain a fix for K8 errata #93\n" 353 "******* Working around it, but it may cause SEGVs or burn power.\n" 354 "******* Please consider a BIOS update.\n" 355 "******* Disabling USB legacy in the BIOS may also help.\n"; 356 #endif 357 358 /* 359 * No vm86 mode in 64-bit mode: 360 */ 361 static inline void 362 check_v8086_mode(struct pt_regs *regs, unsigned long address, 363 struct task_struct *tsk) 364 { 365 } 366 367 static int bad_address(void *p) 368 { 369 unsigned long dummy; 370 371 return get_kernel_nofault(dummy, (unsigned long *)p); 372 } 373 374 static void dump_pagetable(unsigned long address) 375 { 376 pgd_t *base = __va(read_cr3_pa()); 377 pgd_t *pgd = base + pgd_index(address); 378 p4d_t *p4d; 379 pud_t *pud; 380 pmd_t *pmd; 381 pte_t *pte; 382 383 if (bad_address(pgd)) 384 goto bad; 385 386 pr_info("PGD %lx ", pgd_val(*pgd)); 387 388 if (!pgd_present(*pgd)) 389 goto out; 390 391 p4d = p4d_offset(pgd, address); 392 if (bad_address(p4d)) 393 goto bad; 394 395 pr_cont("P4D %lx ", p4d_val(*p4d)); 396 if (!p4d_present(*p4d) || p4d_large(*p4d)) 397 goto out; 398 399 pud = pud_offset(p4d, address); 400 if (bad_address(pud)) 401 goto bad; 402 403 pr_cont("PUD %lx ", pud_val(*pud)); 404 if (!pud_present(*pud) || pud_large(*pud)) 405 goto out; 406 407 pmd = pmd_offset(pud, address); 408 if (bad_address(pmd)) 409 goto bad; 410 411 pr_cont("PMD %lx ", pmd_val(*pmd)); 412 if (!pmd_present(*pmd) || pmd_large(*pmd)) 413 goto out; 414 415 pte = pte_offset_kernel(pmd, address); 416 if (bad_address(pte)) 417 goto bad; 418 419 pr_cont("PTE %lx", pte_val(*pte)); 420 out: 421 pr_cont("\n"); 422 return; 423 bad: 424 pr_info("BAD\n"); 425 } 426 427 #endif /* CONFIG_X86_64 */ 428 429 /* 430 * Workaround for K8 erratum #93 & buggy BIOS. 431 * 432 * BIOS SMM functions are required to use a specific workaround 433 * to avoid corruption of the 64bit RIP register on C stepping K8. 434 * 435 * A lot of BIOS that didn't get tested properly miss this. 436 * 437 * The OS sees this as a page fault with the upper 32bits of RIP cleared. 438 * Try to work around it here. 439 * 440 * Note we only handle faults in kernel here. 441 * Does nothing on 32-bit. 442 */ 443 static int is_errata93(struct pt_regs *regs, unsigned long address) 444 { 445 #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD) 446 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD 447 || boot_cpu_data.x86 != 0xf) 448 return 0; 449 450 if (user_mode(regs)) 451 return 0; 452 453 if (address != regs->ip) 454 return 0; 455 456 if ((address >> 32) != 0) 457 return 0; 458 459 address |= 0xffffffffUL << 32; 460 if ((address >= (u64)_stext && address <= (u64)_etext) || 461 (address >= MODULES_VADDR && address <= MODULES_END)) { 462 printk_once(errata93_warning); 463 regs->ip = address; 464 return 1; 465 } 466 #endif 467 return 0; 468 } 469 470 /* 471 * Work around K8 erratum #100 K8 in compat mode occasionally jumps 472 * to illegal addresses >4GB. 473 * 474 * We catch this in the page fault handler because these addresses 475 * are not reachable. Just detect this case and return. Any code 476 * segment in LDT is compatibility mode. 477 */ 478 static int is_errata100(struct pt_regs *regs, unsigned long address) 479 { 480 #ifdef CONFIG_X86_64 481 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32)) 482 return 1; 483 #endif 484 return 0; 485 } 486 487 /* Pentium F0 0F C7 C8 bug workaround: */ 488 static int is_f00f_bug(struct pt_regs *regs, unsigned long error_code, 489 unsigned long address) 490 { 491 #ifdef CONFIG_X86_F00F_BUG 492 if (boot_cpu_has_bug(X86_BUG_F00F) && !(error_code & X86_PF_USER) && 493 idt_is_f00f_address(address)) { 494 handle_invalid_op(regs); 495 return 1; 496 } 497 #endif 498 return 0; 499 } 500 501 static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index) 502 { 503 u32 offset = (index >> 3) * sizeof(struct desc_struct); 504 unsigned long addr; 505 struct ldttss_desc desc; 506 507 if (index == 0) { 508 pr_alert("%s: NULL\n", name); 509 return; 510 } 511 512 if (offset + sizeof(struct ldttss_desc) >= gdt->size) { 513 pr_alert("%s: 0x%hx -- out of bounds\n", name, index); 514 return; 515 } 516 517 if (copy_from_kernel_nofault(&desc, (void *)(gdt->address + offset), 518 sizeof(struct ldttss_desc))) { 519 pr_alert("%s: 0x%hx -- GDT entry is not readable\n", 520 name, index); 521 return; 522 } 523 524 addr = desc.base0 | (desc.base1 << 16) | ((unsigned long)desc.base2 << 24); 525 #ifdef CONFIG_X86_64 526 addr |= ((u64)desc.base3 << 32); 527 #endif 528 pr_alert("%s: 0x%hx -- base=0x%lx limit=0x%x\n", 529 name, index, addr, (desc.limit0 | (desc.limit1 << 16))); 530 } 531 532 static void 533 show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long address) 534 { 535 if (!oops_may_print()) 536 return; 537 538 if (error_code & X86_PF_INSTR) { 539 unsigned int level; 540 pgd_t *pgd; 541 pte_t *pte; 542 543 pgd = __va(read_cr3_pa()); 544 pgd += pgd_index(address); 545 546 pte = lookup_address_in_pgd(pgd, address, &level); 547 548 if (pte && pte_present(*pte) && !pte_exec(*pte)) 549 pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n", 550 from_kuid(&init_user_ns, current_uid())); 551 if (pte && pte_present(*pte) && pte_exec(*pte) && 552 (pgd_flags(*pgd) & _PAGE_USER) && 553 (__read_cr4() & X86_CR4_SMEP)) 554 pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n", 555 from_kuid(&init_user_ns, current_uid())); 556 } 557 558 if (address < PAGE_SIZE && !user_mode(regs)) 559 pr_alert("BUG: kernel NULL pointer dereference, address: %px\n", 560 (void *)address); 561 else 562 pr_alert("BUG: unable to handle page fault for address: %px\n", 563 (void *)address); 564 565 pr_alert("#PF: %s %s in %s mode\n", 566 (error_code & X86_PF_USER) ? "user" : "supervisor", 567 (error_code & X86_PF_INSTR) ? "instruction fetch" : 568 (error_code & X86_PF_WRITE) ? "write access" : 569 "read access", 570 user_mode(regs) ? "user" : "kernel"); 571 pr_alert("#PF: error_code(0x%04lx) - %s\n", error_code, 572 !(error_code & X86_PF_PROT) ? "not-present page" : 573 (error_code & X86_PF_RSVD) ? "reserved bit violation" : 574 (error_code & X86_PF_PK) ? "protection keys violation" : 575 "permissions violation"); 576 577 if (!(error_code & X86_PF_USER) && user_mode(regs)) { 578 struct desc_ptr idt, gdt; 579 u16 ldtr, tr; 580 581 /* 582 * This can happen for quite a few reasons. The more obvious 583 * ones are faults accessing the GDT, or LDT. Perhaps 584 * surprisingly, if the CPU tries to deliver a benign or 585 * contributory exception from user code and gets a page fault 586 * during delivery, the page fault can be delivered as though 587 * it originated directly from user code. This could happen 588 * due to wrong permissions on the IDT, GDT, LDT, TSS, or 589 * kernel or IST stack. 590 */ 591 store_idt(&idt); 592 593 /* Usable even on Xen PV -- it's just slow. */ 594 native_store_gdt(&gdt); 595 596 pr_alert("IDT: 0x%lx (limit=0x%hx) GDT: 0x%lx (limit=0x%hx)\n", 597 idt.address, idt.size, gdt.address, gdt.size); 598 599 store_ldt(ldtr); 600 show_ldttss(&gdt, "LDTR", ldtr); 601 602 store_tr(tr); 603 show_ldttss(&gdt, "TR", tr); 604 } 605 606 dump_pagetable(address); 607 } 608 609 static noinline void 610 pgtable_bad(struct pt_regs *regs, unsigned long error_code, 611 unsigned long address) 612 { 613 struct task_struct *tsk; 614 unsigned long flags; 615 int sig; 616 617 flags = oops_begin(); 618 tsk = current; 619 sig = SIGKILL; 620 621 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n", 622 tsk->comm, address); 623 dump_pagetable(address); 624 625 if (__die("Bad pagetable", regs, error_code)) 626 sig = 0; 627 628 oops_end(flags, regs, sig); 629 } 630 631 static void sanitize_error_code(unsigned long address, 632 unsigned long *error_code) 633 { 634 /* 635 * To avoid leaking information about the kernel page 636 * table layout, pretend that user-mode accesses to 637 * kernel addresses are always protection faults. 638 * 639 * NB: This means that failed vsyscalls with vsyscall=none 640 * will have the PROT bit. This doesn't leak any 641 * information and does not appear to cause any problems. 642 */ 643 if (address >= TASK_SIZE_MAX) 644 *error_code |= X86_PF_PROT; 645 } 646 647 static void set_signal_archinfo(unsigned long address, 648 unsigned long error_code) 649 { 650 struct task_struct *tsk = current; 651 652 tsk->thread.trap_nr = X86_TRAP_PF; 653 tsk->thread.error_code = error_code | X86_PF_USER; 654 tsk->thread.cr2 = address; 655 } 656 657 static noinline void 658 page_fault_oops(struct pt_regs *regs, unsigned long error_code, 659 unsigned long address) 660 { 661 unsigned long flags; 662 int sig; 663 664 if (user_mode(regs)) { 665 /* 666 * Implicit kernel access from user mode? Skip the stack 667 * overflow and EFI special cases. 668 */ 669 goto oops; 670 } 671 672 #ifdef CONFIG_VMAP_STACK 673 /* 674 * Stack overflow? During boot, we can fault near the initial 675 * stack in the direct map, but that's not an overflow -- check 676 * that we're in vmalloc space to avoid this. 677 */ 678 if (is_vmalloc_addr((void *)address) && 679 (((unsigned long)current->stack - 1 - address < PAGE_SIZE) || 680 address - ((unsigned long)current->stack + THREAD_SIZE) < PAGE_SIZE)) { 681 unsigned long stack = __this_cpu_ist_top_va(DF) - sizeof(void *); 682 /* 683 * We're likely to be running with very little stack space 684 * left. It's plausible that we'd hit this condition but 685 * double-fault even before we get this far, in which case 686 * we're fine: the double-fault handler will deal with it. 687 * 688 * We don't want to make it all the way into the oops code 689 * and then double-fault, though, because we're likely to 690 * break the console driver and lose most of the stack dump. 691 */ 692 asm volatile ("movq %[stack], %%rsp\n\t" 693 "call handle_stack_overflow\n\t" 694 "1: jmp 1b" 695 : ASM_CALL_CONSTRAINT 696 : "D" ("kernel stack overflow (page fault)"), 697 "S" (regs), "d" (address), 698 [stack] "rm" (stack)); 699 unreachable(); 700 } 701 #endif 702 703 /* 704 * Buggy firmware could access regions which might page fault, try to 705 * recover from such faults. 706 */ 707 if (IS_ENABLED(CONFIG_EFI)) 708 efi_recover_from_page_fault(address); 709 710 oops: 711 /* 712 * Oops. The kernel tried to access some bad page. We'll have to 713 * terminate things with extreme prejudice: 714 */ 715 flags = oops_begin(); 716 717 show_fault_oops(regs, error_code, address); 718 719 if (task_stack_end_corrupted(current)) 720 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n"); 721 722 sig = SIGKILL; 723 if (__die("Oops", regs, error_code)) 724 sig = 0; 725 726 /* Executive summary in case the body of the oops scrolled away */ 727 printk(KERN_DEFAULT "CR2: %016lx\n", address); 728 729 oops_end(flags, regs, sig); 730 } 731 732 static noinline void 733 kernelmode_fixup_or_oops(struct pt_regs *regs, unsigned long error_code, 734 unsigned long address, int signal, int si_code) 735 { 736 WARN_ON_ONCE(user_mode(regs)); 737 738 /* Are we prepared to handle this kernel fault? */ 739 if (fixup_exception(regs, X86_TRAP_PF, error_code, address)) { 740 /* 741 * Any interrupt that takes a fault gets the fixup. This makes 742 * the below recursive fault logic only apply to a faults from 743 * task context. 744 */ 745 if (in_interrupt()) 746 return; 747 748 /* 749 * Per the above we're !in_interrupt(), aka. task context. 750 * 751 * In this case we need to make sure we're not recursively 752 * faulting through the emulate_vsyscall() logic. 753 */ 754 if (current->thread.sig_on_uaccess_err && signal) { 755 sanitize_error_code(address, &error_code); 756 757 set_signal_archinfo(address, error_code); 758 759 /* XXX: hwpoison faults will set the wrong code. */ 760 force_sig_fault(signal, si_code, (void __user *)address); 761 } 762 763 /* 764 * Barring that, we can do the fixup and be happy. 765 */ 766 return; 767 } 768 769 /* 770 * AMD erratum #91 manifests as a spurious page fault on a PREFETCH 771 * instruction. 772 */ 773 if (is_prefetch(regs, error_code, address)) 774 return; 775 776 page_fault_oops(regs, error_code, address); 777 } 778 779 /* 780 * Print out info about fatal segfaults, if the show_unhandled_signals 781 * sysctl is set: 782 */ 783 static inline void 784 show_signal_msg(struct pt_regs *regs, unsigned long error_code, 785 unsigned long address, struct task_struct *tsk) 786 { 787 const char *loglvl = task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG; 788 789 if (!unhandled_signal(tsk, SIGSEGV)) 790 return; 791 792 if (!printk_ratelimit()) 793 return; 794 795 printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx", 796 loglvl, tsk->comm, task_pid_nr(tsk), address, 797 (void *)regs->ip, (void *)regs->sp, error_code); 798 799 print_vma_addr(KERN_CONT " in ", regs->ip); 800 801 printk(KERN_CONT "\n"); 802 803 show_opcodes(regs, loglvl); 804 } 805 806 /* 807 * The (legacy) vsyscall page is the long page in the kernel portion 808 * of the address space that has user-accessible permissions. 809 */ 810 static bool is_vsyscall_vaddr(unsigned long vaddr) 811 { 812 return unlikely((vaddr & PAGE_MASK) == VSYSCALL_ADDR); 813 } 814 815 static void 816 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 817 unsigned long address, u32 pkey, int si_code) 818 { 819 struct task_struct *tsk = current; 820 821 if (!user_mode(regs)) { 822 kernelmode_fixup_or_oops(regs, error_code, address, pkey, si_code); 823 return; 824 } 825 826 if (!(error_code & X86_PF_USER)) { 827 /* Implicit user access to kernel memory -- just oops */ 828 page_fault_oops(regs, error_code, address); 829 return; 830 } 831 832 /* 833 * User mode accesses just cause a SIGSEGV. 834 * It's possible to have interrupts off here: 835 */ 836 local_irq_enable(); 837 838 /* 839 * Valid to do another page fault here because this one came 840 * from user space: 841 */ 842 if (is_prefetch(regs, error_code, address)) 843 return; 844 845 if (is_errata100(regs, address)) 846 return; 847 848 sanitize_error_code(address, &error_code); 849 850 if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address)) 851 return; 852 853 if (likely(show_unhandled_signals)) 854 show_signal_msg(regs, error_code, address, tsk); 855 856 set_signal_archinfo(address, error_code); 857 858 if (si_code == SEGV_PKUERR) 859 force_sig_pkuerr((void __user *)address, pkey); 860 861 force_sig_fault(SIGSEGV, si_code, (void __user *)address); 862 863 local_irq_disable(); 864 } 865 866 static noinline void 867 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 868 unsigned long address) 869 { 870 __bad_area_nosemaphore(regs, error_code, address, 0, SEGV_MAPERR); 871 } 872 873 static void 874 __bad_area(struct pt_regs *regs, unsigned long error_code, 875 unsigned long address, u32 pkey, int si_code) 876 { 877 struct mm_struct *mm = current->mm; 878 /* 879 * Something tried to access memory that isn't in our memory map.. 880 * Fix it, but check if it's kernel or user first.. 881 */ 882 mmap_read_unlock(mm); 883 884 __bad_area_nosemaphore(regs, error_code, address, pkey, si_code); 885 } 886 887 static noinline void 888 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address) 889 { 890 __bad_area(regs, error_code, address, 0, SEGV_MAPERR); 891 } 892 893 static inline bool bad_area_access_from_pkeys(unsigned long error_code, 894 struct vm_area_struct *vma) 895 { 896 /* This code is always called on the current mm */ 897 bool foreign = false; 898 899 if (!boot_cpu_has(X86_FEATURE_OSPKE)) 900 return false; 901 if (error_code & X86_PF_PK) 902 return true; 903 /* this checks permission keys on the VMA: */ 904 if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE), 905 (error_code & X86_PF_INSTR), foreign)) 906 return true; 907 return false; 908 } 909 910 static noinline void 911 bad_area_access_error(struct pt_regs *regs, unsigned long error_code, 912 unsigned long address, struct vm_area_struct *vma) 913 { 914 /* 915 * This OSPKE check is not strictly necessary at runtime. 916 * But, doing it this way allows compiler optimizations 917 * if pkeys are compiled out. 918 */ 919 if (bad_area_access_from_pkeys(error_code, vma)) { 920 /* 921 * A protection key fault means that the PKRU value did not allow 922 * access to some PTE. Userspace can figure out what PKRU was 923 * from the XSAVE state. This function captures the pkey from 924 * the vma and passes it to userspace so userspace can discover 925 * which protection key was set on the PTE. 926 * 927 * If we get here, we know that the hardware signaled a X86_PF_PK 928 * fault and that there was a VMA once we got in the fault 929 * handler. It does *not* guarantee that the VMA we find here 930 * was the one that we faulted on. 931 * 932 * 1. T1 : mprotect_key(foo, PAGE_SIZE, pkey=4); 933 * 2. T1 : set PKRU to deny access to pkey=4, touches page 934 * 3. T1 : faults... 935 * 4. T2: mprotect_key(foo, PAGE_SIZE, pkey=5); 936 * 5. T1 : enters fault handler, takes mmap_lock, etc... 937 * 6. T1 : reaches here, sees vma_pkey(vma)=5, when we really 938 * faulted on a pte with its pkey=4. 939 */ 940 u32 pkey = vma_pkey(vma); 941 942 __bad_area(regs, error_code, address, pkey, SEGV_PKUERR); 943 } else { 944 __bad_area(regs, error_code, address, 0, SEGV_ACCERR); 945 } 946 } 947 948 static void 949 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address, 950 vm_fault_t fault) 951 { 952 /* Kernel mode? Handle exceptions or die: */ 953 if (!user_mode(regs)) { 954 kernelmode_fixup_or_oops(regs, error_code, address, SIGBUS, BUS_ADRERR); 955 return; 956 } 957 958 /* User-space => ok to do another page fault: */ 959 if (is_prefetch(regs, error_code, address)) 960 return; 961 962 sanitize_error_code(address, &error_code); 963 964 if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address)) 965 return; 966 967 set_signal_archinfo(address, error_code); 968 969 #ifdef CONFIG_MEMORY_FAILURE 970 if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) { 971 struct task_struct *tsk = current; 972 unsigned lsb = 0; 973 974 pr_err( 975 "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n", 976 tsk->comm, tsk->pid, address); 977 if (fault & VM_FAULT_HWPOISON_LARGE) 978 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault)); 979 if (fault & VM_FAULT_HWPOISON) 980 lsb = PAGE_SHIFT; 981 force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb); 982 return; 983 } 984 #endif 985 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address); 986 } 987 988 static int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte) 989 { 990 if ((error_code & X86_PF_WRITE) && !pte_write(*pte)) 991 return 0; 992 993 if ((error_code & X86_PF_INSTR) && !pte_exec(*pte)) 994 return 0; 995 996 return 1; 997 } 998 999 /* 1000 * Handle a spurious fault caused by a stale TLB entry. 1001 * 1002 * This allows us to lazily refresh the TLB when increasing the 1003 * permissions of a kernel page (RO -> RW or NX -> X). Doing it 1004 * eagerly is very expensive since that implies doing a full 1005 * cross-processor TLB flush, even if no stale TLB entries exist 1006 * on other processors. 1007 * 1008 * Spurious faults may only occur if the TLB contains an entry with 1009 * fewer permission than the page table entry. Non-present (P = 0) 1010 * and reserved bit (R = 1) faults are never spurious. 1011 * 1012 * There are no security implications to leaving a stale TLB when 1013 * increasing the permissions on a page. 1014 * 1015 * Returns non-zero if a spurious fault was handled, zero otherwise. 1016 * 1017 * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3 1018 * (Optional Invalidation). 1019 */ 1020 static noinline int 1021 spurious_kernel_fault(unsigned long error_code, unsigned long address) 1022 { 1023 pgd_t *pgd; 1024 p4d_t *p4d; 1025 pud_t *pud; 1026 pmd_t *pmd; 1027 pte_t *pte; 1028 int ret; 1029 1030 /* 1031 * Only writes to RO or instruction fetches from NX may cause 1032 * spurious faults. 1033 * 1034 * These could be from user or supervisor accesses but the TLB 1035 * is only lazily flushed after a kernel mapping protection 1036 * change, so user accesses are not expected to cause spurious 1037 * faults. 1038 */ 1039 if (error_code != (X86_PF_WRITE | X86_PF_PROT) && 1040 error_code != (X86_PF_INSTR | X86_PF_PROT)) 1041 return 0; 1042 1043 pgd = init_mm.pgd + pgd_index(address); 1044 if (!pgd_present(*pgd)) 1045 return 0; 1046 1047 p4d = p4d_offset(pgd, address); 1048 if (!p4d_present(*p4d)) 1049 return 0; 1050 1051 if (p4d_large(*p4d)) 1052 return spurious_kernel_fault_check(error_code, (pte_t *) p4d); 1053 1054 pud = pud_offset(p4d, address); 1055 if (!pud_present(*pud)) 1056 return 0; 1057 1058 if (pud_large(*pud)) 1059 return spurious_kernel_fault_check(error_code, (pte_t *) pud); 1060 1061 pmd = pmd_offset(pud, address); 1062 if (!pmd_present(*pmd)) 1063 return 0; 1064 1065 if (pmd_large(*pmd)) 1066 return spurious_kernel_fault_check(error_code, (pte_t *) pmd); 1067 1068 pte = pte_offset_kernel(pmd, address); 1069 if (!pte_present(*pte)) 1070 return 0; 1071 1072 ret = spurious_kernel_fault_check(error_code, pte); 1073 if (!ret) 1074 return 0; 1075 1076 /* 1077 * Make sure we have permissions in PMD. 1078 * If not, then there's a bug in the page tables: 1079 */ 1080 ret = spurious_kernel_fault_check(error_code, (pte_t *) pmd); 1081 WARN_ONCE(!ret, "PMD has incorrect permission bits\n"); 1082 1083 return ret; 1084 } 1085 NOKPROBE_SYMBOL(spurious_kernel_fault); 1086 1087 int show_unhandled_signals = 1; 1088 1089 static inline int 1090 access_error(unsigned long error_code, struct vm_area_struct *vma) 1091 { 1092 /* This is only called for the current mm, so: */ 1093 bool foreign = false; 1094 1095 /* 1096 * Read or write was blocked by protection keys. This is 1097 * always an unconditional error and can never result in 1098 * a follow-up action to resolve the fault, like a COW. 1099 */ 1100 if (error_code & X86_PF_PK) 1101 return 1; 1102 1103 /* 1104 * SGX hardware blocked the access. This usually happens 1105 * when the enclave memory contents have been destroyed, like 1106 * after a suspend/resume cycle. In any case, the kernel can't 1107 * fix the cause of the fault. Handle the fault as an access 1108 * error even in cases where no actual access violation 1109 * occurred. This allows userspace to rebuild the enclave in 1110 * response to the signal. 1111 */ 1112 if (unlikely(error_code & X86_PF_SGX)) 1113 return 1; 1114 1115 /* 1116 * Make sure to check the VMA so that we do not perform 1117 * faults just to hit a X86_PF_PK as soon as we fill in a 1118 * page. 1119 */ 1120 if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE), 1121 (error_code & X86_PF_INSTR), foreign)) 1122 return 1; 1123 1124 if (error_code & X86_PF_WRITE) { 1125 /* write, present and write, not present: */ 1126 if (unlikely(!(vma->vm_flags & VM_WRITE))) 1127 return 1; 1128 return 0; 1129 } 1130 1131 /* read, present: */ 1132 if (unlikely(error_code & X86_PF_PROT)) 1133 return 1; 1134 1135 /* read, not present: */ 1136 if (unlikely(!vma_is_accessible(vma))) 1137 return 1; 1138 1139 return 0; 1140 } 1141 1142 bool fault_in_kernel_space(unsigned long address) 1143 { 1144 /* 1145 * On 64-bit systems, the vsyscall page is at an address above 1146 * TASK_SIZE_MAX, but is not considered part of the kernel 1147 * address space. 1148 */ 1149 if (IS_ENABLED(CONFIG_X86_64) && is_vsyscall_vaddr(address)) 1150 return false; 1151 1152 return address >= TASK_SIZE_MAX; 1153 } 1154 1155 /* 1156 * Called for all faults where 'address' is part of the kernel address 1157 * space. Might get called for faults that originate from *code* that 1158 * ran in userspace or the kernel. 1159 */ 1160 static void 1161 do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, 1162 unsigned long address) 1163 { 1164 /* 1165 * Protection keys exceptions only happen on user pages. We 1166 * have no user pages in the kernel portion of the address 1167 * space, so do not expect them here. 1168 */ 1169 WARN_ON_ONCE(hw_error_code & X86_PF_PK); 1170 1171 #ifdef CONFIG_X86_32 1172 /* 1173 * We can fault-in kernel-space virtual memory on-demand. The 1174 * 'reference' page table is init_mm.pgd. 1175 * 1176 * NOTE! We MUST NOT take any locks for this case. We may 1177 * be in an interrupt or a critical region, and should 1178 * only copy the information from the master page table, 1179 * nothing more. 1180 * 1181 * Before doing this on-demand faulting, ensure that the 1182 * fault is not any of the following: 1183 * 1. A fault on a PTE with a reserved bit set. 1184 * 2. A fault caused by a user-mode access. (Do not demand- 1185 * fault kernel memory due to user-mode accesses). 1186 * 3. A fault caused by a page-level protection violation. 1187 * (A demand fault would be on a non-present page which 1188 * would have X86_PF_PROT==0). 1189 * 1190 * This is only needed to close a race condition on x86-32 in 1191 * the vmalloc mapping/unmapping code. See the comment above 1192 * vmalloc_fault() for details. On x86-64 the race does not 1193 * exist as the vmalloc mappings don't need to be synchronized 1194 * there. 1195 */ 1196 if (!(hw_error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) { 1197 if (vmalloc_fault(address) >= 0) 1198 return; 1199 } 1200 #endif 1201 1202 if (is_f00f_bug(regs, hw_error_code, address)) 1203 return; 1204 1205 /* Was the fault spurious, caused by lazy TLB invalidation? */ 1206 if (spurious_kernel_fault(hw_error_code, address)) 1207 return; 1208 1209 /* kprobes don't want to hook the spurious faults: */ 1210 if (kprobe_page_fault(regs, X86_TRAP_PF)) 1211 return; 1212 1213 /* 1214 * Note, despite being a "bad area", there are quite a few 1215 * acceptable reasons to get here, such as erratum fixups 1216 * and handling kernel code that can fault, like get_user(). 1217 * 1218 * Don't take the mm semaphore here. If we fixup a prefetch 1219 * fault we could otherwise deadlock: 1220 */ 1221 bad_area_nosemaphore(regs, hw_error_code, address); 1222 } 1223 NOKPROBE_SYMBOL(do_kern_addr_fault); 1224 1225 /* 1226 * Handle faults in the user portion of the address space. Nothing in here 1227 * should check X86_PF_USER without a specific justification: for almost 1228 * all purposes, we should treat a normal kernel access to user memory 1229 * (e.g. get_user(), put_user(), etc.) the same as the WRUSS instruction. 1230 * The one exception is AC flag handling, which is, per the x86 1231 * architecture, special for WRUSS. 1232 */ 1233 static inline 1234 void do_user_addr_fault(struct pt_regs *regs, 1235 unsigned long error_code, 1236 unsigned long address) 1237 { 1238 struct vm_area_struct *vma; 1239 struct task_struct *tsk; 1240 struct mm_struct *mm; 1241 vm_fault_t fault; 1242 unsigned int flags = FAULT_FLAG_DEFAULT; 1243 1244 tsk = current; 1245 mm = tsk->mm; 1246 1247 if (unlikely((error_code & (X86_PF_USER | X86_PF_INSTR)) == X86_PF_INSTR)) { 1248 /* 1249 * Whoops, this is kernel mode code trying to execute from 1250 * user memory. Unless this is AMD erratum #93, which 1251 * corrupts RIP such that it looks like a user address, 1252 * this is unrecoverable. Don't even try to look up the 1253 * VMA or look for extable entries. 1254 */ 1255 if (is_errata93(regs, address)) 1256 return; 1257 1258 page_fault_oops(regs, error_code, address); 1259 return; 1260 } 1261 1262 /* kprobes don't want to hook the spurious faults: */ 1263 if (unlikely(kprobe_page_fault(regs, X86_TRAP_PF))) 1264 return; 1265 1266 /* 1267 * Reserved bits are never expected to be set on 1268 * entries in the user portion of the page tables. 1269 */ 1270 if (unlikely(error_code & X86_PF_RSVD)) 1271 pgtable_bad(regs, error_code, address); 1272 1273 /* 1274 * If SMAP is on, check for invalid kernel (supervisor) access to user 1275 * pages in the user address space. The odd case here is WRUSS, 1276 * which, according to the preliminary documentation, does not respect 1277 * SMAP and will have the USER bit set so, in all cases, SMAP 1278 * enforcement appears to be consistent with the USER bit. 1279 */ 1280 if (unlikely(cpu_feature_enabled(X86_FEATURE_SMAP) && 1281 !(error_code & X86_PF_USER) && 1282 !(regs->flags & X86_EFLAGS_AC))) 1283 { 1284 bad_area_nosemaphore(regs, error_code, address); 1285 return; 1286 } 1287 1288 /* 1289 * If we're in an interrupt, have no user context or are running 1290 * in a region with pagefaults disabled then we must not take the fault 1291 */ 1292 if (unlikely(faulthandler_disabled() || !mm)) { 1293 bad_area_nosemaphore(regs, error_code, address); 1294 return; 1295 } 1296 1297 /* 1298 * It's safe to allow irq's after cr2 has been saved and the 1299 * vmalloc fault has been handled. 1300 * 1301 * User-mode registers count as a user access even for any 1302 * potential system fault or CPU buglet: 1303 */ 1304 if (user_mode(regs)) { 1305 local_irq_enable(); 1306 flags |= FAULT_FLAG_USER; 1307 } else { 1308 if (regs->flags & X86_EFLAGS_IF) 1309 local_irq_enable(); 1310 } 1311 1312 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 1313 1314 if (error_code & X86_PF_WRITE) 1315 flags |= FAULT_FLAG_WRITE; 1316 if (error_code & X86_PF_INSTR) 1317 flags |= FAULT_FLAG_INSTRUCTION; 1318 1319 #ifdef CONFIG_X86_64 1320 /* 1321 * Faults in the vsyscall page might need emulation. The 1322 * vsyscall page is at a high address (>PAGE_OFFSET), but is 1323 * considered to be part of the user address space. 1324 * 1325 * The vsyscall page does not have a "real" VMA, so do this 1326 * emulation before we go searching for VMAs. 1327 * 1328 * PKRU never rejects instruction fetches, so we don't need 1329 * to consider the PF_PK bit. 1330 */ 1331 if (is_vsyscall_vaddr(address)) { 1332 if (emulate_vsyscall(error_code, regs, address)) 1333 return; 1334 } 1335 #endif 1336 1337 /* 1338 * Kernel-mode access to the user address space should only occur 1339 * on well-defined single instructions listed in the exception 1340 * tables. But, an erroneous kernel fault occurring outside one of 1341 * those areas which also holds mmap_lock might deadlock attempting 1342 * to validate the fault against the address space. 1343 * 1344 * Only do the expensive exception table search when we might be at 1345 * risk of a deadlock. This happens if we 1346 * 1. Failed to acquire mmap_lock, and 1347 * 2. The access did not originate in userspace. 1348 */ 1349 if (unlikely(!mmap_read_trylock(mm))) { 1350 if (!user_mode(regs) && !search_exception_tables(regs->ip)) { 1351 /* 1352 * Fault from code in kernel from 1353 * which we do not expect faults. 1354 */ 1355 bad_area_nosemaphore(regs, error_code, address); 1356 return; 1357 } 1358 retry: 1359 mmap_read_lock(mm); 1360 } else { 1361 /* 1362 * The above down_read_trylock() might have succeeded in 1363 * which case we'll have missed the might_sleep() from 1364 * down_read(): 1365 */ 1366 might_sleep(); 1367 } 1368 1369 vma = find_vma(mm, address); 1370 if (unlikely(!vma)) { 1371 bad_area(regs, error_code, address); 1372 return; 1373 } 1374 if (likely(vma->vm_start <= address)) 1375 goto good_area; 1376 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) { 1377 bad_area(regs, error_code, address); 1378 return; 1379 } 1380 if (unlikely(expand_stack(vma, address))) { 1381 bad_area(regs, error_code, address); 1382 return; 1383 } 1384 1385 /* 1386 * Ok, we have a good vm_area for this memory access, so 1387 * we can handle it.. 1388 */ 1389 good_area: 1390 if (unlikely(access_error(error_code, vma))) { 1391 bad_area_access_error(regs, error_code, address, vma); 1392 return; 1393 } 1394 1395 /* 1396 * If for any reason at all we couldn't handle the fault, 1397 * make sure we exit gracefully rather than endlessly redo 1398 * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if 1399 * we get VM_FAULT_RETRY back, the mmap_lock has been unlocked. 1400 * 1401 * Note that handle_userfault() may also release and reacquire mmap_lock 1402 * (and not return with VM_FAULT_RETRY), when returning to userland to 1403 * repeat the page fault later with a VM_FAULT_NOPAGE retval 1404 * (potentially after handling any pending signal during the return to 1405 * userland). The return to userland is identified whenever 1406 * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags. 1407 */ 1408 fault = handle_mm_fault(vma, address, flags, regs); 1409 1410 if (fault_signal_pending(fault, regs)) { 1411 /* 1412 * Quick path to respond to signals. The core mm code 1413 * has unlocked the mm for us if we get here. 1414 */ 1415 if (!user_mode(regs)) 1416 kernelmode_fixup_or_oops(regs, error_code, address, 1417 SIGBUS, BUS_ADRERR); 1418 return; 1419 } 1420 1421 /* 1422 * If we need to retry the mmap_lock has already been released, 1423 * and if there is a fatal signal pending there is no guarantee 1424 * that we made any progress. Handle this case first. 1425 */ 1426 if (unlikely((fault & VM_FAULT_RETRY) && 1427 (flags & FAULT_FLAG_ALLOW_RETRY))) { 1428 flags |= FAULT_FLAG_TRIED; 1429 goto retry; 1430 } 1431 1432 mmap_read_unlock(mm); 1433 if (likely(!(fault & VM_FAULT_ERROR))) 1434 return; 1435 1436 if (fatal_signal_pending(current) && !user_mode(regs)) { 1437 kernelmode_fixup_or_oops(regs, error_code, address, 0, 0); 1438 return; 1439 } 1440 1441 if (fault & VM_FAULT_OOM) { 1442 /* Kernel mode? Handle exceptions or die: */ 1443 if (!user_mode(regs)) { 1444 kernelmode_fixup_or_oops(regs, error_code, address, 1445 SIGSEGV, SEGV_MAPERR); 1446 return; 1447 } 1448 1449 /* 1450 * We ran out of memory, call the OOM killer, and return the 1451 * userspace (which will retry the fault, or kill us if we got 1452 * oom-killed): 1453 */ 1454 pagefault_out_of_memory(); 1455 } else { 1456 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON| 1457 VM_FAULT_HWPOISON_LARGE)) 1458 do_sigbus(regs, error_code, address, fault); 1459 else if (fault & VM_FAULT_SIGSEGV) 1460 bad_area_nosemaphore(regs, error_code, address); 1461 else 1462 BUG(); 1463 } 1464 1465 check_v8086_mode(regs, address, tsk); 1466 } 1467 NOKPROBE_SYMBOL(do_user_addr_fault); 1468 1469 static __always_inline void 1470 trace_page_fault_entries(struct pt_regs *regs, unsigned long error_code, 1471 unsigned long address) 1472 { 1473 if (!trace_pagefault_enabled()) 1474 return; 1475 1476 if (user_mode(regs)) 1477 trace_page_fault_user(address, regs, error_code); 1478 else 1479 trace_page_fault_kernel(address, regs, error_code); 1480 } 1481 1482 static __always_inline void 1483 handle_page_fault(struct pt_regs *regs, unsigned long error_code, 1484 unsigned long address) 1485 { 1486 trace_page_fault_entries(regs, error_code, address); 1487 1488 if (unlikely(kmmio_fault(regs, address))) 1489 return; 1490 1491 /* Was the fault on kernel-controlled part of the address space? */ 1492 if (unlikely(fault_in_kernel_space(address))) { 1493 do_kern_addr_fault(regs, error_code, address); 1494 } else { 1495 do_user_addr_fault(regs, error_code, address); 1496 /* 1497 * User address page fault handling might have reenabled 1498 * interrupts. Fixing up all potential exit points of 1499 * do_user_addr_fault() and its leaf functions is just not 1500 * doable w/o creating an unholy mess or turning the code 1501 * upside down. 1502 */ 1503 local_irq_disable(); 1504 } 1505 } 1506 1507 DEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault) 1508 { 1509 unsigned long address = read_cr2(); 1510 irqentry_state_t state; 1511 1512 prefetchw(¤t->mm->mmap_lock); 1513 1514 /* 1515 * KVM uses #PF vector to deliver 'page not present' events to guests 1516 * (asynchronous page fault mechanism). The event happens when a 1517 * userspace task is trying to access some valid (from guest's point of 1518 * view) memory which is not currently mapped by the host (e.g. the 1519 * memory is swapped out). Note, the corresponding "page ready" event 1520 * which is injected when the memory becomes available, is delived via 1521 * an interrupt mechanism and not a #PF exception 1522 * (see arch/x86/kernel/kvm.c: sysvec_kvm_asyncpf_interrupt()). 1523 * 1524 * We are relying on the interrupted context being sane (valid RSP, 1525 * relevant locks not held, etc.), which is fine as long as the 1526 * interrupted context had IF=1. We are also relying on the KVM 1527 * async pf type field and CR2 being read consistently instead of 1528 * getting values from real and async page faults mixed up. 1529 * 1530 * Fingers crossed. 1531 * 1532 * The async #PF handling code takes care of idtentry handling 1533 * itself. 1534 */ 1535 if (kvm_handle_async_pf(regs, (u32)address)) 1536 return; 1537 1538 /* 1539 * Entry handling for valid #PF from kernel mode is slightly 1540 * different: RCU is already watching and rcu_irq_enter() must not 1541 * be invoked because a kernel fault on a user space address might 1542 * sleep. 1543 * 1544 * In case the fault hit a RCU idle region the conditional entry 1545 * code reenabled RCU to avoid subsequent wreckage which helps 1546 * debugability. 1547 */ 1548 state = irqentry_enter(regs); 1549 1550 instrumentation_begin(); 1551 handle_page_fault(regs, error_code, address); 1552 instrumentation_end(); 1553 1554 irqentry_exit(regs, state); 1555 } 1556