xref: /openbmc/linux/arch/powerpc/mm/fault.c (revision 33692f27597fcab536d7cbbcc8f52905133e4aa7)
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>
2014cf11afSPaul Mackerras #include <linux/kernel.h>
2114cf11afSPaul Mackerras #include <linux/errno.h>
2214cf11afSPaul Mackerras #include <linux/string.h>
2314cf11afSPaul Mackerras #include <linux/types.h>
2414cf11afSPaul Mackerras #include <linux/ptrace.h>
2514cf11afSPaul Mackerras #include <linux/mman.h>
2614cf11afSPaul Mackerras #include <linux/mm.h>
2714cf11afSPaul Mackerras #include <linux/interrupt.h>
2814cf11afSPaul Mackerras #include <linux/highmem.h>
2914cf11afSPaul Mackerras #include <linux/module.h>
3014cf11afSPaul Mackerras #include <linux/kprobes.h>
311eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
32cdd6c482SIngo Molnar #include <linux/perf_event.h>
3376462232SChristian Dietrich #include <linux/ratelimit.h>
34ba12eedeSLi Zhong #include <linux/context_tracking.h>
359d57472fSAnton Blanchard #include <linux/hugetlb.h>
3614cf11afSPaul Mackerras 
3740900194SBrian King #include <asm/firmware.h>
3814cf11afSPaul Mackerras #include <asm/page.h>
3914cf11afSPaul Mackerras #include <asm/pgtable.h>
4014cf11afSPaul Mackerras #include <asm/mmu.h>
4114cf11afSPaul Mackerras #include <asm/mmu_context.h>
4214cf11afSPaul Mackerras #include <asm/uaccess.h>
4314cf11afSPaul Mackerras #include <asm/tlbflush.h>
4414cf11afSPaul Mackerras #include <asm/siginfo.h>
45ae3a197eSDavid Howells #include <asm/debug.h>
469f90b997SChristoph Hellwig 
47c3dcf53aSJimi Xenidis #include "icswx.h"
48c3dcf53aSJimi Xenidis 
494f9e87c0SAnil S Keshavamurthy #ifdef CONFIG_KPROBES
509f90b997SChristoph Hellwig static inline int notify_page_fault(struct pt_regs *regs)
514f9e87c0SAnil S Keshavamurthy {
529f90b997SChristoph Hellwig 	int ret = 0;
539f90b997SChristoph Hellwig 
549f90b997SChristoph Hellwig 	/* kprobe_running() needs smp_processor_id() */
559f90b997SChristoph Hellwig 	if (!user_mode(regs)) {
569f90b997SChristoph Hellwig 		preempt_disable();
579f90b997SChristoph Hellwig 		if (kprobe_running() && kprobe_fault_handler(regs, 11))
589f90b997SChristoph Hellwig 			ret = 1;
599f90b997SChristoph Hellwig 		preempt_enable();
604f9e87c0SAnil S Keshavamurthy 	}
614f9e87c0SAnil S Keshavamurthy 
629f90b997SChristoph Hellwig 	return ret;
634f9e87c0SAnil S Keshavamurthy }
644f9e87c0SAnil S Keshavamurthy #else
659f90b997SChristoph Hellwig static inline int notify_page_fault(struct pt_regs *regs)
664f9e87c0SAnil S Keshavamurthy {
679f90b997SChristoph Hellwig 	return 0;
684f9e87c0SAnil S Keshavamurthy }
694f9e87c0SAnil S Keshavamurthy #endif
704f9e87c0SAnil S Keshavamurthy 
7114cf11afSPaul Mackerras /*
7214cf11afSPaul Mackerras  * Check whether the instruction at regs->nip is a store using
7314cf11afSPaul Mackerras  * an update addressing form which will update r1.
7414cf11afSPaul Mackerras  */
7514cf11afSPaul Mackerras static int store_updates_sp(struct pt_regs *regs)
7614cf11afSPaul Mackerras {
7714cf11afSPaul Mackerras 	unsigned int inst;
7814cf11afSPaul Mackerras 
7914cf11afSPaul Mackerras 	if (get_user(inst, (unsigned int __user *)regs->nip))
8014cf11afSPaul Mackerras 		return 0;
8114cf11afSPaul Mackerras 	/* check for 1 in the rA field */
8214cf11afSPaul Mackerras 	if (((inst >> 16) & 0x1f) != 1)
8314cf11afSPaul Mackerras 		return 0;
8414cf11afSPaul Mackerras 	/* check major opcode */
8514cf11afSPaul Mackerras 	switch (inst >> 26) {
8614cf11afSPaul Mackerras 	case 37:	/* stwu */
8714cf11afSPaul Mackerras 	case 39:	/* stbu */
8814cf11afSPaul Mackerras 	case 45:	/* sthu */
8914cf11afSPaul Mackerras 	case 53:	/* stfsu */
9014cf11afSPaul Mackerras 	case 55:	/* stfdu */
9114cf11afSPaul Mackerras 		return 1;
9214cf11afSPaul Mackerras 	case 62:	/* std or stdu */
9314cf11afSPaul Mackerras 		return (inst & 3) == 1;
9414cf11afSPaul Mackerras 	case 31:
9514cf11afSPaul Mackerras 		/* check minor opcode */
9614cf11afSPaul Mackerras 		switch ((inst >> 1) & 0x3ff) {
9714cf11afSPaul Mackerras 		case 181:	/* stdux */
9814cf11afSPaul Mackerras 		case 183:	/* stwux */
9914cf11afSPaul Mackerras 		case 247:	/* stbux */
10014cf11afSPaul Mackerras 		case 439:	/* sthux */
10114cf11afSPaul Mackerras 		case 695:	/* stfsux */
10214cf11afSPaul Mackerras 		case 759:	/* stfdux */
10314cf11afSPaul Mackerras 			return 1;
10414cf11afSPaul Mackerras 		}
10514cf11afSPaul Mackerras 	}
10614cf11afSPaul Mackerras 	return 0;
10714cf11afSPaul Mackerras }
1089be72573SBenjamin Herrenschmidt /*
1099be72573SBenjamin Herrenschmidt  * do_page_fault error handling helpers
1109be72573SBenjamin Herrenschmidt  */
1119be72573SBenjamin Herrenschmidt 
1129be72573SBenjamin Herrenschmidt #define MM_FAULT_RETURN		0
1139be72573SBenjamin Herrenschmidt #define MM_FAULT_CONTINUE	-1
1149be72573SBenjamin Herrenschmidt #define MM_FAULT_ERR(sig)	(sig)
1159be72573SBenjamin Herrenschmidt 
1163913fdd7SAnton Blanchard static int do_sigbus(struct pt_regs *regs, unsigned long address,
1173913fdd7SAnton Blanchard 		     unsigned int fault)
1189be72573SBenjamin Herrenschmidt {
1199be72573SBenjamin Herrenschmidt 	siginfo_t info;
1209d57472fSAnton Blanchard 	unsigned int lsb = 0;
1219be72573SBenjamin Herrenschmidt 
1229be72573SBenjamin Herrenschmidt 	up_read(&current->mm->mmap_sem);
1239be72573SBenjamin Herrenschmidt 
12463af5262SAnton Blanchard 	if (!user_mode(regs))
12563af5262SAnton Blanchard 		return MM_FAULT_ERR(SIGBUS);
12663af5262SAnton Blanchard 
12741ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = BUS_ADRERR;
1289be72573SBenjamin Herrenschmidt 	info.si_signo = SIGBUS;
1299be72573SBenjamin Herrenschmidt 	info.si_errno = 0;
1309be72573SBenjamin Herrenschmidt 	info.si_code = BUS_ADRERR;
1319be72573SBenjamin Herrenschmidt 	info.si_addr = (void __user *)address;
1323913fdd7SAnton Blanchard #ifdef CONFIG_MEMORY_FAILURE
1333913fdd7SAnton Blanchard 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
1343913fdd7SAnton Blanchard 		pr_err("MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
1353913fdd7SAnton Blanchard 			current->comm, current->pid, address);
1363913fdd7SAnton Blanchard 		info.si_code = BUS_MCEERR_AR;
1373913fdd7SAnton Blanchard 	}
1389d57472fSAnton Blanchard 
1399d57472fSAnton Blanchard 	if (fault & VM_FAULT_HWPOISON_LARGE)
1409d57472fSAnton Blanchard 		lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
1419d57472fSAnton Blanchard 	if (fault & VM_FAULT_HWPOISON)
1429d57472fSAnton Blanchard 		lsb = PAGE_SHIFT;
1433913fdd7SAnton Blanchard #endif
1449d57472fSAnton Blanchard 	info.si_addr_lsb = lsb;
1459be72573SBenjamin Herrenschmidt 	force_sig_info(SIGBUS, &info, current);
1469be72573SBenjamin Herrenschmidt 	return MM_FAULT_RETURN;
1479be72573SBenjamin Herrenschmidt }
1489be72573SBenjamin Herrenschmidt 
1499be72573SBenjamin Herrenschmidt static int mm_fault_error(struct pt_regs *regs, unsigned long addr, int fault)
1509be72573SBenjamin Herrenschmidt {
1519be72573SBenjamin Herrenschmidt 	/*
1529be72573SBenjamin Herrenschmidt 	 * Pagefault was interrupted by SIGKILL. We have no reason to
1539be72573SBenjamin Herrenschmidt 	 * continue the pagefault.
1549be72573SBenjamin Herrenschmidt 	 */
1559be72573SBenjamin Herrenschmidt 	if (fatal_signal_pending(current)) {
1569be72573SBenjamin Herrenschmidt 		/*
1579be72573SBenjamin Herrenschmidt 		 * If we have retry set, the mmap semaphore will have
1589be72573SBenjamin Herrenschmidt 		 * alrady been released in __lock_page_or_retry(). Else
1599be72573SBenjamin Herrenschmidt 		 * we release it now.
1609be72573SBenjamin Herrenschmidt 		 */
1619be72573SBenjamin Herrenschmidt 		if (!(fault & VM_FAULT_RETRY))
1629be72573SBenjamin Herrenschmidt 			up_read(&current->mm->mmap_sem);
1639be72573SBenjamin Herrenschmidt 		/* Coming from kernel, we need to deal with uaccess fixups */
1649be72573SBenjamin Herrenschmidt 		if (user_mode(regs))
1659be72573SBenjamin Herrenschmidt 			return MM_FAULT_RETURN;
1669be72573SBenjamin Herrenschmidt 		return MM_FAULT_ERR(SIGKILL);
1679be72573SBenjamin Herrenschmidt 	}
1689be72573SBenjamin Herrenschmidt 
1699be72573SBenjamin Herrenschmidt 	/* No fault: be happy */
1709be72573SBenjamin Herrenschmidt 	if (!(fault & VM_FAULT_ERROR))
1719be72573SBenjamin Herrenschmidt 		return MM_FAULT_CONTINUE;
1729be72573SBenjamin Herrenschmidt 
1739be72573SBenjamin Herrenschmidt 	/* Out of memory */
174c2d23f91SDavid Rientjes 	if (fault & VM_FAULT_OOM) {
175c2d23f91SDavid Rientjes 		up_read(&current->mm->mmap_sem);
176c2d23f91SDavid Rientjes 
177c2d23f91SDavid Rientjes 		/*
178c2d23f91SDavid Rientjes 		 * We ran out of memory, or some other thing happened to us that
179c2d23f91SDavid Rientjes 		 * made us unable to handle the page fault gracefully.
180c2d23f91SDavid Rientjes 		 */
181c2d23f91SDavid Rientjes 		if (!user_mode(regs))
182c2d23f91SDavid Rientjes 			return MM_FAULT_ERR(SIGKILL);
183c2d23f91SDavid Rientjes 		pagefault_out_of_memory();
184c2d23f91SDavid Rientjes 		return MM_FAULT_RETURN;
185c2d23f91SDavid Rientjes 	}
1869be72573SBenjamin Herrenschmidt 
1873913fdd7SAnton Blanchard 	if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE))
1883913fdd7SAnton Blanchard 		return do_sigbus(regs, addr, fault);
1899be72573SBenjamin Herrenschmidt 
1909be72573SBenjamin Herrenschmidt 	/* We don't understand the fault code, this is fatal */
1919be72573SBenjamin Herrenschmidt 	BUG();
1929be72573SBenjamin Herrenschmidt 	return MM_FAULT_CONTINUE;
1939be72573SBenjamin Herrenschmidt }
19414cf11afSPaul Mackerras 
19514cf11afSPaul Mackerras /*
19614cf11afSPaul Mackerras  * For 600- and 800-family processors, the error_code parameter is DSISR
19714cf11afSPaul Mackerras  * for a data fault, SRR1 for an instruction fault. For 400-family processors
19814cf11afSPaul Mackerras  * the error_code parameter is ESR for a data fault, 0 for an instruction
19914cf11afSPaul Mackerras  * fault.
20014cf11afSPaul Mackerras  * For 64-bit processors, the error_code parameter is
20114cf11afSPaul Mackerras  *  - DSISR for a non-SLB data access fault,
20214cf11afSPaul Mackerras  *  - SRR1 & 0x08000000 for a non-SLB instruction access fault
20314cf11afSPaul Mackerras  *  - 0 any SLB fault.
20414cf11afSPaul Mackerras  *
20514cf11afSPaul Mackerras  * The return value is 0 if the fault was handled, or the signal
20614cf11afSPaul Mackerras  * number if this is a kernel fault that can't be handled here.
20714cf11afSPaul Mackerras  */
20814cf11afSPaul Mackerras int __kprobes do_page_fault(struct pt_regs *regs, unsigned long address,
20914cf11afSPaul Mackerras 			    unsigned long error_code)
21014cf11afSPaul Mackerras {
211ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
21214cf11afSPaul Mackerras 	struct vm_area_struct * vma;
21314cf11afSPaul Mackerras 	struct mm_struct *mm = current->mm;
2149be72573SBenjamin Herrenschmidt 	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
21514cf11afSPaul Mackerras 	int code = SEGV_MAPERR;
2169be72573SBenjamin Herrenschmidt 	int is_write = 0;
21714cf11afSPaul Mackerras 	int trap = TRAP(regs);
21814cf11afSPaul Mackerras  	int is_exec = trap == 0x400;
2199be72573SBenjamin Herrenschmidt 	int fault;
22069e044ddSAneesh Kumar K.V 	int rc = 0, store_update_sp = 0;
22114cf11afSPaul Mackerras 
22214cf11afSPaul Mackerras #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
22314cf11afSPaul Mackerras 	/*
22414cf11afSPaul Mackerras 	 * Fortunately the bit assignments in SRR1 for an instruction
22514cf11afSPaul Mackerras 	 * fault and DSISR for a data fault are mostly the same for the
22614cf11afSPaul Mackerras 	 * bits we are interested in.  But there are some bits which
22714cf11afSPaul Mackerras 	 * indicate errors in DSISR but can validly be set in SRR1.
22814cf11afSPaul Mackerras 	 */
22914cf11afSPaul Mackerras 	if (trap == 0x400)
23014cf11afSPaul Mackerras 		error_code &= 0x48200000;
23114cf11afSPaul Mackerras 	else
23214cf11afSPaul Mackerras 		is_write = error_code & DSISR_ISSTORE;
23314cf11afSPaul Mackerras #else
23414cf11afSPaul Mackerras 	is_write = error_code & ESR_DST;
23514cf11afSPaul Mackerras #endif /* CONFIG_4xx || CONFIG_BOOKE */
23614cf11afSPaul Mackerras 
237c3dcf53aSJimi Xenidis #ifdef CONFIG_PPC_ICSWX
238c3dcf53aSJimi Xenidis 	/*
239c3dcf53aSJimi Xenidis 	 * we need to do this early because this "data storage
240c3dcf53aSJimi Xenidis 	 * interrupt" does not update the DAR/DEAR so we don't want to
241c3dcf53aSJimi Xenidis 	 * look at it
242c3dcf53aSJimi Xenidis 	 */
243c3dcf53aSJimi Xenidis 	if (error_code & ICSWX_DSI_UCT) {
244ba12eedeSLi Zhong 		rc = acop_handle_fault(regs, address, error_code);
2459be72573SBenjamin Herrenschmidt 		if (rc)
246ba12eedeSLi Zhong 			goto bail;
247c3dcf53aSJimi Xenidis 	}
2489be72573SBenjamin Herrenschmidt #endif /* CONFIG_PPC_ICSWX */
249c3dcf53aSJimi Xenidis 
2509f90b997SChristoph Hellwig 	if (notify_page_fault(regs))
251ba12eedeSLi Zhong 		goto bail;
25214cf11afSPaul Mackerras 
253c3b75bd7SMichael Neuling 	if (unlikely(debugger_fault_handler(regs)))
254ba12eedeSLi Zhong 		goto bail;
25514cf11afSPaul Mackerras 
25614cf11afSPaul Mackerras 	/* On a kernel SLB miss we can only check for a valid exception entry */
257ba12eedeSLi Zhong 	if (!user_mode(regs) && (address >= TASK_SIZE)) {
258ba12eedeSLi Zhong 		rc = SIGSEGV;
259ba12eedeSLi Zhong 		goto bail;
260ba12eedeSLi Zhong 	}
26114cf11afSPaul Mackerras 
2629c7cc234SK.Prasad #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE) || \
2639c7cc234SK.Prasad 			     defined(CONFIG_PPC_BOOK3S_64))
26414cf11afSPaul Mackerras   	if (error_code & DSISR_DABRMATCH) {
2659422de3eSMichael Neuling 		/* breakpoint match */
2669422de3eSMichael Neuling 		do_break(regs, address, error_code);
267ba12eedeSLi Zhong 		goto bail;
26814cf11afSPaul Mackerras 	}
2699c7cc234SK.Prasad #endif
27014cf11afSPaul Mackerras 
271a546498fSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
272a546498fSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
273a546498fSBenjamin Herrenschmidt 		local_irq_enable();
274a546498fSBenjamin Herrenschmidt 
27514cf11afSPaul Mackerras 	if (in_atomic() || mm == NULL) {
276ba12eedeSLi Zhong 		if (!user_mode(regs)) {
277ba12eedeSLi Zhong 			rc = SIGSEGV;
278ba12eedeSLi Zhong 			goto bail;
279ba12eedeSLi Zhong 		}
28014cf11afSPaul Mackerras 		/* in_atomic() in user mode is really bad,
28114cf11afSPaul Mackerras 		   as is current->mm == NULL. */
28214cf11afSPaul Mackerras 		printk(KERN_EMERG "Page fault in user mode with "
28314cf11afSPaul Mackerras 		       "in_atomic() = %d mm = %p\n", in_atomic(), mm);
28414cf11afSPaul Mackerras 		printk(KERN_EMERG "NIP = %lx  MSR = %lx\n",
28514cf11afSPaul Mackerras 		       regs->nip, regs->msr);
28614cf11afSPaul Mackerras 		die("Weird page fault", regs, SIGSEGV);
28714cf11afSPaul Mackerras 	}
28814cf11afSPaul Mackerras 
289a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
2907dd1fcc2SPeter Zijlstra 
29169e044ddSAneesh Kumar K.V 	/*
29269e044ddSAneesh Kumar K.V 	 * We want to do this outside mmap_sem, because reading code around nip
29369e044ddSAneesh Kumar K.V 	 * can result in fault, which will cause a deadlock when called with
29469e044ddSAneesh Kumar K.V 	 * mmap_sem held
29569e044ddSAneesh Kumar K.V 	 */
29669e044ddSAneesh Kumar K.V 	if (user_mode(regs))
29769e044ddSAneesh Kumar K.V 		store_update_sp = store_updates_sp(regs);
29869e044ddSAneesh Kumar K.V 
299759496baSJohannes Weiner 	if (user_mode(regs))
300759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
301759496baSJohannes Weiner 
30214cf11afSPaul Mackerras 	/* When running in the kernel we expect faults to occur only to
30314cf11afSPaul Mackerras 	 * addresses in user space.  All other faults represent errors in the
304fc5266eaSAnton Blanchard 	 * kernel and should generate an OOPS.  Unfortunately, in the case of an
305fc5266eaSAnton Blanchard 	 * erroneous fault occurring in a code path which already holds mmap_sem
30614cf11afSPaul Mackerras 	 * we will deadlock attempting to validate the fault against the
30714cf11afSPaul Mackerras 	 * address space.  Luckily the kernel only validly references user
30814cf11afSPaul Mackerras 	 * space from well defined areas of code, which are listed in the
30914cf11afSPaul Mackerras 	 * exceptions table.
31014cf11afSPaul Mackerras 	 *
31114cf11afSPaul Mackerras 	 * As the vast majority of faults will be valid we will only perform
312fc5266eaSAnton Blanchard 	 * the source reference check when there is a possibility of a deadlock.
31314cf11afSPaul Mackerras 	 * Attempt to lock the address space, if we cannot we then validate the
31414cf11afSPaul Mackerras 	 * source.  If this is invalid we can skip the address space check,
31514cf11afSPaul Mackerras 	 * thus avoiding the deadlock.
31614cf11afSPaul Mackerras 	 */
31714cf11afSPaul Mackerras 	if (!down_read_trylock(&mm->mmap_sem)) {
31814cf11afSPaul Mackerras 		if (!user_mode(regs) && !search_exception_tables(regs->nip))
31914cf11afSPaul Mackerras 			goto bad_area_nosemaphore;
32014cf11afSPaul Mackerras 
3219be72573SBenjamin Herrenschmidt retry:
32214cf11afSPaul Mackerras 		down_read(&mm->mmap_sem);
323a546498fSBenjamin Herrenschmidt 	} else {
324a546498fSBenjamin Herrenschmidt 		/*
325a546498fSBenjamin Herrenschmidt 		 * The above down_read_trylock() might have succeeded in
326a546498fSBenjamin Herrenschmidt 		 * which case we'll have missed the might_sleep() from
327a546498fSBenjamin Herrenschmidt 		 * down_read():
328a546498fSBenjamin Herrenschmidt 		 */
329a546498fSBenjamin Herrenschmidt 		might_sleep();
33014cf11afSPaul Mackerras 	}
33114cf11afSPaul Mackerras 
33214cf11afSPaul Mackerras 	vma = find_vma(mm, address);
33314cf11afSPaul Mackerras 	if (!vma)
33414cf11afSPaul Mackerras 		goto bad_area;
33514cf11afSPaul Mackerras 	if (vma->vm_start <= address)
33614cf11afSPaul Mackerras 		goto good_area;
33714cf11afSPaul Mackerras 	if (!(vma->vm_flags & VM_GROWSDOWN))
33814cf11afSPaul Mackerras 		goto bad_area;
33914cf11afSPaul Mackerras 
34014cf11afSPaul Mackerras 	/*
34114cf11afSPaul Mackerras 	 * N.B. The POWER/Open ABI allows programs to access up to
34214cf11afSPaul Mackerras 	 * 288 bytes below the stack pointer.
34314cf11afSPaul Mackerras 	 * The kernel signal delivery code writes up to about 1.5kB
34414cf11afSPaul Mackerras 	 * below the stack pointer (r1) before decrementing it.
34514cf11afSPaul Mackerras 	 * The exec code can write slightly over 640kB to the stack
34614cf11afSPaul Mackerras 	 * before setting the user r1.  Thus we allow the stack to
34714cf11afSPaul Mackerras 	 * expand to 1MB without further checks.
34814cf11afSPaul Mackerras 	 */
34914cf11afSPaul Mackerras 	if (address + 0x100000 < vma->vm_end) {
35014cf11afSPaul Mackerras 		/* get user regs even if this fault is in kernel mode */
35114cf11afSPaul Mackerras 		struct pt_regs *uregs = current->thread.regs;
35214cf11afSPaul Mackerras 		if (uregs == NULL)
35314cf11afSPaul Mackerras 			goto bad_area;
35414cf11afSPaul Mackerras 
35514cf11afSPaul Mackerras 		/*
35614cf11afSPaul Mackerras 		 * A user-mode access to an address a long way below
35714cf11afSPaul Mackerras 		 * the stack pointer is only valid if the instruction
35814cf11afSPaul Mackerras 		 * is one which would update the stack pointer to the
35914cf11afSPaul Mackerras 		 * address accessed if the instruction completed,
36014cf11afSPaul Mackerras 		 * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
36114cf11afSPaul Mackerras 		 * (or the byte, halfword, float or double forms).
36214cf11afSPaul Mackerras 		 *
36314cf11afSPaul Mackerras 		 * If we don't check this then any write to the area
36414cf11afSPaul Mackerras 		 * between the last mapped region and the stack will
36514cf11afSPaul Mackerras 		 * expand the stack rather than segfaulting.
36614cf11afSPaul Mackerras 		 */
36769e044ddSAneesh Kumar K.V 		if (address + 2048 < uregs->gpr[1] && !store_update_sp)
36814cf11afSPaul Mackerras 			goto bad_area;
36914cf11afSPaul Mackerras 	}
37014cf11afSPaul Mackerras 	if (expand_stack(vma, address))
37114cf11afSPaul Mackerras 		goto bad_area;
37214cf11afSPaul Mackerras 
37314cf11afSPaul Mackerras good_area:
37414cf11afSPaul Mackerras 	code = SEGV_ACCERR;
37514cf11afSPaul Mackerras #if defined(CONFIG_6xx)
37614cf11afSPaul Mackerras 	if (error_code & 0x95700000)
37714cf11afSPaul Mackerras 		/* an error such as lwarx to I/O controller space,
37814cf11afSPaul Mackerras 		   address matching DABR, eciwx, etc. */
37914cf11afSPaul Mackerras 		goto bad_area;
38014cf11afSPaul Mackerras #endif /* CONFIG_6xx */
38114cf11afSPaul Mackerras #if defined(CONFIG_8xx)
38214cf11afSPaul Mackerras         /* The MPC8xx seems to always set 0x80000000, which is
38314cf11afSPaul Mackerras          * "undefined".  Of those that can be set, this is the only
38414cf11afSPaul Mackerras          * one which seems bad.
38514cf11afSPaul Mackerras          */
38614cf11afSPaul Mackerras 	if (error_code & 0x10000000)
38714cf11afSPaul Mackerras                 /* Guarded storage error. */
38814cf11afSPaul Mackerras 		goto bad_area;
38914cf11afSPaul Mackerras #endif /* CONFIG_8xx */
39014cf11afSPaul Mackerras 
39114cf11afSPaul Mackerras 	if (is_exec) {
3928d30c14cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_STD_MMU
3938d30c14cSBenjamin Herrenschmidt 		/* Protection fault on exec go straight to failure on
3948d30c14cSBenjamin Herrenschmidt 		 * Hash based MMUs as they either don't support per-page
3958d30c14cSBenjamin Herrenschmidt 		 * execute permission, or if they do, it's handled already
3968d30c14cSBenjamin Herrenschmidt 		 * at the hash level. This test would probably have to
3978d30c14cSBenjamin Herrenschmidt 		 * be removed if we change the way this works to make hash
3988d30c14cSBenjamin Herrenschmidt 		 * processors use the same I/D cache coherency mechanism
3998d30c14cSBenjamin Herrenschmidt 		 * as embedded.
4008d30c14cSBenjamin Herrenschmidt 		 */
40114cf11afSPaul Mackerras 		if (error_code & DSISR_PROTFAULT)
40214cf11afSPaul Mackerras 			goto bad_area;
4038d30c14cSBenjamin Herrenschmidt #endif /* CONFIG_PPC_STD_MMU */
4048d30c14cSBenjamin Herrenschmidt 
40508ae6cc1SPaul Mackerras 		/*
40608ae6cc1SPaul Mackerras 		 * Allow execution from readable areas if the MMU does not
40708ae6cc1SPaul Mackerras 		 * provide separate controls over reading and executing.
4088d30c14cSBenjamin Herrenschmidt 		 *
4098d30c14cSBenjamin Herrenschmidt 		 * Note: That code used to not be enabled for 4xx/BookE.
4108d30c14cSBenjamin Herrenschmidt 		 * It is now as I/D cache coherency for these is done at
4118d30c14cSBenjamin Herrenschmidt 		 * set_pte_at() time and I see no reason why the test
4128d30c14cSBenjamin Herrenschmidt 		 * below wouldn't be valid on those processors. This -may-
4138d30c14cSBenjamin Herrenschmidt 		 * break programs compiled with a really old ABI though.
41408ae6cc1SPaul Mackerras 		 */
41508ae6cc1SPaul Mackerras 		if (!(vma->vm_flags & VM_EXEC) &&
41608ae6cc1SPaul Mackerras 		    (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
41708ae6cc1SPaul Mackerras 		     !(vma->vm_flags & (VM_READ | VM_WRITE))))
41814cf11afSPaul Mackerras 			goto bad_area;
41914cf11afSPaul Mackerras 	/* a write */
42014cf11afSPaul Mackerras 	} else if (is_write) {
42114cf11afSPaul Mackerras 		if (!(vma->vm_flags & VM_WRITE))
42214cf11afSPaul Mackerras 			goto bad_area;
423759496baSJohannes Weiner 		flags |= FAULT_FLAG_WRITE;
42414cf11afSPaul Mackerras 	/* a read */
42514cf11afSPaul Mackerras 	} else {
42614cf11afSPaul Mackerras 		/* protection fault */
42714cf11afSPaul Mackerras 		if (error_code & 0x08000000)
42814cf11afSPaul Mackerras 			goto bad_area;
429df67b3daSJason Baron 		if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
43014cf11afSPaul Mackerras 			goto bad_area;
43114cf11afSPaul Mackerras 	}
43214cf11afSPaul Mackerras 
43314cf11afSPaul Mackerras 	/*
43414cf11afSPaul Mackerras 	 * If for any reason at all we couldn't handle the fault,
43514cf11afSPaul Mackerras 	 * make sure we exit gracefully rather than endlessly redo
43614cf11afSPaul Mackerras 	 * the fault.
43714cf11afSPaul Mackerras 	 */
4389be72573SBenjamin Herrenschmidt 	fault = handle_mm_fault(mm, vma, address, flags);
4399be72573SBenjamin Herrenschmidt 	if (unlikely(fault & (VM_FAULT_RETRY|VM_FAULT_ERROR))) {
440*33692f27SLinus Torvalds 		if (fault & VM_FAULT_SIGSEGV)
441*33692f27SLinus Torvalds 			goto bad_area;
442ba12eedeSLi Zhong 		rc = mm_fault_error(regs, address, fault);
4439be72573SBenjamin Herrenschmidt 		if (rc >= MM_FAULT_RETURN)
444ba12eedeSLi Zhong 			goto bail;
445ba12eedeSLi Zhong 		else
446ba12eedeSLi Zhong 			rc = 0;
44714cf11afSPaul Mackerras 	}
4489be72573SBenjamin Herrenschmidt 
4499be72573SBenjamin Herrenschmidt 	/*
4509be72573SBenjamin Herrenschmidt 	 * Major/minor page fault accounting is only done on the
4519be72573SBenjamin Herrenschmidt 	 * initial attempt. If we go through a retry, it is extremely
4529be72573SBenjamin Herrenschmidt 	 * likely that the page will be found in page cache at that point.
4539be72573SBenjamin Herrenschmidt 	 */
4549be72573SBenjamin Herrenschmidt 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
4559be72573SBenjamin Herrenschmidt 		if (fault & VM_FAULT_MAJOR) {
45683c54070SNick Piggin 			current->maj_flt++;
457a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
45878f13e95SPeter Zijlstra 				      regs, address);
45940900194SBrian King #ifdef CONFIG_PPC_SMLPAR
46040900194SBrian King 			if (firmware_has_feature(FW_FEATURE_CMO)) {
4617ffcf8ecSAnton Blanchard 				u32 page_ins;
4627ffcf8ecSAnton Blanchard 
46340900194SBrian King 				preempt_disable();
4647ffcf8ecSAnton Blanchard 				page_ins = be32_to_cpu(get_lppaca()->page_ins);
4657ffcf8ecSAnton Blanchard 				page_ins += 1 << PAGE_FACTOR;
4667ffcf8ecSAnton Blanchard 				get_lppaca()->page_ins = cpu_to_be32(page_ins);
46740900194SBrian King 				preempt_enable();
46840900194SBrian King 			}
4699be72573SBenjamin Herrenschmidt #endif /* CONFIG_PPC_SMLPAR */
470ac17dc8eSPeter Zijlstra 		} else {
47183c54070SNick Piggin 			current->min_flt++;
472a8b0ca17SPeter Zijlstra 			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
47378f13e95SPeter Zijlstra 				      regs, address);
474ac17dc8eSPeter Zijlstra 		}
4759be72573SBenjamin Herrenschmidt 		if (fault & VM_FAULT_RETRY) {
4769be72573SBenjamin Herrenschmidt 			/* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
4779be72573SBenjamin Herrenschmidt 			 * of starvation. */
4789be72573SBenjamin Herrenschmidt 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
47945cac65bSShaohua Li 			flags |= FAULT_FLAG_TRIED;
4809be72573SBenjamin Herrenschmidt 			goto retry;
4819be72573SBenjamin Herrenschmidt 		}
4829be72573SBenjamin Herrenschmidt 	}
4839be72573SBenjamin Herrenschmidt 
48414cf11afSPaul Mackerras 	up_read(&mm->mmap_sem);
485ba12eedeSLi Zhong 	goto bail;
48614cf11afSPaul Mackerras 
48714cf11afSPaul Mackerras bad_area:
48814cf11afSPaul Mackerras 	up_read(&mm->mmap_sem);
48914cf11afSPaul Mackerras 
49014cf11afSPaul Mackerras bad_area_nosemaphore:
49114cf11afSPaul Mackerras 	/* User mode accesses cause a SIGSEGV */
49214cf11afSPaul Mackerras 	if (user_mode(regs)) {
49314cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, code, address);
494ba12eedeSLi Zhong 		goto bail;
49514cf11afSPaul Mackerras 	}
49614cf11afSPaul Mackerras 
49776462232SChristian Dietrich 	if (is_exec && (error_code & DSISR_PROTFAULT))
49876462232SChristian Dietrich 		printk_ratelimited(KERN_CRIT "kernel tried to execute NX-protected"
49914cf11afSPaul Mackerras 				   " page (%lx) - exploit attempt? (uid: %d)\n",
5009e184e0aSEric W. Biederman 				   address, from_kuid(&init_user_ns, current_uid()));
50114cf11afSPaul Mackerras 
502ba12eedeSLi Zhong 	rc = SIGSEGV;
503ba12eedeSLi Zhong 
504ba12eedeSLi Zhong bail:
505ba12eedeSLi Zhong 	exception_exit(prev_state);
506ba12eedeSLi Zhong 	return rc;
50714cf11afSPaul Mackerras 
50814cf11afSPaul Mackerras }
50914cf11afSPaul Mackerras 
51014cf11afSPaul Mackerras /*
51114cf11afSPaul Mackerras  * bad_page_fault is called when we have a bad access from the kernel.
51214cf11afSPaul Mackerras  * It is called from the DSI and ISI handlers in head.S and from some
51314cf11afSPaul Mackerras  * of the procedures in traps.c.
51414cf11afSPaul Mackerras  */
51514cf11afSPaul Mackerras void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
51614cf11afSPaul Mackerras {
51714cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
51814cf11afSPaul Mackerras 
51914cf11afSPaul Mackerras 	/* Are we prepared to handle this fault?  */
52014cf11afSPaul Mackerras 	if ((entry = search_exception_tables(regs->nip)) != NULL) {
52114cf11afSPaul Mackerras 		regs->nip = entry->fixup;
52214cf11afSPaul Mackerras 		return;
52314cf11afSPaul Mackerras 	}
52414cf11afSPaul Mackerras 
52514cf11afSPaul Mackerras 	/* kernel has accessed a bad area */
526723925b7SOlof Johansson 
527723925b7SOlof Johansson 	switch (regs->trap) {
528723925b7SOlof Johansson 	case 0x300:
529723925b7SOlof Johansson 	case 0x380:
530a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
531a416dd8dSMichael Ellerman 			"data at address 0x%08lx\n", regs->dar);
532723925b7SOlof Johansson 		break;
533723925b7SOlof Johansson 	case 0x400:
534723925b7SOlof Johansson 	case 0x480:
535a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
536a416dd8dSMichael Ellerman 			"instruction fetch\n");
537723925b7SOlof Johansson 		break;
538723925b7SOlof Johansson 	default:
539a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
540a416dd8dSMichael Ellerman 			"unknown fault\n");
541a416dd8dSMichael Ellerman 		break;
542723925b7SOlof Johansson 	}
543723925b7SOlof Johansson 	printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
544723925b7SOlof Johansson 		regs->nip);
545723925b7SOlof Johansson 
546a70857e4SAaron Tomlin 	if (task_stack_end_corrupted(current))
54728b54990SAnton Blanchard 		printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
54828b54990SAnton Blanchard 
54914cf11afSPaul Mackerras 	die("Kernel access of bad area", regs, sig);
55014cf11afSPaul Mackerras }
551