xref: /openbmc/linux/arch/powerpc/mm/fault.c (revision c9a0dad162014182867f81b28bb7a4b691d65595)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
214cf11afSPaul Mackerras /*
314cf11afSPaul Mackerras  *  PowerPC version
414cf11afSPaul Mackerras  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
514cf11afSPaul Mackerras  *
614cf11afSPaul Mackerras  *  Derived from "arch/i386/mm/fault.c"
714cf11afSPaul Mackerras  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
814cf11afSPaul Mackerras  *
914cf11afSPaul Mackerras  *  Modified by Cort Dougan and Paul Mackerras.
1014cf11afSPaul Mackerras  *
1114cf11afSPaul Mackerras  *  Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
1214cf11afSPaul Mackerras  */
1314cf11afSPaul Mackerras 
1414cf11afSPaul Mackerras #include <linux/signal.h>
1514cf11afSPaul Mackerras #include <linux/sched.h>
1668db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
1714cf11afSPaul Mackerras #include <linux/kernel.h>
1814cf11afSPaul Mackerras #include <linux/errno.h>
1914cf11afSPaul Mackerras #include <linux/string.h>
2014cf11afSPaul Mackerras #include <linux/types.h>
210e36b0d1SChristophe Leroy #include <linux/pagemap.h>
2214cf11afSPaul Mackerras #include <linux/ptrace.h>
2314cf11afSPaul Mackerras #include <linux/mman.h>
2414cf11afSPaul Mackerras #include <linux/mm.h>
2514cf11afSPaul Mackerras #include <linux/interrupt.h>
2614cf11afSPaul Mackerras #include <linux/highmem.h>
278a39b05fSPaul Gortmaker #include <linux/extable.h>
2814cf11afSPaul Mackerras #include <linux/kprobes.h>
291eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
30cdd6c482SIngo Molnar #include <linux/perf_event.h>
3176462232SChristian Dietrich #include <linux/ratelimit.h>
32ba12eedeSLi Zhong #include <linux/context_tracking.h>
339d57472fSAnton Blanchard #include <linux/hugetlb.h>
3470ffdb93SDavid Hildenbrand #include <linux/uaccess.h>
3514cf11afSPaul Mackerras 
3640900194SBrian King #include <asm/firmware.h>
3714cf11afSPaul Mackerras #include <asm/page.h>
3814cf11afSPaul Mackerras #include <asm/pgtable.h>
3914cf11afSPaul Mackerras #include <asm/mmu.h>
4014cf11afSPaul Mackerras #include <asm/mmu_context.h>
4114cf11afSPaul Mackerras #include <asm/siginfo.h>
42ae3a197eSDavid Howells #include <asm/debug.h>
435e5be3aeSMichael Ellerman #include <asm/kup.h>
449f90b997SChristoph Hellwig 
4514cf11afSPaul Mackerras /*
460e36b0d1SChristophe Leroy  * Check whether the instruction inst is a store using
4714cf11afSPaul Mackerras  * an update addressing form which will update r1.
4814cf11afSPaul Mackerras  */
490e36b0d1SChristophe Leroy static bool store_updates_sp(unsigned int inst)
5014cf11afSPaul Mackerras {
5114cf11afSPaul Mackerras 	/* check for 1 in the rA field */
5214cf11afSPaul Mackerras 	if (((inst >> 16) & 0x1f) != 1)
538f5ca0b3SBenjamin Herrenschmidt 		return false;
5414cf11afSPaul Mackerras 	/* check major opcode */
5514cf11afSPaul Mackerras 	switch (inst >> 26) {
568a0b1120SChristophe Leroy 	case OP_STWU:
578a0b1120SChristophe Leroy 	case OP_STBU:
588a0b1120SChristophe Leroy 	case OP_STHU:
598a0b1120SChristophe Leroy 	case OP_STFSU:
608a0b1120SChristophe Leroy 	case OP_STFDU:
618f5ca0b3SBenjamin Herrenschmidt 		return true;
628a0b1120SChristophe Leroy 	case OP_STD:	/* std or stdu */
6314cf11afSPaul Mackerras 		return (inst & 3) == 1;
648a0b1120SChristophe Leroy 	case OP_31:
6514cf11afSPaul Mackerras 		/* check minor opcode */
6614cf11afSPaul Mackerras 		switch ((inst >> 1) & 0x3ff) {
678a0b1120SChristophe Leroy 		case OP_31_XOP_STDUX:
688a0b1120SChristophe Leroy 		case OP_31_XOP_STWUX:
698a0b1120SChristophe Leroy 		case OP_31_XOP_STBUX:
708a0b1120SChristophe Leroy 		case OP_31_XOP_STHUX:
718a0b1120SChristophe Leroy 		case OP_31_XOP_STFSUX:
728a0b1120SChristophe Leroy 		case OP_31_XOP_STFDUX:
738f5ca0b3SBenjamin Herrenschmidt 			return true;
7414cf11afSPaul Mackerras 		}
7514cf11afSPaul Mackerras 	}
768f5ca0b3SBenjamin Herrenschmidt 	return false;
7714cf11afSPaul Mackerras }
789be72573SBenjamin Herrenschmidt /*
799be72573SBenjamin Herrenschmidt  * do_page_fault error handling helpers
809be72573SBenjamin Herrenschmidt  */
819be72573SBenjamin Herrenschmidt 
82c3350602SBenjamin Herrenschmidt static int
83cd60ab7aSEric W. Biederman __bad_area_nosemaphore(struct pt_regs *regs, unsigned long address, int si_code)
84c3350602SBenjamin Herrenschmidt {
85c3350602SBenjamin Herrenschmidt 	/*
86c3350602SBenjamin Herrenschmidt 	 * If we are in kernel mode, bail out with a SEGV, this will
87c3350602SBenjamin Herrenschmidt 	 * be caught by the assembly which will restore the non-volatile
88c3350602SBenjamin Herrenschmidt 	 * registers before calling bad_page_fault()
89c3350602SBenjamin Herrenschmidt 	 */
90c3350602SBenjamin Herrenschmidt 	if (!user_mode(regs))
91c3350602SBenjamin Herrenschmidt 		return SIGSEGV;
92c3350602SBenjamin Herrenschmidt 
93cd60ab7aSEric W. Biederman 	_exception(SIGSEGV, regs, si_code, address);
94c3350602SBenjamin Herrenschmidt 
95c3350602SBenjamin Herrenschmidt 	return 0;
96c3350602SBenjamin Herrenschmidt }
97c3350602SBenjamin Herrenschmidt 
98c3350602SBenjamin Herrenschmidt static noinline int bad_area_nosemaphore(struct pt_regs *regs, unsigned long address)
99c3350602SBenjamin Herrenschmidt {
100cd60ab7aSEric W. Biederman 	return __bad_area_nosemaphore(regs, address, SEGV_MAPERR);
101c3350602SBenjamin Herrenschmidt }
102c3350602SBenjamin Herrenschmidt 
1039f2ee693SEric W. Biederman static int __bad_area(struct pt_regs *regs, unsigned long address, int si_code)
104c3350602SBenjamin Herrenschmidt {
105c3350602SBenjamin Herrenschmidt 	struct mm_struct *mm = current->mm;
106c3350602SBenjamin Herrenschmidt 
107c3350602SBenjamin Herrenschmidt 	/*
108c3350602SBenjamin Herrenschmidt 	 * Something tried to access memory that isn't in our memory map..
109c3350602SBenjamin Herrenschmidt 	 * Fix it, but check if it's kernel or user first..
110c3350602SBenjamin Herrenschmidt 	 */
111c3350602SBenjamin Herrenschmidt 	up_read(&mm->mmap_sem);
112c3350602SBenjamin Herrenschmidt 
113cd60ab7aSEric W. Biederman 	return __bad_area_nosemaphore(regs, address, si_code);
114c3350602SBenjamin Herrenschmidt }
115c3350602SBenjamin Herrenschmidt 
116c3350602SBenjamin Herrenschmidt static noinline int bad_area(struct pt_regs *regs, unsigned long address)
117c3350602SBenjamin Herrenschmidt {
1189f2ee693SEric W. Biederman 	return __bad_area(regs, address, SEGV_MAPERR);
11999cd1302SRam Pai }
12099cd1302SRam Pai 
12199cd1302SRam Pai static int bad_key_fault_exception(struct pt_regs *regs, unsigned long address,
12299cd1302SRam Pai 				    int pkey)
12399cd1302SRam Pai {
1248eb2ba25SEric W. Biederman 	/*
1258eb2ba25SEric W. Biederman 	 * If we are in kernel mode, bail out with a SEGV, this will
1268eb2ba25SEric W. Biederman 	 * be caught by the assembly which will restore the non-volatile
1278eb2ba25SEric W. Biederman 	 * registers before calling bad_page_fault()
1288eb2ba25SEric W. Biederman 	 */
1298eb2ba25SEric W. Biederman 	if (!user_mode(regs))
1308eb2ba25SEric W. Biederman 		return SIGSEGV;
1318eb2ba25SEric W. Biederman 
1325d8fb8a5SEric W. Biederman 	_exception_pkey(regs, address, pkey);
1338eb2ba25SEric W. Biederman 
1348eb2ba25SEric W. Biederman 	return 0;
135c3350602SBenjamin Herrenschmidt }
136c3350602SBenjamin Herrenschmidt 
137ecb101aeSJohn Sperbeck static noinline int bad_access(struct pt_regs *regs, unsigned long address)
138ecb101aeSJohn Sperbeck {
1399f2ee693SEric W. Biederman 	return __bad_area(regs, address, SEGV_ACCERR);
140ecb101aeSJohn Sperbeck }
141ecb101aeSJohn Sperbeck 
1423913fdd7SAnton Blanchard static int do_sigbus(struct pt_regs *regs, unsigned long address,
14350a7ca3cSSouptick Joarder 		     vm_fault_t fault)
1449be72573SBenjamin Herrenschmidt {
14563af5262SAnton Blanchard 	if (!user_mode(regs))
146b5c8f0fdSBenjamin Herrenschmidt 		return SIGBUS;
14763af5262SAnton Blanchard 
14841ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = BUS_ADRERR;
1493913fdd7SAnton Blanchard #ifdef CONFIG_MEMORY_FAILURE
1503913fdd7SAnton Blanchard 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
151f654fc07SEric W. Biederman 		unsigned int lsb = 0; /* shutup gcc */
152f654fc07SEric W. Biederman 
1533913fdd7SAnton Blanchard 		pr_err("MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
1543913fdd7SAnton Blanchard 			current->comm, current->pid, address);
1559d57472fSAnton Blanchard 
1569d57472fSAnton Blanchard 		if (fault & VM_FAULT_HWPOISON_LARGE)
1579d57472fSAnton Blanchard 			lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
1589d57472fSAnton Blanchard 		if (fault & VM_FAULT_HWPOISON)
1599d57472fSAnton Blanchard 			lsb = PAGE_SHIFT;
160f654fc07SEric W. Biederman 
161f8eac901SEric W. Biederman 		force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb);
162f654fc07SEric W. Biederman 		return 0;
163f654fc07SEric W. Biederman 	}
164f654fc07SEric W. Biederman 
1653913fdd7SAnton Blanchard #endif
1662e1661d2SEric W. Biederman 	force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
167b5c8f0fdSBenjamin Herrenschmidt 	return 0;
1689be72573SBenjamin Herrenschmidt }
1699be72573SBenjamin Herrenschmidt 
17050a7ca3cSSouptick Joarder static int mm_fault_error(struct pt_regs *regs, unsigned long addr,
17150a7ca3cSSouptick Joarder 				vm_fault_t fault)
1729be72573SBenjamin Herrenschmidt {
1739be72573SBenjamin Herrenschmidt 	/*
174b5c8f0fdSBenjamin Herrenschmidt 	 * Kernel page fault interrupted by SIGKILL. We have no reason to
175b5c8f0fdSBenjamin Herrenschmidt 	 * continue processing.
1769be72573SBenjamin Herrenschmidt 	 */
177b5c8f0fdSBenjamin Herrenschmidt 	if (fatal_signal_pending(current) && !user_mode(regs))
178b5c8f0fdSBenjamin Herrenschmidt 		return SIGKILL;
1799be72573SBenjamin Herrenschmidt 
1809be72573SBenjamin Herrenschmidt 	/* Out of memory */
181c2d23f91SDavid Rientjes 	if (fault & VM_FAULT_OOM) {
182c2d23f91SDavid Rientjes 		/*
183c2d23f91SDavid Rientjes 		 * We ran out of memory, or some other thing happened to us that
184c2d23f91SDavid Rientjes 		 * made us unable to handle the page fault gracefully.
185c2d23f91SDavid Rientjes 		 */
186c2d23f91SDavid Rientjes 		if (!user_mode(regs))
187b5c8f0fdSBenjamin Herrenschmidt 			return SIGSEGV;
188c2d23f91SDavid Rientjes 		pagefault_out_of_memory();
189b5c8f0fdSBenjamin Herrenschmidt 	} else {
190b5c8f0fdSBenjamin Herrenschmidt 		if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
191b5c8f0fdSBenjamin Herrenschmidt 			     VM_FAULT_HWPOISON_LARGE))
1923913fdd7SAnton Blanchard 			return do_sigbus(regs, addr, fault);
193b5c8f0fdSBenjamin Herrenschmidt 		else if (fault & VM_FAULT_SIGSEGV)
194b5c8f0fdSBenjamin Herrenschmidt 			return bad_area_nosemaphore(regs, addr);
195b5c8f0fdSBenjamin Herrenschmidt 		else
1969be72573SBenjamin Herrenschmidt 			BUG();
197b5c8f0fdSBenjamin Herrenschmidt 	}
198b5c8f0fdSBenjamin Herrenschmidt 	return 0;
1999be72573SBenjamin Herrenschmidt }
20014cf11afSPaul Mackerras 
201d3ca5874SBenjamin Herrenschmidt /* Is this a bad kernel fault ? */
202de78a9c4SChristophe Leroy static bool bad_kernel_fault(struct pt_regs *regs, unsigned long error_code,
2035e5be3aeSMichael Ellerman 			     unsigned long address, bool is_write)
204d3ca5874SBenjamin Herrenschmidt {
205de78a9c4SChristophe Leroy 	int is_exec = TRAP(regs) == 0x400;
206de78a9c4SChristophe Leroy 
207ffca395bSChristophe Leroy 	/* NX faults set DSISR_PROTFAULT on the 8xx, DSISR_NOEXEC_OR_G on others */
208ffca395bSChristophe Leroy 	if (is_exec && (error_code & (DSISR_NOEXEC_OR_G | DSISR_KEYFAULT |
209ffca395bSChristophe Leroy 				      DSISR_PROTFAULT))) {
2100fb1c25aSChristophe Leroy 		pr_crit_ratelimited("kernel tried to execute %s page (%lx) - exploit attempt? (uid: %d)\n",
2110fb1c25aSChristophe Leroy 				    address >= TASK_SIZE ? "exec-protected" : "user",
2120fb1c25aSChristophe Leroy 				    address,
2130fb1c25aSChristophe Leroy 				    from_kuid(&init_user_ns, current_uid()));
2145e5be3aeSMichael Ellerman 
2155e5be3aeSMichael Ellerman 		// Kernel exec fault is always bad
2165e5be3aeSMichael Ellerman 		return true;
217d3ca5874SBenjamin Herrenschmidt 	}
218de78a9c4SChristophe Leroy 
219de78a9c4SChristophe Leroy 	if (!is_exec && address < TASK_SIZE && (error_code & DSISR_PROTFAULT) &&
220de78a9c4SChristophe Leroy 	    !search_exception_tables(regs->nip)) {
221de78a9c4SChristophe Leroy 		pr_crit_ratelimited("Kernel attempted to access user page (%lx) - exploit attempt? (uid: %d)\n",
222de78a9c4SChristophe Leroy 				    address,
223de78a9c4SChristophe Leroy 				    from_kuid(&init_user_ns, current_uid()));
224de78a9c4SChristophe Leroy 	}
225de78a9c4SChristophe Leroy 
2265e5be3aeSMichael Ellerman 	// Kernel fault on kernel address is bad
2275e5be3aeSMichael Ellerman 	if (address >= TASK_SIZE)
2285e5be3aeSMichael Ellerman 		return true;
2295e5be3aeSMichael Ellerman 
2305e5be3aeSMichael Ellerman 	// Fault on user outside of certain regions (eg. copy_tofrom_user()) is bad
2315e5be3aeSMichael Ellerman 	if (!search_exception_tables(regs->nip))
2325e5be3aeSMichael Ellerman 		return true;
2335e5be3aeSMichael Ellerman 
2345e5be3aeSMichael Ellerman 	// Read/write fault in a valid region (the exception table search passed
2355e5be3aeSMichael Ellerman 	// above), but blocked by KUAP is bad, it can never succeed.
2366ec20aa2SChristophe Leroy 	if (bad_kuap_fault(regs, address, is_write))
2375e5be3aeSMichael Ellerman 		return true;
2385e5be3aeSMichael Ellerman 
2395e5be3aeSMichael Ellerman 	// What's left? Kernel fault on user in well defined regions (extable
2405e5be3aeSMichael Ellerman 	// matched), and allowed by KUAP in the faulting context.
2415e5be3aeSMichael Ellerman 	return false;
242d3ca5874SBenjamin Herrenschmidt }
243d3ca5874SBenjamin Herrenschmidt 
2448f5ca0b3SBenjamin Herrenschmidt static bool bad_stack_expansion(struct pt_regs *regs, unsigned long address,
2450e36b0d1SChristophe Leroy 				struct vm_area_struct *vma, unsigned int flags,
2460e36b0d1SChristophe Leroy 				bool *must_retry)
2478f5ca0b3SBenjamin Herrenschmidt {
2488f5ca0b3SBenjamin Herrenschmidt 	/*
2498f5ca0b3SBenjamin Herrenschmidt 	 * N.B. The POWER/Open ABI allows programs to access up to
2508f5ca0b3SBenjamin Herrenschmidt 	 * 288 bytes below the stack pointer.
2518f5ca0b3SBenjamin Herrenschmidt 	 * The kernel signal delivery code writes up to about 1.5kB
2528f5ca0b3SBenjamin Herrenschmidt 	 * below the stack pointer (r1) before decrementing it.
2538f5ca0b3SBenjamin Herrenschmidt 	 * The exec code can write slightly over 640kB to the stack
2548f5ca0b3SBenjamin Herrenschmidt 	 * before setting the user r1.  Thus we allow the stack to
2558f5ca0b3SBenjamin Herrenschmidt 	 * expand to 1MB without further checks.
2568f5ca0b3SBenjamin Herrenschmidt 	 */
2578f5ca0b3SBenjamin Herrenschmidt 	if (address + 0x100000 < vma->vm_end) {
2580e36b0d1SChristophe Leroy 		unsigned int __user *nip = (unsigned int __user *)regs->nip;
2598f5ca0b3SBenjamin Herrenschmidt 		/* get user regs even if this fault is in kernel mode */
2608f5ca0b3SBenjamin Herrenschmidt 		struct pt_regs *uregs = current->thread.regs;
2618f5ca0b3SBenjamin Herrenschmidt 		if (uregs == NULL)
2628f5ca0b3SBenjamin Herrenschmidt 			return true;
2638f5ca0b3SBenjamin Herrenschmidt 
2648f5ca0b3SBenjamin Herrenschmidt 		/*
2658f5ca0b3SBenjamin Herrenschmidt 		 * A user-mode access to an address a long way below
2668f5ca0b3SBenjamin Herrenschmidt 		 * the stack pointer is only valid if the instruction
2678f5ca0b3SBenjamin Herrenschmidt 		 * is one which would update the stack pointer to the
2688f5ca0b3SBenjamin Herrenschmidt 		 * address accessed if the instruction completed,
2698f5ca0b3SBenjamin Herrenschmidt 		 * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
2708f5ca0b3SBenjamin Herrenschmidt 		 * (or the byte, halfword, float or double forms).
2718f5ca0b3SBenjamin Herrenschmidt 		 *
2728f5ca0b3SBenjamin Herrenschmidt 		 * If we don't check this then any write to the area
2738f5ca0b3SBenjamin Herrenschmidt 		 * between the last mapped region and the stack will
2748f5ca0b3SBenjamin Herrenschmidt 		 * expand the stack rather than segfaulting.
2758f5ca0b3SBenjamin Herrenschmidt 		 */
2760e36b0d1SChristophe Leroy 		if (address + 2048 >= uregs->gpr[1])
2770e36b0d1SChristophe Leroy 			return false;
2780e36b0d1SChristophe Leroy 
2790e36b0d1SChristophe Leroy 		if ((flags & FAULT_FLAG_WRITE) && (flags & FAULT_FLAG_USER) &&
28096d4f267SLinus Torvalds 		    access_ok(nip, sizeof(*nip))) {
2810e36b0d1SChristophe Leroy 			unsigned int inst;
2820e36b0d1SChristophe Leroy 
283def0bfdbSChristophe Leroy 			if (!probe_user_read(&inst, nip, sizeof(inst)))
2840e36b0d1SChristophe Leroy 				return !store_updates_sp(inst);
2850e36b0d1SChristophe Leroy 			*must_retry = true;
2860e36b0d1SChristophe Leroy 		}
2878f5ca0b3SBenjamin Herrenschmidt 		return true;
2888f5ca0b3SBenjamin Herrenschmidt 	}
2898f5ca0b3SBenjamin Herrenschmidt 	return false;
2908f5ca0b3SBenjamin Herrenschmidt }
2918f5ca0b3SBenjamin Herrenschmidt 
292bd0d63f8SBenjamin Herrenschmidt static bool access_error(bool is_write, bool is_exec,
293bd0d63f8SBenjamin Herrenschmidt 			 struct vm_area_struct *vma)
294bd0d63f8SBenjamin Herrenschmidt {
295bd0d63f8SBenjamin Herrenschmidt 	/*
296bd0d63f8SBenjamin Herrenschmidt 	 * Allow execution from readable areas if the MMU does not
297bd0d63f8SBenjamin Herrenschmidt 	 * provide separate controls over reading and executing.
298bd0d63f8SBenjamin Herrenschmidt 	 *
299bd0d63f8SBenjamin Herrenschmidt 	 * Note: That code used to not be enabled for 4xx/BookE.
300bd0d63f8SBenjamin Herrenschmidt 	 * It is now as I/D cache coherency for these is done at
301bd0d63f8SBenjamin Herrenschmidt 	 * set_pte_at() time and I see no reason why the test
302bd0d63f8SBenjamin Herrenschmidt 	 * below wouldn't be valid on those processors. This -may-
303bd0d63f8SBenjamin Herrenschmidt 	 * break programs compiled with a really old ABI though.
304bd0d63f8SBenjamin Herrenschmidt 	 */
305bd0d63f8SBenjamin Herrenschmidt 	if (is_exec) {
306bd0d63f8SBenjamin Herrenschmidt 		return !(vma->vm_flags & VM_EXEC) &&
307bd0d63f8SBenjamin Herrenschmidt 			(cpu_has_feature(CPU_FTR_NOEXECUTE) ||
308bd0d63f8SBenjamin Herrenschmidt 			 !(vma->vm_flags & (VM_READ | VM_WRITE)));
309bd0d63f8SBenjamin Herrenschmidt 	}
310bd0d63f8SBenjamin Herrenschmidt 
311bd0d63f8SBenjamin Herrenschmidt 	if (is_write) {
312bd0d63f8SBenjamin Herrenschmidt 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
313bd0d63f8SBenjamin Herrenschmidt 			return true;
314bd0d63f8SBenjamin Herrenschmidt 		return false;
315bd0d63f8SBenjamin Herrenschmidt 	}
316bd0d63f8SBenjamin Herrenschmidt 
317bd0d63f8SBenjamin Herrenschmidt 	if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
318bd0d63f8SBenjamin Herrenschmidt 		return true;
319f2ed480fSAneesh Kumar K.V 	/*
320f2ed480fSAneesh Kumar K.V 	 * We should ideally do the vma pkey access check here. But in the
321f2ed480fSAneesh Kumar K.V 	 * fault path, handle_mm_fault() also does the same check. To avoid
322f2ed480fSAneesh Kumar K.V 	 * these multiple checks, we skip it here and handle access error due
323f2ed480fSAneesh Kumar K.V 	 * to pkeys later.
324f2ed480fSAneesh Kumar K.V 	 */
325bd0d63f8SBenjamin Herrenschmidt 	return false;
326bd0d63f8SBenjamin Herrenschmidt }
327bd0d63f8SBenjamin Herrenschmidt 
3283da02648SBenjamin Herrenschmidt #ifdef CONFIG_PPC_SMLPAR
3293da02648SBenjamin Herrenschmidt static inline void cmo_account_page_fault(void)
3303da02648SBenjamin Herrenschmidt {
3313da02648SBenjamin Herrenschmidt 	if (firmware_has_feature(FW_FEATURE_CMO)) {
3323da02648SBenjamin Herrenschmidt 		u32 page_ins;
3333da02648SBenjamin Herrenschmidt 
3343da02648SBenjamin Herrenschmidt 		preempt_disable();
3353da02648SBenjamin Herrenschmidt 		page_ins = be32_to_cpu(get_lppaca()->page_ins);
3363da02648SBenjamin Herrenschmidt 		page_ins += 1 << PAGE_FACTOR;
3373da02648SBenjamin Herrenschmidt 		get_lppaca()->page_ins = cpu_to_be32(page_ins);
3383da02648SBenjamin Herrenschmidt 		preempt_enable();
3393da02648SBenjamin Herrenschmidt 	}
3403da02648SBenjamin Herrenschmidt }
3413da02648SBenjamin Herrenschmidt #else
3423da02648SBenjamin Herrenschmidt static inline void cmo_account_page_fault(void) { }
3433da02648SBenjamin Herrenschmidt #endif /* CONFIG_PPC_SMLPAR */
3443da02648SBenjamin Herrenschmidt 
3455b3e84fcSChristophe Leroy #ifdef CONFIG_PPC_BOOK3S
346374f3f59SAneesh Kumar K.V static void sanity_check_fault(bool is_write, bool is_user,
347374f3f59SAneesh Kumar K.V 			       unsigned long error_code, unsigned long address)
3482865d08dSBenjamin Herrenschmidt {
3492865d08dSBenjamin Herrenschmidt 	/*
350374f3f59SAneesh Kumar K.V 	 * Userspace trying to access kernel address, we get PROTFAULT for that.
351374f3f59SAneesh Kumar K.V 	 */
352374f3f59SAneesh Kumar K.V 	if (is_user && address >= TASK_SIZE) {
3530f9aee0cSChristophe Leroy 		if ((long)address == -1)
3540f9aee0cSChristophe Leroy 			return;
3550f9aee0cSChristophe Leroy 
356374f3f59SAneesh Kumar K.V 		pr_crit_ratelimited("%s[%d]: User access of kernel address (%lx) - exploit attempt? (uid: %d)\n",
357374f3f59SAneesh Kumar K.V 				   current->comm, current->pid, address,
358374f3f59SAneesh Kumar K.V 				   from_kuid(&init_user_ns, current_uid()));
359374f3f59SAneesh Kumar K.V 		return;
360374f3f59SAneesh Kumar K.V 	}
361374f3f59SAneesh Kumar K.V 
362374f3f59SAneesh Kumar K.V 	/*
3632865d08dSBenjamin Herrenschmidt 	 * For hash translation mode, we should never get a
3642865d08dSBenjamin Herrenschmidt 	 * PROTFAULT. Any update to pte to reduce access will result in us
3652865d08dSBenjamin Herrenschmidt 	 * removing the hash page table entry, thus resulting in a DSISR_NOHPTE
3662865d08dSBenjamin Herrenschmidt 	 * fault instead of DSISR_PROTFAULT.
3672865d08dSBenjamin Herrenschmidt 	 *
3682865d08dSBenjamin Herrenschmidt 	 * A pte update to relax the access will not result in a hash page table
3692865d08dSBenjamin Herrenschmidt 	 * entry invalidate and hence can result in DSISR_PROTFAULT.
3702865d08dSBenjamin Herrenschmidt 	 * ptep_set_access_flags() doesn't do a hpte flush. This is why we have
3712865d08dSBenjamin Herrenschmidt 	 * the special !is_write in the below conditional.
3722865d08dSBenjamin Herrenschmidt 	 *
3732865d08dSBenjamin Herrenschmidt 	 * For platforms that doesn't supports coherent icache and do support
3742865d08dSBenjamin Herrenschmidt 	 * per page noexec bit, we do setup things such that we do the
3752865d08dSBenjamin Herrenschmidt 	 * sync between D/I cache via fault. But that is handled via low level
3762865d08dSBenjamin Herrenschmidt 	 * hash fault code (hash_page_do_lazy_icache()) and we should not reach
3772865d08dSBenjamin Herrenschmidt 	 * here in such case.
3782865d08dSBenjamin Herrenschmidt 	 *
3792865d08dSBenjamin Herrenschmidt 	 * For wrong access that can result in PROTFAULT, the above vma->vm_flags
3802865d08dSBenjamin Herrenschmidt 	 * check should handle those and hence we should fall to the bad_area
3812865d08dSBenjamin Herrenschmidt 	 * handling correctly.
3822865d08dSBenjamin Herrenschmidt 	 *
3832865d08dSBenjamin Herrenschmidt 	 * For embedded with per page exec support that doesn't support coherent
3842865d08dSBenjamin Herrenschmidt 	 * icache we do get PROTFAULT and we handle that D/I cache sync in
3852865d08dSBenjamin Herrenschmidt 	 * set_pte_at while taking the noexec/prot fault. Hence this is WARN_ON
3862865d08dSBenjamin Herrenschmidt 	 * is conditional for server MMU.
3872865d08dSBenjamin Herrenschmidt 	 *
3882865d08dSBenjamin Herrenschmidt 	 * For radix, we can get prot fault for autonuma case, because radix
3892865d08dSBenjamin Herrenschmidt 	 * page table will have them marked noaccess for user.
3902865d08dSBenjamin Herrenschmidt 	 */
391374f3f59SAneesh Kumar K.V 	if (radix_enabled() || is_write)
392374f3f59SAneesh Kumar K.V 		return;
393374f3f59SAneesh Kumar K.V 
3942865d08dSBenjamin Herrenschmidt 	WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
3952865d08dSBenjamin Herrenschmidt }
3962865d08dSBenjamin Herrenschmidt #else
397374f3f59SAneesh Kumar K.V static void sanity_check_fault(bool is_write, bool is_user,
398374f3f59SAneesh Kumar K.V 			       unsigned long error_code, unsigned long address) { }
3995b3e84fcSChristophe Leroy #endif /* CONFIG_PPC_BOOK3S */
4002865d08dSBenjamin Herrenschmidt 
40114cf11afSPaul Mackerras /*
40241b464e5SBenjamin Herrenschmidt  * Define the correct "is_write" bit in error_code based
40341b464e5SBenjamin Herrenschmidt  * on the processor family
40441b464e5SBenjamin Herrenschmidt  */
40541b464e5SBenjamin Herrenschmidt #if (defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
40641b464e5SBenjamin Herrenschmidt #define page_fault_is_write(__err)	((__err) & ESR_DST)
407f3d96e69SBenjamin Herrenschmidt #define page_fault_is_bad(__err)	(0)
40841b464e5SBenjamin Herrenschmidt #else
40941b464e5SBenjamin Herrenschmidt #define page_fault_is_write(__err)	((__err) & DSISR_ISSTORE)
410968159c0SChristophe Leroy #if defined(CONFIG_PPC_8xx)
4114915349bSChristophe Leroy #define page_fault_is_bad(__err)	((__err) & DSISR_NOEXEC_OR_G)
412f3d96e69SBenjamin Herrenschmidt #elif defined(CONFIG_PPC64)
413f3d96e69SBenjamin Herrenschmidt #define page_fault_is_bad(__err)	((__err) & DSISR_BAD_FAULT_64S)
414f3d96e69SBenjamin Herrenschmidt #else
415f3d96e69SBenjamin Herrenschmidt #define page_fault_is_bad(__err)	((__err) & DSISR_BAD_FAULT_32S)
416f3d96e69SBenjamin Herrenschmidt #endif
41741b464e5SBenjamin Herrenschmidt #endif
41841b464e5SBenjamin Herrenschmidt 
41941b464e5SBenjamin Herrenschmidt /*
42014cf11afSPaul Mackerras  * For 600- and 800-family processors, the error_code parameter is DSISR
42114cf11afSPaul Mackerras  * for a data fault, SRR1 for an instruction fault. For 400-family processors
42214cf11afSPaul Mackerras  * the error_code parameter is ESR for a data fault, 0 for an instruction
42314cf11afSPaul Mackerras  * fault.
42414cf11afSPaul Mackerras  * For 64-bit processors, the error_code parameter is
42514cf11afSPaul Mackerras  *  - DSISR for a non-SLB data access fault,
42614cf11afSPaul Mackerras  *  - SRR1 & 0x08000000 for a non-SLB instruction access fault
42714cf11afSPaul Mackerras  *  - 0 any SLB fault.
42814cf11afSPaul Mackerras  *
42914cf11afSPaul Mackerras  * The return value is 0 if the fault was handled, or the signal
43014cf11afSPaul Mackerras  * number if this is a kernel fault that can't be handled here.
43114cf11afSPaul Mackerras  */
4327afad422SBenjamin Herrenschmidt static int __do_page_fault(struct pt_regs *regs, unsigned long address,
43314cf11afSPaul Mackerras 			   unsigned long error_code)
43414cf11afSPaul Mackerras {
43514cf11afSPaul Mackerras 	struct vm_area_struct * vma;
43614cf11afSPaul Mackerras 	struct mm_struct *mm = current->mm;
4379be72573SBenjamin Herrenschmidt 	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
438c433ec04SBenjamin Herrenschmidt  	int is_exec = TRAP(regs) == 0x400;
439da929f6aSChristophe Leroy 	int is_user = user_mode(regs);
44041b464e5SBenjamin Herrenschmidt 	int is_write = page_fault_is_write(error_code);
44150a7ca3cSSouptick Joarder 	vm_fault_t fault, major = 0;
4420e36b0d1SChristophe Leroy 	bool must_retry = false;
443b98cca44SAnshuman Khandual 	bool kprobe_fault = kprobe_page_fault(regs, 11);
44414cf11afSPaul Mackerras 
445b98cca44SAnshuman Khandual 	if (unlikely(debugger_fault_handler(regs) || kprobe_fault))
44665d47fd4SBenjamin Herrenschmidt 		return 0;
44714cf11afSPaul Mackerras 
448f3d96e69SBenjamin Herrenschmidt 	if (unlikely(page_fault_is_bad(error_code))) {
44965d47fd4SBenjamin Herrenschmidt 		if (is_user) {
450f3d96e69SBenjamin Herrenschmidt 			_exception(SIGBUS, regs, BUS_OBJERR, address);
45165d47fd4SBenjamin Herrenschmidt 			return 0;
45265d47fd4SBenjamin Herrenschmidt 		}
45365d47fd4SBenjamin Herrenschmidt 		return SIGBUS;
454e6c8290aSBenjamin Herrenschmidt 	}
455e6c8290aSBenjamin Herrenschmidt 
4562865d08dSBenjamin Herrenschmidt 	/* Additional sanity check(s) */
457374f3f59SAneesh Kumar K.V 	sanity_check_fault(is_write, is_user, error_code, address);
4582865d08dSBenjamin Herrenschmidt 
459d7df2443SBenjamin Herrenschmidt 	/*
460d7df2443SBenjamin Herrenschmidt 	 * The kernel should never take an execute fault nor should it
461de78a9c4SChristophe Leroy 	 * take a page fault to a kernel address or a page fault to a user
462de78a9c4SChristophe Leroy 	 * address outside of dedicated places
463d7df2443SBenjamin Herrenschmidt 	 */
4645e5be3aeSMichael Ellerman 	if (unlikely(!is_user && bad_kernel_fault(regs, error_code, address, is_write)))
46565d47fd4SBenjamin Herrenschmidt 		return SIGSEGV;
46614cf11afSPaul Mackerras 
46711ccdd33SBenjamin Herrenschmidt 	/*
46811ccdd33SBenjamin Herrenschmidt 	 * If we're in an interrupt, have no user context or are running
46911ccdd33SBenjamin Herrenschmidt 	 * in a region with pagefaults disabled then we must not take the fault
47011ccdd33SBenjamin Herrenschmidt 	 */
47111ccdd33SBenjamin Herrenschmidt 	if (unlikely(faulthandler_disabled() || !mm)) {
47211ccdd33SBenjamin Herrenschmidt 		if (is_user)
47311ccdd33SBenjamin Herrenschmidt 			printk_ratelimited(KERN_ERR "Page fault in user mode"
47411ccdd33SBenjamin Herrenschmidt 					   " with faulthandler_disabled()=%d"
47511ccdd33SBenjamin Herrenschmidt 					   " mm=%p\n",
47611ccdd33SBenjamin Herrenschmidt 					   faulthandler_disabled(), mm);
47711ccdd33SBenjamin Herrenschmidt 		return bad_area_nosemaphore(regs, address);
47811ccdd33SBenjamin Herrenschmidt 	}
47911ccdd33SBenjamin Herrenschmidt 
480a546498fSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
481a546498fSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
482a546498fSBenjamin Herrenschmidt 		local_irq_enable();
483a546498fSBenjamin Herrenschmidt 
484a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
4857dd1fcc2SPeter Zijlstra 
48699cd1302SRam Pai 	if (error_code & DSISR_KEYFAULT)
48799cd1302SRam Pai 		return bad_key_fault_exception(regs, address,
48899cd1302SRam Pai 					       get_mm_addr_key(mm, address));
489e6c2a479SRam Pai 
49069e044ddSAneesh Kumar K.V 	/*
49169e044ddSAneesh Kumar K.V 	 * We want to do this outside mmap_sem, because reading code around nip
49269e044ddSAneesh Kumar K.V 	 * can result in fault, which will cause a deadlock when called with
49369e044ddSAneesh Kumar K.V 	 * mmap_sem held
49469e044ddSAneesh Kumar K.V 	 */
495da929f6aSChristophe Leroy 	if (is_user)
496759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
497d2e0d2c5SBenjamin Herrenschmidt 	if (is_write)
498d2e0d2c5SBenjamin Herrenschmidt 		flags |= FAULT_FLAG_WRITE;
499d2e0d2c5SBenjamin Herrenschmidt 	if (is_exec)
500d2e0d2c5SBenjamin Herrenschmidt 		flags |= FAULT_FLAG_INSTRUCTION;
501759496baSJohannes Weiner 
50214cf11afSPaul Mackerras 	/* When running in the kernel we expect faults to occur only to
50314cf11afSPaul Mackerras 	 * addresses in user space.  All other faults represent errors in the
504fc5266eaSAnton Blanchard 	 * kernel and should generate an OOPS.  Unfortunately, in the case of an
505fc5266eaSAnton Blanchard 	 * erroneous fault occurring in a code path which already holds mmap_sem
50614cf11afSPaul Mackerras 	 * we will deadlock attempting to validate the fault against the
50714cf11afSPaul Mackerras 	 * address space.  Luckily the kernel only validly references user
50814cf11afSPaul Mackerras 	 * space from well defined areas of code, which are listed in the
50914cf11afSPaul Mackerras 	 * exceptions table.
51014cf11afSPaul Mackerras 	 *
51114cf11afSPaul Mackerras 	 * As the vast majority of faults will be valid we will only perform
512fc5266eaSAnton Blanchard 	 * the source reference check when there is a possibility of a deadlock.
51314cf11afSPaul Mackerras 	 * Attempt to lock the address space, if we cannot we then validate the
51414cf11afSPaul Mackerras 	 * source.  If this is invalid we can skip the address space check,
51514cf11afSPaul Mackerras 	 * thus avoiding the deadlock.
51614cf11afSPaul Mackerras 	 */
517b15021d9SBenjamin Herrenschmidt 	if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
518da929f6aSChristophe Leroy 		if (!is_user && !search_exception_tables(regs->nip))
519c3350602SBenjamin Herrenschmidt 			return bad_area_nosemaphore(regs, address);
52014cf11afSPaul Mackerras 
5219be72573SBenjamin Herrenschmidt retry:
52214cf11afSPaul Mackerras 		down_read(&mm->mmap_sem);
523a546498fSBenjamin Herrenschmidt 	} else {
524a546498fSBenjamin Herrenschmidt 		/*
525a546498fSBenjamin Herrenschmidt 		 * The above down_read_trylock() might have succeeded in
526a546498fSBenjamin Herrenschmidt 		 * which case we'll have missed the might_sleep() from
527a546498fSBenjamin Herrenschmidt 		 * down_read():
528a546498fSBenjamin Herrenschmidt 		 */
529a546498fSBenjamin Herrenschmidt 		might_sleep();
53014cf11afSPaul Mackerras 	}
53114cf11afSPaul Mackerras 
53214cf11afSPaul Mackerras 	vma = find_vma(mm, address);
533b15021d9SBenjamin Herrenschmidt 	if (unlikely(!vma))
534c3350602SBenjamin Herrenschmidt 		return bad_area(regs, address);
535b15021d9SBenjamin Herrenschmidt 	if (likely(vma->vm_start <= address))
53614cf11afSPaul Mackerras 		goto good_area;
537b15021d9SBenjamin Herrenschmidt 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
538c3350602SBenjamin Herrenschmidt 		return bad_area(regs, address);
53914cf11afSPaul Mackerras 
5408f5ca0b3SBenjamin Herrenschmidt 	/* The stack is being expanded, check if it's valid */
5410e36b0d1SChristophe Leroy 	if (unlikely(bad_stack_expansion(regs, address, vma, flags,
5420e36b0d1SChristophe Leroy 					 &must_retry))) {
5430e36b0d1SChristophe Leroy 		if (!must_retry)
544c3350602SBenjamin Herrenschmidt 			return bad_area(regs, address);
54514cf11afSPaul Mackerras 
5460e36b0d1SChristophe Leroy 		up_read(&mm->mmap_sem);
5470e36b0d1SChristophe Leroy 		if (fault_in_pages_readable((const char __user *)regs->nip,
5480e36b0d1SChristophe Leroy 					    sizeof(unsigned int)))
5490e36b0d1SChristophe Leroy 			return bad_area_nosemaphore(regs, address);
5500e36b0d1SChristophe Leroy 		goto retry;
5510e36b0d1SChristophe Leroy 	}
5520e36b0d1SChristophe Leroy 
5538f5ca0b3SBenjamin Herrenschmidt 	/* Try to expand it */
554b15021d9SBenjamin Herrenschmidt 	if (unlikely(expand_stack(vma, address)))
555c3350602SBenjamin Herrenschmidt 		return bad_area(regs, address);
55614cf11afSPaul Mackerras 
55714cf11afSPaul Mackerras good_area:
558bd0d63f8SBenjamin Herrenschmidt 	if (unlikely(access_error(is_write, is_exec, vma)))
559ecb101aeSJohn Sperbeck 		return bad_access(regs, address);
56014cf11afSPaul Mackerras 
56114cf11afSPaul Mackerras 	/*
56214cf11afSPaul Mackerras 	 * If for any reason at all we couldn't handle the fault,
56314cf11afSPaul Mackerras 	 * make sure we exit gracefully rather than endlessly redo
56414cf11afSPaul Mackerras 	 * the fault.
56514cf11afSPaul Mackerras 	 */
566dcddffd4SKirill A. Shutemov 	fault = handle_mm_fault(vma, address, flags);
567e6c2a479SRam Pai 
568e6c2a479SRam Pai #ifdef CONFIG_PPC_MEM_KEYS
569e6c2a479SRam Pai 	/*
570f2ed480fSAneesh Kumar K.V 	 * we skipped checking for access error due to key earlier.
571f2ed480fSAneesh Kumar K.V 	 * Check that using handle_mm_fault error return.
572e6c2a479SRam Pai 	 */
573e6c2a479SRam Pai 	if (unlikely(fault & VM_FAULT_SIGSEGV) &&
574f2ed480fSAneesh Kumar K.V 		!arch_vma_access_permitted(vma, is_write, is_exec, 0)) {
575f2ed480fSAneesh Kumar K.V 
576e6c2a479SRam Pai 		int pkey = vma_pkey(vma);
577e6c2a479SRam Pai 
57899cd1302SRam Pai 		up_read(&mm->mmap_sem);
57999cd1302SRam Pai 		return bad_key_fault_exception(regs, address, pkey);
58099cd1302SRam Pai 	}
581e6c2a479SRam Pai #endif /* CONFIG_PPC_MEM_KEYS */
582e6c2a479SRam Pai 
583f43bb27eSBenjamin Herrenschmidt 	major |= fault & VM_FAULT_MAJOR;
58414c02e41SLaurent Dufour 
585*c9a0dad1SPeter Xu 	if (fault_signal_pending(fault, regs))
586*c9a0dad1SPeter Xu 		return user_mode(regs) ? 0 : SIGBUS;
587*c9a0dad1SPeter Xu 
58814c02e41SLaurent Dufour 	/*
58914c02e41SLaurent Dufour 	 * Handle the retry right now, the mmap_sem has been released in that
59014c02e41SLaurent Dufour 	 * case.
59114c02e41SLaurent Dufour 	 */
59214c02e41SLaurent Dufour 	if (unlikely(fault & VM_FAULT_RETRY)) {
59314c02e41SLaurent Dufour 		/* We retry only once */
59414c02e41SLaurent Dufour 		if (flags & FAULT_FLAG_ALLOW_RETRY) {
59514c02e41SLaurent Dufour 			/*
59614c02e41SLaurent Dufour 			 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
59714c02e41SLaurent Dufour 			 * of starvation.
59814c02e41SLaurent Dufour 			 */
59914c02e41SLaurent Dufour 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
60014c02e41SLaurent Dufour 			flags |= FAULT_FLAG_TRIED;
60114c02e41SLaurent Dufour 			goto retry;
60214c02e41SLaurent Dufour 		}
603b5c8f0fdSBenjamin Herrenschmidt 	}
604b5c8f0fdSBenjamin Herrenschmidt 
605819cdcdbSLaurent Dufour 	up_read(&current->mm->mmap_sem);
60614c02e41SLaurent Dufour 
607b5c8f0fdSBenjamin Herrenschmidt 	if (unlikely(fault & VM_FAULT_ERROR))
608b5c8f0fdSBenjamin Herrenschmidt 		return mm_fault_error(regs, address, fault);
6099be72573SBenjamin Herrenschmidt 
6109be72573SBenjamin Herrenschmidt 	/*
61114c02e41SLaurent Dufour 	 * Major/minor page fault accounting.
6129be72573SBenjamin Herrenschmidt 	 */
613f43bb27eSBenjamin Herrenschmidt 	if (major) {
61483c54070SNick Piggin 		current->maj_flt++;
61504aafdc6SBenjamin Herrenschmidt 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
6163da02648SBenjamin Herrenschmidt 		cmo_account_page_fault();
617ac17dc8eSPeter Zijlstra 	} else {
61883c54070SNick Piggin 		current->min_flt++;
61904aafdc6SBenjamin Herrenschmidt 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
620ac17dc8eSPeter Zijlstra 	}
62165d47fd4SBenjamin Herrenschmidt 	return 0;
62214cf11afSPaul Mackerras }
6237afad422SBenjamin Herrenschmidt NOKPROBE_SYMBOL(__do_page_fault);
6247afad422SBenjamin Herrenschmidt 
6257afad422SBenjamin Herrenschmidt int do_page_fault(struct pt_regs *regs, unsigned long address,
6267afad422SBenjamin Herrenschmidt 		  unsigned long error_code)
6277afad422SBenjamin Herrenschmidt {
6287afad422SBenjamin Herrenschmidt 	enum ctx_state prev_state = exception_enter();
6297afad422SBenjamin Herrenschmidt 	int rc = __do_page_fault(regs, address, error_code);
630ba12eedeSLi Zhong 	exception_exit(prev_state);
631ba12eedeSLi Zhong 	return rc;
63214cf11afSPaul Mackerras }
63303465f89SNicholas Piggin NOKPROBE_SYMBOL(do_page_fault);
63414cf11afSPaul Mackerras 
63514cf11afSPaul Mackerras /*
63614cf11afSPaul Mackerras  * bad_page_fault is called when we have a bad access from the kernel.
63714cf11afSPaul Mackerras  * It is called from the DSI and ISI handlers in head.S and from some
63814cf11afSPaul Mackerras  * of the procedures in traps.c.
63914cf11afSPaul Mackerras  */
64014cf11afSPaul Mackerras void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
64114cf11afSPaul Mackerras {
64214cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
64346ddcb39SChristophe Leroy 	int is_write = page_fault_is_write(regs->dsisr);
64414cf11afSPaul Mackerras 
64514cf11afSPaul Mackerras 	/* Are we prepared to handle this fault?  */
64614cf11afSPaul Mackerras 	if ((entry = search_exception_tables(regs->nip)) != NULL) {
64761a92f70SNicholas Piggin 		regs->nip = extable_fixup(entry);
64814cf11afSPaul Mackerras 		return;
64914cf11afSPaul Mackerras 	}
65014cf11afSPaul Mackerras 
65114cf11afSPaul Mackerras 	/* kernel has accessed a bad area */
652723925b7SOlof Johansson 
6532271db20SBenjamin Herrenschmidt 	switch (TRAP(regs)) {
654723925b7SOlof Johansson 	case 0x300:
655723925b7SOlof Johansson 	case 0x380:
656d7b45615SSuraj Jitindar Singh 	case 0xe00:
65746ddcb39SChristophe Leroy 		pr_alert("BUG: %s on %s at 0x%08lx\n",
65849a502eaSChristophe Leroy 			 regs->dar < PAGE_SIZE ? "Kernel NULL pointer dereference" :
65946ddcb39SChristophe Leroy 			 "Unable to handle kernel data access",
66046ddcb39SChristophe Leroy 			 is_write ? "write" : "read", regs->dar);
661723925b7SOlof Johansson 		break;
662723925b7SOlof Johansson 	case 0x400:
663723925b7SOlof Johansson 	case 0x480:
66449a502eaSChristophe Leroy 		pr_alert("BUG: Unable to handle kernel instruction fetch%s",
66549a502eaSChristophe Leroy 			 regs->nip < PAGE_SIZE ? " (NULL pointer?)\n" : "\n");
666723925b7SOlof Johansson 		break;
667eab861a7SAnton Blanchard 	case 0x600:
66849a502eaSChristophe Leroy 		pr_alert("BUG: Unable to handle kernel unaligned access at 0x%08lx\n",
66949a502eaSChristophe Leroy 			 regs->dar);
670eab861a7SAnton Blanchard 		break;
671723925b7SOlof Johansson 	default:
67249a502eaSChristophe Leroy 		pr_alert("BUG: Unable to handle unknown paging fault at 0x%08lx\n",
67349a502eaSChristophe Leroy 			 regs->dar);
674a416dd8dSMichael Ellerman 		break;
675723925b7SOlof Johansson 	}
676723925b7SOlof Johansson 	printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
677723925b7SOlof Johansson 		regs->nip);
678723925b7SOlof Johansson 
679a70857e4SAaron Tomlin 	if (task_stack_end_corrupted(current))
68028b54990SAnton Blanchard 		printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
68128b54990SAnton Blanchard 
68214cf11afSPaul Mackerras 	die("Kernel access of bad area", regs, sig);
68314cf11afSPaul Mackerras }
684