xref: /openbmc/linux/arch/x86/mm/fault.c (revision 5372e155a28f56122eb10db56d4130f481a89cd7)
1c61e211dSHarvey Harrison /*
2c61e211dSHarvey Harrison  *  Copyright (C) 1995  Linus Torvalds
3c61e211dSHarvey Harrison  *  Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
4f8eeb2e6SIngo Molnar  *  Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar
5c61e211dSHarvey Harrison  */
6a2bcd473SIngo Molnar #include <linux/sched.h>		/* test_thread_flag(), ...	*/
7a2bcd473SIngo Molnar #include <linux/kdebug.h>		/* oops_begin/end, ...		*/
84cdf8dbeSLinus Torvalds #include <linux/extable.h>		/* search_exception_tables	*/
9a2bcd473SIngo Molnar #include <linux/bootmem.h>		/* max_low_pfn			*/
109326638cSMasami Hiramatsu #include <linux/kprobes.h>		/* NOKPROBE_SYMBOL, ...		*/
11a2bcd473SIngo Molnar #include <linux/mmiotrace.h>		/* kmmio_handler, ...		*/
12cdd6c482SIngo Molnar #include <linux/perf_event.h>		/* perf_sw_event		*/
13f672b49bSAndi Kleen #include <linux/hugetlb.h>		/* hstate_index_to_shift	*/
14268bb0ceSLinus Torvalds #include <linux/prefetch.h>		/* prefetchw			*/
1556dd9470SFrederic Weisbecker #include <linux/context_tracking.h>	/* exception_enter(), ...	*/
1670ffdb93SDavid Hildenbrand #include <linux/uaccess.h>		/* faulthandler_disabled()	*/
17c61e211dSHarvey Harrison 
18019132ffSDave Hansen #include <asm/cpufeature.h>		/* boot_cpu_has, ...		*/
19a2bcd473SIngo Molnar #include <asm/traps.h>			/* dotraplinkage, ...		*/
20a2bcd473SIngo Molnar #include <asm/pgalloc.h>		/* pgd_*(), ...			*/
21f8561296SVegard Nossum #include <asm/kmemcheck.h>		/* kmemcheck_*(), ...		*/
22f40c3300SAndy Lutomirski #include <asm/fixmap.h>			/* VSYSCALL_ADDR		*/
23f40c3300SAndy Lutomirski #include <asm/vsyscall.h>		/* emulate_vsyscall		*/
24ba3e127eSBrian Gerst #include <asm/vm86.h>			/* struct vm86			*/
25019132ffSDave Hansen #include <asm/mmu_context.h>		/* vma_pkey()			*/
26c61e211dSHarvey Harrison 
27d34603b0SSeiji Aguchi #define CREATE_TRACE_POINTS
28d34603b0SSeiji Aguchi #include <asm/trace/exceptions.h>
29d34603b0SSeiji Aguchi 
30c61e211dSHarvey Harrison /*
312d4a7167SIngo Molnar  * Page fault error code bits:
322d4a7167SIngo Molnar  *
332d4a7167SIngo Molnar  *   bit 0 ==	 0: no page found	1: protection fault
342d4a7167SIngo Molnar  *   bit 1 ==	 0: read access		1: write access
352d4a7167SIngo Molnar  *   bit 2 ==	 0: kernel-mode access	1: user-mode access
362d4a7167SIngo Molnar  *   bit 3 ==				1: use of reserved bit detected
372d4a7167SIngo Molnar  *   bit 4 ==				1: fault was an instruction fetch
38b3ecd515SDave Hansen  *   bit 5 ==				1: protection keys block access
39c61e211dSHarvey Harrison  */
402d4a7167SIngo Molnar enum x86_pf_error_code {
412d4a7167SIngo Molnar 
422d4a7167SIngo Molnar 	PF_PROT		=		1 << 0,
432d4a7167SIngo Molnar 	PF_WRITE	=		1 << 1,
442d4a7167SIngo Molnar 	PF_USER		=		1 << 2,
452d4a7167SIngo Molnar 	PF_RSVD		=		1 << 3,
462d4a7167SIngo Molnar 	PF_INSTR	=		1 << 4,
47b3ecd515SDave Hansen 	PF_PK		=		1 << 5,
482d4a7167SIngo Molnar };
49c61e211dSHarvey Harrison 
50b814d41fSIngo Molnar /*
51b319eed0SIngo Molnar  * Returns 0 if mmiotrace is disabled, or if the fault is not
52b319eed0SIngo Molnar  * handled by mmiotrace:
53b814d41fSIngo Molnar  */
549326638cSMasami Hiramatsu static nokprobe_inline int
5562c9295fSMasami Hiramatsu kmmio_fault(struct pt_regs *regs, unsigned long addr)
5686069782SPekka Paalanen {
570fd0e3daSPekka Paalanen 	if (unlikely(is_kmmio_active()))
580fd0e3daSPekka Paalanen 		if (kmmio_handler(regs, addr) == 1)
590fd0e3daSPekka Paalanen 			return -1;
600fd0e3daSPekka Paalanen 	return 0;
6186069782SPekka Paalanen }
6286069782SPekka Paalanen 
639326638cSMasami Hiramatsu static nokprobe_inline int kprobes_fault(struct pt_regs *regs)
64c61e211dSHarvey Harrison {
65c61e211dSHarvey Harrison 	int ret = 0;
66c61e211dSHarvey Harrison 
67c61e211dSHarvey Harrison 	/* kprobe_running() needs smp_processor_id() */
68f39b6f0eSAndy Lutomirski 	if (kprobes_built_in() && !user_mode(regs)) {
69c61e211dSHarvey Harrison 		preempt_disable();
70c61e211dSHarvey Harrison 		if (kprobe_running() && kprobe_fault_handler(regs, 14))
71c61e211dSHarvey Harrison 			ret = 1;
72c61e211dSHarvey Harrison 		preempt_enable();
73c61e211dSHarvey Harrison 	}
74c61e211dSHarvey Harrison 
75c61e211dSHarvey Harrison 	return ret;
76c61e211dSHarvey Harrison }
77c61e211dSHarvey Harrison 
78c61e211dSHarvey Harrison /*
792d4a7167SIngo Molnar  * Prefetch quirks:
802d4a7167SIngo Molnar  *
812d4a7167SIngo Molnar  * 32-bit mode:
822d4a7167SIngo Molnar  *
83c61e211dSHarvey Harrison  *   Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
84c61e211dSHarvey Harrison  *   Check that here and ignore it.
85c61e211dSHarvey Harrison  *
862d4a7167SIngo Molnar  * 64-bit mode:
872d4a7167SIngo Molnar  *
88c61e211dSHarvey Harrison  *   Sometimes the CPU reports invalid exceptions on prefetch.
89c61e211dSHarvey Harrison  *   Check that here and ignore it.
90c61e211dSHarvey Harrison  *
912d4a7167SIngo Molnar  * Opcode checker based on code by Richard Brunner.
92c61e211dSHarvey Harrison  */
93107a0367SIngo Molnar static inline int
94107a0367SIngo Molnar check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
95107a0367SIngo Molnar 		      unsigned char opcode, int *prefetch)
96c61e211dSHarvey Harrison {
97107a0367SIngo Molnar 	unsigned char instr_hi = opcode & 0xf0;
98107a0367SIngo Molnar 	unsigned char instr_lo = opcode & 0x0f;
99c61e211dSHarvey Harrison 
100c61e211dSHarvey Harrison 	switch (instr_hi) {
101c61e211dSHarvey Harrison 	case 0x20:
102c61e211dSHarvey Harrison 	case 0x30:
103c61e211dSHarvey Harrison 		/*
104c61e211dSHarvey Harrison 		 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
105c61e211dSHarvey Harrison 		 * In X86_64 long mode, the CPU will signal invalid
106c61e211dSHarvey Harrison 		 * opcode if some of these prefixes are present so
107c61e211dSHarvey Harrison 		 * X86_64 will never get here anyway
108c61e211dSHarvey Harrison 		 */
109107a0367SIngo Molnar 		return ((instr_lo & 7) == 0x6);
110c61e211dSHarvey Harrison #ifdef CONFIG_X86_64
111c61e211dSHarvey Harrison 	case 0x40:
112c61e211dSHarvey Harrison 		/*
113c61e211dSHarvey Harrison 		 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
114c61e211dSHarvey Harrison 		 * Need to figure out under what instruction mode the
115c61e211dSHarvey Harrison 		 * instruction was issued. Could check the LDT for lm,
116c61e211dSHarvey Harrison 		 * but for now it's good enough to assume that long
117c61e211dSHarvey Harrison 		 * mode only uses well known segments or kernel.
118c61e211dSHarvey Harrison 		 */
119318f5a2aSAndy Lutomirski 		return (!user_mode(regs) || user_64bit_mode(regs));
120c61e211dSHarvey Harrison #endif
121c61e211dSHarvey Harrison 	case 0x60:
122c61e211dSHarvey Harrison 		/* 0x64 thru 0x67 are valid prefixes in all modes. */
123107a0367SIngo Molnar 		return (instr_lo & 0xC) == 0x4;
124c61e211dSHarvey Harrison 	case 0xF0:
125c61e211dSHarvey Harrison 		/* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
126107a0367SIngo Molnar 		return !instr_lo || (instr_lo>>1) == 1;
127c61e211dSHarvey Harrison 	case 0x00:
128c61e211dSHarvey Harrison 		/* Prefetch instruction is 0x0F0D or 0x0F18 */
129107a0367SIngo Molnar 		if (probe_kernel_address(instr, opcode))
130107a0367SIngo Molnar 			return 0;
131107a0367SIngo Molnar 
132107a0367SIngo Molnar 		*prefetch = (instr_lo == 0xF) &&
133107a0367SIngo Molnar 			(opcode == 0x0D || opcode == 0x18);
134107a0367SIngo Molnar 		return 0;
135107a0367SIngo Molnar 	default:
136107a0367SIngo Molnar 		return 0;
137107a0367SIngo Molnar 	}
138107a0367SIngo Molnar }
139107a0367SIngo Molnar 
140107a0367SIngo Molnar static int
141107a0367SIngo Molnar is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
142107a0367SIngo Molnar {
143107a0367SIngo Molnar 	unsigned char *max_instr;
144107a0367SIngo Molnar 	unsigned char *instr;
145107a0367SIngo Molnar 	int prefetch = 0;
146107a0367SIngo Molnar 
147107a0367SIngo Molnar 	/*
148107a0367SIngo Molnar 	 * If it was a exec (instruction fetch) fault on NX page, then
149107a0367SIngo Molnar 	 * do not ignore the fault:
150107a0367SIngo Molnar 	 */
151107a0367SIngo Molnar 	if (error_code & PF_INSTR)
152107a0367SIngo Molnar 		return 0;
153107a0367SIngo Molnar 
154107a0367SIngo Molnar 	instr = (void *)convert_ip_to_linear(current, regs);
155107a0367SIngo Molnar 	max_instr = instr + 15;
156107a0367SIngo Molnar 
157d31bf07fSAndy Lutomirski 	if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE_MAX)
158107a0367SIngo Molnar 		return 0;
159107a0367SIngo Molnar 
160107a0367SIngo Molnar 	while (instr < max_instr) {
161107a0367SIngo Molnar 		unsigned char opcode;
162c61e211dSHarvey Harrison 
163c61e211dSHarvey Harrison 		if (probe_kernel_address(instr, opcode))
164c61e211dSHarvey Harrison 			break;
165107a0367SIngo Molnar 
166107a0367SIngo Molnar 		instr++;
167107a0367SIngo Molnar 
168107a0367SIngo Molnar 		if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
169c61e211dSHarvey Harrison 			break;
170c61e211dSHarvey Harrison 	}
171c61e211dSHarvey Harrison 	return prefetch;
172c61e211dSHarvey Harrison }
173c61e211dSHarvey Harrison 
174019132ffSDave Hansen /*
175019132ffSDave Hansen  * A protection key fault means that the PKRU value did not allow
176019132ffSDave Hansen  * access to some PTE.  Userspace can figure out what PKRU was
177019132ffSDave Hansen  * from the XSAVE state, and this function fills out a field in
178019132ffSDave Hansen  * siginfo so userspace can discover which protection key was set
179019132ffSDave Hansen  * on the PTE.
180019132ffSDave Hansen  *
181019132ffSDave Hansen  * If we get here, we know that the hardware signaled a PF_PK
182019132ffSDave Hansen  * fault and that there was a VMA once we got in the fault
183019132ffSDave Hansen  * handler.  It does *not* guarantee that the VMA we find here
184019132ffSDave Hansen  * was the one that we faulted on.
185019132ffSDave Hansen  *
186019132ffSDave Hansen  * 1. T1   : mprotect_key(foo, PAGE_SIZE, pkey=4);
187019132ffSDave Hansen  * 2. T1   : set PKRU to deny access to pkey=4, touches page
188019132ffSDave Hansen  * 3. T1   : faults...
189019132ffSDave Hansen  * 4.    T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
190019132ffSDave Hansen  * 5. T1   : enters fault handler, takes mmap_sem, etc...
191019132ffSDave Hansen  * 6. T1   : reaches here, sees vma_pkey(vma)=5, when we really
192019132ffSDave Hansen  *	     faulted on a pte with its pkey=4.
193019132ffSDave Hansen  */
194019132ffSDave Hansen static void fill_sig_info_pkey(int si_code, siginfo_t *info,
195019132ffSDave Hansen 		struct vm_area_struct *vma)
196019132ffSDave Hansen {
197019132ffSDave Hansen 	/* This is effectively an #ifdef */
198019132ffSDave Hansen 	if (!boot_cpu_has(X86_FEATURE_OSPKE))
199019132ffSDave Hansen 		return;
200019132ffSDave Hansen 
201019132ffSDave Hansen 	/* Fault not from Protection Keys: nothing to do */
202019132ffSDave Hansen 	if (si_code != SEGV_PKUERR)
203019132ffSDave Hansen 		return;
204019132ffSDave Hansen 	/*
205019132ffSDave Hansen 	 * force_sig_info_fault() is called from a number of
206019132ffSDave Hansen 	 * contexts, some of which have a VMA and some of which
207019132ffSDave Hansen 	 * do not.  The PF_PK handing happens after we have a
208019132ffSDave Hansen 	 * valid VMA, so we should never reach this without a
209019132ffSDave Hansen 	 * valid VMA.
210019132ffSDave Hansen 	 */
211019132ffSDave Hansen 	if (!vma) {
212019132ffSDave Hansen 		WARN_ONCE(1, "PKU fault with no VMA passed in");
213019132ffSDave Hansen 		info->si_pkey = 0;
214019132ffSDave Hansen 		return;
215019132ffSDave Hansen 	}
216019132ffSDave Hansen 	/*
217019132ffSDave Hansen 	 * si_pkey should be thought of as a strong hint, but not
218019132ffSDave Hansen 	 * absolutely guranteed to be 100% accurate because of
219019132ffSDave Hansen 	 * the race explained above.
220019132ffSDave Hansen 	 */
221019132ffSDave Hansen 	info->si_pkey = vma_pkey(vma);
222019132ffSDave Hansen }
223019132ffSDave Hansen 
2242d4a7167SIngo Molnar static void
2252d4a7167SIngo Molnar force_sig_info_fault(int si_signo, int si_code, unsigned long address,
2267b2d0dbaSDave Hansen 		     struct task_struct *tsk, struct vm_area_struct *vma,
2277b2d0dbaSDave Hansen 		     int fault)
228c61e211dSHarvey Harrison {
229f672b49bSAndi Kleen 	unsigned lsb = 0;
230c61e211dSHarvey Harrison 	siginfo_t info;
231c61e211dSHarvey Harrison 
232c61e211dSHarvey Harrison 	info.si_signo	= si_signo;
233c61e211dSHarvey Harrison 	info.si_errno	= 0;
234c61e211dSHarvey Harrison 	info.si_code	= si_code;
235c61e211dSHarvey Harrison 	info.si_addr	= (void __user *)address;
236f672b49bSAndi Kleen 	if (fault & VM_FAULT_HWPOISON_LARGE)
237f672b49bSAndi Kleen 		lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
238f672b49bSAndi Kleen 	if (fault & VM_FAULT_HWPOISON)
239f672b49bSAndi Kleen 		lsb = PAGE_SHIFT;
240f672b49bSAndi Kleen 	info.si_addr_lsb = lsb;
2412d4a7167SIngo Molnar 
242019132ffSDave Hansen 	fill_sig_info_pkey(si_code, &info, vma);
243019132ffSDave Hansen 
244c61e211dSHarvey Harrison 	force_sig_info(si_signo, &info, tsk);
245c61e211dSHarvey Harrison }
246c61e211dSHarvey Harrison 
247f2f13a85SIngo Molnar DEFINE_SPINLOCK(pgd_lock);
248f2f13a85SIngo Molnar LIST_HEAD(pgd_list);
2492d4a7167SIngo Molnar 
250f2f13a85SIngo Molnar #ifdef CONFIG_X86_32
251f2f13a85SIngo Molnar static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
252f2f13a85SIngo Molnar {
253f2f13a85SIngo Molnar 	unsigned index = pgd_index(address);
254f2f13a85SIngo Molnar 	pgd_t *pgd_k;
255f2f13a85SIngo Molnar 	pud_t *pud, *pud_k;
256f2f13a85SIngo Molnar 	pmd_t *pmd, *pmd_k;
257f2f13a85SIngo Molnar 
258f2f13a85SIngo Molnar 	pgd += index;
259f2f13a85SIngo Molnar 	pgd_k = init_mm.pgd + index;
260f2f13a85SIngo Molnar 
261f2f13a85SIngo Molnar 	if (!pgd_present(*pgd_k))
262f2f13a85SIngo Molnar 		return NULL;
263f2f13a85SIngo Molnar 
264f2f13a85SIngo Molnar 	/*
265f2f13a85SIngo Molnar 	 * set_pgd(pgd, *pgd_k); here would be useless on PAE
266f2f13a85SIngo Molnar 	 * and redundant with the set_pmd() on non-PAE. As would
267f2f13a85SIngo Molnar 	 * set_pud.
268f2f13a85SIngo Molnar 	 */
269f2f13a85SIngo Molnar 	pud = pud_offset(pgd, address);
270f2f13a85SIngo Molnar 	pud_k = pud_offset(pgd_k, address);
271f2f13a85SIngo Molnar 	if (!pud_present(*pud_k))
272f2f13a85SIngo Molnar 		return NULL;
273f2f13a85SIngo Molnar 
274f2f13a85SIngo Molnar 	pmd = pmd_offset(pud, address);
275f2f13a85SIngo Molnar 	pmd_k = pmd_offset(pud_k, address);
276f2f13a85SIngo Molnar 	if (!pmd_present(*pmd_k))
277f2f13a85SIngo Molnar 		return NULL;
278f2f13a85SIngo Molnar 
279b8bcfe99SJeremy Fitzhardinge 	if (!pmd_present(*pmd))
280f2f13a85SIngo Molnar 		set_pmd(pmd, *pmd_k);
281b8bcfe99SJeremy Fitzhardinge 	else
282f2f13a85SIngo Molnar 		BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
283f2f13a85SIngo Molnar 
284f2f13a85SIngo Molnar 	return pmd_k;
285f2f13a85SIngo Molnar }
286f2f13a85SIngo Molnar 
287f2f13a85SIngo Molnar void vmalloc_sync_all(void)
288f2f13a85SIngo Molnar {
289f2f13a85SIngo Molnar 	unsigned long address;
290f2f13a85SIngo Molnar 
291f2f13a85SIngo Molnar 	if (SHARED_KERNEL_PMD)
292f2f13a85SIngo Molnar 		return;
293f2f13a85SIngo Molnar 
294f2f13a85SIngo Molnar 	for (address = VMALLOC_START & PMD_MASK;
295dc4fac84SAndy Lutomirski 	     address >= TASK_SIZE_MAX && address < FIXADDR_TOP;
296f2f13a85SIngo Molnar 	     address += PMD_SIZE) {
297f2f13a85SIngo Molnar 		struct page *page;
298f2f13a85SIngo Molnar 
299a79e53d8SAndrea Arcangeli 		spin_lock(&pgd_lock);
300f2f13a85SIngo Molnar 		list_for_each_entry(page, &pgd_list, lru) {
301617d34d9SJeremy Fitzhardinge 			spinlock_t *pgt_lock;
302f01f7c56SBorislav Petkov 			pmd_t *ret;
303617d34d9SJeremy Fitzhardinge 
304a79e53d8SAndrea Arcangeli 			/* the pgt_lock only for Xen */
305617d34d9SJeremy Fitzhardinge 			pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
306617d34d9SJeremy Fitzhardinge 
307617d34d9SJeremy Fitzhardinge 			spin_lock(pgt_lock);
308617d34d9SJeremy Fitzhardinge 			ret = vmalloc_sync_one(page_address(page), address);
309617d34d9SJeremy Fitzhardinge 			spin_unlock(pgt_lock);
310617d34d9SJeremy Fitzhardinge 
311617d34d9SJeremy Fitzhardinge 			if (!ret)
312f2f13a85SIngo Molnar 				break;
313f2f13a85SIngo Molnar 		}
314a79e53d8SAndrea Arcangeli 		spin_unlock(&pgd_lock);
315f2f13a85SIngo Molnar 	}
316f2f13a85SIngo Molnar }
317f2f13a85SIngo Molnar 
318f2f13a85SIngo Molnar /*
319f2f13a85SIngo Molnar  * 32-bit:
320f2f13a85SIngo Molnar  *
321f2f13a85SIngo Molnar  *   Handle a fault on the vmalloc or module mapping area
322f2f13a85SIngo Molnar  */
3239326638cSMasami Hiramatsu static noinline int vmalloc_fault(unsigned long address)
324f2f13a85SIngo Molnar {
325f2f13a85SIngo Molnar 	unsigned long pgd_paddr;
326f2f13a85SIngo Molnar 	pmd_t *pmd_k;
327f2f13a85SIngo Molnar 	pte_t *pte_k;
328f2f13a85SIngo Molnar 
329f2f13a85SIngo Molnar 	/* Make sure we are in vmalloc area: */
330f2f13a85SIngo Molnar 	if (!(address >= VMALLOC_START && address < VMALLOC_END))
331f2f13a85SIngo Molnar 		return -1;
332f2f13a85SIngo Molnar 
333ebc8827fSFrederic Weisbecker 	WARN_ON_ONCE(in_nmi());
334ebc8827fSFrederic Weisbecker 
335f2f13a85SIngo Molnar 	/*
336f2f13a85SIngo Molnar 	 * Synchronize this task's top level page-table
337f2f13a85SIngo Molnar 	 * with the 'reference' page table.
338f2f13a85SIngo Molnar 	 *
339f2f13a85SIngo Molnar 	 * Do _not_ use "current" here. We might be inside
340f2f13a85SIngo Molnar 	 * an interrupt in the middle of a task switch..
341f2f13a85SIngo Molnar 	 */
342f2f13a85SIngo Molnar 	pgd_paddr = read_cr3();
343f2f13a85SIngo Molnar 	pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
344f2f13a85SIngo Molnar 	if (!pmd_k)
345f2f13a85SIngo Molnar 		return -1;
346f2f13a85SIngo Molnar 
347f4eafd8bSToshi Kani 	if (pmd_huge(*pmd_k))
348f4eafd8bSToshi Kani 		return 0;
349f4eafd8bSToshi Kani 
350f2f13a85SIngo Molnar 	pte_k = pte_offset_kernel(pmd_k, address);
351f2f13a85SIngo Molnar 	if (!pte_present(*pte_k))
352f2f13a85SIngo Molnar 		return -1;
353f2f13a85SIngo Molnar 
354f2f13a85SIngo Molnar 	return 0;
355f2f13a85SIngo Molnar }
3569326638cSMasami Hiramatsu NOKPROBE_SYMBOL(vmalloc_fault);
357f2f13a85SIngo Molnar 
358f2f13a85SIngo Molnar /*
359f2f13a85SIngo Molnar  * Did it hit the DOS screen memory VA from vm86 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 {
3659fda6a06SBrian Gerst #ifdef CONFIG_VM86
366f2f13a85SIngo Molnar 	unsigned long bit;
367f2f13a85SIngo Molnar 
3689fda6a06SBrian Gerst 	if (!v8086_mode(regs) || !tsk->thread.vm86)
369f2f13a85SIngo Molnar 		return;
370f2f13a85SIngo Molnar 
371f2f13a85SIngo Molnar 	bit = (address - 0xA0000) >> PAGE_SHIFT;
372f2f13a85SIngo Molnar 	if (bit < 32)
3739fda6a06SBrian Gerst 		tsk->thread.vm86->screen_bitmap |= 1 << bit;
3749fda6a06SBrian Gerst #endif
375f2f13a85SIngo Molnar }
376c61e211dSHarvey Harrison 
377087975b0SAkinobu Mita static bool low_pfn(unsigned long pfn)
378087975b0SAkinobu Mita {
379087975b0SAkinobu Mita 	return pfn < max_low_pfn;
380087975b0SAkinobu Mita }
381087975b0SAkinobu Mita 
382cae30f82SAdrian Bunk static void dump_pagetable(unsigned long address)
383c61e211dSHarvey Harrison {
384087975b0SAkinobu Mita 	pgd_t *base = __va(read_cr3());
385087975b0SAkinobu Mita 	pgd_t *pgd = &base[pgd_index(address)];
386087975b0SAkinobu Mita 	pmd_t *pmd;
387087975b0SAkinobu Mita 	pte_t *pte;
3882d4a7167SIngo Molnar 
389c61e211dSHarvey Harrison #ifdef CONFIG_X86_PAE
390087975b0SAkinobu Mita 	printk("*pdpt = %016Lx ", pgd_val(*pgd));
391087975b0SAkinobu Mita 	if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
392087975b0SAkinobu Mita 		goto out;
393c61e211dSHarvey Harrison #endif
394087975b0SAkinobu Mita 	pmd = pmd_offset(pud_offset(pgd, address), address);
395087975b0SAkinobu Mita 	printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
396c61e211dSHarvey Harrison 
397c61e211dSHarvey Harrison 	/*
398c61e211dSHarvey Harrison 	 * We must not directly access the pte in the highpte
399c61e211dSHarvey Harrison 	 * case if the page table is located in highmem.
400c61e211dSHarvey Harrison 	 * And let's rather not kmap-atomic the pte, just in case
4012d4a7167SIngo Molnar 	 * it's allocated already:
402c61e211dSHarvey Harrison 	 */
403087975b0SAkinobu Mita 	if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
404087975b0SAkinobu Mita 		goto out;
4052d4a7167SIngo Molnar 
406087975b0SAkinobu Mita 	pte = pte_offset_kernel(pmd, address);
407087975b0SAkinobu Mita 	printk("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
408087975b0SAkinobu Mita out:
409c61e211dSHarvey Harrison 	printk("\n");
410f2f13a85SIngo Molnar }
411f2f13a85SIngo Molnar 
412f2f13a85SIngo Molnar #else /* CONFIG_X86_64: */
413f2f13a85SIngo Molnar 
414f2f13a85SIngo Molnar void vmalloc_sync_all(void)
415f2f13a85SIngo Molnar {
416*5372e155SKirill A. Shutemov 	sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END);
417f2f13a85SIngo Molnar }
418f2f13a85SIngo Molnar 
419f2f13a85SIngo Molnar /*
420f2f13a85SIngo Molnar  * 64-bit:
421f2f13a85SIngo Molnar  *
422f2f13a85SIngo Molnar  *   Handle a fault on the vmalloc area
423f2f13a85SIngo Molnar  */
4249326638cSMasami Hiramatsu static noinline int vmalloc_fault(unsigned long address)
425f2f13a85SIngo Molnar {
426f2f13a85SIngo Molnar 	pgd_t *pgd, *pgd_ref;
427f2f13a85SIngo Molnar 	pud_t *pud, *pud_ref;
428f2f13a85SIngo Molnar 	pmd_t *pmd, *pmd_ref;
429f2f13a85SIngo Molnar 	pte_t *pte, *pte_ref;
430f2f13a85SIngo Molnar 
431f2f13a85SIngo Molnar 	/* Make sure we are in vmalloc area: */
432f2f13a85SIngo Molnar 	if (!(address >= VMALLOC_START && address < VMALLOC_END))
433f2f13a85SIngo Molnar 		return -1;
434f2f13a85SIngo Molnar 
435ebc8827fSFrederic Weisbecker 	WARN_ON_ONCE(in_nmi());
436ebc8827fSFrederic Weisbecker 
437f2f13a85SIngo Molnar 	/*
438f2f13a85SIngo Molnar 	 * Copy kernel mappings over when needed. This can also
439f2f13a85SIngo Molnar 	 * happen within a race in page table update. In the later
440f2f13a85SIngo Molnar 	 * case just flush:
441f2f13a85SIngo Molnar 	 */
44246aea387SAndy Lutomirski 	pgd = (pgd_t *)__va(read_cr3()) + pgd_index(address);
443f2f13a85SIngo Molnar 	pgd_ref = pgd_offset_k(address);
444f2f13a85SIngo Molnar 	if (pgd_none(*pgd_ref))
445f2f13a85SIngo Molnar 		return -1;
446f2f13a85SIngo Molnar 
4471160c277SSamu Kallio 	if (pgd_none(*pgd)) {
448f2f13a85SIngo Molnar 		set_pgd(pgd, *pgd_ref);
4491160c277SSamu Kallio 		arch_flush_lazy_mmu_mode();
4501160c277SSamu Kallio 	} else {
451f2f13a85SIngo Molnar 		BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
4521160c277SSamu Kallio 	}
453f2f13a85SIngo Molnar 
454f2f13a85SIngo Molnar 	/*
455f2f13a85SIngo Molnar 	 * Below here mismatches are bugs because these lower tables
456f2f13a85SIngo Molnar 	 * are shared:
457f2f13a85SIngo Molnar 	 */
458f2f13a85SIngo Molnar 
459f2f13a85SIngo Molnar 	pud = pud_offset(pgd, address);
460f2f13a85SIngo Molnar 	pud_ref = pud_offset(pgd_ref, address);
461f2f13a85SIngo Molnar 	if (pud_none(*pud_ref))
462f2f13a85SIngo Molnar 		return -1;
463f2f13a85SIngo Molnar 
464f4eafd8bSToshi Kani 	if (pud_none(*pud) || pud_pfn(*pud) != pud_pfn(*pud_ref))
465f2f13a85SIngo Molnar 		BUG();
466f2f13a85SIngo Molnar 
467f4eafd8bSToshi Kani 	if (pud_huge(*pud))
468f4eafd8bSToshi Kani 		return 0;
469f4eafd8bSToshi Kani 
470f2f13a85SIngo Molnar 	pmd = pmd_offset(pud, address);
471f2f13a85SIngo Molnar 	pmd_ref = pmd_offset(pud_ref, address);
472f2f13a85SIngo Molnar 	if (pmd_none(*pmd_ref))
473f2f13a85SIngo Molnar 		return -1;
474f2f13a85SIngo Molnar 
475f4eafd8bSToshi Kani 	if (pmd_none(*pmd) || pmd_pfn(*pmd) != pmd_pfn(*pmd_ref))
476f2f13a85SIngo Molnar 		BUG();
477f2f13a85SIngo Molnar 
478f4eafd8bSToshi Kani 	if (pmd_huge(*pmd))
479f4eafd8bSToshi Kani 		return 0;
480f4eafd8bSToshi Kani 
481f2f13a85SIngo Molnar 	pte_ref = pte_offset_kernel(pmd_ref, address);
482f2f13a85SIngo Molnar 	if (!pte_present(*pte_ref))
483f2f13a85SIngo Molnar 		return -1;
484f2f13a85SIngo Molnar 
485f2f13a85SIngo Molnar 	pte = pte_offset_kernel(pmd, address);
486f2f13a85SIngo Molnar 
487f2f13a85SIngo Molnar 	/*
488f2f13a85SIngo Molnar 	 * Don't use pte_page here, because the mappings can point
489f2f13a85SIngo Molnar 	 * outside mem_map, and the NUMA hash lookup cannot handle
490f2f13a85SIngo Molnar 	 * that:
491f2f13a85SIngo Molnar 	 */
492f2f13a85SIngo Molnar 	if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
493f2f13a85SIngo Molnar 		BUG();
494f2f13a85SIngo Molnar 
495f2f13a85SIngo Molnar 	return 0;
496f2f13a85SIngo Molnar }
4979326638cSMasami Hiramatsu NOKPROBE_SYMBOL(vmalloc_fault);
498f2f13a85SIngo Molnar 
499e05139f2SJan Beulich #ifdef CONFIG_CPU_SUP_AMD
500f2f13a85SIngo Molnar static const char errata93_warning[] =
501ad361c98SJoe Perches KERN_ERR
502ad361c98SJoe Perches "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
503ad361c98SJoe Perches "******* Working around it, but it may cause SEGVs or burn power.\n"
504ad361c98SJoe Perches "******* Please consider a BIOS update.\n"
505ad361c98SJoe Perches "******* Disabling USB legacy in the BIOS may also help.\n";
506e05139f2SJan Beulich #endif
507f2f13a85SIngo Molnar 
508f2f13a85SIngo Molnar /*
509f2f13a85SIngo Molnar  * No vm86 mode in 64-bit mode:
510f2f13a85SIngo Molnar  */
511f2f13a85SIngo Molnar static inline void
512f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address,
513f2f13a85SIngo Molnar 		 struct task_struct *tsk)
514f2f13a85SIngo Molnar {
515f2f13a85SIngo Molnar }
516f2f13a85SIngo Molnar 
517f2f13a85SIngo Molnar static int bad_address(void *p)
518f2f13a85SIngo Molnar {
519f2f13a85SIngo Molnar 	unsigned long dummy;
520f2f13a85SIngo Molnar 
521f2f13a85SIngo Molnar 	return probe_kernel_address((unsigned long *)p, dummy);
522f2f13a85SIngo Molnar }
523f2f13a85SIngo Molnar 
524f2f13a85SIngo Molnar static void dump_pagetable(unsigned long address)
525f2f13a85SIngo Molnar {
526087975b0SAkinobu Mita 	pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
527087975b0SAkinobu Mita 	pgd_t *pgd = base + pgd_index(address);
528c61e211dSHarvey Harrison 	pud_t *pud;
529c61e211dSHarvey Harrison 	pmd_t *pmd;
530c61e211dSHarvey Harrison 	pte_t *pte;
531c61e211dSHarvey Harrison 
5322d4a7167SIngo Molnar 	if (bad_address(pgd))
5332d4a7167SIngo Molnar 		goto bad;
5342d4a7167SIngo Molnar 
535c61e211dSHarvey Harrison 	printk("PGD %lx ", pgd_val(*pgd));
5362d4a7167SIngo Molnar 
5372d4a7167SIngo Molnar 	if (!pgd_present(*pgd))
5382d4a7167SIngo Molnar 		goto out;
539c61e211dSHarvey Harrison 
540c61e211dSHarvey Harrison 	pud = pud_offset(pgd, address);
5412d4a7167SIngo Molnar 	if (bad_address(pud))
5422d4a7167SIngo Molnar 		goto bad;
5432d4a7167SIngo Molnar 
544c61e211dSHarvey Harrison 	printk("PUD %lx ", pud_val(*pud));
545b5360222SAndi Kleen 	if (!pud_present(*pud) || pud_large(*pud))
5462d4a7167SIngo Molnar 		goto out;
547c61e211dSHarvey Harrison 
548c61e211dSHarvey Harrison 	pmd = pmd_offset(pud, address);
5492d4a7167SIngo Molnar 	if (bad_address(pmd))
5502d4a7167SIngo Molnar 		goto bad;
5512d4a7167SIngo Molnar 
552c61e211dSHarvey Harrison 	printk("PMD %lx ", pmd_val(*pmd));
5532d4a7167SIngo Molnar 	if (!pmd_present(*pmd) || pmd_large(*pmd))
5542d4a7167SIngo Molnar 		goto out;
555c61e211dSHarvey Harrison 
556c61e211dSHarvey Harrison 	pte = pte_offset_kernel(pmd, address);
5572d4a7167SIngo Molnar 	if (bad_address(pte))
5582d4a7167SIngo Molnar 		goto bad;
5592d4a7167SIngo Molnar 
560c61e211dSHarvey Harrison 	printk("PTE %lx", pte_val(*pte));
5612d4a7167SIngo Molnar out:
562c61e211dSHarvey Harrison 	printk("\n");
563c61e211dSHarvey Harrison 	return;
564c61e211dSHarvey Harrison bad:
565c61e211dSHarvey Harrison 	printk("BAD\n");
566c61e211dSHarvey Harrison }
567c61e211dSHarvey Harrison 
568f2f13a85SIngo Molnar #endif /* CONFIG_X86_64 */
569c61e211dSHarvey Harrison 
5702d4a7167SIngo Molnar /*
5712d4a7167SIngo Molnar  * Workaround for K8 erratum #93 & buggy BIOS.
5722d4a7167SIngo Molnar  *
5732d4a7167SIngo Molnar  * BIOS SMM functions are required to use a specific workaround
5742d4a7167SIngo Molnar  * to avoid corruption of the 64bit RIP register on C stepping K8.
5752d4a7167SIngo Molnar  *
5762d4a7167SIngo Molnar  * A lot of BIOS that didn't get tested properly miss this.
5772d4a7167SIngo Molnar  *
5782d4a7167SIngo Molnar  * The OS sees this as a page fault with the upper 32bits of RIP cleared.
5792d4a7167SIngo Molnar  * Try to work around it here.
5802d4a7167SIngo Molnar  *
5812d4a7167SIngo Molnar  * Note we only handle faults in kernel here.
5822d4a7167SIngo Molnar  * Does nothing on 32-bit.
583c61e211dSHarvey Harrison  */
584c61e211dSHarvey Harrison static int is_errata93(struct pt_regs *regs, unsigned long address)
585c61e211dSHarvey Harrison {
586e05139f2SJan Beulich #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
587e05139f2SJan Beulich 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
588e05139f2SJan Beulich 	    || boot_cpu_data.x86 != 0xf)
589e05139f2SJan Beulich 		return 0;
590e05139f2SJan Beulich 
591c61e211dSHarvey Harrison 	if (address != regs->ip)
592c61e211dSHarvey Harrison 		return 0;
5932d4a7167SIngo Molnar 
594c61e211dSHarvey Harrison 	if ((address >> 32) != 0)
595c61e211dSHarvey Harrison 		return 0;
5962d4a7167SIngo Molnar 
597c61e211dSHarvey Harrison 	address |= 0xffffffffUL << 32;
598c61e211dSHarvey Harrison 	if ((address >= (u64)_stext && address <= (u64)_etext) ||
599c61e211dSHarvey Harrison 	    (address >= MODULES_VADDR && address <= MODULES_END)) {
600a454ab31SIngo Molnar 		printk_once(errata93_warning);
601c61e211dSHarvey Harrison 		regs->ip = address;
602c61e211dSHarvey Harrison 		return 1;
603c61e211dSHarvey Harrison 	}
604c61e211dSHarvey Harrison #endif
605c61e211dSHarvey Harrison 	return 0;
606c61e211dSHarvey Harrison }
607c61e211dSHarvey Harrison 
608c61e211dSHarvey Harrison /*
6092d4a7167SIngo Molnar  * Work around K8 erratum #100 K8 in compat mode occasionally jumps
6102d4a7167SIngo Molnar  * to illegal addresses >4GB.
6112d4a7167SIngo Molnar  *
6122d4a7167SIngo Molnar  * We catch this in the page fault handler because these addresses
6132d4a7167SIngo Molnar  * are not reachable. Just detect this case and return.  Any code
614c61e211dSHarvey Harrison  * segment in LDT is compatibility mode.
615c61e211dSHarvey Harrison  */
616c61e211dSHarvey Harrison static int is_errata100(struct pt_regs *regs, unsigned long address)
617c61e211dSHarvey Harrison {
618c61e211dSHarvey Harrison #ifdef CONFIG_X86_64
6192d4a7167SIngo Molnar 	if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
620c61e211dSHarvey Harrison 		return 1;
621c61e211dSHarvey Harrison #endif
622c61e211dSHarvey Harrison 	return 0;
623c61e211dSHarvey Harrison }
624c61e211dSHarvey Harrison 
625c61e211dSHarvey Harrison static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
626c61e211dSHarvey Harrison {
627c61e211dSHarvey Harrison #ifdef CONFIG_X86_F00F_BUG
628c61e211dSHarvey Harrison 	unsigned long nr;
6292d4a7167SIngo Molnar 
630c61e211dSHarvey Harrison 	/*
6312d4a7167SIngo Molnar 	 * Pentium F0 0F C7 C8 bug workaround:
632c61e211dSHarvey Harrison 	 */
633e2604b49SBorislav Petkov 	if (boot_cpu_has_bug(X86_BUG_F00F)) {
634c61e211dSHarvey Harrison 		nr = (address - idt_descr.address) >> 3;
635c61e211dSHarvey Harrison 
636c61e211dSHarvey Harrison 		if (nr == 6) {
637c61e211dSHarvey Harrison 			do_invalid_op(regs, 0);
638c61e211dSHarvey Harrison 			return 1;
639c61e211dSHarvey Harrison 		}
640c61e211dSHarvey Harrison 	}
641c61e211dSHarvey Harrison #endif
642c61e211dSHarvey Harrison 	return 0;
643c61e211dSHarvey Harrison }
644c61e211dSHarvey Harrison 
6458f766149SIngo Molnar static const char nx_warning[] = KERN_CRIT
6468f766149SIngo Molnar "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
647eff50c34SJiri Kosina static const char smep_warning[] = KERN_CRIT
648eff50c34SJiri Kosina "unable to execute userspace code (SMEP?) (uid: %d)\n";
6498f766149SIngo Molnar 
6502d4a7167SIngo Molnar static void
6512d4a7167SIngo Molnar show_fault_oops(struct pt_regs *regs, unsigned long error_code,
652c61e211dSHarvey Harrison 		unsigned long address)
653c61e211dSHarvey Harrison {
654c61e211dSHarvey Harrison 	if (!oops_may_print())
655c61e211dSHarvey Harrison 		return;
656c61e211dSHarvey Harrison 
657c61e211dSHarvey Harrison 	if (error_code & PF_INSTR) {
65893809be8SHarvey Harrison 		unsigned int level;
659426e34ccSMatt Fleming 		pgd_t *pgd;
660426e34ccSMatt Fleming 		pte_t *pte;
6612d4a7167SIngo Molnar 
662426e34ccSMatt Fleming 		pgd = __va(read_cr3() & PHYSICAL_PAGE_MASK);
663426e34ccSMatt Fleming 		pgd += pgd_index(address);
664426e34ccSMatt Fleming 
665426e34ccSMatt Fleming 		pte = lookup_address_in_pgd(pgd, address, &level);
666c61e211dSHarvey Harrison 
6678f766149SIngo Molnar 		if (pte && pte_present(*pte) && !pte_exec(*pte))
668078de5f7SEric W. Biederman 			printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
669eff50c34SJiri Kosina 		if (pte && pte_present(*pte) && pte_exec(*pte) &&
670eff50c34SJiri Kosina 				(pgd_flags(*pgd) & _PAGE_USER) &&
6711e02ce4cSAndy Lutomirski 				(__read_cr4() & X86_CR4_SMEP))
672eff50c34SJiri Kosina 			printk(smep_warning, from_kuid(&init_user_ns, current_uid()));
673c61e211dSHarvey Harrison 	}
674fd40d6e3SHarvey Harrison 
675c61e211dSHarvey Harrison 	printk(KERN_ALERT "BUG: unable to handle kernel ");
676c61e211dSHarvey Harrison 	if (address < PAGE_SIZE)
677c61e211dSHarvey Harrison 		printk(KERN_CONT "NULL pointer dereference");
678c61e211dSHarvey Harrison 	else
679c61e211dSHarvey Harrison 		printk(KERN_CONT "paging request");
6802d4a7167SIngo Molnar 
681f294a8ceSVegard Nossum 	printk(KERN_CONT " at %p\n", (void *) address);
682bb5e5ce5SJosh Poimboeuf 	printk(KERN_ALERT "IP: %pS\n", (void *)regs->ip);
6832d4a7167SIngo Molnar 
684c61e211dSHarvey Harrison 	dump_pagetable(address);
685c61e211dSHarvey Harrison }
686c61e211dSHarvey Harrison 
6872d4a7167SIngo Molnar static noinline void
6882d4a7167SIngo Molnar pgtable_bad(struct pt_regs *regs, unsigned long error_code,
6892d4a7167SIngo Molnar 	    unsigned long address)
690c61e211dSHarvey Harrison {
6912d4a7167SIngo Molnar 	struct task_struct *tsk;
6922d4a7167SIngo Molnar 	unsigned long flags;
6932d4a7167SIngo Molnar 	int sig;
6942d4a7167SIngo Molnar 
6952d4a7167SIngo Molnar 	flags = oops_begin();
6962d4a7167SIngo Molnar 	tsk = current;
6972d4a7167SIngo Molnar 	sig = SIGKILL;
698c61e211dSHarvey Harrison 
699c61e211dSHarvey Harrison 	printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
70092181f19SNick Piggin 	       tsk->comm, address);
701c61e211dSHarvey Harrison 	dump_pagetable(address);
7022d4a7167SIngo Molnar 
703c61e211dSHarvey Harrison 	tsk->thread.cr2		= address;
70451e7dc70SSrikar Dronamraju 	tsk->thread.trap_nr	= X86_TRAP_PF;
705c61e211dSHarvey Harrison 	tsk->thread.error_code	= error_code;
7062d4a7167SIngo Molnar 
707c61e211dSHarvey Harrison 	if (__die("Bad pagetable", regs, error_code))
708874d93d1SAlexander van Heukelum 		sig = 0;
7092d4a7167SIngo Molnar 
710874d93d1SAlexander van Heukelum 	oops_end(flags, regs, sig);
711c61e211dSHarvey Harrison }
712c61e211dSHarvey Harrison 
7132d4a7167SIngo Molnar static noinline void
7142d4a7167SIngo Molnar no_context(struct pt_regs *regs, unsigned long error_code,
7154fc34901SAndy Lutomirski 	   unsigned long address, int signal, int si_code)
71692181f19SNick Piggin {
71792181f19SNick Piggin 	struct task_struct *tsk = current;
71892181f19SNick Piggin 	unsigned long flags;
71992181f19SNick Piggin 	int sig;
7207b2d0dbaSDave Hansen 	/* No context means no VMA to pass down */
7217b2d0dbaSDave Hansen 	struct vm_area_struct *vma = NULL;
72292181f19SNick Piggin 
72392181f19SNick Piggin 	/* Are we prepared to handle this kernel fault? */
724548acf19STony Luck 	if (fixup_exception(regs, X86_TRAP_PF)) {
725c026b359SPeter Zijlstra 		/*
726c026b359SPeter Zijlstra 		 * Any interrupt that takes a fault gets the fixup. This makes
727c026b359SPeter Zijlstra 		 * the below recursive fault logic only apply to a faults from
728c026b359SPeter Zijlstra 		 * task context.
729c026b359SPeter Zijlstra 		 */
730c026b359SPeter Zijlstra 		if (in_interrupt())
731c026b359SPeter Zijlstra 			return;
732c026b359SPeter Zijlstra 
733c026b359SPeter Zijlstra 		/*
734c026b359SPeter Zijlstra 		 * Per the above we're !in_interrupt(), aka. task context.
735c026b359SPeter Zijlstra 		 *
736c026b359SPeter Zijlstra 		 * In this case we need to make sure we're not recursively
737c026b359SPeter Zijlstra 		 * faulting through the emulate_vsyscall() logic.
738c026b359SPeter Zijlstra 		 */
7392a53ccbcSIngo Molnar 		if (current->thread.sig_on_uaccess_err && signal) {
74051e7dc70SSrikar Dronamraju 			tsk->thread.trap_nr = X86_TRAP_PF;
7414fc34901SAndy Lutomirski 			tsk->thread.error_code = error_code | PF_USER;
7424fc34901SAndy Lutomirski 			tsk->thread.cr2 = address;
7434fc34901SAndy Lutomirski 
7444fc34901SAndy Lutomirski 			/* XXX: hwpoison faults will set the wrong code. */
7457b2d0dbaSDave Hansen 			force_sig_info_fault(signal, si_code, address,
7467b2d0dbaSDave Hansen 					     tsk, vma, 0);
7474fc34901SAndy Lutomirski 		}
748c026b359SPeter Zijlstra 
749c026b359SPeter Zijlstra 		/*
750c026b359SPeter Zijlstra 		 * Barring that, we can do the fixup and be happy.
751c026b359SPeter Zijlstra 		 */
75292181f19SNick Piggin 		return;
7534fc34901SAndy Lutomirski 	}
75492181f19SNick Piggin 
7556271cfdfSAndy Lutomirski #ifdef CONFIG_VMAP_STACK
7566271cfdfSAndy Lutomirski 	/*
7576271cfdfSAndy Lutomirski 	 * Stack overflow?  During boot, we can fault near the initial
7586271cfdfSAndy Lutomirski 	 * stack in the direct map, but that's not an overflow -- check
7596271cfdfSAndy Lutomirski 	 * that we're in vmalloc space to avoid this.
7606271cfdfSAndy Lutomirski 	 */
7616271cfdfSAndy Lutomirski 	if (is_vmalloc_addr((void *)address) &&
7626271cfdfSAndy Lutomirski 	    (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) ||
7636271cfdfSAndy Lutomirski 	     address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) {
7646271cfdfSAndy Lutomirski 		register void *__sp asm("rsp");
7656271cfdfSAndy Lutomirski 		unsigned long stack = this_cpu_read(orig_ist.ist[DOUBLEFAULT_STACK]) - sizeof(void *);
7666271cfdfSAndy Lutomirski 		/*
7676271cfdfSAndy Lutomirski 		 * We're likely to be running with very little stack space
7686271cfdfSAndy Lutomirski 		 * left.  It's plausible that we'd hit this condition but
7696271cfdfSAndy Lutomirski 		 * double-fault even before we get this far, in which case
7706271cfdfSAndy Lutomirski 		 * we're fine: the double-fault handler will deal with it.
7716271cfdfSAndy Lutomirski 		 *
7726271cfdfSAndy Lutomirski 		 * We don't want to make it all the way into the oops code
7736271cfdfSAndy Lutomirski 		 * and then double-fault, though, because we're likely to
7746271cfdfSAndy Lutomirski 		 * break the console driver and lose most of the stack dump.
7756271cfdfSAndy Lutomirski 		 */
7766271cfdfSAndy Lutomirski 		asm volatile ("movq %[stack], %%rsp\n\t"
7776271cfdfSAndy Lutomirski 			      "call handle_stack_overflow\n\t"
7786271cfdfSAndy Lutomirski 			      "1: jmp 1b"
7796271cfdfSAndy Lutomirski 			      : "+r" (__sp)
7806271cfdfSAndy Lutomirski 			      : "D" ("kernel stack overflow (page fault)"),
7816271cfdfSAndy Lutomirski 				"S" (regs), "d" (address),
7826271cfdfSAndy Lutomirski 				[stack] "rm" (stack));
7836271cfdfSAndy Lutomirski 		unreachable();
7846271cfdfSAndy Lutomirski 	}
7856271cfdfSAndy Lutomirski #endif
7866271cfdfSAndy Lutomirski 
78792181f19SNick Piggin 	/*
7882d4a7167SIngo Molnar 	 * 32-bit:
7892d4a7167SIngo Molnar 	 *
79092181f19SNick Piggin 	 *   Valid to do another page fault here, because if this fault
79192181f19SNick Piggin 	 *   had been triggered by is_prefetch fixup_exception would have
79292181f19SNick Piggin 	 *   handled it.
79392181f19SNick Piggin 	 *
7942d4a7167SIngo Molnar 	 * 64-bit:
7952d4a7167SIngo Molnar 	 *
79692181f19SNick Piggin 	 *   Hall of shame of CPU/BIOS bugs.
79792181f19SNick Piggin 	 */
79892181f19SNick Piggin 	if (is_prefetch(regs, error_code, address))
79992181f19SNick Piggin 		return;
80092181f19SNick Piggin 
80192181f19SNick Piggin 	if (is_errata93(regs, address))
80292181f19SNick Piggin 		return;
80392181f19SNick Piggin 
80492181f19SNick Piggin 	/*
80592181f19SNick Piggin 	 * Oops. The kernel tried to access some bad page. We'll have to
8062d4a7167SIngo Molnar 	 * terminate things with extreme prejudice:
80792181f19SNick Piggin 	 */
80892181f19SNick Piggin 	flags = oops_begin();
80992181f19SNick Piggin 
81092181f19SNick Piggin 	show_fault_oops(regs, error_code, address);
81192181f19SNick Piggin 
812a70857e4SAaron Tomlin 	if (task_stack_end_corrupted(tsk))
813b0f4c4b3SPrarit Bhargava 		printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
81419803078SIngo Molnar 
81592181f19SNick Piggin 	tsk->thread.cr2		= address;
81651e7dc70SSrikar Dronamraju 	tsk->thread.trap_nr	= X86_TRAP_PF;
81792181f19SNick Piggin 	tsk->thread.error_code	= error_code;
81892181f19SNick Piggin 
81992181f19SNick Piggin 	sig = SIGKILL;
82092181f19SNick Piggin 	if (__die("Oops", regs, error_code))
82192181f19SNick Piggin 		sig = 0;
8222d4a7167SIngo Molnar 
82392181f19SNick Piggin 	/* Executive summary in case the body of the oops scrolled away */
824b0f4c4b3SPrarit Bhargava 	printk(KERN_DEFAULT "CR2: %016lx\n", address);
8252d4a7167SIngo Molnar 
82692181f19SNick Piggin 	oops_end(flags, regs, sig);
82792181f19SNick Piggin }
82892181f19SNick Piggin 
8292d4a7167SIngo Molnar /*
8302d4a7167SIngo Molnar  * Print out info about fatal segfaults, if the show_unhandled_signals
8312d4a7167SIngo Molnar  * sysctl is set:
8322d4a7167SIngo Molnar  */
8332d4a7167SIngo Molnar static inline void
8342d4a7167SIngo Molnar show_signal_msg(struct pt_regs *regs, unsigned long error_code,
8352d4a7167SIngo Molnar 		unsigned long address, struct task_struct *tsk)
8362d4a7167SIngo Molnar {
8372d4a7167SIngo Molnar 	if (!unhandled_signal(tsk, SIGSEGV))
8382d4a7167SIngo Molnar 		return;
8392d4a7167SIngo Molnar 
8402d4a7167SIngo Molnar 	if (!printk_ratelimit())
8412d4a7167SIngo Molnar 		return;
8422d4a7167SIngo Molnar 
843a1a08d1cSRoland Dreier 	printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
8442d4a7167SIngo Molnar 		task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
8452d4a7167SIngo Molnar 		tsk->comm, task_pid_nr(tsk), address,
8462d4a7167SIngo Molnar 		(void *)regs->ip, (void *)regs->sp, error_code);
8472d4a7167SIngo Molnar 
8482d4a7167SIngo Molnar 	print_vma_addr(KERN_CONT " in ", regs->ip);
8492d4a7167SIngo Molnar 
8502d4a7167SIngo Molnar 	printk(KERN_CONT "\n");
8512d4a7167SIngo Molnar }
8522d4a7167SIngo Molnar 
8532d4a7167SIngo Molnar static void
8542d4a7167SIngo Molnar __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
8557b2d0dbaSDave Hansen 		       unsigned long address, struct vm_area_struct *vma,
8567b2d0dbaSDave Hansen 		       int si_code)
85792181f19SNick Piggin {
85892181f19SNick Piggin 	struct task_struct *tsk = current;
85992181f19SNick Piggin 
86092181f19SNick Piggin 	/* User mode accesses just cause a SIGSEGV */
86192181f19SNick Piggin 	if (error_code & PF_USER) {
86292181f19SNick Piggin 		/*
8632d4a7167SIngo Molnar 		 * It's possible to have interrupts off here:
86492181f19SNick Piggin 		 */
86592181f19SNick Piggin 		local_irq_enable();
86692181f19SNick Piggin 
86792181f19SNick Piggin 		/*
86892181f19SNick Piggin 		 * Valid to do another page fault here because this one came
8692d4a7167SIngo Molnar 		 * from user space:
87092181f19SNick Piggin 		 */
87192181f19SNick Piggin 		if (is_prefetch(regs, error_code, address))
87292181f19SNick Piggin 			return;
87392181f19SNick Piggin 
87492181f19SNick Piggin 		if (is_errata100(regs, address))
87592181f19SNick Piggin 			return;
87692181f19SNick Piggin 
8773ae36655SAndy Lutomirski #ifdef CONFIG_X86_64
8783ae36655SAndy Lutomirski 		/*
8793ae36655SAndy Lutomirski 		 * Instruction fetch faults in the vsyscall page might need
8803ae36655SAndy Lutomirski 		 * emulation.
8813ae36655SAndy Lutomirski 		 */
8823ae36655SAndy Lutomirski 		if (unlikely((error_code & PF_INSTR) &&
883f40c3300SAndy Lutomirski 			     ((address & ~0xfff) == VSYSCALL_ADDR))) {
8843ae36655SAndy Lutomirski 			if (emulate_vsyscall(regs, address))
8853ae36655SAndy Lutomirski 				return;
8863ae36655SAndy Lutomirski 		}
8873ae36655SAndy Lutomirski #endif
888dc4fac84SAndy Lutomirski 
889dc4fac84SAndy Lutomirski 		/*
890dc4fac84SAndy Lutomirski 		 * To avoid leaking information about the kernel page table
891dc4fac84SAndy Lutomirski 		 * layout, pretend that user-mode accesses to kernel addresses
892dc4fac84SAndy Lutomirski 		 * are always protection faults.
893dc4fac84SAndy Lutomirski 		 */
894dc4fac84SAndy Lutomirski 		if (address >= TASK_SIZE_MAX)
895e575a86fSKees Cook 			error_code |= PF_PROT;
8963ae36655SAndy Lutomirski 
897e575a86fSKees Cook 		if (likely(show_unhandled_signals))
8982d4a7167SIngo Molnar 			show_signal_msg(regs, error_code, address, tsk);
89992181f19SNick Piggin 
90092181f19SNick Piggin 		tsk->thread.cr2		= address;
901e575a86fSKees Cook 		tsk->thread.error_code	= error_code;
90251e7dc70SSrikar Dronamraju 		tsk->thread.trap_nr	= X86_TRAP_PF;
9032d4a7167SIngo Molnar 
9047b2d0dbaSDave Hansen 		force_sig_info_fault(SIGSEGV, si_code, address, tsk, vma, 0);
9052d4a7167SIngo Molnar 
90692181f19SNick Piggin 		return;
90792181f19SNick Piggin 	}
90892181f19SNick Piggin 
90992181f19SNick Piggin 	if (is_f00f_bug(regs, address))
91092181f19SNick Piggin 		return;
91192181f19SNick Piggin 
9124fc34901SAndy Lutomirski 	no_context(regs, error_code, address, SIGSEGV, si_code);
91392181f19SNick Piggin }
91492181f19SNick Piggin 
9152d4a7167SIngo Molnar static noinline void
9162d4a7167SIngo Molnar bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
9177b2d0dbaSDave Hansen 		     unsigned long address, struct vm_area_struct *vma)
91892181f19SNick Piggin {
9197b2d0dbaSDave Hansen 	__bad_area_nosemaphore(regs, error_code, address, vma, SEGV_MAPERR);
92092181f19SNick Piggin }
92192181f19SNick Piggin 
9222d4a7167SIngo Molnar static void
9232d4a7167SIngo Molnar __bad_area(struct pt_regs *regs, unsigned long error_code,
9247b2d0dbaSDave Hansen 	   unsigned long address,  struct vm_area_struct *vma, int si_code)
92592181f19SNick Piggin {
92692181f19SNick Piggin 	struct mm_struct *mm = current->mm;
92792181f19SNick Piggin 
92892181f19SNick Piggin 	/*
92992181f19SNick Piggin 	 * Something tried to access memory that isn't in our memory map..
93092181f19SNick Piggin 	 * Fix it, but check if it's kernel or user first..
93192181f19SNick Piggin 	 */
93292181f19SNick Piggin 	up_read(&mm->mmap_sem);
93392181f19SNick Piggin 
9347b2d0dbaSDave Hansen 	__bad_area_nosemaphore(regs, error_code, address, vma, si_code);
93592181f19SNick Piggin }
93692181f19SNick Piggin 
9372d4a7167SIngo Molnar static noinline void
9382d4a7167SIngo Molnar bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
93992181f19SNick Piggin {
9407b2d0dbaSDave Hansen 	__bad_area(regs, error_code, address, NULL, SEGV_MAPERR);
94192181f19SNick Piggin }
94292181f19SNick Piggin 
94333a709b2SDave Hansen static inline bool bad_area_access_from_pkeys(unsigned long error_code,
94433a709b2SDave Hansen 		struct vm_area_struct *vma)
94533a709b2SDave Hansen {
94607f146f5SDave Hansen 	/* This code is always called on the current mm */
94707f146f5SDave Hansen 	bool foreign = false;
94807f146f5SDave Hansen 
94933a709b2SDave Hansen 	if (!boot_cpu_has(X86_FEATURE_OSPKE))
95033a709b2SDave Hansen 		return false;
95133a709b2SDave Hansen 	if (error_code & PF_PK)
95233a709b2SDave Hansen 		return true;
95307f146f5SDave Hansen 	/* this checks permission keys on the VMA: */
954d61172b4SDave Hansen 	if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
955d61172b4SDave Hansen 				(error_code & PF_INSTR), foreign))
95607f146f5SDave Hansen 		return true;
95733a709b2SDave Hansen 	return false;
95892181f19SNick Piggin }
95992181f19SNick Piggin 
9602d4a7167SIngo Molnar static noinline void
9612d4a7167SIngo Molnar bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
9627b2d0dbaSDave Hansen 		      unsigned long address, struct vm_area_struct *vma)
96392181f19SNick Piggin {
964019132ffSDave Hansen 	/*
965019132ffSDave Hansen 	 * This OSPKE check is not strictly necessary at runtime.
966019132ffSDave Hansen 	 * But, doing it this way allows compiler optimizations
967019132ffSDave Hansen 	 * if pkeys are compiled out.
968019132ffSDave Hansen 	 */
96933a709b2SDave Hansen 	if (bad_area_access_from_pkeys(error_code, vma))
970019132ffSDave Hansen 		__bad_area(regs, error_code, address, vma, SEGV_PKUERR);
971019132ffSDave Hansen 	else
9727b2d0dbaSDave Hansen 		__bad_area(regs, error_code, address, vma, SEGV_ACCERR);
97392181f19SNick Piggin }
97492181f19SNick Piggin 
9752d4a7167SIngo Molnar static void
976a6e04aa9SAndi Kleen do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
9777b2d0dbaSDave Hansen 	  struct vm_area_struct *vma, unsigned int fault)
97892181f19SNick Piggin {
97992181f19SNick Piggin 	struct task_struct *tsk = current;
980a6e04aa9SAndi Kleen 	int code = BUS_ADRERR;
98192181f19SNick Piggin 
9822d4a7167SIngo Molnar 	/* Kernel mode? Handle exceptions or die: */
98396054569SLinus Torvalds 	if (!(error_code & PF_USER)) {
9844fc34901SAndy Lutomirski 		no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
98596054569SLinus Torvalds 		return;
98696054569SLinus Torvalds 	}
9872d4a7167SIngo Molnar 
988cd1b68f0SIngo Molnar 	/* User-space => ok to do another page fault: */
98992181f19SNick Piggin 	if (is_prefetch(regs, error_code, address))
99092181f19SNick Piggin 		return;
9912d4a7167SIngo Molnar 
99292181f19SNick Piggin 	tsk->thread.cr2		= address;
99392181f19SNick Piggin 	tsk->thread.error_code	= error_code;
99451e7dc70SSrikar Dronamraju 	tsk->thread.trap_nr	= X86_TRAP_PF;
9952d4a7167SIngo Molnar 
996a6e04aa9SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE
997f672b49bSAndi Kleen 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
998a6e04aa9SAndi Kleen 		printk(KERN_ERR
999a6e04aa9SAndi Kleen 	"MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
1000a6e04aa9SAndi Kleen 			tsk->comm, tsk->pid, address);
1001a6e04aa9SAndi Kleen 		code = BUS_MCEERR_AR;
1002a6e04aa9SAndi Kleen 	}
1003a6e04aa9SAndi Kleen #endif
10047b2d0dbaSDave Hansen 	force_sig_info_fault(SIGBUS, code, address, tsk, vma, fault);
100592181f19SNick Piggin }
100692181f19SNick Piggin 
10073a13c4d7SJohannes Weiner static noinline void
10082d4a7167SIngo Molnar mm_fault_error(struct pt_regs *regs, unsigned long error_code,
10097b2d0dbaSDave Hansen 	       unsigned long address, struct vm_area_struct *vma,
10107b2d0dbaSDave Hansen 	       unsigned int fault)
101192181f19SNick Piggin {
10123a13c4d7SJohannes Weiner 	if (fatal_signal_pending(current) && !(error_code & PF_USER)) {
10134fc34901SAndy Lutomirski 		no_context(regs, error_code, address, 0, 0);
10143a13c4d7SJohannes Weiner 		return;
1015b80ef10eSKOSAKI Motohiro 	}
1016b80ef10eSKOSAKI Motohiro 
10172d4a7167SIngo Molnar 	if (fault & VM_FAULT_OOM) {
1018f8626854SAndrey Vagin 		/* Kernel mode? Handle exceptions or die: */
1019f8626854SAndrey Vagin 		if (!(error_code & PF_USER)) {
10204fc34901SAndy Lutomirski 			no_context(regs, error_code, address,
10214fc34901SAndy Lutomirski 				   SIGSEGV, SEGV_MAPERR);
10223a13c4d7SJohannes Weiner 			return;
1023f8626854SAndrey Vagin 		}
1024f8626854SAndrey Vagin 
1025c2d23f91SDavid Rientjes 		/*
1026c2d23f91SDavid Rientjes 		 * We ran out of memory, call the OOM killer, and return the
1027c2d23f91SDavid Rientjes 		 * userspace (which will retry the fault, or kill us if we got
1028c2d23f91SDavid Rientjes 		 * oom-killed):
1029c2d23f91SDavid Rientjes 		 */
1030c2d23f91SDavid Rientjes 		pagefault_out_of_memory();
10312d4a7167SIngo Molnar 	} else {
1032f672b49bSAndi Kleen 		if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
1033f672b49bSAndi Kleen 			     VM_FAULT_HWPOISON_LARGE))
10347b2d0dbaSDave Hansen 			do_sigbus(regs, error_code, address, vma, fault);
103533692f27SLinus Torvalds 		else if (fault & VM_FAULT_SIGSEGV)
10367b2d0dbaSDave Hansen 			bad_area_nosemaphore(regs, error_code, address, vma);
103792181f19SNick Piggin 		else
103892181f19SNick Piggin 			BUG();
103992181f19SNick Piggin 	}
10402d4a7167SIngo Molnar }
104192181f19SNick Piggin 
1042d8b57bb7SThomas Gleixner static int spurious_fault_check(unsigned long error_code, pte_t *pte)
1043d8b57bb7SThomas Gleixner {
1044d8b57bb7SThomas Gleixner 	if ((error_code & PF_WRITE) && !pte_write(*pte))
1045d8b57bb7SThomas Gleixner 		return 0;
10462d4a7167SIngo Molnar 
1047d8b57bb7SThomas Gleixner 	if ((error_code & PF_INSTR) && !pte_exec(*pte))
1048d8b57bb7SThomas Gleixner 		return 0;
1049b3ecd515SDave Hansen 	/*
1050b3ecd515SDave Hansen 	 * Note: We do not do lazy flushing on protection key
1051b3ecd515SDave Hansen 	 * changes, so no spurious fault will ever set PF_PK.
1052b3ecd515SDave Hansen 	 */
1053b3ecd515SDave Hansen 	if ((error_code & PF_PK))
1054b3ecd515SDave Hansen 		return 1;
1055d8b57bb7SThomas Gleixner 
1056d8b57bb7SThomas Gleixner 	return 1;
1057d8b57bb7SThomas Gleixner }
1058d8b57bb7SThomas Gleixner 
1059c61e211dSHarvey Harrison /*
10602d4a7167SIngo Molnar  * Handle a spurious fault caused by a stale TLB entry.
10612d4a7167SIngo Molnar  *
10622d4a7167SIngo Molnar  * This allows us to lazily refresh the TLB when increasing the
10632d4a7167SIngo Molnar  * permissions of a kernel page (RO -> RW or NX -> X).  Doing it
10642d4a7167SIngo Molnar  * eagerly is very expensive since that implies doing a full
10652d4a7167SIngo Molnar  * cross-processor TLB flush, even if no stale TLB entries exist
10662d4a7167SIngo Molnar  * on other processors.
10672d4a7167SIngo Molnar  *
106831668511SDavid Vrabel  * Spurious faults may only occur if the TLB contains an entry with
106931668511SDavid Vrabel  * fewer permission than the page table entry.  Non-present (P = 0)
107031668511SDavid Vrabel  * and reserved bit (R = 1) faults are never spurious.
107131668511SDavid Vrabel  *
10725b727a3bSJeremy Fitzhardinge  * There are no security implications to leaving a stale TLB when
10735b727a3bSJeremy Fitzhardinge  * increasing the permissions on a page.
107431668511SDavid Vrabel  *
107531668511SDavid Vrabel  * Returns non-zero if a spurious fault was handled, zero otherwise.
107631668511SDavid Vrabel  *
107731668511SDavid Vrabel  * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
107831668511SDavid Vrabel  * (Optional Invalidation).
10795b727a3bSJeremy Fitzhardinge  */
10809326638cSMasami Hiramatsu static noinline int
10812d4a7167SIngo Molnar spurious_fault(unsigned long error_code, unsigned long address)
10825b727a3bSJeremy Fitzhardinge {
10835b727a3bSJeremy Fitzhardinge 	pgd_t *pgd;
10845b727a3bSJeremy Fitzhardinge 	pud_t *pud;
10855b727a3bSJeremy Fitzhardinge 	pmd_t *pmd;
10865b727a3bSJeremy Fitzhardinge 	pte_t *pte;
10873c3e5694SSteven Rostedt 	int ret;
10885b727a3bSJeremy Fitzhardinge 
108931668511SDavid Vrabel 	/*
109031668511SDavid Vrabel 	 * Only writes to RO or instruction fetches from NX may cause
109131668511SDavid Vrabel 	 * spurious faults.
109231668511SDavid Vrabel 	 *
109331668511SDavid Vrabel 	 * These could be from user or supervisor accesses but the TLB
109431668511SDavid Vrabel 	 * is only lazily flushed after a kernel mapping protection
109531668511SDavid Vrabel 	 * change, so user accesses are not expected to cause spurious
109631668511SDavid Vrabel 	 * faults.
109731668511SDavid Vrabel 	 */
109831668511SDavid Vrabel 	if (error_code != (PF_WRITE | PF_PROT)
109931668511SDavid Vrabel 	    && error_code != (PF_INSTR | PF_PROT))
11005b727a3bSJeremy Fitzhardinge 		return 0;
11015b727a3bSJeremy Fitzhardinge 
11025b727a3bSJeremy Fitzhardinge 	pgd = init_mm.pgd + pgd_index(address);
11035b727a3bSJeremy Fitzhardinge 	if (!pgd_present(*pgd))
11045b727a3bSJeremy Fitzhardinge 		return 0;
11055b727a3bSJeremy Fitzhardinge 
11065b727a3bSJeremy Fitzhardinge 	pud = pud_offset(pgd, address);
11075b727a3bSJeremy Fitzhardinge 	if (!pud_present(*pud))
11085b727a3bSJeremy Fitzhardinge 		return 0;
11095b727a3bSJeremy Fitzhardinge 
1110d8b57bb7SThomas Gleixner 	if (pud_large(*pud))
1111d8b57bb7SThomas Gleixner 		return spurious_fault_check(error_code, (pte_t *) pud);
1112d8b57bb7SThomas Gleixner 
11135b727a3bSJeremy Fitzhardinge 	pmd = pmd_offset(pud, address);
11145b727a3bSJeremy Fitzhardinge 	if (!pmd_present(*pmd))
11155b727a3bSJeremy Fitzhardinge 		return 0;
11165b727a3bSJeremy Fitzhardinge 
1117d8b57bb7SThomas Gleixner 	if (pmd_large(*pmd))
1118d8b57bb7SThomas Gleixner 		return spurious_fault_check(error_code, (pte_t *) pmd);
1119d8b57bb7SThomas Gleixner 
11205b727a3bSJeremy Fitzhardinge 	pte = pte_offset_kernel(pmd, address);
1121954f8571SAndrea Arcangeli 	if (!pte_present(*pte))
11225b727a3bSJeremy Fitzhardinge 		return 0;
11235b727a3bSJeremy Fitzhardinge 
11243c3e5694SSteven Rostedt 	ret = spurious_fault_check(error_code, pte);
11253c3e5694SSteven Rostedt 	if (!ret)
11263c3e5694SSteven Rostedt 		return 0;
11273c3e5694SSteven Rostedt 
11283c3e5694SSteven Rostedt 	/*
11292d4a7167SIngo Molnar 	 * Make sure we have permissions in PMD.
11302d4a7167SIngo Molnar 	 * If not, then there's a bug in the page tables:
11313c3e5694SSteven Rostedt 	 */
11323c3e5694SSteven Rostedt 	ret = spurious_fault_check(error_code, (pte_t *) pmd);
11333c3e5694SSteven Rostedt 	WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
11342d4a7167SIngo Molnar 
11353c3e5694SSteven Rostedt 	return ret;
11365b727a3bSJeremy Fitzhardinge }
11379326638cSMasami Hiramatsu NOKPROBE_SYMBOL(spurious_fault);
11385b727a3bSJeremy Fitzhardinge 
1139c61e211dSHarvey Harrison int show_unhandled_signals = 1;
1140c61e211dSHarvey Harrison 
11412d4a7167SIngo Molnar static inline int
114268da336aSMichel Lespinasse access_error(unsigned long error_code, struct vm_area_struct *vma)
114392181f19SNick Piggin {
114407f146f5SDave Hansen 	/* This is only called for the current mm, so: */
114507f146f5SDave Hansen 	bool foreign = false;
1146e8c6226dSDave Hansen 
1147e8c6226dSDave Hansen 	/*
1148e8c6226dSDave Hansen 	 * Read or write was blocked by protection keys.  This is
1149e8c6226dSDave Hansen 	 * always an unconditional error and can never result in
1150e8c6226dSDave Hansen 	 * a follow-up action to resolve the fault, like a COW.
1151e8c6226dSDave Hansen 	 */
1152e8c6226dSDave Hansen 	if (error_code & PF_PK)
1153e8c6226dSDave Hansen 		return 1;
1154e8c6226dSDave Hansen 
115533a709b2SDave Hansen 	/*
115607f146f5SDave Hansen 	 * Make sure to check the VMA so that we do not perform
115707f146f5SDave Hansen 	 * faults just to hit a PF_PK as soon as we fill in a
115807f146f5SDave Hansen 	 * page.
115907f146f5SDave Hansen 	 */
1160d61172b4SDave Hansen 	if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
1161d61172b4SDave Hansen 				(error_code & PF_INSTR), foreign))
116207f146f5SDave Hansen 		return 1;
116333a709b2SDave Hansen 
116468da336aSMichel Lespinasse 	if (error_code & PF_WRITE) {
11652d4a7167SIngo Molnar 		/* write, present and write, not present: */
116692181f19SNick Piggin 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
116792181f19SNick Piggin 			return 1;
11682d4a7167SIngo Molnar 		return 0;
11692d4a7167SIngo Molnar 	}
11702d4a7167SIngo Molnar 
11712d4a7167SIngo Molnar 	/* read, present: */
11722d4a7167SIngo Molnar 	if (unlikely(error_code & PF_PROT))
117392181f19SNick Piggin 		return 1;
11742d4a7167SIngo Molnar 
11752d4a7167SIngo Molnar 	/* read, not present: */
117692181f19SNick Piggin 	if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
117792181f19SNick Piggin 		return 1;
117892181f19SNick Piggin 
117992181f19SNick Piggin 	return 0;
118092181f19SNick Piggin }
118192181f19SNick Piggin 
11820973a06cSHiroshi Shimamoto static int fault_in_kernel_space(unsigned long address)
11830973a06cSHiroshi Shimamoto {
1184d9517346SIngo Molnar 	return address >= TASK_SIZE_MAX;
11850973a06cSHiroshi Shimamoto }
11860973a06cSHiroshi Shimamoto 
118740d3cd66SH. Peter Anvin static inline bool smap_violation(int error_code, struct pt_regs *regs)
118840d3cd66SH. Peter Anvin {
11894640c7eeSH. Peter Anvin 	if (!IS_ENABLED(CONFIG_X86_SMAP))
11904640c7eeSH. Peter Anvin 		return false;
11914640c7eeSH. Peter Anvin 
11924640c7eeSH. Peter Anvin 	if (!static_cpu_has(X86_FEATURE_SMAP))
11934640c7eeSH. Peter Anvin 		return false;
11944640c7eeSH. Peter Anvin 
119540d3cd66SH. Peter Anvin 	if (error_code & PF_USER)
119640d3cd66SH. Peter Anvin 		return false;
119740d3cd66SH. Peter Anvin 
1198f39b6f0eSAndy Lutomirski 	if (!user_mode(regs) && (regs->flags & X86_EFLAGS_AC))
119940d3cd66SH. Peter Anvin 		return false;
120040d3cd66SH. Peter Anvin 
120140d3cd66SH. Peter Anvin 	return true;
120240d3cd66SH. Peter Anvin }
120340d3cd66SH. Peter Anvin 
1204c61e211dSHarvey Harrison /*
1205c61e211dSHarvey Harrison  * This routine handles page faults.  It determines the address,
1206c61e211dSHarvey Harrison  * and the problem, and then passes it off to one of the appropriate
1207c61e211dSHarvey Harrison  * routines.
1208d4078e23SPeter Zijlstra  *
1209d4078e23SPeter Zijlstra  * This function must have noinline because both callers
1210d4078e23SPeter Zijlstra  * {,trace_}do_page_fault() have notrace on. Having this an actual function
1211d4078e23SPeter Zijlstra  * guarantees there's a function trace entry.
1212c61e211dSHarvey Harrison  */
12139326638cSMasami Hiramatsu static noinline void
12140ac09f9fSJiri Olsa __do_page_fault(struct pt_regs *regs, unsigned long error_code,
12150ac09f9fSJiri Olsa 		unsigned long address)
1216c61e211dSHarvey Harrison {
1217c61e211dSHarvey Harrison 	struct vm_area_struct *vma;
12182d4a7167SIngo Molnar 	struct task_struct *tsk;
12192d4a7167SIngo Molnar 	struct mm_struct *mm;
122026178ec1SLinus Torvalds 	int fault, major = 0;
1221759496baSJohannes Weiner 	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
1222c61e211dSHarvey Harrison 
1223c61e211dSHarvey Harrison 	tsk = current;
1224c61e211dSHarvey Harrison 	mm = tsk->mm;
12252d4a7167SIngo Molnar 
1226f8561296SVegard Nossum 	/*
1227f8561296SVegard Nossum 	 * Detect and handle instructions that would cause a page fault for
1228f8561296SVegard Nossum 	 * both a tracked kernel page and a userspace page.
1229f8561296SVegard Nossum 	 */
1230f8561296SVegard Nossum 	if (kmemcheck_active(regs))
1231f8561296SVegard Nossum 		kmemcheck_hide(regs);
12325dfaf90fSIngo Molnar 	prefetchw(&mm->mmap_sem);
1233f8561296SVegard Nossum 
12340fd0e3daSPekka Paalanen 	if (unlikely(kmmio_fault(regs, address)))
123586069782SPekka Paalanen 		return;
1236c61e211dSHarvey Harrison 
1237c61e211dSHarvey Harrison 	/*
1238c61e211dSHarvey Harrison 	 * We fault-in kernel-space virtual memory on-demand. The
1239c61e211dSHarvey Harrison 	 * 'reference' page table is init_mm.pgd.
1240c61e211dSHarvey Harrison 	 *
1241c61e211dSHarvey Harrison 	 * NOTE! We MUST NOT take any locks for this case. We may
1242c61e211dSHarvey Harrison 	 * be in an interrupt or a critical region, and should
1243c61e211dSHarvey Harrison 	 * only copy the information from the master page table,
1244c61e211dSHarvey Harrison 	 * nothing more.
1245c61e211dSHarvey Harrison 	 *
1246c61e211dSHarvey Harrison 	 * This verifies that the fault happens in kernel space
1247c61e211dSHarvey Harrison 	 * (error_code & 4) == 0, and that the fault was not a
1248c61e211dSHarvey Harrison 	 * protection error (error_code & 9) == 0.
1249c61e211dSHarvey Harrison 	 */
12500973a06cSHiroshi Shimamoto 	if (unlikely(fault_in_kernel_space(address))) {
1251f8561296SVegard Nossum 		if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
1252f8561296SVegard Nossum 			if (vmalloc_fault(address) >= 0)
1253c61e211dSHarvey Harrison 				return;
12545b727a3bSJeremy Fitzhardinge 
1255f8561296SVegard Nossum 			if (kmemcheck_fault(regs, address, error_code))
1256f8561296SVegard Nossum 				return;
1257f8561296SVegard Nossum 		}
1258f8561296SVegard Nossum 
12592d4a7167SIngo Molnar 		/* Can handle a stale RO->RW TLB: */
126092181f19SNick Piggin 		if (spurious_fault(error_code, address))
12615b727a3bSJeremy Fitzhardinge 			return;
12625b727a3bSJeremy Fitzhardinge 
12632d4a7167SIngo Molnar 		/* kprobes don't want to hook the spurious faults: */
1264e00b12e6SPeter Zijlstra 		if (kprobes_fault(regs))
12659be260a6SMasami Hiramatsu 			return;
1266c61e211dSHarvey Harrison 		/*
1267c61e211dSHarvey Harrison 		 * Don't take the mm semaphore here. If we fixup a prefetch
12682d4a7167SIngo Molnar 		 * fault we could otherwise deadlock:
1269c61e211dSHarvey Harrison 		 */
12707b2d0dbaSDave Hansen 		bad_area_nosemaphore(regs, error_code, address, NULL);
12712d4a7167SIngo Molnar 
127292181f19SNick Piggin 		return;
1273c61e211dSHarvey Harrison 	}
1274c61e211dSHarvey Harrison 
12752d4a7167SIngo Molnar 	/* kprobes don't want to hook the spurious faults: */
1276e00b12e6SPeter Zijlstra 	if (unlikely(kprobes_fault(regs)))
12779be260a6SMasami Hiramatsu 		return;
1278e00b12e6SPeter Zijlstra 
1279e00b12e6SPeter Zijlstra 	if (unlikely(error_code & PF_RSVD))
1280e00b12e6SPeter Zijlstra 		pgtable_bad(regs, error_code, address);
1281e00b12e6SPeter Zijlstra 
1282e00b12e6SPeter Zijlstra 	if (unlikely(smap_violation(error_code, regs))) {
12837b2d0dbaSDave Hansen 		bad_area_nosemaphore(regs, error_code, address, NULL);
1284e00b12e6SPeter Zijlstra 		return;
1285e00b12e6SPeter Zijlstra 	}
1286e00b12e6SPeter Zijlstra 
1287e00b12e6SPeter Zijlstra 	/*
1288e00b12e6SPeter Zijlstra 	 * If we're in an interrupt, have no user context or are running
128970ffdb93SDavid Hildenbrand 	 * in a region with pagefaults disabled then we must not take the fault
1290e00b12e6SPeter Zijlstra 	 */
129170ffdb93SDavid Hildenbrand 	if (unlikely(faulthandler_disabled() || !mm)) {
12927b2d0dbaSDave Hansen 		bad_area_nosemaphore(regs, error_code, address, NULL);
1293e00b12e6SPeter Zijlstra 		return;
1294e00b12e6SPeter Zijlstra 	}
1295e00b12e6SPeter Zijlstra 
1296c61e211dSHarvey Harrison 	/*
1297891cffbdSLinus Torvalds 	 * It's safe to allow irq's after cr2 has been saved and the
1298891cffbdSLinus Torvalds 	 * vmalloc fault has been handled.
1299891cffbdSLinus Torvalds 	 *
1300891cffbdSLinus Torvalds 	 * User-mode registers count as a user access even for any
13012d4a7167SIngo Molnar 	 * potential system fault or CPU buglet:
1302c61e211dSHarvey Harrison 	 */
1303f39b6f0eSAndy Lutomirski 	if (user_mode(regs)) {
1304891cffbdSLinus Torvalds 		local_irq_enable();
1305891cffbdSLinus Torvalds 		error_code |= PF_USER;
1306759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
13072d4a7167SIngo Molnar 	} else {
13082d4a7167SIngo Molnar 		if (regs->flags & X86_EFLAGS_IF)
1309c61e211dSHarvey Harrison 			local_irq_enable();
13102d4a7167SIngo Molnar 	}
1311c61e211dSHarvey Harrison 
1312a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
13137dd1fcc2SPeter Zijlstra 
1314759496baSJohannes Weiner 	if (error_code & PF_WRITE)
1315759496baSJohannes Weiner 		flags |= FAULT_FLAG_WRITE;
1316d61172b4SDave Hansen 	if (error_code & PF_INSTR)
1317d61172b4SDave Hansen 		flags |= FAULT_FLAG_INSTRUCTION;
1318759496baSJohannes Weiner 
13193a1dfe6eSIngo Molnar 	/*
13203a1dfe6eSIngo Molnar 	 * When running in the kernel we expect faults to occur only to
13212d4a7167SIngo Molnar 	 * addresses in user space.  All other faults represent errors in
13222d4a7167SIngo Molnar 	 * the kernel and should generate an OOPS.  Unfortunately, in the
13232d4a7167SIngo Molnar 	 * case of an erroneous fault occurring in a code path which already
13242d4a7167SIngo Molnar 	 * holds mmap_sem we will deadlock attempting to validate the fault
13252d4a7167SIngo Molnar 	 * against the address space.  Luckily the kernel only validly
13262d4a7167SIngo Molnar 	 * references user space from well defined areas of code, which are
13272d4a7167SIngo Molnar 	 * listed in the exceptions table.
1328c61e211dSHarvey Harrison 	 *
1329c61e211dSHarvey Harrison 	 * As the vast majority of faults will be valid we will only perform
13302d4a7167SIngo Molnar 	 * the source reference check when there is a possibility of a
13312d4a7167SIngo Molnar 	 * deadlock. Attempt to lock the address space, if we cannot we then
13322d4a7167SIngo Molnar 	 * validate the source. If this is invalid we can skip the address
13332d4a7167SIngo Molnar 	 * space check, thus avoiding the deadlock:
1334c61e211dSHarvey Harrison 	 */
133592181f19SNick Piggin 	if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1336c61e211dSHarvey Harrison 		if ((error_code & PF_USER) == 0 &&
133792181f19SNick Piggin 		    !search_exception_tables(regs->ip)) {
13387b2d0dbaSDave Hansen 			bad_area_nosemaphore(regs, error_code, address, NULL);
133992181f19SNick Piggin 			return;
134092181f19SNick Piggin 		}
1341d065bd81SMichel Lespinasse retry:
1342c61e211dSHarvey Harrison 		down_read(&mm->mmap_sem);
134301006074SPeter Zijlstra 	} else {
134401006074SPeter Zijlstra 		/*
13452d4a7167SIngo Molnar 		 * The above down_read_trylock() might have succeeded in
13462d4a7167SIngo Molnar 		 * which case we'll have missed the might_sleep() from
13472d4a7167SIngo Molnar 		 * down_read():
134801006074SPeter Zijlstra 		 */
134901006074SPeter Zijlstra 		might_sleep();
1350c61e211dSHarvey Harrison 	}
1351c61e211dSHarvey Harrison 
1352c61e211dSHarvey Harrison 	vma = find_vma(mm, address);
135392181f19SNick Piggin 	if (unlikely(!vma)) {
135492181f19SNick Piggin 		bad_area(regs, error_code, address);
135592181f19SNick Piggin 		return;
135692181f19SNick Piggin 	}
135792181f19SNick Piggin 	if (likely(vma->vm_start <= address))
1358c61e211dSHarvey Harrison 		goto good_area;
135992181f19SNick Piggin 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
136092181f19SNick Piggin 		bad_area(regs, error_code, address);
136192181f19SNick Piggin 		return;
136292181f19SNick Piggin 	}
1363c61e211dSHarvey Harrison 	if (error_code & PF_USER) {
1364c61e211dSHarvey Harrison 		/*
1365c61e211dSHarvey Harrison 		 * Accessing the stack below %sp is always a bug.
1366c61e211dSHarvey Harrison 		 * The large cushion allows instructions like enter
1367c61e211dSHarvey Harrison 		 * and pusha to work. ("enter $65535, $31" pushes
1368c61e211dSHarvey Harrison 		 * 32 pointers and then decrements %sp by 65535.)
1369c61e211dSHarvey Harrison 		 */
137092181f19SNick Piggin 		if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
137192181f19SNick Piggin 			bad_area(regs, error_code, address);
137292181f19SNick Piggin 			return;
1373c61e211dSHarvey Harrison 		}
137492181f19SNick Piggin 	}
137592181f19SNick Piggin 	if (unlikely(expand_stack(vma, address))) {
137692181f19SNick Piggin 		bad_area(regs, error_code, address);
137792181f19SNick Piggin 		return;
137892181f19SNick Piggin 	}
137992181f19SNick Piggin 
1380c61e211dSHarvey Harrison 	/*
1381c61e211dSHarvey Harrison 	 * Ok, we have a good vm_area for this memory access, so
1382c61e211dSHarvey Harrison 	 * we can handle it..
1383c61e211dSHarvey Harrison 	 */
1384c61e211dSHarvey Harrison good_area:
138568da336aSMichel Lespinasse 	if (unlikely(access_error(error_code, vma))) {
13867b2d0dbaSDave Hansen 		bad_area_access_error(regs, error_code, address, vma);
138792181f19SNick Piggin 		return;
1388c61e211dSHarvey Harrison 	}
1389c61e211dSHarvey Harrison 
1390c61e211dSHarvey Harrison 	/*
1391c61e211dSHarvey Harrison 	 * If for any reason at all we couldn't handle the fault,
1392c61e211dSHarvey Harrison 	 * make sure we exit gracefully rather than endlessly redo
13939a95f3cfSPaul Cassella 	 * the fault.  Since we never set FAULT_FLAG_RETRY_NOWAIT, if
13949a95f3cfSPaul Cassella 	 * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked.
1395c61e211dSHarvey Harrison 	 */
1396dcddffd4SKirill A. Shutemov 	fault = handle_mm_fault(vma, address, flags);
139726178ec1SLinus Torvalds 	major |= fault & VM_FAULT_MAJOR;
13982d4a7167SIngo Molnar 
13993a13c4d7SJohannes Weiner 	/*
140026178ec1SLinus Torvalds 	 * If we need to retry the mmap_sem has already been released,
140126178ec1SLinus Torvalds 	 * and if there is a fatal signal pending there is no guarantee
140226178ec1SLinus Torvalds 	 * that we made any progress. Handle this case first.
14033a13c4d7SJohannes Weiner 	 */
140426178ec1SLinus Torvalds 	if (unlikely(fault & VM_FAULT_RETRY)) {
140526178ec1SLinus Torvalds 		/* Retry at most once */
140626178ec1SLinus Torvalds 		if (flags & FAULT_FLAG_ALLOW_RETRY) {
140726178ec1SLinus Torvalds 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
140826178ec1SLinus Torvalds 			flags |= FAULT_FLAG_TRIED;
140926178ec1SLinus Torvalds 			if (!fatal_signal_pending(tsk))
141026178ec1SLinus Torvalds 				goto retry;
141126178ec1SLinus Torvalds 		}
141226178ec1SLinus Torvalds 
141326178ec1SLinus Torvalds 		/* User mode? Just return to handle the fatal exception */
1414cf3c0a15SLinus Torvalds 		if (flags & FAULT_FLAG_USER)
14153a13c4d7SJohannes Weiner 			return;
14163a13c4d7SJohannes Weiner 
141726178ec1SLinus Torvalds 		/* Not returning to user mode? Handle exceptions or die: */
141826178ec1SLinus Torvalds 		no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
141926178ec1SLinus Torvalds 		return;
142026178ec1SLinus Torvalds 	}
142126178ec1SLinus Torvalds 
14227fb08ecaSLinus Torvalds 	up_read(&mm->mmap_sem);
142326178ec1SLinus Torvalds 	if (unlikely(fault & VM_FAULT_ERROR)) {
14247b2d0dbaSDave Hansen 		mm_fault_error(regs, error_code, address, vma, fault);
142537b23e05SKOSAKI Motohiro 		return;
142637b23e05SKOSAKI Motohiro 	}
142737b23e05SKOSAKI Motohiro 
142837b23e05SKOSAKI Motohiro 	/*
142926178ec1SLinus Torvalds 	 * Major/minor page fault accounting. If any of the events
143026178ec1SLinus Torvalds 	 * returned VM_FAULT_MAJOR, we account it as a major fault.
1431d065bd81SMichel Lespinasse 	 */
143226178ec1SLinus Torvalds 	if (major) {
1433c61e211dSHarvey Harrison 		tsk->maj_flt++;
143426178ec1SLinus Torvalds 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
1435ac17dc8eSPeter Zijlstra 	} else {
1436c61e211dSHarvey Harrison 		tsk->min_flt++;
143726178ec1SLinus Torvalds 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
1438d065bd81SMichel Lespinasse 	}
1439c61e211dSHarvey Harrison 
14408c938f9fSIngo Molnar 	check_v8086_mode(regs, address, tsk);
1441c61e211dSHarvey Harrison }
14429326638cSMasami Hiramatsu NOKPROBE_SYMBOL(__do_page_fault);
14436ba3c97aSFrederic Weisbecker 
14449326638cSMasami Hiramatsu dotraplinkage void notrace
14456ba3c97aSFrederic Weisbecker do_page_fault(struct pt_regs *regs, unsigned long error_code)
14466ba3c97aSFrederic Weisbecker {
1447d4078e23SPeter Zijlstra 	unsigned long address = read_cr2(); /* Get the faulting address */
14486c1e0256SFrederic Weisbecker 	enum ctx_state prev_state;
1449d4078e23SPeter Zijlstra 
1450d4078e23SPeter Zijlstra 	/*
1451d4078e23SPeter Zijlstra 	 * We must have this function tagged with __kprobes, notrace and call
1452d4078e23SPeter Zijlstra 	 * read_cr2() before calling anything else. To avoid calling any kind
1453d4078e23SPeter Zijlstra 	 * of tracing machinery before we've observed the CR2 value.
1454d4078e23SPeter Zijlstra 	 *
1455d4078e23SPeter Zijlstra 	 * exception_{enter,exit}() contain all sorts of tracepoints.
1456d4078e23SPeter Zijlstra 	 */
14576c1e0256SFrederic Weisbecker 
14586c1e0256SFrederic Weisbecker 	prev_state = exception_enter();
14590ac09f9fSJiri Olsa 	__do_page_fault(regs, error_code, address);
14606c1e0256SFrederic Weisbecker 	exception_exit(prev_state);
14616ba3c97aSFrederic Weisbecker }
14629326638cSMasami Hiramatsu NOKPROBE_SYMBOL(do_page_fault);
146325c74b10SSeiji Aguchi 
1464d4078e23SPeter Zijlstra #ifdef CONFIG_TRACING
14659326638cSMasami Hiramatsu static nokprobe_inline void
14669326638cSMasami Hiramatsu trace_page_fault_entries(unsigned long address, struct pt_regs *regs,
1467d34603b0SSeiji Aguchi 			 unsigned long error_code)
1468d34603b0SSeiji Aguchi {
1469d34603b0SSeiji Aguchi 	if (user_mode(regs))
1470d4078e23SPeter Zijlstra 		trace_page_fault_user(address, regs, error_code);
1471d34603b0SSeiji Aguchi 	else
1472d4078e23SPeter Zijlstra 		trace_page_fault_kernel(address, regs, error_code);
1473d34603b0SSeiji Aguchi }
1474d34603b0SSeiji Aguchi 
14759326638cSMasami Hiramatsu dotraplinkage void notrace
147625c74b10SSeiji Aguchi trace_do_page_fault(struct pt_regs *regs, unsigned long error_code)
147725c74b10SSeiji Aguchi {
14780ac09f9fSJiri Olsa 	/*
14790ac09f9fSJiri Olsa 	 * The exception_enter and tracepoint processing could
14800ac09f9fSJiri Olsa 	 * trigger another page faults (user space callchain
14810ac09f9fSJiri Olsa 	 * reading) and destroy the original cr2 value, so read
14820ac09f9fSJiri Olsa 	 * the faulting address now.
14830ac09f9fSJiri Olsa 	 */
14840ac09f9fSJiri Olsa 	unsigned long address = read_cr2();
1485d4078e23SPeter Zijlstra 	enum ctx_state prev_state;
148625c74b10SSeiji Aguchi 
148725c74b10SSeiji Aguchi 	prev_state = exception_enter();
1488d4078e23SPeter Zijlstra 	trace_page_fault_entries(address, regs, error_code);
14890ac09f9fSJiri Olsa 	__do_page_fault(regs, error_code, address);
149025c74b10SSeiji Aguchi 	exception_exit(prev_state);
149125c74b10SSeiji Aguchi }
14929326638cSMasami Hiramatsu NOKPROBE_SYMBOL(trace_do_page_fault);
1493d4078e23SPeter Zijlstra #endif /* CONFIG_TRACING */
1494