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/memblock.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() */
19 #include <linux/efi.h> /* efi_recover_from_page_fault()*/
20 #include <linux/mm_types.h>
22 #include <asm/cpufeature.h> /* boot_cpu_has, ... */
23 #include <asm/traps.h> /* dotraplinkage, ... */
24 #include <asm/pgalloc.h> /* pgd_*(), ... */
25 #include <asm/fixmap.h> /* VSYSCALL_ADDR */
26 #include <asm/vsyscall.h> /* emulate_vsyscall */
27 #include <asm/vm86.h> /* struct vm86 */
28 #include <asm/mmu_context.h> /* vma_pkey() */
29 #include <asm/efi.h> /* efi_recover_from_page_fault()*/
31 #define CREATE_TRACE_POINTS
32 #include <asm/trace/exceptions.h>
35 * Returns 0 if mmiotrace is disabled, or if the fault is not
36 * handled by mmiotrace:
38 static nokprobe_inline
int
39 kmmio_fault(struct pt_regs
*regs
, unsigned long addr
)
41 if (unlikely(is_kmmio_active()))
42 if (kmmio_handler(regs
, addr
) == 1)
47 static nokprobe_inline
int kprobes_fault(struct pt_regs
*regs
)
49 if (!kprobes_built_in())
54 * To be potentially processing a kprobe fault and to be allowed to call
55 * kprobe_running(), we have to be non-preemptible.
59 if (!kprobe_running())
61 return kprobe_fault_handler(regs
, X86_TRAP_PF
);
69 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
70 * Check that here and ignore it.
74 * Sometimes the CPU reports invalid exceptions on prefetch.
75 * Check that here and ignore it.
77 * Opcode checker based on code by Richard Brunner.
80 check_prefetch_opcode(struct pt_regs
*regs
, unsigned char *instr
,
81 unsigned char opcode
, int *prefetch
)
83 unsigned char instr_hi
= opcode
& 0xf0;
84 unsigned char instr_lo
= opcode
& 0x0f;
90 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
91 * In X86_64 long mode, the CPU will signal invalid
92 * opcode if some of these prefixes are present so
93 * X86_64 will never get here anyway
95 return ((instr_lo
& 7) == 0x6);
99 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
100 * Need to figure out under what instruction mode the
101 * instruction was issued. Could check the LDT for lm,
102 * but for now it's good enough to assume that long
103 * mode only uses well known segments or kernel.
105 return (!user_mode(regs
) || user_64bit_mode(regs
));
108 /* 0x64 thru 0x67 are valid prefixes in all modes. */
109 return (instr_lo
& 0xC) == 0x4;
111 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
112 return !instr_lo
|| (instr_lo
>>1) == 1;
114 /* Prefetch instruction is 0x0F0D or 0x0F18 */
115 if (probe_kernel_address(instr
, opcode
))
118 *prefetch
= (instr_lo
== 0xF) &&
119 (opcode
== 0x0D || opcode
== 0x18);
127 is_prefetch(struct pt_regs
*regs
, unsigned long error_code
, unsigned long addr
)
129 unsigned char *max_instr
;
130 unsigned char *instr
;
134 * If it was a exec (instruction fetch) fault on NX page, then
135 * do not ignore the fault:
137 if (error_code
& X86_PF_INSTR
)
140 instr
= (void *)convert_ip_to_linear(current
, regs
);
141 max_instr
= instr
+ 15;
143 if (user_mode(regs
) && instr
>= (unsigned char *)TASK_SIZE_MAX
)
146 while (instr
< max_instr
) {
147 unsigned char opcode
;
149 if (probe_kernel_address(instr
, opcode
))
154 if (!check_prefetch_opcode(regs
, instr
, opcode
, &prefetch
))
160 DEFINE_SPINLOCK(pgd_lock
);
164 static inline pmd_t
*vmalloc_sync_one(pgd_t
*pgd
, unsigned long address
)
166 unsigned index
= pgd_index(address
);
173 pgd_k
= init_mm
.pgd
+ index
;
175 if (!pgd_present(*pgd_k
))
179 * set_pgd(pgd, *pgd_k); here would be useless on PAE
180 * and redundant with the set_pmd() on non-PAE. As would
183 p4d
= p4d_offset(pgd
, address
);
184 p4d_k
= p4d_offset(pgd_k
, address
);
185 if (!p4d_present(*p4d_k
))
188 pud
= pud_offset(p4d
, address
);
189 pud_k
= pud_offset(p4d_k
, address
);
190 if (!pud_present(*pud_k
))
193 pmd
= pmd_offset(pud
, address
);
194 pmd_k
= pmd_offset(pud_k
, address
);
195 if (!pmd_present(*pmd_k
))
198 if (!pmd_present(*pmd
))
199 set_pmd(pmd
, *pmd_k
);
201 BUG_ON(pmd_page(*pmd
) != pmd_page(*pmd_k
));
206 void vmalloc_sync_all(void)
208 unsigned long address
;
210 if (SHARED_KERNEL_PMD
)
213 for (address
= VMALLOC_START
& PMD_MASK
;
214 address
>= TASK_SIZE_MAX
&& address
< FIXADDR_TOP
;
215 address
+= PMD_SIZE
) {
218 spin_lock(&pgd_lock
);
219 list_for_each_entry(page
, &pgd_list
, lru
) {
220 spinlock_t
*pgt_lock
;
223 /* the pgt_lock only for Xen */
224 pgt_lock
= &pgd_page_get_mm(page
)->page_table_lock
;
227 ret
= vmalloc_sync_one(page_address(page
), address
);
228 spin_unlock(pgt_lock
);
233 spin_unlock(&pgd_lock
);
240 * Handle a fault on the vmalloc or module mapping area
242 static noinline
int vmalloc_fault(unsigned long address
)
244 unsigned long pgd_paddr
;
248 /* Make sure we are in vmalloc area: */
249 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
253 * Synchronize this task's top level page-table
254 * with the 'reference' page table.
256 * Do _not_ use "current" here. We might be inside
257 * an interrupt in the middle of a task switch..
259 pgd_paddr
= read_cr3_pa();
260 pmd_k
= vmalloc_sync_one(__va(pgd_paddr
), address
);
264 if (pmd_large(*pmd_k
))
267 pte_k
= pte_offset_kernel(pmd_k
, address
);
268 if (!pte_present(*pte_k
))
273 NOKPROBE_SYMBOL(vmalloc_fault
);
276 * Did it hit the DOS screen memory VA from vm86 mode?
279 check_v8086_mode(struct pt_regs
*regs
, unsigned long address
,
280 struct task_struct
*tsk
)
285 if (!v8086_mode(regs
) || !tsk
->thread
.vm86
)
288 bit
= (address
- 0xA0000) >> PAGE_SHIFT
;
290 tsk
->thread
.vm86
->screen_bitmap
|= 1 << bit
;
294 static bool low_pfn(unsigned long pfn
)
296 return pfn
< max_low_pfn
;
299 static void dump_pagetable(unsigned long address
)
301 pgd_t
*base
= __va(read_cr3_pa());
302 pgd_t
*pgd
= &base
[pgd_index(address
)];
308 #ifdef CONFIG_X86_PAE
309 pr_info("*pdpt = %016Lx ", pgd_val(*pgd
));
310 if (!low_pfn(pgd_val(*pgd
) >> PAGE_SHIFT
) || !pgd_present(*pgd
))
312 #define pr_pde pr_cont
314 #define pr_pde pr_info
316 p4d
= p4d_offset(pgd
, address
);
317 pud
= pud_offset(p4d
, address
);
318 pmd
= pmd_offset(pud
, address
);
319 pr_pde("*pde = %0*Lx ", sizeof(*pmd
) * 2, (u64
)pmd_val(*pmd
));
323 * We must not directly access the pte in the highpte
324 * case if the page table is located in highmem.
325 * And let's rather not kmap-atomic the pte, just in case
326 * it's allocated already:
328 if (!low_pfn(pmd_pfn(*pmd
)) || !pmd_present(*pmd
) || pmd_large(*pmd
))
331 pte
= pte_offset_kernel(pmd
, address
);
332 pr_cont("*pte = %0*Lx ", sizeof(*pte
) * 2, (u64
)pte_val(*pte
));
337 #else /* CONFIG_X86_64: */
339 void vmalloc_sync_all(void)
341 sync_global_pgds(VMALLOC_START
& PGDIR_MASK
, VMALLOC_END
);
347 * Handle a fault on the vmalloc area
349 static noinline
int vmalloc_fault(unsigned long address
)
357 /* Make sure we are in vmalloc area: */
358 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
361 WARN_ON_ONCE(in_nmi());
364 * Copy kernel mappings over when needed. This can also
365 * happen within a race in page table update. In the later
368 pgd
= (pgd_t
*)__va(read_cr3_pa()) + pgd_index(address
);
369 pgd_k
= pgd_offset_k(address
);
370 if (pgd_none(*pgd_k
))
373 if (pgtable_l5_enabled()) {
374 if (pgd_none(*pgd
)) {
375 set_pgd(pgd
, *pgd_k
);
376 arch_flush_lazy_mmu_mode();
378 BUG_ON(pgd_page_vaddr(*pgd
) != pgd_page_vaddr(*pgd_k
));
382 /* With 4-level paging, copying happens on the p4d level. */
383 p4d
= p4d_offset(pgd
, address
);
384 p4d_k
= p4d_offset(pgd_k
, address
);
385 if (p4d_none(*p4d_k
))
388 if (p4d_none(*p4d
) && !pgtable_l5_enabled()) {
389 set_p4d(p4d
, *p4d_k
);
390 arch_flush_lazy_mmu_mode();
392 BUG_ON(p4d_pfn(*p4d
) != p4d_pfn(*p4d_k
));
395 BUILD_BUG_ON(CONFIG_PGTABLE_LEVELS
< 4);
397 pud
= pud_offset(p4d
, address
);
404 pmd
= pmd_offset(pud
, address
);
411 pte
= pte_offset_kernel(pmd
, address
);
412 if (!pte_present(*pte
))
417 NOKPROBE_SYMBOL(vmalloc_fault
);
419 #ifdef CONFIG_CPU_SUP_AMD
420 static const char errata93_warning
[] =
422 "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
423 "******* Working around it, but it may cause SEGVs or burn power.\n"
424 "******* Please consider a BIOS update.\n"
425 "******* Disabling USB legacy in the BIOS may also help.\n";
429 * No vm86 mode in 64-bit mode:
432 check_v8086_mode(struct pt_regs
*regs
, unsigned long address
,
433 struct task_struct
*tsk
)
437 static int bad_address(void *p
)
441 return probe_kernel_address((unsigned long *)p
, dummy
);
444 static void dump_pagetable(unsigned long address
)
446 pgd_t
*base
= __va(read_cr3_pa());
447 pgd_t
*pgd
= base
+ pgd_index(address
);
453 if (bad_address(pgd
))
456 pr_info("PGD %lx ", pgd_val(*pgd
));
458 if (!pgd_present(*pgd
))
461 p4d
= p4d_offset(pgd
, address
);
462 if (bad_address(p4d
))
465 pr_cont("P4D %lx ", p4d_val(*p4d
));
466 if (!p4d_present(*p4d
) || p4d_large(*p4d
))
469 pud
= pud_offset(p4d
, address
);
470 if (bad_address(pud
))
473 pr_cont("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 pr_cont("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 pr_cont("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
)
515 #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
516 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
517 || boot_cpu_data
.x86
!= 0xf)
520 if (address
!= regs
->ip
)
523 if ((address
>> 32) != 0)
526 address
|= 0xffffffffUL
<< 32;
527 if ((address
>= (u64
)_stext
&& address
<= (u64
)_etext
) ||
528 (address
>= MODULES_VADDR
&& address
<= MODULES_END
)) {
529 printk_once(errata93_warning
);
538 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
539 * to illegal addresses >4GB.
541 * We catch this in the page fault handler because these addresses
542 * are not reachable. Just detect this case and return. Any code
543 * segment in LDT is compatibility mode.
545 static int is_errata100(struct pt_regs
*regs
, unsigned long address
)
548 if ((regs
->cs
== __USER32_CS
|| (regs
->cs
& (1<<2))) && (address
>> 32))
554 static int is_f00f_bug(struct pt_regs
*regs
, unsigned long address
)
556 #ifdef CONFIG_X86_F00F_BUG
560 * Pentium F0 0F C7 C8 bug workaround:
562 if (boot_cpu_has_bug(X86_BUG_F00F
)) {
563 nr
= (address
- idt_descr
.address
) >> 3;
566 do_invalid_op(regs
, 0);
575 show_fault_oops(struct pt_regs
*regs
, unsigned long error_code
,
576 unsigned long address
)
578 if (!oops_may_print())
581 if (error_code
& X86_PF_INSTR
) {
586 pgd
= __va(read_cr3_pa());
587 pgd
+= pgd_index(address
);
589 pte
= lookup_address_in_pgd(pgd
, address
, &level
);
591 if (pte
&& pte_present(*pte
) && !pte_exec(*pte
))
592 pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n",
593 from_kuid(&init_user_ns
, current_uid()));
594 if (pte
&& pte_present(*pte
) && pte_exec(*pte
) &&
595 (pgd_flags(*pgd
) & _PAGE_USER
) &&
596 (__read_cr4() & X86_CR4_SMEP
))
597 pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n",
598 from_kuid(&init_user_ns
, current_uid()));
601 pr_alert("BUG: unable to handle kernel %s at %px\n",
602 address
< PAGE_SIZE
? "NULL pointer dereference" : "paging request",
605 dump_pagetable(address
);
609 pgtable_bad(struct pt_regs
*regs
, unsigned long error_code
,
610 unsigned long address
)
612 struct task_struct
*tsk
;
616 flags
= oops_begin();
620 printk(KERN_ALERT
"%s: Corrupted page table at address %lx\n",
622 dump_pagetable(address
);
624 tsk
->thread
.cr2
= address
;
625 tsk
->thread
.trap_nr
= X86_TRAP_PF
;
626 tsk
->thread
.error_code
= error_code
;
628 if (__die("Bad pagetable", regs
, error_code
))
631 oops_end(flags
, regs
, sig
);
635 no_context(struct pt_regs
*regs
, unsigned long error_code
,
636 unsigned long address
, int signal
, int si_code
)
638 struct task_struct
*tsk
= current
;
642 /* Are we prepared to handle this kernel fault? */
643 if (fixup_exception(regs
, X86_TRAP_PF
, error_code
, address
)) {
645 * Any interrupt that takes a fault gets the fixup. This makes
646 * the below recursive fault logic only apply to a faults from
653 * Per the above we're !in_interrupt(), aka. task context.
655 * In this case we need to make sure we're not recursively
656 * faulting through the emulate_vsyscall() logic.
658 if (current
->thread
.sig_on_uaccess_err
&& signal
) {
659 tsk
->thread
.trap_nr
= X86_TRAP_PF
;
660 tsk
->thread
.error_code
= error_code
| X86_PF_USER
;
661 tsk
->thread
.cr2
= address
;
663 /* XXX: hwpoison faults will set the wrong code. */
664 force_sig_fault(signal
, si_code
, (void __user
*)address
,
669 * Barring that, we can do the fixup and be happy.
674 #ifdef CONFIG_VMAP_STACK
676 * Stack overflow? During boot, we can fault near the initial
677 * stack in the direct map, but that's not an overflow -- check
678 * that we're in vmalloc space to avoid this.
680 if (is_vmalloc_addr((void *)address
) &&
681 (((unsigned long)tsk
->stack
- 1 - address
< PAGE_SIZE
) ||
682 address
- ((unsigned long)tsk
->stack
+ THREAD_SIZE
) < PAGE_SIZE
)) {
683 unsigned long stack
= this_cpu_read(orig_ist
.ist
[DOUBLEFAULT_STACK
]) - sizeof(void *);
685 * We're likely to be running with very little stack space
686 * left. It's plausible that we'd hit this condition but
687 * double-fault even before we get this far, in which case
688 * we're fine: the double-fault handler will deal with it.
690 * We don't want to make it all the way into the oops code
691 * and then double-fault, though, because we're likely to
692 * break the console driver and lose most of the stack dump.
694 asm volatile ("movq %[stack], %%rsp\n\t"
695 "call handle_stack_overflow\n\t"
697 : ASM_CALL_CONSTRAINT
698 : "D" ("kernel stack overflow (page fault)"),
699 "S" (regs
), "d" (address
),
700 [stack
] "rm" (stack
));
708 * Valid to do another page fault here, because if this fault
709 * had been triggered by is_prefetch fixup_exception would have
714 * Hall of shame of CPU/BIOS bugs.
716 if (is_prefetch(regs
, error_code
, address
))
719 if (is_errata93(regs
, address
))
723 * Buggy firmware could access regions which might page fault, try to
724 * recover from such faults.
726 if (IS_ENABLED(CONFIG_EFI
))
727 efi_recover_from_page_fault(address
);
730 * Oops. The kernel tried to access some bad page. We'll have to
731 * terminate things with extreme prejudice:
733 flags
= oops_begin();
735 show_fault_oops(regs
, error_code
, address
);
737 if (task_stack_end_corrupted(tsk
))
738 printk(KERN_EMERG
"Thread overran stack, or stack corrupted\n");
740 tsk
->thread
.cr2
= address
;
741 tsk
->thread
.trap_nr
= X86_TRAP_PF
;
742 tsk
->thread
.error_code
= error_code
;
745 if (__die("Oops", regs
, error_code
))
748 /* Executive summary in case the body of the oops scrolled away */
749 printk(KERN_DEFAULT
"CR2: %016lx\n", address
);
751 oops_end(flags
, regs
, sig
);
755 * Print out info about fatal segfaults, if the show_unhandled_signals
759 show_signal_msg(struct pt_regs
*regs
, unsigned long error_code
,
760 unsigned long address
, struct task_struct
*tsk
)
762 const char *loglvl
= task_pid_nr(tsk
) > 1 ? KERN_INFO
: KERN_EMERG
;
764 if (!unhandled_signal(tsk
, SIGSEGV
))
767 if (!printk_ratelimit())
770 printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx",
771 loglvl
, tsk
->comm
, task_pid_nr(tsk
), address
,
772 (void *)regs
->ip
, (void *)regs
->sp
, error_code
);
774 print_vma_addr(KERN_CONT
" in ", regs
->ip
);
776 printk(KERN_CONT
"\n");
778 show_opcodes(regs
, loglvl
);
782 * The (legacy) vsyscall page is the long page in the kernel portion
783 * of the address space that has user-accessible permissions.
785 static bool is_vsyscall_vaddr(unsigned long vaddr
)
787 return unlikely((vaddr
& PAGE_MASK
) == VSYSCALL_ADDR
);
791 __bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
792 unsigned long address
, u32 pkey
, int si_code
)
794 struct task_struct
*tsk
= current
;
796 /* User mode accesses just cause a SIGSEGV */
797 if (error_code
& X86_PF_USER
) {
799 * It's possible to have interrupts off here:
804 * Valid to do another page fault here because this one came
807 if (is_prefetch(regs
, error_code
, address
))
810 if (is_errata100(regs
, address
))
814 * To avoid leaking information about the kernel page table
815 * layout, pretend that user-mode accesses to kernel addresses
816 * are always protection faults.
818 if (address
>= TASK_SIZE_MAX
)
819 error_code
|= X86_PF_PROT
;
821 if (likely(show_unhandled_signals
))
822 show_signal_msg(regs
, error_code
, address
, tsk
);
824 tsk
->thread
.cr2
= address
;
825 tsk
->thread
.error_code
= error_code
;
826 tsk
->thread
.trap_nr
= X86_TRAP_PF
;
828 if (si_code
== SEGV_PKUERR
)
829 force_sig_pkuerr((void __user
*)address
, pkey
);
831 force_sig_fault(SIGSEGV
, si_code
, (void __user
*)address
, tsk
);
836 if (is_f00f_bug(regs
, address
))
839 no_context(regs
, error_code
, address
, SIGSEGV
, si_code
);
843 bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
844 unsigned long address
)
846 __bad_area_nosemaphore(regs
, error_code
, address
, 0, SEGV_MAPERR
);
850 __bad_area(struct pt_regs
*regs
, unsigned long error_code
,
851 unsigned long address
, u32 pkey
, int si_code
)
853 struct mm_struct
*mm
= current
->mm
;
855 * Something tried to access memory that isn't in our memory map..
856 * Fix it, but check if it's kernel or user first..
858 up_read(&mm
->mmap_sem
);
860 __bad_area_nosemaphore(regs
, error_code
, address
, pkey
, si_code
);
864 bad_area(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
)
866 __bad_area(regs
, error_code
, address
, 0, SEGV_MAPERR
);
869 static inline bool bad_area_access_from_pkeys(unsigned long error_code
,
870 struct vm_area_struct
*vma
)
872 /* This code is always called on the current mm */
873 bool foreign
= false;
875 if (!boot_cpu_has(X86_FEATURE_OSPKE
))
877 if (error_code
& X86_PF_PK
)
879 /* this checks permission keys on the VMA: */
880 if (!arch_vma_access_permitted(vma
, (error_code
& X86_PF_WRITE
),
881 (error_code
& X86_PF_INSTR
), foreign
))
887 bad_area_access_error(struct pt_regs
*regs
, unsigned long error_code
,
888 unsigned long address
, struct vm_area_struct
*vma
)
891 * This OSPKE check is not strictly necessary at runtime.
892 * But, doing it this way allows compiler optimizations
893 * if pkeys are compiled out.
895 if (bad_area_access_from_pkeys(error_code
, vma
)) {
897 * A protection key fault means that the PKRU value did not allow
898 * access to some PTE. Userspace can figure out what PKRU was
899 * from the XSAVE state. This function captures the pkey from
900 * the vma and passes it to userspace so userspace can discover
901 * which protection key was set on the PTE.
903 * If we get here, we know that the hardware signaled a X86_PF_PK
904 * fault and that there was a VMA once we got in the fault
905 * handler. It does *not* guarantee that the VMA we find here
906 * was the one that we faulted on.
908 * 1. T1 : mprotect_key(foo, PAGE_SIZE, pkey=4);
909 * 2. T1 : set PKRU to deny access to pkey=4, touches page
911 * 4. T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
912 * 5. T1 : enters fault handler, takes mmap_sem, etc...
913 * 6. T1 : reaches here, sees vma_pkey(vma)=5, when we really
914 * faulted on a pte with its pkey=4.
916 u32 pkey
= vma_pkey(vma
);
918 __bad_area(regs
, error_code
, address
, pkey
, SEGV_PKUERR
);
920 __bad_area(regs
, error_code
, address
, 0, SEGV_ACCERR
);
925 do_sigbus(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
,
928 struct task_struct
*tsk
= current
;
930 /* Kernel mode? Handle exceptions or die: */
931 if (!(error_code
& X86_PF_USER
)) {
932 no_context(regs
, error_code
, address
, SIGBUS
, BUS_ADRERR
);
936 /* User-space => ok to do another page fault: */
937 if (is_prefetch(regs
, error_code
, address
))
940 tsk
->thread
.cr2
= address
;
941 tsk
->thread
.error_code
= error_code
;
942 tsk
->thread
.trap_nr
= X86_TRAP_PF
;
944 #ifdef CONFIG_MEMORY_FAILURE
945 if (fault
& (VM_FAULT_HWPOISON
|VM_FAULT_HWPOISON_LARGE
)) {
949 "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
950 tsk
->comm
, tsk
->pid
, address
);
951 if (fault
& VM_FAULT_HWPOISON_LARGE
)
952 lsb
= hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault
));
953 if (fault
& VM_FAULT_HWPOISON
)
955 force_sig_mceerr(BUS_MCEERR_AR
, (void __user
*)address
, lsb
, tsk
);
959 force_sig_fault(SIGBUS
, BUS_ADRERR
, (void __user
*)address
, tsk
);
963 mm_fault_error(struct pt_regs
*regs
, unsigned long error_code
,
964 unsigned long address
, vm_fault_t fault
)
966 if (fatal_signal_pending(current
) && !(error_code
& X86_PF_USER
)) {
967 no_context(regs
, error_code
, address
, 0, 0);
971 if (fault
& VM_FAULT_OOM
) {
972 /* Kernel mode? Handle exceptions or die: */
973 if (!(error_code
& X86_PF_USER
)) {
974 no_context(regs
, error_code
, address
,
975 SIGSEGV
, SEGV_MAPERR
);
980 * We ran out of memory, call the OOM killer, and return the
981 * userspace (which will retry the fault, or kill us if we got
984 pagefault_out_of_memory();
986 if (fault
& (VM_FAULT_SIGBUS
|VM_FAULT_HWPOISON
|
987 VM_FAULT_HWPOISON_LARGE
))
988 do_sigbus(regs
, error_code
, address
, fault
);
989 else if (fault
& VM_FAULT_SIGSEGV
)
990 bad_area_nosemaphore(regs
, error_code
, address
);
996 static int spurious_kernel_fault_check(unsigned long error_code
, pte_t
*pte
)
998 if ((error_code
& X86_PF_WRITE
) && !pte_write(*pte
))
1001 if ((error_code
& X86_PF_INSTR
) && !pte_exec(*pte
))
1008 * Handle a spurious fault caused by a stale TLB entry.
1010 * This allows us to lazily refresh the TLB when increasing the
1011 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
1012 * eagerly is very expensive since that implies doing a full
1013 * cross-processor TLB flush, even if no stale TLB entries exist
1014 * on other processors.
1016 * Spurious faults may only occur if the TLB contains an entry with
1017 * fewer permission than the page table entry. Non-present (P = 0)
1018 * and reserved bit (R = 1) faults are never spurious.
1020 * There are no security implications to leaving a stale TLB when
1021 * increasing the permissions on a page.
1023 * Returns non-zero if a spurious fault was handled, zero otherwise.
1025 * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
1026 * (Optional Invalidation).
1029 spurious_kernel_fault(unsigned long error_code
, unsigned long address
)
1039 * Only writes to RO or instruction fetches from NX may cause
1042 * These could be from user or supervisor accesses but the TLB
1043 * is only lazily flushed after a kernel mapping protection
1044 * change, so user accesses are not expected to cause spurious
1047 if (error_code
!= (X86_PF_WRITE
| X86_PF_PROT
) &&
1048 error_code
!= (X86_PF_INSTR
| X86_PF_PROT
))
1051 pgd
= init_mm
.pgd
+ pgd_index(address
);
1052 if (!pgd_present(*pgd
))
1055 p4d
= p4d_offset(pgd
, address
);
1056 if (!p4d_present(*p4d
))
1059 if (p4d_large(*p4d
))
1060 return spurious_kernel_fault_check(error_code
, (pte_t
*) p4d
);
1062 pud
= pud_offset(p4d
, address
);
1063 if (!pud_present(*pud
))
1066 if (pud_large(*pud
))
1067 return spurious_kernel_fault_check(error_code
, (pte_t
*) pud
);
1069 pmd
= pmd_offset(pud
, address
);
1070 if (!pmd_present(*pmd
))
1073 if (pmd_large(*pmd
))
1074 return spurious_kernel_fault_check(error_code
, (pte_t
*) pmd
);
1076 pte
= pte_offset_kernel(pmd
, address
);
1077 if (!pte_present(*pte
))
1080 ret
= spurious_kernel_fault_check(error_code
, pte
);
1085 * Make sure we have permissions in PMD.
1086 * If not, then there's a bug in the page tables:
1088 ret
= spurious_kernel_fault_check(error_code
, (pte_t
*) pmd
);
1089 WARN_ONCE(!ret
, "PMD has incorrect permission bits\n");
1093 NOKPROBE_SYMBOL(spurious_kernel_fault
);
1095 int show_unhandled_signals
= 1;
1098 access_error(unsigned long error_code
, struct vm_area_struct
*vma
)
1100 /* This is only called for the current mm, so: */
1101 bool foreign
= false;
1104 * Read or write was blocked by protection keys. This is
1105 * always an unconditional error and can never result in
1106 * a follow-up action to resolve the fault, like a COW.
1108 if (error_code
& X86_PF_PK
)
1112 * Make sure to check the VMA so that we do not perform
1113 * faults just to hit a X86_PF_PK as soon as we fill in a
1116 if (!arch_vma_access_permitted(vma
, (error_code
& X86_PF_WRITE
),
1117 (error_code
& X86_PF_INSTR
), foreign
))
1120 if (error_code
& X86_PF_WRITE
) {
1121 /* write, present and write, not present: */
1122 if (unlikely(!(vma
->vm_flags
& VM_WRITE
)))
1127 /* read, present: */
1128 if (unlikely(error_code
& X86_PF_PROT
))
1131 /* read, not present: */
1132 if (unlikely(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
))))
1138 static int fault_in_kernel_space(unsigned long address
)
1141 * On 64-bit systems, the vsyscall page is at an address above
1142 * TASK_SIZE_MAX, but is not considered part of the kernel
1145 if (IS_ENABLED(CONFIG_X86_64
) && is_vsyscall_vaddr(address
))
1148 return address
>= TASK_SIZE_MAX
;
1151 static inline bool smap_violation(int error_code
, struct pt_regs
*regs
)
1153 if (!IS_ENABLED(CONFIG_X86_SMAP
))
1156 if (!static_cpu_has(X86_FEATURE_SMAP
))
1159 if (error_code
& X86_PF_USER
)
1162 if (!user_mode(regs
) && (regs
->flags
& X86_EFLAGS_AC
))
1169 * Called for all faults where 'address' is part of the kernel address
1170 * space. Might get called for faults that originate from *code* that
1171 * ran in userspace or the kernel.
1174 do_kern_addr_fault(struct pt_regs
*regs
, unsigned long hw_error_code
,
1175 unsigned long address
)
1178 * Protection keys exceptions only happen on user pages. We
1179 * have no user pages in the kernel portion of the address
1180 * space, so do not expect them here.
1182 WARN_ON_ONCE(hw_error_code
& X86_PF_PK
);
1185 * We can fault-in kernel-space virtual memory on-demand. The
1186 * 'reference' page table is init_mm.pgd.
1188 * NOTE! We MUST NOT take any locks for this case. We may
1189 * be in an interrupt or a critical region, and should
1190 * only copy the information from the master page table,
1193 * Before doing this on-demand faulting, ensure that the
1194 * fault is not any of the following:
1195 * 1. A fault on a PTE with a reserved bit set.
1196 * 2. A fault caused by a user-mode access. (Do not demand-
1197 * fault kernel memory due to user-mode accesses).
1198 * 3. A fault caused by a page-level protection violation.
1199 * (A demand fault would be on a non-present page which
1200 * would have X86_PF_PROT==0).
1202 if (!(hw_error_code
& (X86_PF_RSVD
| X86_PF_USER
| X86_PF_PROT
))) {
1203 if (vmalloc_fault(address
) >= 0)
1207 /* Was the fault spurious, caused by lazy TLB invalidation? */
1208 if (spurious_kernel_fault(hw_error_code
, address
))
1211 /* kprobes don't want to hook the spurious faults: */
1212 if (kprobes_fault(regs
))
1216 * Note, despite being a "bad area", there are quite a few
1217 * acceptable reasons to get here, such as erratum fixups
1218 * and handling kernel code that can fault, like get_user().
1220 * Don't take the mm semaphore here. If we fixup a prefetch
1221 * fault we could otherwise deadlock:
1223 bad_area_nosemaphore(regs
, hw_error_code
, address
);
1225 NOKPROBE_SYMBOL(do_kern_addr_fault
);
1227 /* Handle faults in the user portion of the address space */
1229 void do_user_addr_fault(struct pt_regs
*regs
,
1230 unsigned long hw_error_code
,
1231 unsigned long address
)
1233 unsigned long sw_error_code
;
1234 struct vm_area_struct
*vma
;
1235 struct task_struct
*tsk
;
1236 struct mm_struct
*mm
;
1237 vm_fault_t fault
, major
= 0;
1238 unsigned int flags
= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_KILLABLE
;
1243 /* kprobes don't want to hook the spurious faults: */
1244 if (unlikely(kprobes_fault(regs
)))
1248 * Reserved bits are never expected to be set on
1249 * entries in the user portion of the page tables.
1251 if (unlikely(hw_error_code
& X86_PF_RSVD
))
1252 pgtable_bad(regs
, hw_error_code
, address
);
1255 * Check for invalid kernel (supervisor) access to user
1256 * pages in the user address space.
1258 if (unlikely(smap_violation(hw_error_code
, regs
))) {
1259 bad_area_nosemaphore(regs
, hw_error_code
, address
);
1264 * If we're in an interrupt, have no user context or are running
1265 * in a region with pagefaults disabled then we must not take the fault
1267 if (unlikely(faulthandler_disabled() || !mm
)) {
1268 bad_area_nosemaphore(regs
, hw_error_code
, address
);
1273 * hw_error_code is literally the "page fault error code" passed to
1274 * the kernel directly from the hardware. But, we will shortly be
1275 * modifying it in software, so give it a new name.
1277 sw_error_code
= hw_error_code
;
1280 * It's safe to allow irq's after cr2 has been saved and the
1281 * vmalloc fault has been handled.
1283 * User-mode registers count as a user access even for any
1284 * potential system fault or CPU buglet:
1286 if (user_mode(regs
)) {
1289 * Up to this point, X86_PF_USER set in hw_error_code
1290 * indicated a user-mode access. But, after this,
1291 * X86_PF_USER in sw_error_code will indicate either
1292 * that, *or* an implicit kernel(supervisor)-mode access
1293 * which originated from user mode.
1295 if (!(hw_error_code
& X86_PF_USER
)) {
1297 * The CPU was in user mode, but the CPU says
1298 * the fault was not a user-mode access.
1299 * Must be an implicit kernel-mode access,
1300 * which we do not expect to happen in the
1301 * user address space.
1303 pr_warn_once("kernel-mode error from user-mode: %lx\n",
1306 sw_error_code
|= X86_PF_USER
;
1308 flags
|= FAULT_FLAG_USER
;
1310 if (regs
->flags
& X86_EFLAGS_IF
)
1314 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS
, 1, regs
, address
);
1316 if (sw_error_code
& X86_PF_WRITE
)
1317 flags
|= FAULT_FLAG_WRITE
;
1318 if (sw_error_code
& X86_PF_INSTR
)
1319 flags
|= FAULT_FLAG_INSTRUCTION
;
1321 #ifdef CONFIG_X86_64
1323 * Instruction fetch faults in the vsyscall page might need
1324 * emulation. The vsyscall page is at a high address
1325 * (>PAGE_OFFSET), but is considered to be part of the user
1328 * The vsyscall page does not have a "real" VMA, so do this
1329 * emulation before we go searching for VMAs.
1331 if ((sw_error_code
& X86_PF_INSTR
) && is_vsyscall_vaddr(address
)) {
1332 if (emulate_vsyscall(regs
, address
))
1338 * Kernel-mode access to the user address space should only occur
1339 * on well-defined single instructions listed in the exception
1340 * tables. But, an erroneous kernel fault occurring outside one of
1341 * those areas which also holds mmap_sem might deadlock attempting
1342 * to validate the fault against the address space.
1344 * Only do the expensive exception table search when we might be at
1345 * risk of a deadlock. This happens if we
1346 * 1. Failed to acquire mmap_sem, and
1347 * 2. The access did not originate in userspace. Note: either the
1348 * hardware or earlier page fault code may set X86_PF_USER
1351 if (unlikely(!down_read_trylock(&mm
->mmap_sem
))) {
1352 if (!(sw_error_code
& X86_PF_USER
) &&
1353 !search_exception_tables(regs
->ip
)) {
1355 * Fault from code in kernel from
1356 * which we do not expect faults.
1358 bad_area_nosemaphore(regs
, sw_error_code
, address
);
1362 down_read(&mm
->mmap_sem
);
1365 * The above down_read_trylock() might have succeeded in
1366 * which case we'll have missed the might_sleep() from
1372 vma
= find_vma(mm
, address
);
1373 if (unlikely(!vma
)) {
1374 bad_area(regs
, sw_error_code
, address
);
1377 if (likely(vma
->vm_start
<= address
))
1379 if (unlikely(!(vma
->vm_flags
& VM_GROWSDOWN
))) {
1380 bad_area(regs
, sw_error_code
, address
);
1383 if (sw_error_code
& X86_PF_USER
) {
1385 * Accessing the stack below %sp is always a bug.
1386 * The large cushion allows instructions like enter
1387 * and pusha to work. ("enter $65535, $31" pushes
1388 * 32 pointers and then decrements %sp by 65535.)
1390 if (unlikely(address
+ 65536 + 32 * sizeof(unsigned long) < regs
->sp
)) {
1391 bad_area(regs
, sw_error_code
, address
);
1395 if (unlikely(expand_stack(vma
, address
))) {
1396 bad_area(regs
, sw_error_code
, address
);
1401 * Ok, we have a good vm_area for this memory access, so
1402 * we can handle it..
1405 if (unlikely(access_error(sw_error_code
, vma
))) {
1406 bad_area_access_error(regs
, sw_error_code
, address
, vma
);
1411 * If for any reason at all we couldn't handle the fault,
1412 * make sure we exit gracefully rather than endlessly redo
1413 * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if
1414 * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked.
1416 * Note that handle_userfault() may also release and reacquire mmap_sem
1417 * (and not return with VM_FAULT_RETRY), when returning to userland to
1418 * repeat the page fault later with a VM_FAULT_NOPAGE retval
1419 * (potentially after handling any pending signal during the return to
1420 * userland). The return to userland is identified whenever
1421 * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags.
1423 fault
= handle_mm_fault(vma
, address
, flags
);
1424 major
|= fault
& VM_FAULT_MAJOR
;
1427 * If we need to retry the mmap_sem has already been released,
1428 * and if there is a fatal signal pending there is no guarantee
1429 * that we made any progress. Handle this case first.
1431 if (unlikely(fault
& VM_FAULT_RETRY
)) {
1432 /* Retry at most once */
1433 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
1434 flags
&= ~FAULT_FLAG_ALLOW_RETRY
;
1435 flags
|= FAULT_FLAG_TRIED
;
1436 if (!fatal_signal_pending(tsk
))
1440 /* User mode? Just return to handle the fatal exception */
1441 if (flags
& FAULT_FLAG_USER
)
1444 /* Not returning to user mode? Handle exceptions or die: */
1445 no_context(regs
, sw_error_code
, address
, SIGBUS
, BUS_ADRERR
);
1449 up_read(&mm
->mmap_sem
);
1450 if (unlikely(fault
& VM_FAULT_ERROR
)) {
1451 mm_fault_error(regs
, sw_error_code
, address
, fault
);
1456 * Major/minor page fault accounting. If any of the events
1457 * returned VM_FAULT_MAJOR, we account it as a major fault.
1461 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ
, 1, regs
, address
);
1464 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN
, 1, regs
, address
);
1467 check_v8086_mode(regs
, address
, tsk
);
1469 NOKPROBE_SYMBOL(do_user_addr_fault
);
1472 * This routine handles page faults. It determines the address,
1473 * and the problem, and then passes it off to one of the appropriate
1476 static noinline
void
1477 __do_page_fault(struct pt_regs
*regs
, unsigned long hw_error_code
,
1478 unsigned long address
)
1480 prefetchw(¤t
->mm
->mmap_sem
);
1482 if (unlikely(kmmio_fault(regs
, address
)))
1485 /* Was the fault on kernel-controlled part of the address space? */
1486 if (unlikely(fault_in_kernel_space(address
)))
1487 do_kern_addr_fault(regs
, hw_error_code
, address
);
1489 do_user_addr_fault(regs
, hw_error_code
, address
);
1491 NOKPROBE_SYMBOL(__do_page_fault
);
1493 static nokprobe_inline
void
1494 trace_page_fault_entries(unsigned long address
, struct pt_regs
*regs
,
1495 unsigned long error_code
)
1497 if (user_mode(regs
))
1498 trace_page_fault_user(address
, regs
, error_code
);
1500 trace_page_fault_kernel(address
, regs
, error_code
);
1504 * We must have this function blacklisted from kprobes, tagged with notrace
1505 * and call read_cr2() before calling anything else. To avoid calling any
1506 * kind of tracing machinery before we've observed the CR2 value.
1508 * exception_{enter,exit}() contains all sorts of tracepoints.
1510 dotraplinkage
void notrace
1511 do_page_fault(struct pt_regs
*regs
, unsigned long error_code
)
1513 unsigned long address
= read_cr2(); /* Get the faulting address */
1514 enum ctx_state prev_state
;
1516 prev_state
= exception_enter();
1517 if (trace_pagefault_enabled())
1518 trace_page_fault_entries(address
, regs
, error_code
);
1520 __do_page_fault(regs
, error_code
, address
);
1521 exception_exit(prev_state
);
1523 NOKPROBE_SYMBOL(do_page_fault
);