1 | /* |
---|
2 | * linux/arch/x86-64/mm/fault.c |
---|
3 | * |
---|
4 | * Copyright (C) 1995 Linus Torvalds |
---|
5 | * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs. |
---|
6 | */ |
---|
7 | |
---|
8 | #include <linux/signal.h> |
---|
9 | #include <linux/sched.h> |
---|
10 | #include <linux/kernel.h> |
---|
11 | #include <linux/errno.h> |
---|
12 | #include <linux/string.h> |
---|
13 | #include <linux/types.h> |
---|
14 | #include <linux/ptrace.h> |
---|
15 | #include <linux/mman.h> |
---|
16 | #include <linux/mm.h> |
---|
17 | #include <linux/smp.h> |
---|
18 | #include <linux/smp_lock.h> |
---|
19 | #include <linux/interrupt.h> |
---|
20 | #include <linux/init.h> |
---|
21 | #include <linux/tty.h> |
---|
22 | #include <linux/vt_kern.h> /* For unblank_screen() */ |
---|
23 | #include <linux/compiler.h> |
---|
24 | #include <linux/module.h> |
---|
25 | #include <linux/kprobes.h> |
---|
26 | |
---|
27 | #include <asm/system.h> |
---|
28 | #include <asm/uaccess.h> |
---|
29 | #include <asm/pgalloc.h> |
---|
30 | #include <asm/smp.h> |
---|
31 | #include <asm/tlbflush.h> |
---|
32 | #include <asm/proto.h> |
---|
33 | #include <asm/kdebug.h> |
---|
34 | #include <asm-generic/sections.h> |
---|
35 | |
---|
36 | /* Page fault error code bits */ |
---|
37 | #define PF_PROT (1<<0) /* or no page found */ |
---|
38 | #define PF_WRITE (1<<1) |
---|
39 | #define PF_USER (1<<2) |
---|
40 | #define PF_RSVD (1<<3) |
---|
41 | #define PF_INSTR (1<<4) |
---|
42 | |
---|
43 | #ifdef CONFIG_KPROBES |
---|
44 | ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain); |
---|
45 | |
---|
46 | /* Hook to register for page fault notifications */ |
---|
47 | int register_page_fault_notifier(struct notifier_block *nb) |
---|
48 | { |
---|
49 | vmalloc_sync_all(); |
---|
50 | return atomic_notifier_chain_register(¬ify_page_fault_chain, nb); |
---|
51 | } |
---|
52 | |
---|
53 | int unregister_page_fault_notifier(struct notifier_block *nb) |
---|
54 | { |
---|
55 | return atomic_notifier_chain_unregister(¬ify_page_fault_chain, nb); |
---|
56 | } |
---|
57 | |
---|
58 | static inline int notify_page_fault(enum die_val val, const char *str, |
---|
59 | struct pt_regs *regs, long err, int trap, int sig) |
---|
60 | { |
---|
61 | struct die_args args = { |
---|
62 | .regs = regs, |
---|
63 | .str = str, |
---|
64 | .err = err, |
---|
65 | .trapnr = trap, |
---|
66 | .signr = sig |
---|
67 | }; |
---|
68 | return atomic_notifier_call_chain(¬ify_page_fault_chain, val, &args); |
---|
69 | } |
---|
70 | #else |
---|
71 | static inline int notify_page_fault(enum die_val val, const char *str, |
---|
72 | struct pt_regs *regs, long err, int trap, int sig) |
---|
73 | { |
---|
74 | return NOTIFY_DONE; |
---|
75 | } |
---|
76 | #endif |
---|
77 | |
---|
78 | void bust_spinlocks(int yes) |
---|
79 | { |
---|
80 | int loglevel_save = console_loglevel; |
---|
81 | if (yes) { |
---|
82 | oops_in_progress = 1; |
---|
83 | } else { |
---|
84 | #ifdef CONFIG_VT |
---|
85 | unblank_screen(); |
---|
86 | #endif |
---|
87 | oops_in_progress = 0; |
---|
88 | /* |
---|
89 | * OK, the message is on the console. Now we call printk() |
---|
90 | * without oops_in_progress set so that printk will give klogd |
---|
91 | * a poke. Hold onto your hats... |
---|
92 | */ |
---|
93 | console_loglevel = 15; /* NMI oopser may have shut the console up */ |
---|
94 | printk(" "); |
---|
95 | console_loglevel = loglevel_save; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /* Sometimes the CPU reports invalid exceptions on prefetch. |
---|
100 | Check that here and ignore. |
---|
101 | Opcode checker based on code by Richard Brunner */ |
---|
102 | static noinline int is_prefetch(struct pt_regs *regs, unsigned long addr, |
---|
103 | unsigned long error_code) |
---|
104 | { |
---|
105 | unsigned char *instr; |
---|
106 | int scan_more = 1; |
---|
107 | int prefetch = 0; |
---|
108 | unsigned char *max_instr; |
---|
109 | |
---|
110 | /* If it was a exec fault ignore */ |
---|
111 | if (error_code & PF_INSTR) |
---|
112 | return 0; |
---|
113 | |
---|
114 | instr = (unsigned char *)convert_rip_to_linear(current, regs); |
---|
115 | max_instr = instr + 15; |
---|
116 | |
---|
117 | if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE) |
---|
118 | return 0; |
---|
119 | |
---|
120 | while (scan_more && instr < max_instr) { |
---|
121 | unsigned char opcode; |
---|
122 | unsigned char instr_hi; |
---|
123 | unsigned char instr_lo; |
---|
124 | |
---|
125 | if (__get_user(opcode, instr)) |
---|
126 | break; |
---|
127 | |
---|
128 | instr_hi = opcode & 0xf0; |
---|
129 | instr_lo = opcode & 0x0f; |
---|
130 | instr++; |
---|
131 | |
---|
132 | switch (instr_hi) { |
---|
133 | case 0x20: |
---|
134 | case 0x30: |
---|
135 | /* Values 0x26,0x2E,0x36,0x3E are valid x86 |
---|
136 | prefixes. In long mode, the CPU will signal |
---|
137 | invalid opcode if some of these prefixes are |
---|
138 | present so we will never get here anyway */ |
---|
139 | scan_more = ((instr_lo & 7) == 0x6); |
---|
140 | break; |
---|
141 | |
---|
142 | case 0x40: |
---|
143 | /* In AMD64 long mode, 0x40 to 0x4F are valid REX prefixes |
---|
144 | Need to figure out under what instruction mode the |
---|
145 | instruction was issued ... */ |
---|
146 | /* Could check the LDT for lm, but for now it's good |
---|
147 | enough to assume that long mode only uses well known |
---|
148 | segments or kernel. */ |
---|
149 | scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS); |
---|
150 | break; |
---|
151 | |
---|
152 | case 0x60: |
---|
153 | /* 0x64 thru 0x67 are valid prefixes in all modes. */ |
---|
154 | scan_more = (instr_lo & 0xC) == 0x4; |
---|
155 | break; |
---|
156 | case 0xF0: |
---|
157 | /* 0xF0, 0xF2, and 0xF3 are valid prefixes in all modes. */ |
---|
158 | scan_more = !instr_lo || (instr_lo>>1) == 1; |
---|
159 | break; |
---|
160 | case 0x00: |
---|
161 | /* Prefetch instruction is 0x0F0D or 0x0F18 */ |
---|
162 | scan_more = 0; |
---|
163 | if (__get_user(opcode, instr)) |
---|
164 | break; |
---|
165 | prefetch = (instr_lo == 0xF) && |
---|
166 | (opcode == 0x0D || opcode == 0x18); |
---|
167 | break; |
---|
168 | default: |
---|
169 | scan_more = 0; |
---|
170 | break; |
---|
171 | } |
---|
172 | } |
---|
173 | return prefetch; |
---|
174 | } |
---|
175 | |
---|
176 | static int bad_address(void *p) |
---|
177 | { |
---|
178 | unsigned long dummy; |
---|
179 | return __get_user(dummy, (unsigned long *)p); |
---|
180 | } |
---|
181 | |
---|
182 | void dump_pagetable(unsigned long address) |
---|
183 | { |
---|
184 | pgd_t *pgd; |
---|
185 | pud_t *pud; |
---|
186 | pmd_t *pmd; |
---|
187 | pte_t *pte; |
---|
188 | |
---|
189 | pgd = __va(read_cr3() & PHYSICAL_PAGE_MASK); |
---|
190 | pgd += pgd_index(address); |
---|
191 | if (bad_address(pgd)) goto bad; |
---|
192 | printk("PGD %lx ", pgd_val(*pgd)); |
---|
193 | if (!pgd_present(*pgd)) goto ret; |
---|
194 | |
---|
195 | pud = pud_offset(pgd, address); |
---|
196 | if (bad_address(pud)) goto bad; |
---|
197 | printk("PUD %lx ", pud_val(*pud)); |
---|
198 | if (!pud_present(*pud)) goto ret; |
---|
199 | |
---|
200 | pmd = pmd_offset(pud, address); |
---|
201 | if (bad_address(pmd)) goto bad; |
---|
202 | printk("PMD %lx ", pmd_val(*pmd)); |
---|
203 | if (!pmd_present(*pmd)) goto ret; |
---|
204 | |
---|
205 | pte = pte_offset_kernel(pmd, address); |
---|
206 | if (bad_address(pte)) goto bad; |
---|
207 | printk("PTE %lx", pte_val(*pte)); |
---|
208 | ret: |
---|
209 | printk("\n"); |
---|
210 | return; |
---|
211 | bad: |
---|
212 | printk("BAD\n"); |
---|
213 | } |
---|
214 | |
---|
215 | static const char errata93_warning[] = |
---|
216 | KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n" |
---|
217 | KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n" |
---|
218 | KERN_ERR "******* Please consider a BIOS update.\n" |
---|
219 | KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n"; |
---|
220 | |
---|
221 | /* Workaround for K8 erratum #93 & buggy BIOS. |
---|
222 | BIOS SMM functions are required to use a specific workaround |
---|
223 | to avoid corruption of the 64bit RIP register on C stepping K8. |
---|
224 | A lot of BIOS that didn't get tested properly miss this. |
---|
225 | The OS sees this as a page fault with the upper 32bits of RIP cleared. |
---|
226 | Try to work around it here. |
---|
227 | Note we only handle faults in kernel here. */ |
---|
228 | |
---|
229 | static int is_errata93(struct pt_regs *regs, unsigned long address) |
---|
230 | { |
---|
231 | static int warned; |
---|
232 | if (address != regs->rip) |
---|
233 | return 0; |
---|
234 | if ((address >> 32) != 0) |
---|
235 | return 0; |
---|
236 | address |= 0xffffffffUL << 32; |
---|
237 | if ((address >= (u64)_stext && address <= (u64)_etext) || |
---|
238 | (address >= MODULES_VADDR && address <= MODULES_END)) { |
---|
239 | if (!warned) { |
---|
240 | printk(errata93_warning); |
---|
241 | warned = 1; |
---|
242 | } |
---|
243 | regs->rip = address; |
---|
244 | return 1; |
---|
245 | } |
---|
246 | return 0; |
---|
247 | } |
---|
248 | |
---|
249 | int unhandled_signal(struct task_struct *tsk, int sig) |
---|
250 | { |
---|
251 | if (tsk->pid == 1) |
---|
252 | return 1; |
---|
253 | if (tsk->ptrace & PT_PTRACED) |
---|
254 | return 0; |
---|
255 | return (tsk->sighand->action[sig-1].sa.sa_handler == SIG_IGN) || |
---|
256 | (tsk->sighand->action[sig-1].sa.sa_handler == SIG_DFL); |
---|
257 | } |
---|
258 | |
---|
259 | static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs, |
---|
260 | unsigned long error_code) |
---|
261 | { |
---|
262 | unsigned long flags = oops_begin(); |
---|
263 | struct task_struct *tsk; |
---|
264 | |
---|
265 | printk(KERN_ALERT "%s: Corrupted page table at address %lx\n", |
---|
266 | current->comm, address); |
---|
267 | dump_pagetable(address); |
---|
268 | tsk = current; |
---|
269 | tsk->thread.cr2 = address; |
---|
270 | tsk->thread.trap_no = 14; |
---|
271 | tsk->thread.error_code = error_code; |
---|
272 | __die("Bad pagetable", regs, error_code); |
---|
273 | oops_end(flags); |
---|
274 | do_exit(SIGKILL); |
---|
275 | } |
---|
276 | |
---|
277 | /* |
---|
278 | * Handle a fault on the vmalloc area |
---|
279 | * |
---|
280 | * This assumes no large pages in there. |
---|
281 | */ |
---|
282 | static int vmalloc_fault(unsigned long address) |
---|
283 | { |
---|
284 | pgd_t *pgd, *pgd_ref; |
---|
285 | pud_t *pud, *pud_ref; |
---|
286 | pmd_t *pmd, *pmd_ref; |
---|
287 | pte_t *pte, *pte_ref; |
---|
288 | |
---|
289 | /* Copy kernel mappings over when needed. This can also |
---|
290 | happen within a race in page table update. In the later |
---|
291 | case just flush. */ |
---|
292 | |
---|
293 | /* On Xen the line below does not always work. Needs investigating! */ |
---|
294 | /*pgd = pgd_offset(current->mm ?: &init_mm, address);*/ |
---|
295 | pgd = __va(read_cr3() & PHYSICAL_PAGE_MASK); |
---|
296 | pgd += pgd_index(address); |
---|
297 | pgd_ref = pgd_offset_k(address); |
---|
298 | if (pgd_none(*pgd_ref)) |
---|
299 | return -1; |
---|
300 | if (pgd_none(*pgd)) |
---|
301 | set_pgd(pgd, *pgd_ref); |
---|
302 | else |
---|
303 | BUG_ON(pgd_page(*pgd) != pgd_page(*pgd_ref)); |
---|
304 | |
---|
305 | /* Below here mismatches are bugs because these lower tables |
---|
306 | are shared */ |
---|
307 | |
---|
308 | pud = pud_offset(pgd, address); |
---|
309 | pud_ref = pud_offset(pgd_ref, address); |
---|
310 | if (pud_none(*pud_ref)) |
---|
311 | return -1; |
---|
312 | if (pud_none(*pud) || pud_page(*pud) != pud_page(*pud_ref)) |
---|
313 | BUG(); |
---|
314 | pmd = pmd_offset(pud, address); |
---|
315 | pmd_ref = pmd_offset(pud_ref, address); |
---|
316 | if (pmd_none(*pmd_ref)) |
---|
317 | return -1; |
---|
318 | if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref)) |
---|
319 | BUG(); |
---|
320 | pte_ref = pte_offset_kernel(pmd_ref, address); |
---|
321 | if (!pte_present(*pte_ref)) |
---|
322 | return -1; |
---|
323 | pte = pte_offset_kernel(pmd, address); |
---|
324 | /* Don't use pte_page here, because the mappings can point |
---|
325 | outside mem_map, and the NUMA hash lookup cannot handle |
---|
326 | that. */ |
---|
327 | if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref)) |
---|
328 | BUG(); |
---|
329 | return 0; |
---|
330 | } |
---|
331 | |
---|
332 | int page_fault_trace = 0; |
---|
333 | int exception_trace = 1; |
---|
334 | |
---|
335 | |
---|
336 | #define MEM_VERBOSE 1 |
---|
337 | |
---|
338 | #ifdef MEM_VERBOSE |
---|
339 | #define MEM_LOG(_f, _a...) \ |
---|
340 | printk("fault.c:[%d]-> " _f "\n", \ |
---|
341 | __LINE__ , ## _a ) |
---|
342 | #else |
---|
343 | #define MEM_LOG(_f, _a...) ((void)0) |
---|
344 | #endif |
---|
345 | |
---|
346 | static int spurious_fault(struct pt_regs *regs, |
---|
347 | unsigned long address, |
---|
348 | unsigned long error_code) |
---|
349 | { |
---|
350 | pgd_t *pgd; |
---|
351 | pud_t *pud; |
---|
352 | pmd_t *pmd; |
---|
353 | pte_t *pte; |
---|
354 | |
---|
355 | #ifdef CONFIG_XEN |
---|
356 | /* Faults in hypervisor area are never spurious. */ |
---|
357 | if ((address >= HYPERVISOR_VIRT_START) && |
---|
358 | (address < HYPERVISOR_VIRT_END)) |
---|
359 | return 0; |
---|
360 | #endif |
---|
361 | |
---|
362 | /* Reserved-bit violation or user access to kernel space? */ |
---|
363 | if (error_code & (PF_RSVD|PF_USER)) |
---|
364 | return 0; |
---|
365 | |
---|
366 | pgd = init_mm.pgd + pgd_index(address); |
---|
367 | if (!pgd_present(*pgd)) |
---|
368 | return 0; |
---|
369 | |
---|
370 | pud = pud_offset(pgd, address); |
---|
371 | if (!pud_present(*pud)) |
---|
372 | return 0; |
---|
373 | |
---|
374 | pmd = pmd_offset(pud, address); |
---|
375 | if (!pmd_present(*pmd)) |
---|
376 | return 0; |
---|
377 | |
---|
378 | pte = pte_offset_kernel(pmd, address); |
---|
379 | if (!pte_present(*pte)) |
---|
380 | return 0; |
---|
381 | if ((error_code & PF_WRITE) && !pte_write(*pte)) |
---|
382 | return 0; |
---|
383 | if ((error_code & PF_INSTR) && (pte_val(*pte) & _PAGE_NX)) |
---|
384 | return 0; |
---|
385 | |
---|
386 | return 1; |
---|
387 | } |
---|
388 | |
---|
389 | /* |
---|
390 | * This routine handles page faults. It determines the address, |
---|
391 | * and the problem, and then passes it off to one of the appropriate |
---|
392 | * routines. |
---|
393 | */ |
---|
394 | asmlinkage void __kprobes do_page_fault(struct pt_regs *regs, |
---|
395 | unsigned long error_code) |
---|
396 | { |
---|
397 | struct task_struct *tsk; |
---|
398 | struct mm_struct *mm; |
---|
399 | struct vm_area_struct * vma; |
---|
400 | unsigned long address; |
---|
401 | const struct exception_table_entry *fixup; |
---|
402 | int write; |
---|
403 | unsigned long flags; |
---|
404 | siginfo_t info; |
---|
405 | |
---|
406 | if (!user_mode(regs)) |
---|
407 | error_code &= ~PF_USER; /* means kernel */ |
---|
408 | |
---|
409 | tsk = current; |
---|
410 | mm = tsk->mm; |
---|
411 | prefetchw(&mm->mmap_sem); |
---|
412 | |
---|
413 | /* get the address */ |
---|
414 | address = current_vcpu_info()->arch.cr2; |
---|
415 | |
---|
416 | info.si_code = SEGV_MAPERR; |
---|
417 | |
---|
418 | |
---|
419 | /* |
---|
420 | * We fault-in kernel-space virtual memory on-demand. The |
---|
421 | * 'reference' page table is init_mm.pgd. |
---|
422 | * |
---|
423 | * NOTE! We MUST NOT take any locks for this case. We may |
---|
424 | * be in an interrupt or a critical region, and should |
---|
425 | * only copy the information from the master page table, |
---|
426 | * nothing more. |
---|
427 | * |
---|
428 | * This verifies that the fault happens in kernel space |
---|
429 | * (error_code & 4) == 0, and that the fault was not a |
---|
430 | * protection error (error_code & 9) == 0. |
---|
431 | */ |
---|
432 | if (unlikely(address >= TASK_SIZE64)) { |
---|
433 | /* |
---|
434 | * Don't check for the module range here: its PML4 |
---|
435 | * is always initialized because it's shared with the main |
---|
436 | * kernel text. Only vmalloc may need PML4 syncups. |
---|
437 | */ |
---|
438 | if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) && |
---|
439 | ((address >= VMALLOC_START && address < VMALLOC_END))) { |
---|
440 | if (vmalloc_fault(address) >= 0) |
---|
441 | return; |
---|
442 | } |
---|
443 | /* Can take a spurious fault if mapping changes R/O -> R/W. */ |
---|
444 | if (spurious_fault(regs, address, error_code)) |
---|
445 | return; |
---|
446 | if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14, |
---|
447 | SIGSEGV) == NOTIFY_STOP) |
---|
448 | return; |
---|
449 | /* |
---|
450 | * Don't take the mm semaphore here. If we fixup a prefetch |
---|
451 | * fault we could otherwise deadlock. |
---|
452 | */ |
---|
453 | goto bad_area_nosemaphore; |
---|
454 | } |
---|
455 | |
---|
456 | if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14, |
---|
457 | SIGSEGV) == NOTIFY_STOP) |
---|
458 | return; |
---|
459 | |
---|
460 | if (likely(regs->eflags & X86_EFLAGS_IF)) |
---|
461 | local_irq_enable(); |
---|
462 | |
---|
463 | if (unlikely(page_fault_trace)) |
---|
464 | printk("pagefault rip:%lx rsp:%lx cs:%lu ss:%lu address %lx error %lx\n", |
---|
465 | regs->rip,regs->rsp,regs->cs,regs->ss,address,error_code); |
---|
466 | |
---|
467 | if (unlikely(error_code & PF_RSVD)) |
---|
468 | pgtable_bad(address, regs, error_code); |
---|
469 | |
---|
470 | /* |
---|
471 | * If we're in an interrupt or have no user |
---|
472 | * context, we must not take the fault.. |
---|
473 | */ |
---|
474 | if (unlikely(in_atomic() || !mm)) |
---|
475 | goto bad_area_nosemaphore; |
---|
476 | |
---|
477 | again: |
---|
478 | /* When running in the kernel we expect faults to occur only to |
---|
479 | * addresses in user space. All other faults represent errors in the |
---|
480 | * kernel and should generate an OOPS. Unfortunatly, in the case of an |
---|
481 | * erroneous fault occurring in a code path which already holds mmap_sem |
---|
482 | * we will deadlock attempting to validate the fault against the |
---|
483 | * address space. Luckily the kernel only validly references user |
---|
484 | * space from well defined areas of code, which are listed in the |
---|
485 | * exceptions table. |
---|
486 | * |
---|
487 | * As the vast majority of faults will be valid we will only perform |
---|
488 | * the source reference check when there is a possibilty of a deadlock. |
---|
489 | * Attempt to lock the address space, if we cannot we then validate the |
---|
490 | * source. If this is invalid we can skip the address space check, |
---|
491 | * thus avoiding the deadlock. |
---|
492 | */ |
---|
493 | if (!down_read_trylock(&mm->mmap_sem)) { |
---|
494 | if ((error_code & PF_USER) == 0 && |
---|
495 | !search_exception_tables(regs->rip)) |
---|
496 | goto bad_area_nosemaphore; |
---|
497 | down_read(&mm->mmap_sem); |
---|
498 | } |
---|
499 | |
---|
500 | vma = find_vma(mm, address); |
---|
501 | if (!vma) |
---|
502 | goto bad_area; |
---|
503 | if (likely(vma->vm_start <= address)) |
---|
504 | goto good_area; |
---|
505 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
---|
506 | goto bad_area; |
---|
507 | if (error_code & 4) { |
---|
508 | /* Allow userspace just enough access below the stack pointer |
---|
509 | * to let the 'enter' instruction work. |
---|
510 | */ |
---|
511 | if (address + 65536 + 32 * sizeof(unsigned long) < regs->rsp) |
---|
512 | goto bad_area; |
---|
513 | } |
---|
514 | if (expand_stack(vma, address)) |
---|
515 | goto bad_area; |
---|
516 | /* |
---|
517 | * Ok, we have a good vm_area for this memory access, so |
---|
518 | * we can handle it.. |
---|
519 | */ |
---|
520 | good_area: |
---|
521 | info.si_code = SEGV_ACCERR; |
---|
522 | write = 0; |
---|
523 | switch (error_code & (PF_PROT|PF_WRITE)) { |
---|
524 | default: /* 3: write, present */ |
---|
525 | /* fall through */ |
---|
526 | case PF_WRITE: /* write, not present */ |
---|
527 | if (!(vma->vm_flags & VM_WRITE)) |
---|
528 | goto bad_area; |
---|
529 | write++; |
---|
530 | break; |
---|
531 | case PF_PROT: /* read, present */ |
---|
532 | goto bad_area; |
---|
533 | case 0: /* read, not present */ |
---|
534 | if (!(vma->vm_flags & (VM_READ | VM_EXEC))) |
---|
535 | goto bad_area; |
---|
536 | } |
---|
537 | |
---|
538 | /* |
---|
539 | * If for any reason at all we couldn't handle the fault, |
---|
540 | * make sure we exit gracefully rather than endlessly redo |
---|
541 | * the fault. |
---|
542 | */ |
---|
543 | switch (handle_mm_fault(mm, vma, address, write)) { |
---|
544 | case VM_FAULT_MINOR: |
---|
545 | tsk->min_flt++; |
---|
546 | break; |
---|
547 | case VM_FAULT_MAJOR: |
---|
548 | tsk->maj_flt++; |
---|
549 | break; |
---|
550 | case VM_FAULT_SIGBUS: |
---|
551 | goto do_sigbus; |
---|
552 | default: |
---|
553 | goto out_of_memory; |
---|
554 | } |
---|
555 | |
---|
556 | up_read(&mm->mmap_sem); |
---|
557 | return; |
---|
558 | |
---|
559 | /* |
---|
560 | * Something tried to access memory that isn't in our memory map.. |
---|
561 | * Fix it, but check if it's kernel or user first.. |
---|
562 | */ |
---|
563 | bad_area: |
---|
564 | up_read(&mm->mmap_sem); |
---|
565 | |
---|
566 | bad_area_nosemaphore: |
---|
567 | /* User mode accesses just cause a SIGSEGV */ |
---|
568 | if (error_code & PF_USER) { |
---|
569 | if (is_prefetch(regs, address, error_code)) |
---|
570 | return; |
---|
571 | |
---|
572 | /* Work around K8 erratum #100 K8 in compat mode |
---|
573 | occasionally jumps to illegal addresses >4GB. We |
---|
574 | catch this here in the page fault handler because |
---|
575 | these addresses are not reachable. Just detect this |
---|
576 | case and return. Any code segment in LDT is |
---|
577 | compatibility mode. */ |
---|
578 | if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && |
---|
579 | (address >> 32)) |
---|
580 | return; |
---|
581 | |
---|
582 | if (exception_trace && unhandled_signal(tsk, SIGSEGV)) { |
---|
583 | printk( |
---|
584 | "%s%s[%d]: segfault at %016lx rip %016lx rsp %016lx error %lx\n", |
---|
585 | tsk->pid > 1 ? KERN_INFO : KERN_EMERG, |
---|
586 | tsk->comm, tsk->pid, address, regs->rip, |
---|
587 | regs->rsp, error_code); |
---|
588 | } |
---|
589 | |
---|
590 | tsk->thread.cr2 = address; |
---|
591 | /* Kernel addresses are always protection faults */ |
---|
592 | tsk->thread.error_code = error_code | (address >= TASK_SIZE); |
---|
593 | tsk->thread.trap_no = 14; |
---|
594 | info.si_signo = SIGSEGV; |
---|
595 | info.si_errno = 0; |
---|
596 | /* info.si_code has been set above */ |
---|
597 | info.si_addr = (void __user *)address; |
---|
598 | force_sig_info(SIGSEGV, &info, tsk); |
---|
599 | return; |
---|
600 | } |
---|
601 | |
---|
602 | no_context: |
---|
603 | |
---|
604 | /* Are we prepared to handle this kernel fault? */ |
---|
605 | fixup = search_exception_tables(regs->rip); |
---|
606 | if (fixup) { |
---|
607 | regs->rip = fixup->fixup; |
---|
608 | return; |
---|
609 | } |
---|
610 | |
---|
611 | /* |
---|
612 | * Hall of shame of CPU/BIOS bugs. |
---|
613 | */ |
---|
614 | |
---|
615 | if (is_prefetch(regs, address, error_code)) |
---|
616 | return; |
---|
617 | |
---|
618 | if (is_errata93(regs, address)) |
---|
619 | return; |
---|
620 | |
---|
621 | /* |
---|
622 | * Oops. The kernel tried to access some bad page. We'll have to |
---|
623 | * terminate things with extreme prejudice. |
---|
624 | */ |
---|
625 | |
---|
626 | flags = oops_begin(); |
---|
627 | |
---|
628 | if (address < PAGE_SIZE) |
---|
629 | printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference"); |
---|
630 | else |
---|
631 | printk(KERN_ALERT "Unable to handle kernel paging request"); |
---|
632 | printk(" at %016lx RIP: \n" KERN_ALERT,address); |
---|
633 | printk_address(regs->rip); |
---|
634 | dump_pagetable(address); |
---|
635 | tsk->thread.cr2 = address; |
---|
636 | tsk->thread.trap_no = 14; |
---|
637 | tsk->thread.error_code = error_code; |
---|
638 | __die("Oops", regs, error_code); |
---|
639 | /* Executive summary in case the body of the oops scrolled away */ |
---|
640 | printk(KERN_EMERG "CR2: %016lx\n", address); |
---|
641 | oops_end(flags); |
---|
642 | do_exit(SIGKILL); |
---|
643 | |
---|
644 | /* |
---|
645 | * We ran out of memory, or some other thing happened to us that made |
---|
646 | * us unable to handle the page fault gracefully. |
---|
647 | */ |
---|
648 | out_of_memory: |
---|
649 | up_read(&mm->mmap_sem); |
---|
650 | if (current->pid == 1) { |
---|
651 | yield(); |
---|
652 | goto again; |
---|
653 | } |
---|
654 | printk("VM: killing process %s\n", tsk->comm); |
---|
655 | if (error_code & 4) |
---|
656 | do_exit(SIGKILL); |
---|
657 | goto no_context; |
---|
658 | |
---|
659 | do_sigbus: |
---|
660 | up_read(&mm->mmap_sem); |
---|
661 | |
---|
662 | /* Kernel mode? Handle exceptions or die */ |
---|
663 | if (!(error_code & PF_USER)) |
---|
664 | goto no_context; |
---|
665 | |
---|
666 | tsk->thread.cr2 = address; |
---|
667 | tsk->thread.error_code = error_code; |
---|
668 | tsk->thread.trap_no = 14; |
---|
669 | info.si_signo = SIGBUS; |
---|
670 | info.si_errno = 0; |
---|
671 | info.si_code = BUS_ADRERR; |
---|
672 | info.si_addr = (void __user *)address; |
---|
673 | force_sig_info(SIGBUS, &info, tsk); |
---|
674 | return; |
---|
675 | } |
---|
676 | |
---|
677 | DEFINE_SPINLOCK(pgd_lock); |
---|
678 | struct page *pgd_list; |
---|
679 | |
---|
680 | void vmalloc_sync_all(void) |
---|
681 | { |
---|
682 | /* Note that races in the updates of insync and start aren't |
---|
683 | problematic: |
---|
684 | insync can only get set bits added, and updates to start are only |
---|
685 | improving performance (without affecting correctness if undone). */ |
---|
686 | static DECLARE_BITMAP(insync, PTRS_PER_PGD); |
---|
687 | static unsigned long start = VMALLOC_START & PGDIR_MASK; |
---|
688 | unsigned long address; |
---|
689 | |
---|
690 | for (address = start; address <= VMALLOC_END; address += PGDIR_SIZE) { |
---|
691 | if (!test_bit(pgd_index(address), insync)) { |
---|
692 | const pgd_t *pgd_ref = pgd_offset_k(address); |
---|
693 | struct page *page; |
---|
694 | |
---|
695 | if (pgd_none(*pgd_ref)) |
---|
696 | continue; |
---|
697 | spin_lock(&pgd_lock); |
---|
698 | for (page = pgd_list; page; |
---|
699 | page = (struct page *)page->index) { |
---|
700 | pgd_t *pgd; |
---|
701 | pgd = (pgd_t *)page_address(page) + pgd_index(address); |
---|
702 | if (pgd_none(*pgd)) |
---|
703 | set_pgd(pgd, *pgd_ref); |
---|
704 | else |
---|
705 | BUG_ON(pgd_page(*pgd) != pgd_page(*pgd_ref)); |
---|
706 | } |
---|
707 | spin_unlock(&pgd_lock); |
---|
708 | set_bit(pgd_index(address), insync); |
---|
709 | } |
---|
710 | if (address == start) |
---|
711 | start = address + PGDIR_SIZE; |
---|
712 | } |
---|
713 | /* Check that there is no need to do the same for the modules area. */ |
---|
714 | BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL)); |
---|
715 | BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) == |
---|
716 | (__START_KERNEL & PGDIR_MASK))); |
---|
717 | } |
---|
718 | |
---|
719 | static int __init enable_pagefaulttrace(char *str) |
---|
720 | { |
---|
721 | page_fault_trace = 1; |
---|
722 | return 1; |
---|
723 | } |
---|
724 | __setup("pagefaulttrace", enable_pagefaulttrace); |
---|