xref: /openbmc/linux/arch/x86/mm/fault.c (revision 023e4163)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
6  */
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>
21 
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(), ...		*/
31 
32 #define CREATE_TRACE_POINTS
33 #include <asm/trace/exceptions.h>
34 
35 /*
36  * Returns 0 if mmiotrace is disabled, or if the fault is not
37  * handled by mmiotrace:
38  */
39 static nokprobe_inline int
40 kmmio_fault(struct pt_regs *regs, unsigned long addr)
41 {
42 	if (unlikely(is_kmmio_active()))
43 		if (kmmio_handler(regs, addr) == 1)
44 			return -1;
45 	return 0;
46 }
47 
48 static nokprobe_inline int kprobes_fault(struct pt_regs *regs)
49 {
50 	if (!kprobes_built_in())
51 		return 0;
52 	if (user_mode(regs))
53 		return 0;
54 	/*
55 	 * To be potentially processing a kprobe fault and to be allowed to call
56 	 * kprobe_running(), we have to be non-preemptible.
57 	 */
58 	if (preemptible())
59 		return 0;
60 	if (!kprobe_running())
61 		return 0;
62 	return kprobe_fault_handler(regs, X86_TRAP_PF);
63 }
64 
65 /*
66  * Prefetch quirks:
67  *
68  * 32-bit mode:
69  *
70  *   Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
71  *   Check that here and ignore it.
72  *
73  * 64-bit mode:
74  *
75  *   Sometimes the CPU reports invalid exceptions on prefetch.
76  *   Check that here and ignore it.
77  *
78  * Opcode checker based on code by Richard Brunner.
79  */
80 static inline int
81 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
82 		      unsigned char opcode, int *prefetch)
83 {
84 	unsigned char instr_hi = opcode & 0xf0;
85 	unsigned char instr_lo = opcode & 0x0f;
86 
87 	switch (instr_hi) {
88 	case 0x20:
89 	case 0x30:
90 		/*
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
95 		 */
96 		return ((instr_lo & 7) == 0x6);
97 #ifdef CONFIG_X86_64
98 	case 0x40:
99 		/*
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.
105 		 */
106 		return (!user_mode(regs) || user_64bit_mode(regs));
107 #endif
108 	case 0x60:
109 		/* 0x64 thru 0x67 are valid prefixes in all modes. */
110 		return (instr_lo & 0xC) == 0x4;
111 	case 0xF0:
112 		/* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
113 		return !instr_lo || (instr_lo>>1) == 1;
114 	case 0x00:
115 		/* Prefetch instruction is 0x0F0D or 0x0F18 */
116 		if (probe_kernel_address(instr, opcode))
117 			return 0;
118 
119 		*prefetch = (instr_lo == 0xF) &&
120 			(opcode == 0x0D || opcode == 0x18);
121 		return 0;
122 	default:
123 		return 0;
124 	}
125 }
126 
127 static int
128 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
129 {
130 	unsigned char *max_instr;
131 	unsigned char *instr;
132 	int prefetch = 0;
133 
134 	/*
135 	 * If it was a exec (instruction fetch) fault on NX page, then
136 	 * do not ignore the fault:
137 	 */
138 	if (error_code & X86_PF_INSTR)
139 		return 0;
140 
141 	instr = (void *)convert_ip_to_linear(current, regs);
142 	max_instr = instr + 15;
143 
144 	if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE_MAX)
145 		return 0;
146 
147 	while (instr < max_instr) {
148 		unsigned char opcode;
149 
150 		if (probe_kernel_address(instr, opcode))
151 			break;
152 
153 		instr++;
154 
155 		if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
156 			break;
157 	}
158 	return prefetch;
159 }
160 
161 DEFINE_SPINLOCK(pgd_lock);
162 LIST_HEAD(pgd_list);
163 
164 #ifdef CONFIG_X86_32
165 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
166 {
167 	unsigned index = pgd_index(address);
168 	pgd_t *pgd_k;
169 	p4d_t *p4d, *p4d_k;
170 	pud_t *pud, *pud_k;
171 	pmd_t *pmd, *pmd_k;
172 
173 	pgd += index;
174 	pgd_k = init_mm.pgd + index;
175 
176 	if (!pgd_present(*pgd_k))
177 		return NULL;
178 
179 	/*
180 	 * set_pgd(pgd, *pgd_k); here would be useless on PAE
181 	 * and redundant with the set_pmd() on non-PAE. As would
182 	 * set_p4d/set_pud.
183 	 */
184 	p4d = p4d_offset(pgd, address);
185 	p4d_k = p4d_offset(pgd_k, address);
186 	if (!p4d_present(*p4d_k))
187 		return NULL;
188 
189 	pud = pud_offset(p4d, address);
190 	pud_k = pud_offset(p4d_k, address);
191 	if (!pud_present(*pud_k))
192 		return NULL;
193 
194 	pmd = pmd_offset(pud, address);
195 	pmd_k = pmd_offset(pud_k, address);
196 	if (!pmd_present(*pmd_k))
197 		return NULL;
198 
199 	if (!pmd_present(*pmd))
200 		set_pmd(pmd, *pmd_k);
201 	else
202 		BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
203 
204 	return pmd_k;
205 }
206 
207 void vmalloc_sync_all(void)
208 {
209 	unsigned long address;
210 
211 	if (SHARED_KERNEL_PMD)
212 		return;
213 
214 	for (address = VMALLOC_START & PMD_MASK;
215 	     address >= TASK_SIZE_MAX && address < FIXADDR_TOP;
216 	     address += PMD_SIZE) {
217 		struct page *page;
218 
219 		spin_lock(&pgd_lock);
220 		list_for_each_entry(page, &pgd_list, lru) {
221 			spinlock_t *pgt_lock;
222 			pmd_t *ret;
223 
224 			/* the pgt_lock only for Xen */
225 			pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
226 
227 			spin_lock(pgt_lock);
228 			ret = vmalloc_sync_one(page_address(page), address);
229 			spin_unlock(pgt_lock);
230 
231 			if (!ret)
232 				break;
233 		}
234 		spin_unlock(&pgd_lock);
235 	}
236 }
237 
238 /*
239  * 32-bit:
240  *
241  *   Handle a fault on the vmalloc or module mapping area
242  */
243 static noinline int vmalloc_fault(unsigned long address)
244 {
245 	unsigned long pgd_paddr;
246 	pmd_t *pmd_k;
247 	pte_t *pte_k;
248 
249 	/* Make sure we are in vmalloc area: */
250 	if (!(address >= VMALLOC_START && address < VMALLOC_END))
251 		return -1;
252 
253 	/*
254 	 * Synchronize this task's top level page-table
255 	 * with the 'reference' page table.
256 	 *
257 	 * Do _not_ use "current" here. We might be inside
258 	 * an interrupt in the middle of a task switch..
259 	 */
260 	pgd_paddr = read_cr3_pa();
261 	pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
262 	if (!pmd_k)
263 		return -1;
264 
265 	if (pmd_large(*pmd_k))
266 		return 0;
267 
268 	pte_k = pte_offset_kernel(pmd_k, address);
269 	if (!pte_present(*pte_k))
270 		return -1;
271 
272 	return 0;
273 }
274 NOKPROBE_SYMBOL(vmalloc_fault);
275 
276 /*
277  * Did it hit the DOS screen memory VA from vm86 mode?
278  */
279 static inline void
280 check_v8086_mode(struct pt_regs *regs, unsigned long address,
281 		 struct task_struct *tsk)
282 {
283 #ifdef CONFIG_VM86
284 	unsigned long bit;
285 
286 	if (!v8086_mode(regs) || !tsk->thread.vm86)
287 		return;
288 
289 	bit = (address - 0xA0000) >> PAGE_SHIFT;
290 	if (bit < 32)
291 		tsk->thread.vm86->screen_bitmap |= 1 << bit;
292 #endif
293 }
294 
295 static bool low_pfn(unsigned long pfn)
296 {
297 	return pfn < max_low_pfn;
298 }
299 
300 static void dump_pagetable(unsigned long address)
301 {
302 	pgd_t *base = __va(read_cr3_pa());
303 	pgd_t *pgd = &base[pgd_index(address)];
304 	p4d_t *p4d;
305 	pud_t *pud;
306 	pmd_t *pmd;
307 	pte_t *pte;
308 
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))
312 		goto out;
313 #define pr_pde pr_cont
314 #else
315 #define pr_pde pr_info
316 #endif
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));
321 #undef pr_pde
322 
323 	/*
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:
328 	 */
329 	if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
330 		goto out;
331 
332 	pte = pte_offset_kernel(pmd, address);
333 	pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
334 out:
335 	pr_cont("\n");
336 }
337 
338 #else /* CONFIG_X86_64: */
339 
340 void vmalloc_sync_all(void)
341 {
342 	sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END);
343 }
344 
345 /*
346  * 64-bit:
347  *
348  *   Handle a fault on the vmalloc area
349  */
350 static noinline int vmalloc_fault(unsigned long address)
351 {
352 	pgd_t *pgd, *pgd_k;
353 	p4d_t *p4d, *p4d_k;
354 	pud_t *pud;
355 	pmd_t *pmd;
356 	pte_t *pte;
357 
358 	/* Make sure we are in vmalloc area: */
359 	if (!(address >= VMALLOC_START && address < VMALLOC_END))
360 		return -1;
361 
362 	WARN_ON_ONCE(in_nmi());
363 
364 	/*
365 	 * Copy kernel mappings over when needed. This can also
366 	 * happen within a race in page table update. In the later
367 	 * case just flush:
368 	 */
369 	pgd = (pgd_t *)__va(read_cr3_pa()) + pgd_index(address);
370 	pgd_k = pgd_offset_k(address);
371 	if (pgd_none(*pgd_k))
372 		return -1;
373 
374 	if (pgtable_l5_enabled()) {
375 		if (pgd_none(*pgd)) {
376 			set_pgd(pgd, *pgd_k);
377 			arch_flush_lazy_mmu_mode();
378 		} else {
379 			BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_k));
380 		}
381 	}
382 
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))
387 		return -1;
388 
389 	if (p4d_none(*p4d) && !pgtable_l5_enabled()) {
390 		set_p4d(p4d, *p4d_k);
391 		arch_flush_lazy_mmu_mode();
392 	} else {
393 		BUG_ON(p4d_pfn(*p4d) != p4d_pfn(*p4d_k));
394 	}
395 
396 	BUILD_BUG_ON(CONFIG_PGTABLE_LEVELS < 4);
397 
398 	pud = pud_offset(p4d, address);
399 	if (pud_none(*pud))
400 		return -1;
401 
402 	if (pud_large(*pud))
403 		return 0;
404 
405 	pmd = pmd_offset(pud, address);
406 	if (pmd_none(*pmd))
407 		return -1;
408 
409 	if (pmd_large(*pmd))
410 		return 0;
411 
412 	pte = pte_offset_kernel(pmd, address);
413 	if (!pte_present(*pte))
414 		return -1;
415 
416 	return 0;
417 }
418 NOKPROBE_SYMBOL(vmalloc_fault);
419 
420 #ifdef CONFIG_CPU_SUP_AMD
421 static const char errata93_warning[] =
422 KERN_ERR
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";
427 #endif
428 
429 /*
430  * No vm86 mode in 64-bit mode:
431  */
432 static inline void
433 check_v8086_mode(struct pt_regs *regs, unsigned long address,
434 		 struct task_struct *tsk)
435 {
436 }
437 
438 static int bad_address(void *p)
439 {
440 	unsigned long dummy;
441 
442 	return probe_kernel_address((unsigned long *)p, dummy);
443 }
444 
445 static void dump_pagetable(unsigned long address)
446 {
447 	pgd_t *base = __va(read_cr3_pa());
448 	pgd_t *pgd = base + pgd_index(address);
449 	p4d_t *p4d;
450 	pud_t *pud;
451 	pmd_t *pmd;
452 	pte_t *pte;
453 
454 	if (bad_address(pgd))
455 		goto bad;
456 
457 	pr_info("PGD %lx ", pgd_val(*pgd));
458 
459 	if (!pgd_present(*pgd))
460 		goto out;
461 
462 	p4d = p4d_offset(pgd, address);
463 	if (bad_address(p4d))
464 		goto bad;
465 
466 	pr_cont("P4D %lx ", p4d_val(*p4d));
467 	if (!p4d_present(*p4d) || p4d_large(*p4d))
468 		goto out;
469 
470 	pud = pud_offset(p4d, address);
471 	if (bad_address(pud))
472 		goto bad;
473 
474 	pr_cont("PUD %lx ", pud_val(*pud));
475 	if (!pud_present(*pud) || pud_large(*pud))
476 		goto out;
477 
478 	pmd = pmd_offset(pud, address);
479 	if (bad_address(pmd))
480 		goto bad;
481 
482 	pr_cont("PMD %lx ", pmd_val(*pmd));
483 	if (!pmd_present(*pmd) || pmd_large(*pmd))
484 		goto out;
485 
486 	pte = pte_offset_kernel(pmd, address);
487 	if (bad_address(pte))
488 		goto bad;
489 
490 	pr_cont("PTE %lx", pte_val(*pte));
491 out:
492 	pr_cont("\n");
493 	return;
494 bad:
495 	pr_info("BAD\n");
496 }
497 
498 #endif /* CONFIG_X86_64 */
499 
500 /*
501  * Workaround for K8 erratum #93 & buggy BIOS.
502  *
503  * BIOS SMM functions are required to use a specific workaround
504  * to avoid corruption of the 64bit RIP register on C stepping K8.
505  *
506  * A lot of BIOS that didn't get tested properly miss this.
507  *
508  * The OS sees this as a page fault with the upper 32bits of RIP cleared.
509  * Try to work around it here.
510  *
511  * Note we only handle faults in kernel here.
512  * Does nothing on 32-bit.
513  */
514 static int is_errata93(struct pt_regs *regs, unsigned long address)
515 {
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)
519 		return 0;
520 
521 	if (address != regs->ip)
522 		return 0;
523 
524 	if ((address >> 32) != 0)
525 		return 0;
526 
527 	address |= 0xffffffffUL << 32;
528 	if ((address >= (u64)_stext && address <= (u64)_etext) ||
529 	    (address >= MODULES_VADDR && address <= MODULES_END)) {
530 		printk_once(errata93_warning);
531 		regs->ip = address;
532 		return 1;
533 	}
534 #endif
535 	return 0;
536 }
537 
538 /*
539  * Work around K8 erratum #100 K8 in compat mode occasionally jumps
540  * to illegal addresses >4GB.
541  *
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.
545  */
546 static int is_errata100(struct pt_regs *regs, unsigned long address)
547 {
548 #ifdef CONFIG_X86_64
549 	if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
550 		return 1;
551 #endif
552 	return 0;
553 }
554 
555 static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
556 {
557 #ifdef CONFIG_X86_F00F_BUG
558 	unsigned long nr;
559 
560 	/*
561 	 * Pentium F0 0F C7 C8 bug workaround:
562 	 */
563 	if (boot_cpu_has_bug(X86_BUG_F00F)) {
564 		nr = (address - idt_descr.address) >> 3;
565 
566 		if (nr == 6) {
567 			do_invalid_op(regs, 0);
568 			return 1;
569 		}
570 	}
571 #endif
572 	return 0;
573 }
574 
575 static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index)
576 {
577 	u32 offset = (index >> 3) * sizeof(struct desc_struct);
578 	unsigned long addr;
579 	struct ldttss_desc desc;
580 
581 	if (index == 0) {
582 		pr_alert("%s: NULL\n", name);
583 		return;
584 	}
585 
586 	if (offset + sizeof(struct ldttss_desc) >= gdt->size) {
587 		pr_alert("%s: 0x%hx -- out of bounds\n", name, index);
588 		return;
589 	}
590 
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",
594 			 name, index);
595 		return;
596 	}
597 
598 	addr = desc.base0 | (desc.base1 << 16) | ((unsigned long)desc.base2 << 24);
599 #ifdef CONFIG_X86_64
600 	addr |= ((u64)desc.base3 << 32);
601 #endif
602 	pr_alert("%s: 0x%hx -- base=0x%lx limit=0x%x\n",
603 		 name, index, addr, (desc.limit0 | (desc.limit1 << 16)));
604 }
605 
606 /*
607  * This helper function transforms the #PF error_code bits into
608  * "[PROT] [USER]" type of descriptive, almost human-readable error strings:
609  */
610 static void err_str_append(unsigned long error_code, char *buf, unsigned long mask, const char *txt)
611 {
612 	if (error_code & mask) {
613 		if (buf[0])
614 			strcat(buf, " ");
615 		strcat(buf, txt);
616 	}
617 }
618 
619 static void
620 show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long address)
621 {
622 	char err_txt[64];
623 
624 	if (!oops_may_print())
625 		return;
626 
627 	if (error_code & X86_PF_INSTR) {
628 		unsigned int level;
629 		pgd_t *pgd;
630 		pte_t *pte;
631 
632 		pgd = __va(read_cr3_pa());
633 		pgd += pgd_index(address);
634 
635 		pte = lookup_address_in_pgd(pgd, address, &level);
636 
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()));
645 	}
646 
647 	pr_alert("BUG: unable to handle kernel %s at %px\n",
648 		 address < PAGE_SIZE ? "NULL pointer dereference" : "paging request",
649 		 (void *)address);
650 
651 	err_txt[0] = 0;
652 
653 	/*
654 	 * Note: length of these appended strings including the separation space and the
655 	 * zero delimiter must fit into err_txt[].
656 	 */
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]"   );
663 
664 	pr_alert("#PF error: %s\n", error_code ? err_txt : "[normal kernel read fault]");
665 
666 	if (!(error_code & X86_PF_USER) && user_mode(regs)) {
667 		struct desc_ptr idt, gdt;
668 		u16 ldtr, tr;
669 
670 		pr_alert("This was a system access from user code\n");
671 
672 		/*
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.
681 		 */
682 		store_idt(&idt);
683 
684 		/* Usable even on Xen PV -- it's just slow. */
685 		native_store_gdt(&gdt);
686 
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);
689 
690 		store_ldt(ldtr);
691 		show_ldttss(&gdt, "LDTR", ldtr);
692 
693 		store_tr(tr);
694 		show_ldttss(&gdt, "TR", tr);
695 	}
696 
697 	dump_pagetable(address);
698 }
699 
700 static noinline void
701 pgtable_bad(struct pt_regs *regs, unsigned long error_code,
702 	    unsigned long address)
703 {
704 	struct task_struct *tsk;
705 	unsigned long flags;
706 	int sig;
707 
708 	flags = oops_begin();
709 	tsk = current;
710 	sig = SIGKILL;
711 
712 	printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
713 	       tsk->comm, address);
714 	dump_pagetable(address);
715 
716 	if (__die("Bad pagetable", regs, error_code))
717 		sig = 0;
718 
719 	oops_end(flags, regs, sig);
720 }
721 
722 static void set_signal_archinfo(unsigned long address,
723 				unsigned long error_code)
724 {
725 	struct task_struct *tsk = current;
726 
727 	/*
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.
731 	 */
732 	if (address >= TASK_SIZE_MAX)
733 		error_code |= X86_PF_PROT;
734 
735 	tsk->thread.trap_nr = X86_TRAP_PF;
736 	tsk->thread.error_code = error_code | X86_PF_USER;
737 	tsk->thread.cr2 = address;
738 }
739 
740 static noinline void
741 no_context(struct pt_regs *regs, unsigned long error_code,
742 	   unsigned long address, int signal, int si_code)
743 {
744 	struct task_struct *tsk = current;
745 	unsigned long flags;
746 	int sig;
747 
748 	if (user_mode(regs)) {
749 		/*
750 		 * This is an implicit supervisor-mode access from user
751 		 * mode.  Bypass all the kernel-mode recovery code and just
752 		 * OOPS.
753 		 */
754 		goto oops;
755 	}
756 
757 	/* Are we prepared to handle this kernel fault? */
758 	if (fixup_exception(regs, X86_TRAP_PF, error_code, address)) {
759 		/*
760 		 * Any interrupt that takes a fault gets the fixup. This makes
761 		 * the below recursive fault logic only apply to a faults from
762 		 * task context.
763 		 */
764 		if (in_interrupt())
765 			return;
766 
767 		/*
768 		 * Per the above we're !in_interrupt(), aka. task context.
769 		 *
770 		 * In this case we need to make sure we're not recursively
771 		 * faulting through the emulate_vsyscall() logic.
772 		 */
773 		if (current->thread.sig_on_uaccess_err && signal) {
774 			set_signal_archinfo(address, error_code);
775 
776 			/* XXX: hwpoison faults will set the wrong code. */
777 			force_sig_fault(signal, si_code, (void __user *)address,
778 					tsk);
779 		}
780 
781 		/*
782 		 * Barring that, we can do the fixup and be happy.
783 		 */
784 		return;
785 	}
786 
787 #ifdef CONFIG_VMAP_STACK
788 	/*
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.
792 	 */
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 *);
797 		/*
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.
802 		 *
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.
806 		 */
807 		asm volatile ("movq %[stack], %%rsp\n\t"
808 			      "call handle_stack_overflow\n\t"
809 			      "1: jmp 1b"
810 			      : ASM_CALL_CONSTRAINT
811 			      : "D" ("kernel stack overflow (page fault)"),
812 				"S" (regs), "d" (address),
813 				[stack] "rm" (stack));
814 		unreachable();
815 	}
816 #endif
817 
818 	/*
819 	 * 32-bit:
820 	 *
821 	 *   Valid to do another page fault here, because if this fault
822 	 *   had been triggered by is_prefetch fixup_exception would have
823 	 *   handled it.
824 	 *
825 	 * 64-bit:
826 	 *
827 	 *   Hall of shame of CPU/BIOS bugs.
828 	 */
829 	if (is_prefetch(regs, error_code, address))
830 		return;
831 
832 	if (is_errata93(regs, address))
833 		return;
834 
835 	/*
836 	 * Buggy firmware could access regions which might page fault, try to
837 	 * recover from such faults.
838 	 */
839 	if (IS_ENABLED(CONFIG_EFI))
840 		efi_recover_from_page_fault(address);
841 
842 oops:
843 	/*
844 	 * Oops. The kernel tried to access some bad page. We'll have to
845 	 * terminate things with extreme prejudice:
846 	 */
847 	flags = oops_begin();
848 
849 	show_fault_oops(regs, error_code, address);
850 
851 	if (task_stack_end_corrupted(tsk))
852 		printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
853 
854 	sig = SIGKILL;
855 	if (__die("Oops", regs, error_code))
856 		sig = 0;
857 
858 	/* Executive summary in case the body of the oops scrolled away */
859 	printk(KERN_DEFAULT "CR2: %016lx\n", address);
860 
861 	oops_end(flags, regs, sig);
862 }
863 
864 /*
865  * Print out info about fatal segfaults, if the show_unhandled_signals
866  * sysctl is set:
867  */
868 static inline void
869 show_signal_msg(struct pt_regs *regs, unsigned long error_code,
870 		unsigned long address, struct task_struct *tsk)
871 {
872 	const char *loglvl = task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG;
873 
874 	if (!unhandled_signal(tsk, SIGSEGV))
875 		return;
876 
877 	if (!printk_ratelimit())
878 		return;
879 
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);
883 
884 	print_vma_addr(KERN_CONT " in ", regs->ip);
885 
886 	printk(KERN_CONT "\n");
887 
888 	show_opcodes(regs, loglvl);
889 }
890 
891 /*
892  * The (legacy) vsyscall page is the long page in the kernel portion
893  * of the address space that has user-accessible permissions.
894  */
895 static bool is_vsyscall_vaddr(unsigned long vaddr)
896 {
897 	return unlikely((vaddr & PAGE_MASK) == VSYSCALL_ADDR);
898 }
899 
900 static void
901 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
902 		       unsigned long address, u32 pkey, int si_code)
903 {
904 	struct task_struct *tsk = current;
905 
906 	/* User mode accesses just cause a SIGSEGV */
907 	if (user_mode(regs) && (error_code & X86_PF_USER)) {
908 		/*
909 		 * It's possible to have interrupts off here:
910 		 */
911 		local_irq_enable();
912 
913 		/*
914 		 * Valid to do another page fault here because this one came
915 		 * from user space:
916 		 */
917 		if (is_prefetch(regs, error_code, address))
918 			return;
919 
920 		if (is_errata100(regs, address))
921 			return;
922 
923 		/*
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.
927 		 */
928 		if (address >= TASK_SIZE_MAX)
929 			error_code |= X86_PF_PROT;
930 
931 		if (likely(show_unhandled_signals))
932 			show_signal_msg(regs, error_code, address, tsk);
933 
934 		set_signal_archinfo(address, error_code);
935 
936 		if (si_code == SEGV_PKUERR)
937 			force_sig_pkuerr((void __user *)address, pkey);
938 
939 		force_sig_fault(SIGSEGV, si_code, (void __user *)address, tsk);
940 
941 		return;
942 	}
943 
944 	if (is_f00f_bug(regs, address))
945 		return;
946 
947 	no_context(regs, error_code, address, SIGSEGV, si_code);
948 }
949 
950 static noinline void
951 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
952 		     unsigned long address)
953 {
954 	__bad_area_nosemaphore(regs, error_code, address, 0, SEGV_MAPERR);
955 }
956 
957 static void
958 __bad_area(struct pt_regs *regs, unsigned long error_code,
959 	   unsigned long address, u32 pkey, int si_code)
960 {
961 	struct mm_struct *mm = current->mm;
962 	/*
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..
965 	 */
966 	up_read(&mm->mmap_sem);
967 
968 	__bad_area_nosemaphore(regs, error_code, address, pkey, si_code);
969 }
970 
971 static noinline void
972 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
973 {
974 	__bad_area(regs, error_code, address, 0, SEGV_MAPERR);
975 }
976 
977 static inline bool bad_area_access_from_pkeys(unsigned long error_code,
978 		struct vm_area_struct *vma)
979 {
980 	/* This code is always called on the current mm */
981 	bool foreign = false;
982 
983 	if (!boot_cpu_has(X86_FEATURE_OSPKE))
984 		return false;
985 	if (error_code & X86_PF_PK)
986 		return true;
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))
990 		return true;
991 	return false;
992 }
993 
994 static noinline void
995 bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
996 		      unsigned long address, struct vm_area_struct *vma)
997 {
998 	/*
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.
1002 	 */
1003 	if (bad_area_access_from_pkeys(error_code, vma)) {
1004 		/*
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.
1010 		 *
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.
1015 		 *
1016 		 * 1. T1   : mprotect_key(foo, PAGE_SIZE, pkey=4);
1017 		 * 2. T1   : set PKRU to deny access to pkey=4, touches page
1018 		 * 3. T1   : faults...
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.
1023 		 */
1024 		u32 pkey = vma_pkey(vma);
1025 
1026 		__bad_area(regs, error_code, address, pkey, SEGV_PKUERR);
1027 	} else {
1028 		__bad_area(regs, error_code, address, 0, SEGV_ACCERR);
1029 	}
1030 }
1031 
1032 static void
1033 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
1034 	  vm_fault_t fault)
1035 {
1036 	struct task_struct *tsk = current;
1037 
1038 	/* Kernel mode? Handle exceptions or die: */
1039 	if (!(error_code & X86_PF_USER)) {
1040 		no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
1041 		return;
1042 	}
1043 
1044 	/* User-space => ok to do another page fault: */
1045 	if (is_prefetch(regs, error_code, address))
1046 		return;
1047 
1048 	set_signal_archinfo(address, error_code);
1049 
1050 #ifdef CONFIG_MEMORY_FAILURE
1051 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
1052 		unsigned lsb = 0;
1053 
1054 		pr_err(
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)
1060 			lsb = PAGE_SHIFT;
1061 		force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb, tsk);
1062 		return;
1063 	}
1064 #endif
1065 	force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address, tsk);
1066 }
1067 
1068 static noinline void
1069 mm_fault_error(struct pt_regs *regs, unsigned long error_code,
1070 	       unsigned long address, vm_fault_t fault)
1071 {
1072 	if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) {
1073 		no_context(regs, error_code, address, 0, 0);
1074 		return;
1075 	}
1076 
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);
1082 			return;
1083 		}
1084 
1085 		/*
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
1088 		 * oom-killed):
1089 		 */
1090 		pagefault_out_of_memory();
1091 	} else {
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);
1097 		else
1098 			BUG();
1099 	}
1100 }
1101 
1102 static int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte)
1103 {
1104 	if ((error_code & X86_PF_WRITE) && !pte_write(*pte))
1105 		return 0;
1106 
1107 	if ((error_code & X86_PF_INSTR) && !pte_exec(*pte))
1108 		return 0;
1109 
1110 	return 1;
1111 }
1112 
1113 /*
1114  * Handle a spurious fault caused by a stale TLB entry.
1115  *
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.
1121  *
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.
1125  *
1126  * There are no security implications to leaving a stale TLB when
1127  * increasing the permissions on a page.
1128  *
1129  * Returns non-zero if a spurious fault was handled, zero otherwise.
1130  *
1131  * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
1132  * (Optional Invalidation).
1133  */
1134 static noinline int
1135 spurious_kernel_fault(unsigned long error_code, unsigned long address)
1136 {
1137 	pgd_t *pgd;
1138 	p4d_t *p4d;
1139 	pud_t *pud;
1140 	pmd_t *pmd;
1141 	pte_t *pte;
1142 	int ret;
1143 
1144 	/*
1145 	 * Only writes to RO or instruction fetches from NX may cause
1146 	 * spurious faults.
1147 	 *
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
1151 	 * faults.
1152 	 */
1153 	if (error_code != (X86_PF_WRITE | X86_PF_PROT) &&
1154 	    error_code != (X86_PF_INSTR | X86_PF_PROT))
1155 		return 0;
1156 
1157 	pgd = init_mm.pgd + pgd_index(address);
1158 	if (!pgd_present(*pgd))
1159 		return 0;
1160 
1161 	p4d = p4d_offset(pgd, address);
1162 	if (!p4d_present(*p4d))
1163 		return 0;
1164 
1165 	if (p4d_large(*p4d))
1166 		return spurious_kernel_fault_check(error_code, (pte_t *) p4d);
1167 
1168 	pud = pud_offset(p4d, address);
1169 	if (!pud_present(*pud))
1170 		return 0;
1171 
1172 	if (pud_large(*pud))
1173 		return spurious_kernel_fault_check(error_code, (pte_t *) pud);
1174 
1175 	pmd = pmd_offset(pud, address);
1176 	if (!pmd_present(*pmd))
1177 		return 0;
1178 
1179 	if (pmd_large(*pmd))
1180 		return spurious_kernel_fault_check(error_code, (pte_t *) pmd);
1181 
1182 	pte = pte_offset_kernel(pmd, address);
1183 	if (!pte_present(*pte))
1184 		return 0;
1185 
1186 	ret = spurious_kernel_fault_check(error_code, pte);
1187 	if (!ret)
1188 		return 0;
1189 
1190 	/*
1191 	 * Make sure we have permissions in PMD.
1192 	 * If not, then there's a bug in the page tables:
1193 	 */
1194 	ret = spurious_kernel_fault_check(error_code, (pte_t *) pmd);
1195 	WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
1196 
1197 	return ret;
1198 }
1199 NOKPROBE_SYMBOL(spurious_kernel_fault);
1200 
1201 int show_unhandled_signals = 1;
1202 
1203 static inline int
1204 access_error(unsigned long error_code, struct vm_area_struct *vma)
1205 {
1206 	/* This is only called for the current mm, so: */
1207 	bool foreign = false;
1208 
1209 	/*
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.
1213 	 */
1214 	if (error_code & X86_PF_PK)
1215 		return 1;
1216 
1217 	/*
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
1220 	 * page.
1221 	 */
1222 	if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
1223 				       (error_code & X86_PF_INSTR), foreign))
1224 		return 1;
1225 
1226 	if (error_code & X86_PF_WRITE) {
1227 		/* write, present and write, not present: */
1228 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
1229 			return 1;
1230 		return 0;
1231 	}
1232 
1233 	/* read, present: */
1234 	if (unlikely(error_code & X86_PF_PROT))
1235 		return 1;
1236 
1237 	/* read, not present: */
1238 	if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
1239 		return 1;
1240 
1241 	return 0;
1242 }
1243 
1244 static int fault_in_kernel_space(unsigned long address)
1245 {
1246 	/*
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
1249 	 * address space.
1250 	 */
1251 	if (IS_ENABLED(CONFIG_X86_64) && is_vsyscall_vaddr(address))
1252 		return false;
1253 
1254 	return address >= TASK_SIZE_MAX;
1255 }
1256 
1257 /*
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.
1261  */
1262 static void
1263 do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code,
1264 		   unsigned long address)
1265 {
1266 	/*
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.
1270 	 */
1271 	WARN_ON_ONCE(hw_error_code & X86_PF_PK);
1272 
1273 	/*
1274 	 * We can fault-in kernel-space virtual memory on-demand. The
1275 	 * 'reference' page table is init_mm.pgd.
1276 	 *
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,
1280 	 * nothing more.
1281 	 *
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).
1290 	 */
1291 	if (!(hw_error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) {
1292 		if (vmalloc_fault(address) >= 0)
1293 			return;
1294 	}
1295 
1296 	/* Was the fault spurious, caused by lazy TLB invalidation? */
1297 	if (spurious_kernel_fault(hw_error_code, address))
1298 		return;
1299 
1300 	/* kprobes don't want to hook the spurious faults: */
1301 	if (kprobes_fault(regs))
1302 		return;
1303 
1304 	/*
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().
1308 	 *
1309 	 * Don't take the mm semaphore here. If we fixup a prefetch
1310 	 * fault we could otherwise deadlock:
1311 	 */
1312 	bad_area_nosemaphore(regs, hw_error_code, address);
1313 }
1314 NOKPROBE_SYMBOL(do_kern_addr_fault);
1315 
1316 /* Handle faults in the user portion of the address space */
1317 static inline
1318 void do_user_addr_fault(struct pt_regs *regs,
1319 			unsigned long hw_error_code,
1320 			unsigned long address)
1321 {
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;
1327 
1328 	tsk = current;
1329 	mm = tsk->mm;
1330 
1331 	/* kprobes don't want to hook the spurious faults: */
1332 	if (unlikely(kprobes_fault(regs)))
1333 		return;
1334 
1335 	/*
1336 	 * Reserved bits are never expected to be set on
1337 	 * entries in the user portion of the page tables.
1338 	 */
1339 	if (unlikely(hw_error_code & X86_PF_RSVD))
1340 		pgtable_bad(regs, hw_error_code, address);
1341 
1342 	/*
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.
1348 	 */
1349 	if (unlikely(cpu_feature_enabled(X86_FEATURE_SMAP) &&
1350 		     !(hw_error_code & X86_PF_USER) &&
1351 		     !(regs->flags & X86_EFLAGS_AC)))
1352 	{
1353 		bad_area_nosemaphore(regs, hw_error_code, address);
1354 		return;
1355 	}
1356 
1357 	/*
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
1360 	 */
1361 	if (unlikely(faulthandler_disabled() || !mm)) {
1362 		bad_area_nosemaphore(regs, hw_error_code, address);
1363 		return;
1364 	}
1365 
1366 	/*
1367 	 * It's safe to allow irq's after cr2 has been saved and the
1368 	 * vmalloc fault has been handled.
1369 	 *
1370 	 * User-mode registers count as a user access even for any
1371 	 * potential system fault or CPU buglet:
1372 	 */
1373 	if (user_mode(regs)) {
1374 		local_irq_enable();
1375 		flags |= FAULT_FLAG_USER;
1376 	} else {
1377 		if (regs->flags & X86_EFLAGS_IF)
1378 			local_irq_enable();
1379 	}
1380 
1381 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
1382 
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;
1387 
1388 #ifdef CONFIG_X86_64
1389 	/*
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
1393 	 * address space.
1394 	 *
1395 	 * The vsyscall page does not have a "real" VMA, so do this
1396 	 * emulation before we go searching for VMAs.
1397 	 */
1398 	if ((hw_error_code & X86_PF_INSTR) && is_vsyscall_vaddr(address)) {
1399 		if (emulate_vsyscall(regs, address))
1400 			return;
1401 	}
1402 #endif
1403 
1404 	/*
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.
1410 	 *
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.
1415 	 */
1416 	if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1417 		if (!user_mode(regs) && !search_exception_tables(regs->ip)) {
1418 			/*
1419 			 * Fault from code in kernel from
1420 			 * which we do not expect faults.
1421 			 */
1422 			bad_area_nosemaphore(regs, hw_error_code, address);
1423 			return;
1424 		}
1425 retry:
1426 		down_read(&mm->mmap_sem);
1427 	} else {
1428 		/*
1429 		 * The above down_read_trylock() might have succeeded in
1430 		 * which case we'll have missed the might_sleep() from
1431 		 * down_read():
1432 		 */
1433 		might_sleep();
1434 	}
1435 
1436 	vma = find_vma(mm, address);
1437 	if (unlikely(!vma)) {
1438 		bad_area(regs, hw_error_code, address);
1439 		return;
1440 	}
1441 	if (likely(vma->vm_start <= address))
1442 		goto good_area;
1443 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1444 		bad_area(regs, hw_error_code, address);
1445 		return;
1446 	}
1447 	if (unlikely(expand_stack(vma, address))) {
1448 		bad_area(regs, hw_error_code, address);
1449 		return;
1450 	}
1451 
1452 	/*
1453 	 * Ok, we have a good vm_area for this memory access, so
1454 	 * we can handle it..
1455 	 */
1456 good_area:
1457 	if (unlikely(access_error(hw_error_code, vma))) {
1458 		bad_area_access_error(regs, hw_error_code, address, vma);
1459 		return;
1460 	}
1461 
1462 	/*
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.
1467 	 *
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.
1474 	 */
1475 	fault = handle_mm_fault(vma, address, flags);
1476 	major |= fault & VM_FAULT_MAJOR;
1477 
1478 	/*
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.
1482 	 */
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))
1489 				goto retry;
1490 		}
1491 
1492 		/* User mode? Just return to handle the fatal exception */
1493 		if (flags & FAULT_FLAG_USER)
1494 			return;
1495 
1496 		/* Not returning to user mode? Handle exceptions or die: */
1497 		no_context(regs, hw_error_code, address, SIGBUS, BUS_ADRERR);
1498 		return;
1499 	}
1500 
1501 	up_read(&mm->mmap_sem);
1502 	if (unlikely(fault & VM_FAULT_ERROR)) {
1503 		mm_fault_error(regs, hw_error_code, address, fault);
1504 		return;
1505 	}
1506 
1507 	/*
1508 	 * Major/minor page fault accounting. If any of the events
1509 	 * returned VM_FAULT_MAJOR, we account it as a major fault.
1510 	 */
1511 	if (major) {
1512 		tsk->maj_flt++;
1513 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
1514 	} else {
1515 		tsk->min_flt++;
1516 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
1517 	}
1518 
1519 	check_v8086_mode(regs, address, tsk);
1520 }
1521 NOKPROBE_SYMBOL(do_user_addr_fault);
1522 
1523 /*
1524  * This routine handles page faults.  It determines the address,
1525  * and the problem, and then passes it off to one of the appropriate
1526  * routines.
1527  */
1528 static noinline void
1529 __do_page_fault(struct pt_regs *regs, unsigned long hw_error_code,
1530 		unsigned long address)
1531 {
1532 	prefetchw(&current->mm->mmap_sem);
1533 
1534 	if (unlikely(kmmio_fault(regs, address)))
1535 		return;
1536 
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);
1540 	else
1541 		do_user_addr_fault(regs, hw_error_code, address);
1542 }
1543 NOKPROBE_SYMBOL(__do_page_fault);
1544 
1545 static nokprobe_inline void
1546 trace_page_fault_entries(unsigned long address, struct pt_regs *regs,
1547 			 unsigned long error_code)
1548 {
1549 	if (user_mode(regs))
1550 		trace_page_fault_user(address, regs, error_code);
1551 	else
1552 		trace_page_fault_kernel(address, regs, error_code);
1553 }
1554 
1555 /*
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.
1559  *
1560  * exception_{enter,exit}() contains all sorts of tracepoints.
1561  */
1562 dotraplinkage void notrace
1563 do_page_fault(struct pt_regs *regs, unsigned long error_code)
1564 {
1565 	unsigned long address = read_cr2(); /* Get the faulting address */
1566 	enum ctx_state prev_state;
1567 
1568 	prev_state = exception_enter();
1569 	if (trace_pagefault_enabled())
1570 		trace_page_fault_entries(address, regs, error_code);
1571 
1572 	__do_page_fault(regs, error_code, address);
1573 	exception_exit(prev_state);
1574 }
1575 NOKPROBE_SYMBOL(do_page_fault);
1576