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/config.h>
12 #include <linux/module.h>
13 #include <linux/signal.h>
14 #include <linux/ptrace.h>
16 #include <linux/init.h>
18 #include <asm/system.h>
19 #include <asm/pgtable.h>
20 #include <asm/tlbflush.h>
21 #include <asm/uaccess.h>
26 * This is useful to dump out the page tables associated with
29 void show_pte(struct mm_struct *mm, unsigned long addr)
36 printk(KERN_ALERT "pgd = %p\n", mm->pgd);
37 pgd = pgd_offset(mm, addr);
38 printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
52 pmd = pmd_offset(pgd, addr);
54 printk(", *pmd=%08lx", pmd_val(*pmd));
65 #ifndef CONFIG_HIGHMEM
66 /* We must not map this if we have highmem enabled */
67 pte = pte_offset_map(pmd, addr);
68 printk(", *pte=%08lx", pte_val(*pte));
69 printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE]));
78 * Oops. The kernel tried to access some page that wasn't present.
81 __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
85 * Are we prepared to handle this kernel fault?
87 if (fixup_exception(regs))
91 * No handler, we'll have to terminate things with extreme prejudice.
95 "Unable to handle kernel %s at virtual address %08lx\n",
96 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
97 "paging request", addr);
100 die("Oops", regs, fsr);
106 * Something tried to access memory that isn't in our memory map..
107 * User mode accesses just cause a SIGSEGV
110 __do_user_fault(struct task_struct *tsk, unsigned long addr,
111 unsigned int fsr, int code, struct pt_regs *regs)
115 #ifdef CONFIG_DEBUG_USER
116 if (user_debug & UDBG_SEGV) {
117 printk(KERN_DEBUG "%s: unhandled page fault at 0x%08lx, code 0x%03x\n",
118 tsk->comm, addr, fsr);
119 show_pte(tsk->mm, addr);
124 tsk->thread.address = addr;
125 tsk->thread.error_code = fsr;
126 tsk->thread.trap_no = 14;
127 si.si_signo = SIGSEGV;
130 si.si_addr = (void __user *)addr;
131 force_sig_info(SIGSEGV, &si, tsk);
135 do_bad_area(struct task_struct *tsk, struct mm_struct *mm, unsigned long addr,
136 unsigned int fsr, struct pt_regs *regs)
139 * If we are in kernel mode at this point, we
140 * have no context to handle this fault with.
143 __do_user_fault(tsk, addr, fsr, SEGV_MAPERR, regs);
145 __do_kernel_fault(mm, addr, fsr, regs);
148 #define VM_FAULT_BADMAP (-20)
149 #define VM_FAULT_BADACCESS (-21)
152 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
153 struct task_struct *tsk)
155 struct vm_area_struct *vma;
158 vma = find_vma(mm, addr);
159 fault = VM_FAULT_BADMAP;
162 if (vma->vm_start > addr)
166 * Ok, we have a good vm_area for this
167 * memory access, so we can handle it.
170 if (fsr & (1 << 11)) /* write? */
173 mask = VM_READ|VM_EXEC;
175 fault = VM_FAULT_BADACCESS;
176 if (!(vma->vm_flags & mask))
180 * If for any reason at all we couldn't handle
181 * the fault, make sure we exit gracefully rather
182 * than endlessly redo the fault.
185 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, fsr & (1 << 11));
188 * Handle the "normal" cases first - successful and sigbus
196 case VM_FAULT_SIGBUS:
204 * If we are out of memory for pid1,
205 * sleep for a while and retry
211 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
218 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
220 struct task_struct *tsk;
221 struct mm_struct *mm;
228 * If we're in an interrupt or have no user
229 * context, we must not take the fault..
231 if (in_interrupt() || !mm)
234 down_read(&mm->mmap_sem);
235 fault = __do_page_fault(mm, addr, fsr, tsk);
236 up_read(&mm->mmap_sem);
239 * Handle the "normal" case first
245 * We had some memory, but were unable to
246 * successfully fix up this page fault.
252 * If we are in kernel mode at this point, we
253 * have no context to handle this fault with.
255 if (!user_mode(regs))
258 if (fault == VM_FAULT_OOM) {
260 * We ran out of memory, or some other thing happened to
261 * us that made us unable to handle the page fault gracefully.
263 printk("VM: killing process %s\n", tsk->comm);
266 __do_user_fault(tsk, addr, fsr, fault == VM_FAULT_BADACCESS ?
267 SEGV_ACCERR : SEGV_MAPERR, regs);
272 * We ran out of memory, or some other thing happened to us that made
273 * us unable to handle the page fault gracefully.
277 * Send a sigbus, regardless of whether we were in kernel
280 tsk->thread.address = addr;
281 tsk->thread.error_code = fsr;
282 tsk->thread.trap_no = 14;
283 force_sig(SIGBUS, tsk);
284 #ifdef CONFIG_DEBUG_USER
285 if (user_debug & UDBG_BUS) {
286 printk(KERN_DEBUG "%s: sigbus at 0x%08lx, pc=0x%08lx\n",
287 current->comm, addr, instruction_pointer(regs));
291 /* Kernel mode? Handle exceptions or die */
296 __do_kernel_fault(mm, addr, fsr, regs);
301 * First Level Translation Fault Handler
303 * We enter here because the first level page table doesn't contain
304 * a valid entry for the address.
306 * If the address is in kernel space (>= TASK_SIZE), then we are
307 * probably faulting in the vmalloc() area.
309 * If the init_task's first level page tables contains the relevant
310 * entry, we copy the it to this task. If not, we send the process
311 * a signal, fixup the exception, or oops the kernel.
313 * NOTE! We MUST NOT take any locks for this case. We may be in an
314 * interrupt or a critical region, and should only copy the information
315 * from the master page table, nothing more.
318 do_translation_fault(unsigned long addr, unsigned int fsr,
319 struct pt_regs *regs)
321 struct task_struct *tsk;
326 if (addr < TASK_SIZE)
327 return do_page_fault(addr, fsr, regs);
329 index = pgd_index(addr);
332 * FIXME: CP15 C1 is write only on ARMv3 architectures.
334 pgd = cpu_get_pgd() + index;
335 pgd_k = init_mm.pgd + index;
337 if (pgd_none(*pgd_k))
340 if (!pgd_present(*pgd))
341 set_pgd(pgd, *pgd_k);
343 pmd_k = pmd_offset(pgd_k, addr);
344 pmd = pmd_offset(pgd, addr);
346 if (pmd_none(*pmd_k))
349 copy_pmd(pmd, pmd_k);
355 do_bad_area(tsk, tsk->active_mm, addr, fsr, regs);
360 * Some section permission faults need to be handled gracefully.
361 * They can happen due to a __{get,put}_user during an oops.
364 do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
366 struct task_struct *tsk = current;
367 do_bad_area(tsk, tsk->active_mm, addr, fsr, regs);
372 * This abort handler always returns "fault".
375 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
380 static struct fsr_info {
381 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
386 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
387 * defines these to be "precise" aborts.
389 { do_bad, SIGSEGV, "vector exception" },
390 { do_bad, SIGILL, "alignment exception" },
391 { do_bad, SIGKILL, "terminal exception" },
392 { do_bad, SIGILL, "alignment exception" },
393 { do_bad, SIGBUS, "external abort on linefetch" },
394 { do_translation_fault, SIGSEGV, "section translation fault" },
395 { do_bad, SIGBUS, "external abort on linefetch" },
396 { do_page_fault, SIGSEGV, "page translation fault" },
397 { do_bad, SIGBUS, "external abort on non-linefetch" },
398 { do_bad, SIGSEGV, "section domain fault" },
399 { do_bad, SIGBUS, "external abort on non-linefetch" },
400 { do_bad, SIGSEGV, "page domain fault" },
401 { do_bad, SIGBUS, "external abort on translation" },
402 { do_sect_fault, SIGSEGV, "section permission fault" },
403 { do_bad, SIGBUS, "external abort on translation" },
404 { do_page_fault, SIGSEGV, "page permission fault" },
406 * The following are "imprecise" aborts, which are signalled by bit
407 * 10 of the FSR, and may not be recoverable. These are only
408 * supported if the CPU abort handler supports bit 10.
410 { do_bad, SIGBUS, "unknown 16" },
411 { do_bad, SIGBUS, "unknown 17" },
412 { do_bad, SIGBUS, "unknown 18" },
413 { do_bad, SIGBUS, "unknown 19" },
414 { do_bad, SIGBUS, "lock abort" }, /* xscale */
415 { do_bad, SIGBUS, "unknown 21" },
416 { do_bad, SIGBUS, "imprecise external abort" }, /* xscale */
417 { do_bad, SIGBUS, "unknown 23" },
418 { do_bad, SIGBUS, "dcache parity error" }, /* xscale */
419 { do_bad, SIGBUS, "unknown 25" },
420 { do_bad, SIGBUS, "unknown 26" },
421 { do_bad, SIGBUS, "unknown 27" },
422 { do_bad, SIGBUS, "unknown 28" },
423 { do_bad, SIGBUS, "unknown 29" },
424 { do_bad, SIGBUS, "unknown 30" },
425 { do_bad, SIGBUS, "unknown 31" }
429 hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
430 int sig, const char *name)
432 if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) {
433 fsr_info[nr].fn = fn;
434 fsr_info[nr].sig = sig;
435 fsr_info[nr].name = name;
440 * Dispatch a data abort to the relevant handler.
443 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
445 const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6);
447 if (!inf->fn(addr, fsr, regs))
450 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
451 inf->name, fsr, addr);
452 force_sig(inf->sig, current);
453 show_pte(current->mm, addr);
454 die_if_kernel("Oops", regs, 0);
458 do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
460 do_translation_fault(addr, 0, regs);