blob: f8e22dbad86c2c633d46ed9fba4d4443fcca6c6b [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 Lutomirskid12a72b2016-01-29 11:42:58 -0800171static int __init x86_noinvpcid_setup(char *s)
172{
173 /* noinvpcid doesn't accept parameters */
174 if (s)
175 return -EINVAL;
176
177 /* do not emit a message if the feature is not present */
178 if (!boot_cpu_has(X86_FEATURE_INVPCID))
179 return 0;
180
181 setup_clear_cpu_cap(X86_FEATURE_INVPCID);
182 pr_info("noinvpcid: INVPCID feature disabled\n");
183 return 0;
184}
185early_param("noinvpcid", x86_noinvpcid_setup);
186
Yinghai Luba51dce2008-09-04 20:09:02 -0700187#ifdef CONFIG_X86_32
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400188static int cachesize_override = -1;
189static int disable_x86_serial_nr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static int __init cachesize_setup(char *str)
192{
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100193 get_option(&str, &cachesize_override);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return 1;
195}
196__setup("cachesize=", cachesize_setup);
197
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100198static int __init x86_sep_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Andi Kleen13530252008-01-30 13:33:20 +0100200 setup_clear_cpu_cap(X86_FEATURE_SEP);
Chuck Ebbert4f886512006-03-23 02:59:34 -0800201 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
Chuck Ebbert4f886512006-03-23 02:59:34 -0800203__setup("nosep", x86_sep_setup);
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/* Standard macro to see if a specific flag is changeable */
206static inline int flag_is_changeable_p(u32 flag)
207{
208 u32 f1, f2;
209
Krzysztof Helt94f6bac2008-09-30 23:17:51 +0200210 /*
211 * Cyrix and IDT cpus allow disabling of CPUID
212 * so the code below may return different results
213 * when it is executed before and after enabling
214 * the CPUID. Add "volatile" to not allow gcc to
215 * optimize the subsequent calls to this function.
216 */
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100217 asm volatile ("pushfl \n\t"
218 "pushfl \n\t"
219 "popl %0 \n\t"
220 "movl %0, %1 \n\t"
221 "xorl %2, %0 \n\t"
222 "pushl %0 \n\t"
223 "popfl \n\t"
224 "pushfl \n\t"
225 "popl %0 \n\t"
226 "popfl \n\t"
227
Krzysztof Helt94f6bac2008-09-30 23:17:51 +0200228 : "=&r" (f1), "=&r" (f2)
229 : "ir" (flag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 return ((f1^f2) & flag) != 0;
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/* Probe for the CPUID instruction */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400235int have_cpuid_p(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 return flag_is_changeable_p(X86_EFLAGS_ID);
238}
239
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400240static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
Yinghai Lu0a488a52008-09-04 21:09:47 +0200241{
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100242 unsigned long lo, hi;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200243
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100244 if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
245 return;
246
247 /* Disable processor serial number: */
248
249 rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
250 lo |= 0x200000;
251 wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
252
Chen Yucong1b74dde2016-02-02 11:45:02 +0800253 pr_notice("CPU serial number disabled.\n");
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100254 clear_cpu_cap(c, X86_FEATURE_PN);
255
256 /* Disabling the serial number may affect the cpuid level */
257 c->cpuid_level = cpuid_eax(0);
Yinghai Lu0a488a52008-09-04 21:09:47 +0200258}
259
260static int __init x86_serial_nr_setup(char *s)
261{
262 disable_x86_serial_nr = 0;
263 return 1;
264}
265__setup("serialnumber", x86_serial_nr_setup);
Yinghai Luba51dce2008-09-04 20:09:02 -0700266#else
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700267static inline int flag_is_changeable_p(u32 flag)
268{
269 return 1;
270}
Yinghai Lu102bbe3a2008-09-04 20:09:13 -0700271static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
272{
273}
Yinghai Luba51dce2008-09-04 20:09:02 -0700274#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Fenghua Yude5397a2011-05-11 16:51:05 -0700276static __init int setup_disable_smep(char *arg)
277{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700278 setup_clear_cpu_cap(X86_FEATURE_SMEP);
Dave Hansen0f6ff2b2016-05-12 15:04:00 -0700279 /* Check for things that depend on SMEP being enabled: */
280 check_mpx_erratum(&boot_cpu_data);
Fenghua Yude5397a2011-05-11 16:51:05 -0700281 return 1;
282}
283__setup("nosmep", setup_disable_smep);
284
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700285static __always_inline void setup_smep(struct cpuinfo_x86 *c)
Fenghua Yude5397a2011-05-11 16:51:05 -0700286{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700287 if (cpu_has(c, X86_FEATURE_SMEP))
Andy Lutomirski375074c2014-10-24 15:58:07 -0700288 cr4_set_bits(X86_CR4_SMEP);
Fenghua Yude5397a2011-05-11 16:51:05 -0700289}
290
H. Peter Anvin52b61792012-09-21 12:43:13 -0700291static __init int setup_disable_smap(char *arg)
292{
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700293 setup_clear_cpu_cap(X86_FEATURE_SMAP);
H. Peter Anvin52b61792012-09-21 12:43:13 -0700294 return 1;
295}
296__setup("nosmap", setup_disable_smap);
297
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700298static __always_inline void setup_smap(struct cpuinfo_x86 *c)
H. Peter Anvin52b61792012-09-21 12:43:13 -0700299{
Andrew Cooper581b7f152015-06-03 10:31:14 +0100300 unsigned long eflags = native_save_fl();
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700301
302 /* This should have been cleared long ago */
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -0700303 BUG_ON(eflags & X86_EFLAGS_AC);
304
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800305 if (cpu_has(c, X86_FEATURE_SMAP)) {
306#ifdef CONFIG_X86_SMAP
Andy Lutomirski375074c2014-10-24 15:58:07 -0700307 cr4_set_bits(X86_CR4_SMAP);
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800308#else
Andy Lutomirski375074c2014-10-24 15:58:07 -0700309 cr4_clear_bits(X86_CR4_SMAP);
H. Peter Anvin03bbd592014-02-13 07:34:30 -0800310#endif
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314/*
Dave Hansen06976942016-02-12 13:02:29 -0800315 * Protection Keys are not available in 32-bit mode.
316 */
317static bool pku_disabled;
318
319static __always_inline void setup_pku(struct cpuinfo_x86 *c)
320{
Dave Hansene8df1a952016-05-13 15:13:28 -0700321 /* check the boot processor, plus compile options for PKU: */
322 if (!cpu_feature_enabled(X86_FEATURE_PKU))
323 return;
324 /* checks the actual processor's cpuid bits: */
Dave Hansen06976942016-02-12 13:02:29 -0800325 if (!cpu_has(c, X86_FEATURE_PKU))
326 return;
327 if (pku_disabled)
328 return;
329
330 cr4_set_bits(X86_CR4_PKE);
331 /*
332 * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
333 * cpuid bit to be set. We need to ensure that we
334 * update that bit in this CPU's "cpu_info".
335 */
336 get_cpu_cap(c);
337}
338
339#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
340static __init int setup_disable_pku(char *arg)
341{
342 /*
343 * Do not clear the X86_FEATURE_PKU bit. All of the
344 * runtime checks are against OSPKE so clearing the
345 * bit does nothing.
346 *
347 * This way, we will see "pku" in cpuinfo, but not
348 * "ospke", which is exactly what we want. It shows
349 * that the CPU has PKU, but the OS has not enabled it.
350 * This happens to be exactly how a system would look
351 * if we disabled the config option.
352 */
353 pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
354 pku_disabled = true;
355 return 1;
356}
357__setup("nopku", setup_disable_pku);
358#endif /* CONFIG_X86_64 */
359
360/*
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800361 * Some CPU features depend on higher CPUID levels, which may not always
362 * be available due to CPUID level capping or broken virtualization
363 * software. Add those features to this table to auto-disable them.
364 */
365struct cpuid_dependent_feature {
366 u32 feature;
367 u32 level;
368};
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100369
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400370static const struct cpuid_dependent_feature
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800371cpuid_dependent_features[] = {
372 { X86_FEATURE_MWAIT, 0x00000005 },
373 { X86_FEATURE_DCA, 0x00000009 },
374 { X86_FEATURE_XSAVE, 0x0000000d },
375 { 0, 0 }
376};
377
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400378static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800379{
380 const struct cpuid_dependent_feature *df;
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530381
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800382 for (df = cpuid_dependent_features; df->feature; df++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100383
384 if (!cpu_has(c, df->feature))
385 continue;
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800386 /*
387 * Note: cpuid_level is set to -1 if unavailable, but
388 * extended_extended_level is set to 0 if unavailable
389 * and the legitimate extended levels are all negative
390 * when signed; hence the weird messing around with
391 * signs here...
392 */
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100393 if (!((s32)df->level < 0 ?
Yinghai Luf6db44d2009-02-14 23:59:18 -0800394 (u32)df->level > (u32)c->extended_cpuid_level :
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100395 (s32)df->level > (s32)c->cpuid_level))
396 continue;
397
398 clear_cpu_cap(c, df->feature);
399 if (!warn)
400 continue;
401
Chen Yucong1b74dde2016-02-02 11:45:02 +0800402 pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
403 x86_cap_flag(df->feature), df->level);
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800404 }
Yinghai Luf6db44d2009-02-14 23:59:18 -0800405}
H. Peter Anvinb38b0662009-01-23 17:20:50 -0800406
407/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * Naming convention should be: <Name> [(<Codename>)]
409 * This table only is used unless init_<vendor>() below doesn't set it;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100410 * in particular, if CPUID levels 0x80000002..4 are supported, this
411 * isn't used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
413
414/* Look up CPU names by table lookup. */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400415static const char *table_lookup_model(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Jan Beulich09dc68d2013-10-21 09:35:20 +0100417#ifdef CONFIG_X86_32
418 const struct legacy_cpu_model_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (c->x86_model >= 16)
421 return NULL; /* Range check */
422
423 if (!this_cpu)
424 return NULL;
425
Jan Beulich09dc68d2013-10-21 09:35:20 +0100426 info = this_cpu->legacy_models;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Jan Beulich09dc68d2013-10-21 09:35:20 +0100428 while (info->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (info->family == c->x86)
430 return info->model_names[c->x86_model];
431 info++;
432 }
Jan Beulich09dc68d2013-10-21 09:35:20 +0100433#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return NULL; /* Not found */
435}
436
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400437__u32 cpu_caps_cleared[NCAPINTS];
438__u32 cpu_caps_set[NCAPINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900440void load_percpu_segment(int cpu)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200441{
Yinghai Lufab334c2008-09-04 20:09:05 -0700442#ifdef CONFIG_X86_32
Brian Gerst2697fbd2009-01-27 12:56:48 +0900443 loadsegment(fs, __KERNEL_PERCPU);
444#else
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700445 __loadsegment_simple(gs, 0);
Brian Gerst2697fbd2009-01-27 12:56:48 +0900446 wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
Yinghai Lufab334c2008-09-04 20:09:05 -0700447#endif
Tejun Heo60a53172009-02-09 22:17:40 +0900448 load_stack_canary_segment();
Yinghai Lu9d31d352008-09-04 21:09:44 +0200449}
450
Thomas Garnier45fc8752017-03-14 10:05:08 -0700451/*
452 * On 64-bit the GDT remapping is read-only.
453 * A global is used for Xen to change the default when required.
454 */
455#ifdef CONFIG_X86_64
456pgprot_t pg_fixmap_gdt_flags = PAGE_KERNEL_RO;
457#else
Thomas Garnier69218e42017-03-14 10:05:07 -0700458pgprot_t pg_fixmap_gdt_flags = PAGE_KERNEL;
Thomas Garnier45fc8752017-03-14 10:05:08 -0700459#endif
Thomas Garnier69218e42017-03-14 10:05:07 -0700460
461/* Setup the fixmap mapping only once per-processor */
462static inline void setup_fixmap_gdt(int cpu)
463{
464 __set_fixmap(get_cpu_gdt_ro_index(cpu),
465 __pa(get_cpu_gdt_rw(cpu)), pg_fixmap_gdt_flags);
466}
467
Thomas Garnier45fc8752017-03-14 10:05:08 -0700468/* Load the original GDT from the per-cpu structure */
469void load_direct_gdt(int cpu)
470{
471 struct desc_ptr gdt_descr;
472
473 gdt_descr.address = (long)get_cpu_gdt_rw(cpu);
474 gdt_descr.size = GDT_SIZE - 1;
475 load_gdt(&gdt_descr);
476}
477EXPORT_SYMBOL_GPL(load_direct_gdt);
478
Thomas Garnier69218e42017-03-14 10:05:07 -0700479/* Load a fixmap remapping of the per-cpu GDT */
480void load_fixmap_gdt(int cpu)
481{
482 struct desc_ptr gdt_descr;
483
484 gdt_descr.address = (long)get_cpu_gdt_ro(cpu);
485 gdt_descr.size = GDT_SIZE - 1;
486 load_gdt(&gdt_descr);
487}
Thomas Garnier45fc8752017-03-14 10:05:08 -0700488EXPORT_SYMBOL_GPL(load_fixmap_gdt);
Thomas Garnier69218e42017-03-14 10:05:07 -0700489
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100490/*
491 * Current gdt points %fs at the "master" per-cpu area: after this,
492 * it's on the real one.
493 */
Brian Gerst552be872009-01-30 17:47:53 +0900494void switch_to_new_gdt(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Thomas Garnier45fc8752017-03-14 10:05:08 -0700496 /* Load the original GDT */
497 load_direct_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /* Reload the per-cpu base */
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900499 load_percpu_segment(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400502static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400504static void get_model_name(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 unsigned int *v;
Borislav Petkovee098e12015-06-01 12:06:57 +0200507 char *p, *q, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Yinghai Lu3da99c92008-09-04 21:09:44 +0200509 if (c->extended_cpuid_level < 0x80000004)
Yinghai Lu1b05d602008-09-06 01:52:27 -0700510 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100512 v = (unsigned int *)c->x86_model_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
514 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
515 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
516 c->x86_model_id[48] = 0;
517
Borislav Petkovee098e12015-06-01 12:06:57 +0200518 /* Trim whitespace */
519 p = q = s = &c->x86_model_id[0];
520
521 while (*p == ' ')
522 p++;
523
524 while (*p) {
525 /* Note the last non-whitespace index */
526 if (!isspace(*p))
527 s = q;
528
529 *q++ = *p++;
530 }
531
532 *(s + 1) = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400535void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Yinghai Lu9d31d352008-09-04 21:09:44 +0200537 unsigned int n, dummy, ebx, ecx, edx, l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Yinghai Lu3da99c92008-09-04 21:09:44 +0200539 n = c->extended_cpuid_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (n >= 0x80000005) {
Yinghai Lu9d31d352008-09-04 21:09:44 +0200542 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
Yinghai Lu9d31d352008-09-04 21:09:44 +0200543 c->x86_cache_size = (ecx>>24) + (edx>>24);
Yinghai Lu140fc722008-09-04 20:09:07 -0700544#ifdef CONFIG_X86_64
545 /* On K8 L1 TLB is inclusive, so don't count it */
546 c->x86_tlbsize = 0;
547#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
550 if (n < 0x80000006) /* Some chips just has a large L1. */
551 return;
552
Yinghai Lu0a488a52008-09-04 21:09:47 +0200553 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 l2size = ecx >> 16;
555
Yinghai Lu140fc722008-09-04 20:09:07 -0700556#ifdef CONFIG_X86_64
557 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
558#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* do processor-specific cache resizing */
Jan Beulich09dc68d2013-10-21 09:35:20 +0100560 if (this_cpu->legacy_cache_size)
561 l2size = this_cpu->legacy_cache_size(c, l2size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /* Allow user to override all this if necessary. */
564 if (cachesize_override != -1)
565 l2size = cachesize_override;
566
567 if (l2size == 0)
568 return; /* Again, no L2 cache is possible */
Yinghai Lu140fc722008-09-04 20:09:07 -0700569#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 c->x86_cache_size = l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Alex Shie0ba94f2012-06-28 09:02:16 +0800574u16 __read_mostly tlb_lli_4k[NR_INFO];
575u16 __read_mostly tlb_lli_2m[NR_INFO];
576u16 __read_mostly tlb_lli_4m[NR_INFO];
577u16 __read_mostly tlb_lld_4k[NR_INFO];
578u16 __read_mostly tlb_lld_2m[NR_INFO];
579u16 __read_mostly tlb_lld_4m[NR_INFO];
Kirill A. Shutemovdd360392013-12-23 14:16:58 +0200580u16 __read_mostly tlb_lld_1g[NR_INFO];
Alex Shie0ba94f2012-06-28 09:02:16 +0800581
Steven Honeymanf94fe112014-11-05 22:52:18 +0000582static void cpu_detect_tlb(struct cpuinfo_x86 *c)
Alex Shie0ba94f2012-06-28 09:02:16 +0800583{
584 if (this_cpu->c_detect_tlb)
585 this_cpu->c_detect_tlb(c);
586
Steven Honeymanf94fe112014-11-05 22:52:18 +0000587 pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
Alex Shie0ba94f2012-06-28 09:02:16 +0800588 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
Steven Honeymanf94fe112014-11-05 22:52:18 +0000589 tlb_lli_4m[ENTRIES]);
590
591 pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
592 tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
593 tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
Alex Shie0ba94f2012-06-28 09:02:16 +0800594}
595
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400596void detect_ht(struct cpuinfo_x86 *c)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200597{
Borislav Petkovc8e56d22015-06-04 18:55:25 +0200598#ifdef CONFIG_SMP
Yinghai Lu0a488a52008-09-04 21:09:47 +0200599 u32 eax, ebx, ecx, edx;
600 int index_msb, core_bits;
Mike Travis2eaad1f2009-12-10 17:19:36 -0800601 static bool printed;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200602
603 if (!cpu_has(c, X86_FEATURE_HT))
604 return;
605
606 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
607 goto out;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200608
Yinghai Lu1cd78772008-09-04 20:09:08 -0700609 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
610 return;
611
Yinghai Lu9d31d352008-09-04 21:09:44 +0200612 cpuid(1, &eax, &ebx, &ecx, &edx);
613
Yinghai Lu9d31d352008-09-04 21:09:44 +0200614 smp_num_siblings = (ebx & 0xff0000) >> 16;
615
616 if (smp_num_siblings == 1) {
Chen Yucong1b74dde2016-02-02 11:45:02 +0800617 pr_info_once("CPU0: Hyper-Threading is disabled\n");
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100618 goto out;
Yinghai Lu0a488a52008-09-04 21:09:47 +0200619 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200620
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100621 if (smp_num_siblings <= 1)
622 goto out;
623
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100624 index_msb = get_count_order(smp_num_siblings);
625 c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
626
627 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
628
629 index_msb = get_count_order(smp_num_siblings);
630
631 core_bits = get_count_order(c->x86_max_cores);
632
633 c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
634 ((1 << core_bits) - 1);
635
Yinghai Lu0a488a52008-09-04 21:09:47 +0200636out:
Mike Travis2eaad1f2009-12-10 17:19:36 -0800637 if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
Chen Yucong1b74dde2016-02-02 11:45:02 +0800638 pr_info("CPU: Physical Processor ID: %d\n",
639 c->phys_proc_id);
640 pr_info("CPU: Processor Core ID: %d\n",
641 c->cpu_core_id);
Mike Travis2eaad1f2009-12-10 17:19:36 -0800642 printed = 1;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200643 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200644#endif
Yinghai Lu97e4db72008-09-04 20:08:59 -0700645}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400647static void get_cpu_vendor(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 char *v = c->x86_vendor_id;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100650 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 for (i = 0; i < X86_VENDOR_NUM; i++) {
Yinghai Lu10a434f2008-09-04 21:09:45 +0200653 if (!cpu_devs[i])
654 break;
655
656 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
657 (cpu_devs[i]->c_ident[1] &&
658 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100659
Yinghai Lu10a434f2008-09-04 21:09:45 +0200660 this_cpu = cpu_devs[i];
661 c->x86_vendor = this_cpu->c_x86_vendor;
662 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664 }
Yinghai Lu10a434f2008-09-04 21:09:45 +0200665
Chen Yucong1b74dde2016-02-02 11:45:02 +0800666 pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \
667 "CPU: Your system may be unstable.\n", v);
Yinghai Lu10a434f2008-09-04 21:09:45 +0200668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 c->x86_vendor = X86_VENDOR_UNKNOWN;
670 this_cpu = &default_cpu;
671}
672
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400673void cpu_detect(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* Get vendor name */
Harvey Harrison4a148512008-02-01 17:49:43 +0100676 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
677 (unsigned int *)&c->x86_vendor_id[0],
678 (unsigned int *)&c->x86_vendor_id[8],
679 (unsigned int *)&c->x86_vendor_id[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 c->x86 = 4;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200682 /* Intel-defined flags: level 0x00000001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (c->cpuid_level >= 0x00000001) {
684 u32 junk, tfms, cap0, misc;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
Borislav Petkov99f925c2015-11-23 11:12:21 +0100687 c->x86 = x86_family(tfms);
688 c->x86_model = x86_model(tfms);
689 c->x86_mask = x86_stepping(tfms);
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100690
Huang, Yingd4387bd2008-01-31 22:05:45 +0100691 if (cap0 & (1<<19)) {
Huang, Yingd4387bd2008-01-31 22:05:45 +0100692 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200693 c->x86_cache_alignment = c->x86_clflush_size;
Huang, Yingd4387bd2008-01-31 22:05:45 +0100694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
Yinghai Lu3da99c92008-09-04 21:09:44 +0200697
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -0800698static void apply_forced_caps(struct cpuinfo_x86 *c)
699{
700 int i;
701
702 for (i = 0; i < NCAPINTS; i++) {
703 c->x86_capability[i] &= ~cpu_caps_cleared[i];
704 c->x86_capability[i] |= cpu_caps_set[i];
705 }
706}
707
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400708void get_cpu_cap(struct cpuinfo_x86 *c)
Yinghai Lu093af8d2008-01-30 13:33:32 +0100709{
Borislav Petkov39c06df2015-12-07 10:39:40 +0100710 u32 eax, ebx, ecx, edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100711
Yinghai Lu3da99c92008-09-04 21:09:44 +0200712 /* Intel-defined flags: level 0x00000001 */
713 if (c->cpuid_level >= 0x00000001) {
Borislav Petkov39c06df2015-12-07 10:39:40 +0100714 cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100715
Borislav Petkov39c06df2015-12-07 10:39:40 +0100716 c->x86_capability[CPUID_1_ECX] = ecx;
717 c->x86_capability[CPUID_1_EDX] = edx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100718 }
719
Andy Lutomirski3df8d9202016-12-15 10:14:42 -0800720 /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
721 if (c->cpuid_level >= 0x00000006)
722 c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
723
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700724 /* Additional Intel-defined flags: level 0x00000007 */
725 if (c->cpuid_level >= 0x00000007) {
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700726 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100727 c->x86_capability[CPUID_7_0_EBX] = ebx;
Dave Hansendfb4a702016-02-12 13:02:01 -0800728 c->x86_capability[CPUID_7_ECX] = ecx;
H. Peter Anvinbdc802d2010-07-07 17:29:18 -0700729 }
730
Fenghua Yu6229ad22014-05-29 11:12:30 -0700731 /* Extended state features: level 0x0000000d */
732 if (c->cpuid_level >= 0x0000000d) {
Fenghua Yu6229ad22014-05-29 11:12:30 -0700733 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
734
Borislav Petkov39c06df2015-12-07 10:39:40 +0100735 c->x86_capability[CPUID_D_1_EAX] = eax;
Fenghua Yu6229ad22014-05-29 11:12:30 -0700736 }
737
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000738 /* Additional Intel-defined flags: level 0x0000000F */
739 if (c->cpuid_level >= 0x0000000F) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000740
741 /* QoS sub-leaf, EAX=0Fh, ECX=0 */
742 cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100743 c->x86_capability[CPUID_F_0_EDX] = edx;
744
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000745 if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
746 /* will be overridden if occupancy monitoring exists */
747 c->x86_cache_max_rmid = ebx;
748
749 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
750 cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
Borislav Petkov39c06df2015-12-07 10:39:40 +0100751 c->x86_capability[CPUID_F_1_EDX] = edx;
752
Vikas Shivappa33c3cc72016-03-10 15:32:09 -0800753 if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) ||
754 ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) ||
755 (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +0000756 c->x86_cache_max_rmid = ecx;
757 c->x86_cache_occ_scale = ebx;
758 }
759 } else {
760 c->x86_cache_max_rmid = -1;
761 c->x86_cache_occ_scale = -1;
762 }
763 }
764
Yinghai Lu3da99c92008-09-04 21:09:44 +0200765 /* AMD-defined flags: level 0x80000001 */
Borislav Petkov39c06df2015-12-07 10:39:40 +0100766 eax = cpuid_eax(0x80000000);
767 c->extended_cpuid_level = eax;
Ingo Molnar0f3fa482009-03-14 08:46:17 +0100768
Borislav Petkov39c06df2015-12-07 10:39:40 +0100769 if ((eax & 0xffff0000) == 0x80000000) {
770 if (eax >= 0x80000001) {
771 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
772
773 c->x86_capability[CPUID_8000_0001_ECX] = ecx;
774 c->x86_capability[CPUID_8000_0001_EDX] = edx;
Yinghai Lu3da99c92008-09-04 21:09:44 +0200775 }
776 }
Yinghai Lu5122c892008-09-04 20:09:09 -0700777
Yazen Ghannam71faad42016-05-11 14:58:26 +0200778 if (c->extended_cpuid_level >= 0x80000007) {
779 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
780
781 c->x86_capability[CPUID_8000_0007_EBX] = ebx;
782 c->x86_power = edx;
783 }
784
Yinghai Lu5122c892008-09-04 20:09:09 -0700785 if (c->extended_cpuid_level >= 0x80000008) {
Borislav Petkov39c06df2015-12-07 10:39:40 +0100786 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
Yinghai Lu5122c892008-09-04 20:09:09 -0700787
788 c->x86_virt_bits = (eax >> 8) & 0xff;
789 c->x86_phys_bits = eax & 0xff;
Borislav Petkov39c06df2015-12-07 10:39:40 +0100790 c->x86_capability[CPUID_8000_0008_EBX] = ebx;
Yinghai Lu5122c892008-09-04 20:09:09 -0700791 }
Jan Beulich13c6c532009-03-12 12:37:34 +0000792#ifdef CONFIG_X86_32
793 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
794 c->x86_phys_bits = 36;
Yinghai Lu5122c892008-09-04 20:09:09 -0700795#endif
Yinghai Lue3224232008-09-06 01:52:28 -0700796
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100797 if (c->extended_cpuid_level >= 0x8000000a)
Borislav Petkov39c06df2015-12-07 10:39:40 +0100798 c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
Borislav Petkov2ccd71f2015-12-07 10:39:39 +0100799
Jacob Pan1dedefd2010-05-19 12:01:23 -0700800 init_scattered_cpuid_features(c);
Andy Lutomirski60d34502017-01-18 11:15:39 -0800801
802 /*
803 * Clear/Set all flags overridden by options, after probe.
804 * This needs to happen each time we re-probe, which may happen
805 * several times during CPU initialization.
806 */
807 apply_forced_caps(c);
Yinghai Lu093af8d2008-01-30 13:33:32 +0100808}
Yinghai Luaef93c82008-09-14 02:33:15 -0700809
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400810static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
Yinghai Luaef93c82008-09-14 02:33:15 -0700811{
812#ifdef CONFIG_X86_32
813 int i;
814
815 /*
816 * First of all, decide if this is a 486 or higher
817 * It's a 486 if we can modify the AC flag
818 */
819 if (flag_is_changeable_p(X86_EFLAGS_AC))
820 c->x86 = 4;
821 else
822 c->x86 = 3;
823
824 for (i = 0; i < X86_VENDOR_NUM; i++)
825 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
826 c->x86_vendor_id[0] = 0;
827 cpu_devs[i]->c_identify(c);
828 if (c->x86_vendor_id[0]) {
829 get_cpu_vendor(c);
830 break;
831 }
832 }
833#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100836/*
837 * Do minimum CPU detection early.
838 * Fields really needed: vendor, cpuid_level, family, model, mask,
839 * cache alignment.
840 * The others are not touched to avoid unwanted side effects.
841 *
842 * WARNING: this function is only called on the BP. Don't add code here
843 * that is supposed to run on all CPUs.
844 */
Yinghai Lu3da99c92008-09-04 21:09:44 +0200845static void __init early_identify_cpu(struct cpuinfo_x86 *c)
Rusty Russelld7cd5612006-12-07 02:14:08 +0100846{
Yinghai Lu6627d242008-09-04 20:09:10 -0700847#ifdef CONFIG_X86_64
848 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +0000849 c->x86_phys_bits = 36;
850 c->x86_virt_bits = 48;
Yinghai Lu6627d242008-09-04 20:09:10 -0700851#else
Huang, Yingd4387bd2008-01-31 22:05:45 +0100852 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +0000853 c->x86_phys_bits = 32;
854 c->x86_virt_bits = 32;
Yinghai Lu6627d242008-09-04 20:09:10 -0700855#endif
Yinghai Lu0a488a52008-09-04 21:09:47 +0200856 c->x86_cache_alignment = c->x86_clflush_size;
Rusty Russelld7cd5612006-12-07 02:14:08 +0100857
Yinghai Lu3da99c92008-09-04 21:09:44 +0200858 memset(&c->x86_capability, 0, sizeof c->x86_capability);
Yinghai Lu0a488a52008-09-04 21:09:47 +0200859 c->extended_cpuid_level = 0;
860
Yinghai Luaef93c82008-09-14 02:33:15 -0700861 /* cyrix could have cpuid enabled via c_identify()*/
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700862 if (have_cpuid_p()) {
863 cpu_detect(c);
864 get_cpu_vendor(c);
865 get_cpu_cap(c);
Borislav Petkov78d1b29682017-01-18 11:15:37 -0800866 setup_force_cpu_cap(X86_FEATURE_CPUID);
Rusty Russelld7cd5612006-12-07 02:14:08 +0100867
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700868 if (this_cpu->c_early_init)
869 this_cpu->c_early_init(c);
Krzysztof Helt12cf1052008-09-04 21:09:43 +0200870
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700871 c->cpu_index = 0;
872 filter_cpuid_features(c, false);
Yinghai Lu3da99c92008-09-04 21:09:44 +0200873
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700874 if (this_cpu->c_bsp_init)
875 this_cpu->c_bsp_init(c);
Borislav Petkov78d1b29682017-01-18 11:15:37 -0800876 } else {
877 identify_cpu_without_cpuid(c);
878 setup_clear_cpu_cap(X86_FEATURE_CPUID);
Andy Lutomirski05fb3c12016-09-28 16:06:33 -0700879 }
Borislav Petkovc3b83592013-06-09 12:07:30 +0200880
881 setup_force_cpu_cap(X86_FEATURE_ALWAYS);
Ingo Molnardb52ef72015-06-27 10:25:14 +0200882 fpu__init_system(c);
Rusty Russelld7cd5612006-12-07 02:14:08 +0100883}
884
Yinghai Lu9d31d352008-09-04 21:09:44 +0200885void __init early_cpu_init(void)
886{
Jan Beulich02dde8b2009-03-12 12:08:49 +0000887 const struct cpu_dev *const *cdev;
Yinghai Lu10a434f2008-09-04 21:09:45 +0200888 int count = 0;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200889
Jan Beulichac23f252011-03-04 15:52:35 +0000890#ifdef CONFIG_PROCESSOR_SELECT
Chen Yucong1b74dde2016-02-02 11:45:02 +0800891 pr_info("KERNEL supported cpus:\n");
Ingo Molnar31c997c2009-11-14 10:34:41 +0100892#endif
893
Yinghai Lu10a434f2008-09-04 21:09:45 +0200894 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
Jan Beulich02dde8b2009-03-12 12:08:49 +0000895 const struct cpu_dev *cpudev = *cdev;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200896
Yinghai Lu10a434f2008-09-04 21:09:45 +0200897 if (count >= X86_VENDOR_NUM)
898 break;
899 cpu_devs[count] = cpudev;
900 count++;
901
Jan Beulichac23f252011-03-04 15:52:35 +0000902#ifdef CONFIG_PROCESSOR_SELECT
Ingo Molnar31c997c2009-11-14 10:34:41 +0100903 {
904 unsigned int j;
Yinghai Lu10a434f2008-09-04 21:09:45 +0200905
Ingo Molnar31c997c2009-11-14 10:34:41 +0100906 for (j = 0; j < 2; j++) {
907 if (!cpudev->c_ident[j])
908 continue;
Chen Yucong1b74dde2016-02-02 11:45:02 +0800909 pr_info(" %s %s\n", cpudev->c_vendor,
Ingo Molnar31c997c2009-11-14 10:34:41 +0100910 cpudev->c_ident[j]);
911 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200912 }
Dave Jones03884232009-11-13 15:30:00 -0500913#endif
Ingo Molnar31c997c2009-11-14 10:34:41 +0100914 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200915 early_identify_cpu(&boot_cpu_data);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800916}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700918/*
Borislav Petkov366d4a42010-10-04 09:31:27 +0200919 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
920 * unfortunately, that's not true in practice because of early VIA
921 * chips and (more importantly) broken virtualizers that are not easy
922 * to detect. In the latter case it doesn't even *fail* reliably, so
923 * probing for it doesn't even work. Disable it completely on 32-bit
H. Peter Anvinba0593b2008-09-16 09:29:40 -0700924 * unless we can find a reliable way to detect all the broken cases.
Borislav Petkov366d4a42010-10-04 09:31:27 +0200925 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700926 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400927static void detect_nopl(struct cpuinfo_x86 *c)
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700928{
Borislav Petkov366d4a42010-10-04 09:31:27 +0200929#ifdef CONFIG_X86_32
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700930 clear_cpu_cap(c, X86_FEATURE_NOPL);
Borislav Petkov366d4a42010-10-04 09:31:27 +0200931#else
932 set_cpu_cap(c, X86_FEATURE_NOPL);
933#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Andy Lutomirski7a5d67042016-04-07 17:31:46 -0700936static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
937{
938#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /*
Andy Lutomirski7a5d67042016-04-07 17:31:46 -0700940 * Empirically, writing zero to a segment selector on AMD does
941 * not clear the base, whereas writing zero to a segment
942 * selector on Intel does clear the base. Intel's behavior
943 * allows slightly faster context switches in the common case
944 * where GS is unused by the prev and next threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 *
Andy Lutomirski7a5d67042016-04-07 17:31:46 -0700946 * Since neither vendor documents this anywhere that I can see,
947 * detect it directly instead of hardcoding the choice by
948 * vendor.
949 *
950 * I've designated AMD's behavior as the "bug" because it's
951 * counterintuitive and less friendly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
Andy Lutomirski7a5d67042016-04-07 17:31:46 -0700953
954 unsigned long old_base, tmp;
955 rdmsrl(MSR_FS_BASE, old_base);
956 wrmsrl(MSR_FS_BASE, 1);
957 loadsegment(fs, 0);
958 rdmsrl(MSR_FS_BASE, tmp);
959 if (tmp != 0)
960 set_cpu_bug(c, X86_BUG_NULL_SEG);
961 wrmsrl(MSR_FS_BASE, old_base);
Yinghai Lu3da99c92008-09-04 21:09:44 +0200962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
Yinghai Luaef93c82008-09-14 02:33:15 -0700964
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400965static void generic_identify(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 c->extended_cpuid_level = 0;
968
Yinghai Luaef93c82008-09-14 02:33:15 -0700969 if (!have_cpuid_p())
970 identify_cpu_without_cpuid(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100971
Yinghai Luaef93c82008-09-14 02:33:15 -0700972 /* cyrix could have cpuid enabled via c_identify()*/
Ingo Molnara9853dd2008-09-14 14:46:58 +0200973 if (!have_cpuid_p())
Yinghai Luaef93c82008-09-14 02:33:15 -0700974 return;
975
Yinghai Lu3da99c92008-09-04 21:09:44 +0200976 cpu_detect(c);
977
978 get_cpu_vendor(c);
979
980 get_cpu_cap(c);
981
982 if (c->cpuid_level >= 0x00000001) {
983 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
Yinghai Lub89d3b32008-09-04 20:09:12 -0700984#ifdef CONFIG_X86_32
Borislav Petkovc8e56d22015-06-04 18:55:25 +0200985# ifdef CONFIG_SMP
Ingo Molnarcb8cc442009-01-28 13:24:54 +0100986 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lub89d3b32008-09-04 20:09:12 -0700987# else
Yinghai Lu3da99c92008-09-04 21:09:44 +0200988 c->apicid = c->initial_apicid;
Yinghai Lub89d3b32008-09-04 20:09:12 -0700989# endif
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800990#endif
Yinghai Lub89d3b32008-09-04 20:09:12 -0700991 c->phys_proc_id = c->initial_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Yinghai Lu3da99c92008-09-04 21:09:44 +0200993
Yinghai Lu1b05d602008-09-06 01:52:27 -0700994 get_model_name(c); /* Default name */
Yinghai Lu3da99c92008-09-04 21:09:44 +0200995
Yinghai Lu3da99c92008-09-04 21:09:44 +0200996 detect_nopl(c);
Andy Lutomirski7a5d67042016-04-07 17:31:46 -0700997
998 detect_null_seg_behavior(c);
Andy Lutomirski0230bb02016-04-07 17:31:48 -0700999
1000 /*
1001 * ESPFIX is a strange bug. All real CPUs have it. Paravirt
1002 * systems that run Linux at CPL > 0 may or may not have the
1003 * issue, but, even if they have the issue, there's absolutely
1004 * nothing we can do about it because we can't use the real IRET
1005 * instruction.
1006 *
1007 * NB: For the time being, only 32-bit kernels support
1008 * X86_BUG_ESPFIX as such. 64-bit kernels directly choose
1009 * whether to apply espfix using paravirt hooks. If any
1010 * non-paravirt system ever shows up that does *not* have the
1011 * ESPFIX issue, we can change this.
1012 */
1013#ifdef CONFIG_X86_32
1014# ifdef CONFIG_PARAVIRT
1015 do {
1016 extern void native_iret(void);
1017 if (pv_cpu_ops.iret == native_iret)
1018 set_cpu_bug(c, X86_BUG_ESPFIX);
1019 } while (0);
1020# else
1021 set_cpu_bug(c, X86_BUG_ESPFIX);
1022# endif
1023#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001026static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1027{
1028 /*
1029 * The heavy lifting of max_rmid and cache_occ_scale are handled
1030 * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
1031 * in case CQM bits really aren't there in this CPU.
1032 */
1033 if (c != &boot_cpu_data) {
1034 boot_cpu_data.x86_cache_max_rmid =
1035 min(boot_cpu_data.x86_cache_max_rmid,
1036 c->x86_cache_max_rmid);
1037 }
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/*
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001041 * Validate that ACPI/mptables have the same information about the
1042 * effective APIC id and update the package map.
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001043 */
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001044static void validate_apic_and_package_id(struct cpuinfo_x86 *c)
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001045{
1046#ifdef CONFIG_SMP
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001047 unsigned int apicid, cpu = smp_processor_id();
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001048
1049 apicid = apic->cpu_present_to_apicid(cpu);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001050
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001051 if (apicid != c->apicid) {
1052 pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n",
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001053 cpu, apicid, c->initial_apicid);
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001054 }
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001055 BUG_ON(topology_update_package_map(c->phys_proc_id, cpu));
Thomas Gleixnerd49597f2016-11-09 16:35:51 +01001056#else
1057 c->logical_proc_id = 0;
1058#endif
1059}
1060
1061/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 * This does the hard work of actually picking apart the CPU stuff...
1063 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001064static void identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 int i;
1067
1068 c->loops_per_jiffy = loops_per_jiffy;
1069 c->x86_cache_size = -1;
1070 c->x86_vendor = X86_VENDOR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 c->x86_model = c->x86_mask = 0; /* So far unknown... */
1072 c->x86_vendor_id[0] = '\0'; /* Unset */
1073 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001074 c->x86_max_cores = 1;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001075 c->x86_coreid_bits = 0;
Borislav Petkov79a8b9a2017-02-05 11:50:21 +01001076 c->cu_id = 0xff;
Yinghai Lu11fdd252008-09-07 17:58:50 -07001077#ifdef CONFIG_X86_64
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001078 c->x86_clflush_size = 64;
Jan Beulich13c6c532009-03-12 12:37:34 +00001079 c->x86_phys_bits = 36;
1080 c->x86_virt_bits = 48;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001081#else
1082 c->cpuid_level = -1; /* CPUID not detected */
Andi Kleen770d1322006-12-07 02:14:05 +01001083 c->x86_clflush_size = 32;
Jan Beulich13c6c532009-03-12 12:37:34 +00001084 c->x86_phys_bits = 32;
1085 c->x86_virt_bits = 32;
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001086#endif
1087 c->x86_cache_alignment = c->x86_clflush_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 memset(&c->x86_capability, 0, sizeof c->x86_capability);
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 generic_identify(c);
1091
Andi Kleen38985342008-01-30 13:32:49 +01001092 if (this_cpu->c_identify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 this_cpu->c_identify(c);
1094
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001095 /* Clear/Set all flags overridden by options, after probe */
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -08001096 apply_forced_caps(c);
Yinghai Lu2759c322009-05-15 13:05:16 -07001097
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001098#ifdef CONFIG_X86_64
Ingo Molnarcb8cc442009-01-28 13:24:54 +01001099 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001100#endif
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /*
1103 * Vendor-specific initialization. In this section we
1104 * canonicalize the feature flags, meaning if there are
1105 * features a certain CPU supports which CPUID doesn't
1106 * tell us, CPUID claiming incorrect flags, or other bugs,
1107 * we handle them here.
1108 *
1109 * At the end of this section, c->x86_capability better
1110 * indicate the features this CPU genuinely supports!
1111 */
1112 if (this_cpu->c_init)
1113 this_cpu->c_init(c);
1114
1115 /* Disable the PN if appropriate */
1116 squash_the_stupid_serial_number(c);
1117
H. Peter Anvinb2cc2a02012-09-26 18:02:28 -07001118 /* Set up SMEP/SMAP */
1119 setup_smep(c);
1120 setup_smap(c);
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /*
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001123 * The vendor-specific functions might have changed features.
1124 * Now we do "generic changes."
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
1126
H. Peter Anvinb38b0662009-01-23 17:20:50 -08001127 /* Filter out anything that depends on CPUID levels we don't have */
1128 filter_cpuid_features(c, true);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* If the model name is still unset, do table lookup. */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001131 if (!c->x86_model_id[0]) {
Jan Beulich02dde8b2009-03-12 12:08:49 +00001132 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 p = table_lookup_model(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001134 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 strcpy(c->x86_model_id, p);
1136 else
1137 /* Last resort... */
1138 sprintf(c->x86_model_id, "%02x/%02x",
Chuck Ebbert54a20f82006-03-23 02:59:36 -08001139 c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001142#ifdef CONFIG_X86_64
1143 detect_ht(c);
1144#endif
1145
Alok Kataria88b094f2008-10-27 10:41:46 -07001146 init_hypervisor(c);
H. Peter Anvin49d859d2011-07-31 14:02:19 -07001147 x86_init_rdrand(c);
Peter P Waskiewicz Jrcbc82b12015-01-23 18:45:43 +00001148 x86_init_cache_qos(c);
Dave Hansen06976942016-02-12 13:02:29 -08001149 setup_pku(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001150
1151 /*
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001152 * Clear/Set all flags overridden by options, need do it
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001153 * before following smp all cpus cap AND.
1154 */
Andy Lutomirski8bf1ebc2017-01-18 11:15:38 -08001155 apply_forced_caps(c);
Yinghai Lu3e0c3732009-05-09 23:47:42 -07001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /*
1158 * On SMP, boot_cpu_data holds the common feature set between
1159 * all CPUs; so make sure that we indicate which features are
1160 * common between the CPUs. The first time this routine gets
1161 * executed, c == &boot_cpu_data.
1162 */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001163 if (c != &boot_cpu_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /* AND the already accumulated flags with these */
Yinghai Lu9d31d352008-09-04 21:09:44 +02001165 for (i = 0; i < NCAPINTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
Borislav Petkov65fc9852013-03-20 15:07:23 +01001167
1168 /* OR, i.e. replicate the bug flags */
1169 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
1170 c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
1173 /* Init Machine Check Exception if available. */
Borislav Petkov5e099542009-10-16 12:31:32 +02001174 mcheck_cpu_init(c);
Andi Kleen30d432d2008-01-30 13:33:16 +01001175
1176 select_idle_routine(c);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001177
Tejun Heode2d9442011-01-23 14:37:41 +01001178#ifdef CONFIG_NUMA
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001179 numa_add_cpu(smp_processor_id());
1180#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001181}
Shaohua Li31ab2692005-11-07 00:58:42 -08001182
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001183/*
1184 * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
1185 * on 32-bit kernels:
1186 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001187#ifdef CONFIG_X86_32
1188void enable_sep_cpu(void)
1189{
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001190 struct tss_struct *tss;
1191 int cpu;
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001192
Borislav Petkovb3edfda2016-03-16 13:19:29 +01001193 if (!boot_cpu_has(X86_FEATURE_SEP))
1194 return;
1195
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001196 cpu = get_cpu();
1197 tss = &per_cpu(cpu_tss, cpu);
1198
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001199 /*
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001200 * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1201 * see the big comment in struct x86_hw_tss's definition.
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001202 */
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001203
1204 tss->x86_tss.ss1 = __KERNEL_CS;
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001205 wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
1206
Andy Lutomirskicf9328c2015-04-02 12:41:45 -07001207 wrmsr(MSR_IA32_SYSENTER_ESP,
1208 (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
1209 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001210
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001211 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
Ingo Molnar8b6c0ab2015-03-16 10:32:20 +01001212
Andy Lutomirskicfda7bb2014-05-05 12:19:33 -07001213 put_cpu();
1214}
Glauber Costae04d6452008-09-22 14:35:08 -03001215#endif
1216
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001217void __init identify_boot_cpu(void)
1218{
1219 identify_cpu(&boot_cpu_data);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001220#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001221 sysenter_setup();
Li Shaohua6fe940d2005-06-25 14:54:53 -07001222 enable_sep_cpu();
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001223#endif
Borislav Petkov5b5563322012-08-06 19:00:37 +02001224 cpu_detect_tlb(&boot_cpu_data);
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001225}
Shaohua Li3b520b22005-07-07 17:56:38 -07001226
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001227void identify_secondary_cpu(struct cpuinfo_x86 *c)
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001228{
1229 BUG_ON(c == &boot_cpu_data);
1230 identify_cpu(c);
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001231#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001232 enable_sep_cpu();
Yinghai Lu102bbe3a2008-09-04 20:09:13 -07001233#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +02001234 mtrr_ap_init();
Thomas Gleixner9d85eb92016-12-12 11:04:53 +01001235 validate_apic_and_package_id(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Andi Kleen191679f2008-01-30 13:33:21 +01001238static __init int setup_noclflush(char *arg)
1239{
H. Peter Anvin840d2832014-02-27 08:31:30 -08001240 setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
H. Peter Anvinda4aaa72014-02-27 08:36:31 -08001241 setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
Andi Kleen191679f2008-01-30 13:33:21 +01001242 return 1;
1243}
1244__setup("noclflush", setup_noclflush);
1245
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001246void print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Jan Beulich02dde8b2009-03-12 12:08:49 +00001248 const char *vendor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001250 if (c->x86_vendor < X86_VENDOR_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 vendor = this_cpu->c_vendor;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001252 } else {
1253 if (c->cpuid_level >= 0)
1254 vendor = c->x86_vendor_id;
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Yinghai Lubd32a8cf2008-09-19 18:41:16 -07001257 if (vendor && !strstr(c->x86_model_id, vendor))
Chen Yucong1b74dde2016-02-02 11:45:02 +08001258 pr_cont("%s ", vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Yinghai Lu9d31d352008-09-04 21:09:44 +02001260 if (c->x86_model_id[0])
Chen Yucong1b74dde2016-02-02 11:45:02 +08001261 pr_cont("%s", c->x86_model_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001263 pr_cont("%d86", c->x86);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Chen Yucong1b74dde2016-02-02 11:45:02 +08001265 pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
Borislav Petkov924e1012012-09-14 18:37:46 +02001266
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001267 if (c->x86_mask || c->cpuid_level >= 0)
Chen Yucong1b74dde2016-02-02 11:45:02 +08001268 pr_cont(", stepping: 0x%x)\n", c->x86_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 else
Chen Yucong1b74dde2016-02-02 11:45:02 +08001270 pr_cont(")\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Andi Kleenac72e782008-01-30 13:33:21 +01001273static __init int setup_disablecpuid(char *arg)
1274{
1275 int bit;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001276
Lukasz Odziobadd853fd2016-12-28 14:55:40 +01001277 if (get_option(&arg, &bit) && bit >= 0 && bit < NCAPINTS * 32)
Andi Kleenac72e782008-01-30 13:33:21 +01001278 setup_clear_cpu_cap(bit);
1279 else
1280 return 0;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001281
Andi Kleenac72e782008-01-30 13:33:21 +01001282 return 1;
1283}
1284__setup("clearcpuid=", setup_disablecpuid);
1285
Yinghai Lud5494d42008-09-04 20:09:03 -07001286#ifdef CONFIG_X86_64
Kees Cook404f6aa2016-08-08 16:29:06 -07001287struct desc_ptr idt_descr __ro_after_init = {
1288 .size = NR_VECTORS * 16 - 1,
1289 .address = (unsigned long) idt_table,
1290};
1291const struct desc_ptr debug_idt_descr = {
1292 .size = NR_VECTORS * 16 - 1,
1293 .address = (unsigned long) debug_idt_table,
1294};
Yinghai Lud5494d42008-09-04 20:09:03 -07001295
Brian Gerst947e76c2009-01-19 12:21:28 +09001296DEFINE_PER_CPU_FIRST(union irq_stack_union,
Andi Kleen277d5b42013-08-05 15:02:43 -07001297 irq_stack_union) __aligned(PAGE_SIZE) __visible;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001298
Tejun Heobdf977b2009-08-03 14:12:19 +09001299/*
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001300 * The following percpu variables are hot. Align current_task to
1301 * cacheline size such that they fall in the same cacheline.
Tejun Heobdf977b2009-08-03 14:12:19 +09001302 */
1303DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1304 &init_task;
1305EXPORT_PER_CPU_SYMBOL(current_task);
Yinghai Lud5494d42008-09-04 20:09:03 -07001306
Tejun Heobdf977b2009-08-03 14:12:19 +09001307DEFINE_PER_CPU(char *, irq_stack_ptr) =
Josh Poimboeuf4950d6d2016-08-18 10:59:08 -05001308 init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE;
Tejun Heobdf977b2009-08-03 14:12:19 +09001309
Andi Kleen277d5b42013-08-05 15:02:43 -07001310DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
Yinghai Lud5494d42008-09-04 20:09:03 -07001311
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001312DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1313EXPORT_PER_CPU_SYMBOL(__preempt_count);
1314
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001315/*
1316 * Special IST stacks which the CPU switches to when it calls
1317 * an IST-marked descriptor entry. Up to 7 stacks (hardware
1318 * limit), all of them are 4K, except the debug stack which
1319 * is 8K.
1320 */
1321static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
1322 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
1323 [DEBUG_STACK - 1] = DEBUG_STKSZ
1324};
1325
Brian Gerst92d65b22009-01-19 00:38:58 +09001326static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
Tejun Heo3e352aa2009-08-03 14:10:11 +09001327 [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
Yinghai Lud5494d42008-09-04 20:09:03 -07001328
Yinghai Lud5494d42008-09-04 20:09:03 -07001329/* May not be marked __init: used by software suspend */
1330void syscall_init(void)
1331{
Borislav Petkov31ac34c2015-11-23 11:12:25 +01001332 wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
Andy Lutomirski47edb652015-07-23 12:14:40 -07001333 wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001334
1335#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski47edb652015-07-23 12:14:40 -07001336 wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001337 /*
Denys Vlasenko487d1ed2015-03-27 11:59:16 +01001338 * This only works on Intel CPUs.
1339 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1340 * This does not cause SYSENTER to jump to the wrong location, because
1341 * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
Denys Vlasenkoa76c7f42015-03-22 20:48:14 +01001342 */
1343 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1344 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
Ingo Molnar4c8cd0c2015-06-08 08:33:56 +02001345 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001346#else
Andy Lutomirski47edb652015-07-23 12:14:40 -07001347 wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
Borislav Petkov6b513112015-04-03 14:25:28 +02001348 wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
Ingo Molnard56fe4b2015-03-24 14:41:37 +01001349 wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1350 wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
Yinghai Lud5494d42008-09-04 20:09:03 -07001351#endif
1352
1353 /* Flags to clear on syscall */
1354 wrmsrl(MSR_SYSCALL_MASK,
H. Peter Anvin63bcff22012-09-21 12:43:12 -07001355 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
Andy Lutomirski8c7aa692014-10-01 11:49:04 -07001356 X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
Yinghai Lud5494d42008-09-04 20:09:03 -07001357}
1358
Yinghai Lud5494d42008-09-04 20:09:03 -07001359/*
1360 * Copies of the original ist values from the tss are only accessed during
1361 * debugging, no special alignment required.
1362 */
1363DEFINE_PER_CPU(struct orig_ist, orig_ist);
1364
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001365static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
Steven Rostedt42181182011-12-16 11:43:02 -05001366DEFINE_PER_CPU(int, debug_stack_usage);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001367
1368int is_debug_stack(unsigned long addr)
1369{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001370 return __this_cpu_read(debug_stack_usage) ||
1371 (addr <= __this_cpu_read(debug_stack_addr) &&
1372 addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001373}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001374NOKPROBE_SYMBOL(is_debug_stack);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001375
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001376DEFINE_PER_CPU(u32, debug_idt_ctr);
Steven Rostedtf8988172012-05-30 11:47:00 -04001377
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001378void debug_stack_set_zero(void)
1379{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001380 this_cpu_inc(debug_idt_ctr);
1381 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001382}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001383NOKPROBE_SYMBOL(debug_stack_set_zero);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001384
1385void debug_stack_reset(void)
1386{
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001387 if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
Steven Rostedtf8988172012-05-30 11:47:00 -04001388 return;
Seiji Aguchi629f4f92013-06-20 11:45:44 -04001389 if (this_cpu_dec_return(debug_idt_ctr) == 0)
1390 load_current_idt();
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001391}
Masami Hiramatsu0f46efeb2014-04-17 17:17:12 +09001392NOKPROBE_SYMBOL(debug_stack_reset);
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001393
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001394#else /* CONFIG_X86_64 */
Yinghai Lud5494d42008-09-04 20:09:03 -07001395
Tejun Heobdf977b2009-08-03 14:12:19 +09001396DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1397EXPORT_PER_CPU_SYMBOL(current_task);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02001398DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1399EXPORT_PER_CPU_SYMBOL(__preempt_count);
Tejun Heobdf977b2009-08-03 14:12:19 +09001400
Andy Lutomirskia7fcf282015-03-06 17:50:19 -08001401/*
1402 * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1403 * the top of the kernel stack. Use an extra percpu variable to track the
1404 * top of the kernel stack directly.
1405 */
1406DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1407 (unsigned long)&init_thread_union + THREAD_SIZE;
1408EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1409
Tejun Heo60a53172009-02-09 22:17:40 +09001410#ifdef CONFIG_CC_STACKPROTECTOR
Jeremy Fitzhardinge53f82452009-09-03 14:31:44 -07001411DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
Tejun Heo60a53172009-02-09 22:17:40 +09001412#endif
1413
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001414#endif /* CONFIG_X86_64 */
Jeremy Fitzhardingec5413fb2007-05-02 19:27:16 +02001415
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001416/*
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301417 * Clear all 6 debug registers:
1418 */
1419static void clear_all_debug_regs(void)
1420{
1421 int i;
1422
1423 for (i = 0; i < 8; i++) {
1424 /* Ignore db4, db5 */
1425 if ((i == 4) || (i == 5))
1426 continue;
1427
1428 set_debugreg(0, i);
1429 }
1430}
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +01001431
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001432#ifdef CONFIG_KGDB
1433/*
1434 * Restore debug regs if using kgdbwait and you have a kernel debugger
1435 * connection established.
1436 */
1437static void dbg_restore_debug_regs(void)
1438{
1439 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1440 arch_kgdb_ops.correct_hw_break();
1441}
1442#else /* ! CONFIG_KGDB */
1443#define dbg_restore_debug_regs()
1444#endif /* ! CONFIG_KGDB */
1445
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001446static void wait_for_master_cpu(int cpu)
1447{
1448#ifdef CONFIG_SMP
1449 /*
1450 * wait for ACK from master CPU before continuing
1451 * with AP initialization
1452 */
1453 WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1454 while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1455 cpu_relax();
1456#endif
1457}
1458
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001459/*
1460 * cpu_init() initializes state that is per-CPU. Some data is already
1461 * initialized (naturally) in the bootstrap process, such as the GDT
1462 * and IDT. We reload them nevertheless, this function acts as a
1463 * 'CPU state barrier', nothing should get across.
Yinghai Lu1ba76582008-09-04 20:09:04 -07001464 * A lot of state is already set up in PDA init for 64 bit
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001465 */
Yinghai Lu1ba76582008-09-04 20:09:04 -07001466#ifdef CONFIG_X86_64
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001467
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001468void cpu_init(void)
Yinghai Lu1ba76582008-09-04 20:09:04 -07001469{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001470 struct orig_ist *oist;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001471 struct task_struct *me;
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001472 struct tss_struct *t;
1473 unsigned long v;
Andy Lutomirskifb598312016-07-14 13:22:58 -07001474 int cpu = raw_smp_processor_id();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001475 int i;
1476
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001477 wait_for_master_cpu(cpu);
1478
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001479 /*
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001480 * Initialize the CR4 shadow before doing anything that could
1481 * try to read it.
1482 */
1483 cr4_init_shadow();
1484
Borislav Petkov777284b2016-10-25 11:55:11 +02001485 if (cpu)
1486 load_ucode_ap();
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001487
Andy Lutomirski24933b82015-03-05 19:19:05 -08001488 t = &per_cpu(cpu_tss, cpu);
Tejun Heo0fe1e002009-10-29 22:34:14 +09001489 oist = &per_cpu(orig_ist, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001490
Brian Gerste7a22c12009-01-19 00:38:59 +09001491#ifdef CONFIG_NUMA
Fenghua Yu27fd1852012-11-13 11:32:47 -08001492 if (this_cpu_read(numa_node) == 0 &&
Lee Schermerhorne534c7c2010-05-26 14:44:58 -07001493 early_cpu_to_node(cpu) != NUMA_NO_NODE)
1494 set_numa_node(early_cpu_to_node(cpu));
Brian Gerste7a22c12009-01-19 00:38:59 +09001495#endif
Yinghai Lu1ba76582008-09-04 20:09:04 -07001496
1497 me = current;
1498
Mike Travis2eaad1f2009-12-10 17:19:36 -08001499 pr_debug("Initializing CPU#%d\n", cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001500
Andy Lutomirski375074c2014-10-24 15:58:07 -07001501 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001502
1503 /*
1504 * Initialize the per-CPU GDT with the boot GDT,
1505 * and set up the GDT descriptor:
1506 */
1507
Brian Gerst552be872009-01-30 17:47:53 +09001508 switch_to_new_gdt(cpu);
Brian Gerst2697fbd2009-01-27 12:56:48 +09001509 loadsegment(fs, 0);
1510
Seiji Aguchicf910e82013-06-20 11:46:53 -04001511 load_current_idt();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001512
1513 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1514 syscall_init();
1515
1516 wrmsrl(MSR_FS_BASE, 0);
1517 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1518 barrier();
1519
H. Peter Anvin4763ed42009-11-13 15:28:16 -08001520 x86_configure_nx();
Thomas Gleixner659006b2015-01-15 21:22:26 +00001521 x2apic_setup();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001522
1523 /*
1524 * set up and load the per-CPU TSS
1525 */
Tejun Heo0fe1e002009-10-29 22:34:14 +09001526 if (!oist->ist[0]) {
Brian Gerst92d65b22009-01-19 00:38:58 +09001527 char *estacks = per_cpu(exception_stacks, cpu);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001528
Yinghai Lu1ba76582008-09-04 20:09:04 -07001529 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001530 estacks += exception_stack_sizes[v];
Tejun Heo0fe1e002009-10-29 22:34:14 +09001531 oist->ist[v] = t->x86_tss.ist[v] =
Yinghai Lu1ba76582008-09-04 20:09:04 -07001532 (unsigned long)estacks;
Steven Rostedt228bdaa2011-12-09 03:02:19 -05001533 if (v == DEBUG_STACK-1)
1534 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
Yinghai Lu1ba76582008-09-04 20:09:04 -07001535 }
1536 }
1537
1538 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
Ingo Molnar0f3fa482009-03-14 08:46:17 +01001539
Yinghai Lu1ba76582008-09-04 20:09:04 -07001540 /*
1541 * <= is required because the CPU will access up to
1542 * 8 bits beyond the end of the IO permission bitmap.
1543 */
1544 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1545 t->io_bitmap[i] = ~0UL;
1546
Vegard Nossumf1f10072017-02-27 14:30:07 -08001547 mmgrab(&init_mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001548 me->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001549 BUG_ON(me->mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001550 enter_lazy_tlb(&init_mm, me);
1551
1552 load_sp0(t, &current->thread);
1553 set_tss_desc(cpu, t);
1554 load_TR_desc();
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001555 load_mm_ldt(&init_mm);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001556
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001557 clear_all_debug_regs();
1558 dbg_restore_debug_regs();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001559
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001560 fpu__init_cpu();
Yinghai Lu1ba76582008-09-04 20:09:04 -07001561
Yinghai Lu1ba76582008-09-04 20:09:04 -07001562 if (is_uv_system())
1563 uv_cpu_init();
Thomas Garnier69218e42017-03-14 10:05:07 -07001564
1565 setup_fixmap_gdt(cpu);
1566 load_fixmap_gdt(cpu);
Yinghai Lu1ba76582008-09-04 20:09:04 -07001567}
1568
1569#else
1570
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001571void cpu_init(void)
James Bottomley9ee79a32007-01-22 09:18:31 -06001572{
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001573 int cpu = smp_processor_id();
1574 struct task_struct *curr = current;
Andy Lutomirski24933b82015-03-05 19:19:05 -08001575 struct tss_struct *t = &per_cpu(cpu_tss, cpu);
James Bottomley9ee79a32007-01-22 09:18:31 -06001576 struct thread_struct *thread = &curr->thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001578 wait_for_master_cpu(cpu);
Fenghua Yue6ebf5d2012-12-20 23:44:24 -08001579
Steven Rostedt5b2bdbc2015-02-27 14:50:19 -05001580 /*
1581 * Initialize the CR4 shadow before doing anything that could
1582 * try to read it.
1583 */
1584 cr4_init_shadow();
1585
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001586 show_ucode_info_early();
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001587
Chen Yucong1b74dde2016-02-02 11:45:02 +08001588 pr_info("Initializing CPU#%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Borislav Petkov362f9242015-12-07 10:39:41 +01001590 if (cpu_feature_enabled(X86_FEATURE_VME) ||
Borislav Petkov59e21e32016-04-04 22:24:59 +02001591 boot_cpu_has(X86_FEATURE_TSC) ||
Borislav Petkov362f9242015-12-07 10:39:41 +01001592 boot_cpu_has(X86_FEATURE_DE))
Andy Lutomirski375074c2014-10-24 15:58:07 -07001593 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Seiji Aguchicf910e82013-06-20 11:46:53 -04001595 load_current_idt();
Brian Gerst552be872009-01-30 17:47:53 +09001596 switch_to_new_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 * Set up and load the per-CPU TSS and LDT
1600 */
Vegard Nossumf1f10072017-02-27 14:30:07 -08001601 mmgrab(&init_mm);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001602 curr->active_mm = &init_mm;
Stoyan Gaydarov8c5dfd22009-03-10 00:10:32 -05001603 BUG_ON(curr->mm);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001604 enter_lazy_tlb(&init_mm, curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
H. Peter Anvinfaca6222008-01-30 13:31:02 +01001606 load_sp0(t, thread);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001607 set_tss_desc(cpu, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 load_TR_desc();
Andy Lutomirski37868fe2015-07-30 14:31:32 -07001609 load_mm_ldt(&init_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Thomas Gleixnerf9a196b2009-05-01 20:59:25 +02001611 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1612
Matt Mackall22c4e302006-01-08 01:05:24 -08001613#ifdef CONFIG_DOUBLEFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /* Set up doublefault TSS pointer in the GDT */
1615 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
Matt Mackall22c4e302006-01-08 01:05:24 -08001616#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +05301618 clear_all_debug_regs();
Jason Wessel0bb9fef2010-05-20 21:04:30 -05001619 dbg_restore_debug_regs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Ingo Molnar21c4cd12015-04-26 14:27:17 +02001621 fpu__init_cpu();
Thomas Garnier69218e42017-03-14 10:05:07 -07001622
1623 setup_fixmap_gdt(cpu);
1624 load_fixmap_gdt(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
Yinghai Lu1ba76582008-09-04 20:09:04 -07001626#endif
Borislav Petkov5700f742013-06-09 12:07:32 +02001627
Laura Abbottb51ef522015-07-20 14:47:58 -07001628static void bsp_resume(void)
1629{
1630 if (this_cpu->c_bsp_resume)
1631 this_cpu->c_bsp_resume(&boot_cpu_data);
1632}
1633
1634static struct syscore_ops cpu_syscore_ops = {
1635 .resume = bsp_resume,
1636};
1637
1638static int __init init_cpu_syscore(void)
1639{
1640 register_syscore_ops(&cpu_syscore_ops);
1641 return 0;
1642}
1643core_initcall(init_cpu_syscore);