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