[XFS] Handle inode semaphores properly for dmapi queues
[linux-2.6.22.y-op.git] / arch / arm / mm / fault.c
blobe25b4fd8412cb4f0c4f0c51c09ea30bc57a9c50d
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, unsigned int sig, int code,
112 struct pt_regs *regs)
114 struct siginfo si;
116 #ifdef CONFIG_DEBUG_USER
117 if (user_debug & UDBG_SEGV) {
118 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
119 tsk->comm, sig, addr, fsr);
120 show_pte(tsk->mm, addr);
121 show_regs(regs);
123 #endif
125 tsk->thread.address = addr;
126 tsk->thread.error_code = fsr;
127 tsk->thread.trap_no = 14;
128 si.si_signo = sig;
129 si.si_errno = 0;
130 si.si_code = code;
131 si.si_addr = (void __user *)addr;
132 force_sig_info(sig, &si, tsk);
135 void
136 do_bad_area(struct task_struct *tsk, struct mm_struct *mm, unsigned long addr,
137 unsigned int fsr, struct pt_regs *regs)
140 * If we are in kernel mode at this point, we
141 * have no context to handle this fault with.
143 if (user_mode(regs))
144 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
145 else
146 __do_kernel_fault(mm, addr, fsr, regs);
149 #define VM_FAULT_BADMAP (-20)
150 #define VM_FAULT_BADACCESS (-21)
152 static int
153 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
154 struct task_struct *tsk)
156 struct vm_area_struct *vma;
157 int fault, mask;
159 vma = find_vma(mm, addr);
160 fault = VM_FAULT_BADMAP;
161 if (!vma)
162 goto out;
163 if (vma->vm_start > addr)
164 goto check_stack;
167 * Ok, we have a good vm_area for this
168 * memory access, so we can handle it.
170 good_area:
171 if (fsr & (1 << 11)) /* write? */
172 mask = VM_WRITE;
173 else
174 mask = VM_READ|VM_EXEC;
176 fault = VM_FAULT_BADACCESS;
177 if (!(vma->vm_flags & mask))
178 goto out;
181 * If for any reason at all we couldn't handle
182 * the fault, make sure we exit gracefully rather
183 * than endlessly redo the fault.
185 survive:
186 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, fsr & (1 << 11));
189 * Handle the "normal" cases first - successful and sigbus
191 switch (fault) {
192 case VM_FAULT_MAJOR:
193 tsk->maj_flt++;
194 return fault;
195 case VM_FAULT_MINOR:
196 tsk->min_flt++;
197 case VM_FAULT_SIGBUS:
198 return fault;
201 if (tsk->pid != 1)
202 goto out;
205 * If we are out of memory for pid1, sleep for a while and retry
207 up_read(&mm->mmap_sem);
208 yield();
209 down_read(&mm->mmap_sem);
210 goto survive;
212 check_stack:
213 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
214 goto good_area;
215 out:
216 return fault;
219 static int
220 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
222 struct task_struct *tsk;
223 struct mm_struct *mm;
224 int fault, sig, code;
226 tsk = current;
227 mm = tsk->mm;
230 * If we're in an interrupt or have no user
231 * context, we must not take the fault..
233 if (in_interrupt() || !mm)
234 goto no_context;
236 down_read(&mm->mmap_sem);
237 fault = __do_page_fault(mm, addr, fsr, tsk);
238 up_read(&mm->mmap_sem);
241 * Handle the "normal" case first
243 if (fault > 0)
244 return 0;
247 * If we are in kernel mode at this point, we
248 * have no context to handle this fault with.
250 if (!user_mode(regs))
251 goto no_context;
253 switch (fault) {
254 case VM_FAULT_OOM:
256 * We ran out of memory, or some other thing
257 * happened to us that made us unable to handle
258 * the page fault gracefully.
260 printk("VM: killing process %s\n", tsk->comm);
261 do_exit(SIGKILL);
262 return 0;
264 case 0:
266 * We had some memory, but were unable to
267 * successfully fix up this page fault.
269 sig = SIGBUS;
270 code = BUS_ADRERR;
271 break;
273 default:
275 * Something tried to access memory that
276 * isn't in our memory map..
278 sig = SIGSEGV;
279 code = fault == VM_FAULT_BADACCESS ?
280 SEGV_ACCERR : SEGV_MAPERR;
281 break;
284 __do_user_fault(tsk, addr, fsr, sig, code, regs);
285 return 0;
287 no_context:
288 __do_kernel_fault(mm, addr, fsr, regs);
289 return 0;
293 * First Level Translation Fault Handler
295 * We enter here because the first level page table doesn't contain
296 * a valid entry for the address.
298 * If the address is in kernel space (>= TASK_SIZE), then we are
299 * probably faulting in the vmalloc() area.
301 * If the init_task's first level page tables contains the relevant
302 * entry, we copy the it to this task. If not, we send the process
303 * a signal, fixup the exception, or oops the kernel.
305 * NOTE! We MUST NOT take any locks for this case. We may be in an
306 * interrupt or a critical region, and should only copy the information
307 * from the master page table, nothing more.
309 static int
310 do_translation_fault(unsigned long addr, unsigned int fsr,
311 struct pt_regs *regs)
313 struct task_struct *tsk;
314 unsigned int index;
315 pgd_t *pgd, *pgd_k;
316 pmd_t *pmd, *pmd_k;
318 if (addr < TASK_SIZE)
319 return do_page_fault(addr, fsr, regs);
321 index = pgd_index(addr);
324 * FIXME: CP15 C1 is write only on ARMv3 architectures.
326 pgd = cpu_get_pgd() + index;
327 pgd_k = init_mm.pgd + index;
329 if (pgd_none(*pgd_k))
330 goto bad_area;
332 if (!pgd_present(*pgd))
333 set_pgd(pgd, *pgd_k);
335 pmd_k = pmd_offset(pgd_k, addr);
336 pmd = pmd_offset(pgd, addr);
338 if (pmd_none(*pmd_k))
339 goto bad_area;
341 copy_pmd(pmd, pmd_k);
342 return 0;
344 bad_area:
345 tsk = current;
347 do_bad_area(tsk, tsk->active_mm, addr, fsr, regs);
348 return 0;
352 * Some section permission faults need to be handled gracefully.
353 * They can happen due to a __{get,put}_user during an oops.
355 static int
356 do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
358 struct task_struct *tsk = current;
359 do_bad_area(tsk, tsk->active_mm, addr, fsr, regs);
360 return 0;
364 * This abort handler always returns "fault".
366 static int
367 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
369 return 1;
372 static struct fsr_info {
373 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
374 int sig;
375 const char *name;
376 } fsr_info[] = {
378 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
379 * defines these to be "precise" aborts.
381 { do_bad, SIGSEGV, "vector exception" },
382 { do_bad, SIGILL, "alignment exception" },
383 { do_bad, SIGKILL, "terminal exception" },
384 { do_bad, SIGILL, "alignment exception" },
385 { do_bad, SIGBUS, "external abort on linefetch" },
386 { do_translation_fault, SIGSEGV, "section translation fault" },
387 { do_bad, SIGBUS, "external abort on linefetch" },
388 { do_page_fault, SIGSEGV, "page translation fault" },
389 { do_bad, SIGBUS, "external abort on non-linefetch" },
390 { do_bad, SIGSEGV, "section domain fault" },
391 { do_bad, SIGBUS, "external abort on non-linefetch" },
392 { do_bad, SIGSEGV, "page domain fault" },
393 { do_bad, SIGBUS, "external abort on translation" },
394 { do_sect_fault, SIGSEGV, "section permission fault" },
395 { do_bad, SIGBUS, "external abort on translation" },
396 { do_page_fault, SIGSEGV, "page permission fault" },
398 * The following are "imprecise" aborts, which are signalled by bit
399 * 10 of the FSR, and may not be recoverable. These are only
400 * supported if the CPU abort handler supports bit 10.
402 { do_bad, SIGBUS, "unknown 16" },
403 { do_bad, SIGBUS, "unknown 17" },
404 { do_bad, SIGBUS, "unknown 18" },
405 { do_bad, SIGBUS, "unknown 19" },
406 { do_bad, SIGBUS, "lock abort" }, /* xscale */
407 { do_bad, SIGBUS, "unknown 21" },
408 { do_bad, SIGBUS, "imprecise external abort" }, /* xscale */
409 { do_bad, SIGBUS, "unknown 23" },
410 { do_bad, SIGBUS, "dcache parity error" }, /* xscale */
411 { do_bad, SIGBUS, "unknown 25" },
412 { do_bad, SIGBUS, "unknown 26" },
413 { do_bad, SIGBUS, "unknown 27" },
414 { do_bad, SIGBUS, "unknown 28" },
415 { do_bad, SIGBUS, "unknown 29" },
416 { do_bad, SIGBUS, "unknown 30" },
417 { do_bad, SIGBUS, "unknown 31" }
420 void __init
421 hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
422 int sig, const char *name)
424 if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) {
425 fsr_info[nr].fn = fn;
426 fsr_info[nr].sig = sig;
427 fsr_info[nr].name = name;
432 * Dispatch a data abort to the relevant handler.
434 asmlinkage void
435 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
437 const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6);
439 if (!inf->fn(addr, fsr, regs))
440 return;
442 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
443 inf->name, fsr, addr);
444 force_sig(inf->sig, current);
445 show_pte(current->mm, addr);
446 die_if_kernel("Oops", regs, 0);
449 asmlinkage void
450 do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
452 do_translation_fault(addr, 0, regs);