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