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