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