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>
31 static inline int notify_page_fault(struct pt_regs
*regs
, unsigned int fsr
)
35 if (!user_mode(regs
)) {
36 /* kprobe_running() needs smp_processor_id() */
38 if (kprobe_running() && kprobe_fault_handler(regs
, fsr
))
46 static inline int notify_page_fault(struct pt_regs
*regs
, unsigned int fsr
)
53 * This is useful to dump out the page tables associated with
56 void show_pte(struct mm_struct
*mm
, unsigned long addr
)
63 printk(KERN_ALERT
"pgd = %p\n", mm
->pgd
);
64 pgd
= pgd_offset(mm
, addr
);
65 printk(KERN_ALERT
"[%08lx] *pgd=%08lx", addr
, pgd_val(*pgd
));
79 pmd
= pmd_offset(pgd
, addr
);
80 if (PTRS_PER_PMD
!= 1)
81 printk(", *pmd=%08lx", pmd_val(*pmd
));
91 /* We must not map this if we have highmem enabled */
92 if (PageHighMem(pfn_to_page(pmd_val(*pmd
) >> PAGE_SHIFT
)))
95 pte
= pte_offset_map(pmd
, addr
);
96 printk(", *pte=%08lx", pte_val(*pte
));
97 printk(", *ppte=%08lx", pte_val(pte
[-PTRS_PER_PTE
]));
103 #else /* CONFIG_MMU */
104 void show_pte(struct mm_struct
*mm
, unsigned long addr
)
106 #endif /* CONFIG_MMU */
109 * Oops. The kernel tried to access some page that wasn't present.
112 __do_kernel_fault(struct mm_struct
*mm
, unsigned long addr
, unsigned int fsr
,
113 struct pt_regs
*regs
)
116 * Are we prepared to handle this kernel fault?
118 if (fixup_exception(regs
))
122 * No handler, we'll have to terminate things with extreme prejudice.
126 "Unable to handle kernel %s at virtual address %08lx\n",
127 (addr
< PAGE_SIZE
) ? "NULL pointer dereference" :
128 "paging request", addr
);
131 die("Oops", regs
, fsr
);
137 * Something tried to access memory that isn't in our memory map..
138 * User mode accesses just cause a SIGSEGV
141 __do_user_fault(struct task_struct
*tsk
, unsigned long addr
,
142 unsigned int fsr
, unsigned int sig
, int code
,
143 struct pt_regs
*regs
)
147 #ifdef CONFIG_DEBUG_USER
148 if (user_debug
& UDBG_SEGV
) {
149 printk(KERN_DEBUG
"%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
150 tsk
->comm
, sig
, addr
, fsr
);
151 show_pte(tsk
->mm
, addr
);
156 tsk
->thread
.address
= addr
;
157 tsk
->thread
.error_code
= fsr
;
158 tsk
->thread
.trap_no
= 14;
162 si
.si_addr
= (void __user
*)addr
;
163 force_sig_info(sig
, &si
, tsk
);
166 void do_bad_area(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
168 struct task_struct
*tsk
= current
;
169 struct mm_struct
*mm
= tsk
->active_mm
;
172 * If we are in kernel mode at this point, we
173 * have no context to handle this fault with.
176 __do_user_fault(tsk
, addr
, fsr
, SIGSEGV
, SEGV_MAPERR
, regs
);
178 __do_kernel_fault(mm
, addr
, fsr
, regs
);
182 #define VM_FAULT_BADMAP 0x010000
183 #define VM_FAULT_BADACCESS 0x020000
186 __do_page_fault(struct mm_struct
*mm
, unsigned long addr
, unsigned int fsr
,
187 struct task_struct
*tsk
)
189 struct vm_area_struct
*vma
;
192 vma
= find_vma(mm
, addr
);
193 fault
= VM_FAULT_BADMAP
;
196 if (vma
->vm_start
> addr
)
200 * Ok, we have a good vm_area for this
201 * memory access, so we can handle it.
204 if (fsr
& (1 << 11)) /* write? */
207 mask
= VM_READ
|VM_EXEC
|VM_WRITE
;
209 fault
= VM_FAULT_BADACCESS
;
210 if (!(vma
->vm_flags
& mask
))
214 * If for any reason at all we couldn't handle
215 * the fault, make sure we exit gracefully rather
216 * than endlessly redo the fault.
219 fault
= handle_mm_fault(mm
, vma
, addr
& PAGE_MASK
, (fsr
& (1 << 11)) ? FAULT_FLAG_WRITE
: 0);
220 if (unlikely(fault
& VM_FAULT_ERROR
)) {
221 if (fault
& VM_FAULT_OOM
)
223 else if (fault
& VM_FAULT_SIGBUS
)
227 if (fault
& VM_FAULT_MAJOR
)
234 if (!is_global_init(tsk
))
238 * If we are out of memory for pid1, sleep for a while and retry
240 up_read(&mm
->mmap_sem
);
242 down_read(&mm
->mmap_sem
);
246 if (vma
->vm_flags
& VM_GROWSDOWN
&& !expand_stack(vma
, addr
))
253 do_page_fault(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
255 struct task_struct
*tsk
;
256 struct mm_struct
*mm
;
257 int fault
, sig
, code
;
259 if (notify_page_fault(regs
, fsr
))
266 * If we're in an interrupt or have no user
267 * context, we must not take the fault..
269 if (in_atomic() || !mm
)
273 * As per x86, we may deadlock here. However, since the kernel only
274 * validly references user space from well defined areas of the code,
275 * we can bug out early if this is from code which shouldn't.
277 if (!down_read_trylock(&mm
->mmap_sem
)) {
278 if (!user_mode(regs
) && !search_exception_tables(regs
->ARM_pc
))
280 down_read(&mm
->mmap_sem
);
283 fault
= __do_page_fault(mm
, addr
, fsr
, tsk
);
284 up_read(&mm
->mmap_sem
);
287 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
289 if (likely(!(fault
& (VM_FAULT_ERROR
| VM_FAULT_BADMAP
| VM_FAULT_BADACCESS
))))
293 * If we are in kernel mode at this point, we
294 * have no context to handle this fault with.
296 if (!user_mode(regs
))
299 if (fault
& VM_FAULT_OOM
) {
301 * We ran out of memory, or some other thing
302 * happened to us that made us unable to handle
303 * the page fault gracefully.
305 printk("VM: killing process %s\n", tsk
->comm
);
306 do_group_exit(SIGKILL
);
309 if (fault
& VM_FAULT_SIGBUS
) {
311 * We had some memory, but were unable to
312 * successfully fix up this page fault.
318 * Something tried to access memory that
319 * isn't in our memory map..
322 code
= fault
== VM_FAULT_BADACCESS
?
323 SEGV_ACCERR
: SEGV_MAPERR
;
326 __do_user_fault(tsk
, addr
, fsr
, sig
, code
, regs
);
330 __do_kernel_fault(mm
, addr
, fsr
, regs
);
333 #else /* CONFIG_MMU */
335 do_page_fault(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
339 #endif /* CONFIG_MMU */
342 * First Level Translation Fault Handler
344 * We enter here because the first level page table doesn't contain
345 * a valid entry for the address.
347 * If the address is in kernel space (>= TASK_SIZE), then we are
348 * probably faulting in the vmalloc() area.
350 * If the init_task's first level page tables contains the relevant
351 * entry, we copy the it to this task. If not, we send the process
352 * a signal, fixup the exception, or oops the kernel.
354 * NOTE! We MUST NOT take any locks for this case. We may be in an
355 * interrupt or a critical region, and should only copy the information
356 * from the master page table, nothing more.
360 do_translation_fault(unsigned long addr
, unsigned int fsr
,
361 struct pt_regs
*regs
)
367 if (addr
< TASK_SIZE
)
368 return do_page_fault(addr
, fsr
, regs
);
370 index
= pgd_index(addr
);
373 * FIXME: CP15 C1 is write only on ARMv3 architectures.
375 pgd
= cpu_get_pgd() + index
;
376 pgd_k
= init_mm
.pgd
+ index
;
378 if (pgd_none(*pgd_k
))
381 if (!pgd_present(*pgd
))
382 set_pgd(pgd
, *pgd_k
);
384 pmd_k
= pmd_offset(pgd_k
, addr
);
385 pmd
= pmd_offset(pgd
, addr
);
387 if (pmd_none(*pmd_k
))
390 copy_pmd(pmd
, pmd_k
);
394 do_bad_area(addr
, fsr
, regs
);
397 #else /* CONFIG_MMU */
399 do_translation_fault(unsigned long addr
, unsigned int fsr
,
400 struct pt_regs
*regs
)
404 #endif /* CONFIG_MMU */
407 * Some section permission faults need to be handled gracefully.
408 * They can happen due to a __{get,put}_user during an oops.
411 do_sect_fault(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
413 do_bad_area(addr
, fsr
, regs
);
418 * This abort handler always returns "fault".
421 do_bad(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
426 static struct fsr_info
{
427 int (*fn
)(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
);
433 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
434 * defines these to be "precise" aborts.
436 { do_bad
, SIGSEGV
, 0, "vector exception" },
437 { do_bad
, SIGILL
, BUS_ADRALN
, "alignment exception" },
438 { do_bad
, SIGKILL
, 0, "terminal exception" },
439 { do_bad
, SIGILL
, BUS_ADRALN
, "alignment exception" },
440 { do_bad
, SIGBUS
, 0, "external abort on linefetch" },
441 { do_translation_fault
, SIGSEGV
, SEGV_MAPERR
, "section translation fault" },
442 { do_bad
, SIGBUS
, 0, "external abort on linefetch" },
443 { do_page_fault
, SIGSEGV
, SEGV_MAPERR
, "page translation fault" },
444 { do_bad
, SIGBUS
, 0, "external abort on non-linefetch" },
445 { do_bad
, SIGSEGV
, SEGV_ACCERR
, "section domain fault" },
446 { do_bad
, SIGBUS
, 0, "external abort on non-linefetch" },
447 { do_bad
, SIGSEGV
, SEGV_ACCERR
, "page domain fault" },
448 { do_bad
, SIGBUS
, 0, "external abort on translation" },
449 { do_sect_fault
, SIGSEGV
, SEGV_ACCERR
, "section permission fault" },
450 { do_bad
, SIGBUS
, 0, "external abort on translation" },
451 { do_page_fault
, SIGSEGV
, SEGV_ACCERR
, "page permission fault" },
453 * The following are "imprecise" aborts, which are signalled by bit
454 * 10 of the FSR, and may not be recoverable. These are only
455 * supported if the CPU abort handler supports bit 10.
457 { do_bad
, SIGBUS
, 0, "unknown 16" },
458 { do_bad
, SIGBUS
, 0, "unknown 17" },
459 { do_bad
, SIGBUS
, 0, "unknown 18" },
460 { do_bad
, SIGBUS
, 0, "unknown 19" },
461 { do_bad
, SIGBUS
, 0, "lock abort" }, /* xscale */
462 { do_bad
, SIGBUS
, 0, "unknown 21" },
463 { do_bad
, SIGBUS
, BUS_OBJERR
, "imprecise external abort" }, /* xscale */
464 { do_bad
, SIGBUS
, 0, "unknown 23" },
465 { do_bad
, SIGBUS
, 0, "dcache parity error" }, /* xscale */
466 { do_bad
, SIGBUS
, 0, "unknown 25" },
467 { do_bad
, SIGBUS
, 0, "unknown 26" },
468 { do_bad
, SIGBUS
, 0, "unknown 27" },
469 { do_bad
, SIGBUS
, 0, "unknown 28" },
470 { do_bad
, SIGBUS
, 0, "unknown 29" },
471 { do_bad
, SIGBUS
, 0, "unknown 30" },
472 { do_bad
, SIGBUS
, 0, "unknown 31" }
476 hook_fault_code(int nr
, int (*fn
)(unsigned long, unsigned int, struct pt_regs
*),
477 int sig
, const char *name
)
479 if (nr
>= 0 && nr
< ARRAY_SIZE(fsr_info
)) {
480 fsr_info
[nr
].fn
= fn
;
481 fsr_info
[nr
].sig
= sig
;
482 fsr_info
[nr
].name
= name
;
487 * Dispatch a data abort to the relevant handler.
489 asmlinkage
void __exception
490 do_DataAbort(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
492 const struct fsr_info
*inf
= fsr_info
+ (fsr
& 15) + ((fsr
& (1 << 10)) >> 6);
495 if (!inf
->fn(addr
, fsr
, regs
))
498 printk(KERN_ALERT
"Unhandled fault: %s (0x%03x) at 0x%08lx\n",
499 inf
->name
, fsr
, addr
);
501 info
.si_signo
= inf
->sig
;
503 info
.si_code
= inf
->code
;
504 info
.si_addr
= (void __user
*)addr
;
505 arm_notify_die("", regs
, &info
, fsr
, 0);
508 asmlinkage
void __exception
509 do_PrefetchAbort(unsigned long addr
, struct pt_regs
*regs
)
511 do_translation_fault(addr
, 0, regs
);