Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | */ | |
1da177e4 LT |
11 | #include <linux/module.h> |
12 | #include <linux/signal.h> | |
1da177e4 | 13 | #include <linux/mm.h> |
67306da6 | 14 | #include <linux/hardirq.h> |
1da177e4 | 15 | #include <linux/init.h> |
25ce1dd7 | 16 | #include <linux/kprobes.h> |
33fa9b13 | 17 | #include <linux/uaccess.h> |
252d4c27 | 18 | #include <linux/page-flags.h> |
412bb0a6 | 19 | #include <linux/sched.h> |
65cec8e3 | 20 | #include <linux/highmem.h> |
1da177e4 LT |
21 | |
22 | #include <asm/system.h> | |
23 | #include <asm/pgtable.h> | |
24 | #include <asm/tlbflush.h> | |
1da177e4 LT |
25 | |
26 | #include "fault.h" | |
27 | ||
c88d6aa7 RK |
28 | /* |
29 | * Fault status register encodings | |
30 | */ | |
31 | #define FSR_WRITE (1 << 11) | |
32 | #define FSR_FS4 (1 << 10) | |
33 | #define FSR_FS3_0 (15) | |
34 | ||
35 | static inline int fsr_fs(unsigned int fsr) | |
36 | { | |
37 | return (fsr & FSR_FS3_0) | (fsr & FSR_FS4) >> 6; | |
38 | } | |
39 | ||
09529f7a | 40 | #ifdef CONFIG_MMU |
25ce1dd7 NP |
41 | |
42 | #ifdef CONFIG_KPROBES | |
43 | static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr) | |
44 | { | |
45 | int ret = 0; | |
46 | ||
47 | if (!user_mode(regs)) { | |
48 | /* kprobe_running() needs smp_processor_id() */ | |
49 | preempt_disable(); | |
50 | if (kprobe_running() && kprobe_fault_handler(regs, fsr)) | |
51 | ret = 1; | |
52 | preempt_enable(); | |
53 | } | |
54 | ||
55 | return ret; | |
56 | } | |
57 | #else | |
58 | static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr) | |
59 | { | |
60 | return 0; | |
61 | } | |
62 | #endif | |
63 | ||
1da177e4 LT |
64 | /* |
65 | * This is useful to dump out the page tables associated with | |
66 | * 'addr' in mm 'mm'. | |
67 | */ | |
68 | void show_pte(struct mm_struct *mm, unsigned long addr) | |
69 | { | |
70 | pgd_t *pgd; | |
71 | ||
72 | if (!mm) | |
73 | mm = &init_mm; | |
74 | ||
75 | printk(KERN_ALERT "pgd = %p\n", mm->pgd); | |
76 | pgd = pgd_offset(mm, addr); | |
77 | printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd)); | |
78 | ||
79 | do { | |
80 | pmd_t *pmd; | |
81 | pte_t *pte; | |
82 | ||
83 | if (pgd_none(*pgd)) | |
84 | break; | |
85 | ||
86 | if (pgd_bad(*pgd)) { | |
87 | printk("(bad)"); | |
88 | break; | |
89 | } | |
90 | ||
91 | pmd = pmd_offset(pgd, addr); | |
da46c79a NP |
92 | if (PTRS_PER_PMD != 1) |
93 | printk(", *pmd=%08lx", pmd_val(*pmd)); | |
1da177e4 LT |
94 | |
95 | if (pmd_none(*pmd)) | |
96 | break; | |
97 | ||
98 | if (pmd_bad(*pmd)) { | |
99 | printk("(bad)"); | |
100 | break; | |
101 | } | |
102 | ||
1da177e4 | 103 | /* We must not map this if we have highmem enabled */ |
252d4c27 NP |
104 | if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT))) |
105 | break; | |
106 | ||
1da177e4 LT |
107 | pte = pte_offset_map(pmd, addr); |
108 | printk(", *pte=%08lx", pte_val(*pte)); | |
109 | printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE])); | |
110 | pte_unmap(pte); | |
1da177e4 LT |
111 | } while(0); |
112 | ||
113 | printk("\n"); | |
114 | } | |
09529f7a CM |
115 | #else /* CONFIG_MMU */ |
116 | void show_pte(struct mm_struct *mm, unsigned long addr) | |
117 | { } | |
118 | #endif /* CONFIG_MMU */ | |
1da177e4 LT |
119 | |
120 | /* | |
121 | * Oops. The kernel tried to access some page that wasn't present. | |
122 | */ | |
123 | static void | |
124 | __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr, | |
125 | struct pt_regs *regs) | |
126 | { | |
127 | /* | |
128 | * Are we prepared to handle this kernel fault? | |
129 | */ | |
130 | if (fixup_exception(regs)) | |
131 | return; | |
132 | ||
133 | /* | |
134 | * No handler, we'll have to terminate things with extreme prejudice. | |
135 | */ | |
136 | bust_spinlocks(1); | |
137 | printk(KERN_ALERT | |
138 | "Unable to handle kernel %s at virtual address %08lx\n", | |
139 | (addr < PAGE_SIZE) ? "NULL pointer dereference" : | |
140 | "paging request", addr); | |
141 | ||
142 | show_pte(mm, addr); | |
143 | die("Oops", regs, fsr); | |
144 | bust_spinlocks(0); | |
145 | do_exit(SIGKILL); | |
146 | } | |
147 | ||
148 | /* | |
149 | * Something tried to access memory that isn't in our memory map.. | |
150 | * User mode accesses just cause a SIGSEGV | |
151 | */ | |
152 | static void | |
153 | __do_user_fault(struct task_struct *tsk, unsigned long addr, | |
2d137c24 AM |
154 | unsigned int fsr, unsigned int sig, int code, |
155 | struct pt_regs *regs) | |
1da177e4 LT |
156 | { |
157 | struct siginfo si; | |
158 | ||
159 | #ifdef CONFIG_DEBUG_USER | |
160 | if (user_debug & UDBG_SEGV) { | |
2d137c24 AM |
161 | printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n", |
162 | tsk->comm, sig, addr, fsr); | |
1da177e4 LT |
163 | show_pte(tsk->mm, addr); |
164 | show_regs(regs); | |
165 | } | |
166 | #endif | |
167 | ||
168 | tsk->thread.address = addr; | |
169 | tsk->thread.error_code = fsr; | |
170 | tsk->thread.trap_no = 14; | |
2d137c24 | 171 | si.si_signo = sig; |
1da177e4 LT |
172 | si.si_errno = 0; |
173 | si.si_code = code; | |
174 | si.si_addr = (void __user *)addr; | |
2d137c24 | 175 | force_sig_info(sig, &si, tsk); |
1da177e4 LT |
176 | } |
177 | ||
e5beac37 | 178 | void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs) |
1da177e4 | 179 | { |
e5beac37 RK |
180 | struct task_struct *tsk = current; |
181 | struct mm_struct *mm = tsk->active_mm; | |
182 | ||
1da177e4 LT |
183 | /* |
184 | * If we are in kernel mode at this point, we | |
185 | * have no context to handle this fault with. | |
186 | */ | |
187 | if (user_mode(regs)) | |
2d137c24 | 188 | __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs); |
1da177e4 LT |
189 | else |
190 | __do_kernel_fault(mm, addr, fsr, regs); | |
191 | } | |
192 | ||
09529f7a | 193 | #ifdef CONFIG_MMU |
5c72fc5c NP |
194 | #define VM_FAULT_BADMAP 0x010000 |
195 | #define VM_FAULT_BADACCESS 0x020000 | |
1da177e4 | 196 | |
d374bf14 RK |
197 | /* |
198 | * Check that the permissions on the VMA allow for the fault which occurred. | |
199 | * If we encountered a write fault, we must have write permission, otherwise | |
200 | * we allow any permission. | |
201 | */ | |
202 | static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma) | |
203 | { | |
204 | unsigned int mask = VM_READ | VM_WRITE | VM_EXEC; | |
205 | ||
206 | if (fsr & FSR_WRITE) | |
207 | mask = VM_WRITE; | |
208 | ||
209 | return vma->vm_flags & mask ? false : true; | |
210 | } | |
211 | ||
212 | static int __kprobes | |
1da177e4 LT |
213 | __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr, |
214 | struct task_struct *tsk) | |
215 | { | |
216 | struct vm_area_struct *vma; | |
d374bf14 | 217 | int fault; |
1da177e4 LT |
218 | |
219 | vma = find_vma(mm, addr); | |
220 | fault = VM_FAULT_BADMAP; | |
d374bf14 | 221 | if (unlikely(!vma)) |
1da177e4 | 222 | goto out; |
d374bf14 | 223 | if (unlikely(vma->vm_start > addr)) |
1da177e4 LT |
224 | goto check_stack; |
225 | ||
226 | /* | |
227 | * Ok, we have a good vm_area for this | |
228 | * memory access, so we can handle it. | |
229 | */ | |
230 | good_area: | |
d374bf14 RK |
231 | if (access_error(fsr, vma)) { |
232 | fault = VM_FAULT_BADACCESS; | |
1da177e4 | 233 | goto out; |
d374bf14 | 234 | } |
1da177e4 LT |
235 | |
236 | /* | |
b42c6344 RK |
237 | * If for any reason at all we couldn't handle the fault, make |
238 | * sure we exit gracefully rather than endlessly redo the fault. | |
1da177e4 | 239 | */ |
c88d6aa7 | 240 | fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & FSR_WRITE) ? FAULT_FLAG_WRITE : 0); |
b42c6344 RK |
241 | if (unlikely(fault & VM_FAULT_ERROR)) |
242 | return fault; | |
83c54070 | 243 | if (fault & VM_FAULT_MAJOR) |
1da177e4 | 244 | tsk->maj_flt++; |
83c54070 | 245 | else |
1da177e4 | 246 | tsk->min_flt++; |
83c54070 | 247 | return fault; |
1da177e4 | 248 | |
1da177e4 LT |
249 | check_stack: |
250 | if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr)) | |
251 | goto good_area; | |
252 | out: | |
253 | return fault; | |
254 | } | |
255 | ||
785d3cd2 | 256 | static int __kprobes |
1da177e4 LT |
257 | do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs) |
258 | { | |
259 | struct task_struct *tsk; | |
260 | struct mm_struct *mm; | |
2d137c24 | 261 | int fault, sig, code; |
1da177e4 | 262 | |
25ce1dd7 NP |
263 | if (notify_page_fault(regs, fsr)) |
264 | return 0; | |
265 | ||
1da177e4 LT |
266 | tsk = current; |
267 | mm = tsk->mm; | |
268 | ||
269 | /* | |
270 | * If we're in an interrupt or have no user | |
271 | * context, we must not take the fault.. | |
272 | */ | |
6edaf68a | 273 | if (in_atomic() || !mm) |
1da177e4 LT |
274 | goto no_context; |
275 | ||
840ff6a4 RK |
276 | /* |
277 | * As per x86, we may deadlock here. However, since the kernel only | |
278 | * validly references user space from well defined areas of the code, | |
279 | * we can bug out early if this is from code which shouldn't. | |
280 | */ | |
281 | if (!down_read_trylock(&mm->mmap_sem)) { | |
282 | if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc)) | |
283 | goto no_context; | |
284 | down_read(&mm->mmap_sem); | |
bf456992 RK |
285 | } else { |
286 | /* | |
287 | * The above down_read_trylock() might have succeeded in | |
288 | * which case, we'll have missed the might_sleep() from | |
289 | * down_read() | |
290 | */ | |
291 | might_sleep(); | |
840ff6a4 RK |
292 | } |
293 | ||
1da177e4 LT |
294 | fault = __do_page_fault(mm, addr, fsr, tsk); |
295 | up_read(&mm->mmap_sem); | |
296 | ||
297 | /* | |
ff2afb9d | 298 | * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR |
1da177e4 | 299 | */ |
5c72fc5c | 300 | if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS)))) |
1da177e4 LT |
301 | return 0; |
302 | ||
b42c6344 RK |
303 | if (fault & VM_FAULT_OOM) { |
304 | /* | |
305 | * We ran out of memory, call the OOM killer, and return to | |
306 | * userspace (which will retry the fault, or kill us if we | |
307 | * got oom-killed) | |
308 | */ | |
309 | pagefault_out_of_memory(); | |
310 | return 0; | |
311 | } | |
312 | ||
1da177e4 LT |
313 | /* |
314 | * If we are in kernel mode at this point, we | |
315 | * have no context to handle this fault with. | |
316 | */ | |
317 | if (!user_mode(regs)) | |
318 | goto no_context; | |
319 | ||
83c54070 | 320 | if (fault & VM_FAULT_SIGBUS) { |
2d137c24 AM |
321 | /* |
322 | * We had some memory, but were unable to | |
323 | * successfully fix up this page fault. | |
324 | */ | |
325 | sig = SIGBUS; | |
326 | code = BUS_ADRERR; | |
83c54070 | 327 | } else { |
2d137c24 AM |
328 | /* |
329 | * Something tried to access memory that | |
330 | * isn't in our memory map.. | |
331 | */ | |
332 | sig = SIGSEGV; | |
333 | code = fault == VM_FAULT_BADACCESS ? | |
334 | SEGV_ACCERR : SEGV_MAPERR; | |
1da177e4 | 335 | } |
1da177e4 | 336 | |
2d137c24 AM |
337 | __do_user_fault(tsk, addr, fsr, sig, code, regs); |
338 | return 0; | |
1da177e4 LT |
339 | |
340 | no_context: | |
341 | __do_kernel_fault(mm, addr, fsr, regs); | |
342 | return 0; | |
343 | } | |
09529f7a CM |
344 | #else /* CONFIG_MMU */ |
345 | static int | |
346 | do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | |
347 | { | |
348 | return 0; | |
349 | } | |
350 | #endif /* CONFIG_MMU */ | |
1da177e4 LT |
351 | |
352 | /* | |
353 | * First Level Translation Fault Handler | |
354 | * | |
355 | * We enter here because the first level page table doesn't contain | |
356 | * a valid entry for the address. | |
357 | * | |
358 | * If the address is in kernel space (>= TASK_SIZE), then we are | |
359 | * probably faulting in the vmalloc() area. | |
360 | * | |
361 | * If the init_task's first level page tables contains the relevant | |
362 | * entry, we copy the it to this task. If not, we send the process | |
363 | * a signal, fixup the exception, or oops the kernel. | |
364 | * | |
365 | * NOTE! We MUST NOT take any locks for this case. We may be in an | |
366 | * interrupt or a critical region, and should only copy the information | |
367 | * from the master page table, nothing more. | |
368 | */ | |
09529f7a | 369 | #ifdef CONFIG_MMU |
785d3cd2 | 370 | static int __kprobes |
1da177e4 LT |
371 | do_translation_fault(unsigned long addr, unsigned int fsr, |
372 | struct pt_regs *regs) | |
373 | { | |
1da177e4 LT |
374 | unsigned int index; |
375 | pgd_t *pgd, *pgd_k; | |
376 | pmd_t *pmd, *pmd_k; | |
377 | ||
378 | if (addr < TASK_SIZE) | |
379 | return do_page_fault(addr, fsr, regs); | |
380 | ||
381 | index = pgd_index(addr); | |
382 | ||
383 | /* | |
384 | * FIXME: CP15 C1 is write only on ARMv3 architectures. | |
385 | */ | |
386 | pgd = cpu_get_pgd() + index; | |
387 | pgd_k = init_mm.pgd + index; | |
388 | ||
389 | if (pgd_none(*pgd_k)) | |
390 | goto bad_area; | |
391 | ||
392 | if (!pgd_present(*pgd)) | |
393 | set_pgd(pgd, *pgd_k); | |
394 | ||
395 | pmd_k = pmd_offset(pgd_k, addr); | |
396 | pmd = pmd_offset(pgd, addr); | |
397 | ||
398 | if (pmd_none(*pmd_k)) | |
399 | goto bad_area; | |
400 | ||
401 | copy_pmd(pmd, pmd_k); | |
402 | return 0; | |
403 | ||
404 | bad_area: | |
e5beac37 | 405 | do_bad_area(addr, fsr, regs); |
1da177e4 LT |
406 | return 0; |
407 | } | |
09529f7a CM |
408 | #else /* CONFIG_MMU */ |
409 | static int | |
410 | do_translation_fault(unsigned long addr, unsigned int fsr, | |
411 | struct pt_regs *regs) | |
412 | { | |
413 | return 0; | |
414 | } | |
415 | #endif /* CONFIG_MMU */ | |
1da177e4 LT |
416 | |
417 | /* | |
418 | * Some section permission faults need to be handled gracefully. | |
419 | * They can happen due to a __{get,put}_user during an oops. | |
420 | */ | |
421 | static int | |
422 | do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | |
423 | { | |
e5beac37 | 424 | do_bad_area(addr, fsr, regs); |
1da177e4 LT |
425 | return 0; |
426 | } | |
427 | ||
428 | /* | |
429 | * This abort handler always returns "fault". | |
430 | */ | |
431 | static int | |
432 | do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | |
433 | { | |
434 | return 1; | |
435 | } | |
436 | ||
437 | static struct fsr_info { | |
438 | int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs); | |
439 | int sig; | |
cfb0810e | 440 | int code; |
1da177e4 LT |
441 | const char *name; |
442 | } fsr_info[] = { | |
443 | /* | |
444 | * The following are the standard ARMv3 and ARMv4 aborts. ARMv5 | |
445 | * defines these to be "precise" aborts. | |
446 | */ | |
cfb0810e RK |
447 | { do_bad, SIGSEGV, 0, "vector exception" }, |
448 | { do_bad, SIGILL, BUS_ADRALN, "alignment exception" }, | |
449 | { do_bad, SIGKILL, 0, "terminal exception" }, | |
450 | { do_bad, SIGILL, BUS_ADRALN, "alignment exception" }, | |
451 | { do_bad, SIGBUS, 0, "external abort on linefetch" }, | |
452 | { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" }, | |
453 | { do_bad, SIGBUS, 0, "external abort on linefetch" }, | |
454 | { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" }, | |
455 | { do_bad, SIGBUS, 0, "external abort on non-linefetch" }, | |
456 | { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" }, | |
457 | { do_bad, SIGBUS, 0, "external abort on non-linefetch" }, | |
458 | { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" }, | |
459 | { do_bad, SIGBUS, 0, "external abort on translation" }, | |
460 | { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" }, | |
461 | { do_bad, SIGBUS, 0, "external abort on translation" }, | |
462 | { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" }, | |
1da177e4 LT |
463 | /* |
464 | * The following are "imprecise" aborts, which are signalled by bit | |
465 | * 10 of the FSR, and may not be recoverable. These are only | |
466 | * supported if the CPU abort handler supports bit 10. | |
467 | */ | |
cfb0810e RK |
468 | { do_bad, SIGBUS, 0, "unknown 16" }, |
469 | { do_bad, SIGBUS, 0, "unknown 17" }, | |
470 | { do_bad, SIGBUS, 0, "unknown 18" }, | |
471 | { do_bad, SIGBUS, 0, "unknown 19" }, | |
472 | { do_bad, SIGBUS, 0, "lock abort" }, /* xscale */ | |
473 | { do_bad, SIGBUS, 0, "unknown 21" }, | |
474 | { do_bad, SIGBUS, BUS_OBJERR, "imprecise external abort" }, /* xscale */ | |
475 | { do_bad, SIGBUS, 0, "unknown 23" }, | |
476 | { do_bad, SIGBUS, 0, "dcache parity error" }, /* xscale */ | |
477 | { do_bad, SIGBUS, 0, "unknown 25" }, | |
478 | { do_bad, SIGBUS, 0, "unknown 26" }, | |
479 | { do_bad, SIGBUS, 0, "unknown 27" }, | |
480 | { do_bad, SIGBUS, 0, "unknown 28" }, | |
481 | { do_bad, SIGBUS, 0, "unknown 29" }, | |
482 | { do_bad, SIGBUS, 0, "unknown 30" }, | |
483 | { do_bad, SIGBUS, 0, "unknown 31" } | |
1da177e4 LT |
484 | }; |
485 | ||
486 | void __init | |
487 | hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *), | |
488 | int sig, const char *name) | |
489 | { | |
490 | if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) { | |
491 | fsr_info[nr].fn = fn; | |
492 | fsr_info[nr].sig = sig; | |
493 | fsr_info[nr].name = name; | |
494 | } | |
495 | } | |
496 | ||
497 | /* | |
498 | * Dispatch a data abort to the relevant handler. | |
499 | */ | |
7ab3f8d5 | 500 | asmlinkage void __exception |
1da177e4 LT |
501 | do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs) |
502 | { | |
c88d6aa7 | 503 | const struct fsr_info *inf = fsr_info + fsr_fs(fsr); |
cfb0810e | 504 | struct siginfo info; |
1da177e4 LT |
505 | |
506 | if (!inf->fn(addr, fsr, regs)) | |
507 | return; | |
508 | ||
509 | printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n", | |
510 | inf->name, fsr, addr); | |
cfb0810e RK |
511 | |
512 | info.si_signo = inf->sig; | |
513 | info.si_errno = 0; | |
514 | info.si_code = inf->code; | |
515 | info.si_addr = (void __user *)addr; | |
1eeb66a1 | 516 | arm_notify_die("", regs, &info, fsr, 0); |
1da177e4 LT |
517 | } |
518 | ||
7ab3f8d5 | 519 | asmlinkage void __exception |
1da177e4 LT |
520 | do_PrefetchAbort(unsigned long addr, struct pt_regs *regs) |
521 | { | |
522 | do_translation_fault(addr, 0, regs); | |
523 | } | |
524 |