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