xref: /openbmc/linux/arch/powerpc/mm/fault.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
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>
3590cbac0eSChristophe Leroy #include <linux/kfence.h>
3698c26a72SChristophe Leroy #include <linux/pkeys.h>
3714cf11afSPaul Mackerras 
3840900194SBrian King #include <asm/firmware.h>
393a96570fSNicholas Piggin #include <asm/interrupt.h>
4014cf11afSPaul Mackerras #include <asm/page.h>
4114cf11afSPaul Mackerras #include <asm/mmu.h>
4214cf11afSPaul Mackerras #include <asm/mmu_context.h>
4314cf11afSPaul Mackerras #include <asm/siginfo.h>
44ae3a197eSDavid Howells #include <asm/debug.h>
455e5be3aeSMichael Ellerman #include <asm/kup.h>
468094892dSJordan Niethe #include <asm/inst.h>
479f90b997SChristoph Hellwig 
48773b3e53SMichael Ellerman 
499be72573SBenjamin Herrenschmidt /*
509be72573SBenjamin Herrenschmidt  * do_page_fault error handling helpers
519be72573SBenjamin Herrenschmidt  */
529be72573SBenjamin Herrenschmidt 
53c3350602SBenjamin Herrenschmidt static int
__bad_area_nosemaphore(struct pt_regs * regs,unsigned long address,int si_code)54cd60ab7aSEric W. Biederman __bad_area_nosemaphore(struct pt_regs *regs, unsigned long address, int si_code)
55c3350602SBenjamin Herrenschmidt {
56c3350602SBenjamin Herrenschmidt 	/*
57c3350602SBenjamin Herrenschmidt 	 * If we are in kernel mode, bail out with a SEGV, this will
58c3350602SBenjamin Herrenschmidt 	 * be caught by the assembly which will restore the non-volatile
59c3350602SBenjamin Herrenschmidt 	 * registers before calling bad_page_fault()
60c3350602SBenjamin Herrenschmidt 	 */
61c3350602SBenjamin Herrenschmidt 	if (!user_mode(regs))
62c3350602SBenjamin Herrenschmidt 		return SIGSEGV;
63c3350602SBenjamin Herrenschmidt 
64cd60ab7aSEric W. Biederman 	_exception(SIGSEGV, regs, si_code, address);
65c3350602SBenjamin Herrenschmidt 
66c3350602SBenjamin Herrenschmidt 	return 0;
67c3350602SBenjamin Herrenschmidt }
68c3350602SBenjamin Herrenschmidt 
bad_area_nosemaphore(struct pt_regs * regs,unsigned long address)69c3350602SBenjamin Herrenschmidt static noinline int bad_area_nosemaphore(struct pt_regs *regs, unsigned long address)
70c3350602SBenjamin Herrenschmidt {
71cd60ab7aSEric W. Biederman 	return __bad_area_nosemaphore(regs, address, SEGV_MAPERR);
72c3350602SBenjamin Herrenschmidt }
73c3350602SBenjamin Herrenschmidt 
__bad_area(struct pt_regs * regs,unsigned long address,int si_code)749f2ee693SEric W. Biederman static int __bad_area(struct pt_regs *regs, unsigned long address, int si_code)
75c3350602SBenjamin Herrenschmidt {
76c3350602SBenjamin Herrenschmidt 	struct mm_struct *mm = current->mm;
77c3350602SBenjamin Herrenschmidt 
78c3350602SBenjamin Herrenschmidt 	/*
79c3350602SBenjamin Herrenschmidt 	 * Something tried to access memory that isn't in our memory map..
80c3350602SBenjamin Herrenschmidt 	 * Fix it, but check if it's kernel or user first..
81c3350602SBenjamin Herrenschmidt 	 */
82d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
83c3350602SBenjamin Herrenschmidt 
84cd60ab7aSEric W. Biederman 	return __bad_area_nosemaphore(regs, address, si_code);
85c3350602SBenjamin Herrenschmidt }
86c3350602SBenjamin Herrenschmidt 
bad_access_pkey(struct pt_regs * regs,unsigned long address,struct vm_area_struct * vma)87fe4a6856SAneesh Kumar K.V static noinline int bad_access_pkey(struct pt_regs *regs, unsigned long address,
88fe4a6856SAneesh Kumar K.V 				    struct vm_area_struct *vma)
8999cd1302SRam Pai {
90fe4a6856SAneesh Kumar K.V 	struct mm_struct *mm = current->mm;
91fe4a6856SAneesh Kumar K.V 	int pkey;
92fe4a6856SAneesh Kumar K.V 
93fe4a6856SAneesh Kumar K.V 	/*
94fe4a6856SAneesh Kumar K.V 	 * We don't try to fetch the pkey from page table because reading
95fe4a6856SAneesh Kumar K.V 	 * page table without locking doesn't guarantee stable pte value.
96fe4a6856SAneesh Kumar K.V 	 * Hence the pkey value that we return to userspace can be different
97fe4a6856SAneesh Kumar K.V 	 * from the pkey that actually caused access error.
98fe4a6856SAneesh Kumar K.V 	 *
99fe4a6856SAneesh Kumar K.V 	 * It does *not* guarantee that the VMA we find here
100fe4a6856SAneesh Kumar K.V 	 * was the one that we faulted on.
101fe4a6856SAneesh Kumar K.V 	 *
102fe4a6856SAneesh Kumar K.V 	 * 1. T1   : mprotect_key(foo, PAGE_SIZE, pkey=4);
103fe4a6856SAneesh Kumar K.V 	 * 2. T1   : set AMR to deny access to pkey=4, touches, page
104fe4a6856SAneesh Kumar K.V 	 * 3. T1   : faults...
105fe4a6856SAneesh Kumar K.V 	 * 4.    T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
106c1e8d7c6SMichel Lespinasse 	 * 5. T1   : enters fault handler, takes mmap_lock, etc...
107fe4a6856SAneesh Kumar K.V 	 * 6. T1   : reaches here, sees vma_pkey(vma)=5, when we really
108fe4a6856SAneesh Kumar K.V 	 *	     faulted on a pte with its pkey=4.
109fe4a6856SAneesh Kumar K.V 	 */
110fe4a6856SAneesh Kumar K.V 	pkey = vma_pkey(vma);
111fe4a6856SAneesh Kumar K.V 
112d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
113fe4a6856SAneesh Kumar K.V 
1148eb2ba25SEric W. Biederman 	/*
1158eb2ba25SEric W. Biederman 	 * If we are in kernel mode, bail out with a SEGV, this will
1168eb2ba25SEric W. Biederman 	 * be caught by the assembly which will restore the non-volatile
1178eb2ba25SEric W. Biederman 	 * registers before calling bad_page_fault()
1188eb2ba25SEric W. Biederman 	 */
1198eb2ba25SEric W. Biederman 	if (!user_mode(regs))
1208eb2ba25SEric W. Biederman 		return SIGSEGV;
1218eb2ba25SEric W. Biederman 
1225d8fb8a5SEric W. Biederman 	_exception_pkey(regs, address, pkey);
1238eb2ba25SEric W. Biederman 
1248eb2ba25SEric W. Biederman 	return 0;
125c3350602SBenjamin Herrenschmidt }
126c3350602SBenjamin Herrenschmidt 
bad_access(struct pt_regs * regs,unsigned long address)127ecb101aeSJohn Sperbeck static noinline int bad_access(struct pt_regs *regs, unsigned long address)
128ecb101aeSJohn Sperbeck {
1299f2ee693SEric W. Biederman 	return __bad_area(regs, address, SEGV_ACCERR);
130ecb101aeSJohn Sperbeck }
131ecb101aeSJohn Sperbeck 
do_sigbus(struct pt_regs * regs,unsigned long address,vm_fault_t fault)1323913fdd7SAnton Blanchard static int do_sigbus(struct pt_regs *regs, unsigned long address,
13350a7ca3cSSouptick Joarder 		     vm_fault_t fault)
1349be72573SBenjamin Herrenschmidt {
13563af5262SAnton Blanchard 	if (!user_mode(regs))
136b5c8f0fdSBenjamin Herrenschmidt 		return SIGBUS;
13763af5262SAnton Blanchard 
13841ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = BUS_ADRERR;
1393913fdd7SAnton Blanchard #ifdef CONFIG_MEMORY_FAILURE
1403913fdd7SAnton Blanchard 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
141f654fc07SEric W. Biederman 		unsigned int lsb = 0; /* shutup gcc */
142f654fc07SEric W. Biederman 
1433913fdd7SAnton Blanchard 		pr_err("MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
1443913fdd7SAnton Blanchard 			current->comm, current->pid, address);
1459d57472fSAnton Blanchard 
1469d57472fSAnton Blanchard 		if (fault & VM_FAULT_HWPOISON_LARGE)
1479d57472fSAnton Blanchard 			lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
1489d57472fSAnton Blanchard 		if (fault & VM_FAULT_HWPOISON)
1499d57472fSAnton Blanchard 			lsb = PAGE_SHIFT;
150f654fc07SEric W. Biederman 
151f8eac901SEric W. Biederman 		force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb);
152f654fc07SEric W. Biederman 		return 0;
153f654fc07SEric W. Biederman 	}
154f654fc07SEric W. Biederman 
1553913fdd7SAnton Blanchard #endif
1562e1661d2SEric W. Biederman 	force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
157b5c8f0fdSBenjamin Herrenschmidt 	return 0;
1589be72573SBenjamin Herrenschmidt }
1599be72573SBenjamin Herrenschmidt 
mm_fault_error(struct pt_regs * regs,unsigned long addr,vm_fault_t fault)16050a7ca3cSSouptick Joarder static int mm_fault_error(struct pt_regs *regs, unsigned long addr,
16150a7ca3cSSouptick Joarder 				vm_fault_t fault)
1629be72573SBenjamin Herrenschmidt {
1639be72573SBenjamin Herrenschmidt 	/*
164b5c8f0fdSBenjamin Herrenschmidt 	 * Kernel page fault interrupted by SIGKILL. We have no reason to
165b5c8f0fdSBenjamin Herrenschmidt 	 * continue processing.
1669be72573SBenjamin Herrenschmidt 	 */
167b5c8f0fdSBenjamin Herrenschmidt 	if (fatal_signal_pending(current) && !user_mode(regs))
168b5c8f0fdSBenjamin Herrenschmidt 		return SIGKILL;
1699be72573SBenjamin Herrenschmidt 
1709be72573SBenjamin Herrenschmidt 	/* Out of memory */
171c2d23f91SDavid Rientjes 	if (fault & VM_FAULT_OOM) {
172c2d23f91SDavid Rientjes 		/*
173c2d23f91SDavid Rientjes 		 * We ran out of memory, or some other thing happened to us that
174c2d23f91SDavid Rientjes 		 * made us unable to handle the page fault gracefully.
175c2d23f91SDavid Rientjes 		 */
176c2d23f91SDavid Rientjes 		if (!user_mode(regs))
177b5c8f0fdSBenjamin Herrenschmidt 			return SIGSEGV;
178c2d23f91SDavid Rientjes 		pagefault_out_of_memory();
179b5c8f0fdSBenjamin Herrenschmidt 	} else {
180b5c8f0fdSBenjamin Herrenschmidt 		if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
181b5c8f0fdSBenjamin Herrenschmidt 			     VM_FAULT_HWPOISON_LARGE))
1823913fdd7SAnton Blanchard 			return do_sigbus(regs, addr, fault);
183b5c8f0fdSBenjamin Herrenschmidt 		else if (fault & VM_FAULT_SIGSEGV)
184b5c8f0fdSBenjamin Herrenschmidt 			return bad_area_nosemaphore(regs, addr);
185b5c8f0fdSBenjamin Herrenschmidt 		else
1869be72573SBenjamin Herrenschmidt 			BUG();
187b5c8f0fdSBenjamin Herrenschmidt 	}
188b5c8f0fdSBenjamin Herrenschmidt 	return 0;
1899be72573SBenjamin Herrenschmidt }
19014cf11afSPaul Mackerras 
191d3ca5874SBenjamin Herrenschmidt /* Is this a bad kernel fault ? */
bad_kernel_fault(struct pt_regs * regs,unsigned long error_code,unsigned long address,bool is_write)192de78a9c4SChristophe Leroy static bool bad_kernel_fault(struct pt_regs *regs, unsigned long error_code,
1935e5be3aeSMichael Ellerman 			     unsigned long address, bool is_write)
194d3ca5874SBenjamin Herrenschmidt {
1957153d4bfSXiongwei Song 	int is_exec = TRAP(regs) == INTERRUPT_INST_STORAGE;
196de78a9c4SChristophe Leroy 
197cd5d5e60SChristophe Leroy 	if (is_exec) {
1980fb1c25aSChristophe Leroy 		pr_crit_ratelimited("kernel tried to execute %s page (%lx) - exploit attempt? (uid: %d)\n",
1990fb1c25aSChristophe Leroy 				    address >= TASK_SIZE ? "exec-protected" : "user",
2000fb1c25aSChristophe Leroy 				    address,
2010fb1c25aSChristophe Leroy 				    from_kuid(&init_user_ns, current_uid()));
2025e5be3aeSMichael Ellerman 
2035e5be3aeSMichael Ellerman 		// Kernel exec fault is always bad
2045e5be3aeSMichael Ellerman 		return true;
205d3ca5874SBenjamin Herrenschmidt 	}
206de78a9c4SChristophe Leroy 
2075e5be3aeSMichael Ellerman 	// Kernel fault on kernel address is bad
2085e5be3aeSMichael Ellerman 	if (address >= TASK_SIZE)
2095e5be3aeSMichael Ellerman 		return true;
2105e5be3aeSMichael Ellerman 
211cbd7e6caSChristophe Leroy 	// Read/write fault blocked by KUAP is bad, it can never succeed.
212cbd7e6caSChristophe Leroy 	if (bad_kuap_fault(regs, address, is_write)) {
213cbd7e6caSChristophe Leroy 		pr_crit_ratelimited("Kernel attempted to %s user page (%lx) - exploit attempt? (uid: %d)\n",
214cbd7e6caSChristophe Leroy 				    is_write ? "write" : "read", address,
215cbd7e6caSChristophe Leroy 				    from_kuid(&init_user_ns, current_uid()));
216cbd7e6caSChristophe Leroy 
2175e5be3aeSMichael Ellerman 		// Fault on user outside of certain regions (eg. copy_tofrom_user()) is bad
2185e5be3aeSMichael Ellerman 		if (!search_exception_tables(regs->nip))
2195e5be3aeSMichael Ellerman 			return true;
2205e5be3aeSMichael Ellerman 
2215e5be3aeSMichael Ellerman 		// Read/write fault in a valid region (the exception table search passed
2225e5be3aeSMichael Ellerman 		// above), but blocked by KUAP is bad, it can never succeed.
2233dc12dfeSChristophe Leroy 		return WARN(true, "Bug: %s fault blocked by KUAP!", is_write ? "Write" : "Read");
224cbd7e6caSChristophe Leroy 	}
2255e5be3aeSMichael Ellerman 
226cbd7e6caSChristophe Leroy 	// What's left? Kernel fault on user and allowed by KUAP in the faulting context.
2275e5be3aeSMichael Ellerman 	return false;
228d3ca5874SBenjamin Herrenschmidt }
229d3ca5874SBenjamin Herrenschmidt 
access_pkey_error(bool is_write,bool is_exec,bool is_pkey,struct vm_area_struct * vma)230fe4a6856SAneesh Kumar K.V static bool access_pkey_error(bool is_write, bool is_exec, bool is_pkey,
231bd0d63f8SBenjamin Herrenschmidt 			      struct vm_area_struct *vma)
232bd0d63f8SBenjamin Herrenschmidt {
233bd0d63f8SBenjamin Herrenschmidt 	/*
234fe4a6856SAneesh Kumar K.V 	 * Make sure to check the VMA so that we do not perform
235fe4a6856SAneesh Kumar K.V 	 * faults just to hit a pkey fault as soon as we fill in a
236fe4a6856SAneesh Kumar K.V 	 * page. Only called for current mm, hence foreign == 0
237fe4a6856SAneesh Kumar K.V 	 */
238fe4a6856SAneesh Kumar K.V 	if (!arch_vma_access_permitted(vma, is_write, is_exec, 0))
239fe4a6856SAneesh Kumar K.V 		return true;
240fe4a6856SAneesh Kumar K.V 
241fe4a6856SAneesh Kumar K.V 	return false;
242fe4a6856SAneesh Kumar K.V }
243fe4a6856SAneesh Kumar K.V 
access_error(bool is_write,bool is_exec,struct vm_area_struct * vma)244fe4a6856SAneesh Kumar K.V static bool access_error(bool is_write, bool is_exec, struct vm_area_struct *vma)
245fe4a6856SAneesh Kumar K.V {
246fe4a6856SAneesh Kumar K.V 	/*
247bd0d63f8SBenjamin Herrenschmidt 	 * Allow execution from readable areas if the MMU does not
248bd0d63f8SBenjamin Herrenschmidt 	 * provide separate controls over reading and executing.
249bd0d63f8SBenjamin Herrenschmidt 	 *
250bd0d63f8SBenjamin Herrenschmidt 	 * Note: That code used to not be enabled for 4xx/BookE.
251bd0d63f8SBenjamin Herrenschmidt 	 * It is now as I/D cache coherency for these is done at
252bd0d63f8SBenjamin Herrenschmidt 	 * set_pte_at() time and I see no reason why the test
253bd0d63f8SBenjamin Herrenschmidt 	 * below wouldn't be valid on those processors. This -may-
254bd0d63f8SBenjamin Herrenschmidt 	 * break programs compiled with a really old ABI though.
255bd0d63f8SBenjamin Herrenschmidt 	 */
256bd0d63f8SBenjamin Herrenschmidt 	if (is_exec) {
257bd0d63f8SBenjamin Herrenschmidt 		return !(vma->vm_flags & VM_EXEC) &&
258bd0d63f8SBenjamin Herrenschmidt 			(cpu_has_feature(CPU_FTR_NOEXECUTE) ||
259bd0d63f8SBenjamin Herrenschmidt 			 !(vma->vm_flags & (VM_READ | VM_WRITE)));
260bd0d63f8SBenjamin Herrenschmidt 	}
261bd0d63f8SBenjamin Herrenschmidt 
262bd0d63f8SBenjamin Herrenschmidt 	if (is_write) {
263bd0d63f8SBenjamin Herrenschmidt 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
264bd0d63f8SBenjamin Herrenschmidt 			return true;
265bd0d63f8SBenjamin Herrenschmidt 		return false;
266bd0d63f8SBenjamin Herrenschmidt 	}
267bd0d63f8SBenjamin Herrenschmidt 
268395cac77SRussell Currey 	/*
269f2c7e356SRussell Currey 	 * VM_READ, VM_WRITE and VM_EXEC all imply read permissions, as
270f2c7e356SRussell Currey 	 * defined in protection_map[].  Read faults can only be caused by
271f2c7e356SRussell Currey 	 * a PROT_NONE mapping, or with a PROT_EXEC-only mapping on Radix.
272395cac77SRussell Currey 	 */
273f2c7e356SRussell Currey 	if (unlikely(!vma_is_accessible(vma)))
274bd0d63f8SBenjamin Herrenschmidt 		return true;
275f2c7e356SRussell Currey 
276f2c7e356SRussell Currey 	if (unlikely(radix_enabled() && ((vma->vm_flags & VM_ACCESS_FLAGS) == VM_EXEC)))
277f2c7e356SRussell Currey 		return true;
278f2c7e356SRussell Currey 
279f2ed480fSAneesh Kumar K.V 	/*
280f2ed480fSAneesh Kumar K.V 	 * We should ideally do the vma pkey access check here. But in the
281f2ed480fSAneesh Kumar K.V 	 * fault path, handle_mm_fault() also does the same check. To avoid
282f2ed480fSAneesh Kumar K.V 	 * these multiple checks, we skip it here and handle access error due
283f2ed480fSAneesh Kumar K.V 	 * to pkeys later.
284f2ed480fSAneesh Kumar K.V 	 */
285bd0d63f8SBenjamin Herrenschmidt 	return false;
286bd0d63f8SBenjamin Herrenschmidt }
287bd0d63f8SBenjamin Herrenschmidt 
2883da02648SBenjamin Herrenschmidt #ifdef CONFIG_PPC_SMLPAR
cmo_account_page_fault(void)2893da02648SBenjamin Herrenschmidt static inline void cmo_account_page_fault(void)
2903da02648SBenjamin Herrenschmidt {
2913da02648SBenjamin Herrenschmidt 	if (firmware_has_feature(FW_FEATURE_CMO)) {
2923da02648SBenjamin Herrenschmidt 		u32 page_ins;
2933da02648SBenjamin Herrenschmidt 
2943da02648SBenjamin Herrenschmidt 		preempt_disable();
2953da02648SBenjamin Herrenschmidt 		page_ins = be32_to_cpu(get_lppaca()->page_ins);
2963da02648SBenjamin Herrenschmidt 		page_ins += 1 << PAGE_FACTOR;
2973da02648SBenjamin Herrenschmidt 		get_lppaca()->page_ins = cpu_to_be32(page_ins);
2983da02648SBenjamin Herrenschmidt 		preempt_enable();
2993da02648SBenjamin Herrenschmidt 	}
3003da02648SBenjamin Herrenschmidt }
3013da02648SBenjamin Herrenschmidt #else
cmo_account_page_fault(void)3023da02648SBenjamin Herrenschmidt static inline void cmo_account_page_fault(void) { }
3033da02648SBenjamin Herrenschmidt #endif /* CONFIG_PPC_SMLPAR */
3043da02648SBenjamin Herrenschmidt 
sanity_check_fault(bool is_write,bool is_user,unsigned long error_code,unsigned long address)305374f3f59SAneesh Kumar K.V static void sanity_check_fault(bool is_write, bool is_user,
306374f3f59SAneesh Kumar K.V 			       unsigned long error_code, unsigned long address)
3072865d08dSBenjamin Herrenschmidt {
3082865d08dSBenjamin Herrenschmidt 	/*
309374f3f59SAneesh Kumar K.V 	 * Userspace trying to access kernel address, we get PROTFAULT for that.
310374f3f59SAneesh Kumar K.V 	 */
311374f3f59SAneesh Kumar K.V 	if (is_user && address >= TASK_SIZE) {
3120f9aee0cSChristophe Leroy 		if ((long)address == -1)
3130f9aee0cSChristophe Leroy 			return;
3140f9aee0cSChristophe Leroy 
315374f3f59SAneesh Kumar K.V 		pr_crit_ratelimited("%s[%d]: User access of kernel address (%lx) - exploit attempt? (uid: %d)\n",
316374f3f59SAneesh Kumar K.V 				   current->comm, current->pid, address,
317374f3f59SAneesh Kumar K.V 				   from_kuid(&init_user_ns, current_uid()));
318374f3f59SAneesh Kumar K.V 		return;
319374f3f59SAneesh Kumar K.V 	}
320374f3f59SAneesh Kumar K.V 
3217ceb4002SChristophe Leroy 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S))
3227ceb4002SChristophe Leroy 		return;
3237ceb4002SChristophe Leroy 
324374f3f59SAneesh Kumar K.V 	/*
3252865d08dSBenjamin Herrenschmidt 	 * For hash translation mode, we should never get a
3262865d08dSBenjamin Herrenschmidt 	 * PROTFAULT. Any update to pte to reduce access will result in us
3272865d08dSBenjamin Herrenschmidt 	 * removing the hash page table entry, thus resulting in a DSISR_NOHPTE
3282865d08dSBenjamin Herrenschmidt 	 * fault instead of DSISR_PROTFAULT.
3292865d08dSBenjamin Herrenschmidt 	 *
3302865d08dSBenjamin Herrenschmidt 	 * A pte update to relax the access will not result in a hash page table
3312865d08dSBenjamin Herrenschmidt 	 * entry invalidate and hence can result in DSISR_PROTFAULT.
3322865d08dSBenjamin Herrenschmidt 	 * ptep_set_access_flags() doesn't do a hpte flush. This is why we have
3332865d08dSBenjamin Herrenschmidt 	 * the special !is_write in the below conditional.
3342865d08dSBenjamin Herrenschmidt 	 *
3352865d08dSBenjamin Herrenschmidt 	 * For platforms that doesn't supports coherent icache and do support
3362865d08dSBenjamin Herrenschmidt 	 * per page noexec bit, we do setup things such that we do the
3372865d08dSBenjamin Herrenschmidt 	 * sync between D/I cache via fault. But that is handled via low level
3382865d08dSBenjamin Herrenschmidt 	 * hash fault code (hash_page_do_lazy_icache()) and we should not reach
3392865d08dSBenjamin Herrenschmidt 	 * here in such case.
3402865d08dSBenjamin Herrenschmidt 	 *
3412865d08dSBenjamin Herrenschmidt 	 * For wrong access that can result in PROTFAULT, the above vma->vm_flags
3422865d08dSBenjamin Herrenschmidt 	 * check should handle those and hence we should fall to the bad_area
3432865d08dSBenjamin Herrenschmidt 	 * handling correctly.
3442865d08dSBenjamin Herrenschmidt 	 *
3452865d08dSBenjamin Herrenschmidt 	 * For embedded with per page exec support that doesn't support coherent
3462865d08dSBenjamin Herrenschmidt 	 * icache we do get PROTFAULT and we handle that D/I cache sync in
3472865d08dSBenjamin Herrenschmidt 	 * set_pte_at while taking the noexec/prot fault. Hence this is WARN_ON
3482865d08dSBenjamin Herrenschmidt 	 * is conditional for server MMU.
3492865d08dSBenjamin Herrenschmidt 	 *
3502865d08dSBenjamin Herrenschmidt 	 * For radix, we can get prot fault for autonuma case, because radix
3512865d08dSBenjamin Herrenschmidt 	 * page table will have them marked noaccess for user.
3522865d08dSBenjamin Herrenschmidt 	 */
353374f3f59SAneesh Kumar K.V 	if (radix_enabled() || is_write)
354374f3f59SAneesh Kumar K.V 		return;
355374f3f59SAneesh Kumar K.V 
3562865d08dSBenjamin Herrenschmidt 	WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
3572865d08dSBenjamin Herrenschmidt }
3582865d08dSBenjamin Herrenschmidt 
35914cf11afSPaul Mackerras /*
36041b464e5SBenjamin Herrenschmidt  * Define the correct "is_write" bit in error_code based
36141b464e5SBenjamin Herrenschmidt  * on the processor family
36241b464e5SBenjamin Herrenschmidt  */
36341b464e5SBenjamin Herrenschmidt #if (defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
36441b464e5SBenjamin Herrenschmidt #define page_fault_is_write(__err)	((__err) & ESR_DST)
36541b464e5SBenjamin Herrenschmidt #else
36641b464e5SBenjamin Herrenschmidt #define page_fault_is_write(__err)	((__err) & DSISR_ISSTORE)
3675250d026SChristophe Leroy #endif
3685250d026SChristophe Leroy 
3695250d026SChristophe Leroy #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
3705250d026SChristophe Leroy #define page_fault_is_bad(__err)	(0)
3715250d026SChristophe Leroy #elif defined(CONFIG_PPC_8xx)
3724915349bSChristophe Leroy #define page_fault_is_bad(__err)	((__err) & DSISR_NOEXEC_OR_G)
373f3d96e69SBenjamin Herrenschmidt #elif defined(CONFIG_PPC64)
page_fault_is_bad(unsigned long err)374335e1a91SHaren Myneni static int page_fault_is_bad(unsigned long err)
375335e1a91SHaren Myneni {
376335e1a91SHaren Myneni 	unsigned long flag = DSISR_BAD_FAULT_64S;
377335e1a91SHaren Myneni 
378335e1a91SHaren Myneni 	/*
379335e1a91SHaren Myneni 	 * PAPR+ v2.11 § 14.15.3.4.1 (unreleased)
380335e1a91SHaren Myneni 	 * If byte 0, bit 3 of pi-attribute-specifier-type in
381335e1a91SHaren Myneni 	 * ibm,pi-features property is defined, ignore the DSI error
382335e1a91SHaren Myneni 	 * which is caused by the paste instruction on the
383335e1a91SHaren Myneni 	 * suspended NX window.
384335e1a91SHaren Myneni 	 */
385335e1a91SHaren Myneni 	if (mmu_has_feature(MMU_FTR_NX_DSI))
386335e1a91SHaren Myneni 		flag &= ~DSISR_BAD_COPYPASTE;
387335e1a91SHaren Myneni 
388335e1a91SHaren Myneni 	return err & flag;
389335e1a91SHaren Myneni }
390f3d96e69SBenjamin Herrenschmidt #else
391f3d96e69SBenjamin Herrenschmidt #define page_fault_is_bad(__err)	((__err) & DSISR_BAD_FAULT_32S)
392f3d96e69SBenjamin Herrenschmidt #endif
39341b464e5SBenjamin Herrenschmidt 
39441b464e5SBenjamin Herrenschmidt /*
39514cf11afSPaul Mackerras  * For 600- and 800-family processors, the error_code parameter is DSISR
39631d6490cSNicholas Piggin  * for a data fault, SRR1 for an instruction fault.
39731d6490cSNicholas Piggin  * For 400-family processors the error_code parameter is ESR for a data fault,
39831d6490cSNicholas Piggin  * 0 for an instruction fault.
39931d6490cSNicholas Piggin  * For 64-bit processors, the error_code parameter is DSISR for a data access
40031d6490cSNicholas Piggin  * fault, SRR1 & 0x08000000 for an instruction access fault.
40114cf11afSPaul Mackerras  *
40214cf11afSPaul Mackerras  * The return value is 0 if the fault was handled, or the signal
40314cf11afSPaul Mackerras  * number if this is a kernel fault that can't be handled here.
40414cf11afSPaul Mackerras  */
___do_page_fault(struct pt_regs * regs,unsigned long address,unsigned long error_code)405a008f8f9SNicholas Piggin static int ___do_page_fault(struct pt_regs *regs, unsigned long address,
40614cf11afSPaul Mackerras 			   unsigned long error_code)
40714cf11afSPaul Mackerras {
40814cf11afSPaul Mackerras 	struct vm_area_struct * vma;
40914cf11afSPaul Mackerras 	struct mm_struct *mm = current->mm;
410dde16072SPeter Xu 	unsigned int flags = FAULT_FLAG_DEFAULT;
4117153d4bfSXiongwei Song 	int is_exec = TRAP(regs) == INTERRUPT_INST_STORAGE;
412da929f6aSChristophe Leroy 	int is_user = user_mode(regs);
41341b464e5SBenjamin Herrenschmidt 	int is_write = page_fault_is_write(error_code);
41450a7ca3cSSouptick Joarder 	vm_fault_t fault, major = 0;
415b98cca44SAnshuman Khandual 	bool kprobe_fault = kprobe_page_fault(regs, 11);
41614cf11afSPaul Mackerras 
417b98cca44SAnshuman Khandual 	if (unlikely(debugger_fault_handler(regs) || kprobe_fault))
41865d47fd4SBenjamin Herrenschmidt 		return 0;
41914cf11afSPaul Mackerras 
420f3d96e69SBenjamin Herrenschmidt 	if (unlikely(page_fault_is_bad(error_code))) {
42165d47fd4SBenjamin Herrenschmidt 		if (is_user) {
422f3d96e69SBenjamin Herrenschmidt 			_exception(SIGBUS, regs, BUS_OBJERR, address);
42365d47fd4SBenjamin Herrenschmidt 			return 0;
42465d47fd4SBenjamin Herrenschmidt 		}
42565d47fd4SBenjamin Herrenschmidt 		return SIGBUS;
426e6c8290aSBenjamin Herrenschmidt 	}
427e6c8290aSBenjamin Herrenschmidt 
4282865d08dSBenjamin Herrenschmidt 	/* Additional sanity check(s) */
429374f3f59SAneesh Kumar K.V 	sanity_check_fault(is_write, is_user, error_code, address);
4302865d08dSBenjamin Herrenschmidt 
431d7df2443SBenjamin Herrenschmidt 	/*
432d7df2443SBenjamin Herrenschmidt 	 * The kernel should never take an execute fault nor should it
433de78a9c4SChristophe Leroy 	 * take a page fault to a kernel address or a page fault to a user
434*9ea8d8bfSRitesh Harjani (IBM) 	 * address outside of dedicated places.
435*9ea8d8bfSRitesh Harjani (IBM) 	 *
436*9ea8d8bfSRitesh Harjani (IBM) 	 * Rather than kfence directly reporting false negatives, search whether
437*9ea8d8bfSRitesh Harjani (IBM) 	 * the NIP belongs to the fixup table for cases where fault could come
438*9ea8d8bfSRitesh Harjani (IBM) 	 * from functions like copy_from_kernel_nofault().
439d7df2443SBenjamin Herrenschmidt 	 */
44090cbac0eSChristophe Leroy 	if (unlikely(!is_user && bad_kernel_fault(regs, error_code, address, is_write))) {
441*9ea8d8bfSRitesh Harjani (IBM) 		if (is_kfence_address((void *)address) &&
442*9ea8d8bfSRitesh Harjani (IBM) 		    !search_exception_tables(instruction_pointer(regs)) &&
443*9ea8d8bfSRitesh Harjani (IBM) 		    kfence_handle_page_fault(address, is_write, regs))
44490cbac0eSChristophe Leroy 			return 0;
44590cbac0eSChristophe Leroy 
44665d47fd4SBenjamin Herrenschmidt 		return SIGSEGV;
44790cbac0eSChristophe Leroy 	}
44814cf11afSPaul Mackerras 
44911ccdd33SBenjamin Herrenschmidt 	/*
45011ccdd33SBenjamin Herrenschmidt 	 * If we're in an interrupt, have no user context or are running
45111ccdd33SBenjamin Herrenschmidt 	 * in a region with pagefaults disabled then we must not take the fault
45211ccdd33SBenjamin Herrenschmidt 	 */
45311ccdd33SBenjamin Herrenschmidt 	if (unlikely(faulthandler_disabled() || !mm)) {
45411ccdd33SBenjamin Herrenschmidt 		if (is_user)
45511ccdd33SBenjamin Herrenschmidt 			printk_ratelimited(KERN_ERR "Page fault in user mode"
45611ccdd33SBenjamin Herrenschmidt 					   " with faulthandler_disabled()=%d"
45711ccdd33SBenjamin Herrenschmidt 					   " mm=%p\n",
45811ccdd33SBenjamin Herrenschmidt 					   faulthandler_disabled(), mm);
45911ccdd33SBenjamin Herrenschmidt 		return bad_area_nosemaphore(regs, address);
46011ccdd33SBenjamin Herrenschmidt 	}
46111ccdd33SBenjamin Herrenschmidt 
462e6f8a6c8SNicholas Piggin 	interrupt_cond_local_irq_enable(regs);
463a546498fSBenjamin Herrenschmidt 
464a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
4657dd1fcc2SPeter Zijlstra 
46669e044ddSAneesh Kumar K.V 	/*
467c1e8d7c6SMichel Lespinasse 	 * We want to do this outside mmap_lock, because reading code around nip
46869e044ddSAneesh Kumar K.V 	 * can result in fault, which will cause a deadlock when called with
469c1e8d7c6SMichel Lespinasse 	 * mmap_lock held
47069e044ddSAneesh Kumar K.V 	 */
471da929f6aSChristophe Leroy 	if (is_user)
472759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
473d2e0d2c5SBenjamin Herrenschmidt 	if (is_write)
474d2e0d2c5SBenjamin Herrenschmidt 		flags |= FAULT_FLAG_WRITE;
475d2e0d2c5SBenjamin Herrenschmidt 	if (is_exec)
476d2e0d2c5SBenjamin Herrenschmidt 		flags |= FAULT_FLAG_INSTRUCTION;
477759496baSJohannes Weiner 
47870d4cbc8SLaurent Dufour 	if (!(flags & FAULT_FLAG_USER))
47970d4cbc8SLaurent Dufour 		goto lock_mmap;
48070d4cbc8SLaurent Dufour 
48170d4cbc8SLaurent Dufour 	vma = lock_vma_under_rcu(mm, address);
48270d4cbc8SLaurent Dufour 	if (!vma)
48370d4cbc8SLaurent Dufour 		goto lock_mmap;
48470d4cbc8SLaurent Dufour 
48570d4cbc8SLaurent Dufour 	if (unlikely(access_pkey_error(is_write, is_exec,
48670d4cbc8SLaurent Dufour 				       (error_code & DSISR_KEYFAULT), vma))) {
48770d4cbc8SLaurent Dufour 		vma_end_read(vma);
48870d4cbc8SLaurent Dufour 		goto lock_mmap;
48970d4cbc8SLaurent Dufour 	}
49070d4cbc8SLaurent Dufour 
49170d4cbc8SLaurent Dufour 	if (unlikely(access_error(is_write, is_exec, vma))) {
49270d4cbc8SLaurent Dufour 		vma_end_read(vma);
49370d4cbc8SLaurent Dufour 		goto lock_mmap;
49470d4cbc8SLaurent Dufour 	}
49570d4cbc8SLaurent Dufour 
49670d4cbc8SLaurent Dufour 	fault = handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs);
4974089eef0SSuren Baghdasaryan 	if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED)))
49870d4cbc8SLaurent Dufour 		vma_end_read(vma);
49970d4cbc8SLaurent Dufour 
50070d4cbc8SLaurent Dufour 	if (!(fault & VM_FAULT_RETRY)) {
50170d4cbc8SLaurent Dufour 		count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
50270d4cbc8SLaurent Dufour 		goto done;
50370d4cbc8SLaurent Dufour 	}
50470d4cbc8SLaurent Dufour 	count_vm_vma_lock_event(VMA_LOCK_RETRY);
50570d4cbc8SLaurent Dufour 
50670d4cbc8SLaurent Dufour 	if (fault_signal_pending(fault, regs))
50770d4cbc8SLaurent Dufour 		return user_mode(regs) ? 0 : SIGBUS;
50870d4cbc8SLaurent Dufour 
50970d4cbc8SLaurent Dufour lock_mmap:
51070d4cbc8SLaurent Dufour 
51114cf11afSPaul Mackerras 	/* When running in the kernel we expect faults to occur only to
51214cf11afSPaul Mackerras 	 * addresses in user space.  All other faults represent errors in the
513fc5266eaSAnton Blanchard 	 * kernel and should generate an OOPS.  Unfortunately, in the case of an
514c1e8d7c6SMichel Lespinasse 	 * erroneous fault occurring in a code path which already holds mmap_lock
51514cf11afSPaul Mackerras 	 * we will deadlock attempting to validate the fault against the
51614cf11afSPaul Mackerras 	 * address space.  Luckily the kernel only validly references user
51714cf11afSPaul Mackerras 	 * space from well defined areas of code, which are listed in the
518e6fe228cSMichael Ellerman 	 * exceptions table. lock_mm_and_find_vma() handles that logic.
51914cf11afSPaul Mackerras 	 */
5209be72573SBenjamin Herrenschmidt retry:
521e6fe228cSMichael Ellerman 	vma = lock_mm_and_find_vma(mm, address, regs);
522b15021d9SBenjamin Herrenschmidt 	if (unlikely(!vma))
523e6fe228cSMichael Ellerman 		return bad_area_nosemaphore(regs, address);
524fe4a6856SAneesh Kumar K.V 
525fe4a6856SAneesh Kumar K.V 	if (unlikely(access_pkey_error(is_write, is_exec,
526fe4a6856SAneesh Kumar K.V 				       (error_code & DSISR_KEYFAULT), vma)))
527fe4a6856SAneesh Kumar K.V 		return bad_access_pkey(regs, address, vma);
528fe4a6856SAneesh Kumar K.V 
529bd0d63f8SBenjamin Herrenschmidt 	if (unlikely(access_error(is_write, is_exec, vma)))
530ecb101aeSJohn Sperbeck 		return bad_access(regs, address);
53114cf11afSPaul Mackerras 
53214cf11afSPaul Mackerras 	/*
53314cf11afSPaul Mackerras 	 * If for any reason at all we couldn't handle the fault,
53414cf11afSPaul Mackerras 	 * make sure we exit gracefully rather than endlessly redo
53514cf11afSPaul Mackerras 	 * the fault.
53614cf11afSPaul Mackerras 	 */
537428fdc09SPeter Xu 	fault = handle_mm_fault(vma, address, flags, regs);
538e6c2a479SRam Pai 
539f43bb27eSBenjamin Herrenschmidt 	major |= fault & VM_FAULT_MAJOR;
54014c02e41SLaurent Dufour 
541c9a0dad1SPeter Xu 	if (fault_signal_pending(fault, regs))
542c9a0dad1SPeter Xu 		return user_mode(regs) ? 0 : SIGBUS;
543c9a0dad1SPeter Xu 
544d9272525SPeter Xu 	/* The fault is fully completed (including releasing mmap lock) */
545d9272525SPeter Xu 	if (fault & VM_FAULT_COMPLETED)
546d9272525SPeter Xu 		goto out;
547d9272525SPeter Xu 
54814c02e41SLaurent Dufour 	/*
549c1e8d7c6SMichel Lespinasse 	 * Handle the retry right now, the mmap_lock has been released in that
55014c02e41SLaurent Dufour 	 * case.
55114c02e41SLaurent Dufour 	 */
55214c02e41SLaurent Dufour 	if (unlikely(fault & VM_FAULT_RETRY)) {
55314c02e41SLaurent Dufour 		flags |= FAULT_FLAG_TRIED;
55414c02e41SLaurent Dufour 		goto retry;
55514c02e41SLaurent Dufour 	}
556b5c8f0fdSBenjamin Herrenschmidt 
557d8ed45c5SMichel Lespinasse 	mmap_read_unlock(current->mm);
55814c02e41SLaurent Dufour 
55970d4cbc8SLaurent Dufour done:
560b5c8f0fdSBenjamin Herrenschmidt 	if (unlikely(fault & VM_FAULT_ERROR))
561b5c8f0fdSBenjamin Herrenschmidt 		return mm_fault_error(regs, address, fault);
5629be72573SBenjamin Herrenschmidt 
563d9272525SPeter Xu out:
5649be72573SBenjamin Herrenschmidt 	/*
56514c02e41SLaurent Dufour 	 * Major/minor page fault accounting.
5669be72573SBenjamin Herrenschmidt 	 */
567428fdc09SPeter Xu 	if (major)
5683da02648SBenjamin Herrenschmidt 		cmo_account_page_fault();
569428fdc09SPeter Xu 
57065d47fd4SBenjamin Herrenschmidt 	return 0;
57114cf11afSPaul Mackerras }
572a008f8f9SNicholas Piggin NOKPROBE_SYMBOL(___do_page_fault);
5737afad422SBenjamin Herrenschmidt 
__do_page_fault(struct pt_regs * regs)574c45ba4f4SNicholas Piggin static __always_inline void __do_page_fault(struct pt_regs *regs)
5757afad422SBenjamin Herrenschmidt {
5764cb84284SNicholas Piggin 	long err;
5774cb84284SNicholas Piggin 
578a008f8f9SNicholas Piggin 	err = ___do_page_fault(regs, regs->dar, regs->dsisr);
579c45ba4f4SNicholas Piggin 	if (unlikely(err))
580c45ba4f4SNicholas Piggin 		bad_page_fault(regs, err);
5814cb84284SNicholas Piggin }
5825f1888a0SChristophe Leroy 
DEFINE_INTERRUPT_HANDLER(do_page_fault)583c45ba4f4SNicholas Piggin DEFINE_INTERRUPT_HANDLER(do_page_fault)
584a008f8f9SNicholas Piggin {
585c45ba4f4SNicholas Piggin 	__do_page_fault(regs);
58614cf11afSPaul Mackerras }
58714cf11afSPaul Mackerras 
588a008f8f9SNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
589a008f8f9SNicholas Piggin /* Same as do_page_fault but interrupt entry has already run in do_hash_fault */
hash__do_page_fault(struct pt_regs * regs)590c45ba4f4SNicholas Piggin void hash__do_page_fault(struct pt_regs *regs)
591a008f8f9SNicholas Piggin {
592c45ba4f4SNicholas Piggin 	__do_page_fault(regs);
593a008f8f9SNicholas Piggin }
594a008f8f9SNicholas Piggin NOKPROBE_SYMBOL(hash__do_page_fault);
595a008f8f9SNicholas Piggin #endif
596a008f8f9SNicholas Piggin 
59714cf11afSPaul Mackerras /*
59814cf11afSPaul Mackerras  * bad_page_fault is called when we have a bad access from the kernel.
59914cf11afSPaul Mackerras  * It is called from the DSI and ISI handlers in head.S and from some
60014cf11afSPaul Mackerras  * of the procedures in traps.c.
60114cf11afSPaul Mackerras  */
__bad_page_fault(struct pt_regs * regs,int sig)602c45ba4f4SNicholas Piggin static void __bad_page_fault(struct pt_regs *regs, int sig)
60314cf11afSPaul Mackerras {
60446ddcb39SChristophe Leroy 	int is_write = page_fault_is_write(regs->dsisr);
605d4679ac8SMichael Ellerman 	const char *msg;
60614cf11afSPaul Mackerras 
60714cf11afSPaul Mackerras 	/* kernel has accessed a bad area */
608723925b7SOlof Johansson 
609d4679ac8SMichael Ellerman 	if (regs->dar < PAGE_SIZE)
610d4679ac8SMichael Ellerman 		msg = "Kernel NULL pointer dereference";
611d4679ac8SMichael Ellerman 	else
612d4679ac8SMichael Ellerman 		msg = "Unable to handle kernel data access";
613d4679ac8SMichael Ellerman 
6142271db20SBenjamin Herrenschmidt 	switch (TRAP(regs)) {
6157153d4bfSXiongwei Song 	case INTERRUPT_DATA_STORAGE:
6167153d4bfSXiongwei Song 	case INTERRUPT_H_DATA_STORAGE:
617d4679ac8SMichael Ellerman 		pr_alert("BUG: %s on %s at 0x%08lx\n", msg,
61846ddcb39SChristophe Leroy 			 is_write ? "write" : "read", regs->dar);
619723925b7SOlof Johansson 		break;
620d4679ac8SMichael Ellerman 	case INTERRUPT_DATA_SEGMENT:
621d4679ac8SMichael Ellerman 		pr_alert("BUG: %s at 0x%08lx\n", msg, regs->dar);
622d4679ac8SMichael Ellerman 		break;
6237153d4bfSXiongwei Song 	case INTERRUPT_INST_STORAGE:
6247153d4bfSXiongwei Song 	case INTERRUPT_INST_SEGMENT:
62549a502eaSChristophe Leroy 		pr_alert("BUG: Unable to handle kernel instruction fetch%s",
62649a502eaSChristophe Leroy 			 regs->nip < PAGE_SIZE ? " (NULL pointer?)\n" : "\n");
627723925b7SOlof Johansson 		break;
6287153d4bfSXiongwei Song 	case INTERRUPT_ALIGNMENT:
62949a502eaSChristophe Leroy 		pr_alert("BUG: Unable to handle kernel unaligned access at 0x%08lx\n",
63049a502eaSChristophe Leroy 			 regs->dar);
631eab861a7SAnton Blanchard 		break;
632723925b7SOlof Johansson 	default:
63349a502eaSChristophe Leroy 		pr_alert("BUG: Unable to handle unknown paging fault at 0x%08lx\n",
63449a502eaSChristophe Leroy 			 regs->dar);
635a416dd8dSMichael Ellerman 		break;
636723925b7SOlof Johansson 	}
637723925b7SOlof Johansson 	printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
638723925b7SOlof Johansson 		regs->nip);
639723925b7SOlof Johansson 
640a70857e4SAaron Tomlin 	if (task_stack_end_corrupted(current))
64128b54990SAnton Blanchard 		printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
64228b54990SAnton Blanchard 
64314cf11afSPaul Mackerras 	die("Kernel access of bad area", regs, sig);
64414cf11afSPaul Mackerras }
6455f1888a0SChristophe Leroy 
bad_page_fault(struct pt_regs * regs,int sig)6468458c628SNicholas Piggin void bad_page_fault(struct pt_regs *regs, int sig)
6475f1888a0SChristophe Leroy {
6485f1888a0SChristophe Leroy 	const struct exception_table_entry *entry;
6495f1888a0SChristophe Leroy 
6505f1888a0SChristophe Leroy 	/* Are we prepared to handle this fault?  */
6515f1888a0SChristophe Leroy 	entry = search_exception_tables(instruction_pointer(regs));
6525f1888a0SChristophe Leroy 	if (entry)
6535f1888a0SChristophe Leroy 		instruction_pointer_set(regs, extable_fixup(entry));
6545f1888a0SChristophe Leroy 	else
6558458c628SNicholas Piggin 		__bad_page_fault(regs, sig);
6565f1888a0SChristophe Leroy }
65771f47976SNicholas Piggin 
65871f47976SNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
DEFINE_INTERRUPT_HANDLER(do_bad_page_fault_segv)6593a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(do_bad_page_fault_segv)
66071f47976SNicholas Piggin {
66171f47976SNicholas Piggin 	bad_page_fault(regs, SIGSEGV);
66271f47976SNicholas Piggin }
663935b534cSNicholas Piggin 
664935b534cSNicholas Piggin /*
665935b534cSNicholas Piggin  * In radix, segment interrupts indicate the EA is not addressable by the
666935b534cSNicholas Piggin  * page table geometry, so they are always sent here.
667935b534cSNicholas Piggin  *
668935b534cSNicholas Piggin  * In hash, this is called if do_slb_fault returns error. Typically it is
669935b534cSNicholas Piggin  * because the EA was outside the region allowed by software.
670935b534cSNicholas Piggin  */
DEFINE_INTERRUPT_HANDLER(do_bad_segment_interrupt)671935b534cSNicholas Piggin DEFINE_INTERRUPT_HANDLER(do_bad_segment_interrupt)
672935b534cSNicholas Piggin {
673935b534cSNicholas Piggin 	int err = regs->result;
674935b534cSNicholas Piggin 
675935b534cSNicholas Piggin 	if (err == -EFAULT) {
676935b534cSNicholas Piggin 		if (user_mode(regs))
677935b534cSNicholas Piggin 			_exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
678935b534cSNicholas Piggin 		else
679935b534cSNicholas Piggin 			bad_page_fault(regs, SIGSEGV);
680935b534cSNicholas Piggin 	} else if (err == -EINVAL) {
681935b534cSNicholas Piggin 		unrecoverable_exception(regs);
682935b534cSNicholas Piggin 	} else {
683935b534cSNicholas Piggin 		BUG();
684935b534cSNicholas Piggin 	}
685935b534cSNicholas Piggin }
68671f47976SNicholas Piggin #endif
687