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/init.h>
18 #include <linux/kprobes.h>
19 #include <linux/kgdb.h>
20 #include <linux/mem_encrypt.h>
21 #include <linux/smp.h>
22 #include <linux/cpu.h>
23 #include <linux/io.h>
24 #include <linux/syscore_ops.h>
25
26 #include <asm/stackprotector.h>
27 #include <linux/utsname.h>
28
29 #include <asm/alternative.h>
30 #include <asm/perf_event.h>
31 #include <asm/mmu_context.h>
32 #include <asm/archrandom.h>
33 #include <asm/hypervisor.h>
34 #include <asm/processor.h>
35 #include <asm/tlbflush.h>
36 #include <asm/debugreg.h>
37 #include <asm/sections.h>
38 #include <asm/vsyscall.h>
39 #include <linux/topology.h>
40 #include <linux/cpumask.h>
41 #include <asm/pgtable.h>
42 #include <linux/atomic.h>
43 #include <asm/proto.h>
44 #include <asm/setup.h>
45 #include <asm/apic.h>
46 #include <asm/desc.h>
47 #include <asm/fpu/internal.h>
48 #include <asm/mtrr.h>
49 #include <asm/hwcap2.h>
50 #include <linux/numa.h>
51 #include <asm/asm.h>
52 #include <asm/bugs.h>
53 #include <asm/cpu.h>
54 #include <asm/mce.h>
55 #include <asm/msr.h>
56 #include <asm/pat.h>
57 #include <asm/microcode.h>
58 #include <asm/microcode_intel.h>
59 #include <asm/intel-family.h>
60 #include <asm/cpu_device_id.h>
61
62 #ifdef CONFIG_X86_LOCAL_APIC
63 #include <asm/uv/uv.h>
64 #endif
65 #include <asm/set_memory.h>
66
67 #include "cpu.h"
68
69 u32 elf_hwcap2 __read_mostly;
70
71 /* all of these masks are initialized in setup_cpu_local_masks() */
72 cpumask_var_t cpu_initialized_mask;
73 cpumask_var_t cpu_callout_mask;
74 cpumask_var_t cpu_callin_mask;
75
76 /* representing cpus for which sibling maps can be computed */
77 cpumask_var_t cpu_sibling_setup_mask;
78
79 /* Number of siblings per CPU package */
80 int smp_num_siblings = 1;
81 EXPORT_SYMBOL(smp_num_siblings);
82
83 /* Last level cache ID of each logical CPU */
84 DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
85
86 /* correctly size the local cpu masks */
setup_cpu_local_masks(void)87 void __init setup_cpu_local_masks(void)
88 {
89 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
90 alloc_bootmem_cpumask_var(&cpu_callin_mask);
91 alloc_bootmem_cpumask_var(&cpu_callout_mask);
92 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
93 }
94
default_init(struct cpuinfo_x86 * c)95 static void default_init(struct cpuinfo_x86 *c)
96 {
97 #ifdef CONFIG_X86_64
98 cpu_detect_cache_sizes(c);
99 #else
100 /* Not much we can do here... */
101 /* Check if at least it has cpuid */
102 if (c->cpuid_level == -1) {
103 /* No cpuid. It must be an ancient CPU */
104 if (c->x86 == 4)
105 strcpy(c->x86_model_id, "486");
106 else if (c->x86 == 3)
107 strcpy(c->x86_model_id, "386");
108 }
109 #endif
110 }
111
112 static const struct cpu_dev default_cpu = {
113 .c_init = default_init,
114 .c_vendor = "Unknown",
115 .c_x86_vendor = X86_VENDOR_UNKNOWN,
116 };
117
118 static const struct cpu_dev *this_cpu = &default_cpu;
119
120 DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
121 #ifdef CONFIG_X86_64
122 /*
123 * We need valid kernel segments for data and code in long mode too
124 * IRET will check the segment types kkeil 2000/10/28
125 * Also sysret mandates a special GDT layout
126 *
127 * TLS descriptors are currently at a different place compared to i386.
128 * Hopefully nobody expects them at a fixed place (Wine?)
129 */
130 [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
131 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
132 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
133 [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
134 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
135 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
136 #else
137 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
138 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
139 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
140 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
141 /*
142 * Segments used for calling PnP BIOS have byte granularity.
143 * They code segments and data segments have fixed 64k limits,
144 * the transfer segment sizes are set at run time.
145 */
146 /* 32-bit code */
147 [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
148 /* 16-bit code */
149 [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
150 /* 16-bit data */
151 [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
152 /* 16-bit data */
153 [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
154 /* 16-bit data */
155 [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
156 /*
157 * The APM segments have byte granularity and their bases
158 * are set at run time. All have 64k limits.
159 */
160 /* 32-bit code */
161 [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
162 /* 16-bit code */
163 [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
164 /* data */
165 [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
166
167 [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
168 [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
169 GDT_STACK_CANARY_INIT
170 #endif
171 } };
172 EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
173
x86_mpx_setup(char * s)174 static int __init x86_mpx_setup(char *s)
175 {
176 /* require an exact match without trailing characters */
177 if (strlen(s))
178 return 0;
179
180 /* do not emit a message if the feature is not present */
181 if (!boot_cpu_has(X86_FEATURE_MPX))
182 return 1;
183
184 setup_clear_cpu_cap(X86_FEATURE_MPX);
185 pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
186 return 1;
187 }
188 __setup("nompx", x86_mpx_setup);
189
190 #ifdef CONFIG_X86_64
x86_nopcid_setup(char * s)191 static int __init x86_nopcid_setup(char *s)
192 {
193 /* nopcid doesn't accept parameters */
194 if (s)
195 return -EINVAL;
196
197 /* do not emit a message if the feature is not present */
198 if (!boot_cpu_has(X86_FEATURE_PCID))
199 return 0;
200
201 setup_clear_cpu_cap(X86_FEATURE_PCID);
202 pr_info("nopcid: PCID feature disabled\n");
203 return 0;
204 }
205 early_param("nopcid", x86_nopcid_setup);
206 #endif
207
x86_noinvpcid_setup(char * s)208 static int __init x86_noinvpcid_setup(char *s)
209 {
210 /* noinvpcid doesn't accept parameters */
211 if (s)
212 return -EINVAL;
213
214 /* do not emit a message if the feature is not present */
215 if (!boot_cpu_has(X86_FEATURE_INVPCID))
216 return 0;
217
218 setup_clear_cpu_cap(X86_FEATURE_INVPCID);
219 pr_info("noinvpcid: INVPCID feature disabled\n");
220 return 0;
221 }
222 early_param("noinvpcid", x86_noinvpcid_setup);
223
224 #ifdef CONFIG_X86_32
225 static int cachesize_override = -1;
226 static int disable_x86_serial_nr = 1;
227
cachesize_setup(char * str)228 static int __init cachesize_setup(char *str)
229 {
230 get_option(&str, &cachesize_override);
231 return 1;
232 }
233 __setup("cachesize=", cachesize_setup);
234
x86_sep_setup(char * s)235 static int __init x86_sep_setup(char *s)
236 {
237 setup_clear_cpu_cap(X86_FEATURE_SEP);
238 return 1;
239 }
240 __setup("nosep", x86_sep_setup);
241
242 /* Standard macro to see if a specific flag is changeable */
flag_is_changeable_p(u32 flag)243 static inline int flag_is_changeable_p(u32 flag)
244 {
245 u32 f1, f2;
246
247 /*
248 * Cyrix and IDT cpus allow disabling of CPUID
249 * so the code below may return different results
250 * when it is executed before and after enabling
251 * the CPUID. Add "volatile" to not allow gcc to
252 * optimize the subsequent calls to this function.
253 */
254 asm volatile ("pushfl \n\t"
255 "pushfl \n\t"
256 "popl %0 \n\t"
257 "movl %0, %1 \n\t"
258 "xorl %2, %0 \n\t"
259 "pushl %0 \n\t"
260 "popfl \n\t"
261 "pushfl \n\t"
262 "popl %0 \n\t"
263 "popfl \n\t"
264
265 : "=&r" (f1), "=&r" (f2)
266 : "ir" (flag));
267
268 return ((f1^f2) & flag) != 0;
269 }
270
271 /* Probe for the CPUID instruction */
have_cpuid_p(void)272 int have_cpuid_p(void)
273 {
274 return flag_is_changeable_p(X86_EFLAGS_ID);
275 }
276
squash_the_stupid_serial_number(struct cpuinfo_x86 * c)277 static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
278 {
279 unsigned long lo, hi;
280
281 if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
282 return;
283
284 /* Disable processor serial number: */
285
286 rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
287 lo |= 0x200000;
288 wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
289
290 pr_notice("CPU serial number disabled.\n");
291 clear_cpu_cap(c, X86_FEATURE_PN);
292
293 /* Disabling the serial number may affect the cpuid level */
294 c->cpuid_level = cpuid_eax(0);
295 }
296
x86_serial_nr_setup(char * s)297 static int __init x86_serial_nr_setup(char *s)
298 {
299 disable_x86_serial_nr = 0;
300 return 1;
301 }
302 __setup("serialnumber", x86_serial_nr_setup);
303 #else
flag_is_changeable_p(u32 flag)304 static inline int flag_is_changeable_p(u32 flag)
305 {
306 return 1;
307 }
squash_the_stupid_serial_number(struct cpuinfo_x86 * c)308 static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
309 {
310 }
311 #endif
312
setup_disable_smep(char * arg)313 static __init int setup_disable_smep(char *arg)
314 {
315 setup_clear_cpu_cap(X86_FEATURE_SMEP);
316 /* Check for things that depend on SMEP being enabled: */
317 check_mpx_erratum(&boot_cpu_data);
318 return 1;
319 }
320 __setup("nosmep", setup_disable_smep);
321
setup_smep(struct cpuinfo_x86 * c)322 static __always_inline void setup_smep(struct cpuinfo_x86 *c)
323 {
324 if (cpu_has(c, X86_FEATURE_SMEP))
325 cr4_set_bits(X86_CR4_SMEP);
326 }
327
setup_disable_smap(char * arg)328 static __init int setup_disable_smap(char *arg)
329 {
330 setup_clear_cpu_cap(X86_FEATURE_SMAP);
331 return 1;
332 }
333 __setup("nosmap", setup_disable_smap);
334
setup_smap(struct cpuinfo_x86 * c)335 static __always_inline void setup_smap(struct cpuinfo_x86 *c)
336 {
337 unsigned long eflags = native_save_fl();
338
339 /* This should have been cleared long ago */
340 BUG_ON(eflags & X86_EFLAGS_AC);
341
342 if (cpu_has(c, X86_FEATURE_SMAP)) {
343 #ifdef CONFIG_X86_SMAP
344 cr4_set_bits(X86_CR4_SMAP);
345 #else
346 cr4_clear_bits(X86_CR4_SMAP);
347 #endif
348 }
349 }
350
setup_umip(struct cpuinfo_x86 * c)351 static __always_inline void setup_umip(struct cpuinfo_x86 *c)
352 {
353 /* Check the boot processor, plus build option for UMIP. */
354 if (!cpu_feature_enabled(X86_FEATURE_UMIP))
355 goto out;
356
357 /* Check the current processor's cpuid bits. */
358 if (!cpu_has(c, X86_FEATURE_UMIP))
359 goto out;
360
361 cr4_set_bits(X86_CR4_UMIP);
362
363 pr_info_once("x86/cpu: User Mode Instruction Prevention (UMIP) activated\n");
364
365 return;
366
367 out:
368 /*
369 * Make sure UMIP is disabled in case it was enabled in a
370 * previous boot (e.g., via kexec).
371 */
372 cr4_clear_bits(X86_CR4_UMIP);
373 }
374
375 /* These bits should not change their value after CPU init is finished. */
376 static const unsigned long cr4_pinned_mask =
377 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP | X86_CR4_FSGSBASE;
378 static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
379 static unsigned long cr4_pinned_bits __ro_after_init;
380
native_write_cr0(unsigned long val)381 void native_write_cr0(unsigned long val)
382 {
383 unsigned long bits_missing = 0;
384
385 set_register:
386 asm volatile("mov %0,%%cr0": "+r" (val) : : "memory");
387
388 if (static_branch_likely(&cr_pinning)) {
389 if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) {
390 bits_missing = X86_CR0_WP;
391 val |= bits_missing;
392 goto set_register;
393 }
394 /* Warn after we've set the missing bits. */
395 WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n");
396 }
397 }
398 EXPORT_SYMBOL(native_write_cr0);
399
native_write_cr4(unsigned long val)400 void native_write_cr4(unsigned long val)
401 {
402 unsigned long bits_changed = 0;
403
404 set_register:
405 asm volatile("mov %0,%%cr4": "+r" (val) : : "memory");
406
407 if (static_branch_likely(&cr_pinning)) {
408 if (unlikely((val & cr4_pinned_mask) != cr4_pinned_bits)) {
409 bits_changed = (val & cr4_pinned_mask) ^ cr4_pinned_bits;
410 val = (val & ~cr4_pinned_mask) | cr4_pinned_bits;
411 goto set_register;
412 }
413 /* Warn after we've corrected the changed bits. */
414 WARN_ONCE(bits_changed, "pinned CR4 bits changed: 0x%lx!?\n",
415 bits_changed);
416 }
417 }
418 EXPORT_SYMBOL(native_write_cr4);
419
cr4_init(void)420 void cr4_init(void)
421 {
422 unsigned long cr4 = __read_cr4();
423
424 if (boot_cpu_has(X86_FEATURE_PCID))
425 cr4 |= X86_CR4_PCIDE;
426 if (static_branch_likely(&cr_pinning))
427 cr4 = (cr4 & ~cr4_pinned_mask) | cr4_pinned_bits;
428
429 __write_cr4(cr4);
430
431 /* Initialize cr4 shadow for this CPU. */
432 this_cpu_write(cpu_tlbstate.cr4, cr4);
433 }
434
435 /*
436 * Once CPU feature detection is finished (and boot params have been
437 * parsed), record any of the sensitive CR bits that are set, and
438 * enable CR pinning.
439 */
setup_cr_pinning(void)440 static void __init setup_cr_pinning(void)
441 {
442 cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & cr4_pinned_mask;
443 static_key_enable(&cr_pinning.key);
444 }
445
446 /*
447 * Protection Keys are not available in 32-bit mode.
448 */
449 static bool pku_disabled;
450
setup_pku(struct cpuinfo_x86 * c)451 static __always_inline void setup_pku(struct cpuinfo_x86 *c)
452 {
453 /* check the boot processor, plus compile options for PKU: */
454 if (!cpu_feature_enabled(X86_FEATURE_PKU))
455 return;
456 /* checks the actual processor's cpuid bits: */
457 if (!cpu_has(c, X86_FEATURE_PKU))
458 return;
459 if (pku_disabled)
460 return;
461
462 cr4_set_bits(X86_CR4_PKE);
463 /*
464 * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
465 * cpuid bit to be set. We need to ensure that we
466 * update that bit in this CPU's "cpu_info".
467 */
468 set_cpu_cap(c, X86_FEATURE_OSPKE);
469 }
470
471 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
setup_disable_pku(char * arg)472 static __init int setup_disable_pku(char *arg)
473 {
474 /*
475 * Do not clear the X86_FEATURE_PKU bit. All of the
476 * runtime checks are against OSPKE so clearing the
477 * bit does nothing.
478 *
479 * This way, we will see "pku" in cpuinfo, but not
480 * "ospke", which is exactly what we want. It shows
481 * that the CPU has PKU, but the OS has not enabled it.
482 * This happens to be exactly how a system would look
483 * if we disabled the config option.
484 */
485 pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
486 pku_disabled = true;
487 return 1;
488 }
489 __setup("nopku", setup_disable_pku);
490 #endif /* CONFIG_X86_64 */
491
492 /*
493 * Some CPU features depend on higher CPUID levels, which may not always
494 * be available due to CPUID level capping or broken virtualization
495 * software. Add those features to this table to auto-disable them.
496 */
497 struct cpuid_dependent_feature {
498 u32 feature;
499 u32 level;
500 };
501
502 static const struct cpuid_dependent_feature
503 cpuid_dependent_features[] = {
504 { X86_FEATURE_MWAIT, 0x00000005 },
505 { X86_FEATURE_DCA, 0x00000009 },
506 { X86_FEATURE_XSAVE, 0x0000000d },
507 { 0, 0 }
508 };
509
filter_cpuid_features(struct cpuinfo_x86 * c,bool warn)510 static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
511 {
512 const struct cpuid_dependent_feature *df;
513
514 for (df = cpuid_dependent_features; df->feature; df++) {
515
516 if (!cpu_has(c, df->feature))
517 continue;
518 /*
519 * Note: cpuid_level is set to -1 if unavailable, but
520 * extended_extended_level is set to 0 if unavailable
521 * and the legitimate extended levels are all negative
522 * when signed; hence the weird messing around with
523 * signs here...
524 */
525 if (!((s32)df->level < 0 ?
526 (u32)df->level > (u32)c->extended_cpuid_level :
527 (s32)df->level > (s32)c->cpuid_level))
528 continue;
529
530 clear_cpu_cap(c, df->feature);
531 if (!warn)
532 continue;
533
534 pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
535 x86_cap_flag(df->feature), df->level);
536 }
537 }
538
539 /*
540 * Naming convention should be: <Name> [(<Codename>)]
541 * This table only is used unless init_<vendor>() below doesn't set it;
542 * in particular, if CPUID levels 0x80000002..4 are supported, this
543 * isn't used
544 */
545
546 /* Look up CPU names by table lookup. */
table_lookup_model(struct cpuinfo_x86 * c)547 static const char *table_lookup_model(struct cpuinfo_x86 *c)
548 {
549 #ifdef CONFIG_X86_32
550 const struct legacy_cpu_model_info *info;
551
552 if (c->x86_model >= 16)
553 return NULL; /* Range check */
554
555 if (!this_cpu)
556 return NULL;
557
558 info = this_cpu->legacy_models;
559
560 while (info->family) {
561 if (info->family == c->x86)
562 return info->model_names[c->x86_model];
563 info++;
564 }
565 #endif
566 return NULL; /* Not found */
567 }
568
569 __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS];
570 __u32 cpu_caps_set[NCAPINTS + NBUGINTS];
571
load_percpu_segment(int cpu)572 void load_percpu_segment(int cpu)
573 {
574 #ifdef CONFIG_X86_32
575 loadsegment(fs, __KERNEL_PERCPU);
576 #else
577 __loadsegment_simple(gs, 0);
578 wrmsrl(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu));
579 #endif
580 load_stack_canary_segment();
581 }
582
583 #ifdef CONFIG_X86_32
584 /* The 32-bit entry code needs to find cpu_entry_area. */
585 DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
586 #endif
587
588 /* Load the original GDT from the per-cpu structure */
load_direct_gdt(int cpu)589 void load_direct_gdt(int cpu)
590 {
591 struct desc_ptr gdt_descr;
592
593 gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
594 gdt_descr.size = GDT_SIZE - 1;
595 load_gdt(&gdt_descr);
596 }
597 EXPORT_SYMBOL_GPL(load_direct_gdt);
598
599 /* Load a fixmap remapping of the per-cpu GDT */
load_fixmap_gdt(int cpu)600 void load_fixmap_gdt(int cpu)
601 {
602 struct desc_ptr gdt_descr;
603
604 gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
605 gdt_descr.size = GDT_SIZE - 1;
606 load_gdt(&gdt_descr);
607 }
608 EXPORT_SYMBOL_GPL(load_fixmap_gdt);
609
610 /*
611 * Current gdt points %fs at the "master" per-cpu area: after this,
612 * it's on the real one.
613 */
switch_to_new_gdt(int cpu)614 void switch_to_new_gdt(int cpu)
615 {
616 /* Load the original GDT */
617 load_direct_gdt(cpu);
618 /* Reload the per-cpu base */
619 load_percpu_segment(cpu);
620 }
621
622 static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
623
get_model_name(struct cpuinfo_x86 * c)624 static void get_model_name(struct cpuinfo_x86 *c)
625 {
626 unsigned int *v;
627 char *p, *q, *s;
628
629 if (c->extended_cpuid_level < 0x80000004)
630 return;
631
632 v = (unsigned int *)c->x86_model_id;
633 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
634 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
635 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
636 c->x86_model_id[48] = 0;
637
638 /* Trim whitespace */
639 p = q = s = &c->x86_model_id[0];
640
641 while (*p == ' ')
642 p++;
643
644 while (*p) {
645 /* Note the last non-whitespace index */
646 if (!isspace(*p))
647 s = q;
648
649 *q++ = *p++;
650 }
651
652 *(s + 1) = '\0';
653 }
654
detect_num_cpu_cores(struct cpuinfo_x86 * c)655 void detect_num_cpu_cores(struct cpuinfo_x86 *c)
656 {
657 unsigned int eax, ebx, ecx, edx;
658
659 c->x86_max_cores = 1;
660 if (!IS_ENABLED(CONFIG_SMP) || c->cpuid_level < 4)
661 return;
662
663 cpuid_count(4, 0, &eax, &ebx, &ecx, &edx);
664 if (eax & 0x1f)
665 c->x86_max_cores = (eax >> 26) + 1;
666 }
667
cpu_detect_cache_sizes(struct cpuinfo_x86 * c)668 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
669 {
670 unsigned int n, dummy, ebx, ecx, edx, l2size;
671
672 n = c->extended_cpuid_level;
673
674 if (n >= 0x80000005) {
675 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
676 c->x86_cache_size = (ecx>>24) + (edx>>24);
677 #ifdef CONFIG_X86_64
678 /* On K8 L1 TLB is inclusive, so don't count it */
679 c->x86_tlbsize = 0;
680 #endif
681 }
682
683 if (n < 0x80000006) /* Some chips just has a large L1. */
684 return;
685
686 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
687 l2size = ecx >> 16;
688
689 #ifdef CONFIG_X86_64
690 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
691 #else
692 /* do processor-specific cache resizing */
693 if (this_cpu->legacy_cache_size)
694 l2size = this_cpu->legacy_cache_size(c, l2size);
695
696 /* Allow user to override all this if necessary. */
697 if (cachesize_override != -1)
698 l2size = cachesize_override;
699
700 if (l2size == 0)
701 return; /* Again, no L2 cache is possible */
702 #endif
703
704 c->x86_cache_size = l2size;
705 }
706
707 u16 __read_mostly tlb_lli_4k[NR_INFO];
708 u16 __read_mostly tlb_lli_2m[NR_INFO];
709 u16 __read_mostly tlb_lli_4m[NR_INFO];
710 u16 __read_mostly tlb_lld_4k[NR_INFO];
711 u16 __read_mostly tlb_lld_2m[NR_INFO];
712 u16 __read_mostly tlb_lld_4m[NR_INFO];
713 u16 __read_mostly tlb_lld_1g[NR_INFO];
714
cpu_detect_tlb(struct cpuinfo_x86 * c)715 static void cpu_detect_tlb(struct cpuinfo_x86 *c)
716 {
717 if (this_cpu->c_detect_tlb)
718 this_cpu->c_detect_tlb(c);
719
720 pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
721 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
722 tlb_lli_4m[ENTRIES]);
723
724 pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
725 tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
726 tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
727 }
728
detect_ht_early(struct cpuinfo_x86 * c)729 int detect_ht_early(struct cpuinfo_x86 *c)
730 {
731 #ifdef CONFIG_SMP
732 u32 eax, ebx, ecx, edx;
733
734 if (!cpu_has(c, X86_FEATURE_HT))
735 return -1;
736
737 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
738 return -1;
739
740 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
741 return -1;
742
743 cpuid(1, &eax, &ebx, &ecx, &edx);
744
745 smp_num_siblings = (ebx & 0xff0000) >> 16;
746 if (smp_num_siblings == 1)
747 pr_info_once("CPU0: Hyper-Threading is disabled\n");
748 #endif
749 return 0;
750 }
751
detect_ht(struct cpuinfo_x86 * c)752 void detect_ht(struct cpuinfo_x86 *c)
753 {
754 #ifdef CONFIG_SMP
755 int index_msb, core_bits;
756
757 if (detect_ht_early(c) < 0)
758 return;
759
760 index_msb = get_count_order(smp_num_siblings);
761 c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
762
763 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
764
765 index_msb = get_count_order(smp_num_siblings);
766
767 core_bits = get_count_order(c->x86_max_cores);
768
769 c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
770 ((1 << core_bits) - 1);
771 #endif
772 }
773
get_cpu_vendor(struct cpuinfo_x86 * c)774 static void get_cpu_vendor(struct cpuinfo_x86 *c)
775 {
776 char *v = c->x86_vendor_id;
777 int i;
778
779 for (i = 0; i < X86_VENDOR_NUM; i++) {
780 if (!cpu_devs[i])
781 break;
782
783 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
784 (cpu_devs[i]->c_ident[1] &&
785 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
786
787 this_cpu = cpu_devs[i];
788 c->x86_vendor = this_cpu->c_x86_vendor;
789 return;
790 }
791 }
792
793 pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
794 "CPU: Your system may be unstable.\n", v);
795
796 c->x86_vendor = X86_VENDOR_UNKNOWN;
797 this_cpu = &default_cpu;
798 }
799
cpu_detect(struct cpuinfo_x86 * c)800 void cpu_detect(struct cpuinfo_x86 *c)
801 {
802 /* Get vendor name */
803 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
804 (unsigned int *)&c->x86_vendor_id[0],
805 (unsigned int *)&c->x86_vendor_id[8],
806 (unsigned int *)&c->x86_vendor_id[4]);
807
808 c->x86 = 4;
809 /* Intel-defined flags: level 0x00000001 */
810 if (c->cpuid_level >= 0x00000001) {
811 u32 junk, tfms, cap0, misc;
812
813 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
814 c->x86 = x86_family(tfms);
815 c->x86_model = x86_model(tfms);
816 c->x86_stepping = x86_stepping(tfms);
817
818 if (cap0 & (1<<19)) {
819 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
820 c->x86_cache_alignment = c->x86_clflush_size;
821 }
822 }
823 }
824
apply_forced_caps(struct cpuinfo_x86 * c)825 static void apply_forced_caps(struct cpuinfo_x86 *c)
826 {
827 int i;
828
829 for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
830 c->x86_capability[i] &= ~cpu_caps_cleared[i];
831 c->x86_capability[i] |= cpu_caps_set[i];
832 }
833 }
834
init_speculation_control(struct cpuinfo_x86 * c)835 static void init_speculation_control(struct cpuinfo_x86 *c)
836 {
837 /*
838 * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
839 * and they also have a different bit for STIBP support. Also,
840 * a hypervisor might have set the individual AMD bits even on
841 * Intel CPUs, for finer-grained selection of what's available.
842 */
843 if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
844 set_cpu_cap(c, X86_FEATURE_IBRS);
845 set_cpu_cap(c, X86_FEATURE_IBPB);
846 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
847 }
848
849 if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
850 set_cpu_cap(c, X86_FEATURE_STIBP);
851
852 if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
853 cpu_has(c, X86_FEATURE_VIRT_SSBD))
854 set_cpu_cap(c, X86_FEATURE_SSBD);
855
856 if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
857 set_cpu_cap(c, X86_FEATURE_IBRS);
858 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
859 }
860
861 if (cpu_has(c, X86_FEATURE_AMD_IBPB))
862 set_cpu_cap(c, X86_FEATURE_IBPB);
863
864 if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
865 set_cpu_cap(c, X86_FEATURE_STIBP);
866 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
867 }
868
869 if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
870 set_cpu_cap(c, X86_FEATURE_SSBD);
871 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
872 clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
873 }
874 }
875
init_cqm(struct cpuinfo_x86 * c)876 static void init_cqm(struct cpuinfo_x86 *c)
877 {
878 if (!cpu_has(c, X86_FEATURE_CQM_LLC)) {
879 c->x86_cache_max_rmid = -1;
880 c->x86_cache_occ_scale = -1;
881 return;
882 }
883
884 /* will be overridden if occupancy monitoring exists */
885 c->x86_cache_max_rmid = cpuid_ebx(0xf);
886
887 if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC) ||
888 cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL) ||
889 cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)) {
890 u32 eax, ebx, ecx, edx;
891
892 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
893 cpuid_count(0xf, 1, &eax, &ebx, &ecx, &edx);
894
895 c->x86_cache_max_rmid = ecx;
896 c->x86_cache_occ_scale = ebx;
897 }
898 }
899
get_cpu_cap(struct cpuinfo_x86 * c)900 void get_cpu_cap(struct cpuinfo_x86 *c)
901 {
902 u32 eax, ebx, ecx, edx;
903
904 /* Intel-defined flags: level 0x00000001 */
905 if (c->cpuid_level >= 0x00000001) {
906 cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
907
908 c->x86_capability[CPUID_1_ECX] = ecx;
909 c->x86_capability[CPUID_1_EDX] = edx;
910 }
911
912 /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
913 if (c->cpuid_level >= 0x00000006)
914 c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
915
916 /* Additional Intel-defined flags: level 0x00000007 */
917 if (c->cpuid_level >= 0x00000007) {
918 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
919 c->x86_capability[CPUID_7_0_EBX] = ebx;
920 c->x86_capability[CPUID_7_ECX] = ecx;
921 c->x86_capability[CPUID_7_EDX] = edx;
922
923 /* Check valid sub-leaf index before accessing it */
924 if (eax >= 1) {
925 cpuid_count(0x00000007, 1, &eax, &ebx, &ecx, &edx);
926 c->x86_capability[CPUID_7_1_EAX] = eax;
927 }
928 }
929
930 /* Extended state features: level 0x0000000d */
931 if (c->cpuid_level >= 0x0000000d) {
932 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
933
934 c->x86_capability[CPUID_D_1_EAX] = eax;
935 }
936
937 /* AMD-defined flags: level 0x80000001 */
938 eax = cpuid_eax(0x80000000);
939 c->extended_cpuid_level = eax;
940
941 if ((eax & 0xffff0000) == 0x80000000) {
942 if (eax >= 0x80000001) {
943 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
944
945 c->x86_capability[CPUID_8000_0001_ECX] = ecx;
946 c->x86_capability[CPUID_8000_0001_EDX] = edx;
947 }
948 }
949
950 if (c->extended_cpuid_level >= 0x80000007) {
951 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
952
953 c->x86_capability[CPUID_8000_0007_EBX] = ebx;
954 c->x86_power = edx;
955 }
956
957 if (c->extended_cpuid_level >= 0x80000008) {
958 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
959 c->x86_capability[CPUID_8000_0008_EBX] = ebx;
960 }
961
962 if (c->extended_cpuid_level >= 0x8000000a)
963 c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
964
965 if (c->extended_cpuid_level >= 0x8000001f)
966 c->x86_capability[CPUID_8000_001F_EAX] = cpuid_eax(0x8000001f);
967
968 if (c->extended_cpuid_level >= 0x80000021)
969 c->x86_capability[CPUID_8000_0021_EAX] = cpuid_eax(0x80000021);
970
971 init_scattered_cpuid_features(c);
972 init_speculation_control(c);
973 init_cqm(c);
974
975 /*
976 * Clear/Set all flags overridden by options, after probe.
977 * This needs to happen each time we re-probe, which may happen
978 * several times during CPU initialization.
979 */
980 apply_forced_caps(c);
981 }
982
get_cpu_address_sizes(struct cpuinfo_x86 * c)983 void get_cpu_address_sizes(struct cpuinfo_x86 *c)
984 {
985 u32 eax, ebx, ecx, edx;
986
987 if (c->extended_cpuid_level >= 0x80000008) {
988 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
989
990 c->x86_virt_bits = (eax >> 8) & 0xff;
991 c->x86_phys_bits = eax & 0xff;
992 }
993 #ifdef CONFIG_X86_32
994 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
995 c->x86_phys_bits = 36;
996 #endif
997 c->x86_cache_bits = c->x86_phys_bits;
998 }
999
identify_cpu_without_cpuid(struct cpuinfo_x86 * c)1000 static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
1001 {
1002 #ifdef CONFIG_X86_32
1003 int i;
1004
1005 /*
1006 * First of all, decide if this is a 486 or higher
1007 * It's a 486 if we can modify the AC flag
1008 */
1009 if (flag_is_changeable_p(X86_EFLAGS_AC))
1010 c->x86 = 4;
1011 else
1012 c->x86 = 3;
1013
1014 for (i = 0; i < X86_VENDOR_NUM; i++)
1015 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
1016 c->x86_vendor_id[0] = 0;
1017 cpu_devs[i]->c_identify(c);
1018 if (c->x86_vendor_id[0]) {
1019 get_cpu_vendor(c);
1020 break;
1021 }
1022 }
1023 #endif
1024 }
1025
1026 #define NO_SPECULATION BIT(0)
1027 #define NO_MELTDOWN BIT(1)
1028 #define NO_SSB BIT(2)
1029 #define NO_L1TF BIT(3)
1030 #define NO_MDS BIT(4)
1031 #define MSBDS_ONLY BIT(5)
1032 #define NO_SWAPGS BIT(6)
1033 #define NO_ITLB_MULTIHIT BIT(7)
1034 #define NO_SPECTRE_V2 BIT(8)
1035 #define NO_EIBRS_PBRSB BIT(9)
1036 #define NO_MMIO BIT(10)
1037
1038 #define VULNWL(_vendor, _family, _model, _whitelist) \
1039 { X86_VENDOR_##_vendor, _family, _model, X86_FEATURE_ANY, _whitelist }
1040
1041 #define VULNWL_INTEL(model, whitelist) \
1042 VULNWL(INTEL, 6, INTEL_FAM6_##model, whitelist)
1043
1044 #define VULNWL_AMD(family, whitelist) \
1045 VULNWL(AMD, family, X86_MODEL_ANY, whitelist)
1046
1047 #define VULNWL_HYGON(family, whitelist) \
1048 VULNWL(HYGON, family, X86_MODEL_ANY, whitelist)
1049
1050 static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
1051 VULNWL(ANY, 4, X86_MODEL_ANY, NO_SPECULATION),
1052 VULNWL(CENTAUR, 5, X86_MODEL_ANY, NO_SPECULATION),
1053 VULNWL(INTEL, 5, X86_MODEL_ANY, NO_SPECULATION),
1054 VULNWL(NSC, 5, X86_MODEL_ANY, NO_SPECULATION),
1055
1056 /* Intel Family 6 */
1057 VULNWL_INTEL(TIGERLAKE, NO_MMIO),
1058 VULNWL_INTEL(TIGERLAKE_L, NO_MMIO),
1059 VULNWL_INTEL(ALDERLAKE, NO_MMIO),
1060 VULNWL_INTEL(ALDERLAKE_L, NO_MMIO),
1061
1062 VULNWL_INTEL(ATOM_SALTWELL, NO_SPECULATION | NO_ITLB_MULTIHIT),
1063 VULNWL_INTEL(ATOM_SALTWELL_TABLET, NO_SPECULATION | NO_ITLB_MULTIHIT),
1064 VULNWL_INTEL(ATOM_SALTWELL_MID, NO_SPECULATION | NO_ITLB_MULTIHIT),
1065 VULNWL_INTEL(ATOM_BONNELL, NO_SPECULATION | NO_ITLB_MULTIHIT),
1066 VULNWL_INTEL(ATOM_BONNELL_MID, NO_SPECULATION | NO_ITLB_MULTIHIT),
1067
1068 VULNWL_INTEL(ATOM_SILVERMONT, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1069 VULNWL_INTEL(ATOM_SILVERMONT_D, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1070 VULNWL_INTEL(ATOM_SILVERMONT_MID, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1071 VULNWL_INTEL(ATOM_AIRMONT, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1072 VULNWL_INTEL(XEON_PHI_KNL, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1073 VULNWL_INTEL(XEON_PHI_KNM, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1074
1075 VULNWL_INTEL(CORE_YONAH, NO_SSB),
1076
1077 VULNWL_INTEL(ATOM_AIRMONT_MID, NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
1078 VULNWL_INTEL(ATOM_AIRMONT_NP, NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
1079
1080 VULNWL_INTEL(ATOM_GOLDMONT, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1081 VULNWL_INTEL(ATOM_GOLDMONT_D, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1082 VULNWL_INTEL(ATOM_GOLDMONT_PLUS, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB),
1083
1084 /*
1085 * Technically, swapgs isn't serializing on AMD (despite it previously
1086 * being documented as such in the APM). But according to AMD, %gs is
1087 * updated non-speculatively, and the issuing of %gs-relative memory
1088 * operands will be blocked until the %gs update completes, which is
1089 * good enough for our purposes.
1090 */
1091
1092 VULNWL_INTEL(ATOM_TREMONT, NO_EIBRS_PBRSB),
1093 VULNWL_INTEL(ATOM_TREMONT_L, NO_EIBRS_PBRSB),
1094 VULNWL_INTEL(ATOM_TREMONT_D, NO_ITLB_MULTIHIT | NO_EIBRS_PBRSB),
1095
1096 /* AMD Family 0xf - 0x12 */
1097 VULNWL_AMD(0x0f, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1098 VULNWL_AMD(0x10, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1099 VULNWL_AMD(0x11, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1100 VULNWL_AMD(0x12, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO),
1101
1102 /* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */
1103 VULNWL_AMD(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB),
1104 VULNWL_HYGON(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT | NO_MMIO | NO_EIBRS_PBRSB),
1105
1106 /* Zhaoxin Family 7 */
1107 VULNWL(CENTAUR, 7, X86_MODEL_ANY, NO_SPECTRE_V2 | NO_MMIO),
1108 VULNWL(ZHAOXIN, 7, X86_MODEL_ANY, NO_SPECTRE_V2 | NO_MMIO),
1109 {}
1110 };
1111
1112 #define VULNBL(vendor, family, model, blacklist) \
1113 X86_MATCH_VENDOR_FAM_MODEL(vendor, family, model, blacklist)
1114
1115 #define VULNBL_INTEL_STEPPINGS(model, steppings, issues) \
1116 X86_MATCH_VENDOR_FAM_MODEL_STEPPINGS_FEATURE(INTEL, 6, \
1117 INTEL_FAM6_##model, steppings, \
1118 X86_FEATURE_ANY, issues)
1119
1120 #define VULNBL_AMD(family, blacklist) \
1121 VULNBL(AMD, family, X86_MODEL_ANY, blacklist)
1122
1123 #define VULNBL_HYGON(family, blacklist) \
1124 VULNBL(HYGON, family, X86_MODEL_ANY, blacklist)
1125
1126 #define SRBDS BIT(0)
1127 /* CPU is affected by X86_BUG_MMIO_STALE_DATA */
1128 #define MMIO BIT(1)
1129 /* CPU is affected by Shared Buffers Data Sampling (SBDS), a variant of X86_BUG_MMIO_STALE_DATA */
1130 #define MMIO_SBDS BIT(2)
1131 /* CPU is affected by RETbleed, speculating where you would not expect it */
1132 #define RETBLEED BIT(3)
1133 /* CPU is affected by SMT (cross-thread) return predictions */
1134 #define SMT_RSB BIT(4)
1135 /* CPU is affected by SRSO */
1136 #define SRSO BIT(5)
1137 /* CPU is affected by GDS */
1138 #define GDS BIT(6)
1139
1140 static const struct x86_cpu_id cpu_vuln_blacklist[] __initconst = {
1141 VULNBL_INTEL_STEPPINGS(IVYBRIDGE, X86_STEPPING_ANY, SRBDS),
1142 VULNBL_INTEL_STEPPINGS(HASWELL, X86_STEPPING_ANY, SRBDS),
1143 VULNBL_INTEL_STEPPINGS(HASWELL_L, X86_STEPPING_ANY, SRBDS),
1144 VULNBL_INTEL_STEPPINGS(HASWELL_G, X86_STEPPING_ANY, SRBDS),
1145 VULNBL_INTEL_STEPPINGS(HASWELL_X, X86_STEPPING_ANY, MMIO),
1146 VULNBL_INTEL_STEPPINGS(BROADWELL_D, X86_STEPPING_ANY, MMIO),
1147 VULNBL_INTEL_STEPPINGS(BROADWELL_G, X86_STEPPING_ANY, SRBDS),
1148 VULNBL_INTEL_STEPPINGS(BROADWELL_X, X86_STEPPING_ANY, MMIO),
1149 VULNBL_INTEL_STEPPINGS(BROADWELL, X86_STEPPING_ANY, SRBDS),
1150 VULNBL_INTEL_STEPPINGS(SKYLAKE_X, X86_STEPPING_ANY, MMIO | RETBLEED | GDS),
1151 VULNBL_INTEL_STEPPINGS(SKYLAKE_L, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS),
1152 VULNBL_INTEL_STEPPINGS(SKYLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS),
1153 VULNBL_INTEL_STEPPINGS(KABYLAKE_L, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS),
1154 VULNBL_INTEL_STEPPINGS(KABYLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS | SRBDS),
1155 VULNBL_INTEL_STEPPINGS(CANNONLAKE_L, X86_STEPPING_ANY, RETBLEED),
1156 VULNBL_INTEL_STEPPINGS(ICELAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS),
1157 VULNBL_INTEL_STEPPINGS(ICELAKE_D, X86_STEPPING_ANY, MMIO | GDS),
1158 VULNBL_INTEL_STEPPINGS(ICELAKE_X, X86_STEPPING_ANY, MMIO | GDS),
1159 VULNBL_INTEL_STEPPINGS(COMETLAKE, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS),
1160 VULNBL_INTEL_STEPPINGS(COMETLAKE_L, X86_STEPPINGS(0x0, 0x0), MMIO | RETBLEED),
1161 VULNBL_INTEL_STEPPINGS(COMETLAKE_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED | GDS),
1162 VULNBL_INTEL_STEPPINGS(TIGERLAKE_L, X86_STEPPING_ANY, GDS),
1163 VULNBL_INTEL_STEPPINGS(TIGERLAKE, X86_STEPPING_ANY, GDS),
1164 VULNBL_INTEL_STEPPINGS(LAKEFIELD, X86_STEPPING_ANY, MMIO | MMIO_SBDS | RETBLEED),
1165 VULNBL_INTEL_STEPPINGS(ROCKETLAKE, X86_STEPPING_ANY, MMIO | RETBLEED | GDS),
1166 VULNBL_INTEL_STEPPINGS(ATOM_TREMONT, X86_STEPPING_ANY, MMIO | MMIO_SBDS),
1167 VULNBL_INTEL_STEPPINGS(ATOM_TREMONT_D, X86_STEPPING_ANY, MMIO),
1168 VULNBL_INTEL_STEPPINGS(ATOM_TREMONT_L, X86_STEPPING_ANY, MMIO | MMIO_SBDS),
1169
1170 VULNBL_AMD(0x15, RETBLEED),
1171 VULNBL_AMD(0x16, RETBLEED),
1172 VULNBL_AMD(0x17, RETBLEED),
1173 VULNBL_HYGON(0x18, RETBLEED),
1174 {}
1175 };
1176
cpu_matches(const struct x86_cpu_id * table,unsigned long which)1177 static bool __init cpu_matches(const struct x86_cpu_id *table, unsigned long which)
1178 {
1179 const struct x86_cpu_id *m = x86_match_cpu(table);
1180
1181 return m && !!(m->driver_data & which);
1182 }
1183
x86_read_arch_cap_msr(void)1184 u64 x86_read_arch_cap_msr(void)
1185 {
1186 u64 ia32_cap = 0;
1187
1188 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1189 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
1190
1191 return ia32_cap;
1192 }
1193
arch_cap_mmio_immune(u64 ia32_cap)1194 static bool arch_cap_mmio_immune(u64 ia32_cap)
1195 {
1196 return (ia32_cap & ARCH_CAP_FBSDP_NO &&
1197 ia32_cap & ARCH_CAP_PSDP_NO &&
1198 ia32_cap & ARCH_CAP_SBDR_SSDP_NO);
1199 }
1200
cpu_set_bug_bits(struct cpuinfo_x86 * c)1201 static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
1202 {
1203 u64 ia32_cap = x86_read_arch_cap_msr();
1204
1205 /* Set ITLB_MULTIHIT bug if cpu is not in the whitelist and not mitigated */
1206 if (!cpu_matches(cpu_vuln_whitelist, NO_ITLB_MULTIHIT) &&
1207 !(ia32_cap & ARCH_CAP_PSCHANGE_MC_NO))
1208 setup_force_cpu_bug(X86_BUG_ITLB_MULTIHIT);
1209
1210 if (cpu_matches(cpu_vuln_whitelist, NO_SPECULATION))
1211 return;
1212
1213 setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
1214
1215 if (!cpu_matches(cpu_vuln_whitelist, NO_SPECTRE_V2))
1216 setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
1217
1218 if (!cpu_matches(cpu_vuln_whitelist, NO_SSB) &&
1219 !(ia32_cap & ARCH_CAP_SSB_NO) &&
1220 !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
1221 setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
1222
1223 /*
1224 * AMD's AutoIBRS is equivalent to Intel's eIBRS - use the Intel feature
1225 * flag and protect from vendor-specific bugs via the whitelist.
1226 */
1227 if ((ia32_cap & ARCH_CAP_IBRS_ALL) || cpu_has(c, X86_FEATURE_AUTOIBRS)) {
1228 setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
1229 if (!cpu_matches(cpu_vuln_whitelist, NO_EIBRS_PBRSB) &&
1230 !(ia32_cap & ARCH_CAP_PBRSB_NO))
1231 setup_force_cpu_bug(X86_BUG_EIBRS_PBRSB);
1232 }
1233
1234 if (!cpu_matches(cpu_vuln_whitelist, NO_MDS) &&
1235 !(ia32_cap & ARCH_CAP_MDS_NO)) {
1236 setup_force_cpu_bug(X86_BUG_MDS);
1237 if (cpu_matches(cpu_vuln_whitelist, MSBDS_ONLY))
1238 setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
1239 }
1240
1241 if (!cpu_matches(cpu_vuln_whitelist, NO_SWAPGS))
1242 setup_force_cpu_bug(X86_BUG_SWAPGS);
1243
1244 /*
1245 * When the CPU is not mitigated for TAA (TAA_NO=0) set TAA bug when:
1246 * - TSX is supported or
1247 * - TSX_CTRL is present
1248 *
1249 * TSX_CTRL check is needed for cases when TSX could be disabled before
1250 * the kernel boot e.g. kexec.
1251 * TSX_CTRL check alone is not sufficient for cases when the microcode
1252 * update is not present or running as guest that don't get TSX_CTRL.
1253 */
1254 if (!(ia32_cap & ARCH_CAP_TAA_NO) &&
1255 (cpu_has(c, X86_FEATURE_RTM) ||
1256 (ia32_cap & ARCH_CAP_TSX_CTRL_MSR)))
1257 setup_force_cpu_bug(X86_BUG_TAA);
1258
1259 /*
1260 * SRBDS affects CPUs which support RDRAND or RDSEED and are listed
1261 * in the vulnerability blacklist.
1262 *
1263 * Some of the implications and mitigation of Shared Buffers Data
1264 * Sampling (SBDS) are similar to SRBDS. Give SBDS same treatment as
1265 * SRBDS.
1266 */
1267 if ((cpu_has(c, X86_FEATURE_RDRAND) ||
1268 cpu_has(c, X86_FEATURE_RDSEED)) &&
1269 cpu_matches(cpu_vuln_blacklist, SRBDS | MMIO_SBDS))
1270 setup_force_cpu_bug(X86_BUG_SRBDS);
1271
1272 /*
1273 * Processor MMIO Stale Data bug enumeration
1274 *
1275 * Affected CPU list is generally enough to enumerate the vulnerability,
1276 * but for virtualization case check for ARCH_CAP MSR bits also, VMM may
1277 * not want the guest to enumerate the bug.
1278 *
1279 * Set X86_BUG_MMIO_UNKNOWN for CPUs that are neither in the blacklist,
1280 * nor in the whitelist and also don't enumerate MSR ARCH_CAP MMIO bits.
1281 */
1282 if (!arch_cap_mmio_immune(ia32_cap)) {
1283 if (cpu_matches(cpu_vuln_blacklist, MMIO))
1284 setup_force_cpu_bug(X86_BUG_MMIO_STALE_DATA);
1285 else if (!cpu_matches(cpu_vuln_whitelist, NO_MMIO))
1286 setup_force_cpu_bug(X86_BUG_MMIO_UNKNOWN);
1287 }
1288
1289 if (!cpu_has(c, X86_FEATURE_BTC_NO)) {
1290 if (cpu_matches(cpu_vuln_blacklist, RETBLEED) || (ia32_cap & ARCH_CAP_RSBA))
1291 setup_force_cpu_bug(X86_BUG_RETBLEED);
1292 }
1293
1294 /*
1295 * Check if CPU is vulnerable to GDS. If running in a virtual machine on
1296 * an affected processor, the VMM may have disabled the use of GATHER by
1297 * disabling AVX2. The only way to do this in HW is to clear XCR0[2],
1298 * which means that AVX will be disabled.
1299 */
1300 if (cpu_matches(cpu_vuln_blacklist, GDS) && !(ia32_cap & ARCH_CAP_GDS_NO) &&
1301 boot_cpu_has(X86_FEATURE_AVX))
1302 setup_force_cpu_bug(X86_BUG_GDS);
1303
1304 if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
1305 return;
1306
1307 /* Rogue Data Cache Load? No! */
1308 if (ia32_cap & ARCH_CAP_RDCL_NO)
1309 return;
1310
1311 setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
1312
1313 if (cpu_matches(cpu_vuln_whitelist, NO_L1TF))
1314 return;
1315
1316 setup_force_cpu_bug(X86_BUG_L1TF);
1317 }
1318
1319 /*
1320 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1321 * unfortunately, that's not true in practice because of early VIA
1322 * chips and (more importantly) broken virtualizers that are not easy
1323 * to detect. In the latter case it doesn't even *fail* reliably, so
1324 * probing for it doesn't even work. Disable it completely on 32-bit
1325 * unless we can find a reliable way to detect all the broken cases.
1326 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
1327 */
detect_nopl(void)1328 static void detect_nopl(void)
1329 {
1330 #ifdef CONFIG_X86_32
1331 setup_clear_cpu_cap(X86_FEATURE_NOPL);
1332 #else
1333 setup_force_cpu_cap(X86_FEATURE_NOPL);
1334 #endif
1335 }
1336
1337 /*
1338 * Do minimum CPU detection early.
1339 * Fields really needed: vendor, cpuid_level, family, model, mask,
1340 * cache alignment.
1341 * The others are not touched to avoid unwanted side effects.
1342 *
1343 * WARNING: this function is only called on the boot CPU. Don't add code
1344 * here that is supposed to run on all CPUs.
1345 */
early_identify_cpu(struct cpuinfo_x86 * c)1346 static void __init early_identify_cpu(struct cpuinfo_x86 *c)
1347 {
1348 #ifdef CONFIG_X86_64
1349 c->x86_clflush_size = 64;
1350 c->x86_phys_bits = 36;
1351 c->x86_virt_bits = 48;
1352 #else
1353 c->x86_clflush_size = 32;
1354 c->x86_phys_bits = 32;
1355 c->x86_virt_bits = 32;
1356 #endif
1357 c->x86_cache_alignment = c->x86_clflush_size;
1358
1359 memset(&c->x86_capability, 0, sizeof(c->x86_capability));
1360 c->extended_cpuid_level = 0;
1361
1362 if (!have_cpuid_p())
1363 identify_cpu_without_cpuid(c);
1364
1365 /* cyrix could have cpuid enabled via c_identify()*/
1366 if (have_cpuid_p()) {
1367 cpu_detect(c);
1368 get_cpu_vendor(c);
1369 get_cpu_cap(c);
1370 get_cpu_address_sizes(c);
1371 setup_force_cpu_cap(X86_FEATURE_CPUID);
1372
1373 if (this_cpu->c_early_init)
1374 this_cpu->c_early_init(c);
1375
1376 c->cpu_index = 0;
1377 filter_cpuid_features(c, false);
1378
1379 if (this_cpu->c_bsp_init)
1380 this_cpu->c_bsp_init(c);
1381 } else {
1382 setup_clear_cpu_cap(X86_FEATURE_CPUID);
1383 }
1384
1385 setup_force_cpu_cap(X86_FEATURE_ALWAYS);
1386
1387 cpu_set_bug_bits(c);
1388
1389 #ifdef CONFIG_X86_32
1390 /*
1391 * Regardless of whether PCID is enumerated, the SDM says
1392 * that it can't be enabled in 32-bit mode.
1393 */
1394 setup_clear_cpu_cap(X86_FEATURE_PCID);
1395 #endif
1396
1397 /*
1398 * Later in the boot process pgtable_l5_enabled() relies on
1399 * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not
1400 * enabled by this point we need to clear the feature bit to avoid
1401 * false-positives at the later stage.
1402 *
1403 * pgtable_l5_enabled() can be false here for several reasons:
1404 * - 5-level paging is disabled compile-time;
1405 * - it's 32-bit kernel;
1406 * - machine doesn't support 5-level paging;
1407 * - user specified 'no5lvl' in kernel command line.
1408 */
1409 if (!pgtable_l5_enabled())
1410 setup_clear_cpu_cap(X86_FEATURE_LA57);
1411
1412 detect_nopl();
1413 }
1414
early_cpu_init(void)1415 void __init early_cpu_init(void)
1416 {
1417 const struct cpu_dev *const *cdev;
1418 int count = 0;
1419
1420 #ifdef CONFIG_PROCESSOR_SELECT
1421 pr_info("KERNEL supported cpus:\n");
1422 #endif
1423
1424 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
1425 const struct cpu_dev *cpudev = *cdev;
1426
1427 if (count >= X86_VENDOR_NUM)
1428 break;
1429 cpu_devs[count] = cpudev;
1430 count++;
1431
1432 #ifdef CONFIG_PROCESSOR_SELECT
1433 {
1434 unsigned int j;
1435
1436 for (j = 0; j < 2; j++) {
1437 if (!cpudev->c_ident[j])
1438 continue;
1439 pr_info(" %s %s\n", cpudev->c_vendor,
1440 cpudev->c_ident[j]);
1441 }
1442 }
1443 #endif
1444 }
1445 early_identify_cpu(&boot_cpu_data);
1446 }
1447
detect_null_seg_behavior(void)1448 static bool detect_null_seg_behavior(void)
1449 {
1450 /*
1451 * Empirically, writing zero to a segment selector on AMD does
1452 * not clear the base, whereas writing zero to a segment
1453 * selector on Intel does clear the base. Intel's behavior
1454 * allows slightly faster context switches in the common case
1455 * where GS is unused by the prev and next threads.
1456 *
1457 * Since neither vendor documents this anywhere that I can see,
1458 * detect it directly instead of hardcoding the choice by
1459 * vendor.
1460 *
1461 * I've designated AMD's behavior as the "bug" because it's
1462 * counterintuitive and less friendly.
1463 */
1464
1465 unsigned long old_base, tmp;
1466 rdmsrl(MSR_FS_BASE, old_base);
1467 wrmsrl(MSR_FS_BASE, 1);
1468 loadsegment(fs, 0);
1469 rdmsrl(MSR_FS_BASE, tmp);
1470 wrmsrl(MSR_FS_BASE, old_base);
1471 return tmp == 0;
1472 }
1473
check_null_seg_clears_base(struct cpuinfo_x86 * c)1474 void check_null_seg_clears_base(struct cpuinfo_x86 *c)
1475 {
1476 /* BUG_NULL_SEG is only relevant with 64bit userspace */
1477 if (!IS_ENABLED(CONFIG_X86_64))
1478 return;
1479
1480 /* Zen3 CPUs advertise Null Selector Clears Base in CPUID. */
1481 if (c->extended_cpuid_level >= 0x80000021 &&
1482 cpuid_eax(0x80000021) & BIT(6))
1483 return;
1484
1485 /*
1486 * CPUID bit above wasn't set. If this kernel is still running
1487 * as a HV guest, then the HV has decided not to advertize
1488 * that CPUID bit for whatever reason. For example, one
1489 * member of the migration pool might be vulnerable. Which
1490 * means, the bug is present: set the BUG flag and return.
1491 */
1492 if (cpu_has(c, X86_FEATURE_HYPERVISOR)) {
1493 set_cpu_bug(c, X86_BUG_NULL_SEG);
1494 return;
1495 }
1496
1497 /*
1498 * Zen2 CPUs also have this behaviour, but no CPUID bit.
1499 * 0x18 is the respective family for Hygon.
1500 */
1501 if ((c->x86 == 0x17 || c->x86 == 0x18) &&
1502 detect_null_seg_behavior())
1503 return;
1504
1505 /* All the remaining ones are affected */
1506 set_cpu_bug(c, X86_BUG_NULL_SEG);
1507 }
1508
generic_identify(struct cpuinfo_x86 * c)1509 static void generic_identify(struct cpuinfo_x86 *c)
1510 {
1511 c->extended_cpuid_level = 0;
1512
1513 if (!have_cpuid_p())
1514 identify_cpu_without_cpuid(c);
1515
1516 /* cyrix could have cpuid enabled via c_identify()*/
1517 if (!have_cpuid_p())
1518 return;
1519
1520 cpu_detect(c);
1521
1522 get_cpu_vendor(c);
1523
1524 get_cpu_cap(c);
1525
1526 get_cpu_address_sizes(c);
1527
1528 if (c->cpuid_level >= 0x00000001) {
1529 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
1530 #ifdef CONFIG_X86_32
1531 # ifdef CONFIG_SMP
1532 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1533 # else
1534 c->apicid = c->initial_apicid;
1535 # endif
1536 #endif
1537 c->phys_proc_id = c->initial_apicid;
1538 }
1539
1540 get_model_name(c); /* Default name */
1541
1542 /*
1543 * ESPFIX is a strange bug. All real CPUs have it. Paravirt
1544 * systems that run Linux at CPL > 0 may or may not have the
1545 * issue, but, even if they have the issue, there's absolutely
1546 * nothing we can do about it because we can't use the real IRET
1547 * instruction.
1548 *
1549 * NB: For the time being, only 32-bit kernels support
1550 * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
1551 * whether to apply espfix using paravirt hooks. If any
1552 * non-paravirt system ever shows up that does *not* have the
1553 * ESPFIX issue, we can change this.
1554 */
1555 #ifdef CONFIG_X86_32
1556 # ifdef CONFIG_PARAVIRT_XXL
1557 do {
1558 extern void native_iret(void);
1559 if (pv_ops.cpu.iret == native_iret)
1560 set_cpu_bug(c, X86_BUG_ESPFIX);
1561 } while (0);
1562 # else
1563 set_cpu_bug(c, X86_BUG_ESPFIX);
1564 # endif
1565 #endif
1566 }
1567
x86_init_cache_qos(struct cpuinfo_x86 * c)1568 static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1569 {
1570 /*
1571 * The heavy lifting of max_rmid and cache_occ_scale are handled
1572 * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
1573 * in case CQM bits really aren't there in this CPU.
1574 */
1575 if (c != &boot_cpu_data) {
1576 boot_cpu_data.x86_cache_max_rmid =
1577 min(boot_cpu_data.x86_cache_max_rmid,
1578 c->x86_cache_max_rmid);
1579 }
1580 }
1581
1582 /*
1583 * Validate that ACPI/mptables have the same information about the
1584 * effective APIC id and update the package map.
1585 */
validate_apic_and_package_id(struct cpuinfo_x86 * c)1586 static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
1587 {
1588 #ifdef CONFIG_SMP
1589 unsigned int apicid, cpu = smp_processor_id();
1590
1591 apicid = apic->cpu_present_to_apicid(cpu);
1592
1593 if (apicid != c->apicid) {
1594 pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
1595 cpu, apicid, c->initial_apicid);
1596 }
1597 BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
1598 BUG_ON(topology_update_die_map(c->cpu_die_id, cpu));
1599 #else
1600 c->logical_proc_id = 0;
1601 #endif
1602 }
1603
1604 /*
1605 * This does the hard work of actually picking apart the CPU stuff...
1606 */
identify_cpu(struct cpuinfo_x86 * c)1607 static void identify_cpu(struct cpuinfo_x86 *c)
1608 {
1609 int i;
1610
1611 c->loops_per_jiffy = loops_per_jiffy;
1612 c->x86_cache_size = 0;
1613 c->x86_vendor = X86_VENDOR_UNKNOWN;
1614 c->x86_model = c->x86_stepping = 0; /* So far unknown... */
1615 c->x86_vendor_id[0] = '\0'; /* Unset */
1616 c->x86_model_id[0] = '\0'; /* Unset */
1617 c->x86_max_cores = 1;
1618 c->x86_coreid_bits = 0;
1619 c->cu_id = 0xff;
1620 #ifdef CONFIG_X86_64
1621 c->x86_clflush_size = 64;
1622 c->x86_phys_bits = 36;
1623 c->x86_virt_bits = 48;
1624 #else
1625 c->cpuid_level = -1; /* CPUID not detected */
1626 c->x86_clflush_size = 32;
1627 c->x86_phys_bits = 32;
1628 c->x86_virt_bits = 32;
1629 #endif
1630 c->x86_cache_alignment = c->x86_clflush_size;
1631 memset(&c->x86_capability, 0, sizeof(c->x86_capability));
1632
1633 generic_identify(c);
1634
1635 if (this_cpu->c_identify)
1636 this_cpu->c_identify(c);
1637
1638 /* Clear/Set all flags overridden by options, after probe */
1639 apply_forced_caps(c);
1640
1641 #ifdef CONFIG_X86_64
1642 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1643 #endif
1644
1645 /*
1646 * Vendor-specific initialization. In this section we
1647 * canonicalize the feature flags, meaning if there are
1648 * features a certain CPU supports which CPUID doesn't
1649 * tell us, CPUID claiming incorrect flags, or other bugs,
1650 * we handle them here.
1651 *
1652 * At the end of this section, c->x86_capability better
1653 * indicate the features this CPU genuinely supports!
1654 */
1655 if (this_cpu->c_init)
1656 this_cpu->c_init(c);
1657
1658 /* Disable the PN if appropriate */
1659 squash_the_stupid_serial_number(c);
1660
1661 /* Set up SMEP/SMAP/UMIP */
1662 setup_smep(c);
1663 setup_smap(c);
1664 setup_umip(c);
1665
1666 /*
1667 * The vendor-specific functions might have changed features.
1668 * Now we do "generic changes."
1669 */
1670
1671 /* Filter out anything that depends on CPUID levels we don't have */
1672 filter_cpuid_features(c, true);
1673
1674 /* If the model name is still unset, do table lookup. */
1675 if (!c->x86_model_id[0]) {
1676 const char *p;
1677 p = table_lookup_model(c);
1678 if (p)
1679 strcpy(c->x86_model_id, p);
1680 else
1681 /* Last resort... */
1682 sprintf(c->x86_model_id, "%02x/%02x",
1683 c->x86, c->x86_model);
1684 }
1685
1686 #ifdef CONFIG_X86_64
1687 detect_ht(c);
1688 #endif
1689
1690 x86_init_rdrand(c);
1691 x86_init_cache_qos(c);
1692 setup_pku(c);
1693
1694 /*
1695 * Clear/Set all flags overridden by options, need do it
1696 * before following smp all cpus cap AND.
1697 */
1698 apply_forced_caps(c);
1699
1700 /*
1701 * On SMP, boot_cpu_data holds the common feature set between
1702 * all CPUs; so make sure that we indicate which features are
1703 * common between the CPUs. The first time this routine gets
1704 * executed, c == &boot_cpu_data.
1705 */
1706 if (c != &boot_cpu_data) {
1707 /* AND the already accumulated flags with these */
1708 for (i = 0; i < NCAPINTS; i++)
1709 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
1710
1711 /* OR, i.e. replicate the bug flags */
1712 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
1713 c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
1714 }
1715
1716 /* Init Machine Check Exception if available. */
1717 mcheck_cpu_init(c);
1718
1719 select_idle_routine(c);
1720
1721 #ifdef CONFIG_NUMA
1722 numa_add_cpu(smp_processor_id());
1723 #endif
1724 }
1725
1726 /*
1727 * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
1728 * on 32-bit kernels:
1729 */
1730 #ifdef CONFIG_X86_32
enable_sep_cpu(void)1731 void enable_sep_cpu(void)
1732 {
1733 struct tss_struct *tss;
1734 int cpu;
1735
1736 if (!boot_cpu_has(X86_FEATURE_SEP))
1737 return;
1738
1739 cpu = get_cpu();
1740 tss = &per_cpu(cpu_tss_rw, cpu);
1741
1742 /*
1743 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1744 * see the big comment in struct x86_hw_tss's definition.
1745 */
1746
1747 tss->x86_tss.ss1 = __KERNEL_CS;
1748 wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
1749 wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0);
1750 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
1751
1752 put_cpu();
1753 }
1754 #endif
1755
identify_boot_cpu(void)1756 void __init identify_boot_cpu(void)
1757 {
1758 identify_cpu(&boot_cpu_data);
1759 #ifdef CONFIG_X86_32
1760 sysenter_setup();
1761 enable_sep_cpu();
1762 #endif
1763 cpu_detect_tlb(&boot_cpu_data);
1764 setup_cr_pinning();
1765
1766 tsx_init();
1767 }
1768
identify_secondary_cpu(struct cpuinfo_x86 * c)1769 void identify_secondary_cpu(struct cpuinfo_x86 *c)
1770 {
1771 BUG_ON(c == &boot_cpu_data);
1772 identify_cpu(c);
1773 #ifdef CONFIG_X86_32
1774 enable_sep_cpu();
1775 #endif
1776 mtrr_ap_init();
1777 validate_apic_and_package_id(c);
1778 x86_spec_ctrl_setup_ap();
1779 update_srbds_msr();
1780 if (boot_cpu_has_bug(X86_BUG_GDS))
1781 update_gds_msr();
1782 }
1783
setup_noclflush(char * arg)1784 static __init int setup_noclflush(char *arg)
1785 {
1786 setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1787 setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1788 return 1;
1789 }
1790 __setup("noclflush", setup_noclflush);
1791
print_cpu_info(struct cpuinfo_x86 * c)1792 void print_cpu_info(struct cpuinfo_x86 *c)
1793 {
1794 const char *vendor = NULL;
1795
1796 if (c->x86_vendor < X86_VENDOR_NUM) {
1797 vendor = this_cpu->c_vendor;
1798 } else {
1799 if (c->cpuid_level >= 0)
1800 vendor = c->x86_vendor_id;
1801 }
1802
1803 if (vendor && !strstr(c->x86_model_id, vendor))
1804 pr_cont("%s ", vendor);
1805
1806 if (c->x86_model_id[0])
1807 pr_cont("%s", c->x86_model_id);
1808 else
1809 pr_cont("%d86", c->x86);
1810
1811 pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
1812
1813 if (c->x86_stepping || c->cpuid_level >= 0)
1814 pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
1815 else
1816 pr_cont(")\n");
1817 }
1818
1819 /*
1820 * clearcpuid= was already parsed in fpu__init_parse_early_param.
1821 * But we need to keep a dummy __setup around otherwise it would
1822 * show up as an environment variable for init.
1823 */
setup_clearcpuid(char * arg)1824 static __init int setup_clearcpuid(char *arg)
1825 {
1826 return 1;
1827 }
1828 __setup("clearcpuid=", setup_clearcpuid);
1829
1830 #ifdef CONFIG_X86_64
1831 DEFINE_PER_CPU_FIRST(struct fixed_percpu_data,
1832 fixed_percpu_data) __aligned(PAGE_SIZE) __visible;
1833 EXPORT_PER_CPU_SYMBOL_GPL(fixed_percpu_data);
1834
1835 /*
1836 * The following percpu variables are hot. Align current_task to
1837 * cacheline size such that they fall in the same cacheline.
1838 */
1839 DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1840 &init_task;
1841 EXPORT_PER_CPU_SYMBOL(current_task);
1842
1843 DEFINE_PER_CPU(struct irq_stack *, hardirq_stack_ptr);
1844 DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
1845
1846 DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1847 EXPORT_PER_CPU_SYMBOL(__preempt_count);
1848
1849 /* May not be marked __init: used by software suspend */
syscall_init(void)1850 void syscall_init(void)
1851 {
1852 wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
1853 wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
1854
1855 #ifdef CONFIG_IA32_EMULATION
1856 wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
1857 /*
1858 * This only works on Intel CPUs.
1859 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1860 * This does not cause SYSENTER to jump to the wrong location, because
1861 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1862 */
1863 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1864 wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
1865 (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1));
1866 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
1867 #else
1868 wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
1869 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1870 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1871 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1872 #endif
1873
1874 /* Flags to clear on syscall */
1875 wrmsrl(MSR_SYSCALL_MASK,
1876 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
1877 X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1878 }
1879
1880 DEFINE_PER_CPU(int, debug_stack_usage);
1881 DEFINE_PER_CPU(u32, debug_idt_ctr);
1882
debug_stack_set_zero(void)1883 void debug_stack_set_zero(void)
1884 {
1885 this_cpu_inc(debug_idt_ctr);
1886 load_current_idt();
1887 }
1888 NOKPROBE_SYMBOL(debug_stack_set_zero);
1889
debug_stack_reset(void)1890 void debug_stack_reset(void)
1891 {
1892 if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
1893 return;
1894 if (this_cpu_dec_return(debug_idt_ctr) == 0)
1895 load_current_idt();
1896 }
1897 NOKPROBE_SYMBOL(debug_stack_reset);
1898
1899 #else /* CONFIG_X86_64 */
1900
1901 DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1902 EXPORT_PER_CPU_SYMBOL(current_task);
1903 DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1904 EXPORT_PER_CPU_SYMBOL(__preempt_count);
1905
1906 /*
1907 * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1908 * the top of the kernel stack. Use an extra percpu variable to track the
1909 * top of the kernel stack directly.
1910 */
1911 DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1912 (unsigned long)&init_thread_union + THREAD_SIZE;
1913 EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1914
1915 #ifdef CONFIG_STACKPROTECTOR
1916 DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
1917 #endif
1918
1919 #endif /* CONFIG_X86_64 */
1920
1921 /*
1922 * Clear all 6 debug registers:
1923 */
clear_all_debug_regs(void)1924 static void clear_all_debug_regs(void)
1925 {
1926 int i;
1927
1928 for (i = 0; i < 8; i++) {
1929 /* Ignore db4, db5 */
1930 if ((i == 4) || (i == 5))
1931 continue;
1932
1933 set_debugreg(0, i);
1934 }
1935 }
1936
1937 #ifdef CONFIG_KGDB
1938 /*
1939 * Restore debug regs if using kgdbwait and you have a kernel debugger
1940 * connection established.
1941 */
dbg_restore_debug_regs(void)1942 static void dbg_restore_debug_regs(void)
1943 {
1944 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1945 arch_kgdb_ops.correct_hw_break();
1946 }
1947 #else /* ! CONFIG_KGDB */
1948 #define dbg_restore_debug_regs()
1949 #endif /* ! CONFIG_KGDB */
1950
wait_for_master_cpu(int cpu)1951 static void wait_for_master_cpu(int cpu)
1952 {
1953 #ifdef CONFIG_SMP
1954 /*
1955 * wait for ACK from master CPU before continuing
1956 * with AP initialization
1957 */
1958 WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1959 while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1960 cpu_relax();
1961 #endif
1962 }
1963
1964 #ifdef CONFIG_X86_64
setup_getcpu(int cpu)1965 static void setup_getcpu(int cpu)
1966 {
1967 unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
1968 struct desc_struct d = { };
1969
1970 if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID))
1971 write_rdtscp_aux(cpudata);
1972
1973 /* Store CPU and node number in limit. */
1974 d.limit0 = cpudata;
1975 d.limit1 = cpudata >> 16;
1976
1977 d.type = 5; /* RO data, expand down, accessed */
1978 d.dpl = 3; /* Visible to user code */
1979 d.s = 1; /* Not a system segment */
1980 d.p = 1; /* Present */
1981 d.d = 1; /* 32-bit */
1982
1983 write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S);
1984 }
1985 #endif
1986
1987 /*
1988 * cpu_init() initializes state that is per-CPU. Some data is already
1989 * initialized (naturally) in the bootstrap process, such as the GDT
1990 * and IDT. We reload them nevertheless, this function acts as a
1991 * 'CPU state barrier', nothing should get across.
1992 */
1993 #ifdef CONFIG_X86_64
1994
cpu_init(void)1995 void cpu_init(void)
1996 {
1997 int cpu = raw_smp_processor_id();
1998 struct task_struct *me;
1999 struct tss_struct *t;
2000 int i;
2001
2002 wait_for_master_cpu(cpu);
2003
2004 if (cpu)
2005 load_ucode_ap();
2006
2007 t = &per_cpu(cpu_tss_rw, cpu);
2008
2009 #ifdef CONFIG_NUMA
2010 if (this_cpu_read(numa_node) == 0 &&
2011 early_cpu_to_node(cpu) != NUMA_NO_NODE)
2012 set_numa_node(early_cpu_to_node(cpu));
2013 #endif
2014 setup_getcpu(cpu);
2015
2016 me = current;
2017
2018 pr_debug("Initializing CPU#%d\n", cpu);
2019
2020 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
2021
2022 /*
2023 * Initialize the per-CPU GDT with the boot GDT,
2024 * and set up the GDT descriptor:
2025 */
2026
2027 switch_to_new_gdt(cpu);
2028 loadsegment(fs, 0);
2029
2030 load_current_idt();
2031
2032 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
2033 syscall_init();
2034
2035 wrmsrl(MSR_FS_BASE, 0);
2036 wrmsrl(MSR_KERNEL_GS_BASE, 0);
2037 barrier();
2038
2039 x86_configure_nx();
2040 x2apic_setup();
2041
2042 /*
2043 * set up and load the per-CPU TSS
2044 */
2045 if (!t->x86_tss.ist[0]) {
2046 t->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
2047 t->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
2048 t->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
2049 t->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
2050 }
2051
2052 t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
2053
2054 /*
2055 * <= is required because the CPU will access up to
2056 * 8 bits beyond the end of the IO permission bitmap.
2057 */
2058 for (i = 0; i <= IO_BITMAP_LONGS; i++)
2059 t->io_bitmap[i] = ~0UL;
2060
2061 mmgrab(&init_mm);
2062 me->active_mm = &init_mm;
2063 BUG_ON(me->mm);
2064 initialize_tlbstate_and_flush();
2065 enter_lazy_tlb(&init_mm, me);
2066
2067 /*
2068 * Initialize the TSS. sp0 points to the entry trampoline stack
2069 * regardless of what task is running.
2070 */
2071 set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
2072 load_TR_desc();
2073 load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
2074
2075 load_mm_ldt(&init_mm);
2076
2077 clear_all_debug_regs();
2078 dbg_restore_debug_regs();
2079
2080 if (is_uv_system())
2081 uv_cpu_init();
2082
2083 load_fixmap_gdt(cpu);
2084 }
2085
2086 #else
2087
cpu_init(void)2088 void cpu_init(void)
2089 {
2090 int cpu = smp_processor_id();
2091 struct task_struct *curr = current;
2092 struct tss_struct *t = &per_cpu(cpu_tss_rw, cpu);
2093
2094 wait_for_master_cpu(cpu);
2095
2096 show_ucode_info_early();
2097
2098 pr_info("Initializing CPU#%d\n", cpu);
2099
2100 if (cpu_feature_enabled(X86_FEATURE_VME) ||
2101 boot_cpu_has(X86_FEATURE_TSC) ||
2102 boot_cpu_has(X86_FEATURE_DE))
2103 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
2104
2105 load_current_idt();
2106 switch_to_new_gdt(cpu);
2107
2108 /*
2109 * Set up and load the per-CPU TSS and LDT
2110 */
2111 mmgrab(&init_mm);
2112 curr->active_mm = &init_mm;
2113 BUG_ON(curr->mm);
2114 initialize_tlbstate_and_flush();
2115 enter_lazy_tlb(&init_mm, curr);
2116
2117 /*
2118 * Initialize the TSS. sp0 points to the entry trampoline stack
2119 * regardless of what task is running.
2120 */
2121 set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
2122 load_TR_desc();
2123 load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
2124
2125 load_mm_ldt(&init_mm);
2126
2127 t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
2128
2129 #ifdef CONFIG_DOUBLEFAULT
2130 /* Set up doublefault TSS pointer in the GDT */
2131 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
2132 #endif
2133
2134 clear_all_debug_regs();
2135 dbg_restore_debug_regs();
2136
2137 load_fixmap_gdt(cpu);
2138 }
2139 #endif
2140
2141 /*
2142 * The microcode loader calls this upon late microcode load to recheck features,
2143 * only when microcode has been updated. Caller holds microcode_mutex and CPU
2144 * hotplug lock.
2145 */
microcode_check(void)2146 void microcode_check(void)
2147 {
2148 struct cpuinfo_x86 info;
2149
2150 perf_check_microcode();
2151
2152 amd_check_microcode();
2153
2154 /* Reload CPUID max function as it might've changed. */
2155 info.cpuid_level = cpuid_eax(0);
2156
2157 /*
2158 * Copy all capability leafs to pick up the synthetic ones so that
2159 * memcmp() below doesn't fail on that. The ones coming from CPUID will
2160 * get overwritten in get_cpu_cap().
2161 */
2162 memcpy(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability));
2163
2164 get_cpu_cap(&info);
2165
2166 if (!memcmp(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability)))
2167 return;
2168
2169 pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
2170 pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
2171 }
2172
2173 /*
2174 * Invoked from core CPU hotplug code after hotplug operations
2175 */
arch_smt_update(void)2176 void arch_smt_update(void)
2177 {
2178 /* Handle the speculative execution misfeatures */
2179 cpu_bugs_smt_update();
2180 /* Check whether IPI broadcasting can be enabled */
2181 apic_smt_update();
2182 }
2183
arch_cpu_finalize_init(void)2184 void __init arch_cpu_finalize_init(void)
2185 {
2186 identify_boot_cpu();
2187
2188 /*
2189 * identify_boot_cpu() initialized SMT support information, let the
2190 * core code know.
2191 */
2192 cpu_smt_check_topology();
2193
2194 if (!IS_ENABLED(CONFIG_SMP)) {
2195 pr_info("CPU: ");
2196 print_cpu_info(&boot_cpu_data);
2197 }
2198
2199 cpu_select_mitigations();
2200
2201 arch_smt_update();
2202
2203 if (IS_ENABLED(CONFIG_X86_32)) {
2204 /*
2205 * Check whether this is a real i386 which is not longer
2206 * supported and fixup the utsname.
2207 */
2208 if (boot_cpu_data.x86 < 4)
2209 panic("Kernel requires i486+ for 'invlpg' and other features");
2210
2211 init_utsname()->machine[1] =
2212 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
2213 }
2214
2215 /*
2216 * Must be before alternatives because it might set or clear
2217 * feature bits.
2218 */
2219 fpu__init_system();
2220 fpu__init_cpu();
2221
2222 alternative_instructions();
2223
2224 if (IS_ENABLED(CONFIG_X86_64)) {
2225 /*
2226 * Make sure the first 2MB area is not mapped by huge pages
2227 * There are typically fixed size MTRRs in there and overlapping
2228 * MTRRs into large pages causes slow downs.
2229 *
2230 * Right now we don't do that with gbpages because there seems
2231 * very little benefit for that case.
2232 */
2233 if (!direct_gbpages)
2234 set_memory_4k((unsigned long)__va(0), 1);
2235 } else {
2236 fpu__init_check_bugs();
2237 }
2238
2239 /*
2240 * This needs to be called before any devices perform DMA
2241 * operations that might use the SWIOTLB bounce buffers. It will
2242 * mark the bounce buffers as decrypted so that their usage will
2243 * not cause "plain-text" data to be decrypted when accessed. It
2244 * must be called after late_time_init() so that Hyper-V x86/x64
2245 * hypercalls work when the SWIOTLB bounce buffers are decrypted.
2246 */
2247 mem_encrypt_init();
2248 }
2249