2 * Copyright (C) 1995 Linus Torvalds
3 * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
4 * Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar
6 #include <linux/interrupt.h>
7 #include <linux/mmiotrace.h>
8 #include <linux/bootmem.h>
9 #include <linux/compiler.h>
10 #include <linux/highmem.h>
11 #include <linux/kprobes.h>
12 #include <linux/uaccess.h>
13 #include <linux/vmalloc.h>
14 #include <linux/vt_kern.h>
15 #include <linux/signal.h>
16 #include <linux/kernel.h>
17 #include <linux/ptrace.h>
18 #include <linux/string.h>
19 #include <linux/module.h>
20 #include <linux/kdebug.h>
21 #include <linux/errno.h>
22 #include <linux/magic.h>
23 #include <linux/sched.h>
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/mman.h>
27 #include <linux/tty.h>
28 #include <linux/smp.h>
31 #include <asm-generic/sections.h>
33 #include <asm/tlbflush.h>
34 #include <asm/pgalloc.h>
35 #include <asm/segment.h>
36 #include <asm/system.h>
37 #include <asm/proto.h>
38 #include <asm/traps.h>
42 * Page fault error code bits:
44 * bit 0 == 0: no page found 1: protection fault
45 * bit 1 == 0: read access 1: write access
46 * bit 2 == 0: kernel-mode access 1: user-mode access
47 * bit 3 == 1: use of reserved bit detected
48 * bit 4 == 1: fault was an instruction fetch
50 enum x86_pf_error_code
{
60 * Returns 0 if mmiotrace is disabled, or if the fault is not
61 * handled by mmiotrace:
63 static inline int kmmio_fault(struct pt_regs
*regs
, unsigned long addr
)
65 if (unlikely(is_kmmio_active()))
66 if (kmmio_handler(regs
, addr
) == 1)
71 static inline int notify_page_fault(struct pt_regs
*regs
)
75 /* kprobe_running() needs smp_processor_id() */
76 if (kprobes_built_in() && !user_mode_vm(regs
)) {
78 if (kprobe_running() && kprobe_fault_handler(regs
, 14))
91 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
92 * Check that here and ignore it.
96 * Sometimes the CPU reports invalid exceptions on prefetch.
97 * Check that here and ignore it.
99 * Opcode checker based on code by Richard Brunner.
102 check_prefetch_opcode(struct pt_regs
*regs
, unsigned char *instr
,
103 unsigned char opcode
, int *prefetch
)
105 unsigned char instr_hi
= opcode
& 0xf0;
106 unsigned char instr_lo
= opcode
& 0x0f;
112 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
113 * In X86_64 long mode, the CPU will signal invalid
114 * opcode if some of these prefixes are present so
115 * X86_64 will never get here anyway
117 return ((instr_lo
& 7) == 0x6);
121 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
122 * Need to figure out under what instruction mode the
123 * instruction was issued. Could check the LDT for lm,
124 * but for now it's good enough to assume that long
125 * mode only uses well known segments or kernel.
127 return (!user_mode(regs
)) || (regs
->cs
== __USER_CS
);
130 /* 0x64 thru 0x67 are valid prefixes in all modes. */
131 return (instr_lo
& 0xC) == 0x4;
133 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
134 return !instr_lo
|| (instr_lo
>>1) == 1;
136 /* Prefetch instruction is 0x0F0D or 0x0F18 */
137 if (probe_kernel_address(instr
, opcode
))
140 *prefetch
= (instr_lo
== 0xF) &&
141 (opcode
== 0x0D || opcode
== 0x18);
149 is_prefetch(struct pt_regs
*regs
, unsigned long error_code
, unsigned long addr
)
151 unsigned char *max_instr
;
152 unsigned char *instr
;
156 * If it was a exec (instruction fetch) fault on NX page, then
157 * do not ignore the fault:
159 if (error_code
& PF_INSTR
)
162 instr
= (void *)convert_ip_to_linear(current
, regs
);
163 max_instr
= instr
+ 15;
165 if (user_mode(regs
) && instr
>= (unsigned char *)TASK_SIZE
)
168 while (instr
< max_instr
) {
169 unsigned char opcode
;
171 if (probe_kernel_address(instr
, opcode
))
176 if (!check_prefetch_opcode(regs
, instr
, opcode
, &prefetch
))
183 force_sig_info_fault(int si_signo
, int si_code
, unsigned long address
,
184 struct task_struct
*tsk
)
188 info
.si_signo
= si_signo
;
190 info
.si_code
= si_code
;
191 info
.si_addr
= (void __user
*)address
;
193 force_sig_info(si_signo
, &info
, tsk
);
196 DEFINE_SPINLOCK(pgd_lock
);
200 static inline pmd_t
*vmalloc_sync_one(pgd_t
*pgd
, unsigned long address
)
202 unsigned index
= pgd_index(address
);
208 pgd_k
= init_mm
.pgd
+ index
;
210 if (!pgd_present(*pgd_k
))
214 * set_pgd(pgd, *pgd_k); here would be useless on PAE
215 * and redundant with the set_pmd() on non-PAE. As would
218 pud
= pud_offset(pgd
, address
);
219 pud_k
= pud_offset(pgd_k
, address
);
220 if (!pud_present(*pud_k
))
223 pmd
= pmd_offset(pud
, address
);
224 pmd_k
= pmd_offset(pud_k
, address
);
225 if (!pmd_present(*pmd_k
))
228 if (!pmd_present(*pmd
)) {
229 set_pmd(pmd
, *pmd_k
);
230 arch_flush_lazy_mmu_mode();
232 BUG_ON(pmd_page(*pmd
) != pmd_page(*pmd_k
));
238 void vmalloc_sync_all(void)
240 unsigned long address
;
242 if (SHARED_KERNEL_PMD
)
245 for (address
= VMALLOC_START
& PMD_MASK
;
246 address
>= TASK_SIZE
&& address
< FIXADDR_TOP
;
247 address
+= PMD_SIZE
) {
252 spin_lock_irqsave(&pgd_lock
, flags
);
253 list_for_each_entry(page
, &pgd_list
, lru
) {
254 if (!vmalloc_sync_one(page_address(page
), address
))
257 spin_unlock_irqrestore(&pgd_lock
, flags
);
264 * Handle a fault on the vmalloc or module mapping area
266 static noinline
int vmalloc_fault(unsigned long address
)
268 unsigned long pgd_paddr
;
272 /* Make sure we are in vmalloc area: */
273 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
277 * Synchronize this task's top level page-table
278 * with the 'reference' page table.
280 * Do _not_ use "current" here. We might be inside
281 * an interrupt in the middle of a task switch..
283 pgd_paddr
= read_cr3();
284 pmd_k
= vmalloc_sync_one(__va(pgd_paddr
), address
);
288 pte_k
= pte_offset_kernel(pmd_k
, address
);
289 if (!pte_present(*pte_k
))
296 * Did it hit the DOS screen memory VA from vm86 mode?
299 check_v8086_mode(struct pt_regs
*regs
, unsigned long address
,
300 struct task_struct
*tsk
)
304 if (!v8086_mode(regs
))
307 bit
= (address
- 0xA0000) >> PAGE_SHIFT
;
309 tsk
->thread
.screen_bitmap
|= 1 << bit
;
312 static void dump_pagetable(unsigned long address
)
314 __typeof__(pte_val(__pte(0))) page
;
317 page
= ((__typeof__(page
) *) __va(page
))[address
>> PGDIR_SHIFT
];
319 #ifdef CONFIG_X86_PAE
320 printk("*pdpt = %016Lx ", page
);
321 if ((page
>> PAGE_SHIFT
) < max_low_pfn
322 && page
& _PAGE_PRESENT
) {
324 page
= ((__typeof__(page
) *) __va(page
))[(address
>> PMD_SHIFT
)
325 & (PTRS_PER_PMD
- 1)];
326 printk(KERN_CONT
"*pde = %016Lx ", page
);
330 printk("*pde = %08lx ", page
);
334 * We must not directly access the pte in the highpte
335 * case if the page table is located in highmem.
336 * And let's rather not kmap-atomic the pte, just in case
337 * it's allocated already:
339 if ((page
>> PAGE_SHIFT
) < max_low_pfn
340 && (page
& _PAGE_PRESENT
)
341 && !(page
& _PAGE_PSE
)) {
344 page
= ((__typeof__(page
) *) __va(page
))[(address
>> PAGE_SHIFT
)
345 & (PTRS_PER_PTE
- 1)];
346 printk("*pte = %0*Lx ", sizeof(page
)*2, (u64
)page
);
352 #else /* CONFIG_X86_64: */
354 void vmalloc_sync_all(void)
356 unsigned long address
;
358 for (address
= VMALLOC_START
& PGDIR_MASK
; address
<= VMALLOC_END
;
359 address
+= PGDIR_SIZE
) {
361 const pgd_t
*pgd_ref
= pgd_offset_k(address
);
365 if (pgd_none(*pgd_ref
))
368 spin_lock_irqsave(&pgd_lock
, flags
);
369 list_for_each_entry(page
, &pgd_list
, lru
) {
371 pgd
= (pgd_t
*)page_address(page
) + pgd_index(address
);
373 set_pgd(pgd
, *pgd_ref
);
375 BUG_ON(pgd_page_vaddr(*pgd
) != pgd_page_vaddr(*pgd_ref
));
377 spin_unlock_irqrestore(&pgd_lock
, flags
);
384 * Handle a fault on the vmalloc area
386 * This assumes no large pages in there.
388 static noinline
int vmalloc_fault(unsigned long address
)
390 pgd_t
*pgd
, *pgd_ref
;
391 pud_t
*pud
, *pud_ref
;
392 pmd_t
*pmd
, *pmd_ref
;
393 pte_t
*pte
, *pte_ref
;
395 /* Make sure we are in vmalloc area: */
396 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
400 * Copy kernel mappings over when needed. This can also
401 * happen within a race in page table update. In the later
404 pgd
= pgd_offset(current
->active_mm
, address
);
405 pgd_ref
= pgd_offset_k(address
);
406 if (pgd_none(*pgd_ref
))
410 set_pgd(pgd
, *pgd_ref
);
412 BUG_ON(pgd_page_vaddr(*pgd
) != pgd_page_vaddr(*pgd_ref
));
415 * Below here mismatches are bugs because these lower tables
419 pud
= pud_offset(pgd
, address
);
420 pud_ref
= pud_offset(pgd_ref
, address
);
421 if (pud_none(*pud_ref
))
424 if (pud_none(*pud
) || pud_page_vaddr(*pud
) != pud_page_vaddr(*pud_ref
))
427 pmd
= pmd_offset(pud
, address
);
428 pmd_ref
= pmd_offset(pud_ref
, address
);
429 if (pmd_none(*pmd_ref
))
432 if (pmd_none(*pmd
) || pmd_page(*pmd
) != pmd_page(*pmd_ref
))
435 pte_ref
= pte_offset_kernel(pmd_ref
, address
);
436 if (!pte_present(*pte_ref
))
439 pte
= pte_offset_kernel(pmd
, address
);
442 * Don't use pte_page here, because the mappings can point
443 * outside mem_map, and the NUMA hash lookup cannot handle
446 if (!pte_present(*pte
) || pte_pfn(*pte
) != pte_pfn(*pte_ref
))
452 static const char errata93_warning
[] =
453 KERN_ERR
"******* Your BIOS seems to not contain a fix for K8 errata #93\n"
454 KERN_ERR
"******* Working around it, but it may cause SEGVs or burn power.\n"
455 KERN_ERR
"******* Please consider a BIOS update.\n"
456 KERN_ERR
"******* Disabling USB legacy in the BIOS may also help.\n";
459 * No vm86 mode in 64-bit mode:
462 check_v8086_mode(struct pt_regs
*regs
, unsigned long address
,
463 struct task_struct
*tsk
)
467 static int bad_address(void *p
)
471 return probe_kernel_address((unsigned long *)p
, dummy
);
474 static void dump_pagetable(unsigned long address
)
481 pgd
= (pgd_t
*)read_cr3();
483 pgd
= __va((unsigned long)pgd
& PHYSICAL_PAGE_MASK
);
485 pgd
+= pgd_index(address
);
486 if (bad_address(pgd
))
489 printk("PGD %lx ", pgd_val(*pgd
));
491 if (!pgd_present(*pgd
))
494 pud
= pud_offset(pgd
, address
);
495 if (bad_address(pud
))
498 printk("PUD %lx ", pud_val(*pud
));
499 if (!pud_present(*pud
) || pud_large(*pud
))
502 pmd
= pmd_offset(pud
, address
);
503 if (bad_address(pmd
))
506 printk("PMD %lx ", pmd_val(*pmd
));
507 if (!pmd_present(*pmd
) || pmd_large(*pmd
))
510 pte
= pte_offset_kernel(pmd
, address
);
511 if (bad_address(pte
))
514 printk("PTE %lx", pte_val(*pte
));
522 #endif /* CONFIG_X86_64 */
525 * Workaround for K8 erratum #93 & buggy BIOS.
527 * BIOS SMM functions are required to use a specific workaround
528 * to avoid corruption of the 64bit RIP register on C stepping K8.
530 * A lot of BIOS that didn't get tested properly miss this.
532 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
533 * Try to work around it here.
535 * Note we only handle faults in kernel here.
536 * Does nothing on 32-bit.
538 static int is_errata93(struct pt_regs
*regs
, unsigned long address
)
543 if (address
!= regs
->ip
)
546 if ((address
>> 32) != 0)
549 address
|= 0xffffffffUL
<< 32;
550 if ((address
>= (u64
)_stext
&& address
<= (u64
)_etext
) ||
551 (address
>= MODULES_VADDR
&& address
<= MODULES_END
)) {
553 printk(errata93_warning
);
564 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
565 * to illegal addresses >4GB.
567 * We catch this in the page fault handler because these addresses
568 * are not reachable. Just detect this case and return. Any code
569 * segment in LDT is compatibility mode.
571 static int is_errata100(struct pt_regs
*regs
, unsigned long address
)
574 if ((regs
->cs
== __USER32_CS
|| (regs
->cs
& (1<<2))) && (address
>> 32))
580 static int is_f00f_bug(struct pt_regs
*regs
, unsigned long address
)
582 #ifdef CONFIG_X86_F00F_BUG
586 * Pentium F0 0F C7 C8 bug workaround:
588 if (boot_cpu_data
.f00f_bug
) {
589 nr
= (address
- idt_descr
.address
) >> 3;
592 do_invalid_op(regs
, 0);
600 static const char nx_warning
[] = KERN_CRIT
601 "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
604 show_fault_oops(struct pt_regs
*regs
, unsigned long error_code
,
605 unsigned long address
)
607 if (!oops_may_print())
610 if (error_code
& PF_INSTR
) {
613 pte_t
*pte
= lookup_address(address
, &level
);
615 if (pte
&& pte_present(*pte
) && !pte_exec(*pte
))
616 printk(nx_warning
, current_uid());
619 printk(KERN_ALERT
"BUG: unable to handle kernel ");
620 if (address
< PAGE_SIZE
)
621 printk(KERN_CONT
"NULL pointer dereference");
623 printk(KERN_CONT
"paging request");
625 printk(KERN_CONT
" at %p\n", (void *) address
);
626 printk(KERN_ALERT
"IP:");
627 printk_address(regs
->ip
, 1);
629 dump_pagetable(address
);
633 pgtable_bad(struct pt_regs
*regs
, unsigned long error_code
,
634 unsigned long address
)
636 struct task_struct
*tsk
;
640 flags
= oops_begin();
644 printk(KERN_ALERT
"%s: Corrupted page table at address %lx\n",
646 dump_pagetable(address
);
648 tsk
->thread
.cr2
= address
;
649 tsk
->thread
.trap_no
= 14;
650 tsk
->thread
.error_code
= error_code
;
652 if (__die("Bad pagetable", regs
, error_code
))
655 oops_end(flags
, regs
, sig
);
659 no_context(struct pt_regs
*regs
, unsigned long error_code
,
660 unsigned long address
)
662 struct task_struct
*tsk
= current
;
663 unsigned long *stackend
;
667 /* Are we prepared to handle this kernel fault? */
668 if (fixup_exception(regs
))
674 * Valid to do another page fault here, because if this fault
675 * had been triggered by is_prefetch fixup_exception would have
680 * Hall of shame of CPU/BIOS bugs.
682 if (is_prefetch(regs
, error_code
, address
))
685 if (is_errata93(regs
, address
))
689 * Oops. The kernel tried to access some bad page. We'll have to
690 * terminate things with extreme prejudice:
692 flags
= oops_begin();
694 show_fault_oops(regs
, error_code
, address
);
696 stackend
= end_of_stack(tsk
);
697 if (*stackend
!= STACK_END_MAGIC
)
698 printk(KERN_ALERT
"Thread overran stack, or stack corrupted\n");
700 tsk
->thread
.cr2
= address
;
701 tsk
->thread
.trap_no
= 14;
702 tsk
->thread
.error_code
= error_code
;
705 if (__die("Oops", regs
, error_code
))
708 /* Executive summary in case the body of the oops scrolled away */
709 printk(KERN_EMERG
"CR2: %016lx\n", address
);
711 oops_end(flags
, regs
, sig
);
715 * Print out info about fatal segfaults, if the show_unhandled_signals
719 show_signal_msg(struct pt_regs
*regs
, unsigned long error_code
,
720 unsigned long address
, struct task_struct
*tsk
)
722 if (!unhandled_signal(tsk
, SIGSEGV
))
725 if (!printk_ratelimit())
728 printk(KERN_CONT
"%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
729 task_pid_nr(tsk
) > 1 ? KERN_INFO
: KERN_EMERG
,
730 tsk
->comm
, task_pid_nr(tsk
), address
,
731 (void *)regs
->ip
, (void *)regs
->sp
, error_code
);
733 print_vma_addr(KERN_CONT
" in ", regs
->ip
);
735 printk(KERN_CONT
"\n");
739 __bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
740 unsigned long address
, int si_code
)
742 struct task_struct
*tsk
= current
;
744 /* User mode accesses just cause a SIGSEGV */
745 if (error_code
& PF_USER
) {
747 * It's possible to have interrupts off here:
752 * Valid to do another page fault here because this one came
755 if (is_prefetch(regs
, error_code
, address
))
758 if (is_errata100(regs
, address
))
761 if (unlikely(show_unhandled_signals
))
762 show_signal_msg(regs
, error_code
, address
, tsk
);
764 /* Kernel addresses are always protection faults: */
765 tsk
->thread
.cr2
= address
;
766 tsk
->thread
.error_code
= error_code
| (address
>= TASK_SIZE
);
767 tsk
->thread
.trap_no
= 14;
769 force_sig_info_fault(SIGSEGV
, si_code
, address
, tsk
);
774 if (is_f00f_bug(regs
, address
))
777 no_context(regs
, error_code
, address
);
781 bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
782 unsigned long address
)
784 __bad_area_nosemaphore(regs
, error_code
, address
, SEGV_MAPERR
);
788 __bad_area(struct pt_regs
*regs
, unsigned long error_code
,
789 unsigned long address
, int si_code
)
791 struct mm_struct
*mm
= current
->mm
;
794 * Something tried to access memory that isn't in our memory map..
795 * Fix it, but check if it's kernel or user first..
797 up_read(&mm
->mmap_sem
);
799 __bad_area_nosemaphore(regs
, error_code
, address
, si_code
);
803 bad_area(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
)
805 __bad_area(regs
, error_code
, address
, SEGV_MAPERR
);
809 bad_area_access_error(struct pt_regs
*regs
, unsigned long error_code
,
810 unsigned long address
)
812 __bad_area(regs
, error_code
, address
, SEGV_ACCERR
);
815 /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
817 out_of_memory(struct pt_regs
*regs
, unsigned long error_code
,
818 unsigned long address
)
821 * We ran out of memory, call the OOM killer, and return the userspace
822 * (which will retry the fault, or kill us if we got oom-killed):
824 up_read(¤t
->mm
->mmap_sem
);
826 pagefault_out_of_memory();
830 do_sigbus(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
)
832 struct task_struct
*tsk
= current
;
833 struct mm_struct
*mm
= tsk
->mm
;
835 up_read(&mm
->mmap_sem
);
837 /* Kernel mode? Handle exceptions or die: */
838 if (!(error_code
& PF_USER
))
839 no_context(regs
, error_code
, address
);
841 /* User-space => ok to do another page fault: */
842 if (is_prefetch(regs
, error_code
, address
))
845 tsk
->thread
.cr2
= address
;
846 tsk
->thread
.error_code
= error_code
;
847 tsk
->thread
.trap_no
= 14;
849 force_sig_info_fault(SIGBUS
, BUS_ADRERR
, address
, tsk
);
853 mm_fault_error(struct pt_regs
*regs
, unsigned long error_code
,
854 unsigned long address
, unsigned int fault
)
856 if (fault
& VM_FAULT_OOM
) {
857 out_of_memory(regs
, error_code
, address
);
859 if (fault
& VM_FAULT_SIGBUS
)
860 do_sigbus(regs
, error_code
, address
);
866 static int spurious_fault_check(unsigned long error_code
, pte_t
*pte
)
868 if ((error_code
& PF_WRITE
) && !pte_write(*pte
))
871 if ((error_code
& PF_INSTR
) && !pte_exec(*pte
))
878 * Handle a spurious fault caused by a stale TLB entry.
880 * This allows us to lazily refresh the TLB when increasing the
881 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
882 * eagerly is very expensive since that implies doing a full
883 * cross-processor TLB flush, even if no stale TLB entries exist
884 * on other processors.
886 * There are no security implications to leaving a stale TLB when
887 * increasing the permissions on a page.
890 spurious_fault(unsigned long error_code
, unsigned long address
)
898 /* Reserved-bit violation or user access to kernel space? */
899 if (error_code
& (PF_USER
| PF_RSVD
))
902 pgd
= init_mm
.pgd
+ pgd_index(address
);
903 if (!pgd_present(*pgd
))
906 pud
= pud_offset(pgd
, address
);
907 if (!pud_present(*pud
))
911 return spurious_fault_check(error_code
, (pte_t
*) pud
);
913 pmd
= pmd_offset(pud
, address
);
914 if (!pmd_present(*pmd
))
918 return spurious_fault_check(error_code
, (pte_t
*) pmd
);
920 pte
= pte_offset_kernel(pmd
, address
);
921 if (!pte_present(*pte
))
924 ret
= spurious_fault_check(error_code
, pte
);
929 * Make sure we have permissions in PMD.
930 * If not, then there's a bug in the page tables:
932 ret
= spurious_fault_check(error_code
, (pte_t
*) pmd
);
933 WARN_ONCE(!ret
, "PMD has incorrect permission bits\n");
938 int show_unhandled_signals
= 1;
941 access_error(unsigned long error_code
, int write
, struct vm_area_struct
*vma
)
944 /* write, present and write, not present: */
945 if (unlikely(!(vma
->vm_flags
& VM_WRITE
)))
951 if (unlikely(error_code
& PF_PROT
))
954 /* read, not present: */
955 if (unlikely(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
))))
961 static int fault_in_kernel_space(unsigned long address
)
963 return address
>= TASK_SIZE_MAX
;
967 * This routine handles page faults. It determines the address,
968 * and the problem, and then passes it off to one of the appropriate
971 dotraplinkage
void __kprobes
972 do_page_fault(struct pt_regs
*regs
, unsigned long error_code
)
974 struct vm_area_struct
*vma
;
975 struct task_struct
*tsk
;
976 unsigned long address
;
977 struct mm_struct
*mm
;
984 prefetchw(&mm
->mmap_sem
);
986 /* Get the faulting address: */
987 address
= read_cr2();
989 if (unlikely(kmmio_fault(regs
, address
)))
993 * We fault-in kernel-space virtual memory on-demand. The
994 * 'reference' page table is init_mm.pgd.
996 * NOTE! We MUST NOT take any locks for this case. We may
997 * be in an interrupt or a critical region, and should
998 * only copy the information from the master page table,
1001 * This verifies that the fault happens in kernel space
1002 * (error_code & 4) == 0, and that the fault was not a
1003 * protection error (error_code & 9) == 0.
1005 if (unlikely(fault_in_kernel_space(address
))) {
1006 if (!(error_code
& (PF_RSVD
|PF_USER
|PF_PROT
)) &&
1007 vmalloc_fault(address
) >= 0)
1010 /* Can handle a stale RO->RW TLB: */
1011 if (spurious_fault(error_code
, address
))
1014 /* kprobes don't want to hook the spurious faults: */
1015 if (notify_page_fault(regs
))
1018 * Don't take the mm semaphore here. If we fixup a prefetch
1019 * fault we could otherwise deadlock:
1021 bad_area_nosemaphore(regs
, error_code
, address
);
1026 /* kprobes don't want to hook the spurious faults: */
1027 if (unlikely(notify_page_fault(regs
)))
1030 * It's safe to allow irq's after cr2 has been saved and the
1031 * vmalloc fault has been handled.
1033 * User-mode registers count as a user access even for any
1034 * potential system fault or CPU buglet:
1036 if (user_mode_vm(regs
)) {
1038 error_code
|= PF_USER
;
1040 if (regs
->flags
& X86_EFLAGS_IF
)
1044 if (unlikely(error_code
& PF_RSVD
))
1045 pgtable_bad(regs
, error_code
, address
);
1048 * If we're in an interrupt, have no user context or are running
1049 * in an atomic region then we must not take the fault:
1051 if (unlikely(in_atomic() || !mm
)) {
1052 bad_area_nosemaphore(regs
, error_code
, address
);
1057 * When running in the kernel we expect faults to occur only to
1058 * addresses in user space. All other faults represent errors in
1059 * the kernel and should generate an OOPS. Unfortunately, in the
1060 * case of an erroneous fault occurring in a code path which already
1061 * holds mmap_sem we will deadlock attempting to validate the fault
1062 * against the address space. Luckily the kernel only validly
1063 * references user space from well defined areas of code, which are
1064 * listed in the exceptions table.
1066 * As the vast majority of faults will be valid we will only perform
1067 * the source reference check when there is a possibility of a
1068 * deadlock. Attempt to lock the address space, if we cannot we then
1069 * validate the source. If this is invalid we can skip the address
1070 * space check, thus avoiding the deadlock:
1072 if (unlikely(!down_read_trylock(&mm
->mmap_sem
))) {
1073 if ((error_code
& PF_USER
) == 0 &&
1074 !search_exception_tables(regs
->ip
)) {
1075 bad_area_nosemaphore(regs
, error_code
, address
);
1078 down_read(&mm
->mmap_sem
);
1081 * The above down_read_trylock() might have succeeded in
1082 * which case we'll have missed the might_sleep() from
1088 vma
= find_vma(mm
, address
);
1089 if (unlikely(!vma
)) {
1090 bad_area(regs
, error_code
, address
);
1093 if (likely(vma
->vm_start
<= address
))
1095 if (unlikely(!(vma
->vm_flags
& VM_GROWSDOWN
))) {
1096 bad_area(regs
, error_code
, address
);
1099 if (error_code
& PF_USER
) {
1101 * Accessing the stack below %sp is always a bug.
1102 * The large cushion allows instructions like enter
1103 * and pusha to work. ("enter $65535, $31" pushes
1104 * 32 pointers and then decrements %sp by 65535.)
1106 if (unlikely(address
+ 65536 + 32 * sizeof(unsigned long) < regs
->sp
)) {
1107 bad_area(regs
, error_code
, address
);
1111 if (unlikely(expand_stack(vma
, address
))) {
1112 bad_area(regs
, error_code
, address
);
1117 * Ok, we have a good vm_area for this memory access, so
1118 * we can handle it..
1121 write
= error_code
& PF_WRITE
;
1123 if (unlikely(access_error(error_code
, write
, vma
))) {
1124 bad_area_access_error(regs
, error_code
, address
);
1129 * If for any reason at all we couldn't handle the fault,
1130 * make sure we exit gracefully rather than endlessly redo
1133 fault
= handle_mm_fault(mm
, vma
, address
, write
);
1135 if (unlikely(fault
& VM_FAULT_ERROR
)) {
1136 mm_fault_error(regs
, error_code
, address
, fault
);
1140 if (fault
& VM_FAULT_MAJOR
)
1145 check_v8086_mode(regs
, address
, tsk
);
1147 up_read(&mm
->mmap_sem
);