xref: /openbmc/linux/arch/x86/mm/fault.c (revision b285d2ae)
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/fixmap.h>			/* VSYSCALL_ADDR		*/
25 #include <asm/vsyscall.h>		/* emulate_vsyscall		*/
26 #include <asm/vm86.h>			/* struct vm86			*/
27 #include <asm/mmu_context.h>		/* vma_pkey()			*/
28 #include <asm/efi.h>			/* efi_recover_from_page_fault()*/
29 #include <asm/desc.h>			/* store_idt(), ...		*/
30 #include <asm/cpu_entry_area.h>		/* exception stack		*/
31 #include <asm/pgtable_areas.h>		/* VMALLOC_START, ...		*/
32 #include <asm/kvm_para.h>		/* kvm_handle_async_pf		*/
33 
34 #define CREATE_TRACE_POINTS
35 #include <asm/trace/exceptions.h>
36 
37 /*
38  * Returns 0 if mmiotrace is disabled, or if the fault is not
39  * handled by mmiotrace:
40  */
41 static nokprobe_inline int
42 kmmio_fault(struct pt_regs *regs, unsigned long addr)
43 {
44 	if (unlikely(is_kmmio_active()))
45 		if (kmmio_handler(regs, addr) == 1)
46 			return -1;
47 	return 0;
48 }
49 
50 /*
51  * Prefetch quirks:
52  *
53  * 32-bit mode:
54  *
55  *   Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
56  *   Check that here and ignore it.
57  *
58  * 64-bit mode:
59  *
60  *   Sometimes the CPU reports invalid exceptions on prefetch.
61  *   Check that here and ignore it.
62  *
63  * Opcode checker based on code by Richard Brunner.
64  */
65 static inline int
66 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
67 		      unsigned char opcode, int *prefetch)
68 {
69 	unsigned char instr_hi = opcode & 0xf0;
70 	unsigned char instr_lo = opcode & 0x0f;
71 
72 	switch (instr_hi) {
73 	case 0x20:
74 	case 0x30:
75 		/*
76 		 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
77 		 * In X86_64 long mode, the CPU will signal invalid
78 		 * opcode if some of these prefixes are present so
79 		 * X86_64 will never get here anyway
80 		 */
81 		return ((instr_lo & 7) == 0x6);
82 #ifdef CONFIG_X86_64
83 	case 0x40:
84 		/*
85 		 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
86 		 * Need to figure out under what instruction mode the
87 		 * instruction was issued. Could check the LDT for lm,
88 		 * but for now it's good enough to assume that long
89 		 * mode only uses well known segments or kernel.
90 		 */
91 		return (!user_mode(regs) || user_64bit_mode(regs));
92 #endif
93 	case 0x60:
94 		/* 0x64 thru 0x67 are valid prefixes in all modes. */
95 		return (instr_lo & 0xC) == 0x4;
96 	case 0xF0:
97 		/* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
98 		return !instr_lo || (instr_lo>>1) == 1;
99 	case 0x00:
100 		/* Prefetch instruction is 0x0F0D or 0x0F18 */
101 		if (get_kernel_nofault(opcode, instr))
102 			return 0;
103 
104 		*prefetch = (instr_lo == 0xF) &&
105 			(opcode == 0x0D || opcode == 0x18);
106 		return 0;
107 	default:
108 		return 0;
109 	}
110 }
111 
112 static int
113 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
114 {
115 	unsigned char *max_instr;
116 	unsigned char *instr;
117 	int prefetch = 0;
118 
119 	/*
120 	 * If it was a exec (instruction fetch) fault on NX page, then
121 	 * do not ignore the fault:
122 	 */
123 	if (error_code & X86_PF_INSTR)
124 		return 0;
125 
126 	instr = (void *)convert_ip_to_linear(current, regs);
127 	max_instr = instr + 15;
128 
129 	if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE_MAX)
130 		return 0;
131 
132 	while (instr < max_instr) {
133 		unsigned char opcode;
134 
135 		if (get_kernel_nofault(opcode, instr))
136 			break;
137 
138 		instr++;
139 
140 		if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
141 			break;
142 	}
143 	return prefetch;
144 }
145 
146 DEFINE_SPINLOCK(pgd_lock);
147 LIST_HEAD(pgd_list);
148 
149 #ifdef CONFIG_X86_32
150 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
151 {
152 	unsigned index = pgd_index(address);
153 	pgd_t *pgd_k;
154 	p4d_t *p4d, *p4d_k;
155 	pud_t *pud, *pud_k;
156 	pmd_t *pmd, *pmd_k;
157 
158 	pgd += index;
159 	pgd_k = init_mm.pgd + index;
160 
161 	if (!pgd_present(*pgd_k))
162 		return NULL;
163 
164 	/*
165 	 * set_pgd(pgd, *pgd_k); here would be useless on PAE
166 	 * and redundant with the set_pmd() on non-PAE. As would
167 	 * set_p4d/set_pud.
168 	 */
169 	p4d = p4d_offset(pgd, address);
170 	p4d_k = p4d_offset(pgd_k, address);
171 	if (!p4d_present(*p4d_k))
172 		return NULL;
173 
174 	pud = pud_offset(p4d, address);
175 	pud_k = pud_offset(p4d_k, address);
176 	if (!pud_present(*pud_k))
177 		return NULL;
178 
179 	pmd = pmd_offset(pud, address);
180 	pmd_k = pmd_offset(pud_k, address);
181 
182 	if (pmd_present(*pmd) != pmd_present(*pmd_k))
183 		set_pmd(pmd, *pmd_k);
184 
185 	if (!pmd_present(*pmd_k))
186 		return NULL;
187 	else
188 		BUG_ON(pmd_pfn(*pmd) != pmd_pfn(*pmd_k));
189 
190 	return pmd_k;
191 }
192 
193 void arch_sync_kernel_mappings(unsigned long start, unsigned long end)
194 {
195 	unsigned long addr;
196 
197 	for (addr = start & PMD_MASK;
198 	     addr >= TASK_SIZE_MAX && addr < VMALLOC_END;
199 	     addr += PMD_SIZE) {
200 		struct page *page;
201 
202 		spin_lock(&pgd_lock);
203 		list_for_each_entry(page, &pgd_list, lru) {
204 			spinlock_t *pgt_lock;
205 
206 			/* the pgt_lock only for Xen */
207 			pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
208 
209 			spin_lock(pgt_lock);
210 			vmalloc_sync_one(page_address(page), addr);
211 			spin_unlock(pgt_lock);
212 		}
213 		spin_unlock(&pgd_lock);
214 	}
215 }
216 
217 /*
218  * Did it hit the DOS screen memory VA from vm86 mode?
219  */
220 static inline void
221 check_v8086_mode(struct pt_regs *regs, unsigned long address,
222 		 struct task_struct *tsk)
223 {
224 #ifdef CONFIG_VM86
225 	unsigned long bit;
226 
227 	if (!v8086_mode(regs) || !tsk->thread.vm86)
228 		return;
229 
230 	bit = (address - 0xA0000) >> PAGE_SHIFT;
231 	if (bit < 32)
232 		tsk->thread.vm86->screen_bitmap |= 1 << bit;
233 #endif
234 }
235 
236 static bool low_pfn(unsigned long pfn)
237 {
238 	return pfn < max_low_pfn;
239 }
240 
241 static void dump_pagetable(unsigned long address)
242 {
243 	pgd_t *base = __va(read_cr3_pa());
244 	pgd_t *pgd = &base[pgd_index(address)];
245 	p4d_t *p4d;
246 	pud_t *pud;
247 	pmd_t *pmd;
248 	pte_t *pte;
249 
250 #ifdef CONFIG_X86_PAE
251 	pr_info("*pdpt = %016Lx ", pgd_val(*pgd));
252 	if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
253 		goto out;
254 #define pr_pde pr_cont
255 #else
256 #define pr_pde pr_info
257 #endif
258 	p4d = p4d_offset(pgd, address);
259 	pud = pud_offset(p4d, address);
260 	pmd = pmd_offset(pud, address);
261 	pr_pde("*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
262 #undef pr_pde
263 
264 	/*
265 	 * We must not directly access the pte in the highpte
266 	 * case if the page table is located in highmem.
267 	 * And let's rather not kmap-atomic the pte, just in case
268 	 * it's allocated already:
269 	 */
270 	if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
271 		goto out;
272 
273 	pte = pte_offset_kernel(pmd, address);
274 	pr_cont("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
275 out:
276 	pr_cont("\n");
277 }
278 
279 #else /* CONFIG_X86_64: */
280 
281 #ifdef CONFIG_CPU_SUP_AMD
282 static const char errata93_warning[] =
283 KERN_ERR
284 "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
285 "******* Working around it, but it may cause SEGVs or burn power.\n"
286 "******* Please consider a BIOS update.\n"
287 "******* Disabling USB legacy in the BIOS may also help.\n";
288 #endif
289 
290 /*
291  * No vm86 mode in 64-bit mode:
292  */
293 static inline void
294 check_v8086_mode(struct pt_regs *regs, unsigned long address,
295 		 struct task_struct *tsk)
296 {
297 }
298 
299 static int bad_address(void *p)
300 {
301 	unsigned long dummy;
302 
303 	return get_kernel_nofault(dummy, (unsigned long *)p);
304 }
305 
306 static void dump_pagetable(unsigned long address)
307 {
308 	pgd_t *base = __va(read_cr3_pa());
309 	pgd_t *pgd = base + pgd_index(address);
310 	p4d_t *p4d;
311 	pud_t *pud;
312 	pmd_t *pmd;
313 	pte_t *pte;
314 
315 	if (bad_address(pgd))
316 		goto bad;
317 
318 	pr_info("PGD %lx ", pgd_val(*pgd));
319 
320 	if (!pgd_present(*pgd))
321 		goto out;
322 
323 	p4d = p4d_offset(pgd, address);
324 	if (bad_address(p4d))
325 		goto bad;
326 
327 	pr_cont("P4D %lx ", p4d_val(*p4d));
328 	if (!p4d_present(*p4d) || p4d_large(*p4d))
329 		goto out;
330 
331 	pud = pud_offset(p4d, address);
332 	if (bad_address(pud))
333 		goto bad;
334 
335 	pr_cont("PUD %lx ", pud_val(*pud));
336 	if (!pud_present(*pud) || pud_large(*pud))
337 		goto out;
338 
339 	pmd = pmd_offset(pud, address);
340 	if (bad_address(pmd))
341 		goto bad;
342 
343 	pr_cont("PMD %lx ", pmd_val(*pmd));
344 	if (!pmd_present(*pmd) || pmd_large(*pmd))
345 		goto out;
346 
347 	pte = pte_offset_kernel(pmd, address);
348 	if (bad_address(pte))
349 		goto bad;
350 
351 	pr_cont("PTE %lx", pte_val(*pte));
352 out:
353 	pr_cont("\n");
354 	return;
355 bad:
356 	pr_info("BAD\n");
357 }
358 
359 #endif /* CONFIG_X86_64 */
360 
361 /*
362  * Workaround for K8 erratum #93 & buggy BIOS.
363  *
364  * BIOS SMM functions are required to use a specific workaround
365  * to avoid corruption of the 64bit RIP register on C stepping K8.
366  *
367  * A lot of BIOS that didn't get tested properly miss this.
368  *
369  * The OS sees this as a page fault with the upper 32bits of RIP cleared.
370  * Try to work around it here.
371  *
372  * Note we only handle faults in kernel here.
373  * Does nothing on 32-bit.
374  */
375 static int is_errata93(struct pt_regs *regs, unsigned long address)
376 {
377 #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
378 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
379 	    || boot_cpu_data.x86 != 0xf)
380 		return 0;
381 
382 	if (address != regs->ip)
383 		return 0;
384 
385 	if ((address >> 32) != 0)
386 		return 0;
387 
388 	address |= 0xffffffffUL << 32;
389 	if ((address >= (u64)_stext && address <= (u64)_etext) ||
390 	    (address >= MODULES_VADDR && address <= MODULES_END)) {
391 		printk_once(errata93_warning);
392 		regs->ip = address;
393 		return 1;
394 	}
395 #endif
396 	return 0;
397 }
398 
399 /*
400  * Work around K8 erratum #100 K8 in compat mode occasionally jumps
401  * to illegal addresses >4GB.
402  *
403  * We catch this in the page fault handler because these addresses
404  * are not reachable. Just detect this case and return.  Any code
405  * segment in LDT is compatibility mode.
406  */
407 static int is_errata100(struct pt_regs *regs, unsigned long address)
408 {
409 #ifdef CONFIG_X86_64
410 	if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
411 		return 1;
412 #endif
413 	return 0;
414 }
415 
416 /* Pentium F0 0F C7 C8 bug workaround: */
417 static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
418 {
419 #ifdef CONFIG_X86_F00F_BUG
420 	if (boot_cpu_has_bug(X86_BUG_F00F) && idt_is_f00f_address(address)) {
421 		handle_invalid_op(regs);
422 		return 1;
423 	}
424 #endif
425 	return 0;
426 }
427 
428 static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index)
429 {
430 	u32 offset = (index >> 3) * sizeof(struct desc_struct);
431 	unsigned long addr;
432 	struct ldttss_desc desc;
433 
434 	if (index == 0) {
435 		pr_alert("%s: NULL\n", name);
436 		return;
437 	}
438 
439 	if (offset + sizeof(struct ldttss_desc) >= gdt->size) {
440 		pr_alert("%s: 0x%hx -- out of bounds\n", name, index);
441 		return;
442 	}
443 
444 	if (copy_from_kernel_nofault(&desc, (void *)(gdt->address + offset),
445 			      sizeof(struct ldttss_desc))) {
446 		pr_alert("%s: 0x%hx -- GDT entry is not readable\n",
447 			 name, index);
448 		return;
449 	}
450 
451 	addr = desc.base0 | (desc.base1 << 16) | ((unsigned long)desc.base2 << 24);
452 #ifdef CONFIG_X86_64
453 	addr |= ((u64)desc.base3 << 32);
454 #endif
455 	pr_alert("%s: 0x%hx -- base=0x%lx limit=0x%x\n",
456 		 name, index, addr, (desc.limit0 | (desc.limit1 << 16)));
457 }
458 
459 static void
460 show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long address)
461 {
462 	if (!oops_may_print())
463 		return;
464 
465 	if (error_code & X86_PF_INSTR) {
466 		unsigned int level;
467 		pgd_t *pgd;
468 		pte_t *pte;
469 
470 		pgd = __va(read_cr3_pa());
471 		pgd += pgd_index(address);
472 
473 		pte = lookup_address_in_pgd(pgd, address, &level);
474 
475 		if (pte && pte_present(*pte) && !pte_exec(*pte))
476 			pr_crit("kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n",
477 				from_kuid(&init_user_ns, current_uid()));
478 		if (pte && pte_present(*pte) && pte_exec(*pte) &&
479 				(pgd_flags(*pgd) & _PAGE_USER) &&
480 				(__read_cr4() & X86_CR4_SMEP))
481 			pr_crit("unable to execute userspace code (SMEP?) (uid: %d)\n",
482 				from_kuid(&init_user_ns, current_uid()));
483 	}
484 
485 	if (address < PAGE_SIZE && !user_mode(regs))
486 		pr_alert("BUG: kernel NULL pointer dereference, address: %px\n",
487 			(void *)address);
488 	else
489 		pr_alert("BUG: unable to handle page fault for address: %px\n",
490 			(void *)address);
491 
492 	pr_alert("#PF: %s %s in %s mode\n",
493 		 (error_code & X86_PF_USER)  ? "user" : "supervisor",
494 		 (error_code & X86_PF_INSTR) ? "instruction fetch" :
495 		 (error_code & X86_PF_WRITE) ? "write access" :
496 					       "read access",
497 			     user_mode(regs) ? "user" : "kernel");
498 	pr_alert("#PF: error_code(0x%04lx) - %s\n", error_code,
499 		 !(error_code & X86_PF_PROT) ? "not-present page" :
500 		 (error_code & X86_PF_RSVD)  ? "reserved bit violation" :
501 		 (error_code & X86_PF_PK)    ? "protection keys violation" :
502 					       "permissions violation");
503 
504 	if (!(error_code & X86_PF_USER) && user_mode(regs)) {
505 		struct desc_ptr idt, gdt;
506 		u16 ldtr, tr;
507 
508 		/*
509 		 * This can happen for quite a few reasons.  The more obvious
510 		 * ones are faults accessing the GDT, or LDT.  Perhaps
511 		 * surprisingly, if the CPU tries to deliver a benign or
512 		 * contributory exception from user code and gets a page fault
513 		 * during delivery, the page fault can be delivered as though
514 		 * it originated directly from user code.  This could happen
515 		 * due to wrong permissions on the IDT, GDT, LDT, TSS, or
516 		 * kernel or IST stack.
517 		 */
518 		store_idt(&idt);
519 
520 		/* Usable even on Xen PV -- it's just slow. */
521 		native_store_gdt(&gdt);
522 
523 		pr_alert("IDT: 0x%lx (limit=0x%hx) GDT: 0x%lx (limit=0x%hx)\n",
524 			 idt.address, idt.size, gdt.address, gdt.size);
525 
526 		store_ldt(ldtr);
527 		show_ldttss(&gdt, "LDTR", ldtr);
528 
529 		store_tr(tr);
530 		show_ldttss(&gdt, "TR", tr);
531 	}
532 
533 	dump_pagetable(address);
534 }
535 
536 static noinline void
537 pgtable_bad(struct pt_regs *regs, unsigned long error_code,
538 	    unsigned long address)
539 {
540 	struct task_struct *tsk;
541 	unsigned long flags;
542 	int sig;
543 
544 	flags = oops_begin();
545 	tsk = current;
546 	sig = SIGKILL;
547 
548 	printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
549 	       tsk->comm, address);
550 	dump_pagetable(address);
551 
552 	if (__die("Bad pagetable", regs, error_code))
553 		sig = 0;
554 
555 	oops_end(flags, regs, sig);
556 }
557 
558 static void set_signal_archinfo(unsigned long address,
559 				unsigned long error_code)
560 {
561 	struct task_struct *tsk = current;
562 
563 	/*
564 	 * To avoid leaking information about the kernel page
565 	 * table layout, pretend that user-mode accesses to
566 	 * kernel addresses are always protection faults.
567 	 *
568 	 * NB: This means that failed vsyscalls with vsyscall=none
569 	 * will have the PROT bit.  This doesn't leak any
570 	 * information and does not appear to cause any problems.
571 	 */
572 	if (address >= TASK_SIZE_MAX)
573 		error_code |= X86_PF_PROT;
574 
575 	tsk->thread.trap_nr = X86_TRAP_PF;
576 	tsk->thread.error_code = error_code | X86_PF_USER;
577 	tsk->thread.cr2 = address;
578 }
579 
580 static noinline void
581 no_context(struct pt_regs *regs, unsigned long error_code,
582 	   unsigned long address, int signal, int si_code)
583 {
584 	struct task_struct *tsk = current;
585 	unsigned long flags;
586 	int sig;
587 
588 	if (user_mode(regs)) {
589 		/*
590 		 * This is an implicit supervisor-mode access from user
591 		 * mode.  Bypass all the kernel-mode recovery code and just
592 		 * OOPS.
593 		 */
594 		goto oops;
595 	}
596 
597 	/* Are we prepared to handle this kernel fault? */
598 	if (fixup_exception(regs, X86_TRAP_PF, error_code, address)) {
599 		/*
600 		 * Any interrupt that takes a fault gets the fixup. This makes
601 		 * the below recursive fault logic only apply to a faults from
602 		 * task context.
603 		 */
604 		if (in_interrupt())
605 			return;
606 
607 		/*
608 		 * Per the above we're !in_interrupt(), aka. task context.
609 		 *
610 		 * In this case we need to make sure we're not recursively
611 		 * faulting through the emulate_vsyscall() logic.
612 		 */
613 		if (current->thread.sig_on_uaccess_err && signal) {
614 			set_signal_archinfo(address, error_code);
615 
616 			/* XXX: hwpoison faults will set the wrong code. */
617 			force_sig_fault(signal, si_code, (void __user *)address);
618 		}
619 
620 		/*
621 		 * Barring that, we can do the fixup and be happy.
622 		 */
623 		return;
624 	}
625 
626 #ifdef CONFIG_VMAP_STACK
627 	/*
628 	 * Stack overflow?  During boot, we can fault near the initial
629 	 * stack in the direct map, but that's not an overflow -- check
630 	 * that we're in vmalloc space to avoid this.
631 	 */
632 	if (is_vmalloc_addr((void *)address) &&
633 	    (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) ||
634 	     address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) {
635 		unsigned long stack = __this_cpu_ist_top_va(DF) - sizeof(void *);
636 		/*
637 		 * We're likely to be running with very little stack space
638 		 * left.  It's plausible that we'd hit this condition but
639 		 * double-fault even before we get this far, in which case
640 		 * we're fine: the double-fault handler will deal with it.
641 		 *
642 		 * We don't want to make it all the way into the oops code
643 		 * and then double-fault, though, because we're likely to
644 		 * break the console driver and lose most of the stack dump.
645 		 */
646 		asm volatile ("movq %[stack], %%rsp\n\t"
647 			      "call handle_stack_overflow\n\t"
648 			      "1: jmp 1b"
649 			      : ASM_CALL_CONSTRAINT
650 			      : "D" ("kernel stack overflow (page fault)"),
651 				"S" (regs), "d" (address),
652 				[stack] "rm" (stack));
653 		unreachable();
654 	}
655 #endif
656 
657 	/*
658 	 * 32-bit:
659 	 *
660 	 *   Valid to do another page fault here, because if this fault
661 	 *   had been triggered by is_prefetch fixup_exception would have
662 	 *   handled it.
663 	 *
664 	 * 64-bit:
665 	 *
666 	 *   Hall of shame of CPU/BIOS bugs.
667 	 */
668 	if (is_prefetch(regs, error_code, address))
669 		return;
670 
671 	if (is_errata93(regs, address))
672 		return;
673 
674 	/*
675 	 * Buggy firmware could access regions which might page fault, try to
676 	 * recover from such faults.
677 	 */
678 	if (IS_ENABLED(CONFIG_EFI))
679 		efi_recover_from_page_fault(address);
680 
681 oops:
682 	/*
683 	 * Oops. The kernel tried to access some bad page. We'll have to
684 	 * terminate things with extreme prejudice:
685 	 */
686 	flags = oops_begin();
687 
688 	show_fault_oops(regs, error_code, address);
689 
690 	if (task_stack_end_corrupted(tsk))
691 		printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
692 
693 	sig = SIGKILL;
694 	if (__die("Oops", regs, error_code))
695 		sig = 0;
696 
697 	/* Executive summary in case the body of the oops scrolled away */
698 	printk(KERN_DEFAULT "CR2: %016lx\n", address);
699 
700 	oops_end(flags, regs, sig);
701 }
702 
703 /*
704  * Print out info about fatal segfaults, if the show_unhandled_signals
705  * sysctl is set:
706  */
707 static inline void
708 show_signal_msg(struct pt_regs *regs, unsigned long error_code,
709 		unsigned long address, struct task_struct *tsk)
710 {
711 	const char *loglvl = task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG;
712 
713 	if (!unhandled_signal(tsk, SIGSEGV))
714 		return;
715 
716 	if (!printk_ratelimit())
717 		return;
718 
719 	printk("%s%s[%d]: segfault at %lx ip %px sp %px error %lx",
720 		loglvl, tsk->comm, task_pid_nr(tsk), address,
721 		(void *)regs->ip, (void *)regs->sp, error_code);
722 
723 	print_vma_addr(KERN_CONT " in ", regs->ip);
724 
725 	printk(KERN_CONT "\n");
726 
727 	show_opcodes(regs, loglvl);
728 }
729 
730 /*
731  * The (legacy) vsyscall page is the long page in the kernel portion
732  * of the address space that has user-accessible permissions.
733  */
734 static bool is_vsyscall_vaddr(unsigned long vaddr)
735 {
736 	return unlikely((vaddr & PAGE_MASK) == VSYSCALL_ADDR);
737 }
738 
739 static void
740 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
741 		       unsigned long address, u32 pkey, int si_code)
742 {
743 	struct task_struct *tsk = current;
744 
745 	/* User mode accesses just cause a SIGSEGV */
746 	if (user_mode(regs) && (error_code & X86_PF_USER)) {
747 		/*
748 		 * It's possible to have interrupts off here:
749 		 */
750 		local_irq_enable();
751 
752 		/*
753 		 * Valid to do another page fault here because this one came
754 		 * from user space:
755 		 */
756 		if (is_prefetch(regs, error_code, address))
757 			return;
758 
759 		if (is_errata100(regs, address))
760 			return;
761 
762 		/*
763 		 * To avoid leaking information about the kernel page table
764 		 * layout, pretend that user-mode accesses to kernel addresses
765 		 * are always protection faults.
766 		 */
767 		if (address >= TASK_SIZE_MAX)
768 			error_code |= X86_PF_PROT;
769 
770 		if (likely(show_unhandled_signals))
771 			show_signal_msg(regs, error_code, address, tsk);
772 
773 		set_signal_archinfo(address, error_code);
774 
775 		if (si_code == SEGV_PKUERR)
776 			force_sig_pkuerr((void __user *)address, pkey);
777 
778 		force_sig_fault(SIGSEGV, si_code, (void __user *)address);
779 
780 		local_irq_disable();
781 
782 		return;
783 	}
784 
785 	if (is_f00f_bug(regs, address))
786 		return;
787 
788 	no_context(regs, error_code, address, SIGSEGV, si_code);
789 }
790 
791 static noinline void
792 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
793 		     unsigned long address)
794 {
795 	__bad_area_nosemaphore(regs, error_code, address, 0, SEGV_MAPERR);
796 }
797 
798 static void
799 __bad_area(struct pt_regs *regs, unsigned long error_code,
800 	   unsigned long address, u32 pkey, int si_code)
801 {
802 	struct mm_struct *mm = current->mm;
803 	/*
804 	 * Something tried to access memory that isn't in our memory map..
805 	 * Fix it, but check if it's kernel or user first..
806 	 */
807 	mmap_read_unlock(mm);
808 
809 	__bad_area_nosemaphore(regs, error_code, address, pkey, si_code);
810 }
811 
812 static noinline void
813 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
814 {
815 	__bad_area(regs, error_code, address, 0, SEGV_MAPERR);
816 }
817 
818 static inline bool bad_area_access_from_pkeys(unsigned long error_code,
819 		struct vm_area_struct *vma)
820 {
821 	/* This code is always called on the current mm */
822 	bool foreign = false;
823 
824 	if (!boot_cpu_has(X86_FEATURE_OSPKE))
825 		return false;
826 	if (error_code & X86_PF_PK)
827 		return true;
828 	/* this checks permission keys on the VMA: */
829 	if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
830 				       (error_code & X86_PF_INSTR), foreign))
831 		return true;
832 	return false;
833 }
834 
835 static noinline void
836 bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
837 		      unsigned long address, struct vm_area_struct *vma)
838 {
839 	/*
840 	 * This OSPKE check is not strictly necessary at runtime.
841 	 * But, doing it this way allows compiler optimizations
842 	 * if pkeys are compiled out.
843 	 */
844 	if (bad_area_access_from_pkeys(error_code, vma)) {
845 		/*
846 		 * A protection key fault means that the PKRU value did not allow
847 		 * access to some PTE.  Userspace can figure out what PKRU was
848 		 * from the XSAVE state.  This function captures the pkey from
849 		 * the vma and passes it to userspace so userspace can discover
850 		 * which protection key was set on the PTE.
851 		 *
852 		 * If we get here, we know that the hardware signaled a X86_PF_PK
853 		 * fault and that there was a VMA once we got in the fault
854 		 * handler.  It does *not* guarantee that the VMA we find here
855 		 * was the one that we faulted on.
856 		 *
857 		 * 1. T1   : mprotect_key(foo, PAGE_SIZE, pkey=4);
858 		 * 2. T1   : set PKRU to deny access to pkey=4, touches page
859 		 * 3. T1   : faults...
860 		 * 4.    T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
861 		 * 5. T1   : enters fault handler, takes mmap_lock, etc...
862 		 * 6. T1   : reaches here, sees vma_pkey(vma)=5, when we really
863 		 *	     faulted on a pte with its pkey=4.
864 		 */
865 		u32 pkey = vma_pkey(vma);
866 
867 		__bad_area(regs, error_code, address, pkey, SEGV_PKUERR);
868 	} else {
869 		__bad_area(regs, error_code, address, 0, SEGV_ACCERR);
870 	}
871 }
872 
873 static void
874 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
875 	  vm_fault_t fault)
876 {
877 	/* Kernel mode? Handle exceptions or die: */
878 	if (!(error_code & X86_PF_USER)) {
879 		no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
880 		return;
881 	}
882 
883 	/* User-space => ok to do another page fault: */
884 	if (is_prefetch(regs, error_code, address))
885 		return;
886 
887 	set_signal_archinfo(address, error_code);
888 
889 #ifdef CONFIG_MEMORY_FAILURE
890 	if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
891 		struct task_struct *tsk = current;
892 		unsigned lsb = 0;
893 
894 		pr_err(
895 	"MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
896 			tsk->comm, tsk->pid, address);
897 		if (fault & VM_FAULT_HWPOISON_LARGE)
898 			lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
899 		if (fault & VM_FAULT_HWPOISON)
900 			lsb = PAGE_SHIFT;
901 		force_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb);
902 		return;
903 	}
904 #endif
905 	force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
906 }
907 
908 static noinline void
909 mm_fault_error(struct pt_regs *regs, unsigned long error_code,
910 	       unsigned long address, vm_fault_t fault)
911 {
912 	if (fatal_signal_pending(current) && !(error_code & X86_PF_USER)) {
913 		no_context(regs, error_code, address, 0, 0);
914 		return;
915 	}
916 
917 	if (fault & VM_FAULT_OOM) {
918 		/* Kernel mode? Handle exceptions or die: */
919 		if (!(error_code & X86_PF_USER)) {
920 			no_context(regs, error_code, address,
921 				   SIGSEGV, SEGV_MAPERR);
922 			return;
923 		}
924 
925 		/*
926 		 * We ran out of memory, call the OOM killer, and return the
927 		 * userspace (which will retry the fault, or kill us if we got
928 		 * oom-killed):
929 		 */
930 		pagefault_out_of_memory();
931 	} else {
932 		if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
933 			     VM_FAULT_HWPOISON_LARGE))
934 			do_sigbus(regs, error_code, address, fault);
935 		else if (fault & VM_FAULT_SIGSEGV)
936 			bad_area_nosemaphore(regs, error_code, address);
937 		else
938 			BUG();
939 	}
940 }
941 
942 static int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte)
943 {
944 	if ((error_code & X86_PF_WRITE) && !pte_write(*pte))
945 		return 0;
946 
947 	if ((error_code & X86_PF_INSTR) && !pte_exec(*pte))
948 		return 0;
949 
950 	return 1;
951 }
952 
953 /*
954  * Handle a spurious fault caused by a stale TLB entry.
955  *
956  * This allows us to lazily refresh the TLB when increasing the
957  * permissions of a kernel page (RO -> RW or NX -> X).  Doing it
958  * eagerly is very expensive since that implies doing a full
959  * cross-processor TLB flush, even if no stale TLB entries exist
960  * on other processors.
961  *
962  * Spurious faults may only occur if the TLB contains an entry with
963  * fewer permission than the page table entry.  Non-present (P = 0)
964  * and reserved bit (R = 1) faults are never spurious.
965  *
966  * There are no security implications to leaving a stale TLB when
967  * increasing the permissions on a page.
968  *
969  * Returns non-zero if a spurious fault was handled, zero otherwise.
970  *
971  * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
972  * (Optional Invalidation).
973  */
974 static noinline int
975 spurious_kernel_fault(unsigned long error_code, unsigned long address)
976 {
977 	pgd_t *pgd;
978 	p4d_t *p4d;
979 	pud_t *pud;
980 	pmd_t *pmd;
981 	pte_t *pte;
982 	int ret;
983 
984 	/*
985 	 * Only writes to RO or instruction fetches from NX may cause
986 	 * spurious faults.
987 	 *
988 	 * These could be from user or supervisor accesses but the TLB
989 	 * is only lazily flushed after a kernel mapping protection
990 	 * change, so user accesses are not expected to cause spurious
991 	 * faults.
992 	 */
993 	if (error_code != (X86_PF_WRITE | X86_PF_PROT) &&
994 	    error_code != (X86_PF_INSTR | X86_PF_PROT))
995 		return 0;
996 
997 	pgd = init_mm.pgd + pgd_index(address);
998 	if (!pgd_present(*pgd))
999 		return 0;
1000 
1001 	p4d = p4d_offset(pgd, address);
1002 	if (!p4d_present(*p4d))
1003 		return 0;
1004 
1005 	if (p4d_large(*p4d))
1006 		return spurious_kernel_fault_check(error_code, (pte_t *) p4d);
1007 
1008 	pud = pud_offset(p4d, address);
1009 	if (!pud_present(*pud))
1010 		return 0;
1011 
1012 	if (pud_large(*pud))
1013 		return spurious_kernel_fault_check(error_code, (pte_t *) pud);
1014 
1015 	pmd = pmd_offset(pud, address);
1016 	if (!pmd_present(*pmd))
1017 		return 0;
1018 
1019 	if (pmd_large(*pmd))
1020 		return spurious_kernel_fault_check(error_code, (pte_t *) pmd);
1021 
1022 	pte = pte_offset_kernel(pmd, address);
1023 	if (!pte_present(*pte))
1024 		return 0;
1025 
1026 	ret = spurious_kernel_fault_check(error_code, pte);
1027 	if (!ret)
1028 		return 0;
1029 
1030 	/*
1031 	 * Make sure we have permissions in PMD.
1032 	 * If not, then there's a bug in the page tables:
1033 	 */
1034 	ret = spurious_kernel_fault_check(error_code, (pte_t *) pmd);
1035 	WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
1036 
1037 	return ret;
1038 }
1039 NOKPROBE_SYMBOL(spurious_kernel_fault);
1040 
1041 int show_unhandled_signals = 1;
1042 
1043 static inline int
1044 access_error(unsigned long error_code, struct vm_area_struct *vma)
1045 {
1046 	/* This is only called for the current mm, so: */
1047 	bool foreign = false;
1048 
1049 	/*
1050 	 * Read or write was blocked by protection keys.  This is
1051 	 * always an unconditional error and can never result in
1052 	 * a follow-up action to resolve the fault, like a COW.
1053 	 */
1054 	if (error_code & X86_PF_PK)
1055 		return 1;
1056 
1057 	/*
1058 	 * Make sure to check the VMA so that we do not perform
1059 	 * faults just to hit a X86_PF_PK as soon as we fill in a
1060 	 * page.
1061 	 */
1062 	if (!arch_vma_access_permitted(vma, (error_code & X86_PF_WRITE),
1063 				       (error_code & X86_PF_INSTR), foreign))
1064 		return 1;
1065 
1066 	if (error_code & X86_PF_WRITE) {
1067 		/* write, present and write, not present: */
1068 		if (unlikely(!(vma->vm_flags & VM_WRITE)))
1069 			return 1;
1070 		return 0;
1071 	}
1072 
1073 	/* read, present: */
1074 	if (unlikely(error_code & X86_PF_PROT))
1075 		return 1;
1076 
1077 	/* read, not present: */
1078 	if (unlikely(!vma_is_accessible(vma)))
1079 		return 1;
1080 
1081 	return 0;
1082 }
1083 
1084 static int fault_in_kernel_space(unsigned long address)
1085 {
1086 	/*
1087 	 * On 64-bit systems, the vsyscall page is at an address above
1088 	 * TASK_SIZE_MAX, but is not considered part of the kernel
1089 	 * address space.
1090 	 */
1091 	if (IS_ENABLED(CONFIG_X86_64) && is_vsyscall_vaddr(address))
1092 		return false;
1093 
1094 	return address >= TASK_SIZE_MAX;
1095 }
1096 
1097 /*
1098  * Called for all faults where 'address' is part of the kernel address
1099  * space.  Might get called for faults that originate from *code* that
1100  * ran in userspace or the kernel.
1101  */
1102 static void
1103 do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code,
1104 		   unsigned long address)
1105 {
1106 	/*
1107 	 * Protection keys exceptions only happen on user pages.  We
1108 	 * have no user pages in the kernel portion of the address
1109 	 * space, so do not expect them here.
1110 	 */
1111 	WARN_ON_ONCE(hw_error_code & X86_PF_PK);
1112 
1113 	/* Was the fault spurious, caused by lazy TLB invalidation? */
1114 	if (spurious_kernel_fault(hw_error_code, address))
1115 		return;
1116 
1117 	/* kprobes don't want to hook the spurious faults: */
1118 	if (kprobe_page_fault(regs, X86_TRAP_PF))
1119 		return;
1120 
1121 	/*
1122 	 * Note, despite being a "bad area", there are quite a few
1123 	 * acceptable reasons to get here, such as erratum fixups
1124 	 * and handling kernel code that can fault, like get_user().
1125 	 *
1126 	 * Don't take the mm semaphore here. If we fixup a prefetch
1127 	 * fault we could otherwise deadlock:
1128 	 */
1129 	bad_area_nosemaphore(regs, hw_error_code, address);
1130 }
1131 NOKPROBE_SYMBOL(do_kern_addr_fault);
1132 
1133 /* Handle faults in the user portion of the address space */
1134 static inline
1135 void do_user_addr_fault(struct pt_regs *regs,
1136 			unsigned long hw_error_code,
1137 			unsigned long address)
1138 {
1139 	struct vm_area_struct *vma;
1140 	struct task_struct *tsk;
1141 	struct mm_struct *mm;
1142 	vm_fault_t fault;
1143 	unsigned int flags = FAULT_FLAG_DEFAULT;
1144 
1145 	tsk = current;
1146 	mm = tsk->mm;
1147 
1148 	/* kprobes don't want to hook the spurious faults: */
1149 	if (unlikely(kprobe_page_fault(regs, X86_TRAP_PF)))
1150 		return;
1151 
1152 	/*
1153 	 * Reserved bits are never expected to be set on
1154 	 * entries in the user portion of the page tables.
1155 	 */
1156 	if (unlikely(hw_error_code & X86_PF_RSVD))
1157 		pgtable_bad(regs, hw_error_code, address);
1158 
1159 	/*
1160 	 * If SMAP is on, check for invalid kernel (supervisor) access to user
1161 	 * pages in the user address space.  The odd case here is WRUSS,
1162 	 * which, according to the preliminary documentation, does not respect
1163 	 * SMAP and will have the USER bit set so, in all cases, SMAP
1164 	 * enforcement appears to be consistent with the USER bit.
1165 	 */
1166 	if (unlikely(cpu_feature_enabled(X86_FEATURE_SMAP) &&
1167 		     !(hw_error_code & X86_PF_USER) &&
1168 		     !(regs->flags & X86_EFLAGS_AC)))
1169 	{
1170 		bad_area_nosemaphore(regs, hw_error_code, address);
1171 		return;
1172 	}
1173 
1174 	/*
1175 	 * If we're in an interrupt, have no user context or are running
1176 	 * in a region with pagefaults disabled then we must not take the fault
1177 	 */
1178 	if (unlikely(faulthandler_disabled() || !mm)) {
1179 		bad_area_nosemaphore(regs, hw_error_code, address);
1180 		return;
1181 	}
1182 
1183 	/*
1184 	 * It's safe to allow irq's after cr2 has been saved and the
1185 	 * vmalloc fault has been handled.
1186 	 *
1187 	 * User-mode registers count as a user access even for any
1188 	 * potential system fault or CPU buglet:
1189 	 */
1190 	if (user_mode(regs)) {
1191 		local_irq_enable();
1192 		flags |= FAULT_FLAG_USER;
1193 	} else {
1194 		if (regs->flags & X86_EFLAGS_IF)
1195 			local_irq_enable();
1196 	}
1197 
1198 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
1199 
1200 	if (hw_error_code & X86_PF_WRITE)
1201 		flags |= FAULT_FLAG_WRITE;
1202 	if (hw_error_code & X86_PF_INSTR)
1203 		flags |= FAULT_FLAG_INSTRUCTION;
1204 
1205 #ifdef CONFIG_X86_64
1206 	/*
1207 	 * Faults in the vsyscall page might need emulation.  The
1208 	 * vsyscall page is at a high address (>PAGE_OFFSET), but is
1209 	 * considered to be part of the user address space.
1210 	 *
1211 	 * The vsyscall page does not have a "real" VMA, so do this
1212 	 * emulation before we go searching for VMAs.
1213 	 *
1214 	 * PKRU never rejects instruction fetches, so we don't need
1215 	 * to consider the PF_PK bit.
1216 	 */
1217 	if (is_vsyscall_vaddr(address)) {
1218 		if (emulate_vsyscall(hw_error_code, regs, address))
1219 			return;
1220 	}
1221 #endif
1222 
1223 	/*
1224 	 * Kernel-mode access to the user address space should only occur
1225 	 * on well-defined single instructions listed in the exception
1226 	 * tables.  But, an erroneous kernel fault occurring outside one of
1227 	 * those areas which also holds mmap_lock might deadlock attempting
1228 	 * to validate the fault against the address space.
1229 	 *
1230 	 * Only do the expensive exception table search when we might be at
1231 	 * risk of a deadlock.  This happens if we
1232 	 * 1. Failed to acquire mmap_lock, and
1233 	 * 2. The access did not originate in userspace.
1234 	 */
1235 	if (unlikely(!mmap_read_trylock(mm))) {
1236 		if (!user_mode(regs) && !search_exception_tables(regs->ip)) {
1237 			/*
1238 			 * Fault from code in kernel from
1239 			 * which we do not expect faults.
1240 			 */
1241 			bad_area_nosemaphore(regs, hw_error_code, address);
1242 			return;
1243 		}
1244 retry:
1245 		mmap_read_lock(mm);
1246 	} else {
1247 		/*
1248 		 * The above down_read_trylock() might have succeeded in
1249 		 * which case we'll have missed the might_sleep() from
1250 		 * down_read():
1251 		 */
1252 		might_sleep();
1253 	}
1254 
1255 	vma = find_vma(mm, address);
1256 	if (unlikely(!vma)) {
1257 		bad_area(regs, hw_error_code, address);
1258 		return;
1259 	}
1260 	if (likely(vma->vm_start <= address))
1261 		goto good_area;
1262 	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1263 		bad_area(regs, hw_error_code, address);
1264 		return;
1265 	}
1266 	if (unlikely(expand_stack(vma, address))) {
1267 		bad_area(regs, hw_error_code, address);
1268 		return;
1269 	}
1270 
1271 	/*
1272 	 * Ok, we have a good vm_area for this memory access, so
1273 	 * we can handle it..
1274 	 */
1275 good_area:
1276 	if (unlikely(access_error(hw_error_code, vma))) {
1277 		bad_area_access_error(regs, hw_error_code, address, vma);
1278 		return;
1279 	}
1280 
1281 	/*
1282 	 * If for any reason at all we couldn't handle the fault,
1283 	 * make sure we exit gracefully rather than endlessly redo
1284 	 * the fault.  Since we never set FAULT_FLAG_RETRY_NOWAIT, if
1285 	 * we get VM_FAULT_RETRY back, the mmap_lock has been unlocked.
1286 	 *
1287 	 * Note that handle_userfault() may also release and reacquire mmap_lock
1288 	 * (and not return with VM_FAULT_RETRY), when returning to userland to
1289 	 * repeat the page fault later with a VM_FAULT_NOPAGE retval
1290 	 * (potentially after handling any pending signal during the return to
1291 	 * userland). The return to userland is identified whenever
1292 	 * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags.
1293 	 */
1294 	fault = handle_mm_fault(vma, address, flags, regs);
1295 
1296 	/* Quick path to respond to signals */
1297 	if (fault_signal_pending(fault, regs)) {
1298 		if (!user_mode(regs))
1299 			no_context(regs, hw_error_code, address, SIGBUS,
1300 				   BUS_ADRERR);
1301 		return;
1302 	}
1303 
1304 	/*
1305 	 * If we need to retry the mmap_lock has already been released,
1306 	 * and if there is a fatal signal pending there is no guarantee
1307 	 * that we made any progress. Handle this case first.
1308 	 */
1309 	if (unlikely((fault & VM_FAULT_RETRY) &&
1310 		     (flags & FAULT_FLAG_ALLOW_RETRY))) {
1311 		flags |= FAULT_FLAG_TRIED;
1312 		goto retry;
1313 	}
1314 
1315 	mmap_read_unlock(mm);
1316 	if (unlikely(fault & VM_FAULT_ERROR)) {
1317 		mm_fault_error(regs, hw_error_code, address, fault);
1318 		return;
1319 	}
1320 
1321 	check_v8086_mode(regs, address, tsk);
1322 }
1323 NOKPROBE_SYMBOL(do_user_addr_fault);
1324 
1325 static __always_inline void
1326 trace_page_fault_entries(struct pt_regs *regs, unsigned long error_code,
1327 			 unsigned long address)
1328 {
1329 	if (!trace_pagefault_enabled())
1330 		return;
1331 
1332 	if (user_mode(regs))
1333 		trace_page_fault_user(address, regs, error_code);
1334 	else
1335 		trace_page_fault_kernel(address, regs, error_code);
1336 }
1337 
1338 static __always_inline void
1339 handle_page_fault(struct pt_regs *regs, unsigned long error_code,
1340 			      unsigned long address)
1341 {
1342 	trace_page_fault_entries(regs, error_code, address);
1343 
1344 	if (unlikely(kmmio_fault(regs, address)))
1345 		return;
1346 
1347 	/* Was the fault on kernel-controlled part of the address space? */
1348 	if (unlikely(fault_in_kernel_space(address))) {
1349 		do_kern_addr_fault(regs, error_code, address);
1350 	} else {
1351 		do_user_addr_fault(regs, error_code, address);
1352 		/*
1353 		 * User address page fault handling might have reenabled
1354 		 * interrupts. Fixing up all potential exit points of
1355 		 * do_user_addr_fault() and its leaf functions is just not
1356 		 * doable w/o creating an unholy mess or turning the code
1357 		 * upside down.
1358 		 */
1359 		local_irq_disable();
1360 	}
1361 }
1362 
1363 DEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault)
1364 {
1365 	unsigned long address = read_cr2();
1366 	irqentry_state_t state;
1367 
1368 	prefetchw(&current->mm->mmap_lock);
1369 
1370 	/*
1371 	 * KVM has two types of events that are, logically, interrupts, but
1372 	 * are unfortunately delivered using the #PF vector.  These events are
1373 	 * "you just accessed valid memory, but the host doesn't have it right
1374 	 * now, so I'll put you to sleep if you continue" and "that memory
1375 	 * you tried to access earlier is available now."
1376 	 *
1377 	 * We are relying on the interrupted context being sane (valid RSP,
1378 	 * relevant locks not held, etc.), which is fine as long as the
1379 	 * interrupted context had IF=1.  We are also relying on the KVM
1380 	 * async pf type field and CR2 being read consistently instead of
1381 	 * getting values from real and async page faults mixed up.
1382 	 *
1383 	 * Fingers crossed.
1384 	 *
1385 	 * The async #PF handling code takes care of idtentry handling
1386 	 * itself.
1387 	 */
1388 	if (kvm_handle_async_pf(regs, (u32)address))
1389 		return;
1390 
1391 	/*
1392 	 * Entry handling for valid #PF from kernel mode is slightly
1393 	 * different: RCU is already watching and rcu_irq_enter() must not
1394 	 * be invoked because a kernel fault on a user space address might
1395 	 * sleep.
1396 	 *
1397 	 * In case the fault hit a RCU idle region the conditional entry
1398 	 * code reenabled RCU to avoid subsequent wreckage which helps
1399 	 * debugability.
1400 	 */
1401 	state = irqentry_enter(regs);
1402 
1403 	instrumentation_begin();
1404 	handle_page_fault(regs, error_code, address);
1405 	instrumentation_end();
1406 
1407 	irqentry_exit(regs, state);
1408 }
1409