xref: /openbmc/linux/arch/powerpc/mm/fault.c (revision b15021d994f09e9309ad37c1821ce4e3ee0cd62d)
114cf11afSPaul Mackerras /*
214cf11afSPaul Mackerras  *  PowerPC version
314cf11afSPaul Mackerras  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
414cf11afSPaul Mackerras  *
514cf11afSPaul Mackerras  *  Derived from "arch/i386/mm/fault.c"
614cf11afSPaul Mackerras  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
714cf11afSPaul Mackerras  *
814cf11afSPaul Mackerras  *  Modified by Cort Dougan and Paul Mackerras.
914cf11afSPaul Mackerras  *
1014cf11afSPaul Mackerras  *  Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
1114cf11afSPaul Mackerras  *
1214cf11afSPaul Mackerras  *  This program is free software; you can redistribute it and/or
1314cf11afSPaul Mackerras  *  modify it under the terms of the GNU General Public License
1414cf11afSPaul Mackerras  *  as published by the Free Software Foundation; either version
1514cf11afSPaul Mackerras  *  2 of the License, or (at your option) any later version.
1614cf11afSPaul Mackerras  */
1714cf11afSPaul Mackerras 
1814cf11afSPaul Mackerras #include <linux/signal.h>
1914cf11afSPaul Mackerras #include <linux/sched.h>
2068db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
2114cf11afSPaul Mackerras #include <linux/kernel.h>
2214cf11afSPaul Mackerras #include <linux/errno.h>
2314cf11afSPaul Mackerras #include <linux/string.h>
2414cf11afSPaul Mackerras #include <linux/types.h>
2514cf11afSPaul Mackerras #include <linux/ptrace.h>
2614cf11afSPaul Mackerras #include <linux/mman.h>
2714cf11afSPaul Mackerras #include <linux/mm.h>
2814cf11afSPaul Mackerras #include <linux/interrupt.h>
2914cf11afSPaul Mackerras #include <linux/highmem.h>
308a39b05fSPaul Gortmaker #include <linux/extable.h>
3114cf11afSPaul Mackerras #include <linux/kprobes.h>
321eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
33cdd6c482SIngo Molnar #include <linux/perf_event.h>
3476462232SChristian Dietrich #include <linux/ratelimit.h>
35ba12eedeSLi Zhong #include <linux/context_tracking.h>
369d57472fSAnton Blanchard #include <linux/hugetlb.h>
3770ffdb93SDavid Hildenbrand #include <linux/uaccess.h>
3814cf11afSPaul Mackerras 
3940900194SBrian King #include <asm/firmware.h>
4014cf11afSPaul Mackerras #include <asm/page.h>
4114cf11afSPaul Mackerras #include <asm/pgtable.h>
4214cf11afSPaul Mackerras #include <asm/mmu.h>
4314cf11afSPaul Mackerras #include <asm/mmu_context.h>
4414cf11afSPaul Mackerras #include <asm/tlbflush.h>
4514cf11afSPaul Mackerras #include <asm/siginfo.h>
46ae3a197eSDavid Howells #include <asm/debug.h>
479f90b997SChristoph Hellwig 
48c3dcf53aSJimi Xenidis #include "icswx.h"
49c3dcf53aSJimi Xenidis 
50bb4be50eSBenjamin Herrenschmidt static inline bool notify_page_fault(struct pt_regs *regs)
514f9e87c0SAnil S Keshavamurthy {
52bb4be50eSBenjamin Herrenschmidt 	bool ret = false;
539f90b997SChristoph Hellwig 
54bb4be50eSBenjamin Herrenschmidt #ifdef CONFIG_KPROBES
559f90b997SChristoph Hellwig 	/* kprobe_running() needs smp_processor_id() */
569f90b997SChristoph Hellwig 	if (!user_mode(regs)) {
579f90b997SChristoph Hellwig 		preempt_disable();
589f90b997SChristoph Hellwig 		if (kprobe_running() && kprobe_fault_handler(regs, 11))
59bb4be50eSBenjamin Herrenschmidt 			ret = true;
609f90b997SChristoph Hellwig 		preempt_enable();
614f9e87c0SAnil S Keshavamurthy 	}
62bb4be50eSBenjamin Herrenschmidt #endif /* CONFIG_KPROBES */
63bb4be50eSBenjamin Herrenschmidt 
64bb4be50eSBenjamin Herrenschmidt 	if (unlikely(debugger_fault_handler(regs)))
65bb4be50eSBenjamin Herrenschmidt 		ret = true;
664f9e87c0SAnil S Keshavamurthy 
679f90b997SChristoph Hellwig 	return ret;
684f9e87c0SAnil S Keshavamurthy }
694f9e87c0SAnil S Keshavamurthy 
7014cf11afSPaul Mackerras /*
7114cf11afSPaul Mackerras  * Check whether the instruction at regs->nip is a store using
7214cf11afSPaul Mackerras  * an update addressing form which will update r1.
7314cf11afSPaul Mackerras  */
7414cf11afSPaul Mackerras static int store_updates_sp(struct pt_regs *regs)
7514cf11afSPaul Mackerras {
7614cf11afSPaul Mackerras 	unsigned int inst;
7714cf11afSPaul Mackerras 
7814cf11afSPaul Mackerras 	if (get_user(inst, (unsigned int __user *)regs->nip))
7914cf11afSPaul Mackerras 		return 0;
8014cf11afSPaul Mackerras 	/* check for 1 in the rA field */
8114cf11afSPaul Mackerras 	if (((inst >> 16) & 0x1f) != 1)
8214cf11afSPaul Mackerras 		return 0;
8314cf11afSPaul Mackerras 	/* check major opcode */
8414cf11afSPaul Mackerras 	switch (inst >> 26) {
8514cf11afSPaul Mackerras 	case 37:	/* stwu */
8614cf11afSPaul Mackerras 	case 39:	/* stbu */
8714cf11afSPaul Mackerras 	case 45:	/* sthu */
8814cf11afSPaul Mackerras 	case 53:	/* stfsu */
8914cf11afSPaul Mackerras 	case 55:	/* stfdu */
9014cf11afSPaul Mackerras 		return 1;
9114cf11afSPaul Mackerras 	case 62:	/* std or stdu */
9214cf11afSPaul Mackerras 		return (inst & 3) == 1;
9314cf11afSPaul Mackerras 	case 31:
9414cf11afSPaul Mackerras 		/* check minor opcode */
9514cf11afSPaul Mackerras 		switch ((inst >> 1) & 0x3ff) {
9614cf11afSPaul Mackerras 		case 181:	/* stdux */
9714cf11afSPaul Mackerras 		case 183:	/* stwux */
9814cf11afSPaul Mackerras 		case 247:	/* stbux */
9914cf11afSPaul Mackerras 		case 439:	/* sthux */
10014cf11afSPaul Mackerras 		case 695:	/* stfsux */
10114cf11afSPaul Mackerras 		case 759:	/* stfdux */
10214cf11afSPaul Mackerras 			return 1;
10314cf11afSPaul Mackerras 		}
10414cf11afSPaul Mackerras 	}
10514cf11afSPaul Mackerras 	return 0;
10614cf11afSPaul Mackerras }
1079be72573SBenjamin Herrenschmidt /*
1089be72573SBenjamin Herrenschmidt  * do_page_fault error handling helpers
1099be72573SBenjamin Herrenschmidt  */
1109be72573SBenjamin Herrenschmidt 
111c3350602SBenjamin Herrenschmidt static int
112c3350602SBenjamin Herrenschmidt __bad_area_nosemaphore(struct pt_regs *regs, unsigned long address, int si_code)
113c3350602SBenjamin Herrenschmidt {
114c3350602SBenjamin Herrenschmidt 	/*
115c3350602SBenjamin Herrenschmidt 	 * If we are in kernel mode, bail out with a SEGV, this will
116c3350602SBenjamin Herrenschmidt 	 * be caught by the assembly which will restore the non-volatile
117c3350602SBenjamin Herrenschmidt 	 * registers before calling bad_page_fault()
118c3350602SBenjamin Herrenschmidt 	 */
119c3350602SBenjamin Herrenschmidt 	if (!user_mode(regs))
120c3350602SBenjamin Herrenschmidt 		return SIGSEGV;
121c3350602SBenjamin Herrenschmidt 
122c3350602SBenjamin Herrenschmidt 	_exception(SIGSEGV, regs, si_code, address);
123c3350602SBenjamin Herrenschmidt 
124c3350602SBenjamin Herrenschmidt 	return 0;
125c3350602SBenjamin Herrenschmidt }
126c3350602SBenjamin Herrenschmidt 
127c3350602SBenjamin Herrenschmidt static noinline int bad_area_nosemaphore(struct pt_regs *regs, unsigned long address)
128c3350602SBenjamin Herrenschmidt {
129c3350602SBenjamin Herrenschmidt 	return __bad_area_nosemaphore(regs, address, SEGV_MAPERR);
130c3350602SBenjamin Herrenschmidt }
131c3350602SBenjamin Herrenschmidt 
132c3350602SBenjamin Herrenschmidt static int __bad_area(struct pt_regs *regs, unsigned long address, int si_code)
133c3350602SBenjamin Herrenschmidt {
134c3350602SBenjamin Herrenschmidt 	struct mm_struct *mm = current->mm;
135c3350602SBenjamin Herrenschmidt 
136c3350602SBenjamin Herrenschmidt 	/*
137c3350602SBenjamin Herrenschmidt 	 * Something tried to access memory that isn't in our memory map..
138c3350602SBenjamin Herrenschmidt 	 * Fix it, but check if it's kernel or user first..
139c3350602SBenjamin Herrenschmidt 	 */
140c3350602SBenjamin Herrenschmidt 	up_read(&mm->mmap_sem);
141c3350602SBenjamin Herrenschmidt 
142c3350602SBenjamin Herrenschmidt 	return __bad_area_nosemaphore(regs, address, si_code);
143c3350602SBenjamin Herrenschmidt }
144c3350602SBenjamin Herrenschmidt 
145c3350602SBenjamin Herrenschmidt static noinline int bad_area(struct pt_regs *regs, unsigned long address)
146c3350602SBenjamin Herrenschmidt {
147c3350602SBenjamin Herrenschmidt 	return __bad_area(regs, address, SEGV_MAPERR);
148c3350602SBenjamin Herrenschmidt }
149c3350602SBenjamin Herrenschmidt 
1503913fdd7SAnton Blanchard static int do_sigbus(struct pt_regs *regs, unsigned long address,
1513913fdd7SAnton Blanchard 		     unsigned int fault)
1529be72573SBenjamin Herrenschmidt {
1539be72573SBenjamin Herrenschmidt 	siginfo_t info;
1549d57472fSAnton Blanchard 	unsigned int lsb = 0;
1559be72573SBenjamin Herrenschmidt 
15663af5262SAnton Blanchard 	if (!user_mode(regs))
157b5c8f0fdSBenjamin Herrenschmidt 		return SIGBUS;
15863af5262SAnton Blanchard 
15941ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = BUS_ADRERR;
1609be72573SBenjamin Herrenschmidt 	info.si_signo = SIGBUS;
1619be72573SBenjamin Herrenschmidt 	info.si_errno = 0;
1629be72573SBenjamin Herrenschmidt 	info.si_code = BUS_ADRERR;
1639be72573SBenjamin Herrenschmidt 	info.si_addr = (void __user *)address;
1643913fdd7SAnton Blanchard #ifdef CONFIG_MEMORY_FAILURE
1653913fdd7SAnton Blanchard 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
1663913fdd7SAnton Blanchard 		pr_err("MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
1673913fdd7SAnton Blanchard 			current->comm, current->pid, address);
1683913fdd7SAnton Blanchard 		info.si_code = BUS_MCEERR_AR;
1693913fdd7SAnton Blanchard 	}
1709d57472fSAnton Blanchard 
1719d57472fSAnton Blanchard 	if (fault & VM_FAULT_HWPOISON_LARGE)
1729d57472fSAnton Blanchard 		lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
1739d57472fSAnton Blanchard 	if (fault & VM_FAULT_HWPOISON)
1749d57472fSAnton Blanchard 		lsb = PAGE_SHIFT;
1753913fdd7SAnton Blanchard #endif
1769d57472fSAnton Blanchard 	info.si_addr_lsb = lsb;
1779be72573SBenjamin Herrenschmidt 	force_sig_info(SIGBUS, &info, current);
178b5c8f0fdSBenjamin Herrenschmidt 	return 0;
1799be72573SBenjamin Herrenschmidt }
1809be72573SBenjamin Herrenschmidt 
1819be72573SBenjamin Herrenschmidt static int mm_fault_error(struct pt_regs *regs, unsigned long addr, int fault)
1829be72573SBenjamin Herrenschmidt {
1839be72573SBenjamin Herrenschmidt 	/*
184b5c8f0fdSBenjamin Herrenschmidt 	 * Kernel page fault interrupted by SIGKILL. We have no reason to
185b5c8f0fdSBenjamin Herrenschmidt 	 * continue processing.
1869be72573SBenjamin Herrenschmidt 	 */
187b5c8f0fdSBenjamin Herrenschmidt 	if (fatal_signal_pending(current) && !user_mode(regs))
188b5c8f0fdSBenjamin Herrenschmidt 		return SIGKILL;
1899be72573SBenjamin Herrenschmidt 
1909be72573SBenjamin Herrenschmidt 	/* Out of memory */
191c2d23f91SDavid Rientjes 	if (fault & VM_FAULT_OOM) {
192c2d23f91SDavid Rientjes 		/*
193c2d23f91SDavid Rientjes 		 * We ran out of memory, or some other thing happened to us that
194c2d23f91SDavid Rientjes 		 * made us unable to handle the page fault gracefully.
195c2d23f91SDavid Rientjes 		 */
196c2d23f91SDavid Rientjes 		if (!user_mode(regs))
197b5c8f0fdSBenjamin Herrenschmidt 			return SIGSEGV;
198c2d23f91SDavid Rientjes 		pagefault_out_of_memory();
199b5c8f0fdSBenjamin Herrenschmidt 	} else {
200b5c8f0fdSBenjamin Herrenschmidt 		if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
201b5c8f0fdSBenjamin Herrenschmidt 			     VM_FAULT_HWPOISON_LARGE))
2023913fdd7SAnton Blanchard 			return do_sigbus(regs, addr, fault);
203b5c8f0fdSBenjamin Herrenschmidt 		else if (fault & VM_FAULT_SIGSEGV)
204b5c8f0fdSBenjamin Herrenschmidt 			return bad_area_nosemaphore(regs, addr);
205b5c8f0fdSBenjamin Herrenschmidt 		else
2069be72573SBenjamin Herrenschmidt 			BUG();
207b5c8f0fdSBenjamin Herrenschmidt 	}
208b5c8f0fdSBenjamin Herrenschmidt 	return 0;
2099be72573SBenjamin Herrenschmidt }
21014cf11afSPaul Mackerras 
211d3ca5874SBenjamin Herrenschmidt /* Is this a bad kernel fault ? */
212d3ca5874SBenjamin Herrenschmidt static bool bad_kernel_fault(bool is_exec, unsigned long error_code,
213d3ca5874SBenjamin Herrenschmidt 			     unsigned long address)
214d3ca5874SBenjamin Herrenschmidt {
215d3ca5874SBenjamin Herrenschmidt 	if (is_exec && (error_code & (DSISR_NOEXEC_OR_G | DSISR_KEYFAULT))) {
216d3ca5874SBenjamin Herrenschmidt 		printk_ratelimited(KERN_CRIT "kernel tried to execute"
217d3ca5874SBenjamin Herrenschmidt 				   " exec-protected page (%lx) -"
218d3ca5874SBenjamin Herrenschmidt 				   "exploit attempt? (uid: %d)\n",
219d3ca5874SBenjamin Herrenschmidt 				   address, from_kuid(&init_user_ns,
220d3ca5874SBenjamin Herrenschmidt 						      current_uid()));
221d3ca5874SBenjamin Herrenschmidt 	}
222d3ca5874SBenjamin Herrenschmidt 	return is_exec || (address >= TASK_SIZE);
223d3ca5874SBenjamin Herrenschmidt }
224d3ca5874SBenjamin Herrenschmidt 
2253da02648SBenjamin Herrenschmidt #ifdef CONFIG_PPC_SMLPAR
2263da02648SBenjamin Herrenschmidt static inline void cmo_account_page_fault(void)
2273da02648SBenjamin Herrenschmidt {
2283da02648SBenjamin Herrenschmidt 	if (firmware_has_feature(FW_FEATURE_CMO)) {
2293da02648SBenjamin Herrenschmidt 		u32 page_ins;
2303da02648SBenjamin Herrenschmidt 
2313da02648SBenjamin Herrenschmidt 		preempt_disable();
2323da02648SBenjamin Herrenschmidt 		page_ins = be32_to_cpu(get_lppaca()->page_ins);
2333da02648SBenjamin Herrenschmidt 		page_ins += 1 << PAGE_FACTOR;
2343da02648SBenjamin Herrenschmidt 		get_lppaca()->page_ins = cpu_to_be32(page_ins);
2353da02648SBenjamin Herrenschmidt 		preempt_enable();
2363da02648SBenjamin Herrenschmidt 	}
2373da02648SBenjamin Herrenschmidt }
2383da02648SBenjamin Herrenschmidt #else
2393da02648SBenjamin Herrenschmidt static inline void cmo_account_page_fault(void) { }
2403da02648SBenjamin Herrenschmidt #endif /* CONFIG_PPC_SMLPAR */
2413da02648SBenjamin Herrenschmidt 
2422865d08dSBenjamin Herrenschmidt #ifdef CONFIG_PPC_STD_MMU
2432865d08dSBenjamin Herrenschmidt static void sanity_check_fault(bool is_write, unsigned long error_code)
2442865d08dSBenjamin Herrenschmidt {
2452865d08dSBenjamin Herrenschmidt 	/*
2462865d08dSBenjamin Herrenschmidt 	 * For hash translation mode, we should never get a
2472865d08dSBenjamin Herrenschmidt 	 * PROTFAULT. Any update to pte to reduce access will result in us
2482865d08dSBenjamin Herrenschmidt 	 * removing the hash page table entry, thus resulting in a DSISR_NOHPTE
2492865d08dSBenjamin Herrenschmidt 	 * fault instead of DSISR_PROTFAULT.
2502865d08dSBenjamin Herrenschmidt 	 *
2512865d08dSBenjamin Herrenschmidt 	 * A pte update to relax the access will not result in a hash page table
2522865d08dSBenjamin Herrenschmidt 	 * entry invalidate and hence can result in DSISR_PROTFAULT.
2532865d08dSBenjamin Herrenschmidt 	 * ptep_set_access_flags() doesn't do a hpte flush. This is why we have
2542865d08dSBenjamin Herrenschmidt 	 * the special !is_write in the below conditional.
2552865d08dSBenjamin Herrenschmidt 	 *
2562865d08dSBenjamin Herrenschmidt 	 * For platforms that doesn't supports coherent icache and do support
2572865d08dSBenjamin Herrenschmidt 	 * per page noexec bit, we do setup things such that we do the
2582865d08dSBenjamin Herrenschmidt 	 * sync between D/I cache via fault. But that is handled via low level
2592865d08dSBenjamin Herrenschmidt 	 * hash fault code (hash_page_do_lazy_icache()) and we should not reach
2602865d08dSBenjamin Herrenschmidt 	 * here in such case.
2612865d08dSBenjamin Herrenschmidt 	 *
2622865d08dSBenjamin Herrenschmidt 	 * For wrong access that can result in PROTFAULT, the above vma->vm_flags
2632865d08dSBenjamin Herrenschmidt 	 * check should handle those and hence we should fall to the bad_area
2642865d08dSBenjamin Herrenschmidt 	 * handling correctly.
2652865d08dSBenjamin Herrenschmidt 	 *
2662865d08dSBenjamin Herrenschmidt 	 * For embedded with per page exec support that doesn't support coherent
2672865d08dSBenjamin Herrenschmidt 	 * icache we do get PROTFAULT and we handle that D/I cache sync in
2682865d08dSBenjamin Herrenschmidt 	 * set_pte_at while taking the noexec/prot fault. Hence this is WARN_ON
2692865d08dSBenjamin Herrenschmidt 	 * is conditional for server MMU.
2702865d08dSBenjamin Herrenschmidt 	 *
2712865d08dSBenjamin Herrenschmidt 	 * For radix, we can get prot fault for autonuma case, because radix
2722865d08dSBenjamin Herrenschmidt 	 * page table will have them marked noaccess for user.
2732865d08dSBenjamin Herrenschmidt 	 */
2742865d08dSBenjamin Herrenschmidt 	if (!radix_enabled() && !is_write)
2752865d08dSBenjamin Herrenschmidt 		WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
2762865d08dSBenjamin Herrenschmidt }
2772865d08dSBenjamin Herrenschmidt #else
2782865d08dSBenjamin Herrenschmidt static void sanity_check_fault(bool is_write, unsigned long error_code) { }
2792865d08dSBenjamin Herrenschmidt #endif /* CONFIG_PPC_STD_MMU */
2802865d08dSBenjamin Herrenschmidt 
28114cf11afSPaul Mackerras /*
28241b464e5SBenjamin Herrenschmidt  * Define the correct "is_write" bit in error_code based
28341b464e5SBenjamin Herrenschmidt  * on the processor family
28441b464e5SBenjamin Herrenschmidt  */
28541b464e5SBenjamin Herrenschmidt #if (defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
28641b464e5SBenjamin Herrenschmidt #define page_fault_is_write(__err)	((__err) & ESR_DST)
287f3d96e69SBenjamin Herrenschmidt #define page_fault_is_bad(__err)	(0)
28841b464e5SBenjamin Herrenschmidt #else
28941b464e5SBenjamin Herrenschmidt #define page_fault_is_write(__err)	((__err) & DSISR_ISSTORE)
290f3d96e69SBenjamin Herrenschmidt #if defined(CONFIG_8xx)
291f3d96e69SBenjamin Herrenschmidt #define page_fault_is_bad(__err)	((__err) & 0x10000000)
292f3d96e69SBenjamin Herrenschmidt #elif defined(CONFIG_PPC64)
293f3d96e69SBenjamin Herrenschmidt #define page_fault_is_bad(__err)	((__err) & DSISR_BAD_FAULT_64S)
294f3d96e69SBenjamin Herrenschmidt #else
295f3d96e69SBenjamin Herrenschmidt #define page_fault_is_bad(__err)	((__err) & DSISR_BAD_FAULT_32S)
296f3d96e69SBenjamin Herrenschmidt #endif
29741b464e5SBenjamin Herrenschmidt #endif
29841b464e5SBenjamin Herrenschmidt 
29941b464e5SBenjamin Herrenschmidt /*
30014cf11afSPaul Mackerras  * For 600- and 800-family processors, the error_code parameter is DSISR
30114cf11afSPaul Mackerras  * for a data fault, SRR1 for an instruction fault. For 400-family processors
30214cf11afSPaul Mackerras  * the error_code parameter is ESR for a data fault, 0 for an instruction
30314cf11afSPaul Mackerras  * fault.
30414cf11afSPaul Mackerras  * For 64-bit processors, the error_code parameter is
30514cf11afSPaul Mackerras  *  - DSISR for a non-SLB data access fault,
30614cf11afSPaul Mackerras  *  - SRR1 & 0x08000000 for a non-SLB instruction access fault
30714cf11afSPaul Mackerras  *  - 0 any SLB fault.
30814cf11afSPaul Mackerras  *
30914cf11afSPaul Mackerras  * The return value is 0 if the fault was handled, or the signal
31014cf11afSPaul Mackerras  * number if this is a kernel fault that can't be handled here.
31114cf11afSPaul Mackerras  */
3127afad422SBenjamin Herrenschmidt static int __do_page_fault(struct pt_regs *regs, unsigned long address,
31314cf11afSPaul Mackerras 			   unsigned long error_code)
31414cf11afSPaul Mackerras {
31514cf11afSPaul Mackerras 	struct vm_area_struct * vma;
31614cf11afSPaul Mackerras 	struct mm_struct *mm = current->mm;
3179be72573SBenjamin Herrenschmidt 	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
318c433ec04SBenjamin Herrenschmidt  	int is_exec = TRAP(regs) == 0x400;
319da929f6aSChristophe Leroy 	int is_user = user_mode(regs);
32041b464e5SBenjamin Herrenschmidt 	int is_write = page_fault_is_write(error_code);
3219be72573SBenjamin Herrenschmidt 	int fault;
322b5c8f0fdSBenjamin Herrenschmidt 	int store_update_sp = 0;
32314cf11afSPaul Mackerras 
324c3dcf53aSJimi Xenidis #ifdef CONFIG_PPC_ICSWX
325c3dcf53aSJimi Xenidis 	/*
326c3dcf53aSJimi Xenidis 	 * we need to do this early because this "data storage
327c3dcf53aSJimi Xenidis 	 * interrupt" does not update the DAR/DEAR so we don't want to
328c3dcf53aSJimi Xenidis 	 * look at it
329c3dcf53aSJimi Xenidis 	 */
330c3dcf53aSJimi Xenidis 	if (error_code & ICSWX_DSI_UCT) {
331b5c8f0fdSBenjamin Herrenschmidt 		int rc = acop_handle_fault(regs, address, error_code);
3329be72573SBenjamin Herrenschmidt 		if (rc)
33365d47fd4SBenjamin Herrenschmidt 			return rc;
334c3dcf53aSJimi Xenidis 	}
3359be72573SBenjamin Herrenschmidt #endif /* CONFIG_PPC_ICSWX */
336c3dcf53aSJimi Xenidis 
3379f90b997SChristoph Hellwig 	if (notify_page_fault(regs))
33865d47fd4SBenjamin Herrenschmidt 		return 0;
33914cf11afSPaul Mackerras 
340f3d96e69SBenjamin Herrenschmidt 	if (unlikely(page_fault_is_bad(error_code))) {
34165d47fd4SBenjamin Herrenschmidt 		if (is_user) {
342f3d96e69SBenjamin Herrenschmidt 			_exception(SIGBUS, regs, BUS_OBJERR, address);
34365d47fd4SBenjamin Herrenschmidt 			return 0;
34465d47fd4SBenjamin Herrenschmidt 		}
34565d47fd4SBenjamin Herrenschmidt 		return SIGBUS;
346e6c8290aSBenjamin Herrenschmidt 	}
347e6c8290aSBenjamin Herrenschmidt 
3482865d08dSBenjamin Herrenschmidt 	/* Additional sanity check(s) */
3492865d08dSBenjamin Herrenschmidt 	sanity_check_fault(is_write, error_code);
3502865d08dSBenjamin Herrenschmidt 
351d7df2443SBenjamin Herrenschmidt 	/*
352d7df2443SBenjamin Herrenschmidt 	 * The kernel should never take an execute fault nor should it
353d7df2443SBenjamin Herrenschmidt 	 * take a page fault to a kernel address.
354d7df2443SBenjamin Herrenschmidt 	 */
355d3ca5874SBenjamin Herrenschmidt 	if (unlikely(!is_user && bad_kernel_fault(is_exec, error_code, address)))
35665d47fd4SBenjamin Herrenschmidt 		return SIGSEGV;
35714cf11afSPaul Mackerras 
35811ccdd33SBenjamin Herrenschmidt 	/*
35911ccdd33SBenjamin Herrenschmidt 	 * If we're in an interrupt, have no user context or are running
36011ccdd33SBenjamin Herrenschmidt 	 * in a region with pagefaults disabled then we must not take the fault
36111ccdd33SBenjamin Herrenschmidt 	 */
36211ccdd33SBenjamin Herrenschmidt 	if (unlikely(faulthandler_disabled() || !mm)) {
36311ccdd33SBenjamin Herrenschmidt 		if (is_user)
36411ccdd33SBenjamin Herrenschmidt 			printk_ratelimited(KERN_ERR "Page fault in user mode"
36511ccdd33SBenjamin Herrenschmidt 					   " with faulthandler_disabled()=%d"
36611ccdd33SBenjamin Herrenschmidt 					   " mm=%p\n",
36711ccdd33SBenjamin Herrenschmidt 					   faulthandler_disabled(), mm);
36811ccdd33SBenjamin Herrenschmidt 		return bad_area_nosemaphore(regs, address);
36911ccdd33SBenjamin Herrenschmidt 	}
37011ccdd33SBenjamin Herrenschmidt 
371a546498fSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
372a546498fSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
373a546498fSBenjamin Herrenschmidt 		local_irq_enable();
374a546498fSBenjamin Herrenschmidt 
375a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
3767dd1fcc2SPeter Zijlstra 
37769e044ddSAneesh Kumar K.V 	/*
37869e044ddSAneesh Kumar K.V 	 * We want to do this outside mmap_sem, because reading code around nip
37969e044ddSAneesh Kumar K.V 	 * can result in fault, which will cause a deadlock when called with
38069e044ddSAneesh Kumar K.V 	 * mmap_sem held
38169e044ddSAneesh Kumar K.V 	 */
382da929f6aSChristophe Leroy 	if (is_write && is_user)
38369e044ddSAneesh Kumar K.V 		store_update_sp = store_updates_sp(regs);
38469e044ddSAneesh Kumar K.V 
385da929f6aSChristophe Leroy 	if (is_user)
386759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
387759496baSJohannes Weiner 
38814cf11afSPaul Mackerras 	/* When running in the kernel we expect faults to occur only to
38914cf11afSPaul Mackerras 	 * addresses in user space.  All other faults represent errors in the
390fc5266eaSAnton Blanchard 	 * kernel and should generate an OOPS.  Unfortunately, in the case of an
391fc5266eaSAnton Blanchard 	 * erroneous fault occurring in a code path which already holds mmap_sem
39214cf11afSPaul Mackerras 	 * we will deadlock attempting to validate the fault against the
39314cf11afSPaul Mackerras 	 * address space.  Luckily the kernel only validly references user
39414cf11afSPaul Mackerras 	 * space from well defined areas of code, which are listed in the
39514cf11afSPaul Mackerras 	 * exceptions table.
39614cf11afSPaul Mackerras 	 *
39714cf11afSPaul Mackerras 	 * As the vast majority of faults will be valid we will only perform
398fc5266eaSAnton Blanchard 	 * the source reference check when there is a possibility of a deadlock.
39914cf11afSPaul Mackerras 	 * Attempt to lock the address space, if we cannot we then validate the
40014cf11afSPaul Mackerras 	 * source.  If this is invalid we can skip the address space check,
40114cf11afSPaul Mackerras 	 * thus avoiding the deadlock.
40214cf11afSPaul Mackerras 	 */
403*b15021d9SBenjamin Herrenschmidt 	if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
404da929f6aSChristophe Leroy 		if (!is_user && !search_exception_tables(regs->nip))
405c3350602SBenjamin Herrenschmidt 			return bad_area_nosemaphore(regs, address);
40614cf11afSPaul Mackerras 
4079be72573SBenjamin Herrenschmidt retry:
40814cf11afSPaul Mackerras 		down_read(&mm->mmap_sem);
409a546498fSBenjamin Herrenschmidt 	} else {
410a546498fSBenjamin Herrenschmidt 		/*
411a546498fSBenjamin Herrenschmidt 		 * The above down_read_trylock() might have succeeded in
412a546498fSBenjamin Herrenschmidt 		 * which case we'll have missed the might_sleep() from
413a546498fSBenjamin Herrenschmidt 		 * down_read():
414a546498fSBenjamin Herrenschmidt 		 */
415a546498fSBenjamin Herrenschmidt 		might_sleep();
41614cf11afSPaul Mackerras 	}
41714cf11afSPaul Mackerras 
41814cf11afSPaul Mackerras 	vma = find_vma(mm, address);
419*b15021d9SBenjamin Herrenschmidt 	if (unlikely(!vma))
420c3350602SBenjamin Herrenschmidt 		return bad_area(regs, address);
421*b15021d9SBenjamin Herrenschmidt 	if (likely(vma->vm_start <= address))
42214cf11afSPaul Mackerras 		goto good_area;
423*b15021d9SBenjamin Herrenschmidt 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
424c3350602SBenjamin Herrenschmidt 		return bad_area(regs, address);
42514cf11afSPaul Mackerras 
42614cf11afSPaul Mackerras 	/*
42714cf11afSPaul Mackerras 	 * N.B. The POWER/Open ABI allows programs to access up to
42814cf11afSPaul Mackerras 	 * 288 bytes below the stack pointer.
42914cf11afSPaul Mackerras 	 * The kernel signal delivery code writes up to about 1.5kB
43014cf11afSPaul Mackerras 	 * below the stack pointer (r1) before decrementing it.
43114cf11afSPaul Mackerras 	 * The exec code can write slightly over 640kB to the stack
43214cf11afSPaul Mackerras 	 * before setting the user r1.  Thus we allow the stack to
43314cf11afSPaul Mackerras 	 * expand to 1MB without further checks.
43414cf11afSPaul Mackerras 	 */
43514cf11afSPaul Mackerras 	if (address + 0x100000 < vma->vm_end) {
43614cf11afSPaul Mackerras 		/* get user regs even if this fault is in kernel mode */
43714cf11afSPaul Mackerras 		struct pt_regs *uregs = current->thread.regs;
43814cf11afSPaul Mackerras 		if (uregs == NULL)
439c3350602SBenjamin Herrenschmidt 			return bad_area(regs, address);
44014cf11afSPaul Mackerras 
44114cf11afSPaul Mackerras 		/*
44214cf11afSPaul Mackerras 		 * A user-mode access to an address a long way below
44314cf11afSPaul Mackerras 		 * the stack pointer is only valid if the instruction
44414cf11afSPaul Mackerras 		 * is one which would update the stack pointer to the
44514cf11afSPaul Mackerras 		 * address accessed if the instruction completed,
44614cf11afSPaul Mackerras 		 * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
44714cf11afSPaul Mackerras 		 * (or the byte, halfword, float or double forms).
44814cf11afSPaul Mackerras 		 *
44914cf11afSPaul Mackerras 		 * If we don't check this then any write to the area
45014cf11afSPaul Mackerras 		 * between the last mapped region and the stack will
45114cf11afSPaul Mackerras 		 * expand the stack rather than segfaulting.
45214cf11afSPaul Mackerras 		 */
45369e044ddSAneesh Kumar K.V 		if (address + 2048 < uregs->gpr[1] && !store_update_sp)
454c3350602SBenjamin Herrenschmidt 			return bad_area(regs, address);
45514cf11afSPaul Mackerras 	}
456*b15021d9SBenjamin Herrenschmidt 	if (unlikely(expand_stack(vma, address)))
457c3350602SBenjamin Herrenschmidt 		return bad_area(regs, address);
45814cf11afSPaul Mackerras 
45914cf11afSPaul Mackerras good_area:
46014cf11afSPaul Mackerras 	if (is_exec) {
46108ae6cc1SPaul Mackerras 		/*
46208ae6cc1SPaul Mackerras 		 * Allow execution from readable areas if the MMU does not
46308ae6cc1SPaul Mackerras 		 * provide separate controls over reading and executing.
4648d30c14cSBenjamin Herrenschmidt 		 *
4658d30c14cSBenjamin Herrenschmidt 		 * Note: That code used to not be enabled for 4xx/BookE.
4668d30c14cSBenjamin Herrenschmidt 		 * It is now as I/D cache coherency for these is done at
4678d30c14cSBenjamin Herrenschmidt 		 * set_pte_at() time and I see no reason why the test
4688d30c14cSBenjamin Herrenschmidt 		 * below wouldn't be valid on those processors. This -may-
4698d30c14cSBenjamin Herrenschmidt 		 * break programs compiled with a really old ABI though.
47008ae6cc1SPaul Mackerras 		 */
471*b15021d9SBenjamin Herrenschmidt 		if (unlikely(!(vma->vm_flags & VM_EXEC) &&
47208ae6cc1SPaul Mackerras 			     (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
473*b15021d9SBenjamin Herrenschmidt 			      !(vma->vm_flags & (VM_READ | VM_WRITE)))))
474c3350602SBenjamin Herrenschmidt 			return bad_area(regs, address);
47514cf11afSPaul Mackerras 	/* a write */
47614cf11afSPaul Mackerras 	} else if (is_write) {
477*b15021d9SBenjamin Herrenschmidt 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
478c3350602SBenjamin Herrenschmidt 			return bad_area(regs, address);
479759496baSJohannes Weiner 		flags |= FAULT_FLAG_WRITE;
48014cf11afSPaul Mackerras 	/* a read */
48114cf11afSPaul Mackerras 	} else {
482*b15021d9SBenjamin Herrenschmidt 		if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
483c3350602SBenjamin Herrenschmidt 			return bad_area(regs, address);
48414cf11afSPaul Mackerras 	}
48514cf11afSPaul Mackerras 
48614cf11afSPaul Mackerras 	/*
48714cf11afSPaul Mackerras 	 * If for any reason at all we couldn't handle the fault,
48814cf11afSPaul Mackerras 	 * make sure we exit gracefully rather than endlessly redo
48914cf11afSPaul Mackerras 	 * the fault.
49014cf11afSPaul Mackerras 	 */
491dcddffd4SKirill A. Shutemov 	fault = handle_mm_fault(vma, address, flags);
49214c02e41SLaurent Dufour 
49314c02e41SLaurent Dufour 	/*
49414c02e41SLaurent Dufour 	 * Handle the retry right now, the mmap_sem has been released in that
49514c02e41SLaurent Dufour 	 * case.
49614c02e41SLaurent Dufour 	 */
49714c02e41SLaurent Dufour 	if (unlikely(fault & VM_FAULT_RETRY)) {
49814c02e41SLaurent Dufour 		/* We retry only once */
49914c02e41SLaurent Dufour 		if (flags & FAULT_FLAG_ALLOW_RETRY) {
50014c02e41SLaurent Dufour 			/*
50114c02e41SLaurent Dufour 			 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
50214c02e41SLaurent Dufour 			 * of starvation.
50314c02e41SLaurent Dufour 			 */
50414c02e41SLaurent Dufour 			flags &= ~FAULT_FLAG_ALLOW_RETRY;
50514c02e41SLaurent Dufour 			flags |= FAULT_FLAG_TRIED;
50614c02e41SLaurent Dufour 			if (!fatal_signal_pending(current))
50714c02e41SLaurent Dufour 				goto retry;
50814c02e41SLaurent Dufour 		}
509b5c8f0fdSBenjamin Herrenschmidt 
510b5c8f0fdSBenjamin Herrenschmidt 		/*
511b5c8f0fdSBenjamin Herrenschmidt 		 * User mode? Just return to handle the fatal exception otherwise
512b5c8f0fdSBenjamin Herrenschmidt 		 * return to bad_page_fault
513b5c8f0fdSBenjamin Herrenschmidt 		 */
514b5c8f0fdSBenjamin Herrenschmidt 		return is_user ? 0 : SIGBUS;
515b5c8f0fdSBenjamin Herrenschmidt 	}
516b5c8f0fdSBenjamin Herrenschmidt 
517819cdcdbSLaurent Dufour 	up_read(&current->mm->mmap_sem);
51814c02e41SLaurent Dufour 
519b5c8f0fdSBenjamin Herrenschmidt 	if (unlikely(fault & VM_FAULT_ERROR))
520b5c8f0fdSBenjamin Herrenschmidt 		return mm_fault_error(regs, address, fault);
5219be72573SBenjamin Herrenschmidt 
5229be72573SBenjamin Herrenschmidt 	/*
52314c02e41SLaurent Dufour 	 * Major/minor page fault accounting.
5249be72573SBenjamin Herrenschmidt 	 */
5259be72573SBenjamin Herrenschmidt 	if (fault & VM_FAULT_MAJOR) {
52683c54070SNick Piggin 		current->maj_flt++;
52704aafdc6SBenjamin Herrenschmidt 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
5283da02648SBenjamin Herrenschmidt 		cmo_account_page_fault();
529ac17dc8eSPeter Zijlstra 	} else {
53083c54070SNick Piggin 		current->min_flt++;
53104aafdc6SBenjamin Herrenschmidt 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
532ac17dc8eSPeter Zijlstra 	}
53365d47fd4SBenjamin Herrenschmidt 	return 0;
53414cf11afSPaul Mackerras }
5357afad422SBenjamin Herrenschmidt NOKPROBE_SYMBOL(__do_page_fault);
5367afad422SBenjamin Herrenschmidt 
5377afad422SBenjamin Herrenschmidt int do_page_fault(struct pt_regs *regs, unsigned long address,
5387afad422SBenjamin Herrenschmidt 		  unsigned long error_code)
5397afad422SBenjamin Herrenschmidt {
5407afad422SBenjamin Herrenschmidt 	enum ctx_state prev_state = exception_enter();
5417afad422SBenjamin Herrenschmidt 	int rc = __do_page_fault(regs, address, error_code);
542ba12eedeSLi Zhong 	exception_exit(prev_state);
543ba12eedeSLi Zhong 	return rc;
54414cf11afSPaul Mackerras }
54503465f89SNicholas Piggin NOKPROBE_SYMBOL(do_page_fault);
54614cf11afSPaul Mackerras 
54714cf11afSPaul Mackerras /*
54814cf11afSPaul Mackerras  * bad_page_fault is called when we have a bad access from the kernel.
54914cf11afSPaul Mackerras  * It is called from the DSI and ISI handlers in head.S and from some
55014cf11afSPaul Mackerras  * of the procedures in traps.c.
55114cf11afSPaul Mackerras  */
55214cf11afSPaul Mackerras void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
55314cf11afSPaul Mackerras {
55414cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
55514cf11afSPaul Mackerras 
55614cf11afSPaul Mackerras 	/* Are we prepared to handle this fault?  */
55714cf11afSPaul Mackerras 	if ((entry = search_exception_tables(regs->nip)) != NULL) {
55861a92f70SNicholas Piggin 		regs->nip = extable_fixup(entry);
55914cf11afSPaul Mackerras 		return;
56014cf11afSPaul Mackerras 	}
56114cf11afSPaul Mackerras 
56214cf11afSPaul Mackerras 	/* kernel has accessed a bad area */
563723925b7SOlof Johansson 
564723925b7SOlof Johansson 	switch (regs->trap) {
565723925b7SOlof Johansson 	case 0x300:
566723925b7SOlof Johansson 	case 0x380:
567a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
568a416dd8dSMichael Ellerman 			"data at address 0x%08lx\n", regs->dar);
569723925b7SOlof Johansson 		break;
570723925b7SOlof Johansson 	case 0x400:
571723925b7SOlof Johansson 	case 0x480:
572a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
573a416dd8dSMichael Ellerman 			"instruction fetch\n");
574723925b7SOlof Johansson 		break;
575eab861a7SAnton Blanchard 	case 0x600:
576eab861a7SAnton Blanchard 		printk(KERN_ALERT "Unable to handle kernel paging request for "
577eab861a7SAnton Blanchard 			"unaligned access at address 0x%08lx\n", regs->dar);
578eab861a7SAnton Blanchard 		break;
579723925b7SOlof Johansson 	default:
580a416dd8dSMichael Ellerman 		printk(KERN_ALERT "Unable to handle kernel paging request for "
581a416dd8dSMichael Ellerman 			"unknown fault\n");
582a416dd8dSMichael Ellerman 		break;
583723925b7SOlof Johansson 	}
584723925b7SOlof Johansson 	printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
585723925b7SOlof Johansson 		regs->nip);
586723925b7SOlof Johansson 
587a70857e4SAaron Tomlin 	if (task_stack_end_corrupted(current))
58828b54990SAnton Blanchard 		printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
58928b54990SAnton Blanchard 
59014cf11afSPaul Mackerras 	die("Kernel access of bad area", regs, sig);
59114cf11afSPaul Mackerras }
592