1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/mm.h>
15#include <linux/interrupt.h>
16#include <linux/extable.h>
17#include <linux/sched/signal.h>
18#include <linux/perf_event.h>
19
20#include <linux/uaccess.h>
21#include <asm/siginfo.h>
22#include <asm/signal.h>
23
24#define NUM_TLB_ENTRIES 64
25#define TLB_OFFSET(add) (((add) >> PAGE_SHIFT) & (NUM_TLB_ENTRIES-1))
26
27unsigned long pte_misses;
28unsigned long pte_errors;
29
30
31
32
33volatile pgd_t *current_pgd[NR_CPUS];
34
35extern void die(char *, struct pt_regs *, long);
36
37
38
39
40
41
42
43
44
45
46asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,
47 unsigned long vector, int write_acc)
48{
49 struct task_struct *tsk;
50 struct mm_struct *mm;
51 struct vm_area_struct *vma;
52 int si_code;
53 vm_fault_t fault;
54 unsigned int flags = FAULT_FLAG_DEFAULT;
55
56 tsk = current;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 if (address >= VMALLOC_START &&
78 (vector != 0x300 && vector != 0x400) &&
79 !user_mode(regs))
80 goto vmalloc_fault;
81
82
83 if (user_mode(regs)) {
84
85 local_irq_enable();
86 flags |= FAULT_FLAG_USER;
87 } else {
88
89
90
91
92 if (regs->sr && (SPR_SR_IEE | SPR_SR_TEE))
93 local_irq_enable();
94 }
95
96 mm = tsk->mm;
97 si_code = SEGV_MAPERR;
98
99
100
101
102
103
104 if (in_interrupt() || !mm)
105 goto no_context;
106
107 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
108
109retry:
110 mmap_read_lock(mm);
111 vma = find_vma(mm, address);
112
113 if (!vma)
114 goto bad_area;
115
116 if (vma->vm_start <= address)
117 goto good_area;
118
119 if (!(vma->vm_flags & VM_GROWSDOWN))
120 goto bad_area;
121
122 if (user_mode(regs)) {
123
124
125
126
127
128
129 if (address + PAGE_SIZE < regs->sp)
130 goto bad_area;
131 }
132 if (expand_stack(vma, address))
133 goto bad_area;
134
135
136
137
138
139
140good_area:
141 si_code = SEGV_ACCERR;
142
143
144
145 if (write_acc) {
146 if (!(vma->vm_flags & VM_WRITE))
147 goto bad_area;
148 flags |= FAULT_FLAG_WRITE;
149 } else {
150
151 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
152 goto bad_area;
153 }
154
155
156 if ((vector == 0x400) && !(vma->vm_page_prot.pgprot & _PAGE_EXEC))
157 goto bad_area;
158
159
160
161
162
163
164
165 fault = handle_mm_fault(vma, address, flags, regs);
166
167 if (fault_signal_pending(fault, regs))
168 return;
169
170 if (unlikely(fault & VM_FAULT_ERROR)) {
171 if (fault & VM_FAULT_OOM)
172 goto out_of_memory;
173 else if (fault & VM_FAULT_SIGSEGV)
174 goto bad_area;
175 else if (fault & VM_FAULT_SIGBUS)
176 goto do_sigbus;
177 BUG();
178 }
179
180 if (flags & FAULT_FLAG_ALLOW_RETRY) {
181
182 if (fault & VM_FAULT_RETRY) {
183 flags |= FAULT_FLAG_TRIED;
184
185
186
187
188
189
190 goto retry;
191 }
192 }
193
194 mmap_read_unlock(mm);
195 return;
196
197
198
199
200
201
202bad_area:
203 mmap_read_unlock(mm);
204
205bad_area_nosemaphore:
206
207
208
209 if (user_mode(regs)) {
210 force_sig_fault(SIGSEGV, si_code, (void __user *)address);
211 return;
212 }
213
214no_context:
215
216
217
218
219
220
221
222
223
224
225 {
226 const struct exception_table_entry *entry;
227
228 __asm__ __volatile__("l.nop 42");
229
230 if ((entry = search_exception_tables(regs->pc)) != NULL) {
231
232 regs->pc = entry->fixup;
233 return;
234 }
235 }
236
237
238
239
240
241
242 if ((unsigned long)(address) < PAGE_SIZE)
243 printk(KERN_ALERT
244 "Unable to handle kernel NULL pointer dereference");
245 else
246 printk(KERN_ALERT "Unable to handle kernel access");
247 printk(" at virtual address 0x%08lx\n", address);
248
249 die("Oops", regs, write_acc);
250
251 do_exit(SIGKILL);
252
253
254
255
256
257
258out_of_memory:
259 __asm__ __volatile__("l.nop 42");
260 __asm__ __volatile__("l.nop 1");
261
262 mmap_read_unlock(mm);
263 if (!user_mode(regs))
264 goto no_context;
265 pagefault_out_of_memory();
266 return;
267
268do_sigbus:
269 mmap_read_unlock(mm);
270
271
272
273
274
275 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
276
277
278 if (!user_mode(regs))
279 goto no_context;
280 return;
281
282vmalloc_fault:
283 {
284
285
286
287
288
289
290
291
292
293
294
295 int offset = pgd_index(address);
296 pgd_t *pgd, *pgd_k;
297 p4d_t *p4d, *p4d_k;
298 pud_t *pud, *pud_k;
299 pmd_t *pmd, *pmd_k;
300 pte_t *pte_k;
301
302
303
304
305
306
307
308
309 pgd = (pgd_t *)current_pgd[smp_processor_id()] + offset;
310 pgd_k = init_mm.pgd + offset;
311
312
313
314
315
316
317
318
319
320
321
322
323
324 p4d = p4d_offset(pgd, address);
325 p4d_k = p4d_offset(pgd_k, address);
326 if (!p4d_present(*p4d_k))
327 goto no_context;
328
329 pud = pud_offset(p4d, address);
330 pud_k = pud_offset(p4d_k, address);
331 if (!pud_present(*pud_k))
332 goto no_context;
333
334 pmd = pmd_offset(pud, address);
335 pmd_k = pmd_offset(pud_k, address);
336
337 if (!pmd_present(*pmd_k))
338 goto bad_area_nosemaphore;
339
340 set_pmd(pmd, *pmd_k);
341
342
343
344
345
346
347
348 pte_k = pte_offset_kernel(pmd_k, address);
349 if (!pte_present(*pte_k))
350 goto no_context;
351
352 return;
353 }
354}
355