1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/module.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 siginfo_t info;
92 int code = SEGV_MAPERR;
93 int is_write = error_code & ESR_S;
94 int fault;
95 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
96 (is_write ? FAULT_FLAG_WRITE : 0);
97
98 regs->ear = address;
99 regs->esr = error_code;
100
101
102 if (unlikely(kernel_mode(regs) && (address >= TASK_SIZE))) {
103 pr_warn("kernel task_size exceed");
104 _exception(SIGSEGV, regs, code, address);
105 }
106
107
108 if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)
109 is_write = 0;
110
111 if (unlikely(in_atomic() || !mm)) {
112 if (kernel_mode(regs))
113 goto bad_area_nosemaphore;
114
115
116
117 pr_emerg("Page fault in user mode with in_atomic(), mm = %p\n",
118 mm);
119 pr_emerg("r15 = %lx MSR = %lx\n",
120 regs->r15, regs->msr);
121 die("Weird page fault", regs, SIGSEGV);
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
140 if (kernel_mode(regs) && !search_exception_tables(regs->pc))
141 goto bad_area_nosemaphore;
142
143retry:
144 down_read(&mm->mmap_sem);
145 }
146
147 vma = find_vma(mm, address);
148 if (unlikely(!vma))
149 goto bad_area;
150
151 if (vma->vm_start <= address)
152 goto good_area;
153
154 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
155 goto bad_area;
156
157 if (unlikely(!is_write))
158 goto bad_area;
159
160
161
162
163
164
165
166
167
168
169 if (unlikely(address + 0x100000 < vma->vm_end)) {
170
171
172 struct pt_regs *uregs = current->thread.regs;
173 if (uregs == NULL)
174 goto bad_area;
175
176
177
178
179
180
181
182
183
184
185
186
187
188 if (address + 2048 < uregs->r1
189 && (kernel_mode(regs) || !store_updates_sp(regs)))
190 goto bad_area;
191 }
192 if (expand_stack(vma, address))
193 goto bad_area;
194
195good_area:
196 code = SEGV_ACCERR;
197
198
199 if (unlikely(is_write)) {
200 if (unlikely(!(vma->vm_flags & VM_WRITE)))
201 goto bad_area;
202
203 } else {
204
205 if (unlikely(error_code & 0x08000000))
206 goto bad_area;
207 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC))))
208 goto bad_area;
209 }
210
211
212
213
214
215
216 fault = handle_mm_fault(mm, vma, address, flags);
217
218 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
219 return;
220
221 if (unlikely(fault & VM_FAULT_ERROR)) {
222 if (fault & VM_FAULT_OOM)
223 goto out_of_memory;
224 else if (fault & VM_FAULT_SIGBUS)
225 goto do_sigbus;
226 BUG();
227 }
228
229 if (flags & FAULT_FLAG_ALLOW_RETRY) {
230 if (unlikely(fault & VM_FAULT_MAJOR))
231 current->maj_flt++;
232 else
233 current->min_flt++;
234 if (fault & VM_FAULT_RETRY) {
235 flags &= ~FAULT_FLAG_ALLOW_RETRY;
236 flags |= FAULT_FLAG_TRIED;
237
238
239
240
241
242
243
244 goto retry;
245 }
246 }
247
248 up_read(&mm->mmap_sem);
249
250
251
252
253
254
255 pte_misses++;
256 return;
257
258bad_area:
259 up_read(&mm->mmap_sem);
260
261bad_area_nosemaphore:
262 pte_errors++;
263
264
265 if (user_mode(regs)) {
266 _exception(SIGSEGV, regs, code, address);
267
268
269
270
271
272 return;
273 }
274
275 bad_page_fault(regs, address, SIGSEGV);
276 return;
277
278
279
280
281
282out_of_memory:
283 up_read(&mm->mmap_sem);
284 if (!user_mode(regs))
285 bad_page_fault(regs, address, SIGKILL);
286 else
287 pagefault_out_of_memory();
288 return;
289
290do_sigbus:
291 up_read(&mm->mmap_sem);
292 if (user_mode(regs)) {
293 info.si_signo = SIGBUS;
294 info.si_errno = 0;
295 info.si_code = BUS_ADRERR;
296 info.si_addr = (void __user *)address;
297 force_sig_info(SIGBUS, &info, current);
298 return;
299 }
300 bad_page_fault(regs, address, SIGBUS);
301}
302