xref: /linux/arch/x86/kernel/cpu/common.c (revision 8a1dc55a3f3ef0a723c3c117a567e7b5dd2c1793)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22458e53fSKirill A. Shutemov /* cpu_feature_enabled() cannot be used this early */
32458e53fSKirill A. Shutemov #define USE_EARLY_PGTABLE_L5
42458e53fSKirill A. Shutemov 
557c8a661SMike Rapoport #include <linux/memblock.h>
69766cdbcSJaswinder Singh Rajput #include <linux/linkage.h>
7f0fc4affSYinghai Lu #include <linux/bitops.h>
89766cdbcSJaswinder Singh Rajput #include <linux/kernel.h>
9186f4360SPaul Gortmaker #include <linux/export.h>
10f7627e25SThomas Gleixner #include <linux/percpu.h>
119766cdbcSJaswinder Singh Rajput #include <linux/string.h>
12ee098e1aSBorislav Petkov #include <linux/ctype.h>
139766cdbcSJaswinder Singh Rajput #include <linux/delay.h>
1468e21be2SIngo Molnar #include <linux/sched/mm.h>
15e6017571SIngo Molnar #include <linux/sched/clock.h>
169164bb4aSIngo Molnar #include <linux/sched/task.h>
17b47a3698SBenjamin Thiel #include <linux/sched/smt.h>
189766cdbcSJaswinder Singh Rajput #include <linux/init.h>
190f46efebSMasami Hiramatsu #include <linux/kprobes.h>
209766cdbcSJaswinder Singh Rajput #include <linux/kgdb.h>
219766cdbcSJaswinder Singh Rajput #include <linux/smp.h>
229766cdbcSJaswinder Singh Rajput #include <linux/io.h>
23b51ef52dSLaura Abbott #include <linux/syscore_ops.h>
2465fddcfcSMike Rapoport #include <linux/pgtable.h>
259766cdbcSJaswinder Singh Rajput 
261ef5423aSMike Hommey #include <asm/cmdline.h>
279766cdbcSJaswinder Singh Rajput #include <asm/stackprotector.h>
28cdd6c482SIngo Molnar #include <asm/perf_event.h>
29f7627e25SThomas Gleixner #include <asm/mmu_context.h>
30dc4e0021SAndy Lutomirski #include <asm/doublefault.h>
3149d859d7SH. Peter Anvin #include <asm/archrandom.h>
329766cdbcSJaswinder Singh Rajput #include <asm/hypervisor.h>
339766cdbcSJaswinder Singh Rajput #include <asm/processor.h>
341e02ce4cSAndy Lutomirski #include <asm/tlbflush.h>
35f649e938SPaul Gortmaker #include <asm/debugreg.h>
369766cdbcSJaswinder Singh Rajput #include <asm/sections.h>
37f40c3300SAndy Lutomirski #include <asm/vsyscall.h>
388bdbd962SAlan Cox #include <linux/topology.h>
398bdbd962SAlan Cox #include <linux/cpumask.h>
4060063497SArun Sharma #include <linux/atomic.h>
419766cdbcSJaswinder Singh Rajput #include <asm/proto.h>
429766cdbcSJaswinder Singh Rajput #include <asm/setup.h>
43f7627e25SThomas Gleixner #include <asm/apic.h>
449766cdbcSJaswinder Singh Rajput #include <asm/desc.h>
4578f7f1e5SIngo Molnar #include <asm/fpu/internal.h>
469766cdbcSJaswinder Singh Rajput #include <asm/mtrr.h>
470274f955SGrzegorz Andrejczuk #include <asm/hwcap2.h>
488bdbd962SAlan Cox #include <linux/numa.h>
490cd39f46SPeter Zijlstra #include <asm/numa.h>
509766cdbcSJaswinder Singh Rajput #include <asm/asm.h>
510f6ff2bcSDave Hansen #include <asm/bugs.h>
529766cdbcSJaswinder Singh Rajput #include <asm/cpu.h>
539766cdbcSJaswinder Singh Rajput #include <asm/mce.h>
549766cdbcSJaswinder Singh Rajput #include <asm/msr.h>
55eb243d1dSIngo Molnar #include <asm/memtype.h>
56d288e1cfSFenghua Yu #include <asm/microcode.h>
57d288e1cfSFenghua Yu #include <asm/microcode_intel.h>
58fec9434aSDavid Woodhouse #include <asm/intel-family.h>
59fec9434aSDavid Woodhouse #include <asm/cpu_device_id.h>
60bdbcdd48STejun Heo #include <asm/uv/uv.h>
61939ef713SChang S. Bae #include <asm/sigframe.h>
62f7627e25SThomas Gleixner 
63f7627e25SThomas Gleixner #include "cpu.h"
64f7627e25SThomas Gleixner 
650274f955SGrzegorz Andrejczuk u32 elf_hwcap2 __read_mostly;
660274f955SGrzegorz Andrejczuk 
67c2d1cec1SMike Travis /* all of these masks are initialized in setup_cpu_local_masks() */
68c2d1cec1SMike Travis cpumask_var_t cpu_initialized_mask;
699766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callout_mask;
709766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callin_mask;
71c2d1cec1SMike Travis 
72c2d1cec1SMike Travis /* representing cpus for which sibling maps can be computed */
73c2d1cec1SMike Travis cpumask_var_t cpu_sibling_setup_mask;
74c2d1cec1SMike Travis 
75f8b64d08SBorislav Petkov /* Number of siblings per CPU package */
76f8b64d08SBorislav Petkov int smp_num_siblings = 1;
77f8b64d08SBorislav Petkov EXPORT_SYMBOL(smp_num_siblings);
78f8b64d08SBorislav Petkov 
79f8b64d08SBorislav Petkov /* Last level cache ID of each logical CPU */
80f8b64d08SBorislav Petkov DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
81f8b64d08SBorislav Petkov 
822f2f52baSBrian Gerst /* correctly size the local cpu masks */
834369f1fbSIngo Molnar void __init setup_cpu_local_masks(void)
842f2f52baSBrian Gerst {
852f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_initialized_mask);
862f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callin_mask);
872f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callout_mask);
882f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
892f2f52baSBrian Gerst }
902f2f52baSBrian Gerst 
91148f9bb8SPaul Gortmaker static void default_init(struct cpuinfo_x86 *c)
92e8055139SOndrej Zary {
93e8055139SOndrej Zary #ifdef CONFIG_X86_64
9427c13eceSBorislav Petkov 	cpu_detect_cache_sizes(c);
95e8055139SOndrej Zary #else
96e8055139SOndrej Zary 	/* Not much we can do here... */
97e8055139SOndrej Zary 	/* Check if at least it has cpuid */
98e8055139SOndrej Zary 	if (c->cpuid_level == -1) {
99e8055139SOndrej Zary 		/* No cpuid. It must be an ancient CPU */
100e8055139SOndrej Zary 		if (c->x86 == 4)
101e8055139SOndrej Zary 			strcpy(c->x86_model_id, "486");
102e8055139SOndrej Zary 		else if (c->x86 == 3)
103e8055139SOndrej Zary 			strcpy(c->x86_model_id, "386");
104e8055139SOndrej Zary 	}
105e8055139SOndrej Zary #endif
106e8055139SOndrej Zary }
107e8055139SOndrej Zary 
108148f9bb8SPaul Gortmaker static const struct cpu_dev default_cpu = {
109e8055139SOndrej Zary 	.c_init		= default_init,
110e8055139SOndrej Zary 	.c_vendor	= "Unknown",
111e8055139SOndrej Zary 	.c_x86_vendor	= X86_VENDOR_UNKNOWN,
112e8055139SOndrej Zary };
113e8055139SOndrej Zary 
114148f9bb8SPaul Gortmaker static const struct cpu_dev *this_cpu = &default_cpu;
1150a488a53SYinghai Lu 
11606deef89SBrian Gerst DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
117950ad7ffSYinghai Lu #ifdef CONFIG_X86_64
11806deef89SBrian Gerst 	/*
11906deef89SBrian Gerst 	 * We need valid kernel segments for data and code in long mode too
120950ad7ffSYinghai Lu 	 * IRET will check the segment types  kkeil 2000/10/28
121950ad7ffSYinghai Lu 	 * Also sysret mandates a special GDT layout
12206deef89SBrian Gerst 	 *
1239766cdbcSJaswinder Singh Rajput 	 * TLS descriptors are currently at a different place compared to i386.
12406deef89SBrian Gerst 	 * Hopefully nobody expects them at a fixed place (Wine?)
125950ad7ffSYinghai Lu 	 */
1261e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL32_CS]		= GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
1271e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
1281e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
1291e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER32_CS]	= GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
1301e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
1311e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
132950ad7ffSYinghai Lu #else
1331e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
1341e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1351e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
1361e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
137f7627e25SThomas Gleixner 	/*
138f7627e25SThomas Gleixner 	 * Segments used for calling PnP BIOS have byte granularity.
139f7627e25SThomas Gleixner 	 * They code segments and data segments have fixed 64k limits,
140f7627e25SThomas Gleixner 	 * the transfer segment sizes are set at run time.
141f7627e25SThomas Gleixner 	 */
1426842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1431e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS32]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
1446842ef0eSGlauber de Oliveira Costa 	/* 16-bit code */
1451e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS16]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1466842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1471e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_DS]		= GDT_ENTRY_INIT(0x0092, 0, 0xffff),
1486842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1491e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS1]		= GDT_ENTRY_INIT(0x0092, 0, 0),
1506842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1511e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS2]		= GDT_ENTRY_INIT(0x0092, 0, 0),
152f7627e25SThomas Gleixner 	/*
153f7627e25SThomas Gleixner 	 * The APM segments have byte granularity and their bases
154f7627e25SThomas Gleixner 	 * are set at run time.  All have 64k limits.
155f7627e25SThomas Gleixner 	 */
1566842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1571e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
158f7627e25SThomas Gleixner 	/* 16-bit code */
1591e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE+1]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1606842ef0eSGlauber de Oliveira Costa 	/* data */
16172c4d853SIngo Molnar 	[GDT_ENTRY_APMBIOS_BASE+2]	= GDT_ENTRY_INIT(0x4092, 0, 0xffff),
162f7627e25SThomas Gleixner 
1631e5de182SAkinobu Mita 	[GDT_ENTRY_ESPFIX_SS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1641e5de182SAkinobu Mita 	[GDT_ENTRY_PERCPU]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
165950ad7ffSYinghai Lu #endif
16606deef89SBrian Gerst } };
167f7627e25SThomas Gleixner EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
168f7627e25SThomas Gleixner 
1690790c9aaSAndy Lutomirski #ifdef CONFIG_X86_64
170c7ad5ad2SAndy Lutomirski static int __init x86_nopcid_setup(char *s)
1710790c9aaSAndy Lutomirski {
172c7ad5ad2SAndy Lutomirski 	/* nopcid doesn't accept parameters */
173c7ad5ad2SAndy Lutomirski 	if (s)
174c7ad5ad2SAndy Lutomirski 		return -EINVAL;
1750790c9aaSAndy Lutomirski 
1760790c9aaSAndy Lutomirski 	/* do not emit a message if the feature is not present */
1770790c9aaSAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_PCID))
178c7ad5ad2SAndy Lutomirski 		return 0;
1790790c9aaSAndy Lutomirski 
1800790c9aaSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_PCID);
1810790c9aaSAndy Lutomirski 	pr_info("nopcid: PCID feature disabled\n");
182c7ad5ad2SAndy Lutomirski 	return 0;
1830790c9aaSAndy Lutomirski }
184c7ad5ad2SAndy Lutomirski early_param("nopcid", x86_nopcid_setup);
1850790c9aaSAndy Lutomirski #endif
1860790c9aaSAndy Lutomirski 
187d12a72b8SAndy Lutomirski static int __init x86_noinvpcid_setup(char *s)
188d12a72b8SAndy Lutomirski {
189d12a72b8SAndy Lutomirski 	/* noinvpcid doesn't accept parameters */
190d12a72b8SAndy Lutomirski 	if (s)
191d12a72b8SAndy Lutomirski 		return -EINVAL;
192d12a72b8SAndy Lutomirski 
193d12a72b8SAndy Lutomirski 	/* do not emit a message if the feature is not present */
194d12a72b8SAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_INVPCID))
195d12a72b8SAndy Lutomirski 		return 0;
196d12a72b8SAndy Lutomirski 
197d12a72b8SAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_INVPCID);
198d12a72b8SAndy Lutomirski 	pr_info("noinvpcid: INVPCID feature disabled\n");
199d12a72b8SAndy Lutomirski 	return 0;
200d12a72b8SAndy Lutomirski }
201d12a72b8SAndy Lutomirski early_param("noinvpcid", x86_noinvpcid_setup);
202d12a72b8SAndy Lutomirski 
203ba51dcedSYinghai Lu #ifdef CONFIG_X86_32
204148f9bb8SPaul Gortmaker static int cachesize_override = -1;
205148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1;
206f7627e25SThomas Gleixner 
207f7627e25SThomas Gleixner static int __init cachesize_setup(char *str)
208f7627e25SThomas Gleixner {
209f7627e25SThomas Gleixner 	get_option(&str, &cachesize_override);
210f7627e25SThomas Gleixner 	return 1;
211f7627e25SThomas Gleixner }
212f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup);
213f7627e25SThomas Gleixner 
214f7627e25SThomas Gleixner static int __init x86_sep_setup(char *s)
215f7627e25SThomas Gleixner {
21613530257SAndi Kleen 	setup_clear_cpu_cap(X86_FEATURE_SEP);
217f7627e25SThomas Gleixner 	return 1;
218f7627e25SThomas Gleixner }
219f7627e25SThomas Gleixner __setup("nosep", x86_sep_setup);
220f7627e25SThomas Gleixner 
221f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */
222f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag)
223f7627e25SThomas Gleixner {
224f7627e25SThomas Gleixner 	u32 f1, f2;
225f7627e25SThomas Gleixner 
22694f6bac1SKrzysztof Helt 	/*
22794f6bac1SKrzysztof Helt 	 * Cyrix and IDT cpus allow disabling of CPUID
22894f6bac1SKrzysztof Helt 	 * so the code below may return different results
22994f6bac1SKrzysztof Helt 	 * when it is executed before and after enabling
23094f6bac1SKrzysztof Helt 	 * the CPUID. Add "volatile" to not allow gcc to
23194f6bac1SKrzysztof Helt 	 * optimize the subsequent calls to this function.
23294f6bac1SKrzysztof Helt 	 */
23394f6bac1SKrzysztof Helt 	asm volatile ("pushfl		\n\t"
234f7627e25SThomas Gleixner 		      "pushfl		\n\t"
235f7627e25SThomas Gleixner 		      "popl %0		\n\t"
236f7627e25SThomas Gleixner 		      "movl %0, %1	\n\t"
237f7627e25SThomas Gleixner 		      "xorl %2, %0	\n\t"
238f7627e25SThomas Gleixner 		      "pushl %0		\n\t"
239f7627e25SThomas Gleixner 		      "popfl		\n\t"
240f7627e25SThomas Gleixner 		      "pushfl		\n\t"
241f7627e25SThomas Gleixner 		      "popl %0		\n\t"
242f7627e25SThomas Gleixner 		      "popfl		\n\t"
2430f3fa48aSIngo Molnar 
244f7627e25SThomas Gleixner 		      : "=&r" (f1), "=&r" (f2)
245f7627e25SThomas Gleixner 		      : "ir" (flag));
246f7627e25SThomas Gleixner 
247f7627e25SThomas Gleixner 	return ((f1^f2) & flag) != 0;
248f7627e25SThomas Gleixner }
249f7627e25SThomas Gleixner 
250f7627e25SThomas Gleixner /* Probe for the CPUID instruction */
251148f9bb8SPaul Gortmaker int have_cpuid_p(void)
252f7627e25SThomas Gleixner {
253f7627e25SThomas Gleixner 	return flag_is_changeable_p(X86_EFLAGS_ID);
254f7627e25SThomas Gleixner }
255f7627e25SThomas Gleixner 
256148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
2570a488a53SYinghai Lu {
2580a488a53SYinghai Lu 	unsigned long lo, hi;
2590f3fa48aSIngo Molnar 
2600f3fa48aSIngo Molnar 	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
2610f3fa48aSIngo Molnar 		return;
2620f3fa48aSIngo Molnar 
2630f3fa48aSIngo Molnar 	/* Disable processor serial number: */
2640f3fa48aSIngo Molnar 
2650a488a53SYinghai Lu 	rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2660a488a53SYinghai Lu 	lo |= 0x200000;
2670a488a53SYinghai Lu 	wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2680f3fa48aSIngo Molnar 
2691b74dde7SChen Yucong 	pr_notice("CPU serial number disabled.\n");
2700a488a53SYinghai Lu 	clear_cpu_cap(c, X86_FEATURE_PN);
2710a488a53SYinghai Lu 
2720a488a53SYinghai Lu 	/* Disabling the serial number may affect the cpuid level */
2730a488a53SYinghai Lu 	c->cpuid_level = cpuid_eax(0);
2740a488a53SYinghai Lu }
2750a488a53SYinghai Lu 
2760a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s)
2770a488a53SYinghai Lu {
2780a488a53SYinghai Lu 	disable_x86_serial_nr = 0;
2790a488a53SYinghai Lu 	return 1;
2800a488a53SYinghai Lu }
2810a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup);
282ba51dcedSYinghai Lu #else
283102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag)
284102bbe3aSYinghai Lu {
285102bbe3aSYinghai Lu 	return 1;
286102bbe3aSYinghai Lu }
287102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
288102bbe3aSYinghai Lu {
289102bbe3aSYinghai Lu }
290ba51dcedSYinghai Lu #endif
2910a488a53SYinghai Lu 
292de5397adSFenghua Yu static __init int setup_disable_smep(char *arg)
293de5397adSFenghua Yu {
294b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMEP);
295de5397adSFenghua Yu 	return 1;
296de5397adSFenghua Yu }
297de5397adSFenghua Yu __setup("nosmep", setup_disable_smep);
298de5397adSFenghua Yu 
299b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c)
300de5397adSFenghua Yu {
301b2cc2a07SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMEP))
302375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMEP);
303de5397adSFenghua Yu }
304de5397adSFenghua Yu 
30552b6179aSH. Peter Anvin static __init int setup_disable_smap(char *arg)
30652b6179aSH. Peter Anvin {
307b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMAP);
30852b6179aSH. Peter Anvin 	return 1;
30952b6179aSH. Peter Anvin }
31052b6179aSH. Peter Anvin __setup("nosmap", setup_disable_smap);
31152b6179aSH. Peter Anvin 
312b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c)
31352b6179aSH. Peter Anvin {
314581b7f15SAndrew Cooper 	unsigned long eflags = native_save_fl();
315b2cc2a07SH. Peter Anvin 
316b2cc2a07SH. Peter Anvin 	/* This should have been cleared long ago */
317b2cc2a07SH. Peter Anvin 	BUG_ON(eflags & X86_EFLAGS_AC);
318b2cc2a07SH. Peter Anvin 
31903bbd596SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMAP)) {
32003bbd596SH. Peter Anvin #ifdef CONFIG_X86_SMAP
321375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMAP);
32203bbd596SH. Peter Anvin #else
323375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_SMAP);
32403bbd596SH. Peter Anvin #endif
32503bbd596SH. Peter Anvin 	}
326f7627e25SThomas Gleixner }
327f7627e25SThomas Gleixner 
328aa35f896SRicardo Neri static __always_inline void setup_umip(struct cpuinfo_x86 *c)
329aa35f896SRicardo Neri {
330aa35f896SRicardo Neri 	/* Check the boot processor, plus build option for UMIP. */
331aa35f896SRicardo Neri 	if (!cpu_feature_enabled(X86_FEATURE_UMIP))
332aa35f896SRicardo Neri 		goto out;
333aa35f896SRicardo Neri 
334aa35f896SRicardo Neri 	/* Check the current processor's cpuid bits. */
335aa35f896SRicardo Neri 	if (!cpu_has(c, X86_FEATURE_UMIP))
336aa35f896SRicardo Neri 		goto out;
337aa35f896SRicardo Neri 
338aa35f896SRicardo Neri 	cr4_set_bits(X86_CR4_UMIP);
339aa35f896SRicardo Neri 
340438cbf88SLendacky, Thomas 	pr_info_once("x86/cpu: User Mode Instruction Prevention (UMIP) activated\n");
341770c7755SRicardo Neri 
342aa35f896SRicardo Neri 	return;
343aa35f896SRicardo Neri 
344aa35f896SRicardo Neri out:
345aa35f896SRicardo Neri 	/*
346aa35f896SRicardo Neri 	 * Make sure UMIP is disabled in case it was enabled in a
347aa35f896SRicardo Neri 	 * previous boot (e.g., via kexec).
348aa35f896SRicardo Neri 	 */
349aa35f896SRicardo Neri 	cr4_clear_bits(X86_CR4_UMIP);
350aa35f896SRicardo Neri }
351aa35f896SRicardo Neri 
352a13b9d0bSKees Cook /* These bits should not change their value after CPU init is finished. */
353a13b9d0bSKees Cook static const unsigned long cr4_pinned_mask =
354a13b9d0bSKees Cook 	X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP | X86_CR4_FSGSBASE;
3557652ac92SThomas Gleixner static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
3567652ac92SThomas Gleixner static unsigned long cr4_pinned_bits __ro_after_init;
3577652ac92SThomas Gleixner 
3587652ac92SThomas Gleixner void native_write_cr0(unsigned long val)
3597652ac92SThomas Gleixner {
3607652ac92SThomas Gleixner 	unsigned long bits_missing = 0;
3617652ac92SThomas Gleixner 
3627652ac92SThomas Gleixner set_register:
363aa5cacdcSArvind Sankar 	asm volatile("mov %0,%%cr0": "+r" (val) : : "memory");
3647652ac92SThomas Gleixner 
3657652ac92SThomas Gleixner 	if (static_branch_likely(&cr_pinning)) {
3667652ac92SThomas Gleixner 		if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) {
3677652ac92SThomas Gleixner 			bits_missing = X86_CR0_WP;
3687652ac92SThomas Gleixner 			val |= bits_missing;
3697652ac92SThomas Gleixner 			goto set_register;
3707652ac92SThomas Gleixner 		}
3717652ac92SThomas Gleixner 		/* Warn after we've set the missing bits. */
3727652ac92SThomas Gleixner 		WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n");
3737652ac92SThomas Gleixner 	}
3747652ac92SThomas Gleixner }
3757652ac92SThomas Gleixner EXPORT_SYMBOL(native_write_cr0);
3767652ac92SThomas Gleixner 
3777652ac92SThomas Gleixner void native_write_cr4(unsigned long val)
3787652ac92SThomas Gleixner {
379a13b9d0bSKees Cook 	unsigned long bits_changed = 0;
3807652ac92SThomas Gleixner 
3817652ac92SThomas Gleixner set_register:
382aa5cacdcSArvind Sankar 	asm volatile("mov %0,%%cr4": "+r" (val) : : "memory");
3837652ac92SThomas Gleixner 
3847652ac92SThomas Gleixner 	if (static_branch_likely(&cr_pinning)) {
385a13b9d0bSKees Cook 		if (unlikely((val & cr4_pinned_mask) != cr4_pinned_bits)) {
386a13b9d0bSKees Cook 			bits_changed = (val & cr4_pinned_mask) ^ cr4_pinned_bits;
387a13b9d0bSKees Cook 			val = (val & ~cr4_pinned_mask) | cr4_pinned_bits;
3887652ac92SThomas Gleixner 			goto set_register;
3897652ac92SThomas Gleixner 		}
390a13b9d0bSKees Cook 		/* Warn after we've corrected the changed bits. */
391a13b9d0bSKees Cook 		WARN_ONCE(bits_changed, "pinned CR4 bits changed: 0x%lx!?\n",
392a13b9d0bSKees Cook 			  bits_changed);
3937652ac92SThomas Gleixner 	}
3947652ac92SThomas Gleixner }
39521953ee5SThomas Gleixner #if IS_MODULE(CONFIG_LKDTM)
396d8f0b353SThomas Gleixner EXPORT_SYMBOL_GPL(native_write_cr4);
39721953ee5SThomas Gleixner #endif
398d8f0b353SThomas Gleixner 
399d8f0b353SThomas Gleixner void cr4_update_irqsoff(unsigned long set, unsigned long clear)
400d8f0b353SThomas Gleixner {
401d8f0b353SThomas Gleixner 	unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4);
402d8f0b353SThomas Gleixner 
403d8f0b353SThomas Gleixner 	lockdep_assert_irqs_disabled();
404d8f0b353SThomas Gleixner 
405d8f0b353SThomas Gleixner 	newval = (cr4 & ~clear) | set;
406d8f0b353SThomas Gleixner 	if (newval != cr4) {
407d8f0b353SThomas Gleixner 		this_cpu_write(cpu_tlbstate.cr4, newval);
408d8f0b353SThomas Gleixner 		__write_cr4(newval);
409d8f0b353SThomas Gleixner 	}
410d8f0b353SThomas Gleixner }
411d8f0b353SThomas Gleixner EXPORT_SYMBOL(cr4_update_irqsoff);
412d8f0b353SThomas Gleixner 
413d8f0b353SThomas Gleixner /* Read the CR4 shadow. */
414d8f0b353SThomas Gleixner unsigned long cr4_read_shadow(void)
415d8f0b353SThomas Gleixner {
416d8f0b353SThomas Gleixner 	return this_cpu_read(cpu_tlbstate.cr4);
417d8f0b353SThomas Gleixner }
418d8f0b353SThomas Gleixner EXPORT_SYMBOL_GPL(cr4_read_shadow);
4197652ac92SThomas Gleixner 
4207652ac92SThomas Gleixner void cr4_init(void)
4217652ac92SThomas Gleixner {
4227652ac92SThomas Gleixner 	unsigned long cr4 = __read_cr4();
4237652ac92SThomas Gleixner 
4247652ac92SThomas Gleixner 	if (boot_cpu_has(X86_FEATURE_PCID))
4257652ac92SThomas Gleixner 		cr4 |= X86_CR4_PCIDE;
4267652ac92SThomas Gleixner 	if (static_branch_likely(&cr_pinning))
427a13b9d0bSKees Cook 		cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits;
4287652ac92SThomas Gleixner 
4297652ac92SThomas Gleixner 	__write_cr4(cr4);
4307652ac92SThomas Gleixner 
4317652ac92SThomas Gleixner 	/* Initialize cr4 shadow for this CPU. */
4327652ac92SThomas Gleixner 	this_cpu_write(cpu_tlbstate.cr4, cr4);
4337652ac92SThomas Gleixner }
434873d50d5SKees Cook 
435873d50d5SKees Cook /*
436873d50d5SKees Cook  * Once CPU feature detection is finished (and boot params have been
437873d50d5SKees Cook  * parsed), record any of the sensitive CR bits that are set, and
438873d50d5SKees Cook  * enable CR pinning.
439873d50d5SKees Cook  */
440873d50d5SKees Cook static void __init setup_cr_pinning(void)
441873d50d5SKees Cook {
442a13b9d0bSKees Cook 	cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask;
443873d50d5SKees Cook 	static_key_enable(&cr_pinning.key);
444873d50d5SKees Cook }
445873d50d5SKees Cook 
446b745cfbaSAndy Lutomirski static __init int x86_nofsgsbase_setup(char *arg)
447dd649bd0SAndy Lutomirski {
448b745cfbaSAndy Lutomirski 	/* Require an exact match without trailing characters. */
449b745cfbaSAndy Lutomirski 	if (strlen(arg))
450b745cfbaSAndy Lutomirski 		return 0;
451b745cfbaSAndy Lutomirski 
452b745cfbaSAndy Lutomirski 	/* Do not emit a message if the feature is not present. */
453b745cfbaSAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_FSGSBASE))
454b745cfbaSAndy Lutomirski 		return 1;
455b745cfbaSAndy Lutomirski 
456b745cfbaSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_FSGSBASE);
457b745cfbaSAndy Lutomirski 	pr_info("FSGSBASE disabled via kernel command line\n");
458dd649bd0SAndy Lutomirski 	return 1;
459dd649bd0SAndy Lutomirski }
460b745cfbaSAndy Lutomirski __setup("nofsgsbase", x86_nofsgsbase_setup);
461dd649bd0SAndy Lutomirski 
462b64ed19bSAndy Lutomirski /*
46306976945SDave Hansen  * Protection Keys are not available in 32-bit mode.
46406976945SDave Hansen  */
46506976945SDave Hansen static bool pku_disabled;
46606976945SDave Hansen 
46706976945SDave Hansen static __always_inline void setup_pku(struct cpuinfo_x86 *c)
46806976945SDave Hansen {
469*8a1dc55aSThomas Gleixner 	if (c == &boot_cpu_data) {
470*8a1dc55aSThomas Gleixner 		if (pku_disabled || !cpu_feature_enabled(X86_FEATURE_PKU))
471e8df1a95SDave Hansen 			return;
472*8a1dc55aSThomas Gleixner 		/*
473*8a1dc55aSThomas Gleixner 		 * Setting CR4.PKE will cause the X86_FEATURE_OSPKE cpuid
474*8a1dc55aSThomas Gleixner 		 * bit to be set.  Enforce it.
475*8a1dc55aSThomas Gleixner 		 */
476*8a1dc55aSThomas Gleixner 		setup_force_cpu_cap(X86_FEATURE_OSPKE);
477*8a1dc55aSThomas Gleixner 
478*8a1dc55aSThomas Gleixner 	} else if (!cpu_feature_enabled(X86_FEATURE_OSPKE)) {
47906976945SDave Hansen 		return;
480*8a1dc55aSThomas Gleixner 	}
48106976945SDave Hansen 
48206976945SDave Hansen 	cr4_set_bits(X86_CR4_PKE);
48306976945SDave Hansen }
48406976945SDave Hansen 
48506976945SDave Hansen #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
48606976945SDave Hansen static __init int setup_disable_pku(char *arg)
48706976945SDave Hansen {
48806976945SDave Hansen 	/*
48906976945SDave Hansen 	 * Do not clear the X86_FEATURE_PKU bit.  All of the
49006976945SDave Hansen 	 * runtime checks are against OSPKE so clearing the
49106976945SDave Hansen 	 * bit does nothing.
49206976945SDave Hansen 	 *
49306976945SDave Hansen 	 * This way, we will see "pku" in cpuinfo, but not
49406976945SDave Hansen 	 * "ospke", which is exactly what we want.  It shows
49506976945SDave Hansen 	 * that the CPU has PKU, but the OS has not enabled it.
49606976945SDave Hansen 	 * This happens to be exactly how a system would look
49706976945SDave Hansen 	 * if we disabled the config option.
49806976945SDave Hansen 	 */
49906976945SDave Hansen 	pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
50006976945SDave Hansen 	pku_disabled = true;
50106976945SDave Hansen 	return 1;
50206976945SDave Hansen }
50306976945SDave Hansen __setup("nopku", setup_disable_pku);
50406976945SDave Hansen #endif /* CONFIG_X86_64 */
50506976945SDave Hansen 
50606976945SDave Hansen /*
507b38b0665SH. Peter Anvin  * Some CPU features depend on higher CPUID levels, which may not always
508b38b0665SH. Peter Anvin  * be available due to CPUID level capping or broken virtualization
509b38b0665SH. Peter Anvin  * software.  Add those features to this table to auto-disable them.
510b38b0665SH. Peter Anvin  */
511b38b0665SH. Peter Anvin struct cpuid_dependent_feature {
512b38b0665SH. Peter Anvin 	u32 feature;
513b38b0665SH. Peter Anvin 	u32 level;
514b38b0665SH. Peter Anvin };
5150f3fa48aSIngo Molnar 
516148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature
517b38b0665SH. Peter Anvin cpuid_dependent_features[] = {
518b38b0665SH. Peter Anvin 	{ X86_FEATURE_MWAIT,		0x00000005 },
519b38b0665SH. Peter Anvin 	{ X86_FEATURE_DCA,		0x00000009 },
520b38b0665SH. Peter Anvin 	{ X86_FEATURE_XSAVE,		0x0000000d },
521b38b0665SH. Peter Anvin 	{ 0, 0 }
522b38b0665SH. Peter Anvin };
523b38b0665SH. Peter Anvin 
524148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
525b38b0665SH. Peter Anvin {
526b38b0665SH. Peter Anvin 	const struct cpuid_dependent_feature *df;
5279766cdbcSJaswinder Singh Rajput 
528b38b0665SH. Peter Anvin 	for (df = cpuid_dependent_features; df->feature; df++) {
5290f3fa48aSIngo Molnar 
5300f3fa48aSIngo Molnar 		if (!cpu_has(c, df->feature))
5310f3fa48aSIngo Molnar 			continue;
532b38b0665SH. Peter Anvin 		/*
533b38b0665SH. Peter Anvin 		 * Note: cpuid_level is set to -1 if unavailable, but
534b38b0665SH. Peter Anvin 		 * extended_extended_level is set to 0 if unavailable
535b38b0665SH. Peter Anvin 		 * and the legitimate extended levels are all negative
536b38b0665SH. Peter Anvin 		 * when signed; hence the weird messing around with
537b38b0665SH. Peter Anvin 		 * signs here...
538b38b0665SH. Peter Anvin 		 */
5390f3fa48aSIngo Molnar 		if (!((s32)df->level < 0 ?
540f6db44dfSYinghai Lu 		     (u32)df->level > (u32)c->extended_cpuid_level :
5410f3fa48aSIngo Molnar 		     (s32)df->level > (s32)c->cpuid_level))
5420f3fa48aSIngo Molnar 			continue;
5430f3fa48aSIngo Molnar 
544b38b0665SH. Peter Anvin 		clear_cpu_cap(c, df->feature);
5450f3fa48aSIngo Molnar 		if (!warn)
5460f3fa48aSIngo Molnar 			continue;
5470f3fa48aSIngo Molnar 
5481b74dde7SChen Yucong 		pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
5499def39beSJosh Triplett 			x86_cap_flag(df->feature), df->level);
550b38b0665SH. Peter Anvin 	}
551b38b0665SH. Peter Anvin }
552b38b0665SH. Peter Anvin 
553b38b0665SH. Peter Anvin /*
554f7627e25SThomas Gleixner  * Naming convention should be: <Name> [(<Codename>)]
555f7627e25SThomas Gleixner  * This table only is used unless init_<vendor>() below doesn't set it;
5560f3fa48aSIngo Molnar  * in particular, if CPUID levels 0x80000002..4 are supported, this
5570f3fa48aSIngo Molnar  * isn't used
558f7627e25SThomas Gleixner  */
559f7627e25SThomas Gleixner 
560f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */
561148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c)
562f7627e25SThomas Gleixner {
56309dc68d9SJan Beulich #ifdef CONFIG_X86_32
56409dc68d9SJan Beulich 	const struct legacy_cpu_model_info *info;
565f7627e25SThomas Gleixner 
566f7627e25SThomas Gleixner 	if (c->x86_model >= 16)
567f7627e25SThomas Gleixner 		return NULL;	/* Range check */
568f7627e25SThomas Gleixner 
569f7627e25SThomas Gleixner 	if (!this_cpu)
570f7627e25SThomas Gleixner 		return NULL;
571f7627e25SThomas Gleixner 
57209dc68d9SJan Beulich 	info = this_cpu->legacy_models;
573f7627e25SThomas Gleixner 
57409dc68d9SJan Beulich 	while (info->family) {
575f7627e25SThomas Gleixner 		if (info->family == c->x86)
576f7627e25SThomas Gleixner 			return info->model_names[c->x86_model];
577f7627e25SThomas Gleixner 		info++;
578f7627e25SThomas Gleixner 	}
57909dc68d9SJan Beulich #endif
580f7627e25SThomas Gleixner 	return NULL;		/* Not found */
581f7627e25SThomas Gleixner }
582f7627e25SThomas Gleixner 
583f6a892ddSFenghua Yu /* Aligned to unsigned long to avoid split lock in atomic bitmap ops */
584f6a892ddSFenghua Yu __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
585f6a892ddSFenghua Yu __u32 cpu_caps_set[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
586f7627e25SThomas Gleixner 
58711e3a840SJeremy Fitzhardinge void load_percpu_segment(int cpu)
5889d31d35bSYinghai Lu {
589fab334c1SYinghai Lu #ifdef CONFIG_X86_32
5902697fbd5SBrian Gerst 	loadsegment(fs, __KERNEL_PERCPU);
5912697fbd5SBrian Gerst #else
59245e876f7SAndy Lutomirski 	__loadsegment_simple(gs, 0);
59335060ed6SVitaly Kuznetsov 	wrmsrl(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu));
594fab334c1SYinghai Lu #endif
5959d31d35bSYinghai Lu }
5969d31d35bSYinghai Lu 
59772f5e08dSAndy Lutomirski #ifdef CONFIG_X86_32
59872f5e08dSAndy Lutomirski /* The 32-bit entry code needs to find cpu_entry_area. */
59972f5e08dSAndy Lutomirski DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
60072f5e08dSAndy Lutomirski #endif
60172f5e08dSAndy Lutomirski 
60245fc8757SThomas Garnier /* Load the original GDT from the per-cpu structure */
60345fc8757SThomas Garnier void load_direct_gdt(int cpu)
60445fc8757SThomas Garnier {
60545fc8757SThomas Garnier 	struct desc_ptr gdt_descr;
60645fc8757SThomas Garnier 
60745fc8757SThomas Garnier 	gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
60845fc8757SThomas Garnier 	gdt_descr.size = GDT_SIZE - 1;
60945fc8757SThomas Garnier 	load_gdt(&gdt_descr);
61045fc8757SThomas Garnier }
61145fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_direct_gdt);
61245fc8757SThomas Garnier 
61369218e47SThomas Garnier /* Load a fixmap remapping of the per-cpu GDT */
61469218e47SThomas Garnier void load_fixmap_gdt(int cpu)
61569218e47SThomas Garnier {
61669218e47SThomas Garnier 	struct desc_ptr gdt_descr;
61769218e47SThomas Garnier 
61869218e47SThomas Garnier 	gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
61969218e47SThomas Garnier 	gdt_descr.size = GDT_SIZE - 1;
62069218e47SThomas Garnier 	load_gdt(&gdt_descr);
62169218e47SThomas Garnier }
62245fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_fixmap_gdt);
62369218e47SThomas Garnier 
6240f3fa48aSIngo Molnar /*
6250f3fa48aSIngo Molnar  * Current gdt points %fs at the "master" per-cpu area: after this,
6260f3fa48aSIngo Molnar  * it's on the real one.
6270f3fa48aSIngo Molnar  */
628552be871SBrian Gerst void switch_to_new_gdt(int cpu)
629f7627e25SThomas Gleixner {
63045fc8757SThomas Garnier 	/* Load the original GDT */
63145fc8757SThomas Garnier 	load_direct_gdt(cpu);
632f7627e25SThomas Gleixner 	/* Reload the per-cpu base */
63311e3a840SJeremy Fitzhardinge 	load_percpu_segment(cpu);
634f7627e25SThomas Gleixner }
635f7627e25SThomas Gleixner 
636148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
637f7627e25SThomas Gleixner 
638148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c)
639f7627e25SThomas Gleixner {
640f7627e25SThomas Gleixner 	unsigned int *v;
641ee098e1aSBorislav Petkov 	char *p, *q, *s;
642f7627e25SThomas Gleixner 
6433da99c97SYinghai Lu 	if (c->extended_cpuid_level < 0x80000004)
6441b05d60dSYinghai Lu 		return;
645f7627e25SThomas Gleixner 
646f7627e25SThomas Gleixner 	v = (unsigned int *)c->x86_model_id;
647f7627e25SThomas Gleixner 	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
648f7627e25SThomas Gleixner 	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
649f7627e25SThomas Gleixner 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
650f7627e25SThomas Gleixner 	c->x86_model_id[48] = 0;
651f7627e25SThomas Gleixner 
652ee098e1aSBorislav Petkov 	/* Trim whitespace */
653ee098e1aSBorislav Petkov 	p = q = s = &c->x86_model_id[0];
654ee098e1aSBorislav Petkov 
655ee098e1aSBorislav Petkov 	while (*p == ' ')
656ee098e1aSBorislav Petkov 		p++;
657ee098e1aSBorislav Petkov 
658ee098e1aSBorislav Petkov 	while (*p) {
659ee098e1aSBorislav Petkov 		/* Note the last non-whitespace index */
660ee098e1aSBorislav Petkov 		if (!isspace(*p))
661ee098e1aSBorislav Petkov 			s = q;
662ee098e1aSBorislav Petkov 
663ee098e1aSBorislav Petkov 		*q++ = *p++;
664ee098e1aSBorislav Petkov 	}
665ee098e1aSBorislav Petkov 
666ee098e1aSBorislav Petkov 	*(s + 1) = '\0';
667f7627e25SThomas Gleixner }
668f7627e25SThomas Gleixner 
6699305bd6cSThomas Gleixner void detect_num_cpu_cores(struct cpuinfo_x86 *c)
6702cc61be6SDavid Wang {
6712cc61be6SDavid Wang 	unsigned int eax, ebx, ecx, edx;
6722cc61be6SDavid Wang 
6739305bd6cSThomas Gleixner 	c->x86_max_cores = 1;
6742cc61be6SDavid Wang 	if (!IS_ENABLED(CONFIG_SMP) || c->cpuid_level < 4)
6759305bd6cSThomas Gleixner 		return;
6762cc61be6SDavid Wang 
6772cc61be6SDavid Wang 	cpuid_count(4, 0, &eax, &ebx, &ecx, &edx);
6782cc61be6SDavid Wang 	if (eax & 0x1f)
6799305bd6cSThomas Gleixner 		c->x86_max_cores = (eax >> 26) + 1;
6802cc61be6SDavid Wang }
6812cc61be6SDavid Wang 
682148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
683f7627e25SThomas Gleixner {
6849d31d35bSYinghai Lu 	unsigned int n, dummy, ebx, ecx, edx, l2size;
685f7627e25SThomas Gleixner 
6863da99c97SYinghai Lu 	n = c->extended_cpuid_level;
687f7627e25SThomas Gleixner 
688f7627e25SThomas Gleixner 	if (n >= 0x80000005) {
6899d31d35bSYinghai Lu 		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
690f7627e25SThomas Gleixner 		c->x86_cache_size = (ecx>>24) + (edx>>24);
691140fc727SYinghai Lu #ifdef CONFIG_X86_64
692140fc727SYinghai Lu 		/* On K8 L1 TLB is inclusive, so don't count it */
693140fc727SYinghai Lu 		c->x86_tlbsize = 0;
694140fc727SYinghai Lu #endif
695f7627e25SThomas Gleixner 	}
696f7627e25SThomas Gleixner 
697f7627e25SThomas Gleixner 	if (n < 0x80000006)	/* Some chips just has a large L1. */
698f7627e25SThomas Gleixner 		return;
699f7627e25SThomas Gleixner 
7000a488a53SYinghai Lu 	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
701f7627e25SThomas Gleixner 	l2size = ecx >> 16;
702f7627e25SThomas Gleixner 
703140fc727SYinghai Lu #ifdef CONFIG_X86_64
704140fc727SYinghai Lu 	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
705140fc727SYinghai Lu #else
706f7627e25SThomas Gleixner 	/* do processor-specific cache resizing */
70709dc68d9SJan Beulich 	if (this_cpu->legacy_cache_size)
70809dc68d9SJan Beulich 		l2size = this_cpu->legacy_cache_size(c, l2size);
709f7627e25SThomas Gleixner 
710f7627e25SThomas Gleixner 	/* Allow user to override all this if necessary. */
711f7627e25SThomas Gleixner 	if (cachesize_override != -1)
712f7627e25SThomas Gleixner 		l2size = cachesize_override;
713f7627e25SThomas Gleixner 
714f7627e25SThomas Gleixner 	if (l2size == 0)
715f7627e25SThomas Gleixner 		return;		/* Again, no L2 cache is possible */
716140fc727SYinghai Lu #endif
717f7627e25SThomas Gleixner 
718f7627e25SThomas Gleixner 	c->x86_cache_size = l2size;
719f7627e25SThomas Gleixner }
720f7627e25SThomas Gleixner 
721e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO];
722e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO];
723e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO];
724e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO];
725e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO];
726e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO];
727dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO];
728e0ba94f1SAlex Shi 
729f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c)
730e0ba94f1SAlex Shi {
731e0ba94f1SAlex Shi 	if (this_cpu->c_detect_tlb)
732e0ba94f1SAlex Shi 		this_cpu->c_detect_tlb(c);
733e0ba94f1SAlex Shi 
734f94fe119SSteven Honeyman 	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
735e0ba94f1SAlex Shi 		tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
736f94fe119SSteven Honeyman 		tlb_lli_4m[ENTRIES]);
737f94fe119SSteven Honeyman 
738f94fe119SSteven Honeyman 	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
739f94fe119SSteven Honeyman 		tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
740f94fe119SSteven Honeyman 		tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
741e0ba94f1SAlex Shi }
742e0ba94f1SAlex Shi 
743545401f4SThomas Gleixner int detect_ht_early(struct cpuinfo_x86 *c)
7449d31d35bSYinghai Lu {
745c8e56d20SBorislav Petkov #ifdef CONFIG_SMP
7469d31d35bSYinghai Lu 	u32 eax, ebx, ecx, edx;
7479d31d35bSYinghai Lu 
7480a488a53SYinghai Lu 	if (!cpu_has(c, X86_FEATURE_HT))
749545401f4SThomas Gleixner 		return -1;
7509d31d35bSYinghai Lu 
7510a488a53SYinghai Lu 	if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
752545401f4SThomas Gleixner 		return -1;
7530a488a53SYinghai Lu 
7541cd78776SYinghai Lu 	if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
755545401f4SThomas Gleixner 		return -1;
7561cd78776SYinghai Lu 
7570a488a53SYinghai Lu 	cpuid(1, &eax, &ebx, &ecx, &edx);
7580a488a53SYinghai Lu 
7599d31d35bSYinghai Lu 	smp_num_siblings = (ebx & 0xff0000) >> 16;
760545401f4SThomas Gleixner 	if (smp_num_siblings == 1)
7611b74dde7SChen Yucong 		pr_info_once("CPU0: Hyper-Threading is disabled\n");
762545401f4SThomas Gleixner #endif
763545401f4SThomas Gleixner 	return 0;
7640f3fa48aSIngo Molnar }
7650f3fa48aSIngo Molnar 
766545401f4SThomas Gleixner void detect_ht(struct cpuinfo_x86 *c)
767545401f4SThomas Gleixner {
768545401f4SThomas Gleixner #ifdef CONFIG_SMP
769545401f4SThomas Gleixner 	int index_msb, core_bits;
770545401f4SThomas Gleixner 
771545401f4SThomas Gleixner 	if (detect_ht_early(c) < 0)
772545401f4SThomas Gleixner 		return;
7739d31d35bSYinghai Lu 
7749d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
775cb8cc442SIngo Molnar 	c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
7769d31d35bSYinghai Lu 
7779d31d35bSYinghai Lu 	smp_num_siblings = smp_num_siblings / c->x86_max_cores;
7789d31d35bSYinghai Lu 
7799d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
7809d31d35bSYinghai Lu 
7819d31d35bSYinghai Lu 	core_bits = get_count_order(c->x86_max_cores);
7829d31d35bSYinghai Lu 
783cb8cc442SIngo Molnar 	c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
7841cd78776SYinghai Lu 				       ((1 << core_bits) - 1);
7859d31d35bSYinghai Lu #endif
78697e4db7cSYinghai Lu }
787f7627e25SThomas Gleixner 
788148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c)
789f7627e25SThomas Gleixner {
790f7627e25SThomas Gleixner 	char *v = c->x86_vendor_id;
7910f3fa48aSIngo Molnar 	int i;
792f7627e25SThomas Gleixner 
793f7627e25SThomas Gleixner 	for (i = 0; i < X86_VENDOR_NUM; i++) {
79410a434fcSYinghai Lu 		if (!cpu_devs[i])
79510a434fcSYinghai Lu 			break;
79610a434fcSYinghai Lu 
797f7627e25SThomas Gleixner 		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
798f7627e25SThomas Gleixner 		    (cpu_devs[i]->c_ident[1] &&
799f7627e25SThomas Gleixner 		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
8000f3fa48aSIngo Molnar 
801f7627e25SThomas Gleixner 			this_cpu = cpu_devs[i];
80210a434fcSYinghai Lu 			c->x86_vendor = this_cpu->c_x86_vendor;
803f7627e25SThomas Gleixner 			return;
804f7627e25SThomas Gleixner 		}
805f7627e25SThomas Gleixner 	}
80610a434fcSYinghai Lu 
8071b74dde7SChen Yucong 	pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
808a9c56953SMinchan Kim 		    "CPU: Your system may be unstable.\n", v);
80910a434fcSYinghai Lu 
810f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
811f7627e25SThomas Gleixner 	this_cpu = &default_cpu;
812f7627e25SThomas Gleixner }
813f7627e25SThomas Gleixner 
814148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c)
815f7627e25SThomas Gleixner {
816f7627e25SThomas Gleixner 	/* Get vendor name */
8174a148513SHarvey Harrison 	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
8184a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[0],
8194a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[8],
8204a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[4]);
821f7627e25SThomas Gleixner 
822f7627e25SThomas Gleixner 	c->x86 = 4;
8239d31d35bSYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
824f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
825f7627e25SThomas Gleixner 		u32 junk, tfms, cap0, misc;
8260f3fa48aSIngo Molnar 
827f7627e25SThomas Gleixner 		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
82899f925ceSBorislav Petkov 		c->x86		= x86_family(tfms);
82999f925ceSBorislav Petkov 		c->x86_model	= x86_model(tfms);
830b399151cSJia Zhang 		c->x86_stepping	= x86_stepping(tfms);
8310f3fa48aSIngo Molnar 
832d4387bd3SHuang, Ying 		if (cap0 & (1<<19)) {
833d4387bd3SHuang, Ying 			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
8349d31d35bSYinghai Lu 			c->x86_cache_alignment = c->x86_clflush_size;
835d4387bd3SHuang, Ying 		}
836f7627e25SThomas Gleixner 	}
837f7627e25SThomas Gleixner }
8383da99c97SYinghai Lu 
8398bf1ebcaSAndy Lutomirski static void apply_forced_caps(struct cpuinfo_x86 *c)
8408bf1ebcaSAndy Lutomirski {
8418bf1ebcaSAndy Lutomirski 	int i;
8428bf1ebcaSAndy Lutomirski 
8436cbd2171SThomas Gleixner 	for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
8448bf1ebcaSAndy Lutomirski 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
8458bf1ebcaSAndy Lutomirski 		c->x86_capability[i] |= cpu_caps_set[i];
8468bf1ebcaSAndy Lutomirski 	}
8478bf1ebcaSAndy Lutomirski }
8488bf1ebcaSAndy Lutomirski 
8497fcae111SDavid Woodhouse static void init_speculation_control(struct cpuinfo_x86 *c)
8507fcae111SDavid Woodhouse {
8517fcae111SDavid Woodhouse 	/*
8527fcae111SDavid Woodhouse 	 * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
8537fcae111SDavid Woodhouse 	 * and they also have a different bit for STIBP support. Also,
8547fcae111SDavid Woodhouse 	 * a hypervisor might have set the individual AMD bits even on
8557fcae111SDavid Woodhouse 	 * Intel CPUs, for finer-grained selection of what's available.
8567fcae111SDavid Woodhouse 	 */
8577fcae111SDavid Woodhouse 	if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
8587fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_IBRS);
8597fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_IBPB);
8607eb8956aSThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8617fcae111SDavid Woodhouse 	}
862e7c587daSBorislav Petkov 
8637fcae111SDavid Woodhouse 	if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
8647fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_STIBP);
865e7c587daSBorislav Petkov 
866bc226f07STom Lendacky 	if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
867bc226f07STom Lendacky 	    cpu_has(c, X86_FEATURE_VIRT_SSBD))
86852817587SThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_SSBD);
86952817587SThomas Gleixner 
8707eb8956aSThomas Gleixner 	if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
871e7c587daSBorislav Petkov 		set_cpu_cap(c, X86_FEATURE_IBRS);
8727eb8956aSThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8737eb8956aSThomas Gleixner 	}
874e7c587daSBorislav Petkov 
875e7c587daSBorislav Petkov 	if (cpu_has(c, X86_FEATURE_AMD_IBPB))
876e7c587daSBorislav Petkov 		set_cpu_cap(c, X86_FEATURE_IBPB);
877e7c587daSBorislav Petkov 
8787eb8956aSThomas Gleixner 	if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
879e7c587daSBorislav Petkov 		set_cpu_cap(c, X86_FEATURE_STIBP);
8807eb8956aSThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8817eb8956aSThomas Gleixner 	}
8826ac2f49eSKonrad Rzeszutek Wilk 
8836ac2f49eSKonrad Rzeszutek Wilk 	if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
8846ac2f49eSKonrad Rzeszutek Wilk 		set_cpu_cap(c, X86_FEATURE_SSBD);
8856ac2f49eSKonrad Rzeszutek Wilk 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8866ac2f49eSKonrad Rzeszutek Wilk 		clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
8876ac2f49eSKonrad Rzeszutek Wilk 	}
8887fcae111SDavid Woodhouse }
8897fcae111SDavid Woodhouse 
890148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c)
891093af8d7SYinghai Lu {
89239c06df4SBorislav Petkov 	u32 eax, ebx, ecx, edx;
893093af8d7SYinghai Lu 
894093af8d7SYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
895093af8d7SYinghai Lu 	if (c->cpuid_level >= 0x00000001) {
89639c06df4SBorislav Petkov 		cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
8970f3fa48aSIngo Molnar 
89839c06df4SBorislav Petkov 		c->x86_capability[CPUID_1_ECX] = ecx;
89939c06df4SBorislav Petkov 		c->x86_capability[CPUID_1_EDX] = edx;
900093af8d7SYinghai Lu 	}
901093af8d7SYinghai Lu 
9023df8d920SAndy Lutomirski 	/* Thermal and Power Management Leaf: level 0x00000006 (eax) */
9033df8d920SAndy Lutomirski 	if (c->cpuid_level >= 0x00000006)
9043df8d920SAndy Lutomirski 		c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
9053df8d920SAndy Lutomirski 
906bdc802dcSH. Peter Anvin 	/* Additional Intel-defined flags: level 0x00000007 */
907bdc802dcSH. Peter Anvin 	if (c->cpuid_level >= 0x00000007) {
908bdc802dcSH. Peter Anvin 		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
90939c06df4SBorislav Petkov 		c->x86_capability[CPUID_7_0_EBX] = ebx;
910dfb4a70fSDave Hansen 		c->x86_capability[CPUID_7_ECX] = ecx;
91195ca0ee8SDavid Woodhouse 		c->x86_capability[CPUID_7_EDX] = edx;
912b302e4b1SFenghua Yu 
913b302e4b1SFenghua Yu 		/* Check valid sub-leaf index before accessing it */
914b302e4b1SFenghua Yu 		if (eax >= 1) {
915b302e4b1SFenghua Yu 			cpuid_count(0x00000007, 1, &eax, &ebx, &ecx, &edx);
916b302e4b1SFenghua Yu 			c->x86_capability[CPUID_7_1_EAX] = eax;
917b302e4b1SFenghua Yu 		}
918bdc802dcSH. Peter Anvin 	}
919bdc802dcSH. Peter Anvin 
9206229ad27SFenghua Yu 	/* Extended state features: level 0x0000000d */
9216229ad27SFenghua Yu 	if (c->cpuid_level >= 0x0000000d) {
9226229ad27SFenghua Yu 		cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
9236229ad27SFenghua Yu 
92439c06df4SBorislav Petkov 		c->x86_capability[CPUID_D_1_EAX] = eax;
9256229ad27SFenghua Yu 	}
9266229ad27SFenghua Yu 
927093af8d7SYinghai Lu 	/* AMD-defined flags: level 0x80000001 */
92839c06df4SBorislav Petkov 	eax = cpuid_eax(0x80000000);
92939c06df4SBorislav Petkov 	c->extended_cpuid_level = eax;
9300f3fa48aSIngo Molnar 
93139c06df4SBorislav Petkov 	if ((eax & 0xffff0000) == 0x80000000) {
93239c06df4SBorislav Petkov 		if (eax >= 0x80000001) {
93339c06df4SBorislav Petkov 			cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
93439c06df4SBorislav Petkov 
93539c06df4SBorislav Petkov 			c->x86_capability[CPUID_8000_0001_ECX] = ecx;
93639c06df4SBorislav Petkov 			c->x86_capability[CPUID_8000_0001_EDX] = edx;
937093af8d7SYinghai Lu 		}
938093af8d7SYinghai Lu 	}
939093af8d7SYinghai Lu 
94071faad43SYazen Ghannam 	if (c->extended_cpuid_level >= 0x80000007) {
94171faad43SYazen Ghannam 		cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
94271faad43SYazen Ghannam 
94371faad43SYazen Ghannam 		c->x86_capability[CPUID_8000_0007_EBX] = ebx;
94471faad43SYazen Ghannam 		c->x86_power = edx;
94571faad43SYazen Ghannam 	}
94671faad43SYazen Ghannam 
947c65732e4SThomas Gleixner 	if (c->extended_cpuid_level >= 0x80000008) {
948c65732e4SThomas Gleixner 		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
949c65732e4SThomas Gleixner 		c->x86_capability[CPUID_8000_0008_EBX] = ebx;
950c65732e4SThomas Gleixner 	}
951c65732e4SThomas Gleixner 
9522ccd71f1SBorislav Petkov 	if (c->extended_cpuid_level >= 0x8000000a)
95339c06df4SBorislav Petkov 		c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
9542ccd71f1SBorislav Petkov 
955fb35d30fSSean Christopherson 	if (c->extended_cpuid_level >= 0x8000001f)
956fb35d30fSSean Christopherson 		c->x86_capability[CPUID_8000_001F_EAX] = cpuid_eax(0x8000001f);
957fb35d30fSSean Christopherson 
9581dedefd1SJacob Pan 	init_scattered_cpuid_features(c);
9597fcae111SDavid Woodhouse 	init_speculation_control(c);
96060d34501SAndy Lutomirski 
96160d34501SAndy Lutomirski 	/*
96260d34501SAndy Lutomirski 	 * Clear/Set all flags overridden by options, after probe.
96360d34501SAndy Lutomirski 	 * This needs to happen each time we re-probe, which may happen
96460d34501SAndy Lutomirski 	 * several times during CPU initialization.
96560d34501SAndy Lutomirski 	 */
96660d34501SAndy Lutomirski 	apply_forced_caps(c);
967093af8d7SYinghai Lu }
968093af8d7SYinghai Lu 
969405c018aSM. Vefa Bicakci void get_cpu_address_sizes(struct cpuinfo_x86 *c)
970d94a155cSKirill A. Shutemov {
971d94a155cSKirill A. Shutemov 	u32 eax, ebx, ecx, edx;
972d94a155cSKirill A. Shutemov 
973d94a155cSKirill A. Shutemov 	if (c->extended_cpuid_level >= 0x80000008) {
974d94a155cSKirill A. Shutemov 		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
975d94a155cSKirill A. Shutemov 
976d94a155cSKirill A. Shutemov 		c->x86_virt_bits = (eax >> 8) & 0xff;
977d94a155cSKirill A. Shutemov 		c->x86_phys_bits = eax & 0xff;
978d94a155cSKirill A. Shutemov 	}
979d94a155cSKirill A. Shutemov #ifdef CONFIG_X86_32
980d94a155cSKirill A. Shutemov 	else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
981d94a155cSKirill A. Shutemov 		c->x86_phys_bits = 36;
982d94a155cSKirill A. Shutemov #endif
983cc51e542SAndi Kleen 	c->x86_cache_bits = c->x86_phys_bits;
984d94a155cSKirill A. Shutemov }
985d94a155cSKirill A. Shutemov 
986148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
987aef93c8bSYinghai Lu {
988aef93c8bSYinghai Lu #ifdef CONFIG_X86_32
989aef93c8bSYinghai Lu 	int i;
990aef93c8bSYinghai Lu 
991aef93c8bSYinghai Lu 	/*
992aef93c8bSYinghai Lu 	 * First of all, decide if this is a 486 or higher
993aef93c8bSYinghai Lu 	 * It's a 486 if we can modify the AC flag
994aef93c8bSYinghai Lu 	 */
995aef93c8bSYinghai Lu 	if (flag_is_changeable_p(X86_EFLAGS_AC))
996aef93c8bSYinghai Lu 		c->x86 = 4;
997aef93c8bSYinghai Lu 	else
998aef93c8bSYinghai Lu 		c->x86 = 3;
999aef93c8bSYinghai Lu 
1000aef93c8bSYinghai Lu 	for (i = 0; i < X86_VENDOR_NUM; i++)
1001aef93c8bSYinghai Lu 		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
1002aef93c8bSYinghai Lu 			c->x86_vendor_id[0] = 0;
1003aef93c8bSYinghai Lu 			cpu_devs[i]->c_identify(c);
1004aef93c8bSYinghai Lu 			if (c->x86_vendor_id[0]) {
1005aef93c8bSYinghai Lu 				get_cpu_vendor(c);
1006aef93c8bSYinghai Lu 				break;
1007aef93c8bSYinghai Lu 			}
1008aef93c8bSYinghai Lu 		}
1009aef93c8bSYinghai Lu #endif
1010093af8d7SYinghai Lu }
1011f7627e25SThomas Gleixner 
101236ad3513SThomas Gleixner #define NO_SPECULATION		BIT(0)
101336ad3513SThomas Gleixner #define NO_MELTDOWN		BIT(1)
101436ad3513SThomas Gleixner #define NO_SSB			BIT(2)
101536ad3513SThomas Gleixner #define NO_L1TF			BIT(3)
1016ed5194c2SAndi Kleen #define NO_MDS			BIT(4)
1017e261f209SThomas Gleixner #define MSBDS_ONLY		BIT(5)
1018f36cf386SThomas Gleixner #define NO_SWAPGS		BIT(6)
1019db4d30fbSVineela Tummalapalli #define NO_ITLB_MULTIHIT	BIT(7)
10201e41a766STony W Wang-oc #define NO_SPECTRE_V2		BIT(8)
102136ad3513SThomas Gleixner 
1022f6d502fcSThomas Gleixner #define VULNWL(vendor, family, model, whitelist)	\
1023f6d502fcSThomas Gleixner 	X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, whitelist)
102436ad3513SThomas Gleixner 
102536ad3513SThomas Gleixner #define VULNWL_INTEL(model, whitelist)		\
102636ad3513SThomas Gleixner 	VULNWL(INTEL, 6, INTEL_FAM6_##model, whitelist)
102736ad3513SThomas Gleixner 
102836ad3513SThomas Gleixner #define VULNWL_AMD(family, whitelist)		\
102936ad3513SThomas Gleixner 	VULNWL(AMD, family, X86_MODEL_ANY, whitelist)
103036ad3513SThomas Gleixner 
103136ad3513SThomas Gleixner #define VULNWL_HYGON(family, whitelist)		\
103236ad3513SThomas Gleixner 	VULNWL(HYGON, family, X86_MODEL_ANY, whitelist)
103336ad3513SThomas Gleixner 
103436ad3513SThomas Gleixner static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
103536ad3513SThomas Gleixner 	VULNWL(ANY,	4, X86_MODEL_ANY,	NO_SPECULATION),
103636ad3513SThomas Gleixner 	VULNWL(CENTAUR,	5, X86_MODEL_ANY,	NO_SPECULATION),
103736ad3513SThomas Gleixner 	VULNWL(INTEL,	5, X86_MODEL_ANY,	NO_SPECULATION),
103836ad3513SThomas Gleixner 	VULNWL(NSC,	5, X86_MODEL_ANY,	NO_SPECULATION),
103936ad3513SThomas Gleixner 
1040ed5194c2SAndi Kleen 	/* Intel Family 6 */
1041db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SALTWELL,		NO_SPECULATION | NO_ITLB_MULTIHIT),
1042db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SALTWELL_TABLET,	NO_SPECULATION | NO_ITLB_MULTIHIT),
1043db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SALTWELL_MID,		NO_SPECULATION | NO_ITLB_MULTIHIT),
1044db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_BONNELL,		NO_SPECULATION | NO_ITLB_MULTIHIT),
1045db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_BONNELL_MID,		NO_SPECULATION | NO_ITLB_MULTIHIT),
104636ad3513SThomas Gleixner 
1047db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SILVERMONT,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1048db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SILVERMONT_D,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1049db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SILVERMONT_MID,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1050db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_AIRMONT,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1051db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(XEON_PHI_KNL,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1052db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(XEON_PHI_KNM,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
105336ad3513SThomas Gleixner 
105436ad3513SThomas Gleixner 	VULNWL_INTEL(CORE_YONAH,		NO_SSB),
105536ad3513SThomas Gleixner 
1056db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_AIRMONT_MID,		NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1057db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_AIRMONT_NP,		NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
105836ad3513SThomas Gleixner 
1059db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_GOLDMONT,		NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
1060db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_GOLDMONT_D,		NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
1061db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_GOLDMONT_PLUS,	NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
1062f36cf386SThomas Gleixner 
1063f36cf386SThomas Gleixner 	/*
1064f36cf386SThomas Gleixner 	 * Technically, swapgs isn't serializing on AMD (despite it previously
1065f36cf386SThomas Gleixner 	 * being documented as such in the APM).  But according to AMD, %gs is
1066f36cf386SThomas Gleixner 	 * updated non-speculatively, and the issuing of %gs-relative memory
1067f36cf386SThomas Gleixner 	 * operands will be blocked until the %gs update completes, which is
1068f36cf386SThomas Gleixner 	 * good enough for our purposes.
1069f36cf386SThomas Gleixner 	 */
1070ed5194c2SAndi Kleen 
1071cad14885SPawan Gupta 	VULNWL_INTEL(ATOM_TREMONT_D,		NO_ITLB_MULTIHIT),
1072cad14885SPawan Gupta 
1073ed5194c2SAndi Kleen 	/* AMD Family 0xf - 0x12 */
1074db4d30fbSVineela Tummalapalli 	VULNWL_AMD(0x0f,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
1075db4d30fbSVineela Tummalapalli 	VULNWL_AMD(0x10,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
1076db4d30fbSVineela Tummalapalli 	VULNWL_AMD(0x11,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
1077db4d30fbSVineela Tummalapalli 	VULNWL_AMD(0x12,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
107836ad3513SThomas Gleixner 
107936ad3513SThomas Gleixner 	/* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */
1080db4d30fbSVineela Tummalapalli 	VULNWL_AMD(X86_FAMILY_ANY,	NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
1081db4d30fbSVineela Tummalapalli 	VULNWL_HYGON(X86_FAMILY_ANY,	NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
10821e41a766STony W Wang-oc 
10831e41a766STony W Wang-oc 	/* Zhaoxin Family 7 */
1084a84de2faSTony W Wang-oc 	VULNWL(CENTAUR,	7, X86_MODEL_ANY,	NO_SPECTRE_V2 | NO_SWAPGS),
1085a84de2faSTony W Wang-oc 	VULNWL(ZHAOXIN,	7, X86_MODEL_ANY,	NO_SPECTRE_V2 | NO_SWAPGS),
1086fec9434aSDavid Woodhouse 	{}
1087fec9434aSDavid Woodhouse };
1088fec9434aSDavid Woodhouse 
10897e5b3c26SMark Gross #define VULNBL_INTEL_STEPPINGS(model, steppings, issues)		   \
10907e5b3c26SMark Gross 	X86_MATCH_VENDOR_FAM_MODEL_STEPPINGS_FEATURE(INTEL, 6,		   \
10917e5b3c26SMark Gross 					    INTEL_FAM6_##model, steppings, \
10927e5b3c26SMark Gross 					    X86_FEATURE_ANY, issues)
10937e5b3c26SMark Gross 
10947e5b3c26SMark Gross #define SRBDS		BIT(0)
10957e5b3c26SMark Gross 
10967e5b3c26SMark Gross static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
10977e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(IVYBRIDGE,	X86_STEPPING_ANY,		SRBDS),
10987e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(HASWELL,		X86_STEPPING_ANY,		SRBDS),
10997e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(HASWELL_L,	X86_STEPPING_ANY,		SRBDS),
11007e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(HASWELL_G,	X86_STEPPING_ANY,		SRBDS),
11017e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(BROADWELL_G,	X86_STEPPING_ANY,		SRBDS),
11027e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(BROADWELL,	X86_STEPPING_ANY,		SRBDS),
11037e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(SKYLAKE_L,	X86_STEPPING_ANY,		SRBDS),
11047e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(SKYLAKE,		X86_STEPPING_ANY,		SRBDS),
11057e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(KABYLAKE_L,	X86_STEPPINGS(0x0, 0xC),	SRBDS),
11067e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(KABYLAKE,	X86_STEPPINGS(0x0, 0xD),	SRBDS),
11077e5b3c26SMark Gross 	{}
11087e5b3c26SMark Gross };
11097e5b3c26SMark Gross 
111093920f61SMark Gross static bool __init cpu_matches(const struct x86_cpu_id *table, unsigned long which)
111136ad3513SThomas Gleixner {
111293920f61SMark Gross 	const struct x86_cpu_id *m = x86_match_cpu(table);
1113fec9434aSDavid Woodhouse 
111436ad3513SThomas Gleixner 	return m && !!(m->driver_data & which);
111536ad3513SThomas Gleixner }
111617dbca11SAndi Kleen 
1117286836a7SPawan Gupta u64 x86_read_arch_cap_msr(void)
1118fec9434aSDavid Woodhouse {
1119fec9434aSDavid Woodhouse 	u64 ia32_cap = 0;
1120fec9434aSDavid Woodhouse 
1121286836a7SPawan Gupta 	if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1122286836a7SPawan Gupta 		rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
1123286836a7SPawan Gupta 
1124286836a7SPawan Gupta 	return ia32_cap;
1125286836a7SPawan Gupta }
1126286836a7SPawan Gupta 
1127286836a7SPawan Gupta static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
1128286836a7SPawan Gupta {
1129286836a7SPawan Gupta 	u64 ia32_cap = x86_read_arch_cap_msr();
1130286836a7SPawan Gupta 
1131db4d30fbSVineela Tummalapalli 	/* Set ITLB_MULTIHIT bug if cpu is not in the whitelist and not mitigated */
113293920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_ITLB_MULTIHIT) &&
113393920f61SMark Gross 	    !(ia32_cap & ARCH_CAP_PSCHANGE_MC_NO))
1134db4d30fbSVineela Tummalapalli 		setup_force_cpu_bug(X86_BUG_ITLB_MULTIHIT);
1135db4d30fbSVineela Tummalapalli 
113693920f61SMark Gross 	if (cpu_matches(cpu_vuln_whitelist, NO_SPECULATION))
11378ecc4979SDominik Brodowski 		return;
11388ecc4979SDominik Brodowski 
11398ecc4979SDominik Brodowski 	setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
11401e41a766STony W Wang-oc 
114193920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_SPECTRE_V2))
11428ecc4979SDominik Brodowski 		setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
11438ecc4979SDominik Brodowski 
114493920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_SSB) &&
114593920f61SMark Gross 	    !(ia32_cap & ARCH_CAP_SSB_NO) &&
114624809860SKonrad Rzeszutek Wilk 	   !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
1147c456442cSKonrad Rzeszutek Wilk 		setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
1148c456442cSKonrad Rzeszutek Wilk 
1149706d5168SSai Praneeth 	if (ia32_cap & ARCH_CAP_IBRS_ALL)
1150706d5168SSai Praneeth 		setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
1151706d5168SSai Praneeth 
115293920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_MDS) &&
115393920f61SMark Gross 	    !(ia32_cap & ARCH_CAP_MDS_NO)) {
1154ed5194c2SAndi Kleen 		setup_force_cpu_bug(X86_BUG_MDS);
115593920f61SMark Gross 		if (cpu_matches(cpu_vuln_whitelist, MSBDS_ONLY))
1156e261f209SThomas Gleixner 			setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
1157e261f209SThomas Gleixner 	}
1158ed5194c2SAndi Kleen 
115993920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_SWAPGS))
1160f36cf386SThomas Gleixner 		setup_force_cpu_bug(X86_BUG_SWAPGS);
1161f36cf386SThomas Gleixner 
11621b42f017SPawan Gupta 	/*
11631b42f017SPawan Gupta 	 * When the CPU is not mitigated for TAA (TAA_NO=0) set TAA bug when:
11641b42f017SPawan Gupta 	 *	- TSX is supported or
11651b42f017SPawan Gupta 	 *	- TSX_CTRL is present
11661b42f017SPawan Gupta 	 *
11671b42f017SPawan Gupta 	 * TSX_CTRL check is needed for cases when TSX could be disabled before
11681b42f017SPawan Gupta 	 * the kernel boot e.g. kexec.
11691b42f017SPawan Gupta 	 * TSX_CTRL check alone is not sufficient for cases when the microcode
11701b42f017SPawan Gupta 	 * update is not present or running as guest that don't get TSX_CTRL.
11711b42f017SPawan Gupta 	 */
11721b42f017SPawan Gupta 	if (!(ia32_cap & ARCH_CAP_TAA_NO) &&
11731b42f017SPawan Gupta 	    (cpu_has(c, X86_FEATURE_RTM) ||
11741b42f017SPawan Gupta 	     (ia32_cap & ARCH_CAP_TSX_CTRL_MSR)))
11751b42f017SPawan Gupta 		setup_force_cpu_bug(X86_BUG_TAA);
11761b42f017SPawan Gupta 
11777e5b3c26SMark Gross 	/*
11787e5b3c26SMark Gross 	 * SRBDS affects CPUs which support RDRAND or RDSEED and are listed
11797e5b3c26SMark Gross 	 * in the vulnerability blacklist.
11807e5b3c26SMark Gross 	 */
11817e5b3c26SMark Gross 	if ((cpu_has(c, X86_FEATURE_RDRAND) ||
11827e5b3c26SMark Gross 	     cpu_has(c, X86_FEATURE_RDSEED)) &&
11837e5b3c26SMark Gross 	    cpu_matches(cpu_vuln_blacklist, SRBDS))
11847e5b3c26SMark Gross 		    setup_force_cpu_bug(X86_BUG_SRBDS);
11857e5b3c26SMark Gross 
118693920f61SMark Gross 	if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
11874a28bfe3SKonrad Rzeszutek Wilk 		return;
1188fec9434aSDavid Woodhouse 
1189fec9434aSDavid Woodhouse 	/* Rogue Data Cache Load? No! */
1190fec9434aSDavid Woodhouse 	if (ia32_cap & ARCH_CAP_RDCL_NO)
11914a28bfe3SKonrad Rzeszutek Wilk 		return;
1192fec9434aSDavid Woodhouse 
11934a28bfe3SKonrad Rzeszutek Wilk 	setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
119417dbca11SAndi Kleen 
119593920f61SMark Gross 	if (cpu_matches(cpu_vuln_whitelist, NO_L1TF))
119617dbca11SAndi Kleen 		return;
119717dbca11SAndi Kleen 
119817dbca11SAndi Kleen 	setup_force_cpu_bug(X86_BUG_L1TF);
1199fec9434aSDavid Woodhouse }
1200fec9434aSDavid Woodhouse 
120134048c9eSPaolo Ciarrocchi /*
12028990cac6SPavel Tatashin  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
12038990cac6SPavel Tatashin  * unfortunately, that's not true in practice because of early VIA
12048990cac6SPavel Tatashin  * chips and (more importantly) broken virtualizers that are not easy
12058990cac6SPavel Tatashin  * to detect. In the latter case it doesn't even *fail* reliably, so
12068990cac6SPavel Tatashin  * probing for it doesn't even work. Disable it completely on 32-bit
12078990cac6SPavel Tatashin  * unless we can find a reliable way to detect all the broken cases.
12088990cac6SPavel Tatashin  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
12098990cac6SPavel Tatashin  */
12109b3661cdSBorislav Petkov static void detect_nopl(void)
12118990cac6SPavel Tatashin {
12128990cac6SPavel Tatashin #ifdef CONFIG_X86_32
12139b3661cdSBorislav Petkov 	setup_clear_cpu_cap(X86_FEATURE_NOPL);
12148990cac6SPavel Tatashin #else
12159b3661cdSBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_NOPL);
12168990cac6SPavel Tatashin #endif
12178990cac6SPavel Tatashin }
12188990cac6SPavel Tatashin 
12198990cac6SPavel Tatashin /*
12201ef5423aSMike Hommey  * We parse cpu parameters early because fpu__init_system() is executed
12211ef5423aSMike Hommey  * before parse_early_param().
12221ef5423aSMike Hommey  */
12231ef5423aSMike Hommey static void __init cpu_parse_early_param(void)
12241ef5423aSMike Hommey {
12251ef5423aSMike Hommey 	char arg[128];
12261ef5423aSMike Hommey 	char *argptr = arg;
12271ef5423aSMike Hommey 	int arglen, res, bit;
12281ef5423aSMike Hommey 
12291ef5423aSMike Hommey #ifdef CONFIG_X86_32
12301ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "no387"))
12311ef5423aSMike Hommey #ifdef CONFIG_MATH_EMULATION
12321ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_FPU);
12331ef5423aSMike Hommey #else
12341ef5423aSMike Hommey 		pr_err("Option 'no387' required CONFIG_MATH_EMULATION enabled.\n");
12351ef5423aSMike Hommey #endif
12361ef5423aSMike Hommey 
12371ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "nofxsr"))
12381ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_FXSR);
12391ef5423aSMike Hommey #endif
12401ef5423aSMike Hommey 
12411ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "noxsave"))
12421ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_XSAVE);
12431ef5423aSMike Hommey 
12441ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
12451ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
12461ef5423aSMike Hommey 
12471ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
12481ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_XSAVES);
12491ef5423aSMike Hommey 
12501ef5423aSMike Hommey 	arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg));
12511ef5423aSMike Hommey 	if (arglen <= 0)
12521ef5423aSMike Hommey 		return;
12531ef5423aSMike Hommey 
12541ef5423aSMike Hommey 	pr_info("Clearing CPUID bits:");
12551ef5423aSMike Hommey 	do {
12561ef5423aSMike Hommey 		res = get_option(&argptr, &bit);
12571ef5423aSMike Hommey 		if (res == 0 || res == 3)
12581ef5423aSMike Hommey 			break;
12591ef5423aSMike Hommey 
12601ef5423aSMike Hommey 		/* If the argument was too long, the last bit may be cut off */
12611ef5423aSMike Hommey 		if (res == 1 && arglen >= sizeof(arg))
12621ef5423aSMike Hommey 			break;
12631ef5423aSMike Hommey 
12641ef5423aSMike Hommey 		if (bit >= 0 && bit < NCAPINTS * 32) {
12651ef5423aSMike Hommey 			pr_cont(" " X86_CAP_FMT, x86_cap_flag(bit));
12661ef5423aSMike Hommey 			setup_clear_cpu_cap(bit);
12671ef5423aSMike Hommey 		}
12681ef5423aSMike Hommey 	} while (res == 2);
12691ef5423aSMike Hommey 	pr_cont("\n");
12701ef5423aSMike Hommey }
12711ef5423aSMike Hommey 
12721ef5423aSMike Hommey /*
127334048c9eSPaolo Ciarrocchi  * Do minimum CPU detection early.
127434048c9eSPaolo Ciarrocchi  * Fields really needed: vendor, cpuid_level, family, model, mask,
127534048c9eSPaolo Ciarrocchi  * cache alignment.
127634048c9eSPaolo Ciarrocchi  * The others are not touched to avoid unwanted side effects.
127734048c9eSPaolo Ciarrocchi  *
1278a1652bb8SJean Delvare  * WARNING: this function is only called on the boot CPU.  Don't add code
1279a1652bb8SJean Delvare  * here that is supposed to run on all CPUs.
128034048c9eSPaolo Ciarrocchi  */
12813da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c)
1282f7627e25SThomas Gleixner {
12836627d242SYinghai Lu #ifdef CONFIG_X86_64
12846627d242SYinghai Lu 	c->x86_clflush_size = 64;
128513c6c532SJan Beulich 	c->x86_phys_bits = 36;
128613c6c532SJan Beulich 	c->x86_virt_bits = 48;
12876627d242SYinghai Lu #else
1288d4387bd3SHuang, Ying 	c->x86_clflush_size = 32;
128913c6c532SJan Beulich 	c->x86_phys_bits = 32;
129013c6c532SJan Beulich 	c->x86_virt_bits = 32;
12916627d242SYinghai Lu #endif
12920a488a53SYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
1293f7627e25SThomas Gleixner 
12940e96f31eSJordan Borgner 	memset(&c->x86_capability, 0, sizeof(c->x86_capability));
12950a488a53SYinghai Lu 	c->extended_cpuid_level = 0;
12960a488a53SYinghai Lu 
12972893cc8fSMatthew Whitehead 	if (!have_cpuid_p())
12982893cc8fSMatthew Whitehead 		identify_cpu_without_cpuid(c);
12992893cc8fSMatthew Whitehead 
1300aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
130105fb3c19SAndy Lutomirski 	if (have_cpuid_p()) {
1302f7627e25SThomas Gleixner 		cpu_detect(c);
13033da99c97SYinghai Lu 		get_cpu_vendor(c);
13043da99c97SYinghai Lu 		get_cpu_cap(c);
1305d94a155cSKirill A. Shutemov 		get_cpu_address_sizes(c);
130678d1b296SBorislav Petkov 		setup_force_cpu_cap(X86_FEATURE_CPUID);
13071ef5423aSMike Hommey 		cpu_parse_early_param();
130812cf105cSKrzysztof Helt 
130910a434fcSYinghai Lu 		if (this_cpu->c_early_init)
131010a434fcSYinghai Lu 			this_cpu->c_early_init(c);
13113da99c97SYinghai Lu 
1312f6e9456cSRobert Richter 		c->cpu_index = 0;
1313b38b0665SH. Peter Anvin 		filter_cpuid_features(c, false);
1314de5397adSFenghua Yu 
1315a110b5ecSBorislav Petkov 		if (this_cpu->c_bsp_init)
1316a110b5ecSBorislav Petkov 			this_cpu->c_bsp_init(c);
131778d1b296SBorislav Petkov 	} else {
131878d1b296SBorislav Petkov 		setup_clear_cpu_cap(X86_FEATURE_CPUID);
131905fb3c19SAndy Lutomirski 	}
1320c3b83598SBorislav Petkov 
1321c3b83598SBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
1322a89f040fSThomas Gleixner 
13234a28bfe3SKonrad Rzeszutek Wilk 	cpu_set_bug_bits(c);
132499c6fa25SDavid Woodhouse 
1325ebb1064eSFenghua Yu 	sld_setup(c);
13266650cdd9SPeter Zijlstra (Intel) 
1327db52ef74SIngo Molnar 	fpu__init_system(c);
1328b8b7abaeSAndy Lutomirski 
1329939ef713SChang S. Bae 	init_sigframe_size();
1330939ef713SChang S. Bae 
1331b8b7abaeSAndy Lutomirski #ifdef CONFIG_X86_32
1332b8b7abaeSAndy Lutomirski 	/*
1333b8b7abaeSAndy Lutomirski 	 * Regardless of whether PCID is enumerated, the SDM says
1334b8b7abaeSAndy Lutomirski 	 * that it can't be enabled in 32-bit mode.
1335b8b7abaeSAndy Lutomirski 	 */
1336b8b7abaeSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_PCID);
1337b8b7abaeSAndy Lutomirski #endif
1338372fddf7SKirill A. Shutemov 
1339372fddf7SKirill A. Shutemov 	/*
1340372fddf7SKirill A. Shutemov 	 * Later in the boot process pgtable_l5_enabled() relies on
1341372fddf7SKirill A. Shutemov 	 * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not
1342372fddf7SKirill A. Shutemov 	 * enabled by this point we need to clear the feature bit to avoid
1343372fddf7SKirill A. Shutemov 	 * false-positives at the later stage.
1344372fddf7SKirill A. Shutemov 	 *
1345372fddf7SKirill A. Shutemov 	 * pgtable_l5_enabled() can be false here for several reasons:
1346372fddf7SKirill A. Shutemov 	 *  - 5-level paging is disabled compile-time;
1347372fddf7SKirill A. Shutemov 	 *  - it's 32-bit kernel;
1348372fddf7SKirill A. Shutemov 	 *  - machine doesn't support 5-level paging;
1349372fddf7SKirill A. Shutemov 	 *  - user specified 'no5lvl' in kernel command line.
1350372fddf7SKirill A. Shutemov 	 */
1351372fddf7SKirill A. Shutemov 	if (!pgtable_l5_enabled())
1352372fddf7SKirill A. Shutemov 		setup_clear_cpu_cap(X86_FEATURE_LA57);
13538990cac6SPavel Tatashin 
13549b3661cdSBorislav Petkov 	detect_nopl();
1355f7627e25SThomas Gleixner }
1356f7627e25SThomas Gleixner 
13579d31d35bSYinghai Lu void __init early_cpu_init(void)
13589d31d35bSYinghai Lu {
135902dde8b4SJan Beulich 	const struct cpu_dev *const *cdev;
136010a434fcSYinghai Lu 	int count = 0;
13619d31d35bSYinghai Lu 
1362ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
13631b74dde7SChen Yucong 	pr_info("KERNEL supported cpus:\n");
136431c997caSIngo Molnar #endif
136531c997caSIngo Molnar 
136610a434fcSYinghai Lu 	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
136702dde8b4SJan Beulich 		const struct cpu_dev *cpudev = *cdev;
13689d31d35bSYinghai Lu 
136910a434fcSYinghai Lu 		if (count >= X86_VENDOR_NUM)
137010a434fcSYinghai Lu 			break;
137110a434fcSYinghai Lu 		cpu_devs[count] = cpudev;
137210a434fcSYinghai Lu 		count++;
137310a434fcSYinghai Lu 
1374ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
137531c997caSIngo Molnar 		{
137631c997caSIngo Molnar 			unsigned int j;
137731c997caSIngo Molnar 
137810a434fcSYinghai Lu 			for (j = 0; j < 2; j++) {
137910a434fcSYinghai Lu 				if (!cpudev->c_ident[j])
138010a434fcSYinghai Lu 					continue;
13811b74dde7SChen Yucong 				pr_info("  %s %s\n", cpudev->c_vendor,
138210a434fcSYinghai Lu 					cpudev->c_ident[j]);
138310a434fcSYinghai Lu 			}
138410a434fcSYinghai Lu 		}
13850388423dSDave Jones #endif
138631c997caSIngo Molnar 	}
13879d31d35bSYinghai Lu 	early_identify_cpu(&boot_cpu_data);
1388f7627e25SThomas Gleixner }
1389f7627e25SThomas Gleixner 
13907a5d6704SAndy Lutomirski static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
13917a5d6704SAndy Lutomirski {
13927a5d6704SAndy Lutomirski #ifdef CONFIG_X86_64
139358a5aac5SAndy Lutomirski 	/*
13947a5d6704SAndy Lutomirski 	 * Empirically, writing zero to a segment selector on AMD does
13957a5d6704SAndy Lutomirski 	 * not clear the base, whereas writing zero to a segment
13967a5d6704SAndy Lutomirski 	 * selector on Intel does clear the base.  Intel's behavior
13977a5d6704SAndy Lutomirski 	 * allows slightly faster context switches in the common case
13987a5d6704SAndy Lutomirski 	 * where GS is unused by the prev and next threads.
139958a5aac5SAndy Lutomirski 	 *
14007a5d6704SAndy Lutomirski 	 * Since neither vendor documents this anywhere that I can see,
1401d9f6e12fSIngo Molnar 	 * detect it directly instead of hard-coding the choice by
14027a5d6704SAndy Lutomirski 	 * vendor.
14037a5d6704SAndy Lutomirski 	 *
14047a5d6704SAndy Lutomirski 	 * I've designated AMD's behavior as the "bug" because it's
14057a5d6704SAndy Lutomirski 	 * counterintuitive and less friendly.
140658a5aac5SAndy Lutomirski 	 */
14077a5d6704SAndy Lutomirski 
14087a5d6704SAndy Lutomirski 	unsigned long old_base, tmp;
14097a5d6704SAndy Lutomirski 	rdmsrl(MSR_FS_BASE, old_base);
14107a5d6704SAndy Lutomirski 	wrmsrl(MSR_FS_BASE, 1);
14117a5d6704SAndy Lutomirski 	loadsegment(fs, 0);
14127a5d6704SAndy Lutomirski 	rdmsrl(MSR_FS_BASE, tmp);
14137a5d6704SAndy Lutomirski 	if (tmp != 0)
14147a5d6704SAndy Lutomirski 		set_cpu_bug(c, X86_BUG_NULL_SEG);
14157a5d6704SAndy Lutomirski 	wrmsrl(MSR_FS_BASE, old_base);
141658a5aac5SAndy Lutomirski #endif
1417f7627e25SThomas Gleixner }
1418f7627e25SThomas Gleixner 
1419148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c)
1420f7627e25SThomas Gleixner {
14213da99c97SYinghai Lu 	c->extended_cpuid_level = 0;
1422f7627e25SThomas Gleixner 
1423aef93c8bSYinghai Lu 	if (!have_cpuid_p())
1424aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
1425f7627e25SThomas Gleixner 
1426aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
1427a9853dd6SIngo Molnar 	if (!have_cpuid_p())
1428aef93c8bSYinghai Lu 		return;
1429aef93c8bSYinghai Lu 
14303da99c97SYinghai Lu 	cpu_detect(c);
14313da99c97SYinghai Lu 
14323da99c97SYinghai Lu 	get_cpu_vendor(c);
14333da99c97SYinghai Lu 
14343da99c97SYinghai Lu 	get_cpu_cap(c);
14353da99c97SYinghai Lu 
1436d94a155cSKirill A. Shutemov 	get_cpu_address_sizes(c);
1437d94a155cSKirill A. Shutemov 
1438f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
14393da99c97SYinghai Lu 		c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
1440b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32
1441c8e56d20SBorislav Petkov # ifdef CONFIG_SMP
1442cb8cc442SIngo Molnar 		c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1443f7627e25SThomas Gleixner # else
144401aaea1aSYinghai Lu 		c->apicid = c->initial_apicid;
1445f7627e25SThomas Gleixner # endif
1446b89d3b3eSYinghai Lu #endif
1447b89d3b3eSYinghai Lu 		c->phys_proc_id = c->initial_apicid;
1448f7627e25SThomas Gleixner 	}
1449f7627e25SThomas Gleixner 
1450f7627e25SThomas Gleixner 	get_model_name(c); /* Default name */
1451f7627e25SThomas Gleixner 
14527a5d6704SAndy Lutomirski 	detect_null_seg_behavior(c);
14530230bb03SAndy Lutomirski 
14540230bb03SAndy Lutomirski 	/*
14550230bb03SAndy Lutomirski 	 * ESPFIX is a strange bug.  All real CPUs have it.  Paravirt
14560230bb03SAndy Lutomirski 	 * systems that run Linux at CPL > 0 may or may not have the
14570230bb03SAndy Lutomirski 	 * issue, but, even if they have the issue, there's absolutely
14580230bb03SAndy Lutomirski 	 * nothing we can do about it because we can't use the real IRET
14590230bb03SAndy Lutomirski 	 * instruction.
14600230bb03SAndy Lutomirski 	 *
14610230bb03SAndy Lutomirski 	 * NB: For the time being, only 32-bit kernels support
14620230bb03SAndy Lutomirski 	 * X86_BUG_ESPFIX as such.  64-bit kernels directly choose
14630230bb03SAndy Lutomirski 	 * whether to apply espfix using paravirt hooks.  If any
14640230bb03SAndy Lutomirski 	 * non-paravirt system ever shows up that does *not* have the
14650230bb03SAndy Lutomirski 	 * ESPFIX issue, we can change this.
14660230bb03SAndy Lutomirski 	 */
14670230bb03SAndy Lutomirski #ifdef CONFIG_X86_32
14680230bb03SAndy Lutomirski 	set_cpu_bug(c, X86_BUG_ESPFIX);
14690230bb03SAndy Lutomirski #endif
1470f7627e25SThomas Gleixner }
1471f7627e25SThomas Gleixner 
1472f7627e25SThomas Gleixner /*
14739d85eb91SThomas Gleixner  * Validate that ACPI/mptables have the same information about the
14749d85eb91SThomas Gleixner  * effective APIC id and update the package map.
1475d49597fdSThomas Gleixner  */
14769d85eb91SThomas Gleixner static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
1477d49597fdSThomas Gleixner {
1478d49597fdSThomas Gleixner #ifdef CONFIG_SMP
14799d85eb91SThomas Gleixner 	unsigned int apicid, cpu = smp_processor_id();
1480d49597fdSThomas Gleixner 
1481d49597fdSThomas Gleixner 	apicid = apic->cpu_present_to_apicid(cpu);
1482d49597fdSThomas Gleixner 
14839d85eb91SThomas Gleixner 	if (apicid != c->apicid) {
14849d85eb91SThomas Gleixner 		pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
1485d49597fdSThomas Gleixner 		       cpu, apicid, c->initial_apicid);
1486d49597fdSThomas Gleixner 	}
14879d85eb91SThomas Gleixner 	BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
1488212bf4fdSLen Brown 	BUG_ON(topology_update_die_map(c->cpu_die_id, cpu));
1489d49597fdSThomas Gleixner #else
1490d49597fdSThomas Gleixner 	c->logical_proc_id = 0;
1491d49597fdSThomas Gleixner #endif
1492d49597fdSThomas Gleixner }
1493d49597fdSThomas Gleixner 
1494d49597fdSThomas Gleixner /*
1495f7627e25SThomas Gleixner  * This does the hard work of actually picking apart the CPU stuff...
1496f7627e25SThomas Gleixner  */
1497148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c)
1498f7627e25SThomas Gleixner {
1499f7627e25SThomas Gleixner 	int i;
1500f7627e25SThomas Gleixner 
1501f7627e25SThomas Gleixner 	c->loops_per_jiffy = loops_per_jiffy;
150224dbc600SGustavo A. R. Silva 	c->x86_cache_size = 0;
1503f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
1504b399151cSJia Zhang 	c->x86_model = c->x86_stepping = 0;	/* So far unknown... */
1505f7627e25SThomas Gleixner 	c->x86_vendor_id[0] = '\0'; /* Unset */
1506f7627e25SThomas Gleixner 	c->x86_model_id[0] = '\0';  /* Unset */
1507f7627e25SThomas Gleixner 	c->x86_max_cores = 1;
1508102bbe3aSYinghai Lu 	c->x86_coreid_bits = 0;
150979a8b9aaSBorislav Petkov 	c->cu_id = 0xff;
151011fdd252SYinghai Lu #ifdef CONFIG_X86_64
1511102bbe3aSYinghai Lu 	c->x86_clflush_size = 64;
151213c6c532SJan Beulich 	c->x86_phys_bits = 36;
151313c6c532SJan Beulich 	c->x86_virt_bits = 48;
1514102bbe3aSYinghai Lu #else
1515102bbe3aSYinghai Lu 	c->cpuid_level = -1;	/* CPUID not detected */
1516f7627e25SThomas Gleixner 	c->x86_clflush_size = 32;
151713c6c532SJan Beulich 	c->x86_phys_bits = 32;
151813c6c532SJan Beulich 	c->x86_virt_bits = 32;
1519102bbe3aSYinghai Lu #endif
1520102bbe3aSYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
15210e96f31eSJordan Borgner 	memset(&c->x86_capability, 0, sizeof(c->x86_capability));
1522b47ce1feSSean Christopherson #ifdef CONFIG_X86_VMX_FEATURE_NAMES
1523b47ce1feSSean Christopherson 	memset(&c->vmx_capability, 0, sizeof(c->vmx_capability));
1524b47ce1feSSean Christopherson #endif
1525f7627e25SThomas Gleixner 
1526f7627e25SThomas Gleixner 	generic_identify(c);
1527f7627e25SThomas Gleixner 
15283898534dSAndi Kleen 	if (this_cpu->c_identify)
1529f7627e25SThomas Gleixner 		this_cpu->c_identify(c);
1530f7627e25SThomas Gleixner 
15316a6256f9SAdam Buchbinder 	/* Clear/Set all flags overridden by options, after probe */
15328bf1ebcaSAndy Lutomirski 	apply_forced_caps(c);
15332759c328SYinghai Lu 
1534102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
1535cb8cc442SIngo Molnar 	c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1536102bbe3aSYinghai Lu #endif
1537102bbe3aSYinghai Lu 
1538f7627e25SThomas Gleixner 	/*
1539f7627e25SThomas Gleixner 	 * Vendor-specific initialization.  In this section we
1540f7627e25SThomas Gleixner 	 * canonicalize the feature flags, meaning if there are
1541f7627e25SThomas Gleixner 	 * features a certain CPU supports which CPUID doesn't
1542f7627e25SThomas Gleixner 	 * tell us, CPUID claiming incorrect flags, or other bugs,
1543f7627e25SThomas Gleixner 	 * we handle them here.
1544f7627e25SThomas Gleixner 	 *
1545f7627e25SThomas Gleixner 	 * At the end of this section, c->x86_capability better
1546f7627e25SThomas Gleixner 	 * indicate the features this CPU genuinely supports!
1547f7627e25SThomas Gleixner 	 */
1548f7627e25SThomas Gleixner 	if (this_cpu->c_init)
1549f7627e25SThomas Gleixner 		this_cpu->c_init(c);
1550f7627e25SThomas Gleixner 
1551f7627e25SThomas Gleixner 	/* Disable the PN if appropriate */
1552f7627e25SThomas Gleixner 	squash_the_stupid_serial_number(c);
1553f7627e25SThomas Gleixner 
1554aa35f896SRicardo Neri 	/* Set up SMEP/SMAP/UMIP */
1555b2cc2a07SH. Peter Anvin 	setup_smep(c);
1556b2cc2a07SH. Peter Anvin 	setup_smap(c);
1557aa35f896SRicardo Neri 	setup_umip(c);
1558b2cc2a07SH. Peter Anvin 
1559dd649bd0SAndy Lutomirski 	/* Enable FSGSBASE instructions if available. */
1560742c45c3SAndi Kleen 	if (cpu_has(c, X86_FEATURE_FSGSBASE)) {
1561dd649bd0SAndy Lutomirski 		cr4_set_bits(X86_CR4_FSGSBASE);
1562742c45c3SAndi Kleen 		elf_hwcap2 |= HWCAP2_FSGSBASE;
1563742c45c3SAndi Kleen 	}
1564dd649bd0SAndy Lutomirski 
1565f7627e25SThomas Gleixner 	/*
15660f3fa48aSIngo Molnar 	 * The vendor-specific functions might have changed features.
15670f3fa48aSIngo Molnar 	 * Now we do "generic changes."
1568f7627e25SThomas Gleixner 	 */
1569f7627e25SThomas Gleixner 
1570b38b0665SH. Peter Anvin 	/* Filter out anything that depends on CPUID levels we don't have */
1571b38b0665SH. Peter Anvin 	filter_cpuid_features(c, true);
1572b38b0665SH. Peter Anvin 
1573f7627e25SThomas Gleixner 	/* If the model name is still unset, do table lookup. */
1574f7627e25SThomas Gleixner 	if (!c->x86_model_id[0]) {
157502dde8b4SJan Beulich 		const char *p;
1576f7627e25SThomas Gleixner 		p = table_lookup_model(c);
1577f7627e25SThomas Gleixner 		if (p)
1578f7627e25SThomas Gleixner 			strcpy(c->x86_model_id, p);
1579f7627e25SThomas Gleixner 		else
1580f7627e25SThomas Gleixner 			/* Last resort... */
1581f7627e25SThomas Gleixner 			sprintf(c->x86_model_id, "%02x/%02x",
1582f7627e25SThomas Gleixner 				c->x86, c->x86_model);
1583f7627e25SThomas Gleixner 	}
1584f7627e25SThomas Gleixner 
1585102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
1586102bbe3aSYinghai Lu 	detect_ht(c);
1587102bbe3aSYinghai Lu #endif
1588102bbe3aSYinghai Lu 
158949d859d7SH. Peter Anvin 	x86_init_rdrand(c);
159006976945SDave Hansen 	setup_pku(c);
15913e0c3737SYinghai Lu 
15923e0c3737SYinghai Lu 	/*
15936a6256f9SAdam Buchbinder 	 * Clear/Set all flags overridden by options, need do it
15943e0c3737SYinghai Lu 	 * before following smp all cpus cap AND.
15953e0c3737SYinghai Lu 	 */
15968bf1ebcaSAndy Lutomirski 	apply_forced_caps(c);
15973e0c3737SYinghai Lu 
1598f7627e25SThomas Gleixner 	/*
1599f7627e25SThomas Gleixner 	 * On SMP, boot_cpu_data holds the common feature set between
1600f7627e25SThomas Gleixner 	 * all CPUs; so make sure that we indicate which features are
1601f7627e25SThomas Gleixner 	 * common between the CPUs.  The first time this routine gets
1602f7627e25SThomas Gleixner 	 * executed, c == &boot_cpu_data.
1603f7627e25SThomas Gleixner 	 */
1604f7627e25SThomas Gleixner 	if (c != &boot_cpu_data) {
1605f7627e25SThomas Gleixner 		/* AND the already accumulated flags with these */
1606f7627e25SThomas Gleixner 		for (i = 0; i < NCAPINTS; i++)
1607f7627e25SThomas Gleixner 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
160865fc985bSBorislav Petkov 
160965fc985bSBorislav Petkov 		/* OR, i.e. replicate the bug flags */
161065fc985bSBorislav Petkov 		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
161165fc985bSBorislav Petkov 			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
1612f7627e25SThomas Gleixner 	}
1613f7627e25SThomas Gleixner 
1614f7627e25SThomas Gleixner 	/* Init Machine Check Exception if available. */
16155e09954aSBorislav Petkov 	mcheck_cpu_init(c);
161630d432dfSAndi Kleen 
161730d432dfSAndi Kleen 	select_idle_routine(c);
1618102bbe3aSYinghai Lu 
1619de2d9445STejun Heo #ifdef CONFIG_NUMA
1620102bbe3aSYinghai Lu 	numa_add_cpu(smp_processor_id());
1621102bbe3aSYinghai Lu #endif
1622f7627e25SThomas Gleixner }
1623f7627e25SThomas Gleixner 
16248b6c0ab1SIngo Molnar /*
16258b6c0ab1SIngo Molnar  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
16268b6c0ab1SIngo Molnar  * on 32-bit kernels:
16278b6c0ab1SIngo Molnar  */
1628cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32
1629cfda7bb9SAndy Lutomirski void enable_sep_cpu(void)
1630cfda7bb9SAndy Lutomirski {
16318b6c0ab1SIngo Molnar 	struct tss_struct *tss;
16328b6c0ab1SIngo Molnar 	int cpu;
1633cfda7bb9SAndy Lutomirski 
1634b3edfda4SBorislav Petkov 	if (!boot_cpu_has(X86_FEATURE_SEP))
1635b3edfda4SBorislav Petkov 		return;
1636b3edfda4SBorislav Petkov 
16378b6c0ab1SIngo Molnar 	cpu = get_cpu();
1638c482feefSAndy Lutomirski 	tss = &per_cpu(cpu_tss_rw, cpu);
16398b6c0ab1SIngo Molnar 
16408b6c0ab1SIngo Molnar 	/*
1641cf9328ccSAndy Lutomirski 	 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1642cf9328ccSAndy Lutomirski 	 * see the big comment in struct x86_hw_tss's definition.
16438b6c0ab1SIngo Molnar 	 */
1644cfda7bb9SAndy Lutomirski 
1645cfda7bb9SAndy Lutomirski 	tss->x86_tss.ss1 = __KERNEL_CS;
16468b6c0ab1SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
16474fe2d8b1SDave Hansen 	wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0);
16484c8cd0c5SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
16498b6c0ab1SIngo Molnar 
1650cfda7bb9SAndy Lutomirski 	put_cpu();
1651cfda7bb9SAndy Lutomirski }
1652e04d645fSGlauber Costa #endif
1653e04d645fSGlauber Costa 
1654f7627e25SThomas Gleixner void __init identify_boot_cpu(void)
1655f7627e25SThomas Gleixner {
1656f7627e25SThomas Gleixner 	identify_cpu(&boot_cpu_data);
1657102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1658f7627e25SThomas Gleixner 	sysenter_setup();
1659f7627e25SThomas Gleixner 	enable_sep_cpu();
1660102bbe3aSYinghai Lu #endif
1661e0ba94f1SAlex Shi 	cpu_detect_tlb(&boot_cpu_data);
1662873d50d5SKees Cook 	setup_cr_pinning();
166395c5824fSPawan Gupta 
166495c5824fSPawan Gupta 	tsx_init();
1665f7627e25SThomas Gleixner }
1666f7627e25SThomas Gleixner 
1667148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c)
1668f7627e25SThomas Gleixner {
1669f7627e25SThomas Gleixner 	BUG_ON(c == &boot_cpu_data);
1670f7627e25SThomas Gleixner 	identify_cpu(c);
1671102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1672f7627e25SThomas Gleixner 	enable_sep_cpu();
1673102bbe3aSYinghai Lu #endif
1674f7627e25SThomas Gleixner 	mtrr_ap_init();
16759d85eb91SThomas Gleixner 	validate_apic_and_package_id(c);
167677243971SKonrad Rzeszutek Wilk 	x86_spec_ctrl_setup_ap();
16777e5b3c26SMark Gross 	update_srbds_msr();
1678f7627e25SThomas Gleixner }
1679f7627e25SThomas Gleixner 
1680191679fdSAndi Kleen static __init int setup_noclflush(char *arg)
1681191679fdSAndi Kleen {
1682840d2830SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1683da4aaa7dSH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1684191679fdSAndi Kleen 	return 1;
1685191679fdSAndi Kleen }
1686191679fdSAndi Kleen __setup("noclflush", setup_noclflush);
1687191679fdSAndi Kleen 
1688148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c)
1689f7627e25SThomas Gleixner {
169002dde8b4SJan Beulich 	const char *vendor = NULL;
1691f7627e25SThomas Gleixner 
16920f3fa48aSIngo Molnar 	if (c->x86_vendor < X86_VENDOR_NUM) {
1693f7627e25SThomas Gleixner 		vendor = this_cpu->c_vendor;
16940f3fa48aSIngo Molnar 	} else {
16950f3fa48aSIngo Molnar 		if (c->cpuid_level >= 0)
1696f7627e25SThomas Gleixner 			vendor = c->x86_vendor_id;
16970f3fa48aSIngo Molnar 	}
1698f7627e25SThomas Gleixner 
1699bd32a8cfSYinghai Lu 	if (vendor && !strstr(c->x86_model_id, vendor))
17001b74dde7SChen Yucong 		pr_cont("%s ", vendor);
1701f7627e25SThomas Gleixner 
17029d31d35bSYinghai Lu 	if (c->x86_model_id[0])
17031b74dde7SChen Yucong 		pr_cont("%s", c->x86_model_id);
1704f7627e25SThomas Gleixner 	else
17051b74dde7SChen Yucong 		pr_cont("%d86", c->x86);
1706f7627e25SThomas Gleixner 
17071b74dde7SChen Yucong 	pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
1708924e101aSBorislav Petkov 
1709b399151cSJia Zhang 	if (c->x86_stepping || c->cpuid_level >= 0)
1710b399151cSJia Zhang 		pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
1711f7627e25SThomas Gleixner 	else
17121b74dde7SChen Yucong 		pr_cont(")\n");
1713f7627e25SThomas Gleixner }
1714f7627e25SThomas Gleixner 
17150c2a3913SAndi Kleen /*
1716ce38f038SThomas Gleixner  * clearcpuid= was already parsed in cpu_parse_early_param().  This dummy
1717ce38f038SThomas Gleixner  * function prevents it from becoming an environment variable for init.
17180c2a3913SAndi Kleen  */
17190c2a3913SAndi Kleen static __init int setup_clearcpuid(char *arg)
1720ac72e788SAndi Kleen {
1721ac72e788SAndi Kleen 	return 1;
1722ac72e788SAndi Kleen }
17230c2a3913SAndi Kleen __setup("clearcpuid=", setup_clearcpuid);
1724ac72e788SAndi Kleen 
1725d5494d4fSYinghai Lu #ifdef CONFIG_X86_64
1726e6401c13SAndy Lutomirski DEFINE_PER_CPU_FIRST(struct fixed_percpu_data,
1727e6401c13SAndy Lutomirski 		     fixed_percpu_data) __aligned(PAGE_SIZE) __visible;
1728e6401c13SAndy Lutomirski EXPORT_PER_CPU_SYMBOL_GPL(fixed_percpu_data);
17290f3fa48aSIngo Molnar 
1730bdf977b3STejun Heo /*
1731a7fcf28dSAndy Lutomirski  * The following percpu variables are hot.  Align current_task to
1732a7fcf28dSAndy Lutomirski  * cacheline size such that they fall in the same cacheline.
1733bdf977b3STejun Heo  */
1734bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1735bdf977b3STejun Heo 	&init_task;
1736bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1737d5494d4fSYinghai Lu 
1738951c2a51SThomas Gleixner DEFINE_PER_CPU(void *, hardirq_stack_ptr);
1739e7f89001SThomas Gleixner DEFINE_PER_CPU(bool, hardirq_stack_inuse);
1740d5494d4fSYinghai Lu 
1741c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1742c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1743c2daa3beSPeter Zijlstra 
17441591584eSLai Jiangshan DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) = TOP_OF_INIT_STACK;
17451591584eSLai Jiangshan 
1746d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */
1747d5494d4fSYinghai Lu void syscall_init(void)
1748d5494d4fSYinghai Lu {
174931ac34caSBorislav Petkov 	wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
17508d4b0678SThomas Gleixner 	wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
1751d56fe4bfSIngo Molnar 
1752d56fe4bfSIngo Molnar #ifdef CONFIG_IA32_EMULATION
175347edb651SAndy Lutomirski 	wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
1754a76c7f46SDenys Vlasenko 	/*
1755487d1edbSDenys Vlasenko 	 * This only works on Intel CPUs.
1756487d1edbSDenys Vlasenko 	 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1757487d1edbSDenys Vlasenko 	 * This does not cause SYSENTER to jump to the wrong location, because
1758487d1edbSDenys Vlasenko 	 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1759a76c7f46SDenys Vlasenko 	 */
1760a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
17618e6b65a1Szhong jiang 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
17628e6b65a1Szhong jiang 		    (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1));
17634c8cd0c5SIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
1764d56fe4bfSIngo Molnar #else
176547edb651SAndy Lutomirski 	wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
17666b51311cSBorislav Petkov 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1767d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1768d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1769d5494d4fSYinghai Lu #endif
1770d5494d4fSYinghai Lu 
1771d5494d4fSYinghai Lu 	/* Flags to clear on syscall */
1772d5494d4fSYinghai Lu 	wrmsrl(MSR_SYSCALL_MASK,
177363bcff2aSH. Peter Anvin 	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
17748c7aa698SAndy Lutomirski 	       X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1775d5494d4fSYinghai Lu }
1776d5494d4fSYinghai Lu 
17770f3fa48aSIngo Molnar #else	/* CONFIG_X86_64 */
1778d5494d4fSYinghai Lu 
1779bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1780bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1781c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1782c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1783bdf977b3STejun Heo 
1784a7fcf28dSAndy Lutomirski /*
1785a7fcf28dSAndy Lutomirski  * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1786a7fcf28dSAndy Lutomirski  * the top of the kernel stack.  Use an extra percpu variable to track the
1787a7fcf28dSAndy Lutomirski  * top of the kernel stack directly.
1788a7fcf28dSAndy Lutomirski  */
1789a7fcf28dSAndy Lutomirski DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1790a7fcf28dSAndy Lutomirski 	(unsigned long)&init_thread_union + THREAD_SIZE;
1791a7fcf28dSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1792a7fcf28dSAndy Lutomirski 
1793050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR
17943fb0fdb3SAndy Lutomirski DEFINE_PER_CPU(unsigned long, __stack_chk_guard);
17953fb0fdb3SAndy Lutomirski EXPORT_PER_CPU_SYMBOL(__stack_chk_guard);
179660a5317fSTejun Heo #endif
179760a5317fSTejun Heo 
17980f3fa48aSIngo Molnar #endif	/* CONFIG_X86_64 */
1799f7627e25SThomas Gleixner 
1800f7627e25SThomas Gleixner /*
18019766cdbcSJaswinder Singh Rajput  * Clear all 6 debug registers:
18029766cdbcSJaswinder Singh Rajput  */
18039766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void)
18049766cdbcSJaswinder Singh Rajput {
18059766cdbcSJaswinder Singh Rajput 	int i;
18069766cdbcSJaswinder Singh Rajput 
18079766cdbcSJaswinder Singh Rajput 	for (i = 0; i < 8; i++) {
18089766cdbcSJaswinder Singh Rajput 		/* Ignore db4, db5 */
18099766cdbcSJaswinder Singh Rajput 		if ((i == 4) || (i == 5))
18109766cdbcSJaswinder Singh Rajput 			continue;
18119766cdbcSJaswinder Singh Rajput 
18129766cdbcSJaswinder Singh Rajput 		set_debugreg(0, i);
18139766cdbcSJaswinder Singh Rajput 	}
18149766cdbcSJaswinder Singh Rajput }
1815f7627e25SThomas Gleixner 
18160bb9fef9SJason Wessel #ifdef CONFIG_KGDB
18170bb9fef9SJason Wessel /*
18180bb9fef9SJason Wessel  * Restore debug regs if using kgdbwait and you have a kernel debugger
18190bb9fef9SJason Wessel  * connection established.
18200bb9fef9SJason Wessel  */
18210bb9fef9SJason Wessel static void dbg_restore_debug_regs(void)
18220bb9fef9SJason Wessel {
18230bb9fef9SJason Wessel 	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
18240bb9fef9SJason Wessel 		arch_kgdb_ops.correct_hw_break();
18250bb9fef9SJason Wessel }
18260bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */
18270bb9fef9SJason Wessel #define dbg_restore_debug_regs()
18280bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */
18290bb9fef9SJason Wessel 
1830ce4b1b16SIgor Mammedov static void wait_for_master_cpu(int cpu)
1831ce4b1b16SIgor Mammedov {
1832ce4b1b16SIgor Mammedov #ifdef CONFIG_SMP
1833ce4b1b16SIgor Mammedov 	/*
1834ce4b1b16SIgor Mammedov 	 * wait for ACK from master CPU before continuing
1835ce4b1b16SIgor Mammedov 	 * with AP initialization
1836ce4b1b16SIgor Mammedov 	 */
1837ce4b1b16SIgor Mammedov 	WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1838ce4b1b16SIgor Mammedov 	while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1839ce4b1b16SIgor Mammedov 		cpu_relax();
1840ce4b1b16SIgor Mammedov #endif
1841ce4b1b16SIgor Mammedov }
1842ce4b1b16SIgor Mammedov 
1843b2e2ba57SChang S. Bae #ifdef CONFIG_X86_64
1844505b7899SThomas Gleixner static inline void setup_getcpu(int cpu)
1845b2e2ba57SChang S. Bae {
184622245bdfSIngo Molnar 	unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
1847b2e2ba57SChang S. Bae 	struct desc_struct d = { };
1848b2e2ba57SChang S. Bae 
1849b6b4fbd9SSean Christopherson 	if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID))
1850fc48a6d1SSean Christopherson 		wrmsr(MSR_TSC_AUX, cpudata, 0);
1851b2e2ba57SChang S. Bae 
1852b2e2ba57SChang S. Bae 	/* Store CPU and node number in limit. */
1853b2e2ba57SChang S. Bae 	d.limit0 = cpudata;
1854b2e2ba57SChang S. Bae 	d.limit1 = cpudata >> 16;
1855b2e2ba57SChang S. Bae 
1856b2e2ba57SChang S. Bae 	d.type = 5;		/* RO data, expand down, accessed */
1857b2e2ba57SChang S. Bae 	d.dpl = 3;		/* Visible to user code */
1858b2e2ba57SChang S. Bae 	d.s = 1;		/* Not a system segment */
1859b2e2ba57SChang S. Bae 	d.p = 1;		/* Present */
1860b2e2ba57SChang S. Bae 	d.d = 1;		/* 32-bit */
1861b2e2ba57SChang S. Bae 
186222245bdfSIngo Molnar 	write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S);
1863b2e2ba57SChang S. Bae }
1864505b7899SThomas Gleixner 
1865505b7899SThomas Gleixner static inline void ucode_cpu_init(int cpu)
1866505b7899SThomas Gleixner {
1867505b7899SThomas Gleixner 	if (cpu)
1868505b7899SThomas Gleixner 		load_ucode_ap();
1869505b7899SThomas Gleixner }
1870505b7899SThomas Gleixner 
1871505b7899SThomas Gleixner static inline void tss_setup_ist(struct tss_struct *tss)
1872505b7899SThomas Gleixner {
1873505b7899SThomas Gleixner 	/* Set up the per-CPU TSS IST stacks */
1874505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
1875505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
1876505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
1877505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
187802772fb9SJoerg Roedel 	/* Only mapped when SEV-ES is active */
187902772fb9SJoerg Roedel 	tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC);
1880505b7899SThomas Gleixner }
1881505b7899SThomas Gleixner 
1882505b7899SThomas Gleixner #else /* CONFIG_X86_64 */
1883505b7899SThomas Gleixner 
1884505b7899SThomas Gleixner static inline void setup_getcpu(int cpu) { }
1885505b7899SThomas Gleixner 
1886505b7899SThomas Gleixner static inline void ucode_cpu_init(int cpu)
1887505b7899SThomas Gleixner {
1888505b7899SThomas Gleixner 	show_ucode_info_early();
1889505b7899SThomas Gleixner }
1890505b7899SThomas Gleixner 
1891505b7899SThomas Gleixner static inline void tss_setup_ist(struct tss_struct *tss) { }
1892505b7899SThomas Gleixner 
1893505b7899SThomas Gleixner #endif /* !CONFIG_X86_64 */
1894b2e2ba57SChang S. Bae 
1895111e7b15SThomas Gleixner static inline void tss_setup_io_bitmap(struct tss_struct *tss)
1896111e7b15SThomas Gleixner {
1897111e7b15SThomas Gleixner 	tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID;
1898111e7b15SThomas Gleixner 
1899111e7b15SThomas Gleixner #ifdef CONFIG_X86_IOPL_IOPERM
1900111e7b15SThomas Gleixner 	tss->io_bitmap.prev_max = 0;
1901111e7b15SThomas Gleixner 	tss->io_bitmap.prev_sequence = 0;
1902111e7b15SThomas Gleixner 	memset(tss->io_bitmap.bitmap, 0xff, sizeof(tss->io_bitmap.bitmap));
1903111e7b15SThomas Gleixner 	/*
1904111e7b15SThomas Gleixner 	 * Invalidate the extra array entry past the end of the all
1905111e7b15SThomas Gleixner 	 * permission bitmap as required by the hardware.
1906111e7b15SThomas Gleixner 	 */
1907111e7b15SThomas Gleixner 	tss->io_bitmap.mapall[IO_BITMAP_LONGS] = ~0UL;
1908111e7b15SThomas Gleixner #endif
1909111e7b15SThomas Gleixner }
1910f7627e25SThomas Gleixner 
1911f7627e25SThomas Gleixner /*
1912520d0308SJoerg Roedel  * Setup everything needed to handle exceptions from the IDT, including the IST
1913520d0308SJoerg Roedel  * exceptions which use paranoid_entry().
1914520d0308SJoerg Roedel  */
1915520d0308SJoerg Roedel void cpu_init_exception_handling(void)
1916520d0308SJoerg Roedel {
1917520d0308SJoerg Roedel 	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
1918520d0308SJoerg Roedel 	int cpu = raw_smp_processor_id();
1919520d0308SJoerg Roedel 
1920520d0308SJoerg Roedel 	/* paranoid_entry() gets the CPU number from the GDT */
1921520d0308SJoerg Roedel 	setup_getcpu(cpu);
1922520d0308SJoerg Roedel 
1923520d0308SJoerg Roedel 	/* IST vectors need TSS to be set up. */
1924520d0308SJoerg Roedel 	tss_setup_ist(tss);
1925520d0308SJoerg Roedel 	tss_setup_io_bitmap(tss);
1926520d0308SJoerg Roedel 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
1927520d0308SJoerg Roedel 
1928520d0308SJoerg Roedel 	load_TR_desc();
1929520d0308SJoerg Roedel 
1930520d0308SJoerg Roedel 	/* Finally load the IDT */
1931520d0308SJoerg Roedel 	load_current_idt();
1932520d0308SJoerg Roedel }
1933520d0308SJoerg Roedel 
1934520d0308SJoerg Roedel /*
1935f7627e25SThomas Gleixner  * cpu_init() initializes state that is per-CPU. Some data is already
1936f7627e25SThomas Gleixner  * initialized (naturally) in the bootstrap process, such as the GDT
1937f7627e25SThomas Gleixner  * and IDT. We reload them nevertheless, this function acts as a
1938f7627e25SThomas Gleixner  * 'CPU state barrier', nothing should get across.
1939f7627e25SThomas Gleixner  */
1940148f9bb8SPaul Gortmaker void cpu_init(void)
19411ba76586SYinghai Lu {
1942505b7899SThomas Gleixner 	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
1943505b7899SThomas Gleixner 	struct task_struct *cur = current;
1944f6ef7322SThomas Gleixner 	int cpu = raw_smp_processor_id();
19451ba76586SYinghai Lu 
1946ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1947ce4b1b16SIgor Mammedov 
1948505b7899SThomas Gleixner 	ucode_cpu_init(cpu);
19490f3fa48aSIngo Molnar 
1950e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA
195127fd185fSFenghua Yu 	if (this_cpu_read(numa_node) == 0 &&
1952e534c7c5SLee Schermerhorn 	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
1953e534c7c5SLee Schermerhorn 		set_numa_node(early_cpu_to_node(cpu));
1954e7a22c1eSBrian Gerst #endif
1955b2e2ba57SChang S. Bae 	setup_getcpu(cpu);
19561ba76586SYinghai Lu 
19572eaad1fdSMike Travis 	pr_debug("Initializing CPU#%d\n", cpu);
19581ba76586SYinghai Lu 
1959505b7899SThomas Gleixner 	if (IS_ENABLED(CONFIG_X86_64) || cpu_feature_enabled(X86_FEATURE_VME) ||
1960505b7899SThomas Gleixner 	    boot_cpu_has(X86_FEATURE_TSC) || boot_cpu_has(X86_FEATURE_DE))
1961375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
19621ba76586SYinghai Lu 
19631ba76586SYinghai Lu 	/*
19641ba76586SYinghai Lu 	 * Initialize the per-CPU GDT with the boot GDT,
19651ba76586SYinghai Lu 	 * and set up the GDT descriptor:
19661ba76586SYinghai Lu 	 */
1967552be871SBrian Gerst 	switch_to_new_gdt(cpu);
1968cf910e83SSeiji Aguchi 	load_current_idt();
19691ba76586SYinghai Lu 
1970505b7899SThomas Gleixner 	if (IS_ENABLED(CONFIG_X86_64)) {
1971505b7899SThomas Gleixner 		loadsegment(fs, 0);
1972505b7899SThomas Gleixner 		memset(cur->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
19731ba76586SYinghai Lu 		syscall_init();
19741ba76586SYinghai Lu 
19751ba76586SYinghai Lu 		wrmsrl(MSR_FS_BASE, 0);
19761ba76586SYinghai Lu 		wrmsrl(MSR_KERNEL_GS_BASE, 0);
19771ba76586SYinghai Lu 		barrier();
19781ba76586SYinghai Lu 
1979659006bfSThomas Gleixner 		x2apic_setup();
19801ba76586SYinghai Lu 	}
19811ba76586SYinghai Lu 
1982f1f10076SVegard Nossum 	mmgrab(&init_mm);
1983505b7899SThomas Gleixner 	cur->active_mm = &init_mm;
1984505b7899SThomas Gleixner 	BUG_ON(cur->mm);
198572c0098dSAndy Lutomirski 	initialize_tlbstate_and_flush();
1986505b7899SThomas Gleixner 	enter_lazy_tlb(&init_mm, cur);
19871ba76586SYinghai Lu 
1988505b7899SThomas Gleixner 	/* Initialize the TSS. */
1989505b7899SThomas Gleixner 	tss_setup_ist(tss);
1990111e7b15SThomas Gleixner 	tss_setup_io_bitmap(tss);
199172f5e08dSAndy Lutomirski 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
1992505b7899SThomas Gleixner 
19931ba76586SYinghai Lu 	load_TR_desc();
1994505b7899SThomas Gleixner 	/*
1995505b7899SThomas Gleixner 	 * sp0 points to the entry trampoline stack regardless of what task
1996505b7899SThomas Gleixner 	 * is running.
1997505b7899SThomas Gleixner 	 */
19984fe2d8b1SDave Hansen 	load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
199920bb8344SAndy Lutomirski 
200037868fe1SAndy Lutomirski 	load_mm_ldt(&init_mm);
20011ba76586SYinghai Lu 
20029766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
20030bb9fef9SJason Wessel 	dbg_restore_debug_regs();
20041ba76586SYinghai Lu 
2005dc4e0021SAndy Lutomirski 	doublefault_init_cpu_tss();
2006505b7899SThomas Gleixner 
200721c4cd10SIngo Molnar 	fpu__init_cpu();
20081ba76586SYinghai Lu 
20091ba76586SYinghai Lu 	if (is_uv_system())
20101ba76586SYinghai Lu 		uv_cpu_init();
201169218e47SThomas Garnier 
201269218e47SThomas Garnier 	load_fixmap_gdt(cpu);
20131ba76586SYinghai Lu }
20141ba76586SYinghai Lu 
20151008c52cSBorislav Petkov /*
20161008c52cSBorislav Petkov  * The microcode loader calls this upon late microcode load to recheck features,
20171008c52cSBorislav Petkov  * only when microcode has been updated. Caller holds microcode_mutex and CPU
20181008c52cSBorislav Petkov  * hotplug lock.
20191008c52cSBorislav Petkov  */
20201008c52cSBorislav Petkov void microcode_check(void)
20211008c52cSBorislav Petkov {
202242ca8082SBorislav Petkov 	struct cpuinfo_x86 info;
202342ca8082SBorislav Petkov 
20241008c52cSBorislav Petkov 	perf_check_microcode();
202542ca8082SBorislav Petkov 
202642ca8082SBorislav Petkov 	/* Reload CPUID max function as it might've changed. */
202742ca8082SBorislav Petkov 	info.cpuid_level = cpuid_eax(0);
202842ca8082SBorislav Petkov 
202942ca8082SBorislav Petkov 	/*
203042ca8082SBorislav Petkov 	 * Copy all capability leafs to pick up the synthetic ones so that
203142ca8082SBorislav Petkov 	 * memcmp() below doesn't fail on that. The ones coming from CPUID will
203242ca8082SBorislav Petkov 	 * get overwritten in get_cpu_cap().
203342ca8082SBorislav Petkov 	 */
203442ca8082SBorislav Petkov 	memcpy(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability));
203542ca8082SBorislav Petkov 
203642ca8082SBorislav Petkov 	get_cpu_cap(&info);
203742ca8082SBorislav Petkov 
203842ca8082SBorislav Petkov 	if (!memcmp(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability)))
203942ca8082SBorislav Petkov 		return;
204042ca8082SBorislav Petkov 
204142ca8082SBorislav Petkov 	pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
204242ca8082SBorislav Petkov 	pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
20431008c52cSBorislav Petkov }
20449c92374bSThomas Gleixner 
20459c92374bSThomas Gleixner /*
20469c92374bSThomas Gleixner  * Invoked from core CPU hotplug code after hotplug operations
20479c92374bSThomas Gleixner  */
20489c92374bSThomas Gleixner void arch_smt_update(void)
20499c92374bSThomas Gleixner {
20509c92374bSThomas Gleixner 	/* Handle the speculative execution misfeatures */
20519c92374bSThomas Gleixner 	cpu_bugs_smt_update();
20526a1cb5f5SThomas Gleixner 	/* Check whether IPI broadcasting can be enabled */
20536a1cb5f5SThomas Gleixner 	apic_smt_update();
20549c92374bSThomas Gleixner }
2055