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/module.h>
21#include <linux/sched.h>
22
23#include <asm/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;
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 siginfo_t info;
56 int 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 } else {
90
91
92
93
94 if (regs->sr && (SPR_SR_IEE | SPR_SR_TEE))
95 local_irq_enable();
96 }
97
98 mm = tsk->mm;
99 info.si_code = SEGV_MAPERR;
100
101
102
103
104
105
106 if (in_interrupt() || !mm)
107 goto no_context;
108
109retry:
110 down_read(&mm->mmap_sem);
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 info.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(mm, vma, address, flags);
166
167 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
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_SIGBUS)
174 goto do_sigbus;
175 BUG();
176 }
177
178 if (flags & FAULT_FLAG_ALLOW_RETRY) {
179
180 if (fault & VM_FAULT_MAJOR)
181 tsk->maj_flt++;
182 else
183 tsk->min_flt++;
184 if (fault & VM_FAULT_RETRY) {
185 flags &= ~FAULT_FLAG_ALLOW_RETRY;
186 flags |= FAULT_FLAG_TRIED;
187
188
189
190
191
192
193 goto retry;
194 }
195 }
196
197 up_read(&mm->mmap_sem);
198 return;
199
200
201
202
203
204
205bad_area:
206 up_read(&mm->mmap_sem);
207
208bad_area_nosemaphore:
209
210
211
212 if (user_mode(regs)) {
213 info.si_signo = SIGSEGV;
214 info.si_errno = 0;
215
216 info.si_addr = (void *)address;
217 force_sig_info(SIGSEGV, &info, tsk);
218 return;
219 }
220
221no_context:
222
223
224
225
226
227
228
229
230
231
232 {
233 const struct exception_table_entry *entry;
234
235 __asm__ __volatile__("l.nop 42");
236
237 if ((entry = search_exception_tables(regs->pc)) != NULL) {
238
239 regs->pc = entry->fixup;
240 return;
241 }
242 }
243
244
245
246
247
248
249 if ((unsigned long)(address) < PAGE_SIZE)
250 printk(KERN_ALERT
251 "Unable to handle kernel NULL pointer dereference");
252 else
253 printk(KERN_ALERT "Unable to handle kernel access");
254 printk(" at virtual address 0x%08lx\n", address);
255
256 die("Oops", regs, write_acc);
257
258 do_exit(SIGKILL);
259
260
261
262
263
264
265out_of_memory:
266 __asm__ __volatile__("l.nop 42");
267 __asm__ __volatile__("l.nop 1");
268
269 up_read(&mm->mmap_sem);
270 if (!user_mode(regs))
271 goto no_context;
272 pagefault_out_of_memory();
273 return;
274
275do_sigbus:
276 up_read(&mm->mmap_sem);
277
278
279
280
281
282 info.si_signo = SIGBUS;
283 info.si_errno = 0;
284 info.si_code = BUS_ADRERR;
285 info.si_addr = (void *)address;
286 force_sig_info(SIGBUS, &info, tsk);
287
288
289 if (!user_mode(regs))
290 goto no_context;
291 return;
292
293vmalloc_fault:
294 {
295
296
297
298
299
300
301
302
303
304
305
306 int offset = pgd_index(address);
307 pgd_t *pgd, *pgd_k;
308 pud_t *pud, *pud_k;
309 pmd_t *pmd, *pmd_k;
310 pte_t *pte_k;
311
312
313
314
315
316
317
318
319 pgd = (pgd_t *)current_pgd + offset;
320 pgd_k = init_mm.pgd + offset;
321
322
323
324
325
326
327
328
329
330
331
332
333
334 pud = pud_offset(pgd, address);
335 pud_k = pud_offset(pgd_k, address);
336 if (!pud_present(*pud_k))
337 goto no_context;
338
339 pmd = pmd_offset(pud, address);
340 pmd_k = pmd_offset(pud_k, address);
341
342 if (!pmd_present(*pmd_k))
343 goto bad_area_nosemaphore;
344
345 set_pmd(pmd, *pmd_k);
346
347
348
349
350
351
352
353 pte_k = pte_offset_kernel(pmd_k, address);
354 if (!pte_present(*pte_k))
355 goto no_context;
356
357 return;
358 }
359}
360