xref: /linux/arch/x86/kernel/cpu/common.c (revision f49ecf5e110ab0ed255ddea5e321689faf4e50e6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* cpu_feature_enabled() cannot be used this early */
3 #define USE_EARLY_PGTABLE_L5
4 
5 #include <linux/memblock.h>
6 #include <linux/linkage.h>
7 #include <linux/bitops.h>
8 #include <linux/kernel.h>
9 #include <linux/export.h>
10 #include <linux/kvm_types.h>
11 #include <linux/percpu.h>
12 #include <linux/string.h>
13 #include <linux/ctype.h>
14 #include <linux/delay.h>
15 #include <linux/sched/mm.h>
16 #include <linux/sched/clock.h>
17 #include <linux/sched/task.h>
18 #include <linux/sched/smt.h>
19 #include <linux/init.h>
20 #include <linux/kprobes.h>
21 #include <linux/kgdb.h>
22 #include <linux/mem_encrypt.h>
23 #include <linux/smp.h>
24 #include <linux/cpu.h>
25 #include <linux/io.h>
26 #include <linux/syscore_ops.h>
27 #include <linux/pgtable.h>
28 #include <linux/stackprotector.h>
29 #include <linux/utsname.h>
30 #include <linux/efi.h>
31 
32 #include <asm/alternative.h>
33 #include <asm/cmdline.h>
34 #include <asm/cpuid/api.h>
35 #include <asm/perf_event.h>
36 #include <asm/mmu_context.h>
37 #include <asm/doublefault.h>
38 #include <asm/archrandom.h>
39 #include <asm/hypervisor.h>
40 #include <asm/processor.h>
41 #include <asm/tlbflush.h>
42 #include <asm/debugreg.h>
43 #include <asm/sections.h>
44 #include <asm/vsyscall.h>
45 #include <linux/topology.h>
46 #include <linux/cpumask.h>
47 #include <linux/atomic.h>
48 #include <asm/proto.h>
49 #include <asm/setup.h>
50 #include <asm/apic.h>
51 #include <asm/desc.h>
52 #include <asm/fpu/api.h>
53 #include <asm/mtrr.h>
54 #include <asm/hwcap2.h>
55 #include <linux/numa.h>
56 #include <asm/numa.h>
57 #include <asm/asm.h>
58 #include <asm/bugs.h>
59 #include <asm/cpu.h>
60 #include <asm/mce.h>
61 #include <asm/msr.h>
62 #include <asm/cacheinfo.h>
63 #include <asm/memtype.h>
64 #include <asm/microcode.h>
65 #include <asm/intel-family.h>
66 #include <asm/cpu_device_id.h>
67 #include <asm/fred.h>
68 #include <asm/uv/uv.h>
69 #include <asm/ia32.h>
70 #include <asm/set_memory.h>
71 #include <asm/traps.h>
72 #include <asm/sev.h>
73 #include <asm/tdx.h>
74 #include <asm/posted_intr.h>
75 #include <asm/runtime-const.h>
76 
77 #include "cpu.h"
78 
79 DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
80 EXPORT_PER_CPU_SYMBOL(cpu_info);
81 
82 /* Used for modules: built-in code uses runtime constants */
83 unsigned long USER_PTR_MAX;
84 EXPORT_SYMBOL(USER_PTR_MAX);
85 
86 u32 elf_hwcap2 __read_mostly;
87 
88 /* Number of siblings per CPU package */
89 unsigned int __max_threads_per_core __ro_after_init = 1;
90 EXPORT_SYMBOL(__max_threads_per_core);
91 
92 unsigned int __max_dies_per_package __ro_after_init = 1;
93 EXPORT_SYMBOL(__max_dies_per_package);
94 
95 unsigned int __max_logical_packages __ro_after_init = 1;
96 EXPORT_SYMBOL(__max_logical_packages);
97 
98 unsigned int __num_cores_per_package __ro_after_init = 1;
99 EXPORT_SYMBOL(__num_cores_per_package);
100 
101 unsigned int __num_threads_per_package __ro_after_init = 1;
102 EXPORT_SYMBOL(__num_threads_per_package);
103 
104 static struct ppin_info {
105 	int	feature;
106 	int	msr_ppin_ctl;
107 	int	msr_ppin;
108 } ppin_info[] = {
109 	[X86_VENDOR_INTEL] = {
110 		.feature = X86_FEATURE_INTEL_PPIN,
111 		.msr_ppin_ctl = MSR_PPIN_CTL,
112 		.msr_ppin = MSR_PPIN
113 	},
114 	[X86_VENDOR_AMD] = {
115 		.feature = X86_FEATURE_AMD_PPIN,
116 		.msr_ppin_ctl = MSR_AMD_PPIN_CTL,
117 		.msr_ppin = MSR_AMD_PPIN
118 	},
119 };
120 
121 static const struct x86_cpu_id ppin_cpuids[] = {
122 	X86_MATCH_FEATURE(X86_FEATURE_AMD_PPIN, &ppin_info[X86_VENDOR_AMD]),
123 	X86_MATCH_FEATURE(X86_FEATURE_INTEL_PPIN, &ppin_info[X86_VENDOR_INTEL]),
124 
125 	/* Legacy models without CPUID enumeration */
126 	X86_MATCH_VFM(INTEL_IVYBRIDGE_X, &ppin_info[X86_VENDOR_INTEL]),
127 	X86_MATCH_VFM(INTEL_HASWELL_X, &ppin_info[X86_VENDOR_INTEL]),
128 	X86_MATCH_VFM(INTEL_BROADWELL_D, &ppin_info[X86_VENDOR_INTEL]),
129 	X86_MATCH_VFM(INTEL_BROADWELL_X, &ppin_info[X86_VENDOR_INTEL]),
130 	X86_MATCH_VFM(INTEL_SKYLAKE_X, &ppin_info[X86_VENDOR_INTEL]),
131 	X86_MATCH_VFM(INTEL_ICELAKE_X, &ppin_info[X86_VENDOR_INTEL]),
132 	X86_MATCH_VFM(INTEL_ICELAKE_D, &ppin_info[X86_VENDOR_INTEL]),
133 	X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X, &ppin_info[X86_VENDOR_INTEL]),
134 	X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X, &ppin_info[X86_VENDOR_INTEL]),
135 	X86_MATCH_VFM(INTEL_XEON_PHI_KNL, &ppin_info[X86_VENDOR_INTEL]),
136 	X86_MATCH_VFM(INTEL_XEON_PHI_KNM, &ppin_info[X86_VENDOR_INTEL]),
137 
138 	{}
139 };
140 
141 static void ppin_init(struct cpuinfo_x86 *c)
142 {
143 	const struct x86_cpu_id *id;
144 	unsigned long long val;
145 	struct ppin_info *info;
146 
147 	id = x86_match_cpu(ppin_cpuids);
148 	if (!id)
149 		return;
150 
151 	/*
152 	 * Testing the presence of the MSR is not enough. Need to check
153 	 * that the PPIN_CTL allows reading of the PPIN.
154 	 */
155 	info = (struct ppin_info *)id->driver_data;
156 
157 	if (rdmsrq_safe(info->msr_ppin_ctl, &val))
158 		goto clear_ppin;
159 
160 	if ((val & 3UL) == 1UL) {
161 		/* PPIN locked in disabled mode */
162 		goto clear_ppin;
163 	}
164 
165 	/* If PPIN is disabled, try to enable */
166 	if (!(val & 2UL)) {
167 		wrmsrq_safe(info->msr_ppin_ctl,  val | 2UL);
168 		rdmsrq_safe(info->msr_ppin_ctl, &val);
169 	}
170 
171 	/* Is the enable bit set? */
172 	if (val & 2UL) {
173 		c->ppin = native_rdmsrq(info->msr_ppin);
174 		set_cpu_cap(c, info->feature);
175 		return;
176 	}
177 
178 clear_ppin:
179 	setup_clear_cpu_cap(info->feature);
180 }
181 
182 static void default_init(struct cpuinfo_x86 *c)
183 {
184 #ifdef CONFIG_X86_64
185 	cpu_detect_cache_sizes(c);
186 #else
187 	/* Not much we can do here... */
188 	/* Check if at least it has cpuid */
189 	if (c->cpuid_level == -1) {
190 		/* No cpuid. It must be an ancient CPU */
191 		if (c->x86 == 4)
192 			strcpy(c->x86_model_id, "486");
193 		else if (c->x86 == 3)
194 			strcpy(c->x86_model_id, "386");
195 	}
196 #endif
197 }
198 
199 static const struct cpu_dev default_cpu = {
200 	.c_init		= default_init,
201 	.c_vendor	= "Unknown",
202 	.c_x86_vendor	= X86_VENDOR_UNKNOWN,
203 };
204 
205 static const struct cpu_dev *this_cpu = &default_cpu;
206 
207 DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
208 #ifdef CONFIG_X86_64
209 	/*
210 	 * We need valid kernel segments for data and code in long mode too
211 	 * IRET will check the segment types  kkeil 2000/10/28
212 	 * Also sysret mandates a special GDT layout
213 	 *
214 	 * TLS descriptors are currently at a different place compared to i386.
215 	 * Hopefully nobody expects them at a fixed place (Wine?)
216 	 */
217 	[GDT_ENTRY_KERNEL32_CS]		= GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff),
218 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(DESC_CODE64, 0, 0xfffff),
219 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(DESC_DATA64, 0, 0xfffff),
220 	[GDT_ENTRY_DEFAULT_USER32_CS]	= GDT_ENTRY_INIT(DESC_CODE32 | DESC_USER, 0, 0xfffff),
221 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(DESC_DATA64 | DESC_USER, 0, 0xfffff),
222 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(DESC_CODE64 | DESC_USER, 0, 0xfffff),
223 #else
224 	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(DESC_CODE32, 0, 0xfffff),
225 	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
226 	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(DESC_CODE32 | DESC_USER, 0, 0xfffff),
227 	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(DESC_DATA32 | DESC_USER, 0, 0xfffff),
228 	/*
229 	 * Segments used for calling PnP BIOS have byte granularity.
230 	 * They code segments and data segments have fixed 64k limits,
231 	 * the transfer segment sizes are set at run time.
232 	 */
233 	[GDT_ENTRY_PNPBIOS_CS32]	= GDT_ENTRY_INIT(DESC_CODE32_BIOS, 0, 0xffff),
234 	[GDT_ENTRY_PNPBIOS_CS16]	= GDT_ENTRY_INIT(DESC_CODE16, 0, 0xffff),
235 	[GDT_ENTRY_PNPBIOS_DS]		= GDT_ENTRY_INIT(DESC_DATA16, 0, 0xffff),
236 	[GDT_ENTRY_PNPBIOS_TS1]		= GDT_ENTRY_INIT(DESC_DATA16, 0, 0),
237 	[GDT_ENTRY_PNPBIOS_TS2]		= GDT_ENTRY_INIT(DESC_DATA16, 0, 0),
238 	/*
239 	 * The APM segments have byte granularity and their bases
240 	 * are set at run time.  All have 64k limits.
241 	 */
242 	[GDT_ENTRY_APMBIOS_BASE]	= GDT_ENTRY_INIT(DESC_CODE32_BIOS, 0, 0xffff),
243 	[GDT_ENTRY_APMBIOS_BASE+1]	= GDT_ENTRY_INIT(DESC_CODE16, 0, 0xffff),
244 	[GDT_ENTRY_APMBIOS_BASE+2]	= GDT_ENTRY_INIT(DESC_DATA32_BIOS, 0, 0xffff),
245 
246 	[GDT_ENTRY_ESPFIX_SS]		= GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
247 	[GDT_ENTRY_PERCPU]		= GDT_ENTRY_INIT(DESC_DATA32, 0, 0xfffff),
248 #endif
249 } };
250 EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
251 SYM_PIC_ALIAS(gdt_page);
252 
253 #ifdef CONFIG_X86_64
254 static int __init x86_nopcid_setup(char *s)
255 {
256 	/* nopcid doesn't accept parameters */
257 	if (s)
258 		return -EINVAL;
259 
260 	/* do not emit a message if the feature is not present */
261 	if (!boot_cpu_has(X86_FEATURE_PCID))
262 		return 0;
263 
264 	setup_clear_cpu_cap(X86_FEATURE_PCID);
265 	pr_info("nopcid: PCID feature disabled\n");
266 	return 0;
267 }
268 early_param("nopcid", x86_nopcid_setup);
269 #endif
270 
271 static int __init x86_noinvpcid_setup(char *s)
272 {
273 	/* noinvpcid doesn't accept parameters */
274 	if (s)
275 		return -EINVAL;
276 
277 	/* do not emit a message if the feature is not present */
278 	if (!boot_cpu_has(X86_FEATURE_INVPCID))
279 		return 0;
280 
281 	setup_clear_cpu_cap(X86_FEATURE_INVPCID);
282 	pr_info("noinvpcid: INVPCID feature disabled\n");
283 	return 0;
284 }
285 early_param("noinvpcid", x86_noinvpcid_setup);
286 
287 /* Standard macro to see if a specific flag is changeable */
288 static inline bool flag_is_changeable_p(unsigned long flag)
289 {
290 	unsigned long f1, f2;
291 
292 	if (!IS_ENABLED(CONFIG_X86_32))
293 		return true;
294 
295 	/*
296 	 * Cyrix and IDT cpus allow disabling of CPUID
297 	 * so the code below may return different results
298 	 * when it is executed before and after enabling
299 	 * the CPUID. Add "volatile" to not allow gcc to
300 	 * optimize the subsequent calls to this function.
301 	 */
302 	asm volatile ("pushfl		\n\t"
303 		      "pushfl		\n\t"
304 		      "popl %0		\n\t"
305 		      "movl %0, %1	\n\t"
306 		      "xorl %2, %0	\n\t"
307 		      "pushl %0		\n\t"
308 		      "popfl		\n\t"
309 		      "pushfl		\n\t"
310 		      "popl %0		\n\t"
311 		      "popfl		\n\t"
312 
313 		      : "=&r" (f1), "=&r" (f2)
314 		      : "ir" (flag));
315 
316 	return (f1 ^ f2) & flag;
317 }
318 
319 #ifdef CONFIG_X86_32
320 static int cachesize_override = -1;
321 static int disable_x86_serial_nr = 1;
322 
323 static int __init cachesize_setup(char *str)
324 {
325 	get_option(&str, &cachesize_override);
326 	return 1;
327 }
328 __setup("cachesize=", cachesize_setup);
329 
330 /* Probe for the CPUID instruction */
331 bool cpuid_feature(void)
332 {
333 	return flag_is_changeable_p(X86_EFLAGS_ID);
334 }
335 
336 static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
337 {
338 	unsigned long lo, hi;
339 
340 	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
341 		return;
342 
343 	/* Disable processor serial number: */
344 
345 	rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
346 	lo |= 0x200000;
347 	wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
348 
349 	pr_notice("CPU serial number disabled.\n");
350 	clear_cpu_cap(c, X86_FEATURE_PN);
351 
352 	/* Disabling the serial number may affect the cpuid level */
353 	c->cpuid_level = cpuid_eax(0);
354 }
355 
356 static int __init x86_serial_nr_setup(char *s)
357 {
358 	disable_x86_serial_nr = 0;
359 	return 1;
360 }
361 __setup("serialnumber", x86_serial_nr_setup);
362 #else
363 static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
364 {
365 }
366 #endif
367 
368 static __always_inline void setup_smep(struct cpuinfo_x86 *c)
369 {
370 	if (cpu_has(c, X86_FEATURE_SMEP))
371 		cr4_set_bits(X86_CR4_SMEP);
372 }
373 
374 static __always_inline void setup_smap(struct cpuinfo_x86 *c)
375 {
376 	unsigned long eflags = native_save_fl();
377 
378 	/* This should have been cleared long ago */
379 	BUG_ON(eflags & X86_EFLAGS_AC);
380 
381 	if (cpu_has(c, X86_FEATURE_SMAP))
382 		cr4_set_bits(X86_CR4_SMAP);
383 }
384 
385 static __always_inline void setup_umip(struct cpuinfo_x86 *c)
386 {
387 	/* Check the boot processor, plus build option for UMIP. */
388 	if (!cpu_feature_enabled(X86_FEATURE_UMIP))
389 		goto out;
390 
391 	/* Check the current processor's cpuid bits. */
392 	if (!cpu_has(c, X86_FEATURE_UMIP))
393 		goto out;
394 
395 	cr4_set_bits(X86_CR4_UMIP);
396 
397 	pr_info_once("x86/cpu: User Mode Instruction Prevention (UMIP) activated\n");
398 
399 	return;
400 
401 out:
402 	/*
403 	 * Make sure UMIP is disabled in case it was enabled in a
404 	 * previous boot (e.g., via kexec).
405 	 */
406 	cr4_clear_bits(X86_CR4_UMIP);
407 }
408 
409 static __always_inline void setup_lass(struct cpuinfo_x86 *c)
410 {
411 	if (!cpu_feature_enabled(X86_FEATURE_LASS))
412 		return;
413 
414 	/*
415 	 * Legacy vsyscall page access causes a #GP when LASS is active.
416 	 * Disable LASS because the #GP handler doesn't support vsyscall
417 	 * emulation.
418 	 *
419 	 * Also disable LASS when running under EFI, as some runtime and
420 	 * boot services rely on 1:1 mappings in the lower half.
421 	 */
422 	if (IS_ENABLED(CONFIG_X86_VSYSCALL_EMULATION) ||
423 	    IS_ENABLED(CONFIG_EFI)) {
424 		setup_clear_cpu_cap(X86_FEATURE_LASS);
425 		return;
426 	}
427 
428 	cr4_set_bits(X86_CR4_LASS);
429 }
430 
431 /* These bits should not change their value after CPU init is finished. */
432 static const unsigned long cr4_pinned_mask = X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP |
433 					     X86_CR4_FSGSBASE | X86_CR4_CET | X86_CR4_FRED;
434 static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
435 static unsigned long cr4_pinned_bits __ro_after_init;
436 
437 void native_write_cr0(unsigned long val)
438 {
439 	unsigned long bits_missing = 0;
440 
441 set_register:
442 	asm volatile("mov %0,%%cr0": "+r" (val) : : "memory");
443 
444 	if (static_branch_likely(&cr_pinning)) {
445 		if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) {
446 			bits_missing = X86_CR0_WP;
447 			val |= bits_missing;
448 			goto set_register;
449 		}
450 		/* Warn after we've set the missing bits. */
451 		WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n");
452 	}
453 }
454 EXPORT_SYMBOL(native_write_cr0);
455 
456 void __no_profile native_write_cr4(unsigned long val)
457 {
458 	unsigned long bits_changed = 0;
459 
460 set_register:
461 	asm volatile("mov %0,%%cr4": "+r" (val) : : "memory");
462 
463 	if (static_branch_likely(&cr_pinning)) {
464 		if (unlikely((val & cr4_pinned_mask) != cr4_pinned_bits)) {
465 			bits_changed = (val & cr4_pinned_mask) ^ cr4_pinned_bits;
466 			val = (val & ~cr4_pinned_mask) | cr4_pinned_bits;
467 			goto set_register;
468 		}
469 		/* Warn after we've corrected the changed bits. */
470 		WARN_ONCE(bits_changed, "pinned CR4 bits changed: 0x%lx!?\n",
471 			  bits_changed);
472 	}
473 }
474 #if IS_MODULE(CONFIG_LKDTM)
475 EXPORT_SYMBOL_GPL(native_write_cr4);
476 #endif
477 
478 void cr4_update_irqsoff(unsigned long set, unsigned long clear)
479 {
480 	unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4);
481 
482 	lockdep_assert_irqs_disabled();
483 
484 	newval = (cr4 & ~clear) | set;
485 	if (newval != cr4) {
486 		this_cpu_write(cpu_tlbstate.cr4, newval);
487 		__write_cr4(newval);
488 	}
489 }
490 EXPORT_SYMBOL_FOR_KVM(cr4_update_irqsoff);
491 
492 /* Read the CR4 shadow. */
493 unsigned long cr4_read_shadow(void)
494 {
495 	return this_cpu_read(cpu_tlbstate.cr4);
496 }
497 EXPORT_SYMBOL_FOR_KVM(cr4_read_shadow);
498 
499 void cr4_init(void)
500 {
501 	unsigned long cr4 = __read_cr4();
502 
503 	if (boot_cpu_has(X86_FEATURE_PCID))
504 		cr4 |= X86_CR4_PCIDE;
505 	if (static_branch_likely(&cr_pinning))
506 		cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits;
507 
508 	__write_cr4(cr4);
509 
510 	/* Initialize cr4 shadow for this CPU. */
511 	this_cpu_write(cpu_tlbstate.cr4, cr4);
512 }
513 
514 /*
515  * Once CPU feature detection is finished (and boot params have been
516  * parsed), record any of the sensitive CR bits that are set, and
517  * enable CR pinning.
518  */
519 static void __init setup_cr_pinning(void)
520 {
521 	cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask;
522 	static_key_enable(&cr_pinning.key);
523 }
524 
525 static __init int x86_nofsgsbase_setup(char *arg)
526 {
527 	/* Require an exact match without trailing characters. */
528 	if (strlen(arg))
529 		return 0;
530 
531 	/* Do not emit a message if the feature is not present. */
532 	if (!boot_cpu_has(X86_FEATURE_FSGSBASE))
533 		return 1;
534 
535 	setup_clear_cpu_cap(X86_FEATURE_FSGSBASE);
536 	pr_info("FSGSBASE disabled via kernel command line\n");
537 	return 1;
538 }
539 __setup("nofsgsbase", x86_nofsgsbase_setup);
540 
541 /*
542  * Protection Keys are not available in 32-bit mode.
543  */
544 static bool pku_disabled;
545 
546 static __always_inline void setup_pku(struct cpuinfo_x86 *c)
547 {
548 	if (c == &boot_cpu_data) {
549 		if (pku_disabled || !cpu_feature_enabled(X86_FEATURE_PKU))
550 			return;
551 		/*
552 		 * Setting CR4.PKE will cause the X86_FEATURE_OSPKE cpuid
553 		 * bit to be set.  Enforce it.
554 		 */
555 		setup_force_cpu_cap(X86_FEATURE_OSPKE);
556 
557 	} else if (!cpu_feature_enabled(X86_FEATURE_OSPKE)) {
558 		return;
559 	}
560 
561 	cr4_set_bits(X86_CR4_PKE);
562 	/* Load the default PKRU value */
563 	pkru_write_default();
564 }
565 
566 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
567 static __init int setup_disable_pku(char *arg)
568 {
569 	/*
570 	 * Do not clear the X86_FEATURE_PKU bit.  All of the
571 	 * runtime checks are against OSPKE so clearing the
572 	 * bit does nothing.
573 	 *
574 	 * This way, we will see "pku" in cpuinfo, but not
575 	 * "ospke", which is exactly what we want.  It shows
576 	 * that the CPU has PKU, but the OS has not enabled it.
577 	 * This happens to be exactly how a system would look
578 	 * if we disabled the config option.
579 	 */
580 	pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
581 	pku_disabled = true;
582 	return 1;
583 }
584 __setup("nopku", setup_disable_pku);
585 #endif
586 
587 #ifdef CONFIG_X86_KERNEL_IBT
588 
589 __noendbr u64 ibt_save(bool disable)
590 {
591 	u64 msr = 0;
592 
593 	if (cpu_feature_enabled(X86_FEATURE_IBT)) {
594 		rdmsrq(MSR_IA32_S_CET, msr);
595 		if (disable)
596 			wrmsrq(MSR_IA32_S_CET, msr & ~CET_ENDBR_EN);
597 	}
598 
599 	return msr;
600 }
601 
602 __noendbr void ibt_restore(u64 save)
603 {
604 	u64 msr;
605 
606 	if (cpu_feature_enabled(X86_FEATURE_IBT)) {
607 		rdmsrq(MSR_IA32_S_CET, msr);
608 		msr &= ~CET_ENDBR_EN;
609 		msr |= (save & CET_ENDBR_EN);
610 		wrmsrq(MSR_IA32_S_CET, msr);
611 	}
612 }
613 
614 #endif
615 
616 static __always_inline void setup_cet(struct cpuinfo_x86 *c)
617 {
618 	bool user_shstk, kernel_ibt;
619 
620 	if (!IS_ENABLED(CONFIG_X86_CET))
621 		return;
622 
623 	kernel_ibt = HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT);
624 	user_shstk = cpu_feature_enabled(X86_FEATURE_SHSTK) &&
625 		     IS_ENABLED(CONFIG_X86_USER_SHADOW_STACK);
626 
627 	if (!kernel_ibt && !user_shstk)
628 		return;
629 
630 	if (user_shstk)
631 		set_cpu_cap(c, X86_FEATURE_USER_SHSTK);
632 
633 	if (kernel_ibt)
634 		wrmsrq(MSR_IA32_S_CET, CET_ENDBR_EN);
635 	else
636 		wrmsrq(MSR_IA32_S_CET, 0);
637 
638 	cr4_set_bits(X86_CR4_CET);
639 
640 	if (kernel_ibt && ibt_selftest()) {
641 		pr_err("IBT selftest: Failed!\n");
642 		wrmsrq(MSR_IA32_S_CET, 0);
643 		setup_clear_cpu_cap(X86_FEATURE_IBT);
644 	}
645 }
646 
647 __noendbr void cet_disable(void)
648 {
649 	if (!(cpu_feature_enabled(X86_FEATURE_IBT) ||
650 	      cpu_feature_enabled(X86_FEATURE_SHSTK)))
651 		return;
652 
653 	wrmsrq(MSR_IA32_S_CET, 0);
654 	wrmsrq(MSR_IA32_U_CET, 0);
655 }
656 
657 /*
658  * Some CPU features depend on higher CPUID levels, which may not always
659  * be available due to CPUID level capping or broken virtualization
660  * software.  Add those features to this table to auto-disable them.
661  */
662 struct cpuid_dependent_feature {
663 	u32 feature;
664 	u32 level;
665 };
666 
667 static const struct cpuid_dependent_feature
668 cpuid_dependent_features[] = {
669 	{ X86_FEATURE_MWAIT,		CPUID_LEAF_MWAIT },
670 	{ X86_FEATURE_DCA,		CPUID_LEAF_DCA },
671 	{ X86_FEATURE_XSAVE,		CPUID_LEAF_XSTATE },
672 	{ 0, 0 }
673 };
674 
675 static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
676 {
677 	const struct cpuid_dependent_feature *df;
678 
679 	for (df = cpuid_dependent_features; df->feature; df++) {
680 
681 		if (!cpu_has(c, df->feature))
682 			continue;
683 		/*
684 		 * Note: cpuid_level is set to -1 if unavailable, but
685 		 * extended_extended_level is set to 0 if unavailable
686 		 * and the legitimate extended levels are all negative
687 		 * when signed; hence the weird messing around with
688 		 * signs here...
689 		 */
690 		if (!((s32)df->level < 0 ?
691 		     (u32)df->level > (u32)c->extended_cpuid_level :
692 		     (s32)df->level > (s32)c->cpuid_level))
693 			continue;
694 
695 		clear_cpu_cap(c, df->feature);
696 		if (!warn)
697 			continue;
698 
699 		pr_warn("CPU: CPU feature %s disabled, no CPUID level 0x%x\n",
700 			x86_cap_flags[df->feature], df->level);
701 	}
702 }
703 
704 /*
705  * Naming convention should be: <Name> [(<Codename>)]
706  * This table only is used unless init_<vendor>() below doesn't set it;
707  * in particular, if CPUID levels 0x80000002..4 are supported, this
708  * isn't used
709  */
710 
711 /* Look up CPU names by table lookup. */
712 static const char *table_lookup_model(struct cpuinfo_x86 *c)
713 {
714 #ifdef CONFIG_X86_32
715 	const struct legacy_cpu_model_info *info;
716 
717 	if (c->x86_model >= 16)
718 		return NULL;	/* Range check */
719 
720 	if (!this_cpu)
721 		return NULL;
722 
723 	info = this_cpu->legacy_models;
724 
725 	while (info->family) {
726 		if (info->family == c->x86)
727 			return info->model_names[c->x86_model];
728 		info++;
729 	}
730 #endif
731 	return NULL;		/* Not found */
732 }
733 
734 /* Aligned to unsigned long to avoid split lock in atomic bitmap ops */
735 __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
736 __u32 cpu_caps_set[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
737 
738 #ifdef CONFIG_X86_32
739 /* The 32-bit entry code needs to find cpu_entry_area. */
740 DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
741 #endif
742 
743 /* Load the original GDT from the per-cpu structure */
744 void load_direct_gdt(int cpu)
745 {
746 	struct desc_ptr gdt_descr;
747 
748 	gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
749 	gdt_descr.size = GDT_SIZE - 1;
750 	load_gdt(&gdt_descr);
751 }
752 EXPORT_SYMBOL_FOR_KVM(load_direct_gdt);
753 
754 /* Load a fixmap remapping of the per-cpu GDT */
755 void load_fixmap_gdt(int cpu)
756 {
757 	struct desc_ptr gdt_descr;
758 
759 	gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
760 	gdt_descr.size = GDT_SIZE - 1;
761 	load_gdt(&gdt_descr);
762 }
763 EXPORT_SYMBOL_GPL(load_fixmap_gdt);
764 
765 /**
766  * switch_gdt_and_percpu_base - Switch to direct GDT and runtime per CPU base
767  * @cpu:	The CPU number for which this is invoked
768  *
769  * Invoked during early boot to switch from early GDT and early per CPU to
770  * the direct GDT and the runtime per CPU area. On 32-bit the percpu base
771  * switch is implicit by loading the direct GDT. On 64bit this requires
772  * to update GSBASE.
773  */
774 void __init switch_gdt_and_percpu_base(int cpu)
775 {
776 	load_direct_gdt(cpu);
777 
778 #ifdef CONFIG_X86_64
779 	/*
780 	 * No need to load %gs. It is already correct.
781 	 *
782 	 * Writing %gs on 64bit would zero GSBASE which would make any per
783 	 * CPU operation up to the point of the wrmsrq() fault.
784 	 *
785 	 * Set GSBASE to the new offset. Until the wrmsrq() happens the
786 	 * early mapping is still valid. That means the GSBASE update will
787 	 * lose any prior per CPU data which was not copied over in
788 	 * setup_per_cpu_areas().
789 	 *
790 	 * This works even with stackprotector enabled because the
791 	 * per CPU stack canary is 0 in both per CPU areas.
792 	 */
793 	wrmsrq(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu));
794 #else
795 	/*
796 	 * %fs is already set to __KERNEL_PERCPU, but after switching GDT
797 	 * it is required to load FS again so that the 'hidden' part is
798 	 * updated from the new GDT. Up to this point the early per CPU
799 	 * translation is active. Any content of the early per CPU data
800 	 * which was not copied over in setup_per_cpu_areas() is lost.
801 	 */
802 	loadsegment(fs, __KERNEL_PERCPU);
803 #endif
804 }
805 
806 static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
807 
808 static void get_model_name(struct cpuinfo_x86 *c)
809 {
810 	unsigned int *v;
811 	char *p, *q, *s;
812 
813 	if (c->extended_cpuid_level < 0x80000004)
814 		return;
815 
816 	v = (unsigned int *)c->x86_model_id;
817 	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
818 	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
819 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
820 	c->x86_model_id[48] = 0;
821 
822 	/* Trim whitespace */
823 	p = q = s = &c->x86_model_id[0];
824 
825 	while (*p == ' ')
826 		p++;
827 
828 	while (*p) {
829 		/* Note the last non-whitespace index */
830 		if (!isspace(*p))
831 			s = q;
832 
833 		*q++ = *p++;
834 	}
835 
836 	*(s + 1) = '\0';
837 }
838 
839 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
840 {
841 	unsigned int n, dummy, ebx, ecx, edx, l2size;
842 
843 	n = c->extended_cpuid_level;
844 
845 	if (n >= 0x80000005) {
846 		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
847 		c->x86_cache_size = (ecx>>24) + (edx>>24);
848 #ifdef CONFIG_X86_64
849 		/* On K8 L1 TLB is inclusive, so don't count it */
850 		c->x86_tlbsize = 0;
851 #endif
852 	}
853 
854 	if (n < 0x80000006)	/* Some chips just has a large L1. */
855 		return;
856 
857 	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
858 	l2size = ecx >> 16;
859 
860 #ifdef CONFIG_X86_64
861 	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
862 #else
863 	/* do processor-specific cache resizing */
864 	if (this_cpu->legacy_cache_size)
865 		l2size = this_cpu->legacy_cache_size(c, l2size);
866 
867 	/* Allow user to override all this if necessary. */
868 	if (cachesize_override != -1)
869 		l2size = cachesize_override;
870 
871 	if (l2size == 0)
872 		return;		/* Again, no L2 cache is possible */
873 #endif
874 
875 	c->x86_cache_size = l2size;
876 }
877 
878 u16 __read_mostly tlb_lli_4k;
879 u16 __read_mostly tlb_lli_2m;
880 u16 __read_mostly tlb_lli_4m;
881 u16 __read_mostly tlb_lld_4k;
882 u16 __read_mostly tlb_lld_2m;
883 u16 __read_mostly tlb_lld_4m;
884 u16 __read_mostly tlb_lld_1g;
885 
886 static void cpu_detect_tlb(struct cpuinfo_x86 *c)
887 {
888 	if (this_cpu->c_detect_tlb)
889 		this_cpu->c_detect_tlb(c);
890 
891 	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
892 		tlb_lli_4k, tlb_lli_2m, tlb_lli_4m);
893 
894 	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
895 		tlb_lld_4k, tlb_lld_2m, tlb_lld_4m, tlb_lld_1g);
896 }
897 
898 void get_cpu_vendor(struct cpuinfo_x86 *c)
899 {
900 	char *v = c->x86_vendor_id;
901 	int i;
902 
903 	for (i = 0; i < X86_VENDOR_NUM; i++) {
904 		if (!cpu_devs[i])
905 			break;
906 
907 		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
908 		    (cpu_devs[i]->c_ident[1] &&
909 		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
910 
911 			this_cpu = cpu_devs[i];
912 			c->x86_vendor = this_cpu->c_x86_vendor;
913 			return;
914 		}
915 	}
916 
917 	pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
918 		    "CPU: Your system may be unstable.\n", v);
919 
920 	c->x86_vendor = X86_VENDOR_UNKNOWN;
921 	this_cpu = &default_cpu;
922 }
923 
924 void cpu_detect(struct cpuinfo_x86 *c)
925 {
926 	/* Get vendor name */
927 	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
928 	      (unsigned int *)&c->x86_vendor_id[0],
929 	      (unsigned int *)&c->x86_vendor_id[8],
930 	      (unsigned int *)&c->x86_vendor_id[4]);
931 
932 	c->x86 = 4;
933 	/* Intel-defined flags: level 0x00000001 */
934 	if (c->cpuid_level >= 0x00000001) {
935 		u32 junk, tfms, cap0, misc;
936 
937 		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
938 		c->x86		= x86_family(tfms);
939 		c->x86_model	= x86_model(tfms);
940 		c->x86_stepping	= x86_stepping(tfms);
941 
942 		if (cap0 & (1<<19)) {
943 			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
944 			c->x86_cache_alignment = c->x86_clflush_size;
945 		}
946 	}
947 }
948 
949 static void apply_forced_caps(struct cpuinfo_x86 *c)
950 {
951 	int i;
952 
953 	for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
954 		c->x86_capability[i] &= ~cpu_caps_cleared[i];
955 		c->x86_capability[i] |= cpu_caps_set[i];
956 	}
957 }
958 
959 static void init_speculation_control(struct cpuinfo_x86 *c)
960 {
961 	/*
962 	 * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
963 	 * and they also have a different bit for STIBP support. Also,
964 	 * a hypervisor might have set the individual AMD bits even on
965 	 * Intel CPUs, for finer-grained selection of what's available.
966 	 */
967 	if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
968 		set_cpu_cap(c, X86_FEATURE_IBRS);
969 		set_cpu_cap(c, X86_FEATURE_IBPB);
970 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
971 	}
972 
973 	if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
974 		set_cpu_cap(c, X86_FEATURE_STIBP);
975 
976 	if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
977 	    cpu_has(c, X86_FEATURE_VIRT_SSBD))
978 		set_cpu_cap(c, X86_FEATURE_SSBD);
979 
980 	if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
981 		set_cpu_cap(c, X86_FEATURE_IBRS);
982 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
983 	}
984 
985 	if (cpu_has(c, X86_FEATURE_AMD_IBPB))
986 		set_cpu_cap(c, X86_FEATURE_IBPB);
987 
988 	if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
989 		set_cpu_cap(c, X86_FEATURE_STIBP);
990 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
991 	}
992 
993 	if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
994 		set_cpu_cap(c, X86_FEATURE_SSBD);
995 		set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
996 		clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
997 	}
998 }
999 
1000 void get_cpu_cap(struct cpuinfo_x86 *c)
1001 {
1002 	u32 eax, ebx, ecx, edx;
1003 
1004 	/* Intel-defined flags: level 0x00000001 */
1005 	if (c->cpuid_level >= 0x00000001) {
1006 		cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
1007 
1008 		c->x86_capability[CPUID_1_ECX] = ecx;
1009 		c->x86_capability[CPUID_1_EDX] = edx;
1010 	}
1011 
1012 	/* Thermal and Power Management Leaf: level 0x00000006 (eax) */
1013 	if (c->cpuid_level >= 0x00000006)
1014 		c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
1015 
1016 	/* Additional Intel-defined flags: level 0x00000007 */
1017 	if (c->cpuid_level >= 0x00000007) {
1018 		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
1019 		c->x86_capability[CPUID_7_0_EBX] = ebx;
1020 		c->x86_capability[CPUID_7_ECX] = ecx;
1021 		c->x86_capability[CPUID_7_EDX] = edx;
1022 
1023 		/* Check valid sub-leaf index before accessing it */
1024 		if (eax >= 1) {
1025 			cpuid_count(0x00000007, 1, &eax, &ebx, &ecx, &edx);
1026 			c->x86_capability[CPUID_7_1_EAX] = eax;
1027 		}
1028 	}
1029 
1030 	/* Extended state features: level 0x0000000d */
1031 	if (c->cpuid_level >= 0x0000000d) {
1032 		cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
1033 
1034 		c->x86_capability[CPUID_D_1_EAX] = eax;
1035 	}
1036 
1037 	/*
1038 	 * Check if extended CPUID leaves are implemented: Max extended
1039 	 * CPUID leaf must be in the 0x80000001-0x8000ffff range.
1040 	 */
1041 	eax = cpuid_eax(0x80000000);
1042 	c->extended_cpuid_level = ((eax & 0xffff0000) == 0x80000000) ? eax : 0;
1043 
1044 	if (c->extended_cpuid_level >= 0x80000001) {
1045 		cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
1046 
1047 		c->x86_capability[CPUID_8000_0001_ECX] = ecx;
1048 		c->x86_capability[CPUID_8000_0001_EDX] = edx;
1049 	}
1050 
1051 	if (c->extended_cpuid_level >= 0x80000007)
1052 		c->x86_power = cpuid_edx(0x80000007);
1053 
1054 	if (c->extended_cpuid_level >= 0x80000008) {
1055 		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
1056 		c->x86_capability[CPUID_8000_0008_EBX] = ebx;
1057 	}
1058 
1059 	if (c->extended_cpuid_level >= 0x8000000a)
1060 		c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
1061 
1062 	if (c->extended_cpuid_level >= 0x8000001f)
1063 		c->x86_capability[CPUID_8000_001F_EAX] = cpuid_eax(0x8000001f);
1064 
1065 	if (c->extended_cpuid_level >= 0x80000021)
1066 		c->x86_capability[CPUID_8000_0021_EAX] = cpuid_eax(0x80000021);
1067 
1068 	init_scattered_cpuid_features(c);
1069 	init_speculation_control(c);
1070 
1071 	if (IS_ENABLED(CONFIG_X86_64) || cpu_has(c, X86_FEATURE_SEP))
1072 		set_cpu_cap(c, X86_FEATURE_SYSFAST32);
1073 
1074 	/*
1075 	 * Clear/Set all flags overridden by options, after probe.
1076 	 * This needs to happen each time we re-probe, which may happen
1077 	 * several times during CPU initialization.
1078 	 */
1079 	apply_forced_caps(c);
1080 }
1081 
1082 void get_cpu_address_sizes(struct cpuinfo_x86 *c)
1083 {
1084 	u32 eax, ebx, ecx, edx;
1085 
1086 	if (!cpu_has(c, X86_FEATURE_CPUID) ||
1087 	    (c->extended_cpuid_level < 0x80000008)) {
1088 		if (IS_ENABLED(CONFIG_X86_64)) {
1089 			c->x86_clflush_size = 64;
1090 			c->x86_phys_bits = 36;
1091 			c->x86_virt_bits = 48;
1092 		} else {
1093 			c->x86_clflush_size = 32;
1094 			c->x86_virt_bits = 32;
1095 			c->x86_phys_bits = 32;
1096 
1097 			if (cpu_has(c, X86_FEATURE_PAE) ||
1098 			    cpu_has(c, X86_FEATURE_PSE36))
1099 				c->x86_phys_bits = 36;
1100 		}
1101 	} else {
1102 		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
1103 
1104 		c->x86_virt_bits = (eax >> 8) & 0xff;
1105 		c->x86_phys_bits = eax & 0xff;
1106 
1107 		/* Provide a sane default if not enumerated: */
1108 		if (!c->x86_clflush_size)
1109 			c->x86_clflush_size = 32;
1110 	}
1111 
1112 	c->x86_cache_bits = c->x86_phys_bits;
1113 	c->x86_cache_alignment = c->x86_clflush_size;
1114 }
1115 
1116 static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
1117 {
1118 	int i;
1119 
1120 	/*
1121 	 * First of all, decide if this is a 486 or higher
1122 	 * It's a 486 if we can modify the AC flag
1123 	 */
1124 	if (flag_is_changeable_p(X86_EFLAGS_AC))
1125 		c->x86 = 4;
1126 	else
1127 		c->x86 = 3;
1128 
1129 	for (i = 0; i < X86_VENDOR_NUM; i++)
1130 		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
1131 			c->x86_vendor_id[0] = 0;
1132 			cpu_devs[i]->c_identify(c);
1133 			if (c->x86_vendor_id[0]) {
1134 				get_cpu_vendor(c);
1135 				break;
1136 			}
1137 		}
1138 }
1139 
1140 #define NO_SPECULATION		BIT(0)
1141 #define NO_MELTDOWN		BIT(1)
1142 #define NO_SSB			BIT(2)
1143 #define NO_L1TF			BIT(3)
1144 #define NO_MDS			BIT(4)
1145 #define MSBDS_ONLY		BIT(5)
1146 #define NO_SWAPGS		BIT(6)
1147 #define NO_ITLB_MULTIHIT	BIT(7)
1148 #define NO_SPECTRE_V2		BIT(8)
1149 #define NO_MMIO			BIT(9)
1150 #define NO_EIBRS_PBRSB		BIT(10)
1151 #define NO_BHI			BIT(11)
1152 
1153 #define VULNWL(vendor, family, model, whitelist)	\
1154 	X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, whitelist)
1155 
1156 #define VULNWL_INTEL(vfm, whitelist)		\
1157 	X86_MATCH_VFM(vfm, whitelist)
1158 
1159 #define VULNWL_AMD(family, whitelist)		\
1160 	VULNWL(AMD, family, X86_MODEL_ANY, whitelist)
1161 
1162 #define VULNWL_HYGON(family, whitelist)		\
1163 	VULNWL(HYGON, family, X86_MODEL_ANY, whitelist)
1164 
1165 static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
1166 	VULNWL(ANY,	4, X86_MODEL_ANY,	NO_SPECULATION),
1167 	VULNWL(CENTAUR,	5, X86_MODEL_ANY,	NO_SPECULATION),
1168 	VULNWL(INTEL,	5, X86_MODEL_ANY,	NO_SPECULATION),
1169 	VULNWL(NSC,	5, X86_MODEL_ANY,	NO_SPECULATION),
1170 	VULNWL(VORTEX,	5, X86_MODEL_ANY,	NO_SPECULATION),
1171 	VULNWL(VORTEX,	6, X86_MODEL_ANY,	NO_SPECULATION),
1172 
1173 	/* Intel Family 6 */
1174 	VULNWL_INTEL(INTEL_TIGERLAKE,		NO_MMIO),
1175 	VULNWL_INTEL(INTEL_TIGERLAKE_L,		NO_MMIO),
1176 	VULNWL_INTEL(INTEL_ALDERLAKE,		NO_MMIO),
1177 	VULNWL_INTEL(INTEL_ALDERLAKE_L,		NO_MMIO),
1178 
1179 	VULNWL_INTEL(INTEL_ATOM_SALTWELL,	NO_SPECULATION | NO_ITLB_MULTIHIT),
1180 	VULNWL_INTEL(INTEL_ATOM_SALTWELL_TABLET, NO_SPECULATION | NO_ITLB_MULTIHIT),
1181 	VULNWL_INTEL(INTEL_ATOM_SALTWELL_MID,	NO_SPECULATION | NO_ITLB_MULTIHIT),
1182 	VULNWL_INTEL(INTEL_ATOM_BONNELL,	NO_SPECULATION | NO_ITLB_MULTIHIT),
1183 	VULNWL_INTEL(INTEL_ATOM_BONNELL_MID,	NO_SPECULATION | NO_ITLB_MULTIHIT),
1184 
1185 	VULNWL_INTEL(INTEL_ATOM_SILVERMONT,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1186 	VULNWL_INTEL(INTEL_ATOM_SILVERMONT_D,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1187 	VULNWL_INTEL(INTEL_ATOM_SILVERMONT_MID,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1188 	VULNWL_INTEL(INTEL_ATOM_AIRMONT,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1189 	VULNWL_INTEL(INTEL_XEON_PHI_KNL,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1190 	VULNWL_INTEL(INTEL_XEON_PHI_KNM,	NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1191 
1192 	VULNWL_INTEL(INTEL_CORE_YONAH,		NO_SSB),
1193 
1194 	VULNWL_INTEL(INTEL_ATOM_SILVERMONT_MID2,NO_SSB | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | MSBDS_ONLY),
1195 	VULNWL_INTEL(INTEL_ATOM_AIRMONT_NP,	NO_SSB | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
1196 
1197 	VULNWL_INTEL(INTEL_ATOM_GOLDMONT,	NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1198 	VULNWL_INTEL(INTEL_ATOM_GOLDMONT_D,	NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1199 	VULNWL_INTEL(INTEL_ATOM_GOLDMONT_PLUS,	NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB),
1200 
1201 	/*
1202 	 * Technically, swapgs isn't serializing on AMD (despite it previously
1203 	 * being documented as such in the APM).  But according to AMD, %gs is
1204 	 * updated non-speculatively, and the issuing of %gs-relative memory
1205 	 * operands will be blocked until the %gs update completes, which is
1206 	 * good enough for our purposes.
1207 	 */
1208 
1209 	VULNWL_INTEL(INTEL_ATOM_TREMONT,	NO_EIBRS_PBRSB),
1210 	VULNWL_INTEL(INTEL_ATOM_TREMONT_L,	NO_EIBRS_PBRSB),
1211 	VULNWL_INTEL(INTEL_ATOM_TREMONT_D,	NO_ITLB_MULTIHIT | NO_EIBRS_PBRSB),
1212 
1213 	/* AMD Family 0xf - 0x12 */
1214 	VULNWL_AMD(0x0f,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
1215 	VULNWL_AMD(0x10,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
1216 	VULNWL_AMD(0x11,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
1217 	VULNWL_AMD(0x12,	NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_BHI),
1218 
1219 	/* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */
1220 	VULNWL_AMD(X86_FAMILY_ANY,	NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB | NO_BHI),
1221 	VULNWL_HYGON(X86_FAMILY_ANY,	NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB | NO_BHI),
1222 
1223 	/* Zhaoxin Family 7 */
1224 	VULNWL(CENTAUR,	7, X86_MODEL_ANY,	NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO | NO_BHI),
1225 	VULNWL(ZHAOXIN,	7, X86_MODEL_ANY,	NO_SPECTRE_V2 | NO_SWAPGS | NO_MMIO | NO_BHI),
1226 	{}
1227 };
1228 
1229 #define VULNBL(vendor, family, model, blacklist)	\
1230 	X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, blacklist)
1231 
1232 #define VULNBL_INTEL_STEPS(vfm, max_stepping, issues)		   \
1233 	X86_MATCH_VFM_STEPS(vfm, X86_STEP_MIN, max_stepping, issues)
1234 
1235 #define VULNBL_INTEL_TYPE(vfm, cpu_type, issues)	\
1236 	X86_MATCH_VFM_CPU_TYPE(vfm, INTEL_CPU_TYPE_##cpu_type, issues)
1237 
1238 #define VULNBL_AMD(family, blacklist)		\
1239 	VULNBL(AMD, family, X86_MODEL_ANY, blacklist)
1240 
1241 #define VULNBL_HYGON(family, blacklist)		\
1242 	VULNBL(HYGON, family, X86_MODEL_ANY, blacklist)
1243 
1244 #define SRBDS		BIT(0)
1245 /* CPU is affected by X86_BUG_MMIO_STALE_DATA */
1246 #define MMIO		BIT(1)
1247 /* CPU is affected by Shared Buffers Data Sampling (SBDS), a variant of X86_BUG_MMIO_STALE_DATA */
1248 #define MMIO_SBDS	BIT(2)
1249 /* CPU is affected by RETbleed, speculating where you would not expect it */
1250 #define RETBLEED	BIT(3)
1251 /* CPU is affected by SMT (cross-thread) return predictions */
1252 #define SMT_RSB		BIT(4)
1253 /* CPU is affected by SRSO */
1254 #define SRSO		BIT(5)
1255 /* CPU is affected by GDS */
1256 #define GDS		BIT(6)
1257 /* CPU is affected by Register File Data Sampling */
1258 #define RFDS		BIT(7)
1259 /* CPU is affected by Indirect Target Selection */
1260 #define ITS		BIT(8)
1261 /* CPU is affected by Indirect Target Selection, but guest-host isolation is not affected */
1262 #define ITS_NATIVE_ONLY	BIT(9)
1263 /* CPU is affected by Transient Scheduler Attacks */
1264 #define TSA		BIT(10)
1265 /* CPU is affected by VMSCAPE */
1266 #define VMSCAPE		BIT(11)
1267 
1268 static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
1269 	VULNBL_INTEL_STEPS(INTEL_SANDYBRIDGE_X,	     X86_STEP_MAX,	VMSCAPE),
1270 	VULNBL_INTEL_STEPS(INTEL_SANDYBRIDGE,	     X86_STEP_MAX,	VMSCAPE),
1271 	VULNBL_INTEL_STEPS(INTEL_IVYBRIDGE_X,	     X86_STEP_MAX,	VMSCAPE),
1272 	VULNBL_INTEL_STEPS(INTEL_IVYBRIDGE,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1273 	VULNBL_INTEL_STEPS(INTEL_HASWELL,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1274 	VULNBL_INTEL_STEPS(INTEL_HASWELL_L,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1275 	VULNBL_INTEL_STEPS(INTEL_HASWELL_G,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1276 	VULNBL_INTEL_STEPS(INTEL_HASWELL_X,	     X86_STEP_MAX,	MMIO | VMSCAPE),
1277 	VULNBL_INTEL_STEPS(INTEL_BROADWELL_D,	     X86_STEP_MAX,	MMIO | VMSCAPE),
1278 	VULNBL_INTEL_STEPS(INTEL_BROADWELL_X,	     X86_STEP_MAX,	MMIO | VMSCAPE),
1279 	VULNBL_INTEL_STEPS(INTEL_BROADWELL_G,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1280 	VULNBL_INTEL_STEPS(INTEL_BROADWELL,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1281 	VULNBL_INTEL_STEPS(INTEL_SKYLAKE_X,		      0x5,	MMIO | RETBLEED | GDS | VMSCAPE),
1282 	VULNBL_INTEL_STEPS(INTEL_SKYLAKE_X,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | ITS | VMSCAPE),
1283 	VULNBL_INTEL_STEPS(INTEL_SKYLAKE_L,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
1284 	VULNBL_INTEL_STEPS(INTEL_SKYLAKE,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
1285 	VULNBL_INTEL_STEPS(INTEL_KABYLAKE_L,		      0xb,	MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
1286 	VULNBL_INTEL_STEPS(INTEL_KABYLAKE_L,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | SRBDS | ITS | VMSCAPE),
1287 	VULNBL_INTEL_STEPS(INTEL_KABYLAKE,		      0xc,	MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
1288 	VULNBL_INTEL_STEPS(INTEL_KABYLAKE,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | SRBDS | ITS | VMSCAPE),
1289 	VULNBL_INTEL_STEPS(INTEL_CANNONLAKE_L,	     X86_STEP_MAX,	RETBLEED | VMSCAPE),
1290 	VULNBL_INTEL_STEPS(INTEL_ICELAKE_L,	     X86_STEP_MAX,	MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY),
1291 	VULNBL_INTEL_STEPS(INTEL_ICELAKE_D,	     X86_STEP_MAX,	MMIO | GDS | ITS | ITS_NATIVE_ONLY),
1292 	VULNBL_INTEL_STEPS(INTEL_ICELAKE_X,	     X86_STEP_MAX,	MMIO | GDS | ITS | ITS_NATIVE_ONLY),
1293 	VULNBL_INTEL_STEPS(INTEL_COMETLAKE,	     X86_STEP_MAX,	MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | VMSCAPE),
1294 	VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L,		      0x0,	MMIO | RETBLEED | ITS | VMSCAPE),
1295 	VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L,	     X86_STEP_MAX,	MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | VMSCAPE),
1296 	VULNBL_INTEL_STEPS(INTEL_TIGERLAKE_L,	     X86_STEP_MAX,	GDS | ITS | ITS_NATIVE_ONLY),
1297 	VULNBL_INTEL_STEPS(INTEL_TIGERLAKE,	     X86_STEP_MAX,	GDS | ITS | ITS_NATIVE_ONLY),
1298 	VULNBL_INTEL_STEPS(INTEL_LAKEFIELD,	     X86_STEP_MAX,	MMIO | MMIO_SBDS | RETBLEED),
1299 	VULNBL_INTEL_STEPS(INTEL_ROCKETLAKE,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY),
1300 	VULNBL_INTEL_TYPE(INTEL_ALDERLAKE,		     ATOM,	RFDS | VMSCAPE),
1301 	VULNBL_INTEL_STEPS(INTEL_ALDERLAKE,	     X86_STEP_MAX,	VMSCAPE),
1302 	VULNBL_INTEL_STEPS(INTEL_ALDERLAKE_L,	     X86_STEP_MAX,	RFDS | VMSCAPE),
1303 	VULNBL_INTEL_TYPE(INTEL_RAPTORLAKE,		     ATOM,	RFDS | VMSCAPE),
1304 	VULNBL_INTEL_STEPS(INTEL_RAPTORLAKE,	     X86_STEP_MAX,	VMSCAPE),
1305 	VULNBL_INTEL_STEPS(INTEL_RAPTORLAKE_P,	     X86_STEP_MAX,	RFDS | VMSCAPE),
1306 	VULNBL_INTEL_STEPS(INTEL_RAPTORLAKE_S,	     X86_STEP_MAX,	RFDS | VMSCAPE),
1307 	VULNBL_INTEL_STEPS(INTEL_METEORLAKE_L,	     X86_STEP_MAX,	VMSCAPE),
1308 	VULNBL_INTEL_STEPS(INTEL_ARROWLAKE_H,	     X86_STEP_MAX,	VMSCAPE),
1309 	VULNBL_INTEL_STEPS(INTEL_ARROWLAKE,	     X86_STEP_MAX,	VMSCAPE),
1310 	VULNBL_INTEL_STEPS(INTEL_ARROWLAKE_U,	     X86_STEP_MAX,	VMSCAPE),
1311 	VULNBL_INTEL_STEPS(INTEL_LUNARLAKE_M,	     X86_STEP_MAX,	VMSCAPE),
1312 	VULNBL_INTEL_STEPS(INTEL_SAPPHIRERAPIDS_X,   X86_STEP_MAX,	VMSCAPE),
1313 	VULNBL_INTEL_STEPS(INTEL_GRANITERAPIDS_X,    X86_STEP_MAX,	VMSCAPE),
1314 	VULNBL_INTEL_STEPS(INTEL_EMERALDRAPIDS_X,    X86_STEP_MAX,	VMSCAPE),
1315 	VULNBL_INTEL_STEPS(INTEL_ATOM_GRACEMONT,     X86_STEP_MAX,	RFDS | VMSCAPE),
1316 	VULNBL_INTEL_STEPS(INTEL_ATOM_TREMONT,	     X86_STEP_MAX,	MMIO | MMIO_SBDS | RFDS),
1317 	VULNBL_INTEL_STEPS(INTEL_ATOM_TREMONT_D,     X86_STEP_MAX,	MMIO | RFDS),
1318 	VULNBL_INTEL_STEPS(INTEL_ATOM_TREMONT_L,     X86_STEP_MAX,	MMIO | MMIO_SBDS | RFDS),
1319 	VULNBL_INTEL_STEPS(INTEL_ATOM_GOLDMONT,      X86_STEP_MAX,	RFDS),
1320 	VULNBL_INTEL_STEPS(INTEL_ATOM_GOLDMONT_D,    X86_STEP_MAX,	RFDS),
1321 	VULNBL_INTEL_STEPS(INTEL_ATOM_GOLDMONT_PLUS, X86_STEP_MAX,	RFDS),
1322 	VULNBL_INTEL_STEPS(INTEL_ATOM_CRESTMONT_X,   X86_STEP_MAX,	VMSCAPE),
1323 
1324 	VULNBL_AMD(0x15, RETBLEED),
1325 	VULNBL_AMD(0x16, RETBLEED),
1326 	VULNBL_AMD(0x17, RETBLEED | SMT_RSB | SRSO | VMSCAPE),
1327 	VULNBL_HYGON(0x18, RETBLEED | SMT_RSB | SRSO | VMSCAPE),
1328 	VULNBL_AMD(0x19, SRSO | TSA | VMSCAPE),
1329 	VULNBL_AMD(0x1a, SRSO | VMSCAPE),
1330 	{}
1331 };
1332 
1333 static bool __init cpu_matches(const struct x86_cpu_id *table, unsigned long which)
1334 {
1335 	const struct x86_cpu_id *m = x86_match_cpu(table);
1336 
1337 	return m && !!(m->driver_data & which);
1338 }
1339 
1340 u64 x86_read_arch_cap_msr(void)
1341 {
1342 	u64 x86_arch_cap_msr = 0;
1343 
1344 	if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1345 		rdmsrq(MSR_IA32_ARCH_CAPABILITIES, x86_arch_cap_msr);
1346 
1347 	return x86_arch_cap_msr;
1348 }
1349 
1350 static bool arch_cap_mmio_immune(u64 x86_arch_cap_msr)
1351 {
1352 	return (x86_arch_cap_msr & ARCH_CAP_FBSDP_NO &&
1353 		x86_arch_cap_msr & ARCH_CAP_PSDP_NO &&
1354 		x86_arch_cap_msr & ARCH_CAP_SBDR_SSDP_NO);
1355 }
1356 
1357 static bool __init vulnerable_to_rfds(u64 x86_arch_cap_msr)
1358 {
1359 	/* The "immunity" bit trumps everything else: */
1360 	if (x86_arch_cap_msr & ARCH_CAP_RFDS_NO)
1361 		return false;
1362 
1363 	/*
1364 	 * VMMs set ARCH_CAP_RFDS_CLEAR for processors not in the blacklist to
1365 	 * indicate that mitigation is needed because guest is running on a
1366 	 * vulnerable hardware or may migrate to such hardware:
1367 	 */
1368 	if (x86_arch_cap_msr & ARCH_CAP_RFDS_CLEAR)
1369 		return true;
1370 
1371 	/* Only consult the blacklist when there is no enumeration: */
1372 	return cpu_matches(cpu_vuln_blacklist, RFDS);
1373 }
1374 
1375 static bool __init vulnerable_to_its(u64 x86_arch_cap_msr)
1376 {
1377 	/* The "immunity" bit trumps everything else: */
1378 	if (x86_arch_cap_msr & ARCH_CAP_ITS_NO)
1379 		return false;
1380 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
1381 		return false;
1382 
1383 	/* None of the affected CPUs have BHI_CTRL */
1384 	if (boot_cpu_has(X86_FEATURE_BHI_CTRL))
1385 		return false;
1386 
1387 	/*
1388 	 * If a VMM did not expose ITS_NO, assume that a guest could
1389 	 * be running on a vulnerable hardware or may migrate to such
1390 	 * hardware.
1391 	 */
1392 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
1393 		return true;
1394 
1395 	if (cpu_matches(cpu_vuln_blacklist, ITS))
1396 		return true;
1397 
1398 	return false;
1399 }
1400 
1401 static struct x86_cpu_id cpu_latest_microcode[] = {
1402 #include "microcode/intel-ucode-defs.h"
1403 	{}
1404 };
1405 
1406 static bool __init cpu_has_old_microcode(void)
1407 {
1408 	const struct x86_cpu_id *m = x86_match_cpu(cpu_latest_microcode);
1409 
1410 	/* Give unknown CPUs a pass: */
1411 	if (!m) {
1412 		/* Intel CPUs should be in the list. Warn if not: */
1413 		if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1414 			pr_info("x86/CPU: Model not found in latest microcode list\n");
1415 		return false;
1416 	}
1417 
1418 	/*
1419 	 * Hosts usually lie to guests with a super high microcode
1420 	 * version. Just ignore what hosts tell guests:
1421 	 */
1422 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
1423 		return false;
1424 
1425 	/* Consider all debug microcode to be old: */
1426 	if (boot_cpu_data.microcode & BIT(31))
1427 		return true;
1428 
1429 	/* Give new microcode a pass: */
1430 	if (boot_cpu_data.microcode >= m->driver_data)
1431 		return false;
1432 
1433 	/* Uh oh, too old: */
1434 	return true;
1435 }
1436 
1437 static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
1438 {
1439 	u64 x86_arch_cap_msr = x86_read_arch_cap_msr();
1440 
1441 	if (cpu_has_old_microcode()) {
1442 		pr_warn("x86/CPU: Running old microcode\n");
1443 		setup_force_cpu_bug(X86_BUG_OLD_MICROCODE);
1444 		add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
1445 	}
1446 
1447 	/* Set ITLB_MULTIHIT bug if cpu is not in the whitelist and not mitigated */
1448 	if (!cpu_matches(cpu_vuln_whitelist, NO_ITLB_MULTIHIT) &&
1449 	    !(x86_arch_cap_msr & ARCH_CAP_PSCHANGE_MC_NO))
1450 		setup_force_cpu_bug(X86_BUG_ITLB_MULTIHIT);
1451 
1452 	if (cpu_matches(cpu_vuln_whitelist, NO_SPECULATION))
1453 		return;
1454 
1455 	setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
1456 
1457 	if (!cpu_matches(cpu_vuln_whitelist, NO_SPECTRE_V2)) {
1458 		setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
1459 		setup_force_cpu_bug(X86_BUG_SPECTRE_V2_USER);
1460 	}
1461 
1462 	if (!cpu_matches(cpu_vuln_whitelist, NO_SSB) &&
1463 	    !(x86_arch_cap_msr & ARCH_CAP_SSB_NO) &&
1464 	   !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
1465 		setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
1466 
1467 	/*
1468 	 * AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature
1469 	 * flag and protect from vendor-specific bugs via the whitelist.
1470 	 *
1471 	 * Don't use AutoIBRS when SNP is enabled because it degrades host
1472 	 * userspace indirect branch performance.
1473 	 */
1474 	if ((x86_arch_cap_msr & ARCH_CAP_IBRS_ALL) ||
1475 	    (cpu_has(c, X86_FEATURE_AUTOIBRS) &&
1476 	     !cpu_feature_enabled(X86_FEATURE_SEV_SNP))) {
1477 		setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
1478 		if (!cpu_matches(cpu_vuln_whitelist, NO_EIBRS_PBRSB) &&
1479 		    !(x86_arch_cap_msr & ARCH_CAP_PBRSB_NO))
1480 			setup_force_cpu_bug(X86_BUG_EIBRS_PBRSB);
1481 	}
1482 
1483 	if (!cpu_matches(cpu_vuln_whitelist, NO_MDS) &&
1484 	    !(x86_arch_cap_msr & ARCH_CAP_MDS_NO)) {
1485 		setup_force_cpu_bug(X86_BUG_MDS);
1486 		if (cpu_matches(cpu_vuln_whitelist, MSBDS_ONLY))
1487 			setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
1488 	}
1489 
1490 	if (!cpu_matches(cpu_vuln_whitelist, NO_SWAPGS))
1491 		setup_force_cpu_bug(X86_BUG_SWAPGS);
1492 
1493 	/*
1494 	 * When the CPU is not mitigated for TAA (TAA_NO=0) set TAA bug when:
1495 	 *	- TSX is supported or
1496 	 *	- TSX_CTRL is present
1497 	 *
1498 	 * TSX_CTRL check is needed for cases when TSX could be disabled before
1499 	 * the kernel boot e.g. kexec.
1500 	 * TSX_CTRL check alone is not sufficient for cases when the microcode
1501 	 * update is not present or running as guest that don't get TSX_CTRL.
1502 	 */
1503 	if (!(x86_arch_cap_msr & ARCH_CAP_TAA_NO) &&
1504 	    (cpu_has(c, X86_FEATURE_RTM) ||
1505 	     (x86_arch_cap_msr & ARCH_CAP_TSX_CTRL_MSR)))
1506 		setup_force_cpu_bug(X86_BUG_TAA);
1507 
1508 	/*
1509 	 * SRBDS affects CPUs which support RDRAND or RDSEED and are listed
1510 	 * in the vulnerability blacklist.
1511 	 *
1512 	 * Some of the implications and mitigation of Shared Buffers Data
1513 	 * Sampling (SBDS) are similar to SRBDS. Give SBDS same treatment as
1514 	 * SRBDS.
1515 	 */
1516 	if ((cpu_has(c, X86_FEATURE_RDRAND) ||
1517 	     cpu_has(c, X86_FEATURE_RDSEED)) &&
1518 	    cpu_matches(cpu_vuln_blacklist, SRBDS | MMIO_SBDS))
1519 		    setup_force_cpu_bug(X86_BUG_SRBDS);
1520 
1521 	/*
1522 	 * Processor MMIO Stale Data bug enumeration
1523 	 *
1524 	 * Affected CPU list is generally enough to enumerate the vulnerability,
1525 	 * but for virtualization case check for ARCH_CAP MSR bits also, VMM may
1526 	 * not want the guest to enumerate the bug.
1527 	 */
1528 	if (!arch_cap_mmio_immune(x86_arch_cap_msr)) {
1529 		if (cpu_matches(cpu_vuln_blacklist, MMIO))
1530 			setup_force_cpu_bug(X86_BUG_MMIO_STALE_DATA);
1531 	}
1532 
1533 	if (!cpu_has(c, X86_FEATURE_BTC_NO)) {
1534 		if (cpu_matches(cpu_vuln_blacklist, RETBLEED) || (x86_arch_cap_msr & ARCH_CAP_RSBA))
1535 			setup_force_cpu_bug(X86_BUG_RETBLEED);
1536 	}
1537 
1538 	if (cpu_matches(cpu_vuln_blacklist, SMT_RSB))
1539 		setup_force_cpu_bug(X86_BUG_SMT_RSB);
1540 
1541 	if (!cpu_has(c, X86_FEATURE_SRSO_NO)) {
1542 		if (cpu_matches(cpu_vuln_blacklist, SRSO))
1543 			setup_force_cpu_bug(X86_BUG_SRSO);
1544 	}
1545 
1546 	/*
1547 	 * Check if CPU is vulnerable to GDS. If running in a virtual machine on
1548 	 * an affected processor, the VMM may have disabled the use of GATHER by
1549 	 * disabling AVX2. The only way to do this in HW is to clear XCR0[2],
1550 	 * which means that AVX will be disabled.
1551 	 */
1552 	if (cpu_matches(cpu_vuln_blacklist, GDS) && !(x86_arch_cap_msr & ARCH_CAP_GDS_NO) &&
1553 	    boot_cpu_has(X86_FEATURE_AVX))
1554 		setup_force_cpu_bug(X86_BUG_GDS);
1555 
1556 	if (vulnerable_to_rfds(x86_arch_cap_msr))
1557 		setup_force_cpu_bug(X86_BUG_RFDS);
1558 
1559 	/*
1560 	 * Intel parts with eIBRS are vulnerable to BHI attacks. Parts with
1561 	 * BHI_NO still need to use the BHI mitigation to prevent Intra-mode
1562 	 * attacks.  When virtualized, eIBRS could be hidden, assume vulnerable.
1563 	 */
1564 	if (!cpu_matches(cpu_vuln_whitelist, NO_BHI) &&
1565 	    (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED) ||
1566 	     boot_cpu_has(X86_FEATURE_HYPERVISOR)))
1567 		setup_force_cpu_bug(X86_BUG_BHI);
1568 
1569 	if (cpu_has(c, X86_FEATURE_AMD_IBPB) && !cpu_has(c, X86_FEATURE_AMD_IBPB_RET))
1570 		setup_force_cpu_bug(X86_BUG_IBPB_NO_RET);
1571 
1572 	if (vulnerable_to_its(x86_arch_cap_msr)) {
1573 		setup_force_cpu_bug(X86_BUG_ITS);
1574 		if (cpu_matches(cpu_vuln_blacklist, ITS_NATIVE_ONLY))
1575 			setup_force_cpu_bug(X86_BUG_ITS_NATIVE_ONLY);
1576 	}
1577 
1578 	if (c->x86_vendor == X86_VENDOR_AMD) {
1579 		if (!cpu_has(c, X86_FEATURE_TSA_SQ_NO) ||
1580 		    !cpu_has(c, X86_FEATURE_TSA_L1_NO)) {
1581 			if (cpu_matches(cpu_vuln_blacklist, TSA) ||
1582 			    /* Enable bug on Zen guests to allow for live migration. */
1583 			    (cpu_has(c, X86_FEATURE_HYPERVISOR) && cpu_has(c, X86_FEATURE_ZEN)))
1584 				setup_force_cpu_bug(X86_BUG_TSA);
1585 		}
1586 	}
1587 
1588 	/*
1589 	 * Set the bug only on bare-metal. A nested hypervisor should already be
1590 	 * deploying IBPB to isolate itself from nested guests.
1591 	 */
1592 	if (cpu_matches(cpu_vuln_blacklist, VMSCAPE) &&
1593 	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
1594 		setup_force_cpu_bug(X86_BUG_VMSCAPE);
1595 
1596 	if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
1597 		return;
1598 
1599 	/* Rogue Data Cache Load? No! */
1600 	if (x86_arch_cap_msr & ARCH_CAP_RDCL_NO)
1601 		return;
1602 
1603 	setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
1604 
1605 	if (cpu_matches(cpu_vuln_whitelist, NO_L1TF))
1606 		return;
1607 
1608 	setup_force_cpu_bug(X86_BUG_L1TF);
1609 }
1610 
1611 /*
1612  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1613  * unfortunately, that's not true in practice because of early VIA
1614  * chips and (more importantly) broken virtualizers that are not easy
1615  * to detect. In the latter case it doesn't even *fail* reliably, so
1616  * probing for it doesn't even work. Disable it completely on 32-bit
1617  * unless we can find a reliable way to detect all the broken cases.
1618  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
1619  */
1620 static void detect_nopl(void)
1621 {
1622 #ifdef CONFIG_X86_32
1623 	setup_clear_cpu_cap(X86_FEATURE_NOPL);
1624 #else
1625 	setup_force_cpu_cap(X86_FEATURE_NOPL);
1626 #endif
1627 }
1628 
1629 static inline bool parse_set_clear_cpuid(char *arg, bool set)
1630 {
1631 	char *opt;
1632 	int taint = 0;
1633 
1634 	while (arg) {
1635 		bool found __maybe_unused = false;
1636 		unsigned int bit;
1637 
1638 		opt = strsep(&arg, ",");
1639 
1640 		/*
1641 		 * Handle naked numbers first for feature flags which don't
1642 		 * have names. It doesn't make sense for a bug not to have a
1643 		 * name so don't handle bug flags here.
1644 		 */
1645 		if (!kstrtouint(opt, 10, &bit)) {
1646 			if (bit < NCAPINTS * 32) {
1647 
1648 				if (set) {
1649 					pr_warn("setcpuid: force-enabling CPU feature flag:");
1650 					setup_force_cpu_cap(bit);
1651 				} else {
1652 					pr_warn("clearcpuid: force-disabling CPU feature flag:");
1653 					setup_clear_cpu_cap(bit);
1654 				}
1655 				/* empty-string, i.e., ""-defined feature flags */
1656 				if (!x86_cap_flags[bit])
1657 					pr_cont(" %d:%d\n", bit >> 5, bit & 31);
1658 				else
1659 					pr_cont(" %s\n", x86_cap_flags[bit]);
1660 
1661 				taint++;
1662 			}
1663 			/*
1664 			 * The assumption is that there are no feature names with only
1665 			 * numbers in the name thus go to the next argument.
1666 			 */
1667 			continue;
1668 		}
1669 
1670 		for (bit = 0; bit < 32 * (NCAPINTS + NBUGINTS); bit++) {
1671 			const char *flag;
1672 			const char *kind;
1673 
1674 			if (bit < 32 * NCAPINTS) {
1675 				flag = x86_cap_flags[bit];
1676 				kind = "feature";
1677 			} else {
1678 				kind = "bug";
1679 				flag = x86_bug_flags[bit - (32 * NCAPINTS)];
1680 			}
1681 
1682 			if (!flag)
1683 				continue;
1684 
1685 			if (strcmp(flag, opt))
1686 				continue;
1687 
1688 			if (set) {
1689 				pr_warn("setcpuid: force-enabling CPU %s flag: %s\n",
1690 					kind, flag);
1691 				setup_force_cpu_cap(bit);
1692 			} else {
1693 				pr_warn("clearcpuid: force-disabling CPU %s flag: %s\n",
1694 					kind, flag);
1695 				setup_clear_cpu_cap(bit);
1696 			}
1697 			taint++;
1698 			found = true;
1699 			break;
1700 		}
1701 
1702 		if (!found)
1703 			pr_warn("%s: unknown CPU flag: %s", set ? "setcpuid" : "clearcpuid", opt);
1704 	}
1705 
1706 	return taint;
1707 }
1708 
1709 
1710 /*
1711  * We parse cpu parameters early because fpu__init_system() is executed
1712  * before parse_early_param().
1713  */
1714 static void __init cpu_parse_early_param(void)
1715 {
1716 	bool cpuid_taint = false;
1717 	char arg[128];
1718 	int arglen;
1719 
1720 #ifdef CONFIG_X86_32
1721 	if (cmdline_find_option_bool(boot_command_line, "no387"))
1722 #ifdef CONFIG_MATH_EMULATION
1723 		setup_clear_cpu_cap(X86_FEATURE_FPU);
1724 #else
1725 		pr_err("Option 'no387' required CONFIG_MATH_EMULATION enabled.\n");
1726 #endif
1727 
1728 	if (cmdline_find_option_bool(boot_command_line, "nofxsr"))
1729 		setup_clear_cpu_cap(X86_FEATURE_FXSR);
1730 #endif
1731 
1732 	if (cmdline_find_option_bool(boot_command_line, "noxsave"))
1733 		setup_clear_cpu_cap(X86_FEATURE_XSAVE);
1734 
1735 	if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
1736 		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
1737 
1738 	if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
1739 		setup_clear_cpu_cap(X86_FEATURE_XSAVES);
1740 
1741 	if (cmdline_find_option_bool(boot_command_line, "nousershstk"))
1742 		setup_clear_cpu_cap(X86_FEATURE_USER_SHSTK);
1743 
1744 	/* Minimize the gap between FRED is available and available but disabled. */
1745 	arglen = cmdline_find_option(boot_command_line, "fred", arg, sizeof(arg));
1746 	if (arglen != 2 || strncmp(arg, "on", 2))
1747 		setup_clear_cpu_cap(X86_FEATURE_FRED);
1748 
1749 	arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg));
1750 	if (arglen > 0)
1751 		cpuid_taint |= parse_set_clear_cpuid(arg, false);
1752 
1753 	arglen = cmdline_find_option(boot_command_line, "setcpuid", arg, sizeof(arg));
1754 	if (arglen > 0)
1755 		cpuid_taint |= parse_set_clear_cpuid(arg, true);
1756 
1757 	if (cpuid_taint) {
1758 		pr_warn("!!! setcpuid=/clearcpuid= in use, this is for TESTING ONLY, may break things horribly. Tainting kernel.\n");
1759 		add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
1760 	}
1761 }
1762 
1763 /*
1764  * Do minimum CPU detection early.
1765  * Fields really needed: vendor, cpuid_level, family, model, mask,
1766  * cache alignment.
1767  * The others are not touched to avoid unwanted side effects.
1768  *
1769  * WARNING: this function is only called on the boot CPU.  Don't add code
1770  * here that is supposed to run on all CPUs.
1771  */
1772 static void __init early_identify_cpu(struct cpuinfo_x86 *c)
1773 {
1774 	memset(&c->x86_capability, 0, sizeof(c->x86_capability));
1775 	c->extended_cpuid_level = 0;
1776 
1777 	if (!cpuid_feature())
1778 		identify_cpu_without_cpuid(c);
1779 
1780 	/* cyrix could have cpuid enabled via c_identify()*/
1781 	if (cpuid_feature()) {
1782 		cpu_detect(c);
1783 		get_cpu_vendor(c);
1784 		intel_unlock_cpuid_leafs(c);
1785 		get_cpu_cap(c);
1786 		setup_force_cpu_cap(X86_FEATURE_CPUID);
1787 		get_cpu_address_sizes(c);
1788 		cpu_parse_early_param();
1789 
1790 		cpu_init_topology(c);
1791 
1792 		if (this_cpu->c_early_init)
1793 			this_cpu->c_early_init(c);
1794 
1795 		c->cpu_index = 0;
1796 		filter_cpuid_features(c, false);
1797 		check_cpufeature_deps(c);
1798 
1799 		if (this_cpu->c_bsp_init)
1800 			this_cpu->c_bsp_init(c);
1801 	} else {
1802 		setup_clear_cpu_cap(X86_FEATURE_CPUID);
1803 		get_cpu_address_sizes(c);
1804 		cpu_init_topology(c);
1805 	}
1806 
1807 	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
1808 
1809 	cpu_set_bug_bits(c);
1810 
1811 	sld_setup(c);
1812 
1813 #ifdef CONFIG_X86_32
1814 	/*
1815 	 * Regardless of whether PCID is enumerated, the SDM says
1816 	 * that it can't be enabled in 32-bit mode.
1817 	 */
1818 	setup_clear_cpu_cap(X86_FEATURE_PCID);
1819 
1820 	/*
1821 	 * Never use SYSCALL on a 32-bit kernel
1822 	 */
1823 	setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
1824 #endif
1825 
1826 	/*
1827 	 * Later in the boot process pgtable_l5_enabled() relies on
1828 	 * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not
1829 	 * enabled by this point we need to clear the feature bit to avoid
1830 	 * false-positives at the later stage.
1831 	 *
1832 	 * pgtable_l5_enabled() can be false here for several reasons:
1833 	 *  - 5-level paging is disabled compile-time;
1834 	 *  - it's 32-bit kernel;
1835 	 *  - machine doesn't support 5-level paging;
1836 	 *  - user specified 'no5lvl' in kernel command line.
1837 	 */
1838 	if (!pgtable_l5_enabled())
1839 		setup_clear_cpu_cap(X86_FEATURE_LA57);
1840 
1841 	detect_nopl();
1842 	mca_bsp_init(c);
1843 }
1844 
1845 void __init init_cpu_devs(void)
1846 {
1847 	const struct cpu_dev *const *cdev;
1848 	int count = 0;
1849 
1850 	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
1851 		const struct cpu_dev *cpudev = *cdev;
1852 
1853 		if (count >= X86_VENDOR_NUM)
1854 			break;
1855 		cpu_devs[count] = cpudev;
1856 		count++;
1857 	}
1858 }
1859 
1860 void __init early_cpu_init(void)
1861 {
1862 #ifdef CONFIG_PROCESSOR_SELECT
1863 	unsigned int i, j;
1864 
1865 	pr_info("KERNEL supported cpus:\n");
1866 #endif
1867 
1868 	init_cpu_devs();
1869 
1870 #ifdef CONFIG_PROCESSOR_SELECT
1871 	for (i = 0; i < X86_VENDOR_NUM && cpu_devs[i]; i++) {
1872 		for (j = 0; j < 2; j++) {
1873 			if (!cpu_devs[i]->c_ident[j])
1874 				continue;
1875 			pr_info("  %s %s\n", cpu_devs[i]->c_vendor,
1876 				cpu_devs[i]->c_ident[j]);
1877 		}
1878 	}
1879 #endif
1880 
1881 	early_identify_cpu(&boot_cpu_data);
1882 }
1883 
1884 static bool detect_null_seg_behavior(void)
1885 {
1886 	/*
1887 	 * Empirically, writing zero to a segment selector on AMD does
1888 	 * not clear the base, whereas writing zero to a segment
1889 	 * selector on Intel does clear the base.  Intel's behavior
1890 	 * allows slightly faster context switches in the common case
1891 	 * where GS is unused by the prev and next threads.
1892 	 *
1893 	 * Since neither vendor documents this anywhere that I can see,
1894 	 * detect it directly instead of hard-coding the choice by
1895 	 * vendor.
1896 	 *
1897 	 * I've designated AMD's behavior as the "bug" because it's
1898 	 * counterintuitive and less friendly.
1899 	 */
1900 
1901 	unsigned long old_base, tmp;
1902 	rdmsrq(MSR_FS_BASE, old_base);
1903 	wrmsrq(MSR_FS_BASE, 1);
1904 	loadsegment(fs, 0);
1905 	rdmsrq(MSR_FS_BASE, tmp);
1906 	wrmsrq(MSR_FS_BASE, old_base);
1907 	return tmp == 0;
1908 }
1909 
1910 void check_null_seg_clears_base(struct cpuinfo_x86 *c)
1911 {
1912 	/* BUG_NULL_SEG is only relevant with 64bit userspace */
1913 	if (!IS_ENABLED(CONFIG_X86_64))
1914 		return;
1915 
1916 	if (cpu_has(c, X86_FEATURE_NULL_SEL_CLR_BASE))
1917 		return;
1918 
1919 	/*
1920 	 * CPUID bit above wasn't set. If this kernel is still running
1921 	 * as a HV guest, then the HV has decided not to advertize
1922 	 * that CPUID bit for whatever reason.	For example, one
1923 	 * member of the migration pool might be vulnerable.  Which
1924 	 * means, the bug is present: set the BUG flag and return.
1925 	 */
1926 	if (cpu_has(c, X86_FEATURE_HYPERVISOR)) {
1927 		set_cpu_bug(c, X86_BUG_NULL_SEG);
1928 		return;
1929 	}
1930 
1931 	/*
1932 	 * Zen2 CPUs also have this behaviour, but no CPUID bit.
1933 	 * 0x18 is the respective family for Hygon.
1934 	 */
1935 	if ((c->x86 == 0x17 || c->x86 == 0x18) &&
1936 	    detect_null_seg_behavior())
1937 		return;
1938 
1939 	/* All the remaining ones are affected */
1940 	set_cpu_bug(c, X86_BUG_NULL_SEG);
1941 }
1942 
1943 static void generic_identify(struct cpuinfo_x86 *c)
1944 {
1945 	c->extended_cpuid_level = 0;
1946 
1947 	if (!cpuid_feature())
1948 		identify_cpu_without_cpuid(c);
1949 
1950 	/* cyrix could have cpuid enabled via c_identify()*/
1951 	if (!cpuid_feature())
1952 		return;
1953 
1954 	cpu_detect(c);
1955 
1956 	get_cpu_vendor(c);
1957 	intel_unlock_cpuid_leafs(c);
1958 	get_cpu_cap(c);
1959 
1960 	get_cpu_address_sizes(c);
1961 
1962 	get_model_name(c); /* Default name */
1963 
1964 	/*
1965 	 * ESPFIX is a strange bug.  All real CPUs have it.  Paravirt
1966 	 * systems that run Linux at CPL > 0 may or may not have the
1967 	 * issue, but, even if they have the issue, there's absolutely
1968 	 * nothing we can do about it because we can't use the real IRET
1969 	 * instruction.
1970 	 *
1971 	 * NB: For the time being, only 32-bit kernels support
1972 	 * X86_BUG_ESPFIX as such.  64-bit kernels directly choose
1973 	 * whether to apply espfix using paravirt hooks.  If any
1974 	 * non-paravirt system ever shows up that does *not* have the
1975 	 * ESPFIX issue, we can change this.
1976 	 */
1977 #ifdef CONFIG_X86_32
1978 	set_cpu_bug(c, X86_BUG_ESPFIX);
1979 #endif
1980 }
1981 
1982 /*
1983  * This does the hard work of actually picking apart the CPU stuff...
1984  */
1985 static void identify_cpu(struct cpuinfo_x86 *c)
1986 {
1987 	int i;
1988 
1989 	c->loops_per_jiffy = loops_per_jiffy;
1990 	c->x86_cache_size = 0;
1991 	c->x86_vendor = X86_VENDOR_UNKNOWN;
1992 	c->x86_model = c->x86_stepping = 0;	/* So far unknown... */
1993 	c->x86_vendor_id[0] = '\0'; /* Unset */
1994 	c->x86_model_id[0] = '\0';  /* Unset */
1995 #ifdef CONFIG_X86_64
1996 	c->x86_clflush_size = 64;
1997 	c->x86_phys_bits = 36;
1998 	c->x86_virt_bits = 48;
1999 #else
2000 	c->cpuid_level = -1;	/* CPUID not detected */
2001 	c->x86_clflush_size = 32;
2002 	c->x86_phys_bits = 32;
2003 	c->x86_virt_bits = 32;
2004 #endif
2005 	c->x86_cache_alignment = c->x86_clflush_size;
2006 	memset(&c->x86_capability, 0, sizeof(c->x86_capability));
2007 #ifdef CONFIG_X86_VMX_FEATURE_NAMES
2008 	memset(&c->vmx_capability, 0, sizeof(c->vmx_capability));
2009 #endif
2010 
2011 	generic_identify(c);
2012 
2013 	cpu_parse_topology(c);
2014 
2015 	if (this_cpu->c_identify)
2016 		this_cpu->c_identify(c);
2017 
2018 	/* Clear/Set all flags overridden by options, after probe */
2019 	apply_forced_caps(c);
2020 
2021 	/*
2022 	 * Set default APIC and TSC_DEADLINE MSR fencing flag. AMD and
2023 	 * Hygon will clear it in ->c_init() below.
2024 	 */
2025 	set_cpu_cap(c, X86_FEATURE_APIC_MSRS_FENCE);
2026 
2027 	/*
2028 	 * Vendor-specific initialization.  In this section we
2029 	 * canonicalize the feature flags, meaning if there are
2030 	 * features a certain CPU supports which CPUID doesn't
2031 	 * tell us, CPUID claiming incorrect flags, or other bugs,
2032 	 * we handle them here.
2033 	 *
2034 	 * At the end of this section, c->x86_capability better
2035 	 * indicate the features this CPU genuinely supports!
2036 	 */
2037 	if (this_cpu->c_init)
2038 		this_cpu->c_init(c);
2039 
2040 	bus_lock_init();
2041 
2042 	/* Disable the PN if appropriate */
2043 	squash_the_stupid_serial_number(c);
2044 
2045 	setup_smep(c);
2046 	setup_smap(c);
2047 	setup_umip(c);
2048 	setup_lass(c);
2049 
2050 	/* Enable FSGSBASE instructions if available. */
2051 	if (cpu_has(c, X86_FEATURE_FSGSBASE)) {
2052 		cr4_set_bits(X86_CR4_FSGSBASE);
2053 		elf_hwcap2 |= HWCAP2_FSGSBASE;
2054 	}
2055 
2056 	/*
2057 	 * The vendor-specific functions might have changed features.
2058 	 * Now we do "generic changes."
2059 	 */
2060 
2061 	/* Filter out anything that depends on CPUID levels we don't have */
2062 	filter_cpuid_features(c, true);
2063 
2064 	/* Check for unmet dependencies based on the CPUID dependency table */
2065 	check_cpufeature_deps(c);
2066 
2067 	/* If the model name is still unset, do table lookup. */
2068 	if (!c->x86_model_id[0]) {
2069 		const char *p;
2070 		p = table_lookup_model(c);
2071 		if (p)
2072 			strcpy(c->x86_model_id, p);
2073 		else
2074 			/* Last resort... */
2075 			sprintf(c->x86_model_id, "%02x/%02x",
2076 				c->x86, c->x86_model);
2077 	}
2078 
2079 	x86_init_rdrand(c);
2080 	setup_pku(c);
2081 	setup_cet(c);
2082 
2083 	/*
2084 	 * Clear/Set all flags overridden by options, need do it
2085 	 * before following smp all cpus cap AND.
2086 	 */
2087 	apply_forced_caps(c);
2088 
2089 	/*
2090 	 * On SMP, boot_cpu_data holds the common feature set between
2091 	 * all CPUs; so make sure that we indicate which features are
2092 	 * common between the CPUs.  The first time this routine gets
2093 	 * executed, c == &boot_cpu_data.
2094 	 */
2095 	if (c != &boot_cpu_data) {
2096 		/* AND the already accumulated flags with these */
2097 		for (i = 0; i < NCAPINTS; i++)
2098 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
2099 
2100 		/* OR, i.e. replicate the bug flags */
2101 		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
2102 			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
2103 	}
2104 
2105 	ppin_init(c);
2106 
2107 	/* Init Machine Check Exception if available. */
2108 	mcheck_cpu_init(c);
2109 
2110 	numa_add_cpu(smp_processor_id());
2111 }
2112 
2113 /*
2114  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
2115  * on 32-bit kernels:
2116  */
2117 #ifdef CONFIG_X86_32
2118 void enable_sep_cpu(void)
2119 {
2120 	struct tss_struct *tss;
2121 	int cpu;
2122 
2123 	if (!boot_cpu_has(X86_FEATURE_SEP))
2124 		return;
2125 
2126 	cpu = get_cpu();
2127 	tss = &per_cpu(cpu_tss_rw, cpu);
2128 
2129 	/*
2130 	 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
2131 	 * see the big comment in struct x86_hw_tss's definition.
2132 	 */
2133 
2134 	tss->x86_tss.ss1 = __KERNEL_CS;
2135 	wrmsrq(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1);
2136 	wrmsrq(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1));
2137 	wrmsrq(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32);
2138 
2139 	put_cpu();
2140 }
2141 #endif
2142 
2143 static __init void identify_boot_cpu(void)
2144 {
2145 	identify_cpu(&boot_cpu_data);
2146 	if (HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT))
2147 		pr_info("CET detected: Indirect Branch Tracking enabled\n");
2148 #ifdef CONFIG_X86_32
2149 	enable_sep_cpu();
2150 #endif
2151 	cpu_detect_tlb(&boot_cpu_data);
2152 	setup_cr_pinning();
2153 
2154 	tsx_init();
2155 	tdx_init();
2156 	lkgs_init();
2157 }
2158 
2159 void identify_secondary_cpu(unsigned int cpu)
2160 {
2161 	struct cpuinfo_x86 *c = &cpu_data(cpu);
2162 
2163 	/* Copy boot_cpu_data only on the first bringup */
2164 	if (!c->initialized)
2165 		*c = boot_cpu_data;
2166 	c->cpu_index = cpu;
2167 
2168 	identify_cpu(c);
2169 #ifdef CONFIG_X86_32
2170 	enable_sep_cpu();
2171 #endif
2172 	x86_spec_ctrl_setup_ap();
2173 	update_srbds_msr();
2174 	if (boot_cpu_has_bug(X86_BUG_GDS))
2175 		update_gds_msr();
2176 
2177 	tsx_ap_init();
2178 	c->initialized = true;
2179 }
2180 
2181 void print_cpu_info(struct cpuinfo_x86 *c)
2182 {
2183 	const char *vendor = NULL;
2184 
2185 	if (c->x86_vendor < X86_VENDOR_NUM) {
2186 		vendor = this_cpu->c_vendor;
2187 	} else {
2188 		if (c->cpuid_level >= 0)
2189 			vendor = c->x86_vendor_id;
2190 	}
2191 
2192 	if (vendor && !strstr(c->x86_model_id, vendor))
2193 		pr_cont("%s ", vendor);
2194 
2195 	if (c->x86_model_id[0])
2196 		pr_cont("%s", c->x86_model_id);
2197 	else
2198 		pr_cont("%d86", c->x86);
2199 
2200 	pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
2201 
2202 	if (c->x86_stepping || c->cpuid_level >= 0)
2203 		pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
2204 	else
2205 		pr_cont(")\n");
2206 }
2207 
2208 /*
2209  * clearcpuid= and setcpuid= were already parsed in cpu_parse_early_param().
2210  * These dummy functions prevent them from becoming an environment variable for
2211  * init.
2212  */
2213 
2214 static __init int setup_clearcpuid(char *arg)
2215 {
2216 	return 1;
2217 }
2218 __setup("clearcpuid=", setup_clearcpuid);
2219 
2220 static __init int setup_setcpuid(char *arg)
2221 {
2222 	return 1;
2223 }
2224 __setup("setcpuid=", setup_setcpuid);
2225 
2226 DEFINE_PER_CPU_CACHE_HOT(struct task_struct *, current_task) = &init_task;
2227 EXPORT_PER_CPU_SYMBOL(current_task);
2228 EXPORT_PER_CPU_SYMBOL(const_current_task);
2229 
2230 DEFINE_PER_CPU_CACHE_HOT(int, __preempt_count) = INIT_PREEMPT_COUNT;
2231 EXPORT_PER_CPU_SYMBOL(__preempt_count);
2232 
2233 DEFINE_PER_CPU_CACHE_HOT(unsigned long, cpu_current_top_of_stack) = TOP_OF_INIT_STACK;
2234 
2235 #ifdef CONFIG_X86_64
2236 /*
2237  * Note: Do not make this dependant on CONFIG_MITIGATION_CALL_DEPTH_TRACKING
2238  * so that this space is reserved in the hot cache section even when the
2239  * mitigation is disabled.
2240  */
2241 DEFINE_PER_CPU_CACHE_HOT(u64, __x86_call_depth);
2242 EXPORT_PER_CPU_SYMBOL(__x86_call_depth);
2243 
2244 static void wrmsrq_cstar(unsigned long val)
2245 {
2246 	/*
2247 	 * Intel CPUs do not support 32-bit SYSCALL. Writing to MSR_CSTAR
2248 	 * is so far ignored by the CPU, but raises a #VE trap in a TDX
2249 	 * guest. Avoid the pointless write on all Intel CPUs.
2250 	 */
2251 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
2252 		wrmsrq(MSR_CSTAR, val);
2253 }
2254 
2255 static inline void idt_syscall_init(void)
2256 {
2257 	wrmsrq(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
2258 
2259 	if (ia32_enabled()) {
2260 		wrmsrq_cstar((unsigned long)entry_SYSCALL_compat);
2261 		/*
2262 		 * This only works on Intel CPUs.
2263 		 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
2264 		 * This does not cause SYSENTER to jump to the wrong location, because
2265 		 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
2266 		 */
2267 		wrmsrq_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
2268 		wrmsrq_safe(MSR_IA32_SYSENTER_ESP,
2269 			    (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1));
2270 		wrmsrq_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
2271 	} else {
2272 		wrmsrq_cstar((unsigned long)entry_SYSCALL32_ignore);
2273 		wrmsrq_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
2274 		wrmsrq_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
2275 		wrmsrq_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
2276 	}
2277 
2278 	/*
2279 	 * Flags to clear on syscall; clear as much as possible
2280 	 * to minimize user space-kernel interference.
2281 	 */
2282 	wrmsrq(MSR_SYSCALL_MASK,
2283 	       X86_EFLAGS_CF|X86_EFLAGS_PF|X86_EFLAGS_AF|
2284 	       X86_EFLAGS_ZF|X86_EFLAGS_SF|X86_EFLAGS_TF|
2285 	       X86_EFLAGS_IF|X86_EFLAGS_DF|X86_EFLAGS_OF|
2286 	       X86_EFLAGS_IOPL|X86_EFLAGS_NT|X86_EFLAGS_RF|
2287 	       X86_EFLAGS_AC|X86_EFLAGS_ID);
2288 }
2289 
2290 /* May not be marked __init: used by software suspend */
2291 void syscall_init(void)
2292 {
2293 	/* The default user and kernel segments */
2294 	wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
2295 
2296 	/*
2297 	 * Except the IA32_STAR MSR, there is NO need to setup SYSCALL and
2298 	 * SYSENTER MSRs for FRED, because FRED uses the ring 3 FRED
2299 	 * entrypoint for SYSCALL and SYSENTER, and ERETU is the only legit
2300 	 * instruction to return to ring 3 (both sysexit and sysret cause
2301 	 * #UD when FRED is enabled).
2302 	 */
2303 	if (!cpu_feature_enabled(X86_FEATURE_FRED))
2304 		idt_syscall_init();
2305 }
2306 #endif /* CONFIG_X86_64 */
2307 
2308 #ifdef CONFIG_STACKPROTECTOR
2309 DEFINE_PER_CPU_CACHE_HOT(unsigned long, __stack_chk_guard);
2310 #ifndef CONFIG_SMP
2311 EXPORT_PER_CPU_SYMBOL(__stack_chk_guard);
2312 #endif
2313 #endif
2314 
2315 static void initialize_debug_regs(void)
2316 {
2317 	/* Control register first -- to make sure everything is disabled. */
2318 	set_debugreg(DR7_FIXED_1, 7);
2319 	set_debugreg(DR6_RESERVED, 6);
2320 	/* dr5 and dr4 don't exist */
2321 	set_debugreg(0, 3);
2322 	set_debugreg(0, 2);
2323 	set_debugreg(0, 1);
2324 	set_debugreg(0, 0);
2325 }
2326 
2327 #ifdef CONFIG_KGDB
2328 /*
2329  * Restore debug regs if using kgdbwait and you have a kernel debugger
2330  * connection established.
2331  */
2332 static void dbg_restore_debug_regs(void)
2333 {
2334 	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
2335 		arch_kgdb_ops.correct_hw_break();
2336 }
2337 #else /* ! CONFIG_KGDB */
2338 #define dbg_restore_debug_regs()
2339 #endif /* ! CONFIG_KGDB */
2340 
2341 static inline void setup_getcpu(int cpu)
2342 {
2343 	unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
2344 	struct desc_struct d = { };
2345 
2346 	if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID))
2347 		wrmsrq(MSR_TSC_AUX, cpudata);
2348 
2349 	/* Store CPU and node number in limit. */
2350 	d.limit0 = cpudata;
2351 	d.limit1 = cpudata >> 16;
2352 
2353 	d.type = 5;		/* RO data, expand down, accessed */
2354 	d.dpl = 3;		/* Visible to user code */
2355 	d.s = 1;		/* Not a system segment */
2356 	d.p = 1;		/* Present */
2357 	d.d = 1;		/* 32-bit */
2358 
2359 	write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S);
2360 }
2361 
2362 #ifdef CONFIG_X86_64
2363 static inline void tss_setup_ist(struct tss_struct *tss)
2364 {
2365 	/* Set up the per-CPU TSS IST stacks */
2366 	tss->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
2367 	tss->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
2368 	tss->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
2369 	tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
2370 	/* Only mapped when SEV-ES is active */
2371 	tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC);
2372 }
2373 #else /* CONFIG_X86_64 */
2374 static inline void tss_setup_ist(struct tss_struct *tss) { }
2375 #endif /* !CONFIG_X86_64 */
2376 
2377 static inline void tss_setup_io_bitmap(struct tss_struct *tss)
2378 {
2379 	tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID;
2380 
2381 #ifdef CONFIG_X86_IOPL_IOPERM
2382 	tss->io_bitmap.prev_max = 0;
2383 	tss->io_bitmap.prev_sequence = 0;
2384 	memset(tss->io_bitmap.bitmap, 0xff, sizeof(tss->io_bitmap.bitmap));
2385 	/*
2386 	 * Invalidate the extra array entry past the end of the all
2387 	 * permission bitmap as required by the hardware.
2388 	 */
2389 	tss->io_bitmap.mapall[IO_BITMAP_LONGS] = ~0UL;
2390 #endif
2391 }
2392 
2393 /*
2394  * Setup everything needed to handle exceptions from the IDT, including the IST
2395  * exceptions which use paranoid_entry().
2396  */
2397 void cpu_init_exception_handling(bool boot_cpu)
2398 {
2399 	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
2400 	int cpu = raw_smp_processor_id();
2401 
2402 	/* paranoid_entry() gets the CPU number from the GDT */
2403 	setup_getcpu(cpu);
2404 
2405 	/* For IDT mode, IST vectors need to be set in TSS. */
2406 	if (!cpu_feature_enabled(X86_FEATURE_FRED))
2407 		tss_setup_ist(tss);
2408 	tss_setup_io_bitmap(tss);
2409 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
2410 
2411 	load_TR_desc();
2412 
2413 	/* GHCB needs to be setup to handle #VC. */
2414 	setup_ghcb();
2415 
2416 	if (cpu_feature_enabled(X86_FEATURE_FRED)) {
2417 		/* The boot CPU has enabled FRED during early boot */
2418 		if (!boot_cpu)
2419 			cpu_init_fred_exceptions();
2420 
2421 		cpu_init_fred_rsps();
2422 	} else {
2423 		load_current_idt();
2424 	}
2425 }
2426 
2427 void __init cpu_init_replace_early_idt(void)
2428 {
2429 	if (cpu_feature_enabled(X86_FEATURE_FRED))
2430 		cpu_init_fred_exceptions();
2431 	else
2432 		idt_setup_early_pf();
2433 }
2434 
2435 /*
2436  * cpu_init() initializes state that is per-CPU. Some data is already
2437  * initialized (naturally) in the bootstrap process, such as the GDT.  We
2438  * reload it nevertheless, this function acts as a 'CPU state barrier',
2439  * nothing should get across.
2440  */
2441 void cpu_init(void)
2442 {
2443 	struct task_struct *cur = current;
2444 	int cpu = raw_smp_processor_id();
2445 
2446 #ifdef CONFIG_NUMA
2447 	if (this_cpu_read(numa_node) == 0 &&
2448 	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
2449 		set_numa_node(early_cpu_to_node(cpu));
2450 #endif
2451 	pr_debug("Initializing CPU#%d\n", cpu);
2452 
2453 	if (IS_ENABLED(CONFIG_X86_64) || cpu_feature_enabled(X86_FEATURE_VME) ||
2454 	    boot_cpu_has(X86_FEATURE_TSC) || boot_cpu_has(X86_FEATURE_DE))
2455 		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
2456 
2457 	if (IS_ENABLED(CONFIG_X86_64)) {
2458 		loadsegment(fs, 0);
2459 		memset(cur->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
2460 		syscall_init();
2461 
2462 		wrmsrq(MSR_FS_BASE, 0);
2463 		wrmsrq(MSR_KERNEL_GS_BASE, 0);
2464 		barrier();
2465 
2466 		x2apic_setup();
2467 
2468 		intel_posted_msi_init();
2469 	}
2470 
2471 	mmgrab(&init_mm);
2472 	cur->active_mm = &init_mm;
2473 	BUG_ON(cur->mm);
2474 	initialize_tlbstate_and_flush();
2475 	enter_lazy_tlb(&init_mm, cur);
2476 
2477 	/*
2478 	 * sp0 points to the entry trampoline stack regardless of what task
2479 	 * is running.
2480 	 */
2481 	load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
2482 
2483 	load_mm_ldt(&init_mm);
2484 
2485 	initialize_debug_regs();
2486 	dbg_restore_debug_regs();
2487 
2488 	doublefault_init_cpu_tss();
2489 
2490 	if (is_uv_system())
2491 		uv_cpu_init();
2492 
2493 	load_fixmap_gdt(cpu);
2494 }
2495 
2496 #ifdef CONFIG_MICROCODE_LATE_LOADING
2497 /**
2498  * store_cpu_caps() - Store a snapshot of CPU capabilities
2499  * @curr_info: Pointer where to store it
2500  *
2501  * Returns: None
2502  */
2503 void store_cpu_caps(struct cpuinfo_x86 *curr_info)
2504 {
2505 	/* Reload CPUID max function as it might've changed. */
2506 	curr_info->cpuid_level = cpuid_eax(0);
2507 
2508 	/* Copy all capability leafs and pick up the synthetic ones. */
2509 	memcpy(&curr_info->x86_capability, &boot_cpu_data.x86_capability,
2510 	       sizeof(curr_info->x86_capability));
2511 
2512 	/* Get the hardware CPUID leafs */
2513 	get_cpu_cap(curr_info);
2514 }
2515 
2516 /**
2517  * microcode_check() - Check if any CPU capabilities changed after an update.
2518  * @prev_info:	CPU capabilities stored before an update.
2519  *
2520  * The microcode loader calls this upon late microcode load to recheck features,
2521  * only when microcode has been updated. Caller holds and CPU hotplug lock.
2522  *
2523  * Return: None
2524  */
2525 void microcode_check(struct cpuinfo_x86 *prev_info)
2526 {
2527 	struct cpuinfo_x86 curr_info;
2528 
2529 	perf_check_microcode();
2530 
2531 	amd_check_microcode();
2532 
2533 	store_cpu_caps(&curr_info);
2534 
2535 	if (!memcmp(&prev_info->x86_capability, &curr_info.x86_capability,
2536 		    sizeof(prev_info->x86_capability)))
2537 		return;
2538 
2539 	pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
2540 	pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
2541 }
2542 #endif
2543 
2544 /*
2545  * Invoked from core CPU hotplug code after hotplug operations
2546  */
2547 void arch_smt_update(void)
2548 {
2549 	/* Handle the speculative execution misfeatures */
2550 	cpu_bugs_smt_update();
2551 	/* Check whether IPI broadcasting can be enabled */
2552 	apic_smt_update();
2553 }
2554 
2555 void __init arch_cpu_finalize_init(void)
2556 {
2557 	struct cpuinfo_x86 *c = this_cpu_ptr(&cpu_info);
2558 
2559 	identify_boot_cpu();
2560 
2561 	select_idle_routine();
2562 
2563 	/*
2564 	 * identify_boot_cpu() initialized SMT support information, let the
2565 	 * core code know.
2566 	 */
2567 	cpu_smt_set_num_threads(__max_threads_per_core, __max_threads_per_core);
2568 
2569 	if (!IS_ENABLED(CONFIG_SMP)) {
2570 		pr_info("CPU: ");
2571 		print_cpu_info(&boot_cpu_data);
2572 	}
2573 
2574 	cpu_select_mitigations();
2575 
2576 	arch_smt_update();
2577 
2578 	if (IS_ENABLED(CONFIG_X86_32)) {
2579 		/*
2580 		 * Check whether this is a real i386 which is not longer
2581 		 * supported and fixup the utsname.
2582 		 */
2583 		if (boot_cpu_data.x86 < 4)
2584 			panic("Kernel requires i486+ for 'invlpg' and other features");
2585 
2586 		init_utsname()->machine[1] =
2587 			'0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
2588 	}
2589 
2590 	/*
2591 	 * Must be before alternatives because it might set or clear
2592 	 * feature bits.
2593 	 */
2594 	fpu__init_system();
2595 	fpu__init_cpu();
2596 
2597 	/*
2598 	 * This needs to follow the FPU initializtion, since EFI depends on it.
2599 	 */
2600 	if (efi_enabled(EFI_RUNTIME_SERVICES))
2601 		efi_enter_virtual_mode();
2602 
2603 	/*
2604 	 * Ensure that access to the per CPU representation has the initial
2605 	 * boot CPU configuration.
2606 	 */
2607 	*c = boot_cpu_data;
2608 	c->initialized = true;
2609 
2610 	alternative_instructions();
2611 
2612 	if (IS_ENABLED(CONFIG_X86_64)) {
2613 		USER_PTR_MAX = TASK_SIZE_MAX;
2614 
2615 		/*
2616 		 * Enable this when LAM is gated on LASS support
2617 		if (cpu_feature_enabled(X86_FEATURE_LAM))
2618 			USER_PTR_MAX = (1ul << 63) - PAGE_SIZE;
2619 		 */
2620 		runtime_const_init(ptr, USER_PTR_MAX);
2621 
2622 		/*
2623 		 * Make sure the first 2MB area is not mapped by huge pages
2624 		 * There are typically fixed size MTRRs in there and overlapping
2625 		 * MTRRs into large pages causes slow downs.
2626 		 *
2627 		 * Right now we don't do that with gbpages because there seems
2628 		 * very little benefit for that case.
2629 		 */
2630 		if (!direct_gbpages)
2631 			set_memory_4k((unsigned long)__va(0), 1);
2632 	} else {
2633 		fpu__init_check_bugs();
2634 	}
2635 
2636 	/*
2637 	 * This needs to be called before any devices perform DMA
2638 	 * operations that might use the SWIOTLB bounce buffers. It will
2639 	 * mark the bounce buffers as decrypted so that their usage will
2640 	 * not cause "plain-text" data to be decrypted when accessed. It
2641 	 * must be called after late_time_init() so that Hyper-V x86/x64
2642 	 * hypercalls work when the SWIOTLB bounce buffers are decrypted.
2643 	 */
2644 	mem_encrypt_init();
2645 }
2646