2 * linux/arch/arm/mm/fault.c
4 * Copyright (C) 1995 Linus Torvalds
5 * Modifications for ARM processor (c) 1995-2004 Russell King
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.
11 #include <linux/module.h>
12 #include <linux/signal.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.h>
20 #include <linux/highmem.h>
21 #include <linux/perf_event.h>
23 #include <asm/exception.h>
24 #include <asm/system.h>
25 #include <asm/pgtable.h>
26 #include <asm/tlbflush.h>
33 static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
37 if (!user_mode(regs)) {
38 /* kprobe_running() needs smp_processor_id() */
40 if (kprobe_running() && kprobe_fault_handler(regs, fsr))
48 static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
55 * This is useful to dump out the page tables associated with
58 void show_pte(struct mm_struct *mm, unsigned long addr)
65 printk(KERN_ALERT "pgd = %p\n", mm->pgd);
66 pgd = pgd_offset(mm, addr);
67 printk(KERN_ALERT "[%08lx] *pgd=%08llx",
68 addr, (long long)pgd_val(*pgd));
83 pud = pud_offset(pgd, addr);
84 if (PTRS_PER_PUD != 1)
85 printk(", *pud=%08llx", (long long)pud_val(*pud));
95 pmd = pmd_offset(pud, addr);
96 if (PTRS_PER_PMD != 1)
97 printk(", *pmd=%08llx", (long long)pmd_val(*pmd));
107 /* We must not map this if we have highmem enabled */
108 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
111 pte = pte_offset_map(pmd, addr);
112 printk(", *pte=%08llx", (long long)pte_val(*pte));
113 #ifndef CONFIG_ARM_LPAE
114 printk(", *ppte=%08llx",
115 (long long)pte_val(pte[PTE_HWTABLE_PTRS]));
122 #else /* CONFIG_MMU */
123 void show_pte(struct mm_struct *mm, unsigned long addr)
125 #endif /* CONFIG_MMU */
128 * Oops. The kernel tried to access some page that wasn't present.
131 __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
132 struct pt_regs *regs)
135 * Are we prepared to handle this kernel fault?
137 if (fixup_exception(regs))
141 * No handler, we'll have to terminate things with extreme prejudice.
145 "Unable to handle kernel %s at virtual address %08lx\n",
146 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
147 "paging request", addr);
150 die("Oops", regs, fsr);
155 #ifdef CONFIG_DEBUG_USER
157 print_user_faulter_location(const char *name, struct pt_regs *regs)
159 struct mm_struct *mm = current->mm;
160 struct vm_area_struct *vma;
161 char *p, *t, buf[128];
163 printk(KERN_DEBUG "%s: pc=%08lx",
169 vma = find_vma(mm, regs->ARM_pc);
170 if (!vma || !vma->vm_file)
173 p = d_path(&vma->vm_file->f_path, buf, sizeof(buf));
181 printk(KERN_CONT " (%s+%lx)", p, regs->ARM_pc - vma->vm_start);
184 printk(KERN_CONT ", lr=%08lx\n", regs->ARM_lr);
189 * Something tried to access memory that isn't in our memory map..
190 * User mode accesses just cause a SIGSEGV
193 __do_user_fault(struct task_struct *tsk, unsigned long addr,
194 unsigned int fsr, unsigned int sig, int code,
195 struct pt_regs *regs)
199 #ifdef CONFIG_DEBUG_USER
200 if ((user_debug & (UDBG_SEGV | UDBG_SEGV_SHORT))
201 && unhandled_signal(tsk, SIGSEGV) && printk_ratelimit()) {
202 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x (%s)\n",
203 tsk->comm, sig, addr, fsr,
204 (fsr & FSR_WRITE) ? "write" : "read");
205 print_user_faulter_location(tsk->comm, regs);
207 if (user_debug & UDBG_SEGV) {
208 show_pte(tsk->mm, addr);
213 tsk->thread.address = addr;
214 tsk->thread.error_code = fsr;
215 tsk->thread.trap_no = 14;
219 si.si_addr = (void __user *)addr;
220 force_sig_info(sig, &si, tsk);
223 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
225 struct task_struct *tsk = current;
226 struct mm_struct *mm = tsk->active_mm;
229 * If we are in kernel mode at this point, we
230 * have no context to handle this fault with.
233 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
235 __do_kernel_fault(mm, addr, fsr, regs);
239 #define VM_FAULT_BADMAP 0x010000
240 #define VM_FAULT_BADACCESS 0x020000
243 * Check that the permissions on the VMA allow for the fault which occurred.
244 * If we encountered a write fault, we must have write permission, otherwise
245 * we allow any permission.
247 static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
249 unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
253 if (fsr & FSR_LNX_PF)
256 return vma->vm_flags & mask ? false : true;
260 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
261 struct task_struct *tsk)
263 struct vm_area_struct *vma;
266 vma = find_vma(mm, addr);
267 fault = VM_FAULT_BADMAP;
270 if (unlikely(vma->vm_start > addr))
274 * Ok, we have a good vm_area for this
275 * memory access, so we can handle it.
278 if (access_error(fsr, vma)) {
279 fault = VM_FAULT_BADACCESS;
284 * If for any reason at all we couldn't handle the fault, make
285 * sure we exit gracefully rather than endlessly redo the fault.
287 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & FSR_WRITE) ? FAULT_FLAG_WRITE : 0);
288 if (unlikely(fault & VM_FAULT_ERROR))
290 if (fault & VM_FAULT_MAJOR)
297 /* Don't allow expansion below FIRST_USER_ADDRESS */
298 if (vma->vm_flags & VM_GROWSDOWN &&
299 addr >= FIRST_USER_ADDRESS && !expand_stack(vma, addr))
306 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
308 struct task_struct *tsk;
309 struct mm_struct *mm;
310 int fault, sig, code;
312 if (notify_page_fault(regs, fsr))
318 /* Enable interrupts if they were enabled in the parent context. */
319 if (interrupts_enabled(regs))
323 * If we're in an interrupt or have no user
324 * context, we must not take the fault..
326 if (in_atomic() || !mm)
330 * As per x86, we may deadlock here. However, since the kernel only
331 * validly references user space from well defined areas of the code,
332 * we can bug out early if this is from code which shouldn't.
334 if (!down_read_trylock(&mm->mmap_sem)) {
335 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
337 down_read(&mm->mmap_sem);
340 * The above down_read_trylock() might have succeeded in
341 * which case, we'll have missed the might_sleep() from
345 #ifdef CONFIG_DEBUG_VM
346 if (!user_mode(regs) &&
347 !search_exception_tables(regs->ARM_pc))
352 fault = __do_page_fault(mm, addr, fsr, tsk);
353 up_read(&mm->mmap_sem);
355 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
356 if (fault & VM_FAULT_MAJOR)
357 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, addr);
358 else if (fault & VM_FAULT_MINOR)
359 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, addr);
362 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
364 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
367 if (fault & VM_FAULT_OOM) {
369 * We ran out of memory, call the OOM killer, and return to
370 * userspace (which will retry the fault, or kill us if we
373 pagefault_out_of_memory();
378 * If we are in kernel mode at this point, we
379 * have no context to handle this fault with.
381 if (!user_mode(regs))
384 if (fault & VM_FAULT_SIGBUS) {
386 * We had some memory, but were unable to
387 * successfully fix up this page fault.
393 * Something tried to access memory that
394 * isn't in our memory map..
397 code = fault == VM_FAULT_BADACCESS ?
398 SEGV_ACCERR : SEGV_MAPERR;
401 __do_user_fault(tsk, addr, fsr, sig, code, regs);
405 __do_kernel_fault(mm, addr, fsr, regs);
408 #else /* CONFIG_MMU */
410 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
414 #endif /* CONFIG_MMU */
417 * First Level Translation Fault Handler
419 * We enter here because the first level page table doesn't contain
420 * a valid entry for the address.
422 * If the address is in kernel space (>= TASK_SIZE), then we are
423 * probably faulting in the vmalloc() area.
425 * If the init_task's first level page tables contains the relevant
426 * entry, we copy the it to this task. If not, we send the process
427 * a signal, fixup the exception, or oops the kernel.
429 * NOTE! We MUST NOT take any locks for this case. We may be in an
430 * interrupt or a critical region, and should only copy the information
431 * from the master page table, nothing more.
435 do_translation_fault(unsigned long addr, unsigned int fsr,
436 struct pt_regs *regs)
443 if (addr < TASK_SIZE)
444 return do_page_fault(addr, fsr, regs);
449 index = pgd_index(addr);
452 * FIXME: CP15 C1 is write only on ARMv3 architectures.
454 pgd = cpu_get_pgd() + index;
455 pgd_k = init_mm.pgd + index;
457 if (pgd_none(*pgd_k))
459 if (!pgd_present(*pgd))
460 set_pgd(pgd, *pgd_k);
462 pud = pud_offset(pgd, addr);
463 pud_k = pud_offset(pgd_k, addr);
465 if (pud_none(*pud_k))
467 if (!pud_present(*pud))
468 set_pud(pud, *pud_k);
470 pmd = pmd_offset(pud, addr);
471 pmd_k = pmd_offset(pud_k, addr);
473 #ifdef CONFIG_ARM_LPAE
475 * Only one hardware entry per PMD with LPAE.
480 * On ARM one Linux PGD entry contains two hardware entries (see page
481 * tables layout in pgtable.h). We normally guarantee that we always
482 * fill both L1 entries. But create_mapping() doesn't follow the rule.
483 * It can create inidividual L1 entries, so here we have to call
484 * pmd_none() check for the entry really corresponded to address, not
485 * for the first of pair.
487 index = (addr >> SECTION_SHIFT) & 1;
489 if (pmd_none(pmd_k[index]))
492 copy_pmd(pmd, pmd_k);
496 do_bad_area(addr, fsr, regs);
499 #else /* CONFIG_MMU */
501 do_translation_fault(unsigned long addr, unsigned int fsr,
502 struct pt_regs *regs)
506 #endif /* CONFIG_MMU */
509 * This abort handler always returns "fault".
512 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
518 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
525 #ifdef CONFIG_ARM_LPAE
526 #include "fsr-3level.c"
528 #include "fsr-2level.c"
532 hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
533 int sig, int code, const char *name)
535 if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
538 fsr_info[nr].fn = fn;
539 fsr_info[nr].sig = sig;
540 fsr_info[nr].code = code;
541 fsr_info[nr].name = name;
545 * Dispatch a data abort to the relevant handler.
547 asmlinkage void __exception
548 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
550 const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
553 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
556 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
557 inf->name, fsr, addr);
559 info.si_signo = inf->sig;
561 info.si_code = inf->code;
562 info.si_addr = (void __user *)addr;
563 arm_notify_die("", regs, &info, fsr, 0);
567 hook_ifault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
568 int sig, int code, const char *name)
570 if (nr < 0 || nr >= ARRAY_SIZE(ifsr_info))
573 ifsr_info[nr].fn = fn;
574 ifsr_info[nr].sig = sig;
575 ifsr_info[nr].code = code;
576 ifsr_info[nr].name = name;
579 asmlinkage void __exception
580 do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
582 const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
585 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
588 printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
589 inf->name, ifsr, addr);
591 info.si_signo = inf->sig;
593 info.si_code = inf->code;
594 info.si_addr = (void __user *)addr;
595 arm_notify_die("", regs, &info, ifsr, 0);
598 #ifndef CONFIG_ARM_LPAE
599 static int __init exceptions_init(void)
601 if (cpu_architecture() >= CPU_ARCH_ARMv6) {
602 hook_fault_code(4, do_translation_fault, SIGSEGV, SEGV_MAPERR,
603 "I-cache maintenance fault");
606 if (cpu_architecture() >= CPU_ARCH_ARMv7) {
608 * TODO: Access flag faults introduced in ARMv6K.
609 * Runtime check for 'K' extension is needed
611 hook_fault_code(3, do_bad, SIGSEGV, SEGV_MAPERR,
612 "section access flag fault");
613 hook_fault_code(6, do_bad, SIGSEGV, SEGV_MAPERR,
614 "section access flag fault");
620 arch_initcall(exceptions_init);