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