blob: 556c8daf087dd0febf52a36ba004ec2e778944b5 [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>
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
Catalin Marinas09529f72009-07-24 12:34:55 +010027#ifdef CONFIG_MMU
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -050028
29#ifdef CONFIG_KPROBES
30static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
31{
32 int ret = 0;
33
34 if (!user_mode(regs)) {
35 /* kprobe_running() needs smp_processor_id() */
36 preempt_disable();
37 if (kprobe_running() && kprobe_fault_handler(regs, fsr))
38 ret = 1;
39 preempt_enable();
40 }
41
42 return ret;
43}
44#else
45static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
46{
47 return 0;
48}
49#endif
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * This is useful to dump out the page tables associated with
53 * 'addr' in mm 'mm'.
54 */
55void show_pte(struct mm_struct *mm, unsigned long addr)
56{
57 pgd_t *pgd;
58
59 if (!mm)
60 mm = &init_mm;
61
62 printk(KERN_ALERT "pgd = %p\n", mm->pgd);
63 pgd = pgd_offset(mm, addr);
64 printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
65
66 do {
67 pmd_t *pmd;
68 pte_t *pte;
69
70 if (pgd_none(*pgd))
71 break;
72
73 if (pgd_bad(*pgd)) {
74 printk("(bad)");
75 break;
76 }
77
78 pmd = pmd_offset(pgd, addr);
Nicolas Pitreda46c792008-09-30 16:10:11 +010079 if (PTRS_PER_PMD != 1)
80 printk(", *pmd=%08lx", pmd_val(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 if (pmd_none(*pmd))
83 break;
84
85 if (pmd_bad(*pmd)) {
86 printk("(bad)");
87 break;
88 }
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /* We must not map this if we have highmem enabled */
Nicolas Pitre252d4c22008-09-11 11:52:02 -040091 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
92 break;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 pte = pte_offset_map(pmd, addr);
95 printk(", *pte=%08lx", pte_val(*pte));
96 printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE]));
97 pte_unmap(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 } while(0);
99
100 printk("\n");
101}
Catalin Marinas09529f72009-07-24 12:34:55 +0100102#else /* CONFIG_MMU */
103void show_pte(struct mm_struct *mm, unsigned long addr)
104{ }
105#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * Oops. The kernel tried to access some page that wasn't present.
109 */
110static void
111__do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
112 struct pt_regs *regs)
113{
114 /*
115 * Are we prepared to handle this kernel fault?
116 */
117 if (fixup_exception(regs))
118 return;
119
120 /*
121 * No handler, we'll have to terminate things with extreme prejudice.
122 */
123 bust_spinlocks(1);
124 printk(KERN_ALERT
125 "Unable to handle kernel %s at virtual address %08lx\n",
126 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
127 "paging request", addr);
128
129 show_pte(mm, addr);
130 die("Oops", regs, fsr);
131 bust_spinlocks(0);
132 do_exit(SIGKILL);
133}
134
135/*
136 * Something tried to access memory that isn't in our memory map..
137 * User mode accesses just cause a SIGSEGV
138 */
139static void
140__do_user_fault(struct task_struct *tsk, unsigned long addr,
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700141 unsigned int fsr, unsigned int sig, int code,
142 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct siginfo si;
145
146#ifdef CONFIG_DEBUG_USER
147 if (user_debug & UDBG_SEGV) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700148 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
149 tsk->comm, sig, addr, fsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 show_pte(tsk->mm, addr);
151 show_regs(regs);
152 }
153#endif
154
155 tsk->thread.address = addr;
156 tsk->thread.error_code = fsr;
157 tsk->thread.trap_no = 14;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700158 si.si_signo = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 si.si_errno = 0;
160 si.si_code = code;
161 si.si_addr = (void __user *)addr;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700162 force_sig_info(sig, &si, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Russell Kinge5beac32006-09-27 16:13:48 +0100165void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Russell Kinge5beac32006-09-27 16:13:48 +0100167 struct task_struct *tsk = current;
168 struct mm_struct *mm = tsk->active_mm;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /*
171 * If we are in kernel mode at this point, we
172 * have no context to handle this fault with.
173 */
174 if (user_mode(regs))
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700175 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 else
177 __do_kernel_fault(mm, addr, fsr, regs);
178}
179
Catalin Marinas09529f72009-07-24 12:34:55 +0100180#ifdef CONFIG_MMU
Nick Piggin5c72fc52007-07-20 09:21:06 +0200181#define VM_FAULT_BADMAP 0x010000
182#define VM_FAULT_BADACCESS 0x020000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184static int
185__do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
186 struct task_struct *tsk)
187{
188 struct vm_area_struct *vma;
189 int fault, mask;
190
191 vma = find_vma(mm, addr);
192 fault = VM_FAULT_BADMAP;
193 if (!vma)
194 goto out;
195 if (vma->vm_start > addr)
196 goto check_stack;
197
198 /*
199 * Ok, we have a good vm_area for this
200 * memory access, so we can handle it.
201 */
202good_area:
203 if (fsr & (1 << 11)) /* write? */
204 mask = VM_WRITE;
205 else
Jason Barondf67b3d2006-09-29 01:58:58 -0700206 mask = VM_READ|VM_EXEC|VM_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 fault = VM_FAULT_BADACCESS;
209 if (!(vma->vm_flags & mask))
210 goto out;
211
212 /*
213 * If for any reason at all we couldn't handle
214 * the fault, make sure we exit gracefully rather
215 * than endlessly redo the fault.
216 */
217survive:
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700218 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & (1 << 11)) ? FAULT_FLAG_WRITE : 0);
Nick Piggin83c54072007-07-19 01:47:05 -0700219 if (unlikely(fault & VM_FAULT_ERROR)) {
220 if (fault & VM_FAULT_OOM)
221 goto out_of_memory;
222 else if (fault & VM_FAULT_SIGBUS)
223 return fault;
224 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Nick Piggin83c54072007-07-19 01:47:05 -0700226 if (fault & VM_FAULT_MAJOR)
227 tsk->maj_flt++;
228 else
229 tsk->min_flt++;
230 return fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Nick Piggin83c54072007-07-19 01:47:05 -0700232out_of_memory:
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700233 if (!is_global_init(tsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto out;
235
236 /*
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700237 * If we are out of memory for pid1, sleep for a while and retry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700239 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 yield();
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700241 down_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto survive;
243
244check_stack:
245 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
246 goto good_area;
247out:
248 return fault;
249}
250
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500251static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
253{
254 struct task_struct *tsk;
255 struct mm_struct *mm;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700256 int fault, sig, code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -0500258 if (notify_page_fault(regs, fsr))
259 return 0;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 tsk = current;
262 mm = tsk->mm;
263
264 /*
265 * If we're in an interrupt or have no user
266 * context, we must not take the fault..
267 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -0800268 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 goto no_context;
270
Russell King840ff6a2005-09-20 17:52:13 +0100271 /*
272 * As per x86, we may deadlock here. However, since the kernel only
273 * validly references user space from well defined areas of the code,
274 * we can bug out early if this is from code which shouldn't.
275 */
276 if (!down_read_trylock(&mm->mmap_sem)) {
277 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
278 goto no_context;
279 down_read(&mm->mmap_sem);
280 }
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 fault = __do_page_fault(mm, addr, fsr, tsk);
283 up_read(&mm->mmap_sem);
284
285 /*
Russell Kingff2afb92005-08-04 14:17:33 +0100286 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
Nick Piggin5c72fc52007-07-20 09:21:06 +0200288 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return 0;
290
291 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * If we are in kernel mode at this point, we
293 * have no context to handle this fault with.
294 */
295 if (!user_mode(regs))
296 goto no_context;
297
Nick Piggin83c54072007-07-19 01:47:05 -0700298 if (fault & VM_FAULT_OOM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /*
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700300 * We ran out of memory, or some other thing
301 * happened to us that made us unable to handle
302 * the page fault gracefully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304 printk("VM: killing process %s\n", tsk->comm);
Will Schmidtdcca2bd2007-10-16 01:24:18 -0700305 do_group_exit(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 0;
Nick Piggin83c54072007-07-19 01:47:05 -0700307 }
308 if (fault & VM_FAULT_SIGBUS) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700309 /*
310 * We had some memory, but were unable to
311 * successfully fix up this page fault.
312 */
313 sig = SIGBUS;
314 code = BUS_ADRERR;
Nick Piggin83c54072007-07-19 01:47:05 -0700315 } else {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700316 /*
317 * Something tried to access memory that
318 * isn't in our memory map..
319 */
320 sig = SIGSEGV;
321 code = fault == VM_FAULT_BADACCESS ?
322 SEGV_ACCERR : SEGV_MAPERR;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700323 }
324
325 __do_user_fault(tsk, addr, fsr, sig, code, regs);
326 return 0;
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328no_context:
329 __do_kernel_fault(mm, addr, fsr, regs);
330 return 0;
331}
Catalin Marinas09529f72009-07-24 12:34:55 +0100332#else /* CONFIG_MMU */
333static int
334do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
335{
336 return 0;
337}
338#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/*
341 * First Level Translation Fault Handler
342 *
343 * We enter here because the first level page table doesn't contain
344 * a valid entry for the address.
345 *
346 * If the address is in kernel space (>= TASK_SIZE), then we are
347 * probably faulting in the vmalloc() area.
348 *
349 * If the init_task's first level page tables contains the relevant
350 * entry, we copy the it to this task. If not, we send the process
351 * a signal, fixup the exception, or oops the kernel.
352 *
353 * NOTE! We MUST NOT take any locks for this case. We may be in an
354 * interrupt or a critical region, and should only copy the information
355 * from the master page table, nothing more.
356 */
Catalin Marinas09529f72009-07-24 12:34:55 +0100357#ifdef CONFIG_MMU
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500358static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359do_translation_fault(unsigned long addr, unsigned int fsr,
360 struct pt_regs *regs)
361{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned int index;
363 pgd_t *pgd, *pgd_k;
364 pmd_t *pmd, *pmd_k;
365
366 if (addr < TASK_SIZE)
367 return do_page_fault(addr, fsr, regs);
368
369 index = pgd_index(addr);
370
371 /*
372 * FIXME: CP15 C1 is write only on ARMv3 architectures.
373 */
374 pgd = cpu_get_pgd() + index;
375 pgd_k = init_mm.pgd + index;
376
377 if (pgd_none(*pgd_k))
378 goto bad_area;
379
380 if (!pgd_present(*pgd))
381 set_pgd(pgd, *pgd_k);
382
383 pmd_k = pmd_offset(pgd_k, addr);
384 pmd = pmd_offset(pgd, addr);
385
386 if (pmd_none(*pmd_k))
387 goto bad_area;
388
389 copy_pmd(pmd, pmd_k);
390 return 0;
391
392bad_area:
Russell Kinge5beac32006-09-27 16:13:48 +0100393 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return 0;
395}
Catalin Marinas09529f72009-07-24 12:34:55 +0100396#else /* CONFIG_MMU */
397static int
398do_translation_fault(unsigned long addr, unsigned int fsr,
399 struct pt_regs *regs)
400{
401 return 0;
402}
403#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405/*
406 * Some section permission faults need to be handled gracefully.
407 * They can happen due to a __{get,put}_user during an oops.
408 */
409static int
410do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
411{
Russell Kinge5beac32006-09-27 16:13:48 +0100412 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return 0;
414}
415
416/*
417 * This abort handler always returns "fault".
418 */
419static int
420do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
421{
422 return 1;
423}
424
425static struct fsr_info {
426 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
427 int sig;
Russell Kingcfb08102005-06-30 11:06:49 +0100428 int code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 const char *name;
430} fsr_info[] = {
431 /*
432 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
433 * defines these to be "precise" aborts.
434 */
Russell Kingcfb08102005-06-30 11:06:49 +0100435 { do_bad, SIGSEGV, 0, "vector exception" },
436 { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
437 { do_bad, SIGKILL, 0, "terminal exception" },
438 { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
439 { do_bad, SIGBUS, 0, "external abort on linefetch" },
440 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
441 { do_bad, SIGBUS, 0, "external abort on linefetch" },
442 { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
443 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
444 { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
445 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
446 { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
447 { do_bad, SIGBUS, 0, "external abort on translation" },
448 { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
449 { do_bad, SIGBUS, 0, "external abort on translation" },
450 { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /*
452 * The following are "imprecise" aborts, which are signalled by bit
453 * 10 of the FSR, and may not be recoverable. These are only
454 * supported if the CPU abort handler supports bit 10.
455 */
Russell Kingcfb08102005-06-30 11:06:49 +0100456 { do_bad, SIGBUS, 0, "unknown 16" },
457 { do_bad, SIGBUS, 0, "unknown 17" },
458 { do_bad, SIGBUS, 0, "unknown 18" },
459 { do_bad, SIGBUS, 0, "unknown 19" },
460 { do_bad, SIGBUS, 0, "lock abort" }, /* xscale */
461 { do_bad, SIGBUS, 0, "unknown 21" },
462 { do_bad, SIGBUS, BUS_OBJERR, "imprecise external abort" }, /* xscale */
463 { do_bad, SIGBUS, 0, "unknown 23" },
464 { do_bad, SIGBUS, 0, "dcache parity error" }, /* xscale */
465 { do_bad, SIGBUS, 0, "unknown 25" },
466 { do_bad, SIGBUS, 0, "unknown 26" },
467 { do_bad, SIGBUS, 0, "unknown 27" },
468 { do_bad, SIGBUS, 0, "unknown 28" },
469 { do_bad, SIGBUS, 0, "unknown 29" },
470 { do_bad, SIGBUS, 0, "unknown 30" },
471 { do_bad, SIGBUS, 0, "unknown 31" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472};
473
474void __init
475hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
476 int sig, const char *name)
477{
478 if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) {
479 fsr_info[nr].fn = fn;
480 fsr_info[nr].sig = sig;
481 fsr_info[nr].name = name;
482 }
483}
484
485/*
486 * Dispatch a data abort to the relevant handler.
487 */
Russell King7ab3f8d2007-03-02 15:01:36 +0000488asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
490{
491 const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6);
Russell Kingcfb08102005-06-30 11:06:49 +0100492 struct siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (!inf->fn(addr, fsr, regs))
495 return;
496
497 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
498 inf->name, fsr, addr);
Russell Kingcfb08102005-06-30 11:06:49 +0100499
500 info.si_signo = inf->sig;
501 info.si_errno = 0;
502 info.si_code = inf->code;
503 info.si_addr = (void __user *)addr;
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700504 arm_notify_die("", regs, &info, fsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Russell King7ab3f8d2007-03-02 15:01:36 +0000507asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
509{
510 do_translation_fault(addr, 0, regs);
511}
512