ia64/xen-unstable

view linux-2.6-xen-sparse/arch/i386/mm/fault-xen.c @ 13409:3efc6a135cc3

x86/PAE linux: don't restrict upper half of page table entries to 3 bits
when dumping the page table contents during an oops.

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author kfraser@localhost.localdomain
date Fri Jan 12 14:48:06 2007 +0000 (2007-01-12)
parents f6b7ae6ed504
children 84b7639a3bd4
line source
1 /*
2 * linux/arch/i386/mm/fault.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 */
7 #include <linux/signal.h>
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/string.h>
12 #include <linux/types.h>
13 #include <linux/ptrace.h>
14 #include <linux/mman.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/interrupt.h>
19 #include <linux/init.h>
20 #include <linux/tty.h>
21 #include <linux/vt_kern.h> /* For unblank_screen() */
22 #include <linux/highmem.h>
23 #include <linux/module.h>
24 #include <linux/kprobes.h>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
28 #include <asm/desc.h>
29 #include <asm/kdebug.h>
31 extern void die(const char *,struct pt_regs *,long);
33 /*
34 * Unlock any spinlocks which will prevent us from getting the
35 * message out
36 */
37 void bust_spinlocks(int yes)
38 {
39 int loglevel_save = console_loglevel;
41 if (yes) {
42 oops_in_progress = 1;
43 return;
44 }
45 #ifdef CONFIG_VT
46 unblank_screen();
47 #endif
48 oops_in_progress = 0;
49 /*
50 * OK, the message is on the console. Now we call printk()
51 * without oops_in_progress set so that printk will give klogd
52 * a poke. Hold onto your hats...
53 */
54 console_loglevel = 15; /* NMI oopser may have shut the console up */
55 printk(" ");
56 console_loglevel = loglevel_save;
57 }
59 /*
60 * Return EIP plus the CS segment base. The segment limit is also
61 * adjusted, clamped to the kernel/user address space (whichever is
62 * appropriate), and returned in *eip_limit.
63 *
64 * The segment is checked, because it might have been changed by another
65 * task between the original faulting instruction and here.
66 *
67 * If CS is no longer a valid code segment, or if EIP is beyond the
68 * limit, or if it is a kernel address when CS is not a kernel segment,
69 * then the returned value will be greater than *eip_limit.
70 *
71 * This is slow, but is very rarely executed.
72 */
73 static inline unsigned long get_segment_eip(struct pt_regs *regs,
74 unsigned long *eip_limit)
75 {
76 unsigned long eip = regs->eip;
77 unsigned seg = regs->xcs & 0xffff;
78 u32 seg_ar, seg_limit, base, *desc;
80 /* The standard kernel/user address space limit. */
81 *eip_limit = (seg & 2) ? USER_DS.seg : KERNEL_DS.seg;
83 /* Unlikely, but must come before segment checks. */
84 if (unlikely((regs->eflags & VM_MASK) != 0))
85 return eip + (seg << 4);
87 /* By far the most common cases. */
88 if (likely(seg == __USER_CS || seg == GET_KERNEL_CS()))
89 return eip;
91 /* Check the segment exists, is within the current LDT/GDT size,
92 that kernel/user (ring 0..3) has the appropriate privilege,
93 that it's a code segment, and get the limit. */
94 __asm__ ("larl %3,%0; lsll %3,%1"
95 : "=&r" (seg_ar), "=r" (seg_limit) : "0" (0), "rm" (seg));
96 if ((~seg_ar & 0x9800) || eip > seg_limit) {
97 *eip_limit = 0;
98 return 1; /* So that returned eip > *eip_limit. */
99 }
101 /* Get the GDT/LDT descriptor base.
102 When you look for races in this code remember that
103 LDT and other horrors are only used in user space. */
104 if (seg & (1<<2)) {
105 /* Must lock the LDT while reading it. */
106 down(&current->mm->context.sem);
107 desc = current->mm->context.ldt;
108 desc = (void *)desc + (seg & ~7);
109 } else {
110 /* Must disable preemption while reading the GDT. */
111 desc = (u32 *)get_cpu_gdt_table(get_cpu());
112 desc = (void *)desc + (seg & ~7);
113 }
115 /* Decode the code segment base from the descriptor */
116 base = get_desc_base((unsigned long *)desc);
118 if (seg & (1<<2)) {
119 up(&current->mm->context.sem);
120 } else
121 put_cpu();
123 /* Adjust EIP and segment limit, and clamp at the kernel limit.
124 It's legitimate for segments to wrap at 0xffffffff. */
125 seg_limit += base;
126 if (seg_limit < *eip_limit && seg_limit >= base)
127 *eip_limit = seg_limit;
128 return eip + base;
129 }
131 /*
132 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
133 * Check that here and ignore it.
134 */
135 static int __is_prefetch(struct pt_regs *regs, unsigned long addr)
136 {
137 unsigned long limit;
138 unsigned long instr = get_segment_eip (regs, &limit);
139 int scan_more = 1;
140 int prefetch = 0;
141 int i;
143 for (i = 0; scan_more && i < 15; i++) {
144 unsigned char opcode;
145 unsigned char instr_hi;
146 unsigned char instr_lo;
148 if (instr > limit)
149 break;
150 if (__get_user(opcode, (unsigned char __user *) instr))
151 break;
153 instr_hi = opcode & 0xf0;
154 instr_lo = opcode & 0x0f;
155 instr++;
157 switch (instr_hi) {
158 case 0x20:
159 case 0x30:
160 /* Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes. */
161 scan_more = ((instr_lo & 7) == 0x6);
162 break;
164 case 0x60:
165 /* 0x64 thru 0x67 are valid prefixes in all modes. */
166 scan_more = (instr_lo & 0xC) == 0x4;
167 break;
168 case 0xF0:
169 /* 0xF0, 0xF2, and 0xF3 are valid prefixes */
170 scan_more = !instr_lo || (instr_lo>>1) == 1;
171 break;
172 case 0x00:
173 /* Prefetch instruction is 0x0F0D or 0x0F18 */
174 scan_more = 0;
175 if (instr > limit)
176 break;
177 if (__get_user(opcode, (unsigned char __user *) instr))
178 break;
179 prefetch = (instr_lo == 0xF) &&
180 (opcode == 0x0D || opcode == 0x18);
181 break;
182 default:
183 scan_more = 0;
184 break;
185 }
186 }
187 return prefetch;
188 }
190 static inline int is_prefetch(struct pt_regs *regs, unsigned long addr,
191 unsigned long error_code)
192 {
193 if (unlikely(boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
194 boot_cpu_data.x86 >= 6)) {
195 /* Catch an obscure case of prefetch inside an NX page. */
196 if (nx_enabled && (error_code & 16))
197 return 0;
198 return __is_prefetch(regs, addr);
199 }
200 return 0;
201 }
203 static noinline void force_sig_info_fault(int si_signo, int si_code,
204 unsigned long address, struct task_struct *tsk)
205 {
206 siginfo_t info;
208 info.si_signo = si_signo;
209 info.si_errno = 0;
210 info.si_code = si_code;
211 info.si_addr = (void __user *)address;
212 force_sig_info(si_signo, &info, tsk);
213 }
215 fastcall void do_invalid_op(struct pt_regs *, unsigned long);
217 #ifdef CONFIG_X86_PAE
218 static void dump_fault_path(unsigned long address)
219 {
220 unsigned long *p, page;
221 unsigned long mfn;
223 page = read_cr3();
224 p = (unsigned long *)__va(page);
225 p += (address >> 30) * 2;
226 printk(KERN_ALERT "%08lx -> *pde = %08lx:%08lx\n", page, p[1], p[0]);
227 if (p[0] & 1) {
228 mfn = (p[0] >> PAGE_SHIFT) | (p[1] << 20);
229 page = mfn_to_pfn(mfn) << PAGE_SHIFT;
230 p = (unsigned long *)__va(page);
231 address &= 0x3fffffff;
232 p += (address >> 21) * 2;
233 printk(KERN_ALERT "%08lx -> *pme = %08lx:%08lx\n",
234 page, p[1], p[0]);
235 #ifndef CONFIG_HIGHPTE
236 if (p[0] & 1) {
237 mfn = (p[0] >> PAGE_SHIFT) | (p[1] << 20);
238 page = mfn_to_pfn(mfn) << PAGE_SHIFT;
239 p = (unsigned long *) __va(page);
240 address &= 0x001fffff;
241 p += (address >> 12) * 2;
242 printk(KERN_ALERT "%08lx -> *pte = %08lx:%08lx\n",
243 page, p[1], p[0]);
244 }
245 #endif
246 }
247 }
248 #else
249 static void dump_fault_path(unsigned long address)
250 {
251 unsigned long page;
253 page = read_cr3();
254 page = ((unsigned long *) __va(page))[address >> 22];
255 printk(KERN_ALERT "*pde = ma %08lx pa %08lx\n", page,
256 machine_to_phys(page));
257 /*
258 * We must not directly access the pte in the highpte
259 * case, the page table might be allocated in highmem.
260 * And lets rather not kmap-atomic the pte, just in case
261 * it's allocated already.
262 */
263 #ifndef CONFIG_HIGHPTE
264 if (page & 1) {
265 page &= PAGE_MASK;
266 address &= 0x003ff000;
267 page = machine_to_phys(page);
268 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
269 printk(KERN_ALERT "*pte = ma %08lx pa %08lx\n", page,
270 machine_to_phys(page));
271 }
272 #endif
273 }
274 #endif
276 static int spurious_fault(struct pt_regs *regs,
277 unsigned long address,
278 unsigned long error_code)
279 {
280 pgd_t *pgd;
281 pud_t *pud;
282 pmd_t *pmd;
283 pte_t *pte;
285 /* Reserved-bit violation or user access to kernel space? */
286 if (error_code & 0x0c)
287 return 0;
289 pgd = init_mm.pgd + pgd_index(address);
290 if (!pgd_present(*pgd))
291 return 0;
293 pud = pud_offset(pgd, address);
294 if (!pud_present(*pud))
295 return 0;
297 pmd = pmd_offset(pud, address);
298 if (!pmd_present(*pmd))
299 return 0;
301 pte = pte_offset_kernel(pmd, address);
302 if (!pte_present(*pte))
303 return 0;
304 if ((error_code & 0x02) && !pte_write(*pte))
305 return 0;
306 #ifdef CONFIG_X86_PAE
307 if ((error_code & 0x10) && (pte_val(*pte) & _PAGE_NX))
308 return 0;
309 #endif
311 return 1;
312 }
314 /*
315 * This routine handles page faults. It determines the address,
316 * and the problem, and then passes it off to one of the appropriate
317 * routines.
318 *
319 * error_code:
320 * bit 0 == 0 means no page found, 1 means protection fault
321 * bit 1 == 0 means read, 1 means write
322 * bit 2 == 0 means kernel, 1 means user-mode
323 */
324 fastcall void __kprobes do_page_fault(struct pt_regs *regs,
325 unsigned long error_code)
326 {
327 struct task_struct *tsk;
328 struct mm_struct *mm;
329 struct vm_area_struct * vma;
330 unsigned long address;
331 int write, si_code;
333 /* get the address */
334 address = read_cr2();
336 /* Set the "privileged fault" bit to something sane. */
337 error_code &= ~4;
338 error_code |= (regs->xcs & 2) << 1;
339 if (regs->eflags & X86_EFLAGS_VM)
340 error_code |= 4;
342 if (notify_die(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
343 SIGSEGV) == NOTIFY_STOP)
344 return;
345 /* It's safe to allow irq's after cr2 has been saved */
346 if (regs->eflags & (X86_EFLAGS_IF|VM_MASK))
347 local_irq_enable();
349 tsk = current;
351 si_code = SEGV_MAPERR;
353 /*
354 * We fault-in kernel-space virtual memory on-demand. The
355 * 'reference' page table is init_mm.pgd.
356 *
357 * NOTE! We MUST NOT take any locks for this case. We may
358 * be in an interrupt or a critical region, and should
359 * only copy the information from the master page table,
360 * nothing more.
361 *
362 * This verifies that the fault happens in kernel space
363 * (error_code & 4) == 0, and that the fault was not a
364 * protection error (error_code & 1) == 0.
365 */
366 if (unlikely(address >= TASK_SIZE)) {
367 #ifdef CONFIG_XEN
368 /* Faults in hypervisor area can never be patched up. */
369 if (address >= hypervisor_virt_start)
370 goto bad_area_nosemaphore;
371 #endif
372 if (!(error_code & 5))
373 goto vmalloc_fault;
374 /* Can take a spurious fault if mapping changes R/O -> R/W. */
375 if (spurious_fault(regs, address, error_code))
376 return;
377 /*
378 * Don't take the mm semaphore here. If we fixup a prefetch
379 * fault we could otherwise deadlock.
380 */
381 goto bad_area_nosemaphore;
382 }
384 mm = tsk->mm;
386 /*
387 * If we're in an interrupt, have no user context or are running in an
388 * atomic region then we must not take the fault..
389 */
390 if (in_atomic() || !mm)
391 goto bad_area_nosemaphore;
393 /* When running in the kernel we expect faults to occur only to
394 * addresses in user space. All other faults represent errors in the
395 * kernel and should generate an OOPS. Unfortunatly, in the case of an
396 * erroneous fault occuring in a code path which already holds mmap_sem
397 * we will deadlock attempting to validate the fault against the
398 * address space. Luckily the kernel only validly references user
399 * space from well defined areas of code, which are listed in the
400 * exceptions table.
401 *
402 * As the vast majority of faults will be valid we will only perform
403 * the source reference check when there is a possibilty of a deadlock.
404 * Attempt to lock the address space, if we cannot we then validate the
405 * source. If this is invalid we can skip the address space check,
406 * thus avoiding the deadlock.
407 */
408 if (!down_read_trylock(&mm->mmap_sem)) {
409 if ((error_code & 4) == 0 &&
410 !search_exception_tables(regs->eip))
411 goto bad_area_nosemaphore;
412 down_read(&mm->mmap_sem);
413 }
415 vma = find_vma(mm, address);
416 if (!vma)
417 goto bad_area;
418 if (vma->vm_start <= address)
419 goto good_area;
420 if (!(vma->vm_flags & VM_GROWSDOWN))
421 goto bad_area;
422 if (error_code & 4) {
423 /*
424 * accessing the stack below %esp is always a bug.
425 * The "+ 32" is there due to some instructions (like
426 * pusha) doing post-decrement on the stack and that
427 * doesn't show up until later..
428 */
429 if (address + 32 < regs->esp)
430 goto bad_area;
431 }
432 if (expand_stack(vma, address))
433 goto bad_area;
434 /*
435 * Ok, we have a good vm_area for this memory access, so
436 * we can handle it..
437 */
438 good_area:
439 si_code = SEGV_ACCERR;
440 write = 0;
441 switch (error_code & 3) {
442 default: /* 3: write, present */
443 #ifdef TEST_VERIFY_AREA
444 if (regs->cs == GET_KERNEL_CS())
445 printk("WP fault at %08lx\n", regs->eip);
446 #endif
447 /* fall through */
448 case 2: /* write, not present */
449 if (!(vma->vm_flags & VM_WRITE))
450 goto bad_area;
451 write++;
452 break;
453 case 1: /* read, present */
454 goto bad_area;
455 case 0: /* read, not present */
456 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
457 goto bad_area;
458 }
460 survive:
461 /*
462 * If for any reason at all we couldn't handle the fault,
463 * make sure we exit gracefully rather than endlessly redo
464 * the fault.
465 */
466 switch (handle_mm_fault(mm, vma, address, write)) {
467 case VM_FAULT_MINOR:
468 tsk->min_flt++;
469 break;
470 case VM_FAULT_MAJOR:
471 tsk->maj_flt++;
472 break;
473 case VM_FAULT_SIGBUS:
474 goto do_sigbus;
475 case VM_FAULT_OOM:
476 goto out_of_memory;
477 default:
478 BUG();
479 }
481 /*
482 * Did it hit the DOS screen memory VA from vm86 mode?
483 */
484 if (regs->eflags & VM_MASK) {
485 unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
486 if (bit < 32)
487 tsk->thread.screen_bitmap |= 1 << bit;
488 }
489 up_read(&mm->mmap_sem);
490 return;
492 /*
493 * Something tried to access memory that isn't in our memory map..
494 * Fix it, but check if it's kernel or user first..
495 */
496 bad_area:
497 up_read(&mm->mmap_sem);
499 bad_area_nosemaphore:
500 /* User mode accesses just cause a SIGSEGV */
501 if (error_code & 4) {
502 /*
503 * Valid to do another page fault here because this one came
504 * from user space.
505 */
506 if (is_prefetch(regs, address, error_code))
507 return;
509 tsk->thread.cr2 = address;
510 /* Kernel addresses are always protection faults */
511 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
512 tsk->thread.trap_no = 14;
513 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
514 return;
515 }
517 #ifdef CONFIG_X86_F00F_BUG
518 /*
519 * Pentium F0 0F C7 C8 bug workaround.
520 */
521 if (boot_cpu_data.f00f_bug) {
522 unsigned long nr;
524 nr = (address - idt_descr.address) >> 3;
526 if (nr == 6) {
527 do_invalid_op(regs, 0);
528 return;
529 }
530 }
531 #endif
533 no_context:
534 /* Are we prepared to handle this kernel fault? */
535 if (fixup_exception(regs))
536 return;
538 /*
539 * Valid to do another page fault here, because if this fault
540 * had been triggered by is_prefetch fixup_exception would have
541 * handled it.
542 */
543 if (is_prefetch(regs, address, error_code))
544 return;
546 /*
547 * Oops. The kernel tried to access some bad page. We'll have to
548 * terminate things with extreme prejudice.
549 */
551 bust_spinlocks(1);
553 #ifdef CONFIG_X86_PAE
554 if (error_code & 16) {
555 pte_t *pte = lookup_address(address);
557 if (pte && pte_present(*pte) && !pte_exec_kernel(*pte))
558 printk(KERN_CRIT "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n", current->uid);
559 }
560 #endif
561 if (address < PAGE_SIZE)
562 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
563 else
564 printk(KERN_ALERT "Unable to handle kernel paging request");
565 printk(" at virtual address %08lx\n",address);
566 printk(KERN_ALERT " printing eip:\n");
567 printk("%08lx\n", regs->eip);
568 dump_fault_path(address);
569 tsk->thread.cr2 = address;
570 tsk->thread.trap_no = 14;
571 tsk->thread.error_code = error_code;
572 die("Oops", regs, error_code);
573 bust_spinlocks(0);
574 do_exit(SIGKILL);
576 /*
577 * We ran out of memory, or some other thing happened to us that made
578 * us unable to handle the page fault gracefully.
579 */
580 out_of_memory:
581 up_read(&mm->mmap_sem);
582 if (tsk->pid == 1) {
583 yield();
584 down_read(&mm->mmap_sem);
585 goto survive;
586 }
587 printk("VM: killing process %s\n", tsk->comm);
588 if (error_code & 4)
589 do_exit(SIGKILL);
590 goto no_context;
592 do_sigbus:
593 up_read(&mm->mmap_sem);
595 /* Kernel mode? Handle exceptions or die */
596 if (!(error_code & 4))
597 goto no_context;
599 /* User space => ok to do another page fault */
600 if (is_prefetch(regs, address, error_code))
601 return;
603 tsk->thread.cr2 = address;
604 tsk->thread.error_code = error_code;
605 tsk->thread.trap_no = 14;
606 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
607 return;
609 vmalloc_fault:
610 {
611 /*
612 * Synchronize this task's top level page-table
613 * with the 'reference' page table.
614 *
615 * Do _not_ use "tsk" here. We might be inside
616 * an interrupt in the middle of a task switch..
617 */
618 int index = pgd_index(address);
619 unsigned long pgd_paddr;
620 pgd_t *pgd, *pgd_k;
621 pud_t *pud, *pud_k;
622 pmd_t *pmd, *pmd_k;
623 pte_t *pte_k;
625 pgd_paddr = read_cr3();
626 pgd = index + (pgd_t *)__va(pgd_paddr);
627 pgd_k = init_mm.pgd + index;
629 if (!pgd_present(*pgd_k))
630 goto no_context;
632 /*
633 * set_pgd(pgd, *pgd_k); here would be useless on PAE
634 * and redundant with the set_pmd() on non-PAE. As would
635 * set_pud.
636 */
638 pud = pud_offset(pgd, address);
639 pud_k = pud_offset(pgd_k, address);
640 if (!pud_present(*pud_k))
641 goto no_context;
643 pmd = pmd_offset(pud, address);
644 pmd_k = pmd_offset(pud_k, address);
645 if (!pmd_present(*pmd_k))
646 goto no_context;
647 #ifndef CONFIG_XEN
648 set_pmd(pmd, *pmd_k);
649 #else
650 /*
651 * When running on Xen we must launder *pmd_k through
652 * pmd_val() to ensure that _PAGE_PRESENT is correctly set.
653 */
654 set_pmd(pmd, __pmd(pmd_val(*pmd_k)));
655 #endif
657 pte_k = pte_offset_kernel(pmd_k, address);
658 if (!pte_present(*pte_k))
659 goto no_context;
660 return;
661 }
662 }