xref: /linux/arch/x86/kernel/cpu/common.c (revision 86e6b1547b3d013bc392adf775b89318441403c2)
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>
21439e1757SThomas Gleixner #include <linux/mem_encrypt.h>
229766cdbcSJaswinder Singh Rajput #include <linux/smp.h>
237c7077a7SThomas Gleixner #include <linux/cpu.h>
249766cdbcSJaswinder Singh Rajput #include <linux/io.h>
25b51ef52dSLaura Abbott #include <linux/syscore_ops.h>
2665fddcfcSMike Rapoport #include <linux/pgtable.h>
27b3883a9aSJason A. Donenfeld #include <linux/stackprotector.h>
287c7077a7SThomas Gleixner #include <linux/utsname.h>
299766cdbcSJaswinder Singh Rajput 
307c7077a7SThomas Gleixner #include <asm/alternative.h>
311ef5423aSMike Hommey #include <asm/cmdline.h>
32cdd6c482SIngo Molnar #include <asm/perf_event.h>
33f7627e25SThomas Gleixner #include <asm/mmu_context.h>
34dc4e0021SAndy Lutomirski #include <asm/doublefault.h>
3549d859d7SH. Peter Anvin #include <asm/archrandom.h>
369766cdbcSJaswinder Singh Rajput #include <asm/hypervisor.h>
379766cdbcSJaswinder Singh Rajput #include <asm/processor.h>
381e02ce4cSAndy Lutomirski #include <asm/tlbflush.h>
39f649e938SPaul Gortmaker #include <asm/debugreg.h>
409766cdbcSJaswinder Singh Rajput #include <asm/sections.h>
41f40c3300SAndy Lutomirski #include <asm/vsyscall.h>
428bdbd962SAlan Cox #include <linux/topology.h>
438bdbd962SAlan Cox #include <linux/cpumask.h>
4460063497SArun Sharma #include <linux/atomic.h>
459766cdbcSJaswinder Singh Rajput #include <asm/proto.h>
469766cdbcSJaswinder Singh Rajput #include <asm/setup.h>
47f7627e25SThomas Gleixner #include <asm/apic.h>
489766cdbcSJaswinder Singh Rajput #include <asm/desc.h>
49b56d2795SThomas Gleixner #include <asm/fpu/api.h>
509766cdbcSJaswinder Singh Rajput #include <asm/mtrr.h>
510274f955SGrzegorz Andrejczuk #include <asm/hwcap2.h>
528bdbd962SAlan Cox #include <linux/numa.h>
530cd39f46SPeter Zijlstra #include <asm/numa.h>
549766cdbcSJaswinder Singh Rajput #include <asm/asm.h>
550f6ff2bcSDave Hansen #include <asm/bugs.h>
569766cdbcSJaswinder Singh Rajput #include <asm/cpu.h>
579766cdbcSJaswinder Singh Rajput #include <asm/mce.h>
589766cdbcSJaswinder Singh Rajput #include <asm/msr.h>
590b9a6a8bSJuergen Gross #include <asm/cacheinfo.h>
60eb243d1dSIngo Molnar #include <asm/memtype.h>
61d288e1cfSFenghua Yu #include <asm/microcode.h>
62fec9434aSDavid Woodhouse #include <asm/intel-family.h>
63fec9434aSDavid Woodhouse #include <asm/cpu_device_id.h>
64208d8c79SH. Peter Anvin (Intel) #include <asm/fred.h>
65bdbcdd48STejun Heo #include <asm/uv/uv.h>
6661382281SNikolay Borisov #include <asm/ia32.h>
677c7077a7SThomas Gleixner #include <asm/set_memory.h>
68991625f3SPeter Zijlstra #include <asm/traps.h>
6995d33bfaSBrijesh Singh #include <asm/sev.h>
70765a0542SKai Huang #include <asm/tdx.h>
7143650dcfSJacob Pan #include <asm/posted_intr.h>
72*86e6b154SLinus Torvalds #include <asm/runtime-const.h>
73f7627e25SThomas Gleixner 
74f7627e25SThomas Gleixner #include "cpu.h"
75f7627e25SThomas Gleixner 
7671eb4893SThomas Gleixner DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
7771eb4893SThomas Gleixner EXPORT_PER_CPU_SYMBOL(cpu_info);
7871eb4893SThomas Gleixner 
790274f955SGrzegorz Andrejczuk u32 elf_hwcap2 __read_mostly;
800274f955SGrzegorz Andrejczuk 
81f8b64d08SBorislav Petkov /* Number of siblings per CPU package */
828078f4d6SThomas Gleixner unsigned int __max_threads_per_core __ro_after_init = 1;
838078f4d6SThomas Gleixner EXPORT_SYMBOL(__max_threads_per_core);
84f8b64d08SBorislav Petkov 
85090610baSThomas Gleixner unsigned int __max_dies_per_package __ro_after_init = 1;
86090610baSThomas Gleixner EXPORT_SYMBOL(__max_dies_per_package);
87090610baSThomas Gleixner 
88090610baSThomas Gleixner unsigned int __max_logical_packages __ro_after_init = 1;
89090610baSThomas Gleixner EXPORT_SYMBOL(__max_logical_packages);
9092853a77SThomas Gleixner 
91fd43b8aeSThomas Gleixner unsigned int __num_cores_per_package __ro_after_init = 1;
92fd43b8aeSThomas Gleixner EXPORT_SYMBOL(__num_cores_per_package);
93fd43b8aeSThomas Gleixner 
94fd43b8aeSThomas Gleixner unsigned int __num_threads_per_package __ro_after_init = 1;
95fd43b8aeSThomas Gleixner EXPORT_SYMBOL(__num_threads_per_package);
96fd43b8aeSThomas Gleixner 
970dcab41dSTony Luck static struct ppin_info {
980dcab41dSTony Luck 	int	feature;
990dcab41dSTony Luck 	int	msr_ppin_ctl;
100822ccfadSTony Luck 	int	msr_ppin;
1010dcab41dSTony Luck } ppin_info[] = {
1020dcab41dSTony Luck 	[X86_VENDOR_INTEL] = {
1030dcab41dSTony Luck 		.feature = X86_FEATURE_INTEL_PPIN,
1040dcab41dSTony Luck 		.msr_ppin_ctl = MSR_PPIN_CTL,
105822ccfadSTony Luck 		.msr_ppin = MSR_PPIN
1060dcab41dSTony Luck 	},
1070dcab41dSTony Luck 	[X86_VENDOR_AMD] = {
1080dcab41dSTony Luck 		.feature = X86_FEATURE_AMD_PPIN,
1090dcab41dSTony Luck 		.msr_ppin_ctl = MSR_AMD_PPIN_CTL,
110822ccfadSTony Luck 		.msr_ppin = MSR_AMD_PPIN
1110dcab41dSTony Luck 	},
1120dcab41dSTony Luck };
1130dcab41dSTony Luck 
1140dcab41dSTony Luck static const struct x86_cpu_id ppin_cpuids[] = {
1150dcab41dSTony Luck 	X86_MATCH_FEATURE(X86_FEATURE_AMD_PPIN, &ppin_info[X86_VENDOR_AMD]),
11600a2f23eSTony Luck 	X86_MATCH_FEATURE(X86_FEATURE_INTEL_PPIN, &ppin_info[X86_VENDOR_INTEL]),
1170dcab41dSTony Luck 
1180dcab41dSTony Luck 	/* Legacy models without CPUID enumeration */
119b24e466aSTony Luck 	X86_MATCH_VFM(INTEL_IVYBRIDGE_X, &ppin_info[X86_VENDOR_INTEL]),
120b24e466aSTony Luck 	X86_MATCH_VFM(INTEL_HASWELL_X, &ppin_info[X86_VENDOR_INTEL]),
121b24e466aSTony Luck 	X86_MATCH_VFM(INTEL_BROADWELL_D, &ppin_info[X86_VENDOR_INTEL]),
122b24e466aSTony Luck 	X86_MATCH_VFM(INTEL_BROADWELL_X, &ppin_info[X86_VENDOR_INTEL]),
123b24e466aSTony Luck 	X86_MATCH_VFM(INTEL_SKYLAKE_X, &ppin_info[X86_VENDOR_INTEL]),
124b24e466aSTony Luck 	X86_MATCH_VFM(INTEL_ICELAKE_X, &ppin_info[X86_VENDOR_INTEL]),
125b24e466aSTony Luck 	X86_MATCH_VFM(INTEL_ICELAKE_D, &ppin_info[X86_VENDOR_INTEL]),
126b24e466aSTony Luck 	X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X, &ppin_info[X86_VENDOR_INTEL]),
127b24e466aSTony Luck 	X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X, &ppin_info[X86_VENDOR_INTEL]),
128b24e466aSTony Luck 	X86_MATCH_VFM(INTEL_XEON_PHI_KNL, &ppin_info[X86_VENDOR_INTEL]),
129b24e466aSTony Luck 	X86_MATCH_VFM(INTEL_XEON_PHI_KNM, &ppin_info[X86_VENDOR_INTEL]),
1300dcab41dSTony Luck 
1310dcab41dSTony Luck 	{}
1320dcab41dSTony Luck };
1330dcab41dSTony Luck 
ppin_init(struct cpuinfo_x86 * c)1340dcab41dSTony Luck static void ppin_init(struct cpuinfo_x86 *c)
1350dcab41dSTony Luck {
1360dcab41dSTony Luck 	const struct x86_cpu_id *id;
1370dcab41dSTony Luck 	unsigned long long val;
1380dcab41dSTony Luck 	struct ppin_info *info;
1390dcab41dSTony Luck 
1400dcab41dSTony Luck 	id = x86_match_cpu(ppin_cpuids);
1410dcab41dSTony Luck 	if (!id)
1420dcab41dSTony Luck 		return;
1430dcab41dSTony Luck 
1440dcab41dSTony Luck 	/*
1450dcab41dSTony Luck 	 * Testing the presence of the MSR is not enough. Need to check
1460dcab41dSTony Luck 	 * that the PPIN_CTL allows reading of the PPIN.
1470dcab41dSTony Luck 	 */
1480dcab41dSTony Luck 	info = (struct ppin_info *)id->driver_data;
1490dcab41dSTony Luck 
1500dcab41dSTony Luck 	if (rdmsrl_safe(info->msr_ppin_ctl, &val))
1510dcab41dSTony Luck 		goto clear_ppin;
1520dcab41dSTony Luck 
1530dcab41dSTony Luck 	if ((val & 3UL) == 1UL) {
1540dcab41dSTony Luck 		/* PPIN locked in disabled mode */
1550dcab41dSTony Luck 		goto clear_ppin;
1560dcab41dSTony Luck 	}
1570dcab41dSTony Luck 
1580dcab41dSTony Luck 	/* If PPIN is disabled, try to enable */
1590dcab41dSTony Luck 	if (!(val & 2UL)) {
1600dcab41dSTony Luck 		wrmsrl_safe(info->msr_ppin_ctl,  val | 2UL);
1610dcab41dSTony Luck 		rdmsrl_safe(info->msr_ppin_ctl, &val);
1620dcab41dSTony Luck 	}
1630dcab41dSTony Luck 
1640dcab41dSTony Luck 	/* Is the enable bit set? */
1650dcab41dSTony Luck 	if (val & 2UL) {
166822ccfadSTony Luck 		c->ppin = __rdmsr(info->msr_ppin);
1670dcab41dSTony Luck 		set_cpu_cap(c, info->feature);
1680dcab41dSTony Luck 		return;
1690dcab41dSTony Luck 	}
1700dcab41dSTony Luck 
1710dcab41dSTony Luck clear_ppin:
1720dcab41dSTony Luck 	clear_cpu_cap(c, info->feature);
1730dcab41dSTony Luck }
1740dcab41dSTony Luck 
default_init(struct cpuinfo_x86 * c)175148f9bb8SPaul Gortmaker static void default_init(struct cpuinfo_x86 *c)
176e8055139SOndrej Zary {
177e8055139SOndrej Zary #ifdef CONFIG_X86_64
17827c13eceSBorislav Petkov 	cpu_detect_cache_sizes(c);
179e8055139SOndrej Zary #else
180e8055139SOndrej Zary 	/* Not much we can do here... */
181e8055139SOndrej Zary 	/* Check if at least it has cpuid */
182e8055139SOndrej Zary 	if (c->cpuid_level == -1) {
183e8055139SOndrej Zary 		/* No cpuid. It must be an ancient CPU */
184e8055139SOndrej Zary 		if (c->x86 == 4)
185e8055139SOndrej Zary 			strcpy(c->x86_model_id, "486");
186e8055139SOndrej Zary 		else if (c->x86 == 3)
187e8055139SOndrej Zary 			strcpy(c->x86_model_id, "386");
188e8055139SOndrej Zary 	}
189e8055139SOndrej Zary #endif
190e8055139SOndrej Zary }
191e8055139SOndrej Zary 
192148f9bb8SPaul Gortmaker static const struct cpu_dev default_cpu = {
193e8055139SOndrej Zary 	.c_init		= default_init,
194e8055139SOndrej Zary 	.c_vendor	= "Unknown",
195e8055139SOndrej Zary 	.c_x86_vendor	= X86_VENDOR_UNKNOWN,
196e8055139SOndrej Zary };
197e8055139SOndrej Zary 
198148f9bb8SPaul Gortmaker static const struct cpu_dev *this_cpu = &default_cpu;
1990a488a53SYinghai Lu 
20006deef89SBrian Gerst DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
201950ad7ffSYinghai Lu #ifdef CONFIG_X86_64
20206deef89SBrian Gerst 	/*
20306deef89SBrian Gerst 	 * We need valid kernel segments for data and code in long mode too
204950ad7ffSYinghai Lu 	 * IRET will check the segment types  kkeil 2000/10/28
205950ad7ffSYinghai Lu 	 * Also sysret mandates a special GDT layout
20606deef89SBrian Gerst 	 *
2079766cdbcSJaswinder Singh Rajput 	 * TLS descriptors are currently at a different place compared to i386.
20806deef89SBrian Gerst 	 * Hopefully nobody expects them at a fixed place (Wine?)
209950ad7ffSYinghai Lu 	 */
2103b184b71SVegard Nossum 	[GDT_ENTRY_KERNEL32_CS]		= GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff),
2113b184b71SVegard Nossum 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(DESC_CODE64, 0, 0xfffff),
2123b184b71SVegard Nossum 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(DESC_DATA64, 0, 0xfffff),
2133b184b71SVegard Nossum 	[GDT_ENTRY_DEFAULT_USER32_CS]	= GDT_ENTRY_INIT(DESC_CODE32 | DESC_USER, 0, 0xfffff),
2143b184b71SVegard Nossum 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(DESC_DATA64 | DESC_USER, 0, 0xfffff),
2153b184b71SVegard Nossum 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(DESC_CODE64 | DESC_USER, 0, 0xfffff),
216950ad7ffSYinghai Lu #else
2171445f6e1SVegard Nossum 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff),
2181445f6e1SVegard Nossum 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
2191445f6e1SVegard Nossum 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(DESC_CODE32 | DESC_USER, 0, 0xfffff),
2201445f6e1SVegard Nossum 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(DESC_DATA32 | DESC_USER, 0, 0xfffff),
221f7627e25SThomas Gleixner 	/*
222f7627e25SThomas Gleixner 	 * Segments used for calling PnP BIOS have byte granularity.
223f7627e25SThomas Gleixner 	 * They code segments and data segments have fixed 64k limits,
224f7627e25SThomas Gleixner 	 * the transfer segment sizes are set at run time.
225f7627e25SThomas Gleixner 	 */
2261445f6e1SVegard Nossum 	[GDT_ENTRY_PNPBIOS_CS32]	= GDT_ENTRY_INIT(DESC_CODE32_BIOS, 0, 0xffff),
2271445f6e1SVegard Nossum 	[GDT_ENTRY_PNPBIOS_CS16]	= GDT_ENTRY_INIT(DESC_CODE16, 0, 0xffff),
2281445f6e1SVegard Nossum 	[GDT_ENTRY_PNPBIOS_DS]		= GDT_ENTRY_INIT(DESC_DATA16, 0, 0xffff),
2291445f6e1SVegard Nossum 	[GDT_ENTRY_PNPBIOS_TS1]		= GDT_ENTRY_INIT(DESC_DATA16, 0, 0),
2301445f6e1SVegard Nossum 	[GDT_ENTRY_PNPBIOS_TS2]		= GDT_ENTRY_INIT(DESC_DATA16, 0, 0),
231f7627e25SThomas Gleixner 	/*
232f7627e25SThomas Gleixner 	 * The APM segments have byte granularity and their bases
233f7627e25SThomas Gleixner 	 * are set at run time.  All have 64k limits.
234f7627e25SThomas Gleixner 	 */
2351445f6e1SVegard Nossum 	[GDT_ENTRY_APMBIOS_BASE]	= GDT_ENTRY_INIT(DESC_CODE32_BIOS, 0, 0xffff),
2361445f6e1SVegard Nossum 	[GDT_ENTRY_APMBIOS_BASE+1]	= GDT_ENTRY_INIT(DESC_CODE16, 0, 0xffff),
2371445f6e1SVegard Nossum 	[GDT_ENTRY_APMBIOS_BASE+2]	= GDT_ENTRY_INIT(DESC_DATA32_BIOS, 0, 0xffff),
238f7627e25SThomas Gleixner 
2391445f6e1SVegard Nossum 	[GDT_ENTRY_ESPFIX_SS]		= GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
2401445f6e1SVegard Nossum 	[GDT_ENTRY_PERCPU]		= GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
241950ad7ffSYinghai Lu #endif
24206deef89SBrian Gerst } };
243f7627e25SThomas Gleixner EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
244f7627e25SThomas Gleixner 
2450790c9aaSAndy Lutomirski #ifdef CONFIG_X86_64
x86_nopcid_setup(char * s)246c7ad5ad2SAndy Lutomirski static int __init x86_nopcid_setup(char *s)
2470790c9aaSAndy Lutomirski {
248c7ad5ad2SAndy Lutomirski 	/* nopcid doesn't accept parameters */
249c7ad5ad2SAndy Lutomirski 	if (s)
250c7ad5ad2SAndy Lutomirski 		return -EINVAL;
2510790c9aaSAndy Lutomirski 
2520790c9aaSAndy Lutomirski 	/* do not emit a message if the feature is not present */
2530790c9aaSAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_PCID))
254c7ad5ad2SAndy Lutomirski 		return 0;
2550790c9aaSAndy Lutomirski 
2560790c9aaSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_PCID);
2570790c9aaSAndy Lutomirski 	pr_info("nopcid: PCID feature disabled\n");
258c7ad5ad2SAndy Lutomirski 	return 0;
2590790c9aaSAndy Lutomirski }
260c7ad5ad2SAndy Lutomirski early_param("nopcid", x86_nopcid_setup);
2610790c9aaSAndy Lutomirski #endif
2620790c9aaSAndy Lutomirski 
x86_noinvpcid_setup(char * s)263d12a72b8SAndy Lutomirski static int __init x86_noinvpcid_setup(char *s)
264d12a72b8SAndy Lutomirski {
265d12a72b8SAndy Lutomirski 	/* noinvpcid doesn't accept parameters */
266d12a72b8SAndy Lutomirski 	if (s)
267d12a72b8SAndy Lutomirski 		return -EINVAL;
268d12a72b8SAndy Lutomirski 
269d12a72b8SAndy Lutomirski 	/* do not emit a message if the feature is not present */
270d12a72b8SAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_INVPCID))
271d12a72b8SAndy Lutomirski 		return 0;
272d12a72b8SAndy Lutomirski 
273d12a72b8SAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_INVPCID);
274d12a72b8SAndy Lutomirski 	pr_info("noinvpcid: INVPCID feature disabled\n");
275d12a72b8SAndy Lutomirski 	return 0;
276d12a72b8SAndy Lutomirski }
277d12a72b8SAndy Lutomirski early_param("noinvpcid", x86_noinvpcid_setup);
278d12a72b8SAndy Lutomirski 
279ba51dcedSYinghai Lu #ifdef CONFIG_X86_32
280148f9bb8SPaul Gortmaker static int cachesize_override = -1;
281148f9bb8SPaul Gortmaker static int disable_x86_serial_nr = 1;
282f7627e25SThomas Gleixner 
cachesize_setup(char * str)283f7627e25SThomas Gleixner static int __init cachesize_setup(char *str)
284f7627e25SThomas Gleixner {
285f7627e25SThomas Gleixner 	get_option(&str, &cachesize_override);
286f7627e25SThomas Gleixner 	return 1;
287f7627e25SThomas Gleixner }
288f7627e25SThomas Gleixner __setup("cachesize=", cachesize_setup);
289f7627e25SThomas Gleixner 
290f7627e25SThomas Gleixner /* Standard macro to see if a specific flag is changeable */
flag_is_changeable_p(u32 flag)291f7627e25SThomas Gleixner static inline int flag_is_changeable_p(u32 flag)
292f7627e25SThomas Gleixner {
293f7627e25SThomas Gleixner 	u32 f1, f2;
294f7627e25SThomas Gleixner 
29594f6bac1SKrzysztof Helt 	/*
29694f6bac1SKrzysztof Helt 	 * Cyrix and IDT cpus allow disabling of CPUID
29794f6bac1SKrzysztof Helt 	 * so the code below may return different results
29894f6bac1SKrzysztof Helt 	 * when it is executed before and after enabling
29994f6bac1SKrzysztof Helt 	 * the CPUID. Add "volatile" to not allow gcc to
30094f6bac1SKrzysztof Helt 	 * optimize the subsequent calls to this function.
30194f6bac1SKrzysztof Helt 	 */
30294f6bac1SKrzysztof Helt 	asm volatile ("pushfl		\n\t"
303f7627e25SThomas Gleixner 		      "pushfl		\n\t"
304f7627e25SThomas Gleixner 		      "popl %0		\n\t"
305f7627e25SThomas Gleixner 		      "movl %0, %1	\n\t"
306f7627e25SThomas Gleixner 		      "xorl %2, %0	\n\t"
307f7627e25SThomas Gleixner 		      "pushl %0		\n\t"
308f7627e25SThomas Gleixner 		      "popfl		\n\t"
309f7627e25SThomas Gleixner 		      "pushfl		\n\t"
310f7627e25SThomas Gleixner 		      "popl %0		\n\t"
311f7627e25SThomas Gleixner 		      "popfl		\n\t"
3120f3fa48aSIngo Molnar 
313f7627e25SThomas Gleixner 		      : "=&r" (f1), "=&r" (f2)
314f7627e25SThomas Gleixner 		      : "ir" (flag));
315f7627e25SThomas Gleixner 
316f7627e25SThomas Gleixner 	return ((f1^f2) & flag) != 0;
317f7627e25SThomas Gleixner }
318f7627e25SThomas Gleixner 
319f7627e25SThomas Gleixner /* Probe for the CPUID instruction */
have_cpuid_p(void)320148f9bb8SPaul Gortmaker int have_cpuid_p(void)
321f7627e25SThomas Gleixner {
322f7627e25SThomas Gleixner 	return flag_is_changeable_p(X86_EFLAGS_ID);
323f7627e25SThomas Gleixner }
324f7627e25SThomas Gleixner 
squash_the_stupid_serial_number(struct cpuinfo_x86 * c)325148f9bb8SPaul Gortmaker static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
3260a488a53SYinghai Lu {
3270a488a53SYinghai Lu 	unsigned long lo, hi;
3280f3fa48aSIngo Molnar 
3290f3fa48aSIngo Molnar 	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
3300f3fa48aSIngo Molnar 		return;
3310f3fa48aSIngo Molnar 
3320f3fa48aSIngo Molnar 	/* Disable processor serial number: */
3330f3fa48aSIngo Molnar 
3340a488a53SYinghai Lu 	rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
3350a488a53SYinghai Lu 	lo |= 0x200000;
3360a488a53SYinghai Lu 	wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
3370f3fa48aSIngo Molnar 
3381b74dde7SChen Yucong 	pr_notice("CPU serial number disabled.\n");
3390a488a53SYinghai Lu 	clear_cpu_cap(c, X86_FEATURE_PN);
3400a488a53SYinghai Lu 
3410a488a53SYinghai Lu 	/* Disabling the serial number may affect the cpuid level */
3420a488a53SYinghai Lu 	c->cpuid_level = cpuid_eax(0);
3430a488a53SYinghai Lu }
3440a488a53SYinghai Lu 
x86_serial_nr_setup(char * s)3450a488a53SYinghai Lu static int __init x86_serial_nr_setup(char *s)
3460a488a53SYinghai Lu {
3470a488a53SYinghai Lu 	disable_x86_serial_nr = 0;
3480a488a53SYinghai Lu 	return 1;
3490a488a53SYinghai Lu }
3500a488a53SYinghai Lu __setup("serialnumber", x86_serial_nr_setup);
351ba51dcedSYinghai Lu #else
flag_is_changeable_p(u32 flag)352102bbe3aSYinghai Lu static inline int flag_is_changeable_p(u32 flag)
353102bbe3aSYinghai Lu {
354102bbe3aSYinghai Lu 	return 1;
355102bbe3aSYinghai Lu }
squash_the_stupid_serial_number(struct cpuinfo_x86 * c)356102bbe3aSYinghai Lu static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
357102bbe3aSYinghai Lu {
358102bbe3aSYinghai Lu }
359ba51dcedSYinghai Lu #endif
3600a488a53SYinghai Lu 
setup_smep(struct cpuinfo_x86 * c)361b2cc2a07SH. Peter Anvin static __always_inline void setup_smep(struct cpuinfo_x86 *c)
362de5397adSFenghua Yu {
363b2cc2a07SH. Peter Anvin 	if (cpu_has(c, X86_FEATURE_SMEP))
364375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMEP);
365de5397adSFenghua Yu }
366de5397adSFenghua Yu 
setup_smap(struct cpuinfo_x86 * c)367b2cc2a07SH. Peter Anvin static __always_inline void setup_smap(struct cpuinfo_x86 *c)
36852b6179aSH. Peter Anvin {
369581b7f15SAndrew Cooper 	unsigned long eflags = native_save_fl();
370b2cc2a07SH. Peter Anvin 
371b2cc2a07SH. Peter Anvin 	/* This should have been cleared long ago */
372b2cc2a07SH. Peter Anvin 	BUG_ON(eflags & X86_EFLAGS_AC);
373b2cc2a07SH. Peter Anvin 
374dbae0a93SBorislav Petkov 	if (cpu_has(c, X86_FEATURE_SMAP))
375375074ccSAndy Lutomirski 		cr4_set_bits(X86_CR4_SMAP);
376f7627e25SThomas Gleixner }
377f7627e25SThomas Gleixner 
setup_umip(struct cpuinfo_x86 * c)378aa35f896SRicardo Neri static __always_inline void setup_umip(struct cpuinfo_x86 *c)
379aa35f896SRicardo Neri {
380aa35f896SRicardo Neri 	/* Check the boot processor, plus build option for UMIP. */
381aa35f896SRicardo Neri 	if (!cpu_feature_enabled(X86_FEATURE_UMIP))
382aa35f896SRicardo Neri 		goto out;
383aa35f896SRicardo Neri 
384aa35f896SRicardo Neri 	/* Check the current processor's cpuid bits. */
385aa35f896SRicardo Neri 	if (!cpu_has(c, X86_FEATURE_UMIP))
386aa35f896SRicardo Neri 		goto out;
387aa35f896SRicardo Neri 
388aa35f896SRicardo Neri 	cr4_set_bits(X86_CR4_UMIP);
389aa35f896SRicardo Neri 
390438cbf88SLendacky, Thomas 	pr_info_once("x86/cpu: User Mode Instruction Prevention (UMIP) activated\n");
391770c7755SRicardo Neri 
392aa35f896SRicardo Neri 	return;
393aa35f896SRicardo Neri 
394aa35f896SRicardo Neri out:
395aa35f896SRicardo Neri 	/*
396aa35f896SRicardo Neri 	 * Make sure UMIP is disabled in case it was enabled in a
397aa35f896SRicardo Neri 	 * previous boot (e.g., via kexec).
398aa35f896SRicardo Neri 	 */
399aa35f896SRicardo Neri 	cr4_clear_bits(X86_CR4_UMIP);
400aa35f896SRicardo Neri }
401aa35f896SRicardo Neri 
402a13b9d0bSKees Cook /* These bits should not change their value after CPU init is finished. */
403ff45746fSH. Peter Anvin (Intel) static const unsigned long cr4_pinned_mask = X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP |
404ff45746fSH. Peter Anvin (Intel) 					     X86_CR4_FSGSBASE | X86_CR4_CET | X86_CR4_FRED;
4057652ac92SThomas Gleixner static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
4067652ac92SThomas Gleixner static unsigned long cr4_pinned_bits __ro_after_init;
4077652ac92SThomas Gleixner 
native_write_cr0(unsigned long val)4087652ac92SThomas Gleixner void native_write_cr0(unsigned long val)
4097652ac92SThomas Gleixner {
4107652ac92SThomas Gleixner 	unsigned long bits_missing = 0;
4117652ac92SThomas Gleixner 
4127652ac92SThomas Gleixner set_register:
413aa5cacdcSArvind Sankar 	asm volatile("mov %0,%%cr0": "+r" (val) : : "memory");
4147652ac92SThomas Gleixner 
4157652ac92SThomas Gleixner 	if (static_branch_likely(&cr_pinning)) {
4167652ac92SThomas Gleixner 		if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) {
4177652ac92SThomas Gleixner 			bits_missing = X86_CR0_WP;
4187652ac92SThomas Gleixner 			val |= bits_missing;
4197652ac92SThomas Gleixner 			goto set_register;
4207652ac92SThomas Gleixner 		}
4217652ac92SThomas Gleixner 		/* Warn after we've set the missing bits. */
4227652ac92SThomas Gleixner 		WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n");
4237652ac92SThomas Gleixner 	}
4247652ac92SThomas Gleixner }
4257652ac92SThomas Gleixner EXPORT_SYMBOL(native_write_cr0);
4267652ac92SThomas Gleixner 
native_write_cr4(unsigned long val)427b64dfcdeSBorislav Petkov void __no_profile native_write_cr4(unsigned long val)
4287652ac92SThomas Gleixner {
429a13b9d0bSKees Cook 	unsigned long bits_changed = 0;
4307652ac92SThomas Gleixner 
4317652ac92SThomas Gleixner set_register:
432aa5cacdcSArvind Sankar 	asm volatile("mov %0,%%cr4": "+r" (val) : : "memory");
4337652ac92SThomas Gleixner 
4347652ac92SThomas Gleixner 	if (static_branch_likely(&cr_pinning)) {
435a13b9d0bSKees Cook 		if (unlikely((val & cr4_pinned_mask) != cr4_pinned_bits)) {
436a13b9d0bSKees Cook 			bits_changed = (val & cr4_pinned_mask) ^ cr4_pinned_bits;
437a13b9d0bSKees Cook 			val = (val & ~cr4_pinned_mask) | cr4_pinned_bits;
4387652ac92SThomas Gleixner 			goto set_register;
4397652ac92SThomas Gleixner 		}
440a13b9d0bSKees Cook 		/* Warn after we've corrected the changed bits. */
441a13b9d0bSKees Cook 		WARN_ONCE(bits_changed, "pinned CR4 bits changed: 0x%lx!?\n",
442a13b9d0bSKees Cook 			  bits_changed);
4437652ac92SThomas Gleixner 	}
4447652ac92SThomas Gleixner }
44521953ee5SThomas Gleixner #if IS_MODULE(CONFIG_LKDTM)
446d8f0b353SThomas Gleixner EXPORT_SYMBOL_GPL(native_write_cr4);
44721953ee5SThomas Gleixner #endif
448d8f0b353SThomas Gleixner 
cr4_update_irqsoff(unsigned long set,unsigned long clear)449d8f0b353SThomas Gleixner void cr4_update_irqsoff(unsigned long set, unsigned long clear)
450d8f0b353SThomas Gleixner {
451d8f0b353SThomas Gleixner 	unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4);
452d8f0b353SThomas Gleixner 
453d8f0b353SThomas Gleixner 	lockdep_assert_irqs_disabled();
454d8f0b353SThomas Gleixner 
455d8f0b353SThomas Gleixner 	newval = (cr4 & ~clear) | set;
456d8f0b353SThomas Gleixner 	if (newval != cr4) {
457d8f0b353SThomas Gleixner 		this_cpu_write(cpu_tlbstate.cr4, newval);
458d8f0b353SThomas Gleixner 		__write_cr4(newval);
459d8f0b353SThomas Gleixner 	}
460d8f0b353SThomas Gleixner }
461d8f0b353SThomas Gleixner EXPORT_SYMBOL(cr4_update_irqsoff);
462d8f0b353SThomas Gleixner 
463d8f0b353SThomas Gleixner /* Read the CR4 shadow. */
cr4_read_shadow(void)464d8f0b353SThomas Gleixner unsigned long cr4_read_shadow(void)
465d8f0b353SThomas Gleixner {
466d8f0b353SThomas Gleixner 	return this_cpu_read(cpu_tlbstate.cr4);
467d8f0b353SThomas Gleixner }
468d8f0b353SThomas Gleixner EXPORT_SYMBOL_GPL(cr4_read_shadow);
4697652ac92SThomas Gleixner 
cr4_init(void)4707652ac92SThomas Gleixner void cr4_init(void)
4717652ac92SThomas Gleixner {
4727652ac92SThomas Gleixner 	unsigned long cr4 = __read_cr4();
4737652ac92SThomas Gleixner 
4747652ac92SThomas Gleixner 	if (boot_cpu_has(X86_FEATURE_PCID))
4757652ac92SThomas Gleixner 		cr4 |= X86_CR4_PCIDE;
4767652ac92SThomas Gleixner 	if (static_branch_likely(&cr_pinning))
477a13b9d0bSKees Cook 		cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits;
4787652ac92SThomas Gleixner 
4797652ac92SThomas Gleixner 	__write_cr4(cr4);
4807652ac92SThomas Gleixner 
4817652ac92SThomas Gleixner 	/* Initialize cr4 shadow for this CPU. */
4827652ac92SThomas Gleixner 	this_cpu_write(cpu_tlbstate.cr4, cr4);
4837652ac92SThomas Gleixner }
484873d50d5SKees Cook 
485873d50d5SKees Cook /*
486873d50d5SKees Cook  * Once CPU feature detection is finished (and boot params have been
487873d50d5SKees Cook  * parsed), record any of the sensitive CR bits that are set, and
488873d50d5SKees Cook  * enable CR pinning.
489873d50d5SKees Cook  */
setup_cr_pinning(void)490873d50d5SKees Cook static void __init setup_cr_pinning(void)
491873d50d5SKees Cook {
492a13b9d0bSKees Cook 	cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask;
493873d50d5SKees Cook 	static_key_enable(&cr_pinning.key);
494873d50d5SKees Cook }
495873d50d5SKees Cook 
x86_nofsgsbase_setup(char * arg)496b745cfbaSAndy Lutomirski static __init int x86_nofsgsbase_setup(char *arg)
497dd649bd0SAndy Lutomirski {
498b745cfbaSAndy Lutomirski 	/* Require an exact match without trailing characters. */
499b745cfbaSAndy Lutomirski 	if (strlen(arg))
500b745cfbaSAndy Lutomirski 		return 0;
501b745cfbaSAndy Lutomirski 
502b745cfbaSAndy Lutomirski 	/* Do not emit a message if the feature is not present. */
503b745cfbaSAndy Lutomirski 	if (!boot_cpu_has(X86_FEATURE_FSGSBASE))
504b745cfbaSAndy Lutomirski 		return 1;
505b745cfbaSAndy Lutomirski 
506b745cfbaSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_FSGSBASE);
507b745cfbaSAndy Lutomirski 	pr_info("FSGSBASE disabled via kernel command line\n");
508dd649bd0SAndy Lutomirski 	return 1;
509dd649bd0SAndy Lutomirski }
510b745cfbaSAndy Lutomirski __setup("nofsgsbase", x86_nofsgsbase_setup);
511dd649bd0SAndy Lutomirski 
512b64ed19bSAndy Lutomirski /*
51306976945SDave Hansen  * Protection Keys are not available in 32-bit mode.
51406976945SDave Hansen  */
51506976945SDave Hansen static bool pku_disabled;
51606976945SDave Hansen 
setup_pku(struct cpuinfo_x86 * c)51706976945SDave Hansen static __always_inline void setup_pku(struct cpuinfo_x86 *c)
51806976945SDave Hansen {
5198a1dc55aSThomas Gleixner 	if (c == &boot_cpu_data) {
5208a1dc55aSThomas Gleixner 		if (pku_disabled || !cpu_feature_enabled(X86_FEATURE_PKU))
521e8df1a95SDave Hansen 			return;
5228a1dc55aSThomas Gleixner 		/*
5238a1dc55aSThomas Gleixner 		 * Setting CR4.PKE will cause the X86_FEATURE_OSPKE cpuid
5248a1dc55aSThomas Gleixner 		 * bit to be set.  Enforce it.
5258a1dc55aSThomas Gleixner 		 */
5268a1dc55aSThomas Gleixner 		setup_force_cpu_cap(X86_FEATURE_OSPKE);
52706976945SDave Hansen 
5288a1dc55aSThomas Gleixner 	} else if (!cpu_feature_enabled(X86_FEATURE_OSPKE)) {
529e8df1a95SDave Hansen 		return;
5308a1dc55aSThomas Gleixner 	}
53106976945SDave Hansen 
53206976945SDave Hansen 	cr4_set_bits(X86_CR4_PKE);
533fa8c84b7SThomas Gleixner 	/* Load the default PKRU value */
534fa8c84b7SThomas Gleixner 	pkru_write_default();
53506976945SDave Hansen }
53606976945SDave Hansen 
53706976945SDave Hansen #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
setup_disable_pku(char * arg)53806976945SDave Hansen static __init int setup_disable_pku(char *arg)
53906976945SDave Hansen {
54006976945SDave Hansen 	/*
54106976945SDave Hansen 	 * Do not clear the X86_FEATURE_PKU bit.  All of the
54206976945SDave Hansen 	 * runtime checks are against OSPKE so clearing the
54306976945SDave Hansen 	 * bit does nothing.
54406976945SDave Hansen 	 *
54506976945SDave Hansen 	 * This way, we will see "pku" in cpuinfo, but not
54606976945SDave Hansen 	 * "ospke", which is exactly what we want.  It shows
54706976945SDave Hansen 	 * that the CPU has PKU, but the OS has not enabled it.
54806976945SDave Hansen 	 * This happens to be exactly how a system would look
54906976945SDave Hansen 	 * if we disabled the config option.
55006976945SDave Hansen 	 */
55106976945SDave Hansen 	pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
55206976945SDave Hansen 	pku_disabled = true;
55306976945SDave Hansen 	return 1;
55406976945SDave Hansen }
55506976945SDave Hansen __setup("nopku", setup_disable_pku);
556d55dcb73SJuergen Gross #endif
55706976945SDave Hansen 
558fe379fa4SPeter Zijlstra #ifdef CONFIG_X86_KERNEL_IBT
559fe379fa4SPeter Zijlstra 
ibt_save(bool disable)56093be2859SArd Biesheuvel __noendbr u64 ibt_save(bool disable)
561fe379fa4SPeter Zijlstra {
562fe379fa4SPeter Zijlstra 	u64 msr = 0;
563fe379fa4SPeter Zijlstra 
564fe379fa4SPeter Zijlstra 	if (cpu_feature_enabled(X86_FEATURE_IBT)) {
565fe379fa4SPeter Zijlstra 		rdmsrl(MSR_IA32_S_CET, msr);
56693be2859SArd Biesheuvel 		if (disable)
567fe379fa4SPeter Zijlstra 			wrmsrl(MSR_IA32_S_CET, msr & ~CET_ENDBR_EN);
568fe379fa4SPeter Zijlstra 	}
569fe379fa4SPeter Zijlstra 
570fe379fa4SPeter Zijlstra 	return msr;
571fe379fa4SPeter Zijlstra }
572fe379fa4SPeter Zijlstra 
ibt_restore(u64 save)573fe379fa4SPeter Zijlstra __noendbr void ibt_restore(u64 save)
574fe379fa4SPeter Zijlstra {
575fe379fa4SPeter Zijlstra 	u64 msr;
576fe379fa4SPeter Zijlstra 
577fe379fa4SPeter Zijlstra 	if (cpu_feature_enabled(X86_FEATURE_IBT)) {
578fe379fa4SPeter Zijlstra 		rdmsrl(MSR_IA32_S_CET, msr);
579fe379fa4SPeter Zijlstra 		msr &= ~CET_ENDBR_EN;
580fe379fa4SPeter Zijlstra 		msr |= (save & CET_ENDBR_EN);
581fe379fa4SPeter Zijlstra 		wrmsrl(MSR_IA32_S_CET, msr);
582fe379fa4SPeter Zijlstra 	}
583fe379fa4SPeter Zijlstra }
584fe379fa4SPeter Zijlstra 
585fe379fa4SPeter Zijlstra #endif
586fe379fa4SPeter Zijlstra 
setup_cet(struct cpuinfo_x86 * c)587991625f3SPeter Zijlstra static __always_inline void setup_cet(struct cpuinfo_x86 *c)
588991625f3SPeter Zijlstra {
5890dc2a760SRick Edgecombe 	bool user_shstk, kernel_ibt;
590991625f3SPeter Zijlstra 
5910dc2a760SRick Edgecombe 	if (!IS_ENABLED(CONFIG_X86_CET))
592991625f3SPeter Zijlstra 		return;
593991625f3SPeter Zijlstra 
5940dc2a760SRick Edgecombe 	kernel_ibt = HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT);
5950dc2a760SRick Edgecombe 	user_shstk = cpu_feature_enabled(X86_FEATURE_SHSTK) &&
5960dc2a760SRick Edgecombe 		     IS_ENABLED(CONFIG_X86_USER_SHADOW_STACK);
5970dc2a760SRick Edgecombe 
5980dc2a760SRick Edgecombe 	if (!kernel_ibt && !user_shstk)
5990dc2a760SRick Edgecombe 		return;
6000dc2a760SRick Edgecombe 
6010dc2a760SRick Edgecombe 	if (user_shstk)
6020dc2a760SRick Edgecombe 		set_cpu_cap(c, X86_FEATURE_USER_SHSTK);
6030dc2a760SRick Edgecombe 
6040dc2a760SRick Edgecombe 	if (kernel_ibt)
6050dc2a760SRick Edgecombe 		wrmsrl(MSR_IA32_S_CET, CET_ENDBR_EN);
6060dc2a760SRick Edgecombe 	else
6070dc2a760SRick Edgecombe 		wrmsrl(MSR_IA32_S_CET, 0);
6080dc2a760SRick Edgecombe 
609991625f3SPeter Zijlstra 	cr4_set_bits(X86_CR4_CET);
610991625f3SPeter Zijlstra 
611c6cfcbd8SJosh Poimboeuf 	if (kernel_ibt && ibt_selftest()) {
612991625f3SPeter Zijlstra 		pr_err("IBT selftest: Failed!\n");
613931ab636SPeter Zijlstra 		wrmsrl(MSR_IA32_S_CET, 0);
614991625f3SPeter Zijlstra 		setup_clear_cpu_cap(X86_FEATURE_IBT);
615991625f3SPeter Zijlstra 	}
616991625f3SPeter Zijlstra }
617991625f3SPeter Zijlstra 
cet_disable(void)618af227003SPeter Zijlstra __noendbr void cet_disable(void)
619af227003SPeter Zijlstra {
6200dc2a760SRick Edgecombe 	if (!(cpu_feature_enabled(X86_FEATURE_IBT) ||
6210dc2a760SRick Edgecombe 	      cpu_feature_enabled(X86_FEATURE_SHSTK)))
6220dc2a760SRick Edgecombe 		return;
6230dc2a760SRick Edgecombe 
624af227003SPeter Zijlstra 	wrmsrl(MSR_IA32_S_CET, 0);
6250dc2a760SRick Edgecombe 	wrmsrl(MSR_IA32_U_CET, 0);
626af227003SPeter Zijlstra }
627af227003SPeter Zijlstra 
62806976945SDave Hansen /*
629b38b0665SH. Peter Anvin  * Some CPU features depend on higher CPUID levels, which may not always
630b38b0665SH. Peter Anvin  * be available due to CPUID level capping or broken virtualization
631b38b0665SH. Peter Anvin  * software.  Add those features to this table to auto-disable them.
632b38b0665SH. Peter Anvin  */
633b38b0665SH. Peter Anvin struct cpuid_dependent_feature {
634b38b0665SH. Peter Anvin 	u32 feature;
635b38b0665SH. Peter Anvin 	u32 level;
636b38b0665SH. Peter Anvin };
6370f3fa48aSIngo Molnar 
638148f9bb8SPaul Gortmaker static const struct cpuid_dependent_feature
639b38b0665SH. Peter Anvin cpuid_dependent_features[] = {
640b38b0665SH. Peter Anvin 	{ X86_FEATURE_MWAIT,		0x00000005 },
641b38b0665SH. Peter Anvin 	{ X86_FEATURE_DCA,		0x00000009 },
642b38b0665SH. Peter Anvin 	{ X86_FEATURE_XSAVE,		0x0000000d },
643b38b0665SH. Peter Anvin 	{ 0, 0 }
644b38b0665SH. Peter Anvin };
645b38b0665SH. Peter Anvin 
filter_cpuid_features(struct cpuinfo_x86 * c,bool warn)646148f9bb8SPaul Gortmaker static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
647b38b0665SH. Peter Anvin {
648b38b0665SH. Peter Anvin 	const struct cpuid_dependent_feature *df;
6499766cdbcSJaswinder Singh Rajput 
650b38b0665SH. Peter Anvin 	for (df = cpuid_dependent_features; df->feature; df++) {
6510f3fa48aSIngo Molnar 
6520f3fa48aSIngo Molnar 		if (!cpu_has(c, df->feature))
6530f3fa48aSIngo Molnar 			continue;
654b38b0665SH. Peter Anvin 		/*
655b38b0665SH. Peter Anvin 		 * Note: cpuid_level is set to -1 if unavailable, but
656b38b0665SH. Peter Anvin 		 * extended_extended_level is set to 0 if unavailable
657b38b0665SH. Peter Anvin 		 * and the legitimate extended levels are all negative
658b38b0665SH. Peter Anvin 		 * when signed; hence the weird messing around with
659b38b0665SH. Peter Anvin 		 * signs here...
660b38b0665SH. Peter Anvin 		 */
6610f3fa48aSIngo Molnar 		if (!((s32)df->level < 0 ?
662f6db44dfSYinghai Lu 		     (u32)df->level > (u32)c->extended_cpuid_level :
6630f3fa48aSIngo Molnar 		     (s32)df->level > (s32)c->cpuid_level))
6640f3fa48aSIngo Molnar 			continue;
6650f3fa48aSIngo Molnar 
666b38b0665SH. Peter Anvin 		clear_cpu_cap(c, df->feature);
6670f3fa48aSIngo Molnar 		if (!warn)
6680f3fa48aSIngo Molnar 			continue;
6690f3fa48aSIngo Molnar 
6701b74dde7SChen Yucong 		pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
6719def39beSJosh Triplett 			x86_cap_flag(df->feature), df->level);
672b38b0665SH. Peter Anvin 	}
673b38b0665SH. Peter Anvin }
674b38b0665SH. Peter Anvin 
675b38b0665SH. Peter Anvin /*
676f7627e25SThomas Gleixner  * Naming convention should be: <Name> [(<Codename>)]
677f7627e25SThomas Gleixner  * This table only is used unless init_<vendor>() below doesn't set it;
6780f3fa48aSIngo Molnar  * in particular, if CPUID levels 0x80000002..4 are supported, this
6790f3fa48aSIngo Molnar  * isn't used
680f7627e25SThomas Gleixner  */
681f7627e25SThomas Gleixner 
682f7627e25SThomas Gleixner /* Look up CPU names by table lookup. */
table_lookup_model(struct cpuinfo_x86 * c)683148f9bb8SPaul Gortmaker static const char *table_lookup_model(struct cpuinfo_x86 *c)
684f7627e25SThomas Gleixner {
68509dc68d9SJan Beulich #ifdef CONFIG_X86_32
68609dc68d9SJan Beulich 	const struct legacy_cpu_model_info *info;
687f7627e25SThomas Gleixner 
688f7627e25SThomas Gleixner 	if (c->x86_model >= 16)
689f7627e25SThomas Gleixner 		return NULL;	/* Range check */
690f7627e25SThomas Gleixner 
691f7627e25SThomas Gleixner 	if (!this_cpu)
692f7627e25SThomas Gleixner 		return NULL;
693f7627e25SThomas Gleixner 
69409dc68d9SJan Beulich 	info = this_cpu->legacy_models;
695f7627e25SThomas Gleixner 
69609dc68d9SJan Beulich 	while (info->family) {
697f7627e25SThomas Gleixner 		if (info->family == c->x86)
698f7627e25SThomas Gleixner 			return info->model_names[c->x86_model];
699f7627e25SThomas Gleixner 		info++;
700f7627e25SThomas Gleixner 	}
70109dc68d9SJan Beulich #endif
702f7627e25SThomas Gleixner 	return NULL;		/* Not found */
703f7627e25SThomas Gleixner }
704f7627e25SThomas Gleixner 
705f6a892ddSFenghua Yu /* Aligned to unsigned long to avoid split lock in atomic bitmap ops */
706f6a892ddSFenghua Yu __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
707f6a892ddSFenghua Yu __u32 cpu_caps_set[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
708f7627e25SThomas Gleixner 
70972f5e08dSAndy Lutomirski #ifdef CONFIG_X86_32
71072f5e08dSAndy Lutomirski /* The 32-bit entry code needs to find cpu_entry_area. */
71172f5e08dSAndy Lutomirski DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
71272f5e08dSAndy Lutomirski #endif
71372f5e08dSAndy Lutomirski 
71445fc8757SThomas Garnier /* Load the original GDT from the per-cpu structure */
load_direct_gdt(int cpu)71545fc8757SThomas Garnier void load_direct_gdt(int cpu)
71645fc8757SThomas Garnier {
71745fc8757SThomas Garnier 	struct desc_ptr gdt_descr;
71845fc8757SThomas Garnier 
71945fc8757SThomas Garnier 	gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
72045fc8757SThomas Garnier 	gdt_descr.size = GDT_SIZE - 1;
72145fc8757SThomas Garnier 	load_gdt(&gdt_descr);
72245fc8757SThomas Garnier }
72345fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_direct_gdt);
72445fc8757SThomas Garnier 
72569218e47SThomas Garnier /* Load a fixmap remapping of the per-cpu GDT */
load_fixmap_gdt(int cpu)72669218e47SThomas Garnier void load_fixmap_gdt(int cpu)
72769218e47SThomas Garnier {
72869218e47SThomas Garnier 	struct desc_ptr gdt_descr;
72969218e47SThomas Garnier 
73069218e47SThomas Garnier 	gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
73169218e47SThomas Garnier 	gdt_descr.size = GDT_SIZE - 1;
73269218e47SThomas Garnier 	load_gdt(&gdt_descr);
73369218e47SThomas Garnier }
73445fc8757SThomas Garnier EXPORT_SYMBOL_GPL(load_fixmap_gdt);
73569218e47SThomas Garnier 
736b5636d45SThomas Gleixner /**
7371f19e2d5SThomas Gleixner  * switch_gdt_and_percpu_base - Switch to direct GDT and runtime per CPU base
738b5636d45SThomas Gleixner  * @cpu:	The CPU number for which this is invoked
739b5636d45SThomas Gleixner  *
7401f19e2d5SThomas Gleixner  * Invoked during early boot to switch from early GDT and early per CPU to
7411f19e2d5SThomas Gleixner  * the direct GDT and the runtime per CPU area. On 32-bit the percpu base
7421f19e2d5SThomas Gleixner  * switch is implicit by loading the direct GDT. On 64bit this requires
7431f19e2d5SThomas Gleixner  * to update GSBASE.
7440f3fa48aSIngo Molnar  */
switch_gdt_and_percpu_base(int cpu)7451f19e2d5SThomas Gleixner void __init switch_gdt_and_percpu_base(int cpu)
7469d31d35bSYinghai Lu {
74745fc8757SThomas Garnier 	load_direct_gdt(cpu);
748b5636d45SThomas Gleixner 
749b5636d45SThomas Gleixner #ifdef CONFIG_X86_64
750b5636d45SThomas Gleixner 	/*
751b5636d45SThomas Gleixner 	 * No need to load %gs. It is already correct.
752b5636d45SThomas Gleixner 	 *
753b5636d45SThomas Gleixner 	 * Writing %gs on 64bit would zero GSBASE which would make any per
754b5636d45SThomas Gleixner 	 * CPU operation up to the point of the wrmsrl() fault.
755b5636d45SThomas Gleixner 	 *
756b5636d45SThomas Gleixner 	 * Set GSBASE to the new offset. Until the wrmsrl() happens the
757b5636d45SThomas Gleixner 	 * early mapping is still valid. That means the GSBASE update will
758b5636d45SThomas Gleixner 	 * lose any prior per CPU data which was not copied over in
759b5636d45SThomas Gleixner 	 * setup_per_cpu_areas().
7602cb15faaSThomas Gleixner 	 *
7612cb15faaSThomas Gleixner 	 * This works even with stackprotector enabled because the
7622cb15faaSThomas Gleixner 	 * per CPU stack canary is 0 in both per CPU areas.
763b5636d45SThomas Gleixner 	 */
764b5636d45SThomas Gleixner 	wrmsrl(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu));
765b5636d45SThomas Gleixner #else
766b5636d45SThomas Gleixner 	/*
767b5636d45SThomas Gleixner 	 * %fs is already set to __KERNEL_PERCPU, but after switching GDT
768b5636d45SThomas Gleixner 	 * it is required to load FS again so that the 'hidden' part is
769b5636d45SThomas Gleixner 	 * updated from the new GDT. Up to this point the early per CPU
770b5636d45SThomas Gleixner 	 * translation is active. Any content of the early per CPU data
771b5636d45SThomas Gleixner 	 * which was not copied over in setup_per_cpu_areas() is lost.
772b5636d45SThomas Gleixner 	 */
773b5636d45SThomas Gleixner 	loadsegment(fs, __KERNEL_PERCPU);
774b5636d45SThomas Gleixner #endif
7759d31d35bSYinghai Lu }
7769d31d35bSYinghai Lu 
777148f9bb8SPaul Gortmaker static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
778f7627e25SThomas Gleixner 
get_model_name(struct cpuinfo_x86 * c)779148f9bb8SPaul Gortmaker static void get_model_name(struct cpuinfo_x86 *c)
780f7627e25SThomas Gleixner {
781f7627e25SThomas Gleixner 	unsigned int *v;
782ee098e1aSBorislav Petkov 	char *p, *q, *s;
783f7627e25SThomas Gleixner 
7843da99c97SYinghai Lu 	if (c->extended_cpuid_level < 0x80000004)
7851b05d60dSYinghai Lu 		return;
786f7627e25SThomas Gleixner 
787f7627e25SThomas Gleixner 	v = (unsigned int *)c->x86_model_id;
788f7627e25SThomas Gleixner 	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
789f7627e25SThomas Gleixner 	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
790f7627e25SThomas Gleixner 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
791f7627e25SThomas Gleixner 	c->x86_model_id[48] = 0;
792f7627e25SThomas Gleixner 
793ee098e1aSBorislav Petkov 	/* Trim whitespace */
794ee098e1aSBorislav Petkov 	p = q = s = &c->x86_model_id[0];
795ee098e1aSBorislav Petkov 
796ee098e1aSBorislav Petkov 	while (*p == ' ')
797ee098e1aSBorislav Petkov 		p++;
798ee098e1aSBorislav Petkov 
799ee098e1aSBorislav Petkov 	while (*p) {
800ee098e1aSBorislav Petkov 		/* Note the last non-whitespace index */
801ee098e1aSBorislav Petkov 		if (!isspace(*p))
802ee098e1aSBorislav Petkov 			s = q;
803ee098e1aSBorislav Petkov 
804ee098e1aSBorislav Petkov 		*q++ = *p++;
805ee098e1aSBorislav Petkov 	}
806ee098e1aSBorislav Petkov 
807ee098e1aSBorislav Petkov 	*(s + 1) = '\0';
808f7627e25SThomas Gleixner }
809f7627e25SThomas Gleixner 
cpu_detect_cache_sizes(struct cpuinfo_x86 * c)810148f9bb8SPaul Gortmaker void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
811f7627e25SThomas Gleixner {
8129d31d35bSYinghai Lu 	unsigned int n, dummy, ebx, ecx, edx, l2size;
813f7627e25SThomas Gleixner 
8143da99c97SYinghai Lu 	n = c->extended_cpuid_level;
815f7627e25SThomas Gleixner 
816f7627e25SThomas Gleixner 	if (n >= 0x80000005) {
8179d31d35bSYinghai Lu 		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
818f7627e25SThomas Gleixner 		c->x86_cache_size = (ecx>>24) + (edx>>24);
819140fc727SYinghai Lu #ifdef CONFIG_X86_64
820140fc727SYinghai Lu 		/* On K8 L1 TLB is inclusive, so don't count it */
821140fc727SYinghai Lu 		c->x86_tlbsize = 0;
822140fc727SYinghai Lu #endif
823f7627e25SThomas Gleixner 	}
824f7627e25SThomas Gleixner 
825f7627e25SThomas Gleixner 	if (n < 0x80000006)	/* Some chips just has a large L1. */
826f7627e25SThomas Gleixner 		return;
827f7627e25SThomas Gleixner 
8280a488a53SYinghai Lu 	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
829f7627e25SThomas Gleixner 	l2size = ecx >> 16;
830f7627e25SThomas Gleixner 
831140fc727SYinghai Lu #ifdef CONFIG_X86_64
832140fc727SYinghai Lu 	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
833140fc727SYinghai Lu #else
834f7627e25SThomas Gleixner 	/* do processor-specific cache resizing */
83509dc68d9SJan Beulich 	if (this_cpu->legacy_cache_size)
83609dc68d9SJan Beulich 		l2size = this_cpu->legacy_cache_size(c, l2size);
837f7627e25SThomas Gleixner 
838f7627e25SThomas Gleixner 	/* Allow user to override all this if necessary. */
839f7627e25SThomas Gleixner 	if (cachesize_override != -1)
840f7627e25SThomas Gleixner 		l2size = cachesize_override;
841f7627e25SThomas Gleixner 
842f7627e25SThomas Gleixner 	if (l2size == 0)
843f7627e25SThomas Gleixner 		return;		/* Again, no L2 cache is possible */
844140fc727SYinghai Lu #endif
845f7627e25SThomas Gleixner 
846f7627e25SThomas Gleixner 	c->x86_cache_size = l2size;
847f7627e25SThomas Gleixner }
848f7627e25SThomas Gleixner 
849e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4k[NR_INFO];
850e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_2m[NR_INFO];
851e0ba94f1SAlex Shi u16 __read_mostly tlb_lli_4m[NR_INFO];
852e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4k[NR_INFO];
853e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_2m[NR_INFO];
854e0ba94f1SAlex Shi u16 __read_mostly tlb_lld_4m[NR_INFO];
855dd360393SKirill A. Shutemov u16 __read_mostly tlb_lld_1g[NR_INFO];
856e0ba94f1SAlex Shi 
cpu_detect_tlb(struct cpuinfo_x86 * c)857f94fe119SSteven Honeyman static void cpu_detect_tlb(struct cpuinfo_x86 *c)
858e0ba94f1SAlex Shi {
859e0ba94f1SAlex Shi 	if (this_cpu->c_detect_tlb)
860e0ba94f1SAlex Shi 		this_cpu->c_detect_tlb(c);
861e0ba94f1SAlex Shi 
862f94fe119SSteven Honeyman 	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
863e0ba94f1SAlex Shi 		tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
864f94fe119SSteven Honeyman 		tlb_lli_4m[ENTRIES]);
865f94fe119SSteven Honeyman 
866f94fe119SSteven Honeyman 	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
867f94fe119SSteven Honeyman 		tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
868f94fe119SSteven Honeyman 		tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
869e0ba94f1SAlex Shi }
870e0ba94f1SAlex Shi 
get_cpu_vendor(struct cpuinfo_x86 * c)871148f9bb8SPaul Gortmaker static void get_cpu_vendor(struct cpuinfo_x86 *c)
872f7627e25SThomas Gleixner {
873f7627e25SThomas Gleixner 	char *v = c->x86_vendor_id;
8740f3fa48aSIngo Molnar 	int i;
875f7627e25SThomas Gleixner 
876f7627e25SThomas Gleixner 	for (i = 0; i < X86_VENDOR_NUM; i++) {
87710a434fcSYinghai Lu 		if (!cpu_devs[i])
87810a434fcSYinghai Lu 			break;
87910a434fcSYinghai Lu 
880f7627e25SThomas Gleixner 		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
881f7627e25SThomas Gleixner 		    (cpu_devs[i]->c_ident[1] &&
882f7627e25SThomas Gleixner 		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
8830f3fa48aSIngo Molnar 
884f7627e25SThomas Gleixner 			this_cpu = cpu_devs[i];
88510a434fcSYinghai Lu 			c->x86_vendor = this_cpu->c_x86_vendor;
886f7627e25SThomas Gleixner 			return;
887f7627e25SThomas Gleixner 		}
888f7627e25SThomas Gleixner 	}
88910a434fcSYinghai Lu 
8901b74dde7SChen Yucong 	pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
891a9c56953SMinchan Kim 		    "CPU: Your system may be unstable.\n", v);
89210a434fcSYinghai Lu 
893f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
894f7627e25SThomas Gleixner 	this_cpu = &default_cpu;
895f7627e25SThomas Gleixner }
896f7627e25SThomas Gleixner 
cpu_detect(struct cpuinfo_x86 * c)897148f9bb8SPaul Gortmaker void cpu_detect(struct cpuinfo_x86 *c)
898f7627e25SThomas Gleixner {
899f7627e25SThomas Gleixner 	/* Get vendor name */
9004a148513SHarvey Harrison 	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
9014a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[0],
9024a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[8],
9034a148513SHarvey Harrison 	      (unsigned int *)&c->x86_vendor_id[4]);
904f7627e25SThomas Gleixner 
905f7627e25SThomas Gleixner 	c->x86 = 4;
9069d31d35bSYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
907f7627e25SThomas Gleixner 	if (c->cpuid_level >= 0x00000001) {
908f7627e25SThomas Gleixner 		u32 junk, tfms, cap0, misc;
9090f3fa48aSIngo Molnar 
910f7627e25SThomas Gleixner 		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
91199f925ceSBorislav Petkov 		c->x86		= x86_family(tfms);
91299f925ceSBorislav Petkov 		c->x86_model	= x86_model(tfms);
913b399151cSJia Zhang 		c->x86_stepping	= x86_stepping(tfms);
9140f3fa48aSIngo Molnar 
915d4387bd3SHuang, Ying 		if (cap0 & (1<<19)) {
916d4387bd3SHuang, Ying 			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
9179d31d35bSYinghai Lu 			c->x86_cache_alignment = c->x86_clflush_size;
918d4387bd3SHuang, Ying 		}
919f7627e25SThomas Gleixner 	}
920f7627e25SThomas Gleixner }
9213da99c97SYinghai Lu 
apply_forced_caps(struct cpuinfo_x86 * c)9228bf1ebcaSAndy Lutomirski static void apply_forced_caps(struct cpuinfo_x86 *c)
9238bf1ebcaSAndy Lutomirski {
9248bf1ebcaSAndy Lutomirski 	int i;
9258bf1ebcaSAndy Lutomirski 
9266cbd2171SThomas Gleixner 	for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
9278bf1ebcaSAndy Lutomirski 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
9288bf1ebcaSAndy Lutomirski 		c->x86_capability[i] |= cpu_caps_set[i];
9298bf1ebcaSAndy Lutomirski 	}
9308bf1ebcaSAndy Lutomirski }
9318bf1ebcaSAndy Lutomirski 
init_speculation_control(struct cpuinfo_x86 * c)9327fcae111SDavid Woodhouse static void init_speculation_control(struct cpuinfo_x86 *c)
9337fcae111SDavid Woodhouse {
9347fcae111SDavid Woodhouse 	/*
9357fcae111SDavid Woodhouse 	 * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
9367fcae111SDavid Woodhouse 	 * and they also have a different bit for STIBP support. Also,
9377fcae111SDavid Woodhouse 	 * a hypervisor might have set the individual AMD bits even on
9387fcae111SDavid Woodhouse 	 * Intel CPUs, for finer-grained selection of what's available.
9397fcae111SDavid Woodhouse 	 */
9407fcae111SDavid Woodhouse 	if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
9417fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_IBRS);
9427fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_IBPB);
9437eb8956aSThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
9447fcae111SDavid Woodhouse 	}
945e7c587daSBorislav Petkov 
9467fcae111SDavid Woodhouse 	if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
9477fcae111SDavid Woodhouse 		set_cpu_cap(c, X86_FEATURE_STIBP);
948e7c587daSBorislav Petkov 
949bc226f07STom Lendacky 	if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
950bc226f07STom Lendacky 	    cpu_has(c, X86_FEATURE_VIRT_SSBD))
95152817587SThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_SSBD);
95252817587SThomas Gleixner 
9537eb8956aSThomas Gleixner 	if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
954e7c587daSBorislav Petkov 		set_cpu_cap(c, X86_FEATURE_IBRS);
9557eb8956aSThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
9567eb8956aSThomas Gleixner 	}
957e7c587daSBorislav Petkov 
958e7c587daSBorislav Petkov 	if (cpu_has(c, X86_FEATURE_AMD_IBPB))
959e7c587daSBorislav Petkov 		set_cpu_cap(c, X86_FEATURE_IBPB);
960e7c587daSBorislav Petkov 
9617eb8956aSThomas Gleixner 	if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
962e7c587daSBorislav Petkov 		set_cpu_cap(c, X86_FEATURE_STIBP);
9637eb8956aSThomas Gleixner 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
9647eb8956aSThomas Gleixner 	}
9656ac2f49eSKonrad Rzeszutek Wilk 
9666ac2f49eSKonrad Rzeszutek Wilk 	if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
9676ac2f49eSKonrad Rzeszutek Wilk 		set_cpu_cap(c, X86_FEATURE_SSBD);
9686ac2f49eSKonrad Rzeszutek Wilk 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
9696ac2f49eSKonrad Rzeszutek Wilk 		clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
9706ac2f49eSKonrad Rzeszutek Wilk 	}
9717fcae111SDavid Woodhouse }
9727fcae111SDavid Woodhouse 
get_cpu_cap(struct cpuinfo_x86 * c)973148f9bb8SPaul Gortmaker void get_cpu_cap(struct cpuinfo_x86 *c)
974093af8d7SYinghai Lu {
97539c06df4SBorislav Petkov 	u32 eax, ebx, ecx, edx;
976093af8d7SYinghai Lu 
977093af8d7SYinghai Lu 	/* Intel-defined flags: level 0x00000001 */
978093af8d7SYinghai Lu 	if (c->cpuid_level >= 0x00000001) {
97939c06df4SBorislav Petkov 		cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
9800f3fa48aSIngo Molnar 
98139c06df4SBorislav Petkov 		c->x86_capability[CPUID_1_ECX] = ecx;
98239c06df4SBorislav Petkov 		c->x86_capability[CPUID_1_EDX] = edx;
983093af8d7SYinghai Lu 	}
984093af8d7SYinghai Lu 
9853df8d920SAndy Lutomirski 	/* Thermal and Power Management Leaf: level 0x00000006 (eax) */
9863df8d920SAndy Lutomirski 	if (c->cpuid_level >= 0x00000006)
9873df8d920SAndy Lutomirski 		c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
9883df8d920SAndy Lutomirski 
989bdc802dcSH. Peter Anvin 	/* Additional Intel-defined flags: level 0x00000007 */
990bdc802dcSH. Peter Anvin 	if (c->cpuid_level >= 0x00000007) {
991bdc802dcSH. Peter Anvin 		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
99239c06df4SBorislav Petkov 		c->x86_capability[CPUID_7_0_EBX] = ebx;
993dfb4a70fSDave Hansen 		c->x86_capability[CPUID_7_ECX] = ecx;
99495ca0ee8SDavid Woodhouse 		c->x86_capability[CPUID_7_EDX] = edx;
995b302e4b1SFenghua Yu 
996b302e4b1SFenghua Yu 		/* Check valid sub-leaf index before accessing it */
997b302e4b1SFenghua Yu 		if (eax >= 1) {
998b302e4b1SFenghua Yu 			cpuid_count(0x00000007, 1, &eax, &ebx, &ecx, &edx);
999b302e4b1SFenghua Yu 			c->x86_capability[CPUID_7_1_EAX] = eax;
1000b302e4b1SFenghua Yu 		}
1001bdc802dcSH. Peter Anvin 	}
1002bdc802dcSH. Peter Anvin 
10036229ad27SFenghua Yu 	/* Extended state features: level 0x0000000d */
10046229ad27SFenghua Yu 	if (c->cpuid_level >= 0x0000000d) {
10056229ad27SFenghua Yu 		cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
10066229ad27SFenghua Yu 
100739c06df4SBorislav Petkov 		c->x86_capability[CPUID_D_1_EAX] = eax;
10086229ad27SFenghua Yu 	}
10096229ad27SFenghua Yu 
1010093af8d7SYinghai Lu 	/* AMD-defined flags: level 0x80000001 */
101139c06df4SBorislav Petkov 	eax = cpuid_eax(0x80000000);
101239c06df4SBorislav Petkov 	c->extended_cpuid_level = eax;
10130f3fa48aSIngo Molnar 
101439c06df4SBorislav Petkov 	if ((eax & 0xffff0000) == 0x80000000) {
101539c06df4SBorislav Petkov 		if (eax >= 0x80000001) {
101639c06df4SBorislav Petkov 			cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
101739c06df4SBorislav Petkov 
101839c06df4SBorislav Petkov 			c->x86_capability[CPUID_8000_0001_ECX] = ecx;
101939c06df4SBorislav Petkov 			c->x86_capability[CPUID_8000_0001_EDX] = edx;
1020093af8d7SYinghai Lu 		}
1021093af8d7SYinghai Lu 	}
1022093af8d7SYinghai Lu 
102371faad43SYazen Ghannam 	if (c->extended_cpuid_level >= 0x80000007) {
102471faad43SYazen Ghannam 		cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
102571faad43SYazen Ghannam 
102671faad43SYazen Ghannam 		c->x86_capability[CPUID_8000_0007_EBX] = ebx;
102771faad43SYazen Ghannam 		c->x86_power = edx;
102871faad43SYazen Ghannam 	}
102971faad43SYazen Ghannam 
1030c65732e4SThomas Gleixner 	if (c->extended_cpuid_level >= 0x80000008) {
1031c65732e4SThomas Gleixner 		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
1032c65732e4SThomas Gleixner 		c->x86_capability[CPUID_8000_0008_EBX] = ebx;
1033c65732e4SThomas Gleixner 	}
1034c65732e4SThomas Gleixner 
10352ccd71f1SBorislav Petkov 	if (c->extended_cpuid_level >= 0x8000000a)
103639c06df4SBorislav Petkov 		c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
10372ccd71f1SBorislav Petkov 
1038fb35d30fSSean Christopherson 	if (c->extended_cpuid_level >= 0x8000001f)
1039fb35d30fSSean Christopherson 		c->x86_capability[CPUID_8000_001F_EAX] = cpuid_eax(0x8000001f);
1040fb35d30fSSean Christopherson 
10418415a748SKim Phillips 	if (c->extended_cpuid_level >= 0x80000021)
10428415a748SKim Phillips 		c->x86_capability[CPUID_8000_0021_EAX] = cpuid_eax(0x80000021);
10438415a748SKim Phillips 
10441dedefd1SJacob Pan 	init_scattered_cpuid_features(c);
10457fcae111SDavid Woodhouse 	init_speculation_control(c);
104660d34501SAndy Lutomirski 
104760d34501SAndy Lutomirski 	/*
104860d34501SAndy Lutomirski 	 * Clear/Set all flags overridden by options, after probe.
104960d34501SAndy Lutomirski 	 * This needs to happen each time we re-probe, which may happen
105060d34501SAndy Lutomirski 	 * several times during CPU initialization.
105160d34501SAndy Lutomirski 	 */
105260d34501SAndy Lutomirski 	apply_forced_caps(c);
1053093af8d7SYinghai Lu }
1054093af8d7SYinghai Lu 
get_cpu_address_sizes(struct cpuinfo_x86 * c)1055405c018aSM. Vefa Bicakci void get_cpu_address_sizes(struct cpuinfo_x86 *c)
1056d94a155cSKirill A. Shutemov {
1057d94a155cSKirill A. Shutemov 	u32 eax, ebx, ecx, edx;
1058d94a155cSKirill A. Shutemov 
1059fbf6449fSAdam Dunlap 	if (!cpu_has(c, X86_FEATURE_CPUID) ||
106095bfb352SBorislav Petkov (AMD) 	    (c->extended_cpuid_level < 0x80000008)) {
1061fbf6449fSAdam Dunlap 		if (IS_ENABLED(CONFIG_X86_64)) {
1062fbf6449fSAdam Dunlap 			c->x86_clflush_size = 64;
1063d94a155cSKirill A. Shutemov 			c->x86_phys_bits = 36;
1064fbf6449fSAdam Dunlap 			c->x86_virt_bits = 48;
1065fbf6449fSAdam Dunlap 		} else {
1066fbf6449fSAdam Dunlap 			c->x86_clflush_size = 32;
1067fbf6449fSAdam Dunlap 			c->x86_virt_bits = 32;
1068fbf6449fSAdam Dunlap 			c->x86_phys_bits = 32;
1069fbf6449fSAdam Dunlap 
1070fbf6449fSAdam Dunlap 			if (cpu_has(c, X86_FEATURE_PAE) ||
1071fbf6449fSAdam Dunlap 			    cpu_has(c, X86_FEATURE_PSE36))
1072fbf6449fSAdam Dunlap 				c->x86_phys_bits = 36;
1073fbf6449fSAdam Dunlap 		}
107495bfb352SBorislav Petkov (AMD) 	} else {
107595bfb352SBorislav Petkov (AMD) 		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
107695bfb352SBorislav Petkov (AMD) 
107795bfb352SBorislav Petkov (AMD) 		c->x86_virt_bits = (eax >> 8) & 0xff;
107895bfb352SBorislav Petkov (AMD) 		c->x86_phys_bits = eax & 0xff;
10792a38e4caSDave Hansen 
10802a38e4caSDave Hansen 		/* Provide a sane default if not enumerated: */
10812a38e4caSDave Hansen 		if (!c->x86_clflush_size)
10822a38e4caSDave Hansen 			c->x86_clflush_size = 32;
1083fbf6449fSAdam Dunlap 	}
108495bfb352SBorislav Petkov (AMD) 
1085cc51e542SAndi Kleen 	c->x86_cache_bits = c->x86_phys_bits;
10863e325526SDave Hansen 	c->x86_cache_alignment = c->x86_clflush_size;
1087d94a155cSKirill A. Shutemov }
1088d94a155cSKirill A. Shutemov 
identify_cpu_without_cpuid(struct cpuinfo_x86 * c)1089148f9bb8SPaul Gortmaker static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
1090aef93c8bSYinghai Lu {
1091aef93c8bSYinghai Lu #ifdef CONFIG_X86_32
1092aef93c8bSYinghai Lu 	int i;
1093aef93c8bSYinghai Lu 
1094aef93c8bSYinghai Lu 	/*
1095aef93c8bSYinghai Lu 	 * First of all, decide if this is a 486 or higher
1096aef93c8bSYinghai Lu 	 * It's a 486 if we can modify the AC flag
1097aef93c8bSYinghai Lu 	 */
1098aef93c8bSYinghai Lu 	if (flag_is_changeable_p(X86_EFLAGS_AC))
1099aef93c8bSYinghai Lu 		c->x86 = 4;
1100aef93c8bSYinghai Lu 	else
1101aef93c8bSYinghai Lu 		c->x86 = 3;
1102aef93c8bSYinghai Lu 
1103aef93c8bSYinghai Lu 	for (i = 0; i < X86_VENDOR_NUM; i++)
1104aef93c8bSYinghai Lu 		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
1105aef93c8bSYinghai Lu 			c->x86_vendor_id[0] = 0;
1106aef93c8bSYinghai Lu 			cpu_devs[i]->c_identify(c);
1107aef93c8bSYinghai Lu 			if (c->x86_vendor_id[0]) {
1108aef93c8bSYinghai Lu 				get_cpu_vendor(c);
1109aef93c8bSYinghai Lu 				break;
1110aef93c8bSYinghai Lu 			}
1111aef93c8bSYinghai Lu 		}
1112aef93c8bSYinghai Lu #endif
1113093af8d7SYinghai Lu }
1114f7627e25SThomas Gleixner 
111536ad3513SThomas Gleixner #define NO_SPECULATION		BIT(0)
111636ad3513SThomas Gleixner #define NO_MELTDOWN		BIT(1)
111736ad3513SThomas Gleixner #define NO_SSB			BIT(2)
111836ad3513SThomas Gleixner #define NO_L1TF			BIT(3)
1119ed5194c2SAndi Kleen #define NO_MDS			BIT(4)
1120e261f209SThomas Gleixner #define MSBDS_ONLY		BIT(5)
1121f36cf386SThomas Gleixner #define NO_SWAPGS		BIT(6)
1122db4d30fbSVineela Tummalapalli #define NO_ITLB_MULTIHIT	BIT(7)
11231e41a766STony W Wang-oc #define NO_SPECTRE_V2		BIT(8)
11247df54884SPawan Gupta #define NO_MMIO			BIT(9)
11257df54884SPawan Gupta #define NO_EIBRS_PBRSB		BIT(10)
1126be482ff9SPawan Gupta #define NO_BHI			BIT(11)
112736ad3513SThomas Gleixner 
1128f6d502fcSThomas Gleixner #define VULNWL(vendor, family, model, whitelist)	\
1129f6d502fcSThomas Gleixner 	X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, whitelist)
113036ad3513SThomas Gleixner 
1131b24e466aSTony Luck #define VULNWL_INTEL(vfm, whitelist)		\
1132b24e466aSTony Luck 	X86_MATCH_VFM(vfm, whitelist)
113336ad3513SThomas Gleixner 
113436ad3513SThomas Gleixner #define VULNWL_AMD(family, whitelist)		\
113536ad3513SThomas Gleixner 	VULNWL(AMD, family, X86_MODEL_ANY, whitelist)
113636ad3513SThomas Gleixner 
113736ad3513SThomas Gleixner #define VULNWL_HYGON(family, whitelist)		\
113836ad3513SThomas Gleixner 	VULNWL(HYGON, family, X86_MODEL_ANY, whitelist)
113936ad3513SThomas Gleixner 
114036ad3513SThomas Gleixner static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
114136ad3513SThomas Gleixner 	VULNWL(ANY,	4, X86_MODEL_ANY,	NO_SPECULATION),
114236ad3513SThomas Gleixner 	VULNWL(CENTAUR,	5, X86_MODEL_ANY,	NO_SPECULATION),
114336ad3513SThomas Gleixner 	VULNWL(INTEL,	5, X86_MODEL_ANY,	NO_SPECULATION),
114436ad3513SThomas Gleixner 	VULNWL(NSC,	5, X86_MODEL_ANY,	NO_SPECULATION),
1145639475d4SMarcos Del Sol Vives 	VULNWL(VORTEX,	5, X86_MODEL_ANY,	NO_SPECULATION),
1146639475d4SMarcos Del Sol Vives 	VULNWL(VORTEX,	6, X86_MODEL_ANY,	NO_SPECULATION),
114736ad3513SThomas Gleixner 
1148ed5194c2SAndi Kleen 	/* Intel Family 6 */
1149b24e466aSTony Luck 	VULNWL_INTEL(INTEL_TIGERLAKE,		NO_MMIO),
1150b24e466aSTony Luck 	VULNWL_INTEL(INTEL_TIGERLAKE_L,		NO_MMIO),
1151b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ALDERLAKE,		NO_MMIO),
1152b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ALDERLAKE_L,		NO_MMIO),
11537df54884SPawan Gupta 
1154b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_SALTWELL,	NO_SPECULATION | NO_ITLB_MULTIHIT),
1155b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_SALTWELL_TABLET, NO_SPECULATION | NO_ITLB_MULTIHIT),
1156b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_SALTWELL_MID,	NO_SPECULATION | NO_ITLB_MULTIHIT),
1157b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_BONNELL,	NO_SPECULATION | NO_ITLB_MULTIHIT),
1158b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_BONNELL_MID,	NO_SPECULATION | NO_ITLB_MULTIHIT),
115936ad3513SThomas Gleixner 
1160b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_SILVERMONT,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1161b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_SILVERMONT_D,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1162b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_SILVERMONT_MID,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1163b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_AIRMONT,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1164b24e466aSTony Luck 	VULNWL_INTEL(INTEL_XEON_PHI_KNL,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1165b24e466aSTony Luck 	VULNWL_INTEL(INTEL_XEON_PHI_KNM,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
116636ad3513SThomas Gleixner 
1167b24e466aSTony Luck 	VULNWL_INTEL(INTEL_CORE_YONAH,		NO_SSB),
116836ad3513SThomas Gleixner 
116923e12b54SDaniel Sneddon 	VULNWL_INTEL(INTEL_ATOM_AIRMONT_MID,	NO_SSB | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | MSBDS_ONLY),
117023e12b54SDaniel Sneddon 	VULNWL_INTEL(INTEL_ATOM_AIRMONT_NP,	NO_SSB | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
117136ad3513SThomas Gleixner 
1172b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_GOLDMONT,	NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1173b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_GOLDMONT_D,	NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1174b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_GOLDMONT_PLUS,	NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB),
1175f36cf386SThomas Gleixner 
1176f36cf386SThomas Gleixner 	/*
1177f36cf386SThomas Gleixner 	 * Technically, swapgs isn't serializing on AMD (despite it previously
1178f36cf386SThomas Gleixner 	 * being documented as such in the APM).  But according to AMD, %gs is
1179f36cf386SThomas Gleixner 	 * updated non-speculatively, and the issuing of %gs-relative memory
1180f36cf386SThomas Gleixner 	 * operands will be blocked until the %gs update completes, which is
1181f36cf386SThomas Gleixner 	 * good enough for our purposes.
1182f36cf386SThomas Gleixner 	 */
1183ed5194c2SAndi Kleen 
1184b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_TREMONT,	NO_EIBRS_PBRSB),
1185b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_TREMONT_L,	NO_EIBRS_PBRSB),
1186b24e466aSTony Luck 	VULNWL_INTEL(INTEL_ATOM_TREMONT_D,	NO_ITLB_MULTIHIT | NO_EIBRS_PBRSB),
1187cad14885SPawan Gupta 
1188ed5194c2SAndi Kleen 	/* AMD Family 0xf - 0x12 */
1189be482ff9SPawan Gupta 	VULNWL_AMD(0x0f,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
1190be482ff9SPawan Gupta 	VULNWL_AMD(0x10,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
1191be482ff9SPawan Gupta 	VULNWL_AMD(0x11,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
1192be482ff9SPawan Gupta 	VULNWL_AMD(0x12,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
119336ad3513SThomas Gleixner 
119436ad3513SThomas Gleixner 	/* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */
1195be482ff9SPawan Gupta 	VULNWL_AMD(X86_FAMILY_ANY,	NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB | NO_BHI),
1196be482ff9SPawan Gupta 	VULNWL_HYGON(X86_FAMILY_ANY,	NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB | NO_BHI),
11971e41a766STony W Wang-oc 
11981e41a766STony W Wang-oc 	/* Zhaoxin Family 7 */
1199be482ff9SPawan Gupta 	VULNWL(CENTAUR,	7, X86_MODEL_ANY,	NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO | NO_BHI),
1200be482ff9SPawan Gupta 	VULNWL(ZHAOXIN,	7, X86_MODEL_ANY,	NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO | NO_BHI),
1201fec9434aSDavid Woodhouse 	{}
1202fec9434aSDavid Woodhouse };
1203fec9434aSDavid Woodhouse 
12046b80b59bSAlexandre Chartre #define VULNBL(vendor, family, model, blacklist)	\
12056b80b59bSAlexandre Chartre 	X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, blacklist)
12066b80b59bSAlexandre Chartre 
1207b24e466aSTony Luck #define VULNBL_INTEL_STEPPINGS(vfm, steppings, issues)		   \
1208b24e466aSTony Luck 	X86_MATCH_VFM_STEPPINGS(vfm, steppings, issues)
12097e5b3c26SMark Gross 
12106b80b59bSAlexandre Chartre #define VULNBL_AMD(family, blacklist)		\
12116b80b59bSAlexandre Chartre 	VULNBL(AMD, family, X86_MODEL_ANY, blacklist)
12126b80b59bSAlexandre Chartre 
12136b80b59bSAlexandre Chartre #define VULNBL_HYGON(family, blacklist)		\
12146b80b59bSAlexandre Chartre 	VULNBL(HYGON, family, X86_MODEL_ANY, blacklist)
12156b80b59bSAlexandre Chartre 
12167e5b3c26SMark Gross #define SRBDS		BIT(0)
121751802186SPawan Gupta /* CPU is affected by X86_BUG_MMIO_STALE_DATA */
121851802186SPawan Gupta #define MMIO		BIT(1)
1219a992b8a4SPawan Gupta /* CPU is affected by Shared Buffers Data Sampling (SBDS), a variant of X86_BUG_MMIO_STALE_DATA */
1220a992b8a4SPawan Gupta #define MMIO_SBDS	BIT(2)
12216b80b59bSAlexandre Chartre /* CPU is affected by RETbleed, speculating where you would not expect it */
12226b80b59bSAlexandre Chartre #define RETBLEED	BIT(3)
1223be8de49bSTom Lendacky /* CPU is affected by SMT (cross-thread) return predictions */
1224be8de49bSTom Lendacky #define SMT_RSB		BIT(4)
1225fb3bd914SBorislav Petkov (AMD) /* CPU is affected by SRSO */
1226fb3bd914SBorislav Petkov (AMD) #define SRSO		BIT(5)
12278974eb58SDaniel Sneddon /* CPU is affected by GDS */
122864094e7eSLinus Torvalds #define GDS		BIT(6)
12298076fcdeSPawan Gupta /* CPU is affected by Register File Data Sampling */
12308076fcdeSPawan Gupta #define RFDS		BIT(7)
12317e5b3c26SMark Gross 
12327e5b3c26SMark Gross static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
1233b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_IVYBRIDGE,		X86_STEPPING_ANY,		SRBDS),
1234b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_HASWELL,		X86_STEPPING_ANY,		SRBDS),
1235b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_HASWELL_L,		X86_STEPPING_ANY,		SRBDS),
1236b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_HASWELL_G,		X86_STEPPING_ANY,		SRBDS),
1237b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_HASWELL_X,		X86_STEPPING_ANY,		MMIO),
1238b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_BROADWELL_D,	X86_STEPPING_ANY,		MMIO),
1239b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_BROADWELL_G,	X86_STEPPING_ANY,		SRBDS),
1240b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_BROADWELL_X,	X86_STEPPING_ANY,		MMIO),
1241b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_BROADWELL,		X86_STEPPING_ANY,		SRBDS),
1242b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_SKYLAKE_X,		X86_STEPPING_ANY,		MMIO | RETBLEED | GDS),
1243b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_SKYLAKE_L,		X86_STEPPING_ANY,		MMIO | RETBLEED | GDS | SRBDS),
1244b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_SKYLAKE,		X86_STEPPING_ANY,		MMIO | RETBLEED | GDS | SRBDS),
1245b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_KABYLAKE_L,	X86_STEPPING_ANY,		MMIO | RETBLEED | GDS | SRBDS),
1246b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_KABYLAKE,		X86_STEPPING_ANY,		MMIO | RETBLEED | GDS | SRBDS),
1247b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_CANNONLAKE_L,	X86_STEPPING_ANY,		RETBLEED),
1248b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ICELAKE_L,		X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RETBLEED | GDS),
1249b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ICELAKE_D,		X86_STEPPING_ANY,		MMIO | GDS),
1250b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ICELAKE_X,		X86_STEPPING_ANY,		MMIO | GDS),
1251b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_COMETLAKE,		X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RETBLEED | GDS),
1252b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_COMETLAKE_L,	X86_STEPPINGS(0x0, 0x0),	MMIO | RETBLEED),
1253b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_COMETLAKE_L,	X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RETBLEED | GDS),
1254b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_TIGERLAKE_L,	X86_STEPPING_ANY,		GDS),
1255b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_TIGERLAKE,		X86_STEPPING_ANY,		GDS),
1256b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_LAKEFIELD,		X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RETBLEED),
1257b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ROCKETLAKE,	X86_STEPPING_ANY,		MMIO | RETBLEED | GDS),
1258b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ALDERLAKE,		X86_STEPPING_ANY,		RFDS),
1259b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ALDERLAKE_L,	X86_STEPPING_ANY,		RFDS),
1260b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_RAPTORLAKE,	X86_STEPPING_ANY,		RFDS),
1261b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_RAPTORLAKE_P,	X86_STEPPING_ANY,		RFDS),
1262b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_RAPTORLAKE_S,	X86_STEPPING_ANY,		RFDS),
1263b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ATOM_GRACEMONT,	X86_STEPPING_ANY,		RFDS),
1264b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ATOM_TREMONT,	X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RFDS),
1265b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ATOM_TREMONT_D,	X86_STEPPING_ANY,		MMIO | RFDS),
1266b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ATOM_TREMONT_L,	X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RFDS),
1267b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ATOM_GOLDMONT,	X86_STEPPING_ANY,		RFDS),
1268b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ATOM_GOLDMONT_D,	X86_STEPPING_ANY,		RFDS),
1269b24e466aSTony Luck 	VULNBL_INTEL_STEPPINGS(INTEL_ATOM_GOLDMONT_PLUS, X86_STEPPING_ANY,		RFDS),
12706b80b59bSAlexandre Chartre 
12716b80b59bSAlexandre Chartre 	VULNBL_AMD(0x15, RETBLEED),
12726b80b59bSAlexandre Chartre 	VULNBL_AMD(0x16, RETBLEED),
1273fb3bd914SBorislav Petkov (AMD) 	VULNBL_AMD(0x17, RETBLEED | SMT_RSB | SRSO),
1274a5ef7d68SPu Wen 	VULNBL_HYGON(0x18, RETBLEED | SMT_RSB | SRSO),
1275fb3bd914SBorislav Petkov (AMD) 	VULNBL_AMD(0x19, SRSO),
12767e5b3c26SMark Gross 	{}
12777e5b3c26SMark Gross };
12787e5b3c26SMark Gross 
cpu_matches(const struct x86_cpu_id * table,unsigned long which)127993920f61SMark Gross static bool __init cpu_matches(const struct x86_cpu_id *table, unsigned long which)
128036ad3513SThomas Gleixner {
128193920f61SMark Gross 	const struct x86_cpu_id *m = x86_match_cpu(table);
1282fec9434aSDavid Woodhouse 
128336ad3513SThomas Gleixner 	return m && !!(m->driver_data & which);
128436ad3513SThomas Gleixner }
128517dbca11SAndi Kleen 
x86_read_arch_cap_msr(void)1286286836a7SPawan Gupta u64 x86_read_arch_cap_msr(void)
1287fec9434aSDavid Woodhouse {
1288d0485730SIngo Molnar 	u64 x86_arch_cap_msr = 0;
1289fec9434aSDavid Woodhouse 
1290286836a7SPawan Gupta 	if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1291d0485730SIngo Molnar 		rdmsrl(MSR_IA32_ARCH_CAPABILITIES, x86_arch_cap_msr);
1292286836a7SPawan Gupta 
1293d0485730SIngo Molnar 	return x86_arch_cap_msr;
1294286836a7SPawan Gupta }
1295286836a7SPawan Gupta 
arch_cap_mmio_immune(u64 x86_arch_cap_msr)1296d0485730SIngo Molnar static bool arch_cap_mmio_immune(u64 x86_arch_cap_msr)
129751802186SPawan Gupta {
1298d0485730SIngo Molnar 	return (x86_arch_cap_msr & ARCH_CAP_FBSDP_NO &&
1299d0485730SIngo Molnar 		x86_arch_cap_msr & ARCH_CAP_PSDP_NO &&
1300d0485730SIngo Molnar 		x86_arch_cap_msr & ARCH_CAP_SBDR_SSDP_NO);
130151802186SPawan Gupta }
130251802186SPawan Gupta 
vulnerable_to_rfds(u64 x86_arch_cap_msr)1303d0485730SIngo Molnar static bool __init vulnerable_to_rfds(u64 x86_arch_cap_msr)
13048076fcdeSPawan Gupta {
13058076fcdeSPawan Gupta 	/* The "immunity" bit trumps everything else: */
1306d0485730SIngo Molnar 	if (x86_arch_cap_msr & ARCH_CAP_RFDS_NO)
13078076fcdeSPawan Gupta 		return false;
13088076fcdeSPawan Gupta 
13098076fcdeSPawan Gupta 	/*
13108076fcdeSPawan Gupta 	 * VMMs set ARCH_CAP_RFDS_CLEAR for processors not in the blacklist to
13118076fcdeSPawan Gupta 	 * indicate that mitigation is needed because guest is running on a
13128076fcdeSPawan Gupta 	 * vulnerable hardware or may migrate to such hardware:
13138076fcdeSPawan Gupta 	 */
1314d0485730SIngo Molnar 	if (x86_arch_cap_msr & ARCH_CAP_RFDS_CLEAR)
13158076fcdeSPawan Gupta 		return true;
13168076fcdeSPawan Gupta 
13178076fcdeSPawan Gupta 	/* Only consult the blacklist when there is no enumeration: */
13188076fcdeSPawan Gupta 	return cpu_matches(cpu_vuln_blacklist, RFDS);
13198076fcdeSPawan Gupta }
13208076fcdeSPawan Gupta 
cpu_set_bug_bits(struct cpuinfo_x86 * c)1321286836a7SPawan Gupta static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
1322286836a7SPawan Gupta {
1323d0485730SIngo Molnar 	u64 x86_arch_cap_msr = x86_read_arch_cap_msr();
1324286836a7SPawan Gupta 
1325db4d30fbSVineela Tummalapalli 	/* Set ITLB_MULTIHIT bug if cpu is not in the whitelist and not mitigated */
132693920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_ITLB_MULTIHIT) &&
1327d0485730SIngo Molnar 	    !(x86_arch_cap_msr & ARCH_CAP_PSCHANGE_MC_NO))
1328db4d30fbSVineela Tummalapalli 		setup_force_cpu_bug(X86_BUG_ITLB_MULTIHIT);
1329db4d30fbSVineela Tummalapalli 
133093920f61SMark Gross 	if (cpu_matches(cpu_vuln_whitelist, NO_SPECULATION))
13318ecc4979SDominik Brodowski 		return;
13328ecc4979SDominik Brodowski 
13338ecc4979SDominik Brodowski 	setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
13341e41a766STony W Wang-oc 
133593920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_SPECTRE_V2))
13368ecc4979SDominik Brodowski 		setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
13378ecc4979SDominik Brodowski 
133893920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_SSB) &&
1339d0485730SIngo Molnar 	    !(x86_arch_cap_msr & ARCH_CAP_SSB_NO) &&
134024809860SKonrad Rzeszutek Wilk 	   !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
1341c456442cSKonrad Rzeszutek Wilk 		setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
1342c456442cSKonrad Rzeszutek Wilk 
1343e7862edaSKim Phillips 	/*
1344e7862edaSKim Phillips 	 * AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature
1345e7862edaSKim Phillips 	 * flag and protect from vendor-specific bugs via the whitelist.
1346acaa4b5cSKim Phillips 	 *
1347acaa4b5cSKim Phillips 	 * Don't use AutoIBRS when SNP is enabled because it degrades host
1348acaa4b5cSKim Phillips 	 * userspace indirect branch performance.
1349e7862edaSKim Phillips 	 */
1350d0485730SIngo Molnar 	if ((x86_arch_cap_msr & ARCH_CAP_IBRS_ALL) ||
1351acaa4b5cSKim Phillips 	    (cpu_has(c, X86_FEATURE_AUTOIBRS) &&
1352acaa4b5cSKim Phillips 	     !cpu_feature_enabled(X86_FEATURE_SEV_SNP))) {
1353706d5168SSai Praneeth 		setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
1354e7862edaSKim Phillips 		if (!cpu_matches(cpu_vuln_whitelist, NO_EIBRS_PBRSB) &&
1355d0485730SIngo Molnar 		    !(x86_arch_cap_msr & ARCH_CAP_PBRSB_NO))
1356e7862edaSKim Phillips 			setup_force_cpu_bug(X86_BUG_EIBRS_PBRSB);
1357e7862edaSKim Phillips 	}
1358706d5168SSai Praneeth 
135993920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_MDS) &&
1360d0485730SIngo Molnar 	    !(x86_arch_cap_msr & ARCH_CAP_MDS_NO)) {
1361ed5194c2SAndi Kleen 		setup_force_cpu_bug(X86_BUG_MDS);
136293920f61SMark Gross 		if (cpu_matches(cpu_vuln_whitelist, MSBDS_ONLY))
1363e261f209SThomas Gleixner 			setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
1364e261f209SThomas Gleixner 	}
1365ed5194c2SAndi Kleen 
136693920f61SMark Gross 	if (!cpu_matches(cpu_vuln_whitelist, NO_SWAPGS))
1367f36cf386SThomas Gleixner 		setup_force_cpu_bug(X86_BUG_SWAPGS);
1368f36cf386SThomas Gleixner 
13691b42f017SPawan Gupta 	/*
13701b42f017SPawan Gupta 	 * When the CPU is not mitigated for TAA (TAA_NO=0) set TAA bug when:
13711b42f017SPawan Gupta 	 *	- TSX is supported or
13721b42f017SPawan Gupta 	 *	- TSX_CTRL is present
13731b42f017SPawan Gupta 	 *
13741b42f017SPawan Gupta 	 * TSX_CTRL check is needed for cases when TSX could be disabled before
13751b42f017SPawan Gupta 	 * the kernel boot e.g. kexec.
13761b42f017SPawan Gupta 	 * TSX_CTRL check alone is not sufficient for cases when the microcode
13771b42f017SPawan Gupta 	 * update is not present or running as guest that don't get TSX_CTRL.
13781b42f017SPawan Gupta 	 */
1379d0485730SIngo Molnar 	if (!(x86_arch_cap_msr & ARCH_CAP_TAA_NO) &&
13801b42f017SPawan Gupta 	    (cpu_has(c, X86_FEATURE_RTM) ||
1381d0485730SIngo Molnar 	     (x86_arch_cap_msr & ARCH_CAP_TSX_CTRL_MSR)))
13821b42f017SPawan Gupta 		setup_force_cpu_bug(X86_BUG_TAA);
13831b42f017SPawan Gupta 
13847e5b3c26SMark Gross 	/*
13857e5b3c26SMark Gross 	 * SRBDS affects CPUs which support RDRAND or RDSEED and are listed
13867e5b3c26SMark Gross 	 * in the vulnerability blacklist.
1387a992b8a4SPawan Gupta 	 *
1388a992b8a4SPawan Gupta 	 * Some of the implications and mitigation of Shared Buffers Data
1389a992b8a4SPawan Gupta 	 * Sampling (SBDS) are similar to SRBDS. Give SBDS same treatment as
1390a992b8a4SPawan Gupta 	 * SRBDS.
13917e5b3c26SMark Gross 	 */
13927e5b3c26SMark Gross 	if ((cpu_has(c, X86_FEATURE_RDRAND) ||
13937e5b3c26SMark Gross 	     cpu_has(c, X86_FEATURE_RDSEED)) &&
1394a992b8a4SPawan Gupta 	    cpu_matches(cpu_vuln_blacklist, SRBDS | MMIO_SBDS))
13957e5b3c26SMark Gross 		    setup_force_cpu_bug(X86_BUG_SRBDS);
13967e5b3c26SMark Gross 
139751802186SPawan Gupta 	/*
139851802186SPawan Gupta 	 * Processor MMIO Stale Data bug enumeration
139951802186SPawan Gupta 	 *
140051802186SPawan Gupta 	 * Affected CPU list is generally enough to enumerate the vulnerability,
140151802186SPawan Gupta 	 * but for virtualization case check for ARCH_CAP MSR bits also, VMM may
140251802186SPawan Gupta 	 * not want the guest to enumerate the bug.
14037df54884SPawan Gupta 	 *
14047df54884SPawan Gupta 	 * Set X86_BUG_MMIO_UNKNOWN for CPUs that are neither in the blacklist,
14057df54884SPawan Gupta 	 * nor in the whitelist and also don't enumerate MSR ARCH_CAP MMIO bits.
140651802186SPawan Gupta 	 */
1407d0485730SIngo Molnar 	if (!arch_cap_mmio_immune(x86_arch_cap_msr)) {
14087df54884SPawan Gupta 		if (cpu_matches(cpu_vuln_blacklist, MMIO))
140951802186SPawan Gupta 			setup_force_cpu_bug(X86_BUG_MMIO_STALE_DATA);
14107df54884SPawan Gupta 		else if (!cpu_matches(cpu_vuln_whitelist, NO_MMIO))
14117df54884SPawan Gupta 			setup_force_cpu_bug(X86_BUG_MMIO_UNKNOWN);
14127df54884SPawan Gupta 	}
141351802186SPawan Gupta 
141426aae8ccSAndrew Cooper 	if (!cpu_has(c, X86_FEATURE_BTC_NO)) {
1415d0485730SIngo Molnar 		if (cpu_matches(cpu_vuln_blacklist, RETBLEED) || (x86_arch_cap_msr & ARCH_CAP_RSBA))
14166b80b59bSAlexandre Chartre 			setup_force_cpu_bug(X86_BUG_RETBLEED);
141726aae8ccSAndrew Cooper 	}
14186b80b59bSAlexandre Chartre 
1419be8de49bSTom Lendacky 	if (cpu_matches(cpu_vuln_blacklist, SMT_RSB))
1420be8de49bSTom Lendacky 		setup_force_cpu_bug(X86_BUG_SMT_RSB);
1421be8de49bSTom Lendacky 
14221b5277c0SBorislav Petkov (AMD) 	if (!cpu_has(c, X86_FEATURE_SRSO_NO)) {
1423fb3bd914SBorislav Petkov (AMD) 		if (cpu_matches(cpu_vuln_blacklist, SRSO))
1424fb3bd914SBorislav Petkov (AMD) 			setup_force_cpu_bug(X86_BUG_SRSO);
14251b5277c0SBorislav Petkov (AMD) 	}
1426fb3bd914SBorislav Petkov (AMD) 
14278974eb58SDaniel Sneddon 	/*
14288974eb58SDaniel Sneddon 	 * Check if CPU is vulnerable to GDS. If running in a virtual machine on
14298974eb58SDaniel Sneddon 	 * an affected processor, the VMM may have disabled the use of GATHER by
14308974eb58SDaniel Sneddon 	 * disabling AVX2. The only way to do this in HW is to clear XCR0[2],
14318974eb58SDaniel Sneddon 	 * which means that AVX will be disabled.
14328974eb58SDaniel Sneddon 	 */
1433d0485730SIngo Molnar 	if (cpu_matches(cpu_vuln_blacklist, GDS) && !(x86_arch_cap_msr & ARCH_CAP_GDS_NO) &&
14348974eb58SDaniel Sneddon 	    boot_cpu_has(X86_FEATURE_AVX))
14358974eb58SDaniel Sneddon 		setup_force_cpu_bug(X86_BUG_GDS);
14368974eb58SDaniel Sneddon 
1437d0485730SIngo Molnar 	if (vulnerable_to_rfds(x86_arch_cap_msr))
14388076fcdeSPawan Gupta 		setup_force_cpu_bug(X86_BUG_RFDS);
14398076fcdeSPawan Gupta 
1440be482ff9SPawan Gupta 	/* When virtualized, eIBRS could be hidden, assume vulnerable */
1441d0485730SIngo Molnar 	if (!(x86_arch_cap_msr & ARCH_CAP_BHI_NO) &&
1442be482ff9SPawan Gupta 	    !cpu_matches(cpu_vuln_whitelist, NO_BHI) &&
1443be482ff9SPawan Gupta 	    (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED) ||
1444be482ff9SPawan Gupta 	     boot_cpu_has(X86_FEATURE_HYPERVISOR)))
1445be482ff9SPawan Gupta 		setup_force_cpu_bug(X86_BUG_BHI);
1446be482ff9SPawan Gupta 
14473ea87dfaSJohannes Wikner 	if (cpu_has(c, X86_FEATURE_AMD_IBPB) && !cpu_has(c, X86_FEATURE_AMD_IBPB_RET))
14483ea87dfaSJohannes Wikner 		setup_force_cpu_bug(X86_BUG_IBPB_NO_RET);
14493ea87dfaSJohannes Wikner 
145093920f61SMark Gross 	if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
14514a28bfe3SKonrad Rzeszutek Wilk 		return;
1452fec9434aSDavid Woodhouse 
1453fec9434aSDavid Woodhouse 	/* Rogue Data Cache Load? No! */
1454d0485730SIngo Molnar 	if (x86_arch_cap_msr & ARCH_CAP_RDCL_NO)
14554a28bfe3SKonrad Rzeszutek Wilk 		return;
1456fec9434aSDavid Woodhouse 
14574a28bfe3SKonrad Rzeszutek Wilk 	setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
145817dbca11SAndi Kleen 
145993920f61SMark Gross 	if (cpu_matches(cpu_vuln_whitelist, NO_L1TF))
146017dbca11SAndi Kleen 		return;
146117dbca11SAndi Kleen 
146217dbca11SAndi Kleen 	setup_force_cpu_bug(X86_BUG_L1TF);
1463fec9434aSDavid Woodhouse }
1464fec9434aSDavid Woodhouse 
146534048c9eSPaolo Ciarrocchi /*
14668990cac6SPavel Tatashin  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
14678990cac6SPavel Tatashin  * unfortunately, that's not true in practice because of early VIA
14688990cac6SPavel Tatashin  * chips and (more importantly) broken virtualizers that are not easy
14698990cac6SPavel Tatashin  * to detect. In the latter case it doesn't even *fail* reliably, so
14708990cac6SPavel Tatashin  * probing for it doesn't even work. Disable it completely on 32-bit
14718990cac6SPavel Tatashin  * unless we can find a reliable way to detect all the broken cases.
14728990cac6SPavel Tatashin  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
14738990cac6SPavel Tatashin  */
detect_nopl(void)14749b3661cdSBorislav Petkov static void detect_nopl(void)
14758990cac6SPavel Tatashin {
14768990cac6SPavel Tatashin #ifdef CONFIG_X86_32
14779b3661cdSBorislav Petkov 	setup_clear_cpu_cap(X86_FEATURE_NOPL);
14788990cac6SPavel Tatashin #else
14799b3661cdSBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_NOPL);
14808990cac6SPavel Tatashin #endif
14818990cac6SPavel Tatashin }
14828990cac6SPavel Tatashin 
14838990cac6SPavel Tatashin /*
14841ef5423aSMike Hommey  * We parse cpu parameters early because fpu__init_system() is executed
14851ef5423aSMike Hommey  * before parse_early_param().
14861ef5423aSMike Hommey  */
cpu_parse_early_param(void)14871ef5423aSMike Hommey static void __init cpu_parse_early_param(void)
14881ef5423aSMike Hommey {
14891ef5423aSMike Hommey 	char arg[128];
14901625c833SBorislav Petkov 	char *argptr = arg, *opt;
14911625c833SBorislav Petkov 	int arglen, taint = 0;
14921ef5423aSMike Hommey 
14931ef5423aSMike Hommey #ifdef CONFIG_X86_32
14941ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "no387"))
14951ef5423aSMike Hommey #ifdef CONFIG_MATH_EMULATION
14961ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_FPU);
14971ef5423aSMike Hommey #else
14981ef5423aSMike Hommey 		pr_err("Option 'no387' required CONFIG_MATH_EMULATION enabled.\n");
14991ef5423aSMike Hommey #endif
15001ef5423aSMike Hommey 
15011ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "nofxsr"))
15021ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_FXSR);
15031ef5423aSMike Hommey #endif
15041ef5423aSMike Hommey 
15051ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "noxsave"))
15061ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_XSAVE);
15071ef5423aSMike Hommey 
15081ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
15091ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
15101ef5423aSMike Hommey 
15111ef5423aSMike Hommey 	if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
15121ef5423aSMike Hommey 		setup_clear_cpu_cap(X86_FEATURE_XSAVES);
15131ef5423aSMike Hommey 
15140dc2a760SRick Edgecombe 	if (cmdline_find_option_bool(boot_command_line, "nousershstk"))
15150dc2a760SRick Edgecombe 		setup_clear_cpu_cap(X86_FEATURE_USER_SHSTK);
15160dc2a760SRick Edgecombe 
1517989b5cfaSXin Li (Intel) 	/* Minimize the gap between FRED is available and available but disabled. */
1518989b5cfaSXin Li (Intel) 	arglen = cmdline_find_option(boot_command_line, "fred", arg, sizeof(arg));
1519989b5cfaSXin Li (Intel) 	if (arglen != 2 || strncmp(arg, "on", 2))
1520989b5cfaSXin Li (Intel) 		setup_clear_cpu_cap(X86_FEATURE_FRED);
1521989b5cfaSXin Li (Intel) 
15221ef5423aSMike Hommey 	arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg));
15231ef5423aSMike Hommey 	if (arglen <= 0)
15241ef5423aSMike Hommey 		return;
15251ef5423aSMike Hommey 
15261ef5423aSMike Hommey 	pr_info("Clearing CPUID bits:");
15271ef5423aSMike Hommey 
15281625c833SBorislav Petkov 	while (argptr) {
15291625c833SBorislav Petkov 		bool found __maybe_unused = false;
15301625c833SBorislav Petkov 		unsigned int bit;
15311ef5423aSMike Hommey 
15321625c833SBorislav Petkov 		opt = strsep(&argptr, ",");
15331625c833SBorislav Petkov 
15341625c833SBorislav Petkov 		/*
15351625c833SBorislav Petkov 		 * Handle naked numbers first for feature flags which don't
15361625c833SBorislav Petkov 		 * have names.
15371625c833SBorislav Petkov 		 */
15381625c833SBorislav Petkov 		if (!kstrtouint(opt, 10, &bit)) {
15391625c833SBorislav Petkov 			if (bit < NCAPINTS * 32) {
15401625c833SBorislav Petkov 
15411625c833SBorislav Petkov 				/* empty-string, i.e., ""-defined feature flags */
15421625c833SBorislav Petkov 				if (!x86_cap_flags[bit])
15431625c833SBorislav Petkov 					pr_cont(" " X86_CAP_FMT_NUM, x86_cap_flag_num(bit));
15441625c833SBorislav Petkov 				else
15451ef5423aSMike Hommey 					pr_cont(" " X86_CAP_FMT, x86_cap_flag(bit));
15461625c833SBorislav Petkov 
15471ef5423aSMike Hommey 				setup_clear_cpu_cap(bit);
15481625c833SBorislav Petkov 				taint++;
15491ef5423aSMike Hommey 			}
15501625c833SBorislav Petkov 			/*
15511625c833SBorislav Petkov 			 * The assumption is that there are no feature names with only
15521625c833SBorislav Petkov 			 * numbers in the name thus go to the next argument.
15531625c833SBorislav Petkov 			 */
15541625c833SBorislav Petkov 			continue;
15551625c833SBorislav Petkov 		}
15561625c833SBorislav Petkov 
15571625c833SBorislav Petkov 		for (bit = 0; bit < 32 * NCAPINTS; bit++) {
15581625c833SBorislav Petkov 			if (!x86_cap_flag(bit))
15591625c833SBorislav Petkov 				continue;
15601625c833SBorislav Petkov 
15611625c833SBorislav Petkov 			if (strcmp(x86_cap_flag(bit), opt))
15621625c833SBorislav Petkov 				continue;
15631625c833SBorislav Petkov 
15641625c833SBorislav Petkov 			pr_cont(" %s", opt);
15651625c833SBorislav Petkov 			setup_clear_cpu_cap(bit);
15661625c833SBorislav Petkov 			taint++;
15671625c833SBorislav Petkov 			found = true;
15681625c833SBorislav Petkov 			break;
15691625c833SBorislav Petkov 		}
15701625c833SBorislav Petkov 
15711625c833SBorislav Petkov 		if (!found)
15721625c833SBorislav Petkov 			pr_cont(" (unknown: %s)", opt);
15731625c833SBorislav Petkov 	}
15741ef5423aSMike Hommey 	pr_cont("\n");
15751625c833SBorislav Petkov 
15761625c833SBorislav Petkov 	if (taint)
15771625c833SBorislav Petkov 		add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
15781ef5423aSMike Hommey }
15791ef5423aSMike Hommey 
15801ef5423aSMike Hommey /*
158134048c9eSPaolo Ciarrocchi  * Do minimum CPU detection early.
158234048c9eSPaolo Ciarrocchi  * Fields really needed: vendor, cpuid_level, family, model, mask,
158334048c9eSPaolo Ciarrocchi  * cache alignment.
158434048c9eSPaolo Ciarrocchi  * The others are not touched to avoid unwanted side effects.
158534048c9eSPaolo Ciarrocchi  *
1586a1652bb8SJean Delvare  * WARNING: this function is only called on the boot CPU.  Don't add code
1587a1652bb8SJean Delvare  * here that is supposed to run on all CPUs.
158834048c9eSPaolo Ciarrocchi  */
early_identify_cpu(struct cpuinfo_x86 * c)15893da99c97SYinghai Lu static void __init early_identify_cpu(struct cpuinfo_x86 *c)
1590f7627e25SThomas Gleixner {
15910e96f31eSJordan Borgner 	memset(&c->x86_capability, 0, sizeof(c->x86_capability));
15920a488a53SYinghai Lu 	c->extended_cpuid_level = 0;
15930a488a53SYinghai Lu 
15942893cc8fSMatthew Whitehead 	if (!have_cpuid_p())
15952893cc8fSMatthew Whitehead 		identify_cpu_without_cpuid(c);
15962893cc8fSMatthew Whitehead 
1597aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
159805fb3c19SAndy Lutomirski 	if (have_cpuid_p()) {
1599f7627e25SThomas Gleixner 		cpu_detect(c);
16003da99c97SYinghai Lu 		get_cpu_vendor(c);
16010c2f6d04SThomas Gleixner 		intel_unlock_cpuid_leafs(c);
16023da99c97SYinghai Lu 		get_cpu_cap(c);
160378d1b296SBorislav Petkov 		setup_force_cpu_cap(X86_FEATURE_CPUID);
16049a458198SPaolo Bonzini 		get_cpu_address_sizes(c);
16051ef5423aSMike Hommey 		cpu_parse_early_param();
160612cf105cSKrzysztof Helt 
1607ebdb2036SThomas Gleixner 		cpu_init_topology(c);
1608ebdb2036SThomas Gleixner 
160910a434fcSYinghai Lu 		if (this_cpu->c_early_init)
161010a434fcSYinghai Lu 			this_cpu->c_early_init(c);
16113da99c97SYinghai Lu 
1612f6e9456cSRobert Richter 		c->cpu_index = 0;
1613b38b0665SH. Peter Anvin 		filter_cpuid_features(c, false);
1614de5397adSFenghua Yu 
1615a110b5ecSBorislav Petkov 		if (this_cpu->c_bsp_init)
1616a110b5ecSBorislav Petkov 			this_cpu->c_bsp_init(c);
161778d1b296SBorislav Petkov 	} else {
161878d1b296SBorislav Petkov 		setup_clear_cpu_cap(X86_FEATURE_CPUID);
1619fbf6449fSAdam Dunlap 		get_cpu_address_sizes(c);
1620ebdb2036SThomas Gleixner 		cpu_init_topology(c);
162105fb3c19SAndy Lutomirski 	}
1622c3b83598SBorislav Petkov 
1623c3b83598SBorislav Petkov 	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
1624a89f040fSThomas Gleixner 
16254a28bfe3SKonrad Rzeszutek Wilk 	cpu_set_bug_bits(c);
162699c6fa25SDavid Woodhouse 
1627ebb1064eSFenghua Yu 	sld_setup(c);
16286650cdd9SPeter Zijlstra (Intel) 
1629b8b7abaeSAndy Lutomirski #ifdef CONFIG_X86_32
1630b8b7abaeSAndy Lutomirski 	/*
1631b8b7abaeSAndy Lutomirski 	 * Regardless of whether PCID is enumerated, the SDM says
1632b8b7abaeSAndy Lutomirski 	 * that it can't be enabled in 32-bit mode.
1633b8b7abaeSAndy Lutomirski 	 */
1634b8b7abaeSAndy Lutomirski 	setup_clear_cpu_cap(X86_FEATURE_PCID);
1635b8b7abaeSAndy Lutomirski #endif
1636372fddf7SKirill A. Shutemov 
1637372fddf7SKirill A. Shutemov 	/*
1638372fddf7SKirill A. Shutemov 	 * Later in the boot process pgtable_l5_enabled() relies on
1639372fddf7SKirill A. Shutemov 	 * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not
1640372fddf7SKirill A. Shutemov 	 * enabled by this point we need to clear the feature bit to avoid
1641372fddf7SKirill A. Shutemov 	 * false-positives at the later stage.
1642372fddf7SKirill A. Shutemov 	 *
1643372fddf7SKirill A. Shutemov 	 * pgtable_l5_enabled() can be false here for several reasons:
1644372fddf7SKirill A. Shutemov 	 *  - 5-level paging is disabled compile-time;
1645372fddf7SKirill A. Shutemov 	 *  - it's 32-bit kernel;
1646372fddf7SKirill A. Shutemov 	 *  - machine doesn't support 5-level paging;
1647372fddf7SKirill A. Shutemov 	 *  - user specified 'no5lvl' in kernel command line.
1648372fddf7SKirill A. Shutemov 	 */
1649372fddf7SKirill A. Shutemov 	if (!pgtable_l5_enabled())
1650372fddf7SKirill A. Shutemov 		setup_clear_cpu_cap(X86_FEATURE_LA57);
16518990cac6SPavel Tatashin 
16529b3661cdSBorislav Petkov 	detect_nopl();
1653f7627e25SThomas Gleixner }
1654f7627e25SThomas Gleixner 
early_cpu_init(void)16559d31d35bSYinghai Lu void __init early_cpu_init(void)
16569d31d35bSYinghai Lu {
165702dde8b4SJan Beulich 	const struct cpu_dev *const *cdev;
165810a434fcSYinghai Lu 	int count = 0;
16599d31d35bSYinghai Lu 
1660ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
16611b74dde7SChen Yucong 	pr_info("KERNEL supported cpus:\n");
166231c997caSIngo Molnar #endif
166331c997caSIngo Molnar 
166410a434fcSYinghai Lu 	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
166502dde8b4SJan Beulich 		const struct cpu_dev *cpudev = *cdev;
16669d31d35bSYinghai Lu 
166710a434fcSYinghai Lu 		if (count >= X86_VENDOR_NUM)
166810a434fcSYinghai Lu 			break;
166910a434fcSYinghai Lu 		cpu_devs[count] = cpudev;
167010a434fcSYinghai Lu 		count++;
167110a434fcSYinghai Lu 
1672ac23f253SJan Beulich #ifdef CONFIG_PROCESSOR_SELECT
167331c997caSIngo Molnar 		{
167431c997caSIngo Molnar 			unsigned int j;
167531c997caSIngo Molnar 
167610a434fcSYinghai Lu 			for (j = 0; j < 2; j++) {
167710a434fcSYinghai Lu 				if (!cpudev->c_ident[j])
167810a434fcSYinghai Lu 					continue;
16791b74dde7SChen Yucong 				pr_info("  %s %s\n", cpudev->c_vendor,
168010a434fcSYinghai Lu 					cpudev->c_ident[j]);
168110a434fcSYinghai Lu 			}
168210a434fcSYinghai Lu 		}
16830388423dSDave Jones #endif
168431c997caSIngo Molnar 	}
16859d31d35bSYinghai Lu 	early_identify_cpu(&boot_cpu_data);
1686f7627e25SThomas Gleixner }
1687f7627e25SThomas Gleixner 
detect_null_seg_behavior(void)1688415de440SJane Malalane static bool detect_null_seg_behavior(void)
16897a5d6704SAndy Lutomirski {
169058a5aac5SAndy Lutomirski 	/*
16917a5d6704SAndy Lutomirski 	 * Empirically, writing zero to a segment selector on AMD does
16927a5d6704SAndy Lutomirski 	 * not clear the base, whereas writing zero to a segment
16937a5d6704SAndy Lutomirski 	 * selector on Intel does clear the base.  Intel's behavior
16947a5d6704SAndy Lutomirski 	 * allows slightly faster context switches in the common case
16957a5d6704SAndy Lutomirski 	 * where GS is unused by the prev and next threads.
169658a5aac5SAndy Lutomirski 	 *
16977a5d6704SAndy Lutomirski 	 * Since neither vendor documents this anywhere that I can see,
1698d9f6e12fSIngo Molnar 	 * detect it directly instead of hard-coding the choice by
16997a5d6704SAndy Lutomirski 	 * vendor.
17007a5d6704SAndy Lutomirski 	 *
17017a5d6704SAndy Lutomirski 	 * I've designated AMD's behavior as the "bug" because it's
17027a5d6704SAndy Lutomirski 	 * counterintuitive and less friendly.
170358a5aac5SAndy Lutomirski 	 */
17047a5d6704SAndy Lutomirski 
17057a5d6704SAndy Lutomirski 	unsigned long old_base, tmp;
17067a5d6704SAndy Lutomirski 	rdmsrl(MSR_FS_BASE, old_base);
17077a5d6704SAndy Lutomirski 	wrmsrl(MSR_FS_BASE, 1);
17087a5d6704SAndy Lutomirski 	loadsegment(fs, 0);
17097a5d6704SAndy Lutomirski 	rdmsrl(MSR_FS_BASE, tmp);
17107a5d6704SAndy Lutomirski 	wrmsrl(MSR_FS_BASE, old_base);
1711415de440SJane Malalane 	return tmp == 0;
1712415de440SJane Malalane }
1713415de440SJane Malalane 
check_null_seg_clears_base(struct cpuinfo_x86 * c)1714415de440SJane Malalane void check_null_seg_clears_base(struct cpuinfo_x86 *c)
1715415de440SJane Malalane {
1716415de440SJane Malalane 	/* BUG_NULL_SEG is only relevant with 64bit userspace */
1717415de440SJane Malalane 	if (!IS_ENABLED(CONFIG_X86_64))
1718415de440SJane Malalane 		return;
1719415de440SJane Malalane 
17205b909d4aSKim Phillips 	if (cpu_has(c, X86_FEATURE_NULL_SEL_CLR_BASE))
1721415de440SJane Malalane 		return;
1722415de440SJane Malalane 
1723415de440SJane Malalane 	/*
1724415de440SJane Malalane 	 * CPUID bit above wasn't set. If this kernel is still running
1725415de440SJane Malalane 	 * as a HV guest, then the HV has decided not to advertize
1726415de440SJane Malalane 	 * that CPUID bit for whatever reason.	For example, one
1727415de440SJane Malalane 	 * member of the migration pool might be vulnerable.  Which
1728415de440SJane Malalane 	 * means, the bug is present: set the BUG flag and return.
1729415de440SJane Malalane 	 */
1730415de440SJane Malalane 	if (cpu_has(c, X86_FEATURE_HYPERVISOR)) {
1731415de440SJane Malalane 		set_cpu_bug(c, X86_BUG_NULL_SEG);
1732415de440SJane Malalane 		return;
1733415de440SJane Malalane 	}
1734415de440SJane Malalane 
1735415de440SJane Malalane 	/*
1736415de440SJane Malalane 	 * Zen2 CPUs also have this behaviour, but no CPUID bit.
1737415de440SJane Malalane 	 * 0x18 is the respective family for Hygon.
1738415de440SJane Malalane 	 */
1739415de440SJane Malalane 	if ((c->x86 == 0x17 || c->x86 == 0x18) &&
1740415de440SJane Malalane 	    detect_null_seg_behavior())
1741415de440SJane Malalane 		return;
1742415de440SJane Malalane 
1743415de440SJane Malalane 	/* All the remaining ones are affected */
1744415de440SJane Malalane 	set_cpu_bug(c, X86_BUG_NULL_SEG);
1745f7627e25SThomas Gleixner }
1746f7627e25SThomas Gleixner 
generic_identify(struct cpuinfo_x86 * c)1747148f9bb8SPaul Gortmaker static void generic_identify(struct cpuinfo_x86 *c)
1748f7627e25SThomas Gleixner {
17493da99c97SYinghai Lu 	c->extended_cpuid_level = 0;
1750f7627e25SThomas Gleixner 
1751aef93c8bSYinghai Lu 	if (!have_cpuid_p())
1752aef93c8bSYinghai Lu 		identify_cpu_without_cpuid(c);
1753f7627e25SThomas Gleixner 
1754aef93c8bSYinghai Lu 	/* cyrix could have cpuid enabled via c_identify()*/
1755a9853dd6SIngo Molnar 	if (!have_cpuid_p())
1756aef93c8bSYinghai Lu 		return;
1757aef93c8bSYinghai Lu 
17583da99c97SYinghai Lu 	cpu_detect(c);
17593da99c97SYinghai Lu 
17603da99c97SYinghai Lu 	get_cpu_vendor(c);
17610c2f6d04SThomas Gleixner 	intel_unlock_cpuid_leafs(c);
17623da99c97SYinghai Lu 	get_cpu_cap(c);
17633da99c97SYinghai Lu 
1764d94a155cSKirill A. Shutemov 	get_cpu_address_sizes(c);
1765d94a155cSKirill A. Shutemov 
1766f7627e25SThomas Gleixner 	get_model_name(c); /* Default name */
1767f7627e25SThomas Gleixner 
17680230bb03SAndy Lutomirski 	/*
17690230bb03SAndy Lutomirski 	 * ESPFIX is a strange bug.  All real CPUs have it.  Paravirt
17700230bb03SAndy Lutomirski 	 * systems that run Linux at CPL > 0 may or may not have the
17710230bb03SAndy Lutomirski 	 * issue, but, even if they have the issue, there's absolutely
17720230bb03SAndy Lutomirski 	 * nothing we can do about it because we can't use the real IRET
17730230bb03SAndy Lutomirski 	 * instruction.
17740230bb03SAndy Lutomirski 	 *
17750230bb03SAndy Lutomirski 	 * NB: For the time being, only 32-bit kernels support
17760230bb03SAndy Lutomirski 	 * X86_BUG_ESPFIX as such.  64-bit kernels directly choose
17770230bb03SAndy Lutomirski 	 * whether to apply espfix using paravirt hooks.  If any
17780230bb03SAndy Lutomirski 	 * non-paravirt system ever shows up that does *not* have the
17790230bb03SAndy Lutomirski 	 * ESPFIX issue, we can change this.
17800230bb03SAndy Lutomirski 	 */
17810230bb03SAndy Lutomirski #ifdef CONFIG_X86_32
17820230bb03SAndy Lutomirski 	set_cpu_bug(c, X86_BUG_ESPFIX);
17830230bb03SAndy Lutomirski #endif
1784f7627e25SThomas Gleixner }
1785f7627e25SThomas Gleixner 
1786d49597fdSThomas Gleixner /*
1787f7627e25SThomas Gleixner  * This does the hard work of actually picking apart the CPU stuff...
1788f7627e25SThomas Gleixner  */
identify_cpu(struct cpuinfo_x86 * c)1789148f9bb8SPaul Gortmaker static void identify_cpu(struct cpuinfo_x86 *c)
1790f7627e25SThomas Gleixner {
1791f7627e25SThomas Gleixner 	int i;
1792f7627e25SThomas Gleixner 
1793f7627e25SThomas Gleixner 	c->loops_per_jiffy = loops_per_jiffy;
179424dbc600SGustavo A. R. Silva 	c->x86_cache_size = 0;
1795f7627e25SThomas Gleixner 	c->x86_vendor = X86_VENDOR_UNKNOWN;
1796b399151cSJia Zhang 	c->x86_model = c->x86_stepping = 0;	/* So far unknown... */
1797f7627e25SThomas Gleixner 	c->x86_vendor_id[0] = '\0'; /* Unset */
1798f7627e25SThomas Gleixner 	c->x86_model_id[0] = '\0';  /* Unset */
179911fdd252SYinghai Lu #ifdef CONFIG_X86_64
1800102bbe3aSYinghai Lu 	c->x86_clflush_size = 64;
180113c6c532SJan Beulich 	c->x86_phys_bits = 36;
180213c6c532SJan Beulich 	c->x86_virt_bits = 48;
1803102bbe3aSYinghai Lu #else
1804102bbe3aSYinghai Lu 	c->cpuid_level = -1;	/* CPUID not detected */
1805f7627e25SThomas Gleixner 	c->x86_clflush_size = 32;
180613c6c532SJan Beulich 	c->x86_phys_bits = 32;
180713c6c532SJan Beulich 	c->x86_virt_bits = 32;
1808102bbe3aSYinghai Lu #endif
1809102bbe3aSYinghai Lu 	c->x86_cache_alignment = c->x86_clflush_size;
18100e96f31eSJordan Borgner 	memset(&c->x86_capability, 0, sizeof(c->x86_capability));
1811b47ce1feSSean Christopherson #ifdef CONFIG_X86_VMX_FEATURE_NAMES
1812b47ce1feSSean Christopherson 	memset(&c->vmx_capability, 0, sizeof(c->vmx_capability));
1813b47ce1feSSean Christopherson #endif
1814f7627e25SThomas Gleixner 
1815f7627e25SThomas Gleixner 	generic_identify(c);
1816f7627e25SThomas Gleixner 
1817ebdb2036SThomas Gleixner 	cpu_parse_topology(c);
1818ebdb2036SThomas Gleixner 
18193898534dSAndi Kleen 	if (this_cpu->c_identify)
1820f7627e25SThomas Gleixner 		this_cpu->c_identify(c);
1821f7627e25SThomas Gleixner 
18226a6256f9SAdam Buchbinder 	/* Clear/Set all flags overridden by options, after probe */
18238bf1ebcaSAndy Lutomirski 	apply_forced_caps(c);
18242759c328SYinghai Lu 
182504c30245SBorislav Petkov (AMD) 	/*
182604c30245SBorislav Petkov (AMD) 	 * Set default APIC and TSC_DEADLINE MSR fencing flag. AMD and
182704c30245SBorislav Petkov (AMD) 	 * Hygon will clear it in ->c_init() below.
182804c30245SBorislav Petkov (AMD) 	 */
182904c30245SBorislav Petkov (AMD) 	set_cpu_cap(c, X86_FEATURE_APIC_MSRS_FENCE);
183004c30245SBorislav Petkov (AMD) 
1831f7627e25SThomas Gleixner 	/*
1832f7627e25SThomas Gleixner 	 * Vendor-specific initialization.  In this section we
1833f7627e25SThomas Gleixner 	 * canonicalize the feature flags, meaning if there are
1834f7627e25SThomas Gleixner 	 * features a certain CPU supports which CPUID doesn't
1835f7627e25SThomas Gleixner 	 * tell us, CPUID claiming incorrect flags, or other bugs,
1836f7627e25SThomas Gleixner 	 * we handle them here.
1837f7627e25SThomas Gleixner 	 *
1838f7627e25SThomas Gleixner 	 * At the end of this section, c->x86_capability better
1839f7627e25SThomas Gleixner 	 * indicate the features this CPU genuinely supports!
1840f7627e25SThomas Gleixner 	 */
1841f7627e25SThomas Gleixner 	if (this_cpu->c_init)
1842f7627e25SThomas Gleixner 		this_cpu->c_init(c);
1843f7627e25SThomas Gleixner 
1844f7627e25SThomas Gleixner 	/* Disable the PN if appropriate */
1845f7627e25SThomas Gleixner 	squash_the_stupid_serial_number(c);
1846f7627e25SThomas Gleixner 
1847aa35f896SRicardo Neri 	/* Set up SMEP/SMAP/UMIP */
1848b2cc2a07SH. Peter Anvin 	setup_smep(c);
1849b2cc2a07SH. Peter Anvin 	setup_smap(c);
1850aa35f896SRicardo Neri 	setup_umip(c);
1851b2cc2a07SH. Peter Anvin 
1852dd649bd0SAndy Lutomirski 	/* Enable FSGSBASE instructions if available. */
1853742c45c3SAndi Kleen 	if (cpu_has(c, X86_FEATURE_FSGSBASE)) {
1854dd649bd0SAndy Lutomirski 		cr4_set_bits(X86_CR4_FSGSBASE);
1855742c45c3SAndi Kleen 		elf_hwcap2 |= HWCAP2_FSGSBASE;
1856742c45c3SAndi Kleen 	}
1857dd649bd0SAndy Lutomirski 
1858f7627e25SThomas Gleixner 	/*
18590f3fa48aSIngo Molnar 	 * The vendor-specific functions might have changed features.
18600f3fa48aSIngo Molnar 	 * Now we do "generic changes."
1861f7627e25SThomas Gleixner 	 */
1862f7627e25SThomas Gleixner 
1863b38b0665SH. Peter Anvin 	/* Filter out anything that depends on CPUID levels we don't have */
1864b38b0665SH. Peter Anvin 	filter_cpuid_features(c, true);
1865b38b0665SH. Peter Anvin 
1866f7627e25SThomas Gleixner 	/* If the model name is still unset, do table lookup. */
1867f7627e25SThomas Gleixner 	if (!c->x86_model_id[0]) {
186802dde8b4SJan Beulich 		const char *p;
1869f7627e25SThomas Gleixner 		p = table_lookup_model(c);
1870f7627e25SThomas Gleixner 		if (p)
1871f7627e25SThomas Gleixner 			strcpy(c->x86_model_id, p);
1872f7627e25SThomas Gleixner 		else
1873f7627e25SThomas Gleixner 			/* Last resort... */
1874f7627e25SThomas Gleixner 			sprintf(c->x86_model_id, "%02x/%02x",
1875f7627e25SThomas Gleixner 				c->x86, c->x86_model);
1876f7627e25SThomas Gleixner 	}
1877f7627e25SThomas Gleixner 
187849d859d7SH. Peter Anvin 	x86_init_rdrand(c);
187906976945SDave Hansen 	setup_pku(c);
1880991625f3SPeter Zijlstra 	setup_cet(c);
18813e0c3737SYinghai Lu 
18823e0c3737SYinghai Lu 	/*
18836a6256f9SAdam Buchbinder 	 * Clear/Set all flags overridden by options, need do it
18843e0c3737SYinghai Lu 	 * before following smp all cpus cap AND.
18853e0c3737SYinghai Lu 	 */
18868bf1ebcaSAndy Lutomirski 	apply_forced_caps(c);
18873e0c3737SYinghai Lu 
1888f7627e25SThomas Gleixner 	/*
1889f7627e25SThomas Gleixner 	 * On SMP, boot_cpu_data holds the common feature set between
1890f7627e25SThomas Gleixner 	 * all CPUs; so make sure that we indicate which features are
1891f7627e25SThomas Gleixner 	 * common between the CPUs.  The first time this routine gets
1892f7627e25SThomas Gleixner 	 * executed, c == &boot_cpu_data.
1893f7627e25SThomas Gleixner 	 */
1894f7627e25SThomas Gleixner 	if (c != &boot_cpu_data) {
1895f7627e25SThomas Gleixner 		/* AND the already accumulated flags with these */
1896f7627e25SThomas Gleixner 		for (i = 0; i < NCAPINTS; i++)
1897f7627e25SThomas Gleixner 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
189865fc985bSBorislav Petkov 
189965fc985bSBorislav Petkov 		/* OR, i.e. replicate the bug flags */
190065fc985bSBorislav Petkov 		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
190165fc985bSBorislav Petkov 			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
1902f7627e25SThomas Gleixner 	}
1903f7627e25SThomas Gleixner 
19040dcab41dSTony Luck 	ppin_init(c);
19050dcab41dSTony Luck 
1906f7627e25SThomas Gleixner 	/* Init Machine Check Exception if available. */
19075e09954aSBorislav Petkov 	mcheck_cpu_init(c);
190830d432dfSAndi Kleen 
1909de2d9445STejun Heo #ifdef CONFIG_NUMA
1910102bbe3aSYinghai Lu 	numa_add_cpu(smp_processor_id());
1911102bbe3aSYinghai Lu #endif
1912f7627e25SThomas Gleixner }
1913f7627e25SThomas Gleixner 
19148b6c0ab1SIngo Molnar /*
19158b6c0ab1SIngo Molnar  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
19168b6c0ab1SIngo Molnar  * on 32-bit kernels:
19178b6c0ab1SIngo Molnar  */
1918cfda7bb9SAndy Lutomirski #ifdef CONFIG_X86_32
enable_sep_cpu(void)1919cfda7bb9SAndy Lutomirski void enable_sep_cpu(void)
1920cfda7bb9SAndy Lutomirski {
19218b6c0ab1SIngo Molnar 	struct tss_struct *tss;
19228b6c0ab1SIngo Molnar 	int cpu;
1923cfda7bb9SAndy Lutomirski 
1924b3edfda4SBorislav Petkov 	if (!boot_cpu_has(X86_FEATURE_SEP))
1925b3edfda4SBorislav Petkov 		return;
1926b3edfda4SBorislav Petkov 
19278b6c0ab1SIngo Molnar 	cpu = get_cpu();
1928c482feefSAndy Lutomirski 	tss = &per_cpu(cpu_tss_rw, cpu);
19298b6c0ab1SIngo Molnar 
19308b6c0ab1SIngo Molnar 	/*
1931cf9328ccSAndy Lutomirski 	 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1932cf9328ccSAndy Lutomirski 	 * see the big comment in struct x86_hw_tss's definition.
19338b6c0ab1SIngo Molnar 	 */
1934cfda7bb9SAndy Lutomirski 
1935cfda7bb9SAndy Lutomirski 	tss->x86_tss.ss1 = __KERNEL_CS;
19368b6c0ab1SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
19374fe2d8b1SDave Hansen 	wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0);
19384c8cd0c5SIngo Molnar 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
19398b6c0ab1SIngo Molnar 
1940cfda7bb9SAndy Lutomirski 	put_cpu();
1941cfda7bb9SAndy Lutomirski }
1942e04d645fSGlauber Costa #endif
1943e04d645fSGlauber Costa 
identify_boot_cpu(void)19443ba3fdfeSThomas Gleixner static __init void identify_boot_cpu(void)
1945f7627e25SThomas Gleixner {
1946f7627e25SThomas Gleixner 	identify_cpu(&boot_cpu_data);
1947991625f3SPeter Zijlstra 	if (HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT))
1948991625f3SPeter Zijlstra 		pr_info("CET detected: Indirect Branch Tracking enabled\n");
1949102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1950f7627e25SThomas Gleixner 	enable_sep_cpu();
1951102bbe3aSYinghai Lu #endif
1952e0ba94f1SAlex Shi 	cpu_detect_tlb(&boot_cpu_data);
1953873d50d5SKees Cook 	setup_cr_pinning();
195495c5824fSPawan Gupta 
195595c5824fSPawan Gupta 	tsx_init();
1956765a0542SKai Huang 	tdx_init();
195792cbbadfSH. Peter Anvin (Intel) 	lkgs_init();
1958f7627e25SThomas Gleixner }
1959f7627e25SThomas Gleixner 
identify_secondary_cpu(struct cpuinfo_x86 * c)1960148f9bb8SPaul Gortmaker void identify_secondary_cpu(struct cpuinfo_x86 *c)
1961f7627e25SThomas Gleixner {
1962f7627e25SThomas Gleixner 	BUG_ON(c == &boot_cpu_data);
1963f7627e25SThomas Gleixner 	identify_cpu(c);
1964102bbe3aSYinghai Lu #ifdef CONFIG_X86_32
1965f7627e25SThomas Gleixner 	enable_sep_cpu();
1966102bbe3aSYinghai Lu #endif
196777243971SKonrad Rzeszutek Wilk 	x86_spec_ctrl_setup_ap();
19687e5b3c26SMark Gross 	update_srbds_msr();
19698974eb58SDaniel Sneddon 	if (boot_cpu_has_bug(X86_BUG_GDS))
19708974eb58SDaniel Sneddon 		update_gds_msr();
1971400331f8SPawan Gupta 
1972400331f8SPawan Gupta 	tsx_ap_init();
1973f7627e25SThomas Gleixner }
1974f7627e25SThomas Gleixner 
print_cpu_info(struct cpuinfo_x86 * c)1975148f9bb8SPaul Gortmaker void print_cpu_info(struct cpuinfo_x86 *c)
1976f7627e25SThomas Gleixner {
197702dde8b4SJan Beulich 	const char *vendor = NULL;
1978f7627e25SThomas Gleixner 
19790f3fa48aSIngo Molnar 	if (c->x86_vendor < X86_VENDOR_NUM) {
1980f7627e25SThomas Gleixner 		vendor = this_cpu->c_vendor;
19810f3fa48aSIngo Molnar 	} else {
19820f3fa48aSIngo Molnar 		if (c->cpuid_level >= 0)
1983f7627e25SThomas Gleixner 			vendor = c->x86_vendor_id;
19840f3fa48aSIngo Molnar 	}
1985f7627e25SThomas Gleixner 
1986bd32a8cfSYinghai Lu 	if (vendor && !strstr(c->x86_model_id, vendor))
19871b74dde7SChen Yucong 		pr_cont("%s ", vendor);
1988f7627e25SThomas Gleixner 
19899d31d35bSYinghai Lu 	if (c->x86_model_id[0])
19901b74dde7SChen Yucong 		pr_cont("%s", c->x86_model_id);
1991f7627e25SThomas Gleixner 	else
19921b74dde7SChen Yucong 		pr_cont("%d86", c->x86);
1993f7627e25SThomas Gleixner 
19941b74dde7SChen Yucong 	pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
1995924e101aSBorislav Petkov 
1996b399151cSJia Zhang 	if (c->x86_stepping || c->cpuid_level >= 0)
1997b399151cSJia Zhang 		pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
1998f7627e25SThomas Gleixner 	else
19991b74dde7SChen Yucong 		pr_cont(")\n");
2000f7627e25SThomas Gleixner }
2001f7627e25SThomas Gleixner 
20020c2a3913SAndi Kleen /*
2003ce38f038SThomas Gleixner  * clearcpuid= was already parsed in cpu_parse_early_param().  This dummy
2004ce38f038SThomas Gleixner  * function prevents it from becoming an environment variable for init.
20050c2a3913SAndi Kleen  */
setup_clearcpuid(char * arg)20060c2a3913SAndi Kleen static __init int setup_clearcpuid(char *arg)
2007ac72e788SAndi Kleen {
2008ac72e788SAndi Kleen 	return 1;
2009ac72e788SAndi Kleen }
20100c2a3913SAndi Kleen __setup("clearcpuid=", setup_clearcpuid);
2011ac72e788SAndi Kleen 
2012e57ef2edSThomas Gleixner DEFINE_PER_CPU_ALIGNED(struct pcpu_hot, pcpu_hot) = {
2013e57ef2edSThomas Gleixner 	.current_task	= &init_task,
201464701838SThomas Gleixner 	.preempt_count	= INIT_PREEMPT_COUNT,
2015c063a217SThomas Gleixner 	.top_of_stack	= TOP_OF_INIT_STACK,
2016e57ef2edSThomas Gleixner };
2017e57ef2edSThomas Gleixner EXPORT_PER_CPU_SYMBOL(pcpu_hot);
2018ed2f752eSUros Bizjak EXPORT_PER_CPU_SYMBOL(const_pcpu_hot);
2019e57ef2edSThomas Gleixner 
2020d5494d4fSYinghai Lu #ifdef CONFIG_X86_64
2021e6401c13SAndy Lutomirski DEFINE_PER_CPU_FIRST(struct fixed_percpu_data,
2022e6401c13SAndy Lutomirski 		     fixed_percpu_data) __aligned(PAGE_SIZE) __visible;
2023e6401c13SAndy Lutomirski EXPORT_PER_CPU_SYMBOL_GPL(fixed_percpu_data);
20240f3fa48aSIngo Molnar 
wrmsrl_cstar(unsigned long val)20259c7e2634SAndi Kleen static void wrmsrl_cstar(unsigned long val)
20269c7e2634SAndi Kleen {
20279c7e2634SAndi Kleen 	/*
20289c7e2634SAndi Kleen 	 * Intel CPUs do not support 32-bit SYSCALL. Writing to MSR_CSTAR
20299c7e2634SAndi Kleen 	 * is so far ignored by the CPU, but raises a #VE trap in a TDX
20309c7e2634SAndi Kleen 	 * guest. Avoid the pointless write on all Intel CPUs.
20319c7e2634SAndi Kleen 	 */
20329c7e2634SAndi Kleen 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
20339c7e2634SAndi Kleen 		wrmsrl(MSR_CSTAR, val);
20349c7e2634SAndi Kleen }
20359c7e2634SAndi Kleen 
idt_syscall_init(void)2036530dce27SXin Li static inline void idt_syscall_init(void)
2037d5494d4fSYinghai Lu {
20388d4b0678SThomas Gleixner 	wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
2039d56fe4bfSIngo Molnar 
204061382281SNikolay Borisov 	if (ia32_enabled()) {
20419c7e2634SAndi Kleen 		wrmsrl_cstar((unsigned long)entry_SYSCALL_compat);
2042a76c7f46SDenys Vlasenko 		/*
2043487d1edbSDenys Vlasenko 		 * This only works on Intel CPUs.
2044487d1edbSDenys Vlasenko 		 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
2045487d1edbSDenys Vlasenko 		 * This does not cause SYSENTER to jump to the wrong location, because
2046487d1edbSDenys Vlasenko 		 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
2047a76c7f46SDenys Vlasenko 		 */
2048a76c7f46SDenys Vlasenko 		wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
20498e6b65a1Szhong jiang 		wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
20508e6b65a1Szhong jiang 			    (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1));
20514c8cd0c5SIngo Molnar 		wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
205261382281SNikolay Borisov 	} else {
2053f71e1d2fSNikolay Borisov 		wrmsrl_cstar((unsigned long)entry_SYSCALL32_ignore);
20546b51311cSBorislav Petkov 		wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
2055d56fe4bfSIngo Molnar 		wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
2056d56fe4bfSIngo Molnar 		wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
205761382281SNikolay Borisov 	}
2058d5494d4fSYinghai Lu 
20596de4ac1dSH. Peter Anvin (Intel) 	/*
20606de4ac1dSH. Peter Anvin (Intel) 	 * Flags to clear on syscall; clear as much as possible
20616de4ac1dSH. Peter Anvin (Intel) 	 * to minimize user space-kernel interference.
20626de4ac1dSH. Peter Anvin (Intel) 	 */
2063d5494d4fSYinghai Lu 	wrmsrl(MSR_SYSCALL_MASK,
20646de4ac1dSH. Peter Anvin (Intel) 	       X86_EFLAGS_CF|X86_EFLAGS_PF|X86_EFLAGS_AF|
20656de4ac1dSH. Peter Anvin (Intel) 	       X86_EFLAGS_ZF|X86_EFLAGS_SF|X86_EFLAGS_TF|
20666de4ac1dSH. Peter Anvin (Intel) 	       X86_EFLAGS_IF|X86_EFLAGS_DF|X86_EFLAGS_OF|
20676de4ac1dSH. Peter Anvin (Intel) 	       X86_EFLAGS_IOPL|X86_EFLAGS_NT|X86_EFLAGS_RF|
20686de4ac1dSH. Peter Anvin (Intel) 	       X86_EFLAGS_AC|X86_EFLAGS_ID);
2069d5494d4fSYinghai Lu }
2070d5494d4fSYinghai Lu 
2071530dce27SXin Li /* May not be marked __init: used by software suspend */
syscall_init(void)2072530dce27SXin Li void syscall_init(void)
2073530dce27SXin Li {
2074530dce27SXin Li 	/* The default user and kernel segments */
2075530dce27SXin Li 	wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
2076530dce27SXin Li 
2077208d8c79SH. Peter Anvin (Intel) 	/*
2078208d8c79SH. Peter Anvin (Intel) 	 * Except the IA32_STAR MSR, there is NO need to setup SYSCALL and
2079208d8c79SH. Peter Anvin (Intel) 	 * SYSENTER MSRs for FRED, because FRED uses the ring 3 FRED
2080208d8c79SH. Peter Anvin (Intel) 	 * entrypoint for SYSCALL and SYSENTER, and ERETU is the only legit
2081208d8c79SH. Peter Anvin (Intel) 	 * instruction to return to ring 3 (both sysexit and sysret cause
2082208d8c79SH. Peter Anvin (Intel) 	 * #UD when FRED is enabled).
2083208d8c79SH. Peter Anvin (Intel) 	 */
2084208d8c79SH. Peter Anvin (Intel) 	if (!cpu_feature_enabled(X86_FEATURE_FRED))
2085530dce27SXin Li 		idt_syscall_init();
2086530dce27SXin Li }
2087530dce27SXin Li 
20880f3fa48aSIngo Molnar #else	/* CONFIG_X86_64 */
2089d5494d4fSYinghai Lu 
2090050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR
20913fb0fdb3SAndy Lutomirski DEFINE_PER_CPU(unsigned long, __stack_chk_guard);
20923fb0fdb3SAndy Lutomirski EXPORT_PER_CPU_SYMBOL(__stack_chk_guard);
209360a5317fSTejun Heo #endif
209460a5317fSTejun Heo 
20950f3fa48aSIngo Molnar #endif	/* CONFIG_X86_64 */
2096f7627e25SThomas Gleixner 
2097f7627e25SThomas Gleixner /*
20989766cdbcSJaswinder Singh Rajput  * Clear all 6 debug registers:
20999766cdbcSJaswinder Singh Rajput  */
clear_all_debug_regs(void)21009766cdbcSJaswinder Singh Rajput static void clear_all_debug_regs(void)
21019766cdbcSJaswinder Singh Rajput {
21029766cdbcSJaswinder Singh Rajput 	int i;
21039766cdbcSJaswinder Singh Rajput 
21049766cdbcSJaswinder Singh Rajput 	for (i = 0; i < 8; i++) {
21059766cdbcSJaswinder Singh Rajput 		/* Ignore db4, db5 */
21069766cdbcSJaswinder Singh Rajput 		if ((i == 4) || (i == 5))
21079766cdbcSJaswinder Singh Rajput 			continue;
21089766cdbcSJaswinder Singh Rajput 
21099766cdbcSJaswinder Singh Rajput 		set_debugreg(0, i);
21109766cdbcSJaswinder Singh Rajput 	}
21119766cdbcSJaswinder Singh Rajput }
2112f7627e25SThomas Gleixner 
21130bb9fef9SJason Wessel #ifdef CONFIG_KGDB
21140bb9fef9SJason Wessel /*
21150bb9fef9SJason Wessel  * Restore debug regs if using kgdbwait and you have a kernel debugger
21160bb9fef9SJason Wessel  * connection established.
21170bb9fef9SJason Wessel  */
dbg_restore_debug_regs(void)21180bb9fef9SJason Wessel static void dbg_restore_debug_regs(void)
21190bb9fef9SJason Wessel {
21200bb9fef9SJason Wessel 	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
21210bb9fef9SJason Wessel 		arch_kgdb_ops.correct_hw_break();
21220bb9fef9SJason Wessel }
21230bb9fef9SJason Wessel #else /* ! CONFIG_KGDB */
21240bb9fef9SJason Wessel #define dbg_restore_debug_regs()
21250bb9fef9SJason Wessel #endif /* ! CONFIG_KGDB */
21260bb9fef9SJason Wessel 
setup_getcpu(int cpu)2127505b7899SThomas Gleixner static inline void setup_getcpu(int cpu)
2128b2e2ba57SChang S. Bae {
212922245bdfSIngo Molnar 	unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
2130b2e2ba57SChang S. Bae 	struct desc_struct d = { };
2131b2e2ba57SChang S. Bae 
2132b6b4fbd9SSean Christopherson 	if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID))
2133fc48a6d1SSean Christopherson 		wrmsr(MSR_TSC_AUX, cpudata, 0);
2134b2e2ba57SChang S. Bae 
2135b2e2ba57SChang S. Bae 	/* Store CPU and node number in limit. */
2136b2e2ba57SChang S. Bae 	d.limit0 = cpudata;
2137b2e2ba57SChang S. Bae 	d.limit1 = cpudata >> 16;
2138b2e2ba57SChang S. Bae 
2139b2e2ba57SChang S. Bae 	d.type = 5;		/* RO data, expand down, accessed */
2140b2e2ba57SChang S. Bae 	d.dpl = 3;		/* Visible to user code */
2141b2e2ba57SChang S. Bae 	d.s = 1;		/* Not a system segment */
2142b2e2ba57SChang S. Bae 	d.p = 1;		/* Present */
2143b2e2ba57SChang S. Bae 	d.d = 1;		/* 32-bit */
2144b2e2ba57SChang S. Bae 
214522245bdfSIngo Molnar 	write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S);
2146b2e2ba57SChang S. Bae }
2147505b7899SThomas Gleixner 
2148717cce3bSSebastian Andrzej Siewior #ifdef CONFIG_X86_64
tss_setup_ist(struct tss_struct * tss)2149505b7899SThomas Gleixner static inline void tss_setup_ist(struct tss_struct *tss)
2150505b7899SThomas Gleixner {
2151505b7899SThomas Gleixner 	/* Set up the per-CPU TSS IST stacks */
2152505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
2153505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
2154505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
2155505b7899SThomas Gleixner 	tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
215602772fb9SJoerg Roedel 	/* Only mapped when SEV-ES is active */
215702772fb9SJoerg Roedel 	tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC);
2158505b7899SThomas Gleixner }
2159505b7899SThomas Gleixner #else /* CONFIG_X86_64 */
tss_setup_ist(struct tss_struct * tss)2160505b7899SThomas Gleixner static inline void tss_setup_ist(struct tss_struct *tss) { }
2161505b7899SThomas Gleixner #endif /* !CONFIG_X86_64 */
2162b2e2ba57SChang S. Bae 
tss_setup_io_bitmap(struct tss_struct * tss)2163111e7b15SThomas Gleixner static inline void tss_setup_io_bitmap(struct tss_struct *tss)
2164111e7b15SThomas Gleixner {
2165111e7b15SThomas Gleixner 	tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID;
2166111e7b15SThomas Gleixner 
2167111e7b15SThomas Gleixner #ifdef CONFIG_X86_IOPL_IOPERM
2168111e7b15SThomas Gleixner 	tss->io_bitmap.prev_max = 0;
2169111e7b15SThomas Gleixner 	tss->io_bitmap.prev_sequence = 0;
2170111e7b15SThomas Gleixner 	memset(tss->io_bitmap.bitmap, 0xff, sizeof(tss->io_bitmap.bitmap));
2171111e7b15SThomas Gleixner 	/*
2172111e7b15SThomas Gleixner 	 * Invalidate the extra array entry past the end of the all
2173111e7b15SThomas Gleixner 	 * permission bitmap as required by the hardware.
2174111e7b15SThomas Gleixner 	 */
2175111e7b15SThomas Gleixner 	tss->io_bitmap.mapall[IO_BITMAP_LONGS] = ~0UL;
2176111e7b15SThomas Gleixner #endif
2177111e7b15SThomas Gleixner }
2178ce4b1b16SIgor Mammedov 
2179f7627e25SThomas Gleixner /*
2180520d0308SJoerg Roedel  * Setup everything needed to handle exceptions from the IDT, including the IST
2181520d0308SJoerg Roedel  * exceptions which use paranoid_entry().
2182520d0308SJoerg Roedel  */
cpu_init_exception_handling(bool boot_cpu)2183a97756cbSXin Li (Intel) void cpu_init_exception_handling(bool boot_cpu)
2184520d0308SJoerg Roedel {
2185520d0308SJoerg Roedel 	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
2186520d0308SJoerg Roedel 	int cpu = raw_smp_processor_id();
2187520d0308SJoerg Roedel 
2188520d0308SJoerg Roedel 	/* paranoid_entry() gets the CPU number from the GDT */
2189520d0308SJoerg Roedel 	setup_getcpu(cpu);
2190520d0308SJoerg Roedel 
2191208d8c79SH. Peter Anvin (Intel) 	/* For IDT mode, IST vectors need to be set in TSS. */
2192208d8c79SH. Peter Anvin (Intel) 	if (!cpu_feature_enabled(X86_FEATURE_FRED))
2193520d0308SJoerg Roedel 		tss_setup_ist(tss);
2194520d0308SJoerg Roedel 	tss_setup_io_bitmap(tss);
2195520d0308SJoerg Roedel 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
2196520d0308SJoerg Roedel 
2197520d0308SJoerg Roedel 	load_TR_desc();
2198520d0308SJoerg Roedel 
219995d33bfaSBrijesh Singh 	/* GHCB needs to be setup to handle #VC. */
220095d33bfaSBrijesh Singh 	setup_ghcb();
220195d33bfaSBrijesh Singh 
220273270c1fSXin Li (Intel) 	if (cpu_feature_enabled(X86_FEATURE_FRED)) {
2203a97756cbSXin Li (Intel) 		/* The boot CPU has enabled FRED during early boot */
2204a97756cbSXin Li (Intel) 		if (!boot_cpu)
2205208d8c79SH. Peter Anvin (Intel) 			cpu_init_fred_exceptions();
2206a97756cbSXin Li (Intel) 
220773270c1fSXin Li (Intel) 		cpu_init_fred_rsps();
220873270c1fSXin Li (Intel) 	} else {
2209520d0308SJoerg Roedel 		load_current_idt();
2210520d0308SJoerg Roedel 	}
221173270c1fSXin Li (Intel) }
2212520d0308SJoerg Roedel 
cpu_init_replace_early_idt(void)2213a97756cbSXin Li (Intel) void __init cpu_init_replace_early_idt(void)
2214a97756cbSXin Li (Intel) {
221527fd185fSFenghua Yu 	if (cpu_feature_enabled(X86_FEATURE_FRED))
2216e534c7c5SLee Schermerhorn 		cpu_init_fred_exceptions();
2217e534c7c5SLee Schermerhorn 	else
2218a97756cbSXin Li (Intel) 		idt_setup_early_pf();
2219e7a22c1eSBrian Gerst }
2220b2e2ba57SChang S. Bae 
22211ba76586SYinghai Lu /*
22222eaad1fdSMike Travis  * cpu_init() initializes state that is per-CPU. Some data is already
22231ba76586SYinghai Lu  * initialized (naturally) in the bootstrap process, such as the GDT.  We
2224505b7899SThomas Gleixner  * reload it nevertheless, this function acts as a 'CPU state barrier',
2225505b7899SThomas Gleixner  * nothing should get across.
2226375074ccSAndy Lutomirski  */
cpu_init(void)22271ba76586SYinghai Lu void cpu_init(void)
22281ba76586SYinghai Lu {
22291ba76586SYinghai Lu 	struct task_struct *cur = current;
22301ba76586SYinghai Lu 	int cpu = raw_smp_processor_id();
22311ba76586SYinghai Lu 
2232552be871SBrian Gerst #ifdef CONFIG_NUMA
2233cf910e83SSeiji Aguchi 	if (this_cpu_read(numa_node) == 0 &&
22341ba76586SYinghai Lu 	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
2235505b7899SThomas Gleixner 		set_numa_node(early_cpu_to_node(cpu));
2236505b7899SThomas Gleixner #endif
2237505b7899SThomas Gleixner 	pr_debug("Initializing CPU#%d\n", cpu);
22381ba76586SYinghai Lu 
22391ba76586SYinghai Lu 	if (IS_ENABLED(CONFIG_X86_64) || cpu_feature_enabled(X86_FEATURE_VME) ||
22401ba76586SYinghai Lu 	    boot_cpu_has(X86_FEATURE_TSC) || boot_cpu_has(X86_FEATURE_DE))
22411ba76586SYinghai Lu 		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
22421ba76586SYinghai Lu 
22431ba76586SYinghai Lu 	if (IS_ENABLED(CONFIG_X86_64)) {
2244659006bfSThomas Gleixner 		loadsegment(fs, 0);
22451ba76586SYinghai Lu 		memset(cur->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
22461ba76586SYinghai Lu 		syscall_init();
22471ba76586SYinghai Lu 
22481ba76586SYinghai Lu 		wrmsrl(MSR_FS_BASE, 0);
22491ba76586SYinghai Lu 		wrmsrl(MSR_KERNEL_GS_BASE, 0);
22501ba76586SYinghai Lu 		barrier();
22511ba76586SYinghai Lu 
22521ba76586SYinghai Lu 		x2apic_setup();
225343650dcfSJacob Pan 
225443650dcfSJacob Pan 		intel_posted_msi_init();
22551ba76586SYinghai Lu 	}
22561ba76586SYinghai Lu 
2257f1f10076SVegard Nossum 	mmgrab(&init_mm);
2258505b7899SThomas Gleixner 	cur->active_mm = &init_mm;
2259505b7899SThomas Gleixner 	BUG_ON(cur->mm);
226072c0098dSAndy Lutomirski 	initialize_tlbstate_and_flush();
2261505b7899SThomas Gleixner 	enter_lazy_tlb(&init_mm, cur);
22621ba76586SYinghai Lu 
2263505b7899SThomas Gleixner 	/*
2264505b7899SThomas Gleixner 	 * sp0 points to the entry trampoline stack regardless of what task
2265505b7899SThomas Gleixner 	 * is running.
2266505b7899SThomas Gleixner 	 */
22674fe2d8b1SDave Hansen 	load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
226820bb8344SAndy Lutomirski 
226937868fe1SAndy Lutomirski 	load_mm_ldt(&init_mm);
22701ba76586SYinghai Lu 
22719766cdbcSJaswinder Singh Rajput 	clear_all_debug_regs();
22720bb9fef9SJason Wessel 	dbg_restore_debug_regs();
22731ba76586SYinghai Lu 
2274dc4e0021SAndy Lutomirski 	doublefault_init_cpu_tss();
2275505b7899SThomas Gleixner 
22761ba76586SYinghai Lu 	if (is_uv_system())
22771ba76586SYinghai Lu 		uv_cpu_init();
227869218e47SThomas Garnier 
227969218e47SThomas Garnier 	load_fixmap_gdt(cpu);
22801ba76586SYinghai Lu }
22811ba76586SYinghai Lu 
2282a77a94f8SBorislav Petkov #ifdef CONFIG_MICROCODE_LATE_LOADING
2283ab31c744SAshok Raj /**
2284c0dd9245SAshok Raj  * store_cpu_caps() - Store a snapshot of CPU capabilities
2285c0dd9245SAshok Raj  * @curr_info: Pointer where to store it
2286c0dd9245SAshok Raj  *
2287c0dd9245SAshok Raj  * Returns: None
2288c0dd9245SAshok Raj  */
store_cpu_caps(struct cpuinfo_x86 * curr_info)2289c0dd9245SAshok Raj void store_cpu_caps(struct cpuinfo_x86 *curr_info)
2290c0dd9245SAshok Raj {
2291c0dd9245SAshok Raj 	/* Reload CPUID max function as it might've changed. */
2292c0dd9245SAshok Raj 	curr_info->cpuid_level = cpuid_eax(0);
2293c0dd9245SAshok Raj 
2294c0dd9245SAshok Raj 	/* Copy all capability leafs and pick up the synthetic ones. */
2295c0dd9245SAshok Raj 	memcpy(&curr_info->x86_capability, &boot_cpu_data.x86_capability,
2296c0dd9245SAshok Raj 	       sizeof(curr_info->x86_capability));
2297c0dd9245SAshok Raj 
2298c0dd9245SAshok Raj 	/* Get the hardware CPUID leafs */
2299c0dd9245SAshok Raj 	get_cpu_cap(curr_info);
2300c0dd9245SAshok Raj }
2301c0dd9245SAshok Raj 
2302c0dd9245SAshok Raj /**
2303ab31c744SAshok Raj  * microcode_check() - Check if any CPU capabilities changed after an update.
2304ab31c744SAshok Raj  * @prev_info:	CPU capabilities stored before an update.
2305ab31c744SAshok Raj  *
23061008c52cSBorislav Petkov  * The microcode loader calls this upon late microcode load to recheck features,
230780347cd5SSebastian Andrzej Siewior  * only when microcode has been updated. Caller holds and CPU hotplug lock.
2308ab31c744SAshok Raj  *
2309ab31c744SAshok Raj  * Return: None
23101008c52cSBorislav Petkov  */
microcode_check(struct cpuinfo_x86 * prev_info)2311ab31c744SAshok Raj void microcode_check(struct cpuinfo_x86 *prev_info)
23121008c52cSBorislav Petkov {
2313c0dd9245SAshok Raj 	struct cpuinfo_x86 curr_info;
231442ca8082SBorislav Petkov 
23151008c52cSBorislav Petkov 	perf_check_microcode();
231642ca8082SBorislav Petkov 
2317522b1d69SBorislav Petkov (AMD) 	amd_check_microcode();
2318522b1d69SBorislav Petkov (AMD) 
2319c0dd9245SAshok Raj 	store_cpu_caps(&curr_info);
232042ca8082SBorislav Petkov 
2321c0dd9245SAshok Raj 	if (!memcmp(&prev_info->x86_capability, &curr_info.x86_capability,
2322ab31c744SAshok Raj 		    sizeof(prev_info->x86_capability)))
232342ca8082SBorislav Petkov 		return;
232442ca8082SBorislav Petkov 
232542ca8082SBorislav Petkov 	pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
232642ca8082SBorislav Petkov 	pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
23271008c52cSBorislav Petkov }
2328a77a94f8SBorislav Petkov #endif
23299c92374bSThomas Gleixner 
23309c92374bSThomas Gleixner /*
23319c92374bSThomas Gleixner  * Invoked from core CPU hotplug code after hotplug operations
23329c92374bSThomas Gleixner  */
arch_smt_update(void)23339c92374bSThomas Gleixner void arch_smt_update(void)
23349c92374bSThomas Gleixner {
23359c92374bSThomas Gleixner 	/* Handle the speculative execution misfeatures */
23369c92374bSThomas Gleixner 	cpu_bugs_smt_update();
23376a1cb5f5SThomas Gleixner 	/* Check whether IPI broadcasting can be enabled */
23386a1cb5f5SThomas Gleixner 	apic_smt_update();
23399c92374bSThomas Gleixner }
23407c7077a7SThomas Gleixner 
arch_cpu_finalize_init(void)23417c7077a7SThomas Gleixner void __init arch_cpu_finalize_init(void)
23427c7077a7SThomas Gleixner {
2343c90399fbSThomas Gleixner 	struct cpuinfo_x86 *c = this_cpu_ptr(&cpu_info);
2344c90399fbSThomas Gleixner 
23457c7077a7SThomas Gleixner 	identify_boot_cpu();
23467c7077a7SThomas Gleixner 
234735ce6492SThomas Gleixner 	select_idle_routine();
234835ce6492SThomas Gleixner 
23497c7077a7SThomas Gleixner 	/*
23507c7077a7SThomas Gleixner 	 * identify_boot_cpu() initialized SMT support information, let the
23517c7077a7SThomas Gleixner 	 * core code know.
23527c7077a7SThomas Gleixner 	 */
23538078f4d6SThomas Gleixner 	cpu_smt_set_num_threads(__max_threads_per_core, __max_threads_per_core);
23547c7077a7SThomas Gleixner 
23557c7077a7SThomas Gleixner 	if (!IS_ENABLED(CONFIG_SMP)) {
23567c7077a7SThomas Gleixner 		pr_info("CPU: ");
23577c7077a7SThomas Gleixner 		print_cpu_info(&boot_cpu_data);
23587c7077a7SThomas Gleixner 	}
23597c7077a7SThomas Gleixner 
23607c7077a7SThomas Gleixner 	cpu_select_mitigations();
23617c7077a7SThomas Gleixner 
23627c7077a7SThomas Gleixner 	arch_smt_update();
23637c7077a7SThomas Gleixner 
23647c7077a7SThomas Gleixner 	if (IS_ENABLED(CONFIG_X86_32)) {
23657c7077a7SThomas Gleixner 		/*
23667c7077a7SThomas Gleixner 		 * Check whether this is a real i386 which is not longer
23677c7077a7SThomas Gleixner 		 * supported and fixup the utsname.
23687c7077a7SThomas Gleixner 		 */
23697c7077a7SThomas Gleixner 		if (boot_cpu_data.x86 < 4)
23707c7077a7SThomas Gleixner 			panic("Kernel requires i486+ for 'invlpg' and other features");
23717c7077a7SThomas Gleixner 
23727c7077a7SThomas Gleixner 		init_utsname()->machine[1] =
23737c7077a7SThomas Gleixner 			'0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
23747c7077a7SThomas Gleixner 	}
23757c7077a7SThomas Gleixner 
2376b81fac90SThomas Gleixner 	/*
2377b81fac90SThomas Gleixner 	 * Must be before alternatives because it might set or clear
2378b81fac90SThomas Gleixner 	 * feature bits.
2379b81fac90SThomas Gleixner 	 */
2380b81fac90SThomas Gleixner 	fpu__init_system();
2381b81fac90SThomas Gleixner 	fpu__init_cpu();
2382b81fac90SThomas Gleixner 
2383c90399fbSThomas Gleixner 	/*
2384c90399fbSThomas Gleixner 	 * Ensure that access to the per CPU representation has the initial
2385c90399fbSThomas Gleixner 	 * boot CPU configuration.
2386c90399fbSThomas Gleixner 	 */
2387c90399fbSThomas Gleixner 	*c = boot_cpu_data;
2388c90399fbSThomas Gleixner 	c->initialized = true;
2389c90399fbSThomas Gleixner 
23907c7077a7SThomas Gleixner 	alternative_instructions();
23917c7077a7SThomas Gleixner 
23927c7077a7SThomas Gleixner 	if (IS_ENABLED(CONFIG_X86_64)) {
2393*86e6b154SLinus Torvalds 		unsigned long USER_PTR_MAX = TASK_SIZE_MAX-1;
2394*86e6b154SLinus Torvalds 
2395*86e6b154SLinus Torvalds 		/*
2396*86e6b154SLinus Torvalds 		 * Enable this when LAM is gated on LASS support
2397*86e6b154SLinus Torvalds 		if (cpu_feature_enabled(X86_FEATURE_LAM))
2398*86e6b154SLinus Torvalds 			USER_PTR_MAX = (1ul << 63) - PAGE_SIZE - 1;
2399*86e6b154SLinus Torvalds 		 */
2400*86e6b154SLinus Torvalds 		runtime_const_init(ptr, USER_PTR_MAX);
2401*86e6b154SLinus Torvalds 
24027c7077a7SThomas Gleixner 		/*
24037c7077a7SThomas Gleixner 		 * Make sure the first 2MB area is not mapped by huge pages
24047c7077a7SThomas Gleixner 		 * There are typically fixed size MTRRs in there and overlapping
24057c7077a7SThomas Gleixner 		 * MTRRs into large pages causes slow downs.
24067c7077a7SThomas Gleixner 		 *
24077c7077a7SThomas Gleixner 		 * Right now we don't do that with gbpages because there seems
24087c7077a7SThomas Gleixner 		 * very little benefit for that case.
24097c7077a7SThomas Gleixner 		 */
24107c7077a7SThomas Gleixner 		if (!direct_gbpages)
24117c7077a7SThomas Gleixner 			set_memory_4k((unsigned long)__va(0), 1);
24127c7077a7SThomas Gleixner 	} else {
24137c7077a7SThomas Gleixner 		fpu__init_check_bugs();
24147c7077a7SThomas Gleixner 	}
2415439e1757SThomas Gleixner 
2416439e1757SThomas Gleixner 	/*
2417439e1757SThomas Gleixner 	 * This needs to be called before any devices perform DMA
2418439e1757SThomas Gleixner 	 * operations that might use the SWIOTLB bounce buffers. It will
2419439e1757SThomas Gleixner 	 * mark the bounce buffers as decrypted so that their usage will
2420439e1757SThomas Gleixner 	 * not cause "plain-text" data to be decrypted when accessed. It
2421439e1757SThomas Gleixner 	 * must be called after late_time_init() so that Hyper-V x86/x64
2422439e1757SThomas Gleixner 	 * hypercalls work when the SWIOTLB bounce buffers are decrypted.
2423439e1757SThomas Gleixner 	 */
2424439e1757SThomas Gleixner 	mem_encrypt_init();
24257c7077a7SThomas Gleixner }
2426