1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2c61e211dSHarvey Harrison /* 3c61e211dSHarvey Harrison * Copyright (C) 1995 Linus Torvalds 4c61e211dSHarvey Harrison * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs. 5f8eeb2e6SIngo Molnar * Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar 6c61e211dSHarvey Harrison */ 7a2bcd473SIngo Molnar #include <linux/sched.h> /* test_thread_flag(), ... */ 868db0cf1SIngo Molnar #include <linux/sched/task_stack.h> /* task_stack_*(), ... */ 9a2bcd473SIngo Molnar #include <linux/kdebug.h> /* oops_begin/end, ... */ 104cdf8dbeSLinus Torvalds #include <linux/extable.h> /* search_exception_tables */ 1157c8a661SMike Rapoport #include <linux/memblock.h> /* max_low_pfn */ 129326638cSMasami Hiramatsu #include <linux/kprobes.h> /* NOKPROBE_SYMBOL, ... */ 13a2bcd473SIngo Molnar #include <linux/mmiotrace.h> /* kmmio_handler, ... */ 14cdd6c482SIngo Molnar #include <linux/perf_event.h> /* perf_sw_event */ 15f672b49bSAndi Kleen #include <linux/hugetlb.h> /* hstate_index_to_shift */ 16268bb0ceSLinus Torvalds #include <linux/prefetch.h> /* prefetchw */ 1756dd9470SFrederic Weisbecker #include <linux/context_tracking.h> /* exception_enter(), ... */ 1870ffdb93SDavid Hildenbrand #include <linux/uaccess.h> /* faulthandler_disabled() */ 193425d934SSai Praneeth #include <linux/efi.h> /* efi_recover_from_page_fault()*/ 2050a7ca3cSSouptick Joarder #include <linux/mm_types.h> 21c61e211dSHarvey Harrison 22019132ffSDave Hansen #include <asm/cpufeature.h> /* boot_cpu_has, ... */ 23a2bcd473SIngo Molnar #include <asm/traps.h> /* dotraplinkage, ... */ 24a2bcd473SIngo Molnar #include <asm/pgalloc.h> /* pgd_*(), ... */ 25f40c3300SAndy Lutomirski #include <asm/fixmap.h> /* VSYSCALL_ADDR */ 26f40c3300SAndy Lutomirski #include <asm/vsyscall.h> /* emulate_vsyscall */ 27ba3e127eSBrian Gerst #include <asm/vm86.h> /* struct vm86 */ 28019132ffSDave Hansen #include <asm/mmu_context.h> /* vma_pkey() */ 293425d934SSai Praneeth #include <asm/efi.h> /* efi_recover_from_page_fault()*/ 30a1a371c4SAndy Lutomirski #include <asm/desc.h> /* store_idt(), ... */ 31d876b673SThomas Gleixner #include <asm/cpu_entry_area.h> /* exception stack */ 32c61e211dSHarvey Harrison 33d34603b0SSeiji Aguchi #define CREATE_TRACE_POINTS 34d34603b0SSeiji Aguchi #include <asm/trace/exceptions.h> 35d34603b0SSeiji Aguchi 36c61e211dSHarvey Harrison /* 37b319eed0SIngo Molnar * Returns 0 if mmiotrace is disabled, or if the fault is not 38b319eed0SIngo Molnar * handled by mmiotrace: 39b814d41fSIngo Molnar */ 409326638cSMasami Hiramatsu static nokprobe_inline int 4162c9295fSMasami Hiramatsu kmmio_fault(struct pt_regs *regs, unsigned long addr) 4286069782SPekka Paalanen { 430fd0e3daSPekka Paalanen if (unlikely(is_kmmio_active())) 440fd0e3daSPekka Paalanen if (kmmio_handler(regs, addr) == 1) 450fd0e3daSPekka Paalanen return -1; 460fd0e3daSPekka Paalanen return 0; 4786069782SPekka Paalanen } 4886069782SPekka Paalanen 49c61e211dSHarvey Harrison /* 502d4a7167SIngo Molnar * Prefetch quirks: 512d4a7167SIngo Molnar * 522d4a7167SIngo Molnar * 32-bit mode: 532d4a7167SIngo Molnar * 54c61e211dSHarvey Harrison * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch. 55c61e211dSHarvey Harrison * Check that here and ignore it. 56c61e211dSHarvey Harrison * 572d4a7167SIngo Molnar * 64-bit mode: 582d4a7167SIngo Molnar * 59c61e211dSHarvey Harrison * Sometimes the CPU reports invalid exceptions on prefetch. 60c61e211dSHarvey Harrison * Check that here and ignore it. 61c61e211dSHarvey Harrison * 622d4a7167SIngo Molnar * Opcode checker based on code by Richard Brunner. 63c61e211dSHarvey Harrison */ 64107a0367SIngo Molnar static inline int 65107a0367SIngo Molnar check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr, 66107a0367SIngo Molnar unsigned char opcode, int *prefetch) 67c61e211dSHarvey Harrison { 68107a0367SIngo Molnar unsigned char instr_hi = opcode & 0xf0; 69107a0367SIngo Molnar unsigned char instr_lo = opcode & 0x0f; 70c61e211dSHarvey Harrison 71c61e211dSHarvey Harrison switch (instr_hi) { 72c61e211dSHarvey Harrison case 0x20: 73c61e211dSHarvey Harrison case 0x30: 74c61e211dSHarvey Harrison /* 75c61e211dSHarvey Harrison * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes. 76c61e211dSHarvey Harrison * In X86_64 long mode, the CPU will signal invalid 77c61e211dSHarvey Harrison * opcode if some of these prefixes are present so 78c61e211dSHarvey Harrison * X86_64 will never get here anyway 79c61e211dSHarvey Harrison */ 80107a0367SIngo Molnar return ((instr_lo & 7) == 0x6); 81c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 82c61e211dSHarvey Harrison case 0x40: 83c61e211dSHarvey Harrison /* 84c61e211dSHarvey Harrison * In AMD64 long mode 0x40..0x4F are valid REX prefixes 85c61e211dSHarvey Harrison * Need to figure out under what instruction mode the 86c61e211dSHarvey Harrison * instruction was issued. Could check the LDT for lm, 87c61e211dSHarvey Harrison * but for now it's good enough to assume that long 88c61e211dSHarvey Harrison * mode only uses well known segments or kernel. 89c61e211dSHarvey Harrison */ 90318f5a2aSAndy Lutomirski return (!user_mode(regs) || user_64bit_mode(regs)); 91c61e211dSHarvey Harrison #endif 92c61e211dSHarvey Harrison case 0x60: 93c61e211dSHarvey Harrison /* 0x64 thru 0x67 are valid prefixes in all modes. */ 94107a0367SIngo Molnar return (instr_lo & 0xC) == 0x4; 95c61e211dSHarvey Harrison case 0xF0: 96c61e211dSHarvey Harrison /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */ 97107a0367SIngo Molnar return !instr_lo || (instr_lo>>1) == 1; 98c61e211dSHarvey Harrison case 0x00: 99c61e211dSHarvey Harrison /* Prefetch instruction is 0x0F0D or 0x0F18 */ 100107a0367SIngo Molnar if (probe_kernel_address(instr, opcode)) 101107a0367SIngo Molnar return 0; 102107a0367SIngo Molnar 103107a0367SIngo Molnar *prefetch = (instr_lo == 0xF) && 104107a0367SIngo Molnar (opcode == 0x0D || opcode == 0x18); 105107a0367SIngo Molnar return 0; 106107a0367SIngo Molnar default: 107107a0367SIngo Molnar return 0; 108107a0367SIngo Molnar } 109107a0367SIngo Molnar } 110107a0367SIngo Molnar 111107a0367SIngo Molnar static int 112107a0367SIngo Molnar is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr) 113107a0367SIngo Molnar { 114107a0367SIngo Molnar unsigned char *max_instr; 115107a0367SIngo Molnar unsigned char *instr; 116107a0367SIngo Molnar int prefetch = 0; 117107a0367SIngo Molnar 118107a0367SIngo Molnar /* 119107a0367SIngo Molnar * If it was a exec (instruction fetch) fault on NX page, then 120107a0367SIngo Molnar * do not ignore the fault: 121107a0367SIngo Molnar */ 1221067f030SRicardo Neri if (error_code & X86_PF_INSTR) 123107a0367SIngo Molnar return 0; 124107a0367SIngo Molnar 125107a0367SIngo Molnar instr = (void *)convert_ip_to_linear(current, regs); 126107a0367SIngo Molnar max_instr = instr + 15; 127107a0367SIngo Molnar 128d31bf07fSAndy Lutomirski if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE_MAX) 129107a0367SIngo Molnar return 0; 130107a0367SIngo Molnar 131107a0367SIngo Molnar while (instr < max_instr) { 132107a0367SIngo Molnar unsigned char opcode; 133c61e211dSHarvey Harrison 134c61e211dSHarvey Harrison if (probe_kernel_address(instr, opcode)) 135c61e211dSHarvey Harrison break; 136107a0367SIngo Molnar 137107a0367SIngo Molnar instr++; 138107a0367SIngo Molnar 139107a0367SIngo Molnar if (!check_prefetch_opcode(regs, instr, opcode, &prefetch)) 140c61e211dSHarvey Harrison break; 141c61e211dSHarvey Harrison } 142c61e211dSHarvey Harrison return prefetch; 143c61e211dSHarvey Harrison } 144c61e211dSHarvey Harrison 145f2f13a85SIngo Molnar DEFINE_SPINLOCK(pgd_lock); 146f2f13a85SIngo Molnar LIST_HEAD(pgd_list); 1472d4a7167SIngo Molnar 148f2f13a85SIngo Molnar #ifdef CONFIG_X86_32 149f2f13a85SIngo Molnar static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address) 150f2f13a85SIngo Molnar { 151f2f13a85SIngo Molnar unsigned index = pgd_index(address); 152f2f13a85SIngo Molnar pgd_t *pgd_k; 153e0c4f675SKirill A. Shutemov p4d_t *p4d, *p4d_k; 154f2f13a85SIngo Molnar pud_t *pud, *pud_k; 155f2f13a85SIngo Molnar pmd_t *pmd, *pmd_k; 156f2f13a85SIngo Molnar 157f2f13a85SIngo Molnar pgd += index; 158f2f13a85SIngo Molnar pgd_k = init_mm.pgd + index; 159f2f13a85SIngo Molnar 160f2f13a85SIngo Molnar if (!pgd_present(*pgd_k)) 161f2f13a85SIngo Molnar return NULL; 162f2f13a85SIngo Molnar 163f2f13a85SIngo Molnar /* 164f2f13a85SIngo Molnar * set_pgd(pgd, *pgd_k); here would be useless on PAE 165f2f13a85SIngo Molnar * and redundant with the set_pmd() on non-PAE. As would 166e0c4f675SKirill A. Shutemov * set_p4d/set_pud. 167f2f13a85SIngo Molnar */ 168e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, address); 169e0c4f675SKirill A. Shutemov p4d_k = p4d_offset(pgd_k, address); 170e0c4f675SKirill A. Shutemov if (!p4d_present(*p4d_k)) 171e0c4f675SKirill A. Shutemov return NULL; 172e0c4f675SKirill A. Shutemov 173e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, address); 174e0c4f675SKirill A. Shutemov pud_k = pud_offset(p4d_k, address); 175f2f13a85SIngo Molnar if (!pud_present(*pud_k)) 176f2f13a85SIngo Molnar return NULL; 177f2f13a85SIngo Molnar 178f2f13a85SIngo Molnar pmd = pmd_offset(pud, address); 179f2f13a85SIngo Molnar pmd_k = pmd_offset(pud_k, address); 180f2f13a85SIngo Molnar if (!pmd_present(*pmd_k)) 181f2f13a85SIngo Molnar return NULL; 182f2f13a85SIngo Molnar 183b8bcfe99SJeremy Fitzhardinge if (!pmd_present(*pmd)) 184f2f13a85SIngo Molnar set_pmd(pmd, *pmd_k); 185b8bcfe99SJeremy Fitzhardinge else 186*51b75b5bSJoerg Roedel BUG_ON(pmd_pfn(*pmd) != pmd_pfn(*pmd_k)); 187f2f13a85SIngo Molnar 188f2f13a85SIngo Molnar return pmd_k; 189f2f13a85SIngo Molnar } 190f2f13a85SIngo Molnar 191f2f13a85SIngo Molnar void vmalloc_sync_all(void) 192f2f13a85SIngo Molnar { 193f2f13a85SIngo Molnar unsigned long address; 194f2f13a85SIngo Molnar 195f2f13a85SIngo Molnar if (SHARED_KERNEL_PMD) 196f2f13a85SIngo Molnar return; 197f2f13a85SIngo Molnar 198f2f13a85SIngo Molnar for (address = VMALLOC_START & PMD_MASK; 199dc4fac84SAndy Lutomirski address >= TASK_SIZE_MAX && address < FIXADDR_TOP; 200f2f13a85SIngo Molnar address += PMD_SIZE) { 201f2f13a85SIngo Molnar struct page *page; 202f2f13a85SIngo Molnar 203a79e53d8SAndrea Arcangeli spin_lock(&pgd_lock); 204f2f13a85SIngo Molnar list_for_each_entry(page, &pgd_list, lru) { 205617d34d9SJeremy Fitzhardinge spinlock_t *pgt_lock; 206f01f7c56SBorislav Petkov pmd_t *ret; 207617d34d9SJeremy Fitzhardinge 208a79e53d8SAndrea Arcangeli /* the pgt_lock only for Xen */ 209617d34d9SJeremy Fitzhardinge pgt_lock = &pgd_page_get_mm(page)->page_table_lock; 210617d34d9SJeremy Fitzhardinge 211617d34d9SJeremy Fitzhardinge spin_lock(pgt_lock); 212617d34d9SJeremy Fitzhardinge ret = vmalloc_sync_one(page_address(page), address); 213617d34d9SJeremy Fitzhardinge spin_unlock(pgt_lock); 214617d34d9SJeremy Fitzhardinge 215617d34d9SJeremy Fitzhardinge if (!ret) 216f2f13a85SIngo Molnar break; 217f2f13a85SIngo Molnar } 218a79e53d8SAndrea Arcangeli spin_unlock(&pgd_lock); 219f2f13a85SIngo Molnar } 220f2f13a85SIngo Molnar } 221f2f13a85SIngo Molnar 222f2f13a85SIngo Molnar /* 223f2f13a85SIngo Molnar * 32-bit: 224f2f13a85SIngo Molnar * 225f2f13a85SIngo Molnar * Handle a fault on the vmalloc or module mapping area 226f2f13a85SIngo Molnar */ 2279326638cSMasami Hiramatsu static noinline int vmalloc_fault(unsigned long address) 228f2f13a85SIngo Molnar { 229f2f13a85SIngo Molnar unsigned long pgd_paddr; 230f2f13a85SIngo Molnar pmd_t *pmd_k; 231f2f13a85SIngo Molnar pte_t *pte_k; 232f2f13a85SIngo Molnar 233f2f13a85SIngo Molnar /* Make sure we are in vmalloc area: */ 234f2f13a85SIngo Molnar if (!(address >= VMALLOC_START && address < VMALLOC_END)) 235f2f13a85SIngo Molnar return -1; 236f2f13a85SIngo Molnar 237f2f13a85SIngo Molnar /* 238f2f13a85SIngo Molnar * Synchronize this task's top level page-table 239f2f13a85SIngo Molnar * with the 'reference' page table. 240f2f13a85SIngo Molnar * 241f2f13a85SIngo Molnar * Do _not_ use "current" here. We might be inside 242f2f13a85SIngo Molnar * an interrupt in the middle of a task switch.. 243f2f13a85SIngo Molnar */ 2446c690ee1SAndy Lutomirski pgd_paddr = read_cr3_pa(); 245f2f13a85SIngo Molnar pmd_k = vmalloc_sync_one(__va(pgd_paddr), address); 246f2f13a85SIngo Molnar if (!pmd_k) 247f2f13a85SIngo Molnar return -1; 248f2f13a85SIngo Molnar 24918a95521SToshi Kani if (pmd_large(*pmd_k)) 250f4eafd8bSToshi Kani return 0; 251f4eafd8bSToshi Kani 252f2f13a85SIngo Molnar pte_k = pte_offset_kernel(pmd_k, address); 253f2f13a85SIngo Molnar if (!pte_present(*pte_k)) 254f2f13a85SIngo Molnar return -1; 255f2f13a85SIngo Molnar 256f2f13a85SIngo Molnar return 0; 257f2f13a85SIngo Molnar } 2589326638cSMasami Hiramatsu NOKPROBE_SYMBOL(vmalloc_fault); 259f2f13a85SIngo Molnar 260f2f13a85SIngo Molnar /* 261f2f13a85SIngo Molnar * Did it hit the DOS screen memory VA from vm86 mode? 262f2f13a85SIngo Molnar */ 263f2f13a85SIngo Molnar static inline void 264f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address, 265f2f13a85SIngo Molnar struct task_struct *tsk) 266f2f13a85SIngo Molnar { 2679fda6a06SBrian Gerst #ifdef CONFIG_VM86 268f2f13a85SIngo Molnar unsigned long bit; 269f2f13a85SIngo Molnar 2709fda6a06SBrian Gerst if (!v8086_mode(regs) || !tsk->thread.vm86) 271f2f13a85SIngo Molnar return; 272f2f13a85SIngo Molnar 273f2f13a85SIngo Molnar bit = (address - 0xA0000) >> PAGE_SHIFT; 274f2f13a85SIngo Molnar if (bit < 32) 2759fda6a06SBrian Gerst tsk->thread.vm86->screen_bitmap |= 1 << bit; 2769fda6a06SBrian Gerst #endif 277f2f13a85SIngo Molnar } 278c61e211dSHarvey Harrison 279087975b0SAkinobu Mita static bool low_pfn(unsigned long pfn) 280087975b0SAkinobu Mita { 281087975b0SAkinobu Mita return pfn < max_low_pfn; 282087975b0SAkinobu Mita } 283087975b0SAkinobu Mita 284cae30f82SAdrian Bunk static void dump_pagetable(unsigned long address) 285c61e211dSHarvey Harrison { 2866c690ee1SAndy Lutomirski pgd_t *base = __va(read_cr3_pa()); 287087975b0SAkinobu Mita pgd_t *pgd = &base[pgd_index(address)]; 288e0c4f675SKirill A. Shutemov p4d_t *p4d; 289e0c4f675SKirill A. Shutemov pud_t *pud; 290087975b0SAkinobu Mita pmd_t *pmd; 291087975b0SAkinobu Mita pte_t *pte; 2922d4a7167SIngo Molnar 293c61e211dSHarvey Harrison #ifdef CONFIG_X86_PAE 29439e48d9bSJan Beulich pr_info("*pdpt = %016Lx ", pgd_val(*pgd)); 295087975b0SAkinobu Mita if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd)) 296087975b0SAkinobu Mita goto out; 29739e48d9bSJan Beulich #define pr_pde pr_cont 29839e48d9bSJan Beulich #else 29939e48d9bSJan Beulich #define pr_pde pr_info 300c61e211dSHarvey Harrison #endif 301e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, address); 302e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, address); 303e0c4f675SKirill A. Shutemov pmd = pmd_offset(pud, address); 30439e48d9bSJan Beulich pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd)); 30539e48d9bSJan Beulich #undef pr_pde 306c61e211dSHarvey Harrison 307c61e211dSHarvey Harrison /* 308c61e211dSHarvey Harrison * We must not directly access the pte in the highpte 309c61e211dSHarvey Harrison * case if the page table is located in highmem. 310c61e211dSHarvey Harrison * And let's rather not kmap-atomic the pte, just in case 3112d4a7167SIngo Molnar * it's allocated already: 312c61e211dSHarvey Harrison */ 313087975b0SAkinobu Mita if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd)) 314087975b0SAkinobu Mita goto out; 3152d4a7167SIngo Molnar 316087975b0SAkinobu Mita pte = pte_offset_kernel(pmd, address); 31739e48d9bSJan Beulich pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte)); 318087975b0SAkinobu Mita out: 31939e48d9bSJan Beulich pr_cont("\n"); 320f2f13a85SIngo Molnar } 321f2f13a85SIngo Molnar 322f2f13a85SIngo Molnar #else /* CONFIG_X86_64: */ 323f2f13a85SIngo Molnar 324f2f13a85SIngo Molnar void vmalloc_sync_all(void) 325f2f13a85SIngo Molnar { 3265372e155SKirill A. Shutemov sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END); 327f2f13a85SIngo Molnar } 328f2f13a85SIngo Molnar 329f2f13a85SIngo Molnar /* 330f2f13a85SIngo Molnar * 64-bit: 331f2f13a85SIngo Molnar * 332f2f13a85SIngo Molnar * Handle a fault on the vmalloc area 333f2f13a85SIngo Molnar */ 3349326638cSMasami Hiramatsu static noinline int vmalloc_fault(unsigned long address) 335f2f13a85SIngo Molnar { 336565977a3SToshi Kani pgd_t *pgd, *pgd_k; 337565977a3SToshi Kani p4d_t *p4d, *p4d_k; 338565977a3SToshi Kani pud_t *pud; 339565977a3SToshi Kani pmd_t *pmd; 340565977a3SToshi Kani pte_t *pte; 341f2f13a85SIngo Molnar 342f2f13a85SIngo Molnar /* Make sure we are in vmalloc area: */ 343f2f13a85SIngo Molnar if (!(address >= VMALLOC_START && address < VMALLOC_END)) 344f2f13a85SIngo Molnar return -1; 345f2f13a85SIngo Molnar 346f2f13a85SIngo Molnar /* 347f2f13a85SIngo Molnar * Copy kernel mappings over when needed. This can also 348f2f13a85SIngo Molnar * happen within a race in page table update. In the later 349f2f13a85SIngo Molnar * case just flush: 350f2f13a85SIngo Molnar */ 3516c690ee1SAndy Lutomirski pgd = (pgd_t *)__va(read_cr3_pa()) + pgd_index(address); 352565977a3SToshi Kani pgd_k = pgd_offset_k(address); 353565977a3SToshi Kani if (pgd_none(*pgd_k)) 354f2f13a85SIngo Molnar return -1; 355f2f13a85SIngo Molnar 356ed7588d5SKirill A. Shutemov if (pgtable_l5_enabled()) { 3571160c277SSamu Kallio if (pgd_none(*pgd)) { 358565977a3SToshi Kani set_pgd(pgd, *pgd_k); 3591160c277SSamu Kallio arch_flush_lazy_mmu_mode(); 36036b3a772SAndy Lutomirski } else { 361565977a3SToshi Kani BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_k)); 3621160c277SSamu Kallio } 36336b3a772SAndy Lutomirski } 364f2f13a85SIngo Molnar 365b50858ceSKirill A. Shutemov /* With 4-level paging, copying happens on the p4d level. */ 366b50858ceSKirill A. Shutemov p4d = p4d_offset(pgd, address); 367565977a3SToshi Kani p4d_k = p4d_offset(pgd_k, address); 368565977a3SToshi Kani if (p4d_none(*p4d_k)) 369b50858ceSKirill A. Shutemov return -1; 370b50858ceSKirill A. Shutemov 371ed7588d5SKirill A. Shutemov if (p4d_none(*p4d) && !pgtable_l5_enabled()) { 372565977a3SToshi Kani set_p4d(p4d, *p4d_k); 373b50858ceSKirill A. Shutemov arch_flush_lazy_mmu_mode(); 374b50858ceSKirill A. Shutemov } else { 375565977a3SToshi Kani BUG_ON(p4d_pfn(*p4d) != p4d_pfn(*p4d_k)); 376b50858ceSKirill A. Shutemov } 377b50858ceSKirill A. Shutemov 37836b3a772SAndy Lutomirski BUILD_BUG_ON(CONFIG_PGTABLE_LEVELS < 4); 379f2f13a85SIngo Molnar 380b50858ceSKirill A. Shutemov pud = pud_offset(p4d, address); 381565977a3SToshi Kani if (pud_none(*pud)) 382f2f13a85SIngo Molnar return -1; 383f2f13a85SIngo Molnar 38418a95521SToshi Kani if (pud_large(*pud)) 385f4eafd8bSToshi Kani return 0; 386f4eafd8bSToshi Kani 387f2f13a85SIngo Molnar pmd = pmd_offset(pud, address); 388565977a3SToshi Kani if (pmd_none(*pmd)) 389f2f13a85SIngo Molnar return -1; 390f2f13a85SIngo Molnar 39118a95521SToshi Kani if (pmd_large(*pmd)) 392f4eafd8bSToshi Kani return 0; 393f4eafd8bSToshi Kani 394f2f13a85SIngo Molnar pte = pte_offset_kernel(pmd, address); 395565977a3SToshi Kani if (!pte_present(*pte)) 396565977a3SToshi Kani return -1; 397f2f13a85SIngo Molnar 398f2f13a85SIngo Molnar return 0; 399f2f13a85SIngo Molnar } 4009326638cSMasami Hiramatsu NOKPROBE_SYMBOL(vmalloc_fault); 401f2f13a85SIngo Molnar 402e05139f2SJan Beulich #ifdef CONFIG_CPU_SUP_AMD 403f2f13a85SIngo Molnar static const char errata93_warning[] = 404ad361c98SJoe Perches KERN_ERR 405ad361c98SJoe Perches "******* Your BIOS seems to not contain a fix for K8 errata #93\n" 406ad361c98SJoe Perches "******* Working around it, but it may cause SEGVs or burn power.\n" 407ad361c98SJoe Perches "******* Please consider a BIOS update.\n" 408ad361c98SJoe Perches "******* Disabling USB legacy in the BIOS may also help.\n"; 409e05139f2SJan Beulich #endif 410f2f13a85SIngo Molnar 411f2f13a85SIngo Molnar /* 412f2f13a85SIngo Molnar * No vm86 mode in 64-bit mode: 413f2f13a85SIngo Molnar */ 414f2f13a85SIngo Molnar static inline void 415f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address, 416f2f13a85SIngo Molnar struct task_struct *tsk) 417f2f13a85SIngo Molnar { 418f2f13a85SIngo Molnar } 419f2f13a85SIngo Molnar 420f2f13a85SIngo Molnar static int bad_address(void *p) 421f2f13a85SIngo Molnar { 422f2f13a85SIngo Molnar unsigned long dummy; 423f2f13a85SIngo Molnar 424f2f13a85SIngo Molnar return probe_kernel_address((unsigned long *)p, dummy); 425f2f13a85SIngo Molnar } 426f2f13a85SIngo Molnar 427f2f13a85SIngo Molnar static void dump_pagetable(unsigned long address) 428f2f13a85SIngo Molnar { 4296c690ee1SAndy Lutomirski pgd_t *base = __va(read_cr3_pa()); 430087975b0SAkinobu Mita pgd_t *pgd = base + pgd_index(address); 431e0c4f675SKirill A. Shutemov p4d_t *p4d; 432c61e211dSHarvey Harrison pud_t *pud; 433c61e211dSHarvey Harrison pmd_t *pmd; 434c61e211dSHarvey Harrison pte_t *pte; 435c61e211dSHarvey Harrison 4362d4a7167SIngo Molnar if (bad_address(pgd)) 4372d4a7167SIngo Molnar goto bad; 4382d4a7167SIngo Molnar 43939e48d9bSJan Beulich pr_info("PGD %lx ", pgd_val(*pgd)); 4402d4a7167SIngo Molnar 4412d4a7167SIngo Molnar if (!pgd_present(*pgd)) 4422d4a7167SIngo Molnar goto out; 443c61e211dSHarvey Harrison 444e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, address); 445e0c4f675SKirill A. Shutemov if (bad_address(p4d)) 446e0c4f675SKirill A. Shutemov goto bad; 447e0c4f675SKirill A. Shutemov 44839e48d9bSJan Beulich pr_cont("P4D %lx ", p4d_val(*p4d)); 449e0c4f675SKirill A. Shutemov if (!p4d_present(*p4d) || p4d_large(*p4d)) 450e0c4f675SKirill A. Shutemov goto out; 451e0c4f675SKirill A. Shutemov 452e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, address); 4532d4a7167SIngo Molnar if (bad_address(pud)) 4542d4a7167SIngo Molnar goto bad; 4552d4a7167SIngo Molnar 45639e48d9bSJan Beulich pr_cont("PUD %lx ", pud_val(*pud)); 457b5360222SAndi Kleen if (!pud_present(*pud) || pud_large(*pud)) 4582d4a7167SIngo Molnar goto out; 459c61e211dSHarvey Harrison 460c61e211dSHarvey Harrison pmd = pmd_offset(pud, address); 4612d4a7167SIngo Molnar if (bad_address(pmd)) 4622d4a7167SIngo Molnar goto bad; 4632d4a7167SIngo Molnar 46439e48d9bSJan Beulich pr_cont("PMD %lx ", pmd_val(*pmd)); 4652d4a7167SIngo Molnar if (!pmd_present(*pmd) || pmd_large(*pmd)) 4662d4a7167SIngo Molnar goto out; 467c61e211dSHarvey Harrison 468c61e211dSHarvey Harrison pte = pte_offset_kernel(pmd, address); 4692d4a7167SIngo Molnar if (bad_address(pte)) 4702d4a7167SIngo Molnar goto bad; 4712d4a7167SIngo Molnar 47239e48d9bSJan Beulich pr_cont("PTE %lx", pte_val(*pte)); 4732d4a7167SIngo Molnar out: 47439e48d9bSJan Beulich pr_cont("\n"); 475c61e211dSHarvey Harrison return; 476c61e211dSHarvey Harrison bad: 47739e48d9bSJan Beulich pr_info("BAD\n"); 478c61e211dSHarvey Harrison } 479c61e211dSHarvey Harrison 480f2f13a85SIngo Molnar #endif /* CONFIG_X86_64 */ 481c61e211dSHarvey Harrison 4822d4a7167SIngo Molnar /* 4832d4a7167SIngo Molnar * Workaround for K8 erratum #93 & buggy BIOS. 4842d4a7167SIngo Molnar * 4852d4a7167SIngo Molnar * BIOS SMM functions are required to use a specific workaround 4862d4a7167SIngo Molnar * to avoid corruption of the 64bit RIP register on C stepping K8. 4872d4a7167SIngo Molnar * 4882d4a7167SIngo Molnar * A lot of BIOS that didn't get tested properly miss this. 4892d4a7167SIngo Molnar * 4902d4a7167SIngo Molnar * The OS sees this as a page fault with the upper 32bits of RIP cleared. 4912d4a7167SIngo Molnar * Try to work around it here. 4922d4a7167SIngo Molnar * 4932d4a7167SIngo Molnar * Note we only handle faults in kernel here. 4942d4a7167SIngo Molnar * Does nothing on 32-bit. 495c61e211dSHarvey Harrison */ 496c61e211dSHarvey Harrison static int is_errata93(struct pt_regs *regs, unsigned long address) 497c61e211dSHarvey Harrison { 498e05139f2SJan Beulich #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD) 499e05139f2SJan Beulich if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD 500e05139f2SJan Beulich || boot_cpu_data.x86 != 0xf) 501e05139f2SJan Beulich return 0; 502e05139f2SJan Beulich 503c61e211dSHarvey Harrison if (address != regs->ip) 504c61e211dSHarvey Harrison return 0; 5052d4a7167SIngo Molnar 506c61e211dSHarvey Harrison if ((address >> 32) != 0) 507c61e211dSHarvey Harrison return 0; 5082d4a7167SIngo Molnar 509c61e211dSHarvey Harrison address |= 0xffffffffUL << 32; 510c61e211dSHarvey Harrison if ((address >= (u64)_stext && address <= (u64)_etext) || 511c61e211dSHarvey Harrison (address >= MODULES_VADDR && address <= MODULES_END)) { 512a454ab31SIngo Molnar printk_once(errata93_warning); 513c61e211dSHarvey Harrison regs->ip = address; 514c61e211dSHarvey Harrison return 1; 515c61e211dSHarvey Harrison } 516c61e211dSHarvey Harrison #endif 517c61e211dSHarvey Harrison return 0; 518c61e211dSHarvey Harrison } 519c61e211dSHarvey Harrison 520c61e211dSHarvey Harrison /* 5212d4a7167SIngo Molnar * Work around K8 erratum #100 K8 in compat mode occasionally jumps 5222d4a7167SIngo Molnar * to illegal addresses >4GB. 5232d4a7167SIngo Molnar * 5242d4a7167SIngo Molnar * We catch this in the page fault handler because these addresses 5252d4a7167SIngo Molnar * are not reachable. Just detect this case and return. Any code 526c61e211dSHarvey Harrison * segment in LDT is compatibility mode. 527c61e211dSHarvey Harrison */ 528c61e211dSHarvey Harrison static int is_errata100(struct pt_regs *regs, unsigned long address) 529c61e211dSHarvey Harrison { 530c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 5312d4a7167SIngo Molnar if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32)) 532c61e211dSHarvey Harrison return 1; 533c61e211dSHarvey Harrison #endif 534c61e211dSHarvey Harrison return 0; 535c61e211dSHarvey Harrison } 536c61e211dSHarvey Harrison 537c61e211dSHarvey Harrison static int is_f00f_bug(struct pt_regs *regs, unsigned long address) 538c61e211dSHarvey Harrison { 539c61e211dSHarvey Harrison #ifdef CONFIG_X86_F00F_BUG 540c61e211dSHarvey Harrison unsigned long nr; 5412d4a7167SIngo Molnar 542c61e211dSHarvey Harrison /* 5432d4a7167SIngo Molnar * Pentium F0 0F C7 C8 bug workaround: 544c61e211dSHarvey Harrison */ 545e2604b49SBorislav Petkov if (boot_cpu_has_bug(X86_BUG_F00F)) { 546c61e211dSHarvey Harrison nr = (address - idt_descr.address) >> 3; 547c61e211dSHarvey Harrison 548c61e211dSHarvey Harrison if (nr == 6) { 549c61e211dSHarvey Harrison do_invalid_op(regs, 0); 550c61e211dSHarvey Harrison return 1; 551c61e211dSHarvey Harrison } 552c61e211dSHarvey Harrison } 553c61e211dSHarvey Harrison #endif 554c61e211dSHarvey Harrison return 0; 555c61e211dSHarvey Harrison } 556c61e211dSHarvey Harrison 557a1a371c4SAndy Lutomirski static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index) 558a1a371c4SAndy Lutomirski { 559a1a371c4SAndy Lutomirski u32 offset = (index >> 3) * sizeof(struct desc_struct); 560a1a371c4SAndy Lutomirski unsigned long addr; 561a1a371c4SAndy Lutomirski struct ldttss_desc desc; 562a1a371c4SAndy Lutomirski 563a1a371c4SAndy Lutomirski if (index == 0) { 564a1a371c4SAndy Lutomirski pr_alert("%s: NULL\n", name); 565a1a371c4SAndy Lutomirski return; 566a1a371c4SAndy Lutomirski } 567a1a371c4SAndy Lutomirski 568a1a371c4SAndy Lutomirski if (offset + sizeof(struct ldttss_desc) >= gdt->size) { 569a1a371c4SAndy Lutomirski pr_alert("%s: 0x%hx -- out of bounds\n", name, index); 570a1a371c4SAndy Lutomirski return; 571a1a371c4SAndy Lutomirski } 572a1a371c4SAndy Lutomirski 573a1a371c4SAndy Lutomirski if (probe_kernel_read(&desc, (void *)(gdt->address + offset), 574a1a371c4SAndy Lutomirski sizeof(struct ldttss_desc))) { 575a1a371c4SAndy Lutomirski pr_alert("%s: 0x%hx -- GDT entry is not readable\n", 576a1a371c4SAndy Lutomirski name, index); 577a1a371c4SAndy Lutomirski return; 578a1a371c4SAndy Lutomirski } 579a1a371c4SAndy Lutomirski 5805ccd3528SColin Ian King addr = desc.base0 | (desc.base1 << 16) | ((unsigned long)desc.base2 << 24); 581a1a371c4SAndy Lutomirski #ifdef CONFIG_X86_64 582a1a371c4SAndy Lutomirski addr |= ((u64)desc.base3 << 32); 583a1a371c4SAndy Lutomirski #endif 584a1a371c4SAndy Lutomirski pr_alert("%s: 0x%hx -- base=0x%lx limit=0x%x\n", 585a1a371c4SAndy Lutomirski name, index, addr, (desc.limit0 | (desc.limit1 << 16))); 586a1a371c4SAndy Lutomirski } 587a1a371c4SAndy Lutomirski 5882d4a7167SIngo Molnar static void 589a2aa52abSIngo Molnar show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long address) 590c61e211dSHarvey Harrison { 591c61e211dSHarvey Harrison if (!oops_may_print()) 592c61e211dSHarvey Harrison return; 593c61e211dSHarvey Harrison 5941067f030SRicardo Neri if (error_code & X86_PF_INSTR) { 59593809be8SHarvey Harrison unsigned int level; 596426e34ccSMatt Fleming pgd_t *pgd; 597426e34ccSMatt Fleming pte_t *pte; 5982d4a7167SIngo Molnar 5996c690ee1SAndy Lutomirski pgd = __va(read_cr3_pa()); 600426e34ccSMatt Fleming pgd += pgd_index(address); 601426e34ccSMatt Fleming 602426e34ccSMatt Fleming pte = lookup_address_in_pgd(pgd, address, &level); 603c61e211dSHarvey Harrison 6048f766149SIngo Molnar if (pte && pte_present(*pte) && !pte_exec(*pte)) 605d79d0d8aSDmitry Vyukov pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n", 606d79d0d8aSDmitry Vyukov from_kuid(&init_user_ns, current_uid())); 607eff50c34SJiri Kosina if (pte && pte_present(*pte) && pte_exec(*pte) && 608eff50c34SJiri Kosina (pgd_flags(*pgd) & _PAGE_USER) && 6091e02ce4cSAndy Lutomirski (__read_cr4() & X86_CR4_SMEP)) 610d79d0d8aSDmitry Vyukov pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n", 611d79d0d8aSDmitry Vyukov from_kuid(&init_user_ns, current_uid())); 612c61e211dSHarvey Harrison } 613fd40d6e3SHarvey Harrison 614f28b11a2SSean Christopherson if (address < PAGE_SIZE && !user_mode(regs)) 615ea2f8d60SBorislav Petkov pr_alert("BUG: kernel NULL pointer dereference, address: %px\n", 616f28b11a2SSean Christopherson (void *)address); 617f28b11a2SSean Christopherson else 618ea2f8d60SBorislav Petkov pr_alert("BUG: unable to handle page fault for address: %px\n", 6194188f063SDmitry Vyukov (void *)address); 6202d4a7167SIngo Molnar 621ea2f8d60SBorislav Petkov pr_alert("#PF: %s %s in %s mode\n", 62218ea35c5SSean Christopherson (error_code & X86_PF_USER) ? "user" : "supervisor", 62318ea35c5SSean Christopherson (error_code & X86_PF_INSTR) ? "instruction fetch" : 62418ea35c5SSean Christopherson (error_code & X86_PF_WRITE) ? "write access" : 62518ea35c5SSean Christopherson "read access", 62618ea35c5SSean Christopherson user_mode(regs) ? "user" : "kernel"); 62718ea35c5SSean Christopherson pr_alert("#PF: error_code(0x%04lx) - %s\n", error_code, 62818ea35c5SSean Christopherson !(error_code & X86_PF_PROT) ? "not-present page" : 62918ea35c5SSean Christopherson (error_code & X86_PF_RSVD) ? "reserved bit violation" : 63018ea35c5SSean Christopherson (error_code & X86_PF_PK) ? "protection keys violation" : 63118ea35c5SSean Christopherson "permissions violation"); 632a2aa52abSIngo Molnar 633a1a371c4SAndy Lutomirski if (!(error_code & X86_PF_USER) && user_mode(regs)) { 634a1a371c4SAndy Lutomirski struct desc_ptr idt, gdt; 635a1a371c4SAndy Lutomirski u16 ldtr, tr; 636a1a371c4SAndy Lutomirski 637a1a371c4SAndy Lutomirski /* 638a1a371c4SAndy Lutomirski * This can happen for quite a few reasons. The more obvious 639a1a371c4SAndy Lutomirski * ones are faults accessing the GDT, or LDT. Perhaps 640a1a371c4SAndy Lutomirski * surprisingly, if the CPU tries to deliver a benign or 641a1a371c4SAndy Lutomirski * contributory exception from user code and gets a page fault 642a1a371c4SAndy Lutomirski * during delivery, the page fault can be delivered as though 643a1a371c4SAndy Lutomirski * it originated directly from user code. This could happen 644a1a371c4SAndy Lutomirski * due to wrong permissions on the IDT, GDT, LDT, TSS, or 645a1a371c4SAndy Lutomirski * kernel or IST stack. 646a1a371c4SAndy Lutomirski */ 647a1a371c4SAndy Lutomirski store_idt(&idt); 648a1a371c4SAndy Lutomirski 649a1a371c4SAndy Lutomirski /* Usable even on Xen PV -- it's just slow. */ 650a1a371c4SAndy Lutomirski native_store_gdt(&gdt); 651a1a371c4SAndy Lutomirski 652a1a371c4SAndy Lutomirski pr_alert("IDT: 0x%lx (limit=0x%hx) GDT: 0x%lx (limit=0x%hx)\n", 653a1a371c4SAndy Lutomirski idt.address, idt.size, gdt.address, gdt.size); 654a1a371c4SAndy Lutomirski 655a1a371c4SAndy Lutomirski store_ldt(ldtr); 656a1a371c4SAndy Lutomirski show_ldttss(&gdt, "LDTR", ldtr); 657a1a371c4SAndy Lutomirski 658a1a371c4SAndy Lutomirski store_tr(tr); 659a1a371c4SAndy Lutomirski show_ldttss(&gdt, "TR", tr); 660a1a371c4SAndy Lutomirski } 661a1a371c4SAndy Lutomirski 662c61e211dSHarvey Harrison dump_pagetable(address); 663c61e211dSHarvey Harrison } 664c61e211dSHarvey Harrison 6652d4a7167SIngo Molnar static noinline void 6662d4a7167SIngo Molnar pgtable_bad(struct pt_regs *regs, unsigned long error_code, 6672d4a7167SIngo Molnar unsigned long address) 668c61e211dSHarvey Harrison { 6692d4a7167SIngo Molnar struct task_struct *tsk; 6702d4a7167SIngo Molnar unsigned long flags; 6712d4a7167SIngo Molnar int sig; 6722d4a7167SIngo Molnar 6732d4a7167SIngo Molnar flags = oops_begin(); 6742d4a7167SIngo Molnar tsk = current; 6752d4a7167SIngo Molnar sig = SIGKILL; 676c61e211dSHarvey Harrison 677c61e211dSHarvey Harrison printk(KERN_ALERT "%s: Corrupted page table at address %lx\n", 67892181f19SNick Piggin tsk->comm, address); 679c61e211dSHarvey Harrison dump_pagetable(address); 6802d4a7167SIngo Molnar 681c61e211dSHarvey Harrison if (__die("Bad pagetable", regs, error_code)) 682874d93d1SAlexander van Heukelum sig = 0; 6832d4a7167SIngo Molnar 684874d93d1SAlexander van Heukelum oops_end(flags, regs, sig); 685c61e211dSHarvey Harrison } 686c61e211dSHarvey Harrison 687e49d3cbeSAndy Lutomirski static void set_signal_archinfo(unsigned long address, 688e49d3cbeSAndy Lutomirski unsigned long error_code) 689e49d3cbeSAndy Lutomirski { 690e49d3cbeSAndy Lutomirski struct task_struct *tsk = current; 691e49d3cbeSAndy Lutomirski 692e49d3cbeSAndy Lutomirski /* 693e49d3cbeSAndy Lutomirski * To avoid leaking information about the kernel page 694e49d3cbeSAndy Lutomirski * table layout, pretend that user-mode accesses to 695e49d3cbeSAndy Lutomirski * kernel addresses are always protection faults. 696e0a446ceSAndy Lutomirski * 697e0a446ceSAndy Lutomirski * NB: This means that failed vsyscalls with vsyscall=none 698e0a446ceSAndy Lutomirski * will have the PROT bit. This doesn't leak any 699e0a446ceSAndy Lutomirski * information and does not appear to cause any problems. 700e49d3cbeSAndy Lutomirski */ 701e49d3cbeSAndy Lutomirski if (address >= TASK_SIZE_MAX) 702e49d3cbeSAndy Lutomirski error_code |= X86_PF_PROT; 703e49d3cbeSAndy Lutomirski 704e49d3cbeSAndy Lutomirski tsk->thread.trap_nr = X86_TRAP_PF; 705e49d3cbeSAndy Lutomirski tsk->thread.error_code = error_code | X86_PF_USER; 706e49d3cbeSAndy Lutomirski tsk->thread.cr2 = address; 707e49d3cbeSAndy Lutomirski } 708e49d3cbeSAndy Lutomirski 7092d4a7167SIngo Molnar static noinline void 7102d4a7167SIngo Molnar no_context(struct pt_regs *regs, unsigned long error_code, 7114fc34901SAndy Lutomirski unsigned long address, int signal, int si_code) 71292181f19SNick Piggin { 71392181f19SNick Piggin struct task_struct *tsk = current; 71492181f19SNick Piggin unsigned long flags; 71592181f19SNick Piggin int sig; 71692181f19SNick Piggin 717ebb53e25SAndy Lutomirski if (user_mode(regs)) { 718ebb53e25SAndy Lutomirski /* 719ebb53e25SAndy Lutomirski * This is an implicit supervisor-mode access from user 720ebb53e25SAndy Lutomirski * mode. Bypass all the kernel-mode recovery code and just 721ebb53e25SAndy Lutomirski * OOPS. 722ebb53e25SAndy Lutomirski */ 723ebb53e25SAndy Lutomirski goto oops; 724ebb53e25SAndy Lutomirski } 725ebb53e25SAndy Lutomirski 72692181f19SNick Piggin /* Are we prepared to handle this kernel fault? */ 72781fd9c18SJann Horn if (fixup_exception(regs, X86_TRAP_PF, error_code, address)) { 728c026b359SPeter Zijlstra /* 729c026b359SPeter Zijlstra * Any interrupt that takes a fault gets the fixup. This makes 730c026b359SPeter Zijlstra * the below recursive fault logic only apply to a faults from 731c026b359SPeter Zijlstra * task context. 732c026b359SPeter Zijlstra */ 733c026b359SPeter Zijlstra if (in_interrupt()) 734c026b359SPeter Zijlstra return; 735c026b359SPeter Zijlstra 736c026b359SPeter Zijlstra /* 737c026b359SPeter Zijlstra * Per the above we're !in_interrupt(), aka. task context. 738c026b359SPeter Zijlstra * 739c026b359SPeter Zijlstra * In this case we need to make sure we're not recursively 740c026b359SPeter Zijlstra * faulting through the emulate_vsyscall() logic. 741c026b359SPeter Zijlstra */ 7422a53ccbcSIngo Molnar if (current->thread.sig_on_uaccess_err && signal) { 743e49d3cbeSAndy Lutomirski set_signal_archinfo(address, error_code); 7444fc34901SAndy Lutomirski 7454fc34901SAndy Lutomirski /* XXX: hwpoison faults will set the wrong code. */ 7462e1661d2SEric W. Biederman force_sig_fault(signal, si_code, (void __user *)address); 7474fc34901SAndy Lutomirski } 748c026b359SPeter Zijlstra 749c026b359SPeter Zijlstra /* 750c026b359SPeter Zijlstra * Barring that, we can do the fixup and be happy. 751c026b359SPeter Zijlstra */ 75292181f19SNick Piggin return; 7534fc34901SAndy Lutomirski } 75492181f19SNick Piggin 7556271cfdfSAndy Lutomirski #ifdef CONFIG_VMAP_STACK 7566271cfdfSAndy Lutomirski /* 7576271cfdfSAndy Lutomirski * Stack overflow? During boot, we can fault near the initial 7586271cfdfSAndy Lutomirski * stack in the direct map, but that's not an overflow -- check 7596271cfdfSAndy Lutomirski * that we're in vmalloc space to avoid this. 7606271cfdfSAndy Lutomirski */ 7616271cfdfSAndy Lutomirski if (is_vmalloc_addr((void *)address) && 7626271cfdfSAndy Lutomirski (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) || 7636271cfdfSAndy Lutomirski address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) { 764d876b673SThomas Gleixner unsigned long stack = __this_cpu_ist_top_va(DF) - sizeof(void *); 7656271cfdfSAndy Lutomirski /* 7666271cfdfSAndy Lutomirski * We're likely to be running with very little stack space 7676271cfdfSAndy Lutomirski * left. It's plausible that we'd hit this condition but 7686271cfdfSAndy Lutomirski * double-fault even before we get this far, in which case 7696271cfdfSAndy Lutomirski * we're fine: the double-fault handler will deal with it. 7706271cfdfSAndy Lutomirski * 7716271cfdfSAndy Lutomirski * We don't want to make it all the way into the oops code 7726271cfdfSAndy Lutomirski * and then double-fault, though, because we're likely to 7736271cfdfSAndy Lutomirski * break the console driver and lose most of the stack dump. 7746271cfdfSAndy Lutomirski */ 7756271cfdfSAndy Lutomirski asm volatile ("movq %[stack], %%rsp\n\t" 7766271cfdfSAndy Lutomirski "call handle_stack_overflow\n\t" 7776271cfdfSAndy Lutomirski "1: jmp 1b" 778f5caf621SJosh Poimboeuf : ASM_CALL_CONSTRAINT 7796271cfdfSAndy Lutomirski : "D" ("kernel stack overflow (page fault)"), 7806271cfdfSAndy Lutomirski "S" (regs), "d" (address), 7816271cfdfSAndy Lutomirski [stack] "rm" (stack)); 7826271cfdfSAndy Lutomirski unreachable(); 7836271cfdfSAndy Lutomirski } 7846271cfdfSAndy Lutomirski #endif 7856271cfdfSAndy Lutomirski 78692181f19SNick Piggin /* 7872d4a7167SIngo Molnar * 32-bit: 7882d4a7167SIngo Molnar * 78992181f19SNick Piggin * Valid to do another page fault here, because if this fault 79092181f19SNick Piggin * had been triggered by is_prefetch fixup_exception would have 79192181f19SNick Piggin * handled it. 79292181f19SNick Piggin * 7932d4a7167SIngo Molnar * 64-bit: 7942d4a7167SIngo Molnar * 79592181f19SNick Piggin * Hall of shame of CPU/BIOS bugs. 79692181f19SNick Piggin */ 79792181f19SNick Piggin if (is_prefetch(regs, error_code, address)) 79892181f19SNick Piggin return; 79992181f19SNick Piggin 80092181f19SNick Piggin if (is_errata93(regs, address)) 80192181f19SNick Piggin return; 80292181f19SNick Piggin 80392181f19SNick Piggin /* 8043425d934SSai Praneeth * Buggy firmware could access regions which might page fault, try to 8053425d934SSai Praneeth * recover from such faults. 8063425d934SSai Praneeth */ 8073425d934SSai Praneeth if (IS_ENABLED(CONFIG_EFI)) 8083425d934SSai Praneeth efi_recover_from_page_fault(address); 8093425d934SSai Praneeth 810ebb53e25SAndy Lutomirski oops: 8113425d934SSai Praneeth /* 81292181f19SNick Piggin * Oops. The kernel tried to access some bad page. We'll have to 8132d4a7167SIngo Molnar * terminate things with extreme prejudice: 81492181f19SNick Piggin */ 81592181f19SNick Piggin flags = oops_begin(); 81692181f19SNick Piggin 81792181f19SNick Piggin show_fault_oops(regs, error_code, address); 81892181f19SNick Piggin 819a70857e4SAaron Tomlin if (task_stack_end_corrupted(tsk)) 820b0f4c4b3SPrarit Bhargava printk(KERN_EMERG "Thread overran stack, or stack corrupted\n"); 82119803078SIngo Molnar 82292181f19SNick Piggin sig = SIGKILL; 82392181f19SNick Piggin if (__die("Oops", regs, error_code)) 82492181f19SNick Piggin sig = 0; 8252d4a7167SIngo Molnar 82692181f19SNick Piggin /* Executive summary in case the body of the oops scrolled away */ 827b0f4c4b3SPrarit Bhargava printk(KERN_DEFAULT "CR2: %016lx\n", address); 8282d4a7167SIngo Molnar 82992181f19SNick Piggin oops_end(flags, regs, sig); 83092181f19SNick Piggin } 83192181f19SNick Piggin 8322d4a7167SIngo Molnar /* 8332d4a7167SIngo Molnar * Print out info about fatal segfaults, if the show_unhandled_signals 8342d4a7167SIngo Molnar * sysctl is set: 8352d4a7167SIngo Molnar */ 8362d4a7167SIngo Molnar static inline void 8372d4a7167SIngo Molnar show_signal_msg(struct pt_regs *regs, unsigned long error_code, 8382d4a7167SIngo Molnar unsigned long address, struct task_struct *tsk) 8392d4a7167SIngo Molnar { 840ba54d856SBorislav Petkov const char *loglvl = task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG; 841ba54d856SBorislav Petkov 8422d4a7167SIngo Molnar if (!unhandled_signal(tsk, SIGSEGV)) 8432d4a7167SIngo Molnar return; 8442d4a7167SIngo Molnar 8452d4a7167SIngo Molnar if (!printk_ratelimit()) 8462d4a7167SIngo Molnar return; 8472d4a7167SIngo Molnar 84810a7e9d8SKees Cook printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx", 849ba54d856SBorislav Petkov loglvl, tsk->comm, task_pid_nr(tsk), address, 8502d4a7167SIngo Molnar (void *)regs->ip, (void *)regs->sp, error_code); 8512d4a7167SIngo Molnar 8522d4a7167SIngo Molnar print_vma_addr(KERN_CONT " in ", regs->ip); 8532d4a7167SIngo Molnar 8542d4a7167SIngo Molnar printk(KERN_CONT "\n"); 855ba54d856SBorislav Petkov 856342db04aSJann Horn show_opcodes(regs, loglvl); 8572d4a7167SIngo Molnar } 8582d4a7167SIngo Molnar 85902e983b7SDave Hansen /* 86002e983b7SDave Hansen * The (legacy) vsyscall page is the long page in the kernel portion 86102e983b7SDave Hansen * of the address space that has user-accessible permissions. 86202e983b7SDave Hansen */ 86302e983b7SDave Hansen static bool is_vsyscall_vaddr(unsigned long vaddr) 86402e983b7SDave Hansen { 8653ae0ad92SDave Hansen return unlikely((vaddr & PAGE_MASK) == VSYSCALL_ADDR); 86602e983b7SDave Hansen } 86702e983b7SDave Hansen 8682d4a7167SIngo Molnar static void 8692d4a7167SIngo Molnar __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 870419ceeb1SEric W. Biederman unsigned long address, u32 pkey, int si_code) 87192181f19SNick Piggin { 87292181f19SNick Piggin struct task_struct *tsk = current; 87392181f19SNick Piggin 87492181f19SNick Piggin /* User mode accesses just cause a SIGSEGV */ 8756ea59b07SAndy Lutomirski if (user_mode(regs) && (error_code & X86_PF_USER)) { 87692181f19SNick Piggin /* 8772d4a7167SIngo Molnar * It's possible to have interrupts off here: 87892181f19SNick Piggin */ 87992181f19SNick Piggin local_irq_enable(); 88092181f19SNick Piggin 88192181f19SNick Piggin /* 88292181f19SNick Piggin * Valid to do another page fault here because this one came 8832d4a7167SIngo Molnar * from user space: 88492181f19SNick Piggin */ 88592181f19SNick Piggin if (is_prefetch(regs, error_code, address)) 88692181f19SNick Piggin return; 88792181f19SNick Piggin 88892181f19SNick Piggin if (is_errata100(regs, address)) 88992181f19SNick Piggin return; 89092181f19SNick Piggin 891dc4fac84SAndy Lutomirski /* 892dc4fac84SAndy Lutomirski * To avoid leaking information about the kernel page table 893dc4fac84SAndy Lutomirski * layout, pretend that user-mode accesses to kernel addresses 894dc4fac84SAndy Lutomirski * are always protection faults. 895dc4fac84SAndy Lutomirski */ 896dc4fac84SAndy Lutomirski if (address >= TASK_SIZE_MAX) 8971067f030SRicardo Neri error_code |= X86_PF_PROT; 8983ae36655SAndy Lutomirski 899e575a86fSKees Cook if (likely(show_unhandled_signals)) 9002d4a7167SIngo Molnar show_signal_msg(regs, error_code, address, tsk); 90192181f19SNick Piggin 902e49d3cbeSAndy Lutomirski set_signal_archinfo(address, error_code); 9032d4a7167SIngo Molnar 9049db812dbSEric W. Biederman if (si_code == SEGV_PKUERR) 905419ceeb1SEric W. Biederman force_sig_pkuerr((void __user *)address, pkey); 9069db812dbSEric W. Biederman 9072e1661d2SEric W. Biederman force_sig_fault(SIGSEGV, si_code, (void __user *)address); 9082d4a7167SIngo Molnar 90992181f19SNick Piggin return; 91092181f19SNick Piggin } 91192181f19SNick Piggin 91292181f19SNick Piggin if (is_f00f_bug(regs, address)) 91392181f19SNick Piggin return; 91492181f19SNick Piggin 9154fc34901SAndy Lutomirski no_context(regs, error_code, address, SIGSEGV, si_code); 91692181f19SNick Piggin } 91792181f19SNick Piggin 9182d4a7167SIngo Molnar static noinline void 9192d4a7167SIngo Molnar bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 920768fd9c6SEric W. Biederman unsigned long address) 92192181f19SNick Piggin { 922419ceeb1SEric W. Biederman __bad_area_nosemaphore(regs, error_code, address, 0, SEGV_MAPERR); 92392181f19SNick Piggin } 92492181f19SNick Piggin 9252d4a7167SIngo Molnar static void 9262d4a7167SIngo Molnar __bad_area(struct pt_regs *regs, unsigned long error_code, 927419ceeb1SEric W. Biederman unsigned long address, u32 pkey, int si_code) 92892181f19SNick Piggin { 92992181f19SNick Piggin struct mm_struct *mm = current->mm; 93092181f19SNick Piggin /* 93192181f19SNick Piggin * Something tried to access memory that isn't in our memory map.. 93292181f19SNick Piggin * Fix it, but check if it's kernel or user first.. 93392181f19SNick Piggin */ 93492181f19SNick Piggin up_read(&mm->mmap_sem); 93592181f19SNick Piggin 936aba1ecd3SEric W. Biederman __bad_area_nosemaphore(regs, error_code, address, pkey, si_code); 93792181f19SNick Piggin } 93892181f19SNick Piggin 9392d4a7167SIngo Molnar static noinline void 9402d4a7167SIngo Molnar bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address) 94192181f19SNick Piggin { 942419ceeb1SEric W. Biederman __bad_area(regs, error_code, address, 0, SEGV_MAPERR); 94392181f19SNick Piggin } 94492181f19SNick Piggin 94533a709b2SDave Hansen static inline bool bad_area_access_from_pkeys(unsigned long error_code, 94633a709b2SDave Hansen struct vm_area_struct *vma) 94733a709b2SDave Hansen { 94807f146f5SDave Hansen /* This code is always called on the current mm */ 94907f146f5SDave Hansen bool foreign = false; 95007f146f5SDave Hansen 95133a709b2SDave Hansen if (!boot_cpu_has(X86_FEATURE_OSPKE)) 95233a709b2SDave Hansen return false; 9531067f030SRicardo Neri if (error_code & X86_PF_PK) 95433a709b2SDave Hansen return true; 95507f146f5SDave Hansen /* this checks permission keys on the VMA: */ 9561067f030SRicardo Neri if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE), 9571067f030SRicardo Neri (error_code & X86_PF_INSTR), foreign)) 95807f146f5SDave Hansen return true; 95933a709b2SDave Hansen return false; 96092181f19SNick Piggin } 96192181f19SNick Piggin 9622d4a7167SIngo Molnar static noinline void 9632d4a7167SIngo Molnar bad_area_access_error(struct pt_regs *regs, unsigned long error_code, 9647b2d0dbaSDave Hansen unsigned long address, struct vm_area_struct *vma) 96592181f19SNick Piggin { 966019132ffSDave Hansen /* 967019132ffSDave Hansen * This OSPKE check is not strictly necessary at runtime. 968019132ffSDave Hansen * But, doing it this way allows compiler optimizations 969019132ffSDave Hansen * if pkeys are compiled out. 970019132ffSDave Hansen */ 971aba1ecd3SEric W. Biederman if (bad_area_access_from_pkeys(error_code, vma)) { 9729db812dbSEric W. Biederman /* 9739db812dbSEric W. Biederman * A protection key fault means that the PKRU value did not allow 9749db812dbSEric W. Biederman * access to some PTE. Userspace can figure out what PKRU was 9759db812dbSEric W. Biederman * from the XSAVE state. This function captures the pkey from 9769db812dbSEric W. Biederman * the vma and passes it to userspace so userspace can discover 9779db812dbSEric W. Biederman * which protection key was set on the PTE. 9789db812dbSEric W. Biederman * 9799db812dbSEric W. Biederman * If we get here, we know that the hardware signaled a X86_PF_PK 9809db812dbSEric W. Biederman * fault and that there was a VMA once we got in the fault 9819db812dbSEric W. Biederman * handler. It does *not* guarantee that the VMA we find here 9829db812dbSEric W. Biederman * was the one that we faulted on. 9839db812dbSEric W. Biederman * 9849db812dbSEric W. Biederman * 1. T1 : mprotect_key(foo, PAGE_SIZE, pkey=4); 9859db812dbSEric W. Biederman * 2. T1 : set PKRU to deny access to pkey=4, touches page 9869db812dbSEric W. Biederman * 3. T1 : faults... 9879db812dbSEric W. Biederman * 4. T2: mprotect_key(foo, PAGE_SIZE, pkey=5); 9889db812dbSEric W. Biederman * 5. T1 : enters fault handler, takes mmap_sem, etc... 9899db812dbSEric W. Biederman * 6. T1 : reaches here, sees vma_pkey(vma)=5, when we really 9909db812dbSEric W. Biederman * faulted on a pte with its pkey=4. 9919db812dbSEric W. Biederman */ 992aba1ecd3SEric W. Biederman u32 pkey = vma_pkey(vma); 9939db812dbSEric W. Biederman 994419ceeb1SEric W. Biederman __bad_area(regs, error_code, address, pkey, SEGV_PKUERR); 995aba1ecd3SEric W. Biederman } else { 996419ceeb1SEric W. Biederman __bad_area(regs, error_code, address, 0, SEGV_ACCERR); 997aba1ecd3SEric W. Biederman } 99892181f19SNick Piggin } 99992181f19SNick Piggin 10002d4a7167SIngo Molnar static void 1001a6e04aa9SAndi Kleen do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address, 10023d353901SSouptick Joarder vm_fault_t fault) 100392181f19SNick Piggin { 10042d4a7167SIngo Molnar /* Kernel mode? Handle exceptions or die: */ 10051067f030SRicardo Neri if (!(error_code & X86_PF_USER)) { 10064fc34901SAndy Lutomirski no_context(regs, error_code, address, SIGBUS, BUS_ADRERR); 100796054569SLinus Torvalds return; 100896054569SLinus Torvalds } 10092d4a7167SIngo Molnar 1010cd1b68f0SIngo Molnar /* User-space => ok to do another page fault: */ 101192181f19SNick Piggin if (is_prefetch(regs, error_code, address)) 101292181f19SNick Piggin return; 10132d4a7167SIngo Molnar 1014e49d3cbeSAndy Lutomirski set_signal_archinfo(address, error_code); 10152d4a7167SIngo Molnar 1016a6e04aa9SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE 1017f672b49bSAndi Kleen if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) { 1018318759b4SEric W. Biederman struct task_struct *tsk = current; 101940e55394SEric W. Biederman unsigned lsb = 0; 102040e55394SEric W. Biederman 102140e55394SEric W. Biederman pr_err( 1022a6e04aa9SAndi Kleen "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n", 1023a6e04aa9SAndi Kleen tsk->comm, tsk->pid, address); 102440e55394SEric W. Biederman if (fault & VM_FAULT_HWPOISON_LARGE) 102540e55394SEric W. Biederman lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault)); 102640e55394SEric W. Biederman if (fault & VM_FAULT_HWPOISON) 102740e55394SEric W. Biederman lsb = PAGE_SHIFT; 1028f8eac901SEric W. Biederman force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb); 102940e55394SEric W. Biederman return; 1030a6e04aa9SAndi Kleen } 1031a6e04aa9SAndi Kleen #endif 10322e1661d2SEric W. Biederman force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address); 103392181f19SNick Piggin } 103492181f19SNick Piggin 10353a13c4d7SJohannes Weiner static noinline void 10362d4a7167SIngo Molnar mm_fault_error(struct pt_regs *regs, unsigned long error_code, 103725c102d8SEric W. Biederman unsigned long address, vm_fault_t fault) 103892181f19SNick Piggin { 10391067f030SRicardo Neri if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) { 10404fc34901SAndy Lutomirski no_context(regs, error_code, address, 0, 0); 10413a13c4d7SJohannes Weiner return; 1042b80ef10eSKOSAKI Motohiro } 1043b80ef10eSKOSAKI Motohiro 10442d4a7167SIngo Molnar if (fault & VM_FAULT_OOM) { 1045f8626854SAndrey Vagin /* Kernel mode? Handle exceptions or die: */ 10461067f030SRicardo Neri if (!(error_code & X86_PF_USER)) { 10474fc34901SAndy Lutomirski no_context(regs, error_code, address, 10484fc34901SAndy Lutomirski SIGSEGV, SEGV_MAPERR); 10493a13c4d7SJohannes Weiner return; 1050f8626854SAndrey Vagin } 1051f8626854SAndrey Vagin 1052c2d23f91SDavid Rientjes /* 1053c2d23f91SDavid Rientjes * We ran out of memory, call the OOM killer, and return the 1054c2d23f91SDavid Rientjes * userspace (which will retry the fault, or kill us if we got 1055c2d23f91SDavid Rientjes * oom-killed): 1056c2d23f91SDavid Rientjes */ 1057c2d23f91SDavid Rientjes pagefault_out_of_memory(); 10582d4a7167SIngo Molnar } else { 1059f672b49bSAndi Kleen if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON| 1060f672b49bSAndi Kleen VM_FAULT_HWPOISON_LARGE)) 106127274f73SEric W. Biederman do_sigbus(regs, error_code, address, fault); 106233692f27SLinus Torvalds else if (fault & VM_FAULT_SIGSEGV) 1063768fd9c6SEric W. Biederman bad_area_nosemaphore(regs, error_code, address); 106492181f19SNick Piggin else 106592181f19SNick Piggin BUG(); 106692181f19SNick Piggin } 10672d4a7167SIngo Molnar } 106892181f19SNick Piggin 10698fed6200SDave Hansen static int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte) 1070d8b57bb7SThomas Gleixner { 10711067f030SRicardo Neri if ((error_code & X86_PF_WRITE) && !pte_write(*pte)) 1072d8b57bb7SThomas Gleixner return 0; 10732d4a7167SIngo Molnar 10741067f030SRicardo Neri if ((error_code & X86_PF_INSTR) && !pte_exec(*pte)) 1075d8b57bb7SThomas Gleixner return 0; 1076d8b57bb7SThomas Gleixner 1077d8b57bb7SThomas Gleixner return 1; 1078d8b57bb7SThomas Gleixner } 1079d8b57bb7SThomas Gleixner 1080c61e211dSHarvey Harrison /* 10812d4a7167SIngo Molnar * Handle a spurious fault caused by a stale TLB entry. 10822d4a7167SIngo Molnar * 10832d4a7167SIngo Molnar * This allows us to lazily refresh the TLB when increasing the 10842d4a7167SIngo Molnar * permissions of a kernel page (RO -> RW or NX -> X). Doing it 10852d4a7167SIngo Molnar * eagerly is very expensive since that implies doing a full 10862d4a7167SIngo Molnar * cross-processor TLB flush, even if no stale TLB entries exist 10872d4a7167SIngo Molnar * on other processors. 10882d4a7167SIngo Molnar * 108931668511SDavid Vrabel * Spurious faults may only occur if the TLB contains an entry with 109031668511SDavid Vrabel * fewer permission than the page table entry. Non-present (P = 0) 109131668511SDavid Vrabel * and reserved bit (R = 1) faults are never spurious. 109231668511SDavid Vrabel * 10935b727a3bSJeremy Fitzhardinge * There are no security implications to leaving a stale TLB when 10945b727a3bSJeremy Fitzhardinge * increasing the permissions on a page. 109531668511SDavid Vrabel * 109631668511SDavid Vrabel * Returns non-zero if a spurious fault was handled, zero otherwise. 109731668511SDavid Vrabel * 109831668511SDavid Vrabel * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3 109931668511SDavid Vrabel * (Optional Invalidation). 11005b727a3bSJeremy Fitzhardinge */ 11019326638cSMasami Hiramatsu static noinline int 11028fed6200SDave Hansen spurious_kernel_fault(unsigned long error_code, unsigned long address) 11035b727a3bSJeremy Fitzhardinge { 11045b727a3bSJeremy Fitzhardinge pgd_t *pgd; 1105e0c4f675SKirill A. Shutemov p4d_t *p4d; 11065b727a3bSJeremy Fitzhardinge pud_t *pud; 11075b727a3bSJeremy Fitzhardinge pmd_t *pmd; 11085b727a3bSJeremy Fitzhardinge pte_t *pte; 11093c3e5694SSteven Rostedt int ret; 11105b727a3bSJeremy Fitzhardinge 111131668511SDavid Vrabel /* 111231668511SDavid Vrabel * Only writes to RO or instruction fetches from NX may cause 111331668511SDavid Vrabel * spurious faults. 111431668511SDavid Vrabel * 111531668511SDavid Vrabel * These could be from user or supervisor accesses but the TLB 111631668511SDavid Vrabel * is only lazily flushed after a kernel mapping protection 111731668511SDavid Vrabel * change, so user accesses are not expected to cause spurious 111831668511SDavid Vrabel * faults. 111931668511SDavid Vrabel */ 11201067f030SRicardo Neri if (error_code != (X86_PF_WRITE | X86_PF_PROT) && 11211067f030SRicardo Neri error_code != (X86_PF_INSTR | X86_PF_PROT)) 11225b727a3bSJeremy Fitzhardinge return 0; 11235b727a3bSJeremy Fitzhardinge 11245b727a3bSJeremy Fitzhardinge pgd = init_mm.pgd + pgd_index(address); 11255b727a3bSJeremy Fitzhardinge if (!pgd_present(*pgd)) 11265b727a3bSJeremy Fitzhardinge return 0; 11275b727a3bSJeremy Fitzhardinge 1128e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, address); 1129e0c4f675SKirill A. Shutemov if (!p4d_present(*p4d)) 1130e0c4f675SKirill A. Shutemov return 0; 1131e0c4f675SKirill A. Shutemov 1132e0c4f675SKirill A. Shutemov if (p4d_large(*p4d)) 11338fed6200SDave Hansen return spurious_kernel_fault_check(error_code, (pte_t *) p4d); 1134e0c4f675SKirill A. Shutemov 1135e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, address); 11365b727a3bSJeremy Fitzhardinge if (!pud_present(*pud)) 11375b727a3bSJeremy Fitzhardinge return 0; 11385b727a3bSJeremy Fitzhardinge 1139d8b57bb7SThomas Gleixner if (pud_large(*pud)) 11408fed6200SDave Hansen return spurious_kernel_fault_check(error_code, (pte_t *) pud); 1141d8b57bb7SThomas Gleixner 11425b727a3bSJeremy Fitzhardinge pmd = pmd_offset(pud, address); 11435b727a3bSJeremy Fitzhardinge if (!pmd_present(*pmd)) 11445b727a3bSJeremy Fitzhardinge return 0; 11455b727a3bSJeremy Fitzhardinge 1146d8b57bb7SThomas Gleixner if (pmd_large(*pmd)) 11478fed6200SDave Hansen return spurious_kernel_fault_check(error_code, (pte_t *) pmd); 1148d8b57bb7SThomas Gleixner 11495b727a3bSJeremy Fitzhardinge pte = pte_offset_kernel(pmd, address); 1150954f8571SAndrea Arcangeli if (!pte_present(*pte)) 11515b727a3bSJeremy Fitzhardinge return 0; 11525b727a3bSJeremy Fitzhardinge 11538fed6200SDave Hansen ret = spurious_kernel_fault_check(error_code, pte); 11543c3e5694SSteven Rostedt if (!ret) 11553c3e5694SSteven Rostedt return 0; 11563c3e5694SSteven Rostedt 11573c3e5694SSteven Rostedt /* 11582d4a7167SIngo Molnar * Make sure we have permissions in PMD. 11592d4a7167SIngo Molnar * If not, then there's a bug in the page tables: 11603c3e5694SSteven Rostedt */ 11618fed6200SDave Hansen ret = spurious_kernel_fault_check(error_code, (pte_t *) pmd); 11623c3e5694SSteven Rostedt WARN_ONCE(!ret, "PMD has incorrect permission bits\n"); 11632d4a7167SIngo Molnar 11643c3e5694SSteven Rostedt return ret; 11655b727a3bSJeremy Fitzhardinge } 11668fed6200SDave Hansen NOKPROBE_SYMBOL(spurious_kernel_fault); 11675b727a3bSJeremy Fitzhardinge 1168c61e211dSHarvey Harrison int show_unhandled_signals = 1; 1169c61e211dSHarvey Harrison 11702d4a7167SIngo Molnar static inline int 117168da336aSMichel Lespinasse access_error(unsigned long error_code, struct vm_area_struct *vma) 117292181f19SNick Piggin { 117307f146f5SDave Hansen /* This is only called for the current mm, so: */ 117407f146f5SDave Hansen bool foreign = false; 1175e8c6226dSDave Hansen 1176e8c6226dSDave Hansen /* 1177e8c6226dSDave Hansen * Read or write was blocked by protection keys. This is 1178e8c6226dSDave Hansen * always an unconditional error and can never result in 1179e8c6226dSDave Hansen * a follow-up action to resolve the fault, like a COW. 1180e8c6226dSDave Hansen */ 11811067f030SRicardo Neri if (error_code & X86_PF_PK) 1182e8c6226dSDave Hansen return 1; 1183e8c6226dSDave Hansen 118433a709b2SDave Hansen /* 118507f146f5SDave Hansen * Make sure to check the VMA so that we do not perform 11861067f030SRicardo Neri * faults just to hit a X86_PF_PK as soon as we fill in a 118707f146f5SDave Hansen * page. 118807f146f5SDave Hansen */ 11891067f030SRicardo Neri if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE), 11901067f030SRicardo Neri (error_code & X86_PF_INSTR), foreign)) 119107f146f5SDave Hansen return 1; 119233a709b2SDave Hansen 11931067f030SRicardo Neri if (error_code & X86_PF_WRITE) { 11942d4a7167SIngo Molnar /* write, present and write, not present: */ 119592181f19SNick Piggin if (unlikely(!(vma->vm_flags & VM_WRITE))) 119692181f19SNick Piggin return 1; 11972d4a7167SIngo Molnar return 0; 11982d4a7167SIngo Molnar } 11992d4a7167SIngo Molnar 12002d4a7167SIngo Molnar /* read, present: */ 12011067f030SRicardo Neri if (unlikely(error_code & X86_PF_PROT)) 120292181f19SNick Piggin return 1; 12032d4a7167SIngo Molnar 12042d4a7167SIngo Molnar /* read, not present: */ 120592181f19SNick Piggin if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))) 120692181f19SNick Piggin return 1; 120792181f19SNick Piggin 120892181f19SNick Piggin return 0; 120992181f19SNick Piggin } 121092181f19SNick Piggin 12110973a06cSHiroshi Shimamoto static int fault_in_kernel_space(unsigned long address) 12120973a06cSHiroshi Shimamoto { 12133ae0ad92SDave Hansen /* 12143ae0ad92SDave Hansen * On 64-bit systems, the vsyscall page is at an address above 12153ae0ad92SDave Hansen * TASK_SIZE_MAX, but is not considered part of the kernel 12163ae0ad92SDave Hansen * address space. 12173ae0ad92SDave Hansen */ 12183ae0ad92SDave Hansen if (IS_ENABLED(CONFIG_X86_64) && is_vsyscall_vaddr(address)) 12193ae0ad92SDave Hansen return false; 12203ae0ad92SDave Hansen 1221d9517346SIngo Molnar return address >= TASK_SIZE_MAX; 12220973a06cSHiroshi Shimamoto } 12230973a06cSHiroshi Shimamoto 1224c61e211dSHarvey Harrison /* 12258fed6200SDave Hansen * Called for all faults where 'address' is part of the kernel address 12268fed6200SDave Hansen * space. Might get called for faults that originate from *code* that 12278fed6200SDave Hansen * ran in userspace or the kernel. 1228c61e211dSHarvey Harrison */ 12298fed6200SDave Hansen static void 12308fed6200SDave Hansen do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, 12310ac09f9fSJiri Olsa unsigned long address) 1232c61e211dSHarvey Harrison { 12338fed6200SDave Hansen /* 1234367e3f1dSDave Hansen * Protection keys exceptions only happen on user pages. We 1235367e3f1dSDave Hansen * have no user pages in the kernel portion of the address 1236367e3f1dSDave Hansen * space, so do not expect them here. 1237367e3f1dSDave Hansen */ 1238367e3f1dSDave Hansen WARN_ON_ONCE(hw_error_code & X86_PF_PK); 1239367e3f1dSDave Hansen 1240367e3f1dSDave Hansen /* 12418fed6200SDave Hansen * We can fault-in kernel-space virtual memory on-demand. The 12428fed6200SDave Hansen * 'reference' page table is init_mm.pgd. 12438fed6200SDave Hansen * 12448fed6200SDave Hansen * NOTE! We MUST NOT take any locks for this case. We may 12458fed6200SDave Hansen * be in an interrupt or a critical region, and should 12468fed6200SDave Hansen * only copy the information from the master page table, 12478fed6200SDave Hansen * nothing more. 12488fed6200SDave Hansen * 12498fed6200SDave Hansen * Before doing this on-demand faulting, ensure that the 12508fed6200SDave Hansen * fault is not any of the following: 12518fed6200SDave Hansen * 1. A fault on a PTE with a reserved bit set. 12528fed6200SDave Hansen * 2. A fault caused by a user-mode access. (Do not demand- 12538fed6200SDave Hansen * fault kernel memory due to user-mode accesses). 12548fed6200SDave Hansen * 3. A fault caused by a page-level protection violation. 12558fed6200SDave Hansen * (A demand fault would be on a non-present page which 12568fed6200SDave Hansen * would have X86_PF_PROT==0). 12578fed6200SDave Hansen */ 12588fed6200SDave Hansen if (!(hw_error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) { 12598fed6200SDave Hansen if (vmalloc_fault(address) >= 0) 12608fed6200SDave Hansen return; 12618fed6200SDave Hansen } 12628fed6200SDave Hansen 12638fed6200SDave Hansen /* Was the fault spurious, caused by lazy TLB invalidation? */ 12648fed6200SDave Hansen if (spurious_kernel_fault(hw_error_code, address)) 12658fed6200SDave Hansen return; 12668fed6200SDave Hansen 12678fed6200SDave Hansen /* kprobes don't want to hook the spurious faults: */ 1268b98cca44SAnshuman Khandual if (kprobe_page_fault(regs, X86_TRAP_PF)) 12698fed6200SDave Hansen return; 12708fed6200SDave Hansen 12718fed6200SDave Hansen /* 12728fed6200SDave Hansen * Note, despite being a "bad area", there are quite a few 12738fed6200SDave Hansen * acceptable reasons to get here, such as erratum fixups 12748fed6200SDave Hansen * and handling kernel code that can fault, like get_user(). 12758fed6200SDave Hansen * 12768fed6200SDave Hansen * Don't take the mm semaphore here. If we fixup a prefetch 12778fed6200SDave Hansen * fault we could otherwise deadlock: 12788fed6200SDave Hansen */ 1279ba9f6f89SLinus Torvalds bad_area_nosemaphore(regs, hw_error_code, address); 12808fed6200SDave Hansen } 12818fed6200SDave Hansen NOKPROBE_SYMBOL(do_kern_addr_fault); 12828fed6200SDave Hansen 1283aa37c51bSDave Hansen /* Handle faults in the user portion of the address space */ 1284aa37c51bSDave Hansen static inline 1285aa37c51bSDave Hansen void do_user_addr_fault(struct pt_regs *regs, 1286aa37c51bSDave Hansen unsigned long hw_error_code, 1287c61e211dSHarvey Harrison unsigned long address) 1288c61e211dSHarvey Harrison { 1289c61e211dSHarvey Harrison struct vm_area_struct *vma; 1290c61e211dSHarvey Harrison struct task_struct *tsk; 12912d4a7167SIngo Molnar struct mm_struct *mm; 129250a7ca3cSSouptick Joarder vm_fault_t fault, major = 0; 1293759496baSJohannes Weiner unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 1294c61e211dSHarvey Harrison 1295c61e211dSHarvey Harrison tsk = current; 1296c61e211dSHarvey Harrison mm = tsk->mm; 12972d4a7167SIngo Molnar 12982d4a7167SIngo Molnar /* kprobes don't want to hook the spurious faults: */ 1299b98cca44SAnshuman Khandual if (unlikely(kprobe_page_fault(regs, X86_TRAP_PF))) 13009be260a6SMasami Hiramatsu return; 1301e00b12e6SPeter Zijlstra 13025b0c2cacSDave Hansen /* 13035b0c2cacSDave Hansen * Reserved bits are never expected to be set on 13045b0c2cacSDave Hansen * entries in the user portion of the page tables. 13055b0c2cacSDave Hansen */ 1306164477c2SDave Hansen if (unlikely(hw_error_code & X86_PF_RSVD)) 1307164477c2SDave Hansen pgtable_bad(regs, hw_error_code, address); 1308e00b12e6SPeter Zijlstra 13095b0c2cacSDave Hansen /* 1310e50928d7SAndy Lutomirski * If SMAP is on, check for invalid kernel (supervisor) access to user 1311e50928d7SAndy Lutomirski * pages in the user address space. The odd case here is WRUSS, 1312e50928d7SAndy Lutomirski * which, according to the preliminary documentation, does not respect 1313e50928d7SAndy Lutomirski * SMAP and will have the USER bit set so, in all cases, SMAP 1314e50928d7SAndy Lutomirski * enforcement appears to be consistent with the USER bit. 13155b0c2cacSDave Hansen */ 1316a15781b5SAndy Lutomirski if (unlikely(cpu_feature_enabled(X86_FEATURE_SMAP) && 1317a15781b5SAndy Lutomirski !(hw_error_code & X86_PF_USER) && 1318e50928d7SAndy Lutomirski !(regs->flags & X86_EFLAGS_AC))) 1319a15781b5SAndy Lutomirski { 1320ba9f6f89SLinus Torvalds bad_area_nosemaphore(regs, hw_error_code, address); 1321e00b12e6SPeter Zijlstra return; 1322e00b12e6SPeter Zijlstra } 1323e00b12e6SPeter Zijlstra 1324e00b12e6SPeter Zijlstra /* 1325e00b12e6SPeter Zijlstra * If we're in an interrupt, have no user context or are running 132670ffdb93SDavid Hildenbrand * in a region with pagefaults disabled then we must not take the fault 1327e00b12e6SPeter Zijlstra */ 132870ffdb93SDavid Hildenbrand if (unlikely(faulthandler_disabled() || !mm)) { 1329ba9f6f89SLinus Torvalds bad_area_nosemaphore(regs, hw_error_code, address); 1330e00b12e6SPeter Zijlstra return; 1331e00b12e6SPeter Zijlstra } 1332e00b12e6SPeter Zijlstra 1333c61e211dSHarvey Harrison /* 1334891cffbdSLinus Torvalds * It's safe to allow irq's after cr2 has been saved and the 1335891cffbdSLinus Torvalds * vmalloc fault has been handled. 1336891cffbdSLinus Torvalds * 1337891cffbdSLinus Torvalds * User-mode registers count as a user access even for any 13382d4a7167SIngo Molnar * potential system fault or CPU buglet: 1339c61e211dSHarvey Harrison */ 1340f39b6f0eSAndy Lutomirski if (user_mode(regs)) { 1341891cffbdSLinus Torvalds local_irq_enable(); 1342759496baSJohannes Weiner flags |= FAULT_FLAG_USER; 13432d4a7167SIngo Molnar } else { 13442d4a7167SIngo Molnar if (regs->flags & X86_EFLAGS_IF) 1345c61e211dSHarvey Harrison local_irq_enable(); 13462d4a7167SIngo Molnar } 1347c61e211dSHarvey Harrison 1348a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 13497dd1fcc2SPeter Zijlstra 13500ed32f1aSAndy Lutomirski if (hw_error_code & X86_PF_WRITE) 1351759496baSJohannes Weiner flags |= FAULT_FLAG_WRITE; 13520ed32f1aSAndy Lutomirski if (hw_error_code & X86_PF_INSTR) 1353d61172b4SDave Hansen flags |= FAULT_FLAG_INSTRUCTION; 1354759496baSJohannes Weiner 13553ae0ad92SDave Hansen #ifdef CONFIG_X86_64 13563a1dfe6eSIngo Molnar /* 1357918ce325SAndy Lutomirski * Faults in the vsyscall page might need emulation. The 1358918ce325SAndy Lutomirski * vsyscall page is at a high address (>PAGE_OFFSET), but is 1359918ce325SAndy Lutomirski * considered to be part of the user address space. 1360c61e211dSHarvey Harrison * 13613ae0ad92SDave Hansen * The vsyscall page does not have a "real" VMA, so do this 13623ae0ad92SDave Hansen * emulation before we go searching for VMAs. 1363e0a446ceSAndy Lutomirski * 1364e0a446ceSAndy Lutomirski * PKRU never rejects instruction fetches, so we don't need 1365e0a446ceSAndy Lutomirski * to consider the PF_PK bit. 13663ae0ad92SDave Hansen */ 1367918ce325SAndy Lutomirski if (is_vsyscall_vaddr(address)) { 1368918ce325SAndy Lutomirski if (emulate_vsyscall(hw_error_code, regs, address)) 13693ae0ad92SDave Hansen return; 13703ae0ad92SDave Hansen } 13713ae0ad92SDave Hansen #endif 13723ae0ad92SDave Hansen 1373c61e211dSHarvey Harrison /* 137488259744SDave Hansen * Kernel-mode access to the user address space should only occur 137588259744SDave Hansen * on well-defined single instructions listed in the exception 137688259744SDave Hansen * tables. But, an erroneous kernel fault occurring outside one of 137788259744SDave Hansen * those areas which also holds mmap_sem might deadlock attempting 137888259744SDave Hansen * to validate the fault against the address space. 1379c61e211dSHarvey Harrison * 138088259744SDave Hansen * Only do the expensive exception table search when we might be at 138188259744SDave Hansen * risk of a deadlock. This happens if we 138288259744SDave Hansen * 1. Failed to acquire mmap_sem, and 13836344be60SAndy Lutomirski * 2. The access did not originate in userspace. 1384c61e211dSHarvey Harrison */ 138592181f19SNick Piggin if (unlikely(!down_read_trylock(&mm->mmap_sem))) { 13866344be60SAndy Lutomirski if (!user_mode(regs) && !search_exception_tables(regs->ip)) { 138788259744SDave Hansen /* 138888259744SDave Hansen * Fault from code in kernel from 138988259744SDave Hansen * which we do not expect faults. 139088259744SDave Hansen */ 13910ed32f1aSAndy Lutomirski bad_area_nosemaphore(regs, hw_error_code, address); 139292181f19SNick Piggin return; 139392181f19SNick Piggin } 1394d065bd81SMichel Lespinasse retry: 1395c61e211dSHarvey Harrison down_read(&mm->mmap_sem); 139601006074SPeter Zijlstra } else { 139701006074SPeter Zijlstra /* 13982d4a7167SIngo Molnar * The above down_read_trylock() might have succeeded in 13992d4a7167SIngo Molnar * which case we'll have missed the might_sleep() from 14002d4a7167SIngo Molnar * down_read(): 140101006074SPeter Zijlstra */ 140201006074SPeter Zijlstra might_sleep(); 1403c61e211dSHarvey Harrison } 1404c61e211dSHarvey Harrison 1405c61e211dSHarvey Harrison vma = find_vma(mm, address); 140692181f19SNick Piggin if (unlikely(!vma)) { 14070ed32f1aSAndy Lutomirski bad_area(regs, hw_error_code, address); 140892181f19SNick Piggin return; 140992181f19SNick Piggin } 141092181f19SNick Piggin if (likely(vma->vm_start <= address)) 1411c61e211dSHarvey Harrison goto good_area; 141292181f19SNick Piggin if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) { 14130ed32f1aSAndy Lutomirski bad_area(regs, hw_error_code, address); 141492181f19SNick Piggin return; 141592181f19SNick Piggin } 141692181f19SNick Piggin if (unlikely(expand_stack(vma, address))) { 14170ed32f1aSAndy Lutomirski bad_area(regs, hw_error_code, address); 141892181f19SNick Piggin return; 141992181f19SNick Piggin } 142092181f19SNick Piggin 1421c61e211dSHarvey Harrison /* 1422c61e211dSHarvey Harrison * Ok, we have a good vm_area for this memory access, so 1423c61e211dSHarvey Harrison * we can handle it.. 1424c61e211dSHarvey Harrison */ 1425c61e211dSHarvey Harrison good_area: 14260ed32f1aSAndy Lutomirski if (unlikely(access_error(hw_error_code, vma))) { 14270ed32f1aSAndy Lutomirski bad_area_access_error(regs, hw_error_code, address, vma); 142892181f19SNick Piggin return; 1429c61e211dSHarvey Harrison } 1430c61e211dSHarvey Harrison 1431c61e211dSHarvey Harrison /* 1432c61e211dSHarvey Harrison * If for any reason at all we couldn't handle the fault, 1433c61e211dSHarvey Harrison * make sure we exit gracefully rather than endlessly redo 14349a95f3cfSPaul Cassella * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if 14359a95f3cfSPaul Cassella * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked. 1436cb0631fdSVlastimil Babka * 1437cb0631fdSVlastimil Babka * Note that handle_userfault() may also release and reacquire mmap_sem 1438cb0631fdSVlastimil Babka * (and not return with VM_FAULT_RETRY), when returning to userland to 1439cb0631fdSVlastimil Babka * repeat the page fault later with a VM_FAULT_NOPAGE retval 1440cb0631fdSVlastimil Babka * (potentially after handling any pending signal during the return to 1441cb0631fdSVlastimil Babka * userland). The return to userland is identified whenever 1442cb0631fdSVlastimil Babka * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags. 1443c61e211dSHarvey Harrison */ 1444dcddffd4SKirill A. Shutemov fault = handle_mm_fault(vma, address, flags); 144526178ec1SLinus Torvalds major |= fault & VM_FAULT_MAJOR; 14462d4a7167SIngo Molnar 14473a13c4d7SJohannes Weiner /* 144826178ec1SLinus Torvalds * If we need to retry the mmap_sem has already been released, 144926178ec1SLinus Torvalds * and if there is a fatal signal pending there is no guarantee 145026178ec1SLinus Torvalds * that we made any progress. Handle this case first. 14513a13c4d7SJohannes Weiner */ 145226178ec1SLinus Torvalds if (unlikely(fault & VM_FAULT_RETRY)) { 145326178ec1SLinus Torvalds /* Retry at most once */ 145426178ec1SLinus Torvalds if (flags & FAULT_FLAG_ALLOW_RETRY) { 145526178ec1SLinus Torvalds flags &= ~FAULT_FLAG_ALLOW_RETRY; 145626178ec1SLinus Torvalds flags |= FAULT_FLAG_TRIED; 145726178ec1SLinus Torvalds if (!fatal_signal_pending(tsk)) 145826178ec1SLinus Torvalds goto retry; 145926178ec1SLinus Torvalds } 146026178ec1SLinus Torvalds 146126178ec1SLinus Torvalds /* User mode? Just return to handle the fatal exception */ 1462cf3c0a15SLinus Torvalds if (flags & FAULT_FLAG_USER) 14633a13c4d7SJohannes Weiner return; 14643a13c4d7SJohannes Weiner 146526178ec1SLinus Torvalds /* Not returning to user mode? Handle exceptions or die: */ 14660ed32f1aSAndy Lutomirski no_context(regs, hw_error_code, address, SIGBUS, BUS_ADRERR); 146726178ec1SLinus Torvalds return; 146826178ec1SLinus Torvalds } 146926178ec1SLinus Torvalds 14707fb08ecaSLinus Torvalds up_read(&mm->mmap_sem); 147126178ec1SLinus Torvalds if (unlikely(fault & VM_FAULT_ERROR)) { 14720ed32f1aSAndy Lutomirski mm_fault_error(regs, hw_error_code, address, fault); 147337b23e05SKOSAKI Motohiro return; 147437b23e05SKOSAKI Motohiro } 147537b23e05SKOSAKI Motohiro 147637b23e05SKOSAKI Motohiro /* 147726178ec1SLinus Torvalds * Major/minor page fault accounting. If any of the events 147826178ec1SLinus Torvalds * returned VM_FAULT_MAJOR, we account it as a major fault. 1479d065bd81SMichel Lespinasse */ 148026178ec1SLinus Torvalds if (major) { 1481c61e211dSHarvey Harrison tsk->maj_flt++; 148226178ec1SLinus Torvalds perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address); 1483ac17dc8eSPeter Zijlstra } else { 1484c61e211dSHarvey Harrison tsk->min_flt++; 148526178ec1SLinus Torvalds perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address); 1486d065bd81SMichel Lespinasse } 1487c61e211dSHarvey Harrison 14888c938f9fSIngo Molnar check_v8086_mode(regs, address, tsk); 1489c61e211dSHarvey Harrison } 1490aa37c51bSDave Hansen NOKPROBE_SYMBOL(do_user_addr_fault); 1491aa37c51bSDave Hansen 1492aa37c51bSDave Hansen /* 1493a0d14b89SPeter Zijlstra * Explicitly marked noinline such that the function tracer sees this as the 1494a0d14b89SPeter Zijlstra * page_fault entry point. 1495aa37c51bSDave Hansen */ 1496aa37c51bSDave Hansen static noinline void 1497aa37c51bSDave Hansen __do_page_fault(struct pt_regs *regs, unsigned long hw_error_code, 1498aa37c51bSDave Hansen unsigned long address) 1499aa37c51bSDave Hansen { 1500aa37c51bSDave Hansen prefetchw(¤t->mm->mmap_sem); 1501aa37c51bSDave Hansen 1502aa37c51bSDave Hansen if (unlikely(kmmio_fault(regs, address))) 1503aa37c51bSDave Hansen return; 1504aa37c51bSDave Hansen 1505aa37c51bSDave Hansen /* Was the fault on kernel-controlled part of the address space? */ 1506aa37c51bSDave Hansen if (unlikely(fault_in_kernel_space(address))) 1507aa37c51bSDave Hansen do_kern_addr_fault(regs, hw_error_code, address); 1508aa37c51bSDave Hansen else 1509aa37c51bSDave Hansen do_user_addr_fault(regs, hw_error_code, address); 1510aa37c51bSDave Hansen } 15119326638cSMasami Hiramatsu NOKPROBE_SYMBOL(__do_page_fault); 15126ba3c97aSFrederic Weisbecker 1513a0d14b89SPeter Zijlstra static __always_inline void 1514a0d14b89SPeter Zijlstra trace_page_fault_entries(struct pt_regs *regs, unsigned long error_code, 1515a0d14b89SPeter Zijlstra unsigned long address) 1516d34603b0SSeiji Aguchi { 1517a0d14b89SPeter Zijlstra if (!trace_pagefault_enabled()) 1518a0d14b89SPeter Zijlstra return; 1519a0d14b89SPeter Zijlstra 1520d34603b0SSeiji Aguchi if (user_mode(regs)) 1521d4078e23SPeter Zijlstra trace_page_fault_user(address, regs, error_code); 1522d34603b0SSeiji Aguchi else 1523d4078e23SPeter Zijlstra trace_page_fault_kernel(address, regs, error_code); 1524d34603b0SSeiji Aguchi } 1525d34603b0SSeiji Aguchi 1526a0d14b89SPeter Zijlstra dotraplinkage void 1527a0d14b89SPeter Zijlstra do_page_fault(struct pt_regs *regs, unsigned long error_code, unsigned long address) 152811a7ffb0SThomas Gleixner { 1529d4078e23SPeter Zijlstra enum ctx_state prev_state; 153025c74b10SSeiji Aguchi 153125c74b10SSeiji Aguchi prev_state = exception_enter(); 1532a0d14b89SPeter Zijlstra trace_page_fault_entries(regs, error_code, address); 15330ac09f9fSJiri Olsa __do_page_fault(regs, error_code, address); 153425c74b10SSeiji Aguchi exception_exit(prev_state); 153525c74b10SSeiji Aguchi } 153611a7ffb0SThomas Gleixner NOKPROBE_SYMBOL(do_page_fault); 1537