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