xref: /openbmc/linux/arch/x86/mm/fault.c (revision 5042d40a264c8a508d58ed71e4c07b05175b3635)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c61e211dSHarvey Harrison /*
3c61e211dSHarvey Harrison  *  Copyright (C) 1995  Linus Torvalds
4c61e211dSHarvey Harrison  *  Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
5f8eeb2e6SIngo Molnar  *  Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar
6c61e211dSHarvey Harrison  */
7a2bcd473SIngo Molnar #include <linux/sched.h>		/* test_thread_flag(), ...	*/
868db0cf1SIngo Molnar #include <linux/sched/task_stack.h>	/* task_stack_*(), ...		*/
9a2bcd473SIngo Molnar #include <linux/kdebug.h>		/* oops_begin/end, ...		*/
104cdf8dbeSLinus Torvalds #include <linux/extable.h>		/* search_exception_tables	*/
1157c8a661SMike Rapoport #include <linux/memblock.h>		/* max_low_pfn			*/
129326638cSMasami Hiramatsu #include <linux/kprobes.h>		/* NOKPROBE_SYMBOL, ...		*/
13a2bcd473SIngo Molnar #include <linux/mmiotrace.h>		/* kmmio_handler, ...		*/
14cdd6c482SIngo Molnar #include <linux/perf_event.h>		/* perf_sw_event		*/
15f672b49bSAndi Kleen #include <linux/hugetlb.h>		/* hstate_index_to_shift	*/
16268bb0ceSLinus Torvalds #include <linux/prefetch.h>		/* prefetchw			*/
1756dd9470SFrederic Weisbecker #include <linux/context_tracking.h>	/* exception_enter(), ...	*/
1870ffdb93SDavid Hildenbrand #include <linux/uaccess.h>		/* faulthandler_disabled()	*/
193425d934SSai Praneeth #include <linux/efi.h>			/* efi_recover_from_page_fault()*/
2050a7ca3cSSouptick Joarder #include <linux/mm_types.h>
21c61e211dSHarvey Harrison 
22019132ffSDave Hansen #include <asm/cpufeature.h>		/* boot_cpu_has, ...		*/
23a2bcd473SIngo Molnar #include <asm/traps.h>			/* dotraplinkage, ...		*/
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()			*/
283425d934SSai Praneeth #include <asm/efi.h>			/* efi_recover_from_page_fault()*/
29a1a371c4SAndy Lutomirski #include <asm/desc.h>			/* store_idt(), ...		*/
30d876b673SThomas Gleixner #include <asm/cpu_entry_area.h>		/* exception stack		*/
31186525bdSIngo Molnar #include <asm/pgtable_areas.h>		/* VMALLOC_START, ...		*/
32ef68017eSAndy Lutomirski #include <asm/kvm_para.h>		/* kvm_handle_async_pf		*/
33334872a0SSean Christopherson #include <asm/vdso.h>			/* fixup_vdso_exception()	*/
34c61e211dSHarvey Harrison 
35d34603b0SSeiji Aguchi #define CREATE_TRACE_POINTS
36d34603b0SSeiji Aguchi #include <asm/trace/exceptions.h>
37d34603b0SSeiji Aguchi 
38c61e211dSHarvey Harrison /*
39b319eed0SIngo Molnar  * Returns 0 if mmiotrace is disabled, or if the fault is not
40b319eed0SIngo Molnar  * handled by mmiotrace:
41b814d41fSIngo Molnar  */
429326638cSMasami Hiramatsu static nokprobe_inline int
4362c9295fSMasami Hiramatsu kmmio_fault(struct pt_regs *regs, unsigned long addr)
4486069782SPekka Paalanen {
450fd0e3daSPekka Paalanen 	if (unlikely(is_kmmio_active()))
460fd0e3daSPekka Paalanen 		if (kmmio_handler(regs, addr) == 1)
470fd0e3daSPekka Paalanen 			return -1;
480fd0e3daSPekka Paalanen 	return 0;
4986069782SPekka Paalanen }
5086069782SPekka Paalanen 
51c61e211dSHarvey Harrison /*
522d4a7167SIngo Molnar  * Prefetch quirks:
532d4a7167SIngo Molnar  *
542d4a7167SIngo Molnar  * 32-bit mode:
552d4a7167SIngo Molnar  *
56c61e211dSHarvey Harrison  *   Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
5735f1c89bSAndy Lutomirski  *   Check that here and ignore it.  This is AMD erratum #91.
58c61e211dSHarvey Harrison  *
592d4a7167SIngo Molnar  * 64-bit mode:
602d4a7167SIngo Molnar  *
61c61e211dSHarvey Harrison  *   Sometimes the CPU reports invalid exceptions on prefetch.
62c61e211dSHarvey Harrison  *   Check that here and ignore it.
63c61e211dSHarvey Harrison  *
642d4a7167SIngo Molnar  * Opcode checker based on code by Richard Brunner.
65c61e211dSHarvey Harrison  */
66107a0367SIngo Molnar static inline int
67107a0367SIngo Molnar check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
68107a0367SIngo Molnar 		      unsigned char opcode, int *prefetch)
69c61e211dSHarvey Harrison {
70107a0367SIngo Molnar 	unsigned char instr_hi = opcode & 0xf0;
71107a0367SIngo Molnar 	unsigned char instr_lo = opcode & 0x0f;
72c61e211dSHarvey Harrison 
73c61e211dSHarvey Harrison 	switch (instr_hi) {
74c61e211dSHarvey Harrison 	case 0x20:
75c61e211dSHarvey Harrison 	case 0x30:
76c61e211dSHarvey Harrison 		/*
77c61e211dSHarvey Harrison 		 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
78c61e211dSHarvey Harrison 		 * In X86_64 long mode, the CPU will signal invalid
79c61e211dSHarvey Harrison 		 * opcode if some of these prefixes are present so
80c61e211dSHarvey Harrison 		 * X86_64 will never get here anyway
81c61e211dSHarvey Harrison 		 */
82107a0367SIngo Molnar 		return ((instr_lo & 7) == 0x6);
83c61e211dSHarvey Harrison #ifdef CONFIG_X86_64
84c61e211dSHarvey Harrison 	case 0x40:
85c61e211dSHarvey Harrison 		/*
8635f1c89bSAndy Lutomirski 		 * In 64-bit mode 0x40..0x4F are valid REX prefixes
87c61e211dSHarvey Harrison 		 */
88318f5a2aSAndy Lutomirski 		return (!user_mode(regs) || user_64bit_mode(regs));
89c61e211dSHarvey Harrison #endif
90c61e211dSHarvey Harrison 	case 0x60:
91c61e211dSHarvey Harrison 		/* 0x64 thru 0x67 are valid prefixes in all modes. */
92107a0367SIngo Molnar 		return (instr_lo & 0xC) == 0x4;
93c61e211dSHarvey Harrison 	case 0xF0:
94c61e211dSHarvey Harrison 		/* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
95107a0367SIngo Molnar 		return !instr_lo || (instr_lo>>1) == 1;
96c61e211dSHarvey Harrison 	case 0x00:
97c61e211dSHarvey Harrison 		/* Prefetch instruction is 0x0F0D or 0x0F18 */
9825f12ae4SChristoph Hellwig 		if (get_kernel_nofault(opcode, instr))
99107a0367SIngo Molnar 			return 0;
100107a0367SIngo Molnar 
101107a0367SIngo Molnar 		*prefetch = (instr_lo == 0xF) &&
102107a0367SIngo Molnar 			(opcode == 0x0D || opcode == 0x18);
103107a0367SIngo Molnar 		return 0;
104107a0367SIngo Molnar 	default:
105107a0367SIngo Molnar 		return 0;
106107a0367SIngo Molnar 	}
107107a0367SIngo Molnar }
108107a0367SIngo Molnar 
109d24df8ecSAndy Lutomirski static bool is_amd_k8_pre_npt(void)
110d24df8ecSAndy Lutomirski {
111d24df8ecSAndy Lutomirski 	struct cpuinfo_x86 *c = &boot_cpu_data;
112d24df8ecSAndy Lutomirski 
113d24df8ecSAndy Lutomirski 	return unlikely(IS_ENABLED(CONFIG_CPU_SUP_AMD) &&
114d24df8ecSAndy Lutomirski 			c->x86_vendor == X86_VENDOR_AMD &&
115d24df8ecSAndy Lutomirski 			c->x86 == 0xf && c->x86_model < 0x40);
116d24df8ecSAndy Lutomirski }
117d24df8ecSAndy Lutomirski 
118107a0367SIngo Molnar static int
119107a0367SIngo Molnar is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
120107a0367SIngo Molnar {
121107a0367SIngo Molnar 	unsigned char *max_instr;
122107a0367SIngo Molnar 	unsigned char *instr;
123107a0367SIngo Molnar 	int prefetch = 0;
124107a0367SIngo Molnar 
125d24df8ecSAndy Lutomirski 	/* Erratum #91 affects AMD K8, pre-NPT CPUs */
126d24df8ecSAndy Lutomirski 	if (!is_amd_k8_pre_npt())
127d24df8ecSAndy Lutomirski 		return 0;
128d24df8ecSAndy Lutomirski 
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 
13935f1c89bSAndy Lutomirski 	/*
14035f1c89bSAndy Lutomirski 	 * This code has historically always bailed out if IP points to a
14135f1c89bSAndy Lutomirski 	 * not-present page (e.g. due to a race).  No one has ever
14235f1c89bSAndy Lutomirski 	 * complained about this.
14335f1c89bSAndy Lutomirski 	 */
14435f1c89bSAndy Lutomirski 	pagefault_disable();
145107a0367SIngo Molnar 
146107a0367SIngo Molnar 	while (instr < max_instr) {
147107a0367SIngo Molnar 		unsigned char opcode;
148c61e211dSHarvey Harrison 
14935f1c89bSAndy Lutomirski 		if (user_mode(regs)) {
15035f1c89bSAndy Lutomirski 			if (get_user(opcode, instr))
15135f1c89bSAndy Lutomirski 				break;
15235f1c89bSAndy Lutomirski 		} else {
15325f12ae4SChristoph Hellwig 			if (get_kernel_nofault(opcode, instr))
154c61e211dSHarvey Harrison 				break;
15535f1c89bSAndy Lutomirski 		}
156107a0367SIngo Molnar 
157107a0367SIngo Molnar 		instr++;
158107a0367SIngo Molnar 
159107a0367SIngo Molnar 		if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
160c61e211dSHarvey Harrison 			break;
161c61e211dSHarvey Harrison 	}
16235f1c89bSAndy Lutomirski 
16335f1c89bSAndy Lutomirski 	pagefault_enable();
164c61e211dSHarvey Harrison 	return prefetch;
165c61e211dSHarvey Harrison }
166c61e211dSHarvey Harrison 
167f2f13a85SIngo Molnar DEFINE_SPINLOCK(pgd_lock);
168f2f13a85SIngo Molnar LIST_HEAD(pgd_list);
1692d4a7167SIngo Molnar 
170f2f13a85SIngo Molnar #ifdef CONFIG_X86_32
171f2f13a85SIngo Molnar static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
172f2f13a85SIngo Molnar {
173f2f13a85SIngo Molnar 	unsigned index = pgd_index(address);
174f2f13a85SIngo Molnar 	pgd_t *pgd_k;
175e0c4f675SKirill A. Shutemov 	p4d_t *p4d, *p4d_k;
176f2f13a85SIngo Molnar 	pud_t *pud, *pud_k;
177f2f13a85SIngo Molnar 	pmd_t *pmd, *pmd_k;
178f2f13a85SIngo Molnar 
179f2f13a85SIngo Molnar 	pgd += index;
180f2f13a85SIngo Molnar 	pgd_k = init_mm.pgd + index;
181f2f13a85SIngo Molnar 
182f2f13a85SIngo Molnar 	if (!pgd_present(*pgd_k))
183f2f13a85SIngo Molnar 		return NULL;
184f2f13a85SIngo Molnar 
185f2f13a85SIngo Molnar 	/*
186f2f13a85SIngo Molnar 	 * set_pgd(pgd, *pgd_k); here would be useless on PAE
187f2f13a85SIngo Molnar 	 * and redundant with the set_pmd() on non-PAE. As would
188e0c4f675SKirill A. Shutemov 	 * set_p4d/set_pud.
189f2f13a85SIngo Molnar 	 */
190e0c4f675SKirill A. Shutemov 	p4d = p4d_offset(pgd, address);
191e0c4f675SKirill A. Shutemov 	p4d_k = p4d_offset(pgd_k, address);
192e0c4f675SKirill A. Shutemov 	if (!p4d_present(*p4d_k))
193e0c4f675SKirill A. Shutemov 		return NULL;
194e0c4f675SKirill A. Shutemov 
195e0c4f675SKirill A. Shutemov 	pud = pud_offset(p4d, address);
196e0c4f675SKirill A. Shutemov 	pud_k = pud_offset(p4d_k, address);
197f2f13a85SIngo Molnar 	if (!pud_present(*pud_k))
198f2f13a85SIngo Molnar 		return NULL;
199f2f13a85SIngo Molnar 
200f2f13a85SIngo Molnar 	pmd = pmd_offset(pud, address);
201f2f13a85SIngo Molnar 	pmd_k = pmd_offset(pud_k, address);
2028e998fc2SJoerg Roedel 
2038e998fc2SJoerg Roedel 	if (pmd_present(*pmd) != pmd_present(*pmd_k))
2048e998fc2SJoerg Roedel 		set_pmd(pmd, *pmd_k);
2058e998fc2SJoerg Roedel 
206f2f13a85SIngo Molnar 	if (!pmd_present(*pmd_k))
207f2f13a85SIngo Molnar 		return NULL;
208b8bcfe99SJeremy Fitzhardinge 	else
20951b75b5bSJoerg Roedel 		BUG_ON(pmd_pfn(*pmd) != pmd_pfn(*pmd_k));
210f2f13a85SIngo Molnar 
211f2f13a85SIngo Molnar 	return pmd_k;
212f2f13a85SIngo Molnar }
213f2f13a85SIngo Molnar 
2144819e15fSJoerg Roedel /*
2154819e15fSJoerg Roedel  *   Handle a fault on the vmalloc or module mapping area
2164819e15fSJoerg Roedel  *
2174819e15fSJoerg Roedel  *   This is needed because there is a race condition between the time
2184819e15fSJoerg Roedel  *   when the vmalloc mapping code updates the PMD to the point in time
2194819e15fSJoerg Roedel  *   where it synchronizes this update with the other page-tables in the
2204819e15fSJoerg Roedel  *   system.
2214819e15fSJoerg Roedel  *
2224819e15fSJoerg Roedel  *   In this race window another thread/CPU can map an area on the same
2234819e15fSJoerg Roedel  *   PMD, finds it already present and does not synchronize it with the
2244819e15fSJoerg Roedel  *   rest of the system yet. As a result v[mz]alloc might return areas
2254819e15fSJoerg Roedel  *   which are not mapped in every page-table in the system, causing an
2264819e15fSJoerg Roedel  *   unhandled page-fault when they are accessed.
2274819e15fSJoerg Roedel  */
2284819e15fSJoerg Roedel static noinline int vmalloc_fault(unsigned long address)
2294819e15fSJoerg Roedel {
2304819e15fSJoerg Roedel 	unsigned long pgd_paddr;
2314819e15fSJoerg Roedel 	pmd_t *pmd_k;
2324819e15fSJoerg Roedel 	pte_t *pte_k;
2334819e15fSJoerg Roedel 
2344819e15fSJoerg Roedel 	/* Make sure we are in vmalloc area: */
2354819e15fSJoerg Roedel 	if (!(address >= VMALLOC_START && address < VMALLOC_END))
2364819e15fSJoerg Roedel 		return -1;
2374819e15fSJoerg Roedel 
2384819e15fSJoerg Roedel 	/*
2394819e15fSJoerg Roedel 	 * Synchronize this task's top level page-table
2404819e15fSJoerg Roedel 	 * with the 'reference' page table.
2414819e15fSJoerg Roedel 	 *
2424819e15fSJoerg Roedel 	 * Do _not_ use "current" here. We might be inside
2434819e15fSJoerg Roedel 	 * an interrupt in the middle of a task switch..
2444819e15fSJoerg Roedel 	 */
2454819e15fSJoerg Roedel 	pgd_paddr = read_cr3_pa();
2464819e15fSJoerg Roedel 	pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
2474819e15fSJoerg Roedel 	if (!pmd_k)
2484819e15fSJoerg Roedel 		return -1;
2494819e15fSJoerg Roedel 
2504819e15fSJoerg Roedel 	if (pmd_large(*pmd_k))
2514819e15fSJoerg Roedel 		return 0;
2524819e15fSJoerg Roedel 
2534819e15fSJoerg Roedel 	pte_k = pte_offset_kernel(pmd_k, address);
2544819e15fSJoerg Roedel 	if (!pte_present(*pte_k))
2554819e15fSJoerg Roedel 		return -1;
2564819e15fSJoerg Roedel 
2574819e15fSJoerg Roedel 	return 0;
2584819e15fSJoerg Roedel }
2594819e15fSJoerg Roedel NOKPROBE_SYMBOL(vmalloc_fault);
2604819e15fSJoerg Roedel 
26186cf69f1SJoerg Roedel void arch_sync_kernel_mappings(unsigned long start, unsigned long end)
262f2f13a85SIngo Molnar {
26386cf69f1SJoerg Roedel 	unsigned long addr;
264f2f13a85SIngo Molnar 
26586cf69f1SJoerg Roedel 	for (addr = start & PMD_MASK;
26686cf69f1SJoerg Roedel 	     addr >= TASK_SIZE_MAX && addr < VMALLOC_END;
26786cf69f1SJoerg Roedel 	     addr += PMD_SIZE) {
268f2f13a85SIngo Molnar 		struct page *page;
269f2f13a85SIngo Molnar 
270a79e53d8SAndrea Arcangeli 		spin_lock(&pgd_lock);
271f2f13a85SIngo Molnar 		list_for_each_entry(page, &pgd_list, lru) {
272617d34d9SJeremy Fitzhardinge 			spinlock_t *pgt_lock;
273617d34d9SJeremy Fitzhardinge 
274a79e53d8SAndrea Arcangeli 			/* the pgt_lock only for Xen */
275617d34d9SJeremy Fitzhardinge 			pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
276617d34d9SJeremy Fitzhardinge 
277617d34d9SJeremy Fitzhardinge 			spin_lock(pgt_lock);
27886cf69f1SJoerg Roedel 			vmalloc_sync_one(page_address(page), addr);
279617d34d9SJeremy Fitzhardinge 			spin_unlock(pgt_lock);
280f2f13a85SIngo Molnar 		}
281a79e53d8SAndrea Arcangeli 		spin_unlock(&pgd_lock);
282f2f13a85SIngo Molnar 	}
283f2f13a85SIngo Molnar }
284f2f13a85SIngo Molnar 
285f2f13a85SIngo Molnar /*
286f2f13a85SIngo Molnar  * Did it hit the DOS screen memory VA from vm86 mode?
287f2f13a85SIngo Molnar  */
288f2f13a85SIngo Molnar static inline void
289f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address,
290f2f13a85SIngo Molnar 		 struct task_struct *tsk)
291f2f13a85SIngo Molnar {
2929fda6a06SBrian Gerst #ifdef CONFIG_VM86
293f2f13a85SIngo Molnar 	unsigned long bit;
294f2f13a85SIngo Molnar 
2959fda6a06SBrian Gerst 	if (!v8086_mode(regs) || !tsk->thread.vm86)
296f2f13a85SIngo Molnar 		return;
297f2f13a85SIngo Molnar 
298f2f13a85SIngo Molnar 	bit = (address - 0xA0000) >> PAGE_SHIFT;
299f2f13a85SIngo Molnar 	if (bit < 32)
3009fda6a06SBrian Gerst 		tsk->thread.vm86->screen_bitmap |= 1 << bit;
3019fda6a06SBrian Gerst #endif
302f2f13a85SIngo Molnar }
303c61e211dSHarvey Harrison 
304087975b0SAkinobu Mita static bool low_pfn(unsigned long pfn)
305087975b0SAkinobu Mita {
306087975b0SAkinobu Mita 	return pfn < max_low_pfn;
307087975b0SAkinobu Mita }
308087975b0SAkinobu Mita 
309cae30f82SAdrian Bunk static void dump_pagetable(unsigned long address)
310c61e211dSHarvey Harrison {
3116c690ee1SAndy Lutomirski 	pgd_t *base = __va(read_cr3_pa());
312087975b0SAkinobu Mita 	pgd_t *pgd = &base[pgd_index(address)];
313e0c4f675SKirill A. Shutemov 	p4d_t *p4d;
314e0c4f675SKirill A. Shutemov 	pud_t *pud;
315087975b0SAkinobu Mita 	pmd_t *pmd;
316087975b0SAkinobu Mita 	pte_t *pte;
3172d4a7167SIngo Molnar 
318c61e211dSHarvey Harrison #ifdef CONFIG_X86_PAE
31939e48d9bSJan Beulich 	pr_info("*pdpt = %016Lx ", pgd_val(*pgd));
320087975b0SAkinobu Mita 	if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
321087975b0SAkinobu Mita 		goto out;
32239e48d9bSJan Beulich #define pr_pde pr_cont
32339e48d9bSJan Beulich #else
32439e48d9bSJan Beulich #define pr_pde pr_info
325c61e211dSHarvey Harrison #endif
326e0c4f675SKirill A. Shutemov 	p4d = p4d_offset(pgd, address);
327e0c4f675SKirill A. Shutemov 	pud = pud_offset(p4d, address);
328e0c4f675SKirill A. Shutemov 	pmd = pmd_offset(pud, address);
32939e48d9bSJan Beulich 	pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
33039e48d9bSJan Beulich #undef pr_pde
331c61e211dSHarvey Harrison 
332c61e211dSHarvey Harrison 	/*
333c61e211dSHarvey Harrison 	 * We must not directly access the pte in the highpte
334c61e211dSHarvey Harrison 	 * case if the page table is located in highmem.
335c61e211dSHarvey Harrison 	 * And let's rather not kmap-atomic the pte, just in case
3362d4a7167SIngo Molnar 	 * it's allocated already:
337c61e211dSHarvey Harrison 	 */
338087975b0SAkinobu Mita 	if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
339087975b0SAkinobu Mita 		goto out;
3402d4a7167SIngo Molnar 
341087975b0SAkinobu Mita 	pte = pte_offset_kernel(pmd, address);
34239e48d9bSJan Beulich 	pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
343087975b0SAkinobu Mita out:
34439e48d9bSJan Beulich 	pr_cont("\n");
345f2f13a85SIngo Molnar }
346f2f13a85SIngo Molnar 
347f2f13a85SIngo Molnar #else /* CONFIG_X86_64: */
348f2f13a85SIngo Molnar 
349e05139f2SJan Beulich #ifdef CONFIG_CPU_SUP_AMD
350f2f13a85SIngo Molnar static const char errata93_warning[] =
351ad361c98SJoe Perches KERN_ERR
352ad361c98SJoe Perches "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
353ad361c98SJoe Perches "******* Working around it, but it may cause SEGVs or burn power.\n"
354ad361c98SJoe Perches "******* Please consider a BIOS update.\n"
355ad361c98SJoe Perches "******* Disabling USB legacy in the BIOS may also help.\n";
356e05139f2SJan Beulich #endif
357f2f13a85SIngo Molnar 
358f2f13a85SIngo Molnar /*
359f2f13a85SIngo Molnar  * No vm86 mode in 64-bit mode:
360f2f13a85SIngo Molnar  */
361f2f13a85SIngo Molnar static inline void
362f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address,
363f2f13a85SIngo Molnar 		 struct task_struct *tsk)
364f2f13a85SIngo Molnar {
365f2f13a85SIngo Molnar }
366f2f13a85SIngo Molnar 
367f2f13a85SIngo Molnar static int bad_address(void *p)
368f2f13a85SIngo Molnar {
369f2f13a85SIngo Molnar 	unsigned long dummy;
370f2f13a85SIngo Molnar 
37125f12ae4SChristoph Hellwig 	return get_kernel_nofault(dummy, (unsigned long *)p);
372f2f13a85SIngo Molnar }
373f2f13a85SIngo Molnar 
374f2f13a85SIngo Molnar static void dump_pagetable(unsigned long address)
375f2f13a85SIngo Molnar {
3766c690ee1SAndy Lutomirski 	pgd_t *base = __va(read_cr3_pa());
377087975b0SAkinobu Mita 	pgd_t *pgd = base + pgd_index(address);
378e0c4f675SKirill A. Shutemov 	p4d_t *p4d;
379c61e211dSHarvey Harrison 	pud_t *pud;
380c61e211dSHarvey Harrison 	pmd_t *pmd;
381c61e211dSHarvey Harrison 	pte_t *pte;
382c61e211dSHarvey Harrison 
3832d4a7167SIngo Molnar 	if (bad_address(pgd))
3842d4a7167SIngo Molnar 		goto bad;
3852d4a7167SIngo Molnar 
38639e48d9bSJan Beulich 	pr_info("PGD %lx ", pgd_val(*pgd));
3872d4a7167SIngo Molnar 
3882d4a7167SIngo Molnar 	if (!pgd_present(*pgd))
3892d4a7167SIngo Molnar 		goto out;
390c61e211dSHarvey Harrison 
391e0c4f675SKirill A. Shutemov 	p4d = p4d_offset(pgd, address);
392e0c4f675SKirill A. Shutemov 	if (bad_address(p4d))
393e0c4f675SKirill A. Shutemov 		goto bad;
394e0c4f675SKirill A. Shutemov 
39539e48d9bSJan Beulich 	pr_cont("P4D %lx ", p4d_val(*p4d));
396e0c4f675SKirill A. Shutemov 	if (!p4d_present(*p4d) || p4d_large(*p4d))
397e0c4f675SKirill A. Shutemov 		goto out;
398e0c4f675SKirill A. Shutemov 
399e0c4f675SKirill A. Shutemov 	pud = pud_offset(p4d, address);
4002d4a7167SIngo Molnar 	if (bad_address(pud))
4012d4a7167SIngo Molnar 		goto bad;
4022d4a7167SIngo Molnar 
40339e48d9bSJan Beulich 	pr_cont("PUD %lx ", pud_val(*pud));
404b5360222SAndi Kleen 	if (!pud_present(*pud) || pud_large(*pud))
4052d4a7167SIngo Molnar 		goto out;
406c61e211dSHarvey Harrison 
407c61e211dSHarvey Harrison 	pmd = pmd_offset(pud, address);
4082d4a7167SIngo Molnar 	if (bad_address(pmd))
4092d4a7167SIngo Molnar 		goto bad;
4102d4a7167SIngo Molnar 
41139e48d9bSJan Beulich 	pr_cont("PMD %lx ", pmd_val(*pmd));
4122d4a7167SIngo Molnar 	if (!pmd_present(*pmd) || pmd_large(*pmd))
4132d4a7167SIngo Molnar 		goto out;
414c61e211dSHarvey Harrison 
415c61e211dSHarvey Harrison 	pte = pte_offset_kernel(pmd, address);
4162d4a7167SIngo Molnar 	if (bad_address(pte))
4172d4a7167SIngo Molnar 		goto bad;
4182d4a7167SIngo Molnar 
41939e48d9bSJan Beulich 	pr_cont("PTE %lx", pte_val(*pte));
4202d4a7167SIngo Molnar out:
42139e48d9bSJan Beulich 	pr_cont("\n");
422c61e211dSHarvey Harrison 	return;
423c61e211dSHarvey Harrison bad:
42439e48d9bSJan Beulich 	pr_info("BAD\n");
425c61e211dSHarvey Harrison }
426c61e211dSHarvey Harrison 
427f2f13a85SIngo Molnar #endif /* CONFIG_X86_64 */
428c61e211dSHarvey Harrison 
4292d4a7167SIngo Molnar /*
4302d4a7167SIngo Molnar  * Workaround for K8 erratum #93 & buggy BIOS.
4312d4a7167SIngo Molnar  *
4322d4a7167SIngo Molnar  * BIOS SMM functions are required to use a specific workaround
4332d4a7167SIngo Molnar  * to avoid corruption of the 64bit RIP register on C stepping K8.
4342d4a7167SIngo Molnar  *
4352d4a7167SIngo Molnar  * A lot of BIOS that didn't get tested properly miss this.
4362d4a7167SIngo Molnar  *
4372d4a7167SIngo Molnar  * The OS sees this as a page fault with the upper 32bits of RIP cleared.
4382d4a7167SIngo Molnar  * Try to work around it here.
4392d4a7167SIngo Molnar  *
4402d4a7167SIngo Molnar  * Note we only handle faults in kernel here.
4412d4a7167SIngo Molnar  * Does nothing on 32-bit.
442c61e211dSHarvey Harrison  */
443c61e211dSHarvey Harrison static int is_errata93(struct pt_regs *regs, unsigned long address)
444c61e211dSHarvey Harrison {
445e05139f2SJan Beulich #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
446e05139f2SJan Beulich 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
447e05139f2SJan Beulich 	    || boot_cpu_data.x86 != 0xf)
448e05139f2SJan Beulich 		return 0;
449e05139f2SJan Beulich 
45003c81ea3SAndy Lutomirski 	if (user_mode(regs))
45103c81ea3SAndy Lutomirski 		return 0;
45203c81ea3SAndy Lutomirski 
453c61e211dSHarvey Harrison 	if (address != regs->ip)
454c61e211dSHarvey Harrison 		return 0;
4552d4a7167SIngo Molnar 
456c61e211dSHarvey Harrison 	if ((address >> 32) != 0)
457c61e211dSHarvey Harrison 		return 0;
4582d4a7167SIngo Molnar 
459c61e211dSHarvey Harrison 	address |= 0xffffffffUL << 32;
460c61e211dSHarvey Harrison 	if ((address >= (u64)_stext && address <= (u64)_etext) ||
461c61e211dSHarvey Harrison 	    (address >= MODULES_VADDR && address <= MODULES_END)) {
462a454ab31SIngo Molnar 		printk_once(errata93_warning);
463c61e211dSHarvey Harrison 		regs->ip = address;
464c61e211dSHarvey Harrison 		return 1;
465c61e211dSHarvey Harrison 	}
466c61e211dSHarvey Harrison #endif
467c61e211dSHarvey Harrison 	return 0;
468c61e211dSHarvey Harrison }
469c61e211dSHarvey Harrison 
470c61e211dSHarvey Harrison /*
4712d4a7167SIngo Molnar  * Work around K8 erratum #100 K8 in compat mode occasionally jumps
4722d4a7167SIngo Molnar  * to illegal addresses >4GB.
4732d4a7167SIngo Molnar  *
4742d4a7167SIngo Molnar  * We catch this in the page fault handler because these addresses
4752d4a7167SIngo Molnar  * are not reachable. Just detect this case and return.  Any code
476c61e211dSHarvey Harrison  * segment in LDT is compatibility mode.
477c61e211dSHarvey Harrison  */
478c61e211dSHarvey Harrison static int is_errata100(struct pt_regs *regs, unsigned long address)
479c61e211dSHarvey Harrison {
480c61e211dSHarvey Harrison #ifdef CONFIG_X86_64
4812d4a7167SIngo Molnar 	if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
482c61e211dSHarvey Harrison 		return 1;
483c61e211dSHarvey Harrison #endif
484c61e211dSHarvey Harrison 	return 0;
485c61e211dSHarvey Harrison }
486c61e211dSHarvey Harrison 
4873e77abdaSThomas Gleixner /* Pentium F0 0F C7 C8 bug workaround: */
488f42a40fdSAndy Lutomirski static int is_f00f_bug(struct pt_regs *regs, unsigned long error_code,
489f42a40fdSAndy Lutomirski 		       unsigned long address)
490c61e211dSHarvey Harrison {
491c61e211dSHarvey Harrison #ifdef CONFIG_X86_F00F_BUG
492f42a40fdSAndy Lutomirski 	if (boot_cpu_has_bug(X86_BUG_F00F) && !(error_code & X86_PF_USER) &&
493f42a40fdSAndy Lutomirski 	    idt_is_f00f_address(address)) {
49449893c5cSThomas Gleixner 		handle_invalid_op(regs);
495c61e211dSHarvey Harrison 		return 1;
496c61e211dSHarvey Harrison 	}
497c61e211dSHarvey Harrison #endif
498c61e211dSHarvey Harrison 	return 0;
499c61e211dSHarvey Harrison }
500c61e211dSHarvey Harrison 
501a1a371c4SAndy Lutomirski static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index)
502a1a371c4SAndy Lutomirski {
503a1a371c4SAndy Lutomirski 	u32 offset = (index >> 3) * sizeof(struct desc_struct);
504a1a371c4SAndy Lutomirski 	unsigned long addr;
505a1a371c4SAndy Lutomirski 	struct ldttss_desc desc;
506a1a371c4SAndy Lutomirski 
507a1a371c4SAndy Lutomirski 	if (index == 0) {
508a1a371c4SAndy Lutomirski 		pr_alert("%s: NULL\n", name);
509a1a371c4SAndy Lutomirski 		return;
510a1a371c4SAndy Lutomirski 	}
511a1a371c4SAndy Lutomirski 
512a1a371c4SAndy Lutomirski 	if (offset + sizeof(struct ldttss_desc) >= gdt->size) {
513a1a371c4SAndy Lutomirski 		pr_alert("%s: 0x%hx -- out of bounds\n", name, index);
514a1a371c4SAndy Lutomirski 		return;
515a1a371c4SAndy Lutomirski 	}
516a1a371c4SAndy Lutomirski 
517fe557319SChristoph Hellwig 	if (copy_from_kernel_nofault(&desc, (void *)(gdt->address + offset),
518a1a371c4SAndy Lutomirski 			      sizeof(struct ldttss_desc))) {
519a1a371c4SAndy Lutomirski 		pr_alert("%s: 0x%hx -- GDT entry is not readable\n",
520a1a371c4SAndy Lutomirski 			 name, index);
521a1a371c4SAndy Lutomirski 		return;
522a1a371c4SAndy Lutomirski 	}
523a1a371c4SAndy Lutomirski 
5245ccd3528SColin Ian King 	addr = desc.base0 | (desc.base1 << 16) | ((unsigned long)desc.base2 << 24);
525a1a371c4SAndy Lutomirski #ifdef CONFIG_X86_64
526a1a371c4SAndy Lutomirski 	addr |= ((u64)desc.base3 << 32);
527a1a371c4SAndy Lutomirski #endif
528a1a371c4SAndy Lutomirski 	pr_alert("%s: 0x%hx -- base=0x%lx limit=0x%x\n",
529a1a371c4SAndy Lutomirski 		 name, index, addr, (desc.limit0 | (desc.limit1 << 16)));
530a1a371c4SAndy Lutomirski }
531a1a371c4SAndy Lutomirski 
5322d4a7167SIngo Molnar static void
533a2aa52abSIngo Molnar show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long address)
534c61e211dSHarvey Harrison {
535c61e211dSHarvey Harrison 	if (!oops_may_print())
536c61e211dSHarvey Harrison 		return;
537c61e211dSHarvey Harrison 
5381067f030SRicardo Neri 	if (error_code & X86_PF_INSTR) {
53993809be8SHarvey Harrison 		unsigned int level;
540426e34ccSMatt Fleming 		pgd_t *pgd;
541426e34ccSMatt Fleming 		pte_t *pte;
5422d4a7167SIngo Molnar 
5436c690ee1SAndy Lutomirski 		pgd = __va(read_cr3_pa());
544426e34ccSMatt Fleming 		pgd += pgd_index(address);
545426e34ccSMatt Fleming 
546426e34ccSMatt Fleming 		pte = lookup_address_in_pgd(pgd, address, &level);
547c61e211dSHarvey Harrison 
5488f766149SIngo Molnar 		if (pte && pte_present(*pte) && !pte_exec(*pte))
549d79d0d8aSDmitry Vyukov 			pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n",
550d79d0d8aSDmitry Vyukov 				from_kuid(&init_user_ns, current_uid()));
551eff50c34SJiri Kosina 		if (pte && pte_present(*pte) && pte_exec(*pte) &&
552eff50c34SJiri Kosina 				(pgd_flags(*pgd) & _PAGE_USER) &&
5531e02ce4cSAndy Lutomirski 				(__read_cr4() & X86_CR4_SMEP))
554d79d0d8aSDmitry Vyukov 			pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n",
555d79d0d8aSDmitry Vyukov 				from_kuid(&init_user_ns, current_uid()));
556c61e211dSHarvey Harrison 	}
557fd40d6e3SHarvey Harrison 
558f28b11a2SSean Christopherson 	if (address < PAGE_SIZE && !user_mode(regs))
559ea2f8d60SBorislav Petkov 		pr_alert("BUG: kernel NULL pointer dereference, address: %px\n",
560f28b11a2SSean Christopherson 			(void *)address);
561f28b11a2SSean Christopherson 	else
562ea2f8d60SBorislav Petkov 		pr_alert("BUG: unable to handle page fault for address: %px\n",
5634188f063SDmitry Vyukov 			(void *)address);
5642d4a7167SIngo Molnar 
565ea2f8d60SBorislav Petkov 	pr_alert("#PF: %s %s in %s mode\n",
56618ea35c5SSean Christopherson 		 (error_code & X86_PF_USER)  ? "user" : "supervisor",
56718ea35c5SSean Christopherson 		 (error_code & X86_PF_INSTR) ? "instruction fetch" :
56818ea35c5SSean Christopherson 		 (error_code & X86_PF_WRITE) ? "write access" :
56918ea35c5SSean Christopherson 					       "read access",
57018ea35c5SSean Christopherson 			     user_mode(regs) ? "user" : "kernel");
57118ea35c5SSean Christopherson 	pr_alert("#PF: error_code(0x%04lx) - %s\n", error_code,
57218ea35c5SSean Christopherson 		 !(error_code & X86_PF_PROT) ? "not-present page" :
57318ea35c5SSean Christopherson 		 (error_code & X86_PF_RSVD)  ? "reserved bit violation" :
57418ea35c5SSean Christopherson 		 (error_code & X86_PF_PK)    ? "protection keys violation" :
57518ea35c5SSean Christopherson 					       "permissions violation");
576a2aa52abSIngo Molnar 
577a1a371c4SAndy Lutomirski 	if (!(error_code & X86_PF_USER) && user_mode(regs)) {
578a1a371c4SAndy Lutomirski 		struct desc_ptr idt, gdt;
579a1a371c4SAndy Lutomirski 		u16 ldtr, tr;
580a1a371c4SAndy Lutomirski 
581a1a371c4SAndy Lutomirski 		/*
582a1a371c4SAndy Lutomirski 		 * This can happen for quite a few reasons.  The more obvious
583a1a371c4SAndy Lutomirski 		 * ones are faults accessing the GDT, or LDT.  Perhaps
584a1a371c4SAndy Lutomirski 		 * surprisingly, if the CPU tries to deliver a benign or
585a1a371c4SAndy Lutomirski 		 * contributory exception from user code and gets a page fault
586a1a371c4SAndy Lutomirski 		 * during delivery, the page fault can be delivered as though
587a1a371c4SAndy Lutomirski 		 * it originated directly from user code.  This could happen
588a1a371c4SAndy Lutomirski 		 * due to wrong permissions on the IDT, GDT, LDT, TSS, or
589a1a371c4SAndy Lutomirski 		 * kernel or IST stack.
590a1a371c4SAndy Lutomirski 		 */
591a1a371c4SAndy Lutomirski 		store_idt(&idt);
592a1a371c4SAndy Lutomirski 
593a1a371c4SAndy Lutomirski 		/* Usable even on Xen PV -- it's just slow. */
594a1a371c4SAndy Lutomirski 		native_store_gdt(&gdt);
595a1a371c4SAndy Lutomirski 
596a1a371c4SAndy Lutomirski 		pr_alert("IDT: 0x%lx (limit=0x%hx) GDT: 0x%lx (limit=0x%hx)\n",
597a1a371c4SAndy Lutomirski 			 idt.address, idt.size, gdt.address, gdt.size);
598a1a371c4SAndy Lutomirski 
599a1a371c4SAndy Lutomirski 		store_ldt(ldtr);
600a1a371c4SAndy Lutomirski 		show_ldttss(&gdt, "LDTR", ldtr);
601a1a371c4SAndy Lutomirski 
602a1a371c4SAndy Lutomirski 		store_tr(tr);
603a1a371c4SAndy Lutomirski 		show_ldttss(&gdt, "TR", tr);
604a1a371c4SAndy Lutomirski 	}
605a1a371c4SAndy Lutomirski 
606c61e211dSHarvey Harrison 	dump_pagetable(address);
607c61e211dSHarvey Harrison }
608c61e211dSHarvey Harrison 
6092d4a7167SIngo Molnar static noinline void
6102d4a7167SIngo Molnar pgtable_bad(struct pt_regs *regs, unsigned long error_code,
6112d4a7167SIngo Molnar 	    unsigned long address)
612c61e211dSHarvey Harrison {
6132d4a7167SIngo Molnar 	struct task_struct *tsk;
6142d4a7167SIngo Molnar 	unsigned long flags;
6152d4a7167SIngo Molnar 	int sig;
6162d4a7167SIngo Molnar 
6172d4a7167SIngo Molnar 	flags = oops_begin();
6182d4a7167SIngo Molnar 	tsk = current;
6192d4a7167SIngo Molnar 	sig = SIGKILL;
620c61e211dSHarvey Harrison 
621c61e211dSHarvey Harrison 	printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
62292181f19SNick Piggin 	       tsk->comm, address);
623c61e211dSHarvey Harrison 	dump_pagetable(address);
6242d4a7167SIngo Molnar 
625c61e211dSHarvey Harrison 	if (__die("Bad pagetable", regs, error_code))
626874d93d1SAlexander van Heukelum 		sig = 0;
6272d4a7167SIngo Molnar 
628874d93d1SAlexander van Heukelum 	oops_end(flags, regs, sig);
629c61e211dSHarvey Harrison }
630c61e211dSHarvey Harrison 
631cd072dabSSean Christopherson static void sanitize_error_code(unsigned long address,
632cd072dabSSean Christopherson 				unsigned long *error_code)
633e49d3cbeSAndy Lutomirski {
634e49d3cbeSAndy Lutomirski 	/*
635e49d3cbeSAndy Lutomirski 	 * To avoid leaking information about the kernel page
636e49d3cbeSAndy Lutomirski 	 * table layout, pretend that user-mode accesses to
637e49d3cbeSAndy Lutomirski 	 * kernel addresses are always protection faults.
638e0a446ceSAndy Lutomirski 	 *
639e0a446ceSAndy Lutomirski 	 * NB: This means that failed vsyscalls with vsyscall=none
640e0a446ceSAndy Lutomirski 	 * will have the PROT bit.  This doesn't leak any
641e0a446ceSAndy Lutomirski 	 * information and does not appear to cause any problems.
642e49d3cbeSAndy Lutomirski 	 */
643e49d3cbeSAndy Lutomirski 	if (address >= TASK_SIZE_MAX)
644cd072dabSSean Christopherson 		*error_code |= X86_PF_PROT;
645cd072dabSSean Christopherson }
646cd072dabSSean Christopherson 
647cd072dabSSean Christopherson static void set_signal_archinfo(unsigned long address,
648cd072dabSSean Christopherson 				unsigned long error_code)
649cd072dabSSean Christopherson {
650cd072dabSSean Christopherson 	struct task_struct *tsk = current;
651e49d3cbeSAndy Lutomirski 
652e49d3cbeSAndy Lutomirski 	tsk->thread.trap_nr = X86_TRAP_PF;
653e49d3cbeSAndy Lutomirski 	tsk->thread.error_code = error_code | X86_PF_USER;
654e49d3cbeSAndy Lutomirski 	tsk->thread.cr2 = address;
655e49d3cbeSAndy Lutomirski }
656e49d3cbeSAndy Lutomirski 
6572d4a7167SIngo Molnar static noinline void
6582cc624b0SAndy Lutomirski page_fault_oops(struct pt_regs *regs, unsigned long error_code,
6592cc624b0SAndy Lutomirski 		unsigned long address)
66092181f19SNick Piggin {
66192181f19SNick Piggin 	unsigned long flags;
66292181f19SNick Piggin 	int sig;
66392181f19SNick Piggin 
664ebb53e25SAndy Lutomirski 	if (user_mode(regs)) {
665ebb53e25SAndy Lutomirski 		/*
6662cc624b0SAndy Lutomirski 		 * Implicit kernel access from user mode?  Skip the stack
6672cc624b0SAndy Lutomirski 		 * overflow and EFI special cases.
6682cc624b0SAndy Lutomirski 		 */
6692cc624b0SAndy Lutomirski 		goto oops;
6702cc624b0SAndy Lutomirski 	}
6712cc624b0SAndy Lutomirski 
6722cc624b0SAndy Lutomirski #ifdef CONFIG_VMAP_STACK
6732cc624b0SAndy Lutomirski 	/*
6742cc624b0SAndy Lutomirski 	 * Stack overflow?  During boot, we can fault near the initial
6752cc624b0SAndy Lutomirski 	 * stack in the direct map, but that's not an overflow -- check
6762cc624b0SAndy Lutomirski 	 * that we're in vmalloc space to avoid this.
6772cc624b0SAndy Lutomirski 	 */
6782cc624b0SAndy Lutomirski 	if (is_vmalloc_addr((void *)address) &&
6792cc624b0SAndy Lutomirski 	    (((unsigned long)current->stack - 1 - address < PAGE_SIZE) ||
6802cc624b0SAndy Lutomirski 	     address - ((unsigned long)current->stack + THREAD_SIZE) < PAGE_SIZE)) {
6812cc624b0SAndy Lutomirski 		unsigned long stack = __this_cpu_ist_top_va(DF) - sizeof(void *);
6822cc624b0SAndy Lutomirski 		/*
6832cc624b0SAndy Lutomirski 		 * We're likely to be running with very little stack space
6842cc624b0SAndy Lutomirski 		 * left.  It's plausible that we'd hit this condition but
6852cc624b0SAndy Lutomirski 		 * double-fault even before we get this far, in which case
6862cc624b0SAndy Lutomirski 		 * we're fine: the double-fault handler will deal with it.
6872cc624b0SAndy Lutomirski 		 *
6882cc624b0SAndy Lutomirski 		 * We don't want to make it all the way into the oops code
6892cc624b0SAndy Lutomirski 		 * and then double-fault, though, because we're likely to
6902cc624b0SAndy Lutomirski 		 * break the console driver and lose most of the stack dump.
6912cc624b0SAndy Lutomirski 		 */
6922cc624b0SAndy Lutomirski 		asm volatile ("movq %[stack], %%rsp\n\t"
6932cc624b0SAndy Lutomirski 			      "call handle_stack_overflow\n\t"
6942cc624b0SAndy Lutomirski 			      "1: jmp 1b"
6952cc624b0SAndy Lutomirski 			      : ASM_CALL_CONSTRAINT
6962cc624b0SAndy Lutomirski 			      : "D" ("kernel stack overflow (page fault)"),
6972cc624b0SAndy Lutomirski 				"S" (regs), "d" (address),
6982cc624b0SAndy Lutomirski 				[stack] "rm" (stack));
6992cc624b0SAndy Lutomirski 		unreachable();
7002cc624b0SAndy Lutomirski 	}
7012cc624b0SAndy Lutomirski #endif
7022cc624b0SAndy Lutomirski 
7032cc624b0SAndy Lutomirski 	/*
7042cc624b0SAndy Lutomirski 	 * Buggy firmware could access regions which might page fault, try to
7052cc624b0SAndy Lutomirski 	 * recover from such faults.
7062cc624b0SAndy Lutomirski 	 */
7072cc624b0SAndy Lutomirski 	if (IS_ENABLED(CONFIG_EFI))
7082cc624b0SAndy Lutomirski 		efi_recover_from_page_fault(address);
7092cc624b0SAndy Lutomirski 
7102cc624b0SAndy Lutomirski oops:
7112cc624b0SAndy Lutomirski 	/*
7122cc624b0SAndy Lutomirski 	 * Oops. The kernel tried to access some bad page. We'll have to
7132cc624b0SAndy Lutomirski 	 * terminate things with extreme prejudice:
7142cc624b0SAndy Lutomirski 	 */
7152cc624b0SAndy Lutomirski 	flags = oops_begin();
7162cc624b0SAndy Lutomirski 
7172cc624b0SAndy Lutomirski 	show_fault_oops(regs, error_code, address);
7182cc624b0SAndy Lutomirski 
7192cc624b0SAndy Lutomirski 	if (task_stack_end_corrupted(current))
7202cc624b0SAndy Lutomirski 		printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
7212cc624b0SAndy Lutomirski 
7222cc624b0SAndy Lutomirski 	sig = SIGKILL;
7232cc624b0SAndy Lutomirski 	if (__die("Oops", regs, error_code))
7242cc624b0SAndy Lutomirski 		sig = 0;
7252cc624b0SAndy Lutomirski 
7262cc624b0SAndy Lutomirski 	/* Executive summary in case the body of the oops scrolled away */
7272cc624b0SAndy Lutomirski 	printk(KERN_DEFAULT "CR2: %016lx\n", address);
7282cc624b0SAndy Lutomirski 
7292cc624b0SAndy Lutomirski 	oops_end(flags, regs, sig);
7302cc624b0SAndy Lutomirski }
7312cc624b0SAndy Lutomirski 
7322cc624b0SAndy Lutomirski static noinline void
7332cc624b0SAndy Lutomirski no_context(struct pt_regs *regs, unsigned long error_code,
7342cc624b0SAndy Lutomirski 	   unsigned long address, int signal, int si_code)
7352cc624b0SAndy Lutomirski {
7362cc624b0SAndy Lutomirski 	if (user_mode(regs)) {
7372cc624b0SAndy Lutomirski 		/*
738ebb53e25SAndy Lutomirski 		 * This is an implicit supervisor-mode access from user
739ebb53e25SAndy Lutomirski 		 * mode.  Bypass all the kernel-mode recovery code and just
740ebb53e25SAndy Lutomirski 		 * OOPS.
741ebb53e25SAndy Lutomirski 		 */
742ebb53e25SAndy Lutomirski 		goto oops;
743ebb53e25SAndy Lutomirski 	}
744ebb53e25SAndy Lutomirski 
74592181f19SNick Piggin 	/* Are we prepared to handle this kernel fault? */
74681fd9c18SJann Horn 	if (fixup_exception(regs, X86_TRAP_PF, error_code, address)) {
747c026b359SPeter Zijlstra 		/*
748c026b359SPeter Zijlstra 		 * Any interrupt that takes a fault gets the fixup. This makes
749c026b359SPeter Zijlstra 		 * the below recursive fault logic only apply to a faults from
750c026b359SPeter Zijlstra 		 * task context.
751c026b359SPeter Zijlstra 		 */
752c026b359SPeter Zijlstra 		if (in_interrupt())
753c026b359SPeter Zijlstra 			return;
754c026b359SPeter Zijlstra 
755c026b359SPeter Zijlstra 		/*
756c026b359SPeter Zijlstra 		 * Per the above we're !in_interrupt(), aka. task context.
757c026b359SPeter Zijlstra 		 *
758c026b359SPeter Zijlstra 		 * In this case we need to make sure we're not recursively
759c026b359SPeter Zijlstra 		 * faulting through the emulate_vsyscall() logic.
760c026b359SPeter Zijlstra 		 */
7612a53ccbcSIngo Molnar 		if (current->thread.sig_on_uaccess_err && signal) {
762cd072dabSSean Christopherson 			sanitize_error_code(address, &error_code);
763cd072dabSSean Christopherson 
764e49d3cbeSAndy Lutomirski 			set_signal_archinfo(address, error_code);
7654fc34901SAndy Lutomirski 
7664fc34901SAndy Lutomirski 			/* XXX: hwpoison faults will set the wrong code. */
7672e1661d2SEric W. Biederman 			force_sig_fault(signal, si_code, (void __user *)address);
7684fc34901SAndy Lutomirski 		}
769c026b359SPeter Zijlstra 
770c026b359SPeter Zijlstra 		/*
771c026b359SPeter Zijlstra 		 * Barring that, we can do the fixup and be happy.
772c026b359SPeter Zijlstra 		 */
77392181f19SNick Piggin 		return;
7744fc34901SAndy Lutomirski 	}
77592181f19SNick Piggin 
7766271cfdfSAndy Lutomirski 	/*
7772cc624b0SAndy Lutomirski 	 * AMD erratum #91 manifests as a spurious page fault on a PREFETCH
7782cc624b0SAndy Lutomirski 	 * instruction.
77992181f19SNick Piggin 	 */
78092181f19SNick Piggin 	if (is_prefetch(regs, error_code, address))
78192181f19SNick Piggin 		return;
78292181f19SNick Piggin 
783ebb53e25SAndy Lutomirski oops:
7842cc624b0SAndy Lutomirski 	page_fault_oops(regs, error_code, address);
78592181f19SNick Piggin }
78692181f19SNick Piggin 
7872d4a7167SIngo Molnar /*
7882d4a7167SIngo Molnar  * Print out info about fatal segfaults, if the show_unhandled_signals
7892d4a7167SIngo Molnar  * sysctl is set:
7902d4a7167SIngo Molnar  */
7912d4a7167SIngo Molnar static inline void
7922d4a7167SIngo Molnar show_signal_msg(struct pt_regs *regs, unsigned long error_code,
7932d4a7167SIngo Molnar 		unsigned long address, struct task_struct *tsk)
7942d4a7167SIngo Molnar {
795ba54d856SBorislav Petkov 	const char *loglvl = task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG;
796ba54d856SBorislav Petkov 
7972d4a7167SIngo Molnar 	if (!unhandled_signal(tsk, SIGSEGV))
7982d4a7167SIngo Molnar 		return;
7992d4a7167SIngo Molnar 
8002d4a7167SIngo Molnar 	if (!printk_ratelimit())
8012d4a7167SIngo Molnar 		return;
8022d4a7167SIngo Molnar 
80310a7e9d8SKees Cook 	printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx",
804ba54d856SBorislav Petkov 		loglvl, tsk->comm, task_pid_nr(tsk), address,
8052d4a7167SIngo Molnar 		(void *)regs->ip, (void *)regs->sp, error_code);
8062d4a7167SIngo Molnar 
8072d4a7167SIngo Molnar 	print_vma_addr(KERN_CONT " in ", regs->ip);
8082d4a7167SIngo Molnar 
8092d4a7167SIngo Molnar 	printk(KERN_CONT "\n");
810ba54d856SBorislav Petkov 
811342db04aSJann Horn 	show_opcodes(regs, loglvl);
8122d4a7167SIngo Molnar }
8132d4a7167SIngo Molnar 
81402e983b7SDave Hansen /*
81502e983b7SDave Hansen  * The (legacy) vsyscall page is the long page in the kernel portion
81602e983b7SDave Hansen  * of the address space that has user-accessible permissions.
81702e983b7SDave Hansen  */
81802e983b7SDave Hansen static bool is_vsyscall_vaddr(unsigned long vaddr)
81902e983b7SDave Hansen {
8203ae0ad92SDave Hansen 	return unlikely((vaddr & PAGE_MASK) == VSYSCALL_ADDR);
82102e983b7SDave Hansen }
82202e983b7SDave Hansen 
8232d4a7167SIngo Molnar static void
8242d4a7167SIngo Molnar __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
825419ceeb1SEric W. Biederman 		       unsigned long address, u32 pkey, int si_code)
82692181f19SNick Piggin {
82792181f19SNick Piggin 	struct task_struct *tsk = current;
82892181f19SNick Piggin 
829*5042d40aSAndy Lutomirski 	if (!user_mode(regs)) {
830*5042d40aSAndy Lutomirski 		no_context(regs, error_code, address, pkey, si_code);
831*5042d40aSAndy Lutomirski 		return;
832*5042d40aSAndy Lutomirski 	}
833*5042d40aSAndy Lutomirski 
834*5042d40aSAndy Lutomirski 	if (!(error_code & X86_PF_USER)) {
835*5042d40aSAndy Lutomirski 		/* Implicit user access to kernel memory -- just oops */
836*5042d40aSAndy Lutomirski 		page_fault_oops(regs, error_code, address);
837*5042d40aSAndy Lutomirski 		return;
838*5042d40aSAndy Lutomirski 	}
839*5042d40aSAndy Lutomirski 
84092181f19SNick Piggin 	/*
841*5042d40aSAndy Lutomirski 	 * User mode accesses just cause a SIGSEGV.
8422d4a7167SIngo Molnar 	 * It's possible to have interrupts off here:
84392181f19SNick Piggin 	 */
84492181f19SNick Piggin 	local_irq_enable();
84592181f19SNick Piggin 
84692181f19SNick Piggin 	/*
84792181f19SNick Piggin 	 * Valid to do another page fault here because this one came
8482d4a7167SIngo Molnar 	 * from user space:
84992181f19SNick Piggin 	 */
85092181f19SNick Piggin 	if (is_prefetch(regs, error_code, address))
85192181f19SNick Piggin 		return;
85292181f19SNick Piggin 
85392181f19SNick Piggin 	if (is_errata100(regs, address))
85492181f19SNick Piggin 		return;
85592181f19SNick Piggin 
856cd072dabSSean Christopherson 	sanitize_error_code(address, &error_code);
8573ae36655SAndy Lutomirski 
858334872a0SSean Christopherson 	if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address))
859334872a0SSean Christopherson 		return;
860334872a0SSean Christopherson 
861e575a86fSKees Cook 	if (likely(show_unhandled_signals))
8622d4a7167SIngo Molnar 		show_signal_msg(regs, error_code, address, tsk);
86392181f19SNick Piggin 
864e49d3cbeSAndy Lutomirski 	set_signal_archinfo(address, error_code);
8652d4a7167SIngo Molnar 
8669db812dbSEric W. Biederman 	if (si_code == SEGV_PKUERR)
867419ceeb1SEric W. Biederman 		force_sig_pkuerr((void __user *)address, pkey);
8689db812dbSEric W. Biederman 
8692e1661d2SEric W. Biederman 	force_sig_fault(SIGSEGV, si_code, (void __user *)address);
8702d4a7167SIngo Molnar 
871ca4c6a98SThomas Gleixner 	local_irq_disable();
87292181f19SNick Piggin }
87392181f19SNick Piggin 
8742d4a7167SIngo Molnar static noinline void
8752d4a7167SIngo Molnar bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
876768fd9c6SEric W. Biederman 		     unsigned long address)
87792181f19SNick Piggin {
878419ceeb1SEric W. Biederman 	__bad_area_nosemaphore(regs, error_code, address, 0, SEGV_MAPERR);
87992181f19SNick Piggin }
88092181f19SNick Piggin 
8812d4a7167SIngo Molnar static void
8822d4a7167SIngo Molnar __bad_area(struct pt_regs *regs, unsigned long error_code,
883419ceeb1SEric W. Biederman 	   unsigned long address, u32 pkey, int si_code)
88492181f19SNick Piggin {
88592181f19SNick Piggin 	struct mm_struct *mm = current->mm;
88692181f19SNick Piggin 	/*
88792181f19SNick Piggin 	 * Something tried to access memory that isn't in our memory map..
88892181f19SNick Piggin 	 * Fix it, but check if it's kernel or user first..
88992181f19SNick Piggin 	 */
890d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
89192181f19SNick Piggin 
892aba1ecd3SEric W. Biederman 	__bad_area_nosemaphore(regs, error_code, address, pkey, si_code);
89392181f19SNick Piggin }
89492181f19SNick Piggin 
8952d4a7167SIngo Molnar static noinline void
8962d4a7167SIngo Molnar bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
89792181f19SNick Piggin {
898419ceeb1SEric W. Biederman 	__bad_area(regs, error_code, address, 0, SEGV_MAPERR);
89992181f19SNick Piggin }
90092181f19SNick Piggin 
90133a709b2SDave Hansen static inline bool bad_area_access_from_pkeys(unsigned long error_code,
90233a709b2SDave Hansen 		struct vm_area_struct *vma)
90333a709b2SDave Hansen {
90407f146f5SDave Hansen 	/* This code is always called on the current mm */
90507f146f5SDave Hansen 	bool foreign = false;
90607f146f5SDave Hansen 
90733a709b2SDave Hansen 	if (!boot_cpu_has(X86_FEATURE_OSPKE))
90833a709b2SDave Hansen 		return false;
9091067f030SRicardo Neri 	if (error_code & X86_PF_PK)
91033a709b2SDave Hansen 		return true;
91107f146f5SDave Hansen 	/* this checks permission keys on the VMA: */
9121067f030SRicardo Neri 	if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
9131067f030SRicardo Neri 				       (error_code & X86_PF_INSTR), foreign))
91407f146f5SDave Hansen 		return true;
91533a709b2SDave Hansen 	return false;
91692181f19SNick Piggin }
91792181f19SNick Piggin 
9182d4a7167SIngo Molnar static noinline void
9192d4a7167SIngo Molnar bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
9207b2d0dbaSDave Hansen 		      unsigned long address, struct vm_area_struct *vma)
92192181f19SNick Piggin {
922019132ffSDave Hansen 	/*
923019132ffSDave Hansen 	 * This OSPKE check is not strictly necessary at runtime.
924019132ffSDave Hansen 	 * But, doing it this way allows compiler optimizations
925019132ffSDave Hansen 	 * if pkeys are compiled out.
926019132ffSDave Hansen 	 */
927aba1ecd3SEric W. Biederman 	if (bad_area_access_from_pkeys(error_code, vma)) {
9289db812dbSEric W. Biederman 		/*
9299db812dbSEric W. Biederman 		 * A protection key fault means that the PKRU value did not allow
9309db812dbSEric W. Biederman 		 * access to some PTE.  Userspace can figure out what PKRU was
9319db812dbSEric W. Biederman 		 * from the XSAVE state.  This function captures the pkey from
9329db812dbSEric W. Biederman 		 * the vma and passes it to userspace so userspace can discover
9339db812dbSEric W. Biederman 		 * which protection key was set on the PTE.
9349db812dbSEric W. Biederman 		 *
9359db812dbSEric W. Biederman 		 * If we get here, we know that the hardware signaled a X86_PF_PK
9369db812dbSEric W. Biederman 		 * fault and that there was a VMA once we got in the fault
9379db812dbSEric W. Biederman 		 * handler.  It does *not* guarantee that the VMA we find here
9389db812dbSEric W. Biederman 		 * was the one that we faulted on.
9399db812dbSEric W. Biederman 		 *
9409db812dbSEric W. Biederman 		 * 1. T1   : mprotect_key(foo, PAGE_SIZE, pkey=4);
9419db812dbSEric W. Biederman 		 * 2. T1   : set PKRU to deny access to pkey=4, touches page
9429db812dbSEric W. Biederman 		 * 3. T1   : faults...
9439db812dbSEric W. Biederman 		 * 4.    T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
944c1e8d7c6SMichel Lespinasse 		 * 5. T1   : enters fault handler, takes mmap_lock, etc...
9459db812dbSEric W. Biederman 		 * 6. T1   : reaches here, sees vma_pkey(vma)=5, when we really
9469db812dbSEric W. Biederman 		 *	     faulted on a pte with its pkey=4.
9479db812dbSEric W. Biederman 		 */
948aba1ecd3SEric W. Biederman 		u32 pkey = vma_pkey(vma);
9499db812dbSEric W. Biederman 
950419ceeb1SEric W. Biederman 		__bad_area(regs, error_code, address, pkey, SEGV_PKUERR);
951aba1ecd3SEric W. Biederman 	} else {
952419ceeb1SEric W. Biederman 		__bad_area(regs, error_code, address, 0, SEGV_ACCERR);
953aba1ecd3SEric W. Biederman 	}
95492181f19SNick Piggin }
95592181f19SNick Piggin 
9562d4a7167SIngo Molnar static void
957a6e04aa9SAndi Kleen do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
9583d353901SSouptick Joarder 	  vm_fault_t fault)
95992181f19SNick Piggin {
9602d4a7167SIngo Molnar 	/* Kernel mode? Handle exceptions or die: */
96156e62cd2SAndy Lutomirski 	if (!user_mode(regs)) {
9624fc34901SAndy Lutomirski 		no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
96396054569SLinus Torvalds 		return;
96496054569SLinus Torvalds 	}
9652d4a7167SIngo Molnar 
966cd1b68f0SIngo Molnar 	/* User-space => ok to do another page fault: */
96792181f19SNick Piggin 	if (is_prefetch(regs, error_code, address))
96892181f19SNick Piggin 		return;
9692d4a7167SIngo Molnar 
970cd072dabSSean Christopherson 	sanitize_error_code(address, &error_code);
971cd072dabSSean Christopherson 
972334872a0SSean Christopherson 	if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address))
973334872a0SSean Christopherson 		return;
974334872a0SSean Christopherson 
975e49d3cbeSAndy Lutomirski 	set_signal_archinfo(address, error_code);
9762d4a7167SIngo Molnar 
977a6e04aa9SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE
978f672b49bSAndi Kleen 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
979318759b4SEric W. Biederman 		struct task_struct *tsk = current;
98040e55394SEric W. Biederman 		unsigned lsb = 0;
98140e55394SEric W. Biederman 
98240e55394SEric W. Biederman 		pr_err(
983a6e04aa9SAndi Kleen 	"MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
984a6e04aa9SAndi Kleen 			tsk->comm, tsk->pid, address);
98540e55394SEric W. Biederman 		if (fault & VM_FAULT_HWPOISON_LARGE)
98640e55394SEric W. Biederman 			lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
98740e55394SEric W. Biederman 		if (fault & VM_FAULT_HWPOISON)
98840e55394SEric W. Biederman 			lsb = PAGE_SHIFT;
989f8eac901SEric W. Biederman 		force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb);
99040e55394SEric W. Biederman 		return;
991a6e04aa9SAndi Kleen 	}
992a6e04aa9SAndi Kleen #endif
9932e1661d2SEric W. Biederman 	force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
99492181f19SNick Piggin }
99592181f19SNick Piggin 
9968fed6200SDave Hansen static int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte)
997d8b57bb7SThomas Gleixner {
9981067f030SRicardo Neri 	if ((error_code & X86_PF_WRITE) && !pte_write(*pte))
999d8b57bb7SThomas Gleixner 		return 0;
10002d4a7167SIngo Molnar 
10011067f030SRicardo Neri 	if ((error_code & X86_PF_INSTR) && !pte_exec(*pte))
1002d8b57bb7SThomas Gleixner 		return 0;
1003d8b57bb7SThomas Gleixner 
1004d8b57bb7SThomas Gleixner 	return 1;
1005d8b57bb7SThomas Gleixner }
1006d8b57bb7SThomas Gleixner 
1007c61e211dSHarvey Harrison /*
10082d4a7167SIngo Molnar  * Handle a spurious fault caused by a stale TLB entry.
10092d4a7167SIngo Molnar  *
10102d4a7167SIngo Molnar  * This allows us to lazily refresh the TLB when increasing the
10112d4a7167SIngo Molnar  * permissions of a kernel page (RO -> RW or NX -> X).  Doing it
10122d4a7167SIngo Molnar  * eagerly is very expensive since that implies doing a full
10132d4a7167SIngo Molnar  * cross-processor TLB flush, even if no stale TLB entries exist
10142d4a7167SIngo Molnar  * on other processors.
10152d4a7167SIngo Molnar  *
101631668511SDavid Vrabel  * Spurious faults may only occur if the TLB contains an entry with
101731668511SDavid Vrabel  * fewer permission than the page table entry.  Non-present (P = 0)
101831668511SDavid Vrabel  * and reserved bit (R = 1) faults are never spurious.
101931668511SDavid Vrabel  *
10205b727a3bSJeremy Fitzhardinge  * There are no security implications to leaving a stale TLB when
10215b727a3bSJeremy Fitzhardinge  * increasing the permissions on a page.
102231668511SDavid Vrabel  *
102331668511SDavid Vrabel  * Returns non-zero if a spurious fault was handled, zero otherwise.
102431668511SDavid Vrabel  *
102531668511SDavid Vrabel  * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
102631668511SDavid Vrabel  * (Optional Invalidation).
10275b727a3bSJeremy Fitzhardinge  */
10289326638cSMasami Hiramatsu static noinline int
10298fed6200SDave Hansen spurious_kernel_fault(unsigned long error_code, unsigned long address)
10305b727a3bSJeremy Fitzhardinge {
10315b727a3bSJeremy Fitzhardinge 	pgd_t *pgd;
1032e0c4f675SKirill A. Shutemov 	p4d_t *p4d;
10335b727a3bSJeremy Fitzhardinge 	pud_t *pud;
10345b727a3bSJeremy Fitzhardinge 	pmd_t *pmd;
10355b727a3bSJeremy Fitzhardinge 	pte_t *pte;
10363c3e5694SSteven Rostedt 	int ret;
10375b727a3bSJeremy Fitzhardinge 
103831668511SDavid Vrabel 	/*
103931668511SDavid Vrabel 	 * Only writes to RO or instruction fetches from NX may cause
104031668511SDavid Vrabel 	 * spurious faults.
104131668511SDavid Vrabel 	 *
104231668511SDavid Vrabel 	 * These could be from user or supervisor accesses but the TLB
104331668511SDavid Vrabel 	 * is only lazily flushed after a kernel mapping protection
104431668511SDavid Vrabel 	 * change, so user accesses are not expected to cause spurious
104531668511SDavid Vrabel 	 * faults.
104631668511SDavid Vrabel 	 */
10471067f030SRicardo Neri 	if (error_code != (X86_PF_WRITE | X86_PF_PROT) &&
10481067f030SRicardo Neri 	    error_code != (X86_PF_INSTR | X86_PF_PROT))
10495b727a3bSJeremy Fitzhardinge 		return 0;
10505b727a3bSJeremy Fitzhardinge 
10515b727a3bSJeremy Fitzhardinge 	pgd = init_mm.pgd + pgd_index(address);
10525b727a3bSJeremy Fitzhardinge 	if (!pgd_present(*pgd))
10535b727a3bSJeremy Fitzhardinge 		return 0;
10545b727a3bSJeremy Fitzhardinge 
1055e0c4f675SKirill A. Shutemov 	p4d = p4d_offset(pgd, address);
1056e0c4f675SKirill A. Shutemov 	if (!p4d_present(*p4d))
1057e0c4f675SKirill A. Shutemov 		return 0;
1058e0c4f675SKirill A. Shutemov 
1059e0c4f675SKirill A. Shutemov 	if (p4d_large(*p4d))
10608fed6200SDave Hansen 		return spurious_kernel_fault_check(error_code, (pte_t *) p4d);
1061e0c4f675SKirill A. Shutemov 
1062e0c4f675SKirill A. Shutemov 	pud = pud_offset(p4d, address);
10635b727a3bSJeremy Fitzhardinge 	if (!pud_present(*pud))
10645b727a3bSJeremy Fitzhardinge 		return 0;
10655b727a3bSJeremy Fitzhardinge 
1066d8b57bb7SThomas Gleixner 	if (pud_large(*pud))
10678fed6200SDave Hansen 		return spurious_kernel_fault_check(error_code, (pte_t *) pud);
1068d8b57bb7SThomas Gleixner 
10695b727a3bSJeremy Fitzhardinge 	pmd = pmd_offset(pud, address);
10705b727a3bSJeremy Fitzhardinge 	if (!pmd_present(*pmd))
10715b727a3bSJeremy Fitzhardinge 		return 0;
10725b727a3bSJeremy Fitzhardinge 
1073d8b57bb7SThomas Gleixner 	if (pmd_large(*pmd))
10748fed6200SDave Hansen 		return spurious_kernel_fault_check(error_code, (pte_t *) pmd);
1075d8b57bb7SThomas Gleixner 
10765b727a3bSJeremy Fitzhardinge 	pte = pte_offset_kernel(pmd, address);
1077954f8571SAndrea Arcangeli 	if (!pte_present(*pte))
10785b727a3bSJeremy Fitzhardinge 		return 0;
10795b727a3bSJeremy Fitzhardinge 
10808fed6200SDave Hansen 	ret = spurious_kernel_fault_check(error_code, pte);
10813c3e5694SSteven Rostedt 	if (!ret)
10823c3e5694SSteven Rostedt 		return 0;
10833c3e5694SSteven Rostedt 
10843c3e5694SSteven Rostedt 	/*
10852d4a7167SIngo Molnar 	 * Make sure we have permissions in PMD.
10862d4a7167SIngo Molnar 	 * If not, then there's a bug in the page tables:
10873c3e5694SSteven Rostedt 	 */
10888fed6200SDave Hansen 	ret = spurious_kernel_fault_check(error_code, (pte_t *) pmd);
10893c3e5694SSteven Rostedt 	WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
10902d4a7167SIngo Molnar 
10913c3e5694SSteven Rostedt 	return ret;
10925b727a3bSJeremy Fitzhardinge }
10938fed6200SDave Hansen NOKPROBE_SYMBOL(spurious_kernel_fault);
10945b727a3bSJeremy Fitzhardinge 
1095c61e211dSHarvey Harrison int show_unhandled_signals = 1;
1096c61e211dSHarvey Harrison 
10972d4a7167SIngo Molnar static inline int
109868da336aSMichel Lespinasse access_error(unsigned long error_code, struct vm_area_struct *vma)
109992181f19SNick Piggin {
110007f146f5SDave Hansen 	/* This is only called for the current mm, so: */
110107f146f5SDave Hansen 	bool foreign = false;
1102e8c6226dSDave Hansen 
1103e8c6226dSDave Hansen 	/*
1104e8c6226dSDave Hansen 	 * Read or write was blocked by protection keys.  This is
1105e8c6226dSDave Hansen 	 * always an unconditional error and can never result in
1106e8c6226dSDave Hansen 	 * a follow-up action to resolve the fault, like a COW.
1107e8c6226dSDave Hansen 	 */
11081067f030SRicardo Neri 	if (error_code & X86_PF_PK)
1109e8c6226dSDave Hansen 		return 1;
1110e8c6226dSDave Hansen 
111133a709b2SDave Hansen 	/*
111274faeee0SSean Christopherson 	 * SGX hardware blocked the access.  This usually happens
111374faeee0SSean Christopherson 	 * when the enclave memory contents have been destroyed, like
111474faeee0SSean Christopherson 	 * after a suspend/resume cycle. In any case, the kernel can't
111574faeee0SSean Christopherson 	 * fix the cause of the fault.  Handle the fault as an access
111674faeee0SSean Christopherson 	 * error even in cases where no actual access violation
111774faeee0SSean Christopherson 	 * occurred.  This allows userspace to rebuild the enclave in
111874faeee0SSean Christopherson 	 * response to the signal.
111974faeee0SSean Christopherson 	 */
112074faeee0SSean Christopherson 	if (unlikely(error_code & X86_PF_SGX))
112174faeee0SSean Christopherson 		return 1;
112274faeee0SSean Christopherson 
112374faeee0SSean Christopherson 	/*
112407f146f5SDave Hansen 	 * Make sure to check the VMA so that we do not perform
11251067f030SRicardo Neri 	 * faults just to hit a X86_PF_PK as soon as we fill in a
112607f146f5SDave Hansen 	 * page.
112707f146f5SDave Hansen 	 */
11281067f030SRicardo Neri 	if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
11291067f030SRicardo Neri 				       (error_code & X86_PF_INSTR), foreign))
113007f146f5SDave Hansen 		return 1;
113133a709b2SDave Hansen 
11321067f030SRicardo Neri 	if (error_code & X86_PF_WRITE) {
11332d4a7167SIngo Molnar 		/* write, present and write, not present: */
113492181f19SNick Piggin 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
113592181f19SNick Piggin 			return 1;
11362d4a7167SIngo Molnar 		return 0;
11372d4a7167SIngo Molnar 	}
11382d4a7167SIngo Molnar 
11392d4a7167SIngo Molnar 	/* read, present: */
11401067f030SRicardo Neri 	if (unlikely(error_code & X86_PF_PROT))
114192181f19SNick Piggin 		return 1;
11422d4a7167SIngo Molnar 
11432d4a7167SIngo Molnar 	/* read, not present: */
11443122e80eSAnshuman Khandual 	if (unlikely(!vma_is_accessible(vma)))
114592181f19SNick Piggin 		return 1;
114692181f19SNick Piggin 
114792181f19SNick Piggin 	return 0;
114892181f19SNick Piggin }
114992181f19SNick Piggin 
115030063810STony Luck bool fault_in_kernel_space(unsigned long address)
11510973a06cSHiroshi Shimamoto {
11523ae0ad92SDave Hansen 	/*
11533ae0ad92SDave Hansen 	 * On 64-bit systems, the vsyscall page is at an address above
11543ae0ad92SDave Hansen 	 * TASK_SIZE_MAX, but is not considered part of the kernel
11553ae0ad92SDave Hansen 	 * address space.
11563ae0ad92SDave Hansen 	 */
11573ae0ad92SDave Hansen 	if (IS_ENABLED(CONFIG_X86_64) && is_vsyscall_vaddr(address))
11583ae0ad92SDave Hansen 		return false;
11593ae0ad92SDave Hansen 
1160d9517346SIngo Molnar 	return address >= TASK_SIZE_MAX;
11610973a06cSHiroshi Shimamoto }
11620973a06cSHiroshi Shimamoto 
1163c61e211dSHarvey Harrison /*
11648fed6200SDave Hansen  * Called for all faults where 'address' is part of the kernel address
11658fed6200SDave Hansen  * space.  Might get called for faults that originate from *code* that
11668fed6200SDave Hansen  * ran in userspace or the kernel.
1167c61e211dSHarvey Harrison  */
11688fed6200SDave Hansen static void
11698fed6200SDave Hansen do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code,
11700ac09f9fSJiri Olsa 		   unsigned long address)
1171c61e211dSHarvey Harrison {
11728fed6200SDave Hansen 	/*
1173367e3f1dSDave Hansen 	 * Protection keys exceptions only happen on user pages.  We
1174367e3f1dSDave Hansen 	 * have no user pages in the kernel portion of the address
1175367e3f1dSDave Hansen 	 * space, so do not expect them here.
1176367e3f1dSDave Hansen 	 */
1177367e3f1dSDave Hansen 	WARN_ON_ONCE(hw_error_code & X86_PF_PK);
1178367e3f1dSDave Hansen 
11794819e15fSJoerg Roedel #ifdef CONFIG_X86_32
11804819e15fSJoerg Roedel 	/*
11814819e15fSJoerg Roedel 	 * We can fault-in kernel-space virtual memory on-demand. The
11824819e15fSJoerg Roedel 	 * 'reference' page table is init_mm.pgd.
11834819e15fSJoerg Roedel 	 *
11844819e15fSJoerg Roedel 	 * NOTE! We MUST NOT take any locks for this case. We may
11854819e15fSJoerg Roedel 	 * be in an interrupt or a critical region, and should
11864819e15fSJoerg Roedel 	 * only copy the information from the master page table,
11874819e15fSJoerg Roedel 	 * nothing more.
11884819e15fSJoerg Roedel 	 *
11894819e15fSJoerg Roedel 	 * Before doing this on-demand faulting, ensure that the
11904819e15fSJoerg Roedel 	 * fault is not any of the following:
11914819e15fSJoerg Roedel 	 * 1. A fault on a PTE with a reserved bit set.
11924819e15fSJoerg Roedel 	 * 2. A fault caused by a user-mode access.  (Do not demand-
11934819e15fSJoerg Roedel 	 *    fault kernel memory due to user-mode accesses).
11944819e15fSJoerg Roedel 	 * 3. A fault caused by a page-level protection violation.
11954819e15fSJoerg Roedel 	 *    (A demand fault would be on a non-present page which
11964819e15fSJoerg Roedel 	 *     would have X86_PF_PROT==0).
11974819e15fSJoerg Roedel 	 *
11984819e15fSJoerg Roedel 	 * This is only needed to close a race condition on x86-32 in
11994819e15fSJoerg Roedel 	 * the vmalloc mapping/unmapping code. See the comment above
12004819e15fSJoerg Roedel 	 * vmalloc_fault() for details. On x86-64 the race does not
12014819e15fSJoerg Roedel 	 * exist as the vmalloc mappings don't need to be synchronized
12024819e15fSJoerg Roedel 	 * there.
12034819e15fSJoerg Roedel 	 */
12044819e15fSJoerg Roedel 	if (!(hw_error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) {
12054819e15fSJoerg Roedel 		if (vmalloc_fault(address) >= 0)
12064819e15fSJoerg Roedel 			return;
12074819e15fSJoerg Roedel 	}
12084819e15fSJoerg Roedel #endif
12094819e15fSJoerg Roedel 
1210f42a40fdSAndy Lutomirski 	if (is_f00f_bug(regs, hw_error_code, address))
1211f42a40fdSAndy Lutomirski 		return;
1212f42a40fdSAndy Lutomirski 
12138fed6200SDave Hansen 	/* Was the fault spurious, caused by lazy TLB invalidation? */
12148fed6200SDave Hansen 	if (spurious_kernel_fault(hw_error_code, address))
12158fed6200SDave Hansen 		return;
12168fed6200SDave Hansen 
12178fed6200SDave Hansen 	/* kprobes don't want to hook the spurious faults: */
1218b98cca44SAnshuman Khandual 	if (kprobe_page_fault(regs, X86_TRAP_PF))
12198fed6200SDave Hansen 		return;
12208fed6200SDave Hansen 
12218fed6200SDave Hansen 	/*
12228fed6200SDave Hansen 	 * Note, despite being a "bad area", there are quite a few
12238fed6200SDave Hansen 	 * acceptable reasons to get here, such as erratum fixups
12248fed6200SDave Hansen 	 * and handling kernel code that can fault, like get_user().
12258fed6200SDave Hansen 	 *
12268fed6200SDave Hansen 	 * Don't take the mm semaphore here. If we fixup a prefetch
12278fed6200SDave Hansen 	 * fault we could otherwise deadlock:
12288fed6200SDave Hansen 	 */
1229ba9f6f89SLinus Torvalds 	bad_area_nosemaphore(regs, hw_error_code, address);
12308fed6200SDave Hansen }
12318fed6200SDave Hansen NOKPROBE_SYMBOL(do_kern_addr_fault);
12328fed6200SDave Hansen 
123356e62cd2SAndy Lutomirski /*
123456e62cd2SAndy Lutomirski  * Handle faults in the user portion of the address space.  Nothing in here
123556e62cd2SAndy Lutomirski  * should check X86_PF_USER without a specific justification: for almost
123656e62cd2SAndy Lutomirski  * all purposes, we should treat a normal kernel access to user memory
123756e62cd2SAndy Lutomirski  * (e.g. get_user(), put_user(), etc.) the same as the WRUSS instruction.
123856e62cd2SAndy Lutomirski  * The one exception is AC flag handling, which is, per the x86
123956e62cd2SAndy Lutomirski  * architecture, special for WRUSS.
124056e62cd2SAndy Lutomirski  */
1241aa37c51bSDave Hansen static inline
1242aa37c51bSDave Hansen void do_user_addr_fault(struct pt_regs *regs,
1243ec352711SAndy Lutomirski 			unsigned long error_code,
1244c61e211dSHarvey Harrison 			unsigned long address)
1245c61e211dSHarvey Harrison {
1246c61e211dSHarvey Harrison 	struct vm_area_struct *vma;
1247c61e211dSHarvey Harrison 	struct task_struct *tsk;
12482d4a7167SIngo Molnar 	struct mm_struct *mm;
1249968614fcSPeter Xu 	vm_fault_t fault;
1250dde16072SPeter Xu 	unsigned int flags = FAULT_FLAG_DEFAULT;
1251c61e211dSHarvey Harrison 
1252c61e211dSHarvey Harrison 	tsk = current;
1253c61e211dSHarvey Harrison 	mm = tsk->mm;
12542d4a7167SIngo Molnar 
125503c81ea3SAndy Lutomirski 	if (unlikely((error_code & (X86_PF_USER | X86_PF_INSTR)) == X86_PF_INSTR)) {
125603c81ea3SAndy Lutomirski 		/*
125703c81ea3SAndy Lutomirski 		 * Whoops, this is kernel mode code trying to execute from
125803c81ea3SAndy Lutomirski 		 * user memory.  Unless this is AMD erratum #93, which
125903c81ea3SAndy Lutomirski 		 * corrupts RIP such that it looks like a user address,
126003c81ea3SAndy Lutomirski 		 * this is unrecoverable.  Don't even try to look up the
126103c81ea3SAndy Lutomirski 		 * VMA.
126203c81ea3SAndy Lutomirski 		 */
126303c81ea3SAndy Lutomirski 		if (is_errata93(regs, address))
126403c81ea3SAndy Lutomirski 			return;
126503c81ea3SAndy Lutomirski 
126603c81ea3SAndy Lutomirski 		bad_area_nosemaphore(regs, error_code, address);
126703c81ea3SAndy Lutomirski 		return;
126803c81ea3SAndy Lutomirski 	}
126903c81ea3SAndy Lutomirski 
12702d4a7167SIngo Molnar 	/* kprobes don't want to hook the spurious faults: */
1271b98cca44SAnshuman Khandual 	if (unlikely(kprobe_page_fault(regs, X86_TRAP_PF)))
12729be260a6SMasami Hiramatsu 		return;
1273e00b12e6SPeter Zijlstra 
12745b0c2cacSDave Hansen 	/*
12755b0c2cacSDave Hansen 	 * Reserved bits are never expected to be set on
12765b0c2cacSDave Hansen 	 * entries in the user portion of the page tables.
12775b0c2cacSDave Hansen 	 */
1278ec352711SAndy Lutomirski 	if (unlikely(error_code & X86_PF_RSVD))
1279ec352711SAndy Lutomirski 		pgtable_bad(regs, error_code, address);
1280e00b12e6SPeter Zijlstra 
12815b0c2cacSDave Hansen 	/*
1282e50928d7SAndy Lutomirski 	 * If SMAP is on, check for invalid kernel (supervisor) access to user
1283e50928d7SAndy Lutomirski 	 * pages in the user address space.  The odd case here is WRUSS,
1284e50928d7SAndy Lutomirski 	 * which, according to the preliminary documentation, does not respect
1285e50928d7SAndy Lutomirski 	 * SMAP and will have the USER bit set so, in all cases, SMAP
1286e50928d7SAndy Lutomirski 	 * enforcement appears to be consistent with the USER bit.
12875b0c2cacSDave Hansen 	 */
1288a15781b5SAndy Lutomirski 	if (unlikely(cpu_feature_enabled(X86_FEATURE_SMAP) &&
1289ec352711SAndy Lutomirski 		     !(error_code & X86_PF_USER) &&
1290e50928d7SAndy Lutomirski 		     !(regs->flags & X86_EFLAGS_AC)))
1291a15781b5SAndy Lutomirski 	{
1292ec352711SAndy Lutomirski 		bad_area_nosemaphore(regs, error_code, address);
1293e00b12e6SPeter Zijlstra 		return;
1294e00b12e6SPeter Zijlstra 	}
1295e00b12e6SPeter Zijlstra 
1296e00b12e6SPeter Zijlstra 	/*
1297e00b12e6SPeter Zijlstra 	 * If we're in an interrupt, have no user context or are running
129870ffdb93SDavid Hildenbrand 	 * in a region with pagefaults disabled then we must not take the fault
1299e00b12e6SPeter Zijlstra 	 */
130070ffdb93SDavid Hildenbrand 	if (unlikely(faulthandler_disabled() || !mm)) {
1301ec352711SAndy Lutomirski 		bad_area_nosemaphore(regs, error_code, address);
1302e00b12e6SPeter Zijlstra 		return;
1303e00b12e6SPeter Zijlstra 	}
1304e00b12e6SPeter Zijlstra 
1305c61e211dSHarvey Harrison 	/*
1306891cffbdSLinus Torvalds 	 * It's safe to allow irq's after cr2 has been saved and the
1307891cffbdSLinus Torvalds 	 * vmalloc fault has been handled.
1308891cffbdSLinus Torvalds 	 *
1309891cffbdSLinus Torvalds 	 * User-mode registers count as a user access even for any
13102d4a7167SIngo Molnar 	 * potential system fault or CPU buglet:
1311c61e211dSHarvey Harrison 	 */
1312f39b6f0eSAndy Lutomirski 	if (user_mode(regs)) {
1313891cffbdSLinus Torvalds 		local_irq_enable();
1314759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
13152d4a7167SIngo Molnar 	} else {
13162d4a7167SIngo Molnar 		if (regs->flags & X86_EFLAGS_IF)
1317c61e211dSHarvey Harrison 			local_irq_enable();
13182d4a7167SIngo Molnar 	}
1319c61e211dSHarvey Harrison 
1320a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
13217dd1fcc2SPeter Zijlstra 
1322ec352711SAndy Lutomirski 	if (error_code & X86_PF_WRITE)
1323759496baSJohannes Weiner 		flags |= FAULT_FLAG_WRITE;
1324ec352711SAndy Lutomirski 	if (error_code & X86_PF_INSTR)
1325d61172b4SDave Hansen 		flags |= FAULT_FLAG_INSTRUCTION;
1326759496baSJohannes Weiner 
13273ae0ad92SDave Hansen #ifdef CONFIG_X86_64
13283a1dfe6eSIngo Molnar 	/*
1329918ce325SAndy Lutomirski 	 * Faults in the vsyscall page might need emulation.  The
1330918ce325SAndy Lutomirski 	 * vsyscall page is at a high address (>PAGE_OFFSET), but is
1331918ce325SAndy Lutomirski 	 * considered to be part of the user address space.
1332c61e211dSHarvey Harrison 	 *
13333ae0ad92SDave Hansen 	 * The vsyscall page does not have a "real" VMA, so do this
13343ae0ad92SDave Hansen 	 * emulation before we go searching for VMAs.
1335e0a446ceSAndy Lutomirski 	 *
1336e0a446ceSAndy Lutomirski 	 * PKRU never rejects instruction fetches, so we don't need
1337e0a446ceSAndy Lutomirski 	 * to consider the PF_PK bit.
13383ae0ad92SDave Hansen 	 */
1339918ce325SAndy Lutomirski 	if (is_vsyscall_vaddr(address)) {
1340ec352711SAndy Lutomirski 		if (emulate_vsyscall(error_code, regs, address))
13413ae0ad92SDave Hansen 			return;
13423ae0ad92SDave Hansen 	}
13433ae0ad92SDave Hansen #endif
13443ae0ad92SDave Hansen 
1345c61e211dSHarvey Harrison 	/*
134688259744SDave Hansen 	 * Kernel-mode access to the user address space should only occur
134788259744SDave Hansen 	 * on well-defined single instructions listed in the exception
134888259744SDave Hansen 	 * tables.  But, an erroneous kernel fault occurring outside one of
1349c1e8d7c6SMichel Lespinasse 	 * those areas which also holds mmap_lock might deadlock attempting
135088259744SDave Hansen 	 * to validate the fault against the address space.
1351c61e211dSHarvey Harrison 	 *
135288259744SDave Hansen 	 * Only do the expensive exception table search when we might be at
135388259744SDave Hansen 	 * risk of a deadlock.  This happens if we
1354c1e8d7c6SMichel Lespinasse 	 * 1. Failed to acquire mmap_lock, and
13556344be60SAndy Lutomirski 	 * 2. The access did not originate in userspace.
1356c61e211dSHarvey Harrison 	 */
1357d8ed45c5SMichel Lespinasse 	if (unlikely(!mmap_read_trylock(mm))) {
13586344be60SAndy Lutomirski 		if (!user_mode(regs) && !search_exception_tables(regs->ip)) {
135988259744SDave Hansen 			/*
136088259744SDave Hansen 			 * Fault from code in kernel from
136188259744SDave Hansen 			 * which we do not expect faults.
136288259744SDave Hansen 			 */
1363ec352711SAndy Lutomirski 			bad_area_nosemaphore(regs, error_code, address);
136492181f19SNick Piggin 			return;
136592181f19SNick Piggin 		}
1366d065bd81SMichel Lespinasse retry:
1367d8ed45c5SMichel Lespinasse 		mmap_read_lock(mm);
136801006074SPeter Zijlstra 	} else {
136901006074SPeter Zijlstra 		/*
13702d4a7167SIngo Molnar 		 * The above down_read_trylock() might have succeeded in
13712d4a7167SIngo Molnar 		 * which case we'll have missed the might_sleep() from
13722d4a7167SIngo Molnar 		 * down_read():
137301006074SPeter Zijlstra 		 */
137401006074SPeter Zijlstra 		might_sleep();
1375c61e211dSHarvey Harrison 	}
1376c61e211dSHarvey Harrison 
1377c61e211dSHarvey Harrison 	vma = find_vma(mm, address);
137892181f19SNick Piggin 	if (unlikely(!vma)) {
1379ec352711SAndy Lutomirski 		bad_area(regs, error_code, address);
138092181f19SNick Piggin 		return;
138192181f19SNick Piggin 	}
138292181f19SNick Piggin 	if (likely(vma->vm_start <= address))
1383c61e211dSHarvey Harrison 		goto good_area;
138492181f19SNick Piggin 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1385ec352711SAndy Lutomirski 		bad_area(regs, error_code, address);
138692181f19SNick Piggin 		return;
138792181f19SNick Piggin 	}
138892181f19SNick Piggin 	if (unlikely(expand_stack(vma, address))) {
1389ec352711SAndy Lutomirski 		bad_area(regs, error_code, address);
139092181f19SNick Piggin 		return;
139192181f19SNick Piggin 	}
139292181f19SNick Piggin 
1393c61e211dSHarvey Harrison 	/*
1394c61e211dSHarvey Harrison 	 * Ok, we have a good vm_area for this memory access, so
1395c61e211dSHarvey Harrison 	 * we can handle it..
1396c61e211dSHarvey Harrison 	 */
1397c61e211dSHarvey Harrison good_area:
1398ec352711SAndy Lutomirski 	if (unlikely(access_error(error_code, vma))) {
1399ec352711SAndy Lutomirski 		bad_area_access_error(regs, error_code, address, vma);
140092181f19SNick Piggin 		return;
1401c61e211dSHarvey Harrison 	}
1402c61e211dSHarvey Harrison 
1403c61e211dSHarvey Harrison 	/*
1404c61e211dSHarvey Harrison 	 * If for any reason at all we couldn't handle the fault,
1405c61e211dSHarvey Harrison 	 * make sure we exit gracefully rather than endlessly redo
14069a95f3cfSPaul Cassella 	 * the fault.  Since we never set FAULT_FLAG_RETRY_NOWAIT, if
1407c1e8d7c6SMichel Lespinasse 	 * we get VM_FAULT_RETRY back, the mmap_lock has been unlocked.
1408cb0631fdSVlastimil Babka 	 *
1409c1e8d7c6SMichel Lespinasse 	 * Note that handle_userfault() may also release and reacquire mmap_lock
1410cb0631fdSVlastimil Babka 	 * (and not return with VM_FAULT_RETRY), when returning to userland to
1411cb0631fdSVlastimil Babka 	 * repeat the page fault later with a VM_FAULT_NOPAGE retval
1412cb0631fdSVlastimil Babka 	 * (potentially after handling any pending signal during the return to
1413cb0631fdSVlastimil Babka 	 * userland). The return to userland is identified whenever
1414cb0631fdSVlastimil Babka 	 * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags.
1415c61e211dSHarvey Harrison 	 */
1416968614fcSPeter Xu 	fault = handle_mm_fault(vma, address, flags, regs);
14172d4a7167SIngo Molnar 
141839678191SPeter Xu 	if (fault_signal_pending(fault, regs)) {
1419ef2544fbSAndy Lutomirski 		/*
1420ef2544fbSAndy Lutomirski 		 * Quick path to respond to signals.  The core mm code
1421ef2544fbSAndy Lutomirski 		 * has unlocked the mm for us if we get here.
1422ef2544fbSAndy Lutomirski 		 */
142339678191SPeter Xu 		if (!user_mode(regs))
1424ec352711SAndy Lutomirski 			no_context(regs, error_code, address, SIGBUS,
142539678191SPeter Xu 				   BUS_ADRERR);
142639678191SPeter Xu 		return;
142739678191SPeter Xu 	}
142839678191SPeter Xu 
14293a13c4d7SJohannes Weiner 	/*
1430c1e8d7c6SMichel Lespinasse 	 * If we need to retry the mmap_lock has already been released,
143126178ec1SLinus Torvalds 	 * and if there is a fatal signal pending there is no guarantee
143226178ec1SLinus Torvalds 	 * that we made any progress. Handle this case first.
14333a13c4d7SJohannes Weiner 	 */
143439678191SPeter Xu 	if (unlikely((fault & VM_FAULT_RETRY) &&
143539678191SPeter Xu 		     (flags & FAULT_FLAG_ALLOW_RETRY))) {
143626178ec1SLinus Torvalds 		flags |= FAULT_FLAG_TRIED;
143726178ec1SLinus Torvalds 		goto retry;
143826178ec1SLinus Torvalds 	}
143926178ec1SLinus Torvalds 
1440d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
1441ec352711SAndy Lutomirski 	if (likely(!(fault & VM_FAULT_ERROR)))
144237b23e05SKOSAKI Motohiro 		return;
1443ec352711SAndy Lutomirski 
144456e62cd2SAndy Lutomirski 	if (fatal_signal_pending(current) && !user_mode(regs)) {
1445ec352711SAndy Lutomirski 		no_context(regs, error_code, address, 0, 0);
1446ec352711SAndy Lutomirski 		return;
1447ec352711SAndy Lutomirski 	}
1448ec352711SAndy Lutomirski 
1449ec352711SAndy Lutomirski 	if (fault & VM_FAULT_OOM) {
1450ec352711SAndy Lutomirski 		/* Kernel mode? Handle exceptions or die: */
145156e62cd2SAndy Lutomirski 		if (!user_mode(regs)) {
1452ec352711SAndy Lutomirski 			no_context(regs, error_code, address,
1453ec352711SAndy Lutomirski 				   SIGSEGV, SEGV_MAPERR);
1454ec352711SAndy Lutomirski 			return;
1455ec352711SAndy Lutomirski 		}
1456ec352711SAndy Lutomirski 
1457ec352711SAndy Lutomirski 		/*
1458ec352711SAndy Lutomirski 		 * We ran out of memory, call the OOM killer, and return the
1459ec352711SAndy Lutomirski 		 * userspace (which will retry the fault, or kill us if we got
1460ec352711SAndy Lutomirski 		 * oom-killed):
1461ec352711SAndy Lutomirski 		 */
1462ec352711SAndy Lutomirski 		pagefault_out_of_memory();
1463ec352711SAndy Lutomirski 	} else {
1464ec352711SAndy Lutomirski 		if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
1465ec352711SAndy Lutomirski 			     VM_FAULT_HWPOISON_LARGE))
1466ec352711SAndy Lutomirski 			do_sigbus(regs, error_code, address, fault);
1467ec352711SAndy Lutomirski 		else if (fault & VM_FAULT_SIGSEGV)
1468ec352711SAndy Lutomirski 			bad_area_nosemaphore(regs, error_code, address);
1469ec352711SAndy Lutomirski 		else
1470ec352711SAndy Lutomirski 			BUG();
147137b23e05SKOSAKI Motohiro 	}
147237b23e05SKOSAKI Motohiro 
14738c938f9fSIngo Molnar 	check_v8086_mode(regs, address, tsk);
1474c61e211dSHarvey Harrison }
1475aa37c51bSDave Hansen NOKPROBE_SYMBOL(do_user_addr_fault);
1476aa37c51bSDave Hansen 
1477a0d14b89SPeter Zijlstra static __always_inline void
1478a0d14b89SPeter Zijlstra trace_page_fault_entries(struct pt_regs *regs, unsigned long error_code,
1479a0d14b89SPeter Zijlstra 			 unsigned long address)
1480d34603b0SSeiji Aguchi {
1481a0d14b89SPeter Zijlstra 	if (!trace_pagefault_enabled())
1482a0d14b89SPeter Zijlstra 		return;
1483a0d14b89SPeter Zijlstra 
1484d34603b0SSeiji Aguchi 	if (user_mode(regs))
1485d4078e23SPeter Zijlstra 		trace_page_fault_user(address, regs, error_code);
1486d34603b0SSeiji Aguchi 	else
1487d4078e23SPeter Zijlstra 		trace_page_fault_kernel(address, regs, error_code);
1488d34603b0SSeiji Aguchi }
1489d34603b0SSeiji Aguchi 
149091eeafeaSThomas Gleixner static __always_inline void
149191eeafeaSThomas Gleixner handle_page_fault(struct pt_regs *regs, unsigned long error_code,
1492ee6352b2SFrederic Weisbecker 			      unsigned long address)
149311a7ffb0SThomas Gleixner {
149491eeafeaSThomas Gleixner 	trace_page_fault_entries(regs, error_code, address);
149591eeafeaSThomas Gleixner 
149691eeafeaSThomas Gleixner 	if (unlikely(kmmio_fault(regs, address)))
149791eeafeaSThomas Gleixner 		return;
149891eeafeaSThomas Gleixner 
149991eeafeaSThomas Gleixner 	/* Was the fault on kernel-controlled part of the address space? */
150091eeafeaSThomas Gleixner 	if (unlikely(fault_in_kernel_space(address))) {
150191eeafeaSThomas Gleixner 		do_kern_addr_fault(regs, error_code, address);
150291eeafeaSThomas Gleixner 	} else {
150391eeafeaSThomas Gleixner 		do_user_addr_fault(regs, error_code, address);
150491eeafeaSThomas Gleixner 		/*
150591eeafeaSThomas Gleixner 		 * User address page fault handling might have reenabled
150691eeafeaSThomas Gleixner 		 * interrupts. Fixing up all potential exit points of
150791eeafeaSThomas Gleixner 		 * do_user_addr_fault() and its leaf functions is just not
150891eeafeaSThomas Gleixner 		 * doable w/o creating an unholy mess or turning the code
150991eeafeaSThomas Gleixner 		 * upside down.
151091eeafeaSThomas Gleixner 		 */
151191eeafeaSThomas Gleixner 		local_irq_disable();
151291eeafeaSThomas Gleixner 	}
151391eeafeaSThomas Gleixner }
151491eeafeaSThomas Gleixner 
151591eeafeaSThomas Gleixner DEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault)
151691eeafeaSThomas Gleixner {
151791eeafeaSThomas Gleixner 	unsigned long address = read_cr2();
1518a27a0a55SThomas Gleixner 	irqentry_state_t state;
151991eeafeaSThomas Gleixner 
1520da1c55f1SMichel Lespinasse 	prefetchw(&current->mm->mmap_lock);
152191eeafeaSThomas Gleixner 
1522ef68017eSAndy Lutomirski 	/*
152366af4f5cSVitaly Kuznetsov 	 * KVM uses #PF vector to deliver 'page not present' events to guests
152466af4f5cSVitaly Kuznetsov 	 * (asynchronous page fault mechanism). The event happens when a
152566af4f5cSVitaly Kuznetsov 	 * userspace task is trying to access some valid (from guest's point of
152666af4f5cSVitaly Kuznetsov 	 * view) memory which is not currently mapped by the host (e.g. the
152766af4f5cSVitaly Kuznetsov 	 * memory is swapped out). Note, the corresponding "page ready" event
152866af4f5cSVitaly Kuznetsov 	 * which is injected when the memory becomes available, is delived via
152966af4f5cSVitaly Kuznetsov 	 * an interrupt mechanism and not a #PF exception
153066af4f5cSVitaly Kuznetsov 	 * (see arch/x86/kernel/kvm.c: sysvec_kvm_asyncpf_interrupt()).
1531ef68017eSAndy Lutomirski 	 *
1532ef68017eSAndy Lutomirski 	 * We are relying on the interrupted context being sane (valid RSP,
1533ef68017eSAndy Lutomirski 	 * relevant locks not held, etc.), which is fine as long as the
1534ef68017eSAndy Lutomirski 	 * interrupted context had IF=1.  We are also relying on the KVM
1535ef68017eSAndy Lutomirski 	 * async pf type field and CR2 being read consistently instead of
1536ef68017eSAndy Lutomirski 	 * getting values from real and async page faults mixed up.
1537ef68017eSAndy Lutomirski 	 *
1538ef68017eSAndy Lutomirski 	 * Fingers crossed.
153991eeafeaSThomas Gleixner 	 *
154091eeafeaSThomas Gleixner 	 * The async #PF handling code takes care of idtentry handling
154191eeafeaSThomas Gleixner 	 * itself.
1542ef68017eSAndy Lutomirski 	 */
1543ef68017eSAndy Lutomirski 	if (kvm_handle_async_pf(regs, (u32)address))
1544ef68017eSAndy Lutomirski 		return;
1545ef68017eSAndy Lutomirski 
1546ca4c6a98SThomas Gleixner 	/*
154791eeafeaSThomas Gleixner 	 * Entry handling for valid #PF from kernel mode is slightly
154891eeafeaSThomas Gleixner 	 * different: RCU is already watching and rcu_irq_enter() must not
154991eeafeaSThomas Gleixner 	 * be invoked because a kernel fault on a user space address might
155091eeafeaSThomas Gleixner 	 * sleep.
155191eeafeaSThomas Gleixner 	 *
155291eeafeaSThomas Gleixner 	 * In case the fault hit a RCU idle region the conditional entry
155391eeafeaSThomas Gleixner 	 * code reenabled RCU to avoid subsequent wreckage which helps
155491eeafeaSThomas Gleixner 	 * debugability.
1555ca4c6a98SThomas Gleixner 	 */
1556a27a0a55SThomas Gleixner 	state = irqentry_enter(regs);
155791eeafeaSThomas Gleixner 
155891eeafeaSThomas Gleixner 	instrumentation_begin();
155991eeafeaSThomas Gleixner 	handle_page_fault(regs, error_code, address);
156091eeafeaSThomas Gleixner 	instrumentation_end();
156191eeafeaSThomas Gleixner 
1562a27a0a55SThomas Gleixner 	irqentry_exit(regs, state);
1563ca4c6a98SThomas Gleixner }
1564