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