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