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