xfs: XFS_IS_REALTIME_INODE() should be false if no rt device present
[linux/fpc-iii.git] / arch / arm64 / mm / fault.c
blob7fabf49f2aeb483c2199fb59b1607b8d65822165
1 /*
2 * Based on arch/arm/mm/fault.c
4 * Copyright (C) 1995 Linus Torvalds
5 * Copyright (C) 1995-2004 Russell King
6 * Copyright (C) 2012 ARM Ltd.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/module.h>
22 #include <linux/signal.h>
23 #include <linux/mm.h>
24 #include <linux/hardirq.h>
25 #include <linux/init.h>
26 #include <linux/kprobes.h>
27 #include <linux/uaccess.h>
28 #include <linux/page-flags.h>
29 #include <linux/sched.h>
30 #include <linux/highmem.h>
31 #include <linux/perf_event.h>
32 #include <linux/preempt.h>
34 #include <asm/bug.h>
35 #include <asm/cpufeature.h>
36 #include <asm/exception.h>
37 #include <asm/debug-monitors.h>
38 #include <asm/esr.h>
39 #include <asm/sysreg.h>
40 #include <asm/system_misc.h>
41 #include <asm/pgtable.h>
42 #include <asm/tlbflush.h>
44 static const char *fault_name(unsigned int esr);
47 * Dump out the page tables associated with 'addr' in mm 'mm'.
49 void show_pte(struct mm_struct *mm, unsigned long addr)
51 pgd_t *pgd;
53 if (!mm)
54 mm = &init_mm;
56 pr_alert("pgd = %p\n", mm->pgd);
57 pgd = pgd_offset(mm, addr);
58 pr_alert("[%08lx] *pgd=%016llx", addr, pgd_val(*pgd));
60 do {
61 pud_t *pud;
62 pmd_t *pmd;
63 pte_t *pte;
65 if (pgd_none(*pgd) || pgd_bad(*pgd))
66 break;
68 pud = pud_offset(pgd, addr);
69 pr_cont(", *pud=%016llx", pud_val(*pud));
70 if (pud_none(*pud) || pud_bad(*pud))
71 break;
73 pmd = pmd_offset(pud, addr);
74 pr_cont(", *pmd=%016llx", pmd_val(*pmd));
75 if (pmd_none(*pmd) || pmd_bad(*pmd))
76 break;
78 pte = pte_offset_map(pmd, addr);
79 pr_cont(", *pte=%016llx", pte_val(*pte));
80 pte_unmap(pte);
81 } while(0);
83 pr_cont("\n");
86 #ifdef CONFIG_ARM64_HW_AFDBM
88 * This function sets the access flags (dirty, accessed), as well as write
89 * permission, and only to a more permissive setting.
91 * It needs to cope with hardware update of the accessed/dirty state by other
92 * agents in the system and can safely skip the __sync_icache_dcache() call as,
93 * like set_pte_at(), the PTE is never changed from no-exec to exec here.
95 * Returns whether or not the PTE actually changed.
97 int ptep_set_access_flags(struct vm_area_struct *vma,
98 unsigned long address, pte_t *ptep,
99 pte_t entry, int dirty)
101 pteval_t old_pteval;
102 unsigned int tmp;
104 if (pte_same(*ptep, entry))
105 return 0;
107 /* only preserve the access flags and write permission */
108 pte_val(entry) &= PTE_AF | PTE_WRITE | PTE_DIRTY;
111 * PTE_RDONLY is cleared by default in the asm below, so set it in
112 * back if necessary (read-only or clean PTE).
114 if (!pte_write(entry) || !pte_sw_dirty(entry))
115 pte_val(entry) |= PTE_RDONLY;
118 * Setting the flags must be done atomically to avoid racing with the
119 * hardware update of the access/dirty state.
121 asm volatile("// ptep_set_access_flags\n"
122 " prfm pstl1strm, %2\n"
123 "1: ldxr %0, %2\n"
124 " and %0, %0, %3 // clear PTE_RDONLY\n"
125 " orr %0, %0, %4 // set flags\n"
126 " stxr %w1, %0, %2\n"
127 " cbnz %w1, 1b\n"
128 : "=&r" (old_pteval), "=&r" (tmp), "+Q" (pte_val(*ptep))
129 : "L" (~PTE_RDONLY), "r" (pte_val(entry)));
131 flush_tlb_fix_spurious_fault(vma, address);
132 return 1;
134 #endif
137 * The kernel tried to access some page that wasn't present.
139 static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
140 unsigned int esr, struct pt_regs *regs)
143 * Are we prepared to handle this kernel fault?
145 if (fixup_exception(regs))
146 return;
149 * No handler, we'll have to terminate things with extreme prejudice.
151 bust_spinlocks(1);
152 pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
153 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
154 "paging request", addr);
156 show_pte(mm, addr);
157 die("Oops", regs, esr);
158 bust_spinlocks(0);
159 do_exit(SIGKILL);
163 * Something tried to access memory that isn't in our memory map. User mode
164 * accesses just cause a SIGSEGV
166 static void __do_user_fault(struct task_struct *tsk, unsigned long addr,
167 unsigned int esr, unsigned int sig, int code,
168 struct pt_regs *regs)
170 struct siginfo si;
172 if (unhandled_signal(tsk, sig) && show_unhandled_signals_ratelimited()) {
173 pr_info("%s[%d]: unhandled %s (%d) at 0x%08lx, esr 0x%03x\n",
174 tsk->comm, task_pid_nr(tsk), fault_name(esr), sig,
175 addr, esr);
176 show_pte(tsk->mm, addr);
177 show_regs(regs);
180 tsk->thread.fault_address = addr;
181 tsk->thread.fault_code = esr;
182 si.si_signo = sig;
183 si.si_errno = 0;
184 si.si_code = code;
185 si.si_addr = (void __user *)addr;
186 force_sig_info(sig, &si, tsk);
189 static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *regs)
191 struct task_struct *tsk = current;
192 struct mm_struct *mm = tsk->active_mm;
195 * If we are in kernel mode at this point, we have no context to
196 * handle this fault with.
198 if (user_mode(regs))
199 __do_user_fault(tsk, addr, esr, SIGSEGV, SEGV_MAPERR, regs);
200 else
201 __do_kernel_fault(mm, addr, esr, regs);
204 #define VM_FAULT_BADMAP 0x010000
205 #define VM_FAULT_BADACCESS 0x020000
207 #define ESR_LNX_EXEC (1 << 24)
209 static int __do_page_fault(struct mm_struct *mm, unsigned long addr,
210 unsigned int mm_flags, unsigned long vm_flags,
211 struct task_struct *tsk)
213 struct vm_area_struct *vma;
214 int fault;
216 vma = find_vma(mm, addr);
217 fault = VM_FAULT_BADMAP;
218 if (unlikely(!vma))
219 goto out;
220 if (unlikely(vma->vm_start > addr))
221 goto check_stack;
224 * Ok, we have a good vm_area for this memory access, so we can handle
225 * it.
227 good_area:
229 * Check that the permissions on the VMA allow for the fault which
230 * occurred. If we encountered a write or exec fault, we must have
231 * appropriate permissions, otherwise we allow any permission.
233 if (!(vma->vm_flags & vm_flags)) {
234 fault = VM_FAULT_BADACCESS;
235 goto out;
238 return handle_mm_fault(mm, vma, addr & PAGE_MASK, mm_flags);
240 check_stack:
241 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
242 goto good_area;
243 out:
244 return fault;
247 static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
248 struct pt_regs *regs)
250 struct task_struct *tsk;
251 struct mm_struct *mm;
252 int fault, sig, code;
253 unsigned long vm_flags = VM_READ | VM_WRITE | VM_EXEC;
254 unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
256 tsk = current;
257 mm = tsk->mm;
259 /* Enable interrupts if they were enabled in the parent context. */
260 if (interrupts_enabled(regs))
261 local_irq_enable();
264 * If we're in an interrupt or have no user context, we must not take
265 * the fault.
267 if (faulthandler_disabled() || !mm)
268 goto no_context;
270 if (user_mode(regs))
271 mm_flags |= FAULT_FLAG_USER;
273 if (esr & ESR_LNX_EXEC) {
274 vm_flags = VM_EXEC;
275 } else if ((esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM)) {
276 vm_flags = VM_WRITE;
277 mm_flags |= FAULT_FLAG_WRITE;
281 * PAN bit set implies the fault happened in kernel space, but not
282 * in the arch's user access functions.
284 if (IS_ENABLED(CONFIG_ARM64_PAN) && (regs->pstate & PSR_PAN_BIT))
285 goto no_context;
288 * As per x86, we may deadlock here. However, since the kernel only
289 * validly references user space from well defined areas of the code,
290 * we can bug out early if this is from code which shouldn't.
292 if (!down_read_trylock(&mm->mmap_sem)) {
293 if (!user_mode(regs) && !search_exception_tables(regs->pc))
294 goto no_context;
295 retry:
296 down_read(&mm->mmap_sem);
297 } else {
299 * The above down_read_trylock() might have succeeded in which
300 * case, we'll have missed the might_sleep() from down_read().
302 might_sleep();
303 #ifdef CONFIG_DEBUG_VM
304 if (!user_mode(regs) && !search_exception_tables(regs->pc))
305 goto no_context;
306 #endif
309 fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk);
312 * If we need to retry but a fatal signal is pending, handle the
313 * signal first. We do not need to release the mmap_sem because it
314 * would already be released in __lock_page_or_retry in mm/filemap.c.
316 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
317 if (!user_mode(regs))
318 goto no_context;
319 return 0;
323 * Major/minor page fault accounting is only done on the initial
324 * attempt. If we go through a retry, it is extremely likely that the
325 * page will be found in page cache at that point.
328 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
329 if (mm_flags & FAULT_FLAG_ALLOW_RETRY) {
330 if (fault & VM_FAULT_MAJOR) {
331 tsk->maj_flt++;
332 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs,
333 addr);
334 } else {
335 tsk->min_flt++;
336 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs,
337 addr);
339 if (fault & VM_FAULT_RETRY) {
341 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk of
342 * starvation.
344 mm_flags &= ~FAULT_FLAG_ALLOW_RETRY;
345 mm_flags |= FAULT_FLAG_TRIED;
346 goto retry;
350 up_read(&mm->mmap_sem);
353 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
355 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP |
356 VM_FAULT_BADACCESS))))
357 return 0;
360 * If we are in kernel mode at this point, we have no context to
361 * handle this fault with.
363 if (!user_mode(regs))
364 goto no_context;
366 if (fault & VM_FAULT_OOM) {
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 got
370 * oom-killed).
372 pagefault_out_of_memory();
373 return 0;
376 if (fault & VM_FAULT_SIGBUS) {
378 * We had some memory, but were unable to successfully fix up
379 * this page fault.
381 sig = SIGBUS;
382 code = BUS_ADRERR;
383 } else {
385 * Something tried to access memory that isn't in our memory
386 * map.
388 sig = SIGSEGV;
389 code = fault == VM_FAULT_BADACCESS ?
390 SEGV_ACCERR : SEGV_MAPERR;
393 __do_user_fault(tsk, addr, esr, sig, code, regs);
394 return 0;
396 no_context:
397 __do_kernel_fault(mm, addr, esr, regs);
398 return 0;
402 * First Level Translation Fault Handler
404 * We enter here because the first level page table doesn't contain a valid
405 * entry for the address.
407 * If the address is in kernel space (>= TASK_SIZE), then we are probably
408 * faulting in the vmalloc() area.
410 * If the init_task's first level page tables contains the relevant entry, we
411 * copy the it to this task. If not, we send the process a signal, fixup the
412 * exception, or oops the kernel.
414 * NOTE! We MUST NOT take any locks for this case. We may be in an interrupt
415 * or a critical region, and should only copy the information from the master
416 * page table, nothing more.
418 static int __kprobes do_translation_fault(unsigned long addr,
419 unsigned int esr,
420 struct pt_regs *regs)
422 if (addr < TASK_SIZE)
423 return do_page_fault(addr, esr, regs);
425 do_bad_area(addr, esr, regs);
426 return 0;
430 * This abort handler always returns "fault".
432 static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs)
434 return 1;
437 static struct fault_info {
438 int (*fn)(unsigned long addr, unsigned int esr, struct pt_regs *regs);
439 int sig;
440 int code;
441 const char *name;
442 } fault_info[] = {
443 { do_bad, SIGBUS, 0, "ttbr address size fault" },
444 { do_bad, SIGBUS, 0, "level 1 address size fault" },
445 { do_bad, SIGBUS, 0, "level 2 address size fault" },
446 { do_bad, SIGBUS, 0, "level 3 address size fault" },
447 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 0 translation fault" },
448 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" },
449 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" },
450 { do_page_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" },
451 { do_bad, SIGBUS, 0, "unknown 8" },
452 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" },
453 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" },
454 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" },
455 { do_bad, SIGBUS, 0, "unknown 12" },
456 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" },
457 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
458 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
459 { do_bad, SIGBUS, 0, "synchronous external abort" },
460 { do_bad, SIGBUS, 0, "unknown 17" },
461 { do_bad, SIGBUS, 0, "unknown 18" },
462 { do_bad, SIGBUS, 0, "unknown 19" },
463 { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
464 { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
465 { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
466 { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
467 { do_bad, SIGBUS, 0, "synchronous parity error" },
468 { do_bad, SIGBUS, 0, "unknown 25" },
469 { do_bad, SIGBUS, 0, "unknown 26" },
470 { do_bad, SIGBUS, 0, "unknown 27" },
471 { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
472 { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
473 { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
474 { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
475 { do_bad, SIGBUS, 0, "unknown 32" },
476 { do_bad, SIGBUS, BUS_ADRALN, "alignment fault" },
477 { do_bad, SIGBUS, 0, "unknown 34" },
478 { do_bad, SIGBUS, 0, "unknown 35" },
479 { do_bad, SIGBUS, 0, "unknown 36" },
480 { do_bad, SIGBUS, 0, "unknown 37" },
481 { do_bad, SIGBUS, 0, "unknown 38" },
482 { do_bad, SIGBUS, 0, "unknown 39" },
483 { do_bad, SIGBUS, 0, "unknown 40" },
484 { do_bad, SIGBUS, 0, "unknown 41" },
485 { do_bad, SIGBUS, 0, "unknown 42" },
486 { do_bad, SIGBUS, 0, "unknown 43" },
487 { do_bad, SIGBUS, 0, "unknown 44" },
488 { do_bad, SIGBUS, 0, "unknown 45" },
489 { do_bad, SIGBUS, 0, "unknown 46" },
490 { do_bad, SIGBUS, 0, "unknown 47" },
491 { do_bad, SIGBUS, 0, "TLB conflict abort" },
492 { do_bad, SIGBUS, 0, "unknown 49" },
493 { do_bad, SIGBUS, 0, "unknown 50" },
494 { do_bad, SIGBUS, 0, "unknown 51" },
495 { do_bad, SIGBUS, 0, "implementation fault (lockdown abort)" },
496 { do_bad, SIGBUS, 0, "implementation fault (unsupported exclusive)" },
497 { do_bad, SIGBUS, 0, "unknown 54" },
498 { do_bad, SIGBUS, 0, "unknown 55" },
499 { do_bad, SIGBUS, 0, "unknown 56" },
500 { do_bad, SIGBUS, 0, "unknown 57" },
501 { do_bad, SIGBUS, 0, "unknown 58" },
502 { do_bad, SIGBUS, 0, "unknown 59" },
503 { do_bad, SIGBUS, 0, "unknown 60" },
504 { do_bad, SIGBUS, 0, "section domain fault" },
505 { do_bad, SIGBUS, 0, "page domain fault" },
506 { do_bad, SIGBUS, 0, "unknown 63" },
509 static const char *fault_name(unsigned int esr)
511 const struct fault_info *inf = fault_info + (esr & 63);
512 return inf->name;
516 * Dispatch a data abort to the relevant handler.
518 asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
519 struct pt_regs *regs)
521 const struct fault_info *inf = fault_info + (esr & 63);
522 struct siginfo info;
524 if (!inf->fn(addr, esr, regs))
525 return;
527 pr_alert("Unhandled fault: %s (0x%08x) at 0x%016lx\n",
528 inf->name, esr, addr);
530 info.si_signo = inf->sig;
531 info.si_errno = 0;
532 info.si_code = inf->code;
533 info.si_addr = (void __user *)addr;
534 arm64_notify_die("", regs, &info, esr);
538 * Handle stack alignment exceptions.
540 asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
541 unsigned int esr,
542 struct pt_regs *regs)
544 struct siginfo info;
545 struct task_struct *tsk = current;
547 if (show_unhandled_signals && unhandled_signal(tsk, SIGBUS))
548 pr_info_ratelimited("%s[%d]: %s exception: pc=%p sp=%p\n",
549 tsk->comm, task_pid_nr(tsk),
550 esr_get_class_string(esr), (void *)regs->pc,
551 (void *)regs->sp);
553 info.si_signo = SIGBUS;
554 info.si_errno = 0;
555 info.si_code = BUS_ADRALN;
556 info.si_addr = (void __user *)addr;
557 arm64_notify_die("Oops - SP/PC alignment exception", regs, &info, esr);
560 int __init early_brk64(unsigned long addr, unsigned int esr,
561 struct pt_regs *regs);
564 * __refdata because early_brk64 is __init, but the reference to it is
565 * clobbered at arch_initcall time.
566 * See traps.c and debug-monitors.c:debug_traps_init().
568 static struct fault_info __refdata debug_fault_info[] = {
569 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware breakpoint" },
570 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware single-step" },
571 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware watchpoint" },
572 { do_bad, SIGBUS, 0, "unknown 3" },
573 { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" },
574 { do_bad, SIGTRAP, 0, "aarch32 vector catch" },
575 { early_brk64, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" },
576 { do_bad, SIGBUS, 0, "unknown 7" },
579 void __init hook_debug_fault_code(int nr,
580 int (*fn)(unsigned long, unsigned int, struct pt_regs *),
581 int sig, int code, const char *name)
583 BUG_ON(nr < 0 || nr >= ARRAY_SIZE(debug_fault_info));
585 debug_fault_info[nr].fn = fn;
586 debug_fault_info[nr].sig = sig;
587 debug_fault_info[nr].code = code;
588 debug_fault_info[nr].name = name;
591 asmlinkage int __exception do_debug_exception(unsigned long addr,
592 unsigned int esr,
593 struct pt_regs *regs)
595 const struct fault_info *inf = debug_fault_info + DBG_ESR_EVT(esr);
596 struct siginfo info;
598 if (!inf->fn(addr, esr, regs))
599 return 1;
601 pr_alert("Unhandled debug exception: %s (0x%08x) at 0x%016lx\n",
602 inf->name, esr, addr);
604 info.si_signo = inf->sig;
605 info.si_errno = 0;
606 info.si_code = inf->code;
607 info.si_addr = (void __user *)addr;
608 arm64_notify_die("", regs, &info, 0);
610 return 0;
613 #ifdef CONFIG_ARM64_PAN
614 int cpu_enable_pan(void *__unused)
617 * We modify PSTATE. This won't work from irq context as the PSTATE
618 * is discarded once we return from the exception.
620 WARN_ON_ONCE(in_interrupt());
622 config_sctlr_el1(SCTLR_EL1_SPAN, 0);
623 asm(SET_PSTATE_PAN(1));
624 return 0;
626 #endif /* CONFIG_ARM64_PAN */