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_event.h> /* perf_sw_event */
14 #include <linux/hugetlb.h> /* hstate_index_to_shift */
15 #include <linux/prefetch.h> /* prefetchw */
17 #include <asm/traps.h> /* dotraplinkage, ... */
18 #include <asm/pgalloc.h> /* pgd_*(), ... */
19 #include <asm/kmemcheck.h> /* kmemcheck_*(), ... */
20 #include <asm/fixmap.h> /* VSYSCALL_START */
21 #include <asm/context_tracking.h> /* exception_enter(), ... */
24 * Page fault error code bits:
26 * bit 0 == 0: no page found 1: protection fault
27 * bit 1 == 0: read access 1: write access
28 * bit 2 == 0: kernel-mode access 1: user-mode access
29 * bit 3 == 1: use of reserved bit detected
30 * bit 4 == 1: fault was an instruction fetch
32 enum x86_pf_error_code
{
42 * Returns 0 if mmiotrace is disabled, or if the fault is not
43 * handled by mmiotrace:
45 static inline int __kprobes
46 kmmio_fault(struct pt_regs
*regs
, unsigned long addr
)
48 if (unlikely(is_kmmio_active()))
49 if (kmmio_handler(regs
, addr
) == 1)
54 static inline int __kprobes
notify_page_fault(struct pt_regs
*regs
)
58 /* kprobe_running() needs smp_processor_id() */
59 if (kprobes_built_in() && !user_mode_vm(regs
)) {
61 if (kprobe_running() && kprobe_fault_handler(regs
, 14))
74 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
75 * Check that here and ignore it.
79 * Sometimes the CPU reports invalid exceptions on prefetch.
80 * Check that here and ignore it.
82 * Opcode checker based on code by Richard Brunner.
85 check_prefetch_opcode(struct pt_regs
*regs
, unsigned char *instr
,
86 unsigned char opcode
, int *prefetch
)
88 unsigned char instr_hi
= opcode
& 0xf0;
89 unsigned char instr_lo
= opcode
& 0x0f;
95 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
96 * In X86_64 long mode, the CPU will signal invalid
97 * opcode if some of these prefixes are present so
98 * X86_64 will never get here anyway
100 return ((instr_lo
& 7) == 0x6);
104 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
105 * Need to figure out under what instruction mode the
106 * instruction was issued. Could check the LDT for lm,
107 * but for now it's good enough to assume that long
108 * mode only uses well known segments or kernel.
110 return (!user_mode(regs
) || user_64bit_mode(regs
));
113 /* 0x64 thru 0x67 are valid prefixes in all modes. */
114 return (instr_lo
& 0xC) == 0x4;
116 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
117 return !instr_lo
|| (instr_lo
>>1) == 1;
119 /* Prefetch instruction is 0x0F0D or 0x0F18 */
120 if (probe_kernel_address(instr
, opcode
))
123 *prefetch
= (instr_lo
== 0xF) &&
124 (opcode
== 0x0D || opcode
== 0x18);
132 is_prefetch(struct pt_regs
*regs
, unsigned long error_code
, unsigned long addr
)
134 unsigned char *max_instr
;
135 unsigned char *instr
;
139 * If it was a exec (instruction fetch) fault on NX page, then
140 * do not ignore the fault:
142 if (error_code
& PF_INSTR
)
145 instr
= (void *)convert_ip_to_linear(current
, regs
);
146 max_instr
= instr
+ 15;
148 if (user_mode(regs
) && instr
>= (unsigned char *)TASK_SIZE
)
151 while (instr
< max_instr
) {
152 unsigned char opcode
;
154 if (probe_kernel_address(instr
, opcode
))
159 if (!check_prefetch_opcode(regs
, instr
, opcode
, &prefetch
))
166 force_sig_info_fault(int si_signo
, int si_code
, unsigned long address
,
167 struct task_struct
*tsk
, int fault
)
172 info
.si_signo
= si_signo
;
174 info
.si_code
= si_code
;
175 info
.si_addr
= (void __user
*)address
;
176 if (fault
& VM_FAULT_HWPOISON_LARGE
)
177 lsb
= hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault
));
178 if (fault
& VM_FAULT_HWPOISON
)
180 info
.si_addr_lsb
= lsb
;
182 force_sig_info(si_signo
, &info
, tsk
);
185 DEFINE_SPINLOCK(pgd_lock
);
189 static inline pmd_t
*vmalloc_sync_one(pgd_t
*pgd
, unsigned long address
)
191 unsigned index
= pgd_index(address
);
197 pgd_k
= init_mm
.pgd
+ index
;
199 if (!pgd_present(*pgd_k
))
203 * set_pgd(pgd, *pgd_k); here would be useless on PAE
204 * and redundant with the set_pmd() on non-PAE. As would
207 pud
= pud_offset(pgd
, address
);
208 pud_k
= pud_offset(pgd_k
, address
);
209 if (!pud_present(*pud_k
))
212 pmd
= pmd_offset(pud
, address
);
213 pmd_k
= pmd_offset(pud_k
, address
);
214 if (!pmd_present(*pmd_k
))
217 if (!pmd_present(*pmd
))
218 set_pmd(pmd
, *pmd_k
);
220 BUG_ON(pmd_page(*pmd
) != pmd_page(*pmd_k
));
225 void vmalloc_sync_all(void)
227 unsigned long address
;
229 if (SHARED_KERNEL_PMD
)
232 for (address
= VMALLOC_START
& PMD_MASK
;
233 address
>= TASK_SIZE
&& address
< FIXADDR_TOP
;
234 address
+= PMD_SIZE
) {
237 spin_lock(&pgd_lock
);
238 list_for_each_entry(page
, &pgd_list
, lru
) {
239 spinlock_t
*pgt_lock
;
242 /* the pgt_lock only for Xen */
243 pgt_lock
= &pgd_page_get_mm(page
)->page_table_lock
;
246 ret
= vmalloc_sync_one(page_address(page
), address
);
247 spin_unlock(pgt_lock
);
252 spin_unlock(&pgd_lock
);
259 * Handle a fault on the vmalloc or module mapping area
261 static noinline __kprobes
int vmalloc_fault(unsigned long address
)
263 unsigned long pgd_paddr
;
267 /* Make sure we are in vmalloc area: */
268 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
271 WARN_ON_ONCE(in_nmi());
274 * Synchronize this task's top level page-table
275 * with the 'reference' page table.
277 * Do _not_ use "current" here. We might be inside
278 * an interrupt in the middle of a task switch..
280 pgd_paddr
= read_cr3();
281 pmd_k
= vmalloc_sync_one(__va(pgd_paddr
), address
);
285 pte_k
= pte_offset_kernel(pmd_k
, address
);
286 if (!pte_present(*pte_k
))
293 * Did it hit the DOS screen memory VA from vm86 mode?
296 check_v8086_mode(struct pt_regs
*regs
, unsigned long address
,
297 struct task_struct
*tsk
)
301 if (!v8086_mode(regs
))
304 bit
= (address
- 0xA0000) >> PAGE_SHIFT
;
306 tsk
->thread
.screen_bitmap
|= 1 << bit
;
309 static bool low_pfn(unsigned long pfn
)
311 return pfn
< max_low_pfn
;
314 static void dump_pagetable(unsigned long address
)
316 pgd_t
*base
= __va(read_cr3());
317 pgd_t
*pgd
= &base
[pgd_index(address
)];
321 #ifdef CONFIG_X86_PAE
322 printk("*pdpt = %016Lx ", pgd_val(*pgd
));
323 if (!low_pfn(pgd_val(*pgd
) >> PAGE_SHIFT
) || !pgd_present(*pgd
))
326 pmd
= pmd_offset(pud_offset(pgd
, address
), address
);
327 printk(KERN_CONT
"*pde = %0*Lx ", sizeof(*pmd
) * 2, (u64
)pmd_val(*pmd
));
330 * We must not directly access the pte in the highpte
331 * case if the page table is located in highmem.
332 * And let's rather not kmap-atomic the pte, just in case
333 * it's allocated already:
335 if (!low_pfn(pmd_pfn(*pmd
)) || !pmd_present(*pmd
) || pmd_large(*pmd
))
338 pte
= pte_offset_kernel(pmd
, address
);
339 printk("*pte = %0*Lx ", sizeof(*pte
) * 2, (u64
)pte_val(*pte
));
344 #else /* CONFIG_X86_64: */
346 void vmalloc_sync_all(void)
348 sync_global_pgds(VMALLOC_START
& PGDIR_MASK
, VMALLOC_END
);
354 * Handle a fault on the vmalloc area
356 * This assumes no large pages in there.
358 static noinline __kprobes
int vmalloc_fault(unsigned long address
)
360 pgd_t
*pgd
, *pgd_ref
;
361 pud_t
*pud
, *pud_ref
;
362 pmd_t
*pmd
, *pmd_ref
;
363 pte_t
*pte
, *pte_ref
;
365 /* Make sure we are in vmalloc area: */
366 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
369 WARN_ON_ONCE(in_nmi());
372 * Copy kernel mappings over when needed. This can also
373 * happen within a race in page table update. In the later
376 pgd
= pgd_offset(current
->active_mm
, address
);
377 pgd_ref
= pgd_offset_k(address
);
378 if (pgd_none(*pgd_ref
))
382 set_pgd(pgd
, *pgd_ref
);
384 BUG_ON(pgd_page_vaddr(*pgd
) != pgd_page_vaddr(*pgd_ref
));
387 * Below here mismatches are bugs because these lower tables
391 pud
= pud_offset(pgd
, address
);
392 pud_ref
= pud_offset(pgd_ref
, address
);
393 if (pud_none(*pud_ref
))
396 if (pud_none(*pud
) || pud_page_vaddr(*pud
) != pud_page_vaddr(*pud_ref
))
399 pmd
= pmd_offset(pud
, address
);
400 pmd_ref
= pmd_offset(pud_ref
, address
);
401 if (pmd_none(*pmd_ref
))
404 if (pmd_none(*pmd
) || pmd_page(*pmd
) != pmd_page(*pmd_ref
))
407 pte_ref
= pte_offset_kernel(pmd_ref
, address
);
408 if (!pte_present(*pte_ref
))
411 pte
= pte_offset_kernel(pmd
, address
);
414 * Don't use pte_page here, because the mappings can point
415 * outside mem_map, and the NUMA hash lookup cannot handle
418 if (!pte_present(*pte
) || pte_pfn(*pte
) != pte_pfn(*pte_ref
))
424 #ifdef CONFIG_CPU_SUP_AMD
425 static const char errata93_warning
[] =
427 "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
428 "******* Working around it, but it may cause SEGVs or burn power.\n"
429 "******* Please consider a BIOS update.\n"
430 "******* 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
)
451 pgd_t
*base
= __va(read_cr3() & PHYSICAL_PAGE_MASK
);
452 pgd_t
*pgd
= base
+ pgd_index(address
);
457 if (bad_address(pgd
))
460 printk("PGD %lx ", pgd_val(*pgd
));
462 if (!pgd_present(*pgd
))
465 pud
= pud_offset(pgd
, address
);
466 if (bad_address(pud
))
469 printk("PUD %lx ", pud_val(*pud
));
470 if (!pud_present(*pud
) || pud_large(*pud
))
473 pmd
= pmd_offset(pud
, address
);
474 if (bad_address(pmd
))
477 printk("PMD %lx ", pmd_val(*pmd
));
478 if (!pmd_present(*pmd
) || pmd_large(*pmd
))
481 pte
= pte_offset_kernel(pmd
, address
);
482 if (bad_address(pte
))
485 printk("PTE %lx", pte_val(*pte
));
493 #endif /* CONFIG_X86_64 */
496 * Workaround for K8 erratum #93 & buggy BIOS.
498 * BIOS SMM functions are required to use a specific workaround
499 * to avoid corruption of the 64bit RIP register on C stepping K8.
501 * A lot of BIOS that didn't get tested properly miss this.
503 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
504 * Try to work around it here.
506 * Note we only handle faults in kernel here.
507 * Does nothing on 32-bit.
509 static int is_errata93(struct pt_regs
*regs
, unsigned long address
)
511 #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
512 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
513 || boot_cpu_data
.x86
!= 0xf)
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
, from_kuid(&init_user_ns
, 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_nr
= X86_TRAP_PF
;
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
, int signal
, int si_code
)
632 struct task_struct
*tsk
= current
;
633 unsigned long *stackend
;
637 /* Are we prepared to handle this kernel fault? */
638 if (fixup_exception(regs
)) {
639 if (current_thread_info()->sig_on_uaccess_error
&& signal
) {
640 tsk
->thread
.trap_nr
= X86_TRAP_PF
;
641 tsk
->thread
.error_code
= error_code
| PF_USER
;
642 tsk
->thread
.cr2
= address
;
644 /* XXX: hwpoison faults will set the wrong code. */
645 force_sig_info_fault(signal
, si_code
, address
, tsk
, 0);
653 * Valid to do another page fault here, because if this fault
654 * had been triggered by is_prefetch fixup_exception would have
659 * Hall of shame of CPU/BIOS bugs.
661 if (is_prefetch(regs
, error_code
, address
))
664 if (is_errata93(regs
, address
))
668 * Oops. The kernel tried to access some bad page. We'll have to
669 * terminate things with extreme prejudice:
671 flags
= oops_begin();
673 show_fault_oops(regs
, error_code
, address
);
675 stackend
= end_of_stack(tsk
);
676 if (tsk
!= &init_task
&& *stackend
!= STACK_END_MAGIC
)
677 printk(KERN_EMERG
"Thread overran stack, or stack corrupted\n");
679 tsk
->thread
.cr2
= address
;
680 tsk
->thread
.trap_nr
= X86_TRAP_PF
;
681 tsk
->thread
.error_code
= error_code
;
684 if (__die("Oops", regs
, error_code
))
687 /* Executive summary in case the body of the oops scrolled away */
688 printk(KERN_DEFAULT
"CR2: %016lx\n", address
);
690 oops_end(flags
, regs
, sig
);
694 * Print out info about fatal segfaults, if the show_unhandled_signals
698 show_signal_msg(struct pt_regs
*regs
, unsigned long error_code
,
699 unsigned long address
, struct task_struct
*tsk
)
701 if (!unhandled_signal(tsk
, SIGSEGV
))
704 if (!printk_ratelimit())
707 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
708 task_pid_nr(tsk
) > 1 ? KERN_INFO
: KERN_EMERG
,
709 tsk
->comm
, task_pid_nr(tsk
), address
,
710 (void *)regs
->ip
, (void *)regs
->sp
, error_code
);
712 print_vma_addr(KERN_CONT
" in ", regs
->ip
);
714 printk(KERN_CONT
"\n");
718 __bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
719 unsigned long address
, int si_code
)
721 struct task_struct
*tsk
= current
;
723 /* User mode accesses just cause a SIGSEGV */
724 if (error_code
& PF_USER
) {
726 * It's possible to have interrupts off here:
731 * Valid to do another page fault here because this one came
734 if (is_prefetch(regs
, error_code
, address
))
737 if (is_errata100(regs
, address
))
742 * Instruction fetch faults in the vsyscall page might need
745 if (unlikely((error_code
& PF_INSTR
) &&
746 ((address
& ~0xfff) == VSYSCALL_START
))) {
747 if (emulate_vsyscall(regs
, address
))
752 if (unlikely(show_unhandled_signals
))
753 show_signal_msg(regs
, error_code
, address
, tsk
);
755 /* Kernel addresses are always protection faults: */
756 tsk
->thread
.cr2
= address
;
757 tsk
->thread
.error_code
= error_code
| (address
>= TASK_SIZE
);
758 tsk
->thread
.trap_nr
= X86_TRAP_PF
;
760 force_sig_info_fault(SIGSEGV
, si_code
, address
, tsk
, 0);
765 if (is_f00f_bug(regs
, address
))
768 no_context(regs
, error_code
, address
, SIGSEGV
, si_code
);
772 bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
773 unsigned long address
)
775 __bad_area_nosemaphore(regs
, error_code
, address
, SEGV_MAPERR
);
779 __bad_area(struct pt_regs
*regs
, unsigned long error_code
,
780 unsigned long address
, int si_code
)
782 struct mm_struct
*mm
= current
->mm
;
785 * Something tried to access memory that isn't in our memory map..
786 * Fix it, but check if it's kernel or user first..
788 up_read(&mm
->mmap_sem
);
790 __bad_area_nosemaphore(regs
, error_code
, address
, si_code
);
794 bad_area(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
)
796 __bad_area(regs
, error_code
, address
, SEGV_MAPERR
);
800 bad_area_access_error(struct pt_regs
*regs
, unsigned long error_code
,
801 unsigned long address
)
803 __bad_area(regs
, error_code
, address
, SEGV_ACCERR
);
807 do_sigbus(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
,
810 struct task_struct
*tsk
= current
;
811 struct mm_struct
*mm
= tsk
->mm
;
812 int code
= BUS_ADRERR
;
814 up_read(&mm
->mmap_sem
);
816 /* Kernel mode? Handle exceptions or die: */
817 if (!(error_code
& PF_USER
)) {
818 no_context(regs
, error_code
, address
, SIGBUS
, BUS_ADRERR
);
822 /* User-space => ok to do another page fault: */
823 if (is_prefetch(regs
, error_code
, address
))
826 tsk
->thread
.cr2
= address
;
827 tsk
->thread
.error_code
= error_code
;
828 tsk
->thread
.trap_nr
= X86_TRAP_PF
;
830 #ifdef CONFIG_MEMORY_FAILURE
831 if (fault
& (VM_FAULT_HWPOISON
|VM_FAULT_HWPOISON_LARGE
)) {
833 "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
834 tsk
->comm
, tsk
->pid
, address
);
835 code
= BUS_MCEERR_AR
;
838 force_sig_info_fault(SIGBUS
, code
, address
, tsk
, fault
);
842 mm_fault_error(struct pt_regs
*regs
, unsigned long error_code
,
843 unsigned long address
, unsigned int fault
)
846 * Pagefault was interrupted by SIGKILL. We have no reason to
847 * continue pagefault.
849 if (fatal_signal_pending(current
)) {
850 if (!(fault
& VM_FAULT_RETRY
))
851 up_read(¤t
->mm
->mmap_sem
);
852 if (!(error_code
& PF_USER
))
853 no_context(regs
, error_code
, address
, 0, 0);
856 if (!(fault
& VM_FAULT_ERROR
))
859 if (fault
& VM_FAULT_OOM
) {
860 /* Kernel mode? Handle exceptions or die: */
861 if (!(error_code
& PF_USER
)) {
862 up_read(¤t
->mm
->mmap_sem
);
863 no_context(regs
, error_code
, address
,
864 SIGSEGV
, SEGV_MAPERR
);
868 up_read(¤t
->mm
->mmap_sem
);
871 * We ran out of memory, call the OOM killer, and return the
872 * userspace (which will retry the fault, or kill us if we got
875 pagefault_out_of_memory();
877 if (fault
& (VM_FAULT_SIGBUS
|VM_FAULT_HWPOISON
|
878 VM_FAULT_HWPOISON_LARGE
))
879 do_sigbus(regs
, error_code
, address
, fault
);
886 static int spurious_fault_check(unsigned long error_code
, pte_t
*pte
)
888 if ((error_code
& PF_WRITE
) && !pte_write(*pte
))
891 if ((error_code
& PF_INSTR
) && !pte_exec(*pte
))
898 * Handle a spurious fault caused by a stale TLB entry.
900 * This allows us to lazily refresh the TLB when increasing the
901 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
902 * eagerly is very expensive since that implies doing a full
903 * cross-processor TLB flush, even if no stale TLB entries exist
904 * on other processors.
906 * There are no security implications to leaving a stale TLB when
907 * increasing the permissions on a page.
909 static noinline __kprobes
int
910 spurious_fault(unsigned long error_code
, unsigned long address
)
918 /* Reserved-bit violation or user access to kernel space? */
919 if (error_code
& (PF_USER
| PF_RSVD
))
922 pgd
= init_mm
.pgd
+ pgd_index(address
);
923 if (!pgd_present(*pgd
))
926 pud
= pud_offset(pgd
, address
);
927 if (!pud_present(*pud
))
931 return spurious_fault_check(error_code
, (pte_t
*) pud
);
933 pmd
= pmd_offset(pud
, address
);
934 if (!pmd_present(*pmd
))
938 return spurious_fault_check(error_code
, (pte_t
*) pmd
);
941 * Note: don't use pte_present() here, since it returns true
942 * if the _PAGE_PROTNONE bit is set. However, this aliases the
943 * _PAGE_GLOBAL bit, which for kernel pages give false positives
944 * when CONFIG_DEBUG_PAGEALLOC is used.
946 pte
= pte_offset_kernel(pmd
, address
);
947 if (!(pte_flags(*pte
) & _PAGE_PRESENT
))
950 ret
= spurious_fault_check(error_code
, pte
);
955 * Make sure we have permissions in PMD.
956 * If not, then there's a bug in the page tables:
958 ret
= spurious_fault_check(error_code
, (pte_t
*) pmd
);
959 WARN_ONCE(!ret
, "PMD has incorrect permission bits\n");
964 int show_unhandled_signals
= 1;
967 access_error(unsigned long error_code
, struct vm_area_struct
*vma
)
969 if (error_code
& PF_WRITE
) {
970 /* write, present and write, not present: */
971 if (unlikely(!(vma
->vm_flags
& VM_WRITE
)))
977 if (unlikely(error_code
& PF_PROT
))
980 /* read, not present: */
981 if (unlikely(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
))))
987 static int fault_in_kernel_space(unsigned long address
)
989 return address
>= TASK_SIZE_MAX
;
992 static inline bool smap_violation(int error_code
, struct pt_regs
*regs
)
994 if (error_code
& PF_USER
)
997 if (!user_mode_vm(regs
) && (regs
->flags
& X86_EFLAGS_AC
))
1004 * This routine handles page faults. It determines the address,
1005 * and the problem, and then passes it off to one of the appropriate
1008 static void __kprobes
1009 __do_page_fault(struct pt_regs
*regs
, unsigned long error_code
)
1011 struct vm_area_struct
*vma
;
1012 struct task_struct
*tsk
;
1013 unsigned long address
;
1014 struct mm_struct
*mm
;
1016 int write
= error_code
& PF_WRITE
;
1017 unsigned int flags
= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_KILLABLE
|
1018 (write
? FAULT_FLAG_WRITE
: 0);
1023 /* Get the faulting address: */
1024 address
= read_cr2();
1027 * Detect and handle instructions that would cause a page fault for
1028 * both a tracked kernel page and a userspace page.
1030 if (kmemcheck_active(regs
))
1031 kmemcheck_hide(regs
);
1032 prefetchw(&mm
->mmap_sem
);
1034 if (unlikely(kmmio_fault(regs
, address
)))
1038 * We fault-in kernel-space virtual memory on-demand. The
1039 * 'reference' page table is init_mm.pgd.
1041 * NOTE! We MUST NOT take any locks for this case. We may
1042 * be in an interrupt or a critical region, and should
1043 * only copy the information from the master page table,
1046 * This verifies that the fault happens in kernel space
1047 * (error_code & 4) == 0, and that the fault was not a
1048 * protection error (error_code & 9) == 0.
1050 if (unlikely(fault_in_kernel_space(address
))) {
1051 if (!(error_code
& (PF_RSVD
| PF_USER
| PF_PROT
))) {
1052 if (vmalloc_fault(address
) >= 0)
1055 if (kmemcheck_fault(regs
, address
, error_code
))
1059 /* Can handle a stale RO->RW TLB: */
1060 if (spurious_fault(error_code
, address
))
1063 /* kprobes don't want to hook the spurious faults: */
1064 if (notify_page_fault(regs
))
1067 * Don't take the mm semaphore here. If we fixup a prefetch
1068 * fault we could otherwise deadlock:
1070 bad_area_nosemaphore(regs
, error_code
, address
);
1075 /* kprobes don't want to hook the spurious faults: */
1076 if (unlikely(notify_page_fault(regs
)))
1079 * It's safe to allow irq's after cr2 has been saved and the
1080 * vmalloc fault has been handled.
1082 * User-mode registers count as a user access even for any
1083 * potential system fault or CPU buglet:
1085 if (user_mode_vm(regs
)) {
1087 error_code
|= PF_USER
;
1089 if (regs
->flags
& X86_EFLAGS_IF
)
1093 if (unlikely(error_code
& PF_RSVD
))
1094 pgtable_bad(regs
, error_code
, address
);
1096 if (static_cpu_has(X86_FEATURE_SMAP
)) {
1097 if (unlikely(smap_violation(error_code
, regs
))) {
1098 bad_area_nosemaphore(regs
, error_code
, address
);
1103 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS
, 1, regs
, address
);
1106 * If we're in an interrupt, have no user context or are running
1107 * in an atomic region then we must not take the fault:
1109 if (unlikely(in_atomic() || !mm
)) {
1110 bad_area_nosemaphore(regs
, error_code
, address
);
1115 * When running in the kernel we expect faults to occur only to
1116 * addresses in user space. All other faults represent errors in
1117 * the kernel and should generate an OOPS. Unfortunately, in the
1118 * case of an erroneous fault occurring in a code path which already
1119 * holds mmap_sem we will deadlock attempting to validate the fault
1120 * against the address space. Luckily the kernel only validly
1121 * references user space from well defined areas of code, which are
1122 * listed in the exceptions table.
1124 * As the vast majority of faults will be valid we will only perform
1125 * the source reference check when there is a possibility of a
1126 * deadlock. Attempt to lock the address space, if we cannot we then
1127 * validate the source. If this is invalid we can skip the address
1128 * space check, thus avoiding the deadlock:
1130 if (unlikely(!down_read_trylock(&mm
->mmap_sem
))) {
1131 if ((error_code
& PF_USER
) == 0 &&
1132 !search_exception_tables(regs
->ip
)) {
1133 bad_area_nosemaphore(regs
, error_code
, address
);
1137 down_read(&mm
->mmap_sem
);
1140 * The above down_read_trylock() might have succeeded in
1141 * which case we'll have missed the might_sleep() from
1147 vma
= find_vma(mm
, address
);
1148 if (unlikely(!vma
)) {
1149 bad_area(regs
, error_code
, address
);
1152 if (likely(vma
->vm_start
<= address
))
1154 if (unlikely(!(vma
->vm_flags
& VM_GROWSDOWN
))) {
1155 bad_area(regs
, error_code
, address
);
1158 if (error_code
& PF_USER
) {
1160 * Accessing the stack below %sp is always a bug.
1161 * The large cushion allows instructions like enter
1162 * and pusha to work. ("enter $65535, $31" pushes
1163 * 32 pointers and then decrements %sp by 65535.)
1165 if (unlikely(address
+ 65536 + 32 * sizeof(unsigned long) < regs
->sp
)) {
1166 bad_area(regs
, error_code
, address
);
1170 if (unlikely(expand_stack(vma
, address
))) {
1171 bad_area(regs
, error_code
, address
);
1176 * Ok, we have a good vm_area for this memory access, so
1177 * we can handle it..
1180 if (unlikely(access_error(error_code
, vma
))) {
1181 bad_area_access_error(regs
, error_code
, address
);
1186 * If for any reason at all we couldn't handle the fault,
1187 * make sure we exit gracefully rather than endlessly redo
1190 fault
= handle_mm_fault(mm
, vma
, address
, flags
);
1192 if (unlikely(fault
& (VM_FAULT_RETRY
|VM_FAULT_ERROR
))) {
1193 if (mm_fault_error(regs
, error_code
, address
, fault
))
1198 * Major/minor page fault accounting is only done on the
1199 * initial attempt. If we go through a retry, it is extremely
1200 * likely that the page will be found in page cache at that point.
1202 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
1203 if (fault
& VM_FAULT_MAJOR
) {
1205 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ
, 1,
1209 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN
, 1,
1212 if (fault
& VM_FAULT_RETRY
) {
1213 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
1215 flags
&= ~FAULT_FLAG_ALLOW_RETRY
;
1216 flags
|= FAULT_FLAG_TRIED
;
1221 check_v8086_mode(regs
, address
, tsk
);
1223 up_read(&mm
->mmap_sem
);
1226 dotraplinkage
void __kprobes
1227 do_page_fault(struct pt_regs
*regs
, unsigned long error_code
)
1229 exception_enter(regs
);
1230 __do_page_fault(regs
, error_code
);
1231 exception_exit(regs
);