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>
20 #include <asm/system.h>
21 #include <asm/pgtable.h>
22 #include <asm/tlbflush.h>
28 static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
32 if (!user_mode(regs)) {
33 /* kprobe_running() needs smp_processor_id() */
35 if (kprobe_running() && kprobe_fault_handler(regs, fsr))
43 static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
50 * This is useful to dump out the page tables associated with
53 void show_pte(struct mm_struct *mm, unsigned long addr)
60 printk(KERN_ALERT "pgd = %p\n", mm->pgd);
61 pgd = pgd_offset(mm, addr);
62 printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
76 pmd = pmd_offset(pgd, addr);
77 if (PTRS_PER_PMD != 1)
78 printk(", *pmd=%08lx", pmd_val(*pmd));
88 /* We must not map this if we have highmem enabled */
89 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
92 pte = pte_offset_map(pmd, addr);
93 printk(", *pte=%08lx", pte_val(*pte));
94 printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE]));
102 * Oops. The kernel tried to access some page that wasn't present.
105 __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
106 struct pt_regs *regs)
109 * Are we prepared to handle this kernel fault?
111 if (fixup_exception(regs))
115 * No handler, we'll have to terminate things with extreme prejudice.
119 "Unable to handle kernel %s at virtual address %08lx\n",
120 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
121 "paging request", addr);
124 die("Oops", regs, fsr);
130 * Something tried to access memory that isn't in our memory map..
131 * User mode accesses just cause a SIGSEGV
134 __do_user_fault(struct task_struct *tsk, unsigned long addr,
135 unsigned int fsr, unsigned int sig, int code,
136 struct pt_regs *regs)
140 #ifdef CONFIG_DEBUG_USER
141 if (user_debug & UDBG_SEGV) {
142 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
143 tsk->comm, sig, addr, fsr);
144 show_pte(tsk->mm, addr);
149 tsk->thread.address = addr;
150 tsk->thread.error_code = fsr;
151 tsk->thread.trap_no = 14;
155 si.si_addr = (void __user *)addr;
156 force_sig_info(sig, &si, tsk);
159 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
161 struct task_struct *tsk = current;
162 struct mm_struct *mm = tsk->active_mm;
165 * If we are in kernel mode at this point, we
166 * have no context to handle this fault with.
169 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
171 __do_kernel_fault(mm, addr, fsr, regs);
174 #define VM_FAULT_BADMAP 0x010000
175 #define VM_FAULT_BADACCESS 0x020000
178 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
179 struct task_struct *tsk)
181 struct vm_area_struct *vma;
184 vma = find_vma(mm, addr);
185 fault = VM_FAULT_BADMAP;
188 if (vma->vm_start > addr)
192 * Ok, we have a good vm_area for this
193 * memory access, so we can handle it.
196 if (fsr & (1 << 11)) /* write? */
199 mask = VM_READ|VM_EXEC|VM_WRITE;
201 fault = VM_FAULT_BADACCESS;
202 if (!(vma->vm_flags & mask))
206 * If for any reason at all we couldn't handle
207 * the fault, make sure we exit gracefully rather
208 * than endlessly redo the fault.
211 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, fsr & (1 << 11));
212 if (unlikely(fault & VM_FAULT_ERROR)) {
213 if (fault & VM_FAULT_OOM)
215 else if (fault & VM_FAULT_SIGBUS)
219 if (fault & VM_FAULT_MAJOR)
226 if (!is_global_init(tsk))
230 * If we are out of memory for pid1, sleep for a while and retry
232 up_read(&mm->mmap_sem);
234 down_read(&mm->mmap_sem);
238 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
245 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
247 struct task_struct *tsk;
248 struct mm_struct *mm;
249 int fault, sig, code;
251 if (notify_page_fault(regs, fsr))
258 * If we're in an interrupt or have no user
259 * context, we must not take the fault..
261 if (in_atomic() || !mm)
265 * As per x86, we may deadlock here. However, since the kernel only
266 * validly references user space from well defined areas of the code,
267 * we can bug out early if this is from code which shouldn't.
269 if (!down_read_trylock(&mm->mmap_sem)) {
270 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
272 down_read(&mm->mmap_sem);
275 fault = __do_page_fault(mm, addr, fsr, tsk);
276 up_read(&mm->mmap_sem);
279 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
281 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
285 * If we are in kernel mode at this point, we
286 * have no context to handle this fault with.
288 if (!user_mode(regs))
291 if (fault & VM_FAULT_OOM) {
293 * We ran out of memory, or some other thing
294 * happened to us that made us unable to handle
295 * the page fault gracefully.
297 printk("VM: killing process %s\n", tsk->comm);
298 do_group_exit(SIGKILL);
301 if (fault & VM_FAULT_SIGBUS) {
303 * We had some memory, but were unable to
304 * successfully fix up this page fault.
310 * Something tried to access memory that
311 * isn't in our memory map..
314 code = fault == VM_FAULT_BADACCESS ?
315 SEGV_ACCERR : SEGV_MAPERR;
318 __do_user_fault(tsk, addr, fsr, sig, code, regs);
322 __do_kernel_fault(mm, addr, fsr, regs);
327 * First Level Translation Fault Handler
329 * We enter here because the first level page table doesn't contain
330 * a valid entry for the address.
332 * If the address is in kernel space (>= TASK_SIZE), then we are
333 * probably faulting in the vmalloc() area.
335 * If the init_task's first level page tables contains the relevant
336 * entry, we copy the it to this task. If not, we send the process
337 * a signal, fixup the exception, or oops the kernel.
339 * NOTE! We MUST NOT take any locks for this case. We may be in an
340 * interrupt or a critical region, and should only copy the information
341 * from the master page table, nothing more.
344 do_translation_fault(unsigned long addr, unsigned int fsr,
345 struct pt_regs *regs)
351 if (addr < TASK_SIZE)
352 return do_page_fault(addr, fsr, regs);
354 index = pgd_index(addr);
357 * FIXME: CP15 C1 is write only on ARMv3 architectures.
359 pgd = cpu_get_pgd() + index;
360 pgd_k = init_mm.pgd + index;
362 if (pgd_none(*pgd_k))
365 if (!pgd_present(*pgd))
366 set_pgd(pgd, *pgd_k);
368 pmd_k = pmd_offset(pgd_k, addr);
369 pmd = pmd_offset(pgd, addr);
371 if (pmd_none(*pmd_k))
374 copy_pmd(pmd, pmd_k);
378 do_bad_area(addr, fsr, regs);
383 * Some section permission faults need to be handled gracefully.
384 * They can happen due to a __{get,put}_user during an oops.
387 do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
389 do_bad_area(addr, fsr, regs);
394 * This abort handler always returns "fault".
397 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
402 static struct fsr_info {
403 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
409 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
410 * defines these to be "precise" aborts.
412 { do_bad, SIGSEGV, 0, "vector exception" },
413 { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
414 { do_bad, SIGKILL, 0, "terminal exception" },
415 { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
416 { do_bad, SIGBUS, 0, "external abort on linefetch" },
417 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
418 { do_bad, SIGBUS, 0, "external abort on linefetch" },
419 { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
420 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
421 { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
422 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
423 { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
424 { do_bad, SIGBUS, 0, "external abort on translation" },
425 { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
426 { do_bad, SIGBUS, 0, "external abort on translation" },
427 { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
429 * The following are "imprecise" aborts, which are signalled by bit
430 * 10 of the FSR, and may not be recoverable. These are only
431 * supported if the CPU abort handler supports bit 10.
433 { do_bad, SIGBUS, 0, "unknown 16" },
434 { do_bad, SIGBUS, 0, "unknown 17" },
435 { do_bad, SIGBUS, 0, "unknown 18" },
436 { do_bad, SIGBUS, 0, "unknown 19" },
437 { do_bad, SIGBUS, 0, "lock abort" }, /* xscale */
438 { do_bad, SIGBUS, 0, "unknown 21" },
439 { do_bad, SIGBUS, BUS_OBJERR, "imprecise external abort" }, /* xscale */
440 { do_bad, SIGBUS, 0, "unknown 23" },
441 { do_bad, SIGBUS, 0, "dcache parity error" }, /* xscale */
442 { do_bad, SIGBUS, 0, "unknown 25" },
443 { do_bad, SIGBUS, 0, "unknown 26" },
444 { do_bad, SIGBUS, 0, "unknown 27" },
445 { do_bad, SIGBUS, 0, "unknown 28" },
446 { do_bad, SIGBUS, 0, "unknown 29" },
447 { do_bad, SIGBUS, 0, "unknown 30" },
448 { do_bad, SIGBUS, 0, "unknown 31" }
452 hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
453 int sig, const char *name)
455 if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) {
456 fsr_info[nr].fn = fn;
457 fsr_info[nr].sig = sig;
458 fsr_info[nr].name = name;
463 * Dispatch a data abort to the relevant handler.
465 asmlinkage void __exception
466 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
468 const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6);
471 if (!inf->fn(addr, fsr, regs))
474 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
475 inf->name, fsr, addr);
477 info.si_signo = inf->sig;
479 info.si_code = inf->code;
480 info.si_addr = (void __user *)addr;
481 arm_notify_die("", regs, &info, fsr, 0);
484 asmlinkage void __exception
485 do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
487 do_translation_fault(addr, 0, regs);