blob: 5fa8dea5a3716173dc92c09b2f5a8540b7a9868a [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>
Russell King65cec8e2009-08-17 20:02:06 +010019#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/system.h>
22#include <asm/pgtable.h>
23#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "fault.h"
26
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -050027
28#ifdef CONFIG_KPROBES
29static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
30{
31 int ret = 0;
32
33 if (!user_mode(regs)) {
34 /* kprobe_running() needs smp_processor_id() */
35 preempt_disable();
36 if (kprobe_running() && kprobe_fault_handler(regs, fsr))
37 ret = 1;
38 preempt_enable();
39 }
40
41 return ret;
42}
43#else
44static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
45{
46 return 0;
47}
48#endif
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * This is useful to dump out the page tables associated with
52 * 'addr' in mm 'mm'.
53 */
54void show_pte(struct mm_struct *mm, unsigned long addr)
55{
56 pgd_t *pgd;
57
58 if (!mm)
59 mm = &init_mm;
60
61 printk(KERN_ALERT "pgd = %p\n", mm->pgd);
62 pgd = pgd_offset(mm, addr);
63 printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
64
65 do {
66 pmd_t *pmd;
67 pte_t *pte;
68
69 if (pgd_none(*pgd))
70 break;
71
72 if (pgd_bad(*pgd)) {
73 printk("(bad)");
74 break;
75 }
76
77 pmd = pmd_offset(pgd, addr);
Nicolas Pitreda46c792008-09-30 16:10:11 +010078 if (PTRS_PER_PMD != 1)
79 printk(", *pmd=%08lx", pmd_val(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 if (pmd_none(*pmd))
82 break;
83
84 if (pmd_bad(*pmd)) {
85 printk("(bad)");
86 break;
87 }
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /* We must not map this if we have highmem enabled */
Nicolas Pitre252d4c22008-09-11 11:52:02 -040090 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
91 break;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 pte = pte_offset_map(pmd, addr);
94 printk(", *pte=%08lx", pte_val(*pte));
95 printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE]));
96 pte_unmap(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 } while(0);
98
99 printk("\n");
100}
101
102/*
103 * Oops. The kernel tried to access some page that wasn't present.
104 */
105static void
106__do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
107 struct pt_regs *regs)
108{
109 /*
110 * Are we prepared to handle this kernel fault?
111 */
112 if (fixup_exception(regs))
113 return;
114
115 /*
116 * No handler, we'll have to terminate things with extreme prejudice.
117 */
118 bust_spinlocks(1);
119 printk(KERN_ALERT
120 "Unable to handle kernel %s at virtual address %08lx\n",
121 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
122 "paging request", addr);
123
124 show_pte(mm, addr);
125 die("Oops", regs, fsr);
126 bust_spinlocks(0);
127 do_exit(SIGKILL);
128}
129
130/*
131 * Something tried to access memory that isn't in our memory map..
132 * User mode accesses just cause a SIGSEGV
133 */
134static void
135__do_user_fault(struct task_struct *tsk, unsigned long addr,
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700136 unsigned int fsr, unsigned int sig, int code,
137 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 struct siginfo si;
140
141#ifdef CONFIG_DEBUG_USER
142 if (user_debug & UDBG_SEGV) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700143 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
144 tsk->comm, sig, addr, fsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 show_pte(tsk->mm, addr);
146 show_regs(regs);
147 }
148#endif
149
150 tsk->thread.address = addr;
151 tsk->thread.error_code = fsr;
152 tsk->thread.trap_no = 14;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700153 si.si_signo = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 si.si_errno = 0;
155 si.si_code = code;
156 si.si_addr = (void __user *)addr;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700157 force_sig_info(sig, &si, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Russell Kinge5beac32006-09-27 16:13:48 +0100160void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Russell Kinge5beac32006-09-27 16:13:48 +0100162 struct task_struct *tsk = current;
163 struct mm_struct *mm = tsk->active_mm;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
166 * If we are in kernel mode at this point, we
167 * have no context to handle this fault with.
168 */
169 if (user_mode(regs))
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700170 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 else
172 __do_kernel_fault(mm, addr, fsr, regs);
173}
174
Nick Piggin5c72fc52007-07-20 09:21:06 +0200175#define VM_FAULT_BADMAP 0x010000
176#define VM_FAULT_BADACCESS 0x020000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178static int
179__do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
180 struct task_struct *tsk)
181{
182 struct vm_area_struct *vma;
183 int fault, mask;
184
185 vma = find_vma(mm, addr);
186 fault = VM_FAULT_BADMAP;
187 if (!vma)
188 goto out;
189 if (vma->vm_start > addr)
190 goto check_stack;
191
192 /*
193 * Ok, we have a good vm_area for this
194 * memory access, so we can handle it.
195 */
196good_area:
197 if (fsr & (1 << 11)) /* write? */
198 mask = VM_WRITE;
199 else
Jason Barondf67b3d2006-09-29 01:58:58 -0700200 mask = VM_READ|VM_EXEC|VM_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 fault = VM_FAULT_BADACCESS;
203 if (!(vma->vm_flags & mask))
204 goto out;
205
206 /*
207 * If for any reason at all we couldn't handle
208 * the fault, make sure we exit gracefully rather
209 * than endlessly redo the fault.
210 */
211survive:
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700212 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & (1 << 11)) ? FAULT_FLAG_WRITE : 0);
Nick Piggin83c54072007-07-19 01:47:05 -0700213 if (unlikely(fault & VM_FAULT_ERROR)) {
214 if (fault & VM_FAULT_OOM)
215 goto out_of_memory;
216 else if (fault & VM_FAULT_SIGBUS)
217 return fault;
218 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Nick Piggin83c54072007-07-19 01:47:05 -0700220 if (fault & VM_FAULT_MAJOR)
221 tsk->maj_flt++;
222 else
223 tsk->min_flt++;
224 return fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Nick Piggin83c54072007-07-19 01:47:05 -0700226out_of_memory:
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700227 if (!is_global_init(tsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto out;
229
230 /*
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700231 * If we are out of memory for pid1, sleep for a while and retry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 */
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700233 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 yield();
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700235 down_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto survive;
237
238check_stack:
239 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
240 goto good_area;
241out:
242 return fault;
243}
244
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500245static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
247{
248 struct task_struct *tsk;
249 struct mm_struct *mm;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700250 int fault, sig, code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -0500252 if (notify_page_fault(regs, fsr))
253 return 0;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 tsk = current;
256 mm = tsk->mm;
257
258 /*
259 * If we're in an interrupt or have no user
260 * context, we must not take the fault..
261 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -0800262 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 goto no_context;
264
Russell King840ff6a2005-09-20 17:52:13 +0100265 /*
266 * As per x86, we may deadlock here. However, since the kernel only
267 * validly references user space from well defined areas of the code,
268 * we can bug out early if this is from code which shouldn't.
269 */
270 if (!down_read_trylock(&mm->mmap_sem)) {
271 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
272 goto no_context;
273 down_read(&mm->mmap_sem);
274 }
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 fault = __do_page_fault(mm, addr, fsr, tsk);
277 up_read(&mm->mmap_sem);
278
279 /*
Russell Kingff2afb92005-08-04 14:17:33 +0100280 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Nick Piggin5c72fc52007-07-20 09:21:06 +0200282 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284
285 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * If we are in kernel mode at this point, we
287 * have no context to handle this fault with.
288 */
289 if (!user_mode(regs))
290 goto no_context;
291
Nick Piggin83c54072007-07-19 01:47:05 -0700292 if (fault & VM_FAULT_OOM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /*
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700294 * We ran out of memory, or some other thing
295 * happened to us that made us unable to handle
296 * the page fault gracefully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
298 printk("VM: killing process %s\n", tsk->comm);
Will Schmidtdcca2bd2007-10-16 01:24:18 -0700299 do_group_exit(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0;
Nick Piggin83c54072007-07-19 01:47:05 -0700301 }
302 if (fault & VM_FAULT_SIGBUS) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700303 /*
304 * We had some memory, but were unable to
305 * successfully fix up this page fault.
306 */
307 sig = SIGBUS;
308 code = BUS_ADRERR;
Nick Piggin83c54072007-07-19 01:47:05 -0700309 } else {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700310 /*
311 * Something tried to access memory that
312 * isn't in our memory map..
313 */
314 sig = SIGSEGV;
315 code = fault == VM_FAULT_BADACCESS ?
316 SEGV_ACCERR : SEGV_MAPERR;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700317 }
318
319 __do_user_fault(tsk, addr, fsr, sig, code, regs);
320 return 0;
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322no_context:
323 __do_kernel_fault(mm, addr, fsr, regs);
324 return 0;
325}
326
327/*
328 * First Level Translation Fault Handler
329 *
330 * We enter here because the first level page table doesn't contain
331 * a valid entry for the address.
332 *
333 * If the address is in kernel space (>= TASK_SIZE), then we are
334 * probably faulting in the vmalloc() area.
335 *
336 * If the init_task's first level page tables contains the relevant
337 * entry, we copy the it to this task. If not, we send the process
338 * a signal, fixup the exception, or oops the kernel.
339 *
340 * NOTE! We MUST NOT take any locks for this case. We may be in an
341 * interrupt or a critical region, and should only copy the information
342 * from the master page table, nothing more.
343 */
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500344static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345do_translation_fault(unsigned long addr, unsigned int fsr,
346 struct pt_regs *regs)
347{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned int index;
349 pgd_t *pgd, *pgd_k;
350 pmd_t *pmd, *pmd_k;
351
352 if (addr < TASK_SIZE)
353 return do_page_fault(addr, fsr, regs);
354
355 index = pgd_index(addr);
356
357 /*
358 * FIXME: CP15 C1 is write only on ARMv3 architectures.
359 */
360 pgd = cpu_get_pgd() + index;
361 pgd_k = init_mm.pgd + index;
362
363 if (pgd_none(*pgd_k))
364 goto bad_area;
365
366 if (!pgd_present(*pgd))
367 set_pgd(pgd, *pgd_k);
368
369 pmd_k = pmd_offset(pgd_k, addr);
370 pmd = pmd_offset(pgd, addr);
371
372 if (pmd_none(*pmd_k))
373 goto bad_area;
374
375 copy_pmd(pmd, pmd_k);
376 return 0;
377
378bad_area:
Russell Kinge5beac32006-09-27 16:13:48 +0100379 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 0;
381}
382
383/*
384 * Some section permission faults need to be handled gracefully.
385 * They can happen due to a __{get,put}_user during an oops.
386 */
387static int
388do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
389{
Russell Kinge5beac32006-09-27 16:13:48 +0100390 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392}
393
394/*
395 * This abort handler always returns "fault".
396 */
397static int
398do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
399{
400 return 1;
401}
402
403static struct fsr_info {
404 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
405 int sig;
Russell Kingcfb08102005-06-30 11:06:49 +0100406 int code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 const char *name;
408} fsr_info[] = {
409 /*
410 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
411 * defines these to be "precise" aborts.
412 */
Russell Kingcfb08102005-06-30 11:06:49 +0100413 { do_bad, SIGSEGV, 0, "vector exception" },
414 { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
415 { do_bad, SIGKILL, 0, "terminal exception" },
416 { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
417 { do_bad, SIGBUS, 0, "external abort on linefetch" },
418 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
419 { do_bad, SIGBUS, 0, "external abort on linefetch" },
420 { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
421 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
422 { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
423 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
424 { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
425 { do_bad, SIGBUS, 0, "external abort on translation" },
426 { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
427 { do_bad, SIGBUS, 0, "external abort on translation" },
428 { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /*
430 * The following are "imprecise" aborts, which are signalled by bit
431 * 10 of the FSR, and may not be recoverable. These are only
432 * supported if the CPU abort handler supports bit 10.
433 */
Russell Kingcfb08102005-06-30 11:06:49 +0100434 { do_bad, SIGBUS, 0, "unknown 16" },
435 { do_bad, SIGBUS, 0, "unknown 17" },
436 { do_bad, SIGBUS, 0, "unknown 18" },
437 { do_bad, SIGBUS, 0, "unknown 19" },
438 { do_bad, SIGBUS, 0, "lock abort" }, /* xscale */
439 { do_bad, SIGBUS, 0, "unknown 21" },
440 { do_bad, SIGBUS, BUS_OBJERR, "imprecise external abort" }, /* xscale */
441 { do_bad, SIGBUS, 0, "unknown 23" },
442 { do_bad, SIGBUS, 0, "dcache parity error" }, /* xscale */
443 { do_bad, SIGBUS, 0, "unknown 25" },
444 { do_bad, SIGBUS, 0, "unknown 26" },
445 { do_bad, SIGBUS, 0, "unknown 27" },
446 { do_bad, SIGBUS, 0, "unknown 28" },
447 { do_bad, SIGBUS, 0, "unknown 29" },
448 { do_bad, SIGBUS, 0, "unknown 30" },
449 { do_bad, SIGBUS, 0, "unknown 31" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450};
451
452void __init
453hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
454 int sig, const char *name)
455{
456 if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) {
457 fsr_info[nr].fn = fn;
458 fsr_info[nr].sig = sig;
459 fsr_info[nr].name = name;
460 }
461}
462
463/*
464 * Dispatch a data abort to the relevant handler.
465 */
Russell King7ab3f8d2007-03-02 15:01:36 +0000466asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
468{
469 const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6);
Russell Kingcfb08102005-06-30 11:06:49 +0100470 struct siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (!inf->fn(addr, fsr, regs))
473 return;
474
475 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
476 inf->name, fsr, addr);
Russell Kingcfb08102005-06-30 11:06:49 +0100477
478 info.si_signo = inf->sig;
479 info.si_errno = 0;
480 info.si_code = inf->code;
481 info.si_addr = (void __user *)addr;
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700482 arm_notify_die("", regs, &info, fsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Russell King7ab3f8d2007-03-02 15:01:36 +0000485asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
487{
488 do_translation_fault(addr, 0, regs);
489}
490