1 // SPDX-License-Identifier: GPL-2.0-only
3 * Memory fault handling for Hexagon
5 * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
9 * Page fault handling for the Hexagon Virtual Machine.
10 * Can also be called by a native port emulating the HVM
14 #include <asm/traps.h>
15 #include <asm/vm_fault.h>
16 #include <linux/uaccess.h>
18 #include <linux/sched/signal.h>
19 #include <linux/signal.h>
20 #include <linux/extable.h>
21 #include <linux/hardirq.h>
22 #include <linux/perf_event.h>
25 * Decode of hardware exception sends us to one of several
26 * entry points. At each, we generate canonical arguments
27 * for handling by the abstract memory management code.
35 * Canonical page fault handler
37 static void do_page_fault(unsigned long address, long cause, struct pt_regs *regs)
39 struct vm_area_struct *vma;
40 struct mm_struct *mm = current->mm;
42 int si_code = SEGV_MAPERR;
44 const struct exception_table_entry *fixup;
45 unsigned int flags = FAULT_FLAG_DEFAULT;
48 * If we're in an interrupt or have no user context,
49 * then must not take the fault.
51 if (unlikely(in_interrupt() || !mm))
57 flags |= FAULT_FLAG_USER;
59 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
61 vma = lock_mm_and_find_vma(mm, address, regs);
63 goto bad_area_nosemaphore;
65 /* Address space is OK. Now check access rights. */
66 si_code = SEGV_ACCERR;
70 if (!(vma->vm_flags & VM_EXEC))
74 if (!(vma->vm_flags & VM_READ))
78 if (!(vma->vm_flags & VM_WRITE))
80 flags |= FAULT_FLAG_WRITE;
84 fault = handle_mm_fault(vma, address, flags, regs);
86 if (fault_signal_pending(fault, regs)) {
92 /* The fault is fully completed (including releasing mmap lock) */
93 if (fault & VM_FAULT_COMPLETED)
96 /* The most common case -- we are done. */
97 if (likely(!(fault & VM_FAULT_ERROR))) {
98 if (fault & VM_FAULT_RETRY) {
99 flags |= FAULT_FLAG_TRIED;
103 mmap_read_unlock(mm);
107 mmap_read_unlock(mm);
109 /* Handle copyin/out exception cases */
110 if (!user_mode(regs))
113 if (fault & VM_FAULT_OOM) {
114 pagefault_out_of_memory();
118 /* User-mode address is in the memory map, but we are
119 * unable to fix up the page fault.
121 if (fault & VM_FAULT_SIGBUS) {
123 si_code = BUS_ADRERR;
125 /* Address is not in the memory map */
128 si_code = SEGV_ACCERR;
130 force_sig_fault(si_signo, si_code, (void __user *)address);
134 mmap_read_unlock(mm);
136 bad_area_nosemaphore:
137 if (user_mode(regs)) {
138 force_sig_fault(SIGSEGV, si_code, (void __user *)address);
141 /* Kernel-mode fault falls through */
144 fixup = search_exception_tables(pt_elr(regs));
146 pt_set_elr(regs, fixup->fixup);
150 /* Things are looking very, very bad now */
152 printk(KERN_EMERG "Unable to handle kernel paging request at "
153 "virtual address 0x%08lx, regs %p\n", address, regs);
154 die("Bad Kernel VA", regs, SIGKILL);
158 void read_protection_fault(struct pt_regs *regs)
160 unsigned long badvadr = pt_badva(regs);
162 do_page_fault(badvadr, FLT_LOAD, regs);
165 void write_protection_fault(struct pt_regs *regs)
167 unsigned long badvadr = pt_badva(regs);
169 do_page_fault(badvadr, FLT_STORE, regs);
172 void execute_protection_fault(struct pt_regs *regs)
174 unsigned long badvadr = pt_badva(regs);
176 do_page_fault(badvadr, FLT_IFETCH, regs);