1f0fc4affSYinghai Lu #include <linux/bootmem.h> 29766cdbcSJaswinder Singh Rajput #include <linux/linkage.h> 3f0fc4affSYinghai Lu #include <linux/bitops.h> 49766cdbcSJaswinder Singh Rajput #include <linux/kernel.h> 5186f4360SPaul Gortmaker #include <linux/export.h> 6f7627e25SThomas Gleixner #include <linux/percpu.h> 79766cdbcSJaswinder Singh Rajput #include <linux/string.h> 8ee098e1aSBorislav Petkov #include <linux/ctype.h> 99766cdbcSJaswinder Singh Rajput #include <linux/delay.h> 109766cdbcSJaswinder Singh Rajput #include <linux/sched.h> 119766cdbcSJaswinder Singh Rajput #include <linux/init.h> 120f46efebSMasami Hiramatsu #include <linux/kprobes.h> 139766cdbcSJaswinder Singh Rajput #include <linux/kgdb.h> 149766cdbcSJaswinder Singh Rajput #include <linux/smp.h> 159766cdbcSJaswinder Singh Rajput #include <linux/io.h> 16b51ef52dSLaura Abbott #include <linux/syscore_ops.h> 179766cdbcSJaswinder Singh Rajput 189766cdbcSJaswinder Singh Rajput #include <asm/stackprotector.h> 19cdd6c482SIngo Molnar #include <asm/perf_event.h> 20f7627e25SThomas Gleixner #include <asm/mmu_context.h> 2149d859d7SH. Peter Anvin #include <asm/archrandom.h> 229766cdbcSJaswinder Singh Rajput #include <asm/hypervisor.h> 239766cdbcSJaswinder Singh Rajput #include <asm/processor.h> 241e02ce4cSAndy Lutomirski #include <asm/tlbflush.h> 25f649e938SPaul Gortmaker #include <asm/debugreg.h> 269766cdbcSJaswinder Singh Rajput #include <asm/sections.h> 27f40c3300SAndy Lutomirski #include <asm/vsyscall.h> 288bdbd962SAlan Cox #include <linux/topology.h> 298bdbd962SAlan Cox #include <linux/cpumask.h> 309766cdbcSJaswinder Singh Rajput #include <asm/pgtable.h> 3160063497SArun Sharma #include <linux/atomic.h> 329766cdbcSJaswinder Singh Rajput #include <asm/proto.h> 339766cdbcSJaswinder Singh Rajput #include <asm/setup.h> 34f7627e25SThomas Gleixner #include <asm/apic.h> 359766cdbcSJaswinder Singh Rajput #include <asm/desc.h> 3678f7f1e5SIngo Molnar #include <asm/fpu/internal.h> 379766cdbcSJaswinder Singh Rajput #include <asm/mtrr.h> 388bdbd962SAlan Cox #include <linux/numa.h> 399766cdbcSJaswinder Singh Rajput #include <asm/asm.h> 400f6ff2bcSDave Hansen #include <asm/bugs.h> 419766cdbcSJaswinder Singh Rajput #include <asm/cpu.h> 429766cdbcSJaswinder Singh Rajput #include <asm/mce.h> 439766cdbcSJaswinder Singh Rajput #include <asm/msr.h> 449766cdbcSJaswinder Singh Rajput #include <asm/pat.h> 45d288e1cfSFenghua Yu #include <asm/microcode.h> 46d288e1cfSFenghua Yu #include <asm/microcode_intel.h> 47e641f5f5SIngo Molnar 48f7627e25SThomas Gleixner #ifdef CONFIG_X86_LOCAL_APIC 49bdbcdd48STejun Heo #include <asm/uv/uv.h> 50f7627e25SThomas Gleixner #endif 51f7627e25SThomas Gleixner 52f7627e25SThomas Gleixner #include "cpu.h" 53f7627e25SThomas Gleixner 54c2d1cec1SMike Travis /* all of these masks are initialized in setup_cpu_local_masks() */ 55c2d1cec1SMike Travis cpumask_var_t cpu_initialized_mask; 569766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callout_mask; 579766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callin_mask; 58c2d1cec1SMike Travis 59c2d1cec1SMike Travis /* representing cpus for which sibling maps can be computed */ 60c2d1cec1SMike Travis cpumask_var_t cpu_sibling_setup_mask; 61c2d1cec1SMike Travis 622f2f52baSBrian Gerst /* correctly size the local cpu masks */ 634369f1fbSIngo Molnar void __init setup_cpu_local_masks(void) 642f2f52baSBrian Gerst { 652f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_initialized_mask); 662f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_callin_mask); 672f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_callout_mask); 682f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask); 692f2f52baSBrian Gerst } 702f2f52baSBrian Gerst 71148f9bb8SPaul Gortmaker static void default_init(struct cpuinfo_x86 *c) 72e8055139SOndrej Zary { 73e8055139SOndrej Zary #ifdef CONFIG_X86_64 7427c13eceSBorislav Petkov cpu_detect_cache_sizes(c); 75e8055139SOndrej Zary #else 76e8055139SOndrej Zary /* Not much we can do here... */ 77e8055139SOndrej Zary /* Check if at least it has cpuid */ 78e8055139SOndrej Zary if (c->cpuid_level == -1) { 79e8055139SOndrej Zary /* No cpuid. It must be an ancient CPU */ 80e8055139SOndrej Zary if (c->x86 == 4) 81e8055139SOndrej Zary strcpy(c->x86_model_id, "486"); 82e8055139SOndrej Zary else if (c->x86 == 3) 83e8055139SOndrej Zary strcpy(c->x86_model_id, "386"); 84e8055139SOndrej Zary } 85e8055139SOndrej Zary #endif 86e8055139SOndrej Zary } 87e8055139SOndrej Zary 88148f9bb8SPaul Gortmaker static const struct cpu_dev default_cpu = { 89e8055139SOndrej Zary .c_init = default_init, 90e8055139SOndrej Zary .c_vendor = "Unknown", 91e8055139SOndrej Zary .c_x86_vendor = X86_VENDOR_UNKNOWN, 92e8055139SOndrej Zary }; 93e8055139SOndrej Zary 94148f9bb8SPaul Gortmaker static const struct cpu_dev *this_cpu = &default_cpu; 950a488a53SYinghai Lu 9606deef89SBrian Gerst DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = { 97950ad7ffSYinghai Lu #ifdef CONFIG_X86_64 9806deef89SBrian Gerst /* 9906deef89SBrian Gerst * We need valid kernel segments for data and code in long mode too 100950ad7ffSYinghai Lu * IRET will check the segment types kkeil 2000/10/28 101950ad7ffSYinghai Lu * Also sysret mandates a special GDT layout 10206deef89SBrian Gerst * 1039766cdbcSJaswinder Singh Rajput * TLS descriptors are currently at a different place compared to i386. 10406deef89SBrian Gerst * Hopefully nobody expects them at a fixed place (Wine?) 105950ad7ffSYinghai Lu */ 1061e5de182SAkinobu Mita [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff), 1071e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff), 1081e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff), 1091e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff), 1101e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff), 1111e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff), 112950ad7ffSYinghai Lu #else 1131e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff), 1141e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 1151e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff), 1161e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff), 117f7627e25SThomas Gleixner /* 118f7627e25SThomas Gleixner * Segments used for calling PnP BIOS have byte granularity. 119f7627e25SThomas Gleixner * They code segments and data segments have fixed 64k limits, 120f7627e25SThomas Gleixner * the transfer segment sizes are set at run time. 121f7627e25SThomas Gleixner */ 1226842ef0eSGlauber de Oliveira Costa /* 32-bit code */ 1231e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), 1246842ef0eSGlauber de Oliveira Costa /* 16-bit code */ 1251e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), 1266842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 1271e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff), 1286842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 1291e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0), 1306842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 1311e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0), 132f7627e25SThomas Gleixner /* 133f7627e25SThomas Gleixner * The APM segments have byte granularity and their bases 134f7627e25SThomas Gleixner * are set at run time. All have 64k limits. 135f7627e25SThomas Gleixner */ 1366842ef0eSGlauber de Oliveira Costa /* 32-bit code */ 1371e5de182SAkinobu Mita [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), 138f7627e25SThomas Gleixner /* 16-bit code */ 1391e5de182SAkinobu Mita [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), 1406842ef0eSGlauber de Oliveira Costa /* data */ 14172c4d853SIngo Molnar [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff), 142f7627e25SThomas Gleixner 1431e5de182SAkinobu Mita [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 1441e5de182SAkinobu Mita [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 14560a5317fSTejun Heo GDT_STACK_CANARY_INIT 146950ad7ffSYinghai Lu #endif 14706deef89SBrian Gerst } }; 148f7627e25SThomas Gleixner EXPORT_PER_CPU_SYMBOL_GPL(gdt_page); 149f7627e25SThomas Gleixner 1508c3641e9SDave Hansen static int __init x86_mpx_setup(char *s) 1510c752a93SSuresh Siddha { 1528c3641e9SDave Hansen /* require an exact match without trailing characters */ 1532cd3949fSDave Hansen if (strlen(s)) 1542cd3949fSDave Hansen return 0; 1550c752a93SSuresh Siddha 1568c3641e9SDave Hansen /* do not emit a message if the feature is not present */ 1578c3641e9SDave Hansen if (!boot_cpu_has(X86_FEATURE_MPX)) 1586bad06b7SSuresh Siddha return 1; 1596bad06b7SSuresh Siddha 1608c3641e9SDave Hansen setup_clear_cpu_cap(X86_FEATURE_MPX); 1618c3641e9SDave Hansen pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n"); 162b6f42a4aSFenghua Yu return 1; 163b6f42a4aSFenghua Yu } 1648c3641e9SDave Hansen __setup("nompx", x86_mpx_setup); 165b6f42a4aSFenghua Yu 166d12a72b8SAndy Lutomirski static int __init x86_noinvpcid_setup(char *s) 167d12a72b8SAndy Lutomirski { 168d12a72b8SAndy Lutomirski /* noinvpcid doesn't accept parameters */ 169d12a72b8SAndy Lutomirski if (s) 170d12a72b8SAndy Lutomirski return -EINVAL; 171d12a72b8SAndy Lutomirski 172d12a72b8SAndy Lutomirski /* do not emit a message if the feature is not present */ 173d12a72b8SAndy Lutomirski if (!boot_cpu_has(X86_FEATURE_INVPCID)) 174d12a72b8SAndy Lutomirski return 0; 175d12a72b8SAndy Lutomirski 176d12a72b8SAndy Lutomirski setup_clear_cpu_cap(X86_FEATURE_INVPCID); 177d12a72b8SAndy Lutomirski pr_info("noinvpcid: INVPCID feature disabled\n"); 178d12a72b8SAndy Lutomirski return 0; 179d12a72b8SAndy Lutomirski } 180d12a72b8SAndy Lutomirski early_param("noinvpcid", x86_noinvpcid_setup); 181d12a72b8SAndy Lutomirski 182ba51dcedSYinghai Lu #ifdef CONFIG_X86_32 183148f9bb8SPaul Gortmaker static int cachesize_override = -1; 184148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1; 185f7627e25SThomas Gleixner 186f7627e25SThomas Gleixner static int __init cachesize_setup(char *str) 187f7627e25SThomas Gleixner { 188f7627e25SThomas Gleixner get_option(&str, &cachesize_override); 189f7627e25SThomas Gleixner return 1; 190f7627e25SThomas Gleixner } 191f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup); 192f7627e25SThomas Gleixner 193f7627e25SThomas Gleixner static int __init x86_sep_setup(char *s) 194f7627e25SThomas Gleixner { 19513530257SAndi Kleen setup_clear_cpu_cap(X86_FEATURE_SEP); 196f7627e25SThomas Gleixner return 1; 197f7627e25SThomas Gleixner } 198f7627e25SThomas Gleixner __setup("nosep", x86_sep_setup); 199f7627e25SThomas Gleixner 200f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */ 201f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag) 202f7627e25SThomas Gleixner { 203f7627e25SThomas Gleixner u32 f1, f2; 204f7627e25SThomas Gleixner 20594f6bac1SKrzysztof Helt /* 20694f6bac1SKrzysztof Helt * Cyrix and IDT cpus allow disabling of CPUID 20794f6bac1SKrzysztof Helt * so the code below may return different results 20894f6bac1SKrzysztof Helt * when it is executed before and after enabling 20994f6bac1SKrzysztof Helt * the CPUID. Add "volatile" to not allow gcc to 21094f6bac1SKrzysztof Helt * optimize the subsequent calls to this function. 21194f6bac1SKrzysztof Helt */ 21294f6bac1SKrzysztof Helt asm volatile ("pushfl \n\t" 213f7627e25SThomas Gleixner "pushfl \n\t" 214f7627e25SThomas Gleixner "popl %0 \n\t" 215f7627e25SThomas Gleixner "movl %0, %1 \n\t" 216f7627e25SThomas Gleixner "xorl %2, %0 \n\t" 217f7627e25SThomas Gleixner "pushl %0 \n\t" 218f7627e25SThomas Gleixner "popfl \n\t" 219f7627e25SThomas Gleixner "pushfl \n\t" 220f7627e25SThomas Gleixner "popl %0 \n\t" 221f7627e25SThomas Gleixner "popfl \n\t" 2220f3fa48aSIngo Molnar 223f7627e25SThomas Gleixner : "=&r" (f1), "=&r" (f2) 224f7627e25SThomas Gleixner : "ir" (flag)); 225f7627e25SThomas Gleixner 226f7627e25SThomas Gleixner return ((f1^f2) & flag) != 0; 227f7627e25SThomas Gleixner } 228f7627e25SThomas Gleixner 229f7627e25SThomas Gleixner /* Probe for the CPUID instruction */ 230148f9bb8SPaul Gortmaker int have_cpuid_p(void) 231f7627e25SThomas Gleixner { 232f7627e25SThomas Gleixner return flag_is_changeable_p(X86_EFLAGS_ID); 233f7627e25SThomas Gleixner } 234f7627e25SThomas Gleixner 235148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) 2360a488a53SYinghai Lu { 2370a488a53SYinghai Lu unsigned long lo, hi; 2380f3fa48aSIngo Molnar 2390f3fa48aSIngo Molnar if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr) 2400f3fa48aSIngo Molnar return; 2410f3fa48aSIngo Molnar 2420f3fa48aSIngo Molnar /* Disable processor serial number: */ 2430f3fa48aSIngo Molnar 2440a488a53SYinghai Lu rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi); 2450a488a53SYinghai Lu lo |= 0x200000; 2460a488a53SYinghai Lu wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi); 2470f3fa48aSIngo Molnar 2481b74dde7SChen Yucong pr_notice("CPU serial number disabled.\n"); 2490a488a53SYinghai Lu clear_cpu_cap(c, X86_FEATURE_PN); 2500a488a53SYinghai Lu 2510a488a53SYinghai Lu /* Disabling the serial number may affect the cpuid level */ 2520a488a53SYinghai Lu c->cpuid_level = cpuid_eax(0); 2530a488a53SYinghai Lu } 2540a488a53SYinghai Lu 2550a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s) 2560a488a53SYinghai Lu { 2570a488a53SYinghai Lu disable_x86_serial_nr = 0; 2580a488a53SYinghai Lu return 1; 2590a488a53SYinghai Lu } 2600a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup); 261ba51dcedSYinghai Lu #else 262102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag) 263102bbe3aSYinghai Lu { 264102bbe3aSYinghai Lu return 1; 265102bbe3aSYinghai Lu } 266102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) 267102bbe3aSYinghai Lu { 268102bbe3aSYinghai Lu } 269ba51dcedSYinghai Lu #endif 2700a488a53SYinghai Lu 271de5397adSFenghua Yu static __init int setup_disable_smep(char *arg) 272de5397adSFenghua Yu { 273b2cc2a07SH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_SMEP); 2740f6ff2bcSDave Hansen /* Check for things that depend on SMEP being enabled: */ 2750f6ff2bcSDave Hansen check_mpx_erratum(&boot_cpu_data); 276de5397adSFenghua Yu return 1; 277de5397adSFenghua Yu } 278de5397adSFenghua Yu __setup("nosmep", setup_disable_smep); 279de5397adSFenghua Yu 280b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c) 281de5397adSFenghua Yu { 282b2cc2a07SH. Peter Anvin if (cpu_has(c, X86_FEATURE_SMEP)) 283375074ccSAndy Lutomirski cr4_set_bits(X86_CR4_SMEP); 284de5397adSFenghua Yu } 285de5397adSFenghua Yu 28652b6179aSH. Peter Anvin static __init int setup_disable_smap(char *arg) 28752b6179aSH. Peter Anvin { 288b2cc2a07SH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_SMAP); 28952b6179aSH. Peter Anvin return 1; 29052b6179aSH. Peter Anvin } 29152b6179aSH. Peter Anvin __setup("nosmap", setup_disable_smap); 29252b6179aSH. Peter Anvin 293b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c) 29452b6179aSH. Peter Anvin { 295581b7f15SAndrew Cooper unsigned long eflags = native_save_fl(); 296b2cc2a07SH. Peter Anvin 297b2cc2a07SH. Peter Anvin /* This should have been cleared long ago */ 298b2cc2a07SH. Peter Anvin BUG_ON(eflags & X86_EFLAGS_AC); 299b2cc2a07SH. Peter Anvin 30003bbd596SH. Peter Anvin if (cpu_has(c, X86_FEATURE_SMAP)) { 30103bbd596SH. Peter Anvin #ifdef CONFIG_X86_SMAP 302375074ccSAndy Lutomirski cr4_set_bits(X86_CR4_SMAP); 30303bbd596SH. Peter Anvin #else 304375074ccSAndy Lutomirski cr4_clear_bits(X86_CR4_SMAP); 30503bbd596SH. Peter Anvin #endif 30603bbd596SH. Peter Anvin } 307f7627e25SThomas Gleixner } 308f7627e25SThomas Gleixner 309f7627e25SThomas Gleixner /* 31006976945SDave Hansen * Protection Keys are not available in 32-bit mode. 31106976945SDave Hansen */ 31206976945SDave Hansen static bool pku_disabled; 31306976945SDave Hansen 31406976945SDave Hansen static __always_inline void setup_pku(struct cpuinfo_x86 *c) 31506976945SDave Hansen { 316e8df1a95SDave Hansen /* check the boot processor, plus compile options for PKU: */ 317e8df1a95SDave Hansen if (!cpu_feature_enabled(X86_FEATURE_PKU)) 318e8df1a95SDave Hansen return; 319e8df1a95SDave Hansen /* checks the actual processor's cpuid bits: */ 32006976945SDave Hansen if (!cpu_has(c, X86_FEATURE_PKU)) 32106976945SDave Hansen return; 32206976945SDave Hansen if (pku_disabled) 32306976945SDave Hansen return; 32406976945SDave Hansen 32506976945SDave Hansen cr4_set_bits(X86_CR4_PKE); 32606976945SDave Hansen /* 32706976945SDave Hansen * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE 32806976945SDave Hansen * cpuid bit to be set. We need to ensure that we 32906976945SDave Hansen * update that bit in this CPU's "cpu_info". 33006976945SDave Hansen */ 33106976945SDave Hansen get_cpu_cap(c); 33206976945SDave Hansen } 33306976945SDave Hansen 33406976945SDave Hansen #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS 33506976945SDave Hansen static __init int setup_disable_pku(char *arg) 33606976945SDave Hansen { 33706976945SDave Hansen /* 33806976945SDave Hansen * Do not clear the X86_FEATURE_PKU bit. All of the 33906976945SDave Hansen * runtime checks are against OSPKE so clearing the 34006976945SDave Hansen * bit does nothing. 34106976945SDave Hansen * 34206976945SDave Hansen * This way, we will see "pku" in cpuinfo, but not 34306976945SDave Hansen * "ospke", which is exactly what we want. It shows 34406976945SDave Hansen * that the CPU has PKU, but the OS has not enabled it. 34506976945SDave Hansen * This happens to be exactly how a system would look 34606976945SDave Hansen * if we disabled the config option. 34706976945SDave Hansen */ 34806976945SDave Hansen pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n"); 34906976945SDave Hansen pku_disabled = true; 35006976945SDave Hansen return 1; 35106976945SDave Hansen } 35206976945SDave Hansen __setup("nopku", setup_disable_pku); 35306976945SDave Hansen #endif /* CONFIG_X86_64 */ 35406976945SDave Hansen 35506976945SDave Hansen /* 356b38b0665SH. Peter Anvin * Some CPU features depend on higher CPUID levels, which may not always 357b38b0665SH. Peter Anvin * be available due to CPUID level capping or broken virtualization 358b38b0665SH. Peter Anvin * software. Add those features to this table to auto-disable them. 359b38b0665SH. Peter Anvin */ 360b38b0665SH. Peter Anvin struct cpuid_dependent_feature { 361b38b0665SH. Peter Anvin u32 feature; 362b38b0665SH. Peter Anvin u32 level; 363b38b0665SH. Peter Anvin }; 3640f3fa48aSIngo Molnar 365148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature 366b38b0665SH. Peter Anvin cpuid_dependent_features[] = { 367b38b0665SH. Peter Anvin { X86_FEATURE_MWAIT, 0x00000005 }, 368b38b0665SH. Peter Anvin { X86_FEATURE_DCA, 0x00000009 }, 369b38b0665SH. Peter Anvin { X86_FEATURE_XSAVE, 0x0000000d }, 370b38b0665SH. Peter Anvin { 0, 0 } 371b38b0665SH. Peter Anvin }; 372b38b0665SH. Peter Anvin 373148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) 374b38b0665SH. Peter Anvin { 375b38b0665SH. Peter Anvin const struct cpuid_dependent_feature *df; 3769766cdbcSJaswinder Singh Rajput 377b38b0665SH. Peter Anvin for (df = cpuid_dependent_features; df->feature; df++) { 3780f3fa48aSIngo Molnar 3790f3fa48aSIngo Molnar if (!cpu_has(c, df->feature)) 3800f3fa48aSIngo Molnar continue; 381b38b0665SH. Peter Anvin /* 382b38b0665SH. Peter Anvin * Note: cpuid_level is set to -1 if unavailable, but 383b38b0665SH. Peter Anvin * extended_extended_level is set to 0 if unavailable 384b38b0665SH. Peter Anvin * and the legitimate extended levels are all negative 385b38b0665SH. Peter Anvin * when signed; hence the weird messing around with 386b38b0665SH. Peter Anvin * signs here... 387b38b0665SH. Peter Anvin */ 3880f3fa48aSIngo Molnar if (!((s32)df->level < 0 ? 389f6db44dfSYinghai Lu (u32)df->level > (u32)c->extended_cpuid_level : 3900f3fa48aSIngo Molnar (s32)df->level > (s32)c->cpuid_level)) 3910f3fa48aSIngo Molnar continue; 3920f3fa48aSIngo Molnar 393b38b0665SH. Peter Anvin clear_cpu_cap(c, df->feature); 3940f3fa48aSIngo Molnar if (!warn) 3950f3fa48aSIngo Molnar continue; 3960f3fa48aSIngo Molnar 3971b74dde7SChen Yucong pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n", 3989def39beSJosh Triplett x86_cap_flag(df->feature), df->level); 399b38b0665SH. Peter Anvin } 400b38b0665SH. Peter Anvin } 401b38b0665SH. Peter Anvin 402b38b0665SH. Peter Anvin /* 403f7627e25SThomas Gleixner * Naming convention should be: <Name> [(<Codename>)] 404f7627e25SThomas Gleixner * This table only is used unless init_<vendor>() below doesn't set it; 4050f3fa48aSIngo Molnar * in particular, if CPUID levels 0x80000002..4 are supported, this 4060f3fa48aSIngo Molnar * isn't used 407f7627e25SThomas Gleixner */ 408f7627e25SThomas Gleixner 409f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */ 410148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c) 411f7627e25SThomas Gleixner { 41209dc68d9SJan Beulich #ifdef CONFIG_X86_32 41309dc68d9SJan Beulich const struct legacy_cpu_model_info *info; 414f7627e25SThomas Gleixner 415f7627e25SThomas Gleixner if (c->x86_model >= 16) 416f7627e25SThomas Gleixner return NULL; /* Range check */ 417f7627e25SThomas Gleixner 418f7627e25SThomas Gleixner if (!this_cpu) 419f7627e25SThomas Gleixner return NULL; 420f7627e25SThomas Gleixner 42109dc68d9SJan Beulich info = this_cpu->legacy_models; 422f7627e25SThomas Gleixner 42309dc68d9SJan Beulich while (info->family) { 424f7627e25SThomas Gleixner if (info->family == c->x86) 425f7627e25SThomas Gleixner return info->model_names[c->x86_model]; 426f7627e25SThomas Gleixner info++; 427f7627e25SThomas Gleixner } 42809dc68d9SJan Beulich #endif 429f7627e25SThomas Gleixner return NULL; /* Not found */ 430f7627e25SThomas Gleixner } 431f7627e25SThomas Gleixner 432148f9bb8SPaul Gortmaker __u32 cpu_caps_cleared[NCAPINTS]; 433148f9bb8SPaul Gortmaker __u32 cpu_caps_set[NCAPINTS]; 434f7627e25SThomas Gleixner 43511e3a840SJeremy Fitzhardinge void load_percpu_segment(int cpu) 4369d31d35bSYinghai Lu { 437fab334c1SYinghai Lu #ifdef CONFIG_X86_32 4382697fbd5SBrian Gerst loadsegment(fs, __KERNEL_PERCPU); 4392697fbd5SBrian Gerst #else 44045e876f7SAndy Lutomirski __loadsegment_simple(gs, 0); 4412697fbd5SBrian Gerst wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu)); 442fab334c1SYinghai Lu #endif 44360a5317fSTejun Heo load_stack_canary_segment(); 4449d31d35bSYinghai Lu } 4459d31d35bSYinghai Lu 4460f3fa48aSIngo Molnar /* 4470f3fa48aSIngo Molnar * Current gdt points %fs at the "master" per-cpu area: after this, 4480f3fa48aSIngo Molnar * it's on the real one. 4490f3fa48aSIngo Molnar */ 450552be871SBrian Gerst void switch_to_new_gdt(int cpu) 451f7627e25SThomas Gleixner { 452f7627e25SThomas Gleixner struct desc_ptr gdt_descr; 453f7627e25SThomas Gleixner 454f7627e25SThomas Gleixner gdt_descr.address = (long)get_cpu_gdt_table(cpu); 455f7627e25SThomas Gleixner gdt_descr.size = GDT_SIZE - 1; 456f7627e25SThomas Gleixner load_gdt(&gdt_descr); 457f7627e25SThomas Gleixner /* Reload the per-cpu base */ 45811e3a840SJeremy Fitzhardinge 45911e3a840SJeremy Fitzhardinge load_percpu_segment(cpu); 460f7627e25SThomas Gleixner } 461f7627e25SThomas Gleixner 462148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {}; 463f7627e25SThomas Gleixner 464148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c) 465f7627e25SThomas Gleixner { 466f7627e25SThomas Gleixner unsigned int *v; 467ee098e1aSBorislav Petkov char *p, *q, *s; 468f7627e25SThomas Gleixner 4693da99c97SYinghai Lu if (c->extended_cpuid_level < 0x80000004) 4701b05d60dSYinghai Lu return; 471f7627e25SThomas Gleixner 472f7627e25SThomas Gleixner v = (unsigned int *)c->x86_model_id; 473f7627e25SThomas Gleixner cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]); 474f7627e25SThomas Gleixner cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]); 475f7627e25SThomas Gleixner cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]); 476f7627e25SThomas Gleixner c->x86_model_id[48] = 0; 477f7627e25SThomas Gleixner 478ee098e1aSBorislav Petkov /* Trim whitespace */ 479ee098e1aSBorislav Petkov p = q = s = &c->x86_model_id[0]; 480ee098e1aSBorislav Petkov 481ee098e1aSBorislav Petkov while (*p == ' ') 482ee098e1aSBorislav Petkov p++; 483ee098e1aSBorislav Petkov 484ee098e1aSBorislav Petkov while (*p) { 485ee098e1aSBorislav Petkov /* Note the last non-whitespace index */ 486ee098e1aSBorislav Petkov if (!isspace(*p)) 487ee098e1aSBorislav Petkov s = q; 488ee098e1aSBorislav Petkov 489ee098e1aSBorislav Petkov *q++ = *p++; 490ee098e1aSBorislav Petkov } 491ee098e1aSBorislav Petkov 492ee098e1aSBorislav Petkov *(s + 1) = '\0'; 493f7627e25SThomas Gleixner } 494f7627e25SThomas Gleixner 495148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c) 496f7627e25SThomas Gleixner { 4979d31d35bSYinghai Lu unsigned int n, dummy, ebx, ecx, edx, l2size; 498f7627e25SThomas Gleixner 4993da99c97SYinghai Lu n = c->extended_cpuid_level; 500f7627e25SThomas Gleixner 501f7627e25SThomas Gleixner if (n >= 0x80000005) { 5029d31d35bSYinghai Lu cpuid(0x80000005, &dummy, &ebx, &ecx, &edx); 503f7627e25SThomas Gleixner c->x86_cache_size = (ecx>>24) + (edx>>24); 504140fc727SYinghai Lu #ifdef CONFIG_X86_64 505140fc727SYinghai Lu /* On K8 L1 TLB is inclusive, so don't count it */ 506140fc727SYinghai Lu c->x86_tlbsize = 0; 507140fc727SYinghai Lu #endif 508f7627e25SThomas Gleixner } 509f7627e25SThomas Gleixner 510f7627e25SThomas Gleixner if (n < 0x80000006) /* Some chips just has a large L1. */ 511f7627e25SThomas Gleixner return; 512f7627e25SThomas Gleixner 5130a488a53SYinghai Lu cpuid(0x80000006, &dummy, &ebx, &ecx, &edx); 514f7627e25SThomas Gleixner l2size = ecx >> 16; 515f7627e25SThomas Gleixner 516140fc727SYinghai Lu #ifdef CONFIG_X86_64 517140fc727SYinghai Lu c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff); 518140fc727SYinghai Lu #else 519f7627e25SThomas Gleixner /* do processor-specific cache resizing */ 52009dc68d9SJan Beulich if (this_cpu->legacy_cache_size) 52109dc68d9SJan Beulich l2size = this_cpu->legacy_cache_size(c, l2size); 522f7627e25SThomas Gleixner 523f7627e25SThomas Gleixner /* Allow user to override all this if necessary. */ 524f7627e25SThomas Gleixner if (cachesize_override != -1) 525f7627e25SThomas Gleixner l2size = cachesize_override; 526f7627e25SThomas Gleixner 527f7627e25SThomas Gleixner if (l2size == 0) 528f7627e25SThomas Gleixner return; /* Again, no L2 cache is possible */ 529140fc727SYinghai Lu #endif 530f7627e25SThomas Gleixner 531f7627e25SThomas Gleixner c->x86_cache_size = l2size; 532f7627e25SThomas Gleixner } 533f7627e25SThomas Gleixner 534e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO]; 535e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO]; 536e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO]; 537e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO]; 538e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO]; 539e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO]; 540dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO]; 541e0ba94f1SAlex Shi 542f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c) 543e0ba94f1SAlex Shi { 544e0ba94f1SAlex Shi if (this_cpu->c_detect_tlb) 545e0ba94f1SAlex Shi this_cpu->c_detect_tlb(c); 546e0ba94f1SAlex Shi 547f94fe119SSteven Honeyman pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n", 548e0ba94f1SAlex Shi tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES], 549f94fe119SSteven Honeyman tlb_lli_4m[ENTRIES]); 550f94fe119SSteven Honeyman 551f94fe119SSteven Honeyman pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n", 552f94fe119SSteven Honeyman tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES], 553f94fe119SSteven Honeyman tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]); 554e0ba94f1SAlex Shi } 555e0ba94f1SAlex Shi 556148f9bb8SPaul Gortmaker void detect_ht(struct cpuinfo_x86 *c) 5579d31d35bSYinghai Lu { 558c8e56d20SBorislav Petkov #ifdef CONFIG_SMP 5599d31d35bSYinghai Lu u32 eax, ebx, ecx, edx; 5609d31d35bSYinghai Lu int index_msb, core_bits; 5612eaad1fdSMike Travis static bool printed; 5629d31d35bSYinghai Lu 5630a488a53SYinghai Lu if (!cpu_has(c, X86_FEATURE_HT)) 5649d31d35bSYinghai Lu return; 5659d31d35bSYinghai Lu 5660a488a53SYinghai Lu if (cpu_has(c, X86_FEATURE_CMP_LEGACY)) 5670a488a53SYinghai Lu goto out; 5680a488a53SYinghai Lu 5691cd78776SYinghai Lu if (cpu_has(c, X86_FEATURE_XTOPOLOGY)) 5701cd78776SYinghai Lu return; 5711cd78776SYinghai Lu 5720a488a53SYinghai Lu cpuid(1, &eax, &ebx, &ecx, &edx); 5730a488a53SYinghai Lu 5749d31d35bSYinghai Lu smp_num_siblings = (ebx & 0xff0000) >> 16; 5759d31d35bSYinghai Lu 5769d31d35bSYinghai Lu if (smp_num_siblings == 1) { 5771b74dde7SChen Yucong pr_info_once("CPU0: Hyper-Threading is disabled\n"); 5780f3fa48aSIngo Molnar goto out; 5790f3fa48aSIngo Molnar } 5800f3fa48aSIngo Molnar 5810f3fa48aSIngo Molnar if (smp_num_siblings <= 1) 5820f3fa48aSIngo Molnar goto out; 5839d31d35bSYinghai Lu 5849d31d35bSYinghai Lu index_msb = get_count_order(smp_num_siblings); 585cb8cc442SIngo Molnar c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb); 5869d31d35bSYinghai Lu 5879d31d35bSYinghai Lu smp_num_siblings = smp_num_siblings / c->x86_max_cores; 5889d31d35bSYinghai Lu 5899d31d35bSYinghai Lu index_msb = get_count_order(smp_num_siblings); 5909d31d35bSYinghai Lu 5919d31d35bSYinghai Lu core_bits = get_count_order(c->x86_max_cores); 5929d31d35bSYinghai Lu 593cb8cc442SIngo Molnar c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) & 5941cd78776SYinghai Lu ((1 << core_bits) - 1); 5959d31d35bSYinghai Lu 5960a488a53SYinghai Lu out: 5972eaad1fdSMike Travis if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) { 5981b74dde7SChen Yucong pr_info("CPU: Physical Processor ID: %d\n", 5990a488a53SYinghai Lu c->phys_proc_id); 6001b74dde7SChen Yucong pr_info("CPU: Processor Core ID: %d\n", 6019d31d35bSYinghai Lu c->cpu_core_id); 6022eaad1fdSMike Travis printed = 1; 6039d31d35bSYinghai Lu } 6049d31d35bSYinghai Lu #endif 60597e4db7cSYinghai Lu } 606f7627e25SThomas Gleixner 607148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c) 608f7627e25SThomas Gleixner { 609f7627e25SThomas Gleixner char *v = c->x86_vendor_id; 6100f3fa48aSIngo Molnar int i; 611f7627e25SThomas Gleixner 612f7627e25SThomas Gleixner for (i = 0; i < X86_VENDOR_NUM; i++) { 61310a434fcSYinghai Lu if (!cpu_devs[i]) 61410a434fcSYinghai Lu break; 61510a434fcSYinghai Lu 616f7627e25SThomas Gleixner if (!strcmp(v, cpu_devs[i]->c_ident[0]) || 617f7627e25SThomas Gleixner (cpu_devs[i]->c_ident[1] && 618f7627e25SThomas Gleixner !strcmp(v, cpu_devs[i]->c_ident[1]))) { 6190f3fa48aSIngo Molnar 620f7627e25SThomas Gleixner this_cpu = cpu_devs[i]; 62110a434fcSYinghai Lu c->x86_vendor = this_cpu->c_x86_vendor; 622f7627e25SThomas Gleixner return; 623f7627e25SThomas Gleixner } 624f7627e25SThomas Gleixner } 62510a434fcSYinghai Lu 6261b74dde7SChen Yucong pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \ 627a9c56953SMinchan Kim "CPU: Your system may be unstable.\n", v); 62810a434fcSYinghai Lu 629f7627e25SThomas Gleixner c->x86_vendor = X86_VENDOR_UNKNOWN; 630f7627e25SThomas Gleixner this_cpu = &default_cpu; 631f7627e25SThomas Gleixner } 632f7627e25SThomas Gleixner 633148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c) 634f7627e25SThomas Gleixner { 635f7627e25SThomas Gleixner /* Get vendor name */ 6364a148513SHarvey Harrison cpuid(0x00000000, (unsigned int *)&c->cpuid_level, 6374a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[0], 6384a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[8], 6394a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[4]); 640f7627e25SThomas Gleixner 641f7627e25SThomas Gleixner c->x86 = 4; 6429d31d35bSYinghai Lu /* Intel-defined flags: level 0x00000001 */ 643f7627e25SThomas Gleixner if (c->cpuid_level >= 0x00000001) { 644f7627e25SThomas Gleixner u32 junk, tfms, cap0, misc; 6450f3fa48aSIngo Molnar 646f7627e25SThomas Gleixner cpuid(0x00000001, &tfms, &misc, &junk, &cap0); 64799f925ceSBorislav Petkov c->x86 = x86_family(tfms); 64899f925ceSBorislav Petkov c->x86_model = x86_model(tfms); 64999f925ceSBorislav Petkov c->x86_mask = x86_stepping(tfms); 6500f3fa48aSIngo Molnar 651d4387bd3SHuang, Ying if (cap0 & (1<<19)) { 652d4387bd3SHuang, Ying c->x86_clflush_size = ((misc >> 8) & 0xff) * 8; 6539d31d35bSYinghai Lu c->x86_cache_alignment = c->x86_clflush_size; 654d4387bd3SHuang, Ying } 655f7627e25SThomas Gleixner } 656f7627e25SThomas Gleixner } 6573da99c97SYinghai Lu 658148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c) 659093af8d7SYinghai Lu { 66039c06df4SBorislav Petkov u32 eax, ebx, ecx, edx; 661093af8d7SYinghai Lu 662093af8d7SYinghai Lu /* Intel-defined flags: level 0x00000001 */ 663093af8d7SYinghai Lu if (c->cpuid_level >= 0x00000001) { 66439c06df4SBorislav Petkov cpuid(0x00000001, &eax, &ebx, &ecx, &edx); 6650f3fa48aSIngo Molnar 66639c06df4SBorislav Petkov c->x86_capability[CPUID_1_ECX] = ecx; 66739c06df4SBorislav Petkov c->x86_capability[CPUID_1_EDX] = edx; 668093af8d7SYinghai Lu } 669093af8d7SYinghai Lu 670bdc802dcSH. Peter Anvin /* Additional Intel-defined flags: level 0x00000007 */ 671bdc802dcSH. Peter Anvin if (c->cpuid_level >= 0x00000007) { 672bdc802dcSH. Peter Anvin cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx); 673bdc802dcSH. Peter Anvin 67439c06df4SBorislav Petkov c->x86_capability[CPUID_7_0_EBX] = ebx; 6752ccd71f1SBorislav Petkov 67639c06df4SBorislav Petkov c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006); 677dfb4a70fSDave Hansen c->x86_capability[CPUID_7_ECX] = ecx; 678bdc802dcSH. Peter Anvin } 679bdc802dcSH. Peter Anvin 6806229ad27SFenghua Yu /* Extended state features: level 0x0000000d */ 6816229ad27SFenghua Yu if (c->cpuid_level >= 0x0000000d) { 6826229ad27SFenghua Yu cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx); 6836229ad27SFenghua Yu 68439c06df4SBorislav Petkov c->x86_capability[CPUID_D_1_EAX] = eax; 6856229ad27SFenghua Yu } 6866229ad27SFenghua Yu 687cbc82b17SPeter P Waskiewicz Jr /* Additional Intel-defined flags: level 0x0000000F */ 688cbc82b17SPeter P Waskiewicz Jr if (c->cpuid_level >= 0x0000000F) { 689cbc82b17SPeter P Waskiewicz Jr 690cbc82b17SPeter P Waskiewicz Jr /* QoS sub-leaf, EAX=0Fh, ECX=0 */ 691cbc82b17SPeter P Waskiewicz Jr cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx); 69239c06df4SBorislav Petkov c->x86_capability[CPUID_F_0_EDX] = edx; 69339c06df4SBorislav Petkov 694cbc82b17SPeter P Waskiewicz Jr if (cpu_has(c, X86_FEATURE_CQM_LLC)) { 695cbc82b17SPeter P Waskiewicz Jr /* will be overridden if occupancy monitoring exists */ 696cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid = ebx; 697cbc82b17SPeter P Waskiewicz Jr 698cbc82b17SPeter P Waskiewicz Jr /* QoS sub-leaf, EAX=0Fh, ECX=1 */ 699cbc82b17SPeter P Waskiewicz Jr cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx); 70039c06df4SBorislav Petkov c->x86_capability[CPUID_F_1_EDX] = edx; 70139c06df4SBorislav Petkov 70233c3cc7aSVikas Shivappa if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) || 70333c3cc7aSVikas Shivappa ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) || 70433c3cc7aSVikas Shivappa (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) { 705cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid = ecx; 706cbc82b17SPeter P Waskiewicz Jr c->x86_cache_occ_scale = ebx; 707cbc82b17SPeter P Waskiewicz Jr } 708cbc82b17SPeter P Waskiewicz Jr } else { 709cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid = -1; 710cbc82b17SPeter P Waskiewicz Jr c->x86_cache_occ_scale = -1; 711cbc82b17SPeter P Waskiewicz Jr } 712cbc82b17SPeter P Waskiewicz Jr } 713cbc82b17SPeter P Waskiewicz Jr 714093af8d7SYinghai Lu /* AMD-defined flags: level 0x80000001 */ 71539c06df4SBorislav Petkov eax = cpuid_eax(0x80000000); 71639c06df4SBorislav Petkov c->extended_cpuid_level = eax; 7170f3fa48aSIngo Molnar 71839c06df4SBorislav Petkov if ((eax & 0xffff0000) == 0x80000000) { 71939c06df4SBorislav Petkov if (eax >= 0x80000001) { 72039c06df4SBorislav Petkov cpuid(0x80000001, &eax, &ebx, &ecx, &edx); 72139c06df4SBorislav Petkov 72239c06df4SBorislav Petkov c->x86_capability[CPUID_8000_0001_ECX] = ecx; 72339c06df4SBorislav Petkov c->x86_capability[CPUID_8000_0001_EDX] = edx; 724093af8d7SYinghai Lu } 725093af8d7SYinghai Lu } 726093af8d7SYinghai Lu 72771faad43SYazen Ghannam if (c->extended_cpuid_level >= 0x80000007) { 72871faad43SYazen Ghannam cpuid(0x80000007, &eax, &ebx, &ecx, &edx); 72971faad43SYazen Ghannam 73071faad43SYazen Ghannam c->x86_capability[CPUID_8000_0007_EBX] = ebx; 73171faad43SYazen Ghannam c->x86_power = edx; 73271faad43SYazen Ghannam } 73371faad43SYazen Ghannam 7345122c890SYinghai Lu if (c->extended_cpuid_level >= 0x80000008) { 73539c06df4SBorislav Petkov cpuid(0x80000008, &eax, &ebx, &ecx, &edx); 7365122c890SYinghai Lu 7375122c890SYinghai Lu c->x86_virt_bits = (eax >> 8) & 0xff; 7385122c890SYinghai Lu c->x86_phys_bits = eax & 0xff; 73939c06df4SBorislav Petkov c->x86_capability[CPUID_8000_0008_EBX] = ebx; 7405122c890SYinghai Lu } 74113c6c532SJan Beulich #ifdef CONFIG_X86_32 74213c6c532SJan Beulich else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36)) 74313c6c532SJan Beulich c->x86_phys_bits = 36; 7445122c890SYinghai Lu #endif 745e3224234SYinghai Lu 7462ccd71f1SBorislav Petkov if (c->extended_cpuid_level >= 0x8000000a) 74739c06df4SBorislav Petkov c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a); 7482ccd71f1SBorislav Petkov 7491dedefd1SJacob Pan init_scattered_cpuid_features(c); 750093af8d7SYinghai Lu } 751093af8d7SYinghai Lu 752148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c) 753aef93c8bSYinghai Lu { 754aef93c8bSYinghai Lu #ifdef CONFIG_X86_32 755aef93c8bSYinghai Lu int i; 756aef93c8bSYinghai Lu 757aef93c8bSYinghai Lu /* 758aef93c8bSYinghai Lu * First of all, decide if this is a 486 or higher 759aef93c8bSYinghai Lu * It's a 486 if we can modify the AC flag 760aef93c8bSYinghai Lu */ 761aef93c8bSYinghai Lu if (flag_is_changeable_p(X86_EFLAGS_AC)) 762aef93c8bSYinghai Lu c->x86 = 4; 763aef93c8bSYinghai Lu else 764aef93c8bSYinghai Lu c->x86 = 3; 765aef93c8bSYinghai Lu 766aef93c8bSYinghai Lu for (i = 0; i < X86_VENDOR_NUM; i++) 767aef93c8bSYinghai Lu if (cpu_devs[i] && cpu_devs[i]->c_identify) { 768aef93c8bSYinghai Lu c->x86_vendor_id[0] = 0; 769aef93c8bSYinghai Lu cpu_devs[i]->c_identify(c); 770aef93c8bSYinghai Lu if (c->x86_vendor_id[0]) { 771aef93c8bSYinghai Lu get_cpu_vendor(c); 772aef93c8bSYinghai Lu break; 773aef93c8bSYinghai Lu } 774aef93c8bSYinghai Lu } 775aef93c8bSYinghai Lu #endif 776093af8d7SYinghai Lu } 777f7627e25SThomas Gleixner 77834048c9eSPaolo Ciarrocchi /* 77934048c9eSPaolo Ciarrocchi * Do minimum CPU detection early. 78034048c9eSPaolo Ciarrocchi * Fields really needed: vendor, cpuid_level, family, model, mask, 78134048c9eSPaolo Ciarrocchi * cache alignment. 78234048c9eSPaolo Ciarrocchi * The others are not touched to avoid unwanted side effects. 78334048c9eSPaolo Ciarrocchi * 78434048c9eSPaolo Ciarrocchi * WARNING: this function is only called on the BP. Don't add code here 78534048c9eSPaolo Ciarrocchi * that is supposed to run on all CPUs. 78634048c9eSPaolo Ciarrocchi */ 7873da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c) 788f7627e25SThomas Gleixner { 7896627d242SYinghai Lu #ifdef CONFIG_X86_64 7906627d242SYinghai Lu c->x86_clflush_size = 64; 79113c6c532SJan Beulich c->x86_phys_bits = 36; 79213c6c532SJan Beulich c->x86_virt_bits = 48; 7936627d242SYinghai Lu #else 794d4387bd3SHuang, Ying c->x86_clflush_size = 32; 79513c6c532SJan Beulich c->x86_phys_bits = 32; 79613c6c532SJan Beulich c->x86_virt_bits = 32; 7976627d242SYinghai Lu #endif 7980a488a53SYinghai Lu c->x86_cache_alignment = c->x86_clflush_size; 799f7627e25SThomas Gleixner 8003da99c97SYinghai Lu memset(&c->x86_capability, 0, sizeof c->x86_capability); 8010a488a53SYinghai Lu c->extended_cpuid_level = 0; 8020a488a53SYinghai Lu 803aef93c8bSYinghai Lu if (!have_cpuid_p()) 804aef93c8bSYinghai Lu identify_cpu_without_cpuid(c); 805aef93c8bSYinghai Lu 806aef93c8bSYinghai Lu /* cyrix could have cpuid enabled via c_identify()*/ 80705fb3c19SAndy Lutomirski if (have_cpuid_p()) { 808f7627e25SThomas Gleixner cpu_detect(c); 8093da99c97SYinghai Lu get_cpu_vendor(c); 8103da99c97SYinghai Lu get_cpu_cap(c); 81112cf105cSKrzysztof Helt 81210a434fcSYinghai Lu if (this_cpu->c_early_init) 81310a434fcSYinghai Lu this_cpu->c_early_init(c); 8143da99c97SYinghai Lu 815f6e9456cSRobert Richter c->cpu_index = 0; 816b38b0665SH. Peter Anvin filter_cpuid_features(c, false); 817de5397adSFenghua Yu 818a110b5ecSBorislav Petkov if (this_cpu->c_bsp_init) 819a110b5ecSBorislav Petkov this_cpu->c_bsp_init(c); 82005fb3c19SAndy Lutomirski } 821c3b83598SBorislav Petkov 822c3b83598SBorislav Petkov setup_force_cpu_cap(X86_FEATURE_ALWAYS); 823db52ef74SIngo Molnar fpu__init_system(c); 824f7627e25SThomas Gleixner } 825f7627e25SThomas Gleixner 8269d31d35bSYinghai Lu void __init early_cpu_init(void) 8279d31d35bSYinghai Lu { 82802dde8b4SJan Beulich const struct cpu_dev *const *cdev; 82910a434fcSYinghai Lu int count = 0; 8309d31d35bSYinghai Lu 831ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT 8321b74dde7SChen Yucong pr_info("KERNEL supported cpus:\n"); 83331c997caSIngo Molnar #endif 83431c997caSIngo Molnar 83510a434fcSYinghai Lu for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) { 83602dde8b4SJan Beulich const struct cpu_dev *cpudev = *cdev; 8379d31d35bSYinghai Lu 83810a434fcSYinghai Lu if (count >= X86_VENDOR_NUM) 83910a434fcSYinghai Lu break; 84010a434fcSYinghai Lu cpu_devs[count] = cpudev; 84110a434fcSYinghai Lu count++; 84210a434fcSYinghai Lu 843ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT 84431c997caSIngo Molnar { 84531c997caSIngo Molnar unsigned int j; 84631c997caSIngo Molnar 84710a434fcSYinghai Lu for (j = 0; j < 2; j++) { 84810a434fcSYinghai Lu if (!cpudev->c_ident[j]) 84910a434fcSYinghai Lu continue; 8501b74dde7SChen Yucong pr_info(" %s %s\n", cpudev->c_vendor, 85110a434fcSYinghai Lu cpudev->c_ident[j]); 85210a434fcSYinghai Lu } 85310a434fcSYinghai Lu } 8540388423dSDave Jones #endif 85531c997caSIngo Molnar } 8569d31d35bSYinghai Lu early_identify_cpu(&boot_cpu_data); 857f7627e25SThomas Gleixner } 858f7627e25SThomas Gleixner 859b6734c35SH. Peter Anvin /* 860366d4a43SBorislav Petkov * The NOPL instruction is supposed to exist on all CPUs of family >= 6; 861366d4a43SBorislav Petkov * unfortunately, that's not true in practice because of early VIA 862366d4a43SBorislav Petkov * chips and (more importantly) broken virtualizers that are not easy 863366d4a43SBorislav Petkov * to detect. In the latter case it doesn't even *fail* reliably, so 864366d4a43SBorislav Petkov * probing for it doesn't even work. Disable it completely on 32-bit 865ba0593bfSH. Peter Anvin * unless we can find a reliable way to detect all the broken cases. 866366d4a43SBorislav Petkov * Enable it explicitly on 64-bit for non-constant inputs of cpu_has(). 867b6734c35SH. Peter Anvin */ 868148f9bb8SPaul Gortmaker static void detect_nopl(struct cpuinfo_x86 *c) 869b6734c35SH. Peter Anvin { 870366d4a43SBorislav Petkov #ifdef CONFIG_X86_32 871b6734c35SH. Peter Anvin clear_cpu_cap(c, X86_FEATURE_NOPL); 872366d4a43SBorislav Petkov #else 873366d4a43SBorislav Petkov set_cpu_cap(c, X86_FEATURE_NOPL); 874366d4a43SBorislav Petkov #endif 875f7627e25SThomas Gleixner } 876f7627e25SThomas Gleixner 8777a5d6704SAndy Lutomirski static void detect_null_seg_behavior(struct cpuinfo_x86 *c) 8787a5d6704SAndy Lutomirski { 8797a5d6704SAndy Lutomirski #ifdef CONFIG_X86_64 880f7627e25SThomas Gleixner /* 8817a5d6704SAndy Lutomirski * Empirically, writing zero to a segment selector on AMD does 8827a5d6704SAndy Lutomirski * not clear the base, whereas writing zero to a segment 8837a5d6704SAndy Lutomirski * selector on Intel does clear the base. Intel's behavior 8847a5d6704SAndy Lutomirski * allows slightly faster context switches in the common case 8857a5d6704SAndy Lutomirski * where GS is unused by the prev and next threads. 886f7627e25SThomas Gleixner * 8877a5d6704SAndy Lutomirski * Since neither vendor documents this anywhere that I can see, 8887a5d6704SAndy Lutomirski * detect it directly instead of hardcoding the choice by 8897a5d6704SAndy Lutomirski * vendor. 8907a5d6704SAndy Lutomirski * 8917a5d6704SAndy Lutomirski * I've designated AMD's behavior as the "bug" because it's 8927a5d6704SAndy Lutomirski * counterintuitive and less friendly. 893f7627e25SThomas Gleixner */ 8947a5d6704SAndy Lutomirski 8957a5d6704SAndy Lutomirski unsigned long old_base, tmp; 8967a5d6704SAndy Lutomirski rdmsrl(MSR_FS_BASE, old_base); 8977a5d6704SAndy Lutomirski wrmsrl(MSR_FS_BASE, 1); 8987a5d6704SAndy Lutomirski loadsegment(fs, 0); 8997a5d6704SAndy Lutomirski rdmsrl(MSR_FS_BASE, tmp); 9007a5d6704SAndy Lutomirski if (tmp != 0) 9017a5d6704SAndy Lutomirski set_cpu_bug(c, X86_BUG_NULL_SEG); 9027a5d6704SAndy Lutomirski wrmsrl(MSR_FS_BASE, old_base); 9033da99c97SYinghai Lu #endif 904f7627e25SThomas Gleixner } 905aef93c8bSYinghai Lu 906148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c) 907f7627e25SThomas Gleixner { 908f7627e25SThomas Gleixner c->extended_cpuid_level = 0; 909f7627e25SThomas Gleixner 910aef93c8bSYinghai Lu if (!have_cpuid_p()) 911aef93c8bSYinghai Lu identify_cpu_without_cpuid(c); 912f7627e25SThomas Gleixner 913aef93c8bSYinghai Lu /* cyrix could have cpuid enabled via c_identify()*/ 914a9853dd6SIngo Molnar if (!have_cpuid_p()) 915aef93c8bSYinghai Lu return; 916aef93c8bSYinghai Lu 9173da99c97SYinghai Lu cpu_detect(c); 9183da99c97SYinghai Lu 9193da99c97SYinghai Lu get_cpu_vendor(c); 9203da99c97SYinghai Lu 9213da99c97SYinghai Lu get_cpu_cap(c); 9223da99c97SYinghai Lu 923f7627e25SThomas Gleixner if (c->cpuid_level >= 0x00000001) { 9243da99c97SYinghai Lu c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF; 925b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32 926c8e56d20SBorislav Petkov # ifdef CONFIG_SMP 927cb8cc442SIngo Molnar c->apicid = apic->phys_pkg_id(c->initial_apicid, 0); 928f7627e25SThomas Gleixner # else 92901aaea1aSYinghai Lu c->apicid = c->initial_apicid; 930f7627e25SThomas Gleixner # endif 931b89d3b3eSYinghai Lu #endif 932b89d3b3eSYinghai Lu c->phys_proc_id = c->initial_apicid; 933f7627e25SThomas Gleixner } 934f7627e25SThomas Gleixner 935f7627e25SThomas Gleixner get_model_name(c); /* Default name */ 936f7627e25SThomas Gleixner 937b6734c35SH. Peter Anvin detect_nopl(c); 9387a5d6704SAndy Lutomirski 9397a5d6704SAndy Lutomirski detect_null_seg_behavior(c); 9400230bb03SAndy Lutomirski 9410230bb03SAndy Lutomirski /* 9420230bb03SAndy Lutomirski * ESPFIX is a strange bug. All real CPUs have it. Paravirt 9430230bb03SAndy Lutomirski * systems that run Linux at CPL > 0 may or may not have the 9440230bb03SAndy Lutomirski * issue, but, even if they have the issue, there's absolutely 9450230bb03SAndy Lutomirski * nothing we can do about it because we can't use the real IRET 9460230bb03SAndy Lutomirski * instruction. 9470230bb03SAndy Lutomirski * 9480230bb03SAndy Lutomirski * NB: For the time being, only 32-bit kernels support 9490230bb03SAndy Lutomirski * X86_BUG_ESPFIX as such. 64-bit kernels directly choose 9500230bb03SAndy Lutomirski * whether to apply espfix using paravirt hooks. If any 9510230bb03SAndy Lutomirski * non-paravirt system ever shows up that does *not* have the 9520230bb03SAndy Lutomirski * ESPFIX issue, we can change this. 9530230bb03SAndy Lutomirski */ 9540230bb03SAndy Lutomirski #ifdef CONFIG_X86_32 9550230bb03SAndy Lutomirski # ifdef CONFIG_PARAVIRT 9560230bb03SAndy Lutomirski do { 9570230bb03SAndy Lutomirski extern void native_iret(void); 9580230bb03SAndy Lutomirski if (pv_cpu_ops.iret == native_iret) 9590230bb03SAndy Lutomirski set_cpu_bug(c, X86_BUG_ESPFIX); 9600230bb03SAndy Lutomirski } while (0); 9610230bb03SAndy Lutomirski # else 9620230bb03SAndy Lutomirski set_cpu_bug(c, X86_BUG_ESPFIX); 9630230bb03SAndy Lutomirski # endif 9640230bb03SAndy Lutomirski #endif 965f7627e25SThomas Gleixner } 966f7627e25SThomas Gleixner 967cbc82b17SPeter P Waskiewicz Jr static void x86_init_cache_qos(struct cpuinfo_x86 *c) 968cbc82b17SPeter P Waskiewicz Jr { 969cbc82b17SPeter P Waskiewicz Jr /* 970cbc82b17SPeter P Waskiewicz Jr * The heavy lifting of max_rmid and cache_occ_scale are handled 971cbc82b17SPeter P Waskiewicz Jr * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu 972cbc82b17SPeter P Waskiewicz Jr * in case CQM bits really aren't there in this CPU. 973cbc82b17SPeter P Waskiewicz Jr */ 974cbc82b17SPeter P Waskiewicz Jr if (c != &boot_cpu_data) { 975cbc82b17SPeter P Waskiewicz Jr boot_cpu_data.x86_cache_max_rmid = 976cbc82b17SPeter P Waskiewicz Jr min(boot_cpu_data.x86_cache_max_rmid, 977cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid); 978cbc82b17SPeter P Waskiewicz Jr } 979cbc82b17SPeter P Waskiewicz Jr } 980cbc82b17SPeter P Waskiewicz Jr 981f7627e25SThomas Gleixner /* 982*d49597fdSThomas Gleixner * The physical to logical package id mapping is initialized from the 983*d49597fdSThomas Gleixner * acpi/mptables information. Make sure that CPUID actually agrees with 984*d49597fdSThomas Gleixner * that. 985*d49597fdSThomas Gleixner */ 986*d49597fdSThomas Gleixner static void sanitize_package_id(struct cpuinfo_x86 *c) 987*d49597fdSThomas Gleixner { 988*d49597fdSThomas Gleixner #ifdef CONFIG_SMP 989*d49597fdSThomas Gleixner unsigned int pkg, apicid, cpu = smp_processor_id(); 990*d49597fdSThomas Gleixner 991*d49597fdSThomas Gleixner apicid = apic->cpu_present_to_apicid(cpu); 992*d49597fdSThomas Gleixner pkg = apicid >> boot_cpu_data.x86_coreid_bits; 993*d49597fdSThomas Gleixner 994*d49597fdSThomas Gleixner if (apicid != c->initial_apicid) { 995*d49597fdSThomas Gleixner pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x CPUID: %x\n", 996*d49597fdSThomas Gleixner cpu, apicid, c->initial_apicid); 997*d49597fdSThomas Gleixner c->initial_apicid = apicid; 998*d49597fdSThomas Gleixner } 999*d49597fdSThomas Gleixner if (pkg != c->phys_proc_id) { 1000*d49597fdSThomas Gleixner pr_err(FW_BUG "CPU%u: Using firmware package id %u instead of %u\n", 1001*d49597fdSThomas Gleixner cpu, pkg, c->phys_proc_id); 1002*d49597fdSThomas Gleixner c->phys_proc_id = pkg; 1003*d49597fdSThomas Gleixner } 1004*d49597fdSThomas Gleixner c->logical_proc_id = topology_phys_to_logical_pkg(pkg); 1005*d49597fdSThomas Gleixner #else 1006*d49597fdSThomas Gleixner c->logical_proc_id = 0; 1007*d49597fdSThomas Gleixner #endif 1008*d49597fdSThomas Gleixner } 1009*d49597fdSThomas Gleixner 1010*d49597fdSThomas Gleixner /* 1011f7627e25SThomas Gleixner * This does the hard work of actually picking apart the CPU stuff... 1012f7627e25SThomas Gleixner */ 1013148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c) 1014f7627e25SThomas Gleixner { 1015f7627e25SThomas Gleixner int i; 1016f7627e25SThomas Gleixner 1017f7627e25SThomas Gleixner c->loops_per_jiffy = loops_per_jiffy; 1018f7627e25SThomas Gleixner c->x86_cache_size = -1; 1019f7627e25SThomas Gleixner c->x86_vendor = X86_VENDOR_UNKNOWN; 1020f7627e25SThomas Gleixner c->x86_model = c->x86_mask = 0; /* So far unknown... */ 1021f7627e25SThomas Gleixner c->x86_vendor_id[0] = '\0'; /* Unset */ 1022f7627e25SThomas Gleixner c->x86_model_id[0] = '\0'; /* Unset */ 1023f7627e25SThomas Gleixner c->x86_max_cores = 1; 1024102bbe3aSYinghai Lu c->x86_coreid_bits = 0; 102511fdd252SYinghai Lu #ifdef CONFIG_X86_64 1026102bbe3aSYinghai Lu c->x86_clflush_size = 64; 102713c6c532SJan Beulich c->x86_phys_bits = 36; 102813c6c532SJan Beulich c->x86_virt_bits = 48; 1029102bbe3aSYinghai Lu #else 1030102bbe3aSYinghai Lu c->cpuid_level = -1; /* CPUID not detected */ 1031f7627e25SThomas Gleixner c->x86_clflush_size = 32; 103213c6c532SJan Beulich c->x86_phys_bits = 32; 103313c6c532SJan Beulich c->x86_virt_bits = 32; 1034102bbe3aSYinghai Lu #endif 1035102bbe3aSYinghai Lu c->x86_cache_alignment = c->x86_clflush_size; 1036f7627e25SThomas Gleixner memset(&c->x86_capability, 0, sizeof c->x86_capability); 1037f7627e25SThomas Gleixner 1038f7627e25SThomas Gleixner generic_identify(c); 1039f7627e25SThomas Gleixner 10403898534dSAndi Kleen if (this_cpu->c_identify) 1041f7627e25SThomas Gleixner this_cpu->c_identify(c); 1042f7627e25SThomas Gleixner 10436a6256f9SAdam Buchbinder /* Clear/Set all flags overridden by options, after probe */ 10442759c328SYinghai Lu for (i = 0; i < NCAPINTS; i++) { 10452759c328SYinghai Lu c->x86_capability[i] &= ~cpu_caps_cleared[i]; 10462759c328SYinghai Lu c->x86_capability[i] |= cpu_caps_set[i]; 10472759c328SYinghai Lu } 10482759c328SYinghai Lu 1049102bbe3aSYinghai Lu #ifdef CONFIG_X86_64 1050cb8cc442SIngo Molnar c->apicid = apic->phys_pkg_id(c->initial_apicid, 0); 1051102bbe3aSYinghai Lu #endif 1052102bbe3aSYinghai Lu 1053f7627e25SThomas Gleixner /* 1054f7627e25SThomas Gleixner * Vendor-specific initialization. In this section we 1055f7627e25SThomas Gleixner * canonicalize the feature flags, meaning if there are 1056f7627e25SThomas Gleixner * features a certain CPU supports which CPUID doesn't 1057f7627e25SThomas Gleixner * tell us, CPUID claiming incorrect flags, or other bugs, 1058f7627e25SThomas Gleixner * we handle them here. 1059f7627e25SThomas Gleixner * 1060f7627e25SThomas Gleixner * At the end of this section, c->x86_capability better 1061f7627e25SThomas Gleixner * indicate the features this CPU genuinely supports! 1062f7627e25SThomas Gleixner */ 1063f7627e25SThomas Gleixner if (this_cpu->c_init) 1064f7627e25SThomas Gleixner this_cpu->c_init(c); 1065f7627e25SThomas Gleixner 1066f7627e25SThomas Gleixner /* Disable the PN if appropriate */ 1067f7627e25SThomas Gleixner squash_the_stupid_serial_number(c); 1068f7627e25SThomas Gleixner 1069b2cc2a07SH. Peter Anvin /* Set up SMEP/SMAP */ 1070b2cc2a07SH. Peter Anvin setup_smep(c); 1071b2cc2a07SH. Peter Anvin setup_smap(c); 1072b2cc2a07SH. Peter Anvin 1073f7627e25SThomas Gleixner /* 10740f3fa48aSIngo Molnar * The vendor-specific functions might have changed features. 10750f3fa48aSIngo Molnar * Now we do "generic changes." 1076f7627e25SThomas Gleixner */ 1077f7627e25SThomas Gleixner 1078b38b0665SH. Peter Anvin /* Filter out anything that depends on CPUID levels we don't have */ 1079b38b0665SH. Peter Anvin filter_cpuid_features(c, true); 1080b38b0665SH. Peter Anvin 1081f7627e25SThomas Gleixner /* If the model name is still unset, do table lookup. */ 1082f7627e25SThomas Gleixner if (!c->x86_model_id[0]) { 108302dde8b4SJan Beulich const char *p; 1084f7627e25SThomas Gleixner p = table_lookup_model(c); 1085f7627e25SThomas Gleixner if (p) 1086f7627e25SThomas Gleixner strcpy(c->x86_model_id, p); 1087f7627e25SThomas Gleixner else 1088f7627e25SThomas Gleixner /* Last resort... */ 1089f7627e25SThomas Gleixner sprintf(c->x86_model_id, "%02x/%02x", 1090f7627e25SThomas Gleixner c->x86, c->x86_model); 1091f7627e25SThomas Gleixner } 1092f7627e25SThomas Gleixner 1093102bbe3aSYinghai Lu #ifdef CONFIG_X86_64 1094102bbe3aSYinghai Lu detect_ht(c); 1095102bbe3aSYinghai Lu #endif 1096102bbe3aSYinghai Lu 109788b094fbSAlok Kataria init_hypervisor(c); 109849d859d7SH. Peter Anvin x86_init_rdrand(c); 1099cbc82b17SPeter P Waskiewicz Jr x86_init_cache_qos(c); 110006976945SDave Hansen setup_pku(c); 11013e0c3737SYinghai Lu 11023e0c3737SYinghai Lu /* 11036a6256f9SAdam Buchbinder * Clear/Set all flags overridden by options, need do it 11043e0c3737SYinghai Lu * before following smp all cpus cap AND. 11053e0c3737SYinghai Lu */ 11063e0c3737SYinghai Lu for (i = 0; i < NCAPINTS; i++) { 11073e0c3737SYinghai Lu c->x86_capability[i] &= ~cpu_caps_cleared[i]; 11083e0c3737SYinghai Lu c->x86_capability[i] |= cpu_caps_set[i]; 11093e0c3737SYinghai Lu } 11103e0c3737SYinghai Lu 1111f7627e25SThomas Gleixner /* 1112f7627e25SThomas Gleixner * On SMP, boot_cpu_data holds the common feature set between 1113f7627e25SThomas Gleixner * all CPUs; so make sure that we indicate which features are 1114f7627e25SThomas Gleixner * common between the CPUs. The first time this routine gets 1115f7627e25SThomas Gleixner * executed, c == &boot_cpu_data. 1116f7627e25SThomas Gleixner */ 1117f7627e25SThomas Gleixner if (c != &boot_cpu_data) { 1118f7627e25SThomas Gleixner /* AND the already accumulated flags with these */ 1119f7627e25SThomas Gleixner for (i = 0; i < NCAPINTS; i++) 1120f7627e25SThomas Gleixner boot_cpu_data.x86_capability[i] &= c->x86_capability[i]; 112165fc985bSBorislav Petkov 112265fc985bSBorislav Petkov /* OR, i.e. replicate the bug flags */ 112365fc985bSBorislav Petkov for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++) 112465fc985bSBorislav Petkov c->x86_capability[i] |= boot_cpu_data.x86_capability[i]; 1125f7627e25SThomas Gleixner } 1126f7627e25SThomas Gleixner 1127f7627e25SThomas Gleixner /* Init Machine Check Exception if available. */ 11285e09954aSBorislav Petkov mcheck_cpu_init(c); 112930d432dfSAndi Kleen 113030d432dfSAndi Kleen select_idle_routine(c); 1131102bbe3aSYinghai Lu 1132de2d9445STejun Heo #ifdef CONFIG_NUMA 1133102bbe3aSYinghai Lu numa_add_cpu(smp_processor_id()); 1134102bbe3aSYinghai Lu #endif 1135*d49597fdSThomas Gleixner sanitize_package_id(c); 1136f7627e25SThomas Gleixner } 1137f7627e25SThomas Gleixner 11388b6c0ab1SIngo Molnar /* 11398b6c0ab1SIngo Molnar * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions 11408b6c0ab1SIngo Molnar * on 32-bit kernels: 11418b6c0ab1SIngo Molnar */ 1142cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32 1143cfda7bb9SAndy Lutomirski void enable_sep_cpu(void) 1144cfda7bb9SAndy Lutomirski { 11458b6c0ab1SIngo Molnar struct tss_struct *tss; 11468b6c0ab1SIngo Molnar int cpu; 1147cfda7bb9SAndy Lutomirski 1148b3edfda4SBorislav Petkov if (!boot_cpu_has(X86_FEATURE_SEP)) 1149b3edfda4SBorislav Petkov return; 1150b3edfda4SBorislav Petkov 11518b6c0ab1SIngo Molnar cpu = get_cpu(); 11528b6c0ab1SIngo Molnar tss = &per_cpu(cpu_tss, cpu); 11538b6c0ab1SIngo Molnar 11548b6c0ab1SIngo Molnar /* 1155cf9328ccSAndy Lutomirski * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field -- 1156cf9328ccSAndy Lutomirski * see the big comment in struct x86_hw_tss's definition. 11578b6c0ab1SIngo Molnar */ 1158cfda7bb9SAndy Lutomirski 1159cfda7bb9SAndy Lutomirski tss->x86_tss.ss1 = __KERNEL_CS; 11608b6c0ab1SIngo Molnar wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0); 11618b6c0ab1SIngo Molnar 1162cf9328ccSAndy Lutomirski wrmsr(MSR_IA32_SYSENTER_ESP, 1163cf9328ccSAndy Lutomirski (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack), 1164cf9328ccSAndy Lutomirski 0); 11658b6c0ab1SIngo Molnar 11664c8cd0c5SIngo Molnar wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0); 11678b6c0ab1SIngo Molnar 1168cfda7bb9SAndy Lutomirski put_cpu(); 1169cfda7bb9SAndy Lutomirski } 1170e04d645fSGlauber Costa #endif 1171e04d645fSGlauber Costa 1172f7627e25SThomas Gleixner void __init identify_boot_cpu(void) 1173f7627e25SThomas Gleixner { 1174f7627e25SThomas Gleixner identify_cpu(&boot_cpu_data); 117502c68a02SLen Brown init_amd_e400_c1e_mask(); 1176102bbe3aSYinghai Lu #ifdef CONFIG_X86_32 1177f7627e25SThomas Gleixner sysenter_setup(); 1178f7627e25SThomas Gleixner enable_sep_cpu(); 1179102bbe3aSYinghai Lu #endif 1180e0ba94f1SAlex Shi cpu_detect_tlb(&boot_cpu_data); 1181f7627e25SThomas Gleixner } 1182f7627e25SThomas Gleixner 1183148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c) 1184f7627e25SThomas Gleixner { 1185f7627e25SThomas Gleixner BUG_ON(c == &boot_cpu_data); 1186f7627e25SThomas Gleixner identify_cpu(c); 1187102bbe3aSYinghai Lu #ifdef CONFIG_X86_32 1188f7627e25SThomas Gleixner enable_sep_cpu(); 1189102bbe3aSYinghai Lu #endif 1190f7627e25SThomas Gleixner mtrr_ap_init(); 1191f7627e25SThomas Gleixner } 1192f7627e25SThomas Gleixner 1193a0854a46SYinghai Lu struct msr_range { 1194a0854a46SYinghai Lu unsigned min; 1195a0854a46SYinghai Lu unsigned max; 1196a0854a46SYinghai Lu }; 1197a0854a46SYinghai Lu 1198148f9bb8SPaul Gortmaker static const struct msr_range msr_range_array[] = { 1199a0854a46SYinghai Lu { 0x00000000, 0x00000418}, 1200a0854a46SYinghai Lu { 0xc0000000, 0xc000040b}, 1201a0854a46SYinghai Lu { 0xc0010000, 0xc0010142}, 1202a0854a46SYinghai Lu { 0xc0011000, 0xc001103b}, 1203a0854a46SYinghai Lu }; 1204a0854a46SYinghai Lu 1205148f9bb8SPaul Gortmaker static void __print_cpu_msr(void) 1206f7627e25SThomas Gleixner { 12070f3fa48aSIngo Molnar unsigned index_min, index_max; 1208a0854a46SYinghai Lu unsigned index; 1209a0854a46SYinghai Lu u64 val; 1210a0854a46SYinghai Lu int i; 1211f7627e25SThomas Gleixner 1212a0854a46SYinghai Lu for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) { 1213a0854a46SYinghai Lu index_min = msr_range_array[i].min; 1214a0854a46SYinghai Lu index_max = msr_range_array[i].max; 12150f3fa48aSIngo Molnar 1216a0854a46SYinghai Lu for (index = index_min; index < index_max; index++) { 1217ecd431d9SBorislav Petkov if (rdmsrl_safe(index, &val)) 1218a0854a46SYinghai Lu continue; 12191b74dde7SChen Yucong pr_info(" MSR%08x: %016llx\n", index, val); 1220f7627e25SThomas Gleixner } 1221f7627e25SThomas Gleixner } 1222a0854a46SYinghai Lu } 1223a0854a46SYinghai Lu 1224148f9bb8SPaul Gortmaker static int show_msr; 12250f3fa48aSIngo Molnar 1226a0854a46SYinghai Lu static __init int setup_show_msr(char *arg) 1227a0854a46SYinghai Lu { 1228a0854a46SYinghai Lu int num; 1229a0854a46SYinghai Lu 1230a0854a46SYinghai Lu get_option(&arg, &num); 1231a0854a46SYinghai Lu 1232a0854a46SYinghai Lu if (num > 0) 1233a0854a46SYinghai Lu show_msr = num; 1234a0854a46SYinghai Lu return 1; 1235a0854a46SYinghai Lu } 1236a0854a46SYinghai Lu __setup("show_msr=", setup_show_msr); 1237f7627e25SThomas Gleixner 1238191679fdSAndi Kleen static __init int setup_noclflush(char *arg) 1239191679fdSAndi Kleen { 1240840d2830SH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_CLFLUSH); 1241da4aaa7dSH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT); 1242191679fdSAndi Kleen return 1; 1243191679fdSAndi Kleen } 1244191679fdSAndi Kleen __setup("noclflush", setup_noclflush); 1245191679fdSAndi Kleen 1246148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c) 1247f7627e25SThomas Gleixner { 124802dde8b4SJan Beulich const char *vendor = NULL; 1249f7627e25SThomas Gleixner 12500f3fa48aSIngo Molnar if (c->x86_vendor < X86_VENDOR_NUM) { 1251f7627e25SThomas Gleixner vendor = this_cpu->c_vendor; 12520f3fa48aSIngo Molnar } else { 12530f3fa48aSIngo Molnar if (c->cpuid_level >= 0) 1254f7627e25SThomas Gleixner vendor = c->x86_vendor_id; 12550f3fa48aSIngo Molnar } 1256f7627e25SThomas Gleixner 1257bd32a8cfSYinghai Lu if (vendor && !strstr(c->x86_model_id, vendor)) 12581b74dde7SChen Yucong pr_cont("%s ", vendor); 1259f7627e25SThomas Gleixner 12609d31d35bSYinghai Lu if (c->x86_model_id[0]) 12611b74dde7SChen Yucong pr_cont("%s", c->x86_model_id); 1262f7627e25SThomas Gleixner else 12631b74dde7SChen Yucong pr_cont("%d86", c->x86); 1264f7627e25SThomas Gleixner 12651b74dde7SChen Yucong pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model); 1266924e101aSBorislav Petkov 1267f7627e25SThomas Gleixner if (c->x86_mask || c->cpuid_level >= 0) 12681b74dde7SChen Yucong pr_cont(", stepping: 0x%x)\n", c->x86_mask); 1269f7627e25SThomas Gleixner else 12701b74dde7SChen Yucong pr_cont(")\n"); 1271a0854a46SYinghai Lu 12720b8b8078SYinghai Lu print_cpu_msr(c); 127321c3fcf3SYinghai Lu } 127421c3fcf3SYinghai Lu 1275148f9bb8SPaul Gortmaker void print_cpu_msr(struct cpuinfo_x86 *c) 127621c3fcf3SYinghai Lu { 1277a0854a46SYinghai Lu if (c->cpu_index < show_msr) 127821c3fcf3SYinghai Lu __print_cpu_msr(); 1279f7627e25SThomas Gleixner } 1280f7627e25SThomas Gleixner 1281ac72e788SAndi Kleen static __init int setup_disablecpuid(char *arg) 1282ac72e788SAndi Kleen { 1283ac72e788SAndi Kleen int bit; 12840f3fa48aSIngo Molnar 1285ac72e788SAndi Kleen if (get_option(&arg, &bit) && bit < NCAPINTS*32) 1286ac72e788SAndi Kleen setup_clear_cpu_cap(bit); 1287ac72e788SAndi Kleen else 1288ac72e788SAndi Kleen return 0; 12890f3fa48aSIngo Molnar 1290ac72e788SAndi Kleen return 1; 1291ac72e788SAndi Kleen } 1292ac72e788SAndi Kleen __setup("clearcpuid=", setup_disablecpuid); 1293ac72e788SAndi Kleen 1294d5494d4fSYinghai Lu #ifdef CONFIG_X86_64 1295404f6aacSKees Cook struct desc_ptr idt_descr __ro_after_init = { 1296404f6aacSKees Cook .size = NR_VECTORS * 16 - 1, 1297404f6aacSKees Cook .address = (unsigned long) idt_table, 1298404f6aacSKees Cook }; 1299404f6aacSKees Cook const struct desc_ptr debug_idt_descr = { 1300404f6aacSKees Cook .size = NR_VECTORS * 16 - 1, 1301404f6aacSKees Cook .address = (unsigned long) debug_idt_table, 1302404f6aacSKees Cook }; 1303d5494d4fSYinghai Lu 1304947e76cdSBrian Gerst DEFINE_PER_CPU_FIRST(union irq_stack_union, 1305277d5b40SAndi Kleen irq_stack_union) __aligned(PAGE_SIZE) __visible; 13060f3fa48aSIngo Molnar 1307bdf977b3STejun Heo /* 1308a7fcf28dSAndy Lutomirski * The following percpu variables are hot. Align current_task to 1309a7fcf28dSAndy Lutomirski * cacheline size such that they fall in the same cacheline. 1310bdf977b3STejun Heo */ 1311bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned = 1312bdf977b3STejun Heo &init_task; 1313bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task); 1314d5494d4fSYinghai Lu 1315bdf977b3STejun Heo DEFINE_PER_CPU(char *, irq_stack_ptr) = 13164950d6d4SJosh Poimboeuf init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE; 1317bdf977b3STejun Heo 1318277d5b40SAndi Kleen DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1; 1319d5494d4fSYinghai Lu 1320c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT; 1321c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count); 1322c2daa3beSPeter Zijlstra 13230f3fa48aSIngo Molnar /* 13240f3fa48aSIngo Molnar * Special IST stacks which the CPU switches to when it calls 13250f3fa48aSIngo Molnar * an IST-marked descriptor entry. Up to 7 stacks (hardware 13260f3fa48aSIngo Molnar * limit), all of them are 4K, except the debug stack which 13270f3fa48aSIngo Molnar * is 8K. 13280f3fa48aSIngo Molnar */ 13290f3fa48aSIngo Molnar static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = { 13300f3fa48aSIngo Molnar [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ, 13310f3fa48aSIngo Molnar [DEBUG_STACK - 1] = DEBUG_STKSZ 13320f3fa48aSIngo Molnar }; 13330f3fa48aSIngo Molnar 133492d65b23SBrian Gerst static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks 13353e352aa8STejun Heo [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]); 1336d5494d4fSYinghai Lu 1337d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */ 1338d5494d4fSYinghai Lu void syscall_init(void) 1339d5494d4fSYinghai Lu { 134031ac34caSBorislav Petkov wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS); 134147edb651SAndy Lutomirski wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64); 1342d56fe4bfSIngo Molnar 1343d56fe4bfSIngo Molnar #ifdef CONFIG_IA32_EMULATION 134447edb651SAndy Lutomirski wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat); 1345a76c7f46SDenys Vlasenko /* 1346487d1edbSDenys Vlasenko * This only works on Intel CPUs. 1347487d1edbSDenys Vlasenko * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP. 1348487d1edbSDenys Vlasenko * This does not cause SYSENTER to jump to the wrong location, because 1349487d1edbSDenys Vlasenko * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit). 1350a76c7f46SDenys Vlasenko */ 1351a76c7f46SDenys Vlasenko wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS); 1352a76c7f46SDenys Vlasenko wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL); 13534c8cd0c5SIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat); 1354d56fe4bfSIngo Molnar #else 135547edb651SAndy Lutomirski wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret); 13566b51311cSBorislav Petkov wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG); 1357d56fe4bfSIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL); 1358d56fe4bfSIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL); 1359d5494d4fSYinghai Lu #endif 1360d5494d4fSYinghai Lu 1361d5494d4fSYinghai Lu /* Flags to clear on syscall */ 1362d5494d4fSYinghai Lu wrmsrl(MSR_SYSCALL_MASK, 136363bcff2aSH. Peter Anvin X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF| 13648c7aa698SAndy Lutomirski X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT); 1365d5494d4fSYinghai Lu } 1366d5494d4fSYinghai Lu 1367d5494d4fSYinghai Lu /* 1368d5494d4fSYinghai Lu * Copies of the original ist values from the tss are only accessed during 1369d5494d4fSYinghai Lu * debugging, no special alignment required. 1370d5494d4fSYinghai Lu */ 1371d5494d4fSYinghai Lu DEFINE_PER_CPU(struct orig_ist, orig_ist); 1372d5494d4fSYinghai Lu 1373228bdaa9SSteven Rostedt static DEFINE_PER_CPU(unsigned long, debug_stack_addr); 137442181186SSteven Rostedt DEFINE_PER_CPU(int, debug_stack_usage); 1375228bdaa9SSteven Rostedt 1376228bdaa9SSteven Rostedt int is_debug_stack(unsigned long addr) 1377228bdaa9SSteven Rostedt { 137889cbc767SChristoph Lameter return __this_cpu_read(debug_stack_usage) || 137989cbc767SChristoph Lameter (addr <= __this_cpu_read(debug_stack_addr) && 138089cbc767SChristoph Lameter addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ)); 1381228bdaa9SSteven Rostedt } 13820f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(is_debug_stack); 1383228bdaa9SSteven Rostedt 1384629f4f9dSSeiji Aguchi DEFINE_PER_CPU(u32, debug_idt_ctr); 1385f8988175SSteven Rostedt 1386228bdaa9SSteven Rostedt void debug_stack_set_zero(void) 1387228bdaa9SSteven Rostedt { 1388629f4f9dSSeiji Aguchi this_cpu_inc(debug_idt_ctr); 1389629f4f9dSSeiji Aguchi load_current_idt(); 1390228bdaa9SSteven Rostedt } 13910f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_set_zero); 1392228bdaa9SSteven Rostedt 1393228bdaa9SSteven Rostedt void debug_stack_reset(void) 1394228bdaa9SSteven Rostedt { 1395629f4f9dSSeiji Aguchi if (WARN_ON(!this_cpu_read(debug_idt_ctr))) 1396f8988175SSteven Rostedt return; 1397629f4f9dSSeiji Aguchi if (this_cpu_dec_return(debug_idt_ctr) == 0) 1398629f4f9dSSeiji Aguchi load_current_idt(); 1399228bdaa9SSteven Rostedt } 14000f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_reset); 1401228bdaa9SSteven Rostedt 14020f3fa48aSIngo Molnar #else /* CONFIG_X86_64 */ 1403d5494d4fSYinghai Lu 1404bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task; 1405bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task); 1406c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT; 1407c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count); 1408bdf977b3STejun Heo 1409a7fcf28dSAndy Lutomirski /* 1410a7fcf28dSAndy Lutomirski * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find 1411a7fcf28dSAndy Lutomirski * the top of the kernel stack. Use an extra percpu variable to track the 1412a7fcf28dSAndy Lutomirski * top of the kernel stack directly. 1413a7fcf28dSAndy Lutomirski */ 1414a7fcf28dSAndy Lutomirski DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) = 1415a7fcf28dSAndy Lutomirski (unsigned long)&init_thread_union + THREAD_SIZE; 1416a7fcf28dSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack); 1417a7fcf28dSAndy Lutomirski 141860a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR 141953f82452SJeremy Fitzhardinge DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary); 142060a5317fSTejun Heo #endif 142160a5317fSTejun Heo 14220f3fa48aSIngo Molnar #endif /* CONFIG_X86_64 */ 1423f7627e25SThomas Gleixner 1424f7627e25SThomas Gleixner /* 14259766cdbcSJaswinder Singh Rajput * Clear all 6 debug registers: 14269766cdbcSJaswinder Singh Rajput */ 14279766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void) 14289766cdbcSJaswinder Singh Rajput { 14299766cdbcSJaswinder Singh Rajput int i; 14309766cdbcSJaswinder Singh Rajput 14319766cdbcSJaswinder Singh Rajput for (i = 0; i < 8; i++) { 14329766cdbcSJaswinder Singh Rajput /* Ignore db4, db5 */ 14339766cdbcSJaswinder Singh Rajput if ((i == 4) || (i == 5)) 14349766cdbcSJaswinder Singh Rajput continue; 14359766cdbcSJaswinder Singh Rajput 14369766cdbcSJaswinder Singh Rajput set_debugreg(0, i); 14379766cdbcSJaswinder Singh Rajput } 14389766cdbcSJaswinder Singh Rajput } 1439f7627e25SThomas Gleixner 14400bb9fef9SJason Wessel #ifdef CONFIG_KGDB 14410bb9fef9SJason Wessel /* 14420bb9fef9SJason Wessel * Restore debug regs if using kgdbwait and you have a kernel debugger 14430bb9fef9SJason Wessel * connection established. 14440bb9fef9SJason Wessel */ 14450bb9fef9SJason Wessel static void dbg_restore_debug_regs(void) 14460bb9fef9SJason Wessel { 14470bb9fef9SJason Wessel if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break)) 14480bb9fef9SJason Wessel arch_kgdb_ops.correct_hw_break(); 14490bb9fef9SJason Wessel } 14500bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */ 14510bb9fef9SJason Wessel #define dbg_restore_debug_regs() 14520bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */ 14530bb9fef9SJason Wessel 1454ce4b1b16SIgor Mammedov static void wait_for_master_cpu(int cpu) 1455ce4b1b16SIgor Mammedov { 1456ce4b1b16SIgor Mammedov #ifdef CONFIG_SMP 1457ce4b1b16SIgor Mammedov /* 1458ce4b1b16SIgor Mammedov * wait for ACK from master CPU before continuing 1459ce4b1b16SIgor Mammedov * with AP initialization 1460ce4b1b16SIgor Mammedov */ 1461ce4b1b16SIgor Mammedov WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)); 1462ce4b1b16SIgor Mammedov while (!cpumask_test_cpu(cpu, cpu_callout_mask)) 1463ce4b1b16SIgor Mammedov cpu_relax(); 1464ce4b1b16SIgor Mammedov #endif 1465ce4b1b16SIgor Mammedov } 1466ce4b1b16SIgor Mammedov 1467f7627e25SThomas Gleixner /* 1468f7627e25SThomas Gleixner * cpu_init() initializes state that is per-CPU. Some data is already 1469f7627e25SThomas Gleixner * initialized (naturally) in the bootstrap process, such as the GDT 1470f7627e25SThomas Gleixner * and IDT. We reload them nevertheless, this function acts as a 1471f7627e25SThomas Gleixner * 'CPU state barrier', nothing should get across. 14721ba76586SYinghai Lu * A lot of state is already set up in PDA init for 64 bit 1473f7627e25SThomas Gleixner */ 14741ba76586SYinghai Lu #ifdef CONFIG_X86_64 14750f3fa48aSIngo Molnar 1476148f9bb8SPaul Gortmaker void cpu_init(void) 14771ba76586SYinghai Lu { 14780fe1e009STejun Heo struct orig_ist *oist; 14791ba76586SYinghai Lu struct task_struct *me; 14800f3fa48aSIngo Molnar struct tss_struct *t; 14810f3fa48aSIngo Molnar unsigned long v; 1482fb59831bSAndy Lutomirski int cpu = raw_smp_processor_id(); 14831ba76586SYinghai Lu int i; 14841ba76586SYinghai Lu 1485ce4b1b16SIgor Mammedov wait_for_master_cpu(cpu); 1486ce4b1b16SIgor Mammedov 1487e6ebf5deSFenghua Yu /* 14881e02ce4cSAndy Lutomirski * Initialize the CR4 shadow before doing anything that could 14891e02ce4cSAndy Lutomirski * try to read it. 14901e02ce4cSAndy Lutomirski */ 14911e02ce4cSAndy Lutomirski cr4_init_shadow(); 14921e02ce4cSAndy Lutomirski 14931e02ce4cSAndy Lutomirski /* 1494e6ebf5deSFenghua Yu * Load microcode on this cpu if a valid microcode is available. 1495e6ebf5deSFenghua Yu * This is early microcode loading procedure. 1496e6ebf5deSFenghua Yu */ 1497e6ebf5deSFenghua Yu load_ucode_ap(); 1498e6ebf5deSFenghua Yu 149924933b82SAndy Lutomirski t = &per_cpu(cpu_tss, cpu); 15000fe1e009STejun Heo oist = &per_cpu(orig_ist, cpu); 15010f3fa48aSIngo Molnar 1502e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA 150327fd185fSFenghua Yu if (this_cpu_read(numa_node) == 0 && 1504e534c7c5SLee Schermerhorn early_cpu_to_node(cpu) != NUMA_NO_NODE) 1505e534c7c5SLee Schermerhorn set_numa_node(early_cpu_to_node(cpu)); 1506e7a22c1eSBrian Gerst #endif 15071ba76586SYinghai Lu 15081ba76586SYinghai Lu me = current; 15091ba76586SYinghai Lu 15102eaad1fdSMike Travis pr_debug("Initializing CPU#%d\n", cpu); 15111ba76586SYinghai Lu 1512375074ccSAndy Lutomirski cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); 15131ba76586SYinghai Lu 15141ba76586SYinghai Lu /* 15151ba76586SYinghai Lu * Initialize the per-CPU GDT with the boot GDT, 15161ba76586SYinghai Lu * and set up the GDT descriptor: 15171ba76586SYinghai Lu */ 15181ba76586SYinghai Lu 1519552be871SBrian Gerst switch_to_new_gdt(cpu); 15202697fbd5SBrian Gerst loadsegment(fs, 0); 15212697fbd5SBrian Gerst 1522cf910e83SSeiji Aguchi load_current_idt(); 15231ba76586SYinghai Lu 15241ba76586SYinghai Lu memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8); 15251ba76586SYinghai Lu syscall_init(); 15261ba76586SYinghai Lu 15271ba76586SYinghai Lu wrmsrl(MSR_FS_BASE, 0); 15281ba76586SYinghai Lu wrmsrl(MSR_KERNEL_GS_BASE, 0); 15291ba76586SYinghai Lu barrier(); 15301ba76586SYinghai Lu 15314763ed4dSH. Peter Anvin x86_configure_nx(); 1532659006bfSThomas Gleixner x2apic_setup(); 15331ba76586SYinghai Lu 15341ba76586SYinghai Lu /* 15351ba76586SYinghai Lu * set up and load the per-CPU TSS 15361ba76586SYinghai Lu */ 15370fe1e009STejun Heo if (!oist->ist[0]) { 153892d65b23SBrian Gerst char *estacks = per_cpu(exception_stacks, cpu); 15390f3fa48aSIngo Molnar 15401ba76586SYinghai Lu for (v = 0; v < N_EXCEPTION_STACKS; v++) { 15410f3fa48aSIngo Molnar estacks += exception_stack_sizes[v]; 15420fe1e009STejun Heo oist->ist[v] = t->x86_tss.ist[v] = 15431ba76586SYinghai Lu (unsigned long)estacks; 1544228bdaa9SSteven Rostedt if (v == DEBUG_STACK-1) 1545228bdaa9SSteven Rostedt per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks; 15461ba76586SYinghai Lu } 15471ba76586SYinghai Lu } 15481ba76586SYinghai Lu 15491ba76586SYinghai Lu t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap); 15500f3fa48aSIngo Molnar 15511ba76586SYinghai Lu /* 15521ba76586SYinghai Lu * <= is required because the CPU will access up to 15531ba76586SYinghai Lu * 8 bits beyond the end of the IO permission bitmap. 15541ba76586SYinghai Lu */ 15551ba76586SYinghai Lu for (i = 0; i <= IO_BITMAP_LONGS; i++) 15561ba76586SYinghai Lu t->io_bitmap[i] = ~0UL; 15571ba76586SYinghai Lu 15581ba76586SYinghai Lu atomic_inc(&init_mm.mm_count); 15591ba76586SYinghai Lu me->active_mm = &init_mm; 15608c5dfd25SStoyan Gaydarov BUG_ON(me->mm); 15611ba76586SYinghai Lu enter_lazy_tlb(&init_mm, me); 15621ba76586SYinghai Lu 15631ba76586SYinghai Lu load_sp0(t, ¤t->thread); 15641ba76586SYinghai Lu set_tss_desc(cpu, t); 15651ba76586SYinghai Lu load_TR_desc(); 156637868fe1SAndy Lutomirski load_mm_ldt(&init_mm); 15671ba76586SYinghai Lu 15689766cdbcSJaswinder Singh Rajput clear_all_debug_regs(); 15690bb9fef9SJason Wessel dbg_restore_debug_regs(); 15701ba76586SYinghai Lu 157121c4cd10SIngo Molnar fpu__init_cpu(); 15721ba76586SYinghai Lu 15731ba76586SYinghai Lu if (is_uv_system()) 15741ba76586SYinghai Lu uv_cpu_init(); 15751ba76586SYinghai Lu } 15761ba76586SYinghai Lu 15771ba76586SYinghai Lu #else 15781ba76586SYinghai Lu 1579148f9bb8SPaul Gortmaker void cpu_init(void) 1580f7627e25SThomas Gleixner { 1581f7627e25SThomas Gleixner int cpu = smp_processor_id(); 1582f7627e25SThomas Gleixner struct task_struct *curr = current; 158324933b82SAndy Lutomirski struct tss_struct *t = &per_cpu(cpu_tss, cpu); 1584f7627e25SThomas Gleixner struct thread_struct *thread = &curr->thread; 1585f7627e25SThomas Gleixner 1586ce4b1b16SIgor Mammedov wait_for_master_cpu(cpu); 1587e6ebf5deSFenghua Yu 15885b2bdbc8SSteven Rostedt /* 15895b2bdbc8SSteven Rostedt * Initialize the CR4 shadow before doing anything that could 15905b2bdbc8SSteven Rostedt * try to read it. 15915b2bdbc8SSteven Rostedt */ 15925b2bdbc8SSteven Rostedt cr4_init_shadow(); 15935b2bdbc8SSteven Rostedt 1594ce4b1b16SIgor Mammedov show_ucode_info_early(); 1595f7627e25SThomas Gleixner 15961b74dde7SChen Yucong pr_info("Initializing CPU#%d\n", cpu); 1597f7627e25SThomas Gleixner 1598362f924bSBorislav Petkov if (cpu_feature_enabled(X86_FEATURE_VME) || 159959e21e3dSBorislav Petkov boot_cpu_has(X86_FEATURE_TSC) || 1600362f924bSBorislav Petkov boot_cpu_has(X86_FEATURE_DE)) 1601375074ccSAndy Lutomirski cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); 1602f7627e25SThomas Gleixner 1603cf910e83SSeiji Aguchi load_current_idt(); 1604552be871SBrian Gerst switch_to_new_gdt(cpu); 1605f7627e25SThomas Gleixner 1606f7627e25SThomas Gleixner /* 1607f7627e25SThomas Gleixner * Set up and load the per-CPU TSS and LDT 1608f7627e25SThomas Gleixner */ 1609f7627e25SThomas Gleixner atomic_inc(&init_mm.mm_count); 1610f7627e25SThomas Gleixner curr->active_mm = &init_mm; 16118c5dfd25SStoyan Gaydarov BUG_ON(curr->mm); 1612f7627e25SThomas Gleixner enter_lazy_tlb(&init_mm, curr); 1613f7627e25SThomas Gleixner 1614faca6227SH. Peter Anvin load_sp0(t, thread); 1615f7627e25SThomas Gleixner set_tss_desc(cpu, t); 1616f7627e25SThomas Gleixner load_TR_desc(); 161737868fe1SAndy Lutomirski load_mm_ldt(&init_mm); 1618f7627e25SThomas Gleixner 1619f9a196b8SThomas Gleixner t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap); 1620f9a196b8SThomas Gleixner 1621f7627e25SThomas Gleixner #ifdef CONFIG_DOUBLEFAULT 1622f7627e25SThomas Gleixner /* Set up doublefault TSS pointer in the GDT */ 1623f7627e25SThomas Gleixner __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss); 1624f7627e25SThomas Gleixner #endif 1625f7627e25SThomas Gleixner 16269766cdbcSJaswinder Singh Rajput clear_all_debug_regs(); 16270bb9fef9SJason Wessel dbg_restore_debug_regs(); 1628f7627e25SThomas Gleixner 162921c4cd10SIngo Molnar fpu__init_cpu(); 1630f7627e25SThomas Gleixner } 16311ba76586SYinghai Lu #endif 16325700f743SBorislav Petkov 1633b51ef52dSLaura Abbott static void bsp_resume(void) 1634b51ef52dSLaura Abbott { 1635b51ef52dSLaura Abbott if (this_cpu->c_bsp_resume) 1636b51ef52dSLaura Abbott this_cpu->c_bsp_resume(&boot_cpu_data); 1637b51ef52dSLaura Abbott } 1638b51ef52dSLaura Abbott 1639b51ef52dSLaura Abbott static struct syscore_ops cpu_syscore_ops = { 1640b51ef52dSLaura Abbott .resume = bsp_resume, 1641b51ef52dSLaura Abbott }; 1642b51ef52dSLaura Abbott 1643b51ef52dSLaura Abbott static int __init init_cpu_syscore(void) 1644b51ef52dSLaura Abbott { 1645b51ef52dSLaura Abbott register_syscore_ops(&cpu_syscore_ops); 1646b51ef52dSLaura Abbott return 0; 1647b51ef52dSLaura Abbott } 1648b51ef52dSLaura Abbott core_initcall(init_cpu_syscore); 1649