1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 22458e53fSKirill A. Shutemov /* cpu_feature_enabled() cannot be used this early */ 32458e53fSKirill A. Shutemov #define USE_EARLY_PGTABLE_L5 42458e53fSKirill A. Shutemov 557c8a661SMike Rapoport #include <linux/memblock.h> 69766cdbcSJaswinder Singh Rajput #include <linux/linkage.h> 7f0fc4affSYinghai Lu #include <linux/bitops.h> 89766cdbcSJaswinder Singh Rajput #include <linux/kernel.h> 9186f4360SPaul Gortmaker #include <linux/export.h> 10f7627e25SThomas Gleixner #include <linux/percpu.h> 119766cdbcSJaswinder Singh Rajput #include <linux/string.h> 12ee098e1aSBorislav Petkov #include <linux/ctype.h> 139766cdbcSJaswinder Singh Rajput #include <linux/delay.h> 1468e21be2SIngo Molnar #include <linux/sched/mm.h> 15e6017571SIngo Molnar #include <linux/sched/clock.h> 169164bb4aSIngo Molnar #include <linux/sched/task.h> 17b47a3698SBenjamin Thiel #include <linux/sched/smt.h> 189766cdbcSJaswinder Singh Rajput #include <linux/init.h> 190f46efebSMasami Hiramatsu #include <linux/kprobes.h> 209766cdbcSJaswinder Singh Rajput #include <linux/kgdb.h> 21439e1757SThomas Gleixner #include <linux/mem_encrypt.h> 229766cdbcSJaswinder Singh Rajput #include <linux/smp.h> 237c7077a7SThomas Gleixner #include <linux/cpu.h> 249766cdbcSJaswinder Singh Rajput #include <linux/io.h> 25b51ef52dSLaura Abbott #include <linux/syscore_ops.h> 2665fddcfcSMike Rapoport #include <linux/pgtable.h> 27b3883a9aSJason A. Donenfeld #include <linux/stackprotector.h> 287c7077a7SThomas Gleixner #include <linux/utsname.h> 299766cdbcSJaswinder Singh Rajput 307c7077a7SThomas Gleixner #include <asm/alternative.h> 311ef5423aSMike Hommey #include <asm/cmdline.h> 32cdd6c482SIngo Molnar #include <asm/perf_event.h> 33f7627e25SThomas Gleixner #include <asm/mmu_context.h> 34dc4e0021SAndy Lutomirski #include <asm/doublefault.h> 3549d859d7SH. Peter Anvin #include <asm/archrandom.h> 369766cdbcSJaswinder Singh Rajput #include <asm/hypervisor.h> 379766cdbcSJaswinder Singh Rajput #include <asm/processor.h> 381e02ce4cSAndy Lutomirski #include <asm/tlbflush.h> 39f649e938SPaul Gortmaker #include <asm/debugreg.h> 409766cdbcSJaswinder Singh Rajput #include <asm/sections.h> 41f40c3300SAndy Lutomirski #include <asm/vsyscall.h> 428bdbd962SAlan Cox #include <linux/topology.h> 438bdbd962SAlan Cox #include <linux/cpumask.h> 4460063497SArun Sharma #include <linux/atomic.h> 459766cdbcSJaswinder Singh Rajput #include <asm/proto.h> 469766cdbcSJaswinder Singh Rajput #include <asm/setup.h> 47f7627e25SThomas Gleixner #include <asm/apic.h> 489766cdbcSJaswinder Singh Rajput #include <asm/desc.h> 49b56d2795SThomas Gleixner #include <asm/fpu/api.h> 509766cdbcSJaswinder Singh Rajput #include <asm/mtrr.h> 510274f955SGrzegorz Andrejczuk #include <asm/hwcap2.h> 528bdbd962SAlan Cox #include <linux/numa.h> 530cd39f46SPeter Zijlstra #include <asm/numa.h> 549766cdbcSJaswinder Singh Rajput #include <asm/asm.h> 550f6ff2bcSDave Hansen #include <asm/bugs.h> 569766cdbcSJaswinder Singh Rajput #include <asm/cpu.h> 579766cdbcSJaswinder Singh Rajput #include <asm/mce.h> 589766cdbcSJaswinder Singh Rajput #include <asm/msr.h> 590b9a6a8bSJuergen Gross #include <asm/cacheinfo.h> 60eb243d1dSIngo Molnar #include <asm/memtype.h> 61d288e1cfSFenghua Yu #include <asm/microcode.h> 62fec9434aSDavid Woodhouse #include <asm/intel-family.h> 63fec9434aSDavid Woodhouse #include <asm/cpu_device_id.h> 64bdbcdd48STejun Heo #include <asm/uv/uv.h> 6561382281SNikolay Borisov #include <asm/ia32.h> 667c7077a7SThomas Gleixner #include <asm/set_memory.h> 67991625f3SPeter Zijlstra #include <asm/traps.h> 6895d33bfaSBrijesh Singh #include <asm/sev.h> 69f7627e25SThomas Gleixner 70f7627e25SThomas Gleixner #include "cpu.h" 71f7627e25SThomas Gleixner 720274f955SGrzegorz Andrejczuk u32 elf_hwcap2 __read_mostly; 730274f955SGrzegorz Andrejczuk 74f8b64d08SBorislav Petkov /* Number of siblings per CPU package */ 75f8b64d08SBorislav Petkov int smp_num_siblings = 1; 76f8b64d08SBorislav Petkov EXPORT_SYMBOL(smp_num_siblings); 77f8b64d08SBorislav Petkov 780dcab41dSTony Luck static struct ppin_info { 790dcab41dSTony Luck int feature; 800dcab41dSTony Luck int msr_ppin_ctl; 81822ccfadSTony Luck int msr_ppin; 820dcab41dSTony Luck } ppin_info[] = { 830dcab41dSTony Luck [X86_VENDOR_INTEL] = { 840dcab41dSTony Luck .feature = X86_FEATURE_INTEL_PPIN, 850dcab41dSTony Luck .msr_ppin_ctl = MSR_PPIN_CTL, 86822ccfadSTony Luck .msr_ppin = MSR_PPIN 870dcab41dSTony Luck }, 880dcab41dSTony Luck [X86_VENDOR_AMD] = { 890dcab41dSTony Luck .feature = X86_FEATURE_AMD_PPIN, 900dcab41dSTony Luck .msr_ppin_ctl = MSR_AMD_PPIN_CTL, 91822ccfadSTony Luck .msr_ppin = MSR_AMD_PPIN 920dcab41dSTony Luck }, 930dcab41dSTony Luck }; 940dcab41dSTony Luck 950dcab41dSTony Luck static const struct x86_cpu_id ppin_cpuids[] = { 960dcab41dSTony Luck X86_MATCH_FEATURE(X86_FEATURE_AMD_PPIN, &ppin_info[X86_VENDOR_AMD]), 9700a2f23eSTony Luck X86_MATCH_FEATURE(X86_FEATURE_INTEL_PPIN, &ppin_info[X86_VENDOR_INTEL]), 980dcab41dSTony Luck 990dcab41dSTony Luck /* Legacy models without CPUID enumeration */ 1000dcab41dSTony Luck X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE_X, &ppin_info[X86_VENDOR_INTEL]), 1010dcab41dSTony Luck X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, &ppin_info[X86_VENDOR_INTEL]), 1020dcab41dSTony Luck X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, &ppin_info[X86_VENDOR_INTEL]), 1030dcab41dSTony Luck X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, &ppin_info[X86_VENDOR_INTEL]), 1040dcab41dSTony Luck X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, &ppin_info[X86_VENDOR_INTEL]), 1050dcab41dSTony Luck X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, &ppin_info[X86_VENDOR_INTEL]), 1060dcab41dSTony Luck X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, &ppin_info[X86_VENDOR_INTEL]), 1070dcab41dSTony Luck X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, &ppin_info[X86_VENDOR_INTEL]), 10836168bc0STony Luck X86_MATCH_INTEL_FAM6_MODEL(EMERALDRAPIDS_X, &ppin_info[X86_VENDOR_INTEL]), 1090dcab41dSTony Luck X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNL, &ppin_info[X86_VENDOR_INTEL]), 1100dcab41dSTony Luck X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNM, &ppin_info[X86_VENDOR_INTEL]), 1110dcab41dSTony Luck 1120dcab41dSTony Luck {} 1130dcab41dSTony Luck }; 1140dcab41dSTony Luck 1150dcab41dSTony Luck static void ppin_init(struct cpuinfo_x86 *c) 1160dcab41dSTony Luck { 1170dcab41dSTony Luck const struct x86_cpu_id *id; 1180dcab41dSTony Luck unsigned long long val; 1190dcab41dSTony Luck struct ppin_info *info; 1200dcab41dSTony Luck 1210dcab41dSTony Luck id = x86_match_cpu(ppin_cpuids); 1220dcab41dSTony Luck if (!id) 1230dcab41dSTony Luck return; 1240dcab41dSTony Luck 1250dcab41dSTony Luck /* 1260dcab41dSTony Luck * Testing the presence of the MSR is not enough. Need to check 1270dcab41dSTony Luck * that the PPIN_CTL allows reading of the PPIN. 1280dcab41dSTony Luck */ 1290dcab41dSTony Luck info = (struct ppin_info *)id->driver_data; 1300dcab41dSTony Luck 1310dcab41dSTony Luck if (rdmsrl_safe(info->msr_ppin_ctl, &val)) 1320dcab41dSTony Luck goto clear_ppin; 1330dcab41dSTony Luck 1340dcab41dSTony Luck if ((val & 3UL) == 1UL) { 1350dcab41dSTony Luck /* PPIN locked in disabled mode */ 1360dcab41dSTony Luck goto clear_ppin; 1370dcab41dSTony Luck } 1380dcab41dSTony Luck 1390dcab41dSTony Luck /* If PPIN is disabled, try to enable */ 1400dcab41dSTony Luck if (!(val & 2UL)) { 1410dcab41dSTony Luck wrmsrl_safe(info->msr_ppin_ctl, val | 2UL); 1420dcab41dSTony Luck rdmsrl_safe(info->msr_ppin_ctl, &val); 1430dcab41dSTony Luck } 1440dcab41dSTony Luck 1450dcab41dSTony Luck /* Is the enable bit set? */ 1460dcab41dSTony Luck if (val & 2UL) { 147822ccfadSTony Luck c->ppin = __rdmsr(info->msr_ppin); 1480dcab41dSTony Luck set_cpu_cap(c, info->feature); 1490dcab41dSTony Luck return; 1500dcab41dSTony Luck } 1510dcab41dSTony Luck 1520dcab41dSTony Luck clear_ppin: 1530dcab41dSTony Luck clear_cpu_cap(c, info->feature); 1540dcab41dSTony Luck } 1550dcab41dSTony Luck 156148f9bb8SPaul Gortmaker static void default_init(struct cpuinfo_x86 *c) 157e8055139SOndrej Zary { 158e8055139SOndrej Zary #ifdef CONFIG_X86_64 15927c13eceSBorislav Petkov cpu_detect_cache_sizes(c); 160e8055139SOndrej Zary #else 161e8055139SOndrej Zary /* Not much we can do here... */ 162e8055139SOndrej Zary /* Check if at least it has cpuid */ 163e8055139SOndrej Zary if (c->cpuid_level == -1) { 164e8055139SOndrej Zary /* No cpuid. It must be an ancient CPU */ 165e8055139SOndrej Zary if (c->x86 == 4) 166e8055139SOndrej Zary strcpy(c->x86_model_id, "486"); 167e8055139SOndrej Zary else if (c->x86 == 3) 168e8055139SOndrej Zary strcpy(c->x86_model_id, "386"); 169e8055139SOndrej Zary } 170e8055139SOndrej Zary #endif 171e8055139SOndrej Zary } 172e8055139SOndrej Zary 173148f9bb8SPaul Gortmaker static const struct cpu_dev default_cpu = { 174e8055139SOndrej Zary .c_init = default_init, 175e8055139SOndrej Zary .c_vendor = "Unknown", 176e8055139SOndrej Zary .c_x86_vendor = X86_VENDOR_UNKNOWN, 177e8055139SOndrej Zary }; 178e8055139SOndrej Zary 179148f9bb8SPaul Gortmaker static const struct cpu_dev *this_cpu = &default_cpu; 1800a488a53SYinghai Lu 18106deef89SBrian Gerst DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = { 182950ad7ffSYinghai Lu #ifdef CONFIG_X86_64 18306deef89SBrian Gerst /* 18406deef89SBrian Gerst * We need valid kernel segments for data and code in long mode too 185950ad7ffSYinghai Lu * IRET will check the segment types kkeil 2000/10/28 186950ad7ffSYinghai Lu * Also sysret mandates a special GDT layout 18706deef89SBrian Gerst * 1889766cdbcSJaswinder Singh Rajput * TLS descriptors are currently at a different place compared to i386. 18906deef89SBrian Gerst * Hopefully nobody expects them at a fixed place (Wine?) 190950ad7ffSYinghai Lu */ 1911e5de182SAkinobu Mita [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff), 1921e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff), 1931e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff), 1941e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff), 1951e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff), 1961e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff), 197950ad7ffSYinghai Lu #else 1981e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff), 1991e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 2001e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff), 2011e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff), 202f7627e25SThomas Gleixner /* 203f7627e25SThomas Gleixner * Segments used for calling PnP BIOS have byte granularity. 204f7627e25SThomas Gleixner * They code segments and data segments have fixed 64k limits, 205f7627e25SThomas Gleixner * the transfer segment sizes are set at run time. 206f7627e25SThomas Gleixner */ 2076842ef0eSGlauber de Oliveira Costa /* 32-bit code */ 2081e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), 2096842ef0eSGlauber de Oliveira Costa /* 16-bit code */ 2101e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), 2116842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 2121e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff), 2136842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 2141e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0), 2156842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 2161e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0), 217f7627e25SThomas Gleixner /* 218f7627e25SThomas Gleixner * The APM segments have byte granularity and their bases 219f7627e25SThomas Gleixner * are set at run time. All have 64k limits. 220f7627e25SThomas Gleixner */ 2216842ef0eSGlauber de Oliveira Costa /* 32-bit code */ 2221e5de182SAkinobu Mita [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), 223f7627e25SThomas Gleixner /* 16-bit code */ 2241e5de182SAkinobu Mita [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), 2256842ef0eSGlauber de Oliveira Costa /* data */ 22672c4d853SIngo Molnar [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff), 227f7627e25SThomas Gleixner 2281e5de182SAkinobu Mita [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 2291e5de182SAkinobu Mita [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 230950ad7ffSYinghai Lu #endif 23106deef89SBrian Gerst } }; 232f7627e25SThomas Gleixner EXPORT_PER_CPU_SYMBOL_GPL(gdt_page); 233f7627e25SThomas Gleixner 2340790c9aaSAndy Lutomirski #ifdef CONFIG_X86_64 235c7ad5ad2SAndy Lutomirski static int __init x86_nopcid_setup(char *s) 2360790c9aaSAndy Lutomirski { 237c7ad5ad2SAndy Lutomirski /* nopcid doesn't accept parameters */ 238c7ad5ad2SAndy Lutomirski if (s) 239c7ad5ad2SAndy Lutomirski return -EINVAL; 2400790c9aaSAndy Lutomirski 2410790c9aaSAndy Lutomirski /* do not emit a message if the feature is not present */ 2420790c9aaSAndy Lutomirski if (!boot_cpu_has(X86_FEATURE_PCID)) 243c7ad5ad2SAndy Lutomirski return 0; 2440790c9aaSAndy Lutomirski 2450790c9aaSAndy Lutomirski setup_clear_cpu_cap(X86_FEATURE_PCID); 2460790c9aaSAndy Lutomirski pr_info("nopcid: PCID feature disabled\n"); 247c7ad5ad2SAndy Lutomirski return 0; 2480790c9aaSAndy Lutomirski } 249c7ad5ad2SAndy Lutomirski early_param("nopcid", x86_nopcid_setup); 2500790c9aaSAndy Lutomirski #endif 2510790c9aaSAndy Lutomirski 252d12a72b8SAndy Lutomirski static int __init x86_noinvpcid_setup(char *s) 253d12a72b8SAndy Lutomirski { 254d12a72b8SAndy Lutomirski /* noinvpcid doesn't accept parameters */ 255d12a72b8SAndy Lutomirski if (s) 256d12a72b8SAndy Lutomirski return -EINVAL; 257d12a72b8SAndy Lutomirski 258d12a72b8SAndy Lutomirski /* do not emit a message if the feature is not present */ 259d12a72b8SAndy Lutomirski if (!boot_cpu_has(X86_FEATURE_INVPCID)) 260d12a72b8SAndy Lutomirski return 0; 261d12a72b8SAndy Lutomirski 262d12a72b8SAndy Lutomirski setup_clear_cpu_cap(X86_FEATURE_INVPCID); 263d12a72b8SAndy Lutomirski pr_info("noinvpcid: INVPCID feature disabled\n"); 264d12a72b8SAndy Lutomirski return 0; 265d12a72b8SAndy Lutomirski } 266d12a72b8SAndy Lutomirski early_param("noinvpcid", x86_noinvpcid_setup); 267d12a72b8SAndy Lutomirski 268ba51dcedSYinghai Lu #ifdef CONFIG_X86_32 269148f9bb8SPaul Gortmaker static int cachesize_override = -1; 270148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1; 271f7627e25SThomas Gleixner 272f7627e25SThomas Gleixner static int __init cachesize_setup(char *str) 273f7627e25SThomas Gleixner { 274f7627e25SThomas Gleixner get_option(&str, &cachesize_override); 275f7627e25SThomas Gleixner return 1; 276f7627e25SThomas Gleixner } 277f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup); 278f7627e25SThomas Gleixner 279f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */ 280f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag) 281f7627e25SThomas Gleixner { 282f7627e25SThomas Gleixner u32 f1, f2; 283f7627e25SThomas Gleixner 28494f6bac1SKrzysztof Helt /* 28594f6bac1SKrzysztof Helt * Cyrix and IDT cpus allow disabling of CPUID 28694f6bac1SKrzysztof Helt * so the code below may return different results 28794f6bac1SKrzysztof Helt * when it is executed before and after enabling 28894f6bac1SKrzysztof Helt * the CPUID. Add "volatile" to not allow gcc to 28994f6bac1SKrzysztof Helt * optimize the subsequent calls to this function. 29094f6bac1SKrzysztof Helt */ 29194f6bac1SKrzysztof Helt asm volatile ("pushfl \n\t" 292f7627e25SThomas Gleixner "pushfl \n\t" 293f7627e25SThomas Gleixner "popl %0 \n\t" 294f7627e25SThomas Gleixner "movl %0, %1 \n\t" 295f7627e25SThomas Gleixner "xorl %2, %0 \n\t" 296f7627e25SThomas Gleixner "pushl %0 \n\t" 297f7627e25SThomas Gleixner "popfl \n\t" 298f7627e25SThomas Gleixner "pushfl \n\t" 299f7627e25SThomas Gleixner "popl %0 \n\t" 300f7627e25SThomas Gleixner "popfl \n\t" 3010f3fa48aSIngo Molnar 302f7627e25SThomas Gleixner : "=&r" (f1), "=&r" (f2) 303f7627e25SThomas Gleixner : "ir" (flag)); 304f7627e25SThomas Gleixner 305f7627e25SThomas Gleixner return ((f1^f2) & flag) != 0; 306f7627e25SThomas Gleixner } 307f7627e25SThomas Gleixner 308f7627e25SThomas Gleixner /* Probe for the CPUID instruction */ 309148f9bb8SPaul Gortmaker int have_cpuid_p(void) 310f7627e25SThomas Gleixner { 311f7627e25SThomas Gleixner return flag_is_changeable_p(X86_EFLAGS_ID); 312f7627e25SThomas Gleixner } 313f7627e25SThomas Gleixner 314148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) 3150a488a53SYinghai Lu { 3160a488a53SYinghai Lu unsigned long lo, hi; 3170f3fa48aSIngo Molnar 3180f3fa48aSIngo Molnar if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr) 3190f3fa48aSIngo Molnar return; 3200f3fa48aSIngo Molnar 3210f3fa48aSIngo Molnar /* Disable processor serial number: */ 3220f3fa48aSIngo Molnar 3230a488a53SYinghai Lu rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi); 3240a488a53SYinghai Lu lo |= 0x200000; 3250a488a53SYinghai Lu wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi); 3260f3fa48aSIngo Molnar 3271b74dde7SChen Yucong pr_notice("CPU serial number disabled.\n"); 3280a488a53SYinghai Lu clear_cpu_cap(c, X86_FEATURE_PN); 3290a488a53SYinghai Lu 3300a488a53SYinghai Lu /* Disabling the serial number may affect the cpuid level */ 3310a488a53SYinghai Lu c->cpuid_level = cpuid_eax(0); 3320a488a53SYinghai Lu } 3330a488a53SYinghai Lu 3340a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s) 3350a488a53SYinghai Lu { 3360a488a53SYinghai Lu disable_x86_serial_nr = 0; 3370a488a53SYinghai Lu return 1; 3380a488a53SYinghai Lu } 3390a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup); 340ba51dcedSYinghai Lu #else 341102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag) 342102bbe3aSYinghai Lu { 343102bbe3aSYinghai Lu return 1; 344102bbe3aSYinghai Lu } 345102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) 346102bbe3aSYinghai Lu { 347102bbe3aSYinghai Lu } 348ba51dcedSYinghai Lu #endif 3490a488a53SYinghai Lu 350b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c) 351de5397adSFenghua Yu { 352b2cc2a07SH. Peter Anvin if (cpu_has(c, X86_FEATURE_SMEP)) 353375074ccSAndy Lutomirski cr4_set_bits(X86_CR4_SMEP); 354de5397adSFenghua Yu } 355de5397adSFenghua Yu 356b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c) 35752b6179aSH. Peter Anvin { 358581b7f15SAndrew Cooper unsigned long eflags = native_save_fl(); 359b2cc2a07SH. Peter Anvin 360b2cc2a07SH. Peter Anvin /* This should have been cleared long ago */ 361b2cc2a07SH. Peter Anvin BUG_ON(eflags & X86_EFLAGS_AC); 362b2cc2a07SH. Peter Anvin 363dbae0a93SBorislav Petkov if (cpu_has(c, X86_FEATURE_SMAP)) 364375074ccSAndy Lutomirski cr4_set_bits(X86_CR4_SMAP); 365f7627e25SThomas Gleixner } 366f7627e25SThomas Gleixner 367aa35f896SRicardo Neri static __always_inline void setup_umip(struct cpuinfo_x86 *c) 368aa35f896SRicardo Neri { 369aa35f896SRicardo Neri /* Check the boot processor, plus build option for UMIP. */ 370aa35f896SRicardo Neri if (!cpu_feature_enabled(X86_FEATURE_UMIP)) 371aa35f896SRicardo Neri goto out; 372aa35f896SRicardo Neri 373aa35f896SRicardo Neri /* Check the current processor's cpuid bits. */ 374aa35f896SRicardo Neri if (!cpu_has(c, X86_FEATURE_UMIP)) 375aa35f896SRicardo Neri goto out; 376aa35f896SRicardo Neri 377aa35f896SRicardo Neri cr4_set_bits(X86_CR4_UMIP); 378aa35f896SRicardo Neri 379438cbf88SLendacky, Thomas pr_info_once("x86/cpu: User Mode Instruction Prevention (UMIP) activated\n"); 380770c7755SRicardo Neri 381aa35f896SRicardo Neri return; 382aa35f896SRicardo Neri 383aa35f896SRicardo Neri out: 384aa35f896SRicardo Neri /* 385aa35f896SRicardo Neri * Make sure UMIP is disabled in case it was enabled in a 386aa35f896SRicardo Neri * previous boot (e.g., via kexec). 387aa35f896SRicardo Neri */ 388aa35f896SRicardo Neri cr4_clear_bits(X86_CR4_UMIP); 389aa35f896SRicardo Neri } 390aa35f896SRicardo Neri 391a13b9d0bSKees Cook /* These bits should not change their value after CPU init is finished. */ 392a13b9d0bSKees Cook static const unsigned long cr4_pinned_mask = 393991625f3SPeter Zijlstra X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP | 394991625f3SPeter Zijlstra X86_CR4_FSGSBASE | X86_CR4_CET; 3957652ac92SThomas Gleixner static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning); 3967652ac92SThomas Gleixner static unsigned long cr4_pinned_bits __ro_after_init; 3977652ac92SThomas Gleixner 3987652ac92SThomas Gleixner void native_write_cr0(unsigned long val) 3997652ac92SThomas Gleixner { 4007652ac92SThomas Gleixner unsigned long bits_missing = 0; 4017652ac92SThomas Gleixner 4027652ac92SThomas Gleixner set_register: 403aa5cacdcSArvind Sankar asm volatile("mov %0,%%cr0": "+r" (val) : : "memory"); 4047652ac92SThomas Gleixner 4057652ac92SThomas Gleixner if (static_branch_likely(&cr_pinning)) { 4067652ac92SThomas Gleixner if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) { 4077652ac92SThomas Gleixner bits_missing = X86_CR0_WP; 4087652ac92SThomas Gleixner val |= bits_missing; 4097652ac92SThomas Gleixner goto set_register; 4107652ac92SThomas Gleixner } 4117652ac92SThomas Gleixner /* Warn after we've set the missing bits. */ 4127652ac92SThomas Gleixner WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n"); 4137652ac92SThomas Gleixner } 4147652ac92SThomas Gleixner } 4157652ac92SThomas Gleixner EXPORT_SYMBOL(native_write_cr0); 4167652ac92SThomas Gleixner 417b64dfcdeSBorislav Petkov void __no_profile native_write_cr4(unsigned long val) 4187652ac92SThomas Gleixner { 419a13b9d0bSKees Cook unsigned long bits_changed = 0; 4207652ac92SThomas Gleixner 4217652ac92SThomas Gleixner set_register: 422aa5cacdcSArvind Sankar asm volatile("mov %0,%%cr4": "+r" (val) : : "memory"); 4237652ac92SThomas Gleixner 4247652ac92SThomas Gleixner if (static_branch_likely(&cr_pinning)) { 425a13b9d0bSKees Cook if (unlikely((val & cr4_pinned_mask) != cr4_pinned_bits)) { 426a13b9d0bSKees Cook bits_changed = (val & cr4_pinned_mask) ^ cr4_pinned_bits; 427a13b9d0bSKees Cook val = (val & ~cr4_pinned_mask) | cr4_pinned_bits; 4287652ac92SThomas Gleixner goto set_register; 4297652ac92SThomas Gleixner } 430a13b9d0bSKees Cook /* Warn after we've corrected the changed bits. */ 431a13b9d0bSKees Cook WARN_ONCE(bits_changed, "pinned CR4 bits changed: 0x%lx!?\n", 432a13b9d0bSKees Cook bits_changed); 4337652ac92SThomas Gleixner } 4347652ac92SThomas Gleixner } 43521953ee5SThomas Gleixner #if IS_MODULE(CONFIG_LKDTM) 436d8f0b353SThomas Gleixner EXPORT_SYMBOL_GPL(native_write_cr4); 43721953ee5SThomas Gleixner #endif 438d8f0b353SThomas Gleixner 439d8f0b353SThomas Gleixner void cr4_update_irqsoff(unsigned long set, unsigned long clear) 440d8f0b353SThomas Gleixner { 441d8f0b353SThomas Gleixner unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4); 442d8f0b353SThomas Gleixner 443d8f0b353SThomas Gleixner lockdep_assert_irqs_disabled(); 444d8f0b353SThomas Gleixner 445d8f0b353SThomas Gleixner newval = (cr4 & ~clear) | set; 446d8f0b353SThomas Gleixner if (newval != cr4) { 447d8f0b353SThomas Gleixner this_cpu_write(cpu_tlbstate.cr4, newval); 448d8f0b353SThomas Gleixner __write_cr4(newval); 449d8f0b353SThomas Gleixner } 450d8f0b353SThomas Gleixner } 451d8f0b353SThomas Gleixner EXPORT_SYMBOL(cr4_update_irqsoff); 452d8f0b353SThomas Gleixner 453d8f0b353SThomas Gleixner /* Read the CR4 shadow. */ 454d8f0b353SThomas Gleixner unsigned long cr4_read_shadow(void) 455d8f0b353SThomas Gleixner { 456d8f0b353SThomas Gleixner return this_cpu_read(cpu_tlbstate.cr4); 457d8f0b353SThomas Gleixner } 458d8f0b353SThomas Gleixner EXPORT_SYMBOL_GPL(cr4_read_shadow); 4597652ac92SThomas Gleixner 4607652ac92SThomas Gleixner void cr4_init(void) 4617652ac92SThomas Gleixner { 4627652ac92SThomas Gleixner unsigned long cr4 = __read_cr4(); 4637652ac92SThomas Gleixner 4647652ac92SThomas Gleixner if (boot_cpu_has(X86_FEATURE_PCID)) 4657652ac92SThomas Gleixner cr4 |= X86_CR4_PCIDE; 4667652ac92SThomas Gleixner if (static_branch_likely(&cr_pinning)) 467a13b9d0bSKees Cook cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits; 4687652ac92SThomas Gleixner 4697652ac92SThomas Gleixner __write_cr4(cr4); 4707652ac92SThomas Gleixner 4717652ac92SThomas Gleixner /* Initialize cr4 shadow for this CPU. */ 4727652ac92SThomas Gleixner this_cpu_write(cpu_tlbstate.cr4, cr4); 4737652ac92SThomas Gleixner } 474873d50d5SKees Cook 475873d50d5SKees Cook /* 476873d50d5SKees Cook * Once CPU feature detection is finished (and boot params have been 477873d50d5SKees Cook * parsed), record any of the sensitive CR bits that are set, and 478873d50d5SKees Cook * enable CR pinning. 479873d50d5SKees Cook */ 480873d50d5SKees Cook static void __init setup_cr_pinning(void) 481873d50d5SKees Cook { 482a13b9d0bSKees Cook cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask; 483873d50d5SKees Cook static_key_enable(&cr_pinning.key); 484873d50d5SKees Cook } 485873d50d5SKees Cook 486b745cfbaSAndy Lutomirski static __init int x86_nofsgsbase_setup(char *arg) 487dd649bd0SAndy Lutomirski { 488b745cfbaSAndy Lutomirski /* Require an exact match without trailing characters. */ 489b745cfbaSAndy Lutomirski if (strlen(arg)) 490b745cfbaSAndy Lutomirski return 0; 491b745cfbaSAndy Lutomirski 492b745cfbaSAndy Lutomirski /* Do not emit a message if the feature is not present. */ 493b745cfbaSAndy Lutomirski if (!boot_cpu_has(X86_FEATURE_FSGSBASE)) 494b745cfbaSAndy Lutomirski return 1; 495b745cfbaSAndy Lutomirski 496b745cfbaSAndy Lutomirski setup_clear_cpu_cap(X86_FEATURE_FSGSBASE); 497b745cfbaSAndy Lutomirski pr_info("FSGSBASE disabled via kernel command line\n"); 498dd649bd0SAndy Lutomirski return 1; 499dd649bd0SAndy Lutomirski } 500b745cfbaSAndy Lutomirski __setup("nofsgsbase", x86_nofsgsbase_setup); 501dd649bd0SAndy Lutomirski 502b64ed19bSAndy Lutomirski /* 50306976945SDave Hansen * Protection Keys are not available in 32-bit mode. 50406976945SDave Hansen */ 50506976945SDave Hansen static bool pku_disabled; 50606976945SDave Hansen 50706976945SDave Hansen static __always_inline void setup_pku(struct cpuinfo_x86 *c) 50806976945SDave Hansen { 5098a1dc55aSThomas Gleixner if (c == &boot_cpu_data) { 5108a1dc55aSThomas Gleixner if (pku_disabled || !cpu_feature_enabled(X86_FEATURE_PKU)) 511e8df1a95SDave Hansen return; 5128a1dc55aSThomas Gleixner /* 5138a1dc55aSThomas Gleixner * Setting CR4.PKE will cause the X86_FEATURE_OSPKE cpuid 5148a1dc55aSThomas Gleixner * bit to be set. Enforce it. 5158a1dc55aSThomas Gleixner */ 5168a1dc55aSThomas Gleixner setup_force_cpu_cap(X86_FEATURE_OSPKE); 51706976945SDave Hansen 5188a1dc55aSThomas Gleixner } else if (!cpu_feature_enabled(X86_FEATURE_OSPKE)) { 519e8df1a95SDave Hansen return; 5208a1dc55aSThomas Gleixner } 52106976945SDave Hansen 52206976945SDave Hansen cr4_set_bits(X86_CR4_PKE); 523fa8c84b7SThomas Gleixner /* Load the default PKRU value */ 524fa8c84b7SThomas Gleixner pkru_write_default(); 52506976945SDave Hansen } 52606976945SDave Hansen 52706976945SDave Hansen #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS 52806976945SDave Hansen static __init int setup_disable_pku(char *arg) 52906976945SDave Hansen { 53006976945SDave Hansen /* 53106976945SDave Hansen * Do not clear the X86_FEATURE_PKU bit. All of the 53206976945SDave Hansen * runtime checks are against OSPKE so clearing the 53306976945SDave Hansen * bit does nothing. 53406976945SDave Hansen * 53506976945SDave Hansen * This way, we will see "pku" in cpuinfo, but not 53606976945SDave Hansen * "ospke", which is exactly what we want. It shows 53706976945SDave Hansen * that the CPU has PKU, but the OS has not enabled it. 53806976945SDave Hansen * This happens to be exactly how a system would look 53906976945SDave Hansen * if we disabled the config option. 54006976945SDave Hansen */ 54106976945SDave Hansen pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n"); 54206976945SDave Hansen pku_disabled = true; 54306976945SDave Hansen return 1; 54406976945SDave Hansen } 54506976945SDave Hansen __setup("nopku", setup_disable_pku); 546d55dcb73SJuergen Gross #endif 54706976945SDave Hansen 548fe379fa4SPeter Zijlstra #ifdef CONFIG_X86_KERNEL_IBT 549fe379fa4SPeter Zijlstra 55093be2859SArd Biesheuvel __noendbr u64 ibt_save(bool disable) 551fe379fa4SPeter Zijlstra { 552fe379fa4SPeter Zijlstra u64 msr = 0; 553fe379fa4SPeter Zijlstra 554fe379fa4SPeter Zijlstra if (cpu_feature_enabled(X86_FEATURE_IBT)) { 555fe379fa4SPeter Zijlstra rdmsrl(MSR_IA32_S_CET, msr); 55693be2859SArd Biesheuvel if (disable) 557fe379fa4SPeter Zijlstra wrmsrl(MSR_IA32_S_CET, msr & ~CET_ENDBR_EN); 558fe379fa4SPeter Zijlstra } 559fe379fa4SPeter Zijlstra 560fe379fa4SPeter Zijlstra return msr; 561fe379fa4SPeter Zijlstra } 562fe379fa4SPeter Zijlstra 563fe379fa4SPeter Zijlstra __noendbr void ibt_restore(u64 save) 564fe379fa4SPeter Zijlstra { 565fe379fa4SPeter Zijlstra u64 msr; 566fe379fa4SPeter Zijlstra 567fe379fa4SPeter Zijlstra if (cpu_feature_enabled(X86_FEATURE_IBT)) { 568fe379fa4SPeter Zijlstra rdmsrl(MSR_IA32_S_CET, msr); 569fe379fa4SPeter Zijlstra msr &= ~CET_ENDBR_EN; 570fe379fa4SPeter Zijlstra msr |= (save & CET_ENDBR_EN); 571fe379fa4SPeter Zijlstra wrmsrl(MSR_IA32_S_CET, msr); 572fe379fa4SPeter Zijlstra } 573fe379fa4SPeter Zijlstra } 574fe379fa4SPeter Zijlstra 575fe379fa4SPeter Zijlstra #endif 576fe379fa4SPeter Zijlstra 577991625f3SPeter Zijlstra static __always_inline void setup_cet(struct cpuinfo_x86 *c) 578991625f3SPeter Zijlstra { 5790dc2a760SRick Edgecombe bool user_shstk, kernel_ibt; 580991625f3SPeter Zijlstra 5810dc2a760SRick Edgecombe if (!IS_ENABLED(CONFIG_X86_CET)) 582991625f3SPeter Zijlstra return; 583991625f3SPeter Zijlstra 5840dc2a760SRick Edgecombe kernel_ibt = HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT); 5850dc2a760SRick Edgecombe user_shstk = cpu_feature_enabled(X86_FEATURE_SHSTK) && 5860dc2a760SRick Edgecombe IS_ENABLED(CONFIG_X86_USER_SHADOW_STACK); 5870dc2a760SRick Edgecombe 5880dc2a760SRick Edgecombe if (!kernel_ibt && !user_shstk) 5890dc2a760SRick Edgecombe return; 5900dc2a760SRick Edgecombe 5910dc2a760SRick Edgecombe if (user_shstk) 5920dc2a760SRick Edgecombe set_cpu_cap(c, X86_FEATURE_USER_SHSTK); 5930dc2a760SRick Edgecombe 5940dc2a760SRick Edgecombe if (kernel_ibt) 5950dc2a760SRick Edgecombe wrmsrl(MSR_IA32_S_CET, CET_ENDBR_EN); 5960dc2a760SRick Edgecombe else 5970dc2a760SRick Edgecombe wrmsrl(MSR_IA32_S_CET, 0); 5980dc2a760SRick Edgecombe 599991625f3SPeter Zijlstra cr4_set_bits(X86_CR4_CET); 600991625f3SPeter Zijlstra 601c6cfcbd8SJosh Poimboeuf if (kernel_ibt && ibt_selftest()) { 602991625f3SPeter Zijlstra pr_err("IBT selftest: Failed!\n"); 603931ab636SPeter Zijlstra wrmsrl(MSR_IA32_S_CET, 0); 604991625f3SPeter Zijlstra setup_clear_cpu_cap(X86_FEATURE_IBT); 605991625f3SPeter Zijlstra } 606991625f3SPeter Zijlstra } 607991625f3SPeter Zijlstra 608af227003SPeter Zijlstra __noendbr void cet_disable(void) 609af227003SPeter Zijlstra { 6100dc2a760SRick Edgecombe if (!(cpu_feature_enabled(X86_FEATURE_IBT) || 6110dc2a760SRick Edgecombe cpu_feature_enabled(X86_FEATURE_SHSTK))) 6120dc2a760SRick Edgecombe return; 6130dc2a760SRick Edgecombe 614af227003SPeter Zijlstra wrmsrl(MSR_IA32_S_CET, 0); 6150dc2a760SRick Edgecombe wrmsrl(MSR_IA32_U_CET, 0); 616af227003SPeter Zijlstra } 617af227003SPeter Zijlstra 61806976945SDave Hansen /* 619b38b0665SH. Peter Anvin * Some CPU features depend on higher CPUID levels, which may not always 620b38b0665SH. Peter Anvin * be available due to CPUID level capping or broken virtualization 621b38b0665SH. Peter Anvin * software. Add those features to this table to auto-disable them. 622b38b0665SH. Peter Anvin */ 623b38b0665SH. Peter Anvin struct cpuid_dependent_feature { 624b38b0665SH. Peter Anvin u32 feature; 625b38b0665SH. Peter Anvin u32 level; 626b38b0665SH. Peter Anvin }; 6270f3fa48aSIngo Molnar 628148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature 629b38b0665SH. Peter Anvin cpuid_dependent_features[] = { 630b38b0665SH. Peter Anvin { X86_FEATURE_MWAIT, 0x00000005 }, 631b38b0665SH. Peter Anvin { X86_FEATURE_DCA, 0x00000009 }, 632b38b0665SH. Peter Anvin { X86_FEATURE_XSAVE, 0x0000000d }, 633b38b0665SH. Peter Anvin { 0, 0 } 634b38b0665SH. Peter Anvin }; 635b38b0665SH. Peter Anvin 636148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) 637b38b0665SH. Peter Anvin { 638b38b0665SH. Peter Anvin const struct cpuid_dependent_feature *df; 6399766cdbcSJaswinder Singh Rajput 640b38b0665SH. Peter Anvin for (df = cpuid_dependent_features; df->feature; df++) { 6410f3fa48aSIngo Molnar 6420f3fa48aSIngo Molnar if (!cpu_has(c, df->feature)) 6430f3fa48aSIngo Molnar continue; 644b38b0665SH. Peter Anvin /* 645b38b0665SH. Peter Anvin * Note: cpuid_level is set to -1 if unavailable, but 646b38b0665SH. Peter Anvin * extended_extended_level is set to 0 if unavailable 647b38b0665SH. Peter Anvin * and the legitimate extended levels are all negative 648b38b0665SH. Peter Anvin * when signed; hence the weird messing around with 649b38b0665SH. Peter Anvin * signs here... 650b38b0665SH. Peter Anvin */ 6510f3fa48aSIngo Molnar if (!((s32)df->level < 0 ? 652f6db44dfSYinghai Lu (u32)df->level > (u32)c->extended_cpuid_level : 6530f3fa48aSIngo Molnar (s32)df->level > (s32)c->cpuid_level)) 6540f3fa48aSIngo Molnar continue; 6550f3fa48aSIngo Molnar 656b38b0665SH. Peter Anvin clear_cpu_cap(c, df->feature); 6570f3fa48aSIngo Molnar if (!warn) 6580f3fa48aSIngo Molnar continue; 6590f3fa48aSIngo Molnar 6601b74dde7SChen Yucong pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n", 6619def39beSJosh Triplett x86_cap_flag(df->feature), df->level); 662b38b0665SH. Peter Anvin } 663b38b0665SH. Peter Anvin } 664b38b0665SH. Peter Anvin 665b38b0665SH. Peter Anvin /* 666f7627e25SThomas Gleixner * Naming convention should be: <Name> [(<Codename>)] 667f7627e25SThomas Gleixner * This table only is used unless init_<vendor>() below doesn't set it; 6680f3fa48aSIngo Molnar * in particular, if CPUID levels 0x80000002..4 are supported, this 6690f3fa48aSIngo Molnar * isn't used 670f7627e25SThomas Gleixner */ 671f7627e25SThomas Gleixner 672f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */ 673148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c) 674f7627e25SThomas Gleixner { 67509dc68d9SJan Beulich #ifdef CONFIG_X86_32 67609dc68d9SJan Beulich const struct legacy_cpu_model_info *info; 677f7627e25SThomas Gleixner 678f7627e25SThomas Gleixner if (c->x86_model >= 16) 679f7627e25SThomas Gleixner return NULL; /* Range check */ 680f7627e25SThomas Gleixner 681f7627e25SThomas Gleixner if (!this_cpu) 682f7627e25SThomas Gleixner return NULL; 683f7627e25SThomas Gleixner 68409dc68d9SJan Beulich info = this_cpu->legacy_models; 685f7627e25SThomas Gleixner 68609dc68d9SJan Beulich while (info->family) { 687f7627e25SThomas Gleixner if (info->family == c->x86) 688f7627e25SThomas Gleixner return info->model_names[c->x86_model]; 689f7627e25SThomas Gleixner info++; 690f7627e25SThomas Gleixner } 69109dc68d9SJan Beulich #endif 692f7627e25SThomas Gleixner return NULL; /* Not found */ 693f7627e25SThomas Gleixner } 694f7627e25SThomas Gleixner 695f6a892ddSFenghua Yu /* Aligned to unsigned long to avoid split lock in atomic bitmap ops */ 696f6a892ddSFenghua Yu __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long)); 697f6a892ddSFenghua Yu __u32 cpu_caps_set[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long)); 698f7627e25SThomas Gleixner 69972f5e08dSAndy Lutomirski #ifdef CONFIG_X86_32 70072f5e08dSAndy Lutomirski /* The 32-bit entry code needs to find cpu_entry_area. */ 70172f5e08dSAndy Lutomirski DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area); 70272f5e08dSAndy Lutomirski #endif 70372f5e08dSAndy Lutomirski 70445fc8757SThomas Garnier /* Load the original GDT from the per-cpu structure */ 70545fc8757SThomas Garnier void load_direct_gdt(int cpu) 70645fc8757SThomas Garnier { 70745fc8757SThomas Garnier struct desc_ptr gdt_descr; 70845fc8757SThomas Garnier 70945fc8757SThomas Garnier gdt_descr.address = (long)get_cpu_gdt_rw(cpu); 71045fc8757SThomas Garnier gdt_descr.size = GDT_SIZE - 1; 71145fc8757SThomas Garnier load_gdt(&gdt_descr); 71245fc8757SThomas Garnier } 71345fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_direct_gdt); 71445fc8757SThomas Garnier 71569218e47SThomas Garnier /* Load a fixmap remapping of the per-cpu GDT */ 71669218e47SThomas Garnier void load_fixmap_gdt(int cpu) 71769218e47SThomas Garnier { 71869218e47SThomas Garnier struct desc_ptr gdt_descr; 71969218e47SThomas Garnier 72069218e47SThomas Garnier gdt_descr.address = (long)get_cpu_gdt_ro(cpu); 72169218e47SThomas Garnier gdt_descr.size = GDT_SIZE - 1; 72269218e47SThomas Garnier load_gdt(&gdt_descr); 72369218e47SThomas Garnier } 72445fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_fixmap_gdt); 72569218e47SThomas Garnier 726b5636d45SThomas Gleixner /** 7271f19e2d5SThomas Gleixner * switch_gdt_and_percpu_base - Switch to direct GDT and runtime per CPU base 728b5636d45SThomas Gleixner * @cpu: The CPU number for which this is invoked 729b5636d45SThomas Gleixner * 7301f19e2d5SThomas Gleixner * Invoked during early boot to switch from early GDT and early per CPU to 7311f19e2d5SThomas Gleixner * the direct GDT and the runtime per CPU area. On 32-bit the percpu base 7321f19e2d5SThomas Gleixner * switch is implicit by loading the direct GDT. On 64bit this requires 7331f19e2d5SThomas Gleixner * to update GSBASE. 7340f3fa48aSIngo Molnar */ 7351f19e2d5SThomas Gleixner void __init switch_gdt_and_percpu_base(int cpu) 7369d31d35bSYinghai Lu { 73745fc8757SThomas Garnier load_direct_gdt(cpu); 738b5636d45SThomas Gleixner 739b5636d45SThomas Gleixner #ifdef CONFIG_X86_64 740b5636d45SThomas Gleixner /* 741b5636d45SThomas Gleixner * No need to load %gs. It is already correct. 742b5636d45SThomas Gleixner * 743b5636d45SThomas Gleixner * Writing %gs on 64bit would zero GSBASE which would make any per 744b5636d45SThomas Gleixner * CPU operation up to the point of the wrmsrl() fault. 745b5636d45SThomas Gleixner * 746b5636d45SThomas Gleixner * Set GSBASE to the new offset. Until the wrmsrl() happens the 747b5636d45SThomas Gleixner * early mapping is still valid. That means the GSBASE update will 748b5636d45SThomas Gleixner * lose any prior per CPU data which was not copied over in 749b5636d45SThomas Gleixner * setup_per_cpu_areas(). 7502cb15faaSThomas Gleixner * 7512cb15faaSThomas Gleixner * This works even with stackprotector enabled because the 7522cb15faaSThomas Gleixner * per CPU stack canary is 0 in both per CPU areas. 753b5636d45SThomas Gleixner */ 754b5636d45SThomas Gleixner wrmsrl(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu)); 755b5636d45SThomas Gleixner #else 756b5636d45SThomas Gleixner /* 757b5636d45SThomas Gleixner * %fs is already set to __KERNEL_PERCPU, but after switching GDT 758b5636d45SThomas Gleixner * it is required to load FS again so that the 'hidden' part is 759b5636d45SThomas Gleixner * updated from the new GDT. Up to this point the early per CPU 760b5636d45SThomas Gleixner * translation is active. Any content of the early per CPU data 761b5636d45SThomas Gleixner * which was not copied over in setup_per_cpu_areas() is lost. 762b5636d45SThomas Gleixner */ 763b5636d45SThomas Gleixner loadsegment(fs, __KERNEL_PERCPU); 764b5636d45SThomas Gleixner #endif 7659d31d35bSYinghai Lu } 7669d31d35bSYinghai Lu 767148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {}; 768f7627e25SThomas Gleixner 769148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c) 770f7627e25SThomas Gleixner { 771f7627e25SThomas Gleixner unsigned int *v; 772ee098e1aSBorislav Petkov char *p, *q, *s; 773f7627e25SThomas Gleixner 7743da99c97SYinghai Lu if (c->extended_cpuid_level < 0x80000004) 7751b05d60dSYinghai Lu return; 776f7627e25SThomas Gleixner 777f7627e25SThomas Gleixner v = (unsigned int *)c->x86_model_id; 778f7627e25SThomas Gleixner cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]); 779f7627e25SThomas Gleixner cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]); 780f7627e25SThomas Gleixner cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]); 781f7627e25SThomas Gleixner c->x86_model_id[48] = 0; 782f7627e25SThomas Gleixner 783ee098e1aSBorislav Petkov /* Trim whitespace */ 784ee098e1aSBorislav Petkov p = q = s = &c->x86_model_id[0]; 785ee098e1aSBorislav Petkov 786ee098e1aSBorislav Petkov while (*p == ' ') 787ee098e1aSBorislav Petkov p++; 788ee098e1aSBorislav Petkov 789ee098e1aSBorislav Petkov while (*p) { 790ee098e1aSBorislav Petkov /* Note the last non-whitespace index */ 791ee098e1aSBorislav Petkov if (!isspace(*p)) 792ee098e1aSBorislav Petkov s = q; 793ee098e1aSBorislav Petkov 794ee098e1aSBorislav Petkov *q++ = *p++; 795ee098e1aSBorislav Petkov } 796ee098e1aSBorislav Petkov 797ee098e1aSBorislav Petkov *(s + 1) = '\0'; 798f7627e25SThomas Gleixner } 799f7627e25SThomas Gleixner 8009305bd6cSThomas Gleixner void detect_num_cpu_cores(struct cpuinfo_x86 *c) 8012cc61be6SDavid Wang { 8022cc61be6SDavid Wang unsigned int eax, ebx, ecx, edx; 8032cc61be6SDavid Wang 8049305bd6cSThomas Gleixner c->x86_max_cores = 1; 8052cc61be6SDavid Wang if (!IS_ENABLED(CONFIG_SMP) || c->cpuid_level < 4) 8069305bd6cSThomas Gleixner return; 8072cc61be6SDavid Wang 8082cc61be6SDavid Wang cpuid_count(4, 0, &eax, &ebx, &ecx, &edx); 8092cc61be6SDavid Wang if (eax & 0x1f) 8109305bd6cSThomas Gleixner c->x86_max_cores = (eax >> 26) + 1; 8112cc61be6SDavid Wang } 8122cc61be6SDavid Wang 813148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c) 814f7627e25SThomas Gleixner { 8159d31d35bSYinghai Lu unsigned int n, dummy, ebx, ecx, edx, l2size; 816f7627e25SThomas Gleixner 8173da99c97SYinghai Lu n = c->extended_cpuid_level; 818f7627e25SThomas Gleixner 819f7627e25SThomas Gleixner if (n >= 0x80000005) { 8209d31d35bSYinghai Lu cpuid(0x80000005, &dummy, &ebx, &ecx, &edx); 821f7627e25SThomas Gleixner c->x86_cache_size = (ecx>>24) + (edx>>24); 822140fc727SYinghai Lu #ifdef CONFIG_X86_64 823140fc727SYinghai Lu /* On K8 L1 TLB is inclusive, so don't count it */ 824140fc727SYinghai Lu c->x86_tlbsize = 0; 825140fc727SYinghai Lu #endif 826f7627e25SThomas Gleixner } 827f7627e25SThomas Gleixner 828f7627e25SThomas Gleixner if (n < 0x80000006) /* Some chips just has a large L1. */ 829f7627e25SThomas Gleixner return; 830f7627e25SThomas Gleixner 8310a488a53SYinghai Lu cpuid(0x80000006, &dummy, &ebx, &ecx, &edx); 832f7627e25SThomas Gleixner l2size = ecx >> 16; 833f7627e25SThomas Gleixner 834140fc727SYinghai Lu #ifdef CONFIG_X86_64 835140fc727SYinghai Lu c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff); 836140fc727SYinghai Lu #else 837f7627e25SThomas Gleixner /* do processor-specific cache resizing */ 83809dc68d9SJan Beulich if (this_cpu->legacy_cache_size) 83909dc68d9SJan Beulich l2size = this_cpu->legacy_cache_size(c, l2size); 840f7627e25SThomas Gleixner 841f7627e25SThomas Gleixner /* Allow user to override all this if necessary. */ 842f7627e25SThomas Gleixner if (cachesize_override != -1) 843f7627e25SThomas Gleixner l2size = cachesize_override; 844f7627e25SThomas Gleixner 845f7627e25SThomas Gleixner if (l2size == 0) 846f7627e25SThomas Gleixner return; /* Again, no L2 cache is possible */ 847140fc727SYinghai Lu #endif 848f7627e25SThomas Gleixner 849f7627e25SThomas Gleixner c->x86_cache_size = l2size; 850f7627e25SThomas Gleixner } 851f7627e25SThomas Gleixner 852e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO]; 853e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO]; 854e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO]; 855e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO]; 856e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO]; 857e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO]; 858dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO]; 859e0ba94f1SAlex Shi 860f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c) 861e0ba94f1SAlex Shi { 862e0ba94f1SAlex Shi if (this_cpu->c_detect_tlb) 863e0ba94f1SAlex Shi this_cpu->c_detect_tlb(c); 864e0ba94f1SAlex Shi 865f94fe119SSteven Honeyman pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n", 866e0ba94f1SAlex Shi tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES], 867f94fe119SSteven Honeyman tlb_lli_4m[ENTRIES]); 868f94fe119SSteven Honeyman 869f94fe119SSteven Honeyman pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n", 870f94fe119SSteven Honeyman tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES], 871f94fe119SSteven Honeyman tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]); 872e0ba94f1SAlex Shi } 873e0ba94f1SAlex Shi 874545401f4SThomas Gleixner int detect_ht_early(struct cpuinfo_x86 *c) 8759d31d35bSYinghai Lu { 876c8e56d20SBorislav Petkov #ifdef CONFIG_SMP 8779d31d35bSYinghai Lu u32 eax, ebx, ecx, edx; 8789d31d35bSYinghai Lu 8790a488a53SYinghai Lu if (!cpu_has(c, X86_FEATURE_HT)) 880545401f4SThomas Gleixner return -1; 8819d31d35bSYinghai Lu 8820a488a53SYinghai Lu if (cpu_has(c, X86_FEATURE_CMP_LEGACY)) 883545401f4SThomas Gleixner return -1; 8840a488a53SYinghai Lu 8851cd78776SYinghai Lu if (cpu_has(c, X86_FEATURE_XTOPOLOGY)) 886545401f4SThomas Gleixner return -1; 8871cd78776SYinghai Lu 8880a488a53SYinghai Lu cpuid(1, &eax, &ebx, &ecx, &edx); 8890a488a53SYinghai Lu 8909d31d35bSYinghai Lu smp_num_siblings = (ebx & 0xff0000) >> 16; 891545401f4SThomas Gleixner if (smp_num_siblings == 1) 8921b74dde7SChen Yucong pr_info_once("CPU0: Hyper-Threading is disabled\n"); 893545401f4SThomas Gleixner #endif 894545401f4SThomas Gleixner return 0; 8950f3fa48aSIngo Molnar } 8960f3fa48aSIngo Molnar 897545401f4SThomas Gleixner void detect_ht(struct cpuinfo_x86 *c) 898545401f4SThomas Gleixner { 899545401f4SThomas Gleixner #ifdef CONFIG_SMP 900545401f4SThomas Gleixner int index_msb, core_bits; 901545401f4SThomas Gleixner 902545401f4SThomas Gleixner if (detect_ht_early(c) < 0) 903545401f4SThomas Gleixner return; 9049d31d35bSYinghai Lu 9059d31d35bSYinghai Lu index_msb = get_count_order(smp_num_siblings); 90602fb601dSThomas Gleixner c->topo.pkg_id = apic->phys_pkg_id(c->topo.initial_apicid, index_msb); 9079d31d35bSYinghai Lu 9089d31d35bSYinghai Lu smp_num_siblings = smp_num_siblings / c->x86_max_cores; 9099d31d35bSYinghai Lu 9109d31d35bSYinghai Lu index_msb = get_count_order(smp_num_siblings); 9119d31d35bSYinghai Lu 9129d31d35bSYinghai Lu core_bits = get_count_order(c->x86_max_cores); 9139d31d35bSYinghai Lu 914e9525633SThomas Gleixner c->topo.core_id = apic->phys_pkg_id(c->topo.initial_apicid, index_msb) & 9151cd78776SYinghai Lu ((1 << core_bits) - 1); 9169d31d35bSYinghai Lu #endif 91797e4db7cSYinghai Lu } 918f7627e25SThomas Gleixner 919148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c) 920f7627e25SThomas Gleixner { 921f7627e25SThomas Gleixner char *v = c->x86_vendor_id; 9220f3fa48aSIngo Molnar int i; 923f7627e25SThomas Gleixner 924f7627e25SThomas Gleixner for (i = 0; i < X86_VENDOR_NUM; i++) { 92510a434fcSYinghai Lu if (!cpu_devs[i]) 92610a434fcSYinghai Lu break; 92710a434fcSYinghai Lu 928f7627e25SThomas Gleixner if (!strcmp(v, cpu_devs[i]->c_ident[0]) || 929f7627e25SThomas Gleixner (cpu_devs[i]->c_ident[1] && 930f7627e25SThomas Gleixner !strcmp(v, cpu_devs[i]->c_ident[1]))) { 9310f3fa48aSIngo Molnar 932f7627e25SThomas Gleixner this_cpu = cpu_devs[i]; 93310a434fcSYinghai Lu c->x86_vendor = this_cpu->c_x86_vendor; 934f7627e25SThomas Gleixner return; 935f7627e25SThomas Gleixner } 936f7627e25SThomas Gleixner } 93710a434fcSYinghai Lu 9381b74dde7SChen Yucong pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \ 939a9c56953SMinchan Kim "CPU: Your system may be unstable.\n", v); 94010a434fcSYinghai Lu 941f7627e25SThomas Gleixner c->x86_vendor = X86_VENDOR_UNKNOWN; 942f7627e25SThomas Gleixner this_cpu = &default_cpu; 943f7627e25SThomas Gleixner } 944f7627e25SThomas Gleixner 945148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c) 946f7627e25SThomas Gleixner { 947f7627e25SThomas Gleixner /* Get vendor name */ 9484a148513SHarvey Harrison cpuid(0x00000000, (unsigned int *)&c->cpuid_level, 9494a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[0], 9504a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[8], 9514a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[4]); 952f7627e25SThomas Gleixner 953f7627e25SThomas Gleixner c->x86 = 4; 9549d31d35bSYinghai Lu /* Intel-defined flags: level 0x00000001 */ 955f7627e25SThomas Gleixner if (c->cpuid_level >= 0x00000001) { 956f7627e25SThomas Gleixner u32 junk, tfms, cap0, misc; 9570f3fa48aSIngo Molnar 958f7627e25SThomas Gleixner cpuid(0x00000001, &tfms, &misc, &junk, &cap0); 95999f925ceSBorislav Petkov c->x86 = x86_family(tfms); 96099f925ceSBorislav Petkov c->x86_model = x86_model(tfms); 961b399151cSJia Zhang c->x86_stepping = x86_stepping(tfms); 9620f3fa48aSIngo Molnar 963d4387bd3SHuang, Ying if (cap0 & (1<<19)) { 964d4387bd3SHuang, Ying c->x86_clflush_size = ((misc >> 8) & 0xff) * 8; 9659d31d35bSYinghai Lu c->x86_cache_alignment = c->x86_clflush_size; 966d4387bd3SHuang, Ying } 967f7627e25SThomas Gleixner } 968f7627e25SThomas Gleixner } 9693da99c97SYinghai Lu 9708bf1ebcaSAndy Lutomirski static void apply_forced_caps(struct cpuinfo_x86 *c) 9718bf1ebcaSAndy Lutomirski { 9728bf1ebcaSAndy Lutomirski int i; 9738bf1ebcaSAndy Lutomirski 9746cbd2171SThomas Gleixner for (i = 0; i < NCAPINTS + NBUGINTS; i++) { 9758bf1ebcaSAndy Lutomirski c->x86_capability[i] &= ~cpu_caps_cleared[i]; 9768bf1ebcaSAndy Lutomirski c->x86_capability[i] |= cpu_caps_set[i]; 9778bf1ebcaSAndy Lutomirski } 9788bf1ebcaSAndy Lutomirski } 9798bf1ebcaSAndy Lutomirski 9807fcae111SDavid Woodhouse static void init_speculation_control(struct cpuinfo_x86 *c) 9817fcae111SDavid Woodhouse { 9827fcae111SDavid Woodhouse /* 9837fcae111SDavid Woodhouse * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support, 9847fcae111SDavid Woodhouse * and they also have a different bit for STIBP support. Also, 9857fcae111SDavid Woodhouse * a hypervisor might have set the individual AMD bits even on 9867fcae111SDavid Woodhouse * Intel CPUs, for finer-grained selection of what's available. 9877fcae111SDavid Woodhouse */ 9887fcae111SDavid Woodhouse if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) { 9897fcae111SDavid Woodhouse set_cpu_cap(c, X86_FEATURE_IBRS); 9907fcae111SDavid Woodhouse set_cpu_cap(c, X86_FEATURE_IBPB); 9917eb8956aSThomas Gleixner set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL); 9927fcae111SDavid Woodhouse } 993e7c587daSBorislav Petkov 9947fcae111SDavid Woodhouse if (cpu_has(c, X86_FEATURE_INTEL_STIBP)) 9957fcae111SDavid Woodhouse set_cpu_cap(c, X86_FEATURE_STIBP); 996e7c587daSBorislav Petkov 997bc226f07STom Lendacky if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) || 998bc226f07STom Lendacky cpu_has(c, X86_FEATURE_VIRT_SSBD)) 99952817587SThomas Gleixner set_cpu_cap(c, X86_FEATURE_SSBD); 100052817587SThomas Gleixner 10017eb8956aSThomas Gleixner if (cpu_has(c, X86_FEATURE_AMD_IBRS)) { 1002e7c587daSBorislav Petkov set_cpu_cap(c, X86_FEATURE_IBRS); 10037eb8956aSThomas Gleixner set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL); 10047eb8956aSThomas Gleixner } 1005e7c587daSBorislav Petkov 1006e7c587daSBorislav Petkov if (cpu_has(c, X86_FEATURE_AMD_IBPB)) 1007e7c587daSBorislav Petkov set_cpu_cap(c, X86_FEATURE_IBPB); 1008e7c587daSBorislav Petkov 10097eb8956aSThomas Gleixner if (cpu_has(c, X86_FEATURE_AMD_STIBP)) { 1010e7c587daSBorislav Petkov set_cpu_cap(c, X86_FEATURE_STIBP); 10117eb8956aSThomas Gleixner set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL); 10127eb8956aSThomas Gleixner } 10136ac2f49eSKonrad Rzeszutek Wilk 10146ac2f49eSKonrad Rzeszutek Wilk if (cpu_has(c, X86_FEATURE_AMD_SSBD)) { 10156ac2f49eSKonrad Rzeszutek Wilk set_cpu_cap(c, X86_FEATURE_SSBD); 10166ac2f49eSKonrad Rzeszutek Wilk set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL); 10176ac2f49eSKonrad Rzeszutek Wilk clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD); 10186ac2f49eSKonrad Rzeszutek Wilk } 10197fcae111SDavid Woodhouse } 10207fcae111SDavid Woodhouse 1021148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c) 1022093af8d7SYinghai Lu { 102339c06df4SBorislav Petkov u32 eax, ebx, ecx, edx; 1024093af8d7SYinghai Lu 1025093af8d7SYinghai Lu /* Intel-defined flags: level 0x00000001 */ 1026093af8d7SYinghai Lu if (c->cpuid_level >= 0x00000001) { 102739c06df4SBorislav Petkov cpuid(0x00000001, &eax, &ebx, &ecx, &edx); 10280f3fa48aSIngo Molnar 102939c06df4SBorislav Petkov c->x86_capability[CPUID_1_ECX] = ecx; 103039c06df4SBorislav Petkov c->x86_capability[CPUID_1_EDX] = edx; 1031093af8d7SYinghai Lu } 1032093af8d7SYinghai Lu 10333df8d920SAndy Lutomirski /* Thermal and Power Management Leaf: level 0x00000006 (eax) */ 10343df8d920SAndy Lutomirski if (c->cpuid_level >= 0x00000006) 10353df8d920SAndy Lutomirski c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006); 10363df8d920SAndy Lutomirski 1037bdc802dcSH. Peter Anvin /* Additional Intel-defined flags: level 0x00000007 */ 1038bdc802dcSH. Peter Anvin if (c->cpuid_level >= 0x00000007) { 1039bdc802dcSH. Peter Anvin cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx); 104039c06df4SBorislav Petkov c->x86_capability[CPUID_7_0_EBX] = ebx; 1041dfb4a70fSDave Hansen c->x86_capability[CPUID_7_ECX] = ecx; 104295ca0ee8SDavid Woodhouse c->x86_capability[CPUID_7_EDX] = edx; 1043b302e4b1SFenghua Yu 1044b302e4b1SFenghua Yu /* Check valid sub-leaf index before accessing it */ 1045b302e4b1SFenghua Yu if (eax >= 1) { 1046b302e4b1SFenghua Yu cpuid_count(0x00000007, 1, &eax, &ebx, &ecx, &edx); 1047b302e4b1SFenghua Yu c->x86_capability[CPUID_7_1_EAX] = eax; 1048b302e4b1SFenghua Yu } 1049bdc802dcSH. Peter Anvin } 1050bdc802dcSH. Peter Anvin 10516229ad27SFenghua Yu /* Extended state features: level 0x0000000d */ 10526229ad27SFenghua Yu if (c->cpuid_level >= 0x0000000d) { 10536229ad27SFenghua Yu cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx); 10546229ad27SFenghua Yu 105539c06df4SBorislav Petkov c->x86_capability[CPUID_D_1_EAX] = eax; 10566229ad27SFenghua Yu } 10576229ad27SFenghua Yu 1058093af8d7SYinghai Lu /* AMD-defined flags: level 0x80000001 */ 105939c06df4SBorislav Petkov eax = cpuid_eax(0x80000000); 106039c06df4SBorislav Petkov c->extended_cpuid_level = eax; 10610f3fa48aSIngo Molnar 106239c06df4SBorislav Petkov if ((eax & 0xffff0000) == 0x80000000) { 106339c06df4SBorislav Petkov if (eax >= 0x80000001) { 106439c06df4SBorislav Petkov cpuid(0x80000001, &eax, &ebx, &ecx, &edx); 106539c06df4SBorislav Petkov 106639c06df4SBorislav Petkov c->x86_capability[CPUID_8000_0001_ECX] = ecx; 106739c06df4SBorislav Petkov c->x86_capability[CPUID_8000_0001_EDX] = edx; 1068093af8d7SYinghai Lu } 1069093af8d7SYinghai Lu } 1070093af8d7SYinghai Lu 107171faad43SYazen Ghannam if (c->extended_cpuid_level >= 0x80000007) { 107271faad43SYazen Ghannam cpuid(0x80000007, &eax, &ebx, &ecx, &edx); 107371faad43SYazen Ghannam 107471faad43SYazen Ghannam c->x86_capability[CPUID_8000_0007_EBX] = ebx; 107571faad43SYazen Ghannam c->x86_power = edx; 107671faad43SYazen Ghannam } 107771faad43SYazen Ghannam 1078c65732e4SThomas Gleixner if (c->extended_cpuid_level >= 0x80000008) { 1079c65732e4SThomas Gleixner cpuid(0x80000008, &eax, &ebx, &ecx, &edx); 1080c65732e4SThomas Gleixner c->x86_capability[CPUID_8000_0008_EBX] = ebx; 1081c65732e4SThomas Gleixner } 1082c65732e4SThomas Gleixner 10832ccd71f1SBorislav Petkov if (c->extended_cpuid_level >= 0x8000000a) 108439c06df4SBorislav Petkov c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a); 10852ccd71f1SBorislav Petkov 1086fb35d30fSSean Christopherson if (c->extended_cpuid_level >= 0x8000001f) 1087fb35d30fSSean Christopherson c->x86_capability[CPUID_8000_001F_EAX] = cpuid_eax(0x8000001f); 1088fb35d30fSSean Christopherson 10898415a748SKim Phillips if (c->extended_cpuid_level >= 0x80000021) 10908415a748SKim Phillips c->x86_capability[CPUID_8000_0021_EAX] = cpuid_eax(0x80000021); 10918415a748SKim Phillips 10921dedefd1SJacob Pan init_scattered_cpuid_features(c); 10937fcae111SDavid Woodhouse init_speculation_control(c); 109460d34501SAndy Lutomirski 109560d34501SAndy Lutomirski /* 109660d34501SAndy Lutomirski * Clear/Set all flags overridden by options, after probe. 109760d34501SAndy Lutomirski * This needs to happen each time we re-probe, which may happen 109860d34501SAndy Lutomirski * several times during CPU initialization. 109960d34501SAndy Lutomirski */ 110060d34501SAndy Lutomirski apply_forced_caps(c); 1101093af8d7SYinghai Lu } 1102093af8d7SYinghai Lu 1103405c018aSM. Vefa Bicakci void get_cpu_address_sizes(struct cpuinfo_x86 *c) 1104d94a155cSKirill A. Shutemov { 1105d94a155cSKirill A. Shutemov u32 eax, ebx, ecx, edx; 1106fbf6449fSAdam Dunlap bool vp_bits_from_cpuid = true; 1107d94a155cSKirill A. Shutemov 1108fbf6449fSAdam Dunlap if (!cpu_has(c, X86_FEATURE_CPUID) || 1109fbf6449fSAdam Dunlap (c->extended_cpuid_level < 0x80000008)) 1110fbf6449fSAdam Dunlap vp_bits_from_cpuid = false; 1111fbf6449fSAdam Dunlap 1112fbf6449fSAdam Dunlap if (vp_bits_from_cpuid) { 1113d94a155cSKirill A. Shutemov cpuid(0x80000008, &eax, &ebx, &ecx, &edx); 1114d94a155cSKirill A. Shutemov 1115d94a155cSKirill A. Shutemov c->x86_virt_bits = (eax >> 8) & 0xff; 1116d94a155cSKirill A. Shutemov c->x86_phys_bits = eax & 0xff; 1117fbf6449fSAdam Dunlap } else { 1118fbf6449fSAdam Dunlap if (IS_ENABLED(CONFIG_X86_64)) { 1119fbf6449fSAdam Dunlap c->x86_clflush_size = 64; 1120d94a155cSKirill A. Shutemov c->x86_phys_bits = 36; 1121fbf6449fSAdam Dunlap c->x86_virt_bits = 48; 1122fbf6449fSAdam Dunlap } else { 1123fbf6449fSAdam Dunlap c->x86_clflush_size = 32; 1124fbf6449fSAdam Dunlap c->x86_virt_bits = 32; 1125fbf6449fSAdam Dunlap c->x86_phys_bits = 32; 1126fbf6449fSAdam Dunlap 1127fbf6449fSAdam Dunlap if (cpu_has(c, X86_FEATURE_PAE) || 1128fbf6449fSAdam Dunlap cpu_has(c, X86_FEATURE_PSE36)) 1129fbf6449fSAdam Dunlap c->x86_phys_bits = 36; 1130fbf6449fSAdam Dunlap } 1131fbf6449fSAdam Dunlap } 1132cc51e542SAndi Kleen c->x86_cache_bits = c->x86_phys_bits; 11333e325526SDave Hansen c->x86_cache_alignment = c->x86_clflush_size; 1134d94a155cSKirill A. Shutemov } 1135d94a155cSKirill A. Shutemov 1136148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c) 1137aef93c8bSYinghai Lu { 1138aef93c8bSYinghai Lu #ifdef CONFIG_X86_32 1139aef93c8bSYinghai Lu int i; 1140aef93c8bSYinghai Lu 1141aef93c8bSYinghai Lu /* 1142aef93c8bSYinghai Lu * First of all, decide if this is a 486 or higher 1143aef93c8bSYinghai Lu * It's a 486 if we can modify the AC flag 1144aef93c8bSYinghai Lu */ 1145aef93c8bSYinghai Lu if (flag_is_changeable_p(X86_EFLAGS_AC)) 1146aef93c8bSYinghai Lu c->x86 = 4; 1147aef93c8bSYinghai Lu else 1148aef93c8bSYinghai Lu c->x86 = 3; 1149aef93c8bSYinghai Lu 1150aef93c8bSYinghai Lu for (i = 0; i < X86_VENDOR_NUM; i++) 1151aef93c8bSYinghai Lu if (cpu_devs[i] && cpu_devs[i]->c_identify) { 1152aef93c8bSYinghai Lu c->x86_vendor_id[0] = 0; 1153aef93c8bSYinghai Lu cpu_devs[i]->c_identify(c); 1154aef93c8bSYinghai Lu if (c->x86_vendor_id[0]) { 1155aef93c8bSYinghai Lu get_cpu_vendor(c); 1156aef93c8bSYinghai Lu break; 1157aef93c8bSYinghai Lu } 1158aef93c8bSYinghai Lu } 1159aef93c8bSYinghai Lu #endif 1160093af8d7SYinghai Lu } 1161f7627e25SThomas Gleixner 116236ad3513SThomas Gleixner #define NO_SPECULATION BIT(0) 116336ad3513SThomas Gleixner #define NO_MELTDOWN BIT(1) 116436ad3513SThomas Gleixner #define NO_SSB BIT(2) 116536ad3513SThomas Gleixner #define NO_L1TF BIT(3) 1166ed5194c2SAndi Kleen #define NO_MDS BIT(4) 1167e261f209SThomas Gleixner #define MSBDS_ONLY BIT(5) 1168f36cf386SThomas Gleixner #define NO_SWAPGS BIT(6) 1169db4d30fbSVineela Tummalapalli #define NO_ITLB_MULTIHIT BIT(7) 11701e41a766STony W Wang-oc #define NO_SPECTRE_V2 BIT(8) 11717df54884SPawan Gupta #define NO_MMIO BIT(9) 11727df54884SPawan Gupta #define NO_EIBRS_PBRSB BIT(10) 117336ad3513SThomas Gleixner 1174f6d502fcSThomas Gleixner #define VULNWL(vendor, family, model, whitelist) \ 1175f6d502fcSThomas Gleixner X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, whitelist) 117636ad3513SThomas Gleixner 117736ad3513SThomas Gleixner #define VULNWL_INTEL(model, whitelist) \ 117836ad3513SThomas Gleixner VULNWL(INTEL, 6, INTEL_FAM6_##model, whitelist) 117936ad3513SThomas Gleixner 118036ad3513SThomas Gleixner #define VULNWL_AMD(family, whitelist) \ 118136ad3513SThomas Gleixner VULNWL(AMD, family, X86_MODEL_ANY, whitelist) 118236ad3513SThomas Gleixner 118336ad3513SThomas Gleixner #define VULNWL_HYGON(family, whitelist) \ 118436ad3513SThomas Gleixner VULNWL(HYGON, family, X86_MODEL_ANY, whitelist) 118536ad3513SThomas Gleixner 118636ad3513SThomas Gleixner static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = { 118736ad3513SThomas Gleixner VULNWL(ANY, 4, X86_MODEL_ANY, NO_SPECULATION), 118836ad3513SThomas Gleixner VULNWL(CENTAUR, 5, X86_MODEL_ANY, NO_SPECULATION), 118936ad3513SThomas Gleixner VULNWL(INTEL, 5, X86_MODEL_ANY, NO_SPECULATION), 119036ad3513SThomas Gleixner VULNWL(NSC, 5, X86_MODEL_ANY, NO_SPECULATION), 1191639475d4SMarcos Del Sol Vives VULNWL(VORTEX, 5, X86_MODEL_ANY, NO_SPECULATION), 1192639475d4SMarcos Del Sol Vives VULNWL(VORTEX, 6, X86_MODEL_ANY, NO_SPECULATION), 119336ad3513SThomas Gleixner 1194ed5194c2SAndi Kleen /* Intel Family 6 */ 11957df54884SPawan Gupta VULNWL_INTEL(TIGERLAKE, NO_MMIO), 11967df54884SPawan Gupta VULNWL_INTEL(TIGERLAKE_L, NO_MMIO), 11977df54884SPawan Gupta VULNWL_INTEL(ALDERLAKE, NO_MMIO), 11987df54884SPawan Gupta VULNWL_INTEL(ALDERLAKE_L, NO_MMIO), 11997df54884SPawan Gupta 1200db4d30fbSVineela Tummalapalli VULNWL_INTEL(ATOM_SALTWELL, NO_SPECULATION | NO_ITLB_MULTIHIT), 1201db4d30fbSVineela Tummalapalli VULNWL_INTEL(ATOM_SALTWELL_TABLET, NO_SPECULATION | NO_ITLB_MULTIHIT), 1202db4d30fbSVineela Tummalapalli VULNWL_INTEL(ATOM_SALTWELL_MID, NO_SPECULATION | NO_ITLB_MULTIHIT), 1203db4d30fbSVineela Tummalapalli VULNWL_INTEL(ATOM_BONNELL, NO_SPECULATION | NO_ITLB_MULTIHIT), 1204db4d30fbSVineela Tummalapalli VULNWL_INTEL(ATOM_BONNELL_MID, NO_SPECULATION | NO_ITLB_MULTIHIT), 120536ad3513SThomas Gleixner 1206db4d30fbSVineela Tummalapalli VULNWL_INTEL(ATOM_SILVERMONT, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1207db4d30fbSVineela Tummalapalli VULNWL_INTEL(ATOM_SILVERMONT_D, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1208db4d30fbSVineela Tummalapalli VULNWL_INTEL(ATOM_SILVERMONT_MID, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1209db4d30fbSVineela Tummalapalli VULNWL_INTEL(ATOM_AIRMONT, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1210db4d30fbSVineela Tummalapalli VULNWL_INTEL(XEON_PHI_KNL, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1211db4d30fbSVineela Tummalapalli VULNWL_INTEL(XEON_PHI_KNM, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 121236ad3513SThomas Gleixner 121336ad3513SThomas Gleixner VULNWL_INTEL(CORE_YONAH, NO_SSB), 121436ad3513SThomas Gleixner 1215db4d30fbSVineela Tummalapalli VULNWL_INTEL(ATOM_AIRMONT_MID, NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT), 1216db4d30fbSVineela Tummalapalli VULNWL_INTEL(ATOM_AIRMONT_NP, NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT), 121736ad3513SThomas Gleixner 12187df54884SPawan Gupta VULNWL_INTEL(ATOM_GOLDMONT, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO), 12197df54884SPawan Gupta VULNWL_INTEL(ATOM_GOLDMONT_D, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO), 12207df54884SPawan Gupta VULNWL_INTEL(ATOM_GOLDMONT_PLUS, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB), 1221f36cf386SThomas Gleixner 1222f36cf386SThomas Gleixner /* 1223f36cf386SThomas Gleixner * Technically, swapgs isn't serializing on AMD (despite it previously 1224f36cf386SThomas Gleixner * being documented as such in the APM). But according to AMD, %gs is 1225f36cf386SThomas Gleixner * updated non-speculatively, and the issuing of %gs-relative memory 1226f36cf386SThomas Gleixner * operands will be blocked until the %gs update completes, which is 1227f36cf386SThomas Gleixner * good enough for our purposes. 1228f36cf386SThomas Gleixner */ 1229ed5194c2SAndi Kleen 12302b129932SDaniel Sneddon VULNWL_INTEL(ATOM_TREMONT, NO_EIBRS_PBRSB), 12312b129932SDaniel Sneddon VULNWL_INTEL(ATOM_TREMONT_L, NO_EIBRS_PBRSB), 12322b129932SDaniel Sneddon VULNWL_INTEL(ATOM_TREMONT_D, NO_ITLB_MULTIHIT | NO_EIBRS_PBRSB), 1233cad14885SPawan Gupta 1234ed5194c2SAndi Kleen /* AMD Family 0xf - 0x12 */ 12357df54884SPawan Gupta VULNWL_AMD(0x0f, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO), 12367df54884SPawan Gupta VULNWL_AMD(0x10, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO), 12377df54884SPawan Gupta VULNWL_AMD(0x11, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO), 12387df54884SPawan Gupta VULNWL_AMD(0x12, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO), 123936ad3513SThomas Gleixner 124036ad3513SThomas Gleixner /* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */ 1241e7862edaSKim Phillips VULNWL_AMD(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB), 1242e7862edaSKim Phillips VULNWL_HYGON(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB), 12431e41a766STony W Wang-oc 12441e41a766STony W Wang-oc /* Zhaoxin Family 7 */ 12457df54884SPawan Gupta VULNWL(CENTAUR, 7, X86_MODEL_ANY, NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO), 12467df54884SPawan Gupta VULNWL(ZHAOXIN, 7, X86_MODEL_ANY, NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO), 1247fec9434aSDavid Woodhouse {} 1248fec9434aSDavid Woodhouse }; 1249fec9434aSDavid Woodhouse 12506b80b59bSAlexandre Chartre #define VULNBL(vendor, family, model, blacklist) \ 12516b80b59bSAlexandre Chartre X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, blacklist) 12526b80b59bSAlexandre Chartre 12537e5b3c26SMark Gross #define VULNBL_INTEL_STEPPINGS(model, steppings, issues) \ 12547e5b3c26SMark Gross X86_MATCH_VENDOR_FAM_MODEL_STEPPINGS_FEATURE(INTEL, 6, \ 12557e5b3c26SMark Gross INTEL_FAM6_##model, steppings, \ 12567e5b3c26SMark Gross X86_FEATURE_ANY, issues) 12577e5b3c26SMark Gross 12586b80b59bSAlexandre Chartre #define VULNBL_AMD(family, blacklist) \ 12596b80b59bSAlexandre Chartre VULNBL(AMD, family, X86_MODEL_ANY, blacklist) 12606b80b59bSAlexandre Chartre 12616b80b59bSAlexandre Chartre #define VULNBL_HYGON(family, blacklist) \ 12626b80b59bSAlexandre Chartre VULNBL(HYGON, family, X86_MODEL_ANY, blacklist) 12636b80b59bSAlexandre Chartre 12647e5b3c26SMark Gross #define SRBDS BIT(0) 126551802186SPawan Gupta /* CPU is affected by X86_BUG_MMIO_STALE_DATA */ 126651802186SPawan Gupta #define MMIO BIT(1) 1267a992b8a4SPawan Gupta /* CPU is affected by Shared Buffers Data Sampling (SBDS), a variant of X86_BUG_MMIO_STALE_DATA */ 1268a992b8a4SPawan Gupta #define MMIO_SBDS BIT(2) 12696b80b59bSAlexandre Chartre /* CPU is affected by RETbleed, speculating where you would not expect it */ 12706b80b59bSAlexandre Chartre #define RETBLEED BIT(3) 1271be8de49bSTom Lendacky /* CPU is affected by SMT (cross-thread) return predictions */ 1272be8de49bSTom Lendacky #define SMT_RSB BIT(4) 1273fb3bd914SBorislav Petkov (AMD) /* CPU is affected by SRSO */ 1274fb3bd914SBorislav Petkov (AMD) #define SRSO BIT(5) 12758974eb58SDaniel Sneddon /* CPU is affected by GDS */ 127664094e7eSLinus Torvalds #define GDS BIT(6) 12777e5b3c26SMark Gross 12787e5b3c26SMark Gross static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = { 12797e5b3c26SMark Gross VULNBL_INTEL_STEPPINGS(IVYBRIDGE, X86_STEPPING_ANY, SRBDS), 12807e5b3c26SMark Gross VULNBL_INTEL_STEPPINGS(HASWELL, X86_STEPPING_ANY, SRBDS), 12817e5b3c26SMark Gross VULNBL_INTEL_STEPPINGS(HASWELL_L, X86_STEPPING_ANY, SRBDS), 12827e5b3c26SMark Gross VULNBL_INTEL_STEPPINGS(HASWELL_G, X86_STEPPING_ANY, SRBDS), 12837a05bc95SPeter Zijlstra VULNBL_INTEL_STEPPINGS(HASWELL_X, X86_STEPPING_ANY, MMIO), 12847a05bc95SPeter Zijlstra VULNBL_INTEL_STEPPINGS(BROADWELL_D, X86_STEPPING_ANY, MMIO), 12857e5b3c26SMark Gross VULNBL_INTEL_STEPPINGS(BROADWELL_G, X86_STEPPING_ANY, SRBDS), 128651802186SPawan Gupta VULNBL_INTEL_STEPPINGS(BROADWELL_X, X86_STEPPING_ANY, MMIO), 12877e5b3c26SMark Gross VULNBL_INTEL_STEPPINGS(BROADWELL, X86_STEPPING_ANY, SRBDS), 12888974eb58SDaniel Sneddon VULNBL_INTEL_STEPPINGS(SKYLAKE_X, X86_STEPPING_ANY, MMIO | RETBLEED | GDS), 1289c9f4c45cSDave Hansen VULNBL_INTEL_STEPPINGS(SKYLAKE_L, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS), 1290c9f4c45cSDave Hansen VULNBL_INTEL_STEPPINGS(SKYLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS), 1291c9f4c45cSDave Hansen VULNBL_INTEL_STEPPINGS(KABYLAKE_L, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS), 1292c9f4c45cSDave Hansen VULNBL_INTEL_STEPPINGS(KABYLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS), 1293f54d4537SPawan Gupta VULNBL_INTEL_STEPPINGS(CANNONLAKE_L, X86_STEPPING_ANY, RETBLEED), 12948974eb58SDaniel Sneddon VULNBL_INTEL_STEPPINGS(ICELAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS), 12958974eb58SDaniel Sneddon VULNBL_INTEL_STEPPINGS(ICELAKE_D, X86_STEPPING_ANY, MMIO | GDS), 12968974eb58SDaniel Sneddon VULNBL_INTEL_STEPPINGS(ICELAKE_X, X86_STEPPING_ANY, MMIO | GDS), 12978974eb58SDaniel Sneddon VULNBL_INTEL_STEPPINGS(COMETLAKE, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS), 12986ad0ad2bSPeter Zijlstra VULNBL_INTEL_STEPPINGS(COMETLAKE_L, X86_STEPPINGS(0x0, 0x0), MMIO | RETBLEED), 12998974eb58SDaniel Sneddon VULNBL_INTEL_STEPPINGS(COMETLAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS), 13008974eb58SDaniel Sneddon VULNBL_INTEL_STEPPINGS(TIGERLAKE_L, X86_STEPPING_ANY, GDS), 13018974eb58SDaniel Sneddon VULNBL_INTEL_STEPPINGS(TIGERLAKE, X86_STEPPING_ANY, GDS), 13027a05bc95SPeter Zijlstra VULNBL_INTEL_STEPPINGS(LAKEFIELD, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED), 13038974eb58SDaniel Sneddon VULNBL_INTEL_STEPPINGS(ROCKETLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS), 13047a05bc95SPeter Zijlstra VULNBL_INTEL_STEPPINGS(ATOM_TREMONT, X86_STEPPING_ANY, MMIO | MMIO_SBDS), 130551802186SPawan Gupta VULNBL_INTEL_STEPPINGS(ATOM_TREMONT_D, X86_STEPPING_ANY, MMIO), 13067a05bc95SPeter Zijlstra VULNBL_INTEL_STEPPINGS(ATOM_TREMONT_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS), 13076b80b59bSAlexandre Chartre 13086b80b59bSAlexandre Chartre VULNBL_AMD(0x15, RETBLEED), 13096b80b59bSAlexandre Chartre VULNBL_AMD(0x16, RETBLEED), 1310fb3bd914SBorislav Petkov (AMD) VULNBL_AMD(0x17, RETBLEED | SMT_RSB | SRSO), 1311a5ef7d68SPu Wen VULNBL_HYGON(0x18, RETBLEED | SMT_RSB | SRSO), 1312fb3bd914SBorislav Petkov (AMD) VULNBL_AMD(0x19, SRSO), 13137e5b3c26SMark Gross {} 13147e5b3c26SMark Gross }; 13157e5b3c26SMark Gross 131693920f61SMark Gross static bool __init cpu_matches(const struct x86_cpu_id *table, unsigned long which) 131736ad3513SThomas Gleixner { 131893920f61SMark Gross const struct x86_cpu_id *m = x86_match_cpu(table); 1319fec9434aSDavid Woodhouse 132036ad3513SThomas Gleixner return m && !!(m->driver_data & which); 132136ad3513SThomas Gleixner } 132217dbca11SAndi Kleen 1323286836a7SPawan Gupta u64 x86_read_arch_cap_msr(void) 1324fec9434aSDavid Woodhouse { 1325fec9434aSDavid Woodhouse u64 ia32_cap = 0; 1326fec9434aSDavid Woodhouse 1327286836a7SPawan Gupta if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) 1328286836a7SPawan Gupta rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap); 1329286836a7SPawan Gupta 1330286836a7SPawan Gupta return ia32_cap; 1331286836a7SPawan Gupta } 1332286836a7SPawan Gupta 133351802186SPawan Gupta static bool arch_cap_mmio_immune(u64 ia32_cap) 133451802186SPawan Gupta { 133551802186SPawan Gupta return (ia32_cap & ARCH_CAP_FBSDP_NO && 133651802186SPawan Gupta ia32_cap & ARCH_CAP_PSDP_NO && 133751802186SPawan Gupta ia32_cap & ARCH_CAP_SBDR_SSDP_NO); 133851802186SPawan Gupta } 133951802186SPawan Gupta 1340286836a7SPawan Gupta static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c) 1341286836a7SPawan Gupta { 1342286836a7SPawan Gupta u64 ia32_cap = x86_read_arch_cap_msr(); 1343286836a7SPawan Gupta 1344db4d30fbSVineela Tummalapalli /* Set ITLB_MULTIHIT bug if cpu is not in the whitelist and not mitigated */ 134593920f61SMark Gross if (!cpu_matches(cpu_vuln_whitelist, NO_ITLB_MULTIHIT) && 134693920f61SMark Gross !(ia32_cap & ARCH_CAP_PSCHANGE_MC_NO)) 1347db4d30fbSVineela Tummalapalli setup_force_cpu_bug(X86_BUG_ITLB_MULTIHIT); 1348db4d30fbSVineela Tummalapalli 134993920f61SMark Gross if (cpu_matches(cpu_vuln_whitelist, NO_SPECULATION)) 13508ecc4979SDominik Brodowski return; 13518ecc4979SDominik Brodowski 13528ecc4979SDominik Brodowski setup_force_cpu_bug(X86_BUG_SPECTRE_V1); 13531e41a766STony W Wang-oc 135493920f61SMark Gross if (!cpu_matches(cpu_vuln_whitelist, NO_SPECTRE_V2)) 13558ecc4979SDominik Brodowski setup_force_cpu_bug(X86_BUG_SPECTRE_V2); 13568ecc4979SDominik Brodowski 135793920f61SMark Gross if (!cpu_matches(cpu_vuln_whitelist, NO_SSB) && 135893920f61SMark Gross !(ia32_cap & ARCH_CAP_SSB_NO) && 135924809860SKonrad Rzeszutek Wilk !cpu_has(c, X86_FEATURE_AMD_SSB_NO)) 1360c456442cSKonrad Rzeszutek Wilk setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS); 1361c456442cSKonrad Rzeszutek Wilk 1362e7862edaSKim Phillips /* 1363e7862edaSKim Phillips * AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature 1364e7862edaSKim Phillips * flag and protect from vendor-specific bugs via the whitelist. 1365e7862edaSKim Phillips */ 1366e7862edaSKim Phillips if ((ia32_cap & ARCH_CAP_IBRS_ALL) || cpu_has(c, X86_FEATURE_AUTOIBRS)) { 1367706d5168SSai Praneeth setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED); 1368e7862edaSKim Phillips if (!cpu_matches(cpu_vuln_whitelist, NO_EIBRS_PBRSB) && 1369e7862edaSKim Phillips !(ia32_cap & ARCH_CAP_PBRSB_NO)) 1370e7862edaSKim Phillips setup_force_cpu_bug(X86_BUG_EIBRS_PBRSB); 1371e7862edaSKim Phillips } 1372706d5168SSai Praneeth 137393920f61SMark Gross if (!cpu_matches(cpu_vuln_whitelist, NO_MDS) && 137493920f61SMark Gross !(ia32_cap & ARCH_CAP_MDS_NO)) { 1375ed5194c2SAndi Kleen setup_force_cpu_bug(X86_BUG_MDS); 137693920f61SMark Gross if (cpu_matches(cpu_vuln_whitelist, MSBDS_ONLY)) 1377e261f209SThomas Gleixner setup_force_cpu_bug(X86_BUG_MSBDS_ONLY); 1378e261f209SThomas Gleixner } 1379ed5194c2SAndi Kleen 138093920f61SMark Gross if (!cpu_matches(cpu_vuln_whitelist, NO_SWAPGS)) 1381f36cf386SThomas Gleixner setup_force_cpu_bug(X86_BUG_SWAPGS); 1382f36cf386SThomas Gleixner 13831b42f017SPawan Gupta /* 13841b42f017SPawan Gupta * When the CPU is not mitigated for TAA (TAA_NO=0) set TAA bug when: 13851b42f017SPawan Gupta * - TSX is supported or 13861b42f017SPawan Gupta * - TSX_CTRL is present 13871b42f017SPawan Gupta * 13881b42f017SPawan Gupta * TSX_CTRL check is needed for cases when TSX could be disabled before 13891b42f017SPawan Gupta * the kernel boot e.g. kexec. 13901b42f017SPawan Gupta * TSX_CTRL check alone is not sufficient for cases when the microcode 13911b42f017SPawan Gupta * update is not present or running as guest that don't get TSX_CTRL. 13921b42f017SPawan Gupta */ 13931b42f017SPawan Gupta if (!(ia32_cap & ARCH_CAP_TAA_NO) && 13941b42f017SPawan Gupta (cpu_has(c, X86_FEATURE_RTM) || 13951b42f017SPawan Gupta (ia32_cap & ARCH_CAP_TSX_CTRL_MSR))) 13961b42f017SPawan Gupta setup_force_cpu_bug(X86_BUG_TAA); 13971b42f017SPawan Gupta 13987e5b3c26SMark Gross /* 13997e5b3c26SMark Gross * SRBDS affects CPUs which support RDRAND or RDSEED and are listed 14007e5b3c26SMark Gross * in the vulnerability blacklist. 1401a992b8a4SPawan Gupta * 1402a992b8a4SPawan Gupta * Some of the implications and mitigation of Shared Buffers Data 1403a992b8a4SPawan Gupta * Sampling (SBDS) are similar to SRBDS. Give SBDS same treatment as 1404a992b8a4SPawan Gupta * SRBDS. 14057e5b3c26SMark Gross */ 14067e5b3c26SMark Gross if ((cpu_has(c, X86_FEATURE_RDRAND) || 14077e5b3c26SMark Gross cpu_has(c, X86_FEATURE_RDSEED)) && 1408a992b8a4SPawan Gupta cpu_matches(cpu_vuln_blacklist, SRBDS | MMIO_SBDS)) 14097e5b3c26SMark Gross setup_force_cpu_bug(X86_BUG_SRBDS); 14107e5b3c26SMark Gross 141151802186SPawan Gupta /* 141251802186SPawan Gupta * Processor MMIO Stale Data bug enumeration 141351802186SPawan Gupta * 141451802186SPawan Gupta * Affected CPU list is generally enough to enumerate the vulnerability, 141551802186SPawan Gupta * but for virtualization case check for ARCH_CAP MSR bits also, VMM may 141651802186SPawan Gupta * not want the guest to enumerate the bug. 14177df54884SPawan Gupta * 14187df54884SPawan Gupta * Set X86_BUG_MMIO_UNKNOWN for CPUs that are neither in the blacklist, 14197df54884SPawan Gupta * nor in the whitelist and also don't enumerate MSR ARCH_CAP MMIO bits. 142051802186SPawan Gupta */ 14217df54884SPawan Gupta if (!arch_cap_mmio_immune(ia32_cap)) { 14227df54884SPawan Gupta if (cpu_matches(cpu_vuln_blacklist, MMIO)) 142351802186SPawan Gupta setup_force_cpu_bug(X86_BUG_MMIO_STALE_DATA); 14247df54884SPawan Gupta else if (!cpu_matches(cpu_vuln_whitelist, NO_MMIO)) 14257df54884SPawan Gupta setup_force_cpu_bug(X86_BUG_MMIO_UNKNOWN); 14267df54884SPawan Gupta } 142751802186SPawan Gupta 142826aae8ccSAndrew Cooper if (!cpu_has(c, X86_FEATURE_BTC_NO)) { 142926aae8ccSAndrew Cooper if (cpu_matches(cpu_vuln_blacklist, RETBLEED) || (ia32_cap & ARCH_CAP_RSBA)) 14306b80b59bSAlexandre Chartre setup_force_cpu_bug(X86_BUG_RETBLEED); 143126aae8ccSAndrew Cooper } 14326b80b59bSAlexandre Chartre 1433be8de49bSTom Lendacky if (cpu_matches(cpu_vuln_blacklist, SMT_RSB)) 1434be8de49bSTom Lendacky setup_force_cpu_bug(X86_BUG_SMT_RSB); 1435be8de49bSTom Lendacky 14361b5277c0SBorislav Petkov (AMD) if (!cpu_has(c, X86_FEATURE_SRSO_NO)) { 1437fb3bd914SBorislav Petkov (AMD) if (cpu_matches(cpu_vuln_blacklist, SRSO)) 1438fb3bd914SBorislav Petkov (AMD) setup_force_cpu_bug(X86_BUG_SRSO); 14391b5277c0SBorislav Petkov (AMD) } 1440fb3bd914SBorislav Petkov (AMD) 14418974eb58SDaniel Sneddon /* 14428974eb58SDaniel Sneddon * Check if CPU is vulnerable to GDS. If running in a virtual machine on 14438974eb58SDaniel Sneddon * an affected processor, the VMM may have disabled the use of GATHER by 14448974eb58SDaniel Sneddon * disabling AVX2. The only way to do this in HW is to clear XCR0[2], 14458974eb58SDaniel Sneddon * which means that AVX will be disabled. 14468974eb58SDaniel Sneddon */ 14478974eb58SDaniel Sneddon if (cpu_matches(cpu_vuln_blacklist, GDS) && !(ia32_cap & ARCH_CAP_GDS_NO) && 14488974eb58SDaniel Sneddon boot_cpu_has(X86_FEATURE_AVX)) 14498974eb58SDaniel Sneddon setup_force_cpu_bug(X86_BUG_GDS); 14508974eb58SDaniel Sneddon 145193920f61SMark Gross if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN)) 14524a28bfe3SKonrad Rzeszutek Wilk return; 1453fec9434aSDavid Woodhouse 1454fec9434aSDavid Woodhouse /* Rogue Data Cache Load? No! */ 1455fec9434aSDavid Woodhouse if (ia32_cap & ARCH_CAP_RDCL_NO) 14564a28bfe3SKonrad Rzeszutek Wilk return; 1457fec9434aSDavid Woodhouse 14584a28bfe3SKonrad Rzeszutek Wilk setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN); 145917dbca11SAndi Kleen 146093920f61SMark Gross if (cpu_matches(cpu_vuln_whitelist, NO_L1TF)) 146117dbca11SAndi Kleen return; 146217dbca11SAndi Kleen 146317dbca11SAndi Kleen setup_force_cpu_bug(X86_BUG_L1TF); 1464fec9434aSDavid Woodhouse } 1465fec9434aSDavid Woodhouse 146634048c9eSPaolo Ciarrocchi /* 14678990cac6SPavel Tatashin * The NOPL instruction is supposed to exist on all CPUs of family >= 6; 14688990cac6SPavel Tatashin * unfortunately, that's not true in practice because of early VIA 14698990cac6SPavel Tatashin * chips and (more importantly) broken virtualizers that are not easy 14708990cac6SPavel Tatashin * to detect. In the latter case it doesn't even *fail* reliably, so 14718990cac6SPavel Tatashin * probing for it doesn't even work. Disable it completely on 32-bit 14728990cac6SPavel Tatashin * unless we can find a reliable way to detect all the broken cases. 14738990cac6SPavel Tatashin * Enable it explicitly on 64-bit for non-constant inputs of cpu_has(). 14748990cac6SPavel Tatashin */ 14759b3661cdSBorislav Petkov static void detect_nopl(void) 14768990cac6SPavel Tatashin { 14778990cac6SPavel Tatashin #ifdef CONFIG_X86_32 14789b3661cdSBorislav Petkov setup_clear_cpu_cap(X86_FEATURE_NOPL); 14798990cac6SPavel Tatashin #else 14809b3661cdSBorislav Petkov setup_force_cpu_cap(X86_FEATURE_NOPL); 14818990cac6SPavel Tatashin #endif 14828990cac6SPavel Tatashin } 14838990cac6SPavel Tatashin 14848990cac6SPavel Tatashin /* 14851ef5423aSMike Hommey * We parse cpu parameters early because fpu__init_system() is executed 14861ef5423aSMike Hommey * before parse_early_param(). 14871ef5423aSMike Hommey */ 14881ef5423aSMike Hommey static void __init cpu_parse_early_param(void) 14891ef5423aSMike Hommey { 14901ef5423aSMike Hommey char arg[128]; 14911625c833SBorislav Petkov char *argptr = arg, *opt; 14921625c833SBorislav Petkov int arglen, taint = 0; 14931ef5423aSMike Hommey 14941ef5423aSMike Hommey #ifdef CONFIG_X86_32 14951ef5423aSMike Hommey if (cmdline_find_option_bool(boot_command_line, "no387")) 14961ef5423aSMike Hommey #ifdef CONFIG_MATH_EMULATION 14971ef5423aSMike Hommey setup_clear_cpu_cap(X86_FEATURE_FPU); 14981ef5423aSMike Hommey #else 14991ef5423aSMike Hommey pr_err("Option 'no387' required CONFIG_MATH_EMULATION enabled.\n"); 15001ef5423aSMike Hommey #endif 15011ef5423aSMike Hommey 15021ef5423aSMike Hommey if (cmdline_find_option_bool(boot_command_line, "nofxsr")) 15031ef5423aSMike Hommey setup_clear_cpu_cap(X86_FEATURE_FXSR); 15041ef5423aSMike Hommey #endif 15051ef5423aSMike Hommey 15061ef5423aSMike Hommey if (cmdline_find_option_bool(boot_command_line, "noxsave")) 15071ef5423aSMike Hommey setup_clear_cpu_cap(X86_FEATURE_XSAVE); 15081ef5423aSMike Hommey 15091ef5423aSMike Hommey if (cmdline_find_option_bool(boot_command_line, "noxsaveopt")) 15101ef5423aSMike Hommey setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT); 15111ef5423aSMike Hommey 15121ef5423aSMike Hommey if (cmdline_find_option_bool(boot_command_line, "noxsaves")) 15131ef5423aSMike Hommey setup_clear_cpu_cap(X86_FEATURE_XSAVES); 15141ef5423aSMike Hommey 15150dc2a760SRick Edgecombe if (cmdline_find_option_bool(boot_command_line, "nousershstk")) 15160dc2a760SRick Edgecombe setup_clear_cpu_cap(X86_FEATURE_USER_SHSTK); 15170dc2a760SRick Edgecombe 15181ef5423aSMike Hommey arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg)); 15191ef5423aSMike Hommey if (arglen <= 0) 15201ef5423aSMike Hommey return; 15211ef5423aSMike Hommey 15221ef5423aSMike Hommey pr_info("Clearing CPUID bits:"); 15231ef5423aSMike Hommey 15241625c833SBorislav Petkov while (argptr) { 15251625c833SBorislav Petkov bool found __maybe_unused = false; 15261625c833SBorislav Petkov unsigned int bit; 15271ef5423aSMike Hommey 15281625c833SBorislav Petkov opt = strsep(&argptr, ","); 15291625c833SBorislav Petkov 15301625c833SBorislav Petkov /* 15311625c833SBorislav Petkov * Handle naked numbers first for feature flags which don't 15321625c833SBorislav Petkov * have names. 15331625c833SBorislav Petkov */ 15341625c833SBorislav Petkov if (!kstrtouint(opt, 10, &bit)) { 15351625c833SBorislav Petkov if (bit < NCAPINTS * 32) { 15361625c833SBorislav Petkov 15371625c833SBorislav Petkov /* empty-string, i.e., ""-defined feature flags */ 15381625c833SBorislav Petkov if (!x86_cap_flags[bit]) 15391625c833SBorislav Petkov pr_cont(" " X86_CAP_FMT_NUM, x86_cap_flag_num(bit)); 15401625c833SBorislav Petkov else 15411ef5423aSMike Hommey pr_cont(" " X86_CAP_FMT, x86_cap_flag(bit)); 15421625c833SBorislav Petkov 15431ef5423aSMike Hommey setup_clear_cpu_cap(bit); 15441625c833SBorislav Petkov taint++; 15451ef5423aSMike Hommey } 15461625c833SBorislav Petkov /* 15471625c833SBorislav Petkov * The assumption is that there are no feature names with only 15481625c833SBorislav Petkov * numbers in the name thus go to the next argument. 15491625c833SBorislav Petkov */ 15501625c833SBorislav Petkov continue; 15511625c833SBorislav Petkov } 15521625c833SBorislav Petkov 15531625c833SBorislav Petkov for (bit = 0; bit < 32 * NCAPINTS; bit++) { 15541625c833SBorislav Petkov if (!x86_cap_flag(bit)) 15551625c833SBorislav Petkov continue; 15561625c833SBorislav Petkov 15571625c833SBorislav Petkov if (strcmp(x86_cap_flag(bit), opt)) 15581625c833SBorislav Petkov continue; 15591625c833SBorislav Petkov 15601625c833SBorislav Petkov pr_cont(" %s", opt); 15611625c833SBorislav Petkov setup_clear_cpu_cap(bit); 15621625c833SBorislav Petkov taint++; 15631625c833SBorislav Petkov found = true; 15641625c833SBorislav Petkov break; 15651625c833SBorislav Petkov } 15661625c833SBorislav Petkov 15671625c833SBorislav Petkov if (!found) 15681625c833SBorislav Petkov pr_cont(" (unknown: %s)", opt); 15691625c833SBorislav Petkov } 15701ef5423aSMike Hommey pr_cont("\n"); 15711625c833SBorislav Petkov 15721625c833SBorislav Petkov if (taint) 15731625c833SBorislav Petkov add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); 15741ef5423aSMike Hommey } 15751ef5423aSMike Hommey 15761ef5423aSMike Hommey /* 157734048c9eSPaolo Ciarrocchi * Do minimum CPU detection early. 157834048c9eSPaolo Ciarrocchi * Fields really needed: vendor, cpuid_level, family, model, mask, 157934048c9eSPaolo Ciarrocchi * cache alignment. 158034048c9eSPaolo Ciarrocchi * The others are not touched to avoid unwanted side effects. 158134048c9eSPaolo Ciarrocchi * 1582a1652bb8SJean Delvare * WARNING: this function is only called on the boot CPU. Don't add code 1583a1652bb8SJean Delvare * here that is supposed to run on all CPUs. 158434048c9eSPaolo Ciarrocchi */ 15853da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c) 1586f7627e25SThomas Gleixner { 15870e96f31eSJordan Borgner memset(&c->x86_capability, 0, sizeof(c->x86_capability)); 15880a488a53SYinghai Lu c->extended_cpuid_level = 0; 15890a488a53SYinghai Lu 15902893cc8fSMatthew Whitehead if (!have_cpuid_p()) 15912893cc8fSMatthew Whitehead identify_cpu_without_cpuid(c); 15922893cc8fSMatthew Whitehead 1593aef93c8bSYinghai Lu /* cyrix could have cpuid enabled via c_identify()*/ 159405fb3c19SAndy Lutomirski if (have_cpuid_p()) { 1595f7627e25SThomas Gleixner cpu_detect(c); 15963da99c97SYinghai Lu get_cpu_vendor(c); 15973da99c97SYinghai Lu get_cpu_cap(c); 159878d1b296SBorislav Petkov setup_force_cpu_cap(X86_FEATURE_CPUID); 15991ef5423aSMike Hommey cpu_parse_early_param(); 160012cf105cSKrzysztof Helt 160110a434fcSYinghai Lu if (this_cpu->c_early_init) 160210a434fcSYinghai Lu this_cpu->c_early_init(c); 16033da99c97SYinghai Lu 1604f6e9456cSRobert Richter c->cpu_index = 0; 1605b38b0665SH. Peter Anvin filter_cpuid_features(c, false); 1606de5397adSFenghua Yu 1607a110b5ecSBorislav Petkov if (this_cpu->c_bsp_init) 1608a110b5ecSBorislav Petkov this_cpu->c_bsp_init(c); 160978d1b296SBorislav Petkov } else { 161078d1b296SBorislav Petkov setup_clear_cpu_cap(X86_FEATURE_CPUID); 161105fb3c19SAndy Lutomirski } 1612c3b83598SBorislav Petkov 1613fbf6449fSAdam Dunlap get_cpu_address_sizes(c); 1614fbf6449fSAdam Dunlap 1615c3b83598SBorislav Petkov setup_force_cpu_cap(X86_FEATURE_ALWAYS); 1616a89f040fSThomas Gleixner 16174a28bfe3SKonrad Rzeszutek Wilk cpu_set_bug_bits(c); 161899c6fa25SDavid Woodhouse 1619ebb1064eSFenghua Yu sld_setup(c); 16206650cdd9SPeter Zijlstra (Intel) 1621b8b7abaeSAndy Lutomirski #ifdef CONFIG_X86_32 1622b8b7abaeSAndy Lutomirski /* 1623b8b7abaeSAndy Lutomirski * Regardless of whether PCID is enumerated, the SDM says 1624b8b7abaeSAndy Lutomirski * that it can't be enabled in 32-bit mode. 1625b8b7abaeSAndy Lutomirski */ 1626b8b7abaeSAndy Lutomirski setup_clear_cpu_cap(X86_FEATURE_PCID); 1627b8b7abaeSAndy Lutomirski #endif 1628372fddf7SKirill A. Shutemov 1629372fddf7SKirill A. Shutemov /* 1630372fddf7SKirill A. Shutemov * Later in the boot process pgtable_l5_enabled() relies on 1631372fddf7SKirill A. Shutemov * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not 1632372fddf7SKirill A. Shutemov * enabled by this point we need to clear the feature bit to avoid 1633372fddf7SKirill A. Shutemov * false-positives at the later stage. 1634372fddf7SKirill A. Shutemov * 1635372fddf7SKirill A. Shutemov * pgtable_l5_enabled() can be false here for several reasons: 1636372fddf7SKirill A. Shutemov * - 5-level paging is disabled compile-time; 1637372fddf7SKirill A. Shutemov * - it's 32-bit kernel; 1638372fddf7SKirill A. Shutemov * - machine doesn't support 5-level paging; 1639372fddf7SKirill A. Shutemov * - user specified 'no5lvl' in kernel command line. 1640372fddf7SKirill A. Shutemov */ 1641372fddf7SKirill A. Shutemov if (!pgtable_l5_enabled()) 1642372fddf7SKirill A. Shutemov setup_clear_cpu_cap(X86_FEATURE_LA57); 16438990cac6SPavel Tatashin 16449b3661cdSBorislav Petkov detect_nopl(); 1645f7627e25SThomas Gleixner } 1646f7627e25SThomas Gleixner 16479d31d35bSYinghai Lu void __init early_cpu_init(void) 16489d31d35bSYinghai Lu { 164902dde8b4SJan Beulich const struct cpu_dev *const *cdev; 165010a434fcSYinghai Lu int count = 0; 16519d31d35bSYinghai Lu 1652ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT 16531b74dde7SChen Yucong pr_info("KERNEL supported cpus:\n"); 165431c997caSIngo Molnar #endif 165531c997caSIngo Molnar 165610a434fcSYinghai Lu for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) { 165702dde8b4SJan Beulich const struct cpu_dev *cpudev = *cdev; 16589d31d35bSYinghai Lu 165910a434fcSYinghai Lu if (count >= X86_VENDOR_NUM) 166010a434fcSYinghai Lu break; 166110a434fcSYinghai Lu cpu_devs[count] = cpudev; 166210a434fcSYinghai Lu count++; 166310a434fcSYinghai Lu 1664ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT 166531c997caSIngo Molnar { 166631c997caSIngo Molnar unsigned int j; 166731c997caSIngo Molnar 166810a434fcSYinghai Lu for (j = 0; j < 2; j++) { 166910a434fcSYinghai Lu if (!cpudev->c_ident[j]) 167010a434fcSYinghai Lu continue; 16711b74dde7SChen Yucong pr_info(" %s %s\n", cpudev->c_vendor, 167210a434fcSYinghai Lu cpudev->c_ident[j]); 167310a434fcSYinghai Lu } 167410a434fcSYinghai Lu } 16750388423dSDave Jones #endif 167631c997caSIngo Molnar } 16779d31d35bSYinghai Lu early_identify_cpu(&boot_cpu_data); 1678f7627e25SThomas Gleixner } 1679f7627e25SThomas Gleixner 1680415de440SJane Malalane static bool detect_null_seg_behavior(void) 16817a5d6704SAndy Lutomirski { 168258a5aac5SAndy Lutomirski /* 16837a5d6704SAndy Lutomirski * Empirically, writing zero to a segment selector on AMD does 16847a5d6704SAndy Lutomirski * not clear the base, whereas writing zero to a segment 16857a5d6704SAndy Lutomirski * selector on Intel does clear the base. Intel's behavior 16867a5d6704SAndy Lutomirski * allows slightly faster context switches in the common case 16877a5d6704SAndy Lutomirski * where GS is unused by the prev and next threads. 168858a5aac5SAndy Lutomirski * 16897a5d6704SAndy Lutomirski * Since neither vendor documents this anywhere that I can see, 1690d9f6e12fSIngo Molnar * detect it directly instead of hard-coding the choice by 16917a5d6704SAndy Lutomirski * vendor. 16927a5d6704SAndy Lutomirski * 16937a5d6704SAndy Lutomirski * I've designated AMD's behavior as the "bug" because it's 16947a5d6704SAndy Lutomirski * counterintuitive and less friendly. 169558a5aac5SAndy Lutomirski */ 16967a5d6704SAndy Lutomirski 16977a5d6704SAndy Lutomirski unsigned long old_base, tmp; 16987a5d6704SAndy Lutomirski rdmsrl(MSR_FS_BASE, old_base); 16997a5d6704SAndy Lutomirski wrmsrl(MSR_FS_BASE, 1); 17007a5d6704SAndy Lutomirski loadsegment(fs, 0); 17017a5d6704SAndy Lutomirski rdmsrl(MSR_FS_BASE, tmp); 17027a5d6704SAndy Lutomirski wrmsrl(MSR_FS_BASE, old_base); 1703415de440SJane Malalane return tmp == 0; 1704415de440SJane Malalane } 1705415de440SJane Malalane 1706415de440SJane Malalane void check_null_seg_clears_base(struct cpuinfo_x86 *c) 1707415de440SJane Malalane { 1708415de440SJane Malalane /* BUG_NULL_SEG is only relevant with 64bit userspace */ 1709415de440SJane Malalane if (!IS_ENABLED(CONFIG_X86_64)) 1710415de440SJane Malalane return; 1711415de440SJane Malalane 17125b909d4aSKim Phillips if (cpu_has(c, X86_FEATURE_NULL_SEL_CLR_BASE)) 1713415de440SJane Malalane return; 1714415de440SJane Malalane 1715415de440SJane Malalane /* 1716415de440SJane Malalane * CPUID bit above wasn't set. If this kernel is still running 1717415de440SJane Malalane * as a HV guest, then the HV has decided not to advertize 1718415de440SJane Malalane * that CPUID bit for whatever reason. For example, one 1719415de440SJane Malalane * member of the migration pool might be vulnerable. Which 1720415de440SJane Malalane * means, the bug is present: set the BUG flag and return. 1721415de440SJane Malalane */ 1722415de440SJane Malalane if (cpu_has(c, X86_FEATURE_HYPERVISOR)) { 1723415de440SJane Malalane set_cpu_bug(c, X86_BUG_NULL_SEG); 1724415de440SJane Malalane return; 1725415de440SJane Malalane } 1726415de440SJane Malalane 1727415de440SJane Malalane /* 1728415de440SJane Malalane * Zen2 CPUs also have this behaviour, but no CPUID bit. 1729415de440SJane Malalane * 0x18 is the respective family for Hygon. 1730415de440SJane Malalane */ 1731415de440SJane Malalane if ((c->x86 == 0x17 || c->x86 == 0x18) && 1732415de440SJane Malalane detect_null_seg_behavior()) 1733415de440SJane Malalane return; 1734415de440SJane Malalane 1735415de440SJane Malalane /* All the remaining ones are affected */ 1736415de440SJane Malalane set_cpu_bug(c, X86_BUG_NULL_SEG); 1737f7627e25SThomas Gleixner } 1738f7627e25SThomas Gleixner 1739148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c) 1740f7627e25SThomas Gleixner { 17413da99c97SYinghai Lu c->extended_cpuid_level = 0; 1742f7627e25SThomas Gleixner 1743aef93c8bSYinghai Lu if (!have_cpuid_p()) 1744aef93c8bSYinghai Lu identify_cpu_without_cpuid(c); 1745f7627e25SThomas Gleixner 1746aef93c8bSYinghai Lu /* cyrix could have cpuid enabled via c_identify()*/ 1747a9853dd6SIngo Molnar if (!have_cpuid_p()) 1748aef93c8bSYinghai Lu return; 1749aef93c8bSYinghai Lu 17503da99c97SYinghai Lu cpu_detect(c); 17513da99c97SYinghai Lu 17523da99c97SYinghai Lu get_cpu_vendor(c); 17533da99c97SYinghai Lu 17543da99c97SYinghai Lu get_cpu_cap(c); 17553da99c97SYinghai Lu 1756d94a155cSKirill A. Shutemov get_cpu_address_sizes(c); 1757d94a155cSKirill A. Shutemov 1758f7627e25SThomas Gleixner if (c->cpuid_level >= 0x00000001) { 1759b9655e70SThomas Gleixner c->topo.initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF; 1760b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32 1761c8e56d20SBorislav Petkov # ifdef CONFIG_SMP 1762b9655e70SThomas Gleixner c->topo.apicid = apic->phys_pkg_id(c->topo.initial_apicid, 0); 1763f7627e25SThomas Gleixner # else 1764b9655e70SThomas Gleixner c->topo.apicid = c->topo.initial_apicid; 1765f7627e25SThomas Gleixner # endif 1766b89d3b3eSYinghai Lu #endif 176702fb601dSThomas Gleixner c->topo.pkg_id = c->topo.initial_apicid; 1768f7627e25SThomas Gleixner } 1769f7627e25SThomas Gleixner 1770f7627e25SThomas Gleixner get_model_name(c); /* Default name */ 1771f7627e25SThomas Gleixner 17720230bb03SAndy Lutomirski /* 17730230bb03SAndy Lutomirski * ESPFIX is a strange bug. All real CPUs have it. Paravirt 17740230bb03SAndy Lutomirski * systems that run Linux at CPL > 0 may or may not have the 17750230bb03SAndy Lutomirski * issue, but, even if they have the issue, there's absolutely 17760230bb03SAndy Lutomirski * nothing we can do about it because we can't use the real IRET 17770230bb03SAndy Lutomirski * instruction. 17780230bb03SAndy Lutomirski * 17790230bb03SAndy Lutomirski * NB: For the time being, only 32-bit kernels support 17800230bb03SAndy Lutomirski * X86_BUG_ESPFIX as such. 64-bit kernels directly choose 17810230bb03SAndy Lutomirski * whether to apply espfix using paravirt hooks. If any 17820230bb03SAndy Lutomirski * non-paravirt system ever shows up that does *not* have the 17830230bb03SAndy Lutomirski * ESPFIX issue, we can change this. 17840230bb03SAndy Lutomirski */ 17850230bb03SAndy Lutomirski #ifdef CONFIG_X86_32 17860230bb03SAndy Lutomirski set_cpu_bug(c, X86_BUG_ESPFIX); 17870230bb03SAndy Lutomirski #endif 1788f7627e25SThomas Gleixner } 1789f7627e25SThomas Gleixner 1790f7627e25SThomas Gleixner /* 17919d85eb91SThomas Gleixner * Validate that ACPI/mptables have the same information about the 17929d85eb91SThomas Gleixner * effective APIC id and update the package map. 1793d49597fdSThomas Gleixner */ 17949d85eb91SThomas Gleixner static void validate_apic_and_package_id(struct cpuinfo_x86 *c) 1795d49597fdSThomas Gleixner { 1796d49597fdSThomas Gleixner #ifdef CONFIG_SMP 17978aa2a417SThomas Gleixner unsigned int cpu = smp_processor_id(); 17988aa2a417SThomas Gleixner u32 apicid; 1799d49597fdSThomas Gleixner 1800d49597fdSThomas Gleixner apicid = apic->cpu_present_to_apicid(cpu); 1801d49597fdSThomas Gleixner 1802b9655e70SThomas Gleixner if (apicid != c->topo.apicid) { 18039d85eb91SThomas Gleixner pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n", 1804b9655e70SThomas Gleixner cpu, apicid, c->topo.initial_apicid); 1805d49597fdSThomas Gleixner } 180602fb601dSThomas Gleixner BUG_ON(topology_update_package_map(c->topo.pkg_id, cpu)); 18078a169ed4SThomas Gleixner BUG_ON(topology_update_die_map(c->topo.die_id, cpu)); 1808d49597fdSThomas Gleixner #else 180922dc9631SThomas Gleixner c->topo.logical_pkg_id = 0; 1810d49597fdSThomas Gleixner #endif 1811d49597fdSThomas Gleixner } 1812d49597fdSThomas Gleixner 1813d49597fdSThomas Gleixner /* 1814f7627e25SThomas Gleixner * This does the hard work of actually picking apart the CPU stuff... 1815f7627e25SThomas Gleixner */ 1816148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c) 1817f7627e25SThomas Gleixner { 1818f7627e25SThomas Gleixner int i; 1819f7627e25SThomas Gleixner 1820f7627e25SThomas Gleixner c->loops_per_jiffy = loops_per_jiffy; 182124dbc600SGustavo A. R. Silva c->x86_cache_size = 0; 1822f7627e25SThomas Gleixner c->x86_vendor = X86_VENDOR_UNKNOWN; 1823b399151cSJia Zhang c->x86_model = c->x86_stepping = 0; /* So far unknown... */ 1824f7627e25SThomas Gleixner c->x86_vendor_id[0] = '\0'; /* Unset */ 1825f7627e25SThomas Gleixner c->x86_model_id[0] = '\0'; /* Unset */ 1826f7627e25SThomas Gleixner c->x86_max_cores = 1; 1827102bbe3aSYinghai Lu c->x86_coreid_bits = 0; 1828e3c0c5d5SThomas Gleixner c->topo.cu_id = 0xff; 18296e290323SThomas Gleixner c->topo.llc_id = BAD_APICID; 18306e290323SThomas Gleixner c->topo.l2c_id = BAD_APICID; 183111fdd252SYinghai Lu #ifdef CONFIG_X86_64 1832102bbe3aSYinghai Lu c->x86_clflush_size = 64; 183313c6c532SJan Beulich c->x86_phys_bits = 36; 183413c6c532SJan Beulich c->x86_virt_bits = 48; 1835102bbe3aSYinghai Lu #else 1836102bbe3aSYinghai Lu c->cpuid_level = -1; /* CPUID not detected */ 1837f7627e25SThomas Gleixner c->x86_clflush_size = 32; 183813c6c532SJan Beulich c->x86_phys_bits = 32; 183913c6c532SJan Beulich c->x86_virt_bits = 32; 1840102bbe3aSYinghai Lu #endif 1841102bbe3aSYinghai Lu c->x86_cache_alignment = c->x86_clflush_size; 18420e96f31eSJordan Borgner memset(&c->x86_capability, 0, sizeof(c->x86_capability)); 1843b47ce1feSSean Christopherson #ifdef CONFIG_X86_VMX_FEATURE_NAMES 1844b47ce1feSSean Christopherson memset(&c->vmx_capability, 0, sizeof(c->vmx_capability)); 1845b47ce1feSSean Christopherson #endif 1846f7627e25SThomas Gleixner 1847f7627e25SThomas Gleixner generic_identify(c); 1848f7627e25SThomas Gleixner 18493898534dSAndi Kleen if (this_cpu->c_identify) 1850f7627e25SThomas Gleixner this_cpu->c_identify(c); 1851f7627e25SThomas Gleixner 18526a6256f9SAdam Buchbinder /* Clear/Set all flags overridden by options, after probe */ 18538bf1ebcaSAndy Lutomirski apply_forced_caps(c); 18542759c328SYinghai Lu 1855102bbe3aSYinghai Lu #ifdef CONFIG_X86_64 1856b9655e70SThomas Gleixner c->topo.apicid = apic->phys_pkg_id(c->topo.initial_apicid, 0); 1857102bbe3aSYinghai Lu #endif 1858102bbe3aSYinghai Lu 1859*04c30245SBorislav Petkov (AMD) 1860*04c30245SBorislav Petkov (AMD) /* 1861*04c30245SBorislav Petkov (AMD) * Set default APIC and TSC_DEADLINE MSR fencing flag. AMD and 1862*04c30245SBorislav Petkov (AMD) * Hygon will clear it in ->c_init() below. 1863*04c30245SBorislav Petkov (AMD) */ 1864*04c30245SBorislav Petkov (AMD) set_cpu_cap(c, X86_FEATURE_APIC_MSRS_FENCE); 1865*04c30245SBorislav Petkov (AMD) 1866f7627e25SThomas Gleixner /* 1867f7627e25SThomas Gleixner * Vendor-specific initialization. In this section we 1868f7627e25SThomas Gleixner * canonicalize the feature flags, meaning if there are 1869f7627e25SThomas Gleixner * features a certain CPU supports which CPUID doesn't 1870f7627e25SThomas Gleixner * tell us, CPUID claiming incorrect flags, or other bugs, 1871f7627e25SThomas Gleixner * we handle them here. 1872f7627e25SThomas Gleixner * 1873f7627e25SThomas Gleixner * At the end of this section, c->x86_capability better 1874f7627e25SThomas Gleixner * indicate the features this CPU genuinely supports! 1875f7627e25SThomas Gleixner */ 1876f7627e25SThomas Gleixner if (this_cpu->c_init) 1877f7627e25SThomas Gleixner this_cpu->c_init(c); 1878f7627e25SThomas Gleixner 1879f7627e25SThomas Gleixner /* Disable the PN if appropriate */ 1880f7627e25SThomas Gleixner squash_the_stupid_serial_number(c); 1881f7627e25SThomas Gleixner 1882aa35f896SRicardo Neri /* Set up SMEP/SMAP/UMIP */ 1883b2cc2a07SH. Peter Anvin setup_smep(c); 1884b2cc2a07SH. Peter Anvin setup_smap(c); 1885aa35f896SRicardo Neri setup_umip(c); 1886b2cc2a07SH. Peter Anvin 1887dd649bd0SAndy Lutomirski /* Enable FSGSBASE instructions if available. */ 1888742c45c3SAndi Kleen if (cpu_has(c, X86_FEATURE_FSGSBASE)) { 1889dd649bd0SAndy Lutomirski cr4_set_bits(X86_CR4_FSGSBASE); 1890742c45c3SAndi Kleen elf_hwcap2 |= HWCAP2_FSGSBASE; 1891742c45c3SAndi Kleen } 1892dd649bd0SAndy Lutomirski 1893f7627e25SThomas Gleixner /* 18940f3fa48aSIngo Molnar * The vendor-specific functions might have changed features. 18950f3fa48aSIngo Molnar * Now we do "generic changes." 1896f7627e25SThomas Gleixner */ 1897f7627e25SThomas Gleixner 1898b38b0665SH. Peter Anvin /* Filter out anything that depends on CPUID levels we don't have */ 1899b38b0665SH. Peter Anvin filter_cpuid_features(c, true); 1900b38b0665SH. Peter Anvin 1901f7627e25SThomas Gleixner /* If the model name is still unset, do table lookup. */ 1902f7627e25SThomas Gleixner if (!c->x86_model_id[0]) { 190302dde8b4SJan Beulich const char *p; 1904f7627e25SThomas Gleixner p = table_lookup_model(c); 1905f7627e25SThomas Gleixner if (p) 1906f7627e25SThomas Gleixner strcpy(c->x86_model_id, p); 1907f7627e25SThomas Gleixner else 1908f7627e25SThomas Gleixner /* Last resort... */ 1909f7627e25SThomas Gleixner sprintf(c->x86_model_id, "%02x/%02x", 1910f7627e25SThomas Gleixner c->x86, c->x86_model); 1911f7627e25SThomas Gleixner } 1912f7627e25SThomas Gleixner 1913102bbe3aSYinghai Lu #ifdef CONFIG_X86_64 1914102bbe3aSYinghai Lu detect_ht(c); 1915102bbe3aSYinghai Lu #endif 1916102bbe3aSYinghai Lu 191749d859d7SH. Peter Anvin x86_init_rdrand(c); 191806976945SDave Hansen setup_pku(c); 1919991625f3SPeter Zijlstra setup_cet(c); 19203e0c3737SYinghai Lu 19213e0c3737SYinghai Lu /* 19226a6256f9SAdam Buchbinder * Clear/Set all flags overridden by options, need do it 19233e0c3737SYinghai Lu * before following smp all cpus cap AND. 19243e0c3737SYinghai Lu */ 19258bf1ebcaSAndy Lutomirski apply_forced_caps(c); 19263e0c3737SYinghai Lu 1927f7627e25SThomas Gleixner /* 1928f7627e25SThomas Gleixner * On SMP, boot_cpu_data holds the common feature set between 1929f7627e25SThomas Gleixner * all CPUs; so make sure that we indicate which features are 1930f7627e25SThomas Gleixner * common between the CPUs. The first time this routine gets 1931f7627e25SThomas Gleixner * executed, c == &boot_cpu_data. 1932f7627e25SThomas Gleixner */ 1933f7627e25SThomas Gleixner if (c != &boot_cpu_data) { 1934f7627e25SThomas Gleixner /* AND the already accumulated flags with these */ 1935f7627e25SThomas Gleixner for (i = 0; i < NCAPINTS; i++) 1936f7627e25SThomas Gleixner boot_cpu_data.x86_capability[i] &= c->x86_capability[i]; 193765fc985bSBorislav Petkov 193865fc985bSBorislav Petkov /* OR, i.e. replicate the bug flags */ 193965fc985bSBorislav Petkov for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++) 194065fc985bSBorislav Petkov c->x86_capability[i] |= boot_cpu_data.x86_capability[i]; 1941f7627e25SThomas Gleixner } 1942f7627e25SThomas Gleixner 19430dcab41dSTony Luck ppin_init(c); 19440dcab41dSTony Luck 1945f7627e25SThomas Gleixner /* Init Machine Check Exception if available. */ 19465e09954aSBorislav Petkov mcheck_cpu_init(c); 194730d432dfSAndi Kleen 194830d432dfSAndi Kleen select_idle_routine(c); 1949102bbe3aSYinghai Lu 1950de2d9445STejun Heo #ifdef CONFIG_NUMA 1951102bbe3aSYinghai Lu numa_add_cpu(smp_processor_id()); 1952102bbe3aSYinghai Lu #endif 1953f7627e25SThomas Gleixner } 1954f7627e25SThomas Gleixner 19558b6c0ab1SIngo Molnar /* 19568b6c0ab1SIngo Molnar * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions 19578b6c0ab1SIngo Molnar * on 32-bit kernels: 19588b6c0ab1SIngo Molnar */ 1959cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32 1960cfda7bb9SAndy Lutomirski void enable_sep_cpu(void) 1961cfda7bb9SAndy Lutomirski { 19628b6c0ab1SIngo Molnar struct tss_struct *tss; 19638b6c0ab1SIngo Molnar int cpu; 1964cfda7bb9SAndy Lutomirski 1965b3edfda4SBorislav Petkov if (!boot_cpu_has(X86_FEATURE_SEP)) 1966b3edfda4SBorislav Petkov return; 1967b3edfda4SBorislav Petkov 19688b6c0ab1SIngo Molnar cpu = get_cpu(); 1969c482feefSAndy Lutomirski tss = &per_cpu(cpu_tss_rw, cpu); 19708b6c0ab1SIngo Molnar 19718b6c0ab1SIngo Molnar /* 1972cf9328ccSAndy Lutomirski * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field -- 1973cf9328ccSAndy Lutomirski * see the big comment in struct x86_hw_tss's definition. 19748b6c0ab1SIngo Molnar */ 1975cfda7bb9SAndy Lutomirski 1976cfda7bb9SAndy Lutomirski tss->x86_tss.ss1 = __KERNEL_CS; 19778b6c0ab1SIngo Molnar wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0); 19784fe2d8b1SDave Hansen wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0); 19794c8cd0c5SIngo Molnar wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0); 19808b6c0ab1SIngo Molnar 1981cfda7bb9SAndy Lutomirski put_cpu(); 1982cfda7bb9SAndy Lutomirski } 1983e04d645fSGlauber Costa #endif 1984e04d645fSGlauber Costa 19853ba3fdfeSThomas Gleixner static __init void identify_boot_cpu(void) 1986f7627e25SThomas Gleixner { 1987f7627e25SThomas Gleixner identify_cpu(&boot_cpu_data); 1988991625f3SPeter Zijlstra if (HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT)) 1989991625f3SPeter Zijlstra pr_info("CET detected: Indirect Branch Tracking enabled\n"); 1990102bbe3aSYinghai Lu #ifdef CONFIG_X86_32 1991f7627e25SThomas Gleixner enable_sep_cpu(); 1992102bbe3aSYinghai Lu #endif 1993e0ba94f1SAlex Shi cpu_detect_tlb(&boot_cpu_data); 1994873d50d5SKees Cook setup_cr_pinning(); 199595c5824fSPawan Gupta 199695c5824fSPawan Gupta tsx_init(); 199792cbbadfSH. Peter Anvin (Intel) lkgs_init(); 1998f7627e25SThomas Gleixner } 1999f7627e25SThomas Gleixner 2000148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c) 2001f7627e25SThomas Gleixner { 2002f7627e25SThomas Gleixner BUG_ON(c == &boot_cpu_data); 2003f7627e25SThomas Gleixner identify_cpu(c); 2004102bbe3aSYinghai Lu #ifdef CONFIG_X86_32 2005f7627e25SThomas Gleixner enable_sep_cpu(); 2006102bbe3aSYinghai Lu #endif 20079d85eb91SThomas Gleixner validate_apic_and_package_id(c); 200877243971SKonrad Rzeszutek Wilk x86_spec_ctrl_setup_ap(); 20097e5b3c26SMark Gross update_srbds_msr(); 20108974eb58SDaniel Sneddon if (boot_cpu_has_bug(X86_BUG_GDS)) 20118974eb58SDaniel Sneddon update_gds_msr(); 2012400331f8SPawan Gupta 2013400331f8SPawan Gupta tsx_ap_init(); 2014f7627e25SThomas Gleixner } 2015f7627e25SThomas Gleixner 2016148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c) 2017f7627e25SThomas Gleixner { 201802dde8b4SJan Beulich const char *vendor = NULL; 2019f7627e25SThomas Gleixner 20200f3fa48aSIngo Molnar if (c->x86_vendor < X86_VENDOR_NUM) { 2021f7627e25SThomas Gleixner vendor = this_cpu->c_vendor; 20220f3fa48aSIngo Molnar } else { 20230f3fa48aSIngo Molnar if (c->cpuid_level >= 0) 2024f7627e25SThomas Gleixner vendor = c->x86_vendor_id; 20250f3fa48aSIngo Molnar } 2026f7627e25SThomas Gleixner 2027bd32a8cfSYinghai Lu if (vendor && !strstr(c->x86_model_id, vendor)) 20281b74dde7SChen Yucong pr_cont("%s ", vendor); 2029f7627e25SThomas Gleixner 20309d31d35bSYinghai Lu if (c->x86_model_id[0]) 20311b74dde7SChen Yucong pr_cont("%s", c->x86_model_id); 2032f7627e25SThomas Gleixner else 20331b74dde7SChen Yucong pr_cont("%d86", c->x86); 2034f7627e25SThomas Gleixner 20351b74dde7SChen Yucong pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model); 2036924e101aSBorislav Petkov 2037b399151cSJia Zhang if (c->x86_stepping || c->cpuid_level >= 0) 2038b399151cSJia Zhang pr_cont(", stepping: 0x%x)\n", c->x86_stepping); 2039f7627e25SThomas Gleixner else 20401b74dde7SChen Yucong pr_cont(")\n"); 2041f7627e25SThomas Gleixner } 2042f7627e25SThomas Gleixner 20430c2a3913SAndi Kleen /* 2044ce38f038SThomas Gleixner * clearcpuid= was already parsed in cpu_parse_early_param(). This dummy 2045ce38f038SThomas Gleixner * function prevents it from becoming an environment variable for init. 20460c2a3913SAndi Kleen */ 20470c2a3913SAndi Kleen static __init int setup_clearcpuid(char *arg) 2048ac72e788SAndi Kleen { 2049ac72e788SAndi Kleen return 1; 2050ac72e788SAndi Kleen } 20510c2a3913SAndi Kleen __setup("clearcpuid=", setup_clearcpuid); 2052ac72e788SAndi Kleen 2053e57ef2edSThomas Gleixner DEFINE_PER_CPU_ALIGNED(struct pcpu_hot, pcpu_hot) = { 2054e57ef2edSThomas Gleixner .current_task = &init_task, 205564701838SThomas Gleixner .preempt_count = INIT_PREEMPT_COUNT, 2056c063a217SThomas Gleixner .top_of_stack = TOP_OF_INIT_STACK, 2057e57ef2edSThomas Gleixner }; 2058e57ef2edSThomas Gleixner EXPORT_PER_CPU_SYMBOL(pcpu_hot); 2059e57ef2edSThomas Gleixner 2060d5494d4fSYinghai Lu #ifdef CONFIG_X86_64 2061e6401c13SAndy Lutomirski DEFINE_PER_CPU_FIRST(struct fixed_percpu_data, 2062e6401c13SAndy Lutomirski fixed_percpu_data) __aligned(PAGE_SIZE) __visible; 2063e6401c13SAndy Lutomirski EXPORT_PER_CPU_SYMBOL_GPL(fixed_percpu_data); 20640f3fa48aSIngo Molnar 20659c7e2634SAndi Kleen static void wrmsrl_cstar(unsigned long val) 20669c7e2634SAndi Kleen { 20679c7e2634SAndi Kleen /* 20689c7e2634SAndi Kleen * Intel CPUs do not support 32-bit SYSCALL. Writing to MSR_CSTAR 20699c7e2634SAndi Kleen * is so far ignored by the CPU, but raises a #VE trap in a TDX 20709c7e2634SAndi Kleen * guest. Avoid the pointless write on all Intel CPUs. 20719c7e2634SAndi Kleen */ 20729c7e2634SAndi Kleen if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) 20739c7e2634SAndi Kleen wrmsrl(MSR_CSTAR, val); 20749c7e2634SAndi Kleen } 20759c7e2634SAndi Kleen 2076d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */ 2077d5494d4fSYinghai Lu void syscall_init(void) 2078d5494d4fSYinghai Lu { 207931ac34caSBorislav Petkov wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS); 20808d4b0678SThomas Gleixner wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64); 2081d56fe4bfSIngo Molnar 208261382281SNikolay Borisov if (ia32_enabled()) { 20839c7e2634SAndi Kleen wrmsrl_cstar((unsigned long)entry_SYSCALL_compat); 2084a76c7f46SDenys Vlasenko /* 2085487d1edbSDenys Vlasenko * This only works on Intel CPUs. 2086487d1edbSDenys Vlasenko * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP. 2087487d1edbSDenys Vlasenko * This does not cause SYSENTER to jump to the wrong location, because 2088487d1edbSDenys Vlasenko * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit). 2089a76c7f46SDenys Vlasenko */ 2090a76c7f46SDenys Vlasenko wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS); 20918e6b65a1Szhong jiang wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 20928e6b65a1Szhong jiang (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1)); 20934c8cd0c5SIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat); 209461382281SNikolay Borisov } else { 2095f71e1d2fSNikolay Borisov wrmsrl_cstar((unsigned long)entry_SYSCALL32_ignore); 20966b51311cSBorislav Petkov wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG); 2097d56fe4bfSIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL); 2098d56fe4bfSIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL); 209961382281SNikolay Borisov } 2100d5494d4fSYinghai Lu 21016de4ac1dSH. Peter Anvin (Intel) /* 21026de4ac1dSH. Peter Anvin (Intel) * Flags to clear on syscall; clear as much as possible 21036de4ac1dSH. Peter Anvin (Intel) * to minimize user space-kernel interference. 21046de4ac1dSH. Peter Anvin (Intel) */ 2105d5494d4fSYinghai Lu wrmsrl(MSR_SYSCALL_MASK, 21066de4ac1dSH. Peter Anvin (Intel) X86_EFLAGS_CF|X86_EFLAGS_PF|X86_EFLAGS_AF| 21076de4ac1dSH. Peter Anvin (Intel) X86_EFLAGS_ZF|X86_EFLAGS_SF|X86_EFLAGS_TF| 21086de4ac1dSH. Peter Anvin (Intel) X86_EFLAGS_IF|X86_EFLAGS_DF|X86_EFLAGS_OF| 21096de4ac1dSH. Peter Anvin (Intel) X86_EFLAGS_IOPL|X86_EFLAGS_NT|X86_EFLAGS_RF| 21106de4ac1dSH. Peter Anvin (Intel) X86_EFLAGS_AC|X86_EFLAGS_ID); 2111d5494d4fSYinghai Lu } 2112d5494d4fSYinghai Lu 21130f3fa48aSIngo Molnar #else /* CONFIG_X86_64 */ 2114d5494d4fSYinghai Lu 2115050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR 21163fb0fdb3SAndy Lutomirski DEFINE_PER_CPU(unsigned long, __stack_chk_guard); 21173fb0fdb3SAndy Lutomirski EXPORT_PER_CPU_SYMBOL(__stack_chk_guard); 211860a5317fSTejun Heo #endif 211960a5317fSTejun Heo 21200f3fa48aSIngo Molnar #endif /* CONFIG_X86_64 */ 2121f7627e25SThomas Gleixner 2122f7627e25SThomas Gleixner /* 21239766cdbcSJaswinder Singh Rajput * Clear all 6 debug registers: 21249766cdbcSJaswinder Singh Rajput */ 21259766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void) 21269766cdbcSJaswinder Singh Rajput { 21279766cdbcSJaswinder Singh Rajput int i; 21289766cdbcSJaswinder Singh Rajput 21299766cdbcSJaswinder Singh Rajput for (i = 0; i < 8; i++) { 21309766cdbcSJaswinder Singh Rajput /* Ignore db4, db5 */ 21319766cdbcSJaswinder Singh Rajput if ((i == 4) || (i == 5)) 21329766cdbcSJaswinder Singh Rajput continue; 21339766cdbcSJaswinder Singh Rajput 21349766cdbcSJaswinder Singh Rajput set_debugreg(0, i); 21359766cdbcSJaswinder Singh Rajput } 21369766cdbcSJaswinder Singh Rajput } 2137f7627e25SThomas Gleixner 21380bb9fef9SJason Wessel #ifdef CONFIG_KGDB 21390bb9fef9SJason Wessel /* 21400bb9fef9SJason Wessel * Restore debug regs if using kgdbwait and you have a kernel debugger 21410bb9fef9SJason Wessel * connection established. 21420bb9fef9SJason Wessel */ 21430bb9fef9SJason Wessel static void dbg_restore_debug_regs(void) 21440bb9fef9SJason Wessel { 21450bb9fef9SJason Wessel if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break)) 21460bb9fef9SJason Wessel arch_kgdb_ops.correct_hw_break(); 21470bb9fef9SJason Wessel } 21480bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */ 21490bb9fef9SJason Wessel #define dbg_restore_debug_regs() 21500bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */ 21510bb9fef9SJason Wessel 2152505b7899SThomas Gleixner static inline void setup_getcpu(int cpu) 2153b2e2ba57SChang S. Bae { 215422245bdfSIngo Molnar unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu)); 2155b2e2ba57SChang S. Bae struct desc_struct d = { }; 2156b2e2ba57SChang S. Bae 2157b6b4fbd9SSean Christopherson if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID)) 2158fc48a6d1SSean Christopherson wrmsr(MSR_TSC_AUX, cpudata, 0); 2159b2e2ba57SChang S. Bae 2160b2e2ba57SChang S. Bae /* Store CPU and node number in limit. */ 2161b2e2ba57SChang S. Bae d.limit0 = cpudata; 2162b2e2ba57SChang S. Bae d.limit1 = cpudata >> 16; 2163b2e2ba57SChang S. Bae 2164b2e2ba57SChang S. Bae d.type = 5; /* RO data, expand down, accessed */ 2165b2e2ba57SChang S. Bae d.dpl = 3; /* Visible to user code */ 2166b2e2ba57SChang S. Bae d.s = 1; /* Not a system segment */ 2167b2e2ba57SChang S. Bae d.p = 1; /* Present */ 2168b2e2ba57SChang S. Bae d.d = 1; /* 32-bit */ 2169b2e2ba57SChang S. Bae 217022245bdfSIngo Molnar write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S); 2171b2e2ba57SChang S. Bae } 2172505b7899SThomas Gleixner 2173717cce3bSSebastian Andrzej Siewior #ifdef CONFIG_X86_64 2174505b7899SThomas Gleixner static inline void tss_setup_ist(struct tss_struct *tss) 2175505b7899SThomas Gleixner { 2176505b7899SThomas Gleixner /* Set up the per-CPU TSS IST stacks */ 2177505b7899SThomas Gleixner tss->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF); 2178505b7899SThomas Gleixner tss->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI); 2179505b7899SThomas Gleixner tss->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB); 2180505b7899SThomas Gleixner tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE); 218102772fb9SJoerg Roedel /* Only mapped when SEV-ES is active */ 218202772fb9SJoerg Roedel tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC); 2183505b7899SThomas Gleixner } 2184505b7899SThomas Gleixner #else /* CONFIG_X86_64 */ 2185505b7899SThomas Gleixner static inline void tss_setup_ist(struct tss_struct *tss) { } 2186505b7899SThomas Gleixner #endif /* !CONFIG_X86_64 */ 2187b2e2ba57SChang S. Bae 2188111e7b15SThomas Gleixner static inline void tss_setup_io_bitmap(struct tss_struct *tss) 2189111e7b15SThomas Gleixner { 2190111e7b15SThomas Gleixner tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID; 2191111e7b15SThomas Gleixner 2192111e7b15SThomas Gleixner #ifdef CONFIG_X86_IOPL_IOPERM 2193111e7b15SThomas Gleixner tss->io_bitmap.prev_max = 0; 2194111e7b15SThomas Gleixner tss->io_bitmap.prev_sequence = 0; 2195111e7b15SThomas Gleixner memset(tss->io_bitmap.bitmap, 0xff, sizeof(tss->io_bitmap.bitmap)); 2196111e7b15SThomas Gleixner /* 2197111e7b15SThomas Gleixner * Invalidate the extra array entry past the end of the all 2198111e7b15SThomas Gleixner * permission bitmap as required by the hardware. 2199111e7b15SThomas Gleixner */ 2200111e7b15SThomas Gleixner tss->io_bitmap.mapall[IO_BITMAP_LONGS] = ~0UL; 2201111e7b15SThomas Gleixner #endif 2202111e7b15SThomas Gleixner } 2203ce4b1b16SIgor Mammedov 2204f7627e25SThomas Gleixner /* 2205520d0308SJoerg Roedel * Setup everything needed to handle exceptions from the IDT, including the IST 2206520d0308SJoerg Roedel * exceptions which use paranoid_entry(). 2207520d0308SJoerg Roedel */ 2208520d0308SJoerg Roedel void cpu_init_exception_handling(void) 2209520d0308SJoerg Roedel { 2210520d0308SJoerg Roedel struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw); 2211520d0308SJoerg Roedel int cpu = raw_smp_processor_id(); 2212520d0308SJoerg Roedel 2213520d0308SJoerg Roedel /* paranoid_entry() gets the CPU number from the GDT */ 2214520d0308SJoerg Roedel setup_getcpu(cpu); 2215520d0308SJoerg Roedel 2216520d0308SJoerg Roedel /* IST vectors need TSS to be set up. */ 2217520d0308SJoerg Roedel tss_setup_ist(tss); 2218520d0308SJoerg Roedel tss_setup_io_bitmap(tss); 2219520d0308SJoerg Roedel set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss); 2220520d0308SJoerg Roedel 2221520d0308SJoerg Roedel load_TR_desc(); 2222520d0308SJoerg Roedel 222395d33bfaSBrijesh Singh /* GHCB needs to be setup to handle #VC. */ 222495d33bfaSBrijesh Singh setup_ghcb(); 222595d33bfaSBrijesh Singh 2226520d0308SJoerg Roedel /* Finally load the IDT */ 2227520d0308SJoerg Roedel load_current_idt(); 2228520d0308SJoerg Roedel } 2229520d0308SJoerg Roedel 2230520d0308SJoerg Roedel /* 2231f7627e25SThomas Gleixner * cpu_init() initializes state that is per-CPU. Some data is already 2232b1efd0ffSBorislav Petkov * initialized (naturally) in the bootstrap process, such as the GDT. We 2233b1efd0ffSBorislav Petkov * reload it nevertheless, this function acts as a 'CPU state barrier', 2234b1efd0ffSBorislav Petkov * nothing should get across. 2235f7627e25SThomas Gleixner */ 2236148f9bb8SPaul Gortmaker void cpu_init(void) 22371ba76586SYinghai Lu { 2238505b7899SThomas Gleixner struct task_struct *cur = current; 2239f6ef7322SThomas Gleixner int cpu = raw_smp_processor_id(); 22401ba76586SYinghai Lu 2241e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA 224227fd185fSFenghua Yu if (this_cpu_read(numa_node) == 0 && 2243e534c7c5SLee Schermerhorn early_cpu_to_node(cpu) != NUMA_NO_NODE) 2244e534c7c5SLee Schermerhorn set_numa_node(early_cpu_to_node(cpu)); 2245e7a22c1eSBrian Gerst #endif 22462eaad1fdSMike Travis pr_debug("Initializing CPU#%d\n", cpu); 22471ba76586SYinghai Lu 2248505b7899SThomas Gleixner if (IS_ENABLED(CONFIG_X86_64) || cpu_feature_enabled(X86_FEATURE_VME) || 2249505b7899SThomas Gleixner boot_cpu_has(X86_FEATURE_TSC) || boot_cpu_has(X86_FEATURE_DE)) 2250375074ccSAndy Lutomirski cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); 22511ba76586SYinghai Lu 2252505b7899SThomas Gleixner if (IS_ENABLED(CONFIG_X86_64)) { 2253505b7899SThomas Gleixner loadsegment(fs, 0); 2254505b7899SThomas Gleixner memset(cur->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8); 22551ba76586SYinghai Lu syscall_init(); 22561ba76586SYinghai Lu 22571ba76586SYinghai Lu wrmsrl(MSR_FS_BASE, 0); 22581ba76586SYinghai Lu wrmsrl(MSR_KERNEL_GS_BASE, 0); 22591ba76586SYinghai Lu barrier(); 22601ba76586SYinghai Lu 2261659006bfSThomas Gleixner x2apic_setup(); 22621ba76586SYinghai Lu } 22631ba76586SYinghai Lu 2264f1f10076SVegard Nossum mmgrab(&init_mm); 2265505b7899SThomas Gleixner cur->active_mm = &init_mm; 2266505b7899SThomas Gleixner BUG_ON(cur->mm); 226772c0098dSAndy Lutomirski initialize_tlbstate_and_flush(); 2268505b7899SThomas Gleixner enter_lazy_tlb(&init_mm, cur); 22691ba76586SYinghai Lu 2270505b7899SThomas Gleixner /* 2271505b7899SThomas Gleixner * sp0 points to the entry trampoline stack regardless of what task 2272505b7899SThomas Gleixner * is running. 2273505b7899SThomas Gleixner */ 22744fe2d8b1SDave Hansen load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1)); 227520bb8344SAndy Lutomirski 227637868fe1SAndy Lutomirski load_mm_ldt(&init_mm); 22771ba76586SYinghai Lu 22789766cdbcSJaswinder Singh Rajput clear_all_debug_regs(); 22790bb9fef9SJason Wessel dbg_restore_debug_regs(); 22801ba76586SYinghai Lu 2281dc4e0021SAndy Lutomirski doublefault_init_cpu_tss(); 2282505b7899SThomas Gleixner 22831ba76586SYinghai Lu if (is_uv_system()) 22841ba76586SYinghai Lu uv_cpu_init(); 228569218e47SThomas Garnier 228669218e47SThomas Garnier load_fixmap_gdt(cpu); 22871ba76586SYinghai Lu } 22881ba76586SYinghai Lu 2289a77a94f8SBorislav Petkov #ifdef CONFIG_MICROCODE_LATE_LOADING 2290ab31c744SAshok Raj /** 2291c0dd9245SAshok Raj * store_cpu_caps() - Store a snapshot of CPU capabilities 2292c0dd9245SAshok Raj * @curr_info: Pointer where to store it 2293c0dd9245SAshok Raj * 2294c0dd9245SAshok Raj * Returns: None 2295c0dd9245SAshok Raj */ 2296c0dd9245SAshok Raj void store_cpu_caps(struct cpuinfo_x86 *curr_info) 2297c0dd9245SAshok Raj { 2298c0dd9245SAshok Raj /* Reload CPUID max function as it might've changed. */ 2299c0dd9245SAshok Raj curr_info->cpuid_level = cpuid_eax(0); 2300c0dd9245SAshok Raj 2301c0dd9245SAshok Raj /* Copy all capability leafs and pick up the synthetic ones. */ 2302c0dd9245SAshok Raj memcpy(&curr_info->x86_capability, &boot_cpu_data.x86_capability, 2303c0dd9245SAshok Raj sizeof(curr_info->x86_capability)); 2304c0dd9245SAshok Raj 2305c0dd9245SAshok Raj /* Get the hardware CPUID leafs */ 2306c0dd9245SAshok Raj get_cpu_cap(curr_info); 2307c0dd9245SAshok Raj } 2308c0dd9245SAshok Raj 2309c0dd9245SAshok Raj /** 2310ab31c744SAshok Raj * microcode_check() - Check if any CPU capabilities changed after an update. 2311ab31c744SAshok Raj * @prev_info: CPU capabilities stored before an update. 2312ab31c744SAshok Raj * 23131008c52cSBorislav Petkov * The microcode loader calls this upon late microcode load to recheck features, 231480347cd5SSebastian Andrzej Siewior * only when microcode has been updated. Caller holds and CPU hotplug lock. 2315ab31c744SAshok Raj * 2316ab31c744SAshok Raj * Return: None 23171008c52cSBorislav Petkov */ 2318ab31c744SAshok Raj void microcode_check(struct cpuinfo_x86 *prev_info) 23191008c52cSBorislav Petkov { 2320c0dd9245SAshok Raj struct cpuinfo_x86 curr_info; 232142ca8082SBorislav Petkov 23221008c52cSBorislav Petkov perf_check_microcode(); 232342ca8082SBorislav Petkov 2324522b1d69SBorislav Petkov (AMD) amd_check_microcode(); 2325522b1d69SBorislav Petkov (AMD) 2326c0dd9245SAshok Raj store_cpu_caps(&curr_info); 232742ca8082SBorislav Petkov 2328c0dd9245SAshok Raj if (!memcmp(&prev_info->x86_capability, &curr_info.x86_capability, 2329ab31c744SAshok Raj sizeof(prev_info->x86_capability))) 233042ca8082SBorislav Petkov return; 233142ca8082SBorislav Petkov 233242ca8082SBorislav Petkov pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n"); 233342ca8082SBorislav Petkov pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n"); 23341008c52cSBorislav Petkov } 2335a77a94f8SBorislav Petkov #endif 23369c92374bSThomas Gleixner 23379c92374bSThomas Gleixner /* 23389c92374bSThomas Gleixner * Invoked from core CPU hotplug code after hotplug operations 23399c92374bSThomas Gleixner */ 23409c92374bSThomas Gleixner void arch_smt_update(void) 23419c92374bSThomas Gleixner { 23429c92374bSThomas Gleixner /* Handle the speculative execution misfeatures */ 23439c92374bSThomas Gleixner cpu_bugs_smt_update(); 23446a1cb5f5SThomas Gleixner /* Check whether IPI broadcasting can be enabled */ 23456a1cb5f5SThomas Gleixner apic_smt_update(); 23469c92374bSThomas Gleixner } 23477c7077a7SThomas Gleixner 23487c7077a7SThomas Gleixner void __init arch_cpu_finalize_init(void) 23497c7077a7SThomas Gleixner { 23507c7077a7SThomas Gleixner identify_boot_cpu(); 23517c7077a7SThomas Gleixner 23527c7077a7SThomas Gleixner /* 23537c7077a7SThomas Gleixner * identify_boot_cpu() initialized SMT support information, let the 23547c7077a7SThomas Gleixner * core code know. 23557c7077a7SThomas Gleixner */ 2356447ae4acSMichael Ellerman cpu_smt_set_num_threads(smp_num_siblings, smp_num_siblings); 23577c7077a7SThomas Gleixner 23587c7077a7SThomas Gleixner if (!IS_ENABLED(CONFIG_SMP)) { 23597c7077a7SThomas Gleixner pr_info("CPU: "); 23607c7077a7SThomas Gleixner print_cpu_info(&boot_cpu_data); 23617c7077a7SThomas Gleixner } 23627c7077a7SThomas Gleixner 23637c7077a7SThomas Gleixner cpu_select_mitigations(); 23647c7077a7SThomas Gleixner 23657c7077a7SThomas Gleixner arch_smt_update(); 23667c7077a7SThomas Gleixner 23677c7077a7SThomas Gleixner if (IS_ENABLED(CONFIG_X86_32)) { 23687c7077a7SThomas Gleixner /* 23697c7077a7SThomas Gleixner * Check whether this is a real i386 which is not longer 23707c7077a7SThomas Gleixner * supported and fixup the utsname. 23717c7077a7SThomas Gleixner */ 23727c7077a7SThomas Gleixner if (boot_cpu_data.x86 < 4) 23737c7077a7SThomas Gleixner panic("Kernel requires i486+ for 'invlpg' and other features"); 23747c7077a7SThomas Gleixner 23757c7077a7SThomas Gleixner init_utsname()->machine[1] = 23767c7077a7SThomas Gleixner '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86); 23777c7077a7SThomas Gleixner } 23787c7077a7SThomas Gleixner 2379b81fac90SThomas Gleixner /* 2380b81fac90SThomas Gleixner * Must be before alternatives because it might set or clear 2381b81fac90SThomas Gleixner * feature bits. 2382b81fac90SThomas Gleixner */ 2383b81fac90SThomas Gleixner fpu__init_system(); 2384b81fac90SThomas Gleixner fpu__init_cpu(); 2385b81fac90SThomas Gleixner 23867c7077a7SThomas Gleixner alternative_instructions(); 23877c7077a7SThomas Gleixner 23887c7077a7SThomas Gleixner if (IS_ENABLED(CONFIG_X86_64)) { 23897c7077a7SThomas Gleixner /* 23907c7077a7SThomas Gleixner * Make sure the first 2MB area is not mapped by huge pages 23917c7077a7SThomas Gleixner * There are typically fixed size MTRRs in there and overlapping 23927c7077a7SThomas Gleixner * MTRRs into large pages causes slow downs. 23937c7077a7SThomas Gleixner * 23947c7077a7SThomas Gleixner * Right now we don't do that with gbpages because there seems 23957c7077a7SThomas Gleixner * very little benefit for that case. 23967c7077a7SThomas Gleixner */ 23977c7077a7SThomas Gleixner if (!direct_gbpages) 23987c7077a7SThomas Gleixner set_memory_4k((unsigned long)__va(0), 1); 23997c7077a7SThomas Gleixner } else { 24007c7077a7SThomas Gleixner fpu__init_check_bugs(); 24017c7077a7SThomas Gleixner } 2402439e1757SThomas Gleixner 2403439e1757SThomas Gleixner /* 2404439e1757SThomas Gleixner * This needs to be called before any devices perform DMA 2405439e1757SThomas Gleixner * operations that might use the SWIOTLB bounce buffers. It will 2406439e1757SThomas Gleixner * mark the bounce buffers as decrypted so that their usage will 2407439e1757SThomas Gleixner * not cause "plain-text" data to be decrypted when accessed. It 2408439e1757SThomas Gleixner * must be called after late_time_init() so that Hyper-V x86/x64 2409439e1757SThomas Gleixner * hypercalls work when the SWIOTLB bounce buffers are decrypted. 2410439e1757SThomas Gleixner */ 2411439e1757SThomas Gleixner mem_encrypt_init(); 24127c7077a7SThomas Gleixner } 2413