xref: /linux/arch/x86/kernel/cpu/common.c (revision 2ccd71f1b278d450a6f8c8c737c7fe237ca06dc6)
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>
16b51ef52dSLaura 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 {
276581b7f15SAndrew Cooper 	unsigned long eflags = native_save_fl();
277b2cc2a07SH. Peter Anvin 
278b2cc2a07SH. Peter Anvin 	/* This should have been cleared long ago */
279b2cc2a07SH. Peter Anvin 	BUG_ON(eflags & X86_EFLAGS_AC);
280b2cc2a07SH. Peter Anvin 
28103bbd596SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMAP)) {
28203bbd596SH. Peter Anvin #ifdef CONFIG_X86_SMAP
283375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMAP);
28403bbd596SH. Peter Anvin #else
285375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_SMAP);
28603bbd596SH. Peter Anvin #endif
28703bbd596SH. Peter Anvin 	}
288f7627e25SThomas Gleixner }
289f7627e25SThomas Gleixner 
290f7627e25SThomas Gleixner /*
291b38b0665SH. Peter Anvin  * Some CPU features depend on higher CPUID levels, which may not always
292b38b0665SH. Peter Anvin  * be available due to CPUID level capping or broken virtualization
293b38b0665SH. Peter Anvin  * software.  Add those features to this table to auto-disable them.
294b38b0665SH. Peter Anvin  */
295b38b0665SH. Peter Anvin struct cpuid_dependent_feature {
296b38b0665SH. Peter Anvin 	u32 feature;
297b38b0665SH. Peter Anvin 	u32 level;
298b38b0665SH. Peter Anvin };
2990f3fa48aSIngo Molnar 
300148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature
301b38b0665SH. Peter Anvin cpuid_dependent_features[] = {
302b38b0665SH. Peter Anvin 	{ X86_FEATURE_MWAIT,		0x00000005 },
303b38b0665SH. Peter Anvin 	{ X86_FEATURE_DCA,		0x00000009 },
304b38b0665SH. Peter Anvin 	{ X86_FEATURE_XSAVE,		0x0000000d },
305b38b0665SH. Peter Anvin 	{ 0, 0 }
306b38b0665SH. Peter Anvin };
307b38b0665SH. Peter Anvin 
308148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
309b38b0665SH. Peter Anvin {
310b38b0665SH. Peter Anvin 	const struct cpuid_dependent_feature *df;
3119766cdbcSJaswinder Singh Rajput 
312b38b0665SH. Peter Anvin 	for (df = cpuid_dependent_features; df->feature; df++) {
3130f3fa48aSIngo Molnar 
3140f3fa48aSIngo Molnar 		if (!cpu_has(c, df->feature))
3150f3fa48aSIngo Molnar 			continue;
316b38b0665SH. Peter Anvin 		/*
317b38b0665SH. Peter Anvin 		 * Note: cpuid_level is set to -1 if unavailable, but
318b38b0665SH. Peter Anvin 		 * extended_extended_level is set to 0 if unavailable
319b38b0665SH. Peter Anvin 		 * and the legitimate extended levels are all negative
320b38b0665SH. Peter Anvin 		 * when signed; hence the weird messing around with
321b38b0665SH. Peter Anvin 		 * signs here...
322b38b0665SH. Peter Anvin 		 */
3230f3fa48aSIngo Molnar 		if (!((s32)df->level < 0 ?
324f6db44dfSYinghai Lu 		     (u32)df->level > (u32)c->extended_cpuid_level :
3250f3fa48aSIngo Molnar 		     (s32)df->level > (s32)c->cpuid_level))
3260f3fa48aSIngo Molnar 			continue;
3270f3fa48aSIngo Molnar 
328b38b0665SH. Peter Anvin 		clear_cpu_cap(c, df->feature);
3290f3fa48aSIngo Molnar 		if (!warn)
3300f3fa48aSIngo Molnar 			continue;
3310f3fa48aSIngo Molnar 
332b38b0665SH. Peter Anvin 		printk(KERN_WARNING
3339def39beSJosh Triplett 		       "CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
3349def39beSJosh Triplett 				x86_cap_flag(df->feature), df->level);
335b38b0665SH. Peter Anvin 	}
336b38b0665SH. Peter Anvin }
337b38b0665SH. Peter Anvin 
338b38b0665SH. Peter Anvin /*
339f7627e25SThomas Gleixner  * Naming convention should be: <Name> [(<Codename>)]
340f7627e25SThomas Gleixner  * This table only is used unless init_<vendor>() below doesn't set it;
3410f3fa48aSIngo Molnar  * in particular, if CPUID levels 0x80000002..4 are supported, this
3420f3fa48aSIngo Molnar  * isn't used
343f7627e25SThomas Gleixner  */
344f7627e25SThomas Gleixner 
345f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */
346148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c)
347f7627e25SThomas Gleixner {
34809dc68d9SJan Beulich #ifdef CONFIG_X86_32
34909dc68d9SJan Beulich 	const struct legacy_cpu_model_info *info;
350f7627e25SThomas Gleixner 
351f7627e25SThomas Gleixner 	if (c->x86_model >= 16)
352f7627e25SThomas Gleixner 		return NULL;	/* Range check */
353f7627e25SThomas Gleixner 
354f7627e25SThomas Gleixner 	if (!this_cpu)
355f7627e25SThomas Gleixner 		return NULL;
356f7627e25SThomas Gleixner 
35709dc68d9SJan Beulich 	info = this_cpu->legacy_models;
358f7627e25SThomas Gleixner 
35909dc68d9SJan Beulich 	while (info->family) {
360f7627e25SThomas Gleixner 		if (info->family == c->x86)
361f7627e25SThomas Gleixner 			return info->model_names[c->x86_model];
362f7627e25SThomas Gleixner 		info++;
363f7627e25SThomas Gleixner 	}
36409dc68d9SJan Beulich #endif
365f7627e25SThomas Gleixner 	return NULL;		/* Not found */
366f7627e25SThomas Gleixner }
367f7627e25SThomas Gleixner 
368148f9bb8SPaul Gortmaker __u32 cpu_caps_cleared[NCAPINTS];
369148f9bb8SPaul Gortmaker __u32 cpu_caps_set[NCAPINTS];
370f7627e25SThomas Gleixner 
37111e3a840SJeremy Fitzhardinge void load_percpu_segment(int cpu)
3729d31d35bSYinghai Lu {
373fab334c1SYinghai Lu #ifdef CONFIG_X86_32
3742697fbd5SBrian Gerst 	loadsegment(fs, __KERNEL_PERCPU);
3752697fbd5SBrian Gerst #else
3762697fbd5SBrian Gerst 	loadsegment(gs, 0);
3772697fbd5SBrian Gerst 	wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
378fab334c1SYinghai Lu #endif
37960a5317fSTejun Heo 	load_stack_canary_segment();
3809d31d35bSYinghai Lu }
3819d31d35bSYinghai Lu 
3820f3fa48aSIngo Molnar /*
3830f3fa48aSIngo Molnar  * Current gdt points %fs at the "master" per-cpu area: after this,
3840f3fa48aSIngo Molnar  * it's on the real one.
3850f3fa48aSIngo Molnar  */
386552be871SBrian Gerst void switch_to_new_gdt(int cpu)
387f7627e25SThomas Gleixner {
388f7627e25SThomas Gleixner 	struct desc_ptr gdt_descr;
389f7627e25SThomas Gleixner 
390f7627e25SThomas Gleixner 	gdt_descr.address = (long)get_cpu_gdt_table(cpu);
391f7627e25SThomas Gleixner 	gdt_descr.size = GDT_SIZE - 1;
392f7627e25SThomas Gleixner 	load_gdt(&gdt_descr);
393f7627e25SThomas Gleixner 	/* Reload the per-cpu base */
39411e3a840SJeremy Fitzhardinge 
39511e3a840SJeremy Fitzhardinge 	load_percpu_segment(cpu);
396f7627e25SThomas Gleixner }
397f7627e25SThomas Gleixner 
398148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
399f7627e25SThomas Gleixner 
400148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c)
401f7627e25SThomas Gleixner {
402f7627e25SThomas Gleixner 	unsigned int *v;
403ee098e1aSBorislav Petkov 	char *p, *q, *s;
404f7627e25SThomas Gleixner 
4053da99c97SYinghai Lu 	if (c->extended_cpuid_level < 0x80000004)
4061b05d60dSYinghai Lu 		return;
407f7627e25SThomas Gleixner 
408f7627e25SThomas Gleixner 	v = (unsigned int *)c->x86_model_id;
409f7627e25SThomas Gleixner 	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
410f7627e25SThomas Gleixner 	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
411f7627e25SThomas Gleixner 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
412f7627e25SThomas Gleixner 	c->x86_model_id[48] = 0;
413f7627e25SThomas Gleixner 
414ee098e1aSBorislav Petkov 	/* Trim whitespace */
415ee098e1aSBorislav Petkov 	p = q = s = &c->x86_model_id[0];
416ee098e1aSBorislav Petkov 
417ee098e1aSBorislav Petkov 	while (*p == ' ')
418ee098e1aSBorislav Petkov 		p++;
419ee098e1aSBorislav Petkov 
420ee098e1aSBorislav Petkov 	while (*p) {
421ee098e1aSBorislav Petkov 		/* Note the last non-whitespace index */
422ee098e1aSBorislav Petkov 		if (!isspace(*p))
423ee098e1aSBorislav Petkov 			s = q;
424ee098e1aSBorislav Petkov 
425ee098e1aSBorislav Petkov 		*q++ = *p++;
426ee098e1aSBorislav Petkov 	}
427ee098e1aSBorislav Petkov 
428ee098e1aSBorislav Petkov 	*(s + 1) = '\0';
429f7627e25SThomas Gleixner }
430f7627e25SThomas Gleixner 
431148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
432f7627e25SThomas Gleixner {
4339d31d35bSYinghai Lu 	unsigned int n, dummy, ebx, ecx, edx, l2size;
434f7627e25SThomas Gleixner 
4353da99c97SYinghai Lu 	n = c->extended_cpuid_level;
436f7627e25SThomas Gleixner 
437f7627e25SThomas Gleixner 	if (n >= 0x80000005) {
4389d31d35bSYinghai Lu 		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
439f7627e25SThomas Gleixner 		c->x86_cache_size = (ecx>>24) + (edx>>24);
440140fc727SYinghai Lu #ifdef CONFIG_X86_64
441140fc727SYinghai Lu 		/* On K8 L1 TLB is inclusive, so don't count it */
442140fc727SYinghai Lu 		c->x86_tlbsize = 0;
443140fc727SYinghai Lu #endif
444f7627e25SThomas Gleixner 	}
445f7627e25SThomas Gleixner 
446f7627e25SThomas Gleixner 	if (n < 0x80000006)	/* Some chips just has a large L1. */
447f7627e25SThomas Gleixner 		return;
448f7627e25SThomas Gleixner 
4490a488a53SYinghai Lu 	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
450f7627e25SThomas Gleixner 	l2size = ecx >> 16;
451f7627e25SThomas Gleixner 
452140fc727SYinghai Lu #ifdef CONFIG_X86_64
453140fc727SYinghai Lu 	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
454140fc727SYinghai Lu #else
455f7627e25SThomas Gleixner 	/* do processor-specific cache resizing */
45609dc68d9SJan Beulich 	if (this_cpu->legacy_cache_size)
45709dc68d9SJan Beulich 		l2size = this_cpu->legacy_cache_size(c, l2size);
458f7627e25SThomas Gleixner 
459f7627e25SThomas Gleixner 	/* Allow user to override all this if necessary. */
460f7627e25SThomas Gleixner 	if (cachesize_override != -1)
461f7627e25SThomas Gleixner 		l2size = cachesize_override;
462f7627e25SThomas Gleixner 
463f7627e25SThomas Gleixner 	if (l2size == 0)
464f7627e25SThomas Gleixner 		return;		/* Again, no L2 cache is possible */
465140fc727SYinghai Lu #endif
466f7627e25SThomas Gleixner 
467f7627e25SThomas Gleixner 	c->x86_cache_size = l2size;
468f7627e25SThomas Gleixner }
469f7627e25SThomas Gleixner 
470e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO];
471e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO];
472e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO];
473e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO];
474e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO];
475e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO];
476dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO];
477e0ba94f1SAlex Shi 
478f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c)
479e0ba94f1SAlex Shi {
480e0ba94f1SAlex Shi 	if (this_cpu->c_detect_tlb)
481e0ba94f1SAlex Shi 		this_cpu->c_detect_tlb(c);
482e0ba94f1SAlex Shi 
483f94fe119SSteven Honeyman 	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
484e0ba94f1SAlex Shi 		tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
485f94fe119SSteven Honeyman 		tlb_lli_4m[ENTRIES]);
486f94fe119SSteven Honeyman 
487f94fe119SSteven Honeyman 	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
488f94fe119SSteven Honeyman 		tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
489f94fe119SSteven Honeyman 		tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
490e0ba94f1SAlex Shi }
491e0ba94f1SAlex Shi 
492148f9bb8SPaul Gortmaker void detect_ht(struct cpuinfo_x86 *c)
4939d31d35bSYinghai Lu {
494c8e56d20SBorislav Petkov #ifdef CONFIG_SMP
4959d31d35bSYinghai Lu 	u32 eax, ebx, ecx, edx;
4969d31d35bSYinghai Lu 	int index_msb, core_bits;
4972eaad1fdSMike Travis 	static bool printed;
4989d31d35bSYinghai Lu 
4990a488a53SYinghai Lu 	if (!cpu_has(c, X86_FEATURE_HT))
5009d31d35bSYinghai Lu 		return;
5019d31d35bSYinghai Lu 
5020a488a53SYinghai Lu 	if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
5030a488a53SYinghai Lu 		goto out;
5040a488a53SYinghai Lu 
5051cd78776SYinghai Lu 	if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
5061cd78776SYinghai Lu 		return;
5071cd78776SYinghai Lu 
5080a488a53SYinghai Lu 	cpuid(1, &eax, &ebx, &ecx, &edx);
5090a488a53SYinghai Lu 
5109d31d35bSYinghai Lu 	smp_num_siblings = (ebx & 0xff0000) >> 16;
5119d31d35bSYinghai Lu 
5129d31d35bSYinghai Lu 	if (smp_num_siblings == 1) {
5132eaad1fdSMike Travis 		printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
5140f3fa48aSIngo Molnar 		goto out;
5150f3fa48aSIngo Molnar 	}
5160f3fa48aSIngo Molnar 
5170f3fa48aSIngo Molnar 	if (smp_num_siblings <= 1)
5180f3fa48aSIngo Molnar 		goto out;
5199d31d35bSYinghai Lu 
5209d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
521cb8cc442SIngo Molnar 	c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
5229d31d35bSYinghai Lu 
5239d31d35bSYinghai Lu 	smp_num_siblings = smp_num_siblings / c->x86_max_cores;
5249d31d35bSYinghai Lu 
5259d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
5269d31d35bSYinghai Lu 
5279d31d35bSYinghai Lu 	core_bits = get_count_order(c->x86_max_cores);
5289d31d35bSYinghai Lu 
529cb8cc442SIngo Molnar 	c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
5301cd78776SYinghai Lu 				       ((1 << core_bits) - 1);
5319d31d35bSYinghai Lu 
5320a488a53SYinghai Lu out:
5332eaad1fdSMike Travis 	if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
5340a488a53SYinghai Lu 		printk(KERN_INFO  "CPU: Physical Processor ID: %d\n",
5350a488a53SYinghai Lu 		       c->phys_proc_id);
5369d31d35bSYinghai Lu 		printk(KERN_INFO  "CPU: Processor Core ID: %d\n",
5379d31d35bSYinghai Lu 		       c->cpu_core_id);
5382eaad1fdSMike Travis 		printed = 1;
5399d31d35bSYinghai Lu 	}
5409d31d35bSYinghai Lu #endif
54197e4db7cSYinghai Lu }
542f7627e25SThomas Gleixner 
543148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c)
544f7627e25SThomas Gleixner {
545f7627e25SThomas Gleixner 	char *v = c->x86_vendor_id;
5460f3fa48aSIngo Molnar 	int i;
547f7627e25SThomas Gleixner 
548f7627e25SThomas Gleixner 	for (i = 0; i < X86_VENDOR_NUM; i++) {
54910a434fcSYinghai Lu 		if (!cpu_devs[i])
55010a434fcSYinghai Lu 			break;
55110a434fcSYinghai Lu 
552f7627e25SThomas Gleixner 		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
553f7627e25SThomas Gleixner 		    (cpu_devs[i]->c_ident[1] &&
554f7627e25SThomas Gleixner 		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
5550f3fa48aSIngo Molnar 
556f7627e25SThomas Gleixner 			this_cpu = cpu_devs[i];
55710a434fcSYinghai Lu 			c->x86_vendor = this_cpu->c_x86_vendor;
558f7627e25SThomas Gleixner 			return;
559f7627e25SThomas Gleixner 		}
560f7627e25SThomas Gleixner 	}
56110a434fcSYinghai Lu 
562a9c56953SMinchan Kim 	printk_once(KERN_ERR
563a9c56953SMinchan Kim 			"CPU: vendor_id '%s' unknown, using generic init.\n" \
564a9c56953SMinchan Kim 			"CPU: Your system may be unstable.\n", v);
56510a434fcSYinghai Lu 
566f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
567f7627e25SThomas Gleixner 	this_cpu = &default_cpu;
568f7627e25SThomas Gleixner }
569f7627e25SThomas Gleixner 
570148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c)
571f7627e25SThomas Gleixner {
572f7627e25SThomas Gleixner 	/* Get vendor name */
5734a148513SHarvey Harrison 	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
5744a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[0],
5754a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[8],
5764a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[4]);
577f7627e25SThomas Gleixner 
578f7627e25SThomas Gleixner 	c->x86 = 4;
5799d31d35bSYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
580f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
581f7627e25SThomas Gleixner 		u32 junk, tfms, cap0, misc;
5820f3fa48aSIngo Molnar 
583f7627e25SThomas Gleixner 		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
5849d31d35bSYinghai Lu 		c->x86 = (tfms >> 8) & 0xf;
5859d31d35bSYinghai Lu 		c->x86_model = (tfms >> 4) & 0xf;
5869d31d35bSYinghai Lu 		c->x86_mask = tfms & 0xf;
5870f3fa48aSIngo Molnar 
588f7627e25SThomas Gleixner 		if (c->x86 == 0xf)
589f7627e25SThomas Gleixner 			c->x86 += (tfms >> 20) & 0xff;
590f7627e25SThomas Gleixner 		if (c->x86 >= 0x6)
5919d31d35bSYinghai Lu 			c->x86_model += ((tfms >> 16) & 0xf) << 4;
5920f3fa48aSIngo Molnar 
593d4387bd3SHuang, Ying 		if (cap0 & (1<<19)) {
594d4387bd3SHuang, Ying 			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
5959d31d35bSYinghai Lu 			c->x86_cache_alignment = c->x86_clflush_size;
596d4387bd3SHuang, Ying 		}
597f7627e25SThomas Gleixner 	}
598f7627e25SThomas Gleixner }
5993da99c97SYinghai Lu 
600148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c)
601093af8d7SYinghai Lu {
602093af8d7SYinghai Lu 	u32 tfms, xlvl;
6033da99c97SYinghai Lu 	u32 ebx;
604093af8d7SYinghai Lu 
605093af8d7SYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
606093af8d7SYinghai Lu 	if (c->cpuid_level >= 0x00000001) {
607093af8d7SYinghai Lu 		u32 capability, excap;
6080f3fa48aSIngo Molnar 
609093af8d7SYinghai Lu 		cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
610093af8d7SYinghai Lu 		c->x86_capability[0] = capability;
611093af8d7SYinghai Lu 		c->x86_capability[4] = excap;
612093af8d7SYinghai Lu 	}
613093af8d7SYinghai Lu 
614bdc802dcSH. Peter Anvin 	/* Additional Intel-defined flags: level 0x00000007 */
615bdc802dcSH. Peter Anvin 	if (c->cpuid_level >= 0x00000007) {
616bdc802dcSH. Peter Anvin 		u32 eax, ebx, ecx, edx;
617bdc802dcSH. Peter Anvin 
618bdc802dcSH. Peter Anvin 		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
619bdc802dcSH. Peter Anvin 
620bdc802dcSH. Peter Anvin 		c->x86_capability[9] = ebx;
621*2ccd71f1SBorislav Petkov 
622*2ccd71f1SBorislav Petkov 		c->x86_capability[14] = cpuid_eax(0x00000006);
623bdc802dcSH. Peter Anvin 	}
624bdc802dcSH. Peter Anvin 
6256229ad27SFenghua Yu 	/* Extended state features: level 0x0000000d */
6266229ad27SFenghua Yu 	if (c->cpuid_level >= 0x0000000d) {
6276229ad27SFenghua Yu 		u32 eax, ebx, ecx, edx;
6286229ad27SFenghua Yu 
6296229ad27SFenghua Yu 		cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
6306229ad27SFenghua Yu 
6316229ad27SFenghua Yu 		c->x86_capability[10] = eax;
6326229ad27SFenghua Yu 	}
6336229ad27SFenghua Yu 
634cbc82b17SPeter P Waskiewicz Jr 	/* Additional Intel-defined flags: level 0x0000000F */
635cbc82b17SPeter P Waskiewicz Jr 	if (c->cpuid_level >= 0x0000000F) {
636cbc82b17SPeter P Waskiewicz Jr 		u32 eax, ebx, ecx, edx;
637cbc82b17SPeter P Waskiewicz Jr 
638cbc82b17SPeter P Waskiewicz Jr 		/* QoS sub-leaf, EAX=0Fh, ECX=0 */
639cbc82b17SPeter P Waskiewicz Jr 		cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
640cbc82b17SPeter P Waskiewicz Jr 		c->x86_capability[11] = edx;
641cbc82b17SPeter P Waskiewicz Jr 		if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
642cbc82b17SPeter P Waskiewicz Jr 			/* will be overridden if occupancy monitoring exists */
643cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_max_rmid = ebx;
644cbc82b17SPeter P Waskiewicz Jr 
645cbc82b17SPeter P Waskiewicz Jr 			/* QoS sub-leaf, EAX=0Fh, ECX=1 */
646cbc82b17SPeter P Waskiewicz Jr 			cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
647cbc82b17SPeter P Waskiewicz Jr 			c->x86_capability[12] = edx;
648cbc82b17SPeter P Waskiewicz Jr 			if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) {
649cbc82b17SPeter P Waskiewicz Jr 				c->x86_cache_max_rmid = ecx;
650cbc82b17SPeter P Waskiewicz Jr 				c->x86_cache_occ_scale = ebx;
651cbc82b17SPeter P Waskiewicz Jr 			}
652cbc82b17SPeter P Waskiewicz Jr 		} else {
653cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_max_rmid = -1;
654cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_occ_scale = -1;
655cbc82b17SPeter P Waskiewicz Jr 		}
656cbc82b17SPeter P Waskiewicz Jr 	}
657cbc82b17SPeter P Waskiewicz Jr 
658093af8d7SYinghai Lu 	/* AMD-defined flags: level 0x80000001 */
659093af8d7SYinghai Lu 	xlvl = cpuid_eax(0x80000000);
6603da99c97SYinghai Lu 	c->extended_cpuid_level = xlvl;
6610f3fa48aSIngo Molnar 
662093af8d7SYinghai Lu 	if ((xlvl & 0xffff0000) == 0x80000000) {
663093af8d7SYinghai Lu 		if (xlvl >= 0x80000001) {
664093af8d7SYinghai Lu 			c->x86_capability[1] = cpuid_edx(0x80000001);
665093af8d7SYinghai Lu 			c->x86_capability[6] = cpuid_ecx(0x80000001);
666093af8d7SYinghai Lu 		}
667093af8d7SYinghai Lu 	}
668093af8d7SYinghai Lu 
6695122c890SYinghai Lu 	if (c->extended_cpuid_level >= 0x80000008) {
6705122c890SYinghai Lu 		u32 eax = cpuid_eax(0x80000008);
6715122c890SYinghai Lu 
6725122c890SYinghai Lu 		c->x86_virt_bits = (eax >> 8) & 0xff;
6735122c890SYinghai Lu 		c->x86_phys_bits = eax & 0xff;
6742167ceabSWan Zongshun 		c->x86_capability[13] = cpuid_ebx(0x80000008);
6755122c890SYinghai Lu 	}
67613c6c532SJan Beulich #ifdef CONFIG_X86_32
67713c6c532SJan Beulich 	else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
67813c6c532SJan Beulich 		c->x86_phys_bits = 36;
6795122c890SYinghai Lu #endif
680e3224234SYinghai Lu 
681e3224234SYinghai Lu 	if (c->extended_cpuid_level >= 0x80000007)
682e3224234SYinghai Lu 		c->x86_power = cpuid_edx(0x80000007);
683e3224234SYinghai Lu 
684*2ccd71f1SBorislav Petkov 	if (c->extended_cpuid_level >= 0x8000000a)
685*2ccd71f1SBorislav Petkov 		c->x86_capability[15] = cpuid_edx(0x8000000a);
686*2ccd71f1SBorislav Petkov 
6871dedefd1SJacob Pan 	init_scattered_cpuid_features(c);
688093af8d7SYinghai Lu }
689093af8d7SYinghai Lu 
690148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
691aef93c8bSYinghai Lu {
692aef93c8bSYinghai Lu #ifdef CONFIG_X86_32
693aef93c8bSYinghai Lu 	int i;
694aef93c8bSYinghai Lu 
695aef93c8bSYinghai Lu 	/*
696aef93c8bSYinghai Lu 	 * First of all, decide if this is a 486 or higher
697aef93c8bSYinghai Lu 	 * It's a 486 if we can modify the AC flag
698aef93c8bSYinghai Lu 	 */
699aef93c8bSYinghai Lu 	if (flag_is_changeable_p(X86_EFLAGS_AC))
700aef93c8bSYinghai Lu 		c->x86 = 4;
701aef93c8bSYinghai Lu 	else
702aef93c8bSYinghai Lu 		c->x86 = 3;
703aef93c8bSYinghai Lu 
704aef93c8bSYinghai Lu 	for (i = 0; i < X86_VENDOR_NUM; i++)
705aef93c8bSYinghai Lu 		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
706aef93c8bSYinghai Lu 			c->x86_vendor_id[0] = 0;
707aef93c8bSYinghai Lu 			cpu_devs[i]->c_identify(c);
708aef93c8bSYinghai Lu 			if (c->x86_vendor_id[0]) {
709aef93c8bSYinghai Lu 				get_cpu_vendor(c);
710aef93c8bSYinghai Lu 				break;
711aef93c8bSYinghai Lu 			}
712aef93c8bSYinghai Lu 		}
713aef93c8bSYinghai Lu #endif
714093af8d7SYinghai Lu }
715f7627e25SThomas Gleixner 
71634048c9eSPaolo Ciarrocchi /*
71734048c9eSPaolo Ciarrocchi  * Do minimum CPU detection early.
71834048c9eSPaolo Ciarrocchi  * Fields really needed: vendor, cpuid_level, family, model, mask,
71934048c9eSPaolo Ciarrocchi  * cache alignment.
72034048c9eSPaolo Ciarrocchi  * The others are not touched to avoid unwanted side effects.
72134048c9eSPaolo Ciarrocchi  *
72234048c9eSPaolo Ciarrocchi  * WARNING: this function is only called on the BP.  Don't add code here
72334048c9eSPaolo Ciarrocchi  * that is supposed to run on all CPUs.
72434048c9eSPaolo Ciarrocchi  */
7253da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c)
726f7627e25SThomas Gleixner {
7276627d242SYinghai Lu #ifdef CONFIG_X86_64
7286627d242SYinghai Lu 	c->x86_clflush_size = 64;
72913c6c532SJan Beulich 	c->x86_phys_bits = 36;
73013c6c532SJan Beulich 	c->x86_virt_bits = 48;
7316627d242SYinghai Lu #else
732d4387bd3SHuang, Ying 	c->x86_clflush_size = 32;
73313c6c532SJan Beulich 	c->x86_phys_bits = 32;
73413c6c532SJan Beulich 	c->x86_virt_bits = 32;
7356627d242SYinghai Lu #endif
7360a488a53SYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
737f7627e25SThomas Gleixner 
7383da99c97SYinghai Lu 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
7390a488a53SYinghai Lu 	c->extended_cpuid_level = 0;
7400a488a53SYinghai Lu 
741aef93c8bSYinghai Lu 	if (!have_cpuid_p())
742aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
743aef93c8bSYinghai Lu 
744aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
745f7627e25SThomas Gleixner 	if (!have_cpuid_p())
746f7627e25SThomas Gleixner 		return;
747f7627e25SThomas Gleixner 
748f7627e25SThomas Gleixner 	cpu_detect(c);
7493da99c97SYinghai Lu 	get_cpu_vendor(c);
7503da99c97SYinghai Lu 	get_cpu_cap(c);
75112cf105cSKrzysztof Helt 
75210a434fcSYinghai Lu 	if (this_cpu->c_early_init)
75310a434fcSYinghai Lu 		this_cpu->c_early_init(c);
7543da99c97SYinghai Lu 
755f6e9456cSRobert Richter 	c->cpu_index = 0;
756b38b0665SH. Peter Anvin 	filter_cpuid_features(c, false);
757de5397adSFenghua Yu 
758a110b5ecSBorislav Petkov 	if (this_cpu->c_bsp_init)
759a110b5ecSBorislav Petkov 		this_cpu->c_bsp_init(c);
760c3b83598SBorislav Petkov 
761c3b83598SBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
762db52ef74SIngo Molnar 	fpu__init_system(c);
763f7627e25SThomas Gleixner }
764f7627e25SThomas Gleixner 
7659d31d35bSYinghai Lu void __init early_cpu_init(void)
7669d31d35bSYinghai Lu {
76702dde8b4SJan Beulich 	const struct cpu_dev *const *cdev;
76810a434fcSYinghai Lu 	int count = 0;
7699d31d35bSYinghai Lu 
770ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
7719766cdbcSJaswinder Singh Rajput 	printk(KERN_INFO "KERNEL supported cpus:\n");
77231c997caSIngo Molnar #endif
77331c997caSIngo Molnar 
77410a434fcSYinghai Lu 	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
77502dde8b4SJan Beulich 		const struct cpu_dev *cpudev = *cdev;
7769d31d35bSYinghai Lu 
77710a434fcSYinghai Lu 		if (count >= X86_VENDOR_NUM)
77810a434fcSYinghai Lu 			break;
77910a434fcSYinghai Lu 		cpu_devs[count] = cpudev;
78010a434fcSYinghai Lu 		count++;
78110a434fcSYinghai Lu 
782ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
78331c997caSIngo Molnar 		{
78431c997caSIngo Molnar 			unsigned int j;
78531c997caSIngo Molnar 
78610a434fcSYinghai Lu 			for (j = 0; j < 2; j++) {
78710a434fcSYinghai Lu 				if (!cpudev->c_ident[j])
78810a434fcSYinghai Lu 					continue;
7899766cdbcSJaswinder Singh Rajput 				printk(KERN_INFO "  %s %s\n", cpudev->c_vendor,
79010a434fcSYinghai Lu 					cpudev->c_ident[j]);
79110a434fcSYinghai Lu 			}
79210a434fcSYinghai Lu 		}
7930388423dSDave Jones #endif
79431c997caSIngo Molnar 	}
7959d31d35bSYinghai Lu 	early_identify_cpu(&boot_cpu_data);
796f7627e25SThomas Gleixner }
797f7627e25SThomas Gleixner 
798b6734c35SH. Peter Anvin /*
799366d4a43SBorislav Petkov  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
800366d4a43SBorislav Petkov  * unfortunately, that's not true in practice because of early VIA
801366d4a43SBorislav Petkov  * chips and (more importantly) broken virtualizers that are not easy
802366d4a43SBorislav Petkov  * to detect. In the latter case it doesn't even *fail* reliably, so
803366d4a43SBorislav Petkov  * probing for it doesn't even work. Disable it completely on 32-bit
804ba0593bfSH. Peter Anvin  * unless we can find a reliable way to detect all the broken cases.
805366d4a43SBorislav Petkov  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
806b6734c35SH. Peter Anvin  */
807148f9bb8SPaul Gortmaker static void detect_nopl(struct cpuinfo_x86 *c)
808b6734c35SH. Peter Anvin {
809366d4a43SBorislav Petkov #ifdef CONFIG_X86_32
810b6734c35SH. Peter Anvin 	clear_cpu_cap(c, X86_FEATURE_NOPL);
811366d4a43SBorislav Petkov #else
812366d4a43SBorislav Petkov 	set_cpu_cap(c, X86_FEATURE_NOPL);
813366d4a43SBorislav Petkov #endif
814f7627e25SThomas Gleixner }
815f7627e25SThomas Gleixner 
816148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c)
817f7627e25SThomas Gleixner {
8183da99c97SYinghai Lu 	c->extended_cpuid_level = 0;
819f7627e25SThomas Gleixner 
820aef93c8bSYinghai Lu 	if (!have_cpuid_p())
821aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
822f7627e25SThomas Gleixner 
823aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
824a9853dd6SIngo Molnar 	if (!have_cpuid_p())
825aef93c8bSYinghai Lu 		return;
826aef93c8bSYinghai Lu 
8273da99c97SYinghai Lu 	cpu_detect(c);
8283da99c97SYinghai Lu 
8293da99c97SYinghai Lu 	get_cpu_vendor(c);
8303da99c97SYinghai Lu 
8313da99c97SYinghai Lu 	get_cpu_cap(c);
8323da99c97SYinghai Lu 
833f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
8343da99c97SYinghai Lu 		c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
835b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32
836c8e56d20SBorislav Petkov # ifdef CONFIG_SMP
837cb8cc442SIngo Molnar 		c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
838f7627e25SThomas Gleixner # else
83901aaea1aSYinghai Lu 		c->apicid = c->initial_apicid;
840f7627e25SThomas Gleixner # endif
841b89d3b3eSYinghai Lu #endif
842b89d3b3eSYinghai Lu 		c->phys_proc_id = c->initial_apicid;
843f7627e25SThomas Gleixner 	}
844f7627e25SThomas Gleixner 
845f7627e25SThomas Gleixner 	get_model_name(c); /* Default name */
846f7627e25SThomas Gleixner 
847b6734c35SH. Peter Anvin 	detect_nopl(c);
848f7627e25SThomas Gleixner }
849f7627e25SThomas Gleixner 
850cbc82b17SPeter P Waskiewicz Jr static void x86_init_cache_qos(struct cpuinfo_x86 *c)
851cbc82b17SPeter P Waskiewicz Jr {
852cbc82b17SPeter P Waskiewicz Jr 	/*
853cbc82b17SPeter P Waskiewicz Jr 	 * The heavy lifting of max_rmid and cache_occ_scale are handled
854cbc82b17SPeter P Waskiewicz Jr 	 * in get_cpu_cap().  Here we just set the max_rmid for the boot_cpu
855cbc82b17SPeter P Waskiewicz Jr 	 * in case CQM bits really aren't there in this CPU.
856cbc82b17SPeter P Waskiewicz Jr 	 */
857cbc82b17SPeter P Waskiewicz Jr 	if (c != &boot_cpu_data) {
858cbc82b17SPeter P Waskiewicz Jr 		boot_cpu_data.x86_cache_max_rmid =
859cbc82b17SPeter P Waskiewicz Jr 			min(boot_cpu_data.x86_cache_max_rmid,
860cbc82b17SPeter P Waskiewicz Jr 			    c->x86_cache_max_rmid);
861cbc82b17SPeter P Waskiewicz Jr 	}
862cbc82b17SPeter P Waskiewicz Jr }
863cbc82b17SPeter P Waskiewicz Jr 
864f7627e25SThomas Gleixner /*
865f7627e25SThomas Gleixner  * This does the hard work of actually picking apart the CPU stuff...
866f7627e25SThomas Gleixner  */
867148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c)
868f7627e25SThomas Gleixner {
869f7627e25SThomas Gleixner 	int i;
870f7627e25SThomas Gleixner 
871f7627e25SThomas Gleixner 	c->loops_per_jiffy = loops_per_jiffy;
872f7627e25SThomas Gleixner 	c->x86_cache_size = -1;
873f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
874f7627e25SThomas Gleixner 	c->x86_model = c->x86_mask = 0;	/* So far unknown... */
875f7627e25SThomas Gleixner 	c->x86_vendor_id[0] = '\0'; /* Unset */
876f7627e25SThomas Gleixner 	c->x86_model_id[0] = '\0';  /* Unset */
877f7627e25SThomas Gleixner 	c->x86_max_cores = 1;
878102bbe3aSYinghai Lu 	c->x86_coreid_bits = 0;
87911fdd252SYinghai Lu #ifdef CONFIG_X86_64
880102bbe3aSYinghai Lu 	c->x86_clflush_size = 64;
88113c6c532SJan Beulich 	c->x86_phys_bits = 36;
88213c6c532SJan Beulich 	c->x86_virt_bits = 48;
883102bbe3aSYinghai Lu #else
884102bbe3aSYinghai Lu 	c->cpuid_level = -1;	/* CPUID not detected */
885f7627e25SThomas Gleixner 	c->x86_clflush_size = 32;
88613c6c532SJan Beulich 	c->x86_phys_bits = 32;
88713c6c532SJan Beulich 	c->x86_virt_bits = 32;
888102bbe3aSYinghai Lu #endif
889102bbe3aSYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
890f7627e25SThomas Gleixner 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
891f7627e25SThomas Gleixner 
892f7627e25SThomas Gleixner 	generic_identify(c);
893f7627e25SThomas Gleixner 
8943898534dSAndi Kleen 	if (this_cpu->c_identify)
895f7627e25SThomas Gleixner 		this_cpu->c_identify(c);
896f7627e25SThomas Gleixner 
8972759c328SYinghai Lu 	/* Clear/Set all flags overriden by options, after probe */
8982759c328SYinghai Lu 	for (i = 0; i < NCAPINTS; i++) {
8992759c328SYinghai Lu 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
9002759c328SYinghai Lu 		c->x86_capability[i] |= cpu_caps_set[i];
9012759c328SYinghai Lu 	}
9022759c328SYinghai Lu 
903102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
904cb8cc442SIngo Molnar 	c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
905102bbe3aSYinghai Lu #endif
906102bbe3aSYinghai Lu 
907f7627e25SThomas Gleixner 	/*
908f7627e25SThomas Gleixner 	 * Vendor-specific initialization.  In this section we
909f7627e25SThomas Gleixner 	 * canonicalize the feature flags, meaning if there are
910f7627e25SThomas Gleixner 	 * features a certain CPU supports which CPUID doesn't
911f7627e25SThomas Gleixner 	 * tell us, CPUID claiming incorrect flags, or other bugs,
912f7627e25SThomas Gleixner 	 * we handle them here.
913f7627e25SThomas Gleixner 	 *
914f7627e25SThomas Gleixner 	 * At the end of this section, c->x86_capability better
915f7627e25SThomas Gleixner 	 * indicate the features this CPU genuinely supports!
916f7627e25SThomas Gleixner 	 */
917f7627e25SThomas Gleixner 	if (this_cpu->c_init)
918f7627e25SThomas Gleixner 		this_cpu->c_init(c);
919f7627e25SThomas Gleixner 
920f7627e25SThomas Gleixner 	/* Disable the PN if appropriate */
921f7627e25SThomas Gleixner 	squash_the_stupid_serial_number(c);
922f7627e25SThomas Gleixner 
923b2cc2a07SH. Peter Anvin 	/* Set up SMEP/SMAP */
924b2cc2a07SH. Peter Anvin 	setup_smep(c);
925b2cc2a07SH. Peter Anvin 	setup_smap(c);
926b2cc2a07SH. Peter Anvin 
927f7627e25SThomas Gleixner 	/*
9280f3fa48aSIngo Molnar 	 * The vendor-specific functions might have changed features.
9290f3fa48aSIngo Molnar 	 * Now we do "generic changes."
930f7627e25SThomas Gleixner 	 */
931f7627e25SThomas Gleixner 
932b38b0665SH. Peter Anvin 	/* Filter out anything that depends on CPUID levels we don't have */
933b38b0665SH. Peter Anvin 	filter_cpuid_features(c, true);
934b38b0665SH. Peter Anvin 
935f7627e25SThomas Gleixner 	/* If the model name is still unset, do table lookup. */
936f7627e25SThomas Gleixner 	if (!c->x86_model_id[0]) {
93702dde8b4SJan Beulich 		const char *p;
938f7627e25SThomas Gleixner 		p = table_lookup_model(c);
939f7627e25SThomas Gleixner 		if (p)
940f7627e25SThomas Gleixner 			strcpy(c->x86_model_id, p);
941f7627e25SThomas Gleixner 		else
942f7627e25SThomas Gleixner 			/* Last resort... */
943f7627e25SThomas Gleixner 			sprintf(c->x86_model_id, "%02x/%02x",
944f7627e25SThomas Gleixner 				c->x86, c->x86_model);
945f7627e25SThomas Gleixner 	}
946f7627e25SThomas Gleixner 
947102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
948102bbe3aSYinghai Lu 	detect_ht(c);
949102bbe3aSYinghai Lu #endif
950102bbe3aSYinghai Lu 
95188b094fbSAlok Kataria 	init_hypervisor(c);
95249d859d7SH. Peter Anvin 	x86_init_rdrand(c);
953cbc82b17SPeter P Waskiewicz Jr 	x86_init_cache_qos(c);
9543e0c3737SYinghai Lu 
9553e0c3737SYinghai Lu 	/*
9563e0c3737SYinghai Lu 	 * Clear/Set all flags overriden by options, need do it
9573e0c3737SYinghai Lu 	 * before following smp all cpus cap AND.
9583e0c3737SYinghai Lu 	 */
9593e0c3737SYinghai Lu 	for (i = 0; i < NCAPINTS; i++) {
9603e0c3737SYinghai Lu 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
9613e0c3737SYinghai Lu 		c->x86_capability[i] |= cpu_caps_set[i];
9623e0c3737SYinghai Lu 	}
9633e0c3737SYinghai Lu 
964f7627e25SThomas Gleixner 	/*
965f7627e25SThomas Gleixner 	 * On SMP, boot_cpu_data holds the common feature set between
966f7627e25SThomas Gleixner 	 * all CPUs; so make sure that we indicate which features are
967f7627e25SThomas Gleixner 	 * common between the CPUs.  The first time this routine gets
968f7627e25SThomas Gleixner 	 * executed, c == &boot_cpu_data.
969f7627e25SThomas Gleixner 	 */
970f7627e25SThomas Gleixner 	if (c != &boot_cpu_data) {
971f7627e25SThomas Gleixner 		/* AND the already accumulated flags with these */
972f7627e25SThomas Gleixner 		for (i = 0; i < NCAPINTS; i++)
973f7627e25SThomas Gleixner 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
97465fc985bSBorislav Petkov 
97565fc985bSBorislav Petkov 		/* OR, i.e. replicate the bug flags */
97665fc985bSBorislav Petkov 		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
97765fc985bSBorislav Petkov 			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
978f7627e25SThomas Gleixner 	}
979f7627e25SThomas Gleixner 
980f7627e25SThomas Gleixner 	/* Init Machine Check Exception if available. */
9815e09954aSBorislav Petkov 	mcheck_cpu_init(c);
98230d432dfSAndi Kleen 
98330d432dfSAndi Kleen 	select_idle_routine(c);
984102bbe3aSYinghai Lu 
985de2d9445STejun Heo #ifdef CONFIG_NUMA
986102bbe3aSYinghai Lu 	numa_add_cpu(smp_processor_id());
987102bbe3aSYinghai Lu #endif
988f7627e25SThomas Gleixner }
989f7627e25SThomas Gleixner 
9908b6c0ab1SIngo Molnar /*
9918b6c0ab1SIngo Molnar  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
9928b6c0ab1SIngo Molnar  * on 32-bit kernels:
9938b6c0ab1SIngo Molnar  */
994cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32
995cfda7bb9SAndy Lutomirski void enable_sep_cpu(void)
996cfda7bb9SAndy Lutomirski {
9978b6c0ab1SIngo Molnar 	struct tss_struct *tss;
9988b6c0ab1SIngo Molnar 	int cpu;
999cfda7bb9SAndy Lutomirski 
10008b6c0ab1SIngo Molnar 	cpu = get_cpu();
10018b6c0ab1SIngo Molnar 	tss = &per_cpu(cpu_tss, cpu);
10028b6c0ab1SIngo Molnar 
10038b6c0ab1SIngo Molnar 	if (!boot_cpu_has(X86_FEATURE_SEP))
10048b6c0ab1SIngo Molnar 		goto out;
10058b6c0ab1SIngo Molnar 
10068b6c0ab1SIngo Molnar 	/*
1007cf9328ccSAndy Lutomirski 	 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1008cf9328ccSAndy Lutomirski 	 * see the big comment in struct x86_hw_tss's definition.
10098b6c0ab1SIngo Molnar 	 */
1010cfda7bb9SAndy Lutomirski 
1011cfda7bb9SAndy Lutomirski 	tss->x86_tss.ss1 = __KERNEL_CS;
10128b6c0ab1SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
10138b6c0ab1SIngo Molnar 
1014cf9328ccSAndy Lutomirski 	wrmsr(MSR_IA32_SYSENTER_ESP,
1015cf9328ccSAndy Lutomirski 	      (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
1016cf9328ccSAndy Lutomirski 	      0);
10178b6c0ab1SIngo Molnar 
10184c8cd0c5SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
10198b6c0ab1SIngo Molnar 
10208b6c0ab1SIngo Molnar out:
1021cfda7bb9SAndy Lutomirski 	put_cpu();
1022cfda7bb9SAndy Lutomirski }
1023e04d645fSGlauber Costa #endif
1024e04d645fSGlauber Costa 
1025f7627e25SThomas Gleixner void __init identify_boot_cpu(void)
1026f7627e25SThomas Gleixner {
1027f7627e25SThomas Gleixner 	identify_cpu(&boot_cpu_data);
102802c68a02SLen Brown 	init_amd_e400_c1e_mask();
1029102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1030f7627e25SThomas Gleixner 	sysenter_setup();
1031f7627e25SThomas Gleixner 	enable_sep_cpu();
1032102bbe3aSYinghai Lu #endif
1033e0ba94f1SAlex Shi 	cpu_detect_tlb(&boot_cpu_data);
1034f7627e25SThomas Gleixner }
1035f7627e25SThomas Gleixner 
1036148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c)
1037f7627e25SThomas Gleixner {
1038f7627e25SThomas Gleixner 	BUG_ON(c == &boot_cpu_data);
1039f7627e25SThomas Gleixner 	identify_cpu(c);
1040102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1041f7627e25SThomas Gleixner 	enable_sep_cpu();
1042102bbe3aSYinghai Lu #endif
1043f7627e25SThomas Gleixner 	mtrr_ap_init();
1044f7627e25SThomas Gleixner }
1045f7627e25SThomas Gleixner 
1046a0854a46SYinghai Lu struct msr_range {
1047a0854a46SYinghai Lu 	unsigned	min;
1048a0854a46SYinghai Lu 	unsigned	max;
1049a0854a46SYinghai Lu };
1050a0854a46SYinghai Lu 
1051148f9bb8SPaul Gortmaker static const struct msr_range msr_range_array[] = {
1052a0854a46SYinghai Lu 	{ 0x00000000, 0x00000418},
1053a0854a46SYinghai Lu 	{ 0xc0000000, 0xc000040b},
1054a0854a46SYinghai Lu 	{ 0xc0010000, 0xc0010142},
1055a0854a46SYinghai Lu 	{ 0xc0011000, 0xc001103b},
1056a0854a46SYinghai Lu };
1057a0854a46SYinghai Lu 
1058148f9bb8SPaul Gortmaker static void __print_cpu_msr(void)
1059f7627e25SThomas Gleixner {
10600f3fa48aSIngo Molnar 	unsigned index_min, index_max;
1061a0854a46SYinghai Lu 	unsigned index;
1062a0854a46SYinghai Lu 	u64 val;
1063a0854a46SYinghai Lu 	int i;
1064f7627e25SThomas Gleixner 
1065a0854a46SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
1066a0854a46SYinghai Lu 		index_min = msr_range_array[i].min;
1067a0854a46SYinghai Lu 		index_max = msr_range_array[i].max;
10680f3fa48aSIngo Molnar 
1069a0854a46SYinghai Lu 		for (index = index_min; index < index_max; index++) {
1070ecd431d9SBorislav Petkov 			if (rdmsrl_safe(index, &val))
1071a0854a46SYinghai Lu 				continue;
1072a0854a46SYinghai Lu 			printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
1073f7627e25SThomas Gleixner 		}
1074f7627e25SThomas Gleixner 	}
1075a0854a46SYinghai Lu }
1076a0854a46SYinghai Lu 
1077148f9bb8SPaul Gortmaker static int show_msr;
10780f3fa48aSIngo Molnar 
1079a0854a46SYinghai Lu static __init int setup_show_msr(char *arg)
1080a0854a46SYinghai Lu {
1081a0854a46SYinghai Lu 	int num;
1082a0854a46SYinghai Lu 
1083a0854a46SYinghai Lu 	get_option(&arg, &num);
1084a0854a46SYinghai Lu 
1085a0854a46SYinghai Lu 	if (num > 0)
1086a0854a46SYinghai Lu 		show_msr = num;
1087a0854a46SYinghai Lu 	return 1;
1088a0854a46SYinghai Lu }
1089a0854a46SYinghai Lu __setup("show_msr=", setup_show_msr);
1090f7627e25SThomas Gleixner 
1091191679fdSAndi Kleen static __init int setup_noclflush(char *arg)
1092191679fdSAndi Kleen {
1093840d2830SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1094da4aaa7dSH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1095191679fdSAndi Kleen 	return 1;
1096191679fdSAndi Kleen }
1097191679fdSAndi Kleen __setup("noclflush", setup_noclflush);
1098191679fdSAndi Kleen 
1099148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c)
1100f7627e25SThomas Gleixner {
110102dde8b4SJan Beulich 	const char *vendor = NULL;
1102f7627e25SThomas Gleixner 
11030f3fa48aSIngo Molnar 	if (c->x86_vendor < X86_VENDOR_NUM) {
1104f7627e25SThomas Gleixner 		vendor = this_cpu->c_vendor;
11050f3fa48aSIngo Molnar 	} else {
11060f3fa48aSIngo Molnar 		if (c->cpuid_level >= 0)
1107f7627e25SThomas Gleixner 			vendor = c->x86_vendor_id;
11080f3fa48aSIngo Molnar 	}
1109f7627e25SThomas Gleixner 
1110bd32a8cfSYinghai Lu 	if (vendor && !strstr(c->x86_model_id, vendor))
11119d31d35bSYinghai Lu 		printk(KERN_CONT "%s ", vendor);
1112f7627e25SThomas Gleixner 
11139d31d35bSYinghai Lu 	if (c->x86_model_id[0])
1114adafb98dSPrarit Bhargava 		printk(KERN_CONT "%s", c->x86_model_id);
1115f7627e25SThomas Gleixner 	else
11169d31d35bSYinghai Lu 		printk(KERN_CONT "%d86", c->x86);
1117f7627e25SThomas Gleixner 
11187c5b190eSBorislav Petkov 	printk(KERN_CONT " (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
1119924e101aSBorislav Petkov 
1120f7627e25SThomas Gleixner 	if (c->x86_mask || c->cpuid_level >= 0)
11217c5b190eSBorislav Petkov 		printk(KERN_CONT ", stepping: 0x%x)\n", c->x86_mask);
1122f7627e25SThomas Gleixner 	else
1123924e101aSBorislav Petkov 		printk(KERN_CONT ")\n");
1124a0854a46SYinghai Lu 
11250b8b8078SYinghai Lu 	print_cpu_msr(c);
112621c3fcf3SYinghai Lu }
112721c3fcf3SYinghai Lu 
1128148f9bb8SPaul Gortmaker void print_cpu_msr(struct cpuinfo_x86 *c)
112921c3fcf3SYinghai Lu {
1130a0854a46SYinghai Lu 	if (c->cpu_index < show_msr)
113121c3fcf3SYinghai Lu 		__print_cpu_msr();
1132f7627e25SThomas Gleixner }
1133f7627e25SThomas Gleixner 
1134ac72e788SAndi Kleen static __init int setup_disablecpuid(char *arg)
1135ac72e788SAndi Kleen {
1136ac72e788SAndi Kleen 	int bit;
11370f3fa48aSIngo Molnar 
1138ac72e788SAndi Kleen 	if (get_option(&arg, &bit) && bit < NCAPINTS*32)
1139ac72e788SAndi Kleen 		setup_clear_cpu_cap(bit);
1140ac72e788SAndi Kleen 	else
1141ac72e788SAndi Kleen 		return 0;
11420f3fa48aSIngo Molnar 
1143ac72e788SAndi Kleen 	return 1;
1144ac72e788SAndi Kleen }
1145ac72e788SAndi Kleen __setup("clearcpuid=", setup_disablecpuid);
1146ac72e788SAndi Kleen 
1147d5494d4fSYinghai Lu #ifdef CONFIG_X86_64
11489ff80942SCyrill Gorcunov struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
1149629f4f9dSSeiji Aguchi struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
1150629f4f9dSSeiji Aguchi 				    (unsigned long) debug_idt_table };
1151d5494d4fSYinghai Lu 
1152947e76cdSBrian Gerst DEFINE_PER_CPU_FIRST(union irq_stack_union,
1153277d5b40SAndi Kleen 		     irq_stack_union) __aligned(PAGE_SIZE) __visible;
11540f3fa48aSIngo Molnar 
1155bdf977b3STejun Heo /*
1156a7fcf28dSAndy Lutomirski  * The following percpu variables are hot.  Align current_task to
1157a7fcf28dSAndy Lutomirski  * cacheline size such that they fall in the same cacheline.
1158bdf977b3STejun Heo  */
1159bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1160bdf977b3STejun Heo 	&init_task;
1161bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1162d5494d4fSYinghai Lu 
1163bdf977b3STejun Heo DEFINE_PER_CPU(char *, irq_stack_ptr) =
1164bdf977b3STejun Heo 	init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
1165bdf977b3STejun Heo 
1166277d5b40SAndi Kleen DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
1167d5494d4fSYinghai Lu 
1168c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1169c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1170c2daa3beSPeter Zijlstra 
11710f3fa48aSIngo Molnar /*
11720f3fa48aSIngo Molnar  * Special IST stacks which the CPU switches to when it calls
11730f3fa48aSIngo Molnar  * an IST-marked descriptor entry. Up to 7 stacks (hardware
11740f3fa48aSIngo Molnar  * limit), all of them are 4K, except the debug stack which
11750f3fa48aSIngo Molnar  * is 8K.
11760f3fa48aSIngo Molnar  */
11770f3fa48aSIngo Molnar static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
11780f3fa48aSIngo Molnar 	  [0 ... N_EXCEPTION_STACKS - 1]	= EXCEPTION_STKSZ,
11790f3fa48aSIngo Molnar 	  [DEBUG_STACK - 1]			= DEBUG_STKSZ
11800f3fa48aSIngo Molnar };
11810f3fa48aSIngo Molnar 
118292d65b23SBrian Gerst static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
11833e352aa8STejun Heo 	[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
1184d5494d4fSYinghai Lu 
1185d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */
1186d5494d4fSYinghai Lu void syscall_init(void)
1187d5494d4fSYinghai Lu {
1188d5494d4fSYinghai Lu 	/*
1189d5494d4fSYinghai Lu 	 * LSTAR and STAR live in a bit strange symbiosis.
1190d5494d4fSYinghai Lu 	 * They both write to the same internal register. STAR allows to
1191d5494d4fSYinghai Lu 	 * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
1192d5494d4fSYinghai Lu 	 */
1193d5494d4fSYinghai Lu 	wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32);
119447edb651SAndy Lutomirski 	wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
1195d56fe4bfSIngo Molnar 
1196d56fe4bfSIngo Molnar #ifdef CONFIG_IA32_EMULATION
119747edb651SAndy Lutomirski 	wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
1198a76c7f46SDenys Vlasenko 	/*
1199487d1edbSDenys Vlasenko 	 * This only works on Intel CPUs.
1200487d1edbSDenys Vlasenko 	 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1201487d1edbSDenys Vlasenko 	 * This does not cause SYSENTER to jump to the wrong location, because
1202487d1edbSDenys Vlasenko 	 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1203a76c7f46SDenys Vlasenko 	 */
1204a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1205a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
12064c8cd0c5SIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
1207d56fe4bfSIngo Molnar #else
120847edb651SAndy Lutomirski 	wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
12096b51311cSBorislav Petkov 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1210d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1211d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1212d5494d4fSYinghai Lu #endif
1213d5494d4fSYinghai Lu 
1214d5494d4fSYinghai Lu 	/* Flags to clear on syscall */
1215d5494d4fSYinghai Lu 	wrmsrl(MSR_SYSCALL_MASK,
121663bcff2aSH. Peter Anvin 	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
12178c7aa698SAndy Lutomirski 	       X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1218d5494d4fSYinghai Lu }
1219d5494d4fSYinghai Lu 
1220d5494d4fSYinghai Lu /*
1221d5494d4fSYinghai Lu  * Copies of the original ist values from the tss are only accessed during
1222d5494d4fSYinghai Lu  * debugging, no special alignment required.
1223d5494d4fSYinghai Lu  */
1224d5494d4fSYinghai Lu DEFINE_PER_CPU(struct orig_ist, orig_ist);
1225d5494d4fSYinghai Lu 
1226228bdaa9SSteven Rostedt static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
122742181186SSteven Rostedt DEFINE_PER_CPU(int, debug_stack_usage);
1228228bdaa9SSteven Rostedt 
1229228bdaa9SSteven Rostedt int is_debug_stack(unsigned long addr)
1230228bdaa9SSteven Rostedt {
123189cbc767SChristoph Lameter 	return __this_cpu_read(debug_stack_usage) ||
123289cbc767SChristoph Lameter 		(addr <= __this_cpu_read(debug_stack_addr) &&
123389cbc767SChristoph Lameter 		 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
1234228bdaa9SSteven Rostedt }
12350f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(is_debug_stack);
1236228bdaa9SSteven Rostedt 
1237629f4f9dSSeiji Aguchi DEFINE_PER_CPU(u32, debug_idt_ctr);
1238f8988175SSteven Rostedt 
1239228bdaa9SSteven Rostedt void debug_stack_set_zero(void)
1240228bdaa9SSteven Rostedt {
1241629f4f9dSSeiji Aguchi 	this_cpu_inc(debug_idt_ctr);
1242629f4f9dSSeiji Aguchi 	load_current_idt();
1243228bdaa9SSteven Rostedt }
12440f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_set_zero);
1245228bdaa9SSteven Rostedt 
1246228bdaa9SSteven Rostedt void debug_stack_reset(void)
1247228bdaa9SSteven Rostedt {
1248629f4f9dSSeiji Aguchi 	if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
1249f8988175SSteven Rostedt 		return;
1250629f4f9dSSeiji Aguchi 	if (this_cpu_dec_return(debug_idt_ctr) == 0)
1251629f4f9dSSeiji Aguchi 		load_current_idt();
1252228bdaa9SSteven Rostedt }
12530f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_reset);
1254228bdaa9SSteven Rostedt 
12550f3fa48aSIngo Molnar #else	/* CONFIG_X86_64 */
1256d5494d4fSYinghai Lu 
1257bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1258bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1259c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1260c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1261bdf977b3STejun Heo 
1262a7fcf28dSAndy Lutomirski /*
1263a7fcf28dSAndy Lutomirski  * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1264a7fcf28dSAndy Lutomirski  * the top of the kernel stack.  Use an extra percpu variable to track the
1265a7fcf28dSAndy Lutomirski  * top of the kernel stack directly.
1266a7fcf28dSAndy Lutomirski  */
1267a7fcf28dSAndy Lutomirski DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1268a7fcf28dSAndy Lutomirski 	(unsigned long)&init_thread_union + THREAD_SIZE;
1269a7fcf28dSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1270a7fcf28dSAndy Lutomirski 
127160a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR
127253f82452SJeremy Fitzhardinge DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
127360a5317fSTejun Heo #endif
127460a5317fSTejun Heo 
12750f3fa48aSIngo Molnar #endif	/* CONFIG_X86_64 */
1276f7627e25SThomas Gleixner 
1277f7627e25SThomas Gleixner /*
12789766cdbcSJaswinder Singh Rajput  * Clear all 6 debug registers:
12799766cdbcSJaswinder Singh Rajput  */
12809766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void)
12819766cdbcSJaswinder Singh Rajput {
12829766cdbcSJaswinder Singh Rajput 	int i;
12839766cdbcSJaswinder Singh Rajput 
12849766cdbcSJaswinder Singh Rajput 	for (i = 0; i < 8; i++) {
12859766cdbcSJaswinder Singh Rajput 		/* Ignore db4, db5 */
12869766cdbcSJaswinder Singh Rajput 		if ((i == 4) || (i == 5))
12879766cdbcSJaswinder Singh Rajput 			continue;
12889766cdbcSJaswinder Singh Rajput 
12899766cdbcSJaswinder Singh Rajput 		set_debugreg(0, i);
12909766cdbcSJaswinder Singh Rajput 	}
12919766cdbcSJaswinder Singh Rajput }
1292f7627e25SThomas Gleixner 
12930bb9fef9SJason Wessel #ifdef CONFIG_KGDB
12940bb9fef9SJason Wessel /*
12950bb9fef9SJason Wessel  * Restore debug regs if using kgdbwait and you have a kernel debugger
12960bb9fef9SJason Wessel  * connection established.
12970bb9fef9SJason Wessel  */
12980bb9fef9SJason Wessel static void dbg_restore_debug_regs(void)
12990bb9fef9SJason Wessel {
13000bb9fef9SJason Wessel 	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
13010bb9fef9SJason Wessel 		arch_kgdb_ops.correct_hw_break();
13020bb9fef9SJason Wessel }
13030bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */
13040bb9fef9SJason Wessel #define dbg_restore_debug_regs()
13050bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */
13060bb9fef9SJason Wessel 
1307ce4b1b16SIgor Mammedov static void wait_for_master_cpu(int cpu)
1308ce4b1b16SIgor Mammedov {
1309ce4b1b16SIgor Mammedov #ifdef CONFIG_SMP
1310ce4b1b16SIgor Mammedov 	/*
1311ce4b1b16SIgor Mammedov 	 * wait for ACK from master CPU before continuing
1312ce4b1b16SIgor Mammedov 	 * with AP initialization
1313ce4b1b16SIgor Mammedov 	 */
1314ce4b1b16SIgor Mammedov 	WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1315ce4b1b16SIgor Mammedov 	while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1316ce4b1b16SIgor Mammedov 		cpu_relax();
1317ce4b1b16SIgor Mammedov #endif
1318ce4b1b16SIgor Mammedov }
1319ce4b1b16SIgor Mammedov 
1320f7627e25SThomas Gleixner /*
1321f7627e25SThomas Gleixner  * cpu_init() initializes state that is per-CPU. Some data is already
1322f7627e25SThomas Gleixner  * initialized (naturally) in the bootstrap process, such as the GDT
1323f7627e25SThomas Gleixner  * and IDT. We reload them nevertheless, this function acts as a
1324f7627e25SThomas Gleixner  * 'CPU state barrier', nothing should get across.
13251ba76586SYinghai Lu  * A lot of state is already set up in PDA init for 64 bit
1326f7627e25SThomas Gleixner  */
13271ba76586SYinghai Lu #ifdef CONFIG_X86_64
13280f3fa48aSIngo Molnar 
1329148f9bb8SPaul Gortmaker void cpu_init(void)
13301ba76586SYinghai Lu {
13310fe1e009STejun Heo 	struct orig_ist *oist;
13321ba76586SYinghai Lu 	struct task_struct *me;
13330f3fa48aSIngo Molnar 	struct tss_struct *t;
13340f3fa48aSIngo Molnar 	unsigned long v;
1335ce4b1b16SIgor Mammedov 	int cpu = stack_smp_processor_id();
13361ba76586SYinghai Lu 	int i;
13371ba76586SYinghai Lu 
1338ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1339ce4b1b16SIgor Mammedov 
1340e6ebf5deSFenghua Yu 	/*
13411e02ce4cSAndy Lutomirski 	 * Initialize the CR4 shadow before doing anything that could
13421e02ce4cSAndy Lutomirski 	 * try to read it.
13431e02ce4cSAndy Lutomirski 	 */
13441e02ce4cSAndy Lutomirski 	cr4_init_shadow();
13451e02ce4cSAndy Lutomirski 
13461e02ce4cSAndy Lutomirski 	/*
1347e6ebf5deSFenghua Yu 	 * Load microcode on this cpu if a valid microcode is available.
1348e6ebf5deSFenghua Yu 	 * This is early microcode loading procedure.
1349e6ebf5deSFenghua Yu 	 */
1350e6ebf5deSFenghua Yu 	load_ucode_ap();
1351e6ebf5deSFenghua Yu 
135224933b82SAndy Lutomirski 	t = &per_cpu(cpu_tss, cpu);
13530fe1e009STejun Heo 	oist = &per_cpu(orig_ist, cpu);
13540f3fa48aSIngo Molnar 
1355e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA
135627fd185fSFenghua Yu 	if (this_cpu_read(numa_node) == 0 &&
1357e534c7c5SLee Schermerhorn 	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
1358e534c7c5SLee Schermerhorn 		set_numa_node(early_cpu_to_node(cpu));
1359e7a22c1eSBrian Gerst #endif
13601ba76586SYinghai Lu 
13611ba76586SYinghai Lu 	me = current;
13621ba76586SYinghai Lu 
13632eaad1fdSMike Travis 	pr_debug("Initializing CPU#%d\n", cpu);
13641ba76586SYinghai Lu 
1365375074ccSAndy Lutomirski 	cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
13661ba76586SYinghai Lu 
13671ba76586SYinghai Lu 	/*
13681ba76586SYinghai Lu 	 * Initialize the per-CPU GDT with the boot GDT,
13691ba76586SYinghai Lu 	 * and set up the GDT descriptor:
13701ba76586SYinghai Lu 	 */
13711ba76586SYinghai Lu 
1372552be871SBrian Gerst 	switch_to_new_gdt(cpu);
13732697fbd5SBrian Gerst 	loadsegment(fs, 0);
13742697fbd5SBrian Gerst 
1375cf910e83SSeiji Aguchi 	load_current_idt();
13761ba76586SYinghai Lu 
13771ba76586SYinghai Lu 	memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
13781ba76586SYinghai Lu 	syscall_init();
13791ba76586SYinghai Lu 
13801ba76586SYinghai Lu 	wrmsrl(MSR_FS_BASE, 0);
13811ba76586SYinghai Lu 	wrmsrl(MSR_KERNEL_GS_BASE, 0);
13821ba76586SYinghai Lu 	barrier();
13831ba76586SYinghai Lu 
13844763ed4dSH. Peter Anvin 	x86_configure_nx();
1385659006bfSThomas Gleixner 	x2apic_setup();
13861ba76586SYinghai Lu 
13871ba76586SYinghai Lu 	/*
13881ba76586SYinghai Lu 	 * set up and load the per-CPU TSS
13891ba76586SYinghai Lu 	 */
13900fe1e009STejun Heo 	if (!oist->ist[0]) {
139192d65b23SBrian Gerst 		char *estacks = per_cpu(exception_stacks, cpu);
13920f3fa48aSIngo Molnar 
13931ba76586SYinghai Lu 		for (v = 0; v < N_EXCEPTION_STACKS; v++) {
13940f3fa48aSIngo Molnar 			estacks += exception_stack_sizes[v];
13950fe1e009STejun Heo 			oist->ist[v] = t->x86_tss.ist[v] =
13961ba76586SYinghai Lu 					(unsigned long)estacks;
1397228bdaa9SSteven Rostedt 			if (v == DEBUG_STACK-1)
1398228bdaa9SSteven Rostedt 				per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
13991ba76586SYinghai Lu 		}
14001ba76586SYinghai Lu 	}
14011ba76586SYinghai Lu 
14021ba76586SYinghai Lu 	t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
14030f3fa48aSIngo Molnar 
14041ba76586SYinghai Lu 	/*
14051ba76586SYinghai Lu 	 * <= is required because the CPU will access up to
14061ba76586SYinghai Lu 	 * 8 bits beyond the end of the IO permission bitmap.
14071ba76586SYinghai Lu 	 */
14081ba76586SYinghai Lu 	for (i = 0; i <= IO_BITMAP_LONGS; i++)
14091ba76586SYinghai Lu 		t->io_bitmap[i] = ~0UL;
14101ba76586SYinghai Lu 
14111ba76586SYinghai Lu 	atomic_inc(&init_mm.mm_count);
14121ba76586SYinghai Lu 	me->active_mm = &init_mm;
14138c5dfd25SStoyan Gaydarov 	BUG_ON(me->mm);
14141ba76586SYinghai Lu 	enter_lazy_tlb(&init_mm, me);
14151ba76586SYinghai Lu 
14161ba76586SYinghai Lu 	load_sp0(t, &current->thread);
14171ba76586SYinghai Lu 	set_tss_desc(cpu, t);
14181ba76586SYinghai Lu 	load_TR_desc();
141937868fe1SAndy Lutomirski 	load_mm_ldt(&init_mm);
14201ba76586SYinghai Lu 
14219766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
14220bb9fef9SJason Wessel 	dbg_restore_debug_regs();
14231ba76586SYinghai Lu 
142421c4cd10SIngo Molnar 	fpu__init_cpu();
14251ba76586SYinghai Lu 
14261ba76586SYinghai Lu 	if (is_uv_system())
14271ba76586SYinghai Lu 		uv_cpu_init();
14281ba76586SYinghai Lu }
14291ba76586SYinghai Lu 
14301ba76586SYinghai Lu #else
14311ba76586SYinghai Lu 
1432148f9bb8SPaul Gortmaker void cpu_init(void)
1433f7627e25SThomas Gleixner {
1434f7627e25SThomas Gleixner 	int cpu = smp_processor_id();
1435f7627e25SThomas Gleixner 	struct task_struct *curr = current;
143624933b82SAndy Lutomirski 	struct tss_struct *t = &per_cpu(cpu_tss, cpu);
1437f7627e25SThomas Gleixner 	struct thread_struct *thread = &curr->thread;
1438f7627e25SThomas Gleixner 
1439ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1440e6ebf5deSFenghua Yu 
14415b2bdbc8SSteven Rostedt 	/*
14425b2bdbc8SSteven Rostedt 	 * Initialize the CR4 shadow before doing anything that could
14435b2bdbc8SSteven Rostedt 	 * try to read it.
14445b2bdbc8SSteven Rostedt 	 */
14455b2bdbc8SSteven Rostedt 	cr4_init_shadow();
14465b2bdbc8SSteven Rostedt 
1447ce4b1b16SIgor Mammedov 	show_ucode_info_early();
1448f7627e25SThomas Gleixner 
1449f7627e25SThomas Gleixner 	printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1450f7627e25SThomas Gleixner 
14519298b815SDave Hansen 	if (cpu_feature_enabled(X86_FEATURE_VME) || cpu_has_tsc || cpu_has_de)
1452375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1453f7627e25SThomas Gleixner 
1454cf910e83SSeiji Aguchi 	load_current_idt();
1455552be871SBrian Gerst 	switch_to_new_gdt(cpu);
1456f7627e25SThomas Gleixner 
1457f7627e25SThomas Gleixner 	/*
1458f7627e25SThomas Gleixner 	 * Set up and load the per-CPU TSS and LDT
1459f7627e25SThomas Gleixner 	 */
1460f7627e25SThomas Gleixner 	atomic_inc(&init_mm.mm_count);
1461f7627e25SThomas Gleixner 	curr->active_mm = &init_mm;
14628c5dfd25SStoyan Gaydarov 	BUG_ON(curr->mm);
1463f7627e25SThomas Gleixner 	enter_lazy_tlb(&init_mm, curr);
1464f7627e25SThomas Gleixner 
1465faca6227SH. Peter Anvin 	load_sp0(t, thread);
1466f7627e25SThomas Gleixner 	set_tss_desc(cpu, t);
1467f7627e25SThomas Gleixner 	load_TR_desc();
146837868fe1SAndy Lutomirski 	load_mm_ldt(&init_mm);
1469f7627e25SThomas Gleixner 
1470f9a196b8SThomas Gleixner 	t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1471f9a196b8SThomas Gleixner 
1472f7627e25SThomas Gleixner #ifdef CONFIG_DOUBLEFAULT
1473f7627e25SThomas Gleixner 	/* Set up doublefault TSS pointer in the GDT */
1474f7627e25SThomas Gleixner 	__set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1475f7627e25SThomas Gleixner #endif
1476f7627e25SThomas Gleixner 
14779766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
14780bb9fef9SJason Wessel 	dbg_restore_debug_regs();
1479f7627e25SThomas Gleixner 
148021c4cd10SIngo Molnar 	fpu__init_cpu();
1481f7627e25SThomas Gleixner }
14821ba76586SYinghai Lu #endif
14835700f743SBorislav Petkov 
14845700f743SBorislav Petkov #ifdef CONFIG_X86_DEBUG_STATIC_CPU_HAS
14855700f743SBorislav Petkov void warn_pre_alternatives(void)
14865700f743SBorislav Petkov {
14875700f743SBorislav Petkov 	WARN(1, "You're using static_cpu_has before alternatives have run!\n");
14885700f743SBorislav Petkov }
14895700f743SBorislav Petkov EXPORT_SYMBOL_GPL(warn_pre_alternatives);
14905700f743SBorislav Petkov #endif
14914a90a99cSBorislav Petkov 
14924a90a99cSBorislav Petkov inline bool __static_cpu_has_safe(u16 bit)
14934a90a99cSBorislav Petkov {
14944a90a99cSBorislav Petkov 	return boot_cpu_has(bit);
14954a90a99cSBorislav Petkov }
14964a90a99cSBorislav Petkov EXPORT_SYMBOL_GPL(__static_cpu_has_safe);
1497b51ef52dSLaura Abbott 
1498b51ef52dSLaura Abbott static void bsp_resume(void)
1499b51ef52dSLaura Abbott {
1500b51ef52dSLaura Abbott 	if (this_cpu->c_bsp_resume)
1501b51ef52dSLaura Abbott 		this_cpu->c_bsp_resume(&boot_cpu_data);
1502b51ef52dSLaura Abbott }
1503b51ef52dSLaura Abbott 
1504b51ef52dSLaura Abbott static struct syscore_ops cpu_syscore_ops = {
1505b51ef52dSLaura Abbott 	.resume		= bsp_resume,
1506b51ef52dSLaura Abbott };
1507b51ef52dSLaura Abbott 
1508b51ef52dSLaura Abbott static int __init init_cpu_syscore(void)
1509b51ef52dSLaura Abbott {
1510b51ef52dSLaura Abbott 	register_syscore_ops(&cpu_syscore_ops);
1511b51ef52dSLaura Abbott 	return 0;
1512b51ef52dSLaura Abbott }
1513b51ef52dSLaura Abbott core_initcall(init_cpu_syscore);
1514