1 // SPDX-License-Identifier: GPL-2.0-only 2 /* cpu_feature_enabled() cannot be used this early */ 3 #define USE_EARLY_PGTABLE_L5 4 5 #include <linux/memblock.h> 6 #include <linux/linkage.h> 7 #include <linux/bitops.h> 8 #include <linux/kernel.h> 9 #include <linux/export.h> 10 #include <linux/kvm_types.h> 11 #include <linux/percpu.h> 12 #include <linux/string.h> 13 #include <linux/ctype.h> 14 #include <linux/delay.h> 15 #include <linux/sched/mm.h> 16 #include <linux/sched/clock.h> 17 #include <linux/sched/task.h> 18 #include <linux/sched/smt.h> 19 #include <linux/init.h> 20 #include <linux/kprobes.h> 21 #include <linux/kgdb.h> 22 #include <linux/mem_encrypt.h> 23 #include <linux/smp.h> 24 #include <linux/cpu.h> 25 #include <linux/io.h> 26 #include <linux/syscore_ops.h> 27 #include <linux/pgtable.h> 28 #include <linux/stackprotector.h> 29 #include <linux/utsname.h> 30 #include <linux/efi.h> 31 32 #include <asm/alternative.h> 33 #include <asm/cmdline.h> 34 #include <asm/cpuid/api.h> 35 #include <asm/perf_event.h> 36 #include <asm/mmu_context.h> 37 #include <asm/doublefault.h> 38 #include <asm/archrandom.h> 39 #include <asm/hypervisor.h> 40 #include <asm/processor.h> 41 #include <asm/tlbflush.h> 42 #include <asm/debugreg.h> 43 #include <asm/sections.h> 44 #include <asm/vsyscall.h> 45 #include <linux/topology.h> 46 #include <linux/cpumask.h> 47 #include <linux/atomic.h> 48 #include <asm/proto.h> 49 #include <asm/setup.h> 50 #include <asm/apic.h> 51 #include <asm/desc.h> 52 #include <asm/fpu/api.h> 53 #include <asm/mtrr.h> 54 #include <asm/hwcap2.h> 55 #include <linux/numa.h> 56 #include <asm/numa.h> 57 #include <asm/asm.h> 58 #include <asm/bugs.h> 59 #include <asm/cpu.h> 60 #include <asm/mce.h> 61 #include <asm/msr.h> 62 #include <asm/cacheinfo.h> 63 #include <asm/memtype.h> 64 #include <asm/microcode.h> 65 #include <asm/intel-family.h> 66 #include <asm/cpu_device_id.h> 67 #include <asm/fred.h> 68 #include <asm/uv/uv.h> 69 #include <asm/ia32.h> 70 #include <asm/set_memory.h> 71 #include <asm/traps.h> 72 #include <asm/sev.h> 73 #include <asm/tdx.h> 74 #include <asm/virt.h> 75 #include <asm/posted_intr.h> 76 #include <asm/runtime-const.h> 77 78 #include "cpu.h" 79 80 DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info); 81 EXPORT_PER_CPU_SYMBOL(cpu_info); 82 83 /* Used for modules: built-in code uses runtime constants */ 84 unsigned long USER_PTR_MAX; 85 EXPORT_SYMBOL(USER_PTR_MAX); 86 87 u32 elf_hwcap2 __read_mostly; 88 89 /* Number of siblings per CPU package */ 90 unsigned int __max_threads_per_core __ro_after_init = 1; 91 EXPORT_SYMBOL(__max_threads_per_core); 92 93 unsigned int __max_dies_per_package __ro_after_init = 1; 94 EXPORT_SYMBOL(__max_dies_per_package); 95 96 unsigned int __max_logical_packages __ro_after_init = 1; 97 EXPORT_SYMBOL(__max_logical_packages); 98 99 unsigned int __num_nodes_per_package __ro_after_init = 1; 100 EXPORT_SYMBOL(__num_nodes_per_package); 101 102 unsigned int __num_cores_per_package __ro_after_init = 1; 103 EXPORT_SYMBOL(__num_cores_per_package); 104 105 unsigned int __num_threads_per_package __ro_after_init = 1; 106 EXPORT_SYMBOL(__num_threads_per_package); 107 108 static struct ppin_info { 109 int feature; 110 int msr_ppin_ctl; 111 int msr_ppin; 112 } ppin_info[] = { 113 [X86_VENDOR_INTEL] = { 114 .feature = X86_FEATURE_INTEL_PPIN, 115 .msr_ppin_ctl = MSR_PPIN_CTL, 116 .msr_ppin = MSR_PPIN 117 }, 118 [X86_VENDOR_AMD] = { 119 .feature = X86_FEATURE_AMD_PPIN, 120 .msr_ppin_ctl = MSR_AMD_PPIN_CTL, 121 .msr_ppin = MSR_AMD_PPIN 122 }, 123 }; 124 125 static const struct x86_cpu_id ppin_cpuids[] = { 126 X86_MATCH_FEATURE(X86_FEATURE_AMD_PPIN, &ppin_info[X86_VENDOR_AMD]), 127 X86_MATCH_FEATURE(X86_FEATURE_INTEL_PPIN, &ppin_info[X86_VENDOR_INTEL]), 128 129 /* Legacy models without CPUID enumeration */ 130 X86_MATCH_VFM(INTEL_IVYBRIDGE_X, &ppin_info[X86_VENDOR_INTEL]), 131 X86_MATCH_VFM(INTEL_HASWELL_X, &ppin_info[X86_VENDOR_INTEL]), 132 X86_MATCH_VFM(INTEL_BROADWELL_D, &ppin_info[X86_VENDOR_INTEL]), 133 X86_MATCH_VFM(INTEL_BROADWELL_X, &ppin_info[X86_VENDOR_INTEL]), 134 X86_MATCH_VFM(INTEL_SKYLAKE_X, &ppin_info[X86_VENDOR_INTEL]), 135 X86_MATCH_VFM(INTEL_ICELAKE_X, &ppin_info[X86_VENDOR_INTEL]), 136 X86_MATCH_VFM(INTEL_ICELAKE_D, &ppin_info[X86_VENDOR_INTEL]), 137 X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X, &ppin_info[X86_VENDOR_INTEL]), 138 X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X, &ppin_info[X86_VENDOR_INTEL]), 139 X86_MATCH_VFM(INTEL_XEON_PHI_KNL, &ppin_info[X86_VENDOR_INTEL]), 140 X86_MATCH_VFM(INTEL_XEON_PHI_KNM, &ppin_info[X86_VENDOR_INTEL]), 141 142 {} 143 }; 144 145 static void ppin_init(struct cpuinfo_x86 *c) 146 { 147 const struct x86_cpu_id *id; 148 unsigned long long val; 149 struct ppin_info *info; 150 151 id = x86_match_cpu(ppin_cpuids); 152 if (!id) 153 return; 154 155 /* 156 * Testing the presence of the MSR is not enough. Need to check 157 * that the PPIN_CTL allows reading of the PPIN. 158 */ 159 info = (struct ppin_info *)id->driver_data; 160 161 if (rdmsrq_safe(info->msr_ppin_ctl, &val)) 162 goto clear_ppin; 163 164 if ((val & 3UL) == 1UL) { 165 /* PPIN locked in disabled mode */ 166 goto clear_ppin; 167 } 168 169 /* If PPIN is disabled, try to enable */ 170 if (!(val & 2UL)) { 171 wrmsrq_safe(info->msr_ppin_ctl, val | 2UL); 172 rdmsrq_safe(info->msr_ppin_ctl, &val); 173 } 174 175 /* Is the enable bit set? */ 176 if (val & 2UL) { 177 c->ppin = native_rdmsrq(info->msr_ppin); 178 set_cpu_cap(c, info->feature); 179 return; 180 } 181 182 clear_ppin: 183 setup_clear_cpu_cap(info->feature); 184 } 185 186 static void default_init(struct cpuinfo_x86 *c) 187 { 188 #ifdef CONFIG_X86_64 189 cpu_detect_cache_sizes(c); 190 #else 191 /* Not much we can do here... */ 192 /* Check if at least it has cpuid */ 193 if (c->cpuid_level == -1) { 194 /* No cpuid. It must be an ancient CPU */ 195 if (c->x86 == 4) 196 strcpy(c->x86_model_id, "486"); 197 else if (c->x86 == 3) 198 strcpy(c->x86_model_id, "386"); 199 } 200 #endif 201 } 202 203 static const struct cpu_dev default_cpu = { 204 .c_init = default_init, 205 .c_vendor = "Unknown", 206 .c_x86_vendor = X86_VENDOR_UNKNOWN, 207 }; 208 209 static const struct cpu_dev *this_cpu = &default_cpu; 210 211 DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = { 212 #ifdef CONFIG_X86_64 213 /* 214 * We need valid kernel segments for data and code in long mode too 215 * IRET will check the segment types kkeil 2000/10/28 216 * Also sysret mandates a special GDT layout 217 * 218 * TLS descriptors are currently at a different place compared to i386. 219 * Hopefully nobody expects them at a fixed place (Wine?) 220 */ 221 [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff), 222 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(DESC_CODE64, 0, 0xfffff), 223 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(DESC_DATA64, 0, 0xfffff), 224 [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(DESC_CODE32 | DESC_USER, 0, 0xfffff), 225 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(DESC_DATA64 | DESC_USER, 0, 0xfffff), 226 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(DESC_CODE64 | DESC_USER, 0, 0xfffff), 227 #else 228 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff), 229 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff), 230 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(DESC_CODE32 | DESC_USER, 0, 0xfffff), 231 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(DESC_DATA32 | DESC_USER, 0, 0xfffff), 232 /* 233 * Segments used for calling PnP BIOS have byte granularity. 234 * They code segments and data segments have fixed 64k limits, 235 * the transfer segment sizes are set at run time. 236 */ 237 [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(DESC_CODE32_BIOS, 0, 0xffff), 238 [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(DESC_CODE16, 0, 0xffff), 239 [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(DESC_DATA16, 0, 0xffff), 240 [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(DESC_DATA16, 0, 0), 241 [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(DESC_DATA16, 0, 0), 242 /* 243 * The APM segments have byte granularity and their bases 244 * are set at run time. All have 64k limits. 245 */ 246 [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(DESC_CODE32_BIOS, 0, 0xffff), 247 [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(DESC_CODE16, 0, 0xffff), 248 [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(DESC_DATA32_BIOS, 0, 0xffff), 249 250 [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff), 251 [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff), 252 #endif 253 } }; 254 EXPORT_PER_CPU_SYMBOL_GPL(gdt_page); 255 SYM_PIC_ALIAS(gdt_page); 256 257 #ifdef CONFIG_X86_64 258 static int __init x86_nopcid_setup(char *s) 259 { 260 /* nopcid doesn't accept parameters */ 261 if (s) 262 return -EINVAL; 263 264 /* do not emit a message if the feature is not present */ 265 if (!boot_cpu_has(X86_FEATURE_PCID)) 266 return 0; 267 268 setup_clear_cpu_cap(X86_FEATURE_PCID); 269 pr_info("nopcid: PCID feature disabled\n"); 270 return 0; 271 } 272 early_param("nopcid", x86_nopcid_setup); 273 #endif 274 275 static int __init x86_noinvpcid_setup(char *s) 276 { 277 /* noinvpcid doesn't accept parameters */ 278 if (s) 279 return -EINVAL; 280 281 /* do not emit a message if the feature is not present */ 282 if (!boot_cpu_has(X86_FEATURE_INVPCID)) 283 return 0; 284 285 setup_clear_cpu_cap(X86_FEATURE_INVPCID); 286 pr_info("noinvpcid: INVPCID feature disabled\n"); 287 return 0; 288 } 289 early_param("noinvpcid", x86_noinvpcid_setup); 290 291 /* Standard macro to see if a specific flag is changeable */ 292 static inline bool flag_is_changeable_p(unsigned long flag) 293 { 294 unsigned long f1, f2; 295 296 if (!IS_ENABLED(CONFIG_X86_32)) 297 return true; 298 299 /* 300 * Cyrix and IDT cpus allow disabling of CPUID 301 * so the code below may return different results 302 * when it is executed before and after enabling 303 * the CPUID. Add "volatile" to not allow gcc to 304 * optimize the subsequent calls to this function. 305 */ 306 asm volatile ("pushfl \n\t" 307 "pushfl \n\t" 308 "popl %0 \n\t" 309 "movl %0, %1 \n\t" 310 "xorl %2, %0 \n\t" 311 "pushl %0 \n\t" 312 "popfl \n\t" 313 "pushfl \n\t" 314 "popl %0 \n\t" 315 "popfl \n\t" 316 317 : "=&r" (f1), "=&r" (f2) 318 : "ir" (flag)); 319 320 return (f1 ^ f2) & flag; 321 } 322 323 #ifdef CONFIG_X86_32 324 static int cachesize_override = -1; 325 static int disable_x86_serial_nr = 1; 326 327 static int __init cachesize_setup(char *str) 328 { 329 get_option(&str, &cachesize_override); 330 return 1; 331 } 332 __setup("cachesize=", cachesize_setup); 333 334 /* Probe for the CPUID instruction */ 335 bool cpuid_feature(void) 336 { 337 return flag_is_changeable_p(X86_EFLAGS_ID); 338 } 339 340 static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) 341 { 342 struct msr val; 343 344 if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr) 345 return; 346 347 /* Disable processor serial number: */ 348 349 rdmsrq(MSR_IA32_BBL_CR_CTL, val.q); 350 val.l |= 0x200000; 351 wrmsrq(MSR_IA32_BBL_CR_CTL, val.q); 352 353 pr_notice("CPU serial number disabled.\n"); 354 clear_cpu_cap(c, X86_FEATURE_PN); 355 356 /* Disabling the serial number may affect the cpuid level */ 357 c->cpuid_level = cpuid_eax(0); 358 } 359 360 static int __init x86_serial_nr_setup(char *s) 361 { 362 disable_x86_serial_nr = 0; 363 return 1; 364 } 365 __setup("serialnumber", x86_serial_nr_setup); 366 #else 367 static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) 368 { 369 } 370 #endif 371 372 static __always_inline void setup_smep(struct cpuinfo_x86 *c) 373 { 374 if (cpu_has(c, X86_FEATURE_SMEP)) 375 cr4_set_bits(X86_CR4_SMEP); 376 } 377 378 static __always_inline void setup_smap(struct cpuinfo_x86 *c) 379 { 380 unsigned long eflags = native_save_fl(); 381 382 /* This should have been cleared long ago */ 383 BUG_ON(eflags & X86_EFLAGS_AC); 384 385 if (cpu_has(c, X86_FEATURE_SMAP)) 386 cr4_set_bits(X86_CR4_SMAP); 387 } 388 389 static __always_inline void setup_umip(struct cpuinfo_x86 *c) 390 { 391 /* Check the boot processor, plus build option for UMIP. */ 392 if (!cpu_feature_enabled(X86_FEATURE_UMIP)) 393 goto out; 394 395 /* Check the current processor's cpuid bits. */ 396 if (!cpu_has(c, X86_FEATURE_UMIP)) 397 goto out; 398 399 cr4_set_bits(X86_CR4_UMIP); 400 401 pr_info_once("x86/cpu: User Mode Instruction Prevention (UMIP) activated\n"); 402 403 return; 404 405 out: 406 /* 407 * Make sure UMIP is disabled in case it was enabled in a 408 * previous boot (e.g., via kexec). 409 */ 410 cr4_clear_bits(X86_CR4_UMIP); 411 } 412 413 static int enable_lass(unsigned int cpu) 414 { 415 cr4_set_bits(X86_CR4_LASS); 416 417 return 0; 418 } 419 420 /* 421 * Finalize features that need to be enabled just before entering 422 * userspace. Note that this only runs on a single CPU. Use appropriate 423 * callbacks if all the CPUs need to reflect the same change. 424 */ 425 static int cpu_finalize_pre_userspace(void) 426 { 427 if (!cpu_feature_enabled(X86_FEATURE_LASS)) 428 return 0; 429 430 /* Runs on all online CPUs and future CPUs that come online. */ 431 cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/lass:enable", enable_lass, NULL); 432 433 return 0; 434 } 435 late_initcall(cpu_finalize_pre_userspace); 436 437 /* These bits should not change their value after CPU init is finished. */ 438 static const unsigned long cr4_pinned_mask = X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP | 439 X86_CR4_FSGSBASE | X86_CR4_CET; 440 441 /* 442 * The CR pinning protects against ROP on the 'mov %reg, %CRn' instruction(s). 443 * Since you can ROP directly to these instructions (barring shadow stack), 444 * any protection must follow immediately and unconditionally after that. 445 * 446 * Specifically, the CR[04] write functions below will have the value 447 * validation controlled by the @cr_pinning static_branch which is 448 * __ro_after_init, just like the cr4_pinned_bits value. 449 * 450 * Once set, an attacker will have to defeat page-tables to get around these 451 * restrictions. Which is a much bigger ask than 'simple' ROP. 452 */ 453 static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning); 454 static unsigned long cr4_pinned_bits __ro_after_init; 455 456 void native_write_cr0(unsigned long val) 457 { 458 unsigned long bits_missing = 0; 459 460 set_register: 461 asm volatile("mov %0,%%cr0": "+r" (val) : : "memory"); 462 463 if (static_branch_likely(&cr_pinning)) { 464 if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) { 465 bits_missing = X86_CR0_WP; 466 val |= bits_missing; 467 goto set_register; 468 } 469 /* Warn after we've set the missing bits. */ 470 WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n"); 471 } 472 } 473 EXPORT_SYMBOL(native_write_cr0); 474 475 void __no_profile native_write_cr4(unsigned long val) 476 { 477 unsigned long bits_changed = 0; 478 479 set_register: 480 asm volatile("mov %0,%%cr4": "+r" (val) : : "memory"); 481 482 if (static_branch_likely(&cr_pinning)) { 483 if (unlikely((val & cr4_pinned_mask) != cr4_pinned_bits)) { 484 bits_changed = (val & cr4_pinned_mask) ^ cr4_pinned_bits; 485 val = (val & ~cr4_pinned_mask) | cr4_pinned_bits; 486 goto set_register; 487 } 488 /* Warn after we've corrected the changed bits. */ 489 WARN_ONCE(bits_changed, "pinned CR4 bits changed: 0x%lx!?\n", 490 bits_changed); 491 } 492 } 493 #if IS_MODULE(CONFIG_LKDTM) 494 EXPORT_SYMBOL_GPL(native_write_cr4); 495 #endif 496 497 void cr4_update_irqsoff(unsigned long set, unsigned long clear) 498 { 499 unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4); 500 501 lockdep_assert_irqs_disabled(); 502 503 newval = (cr4 & ~clear) | set; 504 if (newval != cr4) { 505 this_cpu_write(cpu_tlbstate.cr4, newval); 506 __write_cr4(newval); 507 } 508 } 509 EXPORT_SYMBOL_FOR_KVM(cr4_update_irqsoff); 510 511 /* Read the CR4 shadow. */ 512 unsigned long cr4_read_shadow(void) 513 { 514 return this_cpu_read(cpu_tlbstate.cr4); 515 } 516 EXPORT_SYMBOL_FOR_KVM(cr4_read_shadow); 517 518 void cr4_init(void) 519 { 520 unsigned long cr4 = __read_cr4(); 521 522 if (boot_cpu_has(X86_FEATURE_PCID)) 523 cr4 |= X86_CR4_PCIDE; 524 if (static_branch_likely(&cr_pinning)) 525 cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits; 526 527 __write_cr4(cr4); 528 529 /* Initialize cr4 shadow for this CPU. */ 530 this_cpu_write(cpu_tlbstate.cr4, cr4); 531 } 532 533 /* 534 * Once CPU feature detection is finished (and boot params have been 535 * parsed), record any of the sensitive CR bits that are set, and 536 * enable CR pinning. 537 */ 538 static void __init setup_cr_pinning(void) 539 { 540 cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask; 541 static_key_enable(&cr_pinning.key); 542 } 543 544 static __init int x86_nofsgsbase_setup(char *arg) 545 { 546 /* Require an exact match without trailing characters. */ 547 if (strlen(arg)) 548 return 0; 549 550 /* Do not emit a message if the feature is not present. */ 551 if (!boot_cpu_has(X86_FEATURE_FSGSBASE)) 552 return 1; 553 554 setup_clear_cpu_cap(X86_FEATURE_FSGSBASE); 555 pr_info("FSGSBASE disabled via kernel command line\n"); 556 return 1; 557 } 558 __setup("nofsgsbase", x86_nofsgsbase_setup); 559 560 /* 561 * Protection Keys are not available in 32-bit mode. 562 */ 563 static bool pku_disabled; 564 565 static __always_inline void setup_pku(struct cpuinfo_x86 *c) 566 { 567 if (c == &boot_cpu_data) { 568 if (pku_disabled || !cpu_feature_enabled(X86_FEATURE_PKU)) 569 return; 570 /* 571 * Setting CR4.PKE will cause the X86_FEATURE_OSPKE cpuid 572 * bit to be set. Enforce it. 573 */ 574 setup_force_cpu_cap(X86_FEATURE_OSPKE); 575 576 } else if (!cpu_feature_enabled(X86_FEATURE_OSPKE)) { 577 return; 578 } 579 580 cr4_set_bits(X86_CR4_PKE); 581 /* Load the default PKRU value */ 582 pkru_write_default(); 583 } 584 585 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS 586 static __init int setup_disable_pku(char *arg) 587 { 588 /* 589 * Do not clear the X86_FEATURE_PKU bit. All of the 590 * runtime checks are against OSPKE so clearing the 591 * bit does nothing. 592 * 593 * This way, we will see "pku" in cpuinfo, but not 594 * "ospke", which is exactly what we want. It shows 595 * that the CPU has PKU, but the OS has not enabled it. 596 * This happens to be exactly how a system would look 597 * if we disabled the config option. 598 */ 599 pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n"); 600 pku_disabled = true; 601 return 1; 602 } 603 __setup("nopku", setup_disable_pku); 604 #endif 605 606 #ifdef CONFIG_X86_KERNEL_IBT 607 608 __noendbr u64 ibt_save(bool disable) 609 { 610 u64 msr = 0; 611 612 if (cpu_feature_enabled(X86_FEATURE_IBT)) { 613 rdmsrq(MSR_IA32_S_CET, msr); 614 if (disable) 615 wrmsrq(MSR_IA32_S_CET, msr & ~CET_ENDBR_EN); 616 } 617 618 return msr; 619 } 620 621 __noendbr void ibt_restore(u64 save) 622 { 623 u64 msr; 624 625 if (cpu_feature_enabled(X86_FEATURE_IBT)) { 626 rdmsrq(MSR_IA32_S_CET, msr); 627 msr &= ~CET_ENDBR_EN; 628 msr |= (save & CET_ENDBR_EN); 629 wrmsrq(MSR_IA32_S_CET, msr); 630 } 631 } 632 633 #endif 634 635 static __always_inline void setup_cet(struct cpuinfo_x86 *c) 636 { 637 bool user_shstk, kernel_ibt; 638 639 if (!IS_ENABLED(CONFIG_X86_CET)) 640 return; 641 642 kernel_ibt = HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT); 643 user_shstk = cpu_feature_enabled(X86_FEATURE_SHSTK) && 644 IS_ENABLED(CONFIG_X86_USER_SHADOW_STACK); 645 646 if (!kernel_ibt && !user_shstk) 647 return; 648 649 if (user_shstk) 650 set_cpu_cap(c, X86_FEATURE_USER_SHSTK); 651 652 if (kernel_ibt) 653 wrmsrq(MSR_IA32_S_CET, CET_ENDBR_EN); 654 else 655 wrmsrq(MSR_IA32_S_CET, 0); 656 657 cr4_set_bits(X86_CR4_CET); 658 659 if (kernel_ibt && ibt_selftest()) { 660 pr_err("IBT selftest: Failed!\n"); 661 wrmsrq(MSR_IA32_S_CET, 0); 662 setup_clear_cpu_cap(X86_FEATURE_IBT); 663 } 664 } 665 666 __noendbr void cet_disable(void) 667 { 668 if (!(cpu_feature_enabled(X86_FEATURE_IBT) || 669 cpu_feature_enabled(X86_FEATURE_SHSTK))) 670 return; 671 672 wrmsrq(MSR_IA32_S_CET, 0); 673 wrmsrq(MSR_IA32_U_CET, 0); 674 } 675 676 /* 677 * Some CPU features depend on higher CPUID levels, which may not always 678 * be available due to CPUID level capping or broken virtualization 679 * software. Add those features to this table to auto-disable them. 680 */ 681 struct cpuid_dependent_feature { 682 u32 feature; 683 u32 level; 684 }; 685 686 static const struct cpuid_dependent_feature 687 cpuid_dependent_features[] = { 688 { X86_FEATURE_MWAIT, CPUID_LEAF_MWAIT }, 689 { X86_FEATURE_DCA, CPUID_LEAF_DCA }, 690 { X86_FEATURE_XSAVE, CPUID_LEAF_XSTATE }, 691 { 0, 0 } 692 }; 693 694 static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) 695 { 696 const struct cpuid_dependent_feature *df; 697 698 for (df = cpuid_dependent_features; df->feature; df++) { 699 700 if (!cpu_has(c, df->feature)) 701 continue; 702 /* 703 * Note: cpuid_level is set to -1 if unavailable, but 704 * extended_extended_level is set to 0 if unavailable 705 * and the legitimate extended levels are all negative 706 * when signed; hence the weird messing around with 707 * signs here... 708 */ 709 if (!((s32)df->level < 0 ? 710 (u32)df->level > (u32)c->extended_cpuid_level : 711 (s32)df->level > (s32)c->cpuid_level)) 712 continue; 713 714 clear_cpu_cap(c, df->feature); 715 if (!warn) 716 continue; 717 718 pr_warn("CPU: CPU feature %s disabled, no CPUID level 0x%x\n", 719 x86_cap_flags[df->feature], df->level); 720 } 721 } 722 723 /* 724 * Naming convention should be: <Name> [(<Codename>)] 725 * This table only is used unless init_<vendor>() below doesn't set it; 726 * in particular, if CPUID levels 0x80000002..4 are supported, this 727 * isn't used 728 */ 729 730 /* Look up CPU names by table lookup. */ 731 static const char *table_lookup_model(struct cpuinfo_x86 *c) 732 { 733 #ifdef CONFIG_X86_32 734 const struct legacy_cpu_model_info *info; 735 736 if (c->x86_model >= 16) 737 return NULL; /* Range check */ 738 739 if (!this_cpu) 740 return NULL; 741 742 info = this_cpu->legacy_models; 743 744 while (info->family) { 745 if (info->family == c->x86) 746 return info->model_names[c->x86_model]; 747 info++; 748 } 749 #endif 750 return NULL; /* Not found */ 751 } 752 753 /* Aligned to unsigned long to avoid split lock in atomic bitmap ops */ 754 __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long)); 755 __u32 cpu_caps_set[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long)); 756 757 #ifdef CONFIG_X86_32 758 /* The 32-bit entry code needs to find cpu_entry_area. */ 759 DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area); 760 #endif 761 762 /* Load the original GDT from the per-cpu structure */ 763 void load_direct_gdt(int cpu) 764 { 765 struct desc_ptr gdt_descr; 766 767 gdt_descr.address = (long)get_cpu_gdt_rw(cpu); 768 gdt_descr.size = GDT_SIZE - 1; 769 load_gdt(&gdt_descr); 770 } 771 EXPORT_SYMBOL_FOR_KVM(load_direct_gdt); 772 773 /* Load a fixmap remapping of the per-cpu GDT */ 774 void load_fixmap_gdt(int cpu) 775 { 776 struct desc_ptr gdt_descr; 777 778 gdt_descr.address = (long)get_cpu_gdt_ro(cpu); 779 gdt_descr.size = GDT_SIZE - 1; 780 load_gdt(&gdt_descr); 781 } 782 EXPORT_SYMBOL_GPL(load_fixmap_gdt); 783 784 /** 785 * switch_gdt_and_percpu_base - Switch to direct GDT and runtime per CPU base 786 * @cpu: The CPU number for which this is invoked 787 * 788 * Invoked during early boot to switch from early GDT and early per CPU to 789 * the direct GDT and the runtime per CPU area. On 32-bit the percpu base 790 * switch is implicit by loading the direct GDT. On 64bit this requires 791 * to update GSBASE. 792 */ 793 void __init switch_gdt_and_percpu_base(int cpu) 794 { 795 load_direct_gdt(cpu); 796 797 #ifdef CONFIG_X86_64 798 /* 799 * No need to load %gs. It is already correct. 800 * 801 * Writing %gs on 64bit would zero GSBASE which would make any per 802 * CPU operation up to the point of the wrmsrq() fault. 803 * 804 * Set GSBASE to the new offset. Until the wrmsrq() happens the 805 * early mapping is still valid. That means the GSBASE update will 806 * lose any prior per CPU data which was not copied over in 807 * setup_per_cpu_areas(). 808 * 809 * This works even with stackprotector enabled because the 810 * per CPU stack canary is 0 in both per CPU areas. 811 */ 812 wrmsrq(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu)); 813 #else 814 /* 815 * %fs is already set to __KERNEL_PERCPU, but after switching GDT 816 * it is required to load FS again so that the 'hidden' part is 817 * updated from the new GDT. Up to this point the early per CPU 818 * translation is active. Any content of the early per CPU data 819 * which was not copied over in setup_per_cpu_areas() is lost. 820 */ 821 loadsegment(fs, __KERNEL_PERCPU); 822 #endif 823 } 824 825 static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {}; 826 827 static void get_model_name(struct cpuinfo_x86 *c) 828 { 829 unsigned int *v; 830 char *p, *q, *s; 831 832 if (c->extended_cpuid_level < 0x80000004) 833 return; 834 835 v = (unsigned int *)c->x86_model_id; 836 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]); 837 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]); 838 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]); 839 c->x86_model_id[48] = 0; 840 841 /* Trim whitespace */ 842 p = q = s = &c->x86_model_id[0]; 843 844 while (*p == ' ') 845 p++; 846 847 while (*p) { 848 /* Note the last non-whitespace index */ 849 if (!isspace(*p)) 850 s = q; 851 852 *q++ = *p++; 853 } 854 855 *(s + 1) = '\0'; 856 } 857 858 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c) 859 { 860 unsigned int n, dummy, ebx, ecx, edx, l2size; 861 862 n = c->extended_cpuid_level; 863 864 if (n >= 0x80000005) { 865 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx); 866 c->x86_cache_size = (ecx>>24) + (edx>>24); 867 #ifdef CONFIG_X86_64 868 /* On K8 L1 TLB is inclusive, so don't count it */ 869 c->x86_tlbsize = 0; 870 #endif 871 } 872 873 if (n < 0x80000006) /* Some chips just has a large L1. */ 874 return; 875 876 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx); 877 l2size = ecx >> 16; 878 879 #ifdef CONFIG_X86_64 880 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff); 881 #else 882 /* do processor-specific cache resizing */ 883 if (this_cpu->legacy_cache_size) 884 l2size = this_cpu->legacy_cache_size(c, l2size); 885 886 /* Allow user to override all this if necessary. */ 887 if (cachesize_override != -1) 888 l2size = cachesize_override; 889 890 if (l2size == 0) 891 return; /* Again, no L2 cache is possible */ 892 #endif 893 894 c->x86_cache_size = l2size; 895 } 896 897 u16 __read_mostly tlb_lli_4k; 898 u16 __read_mostly tlb_lli_2m; 899 u16 __read_mostly tlb_lli_4m; 900 u16 __read_mostly tlb_lld_4k; 901 u16 __read_mostly tlb_lld_2m; 902 u16 __read_mostly tlb_lld_4m; 903 u16 __read_mostly tlb_lld_1g; 904 905 static void cpu_detect_tlb(struct cpuinfo_x86 *c) 906 { 907 if (this_cpu->c_detect_tlb) 908 this_cpu->c_detect_tlb(c); 909 910 pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n", 911 tlb_lli_4k, tlb_lli_2m, tlb_lli_4m); 912 913 pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n", 914 tlb_lld_4k, tlb_lld_2m, tlb_lld_4m, tlb_lld_1g); 915 } 916 917 void get_cpu_vendor(struct cpuinfo_x86 *c) 918 { 919 char *v = c->x86_vendor_id; 920 int i; 921 922 for (i = 0; i < X86_VENDOR_NUM; i++) { 923 if (!cpu_devs[i]) 924 break; 925 926 if (!strcmp(v, cpu_devs[i]->c_ident[0]) || 927 (cpu_devs[i]->c_ident[1] && 928 !strcmp(v, cpu_devs[i]->c_ident[1]))) { 929 930 this_cpu = cpu_devs[i]; 931 c->x86_vendor = this_cpu->c_x86_vendor; 932 return; 933 } 934 } 935 936 pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \ 937 "CPU: Your system may be unstable.\n", v); 938 939 c->x86_vendor = X86_VENDOR_UNKNOWN; 940 this_cpu = &default_cpu; 941 } 942 943 void cpu_detect(struct cpuinfo_x86 *c) 944 { 945 const struct leaf_0x0_0 *l0 = cpuid_leaf(c, 0x0); 946 const struct leaf_0x1_0 *l1; 947 948 c->cpuid_level = l0->max_std_leaf; 949 *(u32 *)&c->x86_vendor_id[0] = l0->cpu_vendorid_0; 950 *(u32 *)&c->x86_vendor_id[4] = l0->cpu_vendorid_1; 951 *(u32 *)&c->x86_vendor_id[8] = l0->cpu_vendorid_2; 952 953 c->x86 = 4; 954 955 l1 = cpuid_leaf(c, 0x1); 956 if (!l1) 957 return; 958 959 c->x86 = cpuid_family(l1); 960 c->x86_model = cpuid_model(l1); 961 c->x86_stepping = l1->stepping; 962 963 if (l1->clflush) { 964 c->x86_clflush_size = l1->clflush_size * 8; 965 c->x86_cache_alignment = c->x86_clflush_size; 966 } 967 } 968 969 static void apply_forced_caps(struct cpuinfo_x86 *c) 970 { 971 int i; 972 973 for (i = 0; i < NCAPINTS + NBUGINTS; i++) { 974 c->x86_capability[i] &= ~cpu_caps_cleared[i]; 975 c->x86_capability[i] |= cpu_caps_set[i]; 976 } 977 } 978 979 static void init_speculation_control(struct cpuinfo_x86 *c) 980 { 981 /* 982 * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support, 983 * and they also have a different bit for STIBP support. Also, 984 * a hypervisor might have set the individual AMD bits even on 985 * Intel CPUs, for finer-grained selection of what's available. 986 */ 987 if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) { 988 set_cpu_cap(c, X86_FEATURE_IBRS); 989 set_cpu_cap(c, X86_FEATURE_IBPB); 990 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL); 991 } 992 993 if (cpu_has(c, X86_FEATURE_INTEL_STIBP)) 994 set_cpu_cap(c, X86_FEATURE_STIBP); 995 996 if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) || 997 cpu_has(c, X86_FEATURE_VIRT_SSBD)) 998 set_cpu_cap(c, X86_FEATURE_SSBD); 999 1000 if (cpu_has(c, X86_FEATURE_AMD_IBRS)) { 1001 set_cpu_cap(c, X86_FEATURE_IBRS); 1002 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL); 1003 } 1004 1005 if (cpu_has(c, X86_FEATURE_AMD_IBPB)) 1006 set_cpu_cap(c, X86_FEATURE_IBPB); 1007 1008 if (cpu_has(c, X86_FEATURE_AMD_STIBP)) { 1009 set_cpu_cap(c, X86_FEATURE_STIBP); 1010 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL); 1011 } 1012 1013 if (cpu_has(c, X86_FEATURE_AMD_SSBD)) { 1014 set_cpu_cap(c, X86_FEATURE_SSBD); 1015 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL); 1016 clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD); 1017 } 1018 } 1019 1020 void get_cpu_cap(struct cpuinfo_x86 *c) 1021 { 1022 u32 eax, ebx, ecx, edx; 1023 1024 /* Intel-defined flags: level 0x00000001 */ 1025 if (c->cpuid_level >= 0x00000001) { 1026 cpuid(0x00000001, &eax, &ebx, &ecx, &edx); 1027 1028 c->x86_capability[CPUID_1_ECX] = ecx; 1029 c->x86_capability[CPUID_1_EDX] = edx; 1030 } 1031 1032 /* Thermal and Power Management Leaf: level 0x00000006 (eax) */ 1033 if (c->cpuid_level >= 0x00000006) 1034 c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006); 1035 1036 /* Additional Intel-defined flags: level 0x00000007 */ 1037 if (c->cpuid_level >= 0x00000007) { 1038 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx); 1039 c->x86_capability[CPUID_7_0_EBX] = ebx; 1040 c->x86_capability[CPUID_7_ECX] = ecx; 1041 c->x86_capability[CPUID_7_EDX] = edx; 1042 1043 /* Check valid sub-leaf index before accessing it */ 1044 if (eax >= 1) { 1045 cpuid_count(0x00000007, 1, &eax, &ebx, &ecx, &edx); 1046 c->x86_capability[CPUID_7_1_EAX] = eax; 1047 } 1048 } 1049 1050 /* Extended state features: level 0x0000000d */ 1051 if (c->cpuid_level >= 0x0000000d) { 1052 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx); 1053 1054 c->x86_capability[CPUID_D_1_EAX] = eax; 1055 } 1056 1057 /* 1058 * Check if extended CPUID leaves are implemented: Max extended 1059 * CPUID leaf must be in the 0x80000001-0x8000ffff range. 1060 */ 1061 eax = cpuid_eax(0x80000000); 1062 c->extended_cpuid_level = ((eax & 0xffff0000) == 0x80000000) ? eax : 0; 1063 1064 if (c->extended_cpuid_level >= 0x80000001) { 1065 cpuid(0x80000001, &eax, &ebx, &ecx, &edx); 1066 1067 c->x86_capability[CPUID_8000_0001_ECX] = ecx; 1068 c->x86_capability[CPUID_8000_0001_EDX] = edx; 1069 } 1070 1071 if (c->extended_cpuid_level >= 0x80000007) 1072 c->x86_power = cpuid_edx(0x80000007); 1073 1074 if (c->extended_cpuid_level >= 0x80000008) { 1075 cpuid(0x80000008, &eax, &ebx, &ecx, &edx); 1076 c->x86_capability[CPUID_8000_0008_EBX] = ebx; 1077 } 1078 1079 if (c->extended_cpuid_level >= 0x8000000a) 1080 c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a); 1081 1082 if (c->extended_cpuid_level >= 0x8000001f) 1083 c->x86_capability[CPUID_8000_001F_EAX] = cpuid_eax(0x8000001f); 1084 1085 if (c->extended_cpuid_level >= 0x80000021) 1086 c->x86_capability[CPUID_8000_0021_EAX] = cpuid_eax(0x80000021); 1087 1088 init_scattered_cpuid_features(c); 1089 init_speculation_control(c); 1090 1091 if (IS_ENABLED(CONFIG_X86_64) || cpu_has(c, X86_FEATURE_SEP)) 1092 set_cpu_cap(c, X86_FEATURE_SYSFAST32); 1093 1094 /* 1095 * Clear/Set all flags overridden by options, after probe. 1096 * This needs to happen each time we re-probe, which may happen 1097 * several times during CPU initialization. 1098 */ 1099 apply_forced_caps(c); 1100 } 1101 1102 void get_cpu_address_sizes(struct cpuinfo_x86 *c) 1103 { 1104 u32 eax, ebx, ecx, edx; 1105 1106 if (!cpu_has(c, X86_FEATURE_CPUID) || 1107 (c->extended_cpuid_level < 0x80000008)) { 1108 if (IS_ENABLED(CONFIG_X86_64)) { 1109 c->x86_clflush_size = 64; 1110 c->x86_phys_bits = 36; 1111 c->x86_virt_bits = 48; 1112 } else { 1113 c->x86_clflush_size = 32; 1114 c->x86_virt_bits = 32; 1115 c->x86_phys_bits = 32; 1116 1117 if (cpu_has(c, X86_FEATURE_PAE) || 1118 cpu_has(c, X86_FEATURE_PSE36)) 1119 c->x86_phys_bits = 36; 1120 } 1121 } else { 1122 cpuid(0x80000008, &eax, &ebx, &ecx, &edx); 1123 1124 c->x86_virt_bits = (eax >> 8) & 0xff; 1125 c->x86_phys_bits = eax & 0xff; 1126 1127 /* Provide a sane default if not enumerated: */ 1128 if (!c->x86_clflush_size) 1129 c->x86_clflush_size = 32; 1130 } 1131 1132 c->x86_cache_bits = c->x86_phys_bits; 1133 c->x86_cache_alignment = c->x86_clflush_size; 1134 } 1135 1136 static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c) 1137 { 1138 int i; 1139 1140 /* 1141 * First of all, decide if this is a 486 or higher 1142 * It's a 486 if we can modify the AC flag 1143 */ 1144 if (flag_is_changeable_p(X86_EFLAGS_AC)) 1145 c->x86 = 4; 1146 else 1147 c->x86 = 3; 1148 1149 for (i = 0; i < X86_VENDOR_NUM; i++) 1150 if (cpu_devs[i] && cpu_devs[i]->c_identify) { 1151 c->x86_vendor_id[0] = 0; 1152 cpu_devs[i]->c_identify(c); 1153 if (c->x86_vendor_id[0]) { 1154 get_cpu_vendor(c); 1155 break; 1156 } 1157 } 1158 } 1159 1160 #define NO_SPECULATION BIT(0) 1161 #define NO_MELTDOWN BIT(1) 1162 #define NO_SSB BIT(2) 1163 #define NO_L1TF BIT(3) 1164 #define NO_MDS BIT(4) 1165 #define MSBDS_ONLY BIT(5) 1166 #define NO_SWAPGS BIT(6) 1167 #define NO_ITLB_MULTIHIT BIT(7) 1168 #define NO_SPECTRE_V2 BIT(8) 1169 #define NO_MMIO BIT(9) 1170 #define NO_EIBRS_PBRSB BIT(10) 1171 #define NO_BHI BIT(11) 1172 1173 #define VULNWL(vendor, family, model, whitelist) \ 1174 X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, whitelist) 1175 1176 #define VULNWL_INTEL(vfm, whitelist) \ 1177 X86_MATCH_VFM(vfm, whitelist) 1178 1179 #define VULNWL_AMD(family, whitelist) \ 1180 VULNWL(AMD, family, X86_MODEL_ANY, whitelist) 1181 1182 #define VULNWL_HYGON(family, whitelist) \ 1183 VULNWL(HYGON, family, X86_MODEL_ANY, whitelist) 1184 1185 static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = { 1186 VULNWL(ANY, 4, X86_MODEL_ANY, NO_SPECULATION), 1187 VULNWL(CENTAUR, 5, X86_MODEL_ANY, NO_SPECULATION), 1188 VULNWL(INTEL, 5, X86_MODEL_ANY, NO_SPECULATION), 1189 VULNWL(NSC, 5, X86_MODEL_ANY, NO_SPECULATION), 1190 VULNWL(VORTEX, 5, X86_MODEL_ANY, NO_SPECULATION), 1191 VULNWL(VORTEX, 6, X86_MODEL_ANY, NO_SPECULATION), 1192 1193 /* Intel Family 6 */ 1194 VULNWL_INTEL(INTEL_TIGERLAKE, NO_MMIO), 1195 VULNWL_INTEL(INTEL_TIGERLAKE_L, NO_MMIO), 1196 VULNWL_INTEL(INTEL_ALDERLAKE, NO_MMIO), 1197 VULNWL_INTEL(INTEL_ALDERLAKE_L, NO_MMIO), 1198 1199 VULNWL_INTEL(INTEL_ATOM_SALTWELL, NO_SPECULATION | NO_ITLB_MULTIHIT), 1200 VULNWL_INTEL(INTEL_ATOM_SALTWELL_TABLET, NO_SPECULATION | NO_ITLB_MULTIHIT), 1201 VULNWL_INTEL(INTEL_ATOM_SALTWELL_MID, NO_SPECULATION | NO_ITLB_MULTIHIT), 1202 VULNWL_INTEL(INTEL_ATOM_BONNELL, NO_SPECULATION | NO_ITLB_MULTIHIT), 1203 VULNWL_INTEL(INTEL_ATOM_BONNELL_MID, NO_SPECULATION | NO_ITLB_MULTIHIT), 1204 1205 VULNWL_INTEL(INTEL_ATOM_SILVERMONT, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1206 VULNWL_INTEL(INTEL_ATOM_SILVERMONT_D, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1207 VULNWL_INTEL(INTEL_ATOM_SILVERMONT_MID, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1208 VULNWL_INTEL(INTEL_ATOM_AIRMONT, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1209 VULNWL_INTEL(INTEL_XEON_PHI_KNL, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1210 VULNWL_INTEL(INTEL_XEON_PHI_KNM, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1211 1212 VULNWL_INTEL(INTEL_CORE_YONAH, NO_SSB), 1213 1214 VULNWL_INTEL(INTEL_ATOM_SILVERMONT_MID2,NO_SSB | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | MSBDS_ONLY), 1215 VULNWL_INTEL(INTEL_ATOM_AIRMONT_NP, NO_SSB | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT), 1216 1217 VULNWL_INTEL(INTEL_ATOM_GOLDMONT, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO), 1218 VULNWL_INTEL(INTEL_ATOM_GOLDMONT_D, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO), 1219 VULNWL_INTEL(INTEL_ATOM_GOLDMONT_PLUS, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB), 1220 1221 /* 1222 * Technically, swapgs isn't serializing on AMD (despite it previously 1223 * being documented as such in the APM). But according to AMD, %gs is 1224 * updated non-speculatively, and the issuing of %gs-relative memory 1225 * operands will be blocked until the %gs update completes, which is 1226 * good enough for our purposes. 1227 */ 1228 1229 VULNWL_INTEL(INTEL_ATOM_TREMONT, NO_EIBRS_PBRSB), 1230 VULNWL_INTEL(INTEL_ATOM_TREMONT_L, NO_EIBRS_PBRSB), 1231 VULNWL_INTEL(INTEL_ATOM_TREMONT_D, NO_ITLB_MULTIHIT | NO_EIBRS_PBRSB), 1232 1233 /* AMD Family 0xf - 0x12 */ 1234 VULNWL_AMD(0x0f, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI), 1235 VULNWL_AMD(0x10, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI), 1236 VULNWL_AMD(0x11, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI), 1237 VULNWL_AMD(0x12, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI), 1238 1239 /* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */ 1240 VULNWL_AMD(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB | NO_BHI), 1241 VULNWL_HYGON(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB | NO_BHI), 1242 1243 /* Zhaoxin Family 7 */ 1244 VULNWL(CENTAUR, 7, X86_MODEL_ANY, NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO | NO_BHI), 1245 VULNWL(ZHAOXIN, 7, X86_MODEL_ANY, NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO | NO_BHI), 1246 {} 1247 }; 1248 1249 #define VULNBL(vendor, family, model, blacklist) \ 1250 X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, blacklist) 1251 1252 #define VULNBL_INTEL_STEPS(vfm, max_stepping, issues) \ 1253 X86_MATCH_VFM_STEPS(vfm, X86_STEP_MIN, max_stepping, issues) 1254 1255 #define VULNBL_AMD(family, blacklist) \ 1256 VULNBL(AMD, family, X86_MODEL_ANY, blacklist) 1257 1258 #define VULNBL_HYGON(family, blacklist) \ 1259 VULNBL(HYGON, family, X86_MODEL_ANY, blacklist) 1260 1261 #define SRBDS BIT(0) 1262 /* CPU is affected by X86_BUG_MMIO_STALE_DATA */ 1263 #define MMIO BIT(1) 1264 /* CPU is affected by Shared Buffers Data Sampling (SBDS), a variant of X86_BUG_MMIO_STALE_DATA */ 1265 #define MMIO_SBDS BIT(2) 1266 /* CPU is affected by RETbleed, speculating where you would not expect it */ 1267 #define RETBLEED BIT(3) 1268 /* CPU is affected by SMT (cross-thread) return predictions */ 1269 #define SMT_RSB BIT(4) 1270 /* CPU is affected by SRSO */ 1271 #define SRSO BIT(5) 1272 /* CPU is affected by GDS */ 1273 #define GDS BIT(6) 1274 /* CPU is affected by Register File Data Sampling */ 1275 #define RFDS BIT(7) 1276 /* CPU is affected by Indirect Target Selection */ 1277 #define ITS BIT(8) 1278 /* CPU is affected by Indirect Target Selection, but guest-host isolation is not affected */ 1279 #define ITS_NATIVE_ONLY BIT(9) 1280 /* CPU is affected by Transient Scheduler Attacks */ 1281 #define TSA BIT(10) 1282 /* CPU is affected by VMSCAPE */ 1283 #define VMSCAPE BIT(11) 1284 1285 static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = { 1286 VULNBL_INTEL_STEPS(INTEL_SANDYBRIDGE_X, X86_STEP_MAX, VMSCAPE), 1287 VULNBL_INTEL_STEPS(INTEL_SANDYBRIDGE, X86_STEP_MAX, VMSCAPE), 1288 VULNBL_INTEL_STEPS(INTEL_IVYBRIDGE_X, X86_STEP_MAX, VMSCAPE), 1289 VULNBL_INTEL_STEPS(INTEL_IVYBRIDGE, X86_STEP_MAX, SRBDS | VMSCAPE), 1290 VULNBL_INTEL_STEPS(INTEL_HASWELL, X86_STEP_MAX, SRBDS | VMSCAPE), 1291 VULNBL_INTEL_STEPS(INTEL_HASWELL_L, X86_STEP_MAX, SRBDS | VMSCAPE), 1292 VULNBL_INTEL_STEPS(INTEL_HASWELL_G, X86_STEP_MAX, SRBDS | VMSCAPE), 1293 VULNBL_INTEL_STEPS(INTEL_HASWELL_X, X86_STEP_MAX, MMIO | VMSCAPE), 1294 VULNBL_INTEL_STEPS(INTEL_BROADWELL_D, X86_STEP_MAX, MMIO | VMSCAPE), 1295 VULNBL_INTEL_STEPS(INTEL_BROADWELL_X, X86_STEP_MAX, MMIO | VMSCAPE), 1296 VULNBL_INTEL_STEPS(INTEL_BROADWELL_G, X86_STEP_MAX, SRBDS | VMSCAPE), 1297 VULNBL_INTEL_STEPS(INTEL_BROADWELL, X86_STEP_MAX, SRBDS | VMSCAPE), 1298 VULNBL_INTEL_STEPS(INTEL_SKYLAKE_X, 0x5, MMIO | RETBLEED | GDS | VMSCAPE), 1299 VULNBL_INTEL_STEPS(INTEL_SKYLAKE_X, X86_STEP_MAX, MMIO | RETBLEED | GDS | ITS | VMSCAPE), 1300 VULNBL_INTEL_STEPS(INTEL_SKYLAKE_L, X86_STEP_MAX, MMIO | RETBLEED | GDS | SRBDS | VMSCAPE), 1301 VULNBL_INTEL_STEPS(INTEL_SKYLAKE, X86_STEP_MAX, MMIO | RETBLEED | GDS | SRBDS | VMSCAPE), 1302 VULNBL_INTEL_STEPS(INTEL_KABYLAKE_L, 0xb, MMIO | RETBLEED | GDS | SRBDS | VMSCAPE), 1303 VULNBL_INTEL_STEPS(INTEL_KABYLAKE_L, X86_STEP_MAX, MMIO | RETBLEED | GDS | SRBDS | ITS | VMSCAPE), 1304 VULNBL_INTEL_STEPS(INTEL_KABYLAKE, 0xc, MMIO | RETBLEED | GDS | SRBDS | VMSCAPE), 1305 VULNBL_INTEL_STEPS(INTEL_KABYLAKE, X86_STEP_MAX, MMIO | RETBLEED | GDS | SRBDS | ITS | VMSCAPE), 1306 VULNBL_INTEL_STEPS(INTEL_CANNONLAKE_L, X86_STEP_MAX, RETBLEED | VMSCAPE), 1307 VULNBL_INTEL_STEPS(INTEL_ICELAKE_L, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY), 1308 VULNBL_INTEL_STEPS(INTEL_ICELAKE_D, X86_STEP_MAX, MMIO | GDS | ITS | ITS_NATIVE_ONLY), 1309 VULNBL_INTEL_STEPS(INTEL_ICELAKE_X, X86_STEP_MAX, MMIO | GDS | ITS | ITS_NATIVE_ONLY), 1310 VULNBL_INTEL_STEPS(INTEL_COMETLAKE, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | VMSCAPE), 1311 VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L, 0x0, MMIO | RETBLEED | ITS | VMSCAPE), 1312 VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | VMSCAPE), 1313 VULNBL_INTEL_STEPS(INTEL_TIGERLAKE_L, X86_STEP_MAX, GDS | ITS | ITS_NATIVE_ONLY), 1314 VULNBL_INTEL_STEPS(INTEL_TIGERLAKE, X86_STEP_MAX, GDS | ITS | ITS_NATIVE_ONLY), 1315 VULNBL_INTEL_STEPS(INTEL_LAKEFIELD, X86_STEP_MAX, MMIO | MMIO_SBDS | RETBLEED), 1316 VULNBL_INTEL_STEPS(INTEL_ROCKETLAKE, X86_STEP_MAX, MMIO | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY), 1317 VULNBL_INTEL_STEPS(INTEL_ALDERLAKE, X86_STEP_MAX, RFDS | VMSCAPE), 1318 VULNBL_INTEL_STEPS(INTEL_ALDERLAKE_L, X86_STEP_MAX, RFDS | VMSCAPE), 1319 VULNBL_INTEL_STEPS(INTEL_RAPTORLAKE, X86_STEP_MAX, RFDS | VMSCAPE), 1320 VULNBL_INTEL_STEPS(INTEL_RAPTORLAKE_P, X86_STEP_MAX, RFDS | VMSCAPE), 1321 VULNBL_INTEL_STEPS(INTEL_RAPTORLAKE_S, X86_STEP_MAX, RFDS | VMSCAPE), 1322 VULNBL_INTEL_STEPS(INTEL_METEORLAKE_L, X86_STEP_MAX, VMSCAPE), 1323 VULNBL_INTEL_STEPS(INTEL_ARROWLAKE_H, X86_STEP_MAX, VMSCAPE), 1324 VULNBL_INTEL_STEPS(INTEL_ARROWLAKE, X86_STEP_MAX, VMSCAPE), 1325 VULNBL_INTEL_STEPS(INTEL_ARROWLAKE_U, X86_STEP_MAX, VMSCAPE), 1326 VULNBL_INTEL_STEPS(INTEL_LUNARLAKE_M, X86_STEP_MAX, VMSCAPE), 1327 VULNBL_INTEL_STEPS(INTEL_SAPPHIRERAPIDS_X, X86_STEP_MAX, VMSCAPE), 1328 VULNBL_INTEL_STEPS(INTEL_GRANITERAPIDS_X, X86_STEP_MAX, VMSCAPE), 1329 VULNBL_INTEL_STEPS(INTEL_EMERALDRAPIDS_X, X86_STEP_MAX, VMSCAPE), 1330 VULNBL_INTEL_STEPS(INTEL_ATOM_GRACEMONT, X86_STEP_MAX, RFDS | VMSCAPE), 1331 VULNBL_INTEL_STEPS(INTEL_ATOM_TREMONT, X86_STEP_MAX, MMIO | MMIO_SBDS | RFDS), 1332 VULNBL_INTEL_STEPS(INTEL_ATOM_TREMONT_D, X86_STEP_MAX, MMIO | RFDS), 1333 VULNBL_INTEL_STEPS(INTEL_ATOM_TREMONT_L, X86_STEP_MAX, MMIO | MMIO_SBDS | RFDS), 1334 VULNBL_INTEL_STEPS(INTEL_ATOM_GOLDMONT, X86_STEP_MAX, RFDS), 1335 VULNBL_INTEL_STEPS(INTEL_ATOM_GOLDMONT_D, X86_STEP_MAX, RFDS), 1336 VULNBL_INTEL_STEPS(INTEL_ATOM_GOLDMONT_PLUS, X86_STEP_MAX, RFDS), 1337 VULNBL_INTEL_STEPS(INTEL_ATOM_CRESTMONT_X, X86_STEP_MAX, VMSCAPE), 1338 1339 VULNBL_AMD(0x15, RETBLEED), 1340 VULNBL_AMD(0x16, RETBLEED), 1341 VULNBL_AMD(0x17, RETBLEED | SMT_RSB | SRSO | VMSCAPE), 1342 VULNBL_HYGON(0x18, RETBLEED | SMT_RSB | SRSO | VMSCAPE), 1343 VULNBL_AMD(0x19, SRSO | TSA | VMSCAPE), 1344 VULNBL_AMD(0x1a, SRSO | VMSCAPE), 1345 {} 1346 }; 1347 1348 static bool __init cpu_matches(const struct x86_cpu_id *table, unsigned long which) 1349 { 1350 const struct x86_cpu_id *m = x86_match_cpu(table); 1351 1352 return m && !!(m->driver_data & which); 1353 } 1354 1355 u64 x86_read_arch_cap_msr(void) 1356 { 1357 u64 x86_arch_cap_msr = 0; 1358 1359 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) 1360 rdmsrq(MSR_IA32_ARCH_CAPABILITIES, x86_arch_cap_msr); 1361 1362 return x86_arch_cap_msr; 1363 } 1364 1365 static bool arch_cap_mmio_immune(u64 x86_arch_cap_msr) 1366 { 1367 return (x86_arch_cap_msr & ARCH_CAP_FBSDP_NO && 1368 x86_arch_cap_msr & ARCH_CAP_PSDP_NO && 1369 x86_arch_cap_msr & ARCH_CAP_SBDR_SSDP_NO); 1370 } 1371 1372 static bool __init vulnerable_to_rfds(u64 x86_arch_cap_msr) 1373 { 1374 /* The "immunity" bit trumps everything else: */ 1375 if (x86_arch_cap_msr & ARCH_CAP_RFDS_NO) 1376 return false; 1377 1378 /* 1379 * VMMs set ARCH_CAP_RFDS_CLEAR for processors not in the blacklist to 1380 * indicate that mitigation is needed because guest is running on a 1381 * vulnerable hardware or may migrate to such hardware: 1382 */ 1383 if (x86_arch_cap_msr & ARCH_CAP_RFDS_CLEAR) 1384 return true; 1385 1386 /* Only consult the blacklist when there is no enumeration: */ 1387 if (!cpu_matches(cpu_vuln_blacklist, RFDS)) 1388 return false; 1389 1390 /* 1391 * ADL and RPL are affected only if they have Atom CPUs. Hybrids have 1392 * both Core and Atom CPUs. Mark unaffected when Atom CPUs are not 1393 * present. 1394 */ 1395 if ((boot_cpu_data.x86_model == 0x97 || 1396 boot_cpu_data.x86_model == 0xB7) && 1397 boot_cpu_data.topo.intel_type != INTEL_CPU_TYPE_ATOM && 1398 !boot_cpu_has(X86_FEATURE_HYBRID_CPU)) 1399 return false; 1400 1401 return true; 1402 } 1403 1404 static bool __init vulnerable_to_its(u64 x86_arch_cap_msr) 1405 { 1406 /* The "immunity" bit trumps everything else: */ 1407 if (x86_arch_cap_msr & ARCH_CAP_ITS_NO) 1408 return false; 1409 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) 1410 return false; 1411 1412 /* None of the affected CPUs have BHI_CTRL */ 1413 if (boot_cpu_has(X86_FEATURE_BHI_CTRL)) 1414 return false; 1415 1416 /* 1417 * If a VMM did not expose ITS_NO, assume that a guest could 1418 * be running on a vulnerable hardware or may migrate to such 1419 * hardware. 1420 */ 1421 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) 1422 return true; 1423 1424 if (cpu_matches(cpu_vuln_blacklist, ITS)) 1425 return true; 1426 1427 return false; 1428 } 1429 1430 static struct x86_cpu_id cpu_latest_microcode[] = { 1431 #include "microcode/intel-ucode-defs.h" 1432 {} 1433 }; 1434 1435 static bool __init cpu_has_old_microcode(void) 1436 { 1437 const struct x86_cpu_id *m = x86_match_cpu(cpu_latest_microcode); 1438 1439 /* Give unknown CPUs a pass: */ 1440 if (!m) { 1441 /* Intel CPUs should be in the list. Warn if not: */ 1442 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) 1443 pr_info("x86/CPU: Model not found in latest microcode list\n"); 1444 return false; 1445 } 1446 1447 /* 1448 * Hosts usually lie to guests with a super high microcode 1449 * version. Just ignore what hosts tell guests: 1450 */ 1451 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) 1452 return false; 1453 1454 /* Consider all debug microcode to be old: */ 1455 if (boot_cpu_data.microcode & BIT(31)) 1456 return true; 1457 1458 /* Give new microcode a pass: */ 1459 if (boot_cpu_data.microcode >= m->driver_data) 1460 return false; 1461 1462 /* Uh oh, too old: */ 1463 return true; 1464 } 1465 1466 static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c) 1467 { 1468 u64 x86_arch_cap_msr = x86_read_arch_cap_msr(); 1469 1470 if (cpu_has_old_microcode()) { 1471 pr_warn("x86/CPU: Running old microcode\n"); 1472 setup_force_cpu_bug(X86_BUG_OLD_MICROCODE); 1473 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); 1474 } 1475 1476 /* Set ITLB_MULTIHIT bug if cpu is not in the whitelist and not mitigated */ 1477 if (!cpu_matches(cpu_vuln_whitelist, NO_ITLB_MULTIHIT) && 1478 !(x86_arch_cap_msr & ARCH_CAP_PSCHANGE_MC_NO)) 1479 setup_force_cpu_bug(X86_BUG_ITLB_MULTIHIT); 1480 1481 if (cpu_matches(cpu_vuln_whitelist, NO_SPECULATION)) 1482 return; 1483 1484 setup_force_cpu_bug(X86_BUG_SPECTRE_V1); 1485 1486 if (!cpu_matches(cpu_vuln_whitelist, NO_SPECTRE_V2)) { 1487 setup_force_cpu_bug(X86_BUG_SPECTRE_V2); 1488 setup_force_cpu_bug(X86_BUG_SPECTRE_V2_USER); 1489 } 1490 1491 if (!cpu_matches(cpu_vuln_whitelist, NO_SSB) && 1492 !(x86_arch_cap_msr & ARCH_CAP_SSB_NO) && 1493 !cpu_has(c, X86_FEATURE_AMD_SSB_NO)) 1494 setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS); 1495 1496 /* 1497 * AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature 1498 * flag and protect from vendor-specific bugs via the whitelist. 1499 * 1500 * Don't use AutoIBRS when SNP is enabled because it degrades host 1501 * userspace indirect branch performance. 1502 */ 1503 if ((x86_arch_cap_msr & ARCH_CAP_IBRS_ALL) || 1504 (cpu_has(c, X86_FEATURE_AUTOIBRS) && 1505 !cpu_feature_enabled(X86_FEATURE_SEV_SNP))) { 1506 setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED); 1507 if (!cpu_matches(cpu_vuln_whitelist, NO_EIBRS_PBRSB) && 1508 !(x86_arch_cap_msr & ARCH_CAP_PBRSB_NO)) 1509 setup_force_cpu_bug(X86_BUG_EIBRS_PBRSB); 1510 } 1511 1512 if (!cpu_matches(cpu_vuln_whitelist, NO_MDS) && 1513 !(x86_arch_cap_msr & ARCH_CAP_MDS_NO)) { 1514 setup_force_cpu_bug(X86_BUG_MDS); 1515 if (cpu_matches(cpu_vuln_whitelist, MSBDS_ONLY)) 1516 setup_force_cpu_bug(X86_BUG_MSBDS_ONLY); 1517 } 1518 1519 if (!cpu_matches(cpu_vuln_whitelist, NO_SWAPGS)) 1520 setup_force_cpu_bug(X86_BUG_SWAPGS); 1521 1522 /* 1523 * When the CPU is not mitigated for TAA (TAA_NO=0) set TAA bug when: 1524 * - TSX is supported or 1525 * - TSX_CTRL is present 1526 * 1527 * TSX_CTRL check is needed for cases when TSX could be disabled before 1528 * the kernel boot e.g. kexec. 1529 * TSX_CTRL check alone is not sufficient for cases when the microcode 1530 * update is not present or running as guest that don't get TSX_CTRL. 1531 */ 1532 if (!(x86_arch_cap_msr & ARCH_CAP_TAA_NO) && 1533 (cpu_has(c, X86_FEATURE_RTM) || 1534 (x86_arch_cap_msr & ARCH_CAP_TSX_CTRL_MSR))) 1535 setup_force_cpu_bug(X86_BUG_TAA); 1536 1537 /* 1538 * SRBDS affects CPUs which support RDRAND or RDSEED and are listed 1539 * in the vulnerability blacklist. 1540 * 1541 * Some of the implications and mitigation of Shared Buffers Data 1542 * Sampling (SBDS) are similar to SRBDS. Give SBDS same treatment as 1543 * SRBDS. 1544 */ 1545 if ((cpu_has(c, X86_FEATURE_RDRAND) || 1546 cpu_has(c, X86_FEATURE_RDSEED)) && 1547 cpu_matches(cpu_vuln_blacklist, SRBDS | MMIO_SBDS)) 1548 setup_force_cpu_bug(X86_BUG_SRBDS); 1549 1550 /* 1551 * Processor MMIO Stale Data bug enumeration 1552 * 1553 * Affected CPU list is generally enough to enumerate the vulnerability, 1554 * but for virtualization case check for ARCH_CAP MSR bits also, VMM may 1555 * not want the guest to enumerate the bug. 1556 */ 1557 if (!arch_cap_mmio_immune(x86_arch_cap_msr)) { 1558 if (cpu_matches(cpu_vuln_blacklist, MMIO)) 1559 setup_force_cpu_bug(X86_BUG_MMIO_STALE_DATA); 1560 } 1561 1562 if (!cpu_has(c, X86_FEATURE_BTC_NO)) { 1563 if (cpu_matches(cpu_vuln_blacklist, RETBLEED) || (x86_arch_cap_msr & ARCH_CAP_RSBA)) 1564 setup_force_cpu_bug(X86_BUG_RETBLEED); 1565 } 1566 1567 if (cpu_matches(cpu_vuln_blacklist, SMT_RSB)) 1568 setup_force_cpu_bug(X86_BUG_SMT_RSB); 1569 1570 if (!cpu_has(c, X86_FEATURE_SRSO_NO)) { 1571 if (cpu_matches(cpu_vuln_blacklist, SRSO)) 1572 setup_force_cpu_bug(X86_BUG_SRSO); 1573 } 1574 1575 /* 1576 * Check if CPU is vulnerable to GDS. If running in a virtual machine on 1577 * an affected processor, the VMM may have disabled the use of GATHER by 1578 * disabling AVX2. The only way to do this in HW is to clear XCR0[2], 1579 * which means that AVX will be disabled. 1580 */ 1581 if (cpu_matches(cpu_vuln_blacklist, GDS) && !(x86_arch_cap_msr & ARCH_CAP_GDS_NO) && 1582 boot_cpu_has(X86_FEATURE_AVX)) 1583 setup_force_cpu_bug(X86_BUG_GDS); 1584 1585 if (vulnerable_to_rfds(x86_arch_cap_msr)) 1586 setup_force_cpu_bug(X86_BUG_RFDS); 1587 1588 /* 1589 * Intel parts with eIBRS are vulnerable to BHI attacks. Parts with 1590 * BHI_NO still need to use the BHI mitigation to prevent Intra-mode 1591 * attacks. When virtualized, eIBRS could be hidden, assume vulnerable. 1592 */ 1593 if (!cpu_matches(cpu_vuln_whitelist, NO_BHI) && 1594 (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED) || 1595 boot_cpu_has(X86_FEATURE_HYPERVISOR))) 1596 setup_force_cpu_bug(X86_BUG_BHI); 1597 1598 if (cpu_has(c, X86_FEATURE_AMD_IBPB) && !cpu_has(c, X86_FEATURE_AMD_IBPB_RET)) 1599 setup_force_cpu_bug(X86_BUG_IBPB_NO_RET); 1600 1601 if (vulnerable_to_its(x86_arch_cap_msr)) { 1602 setup_force_cpu_bug(X86_BUG_ITS); 1603 if (cpu_matches(cpu_vuln_blacklist, ITS_NATIVE_ONLY)) 1604 setup_force_cpu_bug(X86_BUG_ITS_NATIVE_ONLY); 1605 } 1606 1607 if (c->x86_vendor == X86_VENDOR_AMD) { 1608 if (!cpu_has(c, X86_FEATURE_TSA_SQ_NO) || 1609 !cpu_has(c, X86_FEATURE_TSA_L1_NO)) { 1610 if (cpu_matches(cpu_vuln_blacklist, TSA) || 1611 /* Enable bug on Zen guests to allow for live migration. */ 1612 (cpu_has(c, X86_FEATURE_HYPERVISOR) && cpu_has(c, X86_FEATURE_ZEN))) 1613 setup_force_cpu_bug(X86_BUG_TSA); 1614 } 1615 } 1616 1617 /* 1618 * Set the bug only on bare-metal. A nested hypervisor should already be 1619 * deploying IBPB to isolate itself from nested guests. 1620 */ 1621 if (cpu_matches(cpu_vuln_blacklist, VMSCAPE) && 1622 !boot_cpu_has(X86_FEATURE_HYPERVISOR)) 1623 setup_force_cpu_bug(X86_BUG_VMSCAPE); 1624 1625 if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN)) 1626 return; 1627 1628 /* Rogue Data Cache Load? No! */ 1629 if (x86_arch_cap_msr & ARCH_CAP_RDCL_NO) 1630 return; 1631 1632 setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN); 1633 1634 if (cpu_matches(cpu_vuln_whitelist, NO_L1TF)) 1635 return; 1636 1637 setup_force_cpu_bug(X86_BUG_L1TF); 1638 } 1639 1640 /* 1641 * The NOPL instruction is supposed to exist on all CPUs of family >= 6; 1642 * unfortunately, that's not true in practice because of early VIA 1643 * chips and (more importantly) broken virtualizers that are not easy 1644 * to detect. In the latter case it doesn't even *fail* reliably, so 1645 * probing for it doesn't even work. Disable it completely on 32-bit 1646 * unless we can find a reliable way to detect all the broken cases. 1647 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has(). 1648 */ 1649 static void detect_nopl(void) 1650 { 1651 #ifdef CONFIG_X86_32 1652 setup_clear_cpu_cap(X86_FEATURE_NOPL); 1653 #else 1654 setup_force_cpu_cap(X86_FEATURE_NOPL); 1655 #endif 1656 } 1657 1658 static inline bool parse_set_clear_cpuid(char *arg, bool set) 1659 { 1660 char *opt; 1661 int taint = 0; 1662 1663 while (arg) { 1664 bool found = false; 1665 unsigned int bit; 1666 1667 opt = strsep(&arg, ","); 1668 1669 /* 1670 * Handle naked numbers first for feature flags which don't 1671 * have names. It doesn't make sense for a bug not to have a 1672 * name so don't handle bug flags here. 1673 */ 1674 if (!kstrtouint(opt, 10, &bit)) { 1675 if (bit < NCAPINTS * 32) { 1676 1677 if (set) { 1678 pr_warn("setcpuid: force-enabling CPU feature flag:"); 1679 setup_force_cpu_cap(bit); 1680 } else { 1681 pr_warn("clearcpuid: force-disabling CPU feature flag:"); 1682 setup_clear_cpu_cap(bit); 1683 } 1684 /* empty-string, i.e., ""-defined feature flags */ 1685 if (!x86_cap_flags[bit]) 1686 pr_cont(" %d:%d\n", bit >> 5, bit & 31); 1687 else 1688 pr_cont(" %s\n", x86_cap_flags[bit]); 1689 1690 taint++; 1691 } 1692 /* 1693 * The assumption is that there are no feature names with only 1694 * numbers in the name thus go to the next argument. 1695 */ 1696 continue; 1697 } 1698 1699 for (bit = 0; bit < 32 * (NCAPINTS + NBUGINTS); bit++) { 1700 const char *flag; 1701 const char *kind; 1702 1703 if (bit < 32 * NCAPINTS) { 1704 flag = x86_cap_flags[bit]; 1705 kind = "feature"; 1706 } else { 1707 kind = "bug"; 1708 flag = x86_bug_flags[bit - (32 * NCAPINTS)]; 1709 } 1710 1711 if (!flag) 1712 continue; 1713 1714 if (strcmp(flag, opt)) 1715 continue; 1716 1717 if (set) { 1718 pr_warn("setcpuid: force-enabling CPU %s flag: %s\n", 1719 kind, flag); 1720 setup_force_cpu_cap(bit); 1721 } else { 1722 pr_warn("clearcpuid: force-disabling CPU %s flag: %s\n", 1723 kind, flag); 1724 setup_clear_cpu_cap(bit); 1725 } 1726 taint++; 1727 found = true; 1728 break; 1729 } 1730 1731 if (!found) 1732 pr_warn("%s: unknown CPU flag: %s", set ? "setcpuid" : "clearcpuid", opt); 1733 } 1734 1735 return taint; 1736 } 1737 1738 1739 /* 1740 * We parse cpu parameters early because fpu__init_system() is executed 1741 * before parse_early_param(). 1742 */ 1743 static void __init cpu_parse_early_param(void) 1744 { 1745 bool cpuid_taint = false; 1746 char arg[128]; 1747 int arglen; 1748 1749 #ifdef CONFIG_X86_32 1750 if (cmdline_find_option_bool(boot_command_line, "nofxsr")) 1751 setup_clear_cpu_cap(X86_FEATURE_FXSR); 1752 #endif 1753 1754 if (cmdline_find_option_bool(boot_command_line, "noxsave")) 1755 setup_clear_cpu_cap(X86_FEATURE_XSAVE); 1756 1757 if (cmdline_find_option_bool(boot_command_line, "noxsaveopt")) 1758 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT); 1759 1760 if (cmdline_find_option_bool(boot_command_line, "noxsaves")) 1761 setup_clear_cpu_cap(X86_FEATURE_XSAVES); 1762 1763 if (cmdline_find_option_bool(boot_command_line, "nousershstk")) 1764 setup_clear_cpu_cap(X86_FEATURE_USER_SHSTK); 1765 1766 /* Minimize the gap between FRED is available and available but disabled. */ 1767 arglen = cmdline_find_option(boot_command_line, "fred", arg, sizeof(arg)); 1768 if (arglen == 3 && !strncmp(arg, "off", 3)) 1769 setup_clear_cpu_cap(X86_FEATURE_FRED); 1770 1771 arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg)); 1772 if (arglen > 0) 1773 cpuid_taint |= parse_set_clear_cpuid(arg, false); 1774 1775 arglen = cmdline_find_option(boot_command_line, "setcpuid", arg, sizeof(arg)); 1776 if (arglen > 0) 1777 cpuid_taint |= parse_set_clear_cpuid(arg, true); 1778 1779 if (cpuid_taint) { 1780 pr_warn("!!! setcpuid=/clearcpuid= in use, this is for TESTING ONLY, may break things horribly. Tainting kernel.\n"); 1781 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); 1782 } 1783 } 1784 1785 /* 1786 * Do minimum CPU detection early. 1787 * Fields really needed: vendor, cpuid_level, family, model, mask, 1788 * cache alignment. 1789 * The others are not touched to avoid unwanted side effects. 1790 * 1791 * WARNING: this function is only called on the boot CPU. Don't add code 1792 * here that is supposed to run on all CPUs. 1793 */ 1794 static void __init early_identify_cpu(struct cpuinfo_x86 *c) 1795 { 1796 memset(&c->x86_capability, 0, sizeof(c->x86_capability)); 1797 memset(&c->cpuid, 0, sizeof(c->cpuid)); 1798 c->extended_cpuid_level = 0; 1799 1800 if (!cpuid_feature()) 1801 identify_cpu_without_cpuid(c); 1802 1803 /* cyrix could have cpuid enabled via c_identify()*/ 1804 if (cpuid_feature()) { 1805 cpuid_scan_cpu(c); 1806 cpu_detect(c); 1807 get_cpu_vendor(c); 1808 intel_unlock_cpuid_leafs(c); 1809 get_cpu_cap(c); 1810 setup_force_cpu_cap(X86_FEATURE_CPUID); 1811 get_cpu_address_sizes(c); 1812 cpu_parse_early_param(); 1813 1814 cpu_init_topology(c); 1815 1816 if (this_cpu->c_early_init) 1817 this_cpu->c_early_init(c); 1818 1819 c->cpu_index = 0; 1820 filter_cpuid_features(c, false); 1821 check_cpufeature_deps(c); 1822 1823 if (this_cpu->c_bsp_init) 1824 this_cpu->c_bsp_init(c); 1825 } else { 1826 setup_clear_cpu_cap(X86_FEATURE_CPUID); 1827 get_cpu_address_sizes(c); 1828 cpu_init_topology(c); 1829 } 1830 1831 setup_force_cpu_cap(X86_FEATURE_ALWAYS); 1832 1833 cpu_set_bug_bits(c); 1834 1835 sld_setup(c); 1836 1837 #ifdef CONFIG_X86_32 1838 /* 1839 * Regardless of whether PCID is enumerated, the SDM says 1840 * that it can't be enabled in 32-bit mode. 1841 */ 1842 setup_clear_cpu_cap(X86_FEATURE_PCID); 1843 1844 /* 1845 * Never use SYSCALL on a 32-bit kernel 1846 */ 1847 setup_clear_cpu_cap(X86_FEATURE_SYSCALL32); 1848 #endif 1849 1850 /* 1851 * Later in the boot process pgtable_l5_enabled() relies on 1852 * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not 1853 * enabled by this point we need to clear the feature bit to avoid 1854 * false-positives at the later stage. 1855 * 1856 * pgtable_l5_enabled() can be false here for several reasons: 1857 * - 5-level paging is disabled compile-time; 1858 * - it's 32-bit kernel; 1859 * - machine doesn't support 5-level paging; 1860 * - user specified 'no5lvl' in kernel command line. 1861 */ 1862 if (!pgtable_l5_enabled()) 1863 setup_clear_cpu_cap(X86_FEATURE_LA57); 1864 1865 detect_nopl(); 1866 mca_bsp_init(c); 1867 } 1868 1869 void __init init_cpu_devs(void) 1870 { 1871 const struct cpu_dev *const *cdev; 1872 int count = 0; 1873 1874 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) { 1875 const struct cpu_dev *cpudev = *cdev; 1876 1877 if (count >= X86_VENDOR_NUM) 1878 break; 1879 cpu_devs[count] = cpudev; 1880 count++; 1881 } 1882 } 1883 1884 void __init early_cpu_init(void) 1885 { 1886 #ifdef CONFIG_PROCESSOR_SELECT 1887 unsigned int i, j; 1888 1889 pr_info("KERNEL supported cpus:\n"); 1890 #endif 1891 1892 init_cpu_devs(); 1893 1894 #ifdef CONFIG_PROCESSOR_SELECT 1895 for (i = 0; i < X86_VENDOR_NUM && cpu_devs[i]; i++) { 1896 for (j = 0; j < 2; j++) { 1897 if (!cpu_devs[i]->c_ident[j]) 1898 continue; 1899 pr_info(" %s %s\n", cpu_devs[i]->c_vendor, 1900 cpu_devs[i]->c_ident[j]); 1901 } 1902 } 1903 #endif 1904 1905 early_identify_cpu(&boot_cpu_data); 1906 } 1907 1908 static bool detect_null_seg_behavior(void) 1909 { 1910 /* 1911 * Empirically, writing zero to a segment selector on AMD does 1912 * not clear the base, whereas writing zero to a segment 1913 * selector on Intel does clear the base. Intel's behavior 1914 * allows slightly faster context switches in the common case 1915 * where GS is unused by the prev and next threads. 1916 * 1917 * Since neither vendor documents this anywhere that I can see, 1918 * detect it directly instead of hard-coding the choice by 1919 * vendor. 1920 * 1921 * I've designated AMD's behavior as the "bug" because it's 1922 * counterintuitive and less friendly. 1923 */ 1924 1925 unsigned long old_base, tmp; 1926 rdmsrq(MSR_FS_BASE, old_base); 1927 wrmsrq(MSR_FS_BASE, 1); 1928 loadsegment(fs, 0); 1929 rdmsrq(MSR_FS_BASE, tmp); 1930 wrmsrq(MSR_FS_BASE, old_base); 1931 return tmp == 0; 1932 } 1933 1934 void check_null_seg_clears_base(struct cpuinfo_x86 *c) 1935 { 1936 /* BUG_NULL_SEG is only relevant with 64bit userspace */ 1937 if (!IS_ENABLED(CONFIG_X86_64)) 1938 return; 1939 1940 if (cpu_has(c, X86_FEATURE_NULL_SEL_CLR_BASE)) 1941 return; 1942 1943 /* 1944 * CPUID bit above wasn't set. If this kernel is still running 1945 * as a HV guest, then the HV has decided not to advertize 1946 * that CPUID bit for whatever reason. For example, one 1947 * member of the migration pool might be vulnerable. Which 1948 * means, the bug is present: set the BUG flag and return. 1949 */ 1950 if (cpu_has(c, X86_FEATURE_HYPERVISOR)) { 1951 set_cpu_bug(c, X86_BUG_NULL_SEG); 1952 return; 1953 } 1954 1955 /* 1956 * Zen2 CPUs also have this behaviour, but no CPUID bit. 1957 * 0x18 is the respective family for Hygon. 1958 */ 1959 if ((c->x86 == 0x17 || c->x86 == 0x18) && 1960 detect_null_seg_behavior()) 1961 return; 1962 1963 /* All the remaining ones are affected */ 1964 set_cpu_bug(c, X86_BUG_NULL_SEG); 1965 } 1966 1967 static void generic_identify(struct cpuinfo_x86 *c) 1968 { 1969 c->extended_cpuid_level = 0; 1970 1971 if (!cpuid_feature()) 1972 identify_cpu_without_cpuid(c); 1973 1974 /* cyrix could have cpuid enabled via c_identify()*/ 1975 if (!cpuid_feature()) 1976 return; 1977 1978 cpuid_scan_cpu(c); 1979 cpu_detect(c); 1980 get_cpu_vendor(c); 1981 intel_unlock_cpuid_leafs(c); 1982 get_cpu_cap(c); 1983 1984 get_cpu_address_sizes(c); 1985 1986 get_model_name(c); /* Default name */ 1987 1988 /* 1989 * ESPFIX is a strange bug. All real CPUs have it. Paravirt 1990 * systems that run Linux at CPL > 0 may or may not have the 1991 * issue, but, even if they have the issue, there's absolutely 1992 * nothing we can do about it because we can't use the real IRET 1993 * instruction. 1994 * 1995 * NB: For the time being, only 32-bit kernels support 1996 * X86_BUG_ESPFIX as such. 64-bit kernels directly choose 1997 * whether to apply espfix using paravirt hooks. If any 1998 * non-paravirt system ever shows up that does *not* have the 1999 * ESPFIX issue, we can change this. 2000 */ 2001 #ifdef CONFIG_X86_32 2002 set_cpu_bug(c, X86_BUG_ESPFIX); 2003 #endif 2004 } 2005 2006 /* 2007 * This does the hard work of actually picking apart the CPU stuff... 2008 */ 2009 static void identify_cpu(struct cpuinfo_x86 *c) 2010 { 2011 int i; 2012 2013 c->loops_per_jiffy = loops_per_jiffy; 2014 c->x86_cache_size = 0; 2015 c->x86_vendor = X86_VENDOR_UNKNOWN; 2016 c->x86_model = c->x86_stepping = 0; /* So far unknown... */ 2017 c->x86_vendor_id[0] = '\0'; /* Unset */ 2018 c->x86_model_id[0] = '\0'; /* Unset */ 2019 #ifdef CONFIG_X86_64 2020 c->x86_clflush_size = 64; 2021 c->x86_phys_bits = 36; 2022 c->x86_virt_bits = 48; 2023 #else 2024 c->cpuid_level = -1; /* CPUID not detected */ 2025 c->x86_clflush_size = 32; 2026 c->x86_phys_bits = 32; 2027 c->x86_virt_bits = 32; 2028 #endif 2029 c->x86_cache_alignment = c->x86_clflush_size; 2030 memset(&c->x86_capability, 0, sizeof(c->x86_capability)); 2031 memset(&c->cpuid, 0, sizeof(c->cpuid)); 2032 #ifdef CONFIG_X86_VMX_FEATURE_NAMES 2033 memset(&c->vmx_capability, 0, sizeof(c->vmx_capability)); 2034 #endif 2035 2036 generic_identify(c); 2037 2038 cpu_parse_topology(c); 2039 2040 if (this_cpu->c_identify) 2041 this_cpu->c_identify(c); 2042 2043 /* Clear/Set all flags overridden by options, after probe */ 2044 apply_forced_caps(c); 2045 2046 /* 2047 * Set default APIC and TSC_DEADLINE MSR fencing flag. AMD and 2048 * Hygon will clear it in ->c_init() below. 2049 */ 2050 set_cpu_cap(c, X86_FEATURE_APIC_MSRS_FENCE); 2051 2052 /* 2053 * Vendor-specific initialization. In this section we 2054 * canonicalize the feature flags, meaning if there are 2055 * features a certain CPU supports which CPUID doesn't 2056 * tell us, CPUID claiming incorrect flags, or other bugs, 2057 * we handle them here. 2058 * 2059 * At the end of this section, c->x86_capability better 2060 * indicate the features this CPU genuinely supports! 2061 */ 2062 if (this_cpu->c_init) 2063 this_cpu->c_init(c); 2064 2065 bus_lock_init(); 2066 2067 /* Disable the PN if appropriate */ 2068 squash_the_stupid_serial_number(c); 2069 2070 setup_smep(c); 2071 setup_smap(c); 2072 setup_umip(c); 2073 2074 /* 2075 * The vendor-specific functions might have changed features. 2076 * Now we do "generic changes." 2077 */ 2078 2079 /* Filter out anything that depends on CPUID levels we don't have */ 2080 filter_cpuid_features(c, true); 2081 2082 /* Check for unmet dependencies based on the CPUID dependency table */ 2083 check_cpufeature_deps(c); 2084 2085 /* If the model name is still unset, do table lookup. */ 2086 if (!c->x86_model_id[0]) { 2087 const char *p; 2088 p = table_lookup_model(c); 2089 if (p) 2090 strcpy(c->x86_model_id, p); 2091 else 2092 /* Last resort... */ 2093 sprintf(c->x86_model_id, "%02x/%02x", 2094 c->x86, c->x86_model); 2095 } 2096 2097 x86_init_rdrand(c); 2098 setup_pku(c); 2099 setup_cet(c); 2100 2101 /* 2102 * Clear/Set all flags overridden by options, need do it 2103 * before following smp all cpus cap AND. 2104 */ 2105 apply_forced_caps(c); 2106 2107 /* 2108 * On SMP, boot_cpu_data holds the common feature set between 2109 * all CPUs; so make sure that we indicate which features are 2110 * common between the CPUs. The first time this routine gets 2111 * executed, c == &boot_cpu_data. 2112 */ 2113 if (c != &boot_cpu_data) { 2114 /* AND the already accumulated flags with these */ 2115 for (i = 0; i < NCAPINTS; i++) 2116 boot_cpu_data.x86_capability[i] &= c->x86_capability[i]; 2117 2118 /* OR, i.e. replicate the bug flags */ 2119 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++) 2120 c->x86_capability[i] |= boot_cpu_data.x86_capability[i]; 2121 } 2122 2123 ppin_init(c); 2124 2125 /* Init Machine Check Exception if available. */ 2126 mcheck_cpu_init(c); 2127 2128 numa_add_cpu(smp_processor_id()); 2129 } 2130 2131 /* 2132 * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions 2133 * on 32-bit kernels: 2134 */ 2135 #ifdef CONFIG_X86_32 2136 void enable_sep_cpu(void) 2137 { 2138 struct tss_struct *tss; 2139 int cpu; 2140 2141 if (!boot_cpu_has(X86_FEATURE_SEP)) 2142 return; 2143 2144 cpu = get_cpu(); 2145 tss = &per_cpu(cpu_tss_rw, cpu); 2146 2147 /* 2148 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field -- 2149 * see the big comment in struct x86_hw_tss's definition. 2150 */ 2151 2152 tss->x86_tss.ss1 = __KERNEL_CS; 2153 wrmsrq(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1); 2154 wrmsrq(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1)); 2155 wrmsrq(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32); 2156 2157 put_cpu(); 2158 } 2159 #endif 2160 2161 static __init void identify_boot_cpu(void) 2162 { 2163 identify_cpu(&boot_cpu_data); 2164 if (HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT)) 2165 pr_info("CET detected: Indirect Branch Tracking enabled\n"); 2166 #ifdef CONFIG_X86_32 2167 enable_sep_cpu(); 2168 #endif 2169 cpu_detect_tlb(&boot_cpu_data); 2170 setup_cr_pinning(); 2171 2172 x86_virt_init(); 2173 tsx_init(); 2174 tdx_init(); 2175 lkgs_init(); 2176 } 2177 2178 void identify_secondary_cpu(unsigned int cpu) 2179 { 2180 struct cpuinfo_x86 *c = &cpu_data(cpu); 2181 2182 /* Copy boot_cpu_data only on the first bringup */ 2183 if (!c->initialized) 2184 *c = boot_cpu_data; 2185 c->cpu_index = cpu; 2186 2187 identify_cpu(c); 2188 #ifdef CONFIG_X86_32 2189 enable_sep_cpu(); 2190 #endif 2191 x86_spec_ctrl_setup_ap(); 2192 update_srbds_msr(); 2193 if (boot_cpu_has_bug(X86_BUG_GDS)) 2194 update_gds_msr(); 2195 2196 tsx_ap_init(); 2197 c->initialized = true; 2198 } 2199 2200 void print_cpu_info(struct cpuinfo_x86 *c) 2201 { 2202 const char *vendor = NULL; 2203 2204 if (c->x86_vendor < X86_VENDOR_NUM) { 2205 vendor = this_cpu->c_vendor; 2206 } else { 2207 if (c->cpuid_level >= 0) 2208 vendor = c->x86_vendor_id; 2209 } 2210 2211 if (vendor && !strstr(c->x86_model_id, vendor)) 2212 pr_cont("%s ", vendor); 2213 2214 if (c->x86_model_id[0]) 2215 pr_cont("%s", c->x86_model_id); 2216 else 2217 pr_cont("%d86", c->x86); 2218 2219 pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model); 2220 2221 if (c->x86_stepping || c->cpuid_level >= 0) 2222 pr_cont(", stepping: 0x%x)\n", c->x86_stepping); 2223 else 2224 pr_cont(")\n"); 2225 } 2226 2227 /* 2228 * clearcpuid= and setcpuid= were already parsed in cpu_parse_early_param(). 2229 * These dummy functions prevent them from becoming an environment variable for 2230 * init. 2231 */ 2232 2233 static __init int setup_clearcpuid(char *arg) 2234 { 2235 return 1; 2236 } 2237 __setup("clearcpuid=", setup_clearcpuid); 2238 2239 static __init int setup_setcpuid(char *arg) 2240 { 2241 return 1; 2242 } 2243 __setup("setcpuid=", setup_setcpuid); 2244 2245 DEFINE_PER_CPU_CACHE_HOT(struct task_struct *, current_task) = &init_task; 2246 EXPORT_PER_CPU_SYMBOL(current_task); 2247 EXPORT_PER_CPU_SYMBOL(const_current_task); 2248 2249 DEFINE_PER_CPU_CACHE_HOT(unsigned long, __preempt_count) = INIT_PREEMPT_COUNT; 2250 EXPORT_PER_CPU_SYMBOL(__preempt_count); 2251 2252 DEFINE_PER_CPU_CACHE_HOT(unsigned long, cpu_current_top_of_stack) = TOP_OF_INIT_STACK; 2253 2254 #ifdef CONFIG_X86_64 2255 /* 2256 * Note: Do not make this dependant on CONFIG_MITIGATION_CALL_DEPTH_TRACKING 2257 * so that this space is reserved in the hot cache section even when the 2258 * mitigation is disabled. 2259 */ 2260 DEFINE_PER_CPU_CACHE_HOT(u64, __x86_call_depth); 2261 EXPORT_PER_CPU_SYMBOL(__x86_call_depth); 2262 2263 static void wrmsrq_cstar(unsigned long val) 2264 { 2265 /* 2266 * Intel CPUs do not support 32-bit SYSCALL. Writing to MSR_CSTAR 2267 * is so far ignored by the CPU, but raises a #VE trap in a TDX 2268 * guest. Avoid the pointless write on all Intel CPUs. 2269 */ 2270 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) 2271 wrmsrq(MSR_CSTAR, val); 2272 } 2273 2274 static inline void idt_syscall_init(void) 2275 { 2276 wrmsrq(MSR_LSTAR, (unsigned long)entry_SYSCALL_64); 2277 2278 if (ia32_enabled()) { 2279 wrmsrq_cstar((unsigned long)entry_SYSCALL_compat); 2280 /* 2281 * This only works on Intel CPUs. 2282 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP. 2283 * This does not cause SYSENTER to jump to the wrong location, because 2284 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit). 2285 */ 2286 wrmsrq_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS); 2287 wrmsrq_safe(MSR_IA32_SYSENTER_ESP, 2288 (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1)); 2289 wrmsrq_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat); 2290 } else { 2291 wrmsrq_cstar((unsigned long)entry_SYSCALL32_ignore); 2292 wrmsrq_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG); 2293 wrmsrq_safe(MSR_IA32_SYSENTER_ESP, 0ULL); 2294 wrmsrq_safe(MSR_IA32_SYSENTER_EIP, 0ULL); 2295 } 2296 2297 /* 2298 * Flags to clear on syscall; clear as much as possible 2299 * to minimize user space-kernel interference. 2300 */ 2301 wrmsrq(MSR_SYSCALL_MASK, 2302 X86_EFLAGS_CF|X86_EFLAGS_PF|X86_EFLAGS_AF| 2303 X86_EFLAGS_ZF|X86_EFLAGS_SF|X86_EFLAGS_TF| 2304 X86_EFLAGS_IF|X86_EFLAGS_DF|X86_EFLAGS_OF| 2305 X86_EFLAGS_IOPL|X86_EFLAGS_NT|X86_EFLAGS_RF| 2306 X86_EFLAGS_AC|X86_EFLAGS_ID); 2307 } 2308 2309 /* May not be marked __init: used by software suspend */ 2310 void syscall_init(void) 2311 { 2312 struct msr val = { .h = (__USER32_CS << 16) | __KERNEL_CS }; 2313 2314 /* The default user and kernel segments */ 2315 wrmsrq(MSR_STAR, val.q); 2316 2317 /* 2318 * Except the IA32_STAR MSR, there is NO need to setup SYSCALL and 2319 * SYSENTER MSRs for FRED, because FRED uses the ring 3 FRED 2320 * entrypoint for SYSCALL and SYSENTER, and ERETU is the only legit 2321 * instruction to return to ring 3 (both sysexit and sysret cause 2322 * #UD when FRED is enabled). 2323 */ 2324 if (!cpu_feature_enabled(X86_FEATURE_FRED)) 2325 idt_syscall_init(); 2326 } 2327 #endif /* CONFIG_X86_64 */ 2328 2329 #ifdef CONFIG_STACKPROTECTOR 2330 DEFINE_PER_CPU_CACHE_HOT(unsigned long, __stack_chk_guard); 2331 #ifndef CONFIG_SMP 2332 EXPORT_PER_CPU_SYMBOL(__stack_chk_guard); 2333 #endif 2334 #endif 2335 2336 static void initialize_debug_regs(void) 2337 { 2338 /* Control register first -- to make sure everything is disabled. */ 2339 set_debugreg(DR7_FIXED_1, 7); 2340 set_debugreg(DR6_RESERVED, 6); 2341 /* dr5 and dr4 don't exist */ 2342 set_debugreg(0, 3); 2343 set_debugreg(0, 2); 2344 set_debugreg(0, 1); 2345 set_debugreg(0, 0); 2346 } 2347 2348 #ifdef CONFIG_KGDB 2349 /* 2350 * Restore debug regs if using kgdbwait and you have a kernel debugger 2351 * connection established. 2352 */ 2353 static void dbg_restore_debug_regs(void) 2354 { 2355 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break)) 2356 arch_kgdb_ops.correct_hw_break(); 2357 } 2358 #else /* ! CONFIG_KGDB */ 2359 #define dbg_restore_debug_regs() 2360 #endif /* ! CONFIG_KGDB */ 2361 2362 static inline void setup_getcpu(int cpu) 2363 { 2364 unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu)); 2365 struct desc_struct d = { }; 2366 2367 if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID)) 2368 wrmsrq(MSR_TSC_AUX, cpudata); 2369 2370 /* Store CPU and node number in limit. */ 2371 d.limit0 = cpudata; 2372 d.limit1 = cpudata >> 16; 2373 2374 d.type = 5; /* RO data, expand down, accessed */ 2375 d.dpl = 3; /* Visible to user code */ 2376 d.s = 1; /* Not a system segment */ 2377 d.p = 1; /* Present */ 2378 d.d = 1; /* 32-bit */ 2379 2380 write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S); 2381 } 2382 2383 #ifdef CONFIG_X86_64 2384 static inline void tss_setup_ist(struct tss_struct *tss) 2385 { 2386 /* Set up the per-CPU TSS IST stacks */ 2387 tss->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF); 2388 tss->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI); 2389 tss->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB); 2390 tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE); 2391 /* Only mapped when SEV-ES is active */ 2392 tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC); 2393 } 2394 #else /* CONFIG_X86_64 */ 2395 static inline void tss_setup_ist(struct tss_struct *tss) { } 2396 #endif /* !CONFIG_X86_64 */ 2397 2398 static inline void tss_setup_io_bitmap(struct tss_struct *tss) 2399 { 2400 tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID; 2401 2402 #ifdef CONFIG_X86_IOPL_IOPERM 2403 tss->io_bitmap.prev_max = 0; 2404 tss->io_bitmap.prev_sequence = 0; 2405 memset(tss->io_bitmap.bitmap, 0xff, sizeof(tss->io_bitmap.bitmap)); 2406 /* 2407 * Invalidate the extra array entry past the end of the all 2408 * permission bitmap as required by the hardware. 2409 */ 2410 tss->io_bitmap.mapall[IO_BITMAP_LONGS] = ~0UL; 2411 #endif 2412 } 2413 2414 /* 2415 * Setup everything needed to handle exceptions from the IDT, including the IST 2416 * exceptions which use paranoid_entry(). 2417 */ 2418 void cpu_init_exception_handling(bool boot_cpu) 2419 { 2420 struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw); 2421 int cpu = raw_smp_processor_id(); 2422 2423 /* paranoid_entry() gets the CPU number from the GDT */ 2424 setup_getcpu(cpu); 2425 2426 /* For IDT mode, IST vectors need to be set in TSS. */ 2427 if (!cpu_feature_enabled(X86_FEATURE_FRED)) 2428 tss_setup_ist(tss); 2429 tss_setup_io_bitmap(tss); 2430 set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss); 2431 2432 load_TR_desc(); 2433 2434 /* GHCB needs to be setup to handle #VC. */ 2435 setup_ghcb(); 2436 2437 /* 2438 * On CPUs with FSGSBASE support, paranoid_entry() uses 2439 * ALTERNATIVE-patched RDGSBASE/WRGSBASE instructions. Secondary CPUs 2440 * boot after alternatives are patched globally, so early exceptions 2441 * execute patched code that depends on FSGSBASE. Enable the feature 2442 * before any exceptions occur. 2443 */ 2444 if (cpu_feature_enabled(X86_FEATURE_FSGSBASE)) { 2445 cr4_set_bits(X86_CR4_FSGSBASE); 2446 elf_hwcap2 |= HWCAP2_FSGSBASE; 2447 } 2448 2449 if (cpu_feature_enabled(X86_FEATURE_FRED)) { 2450 /* The boot CPU has enabled FRED during early boot */ 2451 if (!boot_cpu) 2452 cpu_init_fred_exceptions(); 2453 2454 cpu_init_fred_rsps(); 2455 } else { 2456 load_current_idt(); 2457 } 2458 } 2459 2460 void __init cpu_init_replace_early_idt(void) 2461 { 2462 if (cpu_feature_enabled(X86_FEATURE_FRED)) 2463 cpu_init_fred_exceptions(); 2464 else 2465 idt_setup_early_pf(); 2466 } 2467 2468 /* 2469 * cpu_init() initializes state that is per-CPU. Some data is already 2470 * initialized (naturally) in the bootstrap process, such as the GDT. We 2471 * reload it nevertheless, this function acts as a 'CPU state barrier', 2472 * nothing should get across. 2473 */ 2474 void cpu_init(void) 2475 { 2476 struct task_struct *cur = current; 2477 int cpu = raw_smp_processor_id(); 2478 2479 #ifdef CONFIG_NUMA 2480 if (this_cpu_read(numa_node) == 0 && 2481 early_cpu_to_node(cpu) != NUMA_NO_NODE) 2482 set_numa_node(early_cpu_to_node(cpu)); 2483 #endif 2484 pr_debug("Initializing CPU#%d\n", cpu); 2485 2486 if (IS_ENABLED(CONFIG_X86_64) || cpu_feature_enabled(X86_FEATURE_VME) || 2487 boot_cpu_has(X86_FEATURE_TSC) || boot_cpu_has(X86_FEATURE_DE)) 2488 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); 2489 2490 if (IS_ENABLED(CONFIG_X86_64)) { 2491 loadsegment(fs, 0); 2492 memset(cur->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8); 2493 syscall_init(); 2494 2495 wrmsrq(MSR_FS_BASE, 0); 2496 wrmsrq(MSR_KERNEL_GS_BASE, 0); 2497 barrier(); 2498 2499 x2apic_setup(); 2500 2501 intel_posted_msi_init(); 2502 } 2503 2504 mmgrab(&init_mm); 2505 cur->active_mm = &init_mm; 2506 BUG_ON(cur->mm); 2507 initialize_tlbstate_and_flush(); 2508 enter_lazy_tlb(&init_mm, cur); 2509 2510 /* 2511 * sp0 points to the entry trampoline stack regardless of what task 2512 * is running. 2513 */ 2514 load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1)); 2515 2516 load_mm_ldt(&init_mm); 2517 2518 initialize_debug_regs(); 2519 dbg_restore_debug_regs(); 2520 2521 doublefault_init_cpu_tss(); 2522 2523 if (is_uv_system()) 2524 uv_cpu_init(); 2525 2526 load_fixmap_gdt(cpu); 2527 } 2528 2529 #ifdef CONFIG_MICROCODE_LATE_LOADING 2530 /** 2531 * store_cpu_caps() - Store a snapshot of CPU capabilities 2532 * @curr_info: Pointer where to store it 2533 * 2534 * Returns: None 2535 */ 2536 void store_cpu_caps(struct cpuinfo_x86 *curr_info) 2537 { 2538 /* Reload CPUID max function as it might've changed. */ 2539 curr_info->cpuid_level = cpuid_eax(0); 2540 2541 /* Copy all capability leafs and pick up the synthetic ones. */ 2542 memcpy(&curr_info->x86_capability, &boot_cpu_data.x86_capability, 2543 sizeof(curr_info->x86_capability)); 2544 2545 /* Get the hardware CPUID leafs */ 2546 get_cpu_cap(curr_info); 2547 } 2548 2549 /** 2550 * microcode_check() - Check if any CPU capabilities changed after an update. 2551 * @prev_info: CPU capabilities stored before an update. 2552 * 2553 * The microcode loader calls this upon late microcode load to recheck features, 2554 * only when microcode has been updated. Caller holds and CPU hotplug lock. 2555 * 2556 * Return: None 2557 */ 2558 void microcode_check(struct cpuinfo_x86 *prev_info) 2559 { 2560 struct cpuinfo_x86 curr_info; 2561 2562 perf_check_microcode(); 2563 2564 amd_check_microcode(); 2565 2566 store_cpu_caps(&curr_info); 2567 2568 if (!memcmp(&prev_info->x86_capability, &curr_info.x86_capability, 2569 sizeof(prev_info->x86_capability))) 2570 return; 2571 2572 pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n"); 2573 pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n"); 2574 } 2575 #endif 2576 2577 /* 2578 * Invoked from core CPU hotplug code after hotplug operations 2579 */ 2580 void arch_smt_update(void) 2581 { 2582 /* Handle the speculative execution misfeatures */ 2583 cpu_bugs_smt_update(); 2584 /* Check whether IPI broadcasting can be enabled */ 2585 apic_smt_update(); 2586 } 2587 2588 void __init arch_cpu_finalize_init(void) 2589 { 2590 struct cpuinfo_x86 *c = this_cpu_ptr(&cpu_info); 2591 2592 identify_boot_cpu(); 2593 2594 select_idle_routine(); 2595 2596 /* 2597 * identify_boot_cpu() initialized SMT support information, let the 2598 * core code know. 2599 */ 2600 cpu_smt_set_num_threads(__max_threads_per_core, __max_threads_per_core); 2601 2602 if (!IS_ENABLED(CONFIG_SMP)) { 2603 pr_info("CPU: "); 2604 print_cpu_info(&boot_cpu_data); 2605 } 2606 2607 cpu_select_mitigations(); 2608 2609 arch_smt_update(); 2610 2611 if (IS_ENABLED(CONFIG_X86_32)) { 2612 /* 2613 * Check whether this is a real i386 which is not longer 2614 * supported and fixup the utsname. 2615 */ 2616 if (boot_cpu_data.x86 < 4) 2617 panic("Kernel requires i486+ for 'invlpg' and other features"); 2618 2619 init_utsname()->machine[1] = 2620 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86); 2621 } 2622 2623 /* 2624 * Must be before alternatives because it might set or clear 2625 * feature bits. 2626 */ 2627 fpu__init_system(); 2628 fpu__init_cpu(); 2629 2630 /* 2631 * This needs to follow the FPU initializtion, since EFI depends on it. 2632 */ 2633 if (efi_enabled(EFI_RUNTIME_SERVICES)) 2634 efi_enter_virtual_mode(); 2635 2636 /* 2637 * Ensure that access to the per CPU representation has the initial 2638 * boot CPU configuration. 2639 */ 2640 *c = boot_cpu_data; 2641 c->initialized = true; 2642 2643 alternative_instructions(); 2644 2645 if (IS_ENABLED(CONFIG_X86_64)) { 2646 USER_PTR_MAX = TASK_SIZE_MAX; 2647 2648 /* 2649 * Enable this when LAM is gated on LASS support 2650 if (cpu_feature_enabled(X86_FEATURE_LAM)) 2651 USER_PTR_MAX = (1ul << 63) - PAGE_SIZE; 2652 */ 2653 runtime_const_init(ptr, USER_PTR_MAX); 2654 2655 /* 2656 * Make sure the first 2MB area is not mapped by huge pages 2657 * There are typically fixed size MTRRs in there and overlapping 2658 * MTRRs into large pages causes slow downs. 2659 * 2660 * Right now we don't do that with gbpages because there seems 2661 * very little benefit for that case. 2662 */ 2663 if (!direct_gbpages) 2664 set_memory_4k((unsigned long)__va(0), 1); 2665 } else { 2666 fpu__init_check_bugs(); 2667 } 2668 2669 /* 2670 * This needs to be called before any devices perform DMA 2671 * operations that might use the SWIOTLB bounce buffers. It will 2672 * mark the bounce buffers as decrypted so that their usage will 2673 * not cause "plain-text" data to be decrypted when accessed. It 2674 * must be called after late_time_init() so that Hyper-V x86/x64 2675 * hypercalls work when the SWIOTLB bounce buffers are decrypted. 2676 */ 2677 mem_encrypt_init(); 2678 } 2679 2680 /* Control TLB flushing methods */ 2681 static int __init tlbi_setup(char *str) 2682 { 2683 if (!strcmp(str, "ipi")) 2684 setup_clear_cpu_cap(X86_FEATURE_INVLPGB); 2685 2686 return 1; 2687 } 2688 __setup("tlbi=", tlbi_setup); 2689