18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/* cpu_feature_enabled() cannot be used this early */
38c2ecf20Sopenharmony_ci#define USE_EARLY_PGTABLE_L5
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/memblock.h>
68c2ecf20Sopenharmony_ci#include <linux/linkage.h>
78c2ecf20Sopenharmony_ci#include <linux/bitops.h>
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/export.h>
108c2ecf20Sopenharmony_ci#include <linux/percpu.h>
118c2ecf20Sopenharmony_ci#include <linux/string.h>
128c2ecf20Sopenharmony_ci#include <linux/ctype.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/sched/mm.h>
158c2ecf20Sopenharmony_ci#include <linux/sched/clock.h>
168c2ecf20Sopenharmony_ci#include <linux/sched/task.h>
178c2ecf20Sopenharmony_ci#include <linux/sched/smt.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/kprobes.h>
208c2ecf20Sopenharmony_ci#include <linux/kgdb.h>
218c2ecf20Sopenharmony_ci#include <linux/mem_encrypt.h>
228c2ecf20Sopenharmony_ci#include <linux/smp.h>
238c2ecf20Sopenharmony_ci#include <linux/cpu.h>
248c2ecf20Sopenharmony_ci#include <linux/io.h>
258c2ecf20Sopenharmony_ci#include <linux/syscore_ops.h>
268c2ecf20Sopenharmony_ci#include <linux/pgtable.h>
278c2ecf20Sopenharmony_ci#include <linux/utsname.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include <asm/alternative.h>
308c2ecf20Sopenharmony_ci#include <asm/cmdline.h>
318c2ecf20Sopenharmony_ci#include <asm/stackprotector.h>
328c2ecf20Sopenharmony_ci#include <asm/perf_event.h>
338c2ecf20Sopenharmony_ci#include <asm/mmu_context.h>
348c2ecf20Sopenharmony_ci#include <asm/doublefault.h>
358c2ecf20Sopenharmony_ci#include <asm/archrandom.h>
368c2ecf20Sopenharmony_ci#include <asm/hypervisor.h>
378c2ecf20Sopenharmony_ci#include <asm/processor.h>
388c2ecf20Sopenharmony_ci#include <asm/tlbflush.h>
398c2ecf20Sopenharmony_ci#include <asm/debugreg.h>
408c2ecf20Sopenharmony_ci#include <asm/sections.h>
418c2ecf20Sopenharmony_ci#include <asm/vsyscall.h>
428c2ecf20Sopenharmony_ci#include <linux/topology.h>
438c2ecf20Sopenharmony_ci#include <linux/cpumask.h>
448c2ecf20Sopenharmony_ci#include <linux/atomic.h>
458c2ecf20Sopenharmony_ci#include <asm/proto.h>
468c2ecf20Sopenharmony_ci#include <asm/setup.h>
478c2ecf20Sopenharmony_ci#include <asm/apic.h>
488c2ecf20Sopenharmony_ci#include <asm/desc.h>
498c2ecf20Sopenharmony_ci#include <asm/fpu/internal.h>
508c2ecf20Sopenharmony_ci#include <asm/mtrr.h>
518c2ecf20Sopenharmony_ci#include <asm/hwcap2.h>
528c2ecf20Sopenharmony_ci#include <linux/numa.h>
538c2ecf20Sopenharmony_ci#include <asm/numa.h>
548c2ecf20Sopenharmony_ci#include <asm/asm.h>
558c2ecf20Sopenharmony_ci#include <asm/bugs.h>
568c2ecf20Sopenharmony_ci#include <asm/cpu.h>
578c2ecf20Sopenharmony_ci#include <asm/mce.h>
588c2ecf20Sopenharmony_ci#include <asm/msr.h>
598c2ecf20Sopenharmony_ci#include <asm/memtype.h>
608c2ecf20Sopenharmony_ci#include <asm/microcode.h>
618c2ecf20Sopenharmony_ci#include <asm/microcode_intel.h>
628c2ecf20Sopenharmony_ci#include <asm/intel-family.h>
638c2ecf20Sopenharmony_ci#include <asm/cpu_device_id.h>
648c2ecf20Sopenharmony_ci#include <asm/uv/uv.h>
658c2ecf20Sopenharmony_ci#include <asm/set_memory.h>
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#include "cpu.h"
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciu32 elf_hwcap2 __read_mostly;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* all of these masks are initialized in setup_cpu_local_masks() */
728c2ecf20Sopenharmony_cicpumask_var_t cpu_initialized_mask;
738c2ecf20Sopenharmony_cicpumask_var_t cpu_callout_mask;
748c2ecf20Sopenharmony_cicpumask_var_t cpu_callin_mask;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/* representing cpus for which sibling maps can be computed */
778c2ecf20Sopenharmony_cicpumask_var_t cpu_sibling_setup_mask;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* Number of siblings per CPU package */
808c2ecf20Sopenharmony_ciint smp_num_siblings = 1;
818c2ecf20Sopenharmony_ciEXPORT_SYMBOL(smp_num_siblings);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/* Last level cache ID of each logical CPU */
848c2ecf20Sopenharmony_ciDEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* correctly size the local cpu masks */
878c2ecf20Sopenharmony_civoid __init setup_cpu_local_masks(void)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	alloc_bootmem_cpumask_var(&cpu_initialized_mask);
908c2ecf20Sopenharmony_ci	alloc_bootmem_cpumask_var(&cpu_callin_mask);
918c2ecf20Sopenharmony_ci	alloc_bootmem_cpumask_var(&cpu_callout_mask);
928c2ecf20Sopenharmony_ci	alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic void default_init(struct cpuinfo_x86 *c)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
988c2ecf20Sopenharmony_ci	cpu_detect_cache_sizes(c);
998c2ecf20Sopenharmony_ci#else
1008c2ecf20Sopenharmony_ci	/* Not much we can do here... */
1018c2ecf20Sopenharmony_ci	/* Check if at least it has cpuid */
1028c2ecf20Sopenharmony_ci	if (c->cpuid_level == -1) {
1038c2ecf20Sopenharmony_ci		/* No cpuid. It must be an ancient CPU */
1048c2ecf20Sopenharmony_ci		if (c->x86 == 4)
1058c2ecf20Sopenharmony_ci			strcpy(c->x86_model_id, "486");
1068c2ecf20Sopenharmony_ci		else if (c->x86 == 3)
1078c2ecf20Sopenharmony_ci			strcpy(c->x86_model_id, "386");
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci#endif
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic const struct cpu_dev default_cpu = {
1138c2ecf20Sopenharmony_ci	.c_init		= default_init,
1148c2ecf20Sopenharmony_ci	.c_vendor	= "Unknown",
1158c2ecf20Sopenharmony_ci	.c_x86_vendor	= X86_VENDOR_UNKNOWN,
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic const struct cpu_dev *this_cpu = &default_cpu;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ciDEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
1218c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
1228c2ecf20Sopenharmony_ci	/*
1238c2ecf20Sopenharmony_ci	 * We need valid kernel segments for data and code in long mode too
1248c2ecf20Sopenharmony_ci	 * IRET will check the segment types  kkeil 2000/10/28
1258c2ecf20Sopenharmony_ci	 * Also sysret mandates a special GDT layout
1268c2ecf20Sopenharmony_ci	 *
1278c2ecf20Sopenharmony_ci	 * TLS descriptors are currently at a different place compared to i386.
1288c2ecf20Sopenharmony_ci	 * Hopefully nobody expects them at a fixed place (Wine?)
1298c2ecf20Sopenharmony_ci	 */
1308c2ecf20Sopenharmony_ci	[GDT_ENTRY_KERNEL32_CS]		= GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
1318c2ecf20Sopenharmony_ci	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
1328c2ecf20Sopenharmony_ci	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
1338c2ecf20Sopenharmony_ci	[GDT_ENTRY_DEFAULT_USER32_CS]	= GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
1348c2ecf20Sopenharmony_ci	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
1358c2ecf20Sopenharmony_ci	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
1368c2ecf20Sopenharmony_ci#else
1378c2ecf20Sopenharmony_ci	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
1388c2ecf20Sopenharmony_ci	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1398c2ecf20Sopenharmony_ci	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
1408c2ecf20Sopenharmony_ci	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
1418c2ecf20Sopenharmony_ci	/*
1428c2ecf20Sopenharmony_ci	 * Segments used for calling PnP BIOS have byte granularity.
1438c2ecf20Sopenharmony_ci	 * They code segments and data segments have fixed 64k limits,
1448c2ecf20Sopenharmony_ci	 * the transfer segment sizes are set at run time.
1458c2ecf20Sopenharmony_ci	 */
1468c2ecf20Sopenharmony_ci	/* 32-bit code */
1478c2ecf20Sopenharmony_ci	[GDT_ENTRY_PNPBIOS_CS32]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
1488c2ecf20Sopenharmony_ci	/* 16-bit code */
1498c2ecf20Sopenharmony_ci	[GDT_ENTRY_PNPBIOS_CS16]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1508c2ecf20Sopenharmony_ci	/* 16-bit data */
1518c2ecf20Sopenharmony_ci	[GDT_ENTRY_PNPBIOS_DS]		= GDT_ENTRY_INIT(0x0092, 0, 0xffff),
1528c2ecf20Sopenharmony_ci	/* 16-bit data */
1538c2ecf20Sopenharmony_ci	[GDT_ENTRY_PNPBIOS_TS1]		= GDT_ENTRY_INIT(0x0092, 0, 0),
1548c2ecf20Sopenharmony_ci	/* 16-bit data */
1558c2ecf20Sopenharmony_ci	[GDT_ENTRY_PNPBIOS_TS2]		= GDT_ENTRY_INIT(0x0092, 0, 0),
1568c2ecf20Sopenharmony_ci	/*
1578c2ecf20Sopenharmony_ci	 * The APM segments have byte granularity and their bases
1588c2ecf20Sopenharmony_ci	 * are set at run time.  All have 64k limits.
1598c2ecf20Sopenharmony_ci	 */
1608c2ecf20Sopenharmony_ci	/* 32-bit code */
1618c2ecf20Sopenharmony_ci	[GDT_ENTRY_APMBIOS_BASE]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
1628c2ecf20Sopenharmony_ci	/* 16-bit code */
1638c2ecf20Sopenharmony_ci	[GDT_ENTRY_APMBIOS_BASE+1]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
1648c2ecf20Sopenharmony_ci	/* data */
1658c2ecf20Sopenharmony_ci	[GDT_ENTRY_APMBIOS_BASE+2]	= GDT_ENTRY_INIT(0x4092, 0, 0xffff),
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	[GDT_ENTRY_ESPFIX_SS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1688c2ecf20Sopenharmony_ci	[GDT_ENTRY_PERCPU]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
1698c2ecf20Sopenharmony_ci	GDT_STACK_CANARY_INIT
1708c2ecf20Sopenharmony_ci#endif
1718c2ecf20Sopenharmony_ci} };
1728c2ecf20Sopenharmony_ciEXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
1758c2ecf20Sopenharmony_cistatic int __init x86_nopcid_setup(char *s)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	/* nopcid doesn't accept parameters */
1788c2ecf20Sopenharmony_ci	if (s)
1798c2ecf20Sopenharmony_ci		return -EINVAL;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	/* do not emit a message if the feature is not present */
1828c2ecf20Sopenharmony_ci	if (!boot_cpu_has(X86_FEATURE_PCID))
1838c2ecf20Sopenharmony_ci		return 0;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	setup_clear_cpu_cap(X86_FEATURE_PCID);
1868c2ecf20Sopenharmony_ci	pr_info("nopcid: PCID feature disabled\n");
1878c2ecf20Sopenharmony_ci	return 0;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ciearly_param("nopcid", x86_nopcid_setup);
1908c2ecf20Sopenharmony_ci#endif
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic int __init x86_noinvpcid_setup(char *s)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	/* noinvpcid doesn't accept parameters */
1958c2ecf20Sopenharmony_ci	if (s)
1968c2ecf20Sopenharmony_ci		return -EINVAL;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	/* do not emit a message if the feature is not present */
1998c2ecf20Sopenharmony_ci	if (!boot_cpu_has(X86_FEATURE_INVPCID))
2008c2ecf20Sopenharmony_ci		return 0;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	setup_clear_cpu_cap(X86_FEATURE_INVPCID);
2038c2ecf20Sopenharmony_ci	pr_info("noinvpcid: INVPCID feature disabled\n");
2048c2ecf20Sopenharmony_ci	return 0;
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ciearly_param("noinvpcid", x86_noinvpcid_setup);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
2098c2ecf20Sopenharmony_cistatic int cachesize_override = -1;
2108c2ecf20Sopenharmony_cistatic int disable_x86_serial_nr = 1;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic int __init cachesize_setup(char *str)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	get_option(&str, &cachesize_override);
2158c2ecf20Sopenharmony_ci	return 1;
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci__setup("cachesize=", cachesize_setup);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic int __init x86_sep_setup(char *s)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	setup_clear_cpu_cap(X86_FEATURE_SEP);
2228c2ecf20Sopenharmony_ci	return 1;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci__setup("nosep", x86_sep_setup);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/* Standard macro to see if a specific flag is changeable */
2278c2ecf20Sopenharmony_cistatic inline int flag_is_changeable_p(u32 flag)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	u32 f1, f2;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/*
2328c2ecf20Sopenharmony_ci	 * Cyrix and IDT cpus allow disabling of CPUID
2338c2ecf20Sopenharmony_ci	 * so the code below may return different results
2348c2ecf20Sopenharmony_ci	 * when it is executed before and after enabling
2358c2ecf20Sopenharmony_ci	 * the CPUID. Add "volatile" to not allow gcc to
2368c2ecf20Sopenharmony_ci	 * optimize the subsequent calls to this function.
2378c2ecf20Sopenharmony_ci	 */
2388c2ecf20Sopenharmony_ci	asm volatile ("pushfl		\n\t"
2398c2ecf20Sopenharmony_ci		      "pushfl		\n\t"
2408c2ecf20Sopenharmony_ci		      "popl %0		\n\t"
2418c2ecf20Sopenharmony_ci		      "movl %0, %1	\n\t"
2428c2ecf20Sopenharmony_ci		      "xorl %2, %0	\n\t"
2438c2ecf20Sopenharmony_ci		      "pushl %0		\n\t"
2448c2ecf20Sopenharmony_ci		      "popfl		\n\t"
2458c2ecf20Sopenharmony_ci		      "pushfl		\n\t"
2468c2ecf20Sopenharmony_ci		      "popl %0		\n\t"
2478c2ecf20Sopenharmony_ci		      "popfl		\n\t"
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci		      : "=&r" (f1), "=&r" (f2)
2508c2ecf20Sopenharmony_ci		      : "ir" (flag));
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	return ((f1^f2) & flag) != 0;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci/* Probe for the CPUID instruction */
2568c2ecf20Sopenharmony_ciint have_cpuid_p(void)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	return flag_is_changeable_p(X86_EFLAGS_ID);
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	unsigned long lo, hi;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
2668c2ecf20Sopenharmony_ci		return;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/* Disable processor serial number: */
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2718c2ecf20Sopenharmony_ci	lo |= 0x200000;
2728c2ecf20Sopenharmony_ci	wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	pr_notice("CPU serial number disabled.\n");
2758c2ecf20Sopenharmony_ci	clear_cpu_cap(c, X86_FEATURE_PN);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/* Disabling the serial number may affect the cpuid level */
2788c2ecf20Sopenharmony_ci	c->cpuid_level = cpuid_eax(0);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic int __init x86_serial_nr_setup(char *s)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	disable_x86_serial_nr = 0;
2848c2ecf20Sopenharmony_ci	return 1;
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci__setup("serialnumber", x86_serial_nr_setup);
2878c2ecf20Sopenharmony_ci#else
2888c2ecf20Sopenharmony_cistatic inline int flag_is_changeable_p(u32 flag)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	return 1;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_cistatic inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci#endif
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cistatic __init int setup_disable_smep(char *arg)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	setup_clear_cpu_cap(X86_FEATURE_SMEP);
3008c2ecf20Sopenharmony_ci	return 1;
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci__setup("nosmep", setup_disable_smep);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic __always_inline void setup_smep(struct cpuinfo_x86 *c)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_SMEP))
3078c2ecf20Sopenharmony_ci		cr4_set_bits(X86_CR4_SMEP);
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic __init int setup_disable_smap(char *arg)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	setup_clear_cpu_cap(X86_FEATURE_SMAP);
3138c2ecf20Sopenharmony_ci	return 1;
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci__setup("nosmap", setup_disable_smap);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic __always_inline void setup_smap(struct cpuinfo_x86 *c)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	unsigned long eflags = native_save_fl();
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	/* This should have been cleared long ago */
3228c2ecf20Sopenharmony_ci	BUG_ON(eflags & X86_EFLAGS_AC);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_SMAP)) {
3258c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_SMAP
3268c2ecf20Sopenharmony_ci		cr4_set_bits(X86_CR4_SMAP);
3278c2ecf20Sopenharmony_ci#else
3288c2ecf20Sopenharmony_ci		clear_cpu_cap(c, X86_FEATURE_SMAP);
3298c2ecf20Sopenharmony_ci		cr4_clear_bits(X86_CR4_SMAP);
3308c2ecf20Sopenharmony_ci#endif
3318c2ecf20Sopenharmony_ci	}
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic __always_inline void setup_umip(struct cpuinfo_x86 *c)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	/* Check the boot processor, plus build option for UMIP. */
3378c2ecf20Sopenharmony_ci	if (!cpu_feature_enabled(X86_FEATURE_UMIP))
3388c2ecf20Sopenharmony_ci		goto out;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	/* Check the current processor's cpuid bits. */
3418c2ecf20Sopenharmony_ci	if (!cpu_has(c, X86_FEATURE_UMIP))
3428c2ecf20Sopenharmony_ci		goto out;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	cr4_set_bits(X86_CR4_UMIP);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	pr_info_once("x86/cpu: User Mode Instruction Prevention (UMIP) activated\n");
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	return;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ciout:
3518c2ecf20Sopenharmony_ci	/*
3528c2ecf20Sopenharmony_ci	 * Make sure UMIP is disabled in case it was enabled in a
3538c2ecf20Sopenharmony_ci	 * previous boot (e.g., via kexec).
3548c2ecf20Sopenharmony_ci	 */
3558c2ecf20Sopenharmony_ci	cr4_clear_bits(X86_CR4_UMIP);
3568c2ecf20Sopenharmony_ci}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci/* These bits should not change their value after CPU init is finished. */
3598c2ecf20Sopenharmony_cistatic const unsigned long cr4_pinned_mask =
3608c2ecf20Sopenharmony_ci	X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP | X86_CR4_FSGSBASE;
3618c2ecf20Sopenharmony_cistatic DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
3628c2ecf20Sopenharmony_cistatic unsigned long cr4_pinned_bits __ro_after_init;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_civoid native_write_cr0(unsigned long val)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	unsigned long bits_missing = 0;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ciset_register:
3698c2ecf20Sopenharmony_ci	asm volatile("mov %0,%%cr0": "+r" (val) : : "memory");
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	if (static_branch_likely(&cr_pinning)) {
3728c2ecf20Sopenharmony_ci		if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) {
3738c2ecf20Sopenharmony_ci			bits_missing = X86_CR0_WP;
3748c2ecf20Sopenharmony_ci			val |= bits_missing;
3758c2ecf20Sopenharmony_ci			goto set_register;
3768c2ecf20Sopenharmony_ci		}
3778c2ecf20Sopenharmony_ci		/* Warn after we've set the missing bits. */
3788c2ecf20Sopenharmony_ci		WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n");
3798c2ecf20Sopenharmony_ci	}
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ciEXPORT_SYMBOL(native_write_cr0);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_civoid native_write_cr4(unsigned long val)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	unsigned long bits_changed = 0;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ciset_register:
3888c2ecf20Sopenharmony_ci	asm volatile("mov %0,%%cr4": "+r" (val) : : "memory");
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	if (static_branch_likely(&cr_pinning)) {
3918c2ecf20Sopenharmony_ci		if (unlikely((val & cr4_pinned_mask) != cr4_pinned_bits)) {
3928c2ecf20Sopenharmony_ci			bits_changed = (val & cr4_pinned_mask) ^ cr4_pinned_bits;
3938c2ecf20Sopenharmony_ci			val = (val & ~cr4_pinned_mask) | cr4_pinned_bits;
3948c2ecf20Sopenharmony_ci			goto set_register;
3958c2ecf20Sopenharmony_ci		}
3968c2ecf20Sopenharmony_ci		/* Warn after we've corrected the changed bits. */
3978c2ecf20Sopenharmony_ci		WARN_ONCE(bits_changed, "pinned CR4 bits changed: 0x%lx!?\n",
3988c2ecf20Sopenharmony_ci			  bits_changed);
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci}
4018c2ecf20Sopenharmony_ci#if IS_MODULE(CONFIG_LKDTM)
4028c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(native_write_cr4);
4038c2ecf20Sopenharmony_ci#endif
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_civoid cr4_update_irqsoff(unsigned long set, unsigned long clear)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	lockdep_assert_irqs_disabled();
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	newval = (cr4 & ~clear) | set;
4128c2ecf20Sopenharmony_ci	if (newval != cr4) {
4138c2ecf20Sopenharmony_ci		this_cpu_write(cpu_tlbstate.cr4, newval);
4148c2ecf20Sopenharmony_ci		__write_cr4(newval);
4158c2ecf20Sopenharmony_ci	}
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cr4_update_irqsoff);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci/* Read the CR4 shadow. */
4208c2ecf20Sopenharmony_ciunsigned long cr4_read_shadow(void)
4218c2ecf20Sopenharmony_ci{
4228c2ecf20Sopenharmony_ci	return this_cpu_read(cpu_tlbstate.cr4);
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cr4_read_shadow);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_civoid cr4_init(void)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	unsigned long cr4 = __read_cr4();
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	if (boot_cpu_has(X86_FEATURE_PCID))
4318c2ecf20Sopenharmony_ci		cr4 |= X86_CR4_PCIDE;
4328c2ecf20Sopenharmony_ci	if (static_branch_likely(&cr_pinning))
4338c2ecf20Sopenharmony_ci		cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	__write_cr4(cr4);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	/* Initialize cr4 shadow for this CPU. */
4388c2ecf20Sopenharmony_ci	this_cpu_write(cpu_tlbstate.cr4, cr4);
4398c2ecf20Sopenharmony_ci}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci/*
4428c2ecf20Sopenharmony_ci * Once CPU feature detection is finished (and boot params have been
4438c2ecf20Sopenharmony_ci * parsed), record any of the sensitive CR bits that are set, and
4448c2ecf20Sopenharmony_ci * enable CR pinning.
4458c2ecf20Sopenharmony_ci */
4468c2ecf20Sopenharmony_cistatic void __init setup_cr_pinning(void)
4478c2ecf20Sopenharmony_ci{
4488c2ecf20Sopenharmony_ci	cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask;
4498c2ecf20Sopenharmony_ci	static_key_enable(&cr_pinning.key);
4508c2ecf20Sopenharmony_ci}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_cistatic __init int x86_nofsgsbase_setup(char *arg)
4538c2ecf20Sopenharmony_ci{
4548c2ecf20Sopenharmony_ci	/* Require an exact match without trailing characters. */
4558c2ecf20Sopenharmony_ci	if (strlen(arg))
4568c2ecf20Sopenharmony_ci		return 0;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	/* Do not emit a message if the feature is not present. */
4598c2ecf20Sopenharmony_ci	if (!boot_cpu_has(X86_FEATURE_FSGSBASE))
4608c2ecf20Sopenharmony_ci		return 1;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	setup_clear_cpu_cap(X86_FEATURE_FSGSBASE);
4638c2ecf20Sopenharmony_ci	pr_info("FSGSBASE disabled via kernel command line\n");
4648c2ecf20Sopenharmony_ci	return 1;
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci__setup("nofsgsbase", x86_nofsgsbase_setup);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci/*
4698c2ecf20Sopenharmony_ci * Protection Keys are not available in 32-bit mode.
4708c2ecf20Sopenharmony_ci */
4718c2ecf20Sopenharmony_cistatic bool pku_disabled;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_cistatic __always_inline void setup_pku(struct cpuinfo_x86 *c)
4748c2ecf20Sopenharmony_ci{
4758c2ecf20Sopenharmony_ci	/* check the boot processor, plus compile options for PKU: */
4768c2ecf20Sopenharmony_ci	if (!cpu_feature_enabled(X86_FEATURE_PKU))
4778c2ecf20Sopenharmony_ci		return;
4788c2ecf20Sopenharmony_ci	/* checks the actual processor's cpuid bits: */
4798c2ecf20Sopenharmony_ci	if (!cpu_has(c, X86_FEATURE_PKU))
4808c2ecf20Sopenharmony_ci		return;
4818c2ecf20Sopenharmony_ci	if (pku_disabled)
4828c2ecf20Sopenharmony_ci		return;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	cr4_set_bits(X86_CR4_PKE);
4858c2ecf20Sopenharmony_ci	/*
4868c2ecf20Sopenharmony_ci	 * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
4878c2ecf20Sopenharmony_ci	 * cpuid bit to be set.  We need to ensure that we
4888c2ecf20Sopenharmony_ci	 * update that bit in this CPU's "cpu_info".
4898c2ecf20Sopenharmony_ci	 */
4908c2ecf20Sopenharmony_ci	set_cpu_cap(c, X86_FEATURE_OSPKE);
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
4948c2ecf20Sopenharmony_cistatic __init int setup_disable_pku(char *arg)
4958c2ecf20Sopenharmony_ci{
4968c2ecf20Sopenharmony_ci	/*
4978c2ecf20Sopenharmony_ci	 * Do not clear the X86_FEATURE_PKU bit.  All of the
4988c2ecf20Sopenharmony_ci	 * runtime checks are against OSPKE so clearing the
4998c2ecf20Sopenharmony_ci	 * bit does nothing.
5008c2ecf20Sopenharmony_ci	 *
5018c2ecf20Sopenharmony_ci	 * This way, we will see "pku" in cpuinfo, but not
5028c2ecf20Sopenharmony_ci	 * "ospke", which is exactly what we want.  It shows
5038c2ecf20Sopenharmony_ci	 * that the CPU has PKU, but the OS has not enabled it.
5048c2ecf20Sopenharmony_ci	 * This happens to be exactly how a system would look
5058c2ecf20Sopenharmony_ci	 * if we disabled the config option.
5068c2ecf20Sopenharmony_ci	 */
5078c2ecf20Sopenharmony_ci	pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
5088c2ecf20Sopenharmony_ci	pku_disabled = true;
5098c2ecf20Sopenharmony_ci	return 1;
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci__setup("nopku", setup_disable_pku);
5128c2ecf20Sopenharmony_ci#endif /* CONFIG_X86_64 */
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci/*
5158c2ecf20Sopenharmony_ci * Some CPU features depend on higher CPUID levels, which may not always
5168c2ecf20Sopenharmony_ci * be available due to CPUID level capping or broken virtualization
5178c2ecf20Sopenharmony_ci * software.  Add those features to this table to auto-disable them.
5188c2ecf20Sopenharmony_ci */
5198c2ecf20Sopenharmony_cistruct cpuid_dependent_feature {
5208c2ecf20Sopenharmony_ci	u32 feature;
5218c2ecf20Sopenharmony_ci	u32 level;
5228c2ecf20Sopenharmony_ci};
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic const struct cpuid_dependent_feature
5258c2ecf20Sopenharmony_cicpuid_dependent_features[] = {
5268c2ecf20Sopenharmony_ci	{ X86_FEATURE_MWAIT,		0x00000005 },
5278c2ecf20Sopenharmony_ci	{ X86_FEATURE_DCA,		0x00000009 },
5288c2ecf20Sopenharmony_ci	{ X86_FEATURE_XSAVE,		0x0000000d },
5298c2ecf20Sopenharmony_ci	{ 0, 0 }
5308c2ecf20Sopenharmony_ci};
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_cistatic void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
5338c2ecf20Sopenharmony_ci{
5348c2ecf20Sopenharmony_ci	const struct cpuid_dependent_feature *df;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	for (df = cpuid_dependent_features; df->feature; df++) {
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci		if (!cpu_has(c, df->feature))
5398c2ecf20Sopenharmony_ci			continue;
5408c2ecf20Sopenharmony_ci		/*
5418c2ecf20Sopenharmony_ci		 * Note: cpuid_level is set to -1 if unavailable, but
5428c2ecf20Sopenharmony_ci		 * extended_extended_level is set to 0 if unavailable
5438c2ecf20Sopenharmony_ci		 * and the legitimate extended levels are all negative
5448c2ecf20Sopenharmony_ci		 * when signed; hence the weird messing around with
5458c2ecf20Sopenharmony_ci		 * signs here...
5468c2ecf20Sopenharmony_ci		 */
5478c2ecf20Sopenharmony_ci		if (!((s32)df->level < 0 ?
5488c2ecf20Sopenharmony_ci		     (u32)df->level > (u32)c->extended_cpuid_level :
5498c2ecf20Sopenharmony_ci		     (s32)df->level > (s32)c->cpuid_level))
5508c2ecf20Sopenharmony_ci			continue;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci		clear_cpu_cap(c, df->feature);
5538c2ecf20Sopenharmony_ci		if (!warn)
5548c2ecf20Sopenharmony_ci			continue;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci		pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
5578c2ecf20Sopenharmony_ci			x86_cap_flag(df->feature), df->level);
5588c2ecf20Sopenharmony_ci	}
5598c2ecf20Sopenharmony_ci}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci/*
5628c2ecf20Sopenharmony_ci * Naming convention should be: <Name> [(<Codename>)]
5638c2ecf20Sopenharmony_ci * This table only is used unless init_<vendor>() below doesn't set it;
5648c2ecf20Sopenharmony_ci * in particular, if CPUID levels 0x80000002..4 are supported, this
5658c2ecf20Sopenharmony_ci * isn't used
5668c2ecf20Sopenharmony_ci */
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci/* Look up CPU names by table lookup. */
5698c2ecf20Sopenharmony_cistatic const char *table_lookup_model(struct cpuinfo_x86 *c)
5708c2ecf20Sopenharmony_ci{
5718c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
5728c2ecf20Sopenharmony_ci	const struct legacy_cpu_model_info *info;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	if (c->x86_model >= 16)
5758c2ecf20Sopenharmony_ci		return NULL;	/* Range check */
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	if (!this_cpu)
5788c2ecf20Sopenharmony_ci		return NULL;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	info = this_cpu->legacy_models;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	while (info->family) {
5838c2ecf20Sopenharmony_ci		if (info->family == c->x86)
5848c2ecf20Sopenharmony_ci			return info->model_names[c->x86_model];
5858c2ecf20Sopenharmony_ci		info++;
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci#endif
5888c2ecf20Sopenharmony_ci	return NULL;		/* Not found */
5898c2ecf20Sopenharmony_ci}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci/* Aligned to unsigned long to avoid split lock in atomic bitmap ops */
5928c2ecf20Sopenharmony_ci__u32 cpu_caps_cleared[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
5938c2ecf20Sopenharmony_ci__u32 cpu_caps_set[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_civoid load_percpu_segment(int cpu)
5968c2ecf20Sopenharmony_ci{
5978c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
5988c2ecf20Sopenharmony_ci	loadsegment(fs, __KERNEL_PERCPU);
5998c2ecf20Sopenharmony_ci#else
6008c2ecf20Sopenharmony_ci	__loadsegment_simple(gs, 0);
6018c2ecf20Sopenharmony_ci	wrmsrl(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu));
6028c2ecf20Sopenharmony_ci#endif
6038c2ecf20Sopenharmony_ci	load_stack_canary_segment();
6048c2ecf20Sopenharmony_ci}
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
6078c2ecf20Sopenharmony_ci/* The 32-bit entry code needs to find cpu_entry_area. */
6088c2ecf20Sopenharmony_ciDEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
6098c2ecf20Sopenharmony_ci#endif
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci/* Load the original GDT from the per-cpu structure */
6128c2ecf20Sopenharmony_civoid load_direct_gdt(int cpu)
6138c2ecf20Sopenharmony_ci{
6148c2ecf20Sopenharmony_ci	struct desc_ptr gdt_descr;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
6178c2ecf20Sopenharmony_ci	gdt_descr.size = GDT_SIZE - 1;
6188c2ecf20Sopenharmony_ci	load_gdt(&gdt_descr);
6198c2ecf20Sopenharmony_ci}
6208c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(load_direct_gdt);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci/* Load a fixmap remapping of the per-cpu GDT */
6238c2ecf20Sopenharmony_civoid load_fixmap_gdt(int cpu)
6248c2ecf20Sopenharmony_ci{
6258c2ecf20Sopenharmony_ci	struct desc_ptr gdt_descr;
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
6288c2ecf20Sopenharmony_ci	gdt_descr.size = GDT_SIZE - 1;
6298c2ecf20Sopenharmony_ci	load_gdt(&gdt_descr);
6308c2ecf20Sopenharmony_ci}
6318c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(load_fixmap_gdt);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci/*
6348c2ecf20Sopenharmony_ci * Current gdt points %fs at the "master" per-cpu area: after this,
6358c2ecf20Sopenharmony_ci * it's on the real one.
6368c2ecf20Sopenharmony_ci */
6378c2ecf20Sopenharmony_civoid switch_to_new_gdt(int cpu)
6388c2ecf20Sopenharmony_ci{
6398c2ecf20Sopenharmony_ci	/* Load the original GDT */
6408c2ecf20Sopenharmony_ci	load_direct_gdt(cpu);
6418c2ecf20Sopenharmony_ci	/* Reload the per-cpu base */
6428c2ecf20Sopenharmony_ci	load_percpu_segment(cpu);
6438c2ecf20Sopenharmony_ci}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_cistatic const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic void get_model_name(struct cpuinfo_x86 *c)
6488c2ecf20Sopenharmony_ci{
6498c2ecf20Sopenharmony_ci	unsigned int *v;
6508c2ecf20Sopenharmony_ci	char *p, *q, *s;
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	if (c->extended_cpuid_level < 0x80000004)
6538c2ecf20Sopenharmony_ci		return;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	v = (unsigned int *)c->x86_model_id;
6568c2ecf20Sopenharmony_ci	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
6578c2ecf20Sopenharmony_ci	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
6588c2ecf20Sopenharmony_ci	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
6598c2ecf20Sopenharmony_ci	c->x86_model_id[48] = 0;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	/* Trim whitespace */
6628c2ecf20Sopenharmony_ci	p = q = s = &c->x86_model_id[0];
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	while (*p == ' ')
6658c2ecf20Sopenharmony_ci		p++;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	while (*p) {
6688c2ecf20Sopenharmony_ci		/* Note the last non-whitespace index */
6698c2ecf20Sopenharmony_ci		if (!isspace(*p))
6708c2ecf20Sopenharmony_ci			s = q;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci		*q++ = *p++;
6738c2ecf20Sopenharmony_ci	}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	*(s + 1) = '\0';
6768c2ecf20Sopenharmony_ci}
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_civoid detect_num_cpu_cores(struct cpuinfo_x86 *c)
6798c2ecf20Sopenharmony_ci{
6808c2ecf20Sopenharmony_ci	unsigned int eax, ebx, ecx, edx;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	c->x86_max_cores = 1;
6838c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_SMP) || c->cpuid_level < 4)
6848c2ecf20Sopenharmony_ci		return;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	cpuid_count(4, 0, &eax, &ebx, &ecx, &edx);
6878c2ecf20Sopenharmony_ci	if (eax & 0x1f)
6888c2ecf20Sopenharmony_ci		c->x86_max_cores = (eax >> 26) + 1;
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_civoid cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
6928c2ecf20Sopenharmony_ci{
6938c2ecf20Sopenharmony_ci	unsigned int n, dummy, ebx, ecx, edx, l2size;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	n = c->extended_cpuid_level;
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	if (n >= 0x80000005) {
6988c2ecf20Sopenharmony_ci		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
6998c2ecf20Sopenharmony_ci		c->x86_cache_size = (ecx>>24) + (edx>>24);
7008c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
7018c2ecf20Sopenharmony_ci		/* On K8 L1 TLB is inclusive, so don't count it */
7028c2ecf20Sopenharmony_ci		c->x86_tlbsize = 0;
7038c2ecf20Sopenharmony_ci#endif
7048c2ecf20Sopenharmony_ci	}
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	if (n < 0x80000006)	/* Some chips just has a large L1. */
7078c2ecf20Sopenharmony_ci		return;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
7108c2ecf20Sopenharmony_ci	l2size = ecx >> 16;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
7138c2ecf20Sopenharmony_ci	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
7148c2ecf20Sopenharmony_ci#else
7158c2ecf20Sopenharmony_ci	/* do processor-specific cache resizing */
7168c2ecf20Sopenharmony_ci	if (this_cpu->legacy_cache_size)
7178c2ecf20Sopenharmony_ci		l2size = this_cpu->legacy_cache_size(c, l2size);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	/* Allow user to override all this if necessary. */
7208c2ecf20Sopenharmony_ci	if (cachesize_override != -1)
7218c2ecf20Sopenharmony_ci		l2size = cachesize_override;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	if (l2size == 0)
7248c2ecf20Sopenharmony_ci		return;		/* Again, no L2 cache is possible */
7258c2ecf20Sopenharmony_ci#endif
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	c->x86_cache_size = l2size;
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ciu16 __read_mostly tlb_lli_4k[NR_INFO];
7318c2ecf20Sopenharmony_ciu16 __read_mostly tlb_lli_2m[NR_INFO];
7328c2ecf20Sopenharmony_ciu16 __read_mostly tlb_lli_4m[NR_INFO];
7338c2ecf20Sopenharmony_ciu16 __read_mostly tlb_lld_4k[NR_INFO];
7348c2ecf20Sopenharmony_ciu16 __read_mostly tlb_lld_2m[NR_INFO];
7358c2ecf20Sopenharmony_ciu16 __read_mostly tlb_lld_4m[NR_INFO];
7368c2ecf20Sopenharmony_ciu16 __read_mostly tlb_lld_1g[NR_INFO];
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_cistatic void cpu_detect_tlb(struct cpuinfo_x86 *c)
7398c2ecf20Sopenharmony_ci{
7408c2ecf20Sopenharmony_ci	if (this_cpu->c_detect_tlb)
7418c2ecf20Sopenharmony_ci		this_cpu->c_detect_tlb(c);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
7448c2ecf20Sopenharmony_ci		tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
7458c2ecf20Sopenharmony_ci		tlb_lli_4m[ENTRIES]);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
7488c2ecf20Sopenharmony_ci		tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
7498c2ecf20Sopenharmony_ci		tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
7508c2ecf20Sopenharmony_ci}
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ciint detect_ht_early(struct cpuinfo_x86 *c)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
7558c2ecf20Sopenharmony_ci	u32 eax, ebx, ecx, edx;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	if (!cpu_has(c, X86_FEATURE_HT))
7588c2ecf20Sopenharmony_ci		return -1;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
7618c2ecf20Sopenharmony_ci		return -1;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
7648c2ecf20Sopenharmony_ci		return -1;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	cpuid(1, &eax, &ebx, &ecx, &edx);
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	smp_num_siblings = (ebx & 0xff0000) >> 16;
7698c2ecf20Sopenharmony_ci	if (smp_num_siblings == 1)
7708c2ecf20Sopenharmony_ci		pr_info_once("CPU0: Hyper-Threading is disabled\n");
7718c2ecf20Sopenharmony_ci#endif
7728c2ecf20Sopenharmony_ci	return 0;
7738c2ecf20Sopenharmony_ci}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_civoid detect_ht(struct cpuinfo_x86 *c)
7768c2ecf20Sopenharmony_ci{
7778c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
7788c2ecf20Sopenharmony_ci	int index_msb, core_bits;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	if (detect_ht_early(c) < 0)
7818c2ecf20Sopenharmony_ci		return;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	index_msb = get_count_order(smp_num_siblings);
7848c2ecf20Sopenharmony_ci	c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	smp_num_siblings = smp_num_siblings / c->x86_max_cores;
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	index_msb = get_count_order(smp_num_siblings);
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	core_bits = get_count_order(c->x86_max_cores);
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
7938c2ecf20Sopenharmony_ci				       ((1 << core_bits) - 1);
7948c2ecf20Sopenharmony_ci#endif
7958c2ecf20Sopenharmony_ci}
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_cistatic void get_cpu_vendor(struct cpuinfo_x86 *c)
7988c2ecf20Sopenharmony_ci{
7998c2ecf20Sopenharmony_ci	char *v = c->x86_vendor_id;
8008c2ecf20Sopenharmony_ci	int i;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	for (i = 0; i < X86_VENDOR_NUM; i++) {
8038c2ecf20Sopenharmony_ci		if (!cpu_devs[i])
8048c2ecf20Sopenharmony_ci			break;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
8078c2ecf20Sopenharmony_ci		    (cpu_devs[i]->c_ident[1] &&
8088c2ecf20Sopenharmony_ci		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci			this_cpu = cpu_devs[i];
8118c2ecf20Sopenharmony_ci			c->x86_vendor = this_cpu->c_x86_vendor;
8128c2ecf20Sopenharmony_ci			return;
8138c2ecf20Sopenharmony_ci		}
8148c2ecf20Sopenharmony_ci	}
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
8178c2ecf20Sopenharmony_ci		    "CPU: Your system may be unstable.\n", v);
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	c->x86_vendor = X86_VENDOR_UNKNOWN;
8208c2ecf20Sopenharmony_ci	this_cpu = &default_cpu;
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_civoid cpu_detect(struct cpuinfo_x86 *c)
8248c2ecf20Sopenharmony_ci{
8258c2ecf20Sopenharmony_ci	/* Get vendor name */
8268c2ecf20Sopenharmony_ci	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
8278c2ecf20Sopenharmony_ci	      (unsigned int *)&c->x86_vendor_id[0],
8288c2ecf20Sopenharmony_ci	      (unsigned int *)&c->x86_vendor_id[8],
8298c2ecf20Sopenharmony_ci	      (unsigned int *)&c->x86_vendor_id[4]);
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	c->x86 = 4;
8328c2ecf20Sopenharmony_ci	/* Intel-defined flags: level 0x00000001 */
8338c2ecf20Sopenharmony_ci	if (c->cpuid_level >= 0x00000001) {
8348c2ecf20Sopenharmony_ci		u32 junk, tfms, cap0, misc;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
8378c2ecf20Sopenharmony_ci		c->x86		= x86_family(tfms);
8388c2ecf20Sopenharmony_ci		c->x86_model	= x86_model(tfms);
8398c2ecf20Sopenharmony_ci		c->x86_stepping	= x86_stepping(tfms);
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci		if (cap0 & (1<<19)) {
8428c2ecf20Sopenharmony_ci			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
8438c2ecf20Sopenharmony_ci			c->x86_cache_alignment = c->x86_clflush_size;
8448c2ecf20Sopenharmony_ci		}
8458c2ecf20Sopenharmony_ci	}
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_cistatic void apply_forced_caps(struct cpuinfo_x86 *c)
8498c2ecf20Sopenharmony_ci{
8508c2ecf20Sopenharmony_ci	int i;
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
8538c2ecf20Sopenharmony_ci		c->x86_capability[i] &= ~cpu_caps_cleared[i];
8548c2ecf20Sopenharmony_ci		c->x86_capability[i] |= cpu_caps_set[i];
8558c2ecf20Sopenharmony_ci	}
8568c2ecf20Sopenharmony_ci}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_cistatic void init_speculation_control(struct cpuinfo_x86 *c)
8598c2ecf20Sopenharmony_ci{
8608c2ecf20Sopenharmony_ci	/*
8618c2ecf20Sopenharmony_ci	 * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
8628c2ecf20Sopenharmony_ci	 * and they also have a different bit for STIBP support. Also,
8638c2ecf20Sopenharmony_ci	 * a hypervisor might have set the individual AMD bits even on
8648c2ecf20Sopenharmony_ci	 * Intel CPUs, for finer-grained selection of what's available.
8658c2ecf20Sopenharmony_ci	 */
8668c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
8678c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_IBRS);
8688c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_IBPB);
8698c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8708c2ecf20Sopenharmony_ci	}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
8738c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_STIBP);
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
8768c2ecf20Sopenharmony_ci	    cpu_has(c, X86_FEATURE_VIRT_SSBD))
8778c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_SSBD);
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
8808c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_IBRS);
8818c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8828c2ecf20Sopenharmony_ci	}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_AMD_IBPB))
8858c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_IBPB);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
8888c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_STIBP);
8898c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8908c2ecf20Sopenharmony_ci	}
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
8938c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_SSBD);
8948c2ecf20Sopenharmony_ci		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
8958c2ecf20Sopenharmony_ci		clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
8968c2ecf20Sopenharmony_ci	}
8978c2ecf20Sopenharmony_ci}
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_civoid get_cpu_cap(struct cpuinfo_x86 *c)
9008c2ecf20Sopenharmony_ci{
9018c2ecf20Sopenharmony_ci	u32 eax, ebx, ecx, edx;
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	/* Intel-defined flags: level 0x00000001 */
9048c2ecf20Sopenharmony_ci	if (c->cpuid_level >= 0x00000001) {
9058c2ecf20Sopenharmony_ci		cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_1_ECX] = ecx;
9088c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_1_EDX] = edx;
9098c2ecf20Sopenharmony_ci	}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	/* Thermal and Power Management Leaf: level 0x00000006 (eax) */
9128c2ecf20Sopenharmony_ci	if (c->cpuid_level >= 0x00000006)
9138c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	/* Additional Intel-defined flags: level 0x00000007 */
9168c2ecf20Sopenharmony_ci	if (c->cpuid_level >= 0x00000007) {
9178c2ecf20Sopenharmony_ci		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
9188c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_7_0_EBX] = ebx;
9198c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_7_ECX] = ecx;
9208c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_7_EDX] = edx;
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci		/* Check valid sub-leaf index before accessing it */
9238c2ecf20Sopenharmony_ci		if (eax >= 1) {
9248c2ecf20Sopenharmony_ci			cpuid_count(0x00000007, 1, &eax, &ebx, &ecx, &edx);
9258c2ecf20Sopenharmony_ci			c->x86_capability[CPUID_7_1_EAX] = eax;
9268c2ecf20Sopenharmony_ci		}
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	/* Extended state features: level 0x0000000d */
9308c2ecf20Sopenharmony_ci	if (c->cpuid_level >= 0x0000000d) {
9318c2ecf20Sopenharmony_ci		cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_D_1_EAX] = eax;
9348c2ecf20Sopenharmony_ci	}
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	/* AMD-defined flags: level 0x80000001 */
9378c2ecf20Sopenharmony_ci	eax = cpuid_eax(0x80000000);
9388c2ecf20Sopenharmony_ci	c->extended_cpuid_level = eax;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	if ((eax & 0xffff0000) == 0x80000000) {
9418c2ecf20Sopenharmony_ci		if (eax >= 0x80000001) {
9428c2ecf20Sopenharmony_ci			cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci			c->x86_capability[CPUID_8000_0001_ECX] = ecx;
9458c2ecf20Sopenharmony_ci			c->x86_capability[CPUID_8000_0001_EDX] = edx;
9468c2ecf20Sopenharmony_ci		}
9478c2ecf20Sopenharmony_ci	}
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	if (c->extended_cpuid_level >= 0x80000007) {
9508c2ecf20Sopenharmony_ci		cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_8000_0007_EBX] = ebx;
9538c2ecf20Sopenharmony_ci		c->x86_power = edx;
9548c2ecf20Sopenharmony_ci	}
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	if (c->extended_cpuid_level >= 0x80000008) {
9578c2ecf20Sopenharmony_ci		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
9588c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_8000_0008_EBX] = ebx;
9598c2ecf20Sopenharmony_ci	}
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	if (c->extended_cpuid_level >= 0x8000000a)
9628c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	if (c->extended_cpuid_level >= 0x8000001f)
9658c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_8000_001F_EAX] = cpuid_eax(0x8000001f);
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	if (c->extended_cpuid_level >= 0x80000021)
9688c2ecf20Sopenharmony_ci		c->x86_capability[CPUID_8000_0021_EAX] = cpuid_eax(0x80000021);
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	init_scattered_cpuid_features(c);
9718c2ecf20Sopenharmony_ci	init_speculation_control(c);
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	/*
9748c2ecf20Sopenharmony_ci	 * Clear/Set all flags overridden by options, after probe.
9758c2ecf20Sopenharmony_ci	 * This needs to happen each time we re-probe, which may happen
9768c2ecf20Sopenharmony_ci	 * several times during CPU initialization.
9778c2ecf20Sopenharmony_ci	 */
9788c2ecf20Sopenharmony_ci	apply_forced_caps(c);
9798c2ecf20Sopenharmony_ci}
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_civoid get_cpu_address_sizes(struct cpuinfo_x86 *c)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	u32 eax, ebx, ecx, edx;
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	if (c->extended_cpuid_level >= 0x80000008) {
9868c2ecf20Sopenharmony_ci		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci		c->x86_virt_bits = (eax >> 8) & 0xff;
9898c2ecf20Sopenharmony_ci		c->x86_phys_bits = eax & 0xff;
9908c2ecf20Sopenharmony_ci	}
9918c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
9928c2ecf20Sopenharmony_ci	else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
9938c2ecf20Sopenharmony_ci		c->x86_phys_bits = 36;
9948c2ecf20Sopenharmony_ci#endif
9958c2ecf20Sopenharmony_ci	c->x86_cache_bits = c->x86_phys_bits;
9968c2ecf20Sopenharmony_ci}
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_cistatic void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
9998c2ecf20Sopenharmony_ci{
10008c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
10018c2ecf20Sopenharmony_ci	int i;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	/*
10048c2ecf20Sopenharmony_ci	 * First of all, decide if this is a 486 or higher
10058c2ecf20Sopenharmony_ci	 * It's a 486 if we can modify the AC flag
10068c2ecf20Sopenharmony_ci	 */
10078c2ecf20Sopenharmony_ci	if (flag_is_changeable_p(X86_EFLAGS_AC))
10088c2ecf20Sopenharmony_ci		c->x86 = 4;
10098c2ecf20Sopenharmony_ci	else
10108c2ecf20Sopenharmony_ci		c->x86 = 3;
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	for (i = 0; i < X86_VENDOR_NUM; i++)
10138c2ecf20Sopenharmony_ci		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
10148c2ecf20Sopenharmony_ci			c->x86_vendor_id[0] = 0;
10158c2ecf20Sopenharmony_ci			cpu_devs[i]->c_identify(c);
10168c2ecf20Sopenharmony_ci			if (c->x86_vendor_id[0]) {
10178c2ecf20Sopenharmony_ci				get_cpu_vendor(c);
10188c2ecf20Sopenharmony_ci				break;
10198c2ecf20Sopenharmony_ci			}
10208c2ecf20Sopenharmony_ci		}
10218c2ecf20Sopenharmony_ci#endif
10228c2ecf20Sopenharmony_ci}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci#define NO_SPECULATION		BIT(0)
10258c2ecf20Sopenharmony_ci#define NO_MELTDOWN		BIT(1)
10268c2ecf20Sopenharmony_ci#define NO_SSB			BIT(2)
10278c2ecf20Sopenharmony_ci#define NO_L1TF			BIT(3)
10288c2ecf20Sopenharmony_ci#define NO_MDS			BIT(4)
10298c2ecf20Sopenharmony_ci#define MSBDS_ONLY		BIT(5)
10308c2ecf20Sopenharmony_ci#define NO_SWAPGS		BIT(6)
10318c2ecf20Sopenharmony_ci#define NO_ITLB_MULTIHIT	BIT(7)
10328c2ecf20Sopenharmony_ci#define NO_SPECTRE_V2		BIT(8)
10338c2ecf20Sopenharmony_ci#define NO_MMIO			BIT(9)
10348c2ecf20Sopenharmony_ci#define NO_EIBRS_PBRSB		BIT(10)
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci#define VULNWL(vendor, family, model, whitelist)	\
10378c2ecf20Sopenharmony_ci	X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, whitelist)
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci#define VULNWL_INTEL(model, whitelist)		\
10408c2ecf20Sopenharmony_ci	VULNWL(INTEL, 6, INTEL_FAM6_##model, whitelist)
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci#define VULNWL_AMD(family, whitelist)		\
10438c2ecf20Sopenharmony_ci	VULNWL(AMD, family, X86_MODEL_ANY, whitelist)
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci#define VULNWL_HYGON(family, whitelist)		\
10468c2ecf20Sopenharmony_ci	VULNWL(HYGON, family, X86_MODEL_ANY, whitelist)
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_cistatic const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
10498c2ecf20Sopenharmony_ci	VULNWL(ANY,	4, X86_MODEL_ANY,	NO_SPECULATION),
10508c2ecf20Sopenharmony_ci	VULNWL(CENTAUR,	5, X86_MODEL_ANY,	NO_SPECULATION),
10518c2ecf20Sopenharmony_ci	VULNWL(INTEL,	5, X86_MODEL_ANY,	NO_SPECULATION),
10528c2ecf20Sopenharmony_ci	VULNWL(NSC,	5, X86_MODEL_ANY,	NO_SPECULATION),
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	/* Intel Family 6 */
10558c2ecf20Sopenharmony_ci	VULNWL_INTEL(TIGERLAKE,			NO_MMIO),
10568c2ecf20Sopenharmony_ci	VULNWL_INTEL(TIGERLAKE_L,		NO_MMIO),
10578c2ecf20Sopenharmony_ci	VULNWL_INTEL(ALDERLAKE,			NO_MMIO),
10588c2ecf20Sopenharmony_ci	VULNWL_INTEL(ALDERLAKE_L,		NO_MMIO),
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_SALTWELL,		NO_SPECULATION | NO_ITLB_MULTIHIT),
10618c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_SALTWELL_TABLET,	NO_SPECULATION | NO_ITLB_MULTIHIT),
10628c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_SALTWELL_MID,		NO_SPECULATION | NO_ITLB_MULTIHIT),
10638c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_BONNELL,		NO_SPECULATION | NO_ITLB_MULTIHIT),
10648c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_BONNELL_MID,		NO_SPECULATION | NO_ITLB_MULTIHIT),
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_SILVERMONT,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
10678c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_SILVERMONT_D,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
10688c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_SILVERMONT_MID,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
10698c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_AIRMONT,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
10708c2ecf20Sopenharmony_ci	VULNWL_INTEL(XEON_PHI_KNL,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
10718c2ecf20Sopenharmony_ci	VULNWL_INTEL(XEON_PHI_KNM,		NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	VULNWL_INTEL(CORE_YONAH,		NO_SSB),
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_AIRMONT_MID,		NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
10768c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_AIRMONT_NP,		NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_GOLDMONT,		NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
10798c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_GOLDMONT_D,		NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
10808c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_GOLDMONT_PLUS,	NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB),
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	/*
10838c2ecf20Sopenharmony_ci	 * Technically, swapgs isn't serializing on AMD (despite it previously
10848c2ecf20Sopenharmony_ci	 * being documented as such in the APM).  But according to AMD, %gs is
10858c2ecf20Sopenharmony_ci	 * updated non-speculatively, and the issuing of %gs-relative memory
10868c2ecf20Sopenharmony_ci	 * operands will be blocked until the %gs update completes, which is
10878c2ecf20Sopenharmony_ci	 * good enough for our purposes.
10888c2ecf20Sopenharmony_ci	 */
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_TREMONT,		NO_EIBRS_PBRSB),
10918c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_TREMONT_L,		NO_EIBRS_PBRSB),
10928c2ecf20Sopenharmony_ci	VULNWL_INTEL(ATOM_TREMONT_D,		NO_ITLB_MULTIHIT | NO_EIBRS_PBRSB),
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	/* AMD Family 0xf - 0x12 */
10958c2ecf20Sopenharmony_ci	VULNWL_AMD(0x0f,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
10968c2ecf20Sopenharmony_ci	VULNWL_AMD(0x10,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
10978c2ecf20Sopenharmony_ci	VULNWL_AMD(0x11,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
10988c2ecf20Sopenharmony_ci	VULNWL_AMD(0x12,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	/* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */
11018c2ecf20Sopenharmony_ci	VULNWL_AMD(X86_FAMILY_ANY,	NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB),
11028c2ecf20Sopenharmony_ci	VULNWL_HYGON(X86_FAMILY_ANY,	NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB),
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	/* Zhaoxin Family 7 */
11058c2ecf20Sopenharmony_ci	VULNWL(CENTAUR,	7, X86_MODEL_ANY,	NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO),
11068c2ecf20Sopenharmony_ci	VULNWL(ZHAOXIN,	7, X86_MODEL_ANY,	NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO),
11078c2ecf20Sopenharmony_ci	{}
11088c2ecf20Sopenharmony_ci};
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci#define VULNBL(vendor, family, model, blacklist)	\
11118c2ecf20Sopenharmony_ci	X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, blacklist)
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci#define VULNBL_INTEL_STEPPINGS(model, steppings, issues)		   \
11148c2ecf20Sopenharmony_ci	X86_MATCH_VENDOR_FAM_MODEL_STEPPINGS_FEATURE(INTEL, 6,		   \
11158c2ecf20Sopenharmony_ci					    INTEL_FAM6_##model, steppings, \
11168c2ecf20Sopenharmony_ci					    X86_FEATURE_ANY, issues)
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci#define VULNBL_AMD(family, blacklist)		\
11198c2ecf20Sopenharmony_ci	VULNBL(AMD, family, X86_MODEL_ANY, blacklist)
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci#define VULNBL_HYGON(family, blacklist)		\
11228c2ecf20Sopenharmony_ci	VULNBL(HYGON, family, X86_MODEL_ANY, blacklist)
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci#define SRBDS		BIT(0)
11258c2ecf20Sopenharmony_ci/* CPU is affected by X86_BUG_MMIO_STALE_DATA */
11268c2ecf20Sopenharmony_ci#define MMIO		BIT(1)
11278c2ecf20Sopenharmony_ci/* CPU is affected by Shared Buffers Data Sampling (SBDS), a variant of X86_BUG_MMIO_STALE_DATA */
11288c2ecf20Sopenharmony_ci#define MMIO_SBDS	BIT(2)
11298c2ecf20Sopenharmony_ci/* CPU is affected by RETbleed, speculating where you would not expect it */
11308c2ecf20Sopenharmony_ci#define RETBLEED	BIT(3)
11318c2ecf20Sopenharmony_ci/* CPU is affected by SMT (cross-thread) return predictions */
11328c2ecf20Sopenharmony_ci#define SMT_RSB		BIT(4)
11338c2ecf20Sopenharmony_ci/* CPU is affected by SRSO */
11348c2ecf20Sopenharmony_ci#define SRSO		BIT(5)
11358c2ecf20Sopenharmony_ci/* CPU is affected by GDS */
11368c2ecf20Sopenharmony_ci#define GDS		BIT(6)
11378c2ecf20Sopenharmony_ci/* CPU is affected by Register File Data Sampling */
11388c2ecf20Sopenharmony_ci#define RFDS		BIT(7)
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_cistatic const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
11418c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(IVYBRIDGE,	X86_STEPPING_ANY,		SRBDS),
11428c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(HASWELL,		X86_STEPPING_ANY,		SRBDS),
11438c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(HASWELL_L,	X86_STEPPING_ANY,		SRBDS),
11448c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(HASWELL_G,	X86_STEPPING_ANY,		SRBDS),
11458c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(HASWELL_X,	X86_STEPPING_ANY,		MMIO),
11468c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(BROADWELL_D,	X86_STEPPING_ANY,		MMIO),
11478c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(BROADWELL_G,	X86_STEPPING_ANY,		SRBDS),
11488c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(BROADWELL_X,	X86_STEPPING_ANY,		MMIO),
11498c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(BROADWELL,	X86_STEPPING_ANY,		SRBDS),
11508c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(SKYLAKE_X,	X86_STEPPING_ANY,		MMIO | RETBLEED | GDS),
11518c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(SKYLAKE_L,	X86_STEPPING_ANY,		MMIO | RETBLEED | GDS | SRBDS),
11528c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(SKYLAKE,		X86_STEPPING_ANY,		MMIO | RETBLEED | GDS | SRBDS),
11538c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(KABYLAKE_L,	X86_STEPPING_ANY,		MMIO | RETBLEED | GDS | SRBDS),
11548c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(KABYLAKE,	X86_STEPPING_ANY,		MMIO | RETBLEED | GDS | SRBDS),
11558c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(CANNONLAKE_L,	X86_STEPPING_ANY,		RETBLEED),
11568c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ICELAKE_L,	X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RETBLEED | GDS),
11578c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ICELAKE_D,	X86_STEPPING_ANY,		MMIO | GDS),
11588c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ICELAKE_X,	X86_STEPPING_ANY,		MMIO | GDS),
11598c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(COMETLAKE,	X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RETBLEED | GDS),
11608c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(COMETLAKE_L,	X86_STEPPINGS(0x0, 0x0),	MMIO | RETBLEED),
11618c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(COMETLAKE_L,	X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RETBLEED | GDS),
11628c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(TIGERLAKE_L,	X86_STEPPING_ANY,		GDS),
11638c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(TIGERLAKE,	X86_STEPPING_ANY,		GDS),
11648c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(LAKEFIELD,	X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RETBLEED),
11658c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ROCKETLAKE,	X86_STEPPING_ANY,		MMIO | RETBLEED | GDS),
11668c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ALDERLAKE,	X86_STEPPING_ANY,		RFDS),
11678c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ALDERLAKE_L,	X86_STEPPING_ANY,		RFDS),
11688c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(RAPTORLAKE,	X86_STEPPING_ANY,		RFDS),
11698c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(RAPTORLAKE_P,	X86_STEPPING_ANY,		RFDS),
11708c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(RAPTORLAKE_S,	X86_STEPPING_ANY,		RFDS),
11718c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ALDERLAKE_N,	X86_STEPPING_ANY,		RFDS),
11728c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ATOM_TREMONT,	X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RFDS),
11738c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ATOM_TREMONT_D,	X86_STEPPING_ANY,		MMIO | RFDS),
11748c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ATOM_TREMONT_L,	X86_STEPPING_ANY,		MMIO | MMIO_SBDS | RFDS),
11758c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ATOM_GOLDMONT,	X86_STEPPING_ANY,		RFDS),
11768c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ATOM_GOLDMONT_D,	X86_STEPPING_ANY,		RFDS),
11778c2ecf20Sopenharmony_ci	VULNBL_INTEL_STEPPINGS(ATOM_GOLDMONT_PLUS, X86_STEPPING_ANY,		RFDS),
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	VULNBL_AMD(0x15, RETBLEED),
11808c2ecf20Sopenharmony_ci	VULNBL_AMD(0x16, RETBLEED),
11818c2ecf20Sopenharmony_ci	VULNBL_AMD(0x17, RETBLEED | SRSO),
11828c2ecf20Sopenharmony_ci	VULNBL_HYGON(0x18, RETBLEED),
11838c2ecf20Sopenharmony_ci	VULNBL_AMD(0x19, SRSO),
11848c2ecf20Sopenharmony_ci	{}
11858c2ecf20Sopenharmony_ci};
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_cistatic bool __init cpu_matches(const struct x86_cpu_id *table, unsigned long which)
11888c2ecf20Sopenharmony_ci{
11898c2ecf20Sopenharmony_ci	const struct x86_cpu_id *m = x86_match_cpu(table);
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	return m && !!(m->driver_data & which);
11928c2ecf20Sopenharmony_ci}
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ciu64 x86_read_arch_cap_msr(void)
11958c2ecf20Sopenharmony_ci{
11968c2ecf20Sopenharmony_ci	u64 ia32_cap = 0;
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_ci	if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
11998c2ecf20Sopenharmony_ci		rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci	return ia32_cap;
12028c2ecf20Sopenharmony_ci}
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_cistatic bool arch_cap_mmio_immune(u64 ia32_cap)
12058c2ecf20Sopenharmony_ci{
12068c2ecf20Sopenharmony_ci	return (ia32_cap & ARCH_CAP_FBSDP_NO &&
12078c2ecf20Sopenharmony_ci		ia32_cap & ARCH_CAP_PSDP_NO &&
12088c2ecf20Sopenharmony_ci		ia32_cap & ARCH_CAP_SBDR_SSDP_NO);
12098c2ecf20Sopenharmony_ci}
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_cistatic bool __init vulnerable_to_rfds(u64 ia32_cap)
12128c2ecf20Sopenharmony_ci{
12138c2ecf20Sopenharmony_ci	/* The "immunity" bit trumps everything else: */
12148c2ecf20Sopenharmony_ci	if (ia32_cap & ARCH_CAP_RFDS_NO)
12158c2ecf20Sopenharmony_ci		return false;
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci	/*
12188c2ecf20Sopenharmony_ci	 * VMMs set ARCH_CAP_RFDS_CLEAR for processors not in the blacklist to
12198c2ecf20Sopenharmony_ci	 * indicate that mitigation is needed because guest is running on a
12208c2ecf20Sopenharmony_ci	 * vulnerable hardware or may migrate to such hardware:
12218c2ecf20Sopenharmony_ci	 */
12228c2ecf20Sopenharmony_ci	if (ia32_cap & ARCH_CAP_RFDS_CLEAR)
12238c2ecf20Sopenharmony_ci		return true;
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	/* Only consult the blacklist when there is no enumeration: */
12268c2ecf20Sopenharmony_ci	return cpu_matches(cpu_vuln_blacklist, RFDS);
12278c2ecf20Sopenharmony_ci}
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_cistatic void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
12308c2ecf20Sopenharmony_ci{
12318c2ecf20Sopenharmony_ci	u64 ia32_cap = x86_read_arch_cap_msr();
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci	/* Set ITLB_MULTIHIT bug if cpu is not in the whitelist and not mitigated */
12348c2ecf20Sopenharmony_ci	if (!cpu_matches(cpu_vuln_whitelist, NO_ITLB_MULTIHIT) &&
12358c2ecf20Sopenharmony_ci	    !(ia32_cap & ARCH_CAP_PSCHANGE_MC_NO))
12368c2ecf20Sopenharmony_ci		setup_force_cpu_bug(X86_BUG_ITLB_MULTIHIT);
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	if (cpu_matches(cpu_vuln_whitelist, NO_SPECULATION))
12398c2ecf20Sopenharmony_ci		return;
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	if (!cpu_matches(cpu_vuln_whitelist, NO_SPECTRE_V2))
12448c2ecf20Sopenharmony_ci		setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	if (!cpu_matches(cpu_vuln_whitelist, NO_SSB) &&
12478c2ecf20Sopenharmony_ci	    !(ia32_cap & ARCH_CAP_SSB_NO) &&
12488c2ecf20Sopenharmony_ci	   !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
12498c2ecf20Sopenharmony_ci		setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	/*
12528c2ecf20Sopenharmony_ci	 * AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature
12538c2ecf20Sopenharmony_ci	 * flag and protect from vendor-specific bugs via the whitelist.
12548c2ecf20Sopenharmony_ci	 */
12558c2ecf20Sopenharmony_ci	if ((ia32_cap & ARCH_CAP_IBRS_ALL) || cpu_has(c, X86_FEATURE_AUTOIBRS)) {
12568c2ecf20Sopenharmony_ci		setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
12578c2ecf20Sopenharmony_ci		if (!cpu_matches(cpu_vuln_whitelist, NO_EIBRS_PBRSB) &&
12588c2ecf20Sopenharmony_ci		    !(ia32_cap & ARCH_CAP_PBRSB_NO))
12598c2ecf20Sopenharmony_ci			setup_force_cpu_bug(X86_BUG_EIBRS_PBRSB);
12608c2ecf20Sopenharmony_ci	}
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	if (!cpu_matches(cpu_vuln_whitelist, NO_MDS) &&
12638c2ecf20Sopenharmony_ci	    !(ia32_cap & ARCH_CAP_MDS_NO)) {
12648c2ecf20Sopenharmony_ci		setup_force_cpu_bug(X86_BUG_MDS);
12658c2ecf20Sopenharmony_ci		if (cpu_matches(cpu_vuln_whitelist, MSBDS_ONLY))
12668c2ecf20Sopenharmony_ci			setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
12678c2ecf20Sopenharmony_ci	}
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	if (!cpu_matches(cpu_vuln_whitelist, NO_SWAPGS))
12708c2ecf20Sopenharmony_ci		setup_force_cpu_bug(X86_BUG_SWAPGS);
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci	/*
12738c2ecf20Sopenharmony_ci	 * When the CPU is not mitigated for TAA (TAA_NO=0) set TAA bug when:
12748c2ecf20Sopenharmony_ci	 *	- TSX is supported or
12758c2ecf20Sopenharmony_ci	 *	- TSX_CTRL is present
12768c2ecf20Sopenharmony_ci	 *
12778c2ecf20Sopenharmony_ci	 * TSX_CTRL check is needed for cases when TSX could be disabled before
12788c2ecf20Sopenharmony_ci	 * the kernel boot e.g. kexec.
12798c2ecf20Sopenharmony_ci	 * TSX_CTRL check alone is not sufficient for cases when the microcode
12808c2ecf20Sopenharmony_ci	 * update is not present or running as guest that don't get TSX_CTRL.
12818c2ecf20Sopenharmony_ci	 */
12828c2ecf20Sopenharmony_ci	if (!(ia32_cap & ARCH_CAP_TAA_NO) &&
12838c2ecf20Sopenharmony_ci	    (cpu_has(c, X86_FEATURE_RTM) ||
12848c2ecf20Sopenharmony_ci	     (ia32_cap & ARCH_CAP_TSX_CTRL_MSR)))
12858c2ecf20Sopenharmony_ci		setup_force_cpu_bug(X86_BUG_TAA);
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci	/*
12888c2ecf20Sopenharmony_ci	 * SRBDS affects CPUs which support RDRAND or RDSEED and are listed
12898c2ecf20Sopenharmony_ci	 * in the vulnerability blacklist.
12908c2ecf20Sopenharmony_ci	 *
12918c2ecf20Sopenharmony_ci	 * Some of the implications and mitigation of Shared Buffers Data
12928c2ecf20Sopenharmony_ci	 * Sampling (SBDS) are similar to SRBDS. Give SBDS same treatment as
12938c2ecf20Sopenharmony_ci	 * SRBDS.
12948c2ecf20Sopenharmony_ci	 */
12958c2ecf20Sopenharmony_ci	if ((cpu_has(c, X86_FEATURE_RDRAND) ||
12968c2ecf20Sopenharmony_ci	     cpu_has(c, X86_FEATURE_RDSEED)) &&
12978c2ecf20Sopenharmony_ci	    cpu_matches(cpu_vuln_blacklist, SRBDS | MMIO_SBDS))
12988c2ecf20Sopenharmony_ci		    setup_force_cpu_bug(X86_BUG_SRBDS);
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	/*
13018c2ecf20Sopenharmony_ci	 * Processor MMIO Stale Data bug enumeration
13028c2ecf20Sopenharmony_ci	 *
13038c2ecf20Sopenharmony_ci	 * Affected CPU list is generally enough to enumerate the vulnerability,
13048c2ecf20Sopenharmony_ci	 * but for virtualization case check for ARCH_CAP MSR bits also, VMM may
13058c2ecf20Sopenharmony_ci	 * not want the guest to enumerate the bug.
13068c2ecf20Sopenharmony_ci	 *
13078c2ecf20Sopenharmony_ci	 * Set X86_BUG_MMIO_UNKNOWN for CPUs that are neither in the blacklist,
13088c2ecf20Sopenharmony_ci	 * nor in the whitelist and also don't enumerate MSR ARCH_CAP MMIO bits.
13098c2ecf20Sopenharmony_ci	 */
13108c2ecf20Sopenharmony_ci	if (!arch_cap_mmio_immune(ia32_cap)) {
13118c2ecf20Sopenharmony_ci		if (cpu_matches(cpu_vuln_blacklist, MMIO))
13128c2ecf20Sopenharmony_ci			setup_force_cpu_bug(X86_BUG_MMIO_STALE_DATA);
13138c2ecf20Sopenharmony_ci		else if (!cpu_matches(cpu_vuln_whitelist, NO_MMIO))
13148c2ecf20Sopenharmony_ci			setup_force_cpu_bug(X86_BUG_MMIO_UNKNOWN);
13158c2ecf20Sopenharmony_ci	}
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci	if (!cpu_has(c, X86_FEATURE_BTC_NO)) {
13188c2ecf20Sopenharmony_ci		if (cpu_matches(cpu_vuln_blacklist, RETBLEED) || (ia32_cap & ARCH_CAP_RSBA))
13198c2ecf20Sopenharmony_ci			setup_force_cpu_bug(X86_BUG_RETBLEED);
13208c2ecf20Sopenharmony_ci	}
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_ci	/*
13238c2ecf20Sopenharmony_ci	 * Check if CPU is vulnerable to GDS. If running in a virtual machine on
13248c2ecf20Sopenharmony_ci	 * an affected processor, the VMM may have disabled the use of GATHER by
13258c2ecf20Sopenharmony_ci	 * disabling AVX2. The only way to do this in HW is to clear XCR0[2],
13268c2ecf20Sopenharmony_ci	 * which means that AVX will be disabled.
13278c2ecf20Sopenharmony_ci	 */
13288c2ecf20Sopenharmony_ci	if (cpu_matches(cpu_vuln_blacklist, GDS) && !(ia32_cap & ARCH_CAP_GDS_NO) &&
13298c2ecf20Sopenharmony_ci	    boot_cpu_has(X86_FEATURE_AVX))
13308c2ecf20Sopenharmony_ci		setup_force_cpu_bug(X86_BUG_GDS);
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci	if (!cpu_has(c, X86_FEATURE_SRSO_NO)) {
13338c2ecf20Sopenharmony_ci		if (cpu_matches(cpu_vuln_blacklist, SRSO))
13348c2ecf20Sopenharmony_ci			setup_force_cpu_bug(X86_BUG_SRSO);
13358c2ecf20Sopenharmony_ci	}
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	if (vulnerable_to_rfds(ia32_cap))
13388c2ecf20Sopenharmony_ci		setup_force_cpu_bug(X86_BUG_RFDS);
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci	if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
13418c2ecf20Sopenharmony_ci		return;
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	/* Rogue Data Cache Load? No! */
13448c2ecf20Sopenharmony_ci	if (ia32_cap & ARCH_CAP_RDCL_NO)
13458c2ecf20Sopenharmony_ci		return;
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci	setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_ci	if (cpu_matches(cpu_vuln_whitelist, NO_L1TF))
13508c2ecf20Sopenharmony_ci		return;
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci	setup_force_cpu_bug(X86_BUG_L1TF);
13538c2ecf20Sopenharmony_ci}
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci/*
13568c2ecf20Sopenharmony_ci * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
13578c2ecf20Sopenharmony_ci * unfortunately, that's not true in practice because of early VIA
13588c2ecf20Sopenharmony_ci * chips and (more importantly) broken virtualizers that are not easy
13598c2ecf20Sopenharmony_ci * to detect. In the latter case it doesn't even *fail* reliably, so
13608c2ecf20Sopenharmony_ci * probing for it doesn't even work. Disable it completely on 32-bit
13618c2ecf20Sopenharmony_ci * unless we can find a reliable way to detect all the broken cases.
13628c2ecf20Sopenharmony_ci * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
13638c2ecf20Sopenharmony_ci */
13648c2ecf20Sopenharmony_cistatic void detect_nopl(void)
13658c2ecf20Sopenharmony_ci{
13668c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
13678c2ecf20Sopenharmony_ci	setup_clear_cpu_cap(X86_FEATURE_NOPL);
13688c2ecf20Sopenharmony_ci#else
13698c2ecf20Sopenharmony_ci	setup_force_cpu_cap(X86_FEATURE_NOPL);
13708c2ecf20Sopenharmony_ci#endif
13718c2ecf20Sopenharmony_ci}
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_ci/*
13748c2ecf20Sopenharmony_ci * We parse cpu parameters early because fpu__init_system() is executed
13758c2ecf20Sopenharmony_ci * before parse_early_param().
13768c2ecf20Sopenharmony_ci */
13778c2ecf20Sopenharmony_cistatic void __init cpu_parse_early_param(void)
13788c2ecf20Sopenharmony_ci{
13798c2ecf20Sopenharmony_ci	char arg[128];
13808c2ecf20Sopenharmony_ci	char *argptr = arg;
13818c2ecf20Sopenharmony_ci	int arglen, res, bit;
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
13848c2ecf20Sopenharmony_ci	if (cmdline_find_option_bool(boot_command_line, "no387"))
13858c2ecf20Sopenharmony_ci#ifdef CONFIG_MATH_EMULATION
13868c2ecf20Sopenharmony_ci		setup_clear_cpu_cap(X86_FEATURE_FPU);
13878c2ecf20Sopenharmony_ci#else
13888c2ecf20Sopenharmony_ci		pr_err("Option 'no387' required CONFIG_MATH_EMULATION enabled.\n");
13898c2ecf20Sopenharmony_ci#endif
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci	if (cmdline_find_option_bool(boot_command_line, "nofxsr"))
13928c2ecf20Sopenharmony_ci		setup_clear_cpu_cap(X86_FEATURE_FXSR);
13938c2ecf20Sopenharmony_ci#endif
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ci	if (cmdline_find_option_bool(boot_command_line, "noxsave"))
13968c2ecf20Sopenharmony_ci		setup_clear_cpu_cap(X86_FEATURE_XSAVE);
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci	if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
13998c2ecf20Sopenharmony_ci		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
14028c2ecf20Sopenharmony_ci		setup_clear_cpu_cap(X86_FEATURE_XSAVES);
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci	arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg));
14058c2ecf20Sopenharmony_ci	if (arglen <= 0)
14068c2ecf20Sopenharmony_ci		return;
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_ci	pr_info("Clearing CPUID bits:");
14098c2ecf20Sopenharmony_ci	do {
14108c2ecf20Sopenharmony_ci		res = get_option(&argptr, &bit);
14118c2ecf20Sopenharmony_ci		if (res == 0 || res == 3)
14128c2ecf20Sopenharmony_ci			break;
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci		/* If the argument was too long, the last bit may be cut off */
14158c2ecf20Sopenharmony_ci		if (res == 1 && arglen >= sizeof(arg))
14168c2ecf20Sopenharmony_ci			break;
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci		if (bit >= 0 && bit < NCAPINTS * 32) {
14198c2ecf20Sopenharmony_ci			pr_cont(" " X86_CAP_FMT, x86_cap_flag(bit));
14208c2ecf20Sopenharmony_ci			setup_clear_cpu_cap(bit);
14218c2ecf20Sopenharmony_ci		}
14228c2ecf20Sopenharmony_ci	} while (res == 2);
14238c2ecf20Sopenharmony_ci	pr_cont("\n");
14248c2ecf20Sopenharmony_ci}
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_ci/*
14278c2ecf20Sopenharmony_ci * Do minimum CPU detection early.
14288c2ecf20Sopenharmony_ci * Fields really needed: vendor, cpuid_level, family, model, mask,
14298c2ecf20Sopenharmony_ci * cache alignment.
14308c2ecf20Sopenharmony_ci * The others are not touched to avoid unwanted side effects.
14318c2ecf20Sopenharmony_ci *
14328c2ecf20Sopenharmony_ci * WARNING: this function is only called on the boot CPU.  Don't add code
14338c2ecf20Sopenharmony_ci * here that is supposed to run on all CPUs.
14348c2ecf20Sopenharmony_ci */
14358c2ecf20Sopenharmony_cistatic void __init early_identify_cpu(struct cpuinfo_x86 *c)
14368c2ecf20Sopenharmony_ci{
14378c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
14388c2ecf20Sopenharmony_ci	c->x86_clflush_size = 64;
14398c2ecf20Sopenharmony_ci	c->x86_phys_bits = 36;
14408c2ecf20Sopenharmony_ci	c->x86_virt_bits = 48;
14418c2ecf20Sopenharmony_ci#else
14428c2ecf20Sopenharmony_ci	c->x86_clflush_size = 32;
14438c2ecf20Sopenharmony_ci	c->x86_phys_bits = 32;
14448c2ecf20Sopenharmony_ci	c->x86_virt_bits = 32;
14458c2ecf20Sopenharmony_ci#endif
14468c2ecf20Sopenharmony_ci	c->x86_cache_alignment = c->x86_clflush_size;
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci	memset(&c->x86_capability, 0, sizeof(c->x86_capability));
14498c2ecf20Sopenharmony_ci	c->extended_cpuid_level = 0;
14508c2ecf20Sopenharmony_ci
14518c2ecf20Sopenharmony_ci	if (!have_cpuid_p())
14528c2ecf20Sopenharmony_ci		identify_cpu_without_cpuid(c);
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ci	/* cyrix could have cpuid enabled via c_identify()*/
14558c2ecf20Sopenharmony_ci	if (have_cpuid_p()) {
14568c2ecf20Sopenharmony_ci		cpu_detect(c);
14578c2ecf20Sopenharmony_ci		get_cpu_vendor(c);
14588c2ecf20Sopenharmony_ci		get_cpu_cap(c);
14598c2ecf20Sopenharmony_ci		get_cpu_address_sizes(c);
14608c2ecf20Sopenharmony_ci		setup_force_cpu_cap(X86_FEATURE_CPUID);
14618c2ecf20Sopenharmony_ci		cpu_parse_early_param();
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci		if (this_cpu->c_early_init)
14648c2ecf20Sopenharmony_ci			this_cpu->c_early_init(c);
14658c2ecf20Sopenharmony_ci
14668c2ecf20Sopenharmony_ci		c->cpu_index = 0;
14678c2ecf20Sopenharmony_ci		filter_cpuid_features(c, false);
14688c2ecf20Sopenharmony_ci
14698c2ecf20Sopenharmony_ci		if (this_cpu->c_bsp_init)
14708c2ecf20Sopenharmony_ci			this_cpu->c_bsp_init(c);
14718c2ecf20Sopenharmony_ci	} else {
14728c2ecf20Sopenharmony_ci		setup_clear_cpu_cap(X86_FEATURE_CPUID);
14738c2ecf20Sopenharmony_ci	}
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
14768c2ecf20Sopenharmony_ci
14778c2ecf20Sopenharmony_ci	cpu_set_bug_bits(c);
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ci	cpu_set_core_cap_bits(c);
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
14828c2ecf20Sopenharmony_ci	/*
14838c2ecf20Sopenharmony_ci	 * Regardless of whether PCID is enumerated, the SDM says
14848c2ecf20Sopenharmony_ci	 * that it can't be enabled in 32-bit mode.
14858c2ecf20Sopenharmony_ci	 */
14868c2ecf20Sopenharmony_ci	setup_clear_cpu_cap(X86_FEATURE_PCID);
14878c2ecf20Sopenharmony_ci#endif
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_ci	/*
14908c2ecf20Sopenharmony_ci	 * Later in the boot process pgtable_l5_enabled() relies on
14918c2ecf20Sopenharmony_ci	 * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not
14928c2ecf20Sopenharmony_ci	 * enabled by this point we need to clear the feature bit to avoid
14938c2ecf20Sopenharmony_ci	 * false-positives at the later stage.
14948c2ecf20Sopenharmony_ci	 *
14958c2ecf20Sopenharmony_ci	 * pgtable_l5_enabled() can be false here for several reasons:
14968c2ecf20Sopenharmony_ci	 *  - 5-level paging is disabled compile-time;
14978c2ecf20Sopenharmony_ci	 *  - it's 32-bit kernel;
14988c2ecf20Sopenharmony_ci	 *  - machine doesn't support 5-level paging;
14998c2ecf20Sopenharmony_ci	 *  - user specified 'no5lvl' in kernel command line.
15008c2ecf20Sopenharmony_ci	 */
15018c2ecf20Sopenharmony_ci	if (!pgtable_l5_enabled())
15028c2ecf20Sopenharmony_ci		setup_clear_cpu_cap(X86_FEATURE_LA57);
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_ci	detect_nopl();
15058c2ecf20Sopenharmony_ci}
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_civoid __init early_cpu_init(void)
15088c2ecf20Sopenharmony_ci{
15098c2ecf20Sopenharmony_ci	const struct cpu_dev *const *cdev;
15108c2ecf20Sopenharmony_ci	int count = 0;
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci#ifdef CONFIG_PROCESSOR_SELECT
15138c2ecf20Sopenharmony_ci	pr_info("KERNEL supported cpus:\n");
15148c2ecf20Sopenharmony_ci#endif
15158c2ecf20Sopenharmony_ci
15168c2ecf20Sopenharmony_ci	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
15178c2ecf20Sopenharmony_ci		const struct cpu_dev *cpudev = *cdev;
15188c2ecf20Sopenharmony_ci
15198c2ecf20Sopenharmony_ci		if (count >= X86_VENDOR_NUM)
15208c2ecf20Sopenharmony_ci			break;
15218c2ecf20Sopenharmony_ci		cpu_devs[count] = cpudev;
15228c2ecf20Sopenharmony_ci		count++;
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci#ifdef CONFIG_PROCESSOR_SELECT
15258c2ecf20Sopenharmony_ci		{
15268c2ecf20Sopenharmony_ci			unsigned int j;
15278c2ecf20Sopenharmony_ci
15288c2ecf20Sopenharmony_ci			for (j = 0; j < 2; j++) {
15298c2ecf20Sopenharmony_ci				if (!cpudev->c_ident[j])
15308c2ecf20Sopenharmony_ci					continue;
15318c2ecf20Sopenharmony_ci				pr_info("  %s %s\n", cpudev->c_vendor,
15328c2ecf20Sopenharmony_ci					cpudev->c_ident[j]);
15338c2ecf20Sopenharmony_ci			}
15348c2ecf20Sopenharmony_ci		}
15358c2ecf20Sopenharmony_ci#endif
15368c2ecf20Sopenharmony_ci	}
15378c2ecf20Sopenharmony_ci	early_identify_cpu(&boot_cpu_data);
15388c2ecf20Sopenharmony_ci}
15398c2ecf20Sopenharmony_ci
15408c2ecf20Sopenharmony_cistatic bool detect_null_seg_behavior(void)
15418c2ecf20Sopenharmony_ci{
15428c2ecf20Sopenharmony_ci	/*
15438c2ecf20Sopenharmony_ci	 * Empirically, writing zero to a segment selector on AMD does
15448c2ecf20Sopenharmony_ci	 * not clear the base, whereas writing zero to a segment
15458c2ecf20Sopenharmony_ci	 * selector on Intel does clear the base.  Intel's behavior
15468c2ecf20Sopenharmony_ci	 * allows slightly faster context switches in the common case
15478c2ecf20Sopenharmony_ci	 * where GS is unused by the prev and next threads.
15488c2ecf20Sopenharmony_ci	 *
15498c2ecf20Sopenharmony_ci	 * Since neither vendor documents this anywhere that I can see,
15508c2ecf20Sopenharmony_ci	 * detect it directly instead of hardcoding the choice by
15518c2ecf20Sopenharmony_ci	 * vendor.
15528c2ecf20Sopenharmony_ci	 *
15538c2ecf20Sopenharmony_ci	 * I've designated AMD's behavior as the "bug" because it's
15548c2ecf20Sopenharmony_ci	 * counterintuitive and less friendly.
15558c2ecf20Sopenharmony_ci	 */
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_ci	unsigned long old_base, tmp;
15588c2ecf20Sopenharmony_ci	rdmsrl(MSR_FS_BASE, old_base);
15598c2ecf20Sopenharmony_ci	wrmsrl(MSR_FS_BASE, 1);
15608c2ecf20Sopenharmony_ci	loadsegment(fs, 0);
15618c2ecf20Sopenharmony_ci	rdmsrl(MSR_FS_BASE, tmp);
15628c2ecf20Sopenharmony_ci	wrmsrl(MSR_FS_BASE, old_base);
15638c2ecf20Sopenharmony_ci	return tmp == 0;
15648c2ecf20Sopenharmony_ci}
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_civoid check_null_seg_clears_base(struct cpuinfo_x86 *c)
15678c2ecf20Sopenharmony_ci{
15688c2ecf20Sopenharmony_ci	/* BUG_NULL_SEG is only relevant with 64bit userspace */
15698c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_X86_64))
15708c2ecf20Sopenharmony_ci		return;
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	/* Zen3 CPUs advertise Null Selector Clears Base in CPUID. */
15738c2ecf20Sopenharmony_ci	if (c->extended_cpuid_level >= 0x80000021 &&
15748c2ecf20Sopenharmony_ci	    cpuid_eax(0x80000021) & BIT(6))
15758c2ecf20Sopenharmony_ci		return;
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci	/*
15788c2ecf20Sopenharmony_ci	 * CPUID bit above wasn't set. If this kernel is still running
15798c2ecf20Sopenharmony_ci	 * as a HV guest, then the HV has decided not to advertize
15808c2ecf20Sopenharmony_ci	 * that CPUID bit for whatever reason.	For example, one
15818c2ecf20Sopenharmony_ci	 * member of the migration pool might be vulnerable.  Which
15828c2ecf20Sopenharmony_ci	 * means, the bug is present: set the BUG flag and return.
15838c2ecf20Sopenharmony_ci	 */
15848c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_HYPERVISOR)) {
15858c2ecf20Sopenharmony_ci		set_cpu_bug(c, X86_BUG_NULL_SEG);
15868c2ecf20Sopenharmony_ci		return;
15878c2ecf20Sopenharmony_ci	}
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_ci	/*
15908c2ecf20Sopenharmony_ci	 * Zen2 CPUs also have this behaviour, but no CPUID bit.
15918c2ecf20Sopenharmony_ci	 * 0x18 is the respective family for Hygon.
15928c2ecf20Sopenharmony_ci	 */
15938c2ecf20Sopenharmony_ci	if ((c->x86 == 0x17 || c->x86 == 0x18) &&
15948c2ecf20Sopenharmony_ci	    detect_null_seg_behavior())
15958c2ecf20Sopenharmony_ci		return;
15968c2ecf20Sopenharmony_ci
15978c2ecf20Sopenharmony_ci	/* All the remaining ones are affected */
15988c2ecf20Sopenharmony_ci	set_cpu_bug(c, X86_BUG_NULL_SEG);
15998c2ecf20Sopenharmony_ci}
16008c2ecf20Sopenharmony_ci
16018c2ecf20Sopenharmony_cistatic void generic_identify(struct cpuinfo_x86 *c)
16028c2ecf20Sopenharmony_ci{
16038c2ecf20Sopenharmony_ci	c->extended_cpuid_level = 0;
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_ci	if (!have_cpuid_p())
16068c2ecf20Sopenharmony_ci		identify_cpu_without_cpuid(c);
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci	/* cyrix could have cpuid enabled via c_identify()*/
16098c2ecf20Sopenharmony_ci	if (!have_cpuid_p())
16108c2ecf20Sopenharmony_ci		return;
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci	cpu_detect(c);
16138c2ecf20Sopenharmony_ci
16148c2ecf20Sopenharmony_ci	get_cpu_vendor(c);
16158c2ecf20Sopenharmony_ci
16168c2ecf20Sopenharmony_ci	get_cpu_cap(c);
16178c2ecf20Sopenharmony_ci
16188c2ecf20Sopenharmony_ci	get_cpu_address_sizes(c);
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci	if (c->cpuid_level >= 0x00000001) {
16218c2ecf20Sopenharmony_ci		c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
16228c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
16238c2ecf20Sopenharmony_ci# ifdef CONFIG_SMP
16248c2ecf20Sopenharmony_ci		c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
16258c2ecf20Sopenharmony_ci# else
16268c2ecf20Sopenharmony_ci		c->apicid = c->initial_apicid;
16278c2ecf20Sopenharmony_ci# endif
16288c2ecf20Sopenharmony_ci#endif
16298c2ecf20Sopenharmony_ci		c->phys_proc_id = c->initial_apicid;
16308c2ecf20Sopenharmony_ci	}
16318c2ecf20Sopenharmony_ci
16328c2ecf20Sopenharmony_ci	get_model_name(c); /* Default name */
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci	/*
16358c2ecf20Sopenharmony_ci	 * ESPFIX is a strange bug.  All real CPUs have it.  Paravirt
16368c2ecf20Sopenharmony_ci	 * systems that run Linux at CPL > 0 may or may not have the
16378c2ecf20Sopenharmony_ci	 * issue, but, even if they have the issue, there's absolutely
16388c2ecf20Sopenharmony_ci	 * nothing we can do about it because we can't use the real IRET
16398c2ecf20Sopenharmony_ci	 * instruction.
16408c2ecf20Sopenharmony_ci	 *
16418c2ecf20Sopenharmony_ci	 * NB: For the time being, only 32-bit kernels support
16428c2ecf20Sopenharmony_ci	 * X86_BUG_ESPFIX as such.  64-bit kernels directly choose
16438c2ecf20Sopenharmony_ci	 * whether to apply espfix using paravirt hooks.  If any
16448c2ecf20Sopenharmony_ci	 * non-paravirt system ever shows up that does *not* have the
16458c2ecf20Sopenharmony_ci	 * ESPFIX issue, we can change this.
16468c2ecf20Sopenharmony_ci	 */
16478c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
16488c2ecf20Sopenharmony_ci	set_cpu_bug(c, X86_BUG_ESPFIX);
16498c2ecf20Sopenharmony_ci#endif
16508c2ecf20Sopenharmony_ci}
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci/*
16538c2ecf20Sopenharmony_ci * Validate that ACPI/mptables have the same information about the
16548c2ecf20Sopenharmony_ci * effective APIC id and update the package map.
16558c2ecf20Sopenharmony_ci */
16568c2ecf20Sopenharmony_cistatic void validate_apic_and_package_id(struct cpuinfo_x86 *c)
16578c2ecf20Sopenharmony_ci{
16588c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
16598c2ecf20Sopenharmony_ci	unsigned int apicid, cpu = smp_processor_id();
16608c2ecf20Sopenharmony_ci
16618c2ecf20Sopenharmony_ci	apicid = apic->cpu_present_to_apicid(cpu);
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_ci	if (apicid != c->apicid) {
16648c2ecf20Sopenharmony_ci		pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
16658c2ecf20Sopenharmony_ci		       cpu, apicid, c->initial_apicid);
16668c2ecf20Sopenharmony_ci	}
16678c2ecf20Sopenharmony_ci	BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
16688c2ecf20Sopenharmony_ci	BUG_ON(topology_update_die_map(c->cpu_die_id, cpu));
16698c2ecf20Sopenharmony_ci#else
16708c2ecf20Sopenharmony_ci	c->logical_proc_id = 0;
16718c2ecf20Sopenharmony_ci#endif
16728c2ecf20Sopenharmony_ci}
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci/*
16758c2ecf20Sopenharmony_ci * This does the hard work of actually picking apart the CPU stuff...
16768c2ecf20Sopenharmony_ci */
16778c2ecf20Sopenharmony_cistatic void identify_cpu(struct cpuinfo_x86 *c)
16788c2ecf20Sopenharmony_ci{
16798c2ecf20Sopenharmony_ci	int i;
16808c2ecf20Sopenharmony_ci
16818c2ecf20Sopenharmony_ci	c->loops_per_jiffy = loops_per_jiffy;
16828c2ecf20Sopenharmony_ci	c->x86_cache_size = 0;
16838c2ecf20Sopenharmony_ci	c->x86_vendor = X86_VENDOR_UNKNOWN;
16848c2ecf20Sopenharmony_ci	c->x86_model = c->x86_stepping = 0;	/* So far unknown... */
16858c2ecf20Sopenharmony_ci	c->x86_vendor_id[0] = '\0'; /* Unset */
16868c2ecf20Sopenharmony_ci	c->x86_model_id[0] = '\0';  /* Unset */
16878c2ecf20Sopenharmony_ci	c->x86_max_cores = 1;
16888c2ecf20Sopenharmony_ci	c->x86_coreid_bits = 0;
16898c2ecf20Sopenharmony_ci	c->cu_id = 0xff;
16908c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
16918c2ecf20Sopenharmony_ci	c->x86_clflush_size = 64;
16928c2ecf20Sopenharmony_ci	c->x86_phys_bits = 36;
16938c2ecf20Sopenharmony_ci	c->x86_virt_bits = 48;
16948c2ecf20Sopenharmony_ci#else
16958c2ecf20Sopenharmony_ci	c->cpuid_level = -1;	/* CPUID not detected */
16968c2ecf20Sopenharmony_ci	c->x86_clflush_size = 32;
16978c2ecf20Sopenharmony_ci	c->x86_phys_bits = 32;
16988c2ecf20Sopenharmony_ci	c->x86_virt_bits = 32;
16998c2ecf20Sopenharmony_ci#endif
17008c2ecf20Sopenharmony_ci	c->x86_cache_alignment = c->x86_clflush_size;
17018c2ecf20Sopenharmony_ci	memset(&c->x86_capability, 0, sizeof(c->x86_capability));
17028c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_VMX_FEATURE_NAMES
17038c2ecf20Sopenharmony_ci	memset(&c->vmx_capability, 0, sizeof(c->vmx_capability));
17048c2ecf20Sopenharmony_ci#endif
17058c2ecf20Sopenharmony_ci
17068c2ecf20Sopenharmony_ci	generic_identify(c);
17078c2ecf20Sopenharmony_ci
17088c2ecf20Sopenharmony_ci	if (this_cpu->c_identify)
17098c2ecf20Sopenharmony_ci		this_cpu->c_identify(c);
17108c2ecf20Sopenharmony_ci
17118c2ecf20Sopenharmony_ci	/* Clear/Set all flags overridden by options, after probe */
17128c2ecf20Sopenharmony_ci	apply_forced_caps(c);
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
17158c2ecf20Sopenharmony_ci	c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
17168c2ecf20Sopenharmony_ci#endif
17178c2ecf20Sopenharmony_ci
17188c2ecf20Sopenharmony_ci	/*
17198c2ecf20Sopenharmony_ci	 * Vendor-specific initialization.  In this section we
17208c2ecf20Sopenharmony_ci	 * canonicalize the feature flags, meaning if there are
17218c2ecf20Sopenharmony_ci	 * features a certain CPU supports which CPUID doesn't
17228c2ecf20Sopenharmony_ci	 * tell us, CPUID claiming incorrect flags, or other bugs,
17238c2ecf20Sopenharmony_ci	 * we handle them here.
17248c2ecf20Sopenharmony_ci	 *
17258c2ecf20Sopenharmony_ci	 * At the end of this section, c->x86_capability better
17268c2ecf20Sopenharmony_ci	 * indicate the features this CPU genuinely supports!
17278c2ecf20Sopenharmony_ci	 */
17288c2ecf20Sopenharmony_ci	if (this_cpu->c_init)
17298c2ecf20Sopenharmony_ci		this_cpu->c_init(c);
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_ci	/* Disable the PN if appropriate */
17328c2ecf20Sopenharmony_ci	squash_the_stupid_serial_number(c);
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_ci	/* Set up SMEP/SMAP/UMIP */
17358c2ecf20Sopenharmony_ci	setup_smep(c);
17368c2ecf20Sopenharmony_ci	setup_smap(c);
17378c2ecf20Sopenharmony_ci	setup_umip(c);
17388c2ecf20Sopenharmony_ci
17398c2ecf20Sopenharmony_ci	/* Enable FSGSBASE instructions if available. */
17408c2ecf20Sopenharmony_ci	if (cpu_has(c, X86_FEATURE_FSGSBASE)) {
17418c2ecf20Sopenharmony_ci		cr4_set_bits(X86_CR4_FSGSBASE);
17428c2ecf20Sopenharmony_ci		elf_hwcap2 |= HWCAP2_FSGSBASE;
17438c2ecf20Sopenharmony_ci	}
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_ci	/*
17468c2ecf20Sopenharmony_ci	 * The vendor-specific functions might have changed features.
17478c2ecf20Sopenharmony_ci	 * Now we do "generic changes."
17488c2ecf20Sopenharmony_ci	 */
17498c2ecf20Sopenharmony_ci
17508c2ecf20Sopenharmony_ci	/* Filter out anything that depends on CPUID levels we don't have */
17518c2ecf20Sopenharmony_ci	filter_cpuid_features(c, true);
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_ci	/* If the model name is still unset, do table lookup. */
17548c2ecf20Sopenharmony_ci	if (!c->x86_model_id[0]) {
17558c2ecf20Sopenharmony_ci		const char *p;
17568c2ecf20Sopenharmony_ci		p = table_lookup_model(c);
17578c2ecf20Sopenharmony_ci		if (p)
17588c2ecf20Sopenharmony_ci			strcpy(c->x86_model_id, p);
17598c2ecf20Sopenharmony_ci		else
17608c2ecf20Sopenharmony_ci			/* Last resort... */
17618c2ecf20Sopenharmony_ci			sprintf(c->x86_model_id, "%02x/%02x",
17628c2ecf20Sopenharmony_ci				c->x86, c->x86_model);
17638c2ecf20Sopenharmony_ci	}
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
17668c2ecf20Sopenharmony_ci	detect_ht(c);
17678c2ecf20Sopenharmony_ci#endif
17688c2ecf20Sopenharmony_ci
17698c2ecf20Sopenharmony_ci	x86_init_rdrand(c);
17708c2ecf20Sopenharmony_ci	setup_pku(c);
17718c2ecf20Sopenharmony_ci
17728c2ecf20Sopenharmony_ci	/*
17738c2ecf20Sopenharmony_ci	 * Clear/Set all flags overridden by options, need do it
17748c2ecf20Sopenharmony_ci	 * before following smp all cpus cap AND.
17758c2ecf20Sopenharmony_ci	 */
17768c2ecf20Sopenharmony_ci	apply_forced_caps(c);
17778c2ecf20Sopenharmony_ci
17788c2ecf20Sopenharmony_ci	/*
17798c2ecf20Sopenharmony_ci	 * On SMP, boot_cpu_data holds the common feature set between
17808c2ecf20Sopenharmony_ci	 * all CPUs; so make sure that we indicate which features are
17818c2ecf20Sopenharmony_ci	 * common between the CPUs.  The first time this routine gets
17828c2ecf20Sopenharmony_ci	 * executed, c == &boot_cpu_data.
17838c2ecf20Sopenharmony_ci	 */
17848c2ecf20Sopenharmony_ci	if (c != &boot_cpu_data) {
17858c2ecf20Sopenharmony_ci		/* AND the already accumulated flags with these */
17868c2ecf20Sopenharmony_ci		for (i = 0; i < NCAPINTS; i++)
17878c2ecf20Sopenharmony_ci			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
17888c2ecf20Sopenharmony_ci
17898c2ecf20Sopenharmony_ci		/* OR, i.e. replicate the bug flags */
17908c2ecf20Sopenharmony_ci		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
17918c2ecf20Sopenharmony_ci			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
17928c2ecf20Sopenharmony_ci	}
17938c2ecf20Sopenharmony_ci
17948c2ecf20Sopenharmony_ci	/* Init Machine Check Exception if available. */
17958c2ecf20Sopenharmony_ci	mcheck_cpu_init(c);
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_ci	select_idle_routine(c);
17988c2ecf20Sopenharmony_ci
17998c2ecf20Sopenharmony_ci#ifdef CONFIG_NUMA
18008c2ecf20Sopenharmony_ci	numa_add_cpu(smp_processor_id());
18018c2ecf20Sopenharmony_ci#endif
18028c2ecf20Sopenharmony_ci}
18038c2ecf20Sopenharmony_ci
18048c2ecf20Sopenharmony_ci/*
18058c2ecf20Sopenharmony_ci * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
18068c2ecf20Sopenharmony_ci * on 32-bit kernels:
18078c2ecf20Sopenharmony_ci */
18088c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
18098c2ecf20Sopenharmony_civoid enable_sep_cpu(void)
18108c2ecf20Sopenharmony_ci{
18118c2ecf20Sopenharmony_ci	struct tss_struct *tss;
18128c2ecf20Sopenharmony_ci	int cpu;
18138c2ecf20Sopenharmony_ci
18148c2ecf20Sopenharmony_ci	if (!boot_cpu_has(X86_FEATURE_SEP))
18158c2ecf20Sopenharmony_ci		return;
18168c2ecf20Sopenharmony_ci
18178c2ecf20Sopenharmony_ci	cpu = get_cpu();
18188c2ecf20Sopenharmony_ci	tss = &per_cpu(cpu_tss_rw, cpu);
18198c2ecf20Sopenharmony_ci
18208c2ecf20Sopenharmony_ci	/*
18218c2ecf20Sopenharmony_ci	 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
18228c2ecf20Sopenharmony_ci	 * see the big comment in struct x86_hw_tss's definition.
18238c2ecf20Sopenharmony_ci	 */
18248c2ecf20Sopenharmony_ci
18258c2ecf20Sopenharmony_ci	tss->x86_tss.ss1 = __KERNEL_CS;
18268c2ecf20Sopenharmony_ci	wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
18278c2ecf20Sopenharmony_ci	wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0);
18288c2ecf20Sopenharmony_ci	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	put_cpu();
18318c2ecf20Sopenharmony_ci}
18328c2ecf20Sopenharmony_ci#endif
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_civoid __init identify_boot_cpu(void)
18358c2ecf20Sopenharmony_ci{
18368c2ecf20Sopenharmony_ci	identify_cpu(&boot_cpu_data);
18378c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
18388c2ecf20Sopenharmony_ci	sysenter_setup();
18398c2ecf20Sopenharmony_ci	enable_sep_cpu();
18408c2ecf20Sopenharmony_ci#endif
18418c2ecf20Sopenharmony_ci	cpu_detect_tlb(&boot_cpu_data);
18428c2ecf20Sopenharmony_ci	setup_cr_pinning();
18438c2ecf20Sopenharmony_ci
18448c2ecf20Sopenharmony_ci	tsx_init();
18458c2ecf20Sopenharmony_ci}
18468c2ecf20Sopenharmony_ci
18478c2ecf20Sopenharmony_civoid identify_secondary_cpu(struct cpuinfo_x86 *c)
18488c2ecf20Sopenharmony_ci{
18498c2ecf20Sopenharmony_ci	BUG_ON(c == &boot_cpu_data);
18508c2ecf20Sopenharmony_ci	identify_cpu(c);
18518c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
18528c2ecf20Sopenharmony_ci	enable_sep_cpu();
18538c2ecf20Sopenharmony_ci#endif
18548c2ecf20Sopenharmony_ci	mtrr_ap_init();
18558c2ecf20Sopenharmony_ci	validate_apic_and_package_id(c);
18568c2ecf20Sopenharmony_ci	x86_spec_ctrl_setup_ap();
18578c2ecf20Sopenharmony_ci	update_srbds_msr();
18588c2ecf20Sopenharmony_ci	if (boot_cpu_has_bug(X86_BUG_GDS))
18598c2ecf20Sopenharmony_ci		update_gds_msr();
18608c2ecf20Sopenharmony_ci}
18618c2ecf20Sopenharmony_ci
18628c2ecf20Sopenharmony_cistatic __init int setup_noclflush(char *arg)
18638c2ecf20Sopenharmony_ci{
18648c2ecf20Sopenharmony_ci	setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
18658c2ecf20Sopenharmony_ci	setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
18668c2ecf20Sopenharmony_ci	return 1;
18678c2ecf20Sopenharmony_ci}
18688c2ecf20Sopenharmony_ci__setup("noclflush", setup_noclflush);
18698c2ecf20Sopenharmony_ci
18708c2ecf20Sopenharmony_civoid print_cpu_info(struct cpuinfo_x86 *c)
18718c2ecf20Sopenharmony_ci{
18728c2ecf20Sopenharmony_ci	const char *vendor = NULL;
18738c2ecf20Sopenharmony_ci
18748c2ecf20Sopenharmony_ci	if (c->x86_vendor < X86_VENDOR_NUM) {
18758c2ecf20Sopenharmony_ci		vendor = this_cpu->c_vendor;
18768c2ecf20Sopenharmony_ci	} else {
18778c2ecf20Sopenharmony_ci		if (c->cpuid_level >= 0)
18788c2ecf20Sopenharmony_ci			vendor = c->x86_vendor_id;
18798c2ecf20Sopenharmony_ci	}
18808c2ecf20Sopenharmony_ci
18818c2ecf20Sopenharmony_ci	if (vendor && !strstr(c->x86_model_id, vendor))
18828c2ecf20Sopenharmony_ci		pr_cont("%s ", vendor);
18838c2ecf20Sopenharmony_ci
18848c2ecf20Sopenharmony_ci	if (c->x86_model_id[0])
18858c2ecf20Sopenharmony_ci		pr_cont("%s", c->x86_model_id);
18868c2ecf20Sopenharmony_ci	else
18878c2ecf20Sopenharmony_ci		pr_cont("%d86", c->x86);
18888c2ecf20Sopenharmony_ci
18898c2ecf20Sopenharmony_ci	pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
18908c2ecf20Sopenharmony_ci
18918c2ecf20Sopenharmony_ci	if (c->x86_stepping || c->cpuid_level >= 0)
18928c2ecf20Sopenharmony_ci		pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
18938c2ecf20Sopenharmony_ci	else
18948c2ecf20Sopenharmony_ci		pr_cont(")\n");
18958c2ecf20Sopenharmony_ci}
18968c2ecf20Sopenharmony_ci
18978c2ecf20Sopenharmony_ci/*
18988c2ecf20Sopenharmony_ci * clearcpuid= was already parsed in fpu__init_parse_early_param.
18998c2ecf20Sopenharmony_ci * But we need to keep a dummy __setup around otherwise it would
19008c2ecf20Sopenharmony_ci * show up as an environment variable for init.
19018c2ecf20Sopenharmony_ci */
19028c2ecf20Sopenharmony_cistatic __init int setup_clearcpuid(char *arg)
19038c2ecf20Sopenharmony_ci{
19048c2ecf20Sopenharmony_ci	return 1;
19058c2ecf20Sopenharmony_ci}
19068c2ecf20Sopenharmony_ci__setup("clearcpuid=", setup_clearcpuid);
19078c2ecf20Sopenharmony_ci
19088c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
19098c2ecf20Sopenharmony_ciDEFINE_PER_CPU_FIRST(struct fixed_percpu_data,
19108c2ecf20Sopenharmony_ci		     fixed_percpu_data) __aligned(PAGE_SIZE) __visible;
19118c2ecf20Sopenharmony_ciEXPORT_PER_CPU_SYMBOL_GPL(fixed_percpu_data);
19128c2ecf20Sopenharmony_ci
19138c2ecf20Sopenharmony_ci/*
19148c2ecf20Sopenharmony_ci * The following percpu variables are hot.  Align current_task to
19158c2ecf20Sopenharmony_ci * cacheline size such that they fall in the same cacheline.
19168c2ecf20Sopenharmony_ci */
19178c2ecf20Sopenharmony_ciDEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
19188c2ecf20Sopenharmony_ci	&init_task;
19198c2ecf20Sopenharmony_ciEXPORT_PER_CPU_SYMBOL(current_task);
19208c2ecf20Sopenharmony_ci
19218c2ecf20Sopenharmony_ciDEFINE_PER_CPU(struct irq_stack *, hardirq_stack_ptr);
19228c2ecf20Sopenharmony_ciDEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
19238c2ecf20Sopenharmony_ci
19248c2ecf20Sopenharmony_ciDEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
19258c2ecf20Sopenharmony_ciEXPORT_PER_CPU_SYMBOL(__preempt_count);
19268c2ecf20Sopenharmony_ci
19278c2ecf20Sopenharmony_ci/* May not be marked __init: used by software suspend */
19288c2ecf20Sopenharmony_civoid syscall_init(void)
19298c2ecf20Sopenharmony_ci{
19308c2ecf20Sopenharmony_ci	wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
19318c2ecf20Sopenharmony_ci	wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
19328c2ecf20Sopenharmony_ci
19338c2ecf20Sopenharmony_ci#ifdef CONFIG_IA32_EMULATION
19348c2ecf20Sopenharmony_ci	wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
19358c2ecf20Sopenharmony_ci	/*
19368c2ecf20Sopenharmony_ci	 * This only works on Intel CPUs.
19378c2ecf20Sopenharmony_ci	 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
19388c2ecf20Sopenharmony_ci	 * This does not cause SYSENTER to jump to the wrong location, because
19398c2ecf20Sopenharmony_ci	 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
19408c2ecf20Sopenharmony_ci	 */
19418c2ecf20Sopenharmony_ci	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
19428c2ecf20Sopenharmony_ci	wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
19438c2ecf20Sopenharmony_ci		    (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1));
19448c2ecf20Sopenharmony_ci	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
19458c2ecf20Sopenharmony_ci#else
19468c2ecf20Sopenharmony_ci	wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
19478c2ecf20Sopenharmony_ci	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
19488c2ecf20Sopenharmony_ci	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
19498c2ecf20Sopenharmony_ci	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
19508c2ecf20Sopenharmony_ci#endif
19518c2ecf20Sopenharmony_ci
19528c2ecf20Sopenharmony_ci	/* Flags to clear on syscall */
19538c2ecf20Sopenharmony_ci	wrmsrl(MSR_SYSCALL_MASK,
19548c2ecf20Sopenharmony_ci	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
19558c2ecf20Sopenharmony_ci	       X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
19568c2ecf20Sopenharmony_ci}
19578c2ecf20Sopenharmony_ci
19588c2ecf20Sopenharmony_ci#else	/* CONFIG_X86_64 */
19598c2ecf20Sopenharmony_ci
19608c2ecf20Sopenharmony_ciDEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
19618c2ecf20Sopenharmony_ciEXPORT_PER_CPU_SYMBOL(current_task);
19628c2ecf20Sopenharmony_ciDEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
19638c2ecf20Sopenharmony_ciEXPORT_PER_CPU_SYMBOL(__preempt_count);
19648c2ecf20Sopenharmony_ci
19658c2ecf20Sopenharmony_ci/*
19668c2ecf20Sopenharmony_ci * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
19678c2ecf20Sopenharmony_ci * the top of the kernel stack.  Use an extra percpu variable to track the
19688c2ecf20Sopenharmony_ci * top of the kernel stack directly.
19698c2ecf20Sopenharmony_ci */
19708c2ecf20Sopenharmony_ciDEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
19718c2ecf20Sopenharmony_ci	(unsigned long)&init_thread_union + THREAD_SIZE;
19728c2ecf20Sopenharmony_ciEXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
19738c2ecf20Sopenharmony_ci
19748c2ecf20Sopenharmony_ci#ifdef CONFIG_STACKPROTECTOR
19758c2ecf20Sopenharmony_ciDEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
19768c2ecf20Sopenharmony_ci#endif
19778c2ecf20Sopenharmony_ci
19788c2ecf20Sopenharmony_ci#endif	/* CONFIG_X86_64 */
19798c2ecf20Sopenharmony_ci
19808c2ecf20Sopenharmony_ci/*
19818c2ecf20Sopenharmony_ci * Clear all 6 debug registers:
19828c2ecf20Sopenharmony_ci */
19838c2ecf20Sopenharmony_cistatic void clear_all_debug_regs(void)
19848c2ecf20Sopenharmony_ci{
19858c2ecf20Sopenharmony_ci	int i;
19868c2ecf20Sopenharmony_ci
19878c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++) {
19888c2ecf20Sopenharmony_ci		/* Ignore db4, db5 */
19898c2ecf20Sopenharmony_ci		if ((i == 4) || (i == 5))
19908c2ecf20Sopenharmony_ci			continue;
19918c2ecf20Sopenharmony_ci
19928c2ecf20Sopenharmony_ci		set_debugreg(0, i);
19938c2ecf20Sopenharmony_ci	}
19948c2ecf20Sopenharmony_ci}
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci#ifdef CONFIG_KGDB
19978c2ecf20Sopenharmony_ci/*
19988c2ecf20Sopenharmony_ci * Restore debug regs if using kgdbwait and you have a kernel debugger
19998c2ecf20Sopenharmony_ci * connection established.
20008c2ecf20Sopenharmony_ci */
20018c2ecf20Sopenharmony_cistatic void dbg_restore_debug_regs(void)
20028c2ecf20Sopenharmony_ci{
20038c2ecf20Sopenharmony_ci	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
20048c2ecf20Sopenharmony_ci		arch_kgdb_ops.correct_hw_break();
20058c2ecf20Sopenharmony_ci}
20068c2ecf20Sopenharmony_ci#else /* ! CONFIG_KGDB */
20078c2ecf20Sopenharmony_ci#define dbg_restore_debug_regs()
20088c2ecf20Sopenharmony_ci#endif /* ! CONFIG_KGDB */
20098c2ecf20Sopenharmony_ci
20108c2ecf20Sopenharmony_cistatic void wait_for_master_cpu(int cpu)
20118c2ecf20Sopenharmony_ci{
20128c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
20138c2ecf20Sopenharmony_ci	/*
20148c2ecf20Sopenharmony_ci	 * wait for ACK from master CPU before continuing
20158c2ecf20Sopenharmony_ci	 * with AP initialization
20168c2ecf20Sopenharmony_ci	 */
20178c2ecf20Sopenharmony_ci	WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
20188c2ecf20Sopenharmony_ci	while (!cpumask_test_cpu(cpu, cpu_callout_mask))
20198c2ecf20Sopenharmony_ci		cpu_relax();
20208c2ecf20Sopenharmony_ci#endif
20218c2ecf20Sopenharmony_ci}
20228c2ecf20Sopenharmony_ci
20238c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
20248c2ecf20Sopenharmony_cistatic inline void setup_getcpu(int cpu)
20258c2ecf20Sopenharmony_ci{
20268c2ecf20Sopenharmony_ci	unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
20278c2ecf20Sopenharmony_ci	struct desc_struct d = { };
20288c2ecf20Sopenharmony_ci
20298c2ecf20Sopenharmony_ci	if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID))
20308c2ecf20Sopenharmony_ci		write_rdtscp_aux(cpudata);
20318c2ecf20Sopenharmony_ci
20328c2ecf20Sopenharmony_ci	/* Store CPU and node number in limit. */
20338c2ecf20Sopenharmony_ci	d.limit0 = cpudata;
20348c2ecf20Sopenharmony_ci	d.limit1 = cpudata >> 16;
20358c2ecf20Sopenharmony_ci
20368c2ecf20Sopenharmony_ci	d.type = 5;		/* RO data, expand down, accessed */
20378c2ecf20Sopenharmony_ci	d.dpl = 3;		/* Visible to user code */
20388c2ecf20Sopenharmony_ci	d.s = 1;		/* Not a system segment */
20398c2ecf20Sopenharmony_ci	d.p = 1;		/* Present */
20408c2ecf20Sopenharmony_ci	d.d = 1;		/* 32-bit */
20418c2ecf20Sopenharmony_ci
20428c2ecf20Sopenharmony_ci	write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S);
20438c2ecf20Sopenharmony_ci}
20448c2ecf20Sopenharmony_ci
20458c2ecf20Sopenharmony_cistatic inline void ucode_cpu_init(int cpu)
20468c2ecf20Sopenharmony_ci{
20478c2ecf20Sopenharmony_ci	if (cpu)
20488c2ecf20Sopenharmony_ci		load_ucode_ap();
20498c2ecf20Sopenharmony_ci}
20508c2ecf20Sopenharmony_ci
20518c2ecf20Sopenharmony_cistatic inline void tss_setup_ist(struct tss_struct *tss)
20528c2ecf20Sopenharmony_ci{
20538c2ecf20Sopenharmony_ci	/* Set up the per-CPU TSS IST stacks */
20548c2ecf20Sopenharmony_ci	tss->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
20558c2ecf20Sopenharmony_ci	tss->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
20568c2ecf20Sopenharmony_ci	tss->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
20578c2ecf20Sopenharmony_ci	tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
20588c2ecf20Sopenharmony_ci	/* Only mapped when SEV-ES is active */
20598c2ecf20Sopenharmony_ci	tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC);
20608c2ecf20Sopenharmony_ci}
20618c2ecf20Sopenharmony_ci
20628c2ecf20Sopenharmony_ci#else /* CONFIG_X86_64 */
20638c2ecf20Sopenharmony_ci
20648c2ecf20Sopenharmony_cistatic inline void setup_getcpu(int cpu) { }
20658c2ecf20Sopenharmony_ci
20668c2ecf20Sopenharmony_cistatic inline void ucode_cpu_init(int cpu)
20678c2ecf20Sopenharmony_ci{
20688c2ecf20Sopenharmony_ci	show_ucode_info_early();
20698c2ecf20Sopenharmony_ci}
20708c2ecf20Sopenharmony_ci
20718c2ecf20Sopenharmony_cistatic inline void tss_setup_ist(struct tss_struct *tss) { }
20728c2ecf20Sopenharmony_ci
20738c2ecf20Sopenharmony_ci#endif /* !CONFIG_X86_64 */
20748c2ecf20Sopenharmony_ci
20758c2ecf20Sopenharmony_cistatic inline void tss_setup_io_bitmap(struct tss_struct *tss)
20768c2ecf20Sopenharmony_ci{
20778c2ecf20Sopenharmony_ci	tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID;
20788c2ecf20Sopenharmony_ci
20798c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_IOPL_IOPERM
20808c2ecf20Sopenharmony_ci	tss->io_bitmap.prev_max = 0;
20818c2ecf20Sopenharmony_ci	tss->io_bitmap.prev_sequence = 0;
20828c2ecf20Sopenharmony_ci	memset(tss->io_bitmap.bitmap, 0xff, sizeof(tss->io_bitmap.bitmap));
20838c2ecf20Sopenharmony_ci	/*
20848c2ecf20Sopenharmony_ci	 * Invalidate the extra array entry past the end of the all
20858c2ecf20Sopenharmony_ci	 * permission bitmap as required by the hardware.
20868c2ecf20Sopenharmony_ci	 */
20878c2ecf20Sopenharmony_ci	tss->io_bitmap.mapall[IO_BITMAP_LONGS] = ~0UL;
20888c2ecf20Sopenharmony_ci#endif
20898c2ecf20Sopenharmony_ci}
20908c2ecf20Sopenharmony_ci
20918c2ecf20Sopenharmony_ci/*
20928c2ecf20Sopenharmony_ci * Setup everything needed to handle exceptions from the IDT, including the IST
20938c2ecf20Sopenharmony_ci * exceptions which use paranoid_entry().
20948c2ecf20Sopenharmony_ci */
20958c2ecf20Sopenharmony_civoid cpu_init_exception_handling(void)
20968c2ecf20Sopenharmony_ci{
20978c2ecf20Sopenharmony_ci	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
20988c2ecf20Sopenharmony_ci	int cpu = raw_smp_processor_id();
20998c2ecf20Sopenharmony_ci
21008c2ecf20Sopenharmony_ci	/* paranoid_entry() gets the CPU number from the GDT */
21018c2ecf20Sopenharmony_ci	setup_getcpu(cpu);
21028c2ecf20Sopenharmony_ci
21038c2ecf20Sopenharmony_ci	/* IST vectors need TSS to be set up. */
21048c2ecf20Sopenharmony_ci	tss_setup_ist(tss);
21058c2ecf20Sopenharmony_ci	tss_setup_io_bitmap(tss);
21068c2ecf20Sopenharmony_ci	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
21078c2ecf20Sopenharmony_ci
21088c2ecf20Sopenharmony_ci	load_TR_desc();
21098c2ecf20Sopenharmony_ci
21108c2ecf20Sopenharmony_ci	/* Finally load the IDT */
21118c2ecf20Sopenharmony_ci	load_current_idt();
21128c2ecf20Sopenharmony_ci}
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ci/*
21158c2ecf20Sopenharmony_ci * cpu_init() initializes state that is per-CPU. Some data is already
21168c2ecf20Sopenharmony_ci * initialized (naturally) in the bootstrap process, such as the GDT.  We
21178c2ecf20Sopenharmony_ci * reload it nevertheless, this function acts as a 'CPU state barrier',
21188c2ecf20Sopenharmony_ci * nothing should get across.
21198c2ecf20Sopenharmony_ci */
21208c2ecf20Sopenharmony_civoid cpu_init(void)
21218c2ecf20Sopenharmony_ci{
21228c2ecf20Sopenharmony_ci	struct task_struct *cur = current;
21238c2ecf20Sopenharmony_ci	int cpu = raw_smp_processor_id();
21248c2ecf20Sopenharmony_ci
21258c2ecf20Sopenharmony_ci	wait_for_master_cpu(cpu);
21268c2ecf20Sopenharmony_ci
21278c2ecf20Sopenharmony_ci	ucode_cpu_init(cpu);
21288c2ecf20Sopenharmony_ci
21298c2ecf20Sopenharmony_ci#ifdef CONFIG_NUMA
21308c2ecf20Sopenharmony_ci	if (this_cpu_read(numa_node) == 0 &&
21318c2ecf20Sopenharmony_ci	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
21328c2ecf20Sopenharmony_ci		set_numa_node(early_cpu_to_node(cpu));
21338c2ecf20Sopenharmony_ci#endif
21348c2ecf20Sopenharmony_ci	pr_debug("Initializing CPU#%d\n", cpu);
21358c2ecf20Sopenharmony_ci
21368c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_X86_64) || cpu_feature_enabled(X86_FEATURE_VME) ||
21378c2ecf20Sopenharmony_ci	    boot_cpu_has(X86_FEATURE_TSC) || boot_cpu_has(X86_FEATURE_DE))
21388c2ecf20Sopenharmony_ci		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
21398c2ecf20Sopenharmony_ci
21408c2ecf20Sopenharmony_ci	/*
21418c2ecf20Sopenharmony_ci	 * Initialize the per-CPU GDT with the boot GDT,
21428c2ecf20Sopenharmony_ci	 * and set up the GDT descriptor:
21438c2ecf20Sopenharmony_ci	 */
21448c2ecf20Sopenharmony_ci	switch_to_new_gdt(cpu);
21458c2ecf20Sopenharmony_ci
21468c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_X86_64)) {
21478c2ecf20Sopenharmony_ci		loadsegment(fs, 0);
21488c2ecf20Sopenharmony_ci		memset(cur->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
21498c2ecf20Sopenharmony_ci		syscall_init();
21508c2ecf20Sopenharmony_ci
21518c2ecf20Sopenharmony_ci		wrmsrl(MSR_FS_BASE, 0);
21528c2ecf20Sopenharmony_ci		wrmsrl(MSR_KERNEL_GS_BASE, 0);
21538c2ecf20Sopenharmony_ci		barrier();
21548c2ecf20Sopenharmony_ci
21558c2ecf20Sopenharmony_ci		x2apic_setup();
21568c2ecf20Sopenharmony_ci	}
21578c2ecf20Sopenharmony_ci
21588c2ecf20Sopenharmony_ci	mmgrab(&init_mm);
21598c2ecf20Sopenharmony_ci	cur->active_mm = &init_mm;
21608c2ecf20Sopenharmony_ci	BUG_ON(cur->mm);
21618c2ecf20Sopenharmony_ci	initialize_tlbstate_and_flush();
21628c2ecf20Sopenharmony_ci	enter_lazy_tlb(&init_mm, cur);
21638c2ecf20Sopenharmony_ci
21648c2ecf20Sopenharmony_ci	/*
21658c2ecf20Sopenharmony_ci	 * sp0 points to the entry trampoline stack regardless of what task
21668c2ecf20Sopenharmony_ci	 * is running.
21678c2ecf20Sopenharmony_ci	 */
21688c2ecf20Sopenharmony_ci	load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
21698c2ecf20Sopenharmony_ci
21708c2ecf20Sopenharmony_ci	load_mm_ldt(&init_mm);
21718c2ecf20Sopenharmony_ci
21728c2ecf20Sopenharmony_ci	clear_all_debug_regs();
21738c2ecf20Sopenharmony_ci	dbg_restore_debug_regs();
21748c2ecf20Sopenharmony_ci
21758c2ecf20Sopenharmony_ci	doublefault_init_cpu_tss();
21768c2ecf20Sopenharmony_ci
21778c2ecf20Sopenharmony_ci	if (is_uv_system())
21788c2ecf20Sopenharmony_ci		uv_cpu_init();
21798c2ecf20Sopenharmony_ci
21808c2ecf20Sopenharmony_ci	load_fixmap_gdt(cpu);
21818c2ecf20Sopenharmony_ci}
21828c2ecf20Sopenharmony_ci
21838c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
21848c2ecf20Sopenharmony_civoid cpu_init_secondary(void)
21858c2ecf20Sopenharmony_ci{
21868c2ecf20Sopenharmony_ci	/*
21878c2ecf20Sopenharmony_ci	 * Relies on the BP having set-up the IDT tables, which are loaded
21888c2ecf20Sopenharmony_ci	 * on this CPU in cpu_init_exception_handling().
21898c2ecf20Sopenharmony_ci	 */
21908c2ecf20Sopenharmony_ci	cpu_init_exception_handling();
21918c2ecf20Sopenharmony_ci	cpu_init();
21928c2ecf20Sopenharmony_ci	fpu__init_cpu();
21938c2ecf20Sopenharmony_ci}
21948c2ecf20Sopenharmony_ci#endif
21958c2ecf20Sopenharmony_ci
21968c2ecf20Sopenharmony_ci#ifdef CONFIG_MICROCODE_LATE_LOADING
21978c2ecf20Sopenharmony_ci/**
21988c2ecf20Sopenharmony_ci * store_cpu_caps() - Store a snapshot of CPU capabilities
21998c2ecf20Sopenharmony_ci * @curr_info: Pointer where to store it
22008c2ecf20Sopenharmony_ci *
22018c2ecf20Sopenharmony_ci * Returns: None
22028c2ecf20Sopenharmony_ci */
22038c2ecf20Sopenharmony_civoid store_cpu_caps(struct cpuinfo_x86 *curr_info)
22048c2ecf20Sopenharmony_ci{
22058c2ecf20Sopenharmony_ci	/* Reload CPUID max function as it might've changed. */
22068c2ecf20Sopenharmony_ci	curr_info->cpuid_level = cpuid_eax(0);
22078c2ecf20Sopenharmony_ci
22088c2ecf20Sopenharmony_ci	/* Copy all capability leafs and pick up the synthetic ones. */
22098c2ecf20Sopenharmony_ci	memcpy(&curr_info->x86_capability, &boot_cpu_data.x86_capability,
22108c2ecf20Sopenharmony_ci	       sizeof(curr_info->x86_capability));
22118c2ecf20Sopenharmony_ci
22128c2ecf20Sopenharmony_ci	/* Get the hardware CPUID leafs */
22138c2ecf20Sopenharmony_ci	get_cpu_cap(curr_info);
22148c2ecf20Sopenharmony_ci}
22158c2ecf20Sopenharmony_ci
22168c2ecf20Sopenharmony_ci/**
22178c2ecf20Sopenharmony_ci * microcode_check() - Check if any CPU capabilities changed after an update.
22188c2ecf20Sopenharmony_ci * @prev_info:	CPU capabilities stored before an update.
22198c2ecf20Sopenharmony_ci *
22208c2ecf20Sopenharmony_ci * The microcode loader calls this upon late microcode load to recheck features,
22218c2ecf20Sopenharmony_ci * only when microcode has been updated. Caller holds microcode_mutex and CPU
22228c2ecf20Sopenharmony_ci * hotplug lock.
22238c2ecf20Sopenharmony_ci *
22248c2ecf20Sopenharmony_ci * Return: None
22258c2ecf20Sopenharmony_ci */
22268c2ecf20Sopenharmony_civoid microcode_check(struct cpuinfo_x86 *prev_info)
22278c2ecf20Sopenharmony_ci{
22288c2ecf20Sopenharmony_ci	struct cpuinfo_x86 curr_info;
22298c2ecf20Sopenharmony_ci
22308c2ecf20Sopenharmony_ci	perf_check_microcode();
22318c2ecf20Sopenharmony_ci
22328c2ecf20Sopenharmony_ci	amd_check_microcode();
22338c2ecf20Sopenharmony_ci
22348c2ecf20Sopenharmony_ci	store_cpu_caps(&curr_info);
22358c2ecf20Sopenharmony_ci
22368c2ecf20Sopenharmony_ci	if (!memcmp(&prev_info->x86_capability, &curr_info.x86_capability,
22378c2ecf20Sopenharmony_ci		    sizeof(prev_info->x86_capability)))
22388c2ecf20Sopenharmony_ci		return;
22398c2ecf20Sopenharmony_ci
22408c2ecf20Sopenharmony_ci	pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
22418c2ecf20Sopenharmony_ci	pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
22428c2ecf20Sopenharmony_ci}
22438c2ecf20Sopenharmony_ci#endif
22448c2ecf20Sopenharmony_ci
22458c2ecf20Sopenharmony_ci/*
22468c2ecf20Sopenharmony_ci * Invoked from core CPU hotplug code after hotplug operations
22478c2ecf20Sopenharmony_ci */
22488c2ecf20Sopenharmony_civoid arch_smt_update(void)
22498c2ecf20Sopenharmony_ci{
22508c2ecf20Sopenharmony_ci	/* Handle the speculative execution misfeatures */
22518c2ecf20Sopenharmony_ci	cpu_bugs_smt_update();
22528c2ecf20Sopenharmony_ci	/* Check whether IPI broadcasting can be enabled */
22538c2ecf20Sopenharmony_ci	apic_smt_update();
22548c2ecf20Sopenharmony_ci}
22558c2ecf20Sopenharmony_ci
22568c2ecf20Sopenharmony_civoid __init arch_cpu_finalize_init(void)
22578c2ecf20Sopenharmony_ci{
22588c2ecf20Sopenharmony_ci	identify_boot_cpu();
22598c2ecf20Sopenharmony_ci
22608c2ecf20Sopenharmony_ci	/*
22618c2ecf20Sopenharmony_ci	 * identify_boot_cpu() initialized SMT support information, let the
22628c2ecf20Sopenharmony_ci	 * core code know.
22638c2ecf20Sopenharmony_ci	 */
22648c2ecf20Sopenharmony_ci	cpu_smt_check_topology();
22658c2ecf20Sopenharmony_ci
22668c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_SMP)) {
22678c2ecf20Sopenharmony_ci		pr_info("CPU: ");
22688c2ecf20Sopenharmony_ci		print_cpu_info(&boot_cpu_data);
22698c2ecf20Sopenharmony_ci	}
22708c2ecf20Sopenharmony_ci
22718c2ecf20Sopenharmony_ci	cpu_select_mitigations();
22728c2ecf20Sopenharmony_ci
22738c2ecf20Sopenharmony_ci	arch_smt_update();
22748c2ecf20Sopenharmony_ci
22758c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_X86_32)) {
22768c2ecf20Sopenharmony_ci		/*
22778c2ecf20Sopenharmony_ci		 * Check whether this is a real i386 which is not longer
22788c2ecf20Sopenharmony_ci		 * supported and fixup the utsname.
22798c2ecf20Sopenharmony_ci		 */
22808c2ecf20Sopenharmony_ci		if (boot_cpu_data.x86 < 4)
22818c2ecf20Sopenharmony_ci			panic("Kernel requires i486+ for 'invlpg' and other features");
22828c2ecf20Sopenharmony_ci
22838c2ecf20Sopenharmony_ci		init_utsname()->machine[1] =
22848c2ecf20Sopenharmony_ci			'0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
22858c2ecf20Sopenharmony_ci	}
22868c2ecf20Sopenharmony_ci
22878c2ecf20Sopenharmony_ci	/*
22888c2ecf20Sopenharmony_ci	 * Must be before alternatives because it might set or clear
22898c2ecf20Sopenharmony_ci	 * feature bits.
22908c2ecf20Sopenharmony_ci	 */
22918c2ecf20Sopenharmony_ci	fpu__init_system();
22928c2ecf20Sopenharmony_ci	fpu__init_cpu();
22938c2ecf20Sopenharmony_ci
22948c2ecf20Sopenharmony_ci	alternative_instructions();
22958c2ecf20Sopenharmony_ci
22968c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_X86_64)) {
22978c2ecf20Sopenharmony_ci		/*
22988c2ecf20Sopenharmony_ci		 * Make sure the first 2MB area is not mapped by huge pages
22998c2ecf20Sopenharmony_ci		 * There are typically fixed size MTRRs in there and overlapping
23008c2ecf20Sopenharmony_ci		 * MTRRs into large pages causes slow downs.
23018c2ecf20Sopenharmony_ci		 *
23028c2ecf20Sopenharmony_ci		 * Right now we don't do that with gbpages because there seems
23038c2ecf20Sopenharmony_ci		 * very little benefit for that case.
23048c2ecf20Sopenharmony_ci		 */
23058c2ecf20Sopenharmony_ci		if (!direct_gbpages)
23068c2ecf20Sopenharmony_ci			set_memory_4k((unsigned long)__va(0), 1);
23078c2ecf20Sopenharmony_ci	} else {
23088c2ecf20Sopenharmony_ci		fpu__init_check_bugs();
23098c2ecf20Sopenharmony_ci	}
23108c2ecf20Sopenharmony_ci
23118c2ecf20Sopenharmony_ci	/*
23128c2ecf20Sopenharmony_ci	 * This needs to be called before any devices perform DMA
23138c2ecf20Sopenharmony_ci	 * operations that might use the SWIOTLB bounce buffers. It will
23148c2ecf20Sopenharmony_ci	 * mark the bounce buffers as decrypted so that their usage will
23158c2ecf20Sopenharmony_ci	 * not cause "plain-text" data to be decrypted when accessed. It
23168c2ecf20Sopenharmony_ci	 * must be called after late_time_init() so that Hyper-V x86/x64
23178c2ecf20Sopenharmony_ci	 * hypercalls work when the SWIOTLB bounce buffers are decrypted.
23188c2ecf20Sopenharmony_ci	 */
23198c2ecf20Sopenharmony_ci	mem_encrypt_init();
23208c2ecf20Sopenharmony_ci}
2321