Merge branch 'stable-3.2' into pandora-3.2
[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                 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x (%s)\n",
202                        tsk->comm, sig, addr, fsr,
203                        (fsr & FSR_WRITE) ? "write" : "read");
204                 print_user_faulter_location(tsk->comm, regs);
205         }
206         if (user_debug & UDBG_SEGV) {
207                 show_pte(tsk->mm, addr);
208                 show_regs(regs);
209         }
210 #endif
211
212         tsk->thread.address = addr;
213         tsk->thread.error_code = fsr;
214         tsk->thread.trap_no = 14;
215         si.si_signo = sig;
216         si.si_errno = 0;
217         si.si_code = code;
218         si.si_addr = (void __user *)addr;
219         force_sig_info(sig, &si, tsk);
220 }
221
222 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
223 {
224         struct task_struct *tsk = current;
225         struct mm_struct *mm = tsk->active_mm;
226
227         /*
228          * If we are in kernel mode at this point, we
229          * have no context to handle this fault with.
230          */
231         if (user_mode(regs))
232                 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
233         else
234                 __do_kernel_fault(mm, addr, fsr, regs);
235 }
236
237 #ifdef CONFIG_MMU
238 #define VM_FAULT_BADMAP         0x010000
239 #define VM_FAULT_BADACCESS      0x020000
240
241 /*
242  * Check that the permissions on the VMA allow for the fault which occurred.
243  * If we encountered a write fault, we must have write permission, otherwise
244  * we allow any permission.
245  */
246 static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
247 {
248         unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
249
250         if (fsr & FSR_WRITE)
251                 mask = VM_WRITE;
252         if (fsr & FSR_LNX_PF)
253                 mask = VM_EXEC;
254
255         return vma->vm_flags & mask ? false : true;
256 }
257
258 static int __kprobes
259 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
260                 struct task_struct *tsk)
261 {
262         struct vm_area_struct *vma;
263         int fault;
264
265         vma = find_vma(mm, addr);
266         fault = VM_FAULT_BADMAP;
267         if (unlikely(!vma))
268                 goto out;
269         if (unlikely(vma->vm_start > addr))
270                 goto check_stack;
271
272         /*
273          * Ok, we have a good vm_area for this
274          * memory access, so we can handle it.
275          */
276 good_area:
277         if (access_error(fsr, vma)) {
278                 fault = VM_FAULT_BADACCESS;
279                 goto out;
280         }
281
282         /*
283          * If for any reason at all we couldn't handle the fault, make
284          * sure we exit gracefully rather than endlessly redo the fault.
285          */
286         fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & FSR_WRITE) ? FAULT_FLAG_WRITE : 0);
287         if (unlikely(fault & VM_FAULT_ERROR))
288                 return fault;
289         if (fault & VM_FAULT_MAJOR)
290                 tsk->maj_flt++;
291         else
292                 tsk->min_flt++;
293         return fault;
294
295 check_stack:
296         /* Don't allow expansion below FIRST_USER_ADDRESS */
297         if (vma->vm_flags & VM_GROWSDOWN &&
298             addr >= FIRST_USER_ADDRESS && !expand_stack(vma, addr))
299                 goto good_area;
300 out:
301         return fault;
302 }
303
304 static int __kprobes
305 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
306 {
307         struct task_struct *tsk;
308         struct mm_struct *mm;
309         int fault, sig, code;
310
311         if (notify_page_fault(regs, fsr))
312                 return 0;
313
314         tsk = current;
315         mm  = tsk->mm;
316
317         /* Enable interrupts if they were enabled in the parent context. */
318         if (interrupts_enabled(regs))
319                 local_irq_enable();
320
321         /*
322          * If we're in an interrupt or have no user
323          * context, we must not take the fault..
324          */
325         if (in_atomic() || !mm)
326                 goto no_context;
327
328         /*
329          * As per x86, we may deadlock here.  However, since the kernel only
330          * validly references user space from well defined areas of the code,
331          * we can bug out early if this is from code which shouldn't.
332          */
333         if (!down_read_trylock(&mm->mmap_sem)) {
334                 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
335                         goto no_context;
336                 down_read(&mm->mmap_sem);
337         } else {
338                 /*
339                  * The above down_read_trylock() might have succeeded in
340                  * which case, we'll have missed the might_sleep() from
341                  * down_read()
342                  */
343                 might_sleep();
344 #ifdef CONFIG_DEBUG_VM
345                 if (!user_mode(regs) &&
346                     !search_exception_tables(regs->ARM_pc))
347                         goto no_context;
348 #endif
349         }
350
351         fault = __do_page_fault(mm, addr, fsr, tsk);
352         up_read(&mm->mmap_sem);
353
354         perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
355         if (fault & VM_FAULT_MAJOR)
356                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, addr);
357         else if (fault & VM_FAULT_MINOR)
358                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, addr);
359
360         /*
361          * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
362          */
363         if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
364                 return 0;
365
366         if (fault & VM_FAULT_OOM) {
367                 /*
368                  * We ran out of memory, call the OOM killer, and return to
369                  * userspace (which will retry the fault, or kill us if we
370                  * got oom-killed)
371                  */
372                 pagefault_out_of_memory();
373                 return 0;
374         }
375
376         /*
377          * If we are in kernel mode at this point, we
378          * have no context to handle this fault with.
379          */
380         if (!user_mode(regs))
381                 goto no_context;
382
383         if (fault & VM_FAULT_SIGBUS) {
384                 /*
385                  * We had some memory, but were unable to
386                  * successfully fix up this page fault.
387                  */
388                 sig = SIGBUS;
389                 code = BUS_ADRERR;
390         } else {
391                 /*
392                  * Something tried to access memory that
393                  * isn't in our memory map..
394                  */
395                 sig = SIGSEGV;
396                 code = fault == VM_FAULT_BADACCESS ?
397                         SEGV_ACCERR : SEGV_MAPERR;
398         }
399
400         __do_user_fault(tsk, addr, fsr, sig, code, regs);
401         return 0;
402
403 no_context:
404         __do_kernel_fault(mm, addr, fsr, regs);
405         return 0;
406 }
407 #else                                   /* CONFIG_MMU */
408 static int
409 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
410 {
411         return 0;
412 }
413 #endif                                  /* CONFIG_MMU */
414
415 /*
416  * First Level Translation Fault Handler
417  *
418  * We enter here because the first level page table doesn't contain
419  * a valid entry for the address.
420  *
421  * If the address is in kernel space (>= TASK_SIZE), then we are
422  * probably faulting in the vmalloc() area.
423  *
424  * If the init_task's first level page tables contains the relevant
425  * entry, we copy the it to this task.  If not, we send the process
426  * a signal, fixup the exception, or oops the kernel.
427  *
428  * NOTE! We MUST NOT take any locks for this case. We may be in an
429  * interrupt or a critical region, and should only copy the information
430  * from the master page table, nothing more.
431  */
432 #ifdef CONFIG_MMU
433 static int __kprobes
434 do_translation_fault(unsigned long addr, unsigned int fsr,
435                      struct pt_regs *regs)
436 {
437         unsigned int index;
438         pgd_t *pgd, *pgd_k;
439         pud_t *pud, *pud_k;
440         pmd_t *pmd, *pmd_k;
441
442         if (addr < TASK_SIZE)
443                 return do_page_fault(addr, fsr, regs);
444
445         if (user_mode(regs))
446                 goto bad_area;
447
448         index = pgd_index(addr);
449
450         /*
451          * FIXME: CP15 C1 is write only on ARMv3 architectures.
452          */
453         pgd = cpu_get_pgd() + index;
454         pgd_k = init_mm.pgd + index;
455
456         if (pgd_none(*pgd_k))
457                 goto bad_area;
458         if (!pgd_present(*pgd))
459                 set_pgd(pgd, *pgd_k);
460
461         pud = pud_offset(pgd, addr);
462         pud_k = pud_offset(pgd_k, addr);
463
464         if (pud_none(*pud_k))
465                 goto bad_area;
466         if (!pud_present(*pud))
467                 set_pud(pud, *pud_k);
468
469         pmd = pmd_offset(pud, addr);
470         pmd_k = pmd_offset(pud_k, addr);
471
472 #ifdef CONFIG_ARM_LPAE
473         /*
474          * Only one hardware entry per PMD with LPAE.
475          */
476         index = 0;
477 #else
478         /*
479          * On ARM one Linux PGD entry contains two hardware entries (see page
480          * tables layout in pgtable.h). We normally guarantee that we always
481          * fill both L1 entries. But create_mapping() doesn't follow the rule.
482          * It can create inidividual L1 entries, so here we have to call
483          * pmd_none() check for the entry really corresponded to address, not
484          * for the first of pair.
485          */
486         index = (addr >> SECTION_SHIFT) & 1;
487 #endif
488         if (pmd_none(pmd_k[index]))
489                 goto bad_area;
490
491         copy_pmd(pmd, pmd_k);
492         return 0;
493
494 bad_area:
495         do_bad_area(addr, fsr, regs);
496         return 0;
497 }
498 #else                                   /* CONFIG_MMU */
499 static int
500 do_translation_fault(unsigned long addr, unsigned int fsr,
501                      struct pt_regs *regs)
502 {
503         return 0;
504 }
505 #endif                                  /* CONFIG_MMU */
506
507 /*
508  * This abort handler always returns "fault".
509  */
510 static int
511 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
512 {
513         return 1;
514 }
515
516 struct fsr_info {
517         int     (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
518         int     sig;
519         int     code;
520         const char *name;
521 };
522
523 /* FSR definition */
524 #ifdef CONFIG_ARM_LPAE
525 #include "fsr-3level.c"
526 #else
527 #include "fsr-2level.c"
528 #endif
529
530 void __init
531 hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
532                 int sig, int code, const char *name)
533 {
534         if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
535                 BUG();
536
537         fsr_info[nr].fn   = fn;
538         fsr_info[nr].sig  = sig;
539         fsr_info[nr].code = code;
540         fsr_info[nr].name = name;
541 }
542
543 /*
544  * Dispatch a data abort to the relevant handler.
545  */
546 asmlinkage void __exception
547 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
548 {
549         const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
550         struct siginfo info;
551
552         if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
553                 return;
554
555         printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
556                 inf->name, fsr, addr);
557
558         info.si_signo = inf->sig;
559         info.si_errno = 0;
560         info.si_code  = inf->code;
561         info.si_addr  = (void __user *)addr;
562         arm_notify_die("", regs, &info, fsr, 0);
563 }
564
565 void __init
566 hook_ifault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
567                  int sig, int code, const char *name)
568 {
569         if (nr < 0 || nr >= ARRAY_SIZE(ifsr_info))
570                 BUG();
571
572         ifsr_info[nr].fn   = fn;
573         ifsr_info[nr].sig  = sig;
574         ifsr_info[nr].code = code;
575         ifsr_info[nr].name = name;
576 }
577
578 asmlinkage void __exception
579 do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
580 {
581         const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
582         struct siginfo info;
583
584         if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
585                 return;
586
587         printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
588                 inf->name, ifsr, addr);
589
590         info.si_signo = inf->sig;
591         info.si_errno = 0;
592         info.si_code  = inf->code;
593         info.si_addr  = (void __user *)addr;
594         arm_notify_die("", regs, &info, ifsr, 0);
595 }
596
597 #ifndef CONFIG_ARM_LPAE
598 static int __init exceptions_init(void)
599 {
600         if (cpu_architecture() >= CPU_ARCH_ARMv6) {
601                 hook_fault_code(4, do_translation_fault, SIGSEGV, SEGV_MAPERR,
602                                 "I-cache maintenance fault");
603         }
604
605         if (cpu_architecture() >= CPU_ARCH_ARMv7) {
606                 /*
607                  * TODO: Access flag faults introduced in ARMv6K.
608                  * Runtime check for 'K' extension is needed
609                  */
610                 hook_fault_code(3, do_bad, SIGSEGV, SEGV_MAPERR,
611                                 "section access flag fault");
612                 hook_fault_code(6, do_bad, SIGSEGV, SEGV_MAPERR,
613                                 "section access flag fault");
614         }
615
616         return 0;
617 }
618
619 arch_initcall(exceptions_init);
620 #endif