xref: /linux/arch/x86/kernel/cpu/common.c (revision b51ef52df71cb28e9d90cd1d48b79bf19f0bab06)
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>
16*b51ef52dSLaura Abbott #include <linux/syscore_ops.h>
179766cdbcSJaswinder Singh Rajput 
189766cdbcSJaswinder Singh Rajput #include <asm/stackprotector.h>
19cdd6c482SIngo Molnar #include <asm/perf_event.h>
20f7627e25SThomas Gleixner #include <asm/mmu_context.h>
2149d859d7SH. Peter Anvin #include <asm/archrandom.h>
229766cdbcSJaswinder Singh Rajput #include <asm/hypervisor.h>
239766cdbcSJaswinder Singh Rajput #include <asm/processor.h>
241e02ce4cSAndy Lutomirski #include <asm/tlbflush.h>
25f649e938SPaul Gortmaker #include <asm/debugreg.h>
269766cdbcSJaswinder Singh Rajput #include <asm/sections.h>
27f40c3300SAndy Lutomirski #include <asm/vsyscall.h>
288bdbd962SAlan Cox #include <linux/topology.h>
298bdbd962SAlan Cox #include <linux/cpumask.h>
309766cdbcSJaswinder Singh Rajput #include <asm/pgtable.h>
3160063497SArun Sharma #include <linux/atomic.h>
329766cdbcSJaswinder Singh Rajput #include <asm/proto.h>
339766cdbcSJaswinder Singh Rajput #include <asm/setup.h>
34f7627e25SThomas Gleixner #include <asm/apic.h>
359766cdbcSJaswinder Singh Rajput #include <asm/desc.h>
3678f7f1e5SIngo Molnar #include <asm/fpu/internal.h>
379766cdbcSJaswinder Singh Rajput #include <asm/mtrr.h>
388bdbd962SAlan Cox #include <linux/numa.h>
399766cdbcSJaswinder Singh Rajput #include <asm/asm.h>
409766cdbcSJaswinder Singh Rajput #include <asm/cpu.h>
419766cdbcSJaswinder Singh Rajput #include <asm/mce.h>
429766cdbcSJaswinder Singh Rajput #include <asm/msr.h>
439766cdbcSJaswinder Singh Rajput #include <asm/pat.h>
44d288e1cfSFenghua Yu #include <asm/microcode.h>
45d288e1cfSFenghua Yu #include <asm/microcode_intel.h>
46e641f5f5SIngo Molnar 
47f7627e25SThomas Gleixner #ifdef CONFIG_X86_LOCAL_APIC
48bdbcdd48STejun Heo #include <asm/uv/uv.h>
49f7627e25SThomas Gleixner #endif
50f7627e25SThomas Gleixner 
51f7627e25SThomas Gleixner #include "cpu.h"
52f7627e25SThomas Gleixner 
53c2d1cec1SMike Travis /* all of these masks are initialized in setup_cpu_local_masks() */
54c2d1cec1SMike Travis cpumask_var_t cpu_initialized_mask;
559766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callout_mask;
569766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callin_mask;
57c2d1cec1SMike Travis 
58c2d1cec1SMike Travis /* representing cpus for which sibling maps can be computed */
59c2d1cec1SMike Travis cpumask_var_t cpu_sibling_setup_mask;
60c2d1cec1SMike Travis 
612f2f52baSBrian Gerst /* correctly size the local cpu masks */
624369f1fbSIngo Molnar void __init setup_cpu_local_masks(void)
632f2f52baSBrian Gerst {
642f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_initialized_mask);
652f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callin_mask);
662f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callout_mask);
672f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
682f2f52baSBrian Gerst }
692f2f52baSBrian Gerst 
70148f9bb8SPaul Gortmaker static void default_init(struct cpuinfo_x86 *c)
71e8055139SOndrej Zary {
72e8055139SOndrej Zary #ifdef CONFIG_X86_64
7327c13eceSBorislav Petkov 	cpu_detect_cache_sizes(c);
74e8055139SOndrej Zary #else
75e8055139SOndrej Zary 	/* Not much we can do here... */
76e8055139SOndrej Zary 	/* Check if at least it has cpuid */
77e8055139SOndrej Zary 	if (c->cpuid_level == -1) {
78e8055139SOndrej Zary 		/* No cpuid. It must be an ancient CPU */
79e8055139SOndrej Zary 		if (c->x86 == 4)
80e8055139SOndrej Zary 			strcpy(c->x86_model_id, "486");
81e8055139SOndrej Zary 		else if (c->x86 == 3)
82e8055139SOndrej Zary 			strcpy(c->x86_model_id, "386");
83e8055139SOndrej Zary 	}
84e8055139SOndrej Zary #endif
85e8055139SOndrej Zary }
86e8055139SOndrej Zary 
87148f9bb8SPaul Gortmaker static const struct cpu_dev default_cpu = {
88e8055139SOndrej Zary 	.c_init		= default_init,
89e8055139SOndrej Zary 	.c_vendor	= "Unknown",
90e8055139SOndrej Zary 	.c_x86_vendor	= X86_VENDOR_UNKNOWN,
91e8055139SOndrej Zary };
92e8055139SOndrej Zary 
93148f9bb8SPaul Gortmaker static const struct cpu_dev *this_cpu = &default_cpu;
940a488a53SYinghai Lu 
9506deef89SBrian Gerst DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
96950ad7ffSYinghai Lu #ifdef CONFIG_X86_64
9706deef89SBrian Gerst 	/*
9806deef89SBrian Gerst 	 * We need valid kernel segments for data and code in long mode too
99950ad7ffSYinghai Lu 	 * IRET will check the segment types  kkeil 2000/10/28
100950ad7ffSYinghai Lu 	 * Also sysret mandates a special GDT layout
10106deef89SBrian Gerst 	 *
1029766cdbcSJaswinder Singh Rajput 	 * TLS descriptors are currently at a different place compared to i386.
10306deef89SBrian Gerst 	 * Hopefully nobody expects them at a fixed place (Wine?)
104950ad7ffSYinghai Lu 	 */
1051e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL32_CS]		= GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
1061e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
1071e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
1081e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER32_CS]	= GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
1091e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
1101e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
111950ad7ffSYinghai Lu #else
1121e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
1131e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1141e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
1151e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
116f7627e25SThomas Gleixner 	/*
117f7627e25SThomas Gleixner 	 * Segments used for calling PnP BIOS have byte granularity.
118f7627e25SThomas Gleixner 	 * They code segments and data segments have fixed 64k limits,
119f7627e25SThomas Gleixner 	 * the transfer segment sizes are set at run time.
120f7627e25SThomas Gleixner 	 */
1216842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1221e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS32]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
1236842ef0eSGlauber de Oliveira Costa 	/* 16-bit code */
1241e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS16]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1256842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1261e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_DS]		= GDT_ENTRY_INIT(0x0092, 0, 0xffff),
1276842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1281e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS1]		= GDT_ENTRY_INIT(0x0092, 0, 0),
1296842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1301e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS2]		= GDT_ENTRY_INIT(0x0092, 0, 0),
131f7627e25SThomas Gleixner 	/*
132f7627e25SThomas Gleixner 	 * The APM segments have byte granularity and their bases
133f7627e25SThomas Gleixner 	 * are set at run time.  All have 64k limits.
134f7627e25SThomas Gleixner 	 */
1356842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1361e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
137f7627e25SThomas Gleixner 	/* 16-bit code */
1381e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE+1]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1396842ef0eSGlauber de Oliveira Costa 	/* data */
14072c4d853SIngo Molnar 	[GDT_ENTRY_APMBIOS_BASE+2]	= GDT_ENTRY_INIT(0x4092, 0, 0xffff),
141f7627e25SThomas Gleixner 
1421e5de182SAkinobu Mita 	[GDT_ENTRY_ESPFIX_SS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1431e5de182SAkinobu Mita 	[GDT_ENTRY_PERCPU]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
14460a5317fSTejun Heo 	GDT_STACK_CANARY_INIT
145950ad7ffSYinghai Lu #endif
14606deef89SBrian Gerst } };
147f7627e25SThomas Gleixner EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
148f7627e25SThomas Gleixner 
1498c3641e9SDave Hansen static int __init x86_mpx_setup(char *s)
1500c752a93SSuresh Siddha {
1518c3641e9SDave Hansen 	/* require an exact match without trailing characters */
1522cd3949fSDave Hansen 	if (strlen(s))
1532cd3949fSDave Hansen 		return 0;
1540c752a93SSuresh Siddha 
1558c3641e9SDave Hansen 	/* do not emit a message if the feature is not present */
1568c3641e9SDave Hansen 	if (!boot_cpu_has(X86_FEATURE_MPX))
1576bad06b7SSuresh Siddha 		return 1;
1586bad06b7SSuresh Siddha 
1598c3641e9SDave Hansen 	setup_clear_cpu_cap(X86_FEATURE_MPX);
1608c3641e9SDave Hansen 	pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
161b6f42a4aSFenghua Yu 	return 1;
162b6f42a4aSFenghua Yu }
1638c3641e9SDave Hansen __setup("nompx", x86_mpx_setup);
164b6f42a4aSFenghua Yu 
165ba51dcedSYinghai Lu #ifdef CONFIG_X86_32
166148f9bb8SPaul Gortmaker static int cachesize_override = -1;
167148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1;
168f7627e25SThomas Gleixner 
169f7627e25SThomas Gleixner static int __init cachesize_setup(char *str)
170f7627e25SThomas Gleixner {
171f7627e25SThomas Gleixner 	get_option(&str, &cachesize_override);
172f7627e25SThomas Gleixner 	return 1;
173f7627e25SThomas Gleixner }
174f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup);
175f7627e25SThomas Gleixner 
176f7627e25SThomas Gleixner static int __init x86_sep_setup(char *s)
177f7627e25SThomas Gleixner {
17813530257SAndi Kleen 	setup_clear_cpu_cap(X86_FEATURE_SEP);
179f7627e25SThomas Gleixner 	return 1;
180f7627e25SThomas Gleixner }
181f7627e25SThomas Gleixner __setup("nosep", x86_sep_setup);
182f7627e25SThomas Gleixner 
183f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */
184f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag)
185f7627e25SThomas Gleixner {
186f7627e25SThomas Gleixner 	u32 f1, f2;
187f7627e25SThomas Gleixner 
18894f6bac1SKrzysztof Helt 	/*
18994f6bac1SKrzysztof Helt 	 * Cyrix and IDT cpus allow disabling of CPUID
19094f6bac1SKrzysztof Helt 	 * so the code below may return different results
19194f6bac1SKrzysztof Helt 	 * when it is executed before and after enabling
19294f6bac1SKrzysztof Helt 	 * the CPUID. Add "volatile" to not allow gcc to
19394f6bac1SKrzysztof Helt 	 * optimize the subsequent calls to this function.
19494f6bac1SKrzysztof Helt 	 */
19594f6bac1SKrzysztof Helt 	asm volatile ("pushfl		\n\t"
196f7627e25SThomas Gleixner 		      "pushfl		\n\t"
197f7627e25SThomas Gleixner 		      "popl %0		\n\t"
198f7627e25SThomas Gleixner 		      "movl %0, %1	\n\t"
199f7627e25SThomas Gleixner 		      "xorl %2, %0	\n\t"
200f7627e25SThomas Gleixner 		      "pushl %0		\n\t"
201f7627e25SThomas Gleixner 		      "popfl		\n\t"
202f7627e25SThomas Gleixner 		      "pushfl		\n\t"
203f7627e25SThomas Gleixner 		      "popl %0		\n\t"
204f7627e25SThomas Gleixner 		      "popfl		\n\t"
2050f3fa48aSIngo Molnar 
206f7627e25SThomas Gleixner 		      : "=&r" (f1), "=&r" (f2)
207f7627e25SThomas Gleixner 		      : "ir" (flag));
208f7627e25SThomas Gleixner 
209f7627e25SThomas Gleixner 	return ((f1^f2) & flag) != 0;
210f7627e25SThomas Gleixner }
211f7627e25SThomas Gleixner 
212f7627e25SThomas Gleixner /* Probe for the CPUID instruction */
213148f9bb8SPaul Gortmaker int have_cpuid_p(void)
214f7627e25SThomas Gleixner {
215f7627e25SThomas Gleixner 	return flag_is_changeable_p(X86_EFLAGS_ID);
216f7627e25SThomas Gleixner }
217f7627e25SThomas Gleixner 
218148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
2190a488a53SYinghai Lu {
2200a488a53SYinghai Lu 	unsigned long lo, hi;
2210f3fa48aSIngo Molnar 
2220f3fa48aSIngo Molnar 	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
2230f3fa48aSIngo Molnar 		return;
2240f3fa48aSIngo Molnar 
2250f3fa48aSIngo Molnar 	/* Disable processor serial number: */
2260f3fa48aSIngo Molnar 
2270a488a53SYinghai Lu 	rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2280a488a53SYinghai Lu 	lo |= 0x200000;
2290a488a53SYinghai Lu 	wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2300f3fa48aSIngo Molnar 
2310a488a53SYinghai Lu 	printk(KERN_NOTICE "CPU serial number disabled.\n");
2320a488a53SYinghai Lu 	clear_cpu_cap(c, X86_FEATURE_PN);
2330a488a53SYinghai Lu 
2340a488a53SYinghai Lu 	/* Disabling the serial number may affect the cpuid level */
2350a488a53SYinghai Lu 	c->cpuid_level = cpuid_eax(0);
2360a488a53SYinghai Lu }
2370a488a53SYinghai Lu 
2380a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s)
2390a488a53SYinghai Lu {
2400a488a53SYinghai Lu 	disable_x86_serial_nr = 0;
2410a488a53SYinghai Lu 	return 1;
2420a488a53SYinghai Lu }
2430a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup);
244ba51dcedSYinghai Lu #else
245102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag)
246102bbe3aSYinghai Lu {
247102bbe3aSYinghai Lu 	return 1;
248102bbe3aSYinghai Lu }
249102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
250102bbe3aSYinghai Lu {
251102bbe3aSYinghai Lu }
252ba51dcedSYinghai Lu #endif
2530a488a53SYinghai Lu 
254de5397adSFenghua Yu static __init int setup_disable_smep(char *arg)
255de5397adSFenghua Yu {
256b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMEP);
257de5397adSFenghua Yu 	return 1;
258de5397adSFenghua Yu }
259de5397adSFenghua Yu __setup("nosmep", setup_disable_smep);
260de5397adSFenghua Yu 
261b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c)
262de5397adSFenghua Yu {
263b2cc2a07SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMEP))
264375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMEP);
265de5397adSFenghua Yu }
266de5397adSFenghua Yu 
26752b6179aSH. Peter Anvin static __init int setup_disable_smap(char *arg)
26852b6179aSH. Peter Anvin {
269b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMAP);
27052b6179aSH. Peter Anvin 	return 1;
27152b6179aSH. Peter Anvin }
27252b6179aSH. Peter Anvin __setup("nosmap", setup_disable_smap);
27352b6179aSH. Peter Anvin 
274b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c)
27552b6179aSH. Peter Anvin {
276b2cc2a07SH. Peter Anvin 	unsigned long eflags;
277b2cc2a07SH. Peter Anvin 
278b2cc2a07SH. Peter Anvin 	/* This should have been cleared long ago */
279b2cc2a07SH. Peter Anvin 	raw_local_save_flags(eflags);
280b2cc2a07SH. Peter Anvin 	BUG_ON(eflags & X86_EFLAGS_AC);
281b2cc2a07SH. Peter Anvin 
28203bbd596SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMAP)) {
28303bbd596SH. Peter Anvin #ifdef CONFIG_X86_SMAP
284375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMAP);
28503bbd596SH. Peter Anvin #else
286375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_SMAP);
28703bbd596SH. Peter Anvin #endif
28803bbd596SH. Peter Anvin 	}
289f7627e25SThomas Gleixner }
290f7627e25SThomas Gleixner 
291f7627e25SThomas Gleixner /*
292b38b0665SH. Peter Anvin  * Some CPU features depend on higher CPUID levels, which may not always
293b38b0665SH. Peter Anvin  * be available due to CPUID level capping or broken virtualization
294b38b0665SH. Peter Anvin  * software.  Add those features to this table to auto-disable them.
295b38b0665SH. Peter Anvin  */
296b38b0665SH. Peter Anvin struct cpuid_dependent_feature {
297b38b0665SH. Peter Anvin 	u32 feature;
298b38b0665SH. Peter Anvin 	u32 level;
299b38b0665SH. Peter Anvin };
3000f3fa48aSIngo Molnar 
301148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature
302b38b0665SH. Peter Anvin cpuid_dependent_features[] = {
303b38b0665SH. Peter Anvin 	{ X86_FEATURE_MWAIT,		0x00000005 },
304b38b0665SH. Peter Anvin 	{ X86_FEATURE_DCA,		0x00000009 },
305b38b0665SH. Peter Anvin 	{ X86_FEATURE_XSAVE,		0x0000000d },
306b38b0665SH. Peter Anvin 	{ 0, 0 }
307b38b0665SH. Peter Anvin };
308b38b0665SH. Peter Anvin 
309148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
310b38b0665SH. Peter Anvin {
311b38b0665SH. Peter Anvin 	const struct cpuid_dependent_feature *df;
3129766cdbcSJaswinder Singh Rajput 
313b38b0665SH. Peter Anvin 	for (df = cpuid_dependent_features; df->feature; df++) {
3140f3fa48aSIngo Molnar 
3150f3fa48aSIngo Molnar 		if (!cpu_has(c, df->feature))
3160f3fa48aSIngo Molnar 			continue;
317b38b0665SH. Peter Anvin 		/*
318b38b0665SH. Peter Anvin 		 * Note: cpuid_level is set to -1 if unavailable, but
319b38b0665SH. Peter Anvin 		 * extended_extended_level is set to 0 if unavailable
320b38b0665SH. Peter Anvin 		 * and the legitimate extended levels are all negative
321b38b0665SH. Peter Anvin 		 * when signed; hence the weird messing around with
322b38b0665SH. Peter Anvin 		 * signs here...
323b38b0665SH. Peter Anvin 		 */
3240f3fa48aSIngo Molnar 		if (!((s32)df->level < 0 ?
325f6db44dfSYinghai Lu 		     (u32)df->level > (u32)c->extended_cpuid_level :
3260f3fa48aSIngo Molnar 		     (s32)df->level > (s32)c->cpuid_level))
3270f3fa48aSIngo Molnar 			continue;
3280f3fa48aSIngo Molnar 
329b38b0665SH. Peter Anvin 		clear_cpu_cap(c, df->feature);
3300f3fa48aSIngo Molnar 		if (!warn)
3310f3fa48aSIngo Molnar 			continue;
3320f3fa48aSIngo Molnar 
333b38b0665SH. Peter Anvin 		printk(KERN_WARNING
3349def39beSJosh Triplett 		       "CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
3359def39beSJosh Triplett 				x86_cap_flag(df->feature), df->level);
336b38b0665SH. Peter Anvin 	}
337b38b0665SH. Peter Anvin }
338b38b0665SH. Peter Anvin 
339b38b0665SH. Peter Anvin /*
340f7627e25SThomas Gleixner  * Naming convention should be: <Name> [(<Codename>)]
341f7627e25SThomas Gleixner  * This table only is used unless init_<vendor>() below doesn't set it;
3420f3fa48aSIngo Molnar  * in particular, if CPUID levels 0x80000002..4 are supported, this
3430f3fa48aSIngo Molnar  * isn't used
344f7627e25SThomas Gleixner  */
345f7627e25SThomas Gleixner 
346f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */
347148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c)
348f7627e25SThomas Gleixner {
34909dc68d9SJan Beulich #ifdef CONFIG_X86_32
35009dc68d9SJan Beulich 	const struct legacy_cpu_model_info *info;
351f7627e25SThomas Gleixner 
352f7627e25SThomas Gleixner 	if (c->x86_model >= 16)
353f7627e25SThomas Gleixner 		return NULL;	/* Range check */
354f7627e25SThomas Gleixner 
355f7627e25SThomas Gleixner 	if (!this_cpu)
356f7627e25SThomas Gleixner 		return NULL;
357f7627e25SThomas Gleixner 
35809dc68d9SJan Beulich 	info = this_cpu->legacy_models;
359f7627e25SThomas Gleixner 
36009dc68d9SJan Beulich 	while (info->family) {
361f7627e25SThomas Gleixner 		if (info->family == c->x86)
362f7627e25SThomas Gleixner 			return info->model_names[c->x86_model];
363f7627e25SThomas Gleixner 		info++;
364f7627e25SThomas Gleixner 	}
36509dc68d9SJan Beulich #endif
366f7627e25SThomas Gleixner 	return NULL;		/* Not found */
367f7627e25SThomas Gleixner }
368f7627e25SThomas Gleixner 
369148f9bb8SPaul Gortmaker __u32 cpu_caps_cleared[NCAPINTS];
370148f9bb8SPaul Gortmaker __u32 cpu_caps_set[NCAPINTS];
371f7627e25SThomas Gleixner 
37211e3a840SJeremy Fitzhardinge void load_percpu_segment(int cpu)
3739d31d35bSYinghai Lu {
374fab334c1SYinghai Lu #ifdef CONFIG_X86_32
3752697fbd5SBrian Gerst 	loadsegment(fs, __KERNEL_PERCPU);
3762697fbd5SBrian Gerst #else
3772697fbd5SBrian Gerst 	loadsegment(gs, 0);
3782697fbd5SBrian Gerst 	wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
379fab334c1SYinghai Lu #endif
38060a5317fSTejun Heo 	load_stack_canary_segment();
3819d31d35bSYinghai Lu }
3829d31d35bSYinghai Lu 
3830f3fa48aSIngo Molnar /*
3840f3fa48aSIngo Molnar  * Current gdt points %fs at the "master" per-cpu area: after this,
3850f3fa48aSIngo Molnar  * it's on the real one.
3860f3fa48aSIngo Molnar  */
387552be871SBrian Gerst void switch_to_new_gdt(int cpu)
388f7627e25SThomas Gleixner {
389f7627e25SThomas Gleixner 	struct desc_ptr gdt_descr;
390f7627e25SThomas Gleixner 
391f7627e25SThomas Gleixner 	gdt_descr.address = (long)get_cpu_gdt_table(cpu);
392f7627e25SThomas Gleixner 	gdt_descr.size = GDT_SIZE - 1;
393f7627e25SThomas Gleixner 	load_gdt(&gdt_descr);
394f7627e25SThomas Gleixner 	/* Reload the per-cpu base */
39511e3a840SJeremy Fitzhardinge 
39611e3a840SJeremy Fitzhardinge 	load_percpu_segment(cpu);
397f7627e25SThomas Gleixner }
398f7627e25SThomas Gleixner 
399148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
400f7627e25SThomas Gleixner 
401148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c)
402f7627e25SThomas Gleixner {
403f7627e25SThomas Gleixner 	unsigned int *v;
404ee098e1aSBorislav Petkov 	char *p, *q, *s;
405f7627e25SThomas Gleixner 
4063da99c97SYinghai Lu 	if (c->extended_cpuid_level < 0x80000004)
4071b05d60dSYinghai Lu 		return;
408f7627e25SThomas Gleixner 
409f7627e25SThomas Gleixner 	v = (unsigned int *)c->x86_model_id;
410f7627e25SThomas Gleixner 	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
411f7627e25SThomas Gleixner 	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
412f7627e25SThomas Gleixner 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
413f7627e25SThomas Gleixner 	c->x86_model_id[48] = 0;
414f7627e25SThomas Gleixner 
415ee098e1aSBorislav Petkov 	/* Trim whitespace */
416ee098e1aSBorislav Petkov 	p = q = s = &c->x86_model_id[0];
417ee098e1aSBorislav Petkov 
418ee098e1aSBorislav Petkov 	while (*p == ' ')
419ee098e1aSBorislav Petkov 		p++;
420ee098e1aSBorislav Petkov 
421ee098e1aSBorislav Petkov 	while (*p) {
422ee098e1aSBorislav Petkov 		/* Note the last non-whitespace index */
423ee098e1aSBorislav Petkov 		if (!isspace(*p))
424ee098e1aSBorislav Petkov 			s = q;
425ee098e1aSBorislav Petkov 
426ee098e1aSBorislav Petkov 		*q++ = *p++;
427ee098e1aSBorislav Petkov 	}
428ee098e1aSBorislav Petkov 
429ee098e1aSBorislav Petkov 	*(s + 1) = '\0';
430f7627e25SThomas Gleixner }
431f7627e25SThomas Gleixner 
432148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
433f7627e25SThomas Gleixner {
4349d31d35bSYinghai Lu 	unsigned int n, dummy, ebx, ecx, edx, l2size;
435f7627e25SThomas Gleixner 
4363da99c97SYinghai Lu 	n = c->extended_cpuid_level;
437f7627e25SThomas Gleixner 
438f7627e25SThomas Gleixner 	if (n >= 0x80000005) {
4399d31d35bSYinghai Lu 		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
440f7627e25SThomas Gleixner 		c->x86_cache_size = (ecx>>24) + (edx>>24);
441140fc727SYinghai Lu #ifdef CONFIG_X86_64
442140fc727SYinghai Lu 		/* On K8 L1 TLB is inclusive, so don't count it */
443140fc727SYinghai Lu 		c->x86_tlbsize = 0;
444140fc727SYinghai Lu #endif
445f7627e25SThomas Gleixner 	}
446f7627e25SThomas Gleixner 
447f7627e25SThomas Gleixner 	if (n < 0x80000006)	/* Some chips just has a large L1. */
448f7627e25SThomas Gleixner 		return;
449f7627e25SThomas Gleixner 
4500a488a53SYinghai Lu 	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
451f7627e25SThomas Gleixner 	l2size = ecx >> 16;
452f7627e25SThomas Gleixner 
453140fc727SYinghai Lu #ifdef CONFIG_X86_64
454140fc727SYinghai Lu 	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
455140fc727SYinghai Lu #else
456f7627e25SThomas Gleixner 	/* do processor-specific cache resizing */
45709dc68d9SJan Beulich 	if (this_cpu->legacy_cache_size)
45809dc68d9SJan Beulich 		l2size = this_cpu->legacy_cache_size(c, l2size);
459f7627e25SThomas Gleixner 
460f7627e25SThomas Gleixner 	/* Allow user to override all this if necessary. */
461f7627e25SThomas Gleixner 	if (cachesize_override != -1)
462f7627e25SThomas Gleixner 		l2size = cachesize_override;
463f7627e25SThomas Gleixner 
464f7627e25SThomas Gleixner 	if (l2size == 0)
465f7627e25SThomas Gleixner 		return;		/* Again, no L2 cache is possible */
466140fc727SYinghai Lu #endif
467f7627e25SThomas Gleixner 
468f7627e25SThomas Gleixner 	c->x86_cache_size = l2size;
469f7627e25SThomas Gleixner }
470f7627e25SThomas Gleixner 
471e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO];
472e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO];
473e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO];
474e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO];
475e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO];
476e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO];
477dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO];
478e0ba94f1SAlex Shi 
479f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c)
480e0ba94f1SAlex Shi {
481e0ba94f1SAlex Shi 	if (this_cpu->c_detect_tlb)
482e0ba94f1SAlex Shi 		this_cpu->c_detect_tlb(c);
483e0ba94f1SAlex Shi 
484f94fe119SSteven Honeyman 	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
485e0ba94f1SAlex Shi 		tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
486f94fe119SSteven Honeyman 		tlb_lli_4m[ENTRIES]);
487f94fe119SSteven Honeyman 
488f94fe119SSteven Honeyman 	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
489f94fe119SSteven Honeyman 		tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
490f94fe119SSteven Honeyman 		tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
491e0ba94f1SAlex Shi }
492e0ba94f1SAlex Shi 
493148f9bb8SPaul Gortmaker void detect_ht(struct cpuinfo_x86 *c)
4949d31d35bSYinghai Lu {
495c8e56d20SBorislav Petkov #ifdef CONFIG_SMP
4969d31d35bSYinghai Lu 	u32 eax, ebx, ecx, edx;
4979d31d35bSYinghai Lu 	int index_msb, core_bits;
4982eaad1fdSMike Travis 	static bool printed;
4999d31d35bSYinghai Lu 
5000a488a53SYinghai Lu 	if (!cpu_has(c, X86_FEATURE_HT))
5019d31d35bSYinghai Lu 		return;
5029d31d35bSYinghai Lu 
5030a488a53SYinghai Lu 	if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
5040a488a53SYinghai Lu 		goto out;
5050a488a53SYinghai Lu 
5061cd78776SYinghai Lu 	if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
5071cd78776SYinghai Lu 		return;
5081cd78776SYinghai Lu 
5090a488a53SYinghai Lu 	cpuid(1, &eax, &ebx, &ecx, &edx);
5100a488a53SYinghai Lu 
5119d31d35bSYinghai Lu 	smp_num_siblings = (ebx & 0xff0000) >> 16;
5129d31d35bSYinghai Lu 
5139d31d35bSYinghai Lu 	if (smp_num_siblings == 1) {
5142eaad1fdSMike Travis 		printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
5150f3fa48aSIngo Molnar 		goto out;
5160f3fa48aSIngo Molnar 	}
5170f3fa48aSIngo Molnar 
5180f3fa48aSIngo Molnar 	if (smp_num_siblings <= 1)
5190f3fa48aSIngo Molnar 		goto out;
5209d31d35bSYinghai Lu 
5219d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
522cb8cc442SIngo Molnar 	c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
5239d31d35bSYinghai Lu 
5249d31d35bSYinghai Lu 	smp_num_siblings = smp_num_siblings / c->x86_max_cores;
5259d31d35bSYinghai Lu 
5269d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
5279d31d35bSYinghai Lu 
5289d31d35bSYinghai Lu 	core_bits = get_count_order(c->x86_max_cores);
5299d31d35bSYinghai Lu 
530cb8cc442SIngo Molnar 	c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
5311cd78776SYinghai Lu 				       ((1 << core_bits) - 1);
5329d31d35bSYinghai Lu 
5330a488a53SYinghai Lu out:
5342eaad1fdSMike Travis 	if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
5350a488a53SYinghai Lu 		printk(KERN_INFO  "CPU: Physical Processor ID: %d\n",
5360a488a53SYinghai Lu 		       c->phys_proc_id);
5379d31d35bSYinghai Lu 		printk(KERN_INFO  "CPU: Processor Core ID: %d\n",
5389d31d35bSYinghai Lu 		       c->cpu_core_id);
5392eaad1fdSMike Travis 		printed = 1;
5409d31d35bSYinghai Lu 	}
5419d31d35bSYinghai Lu #endif
54297e4db7cSYinghai Lu }
543f7627e25SThomas Gleixner 
544148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c)
545f7627e25SThomas Gleixner {
546f7627e25SThomas Gleixner 	char *v = c->x86_vendor_id;
5470f3fa48aSIngo Molnar 	int i;
548f7627e25SThomas Gleixner 
549f7627e25SThomas Gleixner 	for (i = 0; i < X86_VENDOR_NUM; i++) {
55010a434fcSYinghai Lu 		if (!cpu_devs[i])
55110a434fcSYinghai Lu 			break;
55210a434fcSYinghai Lu 
553f7627e25SThomas Gleixner 		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
554f7627e25SThomas Gleixner 		    (cpu_devs[i]->c_ident[1] &&
555f7627e25SThomas Gleixner 		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
5560f3fa48aSIngo Molnar 
557f7627e25SThomas Gleixner 			this_cpu = cpu_devs[i];
55810a434fcSYinghai Lu 			c->x86_vendor = this_cpu->c_x86_vendor;
559f7627e25SThomas Gleixner 			return;
560f7627e25SThomas Gleixner 		}
561f7627e25SThomas Gleixner 	}
56210a434fcSYinghai Lu 
563a9c56953SMinchan Kim 	printk_once(KERN_ERR
564a9c56953SMinchan Kim 			"CPU: vendor_id '%s' unknown, using generic init.\n" \
565a9c56953SMinchan Kim 			"CPU: Your system may be unstable.\n", v);
56610a434fcSYinghai Lu 
567f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
568f7627e25SThomas Gleixner 	this_cpu = &default_cpu;
569f7627e25SThomas Gleixner }
570f7627e25SThomas Gleixner 
571148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c)
572f7627e25SThomas Gleixner {
573f7627e25SThomas Gleixner 	/* Get vendor name */
5744a148513SHarvey Harrison 	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
5754a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[0],
5764a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[8],
5774a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[4]);
578f7627e25SThomas Gleixner 
579f7627e25SThomas Gleixner 	c->x86 = 4;
5809d31d35bSYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
581f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
582f7627e25SThomas Gleixner 		u32 junk, tfms, cap0, misc;
5830f3fa48aSIngo Molnar 
584f7627e25SThomas Gleixner 		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
5859d31d35bSYinghai Lu 		c->x86 = (tfms >> 8) & 0xf;
5869d31d35bSYinghai Lu 		c->x86_model = (tfms >> 4) & 0xf;
5879d31d35bSYinghai Lu 		c->x86_mask = tfms & 0xf;
5880f3fa48aSIngo Molnar 
589f7627e25SThomas Gleixner 		if (c->x86 == 0xf)
590f7627e25SThomas Gleixner 			c->x86 += (tfms >> 20) & 0xff;
591f7627e25SThomas Gleixner 		if (c->x86 >= 0x6)
5929d31d35bSYinghai Lu 			c->x86_model += ((tfms >> 16) & 0xf) << 4;
5930f3fa48aSIngo Molnar 
594d4387bd3SHuang, Ying 		if (cap0 & (1<<19)) {
595d4387bd3SHuang, Ying 			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
5969d31d35bSYinghai Lu 			c->x86_cache_alignment = c->x86_clflush_size;
597d4387bd3SHuang, Ying 		}
598f7627e25SThomas Gleixner 	}
599f7627e25SThomas Gleixner }
6003da99c97SYinghai Lu 
601148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c)
602093af8d7SYinghai Lu {
603093af8d7SYinghai Lu 	u32 tfms, xlvl;
6043da99c97SYinghai Lu 	u32 ebx;
605093af8d7SYinghai Lu 
606093af8d7SYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
607093af8d7SYinghai Lu 	if (c->cpuid_level >= 0x00000001) {
608093af8d7SYinghai Lu 		u32 capability, excap;
6090f3fa48aSIngo Molnar 
610093af8d7SYinghai Lu 		cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
611093af8d7SYinghai Lu 		c->x86_capability[0] = capability;
612093af8d7SYinghai Lu 		c->x86_capability[4] = excap;
613093af8d7SYinghai Lu 	}
614093af8d7SYinghai Lu 
615bdc802dcSH. Peter Anvin 	/* Additional Intel-defined flags: level 0x00000007 */
616bdc802dcSH. Peter Anvin 	if (c->cpuid_level >= 0x00000007) {
617bdc802dcSH. Peter Anvin 		u32 eax, ebx, ecx, edx;
618bdc802dcSH. Peter Anvin 
619bdc802dcSH. Peter Anvin 		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
620bdc802dcSH. Peter Anvin 
621bdc802dcSH. Peter Anvin 		c->x86_capability[9] = ebx;
622bdc802dcSH. Peter Anvin 	}
623bdc802dcSH. Peter Anvin 
6246229ad27SFenghua Yu 	/* Extended state features: level 0x0000000d */
6256229ad27SFenghua Yu 	if (c->cpuid_level >= 0x0000000d) {
6266229ad27SFenghua Yu 		u32 eax, ebx, ecx, edx;
6276229ad27SFenghua Yu 
6286229ad27SFenghua Yu 		cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
6296229ad27SFenghua Yu 
6306229ad27SFenghua Yu 		c->x86_capability[10] = eax;
6316229ad27SFenghua Yu 	}
6326229ad27SFenghua Yu 
633cbc82b17SPeter P Waskiewicz Jr 	/* Additional Intel-defined flags: level 0x0000000F */
634cbc82b17SPeter P Waskiewicz Jr 	if (c->cpuid_level >= 0x0000000F) {
635cbc82b17SPeter P Waskiewicz Jr 		u32 eax, ebx, ecx, edx;
636cbc82b17SPeter P Waskiewicz Jr 
637cbc82b17SPeter P Waskiewicz Jr 		/* QoS sub-leaf, EAX=0Fh, ECX=0 */
638cbc82b17SPeter P Waskiewicz Jr 		cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
639cbc82b17SPeter P Waskiewicz Jr 		c->x86_capability[11] = edx;
640cbc82b17SPeter P Waskiewicz Jr 		if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
641cbc82b17SPeter P Waskiewicz Jr 			/* will be overridden if occupancy monitoring exists */
642cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_max_rmid = ebx;
643cbc82b17SPeter P Waskiewicz Jr 
644cbc82b17SPeter P Waskiewicz Jr 			/* QoS sub-leaf, EAX=0Fh, ECX=1 */
645cbc82b17SPeter P Waskiewicz Jr 			cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
646cbc82b17SPeter P Waskiewicz Jr 			c->x86_capability[12] = edx;
647cbc82b17SPeter P Waskiewicz Jr 			if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) {
648cbc82b17SPeter P Waskiewicz Jr 				c->x86_cache_max_rmid = ecx;
649cbc82b17SPeter P Waskiewicz Jr 				c->x86_cache_occ_scale = ebx;
650cbc82b17SPeter P Waskiewicz Jr 			}
651cbc82b17SPeter P Waskiewicz Jr 		} else {
652cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_max_rmid = -1;
653cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_occ_scale = -1;
654cbc82b17SPeter P Waskiewicz Jr 		}
655cbc82b17SPeter P Waskiewicz Jr 	}
656cbc82b17SPeter P Waskiewicz Jr 
657093af8d7SYinghai Lu 	/* AMD-defined flags: level 0x80000001 */
658093af8d7SYinghai Lu 	xlvl = cpuid_eax(0x80000000);
6593da99c97SYinghai Lu 	c->extended_cpuid_level = xlvl;
6600f3fa48aSIngo Molnar 
661093af8d7SYinghai Lu 	if ((xlvl & 0xffff0000) == 0x80000000) {
662093af8d7SYinghai Lu 		if (xlvl >= 0x80000001) {
663093af8d7SYinghai Lu 			c->x86_capability[1] = cpuid_edx(0x80000001);
664093af8d7SYinghai Lu 			c->x86_capability[6] = cpuid_ecx(0x80000001);
665093af8d7SYinghai Lu 		}
666093af8d7SYinghai Lu 	}
667093af8d7SYinghai Lu 
6685122c890SYinghai Lu 	if (c->extended_cpuid_level >= 0x80000008) {
6695122c890SYinghai Lu 		u32 eax = cpuid_eax(0x80000008);
6705122c890SYinghai Lu 
6715122c890SYinghai Lu 		c->x86_virt_bits = (eax >> 8) & 0xff;
6725122c890SYinghai Lu 		c->x86_phys_bits = eax & 0xff;
6735122c890SYinghai Lu 	}
67413c6c532SJan Beulich #ifdef CONFIG_X86_32
67513c6c532SJan Beulich 	else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
67613c6c532SJan Beulich 		c->x86_phys_bits = 36;
6775122c890SYinghai Lu #endif
678e3224234SYinghai Lu 
679e3224234SYinghai Lu 	if (c->extended_cpuid_level >= 0x80000007)
680e3224234SYinghai Lu 		c->x86_power = cpuid_edx(0x80000007);
681e3224234SYinghai Lu 
6821dedefd1SJacob Pan 	init_scattered_cpuid_features(c);
683093af8d7SYinghai Lu }
684093af8d7SYinghai Lu 
685148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
686aef93c8bSYinghai Lu {
687aef93c8bSYinghai Lu #ifdef CONFIG_X86_32
688aef93c8bSYinghai Lu 	int i;
689aef93c8bSYinghai Lu 
690aef93c8bSYinghai Lu 	/*
691aef93c8bSYinghai Lu 	 * First of all, decide if this is a 486 or higher
692aef93c8bSYinghai Lu 	 * It's a 486 if we can modify the AC flag
693aef93c8bSYinghai Lu 	 */
694aef93c8bSYinghai Lu 	if (flag_is_changeable_p(X86_EFLAGS_AC))
695aef93c8bSYinghai Lu 		c->x86 = 4;
696aef93c8bSYinghai Lu 	else
697aef93c8bSYinghai Lu 		c->x86 = 3;
698aef93c8bSYinghai Lu 
699aef93c8bSYinghai Lu 	for (i = 0; i < X86_VENDOR_NUM; i++)
700aef93c8bSYinghai Lu 		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
701aef93c8bSYinghai Lu 			c->x86_vendor_id[0] = 0;
702aef93c8bSYinghai Lu 			cpu_devs[i]->c_identify(c);
703aef93c8bSYinghai Lu 			if (c->x86_vendor_id[0]) {
704aef93c8bSYinghai Lu 				get_cpu_vendor(c);
705aef93c8bSYinghai Lu 				break;
706aef93c8bSYinghai Lu 			}
707aef93c8bSYinghai Lu 		}
708aef93c8bSYinghai Lu #endif
709093af8d7SYinghai Lu }
710f7627e25SThomas Gleixner 
71134048c9eSPaolo Ciarrocchi /*
71234048c9eSPaolo Ciarrocchi  * Do minimum CPU detection early.
71334048c9eSPaolo Ciarrocchi  * Fields really needed: vendor, cpuid_level, family, model, mask,
71434048c9eSPaolo Ciarrocchi  * cache alignment.
71534048c9eSPaolo Ciarrocchi  * The others are not touched to avoid unwanted side effects.
71634048c9eSPaolo Ciarrocchi  *
71734048c9eSPaolo Ciarrocchi  * WARNING: this function is only called on the BP.  Don't add code here
71834048c9eSPaolo Ciarrocchi  * that is supposed to run on all CPUs.
71934048c9eSPaolo Ciarrocchi  */
7203da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c)
721f7627e25SThomas Gleixner {
7226627d242SYinghai Lu #ifdef CONFIG_X86_64
7236627d242SYinghai Lu 	c->x86_clflush_size = 64;
72413c6c532SJan Beulich 	c->x86_phys_bits = 36;
72513c6c532SJan Beulich 	c->x86_virt_bits = 48;
7266627d242SYinghai Lu #else
727d4387bd3SHuang, Ying 	c->x86_clflush_size = 32;
72813c6c532SJan Beulich 	c->x86_phys_bits = 32;
72913c6c532SJan Beulich 	c->x86_virt_bits = 32;
7306627d242SYinghai Lu #endif
7310a488a53SYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
732f7627e25SThomas Gleixner 
7333da99c97SYinghai Lu 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
7340a488a53SYinghai Lu 	c->extended_cpuid_level = 0;
7350a488a53SYinghai Lu 
736aef93c8bSYinghai Lu 	if (!have_cpuid_p())
737aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
738aef93c8bSYinghai Lu 
739aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
740f7627e25SThomas Gleixner 	if (!have_cpuid_p())
741f7627e25SThomas Gleixner 		return;
742f7627e25SThomas Gleixner 
743f7627e25SThomas Gleixner 	cpu_detect(c);
7443da99c97SYinghai Lu 	get_cpu_vendor(c);
7453da99c97SYinghai Lu 	get_cpu_cap(c);
74612cf105cSKrzysztof Helt 
74710a434fcSYinghai Lu 	if (this_cpu->c_early_init)
74810a434fcSYinghai Lu 		this_cpu->c_early_init(c);
7493da99c97SYinghai Lu 
750f6e9456cSRobert Richter 	c->cpu_index = 0;
751b38b0665SH. Peter Anvin 	filter_cpuid_features(c, false);
752de5397adSFenghua Yu 
753a110b5ecSBorislav Petkov 	if (this_cpu->c_bsp_init)
754a110b5ecSBorislav Petkov 		this_cpu->c_bsp_init(c);
755c3b83598SBorislav Petkov 
756c3b83598SBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
757db52ef74SIngo Molnar 	fpu__init_system(c);
758f7627e25SThomas Gleixner }
759f7627e25SThomas Gleixner 
7609d31d35bSYinghai Lu void __init early_cpu_init(void)
7619d31d35bSYinghai Lu {
76202dde8b4SJan Beulich 	const struct cpu_dev *const *cdev;
76310a434fcSYinghai Lu 	int count = 0;
7649d31d35bSYinghai Lu 
765ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
7669766cdbcSJaswinder Singh Rajput 	printk(KERN_INFO "KERNEL supported cpus:\n");
76731c997caSIngo Molnar #endif
76831c997caSIngo Molnar 
76910a434fcSYinghai Lu 	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
77002dde8b4SJan Beulich 		const struct cpu_dev *cpudev = *cdev;
7719d31d35bSYinghai Lu 
77210a434fcSYinghai Lu 		if (count >= X86_VENDOR_NUM)
77310a434fcSYinghai Lu 			break;
77410a434fcSYinghai Lu 		cpu_devs[count] = cpudev;
77510a434fcSYinghai Lu 		count++;
77610a434fcSYinghai Lu 
777ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
77831c997caSIngo Molnar 		{
77931c997caSIngo Molnar 			unsigned int j;
78031c997caSIngo Molnar 
78110a434fcSYinghai Lu 			for (j = 0; j < 2; j++) {
78210a434fcSYinghai Lu 				if (!cpudev->c_ident[j])
78310a434fcSYinghai Lu 					continue;
7849766cdbcSJaswinder Singh Rajput 				printk(KERN_INFO "  %s %s\n", cpudev->c_vendor,
78510a434fcSYinghai Lu 					cpudev->c_ident[j]);
78610a434fcSYinghai Lu 			}
78710a434fcSYinghai Lu 		}
7880388423dSDave Jones #endif
78931c997caSIngo Molnar 	}
7909d31d35bSYinghai Lu 	early_identify_cpu(&boot_cpu_data);
791f7627e25SThomas Gleixner }
792f7627e25SThomas Gleixner 
793b6734c35SH. Peter Anvin /*
794366d4a43SBorislav Petkov  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
795366d4a43SBorislav Petkov  * unfortunately, that's not true in practice because of early VIA
796366d4a43SBorislav Petkov  * chips and (more importantly) broken virtualizers that are not easy
797366d4a43SBorislav Petkov  * to detect. In the latter case it doesn't even *fail* reliably, so
798366d4a43SBorislav Petkov  * probing for it doesn't even work. Disable it completely on 32-bit
799ba0593bfSH. Peter Anvin  * unless we can find a reliable way to detect all the broken cases.
800366d4a43SBorislav Petkov  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
801b6734c35SH. Peter Anvin  */
802148f9bb8SPaul Gortmaker static void detect_nopl(struct cpuinfo_x86 *c)
803b6734c35SH. Peter Anvin {
804366d4a43SBorislav Petkov #ifdef CONFIG_X86_32
805b6734c35SH. Peter Anvin 	clear_cpu_cap(c, X86_FEATURE_NOPL);
806366d4a43SBorislav Petkov #else
807366d4a43SBorislav Petkov 	set_cpu_cap(c, X86_FEATURE_NOPL);
808366d4a43SBorislav Petkov #endif
809f7627e25SThomas Gleixner }
810f7627e25SThomas Gleixner 
811148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c)
812f7627e25SThomas Gleixner {
8133da99c97SYinghai Lu 	c->extended_cpuid_level = 0;
814f7627e25SThomas Gleixner 
815aef93c8bSYinghai Lu 	if (!have_cpuid_p())
816aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
817f7627e25SThomas Gleixner 
818aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
819a9853dd6SIngo Molnar 	if (!have_cpuid_p())
820aef93c8bSYinghai Lu 		return;
821aef93c8bSYinghai Lu 
8223da99c97SYinghai Lu 	cpu_detect(c);
8233da99c97SYinghai Lu 
8243da99c97SYinghai Lu 	get_cpu_vendor(c);
8253da99c97SYinghai Lu 
8263da99c97SYinghai Lu 	get_cpu_cap(c);
8273da99c97SYinghai Lu 
828f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
8293da99c97SYinghai Lu 		c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
830b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32
831c8e56d20SBorislav Petkov # ifdef CONFIG_SMP
832cb8cc442SIngo Molnar 		c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
833f7627e25SThomas Gleixner # else
83401aaea1aSYinghai Lu 		c->apicid = c->initial_apicid;
835f7627e25SThomas Gleixner # endif
836b89d3b3eSYinghai Lu #endif
837b89d3b3eSYinghai Lu 		c->phys_proc_id = c->initial_apicid;
838f7627e25SThomas Gleixner 	}
839f7627e25SThomas Gleixner 
840f7627e25SThomas Gleixner 	get_model_name(c); /* Default name */
841f7627e25SThomas Gleixner 
842b6734c35SH. Peter Anvin 	detect_nopl(c);
843f7627e25SThomas Gleixner }
844f7627e25SThomas Gleixner 
845cbc82b17SPeter P Waskiewicz Jr static void x86_init_cache_qos(struct cpuinfo_x86 *c)
846cbc82b17SPeter P Waskiewicz Jr {
847cbc82b17SPeter P Waskiewicz Jr 	/*
848cbc82b17SPeter P Waskiewicz Jr 	 * The heavy lifting of max_rmid and cache_occ_scale are handled
849cbc82b17SPeter P Waskiewicz Jr 	 * in get_cpu_cap().  Here we just set the max_rmid for the boot_cpu
850cbc82b17SPeter P Waskiewicz Jr 	 * in case CQM bits really aren't there in this CPU.
851cbc82b17SPeter P Waskiewicz Jr 	 */
852cbc82b17SPeter P Waskiewicz Jr 	if (c != &boot_cpu_data) {
853cbc82b17SPeter P Waskiewicz Jr 		boot_cpu_data.x86_cache_max_rmid =
854cbc82b17SPeter P Waskiewicz Jr 			min(boot_cpu_data.x86_cache_max_rmid,
855cbc82b17SPeter P Waskiewicz Jr 			    c->x86_cache_max_rmid);
856cbc82b17SPeter P Waskiewicz Jr 	}
857cbc82b17SPeter P Waskiewicz Jr }
858cbc82b17SPeter P Waskiewicz Jr 
859f7627e25SThomas Gleixner /*
860f7627e25SThomas Gleixner  * This does the hard work of actually picking apart the CPU stuff...
861f7627e25SThomas Gleixner  */
862148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c)
863f7627e25SThomas Gleixner {
864f7627e25SThomas Gleixner 	int i;
865f7627e25SThomas Gleixner 
866f7627e25SThomas Gleixner 	c->loops_per_jiffy = loops_per_jiffy;
867f7627e25SThomas Gleixner 	c->x86_cache_size = -1;
868f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
869f7627e25SThomas Gleixner 	c->x86_model = c->x86_mask = 0;	/* So far unknown... */
870f7627e25SThomas Gleixner 	c->x86_vendor_id[0] = '\0'; /* Unset */
871f7627e25SThomas Gleixner 	c->x86_model_id[0] = '\0';  /* Unset */
872f7627e25SThomas Gleixner 	c->x86_max_cores = 1;
873102bbe3aSYinghai Lu 	c->x86_coreid_bits = 0;
87411fdd252SYinghai Lu #ifdef CONFIG_X86_64
875102bbe3aSYinghai Lu 	c->x86_clflush_size = 64;
87613c6c532SJan Beulich 	c->x86_phys_bits = 36;
87713c6c532SJan Beulich 	c->x86_virt_bits = 48;
878102bbe3aSYinghai Lu #else
879102bbe3aSYinghai Lu 	c->cpuid_level = -1;	/* CPUID not detected */
880f7627e25SThomas Gleixner 	c->x86_clflush_size = 32;
88113c6c532SJan Beulich 	c->x86_phys_bits = 32;
88213c6c532SJan Beulich 	c->x86_virt_bits = 32;
883102bbe3aSYinghai Lu #endif
884102bbe3aSYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
885f7627e25SThomas Gleixner 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
886f7627e25SThomas Gleixner 
887f7627e25SThomas Gleixner 	generic_identify(c);
888f7627e25SThomas Gleixner 
8893898534dSAndi Kleen 	if (this_cpu->c_identify)
890f7627e25SThomas Gleixner 		this_cpu->c_identify(c);
891f7627e25SThomas Gleixner 
8922759c328SYinghai Lu 	/* Clear/Set all flags overriden by options, after probe */
8932759c328SYinghai Lu 	for (i = 0; i < NCAPINTS; i++) {
8942759c328SYinghai Lu 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
8952759c328SYinghai Lu 		c->x86_capability[i] |= cpu_caps_set[i];
8962759c328SYinghai Lu 	}
8972759c328SYinghai Lu 
898102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
899cb8cc442SIngo Molnar 	c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
900102bbe3aSYinghai Lu #endif
901102bbe3aSYinghai Lu 
902f7627e25SThomas Gleixner 	/*
903f7627e25SThomas Gleixner 	 * Vendor-specific initialization.  In this section we
904f7627e25SThomas Gleixner 	 * canonicalize the feature flags, meaning if there are
905f7627e25SThomas Gleixner 	 * features a certain CPU supports which CPUID doesn't
906f7627e25SThomas Gleixner 	 * tell us, CPUID claiming incorrect flags, or other bugs,
907f7627e25SThomas Gleixner 	 * we handle them here.
908f7627e25SThomas Gleixner 	 *
909f7627e25SThomas Gleixner 	 * At the end of this section, c->x86_capability better
910f7627e25SThomas Gleixner 	 * indicate the features this CPU genuinely supports!
911f7627e25SThomas Gleixner 	 */
912f7627e25SThomas Gleixner 	if (this_cpu->c_init)
913f7627e25SThomas Gleixner 		this_cpu->c_init(c);
914f7627e25SThomas Gleixner 
915f7627e25SThomas Gleixner 	/* Disable the PN if appropriate */
916f7627e25SThomas Gleixner 	squash_the_stupid_serial_number(c);
917f7627e25SThomas Gleixner 
918b2cc2a07SH. Peter Anvin 	/* Set up SMEP/SMAP */
919b2cc2a07SH. Peter Anvin 	setup_smep(c);
920b2cc2a07SH. Peter Anvin 	setup_smap(c);
921b2cc2a07SH. Peter Anvin 
922f7627e25SThomas Gleixner 	/*
9230f3fa48aSIngo Molnar 	 * The vendor-specific functions might have changed features.
9240f3fa48aSIngo Molnar 	 * Now we do "generic changes."
925f7627e25SThomas Gleixner 	 */
926f7627e25SThomas Gleixner 
927b38b0665SH. Peter Anvin 	/* Filter out anything that depends on CPUID levels we don't have */
928b38b0665SH. Peter Anvin 	filter_cpuid_features(c, true);
929b38b0665SH. Peter Anvin 
930f7627e25SThomas Gleixner 	/* If the model name is still unset, do table lookup. */
931f7627e25SThomas Gleixner 	if (!c->x86_model_id[0]) {
93202dde8b4SJan Beulich 		const char *p;
933f7627e25SThomas Gleixner 		p = table_lookup_model(c);
934f7627e25SThomas Gleixner 		if (p)
935f7627e25SThomas Gleixner 			strcpy(c->x86_model_id, p);
936f7627e25SThomas Gleixner 		else
937f7627e25SThomas Gleixner 			/* Last resort... */
938f7627e25SThomas Gleixner 			sprintf(c->x86_model_id, "%02x/%02x",
939f7627e25SThomas Gleixner 				c->x86, c->x86_model);
940f7627e25SThomas Gleixner 	}
941f7627e25SThomas Gleixner 
942102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
943102bbe3aSYinghai Lu 	detect_ht(c);
944102bbe3aSYinghai Lu #endif
945102bbe3aSYinghai Lu 
94688b094fbSAlok Kataria 	init_hypervisor(c);
94749d859d7SH. Peter Anvin 	x86_init_rdrand(c);
948cbc82b17SPeter P Waskiewicz Jr 	x86_init_cache_qos(c);
9493e0c3737SYinghai Lu 
9503e0c3737SYinghai Lu 	/*
9513e0c3737SYinghai Lu 	 * Clear/Set all flags overriden by options, need do it
9523e0c3737SYinghai Lu 	 * before following smp all cpus cap AND.
9533e0c3737SYinghai Lu 	 */
9543e0c3737SYinghai Lu 	for (i = 0; i < NCAPINTS; i++) {
9553e0c3737SYinghai Lu 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
9563e0c3737SYinghai Lu 		c->x86_capability[i] |= cpu_caps_set[i];
9573e0c3737SYinghai Lu 	}
9583e0c3737SYinghai Lu 
959f7627e25SThomas Gleixner 	/*
960f7627e25SThomas Gleixner 	 * On SMP, boot_cpu_data holds the common feature set between
961f7627e25SThomas Gleixner 	 * all CPUs; so make sure that we indicate which features are
962f7627e25SThomas Gleixner 	 * common between the CPUs.  The first time this routine gets
963f7627e25SThomas Gleixner 	 * executed, c == &boot_cpu_data.
964f7627e25SThomas Gleixner 	 */
965f7627e25SThomas Gleixner 	if (c != &boot_cpu_data) {
966f7627e25SThomas Gleixner 		/* AND the already accumulated flags with these */
967f7627e25SThomas Gleixner 		for (i = 0; i < NCAPINTS; i++)
968f7627e25SThomas Gleixner 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
96965fc985bSBorislav Petkov 
97065fc985bSBorislav Petkov 		/* OR, i.e. replicate the bug flags */
97165fc985bSBorislav Petkov 		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
97265fc985bSBorislav Petkov 			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
973f7627e25SThomas Gleixner 	}
974f7627e25SThomas Gleixner 
975f7627e25SThomas Gleixner 	/* Init Machine Check Exception if available. */
9765e09954aSBorislav Petkov 	mcheck_cpu_init(c);
97730d432dfSAndi Kleen 
97830d432dfSAndi Kleen 	select_idle_routine(c);
979102bbe3aSYinghai Lu 
980de2d9445STejun Heo #ifdef CONFIG_NUMA
981102bbe3aSYinghai Lu 	numa_add_cpu(smp_processor_id());
982102bbe3aSYinghai Lu #endif
983f7627e25SThomas Gleixner }
984f7627e25SThomas Gleixner 
9858b6c0ab1SIngo Molnar /*
9868b6c0ab1SIngo Molnar  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
9878b6c0ab1SIngo Molnar  * on 32-bit kernels:
9888b6c0ab1SIngo Molnar  */
989cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32
990cfda7bb9SAndy Lutomirski void enable_sep_cpu(void)
991cfda7bb9SAndy Lutomirski {
9928b6c0ab1SIngo Molnar 	struct tss_struct *tss;
9938b6c0ab1SIngo Molnar 	int cpu;
994cfda7bb9SAndy Lutomirski 
9958b6c0ab1SIngo Molnar 	cpu = get_cpu();
9968b6c0ab1SIngo Molnar 	tss = &per_cpu(cpu_tss, cpu);
9978b6c0ab1SIngo Molnar 
9988b6c0ab1SIngo Molnar 	if (!boot_cpu_has(X86_FEATURE_SEP))
9998b6c0ab1SIngo Molnar 		goto out;
10008b6c0ab1SIngo Molnar 
10018b6c0ab1SIngo Molnar 	/*
1002cf9328ccSAndy Lutomirski 	 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1003cf9328ccSAndy Lutomirski 	 * see the big comment in struct x86_hw_tss's definition.
10048b6c0ab1SIngo Molnar 	 */
1005cfda7bb9SAndy Lutomirski 
1006cfda7bb9SAndy Lutomirski 	tss->x86_tss.ss1 = __KERNEL_CS;
10078b6c0ab1SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
10088b6c0ab1SIngo Molnar 
1009cf9328ccSAndy Lutomirski 	wrmsr(MSR_IA32_SYSENTER_ESP,
1010cf9328ccSAndy Lutomirski 	      (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
1011cf9328ccSAndy Lutomirski 	      0);
10128b6c0ab1SIngo Molnar 
10134c8cd0c5SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
10148b6c0ab1SIngo Molnar 
10158b6c0ab1SIngo Molnar out:
1016cfda7bb9SAndy Lutomirski 	put_cpu();
1017cfda7bb9SAndy Lutomirski }
1018e04d645fSGlauber Costa #endif
1019e04d645fSGlauber Costa 
1020f7627e25SThomas Gleixner void __init identify_boot_cpu(void)
1021f7627e25SThomas Gleixner {
1022f7627e25SThomas Gleixner 	identify_cpu(&boot_cpu_data);
102302c68a02SLen Brown 	init_amd_e400_c1e_mask();
1024102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1025f7627e25SThomas Gleixner 	sysenter_setup();
1026f7627e25SThomas Gleixner 	enable_sep_cpu();
1027102bbe3aSYinghai Lu #endif
1028e0ba94f1SAlex Shi 	cpu_detect_tlb(&boot_cpu_data);
1029f7627e25SThomas Gleixner }
1030f7627e25SThomas Gleixner 
1031148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c)
1032f7627e25SThomas Gleixner {
1033f7627e25SThomas Gleixner 	BUG_ON(c == &boot_cpu_data);
1034f7627e25SThomas Gleixner 	identify_cpu(c);
1035102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1036f7627e25SThomas Gleixner 	enable_sep_cpu();
1037102bbe3aSYinghai Lu #endif
1038f7627e25SThomas Gleixner 	mtrr_ap_init();
1039f7627e25SThomas Gleixner }
1040f7627e25SThomas Gleixner 
1041a0854a46SYinghai Lu struct msr_range {
1042a0854a46SYinghai Lu 	unsigned	min;
1043a0854a46SYinghai Lu 	unsigned	max;
1044a0854a46SYinghai Lu };
1045a0854a46SYinghai Lu 
1046148f9bb8SPaul Gortmaker static const struct msr_range msr_range_array[] = {
1047a0854a46SYinghai Lu 	{ 0x00000000, 0x00000418},
1048a0854a46SYinghai Lu 	{ 0xc0000000, 0xc000040b},
1049a0854a46SYinghai Lu 	{ 0xc0010000, 0xc0010142},
1050a0854a46SYinghai Lu 	{ 0xc0011000, 0xc001103b},
1051a0854a46SYinghai Lu };
1052a0854a46SYinghai Lu 
1053148f9bb8SPaul Gortmaker static void __print_cpu_msr(void)
1054f7627e25SThomas Gleixner {
10550f3fa48aSIngo Molnar 	unsigned index_min, index_max;
1056a0854a46SYinghai Lu 	unsigned index;
1057a0854a46SYinghai Lu 	u64 val;
1058a0854a46SYinghai Lu 	int i;
1059f7627e25SThomas Gleixner 
1060a0854a46SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
1061a0854a46SYinghai Lu 		index_min = msr_range_array[i].min;
1062a0854a46SYinghai Lu 		index_max = msr_range_array[i].max;
10630f3fa48aSIngo Molnar 
1064a0854a46SYinghai Lu 		for (index = index_min; index < index_max; index++) {
1065ecd431d9SBorislav Petkov 			if (rdmsrl_safe(index, &val))
1066a0854a46SYinghai Lu 				continue;
1067a0854a46SYinghai Lu 			printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
1068f7627e25SThomas Gleixner 		}
1069f7627e25SThomas Gleixner 	}
1070a0854a46SYinghai Lu }
1071a0854a46SYinghai Lu 
1072148f9bb8SPaul Gortmaker static int show_msr;
10730f3fa48aSIngo Molnar 
1074a0854a46SYinghai Lu static __init int setup_show_msr(char *arg)
1075a0854a46SYinghai Lu {
1076a0854a46SYinghai Lu 	int num;
1077a0854a46SYinghai Lu 
1078a0854a46SYinghai Lu 	get_option(&arg, &num);
1079a0854a46SYinghai Lu 
1080a0854a46SYinghai Lu 	if (num > 0)
1081a0854a46SYinghai Lu 		show_msr = num;
1082a0854a46SYinghai Lu 	return 1;
1083a0854a46SYinghai Lu }
1084a0854a46SYinghai Lu __setup("show_msr=", setup_show_msr);
1085f7627e25SThomas Gleixner 
1086191679fdSAndi Kleen static __init int setup_noclflush(char *arg)
1087191679fdSAndi Kleen {
1088840d2830SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1089da4aaa7dSH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1090191679fdSAndi Kleen 	return 1;
1091191679fdSAndi Kleen }
1092191679fdSAndi Kleen __setup("noclflush", setup_noclflush);
1093191679fdSAndi Kleen 
1094148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c)
1095f7627e25SThomas Gleixner {
109602dde8b4SJan Beulich 	const char *vendor = NULL;
1097f7627e25SThomas Gleixner 
10980f3fa48aSIngo Molnar 	if (c->x86_vendor < X86_VENDOR_NUM) {
1099f7627e25SThomas Gleixner 		vendor = this_cpu->c_vendor;
11000f3fa48aSIngo Molnar 	} else {
11010f3fa48aSIngo Molnar 		if (c->cpuid_level >= 0)
1102f7627e25SThomas Gleixner 			vendor = c->x86_vendor_id;
11030f3fa48aSIngo Molnar 	}
1104f7627e25SThomas Gleixner 
1105bd32a8cfSYinghai Lu 	if (vendor && !strstr(c->x86_model_id, vendor))
11069d31d35bSYinghai Lu 		printk(KERN_CONT "%s ", vendor);
1107f7627e25SThomas Gleixner 
11089d31d35bSYinghai Lu 	if (c->x86_model_id[0])
1109adafb98dSPrarit Bhargava 		printk(KERN_CONT "%s", c->x86_model_id);
1110f7627e25SThomas Gleixner 	else
11119d31d35bSYinghai Lu 		printk(KERN_CONT "%d86", c->x86);
1112f7627e25SThomas Gleixner 
1113924e101aSBorislav Petkov 	printk(KERN_CONT " (fam: %02x, model: %02x", c->x86, c->x86_model);
1114924e101aSBorislav Petkov 
1115f7627e25SThomas Gleixner 	if (c->x86_mask || c->cpuid_level >= 0)
1116924e101aSBorislav Petkov 		printk(KERN_CONT ", stepping: %02x)\n", c->x86_mask);
1117f7627e25SThomas Gleixner 	else
1118924e101aSBorislav Petkov 		printk(KERN_CONT ")\n");
1119a0854a46SYinghai Lu 
11200b8b8078SYinghai Lu 	print_cpu_msr(c);
112121c3fcf3SYinghai Lu }
112221c3fcf3SYinghai Lu 
1123148f9bb8SPaul Gortmaker void print_cpu_msr(struct cpuinfo_x86 *c)
112421c3fcf3SYinghai Lu {
1125a0854a46SYinghai Lu 	if (c->cpu_index < show_msr)
112621c3fcf3SYinghai Lu 		__print_cpu_msr();
1127f7627e25SThomas Gleixner }
1128f7627e25SThomas Gleixner 
1129ac72e788SAndi Kleen static __init int setup_disablecpuid(char *arg)
1130ac72e788SAndi Kleen {
1131ac72e788SAndi Kleen 	int bit;
11320f3fa48aSIngo Molnar 
1133ac72e788SAndi Kleen 	if (get_option(&arg, &bit) && bit < NCAPINTS*32)
1134ac72e788SAndi Kleen 		setup_clear_cpu_cap(bit);
1135ac72e788SAndi Kleen 	else
1136ac72e788SAndi Kleen 		return 0;
11370f3fa48aSIngo Molnar 
1138ac72e788SAndi Kleen 	return 1;
1139ac72e788SAndi Kleen }
1140ac72e788SAndi Kleen __setup("clearcpuid=", setup_disablecpuid);
1141ac72e788SAndi Kleen 
1142d5494d4fSYinghai Lu #ifdef CONFIG_X86_64
11439ff80942SCyrill Gorcunov struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
1144629f4f9dSSeiji Aguchi struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
1145629f4f9dSSeiji Aguchi 				    (unsigned long) debug_idt_table };
1146d5494d4fSYinghai Lu 
1147947e76cdSBrian Gerst DEFINE_PER_CPU_FIRST(union irq_stack_union,
1148277d5b40SAndi Kleen 		     irq_stack_union) __aligned(PAGE_SIZE) __visible;
11490f3fa48aSIngo Molnar 
1150bdf977b3STejun Heo /*
1151a7fcf28dSAndy Lutomirski  * The following percpu variables are hot.  Align current_task to
1152a7fcf28dSAndy Lutomirski  * cacheline size such that they fall in the same cacheline.
1153bdf977b3STejun Heo  */
1154bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1155bdf977b3STejun Heo 	&init_task;
1156bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1157d5494d4fSYinghai Lu 
1158bdf977b3STejun Heo DEFINE_PER_CPU(char *, irq_stack_ptr) =
1159bdf977b3STejun Heo 	init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
1160bdf977b3STejun Heo 
1161277d5b40SAndi Kleen DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
1162d5494d4fSYinghai Lu 
1163c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1164c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1165c2daa3beSPeter Zijlstra 
11660f3fa48aSIngo Molnar /*
11670f3fa48aSIngo Molnar  * Special IST stacks which the CPU switches to when it calls
11680f3fa48aSIngo Molnar  * an IST-marked descriptor entry. Up to 7 stacks (hardware
11690f3fa48aSIngo Molnar  * limit), all of them are 4K, except the debug stack which
11700f3fa48aSIngo Molnar  * is 8K.
11710f3fa48aSIngo Molnar  */
11720f3fa48aSIngo Molnar static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
11730f3fa48aSIngo Molnar 	  [0 ... N_EXCEPTION_STACKS - 1]	= EXCEPTION_STKSZ,
11740f3fa48aSIngo Molnar 	  [DEBUG_STACK - 1]			= DEBUG_STKSZ
11750f3fa48aSIngo Molnar };
11760f3fa48aSIngo Molnar 
117792d65b23SBrian Gerst static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
11783e352aa8STejun Heo 	[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
1179d5494d4fSYinghai Lu 
1180d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */
1181d5494d4fSYinghai Lu void syscall_init(void)
1182d5494d4fSYinghai Lu {
1183d5494d4fSYinghai Lu 	/*
1184d5494d4fSYinghai Lu 	 * LSTAR and STAR live in a bit strange symbiosis.
1185d5494d4fSYinghai Lu 	 * They both write to the same internal register. STAR allows to
1186d5494d4fSYinghai Lu 	 * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
1187d5494d4fSYinghai Lu 	 */
1188d5494d4fSYinghai Lu 	wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32);
1189b2502b41SIngo Molnar 	wrmsrl(MSR_LSTAR, entry_SYSCALL_64);
1190d56fe4bfSIngo Molnar 
1191d56fe4bfSIngo Molnar #ifdef CONFIG_IA32_EMULATION
11922cd23553SIngo Molnar 	wrmsrl(MSR_CSTAR, entry_SYSCALL_compat);
1193a76c7f46SDenys Vlasenko 	/*
1194487d1edbSDenys Vlasenko 	 * This only works on Intel CPUs.
1195487d1edbSDenys Vlasenko 	 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1196487d1edbSDenys Vlasenko 	 * This does not cause SYSENTER to jump to the wrong location, because
1197487d1edbSDenys Vlasenko 	 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1198a76c7f46SDenys Vlasenko 	 */
1199a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1200a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
12014c8cd0c5SIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
1202d56fe4bfSIngo Molnar #else
1203d56fe4bfSIngo Molnar 	wrmsrl(MSR_CSTAR, ignore_sysret);
12046b51311cSBorislav Petkov 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1205d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1206d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1207d5494d4fSYinghai Lu #endif
1208d5494d4fSYinghai Lu 
1209d5494d4fSYinghai Lu 	/* Flags to clear on syscall */
1210d5494d4fSYinghai Lu 	wrmsrl(MSR_SYSCALL_MASK,
121163bcff2aSH. Peter Anvin 	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
12128c7aa698SAndy Lutomirski 	       X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1213d5494d4fSYinghai Lu }
1214d5494d4fSYinghai Lu 
1215d5494d4fSYinghai Lu /*
1216d5494d4fSYinghai Lu  * Copies of the original ist values from the tss are only accessed during
1217d5494d4fSYinghai Lu  * debugging, no special alignment required.
1218d5494d4fSYinghai Lu  */
1219d5494d4fSYinghai Lu DEFINE_PER_CPU(struct orig_ist, orig_ist);
1220d5494d4fSYinghai Lu 
1221228bdaa9SSteven Rostedt static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
122242181186SSteven Rostedt DEFINE_PER_CPU(int, debug_stack_usage);
1223228bdaa9SSteven Rostedt 
1224228bdaa9SSteven Rostedt int is_debug_stack(unsigned long addr)
1225228bdaa9SSteven Rostedt {
122689cbc767SChristoph Lameter 	return __this_cpu_read(debug_stack_usage) ||
122789cbc767SChristoph Lameter 		(addr <= __this_cpu_read(debug_stack_addr) &&
122889cbc767SChristoph Lameter 		 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
1229228bdaa9SSteven Rostedt }
12300f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(is_debug_stack);
1231228bdaa9SSteven Rostedt 
1232629f4f9dSSeiji Aguchi DEFINE_PER_CPU(u32, debug_idt_ctr);
1233f8988175SSteven Rostedt 
1234228bdaa9SSteven Rostedt void debug_stack_set_zero(void)
1235228bdaa9SSteven Rostedt {
1236629f4f9dSSeiji Aguchi 	this_cpu_inc(debug_idt_ctr);
1237629f4f9dSSeiji Aguchi 	load_current_idt();
1238228bdaa9SSteven Rostedt }
12390f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_set_zero);
1240228bdaa9SSteven Rostedt 
1241228bdaa9SSteven Rostedt void debug_stack_reset(void)
1242228bdaa9SSteven Rostedt {
1243629f4f9dSSeiji Aguchi 	if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
1244f8988175SSteven Rostedt 		return;
1245629f4f9dSSeiji Aguchi 	if (this_cpu_dec_return(debug_idt_ctr) == 0)
1246629f4f9dSSeiji Aguchi 		load_current_idt();
1247228bdaa9SSteven Rostedt }
12480f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_reset);
1249228bdaa9SSteven Rostedt 
12500f3fa48aSIngo Molnar #else	/* CONFIG_X86_64 */
1251d5494d4fSYinghai Lu 
1252bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1253bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1254c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1255c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1256bdf977b3STejun Heo 
1257a7fcf28dSAndy Lutomirski /*
1258a7fcf28dSAndy Lutomirski  * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1259a7fcf28dSAndy Lutomirski  * the top of the kernel stack.  Use an extra percpu variable to track the
1260a7fcf28dSAndy Lutomirski  * top of the kernel stack directly.
1261a7fcf28dSAndy Lutomirski  */
1262a7fcf28dSAndy Lutomirski DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1263a7fcf28dSAndy Lutomirski 	(unsigned long)&init_thread_union + THREAD_SIZE;
1264a7fcf28dSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1265a7fcf28dSAndy Lutomirski 
126660a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR
126753f82452SJeremy Fitzhardinge DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
126860a5317fSTejun Heo #endif
126960a5317fSTejun Heo 
12700f3fa48aSIngo Molnar #endif	/* CONFIG_X86_64 */
1271f7627e25SThomas Gleixner 
1272f7627e25SThomas Gleixner /*
12739766cdbcSJaswinder Singh Rajput  * Clear all 6 debug registers:
12749766cdbcSJaswinder Singh Rajput  */
12759766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void)
12769766cdbcSJaswinder Singh Rajput {
12779766cdbcSJaswinder Singh Rajput 	int i;
12789766cdbcSJaswinder Singh Rajput 
12799766cdbcSJaswinder Singh Rajput 	for (i = 0; i < 8; i++) {
12809766cdbcSJaswinder Singh Rajput 		/* Ignore db4, db5 */
12819766cdbcSJaswinder Singh Rajput 		if ((i == 4) || (i == 5))
12829766cdbcSJaswinder Singh Rajput 			continue;
12839766cdbcSJaswinder Singh Rajput 
12849766cdbcSJaswinder Singh Rajput 		set_debugreg(0, i);
12859766cdbcSJaswinder Singh Rajput 	}
12869766cdbcSJaswinder Singh Rajput }
1287f7627e25SThomas Gleixner 
12880bb9fef9SJason Wessel #ifdef CONFIG_KGDB
12890bb9fef9SJason Wessel /*
12900bb9fef9SJason Wessel  * Restore debug regs if using kgdbwait and you have a kernel debugger
12910bb9fef9SJason Wessel  * connection established.
12920bb9fef9SJason Wessel  */
12930bb9fef9SJason Wessel static void dbg_restore_debug_regs(void)
12940bb9fef9SJason Wessel {
12950bb9fef9SJason Wessel 	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
12960bb9fef9SJason Wessel 		arch_kgdb_ops.correct_hw_break();
12970bb9fef9SJason Wessel }
12980bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */
12990bb9fef9SJason Wessel #define dbg_restore_debug_regs()
13000bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */
13010bb9fef9SJason Wessel 
1302ce4b1b16SIgor Mammedov static void wait_for_master_cpu(int cpu)
1303ce4b1b16SIgor Mammedov {
1304ce4b1b16SIgor Mammedov #ifdef CONFIG_SMP
1305ce4b1b16SIgor Mammedov 	/*
1306ce4b1b16SIgor Mammedov 	 * wait for ACK from master CPU before continuing
1307ce4b1b16SIgor Mammedov 	 * with AP initialization
1308ce4b1b16SIgor Mammedov 	 */
1309ce4b1b16SIgor Mammedov 	WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1310ce4b1b16SIgor Mammedov 	while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1311ce4b1b16SIgor Mammedov 		cpu_relax();
1312ce4b1b16SIgor Mammedov #endif
1313ce4b1b16SIgor Mammedov }
1314ce4b1b16SIgor Mammedov 
1315f7627e25SThomas Gleixner /*
1316f7627e25SThomas Gleixner  * cpu_init() initializes state that is per-CPU. Some data is already
1317f7627e25SThomas Gleixner  * initialized (naturally) in the bootstrap process, such as the GDT
1318f7627e25SThomas Gleixner  * and IDT. We reload them nevertheless, this function acts as a
1319f7627e25SThomas Gleixner  * 'CPU state barrier', nothing should get across.
13201ba76586SYinghai Lu  * A lot of state is already set up in PDA init for 64 bit
1321f7627e25SThomas Gleixner  */
13221ba76586SYinghai Lu #ifdef CONFIG_X86_64
13230f3fa48aSIngo Molnar 
1324148f9bb8SPaul Gortmaker void cpu_init(void)
13251ba76586SYinghai Lu {
13260fe1e009STejun Heo 	struct orig_ist *oist;
13271ba76586SYinghai Lu 	struct task_struct *me;
13280f3fa48aSIngo Molnar 	struct tss_struct *t;
13290f3fa48aSIngo Molnar 	unsigned long v;
1330ce4b1b16SIgor Mammedov 	int cpu = stack_smp_processor_id();
13311ba76586SYinghai Lu 	int i;
13321ba76586SYinghai Lu 
1333ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1334ce4b1b16SIgor Mammedov 
1335e6ebf5deSFenghua Yu 	/*
13361e02ce4cSAndy Lutomirski 	 * Initialize the CR4 shadow before doing anything that could
13371e02ce4cSAndy Lutomirski 	 * try to read it.
13381e02ce4cSAndy Lutomirski 	 */
13391e02ce4cSAndy Lutomirski 	cr4_init_shadow();
13401e02ce4cSAndy Lutomirski 
13411e02ce4cSAndy Lutomirski 	/*
1342e6ebf5deSFenghua Yu 	 * Load microcode on this cpu if a valid microcode is available.
1343e6ebf5deSFenghua Yu 	 * This is early microcode loading procedure.
1344e6ebf5deSFenghua Yu 	 */
1345e6ebf5deSFenghua Yu 	load_ucode_ap();
1346e6ebf5deSFenghua Yu 
134724933b82SAndy Lutomirski 	t = &per_cpu(cpu_tss, cpu);
13480fe1e009STejun Heo 	oist = &per_cpu(orig_ist, cpu);
13490f3fa48aSIngo Molnar 
1350e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA
135127fd185fSFenghua Yu 	if (this_cpu_read(numa_node) == 0 &&
1352e534c7c5SLee Schermerhorn 	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
1353e534c7c5SLee Schermerhorn 		set_numa_node(early_cpu_to_node(cpu));
1354e7a22c1eSBrian Gerst #endif
13551ba76586SYinghai Lu 
13561ba76586SYinghai Lu 	me = current;
13571ba76586SYinghai Lu 
13582eaad1fdSMike Travis 	pr_debug("Initializing CPU#%d\n", cpu);
13591ba76586SYinghai Lu 
1360375074ccSAndy Lutomirski 	cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
13611ba76586SYinghai Lu 
13621ba76586SYinghai Lu 	/*
13631ba76586SYinghai Lu 	 * Initialize the per-CPU GDT with the boot GDT,
13641ba76586SYinghai Lu 	 * and set up the GDT descriptor:
13651ba76586SYinghai Lu 	 */
13661ba76586SYinghai Lu 
1367552be871SBrian Gerst 	switch_to_new_gdt(cpu);
13682697fbd5SBrian Gerst 	loadsegment(fs, 0);
13692697fbd5SBrian Gerst 
1370cf910e83SSeiji Aguchi 	load_current_idt();
13711ba76586SYinghai Lu 
13721ba76586SYinghai Lu 	memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
13731ba76586SYinghai Lu 	syscall_init();
13741ba76586SYinghai Lu 
13751ba76586SYinghai Lu 	wrmsrl(MSR_FS_BASE, 0);
13761ba76586SYinghai Lu 	wrmsrl(MSR_KERNEL_GS_BASE, 0);
13771ba76586SYinghai Lu 	barrier();
13781ba76586SYinghai Lu 
13794763ed4dSH. Peter Anvin 	x86_configure_nx();
1380659006bfSThomas Gleixner 	x2apic_setup();
13811ba76586SYinghai Lu 
13821ba76586SYinghai Lu 	/*
13831ba76586SYinghai Lu 	 * set up and load the per-CPU TSS
13841ba76586SYinghai Lu 	 */
13850fe1e009STejun Heo 	if (!oist->ist[0]) {
138692d65b23SBrian Gerst 		char *estacks = per_cpu(exception_stacks, cpu);
13870f3fa48aSIngo Molnar 
13881ba76586SYinghai Lu 		for (v = 0; v < N_EXCEPTION_STACKS; v++) {
13890f3fa48aSIngo Molnar 			estacks += exception_stack_sizes[v];
13900fe1e009STejun Heo 			oist->ist[v] = t->x86_tss.ist[v] =
13911ba76586SYinghai Lu 					(unsigned long)estacks;
1392228bdaa9SSteven Rostedt 			if (v == DEBUG_STACK-1)
1393228bdaa9SSteven Rostedt 				per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
13941ba76586SYinghai Lu 		}
13951ba76586SYinghai Lu 	}
13961ba76586SYinghai Lu 
13971ba76586SYinghai Lu 	t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
13980f3fa48aSIngo Molnar 
13991ba76586SYinghai Lu 	/*
14001ba76586SYinghai Lu 	 * <= is required because the CPU will access up to
14011ba76586SYinghai Lu 	 * 8 bits beyond the end of the IO permission bitmap.
14021ba76586SYinghai Lu 	 */
14031ba76586SYinghai Lu 	for (i = 0; i <= IO_BITMAP_LONGS; i++)
14041ba76586SYinghai Lu 		t->io_bitmap[i] = ~0UL;
14051ba76586SYinghai Lu 
14061ba76586SYinghai Lu 	atomic_inc(&init_mm.mm_count);
14071ba76586SYinghai Lu 	me->active_mm = &init_mm;
14088c5dfd25SStoyan Gaydarov 	BUG_ON(me->mm);
14091ba76586SYinghai Lu 	enter_lazy_tlb(&init_mm, me);
14101ba76586SYinghai Lu 
14111ba76586SYinghai Lu 	load_sp0(t, &current->thread);
14121ba76586SYinghai Lu 	set_tss_desc(cpu, t);
14131ba76586SYinghai Lu 	load_TR_desc();
14141ba76586SYinghai Lu 	load_LDT(&init_mm.context);
14151ba76586SYinghai Lu 
14169766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
14170bb9fef9SJason Wessel 	dbg_restore_debug_regs();
14181ba76586SYinghai Lu 
141921c4cd10SIngo Molnar 	fpu__init_cpu();
14201ba76586SYinghai Lu 
14211ba76586SYinghai Lu 	if (is_uv_system())
14221ba76586SYinghai Lu 		uv_cpu_init();
14231ba76586SYinghai Lu }
14241ba76586SYinghai Lu 
14251ba76586SYinghai Lu #else
14261ba76586SYinghai Lu 
1427148f9bb8SPaul Gortmaker void cpu_init(void)
1428f7627e25SThomas Gleixner {
1429f7627e25SThomas Gleixner 	int cpu = smp_processor_id();
1430f7627e25SThomas Gleixner 	struct task_struct *curr = current;
143124933b82SAndy Lutomirski 	struct tss_struct *t = &per_cpu(cpu_tss, cpu);
1432f7627e25SThomas Gleixner 	struct thread_struct *thread = &curr->thread;
1433f7627e25SThomas Gleixner 
1434ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1435e6ebf5deSFenghua Yu 
14365b2bdbc8SSteven Rostedt 	/*
14375b2bdbc8SSteven Rostedt 	 * Initialize the CR4 shadow before doing anything that could
14385b2bdbc8SSteven Rostedt 	 * try to read it.
14395b2bdbc8SSteven Rostedt 	 */
14405b2bdbc8SSteven Rostedt 	cr4_init_shadow();
14415b2bdbc8SSteven Rostedt 
1442ce4b1b16SIgor Mammedov 	show_ucode_info_early();
1443f7627e25SThomas Gleixner 
1444f7627e25SThomas Gleixner 	printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1445f7627e25SThomas Gleixner 
14469298b815SDave Hansen 	if (cpu_feature_enabled(X86_FEATURE_VME) || cpu_has_tsc || cpu_has_de)
1447375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1448f7627e25SThomas Gleixner 
1449cf910e83SSeiji Aguchi 	load_current_idt();
1450552be871SBrian Gerst 	switch_to_new_gdt(cpu);
1451f7627e25SThomas Gleixner 
1452f7627e25SThomas Gleixner 	/*
1453f7627e25SThomas Gleixner 	 * Set up and load the per-CPU TSS and LDT
1454f7627e25SThomas Gleixner 	 */
1455f7627e25SThomas Gleixner 	atomic_inc(&init_mm.mm_count);
1456f7627e25SThomas Gleixner 	curr->active_mm = &init_mm;
14578c5dfd25SStoyan Gaydarov 	BUG_ON(curr->mm);
1458f7627e25SThomas Gleixner 	enter_lazy_tlb(&init_mm, curr);
1459f7627e25SThomas Gleixner 
1460faca6227SH. Peter Anvin 	load_sp0(t, thread);
1461f7627e25SThomas Gleixner 	set_tss_desc(cpu, t);
1462f7627e25SThomas Gleixner 	load_TR_desc();
1463f7627e25SThomas Gleixner 	load_LDT(&init_mm.context);
1464f7627e25SThomas Gleixner 
1465f9a196b8SThomas Gleixner 	t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1466f9a196b8SThomas Gleixner 
1467f7627e25SThomas Gleixner #ifdef CONFIG_DOUBLEFAULT
1468f7627e25SThomas Gleixner 	/* Set up doublefault TSS pointer in the GDT */
1469f7627e25SThomas Gleixner 	__set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1470f7627e25SThomas Gleixner #endif
1471f7627e25SThomas Gleixner 
14729766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
14730bb9fef9SJason Wessel 	dbg_restore_debug_regs();
1474f7627e25SThomas Gleixner 
147521c4cd10SIngo Molnar 	fpu__init_cpu();
1476f7627e25SThomas Gleixner }
14771ba76586SYinghai Lu #endif
14785700f743SBorislav Petkov 
14795700f743SBorislav Petkov #ifdef CONFIG_X86_DEBUG_STATIC_CPU_HAS
14805700f743SBorislav Petkov void warn_pre_alternatives(void)
14815700f743SBorislav Petkov {
14825700f743SBorislav Petkov 	WARN(1, "You're using static_cpu_has before alternatives have run!\n");
14835700f743SBorislav Petkov }
14845700f743SBorislav Petkov EXPORT_SYMBOL_GPL(warn_pre_alternatives);
14855700f743SBorislav Petkov #endif
14864a90a99cSBorislav Petkov 
14874a90a99cSBorislav Petkov inline bool __static_cpu_has_safe(u16 bit)
14884a90a99cSBorislav Petkov {
14894a90a99cSBorislav Petkov 	return boot_cpu_has(bit);
14904a90a99cSBorislav Petkov }
14914a90a99cSBorislav Petkov EXPORT_SYMBOL_GPL(__static_cpu_has_safe);
1492*b51ef52dSLaura Abbott 
1493*b51ef52dSLaura Abbott static void bsp_resume(void)
1494*b51ef52dSLaura Abbott {
1495*b51ef52dSLaura Abbott 	if (this_cpu->c_bsp_resume)
1496*b51ef52dSLaura Abbott 		this_cpu->c_bsp_resume(&boot_cpu_data);
1497*b51ef52dSLaura Abbott }
1498*b51ef52dSLaura Abbott 
1499*b51ef52dSLaura Abbott static struct syscore_ops cpu_syscore_ops = {
1500*b51ef52dSLaura Abbott 	.resume		= bsp_resume,
1501*b51ef52dSLaura Abbott };
1502*b51ef52dSLaura Abbott 
1503*b51ef52dSLaura Abbott static int __init init_cpu_syscore(void)
1504*b51ef52dSLaura Abbott {
1505*b51ef52dSLaura Abbott 	register_syscore_ops(&cpu_syscore_ops);
1506*b51ef52dSLaura Abbott 	return 0;
1507*b51ef52dSLaura Abbott }
1508*b51ef52dSLaura Abbott core_initcall(init_cpu_syscore);
1509