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