xref: /openbmc/linux/arch/powerpc/mm/fault.c (revision 14c02e419a395c4bd7950175fc47eda1ecaa8c69)
114cf11afSPaul Mackerras /*
214cf11afSPaul Mackerras  *  PowerPC version
314cf11afSPaul Mackerras  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
414cf11afSPaul Mackerras  *
514cf11afSPaul Mackerras  *  Derived from "arch/i386/mm/fault.c"
614cf11afSPaul Mackerras  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
714cf11afSPaul Mackerras  *
814cf11afSPaul Mackerras  *  Modified by Cort Dougan and Paul Mackerras.
914cf11afSPaul Mackerras  *
1014cf11afSPaul Mackerras  *  Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
1114cf11afSPaul Mackerras  *
1214cf11afSPaul Mackerras  *  This program is free software; you can redistribute it and/or
1314cf11afSPaul Mackerras  *  modify it under the terms of the GNU General Public License
1414cf11afSPaul Mackerras  *  as published by the Free Software Foundation; either version
1514cf11afSPaul Mackerras  *  2 of the License, or (at your option) any later version.
1614cf11afSPaul Mackerras  */
1714cf11afSPaul Mackerras 
1814cf11afSPaul Mackerras #include <linux/signal.h>
1914cf11afSPaul Mackerras #include <linux/sched.h>
2068db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
2114cf11afSPaul Mackerras #include <linux/kernel.h>
2214cf11afSPaul Mackerras #include <linux/errno.h>
2314cf11afSPaul Mackerras #include <linux/string.h>
2414cf11afSPaul Mackerras #include <linux/types.h>
2514cf11afSPaul Mackerras #include <linux/ptrace.h>
2614cf11afSPaul Mackerras #include <linux/mman.h>
2714cf11afSPaul Mackerras #include <linux/mm.h>
2814cf11afSPaul Mackerras #include <linux/interrupt.h>
2914cf11afSPaul Mackerras #include <linux/highmem.h>
308a39b05fSPaul Gortmaker #include <linux/extable.h>
3114cf11afSPaul Mackerras #include <linux/kprobes.h>
321eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
33cdd6c482SIngo Molnar #include <linux/perf_event.h>
3476462232SChristian Dietrich #include <linux/ratelimit.h>
35ba12eedeSLi Zhong #include <linux/context_tracking.h>
369d57472fSAnton Blanchard #include <linux/hugetlb.h>
3770ffdb93SDavid Hildenbrand #include <linux/uaccess.h>
3814cf11afSPaul Mackerras 
3940900194SBrian King #include <asm/firmware.h>
4014cf11afSPaul Mackerras #include <asm/page.h>
4114cf11afSPaul Mackerras #include <asm/pgtable.h>
4214cf11afSPaul Mackerras #include <asm/mmu.h>
4314cf11afSPaul Mackerras #include <asm/mmu_context.h>
4414cf11afSPaul Mackerras #include <asm/tlbflush.h>
4514cf11afSPaul Mackerras #include <asm/siginfo.h>
46ae3a197eSDavid Howells #include <asm/debug.h>
479f90b997SChristoph Hellwig 
48c3dcf53aSJimi Xenidis #include "icswx.h"
49c3dcf53aSJimi Xenidis 
504f9e87c0SAnil S Keshavamurthy #ifdef CONFIG_KPROBES
519f90b997SChristoph Hellwig static inline int notify_page_fault(struct pt_regs *regs)
524f9e87c0SAnil S Keshavamurthy {
539f90b997SChristoph Hellwig 	int ret = 0;
549f90b997SChristoph Hellwig 
559f90b997SChristoph Hellwig 	/* kprobe_running() needs smp_processor_id() */
569f90b997SChristoph Hellwig 	if (!user_mode(regs)) {
579f90b997SChristoph Hellwig 		preempt_disable();
589f90b997SChristoph Hellwig 		if (kprobe_running() && kprobe_fault_handler(regs, 11))
599f90b997SChristoph Hellwig 			ret = 1;
609f90b997SChristoph Hellwig 		preempt_enable();
614f9e87c0SAnil S Keshavamurthy 	}
624f9e87c0SAnil S Keshavamurthy 
639f90b997SChristoph Hellwig 	return ret;
644f9e87c0SAnil S Keshavamurthy }
654f9e87c0SAnil S Keshavamurthy #else
669f90b997SChristoph Hellwig static inline int notify_page_fault(struct pt_regs *regs)
674f9e87c0SAnil S Keshavamurthy {
689f90b997SChristoph Hellwig 	return 0;
694f9e87c0SAnil S Keshavamurthy }
704f9e87c0SAnil S Keshavamurthy #endif
714f9e87c0SAnil S Keshavamurthy 
7214cf11afSPaul Mackerras /*
7314cf11afSPaul Mackerras  * Check whether the instruction at regs->nip is a store using
7414cf11afSPaul Mackerras  * an update addressing form which will update r1.
7514cf11afSPaul Mackerras  */
7614cf11afSPaul Mackerras static int store_updates_sp(struct pt_regs *regs)
7714cf11afSPaul Mackerras {
7814cf11afSPaul Mackerras 	unsigned int inst;
7914cf11afSPaul Mackerras 
8014cf11afSPaul Mackerras 	if (get_user(inst, (unsigned int __user *)regs->nip))
8114cf11afSPaul Mackerras 		return 0;
8214cf11afSPaul Mackerras 	/* check for 1 in the rA field */
8314cf11afSPaul Mackerras 	if (((inst >> 16) & 0x1f) != 1)
8414cf11afSPaul Mackerras 		return 0;
8514cf11afSPaul Mackerras 	/* check major opcode */
8614cf11afSPaul Mackerras 	switch (inst >> 26) {
8714cf11afSPaul Mackerras 	case 37:	/* stwu */
8814cf11afSPaul Mackerras 	case 39:	/* stbu */
8914cf11afSPaul Mackerras 	case 45:	/* sthu */
9014cf11afSPaul Mackerras 	case 53:	/* stfsu */
9114cf11afSPaul Mackerras 	case 55:	/* stfdu */
9214cf11afSPaul Mackerras 		return 1;
9314cf11afSPaul Mackerras 	case 62:	/* std or stdu */
9414cf11afSPaul Mackerras 		return (inst & 3) == 1;
9514cf11afSPaul Mackerras 	case 31:
9614cf11afSPaul Mackerras 		/* check minor opcode */
9714cf11afSPaul Mackerras 		switch ((inst >> 1) & 0x3ff) {
9814cf11afSPaul Mackerras 		case 181:	/* stdux */
9914cf11afSPaul Mackerras 		case 183:	/* stwux */
10014cf11afSPaul Mackerras 		case 247:	/* stbux */
10114cf11afSPaul Mackerras 		case 439:	/* sthux */
10214cf11afSPaul Mackerras 		case 695:	/* stfsux */
10314cf11afSPaul Mackerras 		case 759:	/* stfdux */
10414cf11afSPaul Mackerras 			return 1;
10514cf11afSPaul Mackerras 		}
10614cf11afSPaul Mackerras 	}
10714cf11afSPaul Mackerras 	return 0;
10814cf11afSPaul Mackerras }
1099be72573SBenjamin Herrenschmidt /*
1109be72573SBenjamin Herrenschmidt  * do_page_fault error handling helpers
1119be72573SBenjamin Herrenschmidt  */
1129be72573SBenjamin Herrenschmidt 
1139be72573SBenjamin Herrenschmidt #define MM_FAULT_RETURN		0
1149be72573SBenjamin Herrenschmidt #define MM_FAULT_CONTINUE	-1
1159be72573SBenjamin Herrenschmidt #define MM_FAULT_ERR(sig)	(sig)
1169be72573SBenjamin Herrenschmidt 
1173913fdd7SAnton Blanchard static int do_sigbus(struct pt_regs *regs, unsigned long address,
1183913fdd7SAnton Blanchard 		     unsigned int fault)
1199be72573SBenjamin Herrenschmidt {
1209be72573SBenjamin Herrenschmidt 	siginfo_t info;
1219d57472fSAnton Blanchard 	unsigned int lsb = 0;
1229be72573SBenjamin Herrenschmidt 
12363af5262SAnton Blanchard 	if (!user_mode(regs))
12463af5262SAnton Blanchard 		return MM_FAULT_ERR(SIGBUS);
12563af5262SAnton Blanchard 
12641ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = BUS_ADRERR;
1279be72573SBenjamin Herrenschmidt 	info.si_signo = SIGBUS;
1289be72573SBenjamin Herrenschmidt 	info.si_errno = 0;
1299be72573SBenjamin Herrenschmidt 	info.si_code = BUS_ADRERR;
1309be72573SBenjamin Herrenschmidt 	info.si_addr = (void __user *)address;
1313913fdd7SAnton Blanchard #ifdef CONFIG_MEMORY_FAILURE
1323913fdd7SAnton Blanchard 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
1333913fdd7SAnton Blanchard 		pr_err("MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
1343913fdd7SAnton Blanchard 			current->comm, current->pid, address);
1353913fdd7SAnton Blanchard 		info.si_code = BUS_MCEERR_AR;
1363913fdd7SAnton Blanchard 	}
1379d57472fSAnton Blanchard 
1389d57472fSAnton Blanchard 	if (fault & VM_FAULT_HWPOISON_LARGE)
1399d57472fSAnton Blanchard 		lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
1409d57472fSAnton Blanchard 	if (fault & VM_FAULT_HWPOISON)
1419d57472fSAnton Blanchard 		lsb = PAGE_SHIFT;
1423913fdd7SAnton Blanchard #endif
1439d57472fSAnton Blanchard 	info.si_addr_lsb = lsb;
1449be72573SBenjamin Herrenschmidt 	force_sig_info(SIGBUS, &info, current);
1459be72573SBenjamin Herrenschmidt 	return MM_FAULT_RETURN;
1469be72573SBenjamin Herrenschmidt }
1479be72573SBenjamin Herrenschmidt 
1489be72573SBenjamin Herrenschmidt static int mm_fault_error(struct pt_regs *regs, unsigned long addr, int fault)
1499be72573SBenjamin Herrenschmidt {
1509be72573SBenjamin Herrenschmidt 	/*
1519be72573SBenjamin Herrenschmidt 	 * Pagefault was interrupted by SIGKILL. We have no reason to
1529be72573SBenjamin Herrenschmidt 	 * continue the pagefault.
1539be72573SBenjamin Herrenschmidt 	 */
1549be72573SBenjamin Herrenschmidt 	if (fatal_signal_pending(current)) {
1559be72573SBenjamin Herrenschmidt 		/*
1569be72573SBenjamin Herrenschmidt 		 * If we have retry set, the mmap semaphore will have
1579be72573SBenjamin Herrenschmidt 		 * alrady been released in __lock_page_or_retry(). Else
1589be72573SBenjamin Herrenschmidt 		 * we release it now.
1599be72573SBenjamin Herrenschmidt 		 */
1609be72573SBenjamin Herrenschmidt 		if (!(fault & VM_FAULT_RETRY))
1619be72573SBenjamin Herrenschmidt 			up_read(&current->mm->mmap_sem);
1629be72573SBenjamin Herrenschmidt 		/* Coming from kernel, we need to deal with uaccess fixups */
1639be72573SBenjamin Herrenschmidt 		if (user_mode(regs))
1649be72573SBenjamin Herrenschmidt 			return MM_FAULT_RETURN;
1659be72573SBenjamin Herrenschmidt 		return MM_FAULT_ERR(SIGKILL);
1669be72573SBenjamin Herrenschmidt 	}
1679be72573SBenjamin Herrenschmidt 
1689be72573SBenjamin Herrenschmidt 	/* No fault: be happy */
1699be72573SBenjamin Herrenschmidt 	if (!(fault & VM_FAULT_ERROR))
1709be72573SBenjamin Herrenschmidt 		return MM_FAULT_CONTINUE;
1719be72573SBenjamin Herrenschmidt 
1729be72573SBenjamin Herrenschmidt 	/* Out of memory */
173c2d23f91SDavid Rientjes 	if (fault & VM_FAULT_OOM) {
174c2d23f91SDavid Rientjes 		up_read(&current->mm->mmap_sem);
175c2d23f91SDavid Rientjes 
176c2d23f91SDavid Rientjes 		/*
177c2d23f91SDavid Rientjes 		 * We ran out of memory, or some other thing happened to us that
178c2d23f91SDavid Rientjes 		 * made us unable to handle the page fault gracefully.
179c2d23f91SDavid Rientjes 		 */
180c2d23f91SDavid Rientjes 		if (!user_mode(regs))
181c2d23f91SDavid Rientjes 			return MM_FAULT_ERR(SIGKILL);
182c2d23f91SDavid Rientjes 		pagefault_out_of_memory();
183c2d23f91SDavid Rientjes 		return MM_FAULT_RETURN;
184c2d23f91SDavid Rientjes 	}
1859be72573SBenjamin Herrenschmidt 
186c2294e0fSLaurent Dufour 	if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
187c2294e0fSLaurent Dufour 		up_read(&current->mm->mmap_sem);
1883913fdd7SAnton Blanchard 		return do_sigbus(regs, addr, fault);
189c2294e0fSLaurent Dufour 	}
1909be72573SBenjamin Herrenschmidt 
1919be72573SBenjamin Herrenschmidt 	/* We don't understand the fault code, this is fatal */
1929be72573SBenjamin Herrenschmidt 	BUG();
1939be72573SBenjamin Herrenschmidt 	return MM_FAULT_CONTINUE;
1949be72573SBenjamin Herrenschmidt }
19514cf11afSPaul Mackerras 
19614cf11afSPaul Mackerras /*
19714cf11afSPaul Mackerras  * For 600- and 800-family processors, the error_code parameter is DSISR
19814cf11afSPaul Mackerras  * for a data fault, SRR1 for an instruction fault. For 400-family processors
19914cf11afSPaul Mackerras  * the error_code parameter is ESR for a data fault, 0 for an instruction
20014cf11afSPaul Mackerras  * fault.
20114cf11afSPaul Mackerras  * For 64-bit processors, the error_code parameter is
20214cf11afSPaul Mackerras  *  - DSISR for a non-SLB data access fault,
20314cf11afSPaul Mackerras  *  - SRR1 & 0x08000000 for a non-SLB instruction access fault
20414cf11afSPaul Mackerras  *  - 0 any SLB fault.
20514cf11afSPaul Mackerras  *
20614cf11afSPaul Mackerras  * The return value is 0 if the fault was handled, or the signal
20714cf11afSPaul Mackerras  * number if this is a kernel fault that can't be handled here.
20814cf11afSPaul Mackerras  */
20903465f89SNicholas Piggin int do_page_fault(struct pt_regs *regs, unsigned long address,
21014cf11afSPaul Mackerras 			    unsigned long error_code)
21114cf11afSPaul Mackerras {
212ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
21314cf11afSPaul Mackerras 	struct vm_area_struct * vma;
21414cf11afSPaul Mackerras 	struct mm_struct *mm = current->mm;
2159be72573SBenjamin Herrenschmidt 	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
21614cf11afSPaul Mackerras 	int code = SEGV_MAPERR;
2179be72573SBenjamin Herrenschmidt 	int is_write = 0;
21814cf11afSPaul Mackerras 	int trap = TRAP(regs);
21914cf11afSPaul Mackerras  	int is_exec = trap == 0x400;
2209be72573SBenjamin Herrenschmidt 	int fault;
22169e044ddSAneesh Kumar K.V 	int rc = 0, store_update_sp = 0;
22214cf11afSPaul Mackerras 
22314cf11afSPaul Mackerras #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
22414cf11afSPaul Mackerras 	/*
22514cf11afSPaul Mackerras 	 * Fortunately the bit assignments in SRR1 for an instruction
22614cf11afSPaul Mackerras 	 * fault and DSISR for a data fault are mostly the same for the
22714cf11afSPaul Mackerras 	 * bits we are interested in.  But there are some bits which
22814cf11afSPaul Mackerras 	 * indicate errors in DSISR but can validly be set in SRR1.
22914cf11afSPaul Mackerras 	 */
23014cf11afSPaul Mackerras 	if (trap == 0x400)
23114cf11afSPaul Mackerras 		error_code &= 0x48200000;
23214cf11afSPaul Mackerras 	else
23314cf11afSPaul Mackerras 		is_write = error_code & DSISR_ISSTORE;
23414cf11afSPaul Mackerras #else
23514cf11afSPaul Mackerras 	is_write = error_code & ESR_DST;
23614cf11afSPaul Mackerras #endif /* CONFIG_4xx || CONFIG_BOOKE */
23714cf11afSPaul Mackerras 
238c3dcf53aSJimi Xenidis #ifdef CONFIG_PPC_ICSWX
239c3dcf53aSJimi Xenidis 	/*
240c3dcf53aSJimi Xenidis 	 * we need to do this early because this "data storage
241c3dcf53aSJimi Xenidis 	 * interrupt" does not update the DAR/DEAR so we don't want to
242c3dcf53aSJimi Xenidis 	 * look at it
243c3dcf53aSJimi Xenidis 	 */
244c3dcf53aSJimi Xenidis 	if (error_code & ICSWX_DSI_UCT) {
245ba12eedeSLi Zhong 		rc = acop_handle_fault(regs, address, error_code);
2469be72573SBenjamin Herrenschmidt 		if (rc)
247ba12eedeSLi Zhong 			goto bail;
248c3dcf53aSJimi Xenidis 	}
2499be72573SBenjamin Herrenschmidt #endif /* CONFIG_PPC_ICSWX */
250c3dcf53aSJimi Xenidis 
2519f90b997SChristoph Hellwig 	if (notify_page_fault(regs))
252ba12eedeSLi Zhong 		goto bail;
25314cf11afSPaul Mackerras 
254c3b75bd7SMichael Neuling 	if (unlikely(debugger_fault_handler(regs)))
255ba12eedeSLi Zhong 		goto bail;
25614cf11afSPaul Mackerras 
257d7df2443SBenjamin Herrenschmidt 	/*
258d7df2443SBenjamin Herrenschmidt 	 * The kernel should never take an execute fault nor should it
259d7df2443SBenjamin Herrenschmidt 	 * take a page fault to a kernel address.
260d7df2443SBenjamin Herrenschmidt 	 */
261d7df2443SBenjamin Herrenschmidt 	if (!user_mode(regs) && (is_exec || (address >= TASK_SIZE))) {
262ba12eedeSLi Zhong 		rc = SIGSEGV;
263ba12eedeSLi Zhong 		goto bail;
264ba12eedeSLi Zhong 	}
26514cf11afSPaul Mackerras 
2669c7cc234SK.Prasad #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE) || \
2679c7cc234SK.Prasad 			     defined(CONFIG_PPC_BOOK3S_64))
26814cf11afSPaul Mackerras   	if (error_code & DSISR_DABRMATCH) {
2699422de3eSMichael Neuling 		/* breakpoint match */
2709422de3eSMichael Neuling 		do_break(regs, address, error_code);
271ba12eedeSLi Zhong 		goto bail;
27214cf11afSPaul Mackerras 	}
2739c7cc234SK.Prasad #endif
27414cf11afSPaul Mackerras 
275a546498fSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
276a546498fSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
277a546498fSBenjamin Herrenschmidt 		local_irq_enable();
278a546498fSBenjamin Herrenschmidt 
27970ffdb93SDavid Hildenbrand 	if (faulthandler_disabled() || mm == NULL) {
280ba12eedeSLi Zhong 		if (!user_mode(regs)) {
281ba12eedeSLi Zhong 			rc = SIGSEGV;
282ba12eedeSLi Zhong 			goto bail;
283ba12eedeSLi Zhong 		}
28470ffdb93SDavid Hildenbrand 		/* faulthandler_disabled() in user mode is really bad,
28514cf11afSPaul Mackerras 		   as is current->mm == NULL. */
28614cf11afSPaul Mackerras 		printk(KERN_EMERG "Page fault in user mode with "
28770ffdb93SDavid Hildenbrand 		       "faulthandler_disabled() = %d mm = %p\n",
28870ffdb93SDavid Hildenbrand 		       faulthandler_disabled(), mm);
28914cf11afSPaul Mackerras 		printk(KERN_EMERG "NIP = %lx  MSR = %lx\n",
29014cf11afSPaul Mackerras 		       regs->nip, regs->msr);
29114cf11afSPaul Mackerras 		die("Weird page fault", regs, SIGSEGV);
29214cf11afSPaul Mackerras 	}
29314cf11afSPaul Mackerras 
294a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
2957dd1fcc2SPeter Zijlstra 
29669e044ddSAneesh Kumar K.V 	/*
29769e044ddSAneesh Kumar K.V 	 * We want to do this outside mmap_sem, because reading code around nip
29869e044ddSAneesh Kumar K.V 	 * can result in fault, which will cause a deadlock when called with
29969e044ddSAneesh Kumar K.V 	 * mmap_sem held
30069e044ddSAneesh Kumar K.V 	 */
30169e044ddSAneesh Kumar K.V 	if (user_mode(regs))
30269e044ddSAneesh Kumar K.V 		store_update_sp = store_updates_sp(regs);
30369e044ddSAneesh Kumar K.V 
304759496baSJohannes Weiner 	if (user_mode(regs))
305759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
306759496baSJohannes Weiner 
30714cf11afSPaul Mackerras 	/* When running in the kernel we expect faults to occur only to
30814cf11afSPaul Mackerras 	 * addresses in user space.  All other faults represent errors in the
309fc5266eaSAnton Blanchard 	 * kernel and should generate an OOPS.  Unfortunately, in the case of an
310fc5266eaSAnton Blanchard 	 * erroneous fault occurring in a code path which already holds mmap_sem
31114cf11afSPaul Mackerras 	 * we will deadlock attempting to validate the fault against the
31214cf11afSPaul Mackerras 	 * address space.  Luckily the kernel only validly references user
31314cf11afSPaul Mackerras 	 * space from well defined areas of code, which are listed in the
31414cf11afSPaul Mackerras 	 * exceptions table.
31514cf11afSPaul Mackerras 	 *
31614cf11afSPaul Mackerras 	 * As the vast majority of faults will be valid we will only perform
317fc5266eaSAnton Blanchard 	 * the source reference check when there is a possibility of a deadlock.
31814cf11afSPaul Mackerras 	 * Attempt to lock the address space, if we cannot we then validate the
31914cf11afSPaul Mackerras 	 * source.  If this is invalid we can skip the address space check,
32014cf11afSPaul Mackerras 	 * thus avoiding the deadlock.
32114cf11afSPaul Mackerras 	 */
32214cf11afSPaul Mackerras 	if (!down_read_trylock(&mm->mmap_sem)) {
32314cf11afSPaul Mackerras 		if (!user_mode(regs) && !search_exception_tables(regs->nip))
32414cf11afSPaul Mackerras 			goto bad_area_nosemaphore;
32514cf11afSPaul Mackerras 
3269be72573SBenjamin Herrenschmidt retry:
32714cf11afSPaul Mackerras 		down_read(&mm->mmap_sem);
328a546498fSBenjamin Herrenschmidt 	} else {
329a546498fSBenjamin Herrenschmidt 		/*
330a546498fSBenjamin Herrenschmidt 		 * The above down_read_trylock() might have succeeded in
331a546498fSBenjamin Herrenschmidt 		 * which case we'll have missed the might_sleep() from
332a546498fSBenjamin Herrenschmidt 		 * down_read():
333a546498fSBenjamin Herrenschmidt 		 */
334a546498fSBenjamin Herrenschmidt 		might_sleep();
33514cf11afSPaul Mackerras 	}
33614cf11afSPaul Mackerras 
33714cf11afSPaul Mackerras 	vma = find_vma(mm, address);
33814cf11afSPaul Mackerras 	if (!vma)
33914cf11afSPaul Mackerras 		goto bad_area;
34014cf11afSPaul Mackerras 	if (vma->vm_start <= address)
34114cf11afSPaul Mackerras 		goto good_area;
34214cf11afSPaul Mackerras 	if (!(vma->vm_flags & VM_GROWSDOWN))
34314cf11afSPaul Mackerras 		goto bad_area;
34414cf11afSPaul Mackerras 
34514cf11afSPaul Mackerras 	/*
34614cf11afSPaul Mackerras 	 * N.B. The POWER/Open ABI allows programs to access up to
34714cf11afSPaul Mackerras 	 * 288 bytes below the stack pointer.
34814cf11afSPaul Mackerras 	 * The kernel signal delivery code writes up to about 1.5kB
34914cf11afSPaul Mackerras 	 * below the stack pointer (r1) before decrementing it.
35014cf11afSPaul Mackerras 	 * The exec code can write slightly over 640kB to the stack
35114cf11afSPaul Mackerras 	 * before setting the user r1.  Thus we allow the stack to
35214cf11afSPaul Mackerras 	 * expand to 1MB without further checks.
35314cf11afSPaul Mackerras 	 */
35414cf11afSPaul Mackerras 	if (address + 0x100000 < vma->vm_end) {
35514cf11afSPaul Mackerras 		/* get user regs even if this fault is in kernel mode */
35614cf11afSPaul Mackerras 		struct pt_regs *uregs = current->thread.regs;
35714cf11afSPaul Mackerras 		if (uregs == NULL)
35814cf11afSPaul Mackerras 			goto bad_area;
35914cf11afSPaul Mackerras 
36014cf11afSPaul Mackerras 		/*
36114cf11afSPaul Mackerras 		 * A user-mode access to an address a long way below
36214cf11afSPaul Mackerras 		 * the stack pointer is only valid if the instruction
36314cf11afSPaul Mackerras 		 * is one which would update the stack pointer to the
36414cf11afSPaul Mackerras 		 * address accessed if the instruction completed,
36514cf11afSPaul Mackerras 		 * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
36614cf11afSPaul Mackerras 		 * (or the byte, halfword, float or double forms).
36714cf11afSPaul Mackerras 		 *
36814cf11afSPaul Mackerras 		 * If we don't check this then any write to the area
36914cf11afSPaul Mackerras 		 * between the last mapped region and the stack will
37014cf11afSPaul Mackerras 		 * expand the stack rather than segfaulting.
37114cf11afSPaul Mackerras 		 */
37269e044ddSAneesh Kumar K.V 		if (address + 2048 < uregs->gpr[1] && !store_update_sp)
37314cf11afSPaul Mackerras 			goto bad_area;
37414cf11afSPaul Mackerras 	}
37514cf11afSPaul Mackerras 	if (expand_stack(vma, address))
37614cf11afSPaul Mackerras 		goto bad_area;
37714cf11afSPaul Mackerras 
37814cf11afSPaul Mackerras good_area:
37914cf11afSPaul Mackerras 	code = SEGV_ACCERR;
38014cf11afSPaul Mackerras #if defined(CONFIG_6xx)
38114cf11afSPaul Mackerras 	if (error_code & 0x95700000)
38214cf11afSPaul Mackerras 		/* an error such as lwarx to I/O controller space,
38314cf11afSPaul Mackerras 		   address matching DABR, eciwx, etc. */
38414cf11afSPaul Mackerras 		goto bad_area;
38514cf11afSPaul Mackerras #endif /* CONFIG_6xx */
38614cf11afSPaul Mackerras #if defined(CONFIG_8xx)
38714cf11afSPaul Mackerras         /* The MPC8xx seems to always set 0x80000000, which is
38814cf11afSPaul Mackerras          * "undefined".  Of those that can be set, this is the only
38914cf11afSPaul Mackerras          * one which seems bad.
39014cf11afSPaul Mackerras          */
39114cf11afSPaul Mackerras 	if (error_code & 0x10000000)
39214cf11afSPaul Mackerras                 /* Guarded storage error. */
39314cf11afSPaul Mackerras 		goto bad_area;
39414cf11afSPaul Mackerras #endif /* CONFIG_8xx */
39514cf11afSPaul Mackerras 
39614cf11afSPaul Mackerras 	if (is_exec) {
39708ae6cc1SPaul Mackerras 		/*
39808ae6cc1SPaul Mackerras 		 * Allow execution from readable areas if the MMU does not
39908ae6cc1SPaul Mackerras 		 * provide separate controls over reading and executing.
4008d30c14cSBenjamin Herrenschmidt 		 *
4018d30c14cSBenjamin Herrenschmidt 		 * Note: That code used to not be enabled for 4xx/BookE.
4028d30c14cSBenjamin Herrenschmidt 		 * It is now as I/D cache coherency for these is done at
4038d30c14cSBenjamin Herrenschmidt 		 * set_pte_at() time and I see no reason why the test
4048d30c14cSBenjamin Herrenschmidt 		 * below wouldn't be valid on those processors. This -may-
4058d30c14cSBenjamin Herrenschmidt 		 * break programs compiled with a really old ABI though.
40608ae6cc1SPaul Mackerras 		 */
40708ae6cc1SPaul Mackerras 		if (!(vma->vm_flags & VM_EXEC) &&
40808ae6cc1SPaul Mackerras 		    (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
40908ae6cc1SPaul Mackerras 		     !(vma->vm_flags & (VM_READ | VM_WRITE))))
41014cf11afSPaul Mackerras 			goto bad_area;
41114cf11afSPaul Mackerras 	/* a write */
41214cf11afSPaul Mackerras 	} else if (is_write) {
41314cf11afSPaul Mackerras 		if (!(vma->vm_flags & VM_WRITE))
41414cf11afSPaul Mackerras 			goto bad_area;
415759496baSJohannes Weiner 		flags |= FAULT_FLAG_WRITE;
41614cf11afSPaul Mackerras 	/* a read */
41714cf11afSPaul Mackerras 	} else {
418df67b3daSJason Baron 		if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
41914cf11afSPaul Mackerras 			goto bad_area;
42014cf11afSPaul Mackerras 	}
42118061c17SAneesh Kumar K.V #ifdef CONFIG_PPC_STD_MMU
42218061c17SAneesh Kumar K.V 	/*
42318061c17SAneesh Kumar K.V 	 * For hash translation mode, we should never get a
42418061c17SAneesh Kumar K.V 	 * PROTFAULT. Any update to pte to reduce access will result in us
42518061c17SAneesh Kumar K.V 	 * removing the hash page table entry, thus resulting in a DSISR_NOHPTE
42618061c17SAneesh Kumar K.V 	 * fault instead of DSISR_PROTFAULT.
42718061c17SAneesh Kumar K.V 	 *
42818061c17SAneesh Kumar K.V 	 * A pte update to relax the access will not result in a hash page table
42918061c17SAneesh Kumar K.V 	 * entry invalidate and hence can result in DSISR_PROTFAULT.
43018061c17SAneesh Kumar K.V 	 * ptep_set_access_flags() doesn't do a hpte flush. This is why we have
43118061c17SAneesh Kumar K.V 	 * the special !is_write in the below conditional.
43218061c17SAneesh Kumar K.V 	 *
43318061c17SAneesh Kumar K.V 	 * For platforms that doesn't supports coherent icache and do support
43418061c17SAneesh Kumar K.V 	 * per page noexec bit, we do setup things such that we do the
43518061c17SAneesh Kumar K.V 	 * sync between D/I cache via fault. But that is handled via low level
43618061c17SAneesh Kumar K.V 	 * hash fault code (hash_page_do_lazy_icache()) and we should not reach
43718061c17SAneesh Kumar K.V 	 * here in such case.
43818061c17SAneesh Kumar K.V 	 *
43918061c17SAneesh Kumar K.V 	 * For wrong access that can result in PROTFAULT, the above vma->vm_flags
44018061c17SAneesh Kumar K.V 	 * check should handle those and hence we should fall to the bad_area
44118061c17SAneesh Kumar K.V 	 * handling correctly.
44218061c17SAneesh Kumar K.V 	 *
44318061c17SAneesh Kumar K.V 	 * For embedded with per page exec support that doesn't support coherent
44418061c17SAneesh Kumar K.V 	 * icache we do get PROTFAULT and we handle that D/I cache sync in
44518061c17SAneesh Kumar K.V 	 * set_pte_at while taking the noexec/prot fault. Hence this is WARN_ON
44618061c17SAneesh Kumar K.V 	 * is conditional for server MMU.
44718061c17SAneesh Kumar K.V 	 *
44818061c17SAneesh Kumar K.V 	 * For radix, we can get prot fault for autonuma case, because radix
44918061c17SAneesh Kumar K.V 	 * page table will have them marked noaccess for user.
45018061c17SAneesh Kumar K.V 	 */
45118061c17SAneesh Kumar K.V 	if (!radix_enabled() && !is_write)
45218061c17SAneesh Kumar K.V 		WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
45318061c17SAneesh Kumar K.V #endif /* CONFIG_PPC_STD_MMU */
45414cf11afSPaul Mackerras 
45514cf11afSPaul Mackerras 	/*
45614cf11afSPaul Mackerras 	 * If for any reason at all we couldn't handle the fault,
45714cf11afSPaul Mackerras 	 * make sure we exit gracefully rather than endlessly redo
45814cf11afSPaul Mackerras 	 * the fault.
45914cf11afSPaul Mackerras 	 */
460dcddffd4SKirill A. Shutemov 	fault = handle_mm_fault(vma, address, flags);
461*14c02e41SLaurent Dufour 
462*14c02e41SLaurent Dufour 	/*
463*14c02e41SLaurent Dufour 	 * Handle the retry right now, the mmap_sem has been released in that
464*14c02e41SLaurent Dufour 	 * case.
465*14c02e41SLaurent Dufour 	 */
466*14c02e41SLaurent Dufour 	if (unlikely(fault & VM_FAULT_RETRY)) {
467*14c02e41SLaurent Dufour 		/* We retry only once */
468*14c02e41SLaurent Dufour 		if (flags & FAULT_FLAG_ALLOW_RETRY) {
469*14c02e41SLaurent Dufour 			/*
470*14c02e41SLaurent Dufour 			 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
471*14c02e41SLaurent Dufour 			 * of starvation.
472*14c02e41SLaurent Dufour 			 */
473*14c02e41SLaurent Dufour 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
474*14c02e41SLaurent Dufour 			flags |= FAULT_FLAG_TRIED;
475*14c02e41SLaurent Dufour 			if (!fatal_signal_pending(current))
476*14c02e41SLaurent Dufour 				goto retry;
477*14c02e41SLaurent Dufour 		}
478*14c02e41SLaurent Dufour 		/* We will enter mm_fault_error() below */
479*14c02e41SLaurent Dufour 	}
480*14c02e41SLaurent Dufour 
4819be72573SBenjamin Herrenschmidt 	if (unlikely(fault & (VM_FAULT_RETRY|VM_FAULT_ERROR))) {
48233692f27SLinus Torvalds 		if (fault & VM_FAULT_SIGSEGV)
48333692f27SLinus Torvalds 			goto bad_area;
484ba12eedeSLi Zhong 		rc = mm_fault_error(regs, address, fault);
4859be72573SBenjamin Herrenschmidt 		if (rc >= MM_FAULT_RETURN)
486ba12eedeSLi Zhong 			goto bail;
487ba12eedeSLi Zhong 		else
488ba12eedeSLi Zhong 			rc = 0;
48914cf11afSPaul Mackerras 	}
4909be72573SBenjamin Herrenschmidt 
4919be72573SBenjamin Herrenschmidt 	/*
492*14c02e41SLaurent Dufour 	 * Major/minor page fault accounting.
4939be72573SBenjamin Herrenschmidt 	 */
4949be72573SBenjamin Herrenschmidt 	if (fault & VM_FAULT_MAJOR) {
49583c54070SNick Piggin 		current->maj_flt++;
496a8b0ca17SPeter Zijlstra 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
49778f13e95SPeter Zijlstra 			      regs, address);
49840900194SBrian King #ifdef CONFIG_PPC_SMLPAR
49940900194SBrian King 		if (firmware_has_feature(FW_FEATURE_CMO)) {
5007ffcf8ecSAnton Blanchard 			u32 page_ins;
5017ffcf8ecSAnton Blanchard 
50240900194SBrian King 			preempt_disable();
5037ffcf8ecSAnton Blanchard 			page_ins = be32_to_cpu(get_lppaca()->page_ins);
5047ffcf8ecSAnton Blanchard 			page_ins += 1 << PAGE_FACTOR;
5057ffcf8ecSAnton Blanchard 			get_lppaca()->page_ins = cpu_to_be32(page_ins);
50640900194SBrian King 			preempt_enable();
50740900194SBrian King 		}
5089be72573SBenjamin Herrenschmidt #endif /* CONFIG_PPC_SMLPAR */
509ac17dc8eSPeter Zijlstra 	} else {
51083c54070SNick Piggin 		current->min_flt++;
511a8b0ca17SPeter Zijlstra 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
51278f13e95SPeter Zijlstra 			      regs, address);
513ac17dc8eSPeter Zijlstra 	}
5149be72573SBenjamin Herrenschmidt 
51514cf11afSPaul Mackerras 	up_read(&mm->mmap_sem);
516ba12eedeSLi Zhong 	goto bail;
51714cf11afSPaul Mackerras 
51814cf11afSPaul Mackerras bad_area:
51914cf11afSPaul Mackerras 	up_read(&mm->mmap_sem);
52014cf11afSPaul Mackerras 
52114cf11afSPaul Mackerras bad_area_nosemaphore:
52214cf11afSPaul Mackerras 	/* User mode accesses cause a SIGSEGV */
52314cf11afSPaul Mackerras 	if (user_mode(regs)) {
52414cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, code, address);
525ba12eedeSLi Zhong 		goto bail;
52614cf11afSPaul Mackerras 	}
52714cf11afSPaul Mackerras 
52876462232SChristian Dietrich 	if (is_exec && (error_code & DSISR_PROTFAULT))
52976462232SChristian Dietrich 		printk_ratelimited(KERN_CRIT "kernel tried to execute NX-protected"
53014cf11afSPaul Mackerras 				   " page (%lx) - exploit attempt? (uid: %d)\n",
5319e184e0aSEric W. Biederman 				   address, from_kuid(&init_user_ns, current_uid()));
53214cf11afSPaul Mackerras 
533ba12eedeSLi Zhong 	rc = SIGSEGV;
534ba12eedeSLi Zhong 
535ba12eedeSLi Zhong bail:
536ba12eedeSLi Zhong 	exception_exit(prev_state);
537ba12eedeSLi Zhong 	return rc;
53814cf11afSPaul Mackerras }
53903465f89SNicholas Piggin NOKPROBE_SYMBOL(do_page_fault);
54014cf11afSPaul Mackerras 
54114cf11afSPaul Mackerras /*
54214cf11afSPaul Mackerras  * bad_page_fault is called when we have a bad access from the kernel.
54314cf11afSPaul Mackerras  * It is called from the DSI and ISI handlers in head.S and from some
54414cf11afSPaul Mackerras  * of the procedures in traps.c.
54514cf11afSPaul Mackerras  */
54614cf11afSPaul Mackerras void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
54714cf11afSPaul Mackerras {
54814cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
54914cf11afSPaul Mackerras 
55014cf11afSPaul Mackerras 	/* Are we prepared to handle this fault?  */
55114cf11afSPaul Mackerras 	if ((entry = search_exception_tables(regs->nip)) != NULL) {
55261a92f70SNicholas Piggin 		regs->nip = extable_fixup(entry);
55314cf11afSPaul Mackerras 		return;
55414cf11afSPaul Mackerras 	}
55514cf11afSPaul Mackerras 
55614cf11afSPaul Mackerras 	/* kernel has accessed a bad area */
557723925b7SOlof Johansson 
558723925b7SOlof Johansson 	switch (regs->trap) {
559723925b7SOlof Johansson 	case 0x300:
560723925b7SOlof Johansson 	case 0x380:
561a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
562a416dd8dSMichael Ellerman 			"data at address 0x%08lx\n", regs->dar);
563723925b7SOlof Johansson 		break;
564723925b7SOlof Johansson 	case 0x400:
565723925b7SOlof Johansson 	case 0x480:
566a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
567a416dd8dSMichael Ellerman 			"instruction fetch\n");
568723925b7SOlof Johansson 		break;
569eab861a7SAnton Blanchard 	case 0x600:
570eab861a7SAnton Blanchard 		printk(KERN_ALERT "Unable to handle kernel paging request for "
571eab861a7SAnton Blanchard 			"unaligned access at address 0x%08lx\n", regs->dar);
572eab861a7SAnton Blanchard 		break;
573723925b7SOlof Johansson 	default:
574a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
575a416dd8dSMichael Ellerman 			"unknown fault\n");
576a416dd8dSMichael Ellerman 		break;
577723925b7SOlof Johansson 	}
578723925b7SOlof Johansson 	printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
579723925b7SOlof Johansson 		regs->nip);
580723925b7SOlof Johansson 
581a70857e4SAaron Tomlin 	if (task_stack_end_corrupted(current))
58228b54990SAnton Blanchard 		printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
58328b54990SAnton Blanchard 
58414cf11afSPaul Mackerras 	die("Kernel access of bad area", regs, sig);
58514cf11afSPaul Mackerras }
586