1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1995 Linus Torvalds
4 * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
5 * Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar
7 #include <linux/sched.h> /* test_thread_flag(), ... */
8 #include <linux/sched/task_stack.h> /* task_stack_*(), ... */
9 #include <linux/kdebug.h> /* oops_begin/end, ... */
10 #include <linux/extable.h> /* search_exception_tables */
11 #include <linux/bootmem.h> /* max_low_pfn */
12 #include <linux/kprobes.h> /* NOKPROBE_SYMBOL, ... */
13 #include <linux/mmiotrace.h> /* kmmio_handler, ... */
14 #include <linux/perf_event.h> /* perf_sw_event */
15 #include <linux/hugetlb.h> /* hstate_index_to_shift */
16 #include <linux/prefetch.h> /* prefetchw */
17 #include <linux/context_tracking.h> /* exception_enter(), ... */
18 #include <linux/uaccess.h> /* faulthandler_disabled() */
20 #include <asm/cpufeature.h> /* boot_cpu_has, ... */
21 #include <asm/traps.h> /* dotraplinkage, ... */
22 #include <asm/pgalloc.h> /* pgd_*(), ... */
23 #include <asm/kmemcheck.h> /* kmemcheck_*(), ... */
24 #include <asm/fixmap.h> /* VSYSCALL_ADDR */
25 #include <asm/vsyscall.h> /* emulate_vsyscall */
26 #include <asm/vm86.h> /* struct vm86 */
27 #include <asm/mmu_context.h> /* vma_pkey() */
29 #define CREATE_TRACE_POINTS
30 #include <asm/trace/exceptions.h>
33 * Returns 0 if mmiotrace is disabled, or if the fault is not
34 * handled by mmiotrace:
36 static nokprobe_inline int
37 kmmio_fault(struct pt_regs *regs, unsigned long addr)
39 if (unlikely(is_kmmio_active()))
40 if (kmmio_handler(regs, addr) == 1)
45 static nokprobe_inline int kprobes_fault(struct pt_regs *regs)
49 /* kprobe_running() needs smp_processor_id() */
50 if (kprobes_built_in() && !user_mode(regs)) {
52 if (kprobe_running() && kprobe_fault_handler(regs, 14))
65 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
66 * Check that here and ignore it.
70 * Sometimes the CPU reports invalid exceptions on prefetch.
71 * Check that here and ignore it.
73 * Opcode checker based on code by Richard Brunner.
76 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
77 unsigned char opcode, int *prefetch)
79 unsigned char instr_hi = opcode & 0xf0;
80 unsigned char instr_lo = opcode & 0x0f;
86 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
87 * In X86_64 long mode, the CPU will signal invalid
88 * opcode if some of these prefixes are present so
89 * X86_64 will never get here anyway
91 return ((instr_lo & 7) == 0x6);
95 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
96 * Need to figure out under what instruction mode the
97 * instruction was issued. Could check the LDT for lm,
98 * but for now it's good enough to assume that long
99 * mode only uses well known segments or kernel.
101 return (!user_mode(regs) || user_64bit_mode(regs));
104 /* 0x64 thru 0x67 are valid prefixes in all modes. */
105 return (instr_lo & 0xC) == 0x4;
107 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
108 return !instr_lo || (instr_lo>>1) == 1;
110 /* Prefetch instruction is 0x0F0D or 0x0F18 */
111 if (probe_kernel_address(instr, opcode))
114 *prefetch = (instr_lo == 0xF) &&
115 (opcode == 0x0D || opcode == 0x18);
123 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
125 unsigned char *max_instr;
126 unsigned char *instr;
130 * If it was a exec (instruction fetch) fault on NX page, then
131 * do not ignore the fault:
133 if (error_code & X86_PF_INSTR)
136 instr = (void *)convert_ip_to_linear(current, regs);
137 max_instr = instr + 15;
139 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE_MAX)
142 while (instr < max_instr) {
143 unsigned char opcode;
145 if (probe_kernel_address(instr, opcode))
150 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
157 * A protection key fault means that the PKRU value did not allow
158 * access to some PTE. Userspace can figure out what PKRU was
159 * from the XSAVE state, and this function fills out a field in
160 * siginfo so userspace can discover which protection key was set
163 * If we get here, we know that the hardware signaled a X86_PF_PK
164 * fault and that there was a VMA once we got in the fault
165 * handler. It does *not* guarantee that the VMA we find here
166 * was the one that we faulted on.
168 * 1. T1 : mprotect_key(foo, PAGE_SIZE, pkey=4);
169 * 2. T1 : set PKRU to deny access to pkey=4, touches page
171 * 4. T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
172 * 5. T1 : enters fault handler, takes mmap_sem, etc...
173 * 6. T1 : reaches here, sees vma_pkey(vma)=5, when we really
174 * faulted on a pte with its pkey=4.
176 static void fill_sig_info_pkey(int si_code, siginfo_t *info, u32 *pkey)
178 /* This is effectively an #ifdef */
179 if (!boot_cpu_has(X86_FEATURE_OSPKE))
182 /* Fault not from Protection Keys: nothing to do */
183 if (si_code != SEGV_PKUERR)
186 * force_sig_info_fault() is called from a number of
187 * contexts, some of which have a VMA and some of which
188 * do not. The X86_PF_PK handing happens after we have a
189 * valid VMA, so we should never reach this without a
193 WARN_ONCE(1, "PKU fault with no VMA passed in");
198 * si_pkey should be thought of as a strong hint, but not
199 * absolutely guranteed to be 100% accurate because of
200 * the race explained above.
202 info->si_pkey = *pkey;
206 force_sig_info_fault(int si_signo, int si_code, unsigned long address,
207 struct task_struct *tsk, u32 *pkey, int fault)
212 info.si_signo = si_signo;
214 info.si_code = si_code;
215 info.si_addr = (void __user *)address;
216 if (fault & VM_FAULT_HWPOISON_LARGE)
217 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
218 if (fault & VM_FAULT_HWPOISON)
220 info.si_addr_lsb = lsb;
222 fill_sig_info_pkey(si_code, &info, pkey);
224 force_sig_info(si_signo, &info, tsk);
227 DEFINE_SPINLOCK(pgd_lock);
231 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
233 unsigned index = pgd_index(address);
240 pgd_k = init_mm.pgd + index;
242 if (!pgd_present(*pgd_k))
246 * set_pgd(pgd, *pgd_k); here would be useless on PAE
247 * and redundant with the set_pmd() on non-PAE. As would
250 p4d = p4d_offset(pgd, address);
251 p4d_k = p4d_offset(pgd_k, address);
252 if (!p4d_present(*p4d_k))
255 pud = pud_offset(p4d, address);
256 pud_k = pud_offset(p4d_k, address);
257 if (!pud_present(*pud_k))
260 pmd = pmd_offset(pud, address);
261 pmd_k = pmd_offset(pud_k, address);
262 if (!pmd_present(*pmd_k))
265 if (!pmd_present(*pmd))
266 set_pmd(pmd, *pmd_k);
268 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
273 void vmalloc_sync_all(void)
275 unsigned long address;
277 if (SHARED_KERNEL_PMD)
280 for (address = VMALLOC_START & PMD_MASK;
281 address >= TASK_SIZE_MAX && address < FIXADDR_TOP;
282 address += PMD_SIZE) {
285 spin_lock(&pgd_lock);
286 list_for_each_entry(page, &pgd_list, lru) {
287 spinlock_t *pgt_lock;
290 /* the pgt_lock only for Xen */
291 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
294 ret = vmalloc_sync_one(page_address(page), address);
295 spin_unlock(pgt_lock);
300 spin_unlock(&pgd_lock);
307 * Handle a fault on the vmalloc or module mapping area
309 static noinline int vmalloc_fault(unsigned long address)
311 unsigned long pgd_paddr;
315 /* Make sure we are in vmalloc area: */
316 if (!(address >= VMALLOC_START && address < VMALLOC_END))
319 WARN_ON_ONCE(in_nmi());
322 * Synchronize this task's top level page-table
323 * with the 'reference' page table.
325 * Do _not_ use "current" here. We might be inside
326 * an interrupt in the middle of a task switch..
328 pgd_paddr = read_cr3_pa();
329 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
333 if (pmd_huge(*pmd_k))
336 pte_k = pte_offset_kernel(pmd_k, address);
337 if (!pte_present(*pte_k))
342 NOKPROBE_SYMBOL(vmalloc_fault);
345 * Did it hit the DOS screen memory VA from vm86 mode?
348 check_v8086_mode(struct pt_regs *regs, unsigned long address,
349 struct task_struct *tsk)
354 if (!v8086_mode(regs) || !tsk->thread.vm86)
357 bit = (address - 0xA0000) >> PAGE_SHIFT;
359 tsk->thread.vm86->screen_bitmap |= 1 << bit;
363 static bool low_pfn(unsigned long pfn)
365 return pfn < max_low_pfn;
368 static void dump_pagetable(unsigned long address)
370 pgd_t *base = __va(read_cr3_pa());
371 pgd_t *pgd = &base[pgd_index(address)];
377 #ifdef CONFIG_X86_PAE
378 pr_info("*pdpt = %016Lx ", pgd_val(*pgd));
379 if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
381 #define pr_pde pr_cont
383 #define pr_pde pr_info
385 p4d = p4d_offset(pgd, address);
386 pud = pud_offset(p4d, address);
387 pmd = pmd_offset(pud, address);
388 pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
392 * We must not directly access the pte in the highpte
393 * case if the page table is located in highmem.
394 * And let's rather not kmap-atomic the pte, just in case
395 * it's allocated already:
397 if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
400 pte = pte_offset_kernel(pmd, address);
401 pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
406 #else /* CONFIG_X86_64: */
408 void vmalloc_sync_all(void)
410 sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END);
416 * Handle a fault on the vmalloc area
418 static noinline int vmalloc_fault(unsigned long address)
420 pgd_t *pgd, *pgd_ref;
421 p4d_t *p4d, *p4d_ref;
422 pud_t *pud, *pud_ref;
423 pmd_t *pmd, *pmd_ref;
424 pte_t *pte, *pte_ref;
426 /* Make sure we are in vmalloc area: */
427 if (!(address >= VMALLOC_START && address < VMALLOC_END))
430 WARN_ON_ONCE(in_nmi());
433 * Copy kernel mappings over when needed. This can also
434 * happen within a race in page table update. In the later
437 pgd = (pgd_t *)__va(read_cr3_pa()) + pgd_index(address);
438 pgd_ref = pgd_offset_k(address);
439 if (pgd_none(*pgd_ref))
442 if (pgd_none(*pgd)) {
443 set_pgd(pgd, *pgd_ref);
444 arch_flush_lazy_mmu_mode();
445 } else if (CONFIG_PGTABLE_LEVELS > 4) {
447 * With folded p4d, pgd_none() is always false, so the pgd may
448 * point to an empty page table entry and pgd_page_vaddr()
449 * will return garbage.
451 * We will do the correct sanity check on the p4d level.
453 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
456 /* With 4-level paging, copying happens on the p4d level. */
457 p4d = p4d_offset(pgd, address);
458 p4d_ref = p4d_offset(pgd_ref, address);
459 if (p4d_none(*p4d_ref))
462 if (p4d_none(*p4d)) {
463 set_p4d(p4d, *p4d_ref);
464 arch_flush_lazy_mmu_mode();
466 BUG_ON(p4d_pfn(*p4d) != p4d_pfn(*p4d_ref));
470 * Below here mismatches are bugs because these lower tables
474 pud = pud_offset(p4d, address);
475 pud_ref = pud_offset(p4d_ref, address);
476 if (pud_none(*pud_ref))
479 if (pud_none(*pud) || pud_pfn(*pud) != pud_pfn(*pud_ref))
485 pmd = pmd_offset(pud, address);
486 pmd_ref = pmd_offset(pud_ref, address);
487 if (pmd_none(*pmd_ref))
490 if (pmd_none(*pmd) || pmd_pfn(*pmd) != pmd_pfn(*pmd_ref))
496 pte_ref = pte_offset_kernel(pmd_ref, address);
497 if (!pte_present(*pte_ref))
500 pte = pte_offset_kernel(pmd, address);
503 * Don't use pte_page here, because the mappings can point
504 * outside mem_map, and the NUMA hash lookup cannot handle
507 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
512 NOKPROBE_SYMBOL(vmalloc_fault);
514 #ifdef CONFIG_CPU_SUP_AMD
515 static const char errata93_warning[] =
517 "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
518 "******* Working around it, but it may cause SEGVs or burn power.\n"
519 "******* Please consider a BIOS update.\n"
520 "******* Disabling USB legacy in the BIOS may also help.\n";
524 * No vm86 mode in 64-bit mode:
527 check_v8086_mode(struct pt_regs *regs, unsigned long address,
528 struct task_struct *tsk)
532 static int bad_address(void *p)
536 return probe_kernel_address((unsigned long *)p, dummy);
539 static void dump_pagetable(unsigned long address)
541 pgd_t *base = __va(read_cr3_pa());
542 pgd_t *pgd = base + pgd_index(address);
548 if (bad_address(pgd))
551 pr_info("PGD %lx ", pgd_val(*pgd));
553 if (!pgd_present(*pgd))
556 p4d = p4d_offset(pgd, address);
557 if (bad_address(p4d))
560 pr_cont("P4D %lx ", p4d_val(*p4d));
561 if (!p4d_present(*p4d) || p4d_large(*p4d))
564 pud = pud_offset(p4d, address);
565 if (bad_address(pud))
568 pr_cont("PUD %lx ", pud_val(*pud));
569 if (!pud_present(*pud) || pud_large(*pud))
572 pmd = pmd_offset(pud, address);
573 if (bad_address(pmd))
576 pr_cont("PMD %lx ", pmd_val(*pmd));
577 if (!pmd_present(*pmd) || pmd_large(*pmd))
580 pte = pte_offset_kernel(pmd, address);
581 if (bad_address(pte))
584 pr_cont("PTE %lx", pte_val(*pte));
592 #endif /* CONFIG_X86_64 */
595 * Workaround for K8 erratum #93 & buggy BIOS.
597 * BIOS SMM functions are required to use a specific workaround
598 * to avoid corruption of the 64bit RIP register on C stepping K8.
600 * A lot of BIOS that didn't get tested properly miss this.
602 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
603 * Try to work around it here.
605 * Note we only handle faults in kernel here.
606 * Does nothing on 32-bit.
608 static int is_errata93(struct pt_regs *regs, unsigned long address)
610 #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
611 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
612 || boot_cpu_data.x86 != 0xf)
615 if (address != regs->ip)
618 if ((address >> 32) != 0)
621 address |= 0xffffffffUL << 32;
622 if ((address >= (u64)_stext && address <= (u64)_etext) ||
623 (address >= MODULES_VADDR && address <= MODULES_END)) {
624 printk_once(errata93_warning);
633 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
634 * to illegal addresses >4GB.
636 * We catch this in the page fault handler because these addresses
637 * are not reachable. Just detect this case and return. Any code
638 * segment in LDT is compatibility mode.
640 static int is_errata100(struct pt_regs *regs, unsigned long address)
643 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
649 static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
651 #ifdef CONFIG_X86_F00F_BUG
655 * Pentium F0 0F C7 C8 bug workaround:
657 if (boot_cpu_has_bug(X86_BUG_F00F)) {
658 nr = (address - idt_descr.address) >> 3;
661 do_invalid_op(regs, 0);
669 static const char nx_warning[] = KERN_CRIT
670 "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
671 static const char smep_warning[] = KERN_CRIT
672 "unable to execute userspace code (SMEP?) (uid: %d)\n";
675 show_fault_oops(struct pt_regs *regs, unsigned long error_code,
676 unsigned long address)
678 if (!oops_may_print())
681 if (error_code & X86_PF_INSTR) {
686 pgd = __va(read_cr3_pa());
687 pgd += pgd_index(address);
689 pte = lookup_address_in_pgd(pgd, address, &level);
691 if (pte && pte_present(*pte) && !pte_exec(*pte))
692 printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
693 if (pte && pte_present(*pte) && pte_exec(*pte) &&
694 (pgd_flags(*pgd) & _PAGE_USER) &&
695 (__read_cr4() & X86_CR4_SMEP))
696 printk(smep_warning, from_kuid(&init_user_ns, current_uid()));
699 printk(KERN_ALERT "BUG: unable to handle kernel ");
700 if (address < PAGE_SIZE)
701 printk(KERN_CONT "NULL pointer dereference");
703 printk(KERN_CONT "paging request");
705 printk(KERN_CONT " at %p\n", (void *) address);
706 printk(KERN_ALERT "IP: %pS\n", (void *)regs->ip);
708 dump_pagetable(address);
712 pgtable_bad(struct pt_regs *regs, unsigned long error_code,
713 unsigned long address)
715 struct task_struct *tsk;
719 flags = oops_begin();
723 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
725 dump_pagetable(address);
727 tsk->thread.cr2 = address;
728 tsk->thread.trap_nr = X86_TRAP_PF;
729 tsk->thread.error_code = error_code;
731 if (__die("Bad pagetable", regs, error_code))
734 oops_end(flags, regs, sig);
738 no_context(struct pt_regs *regs, unsigned long error_code,
739 unsigned long address, int signal, int si_code)
741 struct task_struct *tsk = current;
745 /* Are we prepared to handle this kernel fault? */
746 if (fixup_exception(regs, X86_TRAP_PF)) {
748 * Any interrupt that takes a fault gets the fixup. This makes
749 * the below recursive fault logic only apply to a faults from
756 * Per the above we're !in_interrupt(), aka. task context.
758 * In this case we need to make sure we're not recursively
759 * faulting through the emulate_vsyscall() logic.
761 if (current->thread.sig_on_uaccess_err && signal) {
762 tsk->thread.trap_nr = X86_TRAP_PF;
763 tsk->thread.error_code = error_code | X86_PF_USER;
764 tsk->thread.cr2 = address;
766 /* XXX: hwpoison faults will set the wrong code. */
767 force_sig_info_fault(signal, si_code, address,
772 * Barring that, we can do the fixup and be happy.
777 #ifdef CONFIG_VMAP_STACK
779 * Stack overflow? During boot, we can fault near the initial
780 * stack in the direct map, but that's not an overflow -- check
781 * that we're in vmalloc space to avoid this.
783 if (is_vmalloc_addr((void *)address) &&
784 (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) ||
785 address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) {
786 unsigned long stack = this_cpu_read(orig_ist.ist[DOUBLEFAULT_STACK]) - sizeof(void *);
788 * We're likely to be running with very little stack space
789 * left. It's plausible that we'd hit this condition but
790 * double-fault even before we get this far, in which case
791 * we're fine: the double-fault handler will deal with it.
793 * We don't want to make it all the way into the oops code
794 * and then double-fault, though, because we're likely to
795 * break the console driver and lose most of the stack dump.
797 asm volatile ("movq %[stack], %%rsp\n\t"
798 "call handle_stack_overflow\n\t"
800 : ASM_CALL_CONSTRAINT
801 : "D" ("kernel stack overflow (page fault)"),
802 "S" (regs), "d" (address),
803 [stack] "rm" (stack));
811 * Valid to do another page fault here, because if this fault
812 * had been triggered by is_prefetch fixup_exception would have
817 * Hall of shame of CPU/BIOS bugs.
819 if (is_prefetch(regs, error_code, address))
822 if (is_errata93(regs, address))
826 * Oops. The kernel tried to access some bad page. We'll have to
827 * terminate things with extreme prejudice:
829 flags = oops_begin();
831 show_fault_oops(regs, error_code, address);
833 if (task_stack_end_corrupted(tsk))
834 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
836 tsk->thread.cr2 = address;
837 tsk->thread.trap_nr = X86_TRAP_PF;
838 tsk->thread.error_code = error_code;
841 if (__die("Oops", regs, error_code))
844 /* Executive summary in case the body of the oops scrolled away */
845 printk(KERN_DEFAULT "CR2: %016lx\n", address);
847 oops_end(flags, regs, sig);
851 * Print out info about fatal segfaults, if the show_unhandled_signals
855 show_signal_msg(struct pt_regs *regs, unsigned long error_code,
856 unsigned long address, struct task_struct *tsk)
858 if (!unhandled_signal(tsk, SIGSEGV))
861 if (!printk_ratelimit())
864 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
865 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
866 tsk->comm, task_pid_nr(tsk), address,
867 (void *)regs->ip, (void *)regs->sp, error_code);
869 print_vma_addr(KERN_CONT " in ", regs->ip);
871 printk(KERN_CONT "\n");
875 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
876 unsigned long address, u32 *pkey, int si_code)
878 struct task_struct *tsk = current;
880 /* User mode accesses just cause a SIGSEGV */
881 if (error_code & X86_PF_USER) {
883 * It's possible to have interrupts off here:
888 * Valid to do another page fault here because this one came
891 if (is_prefetch(regs, error_code, address))
894 if (is_errata100(regs, address))
899 * Instruction fetch faults in the vsyscall page might need
902 if (unlikely((error_code & X86_PF_INSTR) &&
903 ((address & ~0xfff) == VSYSCALL_ADDR))) {
904 if (emulate_vsyscall(regs, address))
910 * To avoid leaking information about the kernel page table
911 * layout, pretend that user-mode accesses to kernel addresses
912 * are always protection faults.
914 if (address >= TASK_SIZE_MAX)
915 error_code |= X86_PF_PROT;
917 if (likely(show_unhandled_signals))
918 show_signal_msg(regs, error_code, address, tsk);
920 tsk->thread.cr2 = address;
921 tsk->thread.error_code = error_code;
922 tsk->thread.trap_nr = X86_TRAP_PF;
924 force_sig_info_fault(SIGSEGV, si_code, address, tsk, pkey, 0);
929 if (is_f00f_bug(regs, address))
932 no_context(regs, error_code, address, SIGSEGV, si_code);
936 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
937 unsigned long address, u32 *pkey)
939 __bad_area_nosemaphore(regs, error_code, address, pkey, SEGV_MAPERR);
943 __bad_area(struct pt_regs *regs, unsigned long error_code,
944 unsigned long address, struct vm_area_struct *vma, int si_code)
946 struct mm_struct *mm = current->mm;
950 pkey = vma_pkey(vma);
953 * Something tried to access memory that isn't in our memory map..
954 * Fix it, but check if it's kernel or user first..
956 up_read(&mm->mmap_sem);
958 __bad_area_nosemaphore(regs, error_code, address,
959 (vma) ? &pkey : NULL, si_code);
963 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
965 __bad_area(regs, error_code, address, NULL, SEGV_MAPERR);
968 static inline bool bad_area_access_from_pkeys(unsigned long error_code,
969 struct vm_area_struct *vma)
971 /* This code is always called on the current mm */
972 bool foreign = false;
974 if (!boot_cpu_has(X86_FEATURE_OSPKE))
976 if (error_code & X86_PF_PK)
978 /* this checks permission keys on the VMA: */
979 if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
980 (error_code & X86_PF_INSTR), foreign))
986 bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
987 unsigned long address, struct vm_area_struct *vma)
990 * This OSPKE check is not strictly necessary at runtime.
991 * But, doing it this way allows compiler optimizations
992 * if pkeys are compiled out.
994 if (bad_area_access_from_pkeys(error_code, vma))
995 __bad_area(regs, error_code, address, vma, SEGV_PKUERR);
997 __bad_area(regs, error_code, address, vma, SEGV_ACCERR);
1001 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
1002 u32 *pkey, unsigned int fault)
1004 struct task_struct *tsk = current;
1005 int code = BUS_ADRERR;
1007 /* Kernel mode? Handle exceptions or die: */
1008 if (!(error_code & X86_PF_USER)) {
1009 no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
1013 /* User-space => ok to do another page fault: */
1014 if (is_prefetch(regs, error_code, address))
1017 tsk->thread.cr2 = address;
1018 tsk->thread.error_code = error_code;
1019 tsk->thread.trap_nr = X86_TRAP_PF;
1021 #ifdef CONFIG_MEMORY_FAILURE
1022 if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
1024 "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
1025 tsk->comm, tsk->pid, address);
1026 code = BUS_MCEERR_AR;
1029 force_sig_info_fault(SIGBUS, code, address, tsk, pkey, fault);
1032 static noinline void
1033 mm_fault_error(struct pt_regs *regs, unsigned long error_code,
1034 unsigned long address, u32 *pkey, unsigned int fault)
1036 if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) {
1037 no_context(regs, error_code, address, 0, 0);
1041 if (fault & VM_FAULT_OOM) {
1042 /* Kernel mode? Handle exceptions or die: */
1043 if (!(error_code & X86_PF_USER)) {
1044 no_context(regs, error_code, address,
1045 SIGSEGV, SEGV_MAPERR);
1050 * We ran out of memory, call the OOM killer, and return the
1051 * userspace (which will retry the fault, or kill us if we got
1054 pagefault_out_of_memory();
1056 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
1057 VM_FAULT_HWPOISON_LARGE))
1058 do_sigbus(regs, error_code, address, pkey, fault);
1059 else if (fault & VM_FAULT_SIGSEGV)
1060 bad_area_nosemaphore(regs, error_code, address, pkey);
1066 static int spurious_fault_check(unsigned long error_code, pte_t *pte)
1068 if ((error_code & X86_PF_WRITE) && !pte_write(*pte))
1071 if ((error_code & X86_PF_INSTR) && !pte_exec(*pte))
1074 * Note: We do not do lazy flushing on protection key
1075 * changes, so no spurious fault will ever set X86_PF_PK.
1077 if ((error_code & X86_PF_PK))
1084 * Handle a spurious fault caused by a stale TLB entry.
1086 * This allows us to lazily refresh the TLB when increasing the
1087 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
1088 * eagerly is very expensive since that implies doing a full
1089 * cross-processor TLB flush, even if no stale TLB entries exist
1090 * on other processors.
1092 * Spurious faults may only occur if the TLB contains an entry with
1093 * fewer permission than the page table entry. Non-present (P = 0)
1094 * and reserved bit (R = 1) faults are never spurious.
1096 * There are no security implications to leaving a stale TLB when
1097 * increasing the permissions on a page.
1099 * Returns non-zero if a spurious fault was handled, zero otherwise.
1101 * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
1102 * (Optional Invalidation).
1105 spurious_fault(unsigned long error_code, unsigned long address)
1115 * Only writes to RO or instruction fetches from NX may cause
1118 * These could be from user or supervisor accesses but the TLB
1119 * is only lazily flushed after a kernel mapping protection
1120 * change, so user accesses are not expected to cause spurious
1123 if (error_code != (X86_PF_WRITE | X86_PF_PROT) &&
1124 error_code != (X86_PF_INSTR | X86_PF_PROT))
1127 pgd = init_mm.pgd + pgd_index(address);
1128 if (!pgd_present(*pgd))
1131 p4d = p4d_offset(pgd, address);
1132 if (!p4d_present(*p4d))
1135 if (p4d_large(*p4d))
1136 return spurious_fault_check(error_code, (pte_t *) p4d);
1138 pud = pud_offset(p4d, address);
1139 if (!pud_present(*pud))
1142 if (pud_large(*pud))
1143 return spurious_fault_check(error_code, (pte_t *) pud);
1145 pmd = pmd_offset(pud, address);
1146 if (!pmd_present(*pmd))
1149 if (pmd_large(*pmd))
1150 return spurious_fault_check(error_code, (pte_t *) pmd);
1152 pte = pte_offset_kernel(pmd, address);
1153 if (!pte_present(*pte))
1156 ret = spurious_fault_check(error_code, pte);
1161 * Make sure we have permissions in PMD.
1162 * If not, then there's a bug in the page tables:
1164 ret = spurious_fault_check(error_code, (pte_t *) pmd);
1165 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
1169 NOKPROBE_SYMBOL(spurious_fault);
1171 int show_unhandled_signals = 1;
1174 access_error(unsigned long error_code, struct vm_area_struct *vma)
1176 /* This is only called for the current mm, so: */
1177 bool foreign = false;
1180 * Read or write was blocked by protection keys. This is
1181 * always an unconditional error and can never result in
1182 * a follow-up action to resolve the fault, like a COW.
1184 if (error_code & X86_PF_PK)
1188 * Make sure to check the VMA so that we do not perform
1189 * faults just to hit a X86_PF_PK as soon as we fill in a
1192 if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
1193 (error_code & X86_PF_INSTR), foreign))
1196 if (error_code & X86_PF_WRITE) {
1197 /* write, present and write, not present: */
1198 if (unlikely(!(vma->vm_flags & VM_WRITE)))
1203 /* read, present: */
1204 if (unlikely(error_code & X86_PF_PROT))
1207 /* read, not present: */
1208 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
1214 static int fault_in_kernel_space(unsigned long address)
1216 return address >= TASK_SIZE_MAX;
1219 static inline bool smap_violation(int error_code, struct pt_regs *regs)
1221 if (!IS_ENABLED(CONFIG_X86_SMAP))
1224 if (!static_cpu_has(X86_FEATURE_SMAP))
1227 if (error_code & X86_PF_USER)
1230 if (!user_mode(regs) && (regs->flags & X86_EFLAGS_AC))
1237 * This routine handles page faults. It determines the address,
1238 * and the problem, and then passes it off to one of the appropriate
1241 static noinline void
1242 __do_page_fault(struct pt_regs *regs, unsigned long error_code,
1243 unsigned long address)
1245 struct vm_area_struct *vma;
1246 struct task_struct *tsk;
1247 struct mm_struct *mm;
1248 int fault, major = 0;
1249 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
1256 * Detect and handle instructions that would cause a page fault for
1257 * both a tracked kernel page and a userspace page.
1259 if (kmemcheck_active(regs))
1260 kmemcheck_hide(regs);
1261 prefetchw(&mm->mmap_sem);
1263 if (unlikely(kmmio_fault(regs, address)))
1267 * We fault-in kernel-space virtual memory on-demand. The
1268 * 'reference' page table is init_mm.pgd.
1270 * NOTE! We MUST NOT take any locks for this case. We may
1271 * be in an interrupt or a critical region, and should
1272 * only copy the information from the master page table,
1275 * This verifies that the fault happens in kernel space
1276 * (error_code & 4) == 0, and that the fault was not a
1277 * protection error (error_code & 9) == 0.
1279 if (unlikely(fault_in_kernel_space(address))) {
1280 if (!(error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) {
1281 if (vmalloc_fault(address) >= 0)
1284 if (kmemcheck_fault(regs, address, error_code))
1288 /* Can handle a stale RO->RW TLB: */
1289 if (spurious_fault(error_code, address))
1292 /* kprobes don't want to hook the spurious faults: */
1293 if (kprobes_fault(regs))
1296 * Don't take the mm semaphore here. If we fixup a prefetch
1297 * fault we could otherwise deadlock:
1299 bad_area_nosemaphore(regs, error_code, address, NULL);
1304 /* kprobes don't want to hook the spurious faults: */
1305 if (unlikely(kprobes_fault(regs)))
1308 if (unlikely(error_code & X86_PF_RSVD))
1309 pgtable_bad(regs, error_code, address);
1311 if (unlikely(smap_violation(error_code, regs))) {
1312 bad_area_nosemaphore(regs, error_code, address, NULL);
1317 * If we're in an interrupt, have no user context or are running
1318 * in a region with pagefaults disabled then we must not take the fault
1320 if (unlikely(faulthandler_disabled() || !mm)) {
1321 bad_area_nosemaphore(regs, error_code, address, NULL);
1326 * It's safe to allow irq's after cr2 has been saved and the
1327 * vmalloc fault has been handled.
1329 * User-mode registers count as a user access even for any
1330 * potential system fault or CPU buglet:
1332 if (user_mode(regs)) {
1334 error_code |= X86_PF_USER;
1335 flags |= FAULT_FLAG_USER;
1337 if (regs->flags & X86_EFLAGS_IF)
1341 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
1343 if (error_code & X86_PF_WRITE)
1344 flags |= FAULT_FLAG_WRITE;
1345 if (error_code & X86_PF_INSTR)
1346 flags |= FAULT_FLAG_INSTRUCTION;
1349 * When running in the kernel we expect faults to occur only to
1350 * addresses in user space. All other faults represent errors in
1351 * the kernel and should generate an OOPS. Unfortunately, in the
1352 * case of an erroneous fault occurring in a code path which already
1353 * holds mmap_sem we will deadlock attempting to validate the fault
1354 * against the address space. Luckily the kernel only validly
1355 * references user space from well defined areas of code, which are
1356 * listed in the exceptions table.
1358 * As the vast majority of faults will be valid we will only perform
1359 * the source reference check when there is a possibility of a
1360 * deadlock. Attempt to lock the address space, if we cannot we then
1361 * validate the source. If this is invalid we can skip the address
1362 * space check, thus avoiding the deadlock:
1364 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1365 if (!(error_code & X86_PF_USER) &&
1366 !search_exception_tables(regs->ip)) {
1367 bad_area_nosemaphore(regs, error_code, address, NULL);
1371 down_read(&mm->mmap_sem);
1374 * The above down_read_trylock() might have succeeded in
1375 * which case we'll have missed the might_sleep() from
1381 vma = find_vma(mm, address);
1382 if (unlikely(!vma)) {
1383 bad_area(regs, error_code, address);
1386 if (likely(vma->vm_start <= address))
1388 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1389 bad_area(regs, error_code, address);
1392 if (error_code & X86_PF_USER) {
1394 * Accessing the stack below %sp is always a bug.
1395 * The large cushion allows instructions like enter
1396 * and pusha to work. ("enter $65535, $31" pushes
1397 * 32 pointers and then decrements %sp by 65535.)
1399 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1400 bad_area(regs, error_code, address);
1404 if (unlikely(expand_stack(vma, address))) {
1405 bad_area(regs, error_code, address);
1410 * Ok, we have a good vm_area for this memory access, so
1411 * we can handle it..
1414 if (unlikely(access_error(error_code, vma))) {
1415 bad_area_access_error(regs, error_code, address, vma);
1420 * If for any reason at all we couldn't handle the fault,
1421 * make sure we exit gracefully rather than endlessly redo
1422 * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if
1423 * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked.
1425 * Note that handle_userfault() may also release and reacquire mmap_sem
1426 * (and not return with VM_FAULT_RETRY), when returning to userland to
1427 * repeat the page fault later with a VM_FAULT_NOPAGE retval
1428 * (potentially after handling any pending signal during the return to
1429 * userland). The return to userland is identified whenever
1430 * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags.
1431 * Thus we have to be careful about not touching vma after handling the
1432 * fault, so we read the pkey beforehand.
1434 pkey = vma_pkey(vma);
1435 fault = handle_mm_fault(vma, address, flags);
1436 major |= fault & VM_FAULT_MAJOR;
1439 * If we need to retry the mmap_sem has already been released,
1440 * and if there is a fatal signal pending there is no guarantee
1441 * that we made any progress. Handle this case first.
1443 if (unlikely(fault & VM_FAULT_RETRY)) {
1444 /* Retry at most once */
1445 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1446 flags &= ~FAULT_FLAG_ALLOW_RETRY;
1447 flags |= FAULT_FLAG_TRIED;
1448 if (!fatal_signal_pending(tsk))
1452 /* User mode? Just return to handle the fatal exception */
1453 if (flags & FAULT_FLAG_USER)
1456 /* Not returning to user mode? Handle exceptions or die: */
1457 no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
1461 up_read(&mm->mmap_sem);
1462 if (unlikely(fault & VM_FAULT_ERROR)) {
1463 mm_fault_error(regs, error_code, address, &pkey, fault);
1468 * Major/minor page fault accounting. If any of the events
1469 * returned VM_FAULT_MAJOR, we account it as a major fault.
1473 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
1476 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
1479 check_v8086_mode(regs, address, tsk);
1481 NOKPROBE_SYMBOL(__do_page_fault);
1483 static nokprobe_inline void
1484 trace_page_fault_entries(unsigned long address, struct pt_regs *regs,
1485 unsigned long error_code)
1487 if (user_mode(regs))
1488 trace_page_fault_user(address, regs, error_code);
1490 trace_page_fault_kernel(address, regs, error_code);
1494 * We must have this function blacklisted from kprobes, tagged with notrace
1495 * and call read_cr2() before calling anything else. To avoid calling any
1496 * kind of tracing machinery before we've observed the CR2 value.
1498 * exception_{enter,exit}() contains all sorts of tracepoints.
1500 dotraplinkage void notrace
1501 do_page_fault(struct pt_regs *regs, unsigned long error_code)
1503 unsigned long address = read_cr2(); /* Get the faulting address */
1504 enum ctx_state prev_state;
1506 prev_state = exception_enter();
1507 if (trace_pagefault_enabled())
1508 trace_page_fault_entries(address, regs, error_code);
1510 __do_page_fault(regs, error_code, address);
1511 exception_exit(prev_state);
1513 NOKPROBE_SYMBOL(do_page_fault);