1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/signal.h>
14#include <linux/sched.h>
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/string.h>
18#include <linux/types.h>
19#include <linux/ptrace.h>
20#include <linux/mman.h>
21#include <linux/mm.h>
22#include <linux/smp.h>
23#include <linux/interrupt.h>
24#include <linux/init.h>
25#include <linux/vt_kern.h>
26#include <linux/uaccess.h>
27
28#include <asm/pgalloc.h>
29#include <asm/hardirq.h>
30#include <asm/cpu-regs.h>
31#include <asm/debugger.h>
32#include <asm/gdb-stub.h>
33
34
35
36
37
38void bust_spinlocks(int yes)
39{
40 if (yes) {
41 oops_in_progress = 1;
42 } else {
43 int loglevel_save = console_loglevel;
44#ifdef CONFIG_VT
45 unblank_screen();
46#endif
47 oops_in_progress = 0;
48
49
50
51
52
53 console_loglevel = 15;
54
55 printk(" ");
56 console_loglevel = loglevel_save;
57 }
58}
59
60void do_BUG(const char *file, int line)
61{
62 bust_spinlocks(1);
63 printk(KERN_EMERG "------------[ cut here ]------------\n");
64 printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line);
65}
66
67#if 0
68static void print_pagetable_entries(pgd_t *pgdir, unsigned long address)
69{
70 pgd_t *pgd;
71 pmd_t *pmd;
72 pte_t *pte;
73
74 pgd = pgdir + __pgd_offset(address);
75 printk(KERN_DEBUG "pgd entry %p: %016Lx\n",
76 pgd, (long long) pgd_val(*pgd));
77
78 if (!pgd_present(*pgd)) {
79 printk(KERN_DEBUG "... pgd not present!\n");
80 return;
81 }
82 pmd = pmd_offset(pgd, address);
83 printk(KERN_DEBUG "pmd entry %p: %016Lx\n",
84 pmd, (long long)pmd_val(*pmd));
85
86 if (!pmd_present(*pmd)) {
87 printk(KERN_DEBUG "... pmd not present!\n");
88 return;
89 }
90 pte = pte_offset(pmd, address);
91 printk(KERN_DEBUG "pte entry %p: %016Lx\n",
92 pte, (long long) pte_val(*pte));
93
94 if (!pte_present(*pte))
95 printk(KERN_DEBUG "... pte not present!\n");
96}
97#endif
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long fault_code,
119 unsigned long address)
120{
121 struct vm_area_struct *vma;
122 struct task_struct *tsk;
123 struct mm_struct *mm;
124 unsigned long page;
125 siginfo_t info;
126 int fault;
127 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
128
129#ifdef CONFIG_GDBSTUB
130
131 if (gdbstub_busy) {
132 gdbstub_exception(regs, TBR & TBR_INT_CODE);
133 return;
134 }
135#endif
136
137#if 0
138 printk(KERN_DEBUG "--- do_page_fault(%p,%s:%04lx,%08lx)\n",
139 regs,
140 fault_code & 0x10000 ? "ins" : "data",
141 fault_code & 0xffff, address);
142#endif
143
144 tsk = current;
145
146
147
148
149
150
151
152
153
154
155
156
157
158 if (address >= VMALLOC_START && address < VMALLOC_END &&
159 (fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR &&
160 (fault_code & MMUFCR_xFC_PGINVAL) == MMUFCR_xFC_PGINVAL
161 )
162 goto vmalloc_fault;
163
164 mm = tsk->mm;
165 info.si_code = SEGV_MAPERR;
166
167
168
169
170
171 if (faulthandler_disabled() || !mm)
172 goto no_context;
173
174 if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR)
175 flags |= FAULT_FLAG_USER;
176retry:
177 down_read(&mm->mmap_sem);
178
179 vma = find_vma(mm, address);
180 if (!vma)
181 goto bad_area;
182 if (vma->vm_start <= address)
183 goto good_area;
184 if (!(vma->vm_flags & VM_GROWSDOWN))
185 goto bad_area;
186
187 if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
188
189
190 if ((address & PAGE_MASK) + 2 * PAGE_SIZE < regs->sp) {
191#if 0
192 printk(KERN_WARNING
193 "[%d] ### Access below stack @%lx (sp=%lx)\n",
194 current->pid, address, regs->sp);
195 printk(KERN_WARNING
196 "vma [%08x - %08x]\n",
197 vma->vm_start, vma->vm_end);
198 show_registers(regs);
199 printk(KERN_WARNING
200 "[%d] ### Code: [%08lx]"
201 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
202 current->pid,
203 regs->pc,
204 ((u8 *) regs->pc)[0],
205 ((u8 *) regs->pc)[1],
206 ((u8 *) regs->pc)[2],
207 ((u8 *) regs->pc)[3],
208 ((u8 *) regs->pc)[4],
209 ((u8 *) regs->pc)[5],
210 ((u8 *) regs->pc)[6],
211 ((u8 *) regs->pc)[7]
212 );
213#endif
214 goto bad_area;
215 }
216 }
217
218 if (expand_stack(vma, address))
219 goto bad_area;
220
221
222
223
224
225good_area:
226 info.si_code = SEGV_ACCERR;
227 switch (fault_code & (MMUFCR_xFC_PGINVAL|MMUFCR_xFC_TYPE)) {
228 default:
229 case MMUFCR_xFC_TYPE_WRITE:
230#ifdef TEST_VERIFY_AREA
231 if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR)
232 printk(KERN_DEBUG "WP fault at %08lx\n", regs->pc);
233#endif
234
235 case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_WRITE:
236 if (!(vma->vm_flags & VM_WRITE))
237 goto bad_area;
238 flags |= FAULT_FLAG_WRITE;
239 break;
240
241
242 case MMUFCR_xFC_TYPE_READ:
243 goto bad_area;
244
245
246 case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_READ:
247 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
248 goto bad_area;
249 break;
250 }
251
252
253
254
255
256
257 fault = handle_mm_fault(mm, vma, address, flags);
258
259 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
260 return;
261
262 if (unlikely(fault & VM_FAULT_ERROR)) {
263 if (fault & VM_FAULT_OOM)
264 goto out_of_memory;
265 else if (fault & VM_FAULT_SIGSEGV)
266 goto bad_area;
267 else if (fault & VM_FAULT_SIGBUS)
268 goto do_sigbus;
269 BUG();
270 }
271 if (flags & FAULT_FLAG_ALLOW_RETRY) {
272 if (fault & VM_FAULT_MAJOR)
273 current->maj_flt++;
274 else
275 current->min_flt++;
276 if (fault & VM_FAULT_RETRY) {
277 flags &= ~FAULT_FLAG_ALLOW_RETRY;
278
279
280
281
282
283
284 goto retry;
285 }
286 }
287
288 up_read(&mm->mmap_sem);
289 return;
290
291
292
293
294
295bad_area:
296 up_read(&mm->mmap_sem);
297
298
299 if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
300 info.si_signo = SIGSEGV;
301 info.si_errno = 0;
302
303 info.si_addr = (void *)address;
304 force_sig_info(SIGSEGV, &info, tsk);
305 return;
306 }
307
308no_context:
309
310 if (fixup_exception(regs))
311 return;
312
313
314
315
316
317
318 bust_spinlocks(1);
319
320 if (address < PAGE_SIZE)
321 printk(KERN_ALERT
322 "Unable to handle kernel NULL pointer dereference");
323 else
324 printk(KERN_ALERT
325 "Unable to handle kernel paging request");
326 printk(" at virtual address %08lx\n", address);
327 printk(" printing pc:\n");
328 printk(KERN_ALERT "%08lx\n", regs->pc);
329
330 debugger_intercept(fault_code & 0x00010000 ? EXCEP_IAERROR : EXCEP_DAERROR,
331 SIGSEGV, SEGV_ACCERR, regs);
332
333 page = PTBR;
334 page = ((unsigned long *) __va(page))[address >> 22];
335 printk(KERN_ALERT "*pde = %08lx\n", page);
336 if (page & 1) {
337 page &= PAGE_MASK;
338 address &= 0x003ff000;
339 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
340 printk(KERN_ALERT "*pte = %08lx\n", page);
341 }
342
343 die("Oops", regs, fault_code);
344 do_exit(SIGKILL);
345
346
347
348
349
350out_of_memory:
351 up_read(&mm->mmap_sem);
352 if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) {
353 pagefault_out_of_memory();
354 return;
355 }
356 goto no_context;
357
358do_sigbus:
359 up_read(&mm->mmap_sem);
360
361
362
363
364
365 info.si_signo = SIGBUS;
366 info.si_errno = 0;
367 info.si_code = BUS_ADRERR;
368 info.si_addr = (void *)address;
369 force_sig_info(SIGBUS, &info, tsk);
370
371
372 if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR)
373 goto no_context;
374 return;
375
376vmalloc_fault:
377 {
378
379
380
381
382
383
384
385 int index = pgd_index(address);
386 pgd_t *pgd, *pgd_k;
387 pud_t *pud, *pud_k;
388 pmd_t *pmd, *pmd_k;
389 pte_t *pte_k;
390
391 pgd_k = init_mm.pgd + index;
392
393 if (!pgd_present(*pgd_k))
394 goto no_context;
395
396 pud_k = pud_offset(pgd_k, address);
397 if (!pud_present(*pud_k))
398 goto no_context;
399
400 pmd_k = pmd_offset(pud_k, address);
401 if (!pmd_present(*pmd_k))
402 goto no_context;
403
404 pgd = (pgd_t *) PTBR + index;
405 pud = pud_offset(pgd, address);
406 pmd = pmd_offset(pud, address);
407 set_pmd(pmd, *pmd_k);
408
409 pte_k = pte_offset_kernel(pmd_k, address);
410 if (!pte_present(*pte_k))
411 goto no_context;
412 return;
413 }
414}
415