xref: /openbmc/linux/arch/powerpc/mm/fault.c (revision 6ec20aa2e510b6297906c45f009aa08b2d97269a)
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.
236*6ec20aa2SChristophe 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 			int res;
2830e36b0d1SChristophe Leroy 
2840e36b0d1SChristophe Leroy 			pagefault_disable();
2850e36b0d1SChristophe Leroy 			res = __get_user_inatomic(inst, nip);
2860e36b0d1SChristophe Leroy 			pagefault_enable();
2870e36b0d1SChristophe Leroy 			if (!res)
2880e36b0d1SChristophe Leroy 				return !store_updates_sp(inst);
2890e36b0d1SChristophe Leroy 			*must_retry = true;
2900e36b0d1SChristophe Leroy 		}
2918f5ca0b3SBenjamin Herrenschmidt 		return true;
2928f5ca0b3SBenjamin Herrenschmidt 	}
2938f5ca0b3SBenjamin Herrenschmidt 	return false;
2948f5ca0b3SBenjamin Herrenschmidt }
2958f5ca0b3SBenjamin Herrenschmidt 
296bd0d63f8SBenjamin Herrenschmidt static bool access_error(bool is_write, bool is_exec,
297bd0d63f8SBenjamin Herrenschmidt 			 struct vm_area_struct *vma)
298bd0d63f8SBenjamin Herrenschmidt {
299bd0d63f8SBenjamin Herrenschmidt 	/*
300bd0d63f8SBenjamin Herrenschmidt 	 * Allow execution from readable areas if the MMU does not
301bd0d63f8SBenjamin Herrenschmidt 	 * provide separate controls over reading and executing.
302bd0d63f8SBenjamin Herrenschmidt 	 *
303bd0d63f8SBenjamin Herrenschmidt 	 * Note: That code used to not be enabled for 4xx/BookE.
304bd0d63f8SBenjamin Herrenschmidt 	 * It is now as I/D cache coherency for these is done at
305bd0d63f8SBenjamin Herrenschmidt 	 * set_pte_at() time and I see no reason why the test
306bd0d63f8SBenjamin Herrenschmidt 	 * below wouldn't be valid on those processors. This -may-
307bd0d63f8SBenjamin Herrenschmidt 	 * break programs compiled with a really old ABI though.
308bd0d63f8SBenjamin Herrenschmidt 	 */
309bd0d63f8SBenjamin Herrenschmidt 	if (is_exec) {
310bd0d63f8SBenjamin Herrenschmidt 		return !(vma->vm_flags & VM_EXEC) &&
311bd0d63f8SBenjamin Herrenschmidt 			(cpu_has_feature(CPU_FTR_NOEXECUTE) ||
312bd0d63f8SBenjamin Herrenschmidt 			 !(vma->vm_flags & (VM_READ | VM_WRITE)));
313bd0d63f8SBenjamin Herrenschmidt 	}
314bd0d63f8SBenjamin Herrenschmidt 
315bd0d63f8SBenjamin Herrenschmidt 	if (is_write) {
316bd0d63f8SBenjamin Herrenschmidt 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
317bd0d63f8SBenjamin Herrenschmidt 			return true;
318bd0d63f8SBenjamin Herrenschmidt 		return false;
319bd0d63f8SBenjamin Herrenschmidt 	}
320bd0d63f8SBenjamin Herrenschmidt 
321bd0d63f8SBenjamin Herrenschmidt 	if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
322bd0d63f8SBenjamin Herrenschmidt 		return true;
323f2ed480fSAneesh Kumar K.V 	/*
324f2ed480fSAneesh Kumar K.V 	 * We should ideally do the vma pkey access check here. But in the
325f2ed480fSAneesh Kumar K.V 	 * fault path, handle_mm_fault() also does the same check. To avoid
326f2ed480fSAneesh Kumar K.V 	 * these multiple checks, we skip it here and handle access error due
327f2ed480fSAneesh Kumar K.V 	 * to pkeys later.
328f2ed480fSAneesh Kumar K.V 	 */
329bd0d63f8SBenjamin Herrenschmidt 	return false;
330bd0d63f8SBenjamin Herrenschmidt }
331bd0d63f8SBenjamin Herrenschmidt 
3323da02648SBenjamin Herrenschmidt #ifdef CONFIG_PPC_SMLPAR
3333da02648SBenjamin Herrenschmidt static inline void cmo_account_page_fault(void)
3343da02648SBenjamin Herrenschmidt {
3353da02648SBenjamin Herrenschmidt 	if (firmware_has_feature(FW_FEATURE_CMO)) {
3363da02648SBenjamin Herrenschmidt 		u32 page_ins;
3373da02648SBenjamin Herrenschmidt 
3383da02648SBenjamin Herrenschmidt 		preempt_disable();
3393da02648SBenjamin Herrenschmidt 		page_ins = be32_to_cpu(get_lppaca()->page_ins);
3403da02648SBenjamin Herrenschmidt 		page_ins += 1 << PAGE_FACTOR;
3413da02648SBenjamin Herrenschmidt 		get_lppaca()->page_ins = cpu_to_be32(page_ins);
3423da02648SBenjamin Herrenschmidt 		preempt_enable();
3433da02648SBenjamin Herrenschmidt 	}
3443da02648SBenjamin Herrenschmidt }
3453da02648SBenjamin Herrenschmidt #else
3463da02648SBenjamin Herrenschmidt static inline void cmo_account_page_fault(void) { }
3473da02648SBenjamin Herrenschmidt #endif /* CONFIG_PPC_SMLPAR */
3483da02648SBenjamin Herrenschmidt 
3495b3e84fcSChristophe Leroy #ifdef CONFIG_PPC_BOOK3S
350374f3f59SAneesh Kumar K.V static void sanity_check_fault(bool is_write, bool is_user,
351374f3f59SAneesh Kumar K.V 			       unsigned long error_code, unsigned long address)
3522865d08dSBenjamin Herrenschmidt {
3532865d08dSBenjamin Herrenschmidt 	/*
354374f3f59SAneesh Kumar K.V 	 * Userspace trying to access kernel address, we get PROTFAULT for that.
355374f3f59SAneesh Kumar K.V 	 */
356374f3f59SAneesh Kumar K.V 	if (is_user && address >= TASK_SIZE) {
357374f3f59SAneesh Kumar K.V 		pr_crit_ratelimited("%s[%d]: User access of kernel address (%lx) - exploit attempt? (uid: %d)\n",
358374f3f59SAneesh Kumar K.V 				   current->comm, current->pid, address,
359374f3f59SAneesh Kumar K.V 				   from_kuid(&init_user_ns, current_uid()));
360374f3f59SAneesh Kumar K.V 		return;
361374f3f59SAneesh Kumar K.V 	}
362374f3f59SAneesh Kumar K.V 
363374f3f59SAneesh Kumar K.V 	/*
3642865d08dSBenjamin Herrenschmidt 	 * For hash translation mode, we should never get a
3652865d08dSBenjamin Herrenschmidt 	 * PROTFAULT. Any update to pte to reduce access will result in us
3662865d08dSBenjamin Herrenschmidt 	 * removing the hash page table entry, thus resulting in a DSISR_NOHPTE
3672865d08dSBenjamin Herrenschmidt 	 * fault instead of DSISR_PROTFAULT.
3682865d08dSBenjamin Herrenschmidt 	 *
3692865d08dSBenjamin Herrenschmidt 	 * A pte update to relax the access will not result in a hash page table
3702865d08dSBenjamin Herrenschmidt 	 * entry invalidate and hence can result in DSISR_PROTFAULT.
3712865d08dSBenjamin Herrenschmidt 	 * ptep_set_access_flags() doesn't do a hpte flush. This is why we have
3722865d08dSBenjamin Herrenschmidt 	 * the special !is_write in the below conditional.
3732865d08dSBenjamin Herrenschmidt 	 *
3742865d08dSBenjamin Herrenschmidt 	 * For platforms that doesn't supports coherent icache and do support
3752865d08dSBenjamin Herrenschmidt 	 * per page noexec bit, we do setup things such that we do the
3762865d08dSBenjamin Herrenschmidt 	 * sync between D/I cache via fault. But that is handled via low level
3772865d08dSBenjamin Herrenschmidt 	 * hash fault code (hash_page_do_lazy_icache()) and we should not reach
3782865d08dSBenjamin Herrenschmidt 	 * here in such case.
3792865d08dSBenjamin Herrenschmidt 	 *
3802865d08dSBenjamin Herrenschmidt 	 * For wrong access that can result in PROTFAULT, the above vma->vm_flags
3812865d08dSBenjamin Herrenschmidt 	 * check should handle those and hence we should fall to the bad_area
3822865d08dSBenjamin Herrenschmidt 	 * handling correctly.
3832865d08dSBenjamin Herrenschmidt 	 *
3842865d08dSBenjamin Herrenschmidt 	 * For embedded with per page exec support that doesn't support coherent
3852865d08dSBenjamin Herrenschmidt 	 * icache we do get PROTFAULT and we handle that D/I cache sync in
3862865d08dSBenjamin Herrenschmidt 	 * set_pte_at while taking the noexec/prot fault. Hence this is WARN_ON
3872865d08dSBenjamin Herrenschmidt 	 * is conditional for server MMU.
3882865d08dSBenjamin Herrenschmidt 	 *
3892865d08dSBenjamin Herrenschmidt 	 * For radix, we can get prot fault for autonuma case, because radix
3902865d08dSBenjamin Herrenschmidt 	 * page table will have them marked noaccess for user.
3912865d08dSBenjamin Herrenschmidt 	 */
392374f3f59SAneesh Kumar K.V 	if (radix_enabled() || is_write)
393374f3f59SAneesh Kumar K.V 		return;
394374f3f59SAneesh Kumar K.V 
3952865d08dSBenjamin Herrenschmidt 	WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
3962865d08dSBenjamin Herrenschmidt }
3972865d08dSBenjamin Herrenschmidt #else
398374f3f59SAneesh Kumar K.V static void sanity_check_fault(bool is_write, bool is_user,
399374f3f59SAneesh Kumar K.V 			       unsigned long error_code, unsigned long address) { }
4005b3e84fcSChristophe Leroy #endif /* CONFIG_PPC_BOOK3S */
4012865d08dSBenjamin Herrenschmidt 
40214cf11afSPaul Mackerras /*
40341b464e5SBenjamin Herrenschmidt  * Define the correct "is_write" bit in error_code based
40441b464e5SBenjamin Herrenschmidt  * on the processor family
40541b464e5SBenjamin Herrenschmidt  */
40641b464e5SBenjamin Herrenschmidt #if (defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
40741b464e5SBenjamin Herrenschmidt #define page_fault_is_write(__err)	((__err) & ESR_DST)
408f3d96e69SBenjamin Herrenschmidt #define page_fault_is_bad(__err)	(0)
40941b464e5SBenjamin Herrenschmidt #else
41041b464e5SBenjamin Herrenschmidt #define page_fault_is_write(__err)	((__err) & DSISR_ISSTORE)
411968159c0SChristophe Leroy #if defined(CONFIG_PPC_8xx)
4124915349bSChristophe Leroy #define page_fault_is_bad(__err)	((__err) & DSISR_NOEXEC_OR_G)
413f3d96e69SBenjamin Herrenschmidt #elif defined(CONFIG_PPC64)
414f3d96e69SBenjamin Herrenschmidt #define page_fault_is_bad(__err)	((__err) & DSISR_BAD_FAULT_64S)
415f3d96e69SBenjamin Herrenschmidt #else
416f3d96e69SBenjamin Herrenschmidt #define page_fault_is_bad(__err)	((__err) & DSISR_BAD_FAULT_32S)
417f3d96e69SBenjamin Herrenschmidt #endif
41841b464e5SBenjamin Herrenschmidt #endif
41941b464e5SBenjamin Herrenschmidt 
42041b464e5SBenjamin Herrenschmidt /*
42114cf11afSPaul Mackerras  * For 600- and 800-family processors, the error_code parameter is DSISR
42214cf11afSPaul Mackerras  * for a data fault, SRR1 for an instruction fault. For 400-family processors
42314cf11afSPaul Mackerras  * the error_code parameter is ESR for a data fault, 0 for an instruction
42414cf11afSPaul Mackerras  * fault.
42514cf11afSPaul Mackerras  * For 64-bit processors, the error_code parameter is
42614cf11afSPaul Mackerras  *  - DSISR for a non-SLB data access fault,
42714cf11afSPaul Mackerras  *  - SRR1 & 0x08000000 for a non-SLB instruction access fault
42814cf11afSPaul Mackerras  *  - 0 any SLB fault.
42914cf11afSPaul Mackerras  *
43014cf11afSPaul Mackerras  * The return value is 0 if the fault was handled, or the signal
43114cf11afSPaul Mackerras  * number if this is a kernel fault that can't be handled here.
43214cf11afSPaul Mackerras  */
4337afad422SBenjamin Herrenschmidt static int __do_page_fault(struct pt_regs *regs, unsigned long address,
43414cf11afSPaul Mackerras 			   unsigned long error_code)
43514cf11afSPaul Mackerras {
43614cf11afSPaul Mackerras 	struct vm_area_struct * vma;
43714cf11afSPaul Mackerras 	struct mm_struct *mm = current->mm;
4389be72573SBenjamin Herrenschmidt 	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
439c433ec04SBenjamin Herrenschmidt  	int is_exec = TRAP(regs) == 0x400;
440da929f6aSChristophe Leroy 	int is_user = user_mode(regs);
44141b464e5SBenjamin Herrenschmidt 	int is_write = page_fault_is_write(error_code);
44250a7ca3cSSouptick Joarder 	vm_fault_t fault, major = 0;
4430e36b0d1SChristophe Leroy 	bool must_retry = false;
444b98cca44SAnshuman Khandual 	bool kprobe_fault = kprobe_page_fault(regs, 11);
44514cf11afSPaul Mackerras 
446b98cca44SAnshuman Khandual 	if (unlikely(debugger_fault_handler(regs) || kprobe_fault))
44765d47fd4SBenjamin Herrenschmidt 		return 0;
44814cf11afSPaul Mackerras 
449f3d96e69SBenjamin Herrenschmidt 	if (unlikely(page_fault_is_bad(error_code))) {
45065d47fd4SBenjamin Herrenschmidt 		if (is_user) {
451f3d96e69SBenjamin Herrenschmidt 			_exception(SIGBUS, regs, BUS_OBJERR, address);
45265d47fd4SBenjamin Herrenschmidt 			return 0;
45365d47fd4SBenjamin Herrenschmidt 		}
45465d47fd4SBenjamin Herrenschmidt 		return SIGBUS;
455e6c8290aSBenjamin Herrenschmidt 	}
456e6c8290aSBenjamin Herrenschmidt 
4572865d08dSBenjamin Herrenschmidt 	/* Additional sanity check(s) */
458374f3f59SAneesh Kumar K.V 	sanity_check_fault(is_write, is_user, error_code, address);
4592865d08dSBenjamin Herrenschmidt 
460d7df2443SBenjamin Herrenschmidt 	/*
461d7df2443SBenjamin Herrenschmidt 	 * The kernel should never take an execute fault nor should it
462de78a9c4SChristophe Leroy 	 * take a page fault to a kernel address or a page fault to a user
463de78a9c4SChristophe Leroy 	 * address outside of dedicated places
464d7df2443SBenjamin Herrenschmidt 	 */
4655e5be3aeSMichael Ellerman 	if (unlikely(!is_user && bad_kernel_fault(regs, error_code, address, is_write)))
46665d47fd4SBenjamin Herrenschmidt 		return SIGSEGV;
46714cf11afSPaul Mackerras 
46811ccdd33SBenjamin Herrenschmidt 	/*
46911ccdd33SBenjamin Herrenschmidt 	 * If we're in an interrupt, have no user context or are running
47011ccdd33SBenjamin Herrenschmidt 	 * in a region with pagefaults disabled then we must not take the fault
47111ccdd33SBenjamin Herrenschmidt 	 */
47211ccdd33SBenjamin Herrenschmidt 	if (unlikely(faulthandler_disabled() || !mm)) {
47311ccdd33SBenjamin Herrenschmidt 		if (is_user)
47411ccdd33SBenjamin Herrenschmidt 			printk_ratelimited(KERN_ERR "Page fault in user mode"
47511ccdd33SBenjamin Herrenschmidt 					   " with faulthandler_disabled()=%d"
47611ccdd33SBenjamin Herrenschmidt 					   " mm=%p\n",
47711ccdd33SBenjamin Herrenschmidt 					   faulthandler_disabled(), mm);
47811ccdd33SBenjamin Herrenschmidt 		return bad_area_nosemaphore(regs, address);
47911ccdd33SBenjamin Herrenschmidt 	}
48011ccdd33SBenjamin Herrenschmidt 
481a546498fSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
482a546498fSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
483a546498fSBenjamin Herrenschmidt 		local_irq_enable();
484a546498fSBenjamin Herrenschmidt 
485a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
4867dd1fcc2SPeter Zijlstra 
48799cd1302SRam Pai 	if (error_code & DSISR_KEYFAULT)
48899cd1302SRam Pai 		return bad_key_fault_exception(regs, address,
48999cd1302SRam Pai 					       get_mm_addr_key(mm, address));
490e6c2a479SRam Pai 
49169e044ddSAneesh Kumar K.V 	/*
49269e044ddSAneesh Kumar K.V 	 * We want to do this outside mmap_sem, because reading code around nip
49369e044ddSAneesh Kumar K.V 	 * can result in fault, which will cause a deadlock when called with
49469e044ddSAneesh Kumar K.V 	 * mmap_sem held
49569e044ddSAneesh Kumar K.V 	 */
496da929f6aSChristophe Leroy 	if (is_user)
497759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
498d2e0d2c5SBenjamin Herrenschmidt 	if (is_write)
499d2e0d2c5SBenjamin Herrenschmidt 		flags |= FAULT_FLAG_WRITE;
500d2e0d2c5SBenjamin Herrenschmidt 	if (is_exec)
501d2e0d2c5SBenjamin Herrenschmidt 		flags |= FAULT_FLAG_INSTRUCTION;
502759496baSJohannes Weiner 
50314cf11afSPaul Mackerras 	/* When running in the kernel we expect faults to occur only to
50414cf11afSPaul Mackerras 	 * addresses in user space.  All other faults represent errors in the
505fc5266eaSAnton Blanchard 	 * kernel and should generate an OOPS.  Unfortunately, in the case of an
506fc5266eaSAnton Blanchard 	 * erroneous fault occurring in a code path which already holds mmap_sem
50714cf11afSPaul Mackerras 	 * we will deadlock attempting to validate the fault against the
50814cf11afSPaul Mackerras 	 * address space.  Luckily the kernel only validly references user
50914cf11afSPaul Mackerras 	 * space from well defined areas of code, which are listed in the
51014cf11afSPaul Mackerras 	 * exceptions table.
51114cf11afSPaul Mackerras 	 *
51214cf11afSPaul Mackerras 	 * As the vast majority of faults will be valid we will only perform
513fc5266eaSAnton Blanchard 	 * the source reference check when there is a possibility of a deadlock.
51414cf11afSPaul Mackerras 	 * Attempt to lock the address space, if we cannot we then validate the
51514cf11afSPaul Mackerras 	 * source.  If this is invalid we can skip the address space check,
51614cf11afSPaul Mackerras 	 * thus avoiding the deadlock.
51714cf11afSPaul Mackerras 	 */
518b15021d9SBenjamin Herrenschmidt 	if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
519da929f6aSChristophe Leroy 		if (!is_user && !search_exception_tables(regs->nip))
520c3350602SBenjamin Herrenschmidt 			return bad_area_nosemaphore(regs, address);
52114cf11afSPaul Mackerras 
5229be72573SBenjamin Herrenschmidt retry:
52314cf11afSPaul Mackerras 		down_read(&mm->mmap_sem);
524a546498fSBenjamin Herrenschmidt 	} else {
525a546498fSBenjamin Herrenschmidt 		/*
526a546498fSBenjamin Herrenschmidt 		 * The above down_read_trylock() might have succeeded in
527a546498fSBenjamin Herrenschmidt 		 * which case we'll have missed the might_sleep() from
528a546498fSBenjamin Herrenschmidt 		 * down_read():
529a546498fSBenjamin Herrenschmidt 		 */
530a546498fSBenjamin Herrenschmidt 		might_sleep();
53114cf11afSPaul Mackerras 	}
53214cf11afSPaul Mackerras 
53314cf11afSPaul Mackerras 	vma = find_vma(mm, address);
534b15021d9SBenjamin Herrenschmidt 	if (unlikely(!vma))
535c3350602SBenjamin Herrenschmidt 		return bad_area(regs, address);
536b15021d9SBenjamin Herrenschmidt 	if (likely(vma->vm_start <= address))
53714cf11afSPaul Mackerras 		goto good_area;
538b15021d9SBenjamin Herrenschmidt 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
539c3350602SBenjamin Herrenschmidt 		return bad_area(regs, address);
54014cf11afSPaul Mackerras 
5418f5ca0b3SBenjamin Herrenschmidt 	/* The stack is being expanded, check if it's valid */
5420e36b0d1SChristophe Leroy 	if (unlikely(bad_stack_expansion(regs, address, vma, flags,
5430e36b0d1SChristophe Leroy 					 &must_retry))) {
5440e36b0d1SChristophe Leroy 		if (!must_retry)
545c3350602SBenjamin Herrenschmidt 			return bad_area(regs, address);
54614cf11afSPaul Mackerras 
5470e36b0d1SChristophe Leroy 		up_read(&mm->mmap_sem);
5480e36b0d1SChristophe Leroy 		if (fault_in_pages_readable((const char __user *)regs->nip,
5490e36b0d1SChristophe Leroy 					    sizeof(unsigned int)))
5500e36b0d1SChristophe Leroy 			return bad_area_nosemaphore(regs, address);
5510e36b0d1SChristophe Leroy 		goto retry;
5520e36b0d1SChristophe Leroy 	}
5530e36b0d1SChristophe Leroy 
5548f5ca0b3SBenjamin Herrenschmidt 	/* Try to expand it */
555b15021d9SBenjamin Herrenschmidt 	if (unlikely(expand_stack(vma, address)))
556c3350602SBenjamin Herrenschmidt 		return bad_area(regs, address);
55714cf11afSPaul Mackerras 
55814cf11afSPaul Mackerras good_area:
559bd0d63f8SBenjamin Herrenschmidt 	if (unlikely(access_error(is_write, is_exec, vma)))
560ecb101aeSJohn Sperbeck 		return bad_access(regs, address);
56114cf11afSPaul Mackerras 
56214cf11afSPaul Mackerras 	/*
56314cf11afSPaul Mackerras 	 * If for any reason at all we couldn't handle the fault,
56414cf11afSPaul Mackerras 	 * make sure we exit gracefully rather than endlessly redo
56514cf11afSPaul Mackerras 	 * the fault.
56614cf11afSPaul Mackerras 	 */
567dcddffd4SKirill A. Shutemov 	fault = handle_mm_fault(vma, address, flags);
568e6c2a479SRam Pai 
569e6c2a479SRam Pai #ifdef CONFIG_PPC_MEM_KEYS
570e6c2a479SRam Pai 	/*
571f2ed480fSAneesh Kumar K.V 	 * we skipped checking for access error due to key earlier.
572f2ed480fSAneesh Kumar K.V 	 * Check that using handle_mm_fault error return.
573e6c2a479SRam Pai 	 */
574e6c2a479SRam Pai 	if (unlikely(fault & VM_FAULT_SIGSEGV) &&
575f2ed480fSAneesh Kumar K.V 		!arch_vma_access_permitted(vma, is_write, is_exec, 0)) {
576f2ed480fSAneesh Kumar K.V 
577e6c2a479SRam Pai 		int pkey = vma_pkey(vma);
578e6c2a479SRam Pai 
57999cd1302SRam Pai 		up_read(&mm->mmap_sem);
58099cd1302SRam Pai 		return bad_key_fault_exception(regs, address, pkey);
58199cd1302SRam Pai 	}
582e6c2a479SRam Pai #endif /* CONFIG_PPC_MEM_KEYS */
583e6c2a479SRam Pai 
584f43bb27eSBenjamin Herrenschmidt 	major |= fault & VM_FAULT_MAJOR;
58514c02e41SLaurent Dufour 
58614c02e41SLaurent Dufour 	/*
58714c02e41SLaurent Dufour 	 * Handle the retry right now, the mmap_sem has been released in that
58814c02e41SLaurent Dufour 	 * case.
58914c02e41SLaurent Dufour 	 */
59014c02e41SLaurent Dufour 	if (unlikely(fault & VM_FAULT_RETRY)) {
59114c02e41SLaurent Dufour 		/* We retry only once */
59214c02e41SLaurent Dufour 		if (flags & FAULT_FLAG_ALLOW_RETRY) {
59314c02e41SLaurent Dufour 			/*
59414c02e41SLaurent Dufour 			 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
59514c02e41SLaurent Dufour 			 * of starvation.
59614c02e41SLaurent Dufour 			 */
59714c02e41SLaurent Dufour 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
59814c02e41SLaurent Dufour 			flags |= FAULT_FLAG_TRIED;
59914c02e41SLaurent Dufour 			if (!fatal_signal_pending(current))
60014c02e41SLaurent Dufour 				goto retry;
60114c02e41SLaurent Dufour 		}
602b5c8f0fdSBenjamin Herrenschmidt 
603b5c8f0fdSBenjamin Herrenschmidt 		/*
604b5c8f0fdSBenjamin Herrenschmidt 		 * User mode? Just return to handle the fatal exception otherwise
605b5c8f0fdSBenjamin Herrenschmidt 		 * return to bad_page_fault
606b5c8f0fdSBenjamin Herrenschmidt 		 */
607b5c8f0fdSBenjamin Herrenschmidt 		return is_user ? 0 : SIGBUS;
608b5c8f0fdSBenjamin Herrenschmidt 	}
609b5c8f0fdSBenjamin Herrenschmidt 
610819cdcdbSLaurent Dufour 	up_read(&current->mm->mmap_sem);
61114c02e41SLaurent Dufour 
612b5c8f0fdSBenjamin Herrenschmidt 	if (unlikely(fault & VM_FAULT_ERROR))
613b5c8f0fdSBenjamin Herrenschmidt 		return mm_fault_error(regs, address, fault);
6149be72573SBenjamin Herrenschmidt 
6159be72573SBenjamin Herrenschmidt 	/*
61614c02e41SLaurent Dufour 	 * Major/minor page fault accounting.
6179be72573SBenjamin Herrenschmidt 	 */
618f43bb27eSBenjamin Herrenschmidt 	if (major) {
61983c54070SNick Piggin 		current->maj_flt++;
62004aafdc6SBenjamin Herrenschmidt 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
6213da02648SBenjamin Herrenschmidt 		cmo_account_page_fault();
622ac17dc8eSPeter Zijlstra 	} else {
62383c54070SNick Piggin 		current->min_flt++;
62404aafdc6SBenjamin Herrenschmidt 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
625ac17dc8eSPeter Zijlstra 	}
62665d47fd4SBenjamin Herrenschmidt 	return 0;
62714cf11afSPaul Mackerras }
6287afad422SBenjamin Herrenschmidt NOKPROBE_SYMBOL(__do_page_fault);
6297afad422SBenjamin Herrenschmidt 
6307afad422SBenjamin Herrenschmidt int do_page_fault(struct pt_regs *regs, unsigned long address,
6317afad422SBenjamin Herrenschmidt 		  unsigned long error_code)
6327afad422SBenjamin Herrenschmidt {
6337afad422SBenjamin Herrenschmidt 	enum ctx_state prev_state = exception_enter();
6347afad422SBenjamin Herrenschmidt 	int rc = __do_page_fault(regs, address, error_code);
635ba12eedeSLi Zhong 	exception_exit(prev_state);
636ba12eedeSLi Zhong 	return rc;
63714cf11afSPaul Mackerras }
63803465f89SNicholas Piggin NOKPROBE_SYMBOL(do_page_fault);
63914cf11afSPaul Mackerras 
64014cf11afSPaul Mackerras /*
64114cf11afSPaul Mackerras  * bad_page_fault is called when we have a bad access from the kernel.
64214cf11afSPaul Mackerras  * It is called from the DSI and ISI handlers in head.S and from some
64314cf11afSPaul Mackerras  * of the procedures in traps.c.
64414cf11afSPaul Mackerras  */
64514cf11afSPaul Mackerras void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
64614cf11afSPaul Mackerras {
64714cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
64846ddcb39SChristophe Leroy 	int is_write = page_fault_is_write(regs->dsisr);
64914cf11afSPaul Mackerras 
65014cf11afSPaul Mackerras 	/* Are we prepared to handle this fault?  */
65114cf11afSPaul Mackerras 	if ((entry = search_exception_tables(regs->nip)) != NULL) {
65261a92f70SNicholas Piggin 		regs->nip = extable_fixup(entry);
65314cf11afSPaul Mackerras 		return;
65414cf11afSPaul Mackerras 	}
65514cf11afSPaul Mackerras 
65614cf11afSPaul Mackerras 	/* kernel has accessed a bad area */
657723925b7SOlof Johansson 
6582271db20SBenjamin Herrenschmidt 	switch (TRAP(regs)) {
659723925b7SOlof Johansson 	case 0x300:
660723925b7SOlof Johansson 	case 0x380:
661d7b45615SSuraj Jitindar Singh 	case 0xe00:
66246ddcb39SChristophe Leroy 		pr_alert("BUG: %s on %s at 0x%08lx\n",
66349a502eaSChristophe Leroy 			 regs->dar < PAGE_SIZE ? "Kernel NULL pointer dereference" :
66446ddcb39SChristophe Leroy 			 "Unable to handle kernel data access",
66546ddcb39SChristophe Leroy 			 is_write ? "write" : "read", regs->dar);
666723925b7SOlof Johansson 		break;
667723925b7SOlof Johansson 	case 0x400:
668723925b7SOlof Johansson 	case 0x480:
66949a502eaSChristophe Leroy 		pr_alert("BUG: Unable to handle kernel instruction fetch%s",
67049a502eaSChristophe Leroy 			 regs->nip < PAGE_SIZE ? " (NULL pointer?)\n" : "\n");
671723925b7SOlof Johansson 		break;
672eab861a7SAnton Blanchard 	case 0x600:
67349a502eaSChristophe Leroy 		pr_alert("BUG: Unable to handle kernel unaligned access at 0x%08lx\n",
67449a502eaSChristophe Leroy 			 regs->dar);
675eab861a7SAnton Blanchard 		break;
676723925b7SOlof Johansson 	default:
67749a502eaSChristophe Leroy 		pr_alert("BUG: Unable to handle unknown paging fault at 0x%08lx\n",
67849a502eaSChristophe Leroy 			 regs->dar);
679a416dd8dSMichael Ellerman 		break;
680723925b7SOlof Johansson 	}
681723925b7SOlof Johansson 	printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
682723925b7SOlof Johansson 		regs->nip);
683723925b7SOlof Johansson 
684a70857e4SAaron Tomlin 	if (task_stack_end_corrupted(current))
68528b54990SAnton Blanchard 		printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
68628b54990SAnton Blanchard 
68714cf11afSPaul Mackerras 	die("Kernel access of bad area", regs, sig);
68814cf11afSPaul Mackerras }
689