18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 1995 Linus Torvalds 48c2ecf20Sopenharmony_ci * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs. 58c2ecf20Sopenharmony_ci * Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci#include <linux/sched.h> /* test_thread_flag(), ... */ 88c2ecf20Sopenharmony_ci#include <linux/sched/task_stack.h> /* task_stack_*(), ... */ 98c2ecf20Sopenharmony_ci#include <linux/kdebug.h> /* oops_begin/end, ... */ 108c2ecf20Sopenharmony_ci#include <linux/extable.h> /* search_exception_tables */ 118c2ecf20Sopenharmony_ci#include <linux/memblock.h> /* max_low_pfn */ 128c2ecf20Sopenharmony_ci#include <linux/kprobes.h> /* NOKPROBE_SYMBOL, ... */ 138c2ecf20Sopenharmony_ci#include <linux/mmiotrace.h> /* kmmio_handler, ... */ 148c2ecf20Sopenharmony_ci#include <linux/perf_event.h> /* perf_sw_event */ 158c2ecf20Sopenharmony_ci#include <linux/hugetlb.h> /* hstate_index_to_shift */ 168c2ecf20Sopenharmony_ci#include <linux/prefetch.h> /* prefetchw */ 178c2ecf20Sopenharmony_ci#include <linux/context_tracking.h> /* exception_enter(), ... */ 188c2ecf20Sopenharmony_ci#include <linux/uaccess.h> /* faulthandler_disabled() */ 198c2ecf20Sopenharmony_ci#include <linux/efi.h> /* efi_recover_from_page_fault()*/ 208c2ecf20Sopenharmony_ci#include <linux/mm_types.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <asm/cpufeature.h> /* boot_cpu_has, ... */ 238c2ecf20Sopenharmony_ci#include <asm/traps.h> /* dotraplinkage, ... */ 248c2ecf20Sopenharmony_ci#include <asm/fixmap.h> /* VSYSCALL_ADDR */ 258c2ecf20Sopenharmony_ci#include <asm/vsyscall.h> /* emulate_vsyscall */ 268c2ecf20Sopenharmony_ci#include <asm/vm86.h> /* struct vm86 */ 278c2ecf20Sopenharmony_ci#include <asm/mmu_context.h> /* vma_pkey() */ 288c2ecf20Sopenharmony_ci#include <asm/efi.h> /* efi_recover_from_page_fault()*/ 298c2ecf20Sopenharmony_ci#include <asm/desc.h> /* store_idt(), ... */ 308c2ecf20Sopenharmony_ci#include <asm/cpu_entry_area.h> /* exception stack */ 318c2ecf20Sopenharmony_ci#include <asm/pgtable_areas.h> /* VMALLOC_START, ... */ 328c2ecf20Sopenharmony_ci#include <asm/kvm_para.h> /* kvm_handle_async_pf */ 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#define CREATE_TRACE_POINTS 358c2ecf20Sopenharmony_ci#include <asm/trace/exceptions.h> 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/* 388c2ecf20Sopenharmony_ci * Returns 0 if mmiotrace is disabled, or if the fault is not 398c2ecf20Sopenharmony_ci * handled by mmiotrace: 408c2ecf20Sopenharmony_ci */ 418c2ecf20Sopenharmony_cistatic nokprobe_inline int 428c2ecf20Sopenharmony_cikmmio_fault(struct pt_regs *regs, unsigned long addr) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci if (unlikely(is_kmmio_active())) 458c2ecf20Sopenharmony_ci if (kmmio_handler(regs, addr) == 1) 468c2ecf20Sopenharmony_ci return -1; 478c2ecf20Sopenharmony_ci return 0; 488c2ecf20Sopenharmony_ci} 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* 518c2ecf20Sopenharmony_ci * Prefetch quirks: 528c2ecf20Sopenharmony_ci * 538c2ecf20Sopenharmony_ci * 32-bit mode: 548c2ecf20Sopenharmony_ci * 558c2ecf20Sopenharmony_ci * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch. 568c2ecf20Sopenharmony_ci * Check that here and ignore it. This is AMD erratum #91. 578c2ecf20Sopenharmony_ci * 588c2ecf20Sopenharmony_ci * 64-bit mode: 598c2ecf20Sopenharmony_ci * 608c2ecf20Sopenharmony_ci * Sometimes the CPU reports invalid exceptions on prefetch. 618c2ecf20Sopenharmony_ci * Check that here and ignore it. 628c2ecf20Sopenharmony_ci * 638c2ecf20Sopenharmony_ci * Opcode checker based on code by Richard Brunner. 648c2ecf20Sopenharmony_ci */ 658c2ecf20Sopenharmony_cistatic inline int 668c2ecf20Sopenharmony_cicheck_prefetch_opcode(struct pt_regs *regs, unsigned char *instr, 678c2ecf20Sopenharmony_ci unsigned char opcode, int *prefetch) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci unsigned char instr_hi = opcode & 0xf0; 708c2ecf20Sopenharmony_ci unsigned char instr_lo = opcode & 0x0f; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci switch (instr_hi) { 738c2ecf20Sopenharmony_ci case 0x20: 748c2ecf20Sopenharmony_ci case 0x30: 758c2ecf20Sopenharmony_ci /* 768c2ecf20Sopenharmony_ci * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes. 778c2ecf20Sopenharmony_ci * In X86_64 long mode, the CPU will signal invalid 788c2ecf20Sopenharmony_ci * opcode if some of these prefixes are present so 798c2ecf20Sopenharmony_ci * X86_64 will never get here anyway 808c2ecf20Sopenharmony_ci */ 818c2ecf20Sopenharmony_ci return ((instr_lo & 7) == 0x6); 828c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64 838c2ecf20Sopenharmony_ci case 0x40: 848c2ecf20Sopenharmony_ci /* 858c2ecf20Sopenharmony_ci * In 64-bit mode 0x40..0x4F are valid REX prefixes 868c2ecf20Sopenharmony_ci */ 878c2ecf20Sopenharmony_ci return (!user_mode(regs) || user_64bit_mode(regs)); 888c2ecf20Sopenharmony_ci#endif 898c2ecf20Sopenharmony_ci case 0x60: 908c2ecf20Sopenharmony_ci /* 0x64 thru 0x67 are valid prefixes in all modes. */ 918c2ecf20Sopenharmony_ci return (instr_lo & 0xC) == 0x4; 928c2ecf20Sopenharmony_ci case 0xF0: 938c2ecf20Sopenharmony_ci /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */ 948c2ecf20Sopenharmony_ci return !instr_lo || (instr_lo>>1) == 1; 958c2ecf20Sopenharmony_ci case 0x00: 968c2ecf20Sopenharmony_ci /* Prefetch instruction is 0x0F0D or 0x0F18 */ 978c2ecf20Sopenharmony_ci if (get_kernel_nofault(opcode, instr)) 988c2ecf20Sopenharmony_ci return 0; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci *prefetch = (instr_lo == 0xF) && 1018c2ecf20Sopenharmony_ci (opcode == 0x0D || opcode == 0x18); 1028c2ecf20Sopenharmony_ci return 0; 1038c2ecf20Sopenharmony_ci default: 1048c2ecf20Sopenharmony_ci return 0; 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic int 1098c2ecf20Sopenharmony_ciis_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci unsigned char *max_instr; 1128c2ecf20Sopenharmony_ci unsigned char *instr; 1138c2ecf20Sopenharmony_ci int prefetch = 0; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci /* 1168c2ecf20Sopenharmony_ci * If it was a exec (instruction fetch) fault on NX page, then 1178c2ecf20Sopenharmony_ci * do not ignore the fault: 1188c2ecf20Sopenharmony_ci */ 1198c2ecf20Sopenharmony_ci if (error_code & X86_PF_INSTR) 1208c2ecf20Sopenharmony_ci return 0; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci instr = (void *)convert_ip_to_linear(current, regs); 1238c2ecf20Sopenharmony_ci max_instr = instr + 15; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci /* 1268c2ecf20Sopenharmony_ci * This code has historically always bailed out if IP points to a 1278c2ecf20Sopenharmony_ci * not-present page (e.g. due to a race). No one has ever 1288c2ecf20Sopenharmony_ci * complained about this. 1298c2ecf20Sopenharmony_ci */ 1308c2ecf20Sopenharmony_ci pagefault_disable(); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci while (instr < max_instr) { 1338c2ecf20Sopenharmony_ci unsigned char opcode; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci if (user_mode(regs)) { 1368c2ecf20Sopenharmony_ci if (get_user(opcode, instr)) 1378c2ecf20Sopenharmony_ci break; 1388c2ecf20Sopenharmony_ci } else { 1398c2ecf20Sopenharmony_ci if (get_kernel_nofault(opcode, instr)) 1408c2ecf20Sopenharmony_ci break; 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci instr++; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci if (!check_prefetch_opcode(regs, instr, opcode, &prefetch)) 1468c2ecf20Sopenharmony_ci break; 1478c2ecf20Sopenharmony_ci } 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci pagefault_enable(); 1508c2ecf20Sopenharmony_ci return prefetch; 1518c2ecf20Sopenharmony_ci} 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ciDEFINE_SPINLOCK(pgd_lock); 1548c2ecf20Sopenharmony_ciLIST_HEAD(pgd_list); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32 1578c2ecf20Sopenharmony_cistatic inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci unsigned index = pgd_index(address); 1608c2ecf20Sopenharmony_ci pgd_t *pgd_k; 1618c2ecf20Sopenharmony_ci p4d_t *p4d, *p4d_k; 1628c2ecf20Sopenharmony_ci pud_t *pud, *pud_k; 1638c2ecf20Sopenharmony_ci pmd_t *pmd, *pmd_k; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci pgd += index; 1668c2ecf20Sopenharmony_ci pgd_k = init_mm.pgd + index; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci if (!pgd_present(*pgd_k)) 1698c2ecf20Sopenharmony_ci return NULL; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci /* 1728c2ecf20Sopenharmony_ci * set_pgd(pgd, *pgd_k); here would be useless on PAE 1738c2ecf20Sopenharmony_ci * and redundant with the set_pmd() on non-PAE. As would 1748c2ecf20Sopenharmony_ci * set_p4d/set_pud. 1758c2ecf20Sopenharmony_ci */ 1768c2ecf20Sopenharmony_ci p4d = p4d_offset(pgd, address); 1778c2ecf20Sopenharmony_ci p4d_k = p4d_offset(pgd_k, address); 1788c2ecf20Sopenharmony_ci if (!p4d_present(*p4d_k)) 1798c2ecf20Sopenharmony_ci return NULL; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci pud = pud_offset(p4d, address); 1828c2ecf20Sopenharmony_ci pud_k = pud_offset(p4d_k, address); 1838c2ecf20Sopenharmony_ci if (!pud_present(*pud_k)) 1848c2ecf20Sopenharmony_ci return NULL; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci pmd = pmd_offset(pud, address); 1878c2ecf20Sopenharmony_ci pmd_k = pmd_offset(pud_k, address); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci if (pmd_present(*pmd) != pmd_present(*pmd_k)) 1908c2ecf20Sopenharmony_ci set_pmd(pmd, *pmd_k); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci if (!pmd_present(*pmd_k)) 1938c2ecf20Sopenharmony_ci return NULL; 1948c2ecf20Sopenharmony_ci else 1958c2ecf20Sopenharmony_ci BUG_ON(pmd_pfn(*pmd) != pmd_pfn(*pmd_k)); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci return pmd_k; 1988c2ecf20Sopenharmony_ci} 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci/* 2018c2ecf20Sopenharmony_ci * Handle a fault on the vmalloc or module mapping area 2028c2ecf20Sopenharmony_ci * 2038c2ecf20Sopenharmony_ci * This is needed because there is a race condition between the time 2048c2ecf20Sopenharmony_ci * when the vmalloc mapping code updates the PMD to the point in time 2058c2ecf20Sopenharmony_ci * where it synchronizes this update with the other page-tables in the 2068c2ecf20Sopenharmony_ci * system. 2078c2ecf20Sopenharmony_ci * 2088c2ecf20Sopenharmony_ci * In this race window another thread/CPU can map an area on the same 2098c2ecf20Sopenharmony_ci * PMD, finds it already present and does not synchronize it with the 2108c2ecf20Sopenharmony_ci * rest of the system yet. As a result v[mz]alloc might return areas 2118c2ecf20Sopenharmony_ci * which are not mapped in every page-table in the system, causing an 2128c2ecf20Sopenharmony_ci * unhandled page-fault when they are accessed. 2138c2ecf20Sopenharmony_ci */ 2148c2ecf20Sopenharmony_cistatic noinline int vmalloc_fault(unsigned long address) 2158c2ecf20Sopenharmony_ci{ 2168c2ecf20Sopenharmony_ci unsigned long pgd_paddr; 2178c2ecf20Sopenharmony_ci pmd_t *pmd_k; 2188c2ecf20Sopenharmony_ci pte_t *pte_k; 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci /* Make sure we are in vmalloc area: */ 2218c2ecf20Sopenharmony_ci if (!(address >= VMALLOC_START && address < VMALLOC_END)) 2228c2ecf20Sopenharmony_ci return -1; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci /* 2258c2ecf20Sopenharmony_ci * Synchronize this task's top level page-table 2268c2ecf20Sopenharmony_ci * with the 'reference' page table. 2278c2ecf20Sopenharmony_ci * 2288c2ecf20Sopenharmony_ci * Do _not_ use "current" here. We might be inside 2298c2ecf20Sopenharmony_ci * an interrupt in the middle of a task switch.. 2308c2ecf20Sopenharmony_ci */ 2318c2ecf20Sopenharmony_ci pgd_paddr = read_cr3_pa(); 2328c2ecf20Sopenharmony_ci pmd_k = vmalloc_sync_one(__va(pgd_paddr), address); 2338c2ecf20Sopenharmony_ci if (!pmd_k) 2348c2ecf20Sopenharmony_ci return -1; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci if (pmd_large(*pmd_k)) 2378c2ecf20Sopenharmony_ci return 0; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci pte_k = pte_offset_kernel(pmd_k, address); 2408c2ecf20Sopenharmony_ci if (!pte_present(*pte_k)) 2418c2ecf20Sopenharmony_ci return -1; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci return 0; 2448c2ecf20Sopenharmony_ci} 2458c2ecf20Sopenharmony_ciNOKPROBE_SYMBOL(vmalloc_fault); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_civoid arch_sync_kernel_mappings(unsigned long start, unsigned long end) 2488c2ecf20Sopenharmony_ci{ 2498c2ecf20Sopenharmony_ci unsigned long addr; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci for (addr = start & PMD_MASK; 2528c2ecf20Sopenharmony_ci addr >= TASK_SIZE_MAX && addr < VMALLOC_END; 2538c2ecf20Sopenharmony_ci addr += PMD_SIZE) { 2548c2ecf20Sopenharmony_ci struct page *page; 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci spin_lock(&pgd_lock); 2578c2ecf20Sopenharmony_ci list_for_each_entry(page, &pgd_list, lru) { 2588c2ecf20Sopenharmony_ci spinlock_t *pgt_lock; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci /* the pgt_lock only for Xen */ 2618c2ecf20Sopenharmony_ci pgt_lock = &pgd_page_get_mm(page)->page_table_lock; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci spin_lock(pgt_lock); 2648c2ecf20Sopenharmony_ci vmalloc_sync_one(page_address(page), addr); 2658c2ecf20Sopenharmony_ci spin_unlock(pgt_lock); 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci spin_unlock(&pgd_lock); 2688c2ecf20Sopenharmony_ci } 2698c2ecf20Sopenharmony_ci} 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci/* 2728c2ecf20Sopenharmony_ci * Did it hit the DOS screen memory VA from vm86 mode? 2738c2ecf20Sopenharmony_ci */ 2748c2ecf20Sopenharmony_cistatic inline void 2758c2ecf20Sopenharmony_cicheck_v8086_mode(struct pt_regs *regs, unsigned long address, 2768c2ecf20Sopenharmony_ci struct task_struct *tsk) 2778c2ecf20Sopenharmony_ci{ 2788c2ecf20Sopenharmony_ci#ifdef CONFIG_VM86 2798c2ecf20Sopenharmony_ci unsigned long bit; 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci if (!v8086_mode(regs) || !tsk->thread.vm86) 2828c2ecf20Sopenharmony_ci return; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci bit = (address - 0xA0000) >> PAGE_SHIFT; 2858c2ecf20Sopenharmony_ci if (bit < 32) 2868c2ecf20Sopenharmony_ci tsk->thread.vm86->screen_bitmap |= 1 << bit; 2878c2ecf20Sopenharmony_ci#endif 2888c2ecf20Sopenharmony_ci} 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_cistatic bool low_pfn(unsigned long pfn) 2918c2ecf20Sopenharmony_ci{ 2928c2ecf20Sopenharmony_ci return pfn < max_low_pfn; 2938c2ecf20Sopenharmony_ci} 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_cistatic void dump_pagetable(unsigned long address) 2968c2ecf20Sopenharmony_ci{ 2978c2ecf20Sopenharmony_ci pgd_t *base = __va(read_cr3_pa()); 2988c2ecf20Sopenharmony_ci pgd_t *pgd = &base[pgd_index(address)]; 2998c2ecf20Sopenharmony_ci p4d_t *p4d; 3008c2ecf20Sopenharmony_ci pud_t *pud; 3018c2ecf20Sopenharmony_ci pmd_t *pmd; 3028c2ecf20Sopenharmony_ci pte_t *pte; 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_PAE 3058c2ecf20Sopenharmony_ci pr_info("*pdpt = %016Lx ", pgd_val(*pgd)); 3068c2ecf20Sopenharmony_ci if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd)) 3078c2ecf20Sopenharmony_ci goto out; 3088c2ecf20Sopenharmony_ci#define pr_pde pr_cont 3098c2ecf20Sopenharmony_ci#else 3108c2ecf20Sopenharmony_ci#define pr_pde pr_info 3118c2ecf20Sopenharmony_ci#endif 3128c2ecf20Sopenharmony_ci p4d = p4d_offset(pgd, address); 3138c2ecf20Sopenharmony_ci pud = pud_offset(p4d, address); 3148c2ecf20Sopenharmony_ci pmd = pmd_offset(pud, address); 3158c2ecf20Sopenharmony_ci pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd)); 3168c2ecf20Sopenharmony_ci#undef pr_pde 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ci /* 3198c2ecf20Sopenharmony_ci * We must not directly access the pte in the highpte 3208c2ecf20Sopenharmony_ci * case if the page table is located in highmem. 3218c2ecf20Sopenharmony_ci * And let's rather not kmap-atomic the pte, just in case 3228c2ecf20Sopenharmony_ci * it's allocated already: 3238c2ecf20Sopenharmony_ci */ 3248c2ecf20Sopenharmony_ci if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd)) 3258c2ecf20Sopenharmony_ci goto out; 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci pte = pte_offset_kernel(pmd, address); 3288c2ecf20Sopenharmony_ci pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte)); 3298c2ecf20Sopenharmony_ciout: 3308c2ecf20Sopenharmony_ci pr_cont("\n"); 3318c2ecf20Sopenharmony_ci} 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci#else /* CONFIG_X86_64: */ 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci#ifdef CONFIG_CPU_SUP_AMD 3368c2ecf20Sopenharmony_cistatic const char errata93_warning[] = 3378c2ecf20Sopenharmony_ciKERN_ERR 3388c2ecf20Sopenharmony_ci"******* Your BIOS seems to not contain a fix for K8 errata #93\n" 3398c2ecf20Sopenharmony_ci"******* Working around it, but it may cause SEGVs or burn power.\n" 3408c2ecf20Sopenharmony_ci"******* Please consider a BIOS update.\n" 3418c2ecf20Sopenharmony_ci"******* Disabling USB legacy in the BIOS may also help.\n"; 3428c2ecf20Sopenharmony_ci#endif 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci/* 3458c2ecf20Sopenharmony_ci * No vm86 mode in 64-bit mode: 3468c2ecf20Sopenharmony_ci */ 3478c2ecf20Sopenharmony_cistatic inline void 3488c2ecf20Sopenharmony_cicheck_v8086_mode(struct pt_regs *regs, unsigned long address, 3498c2ecf20Sopenharmony_ci struct task_struct *tsk) 3508c2ecf20Sopenharmony_ci{ 3518c2ecf20Sopenharmony_ci} 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_cistatic int bad_address(void *p) 3548c2ecf20Sopenharmony_ci{ 3558c2ecf20Sopenharmony_ci unsigned long dummy; 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci return get_kernel_nofault(dummy, (unsigned long *)p); 3588c2ecf20Sopenharmony_ci} 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_cistatic void dump_pagetable(unsigned long address) 3618c2ecf20Sopenharmony_ci{ 3628c2ecf20Sopenharmony_ci pgd_t *base = __va(read_cr3_pa()); 3638c2ecf20Sopenharmony_ci pgd_t *pgd = base + pgd_index(address); 3648c2ecf20Sopenharmony_ci p4d_t *p4d; 3658c2ecf20Sopenharmony_ci pud_t *pud; 3668c2ecf20Sopenharmony_ci pmd_t *pmd; 3678c2ecf20Sopenharmony_ci pte_t *pte; 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci if (bad_address(pgd)) 3708c2ecf20Sopenharmony_ci goto bad; 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci pr_info("PGD %lx ", pgd_val(*pgd)); 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci if (!pgd_present(*pgd)) 3758c2ecf20Sopenharmony_ci goto out; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci p4d = p4d_offset(pgd, address); 3788c2ecf20Sopenharmony_ci if (bad_address(p4d)) 3798c2ecf20Sopenharmony_ci goto bad; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci pr_cont("P4D %lx ", p4d_val(*p4d)); 3828c2ecf20Sopenharmony_ci if (!p4d_present(*p4d) || p4d_large(*p4d)) 3838c2ecf20Sopenharmony_ci goto out; 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci pud = pud_offset(p4d, address); 3868c2ecf20Sopenharmony_ci if (bad_address(pud)) 3878c2ecf20Sopenharmony_ci goto bad; 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci pr_cont("PUD %lx ", pud_val(*pud)); 3908c2ecf20Sopenharmony_ci if (!pud_present(*pud) || pud_large(*pud)) 3918c2ecf20Sopenharmony_ci goto out; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci pmd = pmd_offset(pud, address); 3948c2ecf20Sopenharmony_ci if (bad_address(pmd)) 3958c2ecf20Sopenharmony_ci goto bad; 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci pr_cont("PMD %lx ", pmd_val(*pmd)); 3988c2ecf20Sopenharmony_ci if (!pmd_present(*pmd) || pmd_large(*pmd)) 3998c2ecf20Sopenharmony_ci goto out; 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci pte = pte_offset_kernel(pmd, address); 4028c2ecf20Sopenharmony_ci if (bad_address(pte)) 4038c2ecf20Sopenharmony_ci goto bad; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci pr_cont("PTE %lx", pte_val(*pte)); 4068c2ecf20Sopenharmony_ciout: 4078c2ecf20Sopenharmony_ci pr_cont("\n"); 4088c2ecf20Sopenharmony_ci return; 4098c2ecf20Sopenharmony_cibad: 4108c2ecf20Sopenharmony_ci pr_info("BAD\n"); 4118c2ecf20Sopenharmony_ci} 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci#endif /* CONFIG_X86_64 */ 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci/* 4168c2ecf20Sopenharmony_ci * Workaround for K8 erratum #93 & buggy BIOS. 4178c2ecf20Sopenharmony_ci * 4188c2ecf20Sopenharmony_ci * BIOS SMM functions are required to use a specific workaround 4198c2ecf20Sopenharmony_ci * to avoid corruption of the 64bit RIP register on C stepping K8. 4208c2ecf20Sopenharmony_ci * 4218c2ecf20Sopenharmony_ci * A lot of BIOS that didn't get tested properly miss this. 4228c2ecf20Sopenharmony_ci * 4238c2ecf20Sopenharmony_ci * The OS sees this as a page fault with the upper 32bits of RIP cleared. 4248c2ecf20Sopenharmony_ci * Try to work around it here. 4258c2ecf20Sopenharmony_ci * 4268c2ecf20Sopenharmony_ci * Note we only handle faults in kernel here. 4278c2ecf20Sopenharmony_ci * Does nothing on 32-bit. 4288c2ecf20Sopenharmony_ci */ 4298c2ecf20Sopenharmony_cistatic int is_errata93(struct pt_regs *regs, unsigned long address) 4308c2ecf20Sopenharmony_ci{ 4318c2ecf20Sopenharmony_ci#if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD) 4328c2ecf20Sopenharmony_ci if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD 4338c2ecf20Sopenharmony_ci || boot_cpu_data.x86 != 0xf) 4348c2ecf20Sopenharmony_ci return 0; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci if (address != regs->ip) 4378c2ecf20Sopenharmony_ci return 0; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci if ((address >> 32) != 0) 4408c2ecf20Sopenharmony_ci return 0; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci address |= 0xffffffffUL << 32; 4438c2ecf20Sopenharmony_ci if ((address >= (u64)_stext && address <= (u64)_etext) || 4448c2ecf20Sopenharmony_ci (address >= MODULES_VADDR && address <= MODULES_END)) { 4458c2ecf20Sopenharmony_ci printk_once(errata93_warning); 4468c2ecf20Sopenharmony_ci regs->ip = address; 4478c2ecf20Sopenharmony_ci return 1; 4488c2ecf20Sopenharmony_ci } 4498c2ecf20Sopenharmony_ci#endif 4508c2ecf20Sopenharmony_ci return 0; 4518c2ecf20Sopenharmony_ci} 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci/* 4548c2ecf20Sopenharmony_ci * Work around K8 erratum #100 K8 in compat mode occasionally jumps 4558c2ecf20Sopenharmony_ci * to illegal addresses >4GB. 4568c2ecf20Sopenharmony_ci * 4578c2ecf20Sopenharmony_ci * We catch this in the page fault handler because these addresses 4588c2ecf20Sopenharmony_ci * are not reachable. Just detect this case and return. Any code 4598c2ecf20Sopenharmony_ci * segment in LDT is compatibility mode. 4608c2ecf20Sopenharmony_ci */ 4618c2ecf20Sopenharmony_cistatic int is_errata100(struct pt_regs *regs, unsigned long address) 4628c2ecf20Sopenharmony_ci{ 4638c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64 4648c2ecf20Sopenharmony_ci if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32)) 4658c2ecf20Sopenharmony_ci return 1; 4668c2ecf20Sopenharmony_ci#endif 4678c2ecf20Sopenharmony_ci return 0; 4688c2ecf20Sopenharmony_ci} 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ci/* Pentium F0 0F C7 C8 bug workaround: */ 4718c2ecf20Sopenharmony_cistatic int is_f00f_bug(struct pt_regs *regs, unsigned long address) 4728c2ecf20Sopenharmony_ci{ 4738c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_F00F_BUG 4748c2ecf20Sopenharmony_ci if (boot_cpu_has_bug(X86_BUG_F00F) && idt_is_f00f_address(address)) { 4758c2ecf20Sopenharmony_ci handle_invalid_op(regs); 4768c2ecf20Sopenharmony_ci return 1; 4778c2ecf20Sopenharmony_ci } 4788c2ecf20Sopenharmony_ci#endif 4798c2ecf20Sopenharmony_ci return 0; 4808c2ecf20Sopenharmony_ci} 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_cistatic void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index) 4838c2ecf20Sopenharmony_ci{ 4848c2ecf20Sopenharmony_ci u32 offset = (index >> 3) * sizeof(struct desc_struct); 4858c2ecf20Sopenharmony_ci unsigned long addr; 4868c2ecf20Sopenharmony_ci struct ldttss_desc desc; 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci if (index == 0) { 4898c2ecf20Sopenharmony_ci pr_alert("%s: NULL\n", name); 4908c2ecf20Sopenharmony_ci return; 4918c2ecf20Sopenharmony_ci } 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci if (offset + sizeof(struct ldttss_desc) >= gdt->size) { 4948c2ecf20Sopenharmony_ci pr_alert("%s: 0x%hx -- out of bounds\n", name, index); 4958c2ecf20Sopenharmony_ci return; 4968c2ecf20Sopenharmony_ci } 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci if (copy_from_kernel_nofault(&desc, (void *)(gdt->address + offset), 4998c2ecf20Sopenharmony_ci sizeof(struct ldttss_desc))) { 5008c2ecf20Sopenharmony_ci pr_alert("%s: 0x%hx -- GDT entry is not readable\n", 5018c2ecf20Sopenharmony_ci name, index); 5028c2ecf20Sopenharmony_ci return; 5038c2ecf20Sopenharmony_ci } 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci addr = desc.base0 | (desc.base1 << 16) | ((unsigned long)desc.base2 << 24); 5068c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64 5078c2ecf20Sopenharmony_ci addr |= ((u64)desc.base3 << 32); 5088c2ecf20Sopenharmony_ci#endif 5098c2ecf20Sopenharmony_ci pr_alert("%s: 0x%hx -- base=0x%lx limit=0x%x\n", 5108c2ecf20Sopenharmony_ci name, index, addr, (desc.limit0 | (desc.limit1 << 16))); 5118c2ecf20Sopenharmony_ci} 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_cistatic void 5148c2ecf20Sopenharmony_cishow_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long address) 5158c2ecf20Sopenharmony_ci{ 5168c2ecf20Sopenharmony_ci if (!oops_may_print()) 5178c2ecf20Sopenharmony_ci return; 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci if (error_code & X86_PF_INSTR) { 5208c2ecf20Sopenharmony_ci unsigned int level; 5218c2ecf20Sopenharmony_ci pgd_t *pgd; 5228c2ecf20Sopenharmony_ci pte_t *pte; 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci pgd = __va(read_cr3_pa()); 5258c2ecf20Sopenharmony_ci pgd += pgd_index(address); 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci pte = lookup_address_in_pgd(pgd, address, &level); 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci if (pte && pte_present(*pte) && !pte_exec(*pte)) 5308c2ecf20Sopenharmony_ci pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n", 5318c2ecf20Sopenharmony_ci from_kuid(&init_user_ns, current_uid())); 5328c2ecf20Sopenharmony_ci if (pte && pte_present(*pte) && pte_exec(*pte) && 5338c2ecf20Sopenharmony_ci (pgd_flags(*pgd) & _PAGE_USER) && 5348c2ecf20Sopenharmony_ci (__read_cr4() & X86_CR4_SMEP)) 5358c2ecf20Sopenharmony_ci pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n", 5368c2ecf20Sopenharmony_ci from_kuid(&init_user_ns, current_uid())); 5378c2ecf20Sopenharmony_ci } 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci if (address < PAGE_SIZE && !user_mode(regs)) 5408c2ecf20Sopenharmony_ci pr_alert("BUG: kernel NULL pointer dereference, address: %px\n", 5418c2ecf20Sopenharmony_ci (void *)address); 5428c2ecf20Sopenharmony_ci else 5438c2ecf20Sopenharmony_ci pr_alert("BUG: unable to handle page fault for address: %px\n", 5448c2ecf20Sopenharmony_ci (void *)address); 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci pr_alert("#PF: %s %s in %s mode\n", 5478c2ecf20Sopenharmony_ci (error_code & X86_PF_USER) ? "user" : "supervisor", 5488c2ecf20Sopenharmony_ci (error_code & X86_PF_INSTR) ? "instruction fetch" : 5498c2ecf20Sopenharmony_ci (error_code & X86_PF_WRITE) ? "write access" : 5508c2ecf20Sopenharmony_ci "read access", 5518c2ecf20Sopenharmony_ci user_mode(regs) ? "user" : "kernel"); 5528c2ecf20Sopenharmony_ci pr_alert("#PF: error_code(0x%04lx) - %s\n", error_code, 5538c2ecf20Sopenharmony_ci !(error_code & X86_PF_PROT) ? "not-present page" : 5548c2ecf20Sopenharmony_ci (error_code & X86_PF_RSVD) ? "reserved bit violation" : 5558c2ecf20Sopenharmony_ci (error_code & X86_PF_PK) ? "protection keys violation" : 5568c2ecf20Sopenharmony_ci "permissions violation"); 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci if (!(error_code & X86_PF_USER) && user_mode(regs)) { 5598c2ecf20Sopenharmony_ci struct desc_ptr idt, gdt; 5608c2ecf20Sopenharmony_ci u16 ldtr, tr; 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci /* 5638c2ecf20Sopenharmony_ci * This can happen for quite a few reasons. The more obvious 5648c2ecf20Sopenharmony_ci * ones are faults accessing the GDT, or LDT. Perhaps 5658c2ecf20Sopenharmony_ci * surprisingly, if the CPU tries to deliver a benign or 5668c2ecf20Sopenharmony_ci * contributory exception from user code and gets a page fault 5678c2ecf20Sopenharmony_ci * during delivery, the page fault can be delivered as though 5688c2ecf20Sopenharmony_ci * it originated directly from user code. This could happen 5698c2ecf20Sopenharmony_ci * due to wrong permissions on the IDT, GDT, LDT, TSS, or 5708c2ecf20Sopenharmony_ci * kernel or IST stack. 5718c2ecf20Sopenharmony_ci */ 5728c2ecf20Sopenharmony_ci store_idt(&idt); 5738c2ecf20Sopenharmony_ci 5748c2ecf20Sopenharmony_ci /* Usable even on Xen PV -- it's just slow. */ 5758c2ecf20Sopenharmony_ci native_store_gdt(&gdt); 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci pr_alert("IDT: 0x%lx (limit=0x%hx) GDT: 0x%lx (limit=0x%hx)\n", 5788c2ecf20Sopenharmony_ci idt.address, idt.size, gdt.address, gdt.size); 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_ci store_ldt(ldtr); 5818c2ecf20Sopenharmony_ci show_ldttss(&gdt, "LDTR", ldtr); 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci store_tr(tr); 5848c2ecf20Sopenharmony_ci show_ldttss(&gdt, "TR", tr); 5858c2ecf20Sopenharmony_ci } 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_ci dump_pagetable(address); 5888c2ecf20Sopenharmony_ci} 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_cistatic noinline void 5918c2ecf20Sopenharmony_cipgtable_bad(struct pt_regs *regs, unsigned long error_code, 5928c2ecf20Sopenharmony_ci unsigned long address) 5938c2ecf20Sopenharmony_ci{ 5948c2ecf20Sopenharmony_ci struct task_struct *tsk; 5958c2ecf20Sopenharmony_ci unsigned long flags; 5968c2ecf20Sopenharmony_ci int sig; 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci flags = oops_begin(); 5998c2ecf20Sopenharmony_ci tsk = current; 6008c2ecf20Sopenharmony_ci sig = SIGKILL; 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ci printk(KERN_ALERT "%s: Corrupted page table at address %lx\n", 6038c2ecf20Sopenharmony_ci tsk->comm, address); 6048c2ecf20Sopenharmony_ci dump_pagetable(address); 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci if (__die("Bad pagetable", regs, error_code)) 6078c2ecf20Sopenharmony_ci sig = 0; 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci oops_end(flags, regs, sig); 6108c2ecf20Sopenharmony_ci} 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_cistatic void set_signal_archinfo(unsigned long address, 6138c2ecf20Sopenharmony_ci unsigned long error_code) 6148c2ecf20Sopenharmony_ci{ 6158c2ecf20Sopenharmony_ci struct task_struct *tsk = current; 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci /* 6188c2ecf20Sopenharmony_ci * To avoid leaking information about the kernel page 6198c2ecf20Sopenharmony_ci * table layout, pretend that user-mode accesses to 6208c2ecf20Sopenharmony_ci * kernel addresses are always protection faults. 6218c2ecf20Sopenharmony_ci * 6228c2ecf20Sopenharmony_ci * NB: This means that failed vsyscalls with vsyscall=none 6238c2ecf20Sopenharmony_ci * will have the PROT bit. This doesn't leak any 6248c2ecf20Sopenharmony_ci * information and does not appear to cause any problems. 6258c2ecf20Sopenharmony_ci */ 6268c2ecf20Sopenharmony_ci if (address >= TASK_SIZE_MAX) 6278c2ecf20Sopenharmony_ci error_code |= X86_PF_PROT; 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci tsk->thread.trap_nr = X86_TRAP_PF; 6308c2ecf20Sopenharmony_ci tsk->thread.error_code = error_code | X86_PF_USER; 6318c2ecf20Sopenharmony_ci tsk->thread.cr2 = address; 6328c2ecf20Sopenharmony_ci} 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_cistatic noinline void 6358c2ecf20Sopenharmony_cino_context(struct pt_regs *regs, unsigned long error_code, 6368c2ecf20Sopenharmony_ci unsigned long address, int signal, int si_code) 6378c2ecf20Sopenharmony_ci{ 6388c2ecf20Sopenharmony_ci struct task_struct *tsk = current; 6398c2ecf20Sopenharmony_ci unsigned long flags; 6408c2ecf20Sopenharmony_ci int sig; 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ci if (user_mode(regs)) { 6438c2ecf20Sopenharmony_ci /* 6448c2ecf20Sopenharmony_ci * This is an implicit supervisor-mode access from user 6458c2ecf20Sopenharmony_ci * mode. Bypass all the kernel-mode recovery code and just 6468c2ecf20Sopenharmony_ci * OOPS. 6478c2ecf20Sopenharmony_ci */ 6488c2ecf20Sopenharmony_ci goto oops; 6498c2ecf20Sopenharmony_ci } 6508c2ecf20Sopenharmony_ci 6518c2ecf20Sopenharmony_ci /* Are we prepared to handle this kernel fault? */ 6528c2ecf20Sopenharmony_ci if (fixup_exception(regs, X86_TRAP_PF, error_code, address)) { 6538c2ecf20Sopenharmony_ci /* 6548c2ecf20Sopenharmony_ci * Any interrupt that takes a fault gets the fixup. This makes 6558c2ecf20Sopenharmony_ci * the below recursive fault logic only apply to a faults from 6568c2ecf20Sopenharmony_ci * task context. 6578c2ecf20Sopenharmony_ci */ 6588c2ecf20Sopenharmony_ci if (in_interrupt()) 6598c2ecf20Sopenharmony_ci return; 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_ci /* 6628c2ecf20Sopenharmony_ci * Per the above we're !in_interrupt(), aka. task context. 6638c2ecf20Sopenharmony_ci * 6648c2ecf20Sopenharmony_ci * In this case we need to make sure we're not recursively 6658c2ecf20Sopenharmony_ci * faulting through the emulate_vsyscall() logic. 6668c2ecf20Sopenharmony_ci */ 6678c2ecf20Sopenharmony_ci if (current->thread.sig_on_uaccess_err && signal) { 6688c2ecf20Sopenharmony_ci set_signal_archinfo(address, error_code); 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ci /* XXX: hwpoison faults will set the wrong code. */ 6718c2ecf20Sopenharmony_ci force_sig_fault(signal, si_code, (void __user *)address); 6728c2ecf20Sopenharmony_ci } 6738c2ecf20Sopenharmony_ci 6748c2ecf20Sopenharmony_ci /* 6758c2ecf20Sopenharmony_ci * Barring that, we can do the fixup and be happy. 6768c2ecf20Sopenharmony_ci */ 6778c2ecf20Sopenharmony_ci return; 6788c2ecf20Sopenharmony_ci } 6798c2ecf20Sopenharmony_ci 6808c2ecf20Sopenharmony_ci#ifdef CONFIG_VMAP_STACK 6818c2ecf20Sopenharmony_ci /* 6828c2ecf20Sopenharmony_ci * Stack overflow? During boot, we can fault near the initial 6838c2ecf20Sopenharmony_ci * stack in the direct map, but that's not an overflow -- check 6848c2ecf20Sopenharmony_ci * that we're in vmalloc space to avoid this. 6858c2ecf20Sopenharmony_ci */ 6868c2ecf20Sopenharmony_ci if (is_vmalloc_addr((void *)address) && 6878c2ecf20Sopenharmony_ci (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) || 6888c2ecf20Sopenharmony_ci address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) { 6898c2ecf20Sopenharmony_ci unsigned long stack = __this_cpu_ist_top_va(DF) - sizeof(void *); 6908c2ecf20Sopenharmony_ci /* 6918c2ecf20Sopenharmony_ci * We're likely to be running with very little stack space 6928c2ecf20Sopenharmony_ci * left. It's plausible that we'd hit this condition but 6938c2ecf20Sopenharmony_ci * double-fault even before we get this far, in which case 6948c2ecf20Sopenharmony_ci * we're fine: the double-fault handler will deal with it. 6958c2ecf20Sopenharmony_ci * 6968c2ecf20Sopenharmony_ci * We don't want to make it all the way into the oops code 6978c2ecf20Sopenharmony_ci * and then double-fault, though, because we're likely to 6988c2ecf20Sopenharmony_ci * break the console driver and lose most of the stack dump. 6998c2ecf20Sopenharmony_ci */ 7008c2ecf20Sopenharmony_ci asm volatile ("movq %[stack], %%rsp\n\t" 7018c2ecf20Sopenharmony_ci "call handle_stack_overflow\n\t" 7028c2ecf20Sopenharmony_ci "1: jmp 1b" 7038c2ecf20Sopenharmony_ci : ASM_CALL_CONSTRAINT 7048c2ecf20Sopenharmony_ci : "D" ("kernel stack overflow (page fault)"), 7058c2ecf20Sopenharmony_ci "S" (regs), "d" (address), 7068c2ecf20Sopenharmony_ci [stack] "rm" (stack)); 7078c2ecf20Sopenharmony_ci unreachable(); 7088c2ecf20Sopenharmony_ci } 7098c2ecf20Sopenharmony_ci#endif 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci /* 7128c2ecf20Sopenharmony_ci * 32-bit: 7138c2ecf20Sopenharmony_ci * 7148c2ecf20Sopenharmony_ci * Valid to do another page fault here, because if this fault 7158c2ecf20Sopenharmony_ci * had been triggered by is_prefetch fixup_exception would have 7168c2ecf20Sopenharmony_ci * handled it. 7178c2ecf20Sopenharmony_ci * 7188c2ecf20Sopenharmony_ci * 64-bit: 7198c2ecf20Sopenharmony_ci * 7208c2ecf20Sopenharmony_ci * Hall of shame of CPU/BIOS bugs. 7218c2ecf20Sopenharmony_ci */ 7228c2ecf20Sopenharmony_ci if (is_prefetch(regs, error_code, address)) 7238c2ecf20Sopenharmony_ci return; 7248c2ecf20Sopenharmony_ci 7258c2ecf20Sopenharmony_ci if (is_errata93(regs, address)) 7268c2ecf20Sopenharmony_ci return; 7278c2ecf20Sopenharmony_ci 7288c2ecf20Sopenharmony_ci /* 7298c2ecf20Sopenharmony_ci * Buggy firmware could access regions which might page fault, try to 7308c2ecf20Sopenharmony_ci * recover from such faults. 7318c2ecf20Sopenharmony_ci */ 7328c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_EFI)) 7338c2ecf20Sopenharmony_ci efi_recover_from_page_fault(address); 7348c2ecf20Sopenharmony_ci 7358c2ecf20Sopenharmony_cioops: 7368c2ecf20Sopenharmony_ci /* 7378c2ecf20Sopenharmony_ci * Oops. The kernel tried to access some bad page. We'll have to 7388c2ecf20Sopenharmony_ci * terminate things with extreme prejudice: 7398c2ecf20Sopenharmony_ci */ 7408c2ecf20Sopenharmony_ci flags = oops_begin(); 7418c2ecf20Sopenharmony_ci 7428c2ecf20Sopenharmony_ci show_fault_oops(regs, error_code, address); 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci if (task_stack_end_corrupted(tsk)) 7458c2ecf20Sopenharmony_ci printk(KERN_EMERG "Thread overran stack, or stack corrupted\n"); 7468c2ecf20Sopenharmony_ci 7478c2ecf20Sopenharmony_ci sig = SIGKILL; 7488c2ecf20Sopenharmony_ci if (__die("Oops", regs, error_code)) 7498c2ecf20Sopenharmony_ci sig = 0; 7508c2ecf20Sopenharmony_ci 7518c2ecf20Sopenharmony_ci /* Executive summary in case the body of the oops scrolled away */ 7528c2ecf20Sopenharmony_ci printk(KERN_DEFAULT "CR2: %016lx\n", address); 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ci oops_end(flags, regs, sig); 7558c2ecf20Sopenharmony_ci} 7568c2ecf20Sopenharmony_ci 7578c2ecf20Sopenharmony_ci/* 7588c2ecf20Sopenharmony_ci * Print out info about fatal segfaults, if the show_unhandled_signals 7598c2ecf20Sopenharmony_ci * sysctl is set: 7608c2ecf20Sopenharmony_ci */ 7618c2ecf20Sopenharmony_cistatic inline void 7628c2ecf20Sopenharmony_cishow_signal_msg(struct pt_regs *regs, unsigned long error_code, 7638c2ecf20Sopenharmony_ci unsigned long address, struct task_struct *tsk) 7648c2ecf20Sopenharmony_ci{ 7658c2ecf20Sopenharmony_ci const char *loglvl = task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG; 7668c2ecf20Sopenharmony_ci 7678c2ecf20Sopenharmony_ci if (!unhandled_signal(tsk, SIGSEGV)) 7688c2ecf20Sopenharmony_ci return; 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci if (!printk_ratelimit()) 7718c2ecf20Sopenharmony_ci return; 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ci printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx", 7748c2ecf20Sopenharmony_ci loglvl, tsk->comm, task_pid_nr(tsk), address, 7758c2ecf20Sopenharmony_ci (void *)regs->ip, (void *)regs->sp, error_code); 7768c2ecf20Sopenharmony_ci 7778c2ecf20Sopenharmony_ci print_vma_addr(KERN_CONT " in ", regs->ip); 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_ci printk(KERN_CONT "\n"); 7808c2ecf20Sopenharmony_ci 7818c2ecf20Sopenharmony_ci show_opcodes(regs, loglvl); 7828c2ecf20Sopenharmony_ci} 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci/* 7858c2ecf20Sopenharmony_ci * The (legacy) vsyscall page is the long page in the kernel portion 7868c2ecf20Sopenharmony_ci * of the address space that has user-accessible permissions. 7878c2ecf20Sopenharmony_ci */ 7888c2ecf20Sopenharmony_cistatic bool is_vsyscall_vaddr(unsigned long vaddr) 7898c2ecf20Sopenharmony_ci{ 7908c2ecf20Sopenharmony_ci return unlikely((vaddr & PAGE_MASK) == VSYSCALL_ADDR); 7918c2ecf20Sopenharmony_ci} 7928c2ecf20Sopenharmony_ci 7938c2ecf20Sopenharmony_cistatic void 7948c2ecf20Sopenharmony_ci__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 7958c2ecf20Sopenharmony_ci unsigned long address, u32 pkey, int si_code) 7968c2ecf20Sopenharmony_ci{ 7978c2ecf20Sopenharmony_ci struct task_struct *tsk = current; 7988c2ecf20Sopenharmony_ci 7998c2ecf20Sopenharmony_ci /* User mode accesses just cause a SIGSEGV */ 8008c2ecf20Sopenharmony_ci if (user_mode(regs) && (error_code & X86_PF_USER)) { 8018c2ecf20Sopenharmony_ci /* 8028c2ecf20Sopenharmony_ci * It's possible to have interrupts off here: 8038c2ecf20Sopenharmony_ci */ 8048c2ecf20Sopenharmony_ci local_irq_enable(); 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_ci /* 8078c2ecf20Sopenharmony_ci * Valid to do another page fault here because this one came 8088c2ecf20Sopenharmony_ci * from user space: 8098c2ecf20Sopenharmony_ci */ 8108c2ecf20Sopenharmony_ci if (is_prefetch(regs, error_code, address)) 8118c2ecf20Sopenharmony_ci return; 8128c2ecf20Sopenharmony_ci 8138c2ecf20Sopenharmony_ci if (is_errata100(regs, address)) 8148c2ecf20Sopenharmony_ci return; 8158c2ecf20Sopenharmony_ci 8168c2ecf20Sopenharmony_ci /* 8178c2ecf20Sopenharmony_ci * To avoid leaking information about the kernel page table 8188c2ecf20Sopenharmony_ci * layout, pretend that user-mode accesses to kernel addresses 8198c2ecf20Sopenharmony_ci * are always protection faults. 8208c2ecf20Sopenharmony_ci */ 8218c2ecf20Sopenharmony_ci if (address >= TASK_SIZE_MAX) 8228c2ecf20Sopenharmony_ci error_code |= X86_PF_PROT; 8238c2ecf20Sopenharmony_ci 8248c2ecf20Sopenharmony_ci if (likely(show_unhandled_signals)) 8258c2ecf20Sopenharmony_ci show_signal_msg(regs, error_code, address, tsk); 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci set_signal_archinfo(address, error_code); 8288c2ecf20Sopenharmony_ci 8298c2ecf20Sopenharmony_ci if (si_code == SEGV_PKUERR) 8308c2ecf20Sopenharmony_ci force_sig_pkuerr((void __user *)address, pkey); 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ci force_sig_fault(SIGSEGV, si_code, (void __user *)address); 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_ci local_irq_disable(); 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci return; 8378c2ecf20Sopenharmony_ci } 8388c2ecf20Sopenharmony_ci 8398c2ecf20Sopenharmony_ci if (is_f00f_bug(regs, address)) 8408c2ecf20Sopenharmony_ci return; 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_ci no_context(regs, error_code, address, SIGSEGV, si_code); 8438c2ecf20Sopenharmony_ci} 8448c2ecf20Sopenharmony_ci 8458c2ecf20Sopenharmony_cistatic noinline void 8468c2ecf20Sopenharmony_cibad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, 8478c2ecf20Sopenharmony_ci unsigned long address) 8488c2ecf20Sopenharmony_ci{ 8498c2ecf20Sopenharmony_ci __bad_area_nosemaphore(regs, error_code, address, 0, SEGV_MAPERR); 8508c2ecf20Sopenharmony_ci} 8518c2ecf20Sopenharmony_ci 8528c2ecf20Sopenharmony_cistatic void 8538c2ecf20Sopenharmony_ci__bad_area(struct pt_regs *regs, unsigned long error_code, 8548c2ecf20Sopenharmony_ci unsigned long address, u32 pkey, int si_code) 8558c2ecf20Sopenharmony_ci{ 8568c2ecf20Sopenharmony_ci struct mm_struct *mm = current->mm; 8578c2ecf20Sopenharmony_ci /* 8588c2ecf20Sopenharmony_ci * Something tried to access memory that isn't in our memory map.. 8598c2ecf20Sopenharmony_ci * Fix it, but check if it's kernel or user first.. 8608c2ecf20Sopenharmony_ci */ 8618c2ecf20Sopenharmony_ci mmap_read_unlock(mm); 8628c2ecf20Sopenharmony_ci 8638c2ecf20Sopenharmony_ci __bad_area_nosemaphore(regs, error_code, address, pkey, si_code); 8648c2ecf20Sopenharmony_ci} 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_cistatic noinline void 8678c2ecf20Sopenharmony_cibad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address) 8688c2ecf20Sopenharmony_ci{ 8698c2ecf20Sopenharmony_ci __bad_area(regs, error_code, address, 0, SEGV_MAPERR); 8708c2ecf20Sopenharmony_ci} 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_cistatic inline bool bad_area_access_from_pkeys(unsigned long error_code, 8738c2ecf20Sopenharmony_ci struct vm_area_struct *vma) 8748c2ecf20Sopenharmony_ci{ 8758c2ecf20Sopenharmony_ci /* This code is always called on the current mm */ 8768c2ecf20Sopenharmony_ci bool foreign = false; 8778c2ecf20Sopenharmony_ci 8788c2ecf20Sopenharmony_ci if (!boot_cpu_has(X86_FEATURE_OSPKE)) 8798c2ecf20Sopenharmony_ci return false; 8808c2ecf20Sopenharmony_ci if (error_code & X86_PF_PK) 8818c2ecf20Sopenharmony_ci return true; 8828c2ecf20Sopenharmony_ci /* this checks permission keys on the VMA: */ 8838c2ecf20Sopenharmony_ci if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE), 8848c2ecf20Sopenharmony_ci (error_code & X86_PF_INSTR), foreign)) 8858c2ecf20Sopenharmony_ci return true; 8868c2ecf20Sopenharmony_ci return false; 8878c2ecf20Sopenharmony_ci} 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_cistatic noinline void 8908c2ecf20Sopenharmony_cibad_area_access_error(struct pt_regs *regs, unsigned long error_code, 8918c2ecf20Sopenharmony_ci unsigned long address, struct vm_area_struct *vma) 8928c2ecf20Sopenharmony_ci{ 8938c2ecf20Sopenharmony_ci /* 8948c2ecf20Sopenharmony_ci * This OSPKE check is not strictly necessary at runtime. 8958c2ecf20Sopenharmony_ci * But, doing it this way allows compiler optimizations 8968c2ecf20Sopenharmony_ci * if pkeys are compiled out. 8978c2ecf20Sopenharmony_ci */ 8988c2ecf20Sopenharmony_ci if (bad_area_access_from_pkeys(error_code, vma)) { 8998c2ecf20Sopenharmony_ci /* 9008c2ecf20Sopenharmony_ci * A protection key fault means that the PKRU value did not allow 9018c2ecf20Sopenharmony_ci * access to some PTE. Userspace can figure out what PKRU was 9028c2ecf20Sopenharmony_ci * from the XSAVE state. This function captures the pkey from 9038c2ecf20Sopenharmony_ci * the vma and passes it to userspace so userspace can discover 9048c2ecf20Sopenharmony_ci * which protection key was set on the PTE. 9058c2ecf20Sopenharmony_ci * 9068c2ecf20Sopenharmony_ci * If we get here, we know that the hardware signaled a X86_PF_PK 9078c2ecf20Sopenharmony_ci * fault and that there was a VMA once we got in the fault 9088c2ecf20Sopenharmony_ci * handler. It does *not* guarantee that the VMA we find here 9098c2ecf20Sopenharmony_ci * was the one that we faulted on. 9108c2ecf20Sopenharmony_ci * 9118c2ecf20Sopenharmony_ci * 1. T1 : mprotect_key(foo, PAGE_SIZE, pkey=4); 9128c2ecf20Sopenharmony_ci * 2. T1 : set PKRU to deny access to pkey=4, touches page 9138c2ecf20Sopenharmony_ci * 3. T1 : faults... 9148c2ecf20Sopenharmony_ci * 4. T2: mprotect_key(foo, PAGE_SIZE, pkey=5); 9158c2ecf20Sopenharmony_ci * 5. T1 : enters fault handler, takes mmap_lock, etc... 9168c2ecf20Sopenharmony_ci * 6. T1 : reaches here, sees vma_pkey(vma)=5, when we really 9178c2ecf20Sopenharmony_ci * faulted on a pte with its pkey=4. 9188c2ecf20Sopenharmony_ci */ 9198c2ecf20Sopenharmony_ci u32 pkey = vma_pkey(vma); 9208c2ecf20Sopenharmony_ci 9218c2ecf20Sopenharmony_ci __bad_area(regs, error_code, address, pkey, SEGV_PKUERR); 9228c2ecf20Sopenharmony_ci } else { 9238c2ecf20Sopenharmony_ci __bad_area(regs, error_code, address, 0, SEGV_ACCERR); 9248c2ecf20Sopenharmony_ci } 9258c2ecf20Sopenharmony_ci} 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_cistatic void 9288c2ecf20Sopenharmony_cido_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address, 9298c2ecf20Sopenharmony_ci vm_fault_t fault) 9308c2ecf20Sopenharmony_ci{ 9318c2ecf20Sopenharmony_ci /* Kernel mode? Handle exceptions or die: */ 9328c2ecf20Sopenharmony_ci if (!(error_code & X86_PF_USER)) { 9338c2ecf20Sopenharmony_ci no_context(regs, error_code, address, SIGBUS, BUS_ADRERR); 9348c2ecf20Sopenharmony_ci return; 9358c2ecf20Sopenharmony_ci } 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_ci /* User-space => ok to do another page fault: */ 9388c2ecf20Sopenharmony_ci if (is_prefetch(regs, error_code, address)) 9398c2ecf20Sopenharmony_ci return; 9408c2ecf20Sopenharmony_ci 9418c2ecf20Sopenharmony_ci set_signal_archinfo(address, error_code); 9428c2ecf20Sopenharmony_ci 9438c2ecf20Sopenharmony_ci#ifdef CONFIG_MEMORY_FAILURE 9448c2ecf20Sopenharmony_ci if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) { 9458c2ecf20Sopenharmony_ci struct task_struct *tsk = current; 9468c2ecf20Sopenharmony_ci unsigned lsb = 0; 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci pr_err( 9498c2ecf20Sopenharmony_ci "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n", 9508c2ecf20Sopenharmony_ci tsk->comm, tsk->pid, address); 9518c2ecf20Sopenharmony_ci if (fault & VM_FAULT_HWPOISON_LARGE) 9528c2ecf20Sopenharmony_ci lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault)); 9538c2ecf20Sopenharmony_ci if (fault & VM_FAULT_HWPOISON) 9548c2ecf20Sopenharmony_ci lsb = PAGE_SHIFT; 9558c2ecf20Sopenharmony_ci force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb); 9568c2ecf20Sopenharmony_ci return; 9578c2ecf20Sopenharmony_ci } 9588c2ecf20Sopenharmony_ci#endif 9598c2ecf20Sopenharmony_ci force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address); 9608c2ecf20Sopenharmony_ci} 9618c2ecf20Sopenharmony_ci 9628c2ecf20Sopenharmony_cistatic noinline void 9638c2ecf20Sopenharmony_cimm_fault_error(struct pt_regs *regs, unsigned long error_code, 9648c2ecf20Sopenharmony_ci unsigned long address, vm_fault_t fault) 9658c2ecf20Sopenharmony_ci{ 9668c2ecf20Sopenharmony_ci if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) { 9678c2ecf20Sopenharmony_ci no_context(regs, error_code, address, 0, 0); 9688c2ecf20Sopenharmony_ci return; 9698c2ecf20Sopenharmony_ci } 9708c2ecf20Sopenharmony_ci 9718c2ecf20Sopenharmony_ci if (fault & VM_FAULT_OOM) { 9728c2ecf20Sopenharmony_ci /* Kernel mode? Handle exceptions or die: */ 9738c2ecf20Sopenharmony_ci if (!(error_code & X86_PF_USER)) { 9748c2ecf20Sopenharmony_ci no_context(regs, error_code, address, 9758c2ecf20Sopenharmony_ci SIGSEGV, SEGV_MAPERR); 9768c2ecf20Sopenharmony_ci return; 9778c2ecf20Sopenharmony_ci } 9788c2ecf20Sopenharmony_ci 9798c2ecf20Sopenharmony_ci /* 9808c2ecf20Sopenharmony_ci * We ran out of memory, call the OOM killer, and return the 9818c2ecf20Sopenharmony_ci * userspace (which will retry the fault, or kill us if we got 9828c2ecf20Sopenharmony_ci * oom-killed): 9838c2ecf20Sopenharmony_ci */ 9848c2ecf20Sopenharmony_ci pagefault_out_of_memory(); 9858c2ecf20Sopenharmony_ci } else { 9868c2ecf20Sopenharmony_ci if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON| 9878c2ecf20Sopenharmony_ci VM_FAULT_HWPOISON_LARGE)) 9888c2ecf20Sopenharmony_ci do_sigbus(regs, error_code, address, fault); 9898c2ecf20Sopenharmony_ci else if (fault & VM_FAULT_SIGSEGV) 9908c2ecf20Sopenharmony_ci bad_area_nosemaphore(regs, error_code, address); 9918c2ecf20Sopenharmony_ci else 9928c2ecf20Sopenharmony_ci BUG(); 9938c2ecf20Sopenharmony_ci } 9948c2ecf20Sopenharmony_ci} 9958c2ecf20Sopenharmony_ci 9968c2ecf20Sopenharmony_cistatic int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte) 9978c2ecf20Sopenharmony_ci{ 9988c2ecf20Sopenharmony_ci if ((error_code & X86_PF_WRITE) && !pte_write(*pte)) 9998c2ecf20Sopenharmony_ci return 0; 10008c2ecf20Sopenharmony_ci 10018c2ecf20Sopenharmony_ci if ((error_code & X86_PF_INSTR) && !pte_exec(*pte)) 10028c2ecf20Sopenharmony_ci return 0; 10038c2ecf20Sopenharmony_ci 10048c2ecf20Sopenharmony_ci return 1; 10058c2ecf20Sopenharmony_ci} 10068c2ecf20Sopenharmony_ci 10078c2ecf20Sopenharmony_ci/* 10088c2ecf20Sopenharmony_ci * Handle a spurious fault caused by a stale TLB entry. 10098c2ecf20Sopenharmony_ci * 10108c2ecf20Sopenharmony_ci * This allows us to lazily refresh the TLB when increasing the 10118c2ecf20Sopenharmony_ci * permissions of a kernel page (RO -> RW or NX -> X). Doing it 10128c2ecf20Sopenharmony_ci * eagerly is very expensive since that implies doing a full 10138c2ecf20Sopenharmony_ci * cross-processor TLB flush, even if no stale TLB entries exist 10148c2ecf20Sopenharmony_ci * on other processors. 10158c2ecf20Sopenharmony_ci * 10168c2ecf20Sopenharmony_ci * Spurious faults may only occur if the TLB contains an entry with 10178c2ecf20Sopenharmony_ci * fewer permission than the page table entry. Non-present (P = 0) 10188c2ecf20Sopenharmony_ci * and reserved bit (R = 1) faults are never spurious. 10198c2ecf20Sopenharmony_ci * 10208c2ecf20Sopenharmony_ci * There are no security implications to leaving a stale TLB when 10218c2ecf20Sopenharmony_ci * increasing the permissions on a page. 10228c2ecf20Sopenharmony_ci * 10238c2ecf20Sopenharmony_ci * Returns non-zero if a spurious fault was handled, zero otherwise. 10248c2ecf20Sopenharmony_ci * 10258c2ecf20Sopenharmony_ci * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3 10268c2ecf20Sopenharmony_ci * (Optional Invalidation). 10278c2ecf20Sopenharmony_ci */ 10288c2ecf20Sopenharmony_cistatic noinline int 10298c2ecf20Sopenharmony_cispurious_kernel_fault(unsigned long error_code, unsigned long address) 10308c2ecf20Sopenharmony_ci{ 10318c2ecf20Sopenharmony_ci pgd_t *pgd; 10328c2ecf20Sopenharmony_ci p4d_t *p4d; 10338c2ecf20Sopenharmony_ci pud_t *pud; 10348c2ecf20Sopenharmony_ci pmd_t *pmd; 10358c2ecf20Sopenharmony_ci pte_t *pte; 10368c2ecf20Sopenharmony_ci int ret; 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci /* 10398c2ecf20Sopenharmony_ci * Only writes to RO or instruction fetches from NX may cause 10408c2ecf20Sopenharmony_ci * spurious faults. 10418c2ecf20Sopenharmony_ci * 10428c2ecf20Sopenharmony_ci * These could be from user or supervisor accesses but the TLB 10438c2ecf20Sopenharmony_ci * is only lazily flushed after a kernel mapping protection 10448c2ecf20Sopenharmony_ci * change, so user accesses are not expected to cause spurious 10458c2ecf20Sopenharmony_ci * faults. 10468c2ecf20Sopenharmony_ci */ 10478c2ecf20Sopenharmony_ci if (error_code != (X86_PF_WRITE | X86_PF_PROT) && 10488c2ecf20Sopenharmony_ci error_code != (X86_PF_INSTR | X86_PF_PROT)) 10498c2ecf20Sopenharmony_ci return 0; 10508c2ecf20Sopenharmony_ci 10518c2ecf20Sopenharmony_ci pgd = init_mm.pgd + pgd_index(address); 10528c2ecf20Sopenharmony_ci if (!pgd_present(*pgd)) 10538c2ecf20Sopenharmony_ci return 0; 10548c2ecf20Sopenharmony_ci 10558c2ecf20Sopenharmony_ci p4d = p4d_offset(pgd, address); 10568c2ecf20Sopenharmony_ci if (!p4d_present(*p4d)) 10578c2ecf20Sopenharmony_ci return 0; 10588c2ecf20Sopenharmony_ci 10598c2ecf20Sopenharmony_ci if (p4d_large(*p4d)) 10608c2ecf20Sopenharmony_ci return spurious_kernel_fault_check(error_code, (pte_t *) p4d); 10618c2ecf20Sopenharmony_ci 10628c2ecf20Sopenharmony_ci pud = pud_offset(p4d, address); 10638c2ecf20Sopenharmony_ci if (!pud_present(*pud)) 10648c2ecf20Sopenharmony_ci return 0; 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_ci if (pud_large(*pud)) 10678c2ecf20Sopenharmony_ci return spurious_kernel_fault_check(error_code, (pte_t *) pud); 10688c2ecf20Sopenharmony_ci 10698c2ecf20Sopenharmony_ci pmd = pmd_offset(pud, address); 10708c2ecf20Sopenharmony_ci if (!pmd_present(*pmd)) 10718c2ecf20Sopenharmony_ci return 0; 10728c2ecf20Sopenharmony_ci 10738c2ecf20Sopenharmony_ci if (pmd_large(*pmd)) 10748c2ecf20Sopenharmony_ci return spurious_kernel_fault_check(error_code, (pte_t *) pmd); 10758c2ecf20Sopenharmony_ci 10768c2ecf20Sopenharmony_ci pte = pte_offset_kernel(pmd, address); 10778c2ecf20Sopenharmony_ci if (!pte_present(*pte)) 10788c2ecf20Sopenharmony_ci return 0; 10798c2ecf20Sopenharmony_ci 10808c2ecf20Sopenharmony_ci ret = spurious_kernel_fault_check(error_code, pte); 10818c2ecf20Sopenharmony_ci if (!ret) 10828c2ecf20Sopenharmony_ci return 0; 10838c2ecf20Sopenharmony_ci 10848c2ecf20Sopenharmony_ci /* 10858c2ecf20Sopenharmony_ci * Make sure we have permissions in PMD. 10868c2ecf20Sopenharmony_ci * If not, then there's a bug in the page tables: 10878c2ecf20Sopenharmony_ci */ 10888c2ecf20Sopenharmony_ci ret = spurious_kernel_fault_check(error_code, (pte_t *) pmd); 10898c2ecf20Sopenharmony_ci WARN_ONCE(!ret, "PMD has incorrect permission bits\n"); 10908c2ecf20Sopenharmony_ci 10918c2ecf20Sopenharmony_ci return ret; 10928c2ecf20Sopenharmony_ci} 10938c2ecf20Sopenharmony_ciNOKPROBE_SYMBOL(spurious_kernel_fault); 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_ciint show_unhandled_signals = 1; 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_cistatic inline int 10988c2ecf20Sopenharmony_ciaccess_error(unsigned long error_code, struct vm_area_struct *vma) 10998c2ecf20Sopenharmony_ci{ 11008c2ecf20Sopenharmony_ci /* This is only called for the current mm, so: */ 11018c2ecf20Sopenharmony_ci bool foreign = false; 11028c2ecf20Sopenharmony_ci 11038c2ecf20Sopenharmony_ci /* 11048c2ecf20Sopenharmony_ci * Read or write was blocked by protection keys. This is 11058c2ecf20Sopenharmony_ci * always an unconditional error and can never result in 11068c2ecf20Sopenharmony_ci * a follow-up action to resolve the fault, like a COW. 11078c2ecf20Sopenharmony_ci */ 11088c2ecf20Sopenharmony_ci if (error_code & X86_PF_PK) 11098c2ecf20Sopenharmony_ci return 1; 11108c2ecf20Sopenharmony_ci 11118c2ecf20Sopenharmony_ci /* 11128c2ecf20Sopenharmony_ci * Make sure to check the VMA so that we do not perform 11138c2ecf20Sopenharmony_ci * faults just to hit a X86_PF_PK as soon as we fill in a 11148c2ecf20Sopenharmony_ci * page. 11158c2ecf20Sopenharmony_ci */ 11168c2ecf20Sopenharmony_ci if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE), 11178c2ecf20Sopenharmony_ci (error_code & X86_PF_INSTR), foreign)) 11188c2ecf20Sopenharmony_ci return 1; 11198c2ecf20Sopenharmony_ci 11208c2ecf20Sopenharmony_ci if (error_code & X86_PF_WRITE) { 11218c2ecf20Sopenharmony_ci /* write, present and write, not present: */ 11228c2ecf20Sopenharmony_ci if (unlikely(!(vma->vm_flags & VM_WRITE))) 11238c2ecf20Sopenharmony_ci return 1; 11248c2ecf20Sopenharmony_ci return 0; 11258c2ecf20Sopenharmony_ci } 11268c2ecf20Sopenharmony_ci 11278c2ecf20Sopenharmony_ci /* read, present: */ 11288c2ecf20Sopenharmony_ci if (unlikely(error_code & X86_PF_PROT)) 11298c2ecf20Sopenharmony_ci return 1; 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_ci /* read, not present: */ 11328c2ecf20Sopenharmony_ci if (unlikely(!vma_is_accessible(vma))) 11338c2ecf20Sopenharmony_ci return 1; 11348c2ecf20Sopenharmony_ci 11358c2ecf20Sopenharmony_ci return 0; 11368c2ecf20Sopenharmony_ci} 11378c2ecf20Sopenharmony_ci 11388c2ecf20Sopenharmony_cibool fault_in_kernel_space(unsigned long address) 11398c2ecf20Sopenharmony_ci{ 11408c2ecf20Sopenharmony_ci /* 11418c2ecf20Sopenharmony_ci * On 64-bit systems, the vsyscall page is at an address above 11428c2ecf20Sopenharmony_ci * TASK_SIZE_MAX, but is not considered part of the kernel 11438c2ecf20Sopenharmony_ci * address space. 11448c2ecf20Sopenharmony_ci */ 11458c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_X86_64) && is_vsyscall_vaddr(address)) 11468c2ecf20Sopenharmony_ci return false; 11478c2ecf20Sopenharmony_ci 11488c2ecf20Sopenharmony_ci return address >= TASK_SIZE_MAX; 11498c2ecf20Sopenharmony_ci} 11508c2ecf20Sopenharmony_ci 11518c2ecf20Sopenharmony_ci/* 11528c2ecf20Sopenharmony_ci * Called for all faults where 'address' is part of the kernel address 11538c2ecf20Sopenharmony_ci * space. Might get called for faults that originate from *code* that 11548c2ecf20Sopenharmony_ci * ran in userspace or the kernel. 11558c2ecf20Sopenharmony_ci */ 11568c2ecf20Sopenharmony_cistatic void 11578c2ecf20Sopenharmony_cido_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, 11588c2ecf20Sopenharmony_ci unsigned long address) 11598c2ecf20Sopenharmony_ci{ 11608c2ecf20Sopenharmony_ci /* 11618c2ecf20Sopenharmony_ci * Protection keys exceptions only happen on user pages. We 11628c2ecf20Sopenharmony_ci * have no user pages in the kernel portion of the address 11638c2ecf20Sopenharmony_ci * space, so do not expect them here. 11648c2ecf20Sopenharmony_ci */ 11658c2ecf20Sopenharmony_ci WARN_ON_ONCE(hw_error_code & X86_PF_PK); 11668c2ecf20Sopenharmony_ci 11678c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32 11688c2ecf20Sopenharmony_ci /* 11698c2ecf20Sopenharmony_ci * We can fault-in kernel-space virtual memory on-demand. The 11708c2ecf20Sopenharmony_ci * 'reference' page table is init_mm.pgd. 11718c2ecf20Sopenharmony_ci * 11728c2ecf20Sopenharmony_ci * NOTE! We MUST NOT take any locks for this case. We may 11738c2ecf20Sopenharmony_ci * be in an interrupt or a critical region, and should 11748c2ecf20Sopenharmony_ci * only copy the information from the master page table, 11758c2ecf20Sopenharmony_ci * nothing more. 11768c2ecf20Sopenharmony_ci * 11778c2ecf20Sopenharmony_ci * Before doing this on-demand faulting, ensure that the 11788c2ecf20Sopenharmony_ci * fault is not any of the following: 11798c2ecf20Sopenharmony_ci * 1. A fault on a PTE with a reserved bit set. 11808c2ecf20Sopenharmony_ci * 2. A fault caused by a user-mode access. (Do not demand- 11818c2ecf20Sopenharmony_ci * fault kernel memory due to user-mode accesses). 11828c2ecf20Sopenharmony_ci * 3. A fault caused by a page-level protection violation. 11838c2ecf20Sopenharmony_ci * (A demand fault would be on a non-present page which 11848c2ecf20Sopenharmony_ci * would have X86_PF_PROT==0). 11858c2ecf20Sopenharmony_ci * 11868c2ecf20Sopenharmony_ci * This is only needed to close a race condition on x86-32 in 11878c2ecf20Sopenharmony_ci * the vmalloc mapping/unmapping code. See the comment above 11888c2ecf20Sopenharmony_ci * vmalloc_fault() for details. On x86-64 the race does not 11898c2ecf20Sopenharmony_ci * exist as the vmalloc mappings don't need to be synchronized 11908c2ecf20Sopenharmony_ci * there. 11918c2ecf20Sopenharmony_ci */ 11928c2ecf20Sopenharmony_ci if (!(hw_error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) { 11938c2ecf20Sopenharmony_ci if (vmalloc_fault(address) >= 0) 11948c2ecf20Sopenharmony_ci return; 11958c2ecf20Sopenharmony_ci } 11968c2ecf20Sopenharmony_ci#endif 11978c2ecf20Sopenharmony_ci 11988c2ecf20Sopenharmony_ci /* Was the fault spurious, caused by lazy TLB invalidation? */ 11998c2ecf20Sopenharmony_ci if (spurious_kernel_fault(hw_error_code, address)) 12008c2ecf20Sopenharmony_ci return; 12018c2ecf20Sopenharmony_ci 12028c2ecf20Sopenharmony_ci /* kprobes don't want to hook the spurious faults: */ 12038c2ecf20Sopenharmony_ci if (kprobe_page_fault(regs, X86_TRAP_PF)) 12048c2ecf20Sopenharmony_ci return; 12058c2ecf20Sopenharmony_ci 12068c2ecf20Sopenharmony_ci /* 12078c2ecf20Sopenharmony_ci * Note, despite being a "bad area", there are quite a few 12088c2ecf20Sopenharmony_ci * acceptable reasons to get here, such as erratum fixups 12098c2ecf20Sopenharmony_ci * and handling kernel code that can fault, like get_user(). 12108c2ecf20Sopenharmony_ci * 12118c2ecf20Sopenharmony_ci * Don't take the mm semaphore here. If we fixup a prefetch 12128c2ecf20Sopenharmony_ci * fault we could otherwise deadlock: 12138c2ecf20Sopenharmony_ci */ 12148c2ecf20Sopenharmony_ci bad_area_nosemaphore(regs, hw_error_code, address); 12158c2ecf20Sopenharmony_ci} 12168c2ecf20Sopenharmony_ciNOKPROBE_SYMBOL(do_kern_addr_fault); 12178c2ecf20Sopenharmony_ci 12188c2ecf20Sopenharmony_ci/* Handle faults in the user portion of the address space */ 12198c2ecf20Sopenharmony_cistatic inline 12208c2ecf20Sopenharmony_civoid do_user_addr_fault(struct pt_regs *regs, 12218c2ecf20Sopenharmony_ci unsigned long hw_error_code, 12228c2ecf20Sopenharmony_ci unsigned long address) 12238c2ecf20Sopenharmony_ci{ 12248c2ecf20Sopenharmony_ci struct vm_area_struct *vma; 12258c2ecf20Sopenharmony_ci struct task_struct *tsk; 12268c2ecf20Sopenharmony_ci struct mm_struct *mm; 12278c2ecf20Sopenharmony_ci vm_fault_t fault; 12288c2ecf20Sopenharmony_ci unsigned int flags = FAULT_FLAG_DEFAULT; 12298c2ecf20Sopenharmony_ci 12308c2ecf20Sopenharmony_ci tsk = current; 12318c2ecf20Sopenharmony_ci mm = tsk->mm; 12328c2ecf20Sopenharmony_ci 12338c2ecf20Sopenharmony_ci /* kprobes don't want to hook the spurious faults: */ 12348c2ecf20Sopenharmony_ci if (unlikely(kprobe_page_fault(regs, X86_TRAP_PF))) 12358c2ecf20Sopenharmony_ci return; 12368c2ecf20Sopenharmony_ci 12378c2ecf20Sopenharmony_ci /* 12388c2ecf20Sopenharmony_ci * Reserved bits are never expected to be set on 12398c2ecf20Sopenharmony_ci * entries in the user portion of the page tables. 12408c2ecf20Sopenharmony_ci */ 12418c2ecf20Sopenharmony_ci if (unlikely(hw_error_code & X86_PF_RSVD)) 12428c2ecf20Sopenharmony_ci pgtable_bad(regs, hw_error_code, address); 12438c2ecf20Sopenharmony_ci 12448c2ecf20Sopenharmony_ci /* 12458c2ecf20Sopenharmony_ci * If SMAP is on, check for invalid kernel (supervisor) access to user 12468c2ecf20Sopenharmony_ci * pages in the user address space. The odd case here is WRUSS, 12478c2ecf20Sopenharmony_ci * which, according to the preliminary documentation, does not respect 12488c2ecf20Sopenharmony_ci * SMAP and will have the USER bit set so, in all cases, SMAP 12498c2ecf20Sopenharmony_ci * enforcement appears to be consistent with the USER bit. 12508c2ecf20Sopenharmony_ci */ 12518c2ecf20Sopenharmony_ci if (unlikely(cpu_feature_enabled(X86_FEATURE_SMAP) && 12528c2ecf20Sopenharmony_ci !(hw_error_code & X86_PF_USER) && 12538c2ecf20Sopenharmony_ci !(regs->flags & X86_EFLAGS_AC))) 12548c2ecf20Sopenharmony_ci { 12558c2ecf20Sopenharmony_ci bad_area_nosemaphore(regs, hw_error_code, address); 12568c2ecf20Sopenharmony_ci return; 12578c2ecf20Sopenharmony_ci } 12588c2ecf20Sopenharmony_ci 12598c2ecf20Sopenharmony_ci /* 12608c2ecf20Sopenharmony_ci * If we're in an interrupt, have no user context or are running 12618c2ecf20Sopenharmony_ci * in a region with pagefaults disabled then we must not take the fault 12628c2ecf20Sopenharmony_ci */ 12638c2ecf20Sopenharmony_ci if (unlikely(faulthandler_disabled() || !mm)) { 12648c2ecf20Sopenharmony_ci bad_area_nosemaphore(regs, hw_error_code, address); 12658c2ecf20Sopenharmony_ci return; 12668c2ecf20Sopenharmony_ci } 12678c2ecf20Sopenharmony_ci 12688c2ecf20Sopenharmony_ci /* 12698c2ecf20Sopenharmony_ci * It's safe to allow irq's after cr2 has been saved and the 12708c2ecf20Sopenharmony_ci * vmalloc fault has been handled. 12718c2ecf20Sopenharmony_ci * 12728c2ecf20Sopenharmony_ci * User-mode registers count as a user access even for any 12738c2ecf20Sopenharmony_ci * potential system fault or CPU buglet: 12748c2ecf20Sopenharmony_ci */ 12758c2ecf20Sopenharmony_ci if (user_mode(regs)) { 12768c2ecf20Sopenharmony_ci local_irq_enable(); 12778c2ecf20Sopenharmony_ci flags |= FAULT_FLAG_USER; 12788c2ecf20Sopenharmony_ci } else { 12798c2ecf20Sopenharmony_ci if (regs->flags & X86_EFLAGS_IF) 12808c2ecf20Sopenharmony_ci local_irq_enable(); 12818c2ecf20Sopenharmony_ci } 12828c2ecf20Sopenharmony_ci 12838c2ecf20Sopenharmony_ci perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 12848c2ecf20Sopenharmony_ci 12858c2ecf20Sopenharmony_ci if (hw_error_code & X86_PF_WRITE) 12868c2ecf20Sopenharmony_ci flags |= FAULT_FLAG_WRITE; 12878c2ecf20Sopenharmony_ci if (hw_error_code & X86_PF_INSTR) 12888c2ecf20Sopenharmony_ci flags |= FAULT_FLAG_INSTRUCTION; 12898c2ecf20Sopenharmony_ci 12908c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64 12918c2ecf20Sopenharmony_ci /* 12928c2ecf20Sopenharmony_ci * Faults in the vsyscall page might need emulation. The 12938c2ecf20Sopenharmony_ci * vsyscall page is at a high address (>PAGE_OFFSET), but is 12948c2ecf20Sopenharmony_ci * considered to be part of the user address space. 12958c2ecf20Sopenharmony_ci * 12968c2ecf20Sopenharmony_ci * The vsyscall page does not have a "real" VMA, so do this 12978c2ecf20Sopenharmony_ci * emulation before we go searching for VMAs. 12988c2ecf20Sopenharmony_ci * 12998c2ecf20Sopenharmony_ci * PKRU never rejects instruction fetches, so we don't need 13008c2ecf20Sopenharmony_ci * to consider the PF_PK bit. 13018c2ecf20Sopenharmony_ci */ 13028c2ecf20Sopenharmony_ci if (is_vsyscall_vaddr(address)) { 13038c2ecf20Sopenharmony_ci if (emulate_vsyscall(hw_error_code, regs, address)) 13048c2ecf20Sopenharmony_ci return; 13058c2ecf20Sopenharmony_ci } 13068c2ecf20Sopenharmony_ci#endif 13078c2ecf20Sopenharmony_ci 13088c2ecf20Sopenharmony_ci /* 13098c2ecf20Sopenharmony_ci * Kernel-mode access to the user address space should only occur 13108c2ecf20Sopenharmony_ci * on well-defined single instructions listed in the exception 13118c2ecf20Sopenharmony_ci * tables. But, an erroneous kernel fault occurring outside one of 13128c2ecf20Sopenharmony_ci * those areas which also holds mmap_lock might deadlock attempting 13138c2ecf20Sopenharmony_ci * to validate the fault against the address space. 13148c2ecf20Sopenharmony_ci * 13158c2ecf20Sopenharmony_ci * Only do the expensive exception table search when we might be at 13168c2ecf20Sopenharmony_ci * risk of a deadlock. This happens if we 13178c2ecf20Sopenharmony_ci * 1. Failed to acquire mmap_lock, and 13188c2ecf20Sopenharmony_ci * 2. The access did not originate in userspace. 13198c2ecf20Sopenharmony_ci */ 13208c2ecf20Sopenharmony_ci if (unlikely(!mmap_read_trylock(mm))) { 13218c2ecf20Sopenharmony_ci if (!user_mode(regs) && !search_exception_tables(regs->ip)) { 13228c2ecf20Sopenharmony_ci /* 13238c2ecf20Sopenharmony_ci * Fault from code in kernel from 13248c2ecf20Sopenharmony_ci * which we do not expect faults. 13258c2ecf20Sopenharmony_ci */ 13268c2ecf20Sopenharmony_ci bad_area_nosemaphore(regs, hw_error_code, address); 13278c2ecf20Sopenharmony_ci return; 13288c2ecf20Sopenharmony_ci } 13298c2ecf20Sopenharmony_ciretry: 13308c2ecf20Sopenharmony_ci mmap_read_lock(mm); 13318c2ecf20Sopenharmony_ci } else { 13328c2ecf20Sopenharmony_ci /* 13338c2ecf20Sopenharmony_ci * The above down_read_trylock() might have succeeded in 13348c2ecf20Sopenharmony_ci * which case we'll have missed the might_sleep() from 13358c2ecf20Sopenharmony_ci * down_read(): 13368c2ecf20Sopenharmony_ci */ 13378c2ecf20Sopenharmony_ci might_sleep(); 13388c2ecf20Sopenharmony_ci } 13398c2ecf20Sopenharmony_ci 13408c2ecf20Sopenharmony_ci vma = find_vma(mm, address); 13418c2ecf20Sopenharmony_ci if (unlikely(!vma)) { 13428c2ecf20Sopenharmony_ci bad_area(regs, hw_error_code, address); 13438c2ecf20Sopenharmony_ci return; 13448c2ecf20Sopenharmony_ci } 13458c2ecf20Sopenharmony_ci if (likely(vma->vm_start <= address)) 13468c2ecf20Sopenharmony_ci goto good_area; 13478c2ecf20Sopenharmony_ci if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) { 13488c2ecf20Sopenharmony_ci bad_area(regs, hw_error_code, address); 13498c2ecf20Sopenharmony_ci return; 13508c2ecf20Sopenharmony_ci } 13518c2ecf20Sopenharmony_ci if (unlikely(expand_stack(vma, address))) { 13528c2ecf20Sopenharmony_ci bad_area(regs, hw_error_code, address); 13538c2ecf20Sopenharmony_ci return; 13548c2ecf20Sopenharmony_ci } 13558c2ecf20Sopenharmony_ci 13568c2ecf20Sopenharmony_ci /* 13578c2ecf20Sopenharmony_ci * Ok, we have a good vm_area for this memory access, so 13588c2ecf20Sopenharmony_ci * we can handle it.. 13598c2ecf20Sopenharmony_ci */ 13608c2ecf20Sopenharmony_cigood_area: 13618c2ecf20Sopenharmony_ci if (unlikely(access_error(hw_error_code, vma))) { 13628c2ecf20Sopenharmony_ci bad_area_access_error(regs, hw_error_code, address, vma); 13638c2ecf20Sopenharmony_ci return; 13648c2ecf20Sopenharmony_ci } 13658c2ecf20Sopenharmony_ci 13668c2ecf20Sopenharmony_ci /* 13678c2ecf20Sopenharmony_ci * If for any reason at all we couldn't handle the fault, 13688c2ecf20Sopenharmony_ci * make sure we exit gracefully rather than endlessly redo 13698c2ecf20Sopenharmony_ci * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if 13708c2ecf20Sopenharmony_ci * we get VM_FAULT_RETRY back, the mmap_lock has been unlocked. 13718c2ecf20Sopenharmony_ci * 13728c2ecf20Sopenharmony_ci * Note that handle_userfault() may also release and reacquire mmap_lock 13738c2ecf20Sopenharmony_ci * (and not return with VM_FAULT_RETRY), when returning to userland to 13748c2ecf20Sopenharmony_ci * repeat the page fault later with a VM_FAULT_NOPAGE retval 13758c2ecf20Sopenharmony_ci * (potentially after handling any pending signal during the return to 13768c2ecf20Sopenharmony_ci * userland). The return to userland is identified whenever 13778c2ecf20Sopenharmony_ci * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags. 13788c2ecf20Sopenharmony_ci */ 13798c2ecf20Sopenharmony_ci fault = handle_mm_fault(vma, address, flags, regs); 13808c2ecf20Sopenharmony_ci 13818c2ecf20Sopenharmony_ci /* Quick path to respond to signals */ 13828c2ecf20Sopenharmony_ci if (fault_signal_pending(fault, regs)) { 13838c2ecf20Sopenharmony_ci if (!user_mode(regs)) 13848c2ecf20Sopenharmony_ci no_context(regs, hw_error_code, address, SIGBUS, 13858c2ecf20Sopenharmony_ci BUS_ADRERR); 13868c2ecf20Sopenharmony_ci return; 13878c2ecf20Sopenharmony_ci } 13888c2ecf20Sopenharmony_ci 13898c2ecf20Sopenharmony_ci /* 13908c2ecf20Sopenharmony_ci * If we need to retry the mmap_lock has already been released, 13918c2ecf20Sopenharmony_ci * and if there is a fatal signal pending there is no guarantee 13928c2ecf20Sopenharmony_ci * that we made any progress. Handle this case first. 13938c2ecf20Sopenharmony_ci */ 13948c2ecf20Sopenharmony_ci if (unlikely((fault & VM_FAULT_RETRY) && 13958c2ecf20Sopenharmony_ci (flags & FAULT_FLAG_ALLOW_RETRY))) { 13968c2ecf20Sopenharmony_ci flags |= FAULT_FLAG_TRIED; 13978c2ecf20Sopenharmony_ci goto retry; 13988c2ecf20Sopenharmony_ci } 13998c2ecf20Sopenharmony_ci 14008c2ecf20Sopenharmony_ci mmap_read_unlock(mm); 14018c2ecf20Sopenharmony_ci if (unlikely(fault & VM_FAULT_ERROR)) { 14028c2ecf20Sopenharmony_ci mm_fault_error(regs, hw_error_code, address, fault); 14038c2ecf20Sopenharmony_ci return; 14048c2ecf20Sopenharmony_ci } 14058c2ecf20Sopenharmony_ci 14068c2ecf20Sopenharmony_ci check_v8086_mode(regs, address, tsk); 14078c2ecf20Sopenharmony_ci} 14088c2ecf20Sopenharmony_ciNOKPROBE_SYMBOL(do_user_addr_fault); 14098c2ecf20Sopenharmony_ci 14108c2ecf20Sopenharmony_cistatic __always_inline void 14118c2ecf20Sopenharmony_citrace_page_fault_entries(struct pt_regs *regs, unsigned long error_code, 14128c2ecf20Sopenharmony_ci unsigned long address) 14138c2ecf20Sopenharmony_ci{ 14148c2ecf20Sopenharmony_ci if (!trace_pagefault_enabled()) 14158c2ecf20Sopenharmony_ci return; 14168c2ecf20Sopenharmony_ci 14178c2ecf20Sopenharmony_ci if (user_mode(regs)) 14188c2ecf20Sopenharmony_ci trace_page_fault_user(address, regs, error_code); 14198c2ecf20Sopenharmony_ci else 14208c2ecf20Sopenharmony_ci trace_page_fault_kernel(address, regs, error_code); 14218c2ecf20Sopenharmony_ci} 14228c2ecf20Sopenharmony_ci 14238c2ecf20Sopenharmony_cistatic __always_inline void 14248c2ecf20Sopenharmony_cihandle_page_fault(struct pt_regs *regs, unsigned long error_code, 14258c2ecf20Sopenharmony_ci unsigned long address) 14268c2ecf20Sopenharmony_ci{ 14278c2ecf20Sopenharmony_ci trace_page_fault_entries(regs, error_code, address); 14288c2ecf20Sopenharmony_ci 14298c2ecf20Sopenharmony_ci if (unlikely(kmmio_fault(regs, address))) 14308c2ecf20Sopenharmony_ci return; 14318c2ecf20Sopenharmony_ci 14328c2ecf20Sopenharmony_ci /* Was the fault on kernel-controlled part of the address space? */ 14338c2ecf20Sopenharmony_ci if (unlikely(fault_in_kernel_space(address))) { 14348c2ecf20Sopenharmony_ci do_kern_addr_fault(regs, error_code, address); 14358c2ecf20Sopenharmony_ci } else { 14368c2ecf20Sopenharmony_ci do_user_addr_fault(regs, error_code, address); 14378c2ecf20Sopenharmony_ci /* 14388c2ecf20Sopenharmony_ci * User address page fault handling might have reenabled 14398c2ecf20Sopenharmony_ci * interrupts. Fixing up all potential exit points of 14408c2ecf20Sopenharmony_ci * do_user_addr_fault() and its leaf functions is just not 14418c2ecf20Sopenharmony_ci * doable w/o creating an unholy mess or turning the code 14428c2ecf20Sopenharmony_ci * upside down. 14438c2ecf20Sopenharmony_ci */ 14448c2ecf20Sopenharmony_ci local_irq_disable(); 14458c2ecf20Sopenharmony_ci } 14468c2ecf20Sopenharmony_ci} 14478c2ecf20Sopenharmony_ci 14488c2ecf20Sopenharmony_ciDEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault) 14498c2ecf20Sopenharmony_ci{ 14508c2ecf20Sopenharmony_ci unsigned long address = read_cr2(); 14518c2ecf20Sopenharmony_ci irqentry_state_t state; 14528c2ecf20Sopenharmony_ci 14538c2ecf20Sopenharmony_ci prefetchw(¤t->mm->mmap_lock); 14548c2ecf20Sopenharmony_ci 14558c2ecf20Sopenharmony_ci /* 14568c2ecf20Sopenharmony_ci * KVM uses #PF vector to deliver 'page not present' events to guests 14578c2ecf20Sopenharmony_ci * (asynchronous page fault mechanism). The event happens when a 14588c2ecf20Sopenharmony_ci * userspace task is trying to access some valid (from guest's point of 14598c2ecf20Sopenharmony_ci * view) memory which is not currently mapped by the host (e.g. the 14608c2ecf20Sopenharmony_ci * memory is swapped out). Note, the corresponding "page ready" event 14618c2ecf20Sopenharmony_ci * which is injected when the memory becomes available, is delived via 14628c2ecf20Sopenharmony_ci * an interrupt mechanism and not a #PF exception 14638c2ecf20Sopenharmony_ci * (see arch/x86/kernel/kvm.c: sysvec_kvm_asyncpf_interrupt()). 14648c2ecf20Sopenharmony_ci * 14658c2ecf20Sopenharmony_ci * We are relying on the interrupted context being sane (valid RSP, 14668c2ecf20Sopenharmony_ci * relevant locks not held, etc.), which is fine as long as the 14678c2ecf20Sopenharmony_ci * interrupted context had IF=1. We are also relying on the KVM 14688c2ecf20Sopenharmony_ci * async pf type field and CR2 being read consistently instead of 14698c2ecf20Sopenharmony_ci * getting values from real and async page faults mixed up. 14708c2ecf20Sopenharmony_ci * 14718c2ecf20Sopenharmony_ci * Fingers crossed. 14728c2ecf20Sopenharmony_ci * 14738c2ecf20Sopenharmony_ci * The async #PF handling code takes care of idtentry handling 14748c2ecf20Sopenharmony_ci * itself. 14758c2ecf20Sopenharmony_ci */ 14768c2ecf20Sopenharmony_ci if (kvm_handle_async_pf(regs, (u32)address)) 14778c2ecf20Sopenharmony_ci return; 14788c2ecf20Sopenharmony_ci 14798c2ecf20Sopenharmony_ci /* 14808c2ecf20Sopenharmony_ci * Entry handling for valid #PF from kernel mode is slightly 14818c2ecf20Sopenharmony_ci * different: RCU is already watching and rcu_irq_enter() must not 14828c2ecf20Sopenharmony_ci * be invoked because a kernel fault on a user space address might 14838c2ecf20Sopenharmony_ci * sleep. 14848c2ecf20Sopenharmony_ci * 14858c2ecf20Sopenharmony_ci * In case the fault hit a RCU idle region the conditional entry 14868c2ecf20Sopenharmony_ci * code reenabled RCU to avoid subsequent wreckage which helps 14878c2ecf20Sopenharmony_ci * debugability. 14888c2ecf20Sopenharmony_ci */ 14898c2ecf20Sopenharmony_ci state = irqentry_enter(regs); 14908c2ecf20Sopenharmony_ci 14918c2ecf20Sopenharmony_ci instrumentation_begin(); 14928c2ecf20Sopenharmony_ci handle_page_fault(regs, error_code, address); 14938c2ecf20Sopenharmony_ci instrumentation_end(); 14948c2ecf20Sopenharmony_ci 14958c2ecf20Sopenharmony_ci irqentry_exit(regs, state); 14968c2ecf20Sopenharmony_ci} 1497