pandora: defconfig: update
[pandora-kernel.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 #include <linux/highmem.h>
21 #include <linux/perf_event.h>
22
23 #include <asm/exception.h>
24 #include <asm/system.h>
25 #include <asm/pgtable.h>
26 #include <asm/tlbflush.h>
27
28 #include "fault.h"
29
30 #ifdef CONFIG_MMU
31
32 #ifdef CONFIG_KPROBES
33 static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
34 {
35         int ret = 0;
36
37         if (!user_mode(regs)) {
38                 /* kprobe_running() needs smp_processor_id() */
39                 preempt_disable();
40                 if (kprobe_running() && kprobe_fault_handler(regs, fsr))
41                         ret = 1;
42                 preempt_enable();
43         }
44
45         return ret;
46 }
47 #else
48 static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
49 {
50         return 0;
51 }
52 #endif
53
54 /*
55  * This is useful to dump out the page tables associated with
56  * 'addr' in mm 'mm'.
57  */
58 void show_pte(struct mm_struct *mm, unsigned long addr)
59 {
60         pgd_t *pgd;
61
62         if (!mm)
63                 mm = &init_mm;
64
65         printk(KERN_ALERT "pgd = %p\n", mm->pgd);
66         pgd = pgd_offset(mm, addr);
67         printk(KERN_ALERT "[%08lx] *pgd=%08llx",
68                         addr, (long long)pgd_val(*pgd));
69
70         do {
71                 pud_t *pud;
72                 pmd_t *pmd;
73                 pte_t *pte;
74
75                 if (pgd_none(*pgd))
76                         break;
77
78                 if (pgd_bad(*pgd)) {
79                         printk("(bad)");
80                         break;
81                 }
82
83                 pud = pud_offset(pgd, addr);
84                 if (PTRS_PER_PUD != 1)
85                         printk(", *pud=%08llx", (long long)pud_val(*pud));
86
87                 if (pud_none(*pud))
88                         break;
89
90                 if (pud_bad(*pud)) {
91                         printk("(bad)");
92                         break;
93                 }
94
95                 pmd = pmd_offset(pud, addr);
96                 if (PTRS_PER_PMD != 1)
97                         printk(", *pmd=%08llx", (long long)pmd_val(*pmd));
98
99                 if (pmd_none(*pmd))
100                         break;
101
102                 if (pmd_bad(*pmd)) {
103                         printk("(bad)");
104                         break;
105                 }
106
107                 /* We must not map this if we have highmem enabled */
108                 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
109                         break;
110
111                 pte = pte_offset_map(pmd, addr);
112                 printk(", *pte=%08llx", (long long)pte_val(*pte));
113 #ifndef CONFIG_ARM_LPAE
114                 printk(", *ppte=%08llx",
115                        (long long)pte_val(pte[PTE_HWTABLE_PTRS]));
116 #endif
117                 pte_unmap(pte);
118         } while(0);
119
120         printk("\n");
121 }
122 #else                                   /* CONFIG_MMU */
123 void show_pte(struct mm_struct *mm, unsigned long addr)
124 { }
125 #endif                                  /* CONFIG_MMU */
126
127 /*
128  * Oops.  The kernel tried to access some page that wasn't present.
129  */
130 static void
131 __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
132                   struct pt_regs *regs)
133 {
134         /*
135          * Are we prepared to handle this kernel fault?
136          */
137         if (fixup_exception(regs))
138                 return;
139
140         /*
141          * No handler, we'll have to terminate things with extreme prejudice.
142          */
143         bust_spinlocks(1);
144         printk(KERN_ALERT
145                 "Unable to handle kernel %s at virtual address %08lx\n",
146                 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
147                 "paging request", addr);
148
149         show_pte(mm, addr);
150         die("Oops", regs, fsr);
151         bust_spinlocks(0);
152         do_exit(SIGKILL);
153 }
154
155 #ifdef CONFIG_DEBUG_USER
156 static void
157 print_user_faulter_location(const char *name, struct pt_regs *regs)
158 {
159         struct mm_struct *mm = current->mm;
160         struct vm_area_struct *vma;
161         char *p, *t, buf[128];
162
163         printk(KERN_DEBUG "%s: pc=%08lx",
164                name, regs->ARM_pc);
165
166         do {
167                 if (!mm)
168                         break;
169                 vma = find_vma(mm, regs->ARM_pc);
170                 if (!vma || !vma->vm_file)
171                         break;
172
173                 p = d_path(&vma->vm_file->f_path, buf, sizeof(buf));
174                 if (IS_ERR(p))
175                         break;
176
177                 t = strrchr(p, '/');
178                 if (t)
179                         p = t + 1;
180
181                 printk(KERN_CONT " (%s+%lx)", p, regs->ARM_pc - vma->vm_start);
182         } while (0);
183
184         printk(KERN_CONT ", lr=%08lx\n", regs->ARM_lr);
185 }
186 #endif
187
188 /*
189  * Something tried to access memory that isn't in our memory map..
190  * User mode accesses just cause a SIGSEGV
191  */
192 static void
193 __do_user_fault(struct task_struct *tsk, unsigned long addr,
194                 unsigned int fsr, unsigned int sig, int code,
195                 struct pt_regs *regs)
196 {
197         struct siginfo si;
198
199 #ifdef CONFIG_DEBUG_USER
200         if ((user_debug & (UDBG_SEGV | UDBG_SEGV_SHORT))
201             && unhandled_signal(tsk, SIGSEGV) && printk_ratelimit()) {
202                 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x (%s)\n",
203                        tsk->comm, sig, addr, fsr,
204                        (fsr & FSR_WRITE) ? "write" : "read");
205                 print_user_faulter_location(tsk->comm, regs);
206         }
207         if (user_debug & UDBG_SEGV) {
208                 show_pte(tsk->mm, addr);
209                 show_regs(regs);
210         }
211 #endif
212
213         tsk->thread.address = addr;
214         tsk->thread.error_code = fsr;
215         tsk->thread.trap_no = 14;
216         si.si_signo = sig;
217         si.si_errno = 0;
218         si.si_code = code;
219         si.si_addr = (void __user *)addr;
220         force_sig_info(sig, &si, tsk);
221 }
222
223 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
224 {
225         struct task_struct *tsk = current;
226         struct mm_struct *mm = tsk->active_mm;
227
228         /*
229          * If we are in kernel mode at this point, we
230          * have no context to handle this fault with.
231          */
232         if (user_mode(regs))
233                 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
234         else
235                 __do_kernel_fault(mm, addr, fsr, regs);
236 }
237
238 #ifdef CONFIG_MMU
239 #define VM_FAULT_BADMAP         0x010000
240 #define VM_FAULT_BADACCESS      0x020000
241
242 /*
243  * Check that the permissions on the VMA allow for the fault which occurred.
244  * If we encountered a write fault, we must have write permission, otherwise
245  * we allow any permission.
246  */
247 static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
248 {
249         unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
250
251         if (fsr & FSR_WRITE)
252                 mask = VM_WRITE;
253         if (fsr & FSR_LNX_PF)
254                 mask = VM_EXEC;
255
256         return vma->vm_flags & mask ? false : true;
257 }
258
259 static int __kprobes
260 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
261                 struct task_struct *tsk)
262 {
263         struct vm_area_struct *vma;
264         int fault;
265
266         vma = find_vma(mm, addr);
267         fault = VM_FAULT_BADMAP;
268         if (unlikely(!vma))
269                 goto out;
270         if (unlikely(vma->vm_start > addr))
271                 goto check_stack;
272
273         /*
274          * Ok, we have a good vm_area for this
275          * memory access, so we can handle it.
276          */
277 good_area:
278         if (access_error(fsr, vma)) {
279                 fault = VM_FAULT_BADACCESS;
280                 goto out;
281         }
282
283         /*
284          * If for any reason at all we couldn't handle the fault, make
285          * sure we exit gracefully rather than endlessly redo the fault.
286          */
287         fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & FSR_WRITE) ? FAULT_FLAG_WRITE : 0);
288         if (unlikely(fault & VM_FAULT_ERROR))
289                 return fault;
290         if (fault & VM_FAULT_MAJOR)
291                 tsk->maj_flt++;
292         else
293                 tsk->min_flt++;
294         return fault;
295
296 check_stack:
297         /* Don't allow expansion below FIRST_USER_ADDRESS */
298         if (vma->vm_flags & VM_GROWSDOWN &&
299             addr >= FIRST_USER_ADDRESS && !expand_stack(vma, addr))
300                 goto good_area;
301 out:
302         return fault;
303 }
304
305 static int __kprobes
306 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
307 {
308         struct task_struct *tsk;
309         struct mm_struct *mm;
310         int fault, sig, code;
311
312         if (notify_page_fault(regs, fsr))
313                 return 0;
314
315         tsk = current;
316         mm  = tsk->mm;
317
318         /* Enable interrupts if they were enabled in the parent context. */
319         if (interrupts_enabled(regs))
320                 local_irq_enable();
321
322         /*
323          * If we're in an interrupt or have no user
324          * context, we must not take the fault..
325          */
326         if (in_atomic() || !mm)
327                 goto no_context;
328
329         /*
330          * As per x86, we may deadlock here.  However, since the kernel only
331          * validly references user space from well defined areas of the code,
332          * we can bug out early if this is from code which shouldn't.
333          */
334         if (!down_read_trylock(&mm->mmap_sem)) {
335                 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
336                         goto no_context;
337                 down_read(&mm->mmap_sem);
338         } else {
339                 /*
340                  * The above down_read_trylock() might have succeeded in
341                  * which case, we'll have missed the might_sleep() from
342                  * down_read()
343                  */
344                 might_sleep();
345 #ifdef CONFIG_DEBUG_VM
346                 if (!user_mode(regs) &&
347                     !search_exception_tables(regs->ARM_pc))
348                         goto no_context;
349 #endif
350         }
351
352         fault = __do_page_fault(mm, addr, fsr, tsk);
353         up_read(&mm->mmap_sem);
354
355         perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
356         if (fault & VM_FAULT_MAJOR)
357                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, addr);
358         else if (fault & VM_FAULT_MINOR)
359                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, addr);
360
361         /*
362          * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
363          */
364         if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
365                 return 0;
366
367         if (fault & VM_FAULT_OOM) {
368                 /*
369                  * We ran out of memory, call the OOM killer, and return to
370                  * userspace (which will retry the fault, or kill us if we
371                  * got oom-killed)
372                  */
373                 pagefault_out_of_memory();
374                 return 0;
375         }
376
377         /*
378          * If we are in kernel mode at this point, we
379          * have no context to handle this fault with.
380          */
381         if (!user_mode(regs))
382                 goto no_context;
383
384         if (fault & VM_FAULT_SIGBUS) {
385                 /*
386                  * We had some memory, but were unable to
387                  * successfully fix up this page fault.
388                  */
389                 sig = SIGBUS;
390                 code = BUS_ADRERR;
391         } else {
392                 /*
393                  * Something tried to access memory that
394                  * isn't in our memory map..
395                  */
396                 sig = SIGSEGV;
397                 code = fault == VM_FAULT_BADACCESS ?
398                         SEGV_ACCERR : SEGV_MAPERR;
399         }
400
401         __do_user_fault(tsk, addr, fsr, sig, code, regs);
402         return 0;
403
404 no_context:
405         __do_kernel_fault(mm, addr, fsr, regs);
406         return 0;
407 }
408 #else                                   /* CONFIG_MMU */
409 static int
410 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
411 {
412         return 0;
413 }
414 #endif                                  /* CONFIG_MMU */
415
416 /*
417  * First Level Translation Fault Handler
418  *
419  * We enter here because the first level page table doesn't contain
420  * a valid entry for the address.
421  *
422  * If the address is in kernel space (>= TASK_SIZE), then we are
423  * probably faulting in the vmalloc() area.
424  *
425  * If the init_task's first level page tables contains the relevant
426  * entry, we copy the it to this task.  If not, we send the process
427  * a signal, fixup the exception, or oops the kernel.
428  *
429  * NOTE! We MUST NOT take any locks for this case. We may be in an
430  * interrupt or a critical region, and should only copy the information
431  * from the master page table, nothing more.
432  */
433 #ifdef CONFIG_MMU
434 static int __kprobes
435 do_translation_fault(unsigned long addr, unsigned int fsr,
436                      struct pt_regs *regs)
437 {
438         unsigned int index;
439         pgd_t *pgd, *pgd_k;
440         pud_t *pud, *pud_k;
441         pmd_t *pmd, *pmd_k;
442
443         if (addr < TASK_SIZE)
444                 return do_page_fault(addr, fsr, regs);
445
446         if (user_mode(regs))
447                 goto bad_area;
448
449         index = pgd_index(addr);
450
451         /*
452          * FIXME: CP15 C1 is write only on ARMv3 architectures.
453          */
454         pgd = cpu_get_pgd() + index;
455         pgd_k = init_mm.pgd + index;
456
457         if (pgd_none(*pgd_k))
458                 goto bad_area;
459         if (!pgd_present(*pgd))
460                 set_pgd(pgd, *pgd_k);
461
462         pud = pud_offset(pgd, addr);
463         pud_k = pud_offset(pgd_k, addr);
464
465         if (pud_none(*pud_k))
466                 goto bad_area;
467         if (!pud_present(*pud))
468                 set_pud(pud, *pud_k);
469
470         pmd = pmd_offset(pud, addr);
471         pmd_k = pmd_offset(pud_k, addr);
472
473 #ifdef CONFIG_ARM_LPAE
474         /*
475          * Only one hardware entry per PMD with LPAE.
476          */
477         index = 0;
478 #else
479         /*
480          * On ARM one Linux PGD entry contains two hardware entries (see page
481          * tables layout in pgtable.h). We normally guarantee that we always
482          * fill both L1 entries. But create_mapping() doesn't follow the rule.
483          * It can create inidividual L1 entries, so here we have to call
484          * pmd_none() check for the entry really corresponded to address, not
485          * for the first of pair.
486          */
487         index = (addr >> SECTION_SHIFT) & 1;
488 #endif
489         if (pmd_none(pmd_k[index]))
490                 goto bad_area;
491
492         copy_pmd(pmd, pmd_k);
493         return 0;
494
495 bad_area:
496         do_bad_area(addr, fsr, regs);
497         return 0;
498 }
499 #else                                   /* CONFIG_MMU */
500 static int
501 do_translation_fault(unsigned long addr, unsigned int fsr,
502                      struct pt_regs *regs)
503 {
504         return 0;
505 }
506 #endif                                  /* CONFIG_MMU */
507
508 /*
509  * This abort handler always returns "fault".
510  */
511 static int
512 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
513 {
514         return 1;
515 }
516
517 struct fsr_info {
518         int     (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
519         int     sig;
520         int     code;
521         const char *name;
522 };
523
524 /* FSR definition */
525 #ifdef CONFIG_ARM_LPAE
526 #include "fsr-3level.c"
527 #else
528 #include "fsr-2level.c"
529 #endif
530
531 void __init
532 hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
533                 int sig, int code, const char *name)
534 {
535         if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
536                 BUG();
537
538         fsr_info[nr].fn   = fn;
539         fsr_info[nr].sig  = sig;
540         fsr_info[nr].code = code;
541         fsr_info[nr].name = name;
542 }
543
544 /*
545  * Dispatch a data abort to the relevant handler.
546  */
547 asmlinkage void __exception
548 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
549 {
550         const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
551         struct siginfo info;
552
553         if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
554                 return;
555
556         printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
557                 inf->name, fsr, addr);
558
559         info.si_signo = inf->sig;
560         info.si_errno = 0;
561         info.si_code  = inf->code;
562         info.si_addr  = (void __user *)addr;
563         arm_notify_die("", regs, &info, fsr, 0);
564 }
565
566 void __init
567 hook_ifault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
568                  int sig, int code, const char *name)
569 {
570         if (nr < 0 || nr >= ARRAY_SIZE(ifsr_info))
571                 BUG();
572
573         ifsr_info[nr].fn   = fn;
574         ifsr_info[nr].sig  = sig;
575         ifsr_info[nr].code = code;
576         ifsr_info[nr].name = name;
577 }
578
579 asmlinkage void __exception
580 do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
581 {
582         const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
583         struct siginfo info;
584
585         if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
586                 return;
587
588         printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
589                 inf->name, ifsr, addr);
590
591         info.si_signo = inf->sig;
592         info.si_errno = 0;
593         info.si_code  = inf->code;
594         info.si_addr  = (void __user *)addr;
595         arm_notify_die("", regs, &info, ifsr, 0);
596 }
597
598 #ifndef CONFIG_ARM_LPAE
599 static int __init exceptions_init(void)
600 {
601         if (cpu_architecture() >= CPU_ARCH_ARMv6) {
602                 hook_fault_code(4, do_translation_fault, SIGSEGV, SEGV_MAPERR,
603                                 "I-cache maintenance fault");
604         }
605
606         if (cpu_architecture() >= CPU_ARCH_ARMv7) {
607                 /*
608                  * TODO: Access flag faults introduced in ARMv6K.
609                  * Runtime check for 'K' extension is needed
610                  */
611                 hook_fault_code(3, do_bad, SIGSEGV, SEGV_MAPERR,
612                                 "section access flag fault");
613                 hook_fault_code(6, do_bad, SIGSEGV, SEGV_MAPERR,
614                                 "section access flag fault");
615         }
616
617         return 0;
618 }
619
620 arch_initcall(exceptions_init);
621 #endif