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>
22 #include <asm/system.h>
23 #include <asm/pgtable.h>
24 #include <asm/tlbflush.h>
29 * Fault status register encodings. We steal bit 31 for our own purposes.
31 #define FSR_LNX_PF (1 << 31)
32 #define FSR_WRITE (1 << 11)
33 #define FSR_FS4 (1 << 10)
34 #define FSR_FS3_0 (15)
36 static inline int fsr_fs(unsigned int fsr)
38 return (fsr & FSR_FS3_0) | (fsr & FSR_FS4) >> 6;
44 static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
48 if (!user_mode(regs)) {
49 /* kprobe_running() needs smp_processor_id() */
51 if (kprobe_running() && kprobe_fault_handler(regs, fsr))
59 static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
66 * This is useful to dump out the page tables associated with
69 void show_pte(struct mm_struct *mm, unsigned long addr)
76 printk(KERN_ALERT "pgd = %p\n", mm->pgd);
77 pgd = pgd_offset(mm, addr);
78 printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
92 pmd = pmd_offset(pgd, addr);
93 if (PTRS_PER_PMD != 1)
94 printk(", *pmd=%08lx", pmd_val(*pmd));
104 /* We must not map this if we have highmem enabled */
105 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
108 pte = pte_offset_map(pmd, addr);
109 printk(", *pte=%08lx", pte_val(*pte));
110 printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE]));
116 #else /* CONFIG_MMU */
117 void show_pte(struct mm_struct *mm, unsigned long addr)
119 #endif /* CONFIG_MMU */
122 * Oops. The kernel tried to access some page that wasn't present.
125 __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
126 struct pt_regs *regs)
129 * Are we prepared to handle this kernel fault?
131 if (fixup_exception(regs))
135 * No handler, we'll have to terminate things with extreme prejudice.
139 "Unable to handle kernel %s at virtual address %08lx\n",
140 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
141 "paging request", addr);
144 die("Oops", regs, fsr);
150 * Something tried to access memory that isn't in our memory map..
151 * User mode accesses just cause a SIGSEGV
154 __do_user_fault(struct task_struct *tsk, unsigned long addr,
155 unsigned int fsr, unsigned int sig, int code,
156 struct pt_regs *regs)
160 #ifdef CONFIG_DEBUG_USER
161 if (user_debug & UDBG_SEGV) {
162 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
163 tsk->comm, sig, addr, fsr);
164 show_pte(tsk->mm, addr);
169 tsk->thread.address = addr;
170 tsk->thread.error_code = fsr;
171 tsk->thread.trap_no = 14;
175 si.si_addr = (void __user *)addr;
176 force_sig_info(sig, &si, tsk);
179 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
181 struct task_struct *tsk = current;
182 struct mm_struct *mm = tsk->active_mm;
185 * If we are in kernel mode at this point, we
186 * have no context to handle this fault with.
189 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
191 __do_kernel_fault(mm, addr, fsr, regs);
195 #define VM_FAULT_BADMAP 0x010000
196 #define VM_FAULT_BADACCESS 0x020000
199 * Check that the permissions on the VMA allow for the fault which occurred.
200 * If we encountered a write fault, we must have write permission, otherwise
201 * we allow any permission.
203 static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
205 unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
209 if (fsr & FSR_LNX_PF)
212 return vma->vm_flags & mask ? false : true;
216 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
217 struct task_struct *tsk)
219 struct vm_area_struct *vma;
222 vma = find_vma(mm, addr);
223 fault = VM_FAULT_BADMAP;
226 if (unlikely(vma->vm_start > addr))
230 * Ok, we have a good vm_area for this
231 * memory access, so we can handle it.
234 if (access_error(fsr, vma)) {
235 fault = VM_FAULT_BADACCESS;
240 * If for any reason at all we couldn't handle the fault, make
241 * sure we exit gracefully rather than endlessly redo the fault.
243 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & FSR_WRITE) ? FAULT_FLAG_WRITE : 0);
244 if (unlikely(fault & VM_FAULT_ERROR))
246 if (fault & VM_FAULT_MAJOR)
253 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
260 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
262 struct task_struct *tsk;
263 struct mm_struct *mm;
264 int fault, sig, code;
266 if (notify_page_fault(regs, fsr))
273 * If we're in an interrupt or have no user
274 * context, we must not take the fault..
276 if (in_atomic() || !mm)
280 * As per x86, we may deadlock here. However, since the kernel only
281 * validly references user space from well defined areas of the code,
282 * we can bug out early if this is from code which shouldn't.
284 if (!down_read_trylock(&mm->mmap_sem)) {
285 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
287 down_read(&mm->mmap_sem);
290 * The above down_read_trylock() might have succeeded in
291 * which case, we'll have missed the might_sleep() from
297 fault = __do_page_fault(mm, addr, fsr, tsk);
298 up_read(&mm->mmap_sem);
301 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
303 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
306 if (fault & VM_FAULT_OOM) {
308 * We ran out of memory, call the OOM killer, and return to
309 * userspace (which will retry the fault, or kill us if we
312 pagefault_out_of_memory();
317 * If we are in kernel mode at this point, we
318 * have no context to handle this fault with.
320 if (!user_mode(regs))
323 if (fault & VM_FAULT_SIGBUS) {
325 * We had some memory, but were unable to
326 * successfully fix up this page fault.
332 * Something tried to access memory that
333 * isn't in our memory map..
336 code = fault == VM_FAULT_BADACCESS ?
337 SEGV_ACCERR : SEGV_MAPERR;
340 __do_user_fault(tsk, addr, fsr, sig, code, regs);
344 __do_kernel_fault(mm, addr, fsr, regs);
347 #else /* CONFIG_MMU */
349 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
353 #endif /* CONFIG_MMU */
356 * First Level Translation Fault Handler
358 * We enter here because the first level page table doesn't contain
359 * a valid entry for the address.
361 * If the address is in kernel space (>= TASK_SIZE), then we are
362 * probably faulting in the vmalloc() area.
364 * If the init_task's first level page tables contains the relevant
365 * entry, we copy the it to this task. If not, we send the process
366 * a signal, fixup the exception, or oops the kernel.
368 * NOTE! We MUST NOT take any locks for this case. We may be in an
369 * interrupt or a critical region, and should only copy the information
370 * from the master page table, nothing more.
374 do_translation_fault(unsigned long addr, unsigned int fsr,
375 struct pt_regs *regs)
381 if (addr < TASK_SIZE)
382 return do_page_fault(addr, fsr, regs);
384 index = pgd_index(addr);
387 * FIXME: CP15 C1 is write only on ARMv3 architectures.
389 pgd = cpu_get_pgd() + index;
390 pgd_k = init_mm.pgd + index;
392 if (pgd_none(*pgd_k))
395 if (!pgd_present(*pgd))
396 set_pgd(pgd, *pgd_k);
398 pmd_k = pmd_offset(pgd_k, addr);
399 pmd = pmd_offset(pgd, addr);
401 if (pmd_none(*pmd_k))
404 copy_pmd(pmd, pmd_k);
408 do_bad_area(addr, fsr, regs);
411 #else /* CONFIG_MMU */
413 do_translation_fault(unsigned long addr, unsigned int fsr,
414 struct pt_regs *regs)
418 #endif /* CONFIG_MMU */
421 * Some section permission faults need to be handled gracefully.
422 * They can happen due to a __{get,put}_user during an oops.
425 do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
427 do_bad_area(addr, fsr, regs);
432 * This abort handler always returns "fault".
435 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
440 static struct fsr_info {
441 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
447 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
448 * defines these to be "precise" aborts.
450 { do_bad, SIGSEGV, 0, "vector exception" },
451 { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
452 { do_bad, SIGKILL, 0, "terminal exception" },
453 { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
454 { do_bad, SIGBUS, 0, "external abort on linefetch" },
455 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
456 { do_bad, SIGBUS, 0, "external abort on linefetch" },
457 { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
458 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
459 { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
460 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
461 { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
462 { do_bad, SIGBUS, 0, "external abort on translation" },
463 { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
464 { do_bad, SIGBUS, 0, "external abort on translation" },
465 { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
467 * The following are "imprecise" aborts, which are signalled by bit
468 * 10 of the FSR, and may not be recoverable. These are only
469 * supported if the CPU abort handler supports bit 10.
471 { do_bad, SIGBUS, 0, "unknown 16" },
472 { do_bad, SIGBUS, 0, "unknown 17" },
473 { do_bad, SIGBUS, 0, "unknown 18" },
474 { do_bad, SIGBUS, 0, "unknown 19" },
475 { do_bad, SIGBUS, 0, "lock abort" }, /* xscale */
476 { do_bad, SIGBUS, 0, "unknown 21" },
477 { do_bad, SIGBUS, BUS_OBJERR, "imprecise external abort" }, /* xscale */
478 { do_bad, SIGBUS, 0, "unknown 23" },
479 { do_bad, SIGBUS, 0, "dcache parity error" }, /* xscale */
480 { do_bad, SIGBUS, 0, "unknown 25" },
481 { do_bad, SIGBUS, 0, "unknown 26" },
482 { do_bad, SIGBUS, 0, "unknown 27" },
483 { do_bad, SIGBUS, 0, "unknown 28" },
484 { do_bad, SIGBUS, 0, "unknown 29" },
485 { do_bad, SIGBUS, 0, "unknown 30" },
486 { do_bad, SIGBUS, 0, "unknown 31" }
490 hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
491 int sig, const char *name)
493 if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) {
494 fsr_info[nr].fn = fn;
495 fsr_info[nr].sig = sig;
496 fsr_info[nr].name = name;
501 * Dispatch a data abort to the relevant handler.
503 asmlinkage void __exception
504 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
506 const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
509 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
512 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
513 inf->name, fsr, addr);
515 info.si_signo = inf->sig;
517 info.si_code = inf->code;
518 info.si_addr = (void __user *)addr;
519 arm_notify_die("", regs, &info, fsr, 0);
523 static struct fsr_info ifsr_info[] = {
524 { do_bad, SIGBUS, 0, "unknown 0" },
525 { do_bad, SIGBUS, 0, "unknown 1" },
526 { do_bad, SIGBUS, 0, "debug event" },
527 { do_bad, SIGSEGV, SEGV_ACCERR, "section access flag fault" },
528 { do_bad, SIGBUS, 0, "unknown 4" },
529 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
530 { do_bad, SIGSEGV, SEGV_ACCERR, "page access flag fault" },
531 { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
532 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
533 { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
534 { do_bad, SIGBUS, 0, "unknown 10" },
535 { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
536 { do_bad, SIGBUS, 0, "external abort on translation" },
537 { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
538 { do_bad, SIGBUS, 0, "external abort on translation" },
539 { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
540 { do_bad, SIGBUS, 0, "unknown 16" },
541 { do_bad, SIGBUS, 0, "unknown 17" },
542 { do_bad, SIGBUS, 0, "unknown 18" },
543 { do_bad, SIGBUS, 0, "unknown 19" },
544 { do_bad, SIGBUS, 0, "unknown 20" },
545 { do_bad, SIGBUS, 0, "unknown 21" },
546 { do_bad, SIGBUS, 0, "unknown 22" },
547 { do_bad, SIGBUS, 0, "unknown 23" },
548 { do_bad, SIGBUS, 0, "unknown 24" },
549 { do_bad, SIGBUS, 0, "unknown 25" },
550 { do_bad, SIGBUS, 0, "unknown 26" },
551 { do_bad, SIGBUS, 0, "unknown 27" },
552 { do_bad, SIGBUS, 0, "unknown 28" },
553 { do_bad, SIGBUS, 0, "unknown 29" },
554 { do_bad, SIGBUS, 0, "unknown 30" },
555 { do_bad, SIGBUS, 0, "unknown 31" },
558 asmlinkage void __exception
559 do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
561 const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
564 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
567 printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
568 inf->name, ifsr, addr);
570 info.si_signo = inf->sig;
572 info.si_code = inf->code;
573 info.si_addr = (void __user *)addr;
574 arm_notify_die("", regs, &info, ifsr, 0);