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 */
23 * Page fault error code bits:
25 * bit 0 == 0: no page found 1: protection fault
26 * bit 1 == 0: read access 1: write access
27 * bit 2 == 0: kernel-mode access 1: user-mode access
28 * bit 3 == 1: use of reserved bit detected
29 * bit 4 == 1: fault was an instruction fetch
31 enum x86_pf_error_code {
41 * Returns 0 if mmiotrace is disabled, or if the fault is not
42 * handled by mmiotrace:
44 static inline int __kprobes
45 kmmio_fault(struct pt_regs *regs, unsigned long addr)
47 if (unlikely(is_kmmio_active()))
48 if (kmmio_handler(regs, addr) == 1)
53 static inline int __kprobes notify_page_fault(struct pt_regs *regs)
57 /* kprobe_running() needs smp_processor_id() */
58 if (kprobes_built_in() && !user_mode_vm(regs)) {
60 if (kprobe_running() && kprobe_fault_handler(regs, 14))
73 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
74 * Check that here and ignore it.
78 * Sometimes the CPU reports invalid exceptions on prefetch.
79 * Check that here and ignore it.
81 * Opcode checker based on code by Richard Brunner.
84 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
85 unsigned char opcode, int *prefetch)
87 unsigned char instr_hi = opcode & 0xf0;
88 unsigned char instr_lo = opcode & 0x0f;
94 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
95 * In X86_64 long mode, the CPU will signal invalid
96 * opcode if some of these prefixes are present so
97 * X86_64 will never get here anyway
99 return ((instr_lo & 7) == 0x6);
103 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
104 * Need to figure out under what instruction mode the
105 * instruction was issued. Could check the LDT for lm,
106 * but for now it's good enough to assume that long
107 * mode only uses well known segments or kernel.
109 return (!user_mode(regs) || user_64bit_mode(regs));
112 /* 0x64 thru 0x67 are valid prefixes in all modes. */
113 return (instr_lo & 0xC) == 0x4;
115 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
116 return !instr_lo || (instr_lo>>1) == 1;
118 /* Prefetch instruction is 0x0F0D or 0x0F18 */
119 if (probe_kernel_address(instr, opcode))
122 *prefetch = (instr_lo == 0xF) &&
123 (opcode == 0x0D || opcode == 0x18);
131 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
133 unsigned char *max_instr;
134 unsigned char *instr;
138 * If it was a exec (instruction fetch) fault on NX page, then
139 * do not ignore the fault:
141 if (error_code & PF_INSTR)
144 instr = (void *)convert_ip_to_linear(current, regs);
145 max_instr = instr + 15;
147 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
150 while (instr < max_instr) {
151 unsigned char opcode;
153 if (probe_kernel_address(instr, opcode))
158 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
165 force_sig_info_fault(int si_signo, int si_code, unsigned long address,
166 struct task_struct *tsk, int fault)
171 info.si_signo = si_signo;
173 info.si_code = si_code;
174 info.si_addr = (void __user *)address;
175 if (fault & VM_FAULT_HWPOISON_LARGE)
176 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
177 if (fault & VM_FAULT_HWPOISON)
179 info.si_addr_lsb = lsb;
181 force_sig_info(si_signo, &info, tsk);
184 DEFINE_SPINLOCK(pgd_lock);
188 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
190 unsigned index = pgd_index(address);
196 pgd_k = init_mm.pgd + index;
198 if (!pgd_present(*pgd_k))
202 * set_pgd(pgd, *pgd_k); here would be useless on PAE
203 * and redundant with the set_pmd() on non-PAE. As would
206 pud = pud_offset(pgd, address);
207 pud_k = pud_offset(pgd_k, address);
208 if (!pud_present(*pud_k))
211 pmd = pmd_offset(pud, address);
212 pmd_k = pmd_offset(pud_k, address);
213 if (!pmd_present(*pmd_k))
216 if (!pmd_present(*pmd))
217 #if CONFIG_XEN_COMPAT > 0x030002
218 set_pmd(pmd, *pmd_k);
221 * When running on older Xen we must launder *pmd_k through
222 * pmd_val() to ensure that _PAGE_PRESENT is correctly set.
224 set_pmd(pmd, __pmd(pmd_val(*pmd_k)));
227 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
232 void vmalloc_sync_all(void)
234 unsigned long address;
236 if (SHARED_KERNEL_PMD)
239 for (address = VMALLOC_START & PMD_MASK;
240 address >= TASK_SIZE && address < FIXADDR_TOP;
241 address += PMD_SIZE) {
244 spin_lock(&pgd_lock);
245 list_for_each_entry(page, &pgd_list, lru) {
246 spinlock_t *pgt_lock;
249 /* the pgt_lock only for Xen */
250 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
253 ret = vmalloc_sync_one(page_address(page), address);
254 spin_unlock(pgt_lock);
259 spin_unlock(&pgd_lock);
266 * Handle a fault on the vmalloc or module mapping area
268 static noinline __kprobes int vmalloc_fault(unsigned long address)
270 unsigned long pgd_paddr;
274 /* Make sure we are in vmalloc area: */
275 if (!(address >= VMALLOC_START && address < VMALLOC_END))
278 WARN_ON_ONCE(in_nmi());
281 * Synchronize this task's top level page-table
282 * with the 'reference' page table.
284 * Do _not_ use "current" here. We might be inside
285 * an interrupt in the middle of a task switch..
287 pgd_paddr = read_cr3();
288 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
292 pte_k = pte_offset_kernel(pmd_k, address);
293 if (!pte_present(*pte_k))
300 * Did it hit the DOS screen memory VA from vm86 mode?
303 check_v8086_mode(struct pt_regs *regs, unsigned long address,
304 struct task_struct *tsk)
308 if (!v8086_mode(regs))
311 bit = (address - 0xA0000) >> PAGE_SHIFT;
313 tsk->thread.screen_bitmap |= 1 << bit;
316 static bool low_pfn(unsigned long pfn)
318 return pfn < max_low_pfn;
321 static void dump_pagetable(unsigned long address)
323 pgd_t *base = __va(read_cr3());
324 pgd_t *pgd = &base[pgd_index(address)];
328 #ifdef CONFIG_X86_PAE
329 printk("*pdpt = %016Lx ", __pgd_val(*pgd));
330 if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
333 pmd = pmd_offset(pud_offset(pgd, address), address);
334 printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)__pmd_val(*pmd));
337 * We must not directly access the pte in the highpte
338 * case if the page table is located in highmem.
339 * And let's rather not kmap-atomic the pte, just in case
340 * it's allocated already:
342 if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
345 pte = pte_offset_kernel(pmd, address);
346 printk(KERN_CONT "*pte = %0*Lx ", sizeof(*pte) * 2, (u64)__pte_val(*pte));
348 printk(KERN_CONT "\n");
350 #define dump_pagetable(addr, krnl) dump_pagetable(addr)
352 #else /* CONFIG_X86_64: */
354 void vmalloc_sync_all(void)
356 sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END);
362 * Handle a fault on the vmalloc area
364 * This assumes no large pages in there.
366 static noinline __kprobes int vmalloc_fault(unsigned long address)
368 pgd_t *pgd, *pgd_ref;
369 pud_t *pud, *pud_ref;
370 pmd_t *pmd, *pmd_ref;
371 pte_t *pte, *pte_ref;
373 /* Make sure we are in vmalloc area: */
374 if (!(address >= VMALLOC_START && address < VMALLOC_END))
377 WARN_ON_ONCE(in_nmi());
380 * Copy kernel mappings over when needed. This can also
381 * happen within a race in page table update. In the later
384 pgd = pgd_offset(current->active_mm, address);
385 pgd_ref = pgd_offset_k(address);
386 if (pgd_none(*pgd_ref))
390 set_pgd(pgd, *pgd_ref);
392 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
395 * Below here mismatches are bugs because these lower tables
399 pud = pud_offset(pgd, address);
400 pud_ref = pud_offset(pgd_ref, address);
401 if (pud_none(*pud_ref))
404 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
407 pmd = pmd_offset(pud, address);
408 pmd_ref = pmd_offset(pud_ref, address);
409 if (pmd_none(*pmd_ref))
412 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
415 pte_ref = pte_offset_kernel(pmd_ref, address);
416 if (!pte_present(*pte_ref))
419 pte = pte_offset_kernel(pmd, address);
422 * Don't use pte_page here, because the mappings can point
423 * outside mem_map, and the NUMA hash lookup cannot handle
426 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
432 #ifdef CONFIG_CPU_SUP_AMD
433 static const char errata93_warning[] =
435 "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
436 "******* Working around it, but it may cause SEGVs or burn power.\n"
437 "******* Please consider a BIOS update.\n"
438 "******* Disabling USB legacy in the BIOS may also help.\n";
442 * No vm86 mode in 64-bit mode:
445 check_v8086_mode(struct pt_regs *regs, unsigned long address,
446 struct task_struct *tsk)
450 static int bad_address(void *p)
454 return probe_kernel_address((unsigned long *)p, dummy);
457 static void dump_pagetable(unsigned long address, bool kernel)
459 pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
460 pgd_t *pgd = base + pgd_index(address);
466 pgd = __user_pgd(base) + pgd_index(address);
468 if (bad_address(pgd))
471 printk("PGD %lx ", pgd_val(*pgd));
473 if (!pgd_present(*pgd))
476 pud = pud_offset(pgd, address);
477 if (bad_address(pud))
480 printk(KERN_CONT "PUD %lx ", pud_val(*pud));
481 if (!pud_present(*pud) || pud_large(*pud))
484 pmd = pmd_offset(pud, address);
485 if (bad_address(pmd))
488 printk(KERN_CONT "PMD %lx ", pmd_val(*pmd));
489 if (!pmd_present(*pmd) || pmd_large(*pmd))
492 pte = pte_offset_kernel(pmd, address);
493 if (bad_address(pte))
496 printk(KERN_CONT "PTE %lx", pte_val(*pte));
498 printk(KERN_CONT "\n");
504 #endif /* CONFIG_X86_64 */
507 * Workaround for K8 erratum #93 & buggy BIOS.
509 * BIOS SMM functions are required to use a specific workaround
510 * to avoid corruption of the 64bit RIP register on C stepping K8.
512 * A lot of BIOS that didn't get tested properly miss this.
514 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
515 * Try to work around it here.
517 * Note we only handle faults in kernel here.
518 * Does nothing on 32-bit.
520 static int is_errata93(struct pt_regs *regs, unsigned long address)
522 #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
523 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
524 || boot_cpu_data.x86 != 0xf)
527 if (address != regs->ip)
530 if ((address >> 32) != 0)
533 address |= 0xffffffffUL << 32;
534 if ((address >= (u64)_stext && address <= (u64)_etext) ||
535 (address >= MODULES_VADDR && address <= MODULES_END)) {
536 printk_once(errata93_warning);
545 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
546 * to illegal addresses >4GB.
548 * We catch this in the page fault handler because these addresses
549 * are not reachable. Just detect this case and return. Any code
550 * segment in LDT is compatibility mode.
552 static int is_errata100(struct pt_regs *regs, unsigned long address)
555 if ((regs->cs == __USER32_CS || regs->cs == FLAT_USER_CS32 ||
556 (regs->cs & (1<<2))) && (address >> 32))
562 static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
564 #ifdef CONFIG_X86_F00F_BUG
568 * Pentium F0 0F C7 C8 bug workaround:
570 if (boot_cpu_data.f00f_bug) {
571 nr = (address - idt_descr.address) >> 3;
574 do_invalid_op(regs, 0);
582 static const char nx_warning[] = KERN_CRIT
583 "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
586 show_fault_oops(struct pt_regs *regs, unsigned long error_code,
587 unsigned long address)
589 if (!oops_may_print())
592 if (error_code & PF_INSTR) {
595 pte_t *pte = lookup_address(address, &level);
597 if (pte && pte_present(*pte) && !pte_exec(*pte))
598 printk(nx_warning, current_uid());
601 printk(KERN_ALERT "BUG: unable to handle kernel ");
602 if (address < PAGE_SIZE)
603 printk(KERN_CONT "NULL pointer dereference");
605 printk(KERN_CONT "paging request");
607 printk(KERN_CONT " at %p\n", (void *) address);
608 printk(KERN_ALERT "IP:");
609 printk_address(regs->ip, 1);
611 dump_pagetable(address, !(error_code & PF_USER));
615 pgtable_bad(struct pt_regs *regs, unsigned long error_code,
616 unsigned long address)
618 struct task_struct *tsk;
622 flags = oops_begin();
626 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
628 dump_pagetable(address, !(error_code & PF_USER));
630 tsk->thread.cr2 = address;
631 tsk->thread.trap_no = 14;
632 tsk->thread.error_code = error_code;
634 if (__die("Bad pagetable", regs, error_code))
637 oops_end(flags, regs, sig);
641 no_context(struct pt_regs *regs, unsigned long error_code,
642 unsigned long address, int signal, int si_code)
644 struct task_struct *tsk = current;
645 unsigned long *stackend;
649 /* Are we prepared to handle this kernel fault? */
650 if (fixup_exception(regs)) {
651 if (current_thread_info()->sig_on_uaccess_error && signal) {
652 tsk->thread.trap_no = 14;
653 tsk->thread.error_code = error_code | PF_USER;
654 tsk->thread.cr2 = address;
656 /* XXX: hwpoison faults will set the wrong code. */
657 force_sig_info_fault(signal, si_code, address, tsk, 0);
665 * Valid to do another page fault here, because if this fault
666 * had been triggered by is_prefetch fixup_exception would have
671 * Hall of shame of CPU/BIOS bugs.
673 if (is_prefetch(regs, error_code, address))
676 if (is_errata93(regs, address))
680 * Oops. The kernel tried to access some bad page. We'll have to
681 * terminate things with extreme prejudice:
683 flags = oops_begin();
685 show_fault_oops(regs, error_code, address);
687 stackend = end_of_stack(tsk);
688 if (tsk != &init_task && *stackend != STACK_END_MAGIC)
689 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
691 tsk->thread.cr2 = address;
692 tsk->thread.trap_no = 14;
693 tsk->thread.error_code = error_code;
696 if (__die("Oops", regs, error_code))
699 /* Executive summary in case the body of the oops scrolled away */
700 printk(KERN_DEFAULT "CR2: %016lx\n", address);
702 oops_end(flags, regs, sig);
706 * Print out info about fatal segfaults, if the show_unhandled_signals
710 show_signal_msg(struct pt_regs *regs, unsigned long error_code,
711 unsigned long address, struct task_struct *tsk)
713 if (!unhandled_signal(tsk, SIGSEGV))
716 if (!printk_ratelimit())
719 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
720 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
721 tsk->comm, task_pid_nr(tsk), address,
722 (void *)regs->ip, (void *)regs->sp, error_code);
724 print_vma_addr(KERN_CONT " in ", regs->ip);
726 printk(KERN_CONT "\n");
730 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
731 unsigned long address, int si_code)
733 struct task_struct *tsk = current;
735 /* User mode accesses just cause a SIGSEGV */
736 if (error_code & PF_USER) {
738 * It's possible to have interrupts off here:
743 * Valid to do another page fault here because this one came
746 if (is_prefetch(regs, error_code, address))
749 if (is_errata100(regs, address))
754 * Instruction fetch faults in the vsyscall page might need
757 if (unlikely((error_code & PF_INSTR) &&
758 ((address & ~0xfff) == VSYSCALL_START))) {
759 if (emulate_vsyscall(regs, address))
764 if (unlikely(show_unhandled_signals))
765 show_signal_msg(regs, error_code, address, tsk);
767 /* Kernel addresses are always protection faults: */
768 tsk->thread.cr2 = address;
769 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
770 tsk->thread.trap_no = 14;
772 force_sig_info_fault(SIGSEGV, si_code, address, tsk, 0);
777 if (is_f00f_bug(regs, address))
780 no_context(regs, error_code, address, SIGSEGV, si_code);
784 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
785 unsigned long address)
787 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
791 __bad_area(struct pt_regs *regs, unsigned long error_code,
792 unsigned long address, int si_code)
794 struct mm_struct *mm = current->mm;
797 * Something tried to access memory that isn't in our memory map..
798 * Fix it, but check if it's kernel or user first..
800 up_read(&mm->mmap_sem);
802 __bad_area_nosemaphore(regs, error_code, address, si_code);
806 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
808 __bad_area(regs, error_code, address, SEGV_MAPERR);
812 bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
813 unsigned long address)
815 __bad_area(regs, error_code, address, SEGV_ACCERR);
818 /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
820 out_of_memory(struct pt_regs *regs, unsigned long error_code,
821 unsigned long address)
824 * We ran out of memory, call the OOM killer, and return the userspace
825 * (which will retry the fault, or kill us if we got oom-killed):
827 up_read(¤t->mm->mmap_sem);
829 pagefault_out_of_memory();
833 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
836 struct task_struct *tsk = current;
837 struct mm_struct *mm = tsk->mm;
838 int code = BUS_ADRERR;
840 up_read(&mm->mmap_sem);
842 /* Kernel mode? Handle exceptions or die: */
843 if (!(error_code & PF_USER)) {
844 no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
848 /* User-space => ok to do another page fault: */
849 if (is_prefetch(regs, error_code, address))
852 tsk->thread.cr2 = address;
853 tsk->thread.error_code = error_code;
854 tsk->thread.trap_no = 14;
856 #ifdef CONFIG_MEMORY_FAILURE
857 if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
859 "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
860 tsk->comm, tsk->pid, address);
861 code = BUS_MCEERR_AR;
864 force_sig_info_fault(SIGBUS, code, address, tsk, fault);
868 mm_fault_error(struct pt_regs *regs, unsigned long error_code,
869 unsigned long address, unsigned int fault)
872 * Pagefault was interrupted by SIGKILL. We have no reason to
873 * continue pagefault.
875 if (fatal_signal_pending(current)) {
876 if (!(fault & VM_FAULT_RETRY))
877 up_read(¤t->mm->mmap_sem);
878 if (!(error_code & PF_USER))
879 no_context(regs, error_code, address, 0, 0);
882 if (!(fault & VM_FAULT_ERROR))
885 if (fault & VM_FAULT_OOM) {
886 /* Kernel mode? Handle exceptions or die: */
887 if (!(error_code & PF_USER)) {
888 up_read(¤t->mm->mmap_sem);
889 no_context(regs, error_code, address,
890 SIGSEGV, SEGV_MAPERR);
894 out_of_memory(regs, error_code, address);
896 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
897 VM_FAULT_HWPOISON_LARGE))
898 do_sigbus(regs, error_code, address, fault);
905 static int spurious_fault_check(unsigned long error_code, pte_t *pte)
907 if ((error_code & PF_WRITE) && !pte_write(*pte))
910 if ((error_code & PF_INSTR) && !pte_exec(*pte))
917 * Handle a spurious fault caused by a stale TLB entry.
919 * This allows us to lazily refresh the TLB when increasing the
920 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
921 * eagerly is very expensive since that implies doing a full
922 * cross-processor TLB flush, even if no stale TLB entries exist
923 * on other processors.
925 * There are no security implications to leaving a stale TLB when
926 * increasing the permissions on a page.
928 static noinline __kprobes int
929 spurious_fault(unsigned long error_code, unsigned long address)
937 /* Reserved-bit violation or user access to kernel space? */
938 if (error_code & (PF_USER | PF_RSVD))
941 pgd = init_mm.pgd + pgd_index(address);
942 if (!pgd_present(*pgd))
945 pud = pud_offset(pgd, address);
946 if (!pud_present(*pud))
950 return spurious_fault_check(error_code, (pte_t *) pud);
952 pmd = pmd_offset(pud, address);
953 if (!pmd_present(*pmd))
957 return spurious_fault_check(error_code, (pte_t *) pmd);
960 * Note: don't use pte_present() here, since it returns true
961 * if the _PAGE_PROTNONE bit is set. However, this aliases the
962 * _PAGE_GLOBAL bit, which for kernel pages give false positives
963 * when CONFIG_DEBUG_PAGEALLOC is used.
965 pte = pte_offset_kernel(pmd, address);
966 if (!(pte_flags(*pte) & _PAGE_PRESENT))
969 ret = spurious_fault_check(error_code, pte);
974 * Make sure we have permissions in PMD.
975 * If not, then there's a bug in the page tables:
977 ret = spurious_fault_check(error_code, (pte_t *) pmd);
978 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
983 int show_unhandled_signals = 1;
986 access_error(unsigned long error_code, struct vm_area_struct *vma)
988 if (error_code & PF_WRITE) {
989 /* write, present and write, not present: */
990 if (unlikely(!(vma->vm_flags & VM_WRITE)))
996 if (unlikely(error_code & PF_PROT))
999 /* read, not present: */
1000 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
1006 static int fault_in_kernel_space(unsigned long address)
1008 return address >= TASK_SIZE_MAX;
1012 * This routine handles page faults. It determines the address,
1013 * and the problem, and then passes it off to one of the appropriate
1016 dotraplinkage void __kprobes
1017 do_page_fault(struct pt_regs *regs, unsigned long error_code)
1019 struct vm_area_struct *vma;
1020 struct task_struct *tsk;
1021 unsigned long address;
1022 struct mm_struct *mm;
1024 int write = error_code & PF_WRITE;
1025 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
1026 (write ? FAULT_FLAG_WRITE : 0);
1028 /* Set the "privileged fault" bit to something sane. */
1029 if (user_mode_vm(regs))
1030 error_code |= PF_USER;
1032 error_code &= ~PF_USER;
1037 /* Get the faulting address: */
1038 address = read_cr2();
1041 * Detect and handle instructions that would cause a page fault for
1042 * both a tracked kernel page and a userspace page.
1044 if (kmemcheck_active(regs))
1045 kmemcheck_hide(regs);
1046 prefetchw(&mm->mmap_sem);
1048 if (unlikely(kmmio_fault(regs, address)))
1052 * We fault-in kernel-space virtual memory on-demand. The
1053 * 'reference' page table is init_mm.pgd.
1055 * NOTE! We MUST NOT take any locks for this case. We may
1056 * be in an interrupt or a critical region, and should
1057 * only copy the information from the master page table,
1060 * This verifies that the fault happens in kernel space
1061 * (error_code & 4) == 0, and that the fault was not a
1062 * protection error (error_code & 9) == 0.
1064 if (unlikely(fault_in_kernel_space(address))) {
1065 /* Faults in hypervisor area can never be patched up. */
1066 #if defined(CONFIG_X86_XEN)
1067 if (address >= hypervisor_virt_start) {
1068 #elif defined(CONFIG_X86_64_XEN)
1069 if (address >= HYPERVISOR_VIRT_START
1070 && address < HYPERVISOR_VIRT_END) {
1072 bad_area_nosemaphore(regs, error_code, address);
1076 if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
1077 if (vmalloc_fault(address) >= 0)
1080 if (kmemcheck_fault(regs, address, error_code))
1084 /* Can handle a stale RO->RW TLB: */
1085 if (spurious_fault(error_code, address))
1088 /* kprobes don't want to hook the spurious faults: */
1089 if (notify_page_fault(regs))
1092 * Don't take the mm semaphore here. If we fixup a prefetch
1093 * fault we could otherwise deadlock:
1095 bad_area_nosemaphore(regs, error_code, address);
1100 /* kprobes don't want to hook the spurious faults: */
1101 if (unlikely(notify_page_fault(regs)))
1104 * It's safe to allow irq's after cr2 has been saved and the
1105 * vmalloc fault has been handled.
1107 * User-mode registers count as a user access even for any
1108 * potential system fault or CPU buglet:
1110 if (user_mode_vm(regs)) {
1112 error_code |= PF_USER;
1114 if (regs->flags & X86_EFLAGS_IF)
1118 if (unlikely(error_code & PF_RSVD))
1119 pgtable_bad(regs, error_code, address);
1121 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
1124 * If we're in an interrupt, have no user context or are running
1125 * in an atomic region then we must not take the fault:
1127 if (unlikely(in_atomic() || !mm)) {
1128 bad_area_nosemaphore(regs, error_code, address);
1133 * When running in the kernel we expect faults to occur only to
1134 * addresses in user space. All other faults represent errors in
1135 * the kernel and should generate an OOPS. Unfortunately, in the
1136 * case of an erroneous fault occurring in a code path which already
1137 * holds mmap_sem we will deadlock attempting to validate the fault
1138 * against the address space. Luckily the kernel only validly
1139 * references user space from well defined areas of code, which are
1140 * listed in the exceptions table.
1142 * As the vast majority of faults will be valid we will only perform
1143 * the source reference check when there is a possibility of a
1144 * deadlock. Attempt to lock the address space, if we cannot we then
1145 * validate the source. If this is invalid we can skip the address
1146 * space check, thus avoiding the deadlock:
1148 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1149 if ((error_code & PF_USER) == 0 &&
1150 !search_exception_tables(regs->ip)) {
1151 bad_area_nosemaphore(regs, error_code, address);
1155 down_read(&mm->mmap_sem);
1158 * The above down_read_trylock() might have succeeded in
1159 * which case we'll have missed the might_sleep() from
1165 vma = find_vma(mm, address);
1166 if (unlikely(!vma)) {
1167 bad_area(regs, error_code, address);
1170 if (likely(vma->vm_start <= address))
1172 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1173 bad_area(regs, error_code, address);
1176 if (error_code & PF_USER) {
1178 * Accessing the stack below %sp is always a bug.
1179 * The large cushion allows instructions like enter
1180 * and pusha to work. ("enter $65535, $31" pushes
1181 * 32 pointers and then decrements %sp by 65535.)
1183 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1184 bad_area(regs, error_code, address);
1188 if (unlikely(expand_stack(vma, address))) {
1189 bad_area(regs, error_code, address);
1194 * Ok, we have a good vm_area for this memory access, so
1195 * we can handle it..
1198 if (unlikely(access_error(error_code, vma))) {
1199 bad_area_access_error(regs, error_code, address);
1204 * If for any reason at all we couldn't handle the fault,
1205 * make sure we exit gracefully rather than endlessly redo
1208 fault = handle_mm_fault(mm, vma, address, flags);
1210 if (unlikely(fault & (VM_FAULT_RETRY|VM_FAULT_ERROR))) {
1211 if (mm_fault_error(regs, error_code, address, fault))
1216 * Major/minor page fault accounting is only done on the
1217 * initial attempt. If we go through a retry, it is extremely
1218 * likely that the page will be found in page cache at that point.
1220 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1221 if (fault & VM_FAULT_MAJOR) {
1223 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
1227 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
1230 if (fault & VM_FAULT_RETRY) {
1231 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
1233 flags &= ~FAULT_FLAG_ALLOW_RETRY;
1238 check_v8086_mode(regs, address, tsk);
1240 up_read(&mm->mmap_sem);