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, 208a3c4fb7cSLaurent Dufour struct task_struct *tsk, u32 *pkey, int fault) 209c61e211dSHarvey Harrison { 210f672b49bSAndi Kleen unsigned lsb = 0; 211c61e211dSHarvey Harrison siginfo_t info; 212c61e211dSHarvey Harrison 2133eb0f519SEric W. Biederman clear_siginfo(&info); 214c61e211dSHarvey Harrison info.si_signo = si_signo; 215c61e211dSHarvey Harrison info.si_errno = 0; 216c61e211dSHarvey Harrison info.si_code = si_code; 217c61e211dSHarvey Harrison info.si_addr = (void __user *)address; 218f672b49bSAndi Kleen if (fault & VM_FAULT_HWPOISON_LARGE) 219f672b49bSAndi Kleen lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault)); 220f672b49bSAndi Kleen if (fault & VM_FAULT_HWPOISON) 221f672b49bSAndi Kleen lsb = PAGE_SHIFT; 222f672b49bSAndi Kleen info.si_addr_lsb = lsb; 2232d4a7167SIngo Molnar 224beacd6f7SEric W. Biederman fill_sig_info_pkey(si_signo, si_code, &info, pkey); 225019132ffSDave Hansen 226c61e211dSHarvey Harrison force_sig_info(si_signo, &info, tsk); 227c61e211dSHarvey Harrison } 228c61e211dSHarvey Harrison 229f2f13a85SIngo Molnar DEFINE_SPINLOCK(pgd_lock); 230f2f13a85SIngo Molnar LIST_HEAD(pgd_list); 2312d4a7167SIngo Molnar 232f2f13a85SIngo Molnar #ifdef CONFIG_X86_32 233f2f13a85SIngo Molnar static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address) 234f2f13a85SIngo Molnar { 235f2f13a85SIngo Molnar unsigned index = pgd_index(address); 236f2f13a85SIngo Molnar pgd_t *pgd_k; 237e0c4f675SKirill A. Shutemov p4d_t *p4d, *p4d_k; 238f2f13a85SIngo Molnar pud_t *pud, *pud_k; 239f2f13a85SIngo Molnar pmd_t *pmd, *pmd_k; 240f2f13a85SIngo Molnar 241f2f13a85SIngo Molnar pgd += index; 242f2f13a85SIngo Molnar pgd_k = init_mm.pgd + index; 243f2f13a85SIngo Molnar 244f2f13a85SIngo Molnar if (!pgd_present(*pgd_k)) 245f2f13a85SIngo Molnar return NULL; 246f2f13a85SIngo Molnar 247f2f13a85SIngo Molnar /* 248f2f13a85SIngo Molnar * set_pgd(pgd, *pgd_k); here would be useless on PAE 249f2f13a85SIngo Molnar * and redundant with the set_pmd() on non-PAE. As would 250e0c4f675SKirill A. Shutemov * set_p4d/set_pud. 251f2f13a85SIngo Molnar */ 252e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, address); 253e0c4f675SKirill A. Shutemov p4d_k = p4d_offset(pgd_k, address); 254e0c4f675SKirill A. Shutemov if (!p4d_present(*p4d_k)) 255e0c4f675SKirill A. Shutemov return NULL; 256e0c4f675SKirill A. Shutemov 257e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, address); 258e0c4f675SKirill A. Shutemov pud_k = pud_offset(p4d_k, address); 259f2f13a85SIngo Molnar if (!pud_present(*pud_k)) 260f2f13a85SIngo Molnar return NULL; 261f2f13a85SIngo Molnar 262f2f13a85SIngo Molnar pmd = pmd_offset(pud, address); 263f2f13a85SIngo Molnar pmd_k = pmd_offset(pud_k, address); 264f2f13a85SIngo Molnar if (!pmd_present(*pmd_k)) 265f2f13a85SIngo Molnar return NULL; 266f2f13a85SIngo Molnar 267b8bcfe99SJeremy Fitzhardinge if (!pmd_present(*pmd)) 268f2f13a85SIngo Molnar set_pmd(pmd, *pmd_k); 269b8bcfe99SJeremy Fitzhardinge else 270f2f13a85SIngo Molnar BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k)); 271f2f13a85SIngo Molnar 272f2f13a85SIngo Molnar return pmd_k; 273f2f13a85SIngo Molnar } 274f2f13a85SIngo Molnar 275f2f13a85SIngo Molnar void vmalloc_sync_all(void) 276f2f13a85SIngo Molnar { 277f2f13a85SIngo Molnar unsigned long address; 278f2f13a85SIngo Molnar 279f2f13a85SIngo Molnar if (SHARED_KERNEL_PMD) 280f2f13a85SIngo Molnar return; 281f2f13a85SIngo Molnar 282f2f13a85SIngo Molnar for (address = VMALLOC_START & PMD_MASK; 283dc4fac84SAndy Lutomirski address >= TASK_SIZE_MAX && address < FIXADDR_TOP; 284f2f13a85SIngo Molnar address += PMD_SIZE) { 285f2f13a85SIngo Molnar struct page *page; 286f2f13a85SIngo Molnar 287a79e53d8SAndrea Arcangeli spin_lock(&pgd_lock); 288f2f13a85SIngo Molnar list_for_each_entry(page, &pgd_list, lru) { 289617d34d9SJeremy Fitzhardinge spinlock_t *pgt_lock; 290f01f7c56SBorislav Petkov pmd_t *ret; 291617d34d9SJeremy Fitzhardinge 292a79e53d8SAndrea Arcangeli /* the pgt_lock only for Xen */ 293617d34d9SJeremy Fitzhardinge pgt_lock = &pgd_page_get_mm(page)->page_table_lock; 294617d34d9SJeremy Fitzhardinge 295617d34d9SJeremy Fitzhardinge spin_lock(pgt_lock); 296617d34d9SJeremy Fitzhardinge ret = vmalloc_sync_one(page_address(page), address); 297617d34d9SJeremy Fitzhardinge spin_unlock(pgt_lock); 298617d34d9SJeremy Fitzhardinge 299617d34d9SJeremy Fitzhardinge if (!ret) 300f2f13a85SIngo Molnar break; 301f2f13a85SIngo Molnar } 302a79e53d8SAndrea Arcangeli spin_unlock(&pgd_lock); 303f2f13a85SIngo Molnar } 304f2f13a85SIngo Molnar } 305f2f13a85SIngo Molnar 306f2f13a85SIngo Molnar /* 307f2f13a85SIngo Molnar * 32-bit: 308f2f13a85SIngo Molnar * 309f2f13a85SIngo Molnar * Handle a fault on the vmalloc or module mapping area 310f2f13a85SIngo Molnar */ 3119326638cSMasami Hiramatsu static noinline int vmalloc_fault(unsigned long address) 312f2f13a85SIngo Molnar { 313f2f13a85SIngo Molnar unsigned long pgd_paddr; 314f2f13a85SIngo Molnar pmd_t *pmd_k; 315f2f13a85SIngo Molnar pte_t *pte_k; 316f2f13a85SIngo Molnar 317f2f13a85SIngo Molnar /* Make sure we are in vmalloc area: */ 318f2f13a85SIngo Molnar if (!(address >= VMALLOC_START && address < VMALLOC_END)) 319f2f13a85SIngo Molnar return -1; 320f2f13a85SIngo Molnar 321f2f13a85SIngo Molnar /* 322f2f13a85SIngo Molnar * Synchronize this task's top level page-table 323f2f13a85SIngo Molnar * with the 'reference' page table. 324f2f13a85SIngo Molnar * 325f2f13a85SIngo Molnar * Do _not_ use "current" here. We might be inside 326f2f13a85SIngo Molnar * an interrupt in the middle of a task switch.. 327f2f13a85SIngo Molnar */ 3286c690ee1SAndy Lutomirski pgd_paddr = read_cr3_pa(); 329f2f13a85SIngo Molnar pmd_k = vmalloc_sync_one(__va(pgd_paddr), address); 330f2f13a85SIngo Molnar if (!pmd_k) 331f2f13a85SIngo Molnar return -1; 332f2f13a85SIngo Molnar 33318a95521SToshi Kani if (pmd_large(*pmd_k)) 334f4eafd8bSToshi Kani return 0; 335f4eafd8bSToshi Kani 336f2f13a85SIngo Molnar pte_k = pte_offset_kernel(pmd_k, address); 337f2f13a85SIngo Molnar if (!pte_present(*pte_k)) 338f2f13a85SIngo Molnar return -1; 339f2f13a85SIngo Molnar 340f2f13a85SIngo Molnar return 0; 341f2f13a85SIngo Molnar } 3429326638cSMasami Hiramatsu NOKPROBE_SYMBOL(vmalloc_fault); 343f2f13a85SIngo Molnar 344f2f13a85SIngo Molnar /* 345f2f13a85SIngo Molnar * Did it hit the DOS screen memory VA from vm86 mode? 346f2f13a85SIngo Molnar */ 347f2f13a85SIngo Molnar static inline void 348f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address, 349f2f13a85SIngo Molnar struct task_struct *tsk) 350f2f13a85SIngo Molnar { 3519fda6a06SBrian Gerst #ifdef CONFIG_VM86 352f2f13a85SIngo Molnar unsigned long bit; 353f2f13a85SIngo Molnar 3549fda6a06SBrian Gerst if (!v8086_mode(regs) || !tsk->thread.vm86) 355f2f13a85SIngo Molnar return; 356f2f13a85SIngo Molnar 357f2f13a85SIngo Molnar bit = (address - 0xA0000) >> PAGE_SHIFT; 358f2f13a85SIngo Molnar if (bit < 32) 3599fda6a06SBrian Gerst tsk->thread.vm86->screen_bitmap |= 1 << bit; 3609fda6a06SBrian Gerst #endif 361f2f13a85SIngo Molnar } 362c61e211dSHarvey Harrison 363087975b0SAkinobu Mita static bool low_pfn(unsigned long pfn) 364087975b0SAkinobu Mita { 365087975b0SAkinobu Mita return pfn < max_low_pfn; 366087975b0SAkinobu Mita } 367087975b0SAkinobu Mita 368cae30f82SAdrian Bunk static void dump_pagetable(unsigned long address) 369c61e211dSHarvey Harrison { 3706c690ee1SAndy Lutomirski pgd_t *base = __va(read_cr3_pa()); 371087975b0SAkinobu Mita pgd_t *pgd = &base[pgd_index(address)]; 372e0c4f675SKirill A. Shutemov p4d_t *p4d; 373e0c4f675SKirill A. Shutemov pud_t *pud; 374087975b0SAkinobu Mita pmd_t *pmd; 375087975b0SAkinobu Mita pte_t *pte; 3762d4a7167SIngo Molnar 377c61e211dSHarvey Harrison #ifdef CONFIG_X86_PAE 37839e48d9bSJan Beulich pr_info("*pdpt = %016Lx ", pgd_val(*pgd)); 379087975b0SAkinobu Mita if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd)) 380087975b0SAkinobu Mita goto out; 38139e48d9bSJan Beulich #define pr_pde pr_cont 38239e48d9bSJan Beulich #else 38339e48d9bSJan Beulich #define pr_pde pr_info 384c61e211dSHarvey Harrison #endif 385e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, address); 386e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, address); 387e0c4f675SKirill A. Shutemov pmd = pmd_offset(pud, address); 38839e48d9bSJan Beulich pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd)); 38939e48d9bSJan Beulich #undef pr_pde 390c61e211dSHarvey Harrison 391c61e211dSHarvey Harrison /* 392c61e211dSHarvey Harrison * We must not directly access the pte in the highpte 393c61e211dSHarvey Harrison * case if the page table is located in highmem. 394c61e211dSHarvey Harrison * And let's rather not kmap-atomic the pte, just in case 3952d4a7167SIngo Molnar * it's allocated already: 396c61e211dSHarvey Harrison */ 397087975b0SAkinobu Mita if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd)) 398087975b0SAkinobu Mita goto out; 3992d4a7167SIngo Molnar 400087975b0SAkinobu Mita pte = pte_offset_kernel(pmd, address); 40139e48d9bSJan Beulich pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte)); 402087975b0SAkinobu Mita out: 40339e48d9bSJan Beulich pr_cont("\n"); 404f2f13a85SIngo Molnar } 405f2f13a85SIngo Molnar 406f2f13a85SIngo Molnar #else /* CONFIG_X86_64: */ 407f2f13a85SIngo Molnar 408f2f13a85SIngo Molnar void vmalloc_sync_all(void) 409f2f13a85SIngo Molnar { 4105372e155SKirill A. Shutemov sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END); 411f2f13a85SIngo Molnar } 412f2f13a85SIngo Molnar 413f2f13a85SIngo Molnar /* 414f2f13a85SIngo Molnar * 64-bit: 415f2f13a85SIngo Molnar * 416f2f13a85SIngo Molnar * Handle a fault on the vmalloc area 417f2f13a85SIngo Molnar */ 4189326638cSMasami Hiramatsu static noinline int vmalloc_fault(unsigned long address) 419f2f13a85SIngo Molnar { 420565977a3SToshi Kani pgd_t *pgd, *pgd_k; 421565977a3SToshi Kani p4d_t *p4d, *p4d_k; 422565977a3SToshi Kani pud_t *pud; 423565977a3SToshi Kani pmd_t *pmd; 424565977a3SToshi Kani pte_t *pte; 425f2f13a85SIngo Molnar 426f2f13a85SIngo Molnar /* Make sure we are in vmalloc area: */ 427f2f13a85SIngo Molnar if (!(address >= VMALLOC_START && address < VMALLOC_END)) 428f2f13a85SIngo Molnar return -1; 429f2f13a85SIngo Molnar 430ebc8827fSFrederic Weisbecker WARN_ON_ONCE(in_nmi()); 431ebc8827fSFrederic Weisbecker 432f2f13a85SIngo Molnar /* 433f2f13a85SIngo Molnar * Copy kernel mappings over when needed. This can also 434f2f13a85SIngo Molnar * happen within a race in page table update. In the later 435f2f13a85SIngo Molnar * case just flush: 436f2f13a85SIngo Molnar */ 4376c690ee1SAndy Lutomirski pgd = (pgd_t *)__va(read_cr3_pa()) + pgd_index(address); 438565977a3SToshi Kani pgd_k = pgd_offset_k(address); 439565977a3SToshi Kani if (pgd_none(*pgd_k)) 440f2f13a85SIngo Molnar return -1; 441f2f13a85SIngo Molnar 442ed7588d5SKirill A. Shutemov if (pgtable_l5_enabled()) { 4431160c277SSamu Kallio if (pgd_none(*pgd)) { 444565977a3SToshi Kani set_pgd(pgd, *pgd_k); 4451160c277SSamu Kallio arch_flush_lazy_mmu_mode(); 44636b3a772SAndy Lutomirski } else { 447565977a3SToshi Kani BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_k)); 4481160c277SSamu Kallio } 44936b3a772SAndy Lutomirski } 450f2f13a85SIngo Molnar 451b50858ceSKirill A. Shutemov /* With 4-level paging, copying happens on the p4d level. */ 452b50858ceSKirill A. Shutemov p4d = p4d_offset(pgd, address); 453565977a3SToshi Kani p4d_k = p4d_offset(pgd_k, address); 454565977a3SToshi Kani if (p4d_none(*p4d_k)) 455b50858ceSKirill A. Shutemov return -1; 456b50858ceSKirill A. Shutemov 457ed7588d5SKirill A. Shutemov if (p4d_none(*p4d) && !pgtable_l5_enabled()) { 458565977a3SToshi Kani set_p4d(p4d, *p4d_k); 459b50858ceSKirill A. Shutemov arch_flush_lazy_mmu_mode(); 460b50858ceSKirill A. Shutemov } else { 461565977a3SToshi Kani BUG_ON(p4d_pfn(*p4d) != p4d_pfn(*p4d_k)); 462b50858ceSKirill A. Shutemov } 463b50858ceSKirill A. Shutemov 46436b3a772SAndy Lutomirski BUILD_BUG_ON(CONFIG_PGTABLE_LEVELS < 4); 465f2f13a85SIngo Molnar 466b50858ceSKirill A. Shutemov pud = pud_offset(p4d, address); 467565977a3SToshi Kani if (pud_none(*pud)) 468f2f13a85SIngo Molnar return -1; 469f2f13a85SIngo Molnar 47018a95521SToshi Kani if (pud_large(*pud)) 471f4eafd8bSToshi Kani return 0; 472f4eafd8bSToshi Kani 473f2f13a85SIngo Molnar pmd = pmd_offset(pud, address); 474565977a3SToshi Kani if (pmd_none(*pmd)) 475f2f13a85SIngo Molnar return -1; 476f2f13a85SIngo Molnar 47718a95521SToshi Kani if (pmd_large(*pmd)) 478f4eafd8bSToshi Kani return 0; 479f4eafd8bSToshi Kani 480f2f13a85SIngo Molnar pte = pte_offset_kernel(pmd, address); 481565977a3SToshi Kani if (!pte_present(*pte)) 482565977a3SToshi Kani return -1; 483f2f13a85SIngo Molnar 484f2f13a85SIngo Molnar return 0; 485f2f13a85SIngo Molnar } 4869326638cSMasami Hiramatsu NOKPROBE_SYMBOL(vmalloc_fault); 487f2f13a85SIngo Molnar 488e05139f2SJan Beulich #ifdef CONFIG_CPU_SUP_AMD 489f2f13a85SIngo Molnar static const char errata93_warning[] = 490ad361c98SJoe Perches KERN_ERR 491ad361c98SJoe Perches "******* Your BIOS seems to not contain a fix for K8 errata #93\n" 492ad361c98SJoe Perches "******* Working around it, but it may cause SEGVs or burn power.\n" 493ad361c98SJoe Perches "******* Please consider a BIOS update.\n" 494ad361c98SJoe Perches "******* Disabling USB legacy in the BIOS may also help.\n"; 495e05139f2SJan Beulich #endif 496f2f13a85SIngo Molnar 497f2f13a85SIngo Molnar /* 498f2f13a85SIngo Molnar * No vm86 mode in 64-bit mode: 499f2f13a85SIngo Molnar */ 500f2f13a85SIngo Molnar static inline void 501f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address, 502f2f13a85SIngo Molnar struct task_struct *tsk) 503f2f13a85SIngo Molnar { 504f2f13a85SIngo Molnar } 505f2f13a85SIngo Molnar 506f2f13a85SIngo Molnar static int bad_address(void *p) 507f2f13a85SIngo Molnar { 508f2f13a85SIngo Molnar unsigned long dummy; 509f2f13a85SIngo Molnar 510f2f13a85SIngo Molnar return probe_kernel_address((unsigned long *)p, dummy); 511f2f13a85SIngo Molnar } 512f2f13a85SIngo Molnar 513f2f13a85SIngo Molnar static void dump_pagetable(unsigned long address) 514f2f13a85SIngo Molnar { 5156c690ee1SAndy Lutomirski pgd_t *base = __va(read_cr3_pa()); 516087975b0SAkinobu Mita pgd_t *pgd = base + pgd_index(address); 517e0c4f675SKirill A. Shutemov p4d_t *p4d; 518c61e211dSHarvey Harrison pud_t *pud; 519c61e211dSHarvey Harrison pmd_t *pmd; 520c61e211dSHarvey Harrison pte_t *pte; 521c61e211dSHarvey Harrison 5222d4a7167SIngo Molnar if (bad_address(pgd)) 5232d4a7167SIngo Molnar goto bad; 5242d4a7167SIngo Molnar 52539e48d9bSJan Beulich pr_info("PGD %lx ", pgd_val(*pgd)); 5262d4a7167SIngo Molnar 5272d4a7167SIngo Molnar if (!pgd_present(*pgd)) 5282d4a7167SIngo Molnar goto out; 529c61e211dSHarvey Harrison 530e0c4f675SKirill A. Shutemov p4d = p4d_offset(pgd, address); 531e0c4f675SKirill A. Shutemov if (bad_address(p4d)) 532e0c4f675SKirill A. Shutemov goto bad; 533e0c4f675SKirill A. Shutemov 53439e48d9bSJan Beulich pr_cont("P4D %lx ", p4d_val(*p4d)); 535e0c4f675SKirill A. Shutemov if (!p4d_present(*p4d) || p4d_large(*p4d)) 536e0c4f675SKirill A. Shutemov goto out; 537e0c4f675SKirill A. Shutemov 538e0c4f675SKirill A. Shutemov pud = pud_offset(p4d, address); 5392d4a7167SIngo Molnar if (bad_address(pud)) 5402d4a7167SIngo Molnar goto bad; 5412d4a7167SIngo Molnar 54239e48d9bSJan Beulich pr_cont("PUD %lx ", pud_val(*pud)); 543b5360222SAndi Kleen if (!pud_present(*pud) || pud_large(*pud)) 5442d4a7167SIngo Molnar goto out; 545c61e211dSHarvey Harrison 546c61e211dSHarvey Harrison pmd = pmd_offset(pud, address); 5472d4a7167SIngo Molnar if (bad_address(pmd)) 5482d4a7167SIngo Molnar goto bad; 5492d4a7167SIngo Molnar 55039e48d9bSJan Beulich pr_cont("PMD %lx ", pmd_val(*pmd)); 5512d4a7167SIngo Molnar if (!pmd_present(*pmd) || pmd_large(*pmd)) 5522d4a7167SIngo Molnar goto out; 553c61e211dSHarvey Harrison 554c61e211dSHarvey Harrison pte = pte_offset_kernel(pmd, address); 5552d4a7167SIngo Molnar if (bad_address(pte)) 5562d4a7167SIngo Molnar goto bad; 5572d4a7167SIngo Molnar 55839e48d9bSJan Beulich pr_cont("PTE %lx", pte_val(*pte)); 5592d4a7167SIngo Molnar out: 56039e48d9bSJan Beulich pr_cont("\n"); 561c61e211dSHarvey Harrison return; 562c61e211dSHarvey Harrison bad: 56339e48d9bSJan Beulich pr_info("BAD\n"); 564c61e211dSHarvey Harrison } 565c61e211dSHarvey Harrison 566f2f13a85SIngo Molnar #endif /* CONFIG_X86_64 */ 567c61e211dSHarvey Harrison 5682d4a7167SIngo Molnar /* 5692d4a7167SIngo Molnar * Workaround for K8 erratum #93 & buggy BIOS. 5702d4a7167SIngo Molnar * 5712d4a7167SIngo Molnar * BIOS SMM functions are required to use a specific workaround 5722d4a7167SIngo Molnar * to avoid corruption of the 64bit RIP register on C stepping K8. 5732d4a7167SIngo Molnar * 5742d4a7167SIngo Molnar * A lot of BIOS that didn't get tested properly miss this. 5752d4a7167SIngo Molnar * 5762d4a7167SIngo Molnar * The OS sees this as a page fault with the upper 32bits of RIP cleared. 5772d4a7167SIngo Molnar * Try to work around it here. 5782d4a7167SIngo Molnar * 5792d4a7167SIngo Molnar * Note we only handle faults in kernel here. 5802d4a7167SIngo Molnar * Does nothing on 32-bit. 581c61e211dSHarvey Harrison */ 582c61e211dSHarvey Harrison static int is_errata93(struct pt_regs *regs, unsigned long address) 583c61e211dSHarvey Harrison { 584e05139f2SJan Beulich #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD) 585e05139f2SJan Beulich if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD 586e05139f2SJan Beulich || boot_cpu_data.x86 != 0xf) 587e05139f2SJan Beulich return 0; 588e05139f2SJan Beulich 589c61e211dSHarvey Harrison if (address != regs->ip) 590c61e211dSHarvey Harrison return 0; 5912d4a7167SIngo Molnar 592c61e211dSHarvey Harrison if ((address >> 32) != 0) 593c61e211dSHarvey Harrison return 0; 5942d4a7167SIngo Molnar 595c61e211dSHarvey Harrison address |= 0xffffffffUL << 32; 596c61e211dSHarvey Harrison if ((address >= (u64)_stext && address <= (u64)_etext) || 597c61e211dSHarvey Harrison (address >= MODULES_VADDR && address <= MODULES_END)) { 598a454ab31SIngo Molnar printk_once(errata93_warning); 599c61e211dSHarvey Harrison regs->ip = address; 600c61e211dSHarvey Harrison return 1; 601c61e211dSHarvey Harrison } 602c61e211dSHarvey Harrison #endif 603c61e211dSHarvey Harrison return 0; 604c61e211dSHarvey Harrison } 605c61e211dSHarvey Harrison 606c61e211dSHarvey Harrison /* 6072d4a7167SIngo Molnar * Work around K8 erratum #100 K8 in compat mode occasionally jumps 6082d4a7167SIngo Molnar * to illegal addresses >4GB. 6092d4a7167SIngo Molnar * 6102d4a7167SIngo Molnar * We catch this in the page fault handler because these addresses 6112d4a7167SIngo Molnar * are not reachable. Just detect this case and return. Any code 612c61e211dSHarvey Harrison * segment in LDT is compatibility mode. 613c61e211dSHarvey Harrison */ 614c61e211dSHarvey Harrison static int is_errata100(struct pt_regs *regs, unsigned long address) 615c61e211dSHarvey Harrison { 616c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 6172d4a7167SIngo Molnar if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32)) 618c61e211dSHarvey Harrison return 1; 619c61e211dSHarvey Harrison #endif 620c61e211dSHarvey Harrison return 0; 621c61e211dSHarvey Harrison } 622c61e211dSHarvey Harrison 623c61e211dSHarvey Harrison static int is_f00f_bug(struct pt_regs *regs, unsigned long address) 624c61e211dSHarvey Harrison { 625c61e211dSHarvey Harrison #ifdef CONFIG_X86_F00F_BUG 626c61e211dSHarvey Harrison unsigned long nr; 6272d4a7167SIngo Molnar 628c61e211dSHarvey Harrison /* 6292d4a7167SIngo Molnar * Pentium F0 0F C7 C8 bug workaround: 630c61e211dSHarvey Harrison */ 631e2604b49SBorislav Petkov if (boot_cpu_has_bug(X86_BUG_F00F)) { 632c61e211dSHarvey Harrison nr = (address - idt_descr.address) >> 3; 633c61e211dSHarvey Harrison 634c61e211dSHarvey Harrison if (nr == 6) { 635c61e211dSHarvey Harrison do_invalid_op(regs, 0); 636c61e211dSHarvey Harrison return 1; 637c61e211dSHarvey Harrison } 638c61e211dSHarvey Harrison } 639c61e211dSHarvey Harrison #endif 640c61e211dSHarvey Harrison return 0; 641c61e211dSHarvey Harrison } 642c61e211dSHarvey Harrison 6432d4a7167SIngo Molnar static void 6442d4a7167SIngo Molnar show_fault_oops(struct pt_regs *regs, unsigned long error_code, 645c61e211dSHarvey Harrison unsigned long address) 646c61e211dSHarvey Harrison { 647c61e211dSHarvey Harrison if (!oops_may_print()) 648c61e211dSHarvey Harrison return; 649c61e211dSHarvey Harrison 6501067f030SRicardo Neri if (error_code & X86_PF_INSTR) { 65193809be8SHarvey Harrison unsigned int level; 652426e34ccSMatt Fleming pgd_t *pgd; 653426e34ccSMatt Fleming pte_t *pte; 6542d4a7167SIngo Molnar 6556c690ee1SAndy Lutomirski pgd = __va(read_cr3_pa()); 656426e34ccSMatt Fleming pgd += pgd_index(address); 657426e34ccSMatt Fleming 658426e34ccSMatt Fleming pte = lookup_address_in_pgd(pgd, address, &level); 659c61e211dSHarvey Harrison 6608f766149SIngo Molnar if (pte && pte_present(*pte) && !pte_exec(*pte)) 661d79d0d8aSDmitry Vyukov pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n", 662d79d0d8aSDmitry Vyukov from_kuid(&init_user_ns, current_uid())); 663eff50c34SJiri Kosina if (pte && pte_present(*pte) && pte_exec(*pte) && 664eff50c34SJiri Kosina (pgd_flags(*pgd) & _PAGE_USER) && 6651e02ce4cSAndy Lutomirski (__read_cr4() & X86_CR4_SMEP)) 666d79d0d8aSDmitry Vyukov pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n", 667d79d0d8aSDmitry Vyukov from_kuid(&init_user_ns, current_uid())); 668c61e211dSHarvey Harrison } 669fd40d6e3SHarvey Harrison 6704188f063SDmitry Vyukov pr_alert("BUG: unable to handle kernel %s at %px\n", 6714188f063SDmitry Vyukov address < PAGE_SIZE ? "NULL pointer dereference" : "paging request", 6724188f063SDmitry Vyukov (void *)address); 6732d4a7167SIngo Molnar 674c61e211dSHarvey Harrison dump_pagetable(address); 675c61e211dSHarvey Harrison } 676c61e211dSHarvey Harrison 6772d4a7167SIngo Molnar static noinline void 6782d4a7167SIngo Molnar pgtable_bad(struct pt_regs *regs, unsigned long error_code, 6792d4a7167SIngo Molnar unsigned long address) 680c61e211dSHarvey Harrison { 6812d4a7167SIngo Molnar struct task_struct *tsk; 6822d4a7167SIngo Molnar unsigned long flags; 6832d4a7167SIngo Molnar int sig; 6842d4a7167SIngo Molnar 6852d4a7167SIngo Molnar flags = oops_begin(); 6862d4a7167SIngo Molnar tsk = current; 6872d4a7167SIngo Molnar sig = SIGKILL; 688c61e211dSHarvey Harrison 689c61e211dSHarvey Harrison printk(KERN_ALERT "%s: Corrupted page table at address %lx\n", 69092181f19SNick Piggin tsk->comm, address); 691c61e211dSHarvey Harrison dump_pagetable(address); 6922d4a7167SIngo Molnar 693c61e211dSHarvey Harrison tsk->thread.cr2 = address; 69451e7dc70SSrikar Dronamraju tsk->thread.trap_nr = X86_TRAP_PF; 695c61e211dSHarvey Harrison tsk->thread.error_code = error_code; 6962d4a7167SIngo Molnar 697c61e211dSHarvey Harrison if (__die("Bad pagetable", regs, error_code)) 698874d93d1SAlexander van Heukelum sig = 0; 6992d4a7167SIngo Molnar 700874d93d1SAlexander van Heukelum oops_end(flags, regs, sig); 701c61e211dSHarvey Harrison } 702c61e211dSHarvey Harrison 7032d4a7167SIngo Molnar static noinline void 7042d4a7167SIngo Molnar no_context(struct pt_regs *regs, unsigned long error_code, 7054fc34901SAndy Lutomirski unsigned long address, int signal, int si_code) 70692181f19SNick Piggin { 70792181f19SNick Piggin struct task_struct *tsk = current; 70892181f19SNick Piggin unsigned long flags; 70992181f19SNick Piggin int sig; 71092181f19SNick Piggin 71192181f19SNick Piggin /* Are we prepared to handle this kernel fault? */ 712548acf19STony Luck if (fixup_exception(regs, X86_TRAP_PF)) { 713c026b359SPeter Zijlstra /* 714c026b359SPeter Zijlstra * Any interrupt that takes a fault gets the fixup. This makes 715c026b359SPeter Zijlstra * the below recursive fault logic only apply to a faults from 716c026b359SPeter Zijlstra * task context. 717c026b359SPeter Zijlstra */ 718c026b359SPeter Zijlstra if (in_interrupt()) 719c026b359SPeter Zijlstra return; 720c026b359SPeter Zijlstra 721c026b359SPeter Zijlstra /* 722c026b359SPeter Zijlstra * Per the above we're !in_interrupt(), aka. task context. 723c026b359SPeter Zijlstra * 724c026b359SPeter Zijlstra * In this case we need to make sure we're not recursively 725c026b359SPeter Zijlstra * faulting through the emulate_vsyscall() logic. 726c026b359SPeter Zijlstra */ 7272a53ccbcSIngo Molnar if (current->thread.sig_on_uaccess_err && signal) { 72851e7dc70SSrikar Dronamraju tsk->thread.trap_nr = X86_TRAP_PF; 7291067f030SRicardo Neri tsk->thread.error_code = error_code | X86_PF_USER; 7304fc34901SAndy Lutomirski tsk->thread.cr2 = address; 7314fc34901SAndy Lutomirski 7324fc34901SAndy Lutomirski /* XXX: hwpoison faults will set the wrong code. */ 7337b2d0dbaSDave Hansen force_sig_info_fault(signal, si_code, address, 734a3c4fb7cSLaurent Dufour tsk, NULL, 0); 7354fc34901SAndy Lutomirski } 736c026b359SPeter Zijlstra 737c026b359SPeter Zijlstra /* 738c026b359SPeter Zijlstra * Barring that, we can do the fixup and be happy. 739c026b359SPeter Zijlstra */ 74092181f19SNick Piggin return; 7414fc34901SAndy Lutomirski } 74292181f19SNick Piggin 7436271cfdfSAndy Lutomirski #ifdef CONFIG_VMAP_STACK 7446271cfdfSAndy Lutomirski /* 7456271cfdfSAndy Lutomirski * Stack overflow? During boot, we can fault near the initial 7466271cfdfSAndy Lutomirski * stack in the direct map, but that's not an overflow -- check 7476271cfdfSAndy Lutomirski * that we're in vmalloc space to avoid this. 7486271cfdfSAndy Lutomirski */ 7496271cfdfSAndy Lutomirski if (is_vmalloc_addr((void *)address) && 7506271cfdfSAndy Lutomirski (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) || 7516271cfdfSAndy Lutomirski address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) { 7526271cfdfSAndy Lutomirski unsigned long stack = this_cpu_read(orig_ist.ist[DOUBLEFAULT_STACK]) - sizeof(void *); 7536271cfdfSAndy Lutomirski /* 7546271cfdfSAndy Lutomirski * We're likely to be running with very little stack space 7556271cfdfSAndy Lutomirski * left. It's plausible that we'd hit this condition but 7566271cfdfSAndy Lutomirski * double-fault even before we get this far, in which case 7576271cfdfSAndy Lutomirski * we're fine: the double-fault handler will deal with it. 7586271cfdfSAndy Lutomirski * 7596271cfdfSAndy Lutomirski * We don't want to make it all the way into the oops code 7606271cfdfSAndy Lutomirski * and then double-fault, though, because we're likely to 7616271cfdfSAndy Lutomirski * break the console driver and lose most of the stack dump. 7626271cfdfSAndy Lutomirski */ 7636271cfdfSAndy Lutomirski asm volatile ("movq %[stack], %%rsp\n\t" 7646271cfdfSAndy Lutomirski "call handle_stack_overflow\n\t" 7656271cfdfSAndy Lutomirski "1: jmp 1b" 766f5caf621SJosh Poimboeuf : ASM_CALL_CONSTRAINT 7676271cfdfSAndy Lutomirski : "D" ("kernel stack overflow (page fault)"), 7686271cfdfSAndy Lutomirski "S" (regs), "d" (address), 7696271cfdfSAndy Lutomirski [stack] "rm" (stack)); 7706271cfdfSAndy Lutomirski unreachable(); 7716271cfdfSAndy Lutomirski } 7726271cfdfSAndy Lutomirski #endif 7736271cfdfSAndy Lutomirski 77492181f19SNick Piggin /* 7752d4a7167SIngo Molnar * 32-bit: 7762d4a7167SIngo Molnar * 77792181f19SNick Piggin * Valid to do another page fault here, because if this fault 77892181f19SNick Piggin * had been triggered by is_prefetch fixup_exception would have 77992181f19SNick Piggin * handled it. 78092181f19SNick Piggin * 7812d4a7167SIngo Molnar * 64-bit: 7822d4a7167SIngo Molnar * 78392181f19SNick Piggin * Hall of shame of CPU/BIOS bugs. 78492181f19SNick Piggin */ 78592181f19SNick Piggin if (is_prefetch(regs, error_code, address)) 78692181f19SNick Piggin return; 78792181f19SNick Piggin 78892181f19SNick Piggin if (is_errata93(regs, address)) 78992181f19SNick Piggin return; 79092181f19SNick Piggin 79192181f19SNick Piggin /* 79292181f19SNick Piggin * Oops. The kernel tried to access some bad page. We'll have to 7932d4a7167SIngo Molnar * terminate things with extreme prejudice: 79492181f19SNick Piggin */ 79592181f19SNick Piggin flags = oops_begin(); 79692181f19SNick Piggin 79792181f19SNick Piggin show_fault_oops(regs, error_code, address); 79892181f19SNick Piggin 799a70857e4SAaron Tomlin if (task_stack_end_corrupted(tsk)) 800b0f4c4b3SPrarit Bhargava printk(KERN_EMERG "Thread overran stack, or stack corrupted\n"); 80119803078SIngo Molnar 80292181f19SNick Piggin tsk->thread.cr2 = address; 80351e7dc70SSrikar Dronamraju tsk->thread.trap_nr = X86_TRAP_PF; 80492181f19SNick Piggin tsk->thread.error_code = error_code; 80592181f19SNick Piggin 80692181f19SNick Piggin sig = SIGKILL; 80792181f19SNick Piggin if (__die("Oops", regs, error_code)) 80892181f19SNick Piggin sig = 0; 8092d4a7167SIngo Molnar 81092181f19SNick Piggin /* Executive summary in case the body of the oops scrolled away */ 811b0f4c4b3SPrarit Bhargava printk(KERN_DEFAULT "CR2: %016lx\n", address); 8122d4a7167SIngo Molnar 81392181f19SNick Piggin oops_end(flags, regs, sig); 81492181f19SNick Piggin } 81592181f19SNick Piggin 8162d4a7167SIngo Molnar /* 8172d4a7167SIngo Molnar * Print out info about fatal segfaults, if the show_unhandled_signals 8182d4a7167SIngo Molnar * sysctl is set: 8192d4a7167SIngo Molnar */ 8202d4a7167SIngo Molnar static inline void 8212d4a7167SIngo Molnar show_signal_msg(struct pt_regs *regs, unsigned long error_code, 8222d4a7167SIngo Molnar unsigned long address, struct task_struct *tsk) 8232d4a7167SIngo Molnar { 824ba54d856SBorislav Petkov const char *loglvl = task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG; 825ba54d856SBorislav Petkov 8262d4a7167SIngo Molnar if (!unhandled_signal(tsk, SIGSEGV)) 8272d4a7167SIngo Molnar return; 8282d4a7167SIngo Molnar 8292d4a7167SIngo Molnar if (!printk_ratelimit()) 8302d4a7167SIngo Molnar return; 8312d4a7167SIngo Molnar 83210a7e9d8SKees Cook printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx", 833ba54d856SBorislav Petkov loglvl, tsk->comm, task_pid_nr(tsk), address, 8342d4a7167SIngo Molnar (void *)regs->ip, (void *)regs->sp, error_code); 8352d4a7167SIngo Molnar 8362d4a7167SIngo Molnar print_vma_addr(KERN_CONT " in ", regs->ip); 8372d4a7167SIngo Molnar 8382d4a7167SIngo Molnar printk(KERN_CONT "\n"); 839ba54d856SBorislav Petkov 840342db04aSJann Horn show_opcodes(regs, loglvl); 8412d4a7167SIngo Molnar } 8422d4a7167SIngo Molnar 8432d4a7167SIngo Molnar static void 8442d4a7167SIngo Molnar __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 845a3c4fb7cSLaurent Dufour unsigned long address, u32 *pkey, int si_code) 84692181f19SNick Piggin { 84792181f19SNick Piggin struct task_struct *tsk = current; 84892181f19SNick Piggin 84992181f19SNick Piggin /* User mode accesses just cause a SIGSEGV */ 8501067f030SRicardo Neri if (error_code & X86_PF_USER) { 85192181f19SNick Piggin /* 8522d4a7167SIngo Molnar * It's possible to have interrupts off here: 85392181f19SNick Piggin */ 85492181f19SNick Piggin local_irq_enable(); 85592181f19SNick Piggin 85692181f19SNick Piggin /* 85792181f19SNick Piggin * Valid to do another page fault here because this one came 8582d4a7167SIngo Molnar * from user space: 85992181f19SNick Piggin */ 86092181f19SNick Piggin if (is_prefetch(regs, error_code, address)) 86192181f19SNick Piggin return; 86292181f19SNick Piggin 86392181f19SNick Piggin if (is_errata100(regs, address)) 86492181f19SNick Piggin return; 86592181f19SNick Piggin 8663ae36655SAndy Lutomirski #ifdef CONFIG_X86_64 8673ae36655SAndy Lutomirski /* 8683ae36655SAndy Lutomirski * Instruction fetch faults in the vsyscall page might need 8693ae36655SAndy Lutomirski * emulation. 8703ae36655SAndy Lutomirski */ 8711067f030SRicardo Neri if (unlikely((error_code & X86_PF_INSTR) && 872f40c3300SAndy Lutomirski ((address & ~0xfff) == VSYSCALL_ADDR))) { 8733ae36655SAndy Lutomirski if (emulate_vsyscall(regs, address)) 8743ae36655SAndy Lutomirski return; 8753ae36655SAndy Lutomirski } 8763ae36655SAndy Lutomirski #endif 877dc4fac84SAndy Lutomirski 878dc4fac84SAndy Lutomirski /* 879dc4fac84SAndy Lutomirski * To avoid leaking information about the kernel page table 880dc4fac84SAndy Lutomirski * layout, pretend that user-mode accesses to kernel addresses 881dc4fac84SAndy Lutomirski * are always protection faults. 882dc4fac84SAndy Lutomirski */ 883dc4fac84SAndy Lutomirski if (address >= TASK_SIZE_MAX) 8841067f030SRicardo Neri error_code |= X86_PF_PROT; 8853ae36655SAndy Lutomirski 886e575a86fSKees Cook if (likely(show_unhandled_signals)) 8872d4a7167SIngo Molnar show_signal_msg(regs, error_code, address, tsk); 88892181f19SNick Piggin 88992181f19SNick Piggin tsk->thread.cr2 = address; 890e575a86fSKees Cook tsk->thread.error_code = error_code; 89151e7dc70SSrikar Dronamraju tsk->thread.trap_nr = X86_TRAP_PF; 8922d4a7167SIngo Molnar 893a3c4fb7cSLaurent Dufour force_sig_info_fault(SIGSEGV, si_code, address, tsk, pkey, 0); 8942d4a7167SIngo Molnar 89592181f19SNick Piggin return; 89692181f19SNick Piggin } 89792181f19SNick Piggin 89892181f19SNick Piggin if (is_f00f_bug(regs, address)) 89992181f19SNick Piggin return; 90092181f19SNick Piggin 9014fc34901SAndy Lutomirski no_context(regs, error_code, address, SIGSEGV, si_code); 90292181f19SNick Piggin } 90392181f19SNick Piggin 9042d4a7167SIngo Molnar static noinline void 9052d4a7167SIngo Molnar bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 906a3c4fb7cSLaurent Dufour unsigned long address, u32 *pkey) 90792181f19SNick Piggin { 908a3c4fb7cSLaurent Dufour __bad_area_nosemaphore(regs, error_code, address, pkey, SEGV_MAPERR); 90992181f19SNick Piggin } 91092181f19SNick Piggin 9112d4a7167SIngo Molnar static void 9122d4a7167SIngo Molnar __bad_area(struct pt_regs *regs, unsigned long error_code, 9137b2d0dbaSDave Hansen unsigned long address, struct vm_area_struct *vma, int si_code) 91492181f19SNick Piggin { 91592181f19SNick Piggin struct mm_struct *mm = current->mm; 916a3c4fb7cSLaurent Dufour u32 pkey; 917a3c4fb7cSLaurent Dufour 918a3c4fb7cSLaurent Dufour if (vma) 919a3c4fb7cSLaurent Dufour pkey = vma_pkey(vma); 92092181f19SNick Piggin 92192181f19SNick Piggin /* 92292181f19SNick Piggin * Something tried to access memory that isn't in our memory map.. 92392181f19SNick Piggin * Fix it, but check if it's kernel or user first.. 92492181f19SNick Piggin */ 92592181f19SNick Piggin up_read(&mm->mmap_sem); 92692181f19SNick Piggin 927a3c4fb7cSLaurent Dufour __bad_area_nosemaphore(regs, error_code, address, 928a3c4fb7cSLaurent Dufour (vma) ? &pkey : NULL, si_code); 92992181f19SNick Piggin } 93092181f19SNick Piggin 9312d4a7167SIngo Molnar static noinline void 9322d4a7167SIngo Molnar bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address) 93392181f19SNick Piggin { 9347b2d0dbaSDave Hansen __bad_area(regs, error_code, address, NULL, SEGV_MAPERR); 93592181f19SNick Piggin } 93692181f19SNick Piggin 93733a709b2SDave Hansen static inline bool bad_area_access_from_pkeys(unsigned long error_code, 93833a709b2SDave Hansen struct vm_area_struct *vma) 93933a709b2SDave Hansen { 94007f146f5SDave Hansen /* This code is always called on the current mm */ 94107f146f5SDave Hansen bool foreign = false; 94207f146f5SDave Hansen 94333a709b2SDave Hansen if (!boot_cpu_has(X86_FEATURE_OSPKE)) 94433a709b2SDave Hansen return false; 9451067f030SRicardo Neri if (error_code & X86_PF_PK) 94633a709b2SDave Hansen return true; 94707f146f5SDave Hansen /* this checks permission keys on the VMA: */ 9481067f030SRicardo Neri if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE), 9491067f030SRicardo Neri (error_code & X86_PF_INSTR), foreign)) 95007f146f5SDave Hansen return true; 95133a709b2SDave Hansen return false; 95292181f19SNick Piggin } 95392181f19SNick Piggin 9542d4a7167SIngo Molnar static noinline void 9552d4a7167SIngo Molnar bad_area_access_error(struct pt_regs *regs, unsigned long error_code, 9567b2d0dbaSDave Hansen unsigned long address, struct vm_area_struct *vma) 95792181f19SNick Piggin { 958019132ffSDave Hansen /* 959019132ffSDave Hansen * This OSPKE check is not strictly necessary at runtime. 960019132ffSDave Hansen * But, doing it this way allows compiler optimizations 961019132ffSDave Hansen * if pkeys are compiled out. 962019132ffSDave Hansen */ 96333a709b2SDave Hansen if (bad_area_access_from_pkeys(error_code, vma)) 964019132ffSDave Hansen __bad_area(regs, error_code, address, vma, SEGV_PKUERR); 965019132ffSDave Hansen else 9667b2d0dbaSDave Hansen __bad_area(regs, error_code, address, vma, SEGV_ACCERR); 96792181f19SNick Piggin } 96892181f19SNick Piggin 9692d4a7167SIngo Molnar static void 970a6e04aa9SAndi Kleen do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address, 971a3c4fb7cSLaurent Dufour u32 *pkey, unsigned int fault) 97292181f19SNick Piggin { 97392181f19SNick Piggin struct task_struct *tsk = current; 974a6e04aa9SAndi Kleen int code = BUS_ADRERR; 97592181f19SNick Piggin 9762d4a7167SIngo Molnar /* Kernel mode? Handle exceptions or die: */ 9771067f030SRicardo Neri if (!(error_code & X86_PF_USER)) { 9784fc34901SAndy Lutomirski no_context(regs, error_code, address, SIGBUS, BUS_ADRERR); 97996054569SLinus Torvalds return; 98096054569SLinus Torvalds } 9812d4a7167SIngo Molnar 982cd1b68f0SIngo Molnar /* User-space => ok to do another page fault: */ 98392181f19SNick Piggin if (is_prefetch(regs, error_code, address)) 98492181f19SNick Piggin return; 9852d4a7167SIngo Molnar 98692181f19SNick Piggin tsk->thread.cr2 = address; 98792181f19SNick Piggin tsk->thread.error_code = error_code; 98851e7dc70SSrikar Dronamraju tsk->thread.trap_nr = X86_TRAP_PF; 9892d4a7167SIngo Molnar 990a6e04aa9SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE 991f672b49bSAndi Kleen if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) { 992a6e04aa9SAndi Kleen printk(KERN_ERR 993a6e04aa9SAndi Kleen "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n", 994a6e04aa9SAndi Kleen tsk->comm, tsk->pid, address); 995a6e04aa9SAndi Kleen code = BUS_MCEERR_AR; 996a6e04aa9SAndi Kleen } 997a6e04aa9SAndi Kleen #endif 998a3c4fb7cSLaurent Dufour force_sig_info_fault(SIGBUS, code, address, tsk, pkey, fault); 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) 1029a3c4fb7cSLaurent Dufour bad_area_nosemaphore(regs, error_code, address, pkey); 103092181f19SNick Piggin else 103192181f19SNick Piggin BUG(); 103292181f19SNick Piggin } 10332d4a7167SIngo Molnar } 103492181f19SNick Piggin 1035*8fed6200SDave Hansen static int spurious_kernel_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 1074*8fed6200SDave Hansen spurious_kernel_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)) 1105*8fed6200SDave Hansen return spurious_kernel_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)) 1112*8fed6200SDave Hansen return spurious_kernel_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)) 1119*8fed6200SDave Hansen return spurious_kernel_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 1125*8fed6200SDave Hansen ret = spurious_kernel_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 */ 1133*8fed6200SDave Hansen ret = spurious_kernel_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 } 1138*8fed6200SDave Hansen NOKPROBE_SYMBOL(spurious_kernel_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 /* 1206*8fed6200SDave Hansen * Called for all faults where 'address' is part of the kernel address 1207*8fed6200SDave Hansen * space. Might get called for faults that originate from *code* that 1208*8fed6200SDave Hansen * ran in userspace or the kernel. 1209*8fed6200SDave Hansen */ 1210*8fed6200SDave Hansen static void 1211*8fed6200SDave Hansen do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, 1212*8fed6200SDave Hansen unsigned long address) 1213*8fed6200SDave Hansen { 1214*8fed6200SDave Hansen /* 1215*8fed6200SDave Hansen * We can fault-in kernel-space virtual memory on-demand. The 1216*8fed6200SDave Hansen * 'reference' page table is init_mm.pgd. 1217*8fed6200SDave Hansen * 1218*8fed6200SDave Hansen * NOTE! We MUST NOT take any locks for this case. We may 1219*8fed6200SDave Hansen * be in an interrupt or a critical region, and should 1220*8fed6200SDave Hansen * only copy the information from the master page table, 1221*8fed6200SDave Hansen * nothing more. 1222*8fed6200SDave Hansen * 1223*8fed6200SDave Hansen * Before doing this on-demand faulting, ensure that the 1224*8fed6200SDave Hansen * fault is not any of the following: 1225*8fed6200SDave Hansen * 1. A fault on a PTE with a reserved bit set. 1226*8fed6200SDave Hansen * 2. A fault caused by a user-mode access. (Do not demand- 1227*8fed6200SDave Hansen * fault kernel memory due to user-mode accesses). 1228*8fed6200SDave Hansen * 3. A fault caused by a page-level protection violation. 1229*8fed6200SDave Hansen * (A demand fault would be on a non-present page which 1230*8fed6200SDave Hansen * would have X86_PF_PROT==0). 1231*8fed6200SDave Hansen */ 1232*8fed6200SDave Hansen if (!(hw_error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) { 1233*8fed6200SDave Hansen if (vmalloc_fault(address) >= 0) 1234*8fed6200SDave Hansen return; 1235*8fed6200SDave Hansen } 1236*8fed6200SDave Hansen 1237*8fed6200SDave Hansen /* Was the fault spurious, caused by lazy TLB invalidation? */ 1238*8fed6200SDave Hansen if (spurious_kernel_fault(hw_error_code, address)) 1239*8fed6200SDave Hansen return; 1240*8fed6200SDave Hansen 1241*8fed6200SDave Hansen /* kprobes don't want to hook the spurious faults: */ 1242*8fed6200SDave Hansen if (kprobes_fault(regs)) 1243*8fed6200SDave Hansen return; 1244*8fed6200SDave Hansen 1245*8fed6200SDave Hansen /* 1246*8fed6200SDave Hansen * Note, despite being a "bad area", there are quite a few 1247*8fed6200SDave Hansen * acceptable reasons to get here, such as erratum fixups 1248*8fed6200SDave Hansen * and handling kernel code that can fault, like get_user(). 1249*8fed6200SDave Hansen * 1250*8fed6200SDave Hansen * Don't take the mm semaphore here. If we fixup a prefetch 1251*8fed6200SDave Hansen * fault we could otherwise deadlock: 1252*8fed6200SDave Hansen */ 1253*8fed6200SDave Hansen bad_area_nosemaphore(regs, hw_error_code, address, NULL); 1254*8fed6200SDave Hansen } 1255*8fed6200SDave Hansen NOKPROBE_SYMBOL(do_kern_addr_fault); 1256*8fed6200SDave Hansen 1257*8fed6200SDave Hansen /* 1258c61e211dSHarvey Harrison * This routine handles page faults. It determines the address, 1259c61e211dSHarvey Harrison * and the problem, and then passes it off to one of the appropriate 1260c61e211dSHarvey Harrison * routines. 1261c61e211dSHarvey Harrison */ 12629326638cSMasami Hiramatsu static noinline void 1263164477c2SDave Hansen __do_page_fault(struct pt_regs *regs, unsigned long hw_error_code, 12640ac09f9fSJiri Olsa unsigned long address) 1265c61e211dSHarvey Harrison { 1266164477c2SDave Hansen unsigned long sw_error_code; 1267c61e211dSHarvey Harrison struct vm_area_struct *vma; 12682d4a7167SIngo Molnar struct task_struct *tsk; 12692d4a7167SIngo Molnar struct mm_struct *mm; 127050a7ca3cSSouptick Joarder vm_fault_t fault, major = 0; 1271759496baSJohannes Weiner unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 1272a3c4fb7cSLaurent Dufour u32 pkey; 1273c61e211dSHarvey Harrison 1274c61e211dSHarvey Harrison tsk = current; 1275c61e211dSHarvey Harrison mm = tsk->mm; 12762d4a7167SIngo Molnar 12775dfaf90fSIngo Molnar prefetchw(&mm->mmap_sem); 1278f8561296SVegard Nossum 12790fd0e3daSPekka Paalanen if (unlikely(kmmio_fault(regs, address))) 128086069782SPekka Paalanen return; 1281c61e211dSHarvey Harrison 1282*8fed6200SDave Hansen /* Was the fault on kernel-controlled part of the address space? */ 12830973a06cSHiroshi Shimamoto if (unlikely(fault_in_kernel_space(address))) { 1284*8fed6200SDave Hansen do_kern_addr_fault(regs, hw_error_code, address); 128592181f19SNick Piggin return; 1286c61e211dSHarvey Harrison } 1287c61e211dSHarvey Harrison 12882d4a7167SIngo Molnar /* kprobes don't want to hook the spurious faults: */ 1289e00b12e6SPeter Zijlstra if (unlikely(kprobes_fault(regs))) 12909be260a6SMasami Hiramatsu return; 1291e00b12e6SPeter Zijlstra 1292164477c2SDave Hansen if (unlikely(hw_error_code & X86_PF_RSVD)) 1293164477c2SDave Hansen pgtable_bad(regs, hw_error_code, address); 1294e00b12e6SPeter Zijlstra 1295164477c2SDave Hansen if (unlikely(smap_violation(hw_error_code, regs))) { 1296164477c2SDave Hansen bad_area_nosemaphore(regs, hw_error_code, address, NULL); 1297e00b12e6SPeter Zijlstra return; 1298e00b12e6SPeter Zijlstra } 1299e00b12e6SPeter Zijlstra 1300e00b12e6SPeter Zijlstra /* 1301e00b12e6SPeter Zijlstra * If we're in an interrupt, have no user context or are running 130270ffdb93SDavid Hildenbrand * in a region with pagefaults disabled then we must not take the fault 1303e00b12e6SPeter Zijlstra */ 130470ffdb93SDavid Hildenbrand if (unlikely(faulthandler_disabled() || !mm)) { 1305164477c2SDave Hansen bad_area_nosemaphore(regs, hw_error_code, address, NULL); 1306e00b12e6SPeter Zijlstra return; 1307e00b12e6SPeter Zijlstra } 1308e00b12e6SPeter Zijlstra 1309c61e211dSHarvey Harrison /* 1310164477c2SDave Hansen * hw_error_code is literally the "page fault error code" passed to 1311164477c2SDave Hansen * the kernel directly from the hardware. But, we will shortly be 1312164477c2SDave Hansen * modifying it in software, so give it a new name. 1313164477c2SDave Hansen */ 1314164477c2SDave Hansen sw_error_code = hw_error_code; 1315164477c2SDave Hansen 1316164477c2SDave Hansen /* 1317891cffbdSLinus Torvalds * It's safe to allow irq's after cr2 has been saved and the 1318891cffbdSLinus Torvalds * vmalloc fault has been handled. 1319891cffbdSLinus Torvalds * 1320891cffbdSLinus Torvalds * User-mode registers count as a user access even for any 13212d4a7167SIngo Molnar * potential system fault or CPU buglet: 1322c61e211dSHarvey Harrison */ 1323f39b6f0eSAndy Lutomirski if (user_mode(regs)) { 1324891cffbdSLinus Torvalds local_irq_enable(); 1325164477c2SDave Hansen /* 1326164477c2SDave Hansen * Up to this point, X86_PF_USER set in hw_error_code 1327164477c2SDave Hansen * indicated a user-mode access. But, after this, 1328164477c2SDave Hansen * X86_PF_USER in sw_error_code will indicate either 1329164477c2SDave Hansen * that, *or* an implicit kernel(supervisor)-mode access 1330164477c2SDave Hansen * which originated from user mode. 1331164477c2SDave Hansen */ 1332164477c2SDave Hansen if (!(hw_error_code & X86_PF_USER)) { 1333164477c2SDave Hansen /* 1334164477c2SDave Hansen * The CPU was in user mode, but the CPU says 1335164477c2SDave Hansen * the fault was not a user-mode access. 1336164477c2SDave Hansen * Must be an implicit kernel-mode access, 1337164477c2SDave Hansen * which we do not expect to happen in the 1338164477c2SDave Hansen * user address space. 1339164477c2SDave Hansen */ 1340164477c2SDave Hansen pr_warn_once("kernel-mode error from user-mode: %lx\n", 1341164477c2SDave Hansen hw_error_code); 1342164477c2SDave Hansen 1343164477c2SDave Hansen sw_error_code |= X86_PF_USER; 1344164477c2SDave Hansen } 1345759496baSJohannes Weiner flags |= FAULT_FLAG_USER; 13462d4a7167SIngo Molnar } else { 13472d4a7167SIngo Molnar if (regs->flags & X86_EFLAGS_IF) 1348c61e211dSHarvey Harrison local_irq_enable(); 13492d4a7167SIngo Molnar } 1350c61e211dSHarvey Harrison 1351a8b0ca17SPeter Zijlstra perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 13527dd1fcc2SPeter Zijlstra 1353164477c2SDave Hansen if (sw_error_code & X86_PF_WRITE) 1354759496baSJohannes Weiner flags |= FAULT_FLAG_WRITE; 1355164477c2SDave Hansen if (sw_error_code & X86_PF_INSTR) 1356d61172b4SDave Hansen flags |= FAULT_FLAG_INSTRUCTION; 1357759496baSJohannes Weiner 13583a1dfe6eSIngo Molnar /* 13593a1dfe6eSIngo Molnar * When running in the kernel we expect faults to occur only to 13602d4a7167SIngo Molnar * addresses in user space. All other faults represent errors in 13612d4a7167SIngo Molnar * the kernel and should generate an OOPS. Unfortunately, in the 13622d4a7167SIngo Molnar * case of an erroneous fault occurring in a code path which already 13632d4a7167SIngo Molnar * holds mmap_sem we will deadlock attempting to validate the fault 13642d4a7167SIngo Molnar * against the address space. Luckily the kernel only validly 13652d4a7167SIngo Molnar * references user space from well defined areas of code, which are 13662d4a7167SIngo Molnar * listed in the exceptions table. 1367c61e211dSHarvey Harrison * 1368c61e211dSHarvey Harrison * As the vast majority of faults will be valid we will only perform 13692d4a7167SIngo Molnar * the source reference check when there is a possibility of a 13702d4a7167SIngo Molnar * deadlock. Attempt to lock the address space, if we cannot we then 13712d4a7167SIngo Molnar * validate the source. If this is invalid we can skip the address 13722d4a7167SIngo Molnar * space check, thus avoiding the deadlock: 1373c61e211dSHarvey Harrison */ 137492181f19SNick Piggin if (unlikely(!down_read_trylock(&mm->mmap_sem))) { 1375164477c2SDave Hansen if (!(sw_error_code & X86_PF_USER) && 137692181f19SNick Piggin !search_exception_tables(regs->ip)) { 1377164477c2SDave Hansen bad_area_nosemaphore(regs, sw_error_code, address, NULL); 137892181f19SNick Piggin return; 137992181f19SNick Piggin } 1380d065bd81SMichel Lespinasse retry: 1381c61e211dSHarvey Harrison down_read(&mm->mmap_sem); 138201006074SPeter Zijlstra } else { 138301006074SPeter Zijlstra /* 13842d4a7167SIngo Molnar * The above down_read_trylock() might have succeeded in 13852d4a7167SIngo Molnar * which case we'll have missed the might_sleep() from 13862d4a7167SIngo Molnar * down_read(): 138701006074SPeter Zijlstra */ 138801006074SPeter Zijlstra might_sleep(); 1389c61e211dSHarvey Harrison } 1390c61e211dSHarvey Harrison 1391c61e211dSHarvey Harrison vma = find_vma(mm, address); 139292181f19SNick Piggin if (unlikely(!vma)) { 1393164477c2SDave Hansen bad_area(regs, sw_error_code, address); 139492181f19SNick Piggin return; 139592181f19SNick Piggin } 139692181f19SNick Piggin if (likely(vma->vm_start <= address)) 1397c61e211dSHarvey Harrison goto good_area; 139892181f19SNick Piggin if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) { 1399164477c2SDave Hansen bad_area(regs, sw_error_code, address); 140092181f19SNick Piggin return; 140192181f19SNick Piggin } 1402164477c2SDave Hansen if (sw_error_code & X86_PF_USER) { 1403c61e211dSHarvey Harrison /* 1404c61e211dSHarvey Harrison * Accessing the stack below %sp is always a bug. 1405c61e211dSHarvey Harrison * The large cushion allows instructions like enter 1406c61e211dSHarvey Harrison * and pusha to work. ("enter $65535, $31" pushes 1407c61e211dSHarvey Harrison * 32 pointers and then decrements %sp by 65535.) 1408c61e211dSHarvey Harrison */ 140992181f19SNick Piggin if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) { 1410164477c2SDave Hansen bad_area(regs, sw_error_code, address); 141192181f19SNick Piggin return; 1412c61e211dSHarvey Harrison } 141392181f19SNick Piggin } 141492181f19SNick Piggin if (unlikely(expand_stack(vma, address))) { 1415164477c2SDave Hansen bad_area(regs, sw_error_code, address); 141692181f19SNick Piggin return; 141792181f19SNick Piggin } 141892181f19SNick Piggin 1419c61e211dSHarvey Harrison /* 1420c61e211dSHarvey Harrison * Ok, we have a good vm_area for this memory access, so 1421c61e211dSHarvey Harrison * we can handle it.. 1422c61e211dSHarvey Harrison */ 1423c61e211dSHarvey Harrison good_area: 1424164477c2SDave Hansen if (unlikely(access_error(sw_error_code, vma))) { 1425164477c2SDave Hansen bad_area_access_error(regs, sw_error_code, address, vma); 142692181f19SNick Piggin return; 1427c61e211dSHarvey Harrison } 1428c61e211dSHarvey Harrison 1429c61e211dSHarvey Harrison /* 1430c61e211dSHarvey Harrison * If for any reason at all we couldn't handle the fault, 1431c61e211dSHarvey Harrison * make sure we exit gracefully rather than endlessly redo 14329a95f3cfSPaul Cassella * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if 14339a95f3cfSPaul Cassella * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked. 1434cb0631fdSVlastimil Babka * 1435cb0631fdSVlastimil Babka * Note that handle_userfault() may also release and reacquire mmap_sem 1436cb0631fdSVlastimil Babka * (and not return with VM_FAULT_RETRY), when returning to userland to 1437cb0631fdSVlastimil Babka * repeat the page fault later with a VM_FAULT_NOPAGE retval 1438cb0631fdSVlastimil Babka * (potentially after handling any pending signal during the return to 1439cb0631fdSVlastimil Babka * userland). The return to userland is identified whenever 1440cb0631fdSVlastimil Babka * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags. 1441cb0631fdSVlastimil Babka * Thus we have to be careful about not touching vma after handling the 1442cb0631fdSVlastimil Babka * fault, so we read the pkey beforehand. 1443c61e211dSHarvey Harrison */ 1444cb0631fdSVlastimil Babka pkey = vma_pkey(vma); 1445dcddffd4SKirill A. Shutemov fault = handle_mm_fault(vma, address, flags); 144626178ec1SLinus Torvalds major |= fault & VM_FAULT_MAJOR; 14472d4a7167SIngo Molnar 14483a13c4d7SJohannes Weiner /* 144926178ec1SLinus Torvalds * If we need to retry the mmap_sem has already been released, 145026178ec1SLinus Torvalds * and if there is a fatal signal pending there is no guarantee 145126178ec1SLinus Torvalds * that we made any progress. Handle this case first. 14523a13c4d7SJohannes Weiner */ 145326178ec1SLinus Torvalds if (unlikely(fault & VM_FAULT_RETRY)) { 145426178ec1SLinus Torvalds /* Retry at most once */ 145526178ec1SLinus Torvalds if (flags & FAULT_FLAG_ALLOW_RETRY) { 145626178ec1SLinus Torvalds flags &= ~FAULT_FLAG_ALLOW_RETRY; 145726178ec1SLinus Torvalds flags |= FAULT_FLAG_TRIED; 145826178ec1SLinus Torvalds if (!fatal_signal_pending(tsk)) 145926178ec1SLinus Torvalds goto retry; 146026178ec1SLinus Torvalds } 146126178ec1SLinus Torvalds 146226178ec1SLinus Torvalds /* User mode? Just return to handle the fatal exception */ 1463cf3c0a15SLinus Torvalds if (flags & FAULT_FLAG_USER) 14643a13c4d7SJohannes Weiner return; 14653a13c4d7SJohannes Weiner 146626178ec1SLinus Torvalds /* Not returning to user mode? Handle exceptions or die: */ 1467164477c2SDave Hansen no_context(regs, sw_error_code, address, SIGBUS, BUS_ADRERR); 146826178ec1SLinus Torvalds return; 146926178ec1SLinus Torvalds } 147026178ec1SLinus Torvalds 14717fb08ecaSLinus Torvalds up_read(&mm->mmap_sem); 147226178ec1SLinus Torvalds if (unlikely(fault & VM_FAULT_ERROR)) { 1473164477c2SDave Hansen mm_fault_error(regs, sw_error_code, address, &pkey, fault); 147437b23e05SKOSAKI Motohiro return; 147537b23e05SKOSAKI Motohiro } 147637b23e05SKOSAKI Motohiro 147737b23e05SKOSAKI Motohiro /* 147826178ec1SLinus Torvalds * Major/minor page fault accounting. If any of the events 147926178ec1SLinus Torvalds * returned VM_FAULT_MAJOR, we account it as a major fault. 1480d065bd81SMichel Lespinasse */ 148126178ec1SLinus Torvalds if (major) { 1482c61e211dSHarvey Harrison tsk->maj_flt++; 148326178ec1SLinus Torvalds perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address); 1484ac17dc8eSPeter Zijlstra } else { 1485c61e211dSHarvey Harrison tsk->min_flt++; 148626178ec1SLinus Torvalds perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address); 1487d065bd81SMichel Lespinasse } 1488c61e211dSHarvey Harrison 14898c938f9fSIngo Molnar check_v8086_mode(regs, address, tsk); 1490c61e211dSHarvey Harrison } 14919326638cSMasami Hiramatsu NOKPROBE_SYMBOL(__do_page_fault); 14926ba3c97aSFrederic Weisbecker 14939326638cSMasami Hiramatsu static nokprobe_inline void 14949326638cSMasami Hiramatsu trace_page_fault_entries(unsigned long address, struct pt_regs *regs, 1495d34603b0SSeiji Aguchi unsigned long error_code) 1496d34603b0SSeiji Aguchi { 1497d34603b0SSeiji Aguchi if (user_mode(regs)) 1498d4078e23SPeter Zijlstra trace_page_fault_user(address, regs, error_code); 1499d34603b0SSeiji Aguchi else 1500d4078e23SPeter Zijlstra trace_page_fault_kernel(address, regs, error_code); 1501d34603b0SSeiji Aguchi } 1502d34603b0SSeiji Aguchi 15030ac09f9fSJiri Olsa /* 150411a7ffb0SThomas Gleixner * We must have this function blacklisted from kprobes, tagged with notrace 150511a7ffb0SThomas Gleixner * and call read_cr2() before calling anything else. To avoid calling any 150611a7ffb0SThomas Gleixner * kind of tracing machinery before we've observed the CR2 value. 150711a7ffb0SThomas Gleixner * 150811a7ffb0SThomas Gleixner * exception_{enter,exit}() contains all sorts of tracepoints. 15090ac09f9fSJiri Olsa */ 151011a7ffb0SThomas Gleixner dotraplinkage void notrace 151111a7ffb0SThomas Gleixner do_page_fault(struct pt_regs *regs, unsigned long error_code) 151211a7ffb0SThomas Gleixner { 151311a7ffb0SThomas Gleixner unsigned long address = read_cr2(); /* Get the faulting address */ 1514d4078e23SPeter Zijlstra enum ctx_state prev_state; 151525c74b10SSeiji Aguchi 151625c74b10SSeiji Aguchi prev_state = exception_enter(); 151780954747SThomas Gleixner if (trace_pagefault_enabled()) 1518d4078e23SPeter Zijlstra trace_page_fault_entries(address, regs, error_code); 151911a7ffb0SThomas Gleixner 15200ac09f9fSJiri Olsa __do_page_fault(regs, error_code, address); 152125c74b10SSeiji Aguchi exception_exit(prev_state); 152225c74b10SSeiji Aguchi } 152311a7ffb0SThomas Gleixner NOKPROBE_SYMBOL(do_page_fault); 1524