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 */ 11a2bcd473SIngo Molnar #include <linux/bootmem.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() */ 1950a7ca3cSSouptick Joarder #include <linux/mm_types.h> 20c61e211dSHarvey Harrison 21019132ffSDave Hansen #include <asm/cpufeature.h> /* boot_cpu_has, ... */ 22a2bcd473SIngo Molnar #include <asm/traps.h> /* dotraplinkage, ... */ 23a2bcd473SIngo Molnar #include <asm/pgalloc.h> /* pgd_*(), ... */ 24f40c3300SAndy Lutomirski #include <asm/fixmap.h> /* VSYSCALL_ADDR */ 25f40c3300SAndy Lutomirski #include <asm/vsyscall.h> /* emulate_vsyscall */ 26ba3e127eSBrian Gerst #include <asm/vm86.h> /* struct vm86 */ 27019132ffSDave Hansen #include <asm/mmu_context.h> /* vma_pkey() */ 28c61e211dSHarvey Harrison 29d34603b0SSeiji Aguchi #define CREATE_TRACE_POINTS 30d34603b0SSeiji Aguchi #include <asm/trace/exceptions.h> 31d34603b0SSeiji Aguchi 32c61e211dSHarvey Harrison /* 33b319eed0SIngo Molnar * Returns 0 if mmiotrace is disabled, or if the fault is not 34b319eed0SIngo Molnar * handled by mmiotrace: 35b814d41fSIngo Molnar */ 369326638cSMasami Hiramatsu static nokprobe_inline int 3762c9295fSMasami Hiramatsu kmmio_fault(struct pt_regs *regs, unsigned long addr) 3886069782SPekka Paalanen { 390fd0e3daSPekka Paalanen if (unlikely(is_kmmio_active())) 400fd0e3daSPekka Paalanen if (kmmio_handler(regs, addr) == 1) 410fd0e3daSPekka Paalanen return -1; 420fd0e3daSPekka Paalanen return 0; 4386069782SPekka Paalanen } 4486069782SPekka Paalanen 459326638cSMasami Hiramatsu static nokprobe_inline int kprobes_fault(struct pt_regs *regs) 46c61e211dSHarvey Harrison { 47c61e211dSHarvey Harrison int ret = 0; 48c61e211dSHarvey Harrison 49c61e211dSHarvey Harrison /* kprobe_running() needs smp_processor_id() */ 50f39b6f0eSAndy Lutomirski if (kprobes_built_in() && !user_mode(regs)) { 51c61e211dSHarvey Harrison preempt_disable(); 52c61e211dSHarvey Harrison if (kprobe_running() && kprobe_fault_handler(regs, 14)) 53c61e211dSHarvey Harrison ret = 1; 54c61e211dSHarvey Harrison preempt_enable(); 55c61e211dSHarvey Harrison } 56c61e211dSHarvey Harrison 57c61e211dSHarvey Harrison return ret; 58c61e211dSHarvey Harrison } 59c61e211dSHarvey Harrison 60c61e211dSHarvey Harrison /* 612d4a7167SIngo Molnar * Prefetch quirks: 622d4a7167SIngo Molnar * 632d4a7167SIngo Molnar * 32-bit mode: 642d4a7167SIngo Molnar * 65c61e211dSHarvey Harrison * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch. 66c61e211dSHarvey Harrison * Check that here and ignore it. 67c61e211dSHarvey Harrison * 682d4a7167SIngo Molnar * 64-bit mode: 692d4a7167SIngo Molnar * 70c61e211dSHarvey Harrison * Sometimes the CPU reports invalid exceptions on prefetch. 71c61e211dSHarvey Harrison * Check that here and ignore it. 72c61e211dSHarvey Harrison * 732d4a7167SIngo Molnar * Opcode checker based on code by Richard Brunner. 74c61e211dSHarvey Harrison */ 75107a0367SIngo Molnar static inline int 76107a0367SIngo Molnar check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr, 77107a0367SIngo Molnar unsigned char opcode, int *prefetch) 78c61e211dSHarvey Harrison { 79107a0367SIngo Molnar unsigned char instr_hi = opcode & 0xf0; 80107a0367SIngo Molnar unsigned char instr_lo = opcode & 0x0f; 81c61e211dSHarvey Harrison 82c61e211dSHarvey Harrison switch (instr_hi) { 83c61e211dSHarvey Harrison case 0x20: 84c61e211dSHarvey Harrison case 0x30: 85c61e211dSHarvey Harrison /* 86c61e211dSHarvey Harrison * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes. 87c61e211dSHarvey Harrison * In X86_64 long mode, the CPU will signal invalid 88c61e211dSHarvey Harrison * opcode if some of these prefixes are present so 89c61e211dSHarvey Harrison * X86_64 will never get here anyway 90c61e211dSHarvey Harrison */ 91107a0367SIngo Molnar return ((instr_lo & 7) == 0x6); 92c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 93c61e211dSHarvey Harrison case 0x40: 94c61e211dSHarvey Harrison /* 95c61e211dSHarvey Harrison * In AMD64 long mode 0x40..0x4F are valid REX prefixes 96c61e211dSHarvey Harrison * Need to figure out under what instruction mode the 97c61e211dSHarvey Harrison * instruction was issued. Could check the LDT for lm, 98c61e211dSHarvey Harrison * but for now it's good enough to assume that long 99c61e211dSHarvey Harrison * mode only uses well known segments or kernel. 100c61e211dSHarvey Harrison */ 101318f5a2aSAndy Lutomirski return (!user_mode(regs) || user_64bit_mode(regs)); 102c61e211dSHarvey Harrison #endif 103c61e211dSHarvey Harrison case 0x60: 104c61e211dSHarvey Harrison /* 0x64 thru 0x67 are valid prefixes in all modes. */ 105107a0367SIngo Molnar return (instr_lo & 0xC) == 0x4; 106c61e211dSHarvey Harrison case 0xF0: 107c61e211dSHarvey Harrison /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */ 108107a0367SIngo Molnar return !instr_lo || (instr_lo>>1) == 1; 109c61e211dSHarvey Harrison case 0x00: 110c61e211dSHarvey Harrison /* Prefetch instruction is 0x0F0D or 0x0F18 */ 111107a0367SIngo Molnar if (probe_kernel_address(instr, opcode)) 112107a0367SIngo Molnar return 0; 113107a0367SIngo Molnar 114107a0367SIngo Molnar *prefetch = (instr_lo == 0xF) && 115107a0367SIngo Molnar (opcode == 0x0D || opcode == 0x18); 116107a0367SIngo Molnar return 0; 117107a0367SIngo Molnar default: 118107a0367SIngo Molnar return 0; 119107a0367SIngo Molnar } 120107a0367SIngo Molnar } 121107a0367SIngo Molnar 122107a0367SIngo Molnar static int 123107a0367SIngo Molnar is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr) 124107a0367SIngo Molnar { 125107a0367SIngo Molnar unsigned char *max_instr; 126107a0367SIngo Molnar unsigned char *instr; 127107a0367SIngo Molnar int prefetch = 0; 128107a0367SIngo Molnar 129107a0367SIngo Molnar /* 130107a0367SIngo Molnar * If it was a exec (instruction fetch) fault on NX page, then 131107a0367SIngo Molnar * do not ignore the fault: 132107a0367SIngo Molnar */ 1331067f030SRicardo Neri if (error_code & X86_PF_INSTR) 134107a0367SIngo Molnar return 0; 135107a0367SIngo Molnar 136107a0367SIngo Molnar instr = (void *)convert_ip_to_linear(current, regs); 137107a0367SIngo Molnar max_instr = instr + 15; 138107a0367SIngo Molnar 139d31bf07fSAndy Lutomirski if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE_MAX) 140107a0367SIngo Molnar return 0; 141107a0367SIngo Molnar 142107a0367SIngo Molnar while (instr < max_instr) { 143107a0367SIngo Molnar unsigned char opcode; 144c61e211dSHarvey Harrison 145c61e211dSHarvey Harrison if (probe_kernel_address(instr, opcode)) 146c61e211dSHarvey Harrison break; 147107a0367SIngo Molnar 148107a0367SIngo Molnar instr++; 149107a0367SIngo Molnar 150107a0367SIngo Molnar if (!check_prefetch_opcode(regs, instr, opcode, &prefetch)) 151c61e211dSHarvey Harrison break; 152c61e211dSHarvey Harrison } 153c61e211dSHarvey Harrison return prefetch; 154c61e211dSHarvey Harrison } 155c61e211dSHarvey Harrison 156019132ffSDave Hansen /* 157019132ffSDave Hansen * A protection key fault means that the PKRU value did not allow 158019132ffSDave Hansen * access to some PTE. Userspace can figure out what PKRU was 159019132ffSDave Hansen * from the XSAVE state, and this function fills out a field in 160019132ffSDave Hansen * siginfo so userspace can discover which protection key was set 161019132ffSDave Hansen * on the PTE. 162019132ffSDave Hansen * 1631067f030SRicardo Neri * If we get here, we know that the hardware signaled a X86_PF_PK 164019132ffSDave Hansen * fault and that there was a VMA once we got in the fault 165019132ffSDave Hansen * handler. It does *not* guarantee that the VMA we find here 166019132ffSDave Hansen * was the one that we faulted on. 167019132ffSDave Hansen * 168019132ffSDave Hansen * 1. T1 : mprotect_key(foo, PAGE_SIZE, pkey=4); 169019132ffSDave Hansen * 2. T1 : set PKRU to deny access to pkey=4, touches page 170019132ffSDave Hansen * 3. T1 : faults... 171019132ffSDave Hansen * 4. T2: mprotect_key(foo, PAGE_SIZE, pkey=5); 172019132ffSDave Hansen * 5. T1 : enters fault handler, takes mmap_sem, etc... 173019132ffSDave Hansen * 6. T1 : reaches here, sees vma_pkey(vma)=5, when we really 174019132ffSDave Hansen * faulted on a pte with its pkey=4. 175019132ffSDave Hansen */ 176beacd6f7SEric W. Biederman static void fill_sig_info_pkey(int si_signo, int si_code, siginfo_t *info, 177beacd6f7SEric W. Biederman u32 *pkey) 178019132ffSDave Hansen { 179019132ffSDave Hansen /* This is effectively an #ifdef */ 180019132ffSDave Hansen if (!boot_cpu_has(X86_FEATURE_OSPKE)) 181019132ffSDave Hansen return; 182019132ffSDave Hansen 183019132ffSDave Hansen /* Fault not from Protection Keys: nothing to do */ 184beacd6f7SEric W. Biederman if ((si_code != SEGV_PKUERR) || (si_signo != SIGSEGV)) 185019132ffSDave Hansen return; 186019132ffSDave Hansen /* 187019132ffSDave Hansen * force_sig_info_fault() is called from a number of 188019132ffSDave Hansen * contexts, some of which have a VMA and some of which 1891067f030SRicardo Neri * do not. The X86_PF_PK handing happens after we have a 190019132ffSDave Hansen * valid VMA, so we should never reach this without a 191019132ffSDave Hansen * valid VMA. 192019132ffSDave Hansen */ 193a3c4fb7cSLaurent Dufour if (!pkey) { 194019132ffSDave Hansen WARN_ONCE(1, "PKU fault with no VMA passed in"); 195019132ffSDave Hansen info->si_pkey = 0; 196019132ffSDave Hansen return; 197019132ffSDave Hansen } 198019132ffSDave Hansen /* 199019132ffSDave Hansen * si_pkey should be thought of as a strong hint, but not 200019132ffSDave Hansen * absolutely guranteed to be 100% accurate because of 201019132ffSDave Hansen * the race explained above. 202019132ffSDave Hansen */ 203a3c4fb7cSLaurent Dufour info->si_pkey = *pkey; 204019132ffSDave Hansen } 205019132ffSDave Hansen 2062d4a7167SIngo Molnar static void 2072d4a7167SIngo Molnar force_sig_info_fault(int si_signo, int si_code, unsigned long address, 20840e55394SEric W. Biederman struct task_struct *tsk, u32 *pkey) 209c61e211dSHarvey Harrison { 210c61e211dSHarvey Harrison siginfo_t info; 211c61e211dSHarvey Harrison 2123eb0f519SEric W. Biederman clear_siginfo(&info); 213c61e211dSHarvey Harrison info.si_signo = si_signo; 214c61e211dSHarvey Harrison info.si_errno = 0; 215c61e211dSHarvey Harrison info.si_code = si_code; 216c61e211dSHarvey Harrison info.si_addr = (void __user *)address; 2172d4a7167SIngo Molnar 218beacd6f7SEric W. Biederman fill_sig_info_pkey(si_signo, si_code, &info, pkey); 219019132ffSDave Hansen 220c61e211dSHarvey Harrison force_sig_info(si_signo, &info, tsk); 221c61e211dSHarvey Harrison } 222c61e211dSHarvey Harrison 223f2f13a85SIngo Molnar DEFINE_SPINLOCK(pgd_lock); 224f2f13a85SIngo Molnar LIST_HEAD(pgd_list); 2252d4a7167SIngo Molnar 226f2f13a85SIngo Molnar #ifdef CONFIG_X86_32 227f2f13a85SIngo Molnar static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address) 228f2f13a85SIngo Molnar { 229f2f13a85SIngo Molnar unsigned index = pgd_index(address); 230f2f13a85SIngo Molnar pgd_t *pgd_k; 231e0c4f675SKirill A. Shutemov p4d_t *p4d, *p4d_k; 232f2f13a85SIngo Molnar pud_t *pud, *pud_k; 233f2f13a85SIngo Molnar pmd_t *pmd, *pmd_k; 234f2f13a85SIngo Molnar 235f2f13a85SIngo Molnar pgd += index; 236f2f13a85SIngo Molnar pgd_k = init_mm.pgd + index; 237f2f13a85SIngo Molnar 238f2f13a85SIngo Molnar if (!pgd_present(*pgd_k)) 239f2f13a85SIngo Molnar return NULL; 240f2f13a85SIngo Molnar 241f2f13a85SIngo Molnar /* 242f2f13a85SIngo Molnar * set_pgd(pgd, *pgd_k); here would be useless on PAE 243f2f13a85SIngo Molnar * and redundant with the set_pmd() on non-PAE. As would 244e0c4f675SKirill A. Shutemov * set_p4d/set_pud. 245f2f13a85SIngo Molnar */ 246e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, address); 247e0c4f675SKirill A. Shutemov p4d_k = p4d_offset(pgd_k, address); 248e0c4f675SKirill A. Shutemov if (!p4d_present(*p4d_k)) 249e0c4f675SKirill A. Shutemov return NULL; 250e0c4f675SKirill A. Shutemov 251e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, address); 252e0c4f675SKirill A. Shutemov pud_k = pud_offset(p4d_k, address); 253f2f13a85SIngo Molnar if (!pud_present(*pud_k)) 254f2f13a85SIngo Molnar return NULL; 255f2f13a85SIngo Molnar 256f2f13a85SIngo Molnar pmd = pmd_offset(pud, address); 257f2f13a85SIngo Molnar pmd_k = pmd_offset(pud_k, address); 258f2f13a85SIngo Molnar if (!pmd_present(*pmd_k)) 259f2f13a85SIngo Molnar return NULL; 260f2f13a85SIngo Molnar 261b8bcfe99SJeremy Fitzhardinge if (!pmd_present(*pmd)) 262f2f13a85SIngo Molnar set_pmd(pmd, *pmd_k); 263b8bcfe99SJeremy Fitzhardinge else 264f2f13a85SIngo Molnar BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k)); 265f2f13a85SIngo Molnar 266f2f13a85SIngo Molnar return pmd_k; 267f2f13a85SIngo Molnar } 268f2f13a85SIngo Molnar 269f2f13a85SIngo Molnar void vmalloc_sync_all(void) 270f2f13a85SIngo Molnar { 271f2f13a85SIngo Molnar unsigned long address; 272f2f13a85SIngo Molnar 273f2f13a85SIngo Molnar if (SHARED_KERNEL_PMD) 274f2f13a85SIngo Molnar return; 275f2f13a85SIngo Molnar 276f2f13a85SIngo Molnar for (address = VMALLOC_START & PMD_MASK; 277dc4fac84SAndy Lutomirski address >= TASK_SIZE_MAX && address < FIXADDR_TOP; 278f2f13a85SIngo Molnar address += PMD_SIZE) { 279f2f13a85SIngo Molnar struct page *page; 280f2f13a85SIngo Molnar 281a79e53d8SAndrea Arcangeli spin_lock(&pgd_lock); 282f2f13a85SIngo Molnar list_for_each_entry(page, &pgd_list, lru) { 283617d34d9SJeremy Fitzhardinge spinlock_t *pgt_lock; 284f01f7c56SBorislav Petkov pmd_t *ret; 285617d34d9SJeremy Fitzhardinge 286a79e53d8SAndrea Arcangeli /* the pgt_lock only for Xen */ 287617d34d9SJeremy Fitzhardinge pgt_lock = &pgd_page_get_mm(page)->page_table_lock; 288617d34d9SJeremy Fitzhardinge 289617d34d9SJeremy Fitzhardinge spin_lock(pgt_lock); 290617d34d9SJeremy Fitzhardinge ret = vmalloc_sync_one(page_address(page), address); 291617d34d9SJeremy Fitzhardinge spin_unlock(pgt_lock); 292617d34d9SJeremy Fitzhardinge 293617d34d9SJeremy Fitzhardinge if (!ret) 294f2f13a85SIngo Molnar break; 295f2f13a85SIngo Molnar } 296a79e53d8SAndrea Arcangeli spin_unlock(&pgd_lock); 297f2f13a85SIngo Molnar } 298f2f13a85SIngo Molnar } 299f2f13a85SIngo Molnar 300f2f13a85SIngo Molnar /* 301f2f13a85SIngo Molnar * 32-bit: 302f2f13a85SIngo Molnar * 303f2f13a85SIngo Molnar * Handle a fault on the vmalloc or module mapping area 304f2f13a85SIngo Molnar */ 3059326638cSMasami Hiramatsu static noinline int vmalloc_fault(unsigned long address) 306f2f13a85SIngo Molnar { 307f2f13a85SIngo Molnar unsigned long pgd_paddr; 308f2f13a85SIngo Molnar pmd_t *pmd_k; 309f2f13a85SIngo Molnar pte_t *pte_k; 310f2f13a85SIngo Molnar 311f2f13a85SIngo Molnar /* Make sure we are in vmalloc area: */ 312f2f13a85SIngo Molnar if (!(address >= VMALLOC_START && address < VMALLOC_END)) 313f2f13a85SIngo Molnar return -1; 314f2f13a85SIngo Molnar 315f2f13a85SIngo Molnar /* 316f2f13a85SIngo Molnar * Synchronize this task's top level page-table 317f2f13a85SIngo Molnar * with the 'reference' page table. 318f2f13a85SIngo Molnar * 319f2f13a85SIngo Molnar * Do _not_ use "current" here. We might be inside 320f2f13a85SIngo Molnar * an interrupt in the middle of a task switch.. 321f2f13a85SIngo Molnar */ 3226c690ee1SAndy Lutomirski pgd_paddr = read_cr3_pa(); 323f2f13a85SIngo Molnar pmd_k = vmalloc_sync_one(__va(pgd_paddr), address); 324f2f13a85SIngo Molnar if (!pmd_k) 325f2f13a85SIngo Molnar return -1; 326f2f13a85SIngo Molnar 32718a95521SToshi Kani if (pmd_large(*pmd_k)) 328f4eafd8bSToshi Kani return 0; 329f4eafd8bSToshi Kani 330f2f13a85SIngo Molnar pte_k = pte_offset_kernel(pmd_k, address); 331f2f13a85SIngo Molnar if (!pte_present(*pte_k)) 332f2f13a85SIngo Molnar return -1; 333f2f13a85SIngo Molnar 334f2f13a85SIngo Molnar return 0; 335f2f13a85SIngo Molnar } 3369326638cSMasami Hiramatsu NOKPROBE_SYMBOL(vmalloc_fault); 337f2f13a85SIngo Molnar 338f2f13a85SIngo Molnar /* 339f2f13a85SIngo Molnar * Did it hit the DOS screen memory VA from vm86 mode? 340f2f13a85SIngo Molnar */ 341f2f13a85SIngo Molnar static inline void 342f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address, 343f2f13a85SIngo Molnar struct task_struct *tsk) 344f2f13a85SIngo Molnar { 3459fda6a06SBrian Gerst #ifdef CONFIG_VM86 346f2f13a85SIngo Molnar unsigned long bit; 347f2f13a85SIngo Molnar 3489fda6a06SBrian Gerst if (!v8086_mode(regs) || !tsk->thread.vm86) 349f2f13a85SIngo Molnar return; 350f2f13a85SIngo Molnar 351f2f13a85SIngo Molnar bit = (address - 0xA0000) >> PAGE_SHIFT; 352f2f13a85SIngo Molnar if (bit < 32) 3539fda6a06SBrian Gerst tsk->thread.vm86->screen_bitmap |= 1 << bit; 3549fda6a06SBrian Gerst #endif 355f2f13a85SIngo Molnar } 356c61e211dSHarvey Harrison 357087975b0SAkinobu Mita static bool low_pfn(unsigned long pfn) 358087975b0SAkinobu Mita { 359087975b0SAkinobu Mita return pfn < max_low_pfn; 360087975b0SAkinobu Mita } 361087975b0SAkinobu Mita 362cae30f82SAdrian Bunk static void dump_pagetable(unsigned long address) 363c61e211dSHarvey Harrison { 3646c690ee1SAndy Lutomirski pgd_t *base = __va(read_cr3_pa()); 365087975b0SAkinobu Mita pgd_t *pgd = &base[pgd_index(address)]; 366e0c4f675SKirill A. Shutemov p4d_t *p4d; 367e0c4f675SKirill A. Shutemov pud_t *pud; 368087975b0SAkinobu Mita pmd_t *pmd; 369087975b0SAkinobu Mita pte_t *pte; 3702d4a7167SIngo Molnar 371c61e211dSHarvey Harrison #ifdef CONFIG_X86_PAE 37239e48d9bSJan Beulich pr_info("*pdpt = %016Lx ", pgd_val(*pgd)); 373087975b0SAkinobu Mita if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd)) 374087975b0SAkinobu Mita goto out; 37539e48d9bSJan Beulich #define pr_pde pr_cont 37639e48d9bSJan Beulich #else 37739e48d9bSJan Beulich #define pr_pde pr_info 378c61e211dSHarvey Harrison #endif 379e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, address); 380e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, address); 381e0c4f675SKirill A. Shutemov pmd = pmd_offset(pud, address); 38239e48d9bSJan Beulich pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd)); 38339e48d9bSJan Beulich #undef pr_pde 384c61e211dSHarvey Harrison 385c61e211dSHarvey Harrison /* 386c61e211dSHarvey Harrison * We must not directly access the pte in the highpte 387c61e211dSHarvey Harrison * case if the page table is located in highmem. 388c61e211dSHarvey Harrison * And let's rather not kmap-atomic the pte, just in case 3892d4a7167SIngo Molnar * it's allocated already: 390c61e211dSHarvey Harrison */ 391087975b0SAkinobu Mita if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd)) 392087975b0SAkinobu Mita goto out; 3932d4a7167SIngo Molnar 394087975b0SAkinobu Mita pte = pte_offset_kernel(pmd, address); 39539e48d9bSJan Beulich pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte)); 396087975b0SAkinobu Mita out: 39739e48d9bSJan Beulich pr_cont("\n"); 398f2f13a85SIngo Molnar } 399f2f13a85SIngo Molnar 400f2f13a85SIngo Molnar #else /* CONFIG_X86_64: */ 401f2f13a85SIngo Molnar 402f2f13a85SIngo Molnar void vmalloc_sync_all(void) 403f2f13a85SIngo Molnar { 4045372e155SKirill A. Shutemov sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END); 405f2f13a85SIngo Molnar } 406f2f13a85SIngo Molnar 407f2f13a85SIngo Molnar /* 408f2f13a85SIngo Molnar * 64-bit: 409f2f13a85SIngo Molnar * 410f2f13a85SIngo Molnar * Handle a fault on the vmalloc area 411f2f13a85SIngo Molnar */ 4129326638cSMasami Hiramatsu static noinline int vmalloc_fault(unsigned long address) 413f2f13a85SIngo Molnar { 414565977a3SToshi Kani pgd_t *pgd, *pgd_k; 415565977a3SToshi Kani p4d_t *p4d, *p4d_k; 416565977a3SToshi Kani pud_t *pud; 417565977a3SToshi Kani pmd_t *pmd; 418565977a3SToshi Kani pte_t *pte; 419f2f13a85SIngo Molnar 420f2f13a85SIngo Molnar /* Make sure we are in vmalloc area: */ 421f2f13a85SIngo Molnar if (!(address >= VMALLOC_START && address < VMALLOC_END)) 422f2f13a85SIngo Molnar return -1; 423f2f13a85SIngo Molnar 424ebc8827fSFrederic Weisbecker WARN_ON_ONCE(in_nmi()); 425ebc8827fSFrederic Weisbecker 426f2f13a85SIngo Molnar /* 427f2f13a85SIngo Molnar * Copy kernel mappings over when needed. This can also 428f2f13a85SIngo Molnar * happen within a race in page table update. In the later 429f2f13a85SIngo Molnar * case just flush: 430f2f13a85SIngo Molnar */ 4316c690ee1SAndy Lutomirski pgd = (pgd_t *)__va(read_cr3_pa()) + pgd_index(address); 432565977a3SToshi Kani pgd_k = pgd_offset_k(address); 433565977a3SToshi Kani if (pgd_none(*pgd_k)) 434f2f13a85SIngo Molnar return -1; 435f2f13a85SIngo Molnar 436ed7588d5SKirill A. Shutemov if (pgtable_l5_enabled()) { 4371160c277SSamu Kallio if (pgd_none(*pgd)) { 438565977a3SToshi Kani set_pgd(pgd, *pgd_k); 4391160c277SSamu Kallio arch_flush_lazy_mmu_mode(); 44036b3a772SAndy Lutomirski } else { 441565977a3SToshi Kani BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_k)); 4421160c277SSamu Kallio } 44336b3a772SAndy Lutomirski } 444f2f13a85SIngo Molnar 445b50858ceSKirill A. Shutemov /* With 4-level paging, copying happens on the p4d level. */ 446b50858ceSKirill A. Shutemov p4d = p4d_offset(pgd, address); 447565977a3SToshi Kani p4d_k = p4d_offset(pgd_k, address); 448565977a3SToshi Kani if (p4d_none(*p4d_k)) 449b50858ceSKirill A. Shutemov return -1; 450b50858ceSKirill A. Shutemov 451ed7588d5SKirill A. Shutemov if (p4d_none(*p4d) && !pgtable_l5_enabled()) { 452565977a3SToshi Kani set_p4d(p4d, *p4d_k); 453b50858ceSKirill A. Shutemov arch_flush_lazy_mmu_mode(); 454b50858ceSKirill A. Shutemov } else { 455565977a3SToshi Kani BUG_ON(p4d_pfn(*p4d) != p4d_pfn(*p4d_k)); 456b50858ceSKirill A. Shutemov } 457b50858ceSKirill A. Shutemov 45836b3a772SAndy Lutomirski BUILD_BUG_ON(CONFIG_PGTABLE_LEVELS < 4); 459f2f13a85SIngo Molnar 460b50858ceSKirill A. Shutemov pud = pud_offset(p4d, address); 461565977a3SToshi Kani if (pud_none(*pud)) 462f2f13a85SIngo Molnar return -1; 463f2f13a85SIngo Molnar 46418a95521SToshi Kani if (pud_large(*pud)) 465f4eafd8bSToshi Kani return 0; 466f4eafd8bSToshi Kani 467f2f13a85SIngo Molnar pmd = pmd_offset(pud, address); 468565977a3SToshi Kani if (pmd_none(*pmd)) 469f2f13a85SIngo Molnar return -1; 470f2f13a85SIngo Molnar 47118a95521SToshi Kani if (pmd_large(*pmd)) 472f4eafd8bSToshi Kani return 0; 473f4eafd8bSToshi Kani 474f2f13a85SIngo Molnar pte = pte_offset_kernel(pmd, address); 475565977a3SToshi Kani if (!pte_present(*pte)) 476565977a3SToshi Kani return -1; 477f2f13a85SIngo Molnar 478f2f13a85SIngo Molnar return 0; 479f2f13a85SIngo Molnar } 4809326638cSMasami Hiramatsu NOKPROBE_SYMBOL(vmalloc_fault); 481f2f13a85SIngo Molnar 482e05139f2SJan Beulich #ifdef CONFIG_CPU_SUP_AMD 483f2f13a85SIngo Molnar static const char errata93_warning[] = 484ad361c98SJoe Perches KERN_ERR 485ad361c98SJoe Perches "******* Your BIOS seems to not contain a fix for K8 errata #93\n" 486ad361c98SJoe Perches "******* Working around it, but it may cause SEGVs or burn power.\n" 487ad361c98SJoe Perches "******* Please consider a BIOS update.\n" 488ad361c98SJoe Perches "******* Disabling USB legacy in the BIOS may also help.\n"; 489e05139f2SJan Beulich #endif 490f2f13a85SIngo Molnar 491f2f13a85SIngo Molnar /* 492f2f13a85SIngo Molnar * No vm86 mode in 64-bit mode: 493f2f13a85SIngo Molnar */ 494f2f13a85SIngo Molnar static inline void 495f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address, 496f2f13a85SIngo Molnar struct task_struct *tsk) 497f2f13a85SIngo Molnar { 498f2f13a85SIngo Molnar } 499f2f13a85SIngo Molnar 500f2f13a85SIngo Molnar static int bad_address(void *p) 501f2f13a85SIngo Molnar { 502f2f13a85SIngo Molnar unsigned long dummy; 503f2f13a85SIngo Molnar 504f2f13a85SIngo Molnar return probe_kernel_address((unsigned long *)p, dummy); 505f2f13a85SIngo Molnar } 506f2f13a85SIngo Molnar 507f2f13a85SIngo Molnar static void dump_pagetable(unsigned long address) 508f2f13a85SIngo Molnar { 5096c690ee1SAndy Lutomirski pgd_t *base = __va(read_cr3_pa()); 510087975b0SAkinobu Mita pgd_t *pgd = base + pgd_index(address); 511e0c4f675SKirill A. Shutemov p4d_t *p4d; 512c61e211dSHarvey Harrison pud_t *pud; 513c61e211dSHarvey Harrison pmd_t *pmd; 514c61e211dSHarvey Harrison pte_t *pte; 515c61e211dSHarvey Harrison 5162d4a7167SIngo Molnar if (bad_address(pgd)) 5172d4a7167SIngo Molnar goto bad; 5182d4a7167SIngo Molnar 51939e48d9bSJan Beulich pr_info("PGD %lx ", pgd_val(*pgd)); 5202d4a7167SIngo Molnar 5212d4a7167SIngo Molnar if (!pgd_present(*pgd)) 5222d4a7167SIngo Molnar goto out; 523c61e211dSHarvey Harrison 524e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, address); 525e0c4f675SKirill A. Shutemov if (bad_address(p4d)) 526e0c4f675SKirill A. Shutemov goto bad; 527e0c4f675SKirill A. Shutemov 52839e48d9bSJan Beulich pr_cont("P4D %lx ", p4d_val(*p4d)); 529e0c4f675SKirill A. Shutemov if (!p4d_present(*p4d) || p4d_large(*p4d)) 530e0c4f675SKirill A. Shutemov goto out; 531e0c4f675SKirill A. Shutemov 532e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, address); 5332d4a7167SIngo Molnar if (bad_address(pud)) 5342d4a7167SIngo Molnar goto bad; 5352d4a7167SIngo Molnar 53639e48d9bSJan Beulich pr_cont("PUD %lx ", pud_val(*pud)); 537b5360222SAndi Kleen if (!pud_present(*pud) || pud_large(*pud)) 5382d4a7167SIngo Molnar goto out; 539c61e211dSHarvey Harrison 540c61e211dSHarvey Harrison pmd = pmd_offset(pud, address); 5412d4a7167SIngo Molnar if (bad_address(pmd)) 5422d4a7167SIngo Molnar goto bad; 5432d4a7167SIngo Molnar 54439e48d9bSJan Beulich pr_cont("PMD %lx ", pmd_val(*pmd)); 5452d4a7167SIngo Molnar if (!pmd_present(*pmd) || pmd_large(*pmd)) 5462d4a7167SIngo Molnar goto out; 547c61e211dSHarvey Harrison 548c61e211dSHarvey Harrison pte = pte_offset_kernel(pmd, address); 5492d4a7167SIngo Molnar if (bad_address(pte)) 5502d4a7167SIngo Molnar goto bad; 5512d4a7167SIngo Molnar 55239e48d9bSJan Beulich pr_cont("PTE %lx", pte_val(*pte)); 5532d4a7167SIngo Molnar out: 55439e48d9bSJan Beulich pr_cont("\n"); 555c61e211dSHarvey Harrison return; 556c61e211dSHarvey Harrison bad: 55739e48d9bSJan Beulich pr_info("BAD\n"); 558c61e211dSHarvey Harrison } 559c61e211dSHarvey Harrison 560f2f13a85SIngo Molnar #endif /* CONFIG_X86_64 */ 561c61e211dSHarvey Harrison 5622d4a7167SIngo Molnar /* 5632d4a7167SIngo Molnar * Workaround for K8 erratum #93 & buggy BIOS. 5642d4a7167SIngo Molnar * 5652d4a7167SIngo Molnar * BIOS SMM functions are required to use a specific workaround 5662d4a7167SIngo Molnar * to avoid corruption of the 64bit RIP register on C stepping K8. 5672d4a7167SIngo Molnar * 5682d4a7167SIngo Molnar * A lot of BIOS that didn't get tested properly miss this. 5692d4a7167SIngo Molnar * 5702d4a7167SIngo Molnar * The OS sees this as a page fault with the upper 32bits of RIP cleared. 5712d4a7167SIngo Molnar * Try to work around it here. 5722d4a7167SIngo Molnar * 5732d4a7167SIngo Molnar * Note we only handle faults in kernel here. 5742d4a7167SIngo Molnar * Does nothing on 32-bit. 575c61e211dSHarvey Harrison */ 576c61e211dSHarvey Harrison static int is_errata93(struct pt_regs *regs, unsigned long address) 577c61e211dSHarvey Harrison { 578e05139f2SJan Beulich #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD) 579e05139f2SJan Beulich if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD 580e05139f2SJan Beulich || boot_cpu_data.x86 != 0xf) 581e05139f2SJan Beulich return 0; 582e05139f2SJan Beulich 583c61e211dSHarvey Harrison if (address != regs->ip) 584c61e211dSHarvey Harrison return 0; 5852d4a7167SIngo Molnar 586c61e211dSHarvey Harrison if ((address >> 32) != 0) 587c61e211dSHarvey Harrison return 0; 5882d4a7167SIngo Molnar 589c61e211dSHarvey Harrison address |= 0xffffffffUL << 32; 590c61e211dSHarvey Harrison if ((address >= (u64)_stext && address <= (u64)_etext) || 591c61e211dSHarvey Harrison (address >= MODULES_VADDR && address <= MODULES_END)) { 592a454ab31SIngo Molnar printk_once(errata93_warning); 593c61e211dSHarvey Harrison regs->ip = address; 594c61e211dSHarvey Harrison return 1; 595c61e211dSHarvey Harrison } 596c61e211dSHarvey Harrison #endif 597c61e211dSHarvey Harrison return 0; 598c61e211dSHarvey Harrison } 599c61e211dSHarvey Harrison 600c61e211dSHarvey Harrison /* 6012d4a7167SIngo Molnar * Work around K8 erratum #100 K8 in compat mode occasionally jumps 6022d4a7167SIngo Molnar * to illegal addresses >4GB. 6032d4a7167SIngo Molnar * 6042d4a7167SIngo Molnar * We catch this in the page fault handler because these addresses 6052d4a7167SIngo Molnar * are not reachable. Just detect this case and return. Any code 606c61e211dSHarvey Harrison * segment in LDT is compatibility mode. 607c61e211dSHarvey Harrison */ 608c61e211dSHarvey Harrison static int is_errata100(struct pt_regs *regs, unsigned long address) 609c61e211dSHarvey Harrison { 610c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 6112d4a7167SIngo Molnar if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32)) 612c61e211dSHarvey Harrison return 1; 613c61e211dSHarvey Harrison #endif 614c61e211dSHarvey Harrison return 0; 615c61e211dSHarvey Harrison } 616c61e211dSHarvey Harrison 617c61e211dSHarvey Harrison static int is_f00f_bug(struct pt_regs *regs, unsigned long address) 618c61e211dSHarvey Harrison { 619c61e211dSHarvey Harrison #ifdef CONFIG_X86_F00F_BUG 620c61e211dSHarvey Harrison unsigned long nr; 6212d4a7167SIngo Molnar 622c61e211dSHarvey Harrison /* 6232d4a7167SIngo Molnar * Pentium F0 0F C7 C8 bug workaround: 624c61e211dSHarvey Harrison */ 625e2604b49SBorislav Petkov if (boot_cpu_has_bug(X86_BUG_F00F)) { 626c61e211dSHarvey Harrison nr = (address - idt_descr.address) >> 3; 627c61e211dSHarvey Harrison 628c61e211dSHarvey Harrison if (nr == 6) { 629c61e211dSHarvey Harrison do_invalid_op(regs, 0); 630c61e211dSHarvey Harrison return 1; 631c61e211dSHarvey Harrison } 632c61e211dSHarvey Harrison } 633c61e211dSHarvey Harrison #endif 634c61e211dSHarvey Harrison return 0; 635c61e211dSHarvey Harrison } 636c61e211dSHarvey Harrison 6372d4a7167SIngo Molnar static void 6382d4a7167SIngo Molnar show_fault_oops(struct pt_regs *regs, unsigned long error_code, 639c61e211dSHarvey Harrison unsigned long address) 640c61e211dSHarvey Harrison { 641c61e211dSHarvey Harrison if (!oops_may_print()) 642c61e211dSHarvey Harrison return; 643c61e211dSHarvey Harrison 6441067f030SRicardo Neri if (error_code & X86_PF_INSTR) { 64593809be8SHarvey Harrison unsigned int level; 646426e34ccSMatt Fleming pgd_t *pgd; 647426e34ccSMatt Fleming pte_t *pte; 6482d4a7167SIngo Molnar 6496c690ee1SAndy Lutomirski pgd = __va(read_cr3_pa()); 650426e34ccSMatt Fleming pgd += pgd_index(address); 651426e34ccSMatt Fleming 652426e34ccSMatt Fleming pte = lookup_address_in_pgd(pgd, address, &level); 653c61e211dSHarvey Harrison 6548f766149SIngo Molnar if (pte && pte_present(*pte) && !pte_exec(*pte)) 655d79d0d8aSDmitry Vyukov pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n", 656d79d0d8aSDmitry Vyukov from_kuid(&init_user_ns, current_uid())); 657eff50c34SJiri Kosina if (pte && pte_present(*pte) && pte_exec(*pte) && 658eff50c34SJiri Kosina (pgd_flags(*pgd) & _PAGE_USER) && 6591e02ce4cSAndy Lutomirski (__read_cr4() & X86_CR4_SMEP)) 660d79d0d8aSDmitry Vyukov pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n", 661d79d0d8aSDmitry Vyukov from_kuid(&init_user_ns, current_uid())); 662c61e211dSHarvey Harrison } 663fd40d6e3SHarvey Harrison 6644188f063SDmitry Vyukov pr_alert("BUG: unable to handle kernel %s at %px\n", 6654188f063SDmitry Vyukov address < PAGE_SIZE ? "NULL pointer dereference" : "paging request", 6664188f063SDmitry Vyukov (void *)address); 6672d4a7167SIngo Molnar 668c61e211dSHarvey Harrison dump_pagetable(address); 669c61e211dSHarvey Harrison } 670c61e211dSHarvey Harrison 6712d4a7167SIngo Molnar static noinline void 6722d4a7167SIngo Molnar pgtable_bad(struct pt_regs *regs, unsigned long error_code, 6732d4a7167SIngo Molnar unsigned long address) 674c61e211dSHarvey Harrison { 6752d4a7167SIngo Molnar struct task_struct *tsk; 6762d4a7167SIngo Molnar unsigned long flags; 6772d4a7167SIngo Molnar int sig; 6782d4a7167SIngo Molnar 6792d4a7167SIngo Molnar flags = oops_begin(); 6802d4a7167SIngo Molnar tsk = current; 6812d4a7167SIngo Molnar sig = SIGKILL; 682c61e211dSHarvey Harrison 683c61e211dSHarvey Harrison printk(KERN_ALERT "%s: Corrupted page table at address %lx\n", 68492181f19SNick Piggin tsk->comm, address); 685c61e211dSHarvey Harrison dump_pagetable(address); 6862d4a7167SIngo Molnar 687c61e211dSHarvey Harrison tsk->thread.cr2 = address; 68851e7dc70SSrikar Dronamraju tsk->thread.trap_nr = X86_TRAP_PF; 689c61e211dSHarvey Harrison tsk->thread.error_code = error_code; 6902d4a7167SIngo Molnar 691c61e211dSHarvey Harrison if (__die("Bad pagetable", regs, error_code)) 692874d93d1SAlexander van Heukelum sig = 0; 6932d4a7167SIngo Molnar 694874d93d1SAlexander van Heukelum oops_end(flags, regs, sig); 695c61e211dSHarvey Harrison } 696c61e211dSHarvey Harrison 6972d4a7167SIngo Molnar static noinline void 6982d4a7167SIngo Molnar no_context(struct pt_regs *regs, unsigned long error_code, 6994fc34901SAndy Lutomirski unsigned long address, int signal, int si_code) 70092181f19SNick Piggin { 70192181f19SNick Piggin struct task_struct *tsk = current; 70292181f19SNick Piggin unsigned long flags; 70392181f19SNick Piggin int sig; 70492181f19SNick Piggin 70592181f19SNick Piggin /* Are we prepared to handle this kernel fault? */ 706548acf19STony Luck if (fixup_exception(regs, X86_TRAP_PF)) { 707c026b359SPeter Zijlstra /* 708c026b359SPeter Zijlstra * Any interrupt that takes a fault gets the fixup. This makes 709c026b359SPeter Zijlstra * the below recursive fault logic only apply to a faults from 710c026b359SPeter Zijlstra * task context. 711c026b359SPeter Zijlstra */ 712c026b359SPeter Zijlstra if (in_interrupt()) 713c026b359SPeter Zijlstra return; 714c026b359SPeter Zijlstra 715c026b359SPeter Zijlstra /* 716c026b359SPeter Zijlstra * Per the above we're !in_interrupt(), aka. task context. 717c026b359SPeter Zijlstra * 718c026b359SPeter Zijlstra * In this case we need to make sure we're not recursively 719c026b359SPeter Zijlstra * faulting through the emulate_vsyscall() logic. 720c026b359SPeter Zijlstra */ 7212a53ccbcSIngo Molnar if (current->thread.sig_on_uaccess_err && signal) { 72251e7dc70SSrikar Dronamraju tsk->thread.trap_nr = X86_TRAP_PF; 7231067f030SRicardo Neri tsk->thread.error_code = error_code | X86_PF_USER; 7244fc34901SAndy Lutomirski tsk->thread.cr2 = address; 7254fc34901SAndy Lutomirski 7264fc34901SAndy Lutomirski /* XXX: hwpoison faults will set the wrong code. */ 7277b2d0dbaSDave Hansen force_sig_info_fault(signal, si_code, address, 72840e55394SEric W. Biederman tsk, NULL); 7294fc34901SAndy Lutomirski } 730c026b359SPeter Zijlstra 731c026b359SPeter Zijlstra /* 732c026b359SPeter Zijlstra * Barring that, we can do the fixup and be happy. 733c026b359SPeter Zijlstra */ 73492181f19SNick Piggin return; 7354fc34901SAndy Lutomirski } 73692181f19SNick Piggin 7376271cfdfSAndy Lutomirski #ifdef CONFIG_VMAP_STACK 7386271cfdfSAndy Lutomirski /* 7396271cfdfSAndy Lutomirski * Stack overflow? During boot, we can fault near the initial 7406271cfdfSAndy Lutomirski * stack in the direct map, but that's not an overflow -- check 7416271cfdfSAndy Lutomirski * that we're in vmalloc space to avoid this. 7426271cfdfSAndy Lutomirski */ 7436271cfdfSAndy Lutomirski if (is_vmalloc_addr((void *)address) && 7446271cfdfSAndy Lutomirski (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) || 7456271cfdfSAndy Lutomirski address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) { 7466271cfdfSAndy Lutomirski unsigned long stack = this_cpu_read(orig_ist.ist[DOUBLEFAULT_STACK]) - sizeof(void *); 7476271cfdfSAndy Lutomirski /* 7486271cfdfSAndy Lutomirski * We're likely to be running with very little stack space 7496271cfdfSAndy Lutomirski * left. It's plausible that we'd hit this condition but 7506271cfdfSAndy Lutomirski * double-fault even before we get this far, in which case 7516271cfdfSAndy Lutomirski * we're fine: the double-fault handler will deal with it. 7526271cfdfSAndy Lutomirski * 7536271cfdfSAndy Lutomirski * We don't want to make it all the way into the oops code 7546271cfdfSAndy Lutomirski * and then double-fault, though, because we're likely to 7556271cfdfSAndy Lutomirski * break the console driver and lose most of the stack dump. 7566271cfdfSAndy Lutomirski */ 7576271cfdfSAndy Lutomirski asm volatile ("movq %[stack], %%rsp\n\t" 7586271cfdfSAndy Lutomirski "call handle_stack_overflow\n\t" 7596271cfdfSAndy Lutomirski "1: jmp 1b" 760f5caf621SJosh Poimboeuf : ASM_CALL_CONSTRAINT 7616271cfdfSAndy Lutomirski : "D" ("kernel stack overflow (page fault)"), 7626271cfdfSAndy Lutomirski "S" (regs), "d" (address), 7636271cfdfSAndy Lutomirski [stack] "rm" (stack)); 7646271cfdfSAndy Lutomirski unreachable(); 7656271cfdfSAndy Lutomirski } 7666271cfdfSAndy Lutomirski #endif 7676271cfdfSAndy Lutomirski 76892181f19SNick Piggin /* 7692d4a7167SIngo Molnar * 32-bit: 7702d4a7167SIngo Molnar * 77192181f19SNick Piggin * Valid to do another page fault here, because if this fault 77292181f19SNick Piggin * had been triggered by is_prefetch fixup_exception would have 77392181f19SNick Piggin * handled it. 77492181f19SNick Piggin * 7752d4a7167SIngo Molnar * 64-bit: 7762d4a7167SIngo Molnar * 77792181f19SNick Piggin * Hall of shame of CPU/BIOS bugs. 77892181f19SNick Piggin */ 77992181f19SNick Piggin if (is_prefetch(regs, error_code, address)) 78092181f19SNick Piggin return; 78192181f19SNick Piggin 78292181f19SNick Piggin if (is_errata93(regs, address)) 78392181f19SNick Piggin return; 78492181f19SNick Piggin 78592181f19SNick Piggin /* 78692181f19SNick Piggin * Oops. The kernel tried to access some bad page. We'll have to 7872d4a7167SIngo Molnar * terminate things with extreme prejudice: 78892181f19SNick Piggin */ 78992181f19SNick Piggin flags = oops_begin(); 79092181f19SNick Piggin 79192181f19SNick Piggin show_fault_oops(regs, error_code, address); 79292181f19SNick Piggin 793a70857e4SAaron Tomlin if (task_stack_end_corrupted(tsk)) 794b0f4c4b3SPrarit Bhargava printk(KERN_EMERG "Thread overran stack, or stack corrupted\n"); 79519803078SIngo Molnar 79692181f19SNick Piggin tsk->thread.cr2 = address; 79751e7dc70SSrikar Dronamraju tsk->thread.trap_nr = X86_TRAP_PF; 79892181f19SNick Piggin tsk->thread.error_code = error_code; 79992181f19SNick Piggin 80092181f19SNick Piggin sig = SIGKILL; 80192181f19SNick Piggin if (__die("Oops", regs, error_code)) 80292181f19SNick Piggin sig = 0; 8032d4a7167SIngo Molnar 80492181f19SNick Piggin /* Executive summary in case the body of the oops scrolled away */ 805b0f4c4b3SPrarit Bhargava printk(KERN_DEFAULT "CR2: %016lx\n", address); 8062d4a7167SIngo Molnar 80792181f19SNick Piggin oops_end(flags, regs, sig); 80892181f19SNick Piggin } 80992181f19SNick Piggin 8102d4a7167SIngo Molnar /* 8112d4a7167SIngo Molnar * Print out info about fatal segfaults, if the show_unhandled_signals 8122d4a7167SIngo Molnar * sysctl is set: 8132d4a7167SIngo Molnar */ 8142d4a7167SIngo Molnar static inline void 8152d4a7167SIngo Molnar show_signal_msg(struct pt_regs *regs, unsigned long error_code, 8162d4a7167SIngo Molnar unsigned long address, struct task_struct *tsk) 8172d4a7167SIngo Molnar { 818ba54d856SBorislav Petkov const char *loglvl = task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG; 819ba54d856SBorislav Petkov 8202d4a7167SIngo Molnar if (!unhandled_signal(tsk, SIGSEGV)) 8212d4a7167SIngo Molnar return; 8222d4a7167SIngo Molnar 8232d4a7167SIngo Molnar if (!printk_ratelimit()) 8242d4a7167SIngo Molnar return; 8252d4a7167SIngo Molnar 82610a7e9d8SKees Cook printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx", 827ba54d856SBorislav Petkov loglvl, tsk->comm, task_pid_nr(tsk), address, 8282d4a7167SIngo Molnar (void *)regs->ip, (void *)regs->sp, error_code); 8292d4a7167SIngo Molnar 8302d4a7167SIngo Molnar print_vma_addr(KERN_CONT " in ", regs->ip); 8312d4a7167SIngo Molnar 8322d4a7167SIngo Molnar printk(KERN_CONT "\n"); 833ba54d856SBorislav Petkov 834ba54d856SBorislav Petkov show_opcodes((u8 *)regs->ip, loglvl); 8352d4a7167SIngo Molnar } 8362d4a7167SIngo Molnar 8372d4a7167SIngo Molnar static void 8382d4a7167SIngo Molnar __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 839a3c4fb7cSLaurent Dufour unsigned long address, u32 *pkey, int si_code) 84092181f19SNick Piggin { 84192181f19SNick Piggin struct task_struct *tsk = current; 84292181f19SNick Piggin 84392181f19SNick Piggin /* User mode accesses just cause a SIGSEGV */ 8441067f030SRicardo Neri if (error_code & X86_PF_USER) { 84592181f19SNick Piggin /* 8462d4a7167SIngo Molnar * It's possible to have interrupts off here: 84792181f19SNick Piggin */ 84892181f19SNick Piggin local_irq_enable(); 84992181f19SNick Piggin 85092181f19SNick Piggin /* 85192181f19SNick Piggin * Valid to do another page fault here because this one came 8522d4a7167SIngo Molnar * from user space: 85392181f19SNick Piggin */ 85492181f19SNick Piggin if (is_prefetch(regs, error_code, address)) 85592181f19SNick Piggin return; 85692181f19SNick Piggin 85792181f19SNick Piggin if (is_errata100(regs, address)) 85892181f19SNick Piggin return; 85992181f19SNick Piggin 8603ae36655SAndy Lutomirski #ifdef CONFIG_X86_64 8613ae36655SAndy Lutomirski /* 8623ae36655SAndy Lutomirski * Instruction fetch faults in the vsyscall page might need 8633ae36655SAndy Lutomirski * emulation. 8643ae36655SAndy Lutomirski */ 8651067f030SRicardo Neri if (unlikely((error_code & X86_PF_INSTR) && 866f40c3300SAndy Lutomirski ((address & ~0xfff) == VSYSCALL_ADDR))) { 8673ae36655SAndy Lutomirski if (emulate_vsyscall(regs, address)) 8683ae36655SAndy Lutomirski return; 8693ae36655SAndy Lutomirski } 8703ae36655SAndy Lutomirski #endif 871dc4fac84SAndy Lutomirski 872dc4fac84SAndy Lutomirski /* 873dc4fac84SAndy Lutomirski * To avoid leaking information about the kernel page table 874dc4fac84SAndy Lutomirski * layout, pretend that user-mode accesses to kernel addresses 875dc4fac84SAndy Lutomirski * are always protection faults. 876dc4fac84SAndy Lutomirski */ 877dc4fac84SAndy Lutomirski if (address >= TASK_SIZE_MAX) 8781067f030SRicardo Neri error_code |= X86_PF_PROT; 8793ae36655SAndy Lutomirski 880e575a86fSKees Cook if (likely(show_unhandled_signals)) 8812d4a7167SIngo Molnar show_signal_msg(regs, error_code, address, tsk); 88292181f19SNick Piggin 88392181f19SNick Piggin tsk->thread.cr2 = address; 884e575a86fSKees Cook tsk->thread.error_code = error_code; 88551e7dc70SSrikar Dronamraju tsk->thread.trap_nr = X86_TRAP_PF; 8862d4a7167SIngo Molnar 88740e55394SEric W. Biederman force_sig_info_fault(SIGSEGV, si_code, address, tsk, pkey); 8882d4a7167SIngo Molnar 88992181f19SNick Piggin return; 89092181f19SNick Piggin } 89192181f19SNick Piggin 89292181f19SNick Piggin if (is_f00f_bug(regs, address)) 89392181f19SNick Piggin return; 89492181f19SNick Piggin 8954fc34901SAndy Lutomirski no_context(regs, error_code, address, SIGSEGV, si_code); 89692181f19SNick Piggin } 89792181f19SNick Piggin 8982d4a7167SIngo Molnar static noinline void 8992d4a7167SIngo Molnar bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 900*768fd9c6SEric W. Biederman unsigned long address) 90192181f19SNick Piggin { 902*768fd9c6SEric W. Biederman __bad_area_nosemaphore(regs, error_code, address, NULL, SEGV_MAPERR); 90392181f19SNick Piggin } 90492181f19SNick Piggin 9052d4a7167SIngo Molnar static void 9062d4a7167SIngo Molnar __bad_area(struct pt_regs *regs, unsigned long error_code, 9077b2d0dbaSDave Hansen unsigned long address, struct vm_area_struct *vma, int si_code) 90892181f19SNick Piggin { 90992181f19SNick Piggin struct mm_struct *mm = current->mm; 910a3c4fb7cSLaurent Dufour u32 pkey; 911a3c4fb7cSLaurent Dufour 912a3c4fb7cSLaurent Dufour if (vma) 913a3c4fb7cSLaurent Dufour pkey = vma_pkey(vma); 91492181f19SNick Piggin 91592181f19SNick Piggin /* 91692181f19SNick Piggin * Something tried to access memory that isn't in our memory map.. 91792181f19SNick Piggin * Fix it, but check if it's kernel or user first.. 91892181f19SNick Piggin */ 91992181f19SNick Piggin up_read(&mm->mmap_sem); 92092181f19SNick Piggin 921a3c4fb7cSLaurent Dufour __bad_area_nosemaphore(regs, error_code, address, 922a3c4fb7cSLaurent Dufour (vma) ? &pkey : NULL, si_code); 92392181f19SNick Piggin } 92492181f19SNick Piggin 9252d4a7167SIngo Molnar static noinline void 9262d4a7167SIngo Molnar bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address) 92792181f19SNick Piggin { 9287b2d0dbaSDave Hansen __bad_area(regs, error_code, address, NULL, SEGV_MAPERR); 92992181f19SNick Piggin } 93092181f19SNick Piggin 93133a709b2SDave Hansen static inline bool bad_area_access_from_pkeys(unsigned long error_code, 93233a709b2SDave Hansen struct vm_area_struct *vma) 93333a709b2SDave Hansen { 93407f146f5SDave Hansen /* This code is always called on the current mm */ 93507f146f5SDave Hansen bool foreign = false; 93607f146f5SDave Hansen 93733a709b2SDave Hansen if (!boot_cpu_has(X86_FEATURE_OSPKE)) 93833a709b2SDave Hansen return false; 9391067f030SRicardo Neri if (error_code & X86_PF_PK) 94033a709b2SDave Hansen return true; 94107f146f5SDave Hansen /* this checks permission keys on the VMA: */ 9421067f030SRicardo Neri if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE), 9431067f030SRicardo Neri (error_code & X86_PF_INSTR), foreign)) 94407f146f5SDave Hansen return true; 94533a709b2SDave Hansen return false; 94692181f19SNick Piggin } 94792181f19SNick Piggin 9482d4a7167SIngo Molnar static noinline void 9492d4a7167SIngo Molnar bad_area_access_error(struct pt_regs *regs, unsigned long error_code, 9507b2d0dbaSDave Hansen unsigned long address, struct vm_area_struct *vma) 95192181f19SNick Piggin { 952019132ffSDave Hansen /* 953019132ffSDave Hansen * This OSPKE check is not strictly necessary at runtime. 954019132ffSDave Hansen * But, doing it this way allows compiler optimizations 955019132ffSDave Hansen * if pkeys are compiled out. 956019132ffSDave Hansen */ 95733a709b2SDave Hansen if (bad_area_access_from_pkeys(error_code, vma)) 958019132ffSDave Hansen __bad_area(regs, error_code, address, vma, SEGV_PKUERR); 959019132ffSDave Hansen else 9607b2d0dbaSDave Hansen __bad_area(regs, error_code, address, vma, SEGV_ACCERR); 96192181f19SNick Piggin } 96292181f19SNick Piggin 9632d4a7167SIngo Molnar static void 964a6e04aa9SAndi Kleen do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address, 965a3c4fb7cSLaurent Dufour u32 *pkey, unsigned int fault) 96692181f19SNick Piggin { 96792181f19SNick Piggin struct task_struct *tsk = current; 96892181f19SNick Piggin 9692d4a7167SIngo Molnar /* Kernel mode? Handle exceptions or die: */ 9701067f030SRicardo Neri if (!(error_code & X86_PF_USER)) { 9714fc34901SAndy Lutomirski no_context(regs, error_code, address, SIGBUS, BUS_ADRERR); 97296054569SLinus Torvalds return; 97396054569SLinus Torvalds } 9742d4a7167SIngo Molnar 975cd1b68f0SIngo Molnar /* User-space => ok to do another page fault: */ 97692181f19SNick Piggin if (is_prefetch(regs, error_code, address)) 97792181f19SNick Piggin return; 9782d4a7167SIngo Molnar 97992181f19SNick Piggin tsk->thread.cr2 = address; 98092181f19SNick Piggin tsk->thread.error_code = error_code; 98151e7dc70SSrikar Dronamraju tsk->thread.trap_nr = X86_TRAP_PF; 9822d4a7167SIngo Molnar 983a6e04aa9SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE 984f672b49bSAndi Kleen if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) { 98540e55394SEric W. Biederman unsigned lsb = 0; 98640e55394SEric W. Biederman 98740e55394SEric W. Biederman pr_err( 988a6e04aa9SAndi Kleen "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n", 989a6e04aa9SAndi Kleen tsk->comm, tsk->pid, address); 99040e55394SEric W. Biederman if (fault & VM_FAULT_HWPOISON_LARGE) 99140e55394SEric W. Biederman lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault)); 99240e55394SEric W. Biederman if (fault & VM_FAULT_HWPOISON) 99340e55394SEric W. Biederman lsb = PAGE_SHIFT; 99440e55394SEric W. Biederman force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb, tsk); 99540e55394SEric W. Biederman return; 996a6e04aa9SAndi Kleen } 997a6e04aa9SAndi Kleen #endif 99840e55394SEric W. Biederman force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk, pkey); 99992181f19SNick Piggin } 100092181f19SNick Piggin 10013a13c4d7SJohannes Weiner static noinline void 10022d4a7167SIngo Molnar mm_fault_error(struct pt_regs *regs, unsigned long error_code, 100350a7ca3cSSouptick Joarder unsigned long address, u32 *pkey, vm_fault_t fault) 100492181f19SNick Piggin { 10051067f030SRicardo Neri if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) { 10064fc34901SAndy Lutomirski no_context(regs, error_code, address, 0, 0); 10073a13c4d7SJohannes Weiner return; 1008b80ef10eSKOSAKI Motohiro } 1009b80ef10eSKOSAKI Motohiro 10102d4a7167SIngo Molnar if (fault & VM_FAULT_OOM) { 1011f8626854SAndrey Vagin /* Kernel mode? Handle exceptions or die: */ 10121067f030SRicardo Neri if (!(error_code & X86_PF_USER)) { 10134fc34901SAndy Lutomirski no_context(regs, error_code, address, 10144fc34901SAndy Lutomirski SIGSEGV, SEGV_MAPERR); 10153a13c4d7SJohannes Weiner return; 1016f8626854SAndrey Vagin } 1017f8626854SAndrey Vagin 1018c2d23f91SDavid Rientjes /* 1019c2d23f91SDavid Rientjes * We ran out of memory, call the OOM killer, and return the 1020c2d23f91SDavid Rientjes * userspace (which will retry the fault, or kill us if we got 1021c2d23f91SDavid Rientjes * oom-killed): 1022c2d23f91SDavid Rientjes */ 1023c2d23f91SDavid Rientjes pagefault_out_of_memory(); 10242d4a7167SIngo Molnar } else { 1025f672b49bSAndi Kleen if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON| 1026f672b49bSAndi Kleen VM_FAULT_HWPOISON_LARGE)) 1027a3c4fb7cSLaurent Dufour do_sigbus(regs, error_code, address, pkey, fault); 102833692f27SLinus Torvalds else if (fault & VM_FAULT_SIGSEGV) 1029*768fd9c6SEric W. Biederman bad_area_nosemaphore(regs, error_code, address); 103092181f19SNick Piggin else 103192181f19SNick Piggin BUG(); 103292181f19SNick Piggin } 10332d4a7167SIngo Molnar } 103492181f19SNick Piggin 1035d8b57bb7SThomas Gleixner static int spurious_fault_check(unsigned long error_code, pte_t *pte) 1036d8b57bb7SThomas Gleixner { 10371067f030SRicardo Neri if ((error_code & X86_PF_WRITE) && !pte_write(*pte)) 1038d8b57bb7SThomas Gleixner return 0; 10392d4a7167SIngo Molnar 10401067f030SRicardo Neri if ((error_code & X86_PF_INSTR) && !pte_exec(*pte)) 1041d8b57bb7SThomas Gleixner return 0; 1042b3ecd515SDave Hansen /* 1043b3ecd515SDave Hansen * Note: We do not do lazy flushing on protection key 10441067f030SRicardo Neri * changes, so no spurious fault will ever set X86_PF_PK. 1045b3ecd515SDave Hansen */ 10461067f030SRicardo Neri if ((error_code & X86_PF_PK)) 1047b3ecd515SDave Hansen return 1; 1048d8b57bb7SThomas Gleixner 1049d8b57bb7SThomas Gleixner return 1; 1050d8b57bb7SThomas Gleixner } 1051d8b57bb7SThomas Gleixner 1052c61e211dSHarvey Harrison /* 10532d4a7167SIngo Molnar * Handle a spurious fault caused by a stale TLB entry. 10542d4a7167SIngo Molnar * 10552d4a7167SIngo Molnar * This allows us to lazily refresh the TLB when increasing the 10562d4a7167SIngo Molnar * permissions of a kernel page (RO -> RW or NX -> X). Doing it 10572d4a7167SIngo Molnar * eagerly is very expensive since that implies doing a full 10582d4a7167SIngo Molnar * cross-processor TLB flush, even if no stale TLB entries exist 10592d4a7167SIngo Molnar * on other processors. 10602d4a7167SIngo Molnar * 106131668511SDavid Vrabel * Spurious faults may only occur if the TLB contains an entry with 106231668511SDavid Vrabel * fewer permission than the page table entry. Non-present (P = 0) 106331668511SDavid Vrabel * and reserved bit (R = 1) faults are never spurious. 106431668511SDavid Vrabel * 10655b727a3bSJeremy Fitzhardinge * There are no security implications to leaving a stale TLB when 10665b727a3bSJeremy Fitzhardinge * increasing the permissions on a page. 106731668511SDavid Vrabel * 106831668511SDavid Vrabel * Returns non-zero if a spurious fault was handled, zero otherwise. 106931668511SDavid Vrabel * 107031668511SDavid Vrabel * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3 107131668511SDavid Vrabel * (Optional Invalidation). 10725b727a3bSJeremy Fitzhardinge */ 10739326638cSMasami Hiramatsu static noinline int 10742d4a7167SIngo Molnar spurious_fault(unsigned long error_code, unsigned long address) 10755b727a3bSJeremy Fitzhardinge { 10765b727a3bSJeremy Fitzhardinge pgd_t *pgd; 1077e0c4f675SKirill A. Shutemov p4d_t *p4d; 10785b727a3bSJeremy Fitzhardinge pud_t *pud; 10795b727a3bSJeremy Fitzhardinge pmd_t *pmd; 10805b727a3bSJeremy Fitzhardinge pte_t *pte; 10813c3e5694SSteven Rostedt int ret; 10825b727a3bSJeremy Fitzhardinge 108331668511SDavid Vrabel /* 108431668511SDavid Vrabel * Only writes to RO or instruction fetches from NX may cause 108531668511SDavid Vrabel * spurious faults. 108631668511SDavid Vrabel * 108731668511SDavid Vrabel * These could be from user or supervisor accesses but the TLB 108831668511SDavid Vrabel * is only lazily flushed after a kernel mapping protection 108931668511SDavid Vrabel * change, so user accesses are not expected to cause spurious 109031668511SDavid Vrabel * faults. 109131668511SDavid Vrabel */ 10921067f030SRicardo Neri if (error_code != (X86_PF_WRITE | X86_PF_PROT) && 10931067f030SRicardo Neri error_code != (X86_PF_INSTR | X86_PF_PROT)) 10945b727a3bSJeremy Fitzhardinge return 0; 10955b727a3bSJeremy Fitzhardinge 10965b727a3bSJeremy Fitzhardinge pgd = init_mm.pgd + pgd_index(address); 10975b727a3bSJeremy Fitzhardinge if (!pgd_present(*pgd)) 10985b727a3bSJeremy Fitzhardinge return 0; 10995b727a3bSJeremy Fitzhardinge 1100e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, address); 1101e0c4f675SKirill A. Shutemov if (!p4d_present(*p4d)) 1102e0c4f675SKirill A. Shutemov return 0; 1103e0c4f675SKirill A. Shutemov 1104e0c4f675SKirill A. Shutemov if (p4d_large(*p4d)) 1105e0c4f675SKirill A. Shutemov return spurious_fault_check(error_code, (pte_t *) p4d); 1106e0c4f675SKirill A. Shutemov 1107e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, address); 11085b727a3bSJeremy Fitzhardinge if (!pud_present(*pud)) 11095b727a3bSJeremy Fitzhardinge return 0; 11105b727a3bSJeremy Fitzhardinge 1111d8b57bb7SThomas Gleixner if (pud_large(*pud)) 1112d8b57bb7SThomas Gleixner return spurious_fault_check(error_code, (pte_t *) pud); 1113d8b57bb7SThomas Gleixner 11145b727a3bSJeremy Fitzhardinge pmd = pmd_offset(pud, address); 11155b727a3bSJeremy Fitzhardinge if (!pmd_present(*pmd)) 11165b727a3bSJeremy Fitzhardinge return 0; 11175b727a3bSJeremy Fitzhardinge 1118d8b57bb7SThomas Gleixner if (pmd_large(*pmd)) 1119d8b57bb7SThomas Gleixner return spurious_fault_check(error_code, (pte_t *) pmd); 1120d8b57bb7SThomas Gleixner 11215b727a3bSJeremy Fitzhardinge pte = pte_offset_kernel(pmd, address); 1122954f8571SAndrea Arcangeli if (!pte_present(*pte)) 11235b727a3bSJeremy Fitzhardinge return 0; 11245b727a3bSJeremy Fitzhardinge 11253c3e5694SSteven Rostedt ret = spurious_fault_check(error_code, pte); 11263c3e5694SSteven Rostedt if (!ret) 11273c3e5694SSteven Rostedt return 0; 11283c3e5694SSteven Rostedt 11293c3e5694SSteven Rostedt /* 11302d4a7167SIngo Molnar * Make sure we have permissions in PMD. 11312d4a7167SIngo Molnar * If not, then there's a bug in the page tables: 11323c3e5694SSteven Rostedt */ 11333c3e5694SSteven Rostedt ret = spurious_fault_check(error_code, (pte_t *) pmd); 11343c3e5694SSteven Rostedt WARN_ONCE(!ret, "PMD has incorrect permission bits\n"); 11352d4a7167SIngo Molnar 11363c3e5694SSteven Rostedt return ret; 11375b727a3bSJeremy Fitzhardinge } 11389326638cSMasami Hiramatsu NOKPROBE_SYMBOL(spurious_fault); 11395b727a3bSJeremy Fitzhardinge 1140c61e211dSHarvey Harrison int show_unhandled_signals = 1; 1141c61e211dSHarvey Harrison 11422d4a7167SIngo Molnar static inline int 114368da336aSMichel Lespinasse access_error(unsigned long error_code, struct vm_area_struct *vma) 114492181f19SNick Piggin { 114507f146f5SDave Hansen /* This is only called for the current mm, so: */ 114607f146f5SDave Hansen bool foreign = false; 1147e8c6226dSDave Hansen 1148e8c6226dSDave Hansen /* 1149e8c6226dSDave Hansen * Read or write was blocked by protection keys. This is 1150e8c6226dSDave Hansen * always an unconditional error and can never result in 1151e8c6226dSDave Hansen * a follow-up action to resolve the fault, like a COW. 1152e8c6226dSDave Hansen */ 11531067f030SRicardo Neri if (error_code & X86_PF_PK) 1154e8c6226dSDave Hansen return 1; 1155e8c6226dSDave Hansen 115633a709b2SDave Hansen /* 115707f146f5SDave Hansen * Make sure to check the VMA so that we do not perform 11581067f030SRicardo Neri * faults just to hit a X86_PF_PK as soon as we fill in a 115907f146f5SDave Hansen * page. 116007f146f5SDave Hansen */ 11611067f030SRicardo Neri if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE), 11621067f030SRicardo Neri (error_code & X86_PF_INSTR), foreign)) 116307f146f5SDave Hansen return 1; 116433a709b2SDave Hansen 11651067f030SRicardo Neri if (error_code & X86_PF_WRITE) { 11662d4a7167SIngo Molnar /* write, present and write, not present: */ 116792181f19SNick Piggin if (unlikely(!(vma->vm_flags & VM_WRITE))) 116892181f19SNick Piggin return 1; 11692d4a7167SIngo Molnar return 0; 11702d4a7167SIngo Molnar } 11712d4a7167SIngo Molnar 11722d4a7167SIngo Molnar /* read, present: */ 11731067f030SRicardo Neri if (unlikely(error_code & X86_PF_PROT)) 117492181f19SNick Piggin return 1; 11752d4a7167SIngo Molnar 11762d4a7167SIngo Molnar /* read, not present: */ 117792181f19SNick Piggin if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))) 117892181f19SNick Piggin return 1; 117992181f19SNick Piggin 118092181f19SNick Piggin return 0; 118192181f19SNick Piggin } 118292181f19SNick Piggin 11830973a06cSHiroshi Shimamoto static int fault_in_kernel_space(unsigned long address) 11840973a06cSHiroshi Shimamoto { 1185d9517346SIngo Molnar return address >= TASK_SIZE_MAX; 11860973a06cSHiroshi Shimamoto } 11870973a06cSHiroshi Shimamoto 118840d3cd66SH. Peter Anvin static inline bool smap_violation(int error_code, struct pt_regs *regs) 118940d3cd66SH. Peter Anvin { 11904640c7eeSH. Peter Anvin if (!IS_ENABLED(CONFIG_X86_SMAP)) 11914640c7eeSH. Peter Anvin return false; 11924640c7eeSH. Peter Anvin 11934640c7eeSH. Peter Anvin if (!static_cpu_has(X86_FEATURE_SMAP)) 11944640c7eeSH. Peter Anvin return false; 11954640c7eeSH. Peter Anvin 11961067f030SRicardo Neri if (error_code & X86_PF_USER) 119740d3cd66SH. Peter Anvin return false; 119840d3cd66SH. Peter Anvin 1199f39b6f0eSAndy Lutomirski if (!user_mode(regs) && (regs->flags & X86_EFLAGS_AC)) 120040d3cd66SH. Peter Anvin return false; 120140d3cd66SH. Peter Anvin 120240d3cd66SH. Peter Anvin return true; 120340d3cd66SH. Peter Anvin } 120440d3cd66SH. Peter Anvin 1205c61e211dSHarvey Harrison /* 1206c61e211dSHarvey Harrison * This routine handles page faults. It determines the address, 1207c61e211dSHarvey Harrison * and the problem, and then passes it off to one of the appropriate 1208c61e211dSHarvey Harrison * routines. 1209c61e211dSHarvey Harrison */ 12109326638cSMasami Hiramatsu static noinline void 12110ac09f9fSJiri Olsa __do_page_fault(struct pt_regs *regs, unsigned long error_code, 12120ac09f9fSJiri Olsa unsigned long address) 1213c61e211dSHarvey Harrison { 1214c61e211dSHarvey Harrison struct vm_area_struct *vma; 12152d4a7167SIngo Molnar struct task_struct *tsk; 12162d4a7167SIngo Molnar struct mm_struct *mm; 121750a7ca3cSSouptick Joarder vm_fault_t fault, major = 0; 1218759496baSJohannes Weiner unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 1219a3c4fb7cSLaurent Dufour u32 pkey; 1220c61e211dSHarvey Harrison 1221c61e211dSHarvey Harrison tsk = current; 1222c61e211dSHarvey Harrison mm = tsk->mm; 12232d4a7167SIngo Molnar 12245dfaf90fSIngo Molnar prefetchw(&mm->mmap_sem); 1225f8561296SVegard Nossum 12260fd0e3daSPekka Paalanen if (unlikely(kmmio_fault(regs, address))) 122786069782SPekka Paalanen return; 1228c61e211dSHarvey Harrison 1229c61e211dSHarvey Harrison /* 1230c61e211dSHarvey Harrison * We fault-in kernel-space virtual memory on-demand. The 1231c61e211dSHarvey Harrison * 'reference' page table is init_mm.pgd. 1232c61e211dSHarvey Harrison * 1233c61e211dSHarvey Harrison * NOTE! We MUST NOT take any locks for this case. We may 1234c61e211dSHarvey Harrison * be in an interrupt or a critical region, and should 1235c61e211dSHarvey Harrison * only copy the information from the master page table, 1236c61e211dSHarvey Harrison * nothing more. 1237c61e211dSHarvey Harrison * 1238c61e211dSHarvey Harrison * This verifies that the fault happens in kernel space 1239c61e211dSHarvey Harrison * (error_code & 4) == 0, and that the fault was not a 1240c61e211dSHarvey Harrison * protection error (error_code & 9) == 0. 1241c61e211dSHarvey Harrison */ 12420973a06cSHiroshi Shimamoto if (unlikely(fault_in_kernel_space(address))) { 12431067f030SRicardo Neri if (!(error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) { 1244f8561296SVegard Nossum if (vmalloc_fault(address) >= 0) 1245c61e211dSHarvey Harrison return; 1246f8561296SVegard Nossum } 1247f8561296SVegard Nossum 12482d4a7167SIngo Molnar /* Can handle a stale RO->RW TLB: */ 124992181f19SNick Piggin if (spurious_fault(error_code, address)) 12505b727a3bSJeremy Fitzhardinge return; 12515b727a3bSJeremy Fitzhardinge 12522d4a7167SIngo Molnar /* kprobes don't want to hook the spurious faults: */ 1253e00b12e6SPeter Zijlstra if (kprobes_fault(regs)) 12549be260a6SMasami Hiramatsu return; 1255c61e211dSHarvey Harrison /* 1256c61e211dSHarvey Harrison * Don't take the mm semaphore here. If we fixup a prefetch 12572d4a7167SIngo Molnar * fault we could otherwise deadlock: 1258c61e211dSHarvey Harrison */ 1259*768fd9c6SEric W. Biederman bad_area_nosemaphore(regs, error_code, address); 12602d4a7167SIngo Molnar 126192181f19SNick Piggin return; 1262c61e211dSHarvey Harrison } 1263c61e211dSHarvey Harrison 12642d4a7167SIngo Molnar /* kprobes don't want to hook the spurious faults: */ 1265e00b12e6SPeter Zijlstra if (unlikely(kprobes_fault(regs))) 12669be260a6SMasami Hiramatsu return; 1267e00b12e6SPeter Zijlstra 12681067f030SRicardo Neri if (unlikely(error_code & X86_PF_RSVD)) 1269e00b12e6SPeter Zijlstra pgtable_bad(regs, error_code, address); 1270e00b12e6SPeter Zijlstra 1271e00b12e6SPeter Zijlstra if (unlikely(smap_violation(error_code, regs))) { 1272*768fd9c6SEric W. Biederman bad_area_nosemaphore(regs, error_code, address); 1273e00b12e6SPeter Zijlstra return; 1274e00b12e6SPeter Zijlstra } 1275e00b12e6SPeter Zijlstra 1276e00b12e6SPeter Zijlstra /* 1277e00b12e6SPeter Zijlstra * If we're in an interrupt, have no user context or are running 127870ffdb93SDavid Hildenbrand * in a region with pagefaults disabled then we must not take the fault 1279e00b12e6SPeter Zijlstra */ 128070ffdb93SDavid Hildenbrand if (unlikely(faulthandler_disabled() || !mm)) { 1281*768fd9c6SEric W. Biederman bad_area_nosemaphore(regs, error_code, address); 1282e00b12e6SPeter Zijlstra return; 1283e00b12e6SPeter Zijlstra } 1284e00b12e6SPeter Zijlstra 1285c61e211dSHarvey Harrison /* 1286891cffbdSLinus Torvalds * It's safe to allow irq's after cr2 has been saved and the 1287891cffbdSLinus Torvalds * vmalloc fault has been handled. 1288891cffbdSLinus Torvalds * 1289891cffbdSLinus Torvalds * User-mode registers count as a user access even for any 12902d4a7167SIngo Molnar * potential system fault or CPU buglet: 1291c61e211dSHarvey Harrison */ 1292f39b6f0eSAndy Lutomirski if (user_mode(regs)) { 1293891cffbdSLinus Torvalds local_irq_enable(); 12941067f030SRicardo Neri error_code |= X86_PF_USER; 1295759496baSJohannes Weiner flags |= FAULT_FLAG_USER; 12962d4a7167SIngo Molnar } else { 12972d4a7167SIngo Molnar if (regs->flags & X86_EFLAGS_IF) 1298c61e211dSHarvey Harrison local_irq_enable(); 12992d4a7167SIngo Molnar } 1300c61e211dSHarvey Harrison 1301a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 13027dd1fcc2SPeter Zijlstra 13031067f030SRicardo Neri if (error_code & X86_PF_WRITE) 1304759496baSJohannes Weiner flags |= FAULT_FLAG_WRITE; 13051067f030SRicardo Neri if (error_code & X86_PF_INSTR) 1306d61172b4SDave Hansen flags |= FAULT_FLAG_INSTRUCTION; 1307759496baSJohannes Weiner 13083a1dfe6eSIngo Molnar /* 13093a1dfe6eSIngo Molnar * When running in the kernel we expect faults to occur only to 13102d4a7167SIngo Molnar * addresses in user space. All other faults represent errors in 13112d4a7167SIngo Molnar * the kernel and should generate an OOPS. Unfortunately, in the 13122d4a7167SIngo Molnar * case of an erroneous fault occurring in a code path which already 13132d4a7167SIngo Molnar * holds mmap_sem we will deadlock attempting to validate the fault 13142d4a7167SIngo Molnar * against the address space. Luckily the kernel only validly 13152d4a7167SIngo Molnar * references user space from well defined areas of code, which are 13162d4a7167SIngo Molnar * listed in the exceptions table. 1317c61e211dSHarvey Harrison * 1318c61e211dSHarvey Harrison * As the vast majority of faults will be valid we will only perform 13192d4a7167SIngo Molnar * the source reference check when there is a possibility of a 13202d4a7167SIngo Molnar * deadlock. Attempt to lock the address space, if we cannot we then 13212d4a7167SIngo Molnar * validate the source. If this is invalid we can skip the address 13222d4a7167SIngo Molnar * space check, thus avoiding the deadlock: 1323c61e211dSHarvey Harrison */ 132492181f19SNick Piggin if (unlikely(!down_read_trylock(&mm->mmap_sem))) { 13251067f030SRicardo Neri if (!(error_code & X86_PF_USER) && 132692181f19SNick Piggin !search_exception_tables(regs->ip)) { 1327*768fd9c6SEric W. Biederman bad_area_nosemaphore(regs, error_code, address); 132892181f19SNick Piggin return; 132992181f19SNick Piggin } 1330d065bd81SMichel Lespinasse retry: 1331c61e211dSHarvey Harrison down_read(&mm->mmap_sem); 133201006074SPeter Zijlstra } else { 133301006074SPeter Zijlstra /* 13342d4a7167SIngo Molnar * The above down_read_trylock() might have succeeded in 13352d4a7167SIngo Molnar * which case we'll have missed the might_sleep() from 13362d4a7167SIngo Molnar * down_read(): 133701006074SPeter Zijlstra */ 133801006074SPeter Zijlstra might_sleep(); 1339c61e211dSHarvey Harrison } 1340c61e211dSHarvey Harrison 1341c61e211dSHarvey Harrison vma = find_vma(mm, address); 134292181f19SNick Piggin if (unlikely(!vma)) { 134392181f19SNick Piggin bad_area(regs, error_code, address); 134492181f19SNick Piggin return; 134592181f19SNick Piggin } 134692181f19SNick Piggin if (likely(vma->vm_start <= address)) 1347c61e211dSHarvey Harrison goto good_area; 134892181f19SNick Piggin if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) { 134992181f19SNick Piggin bad_area(regs, error_code, address); 135092181f19SNick Piggin return; 135192181f19SNick Piggin } 13521067f030SRicardo Neri if (error_code & X86_PF_USER) { 1353c61e211dSHarvey Harrison /* 1354c61e211dSHarvey Harrison * Accessing the stack below %sp is always a bug. 1355c61e211dSHarvey Harrison * The large cushion allows instructions like enter 1356c61e211dSHarvey Harrison * and pusha to work. ("enter $65535, $31" pushes 1357c61e211dSHarvey Harrison * 32 pointers and then decrements %sp by 65535.) 1358c61e211dSHarvey Harrison */ 135992181f19SNick Piggin if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) { 136092181f19SNick Piggin bad_area(regs, error_code, address); 136192181f19SNick Piggin return; 1362c61e211dSHarvey Harrison } 136392181f19SNick Piggin } 136492181f19SNick Piggin if (unlikely(expand_stack(vma, address))) { 136592181f19SNick Piggin bad_area(regs, error_code, address); 136692181f19SNick Piggin return; 136792181f19SNick Piggin } 136892181f19SNick Piggin 1369c61e211dSHarvey Harrison /* 1370c61e211dSHarvey Harrison * Ok, we have a good vm_area for this memory access, so 1371c61e211dSHarvey Harrison * we can handle it.. 1372c61e211dSHarvey Harrison */ 1373c61e211dSHarvey Harrison good_area: 137468da336aSMichel Lespinasse if (unlikely(access_error(error_code, vma))) { 13757b2d0dbaSDave Hansen bad_area_access_error(regs, error_code, address, vma); 137692181f19SNick Piggin return; 1377c61e211dSHarvey Harrison } 1378c61e211dSHarvey Harrison 1379c61e211dSHarvey Harrison /* 1380c61e211dSHarvey Harrison * If for any reason at all we couldn't handle the fault, 1381c61e211dSHarvey Harrison * make sure we exit gracefully rather than endlessly redo 13829a95f3cfSPaul Cassella * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if 13839a95f3cfSPaul Cassella * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked. 1384cb0631fdSVlastimil Babka * 1385cb0631fdSVlastimil Babka * Note that handle_userfault() may also release and reacquire mmap_sem 1386cb0631fdSVlastimil Babka * (and not return with VM_FAULT_RETRY), when returning to userland to 1387cb0631fdSVlastimil Babka * repeat the page fault later with a VM_FAULT_NOPAGE retval 1388cb0631fdSVlastimil Babka * (potentially after handling any pending signal during the return to 1389cb0631fdSVlastimil Babka * userland). The return to userland is identified whenever 1390cb0631fdSVlastimil Babka * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags. 1391cb0631fdSVlastimil Babka * Thus we have to be careful about not touching vma after handling the 1392cb0631fdSVlastimil Babka * fault, so we read the pkey beforehand. 1393c61e211dSHarvey Harrison */ 1394cb0631fdSVlastimil Babka pkey = vma_pkey(vma); 1395dcddffd4SKirill A. Shutemov fault = handle_mm_fault(vma, address, flags); 139626178ec1SLinus Torvalds major |= fault & VM_FAULT_MAJOR; 13972d4a7167SIngo Molnar 13983a13c4d7SJohannes Weiner /* 139926178ec1SLinus Torvalds * If we need to retry the mmap_sem has already been released, 140026178ec1SLinus Torvalds * and if there is a fatal signal pending there is no guarantee 140126178ec1SLinus Torvalds * that we made any progress. Handle this case first. 14023a13c4d7SJohannes Weiner */ 140326178ec1SLinus Torvalds if (unlikely(fault & VM_FAULT_RETRY)) { 140426178ec1SLinus Torvalds /* Retry at most once */ 140526178ec1SLinus Torvalds if (flags & FAULT_FLAG_ALLOW_RETRY) { 140626178ec1SLinus Torvalds flags &= ~FAULT_FLAG_ALLOW_RETRY; 140726178ec1SLinus Torvalds flags |= FAULT_FLAG_TRIED; 140826178ec1SLinus Torvalds if (!fatal_signal_pending(tsk)) 140926178ec1SLinus Torvalds goto retry; 141026178ec1SLinus Torvalds } 141126178ec1SLinus Torvalds 141226178ec1SLinus Torvalds /* User mode? Just return to handle the fatal exception */ 1413cf3c0a15SLinus Torvalds if (flags & FAULT_FLAG_USER) 14143a13c4d7SJohannes Weiner return; 14153a13c4d7SJohannes Weiner 141626178ec1SLinus Torvalds /* Not returning to user mode? Handle exceptions or die: */ 141726178ec1SLinus Torvalds no_context(regs, error_code, address, SIGBUS, BUS_ADRERR); 141826178ec1SLinus Torvalds return; 141926178ec1SLinus Torvalds } 142026178ec1SLinus Torvalds 14217fb08ecaSLinus Torvalds up_read(&mm->mmap_sem); 142226178ec1SLinus Torvalds if (unlikely(fault & VM_FAULT_ERROR)) { 1423a3c4fb7cSLaurent Dufour mm_fault_error(regs, error_code, address, &pkey, fault); 142437b23e05SKOSAKI Motohiro return; 142537b23e05SKOSAKI Motohiro } 142637b23e05SKOSAKI Motohiro 142737b23e05SKOSAKI Motohiro /* 142826178ec1SLinus Torvalds * Major/minor page fault accounting. If any of the events 142926178ec1SLinus Torvalds * returned VM_FAULT_MAJOR, we account it as a major fault. 1430d065bd81SMichel Lespinasse */ 143126178ec1SLinus Torvalds if (major) { 1432c61e211dSHarvey Harrison tsk->maj_flt++; 143326178ec1SLinus Torvalds perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address); 1434ac17dc8eSPeter Zijlstra } else { 1435c61e211dSHarvey Harrison tsk->min_flt++; 143626178ec1SLinus Torvalds perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address); 1437d065bd81SMichel Lespinasse } 1438c61e211dSHarvey Harrison 14398c938f9fSIngo Molnar check_v8086_mode(regs, address, tsk); 1440c61e211dSHarvey Harrison } 14419326638cSMasami Hiramatsu NOKPROBE_SYMBOL(__do_page_fault); 14426ba3c97aSFrederic Weisbecker 14439326638cSMasami Hiramatsu static nokprobe_inline void 14449326638cSMasami Hiramatsu trace_page_fault_entries(unsigned long address, struct pt_regs *regs, 1445d34603b0SSeiji Aguchi unsigned long error_code) 1446d34603b0SSeiji Aguchi { 1447d34603b0SSeiji Aguchi if (user_mode(regs)) 1448d4078e23SPeter Zijlstra trace_page_fault_user(address, regs, error_code); 1449d34603b0SSeiji Aguchi else 1450d4078e23SPeter Zijlstra trace_page_fault_kernel(address, regs, error_code); 1451d34603b0SSeiji Aguchi } 1452d34603b0SSeiji Aguchi 14530ac09f9fSJiri Olsa /* 145411a7ffb0SThomas Gleixner * We must have this function blacklisted from kprobes, tagged with notrace 145511a7ffb0SThomas Gleixner * and call read_cr2() before calling anything else. To avoid calling any 145611a7ffb0SThomas Gleixner * kind of tracing machinery before we've observed the CR2 value. 145711a7ffb0SThomas Gleixner * 145811a7ffb0SThomas Gleixner * exception_{enter,exit}() contains all sorts of tracepoints. 14590ac09f9fSJiri Olsa */ 146011a7ffb0SThomas Gleixner dotraplinkage void notrace 146111a7ffb0SThomas Gleixner do_page_fault(struct pt_regs *regs, unsigned long error_code) 146211a7ffb0SThomas Gleixner { 146311a7ffb0SThomas Gleixner unsigned long address = read_cr2(); /* Get the faulting address */ 1464d4078e23SPeter Zijlstra enum ctx_state prev_state; 146525c74b10SSeiji Aguchi 146625c74b10SSeiji Aguchi prev_state = exception_enter(); 146780954747SThomas Gleixner if (trace_pagefault_enabled()) 1468d4078e23SPeter Zijlstra trace_page_fault_entries(address, regs, error_code); 146911a7ffb0SThomas Gleixner 14700ac09f9fSJiri Olsa __do_page_fault(regs, error_code, address); 147125c74b10SSeiji Aguchi exception_exit(prev_state); 147225c74b10SSeiji Aguchi } 147311a7ffb0SThomas Gleixner NOKPROBE_SYMBOL(do_page_fault); 1474