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> 5f0fc4affSYinghai Lu #include <linux/module.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> 409766cdbcSJaswinder Singh Rajput #include <asm/cpu.h> 419766cdbcSJaswinder Singh Rajput #include <asm/mce.h> 429766cdbcSJaswinder Singh Rajput #include <asm/msr.h> 439766cdbcSJaswinder Singh Rajput #include <asm/pat.h> 44d288e1cfSFenghua Yu #include <asm/microcode.h> 45d288e1cfSFenghua Yu #include <asm/microcode_intel.h> 46e641f5f5SIngo Molnar 47f7627e25SThomas Gleixner #ifdef CONFIG_X86_LOCAL_APIC 48bdbcdd48STejun Heo #include <asm/uv/uv.h> 49f7627e25SThomas Gleixner #endif 50f7627e25SThomas Gleixner 51f7627e25SThomas Gleixner #include "cpu.h" 52f7627e25SThomas Gleixner 53c2d1cec1SMike Travis /* all of these masks are initialized in setup_cpu_local_masks() */ 54c2d1cec1SMike Travis cpumask_var_t cpu_initialized_mask; 559766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callout_mask; 569766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callin_mask; 57c2d1cec1SMike Travis 58c2d1cec1SMike Travis /* representing cpus for which sibling maps can be computed */ 59c2d1cec1SMike Travis cpumask_var_t cpu_sibling_setup_mask; 60c2d1cec1SMike Travis 612f2f52baSBrian Gerst /* correctly size the local cpu masks */ 624369f1fbSIngo Molnar void __init setup_cpu_local_masks(void) 632f2f52baSBrian Gerst { 642f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_initialized_mask); 652f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_callin_mask); 662f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_callout_mask); 672f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask); 682f2f52baSBrian Gerst } 692f2f52baSBrian Gerst 70148f9bb8SPaul Gortmaker static void default_init(struct cpuinfo_x86 *c) 71e8055139SOndrej Zary { 72e8055139SOndrej Zary #ifdef CONFIG_X86_64 7327c13eceSBorislav Petkov cpu_detect_cache_sizes(c); 74e8055139SOndrej Zary #else 75e8055139SOndrej Zary /* Not much we can do here... */ 76e8055139SOndrej Zary /* Check if at least it has cpuid */ 77e8055139SOndrej Zary if (c->cpuid_level == -1) { 78e8055139SOndrej Zary /* No cpuid. It must be an ancient CPU */ 79e8055139SOndrej Zary if (c->x86 == 4) 80e8055139SOndrej Zary strcpy(c->x86_model_id, "486"); 81e8055139SOndrej Zary else if (c->x86 == 3) 82e8055139SOndrej Zary strcpy(c->x86_model_id, "386"); 83e8055139SOndrej Zary } 84e8055139SOndrej Zary #endif 85e8055139SOndrej Zary } 86e8055139SOndrej Zary 87148f9bb8SPaul Gortmaker static const struct cpu_dev default_cpu = { 88e8055139SOndrej Zary .c_init = default_init, 89e8055139SOndrej Zary .c_vendor = "Unknown", 90e8055139SOndrej Zary .c_x86_vendor = X86_VENDOR_UNKNOWN, 91e8055139SOndrej Zary }; 92e8055139SOndrej Zary 93148f9bb8SPaul Gortmaker static const struct cpu_dev *this_cpu = &default_cpu; 940a488a53SYinghai Lu 9506deef89SBrian Gerst DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = { 96950ad7ffSYinghai Lu #ifdef CONFIG_X86_64 9706deef89SBrian Gerst /* 9806deef89SBrian Gerst * We need valid kernel segments for data and code in long mode too 99950ad7ffSYinghai Lu * IRET will check the segment types kkeil 2000/10/28 100950ad7ffSYinghai Lu * Also sysret mandates a special GDT layout 10106deef89SBrian Gerst * 1029766cdbcSJaswinder Singh Rajput * TLS descriptors are currently at a different place compared to i386. 10306deef89SBrian Gerst * Hopefully nobody expects them at a fixed place (Wine?) 104950ad7ffSYinghai Lu */ 1051e5de182SAkinobu Mita [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff), 1061e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff), 1071e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff), 1081e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff), 1091e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff), 1101e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff), 111950ad7ffSYinghai Lu #else 1121e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff), 1131e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 1141e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff), 1151e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff), 116f7627e25SThomas Gleixner /* 117f7627e25SThomas Gleixner * Segments used for calling PnP BIOS have byte granularity. 118f7627e25SThomas Gleixner * They code segments and data segments have fixed 64k limits, 119f7627e25SThomas Gleixner * the transfer segment sizes are set at run time. 120f7627e25SThomas Gleixner */ 1216842ef0eSGlauber de Oliveira Costa /* 32-bit code */ 1221e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), 1236842ef0eSGlauber de Oliveira Costa /* 16-bit code */ 1241e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), 1256842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 1261e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff), 1276842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 1281e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0), 1296842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 1301e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0), 131f7627e25SThomas Gleixner /* 132f7627e25SThomas Gleixner * The APM segments have byte granularity and their bases 133f7627e25SThomas Gleixner * are set at run time. All have 64k limits. 134f7627e25SThomas Gleixner */ 1356842ef0eSGlauber de Oliveira Costa /* 32-bit code */ 1361e5de182SAkinobu Mita [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), 137f7627e25SThomas Gleixner /* 16-bit code */ 1381e5de182SAkinobu Mita [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), 1396842ef0eSGlauber de Oliveira Costa /* data */ 14072c4d853SIngo Molnar [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff), 141f7627e25SThomas Gleixner 1421e5de182SAkinobu Mita [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 1431e5de182SAkinobu Mita [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 14460a5317fSTejun Heo GDT_STACK_CANARY_INIT 145950ad7ffSYinghai Lu #endif 14606deef89SBrian Gerst } }; 147f7627e25SThomas Gleixner EXPORT_PER_CPU_SYMBOL_GPL(gdt_page); 148f7627e25SThomas Gleixner 1498c3641e9SDave Hansen static int __init x86_mpx_setup(char *s) 1500c752a93SSuresh Siddha { 1518c3641e9SDave Hansen /* require an exact match without trailing characters */ 1522cd3949fSDave Hansen if (strlen(s)) 1532cd3949fSDave Hansen return 0; 1540c752a93SSuresh Siddha 1558c3641e9SDave Hansen /* do not emit a message if the feature is not present */ 1568c3641e9SDave Hansen if (!boot_cpu_has(X86_FEATURE_MPX)) 1576bad06b7SSuresh Siddha return 1; 1586bad06b7SSuresh Siddha 1598c3641e9SDave Hansen setup_clear_cpu_cap(X86_FEATURE_MPX); 1608c3641e9SDave Hansen pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n"); 161b6f42a4aSFenghua Yu return 1; 162b6f42a4aSFenghua Yu } 1638c3641e9SDave Hansen __setup("nompx", x86_mpx_setup); 164b6f42a4aSFenghua Yu 165d12a72b8SAndy Lutomirski static int __init x86_noinvpcid_setup(char *s) 166d12a72b8SAndy Lutomirski { 167d12a72b8SAndy Lutomirski /* noinvpcid doesn't accept parameters */ 168d12a72b8SAndy Lutomirski if (s) 169d12a72b8SAndy Lutomirski return -EINVAL; 170d12a72b8SAndy Lutomirski 171d12a72b8SAndy Lutomirski /* do not emit a message if the feature is not present */ 172d12a72b8SAndy Lutomirski if (!boot_cpu_has(X86_FEATURE_INVPCID)) 173d12a72b8SAndy Lutomirski return 0; 174d12a72b8SAndy Lutomirski 175d12a72b8SAndy Lutomirski setup_clear_cpu_cap(X86_FEATURE_INVPCID); 176d12a72b8SAndy Lutomirski pr_info("noinvpcid: INVPCID feature disabled\n"); 177d12a72b8SAndy Lutomirski return 0; 178d12a72b8SAndy Lutomirski } 179d12a72b8SAndy Lutomirski early_param("noinvpcid", x86_noinvpcid_setup); 180d12a72b8SAndy Lutomirski 181ba51dcedSYinghai Lu #ifdef CONFIG_X86_32 182148f9bb8SPaul Gortmaker static int cachesize_override = -1; 183148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1; 184f7627e25SThomas Gleixner 185f7627e25SThomas Gleixner static int __init cachesize_setup(char *str) 186f7627e25SThomas Gleixner { 187f7627e25SThomas Gleixner get_option(&str, &cachesize_override); 188f7627e25SThomas Gleixner return 1; 189f7627e25SThomas Gleixner } 190f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup); 191f7627e25SThomas Gleixner 192f7627e25SThomas Gleixner static int __init x86_sep_setup(char *s) 193f7627e25SThomas Gleixner { 19413530257SAndi Kleen setup_clear_cpu_cap(X86_FEATURE_SEP); 195f7627e25SThomas Gleixner return 1; 196f7627e25SThomas Gleixner } 197f7627e25SThomas Gleixner __setup("nosep", x86_sep_setup); 198f7627e25SThomas Gleixner 199f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */ 200f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag) 201f7627e25SThomas Gleixner { 202f7627e25SThomas Gleixner u32 f1, f2; 203f7627e25SThomas Gleixner 20494f6bac1SKrzysztof Helt /* 20594f6bac1SKrzysztof Helt * Cyrix and IDT cpus allow disabling of CPUID 20694f6bac1SKrzysztof Helt * so the code below may return different results 20794f6bac1SKrzysztof Helt * when it is executed before and after enabling 20894f6bac1SKrzysztof Helt * the CPUID. Add "volatile" to not allow gcc to 20994f6bac1SKrzysztof Helt * optimize the subsequent calls to this function. 21094f6bac1SKrzysztof Helt */ 21194f6bac1SKrzysztof Helt asm volatile ("pushfl \n\t" 212f7627e25SThomas Gleixner "pushfl \n\t" 213f7627e25SThomas Gleixner "popl %0 \n\t" 214f7627e25SThomas Gleixner "movl %0, %1 \n\t" 215f7627e25SThomas Gleixner "xorl %2, %0 \n\t" 216f7627e25SThomas Gleixner "pushl %0 \n\t" 217f7627e25SThomas Gleixner "popfl \n\t" 218f7627e25SThomas Gleixner "pushfl \n\t" 219f7627e25SThomas Gleixner "popl %0 \n\t" 220f7627e25SThomas Gleixner "popfl \n\t" 2210f3fa48aSIngo Molnar 222f7627e25SThomas Gleixner : "=&r" (f1), "=&r" (f2) 223f7627e25SThomas Gleixner : "ir" (flag)); 224f7627e25SThomas Gleixner 225f7627e25SThomas Gleixner return ((f1^f2) & flag) != 0; 226f7627e25SThomas Gleixner } 227f7627e25SThomas Gleixner 228f7627e25SThomas Gleixner /* Probe for the CPUID instruction */ 229148f9bb8SPaul Gortmaker int have_cpuid_p(void) 230f7627e25SThomas Gleixner { 231f7627e25SThomas Gleixner return flag_is_changeable_p(X86_EFLAGS_ID); 232f7627e25SThomas Gleixner } 233f7627e25SThomas Gleixner 234148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) 2350a488a53SYinghai Lu { 2360a488a53SYinghai Lu unsigned long lo, hi; 2370f3fa48aSIngo Molnar 2380f3fa48aSIngo Molnar if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr) 2390f3fa48aSIngo Molnar return; 2400f3fa48aSIngo Molnar 2410f3fa48aSIngo Molnar /* Disable processor serial number: */ 2420f3fa48aSIngo Molnar 2430a488a53SYinghai Lu rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi); 2440a488a53SYinghai Lu lo |= 0x200000; 2450a488a53SYinghai Lu wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi); 2460f3fa48aSIngo Molnar 2471b74dde7SChen Yucong pr_notice("CPU serial number disabled.\n"); 2480a488a53SYinghai Lu clear_cpu_cap(c, X86_FEATURE_PN); 2490a488a53SYinghai Lu 2500a488a53SYinghai Lu /* Disabling the serial number may affect the cpuid level */ 2510a488a53SYinghai Lu c->cpuid_level = cpuid_eax(0); 2520a488a53SYinghai Lu } 2530a488a53SYinghai Lu 2540a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s) 2550a488a53SYinghai Lu { 2560a488a53SYinghai Lu disable_x86_serial_nr = 0; 2570a488a53SYinghai Lu return 1; 2580a488a53SYinghai Lu } 2590a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup); 260ba51dcedSYinghai Lu #else 261102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag) 262102bbe3aSYinghai Lu { 263102bbe3aSYinghai Lu return 1; 264102bbe3aSYinghai Lu } 265102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) 266102bbe3aSYinghai Lu { 267102bbe3aSYinghai Lu } 268ba51dcedSYinghai Lu #endif 2690a488a53SYinghai Lu 270de5397adSFenghua Yu static __init int setup_disable_smep(char *arg) 271de5397adSFenghua Yu { 272b2cc2a07SH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_SMEP); 273de5397adSFenghua Yu return 1; 274de5397adSFenghua Yu } 275de5397adSFenghua Yu __setup("nosmep", setup_disable_smep); 276de5397adSFenghua Yu 277b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c) 278de5397adSFenghua Yu { 279b2cc2a07SH. Peter Anvin if (cpu_has(c, X86_FEATURE_SMEP)) 280375074ccSAndy Lutomirski cr4_set_bits(X86_CR4_SMEP); 281de5397adSFenghua Yu } 282de5397adSFenghua Yu 28352b6179aSH. Peter Anvin static __init int setup_disable_smap(char *arg) 28452b6179aSH. Peter Anvin { 285b2cc2a07SH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_SMAP); 28652b6179aSH. Peter Anvin return 1; 28752b6179aSH. Peter Anvin } 28852b6179aSH. Peter Anvin __setup("nosmap", setup_disable_smap); 28952b6179aSH. Peter Anvin 290b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c) 29152b6179aSH. Peter Anvin { 292581b7f15SAndrew Cooper unsigned long eflags = native_save_fl(); 293b2cc2a07SH. Peter Anvin 294b2cc2a07SH. Peter Anvin /* This should have been cleared long ago */ 295b2cc2a07SH. Peter Anvin BUG_ON(eflags & X86_EFLAGS_AC); 296b2cc2a07SH. Peter Anvin 29703bbd596SH. Peter Anvin if (cpu_has(c, X86_FEATURE_SMAP)) { 29803bbd596SH. Peter Anvin #ifdef CONFIG_X86_SMAP 299375074ccSAndy Lutomirski cr4_set_bits(X86_CR4_SMAP); 30003bbd596SH. Peter Anvin #else 301375074ccSAndy Lutomirski cr4_clear_bits(X86_CR4_SMAP); 30203bbd596SH. Peter Anvin #endif 30303bbd596SH. Peter Anvin } 304f7627e25SThomas Gleixner } 305f7627e25SThomas Gleixner 306f7627e25SThomas Gleixner /* 30706976945SDave Hansen * Protection Keys are not available in 32-bit mode. 30806976945SDave Hansen */ 30906976945SDave Hansen static bool pku_disabled; 31006976945SDave Hansen 31106976945SDave Hansen static __always_inline void setup_pku(struct cpuinfo_x86 *c) 31206976945SDave Hansen { 31306976945SDave Hansen if (!cpu_has(c, X86_FEATURE_PKU)) 31406976945SDave Hansen return; 31506976945SDave Hansen if (pku_disabled) 31606976945SDave Hansen return; 31706976945SDave Hansen 31806976945SDave Hansen cr4_set_bits(X86_CR4_PKE); 31906976945SDave Hansen /* 32006976945SDave Hansen * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE 32106976945SDave Hansen * cpuid bit to be set. We need to ensure that we 32206976945SDave Hansen * update that bit in this CPU's "cpu_info". 32306976945SDave Hansen */ 32406976945SDave Hansen get_cpu_cap(c); 32506976945SDave Hansen } 32606976945SDave Hansen 32706976945SDave Hansen #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS 32806976945SDave Hansen static __init int setup_disable_pku(char *arg) 32906976945SDave Hansen { 33006976945SDave Hansen /* 33106976945SDave Hansen * Do not clear the X86_FEATURE_PKU bit. All of the 33206976945SDave Hansen * runtime checks are against OSPKE so clearing the 33306976945SDave Hansen * bit does nothing. 33406976945SDave Hansen * 33506976945SDave Hansen * This way, we will see "pku" in cpuinfo, but not 33606976945SDave Hansen * "ospke", which is exactly what we want. It shows 33706976945SDave Hansen * that the CPU has PKU, but the OS has not enabled it. 33806976945SDave Hansen * This happens to be exactly how a system would look 33906976945SDave Hansen * if we disabled the config option. 34006976945SDave Hansen */ 34106976945SDave Hansen pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n"); 34206976945SDave Hansen pku_disabled = true; 34306976945SDave Hansen return 1; 34406976945SDave Hansen } 34506976945SDave Hansen __setup("nopku", setup_disable_pku); 34606976945SDave Hansen #endif /* CONFIG_X86_64 */ 34706976945SDave Hansen 34806976945SDave Hansen /* 349b38b0665SH. Peter Anvin * Some CPU features depend on higher CPUID levels, which may not always 350b38b0665SH. Peter Anvin * be available due to CPUID level capping or broken virtualization 351b38b0665SH. Peter Anvin * software. Add those features to this table to auto-disable them. 352b38b0665SH. Peter Anvin */ 353b38b0665SH. Peter Anvin struct cpuid_dependent_feature { 354b38b0665SH. Peter Anvin u32 feature; 355b38b0665SH. Peter Anvin u32 level; 356b38b0665SH. Peter Anvin }; 3570f3fa48aSIngo Molnar 358148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature 359b38b0665SH. Peter Anvin cpuid_dependent_features[] = { 360b38b0665SH. Peter Anvin { X86_FEATURE_MWAIT, 0x00000005 }, 361b38b0665SH. Peter Anvin { X86_FEATURE_DCA, 0x00000009 }, 362b38b0665SH. Peter Anvin { X86_FEATURE_XSAVE, 0x0000000d }, 363b38b0665SH. Peter Anvin { 0, 0 } 364b38b0665SH. Peter Anvin }; 365b38b0665SH. Peter Anvin 366148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) 367b38b0665SH. Peter Anvin { 368b38b0665SH. Peter Anvin const struct cpuid_dependent_feature *df; 3699766cdbcSJaswinder Singh Rajput 370b38b0665SH. Peter Anvin for (df = cpuid_dependent_features; df->feature; df++) { 3710f3fa48aSIngo Molnar 3720f3fa48aSIngo Molnar if (!cpu_has(c, df->feature)) 3730f3fa48aSIngo Molnar continue; 374b38b0665SH. Peter Anvin /* 375b38b0665SH. Peter Anvin * Note: cpuid_level is set to -1 if unavailable, but 376b38b0665SH. Peter Anvin * extended_extended_level is set to 0 if unavailable 377b38b0665SH. Peter Anvin * and the legitimate extended levels are all negative 378b38b0665SH. Peter Anvin * when signed; hence the weird messing around with 379b38b0665SH. Peter Anvin * signs here... 380b38b0665SH. Peter Anvin */ 3810f3fa48aSIngo Molnar if (!((s32)df->level < 0 ? 382f6db44dfSYinghai Lu (u32)df->level > (u32)c->extended_cpuid_level : 3830f3fa48aSIngo Molnar (s32)df->level > (s32)c->cpuid_level)) 3840f3fa48aSIngo Molnar continue; 3850f3fa48aSIngo Molnar 386b38b0665SH. Peter Anvin clear_cpu_cap(c, df->feature); 3870f3fa48aSIngo Molnar if (!warn) 3880f3fa48aSIngo Molnar continue; 3890f3fa48aSIngo Molnar 3901b74dde7SChen Yucong pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n", 3919def39beSJosh Triplett x86_cap_flag(df->feature), df->level); 392b38b0665SH. Peter Anvin } 393b38b0665SH. Peter Anvin } 394b38b0665SH. Peter Anvin 395b38b0665SH. Peter Anvin /* 396f7627e25SThomas Gleixner * Naming convention should be: <Name> [(<Codename>)] 397f7627e25SThomas Gleixner * This table only is used unless init_<vendor>() below doesn't set it; 3980f3fa48aSIngo Molnar * in particular, if CPUID levels 0x80000002..4 are supported, this 3990f3fa48aSIngo Molnar * isn't used 400f7627e25SThomas Gleixner */ 401f7627e25SThomas Gleixner 402f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */ 403148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c) 404f7627e25SThomas Gleixner { 40509dc68d9SJan Beulich #ifdef CONFIG_X86_32 40609dc68d9SJan Beulich const struct legacy_cpu_model_info *info; 407f7627e25SThomas Gleixner 408f7627e25SThomas Gleixner if (c->x86_model >= 16) 409f7627e25SThomas Gleixner return NULL; /* Range check */ 410f7627e25SThomas Gleixner 411f7627e25SThomas Gleixner if (!this_cpu) 412f7627e25SThomas Gleixner return NULL; 413f7627e25SThomas Gleixner 41409dc68d9SJan Beulich info = this_cpu->legacy_models; 415f7627e25SThomas Gleixner 41609dc68d9SJan Beulich while (info->family) { 417f7627e25SThomas Gleixner if (info->family == c->x86) 418f7627e25SThomas Gleixner return info->model_names[c->x86_model]; 419f7627e25SThomas Gleixner info++; 420f7627e25SThomas Gleixner } 42109dc68d9SJan Beulich #endif 422f7627e25SThomas Gleixner return NULL; /* Not found */ 423f7627e25SThomas Gleixner } 424f7627e25SThomas Gleixner 425148f9bb8SPaul Gortmaker __u32 cpu_caps_cleared[NCAPINTS]; 426148f9bb8SPaul Gortmaker __u32 cpu_caps_set[NCAPINTS]; 427f7627e25SThomas Gleixner 42811e3a840SJeremy Fitzhardinge void load_percpu_segment(int cpu) 4299d31d35bSYinghai Lu { 430fab334c1SYinghai Lu #ifdef CONFIG_X86_32 4312697fbd5SBrian Gerst loadsegment(fs, __KERNEL_PERCPU); 4322697fbd5SBrian Gerst #else 4332697fbd5SBrian Gerst loadsegment(gs, 0); 4342697fbd5SBrian Gerst wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu)); 435fab334c1SYinghai Lu #endif 43660a5317fSTejun Heo load_stack_canary_segment(); 4379d31d35bSYinghai Lu } 4389d31d35bSYinghai Lu 4390f3fa48aSIngo Molnar /* 4400f3fa48aSIngo Molnar * Current gdt points %fs at the "master" per-cpu area: after this, 4410f3fa48aSIngo Molnar * it's on the real one. 4420f3fa48aSIngo Molnar */ 443552be871SBrian Gerst void switch_to_new_gdt(int cpu) 444f7627e25SThomas Gleixner { 445f7627e25SThomas Gleixner struct desc_ptr gdt_descr; 446f7627e25SThomas Gleixner 447f7627e25SThomas Gleixner gdt_descr.address = (long)get_cpu_gdt_table(cpu); 448f7627e25SThomas Gleixner gdt_descr.size = GDT_SIZE - 1; 449f7627e25SThomas Gleixner load_gdt(&gdt_descr); 450f7627e25SThomas Gleixner /* Reload the per-cpu base */ 45111e3a840SJeremy Fitzhardinge 45211e3a840SJeremy Fitzhardinge load_percpu_segment(cpu); 453f7627e25SThomas Gleixner } 454f7627e25SThomas Gleixner 455148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {}; 456f7627e25SThomas Gleixner 457148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c) 458f7627e25SThomas Gleixner { 459f7627e25SThomas Gleixner unsigned int *v; 460ee098e1aSBorislav Petkov char *p, *q, *s; 461f7627e25SThomas Gleixner 4623da99c97SYinghai Lu if (c->extended_cpuid_level < 0x80000004) 4631b05d60dSYinghai Lu return; 464f7627e25SThomas Gleixner 465f7627e25SThomas Gleixner v = (unsigned int *)c->x86_model_id; 466f7627e25SThomas Gleixner cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]); 467f7627e25SThomas Gleixner cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]); 468f7627e25SThomas Gleixner cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]); 469f7627e25SThomas Gleixner c->x86_model_id[48] = 0; 470f7627e25SThomas Gleixner 471ee098e1aSBorislav Petkov /* Trim whitespace */ 472ee098e1aSBorislav Petkov p = q = s = &c->x86_model_id[0]; 473ee098e1aSBorislav Petkov 474ee098e1aSBorislav Petkov while (*p == ' ') 475ee098e1aSBorislav Petkov p++; 476ee098e1aSBorislav Petkov 477ee098e1aSBorislav Petkov while (*p) { 478ee098e1aSBorislav Petkov /* Note the last non-whitespace index */ 479ee098e1aSBorislav Petkov if (!isspace(*p)) 480ee098e1aSBorislav Petkov s = q; 481ee098e1aSBorislav Petkov 482ee098e1aSBorislav Petkov *q++ = *p++; 483ee098e1aSBorislav Petkov } 484ee098e1aSBorislav Petkov 485ee098e1aSBorislav Petkov *(s + 1) = '\0'; 486f7627e25SThomas Gleixner } 487f7627e25SThomas Gleixner 488148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c) 489f7627e25SThomas Gleixner { 4909d31d35bSYinghai Lu unsigned int n, dummy, ebx, ecx, edx, l2size; 491f7627e25SThomas Gleixner 4923da99c97SYinghai Lu n = c->extended_cpuid_level; 493f7627e25SThomas Gleixner 494f7627e25SThomas Gleixner if (n >= 0x80000005) { 4959d31d35bSYinghai Lu cpuid(0x80000005, &dummy, &ebx, &ecx, &edx); 496f7627e25SThomas Gleixner c->x86_cache_size = (ecx>>24) + (edx>>24); 497140fc727SYinghai Lu #ifdef CONFIG_X86_64 498140fc727SYinghai Lu /* On K8 L1 TLB is inclusive, so don't count it */ 499140fc727SYinghai Lu c->x86_tlbsize = 0; 500140fc727SYinghai Lu #endif 501f7627e25SThomas Gleixner } 502f7627e25SThomas Gleixner 503f7627e25SThomas Gleixner if (n < 0x80000006) /* Some chips just has a large L1. */ 504f7627e25SThomas Gleixner return; 505f7627e25SThomas Gleixner 5060a488a53SYinghai Lu cpuid(0x80000006, &dummy, &ebx, &ecx, &edx); 507f7627e25SThomas Gleixner l2size = ecx >> 16; 508f7627e25SThomas Gleixner 509140fc727SYinghai Lu #ifdef CONFIG_X86_64 510140fc727SYinghai Lu c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff); 511140fc727SYinghai Lu #else 512f7627e25SThomas Gleixner /* do processor-specific cache resizing */ 51309dc68d9SJan Beulich if (this_cpu->legacy_cache_size) 51409dc68d9SJan Beulich l2size = this_cpu->legacy_cache_size(c, l2size); 515f7627e25SThomas Gleixner 516f7627e25SThomas Gleixner /* Allow user to override all this if necessary. */ 517f7627e25SThomas Gleixner if (cachesize_override != -1) 518f7627e25SThomas Gleixner l2size = cachesize_override; 519f7627e25SThomas Gleixner 520f7627e25SThomas Gleixner if (l2size == 0) 521f7627e25SThomas Gleixner return; /* Again, no L2 cache is possible */ 522140fc727SYinghai Lu #endif 523f7627e25SThomas Gleixner 524f7627e25SThomas Gleixner c->x86_cache_size = l2size; 525f7627e25SThomas Gleixner } 526f7627e25SThomas Gleixner 527e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO]; 528e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO]; 529e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO]; 530e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO]; 531e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO]; 532e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO]; 533dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO]; 534e0ba94f1SAlex Shi 535f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c) 536e0ba94f1SAlex Shi { 537e0ba94f1SAlex Shi if (this_cpu->c_detect_tlb) 538e0ba94f1SAlex Shi this_cpu->c_detect_tlb(c); 539e0ba94f1SAlex Shi 540f94fe119SSteven Honeyman pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n", 541e0ba94f1SAlex Shi tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES], 542f94fe119SSteven Honeyman tlb_lli_4m[ENTRIES]); 543f94fe119SSteven Honeyman 544f94fe119SSteven Honeyman pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n", 545f94fe119SSteven Honeyman tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES], 546f94fe119SSteven Honeyman tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]); 547e0ba94f1SAlex Shi } 548e0ba94f1SAlex Shi 549148f9bb8SPaul Gortmaker void detect_ht(struct cpuinfo_x86 *c) 5509d31d35bSYinghai Lu { 551c8e56d20SBorislav Petkov #ifdef CONFIG_SMP 5529d31d35bSYinghai Lu u32 eax, ebx, ecx, edx; 5539d31d35bSYinghai Lu int index_msb, core_bits; 5542eaad1fdSMike Travis static bool printed; 5559d31d35bSYinghai Lu 5560a488a53SYinghai Lu if (!cpu_has(c, X86_FEATURE_HT)) 5579d31d35bSYinghai Lu return; 5589d31d35bSYinghai Lu 5590a488a53SYinghai Lu if (cpu_has(c, X86_FEATURE_CMP_LEGACY)) 5600a488a53SYinghai Lu goto out; 5610a488a53SYinghai Lu 5621cd78776SYinghai Lu if (cpu_has(c, X86_FEATURE_XTOPOLOGY)) 5631cd78776SYinghai Lu return; 5641cd78776SYinghai Lu 5650a488a53SYinghai Lu cpuid(1, &eax, &ebx, &ecx, &edx); 5660a488a53SYinghai Lu 5679d31d35bSYinghai Lu smp_num_siblings = (ebx & 0xff0000) >> 16; 5689d31d35bSYinghai Lu 5699d31d35bSYinghai Lu if (smp_num_siblings == 1) { 5701b74dde7SChen Yucong pr_info_once("CPU0: Hyper-Threading is disabled\n"); 5710f3fa48aSIngo Molnar goto out; 5720f3fa48aSIngo Molnar } 5730f3fa48aSIngo Molnar 5740f3fa48aSIngo Molnar if (smp_num_siblings <= 1) 5750f3fa48aSIngo Molnar goto out; 5769d31d35bSYinghai Lu 5779d31d35bSYinghai Lu index_msb = get_count_order(smp_num_siblings); 578cb8cc442SIngo Molnar c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb); 5799d31d35bSYinghai Lu 5809d31d35bSYinghai Lu smp_num_siblings = smp_num_siblings / c->x86_max_cores; 5819d31d35bSYinghai Lu 5829d31d35bSYinghai Lu index_msb = get_count_order(smp_num_siblings); 5839d31d35bSYinghai Lu 5849d31d35bSYinghai Lu core_bits = get_count_order(c->x86_max_cores); 5859d31d35bSYinghai Lu 586cb8cc442SIngo Molnar c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) & 5871cd78776SYinghai Lu ((1 << core_bits) - 1); 5889d31d35bSYinghai Lu 5890a488a53SYinghai Lu out: 5902eaad1fdSMike Travis if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) { 5911b74dde7SChen Yucong pr_info("CPU: Physical Processor ID: %d\n", 5920a488a53SYinghai Lu c->phys_proc_id); 5931b74dde7SChen Yucong pr_info("CPU: Processor Core ID: %d\n", 5949d31d35bSYinghai Lu c->cpu_core_id); 5952eaad1fdSMike Travis printed = 1; 5969d31d35bSYinghai Lu } 5979d31d35bSYinghai Lu #endif 59897e4db7cSYinghai Lu } 599f7627e25SThomas Gleixner 600148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c) 601f7627e25SThomas Gleixner { 602f7627e25SThomas Gleixner char *v = c->x86_vendor_id; 6030f3fa48aSIngo Molnar int i; 604f7627e25SThomas Gleixner 605f7627e25SThomas Gleixner for (i = 0; i < X86_VENDOR_NUM; i++) { 60610a434fcSYinghai Lu if (!cpu_devs[i]) 60710a434fcSYinghai Lu break; 60810a434fcSYinghai Lu 609f7627e25SThomas Gleixner if (!strcmp(v, cpu_devs[i]->c_ident[0]) || 610f7627e25SThomas Gleixner (cpu_devs[i]->c_ident[1] && 611f7627e25SThomas Gleixner !strcmp(v, cpu_devs[i]->c_ident[1]))) { 6120f3fa48aSIngo Molnar 613f7627e25SThomas Gleixner this_cpu = cpu_devs[i]; 61410a434fcSYinghai Lu c->x86_vendor = this_cpu->c_x86_vendor; 615f7627e25SThomas Gleixner return; 616f7627e25SThomas Gleixner } 617f7627e25SThomas Gleixner } 61810a434fcSYinghai Lu 6191b74dde7SChen Yucong pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \ 620a9c56953SMinchan Kim "CPU: Your system may be unstable.\n", v); 62110a434fcSYinghai Lu 622f7627e25SThomas Gleixner c->x86_vendor = X86_VENDOR_UNKNOWN; 623f7627e25SThomas Gleixner this_cpu = &default_cpu; 624f7627e25SThomas Gleixner } 625f7627e25SThomas Gleixner 626148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c) 627f7627e25SThomas Gleixner { 628f7627e25SThomas Gleixner /* Get vendor name */ 6294a148513SHarvey Harrison cpuid(0x00000000, (unsigned int *)&c->cpuid_level, 6304a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[0], 6314a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[8], 6324a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[4]); 633f7627e25SThomas Gleixner 634f7627e25SThomas Gleixner c->x86 = 4; 6359d31d35bSYinghai Lu /* Intel-defined flags: level 0x00000001 */ 636f7627e25SThomas Gleixner if (c->cpuid_level >= 0x00000001) { 637f7627e25SThomas Gleixner u32 junk, tfms, cap0, misc; 6380f3fa48aSIngo Molnar 639f7627e25SThomas Gleixner cpuid(0x00000001, &tfms, &misc, &junk, &cap0); 64099f925ceSBorislav Petkov c->x86 = x86_family(tfms); 64199f925ceSBorislav Petkov c->x86_model = x86_model(tfms); 64299f925ceSBorislav Petkov c->x86_mask = x86_stepping(tfms); 6430f3fa48aSIngo Molnar 644d4387bd3SHuang, Ying if (cap0 & (1<<19)) { 645d4387bd3SHuang, Ying c->x86_clflush_size = ((misc >> 8) & 0xff) * 8; 6469d31d35bSYinghai Lu c->x86_cache_alignment = c->x86_clflush_size; 647d4387bd3SHuang, Ying } 648f7627e25SThomas Gleixner } 649f7627e25SThomas Gleixner } 6503da99c97SYinghai Lu 651148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c) 652093af8d7SYinghai Lu { 65339c06df4SBorislav Petkov u32 eax, ebx, ecx, edx; 654093af8d7SYinghai Lu 655093af8d7SYinghai Lu /* Intel-defined flags: level 0x00000001 */ 656093af8d7SYinghai Lu if (c->cpuid_level >= 0x00000001) { 65739c06df4SBorislav Petkov cpuid(0x00000001, &eax, &ebx, &ecx, &edx); 6580f3fa48aSIngo Molnar 65939c06df4SBorislav Petkov c->x86_capability[CPUID_1_ECX] = ecx; 66039c06df4SBorislav Petkov c->x86_capability[CPUID_1_EDX] = edx; 661093af8d7SYinghai Lu } 662093af8d7SYinghai Lu 663bdc802dcSH. Peter Anvin /* Additional Intel-defined flags: level 0x00000007 */ 664bdc802dcSH. Peter Anvin if (c->cpuid_level >= 0x00000007) { 665bdc802dcSH. Peter Anvin cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx); 666bdc802dcSH. Peter Anvin 66739c06df4SBorislav Petkov c->x86_capability[CPUID_7_0_EBX] = ebx; 6682ccd71f1SBorislav Petkov 66939c06df4SBorislav Petkov c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006); 670dfb4a70fSDave Hansen c->x86_capability[CPUID_7_ECX] = ecx; 671bdc802dcSH. Peter Anvin } 672bdc802dcSH. Peter Anvin 6736229ad27SFenghua Yu /* Extended state features: level 0x0000000d */ 6746229ad27SFenghua Yu if (c->cpuid_level >= 0x0000000d) { 6756229ad27SFenghua Yu cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx); 6766229ad27SFenghua Yu 67739c06df4SBorislav Petkov c->x86_capability[CPUID_D_1_EAX] = eax; 6786229ad27SFenghua Yu } 6796229ad27SFenghua Yu 680cbc82b17SPeter P Waskiewicz Jr /* Additional Intel-defined flags: level 0x0000000F */ 681cbc82b17SPeter P Waskiewicz Jr if (c->cpuid_level >= 0x0000000F) { 682cbc82b17SPeter P Waskiewicz Jr 683cbc82b17SPeter P Waskiewicz Jr /* QoS sub-leaf, EAX=0Fh, ECX=0 */ 684cbc82b17SPeter P Waskiewicz Jr cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx); 68539c06df4SBorislav Petkov c->x86_capability[CPUID_F_0_EDX] = edx; 68639c06df4SBorislav Petkov 687cbc82b17SPeter P Waskiewicz Jr if (cpu_has(c, X86_FEATURE_CQM_LLC)) { 688cbc82b17SPeter P Waskiewicz Jr /* will be overridden if occupancy monitoring exists */ 689cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid = ebx; 690cbc82b17SPeter P Waskiewicz Jr 691cbc82b17SPeter P Waskiewicz Jr /* QoS sub-leaf, EAX=0Fh, ECX=1 */ 692cbc82b17SPeter P Waskiewicz Jr cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx); 69339c06df4SBorislav Petkov c->x86_capability[CPUID_F_1_EDX] = edx; 69439c06df4SBorislav Petkov 69533c3cc7aSVikas Shivappa if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) || 69633c3cc7aSVikas Shivappa ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) || 69733c3cc7aSVikas Shivappa (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) { 698cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid = ecx; 699cbc82b17SPeter P Waskiewicz Jr c->x86_cache_occ_scale = ebx; 700cbc82b17SPeter P Waskiewicz Jr } 701cbc82b17SPeter P Waskiewicz Jr } else { 702cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid = -1; 703cbc82b17SPeter P Waskiewicz Jr c->x86_cache_occ_scale = -1; 704cbc82b17SPeter P Waskiewicz Jr } 705cbc82b17SPeter P Waskiewicz Jr } 706cbc82b17SPeter P Waskiewicz Jr 707093af8d7SYinghai Lu /* AMD-defined flags: level 0x80000001 */ 70839c06df4SBorislav Petkov eax = cpuid_eax(0x80000000); 70939c06df4SBorislav Petkov c->extended_cpuid_level = eax; 7100f3fa48aSIngo Molnar 71139c06df4SBorislav Petkov if ((eax & 0xffff0000) == 0x80000000) { 71239c06df4SBorislav Petkov if (eax >= 0x80000001) { 71339c06df4SBorislav Petkov cpuid(0x80000001, &eax, &ebx, &ecx, &edx); 71439c06df4SBorislav Petkov 71539c06df4SBorislav Petkov c->x86_capability[CPUID_8000_0001_ECX] = ecx; 71639c06df4SBorislav Petkov c->x86_capability[CPUID_8000_0001_EDX] = edx; 717093af8d7SYinghai Lu } 718093af8d7SYinghai Lu } 719093af8d7SYinghai Lu 7205122c890SYinghai Lu if (c->extended_cpuid_level >= 0x80000008) { 72139c06df4SBorislav Petkov cpuid(0x80000008, &eax, &ebx, &ecx, &edx); 7225122c890SYinghai Lu 7235122c890SYinghai Lu c->x86_virt_bits = (eax >> 8) & 0xff; 7245122c890SYinghai Lu c->x86_phys_bits = eax & 0xff; 72539c06df4SBorislav Petkov c->x86_capability[CPUID_8000_0008_EBX] = ebx; 7265122c890SYinghai Lu } 72713c6c532SJan Beulich #ifdef CONFIG_X86_32 72813c6c532SJan Beulich else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36)) 72913c6c532SJan Beulich c->x86_phys_bits = 36; 7305122c890SYinghai Lu #endif 731e3224234SYinghai Lu 732e3224234SYinghai Lu if (c->extended_cpuid_level >= 0x80000007) 733e3224234SYinghai Lu c->x86_power = cpuid_edx(0x80000007); 734e3224234SYinghai Lu 7352ccd71f1SBorislav Petkov if (c->extended_cpuid_level >= 0x8000000a) 73639c06df4SBorislav Petkov c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a); 7372ccd71f1SBorislav Petkov 7381dedefd1SJacob Pan init_scattered_cpuid_features(c); 739093af8d7SYinghai Lu } 740093af8d7SYinghai Lu 741148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c) 742aef93c8bSYinghai Lu { 743aef93c8bSYinghai Lu #ifdef CONFIG_X86_32 744aef93c8bSYinghai Lu int i; 745aef93c8bSYinghai Lu 746aef93c8bSYinghai Lu /* 747aef93c8bSYinghai Lu * First of all, decide if this is a 486 or higher 748aef93c8bSYinghai Lu * It's a 486 if we can modify the AC flag 749aef93c8bSYinghai Lu */ 750aef93c8bSYinghai Lu if (flag_is_changeable_p(X86_EFLAGS_AC)) 751aef93c8bSYinghai Lu c->x86 = 4; 752aef93c8bSYinghai Lu else 753aef93c8bSYinghai Lu c->x86 = 3; 754aef93c8bSYinghai Lu 755aef93c8bSYinghai Lu for (i = 0; i < X86_VENDOR_NUM; i++) 756aef93c8bSYinghai Lu if (cpu_devs[i] && cpu_devs[i]->c_identify) { 757aef93c8bSYinghai Lu c->x86_vendor_id[0] = 0; 758aef93c8bSYinghai Lu cpu_devs[i]->c_identify(c); 759aef93c8bSYinghai Lu if (c->x86_vendor_id[0]) { 760aef93c8bSYinghai Lu get_cpu_vendor(c); 761aef93c8bSYinghai Lu break; 762aef93c8bSYinghai Lu } 763aef93c8bSYinghai Lu } 764aef93c8bSYinghai Lu #endif 765093af8d7SYinghai Lu } 766f7627e25SThomas Gleixner 76734048c9eSPaolo Ciarrocchi /* 76834048c9eSPaolo Ciarrocchi * Do minimum CPU detection early. 76934048c9eSPaolo Ciarrocchi * Fields really needed: vendor, cpuid_level, family, model, mask, 77034048c9eSPaolo Ciarrocchi * cache alignment. 77134048c9eSPaolo Ciarrocchi * The others are not touched to avoid unwanted side effects. 77234048c9eSPaolo Ciarrocchi * 77334048c9eSPaolo Ciarrocchi * WARNING: this function is only called on the BP. Don't add code here 77434048c9eSPaolo Ciarrocchi * that is supposed to run on all CPUs. 77534048c9eSPaolo Ciarrocchi */ 7763da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c) 777f7627e25SThomas Gleixner { 7786627d242SYinghai Lu #ifdef CONFIG_X86_64 7796627d242SYinghai Lu c->x86_clflush_size = 64; 78013c6c532SJan Beulich c->x86_phys_bits = 36; 78113c6c532SJan Beulich c->x86_virt_bits = 48; 7826627d242SYinghai Lu #else 783d4387bd3SHuang, Ying c->x86_clflush_size = 32; 78413c6c532SJan Beulich c->x86_phys_bits = 32; 78513c6c532SJan Beulich c->x86_virt_bits = 32; 7866627d242SYinghai Lu #endif 7870a488a53SYinghai Lu c->x86_cache_alignment = c->x86_clflush_size; 788f7627e25SThomas Gleixner 7893da99c97SYinghai Lu memset(&c->x86_capability, 0, sizeof c->x86_capability); 7900a488a53SYinghai Lu c->extended_cpuid_level = 0; 7910a488a53SYinghai Lu 792aef93c8bSYinghai Lu if (!have_cpuid_p()) 793aef93c8bSYinghai Lu identify_cpu_without_cpuid(c); 794aef93c8bSYinghai Lu 795aef93c8bSYinghai Lu /* cyrix could have cpuid enabled via c_identify()*/ 796f7627e25SThomas Gleixner if (!have_cpuid_p()) 797f7627e25SThomas Gleixner return; 798f7627e25SThomas Gleixner 799f7627e25SThomas Gleixner cpu_detect(c); 8003da99c97SYinghai Lu get_cpu_vendor(c); 8013da99c97SYinghai Lu get_cpu_cap(c); 80212cf105cSKrzysztof Helt 80310a434fcSYinghai Lu if (this_cpu->c_early_init) 80410a434fcSYinghai Lu this_cpu->c_early_init(c); 8053da99c97SYinghai Lu 806f6e9456cSRobert Richter c->cpu_index = 0; 807b38b0665SH. Peter Anvin filter_cpuid_features(c, false); 808de5397adSFenghua Yu 809a110b5ecSBorislav Petkov if (this_cpu->c_bsp_init) 810a110b5ecSBorislav Petkov this_cpu->c_bsp_init(c); 811c3b83598SBorislav Petkov 812c3b83598SBorislav Petkov setup_force_cpu_cap(X86_FEATURE_ALWAYS); 813db52ef74SIngo Molnar fpu__init_system(c); 814f7627e25SThomas Gleixner } 815f7627e25SThomas Gleixner 8169d31d35bSYinghai Lu void __init early_cpu_init(void) 8179d31d35bSYinghai Lu { 81802dde8b4SJan Beulich const struct cpu_dev *const *cdev; 81910a434fcSYinghai Lu int count = 0; 8209d31d35bSYinghai Lu 821ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT 8221b74dde7SChen Yucong pr_info("KERNEL supported cpus:\n"); 82331c997caSIngo Molnar #endif 82431c997caSIngo Molnar 82510a434fcSYinghai Lu for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) { 82602dde8b4SJan Beulich const struct cpu_dev *cpudev = *cdev; 8279d31d35bSYinghai Lu 82810a434fcSYinghai Lu if (count >= X86_VENDOR_NUM) 82910a434fcSYinghai Lu break; 83010a434fcSYinghai Lu cpu_devs[count] = cpudev; 83110a434fcSYinghai Lu count++; 83210a434fcSYinghai Lu 833ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT 83431c997caSIngo Molnar { 83531c997caSIngo Molnar unsigned int j; 83631c997caSIngo Molnar 83710a434fcSYinghai Lu for (j = 0; j < 2; j++) { 83810a434fcSYinghai Lu if (!cpudev->c_ident[j]) 83910a434fcSYinghai Lu continue; 8401b74dde7SChen Yucong pr_info(" %s %s\n", cpudev->c_vendor, 84110a434fcSYinghai Lu cpudev->c_ident[j]); 84210a434fcSYinghai Lu } 84310a434fcSYinghai Lu } 8440388423dSDave Jones #endif 84531c997caSIngo Molnar } 8469d31d35bSYinghai Lu early_identify_cpu(&boot_cpu_data); 847f7627e25SThomas Gleixner } 848f7627e25SThomas Gleixner 849b6734c35SH. Peter Anvin /* 850366d4a43SBorislav Petkov * The NOPL instruction is supposed to exist on all CPUs of family >= 6; 851366d4a43SBorislav Petkov * unfortunately, that's not true in practice because of early VIA 852366d4a43SBorislav Petkov * chips and (more importantly) broken virtualizers that are not easy 853366d4a43SBorislav Petkov * to detect. In the latter case it doesn't even *fail* reliably, so 854366d4a43SBorislav Petkov * probing for it doesn't even work. Disable it completely on 32-bit 855ba0593bfSH. Peter Anvin * unless we can find a reliable way to detect all the broken cases. 856366d4a43SBorislav Petkov * Enable it explicitly on 64-bit for non-constant inputs of cpu_has(). 857b6734c35SH. Peter Anvin */ 858148f9bb8SPaul Gortmaker static void detect_nopl(struct cpuinfo_x86 *c) 859b6734c35SH. Peter Anvin { 860366d4a43SBorislav Petkov #ifdef CONFIG_X86_32 861b6734c35SH. Peter Anvin clear_cpu_cap(c, X86_FEATURE_NOPL); 862366d4a43SBorislav Petkov #else 863366d4a43SBorislav Petkov set_cpu_cap(c, X86_FEATURE_NOPL); 864366d4a43SBorislav Petkov #endif 865f7627e25SThomas Gleixner } 866f7627e25SThomas Gleixner 8677a5d6704SAndy Lutomirski static void detect_null_seg_behavior(struct cpuinfo_x86 *c) 8687a5d6704SAndy Lutomirski { 8697a5d6704SAndy Lutomirski #ifdef CONFIG_X86_64 8707a5d6704SAndy Lutomirski /* 8717a5d6704SAndy Lutomirski * Empirically, writing zero to a segment selector on AMD does 8727a5d6704SAndy Lutomirski * not clear the base, whereas writing zero to a segment 8737a5d6704SAndy Lutomirski * selector on Intel does clear the base. Intel's behavior 8747a5d6704SAndy Lutomirski * allows slightly faster context switches in the common case 8757a5d6704SAndy Lutomirski * where GS is unused by the prev and next threads. 8767a5d6704SAndy Lutomirski * 8777a5d6704SAndy Lutomirski * Since neither vendor documents this anywhere that I can see, 8787a5d6704SAndy Lutomirski * detect it directly instead of hardcoding the choice by 8797a5d6704SAndy Lutomirski * vendor. 8807a5d6704SAndy Lutomirski * 8817a5d6704SAndy Lutomirski * I've designated AMD's behavior as the "bug" because it's 8827a5d6704SAndy Lutomirski * counterintuitive and less friendly. 8837a5d6704SAndy Lutomirski */ 8847a5d6704SAndy Lutomirski 8857a5d6704SAndy Lutomirski unsigned long old_base, tmp; 8867a5d6704SAndy Lutomirski rdmsrl(MSR_FS_BASE, old_base); 8877a5d6704SAndy Lutomirski wrmsrl(MSR_FS_BASE, 1); 8887a5d6704SAndy Lutomirski loadsegment(fs, 0); 8897a5d6704SAndy Lutomirski rdmsrl(MSR_FS_BASE, tmp); 8907a5d6704SAndy Lutomirski if (tmp != 0) 8917a5d6704SAndy Lutomirski set_cpu_bug(c, X86_BUG_NULL_SEG); 8927a5d6704SAndy Lutomirski wrmsrl(MSR_FS_BASE, old_base); 8937a5d6704SAndy Lutomirski #endif 8947a5d6704SAndy Lutomirski } 8957a5d6704SAndy Lutomirski 896148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c) 897f7627e25SThomas Gleixner { 8983da99c97SYinghai Lu c->extended_cpuid_level = 0; 899f7627e25SThomas Gleixner 900aef93c8bSYinghai Lu if (!have_cpuid_p()) 901aef93c8bSYinghai Lu identify_cpu_without_cpuid(c); 902f7627e25SThomas Gleixner 903aef93c8bSYinghai Lu /* cyrix could have cpuid enabled via c_identify()*/ 904a9853dd6SIngo Molnar if (!have_cpuid_p()) 905aef93c8bSYinghai Lu return; 906aef93c8bSYinghai Lu 9073da99c97SYinghai Lu cpu_detect(c); 9083da99c97SYinghai Lu 9093da99c97SYinghai Lu get_cpu_vendor(c); 9103da99c97SYinghai Lu 9113da99c97SYinghai Lu get_cpu_cap(c); 9123da99c97SYinghai Lu 913f7627e25SThomas Gleixner if (c->cpuid_level >= 0x00000001) { 9143da99c97SYinghai Lu c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF; 915b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32 916c8e56d20SBorislav Petkov # ifdef CONFIG_SMP 917cb8cc442SIngo Molnar c->apicid = apic->phys_pkg_id(c->initial_apicid, 0); 918f7627e25SThomas Gleixner # else 91901aaea1aSYinghai Lu c->apicid = c->initial_apicid; 920f7627e25SThomas Gleixner # endif 921b89d3b3eSYinghai Lu #endif 922b89d3b3eSYinghai Lu c->phys_proc_id = c->initial_apicid; 923f7627e25SThomas Gleixner } 924f7627e25SThomas Gleixner 925f7627e25SThomas Gleixner get_model_name(c); /* Default name */ 926f7627e25SThomas Gleixner 927b6734c35SH. Peter Anvin detect_nopl(c); 9287a5d6704SAndy Lutomirski 9297a5d6704SAndy Lutomirski detect_null_seg_behavior(c); 930*0230bb03SAndy Lutomirski 931*0230bb03SAndy Lutomirski /* 932*0230bb03SAndy Lutomirski * ESPFIX is a strange bug. All real CPUs have it. Paravirt 933*0230bb03SAndy Lutomirski * systems that run Linux at CPL > 0 may or may not have the 934*0230bb03SAndy Lutomirski * issue, but, even if they have the issue, there's absolutely 935*0230bb03SAndy Lutomirski * nothing we can do about it because we can't use the real IRET 936*0230bb03SAndy Lutomirski * instruction. 937*0230bb03SAndy Lutomirski * 938*0230bb03SAndy Lutomirski * NB: For the time being, only 32-bit kernels support 939*0230bb03SAndy Lutomirski * X86_BUG_ESPFIX as such. 64-bit kernels directly choose 940*0230bb03SAndy Lutomirski * whether to apply espfix using paravirt hooks. If any 941*0230bb03SAndy Lutomirski * non-paravirt system ever shows up that does *not* have the 942*0230bb03SAndy Lutomirski * ESPFIX issue, we can change this. 943*0230bb03SAndy Lutomirski */ 944*0230bb03SAndy Lutomirski #ifdef CONFIG_X86_32 945*0230bb03SAndy Lutomirski # ifdef CONFIG_PARAVIRT 946*0230bb03SAndy Lutomirski do { 947*0230bb03SAndy Lutomirski extern void native_iret(void); 948*0230bb03SAndy Lutomirski if (pv_cpu_ops.iret == native_iret) 949*0230bb03SAndy Lutomirski set_cpu_bug(c, X86_BUG_ESPFIX); 950*0230bb03SAndy Lutomirski } while (0); 951*0230bb03SAndy Lutomirski # else 952*0230bb03SAndy Lutomirski set_cpu_bug(c, X86_BUG_ESPFIX); 953*0230bb03SAndy Lutomirski # endif 954*0230bb03SAndy Lutomirski #endif 955f7627e25SThomas Gleixner } 956f7627e25SThomas Gleixner 957cbc82b17SPeter P Waskiewicz Jr static void x86_init_cache_qos(struct cpuinfo_x86 *c) 958cbc82b17SPeter P Waskiewicz Jr { 959cbc82b17SPeter P Waskiewicz Jr /* 960cbc82b17SPeter P Waskiewicz Jr * The heavy lifting of max_rmid and cache_occ_scale are handled 961cbc82b17SPeter P Waskiewicz Jr * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu 962cbc82b17SPeter P Waskiewicz Jr * in case CQM bits really aren't there in this CPU. 963cbc82b17SPeter P Waskiewicz Jr */ 964cbc82b17SPeter P Waskiewicz Jr if (c != &boot_cpu_data) { 965cbc82b17SPeter P Waskiewicz Jr boot_cpu_data.x86_cache_max_rmid = 966cbc82b17SPeter P Waskiewicz Jr min(boot_cpu_data.x86_cache_max_rmid, 967cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid); 968cbc82b17SPeter P Waskiewicz Jr } 969cbc82b17SPeter P Waskiewicz Jr } 970cbc82b17SPeter P Waskiewicz Jr 971f7627e25SThomas Gleixner /* 972f7627e25SThomas Gleixner * This does the hard work of actually picking apart the CPU stuff... 973f7627e25SThomas Gleixner */ 974148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c) 975f7627e25SThomas Gleixner { 976f7627e25SThomas Gleixner int i; 977f7627e25SThomas Gleixner 978f7627e25SThomas Gleixner c->loops_per_jiffy = loops_per_jiffy; 979f7627e25SThomas Gleixner c->x86_cache_size = -1; 980f7627e25SThomas Gleixner c->x86_vendor = X86_VENDOR_UNKNOWN; 981f7627e25SThomas Gleixner c->x86_model = c->x86_mask = 0; /* So far unknown... */ 982f7627e25SThomas Gleixner c->x86_vendor_id[0] = '\0'; /* Unset */ 983f7627e25SThomas Gleixner c->x86_model_id[0] = '\0'; /* Unset */ 984f7627e25SThomas Gleixner c->x86_max_cores = 1; 985102bbe3aSYinghai Lu c->x86_coreid_bits = 0; 98611fdd252SYinghai Lu #ifdef CONFIG_X86_64 987102bbe3aSYinghai Lu c->x86_clflush_size = 64; 98813c6c532SJan Beulich c->x86_phys_bits = 36; 98913c6c532SJan Beulich c->x86_virt_bits = 48; 990102bbe3aSYinghai Lu #else 991102bbe3aSYinghai Lu c->cpuid_level = -1; /* CPUID not detected */ 992f7627e25SThomas Gleixner c->x86_clflush_size = 32; 99313c6c532SJan Beulich c->x86_phys_bits = 32; 99413c6c532SJan Beulich c->x86_virt_bits = 32; 995102bbe3aSYinghai Lu #endif 996102bbe3aSYinghai Lu c->x86_cache_alignment = c->x86_clflush_size; 997f7627e25SThomas Gleixner memset(&c->x86_capability, 0, sizeof c->x86_capability); 998f7627e25SThomas Gleixner 999f7627e25SThomas Gleixner generic_identify(c); 1000f7627e25SThomas Gleixner 10013898534dSAndi Kleen if (this_cpu->c_identify) 1002f7627e25SThomas Gleixner this_cpu->c_identify(c); 1003f7627e25SThomas Gleixner 10046a6256f9SAdam Buchbinder /* Clear/Set all flags overridden by options, after probe */ 10052759c328SYinghai Lu for (i = 0; i < NCAPINTS; i++) { 10062759c328SYinghai Lu c->x86_capability[i] &= ~cpu_caps_cleared[i]; 10072759c328SYinghai Lu c->x86_capability[i] |= cpu_caps_set[i]; 10082759c328SYinghai Lu } 10092759c328SYinghai Lu 1010102bbe3aSYinghai Lu #ifdef CONFIG_X86_64 1011cb8cc442SIngo Molnar c->apicid = apic->phys_pkg_id(c->initial_apicid, 0); 1012102bbe3aSYinghai Lu #endif 1013102bbe3aSYinghai Lu 1014f7627e25SThomas Gleixner /* 1015f7627e25SThomas Gleixner * Vendor-specific initialization. In this section we 1016f7627e25SThomas Gleixner * canonicalize the feature flags, meaning if there are 1017f7627e25SThomas Gleixner * features a certain CPU supports which CPUID doesn't 1018f7627e25SThomas Gleixner * tell us, CPUID claiming incorrect flags, or other bugs, 1019f7627e25SThomas Gleixner * we handle them here. 1020f7627e25SThomas Gleixner * 1021f7627e25SThomas Gleixner * At the end of this section, c->x86_capability better 1022f7627e25SThomas Gleixner * indicate the features this CPU genuinely supports! 1023f7627e25SThomas Gleixner */ 1024f7627e25SThomas Gleixner if (this_cpu->c_init) 1025f7627e25SThomas Gleixner this_cpu->c_init(c); 1026f7627e25SThomas Gleixner 1027f7627e25SThomas Gleixner /* Disable the PN if appropriate */ 1028f7627e25SThomas Gleixner squash_the_stupid_serial_number(c); 1029f7627e25SThomas Gleixner 1030b2cc2a07SH. Peter Anvin /* Set up SMEP/SMAP */ 1031b2cc2a07SH. Peter Anvin setup_smep(c); 1032b2cc2a07SH. Peter Anvin setup_smap(c); 1033b2cc2a07SH. Peter Anvin 1034f7627e25SThomas Gleixner /* 10350f3fa48aSIngo Molnar * The vendor-specific functions might have changed features. 10360f3fa48aSIngo Molnar * Now we do "generic changes." 1037f7627e25SThomas Gleixner */ 1038f7627e25SThomas Gleixner 1039b38b0665SH. Peter Anvin /* Filter out anything that depends on CPUID levels we don't have */ 1040b38b0665SH. Peter Anvin filter_cpuid_features(c, true); 1041b38b0665SH. Peter Anvin 1042f7627e25SThomas Gleixner /* If the model name is still unset, do table lookup. */ 1043f7627e25SThomas Gleixner if (!c->x86_model_id[0]) { 104402dde8b4SJan Beulich const char *p; 1045f7627e25SThomas Gleixner p = table_lookup_model(c); 1046f7627e25SThomas Gleixner if (p) 1047f7627e25SThomas Gleixner strcpy(c->x86_model_id, p); 1048f7627e25SThomas Gleixner else 1049f7627e25SThomas Gleixner /* Last resort... */ 1050f7627e25SThomas Gleixner sprintf(c->x86_model_id, "%02x/%02x", 1051f7627e25SThomas Gleixner c->x86, c->x86_model); 1052f7627e25SThomas Gleixner } 1053f7627e25SThomas Gleixner 1054102bbe3aSYinghai Lu #ifdef CONFIG_X86_64 1055102bbe3aSYinghai Lu detect_ht(c); 1056102bbe3aSYinghai Lu #endif 1057102bbe3aSYinghai Lu 105888b094fbSAlok Kataria init_hypervisor(c); 105949d859d7SH. Peter Anvin x86_init_rdrand(c); 1060cbc82b17SPeter P Waskiewicz Jr x86_init_cache_qos(c); 106106976945SDave Hansen setup_pku(c); 10623e0c3737SYinghai Lu 10633e0c3737SYinghai Lu /* 10646a6256f9SAdam Buchbinder * Clear/Set all flags overridden by options, need do it 10653e0c3737SYinghai Lu * before following smp all cpus cap AND. 10663e0c3737SYinghai Lu */ 10673e0c3737SYinghai Lu for (i = 0; i < NCAPINTS; i++) { 10683e0c3737SYinghai Lu c->x86_capability[i] &= ~cpu_caps_cleared[i]; 10693e0c3737SYinghai Lu c->x86_capability[i] |= cpu_caps_set[i]; 10703e0c3737SYinghai Lu } 10713e0c3737SYinghai Lu 1072f7627e25SThomas Gleixner /* 1073f7627e25SThomas Gleixner * On SMP, boot_cpu_data holds the common feature set between 1074f7627e25SThomas Gleixner * all CPUs; so make sure that we indicate which features are 1075f7627e25SThomas Gleixner * common between the CPUs. The first time this routine gets 1076f7627e25SThomas Gleixner * executed, c == &boot_cpu_data. 1077f7627e25SThomas Gleixner */ 1078f7627e25SThomas Gleixner if (c != &boot_cpu_data) { 1079f7627e25SThomas Gleixner /* AND the already accumulated flags with these */ 1080f7627e25SThomas Gleixner for (i = 0; i < NCAPINTS; i++) 1081f7627e25SThomas Gleixner boot_cpu_data.x86_capability[i] &= c->x86_capability[i]; 108265fc985bSBorislav Petkov 108365fc985bSBorislav Petkov /* OR, i.e. replicate the bug flags */ 108465fc985bSBorislav Petkov for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++) 108565fc985bSBorislav Petkov c->x86_capability[i] |= boot_cpu_data.x86_capability[i]; 1086f7627e25SThomas Gleixner } 1087f7627e25SThomas Gleixner 1088f7627e25SThomas Gleixner /* Init Machine Check Exception if available. */ 10895e09954aSBorislav Petkov mcheck_cpu_init(c); 109030d432dfSAndi Kleen 109130d432dfSAndi Kleen select_idle_routine(c); 1092102bbe3aSYinghai Lu 1093de2d9445STejun Heo #ifdef CONFIG_NUMA 1094102bbe3aSYinghai Lu numa_add_cpu(smp_processor_id()); 1095102bbe3aSYinghai Lu #endif 10961f12e32fSThomas Gleixner /* The boot/hotplug time assigment got cleared, restore it */ 10971f12e32fSThomas Gleixner c->logical_proc_id = topology_phys_to_logical_pkg(c->phys_proc_id); 1098f7627e25SThomas Gleixner } 1099f7627e25SThomas Gleixner 11008b6c0ab1SIngo Molnar /* 11018b6c0ab1SIngo Molnar * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions 11028b6c0ab1SIngo Molnar * on 32-bit kernels: 11038b6c0ab1SIngo Molnar */ 1104cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32 1105cfda7bb9SAndy Lutomirski void enable_sep_cpu(void) 1106cfda7bb9SAndy Lutomirski { 11078b6c0ab1SIngo Molnar struct tss_struct *tss; 11088b6c0ab1SIngo Molnar int cpu; 1109cfda7bb9SAndy Lutomirski 1110b3edfda4SBorislav Petkov if (!boot_cpu_has(X86_FEATURE_SEP)) 1111b3edfda4SBorislav Petkov return; 1112b3edfda4SBorislav Petkov 11138b6c0ab1SIngo Molnar cpu = get_cpu(); 11148b6c0ab1SIngo Molnar tss = &per_cpu(cpu_tss, cpu); 11158b6c0ab1SIngo Molnar 11168b6c0ab1SIngo Molnar /* 1117cf9328ccSAndy Lutomirski * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field -- 1118cf9328ccSAndy Lutomirski * see the big comment in struct x86_hw_tss's definition. 11198b6c0ab1SIngo Molnar */ 1120cfda7bb9SAndy Lutomirski 1121cfda7bb9SAndy Lutomirski tss->x86_tss.ss1 = __KERNEL_CS; 11228b6c0ab1SIngo Molnar wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0); 11238b6c0ab1SIngo Molnar 1124cf9328ccSAndy Lutomirski wrmsr(MSR_IA32_SYSENTER_ESP, 1125cf9328ccSAndy Lutomirski (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack), 1126cf9328ccSAndy Lutomirski 0); 11278b6c0ab1SIngo Molnar 11284c8cd0c5SIngo Molnar wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0); 11298b6c0ab1SIngo Molnar 1130cfda7bb9SAndy Lutomirski put_cpu(); 1131cfda7bb9SAndy Lutomirski } 1132e04d645fSGlauber Costa #endif 1133e04d645fSGlauber Costa 1134f7627e25SThomas Gleixner void __init identify_boot_cpu(void) 1135f7627e25SThomas Gleixner { 1136f7627e25SThomas Gleixner identify_cpu(&boot_cpu_data); 113702c68a02SLen Brown init_amd_e400_c1e_mask(); 1138102bbe3aSYinghai Lu #ifdef CONFIG_X86_32 1139f7627e25SThomas Gleixner sysenter_setup(); 1140f7627e25SThomas Gleixner enable_sep_cpu(); 1141102bbe3aSYinghai Lu #endif 1142e0ba94f1SAlex Shi cpu_detect_tlb(&boot_cpu_data); 1143f7627e25SThomas Gleixner } 1144f7627e25SThomas Gleixner 1145148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c) 1146f7627e25SThomas Gleixner { 1147f7627e25SThomas Gleixner BUG_ON(c == &boot_cpu_data); 1148f7627e25SThomas Gleixner identify_cpu(c); 1149102bbe3aSYinghai Lu #ifdef CONFIG_X86_32 1150f7627e25SThomas Gleixner enable_sep_cpu(); 1151102bbe3aSYinghai Lu #endif 1152f7627e25SThomas Gleixner mtrr_ap_init(); 1153f7627e25SThomas Gleixner } 1154f7627e25SThomas Gleixner 1155a0854a46SYinghai Lu struct msr_range { 1156a0854a46SYinghai Lu unsigned min; 1157a0854a46SYinghai Lu unsigned max; 1158a0854a46SYinghai Lu }; 1159a0854a46SYinghai Lu 1160148f9bb8SPaul Gortmaker static const struct msr_range msr_range_array[] = { 1161a0854a46SYinghai Lu { 0x00000000, 0x00000418}, 1162a0854a46SYinghai Lu { 0xc0000000, 0xc000040b}, 1163a0854a46SYinghai Lu { 0xc0010000, 0xc0010142}, 1164a0854a46SYinghai Lu { 0xc0011000, 0xc001103b}, 1165a0854a46SYinghai Lu }; 1166a0854a46SYinghai Lu 1167148f9bb8SPaul Gortmaker static void __print_cpu_msr(void) 1168f7627e25SThomas Gleixner { 11690f3fa48aSIngo Molnar unsigned index_min, index_max; 1170a0854a46SYinghai Lu unsigned index; 1171a0854a46SYinghai Lu u64 val; 1172a0854a46SYinghai Lu int i; 1173f7627e25SThomas Gleixner 1174a0854a46SYinghai Lu for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) { 1175a0854a46SYinghai Lu index_min = msr_range_array[i].min; 1176a0854a46SYinghai Lu index_max = msr_range_array[i].max; 11770f3fa48aSIngo Molnar 1178a0854a46SYinghai Lu for (index = index_min; index < index_max; index++) { 1179ecd431d9SBorislav Petkov if (rdmsrl_safe(index, &val)) 1180a0854a46SYinghai Lu continue; 11811b74dde7SChen Yucong pr_info(" MSR%08x: %016llx\n", index, val); 1182f7627e25SThomas Gleixner } 1183f7627e25SThomas Gleixner } 1184a0854a46SYinghai Lu } 1185a0854a46SYinghai Lu 1186148f9bb8SPaul Gortmaker static int show_msr; 11870f3fa48aSIngo Molnar 1188a0854a46SYinghai Lu static __init int setup_show_msr(char *arg) 1189a0854a46SYinghai Lu { 1190a0854a46SYinghai Lu int num; 1191a0854a46SYinghai Lu 1192a0854a46SYinghai Lu get_option(&arg, &num); 1193a0854a46SYinghai Lu 1194a0854a46SYinghai Lu if (num > 0) 1195a0854a46SYinghai Lu show_msr = num; 1196a0854a46SYinghai Lu return 1; 1197a0854a46SYinghai Lu } 1198a0854a46SYinghai Lu __setup("show_msr=", setup_show_msr); 1199f7627e25SThomas Gleixner 1200191679fdSAndi Kleen static __init int setup_noclflush(char *arg) 1201191679fdSAndi Kleen { 1202840d2830SH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_CLFLUSH); 1203da4aaa7dSH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT); 1204191679fdSAndi Kleen return 1; 1205191679fdSAndi Kleen } 1206191679fdSAndi Kleen __setup("noclflush", setup_noclflush); 1207191679fdSAndi Kleen 1208148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c) 1209f7627e25SThomas Gleixner { 121002dde8b4SJan Beulich const char *vendor = NULL; 1211f7627e25SThomas Gleixner 12120f3fa48aSIngo Molnar if (c->x86_vendor < X86_VENDOR_NUM) { 1213f7627e25SThomas Gleixner vendor = this_cpu->c_vendor; 12140f3fa48aSIngo Molnar } else { 12150f3fa48aSIngo Molnar if (c->cpuid_level >= 0) 1216f7627e25SThomas Gleixner vendor = c->x86_vendor_id; 12170f3fa48aSIngo Molnar } 1218f7627e25SThomas Gleixner 1219bd32a8cfSYinghai Lu if (vendor && !strstr(c->x86_model_id, vendor)) 12201b74dde7SChen Yucong pr_cont("%s ", vendor); 1221f7627e25SThomas Gleixner 12229d31d35bSYinghai Lu if (c->x86_model_id[0]) 12231b74dde7SChen Yucong pr_cont("%s", c->x86_model_id); 1224f7627e25SThomas Gleixner else 12251b74dde7SChen Yucong pr_cont("%d86", c->x86); 1226f7627e25SThomas Gleixner 12271b74dde7SChen Yucong pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model); 1228924e101aSBorislav Petkov 1229f7627e25SThomas Gleixner if (c->x86_mask || c->cpuid_level >= 0) 12301b74dde7SChen Yucong pr_cont(", stepping: 0x%x)\n", c->x86_mask); 1231f7627e25SThomas Gleixner else 12321b74dde7SChen Yucong pr_cont(")\n"); 1233a0854a46SYinghai Lu 12340b8b8078SYinghai Lu print_cpu_msr(c); 123521c3fcf3SYinghai Lu } 123621c3fcf3SYinghai Lu 1237148f9bb8SPaul Gortmaker void print_cpu_msr(struct cpuinfo_x86 *c) 123821c3fcf3SYinghai Lu { 1239a0854a46SYinghai Lu if (c->cpu_index < show_msr) 124021c3fcf3SYinghai Lu __print_cpu_msr(); 1241f7627e25SThomas Gleixner } 1242f7627e25SThomas Gleixner 1243ac72e788SAndi Kleen static __init int setup_disablecpuid(char *arg) 1244ac72e788SAndi Kleen { 1245ac72e788SAndi Kleen int bit; 12460f3fa48aSIngo Molnar 1247ac72e788SAndi Kleen if (get_option(&arg, &bit) && bit < NCAPINTS*32) 1248ac72e788SAndi Kleen setup_clear_cpu_cap(bit); 1249ac72e788SAndi Kleen else 1250ac72e788SAndi Kleen return 0; 12510f3fa48aSIngo Molnar 1252ac72e788SAndi Kleen return 1; 1253ac72e788SAndi Kleen } 1254ac72e788SAndi Kleen __setup("clearcpuid=", setup_disablecpuid); 1255ac72e788SAndi Kleen 1256d5494d4fSYinghai Lu #ifdef CONFIG_X86_64 12579ff80942SCyrill Gorcunov struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table }; 1258629f4f9dSSeiji Aguchi struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1, 1259629f4f9dSSeiji Aguchi (unsigned long) debug_idt_table }; 1260d5494d4fSYinghai Lu 1261947e76cdSBrian Gerst DEFINE_PER_CPU_FIRST(union irq_stack_union, 1262277d5b40SAndi Kleen irq_stack_union) __aligned(PAGE_SIZE) __visible; 12630f3fa48aSIngo Molnar 1264bdf977b3STejun Heo /* 1265a7fcf28dSAndy Lutomirski * The following percpu variables are hot. Align current_task to 1266a7fcf28dSAndy Lutomirski * cacheline size such that they fall in the same cacheline. 1267bdf977b3STejun Heo */ 1268bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned = 1269bdf977b3STejun Heo &init_task; 1270bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task); 1271d5494d4fSYinghai Lu 1272bdf977b3STejun Heo DEFINE_PER_CPU(char *, irq_stack_ptr) = 1273bdf977b3STejun Heo init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64; 1274bdf977b3STejun Heo 1275277d5b40SAndi Kleen DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1; 1276d5494d4fSYinghai Lu 1277c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT; 1278c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count); 1279c2daa3beSPeter Zijlstra 12800f3fa48aSIngo Molnar /* 12810f3fa48aSIngo Molnar * Special IST stacks which the CPU switches to when it calls 12820f3fa48aSIngo Molnar * an IST-marked descriptor entry. Up to 7 stacks (hardware 12830f3fa48aSIngo Molnar * limit), all of them are 4K, except the debug stack which 12840f3fa48aSIngo Molnar * is 8K. 12850f3fa48aSIngo Molnar */ 12860f3fa48aSIngo Molnar static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = { 12870f3fa48aSIngo Molnar [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ, 12880f3fa48aSIngo Molnar [DEBUG_STACK - 1] = DEBUG_STKSZ 12890f3fa48aSIngo Molnar }; 12900f3fa48aSIngo Molnar 129192d65b23SBrian Gerst static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks 12923e352aa8STejun Heo [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]); 1293d5494d4fSYinghai Lu 1294d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */ 1295d5494d4fSYinghai Lu void syscall_init(void) 1296d5494d4fSYinghai Lu { 1297d5494d4fSYinghai Lu /* 1298d5494d4fSYinghai Lu * LSTAR and STAR live in a bit strange symbiosis. 1299d5494d4fSYinghai Lu * They both write to the same internal register. STAR allows to 1300d5494d4fSYinghai Lu * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip. 1301d5494d4fSYinghai Lu */ 130231ac34caSBorislav Petkov wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS); 130347edb651SAndy Lutomirski wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64); 1304d56fe4bfSIngo Molnar 1305d56fe4bfSIngo Molnar #ifdef CONFIG_IA32_EMULATION 130647edb651SAndy Lutomirski wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat); 1307a76c7f46SDenys Vlasenko /* 1308487d1edbSDenys Vlasenko * This only works on Intel CPUs. 1309487d1edbSDenys Vlasenko * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP. 1310487d1edbSDenys Vlasenko * This does not cause SYSENTER to jump to the wrong location, because 1311487d1edbSDenys Vlasenko * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit). 1312a76c7f46SDenys Vlasenko */ 1313a76c7f46SDenys Vlasenko wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS); 1314a76c7f46SDenys Vlasenko wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL); 13154c8cd0c5SIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat); 1316d56fe4bfSIngo Molnar #else 131747edb651SAndy Lutomirski wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret); 13186b51311cSBorislav Petkov wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG); 1319d56fe4bfSIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL); 1320d56fe4bfSIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL); 1321d5494d4fSYinghai Lu #endif 1322d5494d4fSYinghai Lu 1323d5494d4fSYinghai Lu /* Flags to clear on syscall */ 1324d5494d4fSYinghai Lu wrmsrl(MSR_SYSCALL_MASK, 132563bcff2aSH. Peter Anvin X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF| 13268c7aa698SAndy Lutomirski X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT); 1327d5494d4fSYinghai Lu } 1328d5494d4fSYinghai Lu 1329d5494d4fSYinghai Lu /* 1330d5494d4fSYinghai Lu * Copies of the original ist values from the tss are only accessed during 1331d5494d4fSYinghai Lu * debugging, no special alignment required. 1332d5494d4fSYinghai Lu */ 1333d5494d4fSYinghai Lu DEFINE_PER_CPU(struct orig_ist, orig_ist); 1334d5494d4fSYinghai Lu 1335228bdaa9SSteven Rostedt static DEFINE_PER_CPU(unsigned long, debug_stack_addr); 133642181186SSteven Rostedt DEFINE_PER_CPU(int, debug_stack_usage); 1337228bdaa9SSteven Rostedt 1338228bdaa9SSteven Rostedt int is_debug_stack(unsigned long addr) 1339228bdaa9SSteven Rostedt { 134089cbc767SChristoph Lameter return __this_cpu_read(debug_stack_usage) || 134189cbc767SChristoph Lameter (addr <= __this_cpu_read(debug_stack_addr) && 134289cbc767SChristoph Lameter addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ)); 1343228bdaa9SSteven Rostedt } 13440f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(is_debug_stack); 1345228bdaa9SSteven Rostedt 1346629f4f9dSSeiji Aguchi DEFINE_PER_CPU(u32, debug_idt_ctr); 1347f8988175SSteven Rostedt 1348228bdaa9SSteven Rostedt void debug_stack_set_zero(void) 1349228bdaa9SSteven Rostedt { 1350629f4f9dSSeiji Aguchi this_cpu_inc(debug_idt_ctr); 1351629f4f9dSSeiji Aguchi load_current_idt(); 1352228bdaa9SSteven Rostedt } 13530f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_set_zero); 1354228bdaa9SSteven Rostedt 1355228bdaa9SSteven Rostedt void debug_stack_reset(void) 1356228bdaa9SSteven Rostedt { 1357629f4f9dSSeiji Aguchi if (WARN_ON(!this_cpu_read(debug_idt_ctr))) 1358f8988175SSteven Rostedt return; 1359629f4f9dSSeiji Aguchi if (this_cpu_dec_return(debug_idt_ctr) == 0) 1360629f4f9dSSeiji Aguchi load_current_idt(); 1361228bdaa9SSteven Rostedt } 13620f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_reset); 1363228bdaa9SSteven Rostedt 13640f3fa48aSIngo Molnar #else /* CONFIG_X86_64 */ 1365d5494d4fSYinghai Lu 1366bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task; 1367bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task); 1368c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT; 1369c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count); 1370bdf977b3STejun Heo 1371a7fcf28dSAndy Lutomirski /* 1372a7fcf28dSAndy Lutomirski * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find 1373a7fcf28dSAndy Lutomirski * the top of the kernel stack. Use an extra percpu variable to track the 1374a7fcf28dSAndy Lutomirski * top of the kernel stack directly. 1375a7fcf28dSAndy Lutomirski */ 1376a7fcf28dSAndy Lutomirski DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) = 1377a7fcf28dSAndy Lutomirski (unsigned long)&init_thread_union + THREAD_SIZE; 1378a7fcf28dSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack); 1379a7fcf28dSAndy Lutomirski 138060a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR 138153f82452SJeremy Fitzhardinge DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary); 138260a5317fSTejun Heo #endif 138360a5317fSTejun Heo 13840f3fa48aSIngo Molnar #endif /* CONFIG_X86_64 */ 1385f7627e25SThomas Gleixner 1386f7627e25SThomas Gleixner /* 13879766cdbcSJaswinder Singh Rajput * Clear all 6 debug registers: 13889766cdbcSJaswinder Singh Rajput */ 13899766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void) 13909766cdbcSJaswinder Singh Rajput { 13919766cdbcSJaswinder Singh Rajput int i; 13929766cdbcSJaswinder Singh Rajput 13939766cdbcSJaswinder Singh Rajput for (i = 0; i < 8; i++) { 13949766cdbcSJaswinder Singh Rajput /* Ignore db4, db5 */ 13959766cdbcSJaswinder Singh Rajput if ((i == 4) || (i == 5)) 13969766cdbcSJaswinder Singh Rajput continue; 13979766cdbcSJaswinder Singh Rajput 13989766cdbcSJaswinder Singh Rajput set_debugreg(0, i); 13999766cdbcSJaswinder Singh Rajput } 14009766cdbcSJaswinder Singh Rajput } 1401f7627e25SThomas Gleixner 14020bb9fef9SJason Wessel #ifdef CONFIG_KGDB 14030bb9fef9SJason Wessel /* 14040bb9fef9SJason Wessel * Restore debug regs if using kgdbwait and you have a kernel debugger 14050bb9fef9SJason Wessel * connection established. 14060bb9fef9SJason Wessel */ 14070bb9fef9SJason Wessel static void dbg_restore_debug_regs(void) 14080bb9fef9SJason Wessel { 14090bb9fef9SJason Wessel if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break)) 14100bb9fef9SJason Wessel arch_kgdb_ops.correct_hw_break(); 14110bb9fef9SJason Wessel } 14120bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */ 14130bb9fef9SJason Wessel #define dbg_restore_debug_regs() 14140bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */ 14150bb9fef9SJason Wessel 1416ce4b1b16SIgor Mammedov static void wait_for_master_cpu(int cpu) 1417ce4b1b16SIgor Mammedov { 1418ce4b1b16SIgor Mammedov #ifdef CONFIG_SMP 1419ce4b1b16SIgor Mammedov /* 1420ce4b1b16SIgor Mammedov * wait for ACK from master CPU before continuing 1421ce4b1b16SIgor Mammedov * with AP initialization 1422ce4b1b16SIgor Mammedov */ 1423ce4b1b16SIgor Mammedov WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)); 1424ce4b1b16SIgor Mammedov while (!cpumask_test_cpu(cpu, cpu_callout_mask)) 1425ce4b1b16SIgor Mammedov cpu_relax(); 1426ce4b1b16SIgor Mammedov #endif 1427ce4b1b16SIgor Mammedov } 1428ce4b1b16SIgor Mammedov 1429f7627e25SThomas Gleixner /* 1430f7627e25SThomas Gleixner * cpu_init() initializes state that is per-CPU. Some data is already 1431f7627e25SThomas Gleixner * initialized (naturally) in the bootstrap process, such as the GDT 1432f7627e25SThomas Gleixner * and IDT. We reload them nevertheless, this function acts as a 1433f7627e25SThomas Gleixner * 'CPU state barrier', nothing should get across. 14341ba76586SYinghai Lu * A lot of state is already set up in PDA init for 64 bit 1435f7627e25SThomas Gleixner */ 14361ba76586SYinghai Lu #ifdef CONFIG_X86_64 14370f3fa48aSIngo Molnar 1438148f9bb8SPaul Gortmaker void cpu_init(void) 14391ba76586SYinghai Lu { 14400fe1e009STejun Heo struct orig_ist *oist; 14411ba76586SYinghai Lu struct task_struct *me; 14420f3fa48aSIngo Molnar struct tss_struct *t; 14430f3fa48aSIngo Molnar unsigned long v; 1444ce4b1b16SIgor Mammedov int cpu = stack_smp_processor_id(); 14451ba76586SYinghai Lu int i; 14461ba76586SYinghai Lu 1447ce4b1b16SIgor Mammedov wait_for_master_cpu(cpu); 1448ce4b1b16SIgor Mammedov 1449e6ebf5deSFenghua Yu /* 14501e02ce4cSAndy Lutomirski * Initialize the CR4 shadow before doing anything that could 14511e02ce4cSAndy Lutomirski * try to read it. 14521e02ce4cSAndy Lutomirski */ 14531e02ce4cSAndy Lutomirski cr4_init_shadow(); 14541e02ce4cSAndy Lutomirski 14551e02ce4cSAndy Lutomirski /* 1456e6ebf5deSFenghua Yu * Load microcode on this cpu if a valid microcode is available. 1457e6ebf5deSFenghua Yu * This is early microcode loading procedure. 1458e6ebf5deSFenghua Yu */ 1459e6ebf5deSFenghua Yu load_ucode_ap(); 1460e6ebf5deSFenghua Yu 146124933b82SAndy Lutomirski t = &per_cpu(cpu_tss, cpu); 14620fe1e009STejun Heo oist = &per_cpu(orig_ist, cpu); 14630f3fa48aSIngo Molnar 1464e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA 146527fd185fSFenghua Yu if (this_cpu_read(numa_node) == 0 && 1466e534c7c5SLee Schermerhorn early_cpu_to_node(cpu) != NUMA_NO_NODE) 1467e534c7c5SLee Schermerhorn set_numa_node(early_cpu_to_node(cpu)); 1468e7a22c1eSBrian Gerst #endif 14691ba76586SYinghai Lu 14701ba76586SYinghai Lu me = current; 14711ba76586SYinghai Lu 14722eaad1fdSMike Travis pr_debug("Initializing CPU#%d\n", cpu); 14731ba76586SYinghai Lu 1474375074ccSAndy Lutomirski cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); 14751ba76586SYinghai Lu 14761ba76586SYinghai Lu /* 14771ba76586SYinghai Lu * Initialize the per-CPU GDT with the boot GDT, 14781ba76586SYinghai Lu * and set up the GDT descriptor: 14791ba76586SYinghai Lu */ 14801ba76586SYinghai Lu 1481552be871SBrian Gerst switch_to_new_gdt(cpu); 14822697fbd5SBrian Gerst loadsegment(fs, 0); 14832697fbd5SBrian Gerst 1484cf910e83SSeiji Aguchi load_current_idt(); 14851ba76586SYinghai Lu 14861ba76586SYinghai Lu memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8); 14871ba76586SYinghai Lu syscall_init(); 14881ba76586SYinghai Lu 14891ba76586SYinghai Lu wrmsrl(MSR_FS_BASE, 0); 14901ba76586SYinghai Lu wrmsrl(MSR_KERNEL_GS_BASE, 0); 14911ba76586SYinghai Lu barrier(); 14921ba76586SYinghai Lu 14934763ed4dSH. Peter Anvin x86_configure_nx(); 1494659006bfSThomas Gleixner x2apic_setup(); 14951ba76586SYinghai Lu 14961ba76586SYinghai Lu /* 14971ba76586SYinghai Lu * set up and load the per-CPU TSS 14981ba76586SYinghai Lu */ 14990fe1e009STejun Heo if (!oist->ist[0]) { 150092d65b23SBrian Gerst char *estacks = per_cpu(exception_stacks, cpu); 15010f3fa48aSIngo Molnar 15021ba76586SYinghai Lu for (v = 0; v < N_EXCEPTION_STACKS; v++) { 15030f3fa48aSIngo Molnar estacks += exception_stack_sizes[v]; 15040fe1e009STejun Heo oist->ist[v] = t->x86_tss.ist[v] = 15051ba76586SYinghai Lu (unsigned long)estacks; 1506228bdaa9SSteven Rostedt if (v == DEBUG_STACK-1) 1507228bdaa9SSteven Rostedt per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks; 15081ba76586SYinghai Lu } 15091ba76586SYinghai Lu } 15101ba76586SYinghai Lu 15111ba76586SYinghai Lu t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap); 15120f3fa48aSIngo Molnar 15131ba76586SYinghai Lu /* 15141ba76586SYinghai Lu * <= is required because the CPU will access up to 15151ba76586SYinghai Lu * 8 bits beyond the end of the IO permission bitmap. 15161ba76586SYinghai Lu */ 15171ba76586SYinghai Lu for (i = 0; i <= IO_BITMAP_LONGS; i++) 15181ba76586SYinghai Lu t->io_bitmap[i] = ~0UL; 15191ba76586SYinghai Lu 15201ba76586SYinghai Lu atomic_inc(&init_mm.mm_count); 15211ba76586SYinghai Lu me->active_mm = &init_mm; 15228c5dfd25SStoyan Gaydarov BUG_ON(me->mm); 15231ba76586SYinghai Lu enter_lazy_tlb(&init_mm, me); 15241ba76586SYinghai Lu 15251ba76586SYinghai Lu load_sp0(t, ¤t->thread); 15261ba76586SYinghai Lu set_tss_desc(cpu, t); 15271ba76586SYinghai Lu load_TR_desc(); 152837868fe1SAndy Lutomirski load_mm_ldt(&init_mm); 15291ba76586SYinghai Lu 15309766cdbcSJaswinder Singh Rajput clear_all_debug_regs(); 15310bb9fef9SJason Wessel dbg_restore_debug_regs(); 15321ba76586SYinghai Lu 153321c4cd10SIngo Molnar fpu__init_cpu(); 15341ba76586SYinghai Lu 15351ba76586SYinghai Lu if (is_uv_system()) 15361ba76586SYinghai Lu uv_cpu_init(); 15371ba76586SYinghai Lu } 15381ba76586SYinghai Lu 15391ba76586SYinghai Lu #else 15401ba76586SYinghai Lu 1541148f9bb8SPaul Gortmaker void cpu_init(void) 1542f7627e25SThomas Gleixner { 1543f7627e25SThomas Gleixner int cpu = smp_processor_id(); 1544f7627e25SThomas Gleixner struct task_struct *curr = current; 154524933b82SAndy Lutomirski struct tss_struct *t = &per_cpu(cpu_tss, cpu); 1546f7627e25SThomas Gleixner struct thread_struct *thread = &curr->thread; 1547f7627e25SThomas Gleixner 1548ce4b1b16SIgor Mammedov wait_for_master_cpu(cpu); 1549e6ebf5deSFenghua Yu 15505b2bdbc8SSteven Rostedt /* 15515b2bdbc8SSteven Rostedt * Initialize the CR4 shadow before doing anything that could 15525b2bdbc8SSteven Rostedt * try to read it. 15535b2bdbc8SSteven Rostedt */ 15545b2bdbc8SSteven Rostedt cr4_init_shadow(); 15555b2bdbc8SSteven Rostedt 1556ce4b1b16SIgor Mammedov show_ucode_info_early(); 1557f7627e25SThomas Gleixner 15581b74dde7SChen Yucong pr_info("Initializing CPU#%d\n", cpu); 1559f7627e25SThomas Gleixner 1560362f924bSBorislav Petkov if (cpu_feature_enabled(X86_FEATURE_VME) || 1561362f924bSBorislav Petkov cpu_has_tsc || 1562362f924bSBorislav Petkov boot_cpu_has(X86_FEATURE_DE)) 1563375074ccSAndy Lutomirski cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); 1564f7627e25SThomas Gleixner 1565cf910e83SSeiji Aguchi load_current_idt(); 1566552be871SBrian Gerst switch_to_new_gdt(cpu); 1567f7627e25SThomas Gleixner 1568f7627e25SThomas Gleixner /* 1569f7627e25SThomas Gleixner * Set up and load the per-CPU TSS and LDT 1570f7627e25SThomas Gleixner */ 1571f7627e25SThomas Gleixner atomic_inc(&init_mm.mm_count); 1572f7627e25SThomas Gleixner curr->active_mm = &init_mm; 15738c5dfd25SStoyan Gaydarov BUG_ON(curr->mm); 1574f7627e25SThomas Gleixner enter_lazy_tlb(&init_mm, curr); 1575f7627e25SThomas Gleixner 1576faca6227SH. Peter Anvin load_sp0(t, thread); 1577f7627e25SThomas Gleixner set_tss_desc(cpu, t); 1578f7627e25SThomas Gleixner load_TR_desc(); 157937868fe1SAndy Lutomirski load_mm_ldt(&init_mm); 1580f7627e25SThomas Gleixner 1581f9a196b8SThomas Gleixner t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap); 1582f9a196b8SThomas Gleixner 1583f7627e25SThomas Gleixner #ifdef CONFIG_DOUBLEFAULT 1584f7627e25SThomas Gleixner /* Set up doublefault TSS pointer in the GDT */ 1585f7627e25SThomas Gleixner __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss); 1586f7627e25SThomas Gleixner #endif 1587f7627e25SThomas Gleixner 15889766cdbcSJaswinder Singh Rajput clear_all_debug_regs(); 15890bb9fef9SJason Wessel dbg_restore_debug_regs(); 1590f7627e25SThomas Gleixner 159121c4cd10SIngo Molnar fpu__init_cpu(); 1592f7627e25SThomas Gleixner } 15931ba76586SYinghai Lu #endif 15945700f743SBorislav Petkov 1595b51ef52dSLaura Abbott static void bsp_resume(void) 1596b51ef52dSLaura Abbott { 1597b51ef52dSLaura Abbott if (this_cpu->c_bsp_resume) 1598b51ef52dSLaura Abbott this_cpu->c_bsp_resume(&boot_cpu_data); 1599b51ef52dSLaura Abbott } 1600b51ef52dSLaura Abbott 1601b51ef52dSLaura Abbott static struct syscore_ops cpu_syscore_ops = { 1602b51ef52dSLaura Abbott .resume = bsp_resume, 1603b51ef52dSLaura Abbott }; 1604b51ef52dSLaura Abbott 1605b51ef52dSLaura Abbott static int __init init_cpu_syscore(void) 1606b51ef52dSLaura Abbott { 1607b51ef52dSLaura Abbott register_syscore_ops(&cpu_syscore_ops); 1608b51ef52dSLaura Abbott return 0; 1609b51ef52dSLaura Abbott } 1610b51ef52dSLaura Abbott core_initcall(init_cpu_syscore); 1611