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> 1068e21be2SIngo Molnar #include <linux/sched/mm.h> 11e6017571SIngo Molnar #include <linux/sched/clock.h> 129164bb4aSIngo Molnar #include <linux/sched/task.h> 139766cdbcSJaswinder Singh Rajput #include <linux/init.h> 140f46efebSMasami Hiramatsu #include <linux/kprobes.h> 159766cdbcSJaswinder Singh Rajput #include <linux/kgdb.h> 169766cdbcSJaswinder Singh Rajput #include <linux/smp.h> 179766cdbcSJaswinder Singh Rajput #include <linux/io.h> 18b51ef52dSLaura Abbott #include <linux/syscore_ops.h> 199766cdbcSJaswinder Singh Rajput 209766cdbcSJaswinder Singh Rajput #include <asm/stackprotector.h> 21cdd6c482SIngo Molnar #include <asm/perf_event.h> 22f7627e25SThomas Gleixner #include <asm/mmu_context.h> 2349d859d7SH. Peter Anvin #include <asm/archrandom.h> 249766cdbcSJaswinder Singh Rajput #include <asm/hypervisor.h> 259766cdbcSJaswinder Singh Rajput #include <asm/processor.h> 261e02ce4cSAndy Lutomirski #include <asm/tlbflush.h> 27f649e938SPaul Gortmaker #include <asm/debugreg.h> 289766cdbcSJaswinder Singh Rajput #include <asm/sections.h> 29f40c3300SAndy Lutomirski #include <asm/vsyscall.h> 308bdbd962SAlan Cox #include <linux/topology.h> 318bdbd962SAlan Cox #include <linux/cpumask.h> 329766cdbcSJaswinder Singh Rajput #include <asm/pgtable.h> 3360063497SArun Sharma #include <linux/atomic.h> 349766cdbcSJaswinder Singh Rajput #include <asm/proto.h> 359766cdbcSJaswinder Singh Rajput #include <asm/setup.h> 36f7627e25SThomas Gleixner #include <asm/apic.h> 379766cdbcSJaswinder Singh Rajput #include <asm/desc.h> 3878f7f1e5SIngo Molnar #include <asm/fpu/internal.h> 399766cdbcSJaswinder Singh Rajput #include <asm/mtrr.h> 400274f955SGrzegorz Andrejczuk #include <asm/hwcap2.h> 418bdbd962SAlan Cox #include <linux/numa.h> 429766cdbcSJaswinder Singh Rajput #include <asm/asm.h> 430f6ff2bcSDave Hansen #include <asm/bugs.h> 449766cdbcSJaswinder Singh Rajput #include <asm/cpu.h> 459766cdbcSJaswinder Singh Rajput #include <asm/mce.h> 469766cdbcSJaswinder Singh Rajput #include <asm/msr.h> 479766cdbcSJaswinder Singh Rajput #include <asm/pat.h> 48d288e1cfSFenghua Yu #include <asm/microcode.h> 49d288e1cfSFenghua Yu #include <asm/microcode_intel.h> 50e641f5f5SIngo Molnar 51f7627e25SThomas Gleixner #ifdef CONFIG_X86_LOCAL_APIC 52bdbcdd48STejun Heo #include <asm/uv/uv.h> 53f7627e25SThomas Gleixner #endif 54f7627e25SThomas Gleixner 55f7627e25SThomas Gleixner #include "cpu.h" 56f7627e25SThomas Gleixner 570274f955SGrzegorz Andrejczuk u32 elf_hwcap2 __read_mostly; 580274f955SGrzegorz Andrejczuk 59c2d1cec1SMike Travis /* all of these masks are initialized in setup_cpu_local_masks() */ 60c2d1cec1SMike Travis cpumask_var_t cpu_initialized_mask; 619766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callout_mask; 629766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callin_mask; 63c2d1cec1SMike Travis 64c2d1cec1SMike Travis /* representing cpus for which sibling maps can be computed */ 65c2d1cec1SMike Travis cpumask_var_t cpu_sibling_setup_mask; 66c2d1cec1SMike Travis 672f2f52baSBrian Gerst /* correctly size the local cpu masks */ 684369f1fbSIngo Molnar void __init setup_cpu_local_masks(void) 692f2f52baSBrian Gerst { 702f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_initialized_mask); 712f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_callin_mask); 722f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_callout_mask); 732f2f52baSBrian Gerst alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask); 742f2f52baSBrian Gerst } 752f2f52baSBrian Gerst 76148f9bb8SPaul Gortmaker static void default_init(struct cpuinfo_x86 *c) 77e8055139SOndrej Zary { 78e8055139SOndrej Zary #ifdef CONFIG_X86_64 7927c13eceSBorislav Petkov cpu_detect_cache_sizes(c); 80e8055139SOndrej Zary #else 81e8055139SOndrej Zary /* Not much we can do here... */ 82e8055139SOndrej Zary /* Check if at least it has cpuid */ 83e8055139SOndrej Zary if (c->cpuid_level == -1) { 84e8055139SOndrej Zary /* No cpuid. It must be an ancient CPU */ 85e8055139SOndrej Zary if (c->x86 == 4) 86e8055139SOndrej Zary strcpy(c->x86_model_id, "486"); 87e8055139SOndrej Zary else if (c->x86 == 3) 88e8055139SOndrej Zary strcpy(c->x86_model_id, "386"); 89e8055139SOndrej Zary } 90e8055139SOndrej Zary #endif 91e8055139SOndrej Zary } 92e8055139SOndrej Zary 93148f9bb8SPaul Gortmaker static const struct cpu_dev default_cpu = { 94e8055139SOndrej Zary .c_init = default_init, 95e8055139SOndrej Zary .c_vendor = "Unknown", 96e8055139SOndrej Zary .c_x86_vendor = X86_VENDOR_UNKNOWN, 97e8055139SOndrej Zary }; 98e8055139SOndrej Zary 99148f9bb8SPaul Gortmaker static const struct cpu_dev *this_cpu = &default_cpu; 1000a488a53SYinghai Lu 10106deef89SBrian Gerst DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = { 102950ad7ffSYinghai Lu #ifdef CONFIG_X86_64 10306deef89SBrian Gerst /* 10406deef89SBrian Gerst * We need valid kernel segments for data and code in long mode too 105950ad7ffSYinghai Lu * IRET will check the segment types kkeil 2000/10/28 106950ad7ffSYinghai Lu * Also sysret mandates a special GDT layout 10706deef89SBrian Gerst * 1089766cdbcSJaswinder Singh Rajput * TLS descriptors are currently at a different place compared to i386. 10906deef89SBrian Gerst * Hopefully nobody expects them at a fixed place (Wine?) 110950ad7ffSYinghai Lu */ 1111e5de182SAkinobu Mita [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff), 1121e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff), 1131e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff), 1141e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff), 1151e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff), 1161e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff), 117950ad7ffSYinghai Lu #else 1181e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff), 1191e5de182SAkinobu Mita [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 1201e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff), 1211e5de182SAkinobu Mita [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff), 122f7627e25SThomas Gleixner /* 123f7627e25SThomas Gleixner * Segments used for calling PnP BIOS have byte granularity. 124f7627e25SThomas Gleixner * They code segments and data segments have fixed 64k limits, 125f7627e25SThomas Gleixner * the transfer segment sizes are set at run time. 126f7627e25SThomas Gleixner */ 1276842ef0eSGlauber de Oliveira Costa /* 32-bit code */ 1281e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), 1296842ef0eSGlauber de Oliveira Costa /* 16-bit code */ 1301e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), 1316842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 1321e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff), 1336842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 1341e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0), 1356842ef0eSGlauber de Oliveira Costa /* 16-bit data */ 1361e5de182SAkinobu Mita [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0), 137f7627e25SThomas Gleixner /* 138f7627e25SThomas Gleixner * The APM segments have byte granularity and their bases 139f7627e25SThomas Gleixner * are set at run time. All have 64k limits. 140f7627e25SThomas Gleixner */ 1416842ef0eSGlauber de Oliveira Costa /* 32-bit code */ 1421e5de182SAkinobu Mita [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), 143f7627e25SThomas Gleixner /* 16-bit code */ 1441e5de182SAkinobu Mita [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), 1456842ef0eSGlauber de Oliveira Costa /* data */ 14672c4d853SIngo Molnar [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff), 147f7627e25SThomas Gleixner 1481e5de182SAkinobu Mita [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 1491e5de182SAkinobu Mita [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), 15060a5317fSTejun Heo GDT_STACK_CANARY_INIT 151950ad7ffSYinghai Lu #endif 15206deef89SBrian Gerst } }; 153f7627e25SThomas Gleixner EXPORT_PER_CPU_SYMBOL_GPL(gdt_page); 154f7627e25SThomas Gleixner 1558c3641e9SDave Hansen static int __init x86_mpx_setup(char *s) 1560c752a93SSuresh Siddha { 1578c3641e9SDave Hansen /* require an exact match without trailing characters */ 1582cd3949fSDave Hansen if (strlen(s)) 1592cd3949fSDave Hansen return 0; 1600c752a93SSuresh Siddha 1618c3641e9SDave Hansen /* do not emit a message if the feature is not present */ 1628c3641e9SDave Hansen if (!boot_cpu_has(X86_FEATURE_MPX)) 1636bad06b7SSuresh Siddha return 1; 1646bad06b7SSuresh Siddha 1658c3641e9SDave Hansen setup_clear_cpu_cap(X86_FEATURE_MPX); 1668c3641e9SDave Hansen pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n"); 167b6f42a4aSFenghua Yu return 1; 168b6f42a4aSFenghua Yu } 1698c3641e9SDave Hansen __setup("nompx", x86_mpx_setup); 170b6f42a4aSFenghua Yu 1710790c9aaSAndy Lutomirski #ifdef CONFIG_X86_64 1720790c9aaSAndy Lutomirski static int __init x86_pcid_setup(char *s) 1730790c9aaSAndy Lutomirski { 1740790c9aaSAndy Lutomirski /* require an exact match without trailing characters */ 1750790c9aaSAndy Lutomirski if (strlen(s)) 1760790c9aaSAndy Lutomirski return 0; 1770790c9aaSAndy Lutomirski 1780790c9aaSAndy Lutomirski /* do not emit a message if the feature is not present */ 1790790c9aaSAndy Lutomirski if (!boot_cpu_has(X86_FEATURE_PCID)) 1800790c9aaSAndy Lutomirski return 1; 1810790c9aaSAndy Lutomirski 1820790c9aaSAndy Lutomirski setup_clear_cpu_cap(X86_FEATURE_PCID); 1830790c9aaSAndy Lutomirski pr_info("nopcid: PCID feature disabled\n"); 1840790c9aaSAndy Lutomirski return 1; 1850790c9aaSAndy Lutomirski } 1860790c9aaSAndy Lutomirski __setup("nopcid", x86_pcid_setup); 1870790c9aaSAndy Lutomirski #endif 1880790c9aaSAndy Lutomirski 189d12a72b8SAndy Lutomirski static int __init x86_noinvpcid_setup(char *s) 190d12a72b8SAndy Lutomirski { 191d12a72b8SAndy Lutomirski /* noinvpcid doesn't accept parameters */ 192d12a72b8SAndy Lutomirski if (s) 193d12a72b8SAndy Lutomirski return -EINVAL; 194d12a72b8SAndy Lutomirski 195d12a72b8SAndy Lutomirski /* do not emit a message if the feature is not present */ 196d12a72b8SAndy Lutomirski if (!boot_cpu_has(X86_FEATURE_INVPCID)) 197d12a72b8SAndy Lutomirski return 0; 198d12a72b8SAndy Lutomirski 199d12a72b8SAndy Lutomirski setup_clear_cpu_cap(X86_FEATURE_INVPCID); 200d12a72b8SAndy Lutomirski pr_info("noinvpcid: INVPCID feature disabled\n"); 201d12a72b8SAndy Lutomirski return 0; 202d12a72b8SAndy Lutomirski } 203d12a72b8SAndy Lutomirski early_param("noinvpcid", x86_noinvpcid_setup); 204d12a72b8SAndy Lutomirski 205ba51dcedSYinghai Lu #ifdef CONFIG_X86_32 206148f9bb8SPaul Gortmaker static int cachesize_override = -1; 207148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1; 208f7627e25SThomas Gleixner 209f7627e25SThomas Gleixner static int __init cachesize_setup(char *str) 210f7627e25SThomas Gleixner { 211f7627e25SThomas Gleixner get_option(&str, &cachesize_override); 212f7627e25SThomas Gleixner return 1; 213f7627e25SThomas Gleixner } 214f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup); 215f7627e25SThomas Gleixner 216f7627e25SThomas Gleixner static int __init x86_sep_setup(char *s) 217f7627e25SThomas Gleixner { 21813530257SAndi Kleen setup_clear_cpu_cap(X86_FEATURE_SEP); 219f7627e25SThomas Gleixner return 1; 220f7627e25SThomas Gleixner } 221f7627e25SThomas Gleixner __setup("nosep", x86_sep_setup); 222f7627e25SThomas Gleixner 223f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */ 224f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag) 225f7627e25SThomas Gleixner { 226f7627e25SThomas Gleixner u32 f1, f2; 227f7627e25SThomas Gleixner 22894f6bac1SKrzysztof Helt /* 22994f6bac1SKrzysztof Helt * Cyrix and IDT cpus allow disabling of CPUID 23094f6bac1SKrzysztof Helt * so the code below may return different results 23194f6bac1SKrzysztof Helt * when it is executed before and after enabling 23294f6bac1SKrzysztof Helt * the CPUID. Add "volatile" to not allow gcc to 23394f6bac1SKrzysztof Helt * optimize the subsequent calls to this function. 23494f6bac1SKrzysztof Helt */ 23594f6bac1SKrzysztof Helt asm volatile ("pushfl \n\t" 236f7627e25SThomas Gleixner "pushfl \n\t" 237f7627e25SThomas Gleixner "popl %0 \n\t" 238f7627e25SThomas Gleixner "movl %0, %1 \n\t" 239f7627e25SThomas Gleixner "xorl %2, %0 \n\t" 240f7627e25SThomas Gleixner "pushl %0 \n\t" 241f7627e25SThomas Gleixner "popfl \n\t" 242f7627e25SThomas Gleixner "pushfl \n\t" 243f7627e25SThomas Gleixner "popl %0 \n\t" 244f7627e25SThomas Gleixner "popfl \n\t" 2450f3fa48aSIngo Molnar 246f7627e25SThomas Gleixner : "=&r" (f1), "=&r" (f2) 247f7627e25SThomas Gleixner : "ir" (flag)); 248f7627e25SThomas Gleixner 249f7627e25SThomas Gleixner return ((f1^f2) & flag) != 0; 250f7627e25SThomas Gleixner } 251f7627e25SThomas Gleixner 252f7627e25SThomas Gleixner /* Probe for the CPUID instruction */ 253148f9bb8SPaul Gortmaker int have_cpuid_p(void) 254f7627e25SThomas Gleixner { 255f7627e25SThomas Gleixner return flag_is_changeable_p(X86_EFLAGS_ID); 256f7627e25SThomas Gleixner } 257f7627e25SThomas Gleixner 258148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) 2590a488a53SYinghai Lu { 2600a488a53SYinghai Lu unsigned long lo, hi; 2610f3fa48aSIngo Molnar 2620f3fa48aSIngo Molnar if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr) 2630f3fa48aSIngo Molnar return; 2640f3fa48aSIngo Molnar 2650f3fa48aSIngo Molnar /* Disable processor serial number: */ 2660f3fa48aSIngo Molnar 2670a488a53SYinghai Lu rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi); 2680a488a53SYinghai Lu lo |= 0x200000; 2690a488a53SYinghai Lu wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi); 2700f3fa48aSIngo Molnar 2711b74dde7SChen Yucong pr_notice("CPU serial number disabled.\n"); 2720a488a53SYinghai Lu clear_cpu_cap(c, X86_FEATURE_PN); 2730a488a53SYinghai Lu 2740a488a53SYinghai Lu /* Disabling the serial number may affect the cpuid level */ 2750a488a53SYinghai Lu c->cpuid_level = cpuid_eax(0); 2760a488a53SYinghai Lu } 2770a488a53SYinghai Lu 2780a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s) 2790a488a53SYinghai Lu { 2800a488a53SYinghai Lu disable_x86_serial_nr = 0; 2810a488a53SYinghai Lu return 1; 2820a488a53SYinghai Lu } 2830a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup); 284ba51dcedSYinghai Lu #else 285102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag) 286102bbe3aSYinghai Lu { 287102bbe3aSYinghai Lu return 1; 288102bbe3aSYinghai Lu } 289102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) 290102bbe3aSYinghai Lu { 291102bbe3aSYinghai Lu } 292ba51dcedSYinghai Lu #endif 2930a488a53SYinghai Lu 294de5397adSFenghua Yu static __init int setup_disable_smep(char *arg) 295de5397adSFenghua Yu { 296b2cc2a07SH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_SMEP); 2970f6ff2bcSDave Hansen /* Check for things that depend on SMEP being enabled: */ 2980f6ff2bcSDave Hansen check_mpx_erratum(&boot_cpu_data); 299de5397adSFenghua Yu return 1; 300de5397adSFenghua Yu } 301de5397adSFenghua Yu __setup("nosmep", setup_disable_smep); 302de5397adSFenghua Yu 303b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c) 304de5397adSFenghua Yu { 305b2cc2a07SH. Peter Anvin if (cpu_has(c, X86_FEATURE_SMEP)) 306375074ccSAndy Lutomirski cr4_set_bits(X86_CR4_SMEP); 307de5397adSFenghua Yu } 308de5397adSFenghua Yu 30952b6179aSH. Peter Anvin static __init int setup_disable_smap(char *arg) 31052b6179aSH. Peter Anvin { 311b2cc2a07SH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_SMAP); 31252b6179aSH. Peter Anvin return 1; 31352b6179aSH. Peter Anvin } 31452b6179aSH. Peter Anvin __setup("nosmap", setup_disable_smap); 31552b6179aSH. Peter Anvin 316b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c) 31752b6179aSH. Peter Anvin { 318581b7f15SAndrew Cooper unsigned long eflags = native_save_fl(); 319b2cc2a07SH. Peter Anvin 320b2cc2a07SH. Peter Anvin /* This should have been cleared long ago */ 321b2cc2a07SH. Peter Anvin BUG_ON(eflags & X86_EFLAGS_AC); 322b2cc2a07SH. Peter Anvin 32303bbd596SH. Peter Anvin if (cpu_has(c, X86_FEATURE_SMAP)) { 32403bbd596SH. Peter Anvin #ifdef CONFIG_X86_SMAP 325375074ccSAndy Lutomirski cr4_set_bits(X86_CR4_SMAP); 32603bbd596SH. Peter Anvin #else 327375074ccSAndy Lutomirski cr4_clear_bits(X86_CR4_SMAP); 32803bbd596SH. Peter Anvin #endif 32903bbd596SH. Peter Anvin } 330f7627e25SThomas Gleixner } 331f7627e25SThomas Gleixner 332660da7c9SAndy Lutomirski static void setup_pcid(struct cpuinfo_x86 *c) 333660da7c9SAndy Lutomirski { 334660da7c9SAndy Lutomirski if (cpu_has(c, X86_FEATURE_PCID)) { 335660da7c9SAndy Lutomirski if (cpu_has(c, X86_FEATURE_PGE)) { 336660da7c9SAndy Lutomirski cr4_set_bits(X86_CR4_PCIDE); 337660da7c9SAndy Lutomirski } else { 338660da7c9SAndy Lutomirski /* 339660da7c9SAndy Lutomirski * flush_tlb_all(), as currently implemented, won't 340660da7c9SAndy Lutomirski * work if PCID is on but PGE is not. Since that 341660da7c9SAndy Lutomirski * combination doesn't exist on real hardware, there's 342660da7c9SAndy Lutomirski * no reason to try to fully support it, but it's 343660da7c9SAndy Lutomirski * polite to avoid corrupting data if we're on 344660da7c9SAndy Lutomirski * an improperly configured VM. 345660da7c9SAndy Lutomirski */ 346660da7c9SAndy Lutomirski clear_cpu_cap(c, X86_FEATURE_PCID); 347660da7c9SAndy Lutomirski } 348660da7c9SAndy Lutomirski } 349660da7c9SAndy Lutomirski } 350660da7c9SAndy Lutomirski 351f7627e25SThomas Gleixner /* 35206976945SDave Hansen * Protection Keys are not available in 32-bit mode. 35306976945SDave Hansen */ 35406976945SDave Hansen static bool pku_disabled; 35506976945SDave Hansen 35606976945SDave Hansen static __always_inline void setup_pku(struct cpuinfo_x86 *c) 35706976945SDave Hansen { 358e8df1a95SDave Hansen /* check the boot processor, plus compile options for PKU: */ 359e8df1a95SDave Hansen if (!cpu_feature_enabled(X86_FEATURE_PKU)) 360e8df1a95SDave Hansen return; 361e8df1a95SDave Hansen /* checks the actual processor's cpuid bits: */ 36206976945SDave Hansen if (!cpu_has(c, X86_FEATURE_PKU)) 36306976945SDave Hansen return; 36406976945SDave Hansen if (pku_disabled) 36506976945SDave Hansen return; 36606976945SDave Hansen 36706976945SDave Hansen cr4_set_bits(X86_CR4_PKE); 36806976945SDave Hansen /* 36906976945SDave Hansen * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE 37006976945SDave Hansen * cpuid bit to be set. We need to ensure that we 37106976945SDave Hansen * update that bit in this CPU's "cpu_info". 37206976945SDave Hansen */ 37306976945SDave Hansen get_cpu_cap(c); 37406976945SDave Hansen } 37506976945SDave Hansen 37606976945SDave Hansen #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS 37706976945SDave Hansen static __init int setup_disable_pku(char *arg) 37806976945SDave Hansen { 37906976945SDave Hansen /* 38006976945SDave Hansen * Do not clear the X86_FEATURE_PKU bit. All of the 38106976945SDave Hansen * runtime checks are against OSPKE so clearing the 38206976945SDave Hansen * bit does nothing. 38306976945SDave Hansen * 38406976945SDave Hansen * This way, we will see "pku" in cpuinfo, but not 38506976945SDave Hansen * "ospke", which is exactly what we want. It shows 38606976945SDave Hansen * that the CPU has PKU, but the OS has not enabled it. 38706976945SDave Hansen * This happens to be exactly how a system would look 38806976945SDave Hansen * if we disabled the config option. 38906976945SDave Hansen */ 39006976945SDave Hansen pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n"); 39106976945SDave Hansen pku_disabled = true; 39206976945SDave Hansen return 1; 39306976945SDave Hansen } 39406976945SDave Hansen __setup("nopku", setup_disable_pku); 39506976945SDave Hansen #endif /* CONFIG_X86_64 */ 39606976945SDave Hansen 39706976945SDave Hansen /* 398b38b0665SH. Peter Anvin * Some CPU features depend on higher CPUID levels, which may not always 399b38b0665SH. Peter Anvin * be available due to CPUID level capping or broken virtualization 400b38b0665SH. Peter Anvin * software. Add those features to this table to auto-disable them. 401b38b0665SH. Peter Anvin */ 402b38b0665SH. Peter Anvin struct cpuid_dependent_feature { 403b38b0665SH. Peter Anvin u32 feature; 404b38b0665SH. Peter Anvin u32 level; 405b38b0665SH. Peter Anvin }; 4060f3fa48aSIngo Molnar 407148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature 408b38b0665SH. Peter Anvin cpuid_dependent_features[] = { 409b38b0665SH. Peter Anvin { X86_FEATURE_MWAIT, 0x00000005 }, 410b38b0665SH. Peter Anvin { X86_FEATURE_DCA, 0x00000009 }, 411b38b0665SH. Peter Anvin { X86_FEATURE_XSAVE, 0x0000000d }, 412b38b0665SH. Peter Anvin { 0, 0 } 413b38b0665SH. Peter Anvin }; 414b38b0665SH. Peter Anvin 415148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) 416b38b0665SH. Peter Anvin { 417b38b0665SH. Peter Anvin const struct cpuid_dependent_feature *df; 4189766cdbcSJaswinder Singh Rajput 419b38b0665SH. Peter Anvin for (df = cpuid_dependent_features; df->feature; df++) { 4200f3fa48aSIngo Molnar 4210f3fa48aSIngo Molnar if (!cpu_has(c, df->feature)) 4220f3fa48aSIngo Molnar continue; 423b38b0665SH. Peter Anvin /* 424b38b0665SH. Peter Anvin * Note: cpuid_level is set to -1 if unavailable, but 425b38b0665SH. Peter Anvin * extended_extended_level is set to 0 if unavailable 426b38b0665SH. Peter Anvin * and the legitimate extended levels are all negative 427b38b0665SH. Peter Anvin * when signed; hence the weird messing around with 428b38b0665SH. Peter Anvin * signs here... 429b38b0665SH. Peter Anvin */ 4300f3fa48aSIngo Molnar if (!((s32)df->level < 0 ? 431f6db44dfSYinghai Lu (u32)df->level > (u32)c->extended_cpuid_level : 4320f3fa48aSIngo Molnar (s32)df->level > (s32)c->cpuid_level)) 4330f3fa48aSIngo Molnar continue; 4340f3fa48aSIngo Molnar 435b38b0665SH. Peter Anvin clear_cpu_cap(c, df->feature); 4360f3fa48aSIngo Molnar if (!warn) 4370f3fa48aSIngo Molnar continue; 4380f3fa48aSIngo Molnar 4391b74dde7SChen Yucong pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n", 4409def39beSJosh Triplett x86_cap_flag(df->feature), df->level); 441b38b0665SH. Peter Anvin } 442b38b0665SH. Peter Anvin } 443b38b0665SH. Peter Anvin 444b38b0665SH. Peter Anvin /* 445f7627e25SThomas Gleixner * Naming convention should be: <Name> [(<Codename>)] 446f7627e25SThomas Gleixner * This table only is used unless init_<vendor>() below doesn't set it; 4470f3fa48aSIngo Molnar * in particular, if CPUID levels 0x80000002..4 are supported, this 4480f3fa48aSIngo Molnar * isn't used 449f7627e25SThomas Gleixner */ 450f7627e25SThomas Gleixner 451f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */ 452148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c) 453f7627e25SThomas Gleixner { 45409dc68d9SJan Beulich #ifdef CONFIG_X86_32 45509dc68d9SJan Beulich const struct legacy_cpu_model_info *info; 456f7627e25SThomas Gleixner 457f7627e25SThomas Gleixner if (c->x86_model >= 16) 458f7627e25SThomas Gleixner return NULL; /* Range check */ 459f7627e25SThomas Gleixner 460f7627e25SThomas Gleixner if (!this_cpu) 461f7627e25SThomas Gleixner return NULL; 462f7627e25SThomas Gleixner 46309dc68d9SJan Beulich info = this_cpu->legacy_models; 464f7627e25SThomas Gleixner 46509dc68d9SJan Beulich while (info->family) { 466f7627e25SThomas Gleixner if (info->family == c->x86) 467f7627e25SThomas Gleixner return info->model_names[c->x86_model]; 468f7627e25SThomas Gleixner info++; 469f7627e25SThomas Gleixner } 47009dc68d9SJan Beulich #endif 471f7627e25SThomas Gleixner return NULL; /* Not found */ 472f7627e25SThomas Gleixner } 473f7627e25SThomas Gleixner 474148f9bb8SPaul Gortmaker __u32 cpu_caps_cleared[NCAPINTS]; 475148f9bb8SPaul Gortmaker __u32 cpu_caps_set[NCAPINTS]; 476f7627e25SThomas Gleixner 47711e3a840SJeremy Fitzhardinge void load_percpu_segment(int cpu) 4789d31d35bSYinghai Lu { 479fab334c1SYinghai Lu #ifdef CONFIG_X86_32 4802697fbd5SBrian Gerst loadsegment(fs, __KERNEL_PERCPU); 4812697fbd5SBrian Gerst #else 48245e876f7SAndy Lutomirski __loadsegment_simple(gs, 0); 4832697fbd5SBrian Gerst wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu)); 484fab334c1SYinghai Lu #endif 48560a5317fSTejun Heo load_stack_canary_segment(); 4869d31d35bSYinghai Lu } 4879d31d35bSYinghai Lu 48869218e47SThomas Garnier /* Setup the fixmap mapping only once per-processor */ 48969218e47SThomas Garnier static inline void setup_fixmap_gdt(int cpu) 49069218e47SThomas Garnier { 491b23adb7dSAndy Lutomirski #ifdef CONFIG_X86_64 492b23adb7dSAndy Lutomirski /* On 64-bit systems, we use a read-only fixmap GDT. */ 493b23adb7dSAndy Lutomirski pgprot_t prot = PAGE_KERNEL_RO; 494b23adb7dSAndy Lutomirski #else 495b23adb7dSAndy Lutomirski /* 496b23adb7dSAndy Lutomirski * On native 32-bit systems, the GDT cannot be read-only because 497b23adb7dSAndy Lutomirski * our double fault handler uses a task gate, and entering through 498b23adb7dSAndy Lutomirski * a task gate needs to change an available TSS to busy. If the GDT 499b23adb7dSAndy Lutomirski * is read-only, that will triple fault. 500b23adb7dSAndy Lutomirski * 501b23adb7dSAndy Lutomirski * On Xen PV, the GDT must be read-only because the hypervisor requires 502b23adb7dSAndy Lutomirski * it. 503b23adb7dSAndy Lutomirski */ 504b23adb7dSAndy Lutomirski pgprot_t prot = boot_cpu_has(X86_FEATURE_XENPV) ? 505b23adb7dSAndy Lutomirski PAGE_KERNEL_RO : PAGE_KERNEL; 506b23adb7dSAndy Lutomirski #endif 507b23adb7dSAndy Lutomirski 508b23adb7dSAndy Lutomirski __set_fixmap(get_cpu_gdt_ro_index(cpu), get_cpu_gdt_paddr(cpu), prot); 50969218e47SThomas Garnier } 51069218e47SThomas Garnier 51145fc8757SThomas Garnier /* Load the original GDT from the per-cpu structure */ 51245fc8757SThomas Garnier void load_direct_gdt(int cpu) 51345fc8757SThomas Garnier { 51445fc8757SThomas Garnier struct desc_ptr gdt_descr; 51545fc8757SThomas Garnier 51645fc8757SThomas Garnier gdt_descr.address = (long)get_cpu_gdt_rw(cpu); 51745fc8757SThomas Garnier gdt_descr.size = GDT_SIZE - 1; 51845fc8757SThomas Garnier load_gdt(&gdt_descr); 51945fc8757SThomas Garnier } 52045fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_direct_gdt); 52145fc8757SThomas Garnier 52269218e47SThomas Garnier /* Load a fixmap remapping of the per-cpu GDT */ 52369218e47SThomas Garnier void load_fixmap_gdt(int cpu) 52469218e47SThomas Garnier { 52569218e47SThomas Garnier struct desc_ptr gdt_descr; 52669218e47SThomas Garnier 52769218e47SThomas Garnier gdt_descr.address = (long)get_cpu_gdt_ro(cpu); 52869218e47SThomas Garnier gdt_descr.size = GDT_SIZE - 1; 52969218e47SThomas Garnier load_gdt(&gdt_descr); 53069218e47SThomas Garnier } 53145fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_fixmap_gdt); 53269218e47SThomas Garnier 5330f3fa48aSIngo Molnar /* 5340f3fa48aSIngo Molnar * Current gdt points %fs at the "master" per-cpu area: after this, 5350f3fa48aSIngo Molnar * it's on the real one. 5360f3fa48aSIngo Molnar */ 537552be871SBrian Gerst void switch_to_new_gdt(int cpu) 538f7627e25SThomas Gleixner { 53945fc8757SThomas Garnier /* Load the original GDT */ 54045fc8757SThomas Garnier load_direct_gdt(cpu); 541f7627e25SThomas Gleixner /* Reload the per-cpu base */ 54211e3a840SJeremy Fitzhardinge load_percpu_segment(cpu); 543f7627e25SThomas Gleixner } 544f7627e25SThomas Gleixner 545148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {}; 546f7627e25SThomas Gleixner 547148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c) 548f7627e25SThomas Gleixner { 549f7627e25SThomas Gleixner unsigned int *v; 550ee098e1aSBorislav Petkov char *p, *q, *s; 551f7627e25SThomas Gleixner 5523da99c97SYinghai Lu if (c->extended_cpuid_level < 0x80000004) 5531b05d60dSYinghai Lu return; 554f7627e25SThomas Gleixner 555f7627e25SThomas Gleixner v = (unsigned int *)c->x86_model_id; 556f7627e25SThomas Gleixner cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]); 557f7627e25SThomas Gleixner cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]); 558f7627e25SThomas Gleixner cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]); 559f7627e25SThomas Gleixner c->x86_model_id[48] = 0; 560f7627e25SThomas Gleixner 561ee098e1aSBorislav Petkov /* Trim whitespace */ 562ee098e1aSBorislav Petkov p = q = s = &c->x86_model_id[0]; 563ee098e1aSBorislav Petkov 564ee098e1aSBorislav Petkov while (*p == ' ') 565ee098e1aSBorislav Petkov p++; 566ee098e1aSBorislav Petkov 567ee098e1aSBorislav Petkov while (*p) { 568ee098e1aSBorislav Petkov /* Note the last non-whitespace index */ 569ee098e1aSBorislav Petkov if (!isspace(*p)) 570ee098e1aSBorislav Petkov s = q; 571ee098e1aSBorislav Petkov 572ee098e1aSBorislav Petkov *q++ = *p++; 573ee098e1aSBorislav Petkov } 574ee098e1aSBorislav Petkov 575ee098e1aSBorislav Petkov *(s + 1) = '\0'; 576f7627e25SThomas Gleixner } 577f7627e25SThomas Gleixner 578148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c) 579f7627e25SThomas Gleixner { 5809d31d35bSYinghai Lu unsigned int n, dummy, ebx, ecx, edx, l2size; 581f7627e25SThomas Gleixner 5823da99c97SYinghai Lu n = c->extended_cpuid_level; 583f7627e25SThomas Gleixner 584f7627e25SThomas Gleixner if (n >= 0x80000005) { 5859d31d35bSYinghai Lu cpuid(0x80000005, &dummy, &ebx, &ecx, &edx); 586f7627e25SThomas Gleixner c->x86_cache_size = (ecx>>24) + (edx>>24); 587140fc727SYinghai Lu #ifdef CONFIG_X86_64 588140fc727SYinghai Lu /* On K8 L1 TLB is inclusive, so don't count it */ 589140fc727SYinghai Lu c->x86_tlbsize = 0; 590140fc727SYinghai Lu #endif 591f7627e25SThomas Gleixner } 592f7627e25SThomas Gleixner 593f7627e25SThomas Gleixner if (n < 0x80000006) /* Some chips just has a large L1. */ 594f7627e25SThomas Gleixner return; 595f7627e25SThomas Gleixner 5960a488a53SYinghai Lu cpuid(0x80000006, &dummy, &ebx, &ecx, &edx); 597f7627e25SThomas Gleixner l2size = ecx >> 16; 598f7627e25SThomas Gleixner 599140fc727SYinghai Lu #ifdef CONFIG_X86_64 600140fc727SYinghai Lu c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff); 601140fc727SYinghai Lu #else 602f7627e25SThomas Gleixner /* do processor-specific cache resizing */ 60309dc68d9SJan Beulich if (this_cpu->legacy_cache_size) 60409dc68d9SJan Beulich l2size = this_cpu->legacy_cache_size(c, l2size); 605f7627e25SThomas Gleixner 606f7627e25SThomas Gleixner /* Allow user to override all this if necessary. */ 607f7627e25SThomas Gleixner if (cachesize_override != -1) 608f7627e25SThomas Gleixner l2size = cachesize_override; 609f7627e25SThomas Gleixner 610f7627e25SThomas Gleixner if (l2size == 0) 611f7627e25SThomas Gleixner return; /* Again, no L2 cache is possible */ 612140fc727SYinghai Lu #endif 613f7627e25SThomas Gleixner 614f7627e25SThomas Gleixner c->x86_cache_size = l2size; 615f7627e25SThomas Gleixner } 616f7627e25SThomas Gleixner 617e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO]; 618e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO]; 619e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO]; 620e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO]; 621e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO]; 622e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO]; 623dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO]; 624e0ba94f1SAlex Shi 625f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c) 626e0ba94f1SAlex Shi { 627e0ba94f1SAlex Shi if (this_cpu->c_detect_tlb) 628e0ba94f1SAlex Shi this_cpu->c_detect_tlb(c); 629e0ba94f1SAlex Shi 630f94fe119SSteven Honeyman pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n", 631e0ba94f1SAlex Shi tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES], 632f94fe119SSteven Honeyman tlb_lli_4m[ENTRIES]); 633f94fe119SSteven Honeyman 634f94fe119SSteven Honeyman pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n", 635f94fe119SSteven Honeyman tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES], 636f94fe119SSteven Honeyman tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]); 637e0ba94f1SAlex Shi } 638e0ba94f1SAlex Shi 639148f9bb8SPaul Gortmaker void detect_ht(struct cpuinfo_x86 *c) 6409d31d35bSYinghai Lu { 641c8e56d20SBorislav Petkov #ifdef CONFIG_SMP 6429d31d35bSYinghai Lu u32 eax, ebx, ecx, edx; 6439d31d35bSYinghai Lu int index_msb, core_bits; 6442eaad1fdSMike Travis static bool printed; 6459d31d35bSYinghai Lu 6460a488a53SYinghai Lu if (!cpu_has(c, X86_FEATURE_HT)) 6479d31d35bSYinghai Lu return; 6489d31d35bSYinghai Lu 6490a488a53SYinghai Lu if (cpu_has(c, X86_FEATURE_CMP_LEGACY)) 6500a488a53SYinghai Lu goto out; 6510a488a53SYinghai Lu 6521cd78776SYinghai Lu if (cpu_has(c, X86_FEATURE_XTOPOLOGY)) 6531cd78776SYinghai Lu return; 6541cd78776SYinghai Lu 6550a488a53SYinghai Lu cpuid(1, &eax, &ebx, &ecx, &edx); 6560a488a53SYinghai Lu 6579d31d35bSYinghai Lu smp_num_siblings = (ebx & 0xff0000) >> 16; 6589d31d35bSYinghai Lu 6599d31d35bSYinghai Lu if (smp_num_siblings == 1) { 6601b74dde7SChen Yucong pr_info_once("CPU0: Hyper-Threading is disabled\n"); 6610f3fa48aSIngo Molnar goto out; 6620f3fa48aSIngo Molnar } 6630f3fa48aSIngo Molnar 6640f3fa48aSIngo Molnar if (smp_num_siblings <= 1) 6650f3fa48aSIngo Molnar goto out; 6669d31d35bSYinghai Lu 6679d31d35bSYinghai Lu index_msb = get_count_order(smp_num_siblings); 668cb8cc442SIngo Molnar c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb); 6699d31d35bSYinghai Lu 6709d31d35bSYinghai Lu smp_num_siblings = smp_num_siblings / c->x86_max_cores; 6719d31d35bSYinghai Lu 6729d31d35bSYinghai Lu index_msb = get_count_order(smp_num_siblings); 6739d31d35bSYinghai Lu 6749d31d35bSYinghai Lu core_bits = get_count_order(c->x86_max_cores); 6759d31d35bSYinghai Lu 676cb8cc442SIngo Molnar c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) & 6771cd78776SYinghai Lu ((1 << core_bits) - 1); 6789d31d35bSYinghai Lu 6790a488a53SYinghai Lu out: 6802eaad1fdSMike Travis if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) { 6811b74dde7SChen Yucong pr_info("CPU: Physical Processor ID: %d\n", 6820a488a53SYinghai Lu c->phys_proc_id); 6831b74dde7SChen Yucong pr_info("CPU: Processor Core ID: %d\n", 6849d31d35bSYinghai Lu c->cpu_core_id); 6852eaad1fdSMike Travis printed = 1; 6869d31d35bSYinghai Lu } 6879d31d35bSYinghai Lu #endif 68897e4db7cSYinghai Lu } 689f7627e25SThomas Gleixner 690148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c) 691f7627e25SThomas Gleixner { 692f7627e25SThomas Gleixner char *v = c->x86_vendor_id; 6930f3fa48aSIngo Molnar int i; 694f7627e25SThomas Gleixner 695f7627e25SThomas Gleixner for (i = 0; i < X86_VENDOR_NUM; i++) { 69610a434fcSYinghai Lu if (!cpu_devs[i]) 69710a434fcSYinghai Lu break; 69810a434fcSYinghai Lu 699f7627e25SThomas Gleixner if (!strcmp(v, cpu_devs[i]->c_ident[0]) || 700f7627e25SThomas Gleixner (cpu_devs[i]->c_ident[1] && 701f7627e25SThomas Gleixner !strcmp(v, cpu_devs[i]->c_ident[1]))) { 7020f3fa48aSIngo Molnar 703f7627e25SThomas Gleixner this_cpu = cpu_devs[i]; 70410a434fcSYinghai Lu c->x86_vendor = this_cpu->c_x86_vendor; 705f7627e25SThomas Gleixner return; 706f7627e25SThomas Gleixner } 707f7627e25SThomas Gleixner } 70810a434fcSYinghai Lu 7091b74dde7SChen Yucong pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \ 710a9c56953SMinchan Kim "CPU: Your system may be unstable.\n", v); 71110a434fcSYinghai Lu 712f7627e25SThomas Gleixner c->x86_vendor = X86_VENDOR_UNKNOWN; 713f7627e25SThomas Gleixner this_cpu = &default_cpu; 714f7627e25SThomas Gleixner } 715f7627e25SThomas Gleixner 716148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c) 717f7627e25SThomas Gleixner { 718f7627e25SThomas Gleixner /* Get vendor name */ 7194a148513SHarvey Harrison cpuid(0x00000000, (unsigned int *)&c->cpuid_level, 7204a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[0], 7214a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[8], 7224a148513SHarvey Harrison (unsigned int *)&c->x86_vendor_id[4]); 723f7627e25SThomas Gleixner 724f7627e25SThomas Gleixner c->x86 = 4; 7259d31d35bSYinghai Lu /* Intel-defined flags: level 0x00000001 */ 726f7627e25SThomas Gleixner if (c->cpuid_level >= 0x00000001) { 727f7627e25SThomas Gleixner u32 junk, tfms, cap0, misc; 7280f3fa48aSIngo Molnar 729f7627e25SThomas Gleixner cpuid(0x00000001, &tfms, &misc, &junk, &cap0); 73099f925ceSBorislav Petkov c->x86 = x86_family(tfms); 73199f925ceSBorislav Petkov c->x86_model = x86_model(tfms); 73299f925ceSBorislav Petkov c->x86_mask = x86_stepping(tfms); 7330f3fa48aSIngo Molnar 734d4387bd3SHuang, Ying if (cap0 & (1<<19)) { 735d4387bd3SHuang, Ying c->x86_clflush_size = ((misc >> 8) & 0xff) * 8; 7369d31d35bSYinghai Lu c->x86_cache_alignment = c->x86_clflush_size; 737d4387bd3SHuang, Ying } 738f7627e25SThomas Gleixner } 739f7627e25SThomas Gleixner } 7403da99c97SYinghai Lu 7418bf1ebcaSAndy Lutomirski static void apply_forced_caps(struct cpuinfo_x86 *c) 7428bf1ebcaSAndy Lutomirski { 7438bf1ebcaSAndy Lutomirski int i; 7448bf1ebcaSAndy Lutomirski 7458bf1ebcaSAndy Lutomirski for (i = 0; i < NCAPINTS; i++) { 7468bf1ebcaSAndy Lutomirski c->x86_capability[i] &= ~cpu_caps_cleared[i]; 7478bf1ebcaSAndy Lutomirski c->x86_capability[i] |= cpu_caps_set[i]; 7488bf1ebcaSAndy Lutomirski } 7498bf1ebcaSAndy Lutomirski } 7508bf1ebcaSAndy Lutomirski 751148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c) 752093af8d7SYinghai Lu { 75339c06df4SBorislav Petkov u32 eax, ebx, ecx, edx; 754093af8d7SYinghai Lu 755093af8d7SYinghai Lu /* Intel-defined flags: level 0x00000001 */ 756093af8d7SYinghai Lu if (c->cpuid_level >= 0x00000001) { 75739c06df4SBorislav Petkov cpuid(0x00000001, &eax, &ebx, &ecx, &edx); 7580f3fa48aSIngo Molnar 75939c06df4SBorislav Petkov c->x86_capability[CPUID_1_ECX] = ecx; 76039c06df4SBorislav Petkov c->x86_capability[CPUID_1_EDX] = edx; 761093af8d7SYinghai Lu } 762093af8d7SYinghai Lu 7633df8d920SAndy Lutomirski /* Thermal and Power Management Leaf: level 0x00000006 (eax) */ 7643df8d920SAndy Lutomirski if (c->cpuid_level >= 0x00000006) 7653df8d920SAndy Lutomirski c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006); 7663df8d920SAndy Lutomirski 767bdc802dcSH. Peter Anvin /* Additional Intel-defined flags: level 0x00000007 */ 768bdc802dcSH. Peter Anvin if (c->cpuid_level >= 0x00000007) { 769bdc802dcSH. Peter Anvin cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx); 77039c06df4SBorislav Petkov c->x86_capability[CPUID_7_0_EBX] = ebx; 771dfb4a70fSDave Hansen c->x86_capability[CPUID_7_ECX] = ecx; 772bdc802dcSH. Peter Anvin } 773bdc802dcSH. Peter Anvin 7746229ad27SFenghua Yu /* Extended state features: level 0x0000000d */ 7756229ad27SFenghua Yu if (c->cpuid_level >= 0x0000000d) { 7766229ad27SFenghua Yu cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx); 7776229ad27SFenghua Yu 77839c06df4SBorislav Petkov c->x86_capability[CPUID_D_1_EAX] = eax; 7796229ad27SFenghua Yu } 7806229ad27SFenghua Yu 781cbc82b17SPeter P Waskiewicz Jr /* Additional Intel-defined flags: level 0x0000000F */ 782cbc82b17SPeter P Waskiewicz Jr if (c->cpuid_level >= 0x0000000F) { 783cbc82b17SPeter P Waskiewicz Jr 784cbc82b17SPeter P Waskiewicz Jr /* QoS sub-leaf, EAX=0Fh, ECX=0 */ 785cbc82b17SPeter P Waskiewicz Jr cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx); 78639c06df4SBorislav Petkov c->x86_capability[CPUID_F_0_EDX] = edx; 78739c06df4SBorislav Petkov 788cbc82b17SPeter P Waskiewicz Jr if (cpu_has(c, X86_FEATURE_CQM_LLC)) { 789cbc82b17SPeter P Waskiewicz Jr /* will be overridden if occupancy monitoring exists */ 790cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid = ebx; 791cbc82b17SPeter P Waskiewicz Jr 792cbc82b17SPeter P Waskiewicz Jr /* QoS sub-leaf, EAX=0Fh, ECX=1 */ 793cbc82b17SPeter P Waskiewicz Jr cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx); 79439c06df4SBorislav Petkov c->x86_capability[CPUID_F_1_EDX] = edx; 79539c06df4SBorislav Petkov 79633c3cc7aSVikas Shivappa if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) || 79733c3cc7aSVikas Shivappa ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) || 79833c3cc7aSVikas Shivappa (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) { 799cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid = ecx; 800cbc82b17SPeter P Waskiewicz Jr c->x86_cache_occ_scale = ebx; 801cbc82b17SPeter P Waskiewicz Jr } 802cbc82b17SPeter P Waskiewicz Jr } else { 803cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid = -1; 804cbc82b17SPeter P Waskiewicz Jr c->x86_cache_occ_scale = -1; 805cbc82b17SPeter P Waskiewicz Jr } 806cbc82b17SPeter P Waskiewicz Jr } 807cbc82b17SPeter P Waskiewicz Jr 808093af8d7SYinghai Lu /* AMD-defined flags: level 0x80000001 */ 80939c06df4SBorislav Petkov eax = cpuid_eax(0x80000000); 81039c06df4SBorislav Petkov c->extended_cpuid_level = eax; 8110f3fa48aSIngo Molnar 81239c06df4SBorislav Petkov if ((eax & 0xffff0000) == 0x80000000) { 81339c06df4SBorislav Petkov if (eax >= 0x80000001) { 81439c06df4SBorislav Petkov cpuid(0x80000001, &eax, &ebx, &ecx, &edx); 81539c06df4SBorislav Petkov 81639c06df4SBorislav Petkov c->x86_capability[CPUID_8000_0001_ECX] = ecx; 81739c06df4SBorislav Petkov c->x86_capability[CPUID_8000_0001_EDX] = edx; 818093af8d7SYinghai Lu } 819093af8d7SYinghai Lu } 820093af8d7SYinghai Lu 82171faad43SYazen Ghannam if (c->extended_cpuid_level >= 0x80000007) { 82271faad43SYazen Ghannam cpuid(0x80000007, &eax, &ebx, &ecx, &edx); 82371faad43SYazen Ghannam 82471faad43SYazen Ghannam c->x86_capability[CPUID_8000_0007_EBX] = ebx; 82571faad43SYazen Ghannam c->x86_power = edx; 82671faad43SYazen Ghannam } 82771faad43SYazen Ghannam 8285122c890SYinghai Lu if (c->extended_cpuid_level >= 0x80000008) { 82939c06df4SBorislav Petkov cpuid(0x80000008, &eax, &ebx, &ecx, &edx); 8305122c890SYinghai Lu 8315122c890SYinghai Lu c->x86_virt_bits = (eax >> 8) & 0xff; 8325122c890SYinghai Lu c->x86_phys_bits = eax & 0xff; 83339c06df4SBorislav Petkov c->x86_capability[CPUID_8000_0008_EBX] = ebx; 8345122c890SYinghai Lu } 83513c6c532SJan Beulich #ifdef CONFIG_X86_32 83613c6c532SJan Beulich else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36)) 83713c6c532SJan Beulich c->x86_phys_bits = 36; 8385122c890SYinghai Lu #endif 839e3224234SYinghai Lu 8402ccd71f1SBorislav Petkov if (c->extended_cpuid_level >= 0x8000000a) 84139c06df4SBorislav Petkov c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a); 8422ccd71f1SBorislav Petkov 8431dedefd1SJacob Pan init_scattered_cpuid_features(c); 84460d34501SAndy Lutomirski 84560d34501SAndy Lutomirski /* 84660d34501SAndy Lutomirski * Clear/Set all flags overridden by options, after probe. 84760d34501SAndy Lutomirski * This needs to happen each time we re-probe, which may happen 84860d34501SAndy Lutomirski * several times during CPU initialization. 84960d34501SAndy Lutomirski */ 85060d34501SAndy Lutomirski apply_forced_caps(c); 851093af8d7SYinghai Lu } 852093af8d7SYinghai Lu 853148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c) 854aef93c8bSYinghai Lu { 855aef93c8bSYinghai Lu #ifdef CONFIG_X86_32 856aef93c8bSYinghai Lu int i; 857aef93c8bSYinghai Lu 858aef93c8bSYinghai Lu /* 859aef93c8bSYinghai Lu * First of all, decide if this is a 486 or higher 860aef93c8bSYinghai Lu * It's a 486 if we can modify the AC flag 861aef93c8bSYinghai Lu */ 862aef93c8bSYinghai Lu if (flag_is_changeable_p(X86_EFLAGS_AC)) 863aef93c8bSYinghai Lu c->x86 = 4; 864aef93c8bSYinghai Lu else 865aef93c8bSYinghai Lu c->x86 = 3; 866aef93c8bSYinghai Lu 867aef93c8bSYinghai Lu for (i = 0; i < X86_VENDOR_NUM; i++) 868aef93c8bSYinghai Lu if (cpu_devs[i] && cpu_devs[i]->c_identify) { 869aef93c8bSYinghai Lu c->x86_vendor_id[0] = 0; 870aef93c8bSYinghai Lu cpu_devs[i]->c_identify(c); 871aef93c8bSYinghai Lu if (c->x86_vendor_id[0]) { 872aef93c8bSYinghai Lu get_cpu_vendor(c); 873aef93c8bSYinghai Lu break; 874aef93c8bSYinghai Lu } 875aef93c8bSYinghai Lu } 876aef93c8bSYinghai Lu #endif 877093af8d7SYinghai Lu } 878f7627e25SThomas Gleixner 87934048c9eSPaolo Ciarrocchi /* 88034048c9eSPaolo Ciarrocchi * Do minimum CPU detection early. 88134048c9eSPaolo Ciarrocchi * Fields really needed: vendor, cpuid_level, family, model, mask, 88234048c9eSPaolo Ciarrocchi * cache alignment. 88334048c9eSPaolo Ciarrocchi * The others are not touched to avoid unwanted side effects. 88434048c9eSPaolo Ciarrocchi * 88534048c9eSPaolo Ciarrocchi * WARNING: this function is only called on the BP. Don't add code here 88634048c9eSPaolo Ciarrocchi * that is supposed to run on all CPUs. 88734048c9eSPaolo Ciarrocchi */ 8883da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c) 889f7627e25SThomas Gleixner { 8906627d242SYinghai Lu #ifdef CONFIG_X86_64 8916627d242SYinghai Lu c->x86_clflush_size = 64; 89213c6c532SJan Beulich c->x86_phys_bits = 36; 89313c6c532SJan Beulich c->x86_virt_bits = 48; 8946627d242SYinghai Lu #else 895d4387bd3SHuang, Ying c->x86_clflush_size = 32; 89613c6c532SJan Beulich c->x86_phys_bits = 32; 89713c6c532SJan Beulich c->x86_virt_bits = 32; 8986627d242SYinghai Lu #endif 8990a488a53SYinghai Lu c->x86_cache_alignment = c->x86_clflush_size; 900f7627e25SThomas Gleixner 9013da99c97SYinghai Lu memset(&c->x86_capability, 0, sizeof c->x86_capability); 9020a488a53SYinghai Lu c->extended_cpuid_level = 0; 9030a488a53SYinghai Lu 904aef93c8bSYinghai Lu /* cyrix could have cpuid enabled via c_identify()*/ 90505fb3c19SAndy Lutomirski if (have_cpuid_p()) { 906f7627e25SThomas Gleixner cpu_detect(c); 9073da99c97SYinghai Lu get_cpu_vendor(c); 9083da99c97SYinghai Lu get_cpu_cap(c); 90978d1b296SBorislav Petkov setup_force_cpu_cap(X86_FEATURE_CPUID); 91012cf105cSKrzysztof Helt 91110a434fcSYinghai Lu if (this_cpu->c_early_init) 91210a434fcSYinghai Lu this_cpu->c_early_init(c); 9133da99c97SYinghai Lu 914f6e9456cSRobert Richter c->cpu_index = 0; 915b38b0665SH. Peter Anvin filter_cpuid_features(c, false); 916de5397adSFenghua Yu 917a110b5ecSBorislav Petkov if (this_cpu->c_bsp_init) 918a110b5ecSBorislav Petkov this_cpu->c_bsp_init(c); 91978d1b296SBorislav Petkov } else { 92078d1b296SBorislav Petkov identify_cpu_without_cpuid(c); 92178d1b296SBorislav Petkov setup_clear_cpu_cap(X86_FEATURE_CPUID); 92205fb3c19SAndy Lutomirski } 923c3b83598SBorislav Petkov 924c3b83598SBorislav Petkov setup_force_cpu_cap(X86_FEATURE_ALWAYS); 925db52ef74SIngo Molnar fpu__init_system(c); 926f7627e25SThomas Gleixner } 927f7627e25SThomas Gleixner 9289d31d35bSYinghai Lu void __init early_cpu_init(void) 9299d31d35bSYinghai Lu { 93002dde8b4SJan Beulich const struct cpu_dev *const *cdev; 93110a434fcSYinghai Lu int count = 0; 9329d31d35bSYinghai Lu 933ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT 9341b74dde7SChen Yucong pr_info("KERNEL supported cpus:\n"); 93531c997caSIngo Molnar #endif 93631c997caSIngo Molnar 93710a434fcSYinghai Lu for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) { 93802dde8b4SJan Beulich const struct cpu_dev *cpudev = *cdev; 9399d31d35bSYinghai Lu 94010a434fcSYinghai Lu if (count >= X86_VENDOR_NUM) 94110a434fcSYinghai Lu break; 94210a434fcSYinghai Lu cpu_devs[count] = cpudev; 94310a434fcSYinghai Lu count++; 94410a434fcSYinghai Lu 945ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT 94631c997caSIngo Molnar { 94731c997caSIngo Molnar unsigned int j; 94831c997caSIngo Molnar 94910a434fcSYinghai Lu for (j = 0; j < 2; j++) { 95010a434fcSYinghai Lu if (!cpudev->c_ident[j]) 95110a434fcSYinghai Lu continue; 9521b74dde7SChen Yucong pr_info(" %s %s\n", cpudev->c_vendor, 95310a434fcSYinghai Lu cpudev->c_ident[j]); 95410a434fcSYinghai Lu } 95510a434fcSYinghai Lu } 9560388423dSDave Jones #endif 95731c997caSIngo Molnar } 9589d31d35bSYinghai Lu early_identify_cpu(&boot_cpu_data); 959f7627e25SThomas Gleixner } 960f7627e25SThomas Gleixner 961b6734c35SH. Peter Anvin /* 962366d4a43SBorislav Petkov * The NOPL instruction is supposed to exist on all CPUs of family >= 6; 963366d4a43SBorislav Petkov * unfortunately, that's not true in practice because of early VIA 964366d4a43SBorislav Petkov * chips and (more importantly) broken virtualizers that are not easy 965366d4a43SBorislav Petkov * to detect. In the latter case it doesn't even *fail* reliably, so 966366d4a43SBorislav Petkov * probing for it doesn't even work. Disable it completely on 32-bit 967ba0593bfSH. Peter Anvin * unless we can find a reliable way to detect all the broken cases. 968366d4a43SBorislav Petkov * Enable it explicitly on 64-bit for non-constant inputs of cpu_has(). 969b6734c35SH. Peter Anvin */ 970148f9bb8SPaul Gortmaker static void detect_nopl(struct cpuinfo_x86 *c) 971b6734c35SH. Peter Anvin { 972366d4a43SBorislav Petkov #ifdef CONFIG_X86_32 973b6734c35SH. Peter Anvin clear_cpu_cap(c, X86_FEATURE_NOPL); 974366d4a43SBorislav Petkov #else 975366d4a43SBorislav Petkov set_cpu_cap(c, X86_FEATURE_NOPL); 976366d4a43SBorislav Petkov #endif 977f7627e25SThomas Gleixner } 978f7627e25SThomas Gleixner 9797a5d6704SAndy Lutomirski static void detect_null_seg_behavior(struct cpuinfo_x86 *c) 9807a5d6704SAndy Lutomirski { 9817a5d6704SAndy Lutomirski #ifdef CONFIG_X86_64 982f7627e25SThomas Gleixner /* 9837a5d6704SAndy Lutomirski * Empirically, writing zero to a segment selector on AMD does 9847a5d6704SAndy Lutomirski * not clear the base, whereas writing zero to a segment 9857a5d6704SAndy Lutomirski * selector on Intel does clear the base. Intel's behavior 9867a5d6704SAndy Lutomirski * allows slightly faster context switches in the common case 9877a5d6704SAndy Lutomirski * where GS is unused by the prev and next threads. 988f7627e25SThomas Gleixner * 9897a5d6704SAndy Lutomirski * Since neither vendor documents this anywhere that I can see, 9907a5d6704SAndy Lutomirski * detect it directly instead of hardcoding the choice by 9917a5d6704SAndy Lutomirski * vendor. 9927a5d6704SAndy Lutomirski * 9937a5d6704SAndy Lutomirski * I've designated AMD's behavior as the "bug" because it's 9947a5d6704SAndy Lutomirski * counterintuitive and less friendly. 995f7627e25SThomas Gleixner */ 9967a5d6704SAndy Lutomirski 9977a5d6704SAndy Lutomirski unsigned long old_base, tmp; 9987a5d6704SAndy Lutomirski rdmsrl(MSR_FS_BASE, old_base); 9997a5d6704SAndy Lutomirski wrmsrl(MSR_FS_BASE, 1); 10007a5d6704SAndy Lutomirski loadsegment(fs, 0); 10017a5d6704SAndy Lutomirski rdmsrl(MSR_FS_BASE, tmp); 10027a5d6704SAndy Lutomirski if (tmp != 0) 10037a5d6704SAndy Lutomirski set_cpu_bug(c, X86_BUG_NULL_SEG); 10047a5d6704SAndy Lutomirski wrmsrl(MSR_FS_BASE, old_base); 10053da99c97SYinghai Lu #endif 1006f7627e25SThomas Gleixner } 1007aef93c8bSYinghai Lu 1008148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c) 1009f7627e25SThomas Gleixner { 1010f7627e25SThomas Gleixner c->extended_cpuid_level = 0; 1011f7627e25SThomas Gleixner 1012aef93c8bSYinghai Lu if (!have_cpuid_p()) 1013aef93c8bSYinghai Lu identify_cpu_without_cpuid(c); 1014f7627e25SThomas Gleixner 1015aef93c8bSYinghai Lu /* cyrix could have cpuid enabled via c_identify()*/ 1016a9853dd6SIngo Molnar if (!have_cpuid_p()) 1017aef93c8bSYinghai Lu return; 1018aef93c8bSYinghai Lu 10193da99c97SYinghai Lu cpu_detect(c); 10203da99c97SYinghai Lu 10213da99c97SYinghai Lu get_cpu_vendor(c); 10223da99c97SYinghai Lu 10233da99c97SYinghai Lu get_cpu_cap(c); 10243da99c97SYinghai Lu 1025f7627e25SThomas Gleixner if (c->cpuid_level >= 0x00000001) { 10263da99c97SYinghai Lu c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF; 1027b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32 1028c8e56d20SBorislav Petkov # ifdef CONFIG_SMP 1029cb8cc442SIngo Molnar c->apicid = apic->phys_pkg_id(c->initial_apicid, 0); 1030f7627e25SThomas Gleixner # else 103101aaea1aSYinghai Lu c->apicid = c->initial_apicid; 1032f7627e25SThomas Gleixner # endif 1033b89d3b3eSYinghai Lu #endif 1034b89d3b3eSYinghai Lu c->phys_proc_id = c->initial_apicid; 1035f7627e25SThomas Gleixner } 1036f7627e25SThomas Gleixner 1037f7627e25SThomas Gleixner get_model_name(c); /* Default name */ 1038f7627e25SThomas Gleixner 1039b6734c35SH. Peter Anvin detect_nopl(c); 10407a5d6704SAndy Lutomirski 10417a5d6704SAndy Lutomirski detect_null_seg_behavior(c); 10420230bb03SAndy Lutomirski 10430230bb03SAndy Lutomirski /* 10440230bb03SAndy Lutomirski * ESPFIX is a strange bug. All real CPUs have it. Paravirt 10450230bb03SAndy Lutomirski * systems that run Linux at CPL > 0 may or may not have the 10460230bb03SAndy Lutomirski * issue, but, even if they have the issue, there's absolutely 10470230bb03SAndy Lutomirski * nothing we can do about it because we can't use the real IRET 10480230bb03SAndy Lutomirski * instruction. 10490230bb03SAndy Lutomirski * 10500230bb03SAndy Lutomirski * NB: For the time being, only 32-bit kernels support 10510230bb03SAndy Lutomirski * X86_BUG_ESPFIX as such. 64-bit kernels directly choose 10520230bb03SAndy Lutomirski * whether to apply espfix using paravirt hooks. If any 10530230bb03SAndy Lutomirski * non-paravirt system ever shows up that does *not* have the 10540230bb03SAndy Lutomirski * ESPFIX issue, we can change this. 10550230bb03SAndy Lutomirski */ 10560230bb03SAndy Lutomirski #ifdef CONFIG_X86_32 10570230bb03SAndy Lutomirski # ifdef CONFIG_PARAVIRT 10580230bb03SAndy Lutomirski do { 10590230bb03SAndy Lutomirski extern void native_iret(void); 10600230bb03SAndy Lutomirski if (pv_cpu_ops.iret == native_iret) 10610230bb03SAndy Lutomirski set_cpu_bug(c, X86_BUG_ESPFIX); 10620230bb03SAndy Lutomirski } while (0); 10630230bb03SAndy Lutomirski # else 10640230bb03SAndy Lutomirski set_cpu_bug(c, X86_BUG_ESPFIX); 10650230bb03SAndy Lutomirski # endif 10660230bb03SAndy Lutomirski #endif 1067f7627e25SThomas Gleixner } 1068f7627e25SThomas Gleixner 1069cbc82b17SPeter P Waskiewicz Jr static void x86_init_cache_qos(struct cpuinfo_x86 *c) 1070cbc82b17SPeter P Waskiewicz Jr { 1071cbc82b17SPeter P Waskiewicz Jr /* 1072cbc82b17SPeter P Waskiewicz Jr * The heavy lifting of max_rmid and cache_occ_scale are handled 1073cbc82b17SPeter P Waskiewicz Jr * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu 1074cbc82b17SPeter P Waskiewicz Jr * in case CQM bits really aren't there in this CPU. 1075cbc82b17SPeter P Waskiewicz Jr */ 1076cbc82b17SPeter P Waskiewicz Jr if (c != &boot_cpu_data) { 1077cbc82b17SPeter P Waskiewicz Jr boot_cpu_data.x86_cache_max_rmid = 1078cbc82b17SPeter P Waskiewicz Jr min(boot_cpu_data.x86_cache_max_rmid, 1079cbc82b17SPeter P Waskiewicz Jr c->x86_cache_max_rmid); 1080cbc82b17SPeter P Waskiewicz Jr } 1081cbc82b17SPeter P Waskiewicz Jr } 1082cbc82b17SPeter P Waskiewicz Jr 1083f7627e25SThomas Gleixner /* 10849d85eb91SThomas Gleixner * Validate that ACPI/mptables have the same information about the 10859d85eb91SThomas Gleixner * effective APIC id and update the package map. 1086d49597fdSThomas Gleixner */ 10879d85eb91SThomas Gleixner static void validate_apic_and_package_id(struct cpuinfo_x86 *c) 1088d49597fdSThomas Gleixner { 1089d49597fdSThomas Gleixner #ifdef CONFIG_SMP 10909d85eb91SThomas Gleixner unsigned int apicid, cpu = smp_processor_id(); 1091d49597fdSThomas Gleixner 1092d49597fdSThomas Gleixner apicid = apic->cpu_present_to_apicid(cpu); 1093d49597fdSThomas Gleixner 10949d85eb91SThomas Gleixner if (apicid != c->apicid) { 10959d85eb91SThomas Gleixner pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n", 1096d49597fdSThomas Gleixner cpu, apicid, c->initial_apicid); 1097d49597fdSThomas Gleixner } 10989d85eb91SThomas Gleixner BUG_ON(topology_update_package_map(c->phys_proc_id, cpu)); 1099d49597fdSThomas Gleixner #else 1100d49597fdSThomas Gleixner c->logical_proc_id = 0; 1101d49597fdSThomas Gleixner #endif 1102d49597fdSThomas Gleixner } 1103d49597fdSThomas Gleixner 1104d49597fdSThomas Gleixner /* 1105f7627e25SThomas Gleixner * This does the hard work of actually picking apart the CPU stuff... 1106f7627e25SThomas Gleixner */ 1107148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c) 1108f7627e25SThomas Gleixner { 1109f7627e25SThomas Gleixner int i; 1110f7627e25SThomas Gleixner 1111f7627e25SThomas Gleixner c->loops_per_jiffy = loops_per_jiffy; 1112f7627e25SThomas Gleixner c->x86_cache_size = -1; 1113f7627e25SThomas Gleixner c->x86_vendor = X86_VENDOR_UNKNOWN; 1114f7627e25SThomas Gleixner c->x86_model = c->x86_mask = 0; /* So far unknown... */ 1115f7627e25SThomas Gleixner c->x86_vendor_id[0] = '\0'; /* Unset */ 1116f7627e25SThomas Gleixner c->x86_model_id[0] = '\0'; /* Unset */ 1117f7627e25SThomas Gleixner c->x86_max_cores = 1; 1118102bbe3aSYinghai Lu c->x86_coreid_bits = 0; 111979a8b9aaSBorislav Petkov c->cu_id = 0xff; 112011fdd252SYinghai Lu #ifdef CONFIG_X86_64 1121102bbe3aSYinghai Lu c->x86_clflush_size = 64; 112213c6c532SJan Beulich c->x86_phys_bits = 36; 112313c6c532SJan Beulich c->x86_virt_bits = 48; 1124102bbe3aSYinghai Lu #else 1125102bbe3aSYinghai Lu c->cpuid_level = -1; /* CPUID not detected */ 1126f7627e25SThomas Gleixner c->x86_clflush_size = 32; 112713c6c532SJan Beulich c->x86_phys_bits = 32; 112813c6c532SJan Beulich c->x86_virt_bits = 32; 1129102bbe3aSYinghai Lu #endif 1130102bbe3aSYinghai Lu c->x86_cache_alignment = c->x86_clflush_size; 1131f7627e25SThomas Gleixner memset(&c->x86_capability, 0, sizeof c->x86_capability); 1132f7627e25SThomas Gleixner 1133f7627e25SThomas Gleixner generic_identify(c); 1134f7627e25SThomas Gleixner 11353898534dSAndi Kleen if (this_cpu->c_identify) 1136f7627e25SThomas Gleixner this_cpu->c_identify(c); 1137f7627e25SThomas Gleixner 11386a6256f9SAdam Buchbinder /* Clear/Set all flags overridden by options, after probe */ 11398bf1ebcaSAndy Lutomirski apply_forced_caps(c); 11402759c328SYinghai Lu 1141102bbe3aSYinghai Lu #ifdef CONFIG_X86_64 1142cb8cc442SIngo Molnar c->apicid = apic->phys_pkg_id(c->initial_apicid, 0); 1143102bbe3aSYinghai Lu #endif 1144102bbe3aSYinghai Lu 1145f7627e25SThomas Gleixner /* 1146f7627e25SThomas Gleixner * Vendor-specific initialization. In this section we 1147f7627e25SThomas Gleixner * canonicalize the feature flags, meaning if there are 1148f7627e25SThomas Gleixner * features a certain CPU supports which CPUID doesn't 1149f7627e25SThomas Gleixner * tell us, CPUID claiming incorrect flags, or other bugs, 1150f7627e25SThomas Gleixner * we handle them here. 1151f7627e25SThomas Gleixner * 1152f7627e25SThomas Gleixner * At the end of this section, c->x86_capability better 1153f7627e25SThomas Gleixner * indicate the features this CPU genuinely supports! 1154f7627e25SThomas Gleixner */ 1155f7627e25SThomas Gleixner if (this_cpu->c_init) 1156f7627e25SThomas Gleixner this_cpu->c_init(c); 1157f7627e25SThomas Gleixner 1158f7627e25SThomas Gleixner /* Disable the PN if appropriate */ 1159f7627e25SThomas Gleixner squash_the_stupid_serial_number(c); 1160f7627e25SThomas Gleixner 1161b2cc2a07SH. Peter Anvin /* Set up SMEP/SMAP */ 1162b2cc2a07SH. Peter Anvin setup_smep(c); 1163b2cc2a07SH. Peter Anvin setup_smap(c); 1164b2cc2a07SH. Peter Anvin 1165660da7c9SAndy Lutomirski /* Set up PCID */ 1166660da7c9SAndy Lutomirski setup_pcid(c); 1167660da7c9SAndy Lutomirski 1168f7627e25SThomas Gleixner /* 11690f3fa48aSIngo Molnar * The vendor-specific functions might have changed features. 11700f3fa48aSIngo Molnar * Now we do "generic changes." 1171f7627e25SThomas Gleixner */ 1172f7627e25SThomas Gleixner 1173b38b0665SH. Peter Anvin /* Filter out anything that depends on CPUID levels we don't have */ 1174b38b0665SH. Peter Anvin filter_cpuid_features(c, true); 1175b38b0665SH. Peter Anvin 1176f7627e25SThomas Gleixner /* If the model name is still unset, do table lookup. */ 1177f7627e25SThomas Gleixner if (!c->x86_model_id[0]) { 117802dde8b4SJan Beulich const char *p; 1179f7627e25SThomas Gleixner p = table_lookup_model(c); 1180f7627e25SThomas Gleixner if (p) 1181f7627e25SThomas Gleixner strcpy(c->x86_model_id, p); 1182f7627e25SThomas Gleixner else 1183f7627e25SThomas Gleixner /* Last resort... */ 1184f7627e25SThomas Gleixner sprintf(c->x86_model_id, "%02x/%02x", 1185f7627e25SThomas Gleixner c->x86, c->x86_model); 1186f7627e25SThomas Gleixner } 1187f7627e25SThomas Gleixner 1188102bbe3aSYinghai Lu #ifdef CONFIG_X86_64 1189102bbe3aSYinghai Lu detect_ht(c); 1190102bbe3aSYinghai Lu #endif 1191102bbe3aSYinghai Lu 119249d859d7SH. Peter Anvin x86_init_rdrand(c); 1193cbc82b17SPeter P Waskiewicz Jr x86_init_cache_qos(c); 119406976945SDave Hansen setup_pku(c); 11953e0c3737SYinghai Lu 11963e0c3737SYinghai Lu /* 11976a6256f9SAdam Buchbinder * Clear/Set all flags overridden by options, need do it 11983e0c3737SYinghai Lu * before following smp all cpus cap AND. 11993e0c3737SYinghai Lu */ 12008bf1ebcaSAndy Lutomirski apply_forced_caps(c); 12013e0c3737SYinghai Lu 1202f7627e25SThomas Gleixner /* 1203f7627e25SThomas Gleixner * On SMP, boot_cpu_data holds the common feature set between 1204f7627e25SThomas Gleixner * all CPUs; so make sure that we indicate which features are 1205f7627e25SThomas Gleixner * common between the CPUs. The first time this routine gets 1206f7627e25SThomas Gleixner * executed, c == &boot_cpu_data. 1207f7627e25SThomas Gleixner */ 1208f7627e25SThomas Gleixner if (c != &boot_cpu_data) { 1209f7627e25SThomas Gleixner /* AND the already accumulated flags with these */ 1210f7627e25SThomas Gleixner for (i = 0; i < NCAPINTS; i++) 1211f7627e25SThomas Gleixner boot_cpu_data.x86_capability[i] &= c->x86_capability[i]; 121265fc985bSBorislav Petkov 121365fc985bSBorislav Petkov /* OR, i.e. replicate the bug flags */ 121465fc985bSBorislav Petkov for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++) 121565fc985bSBorislav Petkov c->x86_capability[i] |= boot_cpu_data.x86_capability[i]; 1216f7627e25SThomas Gleixner } 1217f7627e25SThomas Gleixner 1218f7627e25SThomas Gleixner /* Init Machine Check Exception if available. */ 12195e09954aSBorislav Petkov mcheck_cpu_init(c); 122030d432dfSAndi Kleen 122130d432dfSAndi Kleen select_idle_routine(c); 1222102bbe3aSYinghai Lu 1223de2d9445STejun Heo #ifdef CONFIG_NUMA 1224102bbe3aSYinghai Lu numa_add_cpu(smp_processor_id()); 1225102bbe3aSYinghai Lu #endif 1226f7627e25SThomas Gleixner } 1227f7627e25SThomas Gleixner 12288b6c0ab1SIngo Molnar /* 12298b6c0ab1SIngo Molnar * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions 12308b6c0ab1SIngo Molnar * on 32-bit kernels: 12318b6c0ab1SIngo Molnar */ 1232cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32 1233cfda7bb9SAndy Lutomirski void enable_sep_cpu(void) 1234cfda7bb9SAndy Lutomirski { 12358b6c0ab1SIngo Molnar struct tss_struct *tss; 12368b6c0ab1SIngo Molnar int cpu; 1237cfda7bb9SAndy Lutomirski 1238b3edfda4SBorislav Petkov if (!boot_cpu_has(X86_FEATURE_SEP)) 1239b3edfda4SBorislav Petkov return; 1240b3edfda4SBorislav Petkov 12418b6c0ab1SIngo Molnar cpu = get_cpu(); 12428b6c0ab1SIngo Molnar tss = &per_cpu(cpu_tss, cpu); 12438b6c0ab1SIngo Molnar 12448b6c0ab1SIngo Molnar /* 1245cf9328ccSAndy Lutomirski * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field -- 1246cf9328ccSAndy Lutomirski * see the big comment in struct x86_hw_tss's definition. 12478b6c0ab1SIngo Molnar */ 1248cfda7bb9SAndy Lutomirski 1249cfda7bb9SAndy Lutomirski tss->x86_tss.ss1 = __KERNEL_CS; 12508b6c0ab1SIngo Molnar wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0); 12518b6c0ab1SIngo Molnar 1252cf9328ccSAndy Lutomirski wrmsr(MSR_IA32_SYSENTER_ESP, 1253cf9328ccSAndy Lutomirski (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack), 1254cf9328ccSAndy Lutomirski 0); 12558b6c0ab1SIngo Molnar 12564c8cd0c5SIngo Molnar wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0); 12578b6c0ab1SIngo Molnar 1258cfda7bb9SAndy Lutomirski put_cpu(); 1259cfda7bb9SAndy Lutomirski } 1260e04d645fSGlauber Costa #endif 1261e04d645fSGlauber Costa 1262f7627e25SThomas Gleixner void __init identify_boot_cpu(void) 1263f7627e25SThomas Gleixner { 1264f7627e25SThomas Gleixner identify_cpu(&boot_cpu_data); 1265102bbe3aSYinghai Lu #ifdef CONFIG_X86_32 1266f7627e25SThomas Gleixner sysenter_setup(); 1267f7627e25SThomas Gleixner enable_sep_cpu(); 1268102bbe3aSYinghai Lu #endif 1269e0ba94f1SAlex Shi cpu_detect_tlb(&boot_cpu_data); 1270f7627e25SThomas Gleixner } 1271f7627e25SThomas Gleixner 1272148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c) 1273f7627e25SThomas Gleixner { 1274f7627e25SThomas Gleixner BUG_ON(c == &boot_cpu_data); 1275f7627e25SThomas Gleixner identify_cpu(c); 1276102bbe3aSYinghai Lu #ifdef CONFIG_X86_32 1277f7627e25SThomas Gleixner enable_sep_cpu(); 1278102bbe3aSYinghai Lu #endif 1279f7627e25SThomas Gleixner mtrr_ap_init(); 12809d85eb91SThomas Gleixner validate_apic_and_package_id(c); 1281f7627e25SThomas Gleixner } 1282f7627e25SThomas Gleixner 1283191679fdSAndi Kleen static __init int setup_noclflush(char *arg) 1284191679fdSAndi Kleen { 1285840d2830SH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_CLFLUSH); 1286da4aaa7dSH. Peter Anvin setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT); 1287191679fdSAndi Kleen return 1; 1288191679fdSAndi Kleen } 1289191679fdSAndi Kleen __setup("noclflush", setup_noclflush); 1290191679fdSAndi Kleen 1291148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c) 1292f7627e25SThomas Gleixner { 129302dde8b4SJan Beulich const char *vendor = NULL; 1294f7627e25SThomas Gleixner 12950f3fa48aSIngo Molnar if (c->x86_vendor < X86_VENDOR_NUM) { 1296f7627e25SThomas Gleixner vendor = this_cpu->c_vendor; 12970f3fa48aSIngo Molnar } else { 12980f3fa48aSIngo Molnar if (c->cpuid_level >= 0) 1299f7627e25SThomas Gleixner vendor = c->x86_vendor_id; 13000f3fa48aSIngo Molnar } 1301f7627e25SThomas Gleixner 1302bd32a8cfSYinghai Lu if (vendor && !strstr(c->x86_model_id, vendor)) 13031b74dde7SChen Yucong pr_cont("%s ", vendor); 1304f7627e25SThomas Gleixner 13059d31d35bSYinghai Lu if (c->x86_model_id[0]) 13061b74dde7SChen Yucong pr_cont("%s", c->x86_model_id); 1307f7627e25SThomas Gleixner else 13081b74dde7SChen Yucong pr_cont("%d86", c->x86); 1309f7627e25SThomas Gleixner 13101b74dde7SChen Yucong pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model); 1311924e101aSBorislav Petkov 1312f7627e25SThomas Gleixner if (c->x86_mask || c->cpuid_level >= 0) 13131b74dde7SChen Yucong pr_cont(", stepping: 0x%x)\n", c->x86_mask); 1314f7627e25SThomas Gleixner else 13151b74dde7SChen Yucong pr_cont(")\n"); 1316f7627e25SThomas Gleixner } 1317f7627e25SThomas Gleixner 1318ac72e788SAndi Kleen static __init int setup_disablecpuid(char *arg) 1319ac72e788SAndi Kleen { 1320ac72e788SAndi Kleen int bit; 13210f3fa48aSIngo Molnar 1322dd853fd2SLukasz Odzioba if (get_option(&arg, &bit) && bit >= 0 && bit < NCAPINTS * 32) 1323ac72e788SAndi Kleen setup_clear_cpu_cap(bit); 1324ac72e788SAndi Kleen else 1325ac72e788SAndi Kleen return 0; 13260f3fa48aSIngo Molnar 1327ac72e788SAndi Kleen return 1; 1328ac72e788SAndi Kleen } 1329ac72e788SAndi Kleen __setup("clearcpuid=", setup_disablecpuid); 1330ac72e788SAndi Kleen 1331d5494d4fSYinghai Lu #ifdef CONFIG_X86_64 1332947e76cdSBrian Gerst DEFINE_PER_CPU_FIRST(union irq_stack_union, 1333277d5b40SAndi Kleen irq_stack_union) __aligned(PAGE_SIZE) __visible; 13340f3fa48aSIngo Molnar 1335bdf977b3STejun Heo /* 1336a7fcf28dSAndy Lutomirski * The following percpu variables are hot. Align current_task to 1337a7fcf28dSAndy Lutomirski * cacheline size such that they fall in the same cacheline. 1338bdf977b3STejun Heo */ 1339bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned = 1340bdf977b3STejun Heo &init_task; 1341bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task); 1342d5494d4fSYinghai Lu 1343bdf977b3STejun Heo DEFINE_PER_CPU(char *, irq_stack_ptr) = 13444950d6d4SJosh Poimboeuf init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE; 1345bdf977b3STejun Heo 1346277d5b40SAndi Kleen DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1; 1347d5494d4fSYinghai Lu 1348c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT; 1349c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count); 1350c2daa3beSPeter Zijlstra 13510f3fa48aSIngo Molnar /* 13520f3fa48aSIngo Molnar * Special IST stacks which the CPU switches to when it calls 13530f3fa48aSIngo Molnar * an IST-marked descriptor entry. Up to 7 stacks (hardware 13540f3fa48aSIngo Molnar * limit), all of them are 4K, except the debug stack which 13550f3fa48aSIngo Molnar * is 8K. 13560f3fa48aSIngo Molnar */ 13570f3fa48aSIngo Molnar static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = { 13580f3fa48aSIngo Molnar [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ, 13590f3fa48aSIngo Molnar [DEBUG_STACK - 1] = DEBUG_STKSZ 13600f3fa48aSIngo Molnar }; 13610f3fa48aSIngo Molnar 136292d65b23SBrian Gerst static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks 13633e352aa8STejun Heo [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]); 1364d5494d4fSYinghai Lu 1365d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */ 1366d5494d4fSYinghai Lu void syscall_init(void) 1367d5494d4fSYinghai Lu { 136831ac34caSBorislav Petkov wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS); 136947edb651SAndy Lutomirski wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64); 1370d56fe4bfSIngo Molnar 1371d56fe4bfSIngo Molnar #ifdef CONFIG_IA32_EMULATION 137247edb651SAndy Lutomirski wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat); 1373a76c7f46SDenys Vlasenko /* 1374487d1edbSDenys Vlasenko * This only works on Intel CPUs. 1375487d1edbSDenys Vlasenko * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP. 1376487d1edbSDenys Vlasenko * This does not cause SYSENTER to jump to the wrong location, because 1377487d1edbSDenys Vlasenko * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit). 1378a76c7f46SDenys Vlasenko */ 1379a76c7f46SDenys Vlasenko wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS); 1380a76c7f46SDenys Vlasenko wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL); 13814c8cd0c5SIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat); 1382d56fe4bfSIngo Molnar #else 138347edb651SAndy Lutomirski wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret); 13846b51311cSBorislav Petkov wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG); 1385d56fe4bfSIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL); 1386d56fe4bfSIngo Molnar wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL); 1387d5494d4fSYinghai Lu #endif 1388d5494d4fSYinghai Lu 1389d5494d4fSYinghai Lu /* Flags to clear on syscall */ 1390d5494d4fSYinghai Lu wrmsrl(MSR_SYSCALL_MASK, 139163bcff2aSH. Peter Anvin X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF| 13928c7aa698SAndy Lutomirski X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT); 1393d5494d4fSYinghai Lu } 1394d5494d4fSYinghai Lu 1395d5494d4fSYinghai Lu /* 1396d5494d4fSYinghai Lu * Copies of the original ist values from the tss are only accessed during 1397d5494d4fSYinghai Lu * debugging, no special alignment required. 1398d5494d4fSYinghai Lu */ 1399d5494d4fSYinghai Lu DEFINE_PER_CPU(struct orig_ist, orig_ist); 1400d5494d4fSYinghai Lu 1401228bdaa9SSteven Rostedt static DEFINE_PER_CPU(unsigned long, debug_stack_addr); 140242181186SSteven Rostedt DEFINE_PER_CPU(int, debug_stack_usage); 1403228bdaa9SSteven Rostedt 1404228bdaa9SSteven Rostedt int is_debug_stack(unsigned long addr) 1405228bdaa9SSteven Rostedt { 140689cbc767SChristoph Lameter return __this_cpu_read(debug_stack_usage) || 140789cbc767SChristoph Lameter (addr <= __this_cpu_read(debug_stack_addr) && 140889cbc767SChristoph Lameter addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ)); 1409228bdaa9SSteven Rostedt } 14100f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(is_debug_stack); 1411228bdaa9SSteven Rostedt 1412629f4f9dSSeiji Aguchi DEFINE_PER_CPU(u32, debug_idt_ctr); 1413f8988175SSteven Rostedt 1414228bdaa9SSteven Rostedt void debug_stack_set_zero(void) 1415228bdaa9SSteven Rostedt { 1416629f4f9dSSeiji Aguchi this_cpu_inc(debug_idt_ctr); 1417629f4f9dSSeiji Aguchi load_current_idt(); 1418228bdaa9SSteven Rostedt } 14190f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_set_zero); 1420228bdaa9SSteven Rostedt 1421228bdaa9SSteven Rostedt void debug_stack_reset(void) 1422228bdaa9SSteven Rostedt { 1423629f4f9dSSeiji Aguchi if (WARN_ON(!this_cpu_read(debug_idt_ctr))) 1424f8988175SSteven Rostedt return; 1425629f4f9dSSeiji Aguchi if (this_cpu_dec_return(debug_idt_ctr) == 0) 1426629f4f9dSSeiji Aguchi load_current_idt(); 1427228bdaa9SSteven Rostedt } 14280f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_reset); 1429228bdaa9SSteven Rostedt 14300f3fa48aSIngo Molnar #else /* CONFIG_X86_64 */ 1431d5494d4fSYinghai Lu 1432bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task; 1433bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task); 1434c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT; 1435c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count); 1436bdf977b3STejun Heo 1437a7fcf28dSAndy Lutomirski /* 1438a7fcf28dSAndy Lutomirski * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find 1439a7fcf28dSAndy Lutomirski * the top of the kernel stack. Use an extra percpu variable to track the 1440a7fcf28dSAndy Lutomirski * top of the kernel stack directly. 1441a7fcf28dSAndy Lutomirski */ 1442a7fcf28dSAndy Lutomirski DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) = 1443a7fcf28dSAndy Lutomirski (unsigned long)&init_thread_union + THREAD_SIZE; 1444a7fcf28dSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack); 1445a7fcf28dSAndy Lutomirski 144660a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR 144753f82452SJeremy Fitzhardinge DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary); 144860a5317fSTejun Heo #endif 144960a5317fSTejun Heo 14500f3fa48aSIngo Molnar #endif /* CONFIG_X86_64 */ 1451f7627e25SThomas Gleixner 1452f7627e25SThomas Gleixner /* 14539766cdbcSJaswinder Singh Rajput * Clear all 6 debug registers: 14549766cdbcSJaswinder Singh Rajput */ 14559766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void) 14569766cdbcSJaswinder Singh Rajput { 14579766cdbcSJaswinder Singh Rajput int i; 14589766cdbcSJaswinder Singh Rajput 14599766cdbcSJaswinder Singh Rajput for (i = 0; i < 8; i++) { 14609766cdbcSJaswinder Singh Rajput /* Ignore db4, db5 */ 14619766cdbcSJaswinder Singh Rajput if ((i == 4) || (i == 5)) 14629766cdbcSJaswinder Singh Rajput continue; 14639766cdbcSJaswinder Singh Rajput 14649766cdbcSJaswinder Singh Rajput set_debugreg(0, i); 14659766cdbcSJaswinder Singh Rajput } 14669766cdbcSJaswinder Singh Rajput } 1467f7627e25SThomas Gleixner 14680bb9fef9SJason Wessel #ifdef CONFIG_KGDB 14690bb9fef9SJason Wessel /* 14700bb9fef9SJason Wessel * Restore debug regs if using kgdbwait and you have a kernel debugger 14710bb9fef9SJason Wessel * connection established. 14720bb9fef9SJason Wessel */ 14730bb9fef9SJason Wessel static void dbg_restore_debug_regs(void) 14740bb9fef9SJason Wessel { 14750bb9fef9SJason Wessel if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break)) 14760bb9fef9SJason Wessel arch_kgdb_ops.correct_hw_break(); 14770bb9fef9SJason Wessel } 14780bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */ 14790bb9fef9SJason Wessel #define dbg_restore_debug_regs() 14800bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */ 14810bb9fef9SJason Wessel 1482ce4b1b16SIgor Mammedov static void wait_for_master_cpu(int cpu) 1483ce4b1b16SIgor Mammedov { 1484ce4b1b16SIgor Mammedov #ifdef CONFIG_SMP 1485ce4b1b16SIgor Mammedov /* 1486ce4b1b16SIgor Mammedov * wait for ACK from master CPU before continuing 1487ce4b1b16SIgor Mammedov * with AP initialization 1488ce4b1b16SIgor Mammedov */ 1489ce4b1b16SIgor Mammedov WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)); 1490ce4b1b16SIgor Mammedov while (!cpumask_test_cpu(cpu, cpu_callout_mask)) 1491ce4b1b16SIgor Mammedov cpu_relax(); 1492ce4b1b16SIgor Mammedov #endif 1493ce4b1b16SIgor Mammedov } 1494ce4b1b16SIgor Mammedov 1495f7627e25SThomas Gleixner /* 1496f7627e25SThomas Gleixner * cpu_init() initializes state that is per-CPU. Some data is already 1497f7627e25SThomas Gleixner * initialized (naturally) in the bootstrap process, such as the GDT 1498f7627e25SThomas Gleixner * and IDT. We reload them nevertheless, this function acts as a 1499f7627e25SThomas Gleixner * 'CPU state barrier', nothing should get across. 15001ba76586SYinghai Lu * A lot of state is already set up in PDA init for 64 bit 1501f7627e25SThomas Gleixner */ 15021ba76586SYinghai Lu #ifdef CONFIG_X86_64 15030f3fa48aSIngo Molnar 1504148f9bb8SPaul Gortmaker void cpu_init(void) 15051ba76586SYinghai Lu { 15060fe1e009STejun Heo struct orig_ist *oist; 15071ba76586SYinghai Lu struct task_struct *me; 15080f3fa48aSIngo Molnar struct tss_struct *t; 15090f3fa48aSIngo Molnar unsigned long v; 1510fb59831bSAndy Lutomirski int cpu = raw_smp_processor_id(); 15111ba76586SYinghai Lu int i; 15121ba76586SYinghai Lu 1513ce4b1b16SIgor Mammedov wait_for_master_cpu(cpu); 1514ce4b1b16SIgor Mammedov 1515e6ebf5deSFenghua Yu /* 15161e02ce4cSAndy Lutomirski * Initialize the CR4 shadow before doing anything that could 15171e02ce4cSAndy Lutomirski * try to read it. 15181e02ce4cSAndy Lutomirski */ 15191e02ce4cSAndy Lutomirski cr4_init_shadow(); 15201e02ce4cSAndy Lutomirski 1521777284b6SBorislav Petkov if (cpu) 1522e6ebf5deSFenghua Yu load_ucode_ap(); 1523e6ebf5deSFenghua Yu 152424933b82SAndy Lutomirski t = &per_cpu(cpu_tss, cpu); 15250fe1e009STejun Heo oist = &per_cpu(orig_ist, cpu); 15260f3fa48aSIngo Molnar 1527e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA 152827fd185fSFenghua Yu if (this_cpu_read(numa_node) == 0 && 1529e534c7c5SLee Schermerhorn early_cpu_to_node(cpu) != NUMA_NO_NODE) 1530e534c7c5SLee Schermerhorn set_numa_node(early_cpu_to_node(cpu)); 1531e7a22c1eSBrian Gerst #endif 15321ba76586SYinghai Lu 15331ba76586SYinghai Lu me = current; 15341ba76586SYinghai Lu 15352eaad1fdSMike Travis pr_debug("Initializing CPU#%d\n", cpu); 15361ba76586SYinghai Lu 1537375074ccSAndy Lutomirski cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); 15381ba76586SYinghai Lu 15391ba76586SYinghai Lu /* 15401ba76586SYinghai Lu * Initialize the per-CPU GDT with the boot GDT, 15411ba76586SYinghai Lu * and set up the GDT descriptor: 15421ba76586SYinghai Lu */ 15431ba76586SYinghai Lu 1544552be871SBrian Gerst switch_to_new_gdt(cpu); 15452697fbd5SBrian Gerst loadsegment(fs, 0); 15462697fbd5SBrian Gerst 1547cf910e83SSeiji Aguchi load_current_idt(); 15481ba76586SYinghai Lu 15491ba76586SYinghai Lu memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8); 15501ba76586SYinghai Lu syscall_init(); 15511ba76586SYinghai Lu 15521ba76586SYinghai Lu wrmsrl(MSR_FS_BASE, 0); 15531ba76586SYinghai Lu wrmsrl(MSR_KERNEL_GS_BASE, 0); 15541ba76586SYinghai Lu barrier(); 15551ba76586SYinghai Lu 15564763ed4dSH. Peter Anvin x86_configure_nx(); 1557659006bfSThomas Gleixner x2apic_setup(); 15581ba76586SYinghai Lu 15591ba76586SYinghai Lu /* 15601ba76586SYinghai Lu * set up and load the per-CPU TSS 15611ba76586SYinghai Lu */ 15620fe1e009STejun Heo if (!oist->ist[0]) { 156392d65b23SBrian Gerst char *estacks = per_cpu(exception_stacks, cpu); 15640f3fa48aSIngo Molnar 15651ba76586SYinghai Lu for (v = 0; v < N_EXCEPTION_STACKS; v++) { 15660f3fa48aSIngo Molnar estacks += exception_stack_sizes[v]; 15670fe1e009STejun Heo oist->ist[v] = t->x86_tss.ist[v] = 15681ba76586SYinghai Lu (unsigned long)estacks; 1569228bdaa9SSteven Rostedt if (v == DEBUG_STACK-1) 1570228bdaa9SSteven Rostedt per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks; 15711ba76586SYinghai Lu } 15721ba76586SYinghai Lu } 15731ba76586SYinghai Lu 15741ba76586SYinghai Lu t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap); 15750f3fa48aSIngo Molnar 15761ba76586SYinghai Lu /* 15771ba76586SYinghai Lu * <= is required because the CPU will access up to 15781ba76586SYinghai Lu * 8 bits beyond the end of the IO permission bitmap. 15791ba76586SYinghai Lu */ 15801ba76586SYinghai Lu for (i = 0; i <= IO_BITMAP_LONGS; i++) 15811ba76586SYinghai Lu t->io_bitmap[i] = ~0UL; 15821ba76586SYinghai Lu 1583f1f10076SVegard Nossum mmgrab(&init_mm); 15841ba76586SYinghai Lu me->active_mm = &init_mm; 15858c5dfd25SStoyan Gaydarov BUG_ON(me->mm); 1586*72c0098dSAndy Lutomirski initialize_tlbstate_and_flush(); 15871ba76586SYinghai Lu enter_lazy_tlb(&init_mm, me); 15881ba76586SYinghai Lu 15891ba76586SYinghai Lu load_sp0(t, ¤t->thread); 15901ba76586SYinghai Lu set_tss_desc(cpu, t); 15911ba76586SYinghai Lu load_TR_desc(); 159237868fe1SAndy Lutomirski load_mm_ldt(&init_mm); 15931ba76586SYinghai Lu 15949766cdbcSJaswinder Singh Rajput clear_all_debug_regs(); 15950bb9fef9SJason Wessel dbg_restore_debug_regs(); 15961ba76586SYinghai Lu 159721c4cd10SIngo Molnar fpu__init_cpu(); 15981ba76586SYinghai Lu 15991ba76586SYinghai Lu if (is_uv_system()) 16001ba76586SYinghai Lu uv_cpu_init(); 160169218e47SThomas Garnier 160269218e47SThomas Garnier setup_fixmap_gdt(cpu); 160369218e47SThomas Garnier load_fixmap_gdt(cpu); 16041ba76586SYinghai Lu } 16051ba76586SYinghai Lu 16061ba76586SYinghai Lu #else 16071ba76586SYinghai Lu 1608148f9bb8SPaul Gortmaker void cpu_init(void) 1609f7627e25SThomas Gleixner { 1610f7627e25SThomas Gleixner int cpu = smp_processor_id(); 1611f7627e25SThomas Gleixner struct task_struct *curr = current; 161224933b82SAndy Lutomirski struct tss_struct *t = &per_cpu(cpu_tss, cpu); 1613f7627e25SThomas Gleixner struct thread_struct *thread = &curr->thread; 1614f7627e25SThomas Gleixner 1615ce4b1b16SIgor Mammedov wait_for_master_cpu(cpu); 1616e6ebf5deSFenghua Yu 16175b2bdbc8SSteven Rostedt /* 16185b2bdbc8SSteven Rostedt * Initialize the CR4 shadow before doing anything that could 16195b2bdbc8SSteven Rostedt * try to read it. 16205b2bdbc8SSteven Rostedt */ 16215b2bdbc8SSteven Rostedt cr4_init_shadow(); 16225b2bdbc8SSteven Rostedt 1623ce4b1b16SIgor Mammedov show_ucode_info_early(); 1624f7627e25SThomas Gleixner 16251b74dde7SChen Yucong pr_info("Initializing CPU#%d\n", cpu); 1626f7627e25SThomas Gleixner 1627362f924bSBorislav Petkov if (cpu_feature_enabled(X86_FEATURE_VME) || 162859e21e3dSBorislav Petkov boot_cpu_has(X86_FEATURE_TSC) || 1629362f924bSBorislav Petkov boot_cpu_has(X86_FEATURE_DE)) 1630375074ccSAndy Lutomirski cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); 1631f7627e25SThomas Gleixner 1632cf910e83SSeiji Aguchi load_current_idt(); 1633552be871SBrian Gerst switch_to_new_gdt(cpu); 1634f7627e25SThomas Gleixner 1635f7627e25SThomas Gleixner /* 1636f7627e25SThomas Gleixner * Set up and load the per-CPU TSS and LDT 1637f7627e25SThomas Gleixner */ 1638f1f10076SVegard Nossum mmgrab(&init_mm); 1639f7627e25SThomas Gleixner curr->active_mm = &init_mm; 16408c5dfd25SStoyan Gaydarov BUG_ON(curr->mm); 1641*72c0098dSAndy Lutomirski initialize_tlbstate_and_flush(); 1642f7627e25SThomas Gleixner enter_lazy_tlb(&init_mm, curr); 1643f7627e25SThomas Gleixner 1644faca6227SH. Peter Anvin load_sp0(t, thread); 1645f7627e25SThomas Gleixner set_tss_desc(cpu, t); 1646f7627e25SThomas Gleixner load_TR_desc(); 164737868fe1SAndy Lutomirski load_mm_ldt(&init_mm); 1648f7627e25SThomas Gleixner 1649f9a196b8SThomas Gleixner t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap); 1650f9a196b8SThomas Gleixner 1651f7627e25SThomas Gleixner #ifdef CONFIG_DOUBLEFAULT 1652f7627e25SThomas Gleixner /* Set up doublefault TSS pointer in the GDT */ 1653f7627e25SThomas Gleixner __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss); 1654f7627e25SThomas Gleixner #endif 1655f7627e25SThomas Gleixner 16569766cdbcSJaswinder Singh Rajput clear_all_debug_regs(); 16570bb9fef9SJason Wessel dbg_restore_debug_regs(); 1658f7627e25SThomas Gleixner 165921c4cd10SIngo Molnar fpu__init_cpu(); 166069218e47SThomas Garnier 166169218e47SThomas Garnier setup_fixmap_gdt(cpu); 166269218e47SThomas Garnier load_fixmap_gdt(cpu); 1663f7627e25SThomas Gleixner } 16641ba76586SYinghai Lu #endif 16655700f743SBorislav Petkov 1666b51ef52dSLaura Abbott static void bsp_resume(void) 1667b51ef52dSLaura Abbott { 1668b51ef52dSLaura Abbott if (this_cpu->c_bsp_resume) 1669b51ef52dSLaura Abbott this_cpu->c_bsp_resume(&boot_cpu_data); 1670b51ef52dSLaura Abbott } 1671b51ef52dSLaura Abbott 1672b51ef52dSLaura Abbott static struct syscore_ops cpu_syscore_ops = { 1673b51ef52dSLaura Abbott .resume = bsp_resume, 1674b51ef52dSLaura Abbott }; 1675b51ef52dSLaura Abbott 1676b51ef52dSLaura Abbott static int __init init_cpu_syscore(void) 1677b51ef52dSLaura Abbott { 1678b51ef52dSLaura Abbott register_syscore_ops(&cpu_syscore_ops); 1679b51ef52dSLaura Abbott return 0; 1680b51ef52dSLaura Abbott } 1681b51ef52dSLaura Abbott core_initcall(init_cpu_syscore); 1682