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/magic.h> /* STACK_END_MAGIC */
7 #include <linux/sched.h> /* test_thread_flag(), ... */
8 #include <linux/kdebug.h> /* oops_begin/end, ... */
9 #include <linux/module.h> /* search_exception_table */
10 #include <linux/bootmem.h> /* max_low_pfn */
11 #include <linux/kprobes.h> /* __kprobes, ... */
12 #include <linux/mmiotrace.h> /* kmmio_handler, ... */
13 #include <linux/perf_counter.h> /* perf_swcounter_event */
15 #include <asm/traps.h> /* dotraplinkage, ... */
16 #include <asm/pgalloc.h> /* pgd_*(), ... */
19 * Page fault error code bits:
21 * bit 0 == 0: no page found 1: protection fault
22 * bit 1 == 0: read access 1: write access
23 * bit 2 == 0: kernel-mode access 1: user-mode access
24 * bit 3 == 1: use of reserved bit detected
25 * bit 4 == 1: fault was an instruction fetch
27 enum x86_pf_error_code
{
37 * Returns 0 if mmiotrace is disabled, or if the fault is not
38 * handled by mmiotrace:
40 static inline int kmmio_fault(struct pt_regs
*regs
, unsigned long addr
)
42 if (unlikely(is_kmmio_active()))
43 if (kmmio_handler(regs
, addr
) == 1)
48 static inline int notify_page_fault(struct pt_regs
*regs
)
52 /* kprobe_running() needs smp_processor_id() */
53 if (kprobes_built_in() && !user_mode_vm(regs
)) {
55 if (kprobe_running() && kprobe_fault_handler(regs
, 14))
68 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
69 * Check that here and ignore it.
73 * Sometimes the CPU reports invalid exceptions on prefetch.
74 * Check that here and ignore it.
76 * Opcode checker based on code by Richard Brunner.
79 check_prefetch_opcode(struct pt_regs
*regs
, unsigned char *instr
,
80 unsigned char opcode
, int *prefetch
)
82 unsigned char instr_hi
= opcode
& 0xf0;
83 unsigned char instr_lo
= opcode
& 0x0f;
89 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
90 * In X86_64 long mode, the CPU will signal invalid
91 * opcode if some of these prefixes are present so
92 * X86_64 will never get here anyway
94 return ((instr_lo
& 7) == 0x6);
98 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
99 * Need to figure out under what instruction mode the
100 * instruction was issued. Could check the LDT for lm,
101 * but for now it's good enough to assume that long
102 * mode only uses well known segments or kernel.
104 return (!user_mode(regs
)) || (regs
->cs
== __USER_CS
);
107 /* 0x64 thru 0x67 are valid prefixes in all modes. */
108 return (instr_lo
& 0xC) == 0x4;
110 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
111 return !instr_lo
|| (instr_lo
>>1) == 1;
113 /* Prefetch instruction is 0x0F0D or 0x0F18 */
114 if (probe_kernel_address(instr
, opcode
))
117 *prefetch
= (instr_lo
== 0xF) &&
118 (opcode
== 0x0D || opcode
== 0x18);
126 is_prefetch(struct pt_regs
*regs
, unsigned long error_code
, unsigned long addr
)
128 unsigned char *max_instr
;
129 unsigned char *instr
;
133 * If it was a exec (instruction fetch) fault on NX page, then
134 * do not ignore the fault:
136 if (error_code
& PF_INSTR
)
139 instr
= (void *)convert_ip_to_linear(current
, regs
);
140 max_instr
= instr
+ 15;
142 if (user_mode(regs
) && instr
>= (unsigned char *)TASK_SIZE
)
145 while (instr
< max_instr
) {
146 unsigned char opcode
;
148 if (probe_kernel_address(instr
, opcode
))
153 if (!check_prefetch_opcode(regs
, instr
, opcode
, &prefetch
))
160 force_sig_info_fault(int si_signo
, int si_code
, unsigned long address
,
161 struct task_struct
*tsk
)
165 info
.si_signo
= si_signo
;
167 info
.si_code
= si_code
;
168 info
.si_addr
= (void __user
*)address
;
170 force_sig_info(si_signo
, &info
, tsk
);
173 DEFINE_SPINLOCK(pgd_lock
);
177 static inline pmd_t
*vmalloc_sync_one(pgd_t
*pgd
, unsigned long address
)
179 unsigned index
= pgd_index(address
);
185 pgd_k
= init_mm
.pgd
+ index
;
187 if (!pgd_present(*pgd_k
))
191 * set_pgd(pgd, *pgd_k); here would be useless on PAE
192 * and redundant with the set_pmd() on non-PAE. As would
195 pud
= pud_offset(pgd
, address
);
196 pud_k
= pud_offset(pgd_k
, address
);
197 if (!pud_present(*pud_k
))
200 pmd
= pmd_offset(pud
, address
);
201 pmd_k
= pmd_offset(pud_k
, address
);
202 if (!pmd_present(*pmd_k
))
205 if (!pmd_present(*pmd
))
206 set_pmd(pmd
, *pmd_k
);
208 BUG_ON(pmd_page(*pmd
) != pmd_page(*pmd_k
));
213 void vmalloc_sync_all(void)
215 unsigned long address
;
217 if (SHARED_KERNEL_PMD
)
220 for (address
= VMALLOC_START
& PMD_MASK
;
221 address
>= TASK_SIZE
&& address
< FIXADDR_TOP
;
222 address
+= PMD_SIZE
) {
227 spin_lock_irqsave(&pgd_lock
, flags
);
228 list_for_each_entry(page
, &pgd_list
, lru
) {
229 if (!vmalloc_sync_one(page_address(page
), address
))
232 spin_unlock_irqrestore(&pgd_lock
, flags
);
239 * Handle a fault on the vmalloc or module mapping area
241 static noinline
int vmalloc_fault(unsigned long address
)
243 unsigned long pgd_paddr
;
247 /* Make sure we are in vmalloc area: */
248 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
252 * Synchronize this task's top level page-table
253 * with the 'reference' page table.
255 * Do _not_ use "current" here. We might be inside
256 * an interrupt in the middle of a task switch..
258 pgd_paddr
= read_cr3();
259 pmd_k
= vmalloc_sync_one(__va(pgd_paddr
), address
);
263 pte_k
= pte_offset_kernel(pmd_k
, address
);
264 if (!pte_present(*pte_k
))
271 * Did it hit the DOS screen memory VA from vm86 mode?
274 check_v8086_mode(struct pt_regs
*regs
, unsigned long address
,
275 struct task_struct
*tsk
)
279 if (!v8086_mode(regs
))
282 bit
= (address
- 0xA0000) >> PAGE_SHIFT
;
284 tsk
->thread
.screen_bitmap
|= 1 << bit
;
287 static void dump_pagetable(unsigned long address
)
289 __typeof__(pte_val(__pte(0))) page
;
292 page
= ((__typeof__(page
) *) __va(page
))[address
>> PGDIR_SHIFT
];
294 #ifdef CONFIG_X86_PAE
295 printk("*pdpt = %016Lx ", page
);
296 if ((page
>> PAGE_SHIFT
) < max_low_pfn
297 && page
& _PAGE_PRESENT
) {
299 page
= ((__typeof__(page
) *) __va(page
))[(address
>> PMD_SHIFT
)
300 & (PTRS_PER_PMD
- 1)];
301 printk(KERN_CONT
"*pde = %016Lx ", page
);
305 printk("*pde = %08lx ", page
);
309 * We must not directly access the pte in the highpte
310 * case if the page table is located in highmem.
311 * And let's rather not kmap-atomic the pte, just in case
312 * it's allocated already:
314 if ((page
>> PAGE_SHIFT
) < max_low_pfn
315 && (page
& _PAGE_PRESENT
)
316 && !(page
& _PAGE_PSE
)) {
319 page
= ((__typeof__(page
) *) __va(page
))[(address
>> PAGE_SHIFT
)
320 & (PTRS_PER_PTE
- 1)];
321 printk("*pte = %0*Lx ", sizeof(page
)*2, (u64
)page
);
327 #else /* CONFIG_X86_64: */
329 void vmalloc_sync_all(void)
331 unsigned long address
;
333 for (address
= VMALLOC_START
& PGDIR_MASK
; address
<= VMALLOC_END
;
334 address
+= PGDIR_SIZE
) {
336 const pgd_t
*pgd_ref
= pgd_offset_k(address
);
340 if (pgd_none(*pgd_ref
))
343 spin_lock_irqsave(&pgd_lock
, flags
);
344 list_for_each_entry(page
, &pgd_list
, lru
) {
346 pgd
= (pgd_t
*)page_address(page
) + pgd_index(address
);
348 set_pgd(pgd
, *pgd_ref
);
350 BUG_ON(pgd_page_vaddr(*pgd
) != pgd_page_vaddr(*pgd_ref
));
352 spin_unlock_irqrestore(&pgd_lock
, flags
);
359 * Handle a fault on the vmalloc area
361 * This assumes no large pages in there.
363 static noinline
int vmalloc_fault(unsigned long address
)
365 pgd_t
*pgd
, *pgd_ref
;
366 pud_t
*pud
, *pud_ref
;
367 pmd_t
*pmd
, *pmd_ref
;
368 pte_t
*pte
, *pte_ref
;
370 /* Make sure we are in vmalloc area: */
371 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
375 * Copy kernel mappings over when needed. This can also
376 * happen within a race in page table update. In the later
379 pgd
= pgd_offset(current
->active_mm
, address
);
380 pgd_ref
= pgd_offset_k(address
);
381 if (pgd_none(*pgd_ref
))
385 set_pgd(pgd
, *pgd_ref
);
387 BUG_ON(pgd_page_vaddr(*pgd
) != pgd_page_vaddr(*pgd_ref
));
390 * Below here mismatches are bugs because these lower tables
394 pud
= pud_offset(pgd
, address
);
395 pud_ref
= pud_offset(pgd_ref
, address
);
396 if (pud_none(*pud_ref
))
399 if (pud_none(*pud
) || pud_page_vaddr(*pud
) != pud_page_vaddr(*pud_ref
))
402 pmd
= pmd_offset(pud
, address
);
403 pmd_ref
= pmd_offset(pud_ref
, address
);
404 if (pmd_none(*pmd_ref
))
407 if (pmd_none(*pmd
) || pmd_page(*pmd
) != pmd_page(*pmd_ref
))
410 pte_ref
= pte_offset_kernel(pmd_ref
, address
);
411 if (!pte_present(*pte_ref
))
414 pte
= pte_offset_kernel(pmd
, address
);
417 * Don't use pte_page here, because the mappings can point
418 * outside mem_map, and the NUMA hash lookup cannot handle
421 if (!pte_present(*pte
) || pte_pfn(*pte
) != pte_pfn(*pte_ref
))
427 static const char errata93_warning
[] =
428 KERN_ERR
"******* Your BIOS seems to not contain a fix for K8 errata #93\n"
429 KERN_ERR
"******* Working around it, but it may cause SEGVs or burn power.\n"
430 KERN_ERR
"******* Please consider a BIOS update.\n"
431 KERN_ERR
"******* Disabling USB legacy in the BIOS may also help.\n";
434 * No vm86 mode in 64-bit mode:
437 check_v8086_mode(struct pt_regs
*regs
, unsigned long address
,
438 struct task_struct
*tsk
)
442 static int bad_address(void *p
)
446 return probe_kernel_address((unsigned long *)p
, dummy
);
449 static void dump_pagetable(unsigned long address
)
456 pgd
= (pgd_t
*)read_cr3();
458 pgd
= __va((unsigned long)pgd
& PHYSICAL_PAGE_MASK
);
460 pgd
+= pgd_index(address
);
461 if (bad_address(pgd
))
464 printk("PGD %lx ", pgd_val(*pgd
));
466 if (!pgd_present(*pgd
))
469 pud
= pud_offset(pgd
, address
);
470 if (bad_address(pud
))
473 printk("PUD %lx ", pud_val(*pud
));
474 if (!pud_present(*pud
) || pud_large(*pud
))
477 pmd
= pmd_offset(pud
, address
);
478 if (bad_address(pmd
))
481 printk("PMD %lx ", pmd_val(*pmd
));
482 if (!pmd_present(*pmd
) || pmd_large(*pmd
))
485 pte
= pte_offset_kernel(pmd
, address
);
486 if (bad_address(pte
))
489 printk("PTE %lx", pte_val(*pte
));
497 #endif /* CONFIG_X86_64 */
500 * Workaround for K8 erratum #93 & buggy BIOS.
502 * BIOS SMM functions are required to use a specific workaround
503 * to avoid corruption of the 64bit RIP register on C stepping K8.
505 * A lot of BIOS that didn't get tested properly miss this.
507 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
508 * Try to work around it here.
510 * Note we only handle faults in kernel here.
511 * Does nothing on 32-bit.
513 static int is_errata93(struct pt_regs
*regs
, unsigned long address
)
516 if (address
!= regs
->ip
)
519 if ((address
>> 32) != 0)
522 address
|= 0xffffffffUL
<< 32;
523 if ((address
>= (u64
)_stext
&& address
<= (u64
)_etext
) ||
524 (address
>= MODULES_VADDR
&& address
<= MODULES_END
)) {
525 printk_once(errata93_warning
);
534 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
535 * to illegal addresses >4GB.
537 * We catch this in the page fault handler because these addresses
538 * are not reachable. Just detect this case and return. Any code
539 * segment in LDT is compatibility mode.
541 static int is_errata100(struct pt_regs
*regs
, unsigned long address
)
544 if ((regs
->cs
== __USER32_CS
|| (regs
->cs
& (1<<2))) && (address
>> 32))
550 static int is_f00f_bug(struct pt_regs
*regs
, unsigned long address
)
552 #ifdef CONFIG_X86_F00F_BUG
556 * Pentium F0 0F C7 C8 bug workaround:
558 if (boot_cpu_data
.f00f_bug
) {
559 nr
= (address
- idt_descr
.address
) >> 3;
562 do_invalid_op(regs
, 0);
570 static const char nx_warning
[] = KERN_CRIT
571 "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
574 show_fault_oops(struct pt_regs
*regs
, unsigned long error_code
,
575 unsigned long address
)
577 if (!oops_may_print())
580 if (error_code
& PF_INSTR
) {
583 pte_t
*pte
= lookup_address(address
, &level
);
585 if (pte
&& pte_present(*pte
) && !pte_exec(*pte
))
586 printk(nx_warning
, current_uid());
589 printk(KERN_ALERT
"BUG: unable to handle kernel ");
590 if (address
< PAGE_SIZE
)
591 printk(KERN_CONT
"NULL pointer dereference");
593 printk(KERN_CONT
"paging request");
595 printk(KERN_CONT
" at %p\n", (void *) address
);
596 printk(KERN_ALERT
"IP:");
597 printk_address(regs
->ip
, 1);
599 dump_pagetable(address
);
603 pgtable_bad(struct pt_regs
*regs
, unsigned long error_code
,
604 unsigned long address
)
606 struct task_struct
*tsk
;
610 flags
= oops_begin();
614 printk(KERN_ALERT
"%s: Corrupted page table at address %lx\n",
616 dump_pagetable(address
);
618 tsk
->thread
.cr2
= address
;
619 tsk
->thread
.trap_no
= 14;
620 tsk
->thread
.error_code
= error_code
;
622 if (__die("Bad pagetable", regs
, error_code
))
625 oops_end(flags
, regs
, sig
);
629 no_context(struct pt_regs
*regs
, unsigned long error_code
,
630 unsigned long address
)
632 struct task_struct
*tsk
= current
;
633 unsigned long *stackend
;
637 /* Are we prepared to handle this kernel fault? */
638 if (fixup_exception(regs
))
644 * Valid to do another page fault here, because if this fault
645 * had been triggered by is_prefetch fixup_exception would have
650 * Hall of shame of CPU/BIOS bugs.
652 if (is_prefetch(regs
, error_code
, address
))
655 if (is_errata93(regs
, address
))
659 * Oops. The kernel tried to access some bad page. We'll have to
660 * terminate things with extreme prejudice:
662 flags
= oops_begin();
664 show_fault_oops(regs
, error_code
, address
);
666 stackend
= end_of_stack(tsk
);
667 if (*stackend
!= STACK_END_MAGIC
)
668 printk(KERN_ALERT
"Thread overran stack, or stack corrupted\n");
670 tsk
->thread
.cr2
= address
;
671 tsk
->thread
.trap_no
= 14;
672 tsk
->thread
.error_code
= error_code
;
675 if (__die("Oops", regs
, error_code
))
678 /* Executive summary in case the body of the oops scrolled away */
679 printk(KERN_EMERG
"CR2: %016lx\n", address
);
681 oops_end(flags
, regs
, sig
);
685 * Print out info about fatal segfaults, if the show_unhandled_signals
689 show_signal_msg(struct pt_regs
*regs
, unsigned long error_code
,
690 unsigned long address
, struct task_struct
*tsk
)
692 if (!unhandled_signal(tsk
, SIGSEGV
))
695 if (!printk_ratelimit())
698 printk(KERN_CONT
"%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
699 task_pid_nr(tsk
) > 1 ? KERN_INFO
: KERN_EMERG
,
700 tsk
->comm
, task_pid_nr(tsk
), address
,
701 (void *)regs
->ip
, (void *)regs
->sp
, error_code
);
703 print_vma_addr(KERN_CONT
" in ", regs
->ip
);
705 printk(KERN_CONT
"\n");
709 __bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
710 unsigned long address
, int si_code
)
712 struct task_struct
*tsk
= current
;
714 /* User mode accesses just cause a SIGSEGV */
715 if (error_code
& PF_USER
) {
717 * It's possible to have interrupts off here:
722 * Valid to do another page fault here because this one came
725 if (is_prefetch(regs
, error_code
, address
))
728 if (is_errata100(regs
, address
))
731 if (unlikely(show_unhandled_signals
))
732 show_signal_msg(regs
, error_code
, address
, tsk
);
734 /* Kernel addresses are always protection faults: */
735 tsk
->thread
.cr2
= address
;
736 tsk
->thread
.error_code
= error_code
| (address
>= TASK_SIZE
);
737 tsk
->thread
.trap_no
= 14;
739 force_sig_info_fault(SIGSEGV
, si_code
, address
, tsk
);
744 if (is_f00f_bug(regs
, address
))
747 no_context(regs
, error_code
, address
);
751 bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
752 unsigned long address
)
754 __bad_area_nosemaphore(regs
, error_code
, address
, SEGV_MAPERR
);
758 __bad_area(struct pt_regs
*regs
, unsigned long error_code
,
759 unsigned long address
, int si_code
)
761 struct mm_struct
*mm
= current
->mm
;
764 * Something tried to access memory that isn't in our memory map..
765 * Fix it, but check if it's kernel or user first..
767 up_read(&mm
->mmap_sem
);
769 __bad_area_nosemaphore(regs
, error_code
, address
, si_code
);
773 bad_area(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
)
775 __bad_area(regs
, error_code
, address
, SEGV_MAPERR
);
779 bad_area_access_error(struct pt_regs
*regs
, unsigned long error_code
,
780 unsigned long address
)
782 __bad_area(regs
, error_code
, address
, SEGV_ACCERR
);
785 /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
787 out_of_memory(struct pt_regs
*regs
, unsigned long error_code
,
788 unsigned long address
)
791 * We ran out of memory, call the OOM killer, and return the userspace
792 * (which will retry the fault, or kill us if we got oom-killed):
794 up_read(¤t
->mm
->mmap_sem
);
796 pagefault_out_of_memory();
800 do_sigbus(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
)
802 struct task_struct
*tsk
= current
;
803 struct mm_struct
*mm
= tsk
->mm
;
805 up_read(&mm
->mmap_sem
);
807 /* Kernel mode? Handle exceptions or die: */
808 if (!(error_code
& PF_USER
))
809 no_context(regs
, error_code
, address
);
811 /* User-space => ok to do another page fault: */
812 if (is_prefetch(regs
, error_code
, address
))
815 tsk
->thread
.cr2
= address
;
816 tsk
->thread
.error_code
= error_code
;
817 tsk
->thread
.trap_no
= 14;
819 force_sig_info_fault(SIGBUS
, BUS_ADRERR
, address
, tsk
);
823 mm_fault_error(struct pt_regs
*regs
, unsigned long error_code
,
824 unsigned long address
, unsigned int fault
)
826 if (fault
& VM_FAULT_OOM
) {
827 out_of_memory(regs
, error_code
, address
);
829 if (fault
& VM_FAULT_SIGBUS
)
830 do_sigbus(regs
, error_code
, address
);
836 static int spurious_fault_check(unsigned long error_code
, pte_t
*pte
)
838 if ((error_code
& PF_WRITE
) && !pte_write(*pte
))
841 if ((error_code
& PF_INSTR
) && !pte_exec(*pte
))
848 * Handle a spurious fault caused by a stale TLB entry.
850 * This allows us to lazily refresh the TLB when increasing the
851 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
852 * eagerly is very expensive since that implies doing a full
853 * cross-processor TLB flush, even if no stale TLB entries exist
854 * on other processors.
856 * There are no security implications to leaving a stale TLB when
857 * increasing the permissions on a page.
860 spurious_fault(unsigned long error_code
, unsigned long address
)
868 /* Reserved-bit violation or user access to kernel space? */
869 if (error_code
& (PF_USER
| PF_RSVD
))
872 pgd
= init_mm
.pgd
+ pgd_index(address
);
873 if (!pgd_present(*pgd
))
876 pud
= pud_offset(pgd
, address
);
877 if (!pud_present(*pud
))
881 return spurious_fault_check(error_code
, (pte_t
*) pud
);
883 pmd
= pmd_offset(pud
, address
);
884 if (!pmd_present(*pmd
))
888 return spurious_fault_check(error_code
, (pte_t
*) pmd
);
890 pte
= pte_offset_kernel(pmd
, address
);
891 if (!pte_present(*pte
))
894 ret
= spurious_fault_check(error_code
, pte
);
899 * Make sure we have permissions in PMD.
900 * If not, then there's a bug in the page tables:
902 ret
= spurious_fault_check(error_code
, (pte_t
*) pmd
);
903 WARN_ONCE(!ret
, "PMD has incorrect permission bits\n");
908 int show_unhandled_signals
= 1;
911 access_error(unsigned long error_code
, int write
, struct vm_area_struct
*vma
)
914 /* write, present and write, not present: */
915 if (unlikely(!(vma
->vm_flags
& VM_WRITE
)))
921 if (unlikely(error_code
& PF_PROT
))
924 /* read, not present: */
925 if (unlikely(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
))))
931 static int fault_in_kernel_space(unsigned long address
)
933 return address
>= TASK_SIZE_MAX
;
937 * This routine handles page faults. It determines the address,
938 * and the problem, and then passes it off to one of the appropriate
941 dotraplinkage
void __kprobes
942 do_page_fault(struct pt_regs
*regs
, unsigned long error_code
)
944 struct vm_area_struct
*vma
;
945 struct task_struct
*tsk
;
946 unsigned long address
;
947 struct mm_struct
*mm
;
954 prefetchw(&mm
->mmap_sem
);
956 /* Get the faulting address: */
957 address
= read_cr2();
959 if (unlikely(kmmio_fault(regs
, address
)))
963 * We fault-in kernel-space virtual memory on-demand. The
964 * 'reference' page table is init_mm.pgd.
966 * NOTE! We MUST NOT take any locks for this case. We may
967 * be in an interrupt or a critical region, and should
968 * only copy the information from the master page table,
971 * This verifies that the fault happens in kernel space
972 * (error_code & 4) == 0, and that the fault was not a
973 * protection error (error_code & 9) == 0.
975 if (unlikely(fault_in_kernel_space(address
))) {
976 if (!(error_code
& (PF_RSVD
|PF_USER
|PF_PROT
)) &&
977 vmalloc_fault(address
) >= 0)
980 /* Can handle a stale RO->RW TLB: */
981 if (spurious_fault(error_code
, address
))
984 /* kprobes don't want to hook the spurious faults: */
985 if (notify_page_fault(regs
))
988 * Don't take the mm semaphore here. If we fixup a prefetch
989 * fault we could otherwise deadlock:
991 bad_area_nosemaphore(regs
, error_code
, address
);
996 /* kprobes don't want to hook the spurious faults: */
997 if (unlikely(notify_page_fault(regs
)))
1000 * It's safe to allow irq's after cr2 has been saved and the
1001 * vmalloc fault has been handled.
1003 * User-mode registers count as a user access even for any
1004 * potential system fault or CPU buglet:
1006 if (user_mode_vm(regs
)) {
1008 error_code
|= PF_USER
;
1010 if (regs
->flags
& X86_EFLAGS_IF
)
1014 if (unlikely(error_code
& PF_RSVD
))
1015 pgtable_bad(regs
, error_code
, address
);
1017 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS
, 1, 0, regs
, address
);
1020 * If we're in an interrupt, have no user context or are running
1021 * in an atomic region then we must not take the fault:
1023 if (unlikely(in_atomic() || !mm
)) {
1024 bad_area_nosemaphore(regs
, error_code
, address
);
1029 * When running in the kernel we expect faults to occur only to
1030 * addresses in user space. All other faults represent errors in
1031 * the kernel and should generate an OOPS. Unfortunately, in the
1032 * case of an erroneous fault occurring in a code path which already
1033 * holds mmap_sem we will deadlock attempting to validate the fault
1034 * against the address space. Luckily the kernel only validly
1035 * references user space from well defined areas of code, which are
1036 * listed in the exceptions table.
1038 * As the vast majority of faults will be valid we will only perform
1039 * the source reference check when there is a possibility of a
1040 * deadlock. Attempt to lock the address space, if we cannot we then
1041 * validate the source. If this is invalid we can skip the address
1042 * space check, thus avoiding the deadlock:
1044 if (unlikely(!down_read_trylock(&mm
->mmap_sem
))) {
1045 if ((error_code
& PF_USER
) == 0 &&
1046 !search_exception_tables(regs
->ip
)) {
1047 bad_area_nosemaphore(regs
, error_code
, address
);
1050 down_read(&mm
->mmap_sem
);
1053 * The above down_read_trylock() might have succeeded in
1054 * which case we'll have missed the might_sleep() from
1060 vma
= find_vma(mm
, address
);
1061 if (unlikely(!vma
)) {
1062 bad_area(regs
, error_code
, address
);
1065 if (likely(vma
->vm_start
<= address
))
1067 if (unlikely(!(vma
->vm_flags
& VM_GROWSDOWN
))) {
1068 bad_area(regs
, error_code
, address
);
1071 if (error_code
& PF_USER
) {
1073 * Accessing the stack below %sp is always a bug.
1074 * The large cushion allows instructions like enter
1075 * and pusha to work. ("enter $65535, $31" pushes
1076 * 32 pointers and then decrements %sp by 65535.)
1078 if (unlikely(address
+ 65536 + 32 * sizeof(unsigned long) < regs
->sp
)) {
1079 bad_area(regs
, error_code
, address
);
1083 if (unlikely(expand_stack(vma
, address
))) {
1084 bad_area(regs
, error_code
, address
);
1089 * Ok, we have a good vm_area for this memory access, so
1090 * we can handle it..
1093 write
= error_code
& PF_WRITE
;
1095 if (unlikely(access_error(error_code
, write
, vma
))) {
1096 bad_area_access_error(regs
, error_code
, address
);
1101 * If for any reason at all we couldn't handle the fault,
1102 * make sure we exit gracefully rather than endlessly redo
1105 fault
= handle_mm_fault(mm
, vma
, address
, write
);
1107 if (unlikely(fault
& VM_FAULT_ERROR
)) {
1108 mm_fault_error(regs
, error_code
, address
, fault
);
1112 if (fault
& VM_FAULT_MAJOR
) {
1114 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ
, 1, 0,
1118 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MIN
, 1, 0,
1122 check_v8086_mode(regs
, address
, tsk
);
1124 up_read(&mm
->mmap_sem
);