xref: /openbmc/linux/arch/x86/mm/fault.c (revision c2d23f919bafcbc2259f5257d9a7d729802f0e3a)
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			*/
16c61e211dSHarvey Harrison 
17a2bcd473SIngo Molnar #include <asm/traps.h>			/* dotraplinkage, ...		*/
18a2bcd473SIngo Molnar #include <asm/pgalloc.h>		/* pgd_*(), ...			*/
19f8561296SVegard Nossum #include <asm/kmemcheck.h>		/* kmemcheck_*(), ...		*/
20fab1167cSH. Peter Anvin #include <asm/fixmap.h>			/* VSYSCALL_START		*/
2191d1aa43SFrederic Weisbecker #include <asm/context_tracking.h>	/* exception_enter(), ...	*/
22c61e211dSHarvey Harrison 
23c61e211dSHarvey Harrison /*
242d4a7167SIngo Molnar  * Page fault error code bits:
252d4a7167SIngo Molnar  *
262d4a7167SIngo Molnar  *   bit 0 ==	 0: no page found	1: protection fault
272d4a7167SIngo Molnar  *   bit 1 ==	 0: read access		1: write access
282d4a7167SIngo Molnar  *   bit 2 ==	 0: kernel-mode access	1: user-mode access
292d4a7167SIngo Molnar  *   bit 3 ==				1: use of reserved bit detected
302d4a7167SIngo Molnar  *   bit 4 ==				1: fault was an instruction fetch
31c61e211dSHarvey Harrison  */
322d4a7167SIngo Molnar enum x86_pf_error_code {
332d4a7167SIngo Molnar 
342d4a7167SIngo Molnar 	PF_PROT		=		1 << 0,
352d4a7167SIngo Molnar 	PF_WRITE	=		1 << 1,
362d4a7167SIngo Molnar 	PF_USER		=		1 << 2,
372d4a7167SIngo Molnar 	PF_RSVD		=		1 << 3,
382d4a7167SIngo Molnar 	PF_INSTR	=		1 << 4,
392d4a7167SIngo Molnar };
40c61e211dSHarvey Harrison 
41b814d41fSIngo Molnar /*
42b319eed0SIngo Molnar  * Returns 0 if mmiotrace is disabled, or if the fault is not
43b319eed0SIngo Molnar  * handled by mmiotrace:
44b814d41fSIngo Molnar  */
4562c9295fSMasami Hiramatsu static inline int __kprobes
4662c9295fSMasami Hiramatsu kmmio_fault(struct pt_regs *regs, unsigned long addr)
4786069782SPekka Paalanen {
480fd0e3daSPekka Paalanen 	if (unlikely(is_kmmio_active()))
490fd0e3daSPekka Paalanen 		if (kmmio_handler(regs, addr) == 1)
500fd0e3daSPekka Paalanen 			return -1;
510fd0e3daSPekka Paalanen 	return 0;
5286069782SPekka Paalanen }
5386069782SPekka Paalanen 
5462c9295fSMasami Hiramatsu static inline int __kprobes notify_page_fault(struct pt_regs *regs)
55c61e211dSHarvey Harrison {
56c61e211dSHarvey Harrison 	int ret = 0;
57c61e211dSHarvey Harrison 
58c61e211dSHarvey Harrison 	/* kprobe_running() needs smp_processor_id() */
59b1801812SIngo Molnar 	if (kprobes_built_in() && !user_mode_vm(regs)) {
60c61e211dSHarvey Harrison 		preempt_disable();
61c61e211dSHarvey Harrison 		if (kprobe_running() && kprobe_fault_handler(regs, 14))
62c61e211dSHarvey Harrison 			ret = 1;
63c61e211dSHarvey Harrison 		preempt_enable();
64c61e211dSHarvey Harrison 	}
65c61e211dSHarvey Harrison 
66c61e211dSHarvey Harrison 	return ret;
67c61e211dSHarvey Harrison }
68c61e211dSHarvey Harrison 
69c61e211dSHarvey Harrison /*
702d4a7167SIngo Molnar  * Prefetch quirks:
712d4a7167SIngo Molnar  *
722d4a7167SIngo Molnar  * 32-bit mode:
732d4a7167SIngo Molnar  *
74c61e211dSHarvey Harrison  *   Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
75c61e211dSHarvey Harrison  *   Check that here and ignore it.
76c61e211dSHarvey Harrison  *
772d4a7167SIngo Molnar  * 64-bit mode:
782d4a7167SIngo Molnar  *
79c61e211dSHarvey Harrison  *   Sometimes the CPU reports invalid exceptions on prefetch.
80c61e211dSHarvey Harrison  *   Check that here and ignore it.
81c61e211dSHarvey Harrison  *
822d4a7167SIngo Molnar  * Opcode checker based on code by Richard Brunner.
83c61e211dSHarvey Harrison  */
84107a0367SIngo Molnar static inline int
85107a0367SIngo Molnar check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
86107a0367SIngo Molnar 		      unsigned char opcode, int *prefetch)
87c61e211dSHarvey Harrison {
88107a0367SIngo Molnar 	unsigned char instr_hi = opcode & 0xf0;
89107a0367SIngo Molnar 	unsigned char instr_lo = opcode & 0x0f;
90c61e211dSHarvey Harrison 
91c61e211dSHarvey Harrison 	switch (instr_hi) {
92c61e211dSHarvey Harrison 	case 0x20:
93c61e211dSHarvey Harrison 	case 0x30:
94c61e211dSHarvey Harrison 		/*
95c61e211dSHarvey Harrison 		 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
96c61e211dSHarvey Harrison 		 * In X86_64 long mode, the CPU will signal invalid
97c61e211dSHarvey Harrison 		 * opcode if some of these prefixes are present so
98c61e211dSHarvey Harrison 		 * X86_64 will never get here anyway
99c61e211dSHarvey Harrison 		 */
100107a0367SIngo Molnar 		return ((instr_lo & 7) == 0x6);
101c61e211dSHarvey Harrison #ifdef CONFIG_X86_64
102c61e211dSHarvey Harrison 	case 0x40:
103c61e211dSHarvey Harrison 		/*
104c61e211dSHarvey Harrison 		 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
105c61e211dSHarvey Harrison 		 * Need to figure out under what instruction mode the
106c61e211dSHarvey Harrison 		 * instruction was issued. Could check the LDT for lm,
107c61e211dSHarvey Harrison 		 * but for now it's good enough to assume that long
108c61e211dSHarvey Harrison 		 * mode only uses well known segments or kernel.
109c61e211dSHarvey Harrison 		 */
110318f5a2aSAndy Lutomirski 		return (!user_mode(regs) || user_64bit_mode(regs));
111c61e211dSHarvey Harrison #endif
112c61e211dSHarvey Harrison 	case 0x60:
113c61e211dSHarvey Harrison 		/* 0x64 thru 0x67 are valid prefixes in all modes. */
114107a0367SIngo Molnar 		return (instr_lo & 0xC) == 0x4;
115c61e211dSHarvey Harrison 	case 0xF0:
116c61e211dSHarvey Harrison 		/* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
117107a0367SIngo Molnar 		return !instr_lo || (instr_lo>>1) == 1;
118c61e211dSHarvey Harrison 	case 0x00:
119c61e211dSHarvey Harrison 		/* Prefetch instruction is 0x0F0D or 0x0F18 */
120107a0367SIngo Molnar 		if (probe_kernel_address(instr, opcode))
121107a0367SIngo Molnar 			return 0;
122107a0367SIngo Molnar 
123107a0367SIngo Molnar 		*prefetch = (instr_lo == 0xF) &&
124107a0367SIngo Molnar 			(opcode == 0x0D || opcode == 0x18);
125107a0367SIngo Molnar 		return 0;
126107a0367SIngo Molnar 	default:
127107a0367SIngo Molnar 		return 0;
128107a0367SIngo Molnar 	}
129107a0367SIngo Molnar }
130107a0367SIngo Molnar 
131107a0367SIngo Molnar static int
132107a0367SIngo Molnar is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
133107a0367SIngo Molnar {
134107a0367SIngo Molnar 	unsigned char *max_instr;
135107a0367SIngo Molnar 	unsigned char *instr;
136107a0367SIngo Molnar 	int prefetch = 0;
137107a0367SIngo Molnar 
138107a0367SIngo Molnar 	/*
139107a0367SIngo Molnar 	 * If it was a exec (instruction fetch) fault on NX page, then
140107a0367SIngo Molnar 	 * do not ignore the fault:
141107a0367SIngo Molnar 	 */
142107a0367SIngo Molnar 	if (error_code & PF_INSTR)
143107a0367SIngo Molnar 		return 0;
144107a0367SIngo Molnar 
145107a0367SIngo Molnar 	instr = (void *)convert_ip_to_linear(current, regs);
146107a0367SIngo Molnar 	max_instr = instr + 15;
147107a0367SIngo Molnar 
148107a0367SIngo Molnar 	if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
149107a0367SIngo Molnar 		return 0;
150107a0367SIngo Molnar 
151107a0367SIngo Molnar 	while (instr < max_instr) {
152107a0367SIngo Molnar 		unsigned char opcode;
153c61e211dSHarvey Harrison 
154c61e211dSHarvey Harrison 		if (probe_kernel_address(instr, opcode))
155c61e211dSHarvey Harrison 			break;
156107a0367SIngo Molnar 
157107a0367SIngo Molnar 		instr++;
158107a0367SIngo Molnar 
159107a0367SIngo Molnar 		if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
160c61e211dSHarvey Harrison 			break;
161c61e211dSHarvey Harrison 	}
162c61e211dSHarvey Harrison 	return prefetch;
163c61e211dSHarvey Harrison }
164c61e211dSHarvey Harrison 
1652d4a7167SIngo Molnar static void
1662d4a7167SIngo Molnar force_sig_info_fault(int si_signo, int si_code, unsigned long address,
167f672b49bSAndi Kleen 		     struct task_struct *tsk, int fault)
168c61e211dSHarvey Harrison {
169f672b49bSAndi Kleen 	unsigned lsb = 0;
170c61e211dSHarvey Harrison 	siginfo_t info;
171c61e211dSHarvey Harrison 
172c61e211dSHarvey Harrison 	info.si_signo	= si_signo;
173c61e211dSHarvey Harrison 	info.si_errno	= 0;
174c61e211dSHarvey Harrison 	info.si_code	= si_code;
175c61e211dSHarvey Harrison 	info.si_addr	= (void __user *)address;
176f672b49bSAndi Kleen 	if (fault & VM_FAULT_HWPOISON_LARGE)
177f672b49bSAndi Kleen 		lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
178f672b49bSAndi Kleen 	if (fault & VM_FAULT_HWPOISON)
179f672b49bSAndi Kleen 		lsb = PAGE_SHIFT;
180f672b49bSAndi Kleen 	info.si_addr_lsb = lsb;
1812d4a7167SIngo Molnar 
182c61e211dSHarvey Harrison 	force_sig_info(si_signo, &info, tsk);
183c61e211dSHarvey Harrison }
184c61e211dSHarvey Harrison 
185f2f13a85SIngo Molnar DEFINE_SPINLOCK(pgd_lock);
186f2f13a85SIngo Molnar LIST_HEAD(pgd_list);
1872d4a7167SIngo Molnar 
188f2f13a85SIngo Molnar #ifdef CONFIG_X86_32
189f2f13a85SIngo Molnar static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
190f2f13a85SIngo Molnar {
191f2f13a85SIngo Molnar 	unsigned index = pgd_index(address);
192f2f13a85SIngo Molnar 	pgd_t *pgd_k;
193f2f13a85SIngo Molnar 	pud_t *pud, *pud_k;
194f2f13a85SIngo Molnar 	pmd_t *pmd, *pmd_k;
195f2f13a85SIngo Molnar 
196f2f13a85SIngo Molnar 	pgd += index;
197f2f13a85SIngo Molnar 	pgd_k = init_mm.pgd + index;
198f2f13a85SIngo Molnar 
199f2f13a85SIngo Molnar 	if (!pgd_present(*pgd_k))
200f2f13a85SIngo Molnar 		return NULL;
201f2f13a85SIngo Molnar 
202f2f13a85SIngo Molnar 	/*
203f2f13a85SIngo Molnar 	 * set_pgd(pgd, *pgd_k); here would be useless on PAE
204f2f13a85SIngo Molnar 	 * and redundant with the set_pmd() on non-PAE. As would
205f2f13a85SIngo Molnar 	 * set_pud.
206f2f13a85SIngo Molnar 	 */
207f2f13a85SIngo Molnar 	pud = pud_offset(pgd, address);
208f2f13a85SIngo Molnar 	pud_k = pud_offset(pgd_k, address);
209f2f13a85SIngo Molnar 	if (!pud_present(*pud_k))
210f2f13a85SIngo Molnar 		return NULL;
211f2f13a85SIngo Molnar 
212f2f13a85SIngo Molnar 	pmd = pmd_offset(pud, address);
213f2f13a85SIngo Molnar 	pmd_k = pmd_offset(pud_k, address);
214f2f13a85SIngo Molnar 	if (!pmd_present(*pmd_k))
215f2f13a85SIngo Molnar 		return NULL;
216f2f13a85SIngo Molnar 
217b8bcfe99SJeremy Fitzhardinge 	if (!pmd_present(*pmd))
218f2f13a85SIngo Molnar 		set_pmd(pmd, *pmd_k);
219b8bcfe99SJeremy Fitzhardinge 	else
220f2f13a85SIngo Molnar 		BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
221f2f13a85SIngo Molnar 
222f2f13a85SIngo Molnar 	return pmd_k;
223f2f13a85SIngo Molnar }
224f2f13a85SIngo Molnar 
225f2f13a85SIngo Molnar void vmalloc_sync_all(void)
226f2f13a85SIngo Molnar {
227f2f13a85SIngo Molnar 	unsigned long address;
228f2f13a85SIngo Molnar 
229f2f13a85SIngo Molnar 	if (SHARED_KERNEL_PMD)
230f2f13a85SIngo Molnar 		return;
231f2f13a85SIngo Molnar 
232f2f13a85SIngo Molnar 	for (address = VMALLOC_START & PMD_MASK;
233f2f13a85SIngo Molnar 	     address >= TASK_SIZE && address < FIXADDR_TOP;
234f2f13a85SIngo Molnar 	     address += PMD_SIZE) {
235f2f13a85SIngo Molnar 		struct page *page;
236f2f13a85SIngo Molnar 
237a79e53d8SAndrea Arcangeli 		spin_lock(&pgd_lock);
238f2f13a85SIngo Molnar 		list_for_each_entry(page, &pgd_list, lru) {
239617d34d9SJeremy Fitzhardinge 			spinlock_t *pgt_lock;
240f01f7c56SBorislav Petkov 			pmd_t *ret;
241617d34d9SJeremy Fitzhardinge 
242a79e53d8SAndrea Arcangeli 			/* the pgt_lock only for Xen */
243617d34d9SJeremy Fitzhardinge 			pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
244617d34d9SJeremy Fitzhardinge 
245617d34d9SJeremy Fitzhardinge 			spin_lock(pgt_lock);
246617d34d9SJeremy Fitzhardinge 			ret = vmalloc_sync_one(page_address(page), address);
247617d34d9SJeremy Fitzhardinge 			spin_unlock(pgt_lock);
248617d34d9SJeremy Fitzhardinge 
249617d34d9SJeremy Fitzhardinge 			if (!ret)
250f2f13a85SIngo Molnar 				break;
251f2f13a85SIngo Molnar 		}
252a79e53d8SAndrea Arcangeli 		spin_unlock(&pgd_lock);
253f2f13a85SIngo Molnar 	}
254f2f13a85SIngo Molnar }
255f2f13a85SIngo Molnar 
256f2f13a85SIngo Molnar /*
257f2f13a85SIngo Molnar  * 32-bit:
258f2f13a85SIngo Molnar  *
259f2f13a85SIngo Molnar  *   Handle a fault on the vmalloc or module mapping area
260f2f13a85SIngo Molnar  */
26162c9295fSMasami Hiramatsu static noinline __kprobes int vmalloc_fault(unsigned long address)
262f2f13a85SIngo Molnar {
263f2f13a85SIngo Molnar 	unsigned long pgd_paddr;
264f2f13a85SIngo Molnar 	pmd_t *pmd_k;
265f2f13a85SIngo Molnar 	pte_t *pte_k;
266f2f13a85SIngo Molnar 
267f2f13a85SIngo Molnar 	/* Make sure we are in vmalloc area: */
268f2f13a85SIngo Molnar 	if (!(address >= VMALLOC_START && address < VMALLOC_END))
269f2f13a85SIngo Molnar 		return -1;
270f2f13a85SIngo Molnar 
271ebc8827fSFrederic Weisbecker 	WARN_ON_ONCE(in_nmi());
272ebc8827fSFrederic Weisbecker 
273f2f13a85SIngo Molnar 	/*
274f2f13a85SIngo Molnar 	 * Synchronize this task's top level page-table
275f2f13a85SIngo Molnar 	 * with the 'reference' page table.
276f2f13a85SIngo Molnar 	 *
277f2f13a85SIngo Molnar 	 * Do _not_ use "current" here. We might be inside
278f2f13a85SIngo Molnar 	 * an interrupt in the middle of a task switch..
279f2f13a85SIngo Molnar 	 */
280f2f13a85SIngo Molnar 	pgd_paddr = read_cr3();
281f2f13a85SIngo Molnar 	pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
282f2f13a85SIngo Molnar 	if (!pmd_k)
283f2f13a85SIngo Molnar 		return -1;
284f2f13a85SIngo Molnar 
285f2f13a85SIngo Molnar 	pte_k = pte_offset_kernel(pmd_k, address);
286f2f13a85SIngo Molnar 	if (!pte_present(*pte_k))
287f2f13a85SIngo Molnar 		return -1;
288f2f13a85SIngo Molnar 
289f2f13a85SIngo Molnar 	return 0;
290f2f13a85SIngo Molnar }
291f2f13a85SIngo Molnar 
292f2f13a85SIngo Molnar /*
293f2f13a85SIngo Molnar  * Did it hit the DOS screen memory VA from vm86 mode?
294f2f13a85SIngo Molnar  */
295f2f13a85SIngo Molnar static inline void
296f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address,
297f2f13a85SIngo Molnar 		 struct task_struct *tsk)
298f2f13a85SIngo Molnar {
299f2f13a85SIngo Molnar 	unsigned long bit;
300f2f13a85SIngo Molnar 
301f2f13a85SIngo Molnar 	if (!v8086_mode(regs))
302f2f13a85SIngo Molnar 		return;
303f2f13a85SIngo Molnar 
304f2f13a85SIngo Molnar 	bit = (address - 0xA0000) >> PAGE_SHIFT;
305f2f13a85SIngo Molnar 	if (bit < 32)
306f2f13a85SIngo Molnar 		tsk->thread.screen_bitmap |= 1 << bit;
307f2f13a85SIngo Molnar }
308c61e211dSHarvey Harrison 
309087975b0SAkinobu Mita static bool low_pfn(unsigned long pfn)
310087975b0SAkinobu Mita {
311087975b0SAkinobu Mita 	return pfn < max_low_pfn;
312087975b0SAkinobu Mita }
313087975b0SAkinobu Mita 
314cae30f82SAdrian Bunk static void dump_pagetable(unsigned long address)
315c61e211dSHarvey Harrison {
316087975b0SAkinobu Mita 	pgd_t *base = __va(read_cr3());
317087975b0SAkinobu Mita 	pgd_t *pgd = &base[pgd_index(address)];
318087975b0SAkinobu Mita 	pmd_t *pmd;
319087975b0SAkinobu Mita 	pte_t *pte;
3202d4a7167SIngo Molnar 
321c61e211dSHarvey Harrison #ifdef CONFIG_X86_PAE
322087975b0SAkinobu Mita 	printk("*pdpt = %016Lx ", pgd_val(*pgd));
323087975b0SAkinobu Mita 	if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
324087975b0SAkinobu Mita 		goto out;
325c61e211dSHarvey Harrison #endif
326087975b0SAkinobu Mita 	pmd = pmd_offset(pud_offset(pgd, address), address);
327087975b0SAkinobu Mita 	printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
328c61e211dSHarvey Harrison 
329c61e211dSHarvey Harrison 	/*
330c61e211dSHarvey Harrison 	 * We must not directly access the pte in the highpte
331c61e211dSHarvey Harrison 	 * case if the page table is located in highmem.
332c61e211dSHarvey Harrison 	 * And let's rather not kmap-atomic the pte, just in case
3332d4a7167SIngo Molnar 	 * it's allocated already:
334c61e211dSHarvey Harrison 	 */
335087975b0SAkinobu Mita 	if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
336087975b0SAkinobu Mita 		goto out;
3372d4a7167SIngo Molnar 
338087975b0SAkinobu Mita 	pte = pte_offset_kernel(pmd, address);
339087975b0SAkinobu Mita 	printk("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
340087975b0SAkinobu Mita out:
341c61e211dSHarvey Harrison 	printk("\n");
342f2f13a85SIngo Molnar }
343f2f13a85SIngo Molnar 
344f2f13a85SIngo Molnar #else /* CONFIG_X86_64: */
345f2f13a85SIngo Molnar 
346f2f13a85SIngo Molnar void vmalloc_sync_all(void)
347f2f13a85SIngo Molnar {
3486afb5157SHaicheng Li 	sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END);
349f2f13a85SIngo Molnar }
350f2f13a85SIngo Molnar 
351f2f13a85SIngo Molnar /*
352f2f13a85SIngo Molnar  * 64-bit:
353f2f13a85SIngo Molnar  *
354f2f13a85SIngo Molnar  *   Handle a fault on the vmalloc area
355f2f13a85SIngo Molnar  *
356f2f13a85SIngo Molnar  * This assumes no large pages in there.
357f2f13a85SIngo Molnar  */
35862c9295fSMasami Hiramatsu static noinline __kprobes int vmalloc_fault(unsigned long address)
359f2f13a85SIngo Molnar {
360f2f13a85SIngo Molnar 	pgd_t *pgd, *pgd_ref;
361f2f13a85SIngo Molnar 	pud_t *pud, *pud_ref;
362f2f13a85SIngo Molnar 	pmd_t *pmd, *pmd_ref;
363f2f13a85SIngo Molnar 	pte_t *pte, *pte_ref;
364f2f13a85SIngo Molnar 
365f2f13a85SIngo Molnar 	/* Make sure we are in vmalloc area: */
366f2f13a85SIngo Molnar 	if (!(address >= VMALLOC_START && address < VMALLOC_END))
367f2f13a85SIngo Molnar 		return -1;
368f2f13a85SIngo Molnar 
369ebc8827fSFrederic Weisbecker 	WARN_ON_ONCE(in_nmi());
370ebc8827fSFrederic Weisbecker 
371f2f13a85SIngo Molnar 	/*
372f2f13a85SIngo Molnar 	 * Copy kernel mappings over when needed. This can also
373f2f13a85SIngo Molnar 	 * happen within a race in page table update. In the later
374f2f13a85SIngo Molnar 	 * case just flush:
375f2f13a85SIngo Molnar 	 */
376f2f13a85SIngo Molnar 	pgd = pgd_offset(current->active_mm, address);
377f2f13a85SIngo Molnar 	pgd_ref = pgd_offset_k(address);
378f2f13a85SIngo Molnar 	if (pgd_none(*pgd_ref))
379f2f13a85SIngo Molnar 		return -1;
380f2f13a85SIngo Molnar 
381f2f13a85SIngo Molnar 	if (pgd_none(*pgd))
382f2f13a85SIngo Molnar 		set_pgd(pgd, *pgd_ref);
383f2f13a85SIngo Molnar 	else
384f2f13a85SIngo Molnar 		BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
385f2f13a85SIngo Molnar 
386f2f13a85SIngo Molnar 	/*
387f2f13a85SIngo Molnar 	 * Below here mismatches are bugs because these lower tables
388f2f13a85SIngo Molnar 	 * are shared:
389f2f13a85SIngo Molnar 	 */
390f2f13a85SIngo Molnar 
391f2f13a85SIngo Molnar 	pud = pud_offset(pgd, address);
392f2f13a85SIngo Molnar 	pud_ref = pud_offset(pgd_ref, address);
393f2f13a85SIngo Molnar 	if (pud_none(*pud_ref))
394f2f13a85SIngo Molnar 		return -1;
395f2f13a85SIngo Molnar 
396f2f13a85SIngo Molnar 	if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
397f2f13a85SIngo Molnar 		BUG();
398f2f13a85SIngo Molnar 
399f2f13a85SIngo Molnar 	pmd = pmd_offset(pud, address);
400f2f13a85SIngo Molnar 	pmd_ref = pmd_offset(pud_ref, address);
401f2f13a85SIngo Molnar 	if (pmd_none(*pmd_ref))
402f2f13a85SIngo Molnar 		return -1;
403f2f13a85SIngo Molnar 
404f2f13a85SIngo Molnar 	if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
405f2f13a85SIngo Molnar 		BUG();
406f2f13a85SIngo Molnar 
407f2f13a85SIngo Molnar 	pte_ref = pte_offset_kernel(pmd_ref, address);
408f2f13a85SIngo Molnar 	if (!pte_present(*pte_ref))
409f2f13a85SIngo Molnar 		return -1;
410f2f13a85SIngo Molnar 
411f2f13a85SIngo Molnar 	pte = pte_offset_kernel(pmd, address);
412f2f13a85SIngo Molnar 
413f2f13a85SIngo Molnar 	/*
414f2f13a85SIngo Molnar 	 * Don't use pte_page here, because the mappings can point
415f2f13a85SIngo Molnar 	 * outside mem_map, and the NUMA hash lookup cannot handle
416f2f13a85SIngo Molnar 	 * that:
417f2f13a85SIngo Molnar 	 */
418f2f13a85SIngo Molnar 	if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
419f2f13a85SIngo Molnar 		BUG();
420f2f13a85SIngo Molnar 
421f2f13a85SIngo Molnar 	return 0;
422f2f13a85SIngo Molnar }
423f2f13a85SIngo Molnar 
424e05139f2SJan Beulich #ifdef CONFIG_CPU_SUP_AMD
425f2f13a85SIngo Molnar static const char errata93_warning[] =
426ad361c98SJoe Perches KERN_ERR
427ad361c98SJoe Perches "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
428ad361c98SJoe Perches "******* Working around it, but it may cause SEGVs or burn power.\n"
429ad361c98SJoe Perches "******* Please consider a BIOS update.\n"
430ad361c98SJoe Perches "******* Disabling USB legacy in the BIOS may also help.\n";
431e05139f2SJan Beulich #endif
432f2f13a85SIngo Molnar 
433f2f13a85SIngo Molnar /*
434f2f13a85SIngo Molnar  * No vm86 mode in 64-bit mode:
435f2f13a85SIngo Molnar  */
436f2f13a85SIngo Molnar static inline void
437f2f13a85SIngo Molnar check_v8086_mode(struct pt_regs *regs, unsigned long address,
438f2f13a85SIngo Molnar 		 struct task_struct *tsk)
439f2f13a85SIngo Molnar {
440f2f13a85SIngo Molnar }
441f2f13a85SIngo Molnar 
442f2f13a85SIngo Molnar static int bad_address(void *p)
443f2f13a85SIngo Molnar {
444f2f13a85SIngo Molnar 	unsigned long dummy;
445f2f13a85SIngo Molnar 
446f2f13a85SIngo Molnar 	return probe_kernel_address((unsigned long *)p, dummy);
447f2f13a85SIngo Molnar }
448f2f13a85SIngo Molnar 
449f2f13a85SIngo Molnar static void dump_pagetable(unsigned long address)
450f2f13a85SIngo Molnar {
451087975b0SAkinobu Mita 	pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
452087975b0SAkinobu Mita 	pgd_t *pgd = base + pgd_index(address);
453c61e211dSHarvey Harrison 	pud_t *pud;
454c61e211dSHarvey Harrison 	pmd_t *pmd;
455c61e211dSHarvey Harrison 	pte_t *pte;
456c61e211dSHarvey Harrison 
4572d4a7167SIngo Molnar 	if (bad_address(pgd))
4582d4a7167SIngo Molnar 		goto bad;
4592d4a7167SIngo Molnar 
460c61e211dSHarvey Harrison 	printk("PGD %lx ", pgd_val(*pgd));
4612d4a7167SIngo Molnar 
4622d4a7167SIngo Molnar 	if (!pgd_present(*pgd))
4632d4a7167SIngo Molnar 		goto out;
464c61e211dSHarvey Harrison 
465c61e211dSHarvey Harrison 	pud = pud_offset(pgd, address);
4662d4a7167SIngo Molnar 	if (bad_address(pud))
4672d4a7167SIngo Molnar 		goto bad;
4682d4a7167SIngo Molnar 
469c61e211dSHarvey Harrison 	printk("PUD %lx ", pud_val(*pud));
470b5360222SAndi Kleen 	if (!pud_present(*pud) || pud_large(*pud))
4712d4a7167SIngo Molnar 		goto out;
472c61e211dSHarvey Harrison 
473c61e211dSHarvey Harrison 	pmd = pmd_offset(pud, address);
4742d4a7167SIngo Molnar 	if (bad_address(pmd))
4752d4a7167SIngo Molnar 		goto bad;
4762d4a7167SIngo Molnar 
477c61e211dSHarvey Harrison 	printk("PMD %lx ", pmd_val(*pmd));
4782d4a7167SIngo Molnar 	if (!pmd_present(*pmd) || pmd_large(*pmd))
4792d4a7167SIngo Molnar 		goto out;
480c61e211dSHarvey Harrison 
481c61e211dSHarvey Harrison 	pte = pte_offset_kernel(pmd, address);
4822d4a7167SIngo Molnar 	if (bad_address(pte))
4832d4a7167SIngo Molnar 		goto bad;
4842d4a7167SIngo Molnar 
485c61e211dSHarvey Harrison 	printk("PTE %lx", pte_val(*pte));
4862d4a7167SIngo Molnar out:
487c61e211dSHarvey Harrison 	printk("\n");
488c61e211dSHarvey Harrison 	return;
489c61e211dSHarvey Harrison bad:
490c61e211dSHarvey Harrison 	printk("BAD\n");
491c61e211dSHarvey Harrison }
492c61e211dSHarvey Harrison 
493f2f13a85SIngo Molnar #endif /* CONFIG_X86_64 */
494c61e211dSHarvey Harrison 
4952d4a7167SIngo Molnar /*
4962d4a7167SIngo Molnar  * Workaround for K8 erratum #93 & buggy BIOS.
4972d4a7167SIngo Molnar  *
4982d4a7167SIngo Molnar  * BIOS SMM functions are required to use a specific workaround
4992d4a7167SIngo Molnar  * to avoid corruption of the 64bit RIP register on C stepping K8.
5002d4a7167SIngo Molnar  *
5012d4a7167SIngo Molnar  * A lot of BIOS that didn't get tested properly miss this.
5022d4a7167SIngo Molnar  *
5032d4a7167SIngo Molnar  * The OS sees this as a page fault with the upper 32bits of RIP cleared.
5042d4a7167SIngo Molnar  * Try to work around it here.
5052d4a7167SIngo Molnar  *
5062d4a7167SIngo Molnar  * Note we only handle faults in kernel here.
5072d4a7167SIngo Molnar  * Does nothing on 32-bit.
508c61e211dSHarvey Harrison  */
509c61e211dSHarvey Harrison static int is_errata93(struct pt_regs *regs, unsigned long address)
510c61e211dSHarvey Harrison {
511e05139f2SJan Beulich #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
512e05139f2SJan Beulich 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
513e05139f2SJan Beulich 	    || boot_cpu_data.x86 != 0xf)
514e05139f2SJan Beulich 		return 0;
515e05139f2SJan Beulich 
516c61e211dSHarvey Harrison 	if (address != regs->ip)
517c61e211dSHarvey Harrison 		return 0;
5182d4a7167SIngo Molnar 
519c61e211dSHarvey Harrison 	if ((address >> 32) != 0)
520c61e211dSHarvey Harrison 		return 0;
5212d4a7167SIngo Molnar 
522c61e211dSHarvey Harrison 	address |= 0xffffffffUL << 32;
523c61e211dSHarvey Harrison 	if ((address >= (u64)_stext && address <= (u64)_etext) ||
524c61e211dSHarvey Harrison 	    (address >= MODULES_VADDR && address <= MODULES_END)) {
525a454ab31SIngo Molnar 		printk_once(errata93_warning);
526c61e211dSHarvey Harrison 		regs->ip = address;
527c61e211dSHarvey Harrison 		return 1;
528c61e211dSHarvey Harrison 	}
529c61e211dSHarvey Harrison #endif
530c61e211dSHarvey Harrison 	return 0;
531c61e211dSHarvey Harrison }
532c61e211dSHarvey Harrison 
533c61e211dSHarvey Harrison /*
5342d4a7167SIngo Molnar  * Work around K8 erratum #100 K8 in compat mode occasionally jumps
5352d4a7167SIngo Molnar  * to illegal addresses >4GB.
5362d4a7167SIngo Molnar  *
5372d4a7167SIngo Molnar  * We catch this in the page fault handler because these addresses
5382d4a7167SIngo Molnar  * are not reachable. Just detect this case and return.  Any code
539c61e211dSHarvey Harrison  * segment in LDT is compatibility mode.
540c61e211dSHarvey Harrison  */
541c61e211dSHarvey Harrison static int is_errata100(struct pt_regs *regs, unsigned long address)
542c61e211dSHarvey Harrison {
543c61e211dSHarvey Harrison #ifdef CONFIG_X86_64
5442d4a7167SIngo Molnar 	if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
545c61e211dSHarvey Harrison 		return 1;
546c61e211dSHarvey Harrison #endif
547c61e211dSHarvey Harrison 	return 0;
548c61e211dSHarvey Harrison }
549c61e211dSHarvey Harrison 
550c61e211dSHarvey Harrison static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
551c61e211dSHarvey Harrison {
552c61e211dSHarvey Harrison #ifdef CONFIG_X86_F00F_BUG
553c61e211dSHarvey Harrison 	unsigned long nr;
5542d4a7167SIngo Molnar 
555c61e211dSHarvey Harrison 	/*
5562d4a7167SIngo Molnar 	 * Pentium F0 0F C7 C8 bug workaround:
557c61e211dSHarvey Harrison 	 */
558c61e211dSHarvey Harrison 	if (boot_cpu_data.f00f_bug) {
559c61e211dSHarvey Harrison 		nr = (address - idt_descr.address) >> 3;
560c61e211dSHarvey Harrison 
561c61e211dSHarvey Harrison 		if (nr == 6) {
562c61e211dSHarvey Harrison 			do_invalid_op(regs, 0);
563c61e211dSHarvey Harrison 			return 1;
564c61e211dSHarvey Harrison 		}
565c61e211dSHarvey Harrison 	}
566c61e211dSHarvey Harrison #endif
567c61e211dSHarvey Harrison 	return 0;
568c61e211dSHarvey Harrison }
569c61e211dSHarvey Harrison 
5708f766149SIngo Molnar static const char nx_warning[] = KERN_CRIT
5718f766149SIngo Molnar "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
5728f766149SIngo Molnar 
5732d4a7167SIngo Molnar static void
5742d4a7167SIngo Molnar show_fault_oops(struct pt_regs *regs, unsigned long error_code,
575c61e211dSHarvey Harrison 		unsigned long address)
576c61e211dSHarvey Harrison {
577c61e211dSHarvey Harrison 	if (!oops_may_print())
578c61e211dSHarvey Harrison 		return;
579c61e211dSHarvey Harrison 
580c61e211dSHarvey Harrison 	if (error_code & PF_INSTR) {
58193809be8SHarvey Harrison 		unsigned int level;
5822d4a7167SIngo Molnar 
583c61e211dSHarvey Harrison 		pte_t *pte = lookup_address(address, &level);
584c61e211dSHarvey Harrison 
5858f766149SIngo Molnar 		if (pte && pte_present(*pte) && !pte_exec(*pte))
586078de5f7SEric W. Biederman 			printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
587c61e211dSHarvey Harrison 	}
588fd40d6e3SHarvey Harrison 
589c61e211dSHarvey Harrison 	printk(KERN_ALERT "BUG: unable to handle kernel ");
590c61e211dSHarvey Harrison 	if (address < PAGE_SIZE)
591c61e211dSHarvey Harrison 		printk(KERN_CONT "NULL pointer dereference");
592c61e211dSHarvey Harrison 	else
593c61e211dSHarvey Harrison 		printk(KERN_CONT "paging request");
5942d4a7167SIngo Molnar 
595f294a8ceSVegard Nossum 	printk(KERN_CONT " at %p\n", (void *) address);
596c61e211dSHarvey Harrison 	printk(KERN_ALERT "IP:");
597c61e211dSHarvey Harrison 	printk_address(regs->ip, 1);
5982d4a7167SIngo Molnar 
599c61e211dSHarvey Harrison 	dump_pagetable(address);
600c61e211dSHarvey Harrison }
601c61e211dSHarvey Harrison 
6022d4a7167SIngo Molnar static noinline void
6032d4a7167SIngo Molnar pgtable_bad(struct pt_regs *regs, unsigned long error_code,
6042d4a7167SIngo Molnar 	    unsigned long address)
605c61e211dSHarvey Harrison {
6062d4a7167SIngo Molnar 	struct task_struct *tsk;
6072d4a7167SIngo Molnar 	unsigned long flags;
6082d4a7167SIngo Molnar 	int sig;
6092d4a7167SIngo Molnar 
6102d4a7167SIngo Molnar 	flags = oops_begin();
6112d4a7167SIngo Molnar 	tsk = current;
6122d4a7167SIngo Molnar 	sig = SIGKILL;
613c61e211dSHarvey Harrison 
614c61e211dSHarvey Harrison 	printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
61592181f19SNick Piggin 	       tsk->comm, address);
616c61e211dSHarvey Harrison 	dump_pagetable(address);
6172d4a7167SIngo Molnar 
618c61e211dSHarvey Harrison 	tsk->thread.cr2		= address;
61951e7dc70SSrikar Dronamraju 	tsk->thread.trap_nr	= X86_TRAP_PF;
620c61e211dSHarvey Harrison 	tsk->thread.error_code	= error_code;
6212d4a7167SIngo Molnar 
622c61e211dSHarvey Harrison 	if (__die("Bad pagetable", regs, error_code))
623874d93d1SAlexander van Heukelum 		sig = 0;
6242d4a7167SIngo Molnar 
625874d93d1SAlexander van Heukelum 	oops_end(flags, regs, sig);
626c61e211dSHarvey Harrison }
627c61e211dSHarvey Harrison 
6282d4a7167SIngo Molnar static noinline void
6292d4a7167SIngo Molnar no_context(struct pt_regs *regs, unsigned long error_code,
6304fc34901SAndy Lutomirski 	   unsigned long address, int signal, int si_code)
63192181f19SNick Piggin {
63292181f19SNick Piggin 	struct task_struct *tsk = current;
63319803078SIngo Molnar 	unsigned long *stackend;
63492181f19SNick Piggin 	unsigned long flags;
63592181f19SNick Piggin 	int sig;
63692181f19SNick Piggin 
63792181f19SNick Piggin 	/* Are we prepared to handle this kernel fault? */
6384fc34901SAndy Lutomirski 	if (fixup_exception(regs)) {
6394fc34901SAndy Lutomirski 		if (current_thread_info()->sig_on_uaccess_error && signal) {
64051e7dc70SSrikar Dronamraju 			tsk->thread.trap_nr = X86_TRAP_PF;
6414fc34901SAndy Lutomirski 			tsk->thread.error_code = error_code | PF_USER;
6424fc34901SAndy Lutomirski 			tsk->thread.cr2 = address;
6434fc34901SAndy Lutomirski 
6444fc34901SAndy Lutomirski 			/* XXX: hwpoison faults will set the wrong code. */
6454fc34901SAndy Lutomirski 			force_sig_info_fault(signal, si_code, address, tsk, 0);
6464fc34901SAndy Lutomirski 		}
64792181f19SNick Piggin 		return;
6484fc34901SAndy Lutomirski 	}
64992181f19SNick Piggin 
65092181f19SNick Piggin 	/*
6512d4a7167SIngo Molnar 	 * 32-bit:
6522d4a7167SIngo Molnar 	 *
65392181f19SNick Piggin 	 *   Valid to do another page fault here, because if this fault
65492181f19SNick Piggin 	 *   had been triggered by is_prefetch fixup_exception would have
65592181f19SNick Piggin 	 *   handled it.
65692181f19SNick Piggin 	 *
6572d4a7167SIngo Molnar 	 * 64-bit:
6582d4a7167SIngo Molnar 	 *
65992181f19SNick Piggin 	 *   Hall of shame of CPU/BIOS bugs.
66092181f19SNick Piggin 	 */
66192181f19SNick Piggin 	if (is_prefetch(regs, error_code, address))
66292181f19SNick Piggin 		return;
66392181f19SNick Piggin 
66492181f19SNick Piggin 	if (is_errata93(regs, address))
66592181f19SNick Piggin 		return;
66692181f19SNick Piggin 
66792181f19SNick Piggin 	/*
66892181f19SNick Piggin 	 * Oops. The kernel tried to access some bad page. We'll have to
6692d4a7167SIngo Molnar 	 * terminate things with extreme prejudice:
67092181f19SNick Piggin 	 */
67192181f19SNick Piggin 	flags = oops_begin();
67292181f19SNick Piggin 
67392181f19SNick Piggin 	show_fault_oops(regs, error_code, address);
67492181f19SNick Piggin 
67519803078SIngo Molnar 	stackend = end_of_stack(tsk);
6760e7810beSJan Beulich 	if (tsk != &init_task && *stackend != STACK_END_MAGIC)
677b0f4c4b3SPrarit Bhargava 		printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
67819803078SIngo Molnar 
67992181f19SNick Piggin 	tsk->thread.cr2		= address;
68051e7dc70SSrikar Dronamraju 	tsk->thread.trap_nr	= X86_TRAP_PF;
68192181f19SNick Piggin 	tsk->thread.error_code	= error_code;
68292181f19SNick Piggin 
68392181f19SNick Piggin 	sig = SIGKILL;
68492181f19SNick Piggin 	if (__die("Oops", regs, error_code))
68592181f19SNick Piggin 		sig = 0;
6862d4a7167SIngo Molnar 
68792181f19SNick Piggin 	/* Executive summary in case the body of the oops scrolled away */
688b0f4c4b3SPrarit Bhargava 	printk(KERN_DEFAULT "CR2: %016lx\n", address);
6892d4a7167SIngo Molnar 
69092181f19SNick Piggin 	oops_end(flags, regs, sig);
69192181f19SNick Piggin }
69292181f19SNick Piggin 
6932d4a7167SIngo Molnar /*
6942d4a7167SIngo Molnar  * Print out info about fatal segfaults, if the show_unhandled_signals
6952d4a7167SIngo Molnar  * sysctl is set:
6962d4a7167SIngo Molnar  */
6972d4a7167SIngo Molnar static inline void
6982d4a7167SIngo Molnar show_signal_msg(struct pt_regs *regs, unsigned long error_code,
6992d4a7167SIngo Molnar 		unsigned long address, struct task_struct *tsk)
7002d4a7167SIngo Molnar {
7012d4a7167SIngo Molnar 	if (!unhandled_signal(tsk, SIGSEGV))
7022d4a7167SIngo Molnar 		return;
7032d4a7167SIngo Molnar 
7042d4a7167SIngo Molnar 	if (!printk_ratelimit())
7052d4a7167SIngo Molnar 		return;
7062d4a7167SIngo Molnar 
707a1a08d1cSRoland Dreier 	printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
7082d4a7167SIngo Molnar 		task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
7092d4a7167SIngo Molnar 		tsk->comm, task_pid_nr(tsk), address,
7102d4a7167SIngo Molnar 		(void *)regs->ip, (void *)regs->sp, error_code);
7112d4a7167SIngo Molnar 
7122d4a7167SIngo Molnar 	print_vma_addr(KERN_CONT " in ", regs->ip);
7132d4a7167SIngo Molnar 
7142d4a7167SIngo Molnar 	printk(KERN_CONT "\n");
7152d4a7167SIngo Molnar }
7162d4a7167SIngo Molnar 
7172d4a7167SIngo Molnar static void
7182d4a7167SIngo Molnar __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
7192d4a7167SIngo Molnar 		       unsigned long address, int si_code)
72092181f19SNick Piggin {
72192181f19SNick Piggin 	struct task_struct *tsk = current;
72292181f19SNick Piggin 
72392181f19SNick Piggin 	/* User mode accesses just cause a SIGSEGV */
72492181f19SNick Piggin 	if (error_code & PF_USER) {
72592181f19SNick Piggin 		/*
7262d4a7167SIngo Molnar 		 * It's possible to have interrupts off here:
72792181f19SNick Piggin 		 */
72892181f19SNick Piggin 		local_irq_enable();
72992181f19SNick Piggin 
73092181f19SNick Piggin 		/*
73192181f19SNick Piggin 		 * Valid to do another page fault here because this one came
7322d4a7167SIngo Molnar 		 * from user space:
73392181f19SNick Piggin 		 */
73492181f19SNick Piggin 		if (is_prefetch(regs, error_code, address))
73592181f19SNick Piggin 			return;
73692181f19SNick Piggin 
73792181f19SNick Piggin 		if (is_errata100(regs, address))
73892181f19SNick Piggin 			return;
73992181f19SNick Piggin 
7403ae36655SAndy Lutomirski #ifdef CONFIG_X86_64
7413ae36655SAndy Lutomirski 		/*
7423ae36655SAndy Lutomirski 		 * Instruction fetch faults in the vsyscall page might need
7433ae36655SAndy Lutomirski 		 * emulation.
7443ae36655SAndy Lutomirski 		 */
7453ae36655SAndy Lutomirski 		if (unlikely((error_code & PF_INSTR) &&
7463ae36655SAndy Lutomirski 			     ((address & ~0xfff) == VSYSCALL_START))) {
7473ae36655SAndy Lutomirski 			if (emulate_vsyscall(regs, address))
7483ae36655SAndy Lutomirski 				return;
7493ae36655SAndy Lutomirski 		}
7503ae36655SAndy Lutomirski #endif
7513ae36655SAndy Lutomirski 
7522d4a7167SIngo Molnar 		if (unlikely(show_unhandled_signals))
7532d4a7167SIngo Molnar 			show_signal_msg(regs, error_code, address, tsk);
75492181f19SNick Piggin 
7552d4a7167SIngo Molnar 		/* Kernel addresses are always protection faults: */
75692181f19SNick Piggin 		tsk->thread.cr2		= address;
75792181f19SNick Piggin 		tsk->thread.error_code	= error_code | (address >= TASK_SIZE);
75851e7dc70SSrikar Dronamraju 		tsk->thread.trap_nr	= X86_TRAP_PF;
7592d4a7167SIngo Molnar 
760f672b49bSAndi Kleen 		force_sig_info_fault(SIGSEGV, si_code, address, tsk, 0);
7612d4a7167SIngo Molnar 
76292181f19SNick Piggin 		return;
76392181f19SNick Piggin 	}
76492181f19SNick Piggin 
76592181f19SNick Piggin 	if (is_f00f_bug(regs, address))
76692181f19SNick Piggin 		return;
76792181f19SNick Piggin 
7684fc34901SAndy Lutomirski 	no_context(regs, error_code, address, SIGSEGV, si_code);
76992181f19SNick Piggin }
77092181f19SNick Piggin 
7712d4a7167SIngo Molnar static noinline void
7722d4a7167SIngo Molnar bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
7732d4a7167SIngo Molnar 		     unsigned long address)
77492181f19SNick Piggin {
77592181f19SNick Piggin 	__bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
77692181f19SNick Piggin }
77792181f19SNick Piggin 
7782d4a7167SIngo Molnar static void
7792d4a7167SIngo Molnar __bad_area(struct pt_regs *regs, unsigned long error_code,
7802d4a7167SIngo Molnar 	   unsigned long address, int si_code)
78192181f19SNick Piggin {
78292181f19SNick Piggin 	struct mm_struct *mm = current->mm;
78392181f19SNick Piggin 
78492181f19SNick Piggin 	/*
78592181f19SNick Piggin 	 * Something tried to access memory that isn't in our memory map..
78692181f19SNick Piggin 	 * Fix it, but check if it's kernel or user first..
78792181f19SNick Piggin 	 */
78892181f19SNick Piggin 	up_read(&mm->mmap_sem);
78992181f19SNick Piggin 
79092181f19SNick Piggin 	__bad_area_nosemaphore(regs, error_code, address, si_code);
79192181f19SNick Piggin }
79292181f19SNick Piggin 
7932d4a7167SIngo Molnar static noinline void
7942d4a7167SIngo Molnar bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
79592181f19SNick Piggin {
79692181f19SNick Piggin 	__bad_area(regs, error_code, address, SEGV_MAPERR);
79792181f19SNick Piggin }
79892181f19SNick Piggin 
7992d4a7167SIngo Molnar static noinline void
8002d4a7167SIngo Molnar bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
8012d4a7167SIngo Molnar 		      unsigned long address)
80292181f19SNick Piggin {
80392181f19SNick Piggin 	__bad_area(regs, error_code, address, SEGV_ACCERR);
80492181f19SNick Piggin }
80592181f19SNick Piggin 
8062d4a7167SIngo Molnar static void
807a6e04aa9SAndi Kleen do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
808a6e04aa9SAndi Kleen 	  unsigned int fault)
80992181f19SNick Piggin {
81092181f19SNick Piggin 	struct task_struct *tsk = current;
81192181f19SNick Piggin 	struct mm_struct *mm = tsk->mm;
812a6e04aa9SAndi Kleen 	int code = BUS_ADRERR;
81392181f19SNick Piggin 
81492181f19SNick Piggin 	up_read(&mm->mmap_sem);
81592181f19SNick Piggin 
8162d4a7167SIngo Molnar 	/* Kernel mode? Handle exceptions or die: */
81796054569SLinus Torvalds 	if (!(error_code & PF_USER)) {
8184fc34901SAndy Lutomirski 		no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
81996054569SLinus Torvalds 		return;
82096054569SLinus Torvalds 	}
8212d4a7167SIngo Molnar 
822cd1b68f0SIngo Molnar 	/* User-space => ok to do another page fault: */
82392181f19SNick Piggin 	if (is_prefetch(regs, error_code, address))
82492181f19SNick Piggin 		return;
8252d4a7167SIngo Molnar 
82692181f19SNick Piggin 	tsk->thread.cr2		= address;
82792181f19SNick Piggin 	tsk->thread.error_code	= error_code;
82851e7dc70SSrikar Dronamraju 	tsk->thread.trap_nr	= X86_TRAP_PF;
8292d4a7167SIngo Molnar 
830a6e04aa9SAndi Kleen #ifdef CONFIG_MEMORY_FAILURE
831f672b49bSAndi Kleen 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
832a6e04aa9SAndi Kleen 		printk(KERN_ERR
833a6e04aa9SAndi Kleen 	"MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
834a6e04aa9SAndi Kleen 			tsk->comm, tsk->pid, address);
835a6e04aa9SAndi Kleen 		code = BUS_MCEERR_AR;
836a6e04aa9SAndi Kleen 	}
837a6e04aa9SAndi Kleen #endif
838f672b49bSAndi Kleen 	force_sig_info_fault(SIGBUS, code, address, tsk, fault);
83992181f19SNick Piggin }
84092181f19SNick Piggin 
841b80ef10eSKOSAKI Motohiro static noinline int
8422d4a7167SIngo Molnar mm_fault_error(struct pt_regs *regs, unsigned long error_code,
8432d4a7167SIngo Molnar 	       unsigned long address, unsigned int fault)
84492181f19SNick Piggin {
845b80ef10eSKOSAKI Motohiro 	/*
846b80ef10eSKOSAKI Motohiro 	 * Pagefault was interrupted by SIGKILL. We have no reason to
847b80ef10eSKOSAKI Motohiro 	 * continue pagefault.
848b80ef10eSKOSAKI Motohiro 	 */
849b80ef10eSKOSAKI Motohiro 	if (fatal_signal_pending(current)) {
850b80ef10eSKOSAKI Motohiro 		if (!(fault & VM_FAULT_RETRY))
851b80ef10eSKOSAKI Motohiro 			up_read(&current->mm->mmap_sem);
852b80ef10eSKOSAKI Motohiro 		if (!(error_code & PF_USER))
8534fc34901SAndy Lutomirski 			no_context(regs, error_code, address, 0, 0);
854b80ef10eSKOSAKI Motohiro 		return 1;
855b80ef10eSKOSAKI Motohiro 	}
856b80ef10eSKOSAKI Motohiro 	if (!(fault & VM_FAULT_ERROR))
857b80ef10eSKOSAKI Motohiro 		return 0;
858b80ef10eSKOSAKI Motohiro 
8592d4a7167SIngo Molnar 	if (fault & VM_FAULT_OOM) {
860f8626854SAndrey Vagin 		/* Kernel mode? Handle exceptions or die: */
861f8626854SAndrey Vagin 		if (!(error_code & PF_USER)) {
862f8626854SAndrey Vagin 			up_read(&current->mm->mmap_sem);
8634fc34901SAndy Lutomirski 			no_context(regs, error_code, address,
8644fc34901SAndy Lutomirski 				   SIGSEGV, SEGV_MAPERR);
865b80ef10eSKOSAKI Motohiro 			return 1;
866f8626854SAndrey Vagin 		}
867f8626854SAndrey Vagin 
868*c2d23f91SDavid Rientjes 		up_read(&current->mm->mmap_sem);
869*c2d23f91SDavid Rientjes 
870*c2d23f91SDavid Rientjes 		/*
871*c2d23f91SDavid Rientjes 		 * We ran out of memory, call the OOM killer, and return the
872*c2d23f91SDavid Rientjes 		 * userspace (which will retry the fault, or kill us if we got
873*c2d23f91SDavid Rientjes 		 * oom-killed):
874*c2d23f91SDavid Rientjes 		 */
875*c2d23f91SDavid Rientjes 		pagefault_out_of_memory();
8762d4a7167SIngo Molnar 	} else {
877f672b49bSAndi Kleen 		if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
878f672b49bSAndi Kleen 			     VM_FAULT_HWPOISON_LARGE))
879a6e04aa9SAndi Kleen 			do_sigbus(regs, error_code, address, fault);
88092181f19SNick Piggin 		else
88192181f19SNick Piggin 			BUG();
88292181f19SNick Piggin 	}
883b80ef10eSKOSAKI Motohiro 	return 1;
8842d4a7167SIngo Molnar }
88592181f19SNick Piggin 
886d8b57bb7SThomas Gleixner static int spurious_fault_check(unsigned long error_code, pte_t *pte)
887d8b57bb7SThomas Gleixner {
888d8b57bb7SThomas Gleixner 	if ((error_code & PF_WRITE) && !pte_write(*pte))
889d8b57bb7SThomas Gleixner 		return 0;
8902d4a7167SIngo Molnar 
891d8b57bb7SThomas Gleixner 	if ((error_code & PF_INSTR) && !pte_exec(*pte))
892d8b57bb7SThomas Gleixner 		return 0;
893d8b57bb7SThomas Gleixner 
894d8b57bb7SThomas Gleixner 	return 1;
895d8b57bb7SThomas Gleixner }
896d8b57bb7SThomas Gleixner 
897c61e211dSHarvey Harrison /*
8982d4a7167SIngo Molnar  * Handle a spurious fault caused by a stale TLB entry.
8992d4a7167SIngo Molnar  *
9002d4a7167SIngo Molnar  * This allows us to lazily refresh the TLB when increasing the
9012d4a7167SIngo Molnar  * permissions of a kernel page (RO -> RW or NX -> X).  Doing it
9022d4a7167SIngo Molnar  * eagerly is very expensive since that implies doing a full
9032d4a7167SIngo Molnar  * cross-processor TLB flush, even if no stale TLB entries exist
9042d4a7167SIngo Molnar  * on other processors.
9052d4a7167SIngo Molnar  *
9065b727a3bSJeremy Fitzhardinge  * There are no security implications to leaving a stale TLB when
9075b727a3bSJeremy Fitzhardinge  * increasing the permissions on a page.
9085b727a3bSJeremy Fitzhardinge  */
90962c9295fSMasami Hiramatsu static noinline __kprobes int
9102d4a7167SIngo Molnar spurious_fault(unsigned long error_code, unsigned long address)
9115b727a3bSJeremy Fitzhardinge {
9125b727a3bSJeremy Fitzhardinge 	pgd_t *pgd;
9135b727a3bSJeremy Fitzhardinge 	pud_t *pud;
9145b727a3bSJeremy Fitzhardinge 	pmd_t *pmd;
9155b727a3bSJeremy Fitzhardinge 	pte_t *pte;
9163c3e5694SSteven Rostedt 	int ret;
9175b727a3bSJeremy Fitzhardinge 
9185b727a3bSJeremy Fitzhardinge 	/* Reserved-bit violation or user access to kernel space? */
9195b727a3bSJeremy Fitzhardinge 	if (error_code & (PF_USER | PF_RSVD))
9205b727a3bSJeremy Fitzhardinge 		return 0;
9215b727a3bSJeremy Fitzhardinge 
9225b727a3bSJeremy Fitzhardinge 	pgd = init_mm.pgd + pgd_index(address);
9235b727a3bSJeremy Fitzhardinge 	if (!pgd_present(*pgd))
9245b727a3bSJeremy Fitzhardinge 		return 0;
9255b727a3bSJeremy Fitzhardinge 
9265b727a3bSJeremy Fitzhardinge 	pud = pud_offset(pgd, address);
9275b727a3bSJeremy Fitzhardinge 	if (!pud_present(*pud))
9285b727a3bSJeremy Fitzhardinge 		return 0;
9295b727a3bSJeremy Fitzhardinge 
930d8b57bb7SThomas Gleixner 	if (pud_large(*pud))
931d8b57bb7SThomas Gleixner 		return spurious_fault_check(error_code, (pte_t *) pud);
932d8b57bb7SThomas Gleixner 
9335b727a3bSJeremy Fitzhardinge 	pmd = pmd_offset(pud, address);
9345b727a3bSJeremy Fitzhardinge 	if (!pmd_present(*pmd))
9355b727a3bSJeremy Fitzhardinge 		return 0;
9365b727a3bSJeremy Fitzhardinge 
937d8b57bb7SThomas Gleixner 	if (pmd_large(*pmd))
938d8b57bb7SThomas Gleixner 		return spurious_fault_check(error_code, (pte_t *) pmd);
939d8b57bb7SThomas Gleixner 
940660a293eSShaohua Li 	/*
941660a293eSShaohua Li 	 * Note: don't use pte_present() here, since it returns true
942660a293eSShaohua Li 	 * if the _PAGE_PROTNONE bit is set.  However, this aliases the
943660a293eSShaohua Li 	 * _PAGE_GLOBAL bit, which for kernel pages give false positives
944660a293eSShaohua Li 	 * when CONFIG_DEBUG_PAGEALLOC is used.
945660a293eSShaohua Li 	 */
9465b727a3bSJeremy Fitzhardinge 	pte = pte_offset_kernel(pmd, address);
947660a293eSShaohua Li 	if (!(pte_flags(*pte) & _PAGE_PRESENT))
9485b727a3bSJeremy Fitzhardinge 		return 0;
9495b727a3bSJeremy Fitzhardinge 
9503c3e5694SSteven Rostedt 	ret = spurious_fault_check(error_code, pte);
9513c3e5694SSteven Rostedt 	if (!ret)
9523c3e5694SSteven Rostedt 		return 0;
9533c3e5694SSteven Rostedt 
9543c3e5694SSteven Rostedt 	/*
9552d4a7167SIngo Molnar 	 * Make sure we have permissions in PMD.
9562d4a7167SIngo Molnar 	 * If not, then there's a bug in the page tables:
9573c3e5694SSteven Rostedt 	 */
9583c3e5694SSteven Rostedt 	ret = spurious_fault_check(error_code, (pte_t *) pmd);
9593c3e5694SSteven Rostedt 	WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
9602d4a7167SIngo Molnar 
9613c3e5694SSteven Rostedt 	return ret;
9625b727a3bSJeremy Fitzhardinge }
9635b727a3bSJeremy Fitzhardinge 
964c61e211dSHarvey Harrison int show_unhandled_signals = 1;
965c61e211dSHarvey Harrison 
9662d4a7167SIngo Molnar static inline int
96768da336aSMichel Lespinasse access_error(unsigned long error_code, struct vm_area_struct *vma)
96892181f19SNick Piggin {
96968da336aSMichel Lespinasse 	if (error_code & PF_WRITE) {
9702d4a7167SIngo Molnar 		/* write, present and write, not present: */
97192181f19SNick Piggin 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
97292181f19SNick Piggin 			return 1;
9732d4a7167SIngo Molnar 		return 0;
9742d4a7167SIngo Molnar 	}
9752d4a7167SIngo Molnar 
9762d4a7167SIngo Molnar 	/* read, present: */
9772d4a7167SIngo Molnar 	if (unlikely(error_code & PF_PROT))
97892181f19SNick Piggin 		return 1;
9792d4a7167SIngo Molnar 
9802d4a7167SIngo Molnar 	/* read, not present: */
98192181f19SNick Piggin 	if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
98292181f19SNick Piggin 		return 1;
98392181f19SNick Piggin 
98492181f19SNick Piggin 	return 0;
98592181f19SNick Piggin }
98692181f19SNick Piggin 
9870973a06cSHiroshi Shimamoto static int fault_in_kernel_space(unsigned long address)
9880973a06cSHiroshi Shimamoto {
989d9517346SIngo Molnar 	return address >= TASK_SIZE_MAX;
9900973a06cSHiroshi Shimamoto }
9910973a06cSHiroshi Shimamoto 
99240d3cd66SH. Peter Anvin static inline bool smap_violation(int error_code, struct pt_regs *regs)
99340d3cd66SH. Peter Anvin {
99440d3cd66SH. Peter Anvin 	if (error_code & PF_USER)
99540d3cd66SH. Peter Anvin 		return false;
99640d3cd66SH. Peter Anvin 
99740d3cd66SH. Peter Anvin 	if (!user_mode_vm(regs) && (regs->flags & X86_EFLAGS_AC))
99840d3cd66SH. Peter Anvin 		return false;
99940d3cd66SH. Peter Anvin 
100040d3cd66SH. Peter Anvin 	return true;
100140d3cd66SH. Peter Anvin }
100240d3cd66SH. Peter Anvin 
1003c61e211dSHarvey Harrison /*
1004c61e211dSHarvey Harrison  * This routine handles page faults.  It determines the address,
1005c61e211dSHarvey Harrison  * and the problem, and then passes it off to one of the appropriate
1006c61e211dSHarvey Harrison  * routines.
1007c61e211dSHarvey Harrison  */
10086ba3c97aSFrederic Weisbecker static void __kprobes
10096ba3c97aSFrederic Weisbecker __do_page_fault(struct pt_regs *regs, unsigned long error_code)
1010c61e211dSHarvey Harrison {
1011c61e211dSHarvey Harrison 	struct vm_area_struct *vma;
10122d4a7167SIngo Molnar 	struct task_struct *tsk;
10132d4a7167SIngo Molnar 	unsigned long address;
10142d4a7167SIngo Molnar 	struct mm_struct *mm;
1015c61e211dSHarvey Harrison 	int fault;
1016d065bd81SMichel Lespinasse 	int write = error_code & PF_WRITE;
101737b23e05SKOSAKI Motohiro 	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
1018d065bd81SMichel Lespinasse 					(write ? FAULT_FLAG_WRITE : 0);
1019c61e211dSHarvey Harrison 
1020c61e211dSHarvey Harrison 	tsk = current;
1021c61e211dSHarvey Harrison 	mm = tsk->mm;
10222d4a7167SIngo Molnar 
10232d4a7167SIngo Molnar 	/* Get the faulting address: */
1024c61e211dSHarvey Harrison 	address = read_cr2();
1025c61e211dSHarvey Harrison 
1026f8561296SVegard Nossum 	/*
1027f8561296SVegard Nossum 	 * Detect and handle instructions that would cause a page fault for
1028f8561296SVegard Nossum 	 * both a tracked kernel page and a userspace page.
1029f8561296SVegard Nossum 	 */
1030f8561296SVegard Nossum 	if (kmemcheck_active(regs))
1031f8561296SVegard Nossum 		kmemcheck_hide(regs);
10325dfaf90fSIngo Molnar 	prefetchw(&mm->mmap_sem);
1033f8561296SVegard Nossum 
10340fd0e3daSPekka Paalanen 	if (unlikely(kmmio_fault(regs, address)))
103586069782SPekka Paalanen 		return;
1036c61e211dSHarvey Harrison 
1037c61e211dSHarvey Harrison 	/*
1038c61e211dSHarvey Harrison 	 * We fault-in kernel-space virtual memory on-demand. The
1039c61e211dSHarvey Harrison 	 * 'reference' page table is init_mm.pgd.
1040c61e211dSHarvey Harrison 	 *
1041c61e211dSHarvey Harrison 	 * NOTE! We MUST NOT take any locks for this case. We may
1042c61e211dSHarvey Harrison 	 * be in an interrupt or a critical region, and should
1043c61e211dSHarvey Harrison 	 * only copy the information from the master page table,
1044c61e211dSHarvey Harrison 	 * nothing more.
1045c61e211dSHarvey Harrison 	 *
1046c61e211dSHarvey Harrison 	 * This verifies that the fault happens in kernel space
1047c61e211dSHarvey Harrison 	 * (error_code & 4) == 0, and that the fault was not a
1048c61e211dSHarvey Harrison 	 * protection error (error_code & 9) == 0.
1049c61e211dSHarvey Harrison 	 */
10500973a06cSHiroshi Shimamoto 	if (unlikely(fault_in_kernel_space(address))) {
1051f8561296SVegard Nossum 		if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
1052f8561296SVegard Nossum 			if (vmalloc_fault(address) >= 0)
1053c61e211dSHarvey Harrison 				return;
10545b727a3bSJeremy Fitzhardinge 
1055f8561296SVegard Nossum 			if (kmemcheck_fault(regs, address, error_code))
1056f8561296SVegard Nossum 				return;
1057f8561296SVegard Nossum 		}
1058f8561296SVegard Nossum 
10592d4a7167SIngo Molnar 		/* Can handle a stale RO->RW TLB: */
106092181f19SNick Piggin 		if (spurious_fault(error_code, address))
10615b727a3bSJeremy Fitzhardinge 			return;
10625b727a3bSJeremy Fitzhardinge 
10632d4a7167SIngo Molnar 		/* kprobes don't want to hook the spurious faults: */
10649be260a6SMasami Hiramatsu 		if (notify_page_fault(regs))
10659be260a6SMasami Hiramatsu 			return;
1066c61e211dSHarvey Harrison 		/*
1067c61e211dSHarvey Harrison 		 * Don't take the mm semaphore here. If we fixup a prefetch
10682d4a7167SIngo Molnar 		 * fault we could otherwise deadlock:
1069c61e211dSHarvey Harrison 		 */
107092181f19SNick Piggin 		bad_area_nosemaphore(regs, error_code, address);
10712d4a7167SIngo Molnar 
107292181f19SNick Piggin 		return;
1073c61e211dSHarvey Harrison 	}
1074c61e211dSHarvey Harrison 
10752d4a7167SIngo Molnar 	/* kprobes don't want to hook the spurious faults: */
1076f8a6b2b9SIngo Molnar 	if (unlikely(notify_page_fault(regs)))
10779be260a6SMasami Hiramatsu 		return;
1078c61e211dSHarvey Harrison 	/*
1079891cffbdSLinus Torvalds 	 * It's safe to allow irq's after cr2 has been saved and the
1080891cffbdSLinus Torvalds 	 * vmalloc fault has been handled.
1081891cffbdSLinus Torvalds 	 *
1082891cffbdSLinus Torvalds 	 * User-mode registers count as a user access even for any
10832d4a7167SIngo Molnar 	 * potential system fault or CPU buglet:
1084c61e211dSHarvey Harrison 	 */
1085891cffbdSLinus Torvalds 	if (user_mode_vm(regs)) {
1086891cffbdSLinus Torvalds 		local_irq_enable();
1087891cffbdSLinus Torvalds 		error_code |= PF_USER;
10882d4a7167SIngo Molnar 	} else {
10892d4a7167SIngo Molnar 		if (regs->flags & X86_EFLAGS_IF)
1090c61e211dSHarvey Harrison 			local_irq_enable();
10912d4a7167SIngo Molnar 	}
1092c61e211dSHarvey Harrison 
1093c61e211dSHarvey Harrison 	if (unlikely(error_code & PF_RSVD))
109492181f19SNick Piggin 		pgtable_bad(regs, error_code, address);
1095c61e211dSHarvey Harrison 
109640d3cd66SH. Peter Anvin 	if (static_cpu_has(X86_FEATURE_SMAP)) {
109740d3cd66SH. Peter Anvin 		if (unlikely(smap_violation(error_code, regs))) {
109840d3cd66SH. Peter Anvin 			bad_area_nosemaphore(regs, error_code, address);
109940d3cd66SH. Peter Anvin 			return;
110040d3cd66SH. Peter Anvin 		}
110140d3cd66SH. Peter Anvin 	}
110240d3cd66SH. Peter Anvin 
1103a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
11047dd1fcc2SPeter Zijlstra 
1105c61e211dSHarvey Harrison 	/*
11062d4a7167SIngo Molnar 	 * If we're in an interrupt, have no user context or are running
11072d4a7167SIngo Molnar 	 * in an atomic region then we must not take the fault:
1108c61e211dSHarvey Harrison 	 */
110992181f19SNick Piggin 	if (unlikely(in_atomic() || !mm)) {
111092181f19SNick Piggin 		bad_area_nosemaphore(regs, error_code, address);
111192181f19SNick Piggin 		return;
111292181f19SNick Piggin 	}
1113c61e211dSHarvey Harrison 
11143a1dfe6eSIngo Molnar 	/*
11153a1dfe6eSIngo Molnar 	 * When running in the kernel we expect faults to occur only to
11162d4a7167SIngo Molnar 	 * addresses in user space.  All other faults represent errors in
11172d4a7167SIngo Molnar 	 * the kernel and should generate an OOPS.  Unfortunately, in the
11182d4a7167SIngo Molnar 	 * case of an erroneous fault occurring in a code path which already
11192d4a7167SIngo Molnar 	 * holds mmap_sem we will deadlock attempting to validate the fault
11202d4a7167SIngo Molnar 	 * against the address space.  Luckily the kernel only validly
11212d4a7167SIngo Molnar 	 * references user space from well defined areas of code, which are
11222d4a7167SIngo Molnar 	 * listed in the exceptions table.
1123c61e211dSHarvey Harrison 	 *
1124c61e211dSHarvey Harrison 	 * As the vast majority of faults will be valid we will only perform
11252d4a7167SIngo Molnar 	 * the source reference check when there is a possibility of a
11262d4a7167SIngo Molnar 	 * deadlock. Attempt to lock the address space, if we cannot we then
11272d4a7167SIngo Molnar 	 * validate the source. If this is invalid we can skip the address
11282d4a7167SIngo Molnar 	 * space check, thus avoiding the deadlock:
1129c61e211dSHarvey Harrison 	 */
113092181f19SNick Piggin 	if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1131c61e211dSHarvey Harrison 		if ((error_code & PF_USER) == 0 &&
113292181f19SNick Piggin 		    !search_exception_tables(regs->ip)) {
113392181f19SNick Piggin 			bad_area_nosemaphore(regs, error_code, address);
113492181f19SNick Piggin 			return;
113592181f19SNick Piggin 		}
1136d065bd81SMichel Lespinasse retry:
1137c61e211dSHarvey Harrison 		down_read(&mm->mmap_sem);
113801006074SPeter Zijlstra 	} else {
113901006074SPeter Zijlstra 		/*
11402d4a7167SIngo Molnar 		 * The above down_read_trylock() might have succeeded in
11412d4a7167SIngo Molnar 		 * which case we'll have missed the might_sleep() from
11422d4a7167SIngo Molnar 		 * down_read():
114301006074SPeter Zijlstra 		 */
114401006074SPeter Zijlstra 		might_sleep();
1145c61e211dSHarvey Harrison 	}
1146c61e211dSHarvey Harrison 
1147c61e211dSHarvey Harrison 	vma = find_vma(mm, address);
114892181f19SNick Piggin 	if (unlikely(!vma)) {
114992181f19SNick Piggin 		bad_area(regs, error_code, address);
115092181f19SNick Piggin 		return;
115192181f19SNick Piggin 	}
115292181f19SNick Piggin 	if (likely(vma->vm_start <= address))
1153c61e211dSHarvey Harrison 		goto good_area;
115492181f19SNick Piggin 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
115592181f19SNick Piggin 		bad_area(regs, error_code, address);
115692181f19SNick Piggin 		return;
115792181f19SNick Piggin 	}
1158c61e211dSHarvey Harrison 	if (error_code & PF_USER) {
1159c61e211dSHarvey Harrison 		/*
1160c61e211dSHarvey Harrison 		 * Accessing the stack below %sp is always a bug.
1161c61e211dSHarvey Harrison 		 * The large cushion allows instructions like enter
1162c61e211dSHarvey Harrison 		 * and pusha to work. ("enter $65535, $31" pushes
1163c61e211dSHarvey Harrison 		 * 32 pointers and then decrements %sp by 65535.)
1164c61e211dSHarvey Harrison 		 */
116592181f19SNick Piggin 		if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
116692181f19SNick Piggin 			bad_area(regs, error_code, address);
116792181f19SNick Piggin 			return;
1168c61e211dSHarvey Harrison 		}
116992181f19SNick Piggin 	}
117092181f19SNick Piggin 	if (unlikely(expand_stack(vma, address))) {
117192181f19SNick Piggin 		bad_area(regs, error_code, address);
117292181f19SNick Piggin 		return;
117392181f19SNick Piggin 	}
117492181f19SNick Piggin 
1175c61e211dSHarvey Harrison 	/*
1176c61e211dSHarvey Harrison 	 * Ok, we have a good vm_area for this memory access, so
1177c61e211dSHarvey Harrison 	 * we can handle it..
1178c61e211dSHarvey Harrison 	 */
1179c61e211dSHarvey Harrison good_area:
118068da336aSMichel Lespinasse 	if (unlikely(access_error(error_code, vma))) {
118192181f19SNick Piggin 		bad_area_access_error(regs, error_code, address);
118292181f19SNick Piggin 		return;
1183c61e211dSHarvey Harrison 	}
1184c61e211dSHarvey Harrison 
1185c61e211dSHarvey Harrison 	/*
1186c61e211dSHarvey Harrison 	 * If for any reason at all we couldn't handle the fault,
1187c61e211dSHarvey Harrison 	 * make sure we exit gracefully rather than endlessly redo
11882d4a7167SIngo Molnar 	 * the fault:
1189c61e211dSHarvey Harrison 	 */
1190d065bd81SMichel Lespinasse 	fault = handle_mm_fault(mm, vma, address, flags);
11912d4a7167SIngo Molnar 
1192b80ef10eSKOSAKI Motohiro 	if (unlikely(fault & (VM_FAULT_RETRY|VM_FAULT_ERROR))) {
1193b80ef10eSKOSAKI Motohiro 		if (mm_fault_error(regs, error_code, address, fault))
119437b23e05SKOSAKI Motohiro 			return;
119537b23e05SKOSAKI Motohiro 	}
119637b23e05SKOSAKI Motohiro 
119737b23e05SKOSAKI Motohiro 	/*
1198d065bd81SMichel Lespinasse 	 * Major/minor page fault accounting is only done on the
1199d065bd81SMichel Lespinasse 	 * initial attempt. If we go through a retry, it is extremely
1200d065bd81SMichel Lespinasse 	 * likely that the page will be found in page cache at that point.
1201d065bd81SMichel Lespinasse 	 */
1202d065bd81SMichel Lespinasse 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
1203ac17dc8eSPeter Zijlstra 		if (fault & VM_FAULT_MAJOR) {
1204c61e211dSHarvey Harrison 			tsk->maj_flt++;
1205a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
120678f13e95SPeter Zijlstra 				      regs, address);
1207ac17dc8eSPeter Zijlstra 		} else {
1208c61e211dSHarvey Harrison 			tsk->min_flt++;
1209a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
121078f13e95SPeter Zijlstra 				      regs, address);
1211ac17dc8eSPeter Zijlstra 		}
1212d065bd81SMichel Lespinasse 		if (fault & VM_FAULT_RETRY) {
1213d065bd81SMichel Lespinasse 			/* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
1214d065bd81SMichel Lespinasse 			 * of starvation. */
1215d065bd81SMichel Lespinasse 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
121645cac65bSShaohua Li 			flags |= FAULT_FLAG_TRIED;
1217d065bd81SMichel Lespinasse 			goto retry;
1218d065bd81SMichel Lespinasse 		}
1219d065bd81SMichel Lespinasse 	}
1220c61e211dSHarvey Harrison 
12218c938f9fSIngo Molnar 	check_v8086_mode(regs, address, tsk);
12228c938f9fSIngo Molnar 
1223c61e211dSHarvey Harrison 	up_read(&mm->mmap_sem);
1224c61e211dSHarvey Harrison }
12256ba3c97aSFrederic Weisbecker 
12266ba3c97aSFrederic Weisbecker dotraplinkage void __kprobes
12276ba3c97aSFrederic Weisbecker do_page_fault(struct pt_regs *regs, unsigned long error_code)
12286ba3c97aSFrederic Weisbecker {
12296ba3c97aSFrederic Weisbecker 	exception_enter(regs);
12306ba3c97aSFrederic Weisbecker 	__do_page_fault(regs, error_code);
12316ba3c97aSFrederic Weisbecker 	exception_exit(regs);
12326ba3c97aSFrederic Weisbecker }
1233