xref: /linux/arch/x86/kernel/cpu/common.c (revision 487d1edb9a6005cf790c7fe59f25ad1e5cb5817b)
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>
89766cdbcSJaswinder Singh Rajput #include <linux/delay.h>
99766cdbcSJaswinder Singh Rajput #include <linux/sched.h>
109766cdbcSJaswinder Singh Rajput #include <linux/init.h>
110f46efebSMasami Hiramatsu #include <linux/kprobes.h>
129766cdbcSJaswinder Singh Rajput #include <linux/kgdb.h>
139766cdbcSJaswinder Singh Rajput #include <linux/smp.h>
149766cdbcSJaswinder Singh Rajput #include <linux/io.h>
159766cdbcSJaswinder Singh Rajput 
169766cdbcSJaswinder Singh Rajput #include <asm/stackprotector.h>
17cdd6c482SIngo Molnar #include <asm/perf_event.h>
18f7627e25SThomas Gleixner #include <asm/mmu_context.h>
1949d859d7SH. Peter Anvin #include <asm/archrandom.h>
209766cdbcSJaswinder Singh Rajput #include <asm/hypervisor.h>
219766cdbcSJaswinder Singh Rajput #include <asm/processor.h>
221e02ce4cSAndy Lutomirski #include <asm/tlbflush.h>
23f649e938SPaul Gortmaker #include <asm/debugreg.h>
249766cdbcSJaswinder Singh Rajput #include <asm/sections.h>
25f40c3300SAndy Lutomirski #include <asm/vsyscall.h>
268bdbd962SAlan Cox #include <linux/topology.h>
278bdbd962SAlan Cox #include <linux/cpumask.h>
289766cdbcSJaswinder Singh Rajput #include <asm/pgtable.h>
2960063497SArun Sharma #include <linux/atomic.h>
309766cdbcSJaswinder Singh Rajput #include <asm/proto.h>
319766cdbcSJaswinder Singh Rajput #include <asm/setup.h>
32f7627e25SThomas Gleixner #include <asm/apic.h>
339766cdbcSJaswinder Singh Rajput #include <asm/desc.h>
349766cdbcSJaswinder Singh Rajput #include <asm/i387.h>
351361b83aSLinus Torvalds #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 
1480c752a93SSuresh Siddha static int __init x86_xsave_setup(char *s)
1490c752a93SSuresh Siddha {
1502cd3949fSDave Hansen 	if (strlen(s))
1512cd3949fSDave Hansen 		return 0;
1520c752a93SSuresh Siddha 	setup_clear_cpu_cap(X86_FEATURE_XSAVE);
1536bad06b7SSuresh Siddha 	setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
154b6f42a4aSFenghua Yu 	setup_clear_cpu_cap(X86_FEATURE_XSAVES);
155c6fd893dSSuresh Siddha 	setup_clear_cpu_cap(X86_FEATURE_AVX);
156c6fd893dSSuresh Siddha 	setup_clear_cpu_cap(X86_FEATURE_AVX2);
1570c752a93SSuresh Siddha 	return 1;
1580c752a93SSuresh Siddha }
1590c752a93SSuresh Siddha __setup("noxsave", x86_xsave_setup);
1600c752a93SSuresh Siddha 
1616bad06b7SSuresh Siddha static int __init x86_xsaveopt_setup(char *s)
1626bad06b7SSuresh Siddha {
1636bad06b7SSuresh Siddha 	setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
1646bad06b7SSuresh Siddha 	return 1;
1656bad06b7SSuresh Siddha }
1666bad06b7SSuresh Siddha __setup("noxsaveopt", x86_xsaveopt_setup);
1676bad06b7SSuresh Siddha 
168b6f42a4aSFenghua Yu static int __init x86_xsaves_setup(char *s)
169b6f42a4aSFenghua Yu {
170b6f42a4aSFenghua Yu 	setup_clear_cpu_cap(X86_FEATURE_XSAVES);
171b6f42a4aSFenghua Yu 	return 1;
172b6f42a4aSFenghua Yu }
173b6f42a4aSFenghua Yu __setup("noxsaves", x86_xsaves_setup);
174b6f42a4aSFenghua Yu 
175ba51dcedSYinghai Lu #ifdef CONFIG_X86_32
176148f9bb8SPaul Gortmaker static int cachesize_override = -1;
177148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1;
178f7627e25SThomas Gleixner 
179f7627e25SThomas Gleixner static int __init cachesize_setup(char *str)
180f7627e25SThomas Gleixner {
181f7627e25SThomas Gleixner 	get_option(&str, &cachesize_override);
182f7627e25SThomas Gleixner 	return 1;
183f7627e25SThomas Gleixner }
184f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup);
185f7627e25SThomas Gleixner 
186f7627e25SThomas Gleixner static int __init x86_fxsr_setup(char *s)
187f7627e25SThomas Gleixner {
18813530257SAndi Kleen 	setup_clear_cpu_cap(X86_FEATURE_FXSR);
18913530257SAndi Kleen 	setup_clear_cpu_cap(X86_FEATURE_XMM);
190f7627e25SThomas Gleixner 	return 1;
191f7627e25SThomas Gleixner }
192f7627e25SThomas Gleixner __setup("nofxsr", x86_fxsr_setup);
193f7627e25SThomas Gleixner 
194f7627e25SThomas Gleixner static int __init x86_sep_setup(char *s)
195f7627e25SThomas Gleixner {
19613530257SAndi Kleen 	setup_clear_cpu_cap(X86_FEATURE_SEP);
197f7627e25SThomas Gleixner 	return 1;
198f7627e25SThomas Gleixner }
199f7627e25SThomas Gleixner __setup("nosep", x86_sep_setup);
200f7627e25SThomas Gleixner 
201f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */
202f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag)
203f7627e25SThomas Gleixner {
204f7627e25SThomas Gleixner 	u32 f1, f2;
205f7627e25SThomas Gleixner 
20694f6bac1SKrzysztof Helt 	/*
20794f6bac1SKrzysztof Helt 	 * Cyrix and IDT cpus allow disabling of CPUID
20894f6bac1SKrzysztof Helt 	 * so the code below may return different results
20994f6bac1SKrzysztof Helt 	 * when it is executed before and after enabling
21094f6bac1SKrzysztof Helt 	 * the CPUID. Add "volatile" to not allow gcc to
21194f6bac1SKrzysztof Helt 	 * optimize the subsequent calls to this function.
21294f6bac1SKrzysztof Helt 	 */
21394f6bac1SKrzysztof Helt 	asm volatile ("pushfl		\n\t"
214f7627e25SThomas Gleixner 		      "pushfl		\n\t"
215f7627e25SThomas Gleixner 		      "popl %0		\n\t"
216f7627e25SThomas Gleixner 		      "movl %0, %1	\n\t"
217f7627e25SThomas Gleixner 		      "xorl %2, %0	\n\t"
218f7627e25SThomas Gleixner 		      "pushl %0		\n\t"
219f7627e25SThomas Gleixner 		      "popfl		\n\t"
220f7627e25SThomas Gleixner 		      "pushfl		\n\t"
221f7627e25SThomas Gleixner 		      "popl %0		\n\t"
222f7627e25SThomas Gleixner 		      "popfl		\n\t"
2230f3fa48aSIngo Molnar 
224f7627e25SThomas Gleixner 		      : "=&r" (f1), "=&r" (f2)
225f7627e25SThomas Gleixner 		      : "ir" (flag));
226f7627e25SThomas Gleixner 
227f7627e25SThomas Gleixner 	return ((f1^f2) & flag) != 0;
228f7627e25SThomas Gleixner }
229f7627e25SThomas Gleixner 
230f7627e25SThomas Gleixner /* Probe for the CPUID instruction */
231148f9bb8SPaul Gortmaker int have_cpuid_p(void)
232f7627e25SThomas Gleixner {
233f7627e25SThomas Gleixner 	return flag_is_changeable_p(X86_EFLAGS_ID);
234f7627e25SThomas Gleixner }
235f7627e25SThomas Gleixner 
236148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
2370a488a53SYinghai Lu {
2380a488a53SYinghai Lu 	unsigned long lo, hi;
2390f3fa48aSIngo Molnar 
2400f3fa48aSIngo Molnar 	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
2410f3fa48aSIngo Molnar 		return;
2420f3fa48aSIngo Molnar 
2430f3fa48aSIngo Molnar 	/* Disable processor serial number: */
2440f3fa48aSIngo Molnar 
2450a488a53SYinghai Lu 	rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2460a488a53SYinghai Lu 	lo |= 0x200000;
2470a488a53SYinghai Lu 	wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2480f3fa48aSIngo Molnar 
2490a488a53SYinghai Lu 	printk(KERN_NOTICE "CPU serial number disabled.\n");
2500a488a53SYinghai Lu 	clear_cpu_cap(c, X86_FEATURE_PN);
2510a488a53SYinghai Lu 
2520a488a53SYinghai Lu 	/* Disabling the serial number may affect the cpuid level */
2530a488a53SYinghai Lu 	c->cpuid_level = cpuid_eax(0);
2540a488a53SYinghai Lu }
2550a488a53SYinghai Lu 
2560a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s)
2570a488a53SYinghai Lu {
2580a488a53SYinghai Lu 	disable_x86_serial_nr = 0;
2590a488a53SYinghai Lu 	return 1;
2600a488a53SYinghai Lu }
2610a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup);
262ba51dcedSYinghai Lu #else
263102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag)
264102bbe3aSYinghai Lu {
265102bbe3aSYinghai Lu 	return 1;
266102bbe3aSYinghai Lu }
267102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
268102bbe3aSYinghai Lu {
269102bbe3aSYinghai Lu }
270ba51dcedSYinghai Lu #endif
2710a488a53SYinghai Lu 
272de5397adSFenghua Yu static __init int setup_disable_smep(char *arg)
273de5397adSFenghua Yu {
274b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMEP);
275de5397adSFenghua Yu 	return 1;
276de5397adSFenghua Yu }
277de5397adSFenghua Yu __setup("nosmep", setup_disable_smep);
278de5397adSFenghua Yu 
279b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c)
280de5397adSFenghua Yu {
281b2cc2a07SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMEP))
282375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMEP);
283de5397adSFenghua Yu }
284de5397adSFenghua Yu 
28552b6179aSH. Peter Anvin static __init int setup_disable_smap(char *arg)
28652b6179aSH. Peter Anvin {
287b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMAP);
28852b6179aSH. Peter Anvin 	return 1;
28952b6179aSH. Peter Anvin }
29052b6179aSH. Peter Anvin __setup("nosmap", setup_disable_smap);
29152b6179aSH. Peter Anvin 
292b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c)
29352b6179aSH. Peter Anvin {
294b2cc2a07SH. Peter Anvin 	unsigned long eflags;
295b2cc2a07SH. Peter Anvin 
296b2cc2a07SH. Peter Anvin 	/* This should have been cleared long ago */
297b2cc2a07SH. Peter Anvin 	raw_local_save_flags(eflags);
298b2cc2a07SH. Peter Anvin 	BUG_ON(eflags & X86_EFLAGS_AC);
299b2cc2a07SH. Peter Anvin 
30003bbd596SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMAP)) {
30103bbd596SH. Peter Anvin #ifdef CONFIG_X86_SMAP
302375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMAP);
30303bbd596SH. Peter Anvin #else
304375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_SMAP);
30503bbd596SH. Peter Anvin #endif
30603bbd596SH. Peter Anvin 	}
307f7627e25SThomas Gleixner }
308f7627e25SThomas Gleixner 
309f7627e25SThomas Gleixner /*
310b38b0665SH. Peter Anvin  * Some CPU features depend on higher CPUID levels, which may not always
311b38b0665SH. Peter Anvin  * be available due to CPUID level capping or broken virtualization
312b38b0665SH. Peter Anvin  * software.  Add those features to this table to auto-disable them.
313b38b0665SH. Peter Anvin  */
314b38b0665SH. Peter Anvin struct cpuid_dependent_feature {
315b38b0665SH. Peter Anvin 	u32 feature;
316b38b0665SH. Peter Anvin 	u32 level;
317b38b0665SH. Peter Anvin };
3180f3fa48aSIngo Molnar 
319148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature
320b38b0665SH. Peter Anvin cpuid_dependent_features[] = {
321b38b0665SH. Peter Anvin 	{ X86_FEATURE_MWAIT,		0x00000005 },
322b38b0665SH. Peter Anvin 	{ X86_FEATURE_DCA,		0x00000009 },
323b38b0665SH. Peter Anvin 	{ X86_FEATURE_XSAVE,		0x0000000d },
324b38b0665SH. Peter Anvin 	{ 0, 0 }
325b38b0665SH. Peter Anvin };
326b38b0665SH. Peter Anvin 
327148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
328b38b0665SH. Peter Anvin {
329b38b0665SH. Peter Anvin 	const struct cpuid_dependent_feature *df;
3309766cdbcSJaswinder Singh Rajput 
331b38b0665SH. Peter Anvin 	for (df = cpuid_dependent_features; df->feature; df++) {
3320f3fa48aSIngo Molnar 
3330f3fa48aSIngo Molnar 		if (!cpu_has(c, df->feature))
3340f3fa48aSIngo Molnar 			continue;
335b38b0665SH. Peter Anvin 		/*
336b38b0665SH. Peter Anvin 		 * Note: cpuid_level is set to -1 if unavailable, but
337b38b0665SH. Peter Anvin 		 * extended_extended_level is set to 0 if unavailable
338b38b0665SH. Peter Anvin 		 * and the legitimate extended levels are all negative
339b38b0665SH. Peter Anvin 		 * when signed; hence the weird messing around with
340b38b0665SH. Peter Anvin 		 * signs here...
341b38b0665SH. Peter Anvin 		 */
3420f3fa48aSIngo Molnar 		if (!((s32)df->level < 0 ?
343f6db44dfSYinghai Lu 		     (u32)df->level > (u32)c->extended_cpuid_level :
3440f3fa48aSIngo Molnar 		     (s32)df->level > (s32)c->cpuid_level))
3450f3fa48aSIngo Molnar 			continue;
3460f3fa48aSIngo Molnar 
347b38b0665SH. Peter Anvin 		clear_cpu_cap(c, df->feature);
3480f3fa48aSIngo Molnar 		if (!warn)
3490f3fa48aSIngo Molnar 			continue;
3500f3fa48aSIngo Molnar 
351b38b0665SH. Peter Anvin 		printk(KERN_WARNING
3529def39beSJosh Triplett 		       "CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
3539def39beSJosh Triplett 				x86_cap_flag(df->feature), df->level);
354b38b0665SH. Peter Anvin 	}
355b38b0665SH. Peter Anvin }
356b38b0665SH. Peter Anvin 
357b38b0665SH. Peter Anvin /*
358f7627e25SThomas Gleixner  * Naming convention should be: <Name> [(<Codename>)]
359f7627e25SThomas Gleixner  * This table only is used unless init_<vendor>() below doesn't set it;
3600f3fa48aSIngo Molnar  * in particular, if CPUID levels 0x80000002..4 are supported, this
3610f3fa48aSIngo Molnar  * isn't used
362f7627e25SThomas Gleixner  */
363f7627e25SThomas Gleixner 
364f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */
365148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c)
366f7627e25SThomas Gleixner {
36709dc68d9SJan Beulich #ifdef CONFIG_X86_32
36809dc68d9SJan Beulich 	const struct legacy_cpu_model_info *info;
369f7627e25SThomas Gleixner 
370f7627e25SThomas Gleixner 	if (c->x86_model >= 16)
371f7627e25SThomas Gleixner 		return NULL;	/* Range check */
372f7627e25SThomas Gleixner 
373f7627e25SThomas Gleixner 	if (!this_cpu)
374f7627e25SThomas Gleixner 		return NULL;
375f7627e25SThomas Gleixner 
37609dc68d9SJan Beulich 	info = this_cpu->legacy_models;
377f7627e25SThomas Gleixner 
37809dc68d9SJan Beulich 	while (info->family) {
379f7627e25SThomas Gleixner 		if (info->family == c->x86)
380f7627e25SThomas Gleixner 			return info->model_names[c->x86_model];
381f7627e25SThomas Gleixner 		info++;
382f7627e25SThomas Gleixner 	}
38309dc68d9SJan Beulich #endif
384f7627e25SThomas Gleixner 	return NULL;		/* Not found */
385f7627e25SThomas Gleixner }
386f7627e25SThomas Gleixner 
387148f9bb8SPaul Gortmaker __u32 cpu_caps_cleared[NCAPINTS];
388148f9bb8SPaul Gortmaker __u32 cpu_caps_set[NCAPINTS];
389f7627e25SThomas Gleixner 
39011e3a840SJeremy Fitzhardinge void load_percpu_segment(int cpu)
3919d31d35bSYinghai Lu {
392fab334c1SYinghai Lu #ifdef CONFIG_X86_32
3932697fbd5SBrian Gerst 	loadsegment(fs, __KERNEL_PERCPU);
3942697fbd5SBrian Gerst #else
3952697fbd5SBrian Gerst 	loadsegment(gs, 0);
3962697fbd5SBrian Gerst 	wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
397fab334c1SYinghai Lu #endif
39860a5317fSTejun Heo 	load_stack_canary_segment();
3999d31d35bSYinghai Lu }
4009d31d35bSYinghai Lu 
4010f3fa48aSIngo Molnar /*
4020f3fa48aSIngo Molnar  * Current gdt points %fs at the "master" per-cpu area: after this,
4030f3fa48aSIngo Molnar  * it's on the real one.
4040f3fa48aSIngo Molnar  */
405552be871SBrian Gerst void switch_to_new_gdt(int cpu)
406f7627e25SThomas Gleixner {
407f7627e25SThomas Gleixner 	struct desc_ptr gdt_descr;
408f7627e25SThomas Gleixner 
409f7627e25SThomas Gleixner 	gdt_descr.address = (long)get_cpu_gdt_table(cpu);
410f7627e25SThomas Gleixner 	gdt_descr.size = GDT_SIZE - 1;
411f7627e25SThomas Gleixner 	load_gdt(&gdt_descr);
412f7627e25SThomas Gleixner 	/* Reload the per-cpu base */
41311e3a840SJeremy Fitzhardinge 
41411e3a840SJeremy Fitzhardinge 	load_percpu_segment(cpu);
415f7627e25SThomas Gleixner }
416f7627e25SThomas Gleixner 
417148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
418f7627e25SThomas Gleixner 
419148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c)
420f7627e25SThomas Gleixner {
421f7627e25SThomas Gleixner 	unsigned int *v;
422f7627e25SThomas Gleixner 	char *p, *q;
423f7627e25SThomas Gleixner 
4243da99c97SYinghai Lu 	if (c->extended_cpuid_level < 0x80000004)
4251b05d60dSYinghai Lu 		return;
426f7627e25SThomas Gleixner 
427f7627e25SThomas Gleixner 	v = (unsigned int *)c->x86_model_id;
428f7627e25SThomas Gleixner 	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
429f7627e25SThomas Gleixner 	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
430f7627e25SThomas Gleixner 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
431f7627e25SThomas Gleixner 	c->x86_model_id[48] = 0;
432f7627e25SThomas Gleixner 
4330f3fa48aSIngo Molnar 	/*
4340f3fa48aSIngo Molnar 	 * Intel chips right-justify this string for some dumb reason;
4350f3fa48aSIngo Molnar 	 * undo that brain damage:
4360f3fa48aSIngo Molnar 	 */
437f7627e25SThomas Gleixner 	p = q = &c->x86_model_id[0];
438f7627e25SThomas Gleixner 	while (*p == ' ')
439f7627e25SThomas Gleixner 		p++;
440f7627e25SThomas Gleixner 	if (p != q) {
441f7627e25SThomas Gleixner 		while (*p)
442f7627e25SThomas Gleixner 			*q++ = *p++;
443f7627e25SThomas Gleixner 		while (q <= &c->x86_model_id[48])
444f7627e25SThomas Gleixner 			*q++ = '\0';	/* Zero-pad the rest */
445f7627e25SThomas Gleixner 	}
446f7627e25SThomas Gleixner }
447f7627e25SThomas Gleixner 
448148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
449f7627e25SThomas Gleixner {
4509d31d35bSYinghai Lu 	unsigned int n, dummy, ebx, ecx, edx, l2size;
451f7627e25SThomas Gleixner 
4523da99c97SYinghai Lu 	n = c->extended_cpuid_level;
453f7627e25SThomas Gleixner 
454f7627e25SThomas Gleixner 	if (n >= 0x80000005) {
4559d31d35bSYinghai Lu 		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
456f7627e25SThomas Gleixner 		c->x86_cache_size = (ecx>>24) + (edx>>24);
457140fc727SYinghai Lu #ifdef CONFIG_X86_64
458140fc727SYinghai Lu 		/* On K8 L1 TLB is inclusive, so don't count it */
459140fc727SYinghai Lu 		c->x86_tlbsize = 0;
460140fc727SYinghai Lu #endif
461f7627e25SThomas Gleixner 	}
462f7627e25SThomas Gleixner 
463f7627e25SThomas Gleixner 	if (n < 0x80000006)	/* Some chips just has a large L1. */
464f7627e25SThomas Gleixner 		return;
465f7627e25SThomas Gleixner 
4660a488a53SYinghai Lu 	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
467f7627e25SThomas Gleixner 	l2size = ecx >> 16;
468f7627e25SThomas Gleixner 
469140fc727SYinghai Lu #ifdef CONFIG_X86_64
470140fc727SYinghai Lu 	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
471140fc727SYinghai Lu #else
472f7627e25SThomas Gleixner 	/* do processor-specific cache resizing */
47309dc68d9SJan Beulich 	if (this_cpu->legacy_cache_size)
47409dc68d9SJan Beulich 		l2size = this_cpu->legacy_cache_size(c, l2size);
475f7627e25SThomas Gleixner 
476f7627e25SThomas Gleixner 	/* Allow user to override all this if necessary. */
477f7627e25SThomas Gleixner 	if (cachesize_override != -1)
478f7627e25SThomas Gleixner 		l2size = cachesize_override;
479f7627e25SThomas Gleixner 
480f7627e25SThomas Gleixner 	if (l2size == 0)
481f7627e25SThomas Gleixner 		return;		/* Again, no L2 cache is possible */
482140fc727SYinghai Lu #endif
483f7627e25SThomas Gleixner 
484f7627e25SThomas Gleixner 	c->x86_cache_size = l2size;
485f7627e25SThomas Gleixner }
486f7627e25SThomas Gleixner 
487e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO];
488e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO];
489e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO];
490e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO];
491e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO];
492e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO];
493dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO];
494e0ba94f1SAlex Shi 
495f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c)
496e0ba94f1SAlex Shi {
497e0ba94f1SAlex Shi 	if (this_cpu->c_detect_tlb)
498e0ba94f1SAlex Shi 		this_cpu->c_detect_tlb(c);
499e0ba94f1SAlex Shi 
500f94fe119SSteven Honeyman 	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
501e0ba94f1SAlex Shi 		tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
502f94fe119SSteven Honeyman 		tlb_lli_4m[ENTRIES]);
503f94fe119SSteven Honeyman 
504f94fe119SSteven Honeyman 	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
505f94fe119SSteven Honeyman 		tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
506f94fe119SSteven Honeyman 		tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
507e0ba94f1SAlex Shi }
508e0ba94f1SAlex Shi 
509148f9bb8SPaul Gortmaker void detect_ht(struct cpuinfo_x86 *c)
5109d31d35bSYinghai Lu {
51197e4db7cSYinghai Lu #ifdef CONFIG_X86_HT
5129d31d35bSYinghai Lu 	u32 eax, ebx, ecx, edx;
5139d31d35bSYinghai Lu 	int index_msb, core_bits;
5142eaad1fdSMike Travis 	static bool printed;
5159d31d35bSYinghai Lu 
5160a488a53SYinghai Lu 	if (!cpu_has(c, X86_FEATURE_HT))
5179d31d35bSYinghai Lu 		return;
5189d31d35bSYinghai Lu 
5190a488a53SYinghai Lu 	if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
5200a488a53SYinghai Lu 		goto out;
5210a488a53SYinghai Lu 
5221cd78776SYinghai Lu 	if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
5231cd78776SYinghai Lu 		return;
5241cd78776SYinghai Lu 
5250a488a53SYinghai Lu 	cpuid(1, &eax, &ebx, &ecx, &edx);
5260a488a53SYinghai Lu 
5279d31d35bSYinghai Lu 	smp_num_siblings = (ebx & 0xff0000) >> 16;
5289d31d35bSYinghai Lu 
5299d31d35bSYinghai Lu 	if (smp_num_siblings == 1) {
5302eaad1fdSMike Travis 		printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
5310f3fa48aSIngo Molnar 		goto out;
5320f3fa48aSIngo Molnar 	}
5330f3fa48aSIngo Molnar 
5340f3fa48aSIngo Molnar 	if (smp_num_siblings <= 1)
5350f3fa48aSIngo Molnar 		goto out;
5369d31d35bSYinghai Lu 
5379d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
538cb8cc442SIngo Molnar 	c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
5399d31d35bSYinghai Lu 
5409d31d35bSYinghai Lu 	smp_num_siblings = smp_num_siblings / c->x86_max_cores;
5419d31d35bSYinghai Lu 
5429d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
5439d31d35bSYinghai Lu 
5449d31d35bSYinghai Lu 	core_bits = get_count_order(c->x86_max_cores);
5459d31d35bSYinghai Lu 
546cb8cc442SIngo Molnar 	c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
5471cd78776SYinghai Lu 				       ((1 << core_bits) - 1);
5489d31d35bSYinghai Lu 
5490a488a53SYinghai Lu out:
5502eaad1fdSMike Travis 	if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
5510a488a53SYinghai Lu 		printk(KERN_INFO  "CPU: Physical Processor ID: %d\n",
5520a488a53SYinghai Lu 		       c->phys_proc_id);
5539d31d35bSYinghai Lu 		printk(KERN_INFO  "CPU: Processor Core ID: %d\n",
5549d31d35bSYinghai Lu 		       c->cpu_core_id);
5552eaad1fdSMike Travis 		printed = 1;
5569d31d35bSYinghai Lu 	}
5579d31d35bSYinghai Lu #endif
55897e4db7cSYinghai Lu }
559f7627e25SThomas Gleixner 
560148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c)
561f7627e25SThomas Gleixner {
562f7627e25SThomas Gleixner 	char *v = c->x86_vendor_id;
5630f3fa48aSIngo Molnar 	int i;
564f7627e25SThomas Gleixner 
565f7627e25SThomas Gleixner 	for (i = 0; i < X86_VENDOR_NUM; i++) {
56610a434fcSYinghai Lu 		if (!cpu_devs[i])
56710a434fcSYinghai Lu 			break;
56810a434fcSYinghai Lu 
569f7627e25SThomas Gleixner 		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
570f7627e25SThomas Gleixner 		    (cpu_devs[i]->c_ident[1] &&
571f7627e25SThomas Gleixner 		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
5720f3fa48aSIngo Molnar 
573f7627e25SThomas Gleixner 			this_cpu = cpu_devs[i];
57410a434fcSYinghai Lu 			c->x86_vendor = this_cpu->c_x86_vendor;
575f7627e25SThomas Gleixner 			return;
576f7627e25SThomas Gleixner 		}
577f7627e25SThomas Gleixner 	}
57810a434fcSYinghai Lu 
579a9c56953SMinchan Kim 	printk_once(KERN_ERR
580a9c56953SMinchan Kim 			"CPU: vendor_id '%s' unknown, using generic init.\n" \
581a9c56953SMinchan Kim 			"CPU: Your system may be unstable.\n", v);
58210a434fcSYinghai Lu 
583f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
584f7627e25SThomas Gleixner 	this_cpu = &default_cpu;
585f7627e25SThomas Gleixner }
586f7627e25SThomas Gleixner 
587148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c)
588f7627e25SThomas Gleixner {
589f7627e25SThomas Gleixner 	/* Get vendor name */
5904a148513SHarvey Harrison 	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
5914a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[0],
5924a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[8],
5934a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[4]);
594f7627e25SThomas Gleixner 
595f7627e25SThomas Gleixner 	c->x86 = 4;
5969d31d35bSYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
597f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
598f7627e25SThomas Gleixner 		u32 junk, tfms, cap0, misc;
5990f3fa48aSIngo Molnar 
600f7627e25SThomas Gleixner 		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
6019d31d35bSYinghai Lu 		c->x86 = (tfms >> 8) & 0xf;
6029d31d35bSYinghai Lu 		c->x86_model = (tfms >> 4) & 0xf;
6039d31d35bSYinghai Lu 		c->x86_mask = tfms & 0xf;
6040f3fa48aSIngo Molnar 
605f7627e25SThomas Gleixner 		if (c->x86 == 0xf)
606f7627e25SThomas Gleixner 			c->x86 += (tfms >> 20) & 0xff;
607f7627e25SThomas Gleixner 		if (c->x86 >= 0x6)
6089d31d35bSYinghai Lu 			c->x86_model += ((tfms >> 16) & 0xf) << 4;
6090f3fa48aSIngo Molnar 
610d4387bd3SHuang, Ying 		if (cap0 & (1<<19)) {
611d4387bd3SHuang, Ying 			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
6129d31d35bSYinghai Lu 			c->x86_cache_alignment = c->x86_clflush_size;
613d4387bd3SHuang, Ying 		}
614f7627e25SThomas Gleixner 	}
615f7627e25SThomas Gleixner }
6163da99c97SYinghai Lu 
617148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c)
618093af8d7SYinghai Lu {
619093af8d7SYinghai Lu 	u32 tfms, xlvl;
6203da99c97SYinghai Lu 	u32 ebx;
621093af8d7SYinghai Lu 
622093af8d7SYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
623093af8d7SYinghai Lu 	if (c->cpuid_level >= 0x00000001) {
624093af8d7SYinghai Lu 		u32 capability, excap;
6250f3fa48aSIngo Molnar 
626093af8d7SYinghai Lu 		cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
627093af8d7SYinghai Lu 		c->x86_capability[0] = capability;
628093af8d7SYinghai Lu 		c->x86_capability[4] = excap;
629093af8d7SYinghai Lu 	}
630093af8d7SYinghai Lu 
631bdc802dcSH. Peter Anvin 	/* Additional Intel-defined flags: level 0x00000007 */
632bdc802dcSH. Peter Anvin 	if (c->cpuid_level >= 0x00000007) {
633bdc802dcSH. Peter Anvin 		u32 eax, ebx, ecx, edx;
634bdc802dcSH. Peter Anvin 
635bdc802dcSH. Peter Anvin 		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
636bdc802dcSH. Peter Anvin 
637bdc802dcSH. Peter Anvin 		c->x86_capability[9] = ebx;
638bdc802dcSH. Peter Anvin 	}
639bdc802dcSH. Peter Anvin 
6406229ad27SFenghua Yu 	/* Extended state features: level 0x0000000d */
6416229ad27SFenghua Yu 	if (c->cpuid_level >= 0x0000000d) {
6426229ad27SFenghua Yu 		u32 eax, ebx, ecx, edx;
6436229ad27SFenghua Yu 
6446229ad27SFenghua Yu 		cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
6456229ad27SFenghua Yu 
6466229ad27SFenghua Yu 		c->x86_capability[10] = eax;
6476229ad27SFenghua Yu 	}
6486229ad27SFenghua Yu 
649093af8d7SYinghai Lu 	/* AMD-defined flags: level 0x80000001 */
650093af8d7SYinghai Lu 	xlvl = cpuid_eax(0x80000000);
6513da99c97SYinghai Lu 	c->extended_cpuid_level = xlvl;
6520f3fa48aSIngo Molnar 
653093af8d7SYinghai Lu 	if ((xlvl & 0xffff0000) == 0x80000000) {
654093af8d7SYinghai Lu 		if (xlvl >= 0x80000001) {
655093af8d7SYinghai Lu 			c->x86_capability[1] = cpuid_edx(0x80000001);
656093af8d7SYinghai Lu 			c->x86_capability[6] = cpuid_ecx(0x80000001);
657093af8d7SYinghai Lu 		}
658093af8d7SYinghai Lu 	}
659093af8d7SYinghai Lu 
6605122c890SYinghai Lu 	if (c->extended_cpuid_level >= 0x80000008) {
6615122c890SYinghai Lu 		u32 eax = cpuid_eax(0x80000008);
6625122c890SYinghai Lu 
6635122c890SYinghai Lu 		c->x86_virt_bits = (eax >> 8) & 0xff;
6645122c890SYinghai Lu 		c->x86_phys_bits = eax & 0xff;
6655122c890SYinghai Lu 	}
66613c6c532SJan Beulich #ifdef CONFIG_X86_32
66713c6c532SJan Beulich 	else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
66813c6c532SJan Beulich 		c->x86_phys_bits = 36;
6695122c890SYinghai Lu #endif
670e3224234SYinghai Lu 
671e3224234SYinghai Lu 	if (c->extended_cpuid_level >= 0x80000007)
672e3224234SYinghai Lu 		c->x86_power = cpuid_edx(0x80000007);
673e3224234SYinghai Lu 
6741dedefd1SJacob Pan 	init_scattered_cpuid_features(c);
675093af8d7SYinghai Lu }
676093af8d7SYinghai Lu 
677148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
678aef93c8bSYinghai Lu {
679aef93c8bSYinghai Lu #ifdef CONFIG_X86_32
680aef93c8bSYinghai Lu 	int i;
681aef93c8bSYinghai Lu 
682aef93c8bSYinghai Lu 	/*
683aef93c8bSYinghai Lu 	 * First of all, decide if this is a 486 or higher
684aef93c8bSYinghai Lu 	 * It's a 486 if we can modify the AC flag
685aef93c8bSYinghai Lu 	 */
686aef93c8bSYinghai Lu 	if (flag_is_changeable_p(X86_EFLAGS_AC))
687aef93c8bSYinghai Lu 		c->x86 = 4;
688aef93c8bSYinghai Lu 	else
689aef93c8bSYinghai Lu 		c->x86 = 3;
690aef93c8bSYinghai Lu 
691aef93c8bSYinghai Lu 	for (i = 0; i < X86_VENDOR_NUM; i++)
692aef93c8bSYinghai Lu 		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
693aef93c8bSYinghai Lu 			c->x86_vendor_id[0] = 0;
694aef93c8bSYinghai Lu 			cpu_devs[i]->c_identify(c);
695aef93c8bSYinghai Lu 			if (c->x86_vendor_id[0]) {
696aef93c8bSYinghai Lu 				get_cpu_vendor(c);
697aef93c8bSYinghai Lu 				break;
698aef93c8bSYinghai Lu 			}
699aef93c8bSYinghai Lu 		}
700aef93c8bSYinghai Lu #endif
701093af8d7SYinghai Lu }
702f7627e25SThomas Gleixner 
70334048c9eSPaolo Ciarrocchi /*
70434048c9eSPaolo Ciarrocchi  * Do minimum CPU detection early.
70534048c9eSPaolo Ciarrocchi  * Fields really needed: vendor, cpuid_level, family, model, mask,
70634048c9eSPaolo Ciarrocchi  * cache alignment.
70734048c9eSPaolo Ciarrocchi  * The others are not touched to avoid unwanted side effects.
70834048c9eSPaolo Ciarrocchi  *
70934048c9eSPaolo Ciarrocchi  * WARNING: this function is only called on the BP.  Don't add code here
71034048c9eSPaolo Ciarrocchi  * that is supposed to run on all CPUs.
71134048c9eSPaolo Ciarrocchi  */
7123da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c)
713f7627e25SThomas Gleixner {
7146627d242SYinghai Lu #ifdef CONFIG_X86_64
7156627d242SYinghai Lu 	c->x86_clflush_size = 64;
71613c6c532SJan Beulich 	c->x86_phys_bits = 36;
71713c6c532SJan Beulich 	c->x86_virt_bits = 48;
7186627d242SYinghai Lu #else
719d4387bd3SHuang, Ying 	c->x86_clflush_size = 32;
72013c6c532SJan Beulich 	c->x86_phys_bits = 32;
72113c6c532SJan Beulich 	c->x86_virt_bits = 32;
7226627d242SYinghai Lu #endif
7230a488a53SYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
724f7627e25SThomas Gleixner 
7253da99c97SYinghai Lu 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
7260a488a53SYinghai Lu 	c->extended_cpuid_level = 0;
7270a488a53SYinghai Lu 
728aef93c8bSYinghai Lu 	if (!have_cpuid_p())
729aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
730aef93c8bSYinghai Lu 
731aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
732f7627e25SThomas Gleixner 	if (!have_cpuid_p())
733f7627e25SThomas Gleixner 		return;
734f7627e25SThomas Gleixner 
735f7627e25SThomas Gleixner 	cpu_detect(c);
7363da99c97SYinghai Lu 	get_cpu_vendor(c);
7373da99c97SYinghai Lu 	get_cpu_cap(c);
73860e019ebSH. Peter Anvin 	fpu_detect(c);
73912cf105cSKrzysztof Helt 
74010a434fcSYinghai Lu 	if (this_cpu->c_early_init)
74110a434fcSYinghai Lu 		this_cpu->c_early_init(c);
7423da99c97SYinghai Lu 
743f6e9456cSRobert Richter 	c->cpu_index = 0;
744b38b0665SH. Peter Anvin 	filter_cpuid_features(c, false);
745de5397adSFenghua Yu 
746a110b5ecSBorislav Petkov 	if (this_cpu->c_bsp_init)
747a110b5ecSBorislav Petkov 		this_cpu->c_bsp_init(c);
748c3b83598SBorislav Petkov 
749c3b83598SBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
750f7627e25SThomas Gleixner }
751f7627e25SThomas Gleixner 
7529d31d35bSYinghai Lu void __init early_cpu_init(void)
7539d31d35bSYinghai Lu {
75402dde8b4SJan Beulich 	const struct cpu_dev *const *cdev;
75510a434fcSYinghai Lu 	int count = 0;
7569d31d35bSYinghai Lu 
757ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
7589766cdbcSJaswinder Singh Rajput 	printk(KERN_INFO "KERNEL supported cpus:\n");
75931c997caSIngo Molnar #endif
76031c997caSIngo Molnar 
76110a434fcSYinghai Lu 	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
76202dde8b4SJan Beulich 		const struct cpu_dev *cpudev = *cdev;
7639d31d35bSYinghai Lu 
76410a434fcSYinghai Lu 		if (count >= X86_VENDOR_NUM)
76510a434fcSYinghai Lu 			break;
76610a434fcSYinghai Lu 		cpu_devs[count] = cpudev;
76710a434fcSYinghai Lu 		count++;
76810a434fcSYinghai Lu 
769ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
77031c997caSIngo Molnar 		{
77131c997caSIngo Molnar 			unsigned int j;
77231c997caSIngo Molnar 
77310a434fcSYinghai Lu 			for (j = 0; j < 2; j++) {
77410a434fcSYinghai Lu 				if (!cpudev->c_ident[j])
77510a434fcSYinghai Lu 					continue;
7769766cdbcSJaswinder Singh Rajput 				printk(KERN_INFO "  %s %s\n", cpudev->c_vendor,
77710a434fcSYinghai Lu 					cpudev->c_ident[j]);
77810a434fcSYinghai Lu 			}
77910a434fcSYinghai Lu 		}
7800388423dSDave Jones #endif
78131c997caSIngo Molnar 	}
7829d31d35bSYinghai Lu 	early_identify_cpu(&boot_cpu_data);
783f7627e25SThomas Gleixner }
784f7627e25SThomas Gleixner 
785b6734c35SH. Peter Anvin /*
786366d4a43SBorislav Petkov  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
787366d4a43SBorislav Petkov  * unfortunately, that's not true in practice because of early VIA
788366d4a43SBorislav Petkov  * chips and (more importantly) broken virtualizers that are not easy
789366d4a43SBorislav Petkov  * to detect. In the latter case it doesn't even *fail* reliably, so
790366d4a43SBorislav Petkov  * probing for it doesn't even work. Disable it completely on 32-bit
791ba0593bfSH. Peter Anvin  * unless we can find a reliable way to detect all the broken cases.
792366d4a43SBorislav Petkov  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
793b6734c35SH. Peter Anvin  */
794148f9bb8SPaul Gortmaker static void detect_nopl(struct cpuinfo_x86 *c)
795b6734c35SH. Peter Anvin {
796366d4a43SBorislav Petkov #ifdef CONFIG_X86_32
797b6734c35SH. Peter Anvin 	clear_cpu_cap(c, X86_FEATURE_NOPL);
798366d4a43SBorislav Petkov #else
799366d4a43SBorislav Petkov 	set_cpu_cap(c, X86_FEATURE_NOPL);
800366d4a43SBorislav Petkov #endif
801f7627e25SThomas Gleixner }
802f7627e25SThomas Gleixner 
803148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c)
804f7627e25SThomas Gleixner {
8053da99c97SYinghai Lu 	c->extended_cpuid_level = 0;
806f7627e25SThomas Gleixner 
807aef93c8bSYinghai Lu 	if (!have_cpuid_p())
808aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
809f7627e25SThomas Gleixner 
810aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
811a9853dd6SIngo Molnar 	if (!have_cpuid_p())
812aef93c8bSYinghai Lu 		return;
813aef93c8bSYinghai Lu 
8143da99c97SYinghai Lu 	cpu_detect(c);
8153da99c97SYinghai Lu 
8163da99c97SYinghai Lu 	get_cpu_vendor(c);
8173da99c97SYinghai Lu 
8183da99c97SYinghai Lu 	get_cpu_cap(c);
8193da99c97SYinghai Lu 
820f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
8213da99c97SYinghai Lu 		c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
822b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32
823f7627e25SThomas Gleixner # ifdef CONFIG_X86_HT
824cb8cc442SIngo Molnar 		c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
825f7627e25SThomas Gleixner # else
82601aaea1aSYinghai Lu 		c->apicid = c->initial_apicid;
827f7627e25SThomas Gleixner # endif
828b89d3b3eSYinghai Lu #endif
829b89d3b3eSYinghai Lu 		c->phys_proc_id = c->initial_apicid;
830f7627e25SThomas Gleixner 	}
831f7627e25SThomas Gleixner 
832f7627e25SThomas Gleixner 	get_model_name(c); /* Default name */
833f7627e25SThomas Gleixner 
834b6734c35SH. Peter Anvin 	detect_nopl(c);
835f7627e25SThomas Gleixner }
836f7627e25SThomas Gleixner 
837f7627e25SThomas Gleixner /*
838f7627e25SThomas Gleixner  * This does the hard work of actually picking apart the CPU stuff...
839f7627e25SThomas Gleixner  */
840148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c)
841f7627e25SThomas Gleixner {
842f7627e25SThomas Gleixner 	int i;
843f7627e25SThomas Gleixner 
844f7627e25SThomas Gleixner 	c->loops_per_jiffy = loops_per_jiffy;
845f7627e25SThomas Gleixner 	c->x86_cache_size = -1;
846f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
847f7627e25SThomas Gleixner 	c->x86_model = c->x86_mask = 0;	/* So far unknown... */
848f7627e25SThomas Gleixner 	c->x86_vendor_id[0] = '\0'; /* Unset */
849f7627e25SThomas Gleixner 	c->x86_model_id[0] = '\0';  /* Unset */
850f7627e25SThomas Gleixner 	c->x86_max_cores = 1;
851102bbe3aSYinghai Lu 	c->x86_coreid_bits = 0;
85211fdd252SYinghai Lu #ifdef CONFIG_X86_64
853102bbe3aSYinghai Lu 	c->x86_clflush_size = 64;
85413c6c532SJan Beulich 	c->x86_phys_bits = 36;
85513c6c532SJan Beulich 	c->x86_virt_bits = 48;
856102bbe3aSYinghai Lu #else
857102bbe3aSYinghai Lu 	c->cpuid_level = -1;	/* CPUID not detected */
858f7627e25SThomas Gleixner 	c->x86_clflush_size = 32;
85913c6c532SJan Beulich 	c->x86_phys_bits = 32;
86013c6c532SJan Beulich 	c->x86_virt_bits = 32;
861102bbe3aSYinghai Lu #endif
862102bbe3aSYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
863f7627e25SThomas Gleixner 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
864f7627e25SThomas Gleixner 
865f7627e25SThomas Gleixner 	generic_identify(c);
866f7627e25SThomas Gleixner 
8673898534dSAndi Kleen 	if (this_cpu->c_identify)
868f7627e25SThomas Gleixner 		this_cpu->c_identify(c);
869f7627e25SThomas Gleixner 
8702759c328SYinghai Lu 	/* Clear/Set all flags overriden by options, after probe */
8712759c328SYinghai Lu 	for (i = 0; i < NCAPINTS; i++) {
8722759c328SYinghai Lu 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
8732759c328SYinghai Lu 		c->x86_capability[i] |= cpu_caps_set[i];
8742759c328SYinghai Lu 	}
8752759c328SYinghai Lu 
876102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
877cb8cc442SIngo Molnar 	c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
878102bbe3aSYinghai Lu #endif
879102bbe3aSYinghai Lu 
880f7627e25SThomas Gleixner 	/*
881f7627e25SThomas Gleixner 	 * Vendor-specific initialization.  In this section we
882f7627e25SThomas Gleixner 	 * canonicalize the feature flags, meaning if there are
883f7627e25SThomas Gleixner 	 * features a certain CPU supports which CPUID doesn't
884f7627e25SThomas Gleixner 	 * tell us, CPUID claiming incorrect flags, or other bugs,
885f7627e25SThomas Gleixner 	 * we handle them here.
886f7627e25SThomas Gleixner 	 *
887f7627e25SThomas Gleixner 	 * At the end of this section, c->x86_capability better
888f7627e25SThomas Gleixner 	 * indicate the features this CPU genuinely supports!
889f7627e25SThomas Gleixner 	 */
890f7627e25SThomas Gleixner 	if (this_cpu->c_init)
891f7627e25SThomas Gleixner 		this_cpu->c_init(c);
892f7627e25SThomas Gleixner 
893f7627e25SThomas Gleixner 	/* Disable the PN if appropriate */
894f7627e25SThomas Gleixner 	squash_the_stupid_serial_number(c);
895f7627e25SThomas Gleixner 
896b2cc2a07SH. Peter Anvin 	/* Set up SMEP/SMAP */
897b2cc2a07SH. Peter Anvin 	setup_smep(c);
898b2cc2a07SH. Peter Anvin 	setup_smap(c);
899b2cc2a07SH. Peter Anvin 
900f7627e25SThomas Gleixner 	/*
9010f3fa48aSIngo Molnar 	 * The vendor-specific functions might have changed features.
9020f3fa48aSIngo Molnar 	 * Now we do "generic changes."
903f7627e25SThomas Gleixner 	 */
904f7627e25SThomas Gleixner 
905b38b0665SH. Peter Anvin 	/* Filter out anything that depends on CPUID levels we don't have */
906b38b0665SH. Peter Anvin 	filter_cpuid_features(c, true);
907b38b0665SH. Peter Anvin 
908f7627e25SThomas Gleixner 	/* If the model name is still unset, do table lookup. */
909f7627e25SThomas Gleixner 	if (!c->x86_model_id[0]) {
91002dde8b4SJan Beulich 		const char *p;
911f7627e25SThomas Gleixner 		p = table_lookup_model(c);
912f7627e25SThomas Gleixner 		if (p)
913f7627e25SThomas Gleixner 			strcpy(c->x86_model_id, p);
914f7627e25SThomas Gleixner 		else
915f7627e25SThomas Gleixner 			/* Last resort... */
916f7627e25SThomas Gleixner 			sprintf(c->x86_model_id, "%02x/%02x",
917f7627e25SThomas Gleixner 				c->x86, c->x86_model);
918f7627e25SThomas Gleixner 	}
919f7627e25SThomas Gleixner 
920102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
921102bbe3aSYinghai Lu 	detect_ht(c);
922102bbe3aSYinghai Lu #endif
923102bbe3aSYinghai Lu 
92488b094fbSAlok Kataria 	init_hypervisor(c);
92549d859d7SH. Peter Anvin 	x86_init_rdrand(c);
9263e0c3737SYinghai Lu 
9273e0c3737SYinghai Lu 	/*
9283e0c3737SYinghai Lu 	 * Clear/Set all flags overriden by options, need do it
9293e0c3737SYinghai Lu 	 * before following smp all cpus cap AND.
9303e0c3737SYinghai Lu 	 */
9313e0c3737SYinghai Lu 	for (i = 0; i < NCAPINTS; i++) {
9323e0c3737SYinghai Lu 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
9333e0c3737SYinghai Lu 		c->x86_capability[i] |= cpu_caps_set[i];
9343e0c3737SYinghai Lu 	}
9353e0c3737SYinghai Lu 
936f7627e25SThomas Gleixner 	/*
937f7627e25SThomas Gleixner 	 * On SMP, boot_cpu_data holds the common feature set between
938f7627e25SThomas Gleixner 	 * all CPUs; so make sure that we indicate which features are
939f7627e25SThomas Gleixner 	 * common between the CPUs.  The first time this routine gets
940f7627e25SThomas Gleixner 	 * executed, c == &boot_cpu_data.
941f7627e25SThomas Gleixner 	 */
942f7627e25SThomas Gleixner 	if (c != &boot_cpu_data) {
943f7627e25SThomas Gleixner 		/* AND the already accumulated flags with these */
944f7627e25SThomas Gleixner 		for (i = 0; i < NCAPINTS; i++)
945f7627e25SThomas Gleixner 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
94665fc985bSBorislav Petkov 
94765fc985bSBorislav Petkov 		/* OR, i.e. replicate the bug flags */
94865fc985bSBorislav Petkov 		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
94965fc985bSBorislav Petkov 			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
950f7627e25SThomas Gleixner 	}
951f7627e25SThomas Gleixner 
952f7627e25SThomas Gleixner 	/* Init Machine Check Exception if available. */
9535e09954aSBorislav Petkov 	mcheck_cpu_init(c);
95430d432dfSAndi Kleen 
95530d432dfSAndi Kleen 	select_idle_routine(c);
956102bbe3aSYinghai Lu 
957de2d9445STejun Heo #ifdef CONFIG_NUMA
958102bbe3aSYinghai Lu 	numa_add_cpu(smp_processor_id());
959102bbe3aSYinghai Lu #endif
960f7627e25SThomas Gleixner }
961f7627e25SThomas Gleixner 
9628b6c0ab1SIngo Molnar /*
9638b6c0ab1SIngo Molnar  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
9648b6c0ab1SIngo Molnar  * on 32-bit kernels:
9658b6c0ab1SIngo Molnar  */
966cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32
967cfda7bb9SAndy Lutomirski void enable_sep_cpu(void)
968cfda7bb9SAndy Lutomirski {
9698b6c0ab1SIngo Molnar 	struct tss_struct *tss;
9708b6c0ab1SIngo Molnar 	int cpu;
971cfda7bb9SAndy Lutomirski 
9728b6c0ab1SIngo Molnar 	cpu = get_cpu();
9738b6c0ab1SIngo Molnar 	tss = &per_cpu(cpu_tss, cpu);
9748b6c0ab1SIngo Molnar 
9758b6c0ab1SIngo Molnar 	if (!boot_cpu_has(X86_FEATURE_SEP))
9768b6c0ab1SIngo Molnar 		goto out;
9778b6c0ab1SIngo Molnar 
9788b6c0ab1SIngo Molnar 	/*
9798b6c0ab1SIngo Molnar 	 * The struct::SS1 and tss_struct::SP1 fields are not used by the hardware,
9808b6c0ab1SIngo Molnar 	 * we cache the SYSENTER CS and ESP values there for easy access:
9818b6c0ab1SIngo Molnar 	 */
982cfda7bb9SAndy Lutomirski 
983cfda7bb9SAndy Lutomirski 	tss->x86_tss.ss1 = __KERNEL_CS;
9848b6c0ab1SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
9858b6c0ab1SIngo Molnar 
986d828c71fSDenys Vlasenko 	tss->x86_tss.sp1 = (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack);
987cfda7bb9SAndy Lutomirski 	wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.sp1, 0);
9888b6c0ab1SIngo Molnar 
989cfda7bb9SAndy Lutomirski 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)ia32_sysenter_target, 0);
9908b6c0ab1SIngo Molnar 
9918b6c0ab1SIngo Molnar out:
992cfda7bb9SAndy Lutomirski 	put_cpu();
993cfda7bb9SAndy Lutomirski }
994e04d645fSGlauber Costa #endif
995e04d645fSGlauber Costa 
996f7627e25SThomas Gleixner void __init identify_boot_cpu(void)
997f7627e25SThomas Gleixner {
998f7627e25SThomas Gleixner 	identify_cpu(&boot_cpu_data);
99902c68a02SLen Brown 	init_amd_e400_c1e_mask();
1000102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1001f7627e25SThomas Gleixner 	sysenter_setup();
1002f7627e25SThomas Gleixner 	enable_sep_cpu();
1003102bbe3aSYinghai Lu #endif
1004e0ba94f1SAlex Shi 	cpu_detect_tlb(&boot_cpu_data);
1005f7627e25SThomas Gleixner }
1006f7627e25SThomas Gleixner 
1007148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c)
1008f7627e25SThomas Gleixner {
1009f7627e25SThomas Gleixner 	BUG_ON(c == &boot_cpu_data);
1010f7627e25SThomas Gleixner 	identify_cpu(c);
1011102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1012f7627e25SThomas Gleixner 	enable_sep_cpu();
1013102bbe3aSYinghai Lu #endif
1014f7627e25SThomas Gleixner 	mtrr_ap_init();
1015f7627e25SThomas Gleixner }
1016f7627e25SThomas Gleixner 
1017a0854a46SYinghai Lu struct msr_range {
1018a0854a46SYinghai Lu 	unsigned	min;
1019a0854a46SYinghai Lu 	unsigned	max;
1020a0854a46SYinghai Lu };
1021a0854a46SYinghai Lu 
1022148f9bb8SPaul Gortmaker static const struct msr_range msr_range_array[] = {
1023a0854a46SYinghai Lu 	{ 0x00000000, 0x00000418},
1024a0854a46SYinghai Lu 	{ 0xc0000000, 0xc000040b},
1025a0854a46SYinghai Lu 	{ 0xc0010000, 0xc0010142},
1026a0854a46SYinghai Lu 	{ 0xc0011000, 0xc001103b},
1027a0854a46SYinghai Lu };
1028a0854a46SYinghai Lu 
1029148f9bb8SPaul Gortmaker static void __print_cpu_msr(void)
1030f7627e25SThomas Gleixner {
10310f3fa48aSIngo Molnar 	unsigned index_min, index_max;
1032a0854a46SYinghai Lu 	unsigned index;
1033a0854a46SYinghai Lu 	u64 val;
1034a0854a46SYinghai Lu 	int i;
1035f7627e25SThomas Gleixner 
1036a0854a46SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
1037a0854a46SYinghai Lu 		index_min = msr_range_array[i].min;
1038a0854a46SYinghai Lu 		index_max = msr_range_array[i].max;
10390f3fa48aSIngo Molnar 
1040a0854a46SYinghai Lu 		for (index = index_min; index < index_max; index++) {
1041ecd431d9SBorislav Petkov 			if (rdmsrl_safe(index, &val))
1042a0854a46SYinghai Lu 				continue;
1043a0854a46SYinghai Lu 			printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
1044f7627e25SThomas Gleixner 		}
1045f7627e25SThomas Gleixner 	}
1046a0854a46SYinghai Lu }
1047a0854a46SYinghai Lu 
1048148f9bb8SPaul Gortmaker static int show_msr;
10490f3fa48aSIngo Molnar 
1050a0854a46SYinghai Lu static __init int setup_show_msr(char *arg)
1051a0854a46SYinghai Lu {
1052a0854a46SYinghai Lu 	int num;
1053a0854a46SYinghai Lu 
1054a0854a46SYinghai Lu 	get_option(&arg, &num);
1055a0854a46SYinghai Lu 
1056a0854a46SYinghai Lu 	if (num > 0)
1057a0854a46SYinghai Lu 		show_msr = num;
1058a0854a46SYinghai Lu 	return 1;
1059a0854a46SYinghai Lu }
1060a0854a46SYinghai Lu __setup("show_msr=", setup_show_msr);
1061f7627e25SThomas Gleixner 
1062191679fdSAndi Kleen static __init int setup_noclflush(char *arg)
1063191679fdSAndi Kleen {
1064840d2830SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1065da4aaa7dSH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1066191679fdSAndi Kleen 	return 1;
1067191679fdSAndi Kleen }
1068191679fdSAndi Kleen __setup("noclflush", setup_noclflush);
1069191679fdSAndi Kleen 
1070148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c)
1071f7627e25SThomas Gleixner {
107202dde8b4SJan Beulich 	const char *vendor = NULL;
1073f7627e25SThomas Gleixner 
10740f3fa48aSIngo Molnar 	if (c->x86_vendor < X86_VENDOR_NUM) {
1075f7627e25SThomas Gleixner 		vendor = this_cpu->c_vendor;
10760f3fa48aSIngo Molnar 	} else {
10770f3fa48aSIngo Molnar 		if (c->cpuid_level >= 0)
1078f7627e25SThomas Gleixner 			vendor = c->x86_vendor_id;
10790f3fa48aSIngo Molnar 	}
1080f7627e25SThomas Gleixner 
1081bd32a8cfSYinghai Lu 	if (vendor && !strstr(c->x86_model_id, vendor))
10829d31d35bSYinghai Lu 		printk(KERN_CONT "%s ", vendor);
1083f7627e25SThomas Gleixner 
10849d31d35bSYinghai Lu 	if (c->x86_model_id[0])
1085924e101aSBorislav Petkov 		printk(KERN_CONT "%s", strim(c->x86_model_id));
1086f7627e25SThomas Gleixner 	else
10879d31d35bSYinghai Lu 		printk(KERN_CONT "%d86", c->x86);
1088f7627e25SThomas Gleixner 
1089924e101aSBorislav Petkov 	printk(KERN_CONT " (fam: %02x, model: %02x", c->x86, c->x86_model);
1090924e101aSBorislav Petkov 
1091f7627e25SThomas Gleixner 	if (c->x86_mask || c->cpuid_level >= 0)
1092924e101aSBorislav Petkov 		printk(KERN_CONT ", stepping: %02x)\n", c->x86_mask);
1093f7627e25SThomas Gleixner 	else
1094924e101aSBorislav Petkov 		printk(KERN_CONT ")\n");
1095a0854a46SYinghai Lu 
10960b8b8078SYinghai Lu 	print_cpu_msr(c);
109721c3fcf3SYinghai Lu }
109821c3fcf3SYinghai Lu 
1099148f9bb8SPaul Gortmaker void print_cpu_msr(struct cpuinfo_x86 *c)
110021c3fcf3SYinghai Lu {
1101a0854a46SYinghai Lu 	if (c->cpu_index < show_msr)
110221c3fcf3SYinghai Lu 		__print_cpu_msr();
1103f7627e25SThomas Gleixner }
1104f7627e25SThomas Gleixner 
1105ac72e788SAndi Kleen static __init int setup_disablecpuid(char *arg)
1106ac72e788SAndi Kleen {
1107ac72e788SAndi Kleen 	int bit;
11080f3fa48aSIngo Molnar 
1109ac72e788SAndi Kleen 	if (get_option(&arg, &bit) && bit < NCAPINTS*32)
1110ac72e788SAndi Kleen 		setup_clear_cpu_cap(bit);
1111ac72e788SAndi Kleen 	else
1112ac72e788SAndi Kleen 		return 0;
11130f3fa48aSIngo Molnar 
1114ac72e788SAndi Kleen 	return 1;
1115ac72e788SAndi Kleen }
1116ac72e788SAndi Kleen __setup("clearcpuid=", setup_disablecpuid);
1117ac72e788SAndi Kleen 
1118198d208dSSteven Rostedt DEFINE_PER_CPU(unsigned long, kernel_stack) =
1119ef593260SDenys Vlasenko 	(unsigned long)&init_thread_union + THREAD_SIZE;
1120198d208dSSteven Rostedt EXPORT_PER_CPU_SYMBOL(kernel_stack);
1121198d208dSSteven Rostedt 
1122d5494d4fSYinghai Lu #ifdef CONFIG_X86_64
11239ff80942SCyrill Gorcunov struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
1124629f4f9dSSeiji Aguchi struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
1125629f4f9dSSeiji Aguchi 				    (unsigned long) debug_idt_table };
1126d5494d4fSYinghai Lu 
1127947e76cdSBrian Gerst DEFINE_PER_CPU_FIRST(union irq_stack_union,
1128277d5b40SAndi Kleen 		     irq_stack_union) __aligned(PAGE_SIZE) __visible;
11290f3fa48aSIngo Molnar 
1130bdf977b3STejun Heo /*
1131a7fcf28dSAndy Lutomirski  * The following percpu variables are hot.  Align current_task to
1132a7fcf28dSAndy Lutomirski  * cacheline size such that they fall in the same cacheline.
1133bdf977b3STejun Heo  */
1134bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1135bdf977b3STejun Heo 	&init_task;
1136bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1137d5494d4fSYinghai Lu 
1138bdf977b3STejun Heo DEFINE_PER_CPU(char *, irq_stack_ptr) =
1139bdf977b3STejun Heo 	init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
1140bdf977b3STejun Heo 
1141277d5b40SAndi Kleen DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
1142d5494d4fSYinghai Lu 
1143c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1144c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1145c2daa3beSPeter Zijlstra 
11467e16838dSLinus Torvalds DEFINE_PER_CPU(struct task_struct *, fpu_owner_task);
11477e16838dSLinus Torvalds 
11480f3fa48aSIngo Molnar /*
11490f3fa48aSIngo Molnar  * Special IST stacks which the CPU switches to when it calls
11500f3fa48aSIngo Molnar  * an IST-marked descriptor entry. Up to 7 stacks (hardware
11510f3fa48aSIngo Molnar  * limit), all of them are 4K, except the debug stack which
11520f3fa48aSIngo Molnar  * is 8K.
11530f3fa48aSIngo Molnar  */
11540f3fa48aSIngo Molnar static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
11550f3fa48aSIngo Molnar 	  [0 ... N_EXCEPTION_STACKS - 1]	= EXCEPTION_STKSZ,
11560f3fa48aSIngo Molnar 	  [DEBUG_STACK - 1]			= DEBUG_STKSZ
11570f3fa48aSIngo Molnar };
11580f3fa48aSIngo Molnar 
115992d65b23SBrian Gerst static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
11603e352aa8STejun Heo 	[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
1161d5494d4fSYinghai Lu 
1162d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */
1163d5494d4fSYinghai Lu void syscall_init(void)
1164d5494d4fSYinghai Lu {
1165d5494d4fSYinghai Lu 	/*
1166d5494d4fSYinghai Lu 	 * LSTAR and STAR live in a bit strange symbiosis.
1167d5494d4fSYinghai Lu 	 * They both write to the same internal register. STAR allows to
1168d5494d4fSYinghai Lu 	 * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
1169d5494d4fSYinghai Lu 	 */
1170d5494d4fSYinghai Lu 	wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32);
1171d5494d4fSYinghai Lu 	wrmsrl(MSR_LSTAR, system_call);
1172d56fe4bfSIngo Molnar 
1173d56fe4bfSIngo Molnar #ifdef CONFIG_IA32_EMULATION
1174a76c7f46SDenys Vlasenko 	wrmsrl(MSR_CSTAR, ia32_cstar_target);
1175a76c7f46SDenys Vlasenko 	/*
1176*487d1edbSDenys Vlasenko 	 * This only works on Intel CPUs.
1177*487d1edbSDenys Vlasenko 	 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1178*487d1edbSDenys Vlasenko 	 * This does not cause SYSENTER to jump to the wrong location, because
1179*487d1edbSDenys Vlasenko 	 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1180a76c7f46SDenys Vlasenko 	 */
1181a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1182a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1183a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
1184d56fe4bfSIngo Molnar #else
1185d56fe4bfSIngo Molnar 	wrmsrl(MSR_CSTAR, ignore_sysret);
1186d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, 0);
1187d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1188d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1189d5494d4fSYinghai Lu #endif
1190d5494d4fSYinghai Lu 
1191d5494d4fSYinghai Lu 	/* Flags to clear on syscall */
1192d5494d4fSYinghai Lu 	wrmsrl(MSR_SYSCALL_MASK,
119363bcff2aSH. Peter Anvin 	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
11948c7aa698SAndy Lutomirski 	       X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1195d5494d4fSYinghai Lu }
1196d5494d4fSYinghai Lu 
1197d5494d4fSYinghai Lu /*
1198d5494d4fSYinghai Lu  * Copies of the original ist values from the tss are only accessed during
1199d5494d4fSYinghai Lu  * debugging, no special alignment required.
1200d5494d4fSYinghai Lu  */
1201d5494d4fSYinghai Lu DEFINE_PER_CPU(struct orig_ist, orig_ist);
1202d5494d4fSYinghai Lu 
1203228bdaa9SSteven Rostedt static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
120442181186SSteven Rostedt DEFINE_PER_CPU(int, debug_stack_usage);
1205228bdaa9SSteven Rostedt 
1206228bdaa9SSteven Rostedt int is_debug_stack(unsigned long addr)
1207228bdaa9SSteven Rostedt {
120889cbc767SChristoph Lameter 	return __this_cpu_read(debug_stack_usage) ||
120989cbc767SChristoph Lameter 		(addr <= __this_cpu_read(debug_stack_addr) &&
121089cbc767SChristoph Lameter 		 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
1211228bdaa9SSteven Rostedt }
12120f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(is_debug_stack);
1213228bdaa9SSteven Rostedt 
1214629f4f9dSSeiji Aguchi DEFINE_PER_CPU(u32, debug_idt_ctr);
1215f8988175SSteven Rostedt 
1216228bdaa9SSteven Rostedt void debug_stack_set_zero(void)
1217228bdaa9SSteven Rostedt {
1218629f4f9dSSeiji Aguchi 	this_cpu_inc(debug_idt_ctr);
1219629f4f9dSSeiji Aguchi 	load_current_idt();
1220228bdaa9SSteven Rostedt }
12210f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_set_zero);
1222228bdaa9SSteven Rostedt 
1223228bdaa9SSteven Rostedt void debug_stack_reset(void)
1224228bdaa9SSteven Rostedt {
1225629f4f9dSSeiji Aguchi 	if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
1226f8988175SSteven Rostedt 		return;
1227629f4f9dSSeiji Aguchi 	if (this_cpu_dec_return(debug_idt_ctr) == 0)
1228629f4f9dSSeiji Aguchi 		load_current_idt();
1229228bdaa9SSteven Rostedt }
12300f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_reset);
1231228bdaa9SSteven Rostedt 
12320f3fa48aSIngo Molnar #else	/* CONFIG_X86_64 */
1233d5494d4fSYinghai Lu 
1234bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1235bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1236c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1237c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
123827e74da9SLinus Torvalds DEFINE_PER_CPU(struct task_struct *, fpu_owner_task);
1239bdf977b3STejun Heo 
1240a7fcf28dSAndy Lutomirski /*
1241a7fcf28dSAndy Lutomirski  * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1242a7fcf28dSAndy Lutomirski  * the top of the kernel stack.  Use an extra percpu variable to track the
1243a7fcf28dSAndy Lutomirski  * top of the kernel stack directly.
1244a7fcf28dSAndy Lutomirski  */
1245a7fcf28dSAndy Lutomirski DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1246a7fcf28dSAndy Lutomirski 	(unsigned long)&init_thread_union + THREAD_SIZE;
1247a7fcf28dSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1248a7fcf28dSAndy Lutomirski 
124960a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR
125053f82452SJeremy Fitzhardinge DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
125160a5317fSTejun Heo #endif
125260a5317fSTejun Heo 
12530f3fa48aSIngo Molnar #endif	/* CONFIG_X86_64 */
1254f7627e25SThomas Gleixner 
1255f7627e25SThomas Gleixner /*
12569766cdbcSJaswinder Singh Rajput  * Clear all 6 debug registers:
12579766cdbcSJaswinder Singh Rajput  */
12589766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void)
12599766cdbcSJaswinder Singh Rajput {
12609766cdbcSJaswinder Singh Rajput 	int i;
12619766cdbcSJaswinder Singh Rajput 
12629766cdbcSJaswinder Singh Rajput 	for (i = 0; i < 8; i++) {
12639766cdbcSJaswinder Singh Rajput 		/* Ignore db4, db5 */
12649766cdbcSJaswinder Singh Rajput 		if ((i == 4) || (i == 5))
12659766cdbcSJaswinder Singh Rajput 			continue;
12669766cdbcSJaswinder Singh Rajput 
12679766cdbcSJaswinder Singh Rajput 		set_debugreg(0, i);
12689766cdbcSJaswinder Singh Rajput 	}
12699766cdbcSJaswinder Singh Rajput }
1270f7627e25SThomas Gleixner 
12710bb9fef9SJason Wessel #ifdef CONFIG_KGDB
12720bb9fef9SJason Wessel /*
12730bb9fef9SJason Wessel  * Restore debug regs if using kgdbwait and you have a kernel debugger
12740bb9fef9SJason Wessel  * connection established.
12750bb9fef9SJason Wessel  */
12760bb9fef9SJason Wessel static void dbg_restore_debug_regs(void)
12770bb9fef9SJason Wessel {
12780bb9fef9SJason Wessel 	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
12790bb9fef9SJason Wessel 		arch_kgdb_ops.correct_hw_break();
12800bb9fef9SJason Wessel }
12810bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */
12820bb9fef9SJason Wessel #define dbg_restore_debug_regs()
12830bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */
12840bb9fef9SJason Wessel 
1285ce4b1b16SIgor Mammedov static void wait_for_master_cpu(int cpu)
1286ce4b1b16SIgor Mammedov {
1287ce4b1b16SIgor Mammedov #ifdef CONFIG_SMP
1288ce4b1b16SIgor Mammedov 	/*
1289ce4b1b16SIgor Mammedov 	 * wait for ACK from master CPU before continuing
1290ce4b1b16SIgor Mammedov 	 * with AP initialization
1291ce4b1b16SIgor Mammedov 	 */
1292ce4b1b16SIgor Mammedov 	WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1293ce4b1b16SIgor Mammedov 	while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1294ce4b1b16SIgor Mammedov 		cpu_relax();
1295ce4b1b16SIgor Mammedov #endif
1296ce4b1b16SIgor Mammedov }
1297ce4b1b16SIgor Mammedov 
1298f7627e25SThomas Gleixner /*
1299f7627e25SThomas Gleixner  * cpu_init() initializes state that is per-CPU. Some data is already
1300f7627e25SThomas Gleixner  * initialized (naturally) in the bootstrap process, such as the GDT
1301f7627e25SThomas Gleixner  * and IDT. We reload them nevertheless, this function acts as a
1302f7627e25SThomas Gleixner  * 'CPU state barrier', nothing should get across.
13031ba76586SYinghai Lu  * A lot of state is already set up in PDA init for 64 bit
1304f7627e25SThomas Gleixner  */
13051ba76586SYinghai Lu #ifdef CONFIG_X86_64
13060f3fa48aSIngo Molnar 
1307148f9bb8SPaul Gortmaker void cpu_init(void)
13081ba76586SYinghai Lu {
13090fe1e009STejun Heo 	struct orig_ist *oist;
13101ba76586SYinghai Lu 	struct task_struct *me;
13110f3fa48aSIngo Molnar 	struct tss_struct *t;
13120f3fa48aSIngo Molnar 	unsigned long v;
1313ce4b1b16SIgor Mammedov 	int cpu = stack_smp_processor_id();
13141ba76586SYinghai Lu 	int i;
13151ba76586SYinghai Lu 
1316ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1317ce4b1b16SIgor Mammedov 
1318e6ebf5deSFenghua Yu 	/*
13191e02ce4cSAndy Lutomirski 	 * Initialize the CR4 shadow before doing anything that could
13201e02ce4cSAndy Lutomirski 	 * try to read it.
13211e02ce4cSAndy Lutomirski 	 */
13221e02ce4cSAndy Lutomirski 	cr4_init_shadow();
13231e02ce4cSAndy Lutomirski 
13241e02ce4cSAndy Lutomirski 	/*
1325e6ebf5deSFenghua Yu 	 * Load microcode on this cpu if a valid microcode is available.
1326e6ebf5deSFenghua Yu 	 * This is early microcode loading procedure.
1327e6ebf5deSFenghua Yu 	 */
1328e6ebf5deSFenghua Yu 	load_ucode_ap();
1329e6ebf5deSFenghua Yu 
133024933b82SAndy Lutomirski 	t = &per_cpu(cpu_tss, cpu);
13310fe1e009STejun Heo 	oist = &per_cpu(orig_ist, cpu);
13320f3fa48aSIngo Molnar 
1333e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA
133427fd185fSFenghua Yu 	if (this_cpu_read(numa_node) == 0 &&
1335e534c7c5SLee Schermerhorn 	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
1336e534c7c5SLee Schermerhorn 		set_numa_node(early_cpu_to_node(cpu));
1337e7a22c1eSBrian Gerst #endif
13381ba76586SYinghai Lu 
13391ba76586SYinghai Lu 	me = current;
13401ba76586SYinghai Lu 
13412eaad1fdSMike Travis 	pr_debug("Initializing CPU#%d\n", cpu);
13421ba76586SYinghai Lu 
1343375074ccSAndy Lutomirski 	cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
13441ba76586SYinghai Lu 
13451ba76586SYinghai Lu 	/*
13461ba76586SYinghai Lu 	 * Initialize the per-CPU GDT with the boot GDT,
13471ba76586SYinghai Lu 	 * and set up the GDT descriptor:
13481ba76586SYinghai Lu 	 */
13491ba76586SYinghai Lu 
1350552be871SBrian Gerst 	switch_to_new_gdt(cpu);
13512697fbd5SBrian Gerst 	loadsegment(fs, 0);
13522697fbd5SBrian Gerst 
1353cf910e83SSeiji Aguchi 	load_current_idt();
13541ba76586SYinghai Lu 
13551ba76586SYinghai Lu 	memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
13561ba76586SYinghai Lu 	syscall_init();
13571ba76586SYinghai Lu 
13581ba76586SYinghai Lu 	wrmsrl(MSR_FS_BASE, 0);
13591ba76586SYinghai Lu 	wrmsrl(MSR_KERNEL_GS_BASE, 0);
13601ba76586SYinghai Lu 	barrier();
13611ba76586SYinghai Lu 
13624763ed4dSH. Peter Anvin 	x86_configure_nx();
1363659006bfSThomas Gleixner 	x2apic_setup();
13641ba76586SYinghai Lu 
13651ba76586SYinghai Lu 	/*
13661ba76586SYinghai Lu 	 * set up and load the per-CPU TSS
13671ba76586SYinghai Lu 	 */
13680fe1e009STejun Heo 	if (!oist->ist[0]) {
136992d65b23SBrian Gerst 		char *estacks = per_cpu(exception_stacks, cpu);
13700f3fa48aSIngo Molnar 
13711ba76586SYinghai Lu 		for (v = 0; v < N_EXCEPTION_STACKS; v++) {
13720f3fa48aSIngo Molnar 			estacks += exception_stack_sizes[v];
13730fe1e009STejun Heo 			oist->ist[v] = t->x86_tss.ist[v] =
13741ba76586SYinghai Lu 					(unsigned long)estacks;
1375228bdaa9SSteven Rostedt 			if (v == DEBUG_STACK-1)
1376228bdaa9SSteven Rostedt 				per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
13771ba76586SYinghai Lu 		}
13781ba76586SYinghai Lu 	}
13791ba76586SYinghai Lu 
13801ba76586SYinghai Lu 	t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
13810f3fa48aSIngo Molnar 
13821ba76586SYinghai Lu 	/*
13831ba76586SYinghai Lu 	 * <= is required because the CPU will access up to
13841ba76586SYinghai Lu 	 * 8 bits beyond the end of the IO permission bitmap.
13851ba76586SYinghai Lu 	 */
13861ba76586SYinghai Lu 	for (i = 0; i <= IO_BITMAP_LONGS; i++)
13871ba76586SYinghai Lu 		t->io_bitmap[i] = ~0UL;
13881ba76586SYinghai Lu 
13891ba76586SYinghai Lu 	atomic_inc(&init_mm.mm_count);
13901ba76586SYinghai Lu 	me->active_mm = &init_mm;
13918c5dfd25SStoyan Gaydarov 	BUG_ON(me->mm);
13921ba76586SYinghai Lu 	enter_lazy_tlb(&init_mm, me);
13931ba76586SYinghai Lu 
13941ba76586SYinghai Lu 	load_sp0(t, &current->thread);
13951ba76586SYinghai Lu 	set_tss_desc(cpu, t);
13961ba76586SYinghai Lu 	load_TR_desc();
13971ba76586SYinghai Lu 	load_LDT(&init_mm.context);
13981ba76586SYinghai Lu 
13999766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
14000bb9fef9SJason Wessel 	dbg_restore_debug_regs();
14011ba76586SYinghai Lu 
14021ba76586SYinghai Lu 	fpu_init();
14031ba76586SYinghai Lu 
14041ba76586SYinghai Lu 	if (is_uv_system())
14051ba76586SYinghai Lu 		uv_cpu_init();
14061ba76586SYinghai Lu }
14071ba76586SYinghai Lu 
14081ba76586SYinghai Lu #else
14091ba76586SYinghai Lu 
1410148f9bb8SPaul Gortmaker void cpu_init(void)
1411f7627e25SThomas Gleixner {
1412f7627e25SThomas Gleixner 	int cpu = smp_processor_id();
1413f7627e25SThomas Gleixner 	struct task_struct *curr = current;
141424933b82SAndy Lutomirski 	struct tss_struct *t = &per_cpu(cpu_tss, cpu);
1415f7627e25SThomas Gleixner 	struct thread_struct *thread = &curr->thread;
1416f7627e25SThomas Gleixner 
1417ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1418e6ebf5deSFenghua Yu 
14195b2bdbc8SSteven Rostedt 	/*
14205b2bdbc8SSteven Rostedt 	 * Initialize the CR4 shadow before doing anything that could
14215b2bdbc8SSteven Rostedt 	 * try to read it.
14225b2bdbc8SSteven Rostedt 	 */
14235b2bdbc8SSteven Rostedt 	cr4_init_shadow();
14245b2bdbc8SSteven Rostedt 
1425ce4b1b16SIgor Mammedov 	show_ucode_info_early();
1426f7627e25SThomas Gleixner 
1427f7627e25SThomas Gleixner 	printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1428f7627e25SThomas Gleixner 
14299298b815SDave Hansen 	if (cpu_feature_enabled(X86_FEATURE_VME) || cpu_has_tsc || cpu_has_de)
1430375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1431f7627e25SThomas Gleixner 
1432cf910e83SSeiji Aguchi 	load_current_idt();
1433552be871SBrian Gerst 	switch_to_new_gdt(cpu);
1434f7627e25SThomas Gleixner 
1435f7627e25SThomas Gleixner 	/*
1436f7627e25SThomas Gleixner 	 * Set up and load the per-CPU TSS and LDT
1437f7627e25SThomas Gleixner 	 */
1438f7627e25SThomas Gleixner 	atomic_inc(&init_mm.mm_count);
1439f7627e25SThomas Gleixner 	curr->active_mm = &init_mm;
14408c5dfd25SStoyan Gaydarov 	BUG_ON(curr->mm);
1441f7627e25SThomas Gleixner 	enter_lazy_tlb(&init_mm, curr);
1442f7627e25SThomas Gleixner 
1443faca6227SH. Peter Anvin 	load_sp0(t, thread);
1444f7627e25SThomas Gleixner 	set_tss_desc(cpu, t);
1445f7627e25SThomas Gleixner 	load_TR_desc();
1446f7627e25SThomas Gleixner 	load_LDT(&init_mm.context);
1447f7627e25SThomas Gleixner 
1448f9a196b8SThomas Gleixner 	t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1449f9a196b8SThomas Gleixner 
1450f7627e25SThomas Gleixner #ifdef CONFIG_DOUBLEFAULT
1451f7627e25SThomas Gleixner 	/* Set up doublefault TSS pointer in the GDT */
1452f7627e25SThomas Gleixner 	__set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1453f7627e25SThomas Gleixner #endif
1454f7627e25SThomas Gleixner 
14559766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
14560bb9fef9SJason Wessel 	dbg_restore_debug_regs();
1457f7627e25SThomas Gleixner 
14580e49bf66SRobert Richter 	fpu_init();
1459f7627e25SThomas Gleixner }
14601ba76586SYinghai Lu #endif
14615700f743SBorislav Petkov 
14625700f743SBorislav Petkov #ifdef CONFIG_X86_DEBUG_STATIC_CPU_HAS
14635700f743SBorislav Petkov void warn_pre_alternatives(void)
14645700f743SBorislav Petkov {
14655700f743SBorislav Petkov 	WARN(1, "You're using static_cpu_has before alternatives have run!\n");
14665700f743SBorislav Petkov }
14675700f743SBorislav Petkov EXPORT_SYMBOL_GPL(warn_pre_alternatives);
14685700f743SBorislav Petkov #endif
14694a90a99cSBorislav Petkov 
14704a90a99cSBorislav Petkov inline bool __static_cpu_has_safe(u16 bit)
14714a90a99cSBorislav Petkov {
14724a90a99cSBorislav Petkov 	return boot_cpu_has(bit);
14734a90a99cSBorislav Petkov }
14744a90a99cSBorislav Petkov EXPORT_SYMBOL_GPL(__static_cpu_has_safe);
1475