1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/extable.h>
21#include <linux/signal.h>
22#include <linux/sched.h>
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/string.h>
26#include <linux/types.h>
27#include <linux/ptrace.h>
28#include <linux/mman.h>
29#include <linux/mm.h>
30#include <linux/interrupt.h>
31
32#include <asm/page.h>
33#include <asm/pgtable.h>
34#include <asm/mmu.h>
35#include <linux/mmu_context.h>
36#include <linux/uaccess.h>
37#include <asm/exceptions.h>
38
39static unsigned long pte_misses;
40static unsigned long pte_errors;
41
42
43
44
45
46static int store_updates_sp(struct pt_regs *regs)
47{
48 unsigned int inst;
49
50 if (get_user(inst, (unsigned int __user *)regs->pc))
51 return 0;
52
53 if (((inst >> 21) & 0x1f) != 1)
54 return 0;
55
56 if ((inst & 0xd0000000) == 0xd0000000)
57 return 1;
58 return 0;
59}
60
61
62
63
64
65
66
67void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
68{
69 const struct exception_table_entry *fixup;
70
71
72 fixup = search_exception_tables(regs->pc);
73 if (fixup) {
74 regs->pc = fixup->fixup;
75 return;
76 }
77
78
79 die("kernel access of bad area", regs, sig);
80}
81
82
83
84
85
86void do_page_fault(struct pt_regs *regs, unsigned long address,
87 unsigned long error_code)
88{
89 struct vm_area_struct *vma;
90 struct mm_struct *mm = current->mm;
91 int code = SEGV_MAPERR;
92 int is_write = error_code & ESR_S;
93 vm_fault_t fault;
94 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
95
96 regs->ear = address;
97 regs->esr = error_code;
98
99
100 if (unlikely(kernel_mode(regs) && (address >= TASK_SIZE))) {
101 pr_warn("kernel task_size exceed");
102 _exception(SIGSEGV, regs, code, address);
103 }
104
105
106 if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)
107 is_write = 0;
108
109 if (unlikely(faulthandler_disabled() || !mm)) {
110 if (kernel_mode(regs))
111 goto bad_area_nosemaphore;
112
113
114
115 pr_emerg("Page fault in user mode with faulthandler_disabled(), mm = %p\n",
116 mm);
117 pr_emerg("r15 = %lx MSR = %lx\n",
118 regs->r15, regs->msr);
119 die("Weird page fault", regs, SIGSEGV);
120 }
121
122 if (user_mode(regs))
123 flags |= FAULT_FLAG_USER;
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
141 if (kernel_mode(regs) && !search_exception_tables(regs->pc))
142 goto bad_area_nosemaphore;
143
144retry:
145 down_read(&mm->mmap_sem);
146 }
147
148 vma = find_vma(mm, address);
149 if (unlikely(!vma))
150 goto bad_area;
151
152 if (vma->vm_start <= address)
153 goto good_area;
154
155 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
156 goto bad_area;
157
158 if (unlikely(!is_write))
159 goto bad_area;
160
161
162
163
164
165
166
167
168
169
170 if (unlikely(address + 0x100000 < vma->vm_end)) {
171
172
173 struct pt_regs *uregs = current->thread.regs;
174 if (uregs == NULL)
175 goto bad_area;
176
177
178
179
180
181
182
183
184
185
186
187
188
189 if (address + 2048 < uregs->r1
190 && (kernel_mode(regs) || !store_updates_sp(regs)))
191 goto bad_area;
192 }
193 if (expand_stack(vma, address))
194 goto bad_area;
195
196good_area:
197 code = SEGV_ACCERR;
198
199
200 if (unlikely(is_write)) {
201 if (unlikely(!(vma->vm_flags & VM_WRITE)))
202 goto bad_area;
203 flags |= FAULT_FLAG_WRITE;
204
205 } else {
206
207 if (unlikely(error_code & 0x08000000))
208 goto bad_area;
209 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC))))
210 goto bad_area;
211 }
212
213
214
215
216
217
218 fault = handle_mm_fault(vma, address, flags);
219
220 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
221 return;
222
223 if (unlikely(fault & VM_FAULT_ERROR)) {
224 if (fault & VM_FAULT_OOM)
225 goto out_of_memory;
226 else if (fault & VM_FAULT_SIGSEGV)
227 goto bad_area;
228 else if (fault & VM_FAULT_SIGBUS)
229 goto do_sigbus;
230 BUG();
231 }
232
233 if (flags & FAULT_FLAG_ALLOW_RETRY) {
234 if (unlikely(fault & VM_FAULT_MAJOR))
235 current->maj_flt++;
236 else
237 current->min_flt++;
238 if (fault & VM_FAULT_RETRY) {
239 flags &= ~FAULT_FLAG_ALLOW_RETRY;
240 flags |= FAULT_FLAG_TRIED;
241
242
243
244
245
246
247
248 goto retry;
249 }
250 }
251
252 up_read(&mm->mmap_sem);
253
254
255
256
257
258
259 pte_misses++;
260 return;
261
262bad_area:
263 up_read(&mm->mmap_sem);
264
265bad_area_nosemaphore:
266 pte_errors++;
267
268
269 if (user_mode(regs)) {
270 _exception(SIGSEGV, regs, code, address);
271 return;
272 }
273
274 bad_page_fault(regs, address, SIGSEGV);
275 return;
276
277
278
279
280
281out_of_memory:
282 up_read(&mm->mmap_sem);
283 if (!user_mode(regs))
284 bad_page_fault(regs, address, SIGKILL);
285 else
286 pagefault_out_of_memory();
287 return;
288
289do_sigbus:
290 up_read(&mm->mmap_sem);
291 if (user_mode(regs)) {
292 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address, current);
293 return;
294 }
295 bad_page_fault(regs, address, SIGBUS);
296}
297