Merge branch 'next-tpm' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[sfrench/cifs-2.6.git] / arch / unicore32 / mm / fault.c
1 /*
2  * linux/arch/unicore32/mm/fault.c
3  *
4  * Code specific to PKUnity SoC and UniCore ISA
5  *
6  * Copyright (C) 2001-2010 GUAN Xue-tao
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 #include <linux/extable.h>
13 #include <linux/signal.h>
14 #include <linux/mm.h>
15 #include <linux/hardirq.h>
16 #include <linux/init.h>
17 #include <linux/kprobes.h>
18 #include <linux/uaccess.h>
19 #include <linux/page-flags.h>
20 #include <linux/sched/signal.h>
21 #include <linux/io.h>
22
23 #include <asm/pgtable.h>
24 #include <asm/tlbflush.h>
25
26 /*
27  * Fault status register encodings.  We steal bit 31 for our own purposes.
28  */
29 #define FSR_LNX_PF              (1 << 31)
30
31 static inline int fsr_fs(unsigned int fsr)
32 {
33         /* xyabcde will be abcde+xy */
34         return (fsr & 31) + ((fsr & (3 << 5)) >> 5);
35 }
36
37 /*
38  * This is useful to dump out the page tables associated with
39  * 'addr' in mm 'mm'.
40  */
41 void show_pte(struct mm_struct *mm, unsigned long addr)
42 {
43         pgd_t *pgd;
44
45         if (!mm)
46                 mm = &init_mm;
47
48         printk(KERN_ALERT "pgd = %p\n", mm->pgd);
49         pgd = pgd_offset(mm, addr);
50         printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
51
52         do {
53                 pmd_t *pmd;
54                 pte_t *pte;
55
56                 if (pgd_none(*pgd))
57                         break;
58
59                 if (pgd_bad(*pgd)) {
60                         printk("(bad)");
61                         break;
62                 }
63
64                 pmd = pmd_offset((pud_t *) pgd, addr);
65                 if (PTRS_PER_PMD != 1)
66                         printk(", *pmd=%08lx", pmd_val(*pmd));
67
68                 if (pmd_none(*pmd))
69                         break;
70
71                 if (pmd_bad(*pmd)) {
72                         printk("(bad)");
73                         break;
74                 }
75
76                 /* We must not map this if we have highmem enabled */
77                 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
78                         break;
79
80                 pte = pte_offset_map(pmd, addr);
81                 printk(", *pte=%08lx", pte_val(*pte));
82                 pte_unmap(pte);
83         } while (0);
84
85         printk("\n");
86 }
87
88 /*
89  * Oops.  The kernel tried to access some page that wasn't present.
90  */
91 static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
92                 unsigned int fsr, struct pt_regs *regs)
93 {
94         /*
95          * Are we prepared to handle this kernel fault?
96          */
97         if (fixup_exception(regs))
98                 return;
99
100         /*
101          * No handler, we'll have to terminate things with extreme prejudice.
102          */
103         bust_spinlocks(1);
104         printk(KERN_ALERT
105                "Unable to handle kernel %s at virtual address %08lx\n",
106                (addr < PAGE_SIZE) ? "NULL pointer dereference" :
107                "paging request", addr);
108
109         show_pte(mm, addr);
110         die("Oops", regs, fsr);
111         bust_spinlocks(0);
112         do_exit(SIGKILL);
113 }
114
115 /*
116  * Something tried to access memory that isn't in our memory map..
117  * User mode accesses just cause a SIGSEGV
118  */
119 static void __do_user_fault(struct task_struct *tsk, unsigned long addr,
120                 unsigned int fsr, unsigned int sig, int code,
121                 struct pt_regs *regs)
122 {
123         tsk->thread.address = addr;
124         tsk->thread.error_code = fsr;
125         tsk->thread.trap_no = 14;
126         force_sig_fault(sig, code, (void __user *)addr, tsk);
127 }
128
129 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
130 {
131         struct task_struct *tsk = current;
132         struct mm_struct *mm = tsk->active_mm;
133
134         /*
135          * If we are in kernel mode at this point, we
136          * have no context to handle this fault with.
137          */
138         if (user_mode(regs))
139                 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
140         else
141                 __do_kernel_fault(mm, addr, fsr, regs);
142 }
143
144 #define VM_FAULT_BADMAP         0x010000
145 #define VM_FAULT_BADACCESS      0x020000
146
147 /*
148  * Check that the permissions on the VMA allow for the fault which occurred.
149  * If we encountered a write fault, we must have write permission, otherwise
150  * we allow any permission.
151  */
152 static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
153 {
154         unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
155
156         if (!(fsr ^ 0x12))      /* write? */
157                 mask = VM_WRITE;
158         if (fsr & FSR_LNX_PF)
159                 mask = VM_EXEC;
160
161         return vma->vm_flags & mask ? false : true;
162 }
163
164 static vm_fault_t __do_pf(struct mm_struct *mm, unsigned long addr,
165                 unsigned int fsr, unsigned int flags, struct task_struct *tsk)
166 {
167         struct vm_area_struct *vma;
168         vm_fault_t fault;
169
170         vma = find_vma(mm, addr);
171         fault = VM_FAULT_BADMAP;
172         if (unlikely(!vma))
173                 goto out;
174         if (unlikely(vma->vm_start > addr))
175                 goto check_stack;
176
177         /*
178          * Ok, we have a good vm_area for this
179          * memory access, so we can handle it.
180          */
181 good_area:
182         if (access_error(fsr, vma)) {
183                 fault = VM_FAULT_BADACCESS;
184                 goto out;
185         }
186
187         /*
188          * If for any reason at all we couldn't handle the fault, make
189          * sure we exit gracefully rather than endlessly redo the fault.
190          */
191         fault = handle_mm_fault(vma, addr & PAGE_MASK, flags);
192         return fault;
193
194 check_stack:
195         if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
196                 goto good_area;
197 out:
198         return fault;
199 }
200
201 static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
202 {
203         struct task_struct *tsk;
204         struct mm_struct *mm;
205         int sig, code;
206         vm_fault_t fault;
207         unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
208
209         tsk = current;
210         mm = tsk->mm;
211
212         /*
213          * If we're in an interrupt or have no user
214          * context, we must not take the fault..
215          */
216         if (faulthandler_disabled() || !mm)
217                 goto no_context;
218
219         if (user_mode(regs))
220                 flags |= FAULT_FLAG_USER;
221         if (!(fsr ^ 0x12))
222                 flags |= FAULT_FLAG_WRITE;
223
224         /*
225          * As per x86, we may deadlock here.  However, since the kernel only
226          * validly references user space from well defined areas of the code,
227          * we can bug out early if this is from code which shouldn't.
228          */
229         if (!down_read_trylock(&mm->mmap_sem)) {
230                 if (!user_mode(regs)
231                     && !search_exception_tables(regs->UCreg_pc))
232                         goto no_context;
233 retry:
234                 down_read(&mm->mmap_sem);
235         } else {
236                 /*
237                  * The above down_read_trylock() might have succeeded in
238                  * which case, we'll have missed the might_sleep() from
239                  * down_read()
240                  */
241                 might_sleep();
242 #ifdef CONFIG_DEBUG_VM
243                 if (!user_mode(regs) &&
244                     !search_exception_tables(regs->UCreg_pc))
245                         goto no_context;
246 #endif
247         }
248
249         fault = __do_pf(mm, addr, fsr, flags, tsk);
250
251         /* If we need to retry but a fatal signal is pending, handle the
252          * signal first. We do not need to release the mmap_sem because
253          * it would already be released in __lock_page_or_retry in
254          * mm/filemap.c. */
255         if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
256                 return 0;
257
258         if (!(fault & VM_FAULT_ERROR) && (flags & FAULT_FLAG_ALLOW_RETRY)) {
259                 if (fault & VM_FAULT_MAJOR)
260                         tsk->maj_flt++;
261                 else
262                         tsk->min_flt++;
263                 if (fault & VM_FAULT_RETRY) {
264                         /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
265                         * of starvation. */
266                         flags &= ~FAULT_FLAG_ALLOW_RETRY;
267                         goto retry;
268                 }
269         }
270
271         up_read(&mm->mmap_sem);
272
273         /*
274          * Handle the "normal" case first - VM_FAULT_MAJOR
275          */
276         if (likely(!(fault &
277                (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
278                 return 0;
279
280         /*
281          * If we are in kernel mode at this point, we
282          * have no context to handle this fault with.
283          */
284         if (!user_mode(regs))
285                 goto no_context;
286
287         if (fault & VM_FAULT_OOM) {
288                 /*
289                  * We ran out of memory, call the OOM killer, and return to
290                  * userspace (which will retry the fault, or kill us if we
291                  * got oom-killed)
292                  */
293                 pagefault_out_of_memory();
294                 return 0;
295         }
296
297         if (fault & VM_FAULT_SIGBUS) {
298                 /*
299                  * We had some memory, but were unable to
300                  * successfully fix up this page fault.
301                  */
302                 sig = SIGBUS;
303                 code = BUS_ADRERR;
304         } else {
305                 /*
306                  * Something tried to access memory that
307                  * isn't in our memory map..
308                  */
309                 sig = SIGSEGV;
310                 code = fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR;
311         }
312
313         __do_user_fault(tsk, addr, fsr, sig, code, regs);
314         return 0;
315
316 no_context:
317         __do_kernel_fault(mm, addr, fsr, regs);
318         return 0;
319 }
320
321 /*
322  * First Level Translation Fault Handler
323  *
324  * We enter here because the first level page table doesn't contain
325  * a valid entry for the address.
326  *
327  * If the address is in kernel space (>= TASK_SIZE), then we are
328  * probably faulting in the vmalloc() area.
329  *
330  * If the init_task's first level page tables contains the relevant
331  * entry, we copy the it to this task.  If not, we send the process
332  * a signal, fixup the exception, or oops the kernel.
333  *
334  * NOTE! We MUST NOT take any locks for this case. We may be in an
335  * interrupt or a critical region, and should only copy the information
336  * from the master page table, nothing more.
337  */
338 static int do_ifault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
339 {
340         unsigned int index;
341         pgd_t *pgd, *pgd_k;
342         pmd_t *pmd, *pmd_k;
343
344         if (addr < TASK_SIZE)
345                 return do_pf(addr, fsr, regs);
346
347         if (user_mode(regs))
348                 goto bad_area;
349
350         index = pgd_index(addr);
351
352         pgd = cpu_get_pgd() + index;
353         pgd_k = init_mm.pgd + index;
354
355         if (pgd_none(*pgd_k))
356                 goto bad_area;
357
358         pmd_k = pmd_offset((pud_t *) pgd_k, addr);
359         pmd = pmd_offset((pud_t *) pgd, addr);
360
361         if (pmd_none(*pmd_k))
362                 goto bad_area;
363
364         set_pmd(pmd, *pmd_k);
365         flush_pmd_entry(pmd);
366         return 0;
367
368 bad_area:
369         do_bad_area(addr, fsr, regs);
370         return 0;
371 }
372
373 /*
374  * This abort handler always returns "fault".
375  */
376 static int do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
377 {
378         return 1;
379 }
380
381 static int do_good(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
382 {
383         unsigned int res1, res2;
384
385         printk("dabt exception but no error!\n");
386
387         __asm__ __volatile__(
388                         "mff %0,f0\n"
389                         "mff %1,f1\n"
390                         : "=r"(res1), "=r"(res2)
391                         :
392                         : "memory");
393
394         printk(KERN_EMERG "r0 :%08x  r1 :%08x\n", res1, res2);
395         panic("shut up\n");
396         return 0;
397 }
398
399 static struct fsr_info {
400         int (*fn) (unsigned long addr, unsigned int fsr, struct pt_regs *regs);
401         int sig;
402         int code;
403         const char *name;
404 } fsr_info[] = {
405         /*
406          * The following are the standard Unicore-I and UniCore-II aborts.
407          */
408         { do_good,      SIGBUS,  0,             "no error"              },
409         { do_bad,       SIGBUS,  BUS_ADRALN,    "alignment exception"   },
410         { do_bad,       SIGBUS,  BUS_OBJERR,    "external exception"    },
411         { do_bad,       SIGBUS,  0,             "burst operation"       },
412         { do_bad,       SIGBUS,  0,             "unknown 00100"         },
413         { do_ifault,    SIGSEGV, SEGV_MAPERR,   "2nd level pt non-exist"},
414         { do_bad,       SIGBUS,  0,             "2nd lvl large pt non-exist" },
415         { do_bad,       SIGBUS,  0,             "invalid pte"           },
416         { do_pf,        SIGSEGV, SEGV_MAPERR,   "page miss"             },
417         { do_bad,       SIGBUS,  0,             "middle page miss"      },
418         { do_bad,       SIGBUS,  0,             "large page miss"       },
419         { do_pf,        SIGSEGV, SEGV_MAPERR,   "super page (section) miss" },
420         { do_bad,       SIGBUS,  0,             "unknown 01100"         },
421         { do_bad,       SIGBUS,  0,             "unknown 01101"         },
422         { do_bad,       SIGBUS,  0,             "unknown 01110"         },
423         { do_bad,       SIGBUS,  0,             "unknown 01111"         },
424         { do_bad,       SIGBUS,  0,             "addr: up 3G or IO"     },
425         { do_pf,        SIGSEGV, SEGV_ACCERR,   "read unreadable addr"  },
426         { do_pf,        SIGSEGV, SEGV_ACCERR,   "write unwriteable addr"},
427         { do_pf,        SIGSEGV, SEGV_ACCERR,   "exec unexecutable addr"},
428         { do_bad,       SIGBUS,  0,             "unknown 10100"         },
429         { do_bad,       SIGBUS,  0,             "unknown 10101"         },
430         { do_bad,       SIGBUS,  0,             "unknown 10110"         },
431         { do_bad,       SIGBUS,  0,             "unknown 10111"         },
432         { do_bad,       SIGBUS,  0,             "unknown 11000"         },
433         { do_bad,       SIGBUS,  0,             "unknown 11001"         },
434         { do_bad,       SIGBUS,  0,             "unknown 11010"         },
435         { do_bad,       SIGBUS,  0,             "unknown 11011"         },
436         { do_bad,       SIGBUS,  0,             "unknown 11100"         },
437         { do_bad,       SIGBUS,  0,             "unknown 11101"         },
438         { do_bad,       SIGBUS,  0,             "unknown 11110"         },
439         { do_bad,       SIGBUS,  0,             "unknown 11111"         }
440 };
441
442 void __init hook_fault_code(int nr,
443                 int (*fn) (unsigned long, unsigned int, struct pt_regs *),
444                 int sig, int code, const char *name)
445 {
446         if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
447                 BUG();
448
449         fsr_info[nr].fn   = fn;
450         fsr_info[nr].sig  = sig;
451         fsr_info[nr].code = code;
452         fsr_info[nr].name = name;
453 }
454
455 /*
456  * Dispatch a data abort to the relevant handler.
457  */
458 asmlinkage void do_DataAbort(unsigned long addr, unsigned int fsr,
459                         struct pt_regs *regs)
460 {
461         const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
462
463         if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
464                 return;
465
466         printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
467                inf->name, fsr, addr);
468
469         uc32_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
470                         fsr, 0);
471 }
472
473 asmlinkage void do_PrefetchAbort(unsigned long addr,
474                         unsigned int ifsr, struct pt_regs *regs)
475 {
476         const struct fsr_info *inf = fsr_info + fsr_fs(ifsr);
477
478         if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
479                 return;
480
481         printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
482                inf->name, ifsr, addr);
483
484         uc32_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
485                         ifsr, 0);
486 }