1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/arm/mm/fault.c
5 * Copyright (C) 1995 Linus Torvalds
6 * Modifications for ARM processor (c) 1995-2004 Russell King
8 #include <linux/extable.h>
9 #include <linux/signal.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>
21 #include <asm/system_misc.h>
22 #include <asm/system_info.h>
23 #include <asm/tlbflush.h>
30 * This is useful to dump out the page tables associated with
33 void show_pte(const char *lvl, struct mm_struct *mm, unsigned long addr)
40 pgd = pgd_offset(mm, addr);
41 printk("%s[%08lx] *pgd=%08llx", lvl, addr, (long long)pgd_val(*pgd));
49 p4d = p4d_offset(pgd, addr);
58 pud = pud_offset(p4d, addr);
59 if (PTRS_PER_PUD != 1)
60 pr_cont(", *pud=%08llx", (long long)pud_val(*pud));
70 pmd = pmd_offset(pud, addr);
71 if (PTRS_PER_PMD != 1)
72 pr_cont(", *pmd=%08llx", (long long)pmd_val(*pmd));
82 /* We must not map this if we have highmem enabled */
83 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
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]));
97 #else /* CONFIG_MMU */
98 void show_pte(const char *lvl, struct mm_struct *mm, unsigned long addr)
100 #endif /* CONFIG_MMU */
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)
107 pr_alert("8<--- cut here ---\n");
108 pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
111 show_pte(KERN_ALERT, mm, addr);
112 die("Oops", regs, fsr);
118 * Oops. The kernel tried to access some page that wasn't present.
121 __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
122 struct pt_regs *regs)
126 * Are we prepared to handle this kernel fault?
128 if (fixup_exception(regs))
132 * No handler, we'll have to terminate things with extreme prejudice.
134 if (addr < PAGE_SIZE)
135 msg = "NULL pointer dereference";
137 msg = "paging request";
139 die_kernel_fault(msg, mm, addr, fsr, regs);
143 * Something tried to access memory that isn't in our memory map..
144 * User mode accesses just cause a SIGSEGV
147 __do_user_fault(unsigned long addr, unsigned int fsr, unsigned int sig,
148 int code, struct pt_regs *regs)
150 struct task_struct *tsk = current;
152 if (addr > TASK_SIZE)
153 harden_branch_predictor();
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);
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",
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);
178 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
180 struct task_struct *tsk = current;
181 struct mm_struct *mm = tsk->active_mm;
184 * If we are in kernel mode at this point, we
185 * have no context to handle this fault with.
188 __do_user_fault(addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
190 __do_kernel_fault(mm, addr, fsr, regs);
194 #define VM_FAULT_BADMAP 0x010000
195 #define VM_FAULT_BADACCESS 0x020000
197 static inline bool is_permission_fault(unsigned int fsr)
199 int fs = fsr_fs(fsr);
200 #ifdef CONFIG_ARM_LPAE
201 if ((fs & FS_PERM_NOLL_MASK) == FS_PERM_NOLL)
204 if (fs == FS_L1_PERM || fs == FS_L2_PERM)
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)
214 struct vm_area_struct *vma = find_vma(mm, addr);
216 return VM_FAULT_BADMAP;
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;
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.
231 if (!(vma->vm_flags & vma_flags))
232 return VM_FAULT_BADACCESS;
234 return handle_mm_fault(vma, addr & PAGE_MASK, flags, regs);
238 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
240 struct mm_struct *mm = current->mm;
243 unsigned int flags = FAULT_FLAG_DEFAULT;
244 unsigned long vm_flags = VM_ACCESS_FLAGS;
246 if (kprobe_page_fault(regs, fsr))
250 /* Enable interrupts if they were enabled in the parent context. */
251 if (interrupts_enabled(regs))
255 * If we're in an interrupt or have no user
256 * context, we must not take the fault..
258 if (faulthandler_disabled() || !mm)
262 flags |= FAULT_FLAG_USER;
264 if ((fsr & FSR_WRITE) && !(fsr & FSR_CM)) {
265 flags |= FAULT_FLAG_WRITE;
269 if (fsr & FSR_LNX_PF) {
272 if (is_permission_fault(fsr) && !user_mode(regs))
273 die_kernel_fault("execution of memory",
274 mm, addr, fsr, regs);
277 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
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.
284 if (!mmap_read_trylock(mm)) {
285 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
291 * The above down_read_trylock() might have succeeded in
292 * which case, we'll have missed the might_sleep() from
296 #ifdef CONFIG_DEBUG_VM
297 if (!user_mode(regs) &&
298 !search_exception_tables(regs->ARM_pc))
303 fault = __do_page_fault(mm, addr, flags, vm_flags, regs);
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
309 if (fault_signal_pending(fault, regs)) {
310 if (!user_mode(regs))
315 if (!(fault & VM_FAULT_ERROR) && flags & FAULT_FLAG_ALLOW_RETRY) {
316 if (fault & VM_FAULT_RETRY) {
317 flags |= FAULT_FLAG_TRIED;
322 mmap_read_unlock(mm);
325 * Handle the "normal" case first - VM_FAULT_MAJOR
327 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
331 * If we are in kernel mode at this point, we
332 * have no context to handle this fault with.
334 if (!user_mode(regs))
337 if (fault & VM_FAULT_OOM) {
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
343 pagefault_out_of_memory();
347 if (fault & VM_FAULT_SIGBUS) {
349 * We had some memory, but were unable to
350 * successfully fix up this page fault.
356 * Something tried to access memory that
357 * isn't in our memory map..
360 code = fault == VM_FAULT_BADACCESS ?
361 SEGV_ACCERR : SEGV_MAPERR;
364 __do_user_fault(addr, fsr, sig, code, regs);
368 __do_kernel_fault(mm, addr, fsr, regs);
371 #else /* CONFIG_MMU */
373 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
377 #endif /* CONFIG_MMU */
380 * First Level Translation Fault Handler
382 * We enter here because the first level page table doesn't contain
383 * a valid entry for the address.
385 * If the address is in kernel space (>= TASK_SIZE), then we are
386 * probably faulting in the vmalloc() area.
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.
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.
398 do_translation_fault(unsigned long addr, unsigned int fsr,
399 struct pt_regs *regs)
407 if (addr < TASK_SIZE)
408 return do_page_fault(addr, fsr, regs);
413 index = pgd_index(addr);
415 pgd = cpu_get_pgd() + index;
416 pgd_k = init_mm.pgd + index;
418 p4d = p4d_offset(pgd, addr);
419 p4d_k = p4d_offset(pgd_k, addr);
421 if (p4d_none(*p4d_k))
423 if (!p4d_present(*p4d))
424 set_p4d(p4d, *p4d_k);
426 pud = pud_offset(p4d, addr);
427 pud_k = pud_offset(p4d_k, addr);
429 if (pud_none(*pud_k))
431 if (!pud_present(*pud))
432 set_pud(pud, *pud_k);
434 pmd = pmd_offset(pud, addr);
435 pmd_k = pmd_offset(pud_k, addr);
437 #ifdef CONFIG_ARM_LPAE
439 * Only one hardware entry per PMD with LPAE.
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.
451 index = (addr >> SECTION_SHIFT) & 1;
453 if (pmd_none(pmd_k[index]))
456 copy_pmd(pmd, pmd_k);
460 do_bad_area(addr, fsr, regs);
463 #else /* CONFIG_MMU */
465 do_translation_fault(unsigned long addr, unsigned int fsr,
466 struct pt_regs *regs)
470 #endif /* CONFIG_MMU */
473 * Some section permission faults need to be handled gracefully.
474 * They can happen due to a __{get,put}_user during an oops.
476 #ifndef CONFIG_ARM_LPAE
478 do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
480 do_bad_area(addr, fsr, regs);
483 #endif /* CONFIG_ARM_LPAE */
486 * This abort handler always returns "fault".
489 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
495 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
502 #ifdef CONFIG_ARM_LPAE
503 #include "fsr-3level.c"
505 #include "fsr-2level.c"
509 hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
510 int sig, int code, const char *name)
512 if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
515 fsr_info[nr].fn = fn;
516 fsr_info[nr].sig = sig;
517 fsr_info[nr].code = code;
518 fsr_info[nr].name = name;
522 * Dispatch a data abort to the relevant handler.
525 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
527 const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
529 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
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);
537 arm_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
542 hook_ifault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
543 int sig, int code, const char *name)
545 if (nr < 0 || nr >= ARRAY_SIZE(ifsr_info))
548 ifsr_info[nr].fn = fn;
549 ifsr_info[nr].sig = sig;
550 ifsr_info[nr].code = code;
551 ifsr_info[nr].name = name;
555 do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
557 const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
559 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
562 pr_alert("Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
563 inf->name, ifsr, addr);
565 arm_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
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.
574 static int __init early_abort_handler(unsigned long addr, unsigned int fsr,
575 struct pt_regs *regs)
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);
584 void __init early_abt_enable(void)
586 fsr_info[FSR_FS_AEA].fn = early_abort_handler;
588 fsr_info[FSR_FS_AEA].fn = do_bad;
591 #ifndef CONFIG_ARM_LPAE
592 static int __init exceptions_init(void)
594 if (cpu_architecture() >= CPU_ARCH_ARMv6) {
595 hook_fault_code(4, do_translation_fault, SIGSEGV, SEGV_MAPERR,
596 "I-cache maintenance fault");
599 if (cpu_architecture() >= CPU_ARCH_ARMv7) {
601 * TODO: Access flag faults introduced in ARMv6K.
602 * Runtime check for 'K' extension is needed
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");
613 arch_initcall(exceptions_init);