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/extable.h>
13 #include <linux/signal.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>
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
41 void show_pte(struct mm_struct *mm, unsigned long addr)
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));
64 pmd = pmd_offset((pud_t *) pgd, addr);
65 if (PTRS_PER_PMD != 1)
66 printk(", *pmd=%08lx", pmd_val(*pmd));
76 /* We must not map this if we have highmem enabled */
77 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
80 pte = pte_offset_map(pmd, addr);
81 printk(", *pte=%08lx", pte_val(*pte));
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))
101 * No handler, we'll have to terminate things with extreme prejudice.
105 "Unable to handle kernel %s at virtual address %08lx\n",
106 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
107 "paging request", addr);
110 die("Oops", regs, fsr);
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)
125 tsk->thread.address = addr;
126 tsk->thread.error_code = fsr;
127 tsk->thread.trap_no = 14;
132 si.si_addr = (void __user *)addr;
133 force_sig_info(sig, &si, tsk);
136 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
138 struct task_struct *tsk = current;
139 struct mm_struct *mm = tsk->active_mm;
142 * If we are in kernel mode at this point, we
143 * have no context to handle this fault with.
146 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
148 __do_kernel_fault(mm, addr, fsr, regs);
151 #define VM_FAULT_BADMAP 0x010000
152 #define VM_FAULT_BADACCESS 0x020000
155 * Check that the permissions on the VMA allow for the fault which occurred.
156 * If we encountered a write fault, we must have write permission, otherwise
157 * we allow any permission.
159 static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
161 unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
163 if (!(fsr ^ 0x12)) /* write? */
165 if (fsr & FSR_LNX_PF)
168 return vma->vm_flags & mask ? false : true;
171 static vm_fault_t __do_pf(struct mm_struct *mm, unsigned long addr,
172 unsigned int fsr, unsigned int flags, struct task_struct *tsk)
174 struct vm_area_struct *vma;
177 vma = find_vma(mm, addr);
178 fault = VM_FAULT_BADMAP;
181 if (unlikely(vma->vm_start > addr))
185 * Ok, we have a good vm_area for this
186 * memory access, so we can handle it.
189 if (access_error(fsr, vma)) {
190 fault = VM_FAULT_BADACCESS;
195 * If for any reason at all we couldn't handle the fault, make
196 * sure we exit gracefully rather than endlessly redo the fault.
198 fault = handle_mm_fault(vma, addr & PAGE_MASK, flags);
202 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
208 static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
210 struct task_struct *tsk;
211 struct mm_struct *mm;
214 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
220 * If we're in an interrupt or have no user
221 * context, we must not take the fault..
223 if (faulthandler_disabled() || !mm)
227 flags |= FAULT_FLAG_USER;
229 flags |= FAULT_FLAG_WRITE;
232 * As per x86, we may deadlock here. However, since the kernel only
233 * validly references user space from well defined areas of the code,
234 * we can bug out early if this is from code which shouldn't.
236 if (!down_read_trylock(&mm->mmap_sem)) {
238 && !search_exception_tables(regs->UCreg_pc))
241 down_read(&mm->mmap_sem);
244 * The above down_read_trylock() might have succeeded in
245 * which case, we'll have missed the might_sleep() from
249 #ifdef CONFIG_DEBUG_VM
250 if (!user_mode(regs) &&
251 !search_exception_tables(regs->UCreg_pc))
256 fault = __do_pf(mm, addr, fsr, flags, tsk);
258 /* If we need to retry but a fatal signal is pending, handle the
259 * signal first. We do not need to release the mmap_sem because
260 * it would already be released in __lock_page_or_retry in
262 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
265 if (!(fault & VM_FAULT_ERROR) && (flags & FAULT_FLAG_ALLOW_RETRY)) {
266 if (fault & VM_FAULT_MAJOR)
270 if (fault & VM_FAULT_RETRY) {
271 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
273 flags &= ~FAULT_FLAG_ALLOW_RETRY;
278 up_read(&mm->mmap_sem);
281 * Handle the "normal" case first - VM_FAULT_MAJOR
284 (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
288 * If we are in kernel mode at this point, we
289 * have no context to handle this fault with.
291 if (!user_mode(regs))
294 if (fault & VM_FAULT_OOM) {
296 * We ran out of memory, call the OOM killer, and return to
297 * userspace (which will retry the fault, or kill us if we
300 pagefault_out_of_memory();
304 if (fault & VM_FAULT_SIGBUS) {
306 * We had some memory, but were unable to
307 * successfully fix up this page fault.
313 * Something tried to access memory that
314 * isn't in our memory map..
317 code = fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR;
320 __do_user_fault(tsk, addr, fsr, sig, code, regs);
324 __do_kernel_fault(mm, addr, fsr, regs);
329 * First Level Translation Fault Handler
331 * We enter here because the first level page table doesn't contain
332 * a valid entry for the address.
334 * If the address is in kernel space (>= TASK_SIZE), then we are
335 * probably faulting in the vmalloc() area.
337 * If the init_task's first level page tables contains the relevant
338 * entry, we copy the it to this task. If not, we send the process
339 * a signal, fixup the exception, or oops the kernel.
341 * NOTE! We MUST NOT take any locks for this case. We may be in an
342 * interrupt or a critical region, and should only copy the information
343 * from the master page table, nothing more.
345 static int do_ifault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
351 if (addr < TASK_SIZE)
352 return do_pf(addr, fsr, regs);
357 index = pgd_index(addr);
359 pgd = cpu_get_pgd() + index;
360 pgd_k = init_mm.pgd + index;
362 if (pgd_none(*pgd_k))
365 pmd_k = pmd_offset((pud_t *) pgd_k, addr);
366 pmd = pmd_offset((pud_t *) pgd, addr);
368 if (pmd_none(*pmd_k))
371 set_pmd(pmd, *pmd_k);
372 flush_pmd_entry(pmd);
376 do_bad_area(addr, fsr, regs);
381 * This abort handler always returns "fault".
383 static int do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
388 static int do_good(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
390 unsigned int res1, res2;
392 printk("dabt exception but no error!\n");
394 __asm__ __volatile__(
397 : "=r"(res1), "=r"(res2)
401 printk(KERN_EMERG "r0 :%08x r1 :%08x\n", res1, res2);
406 static struct fsr_info {
407 int (*fn) (unsigned long addr, unsigned int fsr, struct pt_regs *regs);
413 * The following are the standard Unicore-I and UniCore-II aborts.
415 { do_good, SIGBUS, 0, "no error" },
416 { do_bad, SIGBUS, BUS_ADRALN, "alignment exception" },
417 { do_bad, SIGBUS, BUS_OBJERR, "external exception" },
418 { do_bad, SIGBUS, 0, "burst operation" },
419 { do_bad, SIGBUS, 0, "unknown 00100" },
420 { do_ifault, SIGSEGV, SEGV_MAPERR, "2nd level pt non-exist"},
421 { do_bad, SIGBUS, 0, "2nd lvl large pt non-exist" },
422 { do_bad, SIGBUS, 0, "invalid pte" },
423 { do_pf, SIGSEGV, SEGV_MAPERR, "page miss" },
424 { do_bad, SIGBUS, 0, "middle page miss" },
425 { do_bad, SIGBUS, 0, "large page miss" },
426 { do_pf, SIGSEGV, SEGV_MAPERR, "super page (section) miss" },
427 { do_bad, SIGBUS, 0, "unknown 01100" },
428 { do_bad, SIGBUS, 0, "unknown 01101" },
429 { do_bad, SIGBUS, 0, "unknown 01110" },
430 { do_bad, SIGBUS, 0, "unknown 01111" },
431 { do_bad, SIGBUS, 0, "addr: up 3G or IO" },
432 { do_pf, SIGSEGV, SEGV_ACCERR, "read unreadable addr" },
433 { do_pf, SIGSEGV, SEGV_ACCERR, "write unwriteable addr"},
434 { do_pf, SIGSEGV, SEGV_ACCERR, "exec unexecutable addr"},
435 { do_bad, SIGBUS, 0, "unknown 10100" },
436 { do_bad, SIGBUS, 0, "unknown 10101" },
437 { do_bad, SIGBUS, 0, "unknown 10110" },
438 { do_bad, SIGBUS, 0, "unknown 10111" },
439 { do_bad, SIGBUS, 0, "unknown 11000" },
440 { do_bad, SIGBUS, 0, "unknown 11001" },
441 { do_bad, SIGBUS, 0, "unknown 11010" },
442 { do_bad, SIGBUS, 0, "unknown 11011" },
443 { do_bad, SIGBUS, 0, "unknown 11100" },
444 { do_bad, SIGBUS, 0, "unknown 11101" },
445 { do_bad, SIGBUS, 0, "unknown 11110" },
446 { do_bad, SIGBUS, 0, "unknown 11111" }
449 void __init hook_fault_code(int nr,
450 int (*fn) (unsigned long, unsigned int, struct pt_regs *),
451 int sig, int code, const char *name)
453 if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
456 fsr_info[nr].fn = fn;
457 fsr_info[nr].sig = sig;
458 fsr_info[nr].code = code;
459 fsr_info[nr].name = name;
463 * Dispatch a data abort to the relevant handler.
465 asmlinkage void do_DataAbort(unsigned long addr, unsigned int fsr,
466 struct pt_regs *regs)
468 const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
471 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
474 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
475 inf->name, fsr, addr);
477 clear_siginfo(&info);
478 info.si_signo = inf->sig;
480 info.si_code = inf->code;
481 info.si_addr = (void __user *)addr;
482 uc32_notify_die("", regs, &info, fsr, 0);
485 asmlinkage void do_PrefetchAbort(unsigned long addr,
486 unsigned int ifsr, struct pt_regs *regs)
488 const struct fsr_info *inf = fsr_info + fsr_fs(ifsr);
491 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
494 printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
495 inf->name, ifsr, addr);
497 clear_siginfo(&info);
498 info.si_signo = inf->sig;
500 info.si_code = inf->code;
501 info.si_addr = (void __user *)addr;
502 uc32_notify_die("", regs, &info, ifsr, 0);