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