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()*/
30 #include <asm/desc.h> /* store_idt(), ... */
32 #define CREATE_TRACE_POINTS
33 #include <asm/trace/exceptions.h>
36 * Returns 0 if mmiotrace is disabled, or if the fault is not
37 * handled by mmiotrace:
39 static nokprobe_inline
int
40 kmmio_fault(struct pt_regs
*regs
, unsigned long addr
)
42 if (unlikely(is_kmmio_active()))
43 if (kmmio_handler(regs
, addr
) == 1)
48 static nokprobe_inline
int kprobes_fault(struct pt_regs
*regs
)
50 if (!kprobes_built_in())
55 * To be potentially processing a kprobe fault and to be allowed to call
56 * kprobe_running(), we have to be non-preemptible.
60 if (!kprobe_running())
62 return kprobe_fault_handler(regs
, X86_TRAP_PF
);
70 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
71 * Check that here and ignore it.
75 * Sometimes the CPU reports invalid exceptions on prefetch.
76 * Check that here and ignore it.
78 * Opcode checker based on code by Richard Brunner.
81 check_prefetch_opcode(struct pt_regs
*regs
, unsigned char *instr
,
82 unsigned char opcode
, int *prefetch
)
84 unsigned char instr_hi
= opcode
& 0xf0;
85 unsigned char instr_lo
= opcode
& 0x0f;
91 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
92 * In X86_64 long mode, the CPU will signal invalid
93 * opcode if some of these prefixes are present so
94 * X86_64 will never get here anyway
96 return ((instr_lo
& 7) == 0x6);
100 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
101 * Need to figure out under what instruction mode the
102 * instruction was issued. Could check the LDT for lm,
103 * but for now it's good enough to assume that long
104 * mode only uses well known segments or kernel.
106 return (!user_mode(regs
) || user_64bit_mode(regs
));
109 /* 0x64 thru 0x67 are valid prefixes in all modes. */
110 return (instr_lo
& 0xC) == 0x4;
112 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
113 return !instr_lo
|| (instr_lo
>>1) == 1;
115 /* Prefetch instruction is 0x0F0D or 0x0F18 */
116 if (probe_kernel_address(instr
, opcode
))
119 *prefetch
= (instr_lo
== 0xF) &&
120 (opcode
== 0x0D || opcode
== 0x18);
128 is_prefetch(struct pt_regs
*regs
, unsigned long error_code
, unsigned long addr
)
130 unsigned char *max_instr
;
131 unsigned char *instr
;
135 * If it was a exec (instruction fetch) fault on NX page, then
136 * do not ignore the fault:
138 if (error_code
& X86_PF_INSTR
)
141 instr
= (void *)convert_ip_to_linear(current
, regs
);
142 max_instr
= instr
+ 15;
144 if (user_mode(regs
) && instr
>= (unsigned char *)TASK_SIZE_MAX
)
147 while (instr
< max_instr
) {
148 unsigned char opcode
;
150 if (probe_kernel_address(instr
, opcode
))
155 if (!check_prefetch_opcode(regs
, instr
, opcode
, &prefetch
))
161 DEFINE_SPINLOCK(pgd_lock
);
165 static inline pmd_t
*vmalloc_sync_one(pgd_t
*pgd
, unsigned long address
)
167 unsigned index
= pgd_index(address
);
174 pgd_k
= init_mm
.pgd
+ index
;
176 if (!pgd_present(*pgd_k
))
180 * set_pgd(pgd, *pgd_k); here would be useless on PAE
181 * and redundant with the set_pmd() on non-PAE. As would
184 p4d
= p4d_offset(pgd
, address
);
185 p4d_k
= p4d_offset(pgd_k
, address
);
186 if (!p4d_present(*p4d_k
))
189 pud
= pud_offset(p4d
, address
);
190 pud_k
= pud_offset(p4d_k
, address
);
191 if (!pud_present(*pud_k
))
194 pmd
= pmd_offset(pud
, address
);
195 pmd_k
= pmd_offset(pud_k
, address
);
196 if (!pmd_present(*pmd_k
))
199 if (!pmd_present(*pmd
))
200 set_pmd(pmd
, *pmd_k
);
202 BUG_ON(pmd_page(*pmd
) != pmd_page(*pmd_k
));
207 void vmalloc_sync_all(void)
209 unsigned long address
;
211 if (SHARED_KERNEL_PMD
)
214 for (address
= VMALLOC_START
& PMD_MASK
;
215 address
>= TASK_SIZE_MAX
&& address
< FIXADDR_TOP
;
216 address
+= PMD_SIZE
) {
219 spin_lock(&pgd_lock
);
220 list_for_each_entry(page
, &pgd_list
, lru
) {
221 spinlock_t
*pgt_lock
;
224 /* the pgt_lock only for Xen */
225 pgt_lock
= &pgd_page_get_mm(page
)->page_table_lock
;
228 ret
= vmalloc_sync_one(page_address(page
), address
);
229 spin_unlock(pgt_lock
);
234 spin_unlock(&pgd_lock
);
241 * Handle a fault on the vmalloc or module mapping area
243 static noinline
int vmalloc_fault(unsigned long address
)
245 unsigned long pgd_paddr
;
249 /* Make sure we are in vmalloc area: */
250 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
254 * Synchronize this task's top level page-table
255 * with the 'reference' page table.
257 * Do _not_ use "current" here. We might be inside
258 * an interrupt in the middle of a task switch..
260 pgd_paddr
= read_cr3_pa();
261 pmd_k
= vmalloc_sync_one(__va(pgd_paddr
), address
);
265 if (pmd_large(*pmd_k
))
268 pte_k
= pte_offset_kernel(pmd_k
, address
);
269 if (!pte_present(*pte_k
))
274 NOKPROBE_SYMBOL(vmalloc_fault
);
277 * Did it hit the DOS screen memory VA from vm86 mode?
280 check_v8086_mode(struct pt_regs
*regs
, unsigned long address
,
281 struct task_struct
*tsk
)
286 if (!v8086_mode(regs
) || !tsk
->thread
.vm86
)
289 bit
= (address
- 0xA0000) >> PAGE_SHIFT
;
291 tsk
->thread
.vm86
->screen_bitmap
|= 1 << bit
;
295 static bool low_pfn(unsigned long pfn
)
297 return pfn
< max_low_pfn
;
300 static void dump_pagetable(unsigned long address
)
302 pgd_t
*base
= __va(read_cr3_pa());
303 pgd_t
*pgd
= &base
[pgd_index(address
)];
309 #ifdef CONFIG_X86_PAE
310 pr_info("*pdpt = %016Lx ", pgd_val(*pgd
));
311 if (!low_pfn(pgd_val(*pgd
) >> PAGE_SHIFT
) || !pgd_present(*pgd
))
313 #define pr_pde pr_cont
315 #define pr_pde pr_info
317 p4d
= p4d_offset(pgd
, address
);
318 pud
= pud_offset(p4d
, address
);
319 pmd
= pmd_offset(pud
, address
);
320 pr_pde("*pde = %0*Lx ", sizeof(*pmd
) * 2, (u64
)pmd_val(*pmd
));
324 * We must not directly access the pte in the highpte
325 * case if the page table is located in highmem.
326 * And let's rather not kmap-atomic the pte, just in case
327 * it's allocated already:
329 if (!low_pfn(pmd_pfn(*pmd
)) || !pmd_present(*pmd
) || pmd_large(*pmd
))
332 pte
= pte_offset_kernel(pmd
, address
);
333 pr_cont("*pte = %0*Lx ", sizeof(*pte
) * 2, (u64
)pte_val(*pte
));
338 #else /* CONFIG_X86_64: */
340 void vmalloc_sync_all(void)
342 sync_global_pgds(VMALLOC_START
& PGDIR_MASK
, VMALLOC_END
);
348 * Handle a fault on the vmalloc area
350 static noinline
int vmalloc_fault(unsigned long address
)
358 /* Make sure we are in vmalloc area: */
359 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
362 WARN_ON_ONCE(in_nmi());
365 * Copy kernel mappings over when needed. This can also
366 * happen within a race in page table update. In the later
369 pgd
= (pgd_t
*)__va(read_cr3_pa()) + pgd_index(address
);
370 pgd_k
= pgd_offset_k(address
);
371 if (pgd_none(*pgd_k
))
374 if (pgtable_l5_enabled()) {
375 if (pgd_none(*pgd
)) {
376 set_pgd(pgd
, *pgd_k
);
377 arch_flush_lazy_mmu_mode();
379 BUG_ON(pgd_page_vaddr(*pgd
) != pgd_page_vaddr(*pgd_k
));
383 /* With 4-level paging, copying happens on the p4d level. */
384 p4d
= p4d_offset(pgd
, address
);
385 p4d_k
= p4d_offset(pgd_k
, address
);
386 if (p4d_none(*p4d_k
))
389 if (p4d_none(*p4d
) && !pgtable_l5_enabled()) {
390 set_p4d(p4d
, *p4d_k
);
391 arch_flush_lazy_mmu_mode();
393 BUG_ON(p4d_pfn(*p4d
) != p4d_pfn(*p4d_k
));
396 BUILD_BUG_ON(CONFIG_PGTABLE_LEVELS
< 4);
398 pud
= pud_offset(p4d
, address
);
405 pmd
= pmd_offset(pud
, address
);
412 pte
= pte_offset_kernel(pmd
, address
);
413 if (!pte_present(*pte
))
418 NOKPROBE_SYMBOL(vmalloc_fault
);
420 #ifdef CONFIG_CPU_SUP_AMD
421 static const char errata93_warning
[] =
423 "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
424 "******* Working around it, but it may cause SEGVs or burn power.\n"
425 "******* Please consider a BIOS update.\n"
426 "******* Disabling USB legacy in the BIOS may also help.\n";
430 * No vm86 mode in 64-bit mode:
433 check_v8086_mode(struct pt_regs
*regs
, unsigned long address
,
434 struct task_struct
*tsk
)
438 static int bad_address(void *p
)
442 return probe_kernel_address((unsigned long *)p
, dummy
);
445 static void dump_pagetable(unsigned long address
)
447 pgd_t
*base
= __va(read_cr3_pa());
448 pgd_t
*pgd
= base
+ pgd_index(address
);
454 if (bad_address(pgd
))
457 pr_info("PGD %lx ", pgd_val(*pgd
));
459 if (!pgd_present(*pgd
))
462 p4d
= p4d_offset(pgd
, address
);
463 if (bad_address(p4d
))
466 pr_cont("P4D %lx ", p4d_val(*p4d
));
467 if (!p4d_present(*p4d
) || p4d_large(*p4d
))
470 pud
= pud_offset(p4d
, address
);
471 if (bad_address(pud
))
474 pr_cont("PUD %lx ", pud_val(*pud
));
475 if (!pud_present(*pud
) || pud_large(*pud
))
478 pmd
= pmd_offset(pud
, address
);
479 if (bad_address(pmd
))
482 pr_cont("PMD %lx ", pmd_val(*pmd
));
483 if (!pmd_present(*pmd
) || pmd_large(*pmd
))
486 pte
= pte_offset_kernel(pmd
, address
);
487 if (bad_address(pte
))
490 pr_cont("PTE %lx", pte_val(*pte
));
498 #endif /* CONFIG_X86_64 */
501 * Workaround for K8 erratum #93 & buggy BIOS.
503 * BIOS SMM functions are required to use a specific workaround
504 * to avoid corruption of the 64bit RIP register on C stepping K8.
506 * A lot of BIOS that didn't get tested properly miss this.
508 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
509 * Try to work around it here.
511 * Note we only handle faults in kernel here.
512 * Does nothing on 32-bit.
514 static int is_errata93(struct pt_regs
*regs
, unsigned long address
)
516 #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
517 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
518 || boot_cpu_data
.x86
!= 0xf)
521 if (address
!= regs
->ip
)
524 if ((address
>> 32) != 0)
527 address
|= 0xffffffffUL
<< 32;
528 if ((address
>= (u64
)_stext
&& address
<= (u64
)_etext
) ||
529 (address
>= MODULES_VADDR
&& address
<= MODULES_END
)) {
530 printk_once(errata93_warning
);
539 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
540 * to illegal addresses >4GB.
542 * We catch this in the page fault handler because these addresses
543 * are not reachable. Just detect this case and return. Any code
544 * segment in LDT is compatibility mode.
546 static int is_errata100(struct pt_regs
*regs
, unsigned long address
)
549 if ((regs
->cs
== __USER32_CS
|| (regs
->cs
& (1<<2))) && (address
>> 32))
555 static int is_f00f_bug(struct pt_regs
*regs
, unsigned long address
)
557 #ifdef CONFIG_X86_F00F_BUG
561 * Pentium F0 0F C7 C8 bug workaround:
563 if (boot_cpu_has_bug(X86_BUG_F00F
)) {
564 nr
= (address
- idt_descr
.address
) >> 3;
567 do_invalid_op(regs
, 0);
575 static void show_ldttss(const struct desc_ptr
*gdt
, const char *name
, u16 index
)
577 u32 offset
= (index
>> 3) * sizeof(struct desc_struct
);
579 struct ldttss_desc desc
;
582 pr_alert("%s: NULL\n", name
);
586 if (offset
+ sizeof(struct ldttss_desc
) >= gdt
->size
) {
587 pr_alert("%s: 0x%hx -- out of bounds\n", name
, index
);
591 if (probe_kernel_read(&desc
, (void *)(gdt
->address
+ offset
),
592 sizeof(struct ldttss_desc
))) {
593 pr_alert("%s: 0x%hx -- GDT entry is not readable\n",
598 addr
= desc
.base0
| (desc
.base1
<< 16) | (desc
.base2
<< 24);
600 addr
|= ((u64
)desc
.base3
<< 32);
602 pr_alert("%s: 0x%hx -- base=0x%lx limit=0x%x\n",
603 name
, index
, addr
, (desc
.limit0
| (desc
.limit1
<< 16)));
607 * This helper function transforms the #PF error_code bits into
608 * "[PROT] [USER]" type of descriptive, almost human-readable error strings:
610 static void err_str_append(unsigned long error_code
, char *buf
, unsigned long mask
, const char *txt
)
612 if (error_code
& mask
) {
620 show_fault_oops(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
)
624 if (!oops_may_print())
627 if (error_code
& X86_PF_INSTR
) {
632 pgd
= __va(read_cr3_pa());
633 pgd
+= pgd_index(address
);
635 pte
= lookup_address_in_pgd(pgd
, address
, &level
);
637 if (pte
&& pte_present(*pte
) && !pte_exec(*pte
))
638 pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n",
639 from_kuid(&init_user_ns
, current_uid()));
640 if (pte
&& pte_present(*pte
) && pte_exec(*pte
) &&
641 (pgd_flags(*pgd
) & _PAGE_USER
) &&
642 (__read_cr4() & X86_CR4_SMEP
))
643 pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n",
644 from_kuid(&init_user_ns
, current_uid()));
647 pr_alert("BUG: unable to handle kernel %s at %px\n",
648 address
< PAGE_SIZE
? "NULL pointer dereference" : "paging request",
654 * Note: length of these appended strings including the separation space and the
655 * zero delimiter must fit into err_txt[].
657 err_str_append(error_code
, err_txt
, X86_PF_PROT
, "[PROT]" );
658 err_str_append(error_code
, err_txt
, X86_PF_WRITE
, "[WRITE]");
659 err_str_append(error_code
, err_txt
, X86_PF_USER
, "[USER]" );
660 err_str_append(error_code
, err_txt
, X86_PF_RSVD
, "[RSVD]" );
661 err_str_append(error_code
, err_txt
, X86_PF_INSTR
, "[INSTR]");
662 err_str_append(error_code
, err_txt
, X86_PF_PK
, "[PK]" );
664 pr_alert("#PF error: %s\n", error_code
? err_txt
: "[normal kernel read fault]");
666 if (!(error_code
& X86_PF_USER
) && user_mode(regs
)) {
667 struct desc_ptr idt
, gdt
;
670 pr_alert("This was a system access from user code\n");
673 * This can happen for quite a few reasons. The more obvious
674 * ones are faults accessing the GDT, or LDT. Perhaps
675 * surprisingly, if the CPU tries to deliver a benign or
676 * contributory exception from user code and gets a page fault
677 * during delivery, the page fault can be delivered as though
678 * it originated directly from user code. This could happen
679 * due to wrong permissions on the IDT, GDT, LDT, TSS, or
680 * kernel or IST stack.
684 /* Usable even on Xen PV -- it's just slow. */
685 native_store_gdt(&gdt
);
687 pr_alert("IDT: 0x%lx (limit=0x%hx) GDT: 0x%lx (limit=0x%hx)\n",
688 idt
.address
, idt
.size
, gdt
.address
, gdt
.size
);
691 show_ldttss(&gdt
, "LDTR", ldtr
);
694 show_ldttss(&gdt
, "TR", tr
);
697 dump_pagetable(address
);
701 pgtable_bad(struct pt_regs
*regs
, unsigned long error_code
,
702 unsigned long address
)
704 struct task_struct
*tsk
;
708 flags
= oops_begin();
712 printk(KERN_ALERT
"%s: Corrupted page table at address %lx\n",
714 dump_pagetable(address
);
716 if (__die("Bad pagetable", regs
, error_code
))
719 oops_end(flags
, regs
, sig
);
722 static void set_signal_archinfo(unsigned long address
,
723 unsigned long error_code
)
725 struct task_struct
*tsk
= current
;
728 * To avoid leaking information about the kernel page
729 * table layout, pretend that user-mode accesses to
730 * kernel addresses are always protection faults.
732 if (address
>= TASK_SIZE_MAX
)
733 error_code
|= X86_PF_PROT
;
735 tsk
->thread
.trap_nr
= X86_TRAP_PF
;
736 tsk
->thread
.error_code
= error_code
| X86_PF_USER
;
737 tsk
->thread
.cr2
= address
;
741 no_context(struct pt_regs
*regs
, unsigned long error_code
,
742 unsigned long address
, int signal
, int si_code
)
744 struct task_struct
*tsk
= current
;
748 if (user_mode(regs
)) {
750 * This is an implicit supervisor-mode access from user
751 * mode. Bypass all the kernel-mode recovery code and just
757 /* Are we prepared to handle this kernel fault? */
758 if (fixup_exception(regs
, X86_TRAP_PF
, error_code
, address
)) {
760 * Any interrupt that takes a fault gets the fixup. This makes
761 * the below recursive fault logic only apply to a faults from
768 * Per the above we're !in_interrupt(), aka. task context.
770 * In this case we need to make sure we're not recursively
771 * faulting through the emulate_vsyscall() logic.
773 if (current
->thread
.sig_on_uaccess_err
&& signal
) {
774 set_signal_archinfo(address
, error_code
);
776 /* XXX: hwpoison faults will set the wrong code. */
777 force_sig_fault(signal
, si_code
, (void __user
*)address
,
782 * Barring that, we can do the fixup and be happy.
787 #ifdef CONFIG_VMAP_STACK
789 * Stack overflow? During boot, we can fault near the initial
790 * stack in the direct map, but that's not an overflow -- check
791 * that we're in vmalloc space to avoid this.
793 if (is_vmalloc_addr((void *)address
) &&
794 (((unsigned long)tsk
->stack
- 1 - address
< PAGE_SIZE
) ||
795 address
- ((unsigned long)tsk
->stack
+ THREAD_SIZE
) < PAGE_SIZE
)) {
796 unsigned long stack
= this_cpu_read(orig_ist
.ist
[DOUBLEFAULT_STACK
]) - sizeof(void *);
798 * We're likely to be running with very little stack space
799 * left. It's plausible that we'd hit this condition but
800 * double-fault even before we get this far, in which case
801 * we're fine: the double-fault handler will deal with it.
803 * We don't want to make it all the way into the oops code
804 * and then double-fault, though, because we're likely to
805 * break the console driver and lose most of the stack dump.
807 asm volatile ("movq %[stack], %%rsp\n\t"
808 "call handle_stack_overflow\n\t"
810 : ASM_CALL_CONSTRAINT
811 : "D" ("kernel stack overflow (page fault)"),
812 "S" (regs
), "d" (address
),
813 [stack
] "rm" (stack
));
821 * Valid to do another page fault here, because if this fault
822 * had been triggered by is_prefetch fixup_exception would have
827 * Hall of shame of CPU/BIOS bugs.
829 if (is_prefetch(regs
, error_code
, address
))
832 if (is_errata93(regs
, address
))
836 * Buggy firmware could access regions which might page fault, try to
837 * recover from such faults.
839 if (IS_ENABLED(CONFIG_EFI
))
840 efi_recover_from_page_fault(address
);
844 * Oops. The kernel tried to access some bad page. We'll have to
845 * terminate things with extreme prejudice:
847 flags
= oops_begin();
849 show_fault_oops(regs
, error_code
, address
);
851 if (task_stack_end_corrupted(tsk
))
852 printk(KERN_EMERG
"Thread overran stack, or stack corrupted\n");
855 if (__die("Oops", regs
, error_code
))
858 /* Executive summary in case the body of the oops scrolled away */
859 printk(KERN_DEFAULT
"CR2: %016lx\n", address
);
861 oops_end(flags
, regs
, sig
);
865 * Print out info about fatal segfaults, if the show_unhandled_signals
869 show_signal_msg(struct pt_regs
*regs
, unsigned long error_code
,
870 unsigned long address
, struct task_struct
*tsk
)
872 const char *loglvl
= task_pid_nr(tsk
) > 1 ? KERN_INFO
: KERN_EMERG
;
874 if (!unhandled_signal(tsk
, SIGSEGV
))
877 if (!printk_ratelimit())
880 printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx",
881 loglvl
, tsk
->comm
, task_pid_nr(tsk
), address
,
882 (void *)regs
->ip
, (void *)regs
->sp
, error_code
);
884 print_vma_addr(KERN_CONT
" in ", regs
->ip
);
886 printk(KERN_CONT
"\n");
888 show_opcodes(regs
, loglvl
);
892 * The (legacy) vsyscall page is the long page in the kernel portion
893 * of the address space that has user-accessible permissions.
895 static bool is_vsyscall_vaddr(unsigned long vaddr
)
897 return unlikely((vaddr
& PAGE_MASK
) == VSYSCALL_ADDR
);
901 __bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
902 unsigned long address
, u32 pkey
, int si_code
)
904 struct task_struct
*tsk
= current
;
906 /* User mode accesses just cause a SIGSEGV */
907 if (user_mode(regs
) && (error_code
& X86_PF_USER
)) {
909 * It's possible to have interrupts off here:
914 * Valid to do another page fault here because this one came
917 if (is_prefetch(regs
, error_code
, address
))
920 if (is_errata100(regs
, address
))
924 * To avoid leaking information about the kernel page table
925 * layout, pretend that user-mode accesses to kernel addresses
926 * are always protection faults.
928 if (address
>= TASK_SIZE_MAX
)
929 error_code
|= X86_PF_PROT
;
931 if (likely(show_unhandled_signals
))
932 show_signal_msg(regs
, error_code
, address
, tsk
);
934 set_signal_archinfo(address
, error_code
);
936 if (si_code
== SEGV_PKUERR
)
937 force_sig_pkuerr((void __user
*)address
, pkey
);
939 force_sig_fault(SIGSEGV
, si_code
, (void __user
*)address
, tsk
);
944 if (is_f00f_bug(regs
, address
))
947 no_context(regs
, error_code
, address
, SIGSEGV
, si_code
);
951 bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
952 unsigned long address
)
954 __bad_area_nosemaphore(regs
, error_code
, address
, 0, SEGV_MAPERR
);
958 __bad_area(struct pt_regs
*regs
, unsigned long error_code
,
959 unsigned long address
, u32 pkey
, int si_code
)
961 struct mm_struct
*mm
= current
->mm
;
963 * Something tried to access memory that isn't in our memory map..
964 * Fix it, but check if it's kernel or user first..
966 up_read(&mm
->mmap_sem
);
968 __bad_area_nosemaphore(regs
, error_code
, address
, pkey
, si_code
);
972 bad_area(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
)
974 __bad_area(regs
, error_code
, address
, 0, SEGV_MAPERR
);
977 static inline bool bad_area_access_from_pkeys(unsigned long error_code
,
978 struct vm_area_struct
*vma
)
980 /* This code is always called on the current mm */
981 bool foreign
= false;
983 if (!boot_cpu_has(X86_FEATURE_OSPKE
))
985 if (error_code
& X86_PF_PK
)
987 /* this checks permission keys on the VMA: */
988 if (!arch_vma_access_permitted(vma
, (error_code
& X86_PF_WRITE
),
989 (error_code
& X86_PF_INSTR
), foreign
))
995 bad_area_access_error(struct pt_regs
*regs
, unsigned long error_code
,
996 unsigned long address
, struct vm_area_struct
*vma
)
999 * This OSPKE check is not strictly necessary at runtime.
1000 * But, doing it this way allows compiler optimizations
1001 * if pkeys are compiled out.
1003 if (bad_area_access_from_pkeys(error_code
, vma
)) {
1005 * A protection key fault means that the PKRU value did not allow
1006 * access to some PTE. Userspace can figure out what PKRU was
1007 * from the XSAVE state. This function captures the pkey from
1008 * the vma and passes it to userspace so userspace can discover
1009 * which protection key was set on the PTE.
1011 * If we get here, we know that the hardware signaled a X86_PF_PK
1012 * fault and that there was a VMA once we got in the fault
1013 * handler. It does *not* guarantee that the VMA we find here
1014 * was the one that we faulted on.
1016 * 1. T1 : mprotect_key(foo, PAGE_SIZE, pkey=4);
1017 * 2. T1 : set PKRU to deny access to pkey=4, touches page
1019 * 4. T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
1020 * 5. T1 : enters fault handler, takes mmap_sem, etc...
1021 * 6. T1 : reaches here, sees vma_pkey(vma)=5, when we really
1022 * faulted on a pte with its pkey=4.
1024 u32 pkey
= vma_pkey(vma
);
1026 __bad_area(regs
, error_code
, address
, pkey
, SEGV_PKUERR
);
1028 __bad_area(regs
, error_code
, address
, 0, SEGV_ACCERR
);
1033 do_sigbus(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
,
1036 struct task_struct
*tsk
= current
;
1038 /* Kernel mode? Handle exceptions or die: */
1039 if (!(error_code
& X86_PF_USER
)) {
1040 no_context(regs
, error_code
, address
, SIGBUS
, BUS_ADRERR
);
1044 /* User-space => ok to do another page fault: */
1045 if (is_prefetch(regs
, error_code
, address
))
1048 set_signal_archinfo(address
, error_code
);
1050 #ifdef CONFIG_MEMORY_FAILURE
1051 if (fault
& (VM_FAULT_HWPOISON
|VM_FAULT_HWPOISON_LARGE
)) {
1055 "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
1056 tsk
->comm
, tsk
->pid
, address
);
1057 if (fault
& VM_FAULT_HWPOISON_LARGE
)
1058 lsb
= hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault
));
1059 if (fault
& VM_FAULT_HWPOISON
)
1061 force_sig_mceerr(BUS_MCEERR_AR
, (void __user
*)address
, lsb
, tsk
);
1065 force_sig_fault(SIGBUS
, BUS_ADRERR
, (void __user
*)address
, tsk
);
1068 static noinline
void
1069 mm_fault_error(struct pt_regs
*regs
, unsigned long error_code
,
1070 unsigned long address
, vm_fault_t fault
)
1072 if (fatal_signal_pending(current
) && !(error_code
& X86_PF_USER
)) {
1073 no_context(regs
, error_code
, address
, 0, 0);
1077 if (fault
& VM_FAULT_OOM
) {
1078 /* Kernel mode? Handle exceptions or die: */
1079 if (!(error_code
& X86_PF_USER
)) {
1080 no_context(regs
, error_code
, address
,
1081 SIGSEGV
, SEGV_MAPERR
);
1086 * We ran out of memory, call the OOM killer, and return the
1087 * userspace (which will retry the fault, or kill us if we got
1090 pagefault_out_of_memory();
1092 if (fault
& (VM_FAULT_SIGBUS
|VM_FAULT_HWPOISON
|
1093 VM_FAULT_HWPOISON_LARGE
))
1094 do_sigbus(regs
, error_code
, address
, fault
);
1095 else if (fault
& VM_FAULT_SIGSEGV
)
1096 bad_area_nosemaphore(regs
, error_code
, address
);
1102 static int spurious_kernel_fault_check(unsigned long error_code
, pte_t
*pte
)
1104 if ((error_code
& X86_PF_WRITE
) && !pte_write(*pte
))
1107 if ((error_code
& X86_PF_INSTR
) && !pte_exec(*pte
))
1114 * Handle a spurious fault caused by a stale TLB entry.
1116 * This allows us to lazily refresh the TLB when increasing the
1117 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
1118 * eagerly is very expensive since that implies doing a full
1119 * cross-processor TLB flush, even if no stale TLB entries exist
1120 * on other processors.
1122 * Spurious faults may only occur if the TLB contains an entry with
1123 * fewer permission than the page table entry. Non-present (P = 0)
1124 * and reserved bit (R = 1) faults are never spurious.
1126 * There are no security implications to leaving a stale TLB when
1127 * increasing the permissions on a page.
1129 * Returns non-zero if a spurious fault was handled, zero otherwise.
1131 * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
1132 * (Optional Invalidation).
1135 spurious_kernel_fault(unsigned long error_code
, unsigned long address
)
1145 * Only writes to RO or instruction fetches from NX may cause
1148 * These could be from user or supervisor accesses but the TLB
1149 * is only lazily flushed after a kernel mapping protection
1150 * change, so user accesses are not expected to cause spurious
1153 if (error_code
!= (X86_PF_WRITE
| X86_PF_PROT
) &&
1154 error_code
!= (X86_PF_INSTR
| X86_PF_PROT
))
1157 pgd
= init_mm
.pgd
+ pgd_index(address
);
1158 if (!pgd_present(*pgd
))
1161 p4d
= p4d_offset(pgd
, address
);
1162 if (!p4d_present(*p4d
))
1165 if (p4d_large(*p4d
))
1166 return spurious_kernel_fault_check(error_code
, (pte_t
*) p4d
);
1168 pud
= pud_offset(p4d
, address
);
1169 if (!pud_present(*pud
))
1172 if (pud_large(*pud
))
1173 return spurious_kernel_fault_check(error_code
, (pte_t
*) pud
);
1175 pmd
= pmd_offset(pud
, address
);
1176 if (!pmd_present(*pmd
))
1179 if (pmd_large(*pmd
))
1180 return spurious_kernel_fault_check(error_code
, (pte_t
*) pmd
);
1182 pte
= pte_offset_kernel(pmd
, address
);
1183 if (!pte_present(*pte
))
1186 ret
= spurious_kernel_fault_check(error_code
, pte
);
1191 * Make sure we have permissions in PMD.
1192 * If not, then there's a bug in the page tables:
1194 ret
= spurious_kernel_fault_check(error_code
, (pte_t
*) pmd
);
1195 WARN_ONCE(!ret
, "PMD has incorrect permission bits\n");
1199 NOKPROBE_SYMBOL(spurious_kernel_fault
);
1201 int show_unhandled_signals
= 1;
1204 access_error(unsigned long error_code
, struct vm_area_struct
*vma
)
1206 /* This is only called for the current mm, so: */
1207 bool foreign
= false;
1210 * Read or write was blocked by protection keys. This is
1211 * always an unconditional error and can never result in
1212 * a follow-up action to resolve the fault, like a COW.
1214 if (error_code
& X86_PF_PK
)
1218 * Make sure to check the VMA so that we do not perform
1219 * faults just to hit a X86_PF_PK as soon as we fill in a
1222 if (!arch_vma_access_permitted(vma
, (error_code
& X86_PF_WRITE
),
1223 (error_code
& X86_PF_INSTR
), foreign
))
1226 if (error_code
& X86_PF_WRITE
) {
1227 /* write, present and write, not present: */
1228 if (unlikely(!(vma
->vm_flags
& VM_WRITE
)))
1233 /* read, present: */
1234 if (unlikely(error_code
& X86_PF_PROT
))
1237 /* read, not present: */
1238 if (unlikely(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
))))
1244 static int fault_in_kernel_space(unsigned long address
)
1247 * On 64-bit systems, the vsyscall page is at an address above
1248 * TASK_SIZE_MAX, but is not considered part of the kernel
1251 if (IS_ENABLED(CONFIG_X86_64
) && is_vsyscall_vaddr(address
))
1254 return address
>= TASK_SIZE_MAX
;
1258 * Called for all faults where 'address' is part of the kernel address
1259 * space. Might get called for faults that originate from *code* that
1260 * ran in userspace or the kernel.
1263 do_kern_addr_fault(struct pt_regs
*regs
, unsigned long hw_error_code
,
1264 unsigned long address
)
1267 * Protection keys exceptions only happen on user pages. We
1268 * have no user pages in the kernel portion of the address
1269 * space, so do not expect them here.
1271 WARN_ON_ONCE(hw_error_code
& X86_PF_PK
);
1274 * We can fault-in kernel-space virtual memory on-demand. The
1275 * 'reference' page table is init_mm.pgd.
1277 * NOTE! We MUST NOT take any locks for this case. We may
1278 * be in an interrupt or a critical region, and should
1279 * only copy the information from the master page table,
1282 * Before doing this on-demand faulting, ensure that the
1283 * fault is not any of the following:
1284 * 1. A fault on a PTE with a reserved bit set.
1285 * 2. A fault caused by a user-mode access. (Do not demand-
1286 * fault kernel memory due to user-mode accesses).
1287 * 3. A fault caused by a page-level protection violation.
1288 * (A demand fault would be on a non-present page which
1289 * would have X86_PF_PROT==0).
1291 if (!(hw_error_code
& (X86_PF_RSVD
| X86_PF_USER
| X86_PF_PROT
))) {
1292 if (vmalloc_fault(address
) >= 0)
1296 /* Was the fault spurious, caused by lazy TLB invalidation? */
1297 if (spurious_kernel_fault(hw_error_code
, address
))
1300 /* kprobes don't want to hook the spurious faults: */
1301 if (kprobes_fault(regs
))
1305 * Note, despite being a "bad area", there are quite a few
1306 * acceptable reasons to get here, such as erratum fixups
1307 * and handling kernel code that can fault, like get_user().
1309 * Don't take the mm semaphore here. If we fixup a prefetch
1310 * fault we could otherwise deadlock:
1312 bad_area_nosemaphore(regs
, hw_error_code
, address
);
1314 NOKPROBE_SYMBOL(do_kern_addr_fault
);
1316 /* Handle faults in the user portion of the address space */
1318 void do_user_addr_fault(struct pt_regs
*regs
,
1319 unsigned long hw_error_code
,
1320 unsigned long address
)
1322 struct vm_area_struct
*vma
;
1323 struct task_struct
*tsk
;
1324 struct mm_struct
*mm
;
1325 vm_fault_t fault
, major
= 0;
1326 unsigned int flags
= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_KILLABLE
;
1331 /* kprobes don't want to hook the spurious faults: */
1332 if (unlikely(kprobes_fault(regs
)))
1336 * Reserved bits are never expected to be set on
1337 * entries in the user portion of the page tables.
1339 if (unlikely(hw_error_code
& X86_PF_RSVD
))
1340 pgtable_bad(regs
, hw_error_code
, address
);
1343 * If SMAP is on, check for invalid kernel (supervisor) access to user
1344 * pages in the user address space. The odd case here is WRUSS,
1345 * which, according to the preliminary documentation, does not respect
1346 * SMAP and will have the USER bit set so, in all cases, SMAP
1347 * enforcement appears to be consistent with the USER bit.
1349 if (unlikely(cpu_feature_enabled(X86_FEATURE_SMAP
) &&
1350 !(hw_error_code
& X86_PF_USER
) &&
1351 !(regs
->flags
& X86_EFLAGS_AC
)))
1353 bad_area_nosemaphore(regs
, hw_error_code
, address
);
1358 * If we're in an interrupt, have no user context or are running
1359 * in a region with pagefaults disabled then we must not take the fault
1361 if (unlikely(faulthandler_disabled() || !mm
)) {
1362 bad_area_nosemaphore(regs
, hw_error_code
, address
);
1367 * It's safe to allow irq's after cr2 has been saved and the
1368 * vmalloc fault has been handled.
1370 * User-mode registers count as a user access even for any
1371 * potential system fault or CPU buglet:
1373 if (user_mode(regs
)) {
1375 flags
|= FAULT_FLAG_USER
;
1377 if (regs
->flags
& X86_EFLAGS_IF
)
1381 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS
, 1, regs
, address
);
1383 if (hw_error_code
& X86_PF_WRITE
)
1384 flags
|= FAULT_FLAG_WRITE
;
1385 if (hw_error_code
& X86_PF_INSTR
)
1386 flags
|= FAULT_FLAG_INSTRUCTION
;
1388 #ifdef CONFIG_X86_64
1390 * Instruction fetch faults in the vsyscall page might need
1391 * emulation. The vsyscall page is at a high address
1392 * (>PAGE_OFFSET), but is considered to be part of the user
1395 * The vsyscall page does not have a "real" VMA, so do this
1396 * emulation before we go searching for VMAs.
1398 if ((hw_error_code
& X86_PF_INSTR
) && is_vsyscall_vaddr(address
)) {
1399 if (emulate_vsyscall(regs
, address
))
1405 * Kernel-mode access to the user address space should only occur
1406 * on well-defined single instructions listed in the exception
1407 * tables. But, an erroneous kernel fault occurring outside one of
1408 * those areas which also holds mmap_sem might deadlock attempting
1409 * to validate the fault against the address space.
1411 * Only do the expensive exception table search when we might be at
1412 * risk of a deadlock. This happens if we
1413 * 1. Failed to acquire mmap_sem, and
1414 * 2. The access did not originate in userspace.
1416 if (unlikely(!down_read_trylock(&mm
->mmap_sem
))) {
1417 if (!user_mode(regs
) && !search_exception_tables(regs
->ip
)) {
1419 * Fault from code in kernel from
1420 * which we do not expect faults.
1422 bad_area_nosemaphore(regs
, hw_error_code
, address
);
1426 down_read(&mm
->mmap_sem
);
1429 * The above down_read_trylock() might have succeeded in
1430 * which case we'll have missed the might_sleep() from
1436 vma
= find_vma(mm
, address
);
1437 if (unlikely(!vma
)) {
1438 bad_area(regs
, hw_error_code
, address
);
1441 if (likely(vma
->vm_start
<= address
))
1443 if (unlikely(!(vma
->vm_flags
& VM_GROWSDOWN
))) {
1444 bad_area(regs
, hw_error_code
, address
);
1447 if (unlikely(expand_stack(vma
, address
))) {
1448 bad_area(regs
, hw_error_code
, address
);
1453 * Ok, we have a good vm_area for this memory access, so
1454 * we can handle it..
1457 if (unlikely(access_error(hw_error_code
, vma
))) {
1458 bad_area_access_error(regs
, hw_error_code
, address
, vma
);
1463 * If for any reason at all we couldn't handle the fault,
1464 * make sure we exit gracefully rather than endlessly redo
1465 * the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if
1466 * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked.
1468 * Note that handle_userfault() may also release and reacquire mmap_sem
1469 * (and not return with VM_FAULT_RETRY), when returning to userland to
1470 * repeat the page fault later with a VM_FAULT_NOPAGE retval
1471 * (potentially after handling any pending signal during the return to
1472 * userland). The return to userland is identified whenever
1473 * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags.
1475 fault
= handle_mm_fault(vma
, address
, flags
);
1476 major
|= fault
& VM_FAULT_MAJOR
;
1479 * If we need to retry the mmap_sem has already been released,
1480 * and if there is a fatal signal pending there is no guarantee
1481 * that we made any progress. Handle this case first.
1483 if (unlikely(fault
& VM_FAULT_RETRY
)) {
1484 /* Retry at most once */
1485 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
1486 flags
&= ~FAULT_FLAG_ALLOW_RETRY
;
1487 flags
|= FAULT_FLAG_TRIED
;
1488 if (!fatal_signal_pending(tsk
))
1492 /* User mode? Just return to handle the fatal exception */
1493 if (flags
& FAULT_FLAG_USER
)
1496 /* Not returning to user mode? Handle exceptions or die: */
1497 no_context(regs
, hw_error_code
, address
, SIGBUS
, BUS_ADRERR
);
1501 up_read(&mm
->mmap_sem
);
1502 if (unlikely(fault
& VM_FAULT_ERROR
)) {
1503 mm_fault_error(regs
, hw_error_code
, address
, fault
);
1508 * Major/minor page fault accounting. If any of the events
1509 * returned VM_FAULT_MAJOR, we account it as a major fault.
1513 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ
, 1, regs
, address
);
1516 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN
, 1, regs
, address
);
1519 check_v8086_mode(regs
, address
, tsk
);
1521 NOKPROBE_SYMBOL(do_user_addr_fault
);
1524 * This routine handles page faults. It determines the address,
1525 * and the problem, and then passes it off to one of the appropriate
1528 static noinline
void
1529 __do_page_fault(struct pt_regs
*regs
, unsigned long hw_error_code
,
1530 unsigned long address
)
1532 prefetchw(¤t
->mm
->mmap_sem
);
1534 if (unlikely(kmmio_fault(regs
, address
)))
1537 /* Was the fault on kernel-controlled part of the address space? */
1538 if (unlikely(fault_in_kernel_space(address
)))
1539 do_kern_addr_fault(regs
, hw_error_code
, address
);
1541 do_user_addr_fault(regs
, hw_error_code
, address
);
1543 NOKPROBE_SYMBOL(__do_page_fault
);
1545 static nokprobe_inline
void
1546 trace_page_fault_entries(unsigned long address
, struct pt_regs
*regs
,
1547 unsigned long error_code
)
1549 if (user_mode(regs
))
1550 trace_page_fault_user(address
, regs
, error_code
);
1552 trace_page_fault_kernel(address
, regs
, error_code
);
1556 * We must have this function blacklisted from kprobes, tagged with notrace
1557 * and call read_cr2() before calling anything else. To avoid calling any
1558 * kind of tracing machinery before we've observed the CR2 value.
1560 * exception_{enter,exit}() contains all sorts of tracepoints.
1562 dotraplinkage
void notrace
1563 do_page_fault(struct pt_regs
*regs
, unsigned long error_code
)
1565 unsigned long address
= read_cr2(); /* Get the faulting address */
1566 enum ctx_state prev_state
;
1568 prev_state
= exception_enter();
1569 if (trace_pagefault_enabled())
1570 trace_page_fault_entries(address
, regs
, error_code
);
1572 __do_page_fault(regs
, error_code
, address
);
1573 exception_exit(prev_state
);
1575 NOKPROBE_SYMBOL(do_page_fault
);