blob: 8cdca1223b0f25e88193814fae315693b99fb5a3 [file] [log] [blame]
Kirill A. Shutemov2458e532018-06-23 01:08:41 +03001/* cpu_feature_enabled() cannot be used this early */
2#define USE_EARLY_PGTABLE_L5
3
Mike Rapoport57c8a662018-10-30 15:09:49 -07004#include <linux/memblock.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05305#include <linux/linkage.h>
Yinghai Luf0fc4af2008-09-04 20:09:00 -07006#include <linux/bitops.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05307#include <linux/kernel.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -04008#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/percpu.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053010#include <linux/string.h>
Borislav Petkovee098e12015-06-01 12:06:57 +020011#include <linux/ctype.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053012#include <linux/delay.h>
Ingo Molnar68e21be2017-02-01 19:08:20 +010013#include <linux/sched/mm.h>
Ingo Molnare6017572017-02-01 16:36:40 +010014#include <linux/sched/clock.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010015#include <linux/sched/task.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053016#include <linux/init.h>
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +090017#include <linux/kprobes.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053018#include <linux/kgdb.h>
19#include <linux/smp.h>
20#include <linux/io.h>
Laura Abbottb51ef522015-07-20 14:47:58 -070021#include <linux/syscore_ops.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053022
23#include <asm/stackprotector.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020024#include <asm/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/mmu_context.h>
H. Peter Anvin49d859d2011-07-31 14:02:19 -070026#include <asm/archrandom.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053027#include <asm/hypervisor.h>
28#include <asm/processor.h>
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070029#include <asm/tlbflush.h>
Paul Gortmakerf649e932012-01-20 16:24:09 -050030#include <asm/debugreg.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053031#include <asm/sections.h>
Andy Lutomirskif40c3302014-05-05 12:19:36 -070032#include <asm/vsyscall.h>
Alan Cox8bdbd962009-07-04 00:35:45 +010033#include <linux/topology.h>
34#include <linux/cpumask.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053035#include <asm/pgtable.h>
Arun Sharma600634972011-07-26 16:09:06 -070036#include <linux/atomic.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053037#include <asm/proto.h>
38#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/apic.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053040#include <asm/desc.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020041#include <asm/fpu/internal.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053042#include <asm/mtrr.h>
Grzegorz Andrejczuk0274f952017-01-20 14:22:34 +010043#include <asm/hwcap2.h>
Alan Cox8bdbd962009-07-04 00:35:45 +010044#include <linux/numa.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053045#include <asm/asm.h>
Dave Hansen0f6ff2b2016-05-12 15:04:00 -070046#include <asm/bugs.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053047#include <asm/cpu.h>
48#include <asm/mce.h>
49#include <asm/msr.h>
50#include <asm/pat.h>
Fenghua Yud288e1c2012-12-20 23:44:23 -080051#include <asm/microcode.h>
52#include <asm/microcode_intel.h>
David Woodhousefec94342018-01-25 16:14:13 +000053#include <asm/intel-family.h>
54#include <asm/cpu_device_id.h>
Ingo Molnare641f5f2009-02-17 14:02:01 +010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#ifdef CONFIG_X86_LOCAL_APIC
Tejun Heobdbcdd42009-01-21 17:26:06 +090057#include <asm/uv/uv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
60#include "cpu.h"
61
Grzegorz Andrejczuk0274f952017-01-20 14:22:34 +010062u32 elf_hwcap2 __read_mostly;
63
Mike Travisc2d1cec2009-01-04 05:18:03 -080064/* all of these masks are initialized in setup_cpu_local_masks() */
Mike Travisc2d1cec2009-01-04 05:18:03 -080065cpumask_var_t cpu_initialized_mask;
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053066cpumask_var_t cpu_callout_mask;
67cpumask_var_t cpu_callin_mask;
Mike Travisc2d1cec2009-01-04 05:18:03 -080068
69/* representing cpus for which sibling maps can be computed */
70cpumask_var_t cpu_sibling_setup_mask;
71
Borislav Petkovf8b64d02018-04-27 16:34:34 -050072/* Number of siblings per CPU package */
73int smp_num_siblings = 1;
74EXPORT_SYMBOL(smp_num_siblings);
75
76/* Last level cache ID of each logical CPU */
77DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
78
Brian Gerst2f2f52b2009-01-27 12:56:47 +090079/* correctly size the local cpu masks */
Ingo Molnar4369f1f2009-01-27 12:03:24 +010080void __init setup_cpu_local_masks(void)
Brian Gerst2f2f52b2009-01-27 12:56:47 +090081{
82 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
83 alloc_bootmem_cpumask_var(&cpu_callin_mask);
84 alloc_bootmem_cpumask_var(&cpu_callout_mask);
85 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
86}
87
Paul Gortmaker148f9bb2013-06-18 18:23:59 -040088static void default_init(struct cpuinfo_x86 *c)
Ondrej Zarye8055132009-08-11 20:00:11 +020089{
90#ifdef CONFIG_X86_64
Borislav Petkov27c13ec2009-11-21 14:01:45 +010091 cpu_detect_cache_sizes(c);
Ondrej Zarye8055132009-08-11 20:00:11 +020092#else
93 /* Not much we can do here... */
94 /* Check if at least it has cpuid */
95 if (c->cpuid_level == -1) {
96 /* No cpuid. It must be an ancient CPU */
97 if (c->x86 == 4)
98 strcpy(c->x86_model_id, "486");
99 else if (c->x86 == 3)
100 strcpy(c->x86_model_id, "386");
101 }
102#endif
103}
104
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400105static const struct cpu_dev default_cpu = {
Ondrej Zarye8055132009-08-11 20:00:11 +0200106 .c_init = default_init,
107 .c_vendor = "Unknown",
108 .c_x86_vendor = X86_VENDOR_UNKNOWN,
109};
110
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400111static const struct cpu_dev *this_cpu = &default_cpu;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200112
Brian Gerst06deef82009-01-21 17:26:05 +0900113DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
Yinghai Lu950ad7f2008-09-04 20:09:01 -0700114#ifdef CONFIG_X86_64
Brian Gerst06deef82009-01-21 17:26:05 +0900115 /*
116 * We need valid kernel segments for data and code in long mode too
117 * IRET will check the segment types kkeil 2000/10/28
118 * Also sysret mandates a special GDT layout
119 *
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530120 * TLS descriptors are currently at a different place compared to i386.
Brian Gerst06deef82009-01-21 17:26:05 +0900121 * Hopefully nobody expects them at a fixed place (Wine?)
122 */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900123 [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
124 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
125 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
126 [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
127 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
128 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
Yinghai Lu950ad7f2008-09-04 20:09:01 -0700129#else
Akinobu Mita1e5de182009-07-19 00:12:20 +0900130 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
131 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
132 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
133 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200134 /*
135 * Segments used for calling PnP BIOS have byte granularity.
136 * They code segments and data segments have fixed 64k limits,
137 * the transfer segment sizes are set at run time.
138 */
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100139 /* 32-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900140 [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100141 /* 16-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900142 [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100143 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900144 [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100145 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900146 [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100147 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900148 [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
Rusty Russellbf5046722007-05-02 19:27:10 +0200149 /*
150 * The APM segments have byte granularity and their bases
151 * are set at run time. All have 64k limits.
152 */
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100153 /* 32-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900154 [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200155 /* 16-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900156 [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100157 /* data */
Ingo Molnar72c4d852009-08-03 08:47:07 +0200158 [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200159
Akinobu Mita1e5de182009-07-19 00:12:20 +0900160 [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
161 [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
Tejun Heo60a53172009-02-09 22:17:40 +0900162 GDT_STACK_CANARY_INIT
Yinghai Lu950ad7f2008-09-04 20:09:01 -0700163#endif
Brian Gerst06deef82009-01-21 17:26:05 +0900164} };
Jeremy Fitzhardinge7a61d352007-05-02 19:27:15 +0200165EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
Rusty Russellae1ee112007-05-02 19:27:10 +0200166
Dave Hansen8c3641e2015-06-07 11:37:02 -0700167static int __init x86_mpx_setup(char *s)
Suresh Siddha0c752a92009-05-22 12:17:45 -0700168{
Dave Hansen8c3641e2015-06-07 11:37:02 -0700169 /* require an exact match without trailing characters */
Dave Hansen2cd39492014-11-11 14:01:33 -0800170 if (strlen(s))
171 return 0;
Suresh Siddha0c752a92009-05-22 12:17:45 -0700172
Dave Hansen8c3641e2015-06-07 11:37:02 -0700173 /* do not emit a message if the feature is not present */
174 if (!boot_cpu_has(X86_FEATURE_MPX))
175 return 1;
Suresh Siddha6bad06b2010-07-19 16:05:52 -0700176
Dave Hansen8c3641e2015-06-07 11:37:02 -0700177 setup_clear_cpu_cap(X86_FEATURE_MPX);
178 pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
Fenghua Yub6f42a42014-05-29 11:12:31 -0700179 return 1;
180}
Dave Hansen8c3641e2015-06-07 11:37:02 -0700181__setup("nompx", x86_mpx_setup);
Fenghua Yub6f42a42014-05-29 11:12:31 -0700182
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700183#ifdef CONFIG_X86_64
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700184static int __init x86_nopcid_setup(char *s)
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700185{
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700186 /* nopcid doesn't accept parameters */
187 if (s)
188 return -EINVAL;
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700189
190 /* do not emit a message if the feature is not present */
191 if (!boot_cpu_has(X86_FEATURE_PCID))
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700192 return 0;
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700193
194 setup_clear_cpu_cap(X86_FEATURE_PCID);
195 pr_info("nopcid: PCID feature disabled\n");
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700196 return 0;
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700197}
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700198early_param("nopcid", x86_nopcid_setup);
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700199#endif
200
Andy Lutomirskid12a72b2016-01-29 11:42:58 -0800201static int __init x86_noinvpcid_setup(char *s)
202{
203 /* noinvpcid doesn't accept parameters */
204 if (s)
205 return -EINVAL;
206
207 /* do not emit a message if the feature is not present */
208 if (!boot_cpu_has(X86_FEATURE_INVPCID))
209 return 0;
210
211 setup_clear_cpu_cap(X86_FEATURE_INVPCID);
212 pr_info("noinvpcid: INVPCID feature disabled\n");
213 return 0;
214}
215early_param("noinvpcid", x86_noinvpcid_setup);
216
Yinghai Luba51dce2008-09-04 20:09:02 -0700217#ifdef CONFIG_X86_32
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400218static int cachesize_override = -1;
219static int disable_x86_serial_nr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static int __init cachesize_setup(char *str)
222{
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100223 get_option(&str, &cachesize_override);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 1;
225}
226__setup("cachesize=", cachesize_setup);
227
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100228static int __init x86_sep_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Andi Kleen13530252008-01-30 13:33:20 +0100230 setup_clear_cpu_cap(X86_FEATURE_SEP);
Chuck Ebbert4f886512006-03-23 02:59:34 -0800231 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
Chuck Ebbert4f886512006-03-23 02:59:34 -0800233__setup("nosep", x86_sep_setup);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/* Standard macro to see if a specific flag is changeable */
236static inline int flag_is_changeable_p(u32 flag)
237{
238 u32 f1, f2;
239
Krzysztof Helt94f6bac2008-09-30 23:17:51 +0200240 /*
241 * Cyrix and IDT cpus allow disabling of CPUID
242 * so the code below may return different results
243 * when it is executed before and after enabling
244 * the CPUID. Add "volatile" to not allow gcc to
245 * optimize the subsequent calls to this function.
246 */
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100247 asm volatile ("pushfl \n\t"
248 "pushfl \n\t"
249 "popl %0 \n\t"
250 "movl %0, %1 \n\t"
251 "xorl %2, %0 \n\t"
252 "pushl %0 \n\t"
253 "popfl \n\t"
254 "pushfl \n\t"
255 "popl %0 \n\t"
256 "popfl \n\t"
257
Krzysztof Helt94f6bac2008-09-30 23:17:51 +0200258 : "=&r" (f1), "=&r" (f2)
259 : "ir" (flag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 return ((f1^f2) & flag) != 0;
262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/* Probe for the CPUID instruction */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400265int have_cpuid_p(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 return flag_is_changeable_p(X86_EFLAGS_ID);
268}
269
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400270static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
Yinghai Lu0a488a52008-09-04 21:09:47 +0200271{
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100272 unsigned long lo, hi;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200273
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100274 if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
275 return;
276
277 /* Disable processor serial number: */
278
279 rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
280 lo |= 0x200000;
281 wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
282
Chen Yucong1b74dde2016-02-02 11:45:02 +0800283 pr_notice("CPU serial number disabled.\n");
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100284 clear_cpu_cap(c, X86_FEATURE_PN);
285
286 /* Disabling the serial number may affect the cpuid level */
287 c->cpuid_level = cpuid_eax(0);
Yinghai Lu0a488a52008-09-04 21:09:47 +0200288}
289
290static int __init x86_serial_nr_setup(char *s)
291{
292 disable_x86_serial_nr = 0;
293 return 1;
294}
295__setup("serialnumber", x86_serial_nr_setup);
Yinghai Luba51dce2008-09-04 20:09:02 -0700296#else
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700297static inline int flag_is_changeable_p(u32 flag)
298{
299 return 1;
300}
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700301static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
302{
303}
Yinghai Luba51dce2008-09-04 20:09:02 -0700304#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Fenghua Yude5397a2011-05-11 16:51:05 -0700306static __init int setup_disable_smep(char *arg)
307{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700308 setup_clear_cpu_cap(X86_FEATURE_SMEP);
Dave Hansen0f6ff2b2016-05-12 15:04:00 -0700309 /* Check for things that depend on SMEP being enabled: */
310 check_mpx_erratum(&boot_cpu_data);
Fenghua Yude5397a2011-05-11 16:51:05 -0700311 return 1;
312}
313__setup("nosmep", setup_disable_smep);
314
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700315static __always_inline void setup_smep(struct cpuinfo_x86 *c)
Fenghua Yude5397a2011-05-11 16:51:05 -0700316{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700317 if (cpu_has(c, X86_FEATURE_SMEP))
Andy Lutomirski375074c2014-10-24 15:58:07 -0700318 cr4_set_bits(X86_CR4_SMEP);
Fenghua Yude5397a2011-05-11 16:51:05 -0700319}
320
H. Peter Anvin52b61792012-09-21 12:43:13 -0700321static __init int setup_disable_smap(char *arg)
322{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700323 setup_clear_cpu_cap(X86_FEATURE_SMAP);
H. Peter Anvin52b61792012-09-21 12:43:13 -0700324 return 1;
325}
326__setup("nosmap", setup_disable_smap);
327
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700328static __always_inline void setup_smap(struct cpuinfo_x86 *c)
H. Peter Anvin52b61792012-09-21 12:43:13 -0700329{
Andrew Cooper581b7f152015-06-03 10:31:14 +0100330 unsigned long eflags = native_save_fl();
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700331
332 /* This should have been cleared long ago */
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700333 BUG_ON(eflags & X86_EFLAGS_AC);
334
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800335 if (cpu_has(c, X86_FEATURE_SMAP)) {
336#ifdef CONFIG_X86_SMAP
Andy Lutomirski375074c2014-10-24 15:58:07 -0700337 cr4_set_bits(X86_CR4_SMAP);
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800338#else
Andy Lutomirski375074c2014-10-24 15:58:07 -0700339 cr4_clear_bits(X86_CR4_SMAP);
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800340#endif
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Ricardo Neriaa35f892017-11-05 18:27:54 -0800344static __always_inline void setup_umip(struct cpuinfo_x86 *c)
345{
346 /* Check the boot processor, plus build option for UMIP. */
347 if (!cpu_feature_enabled(X86_FEATURE_UMIP))
348 goto out;
349
350 /* Check the current processor's cpuid bits. */
351 if (!cpu_has(c, X86_FEATURE_UMIP))
352 goto out;
353
354 cr4_set_bits(X86_CR4_UMIP);
355
Lendacky, Thomas438cbf82018-12-04 22:27:20 +0000356 pr_info_once("x86/cpu: User Mode Instruction Prevention (UMIP) activated\n");
Ricardo Neri770c7752017-11-13 22:29:43 -0800357
Ricardo Neriaa35f892017-11-05 18:27:54 -0800358 return;
359
360out:
361 /*
362 * Make sure UMIP is disabled in case it was enabled in a
363 * previous boot (e.g., via kexec).
364 */
365 cr4_clear_bits(X86_CR4_UMIP);
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*
Dave Hansen06976942016-02-12 13:02:29 -0800369 * Protection Keys are not available in 32-bit mode.
370 */
371static bool pku_disabled;
372
373static __always_inline void setup_pku(struct cpuinfo_x86 *c)
374{
Sebastian Andrzej Siewiora5eff722019-04-03 18:41:56 +0200375 struct pkru_state *pk;
376
Dave Hansene8df1a952016-05-13 15:13:28 -0700377 /* check the boot processor, plus compile options for PKU: */
378 if (!cpu_feature_enabled(X86_FEATURE_PKU))
379 return;
380 /* checks the actual processor's cpuid bits: */
Dave Hansen06976942016-02-12 13:02:29 -0800381 if (!cpu_has(c, X86_FEATURE_PKU))
382 return;
383 if (pku_disabled)
384 return;
385
386 cr4_set_bits(X86_CR4_PKE);
Sebastian Andrzej Siewiora5eff722019-04-03 18:41:56 +0200387 pk = get_xsave_addr(&init_fpstate.xsave, XFEATURE_PKRU);
388 if (pk)
389 pk->pkru = init_pkru_value;
Dave Hansen06976942016-02-12 13:02:29 -0800390 /*
391 * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
392 * cpuid bit to be set. We need to ensure that we
393 * update that bit in this CPU's "cpu_info".
394 */
395 get_cpu_cap(c);
396}
397
398#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
399static __init int setup_disable_pku(char *arg)
400{
401 /*
402 * Do not clear the X86_FEATURE_PKU bit. All of the
403 * runtime checks are against OSPKE so clearing the
404 * bit does nothing.
405 *
406 * This way, we will see "pku" in cpuinfo, but not
407 * "ospke", which is exactly what we want. It shows
408 * that the CPU has PKU, but the OS has not enabled it.
409 * This happens to be exactly how a system would look
410 * if we disabled the config option.
411 */
412 pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
413 pku_disabled = true;
414 return 1;
415}
416__setup("nopku", setup_disable_pku);
417#endif /* CONFIG_X86_64 */
418
419/*
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800420 * Some CPU features depend on higher CPUID levels, which may not always
421 * be available due to CPUID level capping or broken virtualization
422 * software. Add those features to this table to auto-disable them.
423 */
424struct cpuid_dependent_feature {
425 u32 feature;
426 u32 level;
427};
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100428
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400429static const struct cpuid_dependent_feature
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800430cpuid_dependent_features[] = {
431 { X86_FEATURE_MWAIT, 0x00000005 },
432 { X86_FEATURE_DCA, 0x00000009 },
433 { X86_FEATURE_XSAVE, 0x0000000d },
434 { 0, 0 }
435};
436
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400437static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800438{
439 const struct cpuid_dependent_feature *df;
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530440
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800441 for (df = cpuid_dependent_features; df->feature; df++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100442
443 if (!cpu_has(c, df->feature))
444 continue;
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800445 /*
446 * Note: cpuid_level is set to -1 if unavailable, but
447 * extended_extended_level is set to 0 if unavailable
448 * and the legitimate extended levels are all negative
449 * when signed; hence the weird messing around with
450 * signs here...
451 */
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100452 if (!((s32)df->level < 0 ?
Yinghai Luf6db44d2009-02-14 23:59:18 -0800453 (u32)df->level > (u32)c->extended_cpuid_level :
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100454 (s32)df->level > (s32)c->cpuid_level))
455 continue;
456
457 clear_cpu_cap(c, df->feature);
458 if (!warn)
459 continue;
460
Chen Yucong1b74dde2016-02-02 11:45:02 +0800461 pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
462 x86_cap_flag(df->feature), df->level);
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800463 }
Yinghai Luf6db44d2009-02-14 23:59:18 -0800464}
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800465
466/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * Naming convention should be: <Name> [(<Codename>)]
468 * This table only is used unless init_<vendor>() below doesn't set it;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100469 * in particular, if CPUID levels 0x80000002..4 are supported, this
470 * isn't used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
472
473/* Look up CPU names by table lookup. */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400474static const char *table_lookup_model(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Jan Beulich09dc68d2013-10-21 09:35:20 +0100476#ifdef CONFIG_X86_32
477 const struct legacy_cpu_model_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (c->x86_model >= 16)
480 return NULL; /* Range check */
481
482 if (!this_cpu)
483 return NULL;
484
Jan Beulich09dc68d2013-10-21 09:35:20 +0100485 info = this_cpu->legacy_models;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Jan Beulich09dc68d2013-10-21 09:35:20 +0100487 while (info->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (info->family == c->x86)
489 return info->model_names[c->x86_model];
490 info++;
491 }
Jan Beulich09dc68d2013-10-21 09:35:20 +0100492#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return NULL; /* Not found */
494}
495
Thomas Gleixner6cbd2172017-12-04 15:07:32 +0100496__u32 cpu_caps_cleared[NCAPINTS + NBUGINTS];
497__u32 cpu_caps_set[NCAPINTS + NBUGINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900499void load_percpu_segment(int cpu)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200500{
Yinghai Lufab334c2008-09-04 20:09:05 -0700501#ifdef CONFIG_X86_32
Brian Gerst2697fbd2009-01-27 12:56:48 +0900502 loadsegment(fs, __KERNEL_PERCPU);
503#else
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700504 __loadsegment_simple(gs, 0);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +0100505 wrmsrl(MSR_GS_BASE, cpu_kernelmode_gs_base(cpu));
Yinghai Lufab334c2008-09-04 20:09:05 -0700506#endif
Tejun Heo60a53172009-02-09 22:17:40 +0900507 load_stack_canary_segment();
Yinghai Lu9d31d352008-09-04 21:09:44 +0200508}
509
Andy Lutomirski72f5e082017-12-04 15:07:20 +0100510#ifdef CONFIG_X86_32
511/* The 32-bit entry code needs to find cpu_entry_area. */
512DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
513#endif
514
Thomas Garnier45fc8752017-03-14 10:05:08 -0700515/* Load the original GDT from the per-cpu structure */
516void load_direct_gdt(int cpu)
517{
518 struct desc_ptr gdt_descr;
519
520 gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
521 gdt_descr.size = GDT_SIZE - 1;
522 load_gdt(&gdt_descr);
523}
524EXPORT_SYMBOL_GPL(load_direct_gdt);
525
Thomas Garnier69218e42017-03-14 10:05:07 -0700526/* Load a fixmap remapping of the per-cpu GDT */
527void load_fixmap_gdt(int cpu)
528{
529 struct desc_ptr gdt_descr;
530
531 gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
532 gdt_descr.size = GDT_SIZE - 1;
533 load_gdt(&gdt_descr);
534}
Thomas Garnier45fc8752017-03-14 10:05:08 -0700535EXPORT_SYMBOL_GPL(load_fixmap_gdt);
Thomas Garnier69218e42017-03-14 10:05:07 -0700536
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100537/*
538 * Current gdt points %fs at the "master" per-cpu area: after this,
539 * it's on the real one.
540 */
Brian Gerst552be872009-01-30 17:47:53 +0900541void switch_to_new_gdt(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Thomas Garnier45fc8752017-03-14 10:05:08 -0700543 /* Load the original GDT */
544 load_direct_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /* Reload the per-cpu base */
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900546 load_percpu_segment(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400549static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400551static void get_model_name(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 unsigned int *v;
Borislav Petkovee098e12015-06-01 12:06:57 +0200554 char *p, *q, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Yinghai Lu3da99c92008-09-04 21:09:44 +0200556 if (c->extended_cpuid_level < 0x80000004)
Yinghai Lu1b05d602008-09-06 01:52:27 -0700557 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100559 v = (unsigned int *)c->x86_model_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
561 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
562 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
563 c->x86_model_id[48] = 0;
564
Borislav Petkovee098e12015-06-01 12:06:57 +0200565 /* Trim whitespace */
566 p = q = s = &c->x86_model_id[0];
567
568 while (*p == ' ')
569 p++;
570
571 while (*p) {
572 /* Note the last non-whitespace index */
573 if (!isspace(*p))
574 s = q;
575
576 *q++ = *p++;
577 }
578
579 *(s + 1) = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Thomas Gleixner9305bd62018-05-13 11:43:53 +0200582void detect_num_cpu_cores(struct cpuinfo_x86 *c)
David Wang2cc61be2018-05-03 10:32:44 +0800583{
584 unsigned int eax, ebx, ecx, edx;
585
Thomas Gleixner9305bd62018-05-13 11:43:53 +0200586 c->x86_max_cores = 1;
David Wang2cc61be2018-05-03 10:32:44 +0800587 if (!IS_ENABLED(CONFIG_SMP) || c->cpuid_level < 4)
Thomas Gleixner9305bd62018-05-13 11:43:53 +0200588 return;
David Wang2cc61be2018-05-03 10:32:44 +0800589
590 cpuid_count(4, 0, &eax, &ebx, &ecx, &edx);
591 if (eax & 0x1f)
Thomas Gleixner9305bd62018-05-13 11:43:53 +0200592 c->x86_max_cores = (eax >> 26) + 1;
David Wang2cc61be2018-05-03 10:32:44 +0800593}
594
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400595void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Yinghai Lu9d31d352008-09-04 21:09:44 +0200597 unsigned int n, dummy, ebx, ecx, edx, l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Yinghai Lu3da99c92008-09-04 21:09:44 +0200599 n = c->extended_cpuid_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (n >= 0x80000005) {
Yinghai Lu9d31d352008-09-04 21:09:44 +0200602 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
Yinghai Lu9d31d352008-09-04 21:09:44 +0200603 c->x86_cache_size = (ecx>>24) + (edx>>24);
Yinghai Lu140fc722008-09-04 20:09:07 -0700604#ifdef CONFIG_X86_64
605 /* On K8 L1 TLB is inclusive, so don't count it */
606 c->x86_tlbsize = 0;
607#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
610 if (n < 0x80000006) /* Some chips just has a large L1. */
611 return;
612
Yinghai Lu0a488a52008-09-04 21:09:47 +0200613 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 l2size = ecx >> 16;
615
Yinghai Lu140fc722008-09-04 20:09:07 -0700616#ifdef CONFIG_X86_64
617 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
618#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* do processor-specific cache resizing */
Jan Beulich09dc68d2013-10-21 09:35:20 +0100620 if (this_cpu->legacy_cache_size)
621 l2size = this_cpu->legacy_cache_size(c, l2size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* Allow user to override all this if necessary. */
624 if (cachesize_override != -1)
625 l2size = cachesize_override;
626
627 if (l2size == 0)
628 return; /* Again, no L2 cache is possible */
Yinghai Lu140fc722008-09-04 20:09:07 -0700629#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 c->x86_cache_size = l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Alex Shie0ba94f2012-06-28 09:02:16 +0800634u16 __read_mostly tlb_lli_4k[NR_INFO];
635u16 __read_mostly tlb_lli_2m[NR_INFO];
636u16 __read_mostly tlb_lli_4m[NR_INFO];
637u16 __read_mostly tlb_lld_4k[NR_INFO];
638u16 __read_mostly tlb_lld_2m[NR_INFO];
639u16 __read_mostly tlb_lld_4m[NR_INFO];
Kirill A. Shutemovdd360392013-12-23 14:16:58 +0200640u16 __read_mostly tlb_lld_1g[NR_INFO];
Alex Shie0ba94f2012-06-28 09:02:16 +0800641
Steven Honeymanf94fe112014-11-05 22:52:18 +0000642static void cpu_detect_tlb(struct cpuinfo_x86 *c)
Alex Shie0ba94f2012-06-28 09:02:16 +0800643{
644 if (this_cpu->c_detect_tlb)
645 this_cpu->c_detect_tlb(c);
646
Steven Honeymanf94fe112014-11-05 22:52:18 +0000647 pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
Alex Shie0ba94f2012-06-28 09:02:16 +0800648 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
Steven Honeymanf94fe112014-11-05 22:52:18 +0000649 tlb_lli_4m[ENTRIES]);
650
651 pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
652 tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
653 tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
Alex Shie0ba94f2012-06-28 09:02:16 +0800654}
655
Thomas Gleixner545401f2018-06-06 00:53:57 +0200656int detect_ht_early(struct cpuinfo_x86 *c)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200657{
Borislav Petkovc8e56d22015-06-04 18:55:25 +0200658#ifdef CONFIG_SMP
Yinghai Lu0a488a52008-09-04 21:09:47 +0200659 u32 eax, ebx, ecx, edx;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200660
661 if (!cpu_has(c, X86_FEATURE_HT))
Thomas Gleixner545401f2018-06-06 00:53:57 +0200662 return -1;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200663
664 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
Thomas Gleixner545401f2018-06-06 00:53:57 +0200665 return -1;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200666
Yinghai Lu1cd78772008-09-04 20:09:08 -0700667 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
Thomas Gleixner545401f2018-06-06 00:53:57 +0200668 return -1;
Yinghai Lu1cd78772008-09-04 20:09:08 -0700669
Yinghai Lu9d31d352008-09-04 21:09:44 +0200670 cpuid(1, &eax, &ebx, &ecx, &edx);
671
Yinghai Lu9d31d352008-09-04 21:09:44 +0200672 smp_num_siblings = (ebx & 0xff0000) >> 16;
Thomas Gleixner545401f2018-06-06 00:53:57 +0200673 if (smp_num_siblings == 1)
Chen Yucong1b74dde2016-02-02 11:45:02 +0800674 pr_info_once("CPU0: Hyper-Threading is disabled\n");
Thomas Gleixner545401f2018-06-06 00:53:57 +0200675#endif
676 return 0;
677}
Yinghai Lu9d31d352008-09-04 21:09:44 +0200678
Thomas Gleixner545401f2018-06-06 00:53:57 +0200679void detect_ht(struct cpuinfo_x86 *c)
680{
681#ifdef CONFIG_SMP
682 int index_msb, core_bits;
683
684 if (detect_ht_early(c) < 0)
Thomas Gleixner55e6d272018-06-06 00:36:15 +0200685 return;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100686
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100687 index_msb = get_count_order(smp_num_siblings);
688 c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
689
690 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
691
692 index_msb = get_count_order(smp_num_siblings);
693
694 core_bits = get_count_order(c->x86_max_cores);
695
696 c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
697 ((1 << core_bits) - 1);
Yinghai Lu9d31d352008-09-04 21:09:44 +0200698#endif
Yinghai Lu97e4db72008-09-04 20:08:59 -0700699}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400701static void get_cpu_vendor(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 char *v = c->x86_vendor_id;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100704 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 for (i = 0; i < X86_VENDOR_NUM; i++) {
Yinghai Lu10a434f2008-09-04 21:09:45 +0200707 if (!cpu_devs[i])
708 break;
709
710 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
711 (cpu_devs[i]->c_ident[1] &&
712 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100713
Yinghai Lu10a434f2008-09-04 21:09:45 +0200714 this_cpu = cpu_devs[i];
715 c->x86_vendor = this_cpu->c_x86_vendor;
716 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 }
Yinghai Lu10a434f2008-09-04 21:09:45 +0200719
Chen Yucong1b74dde2016-02-02 11:45:02 +0800720 pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
721 "CPU: Your system may be unstable.\n", v);
Yinghai Lu10a434f2008-09-04 21:09:45 +0200722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 c->x86_vendor = X86_VENDOR_UNKNOWN;
724 this_cpu = &default_cpu;
725}
726
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400727void cpu_detect(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* Get vendor name */
Harvey Harrison4a148512008-02-01 17:49:43 +0100730 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
731 (unsigned int *)&c->x86_vendor_id[0],
732 (unsigned int *)&c->x86_vendor_id[8],
733 (unsigned int *)&c->x86_vendor_id[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 c->x86 = 4;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200736 /* Intel-defined flags: level 0x00000001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (c->cpuid_level >= 0x00000001) {
738 u32 junk, tfms, cap0, misc;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
Borislav Petkov99f925c2015-11-23 11:12:21 +0100741 c->x86 = x86_family(tfms);
742 c->x86_model = x86_model(tfms);
Jia Zhangb3991512018-01-01 09:52:10 +0800743 c->x86_stepping = x86_stepping(tfms);
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100744
Huang, Yingd4387bd2008-01-31 22:05:45 +0100745 if (cap0 & (1<<19)) {
Huang, Yingd4387bd2008-01-31 22:05:45 +0100746 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200747 c->x86_cache_alignment = c->x86_clflush_size;
Huang, Yingd4387bd2008-01-31 22:05:45 +0100748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
Yinghai Lu3da99c92008-09-04 21:09:44 +0200751
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -0800752static void apply_forced_caps(struct cpuinfo_x86 *c)
753{
754 int i;
755
Thomas Gleixner6cbd2172017-12-04 15:07:32 +0100756 for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -0800757 c->x86_capability[i] &= ~cpu_caps_cleared[i];
758 c->x86_capability[i] |= cpu_caps_set[i];
759 }
760}
761
David Woodhouse7fcae112018-01-30 14:30:23 +0000762static void init_speculation_control(struct cpuinfo_x86 *c)
763{
764 /*
765 * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
766 * and they also have a different bit for STIBP support. Also,
767 * a hypervisor might have set the individual AMD bits even on
768 * Intel CPUs, for finer-grained selection of what's available.
David Woodhouse7fcae112018-01-30 14:30:23 +0000769 */
770 if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
771 set_cpu_cap(c, X86_FEATURE_IBRS);
772 set_cpu_cap(c, X86_FEATURE_IBPB);
Thomas Gleixner7eb89562018-05-10 19:13:18 +0200773 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
David Woodhouse7fcae112018-01-30 14:30:23 +0000774 }
Borislav Petkove7c587d2018-05-02 18:15:14 +0200775
David Woodhouse7fcae112018-01-30 14:30:23 +0000776 if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
777 set_cpu_cap(c, X86_FEATURE_STIBP);
Borislav Petkove7c587d2018-05-02 18:15:14 +0200778
Tom Lendackybc226f02018-05-10 22:06:39 +0200779 if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
780 cpu_has(c, X86_FEATURE_VIRT_SSBD))
Thomas Gleixner52817582018-05-10 20:21:36 +0200781 set_cpu_cap(c, X86_FEATURE_SSBD);
782
Thomas Gleixner7eb89562018-05-10 19:13:18 +0200783 if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
Borislav Petkove7c587d2018-05-02 18:15:14 +0200784 set_cpu_cap(c, X86_FEATURE_IBRS);
Thomas Gleixner7eb89562018-05-10 19:13:18 +0200785 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
786 }
Borislav Petkove7c587d2018-05-02 18:15:14 +0200787
788 if (cpu_has(c, X86_FEATURE_AMD_IBPB))
789 set_cpu_cap(c, X86_FEATURE_IBPB);
790
Thomas Gleixner7eb89562018-05-10 19:13:18 +0200791 if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
Borislav Petkove7c587d2018-05-02 18:15:14 +0200792 set_cpu_cap(c, X86_FEATURE_STIBP);
Thomas Gleixner7eb89562018-05-10 19:13:18 +0200793 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
794 }
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -0400795
796 if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
797 set_cpu_cap(c, X86_FEATURE_SSBD);
798 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
799 clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
800 }
David Woodhouse7fcae112018-01-30 14:30:23 +0000801}
802
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400803void get_cpu_cap(struct cpuinfo_x86 *c)
Yinghai Lu093af8d2008-01-30 13:33:32 +0100804{
Borislav Petkov39c06df2015-12-07 10:39:40 +0100805 u32 eax, ebx, ecx, edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100806
Yinghai Lu3da99c92008-09-04 21:09:44 +0200807 /* Intel-defined flags: level 0x00000001 */
808 if (c->cpuid_level >= 0x00000001) {
Borislav Petkov39c06df2015-12-07 10:39:40 +0100809 cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100810
Borislav Petkov39c06df2015-12-07 10:39:40 +0100811 c->x86_capability[CPUID_1_ECX] = ecx;
812 c->x86_capability[CPUID_1_EDX] = edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100813 }
814
Andy Lutomirski3df8d9202016-12-15 10:14:42 -0800815 /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
816 if (c->cpuid_level >= 0x00000006)
817 c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
818
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700819 /* Additional Intel-defined flags: level 0x00000007 */
820 if (c->cpuid_level >= 0x00000007) {
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700821 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100822 c->x86_capability[CPUID_7_0_EBX] = ebx;
Dave Hansendfb4a702016-02-12 13:02:01 -0800823 c->x86_capability[CPUID_7_ECX] = ecx;
David Woodhouse95ca0ee2018-01-25 16:14:09 +0000824 c->x86_capability[CPUID_7_EDX] = edx;
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700825 }
826
Fenghua Yu6229ad22014-05-29 11:12:30 -0700827 /* Extended state features: level 0x0000000d */
828 if (c->cpuid_level >= 0x0000000d) {
Fenghua Yu6229ad22014-05-29 11:12:30 -0700829 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
830
Borislav Petkov39c06df2015-12-07 10:39:40 +0100831 c->x86_capability[CPUID_D_1_EAX] = eax;
Fenghua Yu6229ad22014-05-29 11:12:30 -0700832 }
833
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000834 /* Additional Intel-defined flags: level 0x0000000F */
835 if (c->cpuid_level >= 0x0000000F) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000836
837 /* QoS sub-leaf, EAX=0Fh, ECX=0 */
838 cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100839 c->x86_capability[CPUID_F_0_EDX] = edx;
840
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000841 if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
842 /* will be overridden if occupancy monitoring exists */
843 c->x86_cache_max_rmid = ebx;
844
845 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
846 cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100847 c->x86_capability[CPUID_F_1_EDX] = edx;
848
Vikas Shivappa33c3cc72016-03-10 15:32:09 -0800849 if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
850 ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
851 (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000852 c->x86_cache_max_rmid = ecx;
853 c->x86_cache_occ_scale = ebx;
854 }
855 } else {
856 c->x86_cache_max_rmid = -1;
857 c->x86_cache_occ_scale = -1;
858 }
859 }
860
Yinghai Lu3da99c92008-09-04 21:09:44 +0200861 /* AMD-defined flags: level 0x80000001 */
Borislav Petkov39c06df2015-12-07 10:39:40 +0100862 eax = cpuid_eax(0x80000000);
863 c->extended_cpuid_level = eax;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100864
Borislav Petkov39c06df2015-12-07 10:39:40 +0100865 if ((eax & 0xffff0000) == 0x80000000) {
866 if (eax >= 0x80000001) {
867 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
868
869 c->x86_capability[CPUID_8000_0001_ECX] = ecx;
870 c->x86_capability[CPUID_8000_0001_EDX] = edx;
Yinghai Lu3da99c92008-09-04 21:09:44 +0200871 }
872 }
Yinghai Lu5122c892008-09-04 20:09:09 -0700873
Yazen Ghannam71faad42016-05-11 14:58:26 +0200874 if (c->extended_cpuid_level >= 0x80000007) {
875 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
876
877 c->x86_capability[CPUID_8000_0007_EBX] = ebx;
878 c->x86_power = edx;
879 }
880
Thomas Gleixnerc65732e2018-04-30 21:47:46 +0200881 if (c->extended_cpuid_level >= 0x80000008) {
882 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
883 c->x86_capability[CPUID_8000_0008_EBX] = ebx;
884 }
885
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100886 if (c->extended_cpuid_level >= 0x8000000a)
Borislav Petkov39c06df2015-12-07 10:39:40 +0100887 c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100888
Jacob Pan1dedefd2010-05-19 12:01:23 -0700889 init_scattered_cpuid_features(c);
David Woodhouse7fcae112018-01-30 14:30:23 +0000890 init_speculation_control(c);
Andy Lutomirski60d34502017-01-18 11:15:39 -0800891
892 /*
893 * Clear/Set all flags overridden by options, after probe.
894 * This needs to happen each time we re-probe, which may happen
895 * several times during CPU initialization.
896 */
897 apply_forced_caps(c);
Yinghai Lu093af8d2008-01-30 13:33:32 +0100898}
Yinghai Luaef93c82008-09-14 02:33:15 -0700899
M. Vefa Bicakci405c0182018-07-24 08:45:47 -0400900void get_cpu_address_sizes(struct cpuinfo_x86 *c)
Kirill A. Shutemovd94a1552018-04-10 12:27:04 +0300901{
902 u32 eax, ebx, ecx, edx;
903
904 if (c->extended_cpuid_level >= 0x80000008) {
905 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
906
907 c->x86_virt_bits = (eax >> 8) & 0xff;
908 c->x86_phys_bits = eax & 0xff;
Kirill A. Shutemovd94a1552018-04-10 12:27:04 +0300909 }
910#ifdef CONFIG_X86_32
911 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
912 c->x86_phys_bits = 36;
913#endif
Andi Kleencc51e542018-08-24 10:03:50 -0700914 c->x86_cache_bits = c->x86_phys_bits;
Kirill A. Shutemovd94a1552018-04-10 12:27:04 +0300915}
916
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400917static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
Yinghai Luaef93c82008-09-14 02:33:15 -0700918{
919#ifdef CONFIG_X86_32
920 int i;
921
922 /*
923 * First of all, decide if this is a 486 or higher
924 * It's a 486 if we can modify the AC flag
925 */
926 if (flag_is_changeable_p(X86_EFLAGS_AC))
927 c->x86 = 4;
928 else
929 c->x86 = 3;
930
931 for (i = 0; i < X86_VENDOR_NUM; i++)
932 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
933 c->x86_vendor_id[0] = 0;
934 cpu_devs[i]->c_identify(c);
935 if (c->x86_vendor_id[0]) {
936 get_cpu_vendor(c);
937 break;
938 }
939 }
940#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Thomas Gleixner36ad3512019-02-27 10:10:23 +0100943#define NO_SPECULATION BIT(0)
944#define NO_MELTDOWN BIT(1)
945#define NO_SSB BIT(2)
946#define NO_L1TF BIT(3)
Andi Kleened5194c2019-01-18 16:50:16 -0800947#define NO_MDS BIT(4)
Thomas Gleixnere261f202019-03-01 20:21:08 +0100948#define MSBDS_ONLY BIT(5)
Thomas Gleixner36ad3512019-02-27 10:10:23 +0100949
950#define VULNWL(_vendor, _family, _model, _whitelist) \
951 { X86_VENDOR_##_vendor, _family, _model, X86_FEATURE_ANY, _whitelist }
952
953#define VULNWL_INTEL(model, whitelist) \
954 VULNWL(INTEL, 6, INTEL_FAM6_##model, whitelist)
955
956#define VULNWL_AMD(family, whitelist) \
957 VULNWL(AMD, family, X86_MODEL_ANY, whitelist)
958
959#define VULNWL_HYGON(family, whitelist) \
960 VULNWL(HYGON, family, X86_MODEL_ANY, whitelist)
961
962static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
963 VULNWL(ANY, 4, X86_MODEL_ANY, NO_SPECULATION),
964 VULNWL(CENTAUR, 5, X86_MODEL_ANY, NO_SPECULATION),
965 VULNWL(INTEL, 5, X86_MODEL_ANY, NO_SPECULATION),
966 VULNWL(NSC, 5, X86_MODEL_ANY, NO_SPECULATION),
967
Andi Kleened5194c2019-01-18 16:50:16 -0800968 /* Intel Family 6 */
Thomas Gleixner36ad3512019-02-27 10:10:23 +0100969 VULNWL_INTEL(ATOM_SALTWELL, NO_SPECULATION),
970 VULNWL_INTEL(ATOM_SALTWELL_TABLET, NO_SPECULATION),
971 VULNWL_INTEL(ATOM_SALTWELL_MID, NO_SPECULATION),
972 VULNWL_INTEL(ATOM_BONNELL, NO_SPECULATION),
973 VULNWL_INTEL(ATOM_BONNELL_MID, NO_SPECULATION),
974
Thomas Gleixnere261f202019-03-01 20:21:08 +0100975 VULNWL_INTEL(ATOM_SILVERMONT, NO_SSB | NO_L1TF | MSBDS_ONLY),
976 VULNWL_INTEL(ATOM_SILVERMONT_X, NO_SSB | NO_L1TF | MSBDS_ONLY),
977 VULNWL_INTEL(ATOM_SILVERMONT_MID, NO_SSB | NO_L1TF | MSBDS_ONLY),
978 VULNWL_INTEL(ATOM_AIRMONT, NO_SSB | NO_L1TF | MSBDS_ONLY),
979 VULNWL_INTEL(XEON_PHI_KNL, NO_SSB | NO_L1TF | MSBDS_ONLY),
980 VULNWL_INTEL(XEON_PHI_KNM, NO_SSB | NO_L1TF | MSBDS_ONLY),
Thomas Gleixner36ad3512019-02-27 10:10:23 +0100981
982 VULNWL_INTEL(CORE_YONAH, NO_SSB),
983
Thomas Gleixnere261f202019-03-01 20:21:08 +0100984 VULNWL_INTEL(ATOM_AIRMONT_MID, NO_L1TF | MSBDS_ONLY),
Thomas Gleixner36ad3512019-02-27 10:10:23 +0100985
Andi Kleened5194c2019-01-18 16:50:16 -0800986 VULNWL_INTEL(ATOM_GOLDMONT, NO_MDS | NO_L1TF),
987 VULNWL_INTEL(ATOM_GOLDMONT_X, NO_MDS | NO_L1TF),
988 VULNWL_INTEL(ATOM_GOLDMONT_PLUS, NO_MDS | NO_L1TF),
989
990 /* AMD Family 0xf - 0x12 */
991 VULNWL_AMD(0x0f, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS),
992 VULNWL_AMD(0x10, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS),
993 VULNWL_AMD(0x11, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS),
994 VULNWL_AMD(0x12, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS),
Thomas Gleixner36ad3512019-02-27 10:10:23 +0100995
996 /* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */
Andi Kleened5194c2019-01-18 16:50:16 -0800997 VULNWL_AMD(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS),
998 VULNWL_HYGON(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS),
David Woodhousefec94342018-01-25 16:14:13 +0000999 {}
1000};
1001
Thomas Gleixner36ad3512019-02-27 10:10:23 +01001002static bool __init cpu_matches(unsigned long which)
1003{
1004 const struct x86_cpu_id *m = x86_match_cpu(cpu_vuln_whitelist);
David Woodhousefec94342018-01-25 16:14:13 +00001005
Thomas Gleixner36ad3512019-02-27 10:10:23 +01001006 return m && !!(m->driver_data & which);
1007}
Andi Kleen17dbca12018-06-13 15:48:26 -07001008
Konrad Rzeszutek Wilk4a28bfe2018-04-25 22:04:16 -04001009static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
David Woodhousefec94342018-01-25 16:14:13 +00001010{
1011 u64 ia32_cap = 0;
1012
Thomas Gleixner36ad3512019-02-27 10:10:23 +01001013 if (cpu_matches(NO_SPECULATION))
Dominik Brodowski8ecc4972018-05-22 11:05:39 +02001014 return;
1015
1016 setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
1017 setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
1018
Konrad Rzeszutek Wilk77243972018-04-25 22:04:22 -04001019 if (cpu_has(c, X86_FEATURE_ARCH_CAPABILITIES))
1020 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
1021
Thomas Gleixner36ad3512019-02-27 10:10:23 +01001022 if (!cpu_matches(NO_SSB) && !(ia32_cap & ARCH_CAP_SSB_NO) &&
Konrad Rzeszutek Wilk24809862018-06-01 10:59:19 -04001023 !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
Konrad Rzeszutek Wilkc4564422018-04-25 22:04:20 -04001024 setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
1025
Sai Praneeth706d5162018-08-01 11:42:25 -07001026 if (ia32_cap & ARCH_CAP_IBRS_ALL)
1027 setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
1028
Thomas Gleixnere261f202019-03-01 20:21:08 +01001029 if (!cpu_matches(NO_MDS) && !(ia32_cap & ARCH_CAP_MDS_NO)) {
Andi Kleened5194c2019-01-18 16:50:16 -08001030 setup_force_cpu_bug(X86_BUG_MDS);
Thomas Gleixnere261f202019-03-01 20:21:08 +01001031 if (cpu_matches(MSBDS_ONLY))
1032 setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
1033 }
Andi Kleened5194c2019-01-18 16:50:16 -08001034
Thomas Gleixner36ad3512019-02-27 10:10:23 +01001035 if (cpu_matches(NO_MELTDOWN))
Konrad Rzeszutek Wilk4a28bfe2018-04-25 22:04:16 -04001036 return;
David Woodhousefec94342018-01-25 16:14:13 +00001037
David Woodhousefec94342018-01-25 16:14:13 +00001038 /* Rogue Data Cache Load? No! */
1039 if (ia32_cap & ARCH_CAP_RDCL_NO)
Konrad Rzeszutek Wilk4a28bfe2018-04-25 22:04:16 -04001040 return;
David Woodhousefec94342018-01-25 16:14:13 +00001041
Konrad Rzeszutek Wilk4a28bfe2018-04-25 22:04:16 -04001042 setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
Andi Kleen17dbca12018-06-13 15:48:26 -07001043
Thomas Gleixner36ad3512019-02-27 10:10:23 +01001044 if (cpu_matches(NO_L1TF))
Andi Kleen17dbca12018-06-13 15:48:26 -07001045 return;
1046
1047 setup_force_cpu_bug(X86_BUG_L1TF);
David Woodhousefec94342018-01-25 16:14:13 +00001048}
1049
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001050/*
Pavel Tatashin8990cac2018-07-19 16:55:28 -04001051 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1052 * unfortunately, that's not true in practice because of early VIA
1053 * chips and (more importantly) broken virtualizers that are not easy
1054 * to detect. In the latter case it doesn't even *fail* reliably, so
1055 * probing for it doesn't even work. Disable it completely on 32-bit
1056 * unless we can find a reliable way to detect all the broken cases.
1057 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
1058 */
Borislav Petkov9b3661c2018-07-19 16:55:29 -04001059static void detect_nopl(void)
Pavel Tatashin8990cac2018-07-19 16:55:28 -04001060{
1061#ifdef CONFIG_X86_32
Borislav Petkov9b3661c2018-07-19 16:55:29 -04001062 setup_clear_cpu_cap(X86_FEATURE_NOPL);
Pavel Tatashin8990cac2018-07-19 16:55:28 -04001063#else
Borislav Petkov9b3661c2018-07-19 16:55:29 -04001064 setup_force_cpu_cap(X86_FEATURE_NOPL);
Pavel Tatashin8990cac2018-07-19 16:55:28 -04001065#endif
1066}
1067
1068/*
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001069 * Do minimum CPU detection early.
1070 * Fields really needed: vendor, cpuid_level, family, model, mask,
1071 * cache alignment.
1072 * The others are not touched to avoid unwanted side effects.
1073 *
Jean Delvarea1652bb2017-10-03 11:47:27 +02001074 * WARNING: this function is only called on the boot CPU. Don't add code
1075 * here that is supposed to run on all CPUs.
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001076 */
Yinghai Lu3da99c92008-09-04 21:09:44 +02001077static void __init early_identify_cpu(struct cpuinfo_x86 *c)
Rusty Russelld7cd5612006-12-07 02:14:08 +01001078{
Yinghai Lu6627d242008-09-04 20:09:10 -07001079#ifdef CONFIG_X86_64
1080 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +00001081 c->x86_phys_bits = 36;
1082 c->x86_virt_bits = 48;
Yinghai Lu6627d242008-09-04 20:09:10 -07001083#else
Huang, Yingd4387bd2008-01-31 22:05:45 +01001084 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +00001085 c->x86_phys_bits = 32;
1086 c->x86_virt_bits = 32;
Yinghai Lu6627d242008-09-04 20:09:10 -07001087#endif
Yinghai Lu0a488a52008-09-04 21:09:47 +02001088 c->x86_cache_alignment = c->x86_clflush_size;
Rusty Russelld7cd5612006-12-07 02:14:08 +01001089
Jordan Borgner0e96f312018-10-28 12:58:28 +00001090 memset(&c->x86_capability, 0, sizeof(c->x86_capability));
Yinghai Lu0a488a52008-09-04 21:09:47 +02001091 c->extended_cpuid_level = 0;
1092
Matthew Whitehead2893cc82018-09-21 17:20:41 -04001093 if (!have_cpuid_p())
1094 identify_cpu_without_cpuid(c);
1095
Yinghai Luaef93c82008-09-14 02:33:15 -07001096 /* cyrix could have cpuid enabled via c_identify()*/
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001097 if (have_cpuid_p()) {
1098 cpu_detect(c);
1099 get_cpu_vendor(c);
1100 get_cpu_cap(c);
Kirill A. Shutemovd94a1552018-04-10 12:27:04 +03001101 get_cpu_address_sizes(c);
Borislav Petkov78d1b29682017-01-18 11:15:37 -08001102 setup_force_cpu_cap(X86_FEATURE_CPUID);
Rusty Russelld7cd5612006-12-07 02:14:08 +01001103
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001104 if (this_cpu->c_early_init)
1105 this_cpu->c_early_init(c);
Krzysztof Helt12cf1052008-09-04 21:09:43 +02001106
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001107 c->cpu_index = 0;
1108 filter_cpuid_features(c, false);
Yinghai Lu3da99c92008-09-04 21:09:44 +02001109
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001110 if (this_cpu->c_bsp_init)
1111 this_cpu->c_bsp_init(c);
Borislav Petkov78d1b29682017-01-18 11:15:37 -08001112 } else {
Borislav Petkov78d1b29682017-01-18 11:15:37 -08001113 setup_clear_cpu_cap(X86_FEATURE_CPUID);
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001114 }
Borislav Petkovc3b83592013-06-09 12:07:30 +02001115
1116 setup_force_cpu_cap(X86_FEATURE_ALWAYS);
Thomas Gleixnera89f0402017-12-04 15:07:33 +01001117
Konrad Rzeszutek Wilk4a28bfe2018-04-25 22:04:16 -04001118 cpu_set_bug_bits(c);
David Woodhouse99c6fa22018-01-06 11:49:23 +00001119
Ingo Molnardb52ef72015-06-27 10:25:14 +02001120 fpu__init_system(c);
Andy Lutomirskib8b7aba2017-09-17 09:03:50 -07001121
1122#ifdef CONFIG_X86_32
1123 /*
1124 * Regardless of whether PCID is enumerated, the SDM says
1125 * that it can't be enabled in 32-bit mode.
1126 */
1127 setup_clear_cpu_cap(X86_FEATURE_PCID);
1128#endif
Kirill A. Shutemov372fddf2018-05-18 13:35:25 +03001129
1130 /*
1131 * Later in the boot process pgtable_l5_enabled() relies on
1132 * cpu_feature_enabled(X86_FEATURE_LA57). If 5-level paging is not
1133 * enabled by this point we need to clear the feature bit to avoid
1134 * false-positives at the later stage.
1135 *
1136 * pgtable_l5_enabled() can be false here for several reasons:
1137 * - 5-level paging is disabled compile-time;
1138 * - it's 32-bit kernel;
1139 * - machine doesn't support 5-level paging;
1140 * - user specified 'no5lvl' in kernel command line.
1141 */
1142 if (!pgtable_l5_enabled())
1143 setup_clear_cpu_cap(X86_FEATURE_LA57);
Pavel Tatashin8990cac2018-07-19 16:55:28 -04001144
Borislav Petkov9b3661c2018-07-19 16:55:29 -04001145 detect_nopl();
Rusty Russelld7cd5612006-12-07 02:14:08 +01001146}
1147
Yinghai Lu9d31d352008-09-04 21:09:44 +02001148void __init early_cpu_init(void)
1149{
Jan Beulich02dde8b2009-03-12 12:08:49 +00001150 const struct cpu_dev *const *cdev;
Yinghai Lu10a434f2008-09-04 21:09:45 +02001151 int count = 0;
Yinghai Lu9d31d352008-09-04 21:09:44 +02001152
Jan Beulichac23f252011-03-04 15:52:35 +00001153#ifdef CONFIG_PROCESSOR_SELECT
Chen Yucong1b74dde2016-02-02 11:45:02 +08001154 pr_info("KERNEL supported cpus:\n");
Ingo Molnar31c997c2009-11-14 10:34:41 +01001155#endif
1156
Yinghai Lu10a434f2008-09-04 21:09:45 +02001157 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
Jan Beulich02dde8b2009-03-12 12:08:49 +00001158 const struct cpu_dev *cpudev = *cdev;
Yinghai Lu9d31d352008-09-04 21:09:44 +02001159
Yinghai Lu10a434f2008-09-04 21:09:45 +02001160 if (count >= X86_VENDOR_NUM)
1161 break;
1162 cpu_devs[count] = cpudev;
1163 count++;
1164
Jan Beulichac23f252011-03-04 15:52:35 +00001165#ifdef CONFIG_PROCESSOR_SELECT
Ingo Molnar31c997c2009-11-14 10:34:41 +01001166 {
1167 unsigned int j;
Yinghai Lu10a434f2008-09-04 21:09:45 +02001168
Ingo Molnar31c997c2009-11-14 10:34:41 +01001169 for (j = 0; j < 2; j++) {
1170 if (!cpudev->c_ident[j])
1171 continue;
Chen Yucong1b74dde2016-02-02 11:45:02 +08001172 pr_info(" %s %s\n", cpudev->c_vendor,
Ingo Molnar31c997c2009-11-14 10:34:41 +01001173 cpudev->c_ident[j]);
1174 }
Yinghai Lu9d31d352008-09-04 21:09:44 +02001175 }
Dave Jones03884232009-11-13 15:30:00 -05001176#endif
Ingo Molnar31c997c2009-11-14 10:34:41 +01001177 }
Yinghai Lu9d31d352008-09-04 21:09:44 +02001178 early_identify_cpu(&boot_cpu_data);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08001179}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001181static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
1182{
1183#ifdef CONFIG_X86_64
Andy Lutomirski58a5aac2016-02-29 15:50:19 -08001184 /*
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001185 * Empirically, writing zero to a segment selector on AMD does
1186 * not clear the base, whereas writing zero to a segment
1187 * selector on Intel does clear the base. Intel's behavior
1188 * allows slightly faster context switches in the common case
1189 * where GS is unused by the prev and next threads.
Andy Lutomirski58a5aac2016-02-29 15:50:19 -08001190 *
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001191 * Since neither vendor documents this anywhere that I can see,
1192 * detect it directly instead of hardcoding the choice by
1193 * vendor.
1194 *
1195 * I've designated AMD's behavior as the "bug" because it's
1196 * counterintuitive and less friendly.
Andy Lutomirski58a5aac2016-02-29 15:50:19 -08001197 */
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001198
1199 unsigned long old_base, tmp;
1200 rdmsrl(MSR_FS_BASE, old_base);
1201 wrmsrl(MSR_FS_BASE, 1);
1202 loadsegment(fs, 0);
1203 rdmsrl(MSR_FS_BASE, tmp);
1204 if (tmp != 0)
1205 set_cpu_bug(c, X86_BUG_NULL_SEG);
1206 wrmsrl(MSR_FS_BASE, old_base);
Andy Lutomirski58a5aac2016-02-29 15:50:19 -08001207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001210static void generic_identify(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Yinghai Lu3da99c92008-09-04 21:09:44 +02001212 c->extended_cpuid_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Yinghai Luaef93c82008-09-14 02:33:15 -07001214 if (!have_cpuid_p())
1215 identify_cpu_without_cpuid(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001216
Yinghai Luaef93c82008-09-14 02:33:15 -07001217 /* cyrix could have cpuid enabled via c_identify()*/
Ingo Molnara9853dd2008-09-14 14:46:58 +02001218 if (!have_cpuid_p())
Yinghai Luaef93c82008-09-14 02:33:15 -07001219 return;
1220
Yinghai Lu3da99c92008-09-04 21:09:44 +02001221 cpu_detect(c);
1222
1223 get_cpu_vendor(c);
1224
1225 get_cpu_cap(c);
1226
Kirill A. Shutemovd94a1552018-04-10 12:27:04 +03001227 get_cpu_address_sizes(c);
1228
Yinghai Lu3da99c92008-09-04 21:09:44 +02001229 if (c->cpuid_level >= 0x00000001) {
1230 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
Yinghai Lub89d3b32008-09-04 20:09:12 -07001231#ifdef CONFIG_X86_32
Borislav Petkovc8e56d22015-06-04 18:55:25 +02001232# ifdef CONFIG_SMP
Ingo Molnarcb8cc442009-01-28 13:24:54 +01001233 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lub89d3b32008-09-04 20:09:12 -07001234# else
Yinghai Lu3da99c92008-09-04 21:09:44 +02001235 c->apicid = c->initial_apicid;
Yinghai Lub89d3b32008-09-04 20:09:12 -07001236# endif
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08001237#endif
Yinghai Lub89d3b32008-09-04 20:09:12 -07001238 c->phys_proc_id = c->initial_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Yinghai Lu3da99c92008-09-04 21:09:44 +02001240
Yinghai Lu1b05d602008-09-06 01:52:27 -07001241 get_model_name(c); /* Default name */
Yinghai Lu3da99c92008-09-04 21:09:44 +02001242
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001243 detect_null_seg_behavior(c);
Andy Lutomirski0230bb02016-04-07 17:31:48 -07001244
1245 /*
1246 * ESPFIX is a strange bug. All real CPUs have it. Paravirt
1247 * systems that run Linux at CPL > 0 may or may not have the
1248 * issue, but, even if they have the issue, there's absolutely
1249 * nothing we can do about it because we can't use the real IRET
1250 * instruction.
1251 *
1252 * NB: For the time being, only 32-bit kernels support
1253 * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
1254 * whether to apply espfix using paravirt hooks. If any
1255 * non-paravirt system ever shows up that does *not* have the
1256 * ESPFIX issue, we can change this.
1257 */
1258#ifdef CONFIG_X86_32
Juergen Gross9bad5652018-08-28 09:40:23 +02001259# ifdef CONFIG_PARAVIRT_XXL
Andy Lutomirski0230bb02016-04-07 17:31:48 -07001260 do {
1261 extern void native_iret(void);
Juergen Gross5c835112018-08-28 09:40:19 +02001262 if (pv_ops.cpu.iret == native_iret)
Andy Lutomirski0230bb02016-04-07 17:31:48 -07001263 set_cpu_bug(c, X86_BUG_ESPFIX);
1264 } while (0);
1265# else
1266 set_cpu_bug(c, X86_BUG_ESPFIX);
1267# endif
1268#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001271static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1272{
1273 /*
1274 * The heavy lifting of max_rmid and cache_occ_scale are handled
1275 * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
1276 * in case CQM bits really aren't there in this CPU.
1277 */
1278 if (c != &boot_cpu_data) {
1279 boot_cpu_data.x86_cache_max_rmid =
1280 min(boot_cpu_data.x86_cache_max_rmid,
1281 c->x86_cache_max_rmid);
1282 }
1283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285/*
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001286 * Validate that ACPI/mptables have the same information about the
1287 * effective APIC id and update the package map.
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001288 */
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001289static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001290{
1291#ifdef CONFIG_SMP
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001292 unsigned int apicid, cpu = smp_processor_id();
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001293
1294 apicid = apic->cpu_present_to_apicid(cpu);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001295
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001296 if (apicid != c->apicid) {
1297 pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001298 cpu, apicid, c->initial_apicid);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001299 }
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001300 BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
Len Brown212bf4f2019-05-13 13:58:49 -04001301 BUG_ON(topology_update_die_map(c->cpu_die_id, cpu));
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001302#else
1303 c->logical_proc_id = 0;
1304#endif
1305}
1306
1307/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 * This does the hard work of actually picking apart the CPU stuff...
1309 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001310static void identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 int i;
1313
1314 c->loops_per_jiffy = loops_per_jiffy;
Gustavo A. R. Silva24dbc602018-02-13 13:22:08 -06001315 c->x86_cache_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 c->x86_vendor = X86_VENDOR_UNKNOWN;
Jia Zhangb3991512018-01-01 09:52:10 +08001317 c->x86_model = c->x86_stepping = 0; /* So far unknown... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 c->x86_vendor_id[0] = '\0'; /* Unset */
1319 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001320 c->x86_max_cores = 1;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001321 c->x86_coreid_bits = 0;
Borislav Petkov79a8b9a2017-02-05 11:50:21 +01001322 c->cu_id = 0xff;
Yinghai Lu11fdd252008-09-07 17:58:50 -07001323#ifdef CONFIG_X86_64
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001324 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +00001325 c->x86_phys_bits = 36;
1326 c->x86_virt_bits = 48;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001327#else
1328 c->cpuid_level = -1; /* CPUID not detected */
Andi Kleen770d1322006-12-07 02:14:05 +01001329 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +00001330 c->x86_phys_bits = 32;
1331 c->x86_virt_bits = 32;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001332#endif
1333 c->x86_cache_alignment = c->x86_clflush_size;
Jordan Borgner0e96f312018-10-28 12:58:28 +00001334 memset(&c->x86_capability, 0, sizeof(c->x86_capability));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 generic_identify(c);
1337
Andi Kleen38985342008-01-30 13:32:49 +01001338 if (this_cpu->c_identify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 this_cpu->c_identify(c);
1340
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001341 /* Clear/Set all flags overridden by options, after probe */
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -08001342 apply_forced_caps(c);
Yinghai Lu2759c322009-05-15 13:05:16 -07001343
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001344#ifdef CONFIG_X86_64
Ingo Molnarcb8cc442009-01-28 13:24:54 +01001345 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001346#endif
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /*
1349 * Vendor-specific initialization. In this section we
1350 * canonicalize the feature flags, meaning if there are
1351 * features a certain CPU supports which CPUID doesn't
1352 * tell us, CPUID claiming incorrect flags, or other bugs,
1353 * we handle them here.
1354 *
1355 * At the end of this section, c->x86_capability better
1356 * indicate the features this CPU genuinely supports!
1357 */
1358 if (this_cpu->c_init)
1359 this_cpu->c_init(c);
1360
1361 /* Disable the PN if appropriate */
1362 squash_the_stupid_serial_number(c);
1363
Ricardo Neriaa35f892017-11-05 18:27:54 -08001364 /* Set up SMEP/SMAP/UMIP */
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -07001365 setup_smep(c);
1366 setup_smap(c);
Ricardo Neriaa35f892017-11-05 18:27:54 -08001367 setup_umip(c);
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -07001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /*
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001370 * The vendor-specific functions might have changed features.
1371 * Now we do "generic changes."
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 */
1373
H. Peter Anvinb38b0662009-01-23 17:20:50 -08001374 /* Filter out anything that depends on CPUID levels we don't have */
1375 filter_cpuid_features(c, true);
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /* If the model name is still unset, do table lookup. */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001378 if (!c->x86_model_id[0]) {
Jan Beulich02dde8b2009-03-12 12:08:49 +00001379 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 p = table_lookup_model(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001381 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 strcpy(c->x86_model_id, p);
1383 else
1384 /* Last resort... */
1385 sprintf(c->x86_model_id, "%02x/%02x",
Chuck Ebbert54a20f82006-03-23 02:59:36 -08001386 c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001389#ifdef CONFIG_X86_64
1390 detect_ht(c);
1391#endif
1392
H. Peter Anvin49d859d2011-07-31 14:02:19 -07001393 x86_init_rdrand(c);
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001394 x86_init_cache_qos(c);
Dave Hansen06976942016-02-12 13:02:29 -08001395 setup_pku(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001396
1397 /*
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001398 * Clear/Set all flags overridden by options, need do it
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001399 * before following smp all cpus cap AND.
1400 */
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -08001401 apply_forced_caps(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /*
1404 * On SMP, boot_cpu_data holds the common feature set between
1405 * all CPUs; so make sure that we indicate which features are
1406 * common between the CPUs. The first time this routine gets
1407 * executed, c == &boot_cpu_data.
1408 */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001409 if (c != &boot_cpu_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /* AND the already accumulated flags with these */
Yinghai Lu9d31d352008-09-04 21:09:44 +02001411 for (i = 0; i < NCAPINTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
Borislav Petkov65fc9852013-03-20 15:07:23 +01001413
1414 /* OR, i.e. replicate the bug flags */
1415 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
1416 c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418
1419 /* Init Machine Check Exception if available. */
Borislav Petkov5e099542009-10-16 12:31:32 +02001420 mcheck_cpu_init(c);
Andi Kleen30d432d2008-01-30 13:33:16 +01001421
1422 select_idle_routine(c);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001423
Tejun Heode2d9442011-01-23 14:37:41 +01001424#ifdef CONFIG_NUMA
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001425 numa_add_cpu(smp_processor_id());
1426#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001427}
Shaohua Li31ab2692005-11-07 00:58:42 -08001428
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001429/*
1430 * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
1431 * on 32-bit kernels:
1432 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001433#ifdef CONFIG_X86_32
1434void enable_sep_cpu(void)
1435{
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001436 struct tss_struct *tss;
1437 int cpu;
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001438
Borislav Petkovb3edfda2016-03-16 13:19:29 +01001439 if (!boot_cpu_has(X86_FEATURE_SEP))
1440 return;
1441
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001442 cpu = get_cpu();
Andy Lutomirskic482fee2017-12-04 15:07:29 +01001443 tss = &per_cpu(cpu_tss_rw, cpu);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001444
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001445 /*
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001446 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1447 * see the big comment in struct x86_hw_tss's definition.
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001448 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001449
1450 tss->x86_tss.ss1 = __KERNEL_CS;
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001451 wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
Dave Hansen4fe2d8b2017-12-04 17:25:07 -08001452 wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0);
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001453 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001454
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001455 put_cpu();
1456}
Glauber Costae04d6452008-09-22 14:35:08 -03001457#endif
1458
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001459void __init identify_boot_cpu(void)
1460{
1461 identify_cpu(&boot_cpu_data);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001462#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001463 sysenter_setup();
Li Shaohua6fe940d2005-06-25 14:54:53 -07001464 enable_sep_cpu();
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001465#endif
Borislav Petkov5b5563322012-08-06 19:00:37 +02001466 cpu_detect_tlb(&boot_cpu_data);
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001467}
Shaohua Li3b520b22005-07-07 17:56:38 -07001468
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001469void identify_secondary_cpu(struct cpuinfo_x86 *c)
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001470{
1471 BUG_ON(c == &boot_cpu_data);
1472 identify_cpu(c);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001473#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001474 enable_sep_cpu();
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001475#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001476 mtrr_ap_init();
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001477 validate_apic_and_package_id(c);
Konrad Rzeszutek Wilk77243972018-04-25 22:04:22 -04001478 x86_spec_ctrl_setup_ap();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Andi Kleen191679f2008-01-30 13:33:21 +01001481static __init int setup_noclflush(char *arg)
1482{
H. Peter Anvin840d2832014-02-27 08:31:30 -08001483 setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
H. Peter Anvinda4aaa72014-02-27 08:36:31 -08001484 setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
Andi Kleen191679f2008-01-30 13:33:21 +01001485 return 1;
1486}
1487__setup("noclflush", setup_noclflush);
1488
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001489void print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Jan Beulich02dde8b2009-03-12 12:08:49 +00001491 const char *vendor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001493 if (c->x86_vendor < X86_VENDOR_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 vendor = this_cpu->c_vendor;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001495 } else {
1496 if (c->cpuid_level >= 0)
1497 vendor = c->x86_vendor_id;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Yinghai Lubd32a8cf2008-09-19 18:41:16 -07001500 if (vendor && !strstr(c->x86_model_id, vendor))
Chen Yucong1b74dde2016-02-02 11:45:02 +08001501 pr_cont("%s ", vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Yinghai Lu9d31d352008-09-04 21:09:44 +02001503 if (c->x86_model_id[0])
Chen Yucong1b74dde2016-02-02 11:45:02 +08001504 pr_cont("%s", c->x86_model_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001506 pr_cont("%d86", c->x86);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Chen Yucong1b74dde2016-02-02 11:45:02 +08001508 pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
Borislav Petkov924e1012012-09-14 18:37:46 +02001509
Jia Zhangb3991512018-01-01 09:52:10 +08001510 if (c->x86_stepping || c->cpuid_level >= 0)
1511 pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001513 pr_cont(")\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
Andi Kleen0c2a3912017-10-13 14:56:43 -07001516/*
1517 * clearcpuid= was already parsed in fpu__init_parse_early_param.
1518 * But we need to keep a dummy __setup around otherwise it would
1519 * show up as an environment variable for init.
1520 */
1521static __init int setup_clearcpuid(char *arg)
Andi Kleenac72e782008-01-30 13:33:21 +01001522{
Andi Kleenac72e782008-01-30 13:33:21 +01001523 return 1;
1524}
Andi Kleen0c2a3912017-10-13 14:56:43 -07001525__setup("clearcpuid=", setup_clearcpuid);
Andi Kleenac72e782008-01-30 13:33:21 +01001526
Yinghai Lud5494d42008-09-04 20:09:03 -07001527#ifdef CONFIG_X86_64
Andy Lutomirskie6401c12019-04-14 18:00:06 +02001528DEFINE_PER_CPU_FIRST(struct fixed_percpu_data,
1529 fixed_percpu_data) __aligned(PAGE_SIZE) __visible;
1530EXPORT_PER_CPU_SYMBOL_GPL(fixed_percpu_data);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001531
Tejun Heobdf977b2009-08-03 14:12:19 +09001532/*
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001533 * The following percpu variables are hot. Align current_task to
1534 * cacheline size such that they fall in the same cacheline.
Tejun Heobdf977b2009-08-03 14:12:19 +09001535 */
1536DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1537 &init_task;
1538EXPORT_PER_CPU_SYMBOL(current_task);
Yinghai Lud5494d42008-09-04 20:09:03 -07001539
Andy Lutomirskie6401c12019-04-14 18:00:06 +02001540DEFINE_PER_CPU(struct irq_stack *, hardirq_stack_ptr);
Andi Kleen277d5b42013-08-05 15:02:43 -07001541DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
Yinghai Lud5494d42008-09-04 20:09:03 -07001542
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001543DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1544EXPORT_PER_CPU_SYMBOL(__preempt_count);
1545
Yinghai Lud5494d42008-09-04 20:09:03 -07001546/* May not be marked __init: used by software suspend */
1547void syscall_init(void)
1548{
Borislav Petkov31ac34c2015-11-23 11:12:25 +01001549 wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
Andy Lutomirskibf904d22018-09-03 15:59:44 -07001550 wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001551
1552#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski47edb652015-07-23 12:14:40 -07001553 wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001554 /*
Denys Vlasenko487d1ed2015-03-27 11:59:16 +01001555 * This only works on Intel CPUs.
1556 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1557 * This does not cause SYSENTER to jump to the wrong location, because
1558 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001559 */
1560 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
zhong jiang8e6b65a2018-09-13 10:49:45 +08001561 wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
1562 (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1));
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001563 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001564#else
Andy Lutomirski47edb652015-07-23 12:14:40 -07001565 wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
Borislav Petkov6b513112015-04-03 14:25:28 +02001566 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001567 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1568 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
Yinghai Lud5494d42008-09-04 20:09:03 -07001569#endif
1570
1571 /* Flags to clear on syscall */
1572 wrmsrl(MSR_SYSCALL_MASK,
H. Peter Anvin63bcff22012-09-21 12:43:12 -07001573 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
Andy Lutomirski8c7aa692014-10-01 11:49:04 -07001574 X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
Yinghai Lud5494d42008-09-04 20:09:03 -07001575}
1576
Steven Rostedt42181182011-12-16 11:43:02 -05001577DEFINE_PER_CPU(int, debug_stack_usage);
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001578DEFINE_PER_CPU(u32, debug_idt_ctr);
Steven Rostedtf8988172012-05-30 11:47:00 -04001579
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001580void debug_stack_set_zero(void)
1581{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001582 this_cpu_inc(debug_idt_ctr);
1583 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001584}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001585NOKPROBE_SYMBOL(debug_stack_set_zero);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001586
1587void debug_stack_reset(void)
1588{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001589 if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
Steven Rostedtf8988172012-05-30 11:47:00 -04001590 return;
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001591 if (this_cpu_dec_return(debug_idt_ctr) == 0)
1592 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001593}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001594NOKPROBE_SYMBOL(debug_stack_reset);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001595
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001596#else /* CONFIG_X86_64 */
Yinghai Lud5494d42008-09-04 20:09:03 -07001597
Tejun Heobdf977b2009-08-03 14:12:19 +09001598DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1599EXPORT_PER_CPU_SYMBOL(current_task);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001600DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1601EXPORT_PER_CPU_SYMBOL(__preempt_count);
Tejun Heobdf977b2009-08-03 14:12:19 +09001602
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001603/*
1604 * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1605 * the top of the kernel stack. Use an extra percpu variable to track the
1606 * top of the kernel stack directly.
1607 */
1608DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1609 (unsigned long)&init_thread_union + THREAD_SIZE;
1610EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1611
Linus Torvalds050e9ba2018-06-14 12:21:18 +09001612#ifdef CONFIG_STACKPROTECTOR
Jeremy Fitzhardinge53f82452009-09-03 14:31:44 -07001613DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
Tejun Heo60a53172009-02-09 22:17:40 +09001614#endif
1615
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001616#endif /* CONFIG_X86_64 */
Jeremy Fitzhardingec5413fb2007-05-02 19:27:16 +02001617
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001618/*
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301619 * Clear all 6 debug registers:
1620 */
1621static void clear_all_debug_regs(void)
1622{
1623 int i;
1624
1625 for (i = 0; i < 8; i++) {
1626 /* Ignore db4, db5 */
1627 if ((i == 4) || (i == 5))
1628 continue;
1629
1630 set_debugreg(0, i);
1631 }
1632}
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +01001633
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001634#ifdef CONFIG_KGDB
1635/*
1636 * Restore debug regs if using kgdbwait and you have a kernel debugger
1637 * connection established.
1638 */
1639static void dbg_restore_debug_regs(void)
1640{
1641 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1642 arch_kgdb_ops.correct_hw_break();
1643}
1644#else /* ! CONFIG_KGDB */
1645#define dbg_restore_debug_regs()
1646#endif /* ! CONFIG_KGDB */
1647
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001648static void wait_for_master_cpu(int cpu)
1649{
1650#ifdef CONFIG_SMP
1651 /*
1652 * wait for ACK from master CPU before continuing
1653 * with AP initialization
1654 */
1655 WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1656 while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1657 cpu_relax();
1658#endif
1659}
1660
Chang S. Baeb2e2ba52018-09-18 16:08:59 -07001661#ifdef CONFIG_X86_64
1662static void setup_getcpu(int cpu)
1663{
Ingo Molnar22245bd2018-10-08 10:41:59 +02001664 unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
Chang S. Baeb2e2ba52018-09-18 16:08:59 -07001665 struct desc_struct d = { };
1666
Borislav Petkov67e87d42019-03-29 19:52:59 +01001667 if (boot_cpu_has(X86_FEATURE_RDTSCP))
Chang S. Baeb2e2ba52018-09-18 16:08:59 -07001668 write_rdtscp_aux(cpudata);
1669
1670 /* Store CPU and node number in limit. */
1671 d.limit0 = cpudata;
1672 d.limit1 = cpudata >> 16;
1673
1674 d.type = 5; /* RO data, expand down, accessed */
1675 d.dpl = 3; /* Visible to user code */
1676 d.s = 1; /* Not a system segment */
1677 d.p = 1; /* Present */
1678 d.d = 1; /* 32-bit */
1679
Ingo Molnar22245bd2018-10-08 10:41:59 +02001680 write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_CPUNODE, &d, DESCTYPE_S);
Chang S. Baeb2e2ba52018-09-18 16:08:59 -07001681}
1682#endif
1683
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001684/*
1685 * cpu_init() initializes state that is per-CPU. Some data is already
1686 * initialized (naturally) in the bootstrap process, such as the GDT
1687 * and IDT. We reload them nevertheless, this function acts as a
1688 * 'CPU state barrier', nothing should get across.
1689 */
Yinghai Lu1ba76582008-09-04 20:09:04 -07001690#ifdef CONFIG_X86_64
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001691
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001692void cpu_init(void)
Yinghai Lu1ba76582008-09-04 20:09:04 -07001693{
Thomas Gleixnerf6ef7322019-04-14 17:59:53 +02001694 int cpu = raw_smp_processor_id();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001695 struct task_struct *me;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001696 struct tss_struct *t;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001697 int i;
1698
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001699 wait_for_master_cpu(cpu);
1700
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001701 /*
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001702 * Initialize the CR4 shadow before doing anything that could
1703 * try to read it.
1704 */
1705 cr4_init_shadow();
1706
Borislav Petkov777284b2016-10-25 11:55:11 +02001707 if (cpu)
1708 load_ucode_ap();
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001709
Andy Lutomirskic482fee2017-12-04 15:07:29 +01001710 t = &per_cpu(cpu_tss_rw, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001711
Brian Gerste7a22c12009-01-19 00:38:59 +09001712#ifdef CONFIG_NUMA
Fenghua Yu27fd1852012-11-13 11:32:47 -08001713 if (this_cpu_read(numa_node) == 0 &&
Lee Schermerhorne534c7c2010-05-26 14:44:58 -07001714 early_cpu_to_node(cpu) != NUMA_NO_NODE)
1715 set_numa_node(early_cpu_to_node(cpu));
Brian Gerste7a22c12009-01-19 00:38:59 +09001716#endif
Chang S. Baeb2e2ba52018-09-18 16:08:59 -07001717 setup_getcpu(cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001718
1719 me = current;
1720
Mike Travis2eaad1f2009-12-10 17:19:36 -08001721 pr_debug("Initializing CPU#%d\n", cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001722
Andy Lutomirski375074c2014-10-24 15:58:07 -07001723 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001724
1725 /*
1726 * Initialize the per-CPU GDT with the boot GDT,
1727 * and set up the GDT descriptor:
1728 */
1729
Brian Gerst552be872009-01-30 17:47:53 +09001730 switch_to_new_gdt(cpu);
Brian Gerst2697fbd2009-01-27 12:56:48 +09001731 loadsegment(fs, 0);
1732
Seiji Aguchicf910e82013-06-20 11:46:53 -04001733 load_current_idt();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001734
1735 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1736 syscall_init();
1737
1738 wrmsrl(MSR_FS_BASE, 0);
1739 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1740 barrier();
1741
H. Peter Anvin4763ed42009-11-13 15:28:16 -08001742 x86_configure_nx();
Thomas Gleixner659006b2015-01-15 21:22:26 +00001743 x2apic_setup();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001744
1745 /*
1746 * set up and load the per-CPU TSS
1747 */
Thomas Gleixnerf6ef7322019-04-14 17:59:53 +02001748 if (!t->x86_tss.ist[0]) {
Thomas Gleixner32074262019-04-14 17:59:55 +02001749 t->x86_tss.ist[IST_INDEX_DF] = __this_cpu_ist_top_va(DF);
1750 t->x86_tss.ist[IST_INDEX_NMI] = __this_cpu_ist_top_va(NMI);
1751 t->x86_tss.ist[IST_INDEX_DB] = __this_cpu_ist_top_va(DB);
1752 t->x86_tss.ist[IST_INDEX_MCE] = __this_cpu_ist_top_va(MCE);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001753 }
1754
Andy Lutomirski7fb983b2017-12-04 15:07:17 +01001755 t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001756
Yinghai Lu1ba76582008-09-04 20:09:04 -07001757 /*
1758 * <= is required because the CPU will access up to
1759 * 8 bits beyond the end of the IO permission bitmap.
1760 */
1761 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1762 t->io_bitmap[i] = ~0UL;
1763
Vegard Nossumf1f10072017-02-27 14:30:07 -08001764 mmgrab(&init_mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001765 me->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001766 BUG_ON(me->mm);
Andy Lutomirski72c00982017-09-06 19:54:53 -07001767 initialize_tlbstate_and_flush();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001768 enter_lazy_tlb(&init_mm, me);
1769
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001770 /*
Andy Lutomirski7f2590a2017-12-04 15:07:23 +01001771 * Initialize the TSS. sp0 points to the entry trampoline stack
1772 * regardless of what task is running.
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001773 */
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001774 set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001775 load_TR_desc();
Dave Hansen4fe2d8b2017-12-04 17:25:07 -08001776 load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001777
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001778 load_mm_ldt(&init_mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001779
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001780 clear_all_debug_regs();
1781 dbg_restore_debug_regs();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001782
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001783 fpu__init_cpu();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001784
Yinghai Lu1ba76582008-09-04 20:09:04 -07001785 if (is_uv_system())
1786 uv_cpu_init();
Thomas Garnier69218e42017-03-14 10:05:07 -07001787
Thomas Garnier69218e42017-03-14 10:05:07 -07001788 load_fixmap_gdt(cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001789}
1790
1791#else
1792
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001793void cpu_init(void)
James Bottomley9ee79a32007-01-22 09:18:31 -06001794{
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001795 int cpu = smp_processor_id();
1796 struct task_struct *curr = current;
Andy Lutomirskic482fee2017-12-04 15:07:29 +01001797 struct tss_struct *t = &per_cpu(cpu_tss_rw, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001799 wait_for_master_cpu(cpu);
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001800
Steven Rostedt5b2bdbc2015-02-27 14:50:19 -05001801 /*
1802 * Initialize the CR4 shadow before doing anything that could
1803 * try to read it.
1804 */
1805 cr4_init_shadow();
1806
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001807 show_ucode_info_early();
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001808
Chen Yucong1b74dde2016-02-02 11:45:02 +08001809 pr_info("Initializing CPU#%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Borislav Petkov362f9242015-12-07 10:39:41 +01001811 if (cpu_feature_enabled(X86_FEATURE_VME) ||
Borislav Petkov59e21e32016-04-04 22:24:59 +02001812 boot_cpu_has(X86_FEATURE_TSC) ||
Borislav Petkov362f9242015-12-07 10:39:41 +01001813 boot_cpu_has(X86_FEATURE_DE))
Andy Lutomirski375074c2014-10-24 15:58:07 -07001814 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Seiji Aguchicf910e82013-06-20 11:46:53 -04001816 load_current_idt();
Brian Gerst552be872009-01-30 17:47:53 +09001817 switch_to_new_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 * Set up and load the per-CPU TSS and LDT
1821 */
Vegard Nossumf1f10072017-02-27 14:30:07 -08001822 mmgrab(&init_mm);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001823 curr->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001824 BUG_ON(curr->mm);
Andy Lutomirski72c00982017-09-06 19:54:53 -07001825 initialize_tlbstate_and_flush();
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001826 enter_lazy_tlb(&init_mm, curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001828 /*
Joerg Roedel45d7b252018-07-18 11:40:44 +02001829 * Initialize the TSS. sp0 points to the entry trampoline stack
1830 * regardless of what task is running.
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001831 */
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001832 set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 load_TR_desc();
Joerg Roedel45d7b252018-07-18 11:40:44 +02001834 load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001835
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001836 load_mm_ldt(&init_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Andy Lutomirski7fb983b2017-12-04 15:07:17 +01001838 t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
Thomas Gleixnerf9a196b2009-05-01 20:59:25 +02001839
Matt Mackall22c4e302006-01-08 01:05:24 -08001840#ifdef CONFIG_DOUBLEFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 /* Set up doublefault TSS pointer in the GDT */
1842 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
Matt Mackall22c4e302006-01-08 01:05:24 -08001843#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301845 clear_all_debug_regs();
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001846 dbg_restore_debug_regs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001848 fpu__init_cpu();
Thomas Garnier69218e42017-03-14 10:05:07 -07001849
Thomas Garnier69218e42017-03-14 10:05:07 -07001850 load_fixmap_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
Yinghai Lu1ba76582008-09-04 20:09:04 -07001852#endif
Borislav Petkov5700f742013-06-09 12:07:32 +02001853
Borislav Petkov1008c52c2018-02-16 12:26:39 +01001854/*
1855 * The microcode loader calls this upon late microcode load to recheck features,
1856 * only when microcode has been updated. Caller holds microcode_mutex and CPU
1857 * hotplug lock.
1858 */
1859void microcode_check(void)
1860{
Borislav Petkov42ca8082018-02-16 12:26:40 +01001861 struct cpuinfo_x86 info;
1862
Borislav Petkov1008c52c2018-02-16 12:26:39 +01001863 perf_check_microcode();
Borislav Petkov42ca8082018-02-16 12:26:40 +01001864
1865 /* Reload CPUID max function as it might've changed. */
1866 info.cpuid_level = cpuid_eax(0);
1867
1868 /*
1869 * Copy all capability leafs to pick up the synthetic ones so that
1870 * memcmp() below doesn't fail on that. The ones coming from CPUID will
1871 * get overwritten in get_cpu_cap().
1872 */
1873 memcpy(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability));
1874
1875 get_cpu_cap(&info);
1876
1877 if (!memcmp(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability)))
1878 return;
1879
1880 pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
1881 pr_warn("x86/CPU: Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
Borislav Petkov1008c52c2018-02-16 12:26:39 +01001882}