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