Include linux/sched.h in arch/arm/mm/fault.c
[deliverable/linux.git] / arch / arm / mm / fault.c
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 */
11 #include <linux/module.h>
12 #include <linux/signal.h>
13 #include <linux/mm.h>
14 #include <linux/hardirq.h>
15 #include <linux/init.h>
16 #include <linux/kprobes.h>
17 #include <linux/uaccess.h>
18 #include <linux/page-flags.h>
19 #include <linux/sched.h>
20
21 #include <asm/system.h>
22 #include <asm/pgtable.h>
23 #include <asm/tlbflush.h>
24
25 #include "fault.h"
26
27 #ifdef CONFIG_MMU
28
29 #ifdef CONFIG_KPROBES
30 static 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
45 static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
46 {
47 return 0;
48 }
49 #endif
50
51 /*
52 * This is useful to dump out the page tables associated with
53 * 'addr' in mm 'mm'.
54 */
55 void 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);
79 if (PTRS_PER_PMD != 1)
80 printk(", *pmd=%08lx", pmd_val(*pmd));
81
82 if (pmd_none(*pmd))
83 break;
84
85 if (pmd_bad(*pmd)) {
86 printk("(bad)");
87 break;
88 }
89
90 /* We must not map this if we have highmem enabled */
91 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
92 break;
93
94 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);
98 } while(0);
99
100 printk("\n");
101 }
102 #else /* CONFIG_MMU */
103 void show_pte(struct mm_struct *mm, unsigned long addr)
104 { }
105 #endif /* CONFIG_MMU */
106
107 /*
108 * Oops. The kernel tried to access some page that wasn't present.
109 */
110 static 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 */
139 static void
140 __do_user_fault(struct task_struct *tsk, unsigned long addr,
141 unsigned int fsr, unsigned int sig, int code,
142 struct pt_regs *regs)
143 {
144 struct siginfo si;
145
146 #ifdef CONFIG_DEBUG_USER
147 if (user_debug & UDBG_SEGV) {
148 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
149 tsk->comm, sig, addr, fsr);
150 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;
158 si.si_signo = sig;
159 si.si_errno = 0;
160 si.si_code = code;
161 si.si_addr = (void __user *)addr;
162 force_sig_info(sig, &si, tsk);
163 }
164
165 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
166 {
167 struct task_struct *tsk = current;
168 struct mm_struct *mm = tsk->active_mm;
169
170 /*
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))
175 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
176 else
177 __do_kernel_fault(mm, addr, fsr, regs);
178 }
179
180 #ifdef CONFIG_MMU
181 #define VM_FAULT_BADMAP 0x010000
182 #define VM_FAULT_BADACCESS 0x020000
183
184 static 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 */
202 good_area:
203 if (fsr & (1 << 11)) /* write? */
204 mask = VM_WRITE;
205 else
206 mask = VM_READ|VM_EXEC|VM_WRITE;
207
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 */
217 survive:
218 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & (1 << 11)) ? FAULT_FLAG_WRITE : 0);
219 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();
225 }
226 if (fault & VM_FAULT_MAJOR)
227 tsk->maj_flt++;
228 else
229 tsk->min_flt++;
230 return fault;
231
232 out_of_memory:
233 if (!is_global_init(tsk))
234 goto out;
235
236 /*
237 * If we are out of memory for pid1, sleep for a while and retry
238 */
239 up_read(&mm->mmap_sem);
240 yield();
241 down_read(&mm->mmap_sem);
242 goto survive;
243
244 check_stack:
245 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
246 goto good_area;
247 out:
248 return fault;
249 }
250
251 static int __kprobes
252 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
253 {
254 struct task_struct *tsk;
255 struct mm_struct *mm;
256 int fault, sig, code;
257
258 if (notify_page_fault(regs, fsr))
259 return 0;
260
261 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 */
268 if (in_atomic() || !mm)
269 goto no_context;
270
271 /*
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
282 fault = __do_page_fault(mm, addr, fsr, tsk);
283 up_read(&mm->mmap_sem);
284
285 /*
286 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
287 */
288 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
289 return 0;
290
291 /*
292 * 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
298 if (fault & VM_FAULT_OOM) {
299 /*
300 * We ran out of memory, or some other thing
301 * happened to us that made us unable to handle
302 * the page fault gracefully.
303 */
304 printk("VM: killing process %s\n", tsk->comm);
305 do_group_exit(SIGKILL);
306 return 0;
307 }
308 if (fault & VM_FAULT_SIGBUS) {
309 /*
310 * We had some memory, but were unable to
311 * successfully fix up this page fault.
312 */
313 sig = SIGBUS;
314 code = BUS_ADRERR;
315 } else {
316 /*
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;
323 }
324
325 __do_user_fault(tsk, addr, fsr, sig, code, regs);
326 return 0;
327
328 no_context:
329 __do_kernel_fault(mm, addr, fsr, regs);
330 return 0;
331 }
332 #else /* CONFIG_MMU */
333 static int
334 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
335 {
336 return 0;
337 }
338 #endif /* CONFIG_MMU */
339
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 */
357 #ifdef CONFIG_MMU
358 static int __kprobes
359 do_translation_fault(unsigned long addr, unsigned int fsr,
360 struct pt_regs *regs)
361 {
362 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
392 bad_area:
393 do_bad_area(addr, fsr, regs);
394 return 0;
395 }
396 #else /* CONFIG_MMU */
397 static int
398 do_translation_fault(unsigned long addr, unsigned int fsr,
399 struct pt_regs *regs)
400 {
401 return 0;
402 }
403 #endif /* CONFIG_MMU */
404
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 */
409 static int
410 do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
411 {
412 do_bad_area(addr, fsr, regs);
413 return 0;
414 }
415
416 /*
417 * This abort handler always returns "fault".
418 */
419 static int
420 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
421 {
422 return 1;
423 }
424
425 static struct fsr_info {
426 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
427 int sig;
428 int code;
429 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 */
435 { 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" },
451 /*
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 */
456 { 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" }
472 };
473
474 void __init
475 hook_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 */
488 asmlinkage void __exception
489 do_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);
492 struct siginfo info;
493
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);
499
500 info.si_signo = inf->sig;
501 info.si_errno = 0;
502 info.si_code = inf->code;
503 info.si_addr = (void __user *)addr;
504 arm_notify_die("", regs, &info, fsr, 0);
505 }
506
507 asmlinkage void __exception
508 do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
509 {
510 do_translation_fault(addr, 0, regs);
511 }
512
This page took 0.048049 seconds and 5 git commands to generate.