xref: /OK3568_Linux_fs/kernel/arch/powerpc/mm/fault.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  PowerPC version
4*4882a593Smuzhiyun  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *  Derived from "arch/i386/mm/fault.c"
7*4882a593Smuzhiyun  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *  Modified by Cort Dougan and Paul Mackerras.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *  Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/signal.h>
15*4882a593Smuzhiyun #include <linux/sched.h>
16*4882a593Smuzhiyun #include <linux/sched/task_stack.h>
17*4882a593Smuzhiyun #include <linux/kernel.h>
18*4882a593Smuzhiyun #include <linux/errno.h>
19*4882a593Smuzhiyun #include <linux/string.h>
20*4882a593Smuzhiyun #include <linux/types.h>
21*4882a593Smuzhiyun #include <linux/pagemap.h>
22*4882a593Smuzhiyun #include <linux/ptrace.h>
23*4882a593Smuzhiyun #include <linux/mman.h>
24*4882a593Smuzhiyun #include <linux/mm.h>
25*4882a593Smuzhiyun #include <linux/interrupt.h>
26*4882a593Smuzhiyun #include <linux/highmem.h>
27*4882a593Smuzhiyun #include <linux/extable.h>
28*4882a593Smuzhiyun #include <linux/kprobes.h>
29*4882a593Smuzhiyun #include <linux/kdebug.h>
30*4882a593Smuzhiyun #include <linux/perf_event.h>
31*4882a593Smuzhiyun #include <linux/ratelimit.h>
32*4882a593Smuzhiyun #include <linux/context_tracking.h>
33*4882a593Smuzhiyun #include <linux/hugetlb.h>
34*4882a593Smuzhiyun #include <linux/uaccess.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include <asm/firmware.h>
37*4882a593Smuzhiyun #include <asm/page.h>
38*4882a593Smuzhiyun #include <asm/mmu.h>
39*4882a593Smuzhiyun #include <asm/mmu_context.h>
40*4882a593Smuzhiyun #include <asm/siginfo.h>
41*4882a593Smuzhiyun #include <asm/debug.h>
42*4882a593Smuzhiyun #include <asm/kup.h>
43*4882a593Smuzhiyun #include <asm/inst.h>
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun  * do_page_fault error handling helpers
48*4882a593Smuzhiyun  */
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun static int
__bad_area_nosemaphore(struct pt_regs * regs,unsigned long address,int si_code)51*4882a593Smuzhiyun __bad_area_nosemaphore(struct pt_regs *regs, unsigned long address, int si_code)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	/*
54*4882a593Smuzhiyun 	 * If we are in kernel mode, bail out with a SEGV, this will
55*4882a593Smuzhiyun 	 * be caught by the assembly which will restore the non-volatile
56*4882a593Smuzhiyun 	 * registers before calling bad_page_fault()
57*4882a593Smuzhiyun 	 */
58*4882a593Smuzhiyun 	if (!user_mode(regs))
59*4882a593Smuzhiyun 		return SIGSEGV;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	_exception(SIGSEGV, regs, si_code, address);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	return 0;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
bad_area_nosemaphore(struct pt_regs * regs,unsigned long address)66*4882a593Smuzhiyun static noinline int bad_area_nosemaphore(struct pt_regs *regs, unsigned long address)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	return __bad_area_nosemaphore(regs, address, SEGV_MAPERR);
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
__bad_area(struct pt_regs * regs,unsigned long address,int si_code)71*4882a593Smuzhiyun static int __bad_area(struct pt_regs *regs, unsigned long address, int si_code)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	struct mm_struct *mm = current->mm;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	/*
76*4882a593Smuzhiyun 	 * Something tried to access memory that isn't in our memory map..
77*4882a593Smuzhiyun 	 * Fix it, but check if it's kernel or user first..
78*4882a593Smuzhiyun 	 */
79*4882a593Smuzhiyun 	mmap_read_unlock(mm);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	return __bad_area_nosemaphore(regs, address, si_code);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
bad_area(struct pt_regs * regs,unsigned long address)84*4882a593Smuzhiyun static noinline int bad_area(struct pt_regs *regs, unsigned long address)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	return __bad_area(regs, address, SEGV_MAPERR);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun #ifdef CONFIG_PPC_MEM_KEYS
bad_access_pkey(struct pt_regs * regs,unsigned long address,struct vm_area_struct * vma)90*4882a593Smuzhiyun static noinline int bad_access_pkey(struct pt_regs *regs, unsigned long address,
91*4882a593Smuzhiyun 				    struct vm_area_struct *vma)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	struct mm_struct *mm = current->mm;
94*4882a593Smuzhiyun 	int pkey;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	/*
97*4882a593Smuzhiyun 	 * We don't try to fetch the pkey from page table because reading
98*4882a593Smuzhiyun 	 * page table without locking doesn't guarantee stable pte value.
99*4882a593Smuzhiyun 	 * Hence the pkey value that we return to userspace can be different
100*4882a593Smuzhiyun 	 * from the pkey that actually caused access error.
101*4882a593Smuzhiyun 	 *
102*4882a593Smuzhiyun 	 * It does *not* guarantee that the VMA we find here
103*4882a593Smuzhiyun 	 * was the one that we faulted on.
104*4882a593Smuzhiyun 	 *
105*4882a593Smuzhiyun 	 * 1. T1   : mprotect_key(foo, PAGE_SIZE, pkey=4);
106*4882a593Smuzhiyun 	 * 2. T1   : set AMR to deny access to pkey=4, touches, page
107*4882a593Smuzhiyun 	 * 3. T1   : faults...
108*4882a593Smuzhiyun 	 * 4.    T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
109*4882a593Smuzhiyun 	 * 5. T1   : enters fault handler, takes mmap_lock, etc...
110*4882a593Smuzhiyun 	 * 6. T1   : reaches here, sees vma_pkey(vma)=5, when we really
111*4882a593Smuzhiyun 	 *	     faulted on a pte with its pkey=4.
112*4882a593Smuzhiyun 	 */
113*4882a593Smuzhiyun 	pkey = vma_pkey(vma);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	mmap_read_unlock(mm);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	/*
118*4882a593Smuzhiyun 	 * If we are in kernel mode, bail out with a SEGV, this will
119*4882a593Smuzhiyun 	 * be caught by the assembly which will restore the non-volatile
120*4882a593Smuzhiyun 	 * registers before calling bad_page_fault()
121*4882a593Smuzhiyun 	 */
122*4882a593Smuzhiyun 	if (!user_mode(regs))
123*4882a593Smuzhiyun 		return SIGSEGV;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	_exception_pkey(regs, address, pkey);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	return 0;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun #endif
130*4882a593Smuzhiyun 
bad_access(struct pt_regs * regs,unsigned long address)131*4882a593Smuzhiyun static noinline int bad_access(struct pt_regs *regs, unsigned long address)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	return __bad_area(regs, address, SEGV_ACCERR);
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
do_sigbus(struct pt_regs * regs,unsigned long address,vm_fault_t fault)136*4882a593Smuzhiyun static int do_sigbus(struct pt_regs *regs, unsigned long address,
137*4882a593Smuzhiyun 		     vm_fault_t fault)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	if (!user_mode(regs))
140*4882a593Smuzhiyun 		return SIGBUS;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	current->thread.trap_nr = BUS_ADRERR;
143*4882a593Smuzhiyun #ifdef CONFIG_MEMORY_FAILURE
144*4882a593Smuzhiyun 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
145*4882a593Smuzhiyun 		unsigned int lsb = 0; /* shutup gcc */
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 		pr_err("MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
148*4882a593Smuzhiyun 			current->comm, current->pid, address);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 		if (fault & VM_FAULT_HWPOISON_LARGE)
151*4882a593Smuzhiyun 			lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
152*4882a593Smuzhiyun 		if (fault & VM_FAULT_HWPOISON)
153*4882a593Smuzhiyun 			lsb = PAGE_SHIFT;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 		force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb);
156*4882a593Smuzhiyun 		return 0;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun #endif
160*4882a593Smuzhiyun 	force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
161*4882a593Smuzhiyun 	return 0;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
mm_fault_error(struct pt_regs * regs,unsigned long addr,vm_fault_t fault)164*4882a593Smuzhiyun static int mm_fault_error(struct pt_regs *regs, unsigned long addr,
165*4882a593Smuzhiyun 				vm_fault_t fault)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	/*
168*4882a593Smuzhiyun 	 * Kernel page fault interrupted by SIGKILL. We have no reason to
169*4882a593Smuzhiyun 	 * continue processing.
170*4882a593Smuzhiyun 	 */
171*4882a593Smuzhiyun 	if (fatal_signal_pending(current) && !user_mode(regs))
172*4882a593Smuzhiyun 		return SIGKILL;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	/* Out of memory */
175*4882a593Smuzhiyun 	if (fault & VM_FAULT_OOM) {
176*4882a593Smuzhiyun 		/*
177*4882a593Smuzhiyun 		 * We ran out of memory, or some other thing happened to us that
178*4882a593Smuzhiyun 		 * made us unable to handle the page fault gracefully.
179*4882a593Smuzhiyun 		 */
180*4882a593Smuzhiyun 		if (!user_mode(regs))
181*4882a593Smuzhiyun 			return SIGSEGV;
182*4882a593Smuzhiyun 		pagefault_out_of_memory();
183*4882a593Smuzhiyun 	} else {
184*4882a593Smuzhiyun 		if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
185*4882a593Smuzhiyun 			     VM_FAULT_HWPOISON_LARGE))
186*4882a593Smuzhiyun 			return do_sigbus(regs, addr, fault);
187*4882a593Smuzhiyun 		else if (fault & VM_FAULT_SIGSEGV)
188*4882a593Smuzhiyun 			return bad_area_nosemaphore(regs, addr);
189*4882a593Smuzhiyun 		else
190*4882a593Smuzhiyun 			BUG();
191*4882a593Smuzhiyun 	}
192*4882a593Smuzhiyun 	return 0;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun /* Is this a bad kernel fault ? */
bad_kernel_fault(struct pt_regs * regs,unsigned long error_code,unsigned long address,bool is_write)196*4882a593Smuzhiyun static bool bad_kernel_fault(struct pt_regs *regs, unsigned long error_code,
197*4882a593Smuzhiyun 			     unsigned long address, bool is_write)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	int is_exec = TRAP(regs) == 0x400;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	if (is_exec) {
202*4882a593Smuzhiyun 		pr_crit_ratelimited("kernel tried to execute %s page (%lx) - exploit attempt? (uid: %d)\n",
203*4882a593Smuzhiyun 				    address >= TASK_SIZE ? "exec-protected" : "user",
204*4882a593Smuzhiyun 				    address,
205*4882a593Smuzhiyun 				    from_kuid(&init_user_ns, current_uid()));
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 		// Kernel exec fault is always bad
208*4882a593Smuzhiyun 		return true;
209*4882a593Smuzhiyun 	}
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	if (!is_exec && address < TASK_SIZE && (error_code & DSISR_PROTFAULT) &&
212*4882a593Smuzhiyun 	    !search_exception_tables(regs->nip)) {
213*4882a593Smuzhiyun 		pr_crit_ratelimited("Kernel attempted to access user page (%lx) - exploit attempt? (uid: %d)\n",
214*4882a593Smuzhiyun 				    address,
215*4882a593Smuzhiyun 				    from_kuid(&init_user_ns, current_uid()));
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	// Kernel fault on kernel address is bad
219*4882a593Smuzhiyun 	if (address >= TASK_SIZE)
220*4882a593Smuzhiyun 		return true;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	// Fault on user outside of certain regions (eg. copy_tofrom_user()) is bad
223*4882a593Smuzhiyun 	if (!search_exception_tables(regs->nip))
224*4882a593Smuzhiyun 		return true;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	// Read/write fault in a valid region (the exception table search passed
227*4882a593Smuzhiyun 	// above), but blocked by KUAP is bad, it can never succeed.
228*4882a593Smuzhiyun 	if (bad_kuap_fault(regs, address, is_write))
229*4882a593Smuzhiyun 		return true;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	// What's left? Kernel fault on user in well defined regions (extable
232*4882a593Smuzhiyun 	// matched), and allowed by KUAP in the faulting context.
233*4882a593Smuzhiyun 	return false;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun #ifdef CONFIG_PPC_MEM_KEYS
access_pkey_error(bool is_write,bool is_exec,bool is_pkey,struct vm_area_struct * vma)237*4882a593Smuzhiyun static bool access_pkey_error(bool is_write, bool is_exec, bool is_pkey,
238*4882a593Smuzhiyun 			      struct vm_area_struct *vma)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	/*
241*4882a593Smuzhiyun 	 * Make sure to check the VMA so that we do not perform
242*4882a593Smuzhiyun 	 * faults just to hit a pkey fault as soon as we fill in a
243*4882a593Smuzhiyun 	 * page. Only called for current mm, hence foreign == 0
244*4882a593Smuzhiyun 	 */
245*4882a593Smuzhiyun 	if (!arch_vma_access_permitted(vma, is_write, is_exec, 0))
246*4882a593Smuzhiyun 		return true;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	return false;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun #endif
251*4882a593Smuzhiyun 
access_error(bool is_write,bool is_exec,struct vm_area_struct * vma)252*4882a593Smuzhiyun static bool access_error(bool is_write, bool is_exec, struct vm_area_struct *vma)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	/*
255*4882a593Smuzhiyun 	 * Allow execution from readable areas if the MMU does not
256*4882a593Smuzhiyun 	 * provide separate controls over reading and executing.
257*4882a593Smuzhiyun 	 *
258*4882a593Smuzhiyun 	 * Note: That code used to not be enabled for 4xx/BookE.
259*4882a593Smuzhiyun 	 * It is now as I/D cache coherency for these is done at
260*4882a593Smuzhiyun 	 * set_pte_at() time and I see no reason why the test
261*4882a593Smuzhiyun 	 * below wouldn't be valid on those processors. This -may-
262*4882a593Smuzhiyun 	 * break programs compiled with a really old ABI though.
263*4882a593Smuzhiyun 	 */
264*4882a593Smuzhiyun 	if (is_exec) {
265*4882a593Smuzhiyun 		return !(vma->vm_flags & VM_EXEC) &&
266*4882a593Smuzhiyun 			(cpu_has_feature(CPU_FTR_NOEXECUTE) ||
267*4882a593Smuzhiyun 			 !(vma->vm_flags & (VM_READ | VM_WRITE)));
268*4882a593Smuzhiyun 	}
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	if (is_write) {
271*4882a593Smuzhiyun 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
272*4882a593Smuzhiyun 			return true;
273*4882a593Smuzhiyun 		return false;
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	if (unlikely(!vma_is_accessible(vma)))
277*4882a593Smuzhiyun 		return true;
278*4882a593Smuzhiyun 	/*
279*4882a593Smuzhiyun 	 * We should ideally do the vma pkey access check here. But in the
280*4882a593Smuzhiyun 	 * fault path, handle_mm_fault() also does the same check. To avoid
281*4882a593Smuzhiyun 	 * these multiple checks, we skip it here and handle access error due
282*4882a593Smuzhiyun 	 * to pkeys later.
283*4882a593Smuzhiyun 	 */
284*4882a593Smuzhiyun 	return false;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun #ifdef CONFIG_PPC_SMLPAR
cmo_account_page_fault(void)288*4882a593Smuzhiyun static inline void cmo_account_page_fault(void)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun 	if (firmware_has_feature(FW_FEATURE_CMO)) {
291*4882a593Smuzhiyun 		u32 page_ins;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 		preempt_disable();
294*4882a593Smuzhiyun 		page_ins = be32_to_cpu(get_lppaca()->page_ins);
295*4882a593Smuzhiyun 		page_ins += 1 << PAGE_FACTOR;
296*4882a593Smuzhiyun 		get_lppaca()->page_ins = cpu_to_be32(page_ins);
297*4882a593Smuzhiyun 		preempt_enable();
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun #else
cmo_account_page_fault(void)301*4882a593Smuzhiyun static inline void cmo_account_page_fault(void) { }
302*4882a593Smuzhiyun #endif /* CONFIG_PPC_SMLPAR */
303*4882a593Smuzhiyun 
sanity_check_fault(bool is_write,bool is_user,unsigned long error_code,unsigned long address)304*4882a593Smuzhiyun static void sanity_check_fault(bool is_write, bool is_user,
305*4882a593Smuzhiyun 			       unsigned long error_code, unsigned long address)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun 	/*
308*4882a593Smuzhiyun 	 * Userspace trying to access kernel address, we get PROTFAULT for that.
309*4882a593Smuzhiyun 	 */
310*4882a593Smuzhiyun 	if (is_user && address >= TASK_SIZE) {
311*4882a593Smuzhiyun 		if ((long)address == -1)
312*4882a593Smuzhiyun 			return;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 		pr_crit_ratelimited("%s[%d]: User access of kernel address (%lx) - exploit attempt? (uid: %d)\n",
315*4882a593Smuzhiyun 				   current->comm, current->pid, address,
316*4882a593Smuzhiyun 				   from_kuid(&init_user_ns, current_uid()));
317*4882a593Smuzhiyun 		return;
318*4882a593Smuzhiyun 	}
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S))
321*4882a593Smuzhiyun 		return;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	/*
324*4882a593Smuzhiyun 	 * For hash translation mode, we should never get a
325*4882a593Smuzhiyun 	 * PROTFAULT. Any update to pte to reduce access will result in us
326*4882a593Smuzhiyun 	 * removing the hash page table entry, thus resulting in a DSISR_NOHPTE
327*4882a593Smuzhiyun 	 * fault instead of DSISR_PROTFAULT.
328*4882a593Smuzhiyun 	 *
329*4882a593Smuzhiyun 	 * A pte update to relax the access will not result in a hash page table
330*4882a593Smuzhiyun 	 * entry invalidate and hence can result in DSISR_PROTFAULT.
331*4882a593Smuzhiyun 	 * ptep_set_access_flags() doesn't do a hpte flush. This is why we have
332*4882a593Smuzhiyun 	 * the special !is_write in the below conditional.
333*4882a593Smuzhiyun 	 *
334*4882a593Smuzhiyun 	 * For platforms that doesn't supports coherent icache and do support
335*4882a593Smuzhiyun 	 * per page noexec bit, we do setup things such that we do the
336*4882a593Smuzhiyun 	 * sync between D/I cache via fault. But that is handled via low level
337*4882a593Smuzhiyun 	 * hash fault code (hash_page_do_lazy_icache()) and we should not reach
338*4882a593Smuzhiyun 	 * here in such case.
339*4882a593Smuzhiyun 	 *
340*4882a593Smuzhiyun 	 * For wrong access that can result in PROTFAULT, the above vma->vm_flags
341*4882a593Smuzhiyun 	 * check should handle those and hence we should fall to the bad_area
342*4882a593Smuzhiyun 	 * handling correctly.
343*4882a593Smuzhiyun 	 *
344*4882a593Smuzhiyun 	 * For embedded with per page exec support that doesn't support coherent
345*4882a593Smuzhiyun 	 * icache we do get PROTFAULT and we handle that D/I cache sync in
346*4882a593Smuzhiyun 	 * set_pte_at while taking the noexec/prot fault. Hence this is WARN_ON
347*4882a593Smuzhiyun 	 * is conditional for server MMU.
348*4882a593Smuzhiyun 	 *
349*4882a593Smuzhiyun 	 * For radix, we can get prot fault for autonuma case, because radix
350*4882a593Smuzhiyun 	 * page table will have them marked noaccess for user.
351*4882a593Smuzhiyun 	 */
352*4882a593Smuzhiyun 	if (radix_enabled() || is_write)
353*4882a593Smuzhiyun 		return;
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun /*
359*4882a593Smuzhiyun  * Define the correct "is_write" bit in error_code based
360*4882a593Smuzhiyun  * on the processor family
361*4882a593Smuzhiyun  */
362*4882a593Smuzhiyun #if (defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
363*4882a593Smuzhiyun #define page_fault_is_write(__err)	((__err) & ESR_DST)
364*4882a593Smuzhiyun #define page_fault_is_bad(__err)	(0)
365*4882a593Smuzhiyun #else
366*4882a593Smuzhiyun #define page_fault_is_write(__err)	((__err) & DSISR_ISSTORE)
367*4882a593Smuzhiyun #if defined(CONFIG_PPC_8xx)
368*4882a593Smuzhiyun #define page_fault_is_bad(__err)	((__err) & DSISR_NOEXEC_OR_G)
369*4882a593Smuzhiyun #elif defined(CONFIG_PPC64)
370*4882a593Smuzhiyun #define page_fault_is_bad(__err)	((__err) & DSISR_BAD_FAULT_64S)
371*4882a593Smuzhiyun #else
372*4882a593Smuzhiyun #define page_fault_is_bad(__err)	((__err) & DSISR_BAD_FAULT_32S)
373*4882a593Smuzhiyun #endif
374*4882a593Smuzhiyun #endif
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun /*
377*4882a593Smuzhiyun  * For 600- and 800-family processors, the error_code parameter is DSISR
378*4882a593Smuzhiyun  * for a data fault, SRR1 for an instruction fault. For 400-family processors
379*4882a593Smuzhiyun  * the error_code parameter is ESR for a data fault, 0 for an instruction
380*4882a593Smuzhiyun  * fault.
381*4882a593Smuzhiyun  * For 64-bit processors, the error_code parameter is
382*4882a593Smuzhiyun  *  - DSISR for a non-SLB data access fault,
383*4882a593Smuzhiyun  *  - SRR1 & 0x08000000 for a non-SLB instruction access fault
384*4882a593Smuzhiyun  *  - 0 any SLB fault.
385*4882a593Smuzhiyun  *
386*4882a593Smuzhiyun  * The return value is 0 if the fault was handled, or the signal
387*4882a593Smuzhiyun  * number if this is a kernel fault that can't be handled here.
388*4882a593Smuzhiyun  */
__do_page_fault(struct pt_regs * regs,unsigned long address,unsigned long error_code)389*4882a593Smuzhiyun static int __do_page_fault(struct pt_regs *regs, unsigned long address,
390*4882a593Smuzhiyun 			   unsigned long error_code)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	struct vm_area_struct * vma;
393*4882a593Smuzhiyun 	struct mm_struct *mm = current->mm;
394*4882a593Smuzhiyun 	unsigned int flags = FAULT_FLAG_DEFAULT;
395*4882a593Smuzhiyun  	int is_exec = TRAP(regs) == 0x400;
396*4882a593Smuzhiyun 	int is_user = user_mode(regs);
397*4882a593Smuzhiyun 	int is_write = page_fault_is_write(error_code);
398*4882a593Smuzhiyun 	vm_fault_t fault, major = 0;
399*4882a593Smuzhiyun 	bool kprobe_fault = kprobe_page_fault(regs, 11);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	if (unlikely(debugger_fault_handler(regs) || kprobe_fault))
402*4882a593Smuzhiyun 		return 0;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	if (unlikely(page_fault_is_bad(error_code))) {
405*4882a593Smuzhiyun 		if (is_user) {
406*4882a593Smuzhiyun 			_exception(SIGBUS, regs, BUS_OBJERR, address);
407*4882a593Smuzhiyun 			return 0;
408*4882a593Smuzhiyun 		}
409*4882a593Smuzhiyun 		return SIGBUS;
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/* Additional sanity check(s) */
413*4882a593Smuzhiyun 	sanity_check_fault(is_write, is_user, error_code, address);
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	/*
416*4882a593Smuzhiyun 	 * The kernel should never take an execute fault nor should it
417*4882a593Smuzhiyun 	 * take a page fault to a kernel address or a page fault to a user
418*4882a593Smuzhiyun 	 * address outside of dedicated places
419*4882a593Smuzhiyun 	 */
420*4882a593Smuzhiyun 	if (unlikely(!is_user && bad_kernel_fault(regs, error_code, address, is_write)))
421*4882a593Smuzhiyun 		return SIGSEGV;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	/*
424*4882a593Smuzhiyun 	 * If we're in an interrupt, have no user context or are running
425*4882a593Smuzhiyun 	 * in a region with pagefaults disabled then we must not take the fault
426*4882a593Smuzhiyun 	 */
427*4882a593Smuzhiyun 	if (unlikely(faulthandler_disabled() || !mm)) {
428*4882a593Smuzhiyun 		if (is_user)
429*4882a593Smuzhiyun 			printk_ratelimited(KERN_ERR "Page fault in user mode"
430*4882a593Smuzhiyun 					   " with faulthandler_disabled()=%d"
431*4882a593Smuzhiyun 					   " mm=%p\n",
432*4882a593Smuzhiyun 					   faulthandler_disabled(), mm);
433*4882a593Smuzhiyun 		return bad_area_nosemaphore(regs, address);
434*4882a593Smuzhiyun 	}
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	/* We restore the interrupt state now */
437*4882a593Smuzhiyun 	if (!arch_irq_disabled_regs(regs))
438*4882a593Smuzhiyun 		local_irq_enable();
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	/*
443*4882a593Smuzhiyun 	 * We want to do this outside mmap_lock, because reading code around nip
444*4882a593Smuzhiyun 	 * can result in fault, which will cause a deadlock when called with
445*4882a593Smuzhiyun 	 * mmap_lock held
446*4882a593Smuzhiyun 	 */
447*4882a593Smuzhiyun 	if (is_user)
448*4882a593Smuzhiyun 		flags |= FAULT_FLAG_USER;
449*4882a593Smuzhiyun 	if (is_write)
450*4882a593Smuzhiyun 		flags |= FAULT_FLAG_WRITE;
451*4882a593Smuzhiyun 	if (is_exec)
452*4882a593Smuzhiyun 		flags |= FAULT_FLAG_INSTRUCTION;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	/* When running in the kernel we expect faults to occur only to
455*4882a593Smuzhiyun 	 * addresses in user space.  All other faults represent errors in the
456*4882a593Smuzhiyun 	 * kernel and should generate an OOPS.  Unfortunately, in the case of an
457*4882a593Smuzhiyun 	 * erroneous fault occurring in a code path which already holds mmap_lock
458*4882a593Smuzhiyun 	 * we will deadlock attempting to validate the fault against the
459*4882a593Smuzhiyun 	 * address space.  Luckily the kernel only validly references user
460*4882a593Smuzhiyun 	 * space from well defined areas of code, which are listed in the
461*4882a593Smuzhiyun 	 * exceptions table.
462*4882a593Smuzhiyun 	 *
463*4882a593Smuzhiyun 	 * As the vast majority of faults will be valid we will only perform
464*4882a593Smuzhiyun 	 * the source reference check when there is a possibility of a deadlock.
465*4882a593Smuzhiyun 	 * Attempt to lock the address space, if we cannot we then validate the
466*4882a593Smuzhiyun 	 * source.  If this is invalid we can skip the address space check,
467*4882a593Smuzhiyun 	 * thus avoiding the deadlock.
468*4882a593Smuzhiyun 	 */
469*4882a593Smuzhiyun 	if (unlikely(!mmap_read_trylock(mm))) {
470*4882a593Smuzhiyun 		if (!is_user && !search_exception_tables(regs->nip))
471*4882a593Smuzhiyun 			return bad_area_nosemaphore(regs, address);
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun retry:
474*4882a593Smuzhiyun 		mmap_read_lock(mm);
475*4882a593Smuzhiyun 	} else {
476*4882a593Smuzhiyun 		/*
477*4882a593Smuzhiyun 		 * The above down_read_trylock() might have succeeded in
478*4882a593Smuzhiyun 		 * which case we'll have missed the might_sleep() from
479*4882a593Smuzhiyun 		 * down_read():
480*4882a593Smuzhiyun 		 */
481*4882a593Smuzhiyun 		might_sleep();
482*4882a593Smuzhiyun 	}
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	vma = find_vma(mm, address);
485*4882a593Smuzhiyun 	if (unlikely(!vma))
486*4882a593Smuzhiyun 		return bad_area(regs, address);
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	if (unlikely(vma->vm_start > address)) {
489*4882a593Smuzhiyun 		if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
490*4882a593Smuzhiyun 			return bad_area(regs, address);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 		if (unlikely(expand_stack(vma, address)))
493*4882a593Smuzhiyun 			return bad_area(regs, address);
494*4882a593Smuzhiyun 	}
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun #ifdef CONFIG_PPC_MEM_KEYS
497*4882a593Smuzhiyun 	if (unlikely(access_pkey_error(is_write, is_exec,
498*4882a593Smuzhiyun 				       (error_code & DSISR_KEYFAULT), vma)))
499*4882a593Smuzhiyun 		return bad_access_pkey(regs, address, vma);
500*4882a593Smuzhiyun #endif /* CONFIG_PPC_MEM_KEYS */
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	if (unlikely(access_error(is_write, is_exec, vma)))
503*4882a593Smuzhiyun 		return bad_access(regs, address);
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	/*
506*4882a593Smuzhiyun 	 * If for any reason at all we couldn't handle the fault,
507*4882a593Smuzhiyun 	 * make sure we exit gracefully rather than endlessly redo
508*4882a593Smuzhiyun 	 * the fault.
509*4882a593Smuzhiyun 	 */
510*4882a593Smuzhiyun 	fault = handle_mm_fault(vma, address, flags, regs);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	major |= fault & VM_FAULT_MAJOR;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	if (fault_signal_pending(fault, regs))
515*4882a593Smuzhiyun 		return user_mode(regs) ? 0 : SIGBUS;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	/*
518*4882a593Smuzhiyun 	 * Handle the retry right now, the mmap_lock has been released in that
519*4882a593Smuzhiyun 	 * case.
520*4882a593Smuzhiyun 	 */
521*4882a593Smuzhiyun 	if (unlikely(fault & VM_FAULT_RETRY)) {
522*4882a593Smuzhiyun 		if (flags & FAULT_FLAG_ALLOW_RETRY) {
523*4882a593Smuzhiyun 			flags |= FAULT_FLAG_TRIED;
524*4882a593Smuzhiyun 			goto retry;
525*4882a593Smuzhiyun 		}
526*4882a593Smuzhiyun 	}
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	mmap_read_unlock(current->mm);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	if (unlikely(fault & VM_FAULT_ERROR))
531*4882a593Smuzhiyun 		return mm_fault_error(regs, address, fault);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	/*
534*4882a593Smuzhiyun 	 * Major/minor page fault accounting.
535*4882a593Smuzhiyun 	 */
536*4882a593Smuzhiyun 	if (major)
537*4882a593Smuzhiyun 		cmo_account_page_fault();
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	return 0;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun NOKPROBE_SYMBOL(__do_page_fault);
542*4882a593Smuzhiyun 
do_page_fault(struct pt_regs * regs,unsigned long address,unsigned long error_code)543*4882a593Smuzhiyun int do_page_fault(struct pt_regs *regs, unsigned long address,
544*4882a593Smuzhiyun 		  unsigned long error_code)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	enum ctx_state prev_state = exception_enter();
547*4882a593Smuzhiyun 	int rc = __do_page_fault(regs, address, error_code);
548*4882a593Smuzhiyun 	exception_exit(prev_state);
549*4882a593Smuzhiyun 	return rc;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun NOKPROBE_SYMBOL(do_page_fault);
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun /*
554*4882a593Smuzhiyun  * bad_page_fault is called when we have a bad access from the kernel.
555*4882a593Smuzhiyun  * It is called from the DSI and ISI handlers in head.S and from some
556*4882a593Smuzhiyun  * of the procedures in traps.c.
557*4882a593Smuzhiyun  */
bad_page_fault(struct pt_regs * regs,unsigned long address,int sig)558*4882a593Smuzhiyun void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun 	const struct exception_table_entry *entry;
561*4882a593Smuzhiyun 	int is_write = page_fault_is_write(regs->dsisr);
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	/* Are we prepared to handle this fault?  */
564*4882a593Smuzhiyun 	if ((entry = search_exception_tables(regs->nip)) != NULL) {
565*4882a593Smuzhiyun 		regs->nip = extable_fixup(entry);
566*4882a593Smuzhiyun 		return;
567*4882a593Smuzhiyun 	}
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	/* kernel has accessed a bad area */
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	switch (TRAP(regs)) {
572*4882a593Smuzhiyun 	case 0x300:
573*4882a593Smuzhiyun 	case 0x380:
574*4882a593Smuzhiyun 	case 0xe00:
575*4882a593Smuzhiyun 		pr_alert("BUG: %s on %s at 0x%08lx\n",
576*4882a593Smuzhiyun 			 regs->dar < PAGE_SIZE ? "Kernel NULL pointer dereference" :
577*4882a593Smuzhiyun 			 "Unable to handle kernel data access",
578*4882a593Smuzhiyun 			 is_write ? "write" : "read", regs->dar);
579*4882a593Smuzhiyun 		break;
580*4882a593Smuzhiyun 	case 0x400:
581*4882a593Smuzhiyun 	case 0x480:
582*4882a593Smuzhiyun 		pr_alert("BUG: Unable to handle kernel instruction fetch%s",
583*4882a593Smuzhiyun 			 regs->nip < PAGE_SIZE ? " (NULL pointer?)\n" : "\n");
584*4882a593Smuzhiyun 		break;
585*4882a593Smuzhiyun 	case 0x600:
586*4882a593Smuzhiyun 		pr_alert("BUG: Unable to handle kernel unaligned access at 0x%08lx\n",
587*4882a593Smuzhiyun 			 regs->dar);
588*4882a593Smuzhiyun 		break;
589*4882a593Smuzhiyun 	default:
590*4882a593Smuzhiyun 		pr_alert("BUG: Unable to handle unknown paging fault at 0x%08lx\n",
591*4882a593Smuzhiyun 			 regs->dar);
592*4882a593Smuzhiyun 		break;
593*4882a593Smuzhiyun 	}
594*4882a593Smuzhiyun 	printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
595*4882a593Smuzhiyun 		regs->nip);
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	if (task_stack_end_corrupted(current))
598*4882a593Smuzhiyun 		printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	die("Kernel access of bad area", regs, sig);
601*4882a593Smuzhiyun }
602