1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/unicore32/mm/fault.c
5 * Code specific to PKUnity SoC and UniCore ISA
7 * Copyright (C) 2001-2010 GUAN Xue-tao
9 #include <linux/extable.h>
10 #include <linux/signal.h>
12 #include <linux/hardirq.h>
13 #include <linux/init.h>
14 #include <linux/kprobes.h>
15 #include <linux/uaccess.h>
16 #include <linux/page-flags.h>
17 #include <linux/sched/signal.h>
20 #include <asm/pgtable.h>
21 #include <asm/tlbflush.h>
24 * Fault status register encodings. We steal bit 31 for our own purposes.
26 #define FSR_LNX_PF (1 << 31)
28 static inline int fsr_fs(unsigned int fsr)
30 /* xyabcde will be abcde+xy */
31 return (fsr & 31) + ((fsr & (3 << 5)) >> 5);
35 * This is useful to dump out the page tables associated with
38 void show_pte(struct mm_struct *mm, unsigned long addr)
45 printk(KERN_ALERT "pgd = %p\n", mm->pgd);
46 pgd = pgd_offset(mm, addr);
47 printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
61 pmd = pmd_offset((pud_t *) pgd, addr);
62 if (PTRS_PER_PMD != 1)
63 printk(", *pmd=%08lx", pmd_val(*pmd));
73 /* We must not map this if we have highmem enabled */
74 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
77 pte = pte_offset_map(pmd, addr);
78 printk(", *pte=%08lx", pte_val(*pte));
86 * Oops. The kernel tried to access some page that wasn't present.
88 static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
89 unsigned int fsr, struct pt_regs *regs)
92 * Are we prepared to handle this kernel fault?
94 if (fixup_exception(regs))
98 * No handler, we'll have to terminate things with extreme prejudice.
102 "Unable to handle kernel %s at virtual address %08lx\n",
103 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
104 "paging request", addr);
107 die("Oops", regs, fsr);
113 * Something tried to access memory that isn't in our memory map..
114 * User mode accesses just cause a SIGSEGV
116 static void __do_user_fault(struct task_struct *tsk, unsigned long addr,
117 unsigned int fsr, unsigned int sig, int code,
118 struct pt_regs *regs)
120 tsk->thread.address = addr;
121 tsk->thread.error_code = fsr;
122 tsk->thread.trap_no = 14;
123 force_sig_fault(sig, code, (void __user *)addr, tsk);
126 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
128 struct task_struct *tsk = current;
129 struct mm_struct *mm = tsk->active_mm;
132 * If we are in kernel mode at this point, we
133 * have no context to handle this fault with.
136 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
138 __do_kernel_fault(mm, addr, fsr, regs);
141 #define VM_FAULT_BADMAP 0x010000
142 #define VM_FAULT_BADACCESS 0x020000
145 * Check that the permissions on the VMA allow for the fault which occurred.
146 * If we encountered a write fault, we must have write permission, otherwise
147 * we allow any permission.
149 static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
151 unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
153 if (!(fsr ^ 0x12)) /* write? */
155 if (fsr & FSR_LNX_PF)
158 return vma->vm_flags & mask ? false : true;
161 static vm_fault_t __do_pf(struct mm_struct *mm, unsigned long addr,
162 unsigned int fsr, unsigned int flags, struct task_struct *tsk)
164 struct vm_area_struct *vma;
167 vma = find_vma(mm, addr);
168 fault = VM_FAULT_BADMAP;
171 if (unlikely(vma->vm_start > addr))
175 * Ok, we have a good vm_area for this
176 * memory access, so we can handle it.
179 if (access_error(fsr, vma)) {
180 fault = VM_FAULT_BADACCESS;
185 * If for any reason at all we couldn't handle the fault, make
186 * sure we exit gracefully rather than endlessly redo the fault.
188 fault = handle_mm_fault(vma, addr & PAGE_MASK, flags);
192 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
198 static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
200 struct task_struct *tsk;
201 struct mm_struct *mm;
204 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
210 * If we're in an interrupt or have no user
211 * context, we must not take the fault..
213 if (faulthandler_disabled() || !mm)
217 flags |= FAULT_FLAG_USER;
219 flags |= FAULT_FLAG_WRITE;
222 * As per x86, we may deadlock here. However, since the kernel only
223 * validly references user space from well defined areas of the code,
224 * we can bug out early if this is from code which shouldn't.
226 if (!down_read_trylock(&mm->mmap_sem)) {
228 && !search_exception_tables(regs->UCreg_pc))
231 down_read(&mm->mmap_sem);
234 * The above down_read_trylock() might have succeeded in
235 * which case, we'll have missed the might_sleep() from
239 #ifdef CONFIG_DEBUG_VM
240 if (!user_mode(regs) &&
241 !search_exception_tables(regs->UCreg_pc))
246 fault = __do_pf(mm, addr, fsr, flags, tsk);
248 /* If we need to retry but a fatal signal is pending, handle the
249 * signal first. We do not need to release the mmap_sem because
250 * it would already be released in __lock_page_or_retry in
252 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
255 if (!(fault & VM_FAULT_ERROR) && (flags & FAULT_FLAG_ALLOW_RETRY)) {
256 if (fault & VM_FAULT_MAJOR)
260 if (fault & VM_FAULT_RETRY) {
261 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
263 flags &= ~FAULT_FLAG_ALLOW_RETRY;
268 up_read(&mm->mmap_sem);
271 * Handle the "normal" case first - VM_FAULT_MAJOR
274 (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
278 * If we are in kernel mode at this point, we
279 * have no context to handle this fault with.
281 if (!user_mode(regs))
284 if (fault & VM_FAULT_OOM) {
286 * We ran out of memory, call the OOM killer, and return to
287 * userspace (which will retry the fault, or kill us if we
290 pagefault_out_of_memory();
294 if (fault & VM_FAULT_SIGBUS) {
296 * We had some memory, but were unable to
297 * successfully fix up this page fault.
303 * Something tried to access memory that
304 * isn't in our memory map..
307 code = fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR;
310 __do_user_fault(tsk, addr, fsr, sig, code, regs);
314 __do_kernel_fault(mm, addr, fsr, regs);
319 * First Level Translation Fault Handler
321 * We enter here because the first level page table doesn't contain
322 * a valid entry for the address.
324 * If the address is in kernel space (>= TASK_SIZE), then we are
325 * probably faulting in the vmalloc() area.
327 * If the init_task's first level page tables contains the relevant
328 * entry, we copy the it to this task. If not, we send the process
329 * a signal, fixup the exception, or oops the kernel.
331 * NOTE! We MUST NOT take any locks for this case. We may be in an
332 * interrupt or a critical region, and should only copy the information
333 * from the master page table, nothing more.
335 static int do_ifault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
341 if (addr < TASK_SIZE)
342 return do_pf(addr, fsr, regs);
347 index = pgd_index(addr);
349 pgd = cpu_get_pgd() + index;
350 pgd_k = init_mm.pgd + index;
352 if (pgd_none(*pgd_k))
355 pmd_k = pmd_offset((pud_t *) pgd_k, addr);
356 pmd = pmd_offset((pud_t *) pgd, addr);
358 if (pmd_none(*pmd_k))
361 set_pmd(pmd, *pmd_k);
362 flush_pmd_entry(pmd);
366 do_bad_area(addr, fsr, regs);
371 * This abort handler always returns "fault".
373 static int do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
378 static int do_good(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
380 unsigned int res1, res2;
382 printk("dabt exception but no error!\n");
384 __asm__ __volatile__(
387 : "=r"(res1), "=r"(res2)
391 printk(KERN_EMERG "r0 :%08x r1 :%08x\n", res1, res2);
396 static struct fsr_info {
397 int (*fn) (unsigned long addr, unsigned int fsr, struct pt_regs *regs);
403 * The following are the standard Unicore-I and UniCore-II aborts.
405 { do_good, SIGBUS, 0, "no error" },
406 { do_bad, SIGBUS, BUS_ADRALN, "alignment exception" },
407 { do_bad, SIGBUS, BUS_OBJERR, "external exception" },
408 { do_bad, SIGBUS, 0, "burst operation" },
409 { do_bad, SIGBUS, 0, "unknown 00100" },
410 { do_ifault, SIGSEGV, SEGV_MAPERR, "2nd level pt non-exist"},
411 { do_bad, SIGBUS, 0, "2nd lvl large pt non-exist" },
412 { do_bad, SIGBUS, 0, "invalid pte" },
413 { do_pf, SIGSEGV, SEGV_MAPERR, "page miss" },
414 { do_bad, SIGBUS, 0, "middle page miss" },
415 { do_bad, SIGBUS, 0, "large page miss" },
416 { do_pf, SIGSEGV, SEGV_MAPERR, "super page (section) miss" },
417 { do_bad, SIGBUS, 0, "unknown 01100" },
418 { do_bad, SIGBUS, 0, "unknown 01101" },
419 { do_bad, SIGBUS, 0, "unknown 01110" },
420 { do_bad, SIGBUS, 0, "unknown 01111" },
421 { do_bad, SIGBUS, 0, "addr: up 3G or IO" },
422 { do_pf, SIGSEGV, SEGV_ACCERR, "read unreadable addr" },
423 { do_pf, SIGSEGV, SEGV_ACCERR, "write unwriteable addr"},
424 { do_pf, SIGSEGV, SEGV_ACCERR, "exec unexecutable addr"},
425 { do_bad, SIGBUS, 0, "unknown 10100" },
426 { do_bad, SIGBUS, 0, "unknown 10101" },
427 { do_bad, SIGBUS, 0, "unknown 10110" },
428 { do_bad, SIGBUS, 0, "unknown 10111" },
429 { do_bad, SIGBUS, 0, "unknown 11000" },
430 { do_bad, SIGBUS, 0, "unknown 11001" },
431 { do_bad, SIGBUS, 0, "unknown 11010" },
432 { do_bad, SIGBUS, 0, "unknown 11011" },
433 { do_bad, SIGBUS, 0, "unknown 11100" },
434 { do_bad, SIGBUS, 0, "unknown 11101" },
435 { do_bad, SIGBUS, 0, "unknown 11110" },
436 { do_bad, SIGBUS, 0, "unknown 11111" }
439 void __init hook_fault_code(int nr,
440 int (*fn) (unsigned long, unsigned int, struct pt_regs *),
441 int sig, int code, const char *name)
443 if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
446 fsr_info[nr].fn = fn;
447 fsr_info[nr].sig = sig;
448 fsr_info[nr].code = code;
449 fsr_info[nr].name = name;
453 * Dispatch a data abort to the relevant handler.
455 asmlinkage void do_DataAbort(unsigned long addr, unsigned int fsr,
456 struct pt_regs *regs)
458 const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
460 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
463 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
464 inf->name, fsr, addr);
466 uc32_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
470 asmlinkage void do_PrefetchAbort(unsigned long addr,
471 unsigned int ifsr, struct pt_regs *regs)
473 const struct fsr_info *inf = fsr_info + fsr_fs(ifsr);
475 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
478 printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
479 inf->name, ifsr, addr);
481 uc32_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,