xref: /openbmc/linux/arch/arm/mm/fault.c (revision 405db98b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/arch/arm/mm/fault.c
4  *
5  *  Copyright (C) 1995  Linus Torvalds
6  *  Modifications for ARM processor (c) 1995-2004 Russell King
7  */
8 #include <linux/extable.h>
9 #include <linux/signal.h>
10 #include <linux/mm.h>
11 #include <linux/hardirq.h>
12 #include <linux/init.h>
13 #include <linux/kprobes.h>
14 #include <linux/uaccess.h>
15 #include <linux/page-flags.h>
16 #include <linux/sched/signal.h>
17 #include <linux/sched/debug.h>
18 #include <linux/highmem.h>
19 #include <linux/perf_event.h>
20 
21 #include <asm/system_misc.h>
22 #include <asm/system_info.h>
23 #include <asm/tlbflush.h>
24 
25 #include "fault.h"
26 
27 #ifdef CONFIG_MMU
28 
29 /*
30  * This is useful to dump out the page tables associated with
31  * 'addr' in mm 'mm'.
32  */
33 void show_pte(const char *lvl, struct mm_struct *mm, unsigned long addr)
34 {
35 	pgd_t *pgd;
36 
37 	if (!mm)
38 		mm = &init_mm;
39 
40 	pgd = pgd_offset(mm, addr);
41 	printk("%s[%08lx] *pgd=%08llx", lvl, addr, (long long)pgd_val(*pgd));
42 
43 	do {
44 		p4d_t *p4d;
45 		pud_t *pud;
46 		pmd_t *pmd;
47 		pte_t *pte;
48 
49 		p4d = p4d_offset(pgd, addr);
50 		if (p4d_none(*p4d))
51 			break;
52 
53 		if (p4d_bad(*p4d)) {
54 			pr_cont("(bad)");
55 			break;
56 		}
57 
58 		pud = pud_offset(p4d, addr);
59 		if (PTRS_PER_PUD != 1)
60 			pr_cont(", *pud=%08llx", (long long)pud_val(*pud));
61 
62 		if (pud_none(*pud))
63 			break;
64 
65 		if (pud_bad(*pud)) {
66 			pr_cont("(bad)");
67 			break;
68 		}
69 
70 		pmd = pmd_offset(pud, addr);
71 		if (PTRS_PER_PMD != 1)
72 			pr_cont(", *pmd=%08llx", (long long)pmd_val(*pmd));
73 
74 		if (pmd_none(*pmd))
75 			break;
76 
77 		if (pmd_bad(*pmd)) {
78 			pr_cont("(bad)");
79 			break;
80 		}
81 
82 		/* We must not map this if we have highmem enabled */
83 		if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
84 			break;
85 
86 		pte = pte_offset_map(pmd, addr);
87 		pr_cont(", *pte=%08llx", (long long)pte_val(*pte));
88 #ifndef CONFIG_ARM_LPAE
89 		pr_cont(", *ppte=%08llx",
90 		       (long long)pte_val(pte[PTE_HWTABLE_PTRS]));
91 #endif
92 		pte_unmap(pte);
93 	} while(0);
94 
95 	pr_cont("\n");
96 }
97 #else					/* CONFIG_MMU */
98 void show_pte(const char *lvl, struct mm_struct *mm, unsigned long addr)
99 { }
100 #endif					/* CONFIG_MMU */
101 
102 static void die_kernel_fault(const char *msg, struct mm_struct *mm,
103 			     unsigned long addr, unsigned int fsr,
104 			     struct pt_regs *regs)
105 {
106 	bust_spinlocks(1);
107 	pr_alert("8<--- cut here ---\n");
108 	pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
109 		 msg, addr);
110 
111 	show_pte(KERN_ALERT, mm, addr);
112 	die("Oops", regs, fsr);
113 	bust_spinlocks(0);
114 	do_exit(SIGKILL);
115 }
116 
117 /*
118  * Oops.  The kernel tried to access some page that wasn't present.
119  */
120 static void
121 __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
122 		  struct pt_regs *regs)
123 {
124 	const char *msg;
125 	/*
126 	 * Are we prepared to handle this kernel fault?
127 	 */
128 	if (fixup_exception(regs))
129 		return;
130 
131 	/*
132 	 * No handler, we'll have to terminate things with extreme prejudice.
133 	 */
134 	if (addr < PAGE_SIZE)
135 		msg = "NULL pointer dereference";
136 	else
137 		msg = "paging request";
138 
139 	die_kernel_fault(msg, mm, addr, fsr, regs);
140 }
141 
142 /*
143  * Something tried to access memory that isn't in our memory map..
144  * User mode accesses just cause a SIGSEGV
145  */
146 static void
147 __do_user_fault(unsigned long addr, unsigned int fsr, unsigned int sig,
148 		int code, struct pt_regs *regs)
149 {
150 	struct task_struct *tsk = current;
151 
152 	if (addr > TASK_SIZE)
153 		harden_branch_predictor();
154 
155 #ifdef CONFIG_DEBUG_USER
156 	if (((user_debug & UDBG_SEGV) && (sig == SIGSEGV)) ||
157 	    ((user_debug & UDBG_BUS)  && (sig == SIGBUS))) {
158 		pr_err("8<--- cut here ---\n");
159 		pr_err("%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
160 		       tsk->comm, sig, addr, fsr);
161 		show_pte(KERN_ERR, tsk->mm, addr);
162 		show_regs(regs);
163 	}
164 #endif
165 #ifndef CONFIG_KUSER_HELPERS
166 	if ((sig == SIGSEGV) && ((addr & PAGE_MASK) == 0xffff0000))
167 		printk_ratelimited(KERN_DEBUG
168 				   "%s: CONFIG_KUSER_HELPERS disabled at 0x%08lx\n",
169 				   tsk->comm, addr);
170 #endif
171 
172 	tsk->thread.address = addr;
173 	tsk->thread.error_code = fsr;
174 	tsk->thread.trap_no = 14;
175 	force_sig_fault(sig, code, (void __user *)addr);
176 }
177 
178 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
179 {
180 	struct task_struct *tsk = current;
181 	struct mm_struct *mm = tsk->active_mm;
182 
183 	/*
184 	 * If we are in kernel mode at this point, we
185 	 * have no context to handle this fault with.
186 	 */
187 	if (user_mode(regs))
188 		__do_user_fault(addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
189 	else
190 		__do_kernel_fault(mm, addr, fsr, regs);
191 }
192 
193 #ifdef CONFIG_MMU
194 #define VM_FAULT_BADMAP		0x010000
195 #define VM_FAULT_BADACCESS	0x020000
196 
197 static inline bool is_permission_fault(unsigned int fsr)
198 {
199 	int fs = fsr_fs(fsr);
200 #ifdef CONFIG_ARM_LPAE
201 	if ((fs & FS_PERM_NOLL_MASK) == FS_PERM_NOLL)
202 		return true;
203 #else
204 	if (fs == FS_L1_PERM || fs == FS_L2_PERM)
205 		return true;
206 #endif
207 	return false;
208 }
209 
210 static vm_fault_t __kprobes
211 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int flags,
212 		unsigned long vma_flags, struct pt_regs *regs)
213 {
214 	struct vm_area_struct *vma = find_vma(mm, addr);
215 	if (unlikely(!vma))
216 		return VM_FAULT_BADMAP;
217 
218 	if (unlikely(vma->vm_start > addr)) {
219 		if (!(vma->vm_flags & VM_GROWSDOWN))
220 			return VM_FAULT_BADMAP;
221 		if (addr < FIRST_USER_ADDRESS)
222 			return VM_FAULT_BADMAP;
223 		if (expand_stack(vma, addr))
224 			return VM_FAULT_BADMAP;
225 	}
226 
227 	/*
228 	 * ok, we have a good vm_area for this memory access, check the
229 	 * permissions on the VMA allow for the fault which occurred.
230 	 */
231 	if (!(vma->vm_flags & vma_flags))
232 		return VM_FAULT_BADACCESS;
233 
234 	return handle_mm_fault(vma, addr & PAGE_MASK, flags, regs);
235 }
236 
237 static int __kprobes
238 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
239 {
240 	struct mm_struct *mm = current->mm;
241 	int sig, code;
242 	vm_fault_t fault;
243 	unsigned int flags = FAULT_FLAG_DEFAULT;
244 	unsigned long vm_flags = VM_ACCESS_FLAGS;
245 
246 	if (kprobe_page_fault(regs, fsr))
247 		return 0;
248 
249 
250 	/* Enable interrupts if they were enabled in the parent context. */
251 	if (interrupts_enabled(regs))
252 		local_irq_enable();
253 
254 	/*
255 	 * If we're in an interrupt or have no user
256 	 * context, we must not take the fault..
257 	 */
258 	if (faulthandler_disabled() || !mm)
259 		goto no_context;
260 
261 	if (user_mode(regs))
262 		flags |= FAULT_FLAG_USER;
263 
264 	if ((fsr & FSR_WRITE) && !(fsr & FSR_CM)) {
265 		flags |= FAULT_FLAG_WRITE;
266 		vm_flags = VM_WRITE;
267 	}
268 
269 	if (fsr & FSR_LNX_PF) {
270 		vm_flags = VM_EXEC;
271 
272 		if (is_permission_fault(fsr) && !user_mode(regs))
273 			die_kernel_fault("execution of memory",
274 					 mm, addr, fsr, regs);
275 	}
276 
277 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
278 
279 	/*
280 	 * As per x86, we may deadlock here.  However, since the kernel only
281 	 * validly references user space from well defined areas of the code,
282 	 * we can bug out early if this is from code which shouldn't.
283 	 */
284 	if (!mmap_read_trylock(mm)) {
285 		if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
286 			goto no_context;
287 retry:
288 		mmap_read_lock(mm);
289 	} else {
290 		/*
291 		 * The above down_read_trylock() might have succeeded in
292 		 * which case, we'll have missed the might_sleep() from
293 		 * down_read()
294 		 */
295 		might_sleep();
296 #ifdef CONFIG_DEBUG_VM
297 		if (!user_mode(regs) &&
298 		    !search_exception_tables(regs->ARM_pc))
299 			goto no_context;
300 #endif
301 	}
302 
303 	fault = __do_page_fault(mm, addr, flags, vm_flags, regs);
304 
305 	/* If we need to retry but a fatal signal is pending, handle the
306 	 * signal first. We do not need to release the mmap_lock because
307 	 * it would already be released in __lock_page_or_retry in
308 	 * mm/filemap.c. */
309 	if (fault_signal_pending(fault, regs)) {
310 		if (!user_mode(regs))
311 			goto no_context;
312 		return 0;
313 	}
314 
315 	if (!(fault & VM_FAULT_ERROR) && flags & FAULT_FLAG_ALLOW_RETRY) {
316 		if (fault & VM_FAULT_RETRY) {
317 			flags |= FAULT_FLAG_TRIED;
318 			goto retry;
319 		}
320 	}
321 
322 	mmap_read_unlock(mm);
323 
324 	/*
325 	 * Handle the "normal" case first - VM_FAULT_MAJOR
326 	 */
327 	if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
328 		return 0;
329 
330 	/*
331 	 * If we are in kernel mode at this point, we
332 	 * have no context to handle this fault with.
333 	 */
334 	if (!user_mode(regs))
335 		goto no_context;
336 
337 	if (fault & VM_FAULT_OOM) {
338 		/*
339 		 * We ran out of memory, call the OOM killer, and return to
340 		 * userspace (which will retry the fault, or kill us if we
341 		 * got oom-killed)
342 		 */
343 		pagefault_out_of_memory();
344 		return 0;
345 	}
346 
347 	if (fault & VM_FAULT_SIGBUS) {
348 		/*
349 		 * We had some memory, but were unable to
350 		 * successfully fix up this page fault.
351 		 */
352 		sig = SIGBUS;
353 		code = BUS_ADRERR;
354 	} else {
355 		/*
356 		 * Something tried to access memory that
357 		 * isn't in our memory map..
358 		 */
359 		sig = SIGSEGV;
360 		code = fault == VM_FAULT_BADACCESS ?
361 			SEGV_ACCERR : SEGV_MAPERR;
362 	}
363 
364 	__do_user_fault(addr, fsr, sig, code, regs);
365 	return 0;
366 
367 no_context:
368 	__do_kernel_fault(mm, addr, fsr, regs);
369 	return 0;
370 }
371 #else					/* CONFIG_MMU */
372 static int
373 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
374 {
375 	return 0;
376 }
377 #endif					/* CONFIG_MMU */
378 
379 /*
380  * First Level Translation Fault Handler
381  *
382  * We enter here because the first level page table doesn't contain
383  * a valid entry for the address.
384  *
385  * If the address is in kernel space (>= TASK_SIZE), then we are
386  * probably faulting in the vmalloc() area.
387  *
388  * If the init_task's first level page tables contains the relevant
389  * entry, we copy the it to this task.  If not, we send the process
390  * a signal, fixup the exception, or oops the kernel.
391  *
392  * NOTE! We MUST NOT take any locks for this case. We may be in an
393  * interrupt or a critical region, and should only copy the information
394  * from the master page table, nothing more.
395  */
396 #ifdef CONFIG_MMU
397 static int __kprobes
398 do_translation_fault(unsigned long addr, unsigned int fsr,
399 		     struct pt_regs *regs)
400 {
401 	unsigned int index;
402 	pgd_t *pgd, *pgd_k;
403 	p4d_t *p4d, *p4d_k;
404 	pud_t *pud, *pud_k;
405 	pmd_t *pmd, *pmd_k;
406 
407 	if (addr < TASK_SIZE)
408 		return do_page_fault(addr, fsr, regs);
409 
410 	if (user_mode(regs))
411 		goto bad_area;
412 
413 	index = pgd_index(addr);
414 
415 	pgd = cpu_get_pgd() + index;
416 	pgd_k = init_mm.pgd + index;
417 
418 	p4d = p4d_offset(pgd, addr);
419 	p4d_k = p4d_offset(pgd_k, addr);
420 
421 	if (p4d_none(*p4d_k))
422 		goto bad_area;
423 	if (!p4d_present(*p4d))
424 		set_p4d(p4d, *p4d_k);
425 
426 	pud = pud_offset(p4d, addr);
427 	pud_k = pud_offset(p4d_k, addr);
428 
429 	if (pud_none(*pud_k))
430 		goto bad_area;
431 	if (!pud_present(*pud))
432 		set_pud(pud, *pud_k);
433 
434 	pmd = pmd_offset(pud, addr);
435 	pmd_k = pmd_offset(pud_k, addr);
436 
437 #ifdef CONFIG_ARM_LPAE
438 	/*
439 	 * Only one hardware entry per PMD with LPAE.
440 	 */
441 	index = 0;
442 #else
443 	/*
444 	 * On ARM one Linux PGD entry contains two hardware entries (see page
445 	 * tables layout in pgtable.h). We normally guarantee that we always
446 	 * fill both L1 entries. But create_mapping() doesn't follow the rule.
447 	 * It can create inidividual L1 entries, so here we have to call
448 	 * pmd_none() check for the entry really corresponded to address, not
449 	 * for the first of pair.
450 	 */
451 	index = (addr >> SECTION_SHIFT) & 1;
452 #endif
453 	if (pmd_none(pmd_k[index]))
454 		goto bad_area;
455 
456 	copy_pmd(pmd, pmd_k);
457 	return 0;
458 
459 bad_area:
460 	do_bad_area(addr, fsr, regs);
461 	return 0;
462 }
463 #else					/* CONFIG_MMU */
464 static int
465 do_translation_fault(unsigned long addr, unsigned int fsr,
466 		     struct pt_regs *regs)
467 {
468 	return 0;
469 }
470 #endif					/* CONFIG_MMU */
471 
472 /*
473  * Some section permission faults need to be handled gracefully.
474  * They can happen due to a __{get,put}_user during an oops.
475  */
476 #ifndef CONFIG_ARM_LPAE
477 static int
478 do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
479 {
480 	do_bad_area(addr, fsr, regs);
481 	return 0;
482 }
483 #endif /* CONFIG_ARM_LPAE */
484 
485 /*
486  * This abort handler always returns "fault".
487  */
488 static int
489 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
490 {
491 	return 1;
492 }
493 
494 struct fsr_info {
495 	int	(*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
496 	int	sig;
497 	int	code;
498 	const char *name;
499 };
500 
501 /* FSR definition */
502 #ifdef CONFIG_ARM_LPAE
503 #include "fsr-3level.c"
504 #else
505 #include "fsr-2level.c"
506 #endif
507 
508 void __init
509 hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
510 		int sig, int code, const char *name)
511 {
512 	if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
513 		BUG();
514 
515 	fsr_info[nr].fn   = fn;
516 	fsr_info[nr].sig  = sig;
517 	fsr_info[nr].code = code;
518 	fsr_info[nr].name = name;
519 }
520 
521 /*
522  * Dispatch a data abort to the relevant handler.
523  */
524 asmlinkage void
525 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
526 {
527 	const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
528 
529 	if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
530 		return;
531 
532 	pr_alert("8<--- cut here ---\n");
533 	pr_alert("Unhandled fault: %s (0x%03x) at 0x%08lx\n",
534 		inf->name, fsr, addr);
535 	show_pte(KERN_ALERT, current->mm, addr);
536 
537 	arm_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
538 		       fsr, 0);
539 }
540 
541 void __init
542 hook_ifault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
543 		 int sig, int code, const char *name)
544 {
545 	if (nr < 0 || nr >= ARRAY_SIZE(ifsr_info))
546 		BUG();
547 
548 	ifsr_info[nr].fn   = fn;
549 	ifsr_info[nr].sig  = sig;
550 	ifsr_info[nr].code = code;
551 	ifsr_info[nr].name = name;
552 }
553 
554 asmlinkage void
555 do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
556 {
557 	const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
558 
559 	if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
560 		return;
561 
562 	pr_alert("Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
563 		inf->name, ifsr, addr);
564 
565 	arm_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
566 		       ifsr, 0);
567 }
568 
569 /*
570  * Abort handler to be used only during first unmasking of asynchronous aborts
571  * on the boot CPU. This makes sure that the machine will not die if the
572  * firmware/bootloader left an imprecise abort pending for us to trip over.
573  */
574 static int __init early_abort_handler(unsigned long addr, unsigned int fsr,
575 				      struct pt_regs *regs)
576 {
577 	pr_warn("Hit pending asynchronous external abort (FSR=0x%08x) during "
578 		"first unmask, this is most likely caused by a "
579 		"firmware/bootloader bug.\n", fsr);
580 
581 	return 0;
582 }
583 
584 void __init early_abt_enable(void)
585 {
586 	fsr_info[FSR_FS_AEA].fn = early_abort_handler;
587 	local_abt_enable();
588 	fsr_info[FSR_FS_AEA].fn = do_bad;
589 }
590 
591 #ifndef CONFIG_ARM_LPAE
592 static int __init exceptions_init(void)
593 {
594 	if (cpu_architecture() >= CPU_ARCH_ARMv6) {
595 		hook_fault_code(4, do_translation_fault, SIGSEGV, SEGV_MAPERR,
596 				"I-cache maintenance fault");
597 	}
598 
599 	if (cpu_architecture() >= CPU_ARCH_ARMv7) {
600 		/*
601 		 * TODO: Access flag faults introduced in ARMv6K.
602 		 * Runtime check for 'K' extension is needed
603 		 */
604 		hook_fault_code(3, do_bad, SIGSEGV, SEGV_MAPERR,
605 				"section access flag fault");
606 		hook_fault_code(6, do_bad, SIGSEGV, SEGV_MAPERR,
607 				"section access flag fault");
608 	}
609 
610 	return 0;
611 }
612 
613 arch_initcall(exceptions_init);
614 #endif
615