1
2
3
4
5
6
7#include <linux/sched.h>
8#include <linux/sched/task_stack.h>
9#include <linux/kdebug.h>
10#include <linux/extable.h>
11#include <linux/memblock.h>
12#include <linux/kprobes.h>
13#include <linux/mmiotrace.h>
14#include <linux/perf_event.h>
15#include <linux/hugetlb.h>
16#include <linux/prefetch.h>
17#include <linux/context_tracking.h>
18#include <linux/uaccess.h>
19#include <linux/efi.h>
20#include <linux/mm_types.h>
21
22#include <asm/cpufeature.h>
23#include <asm/traps.h>
24#include <asm/pgalloc.h>
25#include <asm/fixmap.h>
26#include <asm/vsyscall.h>
27#include <asm/vm86.h>
28#include <asm/mmu_context.h>
29#include <asm/efi.h>
30#include <asm/desc.h>
31#include <asm/cpu_entry_area.h>
32#include <asm/kvm_para.h>
33#include <asm/vdso.h>
34
35#define CREATE_TRACE_POINTS
36#include <asm/trace/exceptions.h>
37
38
39
40
41
42static nokprobe_inline int
43kmmio_fault(struct pt_regs *regs, unsigned long addr)
44{
45 if (unlikely(is_kmmio_active()))
46 if (kmmio_handler(regs, addr) == 1)
47 return -1;
48 return 0;
49}
50
51static nokprobe_inline int kprobes_fault(struct pt_regs *regs)
52{
53 int ret = 0;
54
55
56 if (kprobes_built_in() && !user_mode(regs)) {
57 preempt_disable();
58 if (kprobe_running() && kprobe_fault_handler(regs, 14))
59 ret = 1;
60 preempt_enable();
61 }
62
63 return ret;
64}
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81static inline int
82check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
83 unsigned char opcode, int *prefetch)
84{
85 unsigned char instr_hi = opcode & 0xf0;
86 unsigned char instr_lo = opcode & 0x0f;
87
88 switch (instr_hi) {
89 case 0x20:
90 case 0x30:
91
92
93
94
95
96
97 return ((instr_lo & 7) == 0x6);
98#ifdef CONFIG_X86_64
99 case 0x40:
100
101
102
103
104
105
106
107 return (!user_mode(regs) || user_64bit_mode(regs));
108#endif
109 case 0x60:
110
111 return (instr_lo & 0xC) == 0x4;
112 case 0xF0:
113
114 return !instr_lo || (instr_lo>>1) == 1;
115 case 0x00:
116
117 if (probe_kernel_address(instr, opcode))
118 return 0;
119
120 *prefetch = (instr_lo == 0xF) &&
121 (opcode == 0x0D || opcode == 0x18);
122 return 0;
123 default:
124 return 0;
125 }
126}
127
128static int
129is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
130{
131 unsigned char *max_instr;
132 unsigned char *instr;
133 int prefetch = 0;
134
135
136
137
138
139 if (error_code & X86_PF_INSTR)
140 return 0;
141
142 instr = (void *)convert_ip_to_linear(current, regs);
143 max_instr = instr + 15;
144
145 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE_MAX)
146 return 0;
147
148 while (instr < max_instr) {
149 unsigned char opcode;
150
151 if (probe_kernel_address(instr, opcode))
152 break;
153
154 instr++;
155
156 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
157 break;
158 }
159 return prefetch;
160}
161
162DEFINE_SPINLOCK(pgd_lock);
163LIST_HEAD(pgd_list);
164
165#ifdef CONFIG_X86_32
166static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
167{
168 unsigned index = pgd_index(address);
169 pgd_t *pgd_k;
170 p4d_t *p4d, *p4d_k;
171 pud_t *pud, *pud_k;
172 pmd_t *pmd, *pmd_k;
173
174 pgd += index;
175 pgd_k = init_mm.pgd + index;
176
177 if (!pgd_present(*pgd_k))
178 return NULL;
179
180
181
182
183
184
185 p4d = p4d_offset(pgd, address);
186 p4d_k = p4d_offset(pgd_k, address);
187 if (!p4d_present(*p4d_k))
188 return NULL;
189
190 pud = pud_offset(p4d, address);
191 pud_k = pud_offset(p4d_k, address);
192 if (!pud_present(*pud_k))
193 return NULL;
194
195 pmd = pmd_offset(pud, address);
196 pmd_k = pmd_offset(pud_k, address);
197 if (!pmd_present(*pmd_k))
198 return NULL;
199
200 if (!pmd_present(*pmd))
201 set_pmd(pmd, *pmd_k);
202 else
203 BUG_ON(pmd_pfn(*pmd) != pmd_pfn(*pmd_k));
204
205 return pmd_k;
206}
207
208static void vmalloc_sync(void)
209{
210 unsigned long address;
211
212 if (SHARED_KERNEL_PMD)
213 return;
214
215 for (address = VMALLOC_START & PMD_MASK;
216 address >= TASK_SIZE_MAX && address < FIXADDR_TOP;
217 address += PMD_SIZE) {
218 struct page *page;
219
220 spin_lock(&pgd_lock);
221 list_for_each_entry(page, &pgd_list, lru) {
222 spinlock_t *pgt_lock;
223 pmd_t *ret;
224
225
226 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
227
228 spin_lock(pgt_lock);
229 ret = vmalloc_sync_one(page_address(page), address);
230 spin_unlock(pgt_lock);
231
232 if (!ret)
233 break;
234 }
235 spin_unlock(&pgd_lock);
236 }
237}
238
239void vmalloc_sync_mappings(void)
240{
241 vmalloc_sync();
242}
243
244void vmalloc_sync_unmappings(void)
245{
246 vmalloc_sync();
247}
248
249
250
251
252
253
254static noinline int vmalloc_fault(unsigned long address)
255{
256 unsigned long pgd_paddr;
257 pmd_t *pmd_k;
258 pte_t *pte_k;
259
260
261 if (!(address >= VMALLOC_START && address < VMALLOC_END))
262 return -1;
263
264 WARN_ON_ONCE(in_nmi());
265
266
267
268
269
270
271
272
273 pgd_paddr = read_cr3_pa();
274 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
275 if (!pmd_k)
276 return -1;
277
278 if (pmd_large(*pmd_k))
279 return 0;
280
281 pte_k = pte_offset_kernel(pmd_k, address);
282 if (!pte_present(*pte_k))
283 return -1;
284
285 return 0;
286}
287NOKPROBE_SYMBOL(vmalloc_fault);
288
289
290
291
292static inline void
293check_v8086_mode(struct pt_regs *regs, unsigned long address,
294 struct task_struct *tsk)
295{
296#ifdef CONFIG_VM86
297 unsigned long bit;
298
299 if (!v8086_mode(regs) || !tsk->thread.vm86)
300 return;
301
302 bit = (address - 0xA0000) >> PAGE_SHIFT;
303 if (bit < 32)
304 tsk->thread.vm86->screen_bitmap |= 1 << bit;
305#endif
306}
307
308static bool low_pfn(unsigned long pfn)
309{
310 return pfn < max_low_pfn;
311}
312
313static void dump_pagetable(unsigned long address)
314{
315 pgd_t *base = __va(read_cr3_pa());
316 pgd_t *pgd = &base[pgd_index(address)];
317 p4d_t *p4d;
318 pud_t *pud;
319 pmd_t *pmd;
320 pte_t *pte;
321
322#ifdef CONFIG_X86_PAE
323 pr_info("*pdpt = %016Lx ", pgd_val(*pgd));
324 if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
325 goto out;
326#define pr_pde pr_cont
327#else
328#define pr_pde pr_info
329#endif
330 p4d = p4d_offset(pgd, address);
331 pud = pud_offset(p4d, address);
332 pmd = pmd_offset(pud, address);
333 pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
334#undef pr_pde
335
336
337
338
339
340
341
342 if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
343 goto out;
344
345 pte = pte_offset_kernel(pmd, address);
346 pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
347out:
348 pr_cont("\n");
349}
350
351#else
352
353void vmalloc_sync_mappings(void)
354{
355
356
357
358
359 sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END);
360}
361
362void vmalloc_sync_unmappings(void)
363{
364
365
366
367
368}
369
370
371
372
373
374
375static noinline int vmalloc_fault(unsigned long address)
376{
377 pgd_t *pgd, *pgd_k;
378 p4d_t *p4d, *p4d_k;
379 pud_t *pud;
380 pmd_t *pmd;
381 pte_t *pte;
382
383
384 if (!(address >= VMALLOC_START && address < VMALLOC_END))
385 return -1;
386
387 WARN_ON_ONCE(in_nmi());
388
389
390
391
392
393
394 pgd = (pgd_t *)__va(read_cr3_pa()) + pgd_index(address);
395 pgd_k = pgd_offset_k(address);
396 if (pgd_none(*pgd_k))
397 return -1;
398
399 if (pgtable_l5_enabled()) {
400 if (pgd_none(*pgd)) {
401 set_pgd(pgd, *pgd_k);
402 arch_flush_lazy_mmu_mode();
403 } else {
404 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_k));
405 }
406 }
407
408
409 p4d = p4d_offset(pgd, address);
410 p4d_k = p4d_offset(pgd_k, address);
411 if (p4d_none(*p4d_k))
412 return -1;
413
414 if (p4d_none(*p4d) && !pgtable_l5_enabled()) {
415 set_p4d(p4d, *p4d_k);
416 arch_flush_lazy_mmu_mode();
417 } else {
418 BUG_ON(p4d_pfn(*p4d) != p4d_pfn(*p4d_k));
419 }
420
421 BUILD_BUG_ON(CONFIG_PGTABLE_LEVELS < 4);
422
423 pud = pud_offset(p4d, address);
424 if (pud_none(*pud))
425 return -1;
426
427 if (pud_large(*pud))
428 return 0;
429
430 pmd = pmd_offset(pud, address);
431 if (pmd_none(*pmd))
432 return -1;
433
434 if (pmd_large(*pmd))
435 return 0;
436
437 pte = pte_offset_kernel(pmd, address);
438 if (!pte_present(*pte))
439 return -1;
440
441 return 0;
442}
443NOKPROBE_SYMBOL(vmalloc_fault);
444
445#ifdef CONFIG_CPU_SUP_AMD
446static const char errata93_warning[] =
447KERN_ERR
448"******* Your BIOS seems to not contain a fix for K8 errata #93\n"
449"******* Working around it, but it may cause SEGVs or burn power.\n"
450"******* Please consider a BIOS update.\n"
451"******* Disabling USB legacy in the BIOS may also help.\n";
452#endif
453
454
455
456
457static inline void
458check_v8086_mode(struct pt_regs *regs, unsigned long address,
459 struct task_struct *tsk)
460{
461}
462
463static int bad_address(void *p)
464{
465 unsigned long dummy;
466
467 return probe_kernel_address((unsigned long *)p, dummy);
468}
469
470static void dump_pagetable(unsigned long address)
471{
472 pgd_t *base = __va(read_cr3_pa());
473 pgd_t *pgd = base + pgd_index(address);
474 p4d_t *p4d;
475 pud_t *pud;
476 pmd_t *pmd;
477 pte_t *pte;
478
479 if (bad_address(pgd))
480 goto bad;
481
482 pr_info("PGD %lx ", pgd_val(*pgd));
483
484 if (!pgd_present(*pgd))
485 goto out;
486
487 p4d = p4d_offset(pgd, address);
488 if (bad_address(p4d))
489 goto bad;
490
491 pr_cont("P4D %lx ", p4d_val(*p4d));
492 if (!p4d_present(*p4d) || p4d_large(*p4d))
493 goto out;
494
495 pud = pud_offset(p4d, address);
496 if (bad_address(pud))
497 goto bad;
498
499 pr_cont("PUD %lx ", pud_val(*pud));
500 if (!pud_present(*pud) || pud_large(*pud))
501 goto out;
502
503 pmd = pmd_offset(pud, address);
504 if (bad_address(pmd))
505 goto bad;
506
507 pr_cont("PMD %lx ", pmd_val(*pmd));
508 if (!pmd_present(*pmd) || pmd_large(*pmd))
509 goto out;
510
511 pte = pte_offset_kernel(pmd, address);
512 if (bad_address(pte))
513 goto bad;
514
515 pr_cont("PTE %lx", pte_val(*pte));
516out:
517 pr_cont("\n");
518 return;
519bad:
520 pr_info("BAD\n");
521}
522
523#endif
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539static int is_errata93(struct pt_regs *regs, unsigned long address)
540{
541#if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
542 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
543 || boot_cpu_data.x86 != 0xf)
544 return 0;
545
546 if (address != regs->ip)
547 return 0;
548
549 if ((address >> 32) != 0)
550 return 0;
551
552 address |= 0xffffffffUL << 32;
553 if ((address >= (u64)_stext && address <= (u64)_etext) ||
554 (address >= MODULES_VADDR && address <= MODULES_END)) {
555 printk_once(errata93_warning);
556 regs->ip = address;
557 return 1;
558 }
559#endif
560 return 0;
561}
562
563
564
565
566
567
568
569
570
571static int is_errata100(struct pt_regs *regs, unsigned long address)
572{
573#ifdef CONFIG_X86_64
574 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
575 return 1;
576#endif
577 return 0;
578}
579
580static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
581{
582#ifdef CONFIG_X86_F00F_BUG
583 unsigned long nr;
584
585
586
587
588 if (boot_cpu_has_bug(X86_BUG_F00F)) {
589 nr = (address - idt_descr.address) >> 3;
590
591 if (nr == 6) {
592 do_invalid_op(regs, 0);
593 return 1;
594 }
595 }
596#endif
597 return 0;
598}
599
600static void
601show_fault_oops(struct pt_regs *regs, unsigned long error_code,
602 unsigned long address)
603{
604 if (!oops_may_print())
605 return;
606
607 if (error_code & X86_PF_INSTR) {
608 unsigned int level;
609 pgd_t *pgd;
610 pte_t *pte;
611
612 pgd = __va(read_cr3_pa());
613 pgd += pgd_index(address);
614
615 pte = lookup_address_in_pgd(pgd, address, &level);
616
617 if (pte && pte_present(*pte) && !pte_exec(*pte))
618 pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n",
619 from_kuid(&init_user_ns, current_uid()));
620 if (pte && pte_present(*pte) && pte_exec(*pte) &&
621 (pgd_flags(*pgd) & _PAGE_USER) &&
622 (__read_cr4() & X86_CR4_SMEP))
623 pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n",
624 from_kuid(&init_user_ns, current_uid()));
625 }
626
627 pr_alert("BUG: unable to handle kernel %s at %px\n",
628 address < PAGE_SIZE ? "NULL pointer dereference" : "paging request",
629 (void *)address);
630
631 dump_pagetable(address);
632}
633
634static noinline void
635pgtable_bad(struct pt_regs *regs, unsigned long error_code,
636 unsigned long address)
637{
638 struct task_struct *tsk;
639 unsigned long flags;
640 int sig;
641
642 flags = oops_begin();
643 tsk = current;
644 sig = SIGKILL;
645
646 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
647 tsk->comm, address);
648 dump_pagetable(address);
649
650 tsk->thread.cr2 = address;
651 tsk->thread.trap_nr = X86_TRAP_PF;
652 tsk->thread.error_code = error_code;
653
654 if (__die("Bad pagetable", regs, error_code))
655 sig = 0;
656
657 oops_end(flags, regs, sig);
658}
659
660static void sanitize_error_code(unsigned long address,
661 unsigned long *error_code)
662{
663
664
665
666
667
668 if (address >= TASK_SIZE_MAX)
669 *error_code |= X86_PF_PROT;
670}
671
672static void set_signal_archinfo(unsigned long address,
673 unsigned long error_code)
674{
675 struct task_struct *tsk = current;
676
677 tsk->thread.trap_nr = X86_TRAP_PF;
678 tsk->thread.error_code = error_code | X86_PF_USER;
679 tsk->thread.cr2 = address;
680}
681
682static noinline void
683no_context(struct pt_regs *regs, unsigned long error_code,
684 unsigned long address, int signal, int si_code)
685{
686 struct task_struct *tsk = current;
687 unsigned long flags;
688 int sig;
689
690
691 if (fixup_exception(regs, X86_TRAP_PF)) {
692
693
694
695
696
697 if (in_interrupt())
698 return;
699
700
701
702
703
704
705
706 if (current->thread.sig_on_uaccess_err && signal) {
707 sanitize_error_code(address, &error_code);
708
709 set_signal_archinfo(address, error_code);
710
711
712 force_sig_fault(signal, si_code, (void __user *)address,
713 tsk);
714 }
715
716
717
718
719 return;
720 }
721
722#ifdef CONFIG_VMAP_STACK
723
724
725
726
727
728 if (is_vmalloc_addr((void *)address) &&
729 (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) ||
730 address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) {
731 unsigned long stack = __this_cpu_ist_top_va(DF) - sizeof(void *);
732
733
734
735
736
737
738
739
740
741
742 asm volatile ("movq %[stack], %%rsp\n\t"
743 "call handle_stack_overflow\n\t"
744 "1: jmp 1b"
745 : ASM_CALL_CONSTRAINT
746 : "D" ("kernel stack overflow (page fault)"),
747 "S" (regs), "d" (address),
748 [stack] "rm" (stack));
749 unreachable();
750 }
751#endif
752
753
754
755
756
757
758
759
760
761
762
763
764 if (is_prefetch(regs, error_code, address))
765 return;
766
767 if (is_errata93(regs, address))
768 return;
769
770
771
772
773
774 if (IS_ENABLED(CONFIG_EFI))
775 efi_recover_from_page_fault(address);
776
777
778
779
780
781 flags = oops_begin();
782
783 show_fault_oops(regs, error_code, address);
784
785 if (task_stack_end_corrupted(tsk))
786 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
787
788 tsk->thread.cr2 = address;
789 tsk->thread.trap_nr = X86_TRAP_PF;
790 tsk->thread.error_code = error_code;
791
792 sig = SIGKILL;
793 if (__die("Oops", regs, error_code))
794 sig = 0;
795
796
797 printk(KERN_DEFAULT "CR2: %016lx\n", address);
798
799 oops_end(flags, regs, sig);
800}
801
802
803
804
805
806static inline void
807show_signal_msg(struct pt_regs *regs, unsigned long error_code,
808 unsigned long address, struct task_struct *tsk)
809{
810 const char *loglvl = task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG;
811
812 if (!unhandled_signal(tsk, SIGSEGV))
813 return;
814
815 if (!printk_ratelimit())
816 return;
817
818 printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx",
819 loglvl, tsk->comm, task_pid_nr(tsk), address,
820 (void *)regs->ip, (void *)regs->sp, error_code);
821
822 print_vma_addr(KERN_CONT " in ", regs->ip);
823
824 printk(KERN_CONT "\n");
825
826 show_opcodes(regs, loglvl);
827}
828
829static void
830__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
831 unsigned long address, u32 pkey, int si_code)
832{
833 struct task_struct *tsk = current;
834
835
836 if (error_code & X86_PF_USER) {
837
838
839
840 local_irq_enable();
841
842
843
844
845
846 if (is_prefetch(regs, error_code, address))
847 return;
848
849 if (is_errata100(regs, address))
850 return;
851
852#ifdef CONFIG_X86_64
853
854
855
856
857 if (unlikely((error_code & X86_PF_INSTR) &&
858 ((address & ~0xfff) == VSYSCALL_ADDR))) {
859 if (emulate_vsyscall(regs, address))
860 return;
861 }
862#endif
863
864 sanitize_error_code(address, &error_code);
865
866 if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address))
867 return;
868
869 if (likely(show_unhandled_signals))
870 show_signal_msg(regs, error_code, address, tsk);
871
872 set_signal_archinfo(address, error_code);
873
874 if (si_code == SEGV_PKUERR)
875 force_sig_pkuerr((void __user *)address, pkey);
876
877 force_sig_fault(SIGSEGV, si_code, (void __user *)address, tsk);
878
879 return;
880 }
881
882 if (is_f00f_bug(regs, address))
883 return;
884
885 no_context(regs, error_code, address, SIGSEGV, si_code);
886}
887
888static noinline void
889bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
890 unsigned long address)
891{
892 __bad_area_nosemaphore(regs, error_code, address, 0, SEGV_MAPERR);
893}
894
895static void
896__bad_area(struct pt_regs *regs, unsigned long error_code,
897 unsigned long address, u32 pkey, int si_code)
898{
899 struct mm_struct *mm = current->mm;
900
901
902
903
904 mmap_read_unlock(mm);
905
906 __bad_area_nosemaphore(regs, error_code, address, pkey, si_code);
907}
908
909static noinline void
910bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
911{
912 __bad_area(regs, error_code, address, 0, SEGV_MAPERR);
913}
914
915static inline bool bad_area_access_from_pkeys(unsigned long error_code,
916 struct vm_area_struct *vma)
917{
918
919 bool foreign = false;
920
921 if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
922 return false;
923 if (error_code & X86_PF_PK)
924 return true;
925
926 if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
927 (error_code & X86_PF_INSTR), foreign))
928 return true;
929 return false;
930}
931
932static noinline void
933bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
934 unsigned long address, struct vm_area_struct *vma)
935{
936
937
938
939
940
941 if (bad_area_access_from_pkeys(error_code, vma)) {
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962 u32 pkey = vma_pkey(vma);
963
964 __bad_area(regs, error_code, address, pkey, SEGV_PKUERR);
965 } else {
966 __bad_area(regs, error_code, address, 0, SEGV_ACCERR);
967 }
968}
969
970
971static void
972do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
973 vm_fault_t fault)
974{
975 struct task_struct *tsk = current;
976
977
978 if (!(error_code & X86_PF_USER)) {
979 no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
980 return;
981 }
982
983
984 if (is_prefetch(regs, error_code, address))
985 return;
986
987 sanitize_error_code(address, &error_code);
988
989 if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address))
990 return;
991
992 set_signal_archinfo(address, error_code);
993
994#ifdef CONFIG_MEMORY_FAILURE
995 if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
996 unsigned lsb = 0;
997
998 pr_err(
999 "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
1000 tsk->comm, tsk->pid, address);
1001 if (fault & VM_FAULT_HWPOISON_LARGE)
1002 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
1003 if (fault & VM_FAULT_HWPOISON)
1004 lsb = PAGE_SHIFT;
1005 force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb, tsk);
1006 return;
1007 }
1008#endif
1009 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address, tsk);
1010}
1011
1012static noinline void
1013mm_fault_error(struct pt_regs *regs, unsigned long error_code,
1014 unsigned long address, vm_fault_t fault)
1015{
1016 if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) {
1017 no_context(regs, error_code, address, 0, 0);
1018 return;
1019 }
1020
1021 if (fault & VM_FAULT_OOM) {
1022
1023 if (!(error_code & X86_PF_USER)) {
1024 no_context(regs, error_code, address,
1025 SIGSEGV, SEGV_MAPERR);
1026 return;
1027 }
1028
1029
1030
1031
1032
1033
1034 pagefault_out_of_memory();
1035 } else {
1036 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
1037 VM_FAULT_HWPOISON_LARGE))
1038 do_sigbus(regs, error_code, address, fault);
1039 else if (fault & VM_FAULT_SIGSEGV)
1040 bad_area_nosemaphore(regs, error_code, address);
1041 else
1042 BUG();
1043 }
1044}
1045
1046static int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte)
1047{
1048 if ((error_code & X86_PF_WRITE) && !pte_write(*pte))
1049 return 0;
1050
1051 if ((error_code & X86_PF_INSTR) && !pte_exec(*pte))
1052 return 0;
1053
1054
1055
1056
1057 if ((error_code & X86_PF_PK))
1058 return 1;
1059
1060 return 1;
1061}
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084static noinline int
1085spurious_kernel_fault(unsigned long error_code, unsigned long address)
1086{
1087 pgd_t *pgd;
1088 p4d_t *p4d;
1089 pud_t *pud;
1090 pmd_t *pmd;
1091 pte_t *pte;
1092 int ret;
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103 if (error_code != (X86_PF_WRITE | X86_PF_PROT) &&
1104 error_code != (X86_PF_INSTR | X86_PF_PROT))
1105 return 0;
1106
1107 pgd = init_mm.pgd + pgd_index(address);
1108 if (!pgd_present(*pgd))
1109 return 0;
1110
1111 p4d = p4d_offset(pgd, address);
1112 if (!p4d_present(*p4d))
1113 return 0;
1114
1115 if (p4d_large(*p4d))
1116 return spurious_kernel_fault_check(error_code, (pte_t *) p4d);
1117
1118 pud = pud_offset(p4d, address);
1119 if (!pud_present(*pud))
1120 return 0;
1121
1122 if (pud_large(*pud))
1123 return spurious_kernel_fault_check(error_code, (pte_t *) pud);
1124
1125 pmd = pmd_offset(pud, address);
1126 if (!pmd_present(*pmd))
1127 return 0;
1128
1129 if (pmd_large(*pmd))
1130 return spurious_kernel_fault_check(error_code, (pte_t *) pmd);
1131
1132 pte = pte_offset_kernel(pmd, address);
1133 if (!pte_present(*pte))
1134 return 0;
1135
1136 ret = spurious_kernel_fault_check(error_code, pte);
1137 if (!ret)
1138 return 0;
1139
1140
1141
1142
1143
1144 ret = spurious_kernel_fault_check(error_code, (pte_t *) pmd);
1145 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
1146
1147 return ret;
1148}
1149NOKPROBE_SYMBOL(spurious_kernel_fault);
1150
1151int show_unhandled_signals = 1;
1152
1153static inline int
1154access_error(unsigned long error_code, struct vm_area_struct *vma)
1155{
1156
1157 bool foreign = false;
1158
1159
1160
1161
1162
1163
1164 if (error_code & X86_PF_PK)
1165 return 1;
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176 if (unlikely(error_code & X86_PF_SGX))
1177 return 1;
1178
1179
1180
1181
1182
1183
1184 if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
1185 (error_code & X86_PF_INSTR), foreign))
1186 return 1;
1187
1188 if (error_code & X86_PF_WRITE) {
1189
1190 if (unlikely(!(vma->vm_flags & VM_WRITE)))
1191 return 1;
1192 return 0;
1193 }
1194
1195
1196 if (unlikely(error_code & X86_PF_PROT))
1197 return 1;
1198
1199
1200 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
1201 return 1;
1202
1203 return 0;
1204}
1205
1206static int fault_in_kernel_space(unsigned long address)
1207{
1208 return address >= TASK_SIZE_MAX;
1209}
1210
1211static inline bool smap_violation(int error_code, struct pt_regs *regs)
1212{
1213 if (!IS_ENABLED(CONFIG_X86_SMAP))
1214 return false;
1215
1216 if (!static_cpu_has(X86_FEATURE_SMAP))
1217 return false;
1218
1219 if (error_code & X86_PF_USER)
1220 return false;
1221
1222 if (!user_mode(regs) && (regs->flags & X86_EFLAGS_AC))
1223 return false;
1224
1225 return true;
1226}
1227
1228
1229
1230
1231
1232
1233static void
1234do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code,
1235 unsigned long address)
1236{
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255 if (!(hw_error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) {
1256 if (vmalloc_fault(address) >= 0)
1257 return;
1258 }
1259
1260
1261 if (spurious_kernel_fault(hw_error_code, address))
1262 return;
1263
1264
1265 if (kprobes_fault(regs))
1266 return;
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276 bad_area_nosemaphore(regs, hw_error_code, address);
1277}
1278NOKPROBE_SYMBOL(do_kern_addr_fault);
1279
1280
1281static inline
1282void do_user_addr_fault(struct pt_regs *regs,
1283 unsigned long hw_error_code,
1284 unsigned long address)
1285{
1286 unsigned long sw_error_code;
1287 struct vm_area_struct *vma;
1288 struct task_struct *tsk;
1289 struct mm_struct *mm;
1290 vm_fault_t fault, major = 0;
1291 unsigned int flags = FAULT_FLAG_DEFAULT;
1292
1293 tsk = current;
1294 mm = tsk->mm;
1295
1296
1297 if (unlikely(kprobes_fault(regs)))
1298 return;
1299
1300 if (unlikely(hw_error_code & X86_PF_RSVD))
1301 pgtable_bad(regs, hw_error_code, address);
1302
1303 if (unlikely(smap_violation(hw_error_code, regs))) {
1304 bad_area_nosemaphore(regs, hw_error_code, address);
1305 return;
1306 }
1307
1308
1309
1310
1311
1312 if (unlikely(faulthandler_disabled() || !mm)) {
1313 bad_area_nosemaphore(regs, hw_error_code, address);
1314 return;
1315 }
1316
1317
1318
1319
1320
1321
1322 sw_error_code = hw_error_code;
1323
1324
1325
1326
1327
1328
1329
1330
1331 if (user_mode(regs)) {
1332 local_irq_enable();
1333
1334
1335
1336
1337
1338
1339
1340 if (!(hw_error_code & X86_PF_USER)) {
1341
1342
1343
1344
1345
1346
1347
1348 pr_warn_once("kernel-mode error from user-mode: %lx\n",
1349 hw_error_code);
1350
1351 sw_error_code |= X86_PF_USER;
1352 }
1353 flags |= FAULT_FLAG_USER;
1354 } else {
1355 if (regs->flags & X86_EFLAGS_IF)
1356 local_irq_enable();
1357 }
1358
1359 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
1360
1361 if (sw_error_code & X86_PF_WRITE)
1362 flags |= FAULT_FLAG_WRITE;
1363 if (sw_error_code & X86_PF_INSTR)
1364 flags |= FAULT_FLAG_INSTRUCTION;
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378 if (unlikely(!mmap_read_trylock(mm))) {
1379 if (!user_mode(regs) && !search_exception_tables(regs->ip)) {
1380
1381
1382
1383
1384 bad_area_nosemaphore(regs, sw_error_code, address);
1385 return;
1386 }
1387retry:
1388 mmap_read_lock(mm);
1389 } else {
1390
1391
1392
1393
1394
1395 might_sleep();
1396 }
1397
1398 vma = find_vma(mm, address);
1399 if (unlikely(!vma)) {
1400 bad_area(regs, sw_error_code, address);
1401 return;
1402 }
1403 if (likely(vma->vm_start <= address))
1404 goto good_area;
1405 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1406 bad_area(regs, sw_error_code, address);
1407 return;
1408 }
1409 if (unlikely(expand_stack(vma, address))) {
1410 bad_area(regs, sw_error_code, address);
1411 return;
1412 }
1413
1414
1415
1416
1417
1418good_area:
1419 if (unlikely(access_error(sw_error_code, vma))) {
1420 bad_area_access_error(regs, sw_error_code, address, vma);
1421 return;
1422 }
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437 fault = handle_mm_fault(vma, address, flags);
1438 major |= fault & VM_FAULT_MAJOR;
1439
1440
1441 if (fault_signal_pending(fault, regs)) {
1442 if (!user_mode(regs))
1443 no_context(regs, sw_error_code, address, SIGBUS,
1444 BUS_ADRERR);
1445 return;
1446 }
1447
1448
1449
1450
1451
1452
1453 if (unlikely((fault & VM_FAULT_RETRY) &&
1454 (flags & FAULT_FLAG_ALLOW_RETRY))) {
1455 flags |= FAULT_FLAG_TRIED;
1456 goto retry;
1457 }
1458
1459 mmap_read_unlock(mm);
1460 if (unlikely(fault & VM_FAULT_ERROR)) {
1461 mm_fault_error(regs, sw_error_code, address, fault);
1462 return;
1463 }
1464
1465
1466
1467
1468
1469 if (major) {
1470 tsk->maj_flt++;
1471 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
1472 } else {
1473 tsk->min_flt++;
1474 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
1475 }
1476
1477 check_v8086_mode(regs, address, tsk);
1478}
1479NOKPROBE_SYMBOL(do_user_addr_fault);
1480
1481
1482
1483
1484
1485
1486static noinline void
1487__do_page_fault(struct pt_regs *regs, unsigned long hw_error_code,
1488 unsigned long address)
1489{
1490 prefetchw(¤t->mm->mmap_sem);
1491
1492 if (unlikely(kmmio_fault(regs, address)))
1493 return;
1494
1495
1496 if (unlikely(fault_in_kernel_space(address)))
1497 do_kern_addr_fault(regs, hw_error_code, address);
1498 else
1499 do_user_addr_fault(regs, hw_error_code, address);
1500}
1501NOKPROBE_SYMBOL(__do_page_fault);
1502
1503static nokprobe_inline void
1504trace_page_fault_entries(unsigned long address, struct pt_regs *regs,
1505 unsigned long error_code)
1506{
1507 if (user_mode(regs))
1508 trace_page_fault_user(address, regs, error_code);
1509 else
1510 trace_page_fault_kernel(address, regs, error_code);
1511}
1512
1513
1514
1515
1516
1517
1518
1519
1520dotraplinkage void notrace
1521do_page_fault(struct pt_regs *regs, unsigned long error_code)
1522{
1523 unsigned long address = read_cr2();
1524 enum ctx_state prev_state;
1525
1526 prev_state = exception_enter();
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543 if (kvm_handle_async_pf(regs, (u32)address))
1544 goto exc_exit;
1545
1546 if (trace_pagefault_enabled())
1547 trace_page_fault_entries(address, regs, error_code);
1548
1549 __do_page_fault(regs, error_code, address);
1550
1551exc_exit:
1552 exception_exit(prev_state);
1553}
1554NOKPROBE_SYMBOL(do_page_fault);
1555