blob: c755173319893e11d6ac7bf64f0e7ea96d89c017 [file] [log] [blame]
Yinghai Luf0fc4af2008-09-04 20:09:00 -07001#include <linux/bootmem.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05302#include <linux/linkage.h>
Yinghai Luf0fc4af2008-09-04 20:09:00 -07003#include <linux/bitops.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05304#include <linux/kernel.h>
Yinghai Luf0fc4af2008-09-04 20:09:00 -07005#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/percpu.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05307#include <linux/string.h>
Borislav Petkovee098e12015-06-01 12:06:57 +02008#include <linux/ctype.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05309#include <linux/delay.h>
10#include <linux/sched.h>
11#include <linux/init.h>
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +090012#include <linux/kprobes.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053013#include <linux/kgdb.h>
14#include <linux/smp.h>
15#include <linux/io.h>
Laura Abbottb51ef522015-07-20 14:47:58 -070016#include <linux/syscore_ops.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053017
18#include <asm/stackprotector.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020019#include <asm/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/mmu_context.h>
H. Peter Anvin49d859d2011-07-31 14:02:19 -070021#include <asm/archrandom.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053022#include <asm/hypervisor.h>
23#include <asm/processor.h>
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070024#include <asm/tlbflush.h>
Paul Gortmakerf649e932012-01-20 16:24:09 -050025#include <asm/debugreg.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053026#include <asm/sections.h>
Andy Lutomirskif40c3302014-05-05 12:19:36 -070027#include <asm/vsyscall.h>
Alan Cox8bdbd962009-07-04 00:35:45 +010028#include <linux/topology.h>
29#include <linux/cpumask.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053030#include <asm/pgtable.h>
Arun Sharma600634972011-07-26 16:09:06 -070031#include <linux/atomic.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053032#include <asm/proto.h>
33#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/apic.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053035#include <asm/desc.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020036#include <asm/fpu/internal.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053037#include <asm/mtrr.h>
Alan Cox8bdbd962009-07-04 00:35:45 +010038#include <linux/numa.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053039#include <asm/asm.h>
40#include <asm/cpu.h>
41#include <asm/mce.h>
42#include <asm/msr.h>
43#include <asm/pat.h>
Fenghua Yud288e1c2012-12-20 23:44:23 -080044#include <asm/microcode.h>
45#include <asm/microcode_intel.h>
Ingo Molnare641f5f2009-02-17 14:02:01 +010046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#ifdef CONFIG_X86_LOCAL_APIC
Tejun Heobdbcdd42009-01-21 17:26:06 +090048#include <asm/uv/uv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#endif
50
51#include "cpu.h"
52
Mike Travisc2d1cec2009-01-04 05:18:03 -080053/* all of these masks are initialized in setup_cpu_local_masks() */
Mike Travisc2d1cec2009-01-04 05:18:03 -080054cpumask_var_t cpu_initialized_mask;
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053055cpumask_var_t cpu_callout_mask;
56cpumask_var_t cpu_callin_mask;
Mike Travisc2d1cec2009-01-04 05:18:03 -080057
58/* representing cpus for which sibling maps can be computed */
59cpumask_var_t cpu_sibling_setup_mask;
60
Brian Gerst2f2f52b2009-01-27 12:56:47 +090061/* correctly size the local cpu masks */
Ingo Molnar4369f1f2009-01-27 12:03:24 +010062void __init setup_cpu_local_masks(void)
Brian Gerst2f2f52b2009-01-27 12:56:47 +090063{
64 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
65 alloc_bootmem_cpumask_var(&cpu_callin_mask);
66 alloc_bootmem_cpumask_var(&cpu_callout_mask);
67 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
68}
69
Paul Gortmaker148f9bb2013-06-18 18:23:59 -040070static void default_init(struct cpuinfo_x86 *c)
Ondrej Zarye8055132009-08-11 20:00:11 +020071{
72#ifdef CONFIG_X86_64
Borislav Petkov27c13ec2009-11-21 14:01:45 +010073 cpu_detect_cache_sizes(c);
Ondrej Zarye8055132009-08-11 20:00:11 +020074#else
75 /* Not much we can do here... */
76 /* Check if at least it has cpuid */
77 if (c->cpuid_level == -1) {
78 /* No cpuid. It must be an ancient CPU */
79 if (c->x86 == 4)
80 strcpy(c->x86_model_id, "486");
81 else if (c->x86 == 3)
82 strcpy(c->x86_model_id, "386");
83 }
84#endif
85}
86
Paul Gortmaker148f9bb2013-06-18 18:23:59 -040087static const struct cpu_dev default_cpu = {
Ondrej Zarye8055132009-08-11 20:00:11 +020088 .c_init = default_init,
89 .c_vendor = "Unknown",
90 .c_x86_vendor = X86_VENDOR_UNKNOWN,
91};
92
Paul Gortmaker148f9bb2013-06-18 18:23:59 -040093static const struct cpu_dev *this_cpu = &default_cpu;
Yinghai Lu0a488a52008-09-04 21:09:47 +020094
Brian Gerst06deef82009-01-21 17:26:05 +090095DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
Yinghai Lu950ad7f2008-09-04 20:09:01 -070096#ifdef CONFIG_X86_64
Brian Gerst06deef82009-01-21 17:26:05 +090097 /*
98 * We need valid kernel segments for data and code in long mode too
99 * IRET will check the segment types kkeil 2000/10/28
100 * Also sysret mandates a special GDT layout
101 *
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530102 * TLS descriptors are currently at a different place compared to i386.
Brian Gerst06deef82009-01-21 17:26:05 +0900103 * Hopefully nobody expects them at a fixed place (Wine?)
104 */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900105 [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
106 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
107 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
108 [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
109 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
110 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
Yinghai Lu950ad7f2008-09-04 20:09:01 -0700111#else
Akinobu Mita1e5de182009-07-19 00:12:20 +0900112 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
113 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
114 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
115 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200116 /*
117 * Segments used for calling PnP BIOS have byte granularity.
118 * They code segments and data segments have fixed 64k limits,
119 * the transfer segment sizes are set at run time.
120 */
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100121 /* 32-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900122 [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100123 /* 16-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900124 [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100125 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900126 [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100127 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900128 [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100129 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900130 [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
Rusty Russellbf5046722007-05-02 19:27:10 +0200131 /*
132 * The APM segments have byte granularity and their bases
133 * are set at run time. All have 64k limits.
134 */
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100135 /* 32-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900136 [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200137 /* 16-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900138 [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100139 /* data */
Ingo Molnar72c4d852009-08-03 08:47:07 +0200140 [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200141
Akinobu Mita1e5de182009-07-19 00:12:20 +0900142 [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
143 [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
Tejun Heo60a53172009-02-09 22:17:40 +0900144 GDT_STACK_CANARY_INIT
Yinghai Lu950ad7f2008-09-04 20:09:01 -0700145#endif
Brian Gerst06deef82009-01-21 17:26:05 +0900146} };
Jeremy Fitzhardinge7a61d352007-05-02 19:27:15 +0200147EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
Rusty Russellae1ee112007-05-02 19:27:10 +0200148
Dave Hansen8c3641e2015-06-07 11:37:02 -0700149static int __init x86_mpx_setup(char *s)
Suresh Siddha0c752a92009-05-22 12:17:45 -0700150{
Dave Hansen8c3641e2015-06-07 11:37:02 -0700151 /* require an exact match without trailing characters */
Dave Hansen2cd39492014-11-11 14:01:33 -0800152 if (strlen(s))
153 return 0;
Suresh Siddha0c752a92009-05-22 12:17:45 -0700154
Dave Hansen8c3641e2015-06-07 11:37:02 -0700155 /* do not emit a message if the feature is not present */
156 if (!boot_cpu_has(X86_FEATURE_MPX))
157 return 1;
Suresh Siddha6bad06b2010-07-19 16:05:52 -0700158
Dave Hansen8c3641e2015-06-07 11:37:02 -0700159 setup_clear_cpu_cap(X86_FEATURE_MPX);
160 pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
Fenghua Yub6f42a42014-05-29 11:12:31 -0700161 return 1;
162}
Dave Hansen8c3641e2015-06-07 11:37:02 -0700163__setup("nompx", x86_mpx_setup);
Fenghua Yub6f42a42014-05-29 11:12:31 -0700164
Yinghai Luba51dce2008-09-04 20:09:02 -0700165#ifdef CONFIG_X86_32
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400166static int cachesize_override = -1;
167static int disable_x86_serial_nr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static int __init cachesize_setup(char *str)
170{
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100171 get_option(&str, &cachesize_override);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return 1;
173}
174__setup("cachesize=", cachesize_setup);
175
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100176static int __init x86_sep_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Andi Kleen13530252008-01-30 13:33:20 +0100178 setup_clear_cpu_cap(X86_FEATURE_SEP);
Chuck Ebbert4f886512006-03-23 02:59:34 -0800179 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
Chuck Ebbert4f886512006-03-23 02:59:34 -0800181__setup("nosep", x86_sep_setup);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/* Standard macro to see if a specific flag is changeable */
184static inline int flag_is_changeable_p(u32 flag)
185{
186 u32 f1, f2;
187
Krzysztof Helt94f6bac2008-09-30 23:17:51 +0200188 /*
189 * Cyrix and IDT cpus allow disabling of CPUID
190 * so the code below may return different results
191 * when it is executed before and after enabling
192 * the CPUID. Add "volatile" to not allow gcc to
193 * optimize the subsequent calls to this function.
194 */
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100195 asm volatile ("pushfl \n\t"
196 "pushfl \n\t"
197 "popl %0 \n\t"
198 "movl %0, %1 \n\t"
199 "xorl %2, %0 \n\t"
200 "pushl %0 \n\t"
201 "popfl \n\t"
202 "pushfl \n\t"
203 "popl %0 \n\t"
204 "popfl \n\t"
205
Krzysztof Helt94f6bac2008-09-30 23:17:51 +0200206 : "=&r" (f1), "=&r" (f2)
207 : "ir" (flag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 return ((f1^f2) & flag) != 0;
210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/* Probe for the CPUID instruction */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400213int have_cpuid_p(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 return flag_is_changeable_p(X86_EFLAGS_ID);
216}
217
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400218static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
Yinghai Lu0a488a52008-09-04 21:09:47 +0200219{
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100220 unsigned long lo, hi;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200221
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100222 if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
223 return;
224
225 /* Disable processor serial number: */
226
227 rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
228 lo |= 0x200000;
229 wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
230
231 printk(KERN_NOTICE "CPU serial number disabled.\n");
232 clear_cpu_cap(c, X86_FEATURE_PN);
233
234 /* Disabling the serial number may affect the cpuid level */
235 c->cpuid_level = cpuid_eax(0);
Yinghai Lu0a488a52008-09-04 21:09:47 +0200236}
237
238static int __init x86_serial_nr_setup(char *s)
239{
240 disable_x86_serial_nr = 0;
241 return 1;
242}
243__setup("serialnumber", x86_serial_nr_setup);
Yinghai Luba51dce2008-09-04 20:09:02 -0700244#else
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700245static inline int flag_is_changeable_p(u32 flag)
246{
247 return 1;
248}
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700249static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
250{
251}
Yinghai Luba51dce2008-09-04 20:09:02 -0700252#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Fenghua Yude5397a2011-05-11 16:51:05 -0700254static __init int setup_disable_smep(char *arg)
255{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700256 setup_clear_cpu_cap(X86_FEATURE_SMEP);
Fenghua Yude5397a2011-05-11 16:51:05 -0700257 return 1;
258}
259__setup("nosmep", setup_disable_smep);
260
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700261static __always_inline void setup_smep(struct cpuinfo_x86 *c)
Fenghua Yude5397a2011-05-11 16:51:05 -0700262{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700263 if (cpu_has(c, X86_FEATURE_SMEP))
Andy Lutomirski375074c2014-10-24 15:58:07 -0700264 cr4_set_bits(X86_CR4_SMEP);
Fenghua Yude5397a2011-05-11 16:51:05 -0700265}
266
H. Peter Anvin52b61792012-09-21 12:43:13 -0700267static __init int setup_disable_smap(char *arg)
268{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700269 setup_clear_cpu_cap(X86_FEATURE_SMAP);
H. Peter Anvin52b61792012-09-21 12:43:13 -0700270 return 1;
271}
272__setup("nosmap", setup_disable_smap);
273
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700274static __always_inline void setup_smap(struct cpuinfo_x86 *c)
H. Peter Anvin52b61792012-09-21 12:43:13 -0700275{
Andrew Cooper581b7f152015-06-03 10:31:14 +0100276 unsigned long eflags = native_save_fl();
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700277
278 /* This should have been cleared long ago */
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700279 BUG_ON(eflags & X86_EFLAGS_AC);
280
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800281 if (cpu_has(c, X86_FEATURE_SMAP)) {
282#ifdef CONFIG_X86_SMAP
Andy Lutomirski375074c2014-10-24 15:58:07 -0700283 cr4_set_bits(X86_CR4_SMAP);
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800284#else
Andy Lutomirski375074c2014-10-24 15:58:07 -0700285 cr4_clear_bits(X86_CR4_SMAP);
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800286#endif
287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/*
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800291 * Some CPU features depend on higher CPUID levels, which may not always
292 * be available due to CPUID level capping or broken virtualization
293 * software. Add those features to this table to auto-disable them.
294 */
295struct cpuid_dependent_feature {
296 u32 feature;
297 u32 level;
298};
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100299
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400300static const struct cpuid_dependent_feature
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800301cpuid_dependent_features[] = {
302 { X86_FEATURE_MWAIT, 0x00000005 },
303 { X86_FEATURE_DCA, 0x00000009 },
304 { X86_FEATURE_XSAVE, 0x0000000d },
305 { 0, 0 }
306};
307
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400308static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800309{
310 const struct cpuid_dependent_feature *df;
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530311
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800312 for (df = cpuid_dependent_features; df->feature; df++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100313
314 if (!cpu_has(c, df->feature))
315 continue;
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800316 /*
317 * Note: cpuid_level is set to -1 if unavailable, but
318 * extended_extended_level is set to 0 if unavailable
319 * and the legitimate extended levels are all negative
320 * when signed; hence the weird messing around with
321 * signs here...
322 */
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100323 if (!((s32)df->level < 0 ?
Yinghai Luf6db44d2009-02-14 23:59:18 -0800324 (u32)df->level > (u32)c->extended_cpuid_level :
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100325 (s32)df->level > (s32)c->cpuid_level))
326 continue;
327
328 clear_cpu_cap(c, df->feature);
329 if (!warn)
330 continue;
331
332 printk(KERN_WARNING
Josh Triplett9def39be2013-10-30 08:09:45 -0700333 "CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
334 x86_cap_flag(df->feature), df->level);
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800335 }
Yinghai Luf6db44d2009-02-14 23:59:18 -0800336}
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800337
338/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * Naming convention should be: <Name> [(<Codename>)]
340 * This table only is used unless init_<vendor>() below doesn't set it;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100341 * in particular, if CPUID levels 0x80000002..4 are supported, this
342 * isn't used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
344
345/* Look up CPU names by table lookup. */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400346static const char *table_lookup_model(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Jan Beulich09dc68d2013-10-21 09:35:20 +0100348#ifdef CONFIG_X86_32
349 const struct legacy_cpu_model_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if (c->x86_model >= 16)
352 return NULL; /* Range check */
353
354 if (!this_cpu)
355 return NULL;
356
Jan Beulich09dc68d2013-10-21 09:35:20 +0100357 info = this_cpu->legacy_models;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Jan Beulich09dc68d2013-10-21 09:35:20 +0100359 while (info->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (info->family == c->x86)
361 return info->model_names[c->x86_model];
362 info++;
363 }
Jan Beulich09dc68d2013-10-21 09:35:20 +0100364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return NULL; /* Not found */
366}
367
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400368__u32 cpu_caps_cleared[NCAPINTS];
369__u32 cpu_caps_set[NCAPINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900371void load_percpu_segment(int cpu)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200372{
Yinghai Lufab334c2008-09-04 20:09:05 -0700373#ifdef CONFIG_X86_32
Brian Gerst2697fbd2009-01-27 12:56:48 +0900374 loadsegment(fs, __KERNEL_PERCPU);
375#else
376 loadsegment(gs, 0);
377 wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
Yinghai Lufab334c2008-09-04 20:09:05 -0700378#endif
Tejun Heo60a53172009-02-09 22:17:40 +0900379 load_stack_canary_segment();
Yinghai Lu9d31d352008-09-04 21:09:44 +0200380}
381
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100382/*
383 * Current gdt points %fs at the "master" per-cpu area: after this,
384 * it's on the real one.
385 */
Brian Gerst552be872009-01-30 17:47:53 +0900386void switch_to_new_gdt(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 struct desc_ptr gdt_descr;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 gdt_descr.address = (long)get_cpu_gdt_table(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 gdt_descr.size = GDT_SIZE - 1;
392 load_gdt(&gdt_descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* Reload the per-cpu base */
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900394
395 load_percpu_segment(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400398static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400400static void get_model_name(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 unsigned int *v;
Borislav Petkovee098e12015-06-01 12:06:57 +0200403 char *p, *q, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Yinghai Lu3da99c92008-09-04 21:09:44 +0200405 if (c->extended_cpuid_level < 0x80000004)
Yinghai Lu1b05d602008-09-06 01:52:27 -0700406 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100408 v = (unsigned int *)c->x86_model_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
410 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
411 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
412 c->x86_model_id[48] = 0;
413
Borislav Petkovee098e12015-06-01 12:06:57 +0200414 /* Trim whitespace */
415 p = q = s = &c->x86_model_id[0];
416
417 while (*p == ' ')
418 p++;
419
420 while (*p) {
421 /* Note the last non-whitespace index */
422 if (!isspace(*p))
423 s = q;
424
425 *q++ = *p++;
426 }
427
428 *(s + 1) = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400431void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Yinghai Lu9d31d352008-09-04 21:09:44 +0200433 unsigned int n, dummy, ebx, ecx, edx, l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Yinghai Lu3da99c92008-09-04 21:09:44 +0200435 n = c->extended_cpuid_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (n >= 0x80000005) {
Yinghai Lu9d31d352008-09-04 21:09:44 +0200438 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
Yinghai Lu9d31d352008-09-04 21:09:44 +0200439 c->x86_cache_size = (ecx>>24) + (edx>>24);
Yinghai Lu140fc722008-09-04 20:09:07 -0700440#ifdef CONFIG_X86_64
441 /* On K8 L1 TLB is inclusive, so don't count it */
442 c->x86_tlbsize = 0;
443#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
446 if (n < 0x80000006) /* Some chips just has a large L1. */
447 return;
448
Yinghai Lu0a488a52008-09-04 21:09:47 +0200449 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 l2size = ecx >> 16;
451
Yinghai Lu140fc722008-09-04 20:09:07 -0700452#ifdef CONFIG_X86_64
453 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
454#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /* do processor-specific cache resizing */
Jan Beulich09dc68d2013-10-21 09:35:20 +0100456 if (this_cpu->legacy_cache_size)
457 l2size = this_cpu->legacy_cache_size(c, l2size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* Allow user to override all this if necessary. */
460 if (cachesize_override != -1)
461 l2size = cachesize_override;
462
463 if (l2size == 0)
464 return; /* Again, no L2 cache is possible */
Yinghai Lu140fc722008-09-04 20:09:07 -0700465#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 c->x86_cache_size = l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Alex Shie0ba94f2012-06-28 09:02:16 +0800470u16 __read_mostly tlb_lli_4k[NR_INFO];
471u16 __read_mostly tlb_lli_2m[NR_INFO];
472u16 __read_mostly tlb_lli_4m[NR_INFO];
473u16 __read_mostly tlb_lld_4k[NR_INFO];
474u16 __read_mostly tlb_lld_2m[NR_INFO];
475u16 __read_mostly tlb_lld_4m[NR_INFO];
Kirill A. Shutemovdd360392013-12-23 14:16:58 +0200476u16 __read_mostly tlb_lld_1g[NR_INFO];
Alex Shie0ba94f2012-06-28 09:02:16 +0800477
Steven Honeymanf94fe112014-11-05 22:52:18 +0000478static void cpu_detect_tlb(struct cpuinfo_x86 *c)
Alex Shie0ba94f2012-06-28 09:02:16 +0800479{
480 if (this_cpu->c_detect_tlb)
481 this_cpu->c_detect_tlb(c);
482
Steven Honeymanf94fe112014-11-05 22:52:18 +0000483 pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
Alex Shie0ba94f2012-06-28 09:02:16 +0800484 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
Steven Honeymanf94fe112014-11-05 22:52:18 +0000485 tlb_lli_4m[ENTRIES]);
486
487 pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
488 tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
489 tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
Alex Shie0ba94f2012-06-28 09:02:16 +0800490}
491
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400492void detect_ht(struct cpuinfo_x86 *c)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200493{
Borislav Petkovc8e56d22015-06-04 18:55:25 +0200494#ifdef CONFIG_SMP
Yinghai Lu0a488a52008-09-04 21:09:47 +0200495 u32 eax, ebx, ecx, edx;
496 int index_msb, core_bits;
Mike Travis2eaad1f2009-12-10 17:19:36 -0800497 static bool printed;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200498
499 if (!cpu_has(c, X86_FEATURE_HT))
500 return;
501
502 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
503 goto out;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200504
Yinghai Lu1cd78772008-09-04 20:09:08 -0700505 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
506 return;
507
Yinghai Lu9d31d352008-09-04 21:09:44 +0200508 cpuid(1, &eax, &ebx, &ecx, &edx);
509
Yinghai Lu9d31d352008-09-04 21:09:44 +0200510 smp_num_siblings = (ebx & 0xff0000) >> 16;
511
512 if (smp_num_siblings == 1) {
Mike Travis2eaad1f2009-12-10 17:19:36 -0800513 printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100514 goto out;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200515 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200516
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100517 if (smp_num_siblings <= 1)
518 goto out;
519
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100520 index_msb = get_count_order(smp_num_siblings);
521 c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
522
523 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
524
525 index_msb = get_count_order(smp_num_siblings);
526
527 core_bits = get_count_order(c->x86_max_cores);
528
529 c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
530 ((1 << core_bits) - 1);
531
Yinghai Lu0a488a52008-09-04 21:09:47 +0200532out:
Mike Travis2eaad1f2009-12-10 17:19:36 -0800533 if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
Yinghai Lu0a488a52008-09-04 21:09:47 +0200534 printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
535 c->phys_proc_id);
536 printk(KERN_INFO "CPU: Processor Core ID: %d\n",
537 c->cpu_core_id);
Mike Travis2eaad1f2009-12-10 17:19:36 -0800538 printed = 1;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200539 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200540#endif
Yinghai Lu97e4db72008-09-04 20:08:59 -0700541}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400543static void get_cpu_vendor(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 char *v = c->x86_vendor_id;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100546 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 for (i = 0; i < X86_VENDOR_NUM; i++) {
Yinghai Lu10a434f2008-09-04 21:09:45 +0200549 if (!cpu_devs[i])
550 break;
551
552 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
553 (cpu_devs[i]->c_ident[1] &&
554 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100555
Yinghai Lu10a434f2008-09-04 21:09:45 +0200556 this_cpu = cpu_devs[i];
557 c->x86_vendor = this_cpu->c_x86_vendor;
558 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560 }
Yinghai Lu10a434f2008-09-04 21:09:45 +0200561
Minchan Kima9c56952009-06-16 15:33:44 -0700562 printk_once(KERN_ERR
563 "CPU: vendor_id '%s' unknown, using generic init.\n" \
564 "CPU: Your system may be unstable.\n", v);
Yinghai Lu10a434f2008-09-04 21:09:45 +0200565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 c->x86_vendor = X86_VENDOR_UNKNOWN;
567 this_cpu = &default_cpu;
568}
569
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400570void cpu_detect(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* Get vendor name */
Harvey Harrison4a148512008-02-01 17:49:43 +0100573 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
574 (unsigned int *)&c->x86_vendor_id[0],
575 (unsigned int *)&c->x86_vendor_id[8],
576 (unsigned int *)&c->x86_vendor_id[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 c->x86 = 4;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200579 /* Intel-defined flags: level 0x00000001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (c->cpuid_level >= 0x00000001) {
581 u32 junk, tfms, cap0, misc;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
Yinghai Lu9d31d352008-09-04 21:09:44 +0200584 c->x86 = (tfms >> 8) & 0xf;
585 c->x86_model = (tfms >> 4) & 0xf;
586 c->x86_mask = tfms & 0xf;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100587
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100588 if (c->x86 == 0xf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 c->x86 += (tfms >> 20) & 0xff;
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100590 if (c->x86 >= 0x6)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200591 c->x86_model += ((tfms >> 16) & 0xf) << 4;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100592
Huang, Yingd4387bd2008-01-31 22:05:45 +0100593 if (cap0 & (1<<19)) {
Huang, Yingd4387bd2008-01-31 22:05:45 +0100594 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200595 c->x86_cache_alignment = c->x86_clflush_size;
Huang, Yingd4387bd2008-01-31 22:05:45 +0100596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
Yinghai Lu3da99c92008-09-04 21:09:44 +0200599
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400600void get_cpu_cap(struct cpuinfo_x86 *c)
Yinghai Lu093af8d2008-01-30 13:33:32 +0100601{
602 u32 tfms, xlvl;
Yinghai Lu3da99c92008-09-04 21:09:44 +0200603 u32 ebx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100604
Yinghai Lu3da99c92008-09-04 21:09:44 +0200605 /* Intel-defined flags: level 0x00000001 */
606 if (c->cpuid_level >= 0x00000001) {
607 u32 capability, excap;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100608
Yinghai Lu3da99c92008-09-04 21:09:44 +0200609 cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
610 c->x86_capability[0] = capability;
611 c->x86_capability[4] = excap;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100612 }
613
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700614 /* Additional Intel-defined flags: level 0x00000007 */
615 if (c->cpuid_level >= 0x00000007) {
616 u32 eax, ebx, ecx, edx;
617
618 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
619
Fenghua Yu2494b032011-05-17 12:33:26 -0700620 c->x86_capability[9] = ebx;
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100621
622 c->x86_capability[14] = cpuid_eax(0x00000006);
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700623 }
624
Fenghua Yu6229ad22014-05-29 11:12:30 -0700625 /* Extended state features: level 0x0000000d */
626 if (c->cpuid_level >= 0x0000000d) {
627 u32 eax, ebx, ecx, edx;
628
629 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
630
631 c->x86_capability[10] = eax;
632 }
633
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000634 /* Additional Intel-defined flags: level 0x0000000F */
635 if (c->cpuid_level >= 0x0000000F) {
636 u32 eax, ebx, ecx, edx;
637
638 /* QoS sub-leaf, EAX=0Fh, ECX=0 */
639 cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
640 c->x86_capability[11] = edx;
641 if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
642 /* will be overridden if occupancy monitoring exists */
643 c->x86_cache_max_rmid = ebx;
644
645 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
646 cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
647 c->x86_capability[12] = edx;
648 if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) {
649 c->x86_cache_max_rmid = ecx;
650 c->x86_cache_occ_scale = ebx;
651 }
652 } else {
653 c->x86_cache_max_rmid = -1;
654 c->x86_cache_occ_scale = -1;
655 }
656 }
657
Yinghai Lu3da99c92008-09-04 21:09:44 +0200658 /* AMD-defined flags: level 0x80000001 */
659 xlvl = cpuid_eax(0x80000000);
660 c->extended_cpuid_level = xlvl;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100661
Yinghai Lu3da99c92008-09-04 21:09:44 +0200662 if ((xlvl & 0xffff0000) == 0x80000000) {
663 if (xlvl >= 0x80000001) {
664 c->x86_capability[1] = cpuid_edx(0x80000001);
665 c->x86_capability[6] = cpuid_ecx(0x80000001);
666 }
667 }
Yinghai Lu5122c892008-09-04 20:09:09 -0700668
Yinghai Lu5122c892008-09-04 20:09:09 -0700669 if (c->extended_cpuid_level >= 0x80000008) {
670 u32 eax = cpuid_eax(0x80000008);
671
672 c->x86_virt_bits = (eax >> 8) & 0xff;
673 c->x86_phys_bits = eax & 0xff;
Wan Zongshun2167cea2015-10-30 13:11:39 +0100674 c->x86_capability[13] = cpuid_ebx(0x80000008);
Yinghai Lu5122c892008-09-04 20:09:09 -0700675 }
Jan Beulich13c6c532009-03-12 12:37:34 +0000676#ifdef CONFIG_X86_32
677 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
678 c->x86_phys_bits = 36;
Yinghai Lu5122c892008-09-04 20:09:09 -0700679#endif
Yinghai Lue3224232008-09-06 01:52:28 -0700680
681 if (c->extended_cpuid_level >= 0x80000007)
682 c->x86_power = cpuid_edx(0x80000007);
683
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100684 if (c->extended_cpuid_level >= 0x8000000a)
685 c->x86_capability[15] = cpuid_edx(0x8000000a);
686
Jacob Pan1dedefd2010-05-19 12:01:23 -0700687 init_scattered_cpuid_features(c);
Yinghai Lu093af8d2008-01-30 13:33:32 +0100688}
Yinghai Luaef93c82008-09-14 02:33:15 -0700689
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400690static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
Yinghai Luaef93c82008-09-14 02:33:15 -0700691{
692#ifdef CONFIG_X86_32
693 int i;
694
695 /*
696 * First of all, decide if this is a 486 or higher
697 * It's a 486 if we can modify the AC flag
698 */
699 if (flag_is_changeable_p(X86_EFLAGS_AC))
700 c->x86 = 4;
701 else
702 c->x86 = 3;
703
704 for (i = 0; i < X86_VENDOR_NUM; i++)
705 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
706 c->x86_vendor_id[0] = 0;
707 cpu_devs[i]->c_identify(c);
708 if (c->x86_vendor_id[0]) {
709 get_cpu_vendor(c);
710 break;
711 }
712 }
713#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100716/*
717 * Do minimum CPU detection early.
718 * Fields really needed: vendor, cpuid_level, family, model, mask,
719 * cache alignment.
720 * The others are not touched to avoid unwanted side effects.
721 *
722 * WARNING: this function is only called on the BP. Don't add code here
723 * that is supposed to run on all CPUs.
724 */
Yinghai Lu3da99c92008-09-04 21:09:44 +0200725static void __init early_identify_cpu(struct cpuinfo_x86 *c)
Rusty Russelld7cd5612006-12-07 02:14:08 +0100726{
Yinghai Lu6627d242008-09-04 20:09:10 -0700727#ifdef CONFIG_X86_64
728 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +0000729 c->x86_phys_bits = 36;
730 c->x86_virt_bits = 48;
Yinghai Lu6627d242008-09-04 20:09:10 -0700731#else
Huang, Yingd4387bd2008-01-31 22:05:45 +0100732 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +0000733 c->x86_phys_bits = 32;
734 c->x86_virt_bits = 32;
Yinghai Lu6627d242008-09-04 20:09:10 -0700735#endif
Yinghai Lu0a488a52008-09-04 21:09:47 +0200736 c->x86_cache_alignment = c->x86_clflush_size;
Rusty Russelld7cd5612006-12-07 02:14:08 +0100737
Yinghai Lu3da99c92008-09-04 21:09:44 +0200738 memset(&c->x86_capability, 0, sizeof c->x86_capability);
Yinghai Lu0a488a52008-09-04 21:09:47 +0200739 c->extended_cpuid_level = 0;
740
Yinghai Luaef93c82008-09-14 02:33:15 -0700741 if (!have_cpuid_p())
742 identify_cpu_without_cpuid(c);
743
744 /* cyrix could have cpuid enabled via c_identify()*/
Rusty Russelld7cd5612006-12-07 02:14:08 +0100745 if (!have_cpuid_p())
746 return;
747
748 cpu_detect(c);
Yinghai Lu3da99c92008-09-04 21:09:44 +0200749 get_cpu_vendor(c);
Yinghai Lu3da99c92008-09-04 21:09:44 +0200750 get_cpu_cap(c);
Krzysztof Helt12cf1052008-09-04 21:09:43 +0200751
Yinghai Lu10a434f2008-09-04 21:09:45 +0200752 if (this_cpu->c_early_init)
753 this_cpu->c_early_init(c);
Yinghai Lu3da99c92008-09-04 21:09:44 +0200754
Robert Richterf6e9456c2010-07-21 19:03:58 +0200755 c->cpu_index = 0;
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800756 filter_cpuid_features(c, false);
Fenghua Yude5397a2011-05-11 16:51:05 -0700757
Borislav Petkova110b5e2011-08-05 20:01:16 +0200758 if (this_cpu->c_bsp_init)
759 this_cpu->c_bsp_init(c);
Borislav Petkovc3b83592013-06-09 12:07:30 +0200760
761 setup_force_cpu_cap(X86_FEATURE_ALWAYS);
Ingo Molnardb52ef72015-06-27 10:25:14 +0200762 fpu__init_system(c);
Rusty Russelld7cd5612006-12-07 02:14:08 +0100763}
764
Yinghai Lu9d31d352008-09-04 21:09:44 +0200765void __init early_cpu_init(void)
766{
Jan Beulich02dde8b2009-03-12 12:08:49 +0000767 const struct cpu_dev *const *cdev;
Yinghai Lu10a434f2008-09-04 21:09:45 +0200768 int count = 0;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200769
Jan Beulichac23f252011-03-04 15:52:35 +0000770#ifdef CONFIG_PROCESSOR_SELECT
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530771 printk(KERN_INFO "KERNEL supported cpus:\n");
Ingo Molnar31c997c2009-11-14 10:34:41 +0100772#endif
773
Yinghai Lu10a434f2008-09-04 21:09:45 +0200774 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
Jan Beulich02dde8b2009-03-12 12:08:49 +0000775 const struct cpu_dev *cpudev = *cdev;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200776
Yinghai Lu10a434f2008-09-04 21:09:45 +0200777 if (count >= X86_VENDOR_NUM)
778 break;
779 cpu_devs[count] = cpudev;
780 count++;
781
Jan Beulichac23f252011-03-04 15:52:35 +0000782#ifdef CONFIG_PROCESSOR_SELECT
Ingo Molnar31c997c2009-11-14 10:34:41 +0100783 {
784 unsigned int j;
Yinghai Lu10a434f2008-09-04 21:09:45 +0200785
Ingo Molnar31c997c2009-11-14 10:34:41 +0100786 for (j = 0; j < 2; j++) {
787 if (!cpudev->c_ident[j])
788 continue;
789 printk(KERN_INFO " %s %s\n", cpudev->c_vendor,
790 cpudev->c_ident[j]);
791 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200792 }
Dave Jones03884232009-11-13 15:30:00 -0500793#endif
Ingo Molnar31c997c2009-11-14 10:34:41 +0100794 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200795 early_identify_cpu(&boot_cpu_data);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800796}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700798/*
Borislav Petkov366d4a42010-10-04 09:31:27 +0200799 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
800 * unfortunately, that's not true in practice because of early VIA
801 * chips and (more importantly) broken virtualizers that are not easy
802 * to detect. In the latter case it doesn't even *fail* reliably, so
803 * probing for it doesn't even work. Disable it completely on 32-bit
H. Peter Anvinba0593b2008-09-16 09:29:40 -0700804 * unless we can find a reliable way to detect all the broken cases.
Borislav Petkov366d4a42010-10-04 09:31:27 +0200805 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700806 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400807static void detect_nopl(struct cpuinfo_x86 *c)
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700808{
Borislav Petkov366d4a42010-10-04 09:31:27 +0200809#ifdef CONFIG_X86_32
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700810 clear_cpu_cap(c, X86_FEATURE_NOPL);
Borislav Petkov366d4a42010-10-04 09:31:27 +0200811#else
812 set_cpu_cap(c, X86_FEATURE_NOPL);
813#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400816static void generic_identify(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Yinghai Lu3da99c92008-09-04 21:09:44 +0200818 c->extended_cpuid_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Yinghai Luaef93c82008-09-14 02:33:15 -0700820 if (!have_cpuid_p())
821 identify_cpu_without_cpuid(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100822
Yinghai Luaef93c82008-09-14 02:33:15 -0700823 /* cyrix could have cpuid enabled via c_identify()*/
Ingo Molnara9853dd2008-09-14 14:46:58 +0200824 if (!have_cpuid_p())
Yinghai Luaef93c82008-09-14 02:33:15 -0700825 return;
826
Yinghai Lu3da99c92008-09-04 21:09:44 +0200827 cpu_detect(c);
828
829 get_cpu_vendor(c);
830
831 get_cpu_cap(c);
832
833 if (c->cpuid_level >= 0x00000001) {
834 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
Yinghai Lub89d3b32008-09-04 20:09:12 -0700835#ifdef CONFIG_X86_32
Borislav Petkovc8e56d22015-06-04 18:55:25 +0200836# ifdef CONFIG_SMP
Ingo Molnarcb8cc442009-01-28 13:24:54 +0100837 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lub89d3b32008-09-04 20:09:12 -0700838# else
Yinghai Lu3da99c92008-09-04 21:09:44 +0200839 c->apicid = c->initial_apicid;
Yinghai Lub89d3b32008-09-04 20:09:12 -0700840# endif
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800841#endif
Yinghai Lub89d3b32008-09-04 20:09:12 -0700842 c->phys_proc_id = c->initial_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Yinghai Lu3da99c92008-09-04 21:09:44 +0200844
Yinghai Lu1b05d602008-09-06 01:52:27 -0700845 get_model_name(c); /* Default name */
Yinghai Lu3da99c92008-09-04 21:09:44 +0200846
Yinghai Lu3da99c92008-09-04 21:09:44 +0200847 detect_nopl(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000850static void x86_init_cache_qos(struct cpuinfo_x86 *c)
851{
852 /*
853 * The heavy lifting of max_rmid and cache_occ_scale are handled
854 * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
855 * in case CQM bits really aren't there in this CPU.
856 */
857 if (c != &boot_cpu_data) {
858 boot_cpu_data.x86_cache_max_rmid =
859 min(boot_cpu_data.x86_cache_max_rmid,
860 c->x86_cache_max_rmid);
861 }
862}
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/*
865 * This does the hard work of actually picking apart the CPU stuff...
866 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400867static void identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 int i;
870
871 c->loops_per_jiffy = loops_per_jiffy;
872 c->x86_cache_size = -1;
873 c->x86_vendor = X86_VENDOR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 c->x86_model = c->x86_mask = 0; /* So far unknown... */
875 c->x86_vendor_id[0] = '\0'; /* Unset */
876 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100877 c->x86_max_cores = 1;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700878 c->x86_coreid_bits = 0;
Yinghai Lu11fdd252008-09-07 17:58:50 -0700879#ifdef CONFIG_X86_64
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700880 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +0000881 c->x86_phys_bits = 36;
882 c->x86_virt_bits = 48;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700883#else
884 c->cpuid_level = -1; /* CPUID not detected */
Andi Kleen770d1322006-12-07 02:14:05 +0100885 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +0000886 c->x86_phys_bits = 32;
887 c->x86_virt_bits = 32;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700888#endif
889 c->x86_cache_alignment = c->x86_clflush_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 memset(&c->x86_capability, 0, sizeof c->x86_capability);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 generic_identify(c);
893
Andi Kleen38985342008-01-30 13:32:49 +0100894 if (this_cpu->c_identify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 this_cpu->c_identify(c);
896
Yinghai Lu2759c322009-05-15 13:05:16 -0700897 /* Clear/Set all flags overriden by options, after probe */
898 for (i = 0; i < NCAPINTS; i++) {
899 c->x86_capability[i] &= ~cpu_caps_cleared[i];
900 c->x86_capability[i] |= cpu_caps_set[i];
901 }
902
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700903#ifdef CONFIG_X86_64
Ingo Molnarcb8cc442009-01-28 13:24:54 +0100904 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700905#endif
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /*
908 * Vendor-specific initialization. In this section we
909 * canonicalize the feature flags, meaning if there are
910 * features a certain CPU supports which CPUID doesn't
911 * tell us, CPUID claiming incorrect flags, or other bugs,
912 * we handle them here.
913 *
914 * At the end of this section, c->x86_capability better
915 * indicate the features this CPU genuinely supports!
916 */
917 if (this_cpu->c_init)
918 this_cpu->c_init(c);
919
920 /* Disable the PN if appropriate */
921 squash_the_stupid_serial_number(c);
922
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700923 /* Set up SMEP/SMAP */
924 setup_smep(c);
925 setup_smap(c);
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /*
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100928 * The vendor-specific functions might have changed features.
929 * Now we do "generic changes."
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 */
931
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800932 /* Filter out anything that depends on CPUID levels we don't have */
933 filter_cpuid_features(c, true);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* If the model name is still unset, do table lookup. */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100936 if (!c->x86_model_id[0]) {
Jan Beulich02dde8b2009-03-12 12:08:49 +0000937 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 p = table_lookup_model(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100939 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 strcpy(c->x86_model_id, p);
941 else
942 /* Last resort... */
943 sprintf(c->x86_model_id, "%02x/%02x",
Chuck Ebbert54a20f82006-03-23 02:59:36 -0800944 c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700947#ifdef CONFIG_X86_64
948 detect_ht(c);
949#endif
950
Alok Kataria88b094f2008-10-27 10:41:46 -0700951 init_hypervisor(c);
H. Peter Anvin49d859d2011-07-31 14:02:19 -0700952 x86_init_rdrand(c);
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000953 x86_init_cache_qos(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -0700954
955 /*
956 * Clear/Set all flags overriden by options, need do it
957 * before following smp all cpus cap AND.
958 */
959 for (i = 0; i < NCAPINTS; i++) {
960 c->x86_capability[i] &= ~cpu_caps_cleared[i];
961 c->x86_capability[i] |= cpu_caps_set[i];
962 }
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /*
965 * On SMP, boot_cpu_data holds the common feature set between
966 * all CPUs; so make sure that we indicate which features are
967 * common between the CPUs. The first time this routine gets
968 * executed, c == &boot_cpu_data.
969 */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100970 if (c != &boot_cpu_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* AND the already accumulated flags with these */
Yinghai Lu9d31d352008-09-04 21:09:44 +0200972 for (i = 0; i < NCAPINTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
Borislav Petkov65fc9852013-03-20 15:07:23 +0100974
975 /* OR, i.e. replicate the bug flags */
976 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
977 c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
980 /* Init Machine Check Exception if available. */
Borislav Petkov5e099542009-10-16 12:31:32 +0200981 mcheck_cpu_init(c);
Andi Kleen30d432d2008-01-30 13:33:16 +0100982
983 select_idle_routine(c);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700984
Tejun Heode2d9442011-01-23 14:37:41 +0100985#ifdef CONFIG_NUMA
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700986 numa_add_cpu(smp_processor_id());
987#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +0200988}
Shaohua Li31ab2692005-11-07 00:58:42 -0800989
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +0100990/*
991 * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
992 * on 32-bit kernels:
993 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -0700994#ifdef CONFIG_X86_32
995void enable_sep_cpu(void)
996{
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +0100997 struct tss_struct *tss;
998 int cpu;
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -0700999
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001000 cpu = get_cpu();
1001 tss = &per_cpu(cpu_tss, cpu);
1002
1003 if (!boot_cpu_has(X86_FEATURE_SEP))
1004 goto out;
1005
1006 /*
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001007 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1008 * see the big comment in struct x86_hw_tss's definition.
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001009 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001010
1011 tss->x86_tss.ss1 = __KERNEL_CS;
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001012 wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
1013
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001014 wrmsr(MSR_IA32_SYSENTER_ESP,
1015 (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
1016 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001017
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001018 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001019
1020out:
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001021 put_cpu();
1022}
Glauber Costae04d6452008-09-22 14:35:08 -03001023#endif
1024
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001025void __init identify_boot_cpu(void)
1026{
1027 identify_cpu(&boot_cpu_data);
Len Brown02c68a02011-04-01 16:59:53 -04001028 init_amd_e400_c1e_mask();
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001029#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001030 sysenter_setup();
Li Shaohua6fe940d2005-06-25 14:54:53 -07001031 enable_sep_cpu();
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001032#endif
Borislav Petkov5b5563322012-08-06 19:00:37 +02001033 cpu_detect_tlb(&boot_cpu_data);
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001034}
Shaohua Li3b520b22005-07-07 17:56:38 -07001035
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001036void identify_secondary_cpu(struct cpuinfo_x86 *c)
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001037{
1038 BUG_ON(c == &boot_cpu_data);
1039 identify_cpu(c);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001040#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001041 enable_sep_cpu();
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001042#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001043 mtrr_ap_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Yinghai Lua0854a42008-09-04 21:09:46 +02001046struct msr_range {
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001047 unsigned min;
1048 unsigned max;
Yinghai Lua0854a42008-09-04 21:09:46 +02001049};
1050
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001051static const struct msr_range msr_range_array[] = {
Yinghai Lua0854a42008-09-04 21:09:46 +02001052 { 0x00000000, 0x00000418},
1053 { 0xc0000000, 0xc000040b},
1054 { 0xc0010000, 0xc0010142},
1055 { 0xc0011000, 0xc001103b},
1056};
1057
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001058static void __print_cpu_msr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001060 unsigned index_min, index_max;
Yinghai Lua0854a42008-09-04 21:09:46 +02001061 unsigned index;
1062 u64 val;
1063 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Yinghai Lua0854a42008-09-04 21:09:46 +02001065 for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
1066 index_min = msr_range_array[i].min;
1067 index_max = msr_range_array[i].max;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001068
Yinghai Lua0854a42008-09-04 21:09:46 +02001069 for (index = index_min; index < index_max; index++) {
Borislav Petkovecd431d2012-06-01 16:52:36 +02001070 if (rdmsrl_safe(index, &val))
Yinghai Lua0854a42008-09-04 21:09:46 +02001071 continue;
1072 printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075}
Yinghai Lua0854a42008-09-04 21:09:46 +02001076
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001077static int show_msr;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001078
Yinghai Lua0854a42008-09-04 21:09:46 +02001079static __init int setup_show_msr(char *arg)
1080{
1081 int num;
1082
1083 get_option(&arg, &num);
1084
1085 if (num > 0)
1086 show_msr = num;
1087 return 1;
1088}
1089__setup("show_msr=", setup_show_msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Andi Kleen191679f2008-01-30 13:33:21 +01001091static __init int setup_noclflush(char *arg)
1092{
H. Peter Anvin840d2832014-02-27 08:31:30 -08001093 setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
H. Peter Anvinda4aaa72014-02-27 08:36:31 -08001094 setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
Andi Kleen191679f2008-01-30 13:33:21 +01001095 return 1;
1096}
1097__setup("noclflush", setup_noclflush);
1098
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001099void print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Jan Beulich02dde8b2009-03-12 12:08:49 +00001101 const char *vendor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001103 if (c->x86_vendor < X86_VENDOR_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 vendor = this_cpu->c_vendor;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001105 } else {
1106 if (c->cpuid_level >= 0)
1107 vendor = c->x86_vendor_id;
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Yinghai Lubd32a8cf2008-09-19 18:41:16 -07001110 if (vendor && !strstr(c->x86_model_id, vendor))
Yinghai Lu9d31d352008-09-04 21:09:44 +02001111 printk(KERN_CONT "%s ", vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Yinghai Lu9d31d352008-09-04 21:09:44 +02001113 if (c->x86_model_id[0])
Prarit Bhargavaadafb982015-05-26 10:28:17 +02001114 printk(KERN_CONT "%s", c->x86_model_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 else
Yinghai Lu9d31d352008-09-04 21:09:44 +02001116 printk(KERN_CONT "%d86", c->x86);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Borislav Petkov7c5b1902015-09-10 21:55:27 +02001118 printk(KERN_CONT " (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
Borislav Petkov924e1012012-09-14 18:37:46 +02001119
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001120 if (c->x86_mask || c->cpuid_level >= 0)
Borislav Petkov7c5b1902015-09-10 21:55:27 +02001121 printk(KERN_CONT ", stepping: 0x%x)\n", c->x86_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 else
Borislav Petkov924e1012012-09-14 18:37:46 +02001123 printk(KERN_CONT ")\n");
Yinghai Lua0854a42008-09-04 21:09:46 +02001124
Yinghai Lu0b8b8072012-03-22 21:31:43 -07001125 print_cpu_msr(c);
Yinghai Lu21c3fcf2012-02-12 09:53:57 -08001126}
1127
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001128void print_cpu_msr(struct cpuinfo_x86 *c)
Yinghai Lu21c3fcf2012-02-12 09:53:57 -08001129{
Yinghai Lua0854a42008-09-04 21:09:46 +02001130 if (c->cpu_index < show_msr)
Yinghai Lu21c3fcf2012-02-12 09:53:57 -08001131 __print_cpu_msr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Andi Kleenac72e782008-01-30 13:33:21 +01001134static __init int setup_disablecpuid(char *arg)
1135{
1136 int bit;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001137
Andi Kleenac72e782008-01-30 13:33:21 +01001138 if (get_option(&arg, &bit) && bit < NCAPINTS*32)
1139 setup_clear_cpu_cap(bit);
1140 else
1141 return 0;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001142
Andi Kleenac72e782008-01-30 13:33:21 +01001143 return 1;
1144}
1145__setup("clearcpuid=", setup_disablecpuid);
1146
Yinghai Lud5494d42008-09-04 20:09:03 -07001147#ifdef CONFIG_X86_64
Cyrill Gorcunov9ff809422009-07-08 22:03:53 +04001148struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001149struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
1150 (unsigned long) debug_idt_table };
Yinghai Lud5494d42008-09-04 20:09:03 -07001151
Brian Gerst947e76c2009-01-19 12:21:28 +09001152DEFINE_PER_CPU_FIRST(union irq_stack_union,
Andi Kleen277d5b42013-08-05 15:02:43 -07001153 irq_stack_union) __aligned(PAGE_SIZE) __visible;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001154
Tejun Heobdf977b2009-08-03 14:12:19 +09001155/*
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001156 * The following percpu variables are hot. Align current_task to
1157 * cacheline size such that they fall in the same cacheline.
Tejun Heobdf977b2009-08-03 14:12:19 +09001158 */
1159DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1160 &init_task;
1161EXPORT_PER_CPU_SYMBOL(current_task);
Yinghai Lud5494d42008-09-04 20:09:03 -07001162
Tejun Heobdf977b2009-08-03 14:12:19 +09001163DEFINE_PER_CPU(char *, irq_stack_ptr) =
1164 init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
1165
Andi Kleen277d5b42013-08-05 15:02:43 -07001166DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
Yinghai Lud5494d42008-09-04 20:09:03 -07001167
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001168DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1169EXPORT_PER_CPU_SYMBOL(__preempt_count);
1170
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001171/*
1172 * Special IST stacks which the CPU switches to when it calls
1173 * an IST-marked descriptor entry. Up to 7 stacks (hardware
1174 * limit), all of them are 4K, except the debug stack which
1175 * is 8K.
1176 */
1177static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
1178 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
1179 [DEBUG_STACK - 1] = DEBUG_STKSZ
1180};
1181
Brian Gerst92d65b22009-01-19 00:38:58 +09001182static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
Tejun Heo3e352aa2009-08-03 14:10:11 +09001183 [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
Yinghai Lud5494d42008-09-04 20:09:03 -07001184
Yinghai Lud5494d42008-09-04 20:09:03 -07001185/* May not be marked __init: used by software suspend */
1186void syscall_init(void)
1187{
1188 /*
1189 * LSTAR and STAR live in a bit strange symbiosis.
1190 * They both write to the same internal register. STAR allows to
1191 * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
1192 */
1193 wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
Andy Lutomirski47edb652015-07-23 12:14:40 -07001194 wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001195
1196#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski47edb652015-07-23 12:14:40 -07001197 wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001198 /*
Denys Vlasenko487d1ed2015-03-27 11:59:16 +01001199 * This only works on Intel CPUs.
1200 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1201 * This does not cause SYSENTER to jump to the wrong location, because
1202 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001203 */
1204 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1205 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001206 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001207#else
Andy Lutomirski47edb652015-07-23 12:14:40 -07001208 wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
Borislav Petkov6b513112015-04-03 14:25:28 +02001209 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001210 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1211 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
Yinghai Lud5494d42008-09-04 20:09:03 -07001212#endif
1213
1214 /* Flags to clear on syscall */
1215 wrmsrl(MSR_SYSCALL_MASK,
H. Peter Anvin63bcff22012-09-21 12:43:12 -07001216 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
Andy Lutomirski8c7aa692014-10-01 11:49:04 -07001217 X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
Yinghai Lud5494d42008-09-04 20:09:03 -07001218}
1219
Yinghai Lud5494d42008-09-04 20:09:03 -07001220/*
1221 * Copies of the original ist values from the tss are only accessed during
1222 * debugging, no special alignment required.
1223 */
1224DEFINE_PER_CPU(struct orig_ist, orig_ist);
1225
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001226static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
Steven Rostedt42181182011-12-16 11:43:02 -05001227DEFINE_PER_CPU(int, debug_stack_usage);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001228
1229int is_debug_stack(unsigned long addr)
1230{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001231 return __this_cpu_read(debug_stack_usage) ||
1232 (addr <= __this_cpu_read(debug_stack_addr) &&
1233 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001234}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001235NOKPROBE_SYMBOL(is_debug_stack);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001236
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001237DEFINE_PER_CPU(u32, debug_idt_ctr);
Steven Rostedtf8988172012-05-30 11:47:00 -04001238
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001239void debug_stack_set_zero(void)
1240{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001241 this_cpu_inc(debug_idt_ctr);
1242 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001243}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001244NOKPROBE_SYMBOL(debug_stack_set_zero);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001245
1246void debug_stack_reset(void)
1247{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001248 if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
Steven Rostedtf8988172012-05-30 11:47:00 -04001249 return;
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001250 if (this_cpu_dec_return(debug_idt_ctr) == 0)
1251 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001252}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001253NOKPROBE_SYMBOL(debug_stack_reset);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001254
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001255#else /* CONFIG_X86_64 */
Yinghai Lud5494d42008-09-04 20:09:03 -07001256
Tejun Heobdf977b2009-08-03 14:12:19 +09001257DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1258EXPORT_PER_CPU_SYMBOL(current_task);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001259DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1260EXPORT_PER_CPU_SYMBOL(__preempt_count);
Tejun Heobdf977b2009-08-03 14:12:19 +09001261
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001262/*
1263 * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1264 * the top of the kernel stack. Use an extra percpu variable to track the
1265 * top of the kernel stack directly.
1266 */
1267DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1268 (unsigned long)&init_thread_union + THREAD_SIZE;
1269EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1270
Tejun Heo60a53172009-02-09 22:17:40 +09001271#ifdef CONFIG_CC_STACKPROTECTOR
Jeremy Fitzhardinge53f82452009-09-03 14:31:44 -07001272DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
Tejun Heo60a53172009-02-09 22:17:40 +09001273#endif
1274
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001275#endif /* CONFIG_X86_64 */
Jeremy Fitzhardingec5413fb2007-05-02 19:27:16 +02001276
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001277/*
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301278 * Clear all 6 debug registers:
1279 */
1280static void clear_all_debug_regs(void)
1281{
1282 int i;
1283
1284 for (i = 0; i < 8; i++) {
1285 /* Ignore db4, db5 */
1286 if ((i == 4) || (i == 5))
1287 continue;
1288
1289 set_debugreg(0, i);
1290 }
1291}
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +01001292
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001293#ifdef CONFIG_KGDB
1294/*
1295 * Restore debug regs if using kgdbwait and you have a kernel debugger
1296 * connection established.
1297 */
1298static void dbg_restore_debug_regs(void)
1299{
1300 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1301 arch_kgdb_ops.correct_hw_break();
1302}
1303#else /* ! CONFIG_KGDB */
1304#define dbg_restore_debug_regs()
1305#endif /* ! CONFIG_KGDB */
1306
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001307static void wait_for_master_cpu(int cpu)
1308{
1309#ifdef CONFIG_SMP
1310 /*
1311 * wait for ACK from master CPU before continuing
1312 * with AP initialization
1313 */
1314 WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1315 while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1316 cpu_relax();
1317#endif
1318}
1319
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001320/*
1321 * cpu_init() initializes state that is per-CPU. Some data is already
1322 * initialized (naturally) in the bootstrap process, such as the GDT
1323 * and IDT. We reload them nevertheless, this function acts as a
1324 * 'CPU state barrier', nothing should get across.
Yinghai Lu1ba76582008-09-04 20:09:04 -07001325 * A lot of state is already set up in PDA init for 64 bit
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001326 */
Yinghai Lu1ba76582008-09-04 20:09:04 -07001327#ifdef CONFIG_X86_64
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001328
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001329void cpu_init(void)
Yinghai Lu1ba76582008-09-04 20:09:04 -07001330{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001331 struct orig_ist *oist;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001332 struct task_struct *me;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001333 struct tss_struct *t;
1334 unsigned long v;
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001335 int cpu = stack_smp_processor_id();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001336 int i;
1337
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001338 wait_for_master_cpu(cpu);
1339
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001340 /*
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001341 * Initialize the CR4 shadow before doing anything that could
1342 * try to read it.
1343 */
1344 cr4_init_shadow();
1345
1346 /*
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001347 * Load microcode on this cpu if a valid microcode is available.
1348 * This is early microcode loading procedure.
1349 */
1350 load_ucode_ap();
1351
Andy Lutomirski24933b82015-03-05 19:19:05 -08001352 t = &per_cpu(cpu_tss, cpu);
Tejun Heo0fe1e002009-10-29 22:34:14 +09001353 oist = &per_cpu(orig_ist, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001354
Brian Gerste7a22c12009-01-19 00:38:59 +09001355#ifdef CONFIG_NUMA
Fenghua Yu27fd1852012-11-13 11:32:47 -08001356 if (this_cpu_read(numa_node) == 0 &&
Lee Schermerhorne534c7c2010-05-26 14:44:58 -07001357 early_cpu_to_node(cpu) != NUMA_NO_NODE)
1358 set_numa_node(early_cpu_to_node(cpu));
Brian Gerste7a22c12009-01-19 00:38:59 +09001359#endif
Yinghai Lu1ba76582008-09-04 20:09:04 -07001360
1361 me = current;
1362
Mike Travis2eaad1f2009-12-10 17:19:36 -08001363 pr_debug("Initializing CPU#%d\n", cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001364
Andy Lutomirski375074c2014-10-24 15:58:07 -07001365 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001366
1367 /*
1368 * Initialize the per-CPU GDT with the boot GDT,
1369 * and set up the GDT descriptor:
1370 */
1371
Brian Gerst552be872009-01-30 17:47:53 +09001372 switch_to_new_gdt(cpu);
Brian Gerst2697fbd2009-01-27 12:56:48 +09001373 loadsegment(fs, 0);
1374
Seiji Aguchicf910e82013-06-20 11:46:53 -04001375 load_current_idt();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001376
1377 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1378 syscall_init();
1379
1380 wrmsrl(MSR_FS_BASE, 0);
1381 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1382 barrier();
1383
H. Peter Anvin4763ed42009-11-13 15:28:16 -08001384 x86_configure_nx();
Thomas Gleixner659006b2015-01-15 21:22:26 +00001385 x2apic_setup();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001386
1387 /*
1388 * set up and load the per-CPU TSS
1389 */
Tejun Heo0fe1e002009-10-29 22:34:14 +09001390 if (!oist->ist[0]) {
Brian Gerst92d65b22009-01-19 00:38:58 +09001391 char *estacks = per_cpu(exception_stacks, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001392
Yinghai Lu1ba76582008-09-04 20:09:04 -07001393 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001394 estacks += exception_stack_sizes[v];
Tejun Heo0fe1e002009-10-29 22:34:14 +09001395 oist->ist[v] = t->x86_tss.ist[v] =
Yinghai Lu1ba76582008-09-04 20:09:04 -07001396 (unsigned long)estacks;
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001397 if (v == DEBUG_STACK-1)
1398 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001399 }
1400 }
1401
1402 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001403
Yinghai Lu1ba76582008-09-04 20:09:04 -07001404 /*
1405 * <= is required because the CPU will access up to
1406 * 8 bits beyond the end of the IO permission bitmap.
1407 */
1408 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1409 t->io_bitmap[i] = ~0UL;
1410
1411 atomic_inc(&init_mm.mm_count);
1412 me->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001413 BUG_ON(me->mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001414 enter_lazy_tlb(&init_mm, me);
1415
1416 load_sp0(t, &current->thread);
1417 set_tss_desc(cpu, t);
1418 load_TR_desc();
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001419 load_mm_ldt(&init_mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001420
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001421 clear_all_debug_regs();
1422 dbg_restore_debug_regs();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001423
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001424 fpu__init_cpu();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001425
Yinghai Lu1ba76582008-09-04 20:09:04 -07001426 if (is_uv_system())
1427 uv_cpu_init();
1428}
1429
1430#else
1431
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001432void cpu_init(void)
James Bottomley9ee79a32007-01-22 09:18:31 -06001433{
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001434 int cpu = smp_processor_id();
1435 struct task_struct *curr = current;
Andy Lutomirski24933b82015-03-05 19:19:05 -08001436 struct tss_struct *t = &per_cpu(cpu_tss, cpu);
James Bottomley9ee79a32007-01-22 09:18:31 -06001437 struct thread_struct *thread = &curr->thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001439 wait_for_master_cpu(cpu);
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001440
Steven Rostedt5b2bdbc2015-02-27 14:50:19 -05001441 /*
1442 * Initialize the CR4 shadow before doing anything that could
1443 * try to read it.
1444 */
1445 cr4_init_shadow();
1446
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001447 show_ucode_info_early();
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1450
Dave Hansen9298b812014-09-11 14:15:24 -07001451 if (cpu_feature_enabled(X86_FEATURE_VME) || cpu_has_tsc || cpu_has_de)
Andy Lutomirski375074c2014-10-24 15:58:07 -07001452 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Seiji Aguchicf910e82013-06-20 11:46:53 -04001454 load_current_idt();
Brian Gerst552be872009-01-30 17:47:53 +09001455 switch_to_new_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 * Set up and load the per-CPU TSS and LDT
1459 */
1460 atomic_inc(&init_mm.mm_count);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001461 curr->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001462 BUG_ON(curr->mm);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001463 enter_lazy_tlb(&init_mm, curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
H. Peter Anvinfaca6222008-01-30 13:31:02 +01001465 load_sp0(t, thread);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001466 set_tss_desc(cpu, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 load_TR_desc();
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001468 load_mm_ldt(&init_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Thomas Gleixnerf9a196b2009-05-01 20:59:25 +02001470 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1471
Matt Mackall22c4e302006-01-08 01:05:24 -08001472#ifdef CONFIG_DOUBLEFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /* Set up doublefault TSS pointer in the GDT */
1474 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
Matt Mackall22c4e302006-01-08 01:05:24 -08001475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301477 clear_all_debug_regs();
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001478 dbg_restore_debug_regs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001480 fpu__init_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
Yinghai Lu1ba76582008-09-04 20:09:04 -07001482#endif
Borislav Petkov5700f742013-06-09 12:07:32 +02001483
1484#ifdef CONFIG_X86_DEBUG_STATIC_CPU_HAS
1485void warn_pre_alternatives(void)
1486{
1487 WARN(1, "You're using static_cpu_has before alternatives have run!\n");
1488}
1489EXPORT_SYMBOL_GPL(warn_pre_alternatives);
1490#endif
Borislav Petkov4a90a992013-06-09 12:07:33 +02001491
1492inline bool __static_cpu_has_safe(u16 bit)
1493{
1494 return boot_cpu_has(bit);
1495}
1496EXPORT_SYMBOL_GPL(__static_cpu_has_safe);
Laura Abbottb51ef522015-07-20 14:47:58 -07001497
1498static void bsp_resume(void)
1499{
1500 if (this_cpu->c_bsp_resume)
1501 this_cpu->c_bsp_resume(&boot_cpu_data);
1502}
1503
1504static struct syscore_ops cpu_syscore_ops = {
1505 .resume = bsp_resume,
1506};
1507
1508static int __init init_cpu_syscore(void)
1509{
1510 register_syscore_ops(&cpu_syscore_ops);
1511 return 0;
1512}
1513core_initcall(init_cpu_syscore);