Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / mm / fault.c
blob29be1c018949b6ef379216000dc45031c46521db
1 /*
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>
15 #include <linux/mm.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>
23 #include "fault.h"
26 * This is useful to dump out the page tables associated with
27 * 'addr' in mm 'mm'.
29 void show_pte(struct mm_struct *mm, unsigned long addr)
31 pgd_t *pgd;
33 if (!mm)
34 mm = &init_mm;
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));
40 do {
41 pmd_t *pmd;
42 pte_t *pte;
44 if (pgd_none(*pgd))
45 break;
47 if (pgd_bad(*pgd)) {
48 printk("(bad)");
49 break;
52 pmd = pmd_offset(pgd, addr);
53 #if PTRS_PER_PMD != 1
54 printk(", *pmd=%08lx", pmd_val(*pmd));
55 #endif
57 if (pmd_none(*pmd))
58 break;
60 if (pmd_bad(*pmd)) {
61 printk("(bad)");
62 break;
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]));
70 pte_unmap(pte);
71 #endif
72 } while(0);
74 printk("\n");
78 * Oops. The kernel tried to access some page that wasn't present.
80 static void
81 __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
82 struct pt_regs *regs)
85 * Are we prepared to handle this kernel fault?
87 if (fixup_exception(regs))
88 return;
91 * No handler, we'll have to terminate things with extreme prejudice.
93 bust_spinlocks(1);
94 printk(KERN_ALERT
95 "Unable to handle kernel %s at virtual address %08lx\n",
96 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
97 "paging request", addr);
99 show_pte(mm, addr);
100 die("Oops", regs, fsr);
101 bust_spinlocks(0);
102 do_exit(SIGKILL);
106 * Something tried to access memory that isn't in our memory map..
107 * User mode accesses just cause a SIGSEGV
109 static void
110 __do_user_fault(struct task_struct *tsk, unsigned long addr,
111 unsigned int fsr, int code, struct pt_regs *regs)
113 struct siginfo si;
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);
120 show_regs(regs);
122 #endif
124 tsk->thread.address = addr;
125 tsk->thread.error_code = fsr;
126 tsk->thread.trap_no = 14;
127 si.si_signo = SIGSEGV;
128 si.si_errno = 0;
129 si.si_code = code;
130 si.si_addr = (void __user *)addr;
131 force_sig_info(SIGSEGV, &si, tsk);
134 void
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.
142 if (user_mode(regs))
143 __do_user_fault(tsk, addr, fsr, SEGV_MAPERR, regs);
144 else
145 __do_kernel_fault(mm, addr, fsr, regs);
148 #define VM_FAULT_BADMAP (-20)
149 #define VM_FAULT_BADACCESS (-21)
151 static int
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;
156 int fault, mask;
158 vma = find_vma(mm, addr);
159 fault = VM_FAULT_BADMAP;
160 if (!vma)
161 goto out;
162 if (vma->vm_start > addr)
163 goto check_stack;
166 * Ok, we have a good vm_area for this
167 * memory access, so we can handle it.
169 good_area:
170 if (fsr & (1 << 11)) /* write? */
171 mask = VM_WRITE;
172 else
173 mask = VM_READ|VM_EXEC;
175 fault = VM_FAULT_BADACCESS;
176 if (!(vma->vm_flags & mask))
177 goto out;
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.
184 survive:
185 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, fsr & (1 << 11));
188 * Handle the "normal" cases first - successful and sigbus
190 switch (fault) {
191 case VM_FAULT_MAJOR:
192 tsk->maj_flt++;
193 return fault;
194 case VM_FAULT_MINOR:
195 tsk->min_flt++;
196 case VM_FAULT_SIGBUS:
197 return fault;
200 if (tsk->pid != 1)
201 goto out;
204 * If we are out of memory for pid1,
205 * sleep for a while and retry
207 yield();
208 goto survive;
210 check_stack:
211 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
212 goto good_area;
213 out:
214 return fault;
217 static int
218 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
220 struct task_struct *tsk;
221 struct mm_struct *mm;
222 int fault;
224 tsk = current;
225 mm = tsk->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)
232 goto no_context;
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
241 if (fault > 0)
242 return 0;
245 * We had some memory, but were unable to
246 * successfully fix up this page fault.
248 if (fault == 0)
249 goto do_sigbus;
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))
256 goto no_context;
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);
264 do_exit(SIGKILL);
265 } else
266 __do_user_fault(tsk, addr, fsr, fault == VM_FAULT_BADACCESS ?
267 SEGV_ACCERR : SEGV_MAPERR, regs);
268 return 0;
272 * We ran out of memory, or some other thing happened to us that made
273 * us unable to handle the page fault gracefully.
275 do_sigbus:
277 * Send a sigbus, regardless of whether we were in kernel
278 * or user mode.
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));
289 #endif
291 /* Kernel mode? Handle exceptions or die */
292 if (user_mode(regs))
293 return 0;
295 no_context:
296 __do_kernel_fault(mm, addr, fsr, regs);
297 return 0;
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.
317 static int
318 do_translation_fault(unsigned long addr, unsigned int fsr,
319 struct pt_regs *regs)
321 struct task_struct *tsk;
322 unsigned int index;
323 pgd_t *pgd, *pgd_k;
324 pmd_t *pmd, *pmd_k;
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))
338 goto bad_area;
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))
347 goto bad_area;
349 copy_pmd(pmd, pmd_k);
350 return 0;
352 bad_area:
353 tsk = current;
355 do_bad_area(tsk, tsk->active_mm, addr, fsr, regs);
356 return 0;
360 * Some section permission faults need to be handled gracefully.
361 * They can happen due to a __{get,put}_user during an oops.
363 static int
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);
368 return 0;
372 * This abort handler always returns "fault".
374 static int
375 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
377 return 1;
380 static struct fsr_info {
381 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
382 int sig;
383 const char *name;
384 } fsr_info[] = {
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" }
428 void __init
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.
442 asmlinkage void
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))
448 return;
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);
457 asmlinkage void
458 do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
460 do_translation_fault(addr, 0, regs);