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