xref: /openbmc/linux/arch/x86/mm/fault.c (revision 334872a0919890a70cccd00b8e11931020a819be)
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		*/
33*334872a0SSean 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.
57c61e211dSHarvey Harrison  *   Check that here and ignore it.
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 		/*
86c61e211dSHarvey Harrison 		 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
87c61e211dSHarvey Harrison 		 * Need to figure out under what instruction mode the
88c61e211dSHarvey Harrison 		 * instruction was issued. Could check the LDT for lm,
89c61e211dSHarvey Harrison 		 * but for now it's good enough to assume that long
90c61e211dSHarvey Harrison 		 * mode only uses well known segments or kernel.
91c61e211dSHarvey Harrison 		 */
92318f5a2aSAndy Lutomirski 		return (!user_mode(regs) || user_64bit_mode(regs));
93c61e211dSHarvey Harrison #endif
94c61e211dSHarvey Harrison 	case 0x60:
95c61e211dSHarvey Harrison 		/* 0x64 thru 0x67 are valid prefixes in all modes. */
96107a0367SIngo Molnar 		return (instr_lo & 0xC) == 0x4;
97c61e211dSHarvey Harrison 	case 0xF0:
98c61e211dSHarvey Harrison 		/* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
99107a0367SIngo Molnar 		return !instr_lo || (instr_lo>>1) == 1;
100c61e211dSHarvey Harrison 	case 0x00:
101c61e211dSHarvey Harrison 		/* Prefetch instruction is 0x0F0D or 0x0F18 */
10225f12ae4SChristoph Hellwig 		if (get_kernel_nofault(opcode, instr))
103107a0367SIngo Molnar 			return 0;
104107a0367SIngo Molnar 
105107a0367SIngo Molnar 		*prefetch = (instr_lo == 0xF) &&
106107a0367SIngo Molnar 			(opcode == 0x0D || opcode == 0x18);
107107a0367SIngo Molnar 		return 0;
108107a0367SIngo Molnar 	default:
109107a0367SIngo Molnar 		return 0;
110107a0367SIngo Molnar 	}
111107a0367SIngo Molnar }
112107a0367SIngo Molnar 
113107a0367SIngo Molnar static int
114107a0367SIngo Molnar is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
115107a0367SIngo Molnar {
116107a0367SIngo Molnar 	unsigned char *max_instr;
117107a0367SIngo Molnar 	unsigned char *instr;
118107a0367SIngo Molnar 	int prefetch = 0;
119107a0367SIngo Molnar 
120107a0367SIngo Molnar 	/*
121107a0367SIngo Molnar 	 * If it was a exec (instruction fetch) fault on NX page, then
122107a0367SIngo Molnar 	 * do not ignore the fault:
123107a0367SIngo Molnar 	 */
1241067f030SRicardo Neri 	if (error_code & X86_PF_INSTR)
125107a0367SIngo Molnar 		return 0;
126107a0367SIngo Molnar 
127107a0367SIngo Molnar 	instr = (void *)convert_ip_to_linear(current, regs);
128107a0367SIngo Molnar 	max_instr = instr + 15;
129107a0367SIngo Molnar 
130d31bf07fSAndy Lutomirski 	if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE_MAX)
131107a0367SIngo Molnar 		return 0;
132107a0367SIngo Molnar 
133107a0367SIngo Molnar 	while (instr < max_instr) {
134107a0367SIngo Molnar 		unsigned char opcode;
135c61e211dSHarvey Harrison 
13625f12ae4SChristoph Hellwig 		if (get_kernel_nofault(opcode, instr))
137c61e211dSHarvey Harrison 			break;
138107a0367SIngo Molnar 
139107a0367SIngo Molnar 		instr++;
140107a0367SIngo Molnar 
141107a0367SIngo Molnar 		if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
142c61e211dSHarvey Harrison 			break;
143c61e211dSHarvey Harrison 	}
144c61e211dSHarvey Harrison 	return prefetch;
145c61e211dSHarvey Harrison }
146c61e211dSHarvey Harrison 
147f2f13a85SIngo Molnar DEFINE_SPINLOCK(pgd_lock);
148f2f13a85SIngo Molnar LIST_HEAD(pgd_list);
1492d4a7167SIngo Molnar 
150f2f13a85SIngo Molnar #ifdef CONFIG_X86_32
151f2f13a85SIngo Molnar static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
152f2f13a85SIngo Molnar {
153f2f13a85SIngo Molnar 	unsigned index = pgd_index(address);
154f2f13a85SIngo Molnar 	pgd_t *pgd_k;
155e0c4f675SKirill A. Shutemov 	p4d_t *p4d, *p4d_k;
156f2f13a85SIngo Molnar 	pud_t *pud, *pud_k;
157f2f13a85SIngo Molnar 	pmd_t *pmd, *pmd_k;
158f2f13a85SIngo Molnar 
159f2f13a85SIngo Molnar 	pgd += index;
160f2f13a85SIngo Molnar 	pgd_k = init_mm.pgd + index;
161f2f13a85SIngo Molnar 
162f2f13a85SIngo Molnar 	if (!pgd_present(*pgd_k))
163f2f13a85SIngo Molnar 		return NULL;
164f2f13a85SIngo Molnar 
165f2f13a85SIngo Molnar 	/*
166f2f13a85SIngo Molnar 	 * set_pgd(pgd, *pgd_k); here would be useless on PAE
167f2f13a85SIngo Molnar 	 * and redundant with the set_pmd() on non-PAE. As would
168e0c4f675SKirill A. Shutemov 	 * set_p4d/set_pud.
169f2f13a85SIngo Molnar 	 */
170e0c4f675SKirill A. Shutemov 	p4d = p4d_offset(pgd, address);
171e0c4f675SKirill A. Shutemov 	p4d_k = p4d_offset(pgd_k, address);
172e0c4f675SKirill A. Shutemov 	if (!p4d_present(*p4d_k))
173e0c4f675SKirill A. Shutemov 		return NULL;
174e0c4f675SKirill A. Shutemov 
175e0c4f675SKirill A. Shutemov 	pud = pud_offset(p4d, address);
176e0c4f675SKirill A. Shutemov 	pud_k = pud_offset(p4d_k, address);
177f2f13a85SIngo Molnar 	if (!pud_present(*pud_k))
178f2f13a85SIngo Molnar 		return NULL;
179f2f13a85SIngo Molnar 
180f2f13a85SIngo Molnar 	pmd = pmd_offset(pud, address);
181f2f13a85SIngo Molnar 	pmd_k = pmd_offset(pud_k, address);
1828e998fc2SJoerg Roedel 
1838e998fc2SJoerg Roedel 	if (pmd_present(*pmd) != pmd_present(*pmd_k))
1848e998fc2SJoerg Roedel 		set_pmd(pmd, *pmd_k);
1858e998fc2SJoerg Roedel 
186f2f13a85SIngo Molnar 	if (!pmd_present(*pmd_k))
187f2f13a85SIngo Molnar 		return NULL;
188b8bcfe99SJeremy Fitzhardinge 	else
18951b75b5bSJoerg Roedel 		BUG_ON(pmd_pfn(*pmd) != pmd_pfn(*pmd_k));
190f2f13a85SIngo Molnar 
191f2f13a85SIngo Molnar 	return pmd_k;
192f2f13a85SIngo Molnar }
193f2f13a85SIngo Molnar 
1944819e15fSJoerg Roedel /*
1954819e15fSJoerg Roedel  *   Handle a fault on the vmalloc or module mapping area
1964819e15fSJoerg Roedel  *
1974819e15fSJoerg Roedel  *   This is needed because there is a race condition between the time
1984819e15fSJoerg Roedel  *   when the vmalloc mapping code updates the PMD to the point in time
1994819e15fSJoerg Roedel  *   where it synchronizes this update with the other page-tables in the
2004819e15fSJoerg Roedel  *   system.
2014819e15fSJoerg Roedel  *
2024819e15fSJoerg Roedel  *   In this race window another thread/CPU can map an area on the same
2034819e15fSJoerg Roedel  *   PMD, finds it already present and does not synchronize it with the
2044819e15fSJoerg Roedel  *   rest of the system yet. As a result v[mz]alloc might return areas
2054819e15fSJoerg Roedel  *   which are not mapped in every page-table in the system, causing an
2064819e15fSJoerg Roedel  *   unhandled page-fault when they are accessed.
2074819e15fSJoerg Roedel  */
2084819e15fSJoerg Roedel static noinline int vmalloc_fault(unsigned long address)
2094819e15fSJoerg Roedel {
2104819e15fSJoerg Roedel 	unsigned long pgd_paddr;
2114819e15fSJoerg Roedel 	pmd_t *pmd_k;
2124819e15fSJoerg Roedel 	pte_t *pte_k;
2134819e15fSJoerg Roedel 
2144819e15fSJoerg Roedel 	/* Make sure we are in vmalloc area: */
2154819e15fSJoerg Roedel 	if (!(address >= VMALLOC_START && address < VMALLOC_END))
2164819e15fSJoerg Roedel 		return -1;
2174819e15fSJoerg Roedel 
2184819e15fSJoerg Roedel 	/*
2194819e15fSJoerg Roedel 	 * Synchronize this task's top level page-table
2204819e15fSJoerg Roedel 	 * with the 'reference' page table.
2214819e15fSJoerg Roedel 	 *
2224819e15fSJoerg Roedel 	 * Do _not_ use "current" here. We might be inside
2234819e15fSJoerg Roedel 	 * an interrupt in the middle of a task switch..
2244819e15fSJoerg Roedel 	 */
2254819e15fSJoerg Roedel 	pgd_paddr = read_cr3_pa();
2264819e15fSJoerg Roedel 	pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
2274819e15fSJoerg Roedel 	if (!pmd_k)
2284819e15fSJoerg Roedel 		return -1;
2294819e15fSJoerg Roedel 
2304819e15fSJoerg Roedel 	if (pmd_large(*pmd_k))
2314819e15fSJoerg Roedel 		return 0;
2324819e15fSJoerg Roedel 
2334819e15fSJoerg Roedel 	pte_k = pte_offset_kernel(pmd_k, address);
2344819e15fSJoerg Roedel 	if (!pte_present(*pte_k))
2354819e15fSJoerg Roedel 		return -1;
2364819e15fSJoerg Roedel 
2374819e15fSJoerg Roedel 	return 0;
2384819e15fSJoerg Roedel }
2394819e15fSJoerg Roedel NOKPROBE_SYMBOL(vmalloc_fault);
2404819e15fSJoerg Roedel 
24186cf69f1SJoerg Roedel void arch_sync_kernel_mappings(unsigned long start, unsigned long end)
242f2f13a85SIngo Molnar {
24386cf69f1SJoerg Roedel 	unsigned long addr;
244f2f13a85SIngo Molnar 
24586cf69f1SJoerg Roedel 	for (addr = start & PMD_MASK;
24686cf69f1SJoerg Roedel 	     addr >= TASK_SIZE_MAX && addr < VMALLOC_END;
24786cf69f1SJoerg Roedel 	     addr += PMD_SIZE) {
248f2f13a85SIngo Molnar 		struct page *page;
249f2f13a85SIngo Molnar 
250a79e53d8SAndrea Arcangeli 		spin_lock(&pgd_lock);
251f2f13a85SIngo Molnar 		list_for_each_entry(page, &pgd_list, lru) {
252617d34d9SJeremy Fitzhardinge 			spinlock_t *pgt_lock;
253617d34d9SJeremy Fitzhardinge 
254a79e53d8SAndrea Arcangeli 			/* the pgt_lock only for Xen */
255617d34d9SJeremy Fitzhardinge 			pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
256617d34d9SJeremy Fitzhardinge 
257617d34d9SJeremy Fitzhardinge 			spin_lock(pgt_lock);
25886cf69f1SJoerg Roedel 			vmalloc_sync_one(page_address(page), addr);
259617d34d9SJeremy Fitzhardinge 			spin_unlock(pgt_lock);
260f2f13a85SIngo Molnar 		}
261a79e53d8SAndrea Arcangeli 		spin_unlock(&pgd_lock);
262f2f13a85SIngo Molnar 	}
263f2f13a85SIngo Molnar }
264f2f13a85SIngo Molnar 
265f2f13a85SIngo Molnar /*
266f2f13a85SIngo Molnar  * Did it hit the DOS screen memory VA from vm86 mode?
267f2f13a85SIngo Molnar  */
268f2f13a85SIngo Molnar static inline void
269f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address,
270f2f13a85SIngo Molnar 		 struct task_struct *tsk)
271f2f13a85SIngo Molnar {
2729fda6a06SBrian Gerst #ifdef CONFIG_VM86
273f2f13a85SIngo Molnar 	unsigned long bit;
274f2f13a85SIngo Molnar 
2759fda6a06SBrian Gerst 	if (!v8086_mode(regs) || !tsk->thread.vm86)
276f2f13a85SIngo Molnar 		return;
277f2f13a85SIngo Molnar 
278f2f13a85SIngo Molnar 	bit = (address - 0xA0000) >> PAGE_SHIFT;
279f2f13a85SIngo Molnar 	if (bit < 32)
2809fda6a06SBrian Gerst 		tsk->thread.vm86->screen_bitmap |= 1 << bit;
2819fda6a06SBrian Gerst #endif
282f2f13a85SIngo Molnar }
283c61e211dSHarvey Harrison 
284087975b0SAkinobu Mita static bool low_pfn(unsigned long pfn)
285087975b0SAkinobu Mita {
286087975b0SAkinobu Mita 	return pfn < max_low_pfn;
287087975b0SAkinobu Mita }
288087975b0SAkinobu Mita 
289cae30f82SAdrian Bunk static void dump_pagetable(unsigned long address)
290c61e211dSHarvey Harrison {
2916c690ee1SAndy Lutomirski 	pgd_t *base = __va(read_cr3_pa());
292087975b0SAkinobu Mita 	pgd_t *pgd = &base[pgd_index(address)];
293e0c4f675SKirill A. Shutemov 	p4d_t *p4d;
294e0c4f675SKirill A. Shutemov 	pud_t *pud;
295087975b0SAkinobu Mita 	pmd_t *pmd;
296087975b0SAkinobu Mita 	pte_t *pte;
2972d4a7167SIngo Molnar 
298c61e211dSHarvey Harrison #ifdef CONFIG_X86_PAE
29939e48d9bSJan Beulich 	pr_info("*pdpt = %016Lx ", pgd_val(*pgd));
300087975b0SAkinobu Mita 	if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
301087975b0SAkinobu Mita 		goto out;
30239e48d9bSJan Beulich #define pr_pde pr_cont
30339e48d9bSJan Beulich #else
30439e48d9bSJan Beulich #define pr_pde pr_info
305c61e211dSHarvey Harrison #endif
306e0c4f675SKirill A. Shutemov 	p4d = p4d_offset(pgd, address);
307e0c4f675SKirill A. Shutemov 	pud = pud_offset(p4d, address);
308e0c4f675SKirill A. Shutemov 	pmd = pmd_offset(pud, address);
30939e48d9bSJan Beulich 	pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
31039e48d9bSJan Beulich #undef pr_pde
311c61e211dSHarvey Harrison 
312c61e211dSHarvey Harrison 	/*
313c61e211dSHarvey Harrison 	 * We must not directly access the pte in the highpte
314c61e211dSHarvey Harrison 	 * case if the page table is located in highmem.
315c61e211dSHarvey Harrison 	 * And let's rather not kmap-atomic the pte, just in case
3162d4a7167SIngo Molnar 	 * it's allocated already:
317c61e211dSHarvey Harrison 	 */
318087975b0SAkinobu Mita 	if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
319087975b0SAkinobu Mita 		goto out;
3202d4a7167SIngo Molnar 
321087975b0SAkinobu Mita 	pte = pte_offset_kernel(pmd, address);
32239e48d9bSJan Beulich 	pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
323087975b0SAkinobu Mita out:
32439e48d9bSJan Beulich 	pr_cont("\n");
325f2f13a85SIngo Molnar }
326f2f13a85SIngo Molnar 
327f2f13a85SIngo Molnar #else /* CONFIG_X86_64: */
328f2f13a85SIngo Molnar 
329e05139f2SJan Beulich #ifdef CONFIG_CPU_SUP_AMD
330f2f13a85SIngo Molnar static const char errata93_warning[] =
331ad361c98SJoe Perches KERN_ERR
332ad361c98SJoe Perches "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
333ad361c98SJoe Perches "******* Working around it, but it may cause SEGVs or burn power.\n"
334ad361c98SJoe Perches "******* Please consider a BIOS update.\n"
335ad361c98SJoe Perches "******* Disabling USB legacy in the BIOS may also help.\n";
336e05139f2SJan Beulich #endif
337f2f13a85SIngo Molnar 
338f2f13a85SIngo Molnar /*
339f2f13a85SIngo Molnar  * No vm86 mode in 64-bit mode:
340f2f13a85SIngo Molnar  */
341f2f13a85SIngo Molnar static inline void
342f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address,
343f2f13a85SIngo Molnar 		 struct task_struct *tsk)
344f2f13a85SIngo Molnar {
345f2f13a85SIngo Molnar }
346f2f13a85SIngo Molnar 
347f2f13a85SIngo Molnar static int bad_address(void *p)
348f2f13a85SIngo Molnar {
349f2f13a85SIngo Molnar 	unsigned long dummy;
350f2f13a85SIngo Molnar 
35125f12ae4SChristoph Hellwig 	return get_kernel_nofault(dummy, (unsigned long *)p);
352f2f13a85SIngo Molnar }
353f2f13a85SIngo Molnar 
354f2f13a85SIngo Molnar static void dump_pagetable(unsigned long address)
355f2f13a85SIngo Molnar {
3566c690ee1SAndy Lutomirski 	pgd_t *base = __va(read_cr3_pa());
357087975b0SAkinobu Mita 	pgd_t *pgd = base + pgd_index(address);
358e0c4f675SKirill A. Shutemov 	p4d_t *p4d;
359c61e211dSHarvey Harrison 	pud_t *pud;
360c61e211dSHarvey Harrison 	pmd_t *pmd;
361c61e211dSHarvey Harrison 	pte_t *pte;
362c61e211dSHarvey Harrison 
3632d4a7167SIngo Molnar 	if (bad_address(pgd))
3642d4a7167SIngo Molnar 		goto bad;
3652d4a7167SIngo Molnar 
36639e48d9bSJan Beulich 	pr_info("PGD %lx ", pgd_val(*pgd));
3672d4a7167SIngo Molnar 
3682d4a7167SIngo Molnar 	if (!pgd_present(*pgd))
3692d4a7167SIngo Molnar 		goto out;
370c61e211dSHarvey Harrison 
371e0c4f675SKirill A. Shutemov 	p4d = p4d_offset(pgd, address);
372e0c4f675SKirill A. Shutemov 	if (bad_address(p4d))
373e0c4f675SKirill A. Shutemov 		goto bad;
374e0c4f675SKirill A. Shutemov 
37539e48d9bSJan Beulich 	pr_cont("P4D %lx ", p4d_val(*p4d));
376e0c4f675SKirill A. Shutemov 	if (!p4d_present(*p4d) || p4d_large(*p4d))
377e0c4f675SKirill A. Shutemov 		goto out;
378e0c4f675SKirill A. Shutemov 
379e0c4f675SKirill A. Shutemov 	pud = pud_offset(p4d, address);
3802d4a7167SIngo Molnar 	if (bad_address(pud))
3812d4a7167SIngo Molnar 		goto bad;
3822d4a7167SIngo Molnar 
38339e48d9bSJan Beulich 	pr_cont("PUD %lx ", pud_val(*pud));
384b5360222SAndi Kleen 	if (!pud_present(*pud) || pud_large(*pud))
3852d4a7167SIngo Molnar 		goto out;
386c61e211dSHarvey Harrison 
387c61e211dSHarvey Harrison 	pmd = pmd_offset(pud, address);
3882d4a7167SIngo Molnar 	if (bad_address(pmd))
3892d4a7167SIngo Molnar 		goto bad;
3902d4a7167SIngo Molnar 
39139e48d9bSJan Beulich 	pr_cont("PMD %lx ", pmd_val(*pmd));
3922d4a7167SIngo Molnar 	if (!pmd_present(*pmd) || pmd_large(*pmd))
3932d4a7167SIngo Molnar 		goto out;
394c61e211dSHarvey Harrison 
395c61e211dSHarvey Harrison 	pte = pte_offset_kernel(pmd, address);
3962d4a7167SIngo Molnar 	if (bad_address(pte))
3972d4a7167SIngo Molnar 		goto bad;
3982d4a7167SIngo Molnar 
39939e48d9bSJan Beulich 	pr_cont("PTE %lx", pte_val(*pte));
4002d4a7167SIngo Molnar out:
40139e48d9bSJan Beulich 	pr_cont("\n");
402c61e211dSHarvey Harrison 	return;
403c61e211dSHarvey Harrison bad:
40439e48d9bSJan Beulich 	pr_info("BAD\n");
405c61e211dSHarvey Harrison }
406c61e211dSHarvey Harrison 
407f2f13a85SIngo Molnar #endif /* CONFIG_X86_64 */
408c61e211dSHarvey Harrison 
4092d4a7167SIngo Molnar /*
4102d4a7167SIngo Molnar  * Workaround for K8 erratum #93 & buggy BIOS.
4112d4a7167SIngo Molnar  *
4122d4a7167SIngo Molnar  * BIOS SMM functions are required to use a specific workaround
4132d4a7167SIngo Molnar  * to avoid corruption of the 64bit RIP register on C stepping K8.
4142d4a7167SIngo Molnar  *
4152d4a7167SIngo Molnar  * A lot of BIOS that didn't get tested properly miss this.
4162d4a7167SIngo Molnar  *
4172d4a7167SIngo Molnar  * The OS sees this as a page fault with the upper 32bits of RIP cleared.
4182d4a7167SIngo Molnar  * Try to work around it here.
4192d4a7167SIngo Molnar  *
4202d4a7167SIngo Molnar  * Note we only handle faults in kernel here.
4212d4a7167SIngo Molnar  * Does nothing on 32-bit.
422c61e211dSHarvey Harrison  */
423c61e211dSHarvey Harrison static int is_errata93(struct pt_regs *regs, unsigned long address)
424c61e211dSHarvey Harrison {
425e05139f2SJan Beulich #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
426e05139f2SJan Beulich 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
427e05139f2SJan Beulich 	    || boot_cpu_data.x86 != 0xf)
428e05139f2SJan Beulich 		return 0;
429e05139f2SJan Beulich 
430c61e211dSHarvey Harrison 	if (address != regs->ip)
431c61e211dSHarvey Harrison 		return 0;
4322d4a7167SIngo Molnar 
433c61e211dSHarvey Harrison 	if ((address >> 32) != 0)
434c61e211dSHarvey Harrison 		return 0;
4352d4a7167SIngo Molnar 
436c61e211dSHarvey Harrison 	address |= 0xffffffffUL << 32;
437c61e211dSHarvey Harrison 	if ((address >= (u64)_stext && address <= (u64)_etext) ||
438c61e211dSHarvey Harrison 	    (address >= MODULES_VADDR && address <= MODULES_END)) {
439a454ab31SIngo Molnar 		printk_once(errata93_warning);
440c61e211dSHarvey Harrison 		regs->ip = address;
441c61e211dSHarvey Harrison 		return 1;
442c61e211dSHarvey Harrison 	}
443c61e211dSHarvey Harrison #endif
444c61e211dSHarvey Harrison 	return 0;
445c61e211dSHarvey Harrison }
446c61e211dSHarvey Harrison 
447c61e211dSHarvey Harrison /*
4482d4a7167SIngo Molnar  * Work around K8 erratum #100 K8 in compat mode occasionally jumps
4492d4a7167SIngo Molnar  * to illegal addresses >4GB.
4502d4a7167SIngo Molnar  *
4512d4a7167SIngo Molnar  * We catch this in the page fault handler because these addresses
4522d4a7167SIngo Molnar  * are not reachable. Just detect this case and return.  Any code
453c61e211dSHarvey Harrison  * segment in LDT is compatibility mode.
454c61e211dSHarvey Harrison  */
455c61e211dSHarvey Harrison static int is_errata100(struct pt_regs *regs, unsigned long address)
456c61e211dSHarvey Harrison {
457c61e211dSHarvey Harrison #ifdef CONFIG_X86_64
4582d4a7167SIngo Molnar 	if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
459c61e211dSHarvey Harrison 		return 1;
460c61e211dSHarvey Harrison #endif
461c61e211dSHarvey Harrison 	return 0;
462c61e211dSHarvey Harrison }
463c61e211dSHarvey Harrison 
4643e77abdaSThomas Gleixner /* Pentium F0 0F C7 C8 bug workaround: */
465c61e211dSHarvey Harrison static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
466c61e211dSHarvey Harrison {
467c61e211dSHarvey Harrison #ifdef CONFIG_X86_F00F_BUG
4683e77abdaSThomas Gleixner 	if (boot_cpu_has_bug(X86_BUG_F00F) && idt_is_f00f_address(address)) {
46949893c5cSThomas Gleixner 		handle_invalid_op(regs);
470c61e211dSHarvey Harrison 		return 1;
471c61e211dSHarvey Harrison 	}
472c61e211dSHarvey Harrison #endif
473c61e211dSHarvey Harrison 	return 0;
474c61e211dSHarvey Harrison }
475c61e211dSHarvey Harrison 
476a1a371c4SAndy Lutomirski static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index)
477a1a371c4SAndy Lutomirski {
478a1a371c4SAndy Lutomirski 	u32 offset = (index >> 3) * sizeof(struct desc_struct);
479a1a371c4SAndy Lutomirski 	unsigned long addr;
480a1a371c4SAndy Lutomirski 	struct ldttss_desc desc;
481a1a371c4SAndy Lutomirski 
482a1a371c4SAndy Lutomirski 	if (index == 0) {
483a1a371c4SAndy Lutomirski 		pr_alert("%s: NULL\n", name);
484a1a371c4SAndy Lutomirski 		return;
485a1a371c4SAndy Lutomirski 	}
486a1a371c4SAndy Lutomirski 
487a1a371c4SAndy Lutomirski 	if (offset + sizeof(struct ldttss_desc) >= gdt->size) {
488a1a371c4SAndy Lutomirski 		pr_alert("%s: 0x%hx -- out of bounds\n", name, index);
489a1a371c4SAndy Lutomirski 		return;
490a1a371c4SAndy Lutomirski 	}
491a1a371c4SAndy Lutomirski 
492fe557319SChristoph Hellwig 	if (copy_from_kernel_nofault(&desc, (void *)(gdt->address + offset),
493a1a371c4SAndy Lutomirski 			      sizeof(struct ldttss_desc))) {
494a1a371c4SAndy Lutomirski 		pr_alert("%s: 0x%hx -- GDT entry is not readable\n",
495a1a371c4SAndy Lutomirski 			 name, index);
496a1a371c4SAndy Lutomirski 		return;
497a1a371c4SAndy Lutomirski 	}
498a1a371c4SAndy Lutomirski 
4995ccd3528SColin Ian King 	addr = desc.base0 | (desc.base1 << 16) | ((unsigned long)desc.base2 << 24);
500a1a371c4SAndy Lutomirski #ifdef CONFIG_X86_64
501a1a371c4SAndy Lutomirski 	addr |= ((u64)desc.base3 << 32);
502a1a371c4SAndy Lutomirski #endif
503a1a371c4SAndy Lutomirski 	pr_alert("%s: 0x%hx -- base=0x%lx limit=0x%x\n",
504a1a371c4SAndy Lutomirski 		 name, index, addr, (desc.limit0 | (desc.limit1 << 16)));
505a1a371c4SAndy Lutomirski }
506a1a371c4SAndy Lutomirski 
5072d4a7167SIngo Molnar static void
508a2aa52abSIngo Molnar show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long address)
509c61e211dSHarvey Harrison {
510c61e211dSHarvey Harrison 	if (!oops_may_print())
511c61e211dSHarvey Harrison 		return;
512c61e211dSHarvey Harrison 
5131067f030SRicardo Neri 	if (error_code & X86_PF_INSTR) {
51493809be8SHarvey Harrison 		unsigned int level;
515426e34ccSMatt Fleming 		pgd_t *pgd;
516426e34ccSMatt Fleming 		pte_t *pte;
5172d4a7167SIngo Molnar 
5186c690ee1SAndy Lutomirski 		pgd = __va(read_cr3_pa());
519426e34ccSMatt Fleming 		pgd += pgd_index(address);
520426e34ccSMatt Fleming 
521426e34ccSMatt Fleming 		pte = lookup_address_in_pgd(pgd, address, &level);
522c61e211dSHarvey Harrison 
5238f766149SIngo Molnar 		if (pte && pte_present(*pte) && !pte_exec(*pte))
524d79d0d8aSDmitry Vyukov 			pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n",
525d79d0d8aSDmitry Vyukov 				from_kuid(&init_user_ns, current_uid()));
526eff50c34SJiri Kosina 		if (pte && pte_present(*pte) && pte_exec(*pte) &&
527eff50c34SJiri Kosina 				(pgd_flags(*pgd) & _PAGE_USER) &&
5281e02ce4cSAndy Lutomirski 				(__read_cr4() & X86_CR4_SMEP))
529d79d0d8aSDmitry Vyukov 			pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n",
530d79d0d8aSDmitry Vyukov 				from_kuid(&init_user_ns, current_uid()));
531c61e211dSHarvey Harrison 	}
532fd40d6e3SHarvey Harrison 
533f28b11a2SSean Christopherson 	if (address < PAGE_SIZE && !user_mode(regs))
534ea2f8d60SBorislav Petkov 		pr_alert("BUG: kernel NULL pointer dereference, address: %px\n",
535f28b11a2SSean Christopherson 			(void *)address);
536f28b11a2SSean Christopherson 	else
537ea2f8d60SBorislav Petkov 		pr_alert("BUG: unable to handle page fault for address: %px\n",
5384188f063SDmitry Vyukov 			(void *)address);
5392d4a7167SIngo Molnar 
540ea2f8d60SBorislav Petkov 	pr_alert("#PF: %s %s in %s mode\n",
54118ea35c5SSean Christopherson 		 (error_code & X86_PF_USER)  ? "user" : "supervisor",
54218ea35c5SSean Christopherson 		 (error_code & X86_PF_INSTR) ? "instruction fetch" :
54318ea35c5SSean Christopherson 		 (error_code & X86_PF_WRITE) ? "write access" :
54418ea35c5SSean Christopherson 					       "read access",
54518ea35c5SSean Christopherson 			     user_mode(regs) ? "user" : "kernel");
54618ea35c5SSean Christopherson 	pr_alert("#PF: error_code(0x%04lx) - %s\n", error_code,
54718ea35c5SSean Christopherson 		 !(error_code & X86_PF_PROT) ? "not-present page" :
54818ea35c5SSean Christopherson 		 (error_code & X86_PF_RSVD)  ? "reserved bit violation" :
54918ea35c5SSean Christopherson 		 (error_code & X86_PF_PK)    ? "protection keys violation" :
55018ea35c5SSean Christopherson 					       "permissions violation");
551a2aa52abSIngo Molnar 
552a1a371c4SAndy Lutomirski 	if (!(error_code & X86_PF_USER) && user_mode(regs)) {
553a1a371c4SAndy Lutomirski 		struct desc_ptr idt, gdt;
554a1a371c4SAndy Lutomirski 		u16 ldtr, tr;
555a1a371c4SAndy Lutomirski 
556a1a371c4SAndy Lutomirski 		/*
557a1a371c4SAndy Lutomirski 		 * This can happen for quite a few reasons.  The more obvious
558a1a371c4SAndy Lutomirski 		 * ones are faults accessing the GDT, or LDT.  Perhaps
559a1a371c4SAndy Lutomirski 		 * surprisingly, if the CPU tries to deliver a benign or
560a1a371c4SAndy Lutomirski 		 * contributory exception from user code and gets a page fault
561a1a371c4SAndy Lutomirski 		 * during delivery, the page fault can be delivered as though
562a1a371c4SAndy Lutomirski 		 * it originated directly from user code.  This could happen
563a1a371c4SAndy Lutomirski 		 * due to wrong permissions on the IDT, GDT, LDT, TSS, or
564a1a371c4SAndy Lutomirski 		 * kernel or IST stack.
565a1a371c4SAndy Lutomirski 		 */
566a1a371c4SAndy Lutomirski 		store_idt(&idt);
567a1a371c4SAndy Lutomirski 
568a1a371c4SAndy Lutomirski 		/* Usable even on Xen PV -- it's just slow. */
569a1a371c4SAndy Lutomirski 		native_store_gdt(&gdt);
570a1a371c4SAndy Lutomirski 
571a1a371c4SAndy Lutomirski 		pr_alert("IDT: 0x%lx (limit=0x%hx) GDT: 0x%lx (limit=0x%hx)\n",
572a1a371c4SAndy Lutomirski 			 idt.address, idt.size, gdt.address, gdt.size);
573a1a371c4SAndy Lutomirski 
574a1a371c4SAndy Lutomirski 		store_ldt(ldtr);
575a1a371c4SAndy Lutomirski 		show_ldttss(&gdt, "LDTR", ldtr);
576a1a371c4SAndy Lutomirski 
577a1a371c4SAndy Lutomirski 		store_tr(tr);
578a1a371c4SAndy Lutomirski 		show_ldttss(&gdt, "TR", tr);
579a1a371c4SAndy Lutomirski 	}
580a1a371c4SAndy Lutomirski 
581c61e211dSHarvey Harrison 	dump_pagetable(address);
582c61e211dSHarvey Harrison }
583c61e211dSHarvey Harrison 
5842d4a7167SIngo Molnar static noinline void
5852d4a7167SIngo Molnar pgtable_bad(struct pt_regs *regs, unsigned long error_code,
5862d4a7167SIngo Molnar 	    unsigned long address)
587c61e211dSHarvey Harrison {
5882d4a7167SIngo Molnar 	struct task_struct *tsk;
5892d4a7167SIngo Molnar 	unsigned long flags;
5902d4a7167SIngo Molnar 	int sig;
5912d4a7167SIngo Molnar 
5922d4a7167SIngo Molnar 	flags = oops_begin();
5932d4a7167SIngo Molnar 	tsk = current;
5942d4a7167SIngo Molnar 	sig = SIGKILL;
595c61e211dSHarvey Harrison 
596c61e211dSHarvey Harrison 	printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
59792181f19SNick Piggin 	       tsk->comm, address);
598c61e211dSHarvey Harrison 	dump_pagetable(address);
5992d4a7167SIngo Molnar 
600c61e211dSHarvey Harrison 	if (__die("Bad pagetable", regs, error_code))
601874d93d1SAlexander van Heukelum 		sig = 0;
6022d4a7167SIngo Molnar 
603874d93d1SAlexander van Heukelum 	oops_end(flags, regs, sig);
604c61e211dSHarvey Harrison }
605c61e211dSHarvey Harrison 
606cd072dabSSean Christopherson static void sanitize_error_code(unsigned long address,
607cd072dabSSean Christopherson 				unsigned long *error_code)
608e49d3cbeSAndy Lutomirski {
609e49d3cbeSAndy Lutomirski 	/*
610e49d3cbeSAndy Lutomirski 	 * To avoid leaking information about the kernel page
611e49d3cbeSAndy Lutomirski 	 * table layout, pretend that user-mode accesses to
612e49d3cbeSAndy Lutomirski 	 * kernel addresses are always protection faults.
613e0a446ceSAndy Lutomirski 	 *
614e0a446ceSAndy Lutomirski 	 * NB: This means that failed vsyscalls with vsyscall=none
615e0a446ceSAndy Lutomirski 	 * will have the PROT bit.  This doesn't leak any
616e0a446ceSAndy Lutomirski 	 * information and does not appear to cause any problems.
617e49d3cbeSAndy Lutomirski 	 */
618e49d3cbeSAndy Lutomirski 	if (address >= TASK_SIZE_MAX)
619cd072dabSSean Christopherson 		*error_code |= X86_PF_PROT;
620cd072dabSSean Christopherson }
621cd072dabSSean Christopherson 
622cd072dabSSean Christopherson static void set_signal_archinfo(unsigned long address,
623cd072dabSSean Christopherson 				unsigned long error_code)
624cd072dabSSean Christopherson {
625cd072dabSSean Christopherson 	struct task_struct *tsk = current;
626e49d3cbeSAndy Lutomirski 
627e49d3cbeSAndy Lutomirski 	tsk->thread.trap_nr = X86_TRAP_PF;
628e49d3cbeSAndy Lutomirski 	tsk->thread.error_code = error_code | X86_PF_USER;
629e49d3cbeSAndy Lutomirski 	tsk->thread.cr2 = address;
630e49d3cbeSAndy Lutomirski }
631e49d3cbeSAndy Lutomirski 
6322d4a7167SIngo Molnar static noinline void
6332d4a7167SIngo Molnar no_context(struct pt_regs *regs, unsigned long error_code,
6344fc34901SAndy Lutomirski 	   unsigned long address, int signal, int si_code)
63592181f19SNick Piggin {
63692181f19SNick Piggin 	struct task_struct *tsk = current;
63792181f19SNick Piggin 	unsigned long flags;
63892181f19SNick Piggin 	int sig;
63992181f19SNick Piggin 
640ebb53e25SAndy Lutomirski 	if (user_mode(regs)) {
641ebb53e25SAndy Lutomirski 		/*
642ebb53e25SAndy Lutomirski 		 * This is an implicit supervisor-mode access from user
643ebb53e25SAndy Lutomirski 		 * mode.  Bypass all the kernel-mode recovery code and just
644ebb53e25SAndy Lutomirski 		 * OOPS.
645ebb53e25SAndy Lutomirski 		 */
646ebb53e25SAndy Lutomirski 		goto oops;
647ebb53e25SAndy Lutomirski 	}
648ebb53e25SAndy Lutomirski 
64992181f19SNick Piggin 	/* Are we prepared to handle this kernel fault? */
65081fd9c18SJann Horn 	if (fixup_exception(regs, X86_TRAP_PF, error_code, address)) {
651c026b359SPeter Zijlstra 		/*
652c026b359SPeter Zijlstra 		 * Any interrupt that takes a fault gets the fixup. This makes
653c026b359SPeter Zijlstra 		 * the below recursive fault logic only apply to a faults from
654c026b359SPeter Zijlstra 		 * task context.
655c026b359SPeter Zijlstra 		 */
656c026b359SPeter Zijlstra 		if (in_interrupt())
657c026b359SPeter Zijlstra 			return;
658c026b359SPeter Zijlstra 
659c026b359SPeter Zijlstra 		/*
660c026b359SPeter Zijlstra 		 * Per the above we're !in_interrupt(), aka. task context.
661c026b359SPeter Zijlstra 		 *
662c026b359SPeter Zijlstra 		 * In this case we need to make sure we're not recursively
663c026b359SPeter Zijlstra 		 * faulting through the emulate_vsyscall() logic.
664c026b359SPeter Zijlstra 		 */
6652a53ccbcSIngo Molnar 		if (current->thread.sig_on_uaccess_err && signal) {
666cd072dabSSean Christopherson 			sanitize_error_code(address, &error_code);
667cd072dabSSean Christopherson 
668e49d3cbeSAndy Lutomirski 			set_signal_archinfo(address, error_code);
6694fc34901SAndy Lutomirski 
6704fc34901SAndy Lutomirski 			/* XXX: hwpoison faults will set the wrong code. */
6712e1661d2SEric W. Biederman 			force_sig_fault(signal, si_code, (void __user *)address);
6724fc34901SAndy Lutomirski 		}
673c026b359SPeter Zijlstra 
674c026b359SPeter Zijlstra 		/*
675c026b359SPeter Zijlstra 		 * Barring that, we can do the fixup and be happy.
676c026b359SPeter Zijlstra 		 */
67792181f19SNick Piggin 		return;
6784fc34901SAndy Lutomirski 	}
67992181f19SNick Piggin 
6806271cfdfSAndy Lutomirski #ifdef CONFIG_VMAP_STACK
6816271cfdfSAndy Lutomirski 	/*
6826271cfdfSAndy Lutomirski 	 * Stack overflow?  During boot, we can fault near the initial
6836271cfdfSAndy Lutomirski 	 * stack in the direct map, but that's not an overflow -- check
6846271cfdfSAndy Lutomirski 	 * that we're in vmalloc space to avoid this.
6856271cfdfSAndy Lutomirski 	 */
6866271cfdfSAndy Lutomirski 	if (is_vmalloc_addr((void *)address) &&
6876271cfdfSAndy Lutomirski 	    (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) ||
6886271cfdfSAndy Lutomirski 	     address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) {
689d876b673SThomas Gleixner 		unsigned long stack = __this_cpu_ist_top_va(DF) - sizeof(void *);
6906271cfdfSAndy Lutomirski 		/*
6916271cfdfSAndy Lutomirski 		 * We're likely to be running with very little stack space
6926271cfdfSAndy Lutomirski 		 * left.  It's plausible that we'd hit this condition but
6936271cfdfSAndy Lutomirski 		 * double-fault even before we get this far, in which case
6946271cfdfSAndy Lutomirski 		 * we're fine: the double-fault handler will deal with it.
6956271cfdfSAndy Lutomirski 		 *
6966271cfdfSAndy Lutomirski 		 * We don't want to make it all the way into the oops code
6976271cfdfSAndy Lutomirski 		 * and then double-fault, though, because we're likely to
6986271cfdfSAndy Lutomirski 		 * break the console driver and lose most of the stack dump.
6996271cfdfSAndy Lutomirski 		 */
7006271cfdfSAndy Lutomirski 		asm volatile ("movq %[stack], %%rsp\n\t"
7016271cfdfSAndy Lutomirski 			      "call handle_stack_overflow\n\t"
7026271cfdfSAndy Lutomirski 			      "1: jmp 1b"
703f5caf621SJosh Poimboeuf 			      : ASM_CALL_CONSTRAINT
7046271cfdfSAndy Lutomirski 			      : "D" ("kernel stack overflow (page fault)"),
7056271cfdfSAndy Lutomirski 				"S" (regs), "d" (address),
7066271cfdfSAndy Lutomirski 				[stack] "rm" (stack));
7076271cfdfSAndy Lutomirski 		unreachable();
7086271cfdfSAndy Lutomirski 	}
7096271cfdfSAndy Lutomirski #endif
7106271cfdfSAndy Lutomirski 
71192181f19SNick Piggin 	/*
7122d4a7167SIngo Molnar 	 * 32-bit:
7132d4a7167SIngo Molnar 	 *
71492181f19SNick Piggin 	 *   Valid to do another page fault here, because if this fault
71592181f19SNick Piggin 	 *   had been triggered by is_prefetch fixup_exception would have
71692181f19SNick Piggin 	 *   handled it.
71792181f19SNick Piggin 	 *
7182d4a7167SIngo Molnar 	 * 64-bit:
7192d4a7167SIngo Molnar 	 *
72092181f19SNick Piggin 	 *   Hall of shame of CPU/BIOS bugs.
72192181f19SNick Piggin 	 */
72292181f19SNick Piggin 	if (is_prefetch(regs, error_code, address))
72392181f19SNick Piggin 		return;
72492181f19SNick Piggin 
72592181f19SNick Piggin 	if (is_errata93(regs, address))
72692181f19SNick Piggin 		return;
72792181f19SNick Piggin 
72892181f19SNick Piggin 	/*
7293425d934SSai Praneeth 	 * Buggy firmware could access regions which might page fault, try to
7303425d934SSai Praneeth 	 * recover from such faults.
7313425d934SSai Praneeth 	 */
7323425d934SSai Praneeth 	if (IS_ENABLED(CONFIG_EFI))
7333425d934SSai Praneeth 		efi_recover_from_page_fault(address);
7343425d934SSai Praneeth 
735ebb53e25SAndy Lutomirski oops:
7363425d934SSai Praneeth 	/*
73792181f19SNick Piggin 	 * Oops. The kernel tried to access some bad page. We'll have to
7382d4a7167SIngo Molnar 	 * terminate things with extreme prejudice:
73992181f19SNick Piggin 	 */
74092181f19SNick Piggin 	flags = oops_begin();
74192181f19SNick Piggin 
74292181f19SNick Piggin 	show_fault_oops(regs, error_code, address);
74392181f19SNick Piggin 
744a70857e4SAaron Tomlin 	if (task_stack_end_corrupted(tsk))
745b0f4c4b3SPrarit Bhargava 		printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
74619803078SIngo Molnar 
74792181f19SNick Piggin 	sig = SIGKILL;
74892181f19SNick Piggin 	if (__die("Oops", regs, error_code))
74992181f19SNick Piggin 		sig = 0;
7502d4a7167SIngo Molnar 
75192181f19SNick Piggin 	/* Executive summary in case the body of the oops scrolled away */
752b0f4c4b3SPrarit Bhargava 	printk(KERN_DEFAULT "CR2: %016lx\n", address);
7532d4a7167SIngo Molnar 
75492181f19SNick Piggin 	oops_end(flags, regs, sig);
75592181f19SNick Piggin }
75692181f19SNick Piggin 
7572d4a7167SIngo Molnar /*
7582d4a7167SIngo Molnar  * Print out info about fatal segfaults, if the show_unhandled_signals
7592d4a7167SIngo Molnar  * sysctl is set:
7602d4a7167SIngo Molnar  */
7612d4a7167SIngo Molnar static inline void
7622d4a7167SIngo Molnar show_signal_msg(struct pt_regs *regs, unsigned long error_code,
7632d4a7167SIngo Molnar 		unsigned long address, struct task_struct *tsk)
7642d4a7167SIngo Molnar {
765ba54d856SBorislav Petkov 	const char *loglvl = task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG;
766ba54d856SBorislav Petkov 
7672d4a7167SIngo Molnar 	if (!unhandled_signal(tsk, SIGSEGV))
7682d4a7167SIngo Molnar 		return;
7692d4a7167SIngo Molnar 
7702d4a7167SIngo Molnar 	if (!printk_ratelimit())
7712d4a7167SIngo Molnar 		return;
7722d4a7167SIngo Molnar 
77310a7e9d8SKees Cook 	printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx",
774ba54d856SBorislav Petkov 		loglvl, tsk->comm, task_pid_nr(tsk), address,
7752d4a7167SIngo Molnar 		(void *)regs->ip, (void *)regs->sp, error_code);
7762d4a7167SIngo Molnar 
7772d4a7167SIngo Molnar 	print_vma_addr(KERN_CONT " in ", regs->ip);
7782d4a7167SIngo Molnar 
7792d4a7167SIngo Molnar 	printk(KERN_CONT "\n");
780ba54d856SBorislav Petkov 
781342db04aSJann Horn 	show_opcodes(regs, loglvl);
7822d4a7167SIngo Molnar }
7832d4a7167SIngo Molnar 
78402e983b7SDave Hansen /*
78502e983b7SDave Hansen  * The (legacy) vsyscall page is the long page in the kernel portion
78602e983b7SDave Hansen  * of the address space that has user-accessible permissions.
78702e983b7SDave Hansen  */
78802e983b7SDave Hansen static bool is_vsyscall_vaddr(unsigned long vaddr)
78902e983b7SDave Hansen {
7903ae0ad92SDave Hansen 	return unlikely((vaddr & PAGE_MASK) == VSYSCALL_ADDR);
79102e983b7SDave Hansen }
79202e983b7SDave Hansen 
7932d4a7167SIngo Molnar static void
7942d4a7167SIngo Molnar __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
795419ceeb1SEric W. Biederman 		       unsigned long address, u32 pkey, int si_code)
79692181f19SNick Piggin {
79792181f19SNick Piggin 	struct task_struct *tsk = current;
79892181f19SNick Piggin 
79992181f19SNick Piggin 	/* User mode accesses just cause a SIGSEGV */
8006ea59b07SAndy Lutomirski 	if (user_mode(regs) && (error_code & X86_PF_USER)) {
80192181f19SNick Piggin 		/*
8022d4a7167SIngo Molnar 		 * It's possible to have interrupts off here:
80392181f19SNick Piggin 		 */
80492181f19SNick Piggin 		local_irq_enable();
80592181f19SNick Piggin 
80692181f19SNick Piggin 		/*
80792181f19SNick Piggin 		 * Valid to do another page fault here because this one came
8082d4a7167SIngo Molnar 		 * from user space:
80992181f19SNick Piggin 		 */
81092181f19SNick Piggin 		if (is_prefetch(regs, error_code, address))
81192181f19SNick Piggin 			return;
81292181f19SNick Piggin 
81392181f19SNick Piggin 		if (is_errata100(regs, address))
81492181f19SNick Piggin 			return;
81592181f19SNick Piggin 
816cd072dabSSean Christopherson 		sanitize_error_code(address, &error_code);
8173ae36655SAndy Lutomirski 
818*334872a0SSean Christopherson 		if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address))
819*334872a0SSean Christopherson 			return;
820*334872a0SSean Christopherson 
821e575a86fSKees Cook 		if (likely(show_unhandled_signals))
8222d4a7167SIngo Molnar 			show_signal_msg(regs, error_code, address, tsk);
82392181f19SNick Piggin 
824e49d3cbeSAndy Lutomirski 		set_signal_archinfo(address, error_code);
8252d4a7167SIngo Molnar 
8269db812dbSEric W. Biederman 		if (si_code == SEGV_PKUERR)
827419ceeb1SEric W. Biederman 			force_sig_pkuerr((void __user *)address, pkey);
8289db812dbSEric W. Biederman 
8292e1661d2SEric W. Biederman 		force_sig_fault(SIGSEGV, si_code, (void __user *)address);
8302d4a7167SIngo Molnar 
831ca4c6a98SThomas Gleixner 		local_irq_disable();
832ca4c6a98SThomas Gleixner 
83392181f19SNick Piggin 		return;
83492181f19SNick Piggin 	}
83592181f19SNick Piggin 
83692181f19SNick Piggin 	if (is_f00f_bug(regs, address))
83792181f19SNick Piggin 		return;
83892181f19SNick Piggin 
8394fc34901SAndy Lutomirski 	no_context(regs, error_code, address, SIGSEGV, si_code);
84092181f19SNick Piggin }
84192181f19SNick Piggin 
8422d4a7167SIngo Molnar static noinline void
8432d4a7167SIngo Molnar bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
844768fd9c6SEric W. Biederman 		     unsigned long address)
84592181f19SNick Piggin {
846419ceeb1SEric W. Biederman 	__bad_area_nosemaphore(regs, error_code, address, 0, SEGV_MAPERR);
84792181f19SNick Piggin }
84892181f19SNick Piggin 
8492d4a7167SIngo Molnar static void
8502d4a7167SIngo Molnar __bad_area(struct pt_regs *regs, unsigned long error_code,
851419ceeb1SEric W. Biederman 	   unsigned long address, u32 pkey, int si_code)
85292181f19SNick Piggin {
85392181f19SNick Piggin 	struct mm_struct *mm = current->mm;
85492181f19SNick Piggin 	/*
85592181f19SNick Piggin 	 * Something tried to access memory that isn't in our memory map..
85692181f19SNick Piggin 	 * Fix it, but check if it's kernel or user first..
85792181f19SNick Piggin 	 */
858d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
85992181f19SNick Piggin 
860aba1ecd3SEric W. Biederman 	__bad_area_nosemaphore(regs, error_code, address, pkey, si_code);
86192181f19SNick Piggin }
86292181f19SNick Piggin 
8632d4a7167SIngo Molnar static noinline void
8642d4a7167SIngo Molnar bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
86592181f19SNick Piggin {
866419ceeb1SEric W. Biederman 	__bad_area(regs, error_code, address, 0, SEGV_MAPERR);
86792181f19SNick Piggin }
86892181f19SNick Piggin 
86933a709b2SDave Hansen static inline bool bad_area_access_from_pkeys(unsigned long error_code,
87033a709b2SDave Hansen 		struct vm_area_struct *vma)
87133a709b2SDave Hansen {
87207f146f5SDave Hansen 	/* This code is always called on the current mm */
87307f146f5SDave Hansen 	bool foreign = false;
87407f146f5SDave Hansen 
87533a709b2SDave Hansen 	if (!boot_cpu_has(X86_FEATURE_OSPKE))
87633a709b2SDave Hansen 		return false;
8771067f030SRicardo Neri 	if (error_code & X86_PF_PK)
87833a709b2SDave Hansen 		return true;
87907f146f5SDave Hansen 	/* this checks permission keys on the VMA: */
8801067f030SRicardo Neri 	if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
8811067f030SRicardo Neri 				       (error_code & X86_PF_INSTR), foreign))
88207f146f5SDave Hansen 		return true;
88333a709b2SDave Hansen 	return false;
88492181f19SNick Piggin }
88592181f19SNick Piggin 
8862d4a7167SIngo Molnar static noinline void
8872d4a7167SIngo Molnar bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
8887b2d0dbaSDave Hansen 		      unsigned long address, struct vm_area_struct *vma)
88992181f19SNick Piggin {
890019132ffSDave Hansen 	/*
891019132ffSDave Hansen 	 * This OSPKE check is not strictly necessary at runtime.
892019132ffSDave Hansen 	 * But, doing it this way allows compiler optimizations
893019132ffSDave Hansen 	 * if pkeys are compiled out.
894019132ffSDave Hansen 	 */
895aba1ecd3SEric W. Biederman 	if (bad_area_access_from_pkeys(error_code, vma)) {
8969db812dbSEric W. Biederman 		/*
8979db812dbSEric W. Biederman 		 * A protection key fault means that the PKRU value did not allow
8989db812dbSEric W. Biederman 		 * access to some PTE.  Userspace can figure out what PKRU was
8999db812dbSEric W. Biederman 		 * from the XSAVE state.  This function captures the pkey from
9009db812dbSEric W. Biederman 		 * the vma and passes it to userspace so userspace can discover
9019db812dbSEric W. Biederman 		 * which protection key was set on the PTE.
9029db812dbSEric W. Biederman 		 *
9039db812dbSEric W. Biederman 		 * If we get here, we know that the hardware signaled a X86_PF_PK
9049db812dbSEric W. Biederman 		 * fault and that there was a VMA once we got in the fault
9059db812dbSEric W. Biederman 		 * handler.  It does *not* guarantee that the VMA we find here
9069db812dbSEric W. Biederman 		 * was the one that we faulted on.
9079db812dbSEric W. Biederman 		 *
9089db812dbSEric W. Biederman 		 * 1. T1   : mprotect_key(foo, PAGE_SIZE, pkey=4);
9099db812dbSEric W. Biederman 		 * 2. T1   : set PKRU to deny access to pkey=4, touches page
9109db812dbSEric W. Biederman 		 * 3. T1   : faults...
9119db812dbSEric W. Biederman 		 * 4.    T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
912c1e8d7c6SMichel Lespinasse 		 * 5. T1   : enters fault handler, takes mmap_lock, etc...
9139db812dbSEric W. Biederman 		 * 6. T1   : reaches here, sees vma_pkey(vma)=5, when we really
9149db812dbSEric W. Biederman 		 *	     faulted on a pte with its pkey=4.
9159db812dbSEric W. Biederman 		 */
916aba1ecd3SEric W. Biederman 		u32 pkey = vma_pkey(vma);
9179db812dbSEric W. Biederman 
918419ceeb1SEric W. Biederman 		__bad_area(regs, error_code, address, pkey, SEGV_PKUERR);
919aba1ecd3SEric W. Biederman 	} else {
920419ceeb1SEric W. Biederman 		__bad_area(regs, error_code, address, 0, SEGV_ACCERR);
921aba1ecd3SEric W. Biederman 	}
92292181f19SNick Piggin }
92392181f19SNick Piggin 
9242d4a7167SIngo Molnar static void
925a6e04aa9SAndi Kleen do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
9263d353901SSouptick Joarder 	  vm_fault_t fault)
92792181f19SNick Piggin {
9282d4a7167SIngo Molnar 	/* Kernel mode? Handle exceptions or die: */
9291067f030SRicardo Neri 	if (!(error_code & X86_PF_USER)) {
9304fc34901SAndy Lutomirski 		no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
93196054569SLinus Torvalds 		return;
93296054569SLinus Torvalds 	}
9332d4a7167SIngo Molnar 
934cd1b68f0SIngo Molnar 	/* User-space => ok to do another page fault: */
93592181f19SNick Piggin 	if (is_prefetch(regs, error_code, address))
93692181f19SNick Piggin 		return;
9372d4a7167SIngo Molnar 
938cd072dabSSean Christopherson 	sanitize_error_code(address, &error_code);
939cd072dabSSean Christopherson 
940*334872a0SSean Christopherson 	if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address))
941*334872a0SSean Christopherson 		return;
942*334872a0SSean Christopherson 
943e49d3cbeSAndy Lutomirski 	set_signal_archinfo(address, error_code);
9442d4a7167SIngo Molnar 
945a6e04aa9SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE
946f672b49bSAndi Kleen 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
947318759b4SEric W. Biederman 		struct task_struct *tsk = current;
94840e55394SEric W. Biederman 		unsigned lsb = 0;
94940e55394SEric W. Biederman 
95040e55394SEric W. Biederman 		pr_err(
951a6e04aa9SAndi Kleen 	"MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
952a6e04aa9SAndi Kleen 			tsk->comm, tsk->pid, address);
95340e55394SEric W. Biederman 		if (fault & VM_FAULT_HWPOISON_LARGE)
95440e55394SEric W. Biederman 			lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
95540e55394SEric W. Biederman 		if (fault & VM_FAULT_HWPOISON)
95640e55394SEric W. Biederman 			lsb = PAGE_SHIFT;
957f8eac901SEric W. Biederman 		force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb);
95840e55394SEric W. Biederman 		return;
959a6e04aa9SAndi Kleen 	}
960a6e04aa9SAndi Kleen #endif
9612e1661d2SEric W. Biederman 	force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
96292181f19SNick Piggin }
96392181f19SNick Piggin 
9643a13c4d7SJohannes Weiner static noinline void
9652d4a7167SIngo Molnar mm_fault_error(struct pt_regs *regs, unsigned long error_code,
96625c102d8SEric W. Biederman 	       unsigned long address, vm_fault_t fault)
96792181f19SNick Piggin {
9681067f030SRicardo Neri 	if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) {
9694fc34901SAndy Lutomirski 		no_context(regs, error_code, address, 0, 0);
9703a13c4d7SJohannes Weiner 		return;
971b80ef10eSKOSAKI Motohiro 	}
972b80ef10eSKOSAKI Motohiro 
9732d4a7167SIngo Molnar 	if (fault & VM_FAULT_OOM) {
974f8626854SAndrey Vagin 		/* Kernel mode? Handle exceptions or die: */
9751067f030SRicardo Neri 		if (!(error_code & X86_PF_USER)) {
9764fc34901SAndy Lutomirski 			no_context(regs, error_code, address,
9774fc34901SAndy Lutomirski 				   SIGSEGV, SEGV_MAPERR);
9783a13c4d7SJohannes Weiner 			return;
979f8626854SAndrey Vagin 		}
980f8626854SAndrey Vagin 
981c2d23f91SDavid Rientjes 		/*
982c2d23f91SDavid Rientjes 		 * We ran out of memory, call the OOM killer, and return the
983c2d23f91SDavid Rientjes 		 * userspace (which will retry the fault, or kill us if we got
984c2d23f91SDavid Rientjes 		 * oom-killed):
985c2d23f91SDavid Rientjes 		 */
986c2d23f91SDavid Rientjes 		pagefault_out_of_memory();
9872d4a7167SIngo Molnar 	} else {
988f672b49bSAndi Kleen 		if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
989f672b49bSAndi Kleen 			     VM_FAULT_HWPOISON_LARGE))
99027274f73SEric W. Biederman 			do_sigbus(regs, error_code, address, fault);
99133692f27SLinus Torvalds 		else if (fault & VM_FAULT_SIGSEGV)
992768fd9c6SEric W. Biederman 			bad_area_nosemaphore(regs, error_code, address);
99392181f19SNick Piggin 		else
99492181f19SNick Piggin 			BUG();
99592181f19SNick Piggin 	}
9962d4a7167SIngo Molnar }
99792181f19SNick Piggin 
9988fed6200SDave Hansen static int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte)
999d8b57bb7SThomas Gleixner {
10001067f030SRicardo Neri 	if ((error_code & X86_PF_WRITE) && !pte_write(*pte))
1001d8b57bb7SThomas Gleixner 		return 0;
10022d4a7167SIngo Molnar 
10031067f030SRicardo Neri 	if ((error_code & X86_PF_INSTR) && !pte_exec(*pte))
1004d8b57bb7SThomas Gleixner 		return 0;
1005d8b57bb7SThomas Gleixner 
1006d8b57bb7SThomas Gleixner 	return 1;
1007d8b57bb7SThomas Gleixner }
1008d8b57bb7SThomas Gleixner 
1009c61e211dSHarvey Harrison /*
10102d4a7167SIngo Molnar  * Handle a spurious fault caused by a stale TLB entry.
10112d4a7167SIngo Molnar  *
10122d4a7167SIngo Molnar  * This allows us to lazily refresh the TLB when increasing the
10132d4a7167SIngo Molnar  * permissions of a kernel page (RO -> RW or NX -> X).  Doing it
10142d4a7167SIngo Molnar  * eagerly is very expensive since that implies doing a full
10152d4a7167SIngo Molnar  * cross-processor TLB flush, even if no stale TLB entries exist
10162d4a7167SIngo Molnar  * on other processors.
10172d4a7167SIngo Molnar  *
101831668511SDavid Vrabel  * Spurious faults may only occur if the TLB contains an entry with
101931668511SDavid Vrabel  * fewer permission than the page table entry.  Non-present (P = 0)
102031668511SDavid Vrabel  * and reserved bit (R = 1) faults are never spurious.
102131668511SDavid Vrabel  *
10225b727a3bSJeremy Fitzhardinge  * There are no security implications to leaving a stale TLB when
10235b727a3bSJeremy Fitzhardinge  * increasing the permissions on a page.
102431668511SDavid Vrabel  *
102531668511SDavid Vrabel  * Returns non-zero if a spurious fault was handled, zero otherwise.
102631668511SDavid Vrabel  *
102731668511SDavid Vrabel  * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
102831668511SDavid Vrabel  * (Optional Invalidation).
10295b727a3bSJeremy Fitzhardinge  */
10309326638cSMasami Hiramatsu static noinline int
10318fed6200SDave Hansen spurious_kernel_fault(unsigned long error_code, unsigned long address)
10325b727a3bSJeremy Fitzhardinge {
10335b727a3bSJeremy Fitzhardinge 	pgd_t *pgd;
1034e0c4f675SKirill A. Shutemov 	p4d_t *p4d;
10355b727a3bSJeremy Fitzhardinge 	pud_t *pud;
10365b727a3bSJeremy Fitzhardinge 	pmd_t *pmd;
10375b727a3bSJeremy Fitzhardinge 	pte_t *pte;
10383c3e5694SSteven Rostedt 	int ret;
10395b727a3bSJeremy Fitzhardinge 
104031668511SDavid Vrabel 	/*
104131668511SDavid Vrabel 	 * Only writes to RO or instruction fetches from NX may cause
104231668511SDavid Vrabel 	 * spurious faults.
104331668511SDavid Vrabel 	 *
104431668511SDavid Vrabel 	 * These could be from user or supervisor accesses but the TLB
104531668511SDavid Vrabel 	 * is only lazily flushed after a kernel mapping protection
104631668511SDavid Vrabel 	 * change, so user accesses are not expected to cause spurious
104731668511SDavid Vrabel 	 * faults.
104831668511SDavid Vrabel 	 */
10491067f030SRicardo Neri 	if (error_code != (X86_PF_WRITE | X86_PF_PROT) &&
10501067f030SRicardo Neri 	    error_code != (X86_PF_INSTR | X86_PF_PROT))
10515b727a3bSJeremy Fitzhardinge 		return 0;
10525b727a3bSJeremy Fitzhardinge 
10535b727a3bSJeremy Fitzhardinge 	pgd = init_mm.pgd + pgd_index(address);
10545b727a3bSJeremy Fitzhardinge 	if (!pgd_present(*pgd))
10555b727a3bSJeremy Fitzhardinge 		return 0;
10565b727a3bSJeremy Fitzhardinge 
1057e0c4f675SKirill A. Shutemov 	p4d = p4d_offset(pgd, address);
1058e0c4f675SKirill A. Shutemov 	if (!p4d_present(*p4d))
1059e0c4f675SKirill A. Shutemov 		return 0;
1060e0c4f675SKirill A. Shutemov 
1061e0c4f675SKirill A. Shutemov 	if (p4d_large(*p4d))
10628fed6200SDave Hansen 		return spurious_kernel_fault_check(error_code, (pte_t *) p4d);
1063e0c4f675SKirill A. Shutemov 
1064e0c4f675SKirill A. Shutemov 	pud = pud_offset(p4d, address);
10655b727a3bSJeremy Fitzhardinge 	if (!pud_present(*pud))
10665b727a3bSJeremy Fitzhardinge 		return 0;
10675b727a3bSJeremy Fitzhardinge 
1068d8b57bb7SThomas Gleixner 	if (pud_large(*pud))
10698fed6200SDave Hansen 		return spurious_kernel_fault_check(error_code, (pte_t *) pud);
1070d8b57bb7SThomas Gleixner 
10715b727a3bSJeremy Fitzhardinge 	pmd = pmd_offset(pud, address);
10725b727a3bSJeremy Fitzhardinge 	if (!pmd_present(*pmd))
10735b727a3bSJeremy Fitzhardinge 		return 0;
10745b727a3bSJeremy Fitzhardinge 
1075d8b57bb7SThomas Gleixner 	if (pmd_large(*pmd))
10768fed6200SDave Hansen 		return spurious_kernel_fault_check(error_code, (pte_t *) pmd);
1077d8b57bb7SThomas Gleixner 
10785b727a3bSJeremy Fitzhardinge 	pte = pte_offset_kernel(pmd, address);
1079954f8571SAndrea Arcangeli 	if (!pte_present(*pte))
10805b727a3bSJeremy Fitzhardinge 		return 0;
10815b727a3bSJeremy Fitzhardinge 
10828fed6200SDave Hansen 	ret = spurious_kernel_fault_check(error_code, pte);
10833c3e5694SSteven Rostedt 	if (!ret)
10843c3e5694SSteven Rostedt 		return 0;
10853c3e5694SSteven Rostedt 
10863c3e5694SSteven Rostedt 	/*
10872d4a7167SIngo Molnar 	 * Make sure we have permissions in PMD.
10882d4a7167SIngo Molnar 	 * If not, then there's a bug in the page tables:
10893c3e5694SSteven Rostedt 	 */
10908fed6200SDave Hansen 	ret = spurious_kernel_fault_check(error_code, (pte_t *) pmd);
10913c3e5694SSteven Rostedt 	WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
10922d4a7167SIngo Molnar 
10933c3e5694SSteven Rostedt 	return ret;
10945b727a3bSJeremy Fitzhardinge }
10958fed6200SDave Hansen NOKPROBE_SYMBOL(spurious_kernel_fault);
10965b727a3bSJeremy Fitzhardinge 
1097c61e211dSHarvey Harrison int show_unhandled_signals = 1;
1098c61e211dSHarvey Harrison 
10992d4a7167SIngo Molnar static inline int
110068da336aSMichel Lespinasse access_error(unsigned long error_code, struct vm_area_struct *vma)
110192181f19SNick Piggin {
110207f146f5SDave Hansen 	/* This is only called for the current mm, so: */
110307f146f5SDave Hansen 	bool foreign = false;
1104e8c6226dSDave Hansen 
1105e8c6226dSDave Hansen 	/*
1106e8c6226dSDave Hansen 	 * Read or write was blocked by protection keys.  This is
1107e8c6226dSDave Hansen 	 * always an unconditional error and can never result in
1108e8c6226dSDave Hansen 	 * a follow-up action to resolve the fault, like a COW.
1109e8c6226dSDave Hansen 	 */
11101067f030SRicardo Neri 	if (error_code & X86_PF_PK)
1111e8c6226dSDave Hansen 		return 1;
1112e8c6226dSDave Hansen 
111333a709b2SDave Hansen 	/*
111474faeee0SSean Christopherson 	 * SGX hardware blocked the access.  This usually happens
111574faeee0SSean Christopherson 	 * when the enclave memory contents have been destroyed, like
111674faeee0SSean Christopherson 	 * after a suspend/resume cycle. In any case, the kernel can't
111774faeee0SSean Christopherson 	 * fix the cause of the fault.  Handle the fault as an access
111874faeee0SSean Christopherson 	 * error even in cases where no actual access violation
111974faeee0SSean Christopherson 	 * occurred.  This allows userspace to rebuild the enclave in
112074faeee0SSean Christopherson 	 * response to the signal.
112174faeee0SSean Christopherson 	 */
112274faeee0SSean Christopherson 	if (unlikely(error_code & X86_PF_SGX))
112374faeee0SSean Christopherson 		return 1;
112474faeee0SSean Christopherson 
112574faeee0SSean Christopherson 	/*
112607f146f5SDave Hansen 	 * Make sure to check the VMA so that we do not perform
11271067f030SRicardo Neri 	 * faults just to hit a X86_PF_PK as soon as we fill in a
112807f146f5SDave Hansen 	 * page.
112907f146f5SDave Hansen 	 */
11301067f030SRicardo Neri 	if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
11311067f030SRicardo Neri 				       (error_code & X86_PF_INSTR), foreign))
113207f146f5SDave Hansen 		return 1;
113333a709b2SDave Hansen 
11341067f030SRicardo Neri 	if (error_code & X86_PF_WRITE) {
11352d4a7167SIngo Molnar 		/* write, present and write, not present: */
113692181f19SNick Piggin 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
113792181f19SNick Piggin 			return 1;
11382d4a7167SIngo Molnar 		return 0;
11392d4a7167SIngo Molnar 	}
11402d4a7167SIngo Molnar 
11412d4a7167SIngo Molnar 	/* read, present: */
11421067f030SRicardo Neri 	if (unlikely(error_code & X86_PF_PROT))
114392181f19SNick Piggin 		return 1;
11442d4a7167SIngo Molnar 
11452d4a7167SIngo Molnar 	/* read, not present: */
11463122e80eSAnshuman Khandual 	if (unlikely(!vma_is_accessible(vma)))
114792181f19SNick Piggin 		return 1;
114892181f19SNick Piggin 
114992181f19SNick Piggin 	return 0;
115092181f19SNick Piggin }
115192181f19SNick Piggin 
115230063810STony Luck bool fault_in_kernel_space(unsigned long address)
11530973a06cSHiroshi Shimamoto {
11543ae0ad92SDave Hansen 	/*
11553ae0ad92SDave Hansen 	 * On 64-bit systems, the vsyscall page is at an address above
11563ae0ad92SDave Hansen 	 * TASK_SIZE_MAX, but is not considered part of the kernel
11573ae0ad92SDave Hansen 	 * address space.
11583ae0ad92SDave Hansen 	 */
11593ae0ad92SDave Hansen 	if (IS_ENABLED(CONFIG_X86_64) && is_vsyscall_vaddr(address))
11603ae0ad92SDave Hansen 		return false;
11613ae0ad92SDave Hansen 
1162d9517346SIngo Molnar 	return address >= TASK_SIZE_MAX;
11630973a06cSHiroshi Shimamoto }
11640973a06cSHiroshi Shimamoto 
1165c61e211dSHarvey Harrison /*
11668fed6200SDave Hansen  * Called for all faults where 'address' is part of the kernel address
11678fed6200SDave Hansen  * space.  Might get called for faults that originate from *code* that
11688fed6200SDave Hansen  * ran in userspace or the kernel.
1169c61e211dSHarvey Harrison  */
11708fed6200SDave Hansen static void
11718fed6200SDave Hansen do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code,
11720ac09f9fSJiri Olsa 		   unsigned long address)
1173c61e211dSHarvey Harrison {
11748fed6200SDave Hansen 	/*
1175367e3f1dSDave Hansen 	 * Protection keys exceptions only happen on user pages.  We
1176367e3f1dSDave Hansen 	 * have no user pages in the kernel portion of the address
1177367e3f1dSDave Hansen 	 * space, so do not expect them here.
1178367e3f1dSDave Hansen 	 */
1179367e3f1dSDave Hansen 	WARN_ON_ONCE(hw_error_code & X86_PF_PK);
1180367e3f1dSDave Hansen 
11814819e15fSJoerg Roedel #ifdef CONFIG_X86_32
11824819e15fSJoerg Roedel 	/*
11834819e15fSJoerg Roedel 	 * We can fault-in kernel-space virtual memory on-demand. The
11844819e15fSJoerg Roedel 	 * 'reference' page table is init_mm.pgd.
11854819e15fSJoerg Roedel 	 *
11864819e15fSJoerg Roedel 	 * NOTE! We MUST NOT take any locks for this case. We may
11874819e15fSJoerg Roedel 	 * be in an interrupt or a critical region, and should
11884819e15fSJoerg Roedel 	 * only copy the information from the master page table,
11894819e15fSJoerg Roedel 	 * nothing more.
11904819e15fSJoerg Roedel 	 *
11914819e15fSJoerg Roedel 	 * Before doing this on-demand faulting, ensure that the
11924819e15fSJoerg Roedel 	 * fault is not any of the following:
11934819e15fSJoerg Roedel 	 * 1. A fault on a PTE with a reserved bit set.
11944819e15fSJoerg Roedel 	 * 2. A fault caused by a user-mode access.  (Do not demand-
11954819e15fSJoerg Roedel 	 *    fault kernel memory due to user-mode accesses).
11964819e15fSJoerg Roedel 	 * 3. A fault caused by a page-level protection violation.
11974819e15fSJoerg Roedel 	 *    (A demand fault would be on a non-present page which
11984819e15fSJoerg Roedel 	 *     would have X86_PF_PROT==0).
11994819e15fSJoerg Roedel 	 *
12004819e15fSJoerg Roedel 	 * This is only needed to close a race condition on x86-32 in
12014819e15fSJoerg Roedel 	 * the vmalloc mapping/unmapping code. See the comment above
12024819e15fSJoerg Roedel 	 * vmalloc_fault() for details. On x86-64 the race does not
12034819e15fSJoerg Roedel 	 * exist as the vmalloc mappings don't need to be synchronized
12044819e15fSJoerg Roedel 	 * there.
12054819e15fSJoerg Roedel 	 */
12064819e15fSJoerg Roedel 	if (!(hw_error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) {
12074819e15fSJoerg Roedel 		if (vmalloc_fault(address) >= 0)
12084819e15fSJoerg Roedel 			return;
12094819e15fSJoerg Roedel 	}
12104819e15fSJoerg Roedel #endif
12114819e15fSJoerg Roedel 
12128fed6200SDave Hansen 	/* Was the fault spurious, caused by lazy TLB invalidation? */
12138fed6200SDave Hansen 	if (spurious_kernel_fault(hw_error_code, address))
12148fed6200SDave Hansen 		return;
12158fed6200SDave Hansen 
12168fed6200SDave Hansen 	/* kprobes don't want to hook the spurious faults: */
1217b98cca44SAnshuman Khandual 	if (kprobe_page_fault(regs, X86_TRAP_PF))
12188fed6200SDave Hansen 		return;
12198fed6200SDave Hansen 
12208fed6200SDave Hansen 	/*
12218fed6200SDave Hansen 	 * Note, despite being a "bad area", there are quite a few
12228fed6200SDave Hansen 	 * acceptable reasons to get here, such as erratum fixups
12238fed6200SDave Hansen 	 * and handling kernel code that can fault, like get_user().
12248fed6200SDave Hansen 	 *
12258fed6200SDave Hansen 	 * Don't take the mm semaphore here. If we fixup a prefetch
12268fed6200SDave Hansen 	 * fault we could otherwise deadlock:
12278fed6200SDave Hansen 	 */
1228ba9f6f89SLinus Torvalds 	bad_area_nosemaphore(regs, hw_error_code, address);
12298fed6200SDave Hansen }
12308fed6200SDave Hansen NOKPROBE_SYMBOL(do_kern_addr_fault);
12318fed6200SDave Hansen 
1232aa37c51bSDave Hansen /* Handle faults in the user portion of the address space */
1233aa37c51bSDave Hansen static inline
1234aa37c51bSDave Hansen void do_user_addr_fault(struct pt_regs *regs,
1235aa37c51bSDave Hansen 			unsigned long hw_error_code,
1236c61e211dSHarvey Harrison 			unsigned long address)
1237c61e211dSHarvey Harrison {
1238c61e211dSHarvey Harrison 	struct vm_area_struct *vma;
1239c61e211dSHarvey Harrison 	struct task_struct *tsk;
12402d4a7167SIngo Molnar 	struct mm_struct *mm;
1241968614fcSPeter Xu 	vm_fault_t fault;
1242dde16072SPeter Xu 	unsigned int flags = FAULT_FLAG_DEFAULT;
1243c61e211dSHarvey Harrison 
1244c61e211dSHarvey Harrison 	tsk = current;
1245c61e211dSHarvey Harrison 	mm = tsk->mm;
12462d4a7167SIngo Molnar 
12472d4a7167SIngo Molnar 	/* kprobes don't want to hook the spurious faults: */
1248b98cca44SAnshuman Khandual 	if (unlikely(kprobe_page_fault(regs, X86_TRAP_PF)))
12499be260a6SMasami Hiramatsu 		return;
1250e00b12e6SPeter Zijlstra 
12515b0c2cacSDave Hansen 	/*
12525b0c2cacSDave Hansen 	 * Reserved bits are never expected to be set on
12535b0c2cacSDave Hansen 	 * entries in the user portion of the page tables.
12545b0c2cacSDave Hansen 	 */
1255164477c2SDave Hansen 	if (unlikely(hw_error_code & X86_PF_RSVD))
1256164477c2SDave Hansen 		pgtable_bad(regs, hw_error_code, address);
1257e00b12e6SPeter Zijlstra 
12585b0c2cacSDave Hansen 	/*
1259e50928d7SAndy Lutomirski 	 * If SMAP is on, check for invalid kernel (supervisor) access to user
1260e50928d7SAndy Lutomirski 	 * pages in the user address space.  The odd case here is WRUSS,
1261e50928d7SAndy Lutomirski 	 * which, according to the preliminary documentation, does not respect
1262e50928d7SAndy Lutomirski 	 * SMAP and will have the USER bit set so, in all cases, SMAP
1263e50928d7SAndy Lutomirski 	 * enforcement appears to be consistent with the USER bit.
12645b0c2cacSDave Hansen 	 */
1265a15781b5SAndy Lutomirski 	if (unlikely(cpu_feature_enabled(X86_FEATURE_SMAP) &&
1266a15781b5SAndy Lutomirski 		     !(hw_error_code & X86_PF_USER) &&
1267e50928d7SAndy Lutomirski 		     !(regs->flags & X86_EFLAGS_AC)))
1268a15781b5SAndy Lutomirski 	{
1269ba9f6f89SLinus Torvalds 		bad_area_nosemaphore(regs, hw_error_code, address);
1270e00b12e6SPeter Zijlstra 		return;
1271e00b12e6SPeter Zijlstra 	}
1272e00b12e6SPeter Zijlstra 
1273e00b12e6SPeter Zijlstra 	/*
1274e00b12e6SPeter Zijlstra 	 * If we're in an interrupt, have no user context or are running
127570ffdb93SDavid Hildenbrand 	 * in a region with pagefaults disabled then we must not take the fault
1276e00b12e6SPeter Zijlstra 	 */
127770ffdb93SDavid Hildenbrand 	if (unlikely(faulthandler_disabled() || !mm)) {
1278ba9f6f89SLinus Torvalds 		bad_area_nosemaphore(regs, hw_error_code, address);
1279e00b12e6SPeter Zijlstra 		return;
1280e00b12e6SPeter Zijlstra 	}
1281e00b12e6SPeter Zijlstra 
1282c61e211dSHarvey Harrison 	/*
1283891cffbdSLinus Torvalds 	 * It's safe to allow irq's after cr2 has been saved and the
1284891cffbdSLinus Torvalds 	 * vmalloc fault has been handled.
1285891cffbdSLinus Torvalds 	 *
1286891cffbdSLinus Torvalds 	 * User-mode registers count as a user access even for any
12872d4a7167SIngo Molnar 	 * potential system fault or CPU buglet:
1288c61e211dSHarvey Harrison 	 */
1289f39b6f0eSAndy Lutomirski 	if (user_mode(regs)) {
1290891cffbdSLinus Torvalds 		local_irq_enable();
1291759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
12922d4a7167SIngo Molnar 	} else {
12932d4a7167SIngo Molnar 		if (regs->flags & X86_EFLAGS_IF)
1294c61e211dSHarvey Harrison 			local_irq_enable();
12952d4a7167SIngo Molnar 	}
1296c61e211dSHarvey Harrison 
1297a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
12987dd1fcc2SPeter Zijlstra 
12990ed32f1aSAndy Lutomirski 	if (hw_error_code & X86_PF_WRITE)
1300759496baSJohannes Weiner 		flags |= FAULT_FLAG_WRITE;
13010ed32f1aSAndy Lutomirski 	if (hw_error_code & X86_PF_INSTR)
1302d61172b4SDave Hansen 		flags |= FAULT_FLAG_INSTRUCTION;
1303759496baSJohannes Weiner 
13043ae0ad92SDave Hansen #ifdef CONFIG_X86_64
13053a1dfe6eSIngo Molnar 	/*
1306918ce325SAndy Lutomirski 	 * Faults in the vsyscall page might need emulation.  The
1307918ce325SAndy Lutomirski 	 * vsyscall page is at a high address (>PAGE_OFFSET), but is
1308918ce325SAndy Lutomirski 	 * considered to be part of the user address space.
1309c61e211dSHarvey Harrison 	 *
13103ae0ad92SDave Hansen 	 * The vsyscall page does not have a "real" VMA, so do this
13113ae0ad92SDave Hansen 	 * emulation before we go searching for VMAs.
1312e0a446ceSAndy Lutomirski 	 *
1313e0a446ceSAndy Lutomirski 	 * PKRU never rejects instruction fetches, so we don't need
1314e0a446ceSAndy Lutomirski 	 * to consider the PF_PK bit.
13153ae0ad92SDave Hansen 	 */
1316918ce325SAndy Lutomirski 	if (is_vsyscall_vaddr(address)) {
1317918ce325SAndy Lutomirski 		if (emulate_vsyscall(hw_error_code, regs, address))
13183ae0ad92SDave Hansen 			return;
13193ae0ad92SDave Hansen 	}
13203ae0ad92SDave Hansen #endif
13213ae0ad92SDave Hansen 
1322c61e211dSHarvey Harrison 	/*
132388259744SDave Hansen 	 * Kernel-mode access to the user address space should only occur
132488259744SDave Hansen 	 * on well-defined single instructions listed in the exception
132588259744SDave Hansen 	 * tables.  But, an erroneous kernel fault occurring outside one of
1326c1e8d7c6SMichel Lespinasse 	 * those areas which also holds mmap_lock might deadlock attempting
132788259744SDave Hansen 	 * to validate the fault against the address space.
1328c61e211dSHarvey Harrison 	 *
132988259744SDave Hansen 	 * Only do the expensive exception table search when we might be at
133088259744SDave Hansen 	 * risk of a deadlock.  This happens if we
1331c1e8d7c6SMichel Lespinasse 	 * 1. Failed to acquire mmap_lock, and
13326344be60SAndy Lutomirski 	 * 2. The access did not originate in userspace.
1333c61e211dSHarvey Harrison 	 */
1334d8ed45c5SMichel Lespinasse 	if (unlikely(!mmap_read_trylock(mm))) {
13356344be60SAndy Lutomirski 		if (!user_mode(regs) && !search_exception_tables(regs->ip)) {
133688259744SDave Hansen 			/*
133788259744SDave Hansen 			 * Fault from code in kernel from
133888259744SDave Hansen 			 * which we do not expect faults.
133988259744SDave Hansen 			 */
13400ed32f1aSAndy Lutomirski 			bad_area_nosemaphore(regs, hw_error_code, address);
134192181f19SNick Piggin 			return;
134292181f19SNick Piggin 		}
1343d065bd81SMichel Lespinasse retry:
1344d8ed45c5SMichel Lespinasse 		mmap_read_lock(mm);
134501006074SPeter Zijlstra 	} else {
134601006074SPeter Zijlstra 		/*
13472d4a7167SIngo Molnar 		 * The above down_read_trylock() might have succeeded in
13482d4a7167SIngo Molnar 		 * which case we'll have missed the might_sleep() from
13492d4a7167SIngo Molnar 		 * down_read():
135001006074SPeter Zijlstra 		 */
135101006074SPeter Zijlstra 		might_sleep();
1352c61e211dSHarvey Harrison 	}
1353c61e211dSHarvey Harrison 
1354c61e211dSHarvey Harrison 	vma = find_vma(mm, address);
135592181f19SNick Piggin 	if (unlikely(!vma)) {
13560ed32f1aSAndy Lutomirski 		bad_area(regs, hw_error_code, address);
135792181f19SNick Piggin 		return;
135892181f19SNick Piggin 	}
135992181f19SNick Piggin 	if (likely(vma->vm_start <= address))
1360c61e211dSHarvey Harrison 		goto good_area;
136192181f19SNick Piggin 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
13620ed32f1aSAndy Lutomirski 		bad_area(regs, hw_error_code, address);
136392181f19SNick Piggin 		return;
136492181f19SNick Piggin 	}
136592181f19SNick Piggin 	if (unlikely(expand_stack(vma, address))) {
13660ed32f1aSAndy Lutomirski 		bad_area(regs, hw_error_code, address);
136792181f19SNick Piggin 		return;
136892181f19SNick Piggin 	}
136992181f19SNick Piggin 
1370c61e211dSHarvey Harrison 	/*
1371c61e211dSHarvey Harrison 	 * Ok, we have a good vm_area for this memory access, so
1372c61e211dSHarvey Harrison 	 * we can handle it..
1373c61e211dSHarvey Harrison 	 */
1374c61e211dSHarvey Harrison good_area:
13750ed32f1aSAndy Lutomirski 	if (unlikely(access_error(hw_error_code, vma))) {
13760ed32f1aSAndy Lutomirski 		bad_area_access_error(regs, hw_error_code, address, vma);
137792181f19SNick Piggin 		return;
1378c61e211dSHarvey Harrison 	}
1379c61e211dSHarvey Harrison 
1380c61e211dSHarvey Harrison 	/*
1381c61e211dSHarvey Harrison 	 * If for any reason at all we couldn't handle the fault,
1382c61e211dSHarvey Harrison 	 * make sure we exit gracefully rather than endlessly redo
13839a95f3cfSPaul Cassella 	 * the fault.  Since we never set FAULT_FLAG_RETRY_NOWAIT, if
1384c1e8d7c6SMichel Lespinasse 	 * we get VM_FAULT_RETRY back, the mmap_lock has been unlocked.
1385cb0631fdSVlastimil Babka 	 *
1386c1e8d7c6SMichel Lespinasse 	 * Note that handle_userfault() may also release and reacquire mmap_lock
1387cb0631fdSVlastimil Babka 	 * (and not return with VM_FAULT_RETRY), when returning to userland to
1388cb0631fdSVlastimil Babka 	 * repeat the page fault later with a VM_FAULT_NOPAGE retval
1389cb0631fdSVlastimil Babka 	 * (potentially after handling any pending signal during the return to
1390cb0631fdSVlastimil Babka 	 * userland). The return to userland is identified whenever
1391cb0631fdSVlastimil Babka 	 * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags.
1392c61e211dSHarvey Harrison 	 */
1393968614fcSPeter Xu 	fault = handle_mm_fault(vma, address, flags, regs);
13942d4a7167SIngo Molnar 
139539678191SPeter Xu 	/* Quick path to respond to signals */
139639678191SPeter Xu 	if (fault_signal_pending(fault, regs)) {
139739678191SPeter Xu 		if (!user_mode(regs))
139839678191SPeter Xu 			no_context(regs, hw_error_code, address, SIGBUS,
139939678191SPeter Xu 				   BUS_ADRERR);
140039678191SPeter Xu 		return;
140139678191SPeter Xu 	}
140239678191SPeter Xu 
14033a13c4d7SJohannes Weiner 	/*
1404c1e8d7c6SMichel Lespinasse 	 * If we need to retry the mmap_lock has already been released,
140526178ec1SLinus Torvalds 	 * and if there is a fatal signal pending there is no guarantee
140626178ec1SLinus Torvalds 	 * that we made any progress. Handle this case first.
14073a13c4d7SJohannes Weiner 	 */
140839678191SPeter Xu 	if (unlikely((fault & VM_FAULT_RETRY) &&
140939678191SPeter Xu 		     (flags & FAULT_FLAG_ALLOW_RETRY))) {
141026178ec1SLinus Torvalds 		flags |= FAULT_FLAG_TRIED;
141126178ec1SLinus Torvalds 		goto retry;
141226178ec1SLinus Torvalds 	}
141326178ec1SLinus Torvalds 
1414d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
141526178ec1SLinus Torvalds 	if (unlikely(fault & VM_FAULT_ERROR)) {
14160ed32f1aSAndy Lutomirski 		mm_fault_error(regs, hw_error_code, address, fault);
141737b23e05SKOSAKI Motohiro 		return;
141837b23e05SKOSAKI Motohiro 	}
141937b23e05SKOSAKI Motohiro 
14208c938f9fSIngo Molnar 	check_v8086_mode(regs, address, tsk);
1421c61e211dSHarvey Harrison }
1422aa37c51bSDave Hansen NOKPROBE_SYMBOL(do_user_addr_fault);
1423aa37c51bSDave Hansen 
1424a0d14b89SPeter Zijlstra static __always_inline void
1425a0d14b89SPeter Zijlstra trace_page_fault_entries(struct pt_regs *regs, unsigned long error_code,
1426a0d14b89SPeter Zijlstra 			 unsigned long address)
1427d34603b0SSeiji Aguchi {
1428a0d14b89SPeter Zijlstra 	if (!trace_pagefault_enabled())
1429a0d14b89SPeter Zijlstra 		return;
1430a0d14b89SPeter Zijlstra 
1431d34603b0SSeiji Aguchi 	if (user_mode(regs))
1432d4078e23SPeter Zijlstra 		trace_page_fault_user(address, regs, error_code);
1433d34603b0SSeiji Aguchi 	else
1434d4078e23SPeter Zijlstra 		trace_page_fault_kernel(address, regs, error_code);
1435d34603b0SSeiji Aguchi }
1436d34603b0SSeiji Aguchi 
143791eeafeaSThomas Gleixner static __always_inline void
143891eeafeaSThomas Gleixner handle_page_fault(struct pt_regs *regs, unsigned long error_code,
1439ee6352b2SFrederic Weisbecker 			      unsigned long address)
144011a7ffb0SThomas Gleixner {
144191eeafeaSThomas Gleixner 	trace_page_fault_entries(regs, error_code, address);
144291eeafeaSThomas Gleixner 
144391eeafeaSThomas Gleixner 	if (unlikely(kmmio_fault(regs, address)))
144491eeafeaSThomas Gleixner 		return;
144591eeafeaSThomas Gleixner 
144691eeafeaSThomas Gleixner 	/* Was the fault on kernel-controlled part of the address space? */
144791eeafeaSThomas Gleixner 	if (unlikely(fault_in_kernel_space(address))) {
144891eeafeaSThomas Gleixner 		do_kern_addr_fault(regs, error_code, address);
144991eeafeaSThomas Gleixner 	} else {
145091eeafeaSThomas Gleixner 		do_user_addr_fault(regs, error_code, address);
145191eeafeaSThomas Gleixner 		/*
145291eeafeaSThomas Gleixner 		 * User address page fault handling might have reenabled
145391eeafeaSThomas Gleixner 		 * interrupts. Fixing up all potential exit points of
145491eeafeaSThomas Gleixner 		 * do_user_addr_fault() and its leaf functions is just not
145591eeafeaSThomas Gleixner 		 * doable w/o creating an unholy mess or turning the code
145691eeafeaSThomas Gleixner 		 * upside down.
145791eeafeaSThomas Gleixner 		 */
145891eeafeaSThomas Gleixner 		local_irq_disable();
145991eeafeaSThomas Gleixner 	}
146091eeafeaSThomas Gleixner }
146191eeafeaSThomas Gleixner 
146291eeafeaSThomas Gleixner DEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault)
146391eeafeaSThomas Gleixner {
146491eeafeaSThomas Gleixner 	unsigned long address = read_cr2();
1465a27a0a55SThomas Gleixner 	irqentry_state_t state;
146691eeafeaSThomas Gleixner 
1467da1c55f1SMichel Lespinasse 	prefetchw(&current->mm->mmap_lock);
146891eeafeaSThomas Gleixner 
1469ef68017eSAndy Lutomirski 	/*
147066af4f5cSVitaly Kuznetsov 	 * KVM uses #PF vector to deliver 'page not present' events to guests
147166af4f5cSVitaly Kuznetsov 	 * (asynchronous page fault mechanism). The event happens when a
147266af4f5cSVitaly Kuznetsov 	 * userspace task is trying to access some valid (from guest's point of
147366af4f5cSVitaly Kuznetsov 	 * view) memory which is not currently mapped by the host (e.g. the
147466af4f5cSVitaly Kuznetsov 	 * memory is swapped out). Note, the corresponding "page ready" event
147566af4f5cSVitaly Kuznetsov 	 * which is injected when the memory becomes available, is delived via
147666af4f5cSVitaly Kuznetsov 	 * an interrupt mechanism and not a #PF exception
147766af4f5cSVitaly Kuznetsov 	 * (see arch/x86/kernel/kvm.c: sysvec_kvm_asyncpf_interrupt()).
1478ef68017eSAndy Lutomirski 	 *
1479ef68017eSAndy Lutomirski 	 * We are relying on the interrupted context being sane (valid RSP,
1480ef68017eSAndy Lutomirski 	 * relevant locks not held, etc.), which is fine as long as the
1481ef68017eSAndy Lutomirski 	 * interrupted context had IF=1.  We are also relying on the KVM
1482ef68017eSAndy Lutomirski 	 * async pf type field and CR2 being read consistently instead of
1483ef68017eSAndy Lutomirski 	 * getting values from real and async page faults mixed up.
1484ef68017eSAndy Lutomirski 	 *
1485ef68017eSAndy Lutomirski 	 * Fingers crossed.
148691eeafeaSThomas Gleixner 	 *
148791eeafeaSThomas Gleixner 	 * The async #PF handling code takes care of idtentry handling
148891eeafeaSThomas Gleixner 	 * itself.
1489ef68017eSAndy Lutomirski 	 */
1490ef68017eSAndy Lutomirski 	if (kvm_handle_async_pf(regs, (u32)address))
1491ef68017eSAndy Lutomirski 		return;
1492ef68017eSAndy Lutomirski 
1493ca4c6a98SThomas Gleixner 	/*
149491eeafeaSThomas Gleixner 	 * Entry handling for valid #PF from kernel mode is slightly
149591eeafeaSThomas Gleixner 	 * different: RCU is already watching and rcu_irq_enter() must not
149691eeafeaSThomas Gleixner 	 * be invoked because a kernel fault on a user space address might
149791eeafeaSThomas Gleixner 	 * sleep.
149891eeafeaSThomas Gleixner 	 *
149991eeafeaSThomas Gleixner 	 * In case the fault hit a RCU idle region the conditional entry
150091eeafeaSThomas Gleixner 	 * code reenabled RCU to avoid subsequent wreckage which helps
150191eeafeaSThomas Gleixner 	 * debugability.
1502ca4c6a98SThomas Gleixner 	 */
1503a27a0a55SThomas Gleixner 	state = irqentry_enter(regs);
150491eeafeaSThomas Gleixner 
150591eeafeaSThomas Gleixner 	instrumentation_begin();
150691eeafeaSThomas Gleixner 	handle_page_fault(regs, error_code, address);
150791eeafeaSThomas Gleixner 	instrumentation_end();
150891eeafeaSThomas Gleixner 
1509a27a0a55SThomas Gleixner 	irqentry_exit(regs, state);
1510ca4c6a98SThomas Gleixner }
1511