xref: /openbmc/linux/arch/powerpc/mm/fault.c (revision a8b0ca17b80e92faab46ee7179ba9e99ccb61233)
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>
3328b54990SAnton Blanchard #include <linux/magic.h>
3414cf11afSPaul Mackerras 
3540900194SBrian King #include <asm/firmware.h>
3614cf11afSPaul Mackerras #include <asm/page.h>
3714cf11afSPaul Mackerras #include <asm/pgtable.h>
3814cf11afSPaul Mackerras #include <asm/mmu.h>
3914cf11afSPaul Mackerras #include <asm/mmu_context.h>
4014cf11afSPaul Mackerras #include <asm/system.h>
4114cf11afSPaul Mackerras #include <asm/uaccess.h>
4214cf11afSPaul Mackerras #include <asm/tlbflush.h>
4314cf11afSPaul Mackerras #include <asm/siginfo.h>
445efab4a0SJoakim Tjernlund #include <mm/mmu_decl.h>
459f90b997SChristoph Hellwig 
464f9e87c0SAnil S Keshavamurthy #ifdef CONFIG_KPROBES
479f90b997SChristoph Hellwig static inline int notify_page_fault(struct pt_regs *regs)
484f9e87c0SAnil S Keshavamurthy {
499f90b997SChristoph Hellwig 	int ret = 0;
509f90b997SChristoph Hellwig 
519f90b997SChristoph Hellwig 	/* kprobe_running() needs smp_processor_id() */
529f90b997SChristoph Hellwig 	if (!user_mode(regs)) {
539f90b997SChristoph Hellwig 		preempt_disable();
549f90b997SChristoph Hellwig 		if (kprobe_running() && kprobe_fault_handler(regs, 11))
559f90b997SChristoph Hellwig 			ret = 1;
569f90b997SChristoph Hellwig 		preempt_enable();
574f9e87c0SAnil S Keshavamurthy 	}
584f9e87c0SAnil S Keshavamurthy 
599f90b997SChristoph Hellwig 	return ret;
604f9e87c0SAnil S Keshavamurthy }
614f9e87c0SAnil S Keshavamurthy #else
629f90b997SChristoph Hellwig static inline int notify_page_fault(struct pt_regs *regs)
634f9e87c0SAnil S Keshavamurthy {
649f90b997SChristoph Hellwig 	return 0;
654f9e87c0SAnil S Keshavamurthy }
664f9e87c0SAnil S Keshavamurthy #endif
674f9e87c0SAnil S Keshavamurthy 
6814cf11afSPaul Mackerras /*
6914cf11afSPaul Mackerras  * Check whether the instruction at regs->nip is a store using
7014cf11afSPaul Mackerras  * an update addressing form which will update r1.
7114cf11afSPaul Mackerras  */
7214cf11afSPaul Mackerras static int store_updates_sp(struct pt_regs *regs)
7314cf11afSPaul Mackerras {
7414cf11afSPaul Mackerras 	unsigned int inst;
7514cf11afSPaul Mackerras 
7614cf11afSPaul Mackerras 	if (get_user(inst, (unsigned int __user *)regs->nip))
7714cf11afSPaul Mackerras 		return 0;
7814cf11afSPaul Mackerras 	/* check for 1 in the rA field */
7914cf11afSPaul Mackerras 	if (((inst >> 16) & 0x1f) != 1)
8014cf11afSPaul Mackerras 		return 0;
8114cf11afSPaul Mackerras 	/* check major opcode */
8214cf11afSPaul Mackerras 	switch (inst >> 26) {
8314cf11afSPaul Mackerras 	case 37:	/* stwu */
8414cf11afSPaul Mackerras 	case 39:	/* stbu */
8514cf11afSPaul Mackerras 	case 45:	/* sthu */
8614cf11afSPaul Mackerras 	case 53:	/* stfsu */
8714cf11afSPaul Mackerras 	case 55:	/* stfdu */
8814cf11afSPaul Mackerras 		return 1;
8914cf11afSPaul Mackerras 	case 62:	/* std or stdu */
9014cf11afSPaul Mackerras 		return (inst & 3) == 1;
9114cf11afSPaul Mackerras 	case 31:
9214cf11afSPaul Mackerras 		/* check minor opcode */
9314cf11afSPaul Mackerras 		switch ((inst >> 1) & 0x3ff) {
9414cf11afSPaul Mackerras 		case 181:	/* stdux */
9514cf11afSPaul Mackerras 		case 183:	/* stwux */
9614cf11afSPaul Mackerras 		case 247:	/* stbux */
9714cf11afSPaul Mackerras 		case 439:	/* sthux */
9814cf11afSPaul Mackerras 		case 695:	/* stfsux */
9914cf11afSPaul Mackerras 		case 759:	/* stfdux */
10014cf11afSPaul Mackerras 			return 1;
10114cf11afSPaul Mackerras 		}
10214cf11afSPaul Mackerras 	}
10314cf11afSPaul Mackerras 	return 0;
10414cf11afSPaul Mackerras }
10514cf11afSPaul Mackerras 
10614cf11afSPaul Mackerras /*
10714cf11afSPaul Mackerras  * For 600- and 800-family processors, the error_code parameter is DSISR
10814cf11afSPaul Mackerras  * for a data fault, SRR1 for an instruction fault. For 400-family processors
10914cf11afSPaul Mackerras  * the error_code parameter is ESR for a data fault, 0 for an instruction
11014cf11afSPaul Mackerras  * fault.
11114cf11afSPaul Mackerras  * For 64-bit processors, the error_code parameter is
11214cf11afSPaul Mackerras  *  - DSISR for a non-SLB data access fault,
11314cf11afSPaul Mackerras  *  - SRR1 & 0x08000000 for a non-SLB instruction access fault
11414cf11afSPaul Mackerras  *  - 0 any SLB fault.
11514cf11afSPaul Mackerras  *
11614cf11afSPaul Mackerras  * The return value is 0 if the fault was handled, or the signal
11714cf11afSPaul Mackerras  * number if this is a kernel fault that can't be handled here.
11814cf11afSPaul Mackerras  */
11914cf11afSPaul Mackerras int __kprobes do_page_fault(struct pt_regs *regs, unsigned long address,
12014cf11afSPaul Mackerras 			    unsigned long error_code)
12114cf11afSPaul Mackerras {
12214cf11afSPaul Mackerras 	struct vm_area_struct * vma;
12314cf11afSPaul Mackerras 	struct mm_struct *mm = current->mm;
12414cf11afSPaul Mackerras 	siginfo_t info;
12514cf11afSPaul Mackerras 	int code = SEGV_MAPERR;
12683c54070SNick Piggin 	int is_write = 0, ret;
12714cf11afSPaul Mackerras 	int trap = TRAP(regs);
12814cf11afSPaul Mackerras  	int is_exec = trap == 0x400;
12914cf11afSPaul Mackerras 
13014cf11afSPaul Mackerras #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
13114cf11afSPaul Mackerras 	/*
13214cf11afSPaul Mackerras 	 * Fortunately the bit assignments in SRR1 for an instruction
13314cf11afSPaul Mackerras 	 * fault and DSISR for a data fault are mostly the same for the
13414cf11afSPaul Mackerras 	 * bits we are interested in.  But there are some bits which
13514cf11afSPaul Mackerras 	 * indicate errors in DSISR but can validly be set in SRR1.
13614cf11afSPaul Mackerras 	 */
13714cf11afSPaul Mackerras 	if (trap == 0x400)
13814cf11afSPaul Mackerras 		error_code &= 0x48200000;
13914cf11afSPaul Mackerras 	else
14014cf11afSPaul Mackerras 		is_write = error_code & DSISR_ISSTORE;
14114cf11afSPaul Mackerras #else
14214cf11afSPaul Mackerras 	is_write = error_code & ESR_DST;
14314cf11afSPaul Mackerras #endif /* CONFIG_4xx || CONFIG_BOOKE */
14414cf11afSPaul Mackerras 
1459f90b997SChristoph Hellwig 	if (notify_page_fault(regs))
14614cf11afSPaul Mackerras 		return 0;
14714cf11afSPaul Mackerras 
148c3b75bd7SMichael Neuling 	if (unlikely(debugger_fault_handler(regs)))
14914cf11afSPaul Mackerras 		return 0;
15014cf11afSPaul Mackerras 
15114cf11afSPaul Mackerras 	/* On a kernel SLB miss we can only check for a valid exception entry */
15214cf11afSPaul Mackerras 	if (!user_mode(regs) && (address >= TASK_SIZE))
15314cf11afSPaul Mackerras 		return SIGSEGV;
15414cf11afSPaul Mackerras 
1559c7cc234SK.Prasad #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE) || \
1569c7cc234SK.Prasad 			     defined(CONFIG_PPC_BOOK3S_64))
15714cf11afSPaul Mackerras   	if (error_code & DSISR_DABRMATCH) {
15814cf11afSPaul Mackerras 		/* DABR match */
159bce6c5fdSAnton Blanchard 		do_dabr(regs, address, error_code);
16014cf11afSPaul Mackerras 		return 0;
16114cf11afSPaul Mackerras 	}
1629c7cc234SK.Prasad #endif
16314cf11afSPaul Mackerras 
16414cf11afSPaul Mackerras 	if (in_atomic() || mm == NULL) {
16514cf11afSPaul Mackerras 		if (!user_mode(regs))
16614cf11afSPaul Mackerras 			return SIGSEGV;
16714cf11afSPaul Mackerras 		/* in_atomic() in user mode is really bad,
16814cf11afSPaul Mackerras 		   as is current->mm == NULL. */
16914cf11afSPaul Mackerras 		printk(KERN_EMERG "Page fault in user mode with "
17014cf11afSPaul Mackerras 		       "in_atomic() = %d mm = %p\n", in_atomic(), mm);
17114cf11afSPaul Mackerras 		printk(KERN_EMERG "NIP = %lx  MSR = %lx\n",
17214cf11afSPaul Mackerras 		       regs->nip, regs->msr);
17314cf11afSPaul Mackerras 		die("Weird page fault", regs, SIGSEGV);
17414cf11afSPaul Mackerras 	}
17514cf11afSPaul Mackerras 
176*a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
1777dd1fcc2SPeter Zijlstra 
17814cf11afSPaul Mackerras 	/* When running in the kernel we expect faults to occur only to
17914cf11afSPaul Mackerras 	 * addresses in user space.  All other faults represent errors in the
180fc5266eaSAnton Blanchard 	 * kernel and should generate an OOPS.  Unfortunately, in the case of an
181fc5266eaSAnton Blanchard 	 * erroneous fault occurring in a code path which already holds mmap_sem
18214cf11afSPaul Mackerras 	 * we will deadlock attempting to validate the fault against the
18314cf11afSPaul Mackerras 	 * address space.  Luckily the kernel only validly references user
18414cf11afSPaul Mackerras 	 * space from well defined areas of code, which are listed in the
18514cf11afSPaul Mackerras 	 * exceptions table.
18614cf11afSPaul Mackerras 	 *
18714cf11afSPaul Mackerras 	 * As the vast majority of faults will be valid we will only perform
188fc5266eaSAnton Blanchard 	 * the source reference check when there is a possibility of a deadlock.
18914cf11afSPaul Mackerras 	 * Attempt to lock the address space, if we cannot we then validate the
19014cf11afSPaul Mackerras 	 * source.  If this is invalid we can skip the address space check,
19114cf11afSPaul Mackerras 	 * thus avoiding the deadlock.
19214cf11afSPaul Mackerras 	 */
19314cf11afSPaul Mackerras 	if (!down_read_trylock(&mm->mmap_sem)) {
19414cf11afSPaul Mackerras 		if (!user_mode(regs) && !search_exception_tables(regs->nip))
19514cf11afSPaul Mackerras 			goto bad_area_nosemaphore;
19614cf11afSPaul Mackerras 
19714cf11afSPaul Mackerras 		down_read(&mm->mmap_sem);
19814cf11afSPaul Mackerras 	}
19914cf11afSPaul Mackerras 
20014cf11afSPaul Mackerras 	vma = find_vma(mm, address);
20114cf11afSPaul Mackerras 	if (!vma)
20214cf11afSPaul Mackerras 		goto bad_area;
20314cf11afSPaul Mackerras 	if (vma->vm_start <= address)
20414cf11afSPaul Mackerras 		goto good_area;
20514cf11afSPaul Mackerras 	if (!(vma->vm_flags & VM_GROWSDOWN))
20614cf11afSPaul Mackerras 		goto bad_area;
20714cf11afSPaul Mackerras 
20814cf11afSPaul Mackerras 	/*
20914cf11afSPaul Mackerras 	 * N.B. The POWER/Open ABI allows programs to access up to
21014cf11afSPaul Mackerras 	 * 288 bytes below the stack pointer.
21114cf11afSPaul Mackerras 	 * The kernel signal delivery code writes up to about 1.5kB
21214cf11afSPaul Mackerras 	 * below the stack pointer (r1) before decrementing it.
21314cf11afSPaul Mackerras 	 * The exec code can write slightly over 640kB to the stack
21414cf11afSPaul Mackerras 	 * before setting the user r1.  Thus we allow the stack to
21514cf11afSPaul Mackerras 	 * expand to 1MB without further checks.
21614cf11afSPaul Mackerras 	 */
21714cf11afSPaul Mackerras 	if (address + 0x100000 < vma->vm_end) {
21814cf11afSPaul Mackerras 		/* get user regs even if this fault is in kernel mode */
21914cf11afSPaul Mackerras 		struct pt_regs *uregs = current->thread.regs;
22014cf11afSPaul Mackerras 		if (uregs == NULL)
22114cf11afSPaul Mackerras 			goto bad_area;
22214cf11afSPaul Mackerras 
22314cf11afSPaul Mackerras 		/*
22414cf11afSPaul Mackerras 		 * A user-mode access to an address a long way below
22514cf11afSPaul Mackerras 		 * the stack pointer is only valid if the instruction
22614cf11afSPaul Mackerras 		 * is one which would update the stack pointer to the
22714cf11afSPaul Mackerras 		 * address accessed if the instruction completed,
22814cf11afSPaul Mackerras 		 * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
22914cf11afSPaul Mackerras 		 * (or the byte, halfword, float or double forms).
23014cf11afSPaul Mackerras 		 *
23114cf11afSPaul Mackerras 		 * If we don't check this then any write to the area
23214cf11afSPaul Mackerras 		 * between the last mapped region and the stack will
23314cf11afSPaul Mackerras 		 * expand the stack rather than segfaulting.
23414cf11afSPaul Mackerras 		 */
23514cf11afSPaul Mackerras 		if (address + 2048 < uregs->gpr[1]
23614cf11afSPaul Mackerras 		    && (!user_mode(regs) || !store_updates_sp(regs)))
23714cf11afSPaul Mackerras 			goto bad_area;
23814cf11afSPaul Mackerras 	}
23914cf11afSPaul Mackerras 	if (expand_stack(vma, address))
24014cf11afSPaul Mackerras 		goto bad_area;
24114cf11afSPaul Mackerras 
24214cf11afSPaul Mackerras good_area:
24314cf11afSPaul Mackerras 	code = SEGV_ACCERR;
24414cf11afSPaul Mackerras #if defined(CONFIG_6xx)
24514cf11afSPaul Mackerras 	if (error_code & 0x95700000)
24614cf11afSPaul Mackerras 		/* an error such as lwarx to I/O controller space,
24714cf11afSPaul Mackerras 		   address matching DABR, eciwx, etc. */
24814cf11afSPaul Mackerras 		goto bad_area;
24914cf11afSPaul Mackerras #endif /* CONFIG_6xx */
25014cf11afSPaul Mackerras #if defined(CONFIG_8xx)
2515efab4a0SJoakim Tjernlund 	/* 8xx sometimes need to load a invalid/non-present TLBs.
2525efab4a0SJoakim Tjernlund 	 * These must be invalidated separately as linux mm don't.
2535efab4a0SJoakim Tjernlund 	 */
2545efab4a0SJoakim Tjernlund 	if (error_code & 0x40000000) /* no translation? */
2555efab4a0SJoakim Tjernlund 		_tlbil_va(address, 0, 0, 0);
2565efab4a0SJoakim Tjernlund 
25714cf11afSPaul Mackerras         /* The MPC8xx seems to always set 0x80000000, which is
25814cf11afSPaul Mackerras          * "undefined".  Of those that can be set, this is the only
25914cf11afSPaul Mackerras          * one which seems bad.
26014cf11afSPaul Mackerras          */
26114cf11afSPaul Mackerras 	if (error_code & 0x10000000)
26214cf11afSPaul Mackerras                 /* Guarded storage error. */
26314cf11afSPaul Mackerras 		goto bad_area;
26414cf11afSPaul Mackerras #endif /* CONFIG_8xx */
26514cf11afSPaul Mackerras 
26614cf11afSPaul Mackerras 	if (is_exec) {
2678d30c14cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_STD_MMU
2688d30c14cSBenjamin Herrenschmidt 		/* Protection fault on exec go straight to failure on
2698d30c14cSBenjamin Herrenschmidt 		 * Hash based MMUs as they either don't support per-page
2708d30c14cSBenjamin Herrenschmidt 		 * execute permission, or if they do, it's handled already
2718d30c14cSBenjamin Herrenschmidt 		 * at the hash level. This test would probably have to
2728d30c14cSBenjamin Herrenschmidt 		 * be removed if we change the way this works to make hash
2738d30c14cSBenjamin Herrenschmidt 		 * processors use the same I/D cache coherency mechanism
2748d30c14cSBenjamin Herrenschmidt 		 * as embedded.
2758d30c14cSBenjamin Herrenschmidt 		 */
27614cf11afSPaul Mackerras 		if (error_code & DSISR_PROTFAULT)
27714cf11afSPaul Mackerras 			goto bad_area;
2788d30c14cSBenjamin Herrenschmidt #endif /* CONFIG_PPC_STD_MMU */
2798d30c14cSBenjamin Herrenschmidt 
28008ae6cc1SPaul Mackerras 		/*
28108ae6cc1SPaul Mackerras 		 * Allow execution from readable areas if the MMU does not
28208ae6cc1SPaul Mackerras 		 * provide separate controls over reading and executing.
2838d30c14cSBenjamin Herrenschmidt 		 *
2848d30c14cSBenjamin Herrenschmidt 		 * Note: That code used to not be enabled for 4xx/BookE.
2858d30c14cSBenjamin Herrenschmidt 		 * It is now as I/D cache coherency for these is done at
2868d30c14cSBenjamin Herrenschmidt 		 * set_pte_at() time and I see no reason why the test
2878d30c14cSBenjamin Herrenschmidt 		 * below wouldn't be valid on those processors. This -may-
2888d30c14cSBenjamin Herrenschmidt 		 * break programs compiled with a really old ABI though.
28908ae6cc1SPaul Mackerras 		 */
29008ae6cc1SPaul Mackerras 		if (!(vma->vm_flags & VM_EXEC) &&
29108ae6cc1SPaul Mackerras 		    (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
29208ae6cc1SPaul Mackerras 		     !(vma->vm_flags & (VM_READ | VM_WRITE))))
29314cf11afSPaul Mackerras 			goto bad_area;
29414cf11afSPaul Mackerras 	/* a write */
29514cf11afSPaul Mackerras 	} else if (is_write) {
29614cf11afSPaul Mackerras 		if (!(vma->vm_flags & VM_WRITE))
29714cf11afSPaul Mackerras 			goto bad_area;
29814cf11afSPaul Mackerras 	/* a read */
29914cf11afSPaul Mackerras 	} else {
30014cf11afSPaul Mackerras 		/* protection fault */
30114cf11afSPaul Mackerras 		if (error_code & 0x08000000)
30214cf11afSPaul Mackerras 			goto bad_area;
303df67b3daSJason Baron 		if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
30414cf11afSPaul Mackerras 			goto bad_area;
30514cf11afSPaul Mackerras 	}
30614cf11afSPaul Mackerras 
30714cf11afSPaul Mackerras 	/*
30814cf11afSPaul Mackerras 	 * If for any reason at all we couldn't handle the fault,
30914cf11afSPaul Mackerras 	 * make sure we exit gracefully rather than endlessly redo
31014cf11afSPaul Mackerras 	 * the fault.
31114cf11afSPaul Mackerras 	 */
312d06063ccSLinus Torvalds 	ret = handle_mm_fault(mm, vma, address, is_write ? FAULT_FLAG_WRITE : 0);
31383c54070SNick Piggin 	if (unlikely(ret & VM_FAULT_ERROR)) {
31483c54070SNick Piggin 		if (ret & VM_FAULT_OOM)
31514cf11afSPaul Mackerras 			goto out_of_memory;
31683c54070SNick Piggin 		else if (ret & VM_FAULT_SIGBUS)
31783c54070SNick Piggin 			goto do_sigbus;
31814cf11afSPaul Mackerras 		BUG();
31914cf11afSPaul Mackerras 	}
32040900194SBrian King 	if (ret & VM_FAULT_MAJOR) {
32183c54070SNick Piggin 		current->maj_flt++;
322*a8b0ca17SPeter Zijlstra 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
32378f13e95SPeter Zijlstra 				     regs, address);
32440900194SBrian King #ifdef CONFIG_PPC_SMLPAR
32540900194SBrian King 		if (firmware_has_feature(FW_FEATURE_CMO)) {
32640900194SBrian King 			preempt_disable();
327a6326e98SRobert Jennings 			get_lppaca()->page_ins += (1 << PAGE_FACTOR);
32840900194SBrian King 			preempt_enable();
32940900194SBrian King 		}
33040900194SBrian King #endif
331ac17dc8eSPeter Zijlstra 	} else {
33283c54070SNick Piggin 		current->min_flt++;
333*a8b0ca17SPeter Zijlstra 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
33478f13e95SPeter Zijlstra 				     regs, address);
335ac17dc8eSPeter Zijlstra 	}
33614cf11afSPaul Mackerras 	up_read(&mm->mmap_sem);
33714cf11afSPaul Mackerras 	return 0;
33814cf11afSPaul Mackerras 
33914cf11afSPaul Mackerras bad_area:
34014cf11afSPaul Mackerras 	up_read(&mm->mmap_sem);
34114cf11afSPaul Mackerras 
34214cf11afSPaul Mackerras bad_area_nosemaphore:
34314cf11afSPaul Mackerras 	/* User mode accesses cause a SIGSEGV */
34414cf11afSPaul Mackerras 	if (user_mode(regs)) {
34514cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, code, address);
34614cf11afSPaul Mackerras 		return 0;
34714cf11afSPaul Mackerras 	}
34814cf11afSPaul Mackerras 
34914cf11afSPaul Mackerras 	if (is_exec && (error_code & DSISR_PROTFAULT)
35014cf11afSPaul Mackerras 	    && printk_ratelimit())
35114cf11afSPaul Mackerras 		printk(KERN_CRIT "kernel tried to execute NX-protected"
35214cf11afSPaul Mackerras 		       " page (%lx) - exploit attempt? (uid: %d)\n",
3531330deb0SDavid Howells 		       address, current_uid());
35414cf11afSPaul Mackerras 
35514cf11afSPaul Mackerras 	return SIGSEGV;
35614cf11afSPaul Mackerras 
35714cf11afSPaul Mackerras /*
35814cf11afSPaul Mackerras  * We ran out of memory, or some other thing happened to us that made
35914cf11afSPaul Mackerras  * us unable to handle the page fault gracefully.
36014cf11afSPaul Mackerras  */
36114cf11afSPaul Mackerras out_of_memory:
36214cf11afSPaul Mackerras 	up_read(&mm->mmap_sem);
363e460c2c9SBenjamin Herrenschmidt 	if (!user_mode(regs))
36414cf11afSPaul Mackerras 		return SIGKILL;
365e460c2c9SBenjamin Herrenschmidt 	pagefault_out_of_memory();
366e460c2c9SBenjamin Herrenschmidt 	return 0;
36714cf11afSPaul Mackerras 
36814cf11afSPaul Mackerras do_sigbus:
36914cf11afSPaul Mackerras 	up_read(&mm->mmap_sem);
37014cf11afSPaul Mackerras 	if (user_mode(regs)) {
37114cf11afSPaul Mackerras 		info.si_signo = SIGBUS;
37214cf11afSPaul Mackerras 		info.si_errno = 0;
37314cf11afSPaul Mackerras 		info.si_code = BUS_ADRERR;
37414cf11afSPaul Mackerras 		info.si_addr = (void __user *)address;
37514cf11afSPaul Mackerras 		force_sig_info(SIGBUS, &info, current);
37614cf11afSPaul Mackerras 		return 0;
37714cf11afSPaul Mackerras 	}
37814cf11afSPaul Mackerras 	return SIGBUS;
37914cf11afSPaul Mackerras }
38014cf11afSPaul Mackerras 
38114cf11afSPaul Mackerras /*
38214cf11afSPaul Mackerras  * bad_page_fault is called when we have a bad access from the kernel.
38314cf11afSPaul Mackerras  * It is called from the DSI and ISI handlers in head.S and from some
38414cf11afSPaul Mackerras  * of the procedures in traps.c.
38514cf11afSPaul Mackerras  */
38614cf11afSPaul Mackerras void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
38714cf11afSPaul Mackerras {
38814cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
38928b54990SAnton Blanchard 	unsigned long *stackend;
39014cf11afSPaul Mackerras 
39114cf11afSPaul Mackerras 	/* Are we prepared to handle this fault?  */
39214cf11afSPaul Mackerras 	if ((entry = search_exception_tables(regs->nip)) != NULL) {
39314cf11afSPaul Mackerras 		regs->nip = entry->fixup;
39414cf11afSPaul Mackerras 		return;
39514cf11afSPaul Mackerras 	}
39614cf11afSPaul Mackerras 
39714cf11afSPaul Mackerras 	/* kernel has accessed a bad area */
398723925b7SOlof Johansson 
399723925b7SOlof Johansson 	switch (regs->trap) {
400723925b7SOlof Johansson 	case 0x300:
401723925b7SOlof Johansson 	case 0x380:
402a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
403a416dd8dSMichael Ellerman 			"data at address 0x%08lx\n", regs->dar);
404723925b7SOlof Johansson 		break;
405723925b7SOlof Johansson 	case 0x400:
406723925b7SOlof Johansson 	case 0x480:
407a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
408a416dd8dSMichael Ellerman 			"instruction fetch\n");
409723925b7SOlof Johansson 		break;
410723925b7SOlof Johansson 	default:
411a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
412a416dd8dSMichael Ellerman 			"unknown fault\n");
413a416dd8dSMichael Ellerman 		break;
414723925b7SOlof Johansson 	}
415723925b7SOlof Johansson 	printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
416723925b7SOlof Johansson 		regs->nip);
417723925b7SOlof Johansson 
41828b54990SAnton Blanchard 	stackend = end_of_stack(current);
41928b54990SAnton Blanchard 	if (current != &init_task && *stackend != STACK_END_MAGIC)
42028b54990SAnton Blanchard 		printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
42128b54990SAnton Blanchard 
42214cf11afSPaul Mackerras 	die("Kernel access of bad area", regs, sig);
42314cf11afSPaul Mackerras }
424