xref: /linux/arch/x86/kernel/cpu/common.c (revision 1008c52c09dcb23d93f8e0ea83a6246265d2cce0)
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>
5186f4360SPaul Gortmaker #include <linux/export.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>
1068e21be2SIngo Molnar #include <linux/sched/mm.h>
11e6017571SIngo Molnar #include <linux/sched/clock.h>
129164bb4aSIngo Molnar #include <linux/sched/task.h>
139766cdbcSJaswinder Singh Rajput #include <linux/init.h>
140f46efebSMasami Hiramatsu #include <linux/kprobes.h>
159766cdbcSJaswinder Singh Rajput #include <linux/kgdb.h>
169766cdbcSJaswinder Singh Rajput #include <linux/smp.h>
179766cdbcSJaswinder Singh Rajput #include <linux/io.h>
18b51ef52dSLaura Abbott #include <linux/syscore_ops.h>
199766cdbcSJaswinder Singh Rajput 
209766cdbcSJaswinder Singh Rajput #include <asm/stackprotector.h>
21cdd6c482SIngo Molnar #include <asm/perf_event.h>
22f7627e25SThomas Gleixner #include <asm/mmu_context.h>
2349d859d7SH. Peter Anvin #include <asm/archrandom.h>
249766cdbcSJaswinder Singh Rajput #include <asm/hypervisor.h>
259766cdbcSJaswinder Singh Rajput #include <asm/processor.h>
261e02ce4cSAndy Lutomirski #include <asm/tlbflush.h>
27f649e938SPaul Gortmaker #include <asm/debugreg.h>
289766cdbcSJaswinder Singh Rajput #include <asm/sections.h>
29f40c3300SAndy Lutomirski #include <asm/vsyscall.h>
308bdbd962SAlan Cox #include <linux/topology.h>
318bdbd962SAlan Cox #include <linux/cpumask.h>
329766cdbcSJaswinder Singh Rajput #include <asm/pgtable.h>
3360063497SArun Sharma #include <linux/atomic.h>
349766cdbcSJaswinder Singh Rajput #include <asm/proto.h>
359766cdbcSJaswinder Singh Rajput #include <asm/setup.h>
36f7627e25SThomas Gleixner #include <asm/apic.h>
379766cdbcSJaswinder Singh Rajput #include <asm/desc.h>
3878f7f1e5SIngo Molnar #include <asm/fpu/internal.h>
399766cdbcSJaswinder Singh Rajput #include <asm/mtrr.h>
400274f955SGrzegorz Andrejczuk #include <asm/hwcap2.h>
418bdbd962SAlan Cox #include <linux/numa.h>
429766cdbcSJaswinder Singh Rajput #include <asm/asm.h>
430f6ff2bcSDave Hansen #include <asm/bugs.h>
449766cdbcSJaswinder Singh Rajput #include <asm/cpu.h>
459766cdbcSJaswinder Singh Rajput #include <asm/mce.h>
469766cdbcSJaswinder Singh Rajput #include <asm/msr.h>
479766cdbcSJaswinder Singh Rajput #include <asm/pat.h>
48d288e1cfSFenghua Yu #include <asm/microcode.h>
49d288e1cfSFenghua Yu #include <asm/microcode_intel.h>
50fec9434aSDavid Woodhouse #include <asm/intel-family.h>
51fec9434aSDavid Woodhouse #include <asm/cpu_device_id.h>
52e641f5f5SIngo Molnar 
53f7627e25SThomas Gleixner #ifdef CONFIG_X86_LOCAL_APIC
54bdbcdd48STejun Heo #include <asm/uv/uv.h>
55f7627e25SThomas Gleixner #endif
56f7627e25SThomas Gleixner 
57f7627e25SThomas Gleixner #include "cpu.h"
58f7627e25SThomas Gleixner 
590274f955SGrzegorz Andrejczuk u32 elf_hwcap2 __read_mostly;
600274f955SGrzegorz Andrejczuk 
61c2d1cec1SMike Travis /* all of these masks are initialized in setup_cpu_local_masks() */
62c2d1cec1SMike Travis cpumask_var_t cpu_initialized_mask;
639766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callout_mask;
649766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callin_mask;
65c2d1cec1SMike Travis 
66c2d1cec1SMike Travis /* representing cpus for which sibling maps can be computed */
67c2d1cec1SMike Travis cpumask_var_t cpu_sibling_setup_mask;
68c2d1cec1SMike Travis 
692f2f52baSBrian Gerst /* correctly size the local cpu masks */
704369f1fbSIngo Molnar void __init setup_cpu_local_masks(void)
712f2f52baSBrian Gerst {
722f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_initialized_mask);
732f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callin_mask);
742f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callout_mask);
752f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
762f2f52baSBrian Gerst }
772f2f52baSBrian Gerst 
78148f9bb8SPaul Gortmaker static void default_init(struct cpuinfo_x86 *c)
79e8055139SOndrej Zary {
80e8055139SOndrej Zary #ifdef CONFIG_X86_64
8127c13eceSBorislav Petkov 	cpu_detect_cache_sizes(c);
82e8055139SOndrej Zary #else
83e8055139SOndrej Zary 	/* Not much we can do here... */
84e8055139SOndrej Zary 	/* Check if at least it has cpuid */
85e8055139SOndrej Zary 	if (c->cpuid_level == -1) {
86e8055139SOndrej Zary 		/* No cpuid. It must be an ancient CPU */
87e8055139SOndrej Zary 		if (c->x86 == 4)
88e8055139SOndrej Zary 			strcpy(c->x86_model_id, "486");
89e8055139SOndrej Zary 		else if (c->x86 == 3)
90e8055139SOndrej Zary 			strcpy(c->x86_model_id, "386");
91e8055139SOndrej Zary 	}
92e8055139SOndrej Zary #endif
93e8055139SOndrej Zary }
94e8055139SOndrej Zary 
95148f9bb8SPaul Gortmaker static const struct cpu_dev default_cpu = {
96e8055139SOndrej Zary 	.c_init		= default_init,
97e8055139SOndrej Zary 	.c_vendor	= "Unknown",
98e8055139SOndrej Zary 	.c_x86_vendor	= X86_VENDOR_UNKNOWN,
99e8055139SOndrej Zary };
100e8055139SOndrej Zary 
101148f9bb8SPaul Gortmaker static const struct cpu_dev *this_cpu = &default_cpu;
1020a488a53SYinghai Lu 
10306deef89SBrian Gerst DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
104950ad7ffSYinghai Lu #ifdef CONFIG_X86_64
10506deef89SBrian Gerst 	/*
10606deef89SBrian Gerst 	 * We need valid kernel segments for data and code in long mode too
107950ad7ffSYinghai Lu 	 * IRET will check the segment types  kkeil 2000/10/28
108950ad7ffSYinghai Lu 	 * Also sysret mandates a special GDT layout
10906deef89SBrian Gerst 	 *
1109766cdbcSJaswinder Singh Rajput 	 * TLS descriptors are currently at a different place compared to i386.
11106deef89SBrian Gerst 	 * Hopefully nobody expects them at a fixed place (Wine?)
112950ad7ffSYinghai Lu 	 */
1131e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL32_CS]		= GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
1141e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
1151e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
1161e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER32_CS]	= GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
1171e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
1181e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
119950ad7ffSYinghai Lu #else
1201e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
1211e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1221e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
1231e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
124f7627e25SThomas Gleixner 	/*
125f7627e25SThomas Gleixner 	 * Segments used for calling PnP BIOS have byte granularity.
126f7627e25SThomas Gleixner 	 * They code segments and data segments have fixed 64k limits,
127f7627e25SThomas Gleixner 	 * the transfer segment sizes are set at run time.
128f7627e25SThomas Gleixner 	 */
1296842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1301e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS32]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
1316842ef0eSGlauber de Oliveira Costa 	/* 16-bit code */
1321e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS16]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1336842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1341e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_DS]		= GDT_ENTRY_INIT(0x0092, 0, 0xffff),
1356842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1361e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS1]		= GDT_ENTRY_INIT(0x0092, 0, 0),
1376842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1381e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS2]		= GDT_ENTRY_INIT(0x0092, 0, 0),
139f7627e25SThomas Gleixner 	/*
140f7627e25SThomas Gleixner 	 * The APM segments have byte granularity and their bases
141f7627e25SThomas Gleixner 	 * are set at run time.  All have 64k limits.
142f7627e25SThomas Gleixner 	 */
1436842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1441e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
145f7627e25SThomas Gleixner 	/* 16-bit code */
1461e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE+1]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1476842ef0eSGlauber de Oliveira Costa 	/* data */
14872c4d853SIngo Molnar 	[GDT_ENTRY_APMBIOS_BASE+2]	= GDT_ENTRY_INIT(0x4092, 0, 0xffff),
149f7627e25SThomas Gleixner 
1501e5de182SAkinobu Mita 	[GDT_ENTRY_ESPFIX_SS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1511e5de182SAkinobu Mita 	[GDT_ENTRY_PERCPU]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
15260a5317fSTejun Heo 	GDT_STACK_CANARY_INIT
153950ad7ffSYinghai Lu #endif
15406deef89SBrian Gerst } };
155f7627e25SThomas Gleixner EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
156f7627e25SThomas Gleixner 
1578c3641e9SDave Hansen static int __init x86_mpx_setup(char *s)
1580c752a93SSuresh Siddha {
1598c3641e9SDave Hansen 	/* require an exact match without trailing characters */
1602cd3949fSDave Hansen 	if (strlen(s))
1612cd3949fSDave Hansen 		return 0;
1620c752a93SSuresh Siddha 
1638c3641e9SDave Hansen 	/* do not emit a message if the feature is not present */
1648c3641e9SDave Hansen 	if (!boot_cpu_has(X86_FEATURE_MPX))
1656bad06b7SSuresh Siddha 		return 1;
1666bad06b7SSuresh Siddha 
1678c3641e9SDave Hansen 	setup_clear_cpu_cap(X86_FEATURE_MPX);
1688c3641e9SDave Hansen 	pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
169b6f42a4aSFenghua Yu 	return 1;
170b6f42a4aSFenghua Yu }
1718c3641e9SDave Hansen __setup("nompx", x86_mpx_setup);
172b6f42a4aSFenghua Yu 
1730790c9aaSAndy Lutomirski #ifdef CONFIG_X86_64
174c7ad5ad2SAndy Lutomirski static int __init x86_nopcid_setup(char *s)
1750790c9aaSAndy Lutomirski {
176c7ad5ad2SAndy Lutomirski 	/* nopcid doesn't accept parameters */
177c7ad5ad2SAndy Lutomirski 	if (s)
178c7ad5ad2SAndy Lutomirski 		return -EINVAL;
1790790c9aaSAndy Lutomirski 
1800790c9aaSAndy Lutomirski 	/* do not emit a message if the feature is not present */
1810790c9aaSAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_PCID))
182c7ad5ad2SAndy Lutomirski 		return 0;
1830790c9aaSAndy Lutomirski 
1840790c9aaSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_PCID);
1850790c9aaSAndy Lutomirski 	pr_info("nopcid: PCID feature disabled\n");
186c7ad5ad2SAndy Lutomirski 	return 0;
1870790c9aaSAndy Lutomirski }
188c7ad5ad2SAndy Lutomirski early_param("nopcid", x86_nopcid_setup);
1890790c9aaSAndy Lutomirski #endif
1900790c9aaSAndy Lutomirski 
191d12a72b8SAndy Lutomirski static int __init x86_noinvpcid_setup(char *s)
192d12a72b8SAndy Lutomirski {
193d12a72b8SAndy Lutomirski 	/* noinvpcid doesn't accept parameters */
194d12a72b8SAndy Lutomirski 	if (s)
195d12a72b8SAndy Lutomirski 		return -EINVAL;
196d12a72b8SAndy Lutomirski 
197d12a72b8SAndy Lutomirski 	/* do not emit a message if the feature is not present */
198d12a72b8SAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_INVPCID))
199d12a72b8SAndy Lutomirski 		return 0;
200d12a72b8SAndy Lutomirski 
201d12a72b8SAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_INVPCID);
202d12a72b8SAndy Lutomirski 	pr_info("noinvpcid: INVPCID feature disabled\n");
203d12a72b8SAndy Lutomirski 	return 0;
204d12a72b8SAndy Lutomirski }
205d12a72b8SAndy Lutomirski early_param("noinvpcid", x86_noinvpcid_setup);
206d12a72b8SAndy Lutomirski 
207ba51dcedSYinghai Lu #ifdef CONFIG_X86_32
208148f9bb8SPaul Gortmaker static int cachesize_override = -1;
209148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1;
210f7627e25SThomas Gleixner 
211f7627e25SThomas Gleixner static int __init cachesize_setup(char *str)
212f7627e25SThomas Gleixner {
213f7627e25SThomas Gleixner 	get_option(&str, &cachesize_override);
214f7627e25SThomas Gleixner 	return 1;
215f7627e25SThomas Gleixner }
216f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup);
217f7627e25SThomas Gleixner 
218f7627e25SThomas Gleixner static int __init x86_sep_setup(char *s)
219f7627e25SThomas Gleixner {
22013530257SAndi Kleen 	setup_clear_cpu_cap(X86_FEATURE_SEP);
221f7627e25SThomas Gleixner 	return 1;
222f7627e25SThomas Gleixner }
223f7627e25SThomas Gleixner __setup("nosep", x86_sep_setup);
224f7627e25SThomas Gleixner 
225f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */
226f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag)
227f7627e25SThomas Gleixner {
228f7627e25SThomas Gleixner 	u32 f1, f2;
229f7627e25SThomas Gleixner 
23094f6bac1SKrzysztof Helt 	/*
23194f6bac1SKrzysztof Helt 	 * Cyrix and IDT cpus allow disabling of CPUID
23294f6bac1SKrzysztof Helt 	 * so the code below may return different results
23394f6bac1SKrzysztof Helt 	 * when it is executed before and after enabling
23494f6bac1SKrzysztof Helt 	 * the CPUID. Add "volatile" to not allow gcc to
23594f6bac1SKrzysztof Helt 	 * optimize the subsequent calls to this function.
23694f6bac1SKrzysztof Helt 	 */
23794f6bac1SKrzysztof Helt 	asm volatile ("pushfl		\n\t"
238f7627e25SThomas Gleixner 		      "pushfl		\n\t"
239f7627e25SThomas Gleixner 		      "popl %0		\n\t"
240f7627e25SThomas Gleixner 		      "movl %0, %1	\n\t"
241f7627e25SThomas Gleixner 		      "xorl %2, %0	\n\t"
242f7627e25SThomas Gleixner 		      "pushl %0		\n\t"
243f7627e25SThomas Gleixner 		      "popfl		\n\t"
244f7627e25SThomas Gleixner 		      "pushfl		\n\t"
245f7627e25SThomas Gleixner 		      "popl %0		\n\t"
246f7627e25SThomas Gleixner 		      "popfl		\n\t"
2470f3fa48aSIngo Molnar 
248f7627e25SThomas Gleixner 		      : "=&r" (f1), "=&r" (f2)
249f7627e25SThomas Gleixner 		      : "ir" (flag));
250f7627e25SThomas Gleixner 
251f7627e25SThomas Gleixner 	return ((f1^f2) & flag) != 0;
252f7627e25SThomas Gleixner }
253f7627e25SThomas Gleixner 
254f7627e25SThomas Gleixner /* Probe for the CPUID instruction */
255148f9bb8SPaul Gortmaker int have_cpuid_p(void)
256f7627e25SThomas Gleixner {
257f7627e25SThomas Gleixner 	return flag_is_changeable_p(X86_EFLAGS_ID);
258f7627e25SThomas Gleixner }
259f7627e25SThomas Gleixner 
260148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
2610a488a53SYinghai Lu {
2620a488a53SYinghai Lu 	unsigned long lo, hi;
2630f3fa48aSIngo Molnar 
2640f3fa48aSIngo Molnar 	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
2650f3fa48aSIngo Molnar 		return;
2660f3fa48aSIngo Molnar 
2670f3fa48aSIngo Molnar 	/* Disable processor serial number: */
2680f3fa48aSIngo Molnar 
2690a488a53SYinghai Lu 	rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2700a488a53SYinghai Lu 	lo |= 0x200000;
2710a488a53SYinghai Lu 	wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2720f3fa48aSIngo Molnar 
2731b74dde7SChen Yucong 	pr_notice("CPU serial number disabled.\n");
2740a488a53SYinghai Lu 	clear_cpu_cap(c, X86_FEATURE_PN);
2750a488a53SYinghai Lu 
2760a488a53SYinghai Lu 	/* Disabling the serial number may affect the cpuid level */
2770a488a53SYinghai Lu 	c->cpuid_level = cpuid_eax(0);
2780a488a53SYinghai Lu }
2790a488a53SYinghai Lu 
2800a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s)
2810a488a53SYinghai Lu {
2820a488a53SYinghai Lu 	disable_x86_serial_nr = 0;
2830a488a53SYinghai Lu 	return 1;
2840a488a53SYinghai Lu }
2850a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup);
286ba51dcedSYinghai Lu #else
287102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag)
288102bbe3aSYinghai Lu {
289102bbe3aSYinghai Lu 	return 1;
290102bbe3aSYinghai Lu }
291102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
292102bbe3aSYinghai Lu {
293102bbe3aSYinghai Lu }
294ba51dcedSYinghai Lu #endif
2950a488a53SYinghai Lu 
296de5397adSFenghua Yu static __init int setup_disable_smep(char *arg)
297de5397adSFenghua Yu {
298b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMEP);
2990f6ff2bcSDave Hansen 	/* Check for things that depend on SMEP being enabled: */
3000f6ff2bcSDave Hansen 	check_mpx_erratum(&boot_cpu_data);
301de5397adSFenghua Yu 	return 1;
302de5397adSFenghua Yu }
303de5397adSFenghua Yu __setup("nosmep", setup_disable_smep);
304de5397adSFenghua Yu 
305b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c)
306de5397adSFenghua Yu {
307b2cc2a07SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMEP))
308375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMEP);
309de5397adSFenghua Yu }
310de5397adSFenghua Yu 
31152b6179aSH. Peter Anvin static __init int setup_disable_smap(char *arg)
31252b6179aSH. Peter Anvin {
313b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMAP);
31452b6179aSH. Peter Anvin 	return 1;
31552b6179aSH. Peter Anvin }
31652b6179aSH. Peter Anvin __setup("nosmap", setup_disable_smap);
31752b6179aSH. Peter Anvin 
318b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c)
31952b6179aSH. Peter Anvin {
320581b7f15SAndrew Cooper 	unsigned long eflags = native_save_fl();
321b2cc2a07SH. Peter Anvin 
322b2cc2a07SH. Peter Anvin 	/* This should have been cleared long ago */
323b2cc2a07SH. Peter Anvin 	BUG_ON(eflags & X86_EFLAGS_AC);
324b2cc2a07SH. Peter Anvin 
32503bbd596SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMAP)) {
32603bbd596SH. Peter Anvin #ifdef CONFIG_X86_SMAP
327375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMAP);
32803bbd596SH. Peter Anvin #else
329375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_SMAP);
33003bbd596SH. Peter Anvin #endif
33103bbd596SH. Peter Anvin 	}
332f7627e25SThomas Gleixner }
333f7627e25SThomas Gleixner 
334aa35f896SRicardo Neri static __always_inline void setup_umip(struct cpuinfo_x86 *c)
335aa35f896SRicardo Neri {
336aa35f896SRicardo Neri 	/* Check the boot processor, plus build option for UMIP. */
337aa35f896SRicardo Neri 	if (!cpu_feature_enabled(X86_FEATURE_UMIP))
338aa35f896SRicardo Neri 		goto out;
339aa35f896SRicardo Neri 
340aa35f896SRicardo Neri 	/* Check the current processor's cpuid bits. */
341aa35f896SRicardo Neri 	if (!cpu_has(c, X86_FEATURE_UMIP))
342aa35f896SRicardo Neri 		goto out;
343aa35f896SRicardo Neri 
344aa35f896SRicardo Neri 	cr4_set_bits(X86_CR4_UMIP);
345aa35f896SRicardo Neri 
346770c7755SRicardo Neri 	pr_info("x86/cpu: Activated the Intel User Mode Instruction Prevention (UMIP) CPU feature\n");
347770c7755SRicardo Neri 
348aa35f896SRicardo Neri 	return;
349aa35f896SRicardo Neri 
350aa35f896SRicardo Neri out:
351aa35f896SRicardo Neri 	/*
352aa35f896SRicardo Neri 	 * Make sure UMIP is disabled in case it was enabled in a
353aa35f896SRicardo Neri 	 * previous boot (e.g., via kexec).
354aa35f896SRicardo Neri 	 */
355aa35f896SRicardo Neri 	cr4_clear_bits(X86_CR4_UMIP);
356aa35f896SRicardo Neri }
357aa35f896SRicardo Neri 
358f7627e25SThomas Gleixner /*
35906976945SDave Hansen  * Protection Keys are not available in 32-bit mode.
36006976945SDave Hansen  */
36106976945SDave Hansen static bool pku_disabled;
36206976945SDave Hansen 
36306976945SDave Hansen static __always_inline void setup_pku(struct cpuinfo_x86 *c)
36406976945SDave Hansen {
365e8df1a95SDave Hansen 	/* check the boot processor, plus compile options for PKU: */
366e8df1a95SDave Hansen 	if (!cpu_feature_enabled(X86_FEATURE_PKU))
367e8df1a95SDave Hansen 		return;
368e8df1a95SDave Hansen 	/* checks the actual processor's cpuid bits: */
36906976945SDave Hansen 	if (!cpu_has(c, X86_FEATURE_PKU))
37006976945SDave Hansen 		return;
37106976945SDave Hansen 	if (pku_disabled)
37206976945SDave Hansen 		return;
37306976945SDave Hansen 
37406976945SDave Hansen 	cr4_set_bits(X86_CR4_PKE);
37506976945SDave Hansen 	/*
37606976945SDave Hansen 	 * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
37706976945SDave Hansen 	 * cpuid bit to be set.  We need to ensure that we
37806976945SDave Hansen 	 * update that bit in this CPU's "cpu_info".
37906976945SDave Hansen 	 */
38006976945SDave Hansen 	get_cpu_cap(c);
38106976945SDave Hansen }
38206976945SDave Hansen 
38306976945SDave Hansen #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
38406976945SDave Hansen static __init int setup_disable_pku(char *arg)
38506976945SDave Hansen {
38606976945SDave Hansen 	/*
38706976945SDave Hansen 	 * Do not clear the X86_FEATURE_PKU bit.  All of the
38806976945SDave Hansen 	 * runtime checks are against OSPKE so clearing the
38906976945SDave Hansen 	 * bit does nothing.
39006976945SDave Hansen 	 *
39106976945SDave Hansen 	 * This way, we will see "pku" in cpuinfo, but not
39206976945SDave Hansen 	 * "ospke", which is exactly what we want.  It shows
39306976945SDave Hansen 	 * that the CPU has PKU, but the OS has not enabled it.
39406976945SDave Hansen 	 * This happens to be exactly how a system would look
39506976945SDave Hansen 	 * if we disabled the config option.
39606976945SDave Hansen 	 */
39706976945SDave Hansen 	pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
39806976945SDave Hansen 	pku_disabled = true;
39906976945SDave Hansen 	return 1;
40006976945SDave Hansen }
40106976945SDave Hansen __setup("nopku", setup_disable_pku);
40206976945SDave Hansen #endif /* CONFIG_X86_64 */
40306976945SDave Hansen 
40406976945SDave Hansen /*
405b38b0665SH. Peter Anvin  * Some CPU features depend on higher CPUID levels, which may not always
406b38b0665SH. Peter Anvin  * be available due to CPUID level capping or broken virtualization
407b38b0665SH. Peter Anvin  * software.  Add those features to this table to auto-disable them.
408b38b0665SH. Peter Anvin  */
409b38b0665SH. Peter Anvin struct cpuid_dependent_feature {
410b38b0665SH. Peter Anvin 	u32 feature;
411b38b0665SH. Peter Anvin 	u32 level;
412b38b0665SH. Peter Anvin };
4130f3fa48aSIngo Molnar 
414148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature
415b38b0665SH. Peter Anvin cpuid_dependent_features[] = {
416b38b0665SH. Peter Anvin 	{ X86_FEATURE_MWAIT,		0x00000005 },
417b38b0665SH. Peter Anvin 	{ X86_FEATURE_DCA,		0x00000009 },
418b38b0665SH. Peter Anvin 	{ X86_FEATURE_XSAVE,		0x0000000d },
419b38b0665SH. Peter Anvin 	{ 0, 0 }
420b38b0665SH. Peter Anvin };
421b38b0665SH. Peter Anvin 
422148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
423b38b0665SH. Peter Anvin {
424b38b0665SH. Peter Anvin 	const struct cpuid_dependent_feature *df;
4259766cdbcSJaswinder Singh Rajput 
426b38b0665SH. Peter Anvin 	for (df = cpuid_dependent_features; df->feature; df++) {
4270f3fa48aSIngo Molnar 
4280f3fa48aSIngo Molnar 		if (!cpu_has(c, df->feature))
4290f3fa48aSIngo Molnar 			continue;
430b38b0665SH. Peter Anvin 		/*
431b38b0665SH. Peter Anvin 		 * Note: cpuid_level is set to -1 if unavailable, but
432b38b0665SH. Peter Anvin 		 * extended_extended_level is set to 0 if unavailable
433b38b0665SH. Peter Anvin 		 * and the legitimate extended levels are all negative
434b38b0665SH. Peter Anvin 		 * when signed; hence the weird messing around with
435b38b0665SH. Peter Anvin 		 * signs here...
436b38b0665SH. Peter Anvin 		 */
4370f3fa48aSIngo Molnar 		if (!((s32)df->level < 0 ?
438f6db44dfSYinghai Lu 		     (u32)df->level > (u32)c->extended_cpuid_level :
4390f3fa48aSIngo Molnar 		     (s32)df->level > (s32)c->cpuid_level))
4400f3fa48aSIngo Molnar 			continue;
4410f3fa48aSIngo Molnar 
442b38b0665SH. Peter Anvin 		clear_cpu_cap(c, df->feature);
4430f3fa48aSIngo Molnar 		if (!warn)
4440f3fa48aSIngo Molnar 			continue;
4450f3fa48aSIngo Molnar 
4461b74dde7SChen Yucong 		pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
4479def39beSJosh Triplett 			x86_cap_flag(df->feature), df->level);
448b38b0665SH. Peter Anvin 	}
449b38b0665SH. Peter Anvin }
450b38b0665SH. Peter Anvin 
451b38b0665SH. Peter Anvin /*
452f7627e25SThomas Gleixner  * Naming convention should be: <Name> [(<Codename>)]
453f7627e25SThomas Gleixner  * This table only is used unless init_<vendor>() below doesn't set it;
4540f3fa48aSIngo Molnar  * in particular, if CPUID levels 0x80000002..4 are supported, this
4550f3fa48aSIngo Molnar  * isn't used
456f7627e25SThomas Gleixner  */
457f7627e25SThomas Gleixner 
458f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */
459148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c)
460f7627e25SThomas Gleixner {
46109dc68d9SJan Beulich #ifdef CONFIG_X86_32
46209dc68d9SJan Beulich 	const struct legacy_cpu_model_info *info;
463f7627e25SThomas Gleixner 
464f7627e25SThomas Gleixner 	if (c->x86_model >= 16)
465f7627e25SThomas Gleixner 		return NULL;	/* Range check */
466f7627e25SThomas Gleixner 
467f7627e25SThomas Gleixner 	if (!this_cpu)
468f7627e25SThomas Gleixner 		return NULL;
469f7627e25SThomas Gleixner 
47009dc68d9SJan Beulich 	info = this_cpu->legacy_models;
471f7627e25SThomas Gleixner 
47209dc68d9SJan Beulich 	while (info->family) {
473f7627e25SThomas Gleixner 		if (info->family == c->x86)
474f7627e25SThomas Gleixner 			return info->model_names[c->x86_model];
475f7627e25SThomas Gleixner 		info++;
476f7627e25SThomas Gleixner 	}
47709dc68d9SJan Beulich #endif
478f7627e25SThomas Gleixner 	return NULL;		/* Not found */
479f7627e25SThomas Gleixner }
480f7627e25SThomas Gleixner 
4816cbd2171SThomas Gleixner __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS];
4826cbd2171SThomas Gleixner __u32 cpu_caps_set[NCAPINTS + NBUGINTS];
483f7627e25SThomas Gleixner 
48411e3a840SJeremy Fitzhardinge void load_percpu_segment(int cpu)
4859d31d35bSYinghai Lu {
486fab334c1SYinghai Lu #ifdef CONFIG_X86_32
4872697fbd5SBrian Gerst 	loadsegment(fs, __KERNEL_PERCPU);
4882697fbd5SBrian Gerst #else
48945e876f7SAndy Lutomirski 	__loadsegment_simple(gs, 0);
4902697fbd5SBrian Gerst 	wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
491fab334c1SYinghai Lu #endif
49260a5317fSTejun Heo 	load_stack_canary_segment();
4939d31d35bSYinghai Lu }
4949d31d35bSYinghai Lu 
49572f5e08dSAndy Lutomirski #ifdef CONFIG_X86_32
49672f5e08dSAndy Lutomirski /* The 32-bit entry code needs to find cpu_entry_area. */
49772f5e08dSAndy Lutomirski DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
49872f5e08dSAndy Lutomirski #endif
49972f5e08dSAndy Lutomirski 
50040e7f949SAndy Lutomirski #ifdef CONFIG_X86_64
50140e7f949SAndy Lutomirski /*
50240e7f949SAndy Lutomirski  * Special IST stacks which the CPU switches to when it calls
50340e7f949SAndy Lutomirski  * an IST-marked descriptor entry. Up to 7 stacks (hardware
50440e7f949SAndy Lutomirski  * limit), all of them are 4K, except the debug stack which
50540e7f949SAndy Lutomirski  * is 8K.
50640e7f949SAndy Lutomirski  */
50740e7f949SAndy Lutomirski static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
50840e7f949SAndy Lutomirski 	  [0 ... N_EXCEPTION_STACKS - 1]	= EXCEPTION_STKSZ,
50940e7f949SAndy Lutomirski 	  [DEBUG_STACK - 1]			= DEBUG_STKSZ
51040e7f949SAndy Lutomirski };
51140e7f949SAndy Lutomirski #endif
51240e7f949SAndy Lutomirski 
51345fc8757SThomas Garnier /* Load the original GDT from the per-cpu structure */
51445fc8757SThomas Garnier void load_direct_gdt(int cpu)
51545fc8757SThomas Garnier {
51645fc8757SThomas Garnier 	struct desc_ptr gdt_descr;
51745fc8757SThomas Garnier 
51845fc8757SThomas Garnier 	gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
51945fc8757SThomas Garnier 	gdt_descr.size = GDT_SIZE - 1;
52045fc8757SThomas Garnier 	load_gdt(&gdt_descr);
52145fc8757SThomas Garnier }
52245fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_direct_gdt);
52345fc8757SThomas Garnier 
52469218e47SThomas Garnier /* Load a fixmap remapping of the per-cpu GDT */
52569218e47SThomas Garnier void load_fixmap_gdt(int cpu)
52669218e47SThomas Garnier {
52769218e47SThomas Garnier 	struct desc_ptr gdt_descr;
52869218e47SThomas Garnier 
52969218e47SThomas Garnier 	gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
53069218e47SThomas Garnier 	gdt_descr.size = GDT_SIZE - 1;
53169218e47SThomas Garnier 	load_gdt(&gdt_descr);
53269218e47SThomas Garnier }
53345fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_fixmap_gdt);
53469218e47SThomas Garnier 
5350f3fa48aSIngo Molnar /*
5360f3fa48aSIngo Molnar  * Current gdt points %fs at the "master" per-cpu area: after this,
5370f3fa48aSIngo Molnar  * it's on the real one.
5380f3fa48aSIngo Molnar  */
539552be871SBrian Gerst void switch_to_new_gdt(int cpu)
540f7627e25SThomas Gleixner {
54145fc8757SThomas Garnier 	/* Load the original GDT */
54245fc8757SThomas Garnier 	load_direct_gdt(cpu);
543f7627e25SThomas Gleixner 	/* Reload the per-cpu base */
54411e3a840SJeremy Fitzhardinge 	load_percpu_segment(cpu);
545f7627e25SThomas Gleixner }
546f7627e25SThomas Gleixner 
547148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
548f7627e25SThomas Gleixner 
549148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c)
550f7627e25SThomas Gleixner {
551f7627e25SThomas Gleixner 	unsigned int *v;
552ee098e1aSBorislav Petkov 	char *p, *q, *s;
553f7627e25SThomas Gleixner 
5543da99c97SYinghai Lu 	if (c->extended_cpuid_level < 0x80000004)
5551b05d60dSYinghai Lu 		return;
556f7627e25SThomas Gleixner 
557f7627e25SThomas Gleixner 	v = (unsigned int *)c->x86_model_id;
558f7627e25SThomas Gleixner 	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
559f7627e25SThomas Gleixner 	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
560f7627e25SThomas Gleixner 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
561f7627e25SThomas Gleixner 	c->x86_model_id[48] = 0;
562f7627e25SThomas Gleixner 
563ee098e1aSBorislav Petkov 	/* Trim whitespace */
564ee098e1aSBorislav Petkov 	p = q = s = &c->x86_model_id[0];
565ee098e1aSBorislav Petkov 
566ee098e1aSBorislav Petkov 	while (*p == ' ')
567ee098e1aSBorislav Petkov 		p++;
568ee098e1aSBorislav Petkov 
569ee098e1aSBorislav Petkov 	while (*p) {
570ee098e1aSBorislav Petkov 		/* Note the last non-whitespace index */
571ee098e1aSBorislav Petkov 		if (!isspace(*p))
572ee098e1aSBorislav Petkov 			s = q;
573ee098e1aSBorislav Petkov 
574ee098e1aSBorislav Petkov 		*q++ = *p++;
575ee098e1aSBorislav Petkov 	}
576ee098e1aSBorislav Petkov 
577ee098e1aSBorislav Petkov 	*(s + 1) = '\0';
578f7627e25SThomas Gleixner }
579f7627e25SThomas Gleixner 
580148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
581f7627e25SThomas Gleixner {
5829d31d35bSYinghai Lu 	unsigned int n, dummy, ebx, ecx, edx, l2size;
583f7627e25SThomas Gleixner 
5843da99c97SYinghai Lu 	n = c->extended_cpuid_level;
585f7627e25SThomas Gleixner 
586f7627e25SThomas Gleixner 	if (n >= 0x80000005) {
5879d31d35bSYinghai Lu 		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
588f7627e25SThomas Gleixner 		c->x86_cache_size = (ecx>>24) + (edx>>24);
589140fc727SYinghai Lu #ifdef CONFIG_X86_64
590140fc727SYinghai Lu 		/* On K8 L1 TLB is inclusive, so don't count it */
591140fc727SYinghai Lu 		c->x86_tlbsize = 0;
592140fc727SYinghai Lu #endif
593f7627e25SThomas Gleixner 	}
594f7627e25SThomas Gleixner 
595f7627e25SThomas Gleixner 	if (n < 0x80000006)	/* Some chips just has a large L1. */
596f7627e25SThomas Gleixner 		return;
597f7627e25SThomas Gleixner 
5980a488a53SYinghai Lu 	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
599f7627e25SThomas Gleixner 	l2size = ecx >> 16;
600f7627e25SThomas Gleixner 
601140fc727SYinghai Lu #ifdef CONFIG_X86_64
602140fc727SYinghai Lu 	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
603140fc727SYinghai Lu #else
604f7627e25SThomas Gleixner 	/* do processor-specific cache resizing */
60509dc68d9SJan Beulich 	if (this_cpu->legacy_cache_size)
60609dc68d9SJan Beulich 		l2size = this_cpu->legacy_cache_size(c, l2size);
607f7627e25SThomas Gleixner 
608f7627e25SThomas Gleixner 	/* Allow user to override all this if necessary. */
609f7627e25SThomas Gleixner 	if (cachesize_override != -1)
610f7627e25SThomas Gleixner 		l2size = cachesize_override;
611f7627e25SThomas Gleixner 
612f7627e25SThomas Gleixner 	if (l2size == 0)
613f7627e25SThomas Gleixner 		return;		/* Again, no L2 cache is possible */
614140fc727SYinghai Lu #endif
615f7627e25SThomas Gleixner 
616f7627e25SThomas Gleixner 	c->x86_cache_size = l2size;
617f7627e25SThomas Gleixner }
618f7627e25SThomas Gleixner 
619e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO];
620e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO];
621e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO];
622e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO];
623e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO];
624e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO];
625dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO];
626e0ba94f1SAlex Shi 
627f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c)
628e0ba94f1SAlex Shi {
629e0ba94f1SAlex Shi 	if (this_cpu->c_detect_tlb)
630e0ba94f1SAlex Shi 		this_cpu->c_detect_tlb(c);
631e0ba94f1SAlex Shi 
632f94fe119SSteven Honeyman 	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
633e0ba94f1SAlex Shi 		tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
634f94fe119SSteven Honeyman 		tlb_lli_4m[ENTRIES]);
635f94fe119SSteven Honeyman 
636f94fe119SSteven Honeyman 	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
637f94fe119SSteven Honeyman 		tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
638f94fe119SSteven Honeyman 		tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
639e0ba94f1SAlex Shi }
640e0ba94f1SAlex Shi 
641148f9bb8SPaul Gortmaker void detect_ht(struct cpuinfo_x86 *c)
6429d31d35bSYinghai Lu {
643c8e56d20SBorislav Petkov #ifdef CONFIG_SMP
6449d31d35bSYinghai Lu 	u32 eax, ebx, ecx, edx;
6459d31d35bSYinghai Lu 	int index_msb, core_bits;
6462eaad1fdSMike Travis 	static bool printed;
6479d31d35bSYinghai Lu 
6480a488a53SYinghai Lu 	if (!cpu_has(c, X86_FEATURE_HT))
6499d31d35bSYinghai Lu 		return;
6509d31d35bSYinghai Lu 
6510a488a53SYinghai Lu 	if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
6520a488a53SYinghai Lu 		goto out;
6530a488a53SYinghai Lu 
6541cd78776SYinghai Lu 	if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
6551cd78776SYinghai Lu 		return;
6561cd78776SYinghai Lu 
6570a488a53SYinghai Lu 	cpuid(1, &eax, &ebx, &ecx, &edx);
6580a488a53SYinghai Lu 
6599d31d35bSYinghai Lu 	smp_num_siblings = (ebx & 0xff0000) >> 16;
6609d31d35bSYinghai Lu 
6619d31d35bSYinghai Lu 	if (smp_num_siblings == 1) {
6621b74dde7SChen Yucong 		pr_info_once("CPU0: Hyper-Threading is disabled\n");
6630f3fa48aSIngo Molnar 		goto out;
6640f3fa48aSIngo Molnar 	}
6650f3fa48aSIngo Molnar 
6660f3fa48aSIngo Molnar 	if (smp_num_siblings <= 1)
6670f3fa48aSIngo Molnar 		goto out;
6689d31d35bSYinghai Lu 
6699d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
670cb8cc442SIngo Molnar 	c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
6719d31d35bSYinghai Lu 
6729d31d35bSYinghai Lu 	smp_num_siblings = smp_num_siblings / c->x86_max_cores;
6739d31d35bSYinghai Lu 
6749d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
6759d31d35bSYinghai Lu 
6769d31d35bSYinghai Lu 	core_bits = get_count_order(c->x86_max_cores);
6779d31d35bSYinghai Lu 
678cb8cc442SIngo Molnar 	c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
6791cd78776SYinghai Lu 				       ((1 << core_bits) - 1);
6809d31d35bSYinghai Lu 
6810a488a53SYinghai Lu out:
6822eaad1fdSMike Travis 	if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
6831b74dde7SChen Yucong 		pr_info("CPU: Physical Processor ID: %d\n",
6840a488a53SYinghai Lu 			c->phys_proc_id);
6851b74dde7SChen Yucong 		pr_info("CPU: Processor Core ID: %d\n",
6869d31d35bSYinghai Lu 			c->cpu_core_id);
6872eaad1fdSMike Travis 		printed = 1;
6889d31d35bSYinghai Lu 	}
6899d31d35bSYinghai Lu #endif
69097e4db7cSYinghai Lu }
691f7627e25SThomas Gleixner 
692148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c)
693f7627e25SThomas Gleixner {
694f7627e25SThomas Gleixner 	char *v = c->x86_vendor_id;
6950f3fa48aSIngo Molnar 	int i;
696f7627e25SThomas Gleixner 
697f7627e25SThomas Gleixner 	for (i = 0; i < X86_VENDOR_NUM; i++) {
69810a434fcSYinghai Lu 		if (!cpu_devs[i])
69910a434fcSYinghai Lu 			break;
70010a434fcSYinghai Lu 
701f7627e25SThomas Gleixner 		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
702f7627e25SThomas Gleixner 		    (cpu_devs[i]->c_ident[1] &&
703f7627e25SThomas Gleixner 		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
7040f3fa48aSIngo Molnar 
705f7627e25SThomas Gleixner 			this_cpu = cpu_devs[i];
70610a434fcSYinghai Lu 			c->x86_vendor = this_cpu->c_x86_vendor;
707f7627e25SThomas Gleixner 			return;
708f7627e25SThomas Gleixner 		}
709f7627e25SThomas Gleixner 	}
71010a434fcSYinghai Lu 
7111b74dde7SChen Yucong 	pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
712a9c56953SMinchan Kim 		    "CPU: Your system may be unstable.\n", v);
71310a434fcSYinghai Lu 
714f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
715f7627e25SThomas Gleixner 	this_cpu = &default_cpu;
716f7627e25SThomas Gleixner }
717f7627e25SThomas Gleixner 
718148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c)
719f7627e25SThomas Gleixner {
720f7627e25SThomas Gleixner 	/* Get vendor name */
7214a148513SHarvey Harrison 	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
7224a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[0],
7234a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[8],
7244a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[4]);
725f7627e25SThomas Gleixner 
726f7627e25SThomas Gleixner 	c->x86 = 4;
7279d31d35bSYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
728f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
729f7627e25SThomas Gleixner 		u32 junk, tfms, cap0, misc;
7300f3fa48aSIngo Molnar 
731f7627e25SThomas Gleixner 		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
73299f925ceSBorislav Petkov 		c->x86		= x86_family(tfms);
73399f925ceSBorislav Petkov 		c->x86_model	= x86_model(tfms);
734b399151cSJia Zhang 		c->x86_stepping	= x86_stepping(tfms);
7350f3fa48aSIngo Molnar 
736d4387bd3SHuang, Ying 		if (cap0 & (1<<19)) {
737d4387bd3SHuang, Ying 			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
7389d31d35bSYinghai Lu 			c->x86_cache_alignment = c->x86_clflush_size;
739d4387bd3SHuang, Ying 		}
740f7627e25SThomas Gleixner 	}
741f7627e25SThomas Gleixner }
7423da99c97SYinghai Lu 
7438bf1ebcaSAndy Lutomirski static void apply_forced_caps(struct cpuinfo_x86 *c)
7448bf1ebcaSAndy Lutomirski {
7458bf1ebcaSAndy Lutomirski 	int i;
7468bf1ebcaSAndy Lutomirski 
7476cbd2171SThomas Gleixner 	for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
7488bf1ebcaSAndy Lutomirski 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
7498bf1ebcaSAndy Lutomirski 		c->x86_capability[i] |= cpu_caps_set[i];
7508bf1ebcaSAndy Lutomirski 	}
7518bf1ebcaSAndy Lutomirski }
7528bf1ebcaSAndy Lutomirski 
7537fcae111SDavid Woodhouse static void init_speculation_control(struct cpuinfo_x86 *c)
7547fcae111SDavid Woodhouse {
7557fcae111SDavid Woodhouse 	/*
7567fcae111SDavid Woodhouse 	 * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
7577fcae111SDavid Woodhouse 	 * and they also have a different bit for STIBP support. Also,
7587fcae111SDavid Woodhouse 	 * a hypervisor might have set the individual AMD bits even on
7597fcae111SDavid Woodhouse 	 * Intel CPUs, for finer-grained selection of what's available.
7607fcae111SDavid Woodhouse 	 *
7617fcae111SDavid Woodhouse 	 * We use the AMD bits in 0x8000_0008 EBX as the generic hardware
7627fcae111SDavid Woodhouse 	 * features, which are visible in /proc/cpuinfo and used by the
7637fcae111SDavid Woodhouse 	 * kernel. So set those accordingly from the Intel bits.
7647fcae111SDavid Woodhouse 	 */
7657fcae111SDavid Woodhouse 	if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
7667fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_IBRS);
7677fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_IBPB);
7687fcae111SDavid Woodhouse 	}
7697fcae111SDavid Woodhouse 	if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
7707fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_STIBP);
7717fcae111SDavid Woodhouse }
7727fcae111SDavid Woodhouse 
773148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c)
774093af8d7SYinghai Lu {
77539c06df4SBorislav Petkov 	u32 eax, ebx, ecx, edx;
776093af8d7SYinghai Lu 
777093af8d7SYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
778093af8d7SYinghai Lu 	if (c->cpuid_level >= 0x00000001) {
77939c06df4SBorislav Petkov 		cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
7800f3fa48aSIngo Molnar 
78139c06df4SBorislav Petkov 		c->x86_capability[CPUID_1_ECX] = ecx;
78239c06df4SBorislav Petkov 		c->x86_capability[CPUID_1_EDX] = edx;
783093af8d7SYinghai Lu 	}
784093af8d7SYinghai Lu 
7853df8d920SAndy Lutomirski 	/* Thermal and Power Management Leaf: level 0x00000006 (eax) */
7863df8d920SAndy Lutomirski 	if (c->cpuid_level >= 0x00000006)
7873df8d920SAndy Lutomirski 		c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
7883df8d920SAndy Lutomirski 
789bdc802dcSH. Peter Anvin 	/* Additional Intel-defined flags: level 0x00000007 */
790bdc802dcSH. Peter Anvin 	if (c->cpuid_level >= 0x00000007) {
791bdc802dcSH. Peter Anvin 		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
79239c06df4SBorislav Petkov 		c->x86_capability[CPUID_7_0_EBX] = ebx;
793dfb4a70fSDave Hansen 		c->x86_capability[CPUID_7_ECX] = ecx;
79495ca0ee8SDavid Woodhouse 		c->x86_capability[CPUID_7_EDX] = edx;
795bdc802dcSH. Peter Anvin 	}
796bdc802dcSH. Peter Anvin 
7976229ad27SFenghua Yu 	/* Extended state features: level 0x0000000d */
7986229ad27SFenghua Yu 	if (c->cpuid_level >= 0x0000000d) {
7996229ad27SFenghua Yu 		cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
8006229ad27SFenghua Yu 
80139c06df4SBorislav Petkov 		c->x86_capability[CPUID_D_1_EAX] = eax;
8026229ad27SFenghua Yu 	}
8036229ad27SFenghua Yu 
804cbc82b17SPeter P Waskiewicz Jr 	/* Additional Intel-defined flags: level 0x0000000F */
805cbc82b17SPeter P Waskiewicz Jr 	if (c->cpuid_level >= 0x0000000F) {
806cbc82b17SPeter P Waskiewicz Jr 
807cbc82b17SPeter P Waskiewicz Jr 		/* QoS sub-leaf, EAX=0Fh, ECX=0 */
808cbc82b17SPeter P Waskiewicz Jr 		cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
80939c06df4SBorislav Petkov 		c->x86_capability[CPUID_F_0_EDX] = edx;
81039c06df4SBorislav Petkov 
811cbc82b17SPeter P Waskiewicz Jr 		if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
812cbc82b17SPeter P Waskiewicz Jr 			/* will be overridden if occupancy monitoring exists */
813cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_max_rmid = ebx;
814cbc82b17SPeter P Waskiewicz Jr 
815cbc82b17SPeter P Waskiewicz Jr 			/* QoS sub-leaf, EAX=0Fh, ECX=1 */
816cbc82b17SPeter P Waskiewicz Jr 			cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
81739c06df4SBorislav Petkov 			c->x86_capability[CPUID_F_1_EDX] = edx;
81839c06df4SBorislav Petkov 
81933c3cc7aSVikas Shivappa 			if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
82033c3cc7aSVikas Shivappa 			      ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
82133c3cc7aSVikas Shivappa 			       (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
822cbc82b17SPeter P Waskiewicz Jr 				c->x86_cache_max_rmid = ecx;
823cbc82b17SPeter P Waskiewicz Jr 				c->x86_cache_occ_scale = ebx;
824cbc82b17SPeter P Waskiewicz Jr 			}
825cbc82b17SPeter P Waskiewicz Jr 		} else {
826cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_max_rmid = -1;
827cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_occ_scale = -1;
828cbc82b17SPeter P Waskiewicz Jr 		}
829cbc82b17SPeter P Waskiewicz Jr 	}
830cbc82b17SPeter P Waskiewicz Jr 
831093af8d7SYinghai Lu 	/* AMD-defined flags: level 0x80000001 */
83239c06df4SBorislav Petkov 	eax = cpuid_eax(0x80000000);
83339c06df4SBorislav Petkov 	c->extended_cpuid_level = eax;
8340f3fa48aSIngo Molnar 
83539c06df4SBorislav Petkov 	if ((eax & 0xffff0000) == 0x80000000) {
83639c06df4SBorislav Petkov 		if (eax >= 0x80000001) {
83739c06df4SBorislav Petkov 			cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
83839c06df4SBorislav Petkov 
83939c06df4SBorislav Petkov 			c->x86_capability[CPUID_8000_0001_ECX] = ecx;
84039c06df4SBorislav Petkov 			c->x86_capability[CPUID_8000_0001_EDX] = edx;
841093af8d7SYinghai Lu 		}
842093af8d7SYinghai Lu 	}
843093af8d7SYinghai Lu 
84471faad43SYazen Ghannam 	if (c->extended_cpuid_level >= 0x80000007) {
84571faad43SYazen Ghannam 		cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
84671faad43SYazen Ghannam 
84771faad43SYazen Ghannam 		c->x86_capability[CPUID_8000_0007_EBX] = ebx;
84871faad43SYazen Ghannam 		c->x86_power = edx;
84971faad43SYazen Ghannam 	}
85071faad43SYazen Ghannam 
8515122c890SYinghai Lu 	if (c->extended_cpuid_level >= 0x80000008) {
85239c06df4SBorislav Petkov 		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
8535122c890SYinghai Lu 
8545122c890SYinghai Lu 		c->x86_virt_bits = (eax >> 8) & 0xff;
8555122c890SYinghai Lu 		c->x86_phys_bits = eax & 0xff;
85639c06df4SBorislav Petkov 		c->x86_capability[CPUID_8000_0008_EBX] = ebx;
8575122c890SYinghai Lu 	}
85813c6c532SJan Beulich #ifdef CONFIG_X86_32
85913c6c532SJan Beulich 	else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
86013c6c532SJan Beulich 		c->x86_phys_bits = 36;
8615122c890SYinghai Lu #endif
862e3224234SYinghai Lu 
8632ccd71f1SBorislav Petkov 	if (c->extended_cpuid_level >= 0x8000000a)
86439c06df4SBorislav Petkov 		c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
8652ccd71f1SBorislav Petkov 
8661dedefd1SJacob Pan 	init_scattered_cpuid_features(c);
8677fcae111SDavid Woodhouse 	init_speculation_control(c);
86860d34501SAndy Lutomirski 
86960d34501SAndy Lutomirski 	/*
87060d34501SAndy Lutomirski 	 * Clear/Set all flags overridden by options, after probe.
87160d34501SAndy Lutomirski 	 * This needs to happen each time we re-probe, which may happen
87260d34501SAndy Lutomirski 	 * several times during CPU initialization.
87360d34501SAndy Lutomirski 	 */
87460d34501SAndy Lutomirski 	apply_forced_caps(c);
875093af8d7SYinghai Lu }
876093af8d7SYinghai Lu 
877148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
878aef93c8bSYinghai Lu {
879aef93c8bSYinghai Lu #ifdef CONFIG_X86_32
880aef93c8bSYinghai Lu 	int i;
881aef93c8bSYinghai Lu 
882aef93c8bSYinghai Lu 	/*
883aef93c8bSYinghai Lu 	 * First of all, decide if this is a 486 or higher
884aef93c8bSYinghai Lu 	 * It's a 486 if we can modify the AC flag
885aef93c8bSYinghai Lu 	 */
886aef93c8bSYinghai Lu 	if (flag_is_changeable_p(X86_EFLAGS_AC))
887aef93c8bSYinghai Lu 		c->x86 = 4;
888aef93c8bSYinghai Lu 	else
889aef93c8bSYinghai Lu 		c->x86 = 3;
890aef93c8bSYinghai Lu 
891aef93c8bSYinghai Lu 	for (i = 0; i < X86_VENDOR_NUM; i++)
892aef93c8bSYinghai Lu 		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
893aef93c8bSYinghai Lu 			c->x86_vendor_id[0] = 0;
894aef93c8bSYinghai Lu 			cpu_devs[i]->c_identify(c);
895aef93c8bSYinghai Lu 			if (c->x86_vendor_id[0]) {
896aef93c8bSYinghai Lu 				get_cpu_vendor(c);
897aef93c8bSYinghai Lu 				break;
898aef93c8bSYinghai Lu 			}
899aef93c8bSYinghai Lu 		}
900aef93c8bSYinghai Lu #endif
901093af8d7SYinghai Lu }
902f7627e25SThomas Gleixner 
9034bf5d56dSArnd Bergmann static const __initconst struct x86_cpu_id cpu_no_speculation[] = {
904fec9434aSDavid Woodhouse 	{ X86_VENDOR_INTEL,	6, INTEL_FAM6_ATOM_CEDARVIEW,	X86_FEATURE_ANY },
905fec9434aSDavid Woodhouse 	{ X86_VENDOR_INTEL,	6, INTEL_FAM6_ATOM_CLOVERVIEW,	X86_FEATURE_ANY },
906fec9434aSDavid Woodhouse 	{ X86_VENDOR_INTEL,	6, INTEL_FAM6_ATOM_LINCROFT,	X86_FEATURE_ANY },
907fec9434aSDavid Woodhouse 	{ X86_VENDOR_INTEL,	6, INTEL_FAM6_ATOM_PENWELL,	X86_FEATURE_ANY },
908fec9434aSDavid Woodhouse 	{ X86_VENDOR_INTEL,	6, INTEL_FAM6_ATOM_PINEVIEW,	X86_FEATURE_ANY },
909fec9434aSDavid Woodhouse 	{ X86_VENDOR_CENTAUR,	5 },
910fec9434aSDavid Woodhouse 	{ X86_VENDOR_INTEL,	5 },
911fec9434aSDavid Woodhouse 	{ X86_VENDOR_NSC,	5 },
912fec9434aSDavid Woodhouse 	{ X86_VENDOR_ANY,	4 },
913fec9434aSDavid Woodhouse 	{}
914fec9434aSDavid Woodhouse };
915fec9434aSDavid Woodhouse 
9164bf5d56dSArnd Bergmann static const __initconst struct x86_cpu_id cpu_no_meltdown[] = {
917fec9434aSDavid Woodhouse 	{ X86_VENDOR_AMD },
918fec9434aSDavid Woodhouse 	{}
919fec9434aSDavid Woodhouse };
920fec9434aSDavid Woodhouse 
921fec9434aSDavid Woodhouse static bool __init cpu_vulnerable_to_meltdown(struct cpuinfo_x86 *c)
922fec9434aSDavid Woodhouse {
923fec9434aSDavid Woodhouse 	u64 ia32_cap = 0;
924fec9434aSDavid Woodhouse 
925fec9434aSDavid Woodhouse 	if (x86_match_cpu(cpu_no_meltdown))
926fec9434aSDavid Woodhouse 		return false;
927fec9434aSDavid Woodhouse 
928fec9434aSDavid Woodhouse 	if (cpu_has(c, X86_FEATURE_ARCH_CAPABILITIES))
929fec9434aSDavid Woodhouse 		rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
930fec9434aSDavid Woodhouse 
931fec9434aSDavid Woodhouse 	/* Rogue Data Cache Load? No! */
932fec9434aSDavid Woodhouse 	if (ia32_cap & ARCH_CAP_RDCL_NO)
933fec9434aSDavid Woodhouse 		return false;
934fec9434aSDavid Woodhouse 
935fec9434aSDavid Woodhouse 	return true;
936fec9434aSDavid Woodhouse }
937fec9434aSDavid Woodhouse 
93834048c9eSPaolo Ciarrocchi /*
93934048c9eSPaolo Ciarrocchi  * Do minimum CPU detection early.
94034048c9eSPaolo Ciarrocchi  * Fields really needed: vendor, cpuid_level, family, model, mask,
94134048c9eSPaolo Ciarrocchi  * cache alignment.
94234048c9eSPaolo Ciarrocchi  * The others are not touched to avoid unwanted side effects.
94334048c9eSPaolo Ciarrocchi  *
944a1652bb8SJean Delvare  * WARNING: this function is only called on the boot CPU.  Don't add code
945a1652bb8SJean Delvare  * here that is supposed to run on all CPUs.
94634048c9eSPaolo Ciarrocchi  */
9473da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c)
948f7627e25SThomas Gleixner {
9496627d242SYinghai Lu #ifdef CONFIG_X86_64
9506627d242SYinghai Lu 	c->x86_clflush_size = 64;
95113c6c532SJan Beulich 	c->x86_phys_bits = 36;
95213c6c532SJan Beulich 	c->x86_virt_bits = 48;
9536627d242SYinghai Lu #else
954d4387bd3SHuang, Ying 	c->x86_clflush_size = 32;
95513c6c532SJan Beulich 	c->x86_phys_bits = 32;
95613c6c532SJan Beulich 	c->x86_virt_bits = 32;
9576627d242SYinghai Lu #endif
9580a488a53SYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
959f7627e25SThomas Gleixner 
9603da99c97SYinghai Lu 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
9610a488a53SYinghai Lu 	c->extended_cpuid_level = 0;
9620a488a53SYinghai Lu 
963aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
96405fb3c19SAndy Lutomirski 	if (have_cpuid_p()) {
965f7627e25SThomas Gleixner 		cpu_detect(c);
9663da99c97SYinghai Lu 		get_cpu_vendor(c);
9673da99c97SYinghai Lu 		get_cpu_cap(c);
96878d1b296SBorislav Petkov 		setup_force_cpu_cap(X86_FEATURE_CPUID);
96912cf105cSKrzysztof Helt 
97010a434fcSYinghai Lu 		if (this_cpu->c_early_init)
97110a434fcSYinghai Lu 			this_cpu->c_early_init(c);
9723da99c97SYinghai Lu 
973f6e9456cSRobert Richter 		c->cpu_index = 0;
974b38b0665SH. Peter Anvin 		filter_cpuid_features(c, false);
975de5397adSFenghua Yu 
976a110b5ecSBorislav Petkov 		if (this_cpu->c_bsp_init)
977a110b5ecSBorislav Petkov 			this_cpu->c_bsp_init(c);
97878d1b296SBorislav Petkov 	} else {
97978d1b296SBorislav Petkov 		identify_cpu_without_cpuid(c);
98078d1b296SBorislav Petkov 		setup_clear_cpu_cap(X86_FEATURE_CPUID);
98105fb3c19SAndy Lutomirski 	}
982c3b83598SBorislav Petkov 
983c3b83598SBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
984a89f040fSThomas Gleixner 
985fec9434aSDavid Woodhouse 	if (!x86_match_cpu(cpu_no_speculation)) {
986fec9434aSDavid Woodhouse 		if (cpu_vulnerable_to_meltdown(c))
987de791821SThomas Gleixner 			setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
98899c6fa25SDavid Woodhouse 		setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
98999c6fa25SDavid Woodhouse 		setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
990fec9434aSDavid Woodhouse 	}
99199c6fa25SDavid Woodhouse 
992db52ef74SIngo Molnar 	fpu__init_system(c);
993b8b7abaeSAndy Lutomirski 
994b8b7abaeSAndy Lutomirski #ifdef CONFIG_X86_32
995b8b7abaeSAndy Lutomirski 	/*
996b8b7abaeSAndy Lutomirski 	 * Regardless of whether PCID is enumerated, the SDM says
997b8b7abaeSAndy Lutomirski 	 * that it can't be enabled in 32-bit mode.
998b8b7abaeSAndy Lutomirski 	 */
999b8b7abaeSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_PCID);
1000b8b7abaeSAndy Lutomirski #endif
1001f7627e25SThomas Gleixner }
1002f7627e25SThomas Gleixner 
10039d31d35bSYinghai Lu void __init early_cpu_init(void)
10049d31d35bSYinghai Lu {
100502dde8b4SJan Beulich 	const struct cpu_dev *const *cdev;
100610a434fcSYinghai Lu 	int count = 0;
10079d31d35bSYinghai Lu 
1008ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
10091b74dde7SChen Yucong 	pr_info("KERNEL supported cpus:\n");
101031c997caSIngo Molnar #endif
101131c997caSIngo Molnar 
101210a434fcSYinghai Lu 	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
101302dde8b4SJan Beulich 		const struct cpu_dev *cpudev = *cdev;
10149d31d35bSYinghai Lu 
101510a434fcSYinghai Lu 		if (count >= X86_VENDOR_NUM)
101610a434fcSYinghai Lu 			break;
101710a434fcSYinghai Lu 		cpu_devs[count] = cpudev;
101810a434fcSYinghai Lu 		count++;
101910a434fcSYinghai Lu 
1020ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
102131c997caSIngo Molnar 		{
102231c997caSIngo Molnar 			unsigned int j;
102331c997caSIngo Molnar 
102410a434fcSYinghai Lu 			for (j = 0; j < 2; j++) {
102510a434fcSYinghai Lu 				if (!cpudev->c_ident[j])
102610a434fcSYinghai Lu 					continue;
10271b74dde7SChen Yucong 				pr_info("  %s %s\n", cpudev->c_vendor,
102810a434fcSYinghai Lu 					cpudev->c_ident[j]);
102910a434fcSYinghai Lu 			}
103010a434fcSYinghai Lu 		}
10310388423dSDave Jones #endif
103231c997caSIngo Molnar 	}
10339d31d35bSYinghai Lu 	early_identify_cpu(&boot_cpu_data);
1034f7627e25SThomas Gleixner }
1035f7627e25SThomas Gleixner 
1036b6734c35SH. Peter Anvin /*
1037366d4a43SBorislav Petkov  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1038366d4a43SBorislav Petkov  * unfortunately, that's not true in practice because of early VIA
1039366d4a43SBorislav Petkov  * chips and (more importantly) broken virtualizers that are not easy
1040366d4a43SBorislav Petkov  * to detect. In the latter case it doesn't even *fail* reliably, so
1041366d4a43SBorislav Petkov  * probing for it doesn't even work. Disable it completely on 32-bit
1042ba0593bfSH. Peter Anvin  * unless we can find a reliable way to detect all the broken cases.
1043366d4a43SBorislav Petkov  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
1044b6734c35SH. Peter Anvin  */
1045148f9bb8SPaul Gortmaker static void detect_nopl(struct cpuinfo_x86 *c)
1046b6734c35SH. Peter Anvin {
1047366d4a43SBorislav Petkov #ifdef CONFIG_X86_32
1048b6734c35SH. Peter Anvin 	clear_cpu_cap(c, X86_FEATURE_NOPL);
1049366d4a43SBorislav Petkov #else
1050366d4a43SBorislav Petkov 	set_cpu_cap(c, X86_FEATURE_NOPL);
1051366d4a43SBorislav Petkov #endif
1052f7627e25SThomas Gleixner }
1053f7627e25SThomas Gleixner 
10547a5d6704SAndy Lutomirski static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
10557a5d6704SAndy Lutomirski {
10567a5d6704SAndy Lutomirski #ifdef CONFIG_X86_64
1057f7627e25SThomas Gleixner 	/*
10587a5d6704SAndy Lutomirski 	 * Empirically, writing zero to a segment selector on AMD does
10597a5d6704SAndy Lutomirski 	 * not clear the base, whereas writing zero to a segment
10607a5d6704SAndy Lutomirski 	 * selector on Intel does clear the base.  Intel's behavior
10617a5d6704SAndy Lutomirski 	 * allows slightly faster context switches in the common case
10627a5d6704SAndy Lutomirski 	 * where GS is unused by the prev and next threads.
1063f7627e25SThomas Gleixner 	 *
10647a5d6704SAndy Lutomirski 	 * Since neither vendor documents this anywhere that I can see,
10657a5d6704SAndy Lutomirski 	 * detect it directly instead of hardcoding the choice by
10667a5d6704SAndy Lutomirski 	 * vendor.
10677a5d6704SAndy Lutomirski 	 *
10687a5d6704SAndy Lutomirski 	 * I've designated AMD's behavior as the "bug" because it's
10697a5d6704SAndy Lutomirski 	 * counterintuitive and less friendly.
1070f7627e25SThomas Gleixner 	 */
10717a5d6704SAndy Lutomirski 
10727a5d6704SAndy Lutomirski 	unsigned long old_base, tmp;
10737a5d6704SAndy Lutomirski 	rdmsrl(MSR_FS_BASE, old_base);
10747a5d6704SAndy Lutomirski 	wrmsrl(MSR_FS_BASE, 1);
10757a5d6704SAndy Lutomirski 	loadsegment(fs, 0);
10767a5d6704SAndy Lutomirski 	rdmsrl(MSR_FS_BASE, tmp);
10777a5d6704SAndy Lutomirski 	if (tmp != 0)
10787a5d6704SAndy Lutomirski 		set_cpu_bug(c, X86_BUG_NULL_SEG);
10797a5d6704SAndy Lutomirski 	wrmsrl(MSR_FS_BASE, old_base);
10803da99c97SYinghai Lu #endif
1081f7627e25SThomas Gleixner }
1082aef93c8bSYinghai Lu 
1083148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c)
1084f7627e25SThomas Gleixner {
1085f7627e25SThomas Gleixner 	c->extended_cpuid_level = 0;
1086f7627e25SThomas Gleixner 
1087aef93c8bSYinghai Lu 	if (!have_cpuid_p())
1088aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
1089f7627e25SThomas Gleixner 
1090aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
1091a9853dd6SIngo Molnar 	if (!have_cpuid_p())
1092aef93c8bSYinghai Lu 		return;
1093aef93c8bSYinghai Lu 
10943da99c97SYinghai Lu 	cpu_detect(c);
10953da99c97SYinghai Lu 
10963da99c97SYinghai Lu 	get_cpu_vendor(c);
10973da99c97SYinghai Lu 
10983da99c97SYinghai Lu 	get_cpu_cap(c);
10993da99c97SYinghai Lu 
1100f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
11013da99c97SYinghai Lu 		c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
1102b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32
1103c8e56d20SBorislav Petkov # ifdef CONFIG_SMP
1104cb8cc442SIngo Molnar 		c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1105f7627e25SThomas Gleixner # else
110601aaea1aSYinghai Lu 		c->apicid = c->initial_apicid;
1107f7627e25SThomas Gleixner # endif
1108b89d3b3eSYinghai Lu #endif
1109b89d3b3eSYinghai Lu 		c->phys_proc_id = c->initial_apicid;
1110f7627e25SThomas Gleixner 	}
1111f7627e25SThomas Gleixner 
1112f7627e25SThomas Gleixner 	get_model_name(c); /* Default name */
1113f7627e25SThomas Gleixner 
1114b6734c35SH. Peter Anvin 	detect_nopl(c);
11157a5d6704SAndy Lutomirski 
11167a5d6704SAndy Lutomirski 	detect_null_seg_behavior(c);
11170230bb03SAndy Lutomirski 
11180230bb03SAndy Lutomirski 	/*
11190230bb03SAndy Lutomirski 	 * ESPFIX is a strange bug.  All real CPUs have it.  Paravirt
11200230bb03SAndy Lutomirski 	 * systems that run Linux at CPL > 0 may or may not have the
11210230bb03SAndy Lutomirski 	 * issue, but, even if they have the issue, there's absolutely
11220230bb03SAndy Lutomirski 	 * nothing we can do about it because we can't use the real IRET
11230230bb03SAndy Lutomirski 	 * instruction.
11240230bb03SAndy Lutomirski 	 *
11250230bb03SAndy Lutomirski 	 * NB: For the time being, only 32-bit kernels support
11260230bb03SAndy Lutomirski 	 * X86_BUG_ESPFIX as such.  64-bit kernels directly choose
11270230bb03SAndy Lutomirski 	 * whether to apply espfix using paravirt hooks.  If any
11280230bb03SAndy Lutomirski 	 * non-paravirt system ever shows up that does *not* have the
11290230bb03SAndy Lutomirski 	 * ESPFIX issue, we can change this.
11300230bb03SAndy Lutomirski 	 */
11310230bb03SAndy Lutomirski #ifdef CONFIG_X86_32
11320230bb03SAndy Lutomirski # ifdef CONFIG_PARAVIRT
11330230bb03SAndy Lutomirski 	do {
11340230bb03SAndy Lutomirski 		extern void native_iret(void);
11350230bb03SAndy Lutomirski 		if (pv_cpu_ops.iret == native_iret)
11360230bb03SAndy Lutomirski 			set_cpu_bug(c, X86_BUG_ESPFIX);
11370230bb03SAndy Lutomirski 	} while (0);
11380230bb03SAndy Lutomirski # else
11390230bb03SAndy Lutomirski 	set_cpu_bug(c, X86_BUG_ESPFIX);
11400230bb03SAndy Lutomirski # endif
11410230bb03SAndy Lutomirski #endif
1142f7627e25SThomas Gleixner }
1143f7627e25SThomas Gleixner 
1144cbc82b17SPeter P Waskiewicz Jr static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1145cbc82b17SPeter P Waskiewicz Jr {
1146cbc82b17SPeter P Waskiewicz Jr 	/*
1147cbc82b17SPeter P Waskiewicz Jr 	 * The heavy lifting of max_rmid and cache_occ_scale are handled
1148cbc82b17SPeter P Waskiewicz Jr 	 * in get_cpu_cap().  Here we just set the max_rmid for the boot_cpu
1149cbc82b17SPeter P Waskiewicz Jr 	 * in case CQM bits really aren't there in this CPU.
1150cbc82b17SPeter P Waskiewicz Jr 	 */
1151cbc82b17SPeter P Waskiewicz Jr 	if (c != &boot_cpu_data) {
1152cbc82b17SPeter P Waskiewicz Jr 		boot_cpu_data.x86_cache_max_rmid =
1153cbc82b17SPeter P Waskiewicz Jr 			min(boot_cpu_data.x86_cache_max_rmid,
1154cbc82b17SPeter P Waskiewicz Jr 			    c->x86_cache_max_rmid);
1155cbc82b17SPeter P Waskiewicz Jr 	}
1156cbc82b17SPeter P Waskiewicz Jr }
1157cbc82b17SPeter P Waskiewicz Jr 
1158f7627e25SThomas Gleixner /*
11599d85eb91SThomas Gleixner  * Validate that ACPI/mptables have the same information about the
11609d85eb91SThomas Gleixner  * effective APIC id and update the package map.
1161d49597fdSThomas Gleixner  */
11629d85eb91SThomas Gleixner static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
1163d49597fdSThomas Gleixner {
1164d49597fdSThomas Gleixner #ifdef CONFIG_SMP
11659d85eb91SThomas Gleixner 	unsigned int apicid, cpu = smp_processor_id();
1166d49597fdSThomas Gleixner 
1167d49597fdSThomas Gleixner 	apicid = apic->cpu_present_to_apicid(cpu);
1168d49597fdSThomas Gleixner 
11699d85eb91SThomas Gleixner 	if (apicid != c->apicid) {
11709d85eb91SThomas Gleixner 		pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
1171d49597fdSThomas Gleixner 		       cpu, apicid, c->initial_apicid);
1172d49597fdSThomas Gleixner 	}
11739d85eb91SThomas Gleixner 	BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
1174d49597fdSThomas Gleixner #else
1175d49597fdSThomas Gleixner 	c->logical_proc_id = 0;
1176d49597fdSThomas Gleixner #endif
1177d49597fdSThomas Gleixner }
1178d49597fdSThomas Gleixner 
1179d49597fdSThomas Gleixner /*
1180f7627e25SThomas Gleixner  * This does the hard work of actually picking apart the CPU stuff...
1181f7627e25SThomas Gleixner  */
1182148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c)
1183f7627e25SThomas Gleixner {
1184f7627e25SThomas Gleixner 	int i;
1185f7627e25SThomas Gleixner 
1186f7627e25SThomas Gleixner 	c->loops_per_jiffy = loops_per_jiffy;
118724dbc600SGustavo A. R. Silva 	c->x86_cache_size = 0;
1188f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
1189b399151cSJia Zhang 	c->x86_model = c->x86_stepping = 0;	/* So far unknown... */
1190f7627e25SThomas Gleixner 	c->x86_vendor_id[0] = '\0'; /* Unset */
1191f7627e25SThomas Gleixner 	c->x86_model_id[0] = '\0';  /* Unset */
1192f7627e25SThomas Gleixner 	c->x86_max_cores = 1;
1193102bbe3aSYinghai Lu 	c->x86_coreid_bits = 0;
119479a8b9aaSBorislav Petkov 	c->cu_id = 0xff;
119511fdd252SYinghai Lu #ifdef CONFIG_X86_64
1196102bbe3aSYinghai Lu 	c->x86_clflush_size = 64;
119713c6c532SJan Beulich 	c->x86_phys_bits = 36;
119813c6c532SJan Beulich 	c->x86_virt_bits = 48;
1199102bbe3aSYinghai Lu #else
1200102bbe3aSYinghai Lu 	c->cpuid_level = -1;	/* CPUID not detected */
1201f7627e25SThomas Gleixner 	c->x86_clflush_size = 32;
120213c6c532SJan Beulich 	c->x86_phys_bits = 32;
120313c6c532SJan Beulich 	c->x86_virt_bits = 32;
1204102bbe3aSYinghai Lu #endif
1205102bbe3aSYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
1206f7627e25SThomas Gleixner 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
1207f7627e25SThomas Gleixner 
1208f7627e25SThomas Gleixner 	generic_identify(c);
1209f7627e25SThomas Gleixner 
12103898534dSAndi Kleen 	if (this_cpu->c_identify)
1211f7627e25SThomas Gleixner 		this_cpu->c_identify(c);
1212f7627e25SThomas Gleixner 
12136a6256f9SAdam Buchbinder 	/* Clear/Set all flags overridden by options, after probe */
12148bf1ebcaSAndy Lutomirski 	apply_forced_caps(c);
12152759c328SYinghai Lu 
1216102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
1217cb8cc442SIngo Molnar 	c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1218102bbe3aSYinghai Lu #endif
1219102bbe3aSYinghai Lu 
1220f7627e25SThomas Gleixner 	/*
1221f7627e25SThomas Gleixner 	 * Vendor-specific initialization.  In this section we
1222f7627e25SThomas Gleixner 	 * canonicalize the feature flags, meaning if there are
1223f7627e25SThomas Gleixner 	 * features a certain CPU supports which CPUID doesn't
1224f7627e25SThomas Gleixner 	 * tell us, CPUID claiming incorrect flags, or other bugs,
1225f7627e25SThomas Gleixner 	 * we handle them here.
1226f7627e25SThomas Gleixner 	 *
1227f7627e25SThomas Gleixner 	 * At the end of this section, c->x86_capability better
1228f7627e25SThomas Gleixner 	 * indicate the features this CPU genuinely supports!
1229f7627e25SThomas Gleixner 	 */
1230f7627e25SThomas Gleixner 	if (this_cpu->c_init)
1231f7627e25SThomas Gleixner 		this_cpu->c_init(c);
1232f7627e25SThomas Gleixner 
1233f7627e25SThomas Gleixner 	/* Disable the PN if appropriate */
1234f7627e25SThomas Gleixner 	squash_the_stupid_serial_number(c);
1235f7627e25SThomas Gleixner 
1236aa35f896SRicardo Neri 	/* Set up SMEP/SMAP/UMIP */
1237b2cc2a07SH. Peter Anvin 	setup_smep(c);
1238b2cc2a07SH. Peter Anvin 	setup_smap(c);
1239aa35f896SRicardo Neri 	setup_umip(c);
1240b2cc2a07SH. Peter Anvin 
1241f7627e25SThomas Gleixner 	/*
12420f3fa48aSIngo Molnar 	 * The vendor-specific functions might have changed features.
12430f3fa48aSIngo Molnar 	 * Now we do "generic changes."
1244f7627e25SThomas Gleixner 	 */
1245f7627e25SThomas Gleixner 
1246b38b0665SH. Peter Anvin 	/* Filter out anything that depends on CPUID levels we don't have */
1247b38b0665SH. Peter Anvin 	filter_cpuid_features(c, true);
1248b38b0665SH. Peter Anvin 
1249f7627e25SThomas Gleixner 	/* If the model name is still unset, do table lookup. */
1250f7627e25SThomas Gleixner 	if (!c->x86_model_id[0]) {
125102dde8b4SJan Beulich 		const char *p;
1252f7627e25SThomas Gleixner 		p = table_lookup_model(c);
1253f7627e25SThomas Gleixner 		if (p)
1254f7627e25SThomas Gleixner 			strcpy(c->x86_model_id, p);
1255f7627e25SThomas Gleixner 		else
1256f7627e25SThomas Gleixner 			/* Last resort... */
1257f7627e25SThomas Gleixner 			sprintf(c->x86_model_id, "%02x/%02x",
1258f7627e25SThomas Gleixner 				c->x86, c->x86_model);
1259f7627e25SThomas Gleixner 	}
1260f7627e25SThomas Gleixner 
1261102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
1262102bbe3aSYinghai Lu 	detect_ht(c);
1263102bbe3aSYinghai Lu #endif
1264102bbe3aSYinghai Lu 
126549d859d7SH. Peter Anvin 	x86_init_rdrand(c);
1266cbc82b17SPeter P Waskiewicz Jr 	x86_init_cache_qos(c);
126706976945SDave Hansen 	setup_pku(c);
12683e0c3737SYinghai Lu 
12693e0c3737SYinghai Lu 	/*
12706a6256f9SAdam Buchbinder 	 * Clear/Set all flags overridden by options, need do it
12713e0c3737SYinghai Lu 	 * before following smp all cpus cap AND.
12723e0c3737SYinghai Lu 	 */
12738bf1ebcaSAndy Lutomirski 	apply_forced_caps(c);
12743e0c3737SYinghai Lu 
1275f7627e25SThomas Gleixner 	/*
1276f7627e25SThomas Gleixner 	 * On SMP, boot_cpu_data holds the common feature set between
1277f7627e25SThomas Gleixner 	 * all CPUs; so make sure that we indicate which features are
1278f7627e25SThomas Gleixner 	 * common between the CPUs.  The first time this routine gets
1279f7627e25SThomas Gleixner 	 * executed, c == &boot_cpu_data.
1280f7627e25SThomas Gleixner 	 */
1281f7627e25SThomas Gleixner 	if (c != &boot_cpu_data) {
1282f7627e25SThomas Gleixner 		/* AND the already accumulated flags with these */
1283f7627e25SThomas Gleixner 		for (i = 0; i < NCAPINTS; i++)
1284f7627e25SThomas Gleixner 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
128565fc985bSBorislav Petkov 
128665fc985bSBorislav Petkov 		/* OR, i.e. replicate the bug flags */
128765fc985bSBorislav Petkov 		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
128865fc985bSBorislav Petkov 			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
1289f7627e25SThomas Gleixner 	}
1290f7627e25SThomas Gleixner 
1291f7627e25SThomas Gleixner 	/* Init Machine Check Exception if available. */
12925e09954aSBorislav Petkov 	mcheck_cpu_init(c);
129330d432dfSAndi Kleen 
129430d432dfSAndi Kleen 	select_idle_routine(c);
1295102bbe3aSYinghai Lu 
1296de2d9445STejun Heo #ifdef CONFIG_NUMA
1297102bbe3aSYinghai Lu 	numa_add_cpu(smp_processor_id());
1298102bbe3aSYinghai Lu #endif
1299f7627e25SThomas Gleixner }
1300f7627e25SThomas Gleixner 
13018b6c0ab1SIngo Molnar /*
13028b6c0ab1SIngo Molnar  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
13038b6c0ab1SIngo Molnar  * on 32-bit kernels:
13048b6c0ab1SIngo Molnar  */
1305cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32
1306cfda7bb9SAndy Lutomirski void enable_sep_cpu(void)
1307cfda7bb9SAndy Lutomirski {
13088b6c0ab1SIngo Molnar 	struct tss_struct *tss;
13098b6c0ab1SIngo Molnar 	int cpu;
1310cfda7bb9SAndy Lutomirski 
1311b3edfda4SBorislav Petkov 	if (!boot_cpu_has(X86_FEATURE_SEP))
1312b3edfda4SBorislav Petkov 		return;
1313b3edfda4SBorislav Petkov 
13148b6c0ab1SIngo Molnar 	cpu = get_cpu();
1315c482feefSAndy Lutomirski 	tss = &per_cpu(cpu_tss_rw, cpu);
13168b6c0ab1SIngo Molnar 
13178b6c0ab1SIngo Molnar 	/*
1318cf9328ccSAndy Lutomirski 	 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1319cf9328ccSAndy Lutomirski 	 * see the big comment in struct x86_hw_tss's definition.
13208b6c0ab1SIngo Molnar 	 */
1321cfda7bb9SAndy Lutomirski 
1322cfda7bb9SAndy Lutomirski 	tss->x86_tss.ss1 = __KERNEL_CS;
13238b6c0ab1SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
13244fe2d8b1SDave Hansen 	wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0);
13254c8cd0c5SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
13268b6c0ab1SIngo Molnar 
1327cfda7bb9SAndy Lutomirski 	put_cpu();
1328cfda7bb9SAndy Lutomirski }
1329e04d645fSGlauber Costa #endif
1330e04d645fSGlauber Costa 
1331f7627e25SThomas Gleixner void __init identify_boot_cpu(void)
1332f7627e25SThomas Gleixner {
1333f7627e25SThomas Gleixner 	identify_cpu(&boot_cpu_data);
1334102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1335f7627e25SThomas Gleixner 	sysenter_setup();
1336f7627e25SThomas Gleixner 	enable_sep_cpu();
1337102bbe3aSYinghai Lu #endif
1338e0ba94f1SAlex Shi 	cpu_detect_tlb(&boot_cpu_data);
1339f7627e25SThomas Gleixner }
1340f7627e25SThomas Gleixner 
1341148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c)
1342f7627e25SThomas Gleixner {
1343f7627e25SThomas Gleixner 	BUG_ON(c == &boot_cpu_data);
1344f7627e25SThomas Gleixner 	identify_cpu(c);
1345102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1346f7627e25SThomas Gleixner 	enable_sep_cpu();
1347102bbe3aSYinghai Lu #endif
1348f7627e25SThomas Gleixner 	mtrr_ap_init();
13499d85eb91SThomas Gleixner 	validate_apic_and_package_id(c);
1350f7627e25SThomas Gleixner }
1351f7627e25SThomas Gleixner 
1352191679fdSAndi Kleen static __init int setup_noclflush(char *arg)
1353191679fdSAndi Kleen {
1354840d2830SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1355da4aaa7dSH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1356191679fdSAndi Kleen 	return 1;
1357191679fdSAndi Kleen }
1358191679fdSAndi Kleen __setup("noclflush", setup_noclflush);
1359191679fdSAndi Kleen 
1360148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c)
1361f7627e25SThomas Gleixner {
136202dde8b4SJan Beulich 	const char *vendor = NULL;
1363f7627e25SThomas Gleixner 
13640f3fa48aSIngo Molnar 	if (c->x86_vendor < X86_VENDOR_NUM) {
1365f7627e25SThomas Gleixner 		vendor = this_cpu->c_vendor;
13660f3fa48aSIngo Molnar 	} else {
13670f3fa48aSIngo Molnar 		if (c->cpuid_level >= 0)
1368f7627e25SThomas Gleixner 			vendor = c->x86_vendor_id;
13690f3fa48aSIngo Molnar 	}
1370f7627e25SThomas Gleixner 
1371bd32a8cfSYinghai Lu 	if (vendor && !strstr(c->x86_model_id, vendor))
13721b74dde7SChen Yucong 		pr_cont("%s ", vendor);
1373f7627e25SThomas Gleixner 
13749d31d35bSYinghai Lu 	if (c->x86_model_id[0])
13751b74dde7SChen Yucong 		pr_cont("%s", c->x86_model_id);
1376f7627e25SThomas Gleixner 	else
13771b74dde7SChen Yucong 		pr_cont("%d86", c->x86);
1378f7627e25SThomas Gleixner 
13791b74dde7SChen Yucong 	pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
1380924e101aSBorislav Petkov 
1381b399151cSJia Zhang 	if (c->x86_stepping || c->cpuid_level >= 0)
1382b399151cSJia Zhang 		pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
1383f7627e25SThomas Gleixner 	else
13841b74dde7SChen Yucong 		pr_cont(")\n");
1385f7627e25SThomas Gleixner }
1386f7627e25SThomas Gleixner 
13870c2a3913SAndi Kleen /*
13880c2a3913SAndi Kleen  * clearcpuid= was already parsed in fpu__init_parse_early_param.
13890c2a3913SAndi Kleen  * But we need to keep a dummy __setup around otherwise it would
13900c2a3913SAndi Kleen  * show up as an environment variable for init.
13910c2a3913SAndi Kleen  */
13920c2a3913SAndi Kleen static __init int setup_clearcpuid(char *arg)
1393ac72e788SAndi Kleen {
1394ac72e788SAndi Kleen 	return 1;
1395ac72e788SAndi Kleen }
13960c2a3913SAndi Kleen __setup("clearcpuid=", setup_clearcpuid);
1397ac72e788SAndi Kleen 
1398d5494d4fSYinghai Lu #ifdef CONFIG_X86_64
1399947e76cdSBrian Gerst DEFINE_PER_CPU_FIRST(union irq_stack_union,
1400277d5b40SAndi Kleen 		     irq_stack_union) __aligned(PAGE_SIZE) __visible;
14010f3fa48aSIngo Molnar 
1402bdf977b3STejun Heo /*
1403a7fcf28dSAndy Lutomirski  * The following percpu variables are hot.  Align current_task to
1404a7fcf28dSAndy Lutomirski  * cacheline size such that they fall in the same cacheline.
1405bdf977b3STejun Heo  */
1406bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1407bdf977b3STejun Heo 	&init_task;
1408bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1409d5494d4fSYinghai Lu 
1410bdf977b3STejun Heo DEFINE_PER_CPU(char *, irq_stack_ptr) =
14114950d6d4SJosh Poimboeuf 	init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE;
1412bdf977b3STejun Heo 
1413277d5b40SAndi Kleen DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
1414d5494d4fSYinghai Lu 
1415c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1416c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1417c2daa3beSPeter Zijlstra 
1418d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */
1419d5494d4fSYinghai Lu void syscall_init(void)
1420d5494d4fSYinghai Lu {
14213386bc8aSAndy Lutomirski 	extern char _entry_trampoline[];
14223386bc8aSAndy Lutomirski 	extern char entry_SYSCALL_64_trampoline[];
14233386bc8aSAndy Lutomirski 
142472f5e08dSAndy Lutomirski 	int cpu = smp_processor_id();
14253386bc8aSAndy Lutomirski 	unsigned long SYSCALL64_entry_trampoline =
14263386bc8aSAndy Lutomirski 		(unsigned long)get_cpu_entry_area(cpu)->entry_trampoline +
14273386bc8aSAndy Lutomirski 		(entry_SYSCALL_64_trampoline - _entry_trampoline);
142872f5e08dSAndy Lutomirski 
142931ac34caSBorislav Petkov 	wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
14308d4b0678SThomas Gleixner 	if (static_cpu_has(X86_FEATURE_PTI))
14313386bc8aSAndy Lutomirski 		wrmsrl(MSR_LSTAR, SYSCALL64_entry_trampoline);
14328d4b0678SThomas Gleixner 	else
14338d4b0678SThomas Gleixner 		wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
1434d56fe4bfSIngo Molnar 
1435d56fe4bfSIngo Molnar #ifdef CONFIG_IA32_EMULATION
143647edb651SAndy Lutomirski 	wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
1437a76c7f46SDenys Vlasenko 	/*
1438487d1edbSDenys Vlasenko 	 * This only works on Intel CPUs.
1439487d1edbSDenys Vlasenko 	 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1440487d1edbSDenys Vlasenko 	 * This does not cause SYSENTER to jump to the wrong location, because
1441487d1edbSDenys Vlasenko 	 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1442a76c7f46SDenys Vlasenko 	 */
1443a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
14444fe2d8b1SDave Hansen 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1));
14454c8cd0c5SIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
1446d56fe4bfSIngo Molnar #else
144747edb651SAndy Lutomirski 	wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
14486b51311cSBorislav Petkov 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1449d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1450d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1451d5494d4fSYinghai Lu #endif
1452d5494d4fSYinghai Lu 
1453d5494d4fSYinghai Lu 	/* Flags to clear on syscall */
1454d5494d4fSYinghai Lu 	wrmsrl(MSR_SYSCALL_MASK,
145563bcff2aSH. Peter Anvin 	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
14568c7aa698SAndy Lutomirski 	       X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1457d5494d4fSYinghai Lu }
1458d5494d4fSYinghai Lu 
1459d5494d4fSYinghai Lu /*
1460d5494d4fSYinghai Lu  * Copies of the original ist values from the tss are only accessed during
1461d5494d4fSYinghai Lu  * debugging, no special alignment required.
1462d5494d4fSYinghai Lu  */
1463d5494d4fSYinghai Lu DEFINE_PER_CPU(struct orig_ist, orig_ist);
1464d5494d4fSYinghai Lu 
1465228bdaa9SSteven Rostedt static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
146642181186SSteven Rostedt DEFINE_PER_CPU(int, debug_stack_usage);
1467228bdaa9SSteven Rostedt 
1468228bdaa9SSteven Rostedt int is_debug_stack(unsigned long addr)
1469228bdaa9SSteven Rostedt {
147089cbc767SChristoph Lameter 	return __this_cpu_read(debug_stack_usage) ||
147189cbc767SChristoph Lameter 		(addr <= __this_cpu_read(debug_stack_addr) &&
147289cbc767SChristoph Lameter 		 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
1473228bdaa9SSteven Rostedt }
14740f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(is_debug_stack);
1475228bdaa9SSteven Rostedt 
1476629f4f9dSSeiji Aguchi DEFINE_PER_CPU(u32, debug_idt_ctr);
1477f8988175SSteven Rostedt 
1478228bdaa9SSteven Rostedt void debug_stack_set_zero(void)
1479228bdaa9SSteven Rostedt {
1480629f4f9dSSeiji Aguchi 	this_cpu_inc(debug_idt_ctr);
1481629f4f9dSSeiji Aguchi 	load_current_idt();
1482228bdaa9SSteven Rostedt }
14830f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_set_zero);
1484228bdaa9SSteven Rostedt 
1485228bdaa9SSteven Rostedt void debug_stack_reset(void)
1486228bdaa9SSteven Rostedt {
1487629f4f9dSSeiji Aguchi 	if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
1488f8988175SSteven Rostedt 		return;
1489629f4f9dSSeiji Aguchi 	if (this_cpu_dec_return(debug_idt_ctr) == 0)
1490629f4f9dSSeiji Aguchi 		load_current_idt();
1491228bdaa9SSteven Rostedt }
14920f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_reset);
1493228bdaa9SSteven Rostedt 
14940f3fa48aSIngo Molnar #else	/* CONFIG_X86_64 */
1495d5494d4fSYinghai Lu 
1496bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1497bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1498c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1499c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1500bdf977b3STejun Heo 
1501a7fcf28dSAndy Lutomirski /*
1502a7fcf28dSAndy Lutomirski  * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1503a7fcf28dSAndy Lutomirski  * the top of the kernel stack.  Use an extra percpu variable to track the
1504a7fcf28dSAndy Lutomirski  * top of the kernel stack directly.
1505a7fcf28dSAndy Lutomirski  */
1506a7fcf28dSAndy Lutomirski DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1507a7fcf28dSAndy Lutomirski 	(unsigned long)&init_thread_union + THREAD_SIZE;
1508a7fcf28dSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1509a7fcf28dSAndy Lutomirski 
151060a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR
151153f82452SJeremy Fitzhardinge DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
151260a5317fSTejun Heo #endif
151360a5317fSTejun Heo 
15140f3fa48aSIngo Molnar #endif	/* CONFIG_X86_64 */
1515f7627e25SThomas Gleixner 
1516f7627e25SThomas Gleixner /*
15179766cdbcSJaswinder Singh Rajput  * Clear all 6 debug registers:
15189766cdbcSJaswinder Singh Rajput  */
15199766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void)
15209766cdbcSJaswinder Singh Rajput {
15219766cdbcSJaswinder Singh Rajput 	int i;
15229766cdbcSJaswinder Singh Rajput 
15239766cdbcSJaswinder Singh Rajput 	for (i = 0; i < 8; i++) {
15249766cdbcSJaswinder Singh Rajput 		/* Ignore db4, db5 */
15259766cdbcSJaswinder Singh Rajput 		if ((i == 4) || (i == 5))
15269766cdbcSJaswinder Singh Rajput 			continue;
15279766cdbcSJaswinder Singh Rajput 
15289766cdbcSJaswinder Singh Rajput 		set_debugreg(0, i);
15299766cdbcSJaswinder Singh Rajput 	}
15309766cdbcSJaswinder Singh Rajput }
1531f7627e25SThomas Gleixner 
15320bb9fef9SJason Wessel #ifdef CONFIG_KGDB
15330bb9fef9SJason Wessel /*
15340bb9fef9SJason Wessel  * Restore debug regs if using kgdbwait and you have a kernel debugger
15350bb9fef9SJason Wessel  * connection established.
15360bb9fef9SJason Wessel  */
15370bb9fef9SJason Wessel static void dbg_restore_debug_regs(void)
15380bb9fef9SJason Wessel {
15390bb9fef9SJason Wessel 	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
15400bb9fef9SJason Wessel 		arch_kgdb_ops.correct_hw_break();
15410bb9fef9SJason Wessel }
15420bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */
15430bb9fef9SJason Wessel #define dbg_restore_debug_regs()
15440bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */
15450bb9fef9SJason Wessel 
1546ce4b1b16SIgor Mammedov static void wait_for_master_cpu(int cpu)
1547ce4b1b16SIgor Mammedov {
1548ce4b1b16SIgor Mammedov #ifdef CONFIG_SMP
1549ce4b1b16SIgor Mammedov 	/*
1550ce4b1b16SIgor Mammedov 	 * wait for ACK from master CPU before continuing
1551ce4b1b16SIgor Mammedov 	 * with AP initialization
1552ce4b1b16SIgor Mammedov 	 */
1553ce4b1b16SIgor Mammedov 	WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1554ce4b1b16SIgor Mammedov 	while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1555ce4b1b16SIgor Mammedov 		cpu_relax();
1556ce4b1b16SIgor Mammedov #endif
1557ce4b1b16SIgor Mammedov }
1558ce4b1b16SIgor Mammedov 
1559f7627e25SThomas Gleixner /*
1560f7627e25SThomas Gleixner  * cpu_init() initializes state that is per-CPU. Some data is already
1561f7627e25SThomas Gleixner  * initialized (naturally) in the bootstrap process, such as the GDT
1562f7627e25SThomas Gleixner  * and IDT. We reload them nevertheless, this function acts as a
1563f7627e25SThomas Gleixner  * 'CPU state barrier', nothing should get across.
15641ba76586SYinghai Lu  * A lot of state is already set up in PDA init for 64 bit
1565f7627e25SThomas Gleixner  */
15661ba76586SYinghai Lu #ifdef CONFIG_X86_64
15670f3fa48aSIngo Molnar 
1568148f9bb8SPaul Gortmaker void cpu_init(void)
15691ba76586SYinghai Lu {
15700fe1e009STejun Heo 	struct orig_ist *oist;
15711ba76586SYinghai Lu 	struct task_struct *me;
15720f3fa48aSIngo Molnar 	struct tss_struct *t;
15730f3fa48aSIngo Molnar 	unsigned long v;
1574fb59831bSAndy Lutomirski 	int cpu = raw_smp_processor_id();
15751ba76586SYinghai Lu 	int i;
15761ba76586SYinghai Lu 
1577ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1578ce4b1b16SIgor Mammedov 
1579e6ebf5deSFenghua Yu 	/*
15801e02ce4cSAndy Lutomirski 	 * Initialize the CR4 shadow before doing anything that could
15811e02ce4cSAndy Lutomirski 	 * try to read it.
15821e02ce4cSAndy Lutomirski 	 */
15831e02ce4cSAndy Lutomirski 	cr4_init_shadow();
15841e02ce4cSAndy Lutomirski 
1585777284b6SBorislav Petkov 	if (cpu)
1586e6ebf5deSFenghua Yu 		load_ucode_ap();
1587e6ebf5deSFenghua Yu 
1588c482feefSAndy Lutomirski 	t = &per_cpu(cpu_tss_rw, cpu);
15890fe1e009STejun Heo 	oist = &per_cpu(orig_ist, cpu);
15900f3fa48aSIngo Molnar 
1591e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA
159227fd185fSFenghua Yu 	if (this_cpu_read(numa_node) == 0 &&
1593e534c7c5SLee Schermerhorn 	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
1594e534c7c5SLee Schermerhorn 		set_numa_node(early_cpu_to_node(cpu));
1595e7a22c1eSBrian Gerst #endif
15961ba76586SYinghai Lu 
15971ba76586SYinghai Lu 	me = current;
15981ba76586SYinghai Lu 
15992eaad1fdSMike Travis 	pr_debug("Initializing CPU#%d\n", cpu);
16001ba76586SYinghai Lu 
1601375074ccSAndy Lutomirski 	cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
16021ba76586SYinghai Lu 
16031ba76586SYinghai Lu 	/*
16041ba76586SYinghai Lu 	 * Initialize the per-CPU GDT with the boot GDT,
16051ba76586SYinghai Lu 	 * and set up the GDT descriptor:
16061ba76586SYinghai Lu 	 */
16071ba76586SYinghai Lu 
1608552be871SBrian Gerst 	switch_to_new_gdt(cpu);
16092697fbd5SBrian Gerst 	loadsegment(fs, 0);
16102697fbd5SBrian Gerst 
1611cf910e83SSeiji Aguchi 	load_current_idt();
16121ba76586SYinghai Lu 
16131ba76586SYinghai Lu 	memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
16141ba76586SYinghai Lu 	syscall_init();
16151ba76586SYinghai Lu 
16161ba76586SYinghai Lu 	wrmsrl(MSR_FS_BASE, 0);
16171ba76586SYinghai Lu 	wrmsrl(MSR_KERNEL_GS_BASE, 0);
16181ba76586SYinghai Lu 	barrier();
16191ba76586SYinghai Lu 
16204763ed4dSH. Peter Anvin 	x86_configure_nx();
1621659006bfSThomas Gleixner 	x2apic_setup();
16221ba76586SYinghai Lu 
16231ba76586SYinghai Lu 	/*
16241ba76586SYinghai Lu 	 * set up and load the per-CPU TSS
16251ba76586SYinghai Lu 	 */
16260fe1e009STejun Heo 	if (!oist->ist[0]) {
162740e7f949SAndy Lutomirski 		char *estacks = get_cpu_entry_area(cpu)->exception_stacks;
16280f3fa48aSIngo Molnar 
16291ba76586SYinghai Lu 		for (v = 0; v < N_EXCEPTION_STACKS; v++) {
16300f3fa48aSIngo Molnar 			estacks += exception_stack_sizes[v];
16310fe1e009STejun Heo 			oist->ist[v] = t->x86_tss.ist[v] =
16321ba76586SYinghai Lu 					(unsigned long)estacks;
1633228bdaa9SSteven Rostedt 			if (v == DEBUG_STACK-1)
1634228bdaa9SSteven Rostedt 				per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
16351ba76586SYinghai Lu 		}
16361ba76586SYinghai Lu 	}
16371ba76586SYinghai Lu 
16387fb983b4SAndy Lutomirski 	t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
16390f3fa48aSIngo Molnar 
16401ba76586SYinghai Lu 	/*
16411ba76586SYinghai Lu 	 * <= is required because the CPU will access up to
16421ba76586SYinghai Lu 	 * 8 bits beyond the end of the IO permission bitmap.
16431ba76586SYinghai Lu 	 */
16441ba76586SYinghai Lu 	for (i = 0; i <= IO_BITMAP_LONGS; i++)
16451ba76586SYinghai Lu 		t->io_bitmap[i] = ~0UL;
16461ba76586SYinghai Lu 
1647f1f10076SVegard Nossum 	mmgrab(&init_mm);
16481ba76586SYinghai Lu 	me->active_mm = &init_mm;
16498c5dfd25SStoyan Gaydarov 	BUG_ON(me->mm);
165072c0098dSAndy Lutomirski 	initialize_tlbstate_and_flush();
16511ba76586SYinghai Lu 	enter_lazy_tlb(&init_mm, me);
16521ba76586SYinghai Lu 
165320bb8344SAndy Lutomirski 	/*
16547f2590a1SAndy Lutomirski 	 * Initialize the TSS.  sp0 points to the entry trampoline stack
16557f2590a1SAndy Lutomirski 	 * regardless of what task is running.
165620bb8344SAndy Lutomirski 	 */
165772f5e08dSAndy Lutomirski 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
16581ba76586SYinghai Lu 	load_TR_desc();
16594fe2d8b1SDave Hansen 	load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
166020bb8344SAndy Lutomirski 
166137868fe1SAndy Lutomirski 	load_mm_ldt(&init_mm);
16621ba76586SYinghai Lu 
16639766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
16640bb9fef9SJason Wessel 	dbg_restore_debug_regs();
16651ba76586SYinghai Lu 
166621c4cd10SIngo Molnar 	fpu__init_cpu();
16671ba76586SYinghai Lu 
16681ba76586SYinghai Lu 	if (is_uv_system())
16691ba76586SYinghai Lu 		uv_cpu_init();
167069218e47SThomas Garnier 
167169218e47SThomas Garnier 	load_fixmap_gdt(cpu);
16721ba76586SYinghai Lu }
16731ba76586SYinghai Lu 
16741ba76586SYinghai Lu #else
16751ba76586SYinghai Lu 
1676148f9bb8SPaul Gortmaker void cpu_init(void)
1677f7627e25SThomas Gleixner {
1678f7627e25SThomas Gleixner 	int cpu = smp_processor_id();
1679f7627e25SThomas Gleixner 	struct task_struct *curr = current;
1680c482feefSAndy Lutomirski 	struct tss_struct *t = &per_cpu(cpu_tss_rw, cpu);
1681f7627e25SThomas Gleixner 
1682ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1683e6ebf5deSFenghua Yu 
16845b2bdbc8SSteven Rostedt 	/*
16855b2bdbc8SSteven Rostedt 	 * Initialize the CR4 shadow before doing anything that could
16865b2bdbc8SSteven Rostedt 	 * try to read it.
16875b2bdbc8SSteven Rostedt 	 */
16885b2bdbc8SSteven Rostedt 	cr4_init_shadow();
16895b2bdbc8SSteven Rostedt 
1690ce4b1b16SIgor Mammedov 	show_ucode_info_early();
1691f7627e25SThomas Gleixner 
16921b74dde7SChen Yucong 	pr_info("Initializing CPU#%d\n", cpu);
1693f7627e25SThomas Gleixner 
1694362f924bSBorislav Petkov 	if (cpu_feature_enabled(X86_FEATURE_VME) ||
169559e21e3dSBorislav Petkov 	    boot_cpu_has(X86_FEATURE_TSC) ||
1696362f924bSBorislav Petkov 	    boot_cpu_has(X86_FEATURE_DE))
1697375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1698f7627e25SThomas Gleixner 
1699cf910e83SSeiji Aguchi 	load_current_idt();
1700552be871SBrian Gerst 	switch_to_new_gdt(cpu);
1701f7627e25SThomas Gleixner 
1702f7627e25SThomas Gleixner 	/*
1703f7627e25SThomas Gleixner 	 * Set up and load the per-CPU TSS and LDT
1704f7627e25SThomas Gleixner 	 */
1705f1f10076SVegard Nossum 	mmgrab(&init_mm);
1706f7627e25SThomas Gleixner 	curr->active_mm = &init_mm;
17078c5dfd25SStoyan Gaydarov 	BUG_ON(curr->mm);
170872c0098dSAndy Lutomirski 	initialize_tlbstate_and_flush();
1709f7627e25SThomas Gleixner 	enter_lazy_tlb(&init_mm, curr);
1710f7627e25SThomas Gleixner 
171120bb8344SAndy Lutomirski 	/*
171220bb8344SAndy Lutomirski 	 * Initialize the TSS.  Don't bother initializing sp0, as the initial
171320bb8344SAndy Lutomirski 	 * task never enters user mode.
171420bb8344SAndy Lutomirski 	 */
171572f5e08dSAndy Lutomirski 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
1716f7627e25SThomas Gleixner 	load_TR_desc();
171720bb8344SAndy Lutomirski 
171837868fe1SAndy Lutomirski 	load_mm_ldt(&init_mm);
1719f7627e25SThomas Gleixner 
17207fb983b4SAndy Lutomirski 	t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
1721f9a196b8SThomas Gleixner 
1722f7627e25SThomas Gleixner #ifdef CONFIG_DOUBLEFAULT
1723f7627e25SThomas Gleixner 	/* Set up doublefault TSS pointer in the GDT */
1724f7627e25SThomas Gleixner 	__set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1725f7627e25SThomas Gleixner #endif
1726f7627e25SThomas Gleixner 
17279766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
17280bb9fef9SJason Wessel 	dbg_restore_debug_regs();
1729f7627e25SThomas Gleixner 
173021c4cd10SIngo Molnar 	fpu__init_cpu();
173169218e47SThomas Garnier 
173269218e47SThomas Garnier 	load_fixmap_gdt(cpu);
1733f7627e25SThomas Gleixner }
17341ba76586SYinghai Lu #endif
17355700f743SBorislav Petkov 
1736b51ef52dSLaura Abbott static void bsp_resume(void)
1737b51ef52dSLaura Abbott {
1738b51ef52dSLaura Abbott 	if (this_cpu->c_bsp_resume)
1739b51ef52dSLaura Abbott 		this_cpu->c_bsp_resume(&boot_cpu_data);
1740b51ef52dSLaura Abbott }
1741b51ef52dSLaura Abbott 
1742b51ef52dSLaura Abbott static struct syscore_ops cpu_syscore_ops = {
1743b51ef52dSLaura Abbott 	.resume		= bsp_resume,
1744b51ef52dSLaura Abbott };
1745b51ef52dSLaura Abbott 
1746b51ef52dSLaura Abbott static int __init init_cpu_syscore(void)
1747b51ef52dSLaura Abbott {
1748b51ef52dSLaura Abbott 	register_syscore_ops(&cpu_syscore_ops);
1749b51ef52dSLaura Abbott 	return 0;
1750b51ef52dSLaura Abbott }
1751b51ef52dSLaura Abbott core_initcall(init_cpu_syscore);
1752*1008c52cSBorislav Petkov 
1753*1008c52cSBorislav Petkov /*
1754*1008c52cSBorislav Petkov  * The microcode loader calls this upon late microcode load to recheck features,
1755*1008c52cSBorislav Petkov  * only when microcode has been updated. Caller holds microcode_mutex and CPU
1756*1008c52cSBorislav Petkov  * hotplug lock.
1757*1008c52cSBorislav Petkov  */
1758*1008c52cSBorislav Petkov void microcode_check(void)
1759*1008c52cSBorislav Petkov {
1760*1008c52cSBorislav Petkov 	perf_check_microcode();
1761*1008c52cSBorislav Petkov }
1762