blob: 59ed1d05b71bf6d858e515d928da4ad102276d91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mm/fault.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 * Modifications for ARM processor (c) 1995-2004 Russell King
6 *
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.
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/mm.h>
14#include <linux/init.h>
15
16#include <asm/system.h>
17#include <asm/pgtable.h>
18#include <asm/tlbflush.h>
19#include <asm/uaccess.h>
20
21#include "fault.h"
22
23/*
24 * This is useful to dump out the page tables associated with
25 * 'addr' in mm 'mm'.
26 */
27void show_pte(struct mm_struct *mm, unsigned long addr)
28{
29 pgd_t *pgd;
30
31 if (!mm)
32 mm = &init_mm;
33
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));
37
38 do {
39 pmd_t *pmd;
40 pte_t *pte;
41
42 if (pgd_none(*pgd))
43 break;
44
45 if (pgd_bad(*pgd)) {
46 printk("(bad)");
47 break;
48 }
49
50 pmd = pmd_offset(pgd, addr);
51#if PTRS_PER_PMD != 1
52 printk(", *pmd=%08lx", pmd_val(*pmd));
53#endif
54
55 if (pmd_none(*pmd))
56 break;
57
58 if (pmd_bad(*pmd)) {
59 printk("(bad)");
60 break;
61 }
62
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]));
68 pte_unmap(pte);
69#endif
70 } while(0);
71
72 printk("\n");
73}
74
75/*
76 * Oops. The kernel tried to access some page that wasn't present.
77 */
78static void
79__do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
80 struct pt_regs *regs)
81{
82 /*
83 * Are we prepared to handle this kernel fault?
84 */
85 if (fixup_exception(regs))
86 return;
87
88 /*
89 * No handler, we'll have to terminate things with extreme prejudice.
90 */
91 bust_spinlocks(1);
92 printk(KERN_ALERT
93 "Unable to handle kernel %s at virtual address %08lx\n",
94 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
95 "paging request", addr);
96
97 show_pte(mm, addr);
98 die("Oops", regs, fsr);
99 bust_spinlocks(0);
100 do_exit(SIGKILL);
101}
102
103/*
104 * Something tried to access memory that isn't in our memory map..
105 * User mode accesses just cause a SIGSEGV
106 */
107static void
108__do_user_fault(struct task_struct *tsk, unsigned long addr,
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700109 unsigned int fsr, unsigned int sig, int code,
110 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct siginfo si;
113
114#ifdef CONFIG_DEBUG_USER
115 if (user_debug & UDBG_SEGV) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700116 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
117 tsk->comm, sig, addr, fsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 show_pte(tsk->mm, addr);
119 show_regs(regs);
120 }
121#endif
122
123 tsk->thread.address = addr;
124 tsk->thread.error_code = fsr;
125 tsk->thread.trap_no = 14;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700126 si.si_signo = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 si.si_errno = 0;
128 si.si_code = code;
129 si.si_addr = (void __user *)addr;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700130 force_sig_info(sig, &si, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Russell Kinge5beac32006-09-27 16:13:48 +0100133void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Russell Kinge5beac32006-09-27 16:13:48 +0100135 struct task_struct *tsk = current;
136 struct mm_struct *mm = tsk->active_mm;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /*
139 * If we are in kernel mode at this point, we
140 * have no context to handle this fault with.
141 */
142 if (user_mode(regs))
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700143 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 else
145 __do_kernel_fault(mm, addr, fsr, regs);
146}
147
Nick Piggin5c72fc52007-07-20 09:21:06 +0200148#define VM_FAULT_BADMAP 0x010000
149#define VM_FAULT_BADACCESS 0x020000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151static int
152__do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
153 struct task_struct *tsk)
154{
155 struct vm_area_struct *vma;
156 int fault, mask;
157
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;
164
165 /*
166 * Ok, we have a good vm_area for this
167 * memory access, so we can handle it.
168 */
169good_area:
170 if (fsr & (1 << 11)) /* write? */
171 mask = VM_WRITE;
172 else
Jason Barondf67b3d2006-09-29 01:58:58 -0700173 mask = VM_READ|VM_EXEC|VM_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 fault = VM_FAULT_BADACCESS;
176 if (!(vma->vm_flags & mask))
177 goto out;
178
179 /*
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.
183 */
184survive:
185 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, fsr & (1 << 11));
Nick Piggin83c54072007-07-19 01:47:05 -0700186 if (unlikely(fault & VM_FAULT_ERROR)) {
187 if (fault & VM_FAULT_OOM)
188 goto out_of_memory;
189 else if (fault & VM_FAULT_SIGBUS)
190 return fault;
191 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Nick Piggin83c54072007-07-19 01:47:05 -0700193 if (fault & VM_FAULT_MAJOR)
194 tsk->maj_flt++;
195 else
196 tsk->min_flt++;
197 return fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Nick Piggin83c54072007-07-19 01:47:05 -0700199out_of_memory:
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700200 if (!is_init(tsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto out;
202
203 /*
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700204 * If we are out of memory for pid1, sleep for a while and retry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700206 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 yield();
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700208 down_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto survive;
210
211check_stack:
212 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
213 goto good_area;
214out:
215 return fault;
216}
217
218static int
219do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
220{
221 struct task_struct *tsk;
222 struct mm_struct *mm;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700223 int fault, sig, code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 tsk = current;
226 mm = tsk->mm;
227
228 /*
229 * If we're in an interrupt or have no user
230 * context, we must not take the fault..
231 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -0800232 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 goto no_context;
234
Russell King840ff6a2005-09-20 17:52:13 +0100235 /*
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.
239 */
240 if (!down_read_trylock(&mm->mmap_sem)) {
241 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
242 goto no_context;
243 down_read(&mm->mmap_sem);
244 }
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 fault = __do_page_fault(mm, addr, fsr, tsk);
247 up_read(&mm->mmap_sem);
248
249 /*
Russell Kingff2afb92005-08-04 14:17:33 +0100250 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
Nick Piggin5c72fc52007-07-20 09:21:06 +0200252 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254
255 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * If we are in kernel mode at this point, we
257 * have no context to handle this fault with.
258 */
259 if (!user_mode(regs))
260 goto no_context;
261
Nick Piggin83c54072007-07-19 01:47:05 -0700262 if (fault & VM_FAULT_OOM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /*
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700264 * We ran out of memory, or some other thing
265 * happened to us that made us unable to handle
266 * the page fault gracefully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
268 printk("VM: killing process %s\n", tsk->comm);
Will Schmidtdcca2bd2007-10-16 01:24:18 -0700269 do_group_exit(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 0;
Nick Piggin83c54072007-07-19 01:47:05 -0700271 }
272 if (fault & VM_FAULT_SIGBUS) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700273 /*
274 * We had some memory, but were unable to
275 * successfully fix up this page fault.
276 */
277 sig = SIGBUS;
278 code = BUS_ADRERR;
Nick Piggin83c54072007-07-19 01:47:05 -0700279 } else {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700280 /*
281 * Something tried to access memory that
282 * isn't in our memory map..
283 */
284 sig = SIGSEGV;
285 code = fault == VM_FAULT_BADACCESS ?
286 SEGV_ACCERR : SEGV_MAPERR;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700287 }
288
289 __do_user_fault(tsk, addr, fsr, sig, code, regs);
290 return 0;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292no_context:
293 __do_kernel_fault(mm, addr, fsr, regs);
294 return 0;
295}
296
297/*
298 * First Level Translation Fault Handler
299 *
300 * We enter here because the first level page table doesn't contain
301 * a valid entry for the address.
302 *
303 * If the address is in kernel space (>= TASK_SIZE), then we are
304 * probably faulting in the vmalloc() area.
305 *
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.
309 *
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.
313 */
314static int
315do_translation_fault(unsigned long addr, unsigned int fsr,
316 struct pt_regs *regs)
317{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unsigned int index;
319 pgd_t *pgd, *pgd_k;
320 pmd_t *pmd, *pmd_k;
321
322 if (addr < TASK_SIZE)
323 return do_page_fault(addr, fsr, regs);
324
325 index = pgd_index(addr);
326
327 /*
328 * FIXME: CP15 C1 is write only on ARMv3 architectures.
329 */
330 pgd = cpu_get_pgd() + index;
331 pgd_k = init_mm.pgd + index;
332
333 if (pgd_none(*pgd_k))
334 goto bad_area;
335
336 if (!pgd_present(*pgd))
337 set_pgd(pgd, *pgd_k);
338
339 pmd_k = pmd_offset(pgd_k, addr);
340 pmd = pmd_offset(pgd, addr);
341
342 if (pmd_none(*pmd_k))
343 goto bad_area;
344
345 copy_pmd(pmd, pmd_k);
346 return 0;
347
348bad_area:
Russell Kinge5beac32006-09-27 16:13:48 +0100349 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
351}
352
353/*
354 * Some section permission faults need to be handled gracefully.
355 * They can happen due to a __{get,put}_user during an oops.
356 */
357static int
358do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
359{
Russell Kinge5beac32006-09-27 16:13:48 +0100360 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
362}
363
364/*
365 * This abort handler always returns "fault".
366 */
367static int
368do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
369{
370 return 1;
371}
372
373static struct fsr_info {
374 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
375 int sig;
Russell Kingcfb08102005-06-30 11:06:49 +0100376 int code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 const char *name;
378} fsr_info[] = {
379 /*
380 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
381 * defines these to be "precise" aborts.
382 */
Russell Kingcfb08102005-06-30 11:06:49 +0100383 { 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" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /*
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.
403 */
Russell Kingcfb08102005-06-30 11:06:49 +0100404 { 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" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420};
421
422void __init
423hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
424 int sig, const char *name)
425{
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;
430 }
431}
432
433/*
434 * Dispatch a data abort to the relevant handler.
435 */
Russell King7ab3f8d2007-03-02 15:01:36 +0000436asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
438{
439 const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6);
Russell Kingcfb08102005-06-30 11:06:49 +0100440 struct siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 if (!inf->fn(addr, fsr, regs))
443 return;
444
445 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
446 inf->name, fsr, addr);
Russell Kingcfb08102005-06-30 11:06:49 +0100447
448 info.si_signo = inf->sig;
449 info.si_errno = 0;
450 info.si_code = inf->code;
451 info.si_addr = (void __user *)addr;
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700452 arm_notify_die("", regs, &info, fsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Russell King7ab3f8d2007-03-02 15:01:36 +0000455asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
457{
458 do_translation_fault(addr, 0, regs);
459}
460