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/init.h>
16 #include <asm/system.h>
17 #include <asm/pgtable.h>
18 #include <asm/tlbflush.h>
19 #include <asm/uaccess.h>
24 * This is useful to dump out the page tables associated with
27 void show_pte(struct mm_struct
*mm
, unsigned long addr
)
34 printk(KERN_ALERT
"pgd = %p\n", mm
->pgd
);
35 pgd
= pgd_offset(mm
, addr
);
36 printk(KERN_ALERT
"[%08lx] *pgd=%08lx", addr
, pgd_val(*pgd
));
50 pmd
= pmd_offset(pgd
, addr
);
52 printk(", *pmd=%08lx", pmd_val(*pmd
));
63 #ifndef CONFIG_HIGHMEM
64 /* We must not map this if we have highmem enabled */
65 pte
= pte_offset_map(pmd
, addr
);
66 printk(", *pte=%08lx", pte_val(*pte
));
67 printk(", *ppte=%08lx", pte_val(pte
[-PTRS_PER_PTE
]));
76 * Oops. The kernel tried to access some page that wasn't present.
79 __do_kernel_fault(struct mm_struct
*mm
, unsigned long addr
, unsigned int fsr
,
83 * Are we prepared to handle this kernel fault?
85 if (fixup_exception(regs
))
89 * No handler, we'll have to terminate things with extreme prejudice.
93 "Unable to handle kernel %s at virtual address %08lx\n",
94 (addr
< PAGE_SIZE
) ? "NULL pointer dereference" :
95 "paging request", addr
);
98 die("Oops", regs
, fsr
);
104 * Something tried to access memory that isn't in our memory map..
105 * User mode accesses just cause a SIGSEGV
108 __do_user_fault(struct task_struct
*tsk
, unsigned long addr
,
109 unsigned int fsr
, unsigned int sig
, int code
,
110 struct pt_regs
*regs
)
114 #ifdef CONFIG_DEBUG_USER
115 if (user_debug
& UDBG_SEGV
) {
116 printk(KERN_DEBUG
"%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
117 tsk
->comm
, sig
, addr
, fsr
);
118 show_pte(tsk
->mm
, addr
);
123 tsk
->thread
.address
= addr
;
124 tsk
->thread
.error_code
= fsr
;
125 tsk
->thread
.trap_no
= 14;
129 si
.si_addr
= (void __user
*)addr
;
130 force_sig_info(sig
, &si
, tsk
);
133 void do_bad_area(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
135 struct task_struct
*tsk
= current
;
136 struct mm_struct
*mm
= tsk
->active_mm
;
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
, SIGSEGV
, SEGV_MAPERR
, regs
);
145 __do_kernel_fault(mm
, addr
, fsr
, regs
);
148 #define VM_FAULT_BADMAP 0x010000
149 #define VM_FAULT_BADACCESS 0x020000
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
|VM_WRITE
;
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));
186 if (unlikely(fault
& VM_FAULT_ERROR
)) {
187 if (fault
& VM_FAULT_OOM
)
189 else if (fault
& VM_FAULT_SIGBUS
)
193 if (fault
& VM_FAULT_MAJOR
)
200 if (!is_global_init(tsk
))
204 * If we are out of memory for pid1, sleep for a while and retry
206 up_read(&mm
->mmap_sem
);
208 down_read(&mm
->mmap_sem
);
212 if (vma
->vm_flags
& VM_GROWSDOWN
&& !expand_stack(vma
, addr
))
219 do_page_fault(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
221 struct task_struct
*tsk
;
222 struct mm_struct
*mm
;
223 int fault
, sig
, code
;
229 * If we're in an interrupt or have no user
230 * context, we must not take the fault..
232 if (in_atomic() || !mm
)
236 * As per x86, we may deadlock here. However, since the kernel only
237 * validly references user space from well defined areas of the code,
238 * we can bug out early if this is from code which shouldn't.
240 if (!down_read_trylock(&mm
->mmap_sem
)) {
241 if (!user_mode(regs
) && !search_exception_tables(regs
->ARM_pc
))
243 down_read(&mm
->mmap_sem
);
246 fault
= __do_page_fault(mm
, addr
, fsr
, tsk
);
247 up_read(&mm
->mmap_sem
);
250 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
252 if (likely(!(fault
& (VM_FAULT_ERROR
| VM_FAULT_BADMAP
| VM_FAULT_BADACCESS
))))
256 * If we are in kernel mode at this point, we
257 * have no context to handle this fault with.
259 if (!user_mode(regs
))
262 if (fault
& VM_FAULT_OOM
) {
264 * We ran out of memory, or some other thing
265 * happened to us that made us unable to handle
266 * the page fault gracefully.
268 printk("VM: killing process %s\n", tsk
->comm
);
269 do_group_exit(SIGKILL
);
272 if (fault
& VM_FAULT_SIGBUS
) {
274 * We had some memory, but were unable to
275 * successfully fix up this page fault.
281 * Something tried to access memory that
282 * isn't in our memory map..
285 code
= fault
== VM_FAULT_BADACCESS
?
286 SEGV_ACCERR
: SEGV_MAPERR
;
289 __do_user_fault(tsk
, addr
, fsr
, sig
, code
, regs
);
293 __do_kernel_fault(mm
, addr
, fsr
, regs
);
298 * First Level Translation Fault Handler
300 * We enter here because the first level page table doesn't contain
301 * a valid entry for the address.
303 * If the address is in kernel space (>= TASK_SIZE), then we are
304 * probably faulting in the vmalloc() area.
306 * If the init_task's first level page tables contains the relevant
307 * entry, we copy the it to this task. If not, we send the process
308 * a signal, fixup the exception, or oops the kernel.
310 * NOTE! We MUST NOT take any locks for this case. We may be in an
311 * interrupt or a critical region, and should only copy the information
312 * from the master page table, nothing more.
315 do_translation_fault(unsigned long addr
, unsigned int fsr
,
316 struct pt_regs
*regs
)
322 if (addr
< TASK_SIZE
)
323 return do_page_fault(addr
, fsr
, regs
);
325 index
= pgd_index(addr
);
328 * FIXME: CP15 C1 is write only on ARMv3 architectures.
330 pgd
= cpu_get_pgd() + index
;
331 pgd_k
= init_mm
.pgd
+ index
;
333 if (pgd_none(*pgd_k
))
336 if (!pgd_present(*pgd
))
337 set_pgd(pgd
, *pgd_k
);
339 pmd_k
= pmd_offset(pgd_k
, addr
);
340 pmd
= pmd_offset(pgd
, addr
);
342 if (pmd_none(*pmd_k
))
345 copy_pmd(pmd
, pmd_k
);
349 do_bad_area(addr
, fsr
, regs
);
354 * Some section permission faults need to be handled gracefully.
355 * They can happen due to a __{get,put}_user during an oops.
358 do_sect_fault(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
360 do_bad_area(addr
, fsr
, regs
);
365 * This abort handler always returns "fault".
368 do_bad(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
373 static struct fsr_info
{
374 int (*fn
)(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
);
380 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
381 * defines these to be "precise" aborts.
383 { do_bad
, SIGSEGV
, 0, "vector exception" },
384 { do_bad
, SIGILL
, BUS_ADRALN
, "alignment exception" },
385 { do_bad
, SIGKILL
, 0, "terminal exception" },
386 { do_bad
, SIGILL
, BUS_ADRALN
, "alignment exception" },
387 { do_bad
, SIGBUS
, 0, "external abort on linefetch" },
388 { do_translation_fault
, SIGSEGV
, SEGV_MAPERR
, "section translation fault" },
389 { do_bad
, SIGBUS
, 0, "external abort on linefetch" },
390 { do_page_fault
, SIGSEGV
, SEGV_MAPERR
, "page translation fault" },
391 { do_bad
, SIGBUS
, 0, "external abort on non-linefetch" },
392 { do_bad
, SIGSEGV
, SEGV_ACCERR
, "section domain fault" },
393 { do_bad
, SIGBUS
, 0, "external abort on non-linefetch" },
394 { do_bad
, SIGSEGV
, SEGV_ACCERR
, "page domain fault" },
395 { do_bad
, SIGBUS
, 0, "external abort on translation" },
396 { do_sect_fault
, SIGSEGV
, SEGV_ACCERR
, "section permission fault" },
397 { do_bad
, SIGBUS
, 0, "external abort on translation" },
398 { do_page_fault
, SIGSEGV
, SEGV_ACCERR
, "page permission fault" },
400 * The following are "imprecise" aborts, which are signalled by bit
401 * 10 of the FSR, and may not be recoverable. These are only
402 * supported if the CPU abort handler supports bit 10.
404 { do_bad
, SIGBUS
, 0, "unknown 16" },
405 { do_bad
, SIGBUS
, 0, "unknown 17" },
406 { do_bad
, SIGBUS
, 0, "unknown 18" },
407 { do_bad
, SIGBUS
, 0, "unknown 19" },
408 { do_bad
, SIGBUS
, 0, "lock abort" }, /* xscale */
409 { do_bad
, SIGBUS
, 0, "unknown 21" },
410 { do_bad
, SIGBUS
, BUS_OBJERR
, "imprecise external abort" }, /* xscale */
411 { do_bad
, SIGBUS
, 0, "unknown 23" },
412 { do_bad
, SIGBUS
, 0, "dcache parity error" }, /* xscale */
413 { do_bad
, SIGBUS
, 0, "unknown 25" },
414 { do_bad
, SIGBUS
, 0, "unknown 26" },
415 { do_bad
, SIGBUS
, 0, "unknown 27" },
416 { do_bad
, SIGBUS
, 0, "unknown 28" },
417 { do_bad
, SIGBUS
, 0, "unknown 29" },
418 { do_bad
, SIGBUS
, 0, "unknown 30" },
419 { do_bad
, SIGBUS
, 0, "unknown 31" }
423 hook_fault_code(int nr
, int (*fn
)(unsigned long, unsigned int, struct pt_regs
*),
424 int sig
, const char *name
)
426 if (nr
>= 0 && nr
< ARRAY_SIZE(fsr_info
)) {
427 fsr_info
[nr
].fn
= fn
;
428 fsr_info
[nr
].sig
= sig
;
429 fsr_info
[nr
].name
= name
;
434 * Dispatch a data abort to the relevant handler.
436 asmlinkage
void __exception
437 do_DataAbort(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
439 const struct fsr_info
*inf
= fsr_info
+ (fsr
& 15) + ((fsr
& (1 << 10)) >> 6);
442 if (!inf
->fn(addr
, fsr
, regs
))
445 printk(KERN_ALERT
"Unhandled fault: %s (0x%03x) at 0x%08lx\n",
446 inf
->name
, fsr
, addr
);
448 info
.si_signo
= inf
->sig
;
450 info
.si_code
= inf
->code
;
451 info
.si_addr
= (void __user
*)addr
;
452 arm_notify_die("", regs
, &info
, fsr
, 0);
455 asmlinkage
void __exception
456 do_PrefetchAbort(unsigned long addr
, struct pt_regs
*regs
)
458 do_translation_fault(addr
, 0, regs
);