blob: cc8829d7e1161822d5a2694d1a008a4ca312a9d8 [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>
Russell King67306da2008-12-14 18:01:44 +000014#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -050016#include <linux/kprobes.h>
Russell King33fa9b12008-09-06 11:35:55 +010017#include <linux/uaccess.h>
Nicolas Pitre252d4c22008-09-11 11:52:02 -040018#include <linux/page-flags.h>
Catalin Marinas412bb0a2009-07-24 12:37:09 +010019#include <linux/sched.h>
Russell King65cec8e2009-08-17 20:02:06 +010020#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/system.h>
23#include <asm/pgtable.h>
24#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "fault.h"
27
Catalin Marinas09529f72009-07-24 12:34:55 +010028#ifdef CONFIG_MMU
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -050029
30#ifdef CONFIG_KPROBES
31static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
32{
33 int ret = 0;
34
35 if (!user_mode(regs)) {
36 /* kprobe_running() needs smp_processor_id() */
37 preempt_disable();
38 if (kprobe_running() && kprobe_fault_handler(regs, fsr))
39 ret = 1;
40 preempt_enable();
41 }
42
43 return ret;
44}
45#else
46static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
47{
48 return 0;
49}
50#endif
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * This is useful to dump out the page tables associated with
54 * 'addr' in mm 'mm'.
55 */
56void show_pte(struct mm_struct *mm, unsigned long addr)
57{
58 pgd_t *pgd;
59
60 if (!mm)
61 mm = &init_mm;
62
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));
66
67 do {
68 pmd_t *pmd;
69 pte_t *pte;
70
71 if (pgd_none(*pgd))
72 break;
73
74 if (pgd_bad(*pgd)) {
75 printk("(bad)");
76 break;
77 }
78
79 pmd = pmd_offset(pgd, addr);
Nicolas Pitreda46c792008-09-30 16:10:11 +010080 if (PTRS_PER_PMD != 1)
81 printk(", *pmd=%08lx", pmd_val(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 if (pmd_none(*pmd))
84 break;
85
86 if (pmd_bad(*pmd)) {
87 printk("(bad)");
88 break;
89 }
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* We must not map this if we have highmem enabled */
Nicolas Pitre252d4c22008-09-11 11:52:02 -040092 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
93 break;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 pte = pte_offset_map(pmd, addr);
96 printk(", *pte=%08lx", pte_val(*pte));
97 printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE]));
98 pte_unmap(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 } while(0);
100
101 printk("\n");
102}
Catalin Marinas09529f72009-07-24 12:34:55 +0100103#else /* CONFIG_MMU */
104void show_pte(struct mm_struct *mm, unsigned long addr)
105{ }
106#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * Oops. The kernel tried to access some page that wasn't present.
110 */
111static void
112__do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
113 struct pt_regs *regs)
114{
115 /*
116 * Are we prepared to handle this kernel fault?
117 */
118 if (fixup_exception(regs))
119 return;
120
121 /*
122 * No handler, we'll have to terminate things with extreme prejudice.
123 */
124 bust_spinlocks(1);
125 printk(KERN_ALERT
126 "Unable to handle kernel %s at virtual address %08lx\n",
127 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
128 "paging request", addr);
129
130 show_pte(mm, addr);
131 die("Oops", regs, fsr);
132 bust_spinlocks(0);
133 do_exit(SIGKILL);
134}
135
136/*
137 * Something tried to access memory that isn't in our memory map..
138 * User mode accesses just cause a SIGSEGV
139 */
140static void
141__do_user_fault(struct task_struct *tsk, unsigned long addr,
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700142 unsigned int fsr, unsigned int sig, int code,
143 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct siginfo si;
146
147#ifdef CONFIG_DEBUG_USER
148 if (user_debug & UDBG_SEGV) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700149 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
150 tsk->comm, sig, addr, fsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 show_pte(tsk->mm, addr);
152 show_regs(regs);
153 }
154#endif
155
156 tsk->thread.address = addr;
157 tsk->thread.error_code = fsr;
158 tsk->thread.trap_no = 14;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700159 si.si_signo = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 si.si_errno = 0;
161 si.si_code = code;
162 si.si_addr = (void __user *)addr;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700163 force_sig_info(sig, &si, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Russell Kinge5beac32006-09-27 16:13:48 +0100166void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Russell Kinge5beac32006-09-27 16:13:48 +0100168 struct task_struct *tsk = current;
169 struct mm_struct *mm = tsk->active_mm;
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /*
172 * If we are in kernel mode at this point, we
173 * have no context to handle this fault with.
174 */
175 if (user_mode(regs))
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700176 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 else
178 __do_kernel_fault(mm, addr, fsr, regs);
179}
180
Catalin Marinas09529f72009-07-24 12:34:55 +0100181#ifdef CONFIG_MMU
Nick Piggin5c72fc52007-07-20 09:21:06 +0200182#define VM_FAULT_BADMAP 0x010000
183#define VM_FAULT_BADACCESS 0x020000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185static int
186__do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
187 struct task_struct *tsk)
188{
189 struct vm_area_struct *vma;
190 int fault, mask;
191
192 vma = find_vma(mm, addr);
193 fault = VM_FAULT_BADMAP;
194 if (!vma)
195 goto out;
196 if (vma->vm_start > addr)
197 goto check_stack;
198
199 /*
200 * Ok, we have a good vm_area for this
201 * memory access, so we can handle it.
202 */
203good_area:
204 if (fsr & (1 << 11)) /* write? */
205 mask = VM_WRITE;
206 else
Jason Barondf67b3d2006-09-29 01:58:58 -0700207 mask = VM_READ|VM_EXEC|VM_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 fault = VM_FAULT_BADACCESS;
210 if (!(vma->vm_flags & mask))
211 goto out;
212
213 /*
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.
217 */
218survive:
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700219 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & (1 << 11)) ? FAULT_FLAG_WRITE : 0);
Nick Piggin83c54072007-07-19 01:47:05 -0700220 if (unlikely(fault & VM_FAULT_ERROR)) {
221 if (fault & VM_FAULT_OOM)
222 goto out_of_memory;
223 else if (fault & VM_FAULT_SIGBUS)
224 return fault;
225 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Nick Piggin83c54072007-07-19 01:47:05 -0700227 if (fault & VM_FAULT_MAJOR)
228 tsk->maj_flt++;
229 else
230 tsk->min_flt++;
231 return fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Nick Piggin83c54072007-07-19 01:47:05 -0700233out_of_memory:
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700234 if (!is_global_init(tsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 goto out;
236
237 /*
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700238 * If we are out of memory for pid1, sleep for a while and retry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700240 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 yield();
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700242 down_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto survive;
244
245check_stack:
246 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
247 goto good_area;
248out:
249 return fault;
250}
251
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500252static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
254{
255 struct task_struct *tsk;
256 struct mm_struct *mm;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700257 int fault, sig, code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -0500259 if (notify_page_fault(regs, fsr))
260 return 0;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 tsk = current;
263 mm = tsk->mm;
264
265 /*
266 * If we're in an interrupt or have no user
267 * context, we must not take the fault..
268 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -0800269 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto no_context;
271
Russell King840ff6a2005-09-20 17:52:13 +0100272 /*
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.
276 */
277 if (!down_read_trylock(&mm->mmap_sem)) {
278 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
279 goto no_context;
280 down_read(&mm->mmap_sem);
281 }
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 fault = __do_page_fault(mm, addr, fsr, tsk);
284 up_read(&mm->mmap_sem);
285
286 /*
Russell Kingff2afb92005-08-04 14:17:33 +0100287 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Nick Piggin5c72fc52007-07-20 09:21:06 +0200289 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return 0;
291
292 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * If we are in kernel mode at this point, we
294 * have no context to handle this fault with.
295 */
296 if (!user_mode(regs))
297 goto no_context;
298
Nick Piggin83c54072007-07-19 01:47:05 -0700299 if (fault & VM_FAULT_OOM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /*
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700301 * We ran out of memory, or some other thing
302 * happened to us that made us unable to handle
303 * the page fault gracefully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305 printk("VM: killing process %s\n", tsk->comm);
Will Schmidtdcca2bd2007-10-16 01:24:18 -0700306 do_group_exit(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
Nick Piggin83c54072007-07-19 01:47:05 -0700308 }
309 if (fault & VM_FAULT_SIGBUS) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700310 /*
311 * We had some memory, but were unable to
312 * successfully fix up this page fault.
313 */
314 sig = SIGBUS;
315 code = BUS_ADRERR;
Nick Piggin83c54072007-07-19 01:47:05 -0700316 } else {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700317 /*
318 * Something tried to access memory that
319 * isn't in our memory map..
320 */
321 sig = SIGSEGV;
322 code = fault == VM_FAULT_BADACCESS ?
323 SEGV_ACCERR : SEGV_MAPERR;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700324 }
325
326 __do_user_fault(tsk, addr, fsr, sig, code, regs);
327 return 0;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329no_context:
330 __do_kernel_fault(mm, addr, fsr, regs);
331 return 0;
332}
Catalin Marinas09529f72009-07-24 12:34:55 +0100333#else /* CONFIG_MMU */
334static int
335do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
336{
337 return 0;
338}
339#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341/*
342 * First Level Translation Fault Handler
343 *
344 * We enter here because the first level page table doesn't contain
345 * a valid entry for the address.
346 *
347 * If the address is in kernel space (>= TASK_SIZE), then we are
348 * probably faulting in the vmalloc() area.
349 *
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.
353 *
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.
357 */
Catalin Marinas09529f72009-07-24 12:34:55 +0100358#ifdef CONFIG_MMU
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500359static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360do_translation_fault(unsigned long addr, unsigned int fsr,
361 struct pt_regs *regs)
362{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 unsigned int index;
364 pgd_t *pgd, *pgd_k;
365 pmd_t *pmd, *pmd_k;
366
367 if (addr < TASK_SIZE)
368 return do_page_fault(addr, fsr, regs);
369
370 index = pgd_index(addr);
371
372 /*
373 * FIXME: CP15 C1 is write only on ARMv3 architectures.
374 */
375 pgd = cpu_get_pgd() + index;
376 pgd_k = init_mm.pgd + index;
377
378 if (pgd_none(*pgd_k))
379 goto bad_area;
380
381 if (!pgd_present(*pgd))
382 set_pgd(pgd, *pgd_k);
383
384 pmd_k = pmd_offset(pgd_k, addr);
385 pmd = pmd_offset(pgd, addr);
386
387 if (pmd_none(*pmd_k))
388 goto bad_area;
389
390 copy_pmd(pmd, pmd_k);
391 return 0;
392
393bad_area:
Russell Kinge5beac32006-09-27 16:13:48 +0100394 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 0;
396}
Catalin Marinas09529f72009-07-24 12:34:55 +0100397#else /* CONFIG_MMU */
398static int
399do_translation_fault(unsigned long addr, unsigned int fsr,
400 struct pt_regs *regs)
401{
402 return 0;
403}
404#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406/*
407 * Some section permission faults need to be handled gracefully.
408 * They can happen due to a __{get,put}_user during an oops.
409 */
410static int
411do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
412{
Russell Kinge5beac32006-09-27 16:13:48 +0100413 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return 0;
415}
416
417/*
418 * This abort handler always returns "fault".
419 */
420static int
421do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
422{
423 return 1;
424}
425
426static struct fsr_info {
427 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
428 int sig;
Russell Kingcfb08102005-06-30 11:06:49 +0100429 int code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 const char *name;
431} fsr_info[] = {
432 /*
433 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
434 * defines these to be "precise" aborts.
435 */
Russell Kingcfb08102005-06-30 11:06:49 +0100436 { 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" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /*
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.
456 */
Russell Kingcfb08102005-06-30 11:06:49 +0100457 { 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" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473};
474
475void __init
476hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
477 int sig, const char *name)
478{
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;
483 }
484}
485
486/*
487 * Dispatch a data abort to the relevant handler.
488 */
Russell King7ab3f8d2007-03-02 15:01:36 +0000489asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
491{
492 const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6);
Russell Kingcfb08102005-06-30 11:06:49 +0100493 struct siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if (!inf->fn(addr, fsr, regs))
496 return;
497
498 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
499 inf->name, fsr, addr);
Russell Kingcfb08102005-06-30 11:06:49 +0100500
501 info.si_signo = inf->sig;
502 info.si_errno = 0;
503 info.si_code = inf->code;
504 info.si_addr = (void __user *)addr;
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700505 arm_notify_die("", regs, &info, fsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Russell King7ab3f8d2007-03-02 15:01:36 +0000508asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
510{
511 do_translation_fault(addr, 0, regs);
512}
513