blob: 430f950b0b7fa3ad5baa10008cb92b6cc16bf54e [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>
Ingo Molnar68e21be2017-02-01 19:08:20 +010010#include <linux/sched/mm.h>
Ingo Molnare6017572017-02-01 16:36:40 +010011#include <linux/sched/clock.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010012#include <linux/sched/task.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053013#include <linux/init.h>
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +090014#include <linux/kprobes.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053015#include <linux/kgdb.h>
16#include <linux/smp.h>
17#include <linux/io.h>
Laura Abbottb51ef522015-07-20 14:47:58 -070018#include <linux/syscore_ops.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053019
20#include <asm/stackprotector.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020021#include <asm/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/mmu_context.h>
H. Peter Anvin49d859d2011-07-31 14:02:19 -070023#include <asm/archrandom.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053024#include <asm/hypervisor.h>
25#include <asm/processor.h>
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070026#include <asm/tlbflush.h>
Paul Gortmakerf649e932012-01-20 16:24:09 -050027#include <asm/debugreg.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053028#include <asm/sections.h>
Andy Lutomirskif40c3302014-05-05 12:19:36 -070029#include <asm/vsyscall.h>
Alan Cox8bdbd962009-07-04 00:35:45 +010030#include <linux/topology.h>
31#include <linux/cpumask.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053032#include <asm/pgtable.h>
Arun Sharma600634972011-07-26 16:09:06 -070033#include <linux/atomic.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053034#include <asm/proto.h>
35#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/apic.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053037#include <asm/desc.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020038#include <asm/fpu/internal.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053039#include <asm/mtrr.h>
Grzegorz Andrejczuk0274f952017-01-20 14:22:34 +010040#include <asm/hwcap2.h>
Alan Cox8bdbd962009-07-04 00:35:45 +010041#include <linux/numa.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053042#include <asm/asm.h>
Dave Hansen0f6ff2b2016-05-12 15:04:00 -070043#include <asm/bugs.h>
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053044#include <asm/cpu.h>
45#include <asm/mce.h>
46#include <asm/msr.h>
47#include <asm/pat.h>
Fenghua Yud288e1c2012-12-20 23:44:23 -080048#include <asm/microcode.h>
49#include <asm/microcode_intel.h>
Ingo Molnare641f5f2009-02-17 14:02:01 +010050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef CONFIG_X86_LOCAL_APIC
Tejun Heobdbcdd42009-01-21 17:26:06 +090052#include <asm/uv/uv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#endif
54
55#include "cpu.h"
56
Grzegorz Andrejczuk0274f952017-01-20 14:22:34 +010057u32 elf_hwcap2 __read_mostly;
58
Mike Travisc2d1cec2009-01-04 05:18:03 -080059/* all of these masks are initialized in setup_cpu_local_masks() */
Mike Travisc2d1cec2009-01-04 05:18:03 -080060cpumask_var_t cpu_initialized_mask;
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +053061cpumask_var_t cpu_callout_mask;
62cpumask_var_t cpu_callin_mask;
Mike Travisc2d1cec2009-01-04 05:18:03 -080063
64/* representing cpus for which sibling maps can be computed */
65cpumask_var_t cpu_sibling_setup_mask;
66
Brian Gerst2f2f52b2009-01-27 12:56:47 +090067/* correctly size the local cpu masks */
Ingo Molnar4369f1f2009-01-27 12:03:24 +010068void __init setup_cpu_local_masks(void)
Brian Gerst2f2f52b2009-01-27 12:56:47 +090069{
70 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
71 alloc_bootmem_cpumask_var(&cpu_callin_mask);
72 alloc_bootmem_cpumask_var(&cpu_callout_mask);
73 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
74}
75
Paul Gortmaker148f9bb2013-06-18 18:23:59 -040076static void default_init(struct cpuinfo_x86 *c)
Ondrej Zarye8055132009-08-11 20:00:11 +020077{
78#ifdef CONFIG_X86_64
Borislav Petkov27c13ec2009-11-21 14:01:45 +010079 cpu_detect_cache_sizes(c);
Ondrej Zarye8055132009-08-11 20:00:11 +020080#else
81 /* Not much we can do here... */
82 /* Check if at least it has cpuid */
83 if (c->cpuid_level == -1) {
84 /* No cpuid. It must be an ancient CPU */
85 if (c->x86 == 4)
86 strcpy(c->x86_model_id, "486");
87 else if (c->x86 == 3)
88 strcpy(c->x86_model_id, "386");
89 }
90#endif
91}
92
Paul Gortmaker148f9bb2013-06-18 18:23:59 -040093static const struct cpu_dev default_cpu = {
Ondrej Zarye8055132009-08-11 20:00:11 +020094 .c_init = default_init,
95 .c_vendor = "Unknown",
96 .c_x86_vendor = X86_VENDOR_UNKNOWN,
97};
98
Paul Gortmaker148f9bb2013-06-18 18:23:59 -040099static const struct cpu_dev *this_cpu = &default_cpu;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200100
Brian Gerst06deef82009-01-21 17:26:05 +0900101DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
Yinghai Lu950ad7f2008-09-04 20:09:01 -0700102#ifdef CONFIG_X86_64
Brian Gerst06deef82009-01-21 17:26:05 +0900103 /*
104 * We need valid kernel segments for data and code in long mode too
105 * IRET will check the segment types kkeil 2000/10/28
106 * Also sysret mandates a special GDT layout
107 *
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530108 * TLS descriptors are currently at a different place compared to i386.
Brian Gerst06deef82009-01-21 17:26:05 +0900109 * Hopefully nobody expects them at a fixed place (Wine?)
110 */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900111 [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
112 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
113 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
114 [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
115 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
116 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
Yinghai Lu950ad7f2008-09-04 20:09:01 -0700117#else
Akinobu Mita1e5de182009-07-19 00:12:20 +0900118 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
119 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
120 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
121 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200122 /*
123 * Segments used for calling PnP BIOS have byte granularity.
124 * They code segments and data segments have fixed 64k limits,
125 * the transfer segment sizes are set at run time.
126 */
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100127 /* 32-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900128 [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100129 /* 16-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900130 [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100131 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900132 [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100133 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900134 [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100135 /* 16-bit data */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900136 [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
Rusty Russellbf5046722007-05-02 19:27:10 +0200137 /*
138 * The APM segments have byte granularity and their bases
139 * are set at run time. All have 64k limits.
140 */
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100141 /* 32-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900142 [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200143 /* 16-bit code */
Akinobu Mita1e5de182009-07-19 00:12:20 +0900144 [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +0100145 /* data */
Ingo Molnar72c4d852009-08-03 08:47:07 +0200146 [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
Rusty Russellbf5046722007-05-02 19:27:10 +0200147
Akinobu Mita1e5de182009-07-19 00:12:20 +0900148 [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
149 [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
Tejun Heo60a53172009-02-09 22:17:40 +0900150 GDT_STACK_CANARY_INIT
Yinghai Lu950ad7f2008-09-04 20:09:01 -0700151#endif
Brian Gerst06deef82009-01-21 17:26:05 +0900152} };
Jeremy Fitzhardinge7a61d352007-05-02 19:27:15 +0200153EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
Rusty Russellae1ee112007-05-02 19:27:10 +0200154
Dave Hansen8c3641e2015-06-07 11:37:02 -0700155static int __init x86_mpx_setup(char *s)
Suresh Siddha0c752a92009-05-22 12:17:45 -0700156{
Dave Hansen8c3641e2015-06-07 11:37:02 -0700157 /* require an exact match without trailing characters */
Dave Hansen2cd39492014-11-11 14:01:33 -0800158 if (strlen(s))
159 return 0;
Suresh Siddha0c752a92009-05-22 12:17:45 -0700160
Dave Hansen8c3641e2015-06-07 11:37:02 -0700161 /* do not emit a message if the feature is not present */
162 if (!boot_cpu_has(X86_FEATURE_MPX))
163 return 1;
Suresh Siddha6bad06b2010-07-19 16:05:52 -0700164
Dave Hansen8c3641e2015-06-07 11:37:02 -0700165 setup_clear_cpu_cap(X86_FEATURE_MPX);
166 pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
Fenghua Yub6f42a42014-05-29 11:12:31 -0700167 return 1;
168}
Dave Hansen8c3641e2015-06-07 11:37:02 -0700169__setup("nompx", x86_mpx_setup);
Fenghua Yub6f42a42014-05-29 11:12:31 -0700170
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700171#ifdef CONFIG_X86_64
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700172static int __init x86_nopcid_setup(char *s)
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700173{
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700174 /* nopcid doesn't accept parameters */
175 if (s)
176 return -EINVAL;
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700177
178 /* do not emit a message if the feature is not present */
179 if (!boot_cpu_has(X86_FEATURE_PCID))
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700180 return 0;
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700181
182 setup_clear_cpu_cap(X86_FEATURE_PCID);
183 pr_info("nopcid: PCID feature disabled\n");
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700184 return 0;
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700185}
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700186early_param("nopcid", x86_nopcid_setup);
Andy Lutomirski0790c9a2017-06-29 08:53:20 -0700187#endif
188
Andy Lutomirskid12a72b2016-01-29 11:42:58 -0800189static int __init x86_noinvpcid_setup(char *s)
190{
191 /* noinvpcid doesn't accept parameters */
192 if (s)
193 return -EINVAL;
194
195 /* do not emit a message if the feature is not present */
196 if (!boot_cpu_has(X86_FEATURE_INVPCID))
197 return 0;
198
199 setup_clear_cpu_cap(X86_FEATURE_INVPCID);
200 pr_info("noinvpcid: INVPCID feature disabled\n");
201 return 0;
202}
203early_param("noinvpcid", x86_noinvpcid_setup);
204
Yinghai Luba51dce2008-09-04 20:09:02 -0700205#ifdef CONFIG_X86_32
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400206static int cachesize_override = -1;
207static int disable_x86_serial_nr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static int __init cachesize_setup(char *str)
210{
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100211 get_option(&str, &cachesize_override);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 1;
213}
214__setup("cachesize=", cachesize_setup);
215
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100216static int __init x86_sep_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Andi Kleen13530252008-01-30 13:33:20 +0100218 setup_clear_cpu_cap(X86_FEATURE_SEP);
Chuck Ebbert4f886512006-03-23 02:59:34 -0800219 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
Chuck Ebbert4f886512006-03-23 02:59:34 -0800221__setup("nosep", x86_sep_setup);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/* Standard macro to see if a specific flag is changeable */
224static inline int flag_is_changeable_p(u32 flag)
225{
226 u32 f1, f2;
227
Krzysztof Helt94f6bac2008-09-30 23:17:51 +0200228 /*
229 * Cyrix and IDT cpus allow disabling of CPUID
230 * so the code below may return different results
231 * when it is executed before and after enabling
232 * the CPUID. Add "volatile" to not allow gcc to
233 * optimize the subsequent calls to this function.
234 */
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100235 asm volatile ("pushfl \n\t"
236 "pushfl \n\t"
237 "popl %0 \n\t"
238 "movl %0, %1 \n\t"
239 "xorl %2, %0 \n\t"
240 "pushl %0 \n\t"
241 "popfl \n\t"
242 "pushfl \n\t"
243 "popl %0 \n\t"
244 "popfl \n\t"
245
Krzysztof Helt94f6bac2008-09-30 23:17:51 +0200246 : "=&r" (f1), "=&r" (f2)
247 : "ir" (flag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 return ((f1^f2) & flag) != 0;
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/* Probe for the CPUID instruction */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400253int have_cpuid_p(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 return flag_is_changeable_p(X86_EFLAGS_ID);
256}
257
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400258static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
Yinghai Lu0a488a52008-09-04 21:09:47 +0200259{
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100260 unsigned long lo, hi;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200261
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100262 if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
263 return;
264
265 /* Disable processor serial number: */
266
267 rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
268 lo |= 0x200000;
269 wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
270
Chen Yucong1b74dde2016-02-02 11:45:02 +0800271 pr_notice("CPU serial number disabled.\n");
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100272 clear_cpu_cap(c, X86_FEATURE_PN);
273
274 /* Disabling the serial number may affect the cpuid level */
275 c->cpuid_level = cpuid_eax(0);
Yinghai Lu0a488a52008-09-04 21:09:47 +0200276}
277
278static int __init x86_serial_nr_setup(char *s)
279{
280 disable_x86_serial_nr = 0;
281 return 1;
282}
283__setup("serialnumber", x86_serial_nr_setup);
Yinghai Luba51dce2008-09-04 20:09:02 -0700284#else
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700285static inline int flag_is_changeable_p(u32 flag)
286{
287 return 1;
288}
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700289static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
290{
291}
Yinghai Luba51dce2008-09-04 20:09:02 -0700292#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Fenghua Yude5397a2011-05-11 16:51:05 -0700294static __init int setup_disable_smep(char *arg)
295{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700296 setup_clear_cpu_cap(X86_FEATURE_SMEP);
Dave Hansen0f6ff2b2016-05-12 15:04:00 -0700297 /* Check for things that depend on SMEP being enabled: */
298 check_mpx_erratum(&boot_cpu_data);
Fenghua Yude5397a2011-05-11 16:51:05 -0700299 return 1;
300}
301__setup("nosmep", setup_disable_smep);
302
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700303static __always_inline void setup_smep(struct cpuinfo_x86 *c)
Fenghua Yude5397a2011-05-11 16:51:05 -0700304{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700305 if (cpu_has(c, X86_FEATURE_SMEP))
Andy Lutomirski375074c2014-10-24 15:58:07 -0700306 cr4_set_bits(X86_CR4_SMEP);
Fenghua Yude5397a2011-05-11 16:51:05 -0700307}
308
H. Peter Anvin52b61792012-09-21 12:43:13 -0700309static __init int setup_disable_smap(char *arg)
310{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700311 setup_clear_cpu_cap(X86_FEATURE_SMAP);
H. Peter Anvin52b61792012-09-21 12:43:13 -0700312 return 1;
313}
314__setup("nosmap", setup_disable_smap);
315
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700316static __always_inline void setup_smap(struct cpuinfo_x86 *c)
H. Peter Anvin52b61792012-09-21 12:43:13 -0700317{
Andrew Cooper581b7f152015-06-03 10:31:14 +0100318 unsigned long eflags = native_save_fl();
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700319
320 /* This should have been cleared long ago */
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700321 BUG_ON(eflags & X86_EFLAGS_AC);
322
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800323 if (cpu_has(c, X86_FEATURE_SMAP)) {
324#ifdef CONFIG_X86_SMAP
Andy Lutomirski375074c2014-10-24 15:58:07 -0700325 cr4_set_bits(X86_CR4_SMAP);
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800326#else
Andy Lutomirski375074c2014-10-24 15:58:07 -0700327 cr4_clear_bits(X86_CR4_SMAP);
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800328#endif
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/*
Dave Hansen06976942016-02-12 13:02:29 -0800333 * Protection Keys are not available in 32-bit mode.
334 */
335static bool pku_disabled;
336
337static __always_inline void setup_pku(struct cpuinfo_x86 *c)
338{
Dave Hansene8df1a952016-05-13 15:13:28 -0700339 /* check the boot processor, plus compile options for PKU: */
340 if (!cpu_feature_enabled(X86_FEATURE_PKU))
341 return;
342 /* checks the actual processor's cpuid bits: */
Dave Hansen06976942016-02-12 13:02:29 -0800343 if (!cpu_has(c, X86_FEATURE_PKU))
344 return;
345 if (pku_disabled)
346 return;
347
348 cr4_set_bits(X86_CR4_PKE);
349 /*
350 * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
351 * cpuid bit to be set. We need to ensure that we
352 * update that bit in this CPU's "cpu_info".
353 */
354 get_cpu_cap(c);
355}
356
357#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
358static __init int setup_disable_pku(char *arg)
359{
360 /*
361 * Do not clear the X86_FEATURE_PKU bit. All of the
362 * runtime checks are against OSPKE so clearing the
363 * bit does nothing.
364 *
365 * This way, we will see "pku" in cpuinfo, but not
366 * "ospke", which is exactly what we want. It shows
367 * that the CPU has PKU, but the OS has not enabled it.
368 * This happens to be exactly how a system would look
369 * if we disabled the config option.
370 */
371 pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
372 pku_disabled = true;
373 return 1;
374}
375__setup("nopku", setup_disable_pku);
376#endif /* CONFIG_X86_64 */
377
378/*
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800379 * Some CPU features depend on higher CPUID levels, which may not always
380 * be available due to CPUID level capping or broken virtualization
381 * software. Add those features to this table to auto-disable them.
382 */
383struct cpuid_dependent_feature {
384 u32 feature;
385 u32 level;
386};
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100387
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400388static const struct cpuid_dependent_feature
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800389cpuid_dependent_features[] = {
390 { X86_FEATURE_MWAIT, 0x00000005 },
391 { X86_FEATURE_DCA, 0x00000009 },
392 { X86_FEATURE_XSAVE, 0x0000000d },
393 { 0, 0 }
394};
395
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400396static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800397{
398 const struct cpuid_dependent_feature *df;
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530399
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800400 for (df = cpuid_dependent_features; df->feature; df++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100401
402 if (!cpu_has(c, df->feature))
403 continue;
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800404 /*
405 * Note: cpuid_level is set to -1 if unavailable, but
406 * extended_extended_level is set to 0 if unavailable
407 * and the legitimate extended levels are all negative
408 * when signed; hence the weird messing around with
409 * signs here...
410 */
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100411 if (!((s32)df->level < 0 ?
Yinghai Luf6db44d2009-02-14 23:59:18 -0800412 (u32)df->level > (u32)c->extended_cpuid_level :
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100413 (s32)df->level > (s32)c->cpuid_level))
414 continue;
415
416 clear_cpu_cap(c, df->feature);
417 if (!warn)
418 continue;
419
Chen Yucong1b74dde2016-02-02 11:45:02 +0800420 pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
421 x86_cap_flag(df->feature), df->level);
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800422 }
Yinghai Luf6db44d2009-02-14 23:59:18 -0800423}
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800424
425/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * Naming convention should be: <Name> [(<Codename>)]
427 * This table only is used unless init_<vendor>() below doesn't set it;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100428 * in particular, if CPUID levels 0x80000002..4 are supported, this
429 * isn't used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
431
432/* Look up CPU names by table lookup. */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400433static const char *table_lookup_model(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Jan Beulich09dc68d2013-10-21 09:35:20 +0100435#ifdef CONFIG_X86_32
436 const struct legacy_cpu_model_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (c->x86_model >= 16)
439 return NULL; /* Range check */
440
441 if (!this_cpu)
442 return NULL;
443
Jan Beulich09dc68d2013-10-21 09:35:20 +0100444 info = this_cpu->legacy_models;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Jan Beulich09dc68d2013-10-21 09:35:20 +0100446 while (info->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (info->family == c->x86)
448 return info->model_names[c->x86_model];
449 info++;
450 }
Jan Beulich09dc68d2013-10-21 09:35:20 +0100451#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return NULL; /* Not found */
453}
454
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400455__u32 cpu_caps_cleared[NCAPINTS];
456__u32 cpu_caps_set[NCAPINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900458void load_percpu_segment(int cpu)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200459{
Yinghai Lufab334c2008-09-04 20:09:05 -0700460#ifdef CONFIG_X86_32
Brian Gerst2697fbd2009-01-27 12:56:48 +0900461 loadsegment(fs, __KERNEL_PERCPU);
462#else
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700463 __loadsegment_simple(gs, 0);
Brian Gerst2697fbd2009-01-27 12:56:48 +0900464 wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
Yinghai Lufab334c2008-09-04 20:09:05 -0700465#endif
Tejun Heo60a53172009-02-09 22:17:40 +0900466 load_stack_canary_segment();
Yinghai Lu9d31d352008-09-04 21:09:44 +0200467}
468
Andy Lutomirski72f5e082017-12-04 15:07:20 +0100469static void set_percpu_fixmap_pages(int fixmap_index, void *ptr,
470 int pages, pgprot_t prot)
471{
472 int i;
473
474 for (i = 0; i < pages; i++) {
475 __set_fixmap(fixmap_index - i,
476 per_cpu_ptr_to_phys(ptr + i * PAGE_SIZE), prot);
477 }
478}
479
480#ifdef CONFIG_X86_32
481/* The 32-bit entry code needs to find cpu_entry_area. */
482DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
483#endif
484
Andy Lutomirskief8813a2017-12-04 15:07:15 +0100485/* Setup the fixmap mappings only once per-processor */
486static inline void setup_cpu_entry_area(int cpu)
Thomas Garnier69218e42017-03-14 10:05:07 -0700487{
Andy Lutomirskib23adb72017-03-22 14:32:34 -0700488#ifdef CONFIG_X86_64
Andy Lutomirski3386bc82017-12-04 15:07:25 +0100489 extern char _entry_trampoline[];
490
Andy Lutomirskib23adb72017-03-22 14:32:34 -0700491 /* On 64-bit systems, we use a read-only fixmap GDT. */
Andy Lutomirskief8813a2017-12-04 15:07:15 +0100492 pgprot_t gdt_prot = PAGE_KERNEL_RO;
Andy Lutomirskib23adb72017-03-22 14:32:34 -0700493#else
494 /*
495 * On native 32-bit systems, the GDT cannot be read-only because
496 * our double fault handler uses a task gate, and entering through
497 * a task gate needs to change an available TSS to busy. If the GDT
498 * is read-only, that will triple fault.
499 *
500 * On Xen PV, the GDT must be read-only because the hypervisor requires
501 * it.
502 */
Andy Lutomirskief8813a2017-12-04 15:07:15 +0100503 pgprot_t gdt_prot = boot_cpu_has(X86_FEATURE_XENPV) ?
Andy Lutomirskib23adb72017-03-22 14:32:34 -0700504 PAGE_KERNEL_RO : PAGE_KERNEL;
505#endif
506
Andy Lutomirskief8813a2017-12-04 15:07:15 +0100507 __set_fixmap(get_cpu_entry_area_index(cpu, gdt), get_cpu_gdt_paddr(cpu), gdt_prot);
Andy Lutomirski1a935bc2017-12-04 15:07:19 +0100508
509 /*
510 * The Intel SDM says (Volume 3, 7.2.1):
511 *
512 * Avoid placing a page boundary in the part of the TSS that the
513 * processor reads during a task switch (the first 104 bytes). The
514 * processor may not correctly perform address translations if a
515 * boundary occurs in this area. During a task switch, the processor
516 * reads and writes into the first 104 bytes of each TSS (using
517 * contiguous physical addresses beginning with the physical address
518 * of the first byte of the TSS). So, after TSS access begins, if
519 * part of the 104 bytes is not physically contiguous, the processor
520 * will access incorrect information without generating a page-fault
521 * exception.
522 *
523 * There are also a lot of errata involving the TSS spanning a page
524 * boundary. Assert that we're not doing that.
525 */
526 BUILD_BUG_ON((offsetof(struct tss_struct, x86_tss) ^
527 offsetofend(struct tss_struct, x86_tss)) & PAGE_MASK);
Andy Lutomirski72f5e082017-12-04 15:07:20 +0100528 BUILD_BUG_ON(sizeof(struct tss_struct) % PAGE_SIZE != 0);
529 set_percpu_fixmap_pages(get_cpu_entry_area_index(cpu, tss),
530 &per_cpu(cpu_tss, cpu),
531 sizeof(struct tss_struct) / PAGE_SIZE,
532 PAGE_KERNEL);
Andy Lutomirski1a935bc2017-12-04 15:07:19 +0100533
Andy Lutomirski72f5e082017-12-04 15:07:20 +0100534#ifdef CONFIG_X86_32
535 this_cpu_write(cpu_entry_area, get_cpu_entry_area(cpu));
536#endif
Andy Lutomirski3386bc82017-12-04 15:07:25 +0100537
538#ifdef CONFIG_X86_64
539 __set_fixmap(get_cpu_entry_area_index(cpu, entry_trampoline),
540 __pa_symbol(_entry_trampoline), PAGE_KERNEL_RX);
541#endif
Thomas Garnier69218e42017-03-14 10:05:07 -0700542}
543
Thomas Garnier45fc8752017-03-14 10:05:08 -0700544/* Load the original GDT from the per-cpu structure */
545void load_direct_gdt(int cpu)
546{
547 struct desc_ptr gdt_descr;
548
549 gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
550 gdt_descr.size = GDT_SIZE - 1;
551 load_gdt(&gdt_descr);
552}
553EXPORT_SYMBOL_GPL(load_direct_gdt);
554
Thomas Garnier69218e42017-03-14 10:05:07 -0700555/* Load a fixmap remapping of the per-cpu GDT */
556void load_fixmap_gdt(int cpu)
557{
558 struct desc_ptr gdt_descr;
559
560 gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
561 gdt_descr.size = GDT_SIZE - 1;
562 load_gdt(&gdt_descr);
563}
Thomas Garnier45fc8752017-03-14 10:05:08 -0700564EXPORT_SYMBOL_GPL(load_fixmap_gdt);
Thomas Garnier69218e42017-03-14 10:05:07 -0700565
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100566/*
567 * Current gdt points %fs at the "master" per-cpu area: after this,
568 * it's on the real one.
569 */
Brian Gerst552be872009-01-30 17:47:53 +0900570void switch_to_new_gdt(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Thomas Garnier45fc8752017-03-14 10:05:08 -0700572 /* Load the original GDT */
573 load_direct_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* Reload the per-cpu base */
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900575 load_percpu_segment(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400578static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400580static void get_model_name(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 unsigned int *v;
Borislav Petkovee098e12015-06-01 12:06:57 +0200583 char *p, *q, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Yinghai Lu3da99c92008-09-04 21:09:44 +0200585 if (c->extended_cpuid_level < 0x80000004)
Yinghai Lu1b05d602008-09-06 01:52:27 -0700586 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100588 v = (unsigned int *)c->x86_model_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
590 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
591 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
592 c->x86_model_id[48] = 0;
593
Borislav Petkovee098e12015-06-01 12:06:57 +0200594 /* Trim whitespace */
595 p = q = s = &c->x86_model_id[0];
596
597 while (*p == ' ')
598 p++;
599
600 while (*p) {
601 /* Note the last non-whitespace index */
602 if (!isspace(*p))
603 s = q;
604
605 *q++ = *p++;
606 }
607
608 *(s + 1) = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400611void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Yinghai Lu9d31d352008-09-04 21:09:44 +0200613 unsigned int n, dummy, ebx, ecx, edx, l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Yinghai Lu3da99c92008-09-04 21:09:44 +0200615 n = c->extended_cpuid_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (n >= 0x80000005) {
Yinghai Lu9d31d352008-09-04 21:09:44 +0200618 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
Yinghai Lu9d31d352008-09-04 21:09:44 +0200619 c->x86_cache_size = (ecx>>24) + (edx>>24);
Yinghai Lu140fc722008-09-04 20:09:07 -0700620#ifdef CONFIG_X86_64
621 /* On K8 L1 TLB is inclusive, so don't count it */
622 c->x86_tlbsize = 0;
623#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
626 if (n < 0x80000006) /* Some chips just has a large L1. */
627 return;
628
Yinghai Lu0a488a52008-09-04 21:09:47 +0200629 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 l2size = ecx >> 16;
631
Yinghai Lu140fc722008-09-04 20:09:07 -0700632#ifdef CONFIG_X86_64
633 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
634#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* do processor-specific cache resizing */
Jan Beulich09dc68d2013-10-21 09:35:20 +0100636 if (this_cpu->legacy_cache_size)
637 l2size = this_cpu->legacy_cache_size(c, l2size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /* Allow user to override all this if necessary. */
640 if (cachesize_override != -1)
641 l2size = cachesize_override;
642
643 if (l2size == 0)
644 return; /* Again, no L2 cache is possible */
Yinghai Lu140fc722008-09-04 20:09:07 -0700645#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 c->x86_cache_size = l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Alex Shie0ba94f2012-06-28 09:02:16 +0800650u16 __read_mostly tlb_lli_4k[NR_INFO];
651u16 __read_mostly tlb_lli_2m[NR_INFO];
652u16 __read_mostly tlb_lli_4m[NR_INFO];
653u16 __read_mostly tlb_lld_4k[NR_INFO];
654u16 __read_mostly tlb_lld_2m[NR_INFO];
655u16 __read_mostly tlb_lld_4m[NR_INFO];
Kirill A. Shutemovdd360392013-12-23 14:16:58 +0200656u16 __read_mostly tlb_lld_1g[NR_INFO];
Alex Shie0ba94f2012-06-28 09:02:16 +0800657
Steven Honeymanf94fe112014-11-05 22:52:18 +0000658static void cpu_detect_tlb(struct cpuinfo_x86 *c)
Alex Shie0ba94f2012-06-28 09:02:16 +0800659{
660 if (this_cpu->c_detect_tlb)
661 this_cpu->c_detect_tlb(c);
662
Steven Honeymanf94fe112014-11-05 22:52:18 +0000663 pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
Alex Shie0ba94f2012-06-28 09:02:16 +0800664 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
Steven Honeymanf94fe112014-11-05 22:52:18 +0000665 tlb_lli_4m[ENTRIES]);
666
667 pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
668 tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
669 tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
Alex Shie0ba94f2012-06-28 09:02:16 +0800670}
671
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400672void detect_ht(struct cpuinfo_x86 *c)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200673{
Borislav Petkovc8e56d22015-06-04 18:55:25 +0200674#ifdef CONFIG_SMP
Yinghai Lu0a488a52008-09-04 21:09:47 +0200675 u32 eax, ebx, ecx, edx;
676 int index_msb, core_bits;
Mike Travis2eaad1f2009-12-10 17:19:36 -0800677 static bool printed;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200678
679 if (!cpu_has(c, X86_FEATURE_HT))
680 return;
681
682 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
683 goto out;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200684
Yinghai Lu1cd78772008-09-04 20:09:08 -0700685 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
686 return;
687
Yinghai Lu9d31d352008-09-04 21:09:44 +0200688 cpuid(1, &eax, &ebx, &ecx, &edx);
689
Yinghai Lu9d31d352008-09-04 21:09:44 +0200690 smp_num_siblings = (ebx & 0xff0000) >> 16;
691
692 if (smp_num_siblings == 1) {
Chen Yucong1b74dde2016-02-02 11:45:02 +0800693 pr_info_once("CPU0: Hyper-Threading is disabled\n");
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100694 goto out;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200695 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200696
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100697 if (smp_num_siblings <= 1)
698 goto out;
699
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100700 index_msb = get_count_order(smp_num_siblings);
701 c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
702
703 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
704
705 index_msb = get_count_order(smp_num_siblings);
706
707 core_bits = get_count_order(c->x86_max_cores);
708
709 c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
710 ((1 << core_bits) - 1);
711
Yinghai Lu0a488a52008-09-04 21:09:47 +0200712out:
Mike Travis2eaad1f2009-12-10 17:19:36 -0800713 if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
Chen Yucong1b74dde2016-02-02 11:45:02 +0800714 pr_info("CPU: Physical Processor ID: %d\n",
715 c->phys_proc_id);
716 pr_info("CPU: Processor Core ID: %d\n",
717 c->cpu_core_id);
Mike Travis2eaad1f2009-12-10 17:19:36 -0800718 printed = 1;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200719 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200720#endif
Yinghai Lu97e4db72008-09-04 20:08:59 -0700721}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400723static void get_cpu_vendor(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 char *v = c->x86_vendor_id;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100726 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 for (i = 0; i < X86_VENDOR_NUM; i++) {
Yinghai Lu10a434f2008-09-04 21:09:45 +0200729 if (!cpu_devs[i])
730 break;
731
732 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
733 (cpu_devs[i]->c_ident[1] &&
734 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100735
Yinghai Lu10a434f2008-09-04 21:09:45 +0200736 this_cpu = cpu_devs[i];
737 c->x86_vendor = this_cpu->c_x86_vendor;
738 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740 }
Yinghai Lu10a434f2008-09-04 21:09:45 +0200741
Chen Yucong1b74dde2016-02-02 11:45:02 +0800742 pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
743 "CPU: Your system may be unstable.\n", v);
Yinghai Lu10a434f2008-09-04 21:09:45 +0200744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 c->x86_vendor = X86_VENDOR_UNKNOWN;
746 this_cpu = &default_cpu;
747}
748
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400749void cpu_detect(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /* Get vendor name */
Harvey Harrison4a148512008-02-01 17:49:43 +0100752 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
753 (unsigned int *)&c->x86_vendor_id[0],
754 (unsigned int *)&c->x86_vendor_id[8],
755 (unsigned int *)&c->x86_vendor_id[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 c->x86 = 4;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200758 /* Intel-defined flags: level 0x00000001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (c->cpuid_level >= 0x00000001) {
760 u32 junk, tfms, cap0, misc;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
Borislav Petkov99f925c2015-11-23 11:12:21 +0100763 c->x86 = x86_family(tfms);
764 c->x86_model = x86_model(tfms);
765 c->x86_mask = x86_stepping(tfms);
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100766
Huang, Yingd4387bd2008-01-31 22:05:45 +0100767 if (cap0 & (1<<19)) {
Huang, Yingd4387bd2008-01-31 22:05:45 +0100768 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200769 c->x86_cache_alignment = c->x86_clflush_size;
Huang, Yingd4387bd2008-01-31 22:05:45 +0100770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
Yinghai Lu3da99c92008-09-04 21:09:44 +0200773
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -0800774static void apply_forced_caps(struct cpuinfo_x86 *c)
775{
776 int i;
777
778 for (i = 0; i < NCAPINTS; i++) {
779 c->x86_capability[i] &= ~cpu_caps_cleared[i];
780 c->x86_capability[i] |= cpu_caps_set[i];
781 }
782}
783
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400784void get_cpu_cap(struct cpuinfo_x86 *c)
Yinghai Lu093af8d2008-01-30 13:33:32 +0100785{
Borislav Petkov39c06df2015-12-07 10:39:40 +0100786 u32 eax, ebx, ecx, edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100787
Yinghai Lu3da99c92008-09-04 21:09:44 +0200788 /* Intel-defined flags: level 0x00000001 */
789 if (c->cpuid_level >= 0x00000001) {
Borislav Petkov39c06df2015-12-07 10:39:40 +0100790 cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100791
Borislav Petkov39c06df2015-12-07 10:39:40 +0100792 c->x86_capability[CPUID_1_ECX] = ecx;
793 c->x86_capability[CPUID_1_EDX] = edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100794 }
795
Andy Lutomirski3df8d9202016-12-15 10:14:42 -0800796 /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
797 if (c->cpuid_level >= 0x00000006)
798 c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
799
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700800 /* Additional Intel-defined flags: level 0x00000007 */
801 if (c->cpuid_level >= 0x00000007) {
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700802 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100803 c->x86_capability[CPUID_7_0_EBX] = ebx;
Dave Hansendfb4a702016-02-12 13:02:01 -0800804 c->x86_capability[CPUID_7_ECX] = ecx;
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700805 }
806
Fenghua Yu6229ad22014-05-29 11:12:30 -0700807 /* Extended state features: level 0x0000000d */
808 if (c->cpuid_level >= 0x0000000d) {
Fenghua Yu6229ad22014-05-29 11:12:30 -0700809 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
810
Borislav Petkov39c06df2015-12-07 10:39:40 +0100811 c->x86_capability[CPUID_D_1_EAX] = eax;
Fenghua Yu6229ad22014-05-29 11:12:30 -0700812 }
813
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000814 /* Additional Intel-defined flags: level 0x0000000F */
815 if (c->cpuid_level >= 0x0000000F) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000816
817 /* QoS sub-leaf, EAX=0Fh, ECX=0 */
818 cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100819 c->x86_capability[CPUID_F_0_EDX] = edx;
820
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000821 if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
822 /* will be overridden if occupancy monitoring exists */
823 c->x86_cache_max_rmid = ebx;
824
825 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
826 cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100827 c->x86_capability[CPUID_F_1_EDX] = edx;
828
Vikas Shivappa33c3cc72016-03-10 15:32:09 -0800829 if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
830 ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
831 (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000832 c->x86_cache_max_rmid = ecx;
833 c->x86_cache_occ_scale = ebx;
834 }
835 } else {
836 c->x86_cache_max_rmid = -1;
837 c->x86_cache_occ_scale = -1;
838 }
839 }
840
Yinghai Lu3da99c92008-09-04 21:09:44 +0200841 /* AMD-defined flags: level 0x80000001 */
Borislav Petkov39c06df2015-12-07 10:39:40 +0100842 eax = cpuid_eax(0x80000000);
843 c->extended_cpuid_level = eax;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100844
Borislav Petkov39c06df2015-12-07 10:39:40 +0100845 if ((eax & 0xffff0000) == 0x80000000) {
846 if (eax >= 0x80000001) {
847 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
848
849 c->x86_capability[CPUID_8000_0001_ECX] = ecx;
850 c->x86_capability[CPUID_8000_0001_EDX] = edx;
Yinghai Lu3da99c92008-09-04 21:09:44 +0200851 }
852 }
Yinghai Lu5122c892008-09-04 20:09:09 -0700853
Yazen Ghannam71faad42016-05-11 14:58:26 +0200854 if (c->extended_cpuid_level >= 0x80000007) {
855 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
856
857 c->x86_capability[CPUID_8000_0007_EBX] = ebx;
858 c->x86_power = edx;
859 }
860
Yinghai Lu5122c892008-09-04 20:09:09 -0700861 if (c->extended_cpuid_level >= 0x80000008) {
Borislav Petkov39c06df2015-12-07 10:39:40 +0100862 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
Yinghai Lu5122c892008-09-04 20:09:09 -0700863
864 c->x86_virt_bits = (eax >> 8) & 0xff;
865 c->x86_phys_bits = eax & 0xff;
Borislav Petkov39c06df2015-12-07 10:39:40 +0100866 c->x86_capability[CPUID_8000_0008_EBX] = ebx;
Yinghai Lu5122c892008-09-04 20:09:09 -0700867 }
Jan Beulich13c6c532009-03-12 12:37:34 +0000868#ifdef CONFIG_X86_32
869 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
870 c->x86_phys_bits = 36;
Yinghai Lu5122c892008-09-04 20:09:09 -0700871#endif
Yinghai Lue3224232008-09-06 01:52:28 -0700872
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100873 if (c->extended_cpuid_level >= 0x8000000a)
Borislav Petkov39c06df2015-12-07 10:39:40 +0100874 c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100875
Jacob Pan1dedefd2010-05-19 12:01:23 -0700876 init_scattered_cpuid_features(c);
Andy Lutomirski60d34502017-01-18 11:15:39 -0800877
878 /*
879 * Clear/Set all flags overridden by options, after probe.
880 * This needs to happen each time we re-probe, which may happen
881 * several times during CPU initialization.
882 */
883 apply_forced_caps(c);
Yinghai Lu093af8d2008-01-30 13:33:32 +0100884}
Yinghai Luaef93c82008-09-14 02:33:15 -0700885
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400886static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
Yinghai Luaef93c82008-09-14 02:33:15 -0700887{
888#ifdef CONFIG_X86_32
889 int i;
890
891 /*
892 * First of all, decide if this is a 486 or higher
893 * It's a 486 if we can modify the AC flag
894 */
895 if (flag_is_changeable_p(X86_EFLAGS_AC))
896 c->x86 = 4;
897 else
898 c->x86 = 3;
899
900 for (i = 0; i < X86_VENDOR_NUM; i++)
901 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
902 c->x86_vendor_id[0] = 0;
903 cpu_devs[i]->c_identify(c);
904 if (c->x86_vendor_id[0]) {
905 get_cpu_vendor(c);
906 break;
907 }
908 }
909#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100912/*
913 * Do minimum CPU detection early.
914 * Fields really needed: vendor, cpuid_level, family, model, mask,
915 * cache alignment.
916 * The others are not touched to avoid unwanted side effects.
917 *
918 * WARNING: this function is only called on the BP. Don't add code here
919 * that is supposed to run on all CPUs.
920 */
Yinghai Lu3da99c92008-09-04 21:09:44 +0200921static void __init early_identify_cpu(struct cpuinfo_x86 *c)
Rusty Russelld7cd5612006-12-07 02:14:08 +0100922{
Yinghai Lu6627d242008-09-04 20:09:10 -0700923#ifdef CONFIG_X86_64
924 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +0000925 c->x86_phys_bits = 36;
926 c->x86_virt_bits = 48;
Yinghai Lu6627d242008-09-04 20:09:10 -0700927#else
Huang, Yingd4387bd2008-01-31 22:05:45 +0100928 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +0000929 c->x86_phys_bits = 32;
930 c->x86_virt_bits = 32;
Yinghai Lu6627d242008-09-04 20:09:10 -0700931#endif
Yinghai Lu0a488a52008-09-04 21:09:47 +0200932 c->x86_cache_alignment = c->x86_clflush_size;
Rusty Russelld7cd5612006-12-07 02:14:08 +0100933
Yinghai Lu3da99c92008-09-04 21:09:44 +0200934 memset(&c->x86_capability, 0, sizeof c->x86_capability);
Yinghai Lu0a488a52008-09-04 21:09:47 +0200935 c->extended_cpuid_level = 0;
936
Yinghai Luaef93c82008-09-14 02:33:15 -0700937 /* cyrix could have cpuid enabled via c_identify()*/
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700938 if (have_cpuid_p()) {
939 cpu_detect(c);
940 get_cpu_vendor(c);
941 get_cpu_cap(c);
Borislav Petkov78d1b29682017-01-18 11:15:37 -0800942 setup_force_cpu_cap(X86_FEATURE_CPUID);
Rusty Russelld7cd5612006-12-07 02:14:08 +0100943
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700944 if (this_cpu->c_early_init)
945 this_cpu->c_early_init(c);
Krzysztof Helt12cf1052008-09-04 21:09:43 +0200946
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700947 c->cpu_index = 0;
948 filter_cpuid_features(c, false);
Yinghai Lu3da99c92008-09-04 21:09:44 +0200949
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700950 if (this_cpu->c_bsp_init)
951 this_cpu->c_bsp_init(c);
Borislav Petkov78d1b29682017-01-18 11:15:37 -0800952 } else {
953 identify_cpu_without_cpuid(c);
954 setup_clear_cpu_cap(X86_FEATURE_CPUID);
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700955 }
Borislav Petkovc3b83592013-06-09 12:07:30 +0200956
957 setup_force_cpu_cap(X86_FEATURE_ALWAYS);
Ingo Molnardb52ef72015-06-27 10:25:14 +0200958 fpu__init_system(c);
Andy Lutomirskib8b7aba2017-09-17 09:03:50 -0700959
960#ifdef CONFIG_X86_32
961 /*
962 * Regardless of whether PCID is enumerated, the SDM says
963 * that it can't be enabled in 32-bit mode.
964 */
965 setup_clear_cpu_cap(X86_FEATURE_PCID);
966#endif
Rusty Russelld7cd5612006-12-07 02:14:08 +0100967}
968
Yinghai Lu9d31d352008-09-04 21:09:44 +0200969void __init early_cpu_init(void)
970{
Jan Beulich02dde8b2009-03-12 12:08:49 +0000971 const struct cpu_dev *const *cdev;
Yinghai Lu10a434f2008-09-04 21:09:45 +0200972 int count = 0;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200973
Jan Beulichac23f252011-03-04 15:52:35 +0000974#ifdef CONFIG_PROCESSOR_SELECT
Chen Yucong1b74dde2016-02-02 11:45:02 +0800975 pr_info("KERNEL supported cpus:\n");
Ingo Molnar31c997c2009-11-14 10:34:41 +0100976#endif
977
Yinghai Lu10a434f2008-09-04 21:09:45 +0200978 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
Jan Beulich02dde8b2009-03-12 12:08:49 +0000979 const struct cpu_dev *cpudev = *cdev;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200980
Yinghai Lu10a434f2008-09-04 21:09:45 +0200981 if (count >= X86_VENDOR_NUM)
982 break;
983 cpu_devs[count] = cpudev;
984 count++;
985
Jan Beulichac23f252011-03-04 15:52:35 +0000986#ifdef CONFIG_PROCESSOR_SELECT
Ingo Molnar31c997c2009-11-14 10:34:41 +0100987 {
988 unsigned int j;
Yinghai Lu10a434f2008-09-04 21:09:45 +0200989
Ingo Molnar31c997c2009-11-14 10:34:41 +0100990 for (j = 0; j < 2; j++) {
991 if (!cpudev->c_ident[j])
992 continue;
Chen Yucong1b74dde2016-02-02 11:45:02 +0800993 pr_info(" %s %s\n", cpudev->c_vendor,
Ingo Molnar31c997c2009-11-14 10:34:41 +0100994 cpudev->c_ident[j]);
995 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200996 }
Dave Jones03884232009-11-13 15:30:00 -0500997#endif
Ingo Molnar31c997c2009-11-14 10:34:41 +0100998 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200999 early_identify_cpu(&boot_cpu_data);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08001000}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001002/*
Borislav Petkov366d4a42010-10-04 09:31:27 +02001003 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1004 * unfortunately, that's not true in practice because of early VIA
1005 * chips and (more importantly) broken virtualizers that are not easy
1006 * to detect. In the latter case it doesn't even *fail* reliably, so
1007 * probing for it doesn't even work. Disable it completely on 32-bit
H. Peter Anvinba0593b2008-09-16 09:29:40 -07001008 * unless we can find a reliable way to detect all the broken cases.
Borislav Petkov366d4a42010-10-04 09:31:27 +02001009 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001010 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001011static void detect_nopl(struct cpuinfo_x86 *c)
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001012{
Borislav Petkov366d4a42010-10-04 09:31:27 +02001013#ifdef CONFIG_X86_32
H. Peter Anvinb6734c32008-08-18 17:39:32 -07001014 clear_cpu_cap(c, X86_FEATURE_NOPL);
Borislav Petkov366d4a42010-10-04 09:31:27 +02001015#else
1016 set_cpu_cap(c, X86_FEATURE_NOPL);
1017#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001020static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
1021{
1022#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001024 * Empirically, writing zero to a segment selector on AMD does
1025 * not clear the base, whereas writing zero to a segment
1026 * selector on Intel does clear the base. Intel's behavior
1027 * allows slightly faster context switches in the common case
1028 * where GS is unused by the prev and next threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 *
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001030 * Since neither vendor documents this anywhere that I can see,
1031 * detect it directly instead of hardcoding the choice by
1032 * vendor.
1033 *
1034 * I've designated AMD's behavior as the "bug" because it's
1035 * counterintuitive and less friendly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001037
1038 unsigned long old_base, tmp;
1039 rdmsrl(MSR_FS_BASE, old_base);
1040 wrmsrl(MSR_FS_BASE, 1);
1041 loadsegment(fs, 0);
1042 rdmsrl(MSR_FS_BASE, tmp);
1043 if (tmp != 0)
1044 set_cpu_bug(c, X86_BUG_NULL_SEG);
1045 wrmsrl(MSR_FS_BASE, old_base);
Yinghai Lu3da99c92008-09-04 21:09:44 +02001046#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
Yinghai Luaef93c82008-09-14 02:33:15 -07001048
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001049static void generic_identify(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 c->extended_cpuid_level = 0;
1052
Yinghai Luaef93c82008-09-14 02:33:15 -07001053 if (!have_cpuid_p())
1054 identify_cpu_without_cpuid(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001055
Yinghai Luaef93c82008-09-14 02:33:15 -07001056 /* cyrix could have cpuid enabled via c_identify()*/
Ingo Molnara9853dd2008-09-14 14:46:58 +02001057 if (!have_cpuid_p())
Yinghai Luaef93c82008-09-14 02:33:15 -07001058 return;
1059
Yinghai Lu3da99c92008-09-04 21:09:44 +02001060 cpu_detect(c);
1061
1062 get_cpu_vendor(c);
1063
1064 get_cpu_cap(c);
1065
1066 if (c->cpuid_level >= 0x00000001) {
1067 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
Yinghai Lub89d3b32008-09-04 20:09:12 -07001068#ifdef CONFIG_X86_32
Borislav Petkovc8e56d22015-06-04 18:55:25 +02001069# ifdef CONFIG_SMP
Ingo Molnarcb8cc442009-01-28 13:24:54 +01001070 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lub89d3b32008-09-04 20:09:12 -07001071# else
Yinghai Lu3da99c92008-09-04 21:09:44 +02001072 c->apicid = c->initial_apicid;
Yinghai Lub89d3b32008-09-04 20:09:12 -07001073# endif
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08001074#endif
Yinghai Lub89d3b32008-09-04 20:09:12 -07001075 c->phys_proc_id = c->initial_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
Yinghai Lu3da99c92008-09-04 21:09:44 +02001077
Yinghai Lu1b05d602008-09-06 01:52:27 -07001078 get_model_name(c); /* Default name */
Yinghai Lu3da99c92008-09-04 21:09:44 +02001079
Yinghai Lu3da99c92008-09-04 21:09:44 +02001080 detect_nopl(c);
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001081
1082 detect_null_seg_behavior(c);
Andy Lutomirski0230bb02016-04-07 17:31:48 -07001083
1084 /*
1085 * ESPFIX is a strange bug. All real CPUs have it. Paravirt
1086 * systems that run Linux at CPL > 0 may or may not have the
1087 * issue, but, even if they have the issue, there's absolutely
1088 * nothing we can do about it because we can't use the real IRET
1089 * instruction.
1090 *
1091 * NB: For the time being, only 32-bit kernels support
1092 * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
1093 * whether to apply espfix using paravirt hooks. If any
1094 * non-paravirt system ever shows up that does *not* have the
1095 * ESPFIX issue, we can change this.
1096 */
1097#ifdef CONFIG_X86_32
1098# ifdef CONFIG_PARAVIRT
1099 do {
1100 extern void native_iret(void);
1101 if (pv_cpu_ops.iret == native_iret)
1102 set_cpu_bug(c, X86_BUG_ESPFIX);
1103 } while (0);
1104# else
1105 set_cpu_bug(c, X86_BUG_ESPFIX);
1106# endif
1107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001110static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1111{
1112 /*
1113 * The heavy lifting of max_rmid and cache_occ_scale are handled
1114 * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
1115 * in case CQM bits really aren't there in this CPU.
1116 */
1117 if (c != &boot_cpu_data) {
1118 boot_cpu_data.x86_cache_max_rmid =
1119 min(boot_cpu_data.x86_cache_max_rmid,
1120 c->x86_cache_max_rmid);
1121 }
1122}
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124/*
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001125 * Validate that ACPI/mptables have the same information about the
1126 * effective APIC id and update the package map.
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001127 */
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001128static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001129{
1130#ifdef CONFIG_SMP
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001131 unsigned int apicid, cpu = smp_processor_id();
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001132
1133 apicid = apic->cpu_present_to_apicid(cpu);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001134
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001135 if (apicid != c->apicid) {
1136 pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001137 cpu, apicid, c->initial_apicid);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001138 }
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001139 BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001140#else
1141 c->logical_proc_id = 0;
1142#endif
1143}
1144
1145/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 * This does the hard work of actually picking apart the CPU stuff...
1147 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001148static void identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 int i;
1151
1152 c->loops_per_jiffy = loops_per_jiffy;
1153 c->x86_cache_size = -1;
1154 c->x86_vendor = X86_VENDOR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 c->x86_model = c->x86_mask = 0; /* So far unknown... */
1156 c->x86_vendor_id[0] = '\0'; /* Unset */
1157 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001158 c->x86_max_cores = 1;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001159 c->x86_coreid_bits = 0;
Borislav Petkov79a8b9a2017-02-05 11:50:21 +01001160 c->cu_id = 0xff;
Yinghai Lu11fdd252008-09-07 17:58:50 -07001161#ifdef CONFIG_X86_64
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001162 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +00001163 c->x86_phys_bits = 36;
1164 c->x86_virt_bits = 48;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001165#else
1166 c->cpuid_level = -1; /* CPUID not detected */
Andi Kleen770d1322006-12-07 02:14:05 +01001167 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +00001168 c->x86_phys_bits = 32;
1169 c->x86_virt_bits = 32;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001170#endif
1171 c->x86_cache_alignment = c->x86_clflush_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 memset(&c->x86_capability, 0, sizeof c->x86_capability);
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 generic_identify(c);
1175
Andi Kleen38985342008-01-30 13:32:49 +01001176 if (this_cpu->c_identify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 this_cpu->c_identify(c);
1178
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001179 /* Clear/Set all flags overridden by options, after probe */
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -08001180 apply_forced_caps(c);
Yinghai Lu2759c322009-05-15 13:05:16 -07001181
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001182#ifdef CONFIG_X86_64
Ingo Molnarcb8cc442009-01-28 13:24:54 +01001183 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001184#endif
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /*
1187 * Vendor-specific initialization. In this section we
1188 * canonicalize the feature flags, meaning if there are
1189 * features a certain CPU supports which CPUID doesn't
1190 * tell us, CPUID claiming incorrect flags, or other bugs,
1191 * we handle them here.
1192 *
1193 * At the end of this section, c->x86_capability better
1194 * indicate the features this CPU genuinely supports!
1195 */
1196 if (this_cpu->c_init)
1197 this_cpu->c_init(c);
1198
1199 /* Disable the PN if appropriate */
1200 squash_the_stupid_serial_number(c);
1201
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -07001202 /* Set up SMEP/SMAP */
1203 setup_smep(c);
1204 setup_smap(c);
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /*
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001207 * The vendor-specific functions might have changed features.
1208 * Now we do "generic changes."
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 */
1210
H. Peter Anvinb38b0662009-01-23 17:20:50 -08001211 /* Filter out anything that depends on CPUID levels we don't have */
1212 filter_cpuid_features(c, true);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 /* If the model name is still unset, do table lookup. */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001215 if (!c->x86_model_id[0]) {
Jan Beulich02dde8b2009-03-12 12:08:49 +00001216 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 p = table_lookup_model(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001218 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 strcpy(c->x86_model_id, p);
1220 else
1221 /* Last resort... */
1222 sprintf(c->x86_model_id, "%02x/%02x",
Chuck Ebbert54a20f82006-03-23 02:59:36 -08001223 c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001226#ifdef CONFIG_X86_64
1227 detect_ht(c);
1228#endif
1229
H. Peter Anvin49d859d2011-07-31 14:02:19 -07001230 x86_init_rdrand(c);
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001231 x86_init_cache_qos(c);
Dave Hansen06976942016-02-12 13:02:29 -08001232 setup_pku(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001233
1234 /*
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001235 * Clear/Set all flags overridden by options, need do it
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001236 * before following smp all cpus cap AND.
1237 */
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -08001238 apply_forced_caps(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /*
1241 * On SMP, boot_cpu_data holds the common feature set between
1242 * all CPUs; so make sure that we indicate which features are
1243 * common between the CPUs. The first time this routine gets
1244 * executed, c == &boot_cpu_data.
1245 */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001246 if (c != &boot_cpu_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /* AND the already accumulated flags with these */
Yinghai Lu9d31d352008-09-04 21:09:44 +02001248 for (i = 0; i < NCAPINTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
Borislav Petkov65fc9852013-03-20 15:07:23 +01001250
1251 /* OR, i.e. replicate the bug flags */
1252 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
1253 c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
1256 /* Init Machine Check Exception if available. */
Borislav Petkov5e099542009-10-16 12:31:32 +02001257 mcheck_cpu_init(c);
Andi Kleen30d432d2008-01-30 13:33:16 +01001258
1259 select_idle_routine(c);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001260
Tejun Heode2d9442011-01-23 14:37:41 +01001261#ifdef CONFIG_NUMA
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001262 numa_add_cpu(smp_processor_id());
1263#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001264}
Shaohua Li31ab2692005-11-07 00:58:42 -08001265
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001266/*
1267 * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
1268 * on 32-bit kernels:
1269 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001270#ifdef CONFIG_X86_32
1271void enable_sep_cpu(void)
1272{
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001273 struct tss_struct *tss;
1274 int cpu;
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001275
Borislav Petkovb3edfda2016-03-16 13:19:29 +01001276 if (!boot_cpu_has(X86_FEATURE_SEP))
1277 return;
1278
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001279 cpu = get_cpu();
1280 tss = &per_cpu(cpu_tss, cpu);
1281
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001282 /*
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001283 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1284 * see the big comment in struct x86_hw_tss's definition.
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001285 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001286
1287 tss->x86_tss.ss1 = __KERNEL_CS;
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001288 wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
1289
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001290 wrmsr(MSR_IA32_SYSENTER_ESP,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001291 (unsigned long)&get_cpu_entry_area(cpu)->tss +
1292 offsetofend(struct tss_struct, SYSENTER_stack),
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001293 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001294
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001295 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001296
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001297 put_cpu();
1298}
Glauber Costae04d6452008-09-22 14:35:08 -03001299#endif
1300
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001301void __init identify_boot_cpu(void)
1302{
1303 identify_cpu(&boot_cpu_data);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001304#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001305 sysenter_setup();
Li Shaohua6fe940d2005-06-25 14:54:53 -07001306 enable_sep_cpu();
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001307#endif
Borislav Petkov5b5563322012-08-06 19:00:37 +02001308 cpu_detect_tlb(&boot_cpu_data);
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001309}
Shaohua Li3b520b22005-07-07 17:56:38 -07001310
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001311void identify_secondary_cpu(struct cpuinfo_x86 *c)
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001312{
1313 BUG_ON(c == &boot_cpu_data);
1314 identify_cpu(c);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001315#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001316 enable_sep_cpu();
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001317#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001318 mtrr_ap_init();
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001319 validate_apic_and_package_id(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Andi Kleen191679f2008-01-30 13:33:21 +01001322static __init int setup_noclflush(char *arg)
1323{
H. Peter Anvin840d2832014-02-27 08:31:30 -08001324 setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
H. Peter Anvinda4aaa72014-02-27 08:36:31 -08001325 setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
Andi Kleen191679f2008-01-30 13:33:21 +01001326 return 1;
1327}
1328__setup("noclflush", setup_noclflush);
1329
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001330void print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Jan Beulich02dde8b2009-03-12 12:08:49 +00001332 const char *vendor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001334 if (c->x86_vendor < X86_VENDOR_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 vendor = this_cpu->c_vendor;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001336 } else {
1337 if (c->cpuid_level >= 0)
1338 vendor = c->x86_vendor_id;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Yinghai Lubd32a8cf2008-09-19 18:41:16 -07001341 if (vendor && !strstr(c->x86_model_id, vendor))
Chen Yucong1b74dde2016-02-02 11:45:02 +08001342 pr_cont("%s ", vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Yinghai Lu9d31d352008-09-04 21:09:44 +02001344 if (c->x86_model_id[0])
Chen Yucong1b74dde2016-02-02 11:45:02 +08001345 pr_cont("%s", c->x86_model_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001347 pr_cont("%d86", c->x86);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Chen Yucong1b74dde2016-02-02 11:45:02 +08001349 pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
Borislav Petkov924e1012012-09-14 18:37:46 +02001350
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001351 if (c->x86_mask || c->cpuid_level >= 0)
Chen Yucong1b74dde2016-02-02 11:45:02 +08001352 pr_cont(", stepping: 0x%x)\n", c->x86_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001354 pr_cont(")\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Andi Kleen0c2a3912017-10-13 14:56:43 -07001357/*
1358 * clearcpuid= was already parsed in fpu__init_parse_early_param.
1359 * But we need to keep a dummy __setup around otherwise it would
1360 * show up as an environment variable for init.
1361 */
1362static __init int setup_clearcpuid(char *arg)
Andi Kleenac72e782008-01-30 13:33:21 +01001363{
Andi Kleenac72e782008-01-30 13:33:21 +01001364 return 1;
1365}
Andi Kleen0c2a3912017-10-13 14:56:43 -07001366__setup("clearcpuid=", setup_clearcpuid);
Andi Kleenac72e782008-01-30 13:33:21 +01001367
Yinghai Lud5494d42008-09-04 20:09:03 -07001368#ifdef CONFIG_X86_64
Brian Gerst947e76c2009-01-19 12:21:28 +09001369DEFINE_PER_CPU_FIRST(union irq_stack_union,
Andi Kleen277d5b42013-08-05 15:02:43 -07001370 irq_stack_union) __aligned(PAGE_SIZE) __visible;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001371
Tejun Heobdf977b2009-08-03 14:12:19 +09001372/*
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001373 * The following percpu variables are hot. Align current_task to
1374 * cacheline size such that they fall in the same cacheline.
Tejun Heobdf977b2009-08-03 14:12:19 +09001375 */
1376DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1377 &init_task;
1378EXPORT_PER_CPU_SYMBOL(current_task);
Yinghai Lud5494d42008-09-04 20:09:03 -07001379
Tejun Heobdf977b2009-08-03 14:12:19 +09001380DEFINE_PER_CPU(char *, irq_stack_ptr) =
Josh Poimboeuf4950d6d2016-08-18 10:59:08 -05001381 init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE;
Tejun Heobdf977b2009-08-03 14:12:19 +09001382
Andi Kleen277d5b42013-08-05 15:02:43 -07001383DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
Yinghai Lud5494d42008-09-04 20:09:03 -07001384
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001385DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1386EXPORT_PER_CPU_SYMBOL(__preempt_count);
1387
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001388/*
1389 * Special IST stacks which the CPU switches to when it calls
1390 * an IST-marked descriptor entry. Up to 7 stacks (hardware
1391 * limit), all of them are 4K, except the debug stack which
1392 * is 8K.
1393 */
1394static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
1395 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
1396 [DEBUG_STACK - 1] = DEBUG_STKSZ
1397};
1398
Brian Gerst92d65b22009-01-19 00:38:58 +09001399static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
Tejun Heo3e352aa2009-08-03 14:10:11 +09001400 [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
Yinghai Lud5494d42008-09-04 20:09:03 -07001401
Yinghai Lud5494d42008-09-04 20:09:03 -07001402/* May not be marked __init: used by software suspend */
1403void syscall_init(void)
1404{
Andy Lutomirski3386bc82017-12-04 15:07:25 +01001405 extern char _entry_trampoline[];
1406 extern char entry_SYSCALL_64_trampoline[];
1407
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001408 int cpu = smp_processor_id();
Andy Lutomirski3386bc82017-12-04 15:07:25 +01001409 unsigned long SYSCALL64_entry_trampoline =
1410 (unsigned long)get_cpu_entry_area(cpu)->entry_trampoline +
1411 (entry_SYSCALL_64_trampoline - _entry_trampoline);
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001412
Borislav Petkov31ac34c2015-11-23 11:12:25 +01001413 wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
Andy Lutomirski3386bc82017-12-04 15:07:25 +01001414 wrmsrl(MSR_LSTAR, SYSCALL64_entry_trampoline);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001415
1416#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski47edb652015-07-23 12:14:40 -07001417 wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001418 /*
Denys Vlasenko487d1ed2015-03-27 11:59:16 +01001419 * This only works on Intel CPUs.
1420 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1421 * This does not cause SYSENTER to jump to the wrong location, because
1422 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001423 */
1424 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
Andy Lutomirski1a797972017-12-04 15:07:12 +01001425 wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001426 (unsigned long)&get_cpu_entry_area(cpu)->tss +
Andy Lutomirski1a797972017-12-04 15:07:12 +01001427 offsetofend(struct tss_struct, SYSENTER_stack));
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001428 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001429#else
Andy Lutomirski47edb652015-07-23 12:14:40 -07001430 wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
Borislav Petkov6b513112015-04-03 14:25:28 +02001431 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001432 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1433 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
Yinghai Lud5494d42008-09-04 20:09:03 -07001434#endif
1435
1436 /* Flags to clear on syscall */
1437 wrmsrl(MSR_SYSCALL_MASK,
H. Peter Anvin63bcff22012-09-21 12:43:12 -07001438 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
Andy Lutomirski8c7aa692014-10-01 11:49:04 -07001439 X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
Yinghai Lud5494d42008-09-04 20:09:03 -07001440}
1441
Yinghai Lud5494d42008-09-04 20:09:03 -07001442/*
1443 * Copies of the original ist values from the tss are only accessed during
1444 * debugging, no special alignment required.
1445 */
1446DEFINE_PER_CPU(struct orig_ist, orig_ist);
1447
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001448static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
Steven Rostedt42181182011-12-16 11:43:02 -05001449DEFINE_PER_CPU(int, debug_stack_usage);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001450
1451int is_debug_stack(unsigned long addr)
1452{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001453 return __this_cpu_read(debug_stack_usage) ||
1454 (addr <= __this_cpu_read(debug_stack_addr) &&
1455 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001456}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001457NOKPROBE_SYMBOL(is_debug_stack);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001458
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001459DEFINE_PER_CPU(u32, debug_idt_ctr);
Steven Rostedtf8988172012-05-30 11:47:00 -04001460
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001461void debug_stack_set_zero(void)
1462{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001463 this_cpu_inc(debug_idt_ctr);
1464 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001465}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001466NOKPROBE_SYMBOL(debug_stack_set_zero);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001467
1468void debug_stack_reset(void)
1469{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001470 if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
Steven Rostedtf8988172012-05-30 11:47:00 -04001471 return;
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001472 if (this_cpu_dec_return(debug_idt_ctr) == 0)
1473 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001474}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001475NOKPROBE_SYMBOL(debug_stack_reset);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001476
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001477#else /* CONFIG_X86_64 */
Yinghai Lud5494d42008-09-04 20:09:03 -07001478
Tejun Heobdf977b2009-08-03 14:12:19 +09001479DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1480EXPORT_PER_CPU_SYMBOL(current_task);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001481DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1482EXPORT_PER_CPU_SYMBOL(__preempt_count);
Tejun Heobdf977b2009-08-03 14:12:19 +09001483
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001484/*
1485 * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1486 * the top of the kernel stack. Use an extra percpu variable to track the
1487 * top of the kernel stack directly.
1488 */
1489DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1490 (unsigned long)&init_thread_union + THREAD_SIZE;
1491EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1492
Tejun Heo60a53172009-02-09 22:17:40 +09001493#ifdef CONFIG_CC_STACKPROTECTOR
Jeremy Fitzhardinge53f82452009-09-03 14:31:44 -07001494DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
Tejun Heo60a53172009-02-09 22:17:40 +09001495#endif
1496
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001497#endif /* CONFIG_X86_64 */
Jeremy Fitzhardingec5413fb2007-05-02 19:27:16 +02001498
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001499/*
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301500 * Clear all 6 debug registers:
1501 */
1502static void clear_all_debug_regs(void)
1503{
1504 int i;
1505
1506 for (i = 0; i < 8; i++) {
1507 /* Ignore db4, db5 */
1508 if ((i == 4) || (i == 5))
1509 continue;
1510
1511 set_debugreg(0, i);
1512 }
1513}
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +01001514
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001515#ifdef CONFIG_KGDB
1516/*
1517 * Restore debug regs if using kgdbwait and you have a kernel debugger
1518 * connection established.
1519 */
1520static void dbg_restore_debug_regs(void)
1521{
1522 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1523 arch_kgdb_ops.correct_hw_break();
1524}
1525#else /* ! CONFIG_KGDB */
1526#define dbg_restore_debug_regs()
1527#endif /* ! CONFIG_KGDB */
1528
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001529static void wait_for_master_cpu(int cpu)
1530{
1531#ifdef CONFIG_SMP
1532 /*
1533 * wait for ACK from master CPU before continuing
1534 * with AP initialization
1535 */
1536 WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1537 while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1538 cpu_relax();
1539#endif
1540}
1541
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001542/*
1543 * cpu_init() initializes state that is per-CPU. Some data is already
1544 * initialized (naturally) in the bootstrap process, such as the GDT
1545 * and IDT. We reload them nevertheless, this function acts as a
1546 * 'CPU state barrier', nothing should get across.
Yinghai Lu1ba76582008-09-04 20:09:04 -07001547 * A lot of state is already set up in PDA init for 64 bit
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001548 */
Yinghai Lu1ba76582008-09-04 20:09:04 -07001549#ifdef CONFIG_X86_64
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001550
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001551void cpu_init(void)
Yinghai Lu1ba76582008-09-04 20:09:04 -07001552{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001553 struct orig_ist *oist;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001554 struct task_struct *me;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001555 struct tss_struct *t;
1556 unsigned long v;
Andy Lutomirskifb598312016-07-14 13:22:58 -07001557 int cpu = raw_smp_processor_id();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001558 int i;
1559
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001560 wait_for_master_cpu(cpu);
1561
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001562 /*
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001563 * Initialize the CR4 shadow before doing anything that could
1564 * try to read it.
1565 */
1566 cr4_init_shadow();
1567
Borislav Petkov777284b2016-10-25 11:55:11 +02001568 if (cpu)
1569 load_ucode_ap();
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001570
Andy Lutomirski24933b82015-03-05 19:19:05 -08001571 t = &per_cpu(cpu_tss, cpu);
Tejun Heo0fe1e002009-10-29 22:34:14 +09001572 oist = &per_cpu(orig_ist, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001573
Brian Gerste7a22c12009-01-19 00:38:59 +09001574#ifdef CONFIG_NUMA
Fenghua Yu27fd1852012-11-13 11:32:47 -08001575 if (this_cpu_read(numa_node) == 0 &&
Lee Schermerhorne534c7c2010-05-26 14:44:58 -07001576 early_cpu_to_node(cpu) != NUMA_NO_NODE)
1577 set_numa_node(early_cpu_to_node(cpu));
Brian Gerste7a22c12009-01-19 00:38:59 +09001578#endif
Yinghai Lu1ba76582008-09-04 20:09:04 -07001579
1580 me = current;
1581
Mike Travis2eaad1f2009-12-10 17:19:36 -08001582 pr_debug("Initializing CPU#%d\n", cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001583
Andy Lutomirski375074c2014-10-24 15:58:07 -07001584 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001585
1586 /*
1587 * Initialize the per-CPU GDT with the boot GDT,
1588 * and set up the GDT descriptor:
1589 */
1590
Brian Gerst552be872009-01-30 17:47:53 +09001591 switch_to_new_gdt(cpu);
Brian Gerst2697fbd2009-01-27 12:56:48 +09001592 loadsegment(fs, 0);
1593
Seiji Aguchicf910e82013-06-20 11:46:53 -04001594 load_current_idt();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001595
1596 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1597 syscall_init();
1598
1599 wrmsrl(MSR_FS_BASE, 0);
1600 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1601 barrier();
1602
H. Peter Anvin4763ed42009-11-13 15:28:16 -08001603 x86_configure_nx();
Thomas Gleixner659006b2015-01-15 21:22:26 +00001604 x2apic_setup();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001605
1606 /*
1607 * set up and load the per-CPU TSS
1608 */
Tejun Heo0fe1e002009-10-29 22:34:14 +09001609 if (!oist->ist[0]) {
Brian Gerst92d65b22009-01-19 00:38:58 +09001610 char *estacks = per_cpu(exception_stacks, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001611
Yinghai Lu1ba76582008-09-04 20:09:04 -07001612 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001613 estacks += exception_stack_sizes[v];
Tejun Heo0fe1e002009-10-29 22:34:14 +09001614 oist->ist[v] = t->x86_tss.ist[v] =
Yinghai Lu1ba76582008-09-04 20:09:04 -07001615 (unsigned long)estacks;
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001616 if (v == DEBUG_STACK-1)
1617 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001618 }
1619 }
1620
Andy Lutomirski7fb983b2017-12-04 15:07:17 +01001621 t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001622
Yinghai Lu1ba76582008-09-04 20:09:04 -07001623 /*
1624 * <= is required because the CPU will access up to
1625 * 8 bits beyond the end of the IO permission bitmap.
1626 */
1627 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1628 t->io_bitmap[i] = ~0UL;
1629
Vegard Nossumf1f10072017-02-27 14:30:07 -08001630 mmgrab(&init_mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001631 me->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001632 BUG_ON(me->mm);
Andy Lutomirski72c00982017-09-06 19:54:53 -07001633 initialize_tlbstate_and_flush();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001634 enter_lazy_tlb(&init_mm, me);
1635
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001636 setup_cpu_entry_area(cpu);
1637
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001638 /*
Andy Lutomirski7f2590a2017-12-04 15:07:23 +01001639 * Initialize the TSS. sp0 points to the entry trampoline stack
1640 * regardless of what task is running.
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001641 */
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001642 set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001643 load_TR_desc();
Andy Lutomirski7f2590a2017-12-04 15:07:23 +01001644 load_sp0((unsigned long)&get_cpu_entry_area(cpu)->tss +
1645 offsetofend(struct tss_struct, SYSENTER_stack));
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001646
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001647 load_mm_ldt(&init_mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001648
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001649 clear_all_debug_regs();
1650 dbg_restore_debug_regs();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001651
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001652 fpu__init_cpu();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001653
Yinghai Lu1ba76582008-09-04 20:09:04 -07001654 if (is_uv_system())
1655 uv_cpu_init();
Thomas Garnier69218e42017-03-14 10:05:07 -07001656
Thomas Garnier69218e42017-03-14 10:05:07 -07001657 load_fixmap_gdt(cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001658}
1659
1660#else
1661
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001662void cpu_init(void)
James Bottomley9ee79a32007-01-22 09:18:31 -06001663{
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001664 int cpu = smp_processor_id();
1665 struct task_struct *curr = current;
Andy Lutomirski24933b82015-03-05 19:19:05 -08001666 struct tss_struct *t = &per_cpu(cpu_tss, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001668 wait_for_master_cpu(cpu);
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001669
Steven Rostedt5b2bdbc2015-02-27 14:50:19 -05001670 /*
1671 * Initialize the CR4 shadow before doing anything that could
1672 * try to read it.
1673 */
1674 cr4_init_shadow();
1675
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001676 show_ucode_info_early();
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001677
Chen Yucong1b74dde2016-02-02 11:45:02 +08001678 pr_info("Initializing CPU#%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Borislav Petkov362f9242015-12-07 10:39:41 +01001680 if (cpu_feature_enabled(X86_FEATURE_VME) ||
Borislav Petkov59e21e32016-04-04 22:24:59 +02001681 boot_cpu_has(X86_FEATURE_TSC) ||
Borislav Petkov362f9242015-12-07 10:39:41 +01001682 boot_cpu_has(X86_FEATURE_DE))
Andy Lutomirski375074c2014-10-24 15:58:07 -07001683 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Seiji Aguchicf910e82013-06-20 11:46:53 -04001685 load_current_idt();
Brian Gerst552be872009-01-30 17:47:53 +09001686 switch_to_new_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 * Set up and load the per-CPU TSS and LDT
1690 */
Vegard Nossumf1f10072017-02-27 14:30:07 -08001691 mmgrab(&init_mm);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001692 curr->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001693 BUG_ON(curr->mm);
Andy Lutomirski72c00982017-09-06 19:54:53 -07001694 initialize_tlbstate_and_flush();
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001695 enter_lazy_tlb(&init_mm, curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001697 setup_cpu_entry_area(cpu);
1698
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001699 /*
1700 * Initialize the TSS. Don't bother initializing sp0, as the initial
1701 * task never enters user mode.
1702 */
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001703 set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 load_TR_desc();
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001705
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001706 load_mm_ldt(&init_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Andy Lutomirski7fb983b2017-12-04 15:07:17 +01001708 t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
Thomas Gleixnerf9a196b2009-05-01 20:59:25 +02001709
Matt Mackall22c4e302006-01-08 01:05:24 -08001710#ifdef CONFIG_DOUBLEFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /* Set up doublefault TSS pointer in the GDT */
1712 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
Matt Mackall22c4e302006-01-08 01:05:24 -08001713#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301715 clear_all_debug_regs();
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001716 dbg_restore_debug_regs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001718 fpu__init_cpu();
Thomas Garnier69218e42017-03-14 10:05:07 -07001719
Thomas Garnier69218e42017-03-14 10:05:07 -07001720 load_fixmap_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
Yinghai Lu1ba76582008-09-04 20:09:04 -07001722#endif
Borislav Petkov5700f742013-06-09 12:07:32 +02001723
Laura Abbottb51ef522015-07-20 14:47:58 -07001724static void bsp_resume(void)
1725{
1726 if (this_cpu->c_bsp_resume)
1727 this_cpu->c_bsp_resume(&boot_cpu_data);
1728}
1729
1730static struct syscore_ops cpu_syscore_ops = {
1731 .resume = bsp_resume,
1732};
1733
1734static int __init init_cpu_syscore(void)
1735{
1736 register_syscore_ops(&cpu_syscore_ops);
1737 return 0;
1738}
1739core_initcall(init_cpu_syscore);