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