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