xref: /linux/arch/x86/kernel/cpu/common.c (revision 864c07de0c7dcbca58b61625dfa2e461ae63b242)
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 	struct msr val;
343 
344 	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
345 		return;
346 
347 	/* Disable processor serial number: */
348 
349 	rdmsrq(MSR_IA32_BBL_CR_CTL, val.q);
350 	val.l |= 0x200000;
351 	wrmsrq(MSR_IA32_BBL_CR_CTL, val.q);
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 
494 void cr4_update_irqsoff(unsigned long set, unsigned long clear)
495 {
496 	unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4);
497 
498 	lockdep_assert_irqs_disabled();
499 
500 	newval = (cr4 & ~clear) | set;
501 	if (newval != cr4) {
502 		this_cpu_write(cpu_tlbstate.cr4, newval);
503 		__write_cr4(newval);
504 	}
505 }
506 EXPORT_SYMBOL_FOR_KVM(cr4_update_irqsoff);
507 
508 /* Read the CR4 shadow. */
509 unsigned long cr4_read_shadow(void)
510 {
511 	return this_cpu_read(cpu_tlbstate.cr4);
512 }
513 EXPORT_SYMBOL_FOR_KVM(cr4_read_shadow);
514 
515 void cr4_init(void)
516 {
517 	unsigned long cr4 = __read_cr4();
518 
519 	if (boot_cpu_has(X86_FEATURE_PCID))
520 		cr4 |= X86_CR4_PCIDE;
521 	if (static_branch_likely(&cr_pinning))
522 		cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits;
523 
524 	__write_cr4(cr4);
525 
526 	/* Initialize cr4 shadow for this CPU. */
527 	this_cpu_write(cpu_tlbstate.cr4, cr4);
528 }
529 
530 /*
531  * Once CPU feature detection is finished (and boot params have been
532  * parsed), record any of the sensitive CR bits that are set, and
533  * enable CR pinning.
534  */
535 static void __init setup_cr_pinning(void)
536 {
537 	cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask;
538 	static_key_enable(&cr_pinning.key);
539 }
540 
541 static __init int x86_nofsgsbase_setup(char *arg)
542 {
543 	/* Require an exact match without trailing characters. */
544 	if (strlen(arg))
545 		return 0;
546 
547 	/* Do not emit a message if the feature is not present. */
548 	if (!boot_cpu_has(X86_FEATURE_FSGSBASE))
549 		return 1;
550 
551 	setup_clear_cpu_cap(X86_FEATURE_FSGSBASE);
552 	pr_info("FSGSBASE disabled via kernel command line\n");
553 	return 1;
554 }
555 __setup("nofsgsbase", x86_nofsgsbase_setup);
556 
557 /*
558  * Protection Keys are not available in 32-bit mode.
559  */
560 static bool pku_disabled;
561 
562 static __always_inline void setup_pku(struct cpuinfo_x86 *c)
563 {
564 	if (c == &boot_cpu_data) {
565 		if (pku_disabled || !cpu_feature_enabled(X86_FEATURE_PKU))
566 			return;
567 		/*
568 		 * Setting CR4.PKE will cause the X86_FEATURE_OSPKE cpuid
569 		 * bit to be set.  Enforce it.
570 		 */
571 		setup_force_cpu_cap(X86_FEATURE_OSPKE);
572 
573 	} else if (!cpu_feature_enabled(X86_FEATURE_OSPKE)) {
574 		return;
575 	}
576 
577 	cr4_set_bits(X86_CR4_PKE);
578 	/* Load the default PKRU value */
579 	pkru_write_default();
580 }
581 
582 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
583 static __init int setup_disable_pku(char *arg)
584 {
585 	/*
586 	 * Do not clear the X86_FEATURE_PKU bit.  All of the
587 	 * runtime checks are against OSPKE so clearing the
588 	 * bit does nothing.
589 	 *
590 	 * This way, we will see "pku" in cpuinfo, but not
591 	 * "ospke", which is exactly what we want.  It shows
592 	 * that the CPU has PKU, but the OS has not enabled it.
593 	 * This happens to be exactly how a system would look
594 	 * if we disabled the config option.
595 	 */
596 	pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
597 	pku_disabled = true;
598 	return 1;
599 }
600 __setup("nopku", setup_disable_pku);
601 #endif
602 
603 #ifdef CONFIG_X86_KERNEL_IBT
604 
605 __noendbr u64 ibt_save(bool disable)
606 {
607 	u64 msr = 0;
608 
609 	if (cpu_feature_enabled(X86_FEATURE_IBT)) {
610 		rdmsrq(MSR_IA32_S_CET, msr);
611 		if (disable)
612 			wrmsrq(MSR_IA32_S_CET, msr & ~CET_ENDBR_EN);
613 	}
614 
615 	return msr;
616 }
617 
618 __noendbr void ibt_restore(u64 save)
619 {
620 	u64 msr;
621 
622 	if (cpu_feature_enabled(X86_FEATURE_IBT)) {
623 		rdmsrq(MSR_IA32_S_CET, msr);
624 		msr &= ~CET_ENDBR_EN;
625 		msr |= (save & CET_ENDBR_EN);
626 		wrmsrq(MSR_IA32_S_CET, msr);
627 	}
628 }
629 
630 #endif
631 
632 static __always_inline void setup_cet(struct cpuinfo_x86 *c)
633 {
634 	bool user_shstk, kernel_ibt;
635 
636 	if (!IS_ENABLED(CONFIG_X86_CET))
637 		return;
638 
639 	kernel_ibt = HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT);
640 	user_shstk = cpu_feature_enabled(X86_FEATURE_SHSTK) &&
641 		     IS_ENABLED(CONFIG_X86_USER_SHADOW_STACK);
642 
643 	if (!kernel_ibt && !user_shstk)
644 		return;
645 
646 	if (user_shstk)
647 		set_cpu_cap(c, X86_FEATURE_USER_SHSTK);
648 
649 	if (kernel_ibt)
650 		wrmsrq(MSR_IA32_S_CET, CET_ENDBR_EN);
651 	else
652 		wrmsrq(MSR_IA32_S_CET, 0);
653 
654 	cr4_set_bits(X86_CR4_CET);
655 
656 	if (kernel_ibt && ibt_selftest()) {
657 		pr_err("IBT selftest: Failed!\n");
658 		wrmsrq(MSR_IA32_S_CET, 0);
659 		setup_clear_cpu_cap(X86_FEATURE_IBT);
660 	}
661 }
662 
663 __noendbr void cet_disable(void)
664 {
665 	if (!(cpu_feature_enabled(X86_FEATURE_IBT) ||
666 	      cpu_feature_enabled(X86_FEATURE_SHSTK)))
667 		return;
668 
669 	wrmsrq(MSR_IA32_S_CET, 0);
670 	wrmsrq(MSR_IA32_U_CET, 0);
671 }
672 
673 /*
674  * Some CPU features depend on higher CPUID levels, which may not always
675  * be available due to CPUID level capping or broken virtualization
676  * software.  Add those features to this table to auto-disable them.
677  */
678 struct cpuid_dependent_feature {
679 	u32 feature;
680 	u32 level;
681 };
682 
683 static const struct cpuid_dependent_feature
684 cpuid_dependent_features[] = {
685 	{ X86_FEATURE_MWAIT,		CPUID_LEAF_MWAIT },
686 	{ X86_FEATURE_DCA,		CPUID_LEAF_DCA },
687 	{ X86_FEATURE_XSAVE,		CPUID_LEAF_XSTATE },
688 	{ 0, 0 }
689 };
690 
691 static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
692 {
693 	const struct cpuid_dependent_feature *df;
694 
695 	for (df = cpuid_dependent_features; df->feature; df++) {
696 
697 		if (!cpu_has(c, df->feature))
698 			continue;
699 		/*
700 		 * Note: cpuid_level is set to -1 if unavailable, but
701 		 * extended_extended_level is set to 0 if unavailable
702 		 * and the legitimate extended levels are all negative
703 		 * when signed; hence the weird messing around with
704 		 * signs here...
705 		 */
706 		if (!((s32)df->level < 0 ?
707 		     (u32)df->level > (u32)c->extended_cpuid_level :
708 		     (s32)df->level > (s32)c->cpuid_level))
709 			continue;
710 
711 		clear_cpu_cap(c, df->feature);
712 		if (!warn)
713 			continue;
714 
715 		pr_warn("CPU: CPU feature %s disabled, no CPUID level 0x%x\n",
716 			x86_cap_flags[df->feature], df->level);
717 	}
718 }
719 
720 /*
721  * Naming convention should be: <Name> [(<Codename>)]
722  * This table only is used unless init_<vendor>() below doesn't set it;
723  * in particular, if CPUID levels 0x80000002..4 are supported, this
724  * isn't used
725  */
726 
727 /* Look up CPU names by table lookup. */
728 static const char *table_lookup_model(struct cpuinfo_x86 *c)
729 {
730 #ifdef CONFIG_X86_32
731 	const struct legacy_cpu_model_info *info;
732 
733 	if (c->x86_model >= 16)
734 		return NULL;	/* Range check */
735 
736 	if (!this_cpu)
737 		return NULL;
738 
739 	info = this_cpu->legacy_models;
740 
741 	while (info->family) {
742 		if (info->family == c->x86)
743 			return info->model_names[c->x86_model];
744 		info++;
745 	}
746 #endif
747 	return NULL;		/* Not found */
748 }
749 
750 /* Aligned to unsigned long to avoid split lock in atomic bitmap ops */
751 __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
752 __u32 cpu_caps_set[NCAPINTS + NBUGINTS] __aligned(sizeof(unsigned long));
753 
754 #ifdef CONFIG_X86_32
755 /* The 32-bit entry code needs to find cpu_entry_area. */
756 DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
757 #endif
758 
759 /* Load the original GDT from the per-cpu structure */
760 void load_direct_gdt(int cpu)
761 {
762 	struct desc_ptr gdt_descr;
763 
764 	gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
765 	gdt_descr.size = GDT_SIZE - 1;
766 	load_gdt(&gdt_descr);
767 }
768 EXPORT_SYMBOL_FOR_KVM(load_direct_gdt);
769 
770 /* Load a fixmap remapping of the per-cpu GDT */
771 void load_fixmap_gdt(int cpu)
772 {
773 	struct desc_ptr gdt_descr;
774 
775 	gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
776 	gdt_descr.size = GDT_SIZE - 1;
777 	load_gdt(&gdt_descr);
778 }
779 EXPORT_SYMBOL_GPL(load_fixmap_gdt);
780 
781 /**
782  * switch_gdt_and_percpu_base - Switch to direct GDT and runtime per CPU base
783  * @cpu:	The CPU number for which this is invoked
784  *
785  * Invoked during early boot to switch from early GDT and early per CPU to
786  * the direct GDT and the runtime per CPU area. On 32-bit the percpu base
787  * switch is implicit by loading the direct GDT. On 64bit this requires
788  * to update GSBASE.
789  */
790 void __init switch_gdt_and_percpu_base(int cpu)
791 {
792 	load_direct_gdt(cpu);
793 
794 #ifdef CONFIG_X86_64
795 	/*
796 	 * No need to load %gs. It is already correct.
797 	 *
798 	 * Writing %gs on 64bit would zero GSBASE which would make any per
799 	 * CPU operation up to the point of the wrmsrq() fault.
800 	 *
801 	 * Set GSBASE to the new offset. Until the wrmsrq() happens the
802 	 * early mapping is still valid. That means the GSBASE update will
803 	 * lose any prior per CPU data which was not copied over in
804 	 * setup_per_cpu_areas().
805 	 *
806 	 * This works even with stackprotector enabled because the
807 	 * per CPU stack canary is 0 in both per CPU areas.
808 	 */
809 	wrmsrq(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu));
810 #else
811 	/*
812 	 * %fs is already set to __KERNEL_PERCPU, but after switching GDT
813 	 * it is required to load FS again so that the 'hidden' part is
814 	 * updated from the new GDT. Up to this point the early per CPU
815 	 * translation is active. Any content of the early per CPU data
816 	 * which was not copied over in setup_per_cpu_areas() is lost.
817 	 */
818 	loadsegment(fs, __KERNEL_PERCPU);
819 #endif
820 }
821 
822 static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
823 
824 static void get_model_name(struct cpuinfo_x86 *c)
825 {
826 	unsigned int *v;
827 	char *p, *q, *s;
828 
829 	if (c->extended_cpuid_level < 0x80000004)
830 		return;
831 
832 	v = (unsigned int *)c->x86_model_id;
833 	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
834 	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
835 	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
836 	c->x86_model_id[48] = 0;
837 
838 	/* Trim whitespace */
839 	p = q = s = &c->x86_model_id[0];
840 
841 	while (*p == ' ')
842 		p++;
843 
844 	while (*p) {
845 		/* Note the last non-whitespace index */
846 		if (!isspace(*p))
847 			s = q;
848 
849 		*q++ = *p++;
850 	}
851 
852 	*(s + 1) = '\0';
853 }
854 
855 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
856 {
857 	unsigned int n, dummy, ebx, ecx, edx, l2size, shift __maybe_unused;
858 
859 	n = c->extended_cpuid_level;
860 
861 	if (n >= 0x80000005) {
862 		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
863 		c->x86_cache_size = (ecx>>24) + (edx>>24);
864 #ifdef CONFIG_X86_64
865 		/* On K8 L1 TLB is inclusive, so don't count it */
866 		c->x86_tlbsize = 0;
867 #endif
868 	}
869 
870 	if (n < 0x80000006)	/* Some chips just has a large L1. */
871 		return;
872 
873 	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
874 	l2size = ecx >> 16;
875 
876 #ifdef CONFIG_X86_64
877 	shift = !!cpu_has(c, X86_FEATURE_L2_TLB_SIZE_X32) * 5;
878 	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
879 	c->x86_tlbsize <<= shift;
880 #else
881 	/* do processor-specific cache resizing */
882 	if (this_cpu->legacy_cache_size)
883 		l2size = this_cpu->legacy_cache_size(c, l2size);
884 
885 	/* Allow user to override all this if necessary. */
886 	if (cachesize_override != -1)
887 		l2size = cachesize_override;
888 
889 	if (l2size == 0)
890 		return;		/* Again, no L2 cache is possible */
891 #endif
892 
893 	c->x86_cache_size = l2size;
894 }
895 
896 u16 __read_mostly tlb_lli_4k;
897 u16 __read_mostly tlb_lli_2m;
898 u16 __read_mostly tlb_lli_4m;
899 u16 __read_mostly tlb_lld_4k;
900 u16 __read_mostly tlb_lld_2m;
901 u16 __read_mostly tlb_lld_4m;
902 u16 __read_mostly tlb_lld_1g;
903 
904 static void cpu_detect_tlb(struct cpuinfo_x86 *c)
905 {
906 	if (this_cpu->c_detect_tlb)
907 		this_cpu->c_detect_tlb(c);
908 
909 	pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
910 		tlb_lli_4k, tlb_lli_2m, tlb_lli_4m);
911 
912 	pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
913 		tlb_lld_4k, tlb_lld_2m, tlb_lld_4m, tlb_lld_1g);
914 }
915 
916 void get_cpu_vendor(struct cpuinfo_x86 *c)
917 {
918 	char *v = c->x86_vendor_id;
919 	int i;
920 
921 	for (i = 0; i < X86_VENDOR_NUM; i++) {
922 		if (!cpu_devs[i])
923 			break;
924 
925 		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
926 		    (cpu_devs[i]->c_ident[1] &&
927 		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
928 
929 			this_cpu = cpu_devs[i];
930 			c->x86_vendor = this_cpu->c_x86_vendor;
931 			return;
932 		}
933 	}
934 
935 	pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
936 		    "CPU: Your system may be unstable.\n", v);
937 
938 	c->x86_vendor = X86_VENDOR_UNKNOWN;
939 	this_cpu = &default_cpu;
940 }
941 
942 void cpu_detect(struct cpuinfo_x86 *c)
943 {
944 	const struct leaf_0x0_0 *l0 = cpuid_leaf(c, 0x0);
945 	const struct leaf_0x1_0 *l1;
946 
947 	c->cpuid_level = l0->max_std_leaf;
948 	*(u32 *)&c->x86_vendor_id[0] = l0->cpu_vendorid_0;
949 	*(u32 *)&c->x86_vendor_id[4] = l0->cpu_vendorid_1;
950 	*(u32 *)&c->x86_vendor_id[8] = l0->cpu_vendorid_2;
951 
952 	c->x86 = 4;
953 
954 	l1 = cpuid_leaf(c, 0x1);
955 	if (!l1)
956 		return;
957 
958 	c->x86		= cpuid_family(l1);
959 	c->x86_model	= cpuid_model(l1);
960 	c->x86_stepping	= l1->stepping;
961 
962 	if (l1->clflush) {
963 		c->x86_clflush_size	= l1->clflush_size * 8;
964 		c->x86_cache_alignment	= c->x86_clflush_size;
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_AMD(family, blacklist)		\
1255 	VULNBL(AMD, family, X86_MODEL_ANY, blacklist)
1256 
1257 #define VULNBL_HYGON(family, blacklist)		\
1258 	VULNBL(HYGON, family, X86_MODEL_ANY, blacklist)
1259 
1260 #define SRBDS		BIT(0)
1261 /* CPU is affected by X86_BUG_MMIO_STALE_DATA */
1262 #define MMIO		BIT(1)
1263 /* CPU is affected by Shared Buffers Data Sampling (SBDS), a variant of X86_BUG_MMIO_STALE_DATA */
1264 #define MMIO_SBDS	BIT(2)
1265 /* CPU is affected by RETbleed, speculating where you would not expect it */
1266 #define RETBLEED	BIT(3)
1267 /* CPU is affected by SMT (cross-thread) return predictions */
1268 #define SMT_RSB		BIT(4)
1269 /* CPU is affected by SRSO */
1270 #define SRSO		BIT(5)
1271 /* CPU is affected by GDS */
1272 #define GDS		BIT(6)
1273 /* CPU is affected by Register File Data Sampling */
1274 #define RFDS		BIT(7)
1275 /* CPU is affected by Indirect Target Selection */
1276 #define ITS		BIT(8)
1277 /* CPU is affected by Indirect Target Selection, but guest-host isolation is not affected */
1278 #define ITS_NATIVE_ONLY	BIT(9)
1279 /* CPU is affected by Transient Scheduler Attacks */
1280 #define TSA		BIT(10)
1281 /* CPU is affected by VMSCAPE */
1282 #define VMSCAPE		BIT(11)
1283 
1284 static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
1285 	VULNBL_INTEL_STEPS(INTEL_SANDYBRIDGE_X,	     X86_STEP_MAX,	VMSCAPE),
1286 	VULNBL_INTEL_STEPS(INTEL_SANDYBRIDGE,	     X86_STEP_MAX,	VMSCAPE),
1287 	VULNBL_INTEL_STEPS(INTEL_IVYBRIDGE_X,	     X86_STEP_MAX,	VMSCAPE),
1288 	VULNBL_INTEL_STEPS(INTEL_IVYBRIDGE,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1289 	VULNBL_INTEL_STEPS(INTEL_HASWELL,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1290 	VULNBL_INTEL_STEPS(INTEL_HASWELL_L,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1291 	VULNBL_INTEL_STEPS(INTEL_HASWELL_G,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1292 	VULNBL_INTEL_STEPS(INTEL_HASWELL_X,	     X86_STEP_MAX,	MMIO | VMSCAPE),
1293 	VULNBL_INTEL_STEPS(INTEL_BROADWELL_D,	     X86_STEP_MAX,	MMIO | VMSCAPE),
1294 	VULNBL_INTEL_STEPS(INTEL_BROADWELL_X,	     X86_STEP_MAX,	MMIO | VMSCAPE),
1295 	VULNBL_INTEL_STEPS(INTEL_BROADWELL_G,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1296 	VULNBL_INTEL_STEPS(INTEL_BROADWELL,	     X86_STEP_MAX,	SRBDS | VMSCAPE),
1297 	VULNBL_INTEL_STEPS(INTEL_SKYLAKE_X,		      0x5,	MMIO | RETBLEED | GDS | VMSCAPE),
1298 	VULNBL_INTEL_STEPS(INTEL_SKYLAKE_X,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | ITS | VMSCAPE),
1299 	VULNBL_INTEL_STEPS(INTEL_SKYLAKE_L,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
1300 	VULNBL_INTEL_STEPS(INTEL_SKYLAKE,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
1301 	VULNBL_INTEL_STEPS(INTEL_KABYLAKE_L,		      0xb,	MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
1302 	VULNBL_INTEL_STEPS(INTEL_KABYLAKE_L,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | SRBDS | ITS | VMSCAPE),
1303 	VULNBL_INTEL_STEPS(INTEL_KABYLAKE,		      0xc,	MMIO | RETBLEED | GDS | SRBDS | VMSCAPE),
1304 	VULNBL_INTEL_STEPS(INTEL_KABYLAKE,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | SRBDS | ITS | VMSCAPE),
1305 	VULNBL_INTEL_STEPS(INTEL_CANNONLAKE_L,	     X86_STEP_MAX,	RETBLEED | VMSCAPE),
1306 	VULNBL_INTEL_STEPS(INTEL_ICELAKE_L,	     X86_STEP_MAX,	MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY),
1307 	VULNBL_INTEL_STEPS(INTEL_ICELAKE_D,	     X86_STEP_MAX,	MMIO | GDS | ITS | ITS_NATIVE_ONLY),
1308 	VULNBL_INTEL_STEPS(INTEL_ICELAKE_X,	     X86_STEP_MAX,	MMIO | GDS | ITS | ITS_NATIVE_ONLY),
1309 	VULNBL_INTEL_STEPS(INTEL_COMETLAKE,	     X86_STEP_MAX,	MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | VMSCAPE),
1310 	VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L,		      0x0,	MMIO | RETBLEED | ITS | VMSCAPE),
1311 	VULNBL_INTEL_STEPS(INTEL_COMETLAKE_L,	     X86_STEP_MAX,	MMIO | MMIO_SBDS | RETBLEED | GDS | ITS | VMSCAPE),
1312 	VULNBL_INTEL_STEPS(INTEL_TIGERLAKE_L,	     X86_STEP_MAX,	GDS | ITS | ITS_NATIVE_ONLY),
1313 	VULNBL_INTEL_STEPS(INTEL_TIGERLAKE,	     X86_STEP_MAX,	GDS | ITS | ITS_NATIVE_ONLY),
1314 	VULNBL_INTEL_STEPS(INTEL_LAKEFIELD,	     X86_STEP_MAX,	MMIO | MMIO_SBDS | RETBLEED),
1315 	VULNBL_INTEL_STEPS(INTEL_ROCKETLAKE,	     X86_STEP_MAX,	MMIO | RETBLEED | GDS | ITS | ITS_NATIVE_ONLY),
1316 	VULNBL_INTEL_STEPS(INTEL_ALDERLAKE,	     X86_STEP_MAX,	RFDS | VMSCAPE),
1317 	VULNBL_INTEL_STEPS(INTEL_ALDERLAKE_L,	     X86_STEP_MAX,	RFDS | VMSCAPE),
1318 	VULNBL_INTEL_STEPS(INTEL_RAPTORLAKE,	     X86_STEP_MAX,	RFDS | VMSCAPE),
1319 	VULNBL_INTEL_STEPS(INTEL_RAPTORLAKE_P,	     X86_STEP_MAX,	RFDS | VMSCAPE),
1320 	VULNBL_INTEL_STEPS(INTEL_RAPTORLAKE_S,	     X86_STEP_MAX,	RFDS | VMSCAPE),
1321 	VULNBL_INTEL_STEPS(INTEL_METEORLAKE_L,	     X86_STEP_MAX,	VMSCAPE),
1322 	VULNBL_INTEL_STEPS(INTEL_ARROWLAKE_H,	     X86_STEP_MAX,	VMSCAPE),
1323 	VULNBL_INTEL_STEPS(INTEL_ARROWLAKE,	     X86_STEP_MAX,	VMSCAPE),
1324 	VULNBL_INTEL_STEPS(INTEL_ARROWLAKE_U,	     X86_STEP_MAX,	VMSCAPE),
1325 	VULNBL_INTEL_STEPS(INTEL_LUNARLAKE_M,	     X86_STEP_MAX,	VMSCAPE),
1326 	VULNBL_INTEL_STEPS(INTEL_SAPPHIRERAPIDS_X,   X86_STEP_MAX,	VMSCAPE),
1327 	VULNBL_INTEL_STEPS(INTEL_GRANITERAPIDS_X,    X86_STEP_MAX,	VMSCAPE),
1328 	VULNBL_INTEL_STEPS(INTEL_EMERALDRAPIDS_X,    X86_STEP_MAX,	VMSCAPE),
1329 	VULNBL_INTEL_STEPS(INTEL_ATOM_GRACEMONT,     X86_STEP_MAX,	RFDS | VMSCAPE),
1330 	VULNBL_INTEL_STEPS(INTEL_ATOM_TREMONT,	     X86_STEP_MAX,	MMIO | MMIO_SBDS | RFDS),
1331 	VULNBL_INTEL_STEPS(INTEL_ATOM_TREMONT_D,     X86_STEP_MAX,	MMIO | RFDS),
1332 	VULNBL_INTEL_STEPS(INTEL_ATOM_TREMONT_L,     X86_STEP_MAX,	MMIO | MMIO_SBDS | RFDS),
1333 	VULNBL_INTEL_STEPS(INTEL_ATOM_GOLDMONT,      X86_STEP_MAX,	RFDS),
1334 	VULNBL_INTEL_STEPS(INTEL_ATOM_GOLDMONT_D,    X86_STEP_MAX,	RFDS),
1335 	VULNBL_INTEL_STEPS(INTEL_ATOM_GOLDMONT_PLUS, X86_STEP_MAX,	RFDS),
1336 	VULNBL_INTEL_STEPS(INTEL_ATOM_CRESTMONT_X,   X86_STEP_MAX,	VMSCAPE),
1337 
1338 	VULNBL_AMD(0x15, RETBLEED),
1339 	VULNBL_AMD(0x16, RETBLEED),
1340 	VULNBL_AMD(0x17, RETBLEED | SMT_RSB | SRSO | VMSCAPE),
1341 	VULNBL_HYGON(0x18, RETBLEED | SMT_RSB | SRSO | VMSCAPE),
1342 	VULNBL_AMD(0x19, SRSO | TSA | VMSCAPE),
1343 	VULNBL_AMD(0x1a, SRSO | VMSCAPE),
1344 	{}
1345 };
1346 
1347 static bool __init cpu_matches(const struct x86_cpu_id *table, unsigned long which)
1348 {
1349 	const struct x86_cpu_id *m = x86_match_cpu(table);
1350 
1351 	return m && !!(m->driver_data & which);
1352 }
1353 
1354 u64 x86_read_arch_cap_msr(void)
1355 {
1356 	u64 x86_arch_cap_msr = 0;
1357 
1358 	if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1359 		rdmsrq(MSR_IA32_ARCH_CAPABILITIES, x86_arch_cap_msr);
1360 
1361 	return x86_arch_cap_msr;
1362 }
1363 
1364 static bool arch_cap_mmio_immune(u64 x86_arch_cap_msr)
1365 {
1366 	return (x86_arch_cap_msr & ARCH_CAP_FBSDP_NO &&
1367 		x86_arch_cap_msr & ARCH_CAP_PSDP_NO &&
1368 		x86_arch_cap_msr & ARCH_CAP_SBDR_SSDP_NO);
1369 }
1370 
1371 static bool __init vulnerable_to_rfds(u64 x86_arch_cap_msr)
1372 {
1373 	/* The "immunity" bit trumps everything else: */
1374 	if (x86_arch_cap_msr & ARCH_CAP_RFDS_NO)
1375 		return false;
1376 
1377 	/*
1378 	 * VMMs set ARCH_CAP_RFDS_CLEAR for processors not in the blacklist to
1379 	 * indicate that mitigation is needed because guest is running on a
1380 	 * vulnerable hardware or may migrate to such hardware:
1381 	 */
1382 	if (x86_arch_cap_msr & ARCH_CAP_RFDS_CLEAR)
1383 		return true;
1384 
1385 	/* Only consult the blacklist when there is no enumeration: */
1386 	if (!cpu_matches(cpu_vuln_blacklist, RFDS))
1387 		return false;
1388 
1389 	/*
1390 	 * ADL and RPL are affected only if they have Atom CPUs. Hybrids have
1391 	 * both Core and Atom CPUs. Mark unaffected when Atom CPUs are not
1392 	 * present.
1393 	 */
1394 	if ((boot_cpu_data.x86_model == 0x97 ||
1395 	     boot_cpu_data.x86_model == 0xB7) &&
1396 	     boot_cpu_data.topo.intel_type != INTEL_CPU_TYPE_ATOM &&
1397 	     !boot_cpu_has(X86_FEATURE_HYBRID_CPU))
1398 		return false;
1399 
1400 	return true;
1401 }
1402 
1403 static bool __init vulnerable_to_its(u64 x86_arch_cap_msr)
1404 {
1405 	/* The "immunity" bit trumps everything else: */
1406 	if (x86_arch_cap_msr & ARCH_CAP_ITS_NO)
1407 		return false;
1408 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
1409 		return false;
1410 
1411 	/* None of the affected CPUs have BHI_CTRL */
1412 	if (boot_cpu_has(X86_FEATURE_BHI_CTRL))
1413 		return false;
1414 
1415 	/*
1416 	 * If a VMM did not expose ITS_NO, assume that a guest could
1417 	 * be running on a vulnerable hardware or may migrate to such
1418 	 * hardware.
1419 	 */
1420 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
1421 		return true;
1422 
1423 	if (cpu_matches(cpu_vuln_blacklist, ITS))
1424 		return true;
1425 
1426 	return false;
1427 }
1428 
1429 static const struct x86_cpu_id cpu_latest_microcode[] __initconst = {
1430 #include "microcode/intel-ucode-defs.h"
1431 	{}
1432 };
1433 
1434 static bool __init cpu_has_old_microcode(void)
1435 {
1436 	const struct x86_cpu_id *m = x86_match_cpu(cpu_latest_microcode);
1437 
1438 	/* Give unknown CPUs a pass: */
1439 	if (!m) {
1440 		/* Intel CPUs should be in the list. Warn if not: */
1441 		if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1442 			pr_info("x86/CPU: Model not found in latest microcode list\n");
1443 		return false;
1444 	}
1445 
1446 	/*
1447 	 * Hosts usually lie to guests with a super high microcode
1448 	 * version. Just ignore what hosts tell guests:
1449 	 */
1450 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
1451 		return false;
1452 
1453 	/* Consider all debug microcode to be old: */
1454 	if (boot_cpu_data.microcode & BIT(31))
1455 		return true;
1456 
1457 	/* Give new microcode a pass: */
1458 	if (boot_cpu_data.microcode >= m->driver_data)
1459 		return false;
1460 
1461 	/* Uh oh, too old: */
1462 	return true;
1463 }
1464 
1465 static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
1466 {
1467 	u64 x86_arch_cap_msr = x86_read_arch_cap_msr();
1468 
1469 	if (cpu_has_old_microcode()) {
1470 		pr_warn("x86/CPU: Running old microcode\n");
1471 		setup_force_cpu_bug(X86_BUG_OLD_MICROCODE);
1472 		add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
1473 	}
1474 
1475 	/* Set ITLB_MULTIHIT bug if cpu is not in the whitelist and not mitigated */
1476 	if (!cpu_matches(cpu_vuln_whitelist, NO_ITLB_MULTIHIT) &&
1477 	    !(x86_arch_cap_msr & ARCH_CAP_PSCHANGE_MC_NO))
1478 		setup_force_cpu_bug(X86_BUG_ITLB_MULTIHIT);
1479 
1480 	if (cpu_matches(cpu_vuln_whitelist, NO_SPECULATION))
1481 		return;
1482 
1483 	setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
1484 
1485 	if (!cpu_matches(cpu_vuln_whitelist, NO_SPECTRE_V2)) {
1486 		setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
1487 		setup_force_cpu_bug(X86_BUG_SPECTRE_V2_USER);
1488 	}
1489 
1490 	if (!cpu_matches(cpu_vuln_whitelist, NO_SSB) &&
1491 	    !(x86_arch_cap_msr & ARCH_CAP_SSB_NO) &&
1492 	   !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
1493 		setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
1494 
1495 	/*
1496 	 * AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature
1497 	 * flag and protect from vendor-specific bugs via the whitelist.
1498 	 *
1499 	 * Don't use AutoIBRS when SNP is enabled because it degrades host
1500 	 * userspace indirect branch performance.
1501 	 */
1502 	if ((x86_arch_cap_msr & ARCH_CAP_IBRS_ALL) ||
1503 	    (cpu_has(c, X86_FEATURE_AUTOIBRS) &&
1504 	     !cpu_feature_enabled(X86_FEATURE_SEV_SNP))) {
1505 		setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
1506 		if (!cpu_matches(cpu_vuln_whitelist, NO_EIBRS_PBRSB) &&
1507 		    !(x86_arch_cap_msr & ARCH_CAP_PBRSB_NO))
1508 			setup_force_cpu_bug(X86_BUG_EIBRS_PBRSB);
1509 	}
1510 
1511 	if (!cpu_matches(cpu_vuln_whitelist, NO_MDS) &&
1512 	    !(x86_arch_cap_msr & ARCH_CAP_MDS_NO)) {
1513 		setup_force_cpu_bug(X86_BUG_MDS);
1514 		if (cpu_matches(cpu_vuln_whitelist, MSBDS_ONLY))
1515 			setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
1516 	}
1517 
1518 	if (!cpu_matches(cpu_vuln_whitelist, NO_SWAPGS))
1519 		setup_force_cpu_bug(X86_BUG_SWAPGS);
1520 
1521 	/*
1522 	 * When the CPU is not mitigated for TAA (TAA_NO=0) set TAA bug when:
1523 	 *	- TSX is supported or
1524 	 *	- TSX_CTRL is present
1525 	 *
1526 	 * TSX_CTRL check is needed for cases when TSX could be disabled before
1527 	 * the kernel boot e.g. kexec.
1528 	 * TSX_CTRL check alone is not sufficient for cases when the microcode
1529 	 * update is not present or running as guest that don't get TSX_CTRL.
1530 	 */
1531 	if (!(x86_arch_cap_msr & ARCH_CAP_TAA_NO) &&
1532 	    (cpu_has(c, X86_FEATURE_RTM) ||
1533 	     (x86_arch_cap_msr & ARCH_CAP_TSX_CTRL_MSR)))
1534 		setup_force_cpu_bug(X86_BUG_TAA);
1535 
1536 	/*
1537 	 * SRBDS affects CPUs which support RDRAND or RDSEED and are listed
1538 	 * in the vulnerability blacklist.
1539 	 *
1540 	 * Some of the implications and mitigation of Shared Buffers Data
1541 	 * Sampling (SBDS) are similar to SRBDS. Give SBDS same treatment as
1542 	 * SRBDS.
1543 	 */
1544 	if ((cpu_has(c, X86_FEATURE_RDRAND) ||
1545 	     cpu_has(c, X86_FEATURE_RDSEED)) &&
1546 	    cpu_matches(cpu_vuln_blacklist, SRBDS | MMIO_SBDS))
1547 		    setup_force_cpu_bug(X86_BUG_SRBDS);
1548 
1549 	/*
1550 	 * Processor MMIO Stale Data bug enumeration
1551 	 *
1552 	 * Affected CPU list is generally enough to enumerate the vulnerability,
1553 	 * but for virtualization case check for ARCH_CAP MSR bits also, VMM may
1554 	 * not want the guest to enumerate the bug.
1555 	 */
1556 	if (!arch_cap_mmio_immune(x86_arch_cap_msr)) {
1557 		if (cpu_matches(cpu_vuln_blacklist, MMIO))
1558 			setup_force_cpu_bug(X86_BUG_MMIO_STALE_DATA);
1559 	}
1560 
1561 	if (!cpu_has(c, X86_FEATURE_BTC_NO)) {
1562 		if (cpu_matches(cpu_vuln_blacklist, RETBLEED) || (x86_arch_cap_msr & ARCH_CAP_RSBA))
1563 			setup_force_cpu_bug(X86_BUG_RETBLEED);
1564 	}
1565 
1566 	if (cpu_matches(cpu_vuln_blacklist, SMT_RSB))
1567 		setup_force_cpu_bug(X86_BUG_SMT_RSB);
1568 
1569 	if (!cpu_has(c, X86_FEATURE_SRSO_NO)) {
1570 		if (cpu_matches(cpu_vuln_blacklist, SRSO))
1571 			setup_force_cpu_bug(X86_BUG_SRSO);
1572 	}
1573 
1574 	/*
1575 	 * Check if CPU is vulnerable to GDS. If running in a virtual machine on
1576 	 * an affected processor, the VMM may have disabled the use of GATHER by
1577 	 * disabling AVX2. The only way to do this in HW is to clear XCR0[2],
1578 	 * which means that AVX will be disabled.
1579 	 */
1580 	if (cpu_matches(cpu_vuln_blacklist, GDS) && !(x86_arch_cap_msr & ARCH_CAP_GDS_NO) &&
1581 	    boot_cpu_has(X86_FEATURE_AVX))
1582 		setup_force_cpu_bug(X86_BUG_GDS);
1583 
1584 	if (vulnerable_to_rfds(x86_arch_cap_msr))
1585 		setup_force_cpu_bug(X86_BUG_RFDS);
1586 
1587 	/*
1588 	 * Intel parts with eIBRS are vulnerable to BHI attacks. Parts with
1589 	 * BHI_NO still need to use the BHI mitigation to prevent Intra-mode
1590 	 * attacks.  When virtualized, eIBRS could be hidden, assume vulnerable.
1591 	 */
1592 	if (!cpu_matches(cpu_vuln_whitelist, NO_BHI) &&
1593 	    (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED) ||
1594 	     boot_cpu_has(X86_FEATURE_HYPERVISOR)))
1595 		setup_force_cpu_bug(X86_BUG_BHI);
1596 
1597 	if (cpu_has(c, X86_FEATURE_AMD_IBPB) && !cpu_has(c, X86_FEATURE_AMD_IBPB_RET))
1598 		setup_force_cpu_bug(X86_BUG_IBPB_NO_RET);
1599 
1600 	if (vulnerable_to_its(x86_arch_cap_msr)) {
1601 		setup_force_cpu_bug(X86_BUG_ITS);
1602 		if (cpu_matches(cpu_vuln_blacklist, ITS_NATIVE_ONLY))
1603 			setup_force_cpu_bug(X86_BUG_ITS_NATIVE_ONLY);
1604 	}
1605 
1606 	if (c->x86_vendor == X86_VENDOR_AMD) {
1607 		if (!cpu_has(c, X86_FEATURE_TSA_SQ_NO) ||
1608 		    !cpu_has(c, X86_FEATURE_TSA_L1_NO)) {
1609 			if (cpu_matches(cpu_vuln_blacklist, TSA) ||
1610 			    /* Enable bug on Zen guests to allow for live migration. */
1611 			    (cpu_has(c, X86_FEATURE_HYPERVISOR) && cpu_has(c, X86_FEATURE_ZEN)))
1612 				setup_force_cpu_bug(X86_BUG_TSA);
1613 		}
1614 	}
1615 
1616 	/*
1617 	 * Set the bug only on bare-metal. A nested hypervisor should already be
1618 	 * deploying IBPB to isolate itself from nested guests.
1619 	 */
1620 	if (cpu_matches(cpu_vuln_blacklist, VMSCAPE) &&
1621 	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
1622 		setup_force_cpu_bug(X86_BUG_VMSCAPE);
1623 
1624 	if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
1625 		return;
1626 
1627 	/* Rogue Data Cache Load? No! */
1628 	if (x86_arch_cap_msr & ARCH_CAP_RDCL_NO)
1629 		return;
1630 
1631 	setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
1632 
1633 	if (cpu_matches(cpu_vuln_whitelist, NO_L1TF))
1634 		return;
1635 
1636 	setup_force_cpu_bug(X86_BUG_L1TF);
1637 }
1638 
1639 /*
1640  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1641  * unfortunately, that's not true in practice because of early VIA
1642  * chips and (more importantly) broken virtualizers that are not easy
1643  * to detect. In the latter case it doesn't even *fail* reliably, so
1644  * probing for it doesn't even work. Disable it completely on 32-bit
1645  * unless we can find a reliable way to detect all the broken cases.
1646  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
1647  */
1648 static void detect_nopl(void)
1649 {
1650 #ifdef CONFIG_X86_32
1651 	setup_clear_cpu_cap(X86_FEATURE_NOPL);
1652 #else
1653 	setup_force_cpu_cap(X86_FEATURE_NOPL);
1654 #endif
1655 }
1656 
1657 static inline bool parse_set_clear_cpuid(char *arg, bool set)
1658 {
1659 	char *opt;
1660 	int taint = 0;
1661 
1662 	while (arg) {
1663 		bool found = false;
1664 		unsigned int bit;
1665 
1666 		opt = strsep(&arg, ",");
1667 
1668 		/*
1669 		 * Handle naked numbers first for feature flags which don't
1670 		 * have names. It doesn't make sense for a bug not to have a
1671 		 * name so don't handle bug flags here.
1672 		 */
1673 		if (!kstrtouint(opt, 10, &bit)) {
1674 			if (bit < NCAPINTS * 32) {
1675 
1676 				if (set) {
1677 					pr_warn("setcpuid: force-enabling CPU feature flag:");
1678 					setup_force_cpu_cap(bit);
1679 				} else {
1680 					pr_warn("clearcpuid: force-disabling CPU feature flag:");
1681 					setup_clear_cpu_cap(bit);
1682 				}
1683 				/* empty-string, i.e., ""-defined feature flags */
1684 				if (!x86_cap_flags[bit])
1685 					pr_cont(" %d:%d\n", bit >> 5, bit & 31);
1686 				else
1687 					pr_cont(" %s\n", x86_cap_flags[bit]);
1688 
1689 				taint++;
1690 			}
1691 			/*
1692 			 * The assumption is that there are no feature names with only
1693 			 * numbers in the name thus go to the next argument.
1694 			 */
1695 			continue;
1696 		}
1697 
1698 		for (bit = 0; bit < 32 * (NCAPINTS + NBUGINTS); bit++) {
1699 			const char *flag;
1700 			const char *kind;
1701 
1702 			if (bit < 32 * NCAPINTS) {
1703 				flag = x86_cap_flags[bit];
1704 				kind = "feature";
1705 			} else {
1706 				kind = "bug";
1707 				flag = x86_bug_flags[bit - (32 * NCAPINTS)];
1708 			}
1709 
1710 			if (!flag)
1711 				continue;
1712 
1713 			if (strcmp(flag, opt))
1714 				continue;
1715 
1716 			if (set) {
1717 				pr_warn("setcpuid: force-enabling CPU %s flag: %s\n",
1718 					kind, flag);
1719 				setup_force_cpu_cap(bit);
1720 			} else {
1721 				pr_warn("clearcpuid: force-disabling CPU %s flag: %s\n",
1722 					kind, flag);
1723 				setup_clear_cpu_cap(bit);
1724 			}
1725 			taint++;
1726 			found = true;
1727 			break;
1728 		}
1729 
1730 		if (!found)
1731 			pr_warn("%s: unknown CPU flag: %s", set ? "setcpuid" : "clearcpuid", opt);
1732 	}
1733 
1734 	return taint;
1735 }
1736 
1737 
1738 /*
1739  * We parse cpu parameters early because fpu__init_system() is executed
1740  * before parse_early_param().
1741  */
1742 static void __init cpu_parse_early_param(void)
1743 {
1744 	bool cpuid_taint = false;
1745 	char arg[128];
1746 	int arglen;
1747 
1748 #ifdef CONFIG_X86_32
1749 	if (cmdline_find_option_bool(boot_command_line, "nofxsr"))
1750 		setup_clear_cpu_cap(X86_FEATURE_FXSR);
1751 #endif
1752 
1753 	if (cmdline_find_option_bool(boot_command_line, "noxsave"))
1754 		setup_clear_cpu_cap(X86_FEATURE_XSAVE);
1755 
1756 	if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
1757 		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
1758 
1759 	if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
1760 		setup_clear_cpu_cap(X86_FEATURE_XSAVES);
1761 
1762 	if (cmdline_find_option_bool(boot_command_line, "nousershstk"))
1763 		setup_clear_cpu_cap(X86_FEATURE_USER_SHSTK);
1764 
1765 	/* Minimize the gap between FRED is available and available but disabled. */
1766 	arglen = cmdline_find_option(boot_command_line, "fred", arg, sizeof(arg));
1767 	if (arglen == 3 && !strncmp(arg, "off", 3))
1768 		setup_clear_cpu_cap(X86_FEATURE_FRED);
1769 
1770 	arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg));
1771 	if (arglen > 0)
1772 		cpuid_taint |= parse_set_clear_cpuid(arg, false);
1773 
1774 	arglen = cmdline_find_option(boot_command_line, "setcpuid", arg, sizeof(arg));
1775 	if (arglen > 0)
1776 		cpuid_taint |= parse_set_clear_cpuid(arg, true);
1777 
1778 	if (cpuid_taint) {
1779 		pr_warn("!!! setcpuid=/clearcpuid= in use, this is for TESTING ONLY, may break things horribly. Tainting kernel.\n");
1780 		add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
1781 	}
1782 }
1783 
1784 static void init_cpu_info(struct cpuinfo_x86 *c)
1785 {
1786 	c->x86_cache_size = 0;
1787 	c->x86_vendor = X86_VENDOR_UNKNOWN;
1788 	c->x86_model = c->x86_stepping = 0;	/* So far unknown... */
1789 	c->x86_vendor_id[0] = '\0'; /* Unset */
1790 	c->x86_model_id[0] = '\0';  /* Unset */
1791 #ifdef CONFIG_X86_64
1792 	c->x86_clflush_size = 64;
1793 	c->x86_phys_bits = 36;
1794 	c->x86_virt_bits = 48;
1795 #else
1796 	c->cpuid_level = -1;	/* CPUID not detected */
1797 	c->x86_clflush_size = 32;
1798 	c->x86_phys_bits = 32;
1799 	c->x86_virt_bits = 32;
1800 #endif
1801 	c->x86_cache_alignment = c->x86_clflush_size;
1802 	memset(&c->x86_capability, 0, sizeof(c->x86_capability));
1803 	memset(&c->cpuid, 0, sizeof(c->cpuid));
1804 #ifdef CONFIG_X86_VMX_FEATURE_NAMES
1805 	memset(&c->vmx_capability, 0, sizeof(c->vmx_capability));
1806 #endif
1807 	c->extended_cpuid_level = 0;
1808 }
1809 
1810 /*
1811  * Do minimum CPU detection early.
1812  * Fields really needed: vendor, cpuid_level, family, model, mask,
1813  * cache alignment.
1814  * The others are reset to their defaults here and only filled in later,
1815  * by identify_cpu().
1816  *
1817  * WARNING: this function is only called on the boot CPU.  Don't add code
1818  * here that is supposed to run on all CPUs.
1819  */
1820 static void __init early_identify_cpu(struct cpuinfo_x86 *c)
1821 {
1822 	init_cpu_info(c);
1823 
1824 	if (!cpuid_feature())
1825 		identify_cpu_without_cpuid(c);
1826 
1827 	/* Cyrix could have CPUID enabled via c_identify(). */
1828 	if (cpuid_feature()) {
1829 		cpuid_scan_cpu(c);
1830 		cpu_detect(c);
1831 		get_cpu_vendor(c);
1832 		intel_unlock_cpuid_leafs(c);
1833 		get_cpu_cap(c);
1834 		setup_force_cpu_cap(X86_FEATURE_CPUID);
1835 		get_cpu_address_sizes(c);
1836 		cpu_parse_early_param();
1837 
1838 		cpu_init_topology(c);
1839 
1840 		if (this_cpu->c_early_init)
1841 			this_cpu->c_early_init(c);
1842 
1843 		c->cpu_index = 0;
1844 		filter_cpuid_features(c, false);
1845 		check_cpufeature_deps(c);
1846 
1847 		if (this_cpu->c_bsp_init)
1848 			this_cpu->c_bsp_init(c);
1849 	} else {
1850 		setup_clear_cpu_cap(X86_FEATURE_CPUID);
1851 		get_cpu_address_sizes(c);
1852 		cpu_init_topology(c);
1853 	}
1854 
1855 	setup_force_cpu_cap(X86_FEATURE_ALWAYS);
1856 
1857 	cpu_set_bug_bits(c);
1858 
1859 	sld_setup(c);
1860 
1861 #ifdef CONFIG_X86_32
1862 	/*
1863 	 * Regardless of whether PCID is enumerated, the SDM says
1864 	 * that it can't be enabled in 32-bit mode.
1865 	 */
1866 	setup_clear_cpu_cap(X86_FEATURE_PCID);
1867 
1868 	/*
1869 	 * Never use SYSCALL on a 32-bit kernel
1870 	 */
1871 	setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
1872 #endif
1873 
1874 	/*
1875 	 * Later in the boot process pgtable_l5_enabled() relies on
1876 	 * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not
1877 	 * enabled by this point we need to clear the feature bit to avoid
1878 	 * false-positives at the later stage.
1879 	 *
1880 	 * pgtable_l5_enabled() can be false here for several reasons:
1881 	 *  - 5-level paging is disabled compile-time;
1882 	 *  - it's 32-bit kernel;
1883 	 *  - machine doesn't support 5-level paging;
1884 	 *  - user specified 'no5lvl' in kernel command line.
1885 	 */
1886 	if (!pgtable_l5_enabled())
1887 		setup_clear_cpu_cap(X86_FEATURE_LA57);
1888 
1889 	detect_nopl();
1890 	mca_bsp_init(c);
1891 }
1892 
1893 void __init init_cpu_devs(void)
1894 {
1895 	const struct cpu_dev *const *cdev;
1896 	int count = 0;
1897 
1898 	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
1899 		const struct cpu_dev *cpudev = *cdev;
1900 
1901 		if (count >= X86_VENDOR_NUM)
1902 			break;
1903 		cpu_devs[count] = cpudev;
1904 		count++;
1905 	}
1906 }
1907 
1908 void __init early_cpu_init(void)
1909 {
1910 #ifdef CONFIG_PROCESSOR_SELECT
1911 	unsigned int i, j;
1912 
1913 	pr_info("KERNEL supported cpus:\n");
1914 #endif
1915 
1916 	init_cpu_devs();
1917 
1918 #ifdef CONFIG_PROCESSOR_SELECT
1919 	for (i = 0; i < X86_VENDOR_NUM && cpu_devs[i]; i++) {
1920 		for (j = 0; j < 2; j++) {
1921 			if (!cpu_devs[i]->c_ident[j])
1922 				continue;
1923 			pr_info("  %s %s\n", cpu_devs[i]->c_vendor,
1924 				cpu_devs[i]->c_ident[j]);
1925 		}
1926 	}
1927 #endif
1928 
1929 	early_identify_cpu(&boot_cpu_data);
1930 }
1931 
1932 static bool detect_null_seg_behavior(void)
1933 {
1934 	/*
1935 	 * Empirically, writing zero to a segment selector on AMD does
1936 	 * not clear the base, whereas writing zero to a segment
1937 	 * selector on Intel does clear the base.  Intel's behavior
1938 	 * allows slightly faster context switches in the common case
1939 	 * where GS is unused by the prev and next threads.
1940 	 *
1941 	 * Since neither vendor documents this anywhere that I can see,
1942 	 * detect it directly instead of hard-coding the choice by
1943 	 * vendor.
1944 	 *
1945 	 * I've designated AMD's behavior as the "bug" because it's
1946 	 * counterintuitive and less friendly.
1947 	 */
1948 
1949 	unsigned long old_base, tmp;
1950 	rdmsrq(MSR_FS_BASE, old_base);
1951 	wrmsrq(MSR_FS_BASE, 1);
1952 	loadsegment(fs, 0);
1953 	rdmsrq(MSR_FS_BASE, tmp);
1954 	wrmsrq(MSR_FS_BASE, old_base);
1955 	return tmp == 0;
1956 }
1957 
1958 void check_null_seg_clears_base(struct cpuinfo_x86 *c)
1959 {
1960 	/* BUG_NULL_SEG is only relevant with 64bit userspace */
1961 	if (!IS_ENABLED(CONFIG_X86_64))
1962 		return;
1963 
1964 	if (cpu_has(c, X86_FEATURE_NULL_SEL_CLR_BASE))
1965 		return;
1966 
1967 	/*
1968 	 * CPUID bit above wasn't set. If this kernel is still running
1969 	 * as a HV guest, then the HV has decided not to advertize
1970 	 * that CPUID bit for whatever reason.	For example, one
1971 	 * member of the migration pool might be vulnerable.  Which
1972 	 * means, the bug is present: set the BUG flag and return.
1973 	 */
1974 	if (cpu_has(c, X86_FEATURE_HYPERVISOR)) {
1975 		set_cpu_bug(c, X86_BUG_NULL_SEG);
1976 		return;
1977 	}
1978 
1979 	/*
1980 	 * Zen2 CPUs also have this behaviour, but no CPUID bit.
1981 	 * 0x18 is the respective family for Hygon.
1982 	 */
1983 	if ((c->x86 == 0x17 || c->x86 == 0x18) &&
1984 	    detect_null_seg_behavior())
1985 		return;
1986 
1987 	/* All the remaining ones are affected */
1988 	set_cpu_bug(c, X86_BUG_NULL_SEG);
1989 }
1990 
1991 /*
1992  * This does the hard work of actually picking apart the CPU stuff...
1993  */
1994 static void identify_cpu(struct cpuinfo_x86 *c)
1995 {
1996 	int i;
1997 
1998 	c->loops_per_jiffy = loops_per_jiffy;
1999 
2000 	if (!cpuid_feature())
2001 		identify_cpu_without_cpuid(c);
2002 
2003 	/* Cyrix could have CPUID enabled via c_identify(). */
2004 	if (!cpuid_feature())
2005 		goto no_cpuid;
2006 
2007 	cpuid_scan_cpu(c);
2008 	cpu_detect(c);
2009 	get_cpu_vendor(c);
2010 	intel_unlock_cpuid_leafs(c);
2011 	get_cpu_cap(c);
2012 
2013 	get_cpu_address_sizes(c);
2014 
2015 	get_model_name(c); /* Default name */
2016 
2017 	/*
2018 	 * ESPFIX is a strange bug.  All real CPUs have it.  Paravirt
2019 	 * systems that run Linux at CPL > 0 may or may not have the
2020 	 * issue, but, even if they have the issue, there's absolutely
2021 	 * nothing we can do about it because we can't use the real IRET
2022 	 * instruction.
2023 	 *
2024 	 * NB: For the time being, only 32-bit kernels support
2025 	 * X86_BUG_ESPFIX as such.  64-bit kernels directly choose
2026 	 * whether to apply espfix using paravirt hooks.  If any
2027 	 * non-paravirt system ever shows up that does *not* have the
2028 	 * ESPFIX issue, we can change this.
2029 	 */
2030 #ifdef CONFIG_X86_32
2031 	set_cpu_bug(c, X86_BUG_ESPFIX);
2032 #endif
2033 
2034 no_cpuid:
2035 	cpu_parse_topology(c);
2036 
2037 	if (this_cpu->c_identify)
2038 		this_cpu->c_identify(c);
2039 
2040 	/* Clear/Set all flags overridden by options, after probe */
2041 	apply_forced_caps(c);
2042 
2043 	/*
2044 	 * Set default APIC and TSC_DEADLINE MSR fencing flag. AMD and
2045 	 * Hygon will clear it in ->c_init() below.
2046 	 */
2047 	set_cpu_cap(c, X86_FEATURE_APIC_MSRS_FENCE);
2048 
2049 	/*
2050 	 * Vendor-specific initialization.  In this section we
2051 	 * canonicalize the feature flags, meaning if there are
2052 	 * features a certain CPU supports which CPUID doesn't
2053 	 * tell us, CPUID claiming incorrect flags, or other bugs,
2054 	 * we handle them here.
2055 	 *
2056 	 * At the end of this section, c->x86_capability better
2057 	 * indicate the features this CPU genuinely supports!
2058 	 */
2059 	if (this_cpu->c_init)
2060 		this_cpu->c_init(c);
2061 
2062 	bus_lock_init();
2063 
2064 	/* Disable the PN if appropriate */
2065 	squash_the_stupid_serial_number(c);
2066 
2067 	setup_smep(c);
2068 	setup_smap(c);
2069 	setup_umip(c);
2070 
2071 	/*
2072 	 * The vendor-specific functions might have changed features.
2073 	 * Now we do "generic changes."
2074 	 */
2075 
2076 	/* Filter out anything that depends on CPUID levels we don't have */
2077 	filter_cpuid_features(c, true);
2078 
2079 	/* Check for unmet dependencies based on the CPUID dependency table */
2080 	check_cpufeature_deps(c);
2081 
2082 	/* If the model name is still unset, do table lookup. */
2083 	if (!c->x86_model_id[0]) {
2084 		const char *p;
2085 		p = table_lookup_model(c);
2086 		if (p)
2087 			strcpy(c->x86_model_id, p);
2088 		else
2089 			/* Last resort... */
2090 			sprintf(c->x86_model_id, "%02x/%02x",
2091 				c->x86, c->x86_model);
2092 	}
2093 
2094 	x86_init_rdrand(c);
2095 	setup_pku(c);
2096 	setup_cet(c);
2097 
2098 	/*
2099 	 * Clear/Set all flags overridden by options, need do it
2100 	 * before following smp all cpus cap AND.
2101 	 */
2102 	apply_forced_caps(c);
2103 
2104 	/*
2105 	 * On SMP, boot_cpu_data holds the common feature set between
2106 	 * all CPUs; so make sure that we indicate which features are
2107 	 * common between the CPUs.  The first time this routine gets
2108 	 * executed, c == &boot_cpu_data.
2109 	 */
2110 	if (c != &boot_cpu_data) {
2111 		/* AND the already accumulated flags with these */
2112 		for (i = 0; i < NCAPINTS; i++)
2113 			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
2114 
2115 		/* OR, i.e. replicate the bug flags */
2116 		for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
2117 			c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
2118 	}
2119 
2120 	ppin_init(c);
2121 
2122 	/* Init Machine Check Exception if available. */
2123 	mcheck_cpu_init(c);
2124 
2125 	numa_add_cpu(smp_processor_id());
2126 
2127 	if (IS_ENABLED(CONFIG_X86_32))
2128 		enable_sep_cpu();
2129 }
2130 
2131 /*
2132  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
2133  * on 32-bit kernels:
2134  */
2135 #ifdef CONFIG_X86_32
2136 void enable_sep_cpu(void)
2137 {
2138 	struct tss_struct *tss;
2139 	int cpu;
2140 
2141 	if (!boot_cpu_has(X86_FEATURE_SEP))
2142 		return;
2143 
2144 	cpu = get_cpu();
2145 	tss = &per_cpu(cpu_tss_rw, cpu);
2146 
2147 	/*
2148 	 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
2149 	 * see the big comment in struct x86_hw_tss's definition.
2150 	 */
2151 
2152 	tss->x86_tss.ss1 = __KERNEL_CS;
2153 	wrmsrq(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1);
2154 	wrmsrq(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1));
2155 	wrmsrq(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32);
2156 
2157 	put_cpu();
2158 }
2159 #endif
2160 
2161 static __init void identify_boot_cpu(void)
2162 {
2163 	identify_cpu(&boot_cpu_data);
2164 	if (HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT))
2165 		pr_info("CET detected: Indirect Branch Tracking enabled\n");
2166 	cpu_detect_tlb(&boot_cpu_data);
2167 	setup_cr_pinning();
2168 
2169 	x86_virt_init();
2170 	tsx_init();
2171 	tdx_init();
2172 	lkgs_init();
2173 }
2174 
2175 void identify_secondary_cpu(unsigned int cpu)
2176 {
2177 	struct cpuinfo_x86 *c = &cpu_data(cpu);
2178 
2179 	/* Copy boot_cpu_data only on the first bringup */
2180 	if (!c->initialized)
2181 		*c = boot_cpu_data;
2182 	c->cpu_index = cpu;
2183 
2184 	init_cpu_info(c);
2185 	identify_cpu(c);
2186 	x86_spec_ctrl_setup_ap();
2187 	update_srbds_msr();
2188 	if (boot_cpu_has_bug(X86_BUG_GDS))
2189 		update_gds_msr();
2190 
2191 	tsx_ap_init();
2192 	c->initialized = true;
2193 }
2194 
2195 void print_cpu_info(struct cpuinfo_x86 *c)
2196 {
2197 	const char *vendor = NULL;
2198 
2199 	if (c->x86_vendor < X86_VENDOR_NUM) {
2200 		vendor = this_cpu->c_vendor;
2201 	} else {
2202 		if (c->cpuid_level >= 0)
2203 			vendor = c->x86_vendor_id;
2204 	}
2205 
2206 	if (vendor && !strstr(c->x86_model_id, vendor))
2207 		pr_cont("%s ", vendor);
2208 
2209 	if (c->x86_model_id[0])
2210 		pr_cont("%s", c->x86_model_id);
2211 	else
2212 		pr_cont("%d86", c->x86);
2213 
2214 	pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
2215 
2216 	if (c->x86_stepping || c->cpuid_level >= 0)
2217 		pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
2218 	else
2219 		pr_cont(")\n");
2220 }
2221 
2222 /*
2223  * clearcpuid= and setcpuid= were already parsed in cpu_parse_early_param().
2224  * These dummy functions prevent them from becoming an environment variable for
2225  * init.
2226  */
2227 
2228 static __init int setup_clearcpuid(char *arg)
2229 {
2230 	return 1;
2231 }
2232 __setup("clearcpuid=", setup_clearcpuid);
2233 
2234 static __init int setup_setcpuid(char *arg)
2235 {
2236 	return 1;
2237 }
2238 __setup("setcpuid=", setup_setcpuid);
2239 
2240 DEFINE_PER_CPU_CACHE_HOT(struct task_struct *, current_task) = &init_task;
2241 EXPORT_PER_CPU_SYMBOL(current_task);
2242 EXPORT_PER_CPU_SYMBOL(const_current_task);
2243 
2244 DEFINE_PER_CPU_CACHE_HOT(unsigned long, __preempt_count) = INIT_PREEMPT_COUNT;
2245 EXPORT_PER_CPU_SYMBOL(__preempt_count);
2246 
2247 DEFINE_PER_CPU_CACHE_HOT(unsigned long, cpu_current_top_of_stack) = TOP_OF_INIT_STACK;
2248 
2249 #ifdef CONFIG_X86_64
2250 /*
2251  * Note: Do not make this dependant on CONFIG_MITIGATION_CALL_DEPTH_TRACKING
2252  * so that this space is reserved in the hot cache section even when the
2253  * mitigation is disabled.
2254  */
2255 DEFINE_PER_CPU_CACHE_HOT(u64, __x86_call_depth);
2256 EXPORT_PER_CPU_SYMBOL(__x86_call_depth);
2257 
2258 static void wrmsrq_cstar(unsigned long val)
2259 {
2260 	/*
2261 	 * Intel CPUs do not support 32-bit SYSCALL. Writing to MSR_CSTAR
2262 	 * is so far ignored by the CPU, but raises a #VE trap in a TDX
2263 	 * guest. Avoid the pointless write on all Intel CPUs.
2264 	 */
2265 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
2266 		wrmsrq(MSR_CSTAR, val);
2267 }
2268 
2269 static inline void idt_syscall_init(void)
2270 {
2271 	wrmsrq(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
2272 
2273 	if (ia32_enabled()) {
2274 		wrmsrq_cstar((unsigned long)entry_SYSCALL_compat);
2275 		/*
2276 		 * This only works on Intel CPUs.
2277 		 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
2278 		 * This does not cause SYSENTER to jump to the wrong location, because
2279 		 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
2280 		 */
2281 		wrmsrq_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
2282 		wrmsrq_safe(MSR_IA32_SYSENTER_ESP,
2283 			    (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1));
2284 		wrmsrq_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
2285 	} else {
2286 		wrmsrq_cstar((unsigned long)entry_SYSCALL32_ignore);
2287 		wrmsrq_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
2288 		wrmsrq_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
2289 		wrmsrq_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
2290 	}
2291 
2292 	/*
2293 	 * Flags to clear on syscall; clear as much as possible
2294 	 * to minimize user space-kernel interference.
2295 	 */
2296 	wrmsrq(MSR_SYSCALL_MASK,
2297 	       X86_EFLAGS_CF|X86_EFLAGS_PF|X86_EFLAGS_AF|
2298 	       X86_EFLAGS_ZF|X86_EFLAGS_SF|X86_EFLAGS_TF|
2299 	       X86_EFLAGS_IF|X86_EFLAGS_DF|X86_EFLAGS_OF|
2300 	       X86_EFLAGS_IOPL|X86_EFLAGS_NT|X86_EFLAGS_RF|
2301 	       X86_EFLAGS_AC|X86_EFLAGS_ID);
2302 }
2303 
2304 /* May not be marked __init: used by software suspend */
2305 void syscall_init(void)
2306 {
2307 	struct msr val = { .h = (__USER32_CS << 16) | __KERNEL_CS };
2308 
2309 	/* The default user and kernel segments */
2310 	wrmsrq(MSR_STAR, val.q);
2311 
2312 	/*
2313 	 * Except the IA32_STAR MSR, there is NO need to setup SYSCALL and
2314 	 * SYSENTER MSRs for FRED, because FRED uses the ring 3 FRED
2315 	 * entrypoint for SYSCALL and SYSENTER, and ERETU is the only legit
2316 	 * instruction to return to ring 3 (both sysexit and sysret cause
2317 	 * #UD when FRED is enabled).
2318 	 */
2319 	if (!cpu_feature_enabled(X86_FEATURE_FRED))
2320 		idt_syscall_init();
2321 }
2322 #endif /* CONFIG_X86_64 */
2323 
2324 #ifdef CONFIG_STACKPROTECTOR
2325 DEFINE_PER_CPU_CACHE_HOT(unsigned long, __stack_chk_guard);
2326 #ifndef CONFIG_SMP
2327 EXPORT_PER_CPU_SYMBOL(__stack_chk_guard);
2328 #endif
2329 #endif
2330 
2331 static void initialize_debug_regs(void)
2332 {
2333 	/* Control register first -- to make sure everything is disabled. */
2334 	set_debugreg(DR7_FIXED_1, 7);
2335 	set_debugreg(DR6_RESERVED, 6);
2336 	/* dr5 and dr4 don't exist */
2337 	set_debugreg(0, 3);
2338 	set_debugreg(0, 2);
2339 	set_debugreg(0, 1);
2340 	set_debugreg(0, 0);
2341 }
2342 
2343 #ifdef CONFIG_KGDB
2344 /*
2345  * Restore debug regs if using kgdbwait and you have a kernel debugger
2346  * connection established.
2347  */
2348 static void dbg_restore_debug_regs(void)
2349 {
2350 	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
2351 		arch_kgdb_ops.correct_hw_break();
2352 }
2353 #else /* ! CONFIG_KGDB */
2354 #define dbg_restore_debug_regs()
2355 #endif /* ! CONFIG_KGDB */
2356 
2357 static inline void setup_getcpu(int cpu)
2358 {
2359 	unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
2360 	struct desc_struct d = { };
2361 
2362 	if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID))
2363 		wrmsrq(MSR_TSC_AUX, cpudata);
2364 
2365 	/* Store CPU and node number in limit. */
2366 	d.limit0 = cpudata;
2367 	d.limit1 = cpudata >> 16;
2368 
2369 	d.type = 5;		/* RO data, expand down, accessed */
2370 	d.dpl = 3;		/* Visible to user code */
2371 	d.s = 1;		/* Not a system segment */
2372 	d.p = 1;		/* Present */
2373 	d.d = 1;		/* 32-bit */
2374 
2375 	write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S);
2376 }
2377 
2378 #ifdef CONFIG_X86_64
2379 static inline void tss_setup_ist(struct tss_struct *tss)
2380 {
2381 	/* Set up the per-CPU TSS IST stacks */
2382 	tss->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
2383 	tss->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
2384 	tss->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
2385 	tss->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
2386 	/* Only mapped when SEV-ES is active */
2387 	tss->x86_tss.ist[IST_INDEX_VC] = __this_cpu_ist_top_va(VC);
2388 }
2389 #else /* CONFIG_X86_64 */
2390 static inline void tss_setup_ist(struct tss_struct *tss) { }
2391 #endif /* !CONFIG_X86_64 */
2392 
2393 static inline void tss_setup_io_bitmap(struct tss_struct *tss)
2394 {
2395 	tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID;
2396 
2397 #ifdef CONFIG_X86_IOPL_IOPERM
2398 	tss->io_bitmap.prev_max = 0;
2399 	tss->io_bitmap.prev_sequence = 0;
2400 	memset(tss->io_bitmap.bitmap, 0xff, sizeof(tss->io_bitmap.bitmap));
2401 	/*
2402 	 * Invalidate the extra array entry past the end of the all
2403 	 * permission bitmap as required by the hardware.
2404 	 */
2405 	tss->io_bitmap.mapall[IO_BITMAP_LONGS] = ~0UL;
2406 #endif
2407 }
2408 
2409 /*
2410  * Setup everything needed to handle exceptions from the IDT, including the IST
2411  * exceptions which use paranoid_entry().
2412  */
2413 void cpu_init_exception_handling(bool boot_cpu)
2414 {
2415 	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
2416 	int cpu = raw_smp_processor_id();
2417 
2418 	/* paranoid_entry() gets the CPU number from the GDT */
2419 	setup_getcpu(cpu);
2420 
2421 	/* For IDT mode, IST vectors need to be set in TSS. */
2422 	if (!cpu_feature_enabled(X86_FEATURE_FRED))
2423 		tss_setup_ist(tss);
2424 	tss_setup_io_bitmap(tss);
2425 	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
2426 
2427 	load_TR_desc();
2428 
2429 	/* GHCB needs to be setup to handle #VC. */
2430 	setup_ghcb();
2431 
2432 	/*
2433 	 * On CPUs with FSGSBASE support, paranoid_entry() uses
2434 	 * ALTERNATIVE-patched RDGSBASE/WRGSBASE instructions. Secondary CPUs
2435 	 * boot after alternatives are patched globally, so early exceptions
2436 	 * execute patched code that depends on FSGSBASE. Enable the feature
2437 	 * before any exceptions occur.
2438 	 */
2439 	if (cpu_feature_enabled(X86_FEATURE_FSGSBASE)) {
2440 		cr4_set_bits(X86_CR4_FSGSBASE);
2441 		elf_hwcap2 |= HWCAP2_FSGSBASE;
2442 	}
2443 
2444 	if (cpu_feature_enabled(X86_FEATURE_FRED)) {
2445 		/* The boot CPU has enabled FRED during early boot */
2446 		if (!boot_cpu)
2447 			cpu_init_fred_exceptions();
2448 
2449 		cpu_init_fred_rsps();
2450 	} else {
2451 		load_current_idt();
2452 	}
2453 }
2454 
2455 void __init cpu_init_replace_early_idt(void)
2456 {
2457 	if (cpu_feature_enabled(X86_FEATURE_FRED))
2458 		cpu_init_fred_exceptions();
2459 	else
2460 		idt_setup_early_pf();
2461 }
2462 
2463 /*
2464  * cpu_init() initializes state that is per-CPU. Some data is already
2465  * initialized (naturally) in the bootstrap process, such as the GDT.  We
2466  * reload it nevertheless, this function acts as a 'CPU state barrier',
2467  * nothing should get across.
2468  */
2469 void cpu_init(void)
2470 {
2471 	struct task_struct *cur = current;
2472 	int cpu = raw_smp_processor_id();
2473 
2474 #ifdef CONFIG_NUMA
2475 	if (this_cpu_read(numa_node) == 0 &&
2476 	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
2477 		set_numa_node(early_cpu_to_node(cpu));
2478 #endif
2479 	pr_debug("Initializing CPU#%d\n", cpu);
2480 
2481 	if (IS_ENABLED(CONFIG_X86_64) || cpu_feature_enabled(X86_FEATURE_VME) ||
2482 	    boot_cpu_has(X86_FEATURE_TSC) || boot_cpu_has(X86_FEATURE_DE))
2483 		cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
2484 
2485 	if (IS_ENABLED(CONFIG_X86_64)) {
2486 		loadsegment(fs, 0);
2487 		memset(cur->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
2488 		syscall_init();
2489 
2490 		wrmsrq(MSR_FS_BASE, 0);
2491 		wrmsrq(MSR_KERNEL_GS_BASE, 0);
2492 		barrier();
2493 
2494 		x2apic_setup();
2495 
2496 		intel_posted_msi_init();
2497 	}
2498 
2499 	mmgrab(&init_mm);
2500 	cur->active_mm = &init_mm;
2501 	BUG_ON(cur->mm);
2502 	initialize_tlbstate_and_flush();
2503 	enter_lazy_tlb(&init_mm, cur);
2504 
2505 	/*
2506 	 * sp0 points to the entry trampoline stack regardless of what task
2507 	 * is running.
2508 	 */
2509 	load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
2510 
2511 	load_mm_ldt(&init_mm);
2512 
2513 	initialize_debug_regs();
2514 	dbg_restore_debug_regs();
2515 
2516 	doublefault_init_cpu_tss();
2517 
2518 	if (is_uv_system())
2519 		uv_cpu_init();
2520 
2521 	load_fixmap_gdt(cpu);
2522 }
2523 
2524 #ifdef CONFIG_MICROCODE_LATE_LOADING
2525 /**
2526  * store_cpu_caps() - Store a snapshot of CPU capabilities
2527  * @curr_info: Pointer where to store it
2528  *
2529  * Returns: None
2530  */
2531 void store_cpu_caps(struct cpuinfo_x86 *curr_info)
2532 {
2533 	/* Reload CPUID max function as it might've changed. */
2534 	curr_info->cpuid_level = cpuid_eax(0);
2535 
2536 	/* Copy all capability leafs and pick up the synthetic ones. */
2537 	memcpy(&curr_info->x86_capability, &boot_cpu_data.x86_capability,
2538 	       sizeof(curr_info->x86_capability));
2539 
2540 	/* Get the hardware CPUID leafs */
2541 	get_cpu_cap(curr_info);
2542 }
2543 
2544 /**
2545  * microcode_check() - Check if any CPU capabilities changed after an update.
2546  * @prev_info:	CPU capabilities stored before an update.
2547  *
2548  * The microcode loader calls this upon late microcode load to recheck features,
2549  * only when microcode has been updated. Caller holds and CPU hotplug lock.
2550  *
2551  * Return: None
2552  */
2553 void microcode_check(struct cpuinfo_x86 *prev_info)
2554 {
2555 	struct cpuinfo_x86 curr_info;
2556 
2557 	perf_check_microcode();
2558 
2559 	amd_check_microcode();
2560 
2561 	store_cpu_caps(&curr_info);
2562 
2563 	if (!memcmp(&prev_info->x86_capability, &curr_info.x86_capability,
2564 		    sizeof(prev_info->x86_capability)))
2565 		return;
2566 
2567 	pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
2568 	pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
2569 }
2570 #endif
2571 
2572 /*
2573  * Invoked from core CPU hotplug code after hotplug operations
2574  */
2575 void arch_smt_update(void)
2576 {
2577 	/* Handle the speculative execution misfeatures */
2578 	cpu_bugs_smt_update();
2579 	/* Check whether IPI broadcasting can be enabled */
2580 	apic_smt_update();
2581 }
2582 
2583 void __init arch_cpu_finalize_init(void)
2584 {
2585 	struct cpuinfo_x86 *c = this_cpu_ptr(&cpu_info);
2586 
2587 	identify_boot_cpu();
2588 
2589 	select_idle_routine();
2590 
2591 	/*
2592 	 * identify_boot_cpu() initialized SMT support information, let the
2593 	 * core code know.
2594 	 */
2595 	cpu_smt_set_num_threads(__max_threads_per_core, __max_threads_per_core);
2596 
2597 	if (!IS_ENABLED(CONFIG_SMP)) {
2598 		pr_info("CPU: ");
2599 		print_cpu_info(&boot_cpu_data);
2600 	}
2601 
2602 	cpu_select_mitigations();
2603 
2604 	arch_smt_update();
2605 
2606 	if (IS_ENABLED(CONFIG_X86_32)) {
2607 		/*
2608 		 * Check whether this is a real i386 which is not longer
2609 		 * supported and fixup the utsname.
2610 		 */
2611 		if (boot_cpu_data.x86 < 4)
2612 			panic("Kernel requires i486+ for 'invlpg' and other features");
2613 
2614 		init_utsname()->machine[1] =
2615 			'0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
2616 	}
2617 
2618 	/*
2619 	 * Must be before alternatives because it might set or clear
2620 	 * feature bits.
2621 	 */
2622 	fpu__init_system();
2623 	fpu__init_cpu();
2624 
2625 	/*
2626 	 * This needs to follow the FPU initializtion, since EFI depends on it.
2627 	 */
2628 	if (efi_enabled(EFI_RUNTIME_SERVICES))
2629 		efi_enter_virtual_mode();
2630 
2631 	/*
2632 	 * Ensure that access to the per CPU representation has the initial
2633 	 * boot CPU configuration.
2634 	 */
2635 	*c = boot_cpu_data;
2636 	c->initialized = true;
2637 
2638 	alternative_instructions();
2639 
2640 	if (IS_ENABLED(CONFIG_X86_64)) {
2641 		USER_PTR_MAX = TASK_SIZE_MAX;
2642 
2643 		/*
2644 		 * Enable this when LAM is gated on LASS support
2645 		if (cpu_feature_enabled(X86_FEATURE_LAM))
2646 			USER_PTR_MAX = (1ul << 63) - PAGE_SIZE;
2647 		 */
2648 		runtime_const_init(ptr, USER_PTR_MAX);
2649 
2650 		/*
2651 		 * Make sure the first 2MB area is not mapped by huge pages
2652 		 * There are typically fixed size MTRRs in there and overlapping
2653 		 * MTRRs into large pages causes slow downs.
2654 		 *
2655 		 * Right now we don't do that with gbpages because there seems
2656 		 * very little benefit for that case.
2657 		 */
2658 		if (!direct_gbpages)
2659 			set_memory_4k((unsigned long)__va(0), 1);
2660 	} else {
2661 		fpu__init_check_bugs();
2662 	}
2663 
2664 	/*
2665 	 * This needs to be called before any devices perform DMA
2666 	 * operations that might use the SWIOTLB bounce buffers. It will
2667 	 * mark the bounce buffers as decrypted so that their usage will
2668 	 * not cause "plain-text" data to be decrypted when accessed. It
2669 	 * must be called after late_time_init() so that Hyper-V x86/x64
2670 	 * hypercalls work when the SWIOTLB bounce buffers are decrypted.
2671 	 */
2672 	mem_encrypt_init();
2673 }
2674 
2675 /* Control TLB flushing methods */
2676 static int __init tlbi_setup(char *str)
2677 {
2678 	if (!strcmp(str, "ipi"))
2679 		setup_clear_cpu_cap(X86_FEATURE_INVLPGB);
2680 
2681 	return 1;
2682 }
2683 __setup("tlbi=", tlbi_setup);
2684