xref: /linux/arch/x86/kernel/cpu/common.c (revision 40e7f949e0d9a33968ebde5d67f7e3a47c97742a)
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>
50e641f5f5SIngo Molnar 
51f7627e25SThomas Gleixner #ifdef CONFIG_X86_LOCAL_APIC
52bdbcdd48STejun Heo #include <asm/uv/uv.h>
53f7627e25SThomas Gleixner #endif
54f7627e25SThomas Gleixner 
55f7627e25SThomas Gleixner #include "cpu.h"
56f7627e25SThomas Gleixner 
570274f955SGrzegorz Andrejczuk u32 elf_hwcap2 __read_mostly;
580274f955SGrzegorz Andrejczuk 
59c2d1cec1SMike Travis /* all of these masks are initialized in setup_cpu_local_masks() */
60c2d1cec1SMike Travis cpumask_var_t cpu_initialized_mask;
619766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callout_mask;
629766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callin_mask;
63c2d1cec1SMike Travis 
64c2d1cec1SMike Travis /* representing cpus for which sibling maps can be computed */
65c2d1cec1SMike Travis cpumask_var_t cpu_sibling_setup_mask;
66c2d1cec1SMike Travis 
672f2f52baSBrian Gerst /* correctly size the local cpu masks */
684369f1fbSIngo Molnar void __init setup_cpu_local_masks(void)
692f2f52baSBrian Gerst {
702f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_initialized_mask);
712f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callin_mask);
722f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callout_mask);
732f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
742f2f52baSBrian Gerst }
752f2f52baSBrian Gerst 
76148f9bb8SPaul Gortmaker static void default_init(struct cpuinfo_x86 *c)
77e8055139SOndrej Zary {
78e8055139SOndrej Zary #ifdef CONFIG_X86_64
7927c13eceSBorislav Petkov 	cpu_detect_cache_sizes(c);
80e8055139SOndrej Zary #else
81e8055139SOndrej Zary 	/* Not much we can do here... */
82e8055139SOndrej Zary 	/* Check if at least it has cpuid */
83e8055139SOndrej Zary 	if (c->cpuid_level == -1) {
84e8055139SOndrej Zary 		/* No cpuid. It must be an ancient CPU */
85e8055139SOndrej Zary 		if (c->x86 == 4)
86e8055139SOndrej Zary 			strcpy(c->x86_model_id, "486");
87e8055139SOndrej Zary 		else if (c->x86 == 3)
88e8055139SOndrej Zary 			strcpy(c->x86_model_id, "386");
89e8055139SOndrej Zary 	}
90e8055139SOndrej Zary #endif
91e8055139SOndrej Zary }
92e8055139SOndrej Zary 
93148f9bb8SPaul Gortmaker static const struct cpu_dev default_cpu = {
94e8055139SOndrej Zary 	.c_init		= default_init,
95e8055139SOndrej Zary 	.c_vendor	= "Unknown",
96e8055139SOndrej Zary 	.c_x86_vendor	= X86_VENDOR_UNKNOWN,
97e8055139SOndrej Zary };
98e8055139SOndrej Zary 
99148f9bb8SPaul Gortmaker static const struct cpu_dev *this_cpu = &default_cpu;
1000a488a53SYinghai Lu 
10106deef89SBrian Gerst DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
102950ad7ffSYinghai Lu #ifdef CONFIG_X86_64
10306deef89SBrian Gerst 	/*
10406deef89SBrian Gerst 	 * We need valid kernel segments for data and code in long mode too
105950ad7ffSYinghai Lu 	 * IRET will check the segment types  kkeil 2000/10/28
106950ad7ffSYinghai Lu 	 * Also sysret mandates a special GDT layout
10706deef89SBrian Gerst 	 *
1089766cdbcSJaswinder Singh Rajput 	 * TLS descriptors are currently at a different place compared to i386.
10906deef89SBrian Gerst 	 * Hopefully nobody expects them at a fixed place (Wine?)
110950ad7ffSYinghai Lu 	 */
1111e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL32_CS]		= GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
1121e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
1131e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
1141e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER32_CS]	= GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
1151e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
1161e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
117950ad7ffSYinghai Lu #else
1181e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
1191e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1201e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
1211e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
122f7627e25SThomas Gleixner 	/*
123f7627e25SThomas Gleixner 	 * Segments used for calling PnP BIOS have byte granularity.
124f7627e25SThomas Gleixner 	 * They code segments and data segments have fixed 64k limits,
125f7627e25SThomas Gleixner 	 * the transfer segment sizes are set at run time.
126f7627e25SThomas Gleixner 	 */
1276842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1281e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS32]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
1296842ef0eSGlauber de Oliveira Costa 	/* 16-bit code */
1301e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS16]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1316842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1321e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_DS]		= GDT_ENTRY_INIT(0x0092, 0, 0xffff),
1336842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1341e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS1]		= GDT_ENTRY_INIT(0x0092, 0, 0),
1356842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1361e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS2]		= GDT_ENTRY_INIT(0x0092, 0, 0),
137f7627e25SThomas Gleixner 	/*
138f7627e25SThomas Gleixner 	 * The APM segments have byte granularity and their bases
139f7627e25SThomas Gleixner 	 * are set at run time.  All have 64k limits.
140f7627e25SThomas Gleixner 	 */
1416842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1421e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
143f7627e25SThomas Gleixner 	/* 16-bit code */
1441e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE+1]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1456842ef0eSGlauber de Oliveira Costa 	/* data */
14672c4d853SIngo Molnar 	[GDT_ENTRY_APMBIOS_BASE+2]	= GDT_ENTRY_INIT(0x4092, 0, 0xffff),
147f7627e25SThomas Gleixner 
1481e5de182SAkinobu Mita 	[GDT_ENTRY_ESPFIX_SS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1491e5de182SAkinobu Mita 	[GDT_ENTRY_PERCPU]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
15060a5317fSTejun Heo 	GDT_STACK_CANARY_INIT
151950ad7ffSYinghai Lu #endif
15206deef89SBrian Gerst } };
153f7627e25SThomas Gleixner EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
154f7627e25SThomas Gleixner 
1558c3641e9SDave Hansen static int __init x86_mpx_setup(char *s)
1560c752a93SSuresh Siddha {
1578c3641e9SDave Hansen 	/* require an exact match without trailing characters */
1582cd3949fSDave Hansen 	if (strlen(s))
1592cd3949fSDave Hansen 		return 0;
1600c752a93SSuresh Siddha 
1618c3641e9SDave Hansen 	/* do not emit a message if the feature is not present */
1628c3641e9SDave Hansen 	if (!boot_cpu_has(X86_FEATURE_MPX))
1636bad06b7SSuresh Siddha 		return 1;
1646bad06b7SSuresh Siddha 
1658c3641e9SDave Hansen 	setup_clear_cpu_cap(X86_FEATURE_MPX);
1668c3641e9SDave Hansen 	pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
167b6f42a4aSFenghua Yu 	return 1;
168b6f42a4aSFenghua Yu }
1698c3641e9SDave Hansen __setup("nompx", x86_mpx_setup);
170b6f42a4aSFenghua Yu 
1710790c9aaSAndy Lutomirski #ifdef CONFIG_X86_64
172c7ad5ad2SAndy Lutomirski static int __init x86_nopcid_setup(char *s)
1730790c9aaSAndy Lutomirski {
174c7ad5ad2SAndy Lutomirski 	/* nopcid doesn't accept parameters */
175c7ad5ad2SAndy Lutomirski 	if (s)
176c7ad5ad2SAndy Lutomirski 		return -EINVAL;
1770790c9aaSAndy Lutomirski 
1780790c9aaSAndy Lutomirski 	/* do not emit a message if the feature is not present */
1790790c9aaSAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_PCID))
180c7ad5ad2SAndy Lutomirski 		return 0;
1810790c9aaSAndy Lutomirski 
1820790c9aaSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_PCID);
1830790c9aaSAndy Lutomirski 	pr_info("nopcid: PCID feature disabled\n");
184c7ad5ad2SAndy Lutomirski 	return 0;
1850790c9aaSAndy Lutomirski }
186c7ad5ad2SAndy Lutomirski early_param("nopcid", x86_nopcid_setup);
1870790c9aaSAndy Lutomirski #endif
1880790c9aaSAndy Lutomirski 
189d12a72b8SAndy Lutomirski static int __init x86_noinvpcid_setup(char *s)
190d12a72b8SAndy Lutomirski {
191d12a72b8SAndy Lutomirski 	/* noinvpcid doesn't accept parameters */
192d12a72b8SAndy Lutomirski 	if (s)
193d12a72b8SAndy Lutomirski 		return -EINVAL;
194d12a72b8SAndy Lutomirski 
195d12a72b8SAndy Lutomirski 	/* do not emit a message if the feature is not present */
196d12a72b8SAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_INVPCID))
197d12a72b8SAndy Lutomirski 		return 0;
198d12a72b8SAndy Lutomirski 
199d12a72b8SAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_INVPCID);
200d12a72b8SAndy Lutomirski 	pr_info("noinvpcid: INVPCID feature disabled\n");
201d12a72b8SAndy Lutomirski 	return 0;
202d12a72b8SAndy Lutomirski }
203d12a72b8SAndy Lutomirski early_param("noinvpcid", x86_noinvpcid_setup);
204d12a72b8SAndy Lutomirski 
205ba51dcedSYinghai Lu #ifdef CONFIG_X86_32
206148f9bb8SPaul Gortmaker static int cachesize_override = -1;
207148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1;
208f7627e25SThomas Gleixner 
209f7627e25SThomas Gleixner static int __init cachesize_setup(char *str)
210f7627e25SThomas Gleixner {
211f7627e25SThomas Gleixner 	get_option(&str, &cachesize_override);
212f7627e25SThomas Gleixner 	return 1;
213f7627e25SThomas Gleixner }
214f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup);
215f7627e25SThomas Gleixner 
216f7627e25SThomas Gleixner static int __init x86_sep_setup(char *s)
217f7627e25SThomas Gleixner {
21813530257SAndi Kleen 	setup_clear_cpu_cap(X86_FEATURE_SEP);
219f7627e25SThomas Gleixner 	return 1;
220f7627e25SThomas Gleixner }
221f7627e25SThomas Gleixner __setup("nosep", x86_sep_setup);
222f7627e25SThomas Gleixner 
223f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */
224f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag)
225f7627e25SThomas Gleixner {
226f7627e25SThomas Gleixner 	u32 f1, f2;
227f7627e25SThomas Gleixner 
22894f6bac1SKrzysztof Helt 	/*
22994f6bac1SKrzysztof Helt 	 * Cyrix and IDT cpus allow disabling of CPUID
23094f6bac1SKrzysztof Helt 	 * so the code below may return different results
23194f6bac1SKrzysztof Helt 	 * when it is executed before and after enabling
23294f6bac1SKrzysztof Helt 	 * the CPUID. Add "volatile" to not allow gcc to
23394f6bac1SKrzysztof Helt 	 * optimize the subsequent calls to this function.
23494f6bac1SKrzysztof Helt 	 */
23594f6bac1SKrzysztof Helt 	asm volatile ("pushfl		\n\t"
236f7627e25SThomas Gleixner 		      "pushfl		\n\t"
237f7627e25SThomas Gleixner 		      "popl %0		\n\t"
238f7627e25SThomas Gleixner 		      "movl %0, %1	\n\t"
239f7627e25SThomas Gleixner 		      "xorl %2, %0	\n\t"
240f7627e25SThomas Gleixner 		      "pushl %0		\n\t"
241f7627e25SThomas Gleixner 		      "popfl		\n\t"
242f7627e25SThomas Gleixner 		      "pushfl		\n\t"
243f7627e25SThomas Gleixner 		      "popl %0		\n\t"
244f7627e25SThomas Gleixner 		      "popfl		\n\t"
2450f3fa48aSIngo Molnar 
246f7627e25SThomas Gleixner 		      : "=&r" (f1), "=&r" (f2)
247f7627e25SThomas Gleixner 		      : "ir" (flag));
248f7627e25SThomas Gleixner 
249f7627e25SThomas Gleixner 	return ((f1^f2) & flag) != 0;
250f7627e25SThomas Gleixner }
251f7627e25SThomas Gleixner 
252f7627e25SThomas Gleixner /* Probe for the CPUID instruction */
253148f9bb8SPaul Gortmaker int have_cpuid_p(void)
254f7627e25SThomas Gleixner {
255f7627e25SThomas Gleixner 	return flag_is_changeable_p(X86_EFLAGS_ID);
256f7627e25SThomas Gleixner }
257f7627e25SThomas Gleixner 
258148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
2590a488a53SYinghai Lu {
2600a488a53SYinghai Lu 	unsigned long lo, hi;
2610f3fa48aSIngo Molnar 
2620f3fa48aSIngo Molnar 	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
2630f3fa48aSIngo Molnar 		return;
2640f3fa48aSIngo Molnar 
2650f3fa48aSIngo Molnar 	/* Disable processor serial number: */
2660f3fa48aSIngo Molnar 
2670a488a53SYinghai Lu 	rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2680a488a53SYinghai Lu 	lo |= 0x200000;
2690a488a53SYinghai Lu 	wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2700f3fa48aSIngo Molnar 
2711b74dde7SChen Yucong 	pr_notice("CPU serial number disabled.\n");
2720a488a53SYinghai Lu 	clear_cpu_cap(c, X86_FEATURE_PN);
2730a488a53SYinghai Lu 
2740a488a53SYinghai Lu 	/* Disabling the serial number may affect the cpuid level */
2750a488a53SYinghai Lu 	c->cpuid_level = cpuid_eax(0);
2760a488a53SYinghai Lu }
2770a488a53SYinghai Lu 
2780a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s)
2790a488a53SYinghai Lu {
2800a488a53SYinghai Lu 	disable_x86_serial_nr = 0;
2810a488a53SYinghai Lu 	return 1;
2820a488a53SYinghai Lu }
2830a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup);
284ba51dcedSYinghai Lu #else
285102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag)
286102bbe3aSYinghai Lu {
287102bbe3aSYinghai Lu 	return 1;
288102bbe3aSYinghai Lu }
289102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
290102bbe3aSYinghai Lu {
291102bbe3aSYinghai Lu }
292ba51dcedSYinghai Lu #endif
2930a488a53SYinghai Lu 
294de5397adSFenghua Yu static __init int setup_disable_smep(char *arg)
295de5397adSFenghua Yu {
296b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMEP);
2970f6ff2bcSDave Hansen 	/* Check for things that depend on SMEP being enabled: */
2980f6ff2bcSDave Hansen 	check_mpx_erratum(&boot_cpu_data);
299de5397adSFenghua Yu 	return 1;
300de5397adSFenghua Yu }
301de5397adSFenghua Yu __setup("nosmep", setup_disable_smep);
302de5397adSFenghua Yu 
303b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c)
304de5397adSFenghua Yu {
305b2cc2a07SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMEP))
306375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMEP);
307de5397adSFenghua Yu }
308de5397adSFenghua Yu 
30952b6179aSH. Peter Anvin static __init int setup_disable_smap(char *arg)
31052b6179aSH. Peter Anvin {
311b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMAP);
31252b6179aSH. Peter Anvin 	return 1;
31352b6179aSH. Peter Anvin }
31452b6179aSH. Peter Anvin __setup("nosmap", setup_disable_smap);
31552b6179aSH. Peter Anvin 
316b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c)
31752b6179aSH. Peter Anvin {
318581b7f15SAndrew Cooper 	unsigned long eflags = native_save_fl();
319b2cc2a07SH. Peter Anvin 
320b2cc2a07SH. Peter Anvin 	/* This should have been cleared long ago */
321b2cc2a07SH. Peter Anvin 	BUG_ON(eflags & X86_EFLAGS_AC);
322b2cc2a07SH. Peter Anvin 
32303bbd596SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMAP)) {
32403bbd596SH. Peter Anvin #ifdef CONFIG_X86_SMAP
325375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMAP);
32603bbd596SH. Peter Anvin #else
327375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_SMAP);
32803bbd596SH. Peter Anvin #endif
32903bbd596SH. Peter Anvin 	}
330f7627e25SThomas Gleixner }
331f7627e25SThomas Gleixner 
332f7627e25SThomas Gleixner /*
33306976945SDave Hansen  * Protection Keys are not available in 32-bit mode.
33406976945SDave Hansen  */
33506976945SDave Hansen static bool pku_disabled;
33606976945SDave Hansen 
33706976945SDave Hansen static __always_inline void setup_pku(struct cpuinfo_x86 *c)
33806976945SDave Hansen {
339e8df1a95SDave Hansen 	/* check the boot processor, plus compile options for PKU: */
340e8df1a95SDave Hansen 	if (!cpu_feature_enabled(X86_FEATURE_PKU))
341e8df1a95SDave Hansen 		return;
342e8df1a95SDave Hansen 	/* checks the actual processor's cpuid bits: */
34306976945SDave Hansen 	if (!cpu_has(c, X86_FEATURE_PKU))
34406976945SDave Hansen 		return;
34506976945SDave Hansen 	if (pku_disabled)
34606976945SDave Hansen 		return;
34706976945SDave Hansen 
34806976945SDave Hansen 	cr4_set_bits(X86_CR4_PKE);
34906976945SDave Hansen 	/*
35006976945SDave Hansen 	 * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
35106976945SDave Hansen 	 * cpuid bit to be set.  We need to ensure that we
35206976945SDave Hansen 	 * update that bit in this CPU's "cpu_info".
35306976945SDave Hansen 	 */
35406976945SDave Hansen 	get_cpu_cap(c);
35506976945SDave Hansen }
35606976945SDave Hansen 
35706976945SDave Hansen #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
35806976945SDave Hansen static __init int setup_disable_pku(char *arg)
35906976945SDave Hansen {
36006976945SDave Hansen 	/*
36106976945SDave Hansen 	 * Do not clear the X86_FEATURE_PKU bit.  All of the
36206976945SDave Hansen 	 * runtime checks are against OSPKE so clearing the
36306976945SDave Hansen 	 * bit does nothing.
36406976945SDave Hansen 	 *
36506976945SDave Hansen 	 * This way, we will see "pku" in cpuinfo, but not
36606976945SDave Hansen 	 * "ospke", which is exactly what we want.  It shows
36706976945SDave Hansen 	 * that the CPU has PKU, but the OS has not enabled it.
36806976945SDave Hansen 	 * This happens to be exactly how a system would look
36906976945SDave Hansen 	 * if we disabled the config option.
37006976945SDave Hansen 	 */
37106976945SDave Hansen 	pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
37206976945SDave Hansen 	pku_disabled = true;
37306976945SDave Hansen 	return 1;
37406976945SDave Hansen }
37506976945SDave Hansen __setup("nopku", setup_disable_pku);
37606976945SDave Hansen #endif /* CONFIG_X86_64 */
37706976945SDave Hansen 
37806976945SDave Hansen /*
379b38b0665SH. Peter Anvin  * Some CPU features depend on higher CPUID levels, which may not always
380b38b0665SH. Peter Anvin  * be available due to CPUID level capping or broken virtualization
381b38b0665SH. Peter Anvin  * software.  Add those features to this table to auto-disable them.
382b38b0665SH. Peter Anvin  */
383b38b0665SH. Peter Anvin struct cpuid_dependent_feature {
384b38b0665SH. Peter Anvin 	u32 feature;
385b38b0665SH. Peter Anvin 	u32 level;
386b38b0665SH. Peter Anvin };
3870f3fa48aSIngo Molnar 
388148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature
389b38b0665SH. Peter Anvin cpuid_dependent_features[] = {
390b38b0665SH. Peter Anvin 	{ X86_FEATURE_MWAIT,		0x00000005 },
391b38b0665SH. Peter Anvin 	{ X86_FEATURE_DCA,		0x00000009 },
392b38b0665SH. Peter Anvin 	{ X86_FEATURE_XSAVE,		0x0000000d },
393b38b0665SH. Peter Anvin 	{ 0, 0 }
394b38b0665SH. Peter Anvin };
395b38b0665SH. Peter Anvin 
396148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
397b38b0665SH. Peter Anvin {
398b38b0665SH. Peter Anvin 	const struct cpuid_dependent_feature *df;
3999766cdbcSJaswinder Singh Rajput 
400b38b0665SH. Peter Anvin 	for (df = cpuid_dependent_features; df->feature; df++) {
4010f3fa48aSIngo Molnar 
4020f3fa48aSIngo Molnar 		if (!cpu_has(c, df->feature))
4030f3fa48aSIngo Molnar 			continue;
404b38b0665SH. Peter Anvin 		/*
405b38b0665SH. Peter Anvin 		 * Note: cpuid_level is set to -1 if unavailable, but
406b38b0665SH. Peter Anvin 		 * extended_extended_level is set to 0 if unavailable
407b38b0665SH. Peter Anvin 		 * and the legitimate extended levels are all negative
408b38b0665SH. Peter Anvin 		 * when signed; hence the weird messing around with
409b38b0665SH. Peter Anvin 		 * signs here...
410b38b0665SH. Peter Anvin 		 */
4110f3fa48aSIngo Molnar 		if (!((s32)df->level < 0 ?
412f6db44dfSYinghai Lu 		     (u32)df->level > (u32)c->extended_cpuid_level :
4130f3fa48aSIngo Molnar 		     (s32)df->level > (s32)c->cpuid_level))
4140f3fa48aSIngo Molnar 			continue;
4150f3fa48aSIngo Molnar 
416b38b0665SH. Peter Anvin 		clear_cpu_cap(c, df->feature);
4170f3fa48aSIngo Molnar 		if (!warn)
4180f3fa48aSIngo Molnar 			continue;
4190f3fa48aSIngo Molnar 
4201b74dde7SChen Yucong 		pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
4219def39beSJosh Triplett 			x86_cap_flag(df->feature), df->level);
422b38b0665SH. Peter Anvin 	}
423b38b0665SH. Peter Anvin }
424b38b0665SH. Peter Anvin 
425b38b0665SH. Peter Anvin /*
426f7627e25SThomas Gleixner  * Naming convention should be: <Name> [(<Codename>)]
427f7627e25SThomas Gleixner  * This table only is used unless init_<vendor>() below doesn't set it;
4280f3fa48aSIngo Molnar  * in particular, if CPUID levels 0x80000002..4 are supported, this
4290f3fa48aSIngo Molnar  * isn't used
430f7627e25SThomas Gleixner  */
431f7627e25SThomas Gleixner 
432f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */
433148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c)
434f7627e25SThomas Gleixner {
43509dc68d9SJan Beulich #ifdef CONFIG_X86_32
43609dc68d9SJan Beulich 	const struct legacy_cpu_model_info *info;
437f7627e25SThomas Gleixner 
438f7627e25SThomas Gleixner 	if (c->x86_model >= 16)
439f7627e25SThomas Gleixner 		return NULL;	/* Range check */
440f7627e25SThomas Gleixner 
441f7627e25SThomas Gleixner 	if (!this_cpu)
442f7627e25SThomas Gleixner 		return NULL;
443f7627e25SThomas Gleixner 
44409dc68d9SJan Beulich 	info = this_cpu->legacy_models;
445f7627e25SThomas Gleixner 
44609dc68d9SJan Beulich 	while (info->family) {
447f7627e25SThomas Gleixner 		if (info->family == c->x86)
448f7627e25SThomas Gleixner 			return info->model_names[c->x86_model];
449f7627e25SThomas Gleixner 		info++;
450f7627e25SThomas Gleixner 	}
45109dc68d9SJan Beulich #endif
452f7627e25SThomas Gleixner 	return NULL;		/* Not found */
453f7627e25SThomas Gleixner }
454f7627e25SThomas Gleixner 
455148f9bb8SPaul Gortmaker __u32 cpu_caps_cleared[NCAPINTS];
456148f9bb8SPaul Gortmaker __u32 cpu_caps_set[NCAPINTS];
457f7627e25SThomas Gleixner 
45811e3a840SJeremy Fitzhardinge void load_percpu_segment(int cpu)
4599d31d35bSYinghai Lu {
460fab334c1SYinghai Lu #ifdef CONFIG_X86_32
4612697fbd5SBrian Gerst 	loadsegment(fs, __KERNEL_PERCPU);
4622697fbd5SBrian Gerst #else
46345e876f7SAndy Lutomirski 	__loadsegment_simple(gs, 0);
4642697fbd5SBrian Gerst 	wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
465fab334c1SYinghai Lu #endif
46660a5317fSTejun Heo 	load_stack_canary_segment();
4679d31d35bSYinghai Lu }
4689d31d35bSYinghai Lu 
46972f5e08dSAndy Lutomirski #ifdef CONFIG_X86_32
47072f5e08dSAndy Lutomirski /* The 32-bit entry code needs to find cpu_entry_area. */
47172f5e08dSAndy Lutomirski DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
47272f5e08dSAndy Lutomirski #endif
47372f5e08dSAndy Lutomirski 
474*40e7f949SAndy Lutomirski #ifdef CONFIG_X86_64
475*40e7f949SAndy Lutomirski /*
476*40e7f949SAndy Lutomirski  * Special IST stacks which the CPU switches to when it calls
477*40e7f949SAndy Lutomirski  * an IST-marked descriptor entry. Up to 7 stacks (hardware
478*40e7f949SAndy Lutomirski  * limit), all of them are 4K, except the debug stack which
479*40e7f949SAndy Lutomirski  * is 8K.
480*40e7f949SAndy Lutomirski  */
481*40e7f949SAndy Lutomirski static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
482*40e7f949SAndy Lutomirski 	  [0 ... N_EXCEPTION_STACKS - 1]	= EXCEPTION_STKSZ,
483*40e7f949SAndy Lutomirski 	  [DEBUG_STACK - 1]			= DEBUG_STKSZ
484*40e7f949SAndy Lutomirski };
485*40e7f949SAndy Lutomirski 
486*40e7f949SAndy Lutomirski static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
487*40e7f949SAndy Lutomirski 	[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
488*40e7f949SAndy Lutomirski #endif
489*40e7f949SAndy Lutomirski 
490*40e7f949SAndy Lutomirski static void __init
491*40e7f949SAndy Lutomirski set_percpu_fixmap_pages(int idx, void *ptr, int pages, pgprot_t prot)
492*40e7f949SAndy Lutomirski {
493*40e7f949SAndy Lutomirski 	for ( ; pages; pages--, idx--, ptr += PAGE_SIZE)
494*40e7f949SAndy Lutomirski 		__set_fixmap(idx, per_cpu_ptr_to_phys(ptr), prot);
495*40e7f949SAndy Lutomirski }
496*40e7f949SAndy Lutomirski 
497ef8813abSAndy Lutomirski /* Setup the fixmap mappings only once per-processor */
498*40e7f949SAndy Lutomirski static void __init setup_cpu_entry_area(int cpu)
49969218e47SThomas Garnier {
500b23adb7dSAndy Lutomirski #ifdef CONFIG_X86_64
5013386bc8aSAndy Lutomirski 	extern char _entry_trampoline[];
5023386bc8aSAndy Lutomirski 
503b23adb7dSAndy Lutomirski 	/* On 64-bit systems, we use a read-only fixmap GDT. */
504ef8813abSAndy Lutomirski 	pgprot_t gdt_prot = PAGE_KERNEL_RO;
505b23adb7dSAndy Lutomirski #else
506b23adb7dSAndy Lutomirski 	/*
507b23adb7dSAndy Lutomirski 	 * On native 32-bit systems, the GDT cannot be read-only because
508b23adb7dSAndy Lutomirski 	 * our double fault handler uses a task gate, and entering through
509b23adb7dSAndy Lutomirski 	 * a task gate needs to change an available TSS to busy.  If the GDT
510b23adb7dSAndy Lutomirski 	 * is read-only, that will triple fault.
511b23adb7dSAndy Lutomirski 	 *
512b23adb7dSAndy Lutomirski 	 * On Xen PV, the GDT must be read-only because the hypervisor requires
513b23adb7dSAndy Lutomirski 	 * it.
514b23adb7dSAndy Lutomirski 	 */
515ef8813abSAndy Lutomirski 	pgprot_t gdt_prot = boot_cpu_has(X86_FEATURE_XENPV) ?
516b23adb7dSAndy Lutomirski 		PAGE_KERNEL_RO : PAGE_KERNEL;
517b23adb7dSAndy Lutomirski #endif
518b23adb7dSAndy Lutomirski 
519ef8813abSAndy Lutomirski 	__set_fixmap(get_cpu_entry_area_index(cpu, gdt), get_cpu_gdt_paddr(cpu), gdt_prot);
5201a935bc3SAndy Lutomirski 
5211a935bc3SAndy Lutomirski 	/*
5221a935bc3SAndy Lutomirski 	 * The Intel SDM says (Volume 3, 7.2.1):
5231a935bc3SAndy Lutomirski 	 *
5241a935bc3SAndy Lutomirski 	 *  Avoid placing a page boundary in the part of the TSS that the
5251a935bc3SAndy Lutomirski 	 *  processor reads during a task switch (the first 104 bytes). The
5261a935bc3SAndy Lutomirski 	 *  processor may not correctly perform address translations if a
5271a935bc3SAndy Lutomirski 	 *  boundary occurs in this area. During a task switch, the processor
5281a935bc3SAndy Lutomirski 	 *  reads and writes into the first 104 bytes of each TSS (using
5291a935bc3SAndy Lutomirski 	 *  contiguous physical addresses beginning with the physical address
5301a935bc3SAndy Lutomirski 	 *  of the first byte of the TSS). So, after TSS access begins, if
5311a935bc3SAndy Lutomirski 	 *  part of the 104 bytes is not physically contiguous, the processor
5321a935bc3SAndy Lutomirski 	 *  will access incorrect information without generating a page-fault
5331a935bc3SAndy Lutomirski 	 *  exception.
5341a935bc3SAndy Lutomirski 	 *
5351a935bc3SAndy Lutomirski 	 * There are also a lot of errata involving the TSS spanning a page
5361a935bc3SAndy Lutomirski 	 * boundary.  Assert that we're not doing that.
5371a935bc3SAndy Lutomirski 	 */
5381a935bc3SAndy Lutomirski 	BUILD_BUG_ON((offsetof(struct tss_struct, x86_tss) ^
5391a935bc3SAndy Lutomirski 		      offsetofend(struct tss_struct, x86_tss)) & PAGE_MASK);
54072f5e08dSAndy Lutomirski 	BUILD_BUG_ON(sizeof(struct tss_struct) % PAGE_SIZE != 0);
54172f5e08dSAndy Lutomirski 	set_percpu_fixmap_pages(get_cpu_entry_area_index(cpu, tss),
54272f5e08dSAndy Lutomirski 				&per_cpu(cpu_tss, cpu),
54372f5e08dSAndy Lutomirski 				sizeof(struct tss_struct) / PAGE_SIZE,
54472f5e08dSAndy Lutomirski 				PAGE_KERNEL);
5451a935bc3SAndy Lutomirski 
54672f5e08dSAndy Lutomirski #ifdef CONFIG_X86_32
547*40e7f949SAndy Lutomirski 	per_cpu(cpu_entry_area, cpu) = get_cpu_entry_area(cpu);
54872f5e08dSAndy Lutomirski #endif
5493386bc8aSAndy Lutomirski 
5503386bc8aSAndy Lutomirski #ifdef CONFIG_X86_64
551*40e7f949SAndy Lutomirski 	BUILD_BUG_ON(sizeof(exception_stacks) % PAGE_SIZE != 0);
552*40e7f949SAndy Lutomirski 	BUILD_BUG_ON(sizeof(exception_stacks) !=
553*40e7f949SAndy Lutomirski 		     sizeof(((struct cpu_entry_area *)0)->exception_stacks));
554*40e7f949SAndy Lutomirski 	set_percpu_fixmap_pages(get_cpu_entry_area_index(cpu, exception_stacks),
555*40e7f949SAndy Lutomirski 				&per_cpu(exception_stacks, cpu),
556*40e7f949SAndy Lutomirski 				sizeof(exception_stacks) / PAGE_SIZE,
557*40e7f949SAndy Lutomirski 				PAGE_KERNEL);
558*40e7f949SAndy Lutomirski 
5593386bc8aSAndy Lutomirski 	__set_fixmap(get_cpu_entry_area_index(cpu, entry_trampoline),
5603386bc8aSAndy Lutomirski 		     __pa_symbol(_entry_trampoline), PAGE_KERNEL_RX);
5613386bc8aSAndy Lutomirski #endif
56269218e47SThomas Garnier }
56369218e47SThomas Garnier 
564*40e7f949SAndy Lutomirski void __init setup_cpu_entry_areas(void)
565*40e7f949SAndy Lutomirski {
566*40e7f949SAndy Lutomirski 	unsigned int cpu;
567*40e7f949SAndy Lutomirski 
568*40e7f949SAndy Lutomirski 	for_each_possible_cpu(cpu)
569*40e7f949SAndy Lutomirski 		setup_cpu_entry_area(cpu);
570*40e7f949SAndy Lutomirski }
571*40e7f949SAndy Lutomirski 
57245fc8757SThomas Garnier /* Load the original GDT from the per-cpu structure */
57345fc8757SThomas Garnier void load_direct_gdt(int cpu)
57445fc8757SThomas Garnier {
57545fc8757SThomas Garnier 	struct desc_ptr gdt_descr;
57645fc8757SThomas Garnier 
57745fc8757SThomas Garnier 	gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
57845fc8757SThomas Garnier 	gdt_descr.size = GDT_SIZE - 1;
57945fc8757SThomas Garnier 	load_gdt(&gdt_descr);
58045fc8757SThomas Garnier }
58145fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_direct_gdt);
58245fc8757SThomas Garnier 
58369218e47SThomas Garnier /* Load a fixmap remapping of the per-cpu GDT */
58469218e47SThomas Garnier void load_fixmap_gdt(int cpu)
58569218e47SThomas Garnier {
58669218e47SThomas Garnier 	struct desc_ptr gdt_descr;
58769218e47SThomas Garnier 
58869218e47SThomas Garnier 	gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
58969218e47SThomas Garnier 	gdt_descr.size = GDT_SIZE - 1;
59069218e47SThomas Garnier 	load_gdt(&gdt_descr);
59169218e47SThomas Garnier }
59245fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_fixmap_gdt);
59369218e47SThomas Garnier 
5940f3fa48aSIngo Molnar /*
5950f3fa48aSIngo Molnar  * Current gdt points %fs at the "master" per-cpu area: after this,
5960f3fa48aSIngo Molnar  * it's on the real one.
5970f3fa48aSIngo Molnar  */
598552be871SBrian Gerst void switch_to_new_gdt(int cpu)
599f7627e25SThomas Gleixner {
60045fc8757SThomas Garnier 	/* Load the original GDT */
60145fc8757SThomas Garnier 	load_direct_gdt(cpu);
602f7627e25SThomas Gleixner 	/* Reload the per-cpu base */
60311e3a840SJeremy Fitzhardinge 	load_percpu_segment(cpu);
604f7627e25SThomas Gleixner }
605f7627e25SThomas Gleixner 
606148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
607f7627e25SThomas Gleixner 
608148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c)
609f7627e25SThomas Gleixner {
610f7627e25SThomas Gleixner 	unsigned int *v;
611ee098e1aSBorislav Petkov 	char *p, *q, *s;
612f7627e25SThomas Gleixner 
6133da99c97SYinghai Lu 	if (c->extended_cpuid_level < 0x80000004)
6141b05d60dSYinghai Lu 		return;
615f7627e25SThomas Gleixner 
616f7627e25SThomas Gleixner 	v = (unsigned int *)c->x86_model_id;
617f7627e25SThomas Gleixner 	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
618f7627e25SThomas Gleixner 	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
619f7627e25SThomas Gleixner 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
620f7627e25SThomas Gleixner 	c->x86_model_id[48] = 0;
621f7627e25SThomas Gleixner 
622ee098e1aSBorislav Petkov 	/* Trim whitespace */
623ee098e1aSBorislav Petkov 	p = q = s = &c->x86_model_id[0];
624ee098e1aSBorislav Petkov 
625ee098e1aSBorislav Petkov 	while (*p == ' ')
626ee098e1aSBorislav Petkov 		p++;
627ee098e1aSBorislav Petkov 
628ee098e1aSBorislav Petkov 	while (*p) {
629ee098e1aSBorislav Petkov 		/* Note the last non-whitespace index */
630ee098e1aSBorislav Petkov 		if (!isspace(*p))
631ee098e1aSBorislav Petkov 			s = q;
632ee098e1aSBorislav Petkov 
633ee098e1aSBorislav Petkov 		*q++ = *p++;
634ee098e1aSBorislav Petkov 	}
635ee098e1aSBorislav Petkov 
636ee098e1aSBorislav Petkov 	*(s + 1) = '\0';
637f7627e25SThomas Gleixner }
638f7627e25SThomas Gleixner 
639148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
640f7627e25SThomas Gleixner {
6419d31d35bSYinghai Lu 	unsigned int n, dummy, ebx, ecx, edx, l2size;
642f7627e25SThomas Gleixner 
6433da99c97SYinghai Lu 	n = c->extended_cpuid_level;
644f7627e25SThomas Gleixner 
645f7627e25SThomas Gleixner 	if (n >= 0x80000005) {
6469d31d35bSYinghai Lu 		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
647f7627e25SThomas Gleixner 		c->x86_cache_size = (ecx>>24) + (edx>>24);
648140fc727SYinghai Lu #ifdef CONFIG_X86_64
649140fc727SYinghai Lu 		/* On K8 L1 TLB is inclusive, so don't count it */
650140fc727SYinghai Lu 		c->x86_tlbsize = 0;
651140fc727SYinghai Lu #endif
652f7627e25SThomas Gleixner 	}
653f7627e25SThomas Gleixner 
654f7627e25SThomas Gleixner 	if (n < 0x80000006)	/* Some chips just has a large L1. */
655f7627e25SThomas Gleixner 		return;
656f7627e25SThomas Gleixner 
6570a488a53SYinghai Lu 	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
658f7627e25SThomas Gleixner 	l2size = ecx >> 16;
659f7627e25SThomas Gleixner 
660140fc727SYinghai Lu #ifdef CONFIG_X86_64
661140fc727SYinghai Lu 	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
662140fc727SYinghai Lu #else
663f7627e25SThomas Gleixner 	/* do processor-specific cache resizing */
66409dc68d9SJan Beulich 	if (this_cpu->legacy_cache_size)
66509dc68d9SJan Beulich 		l2size = this_cpu->legacy_cache_size(c, l2size);
666f7627e25SThomas Gleixner 
667f7627e25SThomas Gleixner 	/* Allow user to override all this if necessary. */
668f7627e25SThomas Gleixner 	if (cachesize_override != -1)
669f7627e25SThomas Gleixner 		l2size = cachesize_override;
670f7627e25SThomas Gleixner 
671f7627e25SThomas Gleixner 	if (l2size == 0)
672f7627e25SThomas Gleixner 		return;		/* Again, no L2 cache is possible */
673140fc727SYinghai Lu #endif
674f7627e25SThomas Gleixner 
675f7627e25SThomas Gleixner 	c->x86_cache_size = l2size;
676f7627e25SThomas Gleixner }
677f7627e25SThomas Gleixner 
678e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO];
679e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO];
680e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO];
681e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO];
682e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO];
683e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO];
684dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO];
685e0ba94f1SAlex Shi 
686f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c)
687e0ba94f1SAlex Shi {
688e0ba94f1SAlex Shi 	if (this_cpu->c_detect_tlb)
689e0ba94f1SAlex Shi 		this_cpu->c_detect_tlb(c);
690e0ba94f1SAlex Shi 
691f94fe119SSteven Honeyman 	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
692e0ba94f1SAlex Shi 		tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
693f94fe119SSteven Honeyman 		tlb_lli_4m[ENTRIES]);
694f94fe119SSteven Honeyman 
695f94fe119SSteven Honeyman 	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
696f94fe119SSteven Honeyman 		tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
697f94fe119SSteven Honeyman 		tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
698e0ba94f1SAlex Shi }
699e0ba94f1SAlex Shi 
700148f9bb8SPaul Gortmaker void detect_ht(struct cpuinfo_x86 *c)
7019d31d35bSYinghai Lu {
702c8e56d20SBorislav Petkov #ifdef CONFIG_SMP
7039d31d35bSYinghai Lu 	u32 eax, ebx, ecx, edx;
7049d31d35bSYinghai Lu 	int index_msb, core_bits;
7052eaad1fdSMike Travis 	static bool printed;
7069d31d35bSYinghai Lu 
7070a488a53SYinghai Lu 	if (!cpu_has(c, X86_FEATURE_HT))
7089d31d35bSYinghai Lu 		return;
7099d31d35bSYinghai Lu 
7100a488a53SYinghai Lu 	if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
7110a488a53SYinghai Lu 		goto out;
7120a488a53SYinghai Lu 
7131cd78776SYinghai Lu 	if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
7141cd78776SYinghai Lu 		return;
7151cd78776SYinghai Lu 
7160a488a53SYinghai Lu 	cpuid(1, &eax, &ebx, &ecx, &edx);
7170a488a53SYinghai Lu 
7189d31d35bSYinghai Lu 	smp_num_siblings = (ebx & 0xff0000) >> 16;
7199d31d35bSYinghai Lu 
7209d31d35bSYinghai Lu 	if (smp_num_siblings == 1) {
7211b74dde7SChen Yucong 		pr_info_once("CPU0: Hyper-Threading is disabled\n");
7220f3fa48aSIngo Molnar 		goto out;
7230f3fa48aSIngo Molnar 	}
7240f3fa48aSIngo Molnar 
7250f3fa48aSIngo Molnar 	if (smp_num_siblings <= 1)
7260f3fa48aSIngo Molnar 		goto out;
7279d31d35bSYinghai Lu 
7289d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
729cb8cc442SIngo Molnar 	c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
7309d31d35bSYinghai Lu 
7319d31d35bSYinghai Lu 	smp_num_siblings = smp_num_siblings / c->x86_max_cores;
7329d31d35bSYinghai Lu 
7339d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
7349d31d35bSYinghai Lu 
7359d31d35bSYinghai Lu 	core_bits = get_count_order(c->x86_max_cores);
7369d31d35bSYinghai Lu 
737cb8cc442SIngo Molnar 	c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
7381cd78776SYinghai Lu 				       ((1 << core_bits) - 1);
7399d31d35bSYinghai Lu 
7400a488a53SYinghai Lu out:
7412eaad1fdSMike Travis 	if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
7421b74dde7SChen Yucong 		pr_info("CPU: Physical Processor ID: %d\n",
7430a488a53SYinghai Lu 			c->phys_proc_id);
7441b74dde7SChen Yucong 		pr_info("CPU: Processor Core ID: %d\n",
7459d31d35bSYinghai Lu 			c->cpu_core_id);
7462eaad1fdSMike Travis 		printed = 1;
7479d31d35bSYinghai Lu 	}
7489d31d35bSYinghai Lu #endif
74997e4db7cSYinghai Lu }
750f7627e25SThomas Gleixner 
751148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c)
752f7627e25SThomas Gleixner {
753f7627e25SThomas Gleixner 	char *v = c->x86_vendor_id;
7540f3fa48aSIngo Molnar 	int i;
755f7627e25SThomas Gleixner 
756f7627e25SThomas Gleixner 	for (i = 0; i < X86_VENDOR_NUM; i++) {
75710a434fcSYinghai Lu 		if (!cpu_devs[i])
75810a434fcSYinghai Lu 			break;
75910a434fcSYinghai Lu 
760f7627e25SThomas Gleixner 		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
761f7627e25SThomas Gleixner 		    (cpu_devs[i]->c_ident[1] &&
762f7627e25SThomas Gleixner 		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
7630f3fa48aSIngo Molnar 
764f7627e25SThomas Gleixner 			this_cpu = cpu_devs[i];
76510a434fcSYinghai Lu 			c->x86_vendor = this_cpu->c_x86_vendor;
766f7627e25SThomas Gleixner 			return;
767f7627e25SThomas Gleixner 		}
768f7627e25SThomas Gleixner 	}
76910a434fcSYinghai Lu 
7701b74dde7SChen Yucong 	pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
771a9c56953SMinchan Kim 		    "CPU: Your system may be unstable.\n", v);
77210a434fcSYinghai Lu 
773f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
774f7627e25SThomas Gleixner 	this_cpu = &default_cpu;
775f7627e25SThomas Gleixner }
776f7627e25SThomas Gleixner 
777148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c)
778f7627e25SThomas Gleixner {
779f7627e25SThomas Gleixner 	/* Get vendor name */
7804a148513SHarvey Harrison 	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
7814a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[0],
7824a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[8],
7834a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[4]);
784f7627e25SThomas Gleixner 
785f7627e25SThomas Gleixner 	c->x86 = 4;
7869d31d35bSYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
787f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
788f7627e25SThomas Gleixner 		u32 junk, tfms, cap0, misc;
7890f3fa48aSIngo Molnar 
790f7627e25SThomas Gleixner 		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
79199f925ceSBorislav Petkov 		c->x86		= x86_family(tfms);
79299f925ceSBorislav Petkov 		c->x86_model	= x86_model(tfms);
79399f925ceSBorislav Petkov 		c->x86_mask	= x86_stepping(tfms);
7940f3fa48aSIngo Molnar 
795d4387bd3SHuang, Ying 		if (cap0 & (1<<19)) {
796d4387bd3SHuang, Ying 			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
7979d31d35bSYinghai Lu 			c->x86_cache_alignment = c->x86_clflush_size;
798d4387bd3SHuang, Ying 		}
799f7627e25SThomas Gleixner 	}
800f7627e25SThomas Gleixner }
8013da99c97SYinghai Lu 
8028bf1ebcaSAndy Lutomirski static void apply_forced_caps(struct cpuinfo_x86 *c)
8038bf1ebcaSAndy Lutomirski {
8048bf1ebcaSAndy Lutomirski 	int i;
8058bf1ebcaSAndy Lutomirski 
8068bf1ebcaSAndy Lutomirski 	for (i = 0; i < NCAPINTS; i++) {
8078bf1ebcaSAndy Lutomirski 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
8088bf1ebcaSAndy Lutomirski 		c->x86_capability[i] |= cpu_caps_set[i];
8098bf1ebcaSAndy Lutomirski 	}
8108bf1ebcaSAndy Lutomirski }
8118bf1ebcaSAndy Lutomirski 
812148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c)
813093af8d7SYinghai Lu {
81439c06df4SBorislav Petkov 	u32 eax, ebx, ecx, edx;
815093af8d7SYinghai Lu 
816093af8d7SYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
817093af8d7SYinghai Lu 	if (c->cpuid_level >= 0x00000001) {
81839c06df4SBorislav Petkov 		cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
8190f3fa48aSIngo Molnar 
82039c06df4SBorislav Petkov 		c->x86_capability[CPUID_1_ECX] = ecx;
82139c06df4SBorislav Petkov 		c->x86_capability[CPUID_1_EDX] = edx;
822093af8d7SYinghai Lu 	}
823093af8d7SYinghai Lu 
8243df8d920SAndy Lutomirski 	/* Thermal and Power Management Leaf: level 0x00000006 (eax) */
8253df8d920SAndy Lutomirski 	if (c->cpuid_level >= 0x00000006)
8263df8d920SAndy Lutomirski 		c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
8273df8d920SAndy Lutomirski 
828bdc802dcSH. Peter Anvin 	/* Additional Intel-defined flags: level 0x00000007 */
829bdc802dcSH. Peter Anvin 	if (c->cpuid_level >= 0x00000007) {
830bdc802dcSH. Peter Anvin 		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
83139c06df4SBorislav Petkov 		c->x86_capability[CPUID_7_0_EBX] = ebx;
832dfb4a70fSDave Hansen 		c->x86_capability[CPUID_7_ECX] = ecx;
833bdc802dcSH. Peter Anvin 	}
834bdc802dcSH. Peter Anvin 
8356229ad27SFenghua Yu 	/* Extended state features: level 0x0000000d */
8366229ad27SFenghua Yu 	if (c->cpuid_level >= 0x0000000d) {
8376229ad27SFenghua Yu 		cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
8386229ad27SFenghua Yu 
83939c06df4SBorislav Petkov 		c->x86_capability[CPUID_D_1_EAX] = eax;
8406229ad27SFenghua Yu 	}
8416229ad27SFenghua Yu 
842cbc82b17SPeter P Waskiewicz Jr 	/* Additional Intel-defined flags: level 0x0000000F */
843cbc82b17SPeter P Waskiewicz Jr 	if (c->cpuid_level >= 0x0000000F) {
844cbc82b17SPeter P Waskiewicz Jr 
845cbc82b17SPeter P Waskiewicz Jr 		/* QoS sub-leaf, EAX=0Fh, ECX=0 */
846cbc82b17SPeter P Waskiewicz Jr 		cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
84739c06df4SBorislav Petkov 		c->x86_capability[CPUID_F_0_EDX] = edx;
84839c06df4SBorislav Petkov 
849cbc82b17SPeter P Waskiewicz Jr 		if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
850cbc82b17SPeter P Waskiewicz Jr 			/* will be overridden if occupancy monitoring exists */
851cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_max_rmid = ebx;
852cbc82b17SPeter P Waskiewicz Jr 
853cbc82b17SPeter P Waskiewicz Jr 			/* QoS sub-leaf, EAX=0Fh, ECX=1 */
854cbc82b17SPeter P Waskiewicz Jr 			cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
85539c06df4SBorislav Petkov 			c->x86_capability[CPUID_F_1_EDX] = edx;
85639c06df4SBorislav Petkov 
85733c3cc7aSVikas Shivappa 			if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
85833c3cc7aSVikas Shivappa 			      ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
85933c3cc7aSVikas Shivappa 			       (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
860cbc82b17SPeter P Waskiewicz Jr 				c->x86_cache_max_rmid = ecx;
861cbc82b17SPeter P Waskiewicz Jr 				c->x86_cache_occ_scale = ebx;
862cbc82b17SPeter P Waskiewicz Jr 			}
863cbc82b17SPeter P Waskiewicz Jr 		} else {
864cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_max_rmid = -1;
865cbc82b17SPeter P Waskiewicz Jr 			c->x86_cache_occ_scale = -1;
866cbc82b17SPeter P Waskiewicz Jr 		}
867cbc82b17SPeter P Waskiewicz Jr 	}
868cbc82b17SPeter P Waskiewicz Jr 
869093af8d7SYinghai Lu 	/* AMD-defined flags: level 0x80000001 */
87039c06df4SBorislav Petkov 	eax = cpuid_eax(0x80000000);
87139c06df4SBorislav Petkov 	c->extended_cpuid_level = eax;
8720f3fa48aSIngo Molnar 
87339c06df4SBorislav Petkov 	if ((eax & 0xffff0000) == 0x80000000) {
87439c06df4SBorislav Petkov 		if (eax >= 0x80000001) {
87539c06df4SBorislav Petkov 			cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
87639c06df4SBorislav Petkov 
87739c06df4SBorislav Petkov 			c->x86_capability[CPUID_8000_0001_ECX] = ecx;
87839c06df4SBorislav Petkov 			c->x86_capability[CPUID_8000_0001_EDX] = edx;
879093af8d7SYinghai Lu 		}
880093af8d7SYinghai Lu 	}
881093af8d7SYinghai Lu 
88271faad43SYazen Ghannam 	if (c->extended_cpuid_level >= 0x80000007) {
88371faad43SYazen Ghannam 		cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
88471faad43SYazen Ghannam 
88571faad43SYazen Ghannam 		c->x86_capability[CPUID_8000_0007_EBX] = ebx;
88671faad43SYazen Ghannam 		c->x86_power = edx;
88771faad43SYazen Ghannam 	}
88871faad43SYazen Ghannam 
8895122c890SYinghai Lu 	if (c->extended_cpuid_level >= 0x80000008) {
89039c06df4SBorislav Petkov 		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
8915122c890SYinghai Lu 
8925122c890SYinghai Lu 		c->x86_virt_bits = (eax >> 8) & 0xff;
8935122c890SYinghai Lu 		c->x86_phys_bits = eax & 0xff;
89439c06df4SBorislav Petkov 		c->x86_capability[CPUID_8000_0008_EBX] = ebx;
8955122c890SYinghai Lu 	}
89613c6c532SJan Beulich #ifdef CONFIG_X86_32
89713c6c532SJan Beulich 	else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
89813c6c532SJan Beulich 		c->x86_phys_bits = 36;
8995122c890SYinghai Lu #endif
900e3224234SYinghai Lu 
9012ccd71f1SBorislav Petkov 	if (c->extended_cpuid_level >= 0x8000000a)
90239c06df4SBorislav Petkov 		c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
9032ccd71f1SBorislav Petkov 
9041dedefd1SJacob Pan 	init_scattered_cpuid_features(c);
90560d34501SAndy Lutomirski 
90660d34501SAndy Lutomirski 	/*
90760d34501SAndy Lutomirski 	 * Clear/Set all flags overridden by options, after probe.
90860d34501SAndy Lutomirski 	 * This needs to happen each time we re-probe, which may happen
90960d34501SAndy Lutomirski 	 * several times during CPU initialization.
91060d34501SAndy Lutomirski 	 */
91160d34501SAndy Lutomirski 	apply_forced_caps(c);
912093af8d7SYinghai Lu }
913093af8d7SYinghai Lu 
914148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
915aef93c8bSYinghai Lu {
916aef93c8bSYinghai Lu #ifdef CONFIG_X86_32
917aef93c8bSYinghai Lu 	int i;
918aef93c8bSYinghai Lu 
919aef93c8bSYinghai Lu 	/*
920aef93c8bSYinghai Lu 	 * First of all, decide if this is a 486 or higher
921aef93c8bSYinghai Lu 	 * It's a 486 if we can modify the AC flag
922aef93c8bSYinghai Lu 	 */
923aef93c8bSYinghai Lu 	if (flag_is_changeable_p(X86_EFLAGS_AC))
924aef93c8bSYinghai Lu 		c->x86 = 4;
925aef93c8bSYinghai Lu 	else
926aef93c8bSYinghai Lu 		c->x86 = 3;
927aef93c8bSYinghai Lu 
928aef93c8bSYinghai Lu 	for (i = 0; i < X86_VENDOR_NUM; i++)
929aef93c8bSYinghai Lu 		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
930aef93c8bSYinghai Lu 			c->x86_vendor_id[0] = 0;
931aef93c8bSYinghai Lu 			cpu_devs[i]->c_identify(c);
932aef93c8bSYinghai Lu 			if (c->x86_vendor_id[0]) {
933aef93c8bSYinghai Lu 				get_cpu_vendor(c);
934aef93c8bSYinghai Lu 				break;
935aef93c8bSYinghai Lu 			}
936aef93c8bSYinghai Lu 		}
937aef93c8bSYinghai Lu #endif
938093af8d7SYinghai Lu }
939f7627e25SThomas Gleixner 
94034048c9eSPaolo Ciarrocchi /*
94134048c9eSPaolo Ciarrocchi  * Do minimum CPU detection early.
94234048c9eSPaolo Ciarrocchi  * Fields really needed: vendor, cpuid_level, family, model, mask,
94334048c9eSPaolo Ciarrocchi  * cache alignment.
94434048c9eSPaolo Ciarrocchi  * The others are not touched to avoid unwanted side effects.
94534048c9eSPaolo Ciarrocchi  *
94634048c9eSPaolo Ciarrocchi  * WARNING: this function is only called on the BP.  Don't add code here
94734048c9eSPaolo Ciarrocchi  * that is supposed to run on all CPUs.
94834048c9eSPaolo Ciarrocchi  */
9493da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c)
950f7627e25SThomas Gleixner {
9516627d242SYinghai Lu #ifdef CONFIG_X86_64
9526627d242SYinghai Lu 	c->x86_clflush_size = 64;
95313c6c532SJan Beulich 	c->x86_phys_bits = 36;
95413c6c532SJan Beulich 	c->x86_virt_bits = 48;
9556627d242SYinghai Lu #else
956d4387bd3SHuang, Ying 	c->x86_clflush_size = 32;
95713c6c532SJan Beulich 	c->x86_phys_bits = 32;
95813c6c532SJan Beulich 	c->x86_virt_bits = 32;
9596627d242SYinghai Lu #endif
9600a488a53SYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
961f7627e25SThomas Gleixner 
9623da99c97SYinghai Lu 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
9630a488a53SYinghai Lu 	c->extended_cpuid_level = 0;
9640a488a53SYinghai Lu 
965aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
96605fb3c19SAndy Lutomirski 	if (have_cpuid_p()) {
967f7627e25SThomas Gleixner 		cpu_detect(c);
9683da99c97SYinghai Lu 		get_cpu_vendor(c);
9693da99c97SYinghai Lu 		get_cpu_cap(c);
97078d1b296SBorislav Petkov 		setup_force_cpu_cap(X86_FEATURE_CPUID);
97112cf105cSKrzysztof Helt 
97210a434fcSYinghai Lu 		if (this_cpu->c_early_init)
97310a434fcSYinghai Lu 			this_cpu->c_early_init(c);
9743da99c97SYinghai Lu 
975f6e9456cSRobert Richter 		c->cpu_index = 0;
976b38b0665SH. Peter Anvin 		filter_cpuid_features(c, false);
977de5397adSFenghua Yu 
978a110b5ecSBorislav Petkov 		if (this_cpu->c_bsp_init)
979a110b5ecSBorislav Petkov 			this_cpu->c_bsp_init(c);
98078d1b296SBorislav Petkov 	} else {
98178d1b296SBorislav Petkov 		identify_cpu_without_cpuid(c);
98278d1b296SBorislav Petkov 		setup_clear_cpu_cap(X86_FEATURE_CPUID);
98305fb3c19SAndy Lutomirski 	}
984c3b83598SBorislav Petkov 
985c3b83598SBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
986db52ef74SIngo Molnar 	fpu__init_system(c);
987b8b7abaeSAndy Lutomirski 
988b8b7abaeSAndy Lutomirski #ifdef CONFIG_X86_32
989b8b7abaeSAndy Lutomirski 	/*
990b8b7abaeSAndy Lutomirski 	 * Regardless of whether PCID is enumerated, the SDM says
991b8b7abaeSAndy Lutomirski 	 * that it can't be enabled in 32-bit mode.
992b8b7abaeSAndy Lutomirski 	 */
993b8b7abaeSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_PCID);
994b8b7abaeSAndy Lutomirski #endif
995f7627e25SThomas Gleixner }
996f7627e25SThomas Gleixner 
9979d31d35bSYinghai Lu void __init early_cpu_init(void)
9989d31d35bSYinghai Lu {
99902dde8b4SJan Beulich 	const struct cpu_dev *const *cdev;
100010a434fcSYinghai Lu 	int count = 0;
10019d31d35bSYinghai Lu 
1002ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
10031b74dde7SChen Yucong 	pr_info("KERNEL supported cpus:\n");
100431c997caSIngo Molnar #endif
100531c997caSIngo Molnar 
100610a434fcSYinghai Lu 	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
100702dde8b4SJan Beulich 		const struct cpu_dev *cpudev = *cdev;
10089d31d35bSYinghai Lu 
100910a434fcSYinghai Lu 		if (count >= X86_VENDOR_NUM)
101010a434fcSYinghai Lu 			break;
101110a434fcSYinghai Lu 		cpu_devs[count] = cpudev;
101210a434fcSYinghai Lu 		count++;
101310a434fcSYinghai Lu 
1014ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
101531c997caSIngo Molnar 		{
101631c997caSIngo Molnar 			unsigned int j;
101731c997caSIngo Molnar 
101810a434fcSYinghai Lu 			for (j = 0; j < 2; j++) {
101910a434fcSYinghai Lu 				if (!cpudev->c_ident[j])
102010a434fcSYinghai Lu 					continue;
10211b74dde7SChen Yucong 				pr_info("  %s %s\n", cpudev->c_vendor,
102210a434fcSYinghai Lu 					cpudev->c_ident[j]);
102310a434fcSYinghai Lu 			}
102410a434fcSYinghai Lu 		}
10250388423dSDave Jones #endif
102631c997caSIngo Molnar 	}
10279d31d35bSYinghai Lu 	early_identify_cpu(&boot_cpu_data);
1028f7627e25SThomas Gleixner }
1029f7627e25SThomas Gleixner 
1030b6734c35SH. Peter Anvin /*
1031366d4a43SBorislav Petkov  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1032366d4a43SBorislav Petkov  * unfortunately, that's not true in practice because of early VIA
1033366d4a43SBorislav Petkov  * chips and (more importantly) broken virtualizers that are not easy
1034366d4a43SBorislav Petkov  * to detect. In the latter case it doesn't even *fail* reliably, so
1035366d4a43SBorislav Petkov  * probing for it doesn't even work. Disable it completely on 32-bit
1036ba0593bfSH. Peter Anvin  * unless we can find a reliable way to detect all the broken cases.
1037366d4a43SBorislav Petkov  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
1038b6734c35SH. Peter Anvin  */
1039148f9bb8SPaul Gortmaker static void detect_nopl(struct cpuinfo_x86 *c)
1040b6734c35SH. Peter Anvin {
1041366d4a43SBorislav Petkov #ifdef CONFIG_X86_32
1042b6734c35SH. Peter Anvin 	clear_cpu_cap(c, X86_FEATURE_NOPL);
1043366d4a43SBorislav Petkov #else
1044366d4a43SBorislav Petkov 	set_cpu_cap(c, X86_FEATURE_NOPL);
1045366d4a43SBorislav Petkov #endif
1046f7627e25SThomas Gleixner }
1047f7627e25SThomas Gleixner 
10487a5d6704SAndy Lutomirski static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
10497a5d6704SAndy Lutomirski {
10507a5d6704SAndy Lutomirski #ifdef CONFIG_X86_64
1051f7627e25SThomas Gleixner 	/*
10527a5d6704SAndy Lutomirski 	 * Empirically, writing zero to a segment selector on AMD does
10537a5d6704SAndy Lutomirski 	 * not clear the base, whereas writing zero to a segment
10547a5d6704SAndy Lutomirski 	 * selector on Intel does clear the base.  Intel's behavior
10557a5d6704SAndy Lutomirski 	 * allows slightly faster context switches in the common case
10567a5d6704SAndy Lutomirski 	 * where GS is unused by the prev and next threads.
1057f7627e25SThomas Gleixner 	 *
10587a5d6704SAndy Lutomirski 	 * Since neither vendor documents this anywhere that I can see,
10597a5d6704SAndy Lutomirski 	 * detect it directly instead of hardcoding the choice by
10607a5d6704SAndy Lutomirski 	 * vendor.
10617a5d6704SAndy Lutomirski 	 *
10627a5d6704SAndy Lutomirski 	 * I've designated AMD's behavior as the "bug" because it's
10637a5d6704SAndy Lutomirski 	 * counterintuitive and less friendly.
1064f7627e25SThomas Gleixner 	 */
10657a5d6704SAndy Lutomirski 
10667a5d6704SAndy Lutomirski 	unsigned long old_base, tmp;
10677a5d6704SAndy Lutomirski 	rdmsrl(MSR_FS_BASE, old_base);
10687a5d6704SAndy Lutomirski 	wrmsrl(MSR_FS_BASE, 1);
10697a5d6704SAndy Lutomirski 	loadsegment(fs, 0);
10707a5d6704SAndy Lutomirski 	rdmsrl(MSR_FS_BASE, tmp);
10717a5d6704SAndy Lutomirski 	if (tmp != 0)
10727a5d6704SAndy Lutomirski 		set_cpu_bug(c, X86_BUG_NULL_SEG);
10737a5d6704SAndy Lutomirski 	wrmsrl(MSR_FS_BASE, old_base);
10743da99c97SYinghai Lu #endif
1075f7627e25SThomas Gleixner }
1076aef93c8bSYinghai Lu 
1077148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c)
1078f7627e25SThomas Gleixner {
1079f7627e25SThomas Gleixner 	c->extended_cpuid_level = 0;
1080f7627e25SThomas Gleixner 
1081aef93c8bSYinghai Lu 	if (!have_cpuid_p())
1082aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
1083f7627e25SThomas Gleixner 
1084aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
1085a9853dd6SIngo Molnar 	if (!have_cpuid_p())
1086aef93c8bSYinghai Lu 		return;
1087aef93c8bSYinghai Lu 
10883da99c97SYinghai Lu 	cpu_detect(c);
10893da99c97SYinghai Lu 
10903da99c97SYinghai Lu 	get_cpu_vendor(c);
10913da99c97SYinghai Lu 
10923da99c97SYinghai Lu 	get_cpu_cap(c);
10933da99c97SYinghai Lu 
1094f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
10953da99c97SYinghai Lu 		c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
1096b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32
1097c8e56d20SBorislav Petkov # ifdef CONFIG_SMP
1098cb8cc442SIngo Molnar 		c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1099f7627e25SThomas Gleixner # else
110001aaea1aSYinghai Lu 		c->apicid = c->initial_apicid;
1101f7627e25SThomas Gleixner # endif
1102b89d3b3eSYinghai Lu #endif
1103b89d3b3eSYinghai Lu 		c->phys_proc_id = c->initial_apicid;
1104f7627e25SThomas Gleixner 	}
1105f7627e25SThomas Gleixner 
1106f7627e25SThomas Gleixner 	get_model_name(c); /* Default name */
1107f7627e25SThomas Gleixner 
1108b6734c35SH. Peter Anvin 	detect_nopl(c);
11097a5d6704SAndy Lutomirski 
11107a5d6704SAndy Lutomirski 	detect_null_seg_behavior(c);
11110230bb03SAndy Lutomirski 
11120230bb03SAndy Lutomirski 	/*
11130230bb03SAndy Lutomirski 	 * ESPFIX is a strange bug.  All real CPUs have it.  Paravirt
11140230bb03SAndy Lutomirski 	 * systems that run Linux at CPL > 0 may or may not have the
11150230bb03SAndy Lutomirski 	 * issue, but, even if they have the issue, there's absolutely
11160230bb03SAndy Lutomirski 	 * nothing we can do about it because we can't use the real IRET
11170230bb03SAndy Lutomirski 	 * instruction.
11180230bb03SAndy Lutomirski 	 *
11190230bb03SAndy Lutomirski 	 * NB: For the time being, only 32-bit kernels support
11200230bb03SAndy Lutomirski 	 * X86_BUG_ESPFIX as such.  64-bit kernels directly choose
11210230bb03SAndy Lutomirski 	 * whether to apply espfix using paravirt hooks.  If any
11220230bb03SAndy Lutomirski 	 * non-paravirt system ever shows up that does *not* have the
11230230bb03SAndy Lutomirski 	 * ESPFIX issue, we can change this.
11240230bb03SAndy Lutomirski 	 */
11250230bb03SAndy Lutomirski #ifdef CONFIG_X86_32
11260230bb03SAndy Lutomirski # ifdef CONFIG_PARAVIRT
11270230bb03SAndy Lutomirski 	do {
11280230bb03SAndy Lutomirski 		extern void native_iret(void);
11290230bb03SAndy Lutomirski 		if (pv_cpu_ops.iret == native_iret)
11300230bb03SAndy Lutomirski 			set_cpu_bug(c, X86_BUG_ESPFIX);
11310230bb03SAndy Lutomirski 	} while (0);
11320230bb03SAndy Lutomirski # else
11330230bb03SAndy Lutomirski 	set_cpu_bug(c, X86_BUG_ESPFIX);
11340230bb03SAndy Lutomirski # endif
11350230bb03SAndy Lutomirski #endif
1136f7627e25SThomas Gleixner }
1137f7627e25SThomas Gleixner 
1138cbc82b17SPeter P Waskiewicz Jr static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1139cbc82b17SPeter P Waskiewicz Jr {
1140cbc82b17SPeter P Waskiewicz Jr 	/*
1141cbc82b17SPeter P Waskiewicz Jr 	 * The heavy lifting of max_rmid and cache_occ_scale are handled
1142cbc82b17SPeter P Waskiewicz Jr 	 * in get_cpu_cap().  Here we just set the max_rmid for the boot_cpu
1143cbc82b17SPeter P Waskiewicz Jr 	 * in case CQM bits really aren't there in this CPU.
1144cbc82b17SPeter P Waskiewicz Jr 	 */
1145cbc82b17SPeter P Waskiewicz Jr 	if (c != &boot_cpu_data) {
1146cbc82b17SPeter P Waskiewicz Jr 		boot_cpu_data.x86_cache_max_rmid =
1147cbc82b17SPeter P Waskiewicz Jr 			min(boot_cpu_data.x86_cache_max_rmid,
1148cbc82b17SPeter P Waskiewicz Jr 			    c->x86_cache_max_rmid);
1149cbc82b17SPeter P Waskiewicz Jr 	}
1150cbc82b17SPeter P Waskiewicz Jr }
1151cbc82b17SPeter P Waskiewicz Jr 
1152f7627e25SThomas Gleixner /*
11539d85eb91SThomas Gleixner  * Validate that ACPI/mptables have the same information about the
11549d85eb91SThomas Gleixner  * effective APIC id and update the package map.
1155d49597fdSThomas Gleixner  */
11569d85eb91SThomas Gleixner static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
1157d49597fdSThomas Gleixner {
1158d49597fdSThomas Gleixner #ifdef CONFIG_SMP
11599d85eb91SThomas Gleixner 	unsigned int apicid, cpu = smp_processor_id();
1160d49597fdSThomas Gleixner 
1161d49597fdSThomas Gleixner 	apicid = apic->cpu_present_to_apicid(cpu);
1162d49597fdSThomas Gleixner 
11639d85eb91SThomas Gleixner 	if (apicid != c->apicid) {
11649d85eb91SThomas Gleixner 		pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
1165d49597fdSThomas Gleixner 		       cpu, apicid, c->initial_apicid);
1166d49597fdSThomas Gleixner 	}
11679d85eb91SThomas Gleixner 	BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
1168d49597fdSThomas Gleixner #else
1169d49597fdSThomas Gleixner 	c->logical_proc_id = 0;
1170d49597fdSThomas Gleixner #endif
1171d49597fdSThomas Gleixner }
1172d49597fdSThomas Gleixner 
1173d49597fdSThomas Gleixner /*
1174f7627e25SThomas Gleixner  * This does the hard work of actually picking apart the CPU stuff...
1175f7627e25SThomas Gleixner  */
1176148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c)
1177f7627e25SThomas Gleixner {
1178f7627e25SThomas Gleixner 	int i;
1179f7627e25SThomas Gleixner 
1180f7627e25SThomas Gleixner 	c->loops_per_jiffy = loops_per_jiffy;
1181f7627e25SThomas Gleixner 	c->x86_cache_size = -1;
1182f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
1183f7627e25SThomas Gleixner 	c->x86_model = c->x86_mask = 0;	/* So far unknown... */
1184f7627e25SThomas Gleixner 	c->x86_vendor_id[0] = '\0'; /* Unset */
1185f7627e25SThomas Gleixner 	c->x86_model_id[0] = '\0';  /* Unset */
1186f7627e25SThomas Gleixner 	c->x86_max_cores = 1;
1187102bbe3aSYinghai Lu 	c->x86_coreid_bits = 0;
118879a8b9aaSBorislav Petkov 	c->cu_id = 0xff;
118911fdd252SYinghai Lu #ifdef CONFIG_X86_64
1190102bbe3aSYinghai Lu 	c->x86_clflush_size = 64;
119113c6c532SJan Beulich 	c->x86_phys_bits = 36;
119213c6c532SJan Beulich 	c->x86_virt_bits = 48;
1193102bbe3aSYinghai Lu #else
1194102bbe3aSYinghai Lu 	c->cpuid_level = -1;	/* CPUID not detected */
1195f7627e25SThomas Gleixner 	c->x86_clflush_size = 32;
119613c6c532SJan Beulich 	c->x86_phys_bits = 32;
119713c6c532SJan Beulich 	c->x86_virt_bits = 32;
1198102bbe3aSYinghai Lu #endif
1199102bbe3aSYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
1200f7627e25SThomas Gleixner 	memset(&c->x86_capability, 0, sizeof c->x86_capability);
1201f7627e25SThomas Gleixner 
1202f7627e25SThomas Gleixner 	generic_identify(c);
1203f7627e25SThomas Gleixner 
12043898534dSAndi Kleen 	if (this_cpu->c_identify)
1205f7627e25SThomas Gleixner 		this_cpu->c_identify(c);
1206f7627e25SThomas Gleixner 
12076a6256f9SAdam Buchbinder 	/* Clear/Set all flags overridden by options, after probe */
12088bf1ebcaSAndy Lutomirski 	apply_forced_caps(c);
12092759c328SYinghai Lu 
1210102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
1211cb8cc442SIngo Molnar 	c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1212102bbe3aSYinghai Lu #endif
1213102bbe3aSYinghai Lu 
1214f7627e25SThomas Gleixner 	/*
1215f7627e25SThomas Gleixner 	 * Vendor-specific initialization.  In this section we
1216f7627e25SThomas Gleixner 	 * canonicalize the feature flags, meaning if there are
1217f7627e25SThomas Gleixner 	 * features a certain CPU supports which CPUID doesn't
1218f7627e25SThomas Gleixner 	 * tell us, CPUID claiming incorrect flags, or other bugs,
1219f7627e25SThomas Gleixner 	 * we handle them here.
1220f7627e25SThomas Gleixner 	 *
1221f7627e25SThomas Gleixner 	 * At the end of this section, c->x86_capability better
1222f7627e25SThomas Gleixner 	 * indicate the features this CPU genuinely supports!
1223f7627e25SThomas Gleixner 	 */
1224f7627e25SThomas Gleixner 	if (this_cpu->c_init)
1225f7627e25SThomas Gleixner 		this_cpu->c_init(c);
1226f7627e25SThomas Gleixner 
1227f7627e25SThomas Gleixner 	/* Disable the PN if appropriate */
1228f7627e25SThomas Gleixner 	squash_the_stupid_serial_number(c);
1229f7627e25SThomas Gleixner 
1230b2cc2a07SH. Peter Anvin 	/* Set up SMEP/SMAP */
1231b2cc2a07SH. Peter Anvin 	setup_smep(c);
1232b2cc2a07SH. Peter Anvin 	setup_smap(c);
1233b2cc2a07SH. Peter Anvin 
1234f7627e25SThomas Gleixner 	/*
12350f3fa48aSIngo Molnar 	 * The vendor-specific functions might have changed features.
12360f3fa48aSIngo Molnar 	 * Now we do "generic changes."
1237f7627e25SThomas Gleixner 	 */
1238f7627e25SThomas Gleixner 
1239b38b0665SH. Peter Anvin 	/* Filter out anything that depends on CPUID levels we don't have */
1240b38b0665SH. Peter Anvin 	filter_cpuid_features(c, true);
1241b38b0665SH. Peter Anvin 
1242f7627e25SThomas Gleixner 	/* If the model name is still unset, do table lookup. */
1243f7627e25SThomas Gleixner 	if (!c->x86_model_id[0]) {
124402dde8b4SJan Beulich 		const char *p;
1245f7627e25SThomas Gleixner 		p = table_lookup_model(c);
1246f7627e25SThomas Gleixner 		if (p)
1247f7627e25SThomas Gleixner 			strcpy(c->x86_model_id, p);
1248f7627e25SThomas Gleixner 		else
1249f7627e25SThomas Gleixner 			/* Last resort... */
1250f7627e25SThomas Gleixner 			sprintf(c->x86_model_id, "%02x/%02x",
1251f7627e25SThomas Gleixner 				c->x86, c->x86_model);
1252f7627e25SThomas Gleixner 	}
1253f7627e25SThomas Gleixner 
1254102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
1255102bbe3aSYinghai Lu 	detect_ht(c);
1256102bbe3aSYinghai Lu #endif
1257102bbe3aSYinghai Lu 
125849d859d7SH. Peter Anvin 	x86_init_rdrand(c);
1259cbc82b17SPeter P Waskiewicz Jr 	x86_init_cache_qos(c);
126006976945SDave Hansen 	setup_pku(c);
12613e0c3737SYinghai Lu 
12623e0c3737SYinghai Lu 	/*
12636a6256f9SAdam Buchbinder 	 * Clear/Set all flags overridden by options, need do it
12643e0c3737SYinghai Lu 	 * before following smp all cpus cap AND.
12653e0c3737SYinghai Lu 	 */
12668bf1ebcaSAndy Lutomirski 	apply_forced_caps(c);
12673e0c3737SYinghai Lu 
1268f7627e25SThomas Gleixner 	/*
1269f7627e25SThomas Gleixner 	 * On SMP, boot_cpu_data holds the common feature set between
1270f7627e25SThomas Gleixner 	 * all CPUs; so make sure that we indicate which features are
1271f7627e25SThomas Gleixner 	 * common between the CPUs.  The first time this routine gets
1272f7627e25SThomas Gleixner 	 * executed, c == &boot_cpu_data.
1273f7627e25SThomas Gleixner 	 */
1274f7627e25SThomas Gleixner 	if (c != &boot_cpu_data) {
1275f7627e25SThomas Gleixner 		/* AND the already accumulated flags with these */
1276f7627e25SThomas Gleixner 		for (i = 0; i < NCAPINTS; i++)
1277f7627e25SThomas Gleixner 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
127865fc985bSBorislav Petkov 
127965fc985bSBorislav Petkov 		/* OR, i.e. replicate the bug flags */
128065fc985bSBorislav Petkov 		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
128165fc985bSBorislav Petkov 			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
1282f7627e25SThomas Gleixner 	}
1283f7627e25SThomas Gleixner 
1284f7627e25SThomas Gleixner 	/* Init Machine Check Exception if available. */
12855e09954aSBorislav Petkov 	mcheck_cpu_init(c);
128630d432dfSAndi Kleen 
128730d432dfSAndi Kleen 	select_idle_routine(c);
1288102bbe3aSYinghai Lu 
1289de2d9445STejun Heo #ifdef CONFIG_NUMA
1290102bbe3aSYinghai Lu 	numa_add_cpu(smp_processor_id());
1291102bbe3aSYinghai Lu #endif
1292f7627e25SThomas Gleixner }
1293f7627e25SThomas Gleixner 
12948b6c0ab1SIngo Molnar /*
12958b6c0ab1SIngo Molnar  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
12968b6c0ab1SIngo Molnar  * on 32-bit kernels:
12978b6c0ab1SIngo Molnar  */
1298cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32
1299cfda7bb9SAndy Lutomirski void enable_sep_cpu(void)
1300cfda7bb9SAndy Lutomirski {
13018b6c0ab1SIngo Molnar 	struct tss_struct *tss;
13028b6c0ab1SIngo Molnar 	int cpu;
1303cfda7bb9SAndy Lutomirski 
1304b3edfda4SBorislav Petkov 	if (!boot_cpu_has(X86_FEATURE_SEP))
1305b3edfda4SBorislav Petkov 		return;
1306b3edfda4SBorislav Petkov 
13078b6c0ab1SIngo Molnar 	cpu = get_cpu();
13088b6c0ab1SIngo Molnar 	tss = &per_cpu(cpu_tss, cpu);
13098b6c0ab1SIngo Molnar 
13108b6c0ab1SIngo Molnar 	/*
1311cf9328ccSAndy Lutomirski 	 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1312cf9328ccSAndy Lutomirski 	 * see the big comment in struct x86_hw_tss's definition.
13138b6c0ab1SIngo Molnar 	 */
1314cfda7bb9SAndy Lutomirski 
1315cfda7bb9SAndy Lutomirski 	tss->x86_tss.ss1 = __KERNEL_CS;
13168b6c0ab1SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
13178b6c0ab1SIngo Molnar 
1318cf9328ccSAndy Lutomirski 	wrmsr(MSR_IA32_SYSENTER_ESP,
131972f5e08dSAndy Lutomirski 	      (unsigned long)&get_cpu_entry_area(cpu)->tss +
132072f5e08dSAndy Lutomirski 	      offsetofend(struct tss_struct, SYSENTER_stack),
1321cf9328ccSAndy Lutomirski 	      0);
13228b6c0ab1SIngo Molnar 
13234c8cd0c5SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
13248b6c0ab1SIngo Molnar 
1325cfda7bb9SAndy Lutomirski 	put_cpu();
1326cfda7bb9SAndy Lutomirski }
1327e04d645fSGlauber Costa #endif
1328e04d645fSGlauber Costa 
1329f7627e25SThomas Gleixner void __init identify_boot_cpu(void)
1330f7627e25SThomas Gleixner {
1331f7627e25SThomas Gleixner 	identify_cpu(&boot_cpu_data);
1332102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1333f7627e25SThomas Gleixner 	sysenter_setup();
1334f7627e25SThomas Gleixner 	enable_sep_cpu();
1335102bbe3aSYinghai Lu #endif
1336e0ba94f1SAlex Shi 	cpu_detect_tlb(&boot_cpu_data);
1337f7627e25SThomas Gleixner }
1338f7627e25SThomas Gleixner 
1339148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c)
1340f7627e25SThomas Gleixner {
1341f7627e25SThomas Gleixner 	BUG_ON(c == &boot_cpu_data);
1342f7627e25SThomas Gleixner 	identify_cpu(c);
1343102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1344f7627e25SThomas Gleixner 	enable_sep_cpu();
1345102bbe3aSYinghai Lu #endif
1346f7627e25SThomas Gleixner 	mtrr_ap_init();
13479d85eb91SThomas Gleixner 	validate_apic_and_package_id(c);
1348f7627e25SThomas Gleixner }
1349f7627e25SThomas Gleixner 
1350191679fdSAndi Kleen static __init int setup_noclflush(char *arg)
1351191679fdSAndi Kleen {
1352840d2830SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1353da4aaa7dSH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1354191679fdSAndi Kleen 	return 1;
1355191679fdSAndi Kleen }
1356191679fdSAndi Kleen __setup("noclflush", setup_noclflush);
1357191679fdSAndi Kleen 
1358148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c)
1359f7627e25SThomas Gleixner {
136002dde8b4SJan Beulich 	const char *vendor = NULL;
1361f7627e25SThomas Gleixner 
13620f3fa48aSIngo Molnar 	if (c->x86_vendor < X86_VENDOR_NUM) {
1363f7627e25SThomas Gleixner 		vendor = this_cpu->c_vendor;
13640f3fa48aSIngo Molnar 	} else {
13650f3fa48aSIngo Molnar 		if (c->cpuid_level >= 0)
1366f7627e25SThomas Gleixner 			vendor = c->x86_vendor_id;
13670f3fa48aSIngo Molnar 	}
1368f7627e25SThomas Gleixner 
1369bd32a8cfSYinghai Lu 	if (vendor && !strstr(c->x86_model_id, vendor))
13701b74dde7SChen Yucong 		pr_cont("%s ", vendor);
1371f7627e25SThomas Gleixner 
13729d31d35bSYinghai Lu 	if (c->x86_model_id[0])
13731b74dde7SChen Yucong 		pr_cont("%s", c->x86_model_id);
1374f7627e25SThomas Gleixner 	else
13751b74dde7SChen Yucong 		pr_cont("%d86", c->x86);
1376f7627e25SThomas Gleixner 
13771b74dde7SChen Yucong 	pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
1378924e101aSBorislav Petkov 
1379f7627e25SThomas Gleixner 	if (c->x86_mask || c->cpuid_level >= 0)
13801b74dde7SChen Yucong 		pr_cont(", stepping: 0x%x)\n", c->x86_mask);
1381f7627e25SThomas Gleixner 	else
13821b74dde7SChen Yucong 		pr_cont(")\n");
1383f7627e25SThomas Gleixner }
1384f7627e25SThomas Gleixner 
13850c2a3913SAndi Kleen /*
13860c2a3913SAndi Kleen  * clearcpuid= was already parsed in fpu__init_parse_early_param.
13870c2a3913SAndi Kleen  * But we need to keep a dummy __setup around otherwise it would
13880c2a3913SAndi Kleen  * show up as an environment variable for init.
13890c2a3913SAndi Kleen  */
13900c2a3913SAndi Kleen static __init int setup_clearcpuid(char *arg)
1391ac72e788SAndi Kleen {
1392ac72e788SAndi Kleen 	return 1;
1393ac72e788SAndi Kleen }
13940c2a3913SAndi Kleen __setup("clearcpuid=", setup_clearcpuid);
1395ac72e788SAndi Kleen 
1396d5494d4fSYinghai Lu #ifdef CONFIG_X86_64
1397947e76cdSBrian Gerst DEFINE_PER_CPU_FIRST(union irq_stack_union,
1398277d5b40SAndi Kleen 		     irq_stack_union) __aligned(PAGE_SIZE) __visible;
13990f3fa48aSIngo Molnar 
1400bdf977b3STejun Heo /*
1401a7fcf28dSAndy Lutomirski  * The following percpu variables are hot.  Align current_task to
1402a7fcf28dSAndy Lutomirski  * cacheline size such that they fall in the same cacheline.
1403bdf977b3STejun Heo  */
1404bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1405bdf977b3STejun Heo 	&init_task;
1406bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1407d5494d4fSYinghai Lu 
1408bdf977b3STejun Heo DEFINE_PER_CPU(char *, irq_stack_ptr) =
14094950d6d4SJosh Poimboeuf 	init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE;
1410bdf977b3STejun Heo 
1411277d5b40SAndi Kleen DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
1412d5494d4fSYinghai Lu 
1413c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1414c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1415c2daa3beSPeter Zijlstra 
1416d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */
1417d5494d4fSYinghai Lu void syscall_init(void)
1418d5494d4fSYinghai Lu {
14193386bc8aSAndy Lutomirski 	extern char _entry_trampoline[];
14203386bc8aSAndy Lutomirski 	extern char entry_SYSCALL_64_trampoline[];
14213386bc8aSAndy Lutomirski 
142272f5e08dSAndy Lutomirski 	int cpu = smp_processor_id();
14233386bc8aSAndy Lutomirski 	unsigned long SYSCALL64_entry_trampoline =
14243386bc8aSAndy Lutomirski 		(unsigned long)get_cpu_entry_area(cpu)->entry_trampoline +
14253386bc8aSAndy Lutomirski 		(entry_SYSCALL_64_trampoline - _entry_trampoline);
142672f5e08dSAndy Lutomirski 
142731ac34caSBorislav Petkov 	wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
14283386bc8aSAndy Lutomirski 	wrmsrl(MSR_LSTAR, SYSCALL64_entry_trampoline);
1429d56fe4bfSIngo Molnar 
1430d56fe4bfSIngo Molnar #ifdef CONFIG_IA32_EMULATION
143147edb651SAndy Lutomirski 	wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
1432a76c7f46SDenys Vlasenko 	/*
1433487d1edbSDenys Vlasenko 	 * This only works on Intel CPUs.
1434487d1edbSDenys Vlasenko 	 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1435487d1edbSDenys Vlasenko 	 * This does not cause SYSENTER to jump to the wrong location, because
1436487d1edbSDenys Vlasenko 	 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1437a76c7f46SDenys Vlasenko 	 */
1438a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
14391a79797bSAndy Lutomirski 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
144072f5e08dSAndy Lutomirski 		    (unsigned long)&get_cpu_entry_area(cpu)->tss +
14411a79797bSAndy Lutomirski 		    offsetofend(struct tss_struct, SYSENTER_stack));
14424c8cd0c5SIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
1443d56fe4bfSIngo Molnar #else
144447edb651SAndy Lutomirski 	wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
14456b51311cSBorislav Petkov 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1446d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1447d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1448d5494d4fSYinghai Lu #endif
1449d5494d4fSYinghai Lu 
1450d5494d4fSYinghai Lu 	/* Flags to clear on syscall */
1451d5494d4fSYinghai Lu 	wrmsrl(MSR_SYSCALL_MASK,
145263bcff2aSH. Peter Anvin 	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
14538c7aa698SAndy Lutomirski 	       X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1454d5494d4fSYinghai Lu }
1455d5494d4fSYinghai Lu 
1456d5494d4fSYinghai Lu /*
1457d5494d4fSYinghai Lu  * Copies of the original ist values from the tss are only accessed during
1458d5494d4fSYinghai Lu  * debugging, no special alignment required.
1459d5494d4fSYinghai Lu  */
1460d5494d4fSYinghai Lu DEFINE_PER_CPU(struct orig_ist, orig_ist);
1461d5494d4fSYinghai Lu 
1462228bdaa9SSteven Rostedt static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
146342181186SSteven Rostedt DEFINE_PER_CPU(int, debug_stack_usage);
1464228bdaa9SSteven Rostedt 
1465228bdaa9SSteven Rostedt int is_debug_stack(unsigned long addr)
1466228bdaa9SSteven Rostedt {
146789cbc767SChristoph Lameter 	return __this_cpu_read(debug_stack_usage) ||
146889cbc767SChristoph Lameter 		(addr <= __this_cpu_read(debug_stack_addr) &&
146989cbc767SChristoph Lameter 		 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
1470228bdaa9SSteven Rostedt }
14710f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(is_debug_stack);
1472228bdaa9SSteven Rostedt 
1473629f4f9dSSeiji Aguchi DEFINE_PER_CPU(u32, debug_idt_ctr);
1474f8988175SSteven Rostedt 
1475228bdaa9SSteven Rostedt void debug_stack_set_zero(void)
1476228bdaa9SSteven Rostedt {
1477629f4f9dSSeiji Aguchi 	this_cpu_inc(debug_idt_ctr);
1478629f4f9dSSeiji Aguchi 	load_current_idt();
1479228bdaa9SSteven Rostedt }
14800f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_set_zero);
1481228bdaa9SSteven Rostedt 
1482228bdaa9SSteven Rostedt void debug_stack_reset(void)
1483228bdaa9SSteven Rostedt {
1484629f4f9dSSeiji Aguchi 	if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
1485f8988175SSteven Rostedt 		return;
1486629f4f9dSSeiji Aguchi 	if (this_cpu_dec_return(debug_idt_ctr) == 0)
1487629f4f9dSSeiji Aguchi 		load_current_idt();
1488228bdaa9SSteven Rostedt }
14890f46efebSMasami Hiramatsu NOKPROBE_SYMBOL(debug_stack_reset);
1490228bdaa9SSteven Rostedt 
14910f3fa48aSIngo Molnar #else	/* CONFIG_X86_64 */
1492d5494d4fSYinghai Lu 
1493bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1494bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1495c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1496c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1497bdf977b3STejun Heo 
1498a7fcf28dSAndy Lutomirski /*
1499a7fcf28dSAndy Lutomirski  * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1500a7fcf28dSAndy Lutomirski  * the top of the kernel stack.  Use an extra percpu variable to track the
1501a7fcf28dSAndy Lutomirski  * top of the kernel stack directly.
1502a7fcf28dSAndy Lutomirski  */
1503a7fcf28dSAndy Lutomirski DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1504a7fcf28dSAndy Lutomirski 	(unsigned long)&init_thread_union + THREAD_SIZE;
1505a7fcf28dSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1506a7fcf28dSAndy Lutomirski 
150760a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR
150853f82452SJeremy Fitzhardinge DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
150960a5317fSTejun Heo #endif
151060a5317fSTejun Heo 
15110f3fa48aSIngo Molnar #endif	/* CONFIG_X86_64 */
1512f7627e25SThomas Gleixner 
1513f7627e25SThomas Gleixner /*
15149766cdbcSJaswinder Singh Rajput  * Clear all 6 debug registers:
15159766cdbcSJaswinder Singh Rajput  */
15169766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void)
15179766cdbcSJaswinder Singh Rajput {
15189766cdbcSJaswinder Singh Rajput 	int i;
15199766cdbcSJaswinder Singh Rajput 
15209766cdbcSJaswinder Singh Rajput 	for (i = 0; i < 8; i++) {
15219766cdbcSJaswinder Singh Rajput 		/* Ignore db4, db5 */
15229766cdbcSJaswinder Singh Rajput 		if ((i == 4) || (i == 5))
15239766cdbcSJaswinder Singh Rajput 			continue;
15249766cdbcSJaswinder Singh Rajput 
15259766cdbcSJaswinder Singh Rajput 		set_debugreg(0, i);
15269766cdbcSJaswinder Singh Rajput 	}
15279766cdbcSJaswinder Singh Rajput }
1528f7627e25SThomas Gleixner 
15290bb9fef9SJason Wessel #ifdef CONFIG_KGDB
15300bb9fef9SJason Wessel /*
15310bb9fef9SJason Wessel  * Restore debug regs if using kgdbwait and you have a kernel debugger
15320bb9fef9SJason Wessel  * connection established.
15330bb9fef9SJason Wessel  */
15340bb9fef9SJason Wessel static void dbg_restore_debug_regs(void)
15350bb9fef9SJason Wessel {
15360bb9fef9SJason Wessel 	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
15370bb9fef9SJason Wessel 		arch_kgdb_ops.correct_hw_break();
15380bb9fef9SJason Wessel }
15390bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */
15400bb9fef9SJason Wessel #define dbg_restore_debug_regs()
15410bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */
15420bb9fef9SJason Wessel 
1543ce4b1b16SIgor Mammedov static void wait_for_master_cpu(int cpu)
1544ce4b1b16SIgor Mammedov {
1545ce4b1b16SIgor Mammedov #ifdef CONFIG_SMP
1546ce4b1b16SIgor Mammedov 	/*
1547ce4b1b16SIgor Mammedov 	 * wait for ACK from master CPU before continuing
1548ce4b1b16SIgor Mammedov 	 * with AP initialization
1549ce4b1b16SIgor Mammedov 	 */
1550ce4b1b16SIgor Mammedov 	WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1551ce4b1b16SIgor Mammedov 	while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1552ce4b1b16SIgor Mammedov 		cpu_relax();
1553ce4b1b16SIgor Mammedov #endif
1554ce4b1b16SIgor Mammedov }
1555ce4b1b16SIgor Mammedov 
1556f7627e25SThomas Gleixner /*
1557f7627e25SThomas Gleixner  * cpu_init() initializes state that is per-CPU. Some data is already
1558f7627e25SThomas Gleixner  * initialized (naturally) in the bootstrap process, such as the GDT
1559f7627e25SThomas Gleixner  * and IDT. We reload them nevertheless, this function acts as a
1560f7627e25SThomas Gleixner  * 'CPU state barrier', nothing should get across.
15611ba76586SYinghai Lu  * A lot of state is already set up in PDA init for 64 bit
1562f7627e25SThomas Gleixner  */
15631ba76586SYinghai Lu #ifdef CONFIG_X86_64
15640f3fa48aSIngo Molnar 
1565148f9bb8SPaul Gortmaker void cpu_init(void)
15661ba76586SYinghai Lu {
15670fe1e009STejun Heo 	struct orig_ist *oist;
15681ba76586SYinghai Lu 	struct task_struct *me;
15690f3fa48aSIngo Molnar 	struct tss_struct *t;
15700f3fa48aSIngo Molnar 	unsigned long v;
1571fb59831bSAndy Lutomirski 	int cpu = raw_smp_processor_id();
15721ba76586SYinghai Lu 	int i;
15731ba76586SYinghai Lu 
1574ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1575ce4b1b16SIgor Mammedov 
1576e6ebf5deSFenghua Yu 	/*
15771e02ce4cSAndy Lutomirski 	 * Initialize the CR4 shadow before doing anything that could
15781e02ce4cSAndy Lutomirski 	 * try to read it.
15791e02ce4cSAndy Lutomirski 	 */
15801e02ce4cSAndy Lutomirski 	cr4_init_shadow();
15811e02ce4cSAndy Lutomirski 
1582777284b6SBorislav Petkov 	if (cpu)
1583e6ebf5deSFenghua Yu 		load_ucode_ap();
1584e6ebf5deSFenghua Yu 
158524933b82SAndy Lutomirski 	t = &per_cpu(cpu_tss, cpu);
15860fe1e009STejun Heo 	oist = &per_cpu(orig_ist, cpu);
15870f3fa48aSIngo Molnar 
1588e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA
158927fd185fSFenghua Yu 	if (this_cpu_read(numa_node) == 0 &&
1590e534c7c5SLee Schermerhorn 	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
1591e534c7c5SLee Schermerhorn 		set_numa_node(early_cpu_to_node(cpu));
1592e7a22c1eSBrian Gerst #endif
15931ba76586SYinghai Lu 
15941ba76586SYinghai Lu 	me = current;
15951ba76586SYinghai Lu 
15962eaad1fdSMike Travis 	pr_debug("Initializing CPU#%d\n", cpu);
15971ba76586SYinghai Lu 
1598375074ccSAndy Lutomirski 	cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
15991ba76586SYinghai Lu 
16001ba76586SYinghai Lu 	/*
16011ba76586SYinghai Lu 	 * Initialize the per-CPU GDT with the boot GDT,
16021ba76586SYinghai Lu 	 * and set up the GDT descriptor:
16031ba76586SYinghai Lu 	 */
16041ba76586SYinghai Lu 
1605552be871SBrian Gerst 	switch_to_new_gdt(cpu);
16062697fbd5SBrian Gerst 	loadsegment(fs, 0);
16072697fbd5SBrian Gerst 
1608cf910e83SSeiji Aguchi 	load_current_idt();
16091ba76586SYinghai Lu 
16101ba76586SYinghai Lu 	memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
16111ba76586SYinghai Lu 	syscall_init();
16121ba76586SYinghai Lu 
16131ba76586SYinghai Lu 	wrmsrl(MSR_FS_BASE, 0);
16141ba76586SYinghai Lu 	wrmsrl(MSR_KERNEL_GS_BASE, 0);
16151ba76586SYinghai Lu 	barrier();
16161ba76586SYinghai Lu 
16174763ed4dSH. Peter Anvin 	x86_configure_nx();
1618659006bfSThomas Gleixner 	x2apic_setup();
16191ba76586SYinghai Lu 
16201ba76586SYinghai Lu 	/*
16211ba76586SYinghai Lu 	 * set up and load the per-CPU TSS
16221ba76586SYinghai Lu 	 */
16230fe1e009STejun Heo 	if (!oist->ist[0]) {
1624*40e7f949SAndy Lutomirski 		char *estacks = get_cpu_entry_area(cpu)->exception_stacks;
16250f3fa48aSIngo Molnar 
16261ba76586SYinghai Lu 		for (v = 0; v < N_EXCEPTION_STACKS; v++) {
16270f3fa48aSIngo Molnar 			estacks += exception_stack_sizes[v];
16280fe1e009STejun Heo 			oist->ist[v] = t->x86_tss.ist[v] =
16291ba76586SYinghai Lu 					(unsigned long)estacks;
1630228bdaa9SSteven Rostedt 			if (v == DEBUG_STACK-1)
1631228bdaa9SSteven Rostedt 				per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
16321ba76586SYinghai Lu 		}
16331ba76586SYinghai Lu 	}
16341ba76586SYinghai Lu 
16357fb983b4SAndy Lutomirski 	t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
16360f3fa48aSIngo Molnar 
16371ba76586SYinghai Lu 	/*
16381ba76586SYinghai Lu 	 * <= is required because the CPU will access up to
16391ba76586SYinghai Lu 	 * 8 bits beyond the end of the IO permission bitmap.
16401ba76586SYinghai Lu 	 */
16411ba76586SYinghai Lu 	for (i = 0; i <= IO_BITMAP_LONGS; i++)
16421ba76586SYinghai Lu 		t->io_bitmap[i] = ~0UL;
16431ba76586SYinghai Lu 
1644f1f10076SVegard Nossum 	mmgrab(&init_mm);
16451ba76586SYinghai Lu 	me->active_mm = &init_mm;
16468c5dfd25SStoyan Gaydarov 	BUG_ON(me->mm);
164772c0098dSAndy Lutomirski 	initialize_tlbstate_and_flush();
16481ba76586SYinghai Lu 	enter_lazy_tlb(&init_mm, me);
16491ba76586SYinghai Lu 
165020bb8344SAndy Lutomirski 	/*
16517f2590a1SAndy Lutomirski 	 * Initialize the TSS.  sp0 points to the entry trampoline stack
16527f2590a1SAndy Lutomirski 	 * regardless of what task is running.
165320bb8344SAndy Lutomirski 	 */
165472f5e08dSAndy Lutomirski 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
16551ba76586SYinghai Lu 	load_TR_desc();
16567f2590a1SAndy Lutomirski 	load_sp0((unsigned long)&get_cpu_entry_area(cpu)->tss +
16577f2590a1SAndy Lutomirski 		 offsetofend(struct tss_struct, SYSENTER_stack));
165820bb8344SAndy Lutomirski 
165937868fe1SAndy Lutomirski 	load_mm_ldt(&init_mm);
16601ba76586SYinghai Lu 
16619766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
16620bb9fef9SJason Wessel 	dbg_restore_debug_regs();
16631ba76586SYinghai Lu 
166421c4cd10SIngo Molnar 	fpu__init_cpu();
16651ba76586SYinghai Lu 
16661ba76586SYinghai Lu 	if (is_uv_system())
16671ba76586SYinghai Lu 		uv_cpu_init();
166869218e47SThomas Garnier 
166969218e47SThomas Garnier 	load_fixmap_gdt(cpu);
16701ba76586SYinghai Lu }
16711ba76586SYinghai Lu 
16721ba76586SYinghai Lu #else
16731ba76586SYinghai Lu 
1674148f9bb8SPaul Gortmaker void cpu_init(void)
1675f7627e25SThomas Gleixner {
1676f7627e25SThomas Gleixner 	int cpu = smp_processor_id();
1677f7627e25SThomas Gleixner 	struct task_struct *curr = current;
167824933b82SAndy Lutomirski 	struct tss_struct *t = &per_cpu(cpu_tss, cpu);
1679f7627e25SThomas Gleixner 
1680ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1681e6ebf5deSFenghua Yu 
16825b2bdbc8SSteven Rostedt 	/*
16835b2bdbc8SSteven Rostedt 	 * Initialize the CR4 shadow before doing anything that could
16845b2bdbc8SSteven Rostedt 	 * try to read it.
16855b2bdbc8SSteven Rostedt 	 */
16865b2bdbc8SSteven Rostedt 	cr4_init_shadow();
16875b2bdbc8SSteven Rostedt 
1688ce4b1b16SIgor Mammedov 	show_ucode_info_early();
1689f7627e25SThomas Gleixner 
16901b74dde7SChen Yucong 	pr_info("Initializing CPU#%d\n", cpu);
1691f7627e25SThomas Gleixner 
1692362f924bSBorislav Petkov 	if (cpu_feature_enabled(X86_FEATURE_VME) ||
169359e21e3dSBorislav Petkov 	    boot_cpu_has(X86_FEATURE_TSC) ||
1694362f924bSBorislav Petkov 	    boot_cpu_has(X86_FEATURE_DE))
1695375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1696f7627e25SThomas Gleixner 
1697cf910e83SSeiji Aguchi 	load_current_idt();
1698552be871SBrian Gerst 	switch_to_new_gdt(cpu);
1699f7627e25SThomas Gleixner 
1700f7627e25SThomas Gleixner 	/*
1701f7627e25SThomas Gleixner 	 * Set up and load the per-CPU TSS and LDT
1702f7627e25SThomas Gleixner 	 */
1703f1f10076SVegard Nossum 	mmgrab(&init_mm);
1704f7627e25SThomas Gleixner 	curr->active_mm = &init_mm;
17058c5dfd25SStoyan Gaydarov 	BUG_ON(curr->mm);
170672c0098dSAndy Lutomirski 	initialize_tlbstate_and_flush();
1707f7627e25SThomas Gleixner 	enter_lazy_tlb(&init_mm, curr);
1708f7627e25SThomas Gleixner 
170920bb8344SAndy Lutomirski 	/*
171020bb8344SAndy Lutomirski 	 * Initialize the TSS.  Don't bother initializing sp0, as the initial
171120bb8344SAndy Lutomirski 	 * task never enters user mode.
171220bb8344SAndy Lutomirski 	 */
171372f5e08dSAndy Lutomirski 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
1714f7627e25SThomas Gleixner 	load_TR_desc();
171520bb8344SAndy Lutomirski 
171637868fe1SAndy Lutomirski 	load_mm_ldt(&init_mm);
1717f7627e25SThomas Gleixner 
17187fb983b4SAndy Lutomirski 	t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
1719f9a196b8SThomas Gleixner 
1720f7627e25SThomas Gleixner #ifdef CONFIG_DOUBLEFAULT
1721f7627e25SThomas Gleixner 	/* Set up doublefault TSS pointer in the GDT */
1722f7627e25SThomas Gleixner 	__set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1723f7627e25SThomas Gleixner #endif
1724f7627e25SThomas Gleixner 
17259766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
17260bb9fef9SJason Wessel 	dbg_restore_debug_regs();
1727f7627e25SThomas Gleixner 
172821c4cd10SIngo Molnar 	fpu__init_cpu();
172969218e47SThomas Garnier 
173069218e47SThomas Garnier 	load_fixmap_gdt(cpu);
1731f7627e25SThomas Gleixner }
17321ba76586SYinghai Lu #endif
17335700f743SBorislav Petkov 
1734b51ef52dSLaura Abbott static void bsp_resume(void)
1735b51ef52dSLaura Abbott {
1736b51ef52dSLaura Abbott 	if (this_cpu->c_bsp_resume)
1737b51ef52dSLaura Abbott 		this_cpu->c_bsp_resume(&boot_cpu_data);
1738b51ef52dSLaura Abbott }
1739b51ef52dSLaura Abbott 
1740b51ef52dSLaura Abbott static struct syscore_ops cpu_syscore_ops = {
1741b51ef52dSLaura Abbott 	.resume		= bsp_resume,
1742b51ef52dSLaura Abbott };
1743b51ef52dSLaura Abbott 
1744b51ef52dSLaura Abbott static int __init init_cpu_syscore(void)
1745b51ef52dSLaura Abbott {
1746b51ef52dSLaura Abbott 	register_syscore_ops(&cpu_syscore_ops);
1747b51ef52dSLaura Abbott 	return 0;
1748b51ef52dSLaura Abbott }
1749b51ef52dSLaura Abbott core_initcall(init_cpu_syscore);
1750