xref: /openbmc/linux/arch/x86/mm/fault.c (revision 426e34cc4f6094cefe4f3175764cdf583128e7cd)
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/magic.h>		/* STACK_END_MAGIC		*/
7a2bcd473SIngo Molnar #include <linux/sched.h>		/* test_thread_flag(), ...	*/
8a2bcd473SIngo Molnar #include <linux/kdebug.h>		/* oops_begin/end, ...		*/
9a2bcd473SIngo Molnar #include <linux/module.h>		/* search_exception_table	*/
10a2bcd473SIngo Molnar #include <linux/bootmem.h>		/* max_low_pfn			*/
11a2bcd473SIngo Molnar #include <linux/kprobes.h>		/* __kprobes, ...		*/
12a2bcd473SIngo Molnar #include <linux/mmiotrace.h>		/* kmmio_handler, ...		*/
13cdd6c482SIngo Molnar #include <linux/perf_event.h>		/* perf_sw_event		*/
14f672b49bSAndi Kleen #include <linux/hugetlb.h>		/* hstate_index_to_shift	*/
15268bb0ceSLinus Torvalds #include <linux/prefetch.h>		/* prefetchw			*/
1656dd9470SFrederic Weisbecker #include <linux/context_tracking.h>	/* exception_enter(), ...	*/
17c61e211dSHarvey Harrison 
18a2bcd473SIngo Molnar #include <asm/traps.h>			/* dotraplinkage, ...		*/
19a2bcd473SIngo Molnar #include <asm/pgalloc.h>		/* pgd_*(), ...			*/
20f8561296SVegard Nossum #include <asm/kmemcheck.h>		/* kmemcheck_*(), ...		*/
21fab1167cSH. Peter Anvin #include <asm/fixmap.h>			/* VSYSCALL_START		*/
22c61e211dSHarvey Harrison 
23d34603b0SSeiji Aguchi #define CREATE_TRACE_POINTS
24d34603b0SSeiji Aguchi #include <asm/trace/exceptions.h>
25d34603b0SSeiji Aguchi 
26c61e211dSHarvey Harrison /*
272d4a7167SIngo Molnar  * Page fault error code bits:
282d4a7167SIngo Molnar  *
292d4a7167SIngo Molnar  *   bit 0 ==	 0: no page found	1: protection fault
302d4a7167SIngo Molnar  *   bit 1 ==	 0: read access		1: write access
312d4a7167SIngo Molnar  *   bit 2 ==	 0: kernel-mode access	1: user-mode access
322d4a7167SIngo Molnar  *   bit 3 ==				1: use of reserved bit detected
332d4a7167SIngo Molnar  *   bit 4 ==				1: fault was an instruction fetch
34c61e211dSHarvey Harrison  */
352d4a7167SIngo Molnar enum x86_pf_error_code {
362d4a7167SIngo Molnar 
372d4a7167SIngo Molnar 	PF_PROT		=		1 << 0,
382d4a7167SIngo Molnar 	PF_WRITE	=		1 << 1,
392d4a7167SIngo Molnar 	PF_USER		=		1 << 2,
402d4a7167SIngo Molnar 	PF_RSVD		=		1 << 3,
412d4a7167SIngo Molnar 	PF_INSTR	=		1 << 4,
422d4a7167SIngo Molnar };
43c61e211dSHarvey Harrison 
44b814d41fSIngo Molnar /*
45b319eed0SIngo Molnar  * Returns 0 if mmiotrace is disabled, or if the fault is not
46b319eed0SIngo Molnar  * handled by mmiotrace:
47b814d41fSIngo Molnar  */
4862c9295fSMasami Hiramatsu static inline int __kprobes
4962c9295fSMasami Hiramatsu kmmio_fault(struct pt_regs *regs, unsigned long addr)
5086069782SPekka Paalanen {
510fd0e3daSPekka Paalanen 	if (unlikely(is_kmmio_active()))
520fd0e3daSPekka Paalanen 		if (kmmio_handler(regs, addr) == 1)
530fd0e3daSPekka Paalanen 			return -1;
540fd0e3daSPekka Paalanen 	return 0;
5586069782SPekka Paalanen }
5686069782SPekka Paalanen 
57e00b12e6SPeter Zijlstra static inline int __kprobes kprobes_fault(struct pt_regs *regs)
58c61e211dSHarvey Harrison {
59c61e211dSHarvey Harrison 	int ret = 0;
60c61e211dSHarvey Harrison 
61c61e211dSHarvey Harrison 	/* kprobe_running() needs smp_processor_id() */
62b1801812SIngo Molnar 	if (kprobes_built_in() && !user_mode_vm(regs)) {
63c61e211dSHarvey Harrison 		preempt_disable();
64c61e211dSHarvey Harrison 		if (kprobe_running() && kprobe_fault_handler(regs, 14))
65c61e211dSHarvey Harrison 			ret = 1;
66c61e211dSHarvey Harrison 		preempt_enable();
67c61e211dSHarvey Harrison 	}
68c61e211dSHarvey Harrison 
69c61e211dSHarvey Harrison 	return ret;
70c61e211dSHarvey Harrison }
71c61e211dSHarvey Harrison 
72c61e211dSHarvey Harrison /*
732d4a7167SIngo Molnar  * Prefetch quirks:
742d4a7167SIngo Molnar  *
752d4a7167SIngo Molnar  * 32-bit mode:
762d4a7167SIngo Molnar  *
77c61e211dSHarvey Harrison  *   Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
78c61e211dSHarvey Harrison  *   Check that here and ignore it.
79c61e211dSHarvey Harrison  *
802d4a7167SIngo Molnar  * 64-bit mode:
812d4a7167SIngo Molnar  *
82c61e211dSHarvey Harrison  *   Sometimes the CPU reports invalid exceptions on prefetch.
83c61e211dSHarvey Harrison  *   Check that here and ignore it.
84c61e211dSHarvey Harrison  *
852d4a7167SIngo Molnar  * Opcode checker based on code by Richard Brunner.
86c61e211dSHarvey Harrison  */
87107a0367SIngo Molnar static inline int
88107a0367SIngo Molnar check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
89107a0367SIngo Molnar 		      unsigned char opcode, int *prefetch)
90c61e211dSHarvey Harrison {
91107a0367SIngo Molnar 	unsigned char instr_hi = opcode & 0xf0;
92107a0367SIngo Molnar 	unsigned char instr_lo = opcode & 0x0f;
93c61e211dSHarvey Harrison 
94c61e211dSHarvey Harrison 	switch (instr_hi) {
95c61e211dSHarvey Harrison 	case 0x20:
96c61e211dSHarvey Harrison 	case 0x30:
97c61e211dSHarvey Harrison 		/*
98c61e211dSHarvey Harrison 		 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
99c61e211dSHarvey Harrison 		 * In X86_64 long mode, the CPU will signal invalid
100c61e211dSHarvey Harrison 		 * opcode if some of these prefixes are present so
101c61e211dSHarvey Harrison 		 * X86_64 will never get here anyway
102c61e211dSHarvey Harrison 		 */
103107a0367SIngo Molnar 		return ((instr_lo & 7) == 0x6);
104c61e211dSHarvey Harrison #ifdef CONFIG_X86_64
105c61e211dSHarvey Harrison 	case 0x40:
106c61e211dSHarvey Harrison 		/*
107c61e211dSHarvey Harrison 		 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
108c61e211dSHarvey Harrison 		 * Need to figure out under what instruction mode the
109c61e211dSHarvey Harrison 		 * instruction was issued. Could check the LDT for lm,
110c61e211dSHarvey Harrison 		 * but for now it's good enough to assume that long
111c61e211dSHarvey Harrison 		 * mode only uses well known segments or kernel.
112c61e211dSHarvey Harrison 		 */
113318f5a2aSAndy Lutomirski 		return (!user_mode(regs) || user_64bit_mode(regs));
114c61e211dSHarvey Harrison #endif
115c61e211dSHarvey Harrison 	case 0x60:
116c61e211dSHarvey Harrison 		/* 0x64 thru 0x67 are valid prefixes in all modes. */
117107a0367SIngo Molnar 		return (instr_lo & 0xC) == 0x4;
118c61e211dSHarvey Harrison 	case 0xF0:
119c61e211dSHarvey Harrison 		/* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
120107a0367SIngo Molnar 		return !instr_lo || (instr_lo>>1) == 1;
121c61e211dSHarvey Harrison 	case 0x00:
122c61e211dSHarvey Harrison 		/* Prefetch instruction is 0x0F0D or 0x0F18 */
123107a0367SIngo Molnar 		if (probe_kernel_address(instr, opcode))
124107a0367SIngo Molnar 			return 0;
125107a0367SIngo Molnar 
126107a0367SIngo Molnar 		*prefetch = (instr_lo == 0xF) &&
127107a0367SIngo Molnar 			(opcode == 0x0D || opcode == 0x18);
128107a0367SIngo Molnar 		return 0;
129107a0367SIngo Molnar 	default:
130107a0367SIngo Molnar 		return 0;
131107a0367SIngo Molnar 	}
132107a0367SIngo Molnar }
133107a0367SIngo Molnar 
134107a0367SIngo Molnar static int
135107a0367SIngo Molnar is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
136107a0367SIngo Molnar {
137107a0367SIngo Molnar 	unsigned char *max_instr;
138107a0367SIngo Molnar 	unsigned char *instr;
139107a0367SIngo Molnar 	int prefetch = 0;
140107a0367SIngo Molnar 
141107a0367SIngo Molnar 	/*
142107a0367SIngo Molnar 	 * If it was a exec (instruction fetch) fault on NX page, then
143107a0367SIngo Molnar 	 * do not ignore the fault:
144107a0367SIngo Molnar 	 */
145107a0367SIngo Molnar 	if (error_code & PF_INSTR)
146107a0367SIngo Molnar 		return 0;
147107a0367SIngo Molnar 
148107a0367SIngo Molnar 	instr = (void *)convert_ip_to_linear(current, regs);
149107a0367SIngo Molnar 	max_instr = instr + 15;
150107a0367SIngo Molnar 
151107a0367SIngo Molnar 	if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
152107a0367SIngo Molnar 		return 0;
153107a0367SIngo Molnar 
154107a0367SIngo Molnar 	while (instr < max_instr) {
155107a0367SIngo Molnar 		unsigned char opcode;
156c61e211dSHarvey Harrison 
157c61e211dSHarvey Harrison 		if (probe_kernel_address(instr, opcode))
158c61e211dSHarvey Harrison 			break;
159107a0367SIngo Molnar 
160107a0367SIngo Molnar 		instr++;
161107a0367SIngo Molnar 
162107a0367SIngo Molnar 		if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
163c61e211dSHarvey Harrison 			break;
164c61e211dSHarvey Harrison 	}
165c61e211dSHarvey Harrison 	return prefetch;
166c61e211dSHarvey Harrison }
167c61e211dSHarvey Harrison 
1682d4a7167SIngo Molnar static void
1692d4a7167SIngo Molnar force_sig_info_fault(int si_signo, int si_code, unsigned long address,
170f672b49bSAndi Kleen 		     struct task_struct *tsk, int fault)
171c61e211dSHarvey Harrison {
172f672b49bSAndi Kleen 	unsigned lsb = 0;
173c61e211dSHarvey Harrison 	siginfo_t info;
174c61e211dSHarvey Harrison 
175c61e211dSHarvey Harrison 	info.si_signo	= si_signo;
176c61e211dSHarvey Harrison 	info.si_errno	= 0;
177c61e211dSHarvey Harrison 	info.si_code	= si_code;
178c61e211dSHarvey Harrison 	info.si_addr	= (void __user *)address;
179f672b49bSAndi Kleen 	if (fault & VM_FAULT_HWPOISON_LARGE)
180f672b49bSAndi Kleen 		lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
181f672b49bSAndi Kleen 	if (fault & VM_FAULT_HWPOISON)
182f672b49bSAndi Kleen 		lsb = PAGE_SHIFT;
183f672b49bSAndi Kleen 	info.si_addr_lsb = lsb;
1842d4a7167SIngo Molnar 
185c61e211dSHarvey Harrison 	force_sig_info(si_signo, &info, tsk);
186c61e211dSHarvey Harrison }
187c61e211dSHarvey Harrison 
188f2f13a85SIngo Molnar DEFINE_SPINLOCK(pgd_lock);
189f2f13a85SIngo Molnar LIST_HEAD(pgd_list);
1902d4a7167SIngo Molnar 
191f2f13a85SIngo Molnar #ifdef CONFIG_X86_32
192f2f13a85SIngo Molnar static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
193f2f13a85SIngo Molnar {
194f2f13a85SIngo Molnar 	unsigned index = pgd_index(address);
195f2f13a85SIngo Molnar 	pgd_t *pgd_k;
196f2f13a85SIngo Molnar 	pud_t *pud, *pud_k;
197f2f13a85SIngo Molnar 	pmd_t *pmd, *pmd_k;
198f2f13a85SIngo Molnar 
199f2f13a85SIngo Molnar 	pgd += index;
200f2f13a85SIngo Molnar 	pgd_k = init_mm.pgd + index;
201f2f13a85SIngo Molnar 
202f2f13a85SIngo Molnar 	if (!pgd_present(*pgd_k))
203f2f13a85SIngo Molnar 		return NULL;
204f2f13a85SIngo Molnar 
205f2f13a85SIngo Molnar 	/*
206f2f13a85SIngo Molnar 	 * set_pgd(pgd, *pgd_k); here would be useless on PAE
207f2f13a85SIngo Molnar 	 * and redundant with the set_pmd() on non-PAE. As would
208f2f13a85SIngo Molnar 	 * set_pud.
209f2f13a85SIngo Molnar 	 */
210f2f13a85SIngo Molnar 	pud = pud_offset(pgd, address);
211f2f13a85SIngo Molnar 	pud_k = pud_offset(pgd_k, address);
212f2f13a85SIngo Molnar 	if (!pud_present(*pud_k))
213f2f13a85SIngo Molnar 		return NULL;
214f2f13a85SIngo Molnar 
215f2f13a85SIngo Molnar 	pmd = pmd_offset(pud, address);
216f2f13a85SIngo Molnar 	pmd_k = pmd_offset(pud_k, address);
217f2f13a85SIngo Molnar 	if (!pmd_present(*pmd_k))
218f2f13a85SIngo Molnar 		return NULL;
219f2f13a85SIngo Molnar 
220b8bcfe99SJeremy Fitzhardinge 	if (!pmd_present(*pmd))
221f2f13a85SIngo Molnar 		set_pmd(pmd, *pmd_k);
222b8bcfe99SJeremy Fitzhardinge 	else
223f2f13a85SIngo Molnar 		BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
224f2f13a85SIngo Molnar 
225f2f13a85SIngo Molnar 	return pmd_k;
226f2f13a85SIngo Molnar }
227f2f13a85SIngo Molnar 
228f2f13a85SIngo Molnar void vmalloc_sync_all(void)
229f2f13a85SIngo Molnar {
230f2f13a85SIngo Molnar 	unsigned long address;
231f2f13a85SIngo Molnar 
232f2f13a85SIngo Molnar 	if (SHARED_KERNEL_PMD)
233f2f13a85SIngo Molnar 		return;
234f2f13a85SIngo Molnar 
235f2f13a85SIngo Molnar 	for (address = VMALLOC_START & PMD_MASK;
236f2f13a85SIngo Molnar 	     address >= TASK_SIZE && address < FIXADDR_TOP;
237f2f13a85SIngo Molnar 	     address += PMD_SIZE) {
238f2f13a85SIngo Molnar 		struct page *page;
239f2f13a85SIngo Molnar 
240a79e53d8SAndrea Arcangeli 		spin_lock(&pgd_lock);
241f2f13a85SIngo Molnar 		list_for_each_entry(page, &pgd_list, lru) {
242617d34d9SJeremy Fitzhardinge 			spinlock_t *pgt_lock;
243f01f7c56SBorislav Petkov 			pmd_t *ret;
244617d34d9SJeremy Fitzhardinge 
245a79e53d8SAndrea Arcangeli 			/* the pgt_lock only for Xen */
246617d34d9SJeremy Fitzhardinge 			pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
247617d34d9SJeremy Fitzhardinge 
248617d34d9SJeremy Fitzhardinge 			spin_lock(pgt_lock);
249617d34d9SJeremy Fitzhardinge 			ret = vmalloc_sync_one(page_address(page), address);
250617d34d9SJeremy Fitzhardinge 			spin_unlock(pgt_lock);
251617d34d9SJeremy Fitzhardinge 
252617d34d9SJeremy Fitzhardinge 			if (!ret)
253f2f13a85SIngo Molnar 				break;
254f2f13a85SIngo Molnar 		}
255a79e53d8SAndrea Arcangeli 		spin_unlock(&pgd_lock);
256f2f13a85SIngo Molnar 	}
257f2f13a85SIngo Molnar }
258f2f13a85SIngo Molnar 
259f2f13a85SIngo Molnar /*
260f2f13a85SIngo Molnar  * 32-bit:
261f2f13a85SIngo Molnar  *
262f2f13a85SIngo Molnar  *   Handle a fault on the vmalloc or module mapping area
263f2f13a85SIngo Molnar  */
26462c9295fSMasami Hiramatsu static noinline __kprobes int vmalloc_fault(unsigned long address)
265f2f13a85SIngo Molnar {
266f2f13a85SIngo Molnar 	unsigned long pgd_paddr;
267f2f13a85SIngo Molnar 	pmd_t *pmd_k;
268f2f13a85SIngo Molnar 	pte_t *pte_k;
269f2f13a85SIngo Molnar 
270f2f13a85SIngo Molnar 	/* Make sure we are in vmalloc area: */
271f2f13a85SIngo Molnar 	if (!(address >= VMALLOC_START && address < VMALLOC_END))
272f2f13a85SIngo Molnar 		return -1;
273f2f13a85SIngo Molnar 
274ebc8827fSFrederic Weisbecker 	WARN_ON_ONCE(in_nmi());
275ebc8827fSFrederic Weisbecker 
276f2f13a85SIngo Molnar 	/*
277f2f13a85SIngo Molnar 	 * Synchronize this task's top level page-table
278f2f13a85SIngo Molnar 	 * with the 'reference' page table.
279f2f13a85SIngo Molnar 	 *
280f2f13a85SIngo Molnar 	 * Do _not_ use "current" here. We might be inside
281f2f13a85SIngo Molnar 	 * an interrupt in the middle of a task switch..
282f2f13a85SIngo Molnar 	 */
283f2f13a85SIngo Molnar 	pgd_paddr = read_cr3();
284f2f13a85SIngo Molnar 	pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
285f2f13a85SIngo Molnar 	if (!pmd_k)
286f2f13a85SIngo Molnar 		return -1;
287f2f13a85SIngo Molnar 
288f2f13a85SIngo Molnar 	pte_k = pte_offset_kernel(pmd_k, address);
289f2f13a85SIngo Molnar 	if (!pte_present(*pte_k))
290f2f13a85SIngo Molnar 		return -1;
291f2f13a85SIngo Molnar 
292f2f13a85SIngo Molnar 	return 0;
293f2f13a85SIngo Molnar }
294f2f13a85SIngo Molnar 
295f2f13a85SIngo Molnar /*
296f2f13a85SIngo Molnar  * Did it hit the DOS screen memory VA from vm86 mode?
297f2f13a85SIngo Molnar  */
298f2f13a85SIngo Molnar static inline void
299f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address,
300f2f13a85SIngo Molnar 		 struct task_struct *tsk)
301f2f13a85SIngo Molnar {
302f2f13a85SIngo Molnar 	unsigned long bit;
303f2f13a85SIngo Molnar 
304f2f13a85SIngo Molnar 	if (!v8086_mode(regs))
305f2f13a85SIngo Molnar 		return;
306f2f13a85SIngo Molnar 
307f2f13a85SIngo Molnar 	bit = (address - 0xA0000) >> PAGE_SHIFT;
308f2f13a85SIngo Molnar 	if (bit < 32)
309f2f13a85SIngo Molnar 		tsk->thread.screen_bitmap |= 1 << bit;
310f2f13a85SIngo Molnar }
311c61e211dSHarvey Harrison 
312087975b0SAkinobu Mita static bool low_pfn(unsigned long pfn)
313087975b0SAkinobu Mita {
314087975b0SAkinobu Mita 	return pfn < max_low_pfn;
315087975b0SAkinobu Mita }
316087975b0SAkinobu Mita 
317cae30f82SAdrian Bunk static void dump_pagetable(unsigned long address)
318c61e211dSHarvey Harrison {
319087975b0SAkinobu Mita 	pgd_t *base = __va(read_cr3());
320087975b0SAkinobu Mita 	pgd_t *pgd = &base[pgd_index(address)];
321087975b0SAkinobu Mita 	pmd_t *pmd;
322087975b0SAkinobu Mita 	pte_t *pte;
3232d4a7167SIngo Molnar 
324c61e211dSHarvey Harrison #ifdef CONFIG_X86_PAE
325087975b0SAkinobu Mita 	printk("*pdpt = %016Lx ", pgd_val(*pgd));
326087975b0SAkinobu Mita 	if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
327087975b0SAkinobu Mita 		goto out;
328c61e211dSHarvey Harrison #endif
329087975b0SAkinobu Mita 	pmd = pmd_offset(pud_offset(pgd, address), address);
330087975b0SAkinobu Mita 	printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
331c61e211dSHarvey Harrison 
332c61e211dSHarvey Harrison 	/*
333c61e211dSHarvey Harrison 	 * We must not directly access the pte in the highpte
334c61e211dSHarvey Harrison 	 * case if the page table is located in highmem.
335c61e211dSHarvey Harrison 	 * And let's rather not kmap-atomic the pte, just in case
3362d4a7167SIngo Molnar 	 * it's allocated already:
337c61e211dSHarvey Harrison 	 */
338087975b0SAkinobu Mita 	if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
339087975b0SAkinobu Mita 		goto out;
3402d4a7167SIngo Molnar 
341087975b0SAkinobu Mita 	pte = pte_offset_kernel(pmd, address);
342087975b0SAkinobu Mita 	printk("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
343087975b0SAkinobu Mita out:
344c61e211dSHarvey Harrison 	printk("\n");
345f2f13a85SIngo Molnar }
346f2f13a85SIngo Molnar 
347f2f13a85SIngo Molnar #else /* CONFIG_X86_64: */
348f2f13a85SIngo Molnar 
349f2f13a85SIngo Molnar void vmalloc_sync_all(void)
350f2f13a85SIngo Molnar {
3516afb5157SHaicheng Li 	sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END);
352f2f13a85SIngo Molnar }
353f2f13a85SIngo Molnar 
354f2f13a85SIngo Molnar /*
355f2f13a85SIngo Molnar  * 64-bit:
356f2f13a85SIngo Molnar  *
357f2f13a85SIngo Molnar  *   Handle a fault on the vmalloc area
358f2f13a85SIngo Molnar  *
359f2f13a85SIngo Molnar  * This assumes no large pages in there.
360f2f13a85SIngo Molnar  */
36162c9295fSMasami Hiramatsu static noinline __kprobes int vmalloc_fault(unsigned long address)
362f2f13a85SIngo Molnar {
363f2f13a85SIngo Molnar 	pgd_t *pgd, *pgd_ref;
364f2f13a85SIngo Molnar 	pud_t *pud, *pud_ref;
365f2f13a85SIngo Molnar 	pmd_t *pmd, *pmd_ref;
366f2f13a85SIngo Molnar 	pte_t *pte, *pte_ref;
367f2f13a85SIngo Molnar 
368f2f13a85SIngo Molnar 	/* Make sure we are in vmalloc area: */
369f2f13a85SIngo Molnar 	if (!(address >= VMALLOC_START && address < VMALLOC_END))
370f2f13a85SIngo Molnar 		return -1;
371f2f13a85SIngo Molnar 
372ebc8827fSFrederic Weisbecker 	WARN_ON_ONCE(in_nmi());
373ebc8827fSFrederic Weisbecker 
374f2f13a85SIngo Molnar 	/*
375f2f13a85SIngo Molnar 	 * Copy kernel mappings over when needed. This can also
376f2f13a85SIngo Molnar 	 * happen within a race in page table update. In the later
377f2f13a85SIngo Molnar 	 * case just flush:
378f2f13a85SIngo Molnar 	 */
379f2f13a85SIngo Molnar 	pgd = pgd_offset(current->active_mm, address);
380f2f13a85SIngo Molnar 	pgd_ref = pgd_offset_k(address);
381f2f13a85SIngo Molnar 	if (pgd_none(*pgd_ref))
382f2f13a85SIngo Molnar 		return -1;
383f2f13a85SIngo Molnar 
3841160c277SSamu Kallio 	if (pgd_none(*pgd)) {
385f2f13a85SIngo Molnar 		set_pgd(pgd, *pgd_ref);
3861160c277SSamu Kallio 		arch_flush_lazy_mmu_mode();
3871160c277SSamu Kallio 	} else {
388f2f13a85SIngo Molnar 		BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
3891160c277SSamu Kallio 	}
390f2f13a85SIngo Molnar 
391f2f13a85SIngo Molnar 	/*
392f2f13a85SIngo Molnar 	 * Below here mismatches are bugs because these lower tables
393f2f13a85SIngo Molnar 	 * are shared:
394f2f13a85SIngo Molnar 	 */
395f2f13a85SIngo Molnar 
396f2f13a85SIngo Molnar 	pud = pud_offset(pgd, address);
397f2f13a85SIngo Molnar 	pud_ref = pud_offset(pgd_ref, address);
398f2f13a85SIngo Molnar 	if (pud_none(*pud_ref))
399f2f13a85SIngo Molnar 		return -1;
400f2f13a85SIngo Molnar 
401f2f13a85SIngo Molnar 	if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
402f2f13a85SIngo Molnar 		BUG();
403f2f13a85SIngo Molnar 
404f2f13a85SIngo Molnar 	pmd = pmd_offset(pud, address);
405f2f13a85SIngo Molnar 	pmd_ref = pmd_offset(pud_ref, address);
406f2f13a85SIngo Molnar 	if (pmd_none(*pmd_ref))
407f2f13a85SIngo Molnar 		return -1;
408f2f13a85SIngo Molnar 
409f2f13a85SIngo Molnar 	if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
410f2f13a85SIngo Molnar 		BUG();
411f2f13a85SIngo Molnar 
412f2f13a85SIngo Molnar 	pte_ref = pte_offset_kernel(pmd_ref, address);
413f2f13a85SIngo Molnar 	if (!pte_present(*pte_ref))
414f2f13a85SIngo Molnar 		return -1;
415f2f13a85SIngo Molnar 
416f2f13a85SIngo Molnar 	pte = pte_offset_kernel(pmd, address);
417f2f13a85SIngo Molnar 
418f2f13a85SIngo Molnar 	/*
419f2f13a85SIngo Molnar 	 * Don't use pte_page here, because the mappings can point
420f2f13a85SIngo Molnar 	 * outside mem_map, and the NUMA hash lookup cannot handle
421f2f13a85SIngo Molnar 	 * that:
422f2f13a85SIngo Molnar 	 */
423f2f13a85SIngo Molnar 	if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
424f2f13a85SIngo Molnar 		BUG();
425f2f13a85SIngo Molnar 
426f2f13a85SIngo Molnar 	return 0;
427f2f13a85SIngo Molnar }
428f2f13a85SIngo Molnar 
429e05139f2SJan Beulich #ifdef CONFIG_CPU_SUP_AMD
430f2f13a85SIngo Molnar static const char errata93_warning[] =
431ad361c98SJoe Perches KERN_ERR
432ad361c98SJoe Perches "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
433ad361c98SJoe Perches "******* Working around it, but it may cause SEGVs or burn power.\n"
434ad361c98SJoe Perches "******* Please consider a BIOS update.\n"
435ad361c98SJoe Perches "******* Disabling USB legacy in the BIOS may also help.\n";
436e05139f2SJan Beulich #endif
437f2f13a85SIngo Molnar 
438f2f13a85SIngo Molnar /*
439f2f13a85SIngo Molnar  * No vm86 mode in 64-bit mode:
440f2f13a85SIngo Molnar  */
441f2f13a85SIngo Molnar static inline void
442f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address,
443f2f13a85SIngo Molnar 		 struct task_struct *tsk)
444f2f13a85SIngo Molnar {
445f2f13a85SIngo Molnar }
446f2f13a85SIngo Molnar 
447f2f13a85SIngo Molnar static int bad_address(void *p)
448f2f13a85SIngo Molnar {
449f2f13a85SIngo Molnar 	unsigned long dummy;
450f2f13a85SIngo Molnar 
451f2f13a85SIngo Molnar 	return probe_kernel_address((unsigned long *)p, dummy);
452f2f13a85SIngo Molnar }
453f2f13a85SIngo Molnar 
454f2f13a85SIngo Molnar static void dump_pagetable(unsigned long address)
455f2f13a85SIngo Molnar {
456087975b0SAkinobu Mita 	pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
457087975b0SAkinobu Mita 	pgd_t *pgd = base + pgd_index(address);
458c61e211dSHarvey Harrison 	pud_t *pud;
459c61e211dSHarvey Harrison 	pmd_t *pmd;
460c61e211dSHarvey Harrison 	pte_t *pte;
461c61e211dSHarvey Harrison 
4622d4a7167SIngo Molnar 	if (bad_address(pgd))
4632d4a7167SIngo Molnar 		goto bad;
4642d4a7167SIngo Molnar 
465c61e211dSHarvey Harrison 	printk("PGD %lx ", pgd_val(*pgd));
4662d4a7167SIngo Molnar 
4672d4a7167SIngo Molnar 	if (!pgd_present(*pgd))
4682d4a7167SIngo Molnar 		goto out;
469c61e211dSHarvey Harrison 
470c61e211dSHarvey Harrison 	pud = pud_offset(pgd, address);
4712d4a7167SIngo Molnar 	if (bad_address(pud))
4722d4a7167SIngo Molnar 		goto bad;
4732d4a7167SIngo Molnar 
474c61e211dSHarvey Harrison 	printk("PUD %lx ", pud_val(*pud));
475b5360222SAndi Kleen 	if (!pud_present(*pud) || pud_large(*pud))
4762d4a7167SIngo Molnar 		goto out;
477c61e211dSHarvey Harrison 
478c61e211dSHarvey Harrison 	pmd = pmd_offset(pud, address);
4792d4a7167SIngo Molnar 	if (bad_address(pmd))
4802d4a7167SIngo Molnar 		goto bad;
4812d4a7167SIngo Molnar 
482c61e211dSHarvey Harrison 	printk("PMD %lx ", pmd_val(*pmd));
4832d4a7167SIngo Molnar 	if (!pmd_present(*pmd) || pmd_large(*pmd))
4842d4a7167SIngo Molnar 		goto out;
485c61e211dSHarvey Harrison 
486c61e211dSHarvey Harrison 	pte = pte_offset_kernel(pmd, address);
4872d4a7167SIngo Molnar 	if (bad_address(pte))
4882d4a7167SIngo Molnar 		goto bad;
4892d4a7167SIngo Molnar 
490c61e211dSHarvey Harrison 	printk("PTE %lx", pte_val(*pte));
4912d4a7167SIngo Molnar out:
492c61e211dSHarvey Harrison 	printk("\n");
493c61e211dSHarvey Harrison 	return;
494c61e211dSHarvey Harrison bad:
495c61e211dSHarvey Harrison 	printk("BAD\n");
496c61e211dSHarvey Harrison }
497c61e211dSHarvey Harrison 
498f2f13a85SIngo Molnar #endif /* CONFIG_X86_64 */
499c61e211dSHarvey Harrison 
5002d4a7167SIngo Molnar /*
5012d4a7167SIngo Molnar  * Workaround for K8 erratum #93 & buggy BIOS.
5022d4a7167SIngo Molnar  *
5032d4a7167SIngo Molnar  * BIOS SMM functions are required to use a specific workaround
5042d4a7167SIngo Molnar  * to avoid corruption of the 64bit RIP register on C stepping K8.
5052d4a7167SIngo Molnar  *
5062d4a7167SIngo Molnar  * A lot of BIOS that didn't get tested properly miss this.
5072d4a7167SIngo Molnar  *
5082d4a7167SIngo Molnar  * The OS sees this as a page fault with the upper 32bits of RIP cleared.
5092d4a7167SIngo Molnar  * Try to work around it here.
5102d4a7167SIngo Molnar  *
5112d4a7167SIngo Molnar  * Note we only handle faults in kernel here.
5122d4a7167SIngo Molnar  * Does nothing on 32-bit.
513c61e211dSHarvey Harrison  */
514c61e211dSHarvey Harrison static int is_errata93(struct pt_regs *regs, unsigned long address)
515c61e211dSHarvey Harrison {
516e05139f2SJan Beulich #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
517e05139f2SJan Beulich 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
518e05139f2SJan Beulich 	    || boot_cpu_data.x86 != 0xf)
519e05139f2SJan Beulich 		return 0;
520e05139f2SJan Beulich 
521c61e211dSHarvey Harrison 	if (address != regs->ip)
522c61e211dSHarvey Harrison 		return 0;
5232d4a7167SIngo Molnar 
524c61e211dSHarvey Harrison 	if ((address >> 32) != 0)
525c61e211dSHarvey Harrison 		return 0;
5262d4a7167SIngo Molnar 
527c61e211dSHarvey Harrison 	address |= 0xffffffffUL << 32;
528c61e211dSHarvey Harrison 	if ((address >= (u64)_stext && address <= (u64)_etext) ||
529c61e211dSHarvey Harrison 	    (address >= MODULES_VADDR && address <= MODULES_END)) {
530a454ab31SIngo Molnar 		printk_once(errata93_warning);
531c61e211dSHarvey Harrison 		regs->ip = address;
532c61e211dSHarvey Harrison 		return 1;
533c61e211dSHarvey Harrison 	}
534c61e211dSHarvey Harrison #endif
535c61e211dSHarvey Harrison 	return 0;
536c61e211dSHarvey Harrison }
537c61e211dSHarvey Harrison 
538c61e211dSHarvey Harrison /*
5392d4a7167SIngo Molnar  * Work around K8 erratum #100 K8 in compat mode occasionally jumps
5402d4a7167SIngo Molnar  * to illegal addresses >4GB.
5412d4a7167SIngo Molnar  *
5422d4a7167SIngo Molnar  * We catch this in the page fault handler because these addresses
5432d4a7167SIngo Molnar  * are not reachable. Just detect this case and return.  Any code
544c61e211dSHarvey Harrison  * segment in LDT is compatibility mode.
545c61e211dSHarvey Harrison  */
546c61e211dSHarvey Harrison static int is_errata100(struct pt_regs *regs, unsigned long address)
547c61e211dSHarvey Harrison {
548c61e211dSHarvey Harrison #ifdef CONFIG_X86_64
5492d4a7167SIngo Molnar 	if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
550c61e211dSHarvey Harrison 		return 1;
551c61e211dSHarvey Harrison #endif
552c61e211dSHarvey Harrison 	return 0;
553c61e211dSHarvey Harrison }
554c61e211dSHarvey Harrison 
555c61e211dSHarvey Harrison static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
556c61e211dSHarvey Harrison {
557c61e211dSHarvey Harrison #ifdef CONFIG_X86_F00F_BUG
558c61e211dSHarvey Harrison 	unsigned long nr;
5592d4a7167SIngo Molnar 
560c61e211dSHarvey Harrison 	/*
5612d4a7167SIngo Molnar 	 * Pentium F0 0F C7 C8 bug workaround:
562c61e211dSHarvey Harrison 	 */
563e2604b49SBorislav Petkov 	if (boot_cpu_has_bug(X86_BUG_F00F)) {
564c61e211dSHarvey Harrison 		nr = (address - idt_descr.address) >> 3;
565c61e211dSHarvey Harrison 
566c61e211dSHarvey Harrison 		if (nr == 6) {
567c61e211dSHarvey Harrison 			do_invalid_op(regs, 0);
568c61e211dSHarvey Harrison 			return 1;
569c61e211dSHarvey Harrison 		}
570c61e211dSHarvey Harrison 	}
571c61e211dSHarvey Harrison #endif
572c61e211dSHarvey Harrison 	return 0;
573c61e211dSHarvey Harrison }
574c61e211dSHarvey Harrison 
5758f766149SIngo Molnar static const char nx_warning[] = KERN_CRIT
5768f766149SIngo Molnar "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
5778f766149SIngo Molnar 
5782d4a7167SIngo Molnar static void
5792d4a7167SIngo Molnar show_fault_oops(struct pt_regs *regs, unsigned long error_code,
580c61e211dSHarvey Harrison 		unsigned long address)
581c61e211dSHarvey Harrison {
582c61e211dSHarvey Harrison 	if (!oops_may_print())
583c61e211dSHarvey Harrison 		return;
584c61e211dSHarvey Harrison 
585c61e211dSHarvey Harrison 	if (error_code & PF_INSTR) {
58693809be8SHarvey Harrison 		unsigned int level;
587*426e34ccSMatt Fleming 		pgd_t *pgd;
588*426e34ccSMatt Fleming 		pte_t *pte;
5892d4a7167SIngo Molnar 
590*426e34ccSMatt Fleming 		pgd = __va(read_cr3() & PHYSICAL_PAGE_MASK);
591*426e34ccSMatt Fleming 		pgd += pgd_index(address);
592*426e34ccSMatt Fleming 
593*426e34ccSMatt Fleming 		pte = lookup_address_in_pgd(pgd, address, &level);
594c61e211dSHarvey Harrison 
5958f766149SIngo Molnar 		if (pte && pte_present(*pte) && !pte_exec(*pte))
596078de5f7SEric W. Biederman 			printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
597c61e211dSHarvey Harrison 	}
598fd40d6e3SHarvey Harrison 
599c61e211dSHarvey Harrison 	printk(KERN_ALERT "BUG: unable to handle kernel ");
600c61e211dSHarvey Harrison 	if (address < PAGE_SIZE)
601c61e211dSHarvey Harrison 		printk(KERN_CONT "NULL pointer dereference");
602c61e211dSHarvey Harrison 	else
603c61e211dSHarvey Harrison 		printk(KERN_CONT "paging request");
6042d4a7167SIngo Molnar 
605f294a8ceSVegard Nossum 	printk(KERN_CONT " at %p\n", (void *) address);
606c61e211dSHarvey Harrison 	printk(KERN_ALERT "IP:");
6075f01c988SJiri Slaby 	printk_address(regs->ip);
6082d4a7167SIngo Molnar 
609c61e211dSHarvey Harrison 	dump_pagetable(address);
610c61e211dSHarvey Harrison }
611c61e211dSHarvey Harrison 
6122d4a7167SIngo Molnar static noinline void
6132d4a7167SIngo Molnar pgtable_bad(struct pt_regs *regs, unsigned long error_code,
6142d4a7167SIngo Molnar 	    unsigned long address)
615c61e211dSHarvey Harrison {
6162d4a7167SIngo Molnar 	struct task_struct *tsk;
6172d4a7167SIngo Molnar 	unsigned long flags;
6182d4a7167SIngo Molnar 	int sig;
6192d4a7167SIngo Molnar 
6202d4a7167SIngo Molnar 	flags = oops_begin();
6212d4a7167SIngo Molnar 	tsk = current;
6222d4a7167SIngo Molnar 	sig = SIGKILL;
623c61e211dSHarvey Harrison 
624c61e211dSHarvey Harrison 	printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
62592181f19SNick Piggin 	       tsk->comm, address);
626c61e211dSHarvey Harrison 	dump_pagetable(address);
6272d4a7167SIngo Molnar 
628c61e211dSHarvey Harrison 	tsk->thread.cr2		= address;
62951e7dc70SSrikar Dronamraju 	tsk->thread.trap_nr	= X86_TRAP_PF;
630c61e211dSHarvey Harrison 	tsk->thread.error_code	= error_code;
6312d4a7167SIngo Molnar 
632c61e211dSHarvey Harrison 	if (__die("Bad pagetable", regs, error_code))
633874d93d1SAlexander van Heukelum 		sig = 0;
6342d4a7167SIngo Molnar 
635874d93d1SAlexander van Heukelum 	oops_end(flags, regs, sig);
636c61e211dSHarvey Harrison }
637c61e211dSHarvey Harrison 
6382d4a7167SIngo Molnar static noinline void
6392d4a7167SIngo Molnar no_context(struct pt_regs *regs, unsigned long error_code,
6404fc34901SAndy Lutomirski 	   unsigned long address, int signal, int si_code)
64192181f19SNick Piggin {
64292181f19SNick Piggin 	struct task_struct *tsk = current;
64319803078SIngo Molnar 	unsigned long *stackend;
64492181f19SNick Piggin 	unsigned long flags;
64592181f19SNick Piggin 	int sig;
64692181f19SNick Piggin 
64792181f19SNick Piggin 	/* Are we prepared to handle this kernel fault? */
6484fc34901SAndy Lutomirski 	if (fixup_exception(regs)) {
649c026b359SPeter Zijlstra 		/*
650c026b359SPeter Zijlstra 		 * Any interrupt that takes a fault gets the fixup. This makes
651c026b359SPeter Zijlstra 		 * the below recursive fault logic only apply to a faults from
652c026b359SPeter Zijlstra 		 * task context.
653c026b359SPeter Zijlstra 		 */
654c026b359SPeter Zijlstra 		if (in_interrupt())
655c026b359SPeter Zijlstra 			return;
656c026b359SPeter Zijlstra 
657c026b359SPeter Zijlstra 		/*
658c026b359SPeter Zijlstra 		 * Per the above we're !in_interrupt(), aka. task context.
659c026b359SPeter Zijlstra 		 *
660c026b359SPeter Zijlstra 		 * In this case we need to make sure we're not recursively
661c026b359SPeter Zijlstra 		 * faulting through the emulate_vsyscall() logic.
662c026b359SPeter Zijlstra 		 */
6634fc34901SAndy Lutomirski 		if (current_thread_info()->sig_on_uaccess_error && signal) {
66451e7dc70SSrikar Dronamraju 			tsk->thread.trap_nr = X86_TRAP_PF;
6654fc34901SAndy Lutomirski 			tsk->thread.error_code = error_code | PF_USER;
6664fc34901SAndy Lutomirski 			tsk->thread.cr2 = address;
6674fc34901SAndy Lutomirski 
6684fc34901SAndy Lutomirski 			/* XXX: hwpoison faults will set the wrong code. */
6694fc34901SAndy Lutomirski 			force_sig_info_fault(signal, si_code, address, tsk, 0);
6704fc34901SAndy Lutomirski 		}
671c026b359SPeter Zijlstra 
672c026b359SPeter Zijlstra 		/*
673c026b359SPeter Zijlstra 		 * Barring that, we can do the fixup and be happy.
674c026b359SPeter Zijlstra 		 */
67592181f19SNick Piggin 		return;
6764fc34901SAndy Lutomirski 	}
67792181f19SNick Piggin 
67892181f19SNick Piggin 	/*
6792d4a7167SIngo Molnar 	 * 32-bit:
6802d4a7167SIngo Molnar 	 *
68192181f19SNick Piggin 	 *   Valid to do another page fault here, because if this fault
68292181f19SNick Piggin 	 *   had been triggered by is_prefetch fixup_exception would have
68392181f19SNick Piggin 	 *   handled it.
68492181f19SNick Piggin 	 *
6852d4a7167SIngo Molnar 	 * 64-bit:
6862d4a7167SIngo Molnar 	 *
68792181f19SNick Piggin 	 *   Hall of shame of CPU/BIOS bugs.
68892181f19SNick Piggin 	 */
68992181f19SNick Piggin 	if (is_prefetch(regs, error_code, address))
69092181f19SNick Piggin 		return;
69192181f19SNick Piggin 
69292181f19SNick Piggin 	if (is_errata93(regs, address))
69392181f19SNick Piggin 		return;
69492181f19SNick Piggin 
69592181f19SNick Piggin 	/*
69692181f19SNick Piggin 	 * Oops. The kernel tried to access some bad page. We'll have to
6972d4a7167SIngo Molnar 	 * terminate things with extreme prejudice:
69892181f19SNick Piggin 	 */
69992181f19SNick Piggin 	flags = oops_begin();
70092181f19SNick Piggin 
70192181f19SNick Piggin 	show_fault_oops(regs, error_code, address);
70292181f19SNick Piggin 
70319803078SIngo Molnar 	stackend = end_of_stack(tsk);
7040e7810beSJan Beulich 	if (tsk != &init_task && *stackend != STACK_END_MAGIC)
705b0f4c4b3SPrarit Bhargava 		printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
70619803078SIngo Molnar 
70792181f19SNick Piggin 	tsk->thread.cr2		= address;
70851e7dc70SSrikar Dronamraju 	tsk->thread.trap_nr	= X86_TRAP_PF;
70992181f19SNick Piggin 	tsk->thread.error_code	= error_code;
71092181f19SNick Piggin 
71192181f19SNick Piggin 	sig = SIGKILL;
71292181f19SNick Piggin 	if (__die("Oops", regs, error_code))
71392181f19SNick Piggin 		sig = 0;
7142d4a7167SIngo Molnar 
71592181f19SNick Piggin 	/* Executive summary in case the body of the oops scrolled away */
716b0f4c4b3SPrarit Bhargava 	printk(KERN_DEFAULT "CR2: %016lx\n", address);
7172d4a7167SIngo Molnar 
71892181f19SNick Piggin 	oops_end(flags, regs, sig);
71992181f19SNick Piggin }
72092181f19SNick Piggin 
7212d4a7167SIngo Molnar /*
7222d4a7167SIngo Molnar  * Print out info about fatal segfaults, if the show_unhandled_signals
7232d4a7167SIngo Molnar  * sysctl is set:
7242d4a7167SIngo Molnar  */
7252d4a7167SIngo Molnar static inline void
7262d4a7167SIngo Molnar show_signal_msg(struct pt_regs *regs, unsigned long error_code,
7272d4a7167SIngo Molnar 		unsigned long address, struct task_struct *tsk)
7282d4a7167SIngo Molnar {
7292d4a7167SIngo Molnar 	if (!unhandled_signal(tsk, SIGSEGV))
7302d4a7167SIngo Molnar 		return;
7312d4a7167SIngo Molnar 
7322d4a7167SIngo Molnar 	if (!printk_ratelimit())
7332d4a7167SIngo Molnar 		return;
7342d4a7167SIngo Molnar 
735a1a08d1cSRoland Dreier 	printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
7362d4a7167SIngo Molnar 		task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
7372d4a7167SIngo Molnar 		tsk->comm, task_pid_nr(tsk), address,
7382d4a7167SIngo Molnar 		(void *)regs->ip, (void *)regs->sp, error_code);
7392d4a7167SIngo Molnar 
7402d4a7167SIngo Molnar 	print_vma_addr(KERN_CONT " in ", regs->ip);
7412d4a7167SIngo Molnar 
7422d4a7167SIngo Molnar 	printk(KERN_CONT "\n");
7432d4a7167SIngo Molnar }
7442d4a7167SIngo Molnar 
7452d4a7167SIngo Molnar static void
7462d4a7167SIngo Molnar __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
7472d4a7167SIngo Molnar 		       unsigned long address, int si_code)
74892181f19SNick Piggin {
74992181f19SNick Piggin 	struct task_struct *tsk = current;
75092181f19SNick Piggin 
75192181f19SNick Piggin 	/* User mode accesses just cause a SIGSEGV */
75292181f19SNick Piggin 	if (error_code & PF_USER) {
75392181f19SNick Piggin 		/*
7542d4a7167SIngo Molnar 		 * It's possible to have interrupts off here:
75592181f19SNick Piggin 		 */
75692181f19SNick Piggin 		local_irq_enable();
75792181f19SNick Piggin 
75892181f19SNick Piggin 		/*
75992181f19SNick Piggin 		 * Valid to do another page fault here because this one came
7602d4a7167SIngo Molnar 		 * from user space:
76192181f19SNick Piggin 		 */
76292181f19SNick Piggin 		if (is_prefetch(regs, error_code, address))
76392181f19SNick Piggin 			return;
76492181f19SNick Piggin 
76592181f19SNick Piggin 		if (is_errata100(regs, address))
76692181f19SNick Piggin 			return;
76792181f19SNick Piggin 
7683ae36655SAndy Lutomirski #ifdef CONFIG_X86_64
7693ae36655SAndy Lutomirski 		/*
7703ae36655SAndy Lutomirski 		 * Instruction fetch faults in the vsyscall page might need
7713ae36655SAndy Lutomirski 		 * emulation.
7723ae36655SAndy Lutomirski 		 */
7733ae36655SAndy Lutomirski 		if (unlikely((error_code & PF_INSTR) &&
7743ae36655SAndy Lutomirski 			     ((address & ~0xfff) == VSYSCALL_START))) {
7753ae36655SAndy Lutomirski 			if (emulate_vsyscall(regs, address))
7763ae36655SAndy Lutomirski 				return;
7773ae36655SAndy Lutomirski 		}
7783ae36655SAndy Lutomirski #endif
779e575a86fSKees Cook 		/* Kernel addresses are always protection faults: */
780e575a86fSKees Cook 		if (address >= TASK_SIZE)
781e575a86fSKees Cook 			error_code |= PF_PROT;
7823ae36655SAndy Lutomirski 
783e575a86fSKees Cook 		if (likely(show_unhandled_signals))
7842d4a7167SIngo Molnar 			show_signal_msg(regs, error_code, address, tsk);
78592181f19SNick Piggin 
78692181f19SNick Piggin 		tsk->thread.cr2		= address;
787e575a86fSKees Cook 		tsk->thread.error_code	= error_code;
78851e7dc70SSrikar Dronamraju 		tsk->thread.trap_nr	= X86_TRAP_PF;
7892d4a7167SIngo Molnar 
790f672b49bSAndi Kleen 		force_sig_info_fault(SIGSEGV, si_code, address, tsk, 0);
7912d4a7167SIngo Molnar 
79292181f19SNick Piggin 		return;
79392181f19SNick Piggin 	}
79492181f19SNick Piggin 
79592181f19SNick Piggin 	if (is_f00f_bug(regs, address))
79692181f19SNick Piggin 		return;
79792181f19SNick Piggin 
7984fc34901SAndy Lutomirski 	no_context(regs, error_code, address, SIGSEGV, si_code);
79992181f19SNick Piggin }
80092181f19SNick Piggin 
8012d4a7167SIngo Molnar static noinline void
8022d4a7167SIngo Molnar bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
8032d4a7167SIngo Molnar 		     unsigned long address)
80492181f19SNick Piggin {
80592181f19SNick Piggin 	__bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
80692181f19SNick Piggin }
80792181f19SNick Piggin 
8082d4a7167SIngo Molnar static void
8092d4a7167SIngo Molnar __bad_area(struct pt_regs *regs, unsigned long error_code,
8102d4a7167SIngo Molnar 	   unsigned long address, int si_code)
81192181f19SNick Piggin {
81292181f19SNick Piggin 	struct mm_struct *mm = current->mm;
81392181f19SNick Piggin 
81492181f19SNick Piggin 	/*
81592181f19SNick Piggin 	 * Something tried to access memory that isn't in our memory map..
81692181f19SNick Piggin 	 * Fix it, but check if it's kernel or user first..
81792181f19SNick Piggin 	 */
81892181f19SNick Piggin 	up_read(&mm->mmap_sem);
81992181f19SNick Piggin 
82092181f19SNick Piggin 	__bad_area_nosemaphore(regs, error_code, address, si_code);
82192181f19SNick Piggin }
82292181f19SNick Piggin 
8232d4a7167SIngo Molnar static noinline void
8242d4a7167SIngo Molnar bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
82592181f19SNick Piggin {
82692181f19SNick Piggin 	__bad_area(regs, error_code, address, SEGV_MAPERR);
82792181f19SNick Piggin }
82892181f19SNick Piggin 
8292d4a7167SIngo Molnar static noinline void
8302d4a7167SIngo Molnar bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
8312d4a7167SIngo Molnar 		      unsigned long address)
83292181f19SNick Piggin {
83392181f19SNick Piggin 	__bad_area(regs, error_code, address, SEGV_ACCERR);
83492181f19SNick Piggin }
83592181f19SNick Piggin 
8362d4a7167SIngo Molnar static void
837a6e04aa9SAndi Kleen do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
838a6e04aa9SAndi Kleen 	  unsigned int fault)
83992181f19SNick Piggin {
84092181f19SNick Piggin 	struct task_struct *tsk = current;
84192181f19SNick Piggin 	struct mm_struct *mm = tsk->mm;
842a6e04aa9SAndi Kleen 	int code = BUS_ADRERR;
84392181f19SNick Piggin 
84492181f19SNick Piggin 	up_read(&mm->mmap_sem);
84592181f19SNick Piggin 
8462d4a7167SIngo Molnar 	/* Kernel mode? Handle exceptions or die: */
84796054569SLinus Torvalds 	if (!(error_code & PF_USER)) {
8484fc34901SAndy Lutomirski 		no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
84996054569SLinus Torvalds 		return;
85096054569SLinus Torvalds 	}
8512d4a7167SIngo Molnar 
852cd1b68f0SIngo Molnar 	/* User-space => ok to do another page fault: */
85392181f19SNick Piggin 	if (is_prefetch(regs, error_code, address))
85492181f19SNick Piggin 		return;
8552d4a7167SIngo Molnar 
85692181f19SNick Piggin 	tsk->thread.cr2		= address;
85792181f19SNick Piggin 	tsk->thread.error_code	= error_code;
85851e7dc70SSrikar Dronamraju 	tsk->thread.trap_nr	= X86_TRAP_PF;
8592d4a7167SIngo Molnar 
860a6e04aa9SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE
861f672b49bSAndi Kleen 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
862a6e04aa9SAndi Kleen 		printk(KERN_ERR
863a6e04aa9SAndi Kleen 	"MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
864a6e04aa9SAndi Kleen 			tsk->comm, tsk->pid, address);
865a6e04aa9SAndi Kleen 		code = BUS_MCEERR_AR;
866a6e04aa9SAndi Kleen 	}
867a6e04aa9SAndi Kleen #endif
868f672b49bSAndi Kleen 	force_sig_info_fault(SIGBUS, code, address, tsk, fault);
86992181f19SNick Piggin }
87092181f19SNick Piggin 
8713a13c4d7SJohannes Weiner static noinline void
8722d4a7167SIngo Molnar mm_fault_error(struct pt_regs *regs, unsigned long error_code,
8732d4a7167SIngo Molnar 	       unsigned long address, unsigned int fault)
87492181f19SNick Piggin {
8753a13c4d7SJohannes Weiner 	if (fatal_signal_pending(current) && !(error_code & PF_USER)) {
876b80ef10eSKOSAKI Motohiro 		up_read(&current->mm->mmap_sem);
8774fc34901SAndy Lutomirski 		no_context(regs, error_code, address, 0, 0);
8783a13c4d7SJohannes Weiner 		return;
879b80ef10eSKOSAKI Motohiro 	}
880b80ef10eSKOSAKI Motohiro 
8812d4a7167SIngo Molnar 	if (fault & VM_FAULT_OOM) {
882f8626854SAndrey Vagin 		/* Kernel mode? Handle exceptions or die: */
883f8626854SAndrey Vagin 		if (!(error_code & PF_USER)) {
884f8626854SAndrey Vagin 			up_read(&current->mm->mmap_sem);
8854fc34901SAndy Lutomirski 			no_context(regs, error_code, address,
8864fc34901SAndy Lutomirski 				   SIGSEGV, SEGV_MAPERR);
8873a13c4d7SJohannes Weiner 			return;
888f8626854SAndrey Vagin 		}
889f8626854SAndrey Vagin 
890c2d23f91SDavid Rientjes 		up_read(&current->mm->mmap_sem);
891c2d23f91SDavid Rientjes 
892c2d23f91SDavid Rientjes 		/*
893c2d23f91SDavid Rientjes 		 * We ran out of memory, call the OOM killer, and return the
894c2d23f91SDavid Rientjes 		 * userspace (which will retry the fault, or kill us if we got
895c2d23f91SDavid Rientjes 		 * oom-killed):
896c2d23f91SDavid Rientjes 		 */
897c2d23f91SDavid Rientjes 		pagefault_out_of_memory();
8982d4a7167SIngo Molnar 	} else {
899f672b49bSAndi Kleen 		if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
900f672b49bSAndi Kleen 			     VM_FAULT_HWPOISON_LARGE))
901a6e04aa9SAndi Kleen 			do_sigbus(regs, error_code, address, fault);
90292181f19SNick Piggin 		else
90392181f19SNick Piggin 			BUG();
90492181f19SNick Piggin 	}
9052d4a7167SIngo Molnar }
90692181f19SNick Piggin 
907d8b57bb7SThomas Gleixner static int spurious_fault_check(unsigned long error_code, pte_t *pte)
908d8b57bb7SThomas Gleixner {
909d8b57bb7SThomas Gleixner 	if ((error_code & PF_WRITE) && !pte_write(*pte))
910d8b57bb7SThomas Gleixner 		return 0;
9112d4a7167SIngo Molnar 
912d8b57bb7SThomas Gleixner 	if ((error_code & PF_INSTR) && !pte_exec(*pte))
913d8b57bb7SThomas Gleixner 		return 0;
914d8b57bb7SThomas Gleixner 
915d8b57bb7SThomas Gleixner 	return 1;
916d8b57bb7SThomas Gleixner }
917d8b57bb7SThomas Gleixner 
918c61e211dSHarvey Harrison /*
9192d4a7167SIngo Molnar  * Handle a spurious fault caused by a stale TLB entry.
9202d4a7167SIngo Molnar  *
9212d4a7167SIngo Molnar  * This allows us to lazily refresh the TLB when increasing the
9222d4a7167SIngo Molnar  * permissions of a kernel page (RO -> RW or NX -> X).  Doing it
9232d4a7167SIngo Molnar  * eagerly is very expensive since that implies doing a full
9242d4a7167SIngo Molnar  * cross-processor TLB flush, even if no stale TLB entries exist
9252d4a7167SIngo Molnar  * on other processors.
9262d4a7167SIngo Molnar  *
9275b727a3bSJeremy Fitzhardinge  * There are no security implications to leaving a stale TLB when
9285b727a3bSJeremy Fitzhardinge  * increasing the permissions on a page.
9295b727a3bSJeremy Fitzhardinge  */
93062c9295fSMasami Hiramatsu static noinline __kprobes int
9312d4a7167SIngo Molnar spurious_fault(unsigned long error_code, unsigned long address)
9325b727a3bSJeremy Fitzhardinge {
9335b727a3bSJeremy Fitzhardinge 	pgd_t *pgd;
9345b727a3bSJeremy Fitzhardinge 	pud_t *pud;
9355b727a3bSJeremy Fitzhardinge 	pmd_t *pmd;
9365b727a3bSJeremy Fitzhardinge 	pte_t *pte;
9373c3e5694SSteven Rostedt 	int ret;
9385b727a3bSJeremy Fitzhardinge 
9395b727a3bSJeremy Fitzhardinge 	/* Reserved-bit violation or user access to kernel space? */
9405b727a3bSJeremy Fitzhardinge 	if (error_code & (PF_USER | PF_RSVD))
9415b727a3bSJeremy Fitzhardinge 		return 0;
9425b727a3bSJeremy Fitzhardinge 
9435b727a3bSJeremy Fitzhardinge 	pgd = init_mm.pgd + pgd_index(address);
9445b727a3bSJeremy Fitzhardinge 	if (!pgd_present(*pgd))
9455b727a3bSJeremy Fitzhardinge 		return 0;
9465b727a3bSJeremy Fitzhardinge 
9475b727a3bSJeremy Fitzhardinge 	pud = pud_offset(pgd, address);
9485b727a3bSJeremy Fitzhardinge 	if (!pud_present(*pud))
9495b727a3bSJeremy Fitzhardinge 		return 0;
9505b727a3bSJeremy Fitzhardinge 
951d8b57bb7SThomas Gleixner 	if (pud_large(*pud))
952d8b57bb7SThomas Gleixner 		return spurious_fault_check(error_code, (pte_t *) pud);
953d8b57bb7SThomas Gleixner 
9545b727a3bSJeremy Fitzhardinge 	pmd = pmd_offset(pud, address);
9555b727a3bSJeremy Fitzhardinge 	if (!pmd_present(*pmd))
9565b727a3bSJeremy Fitzhardinge 		return 0;
9575b727a3bSJeremy Fitzhardinge 
958d8b57bb7SThomas Gleixner 	if (pmd_large(*pmd))
959d8b57bb7SThomas Gleixner 		return spurious_fault_check(error_code, (pte_t *) pmd);
960d8b57bb7SThomas Gleixner 
9615b727a3bSJeremy Fitzhardinge 	pte = pte_offset_kernel(pmd, address);
962954f8571SAndrea Arcangeli 	if (!pte_present(*pte))
9635b727a3bSJeremy Fitzhardinge 		return 0;
9645b727a3bSJeremy Fitzhardinge 
9653c3e5694SSteven Rostedt 	ret = spurious_fault_check(error_code, pte);
9663c3e5694SSteven Rostedt 	if (!ret)
9673c3e5694SSteven Rostedt 		return 0;
9683c3e5694SSteven Rostedt 
9693c3e5694SSteven Rostedt 	/*
9702d4a7167SIngo Molnar 	 * Make sure we have permissions in PMD.
9712d4a7167SIngo Molnar 	 * If not, then there's a bug in the page tables:
9723c3e5694SSteven Rostedt 	 */
9733c3e5694SSteven Rostedt 	ret = spurious_fault_check(error_code, (pte_t *) pmd);
9743c3e5694SSteven Rostedt 	WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
9752d4a7167SIngo Molnar 
9763c3e5694SSteven Rostedt 	return ret;
9775b727a3bSJeremy Fitzhardinge }
9785b727a3bSJeremy Fitzhardinge 
979c61e211dSHarvey Harrison int show_unhandled_signals = 1;
980c61e211dSHarvey Harrison 
9812d4a7167SIngo Molnar static inline int
98268da336aSMichel Lespinasse access_error(unsigned long error_code, struct vm_area_struct *vma)
98392181f19SNick Piggin {
98468da336aSMichel Lespinasse 	if (error_code & PF_WRITE) {
9852d4a7167SIngo Molnar 		/* write, present and write, not present: */
98692181f19SNick Piggin 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
98792181f19SNick Piggin 			return 1;
9882d4a7167SIngo Molnar 		return 0;
9892d4a7167SIngo Molnar 	}
9902d4a7167SIngo Molnar 
9912d4a7167SIngo Molnar 	/* read, present: */
9922d4a7167SIngo Molnar 	if (unlikely(error_code & PF_PROT))
99392181f19SNick Piggin 		return 1;
9942d4a7167SIngo Molnar 
9952d4a7167SIngo Molnar 	/* read, not present: */
99692181f19SNick Piggin 	if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
99792181f19SNick Piggin 		return 1;
99892181f19SNick Piggin 
99992181f19SNick Piggin 	return 0;
100092181f19SNick Piggin }
100192181f19SNick Piggin 
10020973a06cSHiroshi Shimamoto static int fault_in_kernel_space(unsigned long address)
10030973a06cSHiroshi Shimamoto {
1004d9517346SIngo Molnar 	return address >= TASK_SIZE_MAX;
10050973a06cSHiroshi Shimamoto }
10060973a06cSHiroshi Shimamoto 
100740d3cd66SH. Peter Anvin static inline bool smap_violation(int error_code, struct pt_regs *regs)
100840d3cd66SH. Peter Anvin {
10094640c7eeSH. Peter Anvin 	if (!IS_ENABLED(CONFIG_X86_SMAP))
10104640c7eeSH. Peter Anvin 		return false;
10114640c7eeSH. Peter Anvin 
10124640c7eeSH. Peter Anvin 	if (!static_cpu_has(X86_FEATURE_SMAP))
10134640c7eeSH. Peter Anvin 		return false;
10144640c7eeSH. Peter Anvin 
101540d3cd66SH. Peter Anvin 	if (error_code & PF_USER)
101640d3cd66SH. Peter Anvin 		return false;
101740d3cd66SH. Peter Anvin 
101840d3cd66SH. Peter Anvin 	if (!user_mode_vm(regs) && (regs->flags & X86_EFLAGS_AC))
101940d3cd66SH. Peter Anvin 		return false;
102040d3cd66SH. Peter Anvin 
102140d3cd66SH. Peter Anvin 	return true;
102240d3cd66SH. Peter Anvin }
102340d3cd66SH. Peter Anvin 
1024c61e211dSHarvey Harrison /*
1025c61e211dSHarvey Harrison  * This routine handles page faults.  It determines the address,
1026c61e211dSHarvey Harrison  * and the problem, and then passes it off to one of the appropriate
1027c61e211dSHarvey Harrison  * routines.
1028c61e211dSHarvey Harrison  */
10296ba3c97aSFrederic Weisbecker static void __kprobes
10306ba3c97aSFrederic Weisbecker __do_page_fault(struct pt_regs *regs, unsigned long error_code)
1031c61e211dSHarvey Harrison {
1032c61e211dSHarvey Harrison 	struct vm_area_struct *vma;
10332d4a7167SIngo Molnar 	struct task_struct *tsk;
10342d4a7167SIngo Molnar 	unsigned long address;
10352d4a7167SIngo Molnar 	struct mm_struct *mm;
1036c61e211dSHarvey Harrison 	int fault;
1037759496baSJohannes Weiner 	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
1038c61e211dSHarvey Harrison 
1039c61e211dSHarvey Harrison 	tsk = current;
1040c61e211dSHarvey Harrison 	mm = tsk->mm;
10412d4a7167SIngo Molnar 
10422d4a7167SIngo Molnar 	/* Get the faulting address: */
1043c61e211dSHarvey Harrison 	address = read_cr2();
1044c61e211dSHarvey Harrison 
1045f8561296SVegard Nossum 	/*
1046f8561296SVegard Nossum 	 * Detect and handle instructions that would cause a page fault for
1047f8561296SVegard Nossum 	 * both a tracked kernel page and a userspace page.
1048f8561296SVegard Nossum 	 */
1049f8561296SVegard Nossum 	if (kmemcheck_active(regs))
1050f8561296SVegard Nossum 		kmemcheck_hide(regs);
10515dfaf90fSIngo Molnar 	prefetchw(&mm->mmap_sem);
1052f8561296SVegard Nossum 
10530fd0e3daSPekka Paalanen 	if (unlikely(kmmio_fault(regs, address)))
105486069782SPekka Paalanen 		return;
1055c61e211dSHarvey Harrison 
1056c61e211dSHarvey Harrison 	/*
1057c61e211dSHarvey Harrison 	 * We fault-in kernel-space virtual memory on-demand. The
1058c61e211dSHarvey Harrison 	 * 'reference' page table is init_mm.pgd.
1059c61e211dSHarvey Harrison 	 *
1060c61e211dSHarvey Harrison 	 * NOTE! We MUST NOT take any locks for this case. We may
1061c61e211dSHarvey Harrison 	 * be in an interrupt or a critical region, and should
1062c61e211dSHarvey Harrison 	 * only copy the information from the master page table,
1063c61e211dSHarvey Harrison 	 * nothing more.
1064c61e211dSHarvey Harrison 	 *
1065c61e211dSHarvey Harrison 	 * This verifies that the fault happens in kernel space
1066c61e211dSHarvey Harrison 	 * (error_code & 4) == 0, and that the fault was not a
1067c61e211dSHarvey Harrison 	 * protection error (error_code & 9) == 0.
1068c61e211dSHarvey Harrison 	 */
10690973a06cSHiroshi Shimamoto 	if (unlikely(fault_in_kernel_space(address))) {
1070f8561296SVegard Nossum 		if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
1071f8561296SVegard Nossum 			if (vmalloc_fault(address) >= 0)
1072c61e211dSHarvey Harrison 				return;
10735b727a3bSJeremy Fitzhardinge 
1074f8561296SVegard Nossum 			if (kmemcheck_fault(regs, address, error_code))
1075f8561296SVegard Nossum 				return;
1076f8561296SVegard Nossum 		}
1077f8561296SVegard Nossum 
10782d4a7167SIngo Molnar 		/* Can handle a stale RO->RW TLB: */
107992181f19SNick Piggin 		if (spurious_fault(error_code, address))
10805b727a3bSJeremy Fitzhardinge 			return;
10815b727a3bSJeremy Fitzhardinge 
10822d4a7167SIngo Molnar 		/* kprobes don't want to hook the spurious faults: */
1083e00b12e6SPeter Zijlstra 		if (kprobes_fault(regs))
10849be260a6SMasami Hiramatsu 			return;
1085c61e211dSHarvey Harrison 		/*
1086c61e211dSHarvey Harrison 		 * Don't take the mm semaphore here. If we fixup a prefetch
10872d4a7167SIngo Molnar 		 * fault we could otherwise deadlock:
1088c61e211dSHarvey Harrison 		 */
108992181f19SNick Piggin 		bad_area_nosemaphore(regs, error_code, address);
10902d4a7167SIngo Molnar 
109192181f19SNick Piggin 		return;
1092c61e211dSHarvey Harrison 	}
1093c61e211dSHarvey Harrison 
10942d4a7167SIngo Molnar 	/* kprobes don't want to hook the spurious faults: */
1095e00b12e6SPeter Zijlstra 	if (unlikely(kprobes_fault(regs)))
10969be260a6SMasami Hiramatsu 		return;
1097e00b12e6SPeter Zijlstra 
1098e00b12e6SPeter Zijlstra 	if (unlikely(error_code & PF_RSVD))
1099e00b12e6SPeter Zijlstra 		pgtable_bad(regs, error_code, address);
1100e00b12e6SPeter Zijlstra 
1101e00b12e6SPeter Zijlstra 	if (unlikely(smap_violation(error_code, regs))) {
1102e00b12e6SPeter Zijlstra 		bad_area_nosemaphore(regs, error_code, address);
1103e00b12e6SPeter Zijlstra 		return;
1104e00b12e6SPeter Zijlstra 	}
1105e00b12e6SPeter Zijlstra 
1106e00b12e6SPeter Zijlstra 	/*
1107e00b12e6SPeter Zijlstra 	 * If we're in an interrupt, have no user context or are running
1108e00b12e6SPeter Zijlstra 	 * in an atomic region then we must not take the fault:
1109e00b12e6SPeter Zijlstra 	 */
1110e00b12e6SPeter Zijlstra 	if (unlikely(in_atomic() || !mm)) {
1111e00b12e6SPeter Zijlstra 		bad_area_nosemaphore(regs, error_code, address);
1112e00b12e6SPeter Zijlstra 		return;
1113e00b12e6SPeter Zijlstra 	}
1114e00b12e6SPeter Zijlstra 
1115c61e211dSHarvey Harrison 	/*
1116891cffbdSLinus Torvalds 	 * It's safe to allow irq's after cr2 has been saved and the
1117891cffbdSLinus Torvalds 	 * vmalloc fault has been handled.
1118891cffbdSLinus Torvalds 	 *
1119891cffbdSLinus Torvalds 	 * User-mode registers count as a user access even for any
11202d4a7167SIngo Molnar 	 * potential system fault or CPU buglet:
1121c61e211dSHarvey Harrison 	 */
1122891cffbdSLinus Torvalds 	if (user_mode_vm(regs)) {
1123891cffbdSLinus Torvalds 		local_irq_enable();
1124891cffbdSLinus Torvalds 		error_code |= PF_USER;
1125759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
11262d4a7167SIngo Molnar 	} else {
11272d4a7167SIngo Molnar 		if (regs->flags & X86_EFLAGS_IF)
1128c61e211dSHarvey Harrison 			local_irq_enable();
11292d4a7167SIngo Molnar 	}
1130c61e211dSHarvey Harrison 
1131a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
11327dd1fcc2SPeter Zijlstra 
1133759496baSJohannes Weiner 	if (error_code & PF_WRITE)
1134759496baSJohannes Weiner 		flags |= FAULT_FLAG_WRITE;
1135759496baSJohannes Weiner 
11363a1dfe6eSIngo Molnar 	/*
11373a1dfe6eSIngo Molnar 	 * When running in the kernel we expect faults to occur only to
11382d4a7167SIngo Molnar 	 * addresses in user space.  All other faults represent errors in
11392d4a7167SIngo Molnar 	 * the kernel and should generate an OOPS.  Unfortunately, in the
11402d4a7167SIngo Molnar 	 * case of an erroneous fault occurring in a code path which already
11412d4a7167SIngo Molnar 	 * holds mmap_sem we will deadlock attempting to validate the fault
11422d4a7167SIngo Molnar 	 * against the address space.  Luckily the kernel only validly
11432d4a7167SIngo Molnar 	 * references user space from well defined areas of code, which are
11442d4a7167SIngo Molnar 	 * listed in the exceptions table.
1145c61e211dSHarvey Harrison 	 *
1146c61e211dSHarvey Harrison 	 * As the vast majority of faults will be valid we will only perform
11472d4a7167SIngo Molnar 	 * the source reference check when there is a possibility of a
11482d4a7167SIngo Molnar 	 * deadlock. Attempt to lock the address space, if we cannot we then
11492d4a7167SIngo Molnar 	 * validate the source. If this is invalid we can skip the address
11502d4a7167SIngo Molnar 	 * space check, thus avoiding the deadlock:
1151c61e211dSHarvey Harrison 	 */
115292181f19SNick Piggin 	if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1153c61e211dSHarvey Harrison 		if ((error_code & PF_USER) == 0 &&
115492181f19SNick Piggin 		    !search_exception_tables(regs->ip)) {
115592181f19SNick Piggin 			bad_area_nosemaphore(regs, error_code, address);
115692181f19SNick Piggin 			return;
115792181f19SNick Piggin 		}
1158d065bd81SMichel Lespinasse retry:
1159c61e211dSHarvey Harrison 		down_read(&mm->mmap_sem);
116001006074SPeter Zijlstra 	} else {
116101006074SPeter Zijlstra 		/*
11622d4a7167SIngo Molnar 		 * The above down_read_trylock() might have succeeded in
11632d4a7167SIngo Molnar 		 * which case we'll have missed the might_sleep() from
11642d4a7167SIngo Molnar 		 * down_read():
116501006074SPeter Zijlstra 		 */
116601006074SPeter Zijlstra 		might_sleep();
1167c61e211dSHarvey Harrison 	}
1168c61e211dSHarvey Harrison 
1169c61e211dSHarvey Harrison 	vma = find_vma(mm, address);
117092181f19SNick Piggin 	if (unlikely(!vma)) {
117192181f19SNick Piggin 		bad_area(regs, error_code, address);
117292181f19SNick Piggin 		return;
117392181f19SNick Piggin 	}
117492181f19SNick Piggin 	if (likely(vma->vm_start <= address))
1175c61e211dSHarvey Harrison 		goto good_area;
117692181f19SNick Piggin 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
117792181f19SNick Piggin 		bad_area(regs, error_code, address);
117892181f19SNick Piggin 		return;
117992181f19SNick Piggin 	}
1180c61e211dSHarvey Harrison 	if (error_code & PF_USER) {
1181c61e211dSHarvey Harrison 		/*
1182c61e211dSHarvey Harrison 		 * Accessing the stack below %sp is always a bug.
1183c61e211dSHarvey Harrison 		 * The large cushion allows instructions like enter
1184c61e211dSHarvey Harrison 		 * and pusha to work. ("enter $65535, $31" pushes
1185c61e211dSHarvey Harrison 		 * 32 pointers and then decrements %sp by 65535.)
1186c61e211dSHarvey Harrison 		 */
118792181f19SNick Piggin 		if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
118892181f19SNick Piggin 			bad_area(regs, error_code, address);
118992181f19SNick Piggin 			return;
1190c61e211dSHarvey Harrison 		}
119192181f19SNick Piggin 	}
119292181f19SNick Piggin 	if (unlikely(expand_stack(vma, address))) {
119392181f19SNick Piggin 		bad_area(regs, error_code, address);
119492181f19SNick Piggin 		return;
119592181f19SNick Piggin 	}
119692181f19SNick Piggin 
1197c61e211dSHarvey Harrison 	/*
1198c61e211dSHarvey Harrison 	 * Ok, we have a good vm_area for this memory access, so
1199c61e211dSHarvey Harrison 	 * we can handle it..
1200c61e211dSHarvey Harrison 	 */
1201c61e211dSHarvey Harrison good_area:
120268da336aSMichel Lespinasse 	if (unlikely(access_error(error_code, vma))) {
120392181f19SNick Piggin 		bad_area_access_error(regs, error_code, address);
120492181f19SNick Piggin 		return;
1205c61e211dSHarvey Harrison 	}
1206c61e211dSHarvey Harrison 
1207c61e211dSHarvey Harrison 	/*
1208c61e211dSHarvey Harrison 	 * If for any reason at all we couldn't handle the fault,
1209c61e211dSHarvey Harrison 	 * make sure we exit gracefully rather than endlessly redo
12102d4a7167SIngo Molnar 	 * the fault:
1211c61e211dSHarvey Harrison 	 */
1212d065bd81SMichel Lespinasse 	fault = handle_mm_fault(mm, vma, address, flags);
12132d4a7167SIngo Molnar 
12143a13c4d7SJohannes Weiner 	/*
12153a13c4d7SJohannes Weiner 	 * If we need to retry but a fatal signal is pending, handle the
12163a13c4d7SJohannes Weiner 	 * signal first. We do not need to release the mmap_sem because it
12173a13c4d7SJohannes Weiner 	 * would already be released in __lock_page_or_retry in mm/filemap.c.
12183a13c4d7SJohannes Weiner 	 */
12193a13c4d7SJohannes Weiner 	if (unlikely((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)))
12203a13c4d7SJohannes Weiner 		return;
12213a13c4d7SJohannes Weiner 
12223a13c4d7SJohannes Weiner 	if (unlikely(fault & VM_FAULT_ERROR)) {
12233a13c4d7SJohannes Weiner 		mm_fault_error(regs, error_code, address, fault);
122437b23e05SKOSAKI Motohiro 		return;
122537b23e05SKOSAKI Motohiro 	}
122637b23e05SKOSAKI Motohiro 
122737b23e05SKOSAKI Motohiro 	/*
1228d065bd81SMichel Lespinasse 	 * Major/minor page fault accounting is only done on the
1229d065bd81SMichel Lespinasse 	 * initial attempt. If we go through a retry, it is extremely
1230d065bd81SMichel Lespinasse 	 * likely that the page will be found in page cache at that point.
1231d065bd81SMichel Lespinasse 	 */
1232d065bd81SMichel Lespinasse 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
1233ac17dc8eSPeter Zijlstra 		if (fault & VM_FAULT_MAJOR) {
1234c61e211dSHarvey Harrison 			tsk->maj_flt++;
1235a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
123678f13e95SPeter Zijlstra 				      regs, address);
1237ac17dc8eSPeter Zijlstra 		} else {
1238c61e211dSHarvey Harrison 			tsk->min_flt++;
1239a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
124078f13e95SPeter Zijlstra 				      regs, address);
1241ac17dc8eSPeter Zijlstra 		}
1242d065bd81SMichel Lespinasse 		if (fault & VM_FAULT_RETRY) {
1243d065bd81SMichel Lespinasse 			/* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
1244d065bd81SMichel Lespinasse 			 * of starvation. */
1245d065bd81SMichel Lespinasse 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
124645cac65bSShaohua Li 			flags |= FAULT_FLAG_TRIED;
1247d065bd81SMichel Lespinasse 			goto retry;
1248d065bd81SMichel Lespinasse 		}
1249d065bd81SMichel Lespinasse 	}
1250c61e211dSHarvey Harrison 
12518c938f9fSIngo Molnar 	check_v8086_mode(regs, address, tsk);
12528c938f9fSIngo Molnar 
1253c61e211dSHarvey Harrison 	up_read(&mm->mmap_sem);
1254c61e211dSHarvey Harrison }
12556ba3c97aSFrederic Weisbecker 
12566ba3c97aSFrederic Weisbecker dotraplinkage void __kprobes
12576ba3c97aSFrederic Weisbecker do_page_fault(struct pt_regs *regs, unsigned long error_code)
12586ba3c97aSFrederic Weisbecker {
12596c1e0256SFrederic Weisbecker 	enum ctx_state prev_state;
12606c1e0256SFrederic Weisbecker 
12616c1e0256SFrederic Weisbecker 	prev_state = exception_enter();
12626ba3c97aSFrederic Weisbecker 	__do_page_fault(regs, error_code);
12636c1e0256SFrederic Weisbecker 	exception_exit(prev_state);
12646ba3c97aSFrederic Weisbecker }
126525c74b10SSeiji Aguchi 
1266d34603b0SSeiji Aguchi static void trace_page_fault_entries(struct pt_regs *regs,
1267d34603b0SSeiji Aguchi 				     unsigned long error_code)
1268d34603b0SSeiji Aguchi {
1269d34603b0SSeiji Aguchi 	if (user_mode(regs))
1270a4f61decSH. Peter Anvin 		trace_page_fault_user(read_cr2(), regs, error_code);
1271d34603b0SSeiji Aguchi 	else
1272a4f61decSH. Peter Anvin 		trace_page_fault_kernel(read_cr2(), regs, error_code);
1273d34603b0SSeiji Aguchi }
1274d34603b0SSeiji Aguchi 
127525c74b10SSeiji Aguchi dotraplinkage void __kprobes
127625c74b10SSeiji Aguchi trace_do_page_fault(struct pt_regs *regs, unsigned long error_code)
127725c74b10SSeiji Aguchi {
127825c74b10SSeiji Aguchi 	enum ctx_state prev_state;
127925c74b10SSeiji Aguchi 
128025c74b10SSeiji Aguchi 	prev_state = exception_enter();
1281d34603b0SSeiji Aguchi 	trace_page_fault_entries(regs, error_code);
128225c74b10SSeiji Aguchi 	__do_page_fault(regs, error_code);
128325c74b10SSeiji Aguchi 	exception_exit(prev_state);
128425c74b10SSeiji Aguchi }
1285