]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/x86/kernel/cpu/common.c
x86/apic/uv: Make x2apic_extra_bits static
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / kernel / cpu / common.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
2458e53f
KS
2/* cpu_feature_enabled() cannot be used this early */
3#define USE_EARLY_PGTABLE_L5
4
57c8a661 5#include <linux/memblock.h>
9766cdbc 6#include <linux/linkage.h>
f0fc4aff 7#include <linux/bitops.h>
9766cdbc 8#include <linux/kernel.h>
186f4360 9#include <linux/export.h>
9766cdbc
JSR
10#include <linux/percpu.h>
11#include <linux/string.h>
ee098e1a 12#include <linux/ctype.h>
1da177e4 13#include <linux/delay.h>
68e21be2 14#include <linux/sched/mm.h>
e6017571 15#include <linux/sched/clock.h>
9164bb4a 16#include <linux/sched/task.h>
9766cdbc 17#include <linux/init.h>
0f46efeb 18#include <linux/kprobes.h>
9766cdbc 19#include <linux/kgdb.h>
1da177e4 20#include <linux/smp.h>
9766cdbc 21#include <linux/io.h>
b51ef52d 22#include <linux/syscore_ops.h>
9766cdbc
JSR
23
24#include <asm/stackprotector.h>
cdd6c482 25#include <asm/perf_event.h>
1da177e4 26#include <asm/mmu_context.h>
49d859d7 27#include <asm/archrandom.h>
9766cdbc
JSR
28#include <asm/hypervisor.h>
29#include <asm/processor.h>
1e02ce4c 30#include <asm/tlbflush.h>
f649e938 31#include <asm/debugreg.h>
9766cdbc 32#include <asm/sections.h>
f40c3300 33#include <asm/vsyscall.h>
8bdbd962
AC
34#include <linux/topology.h>
35#include <linux/cpumask.h>
9766cdbc 36#include <asm/pgtable.h>
60063497 37#include <linux/atomic.h>
9766cdbc
JSR
38#include <asm/proto.h>
39#include <asm/setup.h>
40#include <asm/apic.h>
41#include <asm/desc.h>
78f7f1e5 42#include <asm/fpu/internal.h>
27b07da7 43#include <asm/mtrr.h>
0274f955 44#include <asm/hwcap2.h>
8bdbd962 45#include <linux/numa.h>
9766cdbc 46#include <asm/asm.h>
0f6ff2bc 47#include <asm/bugs.h>
9766cdbc 48#include <asm/cpu.h>
a03a3e28 49#include <asm/mce.h>
9766cdbc 50#include <asm/msr.h>
8d4a4300 51#include <asm/pat.h>
d288e1cf
FY
52#include <asm/microcode.h>
53#include <asm/microcode_intel.h>
fec9434a
DW
54#include <asm/intel-family.h>
55#include <asm/cpu_device_id.h>
e641f5f5
IM
56
57#ifdef CONFIG_X86_LOCAL_APIC
bdbcdd48 58#include <asm/uv/uv.h>
1da177e4
LT
59#endif
60
61#include "cpu.h"
62
0274f955
GA
63u32 elf_hwcap2 __read_mostly;
64
c2d1cec1 65/* all of these masks are initialized in setup_cpu_local_masks() */
c2d1cec1 66cpumask_var_t cpu_initialized_mask;
9766cdbc
JSR
67cpumask_var_t cpu_callout_mask;
68cpumask_var_t cpu_callin_mask;
c2d1cec1
MT
69
70/* representing cpus for which sibling maps can be computed */
71cpumask_var_t cpu_sibling_setup_mask;
72
f8b64d08
BP
73/* Number of siblings per CPU package */
74int smp_num_siblings = 1;
75EXPORT_SYMBOL(smp_num_siblings);
76
77/* Last level cache ID of each logical CPU */
78DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
79
2f2f52ba 80/* correctly size the local cpu masks */
4369f1fb 81void __init setup_cpu_local_masks(void)
2f2f52ba
BG
82{
83 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
84 alloc_bootmem_cpumask_var(&cpu_callin_mask);
85 alloc_bootmem_cpumask_var(&cpu_callout_mask);
86 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
87}
88
148f9bb8 89static void default_init(struct cpuinfo_x86 *c)
e8055139
OZ
90{
91#ifdef CONFIG_X86_64
27c13ece 92 cpu_detect_cache_sizes(c);
e8055139
OZ
93#else
94 /* Not much we can do here... */
95 /* Check if at least it has cpuid */
96 if (c->cpuid_level == -1) {
97 /* No cpuid. It must be an ancient CPU */
98 if (c->x86 == 4)
99 strcpy(c->x86_model_id, "486");
100 else if (c->x86 == 3)
101 strcpy(c->x86_model_id, "386");
102 }
103#endif
104}
105
148f9bb8 106static const struct cpu_dev default_cpu = {
e8055139
OZ
107 .c_init = default_init,
108 .c_vendor = "Unknown",
109 .c_x86_vendor = X86_VENDOR_UNKNOWN,
110};
111
148f9bb8 112static const struct cpu_dev *this_cpu = &default_cpu;
0a488a53 113
06deef89 114DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
950ad7ff 115#ifdef CONFIG_X86_64
06deef89
BG
116 /*
117 * We need valid kernel segments for data and code in long mode too
118 * IRET will check the segment types kkeil 2000/10/28
119 * Also sysret mandates a special GDT layout
120 *
9766cdbc 121 * TLS descriptors are currently at a different place compared to i386.
06deef89
BG
122 * Hopefully nobody expects them at a fixed place (Wine?)
123 */
1e5de182
AM
124 [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
125 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
126 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
127 [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
128 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
129 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
950ad7ff 130#else
1e5de182
AM
131 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
132 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
133 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
134 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
bf504672
RR
135 /*
136 * Segments used for calling PnP BIOS have byte granularity.
137 * They code segments and data segments have fixed 64k limits,
138 * the transfer segment sizes are set at run time.
139 */
6842ef0e 140 /* 32-bit code */
1e5de182 141 [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
6842ef0e 142 /* 16-bit code */
1e5de182 143 [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
6842ef0e 144 /* 16-bit data */
1e5de182 145 [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
6842ef0e 146 /* 16-bit data */
1e5de182 147 [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
6842ef0e 148 /* 16-bit data */
1e5de182 149 [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
bf504672
RR
150 /*
151 * The APM segments have byte granularity and their bases
152 * are set at run time. All have 64k limits.
153 */
6842ef0e 154 /* 32-bit code */
1e5de182 155 [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
bf504672 156 /* 16-bit code */
1e5de182 157 [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
6842ef0e 158 /* data */
72c4d853 159 [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
bf504672 160
1e5de182
AM
161 [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
162 [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
60a5317f 163 GDT_STACK_CANARY_INIT
950ad7ff 164#endif
06deef89 165} };
7a61d35d 166EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
ae1ee11b 167
8c3641e9 168static int __init x86_mpx_setup(char *s)
0c752a93 169{
8c3641e9 170 /* require an exact match without trailing characters */
2cd3949f
DH
171 if (strlen(s))
172 return 0;
0c752a93 173
8c3641e9
DH
174 /* do not emit a message if the feature is not present */
175 if (!boot_cpu_has(X86_FEATURE_MPX))
176 return 1;
6bad06b7 177
8c3641e9
DH
178 setup_clear_cpu_cap(X86_FEATURE_MPX);
179 pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
b6f42a4a
FY
180 return 1;
181}
8c3641e9 182__setup("nompx", x86_mpx_setup);
b6f42a4a 183
0790c9aa 184#ifdef CONFIG_X86_64
c7ad5ad2 185static int __init x86_nopcid_setup(char *s)
0790c9aa 186{
c7ad5ad2
AL
187 /* nopcid doesn't accept parameters */
188 if (s)
189 return -EINVAL;
0790c9aa
AL
190
191 /* do not emit a message if the feature is not present */
192 if (!boot_cpu_has(X86_FEATURE_PCID))
c7ad5ad2 193 return 0;
0790c9aa
AL
194
195 setup_clear_cpu_cap(X86_FEATURE_PCID);
196 pr_info("nopcid: PCID feature disabled\n");
c7ad5ad2 197 return 0;
0790c9aa 198}
c7ad5ad2 199early_param("nopcid", x86_nopcid_setup);
0790c9aa
AL
200#endif
201
d12a72b8
AL
202static int __init x86_noinvpcid_setup(char *s)
203{
204 /* noinvpcid doesn't accept parameters */
205 if (s)
206 return -EINVAL;
207
208 /* do not emit a message if the feature is not present */
209 if (!boot_cpu_has(X86_FEATURE_INVPCID))
210 return 0;
211
212 setup_clear_cpu_cap(X86_FEATURE_INVPCID);
213 pr_info("noinvpcid: INVPCID feature disabled\n");
214 return 0;
215}
216early_param("noinvpcid", x86_noinvpcid_setup);
217
ba51dced 218#ifdef CONFIG_X86_32
148f9bb8
PG
219static int cachesize_override = -1;
220static int disable_x86_serial_nr = 1;
1da177e4 221
0a488a53
YL
222static int __init cachesize_setup(char *str)
223{
224 get_option(&str, &cachesize_override);
225 return 1;
226}
227__setup("cachesize=", cachesize_setup);
228
0a488a53
YL
229static int __init x86_sep_setup(char *s)
230{
231 setup_clear_cpu_cap(X86_FEATURE_SEP);
232 return 1;
233}
234__setup("nosep", x86_sep_setup);
235
236/* Standard macro to see if a specific flag is changeable */
237static inline int flag_is_changeable_p(u32 flag)
238{
239 u32 f1, f2;
240
94f6bac1
KH
241 /*
242 * Cyrix and IDT cpus allow disabling of CPUID
243 * so the code below may return different results
244 * when it is executed before and after enabling
245 * the CPUID. Add "volatile" to not allow gcc to
246 * optimize the subsequent calls to this function.
247 */
0f3fa48a
IM
248 asm volatile ("pushfl \n\t"
249 "pushfl \n\t"
250 "popl %0 \n\t"
251 "movl %0, %1 \n\t"
252 "xorl %2, %0 \n\t"
253 "pushl %0 \n\t"
254 "popfl \n\t"
255 "pushfl \n\t"
256 "popl %0 \n\t"
257 "popfl \n\t"
258
94f6bac1
KH
259 : "=&r" (f1), "=&r" (f2)
260 : "ir" (flag));
0a488a53
YL
261
262 return ((f1^f2) & flag) != 0;
263}
264
265/* Probe for the CPUID instruction */
148f9bb8 266int have_cpuid_p(void)
0a488a53
YL
267{
268 return flag_is_changeable_p(X86_EFLAGS_ID);
269}
270
148f9bb8 271static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
0a488a53 272{
0f3fa48a
IM
273 unsigned long lo, hi;
274
275 if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
276 return;
277
278 /* Disable processor serial number: */
279
280 rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
281 lo |= 0x200000;
282 wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
283
1b74dde7 284 pr_notice("CPU serial number disabled.\n");
0f3fa48a
IM
285 clear_cpu_cap(c, X86_FEATURE_PN);
286
287 /* Disabling the serial number may affect the cpuid level */
288 c->cpuid_level = cpuid_eax(0);
0a488a53
YL
289}
290
291static int __init x86_serial_nr_setup(char *s)
292{
293 disable_x86_serial_nr = 0;
294 return 1;
295}
296__setup("serialnumber", x86_serial_nr_setup);
ba51dced 297#else
102bbe3a
YL
298static inline int flag_is_changeable_p(u32 flag)
299{
300 return 1;
301}
102bbe3a
YL
302static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
303{
304}
ba51dced 305#endif
0a488a53 306
de5397ad
FY
307static __init int setup_disable_smep(char *arg)
308{
b2cc2a07 309 setup_clear_cpu_cap(X86_FEATURE_SMEP);
0f6ff2bc
DH
310 /* Check for things that depend on SMEP being enabled: */
311 check_mpx_erratum(&boot_cpu_data);
de5397ad
FY
312 return 1;
313}
314__setup("nosmep", setup_disable_smep);
315
b2cc2a07 316static __always_inline void setup_smep(struct cpuinfo_x86 *c)
de5397ad 317{
b2cc2a07 318 if (cpu_has(c, X86_FEATURE_SMEP))
375074cc 319 cr4_set_bits(X86_CR4_SMEP);
de5397ad
FY
320}
321
52b6179a
PA
322static __init int setup_disable_smap(char *arg)
323{
b2cc2a07 324 setup_clear_cpu_cap(X86_FEATURE_SMAP);
52b6179a
PA
325 return 1;
326}
327__setup("nosmap", setup_disable_smap);
328
b2cc2a07
PA
329static __always_inline void setup_smap(struct cpuinfo_x86 *c)
330{
581b7f15 331 unsigned long eflags = native_save_fl();
b2cc2a07
PA
332
333 /* This should have been cleared long ago */
b2cc2a07
PA
334 BUG_ON(eflags & X86_EFLAGS_AC);
335
03bbd596
PA
336 if (cpu_has(c, X86_FEATURE_SMAP)) {
337#ifdef CONFIG_X86_SMAP
375074cc 338 cr4_set_bits(X86_CR4_SMAP);
03bbd596 339#else
375074cc 340 cr4_clear_bits(X86_CR4_SMAP);
03bbd596
PA
341#endif
342 }
de5397ad
FY
343}
344
aa35f896
RN
345static __always_inline void setup_umip(struct cpuinfo_x86 *c)
346{
347 /* Check the boot processor, plus build option for UMIP. */
348 if (!cpu_feature_enabled(X86_FEATURE_UMIP))
349 goto out;
350
351 /* Check the current processor's cpuid bits. */
352 if (!cpu_has(c, X86_FEATURE_UMIP))
353 goto out;
354
355 cr4_set_bits(X86_CR4_UMIP);
356
438cbf88 357 pr_info_once("x86/cpu: User Mode Instruction Prevention (UMIP) activated\n");
770c7755 358
aa35f896
RN
359 return;
360
361out:
362 /*
363 * Make sure UMIP is disabled in case it was enabled in a
364 * previous boot (e.g., via kexec).
365 */
366 cr4_clear_bits(X86_CR4_UMIP);
367}
368
7652ac92
TG
369static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
370static unsigned long cr4_pinned_bits __ro_after_init;
371
372void native_write_cr0(unsigned long val)
373{
374 unsigned long bits_missing = 0;
375
376set_register:
377 asm volatile("mov %0,%%cr0": "+r" (val), "+m" (__force_order));
378
379 if (static_branch_likely(&cr_pinning)) {
380 if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) {
381 bits_missing = X86_CR0_WP;
382 val |= bits_missing;
383 goto set_register;
384 }
385 /* Warn after we've set the missing bits. */
386 WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n");
387 }
388}
389EXPORT_SYMBOL(native_write_cr0);
390
391void native_write_cr4(unsigned long val)
392{
393 unsigned long bits_missing = 0;
394
395set_register:
396 asm volatile("mov %0,%%cr4": "+r" (val), "+m" (cr4_pinned_bits));
397
398 if (static_branch_likely(&cr_pinning)) {
399 if (unlikely((val & cr4_pinned_bits) != cr4_pinned_bits)) {
400 bits_missing = ~val & cr4_pinned_bits;
401 val |= bits_missing;
402 goto set_register;
403 }
404 /* Warn after we've set the missing bits. */
405 WARN_ONCE(bits_missing, "CR4 bits went missing: %lx!?\n",
406 bits_missing);
407 }
408}
409EXPORT_SYMBOL(native_write_cr4);
410
411void cr4_init(void)
412{
413 unsigned long cr4 = __read_cr4();
414
415 if (boot_cpu_has(X86_FEATURE_PCID))
416 cr4 |= X86_CR4_PCIDE;
417 if (static_branch_likely(&cr_pinning))
418 cr4 |= cr4_pinned_bits;
419
420 __write_cr4(cr4);
421
422 /* Initialize cr4 shadow for this CPU. */
423 this_cpu_write(cpu_tlbstate.cr4, cr4);
424}
873d50d5
KC
425
426/*
427 * Once CPU feature detection is finished (and boot params have been
428 * parsed), record any of the sensitive CR bits that are set, and
429 * enable CR pinning.
430 */
431static void __init setup_cr_pinning(void)
432{
433 unsigned long mask;
434
435 mask = (X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP);
436 cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & mask;
437 static_key_enable(&cr_pinning.key);
438}
439
06976945
DH
440/*
441 * Protection Keys are not available in 32-bit mode.
442 */
443static bool pku_disabled;
444
445static __always_inline void setup_pku(struct cpuinfo_x86 *c)
446{
a5eff725
SAS
447 struct pkru_state *pk;
448
e8df1a95
DH
449 /* check the boot processor, plus compile options for PKU: */
450 if (!cpu_feature_enabled(X86_FEATURE_PKU))
451 return;
452 /* checks the actual processor's cpuid bits: */
06976945
DH
453 if (!cpu_has(c, X86_FEATURE_PKU))
454 return;
455 if (pku_disabled)
456 return;
457
458 cr4_set_bits(X86_CR4_PKE);
a5eff725
SAS
459 pk = get_xsave_addr(&init_fpstate.xsave, XFEATURE_PKRU);
460 if (pk)
461 pk->pkru = init_pkru_value;
06976945
DH
462 /*
463 * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
464 * cpuid bit to be set. We need to ensure that we
465 * update that bit in this CPU's "cpu_info".
466 */
467 get_cpu_cap(c);
468}
469
470#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
471static __init int setup_disable_pku(char *arg)
472{
473 /*
474 * Do not clear the X86_FEATURE_PKU bit. All of the
475 * runtime checks are against OSPKE so clearing the
476 * bit does nothing.
477 *
478 * This way, we will see "pku" in cpuinfo, but not
479 * "ospke", which is exactly what we want. It shows
480 * that the CPU has PKU, but the OS has not enabled it.
481 * This happens to be exactly how a system would look
482 * if we disabled the config option.
483 */
484 pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
485 pku_disabled = true;
486 return 1;
487}
488__setup("nopku", setup_disable_pku);
489#endif /* CONFIG_X86_64 */
490
b38b0665
PA
491/*
492 * Some CPU features depend on higher CPUID levels, which may not always
493 * be available due to CPUID level capping or broken virtualization
494 * software. Add those features to this table to auto-disable them.
495 */
496struct cpuid_dependent_feature {
497 u32 feature;
498 u32 level;
499};
0f3fa48a 500
148f9bb8 501static const struct cpuid_dependent_feature
b38b0665
PA
502cpuid_dependent_features[] = {
503 { X86_FEATURE_MWAIT, 0x00000005 },
504 { X86_FEATURE_DCA, 0x00000009 },
505 { X86_FEATURE_XSAVE, 0x0000000d },
506 { 0, 0 }
507};
508
148f9bb8 509static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
b38b0665
PA
510{
511 const struct cpuid_dependent_feature *df;
9766cdbc 512
b38b0665 513 for (df = cpuid_dependent_features; df->feature; df++) {
0f3fa48a
IM
514
515 if (!cpu_has(c, df->feature))
516 continue;
b38b0665
PA
517 /*
518 * Note: cpuid_level is set to -1 if unavailable, but
519 * extended_extended_level is set to 0 if unavailable
520 * and the legitimate extended levels are all negative
521 * when signed; hence the weird messing around with
522 * signs here...
523 */
0f3fa48a 524 if (!((s32)df->level < 0 ?
f6db44df 525 (u32)df->level > (u32)c->extended_cpuid_level :
0f3fa48a
IM
526 (s32)df->level > (s32)c->cpuid_level))
527 continue;
528
529 clear_cpu_cap(c, df->feature);
530 if (!warn)
531 continue;
532
1b74dde7
CY
533 pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
534 x86_cap_flag(df->feature), df->level);
b38b0665 535 }
f6db44df 536}
b38b0665 537
102bbe3a
YL
538/*
539 * Naming convention should be: <Name> [(<Codename>)]
540 * This table only is used unless init_<vendor>() below doesn't set it;
0f3fa48a
IM
541 * in particular, if CPUID levels 0x80000002..4 are supported, this
542 * isn't used
102bbe3a
YL
543 */
544
545/* Look up CPU names by table lookup. */
148f9bb8 546static const char *table_lookup_model(struct cpuinfo_x86 *c)
102bbe3a 547{
09dc68d9
JB
548#ifdef CONFIG_X86_32
549 const struct legacy_cpu_model_info *info;
102bbe3a
YL
550
551 if (c->x86_model >= 16)
552 return NULL; /* Range check */
553
554 if (!this_cpu)
555 return NULL;
556
09dc68d9 557 info = this_cpu->legacy_models;
102bbe3a 558
09dc68d9 559 while (info->family) {
102bbe3a
YL
560 if (info->family == c->x86)
561 return info->model_names[c->x86_model];
562 info++;
563 }
09dc68d9 564#endif
102bbe3a
YL
565 return NULL; /* Not found */
566}
567
6cbd2171
TG
568__u32 cpu_caps_cleared[NCAPINTS + NBUGINTS];
569__u32 cpu_caps_set[NCAPINTS + NBUGINTS];
7d851c8d 570
11e3a840
JF
571void load_percpu_segment(int cpu)
572{
573#ifdef CONFIG_X86_32
574 loadsegment(fs, __KERNEL_PERCPU);
575#else
45e876f7 576 __loadsegment_simple(gs, 0);
35060ed6 577 wrmsrl(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu));
11e3a840 578#endif
60a5317f 579 load_stack_canary_segment();
11e3a840
JF
580}
581
72f5e08d
AL
582#ifdef CONFIG_X86_32
583/* The 32-bit entry code needs to find cpu_entry_area. */
584DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
585#endif
586
45fc8757
TG
587/* Load the original GDT from the per-cpu structure */
588void load_direct_gdt(int cpu)
589{
590 struct desc_ptr gdt_descr;
591
592 gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
593 gdt_descr.size = GDT_SIZE - 1;
594 load_gdt(&gdt_descr);
595}
596EXPORT_SYMBOL_GPL(load_direct_gdt);
597
69218e47
TG
598/* Load a fixmap remapping of the per-cpu GDT */
599void load_fixmap_gdt(int cpu)
600{
601 struct desc_ptr gdt_descr;
602
603 gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
604 gdt_descr.size = GDT_SIZE - 1;
605 load_gdt(&gdt_descr);
606}
45fc8757 607EXPORT_SYMBOL_GPL(load_fixmap_gdt);
69218e47 608
0f3fa48a
IM
609/*
610 * Current gdt points %fs at the "master" per-cpu area: after this,
611 * it's on the real one.
612 */
552be871 613void switch_to_new_gdt(int cpu)
9d31d35b 614{
45fc8757
TG
615 /* Load the original GDT */
616 load_direct_gdt(cpu);
2697fbd5 617 /* Reload the per-cpu base */
11e3a840 618 load_percpu_segment(cpu);
9d31d35b
YL
619}
620
148f9bb8 621static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
1da177e4 622
148f9bb8 623static void get_model_name(struct cpuinfo_x86 *c)
1da177e4
LT
624{
625 unsigned int *v;
ee098e1a 626 char *p, *q, *s;
1da177e4 627
3da99c97 628 if (c->extended_cpuid_level < 0x80000004)
1b05d60d 629 return;
1da177e4 630
0f3fa48a 631 v = (unsigned int *)c->x86_model_id;
1da177e4
LT
632 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
633 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
634 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
635 c->x86_model_id[48] = 0;
636
ee098e1a
BP
637 /* Trim whitespace */
638 p = q = s = &c->x86_model_id[0];
639
640 while (*p == ' ')
641 p++;
642
643 while (*p) {
644 /* Note the last non-whitespace index */
645 if (!isspace(*p))
646 s = q;
647
648 *q++ = *p++;
649 }
650
651 *(s + 1) = '\0';
1da177e4
LT
652}
653
9305bd6c 654void detect_num_cpu_cores(struct cpuinfo_x86 *c)
2cc61be6
DW
655{
656 unsigned int eax, ebx, ecx, edx;
657
9305bd6c 658 c->x86_max_cores = 1;
2cc61be6 659 if (!IS_ENABLED(CONFIG_SMP) || c->cpuid_level < 4)
9305bd6c 660 return;
2cc61be6
DW
661
662 cpuid_count(4, 0, &eax, &ebx, &ecx, &edx);
663 if (eax & 0x1f)
9305bd6c 664 c->x86_max_cores = (eax >> 26) + 1;
2cc61be6
DW
665}
666
148f9bb8 667void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
1da177e4 668{
9d31d35b 669 unsigned int n, dummy, ebx, ecx, edx, l2size;
1da177e4 670
3da99c97 671 n = c->extended_cpuid_level;
1da177e4
LT
672
673 if (n >= 0x80000005) {
9d31d35b 674 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
9d31d35b 675 c->x86_cache_size = (ecx>>24) + (edx>>24);
140fc727
YL
676#ifdef CONFIG_X86_64
677 /* On K8 L1 TLB is inclusive, so don't count it */
678 c->x86_tlbsize = 0;
679#endif
1da177e4
LT
680 }
681
682 if (n < 0x80000006) /* Some chips just has a large L1. */
683 return;
684
0a488a53 685 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
1da177e4 686 l2size = ecx >> 16;
34048c9e 687
140fc727
YL
688#ifdef CONFIG_X86_64
689 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
690#else
1da177e4 691 /* do processor-specific cache resizing */
09dc68d9
JB
692 if (this_cpu->legacy_cache_size)
693 l2size = this_cpu->legacy_cache_size(c, l2size);
1da177e4
LT
694
695 /* Allow user to override all this if necessary. */
696 if (cachesize_override != -1)
697 l2size = cachesize_override;
698
34048c9e 699 if (l2size == 0)
1da177e4 700 return; /* Again, no L2 cache is possible */
140fc727 701#endif
1da177e4
LT
702
703 c->x86_cache_size = l2size;
1da177e4
LT
704}
705
e0ba94f1
AS
706u16 __read_mostly tlb_lli_4k[NR_INFO];
707u16 __read_mostly tlb_lli_2m[NR_INFO];
708u16 __read_mostly tlb_lli_4m[NR_INFO];
709u16 __read_mostly tlb_lld_4k[NR_INFO];
710u16 __read_mostly tlb_lld_2m[NR_INFO];
711u16 __read_mostly tlb_lld_4m[NR_INFO];
dd360393 712u16 __read_mostly tlb_lld_1g[NR_INFO];
e0ba94f1 713
f94fe119 714static void cpu_detect_tlb(struct cpuinfo_x86 *c)
e0ba94f1
AS
715{
716 if (this_cpu->c_detect_tlb)
717 this_cpu->c_detect_tlb(c);
718
f94fe119 719 pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
e0ba94f1 720 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
f94fe119
SH
721 tlb_lli_4m[ENTRIES]);
722
723 pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
724 tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
725 tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
e0ba94f1
AS
726}
727
545401f4 728int detect_ht_early(struct cpuinfo_x86 *c)
1da177e4 729{
c8e56d20 730#ifdef CONFIG_SMP
0a488a53 731 u32 eax, ebx, ecx, edx;
1da177e4 732
0a488a53 733 if (!cpu_has(c, X86_FEATURE_HT))
545401f4 734 return -1;
1da177e4 735
0a488a53 736 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
545401f4 737 return -1;
1da177e4 738
1cd78776 739 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
545401f4 740 return -1;
1da177e4 741
0a488a53 742 cpuid(1, &eax, &ebx, &ecx, &edx);
1da177e4 743
9d31d35b 744 smp_num_siblings = (ebx & 0xff0000) >> 16;
545401f4 745 if (smp_num_siblings == 1)
1b74dde7 746 pr_info_once("CPU0: Hyper-Threading is disabled\n");
545401f4
TG
747#endif
748 return 0;
749}
9d31d35b 750
545401f4
TG
751void detect_ht(struct cpuinfo_x86 *c)
752{
753#ifdef CONFIG_SMP
754 int index_msb, core_bits;
55e6d279 755
545401f4 756 if (detect_ht_early(c) < 0)
55e6d279 757 return;
9d31d35b 758
0f3fa48a
IM
759 index_msb = get_count_order(smp_num_siblings);
760 c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
9d31d35b 761
0f3fa48a 762 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
9d31d35b 763
0f3fa48a 764 index_msb = get_count_order(smp_num_siblings);
9d31d35b 765
0f3fa48a 766 core_bits = get_count_order(c->x86_max_cores);
9d31d35b 767
0f3fa48a
IM
768 c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
769 ((1 << core_bits) - 1);
9d31d35b 770#endif
97e4db7c 771}
1da177e4 772
148f9bb8 773static void get_cpu_vendor(struct cpuinfo_x86 *c)
1da177e4
LT
774{
775 char *v = c->x86_vendor_id;
0f3fa48a 776 int i;
1da177e4
LT
777
778 for (i = 0; i < X86_VENDOR_NUM; i++) {
10a434fc
YL
779 if (!cpu_devs[i])
780 break;
781
782 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
783 (cpu_devs[i]->c_ident[1] &&
784 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
0f3fa48a 785
10a434fc
YL
786 this_cpu = cpu_devs[i];
787 c->x86_vendor = this_cpu->c_x86_vendor;
788 return;
1da177e4
LT
789 }
790 }
10a434fc 791
1b74dde7
CY
792 pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
793 "CPU: Your system may be unstable.\n", v);
10a434fc 794
fe38d855
CE
795 c->x86_vendor = X86_VENDOR_UNKNOWN;
796 this_cpu = &default_cpu;
1da177e4
LT
797}
798
148f9bb8 799void cpu_detect(struct cpuinfo_x86 *c)
1da177e4 800{
1da177e4 801 /* Get vendor name */
4a148513
HH
802 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
803 (unsigned int *)&c->x86_vendor_id[0],
804 (unsigned int *)&c->x86_vendor_id[8],
805 (unsigned int *)&c->x86_vendor_id[4]);
1da177e4 806
1da177e4 807 c->x86 = 4;
9d31d35b 808 /* Intel-defined flags: level 0x00000001 */
1da177e4
LT
809 if (c->cpuid_level >= 0x00000001) {
810 u32 junk, tfms, cap0, misc;
0f3fa48a 811
1da177e4 812 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
99f925ce
BP
813 c->x86 = x86_family(tfms);
814 c->x86_model = x86_model(tfms);
b399151c 815 c->x86_stepping = x86_stepping(tfms);
0f3fa48a 816
d4387bd3 817 if (cap0 & (1<<19)) {
d4387bd3 818 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
9d31d35b 819 c->x86_cache_alignment = c->x86_clflush_size;
d4387bd3 820 }
1da177e4 821 }
1da177e4 822}
3da99c97 823
8bf1ebca
AL
824static void apply_forced_caps(struct cpuinfo_x86 *c)
825{
826 int i;
827
6cbd2171 828 for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
8bf1ebca
AL
829 c->x86_capability[i] &= ~cpu_caps_cleared[i];
830 c->x86_capability[i] |= cpu_caps_set[i];
831 }
832}
833
7fcae111
DW
834static void init_speculation_control(struct cpuinfo_x86 *c)
835{
836 /*
837 * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
838 * and they also have a different bit for STIBP support. Also,
839 * a hypervisor might have set the individual AMD bits even on
840 * Intel CPUs, for finer-grained selection of what's available.
7fcae111
DW
841 */
842 if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
843 set_cpu_cap(c, X86_FEATURE_IBRS);
844 set_cpu_cap(c, X86_FEATURE_IBPB);
7eb8956a 845 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
7fcae111 846 }
e7c587da 847
7fcae111
DW
848 if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
849 set_cpu_cap(c, X86_FEATURE_STIBP);
e7c587da 850
bc226f07
TL
851 if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
852 cpu_has(c, X86_FEATURE_VIRT_SSBD))
52817587
TG
853 set_cpu_cap(c, X86_FEATURE_SSBD);
854
7eb8956a 855 if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
e7c587da 856 set_cpu_cap(c, X86_FEATURE_IBRS);
7eb8956a
TG
857 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
858 }
e7c587da
BP
859
860 if (cpu_has(c, X86_FEATURE_AMD_IBPB))
861 set_cpu_cap(c, X86_FEATURE_IBPB);
862
7eb8956a 863 if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
e7c587da 864 set_cpu_cap(c, X86_FEATURE_STIBP);
7eb8956a
TG
865 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
866 }
6ac2f49e
KRW
867
868 if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
869 set_cpu_cap(c, X86_FEATURE_SSBD);
870 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
871 clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
872 }
7fcae111
DW
873}
874
45fc56e6
BP
875static void init_cqm(struct cpuinfo_x86 *c)
876{
acec0ce0
FY
877 if (!cpu_has(c, X86_FEATURE_CQM_LLC)) {
878 c->x86_cache_max_rmid = -1;
879 c->x86_cache_occ_scale = -1;
880 return;
881 }
45fc56e6 882
acec0ce0
FY
883 /* will be overridden if occupancy monitoring exists */
884 c->x86_cache_max_rmid = cpuid_ebx(0xf);
885
886 if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC) ||
887 cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL) ||
888 cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)) {
889 u32 eax, ebx, ecx, edx;
890
891 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
892 cpuid_count(0xf, 1, &eax, &ebx, &ecx, &edx);
893
894 c->x86_cache_max_rmid = ecx;
895 c->x86_cache_occ_scale = ebx;
45fc56e6
BP
896 }
897}
898
148f9bb8 899void get_cpu_cap(struct cpuinfo_x86 *c)
093af8d7 900{
39c06df4 901 u32 eax, ebx, ecx, edx;
093af8d7 902
3da99c97
YL
903 /* Intel-defined flags: level 0x00000001 */
904 if (c->cpuid_level >= 0x00000001) {
39c06df4 905 cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
0f3fa48a 906
39c06df4
BP
907 c->x86_capability[CPUID_1_ECX] = ecx;
908 c->x86_capability[CPUID_1_EDX] = edx;
3da99c97 909 }
093af8d7 910
3df8d920
AL
911 /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
912 if (c->cpuid_level >= 0x00000006)
913 c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
914
bdc802dc
PA
915 /* Additional Intel-defined flags: level 0x00000007 */
916 if (c->cpuid_level >= 0x00000007) {
bdc802dc 917 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
39c06df4 918 c->x86_capability[CPUID_7_0_EBX] = ebx;
dfb4a70f 919 c->x86_capability[CPUID_7_ECX] = ecx;
95ca0ee8 920 c->x86_capability[CPUID_7_EDX] = edx;
b302e4b1
FY
921
922 /* Check valid sub-leaf index before accessing it */
923 if (eax >= 1) {
924 cpuid_count(0x00000007, 1, &eax, &ebx, &ecx, &edx);
925 c->x86_capability[CPUID_7_1_EAX] = eax;
926 }
bdc802dc
PA
927 }
928
6229ad27
FY
929 /* Extended state features: level 0x0000000d */
930 if (c->cpuid_level >= 0x0000000d) {
6229ad27
FY
931 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
932
39c06df4 933 c->x86_capability[CPUID_D_1_EAX] = eax;
6229ad27
FY
934 }
935
3da99c97 936 /* AMD-defined flags: level 0x80000001 */
39c06df4
BP
937 eax = cpuid_eax(0x80000000);
938 c->extended_cpuid_level = eax;
939
940 if ((eax & 0xffff0000) == 0x80000000) {
941 if (eax >= 0x80000001) {
942 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
0f3fa48a 943
39c06df4
BP
944 c->x86_capability[CPUID_8000_0001_ECX] = ecx;
945 c->x86_capability[CPUID_8000_0001_EDX] = edx;
093af8d7 946 }
093af8d7 947 }
093af8d7 948
71faad43
YG
949 if (c->extended_cpuid_level >= 0x80000007) {
950 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
951
952 c->x86_capability[CPUID_8000_0007_EBX] = ebx;
953 c->x86_power = edx;
954 }
955
c65732e4
TG
956 if (c->extended_cpuid_level >= 0x80000008) {
957 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
958 c->x86_capability[CPUID_8000_0008_EBX] = ebx;
959 }
960
2ccd71f1 961 if (c->extended_cpuid_level >= 0x8000000a)
39c06df4 962 c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
093af8d7 963
1dedefd1 964 init_scattered_cpuid_features(c);
7fcae111 965 init_speculation_control(c);
45fc56e6 966 init_cqm(c);
60d34501
AL
967
968 /*
969 * Clear/Set all flags overridden by options, after probe.
970 * This needs to happen each time we re-probe, which may happen
971 * several times during CPU initialization.
972 */
973 apply_forced_caps(c);
093af8d7 974}
1da177e4 975
405c018a 976void get_cpu_address_sizes(struct cpuinfo_x86 *c)
d94a155c
KS
977{
978 u32 eax, ebx, ecx, edx;
979
980 if (c->extended_cpuid_level >= 0x80000008) {
981 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
982
983 c->x86_virt_bits = (eax >> 8) & 0xff;
984 c->x86_phys_bits = eax & 0xff;
d94a155c
KS
985 }
986#ifdef CONFIG_X86_32
987 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
988 c->x86_phys_bits = 36;
989#endif
cc51e542 990 c->x86_cache_bits = c->x86_phys_bits;
d94a155c
KS
991}
992
148f9bb8 993static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
aef93c8b
YL
994{
995#ifdef CONFIG_X86_32
996 int i;
997
998 /*
999 * First of all, decide if this is a 486 or higher
1000 * It's a 486 if we can modify the AC flag
1001 */
1002 if (flag_is_changeable_p(X86_EFLAGS_AC))
1003 c->x86 = 4;
1004 else
1005 c->x86 = 3;
1006
1007 for (i = 0; i < X86_VENDOR_NUM; i++)
1008 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
1009 c->x86_vendor_id[0] = 0;
1010 cpu_devs[i]->c_identify(c);
1011 if (c->x86_vendor_id[0]) {
1012 get_cpu_vendor(c);
1013 break;
1014 }
1015 }
1016#endif
1017}
1018
36ad3513
TG
1019#define NO_SPECULATION BIT(0)
1020#define NO_MELTDOWN BIT(1)
1021#define NO_SSB BIT(2)
1022#define NO_L1TF BIT(3)
ed5194c2 1023#define NO_MDS BIT(4)
e261f209 1024#define MSBDS_ONLY BIT(5)
36ad3513
TG
1025
1026#define VULNWL(_vendor, _family, _model, _whitelist) \
1027 { X86_VENDOR_##_vendor, _family, _model, X86_FEATURE_ANY, _whitelist }
1028
1029#define VULNWL_INTEL(model, whitelist) \
1030 VULNWL(INTEL, 6, INTEL_FAM6_##model, whitelist)
1031
1032#define VULNWL_AMD(family, whitelist) \
1033 VULNWL(AMD, family, X86_MODEL_ANY, whitelist)
1034
1035#define VULNWL_HYGON(family, whitelist) \
1036 VULNWL(HYGON, family, X86_MODEL_ANY, whitelist)
1037
1038static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
1039 VULNWL(ANY, 4, X86_MODEL_ANY, NO_SPECULATION),
1040 VULNWL(CENTAUR, 5, X86_MODEL_ANY, NO_SPECULATION),
1041 VULNWL(INTEL, 5, X86_MODEL_ANY, NO_SPECULATION),
1042 VULNWL(NSC, 5, X86_MODEL_ANY, NO_SPECULATION),
1043
ed5194c2 1044 /* Intel Family 6 */
36ad3513
TG
1045 VULNWL_INTEL(ATOM_SALTWELL, NO_SPECULATION),
1046 VULNWL_INTEL(ATOM_SALTWELL_TABLET, NO_SPECULATION),
1047 VULNWL_INTEL(ATOM_SALTWELL_MID, NO_SPECULATION),
1048 VULNWL_INTEL(ATOM_BONNELL, NO_SPECULATION),
1049 VULNWL_INTEL(ATOM_BONNELL_MID, NO_SPECULATION),
1050
e261f209
TG
1051 VULNWL_INTEL(ATOM_SILVERMONT, NO_SSB | NO_L1TF | MSBDS_ONLY),
1052 VULNWL_INTEL(ATOM_SILVERMONT_X, NO_SSB | NO_L1TF | MSBDS_ONLY),
1053 VULNWL_INTEL(ATOM_SILVERMONT_MID, NO_SSB | NO_L1TF | MSBDS_ONLY),
1054 VULNWL_INTEL(ATOM_AIRMONT, NO_SSB | NO_L1TF | MSBDS_ONLY),
1055 VULNWL_INTEL(XEON_PHI_KNL, NO_SSB | NO_L1TF | MSBDS_ONLY),
1056 VULNWL_INTEL(XEON_PHI_KNM, NO_SSB | NO_L1TF | MSBDS_ONLY),
36ad3513
TG
1057
1058 VULNWL_INTEL(CORE_YONAH, NO_SSB),
1059
e261f209 1060 VULNWL_INTEL(ATOM_AIRMONT_MID, NO_L1TF | MSBDS_ONLY),
36ad3513 1061
ed5194c2
AK
1062 VULNWL_INTEL(ATOM_GOLDMONT, NO_MDS | NO_L1TF),
1063 VULNWL_INTEL(ATOM_GOLDMONT_X, NO_MDS | NO_L1TF),
1064 VULNWL_INTEL(ATOM_GOLDMONT_PLUS, NO_MDS | NO_L1TF),
1065
1066 /* AMD Family 0xf - 0x12 */
1067 VULNWL_AMD(0x0f, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS),
1068 VULNWL_AMD(0x10, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS),
1069 VULNWL_AMD(0x11, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS),
1070 VULNWL_AMD(0x12, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS),
36ad3513
TG
1071
1072 /* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */
ed5194c2
AK
1073 VULNWL_AMD(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS),
1074 VULNWL_HYGON(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS),
fec9434a
DW
1075 {}
1076};
1077
36ad3513
TG
1078static bool __init cpu_matches(unsigned long which)
1079{
1080 const struct x86_cpu_id *m = x86_match_cpu(cpu_vuln_whitelist);
c456442c 1081
36ad3513
TG
1082 return m && !!(m->driver_data & which);
1083}
17dbca11 1084
4a28bfe3 1085static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
fec9434a
DW
1086{
1087 u64 ia32_cap = 0;
1088
36ad3513 1089 if (cpu_matches(NO_SPECULATION))
8ecc4979
DB
1090 return;
1091
1092 setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
1093 setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
1094
77243971
KRW
1095 if (cpu_has(c, X86_FEATURE_ARCH_CAPABILITIES))
1096 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
1097
36ad3513 1098 if (!cpu_matches(NO_SSB) && !(ia32_cap & ARCH_CAP_SSB_NO) &&
24809860 1099 !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
c456442c
KRW
1100 setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
1101
706d5168
SP
1102 if (ia32_cap & ARCH_CAP_IBRS_ALL)
1103 setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
1104
e261f209 1105 if (!cpu_matches(NO_MDS) && !(ia32_cap & ARCH_CAP_MDS_NO)) {
ed5194c2 1106 setup_force_cpu_bug(X86_BUG_MDS);
e261f209
TG
1107 if (cpu_matches(MSBDS_ONLY))
1108 setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
1109 }
ed5194c2 1110
36ad3513 1111 if (cpu_matches(NO_MELTDOWN))
4a28bfe3 1112 return;
fec9434a 1113
fec9434a
DW
1114 /* Rogue Data Cache Load? No! */
1115 if (ia32_cap & ARCH_CAP_RDCL_NO)
4a28bfe3 1116 return;
fec9434a 1117
4a28bfe3 1118 setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
17dbca11 1119
36ad3513 1120 if (cpu_matches(NO_L1TF))
17dbca11
AK
1121 return;
1122
1123 setup_force_cpu_bug(X86_BUG_L1TF);
fec9434a
DW
1124}
1125
8990cac6
PT
1126/*
1127 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1128 * unfortunately, that's not true in practice because of early VIA
1129 * chips and (more importantly) broken virtualizers that are not easy
1130 * to detect. In the latter case it doesn't even *fail* reliably, so
1131 * probing for it doesn't even work. Disable it completely on 32-bit
1132 * unless we can find a reliable way to detect all the broken cases.
1133 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
1134 */
9b3661cd 1135static void detect_nopl(void)
8990cac6
PT
1136{
1137#ifdef CONFIG_X86_32
9b3661cd 1138 setup_clear_cpu_cap(X86_FEATURE_NOPL);
8990cac6 1139#else
9b3661cd 1140 setup_force_cpu_cap(X86_FEATURE_NOPL);
8990cac6
PT
1141#endif
1142}
1143
34048c9e
PC
1144/*
1145 * Do minimum CPU detection early.
1146 * Fields really needed: vendor, cpuid_level, family, model, mask,
1147 * cache alignment.
1148 * The others are not touched to avoid unwanted side effects.
1149 *
a1652bb8
JD
1150 * WARNING: this function is only called on the boot CPU. Don't add code
1151 * here that is supposed to run on all CPUs.
34048c9e 1152 */
3da99c97 1153static void __init early_identify_cpu(struct cpuinfo_x86 *c)
d7cd5611 1154{
6627d242
YL
1155#ifdef CONFIG_X86_64
1156 c->x86_clflush_size = 64;
13c6c532
JB
1157 c->x86_phys_bits = 36;
1158 c->x86_virt_bits = 48;
6627d242 1159#else
d4387bd3 1160 c->x86_clflush_size = 32;
13c6c532
JB
1161 c->x86_phys_bits = 32;
1162 c->x86_virt_bits = 32;
6627d242 1163#endif
0a488a53 1164 c->x86_cache_alignment = c->x86_clflush_size;
d7cd5611 1165
0e96f31e 1166 memset(&c->x86_capability, 0, sizeof(c->x86_capability));
0a488a53 1167 c->extended_cpuid_level = 0;
d7cd5611 1168
2893cc8f
MW
1169 if (!have_cpuid_p())
1170 identify_cpu_without_cpuid(c);
1171
aef93c8b 1172 /* cyrix could have cpuid enabled via c_identify()*/
05fb3c19
AL
1173 if (have_cpuid_p()) {
1174 cpu_detect(c);
1175 get_cpu_vendor(c);
1176 get_cpu_cap(c);
d94a155c 1177 get_cpu_address_sizes(c);
78d1b296 1178 setup_force_cpu_cap(X86_FEATURE_CPUID);
d7cd5611 1179
05fb3c19
AL
1180 if (this_cpu->c_early_init)
1181 this_cpu->c_early_init(c);
12cf105c 1182
05fb3c19
AL
1183 c->cpu_index = 0;
1184 filter_cpuid_features(c, false);
093af8d7 1185
05fb3c19
AL
1186 if (this_cpu->c_bsp_init)
1187 this_cpu->c_bsp_init(c);
78d1b296 1188 } else {
78d1b296 1189 setup_clear_cpu_cap(X86_FEATURE_CPUID);
05fb3c19 1190 }
c3b83598
BP
1191
1192 setup_force_cpu_cap(X86_FEATURE_ALWAYS);
a89f040f 1193
4a28bfe3 1194 cpu_set_bug_bits(c);
99c6fa25 1195
db52ef74 1196 fpu__init_system(c);
b8b7abae
AL
1197
1198#ifdef CONFIG_X86_32
1199 /*
1200 * Regardless of whether PCID is enumerated, the SDM says
1201 * that it can't be enabled in 32-bit mode.
1202 */
1203 setup_clear_cpu_cap(X86_FEATURE_PCID);
1204#endif
372fddf7
KS
1205
1206 /*
1207 * Later in the boot process pgtable_l5_enabled() relies on
1208 * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not
1209 * enabled by this point we need to clear the feature bit to avoid
1210 * false-positives at the later stage.
1211 *
1212 * pgtable_l5_enabled() can be false here for several reasons:
1213 * - 5-level paging is disabled compile-time;
1214 * - it's 32-bit kernel;
1215 * - machine doesn't support 5-level paging;
1216 * - user specified 'no5lvl' in kernel command line.
1217 */
1218 if (!pgtable_l5_enabled())
1219 setup_clear_cpu_cap(X86_FEATURE_LA57);
8990cac6 1220
9b3661cd 1221 detect_nopl();
d7cd5611
RR
1222}
1223
9d31d35b
YL
1224void __init early_cpu_init(void)
1225{
02dde8b4 1226 const struct cpu_dev *const *cdev;
10a434fc
YL
1227 int count = 0;
1228
ac23f253 1229#ifdef CONFIG_PROCESSOR_SELECT
1b74dde7 1230 pr_info("KERNEL supported cpus:\n");
31c997ca
IM
1231#endif
1232
10a434fc 1233 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
02dde8b4 1234 const struct cpu_dev *cpudev = *cdev;
9d31d35b 1235
10a434fc
YL
1236 if (count >= X86_VENDOR_NUM)
1237 break;
1238 cpu_devs[count] = cpudev;
1239 count++;
1240
ac23f253 1241#ifdef CONFIG_PROCESSOR_SELECT
31c997ca
IM
1242 {
1243 unsigned int j;
1244
1245 for (j = 0; j < 2; j++) {
1246 if (!cpudev->c_ident[j])
1247 continue;
1b74dde7 1248 pr_info(" %s %s\n", cpudev->c_vendor,
31c997ca
IM
1249 cpudev->c_ident[j]);
1250 }
10a434fc 1251 }
0388423d 1252#endif
10a434fc 1253 }
9d31d35b 1254 early_identify_cpu(&boot_cpu_data);
d7cd5611 1255}
093af8d7 1256
7a5d6704
AL
1257static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
1258{
1259#ifdef CONFIG_X86_64
58a5aac5 1260 /*
7a5d6704
AL
1261 * Empirically, writing zero to a segment selector on AMD does
1262 * not clear the base, whereas writing zero to a segment
1263 * selector on Intel does clear the base. Intel's behavior
1264 * allows slightly faster context switches in the common case
1265 * where GS is unused by the prev and next threads.
58a5aac5 1266 *
7a5d6704
AL
1267 * Since neither vendor documents this anywhere that I can see,
1268 * detect it directly instead of hardcoding the choice by
1269 * vendor.
1270 *
1271 * I've designated AMD's behavior as the "bug" because it's
1272 * counterintuitive and less friendly.
58a5aac5 1273 */
7a5d6704
AL
1274
1275 unsigned long old_base, tmp;
1276 rdmsrl(MSR_FS_BASE, old_base);
1277 wrmsrl(MSR_FS_BASE, 1);
1278 loadsegment(fs, 0);
1279 rdmsrl(MSR_FS_BASE, tmp);
1280 if (tmp != 0)
1281 set_cpu_bug(c, X86_BUG_NULL_SEG);
1282 wrmsrl(MSR_FS_BASE, old_base);
366d4a43 1283#endif
d7cd5611
RR
1284}
1285
148f9bb8 1286static void generic_identify(struct cpuinfo_x86 *c)
1da177e4 1287{
aef93c8b 1288 c->extended_cpuid_level = 0;
1da177e4 1289
3da99c97 1290 if (!have_cpuid_p())
aef93c8b 1291 identify_cpu_without_cpuid(c);
1d67953f 1292
aef93c8b 1293 /* cyrix could have cpuid enabled via c_identify()*/
a9853dd6 1294 if (!have_cpuid_p())
aef93c8b 1295 return;
1da177e4 1296
3da99c97 1297 cpu_detect(c);
1da177e4 1298
3da99c97 1299 get_cpu_vendor(c);
1da177e4 1300
3da99c97 1301 get_cpu_cap(c);
1da177e4 1302
d94a155c
KS
1303 get_cpu_address_sizes(c);
1304
3da99c97
YL
1305 if (c->cpuid_level >= 0x00000001) {
1306 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
b89d3b3e 1307#ifdef CONFIG_X86_32
c8e56d20 1308# ifdef CONFIG_SMP
cb8cc442 1309 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
b89d3b3e 1310# else
3da99c97 1311 c->apicid = c->initial_apicid;
b89d3b3e
YL
1312# endif
1313#endif
b89d3b3e 1314 c->phys_proc_id = c->initial_apicid;
3da99c97 1315 }
1da177e4 1316
1b05d60d 1317 get_model_name(c); /* Default name */
1da177e4 1318
7a5d6704 1319 detect_null_seg_behavior(c);
0230bb03
AL
1320
1321 /*
1322 * ESPFIX is a strange bug. All real CPUs have it. Paravirt
1323 * systems that run Linux at CPL > 0 may or may not have the
1324 * issue, but, even if they have the issue, there's absolutely
1325 * nothing we can do about it because we can't use the real IRET
1326 * instruction.
1327 *
1328 * NB: For the time being, only 32-bit kernels support
1329 * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
1330 * whether to apply espfix using paravirt hooks. If any
1331 * non-paravirt system ever shows up that does *not* have the
1332 * ESPFIX issue, we can change this.
1333 */
1334#ifdef CONFIG_X86_32
9bad5658 1335# ifdef CONFIG_PARAVIRT_XXL
0230bb03
AL
1336 do {
1337 extern void native_iret(void);
5c83511b 1338 if (pv_ops.cpu.iret == native_iret)
0230bb03
AL
1339 set_cpu_bug(c, X86_BUG_ESPFIX);
1340 } while (0);
1341# else
1342 set_cpu_bug(c, X86_BUG_ESPFIX);
1343# endif
1344#endif
1da177e4 1345}
1da177e4 1346
cbc82b17
PWJ
1347static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1348{
1349 /*
1350 * The heavy lifting of max_rmid and cache_occ_scale are handled
1351 * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
1352 * in case CQM bits really aren't there in this CPU.
1353 */
1354 if (c != &boot_cpu_data) {
1355 boot_cpu_data.x86_cache_max_rmid =
1356 min(boot_cpu_data.x86_cache_max_rmid,
1357 c->x86_cache_max_rmid);
1358 }
1359}
1360
d49597fd 1361/*
9d85eb91
TG
1362 * Validate that ACPI/mptables have the same information about the
1363 * effective APIC id and update the package map.
d49597fd 1364 */
9d85eb91 1365static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
d49597fd
TG
1366{
1367#ifdef CONFIG_SMP
9d85eb91 1368 unsigned int apicid, cpu = smp_processor_id();
d49597fd
TG
1369
1370 apicid = apic->cpu_present_to_apicid(cpu);
d49597fd 1371
9d85eb91
TG
1372 if (apicid != c->apicid) {
1373 pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
d49597fd 1374 cpu, apicid, c->initial_apicid);
d49597fd 1375 }
9d85eb91 1376 BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
212bf4fd 1377 BUG_ON(topology_update_die_map(c->cpu_die_id, cpu));
d49597fd
TG
1378#else
1379 c->logical_proc_id = 0;
1380#endif
1381}
1382
1da177e4
LT
1383/*
1384 * This does the hard work of actually picking apart the CPU stuff...
1385 */
148f9bb8 1386static void identify_cpu(struct cpuinfo_x86 *c)
1da177e4
LT
1387{
1388 int i;
1389
1390 c->loops_per_jiffy = loops_per_jiffy;
24dbc600 1391 c->x86_cache_size = 0;
1da177e4 1392 c->x86_vendor = X86_VENDOR_UNKNOWN;
b399151c 1393 c->x86_model = c->x86_stepping = 0; /* So far unknown... */
1da177e4
LT
1394 c->x86_vendor_id[0] = '\0'; /* Unset */
1395 c->x86_model_id[0] = '\0'; /* Unset */
94605eff 1396 c->x86_max_cores = 1;
102bbe3a 1397 c->x86_coreid_bits = 0;
79a8b9aa 1398 c->cu_id = 0xff;
11fdd252 1399#ifdef CONFIG_X86_64
102bbe3a 1400 c->x86_clflush_size = 64;
13c6c532
JB
1401 c->x86_phys_bits = 36;
1402 c->x86_virt_bits = 48;
102bbe3a
YL
1403#else
1404 c->cpuid_level = -1; /* CPUID not detected */
770d132f 1405 c->x86_clflush_size = 32;
13c6c532
JB
1406 c->x86_phys_bits = 32;
1407 c->x86_virt_bits = 32;
102bbe3a
YL
1408#endif
1409 c->x86_cache_alignment = c->x86_clflush_size;
0e96f31e 1410 memset(&c->x86_capability, 0, sizeof(c->x86_capability));
1da177e4 1411
1da177e4
LT
1412 generic_identify(c);
1413
3898534d 1414 if (this_cpu->c_identify)
1da177e4
LT
1415 this_cpu->c_identify(c);
1416
6a6256f9 1417 /* Clear/Set all flags overridden by options, after probe */
8bf1ebca 1418 apply_forced_caps(c);
2759c328 1419
102bbe3a 1420#ifdef CONFIG_X86_64
cb8cc442 1421 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
102bbe3a
YL
1422#endif
1423
1da177e4
LT
1424 /*
1425 * Vendor-specific initialization. In this section we
1426 * canonicalize the feature flags, meaning if there are
1427 * features a certain CPU supports which CPUID doesn't
1428 * tell us, CPUID claiming incorrect flags, or other bugs,
1429 * we handle them here.
1430 *
1431 * At the end of this section, c->x86_capability better
1432 * indicate the features this CPU genuinely supports!
1433 */
1434 if (this_cpu->c_init)
1435 this_cpu->c_init(c);
1436
1437 /* Disable the PN if appropriate */
1438 squash_the_stupid_serial_number(c);
1439
aa35f896 1440 /* Set up SMEP/SMAP/UMIP */
b2cc2a07
PA
1441 setup_smep(c);
1442 setup_smap(c);
aa35f896 1443 setup_umip(c);
b2cc2a07 1444
1da177e4 1445 /*
0f3fa48a
IM
1446 * The vendor-specific functions might have changed features.
1447 * Now we do "generic changes."
1da177e4
LT
1448 */
1449
b38b0665
PA
1450 /* Filter out anything that depends on CPUID levels we don't have */
1451 filter_cpuid_features(c, true);
1452
1da177e4 1453 /* If the model name is still unset, do table lookup. */
34048c9e 1454 if (!c->x86_model_id[0]) {
02dde8b4 1455 const char *p;
1da177e4 1456 p = table_lookup_model(c);
34048c9e 1457 if (p)
1da177e4
LT
1458 strcpy(c->x86_model_id, p);
1459 else
1460 /* Last resort... */
1461 sprintf(c->x86_model_id, "%02x/%02x",
54a20f8c 1462 c->x86, c->x86_model);
1da177e4
LT
1463 }
1464
102bbe3a
YL
1465#ifdef CONFIG_X86_64
1466 detect_ht(c);
1467#endif
1468
49d859d7 1469 x86_init_rdrand(c);
cbc82b17 1470 x86_init_cache_qos(c);
06976945 1471 setup_pku(c);
3e0c3737
YL
1472
1473 /*
6a6256f9 1474 * Clear/Set all flags overridden by options, need do it
3e0c3737
YL
1475 * before following smp all cpus cap AND.
1476 */
8bf1ebca 1477 apply_forced_caps(c);
3e0c3737 1478
1da177e4
LT
1479 /*
1480 * On SMP, boot_cpu_data holds the common feature set between
1481 * all CPUs; so make sure that we indicate which features are
1482 * common between the CPUs. The first time this routine gets
1483 * executed, c == &boot_cpu_data.
1484 */
34048c9e 1485 if (c != &boot_cpu_data) {
1da177e4 1486 /* AND the already accumulated flags with these */
9d31d35b 1487 for (i = 0; i < NCAPINTS; i++)
1da177e4 1488 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
65fc985b
BP
1489
1490 /* OR, i.e. replicate the bug flags */
1491 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
1492 c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
1da177e4
LT
1493 }
1494
1495 /* Init Machine Check Exception if available. */
5e09954a 1496 mcheck_cpu_init(c);
30d432df
AK
1497
1498 select_idle_routine(c);
102bbe3a 1499
de2d9445 1500#ifdef CONFIG_NUMA
102bbe3a
YL
1501 numa_add_cpu(smp_processor_id());
1502#endif
a6c4e076 1503}
31ab269a 1504
8b6c0ab1
IM
1505/*
1506 * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
1507 * on 32-bit kernels:
1508 */
cfda7bb9
AL
1509#ifdef CONFIG_X86_32
1510void enable_sep_cpu(void)
1511{
8b6c0ab1
IM
1512 struct tss_struct *tss;
1513 int cpu;
cfda7bb9 1514
b3edfda4
BP
1515 if (!boot_cpu_has(X86_FEATURE_SEP))
1516 return;
1517
8b6c0ab1 1518 cpu = get_cpu();
c482feef 1519 tss = &per_cpu(cpu_tss_rw, cpu);
8b6c0ab1 1520
8b6c0ab1 1521 /*
cf9328cc
AL
1522 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1523 * see the big comment in struct x86_hw_tss's definition.
8b6c0ab1 1524 */
cfda7bb9
AL
1525
1526 tss->x86_tss.ss1 = __KERNEL_CS;
8b6c0ab1 1527 wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
4fe2d8b1 1528 wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0);
4c8cd0c5 1529 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
8b6c0ab1 1530
cfda7bb9
AL
1531 put_cpu();
1532}
e04d645f
GC
1533#endif
1534
a6c4e076
JF
1535void __init identify_boot_cpu(void)
1536{
1537 identify_cpu(&boot_cpu_data);
102bbe3a 1538#ifdef CONFIG_X86_32
a6c4e076 1539 sysenter_setup();
6fe940d6 1540 enable_sep_cpu();
102bbe3a 1541#endif
5b556332 1542 cpu_detect_tlb(&boot_cpu_data);
873d50d5 1543 setup_cr_pinning();
a6c4e076 1544}
3b520b23 1545
148f9bb8 1546void identify_secondary_cpu(struct cpuinfo_x86 *c)
a6c4e076
JF
1547{
1548 BUG_ON(c == &boot_cpu_data);
1549 identify_cpu(c);
102bbe3a 1550#ifdef CONFIG_X86_32
a6c4e076 1551 enable_sep_cpu();
102bbe3a 1552#endif
a6c4e076 1553 mtrr_ap_init();
9d85eb91 1554 validate_apic_and_package_id(c);
77243971 1555 x86_spec_ctrl_setup_ap();
1da177e4
LT
1556}
1557
191679fd
AK
1558static __init int setup_noclflush(char *arg)
1559{
840d2830 1560 setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
da4aaa7d 1561 setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
191679fd
AK
1562 return 1;
1563}
1564__setup("noclflush", setup_noclflush);
1565
148f9bb8 1566void print_cpu_info(struct cpuinfo_x86 *c)
1da177e4 1567{
02dde8b4 1568 const char *vendor = NULL;
1da177e4 1569
0f3fa48a 1570 if (c->x86_vendor < X86_VENDOR_NUM) {
1da177e4 1571 vendor = this_cpu->c_vendor;
0f3fa48a
IM
1572 } else {
1573 if (c->cpuid_level >= 0)
1574 vendor = c->x86_vendor_id;
1575 }
1da177e4 1576
bd32a8cf 1577 if (vendor && !strstr(c->x86_model_id, vendor))
1b74dde7 1578 pr_cont("%s ", vendor);
1da177e4 1579
9d31d35b 1580 if (c->x86_model_id[0])
1b74dde7 1581 pr_cont("%s", c->x86_model_id);
1da177e4 1582 else
1b74dde7 1583 pr_cont("%d86", c->x86);
1da177e4 1584
1b74dde7 1585 pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
924e101a 1586
b399151c
JZ
1587 if (c->x86_stepping || c->cpuid_level >= 0)
1588 pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
1da177e4 1589 else
1b74dde7 1590 pr_cont(")\n");
1da177e4
LT
1591}
1592
0c2a3913
AK
1593/*
1594 * clearcpuid= was already parsed in fpu__init_parse_early_param.
1595 * But we need to keep a dummy __setup around otherwise it would
1596 * show up as an environment variable for init.
1597 */
1598static __init int setup_clearcpuid(char *arg)
ac72e788 1599{
ac72e788
AK
1600 return 1;
1601}
0c2a3913 1602__setup("clearcpuid=", setup_clearcpuid);
ac72e788 1603
d5494d4f 1604#ifdef CONFIG_X86_64
e6401c13
AL
1605DEFINE_PER_CPU_FIRST(struct fixed_percpu_data,
1606 fixed_percpu_data) __aligned(PAGE_SIZE) __visible;
1607EXPORT_PER_CPU_SYMBOL_GPL(fixed_percpu_data);
0f3fa48a 1608
bdf977b3 1609/*
a7fcf28d
AL
1610 * The following percpu variables are hot. Align current_task to
1611 * cacheline size such that they fall in the same cacheline.
bdf977b3
TH
1612 */
1613DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1614 &init_task;
1615EXPORT_PER_CPU_SYMBOL(current_task);
d5494d4f 1616
e6401c13 1617DEFINE_PER_CPU(struct irq_stack *, hardirq_stack_ptr);
277d5b40 1618DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
d5494d4f 1619
c2daa3be
PZ
1620DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1621EXPORT_PER_CPU_SYMBOL(__preempt_count);
1622
d5494d4f
YL
1623/* May not be marked __init: used by software suspend */
1624void syscall_init(void)
1da177e4 1625{
31ac34ca 1626 wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
bf904d27 1627 wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
d56fe4bf
IM
1628
1629#ifdef CONFIG_IA32_EMULATION
47edb651 1630 wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
a76c7f46 1631 /*
487d1edb
DV
1632 * This only works on Intel CPUs.
1633 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1634 * This does not cause SYSENTER to jump to the wrong location, because
1635 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
a76c7f46
DV
1636 */
1637 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
8e6b65a1 1638 wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
1639 (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1));
4c8cd0c5 1640 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
d56fe4bf 1641#else
47edb651 1642 wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
6b51311c 1643 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
d56fe4bf
IM
1644 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1645 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
d5494d4f 1646#endif
03ae5768 1647
d5494d4f
YL
1648 /* Flags to clear on syscall */
1649 wrmsrl(MSR_SYSCALL_MASK,
63bcff2a 1650 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
8c7aa698 1651 X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1da177e4 1652}
62111195 1653
42181186 1654DEFINE_PER_CPU(int, debug_stack_usage);
629f4f9d 1655DEFINE_PER_CPU(u32, debug_idt_ctr);
f8988175 1656
228bdaa9
SR
1657void debug_stack_set_zero(void)
1658{
629f4f9d
SA
1659 this_cpu_inc(debug_idt_ctr);
1660 load_current_idt();
228bdaa9 1661}
0f46efeb 1662NOKPROBE_SYMBOL(debug_stack_set_zero);
228bdaa9
SR
1663
1664void debug_stack_reset(void)
1665{
629f4f9d 1666 if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
f8988175 1667 return;
629f4f9d
SA
1668 if (this_cpu_dec_return(debug_idt_ctr) == 0)
1669 load_current_idt();
228bdaa9 1670}
0f46efeb 1671NOKPROBE_SYMBOL(debug_stack_reset);
228bdaa9 1672
0f3fa48a 1673#else /* CONFIG_X86_64 */
d5494d4f 1674
bdf977b3
TH
1675DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1676EXPORT_PER_CPU_SYMBOL(current_task);
c2daa3be
PZ
1677DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1678EXPORT_PER_CPU_SYMBOL(__preempt_count);
bdf977b3 1679
a7fcf28d
AL
1680/*
1681 * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1682 * the top of the kernel stack. Use an extra percpu variable to track the
1683 * top of the kernel stack directly.
1684 */
1685DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1686 (unsigned long)&init_thread_union + THREAD_SIZE;
1687EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1688
050e9baa 1689#ifdef CONFIG_STACKPROTECTOR
53f82452 1690DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
60a5317f 1691#endif
d5494d4f 1692
0f3fa48a 1693#endif /* CONFIG_X86_64 */
c5413fbe 1694
9766cdbc
JSR
1695/*
1696 * Clear all 6 debug registers:
1697 */
1698static void clear_all_debug_regs(void)
1699{
1700 int i;
1701
1702 for (i = 0; i < 8; i++) {
1703 /* Ignore db4, db5 */
1704 if ((i == 4) || (i == 5))
1705 continue;
1706
1707 set_debugreg(0, i);
1708 }
1709}
c5413fbe 1710
0bb9fef9
JW
1711#ifdef CONFIG_KGDB
1712/*
1713 * Restore debug regs if using kgdbwait and you have a kernel debugger
1714 * connection established.
1715 */
1716static void dbg_restore_debug_regs(void)
1717{
1718 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1719 arch_kgdb_ops.correct_hw_break();
1720}
1721#else /* ! CONFIG_KGDB */
1722#define dbg_restore_debug_regs()
1723#endif /* ! CONFIG_KGDB */
1724
ce4b1b16
IM
1725static void wait_for_master_cpu(int cpu)
1726{
1727#ifdef CONFIG_SMP
1728 /*
1729 * wait for ACK from master CPU before continuing
1730 * with AP initialization
1731 */
1732 WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1733 while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1734 cpu_relax();
1735#endif
1736}
1737
b2e2ba57
CB
1738#ifdef CONFIG_X86_64
1739static void setup_getcpu(int cpu)
1740{
22245bdf 1741 unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
b2e2ba57
CB
1742 struct desc_struct d = { };
1743
67e87d43 1744 if (boot_cpu_has(X86_FEATURE_RDTSCP))
b2e2ba57
CB
1745 write_rdtscp_aux(cpudata);
1746
1747 /* Store CPU and node number in limit. */
1748 d.limit0 = cpudata;
1749 d.limit1 = cpudata >> 16;
1750
1751 d.type = 5; /* RO data, expand down, accessed */
1752 d.dpl = 3; /* Visible to user code */
1753 d.s = 1; /* Not a system segment */
1754 d.p = 1; /* Present */
1755 d.d = 1; /* 32-bit */
1756
22245bdf 1757 write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S);
b2e2ba57
CB
1758}
1759#endif
1760
d2cbcc49
RR
1761/*
1762 * cpu_init() initializes state that is per-CPU. Some data is already
1763 * initialized (naturally) in the bootstrap process, such as the GDT
1764 * and IDT. We reload them nevertheless, this function acts as a
1765 * 'CPU state barrier', nothing should get across.
1766 */
1ba76586 1767#ifdef CONFIG_X86_64
0f3fa48a 1768
148f9bb8 1769void cpu_init(void)
1ba76586 1770{
f6ef7322 1771 int cpu = raw_smp_processor_id();
1ba76586 1772 struct task_struct *me;
0f3fa48a 1773 struct tss_struct *t;
1ba76586
YL
1774 int i;
1775
ce4b1b16
IM
1776 wait_for_master_cpu(cpu);
1777
777284b6
BP
1778 if (cpu)
1779 load_ucode_ap();
e6ebf5de 1780
c482feef 1781 t = &per_cpu(cpu_tss_rw, cpu);
0f3fa48a 1782
e7a22c1e 1783#ifdef CONFIG_NUMA
27fd185f 1784 if (this_cpu_read(numa_node) == 0 &&
e534c7c5
LS
1785 early_cpu_to_node(cpu) != NUMA_NO_NODE)
1786 set_numa_node(early_cpu_to_node(cpu));
e7a22c1e 1787#endif
b2e2ba57 1788 setup_getcpu(cpu);
1ba76586
YL
1789
1790 me = current;
1791
2eaad1fd 1792 pr_debug("Initializing CPU#%d\n", cpu);
1ba76586 1793
375074cc 1794 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1ba76586
YL
1795
1796 /*
1797 * Initialize the per-CPU GDT with the boot GDT,
1798 * and set up the GDT descriptor:
1799 */
1800
552be871 1801 switch_to_new_gdt(cpu);
2697fbd5
BG
1802 loadsegment(fs, 0);
1803
cf910e83 1804 load_current_idt();
1ba76586
YL
1805
1806 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1807 syscall_init();
1808
1809 wrmsrl(MSR_FS_BASE, 0);
1810 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1811 barrier();
1812
4763ed4d 1813 x86_configure_nx();
659006bf 1814 x2apic_setup();
1ba76586
YL
1815
1816 /*
1817 * set up and load the per-CPU TSS
1818 */
f6ef7322 1819 if (!t->x86_tss.ist[0]) {
32074269
TG
1820 t->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
1821 t->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
1822 t->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
1823 t->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
1ba76586
YL
1824 }
1825
7fb983b4 1826 t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
0f3fa48a 1827
1ba76586
YL
1828 /*
1829 * <= is required because the CPU will access up to
1830 * 8 bits beyond the end of the IO permission bitmap.
1831 */
1832 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1833 t->io_bitmap[i] = ~0UL;
1834
f1f10076 1835 mmgrab(&init_mm);
1ba76586 1836 me->active_mm = &init_mm;
8c5dfd25 1837 BUG_ON(me->mm);
72c0098d 1838 initialize_tlbstate_and_flush();
1ba76586
YL
1839 enter_lazy_tlb(&init_mm, me);
1840
20bb8344 1841 /*
7f2590a1
AL
1842 * Initialize the TSS. sp0 points to the entry trampoline stack
1843 * regardless of what task is running.
20bb8344 1844 */
72f5e08d 1845 set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
1ba76586 1846 load_TR_desc();
4fe2d8b1 1847 load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
20bb8344 1848
37868fe1 1849 load_mm_ldt(&init_mm);
1ba76586 1850
0bb9fef9
JW
1851 clear_all_debug_regs();
1852 dbg_restore_debug_regs();
1ba76586 1853
21c4cd10 1854 fpu__init_cpu();
1ba76586 1855
1ba76586
YL
1856 if (is_uv_system())
1857 uv_cpu_init();
69218e47 1858
69218e47 1859 load_fixmap_gdt(cpu);
1ba76586
YL
1860}
1861
1862#else
1863
148f9bb8 1864void cpu_init(void)
9ee79a3d 1865{
d2cbcc49
RR
1866 int cpu = smp_processor_id();
1867 struct task_struct *curr = current;
c482feef 1868 struct tss_struct *t = &per_cpu(cpu_tss_rw, cpu);
62111195 1869
ce4b1b16 1870 wait_for_master_cpu(cpu);
5b2bdbc8 1871
ce4b1b16 1872 show_ucode_info_early();
62111195 1873
1b74dde7 1874 pr_info("Initializing CPU#%d\n", cpu);
62111195 1875
362f924b 1876 if (cpu_feature_enabled(X86_FEATURE_VME) ||
59e21e3d 1877 boot_cpu_has(X86_FEATURE_TSC) ||
362f924b 1878 boot_cpu_has(X86_FEATURE_DE))
375074cc 1879 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
62111195 1880
cf910e83 1881 load_current_idt();
552be871 1882 switch_to_new_gdt(cpu);
1da177e4 1883
1da177e4
LT
1884 /*
1885 * Set up and load the per-CPU TSS and LDT
1886 */
f1f10076 1887 mmgrab(&init_mm);
62111195 1888 curr->active_mm = &init_mm;
8c5dfd25 1889 BUG_ON(curr->mm);
72c0098d 1890 initialize_tlbstate_and_flush();
62111195 1891 enter_lazy_tlb(&init_mm, curr);
1da177e4 1892
20bb8344 1893 /*
45d7b255
JR
1894 * Initialize the TSS. sp0 points to the entry trampoline stack
1895 * regardless of what task is running.
20bb8344 1896 */
72f5e08d 1897 set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
1da177e4 1898 load_TR_desc();
45d7b255 1899 load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
20bb8344 1900
37868fe1 1901 load_mm_ldt(&init_mm);
1da177e4 1902
7fb983b4 1903 t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
f9a196b8 1904
22c4e308 1905#ifdef CONFIG_DOUBLEFAULT
1da177e4
LT
1906 /* Set up doublefault TSS pointer in the GDT */
1907 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
22c4e308 1908#endif
1da177e4 1909
9766cdbc 1910 clear_all_debug_regs();
0bb9fef9 1911 dbg_restore_debug_regs();
1da177e4 1912
21c4cd10 1913 fpu__init_cpu();
69218e47 1914
69218e47 1915 load_fixmap_gdt(cpu);
1da177e4 1916}
1ba76586 1917#endif
5700f743 1918
1008c52c
BP
1919/*
1920 * The microcode loader calls this upon late microcode load to recheck features,
1921 * only when microcode has been updated. Caller holds microcode_mutex and CPU
1922 * hotplug lock.
1923 */
1924void microcode_check(void)
1925{
42ca8082
BP
1926 struct cpuinfo_x86 info;
1927
1008c52c 1928 perf_check_microcode();
42ca8082
BP
1929
1930 /* Reload CPUID max function as it might've changed. */
1931 info.cpuid_level = cpuid_eax(0);
1932
1933 /*
1934 * Copy all capability leafs to pick up the synthetic ones so that
1935 * memcmp() below doesn't fail on that. The ones coming from CPUID will
1936 * get overwritten in get_cpu_cap().
1937 */
1938 memcpy(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability));
1939
1940 get_cpu_cap(&info);
1941
1942 if (!memcmp(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability)))
1943 return;
1944
1945 pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
1946 pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
1008c52c 1947}