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/bootmem.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 20 #include <asm/cpufeature.h> /* boot_cpu_has, ... */ 21 #include <asm/traps.h> /* dotraplinkage, ... */ 22 #include <asm/pgalloc.h> /* pgd_*(), ... */ 23 #include <asm/kmemcheck.h> /* kmemcheck_*(), ... */ 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 29 #define CREATE_TRACE_POINTS 30 #include <asm/trace/exceptions.h> 31 32 /* 33 * Returns 0 if mmiotrace is disabled, or if the fault is not 34 * handled by mmiotrace: 35 */ 36 static nokprobe_inline int 37 kmmio_fault(struct pt_regs *regs, unsigned long addr) 38 { 39 if (unlikely(is_kmmio_active())) 40 if (kmmio_handler(regs, addr) == 1) 41 return -1; 42 return 0; 43 } 44 45 static nokprobe_inline int kprobes_fault(struct pt_regs *regs) 46 { 47 int ret = 0; 48 49 /* kprobe_running() needs smp_processor_id() */ 50 if (kprobes_built_in() && !user_mode(regs)) { 51 preempt_disable(); 52 if (kprobe_running() && kprobe_fault_handler(regs, 14)) 53 ret = 1; 54 preempt_enable(); 55 } 56 57 return ret; 58 } 59 60 /* 61 * Prefetch quirks: 62 * 63 * 32-bit mode: 64 * 65 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch. 66 * Check that here and ignore it. 67 * 68 * 64-bit mode: 69 * 70 * Sometimes the CPU reports invalid exceptions on prefetch. 71 * Check that here and ignore it. 72 * 73 * Opcode checker based on code by Richard Brunner. 74 */ 75 static inline int 76 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr, 77 unsigned char opcode, int *prefetch) 78 { 79 unsigned char instr_hi = opcode & 0xf0; 80 unsigned char instr_lo = opcode & 0x0f; 81 82 switch (instr_hi) { 83 case 0x20: 84 case 0x30: 85 /* 86 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes. 87 * In X86_64 long mode, the CPU will signal invalid 88 * opcode if some of these prefixes are present so 89 * X86_64 will never get here anyway 90 */ 91 return ((instr_lo & 7) == 0x6); 92 #ifdef CONFIG_X86_64 93 case 0x40: 94 /* 95 * In AMD64 long mode 0x40..0x4F are valid REX prefixes 96 * Need to figure out under what instruction mode the 97 * instruction was issued. Could check the LDT for lm, 98 * but for now it's good enough to assume that long 99 * mode only uses well known segments or kernel. 100 */ 101 return (!user_mode(regs) || user_64bit_mode(regs)); 102 #endif 103 case 0x60: 104 /* 0x64 thru 0x67 are valid prefixes in all modes. */ 105 return (instr_lo & 0xC) == 0x4; 106 case 0xF0: 107 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */ 108 return !instr_lo || (instr_lo>>1) == 1; 109 case 0x00: 110 /* Prefetch instruction is 0x0F0D or 0x0F18 */ 111 if (probe_kernel_address(instr, opcode)) 112 return 0; 113 114 *prefetch = (instr_lo == 0xF) && 115 (opcode == 0x0D || opcode == 0x18); 116 return 0; 117 default: 118 return 0; 119 } 120 } 121 122 static int 123 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr) 124 { 125 unsigned char *max_instr; 126 unsigned char *instr; 127 int prefetch = 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 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE_MAX) 140 return 0; 141 142 while (instr < max_instr) { 143 unsigned char opcode; 144 145 if (probe_kernel_address(instr, opcode)) 146 break; 147 148 instr++; 149 150 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch)) 151 break; 152 } 153 return prefetch; 154 } 155 156 /* 157 * A protection key fault means that the PKRU value did not allow 158 * access to some PTE. Userspace can figure out what PKRU was 159 * from the XSAVE state, and this function fills out a field in 160 * siginfo so userspace can discover which protection key was set 161 * on the PTE. 162 * 163 * If we get here, we know that the hardware signaled a X86_PF_PK 164 * fault and that there was a VMA once we got in the fault 165 * handler. It does *not* guarantee that the VMA we find here 166 * was the one that we faulted on. 167 * 168 * 1. T1 : mprotect_key(foo, PAGE_SIZE, pkey=4); 169 * 2. T1 : set PKRU to deny access to pkey=4, touches page 170 * 3. T1 : faults... 171 * 4. T2: mprotect_key(foo, PAGE_SIZE, pkey=5); 172 * 5. T1 : enters fault handler, takes mmap_sem, etc... 173 * 6. T1 : reaches here, sees vma_pkey(vma)=5, when we really 174 * faulted on a pte with its pkey=4. 175 */ 176 static void fill_sig_info_pkey(int si_code, siginfo_t *info, u32 *pkey) 177 { 178 /* This is effectively an #ifdef */ 179 if (!boot_cpu_has(X86_FEATURE_OSPKE)) 180 return; 181 182 /* Fault not from Protection Keys: nothing to do */ 183 if (si_code != SEGV_PKUERR) 184 return; 185 /* 186 * force_sig_info_fault() is called from a number of 187 * contexts, some of which have a VMA and some of which 188 * do not. The X86_PF_PK handing happens after we have a 189 * valid VMA, so we should never reach this without a 190 * valid VMA. 191 */ 192 if (!pkey) { 193 WARN_ONCE(1, "PKU fault with no VMA passed in"); 194 info->si_pkey = 0; 195 return; 196 } 197 /* 198 * si_pkey should be thought of as a strong hint, but not 199 * absolutely guranteed to be 100% accurate because of 200 * the race explained above. 201 */ 202 info->si_pkey = *pkey; 203 } 204 205 static void 206 force_sig_info_fault(int si_signo, int si_code, unsigned long address, 207 struct task_struct *tsk, u32 *pkey, int fault) 208 { 209 unsigned lsb = 0; 210 siginfo_t info; 211 212 info.si_signo = si_signo; 213 info.si_errno = 0; 214 info.si_code = si_code; 215 info.si_addr = (void __user *)address; 216 if (fault & VM_FAULT_HWPOISON_LARGE) 217 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault)); 218 if (fault & VM_FAULT_HWPOISON) 219 lsb = PAGE_SHIFT; 220 info.si_addr_lsb = lsb; 221 222 fill_sig_info_pkey(si_code, &info, pkey); 223 224 force_sig_info(si_signo, &info, tsk); 225 } 226 227 DEFINE_SPINLOCK(pgd_lock); 228 LIST_HEAD(pgd_list); 229 230 #ifdef CONFIG_X86_32 231 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address) 232 { 233 unsigned index = pgd_index(address); 234 pgd_t *pgd_k; 235 p4d_t *p4d, *p4d_k; 236 pud_t *pud, *pud_k; 237 pmd_t *pmd, *pmd_k; 238 239 pgd += index; 240 pgd_k = init_mm.pgd + index; 241 242 if (!pgd_present(*pgd_k)) 243 return NULL; 244 245 /* 246 * set_pgd(pgd, *pgd_k); here would be useless on PAE 247 * and redundant with the set_pmd() on non-PAE. As would 248 * set_p4d/set_pud. 249 */ 250 p4d = p4d_offset(pgd, address); 251 p4d_k = p4d_offset(pgd_k, address); 252 if (!p4d_present(*p4d_k)) 253 return NULL; 254 255 pud = pud_offset(p4d, address); 256 pud_k = pud_offset(p4d_k, address); 257 if (!pud_present(*pud_k)) 258 return NULL; 259 260 pmd = pmd_offset(pud, address); 261 pmd_k = pmd_offset(pud_k, address); 262 if (!pmd_present(*pmd_k)) 263 return NULL; 264 265 if (!pmd_present(*pmd)) 266 set_pmd(pmd, *pmd_k); 267 else 268 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k)); 269 270 return pmd_k; 271 } 272 273 void vmalloc_sync_all(void) 274 { 275 unsigned long address; 276 277 if (SHARED_KERNEL_PMD) 278 return; 279 280 for (address = VMALLOC_START & PMD_MASK; 281 address >= TASK_SIZE_MAX && address < FIXADDR_TOP; 282 address += PMD_SIZE) { 283 struct page *page; 284 285 spin_lock(&pgd_lock); 286 list_for_each_entry(page, &pgd_list, lru) { 287 spinlock_t *pgt_lock; 288 pmd_t *ret; 289 290 /* the pgt_lock only for Xen */ 291 pgt_lock = &pgd_page_get_mm(page)->page_table_lock; 292 293 spin_lock(pgt_lock); 294 ret = vmalloc_sync_one(page_address(page), address); 295 spin_unlock(pgt_lock); 296 297 if (!ret) 298 break; 299 } 300 spin_unlock(&pgd_lock); 301 } 302 } 303 304 /* 305 * 32-bit: 306 * 307 * Handle a fault on the vmalloc or module mapping area 308 */ 309 static noinline int vmalloc_fault(unsigned long address) 310 { 311 unsigned long pgd_paddr; 312 pmd_t *pmd_k; 313 pte_t *pte_k; 314 315 /* Make sure we are in vmalloc area: */ 316 if (!(address >= VMALLOC_START && address < VMALLOC_END)) 317 return -1; 318 319 WARN_ON_ONCE(in_nmi()); 320 321 /* 322 * Synchronize this task's top level page-table 323 * with the 'reference' page table. 324 * 325 * Do _not_ use "current" here. We might be inside 326 * an interrupt in the middle of a task switch.. 327 */ 328 pgd_paddr = read_cr3_pa(); 329 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address); 330 if (!pmd_k) 331 return -1; 332 333 if (pmd_huge(*pmd_k)) 334 return 0; 335 336 pte_k = pte_offset_kernel(pmd_k, address); 337 if (!pte_present(*pte_k)) 338 return -1; 339 340 return 0; 341 } 342 NOKPROBE_SYMBOL(vmalloc_fault); 343 344 /* 345 * Did it hit the DOS screen memory VA from vm86 mode? 346 */ 347 static inline void 348 check_v8086_mode(struct pt_regs *regs, unsigned long address, 349 struct task_struct *tsk) 350 { 351 #ifdef CONFIG_VM86 352 unsigned long bit; 353 354 if (!v8086_mode(regs) || !tsk->thread.vm86) 355 return; 356 357 bit = (address - 0xA0000) >> PAGE_SHIFT; 358 if (bit < 32) 359 tsk->thread.vm86->screen_bitmap |= 1 << bit; 360 #endif 361 } 362 363 static bool low_pfn(unsigned long pfn) 364 { 365 return pfn < max_low_pfn; 366 } 367 368 static void dump_pagetable(unsigned long address) 369 { 370 pgd_t *base = __va(read_cr3_pa()); 371 pgd_t *pgd = &base[pgd_index(address)]; 372 p4d_t *p4d; 373 pud_t *pud; 374 pmd_t *pmd; 375 pte_t *pte; 376 377 #ifdef CONFIG_X86_PAE 378 pr_info("*pdpt = %016Lx ", pgd_val(*pgd)); 379 if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd)) 380 goto out; 381 #define pr_pde pr_cont 382 #else 383 #define pr_pde pr_info 384 #endif 385 p4d = p4d_offset(pgd, address); 386 pud = pud_offset(p4d, address); 387 pmd = pmd_offset(pud, address); 388 pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd)); 389 #undef pr_pde 390 391 /* 392 * We must not directly access the pte in the highpte 393 * case if the page table is located in highmem. 394 * And let's rather not kmap-atomic the pte, just in case 395 * it's allocated already: 396 */ 397 if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd)) 398 goto out; 399 400 pte = pte_offset_kernel(pmd, address); 401 pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte)); 402 out: 403 pr_cont("\n"); 404 } 405 406 #else /* CONFIG_X86_64: */ 407 408 void vmalloc_sync_all(void) 409 { 410 sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END); 411 } 412 413 /* 414 * 64-bit: 415 * 416 * Handle a fault on the vmalloc area 417 */ 418 static noinline int vmalloc_fault(unsigned long address) 419 { 420 pgd_t *pgd, *pgd_ref; 421 p4d_t *p4d, *p4d_ref; 422 pud_t *pud, *pud_ref; 423 pmd_t *pmd, *pmd_ref; 424 pte_t *pte, *pte_ref; 425 426 /* Make sure we are in vmalloc area: */ 427 if (!(address >= VMALLOC_START && address < VMALLOC_END)) 428 return -1; 429 430 WARN_ON_ONCE(in_nmi()); 431 432 /* 433 * Copy kernel mappings over when needed. This can also 434 * happen within a race in page table update. In the later 435 * case just flush: 436 */ 437 pgd = (pgd_t *)__va(read_cr3_pa()) + pgd_index(address); 438 pgd_ref = pgd_offset_k(address); 439 if (pgd_none(*pgd_ref)) 440 return -1; 441 442 if (pgd_none(*pgd)) { 443 set_pgd(pgd, *pgd_ref); 444 arch_flush_lazy_mmu_mode(); 445 } else if (CONFIG_PGTABLE_LEVELS > 4) { 446 /* 447 * With folded p4d, pgd_none() is always false, so the pgd may 448 * point to an empty page table entry and pgd_page_vaddr() 449 * will return garbage. 450 * 451 * We will do the correct sanity check on the p4d level. 452 */ 453 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref)); 454 } 455 456 /* With 4-level paging, copying happens on the p4d level. */ 457 p4d = p4d_offset(pgd, address); 458 p4d_ref = p4d_offset(pgd_ref, address); 459 if (p4d_none(*p4d_ref)) 460 return -1; 461 462 if (p4d_none(*p4d)) { 463 set_p4d(p4d, *p4d_ref); 464 arch_flush_lazy_mmu_mode(); 465 } else { 466 BUG_ON(p4d_pfn(*p4d) != p4d_pfn(*p4d_ref)); 467 } 468 469 /* 470 * Below here mismatches are bugs because these lower tables 471 * are shared: 472 */ 473 474 pud = pud_offset(p4d, address); 475 pud_ref = pud_offset(p4d_ref, address); 476 if (pud_none(*pud_ref)) 477 return -1; 478 479 if (pud_none(*pud) || pud_pfn(*pud) != pud_pfn(*pud_ref)) 480 BUG(); 481 482 if (pud_huge(*pud)) 483 return 0; 484 485 pmd = pmd_offset(pud, address); 486 pmd_ref = pmd_offset(pud_ref, address); 487 if (pmd_none(*pmd_ref)) 488 return -1; 489 490 if (pmd_none(*pmd) || pmd_pfn(*pmd) != pmd_pfn(*pmd_ref)) 491 BUG(); 492 493 if (pmd_huge(*pmd)) 494 return 0; 495 496 pte_ref = pte_offset_kernel(pmd_ref, address); 497 if (!pte_present(*pte_ref)) 498 return -1; 499 500 pte = pte_offset_kernel(pmd, address); 501 502 /* 503 * Don't use pte_page here, because the mappings can point 504 * outside mem_map, and the NUMA hash lookup cannot handle 505 * that: 506 */ 507 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref)) 508 BUG(); 509 510 return 0; 511 } 512 NOKPROBE_SYMBOL(vmalloc_fault); 513 514 #ifdef CONFIG_CPU_SUP_AMD 515 static const char errata93_warning[] = 516 KERN_ERR 517 "******* Your BIOS seems to not contain a fix for K8 errata #93\n" 518 "******* Working around it, but it may cause SEGVs or burn power.\n" 519 "******* Please consider a BIOS update.\n" 520 "******* Disabling USB legacy in the BIOS may also help.\n"; 521 #endif 522 523 /* 524 * No vm86 mode in 64-bit mode: 525 */ 526 static inline void 527 check_v8086_mode(struct pt_regs *regs, unsigned long address, 528 struct task_struct *tsk) 529 { 530 } 531 532 static int bad_address(void *p) 533 { 534 unsigned long dummy; 535 536 return probe_kernel_address((unsigned long *)p, dummy); 537 } 538 539 static void dump_pagetable(unsigned long address) 540 { 541 pgd_t *base = __va(read_cr3_pa()); 542 pgd_t *pgd = base + pgd_index(address); 543 p4d_t *p4d; 544 pud_t *pud; 545 pmd_t *pmd; 546 pte_t *pte; 547 548 if (bad_address(pgd)) 549 goto bad; 550 551 pr_info("PGD %lx ", pgd_val(*pgd)); 552 553 if (!pgd_present(*pgd)) 554 goto out; 555 556 p4d = p4d_offset(pgd, address); 557 if (bad_address(p4d)) 558 goto bad; 559 560 pr_cont("P4D %lx ", p4d_val(*p4d)); 561 if (!p4d_present(*p4d) || p4d_large(*p4d)) 562 goto out; 563 564 pud = pud_offset(p4d, address); 565 if (bad_address(pud)) 566 goto bad; 567 568 pr_cont("PUD %lx ", pud_val(*pud)); 569 if (!pud_present(*pud) || pud_large(*pud)) 570 goto out; 571 572 pmd = pmd_offset(pud, address); 573 if (bad_address(pmd)) 574 goto bad; 575 576 pr_cont("PMD %lx ", pmd_val(*pmd)); 577 if (!pmd_present(*pmd) || pmd_large(*pmd)) 578 goto out; 579 580 pte = pte_offset_kernel(pmd, address); 581 if (bad_address(pte)) 582 goto bad; 583 584 pr_cont("PTE %lx", pte_val(*pte)); 585 out: 586 pr_cont("\n"); 587 return; 588 bad: 589 pr_info("BAD\n"); 590 } 591 592 #endif /* CONFIG_X86_64 */ 593 594 /* 595 * Workaround for K8 erratum #93 & buggy BIOS. 596 * 597 * BIOS SMM functions are required to use a specific workaround 598 * to avoid corruption of the 64bit RIP register on C stepping K8. 599 * 600 * A lot of BIOS that didn't get tested properly miss this. 601 * 602 * The OS sees this as a page fault with the upper 32bits of RIP cleared. 603 * Try to work around it here. 604 * 605 * Note we only handle faults in kernel here. 606 * Does nothing on 32-bit. 607 */ 608 static int is_errata93(struct pt_regs *regs, unsigned long address) 609 { 610 #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD) 611 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD 612 || boot_cpu_data.x86 != 0xf) 613 return 0; 614 615 if (address != regs->ip) 616 return 0; 617 618 if ((address >> 32) != 0) 619 return 0; 620 621 address |= 0xffffffffUL << 32; 622 if ((address >= (u64)_stext && address <= (u64)_etext) || 623 (address >= MODULES_VADDR && address <= MODULES_END)) { 624 printk_once(errata93_warning); 625 regs->ip = address; 626 return 1; 627 } 628 #endif 629 return 0; 630 } 631 632 /* 633 * Work around K8 erratum #100 K8 in compat mode occasionally jumps 634 * to illegal addresses >4GB. 635 * 636 * We catch this in the page fault handler because these addresses 637 * are not reachable. Just detect this case and return. Any code 638 * segment in LDT is compatibility mode. 639 */ 640 static int is_errata100(struct pt_regs *regs, unsigned long address) 641 { 642 #ifdef CONFIG_X86_64 643 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32)) 644 return 1; 645 #endif 646 return 0; 647 } 648 649 static int is_f00f_bug(struct pt_regs *regs, unsigned long address) 650 { 651 #ifdef CONFIG_X86_F00F_BUG 652 unsigned long nr; 653 654 /* 655 * Pentium F0 0F C7 C8 bug workaround: 656 */ 657 if (boot_cpu_has_bug(X86_BUG_F00F)) { 658 nr = (address - idt_descr.address) >> 3; 659 660 if (nr == 6) { 661 do_invalid_op(regs, 0); 662 return 1; 663 } 664 } 665 #endif 666 return 0; 667 } 668 669 static const char nx_warning[] = KERN_CRIT 670 "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n"; 671 static const char smep_warning[] = KERN_CRIT 672 "unable to execute userspace code (SMEP?) (uid: %d)\n"; 673 674 static void 675 show_fault_oops(struct pt_regs *regs, unsigned long error_code, 676 unsigned long address) 677 { 678 if (!oops_may_print()) 679 return; 680 681 if (error_code & X86_PF_INSTR) { 682 unsigned int level; 683 pgd_t *pgd; 684 pte_t *pte; 685 686 pgd = __va(read_cr3_pa()); 687 pgd += pgd_index(address); 688 689 pte = lookup_address_in_pgd(pgd, address, &level); 690 691 if (pte && pte_present(*pte) && !pte_exec(*pte)) 692 printk(nx_warning, from_kuid(&init_user_ns, current_uid())); 693 if (pte && pte_present(*pte) && pte_exec(*pte) && 694 (pgd_flags(*pgd) & _PAGE_USER) && 695 (__read_cr4() & X86_CR4_SMEP)) 696 printk(smep_warning, from_kuid(&init_user_ns, current_uid())); 697 } 698 699 printk(KERN_ALERT "BUG: unable to handle kernel "); 700 if (address < PAGE_SIZE) 701 printk(KERN_CONT "NULL pointer dereference"); 702 else 703 printk(KERN_CONT "paging request"); 704 705 printk(KERN_CONT " at %p\n", (void *) address); 706 printk(KERN_ALERT "IP: %pS\n", (void *)regs->ip); 707 708 dump_pagetable(address); 709 } 710 711 static noinline void 712 pgtable_bad(struct pt_regs *regs, unsigned long error_code, 713 unsigned long address) 714 { 715 struct task_struct *tsk; 716 unsigned long flags; 717 int sig; 718 719 flags = oops_begin(); 720 tsk = current; 721 sig = SIGKILL; 722 723 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n", 724 tsk->comm, address); 725 dump_pagetable(address); 726 727 tsk->thread.cr2 = address; 728 tsk->thread.trap_nr = X86_TRAP_PF; 729 tsk->thread.error_code = error_code; 730 731 if (__die("Bad pagetable", regs, error_code)) 732 sig = 0; 733 734 oops_end(flags, regs, sig); 735 } 736 737 static noinline void 738 no_context(struct pt_regs *regs, unsigned long error_code, 739 unsigned long address, int signal, int si_code) 740 { 741 struct task_struct *tsk = current; 742 unsigned long flags; 743 int sig; 744 745 /* Are we prepared to handle this kernel fault? */ 746 if (fixup_exception(regs, X86_TRAP_PF)) { 747 /* 748 * Any interrupt that takes a fault gets the fixup. This makes 749 * the below recursive fault logic only apply to a faults from 750 * task context. 751 */ 752 if (in_interrupt()) 753 return; 754 755 /* 756 * Per the above we're !in_interrupt(), aka. task context. 757 * 758 * In this case we need to make sure we're not recursively 759 * faulting through the emulate_vsyscall() logic. 760 */ 761 if (current->thread.sig_on_uaccess_err && signal) { 762 tsk->thread.trap_nr = X86_TRAP_PF; 763 tsk->thread.error_code = error_code | X86_PF_USER; 764 tsk->thread.cr2 = address; 765 766 /* XXX: hwpoison faults will set the wrong code. */ 767 force_sig_info_fault(signal, si_code, address, 768 tsk, NULL, 0); 769 } 770 771 /* 772 * Barring that, we can do the fixup and be happy. 773 */ 774 return; 775 } 776 777 #ifdef CONFIG_VMAP_STACK 778 /* 779 * Stack overflow? During boot, we can fault near the initial 780 * stack in the direct map, but that's not an overflow -- check 781 * that we're in vmalloc space to avoid this. 782 */ 783 if (is_vmalloc_addr((void *)address) && 784 (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) || 785 address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) { 786 unsigned long stack = this_cpu_read(orig_ist.ist[DOUBLEFAULT_STACK]) - sizeof(void *); 787 /* 788 * We're likely to be running with very little stack space 789 * left. It's plausible that we'd hit this condition but 790 * double-fault even before we get this far, in which case 791 * we're fine: the double-fault handler will deal with it. 792 * 793 * We don't want to make it all the way into the oops code 794 * and then double-fault, though, because we're likely to 795 * break the console driver and lose most of the stack dump. 796 */ 797 asm volatile ("movq %[stack], %%rsp\n\t" 798 "call handle_stack_overflow\n\t" 799 "1: jmp 1b" 800 : ASM_CALL_CONSTRAINT 801 : "D" ("kernel stack overflow (page fault)"), 802 "S" (regs), "d" (address), 803 [stack] "rm" (stack)); 804 unreachable(); 805 } 806 #endif 807 808 /* 809 * 32-bit: 810 * 811 * Valid to do another page fault here, because if this fault 812 * had been triggered by is_prefetch fixup_exception would have 813 * handled it. 814 * 815 * 64-bit: 816 * 817 * Hall of shame of CPU/BIOS bugs. 818 */ 819 if (is_prefetch(regs, error_code, address)) 820 return; 821 822 if (is_errata93(regs, address)) 823 return; 824 825 /* 826 * Oops. The kernel tried to access some bad page. We'll have to 827 * terminate things with extreme prejudice: 828 */ 829 flags = oops_begin(); 830 831 show_fault_oops(regs, error_code, address); 832 833 if (task_stack_end_corrupted(tsk)) 834 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n"); 835 836 tsk->thread.cr2 = address; 837 tsk->thread.trap_nr = X86_TRAP_PF; 838 tsk->thread.error_code = error_code; 839 840 sig = SIGKILL; 841 if (__die("Oops", regs, error_code)) 842 sig = 0; 843 844 /* Executive summary in case the body of the oops scrolled away */ 845 printk(KERN_DEFAULT "CR2: %016lx\n", address); 846 847 oops_end(flags, regs, sig); 848 } 849 850 /* 851 * Print out info about fatal segfaults, if the show_unhandled_signals 852 * sysctl is set: 853 */ 854 static inline void 855 show_signal_msg(struct pt_regs *regs, unsigned long error_code, 856 unsigned long address, struct task_struct *tsk) 857 { 858 if (!unhandled_signal(tsk, SIGSEGV)) 859 return; 860 861 if (!printk_ratelimit()) 862 return; 863 864 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx", 865 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG, 866 tsk->comm, task_pid_nr(tsk), address, 867 (void *)regs->ip, (void *)regs->sp, error_code); 868 869 print_vma_addr(KERN_CONT " in ", regs->ip); 870 871 printk(KERN_CONT "\n"); 872 } 873 874 static void 875 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 876 unsigned long address, u32 *pkey, int si_code) 877 { 878 struct task_struct *tsk = current; 879 880 /* User mode accesses just cause a SIGSEGV */ 881 if (error_code & X86_PF_USER) { 882 /* 883 * It's possible to have interrupts off here: 884 */ 885 local_irq_enable(); 886 887 /* 888 * Valid to do another page fault here because this one came 889 * from user space: 890 */ 891 if (is_prefetch(regs, error_code, address)) 892 return; 893 894 if (is_errata100(regs, address)) 895 return; 896 897 #ifdef CONFIG_X86_64 898 /* 899 * Instruction fetch faults in the vsyscall page might need 900 * emulation. 901 */ 902 if (unlikely((error_code & X86_PF_INSTR) && 903 ((address & ~0xfff) == VSYSCALL_ADDR))) { 904 if (emulate_vsyscall(regs, address)) 905 return; 906 } 907 #endif 908 909 /* 910 * To avoid leaking information about the kernel page table 911 * layout, pretend that user-mode accesses to kernel addresses 912 * are always protection faults. 913 */ 914 if (address >= TASK_SIZE_MAX) 915 error_code |= X86_PF_PROT; 916 917 if (likely(show_unhandled_signals)) 918 show_signal_msg(regs, error_code, address, tsk); 919 920 tsk->thread.cr2 = address; 921 tsk->thread.error_code = error_code; 922 tsk->thread.trap_nr = X86_TRAP_PF; 923 924 force_sig_info_fault(SIGSEGV, si_code, address, tsk, pkey, 0); 925 926 return; 927 } 928 929 if (is_f00f_bug(regs, address)) 930 return; 931 932 no_context(regs, error_code, address, SIGSEGV, si_code); 933 } 934 935 static noinline void 936 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 937 unsigned long address, u32 *pkey) 938 { 939 __bad_area_nosemaphore(regs, error_code, address, pkey, SEGV_MAPERR); 940 } 941 942 static void 943 __bad_area(struct pt_regs *regs, unsigned long error_code, 944 unsigned long address, struct vm_area_struct *vma, int si_code) 945 { 946 struct mm_struct *mm = current->mm; 947 u32 pkey; 948 949 if (vma) 950 pkey = vma_pkey(vma); 951 952 /* 953 * Something tried to access memory that isn't in our memory map.. 954 * Fix it, but check if it's kernel or user first.. 955 */ 956 up_read(&mm->mmap_sem); 957 958 __bad_area_nosemaphore(regs, error_code, address, 959 (vma) ? &pkey : NULL, si_code); 960 } 961 962 static noinline void 963 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address) 964 { 965 __bad_area(regs, error_code, address, NULL, SEGV_MAPERR); 966 } 967 968 static inline bool bad_area_access_from_pkeys(unsigned long error_code, 969 struct vm_area_struct *vma) 970 { 971 /* This code is always called on the current mm */ 972 bool foreign = false; 973 974 if (!boot_cpu_has(X86_FEATURE_OSPKE)) 975 return false; 976 if (error_code & X86_PF_PK) 977 return true; 978 /* this checks permission keys on the VMA: */ 979 if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE), 980 (error_code & X86_PF_INSTR), foreign)) 981 return true; 982 return false; 983 } 984 985 static noinline void 986 bad_area_access_error(struct pt_regs *regs, unsigned long error_code, 987 unsigned long address, struct vm_area_struct *vma) 988 { 989 /* 990 * This OSPKE check is not strictly necessary at runtime. 991 * But, doing it this way allows compiler optimizations 992 * if pkeys are compiled out. 993 */ 994 if (bad_area_access_from_pkeys(error_code, vma)) 995 __bad_area(regs, error_code, address, vma, SEGV_PKUERR); 996 else 997 __bad_area(regs, error_code, address, vma, SEGV_ACCERR); 998 } 999 1000 static void 1001 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address, 1002 u32 *pkey, unsigned int fault) 1003 { 1004 struct task_struct *tsk = current; 1005 int code = BUS_ADRERR; 1006 1007 /* Kernel mode? Handle exceptions or die: */ 1008 if (!(error_code & X86_PF_USER)) { 1009 no_context(regs, error_code, address, SIGBUS, BUS_ADRERR); 1010 return; 1011 } 1012 1013 /* User-space => ok to do another page fault: */ 1014 if (is_prefetch(regs, error_code, address)) 1015 return; 1016 1017 tsk->thread.cr2 = address; 1018 tsk->thread.error_code = error_code; 1019 tsk->thread.trap_nr = X86_TRAP_PF; 1020 1021 #ifdef CONFIG_MEMORY_FAILURE 1022 if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) { 1023 printk(KERN_ERR 1024 "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n", 1025 tsk->comm, tsk->pid, address); 1026 code = BUS_MCEERR_AR; 1027 } 1028 #endif 1029 force_sig_info_fault(SIGBUS, code, address, tsk, pkey, fault); 1030 } 1031 1032 static noinline void 1033 mm_fault_error(struct pt_regs *regs, unsigned long error_code, 1034 unsigned long address, u32 *pkey, unsigned int fault) 1035 { 1036 if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) { 1037 no_context(regs, error_code, address, 0, 0); 1038 return; 1039 } 1040 1041 if (fault & VM_FAULT_OOM) { 1042 /* Kernel mode? Handle exceptions or die: */ 1043 if (!(error_code & X86_PF_USER)) { 1044 no_context(regs, error_code, address, 1045 SIGSEGV, SEGV_MAPERR); 1046 return; 1047 } 1048 1049 /* 1050 * We ran out of memory, call the OOM killer, and return the 1051 * userspace (which will retry the fault, or kill us if we got 1052 * oom-killed): 1053 */ 1054 pagefault_out_of_memory(); 1055 } else { 1056 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON| 1057 VM_FAULT_HWPOISON_LARGE)) 1058 do_sigbus(regs, error_code, address, pkey, fault); 1059 else if (fault & VM_FAULT_SIGSEGV) 1060 bad_area_nosemaphore(regs, error_code, address, pkey); 1061 else 1062 BUG(); 1063 } 1064 } 1065 1066 static int spurious_fault_check(unsigned long error_code, pte_t *pte) 1067 { 1068 if ((error_code & X86_PF_WRITE) && !pte_write(*pte)) 1069 return 0; 1070 1071 if ((error_code & X86_PF_INSTR) && !pte_exec(*pte)) 1072 return 0; 1073 /* 1074 * Note: We do not do lazy flushing on protection key 1075 * changes, so no spurious fault will ever set X86_PF_PK. 1076 */ 1077 if ((error_code & X86_PF_PK)) 1078 return 1; 1079 1080 return 1; 1081 } 1082 1083 /* 1084 * Handle a spurious fault caused by a stale TLB entry. 1085 * 1086 * This allows us to lazily refresh the TLB when increasing the 1087 * permissions of a kernel page (RO -> RW or NX -> X). Doing it 1088 * eagerly is very expensive since that implies doing a full 1089 * cross-processor TLB flush, even if no stale TLB entries exist 1090 * on other processors. 1091 * 1092 * Spurious faults may only occur if the TLB contains an entry with 1093 * fewer permission than the page table entry. Non-present (P = 0) 1094 * and reserved bit (R = 1) faults are never spurious. 1095 * 1096 * There are no security implications to leaving a stale TLB when 1097 * increasing the permissions on a page. 1098 * 1099 * Returns non-zero if a spurious fault was handled, zero otherwise. 1100 * 1101 * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3 1102 * (Optional Invalidation). 1103 */ 1104 static noinline int 1105 spurious_fault(unsigned long error_code, unsigned long address) 1106 { 1107 pgd_t *pgd; 1108 p4d_t *p4d; 1109 pud_t *pud; 1110 pmd_t *pmd; 1111 pte_t *pte; 1112 int ret; 1113 1114 /* 1115 * Only writes to RO or instruction fetches from NX may cause 1116 * spurious faults. 1117 * 1118 * These could be from user or supervisor accesses but the TLB 1119 * is only lazily flushed after a kernel mapping protection 1120 * change, so user accesses are not expected to cause spurious 1121 * faults. 1122 */ 1123 if (error_code != (X86_PF_WRITE | X86_PF_PROT) && 1124 error_code != (X86_PF_INSTR | X86_PF_PROT)) 1125 return 0; 1126 1127 pgd = init_mm.pgd + pgd_index(address); 1128 if (!pgd_present(*pgd)) 1129 return 0; 1130 1131 p4d = p4d_offset(pgd, address); 1132 if (!p4d_present(*p4d)) 1133 return 0; 1134 1135 if (p4d_large(*p4d)) 1136 return spurious_fault_check(error_code, (pte_t *) p4d); 1137 1138 pud = pud_offset(p4d, address); 1139 if (!pud_present(*pud)) 1140 return 0; 1141 1142 if (pud_large(*pud)) 1143 return spurious_fault_check(error_code, (pte_t *) pud); 1144 1145 pmd = pmd_offset(pud, address); 1146 if (!pmd_present(*pmd)) 1147 return 0; 1148 1149 if (pmd_large(*pmd)) 1150 return spurious_fault_check(error_code, (pte_t *) pmd); 1151 1152 pte = pte_offset_kernel(pmd, address); 1153 if (!pte_present(*pte)) 1154 return 0; 1155 1156 ret = spurious_fault_check(error_code, pte); 1157 if (!ret) 1158 return 0; 1159 1160 /* 1161 * Make sure we have permissions in PMD. 1162 * If not, then there's a bug in the page tables: 1163 */ 1164 ret = spurious_fault_check(error_code, (pte_t *) pmd); 1165 WARN_ONCE(!ret, "PMD has incorrect permission bits\n"); 1166 1167 return ret; 1168 } 1169 NOKPROBE_SYMBOL(spurious_fault); 1170 1171 int show_unhandled_signals = 1; 1172 1173 static inline int 1174 access_error(unsigned long error_code, struct vm_area_struct *vma) 1175 { 1176 /* This is only called for the current mm, so: */ 1177 bool foreign = false; 1178 1179 /* 1180 * Read or write was blocked by protection keys. This is 1181 * always an unconditional error and can never result in 1182 * a follow-up action to resolve the fault, like a COW. 1183 */ 1184 if (error_code & X86_PF_PK) 1185 return 1; 1186 1187 /* 1188 * Make sure to check the VMA so that we do not perform 1189 * faults just to hit a X86_PF_PK as soon as we fill in a 1190 * page. 1191 */ 1192 if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE), 1193 (error_code & X86_PF_INSTR), foreign)) 1194 return 1; 1195 1196 if (error_code & X86_PF_WRITE) { 1197 /* write, present and write, not present: */ 1198 if (unlikely(!(vma->vm_flags & VM_WRITE))) 1199 return 1; 1200 return 0; 1201 } 1202 1203 /* read, present: */ 1204 if (unlikely(error_code & X86_PF_PROT)) 1205 return 1; 1206 1207 /* read, not present: */ 1208 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))) 1209 return 1; 1210 1211 return 0; 1212 } 1213 1214 static int fault_in_kernel_space(unsigned long address) 1215 { 1216 return address >= TASK_SIZE_MAX; 1217 } 1218 1219 static inline bool smap_violation(int error_code, struct pt_regs *regs) 1220 { 1221 if (!IS_ENABLED(CONFIG_X86_SMAP)) 1222 return false; 1223 1224 if (!static_cpu_has(X86_FEATURE_SMAP)) 1225 return false; 1226 1227 if (error_code & X86_PF_USER) 1228 return false; 1229 1230 if (!user_mode(regs) && (regs->flags & X86_EFLAGS_AC)) 1231 return false; 1232 1233 return true; 1234 } 1235 1236 /* 1237 * This routine handles page faults. It determines the address, 1238 * and the problem, and then passes it off to one of the appropriate 1239 * routines. 1240 */ 1241 static noinline void 1242 __do_page_fault(struct pt_regs *regs, unsigned long error_code, 1243 unsigned long address) 1244 { 1245 struct vm_area_struct *vma; 1246 struct task_struct *tsk; 1247 struct mm_struct *mm; 1248 int fault, major = 0; 1249 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 1250 u32 pkey; 1251 1252 tsk = current; 1253 mm = tsk->mm; 1254 1255 /* 1256 * Detect and handle instructions that would cause a page fault for 1257 * both a tracked kernel page and a userspace page. 1258 */ 1259 if (kmemcheck_active(regs)) 1260 kmemcheck_hide(regs); 1261 prefetchw(&mm->mmap_sem); 1262 1263 if (unlikely(kmmio_fault(regs, address))) 1264 return; 1265 1266 /* 1267 * We fault-in kernel-space virtual memory on-demand. The 1268 * 'reference' page table is init_mm.pgd. 1269 * 1270 * NOTE! We MUST NOT take any locks for this case. We may 1271 * be in an interrupt or a critical region, and should 1272 * only copy the information from the master page table, 1273 * nothing more. 1274 * 1275 * This verifies that the fault happens in kernel space 1276 * (error_code & 4) == 0, and that the fault was not a 1277 * protection error (error_code & 9) == 0. 1278 */ 1279 if (unlikely(fault_in_kernel_space(address))) { 1280 if (!(error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) { 1281 if (vmalloc_fault(address) >= 0) 1282 return; 1283 1284 if (kmemcheck_fault(regs, address, error_code)) 1285 return; 1286 } 1287 1288 /* Can handle a stale RO->RW TLB: */ 1289 if (spurious_fault(error_code, address)) 1290 return; 1291 1292 /* kprobes don't want to hook the spurious faults: */ 1293 if (kprobes_fault(regs)) 1294 return; 1295 /* 1296 * Don't take the mm semaphore here. If we fixup a prefetch 1297 * fault we could otherwise deadlock: 1298 */ 1299 bad_area_nosemaphore(regs, error_code, address, NULL); 1300 1301 return; 1302 } 1303 1304 /* kprobes don't want to hook the spurious faults: */ 1305 if (unlikely(kprobes_fault(regs))) 1306 return; 1307 1308 if (unlikely(error_code & X86_PF_RSVD)) 1309 pgtable_bad(regs, error_code, address); 1310 1311 if (unlikely(smap_violation(error_code, regs))) { 1312 bad_area_nosemaphore(regs, error_code, address, NULL); 1313 return; 1314 } 1315 1316 /* 1317 * If we're in an interrupt, have no user context or are running 1318 * in a region with pagefaults disabled then we must not take the fault 1319 */ 1320 if (unlikely(faulthandler_disabled() || !mm)) { 1321 bad_area_nosemaphore(regs, error_code, address, NULL); 1322 return; 1323 } 1324 1325 /* 1326 * It's safe to allow irq's after cr2 has been saved and the 1327 * vmalloc fault has been handled. 1328 * 1329 * User-mode registers count as a user access even for any 1330 * potential system fault or CPU buglet: 1331 */ 1332 if (user_mode(regs)) { 1333 local_irq_enable(); 1334 error_code |= X86_PF_USER; 1335 flags |= FAULT_FLAG_USER; 1336 } else { 1337 if (regs->flags & X86_EFLAGS_IF) 1338 local_irq_enable(); 1339 } 1340 1341 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 1342 1343 if (error_code & X86_PF_WRITE) 1344 flags |= FAULT_FLAG_WRITE; 1345 if (error_code & X86_PF_INSTR) 1346 flags |= FAULT_FLAG_INSTRUCTION; 1347 1348 /* 1349 * When running in the kernel we expect faults to occur only to 1350 * addresses in user space. All other faults represent errors in 1351 * the kernel and should generate an OOPS. Unfortunately, in the 1352 * case of an erroneous fault occurring in a code path which already 1353 * holds mmap_sem we will deadlock attempting to validate the fault 1354 * against the address space. Luckily the kernel only validly 1355 * references user space from well defined areas of code, which are 1356 * listed in the exceptions table. 1357 * 1358 * As the vast majority of faults will be valid we will only perform 1359 * the source reference check when there is a possibility of a 1360 * deadlock. Attempt to lock the address space, if we cannot we then 1361 * validate the source. If this is invalid we can skip the address 1362 * space check, thus avoiding the deadlock: 1363 */ 1364 if (unlikely(!down_read_trylock(&mm->mmap_sem))) { 1365 if (!(error_code & X86_PF_USER) && 1366 !search_exception_tables(regs->ip)) { 1367 bad_area_nosemaphore(regs, error_code, address, NULL); 1368 return; 1369 } 1370 retry: 1371 down_read(&mm->mmap_sem); 1372 } else { 1373 /* 1374 * The above down_read_trylock() might have succeeded in 1375 * which case we'll have missed the might_sleep() from 1376 * down_read(): 1377 */ 1378 might_sleep(); 1379 } 1380 1381 vma = find_vma(mm, address); 1382 if (unlikely(!vma)) { 1383 bad_area(regs, error_code, address); 1384 return; 1385 } 1386 if (likely(vma->vm_start <= address)) 1387 goto good_area; 1388 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) { 1389 bad_area(regs, error_code, address); 1390 return; 1391 } 1392 if (error_code & X86_PF_USER) { 1393 /* 1394 * Accessing the stack below %sp is always a bug. 1395 * The large cushion allows instructions like enter 1396 * and pusha to work. ("enter $65535, $31" pushes 1397 * 32 pointers and then decrements %sp by 65535.) 1398 */ 1399 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) { 1400 bad_area(regs, error_code, address); 1401 return; 1402 } 1403 } 1404 if (unlikely(expand_stack(vma, address))) { 1405 bad_area(regs, error_code, address); 1406 return; 1407 } 1408 1409 /* 1410 * Ok, we have a good vm_area for this memory access, so 1411 * we can handle it.. 1412 */ 1413 good_area: 1414 if (unlikely(access_error(error_code, vma))) { 1415 bad_area_access_error(regs, error_code, address, vma); 1416 return; 1417 } 1418 1419 /* 1420 * If for any reason at all we couldn't handle the fault, 1421 * make sure we exit gracefully rather than endlessly redo 1422 * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if 1423 * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked. 1424 * 1425 * Note that handle_userfault() may also release and reacquire mmap_sem 1426 * (and not return with VM_FAULT_RETRY), when returning to userland to 1427 * repeat the page fault later with a VM_FAULT_NOPAGE retval 1428 * (potentially after handling any pending signal during the return to 1429 * userland). The return to userland is identified whenever 1430 * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags. 1431 * Thus we have to be careful about not touching vma after handling the 1432 * fault, so we read the pkey beforehand. 1433 */ 1434 pkey = vma_pkey(vma); 1435 fault = handle_mm_fault(vma, address, flags); 1436 major |= fault & VM_FAULT_MAJOR; 1437 1438 /* 1439 * If we need to retry the mmap_sem has already been released, 1440 * and if there is a fatal signal pending there is no guarantee 1441 * that we made any progress. Handle this case first. 1442 */ 1443 if (unlikely(fault & VM_FAULT_RETRY)) { 1444 /* Retry at most once */ 1445 if (flags & FAULT_FLAG_ALLOW_RETRY) { 1446 flags &= ~FAULT_FLAG_ALLOW_RETRY; 1447 flags |= FAULT_FLAG_TRIED; 1448 if (!fatal_signal_pending(tsk)) 1449 goto retry; 1450 } 1451 1452 /* User mode? Just return to handle the fatal exception */ 1453 if (flags & FAULT_FLAG_USER) 1454 return; 1455 1456 /* Not returning to user mode? Handle exceptions or die: */ 1457 no_context(regs, error_code, address, SIGBUS, BUS_ADRERR); 1458 return; 1459 } 1460 1461 up_read(&mm->mmap_sem); 1462 if (unlikely(fault & VM_FAULT_ERROR)) { 1463 mm_fault_error(regs, error_code, address, &pkey, fault); 1464 return; 1465 } 1466 1467 /* 1468 * Major/minor page fault accounting. If any of the events 1469 * returned VM_FAULT_MAJOR, we account it as a major fault. 1470 */ 1471 if (major) { 1472 tsk->maj_flt++; 1473 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address); 1474 } else { 1475 tsk->min_flt++; 1476 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address); 1477 } 1478 1479 check_v8086_mode(regs, address, tsk); 1480 } 1481 NOKPROBE_SYMBOL(__do_page_fault); 1482 1483 static nokprobe_inline void 1484 trace_page_fault_entries(unsigned long address, struct pt_regs *regs, 1485 unsigned long error_code) 1486 { 1487 if (user_mode(regs)) 1488 trace_page_fault_user(address, regs, error_code); 1489 else 1490 trace_page_fault_kernel(address, regs, error_code); 1491 } 1492 1493 /* 1494 * We must have this function blacklisted from kprobes, tagged with notrace 1495 * and call read_cr2() before calling anything else. To avoid calling any 1496 * kind of tracing machinery before we've observed the CR2 value. 1497 * 1498 * exception_{enter,exit}() contains all sorts of tracepoints. 1499 */ 1500 dotraplinkage void notrace 1501 do_page_fault(struct pt_regs *regs, unsigned long error_code) 1502 { 1503 unsigned long address = read_cr2(); /* Get the faulting address */ 1504 enum ctx_state prev_state; 1505 1506 prev_state = exception_enter(); 1507 if (trace_pagefault_enabled()) 1508 trace_page_fault_entries(address, regs, error_code); 1509 1510 __do_page_fault(regs, error_code, address); 1511 exception_exit(prev_state); 1512 } 1513 NOKPROBE_SYMBOL(do_page_fault); 1514