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