blob: 39d7ea865207d102d2f03fc9ca779b2bdbb03c61 [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
Ricardo Neriaa35f892017-11-05 18:27:54 -0800332static __always_inline void setup_umip(struct cpuinfo_x86 *c)
333{
334 /* Check the boot processor, plus build option for UMIP. */
335 if (!cpu_feature_enabled(X86_FEATURE_UMIP))
336 goto out;
337
338 /* Check the current processor's cpuid bits. */
339 if (!cpu_has(c, X86_FEATURE_UMIP))
340 goto out;
341
342 cr4_set_bits(X86_CR4_UMIP);
343
Ricardo Neri770c7752017-11-13 22:29:43 -0800344 pr_info("x86/cpu: Activated the Intel User Mode Instruction Prevention (UMIP) CPU feature\n");
345
Ricardo Neriaa35f892017-11-05 18:27:54 -0800346 return;
347
348out:
349 /*
350 * Make sure UMIP is disabled in case it was enabled in a
351 * previous boot (e.g., via kexec).
352 */
353 cr4_clear_bits(X86_CR4_UMIP);
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/*
Dave Hansen06976942016-02-12 13:02:29 -0800357 * Protection Keys are not available in 32-bit mode.
358 */
359static bool pku_disabled;
360
361static __always_inline void setup_pku(struct cpuinfo_x86 *c)
362{
Dave Hansene8df1a952016-05-13 15:13:28 -0700363 /* check the boot processor, plus compile options for PKU: */
364 if (!cpu_feature_enabled(X86_FEATURE_PKU))
365 return;
366 /* checks the actual processor's cpuid bits: */
Dave Hansen06976942016-02-12 13:02:29 -0800367 if (!cpu_has(c, X86_FEATURE_PKU))
368 return;
369 if (pku_disabled)
370 return;
371
372 cr4_set_bits(X86_CR4_PKE);
373 /*
374 * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
375 * cpuid bit to be set. We need to ensure that we
376 * update that bit in this CPU's "cpu_info".
377 */
378 get_cpu_cap(c);
379}
380
381#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
382static __init int setup_disable_pku(char *arg)
383{
384 /*
385 * Do not clear the X86_FEATURE_PKU bit. All of the
386 * runtime checks are against OSPKE so clearing the
387 * bit does nothing.
388 *
389 * This way, we will see "pku" in cpuinfo, but not
390 * "ospke", which is exactly what we want. It shows
391 * that the CPU has PKU, but the OS has not enabled it.
392 * This happens to be exactly how a system would look
393 * if we disabled the config option.
394 */
395 pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
396 pku_disabled = true;
397 return 1;
398}
399__setup("nopku", setup_disable_pku);
400#endif /* CONFIG_X86_64 */
401
402/*
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800403 * Some CPU features depend on higher CPUID levels, which may not always
404 * be available due to CPUID level capping or broken virtualization
405 * software. Add those features to this table to auto-disable them.
406 */
407struct cpuid_dependent_feature {
408 u32 feature;
409 u32 level;
410};
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100411
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400412static const struct cpuid_dependent_feature
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800413cpuid_dependent_features[] = {
414 { X86_FEATURE_MWAIT, 0x00000005 },
415 { X86_FEATURE_DCA, 0x00000009 },
416 { X86_FEATURE_XSAVE, 0x0000000d },
417 { 0, 0 }
418};
419
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400420static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800421{
422 const struct cpuid_dependent_feature *df;
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530423
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800424 for (df = cpuid_dependent_features; df->feature; df++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100425
426 if (!cpu_has(c, df->feature))
427 continue;
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800428 /*
429 * Note: cpuid_level is set to -1 if unavailable, but
430 * extended_extended_level is set to 0 if unavailable
431 * and the legitimate extended levels are all negative
432 * when signed; hence the weird messing around with
433 * signs here...
434 */
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100435 if (!((s32)df->level < 0 ?
Yinghai Luf6db44d2009-02-14 23:59:18 -0800436 (u32)df->level > (u32)c->extended_cpuid_level :
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100437 (s32)df->level > (s32)c->cpuid_level))
438 continue;
439
440 clear_cpu_cap(c, df->feature);
441 if (!warn)
442 continue;
443
Chen Yucong1b74dde2016-02-02 11:45:02 +0800444 pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
445 x86_cap_flag(df->feature), df->level);
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800446 }
Yinghai Luf6db44d2009-02-14 23:59:18 -0800447}
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800448
449/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * Naming convention should be: <Name> [(<Codename>)]
451 * This table only is used unless init_<vendor>() below doesn't set it;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100452 * in particular, if CPUID levels 0x80000002..4 are supported, this
453 * isn't used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 */
455
456/* Look up CPU names by table lookup. */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400457static const char *table_lookup_model(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Jan Beulich09dc68d2013-10-21 09:35:20 +0100459#ifdef CONFIG_X86_32
460 const struct legacy_cpu_model_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (c->x86_model >= 16)
463 return NULL; /* Range check */
464
465 if (!this_cpu)
466 return NULL;
467
Jan Beulich09dc68d2013-10-21 09:35:20 +0100468 info = this_cpu->legacy_models;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Jan Beulich09dc68d2013-10-21 09:35:20 +0100470 while (info->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (info->family == c->x86)
472 return info->model_names[c->x86_model];
473 info++;
474 }
Jan Beulich09dc68d2013-10-21 09:35:20 +0100475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return NULL; /* Not found */
477}
478
Thomas Gleixner6cbd2172017-12-04 15:07:32 +0100479__u32 cpu_caps_cleared[NCAPINTS + NBUGINTS];
480__u32 cpu_caps_set[NCAPINTS + NBUGINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900482void load_percpu_segment(int cpu)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200483{
Yinghai Lufab334c2008-09-04 20:09:05 -0700484#ifdef CONFIG_X86_32
Brian Gerst2697fbd2009-01-27 12:56:48 +0900485 loadsegment(fs, __KERNEL_PERCPU);
486#else
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700487 __loadsegment_simple(gs, 0);
Brian Gerst2697fbd2009-01-27 12:56:48 +0900488 wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
Yinghai Lufab334c2008-09-04 20:09:05 -0700489#endif
Tejun Heo60a53172009-02-09 22:17:40 +0900490 load_stack_canary_segment();
Yinghai Lu9d31d352008-09-04 21:09:44 +0200491}
492
Andy Lutomirski72f5e082017-12-04 15:07:20 +0100493#ifdef CONFIG_X86_32
494/* The 32-bit entry code needs to find cpu_entry_area. */
495DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
496#endif
497
Andy Lutomirski40e7f942017-12-04 15:07:26 +0100498#ifdef CONFIG_X86_64
499/*
500 * Special IST stacks which the CPU switches to when it calls
501 * an IST-marked descriptor entry. Up to 7 stacks (hardware
502 * limit), all of them are 4K, except the debug stack which
503 * is 8K.
504 */
505static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
506 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
507 [DEBUG_STACK - 1] = DEBUG_STKSZ
508};
Andy Lutomirski40e7f942017-12-04 15:07:26 +0100509#endif
510
Thomas Garnier45fc8752017-03-14 10:05:08 -0700511/* Load the original GDT from the per-cpu structure */
512void load_direct_gdt(int cpu)
513{
514 struct desc_ptr gdt_descr;
515
516 gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
517 gdt_descr.size = GDT_SIZE - 1;
518 load_gdt(&gdt_descr);
519}
520EXPORT_SYMBOL_GPL(load_direct_gdt);
521
Thomas Garnier69218e42017-03-14 10:05:07 -0700522/* Load a fixmap remapping of the per-cpu GDT */
523void load_fixmap_gdt(int cpu)
524{
525 struct desc_ptr gdt_descr;
526
527 gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
528 gdt_descr.size = GDT_SIZE - 1;
529 load_gdt(&gdt_descr);
530}
Thomas Garnier45fc8752017-03-14 10:05:08 -0700531EXPORT_SYMBOL_GPL(load_fixmap_gdt);
Thomas Garnier69218e42017-03-14 10:05:07 -0700532
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100533/*
534 * Current gdt points %fs at the "master" per-cpu area: after this,
535 * it's on the real one.
536 */
Brian Gerst552be872009-01-30 17:47:53 +0900537void switch_to_new_gdt(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Thomas Garnier45fc8752017-03-14 10:05:08 -0700539 /* Load the original GDT */
540 load_direct_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* Reload the per-cpu base */
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900542 load_percpu_segment(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400545static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400547static void get_model_name(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 unsigned int *v;
Borislav Petkovee098e12015-06-01 12:06:57 +0200550 char *p, *q, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Yinghai Lu3da99c92008-09-04 21:09:44 +0200552 if (c->extended_cpuid_level < 0x80000004)
Yinghai Lu1b05d602008-09-06 01:52:27 -0700553 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100555 v = (unsigned int *)c->x86_model_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
557 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
558 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
559 c->x86_model_id[48] = 0;
560
Borislav Petkovee098e12015-06-01 12:06:57 +0200561 /* Trim whitespace */
562 p = q = s = &c->x86_model_id[0];
563
564 while (*p == ' ')
565 p++;
566
567 while (*p) {
568 /* Note the last non-whitespace index */
569 if (!isspace(*p))
570 s = q;
571
572 *q++ = *p++;
573 }
574
575 *(s + 1) = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400578void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Yinghai Lu9d31d352008-09-04 21:09:44 +0200580 unsigned int n, dummy, ebx, ecx, edx, l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Yinghai Lu3da99c92008-09-04 21:09:44 +0200582 n = c->extended_cpuid_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (n >= 0x80000005) {
Yinghai Lu9d31d352008-09-04 21:09:44 +0200585 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
Yinghai Lu9d31d352008-09-04 21:09:44 +0200586 c->x86_cache_size = (ecx>>24) + (edx>>24);
Yinghai Lu140fc722008-09-04 20:09:07 -0700587#ifdef CONFIG_X86_64
588 /* On K8 L1 TLB is inclusive, so don't count it */
589 c->x86_tlbsize = 0;
590#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
593 if (n < 0x80000006) /* Some chips just has a large L1. */
594 return;
595
Yinghai Lu0a488a52008-09-04 21:09:47 +0200596 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 l2size = ecx >> 16;
598
Yinghai Lu140fc722008-09-04 20:09:07 -0700599#ifdef CONFIG_X86_64
600 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
601#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* do processor-specific cache resizing */
Jan Beulich09dc68d2013-10-21 09:35:20 +0100603 if (this_cpu->legacy_cache_size)
604 l2size = this_cpu->legacy_cache_size(c, l2size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* Allow user to override all this if necessary. */
607 if (cachesize_override != -1)
608 l2size = cachesize_override;
609
610 if (l2size == 0)
611 return; /* Again, no L2 cache is possible */
Yinghai Lu140fc722008-09-04 20:09:07 -0700612#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 c->x86_cache_size = l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Alex Shie0ba94f2012-06-28 09:02:16 +0800617u16 __read_mostly tlb_lli_4k[NR_INFO];
618u16 __read_mostly tlb_lli_2m[NR_INFO];
619u16 __read_mostly tlb_lli_4m[NR_INFO];
620u16 __read_mostly tlb_lld_4k[NR_INFO];
621u16 __read_mostly tlb_lld_2m[NR_INFO];
622u16 __read_mostly tlb_lld_4m[NR_INFO];
Kirill A. Shutemovdd360392013-12-23 14:16:58 +0200623u16 __read_mostly tlb_lld_1g[NR_INFO];
Alex Shie0ba94f2012-06-28 09:02:16 +0800624
Steven Honeymanf94fe112014-11-05 22:52:18 +0000625static void cpu_detect_tlb(struct cpuinfo_x86 *c)
Alex Shie0ba94f2012-06-28 09:02:16 +0800626{
627 if (this_cpu->c_detect_tlb)
628 this_cpu->c_detect_tlb(c);
629
Steven Honeymanf94fe112014-11-05 22:52:18 +0000630 pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
Alex Shie0ba94f2012-06-28 09:02:16 +0800631 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
Steven Honeymanf94fe112014-11-05 22:52:18 +0000632 tlb_lli_4m[ENTRIES]);
633
634 pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
635 tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
636 tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
Alex Shie0ba94f2012-06-28 09:02:16 +0800637}
638
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400639void detect_ht(struct cpuinfo_x86 *c)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200640{
Borislav Petkovc8e56d22015-06-04 18:55:25 +0200641#ifdef CONFIG_SMP
Yinghai Lu0a488a52008-09-04 21:09:47 +0200642 u32 eax, ebx, ecx, edx;
643 int index_msb, core_bits;
Mike Travis2eaad1f2009-12-10 17:19:36 -0800644 static bool printed;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200645
646 if (!cpu_has(c, X86_FEATURE_HT))
647 return;
648
649 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
650 goto out;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200651
Yinghai Lu1cd78772008-09-04 20:09:08 -0700652 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
653 return;
654
Yinghai Lu9d31d352008-09-04 21:09:44 +0200655 cpuid(1, &eax, &ebx, &ecx, &edx);
656
Yinghai Lu9d31d352008-09-04 21:09:44 +0200657 smp_num_siblings = (ebx & 0xff0000) >> 16;
658
659 if (smp_num_siblings == 1) {
Chen Yucong1b74dde2016-02-02 11:45:02 +0800660 pr_info_once("CPU0: Hyper-Threading is disabled\n");
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100661 goto out;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200662 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200663
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100664 if (smp_num_siblings <= 1)
665 goto out;
666
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100667 index_msb = get_count_order(smp_num_siblings);
668 c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
669
670 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
671
672 index_msb = get_count_order(smp_num_siblings);
673
674 core_bits = get_count_order(c->x86_max_cores);
675
676 c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
677 ((1 << core_bits) - 1);
678
Yinghai Lu0a488a52008-09-04 21:09:47 +0200679out:
Mike Travis2eaad1f2009-12-10 17:19:36 -0800680 if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
Chen Yucong1b74dde2016-02-02 11:45:02 +0800681 pr_info("CPU: Physical Processor ID: %d\n",
682 c->phys_proc_id);
683 pr_info("CPU: Processor Core ID: %d\n",
684 c->cpu_core_id);
Mike Travis2eaad1f2009-12-10 17:19:36 -0800685 printed = 1;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200686 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200687#endif
Yinghai Lu97e4db72008-09-04 20:08:59 -0700688}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400690static void get_cpu_vendor(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 char *v = c->x86_vendor_id;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100693 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 for (i = 0; i < X86_VENDOR_NUM; i++) {
Yinghai Lu10a434f2008-09-04 21:09:45 +0200696 if (!cpu_devs[i])
697 break;
698
699 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
700 (cpu_devs[i]->c_ident[1] &&
701 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100702
Yinghai Lu10a434f2008-09-04 21:09:45 +0200703 this_cpu = cpu_devs[i];
704 c->x86_vendor = this_cpu->c_x86_vendor;
705 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 }
Yinghai Lu10a434f2008-09-04 21:09:45 +0200708
Chen Yucong1b74dde2016-02-02 11:45:02 +0800709 pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
710 "CPU: Your system may be unstable.\n", v);
Yinghai Lu10a434f2008-09-04 21:09:45 +0200711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 c->x86_vendor = X86_VENDOR_UNKNOWN;
713 this_cpu = &default_cpu;
714}
715
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400716void cpu_detect(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /* Get vendor name */
Harvey Harrison4a148512008-02-01 17:49:43 +0100719 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
720 (unsigned int *)&c->x86_vendor_id[0],
721 (unsigned int *)&c->x86_vendor_id[8],
722 (unsigned int *)&c->x86_vendor_id[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 c->x86 = 4;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200725 /* Intel-defined flags: level 0x00000001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (c->cpuid_level >= 0x00000001) {
727 u32 junk, tfms, cap0, misc;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
Borislav Petkov99f925c2015-11-23 11:12:21 +0100730 c->x86 = x86_family(tfms);
731 c->x86_model = x86_model(tfms);
732 c->x86_mask = x86_stepping(tfms);
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100733
Huang, Yingd4387bd2008-01-31 22:05:45 +0100734 if (cap0 & (1<<19)) {
Huang, Yingd4387bd2008-01-31 22:05:45 +0100735 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200736 c->x86_cache_alignment = c->x86_clflush_size;
Huang, Yingd4387bd2008-01-31 22:05:45 +0100737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
Yinghai Lu3da99c92008-09-04 21:09:44 +0200740
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -0800741static void apply_forced_caps(struct cpuinfo_x86 *c)
742{
743 int i;
744
Thomas Gleixner6cbd2172017-12-04 15:07:32 +0100745 for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -0800746 c->x86_capability[i] &= ~cpu_caps_cleared[i];
747 c->x86_capability[i] |= cpu_caps_set[i];
748 }
749}
750
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400751void get_cpu_cap(struct cpuinfo_x86 *c)
Yinghai Lu093af8d2008-01-30 13:33:32 +0100752{
Borislav Petkov39c06df2015-12-07 10:39:40 +0100753 u32 eax, ebx, ecx, edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100754
Yinghai Lu3da99c92008-09-04 21:09:44 +0200755 /* Intel-defined flags: level 0x00000001 */
756 if (c->cpuid_level >= 0x00000001) {
Borislav Petkov39c06df2015-12-07 10:39:40 +0100757 cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100758
Borislav Petkov39c06df2015-12-07 10:39:40 +0100759 c->x86_capability[CPUID_1_ECX] = ecx;
760 c->x86_capability[CPUID_1_EDX] = edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100761 }
762
Andy Lutomirski3df8d9202016-12-15 10:14:42 -0800763 /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
764 if (c->cpuid_level >= 0x00000006)
765 c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
766
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700767 /* Additional Intel-defined flags: level 0x00000007 */
768 if (c->cpuid_level >= 0x00000007) {
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700769 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100770 c->x86_capability[CPUID_7_0_EBX] = ebx;
Dave Hansendfb4a702016-02-12 13:02:01 -0800771 c->x86_capability[CPUID_7_ECX] = ecx;
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700772 }
773
Fenghua Yu6229ad22014-05-29 11:12:30 -0700774 /* Extended state features: level 0x0000000d */
775 if (c->cpuid_level >= 0x0000000d) {
Fenghua Yu6229ad22014-05-29 11:12:30 -0700776 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
777
Borislav Petkov39c06df2015-12-07 10:39:40 +0100778 c->x86_capability[CPUID_D_1_EAX] = eax;
Fenghua Yu6229ad22014-05-29 11:12:30 -0700779 }
780
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000781 /* Additional Intel-defined flags: level 0x0000000F */
782 if (c->cpuid_level >= 0x0000000F) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000783
784 /* QoS sub-leaf, EAX=0Fh, ECX=0 */
785 cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100786 c->x86_capability[CPUID_F_0_EDX] = edx;
787
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000788 if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
789 /* will be overridden if occupancy monitoring exists */
790 c->x86_cache_max_rmid = ebx;
791
792 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
793 cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100794 c->x86_capability[CPUID_F_1_EDX] = edx;
795
Vikas Shivappa33c3cc72016-03-10 15:32:09 -0800796 if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
797 ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
798 (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000799 c->x86_cache_max_rmid = ecx;
800 c->x86_cache_occ_scale = ebx;
801 }
802 } else {
803 c->x86_cache_max_rmid = -1;
804 c->x86_cache_occ_scale = -1;
805 }
806 }
807
Yinghai Lu3da99c92008-09-04 21:09:44 +0200808 /* AMD-defined flags: level 0x80000001 */
Borislav Petkov39c06df2015-12-07 10:39:40 +0100809 eax = cpuid_eax(0x80000000);
810 c->extended_cpuid_level = eax;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100811
Borislav Petkov39c06df2015-12-07 10:39:40 +0100812 if ((eax & 0xffff0000) == 0x80000000) {
813 if (eax >= 0x80000001) {
814 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
815
816 c->x86_capability[CPUID_8000_0001_ECX] = ecx;
817 c->x86_capability[CPUID_8000_0001_EDX] = edx;
Yinghai Lu3da99c92008-09-04 21:09:44 +0200818 }
819 }
Yinghai Lu5122c892008-09-04 20:09:09 -0700820
Yazen Ghannam71faad42016-05-11 14:58:26 +0200821 if (c->extended_cpuid_level >= 0x80000007) {
822 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
823
824 c->x86_capability[CPUID_8000_0007_EBX] = ebx;
825 c->x86_power = edx;
826 }
827
Yinghai Lu5122c892008-09-04 20:09:09 -0700828 if (c->extended_cpuid_level >= 0x80000008) {
Borislav Petkov39c06df2015-12-07 10:39:40 +0100829 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
Yinghai Lu5122c892008-09-04 20:09:09 -0700830
831 c->x86_virt_bits = (eax >> 8) & 0xff;
832 c->x86_phys_bits = eax & 0xff;
Borislav Petkov39c06df2015-12-07 10:39:40 +0100833 c->x86_capability[CPUID_8000_0008_EBX] = ebx;
Yinghai Lu5122c892008-09-04 20:09:09 -0700834 }
Jan Beulich13c6c532009-03-12 12:37:34 +0000835#ifdef CONFIG_X86_32
836 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
837 c->x86_phys_bits = 36;
Yinghai Lu5122c892008-09-04 20:09:09 -0700838#endif
Yinghai Lue3224232008-09-06 01:52:28 -0700839
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100840 if (c->extended_cpuid_level >= 0x8000000a)
Borislav Petkov39c06df2015-12-07 10:39:40 +0100841 c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100842
Jacob Pan1dedefd2010-05-19 12:01:23 -0700843 init_scattered_cpuid_features(c);
Andy Lutomirski60d34502017-01-18 11:15:39 -0800844
845 /*
846 * Clear/Set all flags overridden by options, after probe.
847 * This needs to happen each time we re-probe, which may happen
848 * several times during CPU initialization.
849 */
850 apply_forced_caps(c);
Yinghai Lu093af8d2008-01-30 13:33:32 +0100851}
Yinghai Luaef93c82008-09-14 02:33:15 -0700852
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400853static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
Yinghai Luaef93c82008-09-14 02:33:15 -0700854{
855#ifdef CONFIG_X86_32
856 int i;
857
858 /*
859 * First of all, decide if this is a 486 or higher
860 * It's a 486 if we can modify the AC flag
861 */
862 if (flag_is_changeable_p(X86_EFLAGS_AC))
863 c->x86 = 4;
864 else
865 c->x86 = 3;
866
867 for (i = 0; i < X86_VENDOR_NUM; i++)
868 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
869 c->x86_vendor_id[0] = 0;
870 cpu_devs[i]->c_identify(c);
871 if (c->x86_vendor_id[0]) {
872 get_cpu_vendor(c);
873 break;
874 }
875 }
876#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100879/*
880 * Do minimum CPU detection early.
881 * Fields really needed: vendor, cpuid_level, family, model, mask,
882 * cache alignment.
883 * The others are not touched to avoid unwanted side effects.
884 *
Jean Delvarea1652bb2017-10-03 11:47:27 +0200885 * WARNING: this function is only called on the boot CPU. Don't add code
886 * here that is supposed to run on all CPUs.
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100887 */
Yinghai Lu3da99c92008-09-04 21:09:44 +0200888static void __init early_identify_cpu(struct cpuinfo_x86 *c)
Rusty Russelld7cd5612006-12-07 02:14:08 +0100889{
Yinghai Lu6627d242008-09-04 20:09:10 -0700890#ifdef CONFIG_X86_64
891 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +0000892 c->x86_phys_bits = 36;
893 c->x86_virt_bits = 48;
Yinghai Lu6627d242008-09-04 20:09:10 -0700894#else
Huang, Yingd4387bd2008-01-31 22:05:45 +0100895 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +0000896 c->x86_phys_bits = 32;
897 c->x86_virt_bits = 32;
Yinghai Lu6627d242008-09-04 20:09:10 -0700898#endif
Yinghai Lu0a488a52008-09-04 21:09:47 +0200899 c->x86_cache_alignment = c->x86_clflush_size;
Rusty Russelld7cd5612006-12-07 02:14:08 +0100900
Yinghai Lu3da99c92008-09-04 21:09:44 +0200901 memset(&c->x86_capability, 0, sizeof c->x86_capability);
Yinghai Lu0a488a52008-09-04 21:09:47 +0200902 c->extended_cpuid_level = 0;
903
Yinghai Luaef93c82008-09-14 02:33:15 -0700904 /* cyrix could have cpuid enabled via c_identify()*/
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700905 if (have_cpuid_p()) {
906 cpu_detect(c);
907 get_cpu_vendor(c);
908 get_cpu_cap(c);
Borislav Petkov78d1b29682017-01-18 11:15:37 -0800909 setup_force_cpu_cap(X86_FEATURE_CPUID);
Rusty Russelld7cd5612006-12-07 02:14:08 +0100910
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700911 if (this_cpu->c_early_init)
912 this_cpu->c_early_init(c);
Krzysztof Helt12cf1052008-09-04 21:09:43 +0200913
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700914 c->cpu_index = 0;
915 filter_cpuid_features(c, false);
Yinghai Lu3da99c92008-09-04 21:09:44 +0200916
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700917 if (this_cpu->c_bsp_init)
918 this_cpu->c_bsp_init(c);
Borislav Petkov78d1b29682017-01-18 11:15:37 -0800919 } else {
920 identify_cpu_without_cpuid(c);
921 setup_clear_cpu_cap(X86_FEATURE_CPUID);
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700922 }
Borislav Petkovc3b83592013-06-09 12:07:30 +0200923
924 setup_force_cpu_cap(X86_FEATURE_ALWAYS);
Thomas Gleixnera89f0402017-12-04 15:07:33 +0100925
Tom Lendacky694d99d2017-12-26 23:43:54 -0600926 if (c->x86_vendor != X86_VENDOR_AMD)
Thomas Gleixnerde791822018-01-05 15:27:34 +0100927 setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
Thomas Gleixnera89f0402017-12-04 15:07:33 +0100928
Ingo Molnardb52ef72015-06-27 10:25:14 +0200929 fpu__init_system(c);
Andy Lutomirskib8b7aba2017-09-17 09:03:50 -0700930
931#ifdef CONFIG_X86_32
932 /*
933 * Regardless of whether PCID is enumerated, the SDM says
934 * that it can't be enabled in 32-bit mode.
935 */
936 setup_clear_cpu_cap(X86_FEATURE_PCID);
937#endif
Rusty Russelld7cd5612006-12-07 02:14:08 +0100938}
939
Yinghai Lu9d31d352008-09-04 21:09:44 +0200940void __init early_cpu_init(void)
941{
Jan Beulich02dde8b2009-03-12 12:08:49 +0000942 const struct cpu_dev *const *cdev;
Yinghai Lu10a434f2008-09-04 21:09:45 +0200943 int count = 0;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200944
Jan Beulichac23f252011-03-04 15:52:35 +0000945#ifdef CONFIG_PROCESSOR_SELECT
Chen Yucong1b74dde2016-02-02 11:45:02 +0800946 pr_info("KERNEL supported cpus:\n");
Ingo Molnar31c997c2009-11-14 10:34:41 +0100947#endif
948
Yinghai Lu10a434f2008-09-04 21:09:45 +0200949 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
Jan Beulich02dde8b2009-03-12 12:08:49 +0000950 const struct cpu_dev *cpudev = *cdev;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200951
Yinghai Lu10a434f2008-09-04 21:09:45 +0200952 if (count >= X86_VENDOR_NUM)
953 break;
954 cpu_devs[count] = cpudev;
955 count++;
956
Jan Beulichac23f252011-03-04 15:52:35 +0000957#ifdef CONFIG_PROCESSOR_SELECT
Ingo Molnar31c997c2009-11-14 10:34:41 +0100958 {
959 unsigned int j;
Yinghai Lu10a434f2008-09-04 21:09:45 +0200960
Ingo Molnar31c997c2009-11-14 10:34:41 +0100961 for (j = 0; j < 2; j++) {
962 if (!cpudev->c_ident[j])
963 continue;
Chen Yucong1b74dde2016-02-02 11:45:02 +0800964 pr_info(" %s %s\n", cpudev->c_vendor,
Ingo Molnar31c997c2009-11-14 10:34:41 +0100965 cpudev->c_ident[j]);
966 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200967 }
Dave Jones03884232009-11-13 15:30:00 -0500968#endif
Ingo Molnar31c997c2009-11-14 10:34:41 +0100969 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200970 early_identify_cpu(&boot_cpu_data);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800971}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700973/*
Borislav Petkov366d4a42010-10-04 09:31:27 +0200974 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
975 * unfortunately, that's not true in practice because of early VIA
976 * chips and (more importantly) broken virtualizers that are not easy
977 * to detect. In the latter case it doesn't even *fail* reliably, so
978 * probing for it doesn't even work. Disable it completely on 32-bit
H. Peter Anvinba0593b2008-09-16 09:29:40 -0700979 * unless we can find a reliable way to detect all the broken cases.
Borislav Petkov366d4a42010-10-04 09:31:27 +0200980 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700981 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400982static void detect_nopl(struct cpuinfo_x86 *c)
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700983{
Borislav Petkov366d4a42010-10-04 09:31:27 +0200984#ifdef CONFIG_X86_32
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700985 clear_cpu_cap(c, X86_FEATURE_NOPL);
Borislav Petkov366d4a42010-10-04 09:31:27 +0200986#else
987 set_cpu_cap(c, X86_FEATURE_NOPL);
988#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Andy Lutomirski7a5d67042016-04-07 17:31:46 -0700991static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
992{
993#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
Andy Lutomirski7a5d67042016-04-07 17:31:46 -0700995 * Empirically, writing zero to a segment selector on AMD does
996 * not clear the base, whereas writing zero to a segment
997 * selector on Intel does clear the base. Intel's behavior
998 * allows slightly faster context switches in the common case
999 * where GS is unused by the prev and next threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 *
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001001 * Since neither vendor documents this anywhere that I can see,
1002 * detect it directly instead of hardcoding the choice by
1003 * vendor.
1004 *
1005 * I've designated AMD's behavior as the "bug" because it's
1006 * counterintuitive and less friendly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 */
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001008
1009 unsigned long old_base, tmp;
1010 rdmsrl(MSR_FS_BASE, old_base);
1011 wrmsrl(MSR_FS_BASE, 1);
1012 loadsegment(fs, 0);
1013 rdmsrl(MSR_FS_BASE, tmp);
1014 if (tmp != 0)
1015 set_cpu_bug(c, X86_BUG_NULL_SEG);
1016 wrmsrl(MSR_FS_BASE, old_base);
Yinghai Lu3da99c92008-09-04 21:09:44 +02001017#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
Yinghai Luaef93c82008-09-14 02:33:15 -07001019
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001020static void generic_identify(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 c->extended_cpuid_level = 0;
1023
Yinghai Luaef93c82008-09-14 02:33:15 -07001024 if (!have_cpuid_p())
1025 identify_cpu_without_cpuid(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001026
Yinghai Luaef93c82008-09-14 02:33:15 -07001027 /* cyrix could have cpuid enabled via c_identify()*/
Ingo Molnara9853dd2008-09-14 14:46:58 +02001028 if (!have_cpuid_p())
Yinghai Luaef93c82008-09-14 02:33:15 -07001029 return;
1030
Yinghai Lu3da99c92008-09-04 21:09:44 +02001031 cpu_detect(c);
1032
1033 get_cpu_vendor(c);
1034
1035 get_cpu_cap(c);
1036
1037 if (c->cpuid_level >= 0x00000001) {
1038 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
Yinghai Lub89d3b32008-09-04 20:09:12 -07001039#ifdef CONFIG_X86_32
Borislav Petkovc8e56d22015-06-04 18:55:25 +02001040# ifdef CONFIG_SMP
Ingo Molnarcb8cc442009-01-28 13:24:54 +01001041 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lub89d3b32008-09-04 20:09:12 -07001042# else
Yinghai Lu3da99c92008-09-04 21:09:44 +02001043 c->apicid = c->initial_apicid;
Yinghai Lub89d3b32008-09-04 20:09:12 -07001044# endif
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08001045#endif
Yinghai Lub89d3b32008-09-04 20:09:12 -07001046 c->phys_proc_id = c->initial_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Yinghai Lu3da99c92008-09-04 21:09:44 +02001048
Yinghai Lu1b05d602008-09-06 01:52:27 -07001049 get_model_name(c); /* Default name */
Yinghai Lu3da99c92008-09-04 21:09:44 +02001050
Yinghai Lu3da99c92008-09-04 21:09:44 +02001051 detect_nopl(c);
Andy Lutomirski7a5d67042016-04-07 17:31:46 -07001052
1053 detect_null_seg_behavior(c);
Andy Lutomirski0230bb02016-04-07 17:31:48 -07001054
1055 /*
1056 * ESPFIX is a strange bug. All real CPUs have it. Paravirt
1057 * systems that run Linux at CPL > 0 may or may not have the
1058 * issue, but, even if they have the issue, there's absolutely
1059 * nothing we can do about it because we can't use the real IRET
1060 * instruction.
1061 *
1062 * NB: For the time being, only 32-bit kernels support
1063 * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
1064 * whether to apply espfix using paravirt hooks. If any
1065 * non-paravirt system ever shows up that does *not* have the
1066 * ESPFIX issue, we can change this.
1067 */
1068#ifdef CONFIG_X86_32
1069# ifdef CONFIG_PARAVIRT
1070 do {
1071 extern void native_iret(void);
1072 if (pv_cpu_ops.iret == native_iret)
1073 set_cpu_bug(c, X86_BUG_ESPFIX);
1074 } while (0);
1075# else
1076 set_cpu_bug(c, X86_BUG_ESPFIX);
1077# endif
1078#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001081static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1082{
1083 /*
1084 * The heavy lifting of max_rmid and cache_occ_scale are handled
1085 * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
1086 * in case CQM bits really aren't there in this CPU.
1087 */
1088 if (c != &boot_cpu_data) {
1089 boot_cpu_data.x86_cache_max_rmid =
1090 min(boot_cpu_data.x86_cache_max_rmid,
1091 c->x86_cache_max_rmid);
1092 }
1093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095/*
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001096 * Validate that ACPI/mptables have the same information about the
1097 * effective APIC id and update the package map.
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001098 */
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001099static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001100{
1101#ifdef CONFIG_SMP
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001102 unsigned int apicid, cpu = smp_processor_id();
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001103
1104 apicid = apic->cpu_present_to_apicid(cpu);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001105
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001106 if (apicid != c->apicid) {
1107 pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001108 cpu, apicid, c->initial_apicid);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001109 }
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001110 BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001111#else
1112 c->logical_proc_id = 0;
1113#endif
1114}
1115
1116/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 * This does the hard work of actually picking apart the CPU stuff...
1118 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001119static void identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 int i;
1122
1123 c->loops_per_jiffy = loops_per_jiffy;
1124 c->x86_cache_size = -1;
1125 c->x86_vendor = X86_VENDOR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 c->x86_model = c->x86_mask = 0; /* So far unknown... */
1127 c->x86_vendor_id[0] = '\0'; /* Unset */
1128 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001129 c->x86_max_cores = 1;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001130 c->x86_coreid_bits = 0;
Borislav Petkov79a8b9a2017-02-05 11:50:21 +01001131 c->cu_id = 0xff;
Yinghai Lu11fdd252008-09-07 17:58:50 -07001132#ifdef CONFIG_X86_64
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001133 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +00001134 c->x86_phys_bits = 36;
1135 c->x86_virt_bits = 48;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001136#else
1137 c->cpuid_level = -1; /* CPUID not detected */
Andi Kleen770d1322006-12-07 02:14:05 +01001138 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +00001139 c->x86_phys_bits = 32;
1140 c->x86_virt_bits = 32;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001141#endif
1142 c->x86_cache_alignment = c->x86_clflush_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 memset(&c->x86_capability, 0, sizeof c->x86_capability);
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 generic_identify(c);
1146
Andi Kleen38985342008-01-30 13:32:49 +01001147 if (this_cpu->c_identify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 this_cpu->c_identify(c);
1149
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001150 /* Clear/Set all flags overridden by options, after probe */
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -08001151 apply_forced_caps(c);
Yinghai Lu2759c322009-05-15 13:05:16 -07001152
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001153#ifdef CONFIG_X86_64
Ingo Molnarcb8cc442009-01-28 13:24:54 +01001154 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001155#endif
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /*
1158 * Vendor-specific initialization. In this section we
1159 * canonicalize the feature flags, meaning if there are
1160 * features a certain CPU supports which CPUID doesn't
1161 * tell us, CPUID claiming incorrect flags, or other bugs,
1162 * we handle them here.
1163 *
1164 * At the end of this section, c->x86_capability better
1165 * indicate the features this CPU genuinely supports!
1166 */
1167 if (this_cpu->c_init)
1168 this_cpu->c_init(c);
1169
1170 /* Disable the PN if appropriate */
1171 squash_the_stupid_serial_number(c);
1172
Ricardo Neriaa35f892017-11-05 18:27:54 -08001173 /* Set up SMEP/SMAP/UMIP */
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -07001174 setup_smep(c);
1175 setup_smap(c);
Ricardo Neriaa35f892017-11-05 18:27:54 -08001176 setup_umip(c);
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -07001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001179 * The vendor-specific functions might have changed features.
1180 * Now we do "generic changes."
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
1182
H. Peter Anvinb38b0662009-01-23 17:20:50 -08001183 /* Filter out anything that depends on CPUID levels we don't have */
1184 filter_cpuid_features(c, true);
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* If the model name is still unset, do table lookup. */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001187 if (!c->x86_model_id[0]) {
Jan Beulich02dde8b2009-03-12 12:08:49 +00001188 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 p = table_lookup_model(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001190 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 strcpy(c->x86_model_id, p);
1192 else
1193 /* Last resort... */
1194 sprintf(c->x86_model_id, "%02x/%02x",
Chuck Ebbert54a20f82006-03-23 02:59:36 -08001195 c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001198#ifdef CONFIG_X86_64
1199 detect_ht(c);
1200#endif
1201
H. Peter Anvin49d859d2011-07-31 14:02:19 -07001202 x86_init_rdrand(c);
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001203 x86_init_cache_qos(c);
Dave Hansen06976942016-02-12 13:02:29 -08001204 setup_pku(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001205
1206 /*
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001207 * Clear/Set all flags overridden by options, need do it
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001208 * before following smp all cpus cap AND.
1209 */
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -08001210 apply_forced_caps(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /*
1213 * On SMP, boot_cpu_data holds the common feature set between
1214 * all CPUs; so make sure that we indicate which features are
1215 * common between the CPUs. The first time this routine gets
1216 * executed, c == &boot_cpu_data.
1217 */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001218 if (c != &boot_cpu_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /* AND the already accumulated flags with these */
Yinghai Lu9d31d352008-09-04 21:09:44 +02001220 for (i = 0; i < NCAPINTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
Borislav Petkov65fc9852013-03-20 15:07:23 +01001222
1223 /* OR, i.e. replicate the bug flags */
1224 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
1225 c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
1228 /* Init Machine Check Exception if available. */
Borislav Petkov5e099542009-10-16 12:31:32 +02001229 mcheck_cpu_init(c);
Andi Kleen30d432d2008-01-30 13:33:16 +01001230
1231 select_idle_routine(c);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001232
Tejun Heode2d9442011-01-23 14:37:41 +01001233#ifdef CONFIG_NUMA
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001234 numa_add_cpu(smp_processor_id());
1235#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001236}
Shaohua Li31ab2692005-11-07 00:58:42 -08001237
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001238/*
1239 * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
1240 * on 32-bit kernels:
1241 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001242#ifdef CONFIG_X86_32
1243void enable_sep_cpu(void)
1244{
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001245 struct tss_struct *tss;
1246 int cpu;
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001247
Borislav Petkovb3edfda2016-03-16 13:19:29 +01001248 if (!boot_cpu_has(X86_FEATURE_SEP))
1249 return;
1250
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001251 cpu = get_cpu();
Andy Lutomirskic482fee2017-12-04 15:07:29 +01001252 tss = &per_cpu(cpu_tss_rw, cpu);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001253
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001254 /*
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001255 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1256 * see the big comment in struct x86_hw_tss's definition.
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001257 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001258
1259 tss->x86_tss.ss1 = __KERNEL_CS;
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001260 wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
Dave Hansen4fe2d8b2017-12-04 17:25:07 -08001261 wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0);
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001262 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001263
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001264 put_cpu();
1265}
Glauber Costae04d6452008-09-22 14:35:08 -03001266#endif
1267
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001268void __init identify_boot_cpu(void)
1269{
1270 identify_cpu(&boot_cpu_data);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001271#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001272 sysenter_setup();
Li Shaohua6fe940d2005-06-25 14:54:53 -07001273 enable_sep_cpu();
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001274#endif
Borislav Petkov5b5563322012-08-06 19:00:37 +02001275 cpu_detect_tlb(&boot_cpu_data);
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001276}
Shaohua Li3b520b22005-07-07 17:56:38 -07001277
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001278void identify_secondary_cpu(struct cpuinfo_x86 *c)
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001279{
1280 BUG_ON(c == &boot_cpu_data);
1281 identify_cpu(c);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001282#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001283 enable_sep_cpu();
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001284#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001285 mtrr_ap_init();
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001286 validate_apic_and_package_id(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Andi Kleen191679f2008-01-30 13:33:21 +01001289static __init int setup_noclflush(char *arg)
1290{
H. Peter Anvin840d2832014-02-27 08:31:30 -08001291 setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
H. Peter Anvinda4aaa72014-02-27 08:36:31 -08001292 setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
Andi Kleen191679f2008-01-30 13:33:21 +01001293 return 1;
1294}
1295__setup("noclflush", setup_noclflush);
1296
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001297void print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Jan Beulich02dde8b2009-03-12 12:08:49 +00001299 const char *vendor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001301 if (c->x86_vendor < X86_VENDOR_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 vendor = this_cpu->c_vendor;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001303 } else {
1304 if (c->cpuid_level >= 0)
1305 vendor = c->x86_vendor_id;
1306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Yinghai Lubd32a8cf2008-09-19 18:41:16 -07001308 if (vendor && !strstr(c->x86_model_id, vendor))
Chen Yucong1b74dde2016-02-02 11:45:02 +08001309 pr_cont("%s ", vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Yinghai Lu9d31d352008-09-04 21:09:44 +02001311 if (c->x86_model_id[0])
Chen Yucong1b74dde2016-02-02 11:45:02 +08001312 pr_cont("%s", c->x86_model_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001314 pr_cont("%d86", c->x86);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Chen Yucong1b74dde2016-02-02 11:45:02 +08001316 pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
Borislav Petkov924e1012012-09-14 18:37:46 +02001317
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001318 if (c->x86_mask || c->cpuid_level >= 0)
Chen Yucong1b74dde2016-02-02 11:45:02 +08001319 pr_cont(", stepping: 0x%x)\n", c->x86_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001321 pr_cont(")\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Andi Kleen0c2a3912017-10-13 14:56:43 -07001324/*
1325 * clearcpuid= was already parsed in fpu__init_parse_early_param.
1326 * But we need to keep a dummy __setup around otherwise it would
1327 * show up as an environment variable for init.
1328 */
1329static __init int setup_clearcpuid(char *arg)
Andi Kleenac72e782008-01-30 13:33:21 +01001330{
Andi Kleenac72e782008-01-30 13:33:21 +01001331 return 1;
1332}
Andi Kleen0c2a3912017-10-13 14:56:43 -07001333__setup("clearcpuid=", setup_clearcpuid);
Andi Kleenac72e782008-01-30 13:33:21 +01001334
Yinghai Lud5494d42008-09-04 20:09:03 -07001335#ifdef CONFIG_X86_64
Brian Gerst947e76c2009-01-19 12:21:28 +09001336DEFINE_PER_CPU_FIRST(union irq_stack_union,
Andi Kleen277d5b42013-08-05 15:02:43 -07001337 irq_stack_union) __aligned(PAGE_SIZE) __visible;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001338
Tejun Heobdf977b2009-08-03 14:12:19 +09001339/*
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001340 * The following percpu variables are hot. Align current_task to
1341 * cacheline size such that they fall in the same cacheline.
Tejun Heobdf977b2009-08-03 14:12:19 +09001342 */
1343DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1344 &init_task;
1345EXPORT_PER_CPU_SYMBOL(current_task);
Yinghai Lud5494d42008-09-04 20:09:03 -07001346
Tejun Heobdf977b2009-08-03 14:12:19 +09001347DEFINE_PER_CPU(char *, irq_stack_ptr) =
Josh Poimboeuf4950d6d2016-08-18 10:59:08 -05001348 init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE;
Tejun Heobdf977b2009-08-03 14:12:19 +09001349
Andi Kleen277d5b42013-08-05 15:02:43 -07001350DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
Yinghai Lud5494d42008-09-04 20:09:03 -07001351
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001352DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1353EXPORT_PER_CPU_SYMBOL(__preempt_count);
1354
Yinghai Lud5494d42008-09-04 20:09:03 -07001355/* May not be marked __init: used by software suspend */
1356void syscall_init(void)
1357{
Andy Lutomirski3386bc82017-12-04 15:07:25 +01001358 extern char _entry_trampoline[];
1359 extern char entry_SYSCALL_64_trampoline[];
1360
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001361 int cpu = smp_processor_id();
Andy Lutomirski3386bc82017-12-04 15:07:25 +01001362 unsigned long SYSCALL64_entry_trampoline =
1363 (unsigned long)get_cpu_entry_area(cpu)->entry_trampoline +
1364 (entry_SYSCALL_64_trampoline - _entry_trampoline);
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001365
Borislav Petkov31ac34c2015-11-23 11:12:25 +01001366 wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
Thomas Gleixner8d4b0672017-12-04 15:07:43 +01001367 if (static_cpu_has(X86_FEATURE_PTI))
1368 wrmsrl(MSR_LSTAR, SYSCALL64_entry_trampoline);
1369 else
1370 wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001371
1372#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski47edb652015-07-23 12:14:40 -07001373 wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001374 /*
Denys Vlasenko487d1ed2015-03-27 11:59:16 +01001375 * This only works on Intel CPUs.
1376 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1377 * This does not cause SYSENTER to jump to the wrong location, because
1378 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001379 */
1380 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
Dave Hansen4fe2d8b2017-12-04 17:25:07 -08001381 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1));
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001382 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001383#else
Andy Lutomirski47edb652015-07-23 12:14:40 -07001384 wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
Borislav Petkov6b513112015-04-03 14:25:28 +02001385 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001386 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1387 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
Yinghai Lud5494d42008-09-04 20:09:03 -07001388#endif
1389
1390 /* Flags to clear on syscall */
1391 wrmsrl(MSR_SYSCALL_MASK,
H. Peter Anvin63bcff22012-09-21 12:43:12 -07001392 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
Andy Lutomirski8c7aa692014-10-01 11:49:04 -07001393 X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
Yinghai Lud5494d42008-09-04 20:09:03 -07001394}
1395
Yinghai Lud5494d42008-09-04 20:09:03 -07001396/*
1397 * Copies of the original ist values from the tss are only accessed during
1398 * debugging, no special alignment required.
1399 */
1400DEFINE_PER_CPU(struct orig_ist, orig_ist);
1401
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001402static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
Steven Rostedt42181182011-12-16 11:43:02 -05001403DEFINE_PER_CPU(int, debug_stack_usage);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001404
1405int is_debug_stack(unsigned long addr)
1406{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001407 return __this_cpu_read(debug_stack_usage) ||
1408 (addr <= __this_cpu_read(debug_stack_addr) &&
1409 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001410}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001411NOKPROBE_SYMBOL(is_debug_stack);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001412
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001413DEFINE_PER_CPU(u32, debug_idt_ctr);
Steven Rostedtf8988172012-05-30 11:47:00 -04001414
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001415void debug_stack_set_zero(void)
1416{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001417 this_cpu_inc(debug_idt_ctr);
1418 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001419}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001420NOKPROBE_SYMBOL(debug_stack_set_zero);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001421
1422void debug_stack_reset(void)
1423{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001424 if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
Steven Rostedtf8988172012-05-30 11:47:00 -04001425 return;
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001426 if (this_cpu_dec_return(debug_idt_ctr) == 0)
1427 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001428}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001429NOKPROBE_SYMBOL(debug_stack_reset);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001430
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001431#else /* CONFIG_X86_64 */
Yinghai Lud5494d42008-09-04 20:09:03 -07001432
Tejun Heobdf977b2009-08-03 14:12:19 +09001433DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1434EXPORT_PER_CPU_SYMBOL(current_task);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001435DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1436EXPORT_PER_CPU_SYMBOL(__preempt_count);
Tejun Heobdf977b2009-08-03 14:12:19 +09001437
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001438/*
1439 * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1440 * the top of the kernel stack. Use an extra percpu variable to track the
1441 * top of the kernel stack directly.
1442 */
1443DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1444 (unsigned long)&init_thread_union + THREAD_SIZE;
1445EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1446
Tejun Heo60a53172009-02-09 22:17:40 +09001447#ifdef CONFIG_CC_STACKPROTECTOR
Jeremy Fitzhardinge53f82452009-09-03 14:31:44 -07001448DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
Tejun Heo60a53172009-02-09 22:17:40 +09001449#endif
1450
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001451#endif /* CONFIG_X86_64 */
Jeremy Fitzhardingec5413fb2007-05-02 19:27:16 +02001452
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001453/*
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301454 * Clear all 6 debug registers:
1455 */
1456static void clear_all_debug_regs(void)
1457{
1458 int i;
1459
1460 for (i = 0; i < 8; i++) {
1461 /* Ignore db4, db5 */
1462 if ((i == 4) || (i == 5))
1463 continue;
1464
1465 set_debugreg(0, i);
1466 }
1467}
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +01001468
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001469#ifdef CONFIG_KGDB
1470/*
1471 * Restore debug regs if using kgdbwait and you have a kernel debugger
1472 * connection established.
1473 */
1474static void dbg_restore_debug_regs(void)
1475{
1476 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1477 arch_kgdb_ops.correct_hw_break();
1478}
1479#else /* ! CONFIG_KGDB */
1480#define dbg_restore_debug_regs()
1481#endif /* ! CONFIG_KGDB */
1482
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001483static void wait_for_master_cpu(int cpu)
1484{
1485#ifdef CONFIG_SMP
1486 /*
1487 * wait for ACK from master CPU before continuing
1488 * with AP initialization
1489 */
1490 WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1491 while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1492 cpu_relax();
1493#endif
1494}
1495
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001496/*
1497 * cpu_init() initializes state that is per-CPU. Some data is already
1498 * initialized (naturally) in the bootstrap process, such as the GDT
1499 * and IDT. We reload them nevertheless, this function acts as a
1500 * 'CPU state barrier', nothing should get across.
Yinghai Lu1ba76582008-09-04 20:09:04 -07001501 * A lot of state is already set up in PDA init for 64 bit
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001502 */
Yinghai Lu1ba76582008-09-04 20:09:04 -07001503#ifdef CONFIG_X86_64
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001504
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001505void cpu_init(void)
Yinghai Lu1ba76582008-09-04 20:09:04 -07001506{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001507 struct orig_ist *oist;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001508 struct task_struct *me;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001509 struct tss_struct *t;
1510 unsigned long v;
Andy Lutomirskifb598312016-07-14 13:22:58 -07001511 int cpu = raw_smp_processor_id();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001512 int i;
1513
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001514 wait_for_master_cpu(cpu);
1515
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001516 /*
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001517 * Initialize the CR4 shadow before doing anything that could
1518 * try to read it.
1519 */
1520 cr4_init_shadow();
1521
Borislav Petkov777284b2016-10-25 11:55:11 +02001522 if (cpu)
1523 load_ucode_ap();
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001524
Andy Lutomirskic482fee2017-12-04 15:07:29 +01001525 t = &per_cpu(cpu_tss_rw, cpu);
Tejun Heo0fe1e002009-10-29 22:34:14 +09001526 oist = &per_cpu(orig_ist, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001527
Brian Gerste7a22c12009-01-19 00:38:59 +09001528#ifdef CONFIG_NUMA
Fenghua Yu27fd1852012-11-13 11:32:47 -08001529 if (this_cpu_read(numa_node) == 0 &&
Lee Schermerhorne534c7c2010-05-26 14:44:58 -07001530 early_cpu_to_node(cpu) != NUMA_NO_NODE)
1531 set_numa_node(early_cpu_to_node(cpu));
Brian Gerste7a22c12009-01-19 00:38:59 +09001532#endif
Yinghai Lu1ba76582008-09-04 20:09:04 -07001533
1534 me = current;
1535
Mike Travis2eaad1f2009-12-10 17:19:36 -08001536 pr_debug("Initializing CPU#%d\n", cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001537
Andy Lutomirski375074c2014-10-24 15:58:07 -07001538 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001539
1540 /*
1541 * Initialize the per-CPU GDT with the boot GDT,
1542 * and set up the GDT descriptor:
1543 */
1544
Brian Gerst552be872009-01-30 17:47:53 +09001545 switch_to_new_gdt(cpu);
Brian Gerst2697fbd2009-01-27 12:56:48 +09001546 loadsegment(fs, 0);
1547
Seiji Aguchicf910e82013-06-20 11:46:53 -04001548 load_current_idt();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001549
1550 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1551 syscall_init();
1552
1553 wrmsrl(MSR_FS_BASE, 0);
1554 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1555 barrier();
1556
H. Peter Anvin4763ed42009-11-13 15:28:16 -08001557 x86_configure_nx();
Thomas Gleixner659006b2015-01-15 21:22:26 +00001558 x2apic_setup();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001559
1560 /*
1561 * set up and load the per-CPU TSS
1562 */
Tejun Heo0fe1e002009-10-29 22:34:14 +09001563 if (!oist->ist[0]) {
Andy Lutomirski40e7f942017-12-04 15:07:26 +01001564 char *estacks = get_cpu_entry_area(cpu)->exception_stacks;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001565
Yinghai Lu1ba76582008-09-04 20:09:04 -07001566 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001567 estacks += exception_stack_sizes[v];
Tejun Heo0fe1e002009-10-29 22:34:14 +09001568 oist->ist[v] = t->x86_tss.ist[v] =
Yinghai Lu1ba76582008-09-04 20:09:04 -07001569 (unsigned long)estacks;
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001570 if (v == DEBUG_STACK-1)
1571 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001572 }
1573 }
1574
Andy Lutomirski7fb983b2017-12-04 15:07:17 +01001575 t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001576
Yinghai Lu1ba76582008-09-04 20:09:04 -07001577 /*
1578 * <= is required because the CPU will access up to
1579 * 8 bits beyond the end of the IO permission bitmap.
1580 */
1581 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1582 t->io_bitmap[i] = ~0UL;
1583
Vegard Nossumf1f10072017-02-27 14:30:07 -08001584 mmgrab(&init_mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001585 me->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001586 BUG_ON(me->mm);
Andy Lutomirski72c00982017-09-06 19:54:53 -07001587 initialize_tlbstate_and_flush();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001588 enter_lazy_tlb(&init_mm, me);
1589
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001590 /*
Andy Lutomirski7f2590a2017-12-04 15:07:23 +01001591 * Initialize the TSS. sp0 points to the entry trampoline stack
1592 * regardless of what task is running.
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001593 */
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001594 set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001595 load_TR_desc();
Dave Hansen4fe2d8b2017-12-04 17:25:07 -08001596 load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1));
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001597
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001598 load_mm_ldt(&init_mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001599
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001600 clear_all_debug_regs();
1601 dbg_restore_debug_regs();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001602
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001603 fpu__init_cpu();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001604
Yinghai Lu1ba76582008-09-04 20:09:04 -07001605 if (is_uv_system())
1606 uv_cpu_init();
Thomas Garnier69218e42017-03-14 10:05:07 -07001607
Thomas Garnier69218e42017-03-14 10:05:07 -07001608 load_fixmap_gdt(cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001609}
1610
1611#else
1612
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001613void cpu_init(void)
James Bottomley9ee79a32007-01-22 09:18:31 -06001614{
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001615 int cpu = smp_processor_id();
1616 struct task_struct *curr = current;
Andy Lutomirskic482fee2017-12-04 15:07:29 +01001617 struct tss_struct *t = &per_cpu(cpu_tss_rw, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001619 wait_for_master_cpu(cpu);
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001620
Steven Rostedt5b2bdbc2015-02-27 14:50:19 -05001621 /*
1622 * Initialize the CR4 shadow before doing anything that could
1623 * try to read it.
1624 */
1625 cr4_init_shadow();
1626
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001627 show_ucode_info_early();
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001628
Chen Yucong1b74dde2016-02-02 11:45:02 +08001629 pr_info("Initializing CPU#%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Borislav Petkov362f9242015-12-07 10:39:41 +01001631 if (cpu_feature_enabled(X86_FEATURE_VME) ||
Borislav Petkov59e21e32016-04-04 22:24:59 +02001632 boot_cpu_has(X86_FEATURE_TSC) ||
Borislav Petkov362f9242015-12-07 10:39:41 +01001633 boot_cpu_has(X86_FEATURE_DE))
Andy Lutomirski375074c2014-10-24 15:58:07 -07001634 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Seiji Aguchicf910e82013-06-20 11:46:53 -04001636 load_current_idt();
Brian Gerst552be872009-01-30 17:47:53 +09001637 switch_to_new_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 * Set up and load the per-CPU TSS and LDT
1641 */
Vegard Nossumf1f10072017-02-27 14:30:07 -08001642 mmgrab(&init_mm);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001643 curr->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001644 BUG_ON(curr->mm);
Andy Lutomirski72c00982017-09-06 19:54:53 -07001645 initialize_tlbstate_and_flush();
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001646 enter_lazy_tlb(&init_mm, curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001648 /*
1649 * Initialize the TSS. Don't bother initializing sp0, as the initial
1650 * task never enters user mode.
1651 */
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001652 set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 load_TR_desc();
Andy Lutomirski20bb8342017-11-02 00:59:13 -07001654
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001655 load_mm_ldt(&init_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Andy Lutomirski7fb983b2017-12-04 15:07:17 +01001657 t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
Thomas Gleixnerf9a196b2009-05-01 20:59:25 +02001658
Matt Mackall22c4e302006-01-08 01:05:24 -08001659#ifdef CONFIG_DOUBLEFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 /* Set up doublefault TSS pointer in the GDT */
1661 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
Matt Mackall22c4e302006-01-08 01:05:24 -08001662#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301664 clear_all_debug_regs();
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001665 dbg_restore_debug_regs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001667 fpu__init_cpu();
Thomas Garnier69218e42017-03-14 10:05:07 -07001668
Thomas Garnier69218e42017-03-14 10:05:07 -07001669 load_fixmap_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
Yinghai Lu1ba76582008-09-04 20:09:04 -07001671#endif
Borislav Petkov5700f742013-06-09 12:07:32 +02001672
Laura Abbottb51ef522015-07-20 14:47:58 -07001673static void bsp_resume(void)
1674{
1675 if (this_cpu->c_bsp_resume)
1676 this_cpu->c_bsp_resume(&boot_cpu_data);
1677}
1678
1679static struct syscore_ops cpu_syscore_ops = {
1680 .resume = bsp_resume,
1681};
1682
1683static int __init init_cpu_syscore(void)
1684{
1685 register_syscore_ops(&cpu_syscore_ops);
1686 return 0;
1687}
1688core_initcall(init_cpu_syscore);