blob: f490a4fab2f71d8b2c692ae94aea201921b2262c [file] [log] [blame]
Yinghai Luf0fc4af2008-09-04 20:09:00 -07001#include <linux/bootmem.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05302#include <linux/linkage.h>
Yinghai Luf0fc4af2008-09-04 20:09:00 -07003#include <linux/bitops.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05304#include <linux/kernel.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -04005#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/percpu.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05307#include <linux/string.h>
Borislav Petkovee098e12015-06-01 12:06:57 +02008#include <linux/ctype.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05309#include <linux/delay.h>
10#include <linux/sched.h>
11#include <linux/init.h>
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +090012#include <linux/kprobes.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053013#include <linux/kgdb.h>
14#include <linux/smp.h>
15#include <linux/io.h>
Laura Abbottb51ef522015-07-20 14:47:58 -070016#include <linux/syscore_ops.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053017
18#include <asm/stackprotector.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020019#include <asm/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/mmu_context.h>
H. Peter Anvin49d859d2011-07-31 14:02:19 -070021#include <asm/archrandom.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053022#include <asm/hypervisor.h>
23#include <asm/processor.h>
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070024#include <asm/tlbflush.h>
Paul Gortmakerf649e932012-01-20 16:24:09 -050025#include <asm/debugreg.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053026#include <asm/sections.h>
Andy Lutomirskif40c3302014-05-05 12:19:36 -070027#include <asm/vsyscall.h>
Alan Cox8bdbd962009-07-04 00:35:45 +010028#include <linux/topology.h>
29#include <linux/cpumask.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053030#include <asm/pgtable.h>
Arun Sharma600634972011-07-26 16:09:06 -070031#include <linux/atomic.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053032#include <asm/proto.h>
33#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/apic.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053035#include <asm/desc.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020036#include <asm/fpu/internal.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053037#include <asm/mtrr.h>
Alan Cox8bdbd962009-07-04 00:35:45 +010038#include <linux/numa.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053039#include <asm/asm.h>
Dave Hansen0f6ff2b2016-05-12 15:04:00 -070040#include <asm/bugs.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053041#include <asm/cpu.h>
42#include <asm/mce.h>
43#include <asm/msr.h>
44#include <asm/pat.h>
Fenghua Yud288e1c2012-12-20 23:44:23 -080045#include <asm/microcode.h>
46#include <asm/microcode_intel.h>
David Woodhousea8799fd2018-01-25 16:14:13 +000047#include <asm/intel-family.h>
48#include <asm/cpu_device_id.h>
Ingo Molnare641f5f2009-02-17 14:02:01 +010049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifdef CONFIG_X86_LOCAL_APIC
Tejun Heobdbcdd42009-01-21 17:26:06 +090051#include <asm/uv/uv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#endif
53
54#include "cpu.h"
55
Mike Travisc2d1cec2009-01-04 05:18:03 -080056/* all of these masks are initialized in setup_cpu_local_masks() */
Mike Travisc2d1cec2009-01-04 05:18:03 -080057cpumask_var_t cpu_initialized_mask;
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053058cpumask_var_t cpu_callout_mask;
59cpumask_var_t cpu_callin_mask;
Mike Travisc2d1cec2009-01-04 05:18:03 -080060
61/* representing cpus for which sibling maps can be computed */
62cpumask_var_t cpu_sibling_setup_mask;
63
Borislav Petkov7f5d0902018-04-27 16:34:34 -050064/* Number of siblings per CPU package */
65int smp_num_siblings = 1;
66EXPORT_SYMBOL(smp_num_siblings);
67
68/* Last level cache ID of each logical CPU */
69DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
70
Brian Gerst2f2f52b2009-01-27 12:56:47 +090071/* correctly size the local cpu masks */
Ingo Molnar4369f1f2009-01-27 12:03:24 +010072void __init setup_cpu_local_masks(void)
Brian Gerst2f2f52b2009-01-27 12:56:47 +090073{
74 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
75 alloc_bootmem_cpumask_var(&cpu_callin_mask);
76 alloc_bootmem_cpumask_var(&cpu_callout_mask);
77 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
78}
79
Paul Gortmaker148f9bb2013-06-18 18:23:59 -040080static void default_init(struct cpuinfo_x86 *c)
Ondrej Zarye8055132009-08-11 20:00:11 +020081{
82#ifdef CONFIG_X86_64
Borislav Petkov27c13ec2009-11-21 14:01:45 +010083 cpu_detect_cache_sizes(c);
Ondrej Zarye8055132009-08-11 20:00:11 +020084#else
85 /* Not much we can do here... */
86 /* Check if at least it has cpuid */
87 if (c->cpuid_level == -1) {
88 /* No cpuid. It must be an ancient CPU */
89 if (c->x86 == 4)
90 strcpy(c->x86_model_id, "486");
91 else if (c->x86 == 3)
92 strcpy(c->x86_model_id, "386");
93 }
94#endif
95}
96
Paul Gortmaker148f9bb2013-06-18 18:23:59 -040097static const struct cpu_dev default_cpu = {
Ondrej Zarye8055132009-08-11 20:00:11 +020098 .c_init = default_init,
99 .c_vendor = "Unknown",
100 .c_x86_vendor = X86_VENDOR_UNKNOWN,
101};
102
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400103static const struct cpu_dev *this_cpu = &default_cpu;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200104
Richard Fellner13be4482017-05-04 14:26:50 +0200105DEFINE_PER_CPU_PAGE_ALIGNED_USER_MAPPED(struct gdt_page, gdt_page) = { .gdt = {
Yinghai Lu950ad7f2008-09-04 20:09:01 -0700106#ifdef CONFIG_X86_64
Brian Gerst06deef82009-01-21 17:26:05 +0900107 /*
108 * We need valid kernel segments for data and code in long mode too
109 * IRET will check the segment types kkeil 2000/10/28
110 * Also sysret mandates a special GDT layout
111 *
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530112 * TLS descriptors are currently at a different place compared to i386.
Brian Gerst06deef82009-01-21 17:26:05 +0900113 * Hopefully nobody expects them at a fixed place (Wine?)
114 */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900115 [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
116 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
117 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
118 [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
119 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
120 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
Yinghai Lu950ad7f2008-09-04 20:09:01 -0700121#else
Akinobu Mita1e5de182009-07-19 00:12:20 +0900122 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
123 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
124 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
125 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200126 /*
127 * Segments used for calling PnP BIOS have byte granularity.
128 * They code segments and data segments have fixed 64k limits,
129 * the transfer segment sizes are set at run time.
130 */
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100131 /* 32-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900132 [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100133 /* 16-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900134 [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100135 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900136 [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100137 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900138 [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100139 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900140 [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
Rusty Russellbf5046722007-05-02 19:27:10 +0200141 /*
142 * The APM segments have byte granularity and their bases
143 * are set at run time. All have 64k limits.
144 */
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100145 /* 32-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900146 [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200147 /* 16-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900148 [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100149 /* data */
Ingo Molnar72c4d852009-08-03 08:47:07 +0200150 [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200151
Akinobu Mita1e5de182009-07-19 00:12:20 +0900152 [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
153 [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
Tejun Heo60a53172009-02-09 22:17:40 +0900154 GDT_STACK_CANARY_INIT
Yinghai Lu950ad7f2008-09-04 20:09:01 -0700155#endif
Brian Gerst06deef82009-01-21 17:26:05 +0900156} };
Jeremy Fitzhardinge7a61d352007-05-02 19:27:15 +0200157EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
Rusty Russellae1ee112007-05-02 19:27:10 +0200158
Dave Hansen8c3641e2015-06-07 11:37:02 -0700159static int __init x86_mpx_setup(char *s)
Suresh Siddha0c752a92009-05-22 12:17:45 -0700160{
Dave Hansen8c3641e2015-06-07 11:37:02 -0700161 /* require an exact match without trailing characters */
Dave Hansen2cd39492014-11-11 14:01:33 -0800162 if (strlen(s))
163 return 0;
Suresh Siddha0c752a92009-05-22 12:17:45 -0700164
Dave Hansen8c3641e2015-06-07 11:37:02 -0700165 /* do not emit a message if the feature is not present */
166 if (!boot_cpu_has(X86_FEATURE_MPX))
167 return 1;
Suresh Siddha6bad06b2010-07-19 16:05:52 -0700168
Dave Hansen8c3641e2015-06-07 11:37:02 -0700169 setup_clear_cpu_cap(X86_FEATURE_MPX);
170 pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
Fenghua Yub6f42a42014-05-29 11:12:31 -0700171 return 1;
172}
Dave Hansen8c3641e2015-06-07 11:37:02 -0700173__setup("nompx", x86_mpx_setup);
Fenghua Yub6f42a42014-05-29 11:12:31 -0700174
Andy Lutomirskie6a29322017-06-29 08:53:20 -0700175#ifdef CONFIG_X86_64
176static int __init x86_pcid_setup(char *s)
177{
178 /* require an exact match without trailing characters */
179 if (strlen(s))
180 return 0;
181
182 /* do not emit a message if the feature is not present */
183 if (!boot_cpu_has(X86_FEATURE_PCID))
184 return 1;
185
186 setup_clear_cpu_cap(X86_FEATURE_PCID);
187 pr_info("nopcid: PCID feature disabled\n");
188 return 1;
189}
190__setup("nopcid", x86_pcid_setup);
191#endif
192
Andy Lutomirskid12a72b2016-01-29 11:42:58 -0800193static int __init x86_noinvpcid_setup(char *s)
194{
195 /* noinvpcid doesn't accept parameters */
196 if (s)
197 return -EINVAL;
198
199 /* do not emit a message if the feature is not present */
200 if (!boot_cpu_has(X86_FEATURE_INVPCID))
201 return 0;
202
203 setup_clear_cpu_cap(X86_FEATURE_INVPCID);
204 pr_info("noinvpcid: INVPCID feature disabled\n");
205 return 0;
206}
207early_param("noinvpcid", x86_noinvpcid_setup);
208
Yinghai Luba51dce2008-09-04 20:09:02 -0700209#ifdef CONFIG_X86_32
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400210static int cachesize_override = -1;
211static int disable_x86_serial_nr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static int __init cachesize_setup(char *str)
214{
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100215 get_option(&str, &cachesize_override);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 1;
217}
218__setup("cachesize=", cachesize_setup);
219
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100220static int __init x86_sep_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Andi Kleen13530252008-01-30 13:33:20 +0100222 setup_clear_cpu_cap(X86_FEATURE_SEP);
Chuck Ebbert4f886512006-03-23 02:59:34 -0800223 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
Chuck Ebbert4f886512006-03-23 02:59:34 -0800225__setup("nosep", x86_sep_setup);
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/* Standard macro to see if a specific flag is changeable */
228static inline int flag_is_changeable_p(u32 flag)
229{
230 u32 f1, f2;
231
Krzysztof Helt94f6bac2008-09-30 23:17:51 +0200232 /*
233 * Cyrix and IDT cpus allow disabling of CPUID
234 * so the code below may return different results
235 * when it is executed before and after enabling
236 * the CPUID. Add "volatile" to not allow gcc to
237 * optimize the subsequent calls to this function.
238 */
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100239 asm volatile ("pushfl \n\t"
240 "pushfl \n\t"
241 "popl %0 \n\t"
242 "movl %0, %1 \n\t"
243 "xorl %2, %0 \n\t"
244 "pushl %0 \n\t"
245 "popfl \n\t"
246 "pushfl \n\t"
247 "popl %0 \n\t"
248 "popfl \n\t"
249
Krzysztof Helt94f6bac2008-09-30 23:17:51 +0200250 : "=&r" (f1), "=&r" (f2)
251 : "ir" (flag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 return ((f1^f2) & flag) != 0;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/* Probe for the CPUID instruction */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400257int have_cpuid_p(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 return flag_is_changeable_p(X86_EFLAGS_ID);
260}
261
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400262static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
Yinghai Lu0a488a52008-09-04 21:09:47 +0200263{
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100264 unsigned long lo, hi;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200265
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100266 if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
267 return;
268
269 /* Disable processor serial number: */
270
271 rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
272 lo |= 0x200000;
273 wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
274
Chen Yucong1b74dde2016-02-02 11:45:02 +0800275 pr_notice("CPU serial number disabled.\n");
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100276 clear_cpu_cap(c, X86_FEATURE_PN);
277
278 /* Disabling the serial number may affect the cpuid level */
279 c->cpuid_level = cpuid_eax(0);
Yinghai Lu0a488a52008-09-04 21:09:47 +0200280}
281
282static int __init x86_serial_nr_setup(char *s)
283{
284 disable_x86_serial_nr = 0;
285 return 1;
286}
287__setup("serialnumber", x86_serial_nr_setup);
Yinghai Luba51dce2008-09-04 20:09:02 -0700288#else
Yinghai Lu102bbe32008-09-04 20:09:13 -0700289static inline int flag_is_changeable_p(u32 flag)
290{
291 return 1;
292}
Yinghai Lu102bbe32008-09-04 20:09:13 -0700293static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
294{
295}
Yinghai Luba51dce2008-09-04 20:09:02 -0700296#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Fenghua Yude5397a2011-05-11 16:51:05 -0700298static __init int setup_disable_smep(char *arg)
299{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700300 setup_clear_cpu_cap(X86_FEATURE_SMEP);
Dave Hansen0f6ff2b2016-05-12 15:04:00 -0700301 /* Check for things that depend on SMEP being enabled: */
302 check_mpx_erratum(&boot_cpu_data);
Fenghua Yude5397a2011-05-11 16:51:05 -0700303 return 1;
304}
305__setup("nosmep", setup_disable_smep);
306
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700307static __always_inline void setup_smep(struct cpuinfo_x86 *c)
Fenghua Yude5397a2011-05-11 16:51:05 -0700308{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700309 if (cpu_has(c, X86_FEATURE_SMEP))
Andy Lutomirski375074c2014-10-24 15:58:07 -0700310 cr4_set_bits(X86_CR4_SMEP);
Fenghua Yude5397a2011-05-11 16:51:05 -0700311}
312
H. Peter Anvin52b61792012-09-21 12:43:13 -0700313static __init int setup_disable_smap(char *arg)
314{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700315 setup_clear_cpu_cap(X86_FEATURE_SMAP);
H. Peter Anvin52b61792012-09-21 12:43:13 -0700316 return 1;
317}
318__setup("nosmap", setup_disable_smap);
319
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700320static __always_inline void setup_smap(struct cpuinfo_x86 *c)
H. Peter Anvin52b61792012-09-21 12:43:13 -0700321{
Andrew Cooper581b7f152015-06-03 10:31:14 +0100322 unsigned long eflags = native_save_fl();
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700323
324 /* This should have been cleared long ago */
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700325 BUG_ON(eflags & X86_EFLAGS_AC);
326
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800327 if (cpu_has(c, X86_FEATURE_SMAP)) {
328#ifdef CONFIG_X86_SMAP
Andy Lutomirski375074c2014-10-24 15:58:07 -0700329 cr4_set_bits(X86_CR4_SMAP);
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800330#else
Andy Lutomirski375074c2014-10-24 15:58:07 -0700331 cr4_clear_bits(X86_CR4_SMAP);
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800332#endif
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Andy Lutomirskib52f9372017-06-29 08:53:21 -0700336static void setup_pcid(struct cpuinfo_x86 *c)
337{
338 if (cpu_has(c, X86_FEATURE_PCID)) {
Hugh Dickins23e09432017-09-24 16:59:49 -0700339 if (cpu_has(c, X86_FEATURE_PGE) || kaiser_enabled) {
Andy Lutomirskib52f9372017-06-29 08:53:21 -0700340 cr4_set_bits(X86_CR4_PCIDE);
Hugh Dickins2684b122017-08-30 16:23:00 -0700341 /*
Hugh Dickins2684b122017-08-30 16:23:00 -0700342 * INVPCID has two "groups" of types:
343 * 1/2: Invalidate an individual address
344 * 3/4: Invalidate all contexts
345 *
346 * 1/2 take a PCID, but 3/4 do not. So, 3/4
347 * ignore the PCID argument in the descriptor.
348 * But, we have to be careful not to call 1/2
349 * with an actual non-zero PCID in them before
350 * we do the above cr4_set_bits().
351 */
352 if (cpu_has(c, X86_FEATURE_INVPCID))
353 set_cpu_cap(c, X86_FEATURE_INVPCID_SINGLE);
Andy Lutomirskib52f9372017-06-29 08:53:21 -0700354 } else {
355 /*
356 * flush_tlb_all(), as currently implemented, won't
357 * work if PCID is on but PGE is not. Since that
358 * combination doesn't exist on real hardware, there's
359 * no reason to try to fully support it, but it's
360 * polite to avoid corrupting data if we're on
361 * an improperly configured VM.
362 */
363 clear_cpu_cap(c, X86_FEATURE_PCID);
364 }
365 }
Hugh Dickins0b5ca9d2017-08-17 15:00:37 -0700366 kaiser_setup_pcid();
Andy Lutomirskib52f9372017-06-29 08:53:21 -0700367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/*
Dave Hansen06976942016-02-12 13:02:29 -0800370 * Protection Keys are not available in 32-bit mode.
371 */
372static bool pku_disabled;
373
374static __always_inline void setup_pku(struct cpuinfo_x86 *c)
375{
Dave Hansene8df1a952016-05-13 15:13:28 -0700376 /* check the boot processor, plus compile options for PKU: */
377 if (!cpu_feature_enabled(X86_FEATURE_PKU))
378 return;
379 /* checks the actual processor's cpuid bits: */
Dave Hansen06976942016-02-12 13:02:29 -0800380 if (!cpu_has(c, X86_FEATURE_PKU))
381 return;
382 if (pku_disabled)
383 return;
384
385 cr4_set_bits(X86_CR4_PKE);
386 /*
387 * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
388 * cpuid bit to be set. We need to ensure that we
389 * update that bit in this CPU's "cpu_info".
390 */
Sean Christopherson83057802020-02-26 15:16:15 -0800391 set_cpu_cap(c, X86_FEATURE_OSPKE);
Dave Hansen06976942016-02-12 13:02:29 -0800392}
393
394#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
395static __init int setup_disable_pku(char *arg)
396{
397 /*
398 * Do not clear the X86_FEATURE_PKU bit. All of the
399 * runtime checks are against OSPKE so clearing the
400 * bit does nothing.
401 *
402 * This way, we will see "pku" in cpuinfo, but not
403 * "ospke", which is exactly what we want. It shows
404 * that the CPU has PKU, but the OS has not enabled it.
405 * This happens to be exactly how a system would look
406 * if we disabled the config option.
407 */
408 pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
409 pku_disabled = true;
410 return 1;
411}
412__setup("nopku", setup_disable_pku);
413#endif /* CONFIG_X86_64 */
414
415/*
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800416 * Some CPU features depend on higher CPUID levels, which may not always
417 * be available due to CPUID level capping or broken virtualization
418 * software. Add those features to this table to auto-disable them.
419 */
420struct cpuid_dependent_feature {
421 u32 feature;
422 u32 level;
423};
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100424
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400425static const struct cpuid_dependent_feature
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800426cpuid_dependent_features[] = {
427 { X86_FEATURE_MWAIT, 0x00000005 },
428 { X86_FEATURE_DCA, 0x00000009 },
429 { X86_FEATURE_XSAVE, 0x0000000d },
430 { 0, 0 }
431};
432
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400433static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800434{
435 const struct cpuid_dependent_feature *df;
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530436
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800437 for (df = cpuid_dependent_features; df->feature; df++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100438
439 if (!cpu_has(c, df->feature))
440 continue;
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800441 /*
442 * Note: cpuid_level is set to -1 if unavailable, but
443 * extended_extended_level is set to 0 if unavailable
444 * and the legitimate extended levels are all negative
445 * when signed; hence the weird messing around with
446 * signs here...
447 */
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100448 if (!((s32)df->level < 0 ?
Yinghai Luf6db44d2009-02-14 23:59:18 -0800449 (u32)df->level > (u32)c->extended_cpuid_level :
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100450 (s32)df->level > (s32)c->cpuid_level))
451 continue;
452
453 clear_cpu_cap(c, df->feature);
454 if (!warn)
455 continue;
456
Chen Yucong1b74dde2016-02-02 11:45:02 +0800457 pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
458 x86_cap_flag(df->feature), df->level);
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800459 }
Yinghai Luf6db44d2009-02-14 23:59:18 -0800460}
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800461
462/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * Naming convention should be: <Name> [(<Codename>)]
464 * This table only is used unless init_<vendor>() below doesn't set it;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100465 * in particular, if CPUID levels 0x80000002..4 are supported, this
466 * isn't used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
468
469/* Look up CPU names by table lookup. */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400470static const char *table_lookup_model(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Jan Beulich09dc68d2013-10-21 09:35:20 +0100472#ifdef CONFIG_X86_32
473 const struct legacy_cpu_model_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 if (c->x86_model >= 16)
476 return NULL; /* Range check */
477
478 if (!this_cpu)
479 return NULL;
480
Jan Beulich09dc68d2013-10-21 09:35:20 +0100481 info = this_cpu->legacy_models;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Jan Beulich09dc68d2013-10-21 09:35:20 +0100483 while (info->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (info->family == c->x86)
485 return info->model_names[c->x86_model];
486 info++;
487 }
Jan Beulich09dc68d2013-10-21 09:35:20 +0100488#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return NULL; /* Not found */
490}
491
Thomas Gleixnerc2cacde2017-12-04 15:07:32 +0100492__u32 cpu_caps_cleared[NCAPINTS + NBUGINTS];
493__u32 cpu_caps_set[NCAPINTS + NBUGINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900495void load_percpu_segment(int cpu)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200496{
Yinghai Lufab334c2008-09-04 20:09:05 -0700497#ifdef CONFIG_X86_32
Brian Gerst2697fbd2009-01-27 12:56:48 +0900498 loadsegment(fs, __KERNEL_PERCPU);
499#else
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700500 __loadsegment_simple(gs, 0);
Brian Gerst2697fbd2009-01-27 12:56:48 +0900501 wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
Yinghai Lufab334c2008-09-04 20:09:05 -0700502#endif
Tejun Heo60a53172009-02-09 22:17:40 +0900503 load_stack_canary_segment();
Yinghai Lu9d31d352008-09-04 21:09:44 +0200504}
505
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100506/*
507 * Current gdt points %fs at the "master" per-cpu area: after this,
508 * it's on the real one.
509 */
Brian Gerst552be872009-01-30 17:47:53 +0900510void switch_to_new_gdt(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct desc_ptr gdt_descr;
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 gdt_descr.address = (long)get_cpu_gdt_table(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 gdt_descr.size = GDT_SIZE - 1;
516 load_gdt(&gdt_descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* Reload the per-cpu base */
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900518
519 load_percpu_segment(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400522static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400524static void get_model_name(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 unsigned int *v;
Borislav Petkovee098e12015-06-01 12:06:57 +0200527 char *p, *q, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Yinghai Lu3da99c92008-09-04 21:09:44 +0200529 if (c->extended_cpuid_level < 0x80000004)
Yinghai Lu1b05d602008-09-06 01:52:27 -0700530 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100532 v = (unsigned int *)c->x86_model_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
534 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
535 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
536 c->x86_model_id[48] = 0;
537
Borislav Petkovee098e12015-06-01 12:06:57 +0200538 /* Trim whitespace */
539 p = q = s = &c->x86_model_id[0];
540
541 while (*p == ' ')
542 p++;
543
544 while (*p) {
545 /* Note the last non-whitespace index */
546 if (!isspace(*p))
547 s = q;
548
549 *q++ = *p++;
550 }
551
552 *(s + 1) = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400555void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Yinghai Lu9d31d352008-09-04 21:09:44 +0200557 unsigned int n, dummy, ebx, ecx, edx, l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Yinghai Lu3da99c92008-09-04 21:09:44 +0200559 n = c->extended_cpuid_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (n >= 0x80000005) {
Yinghai Lu9d31d352008-09-04 21:09:44 +0200562 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
Yinghai Lu9d31d352008-09-04 21:09:44 +0200563 c->x86_cache_size = (ecx>>24) + (edx>>24);
Yinghai Lu140fc722008-09-04 20:09:07 -0700564#ifdef CONFIG_X86_64
565 /* On K8 L1 TLB is inclusive, so don't count it */
566 c->x86_tlbsize = 0;
567#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
570 if (n < 0x80000006) /* Some chips just has a large L1. */
571 return;
572
Yinghai Lu0a488a52008-09-04 21:09:47 +0200573 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 l2size = ecx >> 16;
575
Yinghai Lu140fc722008-09-04 20:09:07 -0700576#ifdef CONFIG_X86_64
577 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
578#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* do processor-specific cache resizing */
Jan Beulich09dc68d2013-10-21 09:35:20 +0100580 if (this_cpu->legacy_cache_size)
581 l2size = this_cpu->legacy_cache_size(c, l2size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* Allow user to override all this if necessary. */
584 if (cachesize_override != -1)
585 l2size = cachesize_override;
586
587 if (l2size == 0)
588 return; /* Again, no L2 cache is possible */
Yinghai Lu140fc722008-09-04 20:09:07 -0700589#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 c->x86_cache_size = l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Alex Shie0ba94f2012-06-28 09:02:16 +0800594u16 __read_mostly tlb_lli_4k[NR_INFO];
595u16 __read_mostly tlb_lli_2m[NR_INFO];
596u16 __read_mostly tlb_lli_4m[NR_INFO];
597u16 __read_mostly tlb_lld_4k[NR_INFO];
598u16 __read_mostly tlb_lld_2m[NR_INFO];
599u16 __read_mostly tlb_lld_4m[NR_INFO];
Kirill A. Shutemovdd360392013-12-23 14:16:58 +0200600u16 __read_mostly tlb_lld_1g[NR_INFO];
Alex Shie0ba94f2012-06-28 09:02:16 +0800601
Steven Honeymanf94fe112014-11-05 22:52:18 +0000602static void cpu_detect_tlb(struct cpuinfo_x86 *c)
Alex Shie0ba94f2012-06-28 09:02:16 +0800603{
604 if (this_cpu->c_detect_tlb)
605 this_cpu->c_detect_tlb(c);
606
Steven Honeymanf94fe112014-11-05 22:52:18 +0000607 pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
Alex Shie0ba94f2012-06-28 09:02:16 +0800608 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
Steven Honeymanf94fe112014-11-05 22:52:18 +0000609 tlb_lli_4m[ENTRIES]);
610
611 pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
612 tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
613 tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
Alex Shie0ba94f2012-06-28 09:02:16 +0800614}
615
Thomas Gleixner691997b2018-06-06 00:53:57 +0200616int detect_ht_early(struct cpuinfo_x86 *c)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200617{
Borislav Petkovc8e56d22015-06-04 18:55:25 +0200618#ifdef CONFIG_SMP
Yinghai Lu0a488a52008-09-04 21:09:47 +0200619 u32 eax, ebx, ecx, edx;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200620
621 if (!cpu_has(c, X86_FEATURE_HT))
Thomas Gleixner691997b2018-06-06 00:53:57 +0200622 return -1;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200623
624 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
Thomas Gleixner691997b2018-06-06 00:53:57 +0200625 return -1;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200626
Yinghai Lu1cd78772008-09-04 20:09:08 -0700627 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
Thomas Gleixner691997b2018-06-06 00:53:57 +0200628 return -1;
Yinghai Lu1cd78772008-09-04 20:09:08 -0700629
Yinghai Lu9d31d352008-09-04 21:09:44 +0200630 cpuid(1, &eax, &ebx, &ecx, &edx);
631
Yinghai Lu9d31d352008-09-04 21:09:44 +0200632 smp_num_siblings = (ebx & 0xff0000) >> 16;
Thomas Gleixner691997b2018-06-06 00:53:57 +0200633 if (smp_num_siblings == 1)
Chen Yucong1b74dde2016-02-02 11:45:02 +0800634 pr_info_once("CPU0: Hyper-Threading is disabled\n");
Thomas Gleixner691997b2018-06-06 00:53:57 +0200635#endif
636 return 0;
637}
638
639void detect_ht(struct cpuinfo_x86 *c)
640{
641#ifdef CONFIG_SMP
642 int index_msb, core_bits;
643
644 if (detect_ht_early(c) < 0)
Thomas Gleixnere0439282018-06-06 00:36:15 +0200645 return;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200646
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100647 index_msb = get_count_order(smp_num_siblings);
648 c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
649
650 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
651
652 index_msb = get_count_order(smp_num_siblings);
653
654 core_bits = get_count_order(c->x86_max_cores);
655
656 c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
657 ((1 << core_bits) - 1);
Yinghai Lu9d31d352008-09-04 21:09:44 +0200658#endif
Yinghai Lu97e4db72008-09-04 20:08:59 -0700659}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400661static void get_cpu_vendor(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 char *v = c->x86_vendor_id;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100664 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 for (i = 0; i < X86_VENDOR_NUM; i++) {
Yinghai Lu10a434f2008-09-04 21:09:45 +0200667 if (!cpu_devs[i])
668 break;
669
670 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
671 (cpu_devs[i]->c_ident[1] &&
672 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100673
Yinghai Lu10a434f2008-09-04 21:09:45 +0200674 this_cpu = cpu_devs[i];
675 c->x86_vendor = this_cpu->c_x86_vendor;
676 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 }
Yinghai Lu10a434f2008-09-04 21:09:45 +0200679
Chen Yucong1b74dde2016-02-02 11:45:02 +0800680 pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
681 "CPU: Your system may be unstable.\n", v);
Yinghai Lu10a434f2008-09-04 21:09:45 +0200682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 c->x86_vendor = X86_VENDOR_UNKNOWN;
684 this_cpu = &default_cpu;
685}
686
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400687void cpu_detect(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* Get vendor name */
Harvey Harrison4a148512008-02-01 17:49:43 +0100690 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
691 (unsigned int *)&c->x86_vendor_id[0],
692 (unsigned int *)&c->x86_vendor_id[8],
693 (unsigned int *)&c->x86_vendor_id[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 c->x86 = 4;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200696 /* Intel-defined flags: level 0x00000001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (c->cpuid_level >= 0x00000001) {
698 u32 junk, tfms, cap0, misc;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
Borislav Petkov99f925c2015-11-23 11:12:21 +0100701 c->x86 = x86_family(tfms);
702 c->x86_model = x86_model(tfms);
Jia Zhang06be0072018-01-01 09:52:10 +0800703 c->x86_stepping = x86_stepping(tfms);
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100704
Huang, Yingd4387bd2008-01-31 22:05:45 +0100705 if (cap0 & (1<<19)) {
Huang, Yingd4387bd2008-01-31 22:05:45 +0100706 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200707 c->x86_cache_alignment = c->x86_clflush_size;
Huang, Yingd4387bd2008-01-31 22:05:45 +0100708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
Yinghai Lu3da99c92008-09-04 21:09:44 +0200711
Andy Lutomirskief463982017-01-18 11:15:38 -0800712static void apply_forced_caps(struct cpuinfo_x86 *c)
713{
714 int i;
715
Thomas Gleixnerc2cacde2017-12-04 15:07:32 +0100716 for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
Andy Lutomirskief463982017-01-18 11:15:38 -0800717 c->x86_capability[i] &= ~cpu_caps_cleared[i];
718 c->x86_capability[i] |= cpu_caps_set[i];
719 }
720}
721
David Woodhousecda6b602018-01-30 14:30:23 +0000722static void init_speculation_control(struct cpuinfo_x86 *c)
723{
724 /*
725 * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
726 * and they also have a different bit for STIBP support. Also,
727 * a hypervisor might have set the individual AMD bits even on
728 * Intel CPUs, for finer-grained selection of what's available.
David Woodhousecda6b602018-01-30 14:30:23 +0000729 */
730 if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
731 set_cpu_cap(c, X86_FEATURE_IBRS);
732 set_cpu_cap(c, X86_FEATURE_IBPB);
Thomas Gleixnera7c34322018-05-10 19:13:18 +0200733 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
David Woodhousecda6b602018-01-30 14:30:23 +0000734 }
Borislav Petkov4a589082018-05-02 18:15:14 +0200735
David Woodhousecda6b602018-01-30 14:30:23 +0000736 if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
737 set_cpu_cap(c, X86_FEATURE_STIBP);
Borislav Petkov4a589082018-05-02 18:15:14 +0200738
Tom Lendackyb9655922018-05-10 22:06:39 +0200739 if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
740 cpu_has(c, X86_FEATURE_VIRT_SSBD))
Thomas Gleixnerf69e91f2018-05-10 20:21:36 +0200741 set_cpu_cap(c, X86_FEATURE_SSBD);
742
Thomas Gleixnera7c34322018-05-10 19:13:18 +0200743 if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
Borislav Petkov4a589082018-05-02 18:15:14 +0200744 set_cpu_cap(c, X86_FEATURE_IBRS);
Thomas Gleixnera7c34322018-05-10 19:13:18 +0200745 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
746 }
Borislav Petkov4a589082018-05-02 18:15:14 +0200747
748 if (cpu_has(c, X86_FEATURE_AMD_IBPB))
749 set_cpu_cap(c, X86_FEATURE_IBPB);
750
Thomas Gleixnera7c34322018-05-10 19:13:18 +0200751 if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
Borislav Petkov4a589082018-05-02 18:15:14 +0200752 set_cpu_cap(c, X86_FEATURE_STIBP);
Thomas Gleixnera7c34322018-05-10 19:13:18 +0200753 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
754 }
Konrad Rzeszutek Wilk9ad05582018-06-01 10:59:20 -0400755
756 if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
757 set_cpu_cap(c, X86_FEATURE_SSBD);
758 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
759 clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
760 }
David Woodhousecda6b602018-01-30 14:30:23 +0000761}
762
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400763void get_cpu_cap(struct cpuinfo_x86 *c)
Yinghai Lu093af8d2008-01-30 13:33:32 +0100764{
Borislav Petkov39c06df2015-12-07 10:39:40 +0100765 u32 eax, ebx, ecx, edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100766
Yinghai Lu3da99c92008-09-04 21:09:44 +0200767 /* Intel-defined flags: level 0x00000001 */
768 if (c->cpuid_level >= 0x00000001) {
Borislav Petkov39c06df2015-12-07 10:39:40 +0100769 cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100770
Borislav Petkov39c06df2015-12-07 10:39:40 +0100771 c->x86_capability[CPUID_1_ECX] = ecx;
772 c->x86_capability[CPUID_1_EDX] = edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100773 }
774
Andy Lutomirski36c1bc62016-12-15 10:14:42 -0800775 /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
776 if (c->cpuid_level >= 0x00000006)
777 c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
778
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700779 /* Additional Intel-defined flags: level 0x00000007 */
780 if (c->cpuid_level >= 0x00000007) {
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700781 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100782 c->x86_capability[CPUID_7_0_EBX] = ebx;
Dave Hansendfb4a702016-02-12 13:02:01 -0800783 c->x86_capability[CPUID_7_ECX] = ecx;
David Woodhoused3eba772018-01-25 16:14:09 +0000784 c->x86_capability[CPUID_7_EDX] = edx;
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700785 }
786
Fenghua Yu6229ad22014-05-29 11:12:30 -0700787 /* Extended state features: level 0x0000000d */
788 if (c->cpuid_level >= 0x0000000d) {
Fenghua Yu6229ad22014-05-29 11:12:30 -0700789 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
790
Borislav Petkov39c06df2015-12-07 10:39:40 +0100791 c->x86_capability[CPUID_D_1_EAX] = eax;
Fenghua Yu6229ad22014-05-29 11:12:30 -0700792 }
793
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000794 /* Additional Intel-defined flags: level 0x0000000F */
795 if (c->cpuid_level >= 0x0000000F) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000796
797 /* QoS sub-leaf, EAX=0Fh, ECX=0 */
798 cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100799 c->x86_capability[CPUID_F_0_EDX] = edx;
800
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000801 if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
802 /* will be overridden if occupancy monitoring exists */
803 c->x86_cache_max_rmid = ebx;
804
805 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
806 cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100807 c->x86_capability[CPUID_F_1_EDX] = edx;
808
Vikas Shivappa33c3cc72016-03-10 15:32:09 -0800809 if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
810 ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
811 (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000812 c->x86_cache_max_rmid = ecx;
813 c->x86_cache_occ_scale = ebx;
814 }
815 } else {
816 c->x86_cache_max_rmid = -1;
817 c->x86_cache_occ_scale = -1;
818 }
819 }
820
Yinghai Lu3da99c92008-09-04 21:09:44 +0200821 /* AMD-defined flags: level 0x80000001 */
Borislav Petkov39c06df2015-12-07 10:39:40 +0100822 eax = cpuid_eax(0x80000000);
823 c->extended_cpuid_level = eax;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100824
Borislav Petkov39c06df2015-12-07 10:39:40 +0100825 if ((eax & 0xffff0000) == 0x80000000) {
826 if (eax >= 0x80000001) {
827 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
828
829 c->x86_capability[CPUID_8000_0001_ECX] = ecx;
830 c->x86_capability[CPUID_8000_0001_EDX] = edx;
Yinghai Lu3da99c92008-09-04 21:09:44 +0200831 }
832 }
Yinghai Lu5122c892008-09-04 20:09:09 -0700833
Yazen Ghannam71faad42016-05-11 14:58:26 +0200834 if (c->extended_cpuid_level >= 0x80000007) {
835 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
836
837 c->x86_capability[CPUID_8000_0007_EBX] = ebx;
838 c->x86_power = edx;
839 }
840
Yinghai Lu5122c892008-09-04 20:09:09 -0700841 if (c->extended_cpuid_level >= 0x80000008) {
Borislav Petkov39c06df2015-12-07 10:39:40 +0100842 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
Yinghai Lu5122c892008-09-04 20:09:09 -0700843
844 c->x86_virt_bits = (eax >> 8) & 0xff;
845 c->x86_phys_bits = eax & 0xff;
Borislav Petkov39c06df2015-12-07 10:39:40 +0100846 c->x86_capability[CPUID_8000_0008_EBX] = ebx;
Yinghai Lu5122c892008-09-04 20:09:09 -0700847 }
Jan Beulich13c6c532009-03-12 12:37:34 +0000848#ifdef CONFIG_X86_32
849 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
850 c->x86_phys_bits = 36;
Yinghai Lu5122c892008-09-04 20:09:09 -0700851#endif
Yinghai Lue3224232008-09-06 01:52:28 -0700852
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100853 if (c->extended_cpuid_level >= 0x8000000a)
Borislav Petkov39c06df2015-12-07 10:39:40 +0100854 c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100855
Jacob Pan1dedefd2010-05-19 12:01:23 -0700856 init_scattered_cpuid_features(c);
David Woodhousecda6b602018-01-30 14:30:23 +0000857 init_speculation_control(c);
Andy Lutomirski1adc34a2017-01-18 11:15:39 -0800858
859 /*
860 * Clear/Set all flags overridden by options, after probe.
861 * This needs to happen each time we re-probe, which may happen
862 * several times during CPU initialization.
863 */
864 apply_forced_caps(c);
Yinghai Lu093af8d2008-01-30 13:33:32 +0100865}
Yinghai Luaef93c82008-09-14 02:33:15 -0700866
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400867static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
Yinghai Luaef93c82008-09-14 02:33:15 -0700868{
869#ifdef CONFIG_X86_32
870 int i;
871
872 /*
873 * First of all, decide if this is a 486 or higher
874 * It's a 486 if we can modify the AC flag
875 */
876 if (flag_is_changeable_p(X86_EFLAGS_AC))
877 c->x86 = 4;
878 else
879 c->x86 = 3;
880
881 for (i = 0; i < X86_VENDOR_NUM; i++)
882 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
883 c->x86_vendor_id[0] = 0;
884 cpu_devs[i]->c_identify(c);
885 if (c->x86_vendor_id[0]) {
886 get_cpu_vendor(c);
887 break;
888 }
889 }
890#endif
Andi Kleenef3d45c2018-08-24 10:03:50 -0700891 c->x86_cache_bits = c->x86_phys_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Vineela Tummalapalli12ceedb2019-11-04 12:22:01 +0100894#define NO_SPECULATION BIT(0)
895#define NO_MELTDOWN BIT(1)
896#define NO_SSB BIT(2)
897#define NO_L1TF BIT(3)
898#define NO_MDS BIT(4)
899#define MSBDS_ONLY BIT(5)
900#define NO_SWAPGS BIT(6)
901#define NO_ITLB_MULTIHIT BIT(7)
Thomas Gleixnerd5272d02019-02-27 10:10:23 +0100902
903#define VULNWL(_vendor, _family, _model, _whitelist) \
904 { X86_VENDOR_##_vendor, _family, _model, X86_FEATURE_ANY, _whitelist }
905
906#define VULNWL_INTEL(model, whitelist) \
907 VULNWL(INTEL, 6, INTEL_FAM6_##model, whitelist)
908
909#define VULNWL_AMD(family, whitelist) \
910 VULNWL(AMD, family, X86_MODEL_ANY, whitelist)
911
912static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
913 VULNWL(ANY, 4, X86_MODEL_ANY, NO_SPECULATION),
914 VULNWL(CENTAUR, 5, X86_MODEL_ANY, NO_SPECULATION),
915 VULNWL(INTEL, 5, X86_MODEL_ANY, NO_SPECULATION),
916 VULNWL(NSC, 5, X86_MODEL_ANY, NO_SPECULATION),
917
Andi Kleenfbf6ad08fd2019-01-18 16:50:16 -0800918 /* Intel Family 6 */
Vineela Tummalapalli12ceedb2019-11-04 12:22:01 +0100919 VULNWL_INTEL(ATOM_SALTWELL, NO_SPECULATION | NO_ITLB_MULTIHIT),
920 VULNWL_INTEL(ATOM_SALTWELL_TABLET, NO_SPECULATION | NO_ITLB_MULTIHIT),
921 VULNWL_INTEL(ATOM_SALTWELL_MID, NO_SPECULATION | NO_ITLB_MULTIHIT),
922 VULNWL_INTEL(ATOM_BONNELL, NO_SPECULATION | NO_ITLB_MULTIHIT),
923 VULNWL_INTEL(ATOM_BONNELL_MID, NO_SPECULATION | NO_ITLB_MULTIHIT),
Thomas Gleixnerd5272d02019-02-27 10:10:23 +0100924
Vineela Tummalapalli12ceedb2019-11-04 12:22:01 +0100925 VULNWL_INTEL(ATOM_SILVERMONT, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
926 VULNWL_INTEL(ATOM_SILVERMONT_X, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
927 VULNWL_INTEL(ATOM_SILVERMONT_MID, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
928 VULNWL_INTEL(ATOM_AIRMONT, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
929 VULNWL_INTEL(XEON_PHI_KNL, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
930 VULNWL_INTEL(XEON_PHI_KNM, NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
Thomas Gleixnerd5272d02019-02-27 10:10:23 +0100931
932 VULNWL_INTEL(CORE_YONAH, NO_SSB),
933
Vineela Tummalapalli12ceedb2019-11-04 12:22:01 +0100934 VULNWL_INTEL(ATOM_AIRMONT_MID, NO_L1TF | MSBDS_ONLY | NO_SWAPGS | NO_ITLB_MULTIHIT),
Thomas Gleixnerd5272d02019-02-27 10:10:23 +0100935
Vineela Tummalapalli12ceedb2019-11-04 12:22:01 +0100936 VULNWL_INTEL(ATOM_GOLDMONT, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
937 VULNWL_INTEL(ATOM_GOLDMONT_X, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
938 VULNWL_INTEL(ATOM_GOLDMONT_PLUS, NO_MDS | NO_L1TF | NO_SWAPGS | NO_ITLB_MULTIHIT),
Thomas Gleixner2224e892019-07-17 21:18:59 +0200939
940 /*
941 * Technically, swapgs isn't serializing on AMD (despite it previously
942 * being documented as such in the APM). But according to AMD, %gs is
943 * updated non-speculatively, and the issuing of %gs-relative memory
944 * operands will be blocked until the %gs update completes, which is
945 * good enough for our purposes.
946 */
Andi Kleenfbf6ad08fd2019-01-18 16:50:16 -0800947
948 /* AMD Family 0xf - 0x12 */
Vineela Tummalapalli12ceedb2019-11-04 12:22:01 +0100949 VULNWL_AMD(0x0f, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
950 VULNWL_AMD(0x10, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
951 VULNWL_AMD(0x11, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
952 VULNWL_AMD(0x12, NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
Thomas Gleixnerd5272d02019-02-27 10:10:23 +0100953
954 /* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */
Vineela Tummalapalli12ceedb2019-11-04 12:22:01 +0100955 VULNWL_AMD(X86_FAMILY_ANY, NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS | NO_ITLB_MULTIHIT),
David Woodhousea8799fd2018-01-25 16:14:13 +0000956 {}
957};
958
Thomas Gleixnerd5272d02019-02-27 10:10:23 +0100959static bool __init cpu_matches(unsigned long which)
960{
961 const struct x86_cpu_id *m = x86_match_cpu(cpu_vuln_whitelist);
David Woodhousea8799fd2018-01-25 16:14:13 +0000962
Thomas Gleixnerd5272d02019-02-27 10:10:23 +0100963 return m && !!(m->driver_data & which);
964}
Andi Kleen432e99b2018-06-13 15:48:26 -0700965
Pawan Gupta919d5612019-10-23 10:52:35 +0200966u64 x86_read_arch_cap_msr(void)
David Woodhousea8799fd2018-01-25 16:14:13 +0000967{
968 u64 ia32_cap = 0;
969
Pawan Gupta919d5612019-10-23 10:52:35 +0200970 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
971 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
972
973 return ia32_cap;
974}
975
976static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
977{
978 u64 ia32_cap = x86_read_arch_cap_msr();
979
Vineela Tummalapalli12ceedb2019-11-04 12:22:01 +0100980 /* Set ITLB_MULTIHIT bug if cpu is not in the whitelist and not mitigated */
981 if (!cpu_matches(NO_ITLB_MULTIHIT) && !(ia32_cap & ARCH_CAP_PSCHANGE_MC_NO))
982 setup_force_cpu_bug(X86_BUG_ITLB_MULTIHIT);
983
Thomas Gleixnerd5272d02019-02-27 10:10:23 +0100984 if (cpu_matches(NO_SPECULATION))
Dominik Brodowskief0efbb2018-05-22 11:05:39 +0200985 return;
986
987 setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
988 setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
989
Thomas Gleixnerd5272d02019-02-27 10:10:23 +0100990 if (!cpu_matches(NO_SSB) && !(ia32_cap & ARCH_CAP_SSB_NO) &&
Konrad Rzeszutek Wilk98ccdae2018-06-01 10:59:19 -0400991 !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
Konrad Rzeszutek Wilk24e4dd92018-04-25 22:04:20 -0400992 setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
993
Sai Praneethb0c05452018-08-01 11:42:25 -0700994 if (ia32_cap & ARCH_CAP_IBRS_ALL)
995 setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
996
Thomas Gleixner1cdffec2019-03-01 20:21:08 +0100997 if (!cpu_matches(NO_MDS) && !(ia32_cap & ARCH_CAP_MDS_NO)) {
Andi Kleenfbf6ad08fd2019-01-18 16:50:16 -0800998 setup_force_cpu_bug(X86_BUG_MDS);
Thomas Gleixner1cdffec2019-03-01 20:21:08 +0100999 if (cpu_matches(MSBDS_ONLY))
1000 setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
1001 }
Andi Kleenfbf6ad08fd2019-01-18 16:50:16 -08001002
Thomas Gleixner2224e892019-07-17 21:18:59 +02001003 if (!cpu_matches(NO_SWAPGS))
1004 setup_force_cpu_bug(X86_BUG_SWAPGS);
1005
Pawan Guptaa117aa42019-10-23 11:30:45 +02001006 /*
1007 * When the CPU is not mitigated for TAA (TAA_NO=0) set TAA bug when:
1008 * - TSX is supported or
1009 * - TSX_CTRL is present
1010 *
1011 * TSX_CTRL check is needed for cases when TSX could be disabled before
1012 * the kernel boot e.g. kexec.
1013 * TSX_CTRL check alone is not sufficient for cases when the microcode
1014 * update is not present or running as guest that don't get TSX_CTRL.
1015 */
1016 if (!(ia32_cap & ARCH_CAP_TAA_NO) &&
1017 (cpu_has(c, X86_FEATURE_RTM) ||
1018 (ia32_cap & ARCH_CAP_TSX_CTRL_MSR)))
1019 setup_force_cpu_bug(X86_BUG_TAA);
1020
Thomas Gleixnerd5272d02019-02-27 10:10:23 +01001021 if (cpu_matches(NO_MELTDOWN))
Konrad Rzeszutek Wilk88659d52018-04-25 22:04:16 -04001022 return;
David Woodhousea8799fd2018-01-25 16:14:13 +00001023
David Woodhousea8799fd2018-01-25 16:14:13 +00001024 /* Rogue Data Cache Load? No! */
1025 if (ia32_cap & ARCH_CAP_RDCL_NO)
Konrad Rzeszutek Wilk88659d52018-04-25 22:04:16 -04001026 return;
David Woodhousea8799fd2018-01-25 16:14:13 +00001027
Konrad Rzeszutek Wilk88659d52018-04-25 22:04:16 -04001028 setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
Andi Kleen432e99b2018-06-13 15:48:26 -07001029
Thomas Gleixnerd5272d02019-02-27 10:10:23 +01001030 if (cpu_matches(NO_L1TF))
Andi Kleen432e99b2018-06-13 15:48:26 -07001031 return;
1032
1033 setup_force_cpu_bug(X86_BUG_L1TF);
David Woodhousea8799fd2018-01-25 16:14:13 +00001034}
1035
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001036/*
1037 * Do minimum CPU detection early.
1038 * Fields really needed: vendor, cpuid_level, family, model, mask,
1039 * cache alignment.
1040 * The others are not touched to avoid unwanted side effects.
1041 *
1042 * WARNING: this function is only called on the BP. Don't add code here
1043 * that is supposed to run on all CPUs.
1044 */
Yinghai Lu3da99c92008-09-04 21:09:44 +02001045static void __init early_identify_cpu(struct cpuinfo_x86 *c)
Rusty Russelld7cd5612006-12-07 02:14:08 +01001046{
Yinghai Lu6627d242008-09-04 20:09:10 -07001047#ifdef CONFIG_X86_64
1048 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +00001049 c->x86_phys_bits = 36;
1050 c->x86_virt_bits = 48;
Yinghai Lu6627d242008-09-04 20:09:10 -07001051#else
Huang, Yingd4387bd2008-01-31 22:05:45 +01001052 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +00001053 c->x86_phys_bits = 32;
1054 c->x86_virt_bits = 32;
Yinghai Lu6627d242008-09-04 20:09:10 -07001055#endif
Yinghai Lu0a488a52008-09-04 21:09:47 +02001056 c->x86_cache_alignment = c->x86_clflush_size;
Rusty Russelld7cd5612006-12-07 02:14:08 +01001057
Yinghai Lu3da99c92008-09-04 21:09:44 +02001058 memset(&c->x86_capability, 0, sizeof c->x86_capability);
Yinghai Lu0a488a52008-09-04 21:09:47 +02001059 c->extended_cpuid_level = 0;
1060
Yinghai Luaef93c82008-09-14 02:33:15 -07001061 if (!have_cpuid_p())
1062 identify_cpu_without_cpuid(c);
1063
1064 /* cyrix could have cpuid enabled via c_identify()*/
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001065 if (have_cpuid_p()) {
1066 cpu_detect(c);
1067 get_cpu_vendor(c);
1068 get_cpu_cap(c);
Rusty Russelld7cd5612006-12-07 02:14:08 +01001069
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001070 if (this_cpu->c_early_init)
1071 this_cpu->c_early_init(c);
Krzysztof Helt12cf1052008-09-04 21:09:43 +02001072
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001073 c->cpu_index = 0;
1074 filter_cpuid_features(c, false);
Yinghai Lu3da99c92008-09-04 21:09:44 +02001075
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001076 if (this_cpu->c_bsp_init)
1077 this_cpu->c_bsp_init(c);
1078 }
Borislav Petkovc3b83592013-06-09 12:07:30 +02001079
1080 setup_force_cpu_cap(X86_FEATURE_ALWAYS);
Thomas Gleixnerd88f6012017-12-04 15:07:33 +01001081
Konrad Rzeszutek Wilk88659d52018-04-25 22:04:16 -04001082 cpu_set_bug_bits(c);
David Woodhouse26323fb2018-01-06 11:49:23 +00001083
Ingo Molnardb52ef72015-06-27 10:25:14 +02001084 fpu__init_system(c);
Andy Lutomirski00bcb5a2017-09-17 09:03:50 -07001085
1086#ifdef CONFIG_X86_32
1087 /*
1088 * Regardless of whether PCID is enumerated, the SDM says
1089 * that it can't be enabled in 32-bit mode.
1090 */
1091 setup_clear_cpu_cap(X86_FEATURE_PCID);
1092#endif
Rusty Russelld7cd5612006-12-07 02:14:08 +01001093}
1094
Yinghai Lu9d31d352008-09-04 21:09:44 +02001095void __init early_cpu_init(void)
1096{
Jan Beulich02dde8b2009-03-12 12:08:49 +00001097 const struct cpu_dev *const *cdev;
Yinghai Lu10a434f2008-09-04 21:09:45 +02001098 int count = 0;
Yinghai Lu9d31d352008-09-04 21:09:44 +02001099
Jan Beulichac23f252011-03-04 15:52:35 +00001100#ifdef CONFIG_PROCESSOR_SELECT
Chen Yucong1b74dde2016-02-02 11:45:02 +08001101 pr_info("KERNEL supported cpus:\n");
Ingo Molnar31c997c2009-11-14 10:34:41 +01001102#endif
1103
Yinghai Lu10a434f2008-09-04 21:09:45 +02001104 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
Jan Beulich02dde8b2009-03-12 12:08:49 +00001105 const struct cpu_dev *cpudev = *cdev;
Yinghai Lu9d31d352008-09-04 21:09:44 +02001106
Yinghai Lu10a434f2008-09-04 21:09:45 +02001107 if (count >= X86_VENDOR_NUM)
1108 break;
1109 cpu_devs[count] = cpudev;
1110 count++;
1111
Jan Beulichac23f252011-03-04 15:52:35 +00001112#ifdef CONFIG_PROCESSOR_SELECT
Ingo Molnar31c997c2009-11-14 10:34:41 +01001113 {
1114 unsigned int j;
Yinghai Lu10a434f2008-09-04 21:09:45 +02001115
Ingo Molnar31c997c2009-11-14 10:34:41 +01001116 for (j = 0; j < 2; j++) {
1117 if (!cpudev->c_ident[j])
1118 continue;
Chen Yucong1b74dde2016-02-02 11:45:02 +08001119 pr_info(" %s %s\n", cpudev->c_vendor,
Ingo Molnar31c997c2009-11-14 10:34:41 +01001120 cpudev->c_ident[j]);
1121 }
Yinghai Lu9d31d352008-09-04 21:09:44 +02001122 }
Dave Jones03884232009-11-13 15:30:00 -05001123#endif
Ingo Molnar31c997c2009-11-14 10:34:41 +01001124 }
Yinghai Lu9d31d352008-09-04 21:09:44 +02001125 early_identify_cpu(&boot_cpu_data);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08001126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001128/*
Borislav Petkov366d4a42010-10-04 09:31:27 +02001129 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1130 * unfortunately, that's not true in practice because of early VIA
1131 * chips and (more importantly) broken virtualizers that are not easy
1132 * to detect. In the latter case it doesn't even *fail* reliably, so
1133 * probing for it doesn't even work. Disable it completely on 32-bit
H. Peter Anvinba0593b2008-09-16 09:29:40 -07001134 * unless we can find a reliable way to detect all the broken cases.
Borislav Petkov366d4a42010-10-04 09:31:27 +02001135 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001136 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001137static void detect_nopl(struct cpuinfo_x86 *c)
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001138{
Borislav Petkov366d4a42010-10-04 09:31:27 +02001139#ifdef CONFIG_X86_32
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001140 clear_cpu_cap(c, X86_FEATURE_NOPL);
Borislav Petkov366d4a42010-10-04 09:31:27 +02001141#else
1142 set_cpu_cap(c, X86_FEATURE_NOPL);
1143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001146static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
1147{
1148#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /*
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001150 * Empirically, writing zero to a segment selector on AMD does
1151 * not clear the base, whereas writing zero to a segment
1152 * selector on Intel does clear the base. Intel's behavior
1153 * allows slightly faster context switches in the common case
1154 * where GS is unused by the prev and next threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 *
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001156 * Since neither vendor documents this anywhere that I can see,
1157 * detect it directly instead of hardcoding the choice by
1158 * vendor.
1159 *
1160 * I've designated AMD's behavior as the "bug" because it's
1161 * counterintuitive and less friendly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001163
1164 unsigned long old_base, tmp;
1165 rdmsrl(MSR_FS_BASE, old_base);
1166 wrmsrl(MSR_FS_BASE, 1);
1167 loadsegment(fs, 0);
1168 rdmsrl(MSR_FS_BASE, tmp);
1169 if (tmp != 0)
1170 set_cpu_bug(c, X86_BUG_NULL_SEG);
1171 wrmsrl(MSR_FS_BASE, old_base);
Yinghai Lu3da99c92008-09-04 21:09:44 +02001172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
Yinghai Luaef93c82008-09-14 02:33:15 -07001174
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001175static void generic_identify(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 c->extended_cpuid_level = 0;
1178
Yinghai Luaef93c82008-09-14 02:33:15 -07001179 if (!have_cpuid_p())
1180 identify_cpu_without_cpuid(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001181
Yinghai Luaef93c82008-09-14 02:33:15 -07001182 /* cyrix could have cpuid enabled via c_identify()*/
Ingo Molnara9853dd2008-09-14 14:46:58 +02001183 if (!have_cpuid_p())
Yinghai Luaef93c82008-09-14 02:33:15 -07001184 return;
1185
Yinghai Lu3da99c92008-09-04 21:09:44 +02001186 cpu_detect(c);
1187
1188 get_cpu_vendor(c);
1189
1190 get_cpu_cap(c);
1191
1192 if (c->cpuid_level >= 0x00000001) {
1193 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
Yinghai Lub89d3b32008-09-04 20:09:12 -07001194#ifdef CONFIG_X86_32
Borislav Petkovc8e56d22015-06-04 18:55:25 +02001195# ifdef CONFIG_SMP
Ingo Molnarcb8cc442009-01-28 13:24:54 +01001196 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lub89d3b32008-09-04 20:09:12 -07001197# else
Yinghai Lu3da99c92008-09-04 21:09:44 +02001198 c->apicid = c->initial_apicid;
Yinghai Lub89d3b32008-09-04 20:09:12 -07001199# endif
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08001200#endif
Yinghai Lub89d3b32008-09-04 20:09:12 -07001201 c->phys_proc_id = c->initial_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Yinghai Lu3da99c92008-09-04 21:09:44 +02001203
Yinghai Lu1b05d602008-09-06 01:52:27 -07001204 get_model_name(c); /* Default name */
Yinghai Lu3da99c92008-09-04 21:09:44 +02001205
Yinghai Lu3da99c92008-09-04 21:09:44 +02001206 detect_nopl(c);
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001207
1208 detect_null_seg_behavior(c);
Andy Lutomirski0230bb02016-04-07 17:31:48 -07001209
1210 /*
1211 * ESPFIX is a strange bug. All real CPUs have it. Paravirt
1212 * systems that run Linux at CPL > 0 may or may not have the
1213 * issue, but, even if they have the issue, there's absolutely
1214 * nothing we can do about it because we can't use the real IRET
1215 * instruction.
1216 *
1217 * NB: For the time being, only 32-bit kernels support
1218 * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
1219 * whether to apply espfix using paravirt hooks. If any
1220 * non-paravirt system ever shows up that does *not* have the
1221 * ESPFIX issue, we can change this.
1222 */
1223#ifdef CONFIG_X86_32
1224# ifdef CONFIG_PARAVIRT
1225 do {
1226 extern void native_iret(void);
1227 if (pv_cpu_ops.iret == native_iret)
1228 set_cpu_bug(c, X86_BUG_ESPFIX);
1229 } while (0);
1230# else
1231 set_cpu_bug(c, X86_BUG_ESPFIX);
1232# endif
1233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001236static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1237{
1238 /*
1239 * The heavy lifting of max_rmid and cache_occ_scale are handled
1240 * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
1241 * in case CQM bits really aren't there in this CPU.
1242 */
1243 if (c != &boot_cpu_data) {
1244 boot_cpu_data.x86_cache_max_rmid =
1245 min(boot_cpu_data.x86_cache_max_rmid,
1246 c->x86_cache_max_rmid);
1247 }
1248}
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250/*
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001251 * Validate that ACPI/mptables have the same information about the
1252 * effective APIC id and update the package map.
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001253 */
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001254static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001255{
1256#ifdef CONFIG_SMP
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001257 unsigned int apicid, cpu = smp_processor_id();
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001258
1259 apicid = apic->cpu_present_to_apicid(cpu);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001260
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001261 if (apicid != c->apicid) {
1262 pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001263 cpu, apicid, c->initial_apicid);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001264 }
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001265 BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001266#else
1267 c->logical_proc_id = 0;
1268#endif
1269}
1270
1271/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 * This does the hard work of actually picking apart the CPU stuff...
1273 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001274static void identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 int i;
1277
1278 c->loops_per_jiffy = loops_per_jiffy;
Gustavo A. R. Silvaaa72eec2018-02-13 13:22:08 -06001279 c->x86_cache_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 c->x86_vendor = X86_VENDOR_UNKNOWN;
Jia Zhang06be0072018-01-01 09:52:10 +08001281 c->x86_model = c->x86_stepping = 0; /* So far unknown... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 c->x86_vendor_id[0] = '\0'; /* Unset */
1283 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001284 c->x86_max_cores = 1;
Yinghai Lu102bbe32008-09-04 20:09:13 -07001285 c->x86_coreid_bits = 0;
Borislav Petkov6e306c52017-02-05 11:50:21 +01001286 c->cu_id = 0xff;
Yinghai Lu11fdd252008-09-07 17:58:50 -07001287#ifdef CONFIG_X86_64
Yinghai Lu102bbe32008-09-04 20:09:13 -07001288 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +00001289 c->x86_phys_bits = 36;
1290 c->x86_virt_bits = 48;
Yinghai Lu102bbe32008-09-04 20:09:13 -07001291#else
1292 c->cpuid_level = -1; /* CPUID not detected */
Andi Kleen770d1322006-12-07 02:14:05 +01001293 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +00001294 c->x86_phys_bits = 32;
1295 c->x86_virt_bits = 32;
Yinghai Lu102bbe32008-09-04 20:09:13 -07001296#endif
1297 c->x86_cache_alignment = c->x86_clflush_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 memset(&c->x86_capability, 0, sizeof c->x86_capability);
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 generic_identify(c);
1301
Andi Kleen38985342008-01-30 13:32:49 +01001302 if (this_cpu->c_identify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 this_cpu->c_identify(c);
1304
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001305 /* Clear/Set all flags overridden by options, after probe */
Andy Lutomirskief463982017-01-18 11:15:38 -08001306 apply_forced_caps(c);
Yinghai Lu2759c322009-05-15 13:05:16 -07001307
Yinghai Lu102bbe32008-09-04 20:09:13 -07001308#ifdef CONFIG_X86_64
Ingo Molnarcb8cc442009-01-28 13:24:54 +01001309 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lu102bbe32008-09-04 20:09:13 -07001310#endif
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /*
1313 * Vendor-specific initialization. In this section we
1314 * canonicalize the feature flags, meaning if there are
1315 * features a certain CPU supports which CPUID doesn't
1316 * tell us, CPUID claiming incorrect flags, or other bugs,
1317 * we handle them here.
1318 *
1319 * At the end of this section, c->x86_capability better
1320 * indicate the features this CPU genuinely supports!
1321 */
1322 if (this_cpu->c_init)
1323 this_cpu->c_init(c);
1324
1325 /* Disable the PN if appropriate */
1326 squash_the_stupid_serial_number(c);
1327
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -07001328 /* Set up SMEP/SMAP */
1329 setup_smep(c);
1330 setup_smap(c);
1331
Andy Lutomirskib52f9372017-06-29 08:53:21 -07001332 /* Set up PCID */
1333 setup_pcid(c);
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /*
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001336 * The vendor-specific functions might have changed features.
1337 * Now we do "generic changes."
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
1339
H. Peter Anvinb38b0662009-01-23 17:20:50 -08001340 /* Filter out anything that depends on CPUID levels we don't have */
1341 filter_cpuid_features(c, true);
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* If the model name is still unset, do table lookup. */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001344 if (!c->x86_model_id[0]) {
Jan Beulich02dde8b2009-03-12 12:08:49 +00001345 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 p = table_lookup_model(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001347 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 strcpy(c->x86_model_id, p);
1349 else
1350 /* Last resort... */
1351 sprintf(c->x86_model_id, "%02x/%02x",
Chuck Ebbert54a20f82006-03-23 02:59:36 -08001352 c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354
Yinghai Lu102bbe32008-09-04 20:09:13 -07001355#ifdef CONFIG_X86_64
1356 detect_ht(c);
1357#endif
1358
Alok Kataria88b094f2008-10-27 10:41:46 -07001359 init_hypervisor(c);
H. Peter Anvin49d859d2011-07-31 14:02:19 -07001360 x86_init_rdrand(c);
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001361 x86_init_cache_qos(c);
Dave Hansen06976942016-02-12 13:02:29 -08001362 setup_pku(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001363
1364 /*
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001365 * Clear/Set all flags overridden by options, need do it
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001366 * before following smp all cpus cap AND.
1367 */
Andy Lutomirskief463982017-01-18 11:15:38 -08001368 apply_forced_caps(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /*
1371 * On SMP, boot_cpu_data holds the common feature set between
1372 * all CPUs; so make sure that we indicate which features are
1373 * common between the CPUs. The first time this routine gets
1374 * executed, c == &boot_cpu_data.
1375 */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001376 if (c != &boot_cpu_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /* AND the already accumulated flags with these */
Yinghai Lu9d31d352008-09-04 21:09:44 +02001378 for (i = 0; i < NCAPINTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
Borislav Petkov65fc9852013-03-20 15:07:23 +01001380
1381 /* OR, i.e. replicate the bug flags */
1382 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
1383 c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385
1386 /* Init Machine Check Exception if available. */
Borislav Petkov5e099542009-10-16 12:31:32 +02001387 mcheck_cpu_init(c);
Andi Kleen30d432d2008-01-30 13:33:16 +01001388
1389 select_idle_routine(c);
Yinghai Lu102bbe32008-09-04 20:09:13 -07001390
Tejun Heode2d9442011-01-23 14:37:41 +01001391#ifdef CONFIG_NUMA
Yinghai Lu102bbe32008-09-04 20:09:13 -07001392 numa_add_cpu(smp_processor_id());
1393#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001394}
Shaohua Li31ab2692005-11-07 00:58:42 -08001395
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001396/*
1397 * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
1398 * on 32-bit kernels:
1399 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001400#ifdef CONFIG_X86_32
1401void enable_sep_cpu(void)
1402{
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001403 struct tss_struct *tss;
1404 int cpu;
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001405
Borislav Petkovb3edfda2016-03-16 13:19:29 +01001406 if (!boot_cpu_has(X86_FEATURE_SEP))
1407 return;
1408
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001409 cpu = get_cpu();
1410 tss = &per_cpu(cpu_tss, cpu);
1411
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001412 /*
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001413 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1414 * see the big comment in struct x86_hw_tss's definition.
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001415 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001416
1417 tss->x86_tss.ss1 = __KERNEL_CS;
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001418 wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
1419
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001420 wrmsr(MSR_IA32_SYSENTER_ESP,
1421 (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
1422 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001423
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001424 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001425
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001426 put_cpu();
1427}
Glauber Costae04d6452008-09-22 14:35:08 -03001428#endif
1429
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001430void __init identify_boot_cpu(void)
1431{
1432 identify_cpu(&boot_cpu_data);
Len Brown02c68a02011-04-01 16:59:53 -04001433 init_amd_e400_c1e_mask();
Yinghai Lu102bbe32008-09-04 20:09:13 -07001434#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001435 sysenter_setup();
Li Shaohua6fe940d2005-06-25 14:54:53 -07001436 enable_sep_cpu();
Yinghai Lu102bbe32008-09-04 20:09:13 -07001437#endif
Borislav Petkov5b556332012-08-06 19:00:37 +02001438 cpu_detect_tlb(&boot_cpu_data);
Pawan Gupta21127882019-10-23 11:01:53 +02001439
1440 tsx_init();
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001441}
Shaohua Li3b520b22005-07-07 17:56:38 -07001442
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001443void identify_secondary_cpu(struct cpuinfo_x86 *c)
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001444{
1445 BUG_ON(c == &boot_cpu_data);
1446 identify_cpu(c);
Yinghai Lu102bbe32008-09-04 20:09:13 -07001447#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001448 enable_sep_cpu();
Yinghai Lu102bbe32008-09-04 20:09:13 -07001449#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001450 mtrr_ap_init();
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001451 validate_apic_and_package_id(c);
Konrad Rzeszutek Wilk19e3a2b2018-04-25 22:04:22 -04001452 x86_spec_ctrl_setup_ap();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
Yinghai Lua0854a42008-09-04 21:09:46 +02001455struct msr_range {
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001456 unsigned min;
1457 unsigned max;
Yinghai Lua0854a42008-09-04 21:09:46 +02001458};
1459
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001460static const struct msr_range msr_range_array[] = {
Yinghai Lua0854a42008-09-04 21:09:46 +02001461 { 0x00000000, 0x00000418},
1462 { 0xc0000000, 0xc000040b},
1463 { 0xc0010000, 0xc0010142},
1464 { 0xc0011000, 0xc001103b},
1465};
1466
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001467static void __print_cpu_msr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001469 unsigned index_min, index_max;
Yinghai Lua0854a42008-09-04 21:09:46 +02001470 unsigned index;
1471 u64 val;
1472 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Yinghai Lua0854a42008-09-04 21:09:46 +02001474 for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
1475 index_min = msr_range_array[i].min;
1476 index_max = msr_range_array[i].max;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001477
Yinghai Lua0854a42008-09-04 21:09:46 +02001478 for (index = index_min; index < index_max; index++) {
Borislav Petkovecd431d2012-06-01 16:52:36 +02001479 if (rdmsrl_safe(index, &val))
Yinghai Lua0854a42008-09-04 21:09:46 +02001480 continue;
Chen Yucong1b74dde2016-02-02 11:45:02 +08001481 pr_info(" MSR%08x: %016llx\n", index, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484}
Yinghai Lua0854a42008-09-04 21:09:46 +02001485
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001486static int show_msr;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001487
Yinghai Lua0854a42008-09-04 21:09:46 +02001488static __init int setup_show_msr(char *arg)
1489{
1490 int num;
1491
1492 get_option(&arg, &num);
1493
1494 if (num > 0)
1495 show_msr = num;
1496 return 1;
1497}
1498__setup("show_msr=", setup_show_msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Andi Kleen191679f2008-01-30 13:33:21 +01001500static __init int setup_noclflush(char *arg)
1501{
H. Peter Anvin840d2832014-02-27 08:31:30 -08001502 setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
H. Peter Anvinda4aaa72014-02-27 08:36:31 -08001503 setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
Andi Kleen191679f2008-01-30 13:33:21 +01001504 return 1;
1505}
1506__setup("noclflush", setup_noclflush);
1507
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001508void print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Jan Beulich02dde8b2009-03-12 12:08:49 +00001510 const char *vendor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001512 if (c->x86_vendor < X86_VENDOR_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 vendor = this_cpu->c_vendor;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001514 } else {
1515 if (c->cpuid_level >= 0)
1516 vendor = c->x86_vendor_id;
1517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Yinghai Lubd32a8cf2008-09-19 18:41:16 -07001519 if (vendor && !strstr(c->x86_model_id, vendor))
Chen Yucong1b74dde2016-02-02 11:45:02 +08001520 pr_cont("%s ", vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Yinghai Lu9d31d352008-09-04 21:09:44 +02001522 if (c->x86_model_id[0])
Chen Yucong1b74dde2016-02-02 11:45:02 +08001523 pr_cont("%s", c->x86_model_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001525 pr_cont("%d86", c->x86);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Chen Yucong1b74dde2016-02-02 11:45:02 +08001527 pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
Borislav Petkov924e1012012-09-14 18:37:46 +02001528
Jia Zhang06be0072018-01-01 09:52:10 +08001529 if (c->x86_stepping || c->cpuid_level >= 0)
1530 pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001532 pr_cont(")\n");
Yinghai Lua0854a42008-09-04 21:09:46 +02001533
Yinghai Lu0b8b8072012-03-22 21:31:43 -07001534 print_cpu_msr(c);
Yinghai Lu21c3fcf2012-02-12 09:53:57 -08001535}
1536
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001537void print_cpu_msr(struct cpuinfo_x86 *c)
Yinghai Lu21c3fcf2012-02-12 09:53:57 -08001538{
Yinghai Lua0854a42008-09-04 21:09:46 +02001539 if (c->cpu_index < show_msr)
Yinghai Lu21c3fcf2012-02-12 09:53:57 -08001540 __print_cpu_msr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Andi Kleenac72e782008-01-30 13:33:21 +01001543static __init int setup_disablecpuid(char *arg)
1544{
1545 int bit;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001546
Lukasz Odzioba9bae3c32016-12-28 14:55:40 +01001547 if (get_option(&arg, &bit) && bit >= 0 && bit < NCAPINTS * 32)
Andi Kleenac72e782008-01-30 13:33:21 +01001548 setup_clear_cpu_cap(bit);
1549 else
1550 return 0;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001551
Andi Kleenac72e782008-01-30 13:33:21 +01001552 return 1;
1553}
1554__setup("clearcpuid=", setup_disablecpuid);
1555
Yinghai Lud5494d42008-09-04 20:09:03 -07001556#ifdef CONFIG_X86_64
Kees Cook404f6aa2016-08-08 16:29:06 -07001557struct desc_ptr idt_descr __ro_after_init = {
1558 .size = NR_VECTORS * 16 - 1,
1559 .address = (unsigned long) idt_table,
1560};
1561const struct desc_ptr debug_idt_descr = {
1562 .size = NR_VECTORS * 16 - 1,
1563 .address = (unsigned long) debug_idt_table,
1564};
Yinghai Lud5494d42008-09-04 20:09:03 -07001565
Brian Gerst947e76c2009-01-19 12:21:28 +09001566DEFINE_PER_CPU_FIRST(union irq_stack_union,
Andi Kleen277d5b42013-08-05 15:02:43 -07001567 irq_stack_union) __aligned(PAGE_SIZE) __visible;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001568
Tejun Heobdf977b2009-08-03 14:12:19 +09001569/*
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001570 * The following percpu variables are hot. Align current_task to
1571 * cacheline size such that they fall in the same cacheline.
Tejun Heobdf977b2009-08-03 14:12:19 +09001572 */
1573DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1574 &init_task;
1575EXPORT_PER_CPU_SYMBOL(current_task);
Yinghai Lud5494d42008-09-04 20:09:03 -07001576
Tejun Heobdf977b2009-08-03 14:12:19 +09001577DEFINE_PER_CPU(char *, irq_stack_ptr) =
Josh Poimboeuf4950d6d2016-08-18 10:59:08 -05001578 init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE;
Tejun Heobdf977b2009-08-03 14:12:19 +09001579
Andi Kleen277d5b42013-08-05 15:02:43 -07001580DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
Yinghai Lud5494d42008-09-04 20:09:03 -07001581
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001582DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1583EXPORT_PER_CPU_SYMBOL(__preempt_count);
1584
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001585/*
1586 * Special IST stacks which the CPU switches to when it calls
1587 * an IST-marked descriptor entry. Up to 7 stacks (hardware
1588 * limit), all of them are 4K, except the debug stack which
1589 * is 8K.
1590 */
1591static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
1592 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
1593 [DEBUG_STACK - 1] = DEBUG_STKSZ
1594};
1595
Richard Fellner13be4482017-05-04 14:26:50 +02001596DEFINE_PER_CPU_PAGE_ALIGNED_USER_MAPPED(char, exception_stacks
Tejun Heo3e352aa2009-08-03 14:10:11 +09001597 [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
Yinghai Lud5494d42008-09-04 20:09:03 -07001598
Yinghai Lud5494d42008-09-04 20:09:03 -07001599/* May not be marked __init: used by software suspend */
1600void syscall_init(void)
1601{
Borislav Petkov31ac34c2015-11-23 11:12:25 +01001602 wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
Andy Lutomirski47edb652015-07-23 12:14:40 -07001603 wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001604
1605#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski47edb652015-07-23 12:14:40 -07001606 wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001607 /*
Denys Vlasenko487d1ed2015-03-27 11:59:16 +01001608 * This only works on Intel CPUs.
1609 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1610 * This does not cause SYSENTER to jump to the wrong location, because
1611 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001612 */
1613 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1614 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001615 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001616#else
Andy Lutomirski47edb652015-07-23 12:14:40 -07001617 wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
Borislav Petkov6b513112015-04-03 14:25:28 +02001618 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001619 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1620 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
Yinghai Lud5494d42008-09-04 20:09:03 -07001621#endif
1622
1623 /* Flags to clear on syscall */
1624 wrmsrl(MSR_SYSCALL_MASK,
H. Peter Anvin63bcff22012-09-21 12:43:12 -07001625 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
Andy Lutomirski8c7aa692014-10-01 11:49:04 -07001626 X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
Yinghai Lud5494d42008-09-04 20:09:03 -07001627}
1628
Yinghai Lud5494d42008-09-04 20:09:03 -07001629/*
1630 * Copies of the original ist values from the tss are only accessed during
1631 * debugging, no special alignment required.
1632 */
1633DEFINE_PER_CPU(struct orig_ist, orig_ist);
1634
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001635static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
Steven Rostedt42181182011-12-16 11:43:02 -05001636DEFINE_PER_CPU(int, debug_stack_usage);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001637
1638int is_debug_stack(unsigned long addr)
1639{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001640 return __this_cpu_read(debug_stack_usage) ||
1641 (addr <= __this_cpu_read(debug_stack_addr) &&
1642 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001643}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001644NOKPROBE_SYMBOL(is_debug_stack);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001645
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001646DEFINE_PER_CPU(u32, debug_idt_ctr);
Steven Rostedtf8988172012-05-30 11:47:00 -04001647
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001648void debug_stack_set_zero(void)
1649{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001650 this_cpu_inc(debug_idt_ctr);
1651 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001652}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001653NOKPROBE_SYMBOL(debug_stack_set_zero);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001654
1655void debug_stack_reset(void)
1656{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001657 if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
Steven Rostedtf8988172012-05-30 11:47:00 -04001658 return;
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001659 if (this_cpu_dec_return(debug_idt_ctr) == 0)
1660 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001661}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001662NOKPROBE_SYMBOL(debug_stack_reset);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001663
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001664#else /* CONFIG_X86_64 */
Yinghai Lud5494d42008-09-04 20:09:03 -07001665
Tejun Heobdf977b2009-08-03 14:12:19 +09001666DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1667EXPORT_PER_CPU_SYMBOL(current_task);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001668DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1669EXPORT_PER_CPU_SYMBOL(__preempt_count);
Tejun Heobdf977b2009-08-03 14:12:19 +09001670
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001671/*
1672 * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1673 * the top of the kernel stack. Use an extra percpu variable to track the
1674 * top of the kernel stack directly.
1675 */
1676DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1677 (unsigned long)&init_thread_union + THREAD_SIZE;
1678EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1679
Tejun Heo60a53172009-02-09 22:17:40 +09001680#ifdef CONFIG_CC_STACKPROTECTOR
Jeremy Fitzhardinge53f82452009-09-03 14:31:44 -07001681DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
Tejun Heo60a53172009-02-09 22:17:40 +09001682#endif
1683
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001684#endif /* CONFIG_X86_64 */
Jeremy Fitzhardingec5413fb2007-05-02 19:27:16 +02001685
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001686/*
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301687 * Clear all 6 debug registers:
1688 */
1689static void clear_all_debug_regs(void)
1690{
1691 int i;
1692
1693 for (i = 0; i < 8; i++) {
1694 /* Ignore db4, db5 */
1695 if ((i == 4) || (i == 5))
1696 continue;
1697
1698 set_debugreg(0, i);
1699 }
1700}
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +01001701
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001702#ifdef CONFIG_KGDB
1703/*
1704 * Restore debug regs if using kgdbwait and you have a kernel debugger
1705 * connection established.
1706 */
1707static void dbg_restore_debug_regs(void)
1708{
1709 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1710 arch_kgdb_ops.correct_hw_break();
1711}
1712#else /* ! CONFIG_KGDB */
1713#define dbg_restore_debug_regs()
1714#endif /* ! CONFIG_KGDB */
1715
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001716static void wait_for_master_cpu(int cpu)
1717{
1718#ifdef CONFIG_SMP
1719 /*
1720 * wait for ACK from master CPU before continuing
1721 * with AP initialization
1722 */
1723 WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1724 while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1725 cpu_relax();
1726#endif
1727}
1728
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001729/*
1730 * cpu_init() initializes state that is per-CPU. Some data is already
1731 * initialized (naturally) in the bootstrap process, such as the GDT
1732 * and IDT. We reload them nevertheless, this function acts as a
1733 * 'CPU state barrier', nothing should get across.
Yinghai Lu1ba76582008-09-04 20:09:04 -07001734 * A lot of state is already set up in PDA init for 64 bit
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001735 */
Yinghai Lu1ba76582008-09-04 20:09:04 -07001736#ifdef CONFIG_X86_64
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001737
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001738void cpu_init(void)
Yinghai Lu1ba76582008-09-04 20:09:04 -07001739{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001740 struct orig_ist *oist;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001741 struct task_struct *me;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001742 struct tss_struct *t;
1743 unsigned long v;
Andy Lutomirskifb598312016-07-14 13:22:58 -07001744 int cpu = raw_smp_processor_id();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001745 int i;
1746
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001747 wait_for_master_cpu(cpu);
1748
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001749 /*
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001750 * Initialize the CR4 shadow before doing anything that could
1751 * try to read it.
1752 */
1753 cr4_init_shadow();
Hugh Dickins23e09432017-09-24 16:59:49 -07001754 if (!kaiser_enabled) {
1755 /*
1756 * secondary_startup_64() deferred setting PGE in cr4:
1757 * probe_page_size_mask() sets it on the boot cpu,
1758 * but it needs to be set on each secondary cpu.
1759 */
1760 cr4_set_bits(X86_CR4_PGE);
1761 }
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001762
1763 /*
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001764 * Load microcode on this cpu if a valid microcode is available.
1765 * This is early microcode loading procedure.
1766 */
1767 load_ucode_ap();
1768
Andy Lutomirski24933b82015-03-05 19:19:05 -08001769 t = &per_cpu(cpu_tss, cpu);
Tejun Heo0fe1e002009-10-29 22:34:14 +09001770 oist = &per_cpu(orig_ist, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001771
Brian Gerste7a22c12009-01-19 00:38:59 +09001772#ifdef CONFIG_NUMA
Fenghua Yu27fd1852012-11-13 11:32:47 -08001773 if (this_cpu_read(numa_node) == 0 &&
Lee Schermerhorne534c7c2010-05-26 14:44:58 -07001774 early_cpu_to_node(cpu) != NUMA_NO_NODE)
1775 set_numa_node(early_cpu_to_node(cpu));
Brian Gerste7a22c12009-01-19 00:38:59 +09001776#endif
Yinghai Lu1ba76582008-09-04 20:09:04 -07001777
1778 me = current;
1779
Mike Travis2eaad1f2009-12-10 17:19:36 -08001780 pr_debug("Initializing CPU#%d\n", cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001781
Andy Lutomirski375074c2014-10-24 15:58:07 -07001782 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001783
1784 /*
1785 * Initialize the per-CPU GDT with the boot GDT,
1786 * and set up the GDT descriptor:
1787 */
1788
Brian Gerst552be872009-01-30 17:47:53 +09001789 switch_to_new_gdt(cpu);
Brian Gerst2697fbd2009-01-27 12:56:48 +09001790 loadsegment(fs, 0);
1791
Seiji Aguchicf910e82013-06-20 11:46:53 -04001792 load_current_idt();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001793
1794 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1795 syscall_init();
1796
1797 wrmsrl(MSR_FS_BASE, 0);
1798 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1799 barrier();
1800
H. Peter Anvin4763ed42009-11-13 15:28:16 -08001801 x86_configure_nx();
Thomas Gleixner659006b2015-01-15 21:22:26 +00001802 x2apic_setup();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001803
1804 /*
1805 * set up and load the per-CPU TSS
1806 */
Tejun Heo0fe1e002009-10-29 22:34:14 +09001807 if (!oist->ist[0]) {
Brian Gerst92d65b22009-01-19 00:38:58 +09001808 char *estacks = per_cpu(exception_stacks, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001809
Yinghai Lu1ba76582008-09-04 20:09:04 -07001810 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001811 estacks += exception_stack_sizes[v];
Tejun Heo0fe1e002009-10-29 22:34:14 +09001812 oist->ist[v] = t->x86_tss.ist[v] =
Yinghai Lu1ba76582008-09-04 20:09:04 -07001813 (unsigned long)estacks;
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001814 if (v == DEBUG_STACK-1)
1815 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001816 }
1817 }
1818
1819 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001820
Yinghai Lu1ba76582008-09-04 20:09:04 -07001821 /*
1822 * <= is required because the CPU will access up to
1823 * 8 bits beyond the end of the IO permission bitmap.
1824 */
1825 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1826 t->io_bitmap[i] = ~0UL;
1827
1828 atomic_inc(&init_mm.mm_count);
1829 me->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001830 BUG_ON(me->mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001831 enter_lazy_tlb(&init_mm, me);
1832
1833 load_sp0(t, &current->thread);
1834 set_tss_desc(cpu, t);
1835 load_TR_desc();
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001836 load_mm_ldt(&init_mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001837
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001838 clear_all_debug_regs();
1839 dbg_restore_debug_regs();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001840
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001841 fpu__init_cpu();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001842
Yinghai Lu1ba76582008-09-04 20:09:04 -07001843 if (is_uv_system())
1844 uv_cpu_init();
1845}
1846
1847#else
1848
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001849void cpu_init(void)
James Bottomley9ee79a32007-01-22 09:18:31 -06001850{
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001851 int cpu = smp_processor_id();
1852 struct task_struct *curr = current;
Andy Lutomirski24933b82015-03-05 19:19:05 -08001853 struct tss_struct *t = &per_cpu(cpu_tss, cpu);
James Bottomley9ee79a32007-01-22 09:18:31 -06001854 struct thread_struct *thread = &curr->thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001856 wait_for_master_cpu(cpu);
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001857
Steven Rostedt5b2bdbc2015-02-27 14:50:19 -05001858 /*
1859 * Initialize the CR4 shadow before doing anything that could
1860 * try to read it.
1861 */
1862 cr4_init_shadow();
1863
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001864 show_ucode_info_early();
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001865
Chen Yucong1b74dde2016-02-02 11:45:02 +08001866 pr_info("Initializing CPU#%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Borislav Petkov362f9242015-12-07 10:39:41 +01001868 if (cpu_feature_enabled(X86_FEATURE_VME) ||
Borislav Petkov59e21e32016-04-04 22:24:59 +02001869 boot_cpu_has(X86_FEATURE_TSC) ||
Borislav Petkov362f9242015-12-07 10:39:41 +01001870 boot_cpu_has(X86_FEATURE_DE))
Andy Lutomirski375074c2014-10-24 15:58:07 -07001871 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Seiji Aguchicf910e82013-06-20 11:46:53 -04001873 load_current_idt();
Brian Gerst552be872009-01-30 17:47:53 +09001874 switch_to_new_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 * Set up and load the per-CPU TSS and LDT
1878 */
1879 atomic_inc(&init_mm.mm_count);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001880 curr->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001881 BUG_ON(curr->mm);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001882 enter_lazy_tlb(&init_mm, curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
H. Peter Anvinfaca6222008-01-30 13:31:02 +01001884 load_sp0(t, thread);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001885 set_tss_desc(cpu, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 load_TR_desc();
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001887 load_mm_ldt(&init_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Thomas Gleixnerf9a196b2009-05-01 20:59:25 +02001889 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1890
Matt Mackall22c4e302006-01-08 01:05:24 -08001891#ifdef CONFIG_DOUBLEFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /* Set up doublefault TSS pointer in the GDT */
1893 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
Matt Mackall22c4e302006-01-08 01:05:24 -08001894#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301896 clear_all_debug_regs();
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001897 dbg_restore_debug_regs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001899 fpu__init_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
Yinghai Lu1ba76582008-09-04 20:09:04 -07001901#endif
Borislav Petkov5700f742013-06-09 12:07:32 +02001902
Laura Abbottb51ef522015-07-20 14:47:58 -07001903static void bsp_resume(void)
1904{
1905 if (this_cpu->c_bsp_resume)
1906 this_cpu->c_bsp_resume(&boot_cpu_data);
1907}
1908
1909static struct syscore_ops cpu_syscore_ops = {
1910 .resume = bsp_resume,
1911};
1912
1913static int __init init_cpu_syscore(void)
1914{
1915 register_syscore_ops(&cpu_syscore_ops);
1916 return 0;
1917}
1918core_initcall(init_cpu_syscore);