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