xref: /openbmc/linux/arch/hexagon/mm/vm_fault.c (revision 82df5b73)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Memory fault handling for Hexagon
4  *
5  * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
6  */
7 
8 /*
9  * Page fault handling for the Hexagon Virtual Machine.
10  * Can also be called by a native port emulating the HVM
11  * execptions.
12  */
13 
14 #include <asm/traps.h>
15 #include <linux/uaccess.h>
16 #include <linux/mm.h>
17 #include <linux/sched/signal.h>
18 #include <linux/signal.h>
19 #include <linux/extable.h>
20 #include <linux/hardirq.h>
21 
22 /*
23  * Decode of hardware exception sends us to one of several
24  * entry points.  At each, we generate canonical arguments
25  * for handling by the abstract memory management code.
26  */
27 #define FLT_IFETCH     -1
28 #define FLT_LOAD        0
29 #define FLT_STORE       1
30 
31 
32 /*
33  * Canonical page fault handler
34  */
35 void do_page_fault(unsigned long address, long cause, struct pt_regs *regs)
36 {
37 	struct vm_area_struct *vma;
38 	struct mm_struct *mm = current->mm;
39 	int si_signo;
40 	int si_code = SEGV_MAPERR;
41 	vm_fault_t fault;
42 	const struct exception_table_entry *fixup;
43 	unsigned int flags = FAULT_FLAG_DEFAULT;
44 
45 	/*
46 	 * If we're in an interrupt or have no user context,
47 	 * then must not take the fault.
48 	 */
49 	if (unlikely(in_interrupt() || !mm))
50 		goto no_context;
51 
52 	local_irq_enable();
53 
54 	if (user_mode(regs))
55 		flags |= FAULT_FLAG_USER;
56 retry:
57 	mmap_read_lock(mm);
58 	vma = find_vma(mm, address);
59 	if (!vma)
60 		goto bad_area;
61 
62 	if (vma->vm_start <= address)
63 		goto good_area;
64 
65 	if (!(vma->vm_flags & VM_GROWSDOWN))
66 		goto bad_area;
67 
68 	if (expand_stack(vma, address))
69 		goto bad_area;
70 
71 good_area:
72 	/* Address space is OK.  Now check access rights. */
73 	si_code = SEGV_ACCERR;
74 
75 	switch (cause) {
76 	case FLT_IFETCH:
77 		if (!(vma->vm_flags & VM_EXEC))
78 			goto bad_area;
79 		break;
80 	case FLT_LOAD:
81 		if (!(vma->vm_flags & VM_READ))
82 			goto bad_area;
83 		break;
84 	case FLT_STORE:
85 		if (!(vma->vm_flags & VM_WRITE))
86 			goto bad_area;
87 		flags |= FAULT_FLAG_WRITE;
88 		break;
89 	}
90 
91 	fault = handle_mm_fault(vma, address, flags);
92 
93 	if (fault_signal_pending(fault, regs))
94 		return;
95 
96 	/* The most common case -- we are done. */
97 	if (likely(!(fault & VM_FAULT_ERROR))) {
98 		if (flags & FAULT_FLAG_ALLOW_RETRY) {
99 			if (fault & VM_FAULT_MAJOR)
100 				current->maj_flt++;
101 			else
102 				current->min_flt++;
103 			if (fault & VM_FAULT_RETRY) {
104 				flags |= FAULT_FLAG_TRIED;
105 				goto retry;
106 			}
107 		}
108 
109 		mmap_read_unlock(mm);
110 		return;
111 	}
112 
113 	mmap_read_unlock(mm);
114 
115 	/* Handle copyin/out exception cases */
116 	if (!user_mode(regs))
117 		goto no_context;
118 
119 	if (fault & VM_FAULT_OOM) {
120 		pagefault_out_of_memory();
121 		return;
122 	}
123 
124 	/* User-mode address is in the memory map, but we are
125 	 * unable to fix up the page fault.
126 	 */
127 	if (fault & VM_FAULT_SIGBUS) {
128 		si_signo = SIGBUS;
129 		si_code = BUS_ADRERR;
130 	}
131 	/* Address is not in the memory map */
132 	else {
133 		si_signo = SIGSEGV;
134 		si_code  = SEGV_ACCERR;
135 	}
136 	force_sig_fault(si_signo, si_code, (void __user *)address);
137 	return;
138 
139 bad_area:
140 	mmap_read_unlock(mm);
141 
142 	if (user_mode(regs)) {
143 		force_sig_fault(SIGSEGV, si_code, (void __user *)address);
144 		return;
145 	}
146 	/* Kernel-mode fault falls through */
147 
148 no_context:
149 	fixup = search_exception_tables(pt_elr(regs));
150 	if (fixup) {
151 		pt_set_elr(regs, fixup->fixup);
152 		return;
153 	}
154 
155 	/* Things are looking very, very bad now */
156 	bust_spinlocks(1);
157 	printk(KERN_EMERG "Unable to handle kernel paging request at "
158 		"virtual address 0x%08lx, regs %p\n", address, regs);
159 	die("Bad Kernel VA", regs, SIGKILL);
160 }
161 
162 
163 void read_protection_fault(struct pt_regs *regs)
164 {
165 	unsigned long badvadr = pt_badva(regs);
166 
167 	do_page_fault(badvadr, FLT_LOAD, regs);
168 }
169 
170 void write_protection_fault(struct pt_regs *regs)
171 {
172 	unsigned long badvadr = pt_badva(regs);
173 
174 	do_page_fault(badvadr, FLT_STORE, regs);
175 }
176 
177 void execute_protection_fault(struct pt_regs *regs)
178 {
179 	unsigned long badvadr = pt_badva(regs);
180 
181 	do_page_fault(badvadr, FLT_IFETCH, regs);
182 }
183