xref: /linux/arch/x86/kernel/cpu/common.c (revision 3fb0fdb3bbe7aed495109b3296b06c2409734023)
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>
61f7627e25SThomas Gleixner 
62f7627e25SThomas Gleixner #include "cpu.h"
63f7627e25SThomas Gleixner 
640274f955SGrzegorz Andrejczuk u32 elf_hwcap2 __read_mostly;
650274f955SGrzegorz Andrejczuk 
66c2d1cec1SMike Travis /* all of these masks are initialized in setup_cpu_local_masks() */
67c2d1cec1SMike Travis cpumask_var_t cpu_initialized_mask;
689766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callout_mask;
699766cdbcSJaswinder Singh Rajput cpumask_var_t cpu_callin_mask;
70c2d1cec1SMike Travis 
71c2d1cec1SMike Travis /* representing cpus for which sibling maps can be computed */
72c2d1cec1SMike Travis cpumask_var_t cpu_sibling_setup_mask;
73c2d1cec1SMike Travis 
74f8b64d08SBorislav Petkov /* Number of siblings per CPU package */
75f8b64d08SBorislav Petkov int smp_num_siblings = 1;
76f8b64d08SBorislav Petkov EXPORT_SYMBOL(smp_num_siblings);
77f8b64d08SBorislav Petkov 
78f8b64d08SBorislav Petkov /* Last level cache ID of each logical CPU */
79f8b64d08SBorislav Petkov DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
80f8b64d08SBorislav Petkov 
812f2f52baSBrian Gerst /* correctly size the local cpu masks */
824369f1fbSIngo Molnar void __init setup_cpu_local_masks(void)
832f2f52baSBrian Gerst {
842f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_initialized_mask);
852f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callin_mask);
862f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_callout_mask);
872f2f52baSBrian Gerst 	alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
882f2f52baSBrian Gerst }
892f2f52baSBrian Gerst 
90148f9bb8SPaul Gortmaker static void default_init(struct cpuinfo_x86 *c)
91e8055139SOndrej Zary {
92e8055139SOndrej Zary #ifdef CONFIG_X86_64
9327c13eceSBorislav Petkov 	cpu_detect_cache_sizes(c);
94e8055139SOndrej Zary #else
95e8055139SOndrej Zary 	/* Not much we can do here... */
96e8055139SOndrej Zary 	/* Check if at least it has cpuid */
97e8055139SOndrej Zary 	if (c->cpuid_level == -1) {
98e8055139SOndrej Zary 		/* No cpuid. It must be an ancient CPU */
99e8055139SOndrej Zary 		if (c->x86 == 4)
100e8055139SOndrej Zary 			strcpy(c->x86_model_id, "486");
101e8055139SOndrej Zary 		else if (c->x86 == 3)
102e8055139SOndrej Zary 			strcpy(c->x86_model_id, "386");
103e8055139SOndrej Zary 	}
104e8055139SOndrej Zary #endif
105e8055139SOndrej Zary }
106e8055139SOndrej Zary 
107148f9bb8SPaul Gortmaker static const struct cpu_dev default_cpu = {
108e8055139SOndrej Zary 	.c_init		= default_init,
109e8055139SOndrej Zary 	.c_vendor	= "Unknown",
110e8055139SOndrej Zary 	.c_x86_vendor	= X86_VENDOR_UNKNOWN,
111e8055139SOndrej Zary };
112e8055139SOndrej Zary 
113148f9bb8SPaul Gortmaker static const struct cpu_dev *this_cpu = &default_cpu;
1140a488a53SYinghai Lu 
11506deef89SBrian Gerst DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
116950ad7ffSYinghai Lu #ifdef CONFIG_X86_64
11706deef89SBrian Gerst 	/*
11806deef89SBrian Gerst 	 * We need valid kernel segments for data and code in long mode too
119950ad7ffSYinghai Lu 	 * IRET will check the segment types  kkeil 2000/10/28
120950ad7ffSYinghai Lu 	 * Also sysret mandates a special GDT layout
12106deef89SBrian Gerst 	 *
1229766cdbcSJaswinder Singh Rajput 	 * TLS descriptors are currently at a different place compared to i386.
12306deef89SBrian Gerst 	 * Hopefully nobody expects them at a fixed place (Wine?)
124950ad7ffSYinghai Lu 	 */
1251e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL32_CS]		= GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
1261e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
1271e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
1281e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER32_CS]	= GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
1291e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
1301e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
131950ad7ffSYinghai Lu #else
1321e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
1331e5de182SAkinobu Mita 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1341e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
1351e5de182SAkinobu Mita 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
136f7627e25SThomas Gleixner 	/*
137f7627e25SThomas Gleixner 	 * Segments used for calling PnP BIOS have byte granularity.
138f7627e25SThomas Gleixner 	 * They code segments and data segments have fixed 64k limits,
139f7627e25SThomas Gleixner 	 * the transfer segment sizes are set at run time.
140f7627e25SThomas Gleixner 	 */
1416842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1421e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS32]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
1436842ef0eSGlauber de Oliveira Costa 	/* 16-bit code */
1441e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_CS16]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1456842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1461e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_DS]		= GDT_ENTRY_INIT(0x0092, 0, 0xffff),
1476842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1481e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS1]		= GDT_ENTRY_INIT(0x0092, 0, 0),
1496842ef0eSGlauber de Oliveira Costa 	/* 16-bit data */
1501e5de182SAkinobu Mita 	[GDT_ENTRY_PNPBIOS_TS2]		= GDT_ENTRY_INIT(0x0092, 0, 0),
151f7627e25SThomas Gleixner 	/*
152f7627e25SThomas Gleixner 	 * The APM segments have byte granularity and their bases
153f7627e25SThomas Gleixner 	 * are set at run time.  All have 64k limits.
154f7627e25SThomas Gleixner 	 */
1556842ef0eSGlauber de Oliveira Costa 	/* 32-bit code */
1561e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
157f7627e25SThomas Gleixner 	/* 16-bit code */
1581e5de182SAkinobu Mita 	[GDT_ENTRY_APMBIOS_BASE+1]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1596842ef0eSGlauber de Oliveira Costa 	/* data */
16072c4d853SIngo Molnar 	[GDT_ENTRY_APMBIOS_BASE+2]	= GDT_ENTRY_INIT(0x4092, 0, 0xffff),
161f7627e25SThomas Gleixner 
1621e5de182SAkinobu Mita 	[GDT_ENTRY_ESPFIX_SS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1631e5de182SAkinobu Mita 	[GDT_ENTRY_PERCPU]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
164950ad7ffSYinghai Lu #endif
16506deef89SBrian Gerst } };
166f7627e25SThomas Gleixner EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
167f7627e25SThomas Gleixner 
1680790c9aaSAndy Lutomirski #ifdef CONFIG_X86_64
169c7ad5ad2SAndy Lutomirski static int __init x86_nopcid_setup(char *s)
1700790c9aaSAndy Lutomirski {
171c7ad5ad2SAndy Lutomirski 	/* nopcid doesn't accept parameters */
172c7ad5ad2SAndy Lutomirski 	if (s)
173c7ad5ad2SAndy Lutomirski 		return -EINVAL;
1740790c9aaSAndy Lutomirski 
1750790c9aaSAndy Lutomirski 	/* do not emit a message if the feature is not present */
1760790c9aaSAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_PCID))
177c7ad5ad2SAndy Lutomirski 		return 0;
1780790c9aaSAndy Lutomirski 
1790790c9aaSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_PCID);
1800790c9aaSAndy Lutomirski 	pr_info("nopcid: PCID feature disabled\n");
181c7ad5ad2SAndy Lutomirski 	return 0;
1820790c9aaSAndy Lutomirski }
183c7ad5ad2SAndy Lutomirski early_param("nopcid", x86_nopcid_setup);
1840790c9aaSAndy Lutomirski #endif
1850790c9aaSAndy Lutomirski 
186d12a72b8SAndy Lutomirski static int __init x86_noinvpcid_setup(char *s)
187d12a72b8SAndy Lutomirski {
188d12a72b8SAndy Lutomirski 	/* noinvpcid doesn't accept parameters */
189d12a72b8SAndy Lutomirski 	if (s)
190d12a72b8SAndy Lutomirski 		return -EINVAL;
191d12a72b8SAndy Lutomirski 
192d12a72b8SAndy Lutomirski 	/* do not emit a message if the feature is not present */
193d12a72b8SAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_INVPCID))
194d12a72b8SAndy Lutomirski 		return 0;
195d12a72b8SAndy Lutomirski 
196d12a72b8SAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_INVPCID);
197d12a72b8SAndy Lutomirski 	pr_info("noinvpcid: INVPCID feature disabled\n");
198d12a72b8SAndy Lutomirski 	return 0;
199d12a72b8SAndy Lutomirski }
200d12a72b8SAndy Lutomirski early_param("noinvpcid", x86_noinvpcid_setup);
201d12a72b8SAndy Lutomirski 
202ba51dcedSYinghai Lu #ifdef CONFIG_X86_32
203148f9bb8SPaul Gortmaker static int cachesize_override = -1;
204148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1;
205f7627e25SThomas Gleixner 
206f7627e25SThomas Gleixner static int __init cachesize_setup(char *str)
207f7627e25SThomas Gleixner {
208f7627e25SThomas Gleixner 	get_option(&str, &cachesize_override);
209f7627e25SThomas Gleixner 	return 1;
210f7627e25SThomas Gleixner }
211f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup);
212f7627e25SThomas Gleixner 
213f7627e25SThomas Gleixner static int __init x86_sep_setup(char *s)
214f7627e25SThomas Gleixner {
21513530257SAndi Kleen 	setup_clear_cpu_cap(X86_FEATURE_SEP);
216f7627e25SThomas Gleixner 	return 1;
217f7627e25SThomas Gleixner }
218f7627e25SThomas Gleixner __setup("nosep", x86_sep_setup);
219f7627e25SThomas Gleixner 
220f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */
221f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag)
222f7627e25SThomas Gleixner {
223f7627e25SThomas Gleixner 	u32 f1, f2;
224f7627e25SThomas Gleixner 
22594f6bac1SKrzysztof Helt 	/*
22694f6bac1SKrzysztof Helt 	 * Cyrix and IDT cpus allow disabling of CPUID
22794f6bac1SKrzysztof Helt 	 * so the code below may return different results
22894f6bac1SKrzysztof Helt 	 * when it is executed before and after enabling
22994f6bac1SKrzysztof Helt 	 * the CPUID. Add "volatile" to not allow gcc to
23094f6bac1SKrzysztof Helt 	 * optimize the subsequent calls to this function.
23194f6bac1SKrzysztof Helt 	 */
23294f6bac1SKrzysztof Helt 	asm volatile ("pushfl		\n\t"
233f7627e25SThomas Gleixner 		      "pushfl		\n\t"
234f7627e25SThomas Gleixner 		      "popl %0		\n\t"
235f7627e25SThomas Gleixner 		      "movl %0, %1	\n\t"
236f7627e25SThomas Gleixner 		      "xorl %2, %0	\n\t"
237f7627e25SThomas Gleixner 		      "pushl %0		\n\t"
238f7627e25SThomas Gleixner 		      "popfl		\n\t"
239f7627e25SThomas Gleixner 		      "pushfl		\n\t"
240f7627e25SThomas Gleixner 		      "popl %0		\n\t"
241f7627e25SThomas Gleixner 		      "popfl		\n\t"
2420f3fa48aSIngo Molnar 
243f7627e25SThomas Gleixner 		      : "=&r" (f1), "=&r" (f2)
244f7627e25SThomas Gleixner 		      : "ir" (flag));
245f7627e25SThomas Gleixner 
246f7627e25SThomas Gleixner 	return ((f1^f2) & flag) != 0;
247f7627e25SThomas Gleixner }
248f7627e25SThomas Gleixner 
249f7627e25SThomas Gleixner /* Probe for the CPUID instruction */
250148f9bb8SPaul Gortmaker int have_cpuid_p(void)
251f7627e25SThomas Gleixner {
252f7627e25SThomas Gleixner 	return flag_is_changeable_p(X86_EFLAGS_ID);
253f7627e25SThomas Gleixner }
254f7627e25SThomas Gleixner 
255148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
2560a488a53SYinghai Lu {
2570a488a53SYinghai Lu 	unsigned long lo, hi;
2580f3fa48aSIngo Molnar 
2590f3fa48aSIngo Molnar 	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
2600f3fa48aSIngo Molnar 		return;
2610f3fa48aSIngo Molnar 
2620f3fa48aSIngo Molnar 	/* Disable processor serial number: */
2630f3fa48aSIngo Molnar 
2640a488a53SYinghai Lu 	rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2650a488a53SYinghai Lu 	lo |= 0x200000;
2660a488a53SYinghai Lu 	wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2670f3fa48aSIngo Molnar 
2681b74dde7SChen Yucong 	pr_notice("CPU serial number disabled.\n");
2690a488a53SYinghai Lu 	clear_cpu_cap(c, X86_FEATURE_PN);
2700a488a53SYinghai Lu 
2710a488a53SYinghai Lu 	/* Disabling the serial number may affect the cpuid level */
2720a488a53SYinghai Lu 	c->cpuid_level = cpuid_eax(0);
2730a488a53SYinghai Lu }
2740a488a53SYinghai Lu 
2750a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s)
2760a488a53SYinghai Lu {
2770a488a53SYinghai Lu 	disable_x86_serial_nr = 0;
2780a488a53SYinghai Lu 	return 1;
2790a488a53SYinghai Lu }
2800a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup);
281ba51dcedSYinghai Lu #else
282102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag)
283102bbe3aSYinghai Lu {
284102bbe3aSYinghai Lu 	return 1;
285102bbe3aSYinghai Lu }
286102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
287102bbe3aSYinghai Lu {
288102bbe3aSYinghai Lu }
289ba51dcedSYinghai Lu #endif
2900a488a53SYinghai Lu 
291de5397adSFenghua Yu static __init int setup_disable_smep(char *arg)
292de5397adSFenghua Yu {
293b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMEP);
294de5397adSFenghua Yu 	return 1;
295de5397adSFenghua Yu }
296de5397adSFenghua Yu __setup("nosmep", setup_disable_smep);
297de5397adSFenghua Yu 
298b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c)
299de5397adSFenghua Yu {
300b2cc2a07SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMEP))
301375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMEP);
302de5397adSFenghua Yu }
303de5397adSFenghua Yu 
30452b6179aSH. Peter Anvin static __init int setup_disable_smap(char *arg)
30552b6179aSH. Peter Anvin {
306b2cc2a07SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_SMAP);
30752b6179aSH. Peter Anvin 	return 1;
30852b6179aSH. Peter Anvin }
30952b6179aSH. Peter Anvin __setup("nosmap", setup_disable_smap);
31052b6179aSH. Peter Anvin 
311b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c)
31252b6179aSH. Peter Anvin {
313581b7f15SAndrew Cooper 	unsigned long eflags = native_save_fl();
314b2cc2a07SH. Peter Anvin 
315b2cc2a07SH. Peter Anvin 	/* This should have been cleared long ago */
316b2cc2a07SH. Peter Anvin 	BUG_ON(eflags & X86_EFLAGS_AC);
317b2cc2a07SH. Peter Anvin 
31803bbd596SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMAP)) {
31903bbd596SH. Peter Anvin #ifdef CONFIG_X86_SMAP
320375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMAP);
32103bbd596SH. Peter Anvin #else
322375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_SMAP);
32303bbd596SH. Peter Anvin #endif
32403bbd596SH. Peter Anvin 	}
325f7627e25SThomas Gleixner }
326f7627e25SThomas Gleixner 
327aa35f896SRicardo Neri static __always_inline void setup_umip(struct cpuinfo_x86 *c)
328aa35f896SRicardo Neri {
329aa35f896SRicardo Neri 	/* Check the boot processor, plus build option for UMIP. */
330aa35f896SRicardo Neri 	if (!cpu_feature_enabled(X86_FEATURE_UMIP))
331aa35f896SRicardo Neri 		goto out;
332aa35f896SRicardo Neri 
333aa35f896SRicardo Neri 	/* Check the current processor's cpuid bits. */
334aa35f896SRicardo Neri 	if (!cpu_has(c, X86_FEATURE_UMIP))
335aa35f896SRicardo Neri 		goto out;
336aa35f896SRicardo Neri 
337aa35f896SRicardo Neri 	cr4_set_bits(X86_CR4_UMIP);
338aa35f896SRicardo Neri 
339438cbf88SLendacky, Thomas 	pr_info_once("x86/cpu: User Mode Instruction Prevention (UMIP) activated\n");
340770c7755SRicardo Neri 
341aa35f896SRicardo Neri 	return;
342aa35f896SRicardo Neri 
343aa35f896SRicardo Neri out:
344aa35f896SRicardo Neri 	/*
345aa35f896SRicardo Neri 	 * Make sure UMIP is disabled in case it was enabled in a
346aa35f896SRicardo Neri 	 * previous boot (e.g., via kexec).
347aa35f896SRicardo Neri 	 */
348aa35f896SRicardo Neri 	cr4_clear_bits(X86_CR4_UMIP);
349aa35f896SRicardo Neri }
350aa35f896SRicardo Neri 
351a13b9d0bSKees Cook /* These bits should not change their value after CPU init is finished. */
352a13b9d0bSKees Cook static const unsigned long cr4_pinned_mask =
353a13b9d0bSKees Cook 	X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP | X86_CR4_FSGSBASE;
3547652ac92SThomas Gleixner static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
3557652ac92SThomas Gleixner static unsigned long cr4_pinned_bits __ro_after_init;
3567652ac92SThomas Gleixner 
3577652ac92SThomas Gleixner void native_write_cr0(unsigned long val)
3587652ac92SThomas Gleixner {
3597652ac92SThomas Gleixner 	unsigned long bits_missing = 0;
3607652ac92SThomas Gleixner 
3617652ac92SThomas Gleixner set_register:
362aa5cacdcSArvind Sankar 	asm volatile("mov %0,%%cr0": "+r" (val) : : "memory");
3637652ac92SThomas Gleixner 
3647652ac92SThomas Gleixner 	if (static_branch_likely(&cr_pinning)) {
3657652ac92SThomas Gleixner 		if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) {
3667652ac92SThomas Gleixner 			bits_missing = X86_CR0_WP;
3677652ac92SThomas Gleixner 			val |= bits_missing;
3687652ac92SThomas Gleixner 			goto set_register;
3697652ac92SThomas Gleixner 		}
3707652ac92SThomas Gleixner 		/* Warn after we've set the missing bits. */
3717652ac92SThomas Gleixner 		WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n");
3727652ac92SThomas Gleixner 	}
3737652ac92SThomas Gleixner }
3747652ac92SThomas Gleixner EXPORT_SYMBOL(native_write_cr0);
3757652ac92SThomas Gleixner 
3767652ac92SThomas Gleixner void native_write_cr4(unsigned long val)
3777652ac92SThomas Gleixner {
378a13b9d0bSKees Cook 	unsigned long bits_changed = 0;
3797652ac92SThomas Gleixner 
3807652ac92SThomas Gleixner set_register:
381aa5cacdcSArvind Sankar 	asm volatile("mov %0,%%cr4": "+r" (val) : : "memory");
3827652ac92SThomas Gleixner 
3837652ac92SThomas Gleixner 	if (static_branch_likely(&cr_pinning)) {
384a13b9d0bSKees Cook 		if (unlikely((val & cr4_pinned_mask) != cr4_pinned_bits)) {
385a13b9d0bSKees Cook 			bits_changed = (val & cr4_pinned_mask) ^ cr4_pinned_bits;
386a13b9d0bSKees Cook 			val = (val & ~cr4_pinned_mask) | cr4_pinned_bits;
3877652ac92SThomas Gleixner 			goto set_register;
3887652ac92SThomas Gleixner 		}
389a13b9d0bSKees Cook 		/* Warn after we've corrected the changed bits. */
390a13b9d0bSKees Cook 		WARN_ONCE(bits_changed, "pinned CR4 bits changed: 0x%lx!?\n",
391a13b9d0bSKees Cook 			  bits_changed);
3927652ac92SThomas Gleixner 	}
3937652ac92SThomas Gleixner }
39421953ee5SThomas Gleixner #if IS_MODULE(CONFIG_LKDTM)
395d8f0b353SThomas Gleixner EXPORT_SYMBOL_GPL(native_write_cr4);
39621953ee5SThomas Gleixner #endif
397d8f0b353SThomas Gleixner 
398d8f0b353SThomas Gleixner void cr4_update_irqsoff(unsigned long set, unsigned long clear)
399d8f0b353SThomas Gleixner {
400d8f0b353SThomas Gleixner 	unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4);
401d8f0b353SThomas Gleixner 
402d8f0b353SThomas Gleixner 	lockdep_assert_irqs_disabled();
403d8f0b353SThomas Gleixner 
404d8f0b353SThomas Gleixner 	newval = (cr4 & ~clear) | set;
405d8f0b353SThomas Gleixner 	if (newval != cr4) {
406d8f0b353SThomas Gleixner 		this_cpu_write(cpu_tlbstate.cr4, newval);
407d8f0b353SThomas Gleixner 		__write_cr4(newval);
408d8f0b353SThomas Gleixner 	}
409d8f0b353SThomas Gleixner }
410d8f0b353SThomas Gleixner EXPORT_SYMBOL(cr4_update_irqsoff);
411d8f0b353SThomas Gleixner 
412d8f0b353SThomas Gleixner /* Read the CR4 shadow. */
413d8f0b353SThomas Gleixner unsigned long cr4_read_shadow(void)
414d8f0b353SThomas Gleixner {
415d8f0b353SThomas Gleixner 	return this_cpu_read(cpu_tlbstate.cr4);
416d8f0b353SThomas Gleixner }
417d8f0b353SThomas Gleixner EXPORT_SYMBOL_GPL(cr4_read_shadow);
4187652ac92SThomas Gleixner 
4197652ac92SThomas Gleixner void cr4_init(void)
4207652ac92SThomas Gleixner {
4217652ac92SThomas Gleixner 	unsigned long cr4 = __read_cr4();
4227652ac92SThomas Gleixner 
4237652ac92SThomas Gleixner 	if (boot_cpu_has(X86_FEATURE_PCID))
4247652ac92SThomas Gleixner 		cr4 |= X86_CR4_PCIDE;
4257652ac92SThomas Gleixner 	if (static_branch_likely(&cr_pinning))
426a13b9d0bSKees Cook 		cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits;
4277652ac92SThomas Gleixner 
4287652ac92SThomas Gleixner 	__write_cr4(cr4);
4297652ac92SThomas Gleixner 
4307652ac92SThomas Gleixner 	/* Initialize cr4 shadow for this CPU. */
4317652ac92SThomas Gleixner 	this_cpu_write(cpu_tlbstate.cr4, cr4);
4327652ac92SThomas Gleixner }
433873d50d5SKees Cook 
434873d50d5SKees Cook /*
435873d50d5SKees Cook  * Once CPU feature detection is finished (and boot params have been
436873d50d5SKees Cook  * parsed), record any of the sensitive CR bits that are set, and
437873d50d5SKees Cook  * enable CR pinning.
438873d50d5SKees Cook  */
439873d50d5SKees Cook static void __init setup_cr_pinning(void)
440873d50d5SKees Cook {
441a13b9d0bSKees Cook 	cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask;
442873d50d5SKees Cook 	static_key_enable(&cr_pinning.key);
443873d50d5SKees Cook }
444873d50d5SKees Cook 
445b745cfbaSAndy Lutomirski static __init int x86_nofsgsbase_setup(char *arg)
446dd649bd0SAndy Lutomirski {
447b745cfbaSAndy Lutomirski 	/* Require an exact match without trailing characters. */
448b745cfbaSAndy Lutomirski 	if (strlen(arg))
449b745cfbaSAndy Lutomirski 		return 0;
450b745cfbaSAndy Lutomirski 
451b745cfbaSAndy Lutomirski 	/* Do not emit a message if the feature is not present. */
452b745cfbaSAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_FSGSBASE))
453b745cfbaSAndy Lutomirski 		return 1;
454b745cfbaSAndy Lutomirski 
455b745cfbaSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_FSGSBASE);
456b745cfbaSAndy Lutomirski 	pr_info("FSGSBASE disabled via kernel command line\n");
457dd649bd0SAndy Lutomirski 	return 1;
458dd649bd0SAndy Lutomirski }
459b745cfbaSAndy Lutomirski __setup("nofsgsbase", x86_nofsgsbase_setup);
460dd649bd0SAndy Lutomirski 
461b64ed19bSAndy Lutomirski /*
46206976945SDave Hansen  * Protection Keys are not available in 32-bit mode.
46306976945SDave Hansen  */
46406976945SDave Hansen static bool pku_disabled;
46506976945SDave Hansen 
46606976945SDave Hansen static __always_inline void setup_pku(struct cpuinfo_x86 *c)
46706976945SDave Hansen {
468a5eff725SSebastian Andrzej Siewior 	struct pkru_state *pk;
469a5eff725SSebastian Andrzej Siewior 
470e8df1a95SDave Hansen 	/* check the boot processor, plus compile options for PKU: */
471e8df1a95SDave Hansen 	if (!cpu_feature_enabled(X86_FEATURE_PKU))
472e8df1a95SDave Hansen 		return;
473e8df1a95SDave Hansen 	/* checks the actual processor's cpuid bits: */
47406976945SDave Hansen 	if (!cpu_has(c, X86_FEATURE_PKU))
47506976945SDave Hansen 		return;
47606976945SDave Hansen 	if (pku_disabled)
47706976945SDave Hansen 		return;
47806976945SDave Hansen 
47906976945SDave Hansen 	cr4_set_bits(X86_CR4_PKE);
480a5eff725SSebastian Andrzej Siewior 	pk = get_xsave_addr(&init_fpstate.xsave, XFEATURE_PKRU);
481a5eff725SSebastian Andrzej Siewior 	if (pk)
482a5eff725SSebastian Andrzej Siewior 		pk->pkru = init_pkru_value;
48306976945SDave Hansen 	/*
48406976945SDave Hansen 	 * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
48506976945SDave Hansen 	 * cpuid bit to be set.  We need to ensure that we
48606976945SDave Hansen 	 * update that bit in this CPU's "cpu_info".
48706976945SDave Hansen 	 */
488735a6dd0SSean Christopherson 	set_cpu_cap(c, X86_FEATURE_OSPKE);
48906976945SDave Hansen }
49006976945SDave Hansen 
49106976945SDave Hansen #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
49206976945SDave Hansen static __init int setup_disable_pku(char *arg)
49306976945SDave Hansen {
49406976945SDave Hansen 	/*
49506976945SDave Hansen 	 * Do not clear the X86_FEATURE_PKU bit.  All of the
49606976945SDave Hansen 	 * runtime checks are against OSPKE so clearing the
49706976945SDave Hansen 	 * bit does nothing.
49806976945SDave Hansen 	 *
49906976945SDave Hansen 	 * This way, we will see "pku" in cpuinfo, but not
50006976945SDave Hansen 	 * "ospke", which is exactly what we want.  It shows
50106976945SDave Hansen 	 * that the CPU has PKU, but the OS has not enabled it.
50206976945SDave Hansen 	 * This happens to be exactly how a system would look
50306976945SDave Hansen 	 * if we disabled the config option.
50406976945SDave Hansen 	 */
50506976945SDave Hansen 	pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
50606976945SDave Hansen 	pku_disabled = true;
50706976945SDave Hansen 	return 1;
50806976945SDave Hansen }
50906976945SDave Hansen __setup("nopku", setup_disable_pku);
51006976945SDave Hansen #endif /* CONFIG_X86_64 */
51106976945SDave Hansen 
51206976945SDave Hansen /*
513b38b0665SH. Peter Anvin  * Some CPU features depend on higher CPUID levels, which may not always
514b38b0665SH. Peter Anvin  * be available due to CPUID level capping or broken virtualization
515b38b0665SH. Peter Anvin  * software.  Add those features to this table to auto-disable them.
516b38b0665SH. Peter Anvin  */
517b38b0665SH. Peter Anvin struct cpuid_dependent_feature {
518b38b0665SH. Peter Anvin 	u32 feature;
519b38b0665SH. Peter Anvin 	u32 level;
520b38b0665SH. Peter Anvin };
5210f3fa48aSIngo Molnar 
522148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature
523b38b0665SH. Peter Anvin cpuid_dependent_features[] = {
524b38b0665SH. Peter Anvin 	{ X86_FEATURE_MWAIT,		0x00000005 },
525b38b0665SH. Peter Anvin 	{ X86_FEATURE_DCA,		0x00000009 },
526b38b0665SH. Peter Anvin 	{ X86_FEATURE_XSAVE,		0x0000000d },
527b38b0665SH. Peter Anvin 	{ 0, 0 }
528b38b0665SH. Peter Anvin };
529b38b0665SH. Peter Anvin 
530148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
531b38b0665SH. Peter Anvin {
532b38b0665SH. Peter Anvin 	const struct cpuid_dependent_feature *df;
5339766cdbcSJaswinder Singh Rajput 
534b38b0665SH. Peter Anvin 	for (df = cpuid_dependent_features; df->feature; df++) {
5350f3fa48aSIngo Molnar 
5360f3fa48aSIngo Molnar 		if (!cpu_has(c, df->feature))
5370f3fa48aSIngo Molnar 			continue;
538b38b0665SH. Peter Anvin 		/*
539b38b0665SH. Peter Anvin 		 * Note: cpuid_level is set to -1 if unavailable, but
540b38b0665SH. Peter Anvin 		 * extended_extended_level is set to 0 if unavailable
541b38b0665SH. Peter Anvin 		 * and the legitimate extended levels are all negative
542b38b0665SH. Peter Anvin 		 * when signed; hence the weird messing around with
543b38b0665SH. Peter Anvin 		 * signs here...
544b38b0665SH. Peter Anvin 		 */
5450f3fa48aSIngo Molnar 		if (!((s32)df->level < 0 ?
546f6db44dfSYinghai Lu 		     (u32)df->level > (u32)c->extended_cpuid_level :
5470f3fa48aSIngo Molnar 		     (s32)df->level > (s32)c->cpuid_level))
5480f3fa48aSIngo Molnar 			continue;
5490f3fa48aSIngo Molnar 
550b38b0665SH. Peter Anvin 		clear_cpu_cap(c, df->feature);
5510f3fa48aSIngo Molnar 		if (!warn)
5520f3fa48aSIngo Molnar 			continue;
5530f3fa48aSIngo Molnar 
5541b74dde7SChen Yucong 		pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
5559def39beSJosh Triplett 			x86_cap_flag(df->feature), df->level);
556b38b0665SH. Peter Anvin 	}
557b38b0665SH. Peter Anvin }
558b38b0665SH. Peter Anvin 
559b38b0665SH. Peter Anvin /*
560f7627e25SThomas Gleixner  * Naming convention should be: <Name> [(<Codename>)]
561f7627e25SThomas Gleixner  * This table only is used unless init_<vendor>() below doesn't set it;
5620f3fa48aSIngo Molnar  * in particular, if CPUID levels 0x80000002..4 are supported, this
5630f3fa48aSIngo Molnar  * isn't used
564f7627e25SThomas Gleixner  */
565f7627e25SThomas Gleixner 
566f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */
567148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c)
568f7627e25SThomas Gleixner {
56909dc68d9SJan Beulich #ifdef CONFIG_X86_32
57009dc68d9SJan Beulich 	const struct legacy_cpu_model_info *info;
571f7627e25SThomas Gleixner 
572f7627e25SThomas Gleixner 	if (c->x86_model >= 16)
573f7627e25SThomas Gleixner 		return NULL;	/* Range check */
574f7627e25SThomas Gleixner 
575f7627e25SThomas Gleixner 	if (!this_cpu)
576f7627e25SThomas Gleixner 		return NULL;
577f7627e25SThomas Gleixner 
57809dc68d9SJan Beulich 	info = this_cpu->legacy_models;
579f7627e25SThomas Gleixner 
58009dc68d9SJan Beulich 	while (info->family) {
581f7627e25SThomas Gleixner 		if (info->family == c->x86)
582f7627e25SThomas Gleixner 			return info->model_names[c->x86_model];
583f7627e25SThomas Gleixner 		info++;
584f7627e25SThomas Gleixner 	}
58509dc68d9SJan Beulich #endif
586f7627e25SThomas Gleixner 	return NULL;		/* Not found */
587f7627e25SThomas Gleixner }
588f7627e25SThomas Gleixner 
589f6a892ddSFenghua Yu /* Aligned to unsigned long to avoid split lock in atomic bitmap ops */
590f6a892ddSFenghua Yu __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
591f6a892ddSFenghua Yu __u32 cpu_caps_set[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
592f7627e25SThomas Gleixner 
59311e3a840SJeremy Fitzhardinge void load_percpu_segment(int cpu)
5949d31d35bSYinghai Lu {
595fab334c1SYinghai Lu #ifdef CONFIG_X86_32
5962697fbd5SBrian Gerst 	loadsegment(fs, __KERNEL_PERCPU);
5972697fbd5SBrian Gerst #else
59845e876f7SAndy Lutomirski 	__loadsegment_simple(gs, 0);
59935060ed6SVitaly Kuznetsov 	wrmsrl(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu));
600fab334c1SYinghai Lu #endif
6019d31d35bSYinghai Lu }
6029d31d35bSYinghai Lu 
60372f5e08dSAndy Lutomirski #ifdef CONFIG_X86_32
60472f5e08dSAndy Lutomirski /* The 32-bit entry code needs to find cpu_entry_area. */
60572f5e08dSAndy Lutomirski DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
60672f5e08dSAndy Lutomirski #endif
60772f5e08dSAndy Lutomirski 
60845fc8757SThomas Garnier /* Load the original GDT from the per-cpu structure */
60945fc8757SThomas Garnier void load_direct_gdt(int cpu)
61045fc8757SThomas Garnier {
61145fc8757SThomas Garnier 	struct desc_ptr gdt_descr;
61245fc8757SThomas Garnier 
61345fc8757SThomas Garnier 	gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
61445fc8757SThomas Garnier 	gdt_descr.size = GDT_SIZE - 1;
61545fc8757SThomas Garnier 	load_gdt(&gdt_descr);
61645fc8757SThomas Garnier }
61745fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_direct_gdt);
61845fc8757SThomas Garnier 
61969218e47SThomas Garnier /* Load a fixmap remapping of the per-cpu GDT */
62069218e47SThomas Garnier void load_fixmap_gdt(int cpu)
62169218e47SThomas Garnier {
62269218e47SThomas Garnier 	struct desc_ptr gdt_descr;
62369218e47SThomas Garnier 
62469218e47SThomas Garnier 	gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
62569218e47SThomas Garnier 	gdt_descr.size = GDT_SIZE - 1;
62669218e47SThomas Garnier 	load_gdt(&gdt_descr);
62769218e47SThomas Garnier }
62845fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_fixmap_gdt);
62969218e47SThomas Garnier 
6300f3fa48aSIngo Molnar /*
6310f3fa48aSIngo Molnar  * Current gdt points %fs at the "master" per-cpu area: after this,
6320f3fa48aSIngo Molnar  * it's on the real one.
6330f3fa48aSIngo Molnar  */
634552be871SBrian Gerst void switch_to_new_gdt(int cpu)
635f7627e25SThomas Gleixner {
63645fc8757SThomas Garnier 	/* Load the original GDT */
63745fc8757SThomas Garnier 	load_direct_gdt(cpu);
638f7627e25SThomas Gleixner 	/* Reload the per-cpu base */
63911e3a840SJeremy Fitzhardinge 	load_percpu_segment(cpu);
640f7627e25SThomas Gleixner }
641f7627e25SThomas Gleixner 
642148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
643f7627e25SThomas Gleixner 
644148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c)
645f7627e25SThomas Gleixner {
646f7627e25SThomas Gleixner 	unsigned int *v;
647ee098e1aSBorislav Petkov 	char *p, *q, *s;
648f7627e25SThomas Gleixner 
6493da99c97SYinghai Lu 	if (c->extended_cpuid_level < 0x80000004)
6501b05d60dSYinghai Lu 		return;
651f7627e25SThomas Gleixner 
652f7627e25SThomas Gleixner 	v = (unsigned int *)c->x86_model_id;
653f7627e25SThomas Gleixner 	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
654f7627e25SThomas Gleixner 	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
655f7627e25SThomas Gleixner 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
656f7627e25SThomas Gleixner 	c->x86_model_id[48] = 0;
657f7627e25SThomas Gleixner 
658ee098e1aSBorislav Petkov 	/* Trim whitespace */
659ee098e1aSBorislav Petkov 	p = q = s = &c->x86_model_id[0];
660ee098e1aSBorislav Petkov 
661ee098e1aSBorislav Petkov 	while (*p == ' ')
662ee098e1aSBorislav Petkov 		p++;
663ee098e1aSBorislav Petkov 
664ee098e1aSBorislav Petkov 	while (*p) {
665ee098e1aSBorislav Petkov 		/* Note the last non-whitespace index */
666ee098e1aSBorislav Petkov 		if (!isspace(*p))
667ee098e1aSBorislav Petkov 			s = q;
668ee098e1aSBorislav Petkov 
669ee098e1aSBorislav Petkov 		*q++ = *p++;
670ee098e1aSBorislav Petkov 	}
671ee098e1aSBorislav Petkov 
672ee098e1aSBorislav Petkov 	*(s + 1) = '\0';
673f7627e25SThomas Gleixner }
674f7627e25SThomas Gleixner 
6759305bd6cSThomas Gleixner void detect_num_cpu_cores(struct cpuinfo_x86 *c)
6762cc61be6SDavid Wang {
6772cc61be6SDavid Wang 	unsigned int eax, ebx, ecx, edx;
6782cc61be6SDavid Wang 
6799305bd6cSThomas Gleixner 	c->x86_max_cores = 1;
6802cc61be6SDavid Wang 	if (!IS_ENABLED(CONFIG_SMP) || c->cpuid_level < 4)
6819305bd6cSThomas Gleixner 		return;
6822cc61be6SDavid Wang 
6832cc61be6SDavid Wang 	cpuid_count(4, 0, &eax, &ebx, &ecx, &edx);
6842cc61be6SDavid Wang 	if (eax & 0x1f)
6859305bd6cSThomas Gleixner 		c->x86_max_cores = (eax >> 26) + 1;
6862cc61be6SDavid Wang }
6872cc61be6SDavid Wang 
688148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
689f7627e25SThomas Gleixner {
6909d31d35bSYinghai Lu 	unsigned int n, dummy, ebx, ecx, edx, l2size;
691f7627e25SThomas Gleixner 
6923da99c97SYinghai Lu 	n = c->extended_cpuid_level;
693f7627e25SThomas Gleixner 
694f7627e25SThomas Gleixner 	if (n >= 0x80000005) {
6959d31d35bSYinghai Lu 		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
696f7627e25SThomas Gleixner 		c->x86_cache_size = (ecx>>24) + (edx>>24);
697140fc727SYinghai Lu #ifdef CONFIG_X86_64
698140fc727SYinghai Lu 		/* On K8 L1 TLB is inclusive, so don't count it */
699140fc727SYinghai Lu 		c->x86_tlbsize = 0;
700140fc727SYinghai Lu #endif
701f7627e25SThomas Gleixner 	}
702f7627e25SThomas Gleixner 
703f7627e25SThomas Gleixner 	if (n < 0x80000006)	/* Some chips just has a large L1. */
704f7627e25SThomas Gleixner 		return;
705f7627e25SThomas Gleixner 
7060a488a53SYinghai Lu 	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
707f7627e25SThomas Gleixner 	l2size = ecx >> 16;
708f7627e25SThomas Gleixner 
709140fc727SYinghai Lu #ifdef CONFIG_X86_64
710140fc727SYinghai Lu 	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
711140fc727SYinghai Lu #else
712f7627e25SThomas Gleixner 	/* do processor-specific cache resizing */
71309dc68d9SJan Beulich 	if (this_cpu->legacy_cache_size)
71409dc68d9SJan Beulich 		l2size = this_cpu->legacy_cache_size(c, l2size);
715f7627e25SThomas Gleixner 
716f7627e25SThomas Gleixner 	/* Allow user to override all this if necessary. */
717f7627e25SThomas Gleixner 	if (cachesize_override != -1)
718f7627e25SThomas Gleixner 		l2size = cachesize_override;
719f7627e25SThomas Gleixner 
720f7627e25SThomas Gleixner 	if (l2size == 0)
721f7627e25SThomas Gleixner 		return;		/* Again, no L2 cache is possible */
722140fc727SYinghai Lu #endif
723f7627e25SThomas Gleixner 
724f7627e25SThomas Gleixner 	c->x86_cache_size = l2size;
725f7627e25SThomas Gleixner }
726f7627e25SThomas Gleixner 
727e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO];
728e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO];
729e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO];
730e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO];
731e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO];
732e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO];
733dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO];
734e0ba94f1SAlex Shi 
735f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c)
736e0ba94f1SAlex Shi {
737e0ba94f1SAlex Shi 	if (this_cpu->c_detect_tlb)
738e0ba94f1SAlex Shi 		this_cpu->c_detect_tlb(c);
739e0ba94f1SAlex Shi 
740f94fe119SSteven Honeyman 	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
741e0ba94f1SAlex Shi 		tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
742f94fe119SSteven Honeyman 		tlb_lli_4m[ENTRIES]);
743f94fe119SSteven Honeyman 
744f94fe119SSteven Honeyman 	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
745f94fe119SSteven Honeyman 		tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
746f94fe119SSteven Honeyman 		tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
747e0ba94f1SAlex Shi }
748e0ba94f1SAlex Shi 
749545401f4SThomas Gleixner int detect_ht_early(struct cpuinfo_x86 *c)
7509d31d35bSYinghai Lu {
751c8e56d20SBorislav Petkov #ifdef CONFIG_SMP
7529d31d35bSYinghai Lu 	u32 eax, ebx, ecx, edx;
7539d31d35bSYinghai Lu 
7540a488a53SYinghai Lu 	if (!cpu_has(c, X86_FEATURE_HT))
755545401f4SThomas Gleixner 		return -1;
7569d31d35bSYinghai Lu 
7570a488a53SYinghai Lu 	if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
758545401f4SThomas Gleixner 		return -1;
7590a488a53SYinghai Lu 
7601cd78776SYinghai Lu 	if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
761545401f4SThomas Gleixner 		return -1;
7621cd78776SYinghai Lu 
7630a488a53SYinghai Lu 	cpuid(1, &eax, &ebx, &ecx, &edx);
7640a488a53SYinghai Lu 
7659d31d35bSYinghai Lu 	smp_num_siblings = (ebx & 0xff0000) >> 16;
766545401f4SThomas Gleixner 	if (smp_num_siblings == 1)
7671b74dde7SChen Yucong 		pr_info_once("CPU0: Hyper-Threading is disabled\n");
768545401f4SThomas Gleixner #endif
769545401f4SThomas Gleixner 	return 0;
7700f3fa48aSIngo Molnar }
7710f3fa48aSIngo Molnar 
772545401f4SThomas Gleixner void detect_ht(struct cpuinfo_x86 *c)
773545401f4SThomas Gleixner {
774545401f4SThomas Gleixner #ifdef CONFIG_SMP
775545401f4SThomas Gleixner 	int index_msb, core_bits;
776545401f4SThomas Gleixner 
777545401f4SThomas Gleixner 	if (detect_ht_early(c) < 0)
778545401f4SThomas Gleixner 		return;
7799d31d35bSYinghai Lu 
7809d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
781cb8cc442SIngo Molnar 	c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
7829d31d35bSYinghai Lu 
7839d31d35bSYinghai Lu 	smp_num_siblings = smp_num_siblings / c->x86_max_cores;
7849d31d35bSYinghai Lu 
7859d31d35bSYinghai Lu 	index_msb = get_count_order(smp_num_siblings);
7869d31d35bSYinghai Lu 
7879d31d35bSYinghai Lu 	core_bits = get_count_order(c->x86_max_cores);
7889d31d35bSYinghai Lu 
789cb8cc442SIngo Molnar 	c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
7901cd78776SYinghai Lu 				       ((1 << core_bits) - 1);
7919d31d35bSYinghai Lu #endif
79297e4db7cSYinghai Lu }
793f7627e25SThomas Gleixner 
794148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c)
795f7627e25SThomas Gleixner {
796f7627e25SThomas Gleixner 	char *v = c->x86_vendor_id;
7970f3fa48aSIngo Molnar 	int i;
798f7627e25SThomas Gleixner 
799f7627e25SThomas Gleixner 	for (i = 0; i < X86_VENDOR_NUM; i++) {
80010a434fcSYinghai Lu 		if (!cpu_devs[i])
80110a434fcSYinghai Lu 			break;
80210a434fcSYinghai Lu 
803f7627e25SThomas Gleixner 		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
804f7627e25SThomas Gleixner 		    (cpu_devs[i]->c_ident[1] &&
805f7627e25SThomas Gleixner 		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
8060f3fa48aSIngo Molnar 
807f7627e25SThomas Gleixner 			this_cpu = cpu_devs[i];
80810a434fcSYinghai Lu 			c->x86_vendor = this_cpu->c_x86_vendor;
809f7627e25SThomas Gleixner 			return;
810f7627e25SThomas Gleixner 		}
811f7627e25SThomas Gleixner 	}
81210a434fcSYinghai Lu 
8131b74dde7SChen Yucong 	pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
814a9c56953SMinchan Kim 		    "CPU: Your system may be unstable.\n", v);
81510a434fcSYinghai Lu 
816f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
817f7627e25SThomas Gleixner 	this_cpu = &default_cpu;
818f7627e25SThomas Gleixner }
819f7627e25SThomas Gleixner 
820148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c)
821f7627e25SThomas Gleixner {
822f7627e25SThomas Gleixner 	/* Get vendor name */
8234a148513SHarvey Harrison 	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
8244a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[0],
8254a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[8],
8264a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[4]);
827f7627e25SThomas Gleixner 
828f7627e25SThomas Gleixner 	c->x86 = 4;
8299d31d35bSYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
830f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
831f7627e25SThomas Gleixner 		u32 junk, tfms, cap0, misc;
8320f3fa48aSIngo Molnar 
833f7627e25SThomas Gleixner 		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
83499f925ceSBorislav Petkov 		c->x86		= x86_family(tfms);
83599f925ceSBorislav Petkov 		c->x86_model	= x86_model(tfms);
836b399151cSJia Zhang 		c->x86_stepping	= x86_stepping(tfms);
8370f3fa48aSIngo Molnar 
838d4387bd3SHuang, Ying 		if (cap0 & (1<<19)) {
839d4387bd3SHuang, Ying 			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
8409d31d35bSYinghai Lu 			c->x86_cache_alignment = c->x86_clflush_size;
841d4387bd3SHuang, Ying 		}
842f7627e25SThomas Gleixner 	}
843f7627e25SThomas Gleixner }
8443da99c97SYinghai Lu 
8458bf1ebcaSAndy Lutomirski static void apply_forced_caps(struct cpuinfo_x86 *c)
8468bf1ebcaSAndy Lutomirski {
8478bf1ebcaSAndy Lutomirski 	int i;
8488bf1ebcaSAndy Lutomirski 
8496cbd2171SThomas Gleixner 	for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
8508bf1ebcaSAndy Lutomirski 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
8518bf1ebcaSAndy Lutomirski 		c->x86_capability[i] |= cpu_caps_set[i];
8528bf1ebcaSAndy Lutomirski 	}
8538bf1ebcaSAndy Lutomirski }
8548bf1ebcaSAndy Lutomirski 
8557fcae111SDavid Woodhouse static void init_speculation_control(struct cpuinfo_x86 *c)
8567fcae111SDavid Woodhouse {
8577fcae111SDavid Woodhouse 	/*
8587fcae111SDavid Woodhouse 	 * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
8597fcae111SDavid Woodhouse 	 * and they also have a different bit for STIBP support. Also,
8607fcae111SDavid Woodhouse 	 * a hypervisor might have set the individual AMD bits even on
8617fcae111SDavid Woodhouse 	 * Intel CPUs, for finer-grained selection of what's available.
8627fcae111SDavid Woodhouse 	 */
8637fcae111SDavid Woodhouse 	if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
8647fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_IBRS);
8657fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_IBPB);
8667eb8956aSThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8677fcae111SDavid Woodhouse 	}
868e7c587daSBorislav Petkov 
8697fcae111SDavid Woodhouse 	if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
8707fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_STIBP);
871e7c587daSBorislav Petkov 
872bc226f07STom Lendacky 	if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
873bc226f07STom Lendacky 	    cpu_has(c, X86_FEATURE_VIRT_SSBD))
87452817587SThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_SSBD);
87552817587SThomas Gleixner 
8767eb8956aSThomas Gleixner 	if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
877e7c587daSBorislav Petkov 		set_cpu_cap(c, X86_FEATURE_IBRS);
8787eb8956aSThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8797eb8956aSThomas Gleixner 	}
880e7c587daSBorislav Petkov 
881e7c587daSBorislav Petkov 	if (cpu_has(c, X86_FEATURE_AMD_IBPB))
882e7c587daSBorislav Petkov 		set_cpu_cap(c, X86_FEATURE_IBPB);
883e7c587daSBorislav Petkov 
8847eb8956aSThomas Gleixner 	if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
885e7c587daSBorislav Petkov 		set_cpu_cap(c, X86_FEATURE_STIBP);
8867eb8956aSThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8877eb8956aSThomas Gleixner 	}
8886ac2f49eSKonrad Rzeszutek Wilk 
8896ac2f49eSKonrad Rzeszutek Wilk 	if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
8906ac2f49eSKonrad Rzeszutek Wilk 		set_cpu_cap(c, X86_FEATURE_SSBD);
8916ac2f49eSKonrad Rzeszutek Wilk 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8926ac2f49eSKonrad Rzeszutek Wilk 		clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
8936ac2f49eSKonrad Rzeszutek Wilk 	}
8947fcae111SDavid Woodhouse }
8957fcae111SDavid Woodhouse 
896148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c)
897093af8d7SYinghai Lu {
89839c06df4SBorislav Petkov 	u32 eax, ebx, ecx, edx;
899093af8d7SYinghai Lu 
900093af8d7SYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
901093af8d7SYinghai Lu 	if (c->cpuid_level >= 0x00000001) {
90239c06df4SBorislav Petkov 		cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
9030f3fa48aSIngo Molnar 
90439c06df4SBorislav Petkov 		c->x86_capability[CPUID_1_ECX] = ecx;
90539c06df4SBorislav Petkov 		c->x86_capability[CPUID_1_EDX] = edx;
906093af8d7SYinghai Lu 	}
907093af8d7SYinghai Lu 
9083df8d920SAndy Lutomirski 	/* Thermal and Power Management Leaf: level 0x00000006 (eax) */
9093df8d920SAndy Lutomirski 	if (c->cpuid_level >= 0x00000006)
9103df8d920SAndy Lutomirski 		c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
9113df8d920SAndy Lutomirski 
912bdc802dcSH. Peter Anvin 	/* Additional Intel-defined flags: level 0x00000007 */
913bdc802dcSH. Peter Anvin 	if (c->cpuid_level >= 0x00000007) {
914bdc802dcSH. Peter Anvin 		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
91539c06df4SBorislav Petkov 		c->x86_capability[CPUID_7_0_EBX] = ebx;
916dfb4a70fSDave Hansen 		c->x86_capability[CPUID_7_ECX] = ecx;
91795ca0ee8SDavid Woodhouse 		c->x86_capability[CPUID_7_EDX] = edx;
918b302e4b1SFenghua Yu 
919b302e4b1SFenghua Yu 		/* Check valid sub-leaf index before accessing it */
920b302e4b1SFenghua Yu 		if (eax >= 1) {
921b302e4b1SFenghua Yu 			cpuid_count(0x00000007, 1, &eax, &ebx, &ecx, &edx);
922b302e4b1SFenghua Yu 			c->x86_capability[CPUID_7_1_EAX] = eax;
923b302e4b1SFenghua Yu 		}
924bdc802dcSH. Peter Anvin 	}
925bdc802dcSH. Peter Anvin 
9266229ad27SFenghua Yu 	/* Extended state features: level 0x0000000d */
9276229ad27SFenghua Yu 	if (c->cpuid_level >= 0x0000000d) {
9286229ad27SFenghua Yu 		cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
9296229ad27SFenghua Yu 
93039c06df4SBorislav Petkov 		c->x86_capability[CPUID_D_1_EAX] = eax;
9316229ad27SFenghua Yu 	}
9326229ad27SFenghua Yu 
933093af8d7SYinghai Lu 	/* AMD-defined flags: level 0x80000001 */
93439c06df4SBorislav Petkov 	eax = cpuid_eax(0x80000000);
93539c06df4SBorislav Petkov 	c->extended_cpuid_level = eax;
9360f3fa48aSIngo Molnar 
93739c06df4SBorislav Petkov 	if ((eax & 0xffff0000) == 0x80000000) {
93839c06df4SBorislav Petkov 		if (eax >= 0x80000001) {
93939c06df4SBorislav Petkov 			cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
94039c06df4SBorislav Petkov 
94139c06df4SBorislav Petkov 			c->x86_capability[CPUID_8000_0001_ECX] = ecx;
94239c06df4SBorislav Petkov 			c->x86_capability[CPUID_8000_0001_EDX] = edx;
943093af8d7SYinghai Lu 		}
944093af8d7SYinghai Lu 	}
945093af8d7SYinghai Lu 
94671faad43SYazen Ghannam 	if (c->extended_cpuid_level >= 0x80000007) {
94771faad43SYazen Ghannam 		cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
94871faad43SYazen Ghannam 
94971faad43SYazen Ghannam 		c->x86_capability[CPUID_8000_0007_EBX] = ebx;
95071faad43SYazen Ghannam 		c->x86_power = edx;
95171faad43SYazen Ghannam 	}
95271faad43SYazen Ghannam 
953c65732e4SThomas Gleixner 	if (c->extended_cpuid_level >= 0x80000008) {
954c65732e4SThomas Gleixner 		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
955c65732e4SThomas Gleixner 		c->x86_capability[CPUID_8000_0008_EBX] = ebx;
956c65732e4SThomas Gleixner 	}
957c65732e4SThomas Gleixner 
9582ccd71f1SBorislav Petkov 	if (c->extended_cpuid_level >= 0x8000000a)
95939c06df4SBorislav Petkov 		c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
9602ccd71f1SBorislav Petkov 
961fb35d30fSSean Christopherson 	if (c->extended_cpuid_level >= 0x8000001f)
962fb35d30fSSean Christopherson 		c->x86_capability[CPUID_8000_001F_EAX] = cpuid_eax(0x8000001f);
963fb35d30fSSean Christopherson 
9641dedefd1SJacob Pan 	init_scattered_cpuid_features(c);
9657fcae111SDavid Woodhouse 	init_speculation_control(c);
96660d34501SAndy Lutomirski 
96760d34501SAndy Lutomirski 	/*
96860d34501SAndy Lutomirski 	 * Clear/Set all flags overridden by options, after probe.
96960d34501SAndy Lutomirski 	 * This needs to happen each time we re-probe, which may happen
97060d34501SAndy Lutomirski 	 * several times during CPU initialization.
97160d34501SAndy Lutomirski 	 */
97260d34501SAndy Lutomirski 	apply_forced_caps(c);
973093af8d7SYinghai Lu }
974093af8d7SYinghai Lu 
975405c018aSM. Vefa Bicakci void get_cpu_address_sizes(struct cpuinfo_x86 *c)
976d94a155cSKirill A. Shutemov {
977d94a155cSKirill A. Shutemov 	u32 eax, ebx, ecx, edx;
978d94a155cSKirill A. Shutemov 
979d94a155cSKirill A. Shutemov 	if (c->extended_cpuid_level >= 0x80000008) {
980d94a155cSKirill A. Shutemov 		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
981d94a155cSKirill A. Shutemov 
982d94a155cSKirill A. Shutemov 		c->x86_virt_bits = (eax >> 8) & 0xff;
983d94a155cSKirill A. Shutemov 		c->x86_phys_bits = eax & 0xff;
984d94a155cSKirill A. Shutemov 	}
985d94a155cSKirill A. Shutemov #ifdef CONFIG_X86_32
986d94a155cSKirill A. Shutemov 	else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
987d94a155cSKirill A. Shutemov 		c->x86_phys_bits = 36;
988d94a155cSKirill A. Shutemov #endif
989cc51e542SAndi Kleen 	c->x86_cache_bits = c->x86_phys_bits;
990d94a155cSKirill A. Shutemov }
991d94a155cSKirill A. Shutemov 
992148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
993aef93c8bSYinghai Lu {
994aef93c8bSYinghai Lu #ifdef CONFIG_X86_32
995aef93c8bSYinghai Lu 	int i;
996aef93c8bSYinghai Lu 
997aef93c8bSYinghai Lu 	/*
998aef93c8bSYinghai Lu 	 * First of all, decide if this is a 486 or higher
999aef93c8bSYinghai Lu 	 * It's a 486 if we can modify the AC flag
1000aef93c8bSYinghai Lu 	 */
1001aef93c8bSYinghai Lu 	if (flag_is_changeable_p(X86_EFLAGS_AC))
1002aef93c8bSYinghai Lu 		c->x86 = 4;
1003aef93c8bSYinghai Lu 	else
1004aef93c8bSYinghai Lu 		c->x86 = 3;
1005aef93c8bSYinghai Lu 
1006aef93c8bSYinghai Lu 	for (i = 0; i < X86_VENDOR_NUM; i++)
1007aef93c8bSYinghai Lu 		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
1008aef93c8bSYinghai Lu 			c->x86_vendor_id[0] = 0;
1009aef93c8bSYinghai Lu 			cpu_devs[i]->c_identify(c);
1010aef93c8bSYinghai Lu 			if (c->x86_vendor_id[0]) {
1011aef93c8bSYinghai Lu 				get_cpu_vendor(c);
1012aef93c8bSYinghai Lu 				break;
1013aef93c8bSYinghai Lu 			}
1014aef93c8bSYinghai Lu 		}
1015aef93c8bSYinghai Lu #endif
1016093af8d7SYinghai Lu }
1017f7627e25SThomas Gleixner 
101836ad3513SThomas Gleixner #define NO_SPECULATION		BIT(0)
101936ad3513SThomas Gleixner #define NO_MELTDOWN		BIT(1)
102036ad3513SThomas Gleixner #define NO_SSB			BIT(2)
102136ad3513SThomas Gleixner #define NO_L1TF			BIT(3)
1022ed5194c2SAndi Kleen #define NO_MDS			BIT(4)
1023e261f209SThomas Gleixner #define MSBDS_ONLY		BIT(5)
1024f36cf386SThomas Gleixner #define NO_SWAPGS		BIT(6)
1025db4d30fbSVineela Tummalapalli #define NO_ITLB_MULTIHIT	BIT(7)
10261e41a766STony W Wang-oc #define NO_SPECTRE_V2		BIT(8)
102736ad3513SThomas Gleixner 
1028f6d502fcSThomas Gleixner #define VULNWL(vendor, family, model, whitelist)	\
1029f6d502fcSThomas Gleixner 	X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, whitelist)
103036ad3513SThomas Gleixner 
103136ad3513SThomas Gleixner #define VULNWL_INTEL(model, whitelist)		\
103236ad3513SThomas Gleixner 	VULNWL(INTEL, 6, INTEL_FAM6_##model, whitelist)
103336ad3513SThomas Gleixner 
103436ad3513SThomas Gleixner #define VULNWL_AMD(family, whitelist)		\
103536ad3513SThomas Gleixner 	VULNWL(AMD, family, X86_MODEL_ANY, whitelist)
103636ad3513SThomas Gleixner 
103736ad3513SThomas Gleixner #define VULNWL_HYGON(family, whitelist)		\
103836ad3513SThomas Gleixner 	VULNWL(HYGON, family, X86_MODEL_ANY, whitelist)
103936ad3513SThomas Gleixner 
104036ad3513SThomas Gleixner static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
104136ad3513SThomas Gleixner 	VULNWL(ANY,	4, X86_MODEL_ANY,	NO_SPECULATION),
104236ad3513SThomas Gleixner 	VULNWL(CENTAUR,	5, X86_MODEL_ANY,	NO_SPECULATION),
104336ad3513SThomas Gleixner 	VULNWL(INTEL,	5, X86_MODEL_ANY,	NO_SPECULATION),
104436ad3513SThomas Gleixner 	VULNWL(NSC,	5, X86_MODEL_ANY,	NO_SPECULATION),
104536ad3513SThomas Gleixner 
1046ed5194c2SAndi Kleen 	/* Intel Family 6 */
1047db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SALTWELL,		NO_SPECULATION | NO_ITLB_MULTIHIT),
1048db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SALTWELL_TABLET,	NO_SPECULATION | NO_ITLB_MULTIHIT),
1049db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SALTWELL_MID,		NO_SPECULATION | NO_ITLB_MULTIHIT),
1050db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_BONNELL,		NO_SPECULATION | NO_ITLB_MULTIHIT),
1051db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_BONNELL_MID,		NO_SPECULATION | NO_ITLB_MULTIHIT),
105236ad3513SThomas Gleixner 
1053db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SILVERMONT,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1054db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SILVERMONT_D,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1055db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_SILVERMONT_MID,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1056db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_AIRMONT,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1057db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(XEON_PHI_KNL,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1058db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(XEON_PHI_KNM,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
105936ad3513SThomas Gleixner 
106036ad3513SThomas Gleixner 	VULNWL_INTEL(CORE_YONAH,		NO_SSB),
106136ad3513SThomas Gleixner 
1062db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_AIRMONT_MID,		NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1063db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_AIRMONT_NP,		NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
106436ad3513SThomas Gleixner 
1065db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_GOLDMONT,		NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
1066db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_GOLDMONT_D,		NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
1067db4d30fbSVineela Tummalapalli 	VULNWL_INTEL(ATOM_GOLDMONT_PLUS,	NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
1068f36cf386SThomas Gleixner 
1069f36cf386SThomas Gleixner 	/*
1070f36cf386SThomas Gleixner 	 * Technically, swapgs isn't serializing on AMD (despite it previously
1071f36cf386SThomas Gleixner 	 * being documented as such in the APM).  But according to AMD, %gs is
1072f36cf386SThomas Gleixner 	 * updated non-speculatively, and the issuing of %gs-relative memory
1073f36cf386SThomas Gleixner 	 * operands will be blocked until the %gs update completes, which is
1074f36cf386SThomas Gleixner 	 * good enough for our purposes.
1075f36cf386SThomas Gleixner 	 */
1076ed5194c2SAndi Kleen 
1077cad14885SPawan Gupta 	VULNWL_INTEL(ATOM_TREMONT_D,		NO_ITLB_MULTIHIT),
1078cad14885SPawan Gupta 
1079ed5194c2SAndi Kleen 	/* AMD Family 0xf - 0x12 */
1080db4d30fbSVineela Tummalapalli 	VULNWL_AMD(0x0f,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
1081db4d30fbSVineela Tummalapalli 	VULNWL_AMD(0x10,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
1082db4d30fbSVineela Tummalapalli 	VULNWL_AMD(0x11,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
1083db4d30fbSVineela Tummalapalli 	VULNWL_AMD(0x12,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
108436ad3513SThomas Gleixner 
108536ad3513SThomas Gleixner 	/* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */
1086db4d30fbSVineela Tummalapalli 	VULNWL_AMD(X86_FAMILY_ANY,	NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
1087db4d30fbSVineela Tummalapalli 	VULNWL_HYGON(X86_FAMILY_ANY,	NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
10881e41a766STony W Wang-oc 
10891e41a766STony W Wang-oc 	/* Zhaoxin Family 7 */
1090a84de2faSTony W Wang-oc 	VULNWL(CENTAUR,	7, X86_MODEL_ANY,	NO_SPECTRE_V2 | NO_SWAPGS),
1091a84de2faSTony W Wang-oc 	VULNWL(ZHAOXIN,	7, X86_MODEL_ANY,	NO_SPECTRE_V2 | NO_SWAPGS),
1092fec9434aSDavid Woodhouse 	{}
1093fec9434aSDavid Woodhouse };
1094fec9434aSDavid Woodhouse 
10957e5b3c26SMark Gross #define VULNBL_INTEL_STEPPINGS(model, steppings, issues)		   \
10967e5b3c26SMark Gross 	X86_MATCH_VENDOR_FAM_MODEL_STEPPINGS_FEATURE(INTEL, 6,		   \
10977e5b3c26SMark Gross 					    INTEL_FAM6_##model, steppings, \
10987e5b3c26SMark Gross 					    X86_FEATURE_ANY, issues)
10997e5b3c26SMark Gross 
11007e5b3c26SMark Gross #define SRBDS		BIT(0)
11017e5b3c26SMark Gross 
11027e5b3c26SMark Gross static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
11037e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(IVYBRIDGE,	X86_STEPPING_ANY,		SRBDS),
11047e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(HASWELL,		X86_STEPPING_ANY,		SRBDS),
11057e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(HASWELL_L,	X86_STEPPING_ANY,		SRBDS),
11067e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(HASWELL_G,	X86_STEPPING_ANY,		SRBDS),
11077e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(BROADWELL_G,	X86_STEPPING_ANY,		SRBDS),
11087e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(BROADWELL,	X86_STEPPING_ANY,		SRBDS),
11097e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(SKYLAKE_L,	X86_STEPPING_ANY,		SRBDS),
11107e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(SKYLAKE,		X86_STEPPING_ANY,		SRBDS),
11117e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(KABYLAKE_L,	X86_STEPPINGS(0x0, 0xC),	SRBDS),
11127e5b3c26SMark Gross 	VULNBL_INTEL_STEPPINGS(KABYLAKE,	X86_STEPPINGS(0x0, 0xD),	SRBDS),
11137e5b3c26SMark Gross 	{}
11147e5b3c26SMark Gross };
11157e5b3c26SMark Gross 
111693920f61SMark Gross static bool __init cpu_matches(const struct x86_cpu_id *table, unsigned long which)
111736ad3513SThomas Gleixner {
111893920f61SMark Gross 	const struct x86_cpu_id *m = x86_match_cpu(table);
1119fec9434aSDavid Woodhouse 
112036ad3513SThomas Gleixner 	return m && !!(m->driver_data & which);
112136ad3513SThomas Gleixner }
112217dbca11SAndi Kleen 
1123286836a7SPawan Gupta u64 x86_read_arch_cap_msr(void)
1124fec9434aSDavid Woodhouse {
1125fec9434aSDavid Woodhouse 	u64 ia32_cap = 0;
1126fec9434aSDavid Woodhouse 
1127286836a7SPawan Gupta 	if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1128286836a7SPawan Gupta 		rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
1129286836a7SPawan Gupta 
1130286836a7SPawan Gupta 	return ia32_cap;
1131286836a7SPawan Gupta }
1132286836a7SPawan Gupta 
1133286836a7SPawan Gupta static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
1134286836a7SPawan Gupta {
1135286836a7SPawan Gupta 	u64 ia32_cap = x86_read_arch_cap_msr();
1136286836a7SPawan Gupta 
1137db4d30fbSVineela Tummalapalli 	/* Set ITLB_MULTIHIT bug if cpu is not in the whitelist and not mitigated */
113893920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_ITLB_MULTIHIT) &&
113993920f61SMark Gross 	    !(ia32_cap & ARCH_CAP_PSCHANGE_MC_NO))
1140db4d30fbSVineela Tummalapalli 		setup_force_cpu_bug(X86_BUG_ITLB_MULTIHIT);
1141db4d30fbSVineela Tummalapalli 
114293920f61SMark Gross 	if (cpu_matches(cpu_vuln_whitelist, NO_SPECULATION))
11438ecc4979SDominik Brodowski 		return;
11448ecc4979SDominik Brodowski 
11458ecc4979SDominik Brodowski 	setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
11461e41a766STony W Wang-oc 
114793920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_SPECTRE_V2))
11488ecc4979SDominik Brodowski 		setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
11498ecc4979SDominik Brodowski 
115093920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_SSB) &&
115193920f61SMark Gross 	    !(ia32_cap & ARCH_CAP_SSB_NO) &&
115224809860SKonrad Rzeszutek Wilk 	   !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
1153c456442cSKonrad Rzeszutek Wilk 		setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
1154c456442cSKonrad Rzeszutek Wilk 
1155706d5168SSai Praneeth 	if (ia32_cap & ARCH_CAP_IBRS_ALL)
1156706d5168SSai Praneeth 		setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
1157706d5168SSai Praneeth 
115893920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_MDS) &&
115993920f61SMark Gross 	    !(ia32_cap & ARCH_CAP_MDS_NO)) {
1160ed5194c2SAndi Kleen 		setup_force_cpu_bug(X86_BUG_MDS);
116193920f61SMark Gross 		if (cpu_matches(cpu_vuln_whitelist, MSBDS_ONLY))
1162e261f209SThomas Gleixner 			setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
1163e261f209SThomas Gleixner 	}
1164ed5194c2SAndi Kleen 
116593920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_SWAPGS))
1166f36cf386SThomas Gleixner 		setup_force_cpu_bug(X86_BUG_SWAPGS);
1167f36cf386SThomas Gleixner 
11681b42f017SPawan Gupta 	/*
11691b42f017SPawan Gupta 	 * When the CPU is not mitigated for TAA (TAA_NO=0) set TAA bug when:
11701b42f017SPawan Gupta 	 *	- TSX is supported or
11711b42f017SPawan Gupta 	 *	- TSX_CTRL is present
11721b42f017SPawan Gupta 	 *
11731b42f017SPawan Gupta 	 * TSX_CTRL check is needed for cases when TSX could be disabled before
11741b42f017SPawan Gupta 	 * the kernel boot e.g. kexec.
11751b42f017SPawan Gupta 	 * TSX_CTRL check alone is not sufficient for cases when the microcode
11761b42f017SPawan Gupta 	 * update is not present or running as guest that don't get TSX_CTRL.
11771b42f017SPawan Gupta 	 */
11781b42f017SPawan Gupta 	if (!(ia32_cap & ARCH_CAP_TAA_NO) &&
11791b42f017SPawan Gupta 	    (cpu_has(c, X86_FEATURE_RTM) ||
11801b42f017SPawan Gupta 	     (ia32_cap & ARCH_CAP_TSX_CTRL_MSR)))
11811b42f017SPawan Gupta 		setup_force_cpu_bug(X86_BUG_TAA);
11821b42f017SPawan Gupta 
11837e5b3c26SMark Gross 	/*
11847e5b3c26SMark Gross 	 * SRBDS affects CPUs which support RDRAND or RDSEED and are listed
11857e5b3c26SMark Gross 	 * in the vulnerability blacklist.
11867e5b3c26SMark Gross 	 */
11877e5b3c26SMark Gross 	if ((cpu_has(c, X86_FEATURE_RDRAND) ||
11887e5b3c26SMark Gross 	     cpu_has(c, X86_FEATURE_RDSEED)) &&
11897e5b3c26SMark Gross 	    cpu_matches(cpu_vuln_blacklist, SRBDS))
11907e5b3c26SMark Gross 		    setup_force_cpu_bug(X86_BUG_SRBDS);
11917e5b3c26SMark Gross 
119293920f61SMark Gross 	if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
11934a28bfe3SKonrad Rzeszutek Wilk 		return;
1194fec9434aSDavid Woodhouse 
1195fec9434aSDavid Woodhouse 	/* Rogue Data Cache Load? No! */
1196fec9434aSDavid Woodhouse 	if (ia32_cap & ARCH_CAP_RDCL_NO)
11974a28bfe3SKonrad Rzeszutek Wilk 		return;
1198fec9434aSDavid Woodhouse 
11994a28bfe3SKonrad Rzeszutek Wilk 	setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
120017dbca11SAndi Kleen 
120193920f61SMark Gross 	if (cpu_matches(cpu_vuln_whitelist, NO_L1TF))
120217dbca11SAndi Kleen 		return;
120317dbca11SAndi Kleen 
120417dbca11SAndi Kleen 	setup_force_cpu_bug(X86_BUG_L1TF);
1205fec9434aSDavid Woodhouse }
1206fec9434aSDavid Woodhouse 
120734048c9eSPaolo Ciarrocchi /*
12088990cac6SPavel Tatashin  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
12098990cac6SPavel Tatashin  * unfortunately, that's not true in practice because of early VIA
12108990cac6SPavel Tatashin  * chips and (more importantly) broken virtualizers that are not easy
12118990cac6SPavel Tatashin  * to detect. In the latter case it doesn't even *fail* reliably, so
12128990cac6SPavel Tatashin  * probing for it doesn't even work. Disable it completely on 32-bit
12138990cac6SPavel Tatashin  * unless we can find a reliable way to detect all the broken cases.
12148990cac6SPavel Tatashin  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
12158990cac6SPavel Tatashin  */
12169b3661cdSBorislav Petkov static void detect_nopl(void)
12178990cac6SPavel Tatashin {
12188990cac6SPavel Tatashin #ifdef CONFIG_X86_32
12199b3661cdSBorislav Petkov 	setup_clear_cpu_cap(X86_FEATURE_NOPL);
12208990cac6SPavel Tatashin #else
12219b3661cdSBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_NOPL);
12228990cac6SPavel Tatashin #endif
12238990cac6SPavel Tatashin }
12248990cac6SPavel Tatashin 
12258990cac6SPavel Tatashin /*
12261ef5423aSMike Hommey  * We parse cpu parameters early because fpu__init_system() is executed
12271ef5423aSMike Hommey  * before parse_early_param().
12281ef5423aSMike Hommey  */
12291ef5423aSMike Hommey static void __init cpu_parse_early_param(void)
12301ef5423aSMike Hommey {
12311ef5423aSMike Hommey 	char arg[128];
12321ef5423aSMike Hommey 	char *argptr = arg;
12331ef5423aSMike Hommey 	int arglen, res, bit;
12341ef5423aSMike Hommey 
12351ef5423aSMike Hommey #ifdef CONFIG_X86_32
12361ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "no387"))
12371ef5423aSMike Hommey #ifdef CONFIG_MATH_EMULATION
12381ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_FPU);
12391ef5423aSMike Hommey #else
12401ef5423aSMike Hommey 		pr_err("Option 'no387' required CONFIG_MATH_EMULATION enabled.\n");
12411ef5423aSMike Hommey #endif
12421ef5423aSMike Hommey 
12431ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "nofxsr"))
12441ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_FXSR);
12451ef5423aSMike Hommey #endif
12461ef5423aSMike Hommey 
12471ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "noxsave"))
12481ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_XSAVE);
12491ef5423aSMike Hommey 
12501ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
12511ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
12521ef5423aSMike Hommey 
12531ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
12541ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_XSAVES);
12551ef5423aSMike Hommey 
12561ef5423aSMike Hommey 	arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg));
12571ef5423aSMike Hommey 	if (arglen <= 0)
12581ef5423aSMike Hommey 		return;
12591ef5423aSMike Hommey 
12601ef5423aSMike Hommey 	pr_info("Clearing CPUID bits:");
12611ef5423aSMike Hommey 	do {
12621ef5423aSMike Hommey 		res = get_option(&argptr, &bit);
12631ef5423aSMike Hommey 		if (res == 0 || res == 3)
12641ef5423aSMike Hommey 			break;
12651ef5423aSMike Hommey 
12661ef5423aSMike Hommey 		/* If the argument was too long, the last bit may be cut off */
12671ef5423aSMike Hommey 		if (res == 1 && arglen >= sizeof(arg))
12681ef5423aSMike Hommey 			break;
12691ef5423aSMike Hommey 
12701ef5423aSMike Hommey 		if (bit >= 0 && bit < NCAPINTS * 32) {
12711ef5423aSMike Hommey 			pr_cont(" " X86_CAP_FMT, x86_cap_flag(bit));
12721ef5423aSMike Hommey 			setup_clear_cpu_cap(bit);
12731ef5423aSMike Hommey 		}
12741ef5423aSMike Hommey 	} while (res == 2);
12751ef5423aSMike Hommey 	pr_cont("\n");
12761ef5423aSMike Hommey }
12771ef5423aSMike Hommey 
12781ef5423aSMike Hommey /*
127934048c9eSPaolo Ciarrocchi  * Do minimum CPU detection early.
128034048c9eSPaolo Ciarrocchi  * Fields really needed: vendor, cpuid_level, family, model, mask,
128134048c9eSPaolo Ciarrocchi  * cache alignment.
128234048c9eSPaolo Ciarrocchi  * The others are not touched to avoid unwanted side effects.
128334048c9eSPaolo Ciarrocchi  *
1284a1652bb8SJean Delvare  * WARNING: this function is only called on the boot CPU.  Don't add code
1285a1652bb8SJean Delvare  * here that is supposed to run on all CPUs.
128634048c9eSPaolo Ciarrocchi  */
12873da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c)
1288f7627e25SThomas Gleixner {
12896627d242SYinghai Lu #ifdef CONFIG_X86_64
12906627d242SYinghai Lu 	c->x86_clflush_size = 64;
129113c6c532SJan Beulich 	c->x86_phys_bits = 36;
129213c6c532SJan Beulich 	c->x86_virt_bits = 48;
12936627d242SYinghai Lu #else
1294d4387bd3SHuang, Ying 	c->x86_clflush_size = 32;
129513c6c532SJan Beulich 	c->x86_phys_bits = 32;
129613c6c532SJan Beulich 	c->x86_virt_bits = 32;
12976627d242SYinghai Lu #endif
12980a488a53SYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
1299f7627e25SThomas Gleixner 
13000e96f31eSJordan Borgner 	memset(&c->x86_capability, 0, sizeof(c->x86_capability));
13010a488a53SYinghai Lu 	c->extended_cpuid_level = 0;
13020a488a53SYinghai Lu 
13032893cc8fSMatthew Whitehead 	if (!have_cpuid_p())
13042893cc8fSMatthew Whitehead 		identify_cpu_without_cpuid(c);
13052893cc8fSMatthew Whitehead 
1306aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
130705fb3c19SAndy Lutomirski 	if (have_cpuid_p()) {
1308f7627e25SThomas Gleixner 		cpu_detect(c);
13093da99c97SYinghai Lu 		get_cpu_vendor(c);
13103da99c97SYinghai Lu 		get_cpu_cap(c);
1311d94a155cSKirill A. Shutemov 		get_cpu_address_sizes(c);
131278d1b296SBorislav Petkov 		setup_force_cpu_cap(X86_FEATURE_CPUID);
13131ef5423aSMike Hommey 		cpu_parse_early_param();
131412cf105cSKrzysztof Helt 
131510a434fcSYinghai Lu 		if (this_cpu->c_early_init)
131610a434fcSYinghai Lu 			this_cpu->c_early_init(c);
13173da99c97SYinghai Lu 
1318f6e9456cSRobert Richter 		c->cpu_index = 0;
1319b38b0665SH. Peter Anvin 		filter_cpuid_features(c, false);
1320de5397adSFenghua Yu 
1321a110b5ecSBorislav Petkov 		if (this_cpu->c_bsp_init)
1322a110b5ecSBorislav Petkov 			this_cpu->c_bsp_init(c);
132378d1b296SBorislav Petkov 	} else {
132478d1b296SBorislav Petkov 		setup_clear_cpu_cap(X86_FEATURE_CPUID);
132505fb3c19SAndy Lutomirski 	}
1326c3b83598SBorislav Petkov 
1327c3b83598SBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
1328a89f040fSThomas Gleixner 
13294a28bfe3SKonrad Rzeszutek Wilk 	cpu_set_bug_bits(c);
133099c6fa25SDavid Woodhouse 
13316650cdd9SPeter Zijlstra (Intel) 	cpu_set_core_cap_bits(c);
13326650cdd9SPeter Zijlstra (Intel) 
1333db52ef74SIngo Molnar 	fpu__init_system(c);
1334b8b7abaeSAndy Lutomirski 
1335b8b7abaeSAndy Lutomirski #ifdef CONFIG_X86_32
1336b8b7abaeSAndy Lutomirski 	/*
1337b8b7abaeSAndy Lutomirski 	 * Regardless of whether PCID is enumerated, the SDM says
1338b8b7abaeSAndy Lutomirski 	 * that it can't be enabled in 32-bit mode.
1339b8b7abaeSAndy Lutomirski 	 */
1340b8b7abaeSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_PCID);
1341b8b7abaeSAndy Lutomirski #endif
1342372fddf7SKirill A. Shutemov 
1343372fddf7SKirill A. Shutemov 	/*
1344372fddf7SKirill A. Shutemov 	 * Later in the boot process pgtable_l5_enabled() relies on
1345372fddf7SKirill A. Shutemov 	 * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not
1346372fddf7SKirill A. Shutemov 	 * enabled by this point we need to clear the feature bit to avoid
1347372fddf7SKirill A. Shutemov 	 * false-positives at the later stage.
1348372fddf7SKirill A. Shutemov 	 *
1349372fddf7SKirill A. Shutemov 	 * pgtable_l5_enabled() can be false here for several reasons:
1350372fddf7SKirill A. Shutemov 	 *  - 5-level paging is disabled compile-time;
1351372fddf7SKirill A. Shutemov 	 *  - it's 32-bit kernel;
1352372fddf7SKirill A. Shutemov 	 *  - machine doesn't support 5-level paging;
1353372fddf7SKirill A. Shutemov 	 *  - user specified 'no5lvl' in kernel command line.
1354372fddf7SKirill A. Shutemov 	 */
1355372fddf7SKirill A. Shutemov 	if (!pgtable_l5_enabled())
1356372fddf7SKirill A. Shutemov 		setup_clear_cpu_cap(X86_FEATURE_LA57);
13578990cac6SPavel Tatashin 
13589b3661cdSBorislav Petkov 	detect_nopl();
1359f7627e25SThomas Gleixner }
1360f7627e25SThomas Gleixner 
13619d31d35bSYinghai Lu void __init early_cpu_init(void)
13629d31d35bSYinghai Lu {
136302dde8b4SJan Beulich 	const struct cpu_dev *const *cdev;
136410a434fcSYinghai Lu 	int count = 0;
13659d31d35bSYinghai Lu 
1366ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
13671b74dde7SChen Yucong 	pr_info("KERNEL supported cpus:\n");
136831c997caSIngo Molnar #endif
136931c997caSIngo Molnar 
137010a434fcSYinghai Lu 	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
137102dde8b4SJan Beulich 		const struct cpu_dev *cpudev = *cdev;
13729d31d35bSYinghai Lu 
137310a434fcSYinghai Lu 		if (count >= X86_VENDOR_NUM)
137410a434fcSYinghai Lu 			break;
137510a434fcSYinghai Lu 		cpu_devs[count] = cpudev;
137610a434fcSYinghai Lu 		count++;
137710a434fcSYinghai Lu 
1378ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
137931c997caSIngo Molnar 		{
138031c997caSIngo Molnar 			unsigned int j;
138131c997caSIngo Molnar 
138210a434fcSYinghai Lu 			for (j = 0; j < 2; j++) {
138310a434fcSYinghai Lu 				if (!cpudev->c_ident[j])
138410a434fcSYinghai Lu 					continue;
13851b74dde7SChen Yucong 				pr_info("  %s %s\n", cpudev->c_vendor,
138610a434fcSYinghai Lu 					cpudev->c_ident[j]);
138710a434fcSYinghai Lu 			}
138810a434fcSYinghai Lu 		}
13890388423dSDave Jones #endif
139031c997caSIngo Molnar 	}
13919d31d35bSYinghai Lu 	early_identify_cpu(&boot_cpu_data);
1392f7627e25SThomas Gleixner }
1393f7627e25SThomas Gleixner 
13947a5d6704SAndy Lutomirski static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
13957a5d6704SAndy Lutomirski {
13967a5d6704SAndy Lutomirski #ifdef CONFIG_X86_64
139758a5aac5SAndy Lutomirski 	/*
13987a5d6704SAndy Lutomirski 	 * Empirically, writing zero to a segment selector on AMD does
13997a5d6704SAndy Lutomirski 	 * not clear the base, whereas writing zero to a segment
14007a5d6704SAndy Lutomirski 	 * selector on Intel does clear the base.  Intel's behavior
14017a5d6704SAndy Lutomirski 	 * allows slightly faster context switches in the common case
14027a5d6704SAndy Lutomirski 	 * where GS is unused by the prev and next threads.
140358a5aac5SAndy Lutomirski 	 *
14047a5d6704SAndy Lutomirski 	 * Since neither vendor documents this anywhere that I can see,
14057a5d6704SAndy Lutomirski 	 * detect it directly instead of hardcoding the choice by
14067a5d6704SAndy Lutomirski 	 * vendor.
14077a5d6704SAndy Lutomirski 	 *
14087a5d6704SAndy Lutomirski 	 * I've designated AMD's behavior as the "bug" because it's
14097a5d6704SAndy Lutomirski 	 * counterintuitive and less friendly.
141058a5aac5SAndy Lutomirski 	 */
14117a5d6704SAndy Lutomirski 
14127a5d6704SAndy Lutomirski 	unsigned long old_base, tmp;
14137a5d6704SAndy Lutomirski 	rdmsrl(MSR_FS_BASE, old_base);
14147a5d6704SAndy Lutomirski 	wrmsrl(MSR_FS_BASE, 1);
14157a5d6704SAndy Lutomirski 	loadsegment(fs, 0);
14167a5d6704SAndy Lutomirski 	rdmsrl(MSR_FS_BASE, tmp);
14177a5d6704SAndy Lutomirski 	if (tmp != 0)
14187a5d6704SAndy Lutomirski 		set_cpu_bug(c, X86_BUG_NULL_SEG);
14197a5d6704SAndy Lutomirski 	wrmsrl(MSR_FS_BASE, old_base);
142058a5aac5SAndy Lutomirski #endif
1421f7627e25SThomas Gleixner }
1422f7627e25SThomas Gleixner 
1423148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c)
1424f7627e25SThomas Gleixner {
14253da99c97SYinghai Lu 	c->extended_cpuid_level = 0;
1426f7627e25SThomas Gleixner 
1427aef93c8bSYinghai Lu 	if (!have_cpuid_p())
1428aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
1429f7627e25SThomas Gleixner 
1430aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
1431a9853dd6SIngo Molnar 	if (!have_cpuid_p())
1432aef93c8bSYinghai Lu 		return;
1433aef93c8bSYinghai Lu 
14343da99c97SYinghai Lu 	cpu_detect(c);
14353da99c97SYinghai Lu 
14363da99c97SYinghai Lu 	get_cpu_vendor(c);
14373da99c97SYinghai Lu 
14383da99c97SYinghai Lu 	get_cpu_cap(c);
14393da99c97SYinghai Lu 
1440d94a155cSKirill A. Shutemov 	get_cpu_address_sizes(c);
1441d94a155cSKirill A. Shutemov 
1442f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
14433da99c97SYinghai Lu 		c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
1444b89d3b3eSYinghai Lu #ifdef CONFIG_X86_32
1445c8e56d20SBorislav Petkov # ifdef CONFIG_SMP
1446cb8cc442SIngo Molnar 		c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1447f7627e25SThomas Gleixner # else
144801aaea1aSYinghai Lu 		c->apicid = c->initial_apicid;
1449f7627e25SThomas Gleixner # endif
1450b89d3b3eSYinghai Lu #endif
1451b89d3b3eSYinghai Lu 		c->phys_proc_id = c->initial_apicid;
1452f7627e25SThomas Gleixner 	}
1453f7627e25SThomas Gleixner 
1454f7627e25SThomas Gleixner 	get_model_name(c); /* Default name */
1455f7627e25SThomas Gleixner 
14567a5d6704SAndy Lutomirski 	detect_null_seg_behavior(c);
14570230bb03SAndy Lutomirski 
14580230bb03SAndy Lutomirski 	/*
14590230bb03SAndy Lutomirski 	 * ESPFIX is a strange bug.  All real CPUs have it.  Paravirt
14600230bb03SAndy Lutomirski 	 * systems that run Linux at CPL > 0 may or may not have the
14610230bb03SAndy Lutomirski 	 * issue, but, even if they have the issue, there's absolutely
14620230bb03SAndy Lutomirski 	 * nothing we can do about it because we can't use the real IRET
14630230bb03SAndy Lutomirski 	 * instruction.
14640230bb03SAndy Lutomirski 	 *
14650230bb03SAndy Lutomirski 	 * NB: For the time being, only 32-bit kernels support
14660230bb03SAndy Lutomirski 	 * X86_BUG_ESPFIX as such.  64-bit kernels directly choose
14670230bb03SAndy Lutomirski 	 * whether to apply espfix using paravirt hooks.  If any
14680230bb03SAndy Lutomirski 	 * non-paravirt system ever shows up that does *not* have the
14690230bb03SAndy Lutomirski 	 * ESPFIX issue, we can change this.
14700230bb03SAndy Lutomirski 	 */
14710230bb03SAndy Lutomirski #ifdef CONFIG_X86_32
14720230bb03SAndy Lutomirski 	set_cpu_bug(c, X86_BUG_ESPFIX);
14730230bb03SAndy Lutomirski #endif
1474f7627e25SThomas Gleixner }
1475f7627e25SThomas Gleixner 
1476f7627e25SThomas Gleixner /*
14779d85eb91SThomas Gleixner  * Validate that ACPI/mptables have the same information about the
14789d85eb91SThomas Gleixner  * effective APIC id and update the package map.
1479d49597fdSThomas Gleixner  */
14809d85eb91SThomas Gleixner static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
1481d49597fdSThomas Gleixner {
1482d49597fdSThomas Gleixner #ifdef CONFIG_SMP
14839d85eb91SThomas Gleixner 	unsigned int apicid, cpu = smp_processor_id();
1484d49597fdSThomas Gleixner 
1485d49597fdSThomas Gleixner 	apicid = apic->cpu_present_to_apicid(cpu);
1486d49597fdSThomas Gleixner 
14879d85eb91SThomas Gleixner 	if (apicid != c->apicid) {
14889d85eb91SThomas Gleixner 		pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
1489d49597fdSThomas Gleixner 		       cpu, apicid, c->initial_apicid);
1490d49597fdSThomas Gleixner 	}
14919d85eb91SThomas Gleixner 	BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
1492212bf4fdSLen Brown 	BUG_ON(topology_update_die_map(c->cpu_die_id, cpu));
1493d49597fdSThomas Gleixner #else
1494d49597fdSThomas Gleixner 	c->logical_proc_id = 0;
1495d49597fdSThomas Gleixner #endif
1496d49597fdSThomas Gleixner }
1497d49597fdSThomas Gleixner 
1498d49597fdSThomas Gleixner /*
1499f7627e25SThomas Gleixner  * This does the hard work of actually picking apart the CPU stuff...
1500f7627e25SThomas Gleixner  */
1501148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c)
1502f7627e25SThomas Gleixner {
1503f7627e25SThomas Gleixner 	int i;
1504f7627e25SThomas Gleixner 
1505f7627e25SThomas Gleixner 	c->loops_per_jiffy = loops_per_jiffy;
150624dbc600SGustavo A. R. Silva 	c->x86_cache_size = 0;
1507f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
1508b399151cSJia Zhang 	c->x86_model = c->x86_stepping = 0;	/* So far unknown... */
1509f7627e25SThomas Gleixner 	c->x86_vendor_id[0] = '\0'; /* Unset */
1510f7627e25SThomas Gleixner 	c->x86_model_id[0] = '\0';  /* Unset */
1511f7627e25SThomas Gleixner 	c->x86_max_cores = 1;
1512102bbe3aSYinghai Lu 	c->x86_coreid_bits = 0;
151379a8b9aaSBorislav Petkov 	c->cu_id = 0xff;
151411fdd252SYinghai Lu #ifdef CONFIG_X86_64
1515102bbe3aSYinghai Lu 	c->x86_clflush_size = 64;
151613c6c532SJan Beulich 	c->x86_phys_bits = 36;
151713c6c532SJan Beulich 	c->x86_virt_bits = 48;
1518102bbe3aSYinghai Lu #else
1519102bbe3aSYinghai Lu 	c->cpuid_level = -1;	/* CPUID not detected */
1520f7627e25SThomas Gleixner 	c->x86_clflush_size = 32;
152113c6c532SJan Beulich 	c->x86_phys_bits = 32;
152213c6c532SJan Beulich 	c->x86_virt_bits = 32;
1523102bbe3aSYinghai Lu #endif
1524102bbe3aSYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
15250e96f31eSJordan Borgner 	memset(&c->x86_capability, 0, sizeof(c->x86_capability));
1526b47ce1feSSean Christopherson #ifdef CONFIG_X86_VMX_FEATURE_NAMES
1527b47ce1feSSean Christopherson 	memset(&c->vmx_capability, 0, sizeof(c->vmx_capability));
1528b47ce1feSSean Christopherson #endif
1529f7627e25SThomas Gleixner 
1530f7627e25SThomas Gleixner 	generic_identify(c);
1531f7627e25SThomas Gleixner 
15323898534dSAndi Kleen 	if (this_cpu->c_identify)
1533f7627e25SThomas Gleixner 		this_cpu->c_identify(c);
1534f7627e25SThomas Gleixner 
15356a6256f9SAdam Buchbinder 	/* Clear/Set all flags overridden by options, after probe */
15368bf1ebcaSAndy Lutomirski 	apply_forced_caps(c);
15372759c328SYinghai Lu 
1538102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
1539cb8cc442SIngo Molnar 	c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1540102bbe3aSYinghai Lu #endif
1541102bbe3aSYinghai Lu 
1542f7627e25SThomas Gleixner 	/*
1543f7627e25SThomas Gleixner 	 * Vendor-specific initialization.  In this section we
1544f7627e25SThomas Gleixner 	 * canonicalize the feature flags, meaning if there are
1545f7627e25SThomas Gleixner 	 * features a certain CPU supports which CPUID doesn't
1546f7627e25SThomas Gleixner 	 * tell us, CPUID claiming incorrect flags, or other bugs,
1547f7627e25SThomas Gleixner 	 * we handle them here.
1548f7627e25SThomas Gleixner 	 *
1549f7627e25SThomas Gleixner 	 * At the end of this section, c->x86_capability better
1550f7627e25SThomas Gleixner 	 * indicate the features this CPU genuinely supports!
1551f7627e25SThomas Gleixner 	 */
1552f7627e25SThomas Gleixner 	if (this_cpu->c_init)
1553f7627e25SThomas Gleixner 		this_cpu->c_init(c);
1554f7627e25SThomas Gleixner 
1555f7627e25SThomas Gleixner 	/* Disable the PN if appropriate */
1556f7627e25SThomas Gleixner 	squash_the_stupid_serial_number(c);
1557f7627e25SThomas Gleixner 
1558aa35f896SRicardo Neri 	/* Set up SMEP/SMAP/UMIP */
1559b2cc2a07SH. Peter Anvin 	setup_smep(c);
1560b2cc2a07SH. Peter Anvin 	setup_smap(c);
1561aa35f896SRicardo Neri 	setup_umip(c);
1562b2cc2a07SH. Peter Anvin 
1563dd649bd0SAndy Lutomirski 	/* Enable FSGSBASE instructions if available. */
1564742c45c3SAndi Kleen 	if (cpu_has(c, X86_FEATURE_FSGSBASE)) {
1565dd649bd0SAndy Lutomirski 		cr4_set_bits(X86_CR4_FSGSBASE);
1566742c45c3SAndi Kleen 		elf_hwcap2 |= HWCAP2_FSGSBASE;
1567742c45c3SAndi Kleen 	}
1568dd649bd0SAndy Lutomirski 
1569f7627e25SThomas Gleixner 	/*
15700f3fa48aSIngo Molnar 	 * The vendor-specific functions might have changed features.
15710f3fa48aSIngo Molnar 	 * Now we do "generic changes."
1572f7627e25SThomas Gleixner 	 */
1573f7627e25SThomas Gleixner 
1574b38b0665SH. Peter Anvin 	/* Filter out anything that depends on CPUID levels we don't have */
1575b38b0665SH. Peter Anvin 	filter_cpuid_features(c, true);
1576b38b0665SH. Peter Anvin 
1577f7627e25SThomas Gleixner 	/* If the model name is still unset, do table lookup. */
1578f7627e25SThomas Gleixner 	if (!c->x86_model_id[0]) {
157902dde8b4SJan Beulich 		const char *p;
1580f7627e25SThomas Gleixner 		p = table_lookup_model(c);
1581f7627e25SThomas Gleixner 		if (p)
1582f7627e25SThomas Gleixner 			strcpy(c->x86_model_id, p);
1583f7627e25SThomas Gleixner 		else
1584f7627e25SThomas Gleixner 			/* Last resort... */
1585f7627e25SThomas Gleixner 			sprintf(c->x86_model_id, "%02x/%02x",
1586f7627e25SThomas Gleixner 				c->x86, c->x86_model);
1587f7627e25SThomas Gleixner 	}
1588f7627e25SThomas Gleixner 
1589102bbe3aSYinghai Lu #ifdef CONFIG_X86_64
1590102bbe3aSYinghai Lu 	detect_ht(c);
1591102bbe3aSYinghai Lu #endif
1592102bbe3aSYinghai Lu 
159349d859d7SH. Peter Anvin 	x86_init_rdrand(c);
159406976945SDave Hansen 	setup_pku(c);
15953e0c3737SYinghai Lu 
15963e0c3737SYinghai Lu 	/*
15976a6256f9SAdam Buchbinder 	 * Clear/Set all flags overridden by options, need do it
15983e0c3737SYinghai Lu 	 * before following smp all cpus cap AND.
15993e0c3737SYinghai Lu 	 */
16008bf1ebcaSAndy Lutomirski 	apply_forced_caps(c);
16013e0c3737SYinghai Lu 
1602f7627e25SThomas Gleixner 	/*
1603f7627e25SThomas Gleixner 	 * On SMP, boot_cpu_data holds the common feature set between
1604f7627e25SThomas Gleixner 	 * all CPUs; so make sure that we indicate which features are
1605f7627e25SThomas Gleixner 	 * common between the CPUs.  The first time this routine gets
1606f7627e25SThomas Gleixner 	 * executed, c == &boot_cpu_data.
1607f7627e25SThomas Gleixner 	 */
1608f7627e25SThomas Gleixner 	if (c != &boot_cpu_data) {
1609f7627e25SThomas Gleixner 		/* AND the already accumulated flags with these */
1610f7627e25SThomas Gleixner 		for (i = 0; i < NCAPINTS; i++)
1611f7627e25SThomas Gleixner 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
161265fc985bSBorislav Petkov 
161365fc985bSBorislav Petkov 		/* OR, i.e. replicate the bug flags */
161465fc985bSBorislav Petkov 		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
161565fc985bSBorislav Petkov 			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
1616f7627e25SThomas Gleixner 	}
1617f7627e25SThomas Gleixner 
1618f7627e25SThomas Gleixner 	/* Init Machine Check Exception if available. */
16195e09954aSBorislav Petkov 	mcheck_cpu_init(c);
162030d432dfSAndi Kleen 
162130d432dfSAndi Kleen 	select_idle_routine(c);
1622102bbe3aSYinghai Lu 
1623de2d9445STejun Heo #ifdef CONFIG_NUMA
1624102bbe3aSYinghai Lu 	numa_add_cpu(smp_processor_id());
1625102bbe3aSYinghai Lu #endif
1626f7627e25SThomas Gleixner }
1627f7627e25SThomas Gleixner 
16288b6c0ab1SIngo Molnar /*
16298b6c0ab1SIngo Molnar  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
16308b6c0ab1SIngo Molnar  * on 32-bit kernels:
16318b6c0ab1SIngo Molnar  */
1632cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32
1633cfda7bb9SAndy Lutomirski void enable_sep_cpu(void)
1634cfda7bb9SAndy Lutomirski {
16358b6c0ab1SIngo Molnar 	struct tss_struct *tss;
16368b6c0ab1SIngo Molnar 	int cpu;
1637cfda7bb9SAndy Lutomirski 
1638b3edfda4SBorislav Petkov 	if (!boot_cpu_has(X86_FEATURE_SEP))
1639b3edfda4SBorislav Petkov 		return;
1640b3edfda4SBorislav Petkov 
16418b6c0ab1SIngo Molnar 	cpu = get_cpu();
1642c482feefSAndy Lutomirski 	tss = &per_cpu(cpu_tss_rw, cpu);
16438b6c0ab1SIngo Molnar 
16448b6c0ab1SIngo Molnar 	/*
1645cf9328ccSAndy Lutomirski 	 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1646cf9328ccSAndy Lutomirski 	 * see the big comment in struct x86_hw_tss's definition.
16478b6c0ab1SIngo Molnar 	 */
1648cfda7bb9SAndy Lutomirski 
1649cfda7bb9SAndy Lutomirski 	tss->x86_tss.ss1 = __KERNEL_CS;
16508b6c0ab1SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
16514fe2d8b1SDave Hansen 	wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0);
16524c8cd0c5SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
16538b6c0ab1SIngo Molnar 
1654cfda7bb9SAndy Lutomirski 	put_cpu();
1655cfda7bb9SAndy Lutomirski }
1656e04d645fSGlauber Costa #endif
1657e04d645fSGlauber Costa 
1658f7627e25SThomas Gleixner void __init identify_boot_cpu(void)
1659f7627e25SThomas Gleixner {
1660f7627e25SThomas Gleixner 	identify_cpu(&boot_cpu_data);
1661102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1662f7627e25SThomas Gleixner 	sysenter_setup();
1663f7627e25SThomas Gleixner 	enable_sep_cpu();
1664102bbe3aSYinghai Lu #endif
1665e0ba94f1SAlex Shi 	cpu_detect_tlb(&boot_cpu_data);
1666873d50d5SKees Cook 	setup_cr_pinning();
166795c5824fSPawan Gupta 
166895c5824fSPawan Gupta 	tsx_init();
1669f7627e25SThomas Gleixner }
1670f7627e25SThomas Gleixner 
1671148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c)
1672f7627e25SThomas Gleixner {
1673f7627e25SThomas Gleixner 	BUG_ON(c == &boot_cpu_data);
1674f7627e25SThomas Gleixner 	identify_cpu(c);
1675102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1676f7627e25SThomas Gleixner 	enable_sep_cpu();
1677102bbe3aSYinghai Lu #endif
1678f7627e25SThomas Gleixner 	mtrr_ap_init();
16799d85eb91SThomas Gleixner 	validate_apic_and_package_id(c);
168077243971SKonrad Rzeszutek Wilk 	x86_spec_ctrl_setup_ap();
16817e5b3c26SMark Gross 	update_srbds_msr();
1682f7627e25SThomas Gleixner }
1683f7627e25SThomas Gleixner 
1684191679fdSAndi Kleen static __init int setup_noclflush(char *arg)
1685191679fdSAndi Kleen {
1686840d2830SH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1687da4aaa7dSH. Peter Anvin 	setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1688191679fdSAndi Kleen 	return 1;
1689191679fdSAndi Kleen }
1690191679fdSAndi Kleen __setup("noclflush", setup_noclflush);
1691191679fdSAndi Kleen 
1692148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c)
1693f7627e25SThomas Gleixner {
169402dde8b4SJan Beulich 	const char *vendor = NULL;
1695f7627e25SThomas Gleixner 
16960f3fa48aSIngo Molnar 	if (c->x86_vendor < X86_VENDOR_NUM) {
1697f7627e25SThomas Gleixner 		vendor = this_cpu->c_vendor;
16980f3fa48aSIngo Molnar 	} else {
16990f3fa48aSIngo Molnar 		if (c->cpuid_level >= 0)
1700f7627e25SThomas Gleixner 			vendor = c->x86_vendor_id;
17010f3fa48aSIngo Molnar 	}
1702f7627e25SThomas Gleixner 
1703bd32a8cfSYinghai Lu 	if (vendor && !strstr(c->x86_model_id, vendor))
17041b74dde7SChen Yucong 		pr_cont("%s ", vendor);
1705f7627e25SThomas Gleixner 
17069d31d35bSYinghai Lu 	if (c->x86_model_id[0])
17071b74dde7SChen Yucong 		pr_cont("%s", c->x86_model_id);
1708f7627e25SThomas Gleixner 	else
17091b74dde7SChen Yucong 		pr_cont("%d86", c->x86);
1710f7627e25SThomas Gleixner 
17111b74dde7SChen Yucong 	pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
1712924e101aSBorislav Petkov 
1713b399151cSJia Zhang 	if (c->x86_stepping || c->cpuid_level >= 0)
1714b399151cSJia Zhang 		pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
1715f7627e25SThomas Gleixner 	else
17161b74dde7SChen Yucong 		pr_cont(")\n");
1717f7627e25SThomas Gleixner }
1718f7627e25SThomas Gleixner 
17190c2a3913SAndi Kleen /*
17200c2a3913SAndi Kleen  * clearcpuid= was already parsed in fpu__init_parse_early_param.
17210c2a3913SAndi Kleen  * But we need to keep a dummy __setup around otherwise it would
17220c2a3913SAndi Kleen  * show up as an environment variable for init.
17230c2a3913SAndi Kleen  */
17240c2a3913SAndi Kleen static __init int setup_clearcpuid(char *arg)
1725ac72e788SAndi Kleen {
1726ac72e788SAndi Kleen 	return 1;
1727ac72e788SAndi Kleen }
17280c2a3913SAndi Kleen __setup("clearcpuid=", setup_clearcpuid);
1729ac72e788SAndi Kleen 
1730d5494d4fSYinghai Lu #ifdef CONFIG_X86_64
1731e6401c13SAndy Lutomirski DEFINE_PER_CPU_FIRST(struct fixed_percpu_data,
1732e6401c13SAndy Lutomirski 		     fixed_percpu_data) __aligned(PAGE_SIZE) __visible;
1733e6401c13SAndy Lutomirski EXPORT_PER_CPU_SYMBOL_GPL(fixed_percpu_data);
17340f3fa48aSIngo Molnar 
1735bdf977b3STejun Heo /*
1736a7fcf28dSAndy Lutomirski  * The following percpu variables are hot.  Align current_task to
1737a7fcf28dSAndy Lutomirski  * cacheline size such that they fall in the same cacheline.
1738bdf977b3STejun Heo  */
1739bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1740bdf977b3STejun Heo 	&init_task;
1741bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1742d5494d4fSYinghai Lu 
1743951c2a51SThomas Gleixner DEFINE_PER_CPU(void *, hardirq_stack_ptr);
1744e7f89001SThomas Gleixner DEFINE_PER_CPU(bool, hardirq_stack_inuse);
1745d5494d4fSYinghai Lu 
1746c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1747c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1748c2daa3beSPeter Zijlstra 
1749d5494d4fSYinghai Lu /* May not be marked __init: used by software suspend */
1750d5494d4fSYinghai Lu void syscall_init(void)
1751d5494d4fSYinghai Lu {
175231ac34caSBorislav Petkov 	wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
17538d4b0678SThomas Gleixner 	wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
1754d56fe4bfSIngo Molnar 
1755d56fe4bfSIngo Molnar #ifdef CONFIG_IA32_EMULATION
175647edb651SAndy Lutomirski 	wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
1757a76c7f46SDenys Vlasenko 	/*
1758487d1edbSDenys Vlasenko 	 * This only works on Intel CPUs.
1759487d1edbSDenys Vlasenko 	 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1760487d1edbSDenys Vlasenko 	 * This does not cause SYSENTER to jump to the wrong location, because
1761487d1edbSDenys Vlasenko 	 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1762a76c7f46SDenys Vlasenko 	 */
1763a76c7f46SDenys Vlasenko 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
17648e6b65a1Szhong jiang 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
17658e6b65a1Szhong jiang 		    (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1));
17664c8cd0c5SIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
1767d56fe4bfSIngo Molnar #else
176847edb651SAndy Lutomirski 	wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
17696b51311cSBorislav Petkov 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1770d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1771d56fe4bfSIngo Molnar 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1772d5494d4fSYinghai Lu #endif
1773d5494d4fSYinghai Lu 
1774d5494d4fSYinghai Lu 	/* Flags to clear on syscall */
1775d5494d4fSYinghai Lu 	wrmsrl(MSR_SYSCALL_MASK,
177663bcff2aSH. Peter Anvin 	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
17778c7aa698SAndy Lutomirski 	       X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1778d5494d4fSYinghai Lu }
1779d5494d4fSYinghai Lu 
17800f3fa48aSIngo Molnar #else	/* CONFIG_X86_64 */
1781d5494d4fSYinghai Lu 
1782bdf977b3STejun Heo DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1783bdf977b3STejun Heo EXPORT_PER_CPU_SYMBOL(current_task);
1784c2daa3beSPeter Zijlstra DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1785c2daa3beSPeter Zijlstra EXPORT_PER_CPU_SYMBOL(__preempt_count);
1786bdf977b3STejun Heo 
1787a7fcf28dSAndy Lutomirski /*
1788a7fcf28dSAndy Lutomirski  * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1789a7fcf28dSAndy Lutomirski  * the top of the kernel stack.  Use an extra percpu variable to track the
1790a7fcf28dSAndy Lutomirski  * top of the kernel stack directly.
1791a7fcf28dSAndy Lutomirski  */
1792a7fcf28dSAndy Lutomirski DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1793a7fcf28dSAndy Lutomirski 	(unsigned long)&init_thread_union + THREAD_SIZE;
1794a7fcf28dSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1795a7fcf28dSAndy Lutomirski 
1796050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR
1797*3fb0fdb3SAndy Lutomirski DEFINE_PER_CPU(unsigned long, __stack_chk_guard);
1798*3fb0fdb3SAndy Lutomirski EXPORT_PER_CPU_SYMBOL(__stack_chk_guard);
179960a5317fSTejun Heo #endif
180060a5317fSTejun Heo 
18010f3fa48aSIngo Molnar #endif	/* CONFIG_X86_64 */
1802f7627e25SThomas Gleixner 
1803f7627e25SThomas Gleixner /*
18049766cdbcSJaswinder Singh Rajput  * Clear all 6 debug registers:
18059766cdbcSJaswinder Singh Rajput  */
18069766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void)
18079766cdbcSJaswinder Singh Rajput {
18089766cdbcSJaswinder Singh Rajput 	int i;
18099766cdbcSJaswinder Singh Rajput 
18109766cdbcSJaswinder Singh Rajput 	for (i = 0; i < 8; i++) {
18119766cdbcSJaswinder Singh Rajput 		/* Ignore db4, db5 */
18129766cdbcSJaswinder Singh Rajput 		if ((i == 4) || (i == 5))
18139766cdbcSJaswinder Singh Rajput 			continue;
18149766cdbcSJaswinder Singh Rajput 
18159766cdbcSJaswinder Singh Rajput 		set_debugreg(0, i);
18169766cdbcSJaswinder Singh Rajput 	}
18179766cdbcSJaswinder Singh Rajput }
1818f7627e25SThomas Gleixner 
18190bb9fef9SJason Wessel #ifdef CONFIG_KGDB
18200bb9fef9SJason Wessel /*
18210bb9fef9SJason Wessel  * Restore debug regs if using kgdbwait and you have a kernel debugger
18220bb9fef9SJason Wessel  * connection established.
18230bb9fef9SJason Wessel  */
18240bb9fef9SJason Wessel static void dbg_restore_debug_regs(void)
18250bb9fef9SJason Wessel {
18260bb9fef9SJason Wessel 	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
18270bb9fef9SJason Wessel 		arch_kgdb_ops.correct_hw_break();
18280bb9fef9SJason Wessel }
18290bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */
18300bb9fef9SJason Wessel #define dbg_restore_debug_regs()
18310bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */
18320bb9fef9SJason Wessel 
1833ce4b1b16SIgor Mammedov static void wait_for_master_cpu(int cpu)
1834ce4b1b16SIgor Mammedov {
1835ce4b1b16SIgor Mammedov #ifdef CONFIG_SMP
1836ce4b1b16SIgor Mammedov 	/*
1837ce4b1b16SIgor Mammedov 	 * wait for ACK from master CPU before continuing
1838ce4b1b16SIgor Mammedov 	 * with AP initialization
1839ce4b1b16SIgor Mammedov 	 */
1840ce4b1b16SIgor Mammedov 	WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1841ce4b1b16SIgor Mammedov 	while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1842ce4b1b16SIgor Mammedov 		cpu_relax();
1843ce4b1b16SIgor Mammedov #endif
1844ce4b1b16SIgor Mammedov }
1845ce4b1b16SIgor Mammedov 
1846b2e2ba57SChang S. Bae #ifdef CONFIG_X86_64
1847505b7899SThomas Gleixner static inline void setup_getcpu(int cpu)
1848b2e2ba57SChang S. Bae {
184922245bdfSIngo Molnar 	unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
1850b2e2ba57SChang S. Bae 	struct desc_struct d = { };
1851b2e2ba57SChang S. Bae 
185267e87d43SBorislav Petkov 	if (boot_cpu_has(X86_FEATURE_RDTSCP))
1853b2e2ba57SChang S. Bae 		write_rdtscp_aux(cpudata);
1854b2e2ba57SChang S. Bae 
1855b2e2ba57SChang S. Bae 	/* Store CPU and node number in limit. */
1856b2e2ba57SChang S. Bae 	d.limit0 = cpudata;
1857b2e2ba57SChang S. Bae 	d.limit1 = cpudata >> 16;
1858b2e2ba57SChang S. Bae 
1859b2e2ba57SChang S. Bae 	d.type = 5;		/* RO data, expand down, accessed */
1860b2e2ba57SChang S. Bae 	d.dpl = 3;		/* Visible to user code */
1861b2e2ba57SChang S. Bae 	d.s = 1;		/* Not a system segment */
1862b2e2ba57SChang S. Bae 	d.p = 1;		/* Present */
1863b2e2ba57SChang S. Bae 	d.d = 1;		/* 32-bit */
1864b2e2ba57SChang S. Bae 
186522245bdfSIngo Molnar 	write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S);
1866b2e2ba57SChang S. Bae }
1867505b7899SThomas Gleixner 
1868505b7899SThomas Gleixner static inline void ucode_cpu_init(int cpu)
1869505b7899SThomas Gleixner {
1870505b7899SThomas Gleixner 	if (cpu)
1871505b7899SThomas Gleixner 		load_ucode_ap();
1872505b7899SThomas Gleixner }
1873505b7899SThomas Gleixner 
1874505b7899SThomas Gleixner static inline void tss_setup_ist(struct tss_struct *tss)
1875505b7899SThomas Gleixner {
1876505b7899SThomas Gleixner 	/* Set up the per-CPU TSS IST stacks */
1877505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
1878505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
1879505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
1880505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
188102772fb9SJoerg Roedel 	/* Only mapped when SEV-ES is active */
188202772fb9SJoerg Roedel 	tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC);
1883505b7899SThomas Gleixner }
1884505b7899SThomas Gleixner 
1885505b7899SThomas Gleixner #else /* CONFIG_X86_64 */
1886505b7899SThomas Gleixner 
1887505b7899SThomas Gleixner static inline void setup_getcpu(int cpu) { }
1888505b7899SThomas Gleixner 
1889505b7899SThomas Gleixner static inline void ucode_cpu_init(int cpu)
1890505b7899SThomas Gleixner {
1891505b7899SThomas Gleixner 	show_ucode_info_early();
1892505b7899SThomas Gleixner }
1893505b7899SThomas Gleixner 
1894505b7899SThomas Gleixner static inline void tss_setup_ist(struct tss_struct *tss) { }
1895505b7899SThomas Gleixner 
1896505b7899SThomas Gleixner #endif /* !CONFIG_X86_64 */
1897b2e2ba57SChang S. Bae 
1898111e7b15SThomas Gleixner static inline void tss_setup_io_bitmap(struct tss_struct *tss)
1899111e7b15SThomas Gleixner {
1900111e7b15SThomas Gleixner 	tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID;
1901111e7b15SThomas Gleixner 
1902111e7b15SThomas Gleixner #ifdef CONFIG_X86_IOPL_IOPERM
1903111e7b15SThomas Gleixner 	tss->io_bitmap.prev_max = 0;
1904111e7b15SThomas Gleixner 	tss->io_bitmap.prev_sequence = 0;
1905111e7b15SThomas Gleixner 	memset(tss->io_bitmap.bitmap, 0xff, sizeof(tss->io_bitmap.bitmap));
1906111e7b15SThomas Gleixner 	/*
1907111e7b15SThomas Gleixner 	 * Invalidate the extra array entry past the end of the all
1908111e7b15SThomas Gleixner 	 * permission bitmap as required by the hardware.
1909111e7b15SThomas Gleixner 	 */
1910111e7b15SThomas Gleixner 	tss->io_bitmap.mapall[IO_BITMAP_LONGS] = ~0UL;
1911111e7b15SThomas Gleixner #endif
1912111e7b15SThomas Gleixner }
1913f7627e25SThomas Gleixner 
1914f7627e25SThomas Gleixner /*
1915520d0308SJoerg Roedel  * Setup everything needed to handle exceptions from the IDT, including the IST
1916520d0308SJoerg Roedel  * exceptions which use paranoid_entry().
1917520d0308SJoerg Roedel  */
1918520d0308SJoerg Roedel void cpu_init_exception_handling(void)
1919520d0308SJoerg Roedel {
1920520d0308SJoerg Roedel 	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
1921520d0308SJoerg Roedel 	int cpu = raw_smp_processor_id();
1922520d0308SJoerg Roedel 
1923520d0308SJoerg Roedel 	/* paranoid_entry() gets the CPU number from the GDT */
1924520d0308SJoerg Roedel 	setup_getcpu(cpu);
1925520d0308SJoerg Roedel 
1926520d0308SJoerg Roedel 	/* IST vectors need TSS to be set up. */
1927520d0308SJoerg Roedel 	tss_setup_ist(tss);
1928520d0308SJoerg Roedel 	tss_setup_io_bitmap(tss);
1929520d0308SJoerg Roedel 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
1930520d0308SJoerg Roedel 
1931520d0308SJoerg Roedel 	load_TR_desc();
1932520d0308SJoerg Roedel 
1933520d0308SJoerg Roedel 	/* Finally load the IDT */
1934520d0308SJoerg Roedel 	load_current_idt();
1935520d0308SJoerg Roedel }
1936520d0308SJoerg Roedel 
1937520d0308SJoerg Roedel /*
1938f7627e25SThomas Gleixner  * cpu_init() initializes state that is per-CPU. Some data is already
1939f7627e25SThomas Gleixner  * initialized (naturally) in the bootstrap process, such as the GDT
1940f7627e25SThomas Gleixner  * and IDT. We reload them nevertheless, this function acts as a
1941f7627e25SThomas Gleixner  * 'CPU state barrier', nothing should get across.
1942f7627e25SThomas Gleixner  */
1943148f9bb8SPaul Gortmaker void cpu_init(void)
19441ba76586SYinghai Lu {
1945505b7899SThomas Gleixner 	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
1946505b7899SThomas Gleixner 	struct task_struct *cur = current;
1947f6ef7322SThomas Gleixner 	int cpu = raw_smp_processor_id();
19481ba76586SYinghai Lu 
1949ce4b1b16SIgor Mammedov 	wait_for_master_cpu(cpu);
1950ce4b1b16SIgor Mammedov 
1951505b7899SThomas Gleixner 	ucode_cpu_init(cpu);
19520f3fa48aSIngo Molnar 
1953e7a22c1eSBrian Gerst #ifdef CONFIG_NUMA
195427fd185fSFenghua Yu 	if (this_cpu_read(numa_node) == 0 &&
1955e534c7c5SLee Schermerhorn 	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
1956e534c7c5SLee Schermerhorn 		set_numa_node(early_cpu_to_node(cpu));
1957e7a22c1eSBrian Gerst #endif
1958b2e2ba57SChang S. Bae 	setup_getcpu(cpu);
19591ba76586SYinghai Lu 
19602eaad1fdSMike Travis 	pr_debug("Initializing CPU#%d\n", cpu);
19611ba76586SYinghai Lu 
1962505b7899SThomas Gleixner 	if (IS_ENABLED(CONFIG_X86_64) || cpu_feature_enabled(X86_FEATURE_VME) ||
1963505b7899SThomas Gleixner 	    boot_cpu_has(X86_FEATURE_TSC) || boot_cpu_has(X86_FEATURE_DE))
1964375074ccSAndy Lutomirski 		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
19651ba76586SYinghai Lu 
19661ba76586SYinghai Lu 	/*
19671ba76586SYinghai Lu 	 * Initialize the per-CPU GDT with the boot GDT,
19681ba76586SYinghai Lu 	 * and set up the GDT descriptor:
19691ba76586SYinghai Lu 	 */
1970552be871SBrian Gerst 	switch_to_new_gdt(cpu);
1971cf910e83SSeiji Aguchi 	load_current_idt();
19721ba76586SYinghai Lu 
1973505b7899SThomas Gleixner 	if (IS_ENABLED(CONFIG_X86_64)) {
1974505b7899SThomas Gleixner 		loadsegment(fs, 0);
1975505b7899SThomas Gleixner 		memset(cur->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
19761ba76586SYinghai Lu 		syscall_init();
19771ba76586SYinghai Lu 
19781ba76586SYinghai Lu 		wrmsrl(MSR_FS_BASE, 0);
19791ba76586SYinghai Lu 		wrmsrl(MSR_KERNEL_GS_BASE, 0);
19801ba76586SYinghai Lu 		barrier();
19811ba76586SYinghai Lu 
1982659006bfSThomas Gleixner 		x2apic_setup();
19831ba76586SYinghai Lu 	}
19841ba76586SYinghai Lu 
1985f1f10076SVegard Nossum 	mmgrab(&init_mm);
1986505b7899SThomas Gleixner 	cur->active_mm = &init_mm;
1987505b7899SThomas Gleixner 	BUG_ON(cur->mm);
198872c0098dSAndy Lutomirski 	initialize_tlbstate_and_flush();
1989505b7899SThomas Gleixner 	enter_lazy_tlb(&init_mm, cur);
19901ba76586SYinghai Lu 
1991505b7899SThomas Gleixner 	/* Initialize the TSS. */
1992505b7899SThomas Gleixner 	tss_setup_ist(tss);
1993111e7b15SThomas Gleixner 	tss_setup_io_bitmap(tss);
199472f5e08dSAndy Lutomirski 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
1995505b7899SThomas Gleixner 
19961ba76586SYinghai Lu 	load_TR_desc();
1997505b7899SThomas Gleixner 	/*
1998505b7899SThomas Gleixner 	 * sp0 points to the entry trampoline stack regardless of what task
1999505b7899SThomas Gleixner 	 * is running.
2000505b7899SThomas Gleixner 	 */
20014fe2d8b1SDave Hansen 	load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
200220bb8344SAndy Lutomirski 
200337868fe1SAndy Lutomirski 	load_mm_ldt(&init_mm);
20041ba76586SYinghai Lu 
20059766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
20060bb9fef9SJason Wessel 	dbg_restore_debug_regs();
20071ba76586SYinghai Lu 
2008dc4e0021SAndy Lutomirski 	doublefault_init_cpu_tss();
2009505b7899SThomas Gleixner 
201021c4cd10SIngo Molnar 	fpu__init_cpu();
20111ba76586SYinghai Lu 
20121ba76586SYinghai Lu 	if (is_uv_system())
20131ba76586SYinghai Lu 		uv_cpu_init();
201469218e47SThomas Garnier 
201569218e47SThomas Garnier 	load_fixmap_gdt(cpu);
20161ba76586SYinghai Lu }
20171ba76586SYinghai Lu 
20181008c52cSBorislav Petkov /*
20191008c52cSBorislav Petkov  * The microcode loader calls this upon late microcode load to recheck features,
20201008c52cSBorislav Petkov  * only when microcode has been updated. Caller holds microcode_mutex and CPU
20211008c52cSBorislav Petkov  * hotplug lock.
20221008c52cSBorislav Petkov  */
20231008c52cSBorislav Petkov void microcode_check(void)
20241008c52cSBorislav Petkov {
202542ca8082SBorislav Petkov 	struct cpuinfo_x86 info;
202642ca8082SBorislav Petkov 
20271008c52cSBorislav Petkov 	perf_check_microcode();
202842ca8082SBorislav Petkov 
202942ca8082SBorislav Petkov 	/* Reload CPUID max function as it might've changed. */
203042ca8082SBorislav Petkov 	info.cpuid_level = cpuid_eax(0);
203142ca8082SBorislav Petkov 
203242ca8082SBorislav Petkov 	/*
203342ca8082SBorislav Petkov 	 * Copy all capability leafs to pick up the synthetic ones so that
203442ca8082SBorislav Petkov 	 * memcmp() below doesn't fail on that. The ones coming from CPUID will
203542ca8082SBorislav Petkov 	 * get overwritten in get_cpu_cap().
203642ca8082SBorislav Petkov 	 */
203742ca8082SBorislav Petkov 	memcpy(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability));
203842ca8082SBorislav Petkov 
203942ca8082SBorislav Petkov 	get_cpu_cap(&info);
204042ca8082SBorislav Petkov 
204142ca8082SBorislav Petkov 	if (!memcmp(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability)))
204242ca8082SBorislav Petkov 		return;
204342ca8082SBorislav Petkov 
204442ca8082SBorislav Petkov 	pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
204542ca8082SBorislav Petkov 	pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
20461008c52cSBorislav Petkov }
20479c92374bSThomas Gleixner 
20489c92374bSThomas Gleixner /*
20499c92374bSThomas Gleixner  * Invoked from core CPU hotplug code after hotplug operations
20509c92374bSThomas Gleixner  */
20519c92374bSThomas Gleixner void arch_smt_update(void)
20529c92374bSThomas Gleixner {
20539c92374bSThomas Gleixner 	/* Handle the speculative execution misfeatures */
20549c92374bSThomas Gleixner 	cpu_bugs_smt_update();
20556a1cb5f5SThomas Gleixner 	/* Check whether IPI broadcasting can be enabled */
20566a1cb5f5SThomas Gleixner 	apic_smt_update();
20579c92374bSThomas Gleixner }
2058