blob: 3c01610c5ba9e9e4df4e192d95143f8e3cabbb85 [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 */
391 get_cpu_cap(c);
392}
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 }
David Woodhousecda6b602018-01-30 14:30:23 +0000755}
756
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400757void get_cpu_cap(struct cpuinfo_x86 *c)
Yinghai Lu093af8d2008-01-30 13:33:32 +0100758{
Borislav Petkov39c06df2015-12-07 10:39:40 +0100759 u32 eax, ebx, ecx, edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100760
Yinghai Lu3da99c92008-09-04 21:09:44 +0200761 /* Intel-defined flags: level 0x00000001 */
762 if (c->cpuid_level >= 0x00000001) {
Borislav Petkov39c06df2015-12-07 10:39:40 +0100763 cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100764
Borislav Petkov39c06df2015-12-07 10:39:40 +0100765 c->x86_capability[CPUID_1_ECX] = ecx;
766 c->x86_capability[CPUID_1_EDX] = edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100767 }
768
Andy Lutomirski36c1bc62016-12-15 10:14:42 -0800769 /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
770 if (c->cpuid_level >= 0x00000006)
771 c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
772
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700773 /* Additional Intel-defined flags: level 0x00000007 */
774 if (c->cpuid_level >= 0x00000007) {
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700775 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100776 c->x86_capability[CPUID_7_0_EBX] = ebx;
Dave Hansendfb4a702016-02-12 13:02:01 -0800777 c->x86_capability[CPUID_7_ECX] = ecx;
David Woodhoused3eba772018-01-25 16:14:09 +0000778 c->x86_capability[CPUID_7_EDX] = edx;
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700779 }
780
Fenghua Yu6229ad22014-05-29 11:12:30 -0700781 /* Extended state features: level 0x0000000d */
782 if (c->cpuid_level >= 0x0000000d) {
Fenghua Yu6229ad22014-05-29 11:12:30 -0700783 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
784
Borislav Petkov39c06df2015-12-07 10:39:40 +0100785 c->x86_capability[CPUID_D_1_EAX] = eax;
Fenghua Yu6229ad22014-05-29 11:12:30 -0700786 }
787
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000788 /* Additional Intel-defined flags: level 0x0000000F */
789 if (c->cpuid_level >= 0x0000000F) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000790
791 /* QoS sub-leaf, EAX=0Fh, ECX=0 */
792 cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100793 c->x86_capability[CPUID_F_0_EDX] = edx;
794
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000795 if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
796 /* will be overridden if occupancy monitoring exists */
797 c->x86_cache_max_rmid = ebx;
798
799 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
800 cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100801 c->x86_capability[CPUID_F_1_EDX] = edx;
802
Vikas Shivappa33c3cc72016-03-10 15:32:09 -0800803 if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
804 ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
805 (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000806 c->x86_cache_max_rmid = ecx;
807 c->x86_cache_occ_scale = ebx;
808 }
809 } else {
810 c->x86_cache_max_rmid = -1;
811 c->x86_cache_occ_scale = -1;
812 }
813 }
814
Yinghai Lu3da99c92008-09-04 21:09:44 +0200815 /* AMD-defined flags: level 0x80000001 */
Borislav Petkov39c06df2015-12-07 10:39:40 +0100816 eax = cpuid_eax(0x80000000);
817 c->extended_cpuid_level = eax;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100818
Borislav Petkov39c06df2015-12-07 10:39:40 +0100819 if ((eax & 0xffff0000) == 0x80000000) {
820 if (eax >= 0x80000001) {
821 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
822
823 c->x86_capability[CPUID_8000_0001_ECX] = ecx;
824 c->x86_capability[CPUID_8000_0001_EDX] = edx;
Yinghai Lu3da99c92008-09-04 21:09:44 +0200825 }
826 }
Yinghai Lu5122c892008-09-04 20:09:09 -0700827
Yazen Ghannam71faad42016-05-11 14:58:26 +0200828 if (c->extended_cpuid_level >= 0x80000007) {
829 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
830
831 c->x86_capability[CPUID_8000_0007_EBX] = ebx;
832 c->x86_power = edx;
833 }
834
Yinghai Lu5122c892008-09-04 20:09:09 -0700835 if (c->extended_cpuid_level >= 0x80000008) {
Borislav Petkov39c06df2015-12-07 10:39:40 +0100836 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
Yinghai Lu5122c892008-09-04 20:09:09 -0700837
838 c->x86_virt_bits = (eax >> 8) & 0xff;
839 c->x86_phys_bits = eax & 0xff;
Borislav Petkov39c06df2015-12-07 10:39:40 +0100840 c->x86_capability[CPUID_8000_0008_EBX] = ebx;
Yinghai Lu5122c892008-09-04 20:09:09 -0700841 }
Jan Beulich13c6c532009-03-12 12:37:34 +0000842#ifdef CONFIG_X86_32
843 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
844 c->x86_phys_bits = 36;
Yinghai Lu5122c892008-09-04 20:09:09 -0700845#endif
Yinghai Lue3224232008-09-06 01:52:28 -0700846
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100847 if (c->extended_cpuid_level >= 0x8000000a)
Borislav Petkov39c06df2015-12-07 10:39:40 +0100848 c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100849
Jacob Pan1dedefd2010-05-19 12:01:23 -0700850 init_scattered_cpuid_features(c);
David Woodhousecda6b602018-01-30 14:30:23 +0000851 init_speculation_control(c);
Andy Lutomirski1adc34a2017-01-18 11:15:39 -0800852
853 /*
854 * Clear/Set all flags overridden by options, after probe.
855 * This needs to happen each time we re-probe, which may happen
856 * several times during CPU initialization.
857 */
858 apply_forced_caps(c);
Yinghai Lu093af8d2008-01-30 13:33:32 +0100859}
Yinghai Luaef93c82008-09-14 02:33:15 -0700860
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400861static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
Yinghai Luaef93c82008-09-14 02:33:15 -0700862{
863#ifdef CONFIG_X86_32
864 int i;
865
866 /*
867 * First of all, decide if this is a 486 or higher
868 * It's a 486 if we can modify the AC flag
869 */
870 if (flag_is_changeable_p(X86_EFLAGS_AC))
871 c->x86 = 4;
872 else
873 c->x86 = 3;
874
875 for (i = 0; i < X86_VENDOR_NUM; i++)
876 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
877 c->x86_vendor_id[0] = 0;
878 cpu_devs[i]->c_identify(c);
879 if (c->x86_vendor_id[0]) {
880 get_cpu_vendor(c);
881 break;
882 }
883 }
884#endif
Andi Kleenef3d45c2018-08-24 10:03:50 -0700885 c->x86_cache_bits = c->x86_phys_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Arnd Bergmann4b234a22018-02-02 22:39:23 +0100888static const __initconst struct x86_cpu_id cpu_no_speculation[] = {
David Woodhousea8799fd2018-01-25 16:14:13 +0000889 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_CEDARVIEW, X86_FEATURE_ANY },
890 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_CLOVERVIEW, X86_FEATURE_ANY },
891 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_LINCROFT, X86_FEATURE_ANY },
892 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_PENWELL, X86_FEATURE_ANY },
893 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_PINEVIEW, X86_FEATURE_ANY },
894 { X86_VENDOR_CENTAUR, 5 },
895 { X86_VENDOR_INTEL, 5 },
896 { X86_VENDOR_NSC, 5 },
897 { X86_VENDOR_ANY, 4 },
898 {}
899};
900
Arnd Bergmann4b234a22018-02-02 22:39:23 +0100901static const __initconst struct x86_cpu_id cpu_no_meltdown[] = {
David Woodhousea8799fd2018-01-25 16:14:13 +0000902 { X86_VENDOR_AMD },
903 {}
904};
905
Konrad Rzeszutek Wilk24e4dd92018-04-25 22:04:20 -0400906static const __initconst struct x86_cpu_id cpu_no_spec_store_bypass[] = {
907 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_PINEVIEW },
908 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_LINCROFT },
909 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_PENWELL },
910 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_CLOVERVIEW },
911 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_CEDARVIEW },
912 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 },
913 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT },
914 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT2 },
915 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_MERRIFIELD },
916 { X86_VENDOR_INTEL, 6, INTEL_FAM6_CORE_YONAH },
917 { X86_VENDOR_INTEL, 6, INTEL_FAM6_XEON_PHI_KNL },
918 { X86_VENDOR_INTEL, 6, INTEL_FAM6_XEON_PHI_KNM },
919 { X86_VENDOR_CENTAUR, 5, },
920 { X86_VENDOR_INTEL, 5, },
921 { X86_VENDOR_NSC, 5, },
David Woodhousef8544342018-05-20 20:52:05 +0100922 { X86_VENDOR_AMD, 0x12, },
923 { X86_VENDOR_AMD, 0x11, },
924 { X86_VENDOR_AMD, 0x10, },
925 { X86_VENDOR_AMD, 0xf, },
Konrad Rzeszutek Wilk24e4dd92018-04-25 22:04:20 -0400926 { X86_VENDOR_ANY, 4, },
927 {}
928};
929
Andi Kleen432e99b2018-06-13 15:48:26 -0700930static const __initconst struct x86_cpu_id cpu_no_l1tf[] = {
931 /* in addition to cpu_no_speculation */
932 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 },
933 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT2 },
934 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT },
935 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_MERRIFIELD },
936 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_MOOREFIELD },
937 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_GOLDMONT },
938 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_DENVERTON },
939 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_GEMINI_LAKE },
940 { X86_VENDOR_INTEL, 6, INTEL_FAM6_XEON_PHI_KNL },
941 { X86_VENDOR_INTEL, 6, INTEL_FAM6_XEON_PHI_KNM },
942 {}
943};
944
Konrad Rzeszutek Wilk88659d52018-04-25 22:04:16 -0400945static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
David Woodhousea8799fd2018-01-25 16:14:13 +0000946{
947 u64 ia32_cap = 0;
948
Konrad Rzeszutek Wilk19e3a2b2018-04-25 22:04:22 -0400949 if (cpu_has(c, X86_FEATURE_ARCH_CAPABILITIES))
950 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
951
952 if (!x86_match_cpu(cpu_no_spec_store_bypass) &&
Konrad Rzeszutek Wilk3394ef12018-05-16 23:18:09 -0400953 !(ia32_cap & ARCH_CAP_SSB_NO))
Konrad Rzeszutek Wilk24e4dd92018-04-25 22:04:20 -0400954 setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
955
Konrad Rzeszutek Wilk88659d52018-04-25 22:04:16 -0400956 if (x86_match_cpu(cpu_no_speculation))
957 return;
958
959 setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
960 setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
961
Sai Praneethb0c05452018-08-01 11:42:25 -0700962 if (ia32_cap & ARCH_CAP_IBRS_ALL)
963 setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
964
David Woodhousea8799fd2018-01-25 16:14:13 +0000965 if (x86_match_cpu(cpu_no_meltdown))
Konrad Rzeszutek Wilk88659d52018-04-25 22:04:16 -0400966 return;
David Woodhousea8799fd2018-01-25 16:14:13 +0000967
David Woodhousea8799fd2018-01-25 16:14:13 +0000968 /* Rogue Data Cache Load? No! */
969 if (ia32_cap & ARCH_CAP_RDCL_NO)
Konrad Rzeszutek Wilk88659d52018-04-25 22:04:16 -0400970 return;
David Woodhousea8799fd2018-01-25 16:14:13 +0000971
Konrad Rzeszutek Wilk88659d52018-04-25 22:04:16 -0400972 setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
Andi Kleen432e99b2018-06-13 15:48:26 -0700973
974 if (x86_match_cpu(cpu_no_l1tf))
975 return;
976
977 setup_force_cpu_bug(X86_BUG_L1TF);
David Woodhousea8799fd2018-01-25 16:14:13 +0000978}
979
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100980/*
981 * Do minimum CPU detection early.
982 * Fields really needed: vendor, cpuid_level, family, model, mask,
983 * cache alignment.
984 * The others are not touched to avoid unwanted side effects.
985 *
986 * WARNING: this function is only called on the BP. Don't add code here
987 * that is supposed to run on all CPUs.
988 */
Yinghai Lu3da99c92008-09-04 21:09:44 +0200989static void __init early_identify_cpu(struct cpuinfo_x86 *c)
Rusty Russelld7cd5612006-12-07 02:14:08 +0100990{
Yinghai Lu6627d242008-09-04 20:09:10 -0700991#ifdef CONFIG_X86_64
992 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +0000993 c->x86_phys_bits = 36;
994 c->x86_virt_bits = 48;
Yinghai Lu6627d242008-09-04 20:09:10 -0700995#else
Huang, Yingd4387bd2008-01-31 22:05:45 +0100996 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +0000997 c->x86_phys_bits = 32;
998 c->x86_virt_bits = 32;
Yinghai Lu6627d242008-09-04 20:09:10 -0700999#endif
Yinghai Lu0a488a52008-09-04 21:09:47 +02001000 c->x86_cache_alignment = c->x86_clflush_size;
Rusty Russelld7cd5612006-12-07 02:14:08 +01001001
Yinghai Lu3da99c92008-09-04 21:09:44 +02001002 memset(&c->x86_capability, 0, sizeof c->x86_capability);
Yinghai Lu0a488a52008-09-04 21:09:47 +02001003 c->extended_cpuid_level = 0;
1004
Yinghai Luaef93c82008-09-14 02:33:15 -07001005 if (!have_cpuid_p())
1006 identify_cpu_without_cpuid(c);
1007
1008 /* cyrix could have cpuid enabled via c_identify()*/
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001009 if (have_cpuid_p()) {
1010 cpu_detect(c);
1011 get_cpu_vendor(c);
1012 get_cpu_cap(c);
Rusty Russelld7cd5612006-12-07 02:14:08 +01001013
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001014 if (this_cpu->c_early_init)
1015 this_cpu->c_early_init(c);
Krzysztof Helt12cf1052008-09-04 21:09:43 +02001016
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001017 c->cpu_index = 0;
1018 filter_cpuid_features(c, false);
Yinghai Lu3da99c92008-09-04 21:09:44 +02001019
Andy Lutomirski05fb3c12016-09-28 16:06:33 -07001020 if (this_cpu->c_bsp_init)
1021 this_cpu->c_bsp_init(c);
1022 }
Borislav Petkovc3b83592013-06-09 12:07:30 +02001023
1024 setup_force_cpu_cap(X86_FEATURE_ALWAYS);
Thomas Gleixnerd88f6012017-12-04 15:07:33 +01001025
Konrad Rzeszutek Wilk88659d52018-04-25 22:04:16 -04001026 cpu_set_bug_bits(c);
David Woodhouse26323fb2018-01-06 11:49:23 +00001027
Ingo Molnardb52ef72015-06-27 10:25:14 +02001028 fpu__init_system(c);
Andy Lutomirski00bcb5a2017-09-17 09:03:50 -07001029
1030#ifdef CONFIG_X86_32
1031 /*
1032 * Regardless of whether PCID is enumerated, the SDM says
1033 * that it can't be enabled in 32-bit mode.
1034 */
1035 setup_clear_cpu_cap(X86_FEATURE_PCID);
1036#endif
Rusty Russelld7cd5612006-12-07 02:14:08 +01001037}
1038
Yinghai Lu9d31d352008-09-04 21:09:44 +02001039void __init early_cpu_init(void)
1040{
Jan Beulich02dde8b2009-03-12 12:08:49 +00001041 const struct cpu_dev *const *cdev;
Yinghai Lu10a434f2008-09-04 21:09:45 +02001042 int count = 0;
Yinghai Lu9d31d352008-09-04 21:09:44 +02001043
Jan Beulichac23f252011-03-04 15:52:35 +00001044#ifdef CONFIG_PROCESSOR_SELECT
Chen Yucong1b74dde2016-02-02 11:45:02 +08001045 pr_info("KERNEL supported cpus:\n");
Ingo Molnar31c997c2009-11-14 10:34:41 +01001046#endif
1047
Yinghai Lu10a434f2008-09-04 21:09:45 +02001048 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
Jan Beulich02dde8b2009-03-12 12:08:49 +00001049 const struct cpu_dev *cpudev = *cdev;
Yinghai Lu9d31d352008-09-04 21:09:44 +02001050
Yinghai Lu10a434f2008-09-04 21:09:45 +02001051 if (count >= X86_VENDOR_NUM)
1052 break;
1053 cpu_devs[count] = cpudev;
1054 count++;
1055
Jan Beulichac23f252011-03-04 15:52:35 +00001056#ifdef CONFIG_PROCESSOR_SELECT
Ingo Molnar31c997c2009-11-14 10:34:41 +01001057 {
1058 unsigned int j;
Yinghai Lu10a434f2008-09-04 21:09:45 +02001059
Ingo Molnar31c997c2009-11-14 10:34:41 +01001060 for (j = 0; j < 2; j++) {
1061 if (!cpudev->c_ident[j])
1062 continue;
Chen Yucong1b74dde2016-02-02 11:45:02 +08001063 pr_info(" %s %s\n", cpudev->c_vendor,
Ingo Molnar31c997c2009-11-14 10:34:41 +01001064 cpudev->c_ident[j]);
1065 }
Yinghai Lu9d31d352008-09-04 21:09:44 +02001066 }
Dave Jones03884232009-11-13 15:30:00 -05001067#endif
Ingo Molnar31c997c2009-11-14 10:34:41 +01001068 }
Yinghai Lu9d31d352008-09-04 21:09:44 +02001069 early_identify_cpu(&boot_cpu_data);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08001070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001072/*
Borislav Petkov366d4a42010-10-04 09:31:27 +02001073 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1074 * unfortunately, that's not true in practice because of early VIA
1075 * chips and (more importantly) broken virtualizers that are not easy
1076 * to detect. In the latter case it doesn't even *fail* reliably, so
1077 * probing for it doesn't even work. Disable it completely on 32-bit
H. Peter Anvinba0593b2008-09-16 09:29:40 -07001078 * unless we can find a reliable way to detect all the broken cases.
Borislav Petkov366d4a42010-10-04 09:31:27 +02001079 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001080 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001081static void detect_nopl(struct cpuinfo_x86 *c)
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001082{
Borislav Petkov366d4a42010-10-04 09:31:27 +02001083#ifdef CONFIG_X86_32
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001084 clear_cpu_cap(c, X86_FEATURE_NOPL);
Borislav Petkov366d4a42010-10-04 09:31:27 +02001085#else
1086 set_cpu_cap(c, X86_FEATURE_NOPL);
1087#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001090static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
1091{
1092#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001094 * Empirically, writing zero to a segment selector on AMD does
1095 * not clear the base, whereas writing zero to a segment
1096 * selector on Intel does clear the base. Intel's behavior
1097 * allows slightly faster context switches in the common case
1098 * where GS is unused by the prev and next threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 *
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001100 * Since neither vendor documents this anywhere that I can see,
1101 * detect it directly instead of hardcoding the choice by
1102 * vendor.
1103 *
1104 * I've designated AMD's behavior as the "bug" because it's
1105 * counterintuitive and less friendly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001107
1108 unsigned long old_base, tmp;
1109 rdmsrl(MSR_FS_BASE, old_base);
1110 wrmsrl(MSR_FS_BASE, 1);
1111 loadsegment(fs, 0);
1112 rdmsrl(MSR_FS_BASE, tmp);
1113 if (tmp != 0)
1114 set_cpu_bug(c, X86_BUG_NULL_SEG);
1115 wrmsrl(MSR_FS_BASE, old_base);
Yinghai Lu3da99c92008-09-04 21:09:44 +02001116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
Yinghai Luaef93c82008-09-14 02:33:15 -07001118
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001119static void generic_identify(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 c->extended_cpuid_level = 0;
1122
Yinghai Luaef93c82008-09-14 02:33:15 -07001123 if (!have_cpuid_p())
1124 identify_cpu_without_cpuid(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001125
Yinghai Luaef93c82008-09-14 02:33:15 -07001126 /* cyrix could have cpuid enabled via c_identify()*/
Ingo Molnara9853dd2008-09-14 14:46:58 +02001127 if (!have_cpuid_p())
Yinghai Luaef93c82008-09-14 02:33:15 -07001128 return;
1129
Yinghai Lu3da99c92008-09-04 21:09:44 +02001130 cpu_detect(c);
1131
1132 get_cpu_vendor(c);
1133
1134 get_cpu_cap(c);
1135
1136 if (c->cpuid_level >= 0x00000001) {
1137 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
Yinghai Lub89d3b32008-09-04 20:09:12 -07001138#ifdef CONFIG_X86_32
Borislav Petkovc8e56d22015-06-04 18:55:25 +02001139# ifdef CONFIG_SMP
Ingo Molnarcb8cc442009-01-28 13:24:54 +01001140 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lub89d3b32008-09-04 20:09:12 -07001141# else
Yinghai Lu3da99c92008-09-04 21:09:44 +02001142 c->apicid = c->initial_apicid;
Yinghai Lub89d3b32008-09-04 20:09:12 -07001143# endif
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08001144#endif
Yinghai Lub89d3b32008-09-04 20:09:12 -07001145 c->phys_proc_id = c->initial_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
Yinghai Lu3da99c92008-09-04 21:09:44 +02001147
Yinghai Lu1b05d602008-09-06 01:52:27 -07001148 get_model_name(c); /* Default name */
Yinghai Lu3da99c92008-09-04 21:09:44 +02001149
Yinghai Lu3da99c92008-09-04 21:09:44 +02001150 detect_nopl(c);
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001151
1152 detect_null_seg_behavior(c);
Andy Lutomirski0230bb02016-04-07 17:31:48 -07001153
1154 /*
1155 * ESPFIX is a strange bug. All real CPUs have it. Paravirt
1156 * systems that run Linux at CPL > 0 may or may not have the
1157 * issue, but, even if they have the issue, there's absolutely
1158 * nothing we can do about it because we can't use the real IRET
1159 * instruction.
1160 *
1161 * NB: For the time being, only 32-bit kernels support
1162 * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
1163 * whether to apply espfix using paravirt hooks. If any
1164 * non-paravirt system ever shows up that does *not* have the
1165 * ESPFIX issue, we can change this.
1166 */
1167#ifdef CONFIG_X86_32
1168# ifdef CONFIG_PARAVIRT
1169 do {
1170 extern void native_iret(void);
1171 if (pv_cpu_ops.iret == native_iret)
1172 set_cpu_bug(c, X86_BUG_ESPFIX);
1173 } while (0);
1174# else
1175 set_cpu_bug(c, X86_BUG_ESPFIX);
1176# endif
1177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001180static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1181{
1182 /*
1183 * The heavy lifting of max_rmid and cache_occ_scale are handled
1184 * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
1185 * in case CQM bits really aren't there in this CPU.
1186 */
1187 if (c != &boot_cpu_data) {
1188 boot_cpu_data.x86_cache_max_rmid =
1189 min(boot_cpu_data.x86_cache_max_rmid,
1190 c->x86_cache_max_rmid);
1191 }
1192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194/*
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001195 * Validate that ACPI/mptables have the same information about the
1196 * effective APIC id and update the package map.
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001197 */
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001198static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001199{
1200#ifdef CONFIG_SMP
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001201 unsigned int apicid, cpu = smp_processor_id();
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001202
1203 apicid = apic->cpu_present_to_apicid(cpu);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001204
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001205 if (apicid != c->apicid) {
1206 pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001207 cpu, apicid, c->initial_apicid);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001208 }
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001209 BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001210#else
1211 c->logical_proc_id = 0;
1212#endif
1213}
1214
1215/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 * This does the hard work of actually picking apart the CPU stuff...
1217 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001218static void identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 int i;
1221
1222 c->loops_per_jiffy = loops_per_jiffy;
Gustavo A. R. Silvaaa72eec2018-02-13 13:22:08 -06001223 c->x86_cache_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 c->x86_vendor = X86_VENDOR_UNKNOWN;
Jia Zhang06be0072018-01-01 09:52:10 +08001225 c->x86_model = c->x86_stepping = 0; /* So far unknown... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 c->x86_vendor_id[0] = '\0'; /* Unset */
1227 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001228 c->x86_max_cores = 1;
Yinghai Lu102bbe32008-09-04 20:09:13 -07001229 c->x86_coreid_bits = 0;
Borislav Petkov6e306c52017-02-05 11:50:21 +01001230 c->cu_id = 0xff;
Yinghai Lu11fdd252008-09-07 17:58:50 -07001231#ifdef CONFIG_X86_64
Yinghai Lu102bbe32008-09-04 20:09:13 -07001232 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +00001233 c->x86_phys_bits = 36;
1234 c->x86_virt_bits = 48;
Yinghai Lu102bbe32008-09-04 20:09:13 -07001235#else
1236 c->cpuid_level = -1; /* CPUID not detected */
Andi Kleen770d1322006-12-07 02:14:05 +01001237 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +00001238 c->x86_phys_bits = 32;
1239 c->x86_virt_bits = 32;
Yinghai Lu102bbe32008-09-04 20:09:13 -07001240#endif
1241 c->x86_cache_alignment = c->x86_clflush_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 memset(&c->x86_capability, 0, sizeof c->x86_capability);
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 generic_identify(c);
1245
Andi Kleen38985342008-01-30 13:32:49 +01001246 if (this_cpu->c_identify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 this_cpu->c_identify(c);
1248
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001249 /* Clear/Set all flags overridden by options, after probe */
Andy Lutomirskief463982017-01-18 11:15:38 -08001250 apply_forced_caps(c);
Yinghai Lu2759c322009-05-15 13:05:16 -07001251
Yinghai Lu102bbe32008-09-04 20:09:13 -07001252#ifdef CONFIG_X86_64
Ingo Molnarcb8cc442009-01-28 13:24:54 +01001253 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lu102bbe32008-09-04 20:09:13 -07001254#endif
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
1257 * Vendor-specific initialization. In this section we
1258 * canonicalize the feature flags, meaning if there are
1259 * features a certain CPU supports which CPUID doesn't
1260 * tell us, CPUID claiming incorrect flags, or other bugs,
1261 * we handle them here.
1262 *
1263 * At the end of this section, c->x86_capability better
1264 * indicate the features this CPU genuinely supports!
1265 */
1266 if (this_cpu->c_init)
1267 this_cpu->c_init(c);
1268
1269 /* Disable the PN if appropriate */
1270 squash_the_stupid_serial_number(c);
1271
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -07001272 /* Set up SMEP/SMAP */
1273 setup_smep(c);
1274 setup_smap(c);
1275
Andy Lutomirskib52f9372017-06-29 08:53:21 -07001276 /* Set up PCID */
1277 setup_pcid(c);
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001280 * The vendor-specific functions might have changed features.
1281 * Now we do "generic changes."
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
1283
H. Peter Anvinb38b0662009-01-23 17:20:50 -08001284 /* Filter out anything that depends on CPUID levels we don't have */
1285 filter_cpuid_features(c, true);
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /* If the model name is still unset, do table lookup. */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001288 if (!c->x86_model_id[0]) {
Jan Beulich02dde8b2009-03-12 12:08:49 +00001289 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 p = table_lookup_model(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001291 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 strcpy(c->x86_model_id, p);
1293 else
1294 /* Last resort... */
1295 sprintf(c->x86_model_id, "%02x/%02x",
Chuck Ebbert54a20f82006-03-23 02:59:36 -08001296 c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
Yinghai Lu102bbe32008-09-04 20:09:13 -07001299#ifdef CONFIG_X86_64
1300 detect_ht(c);
1301#endif
1302
Alok Kataria88b094f2008-10-27 10:41:46 -07001303 init_hypervisor(c);
H. Peter Anvin49d859d2011-07-31 14:02:19 -07001304 x86_init_rdrand(c);
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001305 x86_init_cache_qos(c);
Dave Hansen06976942016-02-12 13:02:29 -08001306 setup_pku(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001307
1308 /*
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001309 * Clear/Set all flags overridden by options, need do it
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001310 * before following smp all cpus cap AND.
1311 */
Andy Lutomirskief463982017-01-18 11:15:38 -08001312 apply_forced_caps(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /*
1315 * On SMP, boot_cpu_data holds the common feature set between
1316 * all CPUs; so make sure that we indicate which features are
1317 * common between the CPUs. The first time this routine gets
1318 * executed, c == &boot_cpu_data.
1319 */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001320 if (c != &boot_cpu_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* AND the already accumulated flags with these */
Yinghai Lu9d31d352008-09-04 21:09:44 +02001322 for (i = 0; i < NCAPINTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
Borislav Petkov65fc9852013-03-20 15:07:23 +01001324
1325 /* OR, i.e. replicate the bug flags */
1326 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
1327 c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329
1330 /* Init Machine Check Exception if available. */
Borislav Petkov5e099542009-10-16 12:31:32 +02001331 mcheck_cpu_init(c);
Andi Kleen30d432d2008-01-30 13:33:16 +01001332
1333 select_idle_routine(c);
Yinghai Lu102bbe32008-09-04 20:09:13 -07001334
Tejun Heode2d9442011-01-23 14:37:41 +01001335#ifdef CONFIG_NUMA
Yinghai Lu102bbe32008-09-04 20:09:13 -07001336 numa_add_cpu(smp_processor_id());
1337#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001338}
Shaohua Li31ab2692005-11-07 00:58:42 -08001339
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001340/*
1341 * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
1342 * on 32-bit kernels:
1343 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001344#ifdef CONFIG_X86_32
1345void enable_sep_cpu(void)
1346{
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001347 struct tss_struct *tss;
1348 int cpu;
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001349
Borislav Petkovb3edfda2016-03-16 13:19:29 +01001350 if (!boot_cpu_has(X86_FEATURE_SEP))
1351 return;
1352
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001353 cpu = get_cpu();
1354 tss = &per_cpu(cpu_tss, cpu);
1355
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001356 /*
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001357 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1358 * see the big comment in struct x86_hw_tss's definition.
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001359 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001360
1361 tss->x86_tss.ss1 = __KERNEL_CS;
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001362 wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
1363
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001364 wrmsr(MSR_IA32_SYSENTER_ESP,
1365 (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
1366 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001367
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001368 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001369
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001370 put_cpu();
1371}
Glauber Costae04d6452008-09-22 14:35:08 -03001372#endif
1373
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001374void __init identify_boot_cpu(void)
1375{
1376 identify_cpu(&boot_cpu_data);
Len Brown02c68a02011-04-01 16:59:53 -04001377 init_amd_e400_c1e_mask();
Yinghai Lu102bbe32008-09-04 20:09:13 -07001378#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001379 sysenter_setup();
Li Shaohua6fe940d2005-06-25 14:54:53 -07001380 enable_sep_cpu();
Yinghai Lu102bbe32008-09-04 20:09:13 -07001381#endif
Borislav Petkov5b556332012-08-06 19:00:37 +02001382 cpu_detect_tlb(&boot_cpu_data);
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001383}
Shaohua Li3b520b22005-07-07 17:56:38 -07001384
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001385void identify_secondary_cpu(struct cpuinfo_x86 *c)
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001386{
1387 BUG_ON(c == &boot_cpu_data);
1388 identify_cpu(c);
Yinghai Lu102bbe32008-09-04 20:09:13 -07001389#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001390 enable_sep_cpu();
Yinghai Lu102bbe32008-09-04 20:09:13 -07001391#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001392 mtrr_ap_init();
Thomas Gleixnera035dc62016-12-12 11:04:53 +01001393 validate_apic_and_package_id(c);
Konrad Rzeszutek Wilk19e3a2b2018-04-25 22:04:22 -04001394 x86_spec_ctrl_setup_ap();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Yinghai Lua0854a42008-09-04 21:09:46 +02001397struct msr_range {
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001398 unsigned min;
1399 unsigned max;
Yinghai Lua0854a42008-09-04 21:09:46 +02001400};
1401
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001402static const struct msr_range msr_range_array[] = {
Yinghai Lua0854a42008-09-04 21:09:46 +02001403 { 0x00000000, 0x00000418},
1404 { 0xc0000000, 0xc000040b},
1405 { 0xc0010000, 0xc0010142},
1406 { 0xc0011000, 0xc001103b},
1407};
1408
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001409static void __print_cpu_msr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001411 unsigned index_min, index_max;
Yinghai Lua0854a42008-09-04 21:09:46 +02001412 unsigned index;
1413 u64 val;
1414 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Yinghai Lua0854a42008-09-04 21:09:46 +02001416 for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
1417 index_min = msr_range_array[i].min;
1418 index_max = msr_range_array[i].max;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001419
Yinghai Lua0854a42008-09-04 21:09:46 +02001420 for (index = index_min; index < index_max; index++) {
Borislav Petkovecd431d2012-06-01 16:52:36 +02001421 if (rdmsrl_safe(index, &val))
Yinghai Lua0854a42008-09-04 21:09:46 +02001422 continue;
Chen Yucong1b74dde2016-02-02 11:45:02 +08001423 pr_info(" MSR%08x: %016llx\n", index, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426}
Yinghai Lua0854a42008-09-04 21:09:46 +02001427
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001428static int show_msr;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001429
Yinghai Lua0854a42008-09-04 21:09:46 +02001430static __init int setup_show_msr(char *arg)
1431{
1432 int num;
1433
1434 get_option(&arg, &num);
1435
1436 if (num > 0)
1437 show_msr = num;
1438 return 1;
1439}
1440__setup("show_msr=", setup_show_msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Andi Kleen191679f2008-01-30 13:33:21 +01001442static __init int setup_noclflush(char *arg)
1443{
H. Peter Anvin840d2832014-02-27 08:31:30 -08001444 setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
H. Peter Anvinda4aaa72014-02-27 08:36:31 -08001445 setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
Andi Kleen191679f2008-01-30 13:33:21 +01001446 return 1;
1447}
1448__setup("noclflush", setup_noclflush);
1449
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001450void print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Jan Beulich02dde8b2009-03-12 12:08:49 +00001452 const char *vendor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001454 if (c->x86_vendor < X86_VENDOR_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 vendor = this_cpu->c_vendor;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001456 } else {
1457 if (c->cpuid_level >= 0)
1458 vendor = c->x86_vendor_id;
1459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Yinghai Lubd32a8cf2008-09-19 18:41:16 -07001461 if (vendor && !strstr(c->x86_model_id, vendor))
Chen Yucong1b74dde2016-02-02 11:45:02 +08001462 pr_cont("%s ", vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Yinghai Lu9d31d352008-09-04 21:09:44 +02001464 if (c->x86_model_id[0])
Chen Yucong1b74dde2016-02-02 11:45:02 +08001465 pr_cont("%s", c->x86_model_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001467 pr_cont("%d86", c->x86);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Chen Yucong1b74dde2016-02-02 11:45:02 +08001469 pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
Borislav Petkov924e1012012-09-14 18:37:46 +02001470
Jia Zhang06be0072018-01-01 09:52:10 +08001471 if (c->x86_stepping || c->cpuid_level >= 0)
1472 pr_cont(", stepping: 0x%x)\n", c->x86_stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001474 pr_cont(")\n");
Yinghai Lua0854a42008-09-04 21:09:46 +02001475
Yinghai Lu0b8b8072012-03-22 21:31:43 -07001476 print_cpu_msr(c);
Yinghai Lu21c3fcf2012-02-12 09:53:57 -08001477}
1478
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001479void print_cpu_msr(struct cpuinfo_x86 *c)
Yinghai Lu21c3fcf2012-02-12 09:53:57 -08001480{
Yinghai Lua0854a42008-09-04 21:09:46 +02001481 if (c->cpu_index < show_msr)
Yinghai Lu21c3fcf2012-02-12 09:53:57 -08001482 __print_cpu_msr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Andi Kleenac72e782008-01-30 13:33:21 +01001485static __init int setup_disablecpuid(char *arg)
1486{
1487 int bit;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001488
Lukasz Odzioba9bae3c32016-12-28 14:55:40 +01001489 if (get_option(&arg, &bit) && bit >= 0 && bit < NCAPINTS * 32)
Andi Kleenac72e782008-01-30 13:33:21 +01001490 setup_clear_cpu_cap(bit);
1491 else
1492 return 0;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001493
Andi Kleenac72e782008-01-30 13:33:21 +01001494 return 1;
1495}
1496__setup("clearcpuid=", setup_disablecpuid);
1497
Yinghai Lud5494d42008-09-04 20:09:03 -07001498#ifdef CONFIG_X86_64
Kees Cook404f6aa2016-08-08 16:29:06 -07001499struct desc_ptr idt_descr __ro_after_init = {
1500 .size = NR_VECTORS * 16 - 1,
1501 .address = (unsigned long) idt_table,
1502};
1503const struct desc_ptr debug_idt_descr = {
1504 .size = NR_VECTORS * 16 - 1,
1505 .address = (unsigned long) debug_idt_table,
1506};
Yinghai Lud5494d42008-09-04 20:09:03 -07001507
Brian Gerst947e76c2009-01-19 12:21:28 +09001508DEFINE_PER_CPU_FIRST(union irq_stack_union,
Andi Kleen277d5b42013-08-05 15:02:43 -07001509 irq_stack_union) __aligned(PAGE_SIZE) __visible;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001510
Tejun Heobdf977b2009-08-03 14:12:19 +09001511/*
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001512 * The following percpu variables are hot. Align current_task to
1513 * cacheline size such that they fall in the same cacheline.
Tejun Heobdf977b2009-08-03 14:12:19 +09001514 */
1515DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1516 &init_task;
1517EXPORT_PER_CPU_SYMBOL(current_task);
Yinghai Lud5494d42008-09-04 20:09:03 -07001518
Tejun Heobdf977b2009-08-03 14:12:19 +09001519DEFINE_PER_CPU(char *, irq_stack_ptr) =
Josh Poimboeuf4950d6d2016-08-18 10:59:08 -05001520 init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE;
Tejun Heobdf977b2009-08-03 14:12:19 +09001521
Andi Kleen277d5b42013-08-05 15:02:43 -07001522DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
Yinghai Lud5494d42008-09-04 20:09:03 -07001523
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001524DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1525EXPORT_PER_CPU_SYMBOL(__preempt_count);
1526
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001527/*
1528 * Special IST stacks which the CPU switches to when it calls
1529 * an IST-marked descriptor entry. Up to 7 stacks (hardware
1530 * limit), all of them are 4K, except the debug stack which
1531 * is 8K.
1532 */
1533static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
1534 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
1535 [DEBUG_STACK - 1] = DEBUG_STKSZ
1536};
1537
Richard Fellner13be4482017-05-04 14:26:50 +02001538DEFINE_PER_CPU_PAGE_ALIGNED_USER_MAPPED(char, exception_stacks
Tejun Heo3e352aa2009-08-03 14:10:11 +09001539 [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
Yinghai Lud5494d42008-09-04 20:09:03 -07001540
Yinghai Lud5494d42008-09-04 20:09:03 -07001541/* May not be marked __init: used by software suspend */
1542void syscall_init(void)
1543{
Borislav Petkov31ac34c2015-11-23 11:12:25 +01001544 wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
Andy Lutomirski47edb652015-07-23 12:14:40 -07001545 wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001546
1547#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski47edb652015-07-23 12:14:40 -07001548 wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001549 /*
Denys Vlasenko487d1ed2015-03-27 11:59:16 +01001550 * This only works on Intel CPUs.
1551 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1552 * This does not cause SYSENTER to jump to the wrong location, because
1553 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001554 */
1555 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1556 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001557 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001558#else
Andy Lutomirski47edb652015-07-23 12:14:40 -07001559 wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
Borislav Petkov6b513112015-04-03 14:25:28 +02001560 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001561 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1562 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
Yinghai Lud5494d42008-09-04 20:09:03 -07001563#endif
1564
1565 /* Flags to clear on syscall */
1566 wrmsrl(MSR_SYSCALL_MASK,
H. Peter Anvin63bcff22012-09-21 12:43:12 -07001567 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
Andy Lutomirski8c7aa692014-10-01 11:49:04 -07001568 X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
Yinghai Lud5494d42008-09-04 20:09:03 -07001569}
1570
Yinghai Lud5494d42008-09-04 20:09:03 -07001571/*
1572 * Copies of the original ist values from the tss are only accessed during
1573 * debugging, no special alignment required.
1574 */
1575DEFINE_PER_CPU(struct orig_ist, orig_ist);
1576
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001577static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
Steven Rostedt42181182011-12-16 11:43:02 -05001578DEFINE_PER_CPU(int, debug_stack_usage);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001579
1580int is_debug_stack(unsigned long addr)
1581{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001582 return __this_cpu_read(debug_stack_usage) ||
1583 (addr <= __this_cpu_read(debug_stack_addr) &&
1584 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001585}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001586NOKPROBE_SYMBOL(is_debug_stack);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001587
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001588DEFINE_PER_CPU(u32, debug_idt_ctr);
Steven Rostedtf8988172012-05-30 11:47:00 -04001589
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001590void debug_stack_set_zero(void)
1591{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001592 this_cpu_inc(debug_idt_ctr);
1593 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001594}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001595NOKPROBE_SYMBOL(debug_stack_set_zero);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001596
1597void debug_stack_reset(void)
1598{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001599 if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
Steven Rostedtf8988172012-05-30 11:47:00 -04001600 return;
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001601 if (this_cpu_dec_return(debug_idt_ctr) == 0)
1602 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001603}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001604NOKPROBE_SYMBOL(debug_stack_reset);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001605
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001606#else /* CONFIG_X86_64 */
Yinghai Lud5494d42008-09-04 20:09:03 -07001607
Tejun Heobdf977b2009-08-03 14:12:19 +09001608DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1609EXPORT_PER_CPU_SYMBOL(current_task);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001610DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1611EXPORT_PER_CPU_SYMBOL(__preempt_count);
Tejun Heobdf977b2009-08-03 14:12:19 +09001612
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001613/*
1614 * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1615 * the top of the kernel stack. Use an extra percpu variable to track the
1616 * top of the kernel stack directly.
1617 */
1618DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1619 (unsigned long)&init_thread_union + THREAD_SIZE;
1620EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1621
Tejun Heo60a53172009-02-09 22:17:40 +09001622#ifdef CONFIG_CC_STACKPROTECTOR
Jeremy Fitzhardinge53f82452009-09-03 14:31:44 -07001623DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
Tejun Heo60a53172009-02-09 22:17:40 +09001624#endif
1625
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001626#endif /* CONFIG_X86_64 */
Jeremy Fitzhardingec5413fb2007-05-02 19:27:16 +02001627
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001628/*
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301629 * Clear all 6 debug registers:
1630 */
1631static void clear_all_debug_regs(void)
1632{
1633 int i;
1634
1635 for (i = 0; i < 8; i++) {
1636 /* Ignore db4, db5 */
1637 if ((i == 4) || (i == 5))
1638 continue;
1639
1640 set_debugreg(0, i);
1641 }
1642}
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +01001643
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001644#ifdef CONFIG_KGDB
1645/*
1646 * Restore debug regs if using kgdbwait and you have a kernel debugger
1647 * connection established.
1648 */
1649static void dbg_restore_debug_regs(void)
1650{
1651 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1652 arch_kgdb_ops.correct_hw_break();
1653}
1654#else /* ! CONFIG_KGDB */
1655#define dbg_restore_debug_regs()
1656#endif /* ! CONFIG_KGDB */
1657
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001658static void wait_for_master_cpu(int cpu)
1659{
1660#ifdef CONFIG_SMP
1661 /*
1662 * wait for ACK from master CPU before continuing
1663 * with AP initialization
1664 */
1665 WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1666 while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1667 cpu_relax();
1668#endif
1669}
1670
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001671/*
1672 * cpu_init() initializes state that is per-CPU. Some data is already
1673 * initialized (naturally) in the bootstrap process, such as the GDT
1674 * and IDT. We reload them nevertheless, this function acts as a
1675 * 'CPU state barrier', nothing should get across.
Yinghai Lu1ba76582008-09-04 20:09:04 -07001676 * A lot of state is already set up in PDA init for 64 bit
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001677 */
Yinghai Lu1ba76582008-09-04 20:09:04 -07001678#ifdef CONFIG_X86_64
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001679
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001680void cpu_init(void)
Yinghai Lu1ba76582008-09-04 20:09:04 -07001681{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001682 struct orig_ist *oist;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001683 struct task_struct *me;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001684 struct tss_struct *t;
1685 unsigned long v;
Andy Lutomirskifb598312016-07-14 13:22:58 -07001686 int cpu = raw_smp_processor_id();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001687 int i;
1688
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001689 wait_for_master_cpu(cpu);
1690
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001691 /*
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001692 * Initialize the CR4 shadow before doing anything that could
1693 * try to read it.
1694 */
1695 cr4_init_shadow();
Hugh Dickins23e09432017-09-24 16:59:49 -07001696 if (!kaiser_enabled) {
1697 /*
1698 * secondary_startup_64() deferred setting PGE in cr4:
1699 * probe_page_size_mask() sets it on the boot cpu,
1700 * but it needs to be set on each secondary cpu.
1701 */
1702 cr4_set_bits(X86_CR4_PGE);
1703 }
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001704
1705 /*
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001706 * Load microcode on this cpu if a valid microcode is available.
1707 * This is early microcode loading procedure.
1708 */
1709 load_ucode_ap();
1710
Andy Lutomirski24933b82015-03-05 19:19:05 -08001711 t = &per_cpu(cpu_tss, cpu);
Tejun Heo0fe1e002009-10-29 22:34:14 +09001712 oist = &per_cpu(orig_ist, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001713
Brian Gerste7a22c12009-01-19 00:38:59 +09001714#ifdef CONFIG_NUMA
Fenghua Yu27fd1852012-11-13 11:32:47 -08001715 if (this_cpu_read(numa_node) == 0 &&
Lee Schermerhorne534c7c2010-05-26 14:44:58 -07001716 early_cpu_to_node(cpu) != NUMA_NO_NODE)
1717 set_numa_node(early_cpu_to_node(cpu));
Brian Gerste7a22c12009-01-19 00:38:59 +09001718#endif
Yinghai Lu1ba76582008-09-04 20:09:04 -07001719
1720 me = current;
1721
Mike Travis2eaad1f2009-12-10 17:19:36 -08001722 pr_debug("Initializing CPU#%d\n", cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001723
Andy Lutomirski375074c2014-10-24 15:58:07 -07001724 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001725
1726 /*
1727 * Initialize the per-CPU GDT with the boot GDT,
1728 * and set up the GDT descriptor:
1729 */
1730
Brian Gerst552be872009-01-30 17:47:53 +09001731 switch_to_new_gdt(cpu);
Brian Gerst2697fbd2009-01-27 12:56:48 +09001732 loadsegment(fs, 0);
1733
Seiji Aguchicf910e82013-06-20 11:46:53 -04001734 load_current_idt();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001735
1736 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1737 syscall_init();
1738
1739 wrmsrl(MSR_FS_BASE, 0);
1740 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1741 barrier();
1742
H. Peter Anvin4763ed42009-11-13 15:28:16 -08001743 x86_configure_nx();
Thomas Gleixner659006b2015-01-15 21:22:26 +00001744 x2apic_setup();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001745
1746 /*
1747 * set up and load the per-CPU TSS
1748 */
Tejun Heo0fe1e002009-10-29 22:34:14 +09001749 if (!oist->ist[0]) {
Brian Gerst92d65b22009-01-19 00:38:58 +09001750 char *estacks = per_cpu(exception_stacks, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001751
Yinghai Lu1ba76582008-09-04 20:09:04 -07001752 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001753 estacks += exception_stack_sizes[v];
Tejun Heo0fe1e002009-10-29 22:34:14 +09001754 oist->ist[v] = t->x86_tss.ist[v] =
Yinghai Lu1ba76582008-09-04 20:09:04 -07001755 (unsigned long)estacks;
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001756 if (v == DEBUG_STACK-1)
1757 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001758 }
1759 }
1760
1761 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001762
Yinghai Lu1ba76582008-09-04 20:09:04 -07001763 /*
1764 * <= is required because the CPU will access up to
1765 * 8 bits beyond the end of the IO permission bitmap.
1766 */
1767 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1768 t->io_bitmap[i] = ~0UL;
1769
1770 atomic_inc(&init_mm.mm_count);
1771 me->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001772 BUG_ON(me->mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001773 enter_lazy_tlb(&init_mm, me);
1774
1775 load_sp0(t, &current->thread);
1776 set_tss_desc(cpu, t);
1777 load_TR_desc();
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001778 load_mm_ldt(&init_mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001779
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001780 clear_all_debug_regs();
1781 dbg_restore_debug_regs();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001782
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001783 fpu__init_cpu();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001784
Yinghai Lu1ba76582008-09-04 20:09:04 -07001785 if (is_uv_system())
1786 uv_cpu_init();
1787}
1788
1789#else
1790
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001791void cpu_init(void)
James Bottomley9ee79a32007-01-22 09:18:31 -06001792{
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001793 int cpu = smp_processor_id();
1794 struct task_struct *curr = current;
Andy Lutomirski24933b82015-03-05 19:19:05 -08001795 struct tss_struct *t = &per_cpu(cpu_tss, cpu);
James Bottomley9ee79a32007-01-22 09:18:31 -06001796 struct thread_struct *thread = &curr->thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001798 wait_for_master_cpu(cpu);
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001799
Steven Rostedt5b2bdbc2015-02-27 14:50:19 -05001800 /*
1801 * Initialize the CR4 shadow before doing anything that could
1802 * try to read it.
1803 */
1804 cr4_init_shadow();
1805
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001806 show_ucode_info_early();
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001807
Chen Yucong1b74dde2016-02-02 11:45:02 +08001808 pr_info("Initializing CPU#%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Borislav Petkov362f9242015-12-07 10:39:41 +01001810 if (cpu_feature_enabled(X86_FEATURE_VME) ||
Borislav Petkov59e21e32016-04-04 22:24:59 +02001811 boot_cpu_has(X86_FEATURE_TSC) ||
Borislav Petkov362f9242015-12-07 10:39:41 +01001812 boot_cpu_has(X86_FEATURE_DE))
Andy Lutomirski375074c2014-10-24 15:58:07 -07001813 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Seiji Aguchicf910e82013-06-20 11:46:53 -04001815 load_current_idt();
Brian Gerst552be872009-01-30 17:47:53 +09001816 switch_to_new_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 * Set up and load the per-CPU TSS and LDT
1820 */
1821 atomic_inc(&init_mm.mm_count);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001822 curr->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001823 BUG_ON(curr->mm);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001824 enter_lazy_tlb(&init_mm, curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
H. Peter Anvinfaca6222008-01-30 13:31:02 +01001826 load_sp0(t, thread);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001827 set_tss_desc(cpu, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 load_TR_desc();
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001829 load_mm_ldt(&init_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Thomas Gleixnerf9a196b2009-05-01 20:59:25 +02001831 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1832
Matt Mackall22c4e302006-01-08 01:05:24 -08001833#ifdef CONFIG_DOUBLEFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /* Set up doublefault TSS pointer in the GDT */
1835 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
Matt Mackall22c4e302006-01-08 01:05:24 -08001836#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301838 clear_all_debug_regs();
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001839 dbg_restore_debug_regs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001841 fpu__init_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
Yinghai Lu1ba76582008-09-04 20:09:04 -07001843#endif
Borislav Petkov5700f742013-06-09 12:07:32 +02001844
Laura Abbottb51ef522015-07-20 14:47:58 -07001845static void bsp_resume(void)
1846{
1847 if (this_cpu->c_bsp_resume)
1848 this_cpu->c_bsp_resume(&boot_cpu_data);
1849}
1850
1851static struct syscore_ops cpu_syscore_ops = {
1852 .resume = bsp_resume,
1853};
1854
1855static int __init init_cpu_syscore(void)
1856{
1857 register_syscore_ops(&cpu_syscore_ops);
1858 return 0;
1859}
1860core_initcall(init_cpu_syscore);