xref: /linux/arch/x86/kernel/cpu/common.c (revision 37868fe113ff2ba814b3b4eb12df214df555f8dc)
1f0fc4affSYinghai Lu #include <linux/bootmem.h>
29766cdbcSJaswinder Singh Rajput #include <linux/linkage.h>
3f0fc4affSYinghai Lu #include <linux/bitops.h>
49766cdbcSJaswinder Singh Rajput #include <linux/kernel.h>
5f0fc4affSYinghai Lu #include <linux/module.h>
6f7627e25SThomas Gleixner #include <linux/percpu.h>
79766cdbcSJaswinder Singh Rajput #include <linux/string.h>
8ee098e1aSBorislav Petkov #include <linux/ctype.h>
99766cdbcSJaswinder Singh Rajput #include <linux/delay.h>
109766cdbcSJaswinder Singh Rajput #include <linux/sched.h>
119766cdbcSJaswinder Singh Rajput #include <linux/init.h>
120f46efebSMasami Hiramatsu #include <linux/kprobes.h>
139766cdbcSJaswinder Singh Rajput #include <linux/kgdb.h>
149766cdbcSJaswinder Singh Rajput #include <linux/smp.h>
159766cdbcSJaswinder Singh Rajput #include <linux/io.h>
169766cdbcSJaswinder Singh Rajput 
179766cdbcSJaswinder Singh Rajput #include <asm/stackprotector.h>
18cdd6c482SIngo Molnar #include <asm/perf_event.h>
19f7627e25SThomas Gleixner #include <asm/mmu_context.h>
2049d859d7SH. Peter Anvin #include <asm/archrandom.h>
219766cdbcSJaswinder Singh Rajput #include <asm/hypervisor.h>
229766cdbcSJaswinder Singh Rajput #include <asm/processor.h>
231e02ce4cSAndy Lutomirski #include <asm/tlbflush.h>
24f649e938SPaul Gortmaker #include <asm/debugreg.h>
259766cdbcSJaswinder Singh Rajput #include <asm/sections.h>
26f40c3300SAndy Lutomirski #include <asm/vsyscall.h>
278bdbd962SAlan Cox #include <linux/topology.h>
288bdbd962SAlan Cox #include <linux/cpumask.h>
299766cdbcSJaswinder Singh Rajput #include <asm/pgtable.h>
3060063497SArun Sharma #include <linux/atomic.h>
319766cdbcSJaswinder Singh Rajput #include <asm/proto.h>
329766cdbcSJaswinder Singh Rajput #include <asm/setup.h>
33f7627e25SThomas Gleixner #include <asm/apic.h>
349766cdbcSJaswinder Singh Rajput #include <asm/desc.h>
3578f7f1e5SIngo Molnar #include <asm/fpu/internal.h>
369766cdbcSJaswinder Singh Rajput #include <asm/mtrr.h>
378bdbd962SAlan Cox #include <linux/numa.h>
389766cdbcSJaswinder Singh Rajput #include <asm/asm.h>
399766cdbcSJaswinder Singh Rajput #include <asm/cpu.h>
409766cdbcSJaswinder Singh Rajput #include <asm/mce.h>
419766cdbcSJaswinder Singh Rajput #include <asm/msr.h>
429766cdbcSJaswinder Singh Rajput #include <asm/pat.h>
43d288e1cfSFenghua Yu #include <asm/microcode.h>
44d288e1cfSFenghua Yu #include <asm/microcode_intel.h>
45e641f5f5SIngo Molnar 
46f7627e25SThomas Gleixner #ifdef CONFIG_X86_LOCAL_APIC
47bdbcdd48STejun Heo #include <asm/uv/uv.h>
48f7627e25SThomas Gleixner #endif
49f7627e25SThomas Gleixner 
50f7627e25SThomas Gleixner #include "cpu.h"
51f7627e25SThomas Gleixner 
52c2d1cec1SMike Travis /* all of these masks are initialized in setup_cpu_local_masks() */
53c2d1cec1SMike Travis cpumask_var_t cpu_initialized_mask;
549766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callout_mask;
559766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callin_mask;
56c2d1cec1SMike Travis 
57c2d1cec1SMike Travis /* representing cpus for which sibling maps can be computed */
58c2d1cec1SMike Travis cpumask_var_t cpu_sibling_setup_mask;
59c2d1cec1SMike Travis 
602f2f52baSBrian Gerst /* correctly size the local cpu masks */
614369f1fbSIngo Molnar void __init setup_cpu_local_masks(void)
622f2f52baSBrian Gerst {
632f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_initialized_mask);
642f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callin_mask);
652f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callout_mask);
662f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
672f2f52baSBrian Gerst }
682f2f52baSBrian Gerst 
69148f9bb8SPaul Gortmaker static void default_init(struct cpuinfo_x86 *c)
70e8055139SOndrej Zary {
71e8055139SOndrej Zary #ifdef CONFIG_X86_64
7227c13eceSBorislav Petkov 	cpu_detect_cache_sizes(c);
73e8055139SOndrej Zary #else
74e8055139SOndrej Zary 	/* Not much we can do here... */
75e8055139SOndrej Zary 	/* Check if at least it has cpuid */
76e8055139SOndrej Zary 	if (c->cpuid_level == -1) {
77e8055139SOndrej Zary 		/* No cpuid. It must be an ancient CPU */
78e8055139SOndrej Zary 		if (c->x86 == 4)
79e8055139SOndrej Zary 			strcpy(c->x86_model_id, "486");
80e8055139SOndrej Zary 		else if (c->x86 == 3)
81e8055139SOndrej Zary 			strcpy(c->x86_model_id, "386");
82e8055139SOndrej Zary 	}
83e8055139SOndrej Zary #endif
84e8055139SOndrej Zary }
85e8055139SOndrej Zary 
86148f9bb8SPaul Gortmaker static const struct cpu_dev default_cpu = {
87e8055139SOndrej Zary 	.c_init		= default_init,
88e8055139SOndrej Zary 	.c_vendor	= "Unknown",
89e8055139SOndrej Zary 	.c_x86_vendor	= X86_VENDOR_UNKNOWN,
90e8055139SOndrej Zary };
91e8055139SOndrej Zary 
92148f9bb8SPaul Gortmaker static const struct cpu_dev *this_cpu = &default_cpu;
930a488a53SYinghai Lu 
9406deef89SBrian Gerst DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
95950ad7ffSYinghai Lu #ifdef CONFIG_X86_64
9606deef89SBrian Gerst 	/*
9706deef89SBrian Gerst 	 * We need valid kernel segments for data and code in long mode too
98950ad7ffSYinghai Lu 	 * IRET will check the segment types  kkeil 2000/10/28
99950ad7ffSYinghai Lu 	 * Also sysret mandates a special GDT layout
10006deef89SBrian Gerst 	 *
1019766cdbcSJaswinder Singh Rajput 	 * TLS descriptors are currently at a different place compared to i386.
10206deef89SBrian Gerst 	 * Hopefully nobody expects them at a fixed place (Wine?)
103950ad7ffSYinghai Lu 	 */
1041e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL32_CS]		= GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
1051e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
1061e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
1071e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER32_CS]	= GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
1081e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
1091e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
110950ad7ffSYinghai Lu #else
1111e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
1121e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1131e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
1141e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
115f7627e25SThomas Gleixner 	/*
116f7627e25SThomas Gleixner 	 * Segments used for calling PnP BIOS have byte granularity.
117f7627e25SThomas Gleixner 	 * They code segments and data segments have fixed 64k limits,
118f7627e25SThomas Gleixner 	 * the transfer segment sizes are set at run time.
119f7627e25SThomas Gleixner 	 */
1206842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1211e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS32]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
1226842ef0eSGlauber de Oliveira Costa 	/* 16-bit code */
1231e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS16]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1246842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1251e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_DS]		= GDT_ENTRY_INIT(0x0092, 0, 0xffff),
1266842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1271e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS1]		= GDT_ENTRY_INIT(0x0092, 0, 0),
1286842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1291e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS2]		= GDT_ENTRY_INIT(0x0092, 0, 0),
130f7627e25SThomas Gleixner 	/*
131f7627e25SThomas Gleixner 	 * The APM segments have byte granularity and their bases
132f7627e25SThomas Gleixner 	 * are set at run time.  All have 64k limits.
133f7627e25SThomas Gleixner 	 */
1346842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1351e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
136f7627e25SThomas Gleixner 	/* 16-bit code */
1371e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE+1]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1386842ef0eSGlauber de Oliveira Costa 	/* data */
13972c4d853SIngo Molnar 	[GDT_ENTRY_APMBIOS_BASE+2]	= GDT_ENTRY_INIT(0x4092, 0, 0xffff),
140f7627e25SThomas Gleixner 
1411e5de182SAkinobu Mita 	[GDT_ENTRY_ESPFIX_SS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1421e5de182SAkinobu Mita 	[GDT_ENTRY_PERCPU]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
14360a5317fSTejun Heo 	GDT_STACK_CANARY_INIT
144950ad7ffSYinghai Lu #endif
14506deef89SBrian Gerst } };
146f7627e25SThomas Gleixner EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
147f7627e25SThomas Gleixner 
1488c3641e9SDave Hansen static int __init x86_mpx_setup(char *s)
1490c752a93SSuresh Siddha {
1508c3641e9SDave Hansen 	/* require an exact match without trailing characters */
1512cd3949fSDave Hansen 	if (strlen(s))
1522cd3949fSDave Hansen 		return 0;
1530c752a93SSuresh Siddha 
1548c3641e9SDave Hansen 	/* do not emit a message if the feature is not present */
1558c3641e9SDave Hansen 	if (!boot_cpu_has(X86_FEATURE_MPX))
1566bad06b7SSuresh Siddha 		return 1;
1576bad06b7SSuresh Siddha 
1588c3641e9SDave Hansen 	setup_clear_cpu_cap(X86_FEATURE_MPX);
1598c3641e9SDave Hansen 	pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
160b6f42a4aSFenghua Yu 	return 1;
161b6f42a4aSFenghua Yu }
1628c3641e9SDave Hansen __setup("nompx", x86_mpx_setup);
163b6f42a4aSFenghua Yu 
164ba51dcedSYinghai Lu #ifdef CONFIG_X86_32
165148f9bb8SPaul Gortmaker static int cachesize_override = -1;
166148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1;
167f7627e25SThomas Gleixner 
168f7627e25SThomas Gleixner static int __init cachesize_setup(char *str)
169f7627e25SThomas Gleixner {
170f7627e25SThomas Gleixner 	get_option(&str, &cachesize_override);
171f7627e25SThomas Gleixner 	return 1;
172f7627e25SThomas Gleixner }
173f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup);
174f7627e25SThomas Gleixner 
175f7627e25SThomas Gleixner static int __init x86_sep_setup(char *s)
176f7627e25SThomas Gleixner {
17713530257SAndi Kleen 	setup_clear_cpu_cap(X86_FEATURE_SEP);
178f7627e25SThomas Gleixner 	return 1;
179f7627e25SThomas Gleixner }
180f7627e25SThomas Gleixner __setup("nosep", x86_sep_setup);
181f7627e25SThomas Gleixner 
182f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */
183f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag)
184f7627e25SThomas Gleixner {
185f7627e25SThomas Gleixner 	u32 f1, f2;
186f7627e25SThomas Gleixner 
18794f6bac1SKrzysztof Helt 	/*
18894f6bac1SKrzysztof Helt 	 * Cyrix and IDT cpus allow disabling of CPUID
18994f6bac1SKrzysztof Helt 	 * so the code below may return different results
19094f6bac1SKrzysztof Helt 	 * when it is executed before and after enabling
19194f6bac1SKrzysztof Helt 	 * the CPUID. Add "volatile" to not allow gcc to
19294f6bac1SKrzysztof Helt 	 * optimize the subsequent calls to this function.
19394f6bac1SKrzysztof Helt 	 */
19494f6bac1SKrzysztof Helt 	asm volatile ("pushfl		\n\t"
195f7627e25SThomas Gleixner 		      "pushfl		\n\t"
196f7627e25SThomas Gleixner 		      "popl %0		\n\t"
197f7627e25SThomas Gleixner 		      "movl %0, %1	\n\t"
198f7627e25SThomas Gleixner 		      "xorl %2, %0	\n\t"
199f7627e25SThomas Gleixner 		      "pushl %0		\n\t"
200f7627e25SThomas Gleixner 		      "popfl		\n\t"
201f7627e25SThomas Gleixner 		      "pushfl		\n\t"
202f7627e25SThomas Gleixner 		      "popl %0		\n\t"
203f7627e25SThomas Gleixner 		      "popfl		\n\t"
2040f3fa48aSIngo Molnar 
205f7627e25SThomas Gleixner 		      : "=&r" (f1), "=&r" (f2)
206f7627e25SThomas Gleixner 		      : "ir" (flag));
207f7627e25SThomas Gleixner 
208f7627e25SThomas Gleixner 	return ((f1^f2) & flag) != 0;
209f7627e25SThomas Gleixner }
210f7627e25SThomas Gleixner 
211f7627e25SThomas Gleixner /* Probe for the CPUID instruction */
212148f9bb8SPaul Gortmaker int have_cpuid_p(void)
213f7627e25SThomas Gleixner {
214f7627e25SThomas Gleixner 	return flag_is_changeable_p(X86_EFLAGS_ID);
215f7627e25SThomas Gleixner }
216f7627e25SThomas Gleixner 
217148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
2180a488a53SYinghai Lu {
2190a488a53SYinghai Lu 	unsigned long lo, hi;
2200f3fa48aSIngo Molnar 
2210f3fa48aSIngo Molnar 	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
2220f3fa48aSIngo Molnar 		return;
2230f3fa48aSIngo Molnar 
2240f3fa48aSIngo Molnar 	/* Disable processor serial number: */
2250f3fa48aSIngo Molnar 
2260a488a53SYinghai Lu 	rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2270a488a53SYinghai Lu 	lo |= 0x200000;
2280a488a53SYinghai Lu 	wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2290f3fa48aSIngo Molnar 
2300a488a53SYinghai Lu 	printk(KERN_NOTICE "CPU serial number disabled.\n");
2310a488a53SYinghai Lu 	clear_cpu_cap(c, X86_FEATURE_PN);
2320a488a53SYinghai Lu 
2330a488a53SYinghai Lu 	/* Disabling the serial number may affect the cpuid level */
2340a488a53SYinghai Lu 	c->cpuid_level = cpuid_eax(0);
2350a488a53SYinghai Lu }
2360a488a53SYinghai Lu 
2370a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s)
2380a488a53SYinghai Lu {
2390a488a53SYinghai Lu 	disable_x86_serial_nr = 0;
2400a488a53SYinghai Lu 	return 1;
2410a488a53SYinghai Lu }
2420a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup);
243ba51dcedSYinghai Lu #else
244102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag)
245102bbe3aSYinghai Lu {
246102bbe3aSYinghai Lu 	return 1;
247102bbe3aSYinghai Lu }
248102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
249102bbe3aSYinghai Lu {
250102bbe3aSYinghai Lu }
251ba51dcedSYinghai Lu #endif
2520a488a53SYinghai Lu 
253de5397adSFenghua Yu static __init int setup_disable_smep(char *arg)
254de5397adSFenghua Yu {
255b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMEP);
256de5397adSFenghua Yu 	return 1;
257de5397adSFenghua Yu }
258de5397adSFenghua Yu __setup("nosmep", setup_disable_smep);
259de5397adSFenghua Yu 
260b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c)
261de5397adSFenghua Yu {
262b2cc2a07SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMEP))
263375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMEP);
264de5397adSFenghua Yu }
265de5397adSFenghua Yu 
26652b6179aSH. Peter Anvin static __init int setup_disable_smap(char *arg)
26752b6179aSH. Peter Anvin {
268b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMAP);
26952b6179aSH. Peter Anvin 	return 1;
27052b6179aSH. Peter Anvin }
27152b6179aSH. Peter Anvin __setup("nosmap", setup_disable_smap);
27252b6179aSH. Peter Anvin 
273b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c)
27452b6179aSH. Peter Anvin {
275b2cc2a07SH. Peter Anvin 	unsigned long eflags;
276b2cc2a07SH. Peter Anvin 
277b2cc2a07SH. Peter Anvin 	/* This should have been cleared long ago */
278b2cc2a07SH. Peter Anvin 	raw_local_save_flags(eflags);
279b2cc2a07SH. Peter Anvin 	BUG_ON(eflags & X86_EFLAGS_AC);
280b2cc2a07SH. Peter Anvin 
28103bbd596SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMAP)) {
28203bbd596SH. Peter Anvin #ifdef CONFIG_X86_SMAP
283375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMAP);
28403bbd596SH. Peter Anvin #else
285375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_SMAP);
28603bbd596SH. Peter Anvin #endif
28703bbd596SH. Peter Anvin 	}
288f7627e25SThomas Gleixner }
289f7627e25SThomas Gleixner 
290f7627e25SThomas Gleixner /*
291b38b0665SH. Peter Anvin  * Some CPU features depend on higher CPUID levels, which may not always
292b38b0665SH. Peter Anvin  * be available due to CPUID level capping or broken virtualization
293b38b0665SH. Peter Anvin  * software.  Add those features to this table to auto-disable them.
294b38b0665SH. Peter Anvin  */
295b38b0665SH. Peter Anvin struct cpuid_dependent_feature {
296b38b0665SH. Peter Anvin 	u32 feature;
297b38b0665SH. Peter Anvin 	u32 level;
298b38b0665SH. Peter Anvin };
2990f3fa48aSIngo Molnar 
300148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature
301b38b0665SH. Peter Anvin cpuid_dependent_features[] = {
302b38b0665SH. Peter Anvin 	{ X86_FEATURE_MWAIT,		0x00000005 },
303b38b0665SH. Peter Anvin 	{ X86_FEATURE_DCA,		0x00000009 },
304b38b0665SH. Peter Anvin 	{ X86_FEATURE_XSAVE,		0x0000000d },
305b38b0665SH. Peter Anvin 	{ 0, 0 }
306b38b0665SH. Peter Anvin };
307b38b0665SH. Peter Anvin 
308148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
309b38b0665SH. Peter Anvin {
310b38b0665SH. Peter Anvin 	const struct cpuid_dependent_feature *df;
3119766cdbcSJaswinder Singh Rajput 
312b38b0665SH. Peter Anvin 	for (df = cpuid_dependent_features; df->feature; df++) {
3130f3fa48aSIngo Molnar 
3140f3fa48aSIngo Molnar 		if (!cpu_has(c, df->feature))
3150f3fa48aSIngo Molnar 			continue;
316b38b0665SH. Peter Anvin 		/*
317b38b0665SH. Peter Anvin 		 * Note: cpuid_level is set to -1 if unavailable, but
318b38b0665SH. Peter Anvin 		 * extended_extended_level is set to 0 if unavailable
319b38b0665SH. Peter Anvin 		 * and the legitimate extended levels are all negative
320b38b0665SH. Peter Anvin 		 * when signed; hence the weird messing around with
321b38b0665SH. Peter Anvin 		 * signs here...
322b38b0665SH. Peter Anvin 		 */
3230f3fa48aSIngo Molnar 		if (!((s32)df->level < 0 ?
324f6db44dfSYinghai Lu 		     (u32)df->level > (u32)c->extended_cpuid_level :
3250f3fa48aSIngo Molnar 		     (s32)df->level > (s32)c->cpuid_level))
3260f3fa48aSIngo Molnar 			continue;
3270f3fa48aSIngo Molnar 
328b38b0665SH. Peter Anvin 		clear_cpu_cap(c, df->feature);
3290f3fa48aSIngo Molnar 		if (!warn)
3300f3fa48aSIngo Molnar 			continue;
3310f3fa48aSIngo Molnar 
332b38b0665SH. Peter Anvin 		printk(KERN_WARNING
3339def39beSJosh Triplett 		       "CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
3349def39beSJosh Triplett 				x86_cap_flag(df->feature), df->level);
335b38b0665SH. Peter Anvin 	}
336b38b0665SH. Peter Anvin }
337b38b0665SH. Peter Anvin 
338b38b0665SH. Peter Anvin /*
339f7627e25SThomas Gleixner  * Naming convention should be: <Name> [(<Codename>)]
340f7627e25SThomas Gleixner  * This table only is used unless init_<vendor>() below doesn't set it;
3410f3fa48aSIngo Molnar  * in particular, if CPUID levels 0x80000002..4 are supported, this
3420f3fa48aSIngo Molnar  * isn't used
343f7627e25SThomas Gleixner  */
344f7627e25SThomas Gleixner 
345f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */
346148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c)
347f7627e25SThomas Gleixner {
34809dc68d9SJan Beulich #ifdef CONFIG_X86_32
34909dc68d9SJan Beulich 	const struct legacy_cpu_model_info *info;
350f7627e25SThomas Gleixner 
351f7627e25SThomas Gleixner 	if (c->x86_model >= 16)
352f7627e25SThomas Gleixner 		return NULL;	/* Range check */
353f7627e25SThomas Gleixner 
354f7627e25SThomas Gleixner 	if (!this_cpu)
355f7627e25SThomas Gleixner 		return NULL;
356f7627e25SThomas Gleixner 
35709dc68d9SJan Beulich 	info = this_cpu->legacy_models;
358f7627e25SThomas Gleixner 
35909dc68d9SJan Beulich 	while (info->family) {
360f7627e25SThomas Gleixner 		if (info->family == c->x86)
361f7627e25SThomas Gleixner 			return info->model_names[c->x86_model];
362f7627e25SThomas Gleixner 		info++;
363f7627e25SThomas Gleixner 	}
36409dc68d9SJan Beulich #endif
365f7627e25SThomas Gleixner 	return NULL;		/* Not found */
366f7627e25SThomas Gleixner }
367f7627e25SThomas Gleixner 
368148f9bb8SPaul Gortmaker __u32 cpu_caps_cleared[NCAPINTS];
369148f9bb8SPaul Gortmaker __u32 cpu_caps_set[NCAPINTS];
370f7627e25SThomas Gleixner 
37111e3a840SJeremy Fitzhardinge void load_percpu_segment(int cpu)
3729d31d35bSYinghai Lu {
373fab334c1SYinghai Lu #ifdef CONFIG_X86_32
3742697fbd5SBrian Gerst 	loadsegment(fs, __KERNEL_PERCPU);
3752697fbd5SBrian Gerst #else
3762697fbd5SBrian Gerst 	loadsegment(gs, 0);
3772697fbd5SBrian Gerst 	wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
378fab334c1SYinghai Lu #endif
37960a5317fSTejun Heo 	load_stack_canary_segment();
3809d31d35bSYinghai Lu }
3819d31d35bSYinghai Lu 
3820f3fa48aSIngo Molnar /*
3830f3fa48aSIngo Molnar  * Current gdt points %fs at the "master" per-cpu area: after this,
3840f3fa48aSIngo Molnar  * it's on the real one.
3850f3fa48aSIngo Molnar  */
386552be871SBrian Gerst void switch_to_new_gdt(int cpu)
387f7627e25SThomas Gleixner {
388f7627e25SThomas Gleixner 	struct desc_ptr gdt_descr;
389f7627e25SThomas Gleixner 
390f7627e25SThomas Gleixner 	gdt_descr.address = (long)get_cpu_gdt_table(cpu);
391f7627e25SThomas Gleixner 	gdt_descr.size = GDT_SIZE - 1;
392f7627e25SThomas Gleixner 	load_gdt(&gdt_descr);
393f7627e25SThomas Gleixner 	/* Reload the per-cpu base */
39411e3a840SJeremy Fitzhardinge 
39511e3a840SJeremy Fitzhardinge 	load_percpu_segment(cpu);
396f7627e25SThomas Gleixner }
397f7627e25SThomas Gleixner 
398148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
399f7627e25SThomas Gleixner 
400148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c)
401f7627e25SThomas Gleixner {
402f7627e25SThomas Gleixner 	unsigned int *v;
403ee098e1aSBorislav Petkov 	char *p, *q, *s;
404f7627e25SThomas Gleixner 
4053da99c97SYinghai Lu 	if (c->extended_cpuid_level < 0x80000004)
4061b05d60dSYinghai Lu 		return;
407f7627e25SThomas Gleixner 
408f7627e25SThomas Gleixner 	v = (unsigned int *)c->x86_model_id;
409f7627e25SThomas Gleixner 	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
410f7627e25SThomas Gleixner 	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
411f7627e25SThomas Gleixner 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
412f7627e25SThomas Gleixner 	c->x86_model_id[48] = 0;
413f7627e25SThomas Gleixner 
414ee098e1aSBorislav Petkov 	/* Trim whitespace */
415ee098e1aSBorislav Petkov 	p = q = s = &c->x86_model_id[0];
416ee098e1aSBorislav Petkov 
417ee098e1aSBorislav Petkov 	while (*p == ' ')
418ee098e1aSBorislav Petkov 		p++;
419ee098e1aSBorislav Petkov 
420ee098e1aSBorislav Petkov 	while (*p) {
421ee098e1aSBorislav Petkov 		/* Note the last non-whitespace index */
422ee098e1aSBorislav Petkov 		if (!isspace(*p))
423ee098e1aSBorislav Petkov 			s = q;
424ee098e1aSBorislav Petkov 
425ee098e1aSBorislav Petkov 		*q++ = *p++;
426ee098e1aSBorislav Petkov 	}
427ee098e1aSBorislav Petkov 
428ee098e1aSBorislav Petkov 	*(s + 1) = '\0';
429f7627e25SThomas Gleixner }
430f7627e25SThomas Gleixner 
431148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
432f7627e25SThomas Gleixner {
4339d31d35bSYinghai Lu 	unsigned int n, dummy, ebx, ecx, edx, l2size;
434f7627e25SThomas Gleixner 
4353da99c97SYinghai Lu 	n = c->extended_cpuid_level;
436f7627e25SThomas Gleixner 
437f7627e25SThomas Gleixner 	if (n >= 0x80000005) {
4389d31d35bSYinghai Lu 		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
439f7627e25SThomas Gleixner 		c->x86_cache_size = (ecx>>24) + (edx>>24);
440140fc727SYinghai Lu #ifdef CONFIG_X86_64
441140fc727SYinghai Lu 		/* On K8 L1 TLB is inclusive, so don't count it */
442140fc727SYinghai Lu 		c->x86_tlbsize = 0;
443140fc727SYinghai Lu #endif
444f7627e25SThomas Gleixner 	}
445f7627e25SThomas Gleixner 
446f7627e25SThomas Gleixner 	if (n < 0x80000006)	/* Some chips just has a large L1. */
447f7627e25SThomas Gleixner 		return;
448f7627e25SThomas Gleixner 
4490a488a53SYinghai Lu 	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
450f7627e25SThomas Gleixner 	l2size = ecx >> 16;
451f7627e25SThomas Gleixner 
452140fc727SYinghai Lu #ifdef CONFIG_X86_64
453140fc727SYinghai Lu 	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
454140fc727SYinghai Lu #else
455f7627e25SThomas Gleixner 	/* do processor-specific cache resizing */
45609dc68d9SJan Beulich 	if (this_cpu->legacy_cache_size)
45709dc68d9SJan Beulich 		l2size = this_cpu->legacy_cache_size(c, l2size);
458f7627e25SThomas Gleixner 
459f7627e25SThomas Gleixner 	/* Allow user to override all this if necessary. */
460f7627e25SThomas Gleixner 	if (cachesize_override != -1)
461f7627e25SThomas Gleixner 		l2size = cachesize_override;
462f7627e25SThomas Gleixner 
463f7627e25SThomas Gleixner 	if (l2size == 0)
464f7627e25SThomas Gleixner 		return;		/* Again, no L2 cache is possible */
465140fc727SYinghai Lu #endif
466f7627e25SThomas Gleixner 
467f7627e25SThomas Gleixner 	c->x86_cache_size = l2size;
468f7627e25SThomas Gleixner }
469f7627e25SThomas Gleixner 
470e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO];
471e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO];
472e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO];
473e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO];
474e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO];
475e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO];
476dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO];
477e0ba94f1SAlex Shi 
478f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c)
479e0ba94f1SAlex Shi {
480e0ba94f1SAlex Shi 	if (this_cpu->c_detect_tlb)
481e0ba94f1SAlex Shi 		this_cpu->c_detect_tlb(c);
482e0ba94f1SAlex Shi 
483f94fe119SSteven Honeyman 	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
484e0ba94f1SAlex Shi 		tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
485f94fe119SSteven Honeyman 		tlb_lli_4m[ENTRIES]);
486f94fe119SSteven Honeyman 
487f94fe119SSteven Honeyman 	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
488f94fe119SSteven Honeyman 		tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
489f94fe119SSteven Honeyman 		tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
490e0ba94f1SAlex Shi }
491e0ba94f1SAlex Shi 
492148f9bb8SPaul Gortmaker void detect_ht(struct cpuinfo_x86 *c)
4939d31d35bSYinghai Lu {
494c8e56d20SBorislav Petkov #ifdef CONFIG_SMP
4959d31d35bSYinghai Lu 	u32 eax, ebx, ecx, edx;
4969d31d35bSYinghai Lu 	int index_msb, core_bits;
4972eaad1fdSMike Travis 	static bool printed;
4989d31d35bSYinghai Lu 
4990a488a53SYinghai Lu 	if (!cpu_has(c, X86_FEATURE_HT))
5009d31d35bSYinghai Lu 		return;
5019d31d35bSYinghai Lu 
5020a488a53SYinghai Lu 	if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
5030a488a53SYinghai Lu 		goto out;
5040a488a53SYinghai Lu 
5051cd78776SYinghai Lu 	if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
5061cd78776SYinghai Lu 		return;
5071cd78776SYinghai Lu 
5080a488a53SYinghai Lu 	cpuid(1, &eax, &ebx, &ecx, &edx);
5090a488a53SYinghai Lu 
5109d31d35bSYinghai Lu 	smp_num_siblings = (ebx & 0xff0000) >> 16;
5119d31d35bSYinghai Lu 
5129d31d35bSYinghai Lu 	if (smp_num_siblings == 1) {
5132eaad1fdSMike Travis 		printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
5140f3fa48aSIngo Molnar 		goto out;
5150f3fa48aSIngo Molnar 	}
5160f3fa48aSIngo Molnar 
5170f3fa48aSIngo Molnar 	if (smp_num_siblings <= 1)
5180f3fa48aSIngo Molnar 		goto out;
5199d31d35bSYinghai Lu 
5209d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
521cb8cc442SIngo Molnar 	c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
5229d31d35bSYinghai Lu 
5239d31d35bSYinghai Lu 	smp_num_siblings = smp_num_siblings / c->x86_max_cores;
5249d31d35bSYinghai Lu 
5259d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
5269d31d35bSYinghai Lu 
5279d31d35bSYinghai Lu 	core_bits = get_count_order(c->x86_max_cores);
5289d31d35bSYinghai Lu 
529cb8cc442SIngo Molnar 	c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
5301cd78776SYinghai Lu 				       ((1 << core_bits) - 1);
5319d31d35bSYinghai Lu 
5320a488a53SYinghai Lu out:
5332eaad1fdSMike Travis 	if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
5340a488a53SYinghai Lu 		printk(KERN_INFO  "CPU: Physical Processor ID: %d\n",
5350a488a53SYinghai Lu 		       c->phys_proc_id);
5369d31d35bSYinghai Lu 		printk(KERN_INFO  "CPU: Processor Core ID: %d\n",
5379d31d35bSYinghai Lu 		       c->cpu_core_id);
5382eaad1fdSMike Travis 		printed = 1;
5399d31d35bSYinghai Lu 	}
5409d31d35bSYinghai Lu #endif
54197e4db7cSYinghai Lu }
542f7627e25SThomas Gleixner 
543148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c)
544f7627e25SThomas Gleixner {
545f7627e25SThomas Gleixner 	char *v = c->x86_vendor_id;
5460f3fa48aSIngo Molnar 	int i;
547f7627e25SThomas Gleixner 
548f7627e25SThomas Gleixner 	for (i = 0; i < X86_VENDOR_NUM; i++) {
54910a434fcSYinghai Lu 		if (!cpu_devs[i])
55010a434fcSYinghai Lu 			break;
55110a434fcSYinghai Lu 
552f7627e25SThomas Gleixner 		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
553f7627e25SThomas Gleixner 		    (cpu_devs[i]->c_ident[1] &&
554f7627e25SThomas Gleixner 		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
5550f3fa48aSIngo Molnar 
556f7627e25SThomas Gleixner 			this_cpu = cpu_devs[i];
55710a434fcSYinghai Lu 			c->x86_vendor = this_cpu->c_x86_vendor;
558f7627e25SThomas Gleixner 			return;
559f7627e25SThomas Gleixner 		}
560f7627e25SThomas Gleixner 	}
56110a434fcSYinghai Lu 
562a9c56953SMinchan Kim 	printk_once(KERN_ERR
563a9c56953SMinchan Kim 			"CPU: vendor_id '%s' unknown, using generic init.\n" \
564a9c56953SMinchan Kim 			"CPU: Your system may be unstable.\n", v);
56510a434fcSYinghai Lu 
566f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
567f7627e25SThomas Gleixner 	this_cpu = &default_cpu;
568f7627e25SThomas Gleixner }
569f7627e25SThomas Gleixner 
570148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c)
571f7627e25SThomas Gleixner {
572f7627e25SThomas Gleixner 	/* Get vendor name */
5734a148513SHarvey Harrison 	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
5744a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[0],
5754a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[8],
5764a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[4]);
577f7627e25SThomas Gleixner 
578f7627e25SThomas Gleixner 	c->x86 = 4;
5799d31d35bSYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
580f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
581f7627e25SThomas Gleixner 		u32 junk, tfms, cap0, misc;
5820f3fa48aSIngo Molnar 
583f7627e25SThomas Gleixner 		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
5849d31d35bSYinghai Lu 		c->x86 = (tfms >> 8) & 0xf;
5859d31d35bSYinghai Lu 		c->x86_model = (tfms >> 4) & 0xf;
5869d31d35bSYinghai Lu 		c->x86_mask = tfms & 0xf;
5870f3fa48aSIngo Molnar 
588f7627e25SThomas Gleixner 		if (c->x86 == 0xf)
589f7627e25SThomas Gleixner 			c->x86 += (tfms >> 20) & 0xff;
590f7627e25SThomas Gleixner 		if (c->x86 >= 0x6)
5919d31d35bSYinghai Lu 			c->x86_model += ((tfms >> 16) & 0xf) << 4;
5920f3fa48aSIngo Molnar 
593d4387bd3SHuang, Ying 		if (cap0 & (1<<19)) {
594d4387bd3SHuang, Ying 			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
5959d31d35bSYinghai Lu 			c->x86_cache_alignment = c->x86_clflush_size;
596d4387bd3SHuang, Ying 		}
597f7627e25SThomas Gleixner 	}
598f7627e25SThomas Gleixner }
5993da99c97SYinghai Lu 
600148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c)
601093af8d7SYinghai Lu {
602093af8d7SYinghai Lu 	u32 tfms, xlvl;
6033da99c97SYinghai Lu 	u32 ebx;
604093af8d7SYinghai Lu 
605093af8d7SYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
606093af8d7SYinghai Lu 	if (c->cpuid_level >= 0x00000001) {
607093af8d7SYinghai Lu 		u32 capability, excap;
6080f3fa48aSIngo Molnar 
609093af8d7SYinghai Lu 		cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
610093af8d7SYinghai Lu 		c->x86_capability[0] = capability;
611093af8d7SYinghai Lu 		c->x86_capability[4] = excap;
612093af8d7SYinghai Lu 	}
613093af8d7SYinghai Lu 
614bdc802dcSH. Peter Anvin 	/* Additional Intel-defined flags: level 0x00000007 */
615bdc802dcSH. Peter Anvin 	if (c->cpuid_level >= 0x00000007) {
616bdc802dcSH. Peter Anvin 		u32 eax, ebx, ecx, edx;
617bdc802dcSH. Peter Anvin 
618bdc802dcSH. Peter Anvin 		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
619bdc802dcSH. Peter Anvin 
620bdc802dcSH. Peter Anvin 		c->x86_capability[9] = ebx;
621bdc802dcSH. Peter Anvin 	}
622bdc802dcSH. Peter Anvin 
6236229ad27SFenghua Yu 	/* Extended state features: level 0x0000000d */
6246229ad27SFenghua Yu 	if (c->cpuid_level >= 0x0000000d) {
6256229ad27SFenghua Yu 		u32 eax, ebx, ecx, edx;
6266229ad27SFenghua Yu 
6276229ad27SFenghua Yu 		cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
6286229ad27SFenghua Yu 
6296229ad27SFenghua Yu 		c->x86_capability[10] = eax;
6306229ad27SFenghua Yu 	}
6316229ad27SFenghua Yu 
632cbc82b17SPeter P Waskiewicz Jr 	/* Additional Intel-defined flags: level 0x0000000F */
633cbc82b17SPeter P Waskiewicz Jr 	if (c->cpuid_level >= 0x0000000F) {
634cbc82b17SPeter P Waskiewicz Jr 		u32 eax, ebx, ecx, edx;
635cbc82b17SPeter P Waskiewicz Jr 
636cbc82b17SPeter P Waskiewicz Jr 		/* QoS sub-leaf, EAX=0Fh, ECX=0 */
637cbc82b17SPeter P Waskiewicz Jr 		cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
638cbc82b17SPeter P Waskiewicz Jr 		c->x86_capability[11] = edx;
639cbc82b17SPeter P Waskiewicz Jr 		if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
640cbc82b17SPeter P Waskiewicz Jr 			/* will be overridden if occupancy monitoring exists */
641cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_max_rmid = ebx;
642cbc82b17SPeter P Waskiewicz Jr 
643cbc82b17SPeter P Waskiewicz Jr 			/* QoS sub-leaf, EAX=0Fh, ECX=1 */
644cbc82b17SPeter P Waskiewicz Jr 			cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
645cbc82b17SPeter P Waskiewicz Jr 			c->x86_capability[12] = edx;
646cbc82b17SPeter P Waskiewicz Jr 			if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) {
647cbc82b17SPeter P Waskiewicz Jr 				c->x86_cache_max_rmid = ecx;
648cbc82b17SPeter P Waskiewicz Jr 				c->x86_cache_occ_scale = ebx;
649cbc82b17SPeter P Waskiewicz Jr 			}
650cbc82b17SPeter P Waskiewicz Jr 		} else {
651cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_max_rmid = -1;
652cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_occ_scale = -1;
653cbc82b17SPeter P Waskiewicz Jr 		}
654cbc82b17SPeter P Waskiewicz Jr 	}
655cbc82b17SPeter P Waskiewicz Jr 
656093af8d7SYinghai Lu 	/* AMD-defined flags: level 0x80000001 */
657093af8d7SYinghai Lu 	xlvl = cpuid_eax(0x80000000);
6583da99c97SYinghai Lu 	c->extended_cpuid_level = xlvl;
6590f3fa48aSIngo Molnar 
660093af8d7SYinghai Lu 	if ((xlvl & 0xffff0000) == 0x80000000) {
661093af8d7SYinghai Lu 		if (xlvl >= 0x80000001) {
662093af8d7SYinghai Lu 			c->x86_capability[1] = cpuid_edx(0x80000001);
663093af8d7SYinghai Lu 			c->x86_capability[6] = cpuid_ecx(0x80000001);
664093af8d7SYinghai Lu 		}
665093af8d7SYinghai Lu 	}
666093af8d7SYinghai Lu 
6675122c890SYinghai Lu 	if (c->extended_cpuid_level >= 0x80000008) {
6685122c890SYinghai Lu 		u32 eax = cpuid_eax(0x80000008);
6695122c890SYinghai Lu 
6705122c890SYinghai Lu 		c->x86_virt_bits = (eax >> 8) & 0xff;
6715122c890SYinghai Lu 		c->x86_phys_bits = eax & 0xff;
6725122c890SYinghai Lu 	}
67313c6c532SJan Beulich #ifdef CONFIG_X86_32
67413c6c532SJan Beulich 	else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
67513c6c532SJan Beulich 		c->x86_phys_bits = 36;
6765122c890SYinghai Lu #endif
677e3224234SYinghai Lu 
678e3224234SYinghai Lu 	if (c->extended_cpuid_level >= 0x80000007)
679e3224234SYinghai Lu 		c->x86_power = cpuid_edx(0x80000007);
680e3224234SYinghai Lu 
6811dedefd1SJacob Pan 	init_scattered_cpuid_features(c);
682093af8d7SYinghai Lu }
683093af8d7SYinghai Lu 
684148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
685aef93c8bSYinghai Lu {
686aef93c8bSYinghai Lu #ifdef CONFIG_X86_32
687aef93c8bSYinghai Lu 	int i;
688aef93c8bSYinghai Lu 
689aef93c8bSYinghai Lu 	/*
690aef93c8bSYinghai Lu 	 * First of all, decide if this is a 486 or higher
691aef93c8bSYinghai Lu 	 * It's a 486 if we can modify the AC flag
692aef93c8bSYinghai Lu 	 */
693aef93c8bSYinghai Lu 	if (flag_is_changeable_p(X86_EFLAGS_AC))
694aef93c8bSYinghai Lu 		c->x86 = 4;
695aef93c8bSYinghai Lu 	else
696aef93c8bSYinghai Lu 		c->x86 = 3;
697aef93c8bSYinghai Lu 
698aef93c8bSYinghai Lu 	for (i = 0; i < X86_VENDOR_NUM; i++)
699aef93c8bSYinghai Lu 		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
700aef93c8bSYinghai Lu 			c->x86_vendor_id[0] = 0;
701aef93c8bSYinghai Lu 			cpu_devs[i]->c_identify(c);
702aef93c8bSYinghai Lu 			if (c->x86_vendor_id[0]) {
703aef93c8bSYinghai Lu 				get_cpu_vendor(c);
704aef93c8bSYinghai Lu 				break;
705aef93c8bSYinghai Lu 			}
706aef93c8bSYinghai Lu 		}
707aef93c8bSYinghai Lu #endif
708093af8d7SYinghai Lu }
709f7627e25SThomas Gleixner 
71034048c9eSPaolo Ciarrocchi /*
71134048c9eSPaolo Ciarrocchi  * Do minimum CPU detection early.
71234048c9eSPaolo Ciarrocchi  * Fields really needed: vendor, cpuid_level, family, model, mask,
71334048c9eSPaolo Ciarrocchi  * cache alignment.
71434048c9eSPaolo Ciarrocchi  * The others are not touched to avoid unwanted side effects.
71534048c9eSPaolo Ciarrocchi  *
71634048c9eSPaolo Ciarrocchi  * WARNING: this function is only called on the BP.  Don't add code here
71734048c9eSPaolo Ciarrocchi  * that is supposed to run on all CPUs.
71834048c9eSPaolo Ciarrocchi  */
7193da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c)
720f7627e25SThomas Gleixner {
7216627d242SYinghai Lu #ifdef CONFIG_X86_64
7226627d242SYinghai Lu 	c->x86_clflush_size = 64;
72313c6c532SJan Beulich 	c->x86_phys_bits = 36;
72413c6c532SJan Beulich 	c->x86_virt_bits = 48;
7256627d242SYinghai Lu #else
726d4387bd3SHuang, Ying 	c->x86_clflush_size = 32;
72713c6c532SJan Beulich 	c->x86_phys_bits = 32;
72813c6c532SJan Beulich 	c->x86_virt_bits = 32;
7296627d242SYinghai Lu #endif
7300a488a53SYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
731f7627e25SThomas Gleixner 
7323da99c97SYinghai Lu 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
7330a488a53SYinghai Lu 	c->extended_cpuid_level = 0;
7340a488a53SYinghai Lu 
735aef93c8bSYinghai Lu 	if (!have_cpuid_p())
736aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
737aef93c8bSYinghai Lu 
738aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
739f7627e25SThomas Gleixner 	if (!have_cpuid_p())
740f7627e25SThomas Gleixner 		return;
741f7627e25SThomas Gleixner 
742f7627e25SThomas Gleixner 	cpu_detect(c);
7433da99c97SYinghai Lu 	get_cpu_vendor(c);
7443da99c97SYinghai Lu 	get_cpu_cap(c);
74512cf105cSKrzysztof Helt 
74610a434fcSYinghai Lu 	if (this_cpu->c_early_init)
74710a434fcSYinghai Lu 		this_cpu->c_early_init(c);
7483da99c97SYinghai Lu 
749f6e9456cSRobert Richter 	c->cpu_index = 0;
750b38b0665SH. Peter Anvin 	filter_cpuid_features(c, false);
751de5397adSFenghua Yu 
752a110b5ecSBorislav Petkov 	if (this_cpu->c_bsp_init)
753a110b5ecSBorislav Petkov 		this_cpu->c_bsp_init(c);
754c3b83598SBorislav Petkov 
755c3b83598SBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
756db52ef74SIngo Molnar 	fpu__init_system(c);
757f7627e25SThomas Gleixner }
758f7627e25SThomas Gleixner 
7599d31d35bSYinghai Lu void __init early_cpu_init(void)
7609d31d35bSYinghai Lu {
76102dde8b4SJan Beulich 	const struct cpu_dev *const *cdev;
76210a434fcSYinghai Lu 	int count = 0;
7639d31d35bSYinghai Lu 
764ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
7659766cdbcSJaswinder Singh Rajput 	printk(KERN_INFO "KERNEL supported cpus:\n");
76631c997caSIngo Molnar #endif
76731c997caSIngo Molnar 
76810a434fcSYinghai Lu 	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
76902dde8b4SJan Beulich 		const struct cpu_dev *cpudev = *cdev;
7709d31d35bSYinghai Lu 
77110a434fcSYinghai Lu 		if (count >= X86_VENDOR_NUM)
77210a434fcSYinghai Lu 			break;
77310a434fcSYinghai Lu 		cpu_devs[count] = cpudev;
77410a434fcSYinghai Lu 		count++;
77510a434fcSYinghai Lu 
776ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
77731c997caSIngo Molnar 		{
77831c997caSIngo Molnar 			unsigned int j;
77931c997caSIngo Molnar 
78010a434fcSYinghai Lu 			for (j = 0; j < 2; j++) {
78110a434fcSYinghai Lu 				if (!cpudev->c_ident[j])
78210a434fcSYinghai Lu 					continue;
7839766cdbcSJaswinder Singh Rajput 				printk(KERN_INFO "  %s %s\n", cpudev->c_vendor,
78410a434fcSYinghai Lu 					cpudev->c_ident[j]);
78510a434fcSYinghai Lu 			}
78610a434fcSYinghai Lu 		}
7870388423dSDave Jones #endif
78831c997caSIngo Molnar 	}
7899d31d35bSYinghai Lu 	early_identify_cpu(&boot_cpu_data);
790f7627e25SThomas Gleixner }
791f7627e25SThomas Gleixner 
792b6734c35SH. Peter Anvin /*
793366d4a43SBorislav Petkov  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
794366d4a43SBorislav Petkov  * unfortunately, that's not true in practice because of early VIA
795366d4a43SBorislav Petkov  * chips and (more importantly) broken virtualizers that are not easy
796366d4a43SBorislav Petkov  * to detect. In the latter case it doesn't even *fail* reliably, so
797366d4a43SBorislav Petkov  * probing for it doesn't even work. Disable it completely on 32-bit
798ba0593bfSH. Peter Anvin  * unless we can find a reliable way to detect all the broken cases.
799366d4a43SBorislav Petkov  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
800b6734c35SH. Peter Anvin  */
801148f9bb8SPaul Gortmaker static void detect_nopl(struct cpuinfo_x86 *c)
802b6734c35SH. Peter Anvin {
803366d4a43SBorislav Petkov #ifdef CONFIG_X86_32
804b6734c35SH. Peter Anvin 	clear_cpu_cap(c, X86_FEATURE_NOPL);
805366d4a43SBorislav Petkov #else
806366d4a43SBorislav Petkov 	set_cpu_cap(c, X86_FEATURE_NOPL);
807366d4a43SBorislav Petkov #endif
808f7627e25SThomas Gleixner }
809f7627e25SThomas Gleixner 
810148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c)
811f7627e25SThomas Gleixner {
8123da99c97SYinghai Lu 	c->extended_cpuid_level = 0;
813f7627e25SThomas Gleixner 
814aef93c8bSYinghai Lu 	if (!have_cpuid_p())
815aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
816f7627e25SThomas Gleixner 
817aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
818a9853dd6SIngo Molnar 	if (!have_cpuid_p())
819aef93c8bSYinghai Lu 		return;
820aef93c8bSYinghai Lu 
8213da99c97SYinghai Lu 	cpu_detect(c);
8223da99c97SYinghai Lu 
8233da99c97SYinghai Lu 	get_cpu_vendor(c);
8243da99c97SYinghai Lu 
8253da99c97SYinghai Lu 	get_cpu_cap(c);
8263da99c97SYinghai Lu 
827f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
8283da99c97SYinghai Lu 		c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
829b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32
830c8e56d20SBorislav Petkov # ifdef CONFIG_SMP
831cb8cc442SIngo Molnar 		c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
832f7627e25SThomas Gleixner # else
83301aaea1aSYinghai Lu 		c->apicid = c->initial_apicid;
834f7627e25SThomas Gleixner # endif
835b89d3b3eSYinghai Lu #endif
836b89d3b3eSYinghai Lu 		c->phys_proc_id = c->initial_apicid;
837f7627e25SThomas Gleixner 	}
838f7627e25SThomas Gleixner 
839f7627e25SThomas Gleixner 	get_model_name(c); /* Default name */
840f7627e25SThomas Gleixner 
841b6734c35SH. Peter Anvin 	detect_nopl(c);
842f7627e25SThomas Gleixner }
843f7627e25SThomas Gleixner 
844cbc82b17SPeter P Waskiewicz Jr static void x86_init_cache_qos(struct cpuinfo_x86 *c)
845cbc82b17SPeter P Waskiewicz Jr {
846cbc82b17SPeter P Waskiewicz Jr 	/*
847cbc82b17SPeter P Waskiewicz Jr 	 * The heavy lifting of max_rmid and cache_occ_scale are handled
848cbc82b17SPeter P Waskiewicz Jr 	 * in get_cpu_cap().  Here we just set the max_rmid for the boot_cpu
849cbc82b17SPeter P Waskiewicz Jr 	 * in case CQM bits really aren't there in this CPU.
850cbc82b17SPeter P Waskiewicz Jr 	 */
851cbc82b17SPeter P Waskiewicz Jr 	if (c != &boot_cpu_data) {
852cbc82b17SPeter P Waskiewicz Jr 		boot_cpu_data.x86_cache_max_rmid =
853cbc82b17SPeter P Waskiewicz Jr 			min(boot_cpu_data.x86_cache_max_rmid,
854cbc82b17SPeter P Waskiewicz Jr 			    c->x86_cache_max_rmid);
855cbc82b17SPeter P Waskiewicz Jr 	}
856cbc82b17SPeter P Waskiewicz Jr }
857cbc82b17SPeter P Waskiewicz Jr 
858f7627e25SThomas Gleixner /*
859f7627e25SThomas Gleixner  * This does the hard work of actually picking apart the CPU stuff...
860f7627e25SThomas Gleixner  */
861148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c)
862f7627e25SThomas Gleixner {
863f7627e25SThomas Gleixner 	int i;
864f7627e25SThomas Gleixner 
865f7627e25SThomas Gleixner 	c->loops_per_jiffy = loops_per_jiffy;
866f7627e25SThomas Gleixner 	c->x86_cache_size = -1;
867f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
868f7627e25SThomas Gleixner 	c->x86_model = c->x86_mask = 0;	/* So far unknown... */
869f7627e25SThomas Gleixner 	c->x86_vendor_id[0] = '\0'; /* Unset */
870f7627e25SThomas Gleixner 	c->x86_model_id[0] = '\0';  /* Unset */
871f7627e25SThomas Gleixner 	c->x86_max_cores = 1;
872102bbe3aSYinghai Lu 	c->x86_coreid_bits = 0;
87311fdd252SYinghai Lu #ifdef CONFIG_X86_64
874102bbe3aSYinghai Lu 	c->x86_clflush_size = 64;
87513c6c532SJan Beulich 	c->x86_phys_bits = 36;
87613c6c532SJan Beulich 	c->x86_virt_bits = 48;
877102bbe3aSYinghai Lu #else
878102bbe3aSYinghai Lu 	c->cpuid_level = -1;	/* CPUID not detected */
879f7627e25SThomas Gleixner 	c->x86_clflush_size = 32;
88013c6c532SJan Beulich 	c->x86_phys_bits = 32;
88113c6c532SJan Beulich 	c->x86_virt_bits = 32;
882102bbe3aSYinghai Lu #endif
883102bbe3aSYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
884f7627e25SThomas Gleixner 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
885f7627e25SThomas Gleixner 
886f7627e25SThomas Gleixner 	generic_identify(c);
887f7627e25SThomas Gleixner 
8883898534dSAndi Kleen 	if (this_cpu->c_identify)
889f7627e25SThomas Gleixner 		this_cpu->c_identify(c);
890f7627e25SThomas Gleixner 
8912759c328SYinghai Lu 	/* Clear/Set all flags overriden by options, after probe */
8922759c328SYinghai Lu 	for (i = 0; i < NCAPINTS; i++) {
8932759c328SYinghai Lu 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
8942759c328SYinghai Lu 		c->x86_capability[i] |= cpu_caps_set[i];
8952759c328SYinghai Lu 	}
8962759c328SYinghai Lu 
897102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
898cb8cc442SIngo Molnar 	c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
899102bbe3aSYinghai Lu #endif
900102bbe3aSYinghai Lu 
901f7627e25SThomas Gleixner 	/*
902f7627e25SThomas Gleixner 	 * Vendor-specific initialization.  In this section we
903f7627e25SThomas Gleixner 	 * canonicalize the feature flags, meaning if there are
904f7627e25SThomas Gleixner 	 * features a certain CPU supports which CPUID doesn't
905f7627e25SThomas Gleixner 	 * tell us, CPUID claiming incorrect flags, or other bugs,
906f7627e25SThomas Gleixner 	 * we handle them here.
907f7627e25SThomas Gleixner 	 *
908f7627e25SThomas Gleixner 	 * At the end of this section, c->x86_capability better
909f7627e25SThomas Gleixner 	 * indicate the features this CPU genuinely supports!
910f7627e25SThomas Gleixner 	 */
911f7627e25SThomas Gleixner 	if (this_cpu->c_init)
912f7627e25SThomas Gleixner 		this_cpu->c_init(c);
913f7627e25SThomas Gleixner 
914f7627e25SThomas Gleixner 	/* Disable the PN if appropriate */
915f7627e25SThomas Gleixner 	squash_the_stupid_serial_number(c);
916f7627e25SThomas Gleixner 
917b2cc2a07SH. Peter Anvin 	/* Set up SMEP/SMAP */
918b2cc2a07SH. Peter Anvin 	setup_smep(c);
919b2cc2a07SH. Peter Anvin 	setup_smap(c);
920b2cc2a07SH. Peter Anvin 
921f7627e25SThomas Gleixner 	/*
9220f3fa48aSIngo Molnar 	 * The vendor-specific functions might have changed features.
9230f3fa48aSIngo Molnar 	 * Now we do "generic changes."
924f7627e25SThomas Gleixner 	 */
925f7627e25SThomas Gleixner 
926b38b0665SH. Peter Anvin 	/* Filter out anything that depends on CPUID levels we don't have */
927b38b0665SH. Peter Anvin 	filter_cpuid_features(c, true);
928b38b0665SH. Peter Anvin 
929f7627e25SThomas Gleixner 	/* If the model name is still unset, do table lookup. */
930f7627e25SThomas Gleixner 	if (!c->x86_model_id[0]) {
93102dde8b4SJan Beulich 		const char *p;
932f7627e25SThomas Gleixner 		p = table_lookup_model(c);
933f7627e25SThomas Gleixner 		if (p)
934f7627e25SThomas Gleixner 			strcpy(c->x86_model_id, p);
935f7627e25SThomas Gleixner 		else
936f7627e25SThomas Gleixner 			/* Last resort... */
937f7627e25SThomas Gleixner 			sprintf(c->x86_model_id, "%02x/%02x",
938f7627e25SThomas Gleixner 				c->x86, c->x86_model);
939f7627e25SThomas Gleixner 	}
940f7627e25SThomas Gleixner 
941102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
942102bbe3aSYinghai Lu 	detect_ht(c);
943102bbe3aSYinghai Lu #endif
944102bbe3aSYinghai Lu 
94588b094fbSAlok Kataria 	init_hypervisor(c);
94649d859d7SH. Peter Anvin 	x86_init_rdrand(c);
947cbc82b17SPeter P Waskiewicz Jr 	x86_init_cache_qos(c);
9483e0c3737SYinghai Lu 
9493e0c3737SYinghai Lu 	/*
9503e0c3737SYinghai Lu 	 * Clear/Set all flags overriden by options, need do it
9513e0c3737SYinghai Lu 	 * before following smp all cpus cap AND.
9523e0c3737SYinghai Lu 	 */
9533e0c3737SYinghai Lu 	for (i = 0; i < NCAPINTS; i++) {
9543e0c3737SYinghai Lu 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
9553e0c3737SYinghai Lu 		c->x86_capability[i] |= cpu_caps_set[i];
9563e0c3737SYinghai Lu 	}
9573e0c3737SYinghai Lu 
958f7627e25SThomas Gleixner 	/*
959f7627e25SThomas Gleixner 	 * On SMP, boot_cpu_data holds the common feature set between
960f7627e25SThomas Gleixner 	 * all CPUs; so make sure that we indicate which features are
961f7627e25SThomas Gleixner 	 * common between the CPUs.  The first time this routine gets
962f7627e25SThomas Gleixner 	 * executed, c == &boot_cpu_data.
963f7627e25SThomas Gleixner 	 */
964f7627e25SThomas Gleixner 	if (c != &boot_cpu_data) {
965f7627e25SThomas Gleixner 		/* AND the already accumulated flags with these */
966f7627e25SThomas Gleixner 		for (i = 0; i < NCAPINTS; i++)
967f7627e25SThomas Gleixner 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
96865fc985bSBorislav Petkov 
96965fc985bSBorislav Petkov 		/* OR, i.e. replicate the bug flags */
97065fc985bSBorislav Petkov 		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
97165fc985bSBorislav Petkov 			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
972f7627e25SThomas Gleixner 	}
973f7627e25SThomas Gleixner 
974f7627e25SThomas Gleixner 	/* Init Machine Check Exception if available. */
9755e09954aSBorislav Petkov 	mcheck_cpu_init(c);
97630d432dfSAndi Kleen 
97730d432dfSAndi Kleen 	select_idle_routine(c);
978102bbe3aSYinghai Lu 
979de2d9445STejun Heo #ifdef CONFIG_NUMA
980102bbe3aSYinghai Lu 	numa_add_cpu(smp_processor_id());
981102bbe3aSYinghai Lu #endif
982f7627e25SThomas Gleixner }
983f7627e25SThomas Gleixner 
9848b6c0ab1SIngo Molnar /*
9858b6c0ab1SIngo Molnar  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
9868b6c0ab1SIngo Molnar  * on 32-bit kernels:
9878b6c0ab1SIngo Molnar  */
988cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32
989cfda7bb9SAndy Lutomirski void enable_sep_cpu(void)
990cfda7bb9SAndy Lutomirski {
9918b6c0ab1SIngo Molnar 	struct tss_struct *tss;
9928b6c0ab1SIngo Molnar 	int cpu;
993cfda7bb9SAndy Lutomirski 
9948b6c0ab1SIngo Molnar 	cpu = get_cpu();
9958b6c0ab1SIngo Molnar 	tss = &per_cpu(cpu_tss, cpu);
9968b6c0ab1SIngo Molnar 
9978b6c0ab1SIngo Molnar 	if (!boot_cpu_has(X86_FEATURE_SEP))
9988b6c0ab1SIngo Molnar 		goto out;
9998b6c0ab1SIngo Molnar 
10008b6c0ab1SIngo Molnar 	/*
1001cf9328ccSAndy Lutomirski 	 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1002cf9328ccSAndy Lutomirski 	 * see the big comment in struct x86_hw_tss's definition.
10038b6c0ab1SIngo Molnar 	 */
1004cfda7bb9SAndy Lutomirski 
1005cfda7bb9SAndy Lutomirski 	tss->x86_tss.ss1 = __KERNEL_CS;
10068b6c0ab1SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
10078b6c0ab1SIngo Molnar 
1008cf9328ccSAndy Lutomirski 	wrmsr(MSR_IA32_SYSENTER_ESP,
1009cf9328ccSAndy Lutomirski 	      (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
1010cf9328ccSAndy Lutomirski 	      0);
10118b6c0ab1SIngo Molnar 
10124c8cd0c5SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
10138b6c0ab1SIngo Molnar 
10148b6c0ab1SIngo Molnar out:
1015cfda7bb9SAndy Lutomirski 	put_cpu();
1016cfda7bb9SAndy Lutomirski }
1017e04d645fSGlauber Costa #endif
1018e04d645fSGlauber Costa 
1019f7627e25SThomas Gleixner void __init identify_boot_cpu(void)
1020f7627e25SThomas Gleixner {
1021f7627e25SThomas Gleixner 	identify_cpu(&boot_cpu_data);
102202c68a02SLen Brown 	init_amd_e400_c1e_mask();
1023102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1024f7627e25SThomas Gleixner 	sysenter_setup();
1025f7627e25SThomas Gleixner 	enable_sep_cpu();
1026102bbe3aSYinghai Lu #endif
1027e0ba94f1SAlex Shi 	cpu_detect_tlb(&boot_cpu_data);
1028f7627e25SThomas Gleixner }
1029f7627e25SThomas Gleixner 
1030148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c)
1031f7627e25SThomas Gleixner {
1032f7627e25SThomas Gleixner 	BUG_ON(c == &boot_cpu_data);
1033f7627e25SThomas Gleixner 	identify_cpu(c);
1034102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1035f7627e25SThomas Gleixner 	enable_sep_cpu();
1036102bbe3aSYinghai Lu #endif
1037f7627e25SThomas Gleixner 	mtrr_ap_init();
1038f7627e25SThomas Gleixner }
1039f7627e25SThomas Gleixner 
1040a0854a46SYinghai Lu struct msr_range {
1041a0854a46SYinghai Lu 	unsigned	min;
1042a0854a46SYinghai Lu 	unsigned	max;
1043a0854a46SYinghai Lu };
1044a0854a46SYinghai Lu 
1045148f9bb8SPaul Gortmaker static const struct msr_range msr_range_array[] = {
1046a0854a46SYinghai Lu 	{ 0x00000000, 0x00000418},
1047a0854a46SYinghai Lu 	{ 0xc0000000, 0xc000040b},
1048a0854a46SYinghai Lu 	{ 0xc0010000, 0xc0010142},
1049a0854a46SYinghai Lu 	{ 0xc0011000, 0xc001103b},
1050a0854a46SYinghai Lu };
1051a0854a46SYinghai Lu 
1052148f9bb8SPaul Gortmaker static void __print_cpu_msr(void)
1053f7627e25SThomas Gleixner {
10540f3fa48aSIngo Molnar 	unsigned index_min, index_max;
1055a0854a46SYinghai Lu 	unsigned index;
1056a0854a46SYinghai Lu 	u64 val;
1057a0854a46SYinghai Lu 	int i;
1058f7627e25SThomas Gleixner 
1059a0854a46SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
1060a0854a46SYinghai Lu 		index_min = msr_range_array[i].min;
1061a0854a46SYinghai Lu 		index_max = msr_range_array[i].max;
10620f3fa48aSIngo Molnar 
1063a0854a46SYinghai Lu 		for (index = index_min; index < index_max; index++) {
1064ecd431d9SBorislav Petkov 			if (rdmsrl_safe(index, &val))
1065a0854a46SYinghai Lu 				continue;
1066a0854a46SYinghai Lu 			printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
1067f7627e25SThomas Gleixner 		}
1068f7627e25SThomas Gleixner 	}
1069a0854a46SYinghai Lu }
1070a0854a46SYinghai Lu 
1071148f9bb8SPaul Gortmaker static int show_msr;
10720f3fa48aSIngo Molnar 
1073a0854a46SYinghai Lu static __init int setup_show_msr(char *arg)
1074a0854a46SYinghai Lu {
1075a0854a46SYinghai Lu 	int num;
1076a0854a46SYinghai Lu 
1077a0854a46SYinghai Lu 	get_option(&arg, &num);
1078a0854a46SYinghai Lu 
1079a0854a46SYinghai Lu 	if (num > 0)
1080a0854a46SYinghai Lu 		show_msr = num;
1081a0854a46SYinghai Lu 	return 1;
1082a0854a46SYinghai Lu }
1083a0854a46SYinghai Lu __setup("show_msr=", setup_show_msr);
1084f7627e25SThomas Gleixner 
1085191679fdSAndi Kleen static __init int setup_noclflush(char *arg)
1086191679fdSAndi Kleen {
1087840d2830SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1088da4aaa7dSH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1089191679fdSAndi Kleen 	return 1;
1090191679fdSAndi Kleen }
1091191679fdSAndi Kleen __setup("noclflush", setup_noclflush);
1092191679fdSAndi Kleen 
1093148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c)
1094f7627e25SThomas Gleixner {
109502dde8b4SJan Beulich 	const char *vendor = NULL;
1096f7627e25SThomas Gleixner 
10970f3fa48aSIngo Molnar 	if (c->x86_vendor < X86_VENDOR_NUM) {
1098f7627e25SThomas Gleixner 		vendor = this_cpu->c_vendor;
10990f3fa48aSIngo Molnar 	} else {
11000f3fa48aSIngo Molnar 		if (c->cpuid_level >= 0)
1101f7627e25SThomas Gleixner 			vendor = c->x86_vendor_id;
11020f3fa48aSIngo Molnar 	}
1103f7627e25SThomas Gleixner 
1104bd32a8cfSYinghai Lu 	if (vendor && !strstr(c->x86_model_id, vendor))
11059d31d35bSYinghai Lu 		printk(KERN_CONT "%s ", vendor);
1106f7627e25SThomas Gleixner 
11079d31d35bSYinghai Lu 	if (c->x86_model_id[0])
1108adafb98dSPrarit Bhargava 		printk(KERN_CONT "%s", c->x86_model_id);
1109f7627e25SThomas Gleixner 	else
11109d31d35bSYinghai Lu 		printk(KERN_CONT "%d86", c->x86);
1111f7627e25SThomas Gleixner 
1112924e101aSBorislav Petkov 	printk(KERN_CONT " (fam: %02x, model: %02x", c->x86, c->x86_model);
1113924e101aSBorislav Petkov 
1114f7627e25SThomas Gleixner 	if (c->x86_mask || c->cpuid_level >= 0)
1115924e101aSBorislav Petkov 		printk(KERN_CONT ", stepping: %02x)\n", c->x86_mask);
1116f7627e25SThomas Gleixner 	else
1117924e101aSBorislav Petkov 		printk(KERN_CONT ")\n");
1118a0854a46SYinghai Lu 
11190b8b8078SYinghai Lu 	print_cpu_msr(c);
112021c3fcf3SYinghai Lu }
112121c3fcf3SYinghai Lu 
1122148f9bb8SPaul Gortmaker void print_cpu_msr(struct cpuinfo_x86 *c)
112321c3fcf3SYinghai Lu {
1124a0854a46SYinghai Lu 	if (c->cpu_index < show_msr)
112521c3fcf3SYinghai Lu 		__print_cpu_msr();
1126f7627e25SThomas Gleixner }
1127f7627e25SThomas Gleixner 
1128ac72e788SAndi Kleen static __init int setup_disablecpuid(char *arg)
1129ac72e788SAndi Kleen {
1130ac72e788SAndi Kleen 	int bit;
11310f3fa48aSIngo Molnar 
1132ac72e788SAndi Kleen 	if (get_option(&arg, &bit) && bit < NCAPINTS*32)
1133ac72e788SAndi Kleen 		setup_clear_cpu_cap(bit);
1134ac72e788SAndi Kleen 	else
1135ac72e788SAndi Kleen 		return 0;
11360f3fa48aSIngo Molnar 
1137ac72e788SAndi Kleen 	return 1;
1138ac72e788SAndi Kleen }
1139ac72e788SAndi Kleen __setup("clearcpuid=", setup_disablecpuid);
1140ac72e788SAndi Kleen 
1141d5494d4fSYinghai Lu #ifdef CONFIG_X86_64
11429ff80942SCyrill Gorcunov struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
1143629f4f9dSSeiji Aguchi struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
1144629f4f9dSSeiji Aguchi 				    (unsigned long) debug_idt_table };
1145d5494d4fSYinghai Lu 
1146947e76cdSBrian Gerst DEFINE_PER_CPU_FIRST(union irq_stack_union,
1147277d5b40SAndi Kleen 		     irq_stack_union) __aligned(PAGE_SIZE) __visible;
11480f3fa48aSIngo Molnar 
1149bdf977b3STejun Heo /*
1150a7fcf28dSAndy Lutomirski  * The following percpu variables are hot.  Align current_task to
1151a7fcf28dSAndy Lutomirski  * cacheline size such that they fall in the same cacheline.
1152bdf977b3STejun Heo  */
1153bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1154bdf977b3STejun Heo 	&init_task;
1155bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1156d5494d4fSYinghai Lu 
1157bdf977b3STejun Heo DEFINE_PER_CPU(char *, irq_stack_ptr) =
1158bdf977b3STejun Heo 	init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
1159bdf977b3STejun Heo 
1160277d5b40SAndi Kleen DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
1161d5494d4fSYinghai Lu 
1162c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1163c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1164c2daa3beSPeter Zijlstra 
11650f3fa48aSIngo Molnar /*
11660f3fa48aSIngo Molnar  * Special IST stacks which the CPU switches to when it calls
11670f3fa48aSIngo Molnar  * an IST-marked descriptor entry. Up to 7 stacks (hardware
11680f3fa48aSIngo Molnar  * limit), all of them are 4K, except the debug stack which
11690f3fa48aSIngo Molnar  * is 8K.
11700f3fa48aSIngo Molnar  */
11710f3fa48aSIngo Molnar static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
11720f3fa48aSIngo Molnar 	  [0 ... N_EXCEPTION_STACKS - 1]	= EXCEPTION_STKSZ,
11730f3fa48aSIngo Molnar 	  [DEBUG_STACK - 1]			= DEBUG_STKSZ
11740f3fa48aSIngo Molnar };
11750f3fa48aSIngo Molnar 
117692d65b23SBrian Gerst static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
11773e352aa8STejun Heo 	[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
1178d5494d4fSYinghai Lu 
1179d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */
1180d5494d4fSYinghai Lu void syscall_init(void)
1181d5494d4fSYinghai Lu {
1182d5494d4fSYinghai Lu 	/*
1183d5494d4fSYinghai Lu 	 * LSTAR and STAR live in a bit strange symbiosis.
1184d5494d4fSYinghai Lu 	 * They both write to the same internal register. STAR allows to
1185d5494d4fSYinghai Lu 	 * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
1186d5494d4fSYinghai Lu 	 */
1187d5494d4fSYinghai Lu 	wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32);
1188b2502b41SIngo Molnar 	wrmsrl(MSR_LSTAR, entry_SYSCALL_64);
1189d56fe4bfSIngo Molnar 
1190d56fe4bfSIngo Molnar #ifdef CONFIG_IA32_EMULATION
11912cd23553SIngo Molnar 	wrmsrl(MSR_CSTAR, entry_SYSCALL_compat);
1192a76c7f46SDenys Vlasenko 	/*
1193487d1edbSDenys Vlasenko 	 * This only works on Intel CPUs.
1194487d1edbSDenys Vlasenko 	 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1195487d1edbSDenys Vlasenko 	 * This does not cause SYSENTER to jump to the wrong location, because
1196487d1edbSDenys Vlasenko 	 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1197a76c7f46SDenys Vlasenko 	 */
1198a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1199a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
12004c8cd0c5SIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
1201d56fe4bfSIngo Molnar #else
1202d56fe4bfSIngo Molnar 	wrmsrl(MSR_CSTAR, ignore_sysret);
12036b51311cSBorislav Petkov 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1204d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1205d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1206d5494d4fSYinghai Lu #endif
1207d5494d4fSYinghai Lu 
1208d5494d4fSYinghai Lu 	/* Flags to clear on syscall */
1209d5494d4fSYinghai Lu 	wrmsrl(MSR_SYSCALL_MASK,
121063bcff2aSH. Peter Anvin 	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
12118c7aa698SAndy Lutomirski 	       X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1212d5494d4fSYinghai Lu }
1213d5494d4fSYinghai Lu 
1214d5494d4fSYinghai Lu /*
1215d5494d4fSYinghai Lu  * Copies of the original ist values from the tss are only accessed during
1216d5494d4fSYinghai Lu  * debugging, no special alignment required.
1217d5494d4fSYinghai Lu  */
1218d5494d4fSYinghai Lu DEFINE_PER_CPU(struct orig_ist, orig_ist);
1219d5494d4fSYinghai Lu 
1220228bdaa9SSteven Rostedt static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
122142181186SSteven Rostedt DEFINE_PER_CPU(int, debug_stack_usage);
1222228bdaa9SSteven Rostedt 
1223228bdaa9SSteven Rostedt int is_debug_stack(unsigned long addr)
1224228bdaa9SSteven Rostedt {
122589cbc767SChristoph Lameter 	return __this_cpu_read(debug_stack_usage) ||
122689cbc767SChristoph Lameter 		(addr <= __this_cpu_read(debug_stack_addr) &&
122789cbc767SChristoph Lameter 		 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
1228228bdaa9SSteven Rostedt }
12290f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(is_debug_stack);
1230228bdaa9SSteven Rostedt 
1231629f4f9dSSeiji Aguchi DEFINE_PER_CPU(u32, debug_idt_ctr);
1232f8988175SSteven Rostedt 
1233228bdaa9SSteven Rostedt void debug_stack_set_zero(void)
1234228bdaa9SSteven Rostedt {
1235629f4f9dSSeiji Aguchi 	this_cpu_inc(debug_idt_ctr);
1236629f4f9dSSeiji Aguchi 	load_current_idt();
1237228bdaa9SSteven Rostedt }
12380f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_set_zero);
1239228bdaa9SSteven Rostedt 
1240228bdaa9SSteven Rostedt void debug_stack_reset(void)
1241228bdaa9SSteven Rostedt {
1242629f4f9dSSeiji Aguchi 	if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
1243f8988175SSteven Rostedt 		return;
1244629f4f9dSSeiji Aguchi 	if (this_cpu_dec_return(debug_idt_ctr) == 0)
1245629f4f9dSSeiji Aguchi 		load_current_idt();
1246228bdaa9SSteven Rostedt }
12470f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_reset);
1248228bdaa9SSteven Rostedt 
12490f3fa48aSIngo Molnar #else	/* CONFIG_X86_64 */
1250d5494d4fSYinghai Lu 
1251bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1252bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1253c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1254c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1255bdf977b3STejun Heo 
1256a7fcf28dSAndy Lutomirski /*
1257a7fcf28dSAndy Lutomirski  * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1258a7fcf28dSAndy Lutomirski  * the top of the kernel stack.  Use an extra percpu variable to track the
1259a7fcf28dSAndy Lutomirski  * top of the kernel stack directly.
1260a7fcf28dSAndy Lutomirski  */
1261a7fcf28dSAndy Lutomirski DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1262a7fcf28dSAndy Lutomirski 	(unsigned long)&init_thread_union + THREAD_SIZE;
1263a7fcf28dSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1264a7fcf28dSAndy Lutomirski 
126560a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR
126653f82452SJeremy Fitzhardinge DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
126760a5317fSTejun Heo #endif
126860a5317fSTejun Heo 
12690f3fa48aSIngo Molnar #endif	/* CONFIG_X86_64 */
1270f7627e25SThomas Gleixner 
1271f7627e25SThomas Gleixner /*
12729766cdbcSJaswinder Singh Rajput  * Clear all 6 debug registers:
12739766cdbcSJaswinder Singh Rajput  */
12749766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void)
12759766cdbcSJaswinder Singh Rajput {
12769766cdbcSJaswinder Singh Rajput 	int i;
12779766cdbcSJaswinder Singh Rajput 
12789766cdbcSJaswinder Singh Rajput 	for (i = 0; i < 8; i++) {
12799766cdbcSJaswinder Singh Rajput 		/* Ignore db4, db5 */
12809766cdbcSJaswinder Singh Rajput 		if ((i == 4) || (i == 5))
12819766cdbcSJaswinder Singh Rajput 			continue;
12829766cdbcSJaswinder Singh Rajput 
12839766cdbcSJaswinder Singh Rajput 		set_debugreg(0, i);
12849766cdbcSJaswinder Singh Rajput 	}
12859766cdbcSJaswinder Singh Rajput }
1286f7627e25SThomas Gleixner 
12870bb9fef9SJason Wessel #ifdef CONFIG_KGDB
12880bb9fef9SJason Wessel /*
12890bb9fef9SJason Wessel  * Restore debug regs if using kgdbwait and you have a kernel debugger
12900bb9fef9SJason Wessel  * connection established.
12910bb9fef9SJason Wessel  */
12920bb9fef9SJason Wessel static void dbg_restore_debug_regs(void)
12930bb9fef9SJason Wessel {
12940bb9fef9SJason Wessel 	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
12950bb9fef9SJason Wessel 		arch_kgdb_ops.correct_hw_break();
12960bb9fef9SJason Wessel }
12970bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */
12980bb9fef9SJason Wessel #define dbg_restore_debug_regs()
12990bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */
13000bb9fef9SJason Wessel 
1301ce4b1b16SIgor Mammedov static void wait_for_master_cpu(int cpu)
1302ce4b1b16SIgor Mammedov {
1303ce4b1b16SIgor Mammedov #ifdef CONFIG_SMP
1304ce4b1b16SIgor Mammedov 	/*
1305ce4b1b16SIgor Mammedov 	 * wait for ACK from master CPU before continuing
1306ce4b1b16SIgor Mammedov 	 * with AP initialization
1307ce4b1b16SIgor Mammedov 	 */
1308ce4b1b16SIgor Mammedov 	WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1309ce4b1b16SIgor Mammedov 	while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1310ce4b1b16SIgor Mammedov 		cpu_relax();
1311ce4b1b16SIgor Mammedov #endif
1312ce4b1b16SIgor Mammedov }
1313ce4b1b16SIgor Mammedov 
1314f7627e25SThomas Gleixner /*
1315f7627e25SThomas Gleixner  * cpu_init() initializes state that is per-CPU. Some data is already
1316f7627e25SThomas Gleixner  * initialized (naturally) in the bootstrap process, such as the GDT
1317f7627e25SThomas Gleixner  * and IDT. We reload them nevertheless, this function acts as a
1318f7627e25SThomas Gleixner  * 'CPU state barrier', nothing should get across.
13191ba76586SYinghai Lu  * A lot of state is already set up in PDA init for 64 bit
1320f7627e25SThomas Gleixner  */
13211ba76586SYinghai Lu #ifdef CONFIG_X86_64
13220f3fa48aSIngo Molnar 
1323148f9bb8SPaul Gortmaker void cpu_init(void)
13241ba76586SYinghai Lu {
13250fe1e009STejun Heo 	struct orig_ist *oist;
13261ba76586SYinghai Lu 	struct task_struct *me;
13270f3fa48aSIngo Molnar 	struct tss_struct *t;
13280f3fa48aSIngo Molnar 	unsigned long v;
1329ce4b1b16SIgor Mammedov 	int cpu = stack_smp_processor_id();
13301ba76586SYinghai Lu 	int i;
13311ba76586SYinghai Lu 
1332ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1333ce4b1b16SIgor Mammedov 
1334e6ebf5deSFenghua Yu 	/*
13351e02ce4cSAndy Lutomirski 	 * Initialize the CR4 shadow before doing anything that could
13361e02ce4cSAndy Lutomirski 	 * try to read it.
13371e02ce4cSAndy Lutomirski 	 */
13381e02ce4cSAndy Lutomirski 	cr4_init_shadow();
13391e02ce4cSAndy Lutomirski 
13401e02ce4cSAndy Lutomirski 	/*
1341e6ebf5deSFenghua Yu 	 * Load microcode on this cpu if a valid microcode is available.
1342e6ebf5deSFenghua Yu 	 * This is early microcode loading procedure.
1343e6ebf5deSFenghua Yu 	 */
1344e6ebf5deSFenghua Yu 	load_ucode_ap();
1345e6ebf5deSFenghua Yu 
134624933b82SAndy Lutomirski 	t = &per_cpu(cpu_tss, cpu);
13470fe1e009STejun Heo 	oist = &per_cpu(orig_ist, cpu);
13480f3fa48aSIngo Molnar 
1349e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA
135027fd185fSFenghua Yu 	if (this_cpu_read(numa_node) == 0 &&
1351e534c7c5SLee Schermerhorn 	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
1352e534c7c5SLee Schermerhorn 		set_numa_node(early_cpu_to_node(cpu));
1353e7a22c1eSBrian Gerst #endif
13541ba76586SYinghai Lu 
13551ba76586SYinghai Lu 	me = current;
13561ba76586SYinghai Lu 
13572eaad1fdSMike Travis 	pr_debug("Initializing CPU#%d\n", cpu);
13581ba76586SYinghai Lu 
1359375074ccSAndy Lutomirski 	cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
13601ba76586SYinghai Lu 
13611ba76586SYinghai Lu 	/*
13621ba76586SYinghai Lu 	 * Initialize the per-CPU GDT with the boot GDT,
13631ba76586SYinghai Lu 	 * and set up the GDT descriptor:
13641ba76586SYinghai Lu 	 */
13651ba76586SYinghai Lu 
1366552be871SBrian Gerst 	switch_to_new_gdt(cpu);
13672697fbd5SBrian Gerst 	loadsegment(fs, 0);
13682697fbd5SBrian Gerst 
1369cf910e83SSeiji Aguchi 	load_current_idt();
13701ba76586SYinghai Lu 
13711ba76586SYinghai Lu 	memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
13721ba76586SYinghai Lu 	syscall_init();
13731ba76586SYinghai Lu 
13741ba76586SYinghai Lu 	wrmsrl(MSR_FS_BASE, 0);
13751ba76586SYinghai Lu 	wrmsrl(MSR_KERNEL_GS_BASE, 0);
13761ba76586SYinghai Lu 	barrier();
13771ba76586SYinghai Lu 
13784763ed4dSH. Peter Anvin 	x86_configure_nx();
1379659006bfSThomas Gleixner 	x2apic_setup();
13801ba76586SYinghai Lu 
13811ba76586SYinghai Lu 	/*
13821ba76586SYinghai Lu 	 * set up and load the per-CPU TSS
13831ba76586SYinghai Lu 	 */
13840fe1e009STejun Heo 	if (!oist->ist[0]) {
138592d65b23SBrian Gerst 		char *estacks = per_cpu(exception_stacks, cpu);
13860f3fa48aSIngo Molnar 
13871ba76586SYinghai Lu 		for (v = 0; v < N_EXCEPTION_STACKS; v++) {
13880f3fa48aSIngo Molnar 			estacks += exception_stack_sizes[v];
13890fe1e009STejun Heo 			oist->ist[v] = t->x86_tss.ist[v] =
13901ba76586SYinghai Lu 					(unsigned long)estacks;
1391228bdaa9SSteven Rostedt 			if (v == DEBUG_STACK-1)
1392228bdaa9SSteven Rostedt 				per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
13931ba76586SYinghai Lu 		}
13941ba76586SYinghai Lu 	}
13951ba76586SYinghai Lu 
13961ba76586SYinghai Lu 	t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
13970f3fa48aSIngo Molnar 
13981ba76586SYinghai Lu 	/*
13991ba76586SYinghai Lu 	 * <= is required because the CPU will access up to
14001ba76586SYinghai Lu 	 * 8 bits beyond the end of the IO permission bitmap.
14011ba76586SYinghai Lu 	 */
14021ba76586SYinghai Lu 	for (i = 0; i <= IO_BITMAP_LONGS; i++)
14031ba76586SYinghai Lu 		t->io_bitmap[i] = ~0UL;
14041ba76586SYinghai Lu 
14051ba76586SYinghai Lu 	atomic_inc(&init_mm.mm_count);
14061ba76586SYinghai Lu 	me->active_mm = &init_mm;
14078c5dfd25SStoyan Gaydarov 	BUG_ON(me->mm);
14081ba76586SYinghai Lu 	enter_lazy_tlb(&init_mm, me);
14091ba76586SYinghai Lu 
14101ba76586SYinghai Lu 	load_sp0(t, &current->thread);
14111ba76586SYinghai Lu 	set_tss_desc(cpu, t);
14121ba76586SYinghai Lu 	load_TR_desc();
1413*37868fe1SAndy Lutomirski 	load_mm_ldt(&init_mm);
14141ba76586SYinghai Lu 
14159766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
14160bb9fef9SJason Wessel 	dbg_restore_debug_regs();
14171ba76586SYinghai Lu 
141821c4cd10SIngo Molnar 	fpu__init_cpu();
14191ba76586SYinghai Lu 
14201ba76586SYinghai Lu 	if (is_uv_system())
14211ba76586SYinghai Lu 		uv_cpu_init();
14221ba76586SYinghai Lu }
14231ba76586SYinghai Lu 
14241ba76586SYinghai Lu #else
14251ba76586SYinghai Lu 
1426148f9bb8SPaul Gortmaker void cpu_init(void)
1427f7627e25SThomas Gleixner {
1428f7627e25SThomas Gleixner 	int cpu = smp_processor_id();
1429f7627e25SThomas Gleixner 	struct task_struct *curr = current;
143024933b82SAndy Lutomirski 	struct tss_struct *t = &per_cpu(cpu_tss, cpu);
1431f7627e25SThomas Gleixner 	struct thread_struct *thread = &curr->thread;
1432f7627e25SThomas Gleixner 
1433ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1434e6ebf5deSFenghua Yu 
14355b2bdbc8SSteven Rostedt 	/*
14365b2bdbc8SSteven Rostedt 	 * Initialize the CR4 shadow before doing anything that could
14375b2bdbc8SSteven Rostedt 	 * try to read it.
14385b2bdbc8SSteven Rostedt 	 */
14395b2bdbc8SSteven Rostedt 	cr4_init_shadow();
14405b2bdbc8SSteven Rostedt 
1441ce4b1b16SIgor Mammedov 	show_ucode_info_early();
1442f7627e25SThomas Gleixner 
1443f7627e25SThomas Gleixner 	printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1444f7627e25SThomas Gleixner 
14459298b815SDave Hansen 	if (cpu_feature_enabled(X86_FEATURE_VME) || cpu_has_tsc || cpu_has_de)
1446375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1447f7627e25SThomas Gleixner 
1448cf910e83SSeiji Aguchi 	load_current_idt();
1449552be871SBrian Gerst 	switch_to_new_gdt(cpu);
1450f7627e25SThomas Gleixner 
1451f7627e25SThomas Gleixner 	/*
1452f7627e25SThomas Gleixner 	 * Set up and load the per-CPU TSS and LDT
1453f7627e25SThomas Gleixner 	 */
1454f7627e25SThomas Gleixner 	atomic_inc(&init_mm.mm_count);
1455f7627e25SThomas Gleixner 	curr->active_mm = &init_mm;
14568c5dfd25SStoyan Gaydarov 	BUG_ON(curr->mm);
1457f7627e25SThomas Gleixner 	enter_lazy_tlb(&init_mm, curr);
1458f7627e25SThomas Gleixner 
1459faca6227SH. Peter Anvin 	load_sp0(t, thread);
1460f7627e25SThomas Gleixner 	set_tss_desc(cpu, t);
1461f7627e25SThomas Gleixner 	load_TR_desc();
1462*37868fe1SAndy Lutomirski 	load_mm_ldt(&init_mm);
1463f7627e25SThomas Gleixner 
1464f9a196b8SThomas Gleixner 	t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1465f9a196b8SThomas Gleixner 
1466f7627e25SThomas Gleixner #ifdef CONFIG_DOUBLEFAULT
1467f7627e25SThomas Gleixner 	/* Set up doublefault TSS pointer in the GDT */
1468f7627e25SThomas Gleixner 	__set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1469f7627e25SThomas Gleixner #endif
1470f7627e25SThomas Gleixner 
14719766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
14720bb9fef9SJason Wessel 	dbg_restore_debug_regs();
1473f7627e25SThomas Gleixner 
147421c4cd10SIngo Molnar 	fpu__init_cpu();
1475f7627e25SThomas Gleixner }
14761ba76586SYinghai Lu #endif
14775700f743SBorislav Petkov 
14785700f743SBorislav Petkov #ifdef CONFIG_X86_DEBUG_STATIC_CPU_HAS
14795700f743SBorislav Petkov void warn_pre_alternatives(void)
14805700f743SBorislav Petkov {
14815700f743SBorislav Petkov 	WARN(1, "You're using static_cpu_has before alternatives have run!\n");
14825700f743SBorislav Petkov }
14835700f743SBorislav Petkov EXPORT_SYMBOL_GPL(warn_pre_alternatives);
14845700f743SBorislav Petkov #endif
14854a90a99cSBorislav Petkov 
14864a90a99cSBorislav Petkov inline bool __static_cpu_has_safe(u16 bit)
14874a90a99cSBorislav Petkov {
14884a90a99cSBorislav Petkov 	return boot_cpu_has(bit);
14894a90a99cSBorislav Petkov }
14904a90a99cSBorislav Petkov EXPORT_SYMBOL_GPL(__static_cpu_has_safe);
1491