Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6/libata-dev.git] / arch / unicore32 / mm / fault.c
blobf9b5c10bccee96e8838484aaf6effc39b3c89bd1
1 /*
2 * linux/arch/unicore32/mm/fault.c
4 * Code specific to PKUnity SoC and UniCore ISA
6 * Copyright (C) 2001-2010 GUAN Xue-tao
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.
12 #include <linux/module.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.h>
21 #include <linux/io.h>
23 #include <asm/pgtable.h>
24 #include <asm/tlbflush.h>
27 * Fault status register encodings. We steal bit 31 for our own purposes.
29 #define FSR_LNX_PF (1 << 31)
31 static inline int fsr_fs(unsigned int fsr)
33 /* xyabcde will be abcde+xy */
34 return (fsr & 31) + ((fsr & (3 << 5)) >> 5);
38 * This is useful to dump out the page tables associated with
39 * 'addr' in mm 'mm'.
41 void show_pte(struct mm_struct *mm, unsigned long addr)
43 pgd_t *pgd;
45 if (!mm)
46 mm = &init_mm;
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));
52 do {
53 pmd_t *pmd;
54 pte_t *pte;
56 if (pgd_none(*pgd))
57 break;
59 if (pgd_bad(*pgd)) {
60 printk("(bad)");
61 break;
64 pmd = pmd_offset((pud_t *) pgd, addr);
65 if (PTRS_PER_PMD != 1)
66 printk(", *pmd=%08lx", pmd_val(*pmd));
68 if (pmd_none(*pmd))
69 break;
71 if (pmd_bad(*pmd)) {
72 printk("(bad)");
73 break;
76 /* We must not map this if we have highmem enabled */
77 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
78 break;
80 pte = pte_offset_map(pmd, addr);
81 printk(", *pte=%08lx", pte_val(*pte));
82 pte_unmap(pte);
83 } while (0);
85 printk("\n");
89 * Oops. The kernel tried to access some page that wasn't present.
91 static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
92 unsigned int fsr, struct pt_regs *regs)
95 * Are we prepared to handle this kernel fault?
97 if (fixup_exception(regs))
98 return;
101 * No handler, we'll have to terminate things with extreme prejudice.
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);
109 show_pte(mm, addr);
110 die("Oops", regs, fsr);
111 bust_spinlocks(0);
112 do_exit(SIGKILL);
116 * Something tried to access memory that isn't in our memory map..
117 * User mode accesses just cause a SIGSEGV
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)
123 struct siginfo si;
125 tsk->thread.address = addr;
126 tsk->thread.error_code = fsr;
127 tsk->thread.trap_no = 14;
128 si.si_signo = sig;
129 si.si_errno = 0;
130 si.si_code = code;
131 si.si_addr = (void __user *)addr;
132 force_sig_info(sig, &si, tsk);
135 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
137 struct task_struct *tsk = current;
138 struct mm_struct *mm = tsk->active_mm;
141 * If we are in kernel mode at this point, we
142 * have no context to handle this fault with.
144 if (user_mode(regs))
145 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
146 else
147 __do_kernel_fault(mm, addr, fsr, regs);
150 #define VM_FAULT_BADMAP 0x010000
151 #define VM_FAULT_BADACCESS 0x020000
154 * Check that the permissions on the VMA allow for the fault which occurred.
155 * If we encountered a write fault, we must have write permission, otherwise
156 * we allow any permission.
158 static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
160 unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
162 if (!(fsr ^ 0x12)) /* write? */
163 mask = VM_WRITE;
164 if (fsr & FSR_LNX_PF)
165 mask = VM_EXEC;
167 return vma->vm_flags & mask ? false : true;
170 static int __do_pf(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
171 unsigned int flags, struct task_struct *tsk)
173 struct vm_area_struct *vma;
174 int fault;
176 vma = find_vma(mm, addr);
177 fault = VM_FAULT_BADMAP;
178 if (unlikely(!vma))
179 goto out;
180 if (unlikely(vma->vm_start > addr))
181 goto check_stack;
184 * Ok, we have a good vm_area for this
185 * memory access, so we can handle it.
187 good_area:
188 if (access_error(fsr, vma)) {
189 fault = VM_FAULT_BADACCESS;
190 goto out;
194 * If for any reason at all we couldn't handle the fault, make
195 * sure we exit gracefully rather than endlessly redo the fault.
197 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, flags);
198 return fault;
200 check_stack:
201 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
202 goto good_area;
203 out:
204 return fault;
207 static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
209 struct task_struct *tsk;
210 struct mm_struct *mm;
211 int fault, sig, code;
212 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
213 ((!(fsr ^ 0x12)) ? FAULT_FLAG_WRITE : 0);
215 tsk = current;
216 mm = tsk->mm;
219 * If we're in an interrupt or have no user
220 * context, we must not take the fault..
222 if (in_atomic() || !mm)
223 goto no_context;
226 * As per x86, we may deadlock here. However, since the kernel only
227 * validly references user space from well defined areas of the code,
228 * we can bug out early if this is from code which shouldn't.
230 if (!down_read_trylock(&mm->mmap_sem)) {
231 if (!user_mode(regs)
232 && !search_exception_tables(regs->UCreg_pc))
233 goto no_context;
234 retry:
235 down_read(&mm->mmap_sem);
236 } else {
238 * The above down_read_trylock() might have succeeded in
239 * which case, we'll have missed the might_sleep() from
240 * down_read()
242 might_sleep();
243 #ifdef CONFIG_DEBUG_VM
244 if (!user_mode(regs) &&
245 !search_exception_tables(regs->UCreg_pc))
246 goto no_context;
247 #endif
250 fault = __do_pf(mm, addr, fsr, flags, tsk);
252 /* If we need to retry but a fatal signal is pending, handle the
253 * signal first. We do not need to release the mmap_sem because
254 * it would already be released in __lock_page_or_retry in
255 * mm/filemap.c. */
256 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
257 return 0;
259 if (!(fault & VM_FAULT_ERROR) && (flags & FAULT_FLAG_ALLOW_RETRY)) {
260 if (fault & VM_FAULT_MAJOR)
261 tsk->maj_flt++;
262 else
263 tsk->min_flt++;
264 if (fault & VM_FAULT_RETRY) {
265 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
266 * of starvation. */
267 flags &= ~FAULT_FLAG_ALLOW_RETRY;
268 goto retry;
272 up_read(&mm->mmap_sem);
275 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
277 if (likely(!(fault &
278 (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
279 return 0;
281 if (fault & VM_FAULT_OOM) {
283 * We ran out of memory, call the OOM killer, and return to
284 * userspace (which will retry the fault, or kill us if we
285 * got oom-killed)
287 pagefault_out_of_memory();
288 return 0;
292 * If we are in kernel mode at this point, we
293 * have no context to handle this fault with.
295 if (!user_mode(regs))
296 goto no_context;
298 if (fault & VM_FAULT_SIGBUS) {
300 * We had some memory, but were unable to
301 * successfully fix up this page fault.
303 sig = SIGBUS;
304 code = BUS_ADRERR;
305 } else {
307 * Something tried to access memory that
308 * isn't in our memory map..
310 sig = SIGSEGV;
311 code = fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR;
314 __do_user_fault(tsk, addr, fsr, sig, code, regs);
315 return 0;
317 no_context:
318 __do_kernel_fault(mm, addr, fsr, regs);
319 return 0;
323 * First Level Translation Fault Handler
325 * We enter here because the first level page table doesn't contain
326 * a valid entry for the address.
328 * If the address is in kernel space (>= TASK_SIZE), then we are
329 * probably faulting in the vmalloc() area.
331 * If the init_task's first level page tables contains the relevant
332 * entry, we copy the it to this task. If not, we send the process
333 * a signal, fixup the exception, or oops the kernel.
335 * NOTE! We MUST NOT take any locks for this case. We may be in an
336 * interrupt or a critical region, and should only copy the information
337 * from the master page table, nothing more.
339 static int do_ifault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
341 unsigned int index;
342 pgd_t *pgd, *pgd_k;
343 pmd_t *pmd, *pmd_k;
345 if (addr < TASK_SIZE)
346 return do_pf(addr, fsr, regs);
348 if (user_mode(regs))
349 goto bad_area;
351 index = pgd_index(addr);
353 pgd = cpu_get_pgd() + index;
354 pgd_k = init_mm.pgd + index;
356 if (pgd_none(*pgd_k))
357 goto bad_area;
359 pmd_k = pmd_offset((pud_t *) pgd_k, addr);
360 pmd = pmd_offset((pud_t *) pgd, addr);
362 if (pmd_none(*pmd_k))
363 goto bad_area;
365 set_pmd(pmd, *pmd_k);
366 flush_pmd_entry(pmd);
367 return 0;
369 bad_area:
370 do_bad_area(addr, fsr, regs);
371 return 0;
375 * This abort handler always returns "fault".
377 static int do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
379 return 1;
382 static int do_good(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
384 unsigned int res1, res2;
386 printk("dabt exception but no error!\n");
388 __asm__ __volatile__(
389 "mff %0,f0\n"
390 "mff %1,f1\n"
391 : "=r"(res1), "=r"(res2)
393 : "memory");
395 printk(KERN_EMERG "r0 :%08x r1 :%08x\n", res1, res2);
396 panic("shut up\n");
397 return 0;
400 static struct fsr_info {
401 int (*fn) (unsigned long addr, unsigned int fsr, struct pt_regs *regs);
402 int sig;
403 int code;
404 const char *name;
405 } fsr_info[] = {
407 * The following are the standard Unicore-I and UniCore-II aborts.
409 { do_good, SIGBUS, 0, "no error" },
410 { do_bad, SIGBUS, BUS_ADRALN, "alignment exception" },
411 { do_bad, SIGBUS, BUS_OBJERR, "external exception" },
412 { do_bad, SIGBUS, 0, "burst operation" },
413 { do_bad, SIGBUS, 0, "unknown 00100" },
414 { do_ifault, SIGSEGV, SEGV_MAPERR, "2nd level pt non-exist"},
415 { do_bad, SIGBUS, 0, "2nd lvl large pt non-exist" },
416 { do_bad, SIGBUS, 0, "invalid pte" },
417 { do_pf, SIGSEGV, SEGV_MAPERR, "page miss" },
418 { do_bad, SIGBUS, 0, "middle page miss" },
419 { do_bad, SIGBUS, 0, "large page miss" },
420 { do_pf, SIGSEGV, SEGV_MAPERR, "super page (section) miss" },
421 { do_bad, SIGBUS, 0, "unknown 01100" },
422 { do_bad, SIGBUS, 0, "unknown 01101" },
423 { do_bad, SIGBUS, 0, "unknown 01110" },
424 { do_bad, SIGBUS, 0, "unknown 01111" },
425 { do_bad, SIGBUS, 0, "addr: up 3G or IO" },
426 { do_pf, SIGSEGV, SEGV_ACCERR, "read unreadable addr" },
427 { do_pf, SIGSEGV, SEGV_ACCERR, "write unwriteable addr"},
428 { do_pf, SIGSEGV, SEGV_ACCERR, "exec unexecutable addr"},
429 { do_bad, SIGBUS, 0, "unknown 10100" },
430 { do_bad, SIGBUS, 0, "unknown 10101" },
431 { do_bad, SIGBUS, 0, "unknown 10110" },
432 { do_bad, SIGBUS, 0, "unknown 10111" },
433 { do_bad, SIGBUS, 0, "unknown 11000" },
434 { do_bad, SIGBUS, 0, "unknown 11001" },
435 { do_bad, SIGBUS, 0, "unknown 11010" },
436 { do_bad, SIGBUS, 0, "unknown 11011" },
437 { do_bad, SIGBUS, 0, "unknown 11100" },
438 { do_bad, SIGBUS, 0, "unknown 11101" },
439 { do_bad, SIGBUS, 0, "unknown 11110" },
440 { do_bad, SIGBUS, 0, "unknown 11111" }
443 void __init hook_fault_code(int nr,
444 int (*fn) (unsigned long, unsigned int, struct pt_regs *),
445 int sig, int code, const char *name)
447 if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
448 BUG();
450 fsr_info[nr].fn = fn;
451 fsr_info[nr].sig = sig;
452 fsr_info[nr].code = code;
453 fsr_info[nr].name = name;
457 * Dispatch a data abort to the relevant handler.
459 asmlinkage void do_DataAbort(unsigned long addr, unsigned int fsr,
460 struct pt_regs *regs)
462 const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
463 struct siginfo info;
465 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
466 return;
468 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
469 inf->name, fsr, addr);
471 info.si_signo = inf->sig;
472 info.si_errno = 0;
473 info.si_code = inf->code;
474 info.si_addr = (void __user *)addr;
475 uc32_notify_die("", regs, &info, fsr, 0);
478 asmlinkage void do_PrefetchAbort(unsigned long addr,
479 unsigned int ifsr, struct pt_regs *regs)
481 const struct fsr_info *inf = fsr_info + fsr_fs(ifsr);
482 struct siginfo info;
484 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
485 return;
487 printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
488 inf->name, ifsr, addr);
490 info.si_signo = inf->sig;
491 info.si_errno = 0;
492 info.si_code = inf->code;
493 info.si_addr = (void __user *)addr;
494 uc32_notify_die("", regs, &info, ifsr, 0);