]>
Commit | Line | Data |
---|---|---|
f0fc4aff | 1 | #include <linux/bootmem.h> |
9766cdbc | 2 | #include <linux/linkage.h> |
f0fc4aff | 3 | #include <linux/bitops.h> |
9766cdbc | 4 | #include <linux/kernel.h> |
186f4360 | 5 | #include <linux/export.h> |
9766cdbc JSR |
6 | #include <linux/percpu.h> |
7 | #include <linux/string.h> | |
ee098e1a | 8 | #include <linux/ctype.h> |
1da177e4 | 9 | #include <linux/delay.h> |
68e21be2 | 10 | #include <linux/sched/mm.h> |
e6017571 | 11 | #include <linux/sched/clock.h> |
9164bb4a | 12 | #include <linux/sched/task.h> |
9766cdbc | 13 | #include <linux/init.h> |
0f46efeb | 14 | #include <linux/kprobes.h> |
9766cdbc | 15 | #include <linux/kgdb.h> |
1da177e4 | 16 | #include <linux/smp.h> |
9766cdbc | 17 | #include <linux/io.h> |
b51ef52d | 18 | #include <linux/syscore_ops.h> |
9766cdbc JSR |
19 | |
20 | #include <asm/stackprotector.h> | |
cdd6c482 | 21 | #include <asm/perf_event.h> |
1da177e4 | 22 | #include <asm/mmu_context.h> |
49d859d7 | 23 | #include <asm/archrandom.h> |
9766cdbc JSR |
24 | #include <asm/hypervisor.h> |
25 | #include <asm/processor.h> | |
1e02ce4c | 26 | #include <asm/tlbflush.h> |
f649e938 | 27 | #include <asm/debugreg.h> |
9766cdbc | 28 | #include <asm/sections.h> |
f40c3300 | 29 | #include <asm/vsyscall.h> |
8bdbd962 AC |
30 | #include <linux/topology.h> |
31 | #include <linux/cpumask.h> | |
9766cdbc | 32 | #include <asm/pgtable.h> |
60063497 | 33 | #include <linux/atomic.h> |
9766cdbc JSR |
34 | #include <asm/proto.h> |
35 | #include <asm/setup.h> | |
36 | #include <asm/apic.h> | |
37 | #include <asm/desc.h> | |
78f7f1e5 | 38 | #include <asm/fpu/internal.h> |
27b07da7 | 39 | #include <asm/mtrr.h> |
0274f955 | 40 | #include <asm/hwcap2.h> |
8bdbd962 | 41 | #include <linux/numa.h> |
9766cdbc | 42 | #include <asm/asm.h> |
0f6ff2bc | 43 | #include <asm/bugs.h> |
9766cdbc | 44 | #include <asm/cpu.h> |
a03a3e28 | 45 | #include <asm/mce.h> |
9766cdbc | 46 | #include <asm/msr.h> |
8d4a4300 | 47 | #include <asm/pat.h> |
d288e1cf FY |
48 | #include <asm/microcode.h> |
49 | #include <asm/microcode_intel.h> | |
e641f5f5 IM |
50 | |
51 | #ifdef CONFIG_X86_LOCAL_APIC | |
bdbcdd48 | 52 | #include <asm/uv/uv.h> |
1da177e4 LT |
53 | #endif |
54 | ||
55 | #include "cpu.h" | |
56 | ||
0274f955 GA |
57 | u32 elf_hwcap2 __read_mostly; |
58 | ||
c2d1cec1 | 59 | /* all of these masks are initialized in setup_cpu_local_masks() */ |
c2d1cec1 | 60 | cpumask_var_t cpu_initialized_mask; |
9766cdbc JSR |
61 | cpumask_var_t cpu_callout_mask; |
62 | cpumask_var_t cpu_callin_mask; | |
c2d1cec1 MT |
63 | |
64 | /* representing cpus for which sibling maps can be computed */ | |
65 | cpumask_var_t cpu_sibling_setup_mask; | |
66 | ||
2f2f52ba | 67 | /* correctly size the local cpu masks */ |
4369f1fb | 68 | void __init setup_cpu_local_masks(void) |
2f2f52ba BG |
69 | { |
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 | ||
148f9bb8 | 76 | static void default_init(struct cpuinfo_x86 *c) |
e8055139 OZ |
77 | { |
78 | #ifdef CONFIG_X86_64 | |
27c13ece | 79 | cpu_detect_cache_sizes(c); |
e8055139 OZ |
80 | #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 | ||
148f9bb8 | 93 | static const struct cpu_dev default_cpu = { |
e8055139 OZ |
94 | .c_init = default_init, |
95 | .c_vendor = "Unknown", | |
96 | .c_x86_vendor = X86_VENDOR_UNKNOWN, | |
97 | }; | |
98 | ||
148f9bb8 | 99 | static const struct cpu_dev *this_cpu = &default_cpu; |
0a488a53 | 100 | |
06deef89 | 101 | DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = { |
950ad7ff | 102 | #ifdef CONFIG_X86_64 |
06deef89 BG |
103 | /* |
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 | * | |
9766cdbc | 108 | * TLS descriptors are currently at a different place compared to i386. |
06deef89 BG |
109 | * Hopefully nobody expects them at a fixed place (Wine?) |
110 | */ | |
1e5de182 AM |
111 | [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), | |
950ad7ff | 117 | #else |
1e5de182 AM |
118 | [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), | |
bf504672 RR |
122 | /* |
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 | */ | |
6842ef0e | 127 | /* 32-bit code */ |
1e5de182 | 128 | [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), |
6842ef0e | 129 | /* 16-bit code */ |
1e5de182 | 130 | [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), |
6842ef0e | 131 | /* 16-bit data */ |
1e5de182 | 132 | [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff), |
6842ef0e | 133 | /* 16-bit data */ |
1e5de182 | 134 | [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0), |
6842ef0e | 135 | /* 16-bit data */ |
1e5de182 | 136 | [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0), |
bf504672 RR |
137 | /* |
138 | * The APM segments have byte granularity and their bases | |
139 | * are set at run time. All have 64k limits. | |
140 | */ | |
6842ef0e | 141 | /* 32-bit code */ |
1e5de182 | 142 | [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), |
bf504672 | 143 | /* 16-bit code */ |
1e5de182 | 144 | [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), |
6842ef0e | 145 | /* data */ |
72c4d853 | 146 | [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff), |
bf504672 | 147 | |
1e5de182 AM |
148 | [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), |
149 | [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), | |
60a5317f | 150 | GDT_STACK_CANARY_INIT |
950ad7ff | 151 | #endif |
06deef89 | 152 | } }; |
7a61d35d | 153 | EXPORT_PER_CPU_SYMBOL_GPL(gdt_page); |
ae1ee11b | 154 | |
8c3641e9 | 155 | static int __init x86_mpx_setup(char *s) |
0c752a93 | 156 | { |
8c3641e9 | 157 | /* require an exact match without trailing characters */ |
2cd3949f DH |
158 | if (strlen(s)) |
159 | return 0; | |
0c752a93 | 160 | |
8c3641e9 DH |
161 | /* do not emit a message if the feature is not present */ |
162 | if (!boot_cpu_has(X86_FEATURE_MPX)) | |
163 | return 1; | |
6bad06b7 | 164 | |
8c3641e9 DH |
165 | setup_clear_cpu_cap(X86_FEATURE_MPX); |
166 | pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n"); | |
b6f42a4a FY |
167 | return 1; |
168 | } | |
8c3641e9 | 169 | __setup("nompx", x86_mpx_setup); |
b6f42a4a | 170 | |
62d3a636 | 171 | #ifdef CONFIG_X86_64 |
0e6a37a4 | 172 | static int __init x86_nopcid_setup(char *s) |
62d3a636 | 173 | { |
0e6a37a4 AL |
174 | /* nopcid doesn't accept parameters */ |
175 | if (s) | |
176 | return -EINVAL; | |
62d3a636 AL |
177 | |
178 | /* do not emit a message if the feature is not present */ | |
179 | if (!boot_cpu_has(X86_FEATURE_PCID)) | |
0e6a37a4 | 180 | return 0; |
62d3a636 AL |
181 | |
182 | setup_clear_cpu_cap(X86_FEATURE_PCID); | |
183 | pr_info("nopcid: PCID feature disabled\n"); | |
0e6a37a4 | 184 | return 0; |
62d3a636 | 185 | } |
0e6a37a4 | 186 | early_param("nopcid", x86_nopcid_setup); |
62d3a636 AL |
187 | #endif |
188 | ||
d12a72b8 AL |
189 | static 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 | } | |
203 | early_param("noinvpcid", x86_noinvpcid_setup); | |
204 | ||
ba51dced | 205 | #ifdef CONFIG_X86_32 |
148f9bb8 PG |
206 | static int cachesize_override = -1; |
207 | static int disable_x86_serial_nr = 1; | |
1da177e4 | 208 | |
0a488a53 YL |
209 | static int __init cachesize_setup(char *str) |
210 | { | |
211 | get_option(&str, &cachesize_override); | |
212 | return 1; | |
213 | } | |
214 | __setup("cachesize=", cachesize_setup); | |
215 | ||
0a488a53 YL |
216 | static int __init x86_sep_setup(char *s) |
217 | { | |
218 | setup_clear_cpu_cap(X86_FEATURE_SEP); | |
219 | return 1; | |
220 | } | |
221 | __setup("nosep", x86_sep_setup); | |
222 | ||
223 | /* Standard macro to see if a specific flag is changeable */ | |
224 | static inline int flag_is_changeable_p(u32 flag) | |
225 | { | |
226 | u32 f1, f2; | |
227 | ||
94f6bac1 KH |
228 | /* |
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 | */ | |
0f3fa48a IM |
235 | 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 | ||
94f6bac1 KH |
246 | : "=&r" (f1), "=&r" (f2) |
247 | : "ir" (flag)); | |
0a488a53 YL |
248 | |
249 | return ((f1^f2) & flag) != 0; | |
250 | } | |
251 | ||
252 | /* Probe for the CPUID instruction */ | |
148f9bb8 | 253 | int have_cpuid_p(void) |
0a488a53 YL |
254 | { |
255 | return flag_is_changeable_p(X86_EFLAGS_ID); | |
256 | } | |
257 | ||
148f9bb8 | 258 | static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) |
0a488a53 | 259 | { |
0f3fa48a IM |
260 | unsigned long lo, hi; |
261 | ||
262 | 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 | ||
1b74dde7 | 271 | pr_notice("CPU serial number disabled.\n"); |
0f3fa48a IM |
272 | 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); | |
0a488a53 YL |
276 | } |
277 | ||
278 | static 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); | |
ba51dced | 284 | #else |
102bbe3a YL |
285 | static inline int flag_is_changeable_p(u32 flag) |
286 | { | |
287 | return 1; | |
288 | } | |
102bbe3a YL |
289 | static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) |
290 | { | |
291 | } | |
ba51dced | 292 | #endif |
0a488a53 | 293 | |
de5397ad FY |
294 | static __init int setup_disable_smep(char *arg) |
295 | { | |
b2cc2a07 | 296 | setup_clear_cpu_cap(X86_FEATURE_SMEP); |
0f6ff2bc DH |
297 | /* Check for things that depend on SMEP being enabled: */ |
298 | check_mpx_erratum(&boot_cpu_data); | |
de5397ad FY |
299 | return 1; |
300 | } | |
301 | __setup("nosmep", setup_disable_smep); | |
302 | ||
b2cc2a07 | 303 | static __always_inline void setup_smep(struct cpuinfo_x86 *c) |
de5397ad | 304 | { |
b2cc2a07 | 305 | if (cpu_has(c, X86_FEATURE_SMEP)) |
375074cc | 306 | cr4_set_bits(X86_CR4_SMEP); |
de5397ad FY |
307 | } |
308 | ||
52b6179a PA |
309 | static __init int setup_disable_smap(char *arg) |
310 | { | |
b2cc2a07 | 311 | setup_clear_cpu_cap(X86_FEATURE_SMAP); |
52b6179a PA |
312 | return 1; |
313 | } | |
314 | __setup("nosmap", setup_disable_smap); | |
315 | ||
b2cc2a07 PA |
316 | static __always_inline void setup_smap(struct cpuinfo_x86 *c) |
317 | { | |
581b7f15 | 318 | unsigned long eflags = native_save_fl(); |
b2cc2a07 PA |
319 | |
320 | /* This should have been cleared long ago */ | |
b2cc2a07 PA |
321 | BUG_ON(eflags & X86_EFLAGS_AC); |
322 | ||
03bbd596 PA |
323 | if (cpu_has(c, X86_FEATURE_SMAP)) { |
324 | #ifdef CONFIG_X86_SMAP | |
375074cc | 325 | cr4_set_bits(X86_CR4_SMAP); |
03bbd596 | 326 | #else |
375074cc | 327 | cr4_clear_bits(X86_CR4_SMAP); |
03bbd596 PA |
328 | #endif |
329 | } | |
de5397ad FY |
330 | } |
331 | ||
06976945 DH |
332 | /* |
333 | * Protection Keys are not available in 32-bit mode. | |
334 | */ | |
335 | static bool pku_disabled; | |
336 | ||
337 | static __always_inline void setup_pku(struct cpuinfo_x86 *c) | |
338 | { | |
e8df1a95 DH |
339 | /* check the boot processor, plus compile options for PKU: */ |
340 | if (!cpu_feature_enabled(X86_FEATURE_PKU)) | |
341 | return; | |
342 | /* checks the actual processor's cpuid bits: */ | |
06976945 DH |
343 | if (!cpu_has(c, X86_FEATURE_PKU)) |
344 | return; | |
345 | if (pku_disabled) | |
346 | return; | |
347 | ||
348 | cr4_set_bits(X86_CR4_PKE); | |
349 | /* | |
350 | * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE | |
351 | * cpuid bit to be set. We need to ensure that we | |
352 | * update that bit in this CPU's "cpu_info". | |
353 | */ | |
354 | get_cpu_cap(c); | |
355 | } | |
356 | ||
357 | #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS | |
358 | static __init int setup_disable_pku(char *arg) | |
359 | { | |
360 | /* | |
361 | * Do not clear the X86_FEATURE_PKU bit. All of the | |
362 | * runtime checks are against OSPKE so clearing the | |
363 | * bit does nothing. | |
364 | * | |
365 | * This way, we will see "pku" in cpuinfo, but not | |
366 | * "ospke", which is exactly what we want. It shows | |
367 | * that the CPU has PKU, but the OS has not enabled it. | |
368 | * This happens to be exactly how a system would look | |
369 | * if we disabled the config option. | |
370 | */ | |
371 | pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n"); | |
372 | pku_disabled = true; | |
373 | return 1; | |
374 | } | |
375 | __setup("nopku", setup_disable_pku); | |
376 | #endif /* CONFIG_X86_64 */ | |
377 | ||
b38b0665 PA |
378 | /* |
379 | * Some CPU features depend on higher CPUID levels, which may not always | |
380 | * be available due to CPUID level capping or broken virtualization | |
381 | * software. Add those features to this table to auto-disable them. | |
382 | */ | |
383 | struct cpuid_dependent_feature { | |
384 | u32 feature; | |
385 | u32 level; | |
386 | }; | |
0f3fa48a | 387 | |
148f9bb8 | 388 | static const struct cpuid_dependent_feature |
b38b0665 PA |
389 | cpuid_dependent_features[] = { |
390 | { X86_FEATURE_MWAIT, 0x00000005 }, | |
391 | { X86_FEATURE_DCA, 0x00000009 }, | |
392 | { X86_FEATURE_XSAVE, 0x0000000d }, | |
393 | { 0, 0 } | |
394 | }; | |
395 | ||
148f9bb8 | 396 | static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) |
b38b0665 PA |
397 | { |
398 | const struct cpuid_dependent_feature *df; | |
9766cdbc | 399 | |
b38b0665 | 400 | for (df = cpuid_dependent_features; df->feature; df++) { |
0f3fa48a IM |
401 | |
402 | if (!cpu_has(c, df->feature)) | |
403 | continue; | |
b38b0665 PA |
404 | /* |
405 | * Note: cpuid_level is set to -1 if unavailable, but | |
406 | * extended_extended_level is set to 0 if unavailable | |
407 | * and the legitimate extended levels are all negative | |
408 | * when signed; hence the weird messing around with | |
409 | * signs here... | |
410 | */ | |
0f3fa48a | 411 | if (!((s32)df->level < 0 ? |
f6db44df | 412 | (u32)df->level > (u32)c->extended_cpuid_level : |
0f3fa48a IM |
413 | (s32)df->level > (s32)c->cpuid_level)) |
414 | continue; | |
415 | ||
416 | clear_cpu_cap(c, df->feature); | |
417 | if (!warn) | |
418 | continue; | |
419 | ||
1b74dde7 CY |
420 | pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n", |
421 | x86_cap_flag(df->feature), df->level); | |
b38b0665 | 422 | } |
f6db44df | 423 | } |
b38b0665 | 424 | |
102bbe3a YL |
425 | /* |
426 | * Naming convention should be: <Name> [(<Codename>)] | |
427 | * This table only is used unless init_<vendor>() below doesn't set it; | |
0f3fa48a IM |
428 | * in particular, if CPUID levels 0x80000002..4 are supported, this |
429 | * isn't used | |
102bbe3a YL |
430 | */ |
431 | ||
432 | /* Look up CPU names by table lookup. */ | |
148f9bb8 | 433 | static const char *table_lookup_model(struct cpuinfo_x86 *c) |
102bbe3a | 434 | { |
09dc68d9 JB |
435 | #ifdef CONFIG_X86_32 |
436 | const struct legacy_cpu_model_info *info; | |
102bbe3a YL |
437 | |
438 | if (c->x86_model >= 16) | |
439 | return NULL; /* Range check */ | |
440 | ||
441 | if (!this_cpu) | |
442 | return NULL; | |
443 | ||
09dc68d9 | 444 | info = this_cpu->legacy_models; |
102bbe3a | 445 | |
09dc68d9 | 446 | while (info->family) { |
102bbe3a YL |
447 | if (info->family == c->x86) |
448 | return info->model_names[c->x86_model]; | |
449 | info++; | |
450 | } | |
09dc68d9 | 451 | #endif |
102bbe3a YL |
452 | return NULL; /* Not found */ |
453 | } | |
454 | ||
148f9bb8 PG |
455 | __u32 cpu_caps_cleared[NCAPINTS]; |
456 | __u32 cpu_caps_set[NCAPINTS]; | |
7d851c8d | 457 | |
11e3a840 JF |
458 | void load_percpu_segment(int cpu) |
459 | { | |
460 | #ifdef CONFIG_X86_32 | |
461 | loadsegment(fs, __KERNEL_PERCPU); | |
462 | #else | |
45e876f7 | 463 | __loadsegment_simple(gs, 0); |
11e3a840 JF |
464 | wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu)); |
465 | #endif | |
60a5317f | 466 | load_stack_canary_segment(); |
11e3a840 JF |
467 | } |
468 | ||
475b37e7 AL |
469 | static void set_percpu_fixmap_pages(int fixmap_index, void *ptr, |
470 | int pages, pgprot_t prot) | |
471 | { | |
472 | int i; | |
473 | ||
474 | for (i = 0; i < pages; i++) { | |
475 | __set_fixmap(fixmap_index - i, | |
476 | per_cpu_ptr_to_phys(ptr + i * PAGE_SIZE), prot); | |
477 | } | |
478 | } | |
479 | ||
480 | #ifdef CONFIG_X86_32 | |
481 | /* The 32-bit entry code needs to find cpu_entry_area. */ | |
482 | DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area); | |
483 | #endif | |
484 | ||
b17894f1 AL |
485 | /* Setup the fixmap mappings only once per-processor */ |
486 | static inline void setup_cpu_entry_area(int cpu) | |
b23adb7d | 487 | { |
45fc8757 | 488 | #ifdef CONFIG_X86_64 |
9fec5954 AL |
489 | extern char _entry_trampoline[]; |
490 | ||
b23adb7d | 491 | /* On 64-bit systems, we use a read-only fixmap GDT. */ |
b17894f1 | 492 | pgprot_t gdt_prot = PAGE_KERNEL_RO; |
45fc8757 | 493 | #else |
b23adb7d AL |
494 | /* |
495 | * On native 32-bit systems, the GDT cannot be read-only because | |
496 | * our double fault handler uses a task gate, and entering through | |
497 | * a task gate needs to change an available TSS to busy. If the GDT | |
498 | * is read-only, that will triple fault. | |
499 | * | |
500 | * On Xen PV, the GDT must be read-only because the hypervisor requires | |
501 | * it. | |
502 | */ | |
b17894f1 | 503 | pgprot_t gdt_prot = boot_cpu_has(X86_FEATURE_XENPV) ? |
b23adb7d | 504 | PAGE_KERNEL_RO : PAGE_KERNEL; |
45fc8757 | 505 | #endif |
69218e47 | 506 | |
b17894f1 | 507 | __set_fixmap(get_cpu_entry_area_index(cpu, gdt), get_cpu_gdt_paddr(cpu), gdt_prot); |
57d6cfd9 AL |
508 | |
509 | /* | |
510 | * The Intel SDM says (Volume 3, 7.2.1): | |
511 | * | |
512 | * Avoid placing a page boundary in the part of the TSS that the | |
513 | * processor reads during a task switch (the first 104 bytes). The | |
514 | * processor may not correctly perform address translations if a | |
515 | * boundary occurs in this area. During a task switch, the processor | |
516 | * reads and writes into the first 104 bytes of each TSS (using | |
517 | * contiguous physical addresses beginning with the physical address | |
518 | * of the first byte of the TSS). So, after TSS access begins, if | |
519 | * part of the 104 bytes is not physically contiguous, the processor | |
520 | * will access incorrect information without generating a page-fault | |
521 | * exception. | |
522 | * | |
523 | * There are also a lot of errata involving the TSS spanning a page | |
524 | * boundary. Assert that we're not doing that. | |
525 | */ | |
526 | BUILD_BUG_ON((offsetof(struct tss_struct, x86_tss) ^ | |
527 | offsetofend(struct tss_struct, x86_tss)) & PAGE_MASK); | |
475b37e7 AL |
528 | BUILD_BUG_ON(sizeof(struct tss_struct) % PAGE_SIZE != 0); |
529 | set_percpu_fixmap_pages(get_cpu_entry_area_index(cpu, tss), | |
530 | &per_cpu(cpu_tss, cpu), | |
531 | sizeof(struct tss_struct) / PAGE_SIZE, | |
532 | PAGE_KERNEL); | |
57d6cfd9 | 533 | |
475b37e7 AL |
534 | #ifdef CONFIG_X86_32 |
535 | this_cpu_write(cpu_entry_area, get_cpu_entry_area(cpu)); | |
536 | #endif | |
9fec5954 AL |
537 | |
538 | #ifdef CONFIG_X86_64 | |
539 | __set_fixmap(get_cpu_entry_area_index(cpu, entry_trampoline), | |
540 | __pa_symbol(_entry_trampoline), PAGE_KERNEL_RX); | |
541 | #endif | |
69218e47 TG |
542 | } |
543 | ||
45fc8757 TG |
544 | /* Load the original GDT from the per-cpu structure */ |
545 | void load_direct_gdt(int cpu) | |
546 | { | |
547 | struct desc_ptr gdt_descr; | |
548 | ||
549 | gdt_descr.address = (long)get_cpu_gdt_rw(cpu); | |
550 | gdt_descr.size = GDT_SIZE - 1; | |
551 | load_gdt(&gdt_descr); | |
552 | } | |
553 | EXPORT_SYMBOL_GPL(load_direct_gdt); | |
554 | ||
69218e47 TG |
555 | /* Load a fixmap remapping of the per-cpu GDT */ |
556 | void load_fixmap_gdt(int cpu) | |
557 | { | |
558 | struct desc_ptr gdt_descr; | |
559 | ||
560 | gdt_descr.address = (long)get_cpu_gdt_ro(cpu); | |
561 | gdt_descr.size = GDT_SIZE - 1; | |
562 | load_gdt(&gdt_descr); | |
563 | } | |
45fc8757 | 564 | EXPORT_SYMBOL_GPL(load_fixmap_gdt); |
69218e47 | 565 | |
0f3fa48a IM |
566 | /* |
567 | * Current gdt points %fs at the "master" per-cpu area: after this, | |
568 | * it's on the real one. | |
569 | */ | |
552be871 | 570 | void switch_to_new_gdt(int cpu) |
9d31d35b | 571 | { |
45fc8757 TG |
572 | /* Load the original GDT */ |
573 | load_direct_gdt(cpu); | |
2697fbd5 | 574 | /* Reload the per-cpu base */ |
11e3a840 | 575 | load_percpu_segment(cpu); |
9d31d35b YL |
576 | } |
577 | ||
148f9bb8 | 578 | static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {}; |
1da177e4 | 579 | |
148f9bb8 | 580 | static void get_model_name(struct cpuinfo_x86 *c) |
1da177e4 LT |
581 | { |
582 | unsigned int *v; | |
ee098e1a | 583 | char *p, *q, *s; |
1da177e4 | 584 | |
3da99c97 | 585 | if (c->extended_cpuid_level < 0x80000004) |
1b05d60d | 586 | return; |
1da177e4 | 587 | |
0f3fa48a | 588 | v = (unsigned int *)c->x86_model_id; |
1da177e4 LT |
589 | cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]); |
590 | cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]); | |
591 | cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]); | |
592 | c->x86_model_id[48] = 0; | |
593 | ||
ee098e1a BP |
594 | /* Trim whitespace */ |
595 | p = q = s = &c->x86_model_id[0]; | |
596 | ||
597 | while (*p == ' ') | |
598 | p++; | |
599 | ||
600 | while (*p) { | |
601 | /* Note the last non-whitespace index */ | |
602 | if (!isspace(*p)) | |
603 | s = q; | |
604 | ||
605 | *q++ = *p++; | |
606 | } | |
607 | ||
608 | *(s + 1) = '\0'; | |
1da177e4 LT |
609 | } |
610 | ||
148f9bb8 | 611 | void cpu_detect_cache_sizes(struct cpuinfo_x86 *c) |
1da177e4 | 612 | { |
9d31d35b | 613 | unsigned int n, dummy, ebx, ecx, edx, l2size; |
1da177e4 | 614 | |
3da99c97 | 615 | n = c->extended_cpuid_level; |
1da177e4 LT |
616 | |
617 | if (n >= 0x80000005) { | |
9d31d35b | 618 | cpuid(0x80000005, &dummy, &ebx, &ecx, &edx); |
9d31d35b | 619 | c->x86_cache_size = (ecx>>24) + (edx>>24); |
140fc727 YL |
620 | #ifdef CONFIG_X86_64 |
621 | /* On K8 L1 TLB is inclusive, so don't count it */ | |
622 | c->x86_tlbsize = 0; | |
623 | #endif | |
1da177e4 LT |
624 | } |
625 | ||
626 | if (n < 0x80000006) /* Some chips just has a large L1. */ | |
627 | return; | |
628 | ||
0a488a53 | 629 | cpuid(0x80000006, &dummy, &ebx, &ecx, &edx); |
1da177e4 | 630 | l2size = ecx >> 16; |
34048c9e | 631 | |
140fc727 YL |
632 | #ifdef CONFIG_X86_64 |
633 | c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff); | |
634 | #else | |
1da177e4 | 635 | /* do processor-specific cache resizing */ |
09dc68d9 JB |
636 | if (this_cpu->legacy_cache_size) |
637 | l2size = this_cpu->legacy_cache_size(c, l2size); | |
1da177e4 LT |
638 | |
639 | /* Allow user to override all this if necessary. */ | |
640 | if (cachesize_override != -1) | |
641 | l2size = cachesize_override; | |
642 | ||
34048c9e | 643 | if (l2size == 0) |
1da177e4 | 644 | return; /* Again, no L2 cache is possible */ |
140fc727 | 645 | #endif |
1da177e4 LT |
646 | |
647 | c->x86_cache_size = l2size; | |
1da177e4 LT |
648 | } |
649 | ||
e0ba94f1 AS |
650 | u16 __read_mostly tlb_lli_4k[NR_INFO]; |
651 | u16 __read_mostly tlb_lli_2m[NR_INFO]; | |
652 | u16 __read_mostly tlb_lli_4m[NR_INFO]; | |
653 | u16 __read_mostly tlb_lld_4k[NR_INFO]; | |
654 | u16 __read_mostly tlb_lld_2m[NR_INFO]; | |
655 | u16 __read_mostly tlb_lld_4m[NR_INFO]; | |
dd360393 | 656 | u16 __read_mostly tlb_lld_1g[NR_INFO]; |
e0ba94f1 | 657 | |
f94fe119 | 658 | static void cpu_detect_tlb(struct cpuinfo_x86 *c) |
e0ba94f1 AS |
659 | { |
660 | if (this_cpu->c_detect_tlb) | |
661 | this_cpu->c_detect_tlb(c); | |
662 | ||
f94fe119 | 663 | pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n", |
e0ba94f1 | 664 | tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES], |
f94fe119 SH |
665 | tlb_lli_4m[ENTRIES]); |
666 | ||
667 | pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n", | |
668 | tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES], | |
669 | tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]); | |
e0ba94f1 AS |
670 | } |
671 | ||
148f9bb8 | 672 | void detect_ht(struct cpuinfo_x86 *c) |
1da177e4 | 673 | { |
c8e56d20 | 674 | #ifdef CONFIG_SMP |
0a488a53 YL |
675 | u32 eax, ebx, ecx, edx; |
676 | int index_msb, core_bits; | |
2eaad1fd | 677 | static bool printed; |
1da177e4 | 678 | |
0a488a53 | 679 | if (!cpu_has(c, X86_FEATURE_HT)) |
9d31d35b | 680 | return; |
1da177e4 | 681 | |
0a488a53 YL |
682 | if (cpu_has(c, X86_FEATURE_CMP_LEGACY)) |
683 | goto out; | |
1da177e4 | 684 | |
1cd78776 YL |
685 | if (cpu_has(c, X86_FEATURE_XTOPOLOGY)) |
686 | return; | |
1da177e4 | 687 | |
0a488a53 | 688 | cpuid(1, &eax, &ebx, &ecx, &edx); |
1da177e4 | 689 | |
9d31d35b YL |
690 | smp_num_siblings = (ebx & 0xff0000) >> 16; |
691 | ||
692 | if (smp_num_siblings == 1) { | |
1b74dde7 | 693 | pr_info_once("CPU0: Hyper-Threading is disabled\n"); |
0f3fa48a IM |
694 | goto out; |
695 | } | |
9d31d35b | 696 | |
0f3fa48a IM |
697 | if (smp_num_siblings <= 1) |
698 | goto out; | |
9d31d35b | 699 | |
0f3fa48a IM |
700 | index_msb = get_count_order(smp_num_siblings); |
701 | c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb); | |
9d31d35b | 702 | |
0f3fa48a | 703 | smp_num_siblings = smp_num_siblings / c->x86_max_cores; |
9d31d35b | 704 | |
0f3fa48a | 705 | index_msb = get_count_order(smp_num_siblings); |
9d31d35b | 706 | |
0f3fa48a | 707 | core_bits = get_count_order(c->x86_max_cores); |
9d31d35b | 708 | |
0f3fa48a IM |
709 | c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) & |
710 | ((1 << core_bits) - 1); | |
1da177e4 | 711 | |
0a488a53 | 712 | out: |
2eaad1fd | 713 | if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) { |
1b74dde7 CY |
714 | pr_info("CPU: Physical Processor ID: %d\n", |
715 | c->phys_proc_id); | |
716 | pr_info("CPU: Processor Core ID: %d\n", | |
717 | c->cpu_core_id); | |
2eaad1fd | 718 | printed = 1; |
9d31d35b | 719 | } |
9d31d35b | 720 | #endif |
97e4db7c | 721 | } |
1da177e4 | 722 | |
148f9bb8 | 723 | static void get_cpu_vendor(struct cpuinfo_x86 *c) |
1da177e4 LT |
724 | { |
725 | char *v = c->x86_vendor_id; | |
0f3fa48a | 726 | int i; |
1da177e4 LT |
727 | |
728 | for (i = 0; i < X86_VENDOR_NUM; i++) { | |
10a434fc YL |
729 | if (!cpu_devs[i]) |
730 | break; | |
731 | ||
732 | if (!strcmp(v, cpu_devs[i]->c_ident[0]) || | |
733 | (cpu_devs[i]->c_ident[1] && | |
734 | !strcmp(v, cpu_devs[i]->c_ident[1]))) { | |
0f3fa48a | 735 | |
10a434fc YL |
736 | this_cpu = cpu_devs[i]; |
737 | c->x86_vendor = this_cpu->c_x86_vendor; | |
738 | return; | |
1da177e4 LT |
739 | } |
740 | } | |
10a434fc | 741 | |
1b74dde7 CY |
742 | pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \ |
743 | "CPU: Your system may be unstable.\n", v); | |
10a434fc | 744 | |
fe38d855 CE |
745 | c->x86_vendor = X86_VENDOR_UNKNOWN; |
746 | this_cpu = &default_cpu; | |
1da177e4 LT |
747 | } |
748 | ||
148f9bb8 | 749 | void cpu_detect(struct cpuinfo_x86 *c) |
1da177e4 | 750 | { |
1da177e4 | 751 | /* Get vendor name */ |
4a148513 HH |
752 | cpuid(0x00000000, (unsigned int *)&c->cpuid_level, |
753 | (unsigned int *)&c->x86_vendor_id[0], | |
754 | (unsigned int *)&c->x86_vendor_id[8], | |
755 | (unsigned int *)&c->x86_vendor_id[4]); | |
1da177e4 | 756 | |
1da177e4 | 757 | c->x86 = 4; |
9d31d35b | 758 | /* Intel-defined flags: level 0x00000001 */ |
1da177e4 LT |
759 | if (c->cpuid_level >= 0x00000001) { |
760 | u32 junk, tfms, cap0, misc; | |
0f3fa48a | 761 | |
1da177e4 | 762 | cpuid(0x00000001, &tfms, &misc, &junk, &cap0); |
99f925ce BP |
763 | c->x86 = x86_family(tfms); |
764 | c->x86_model = x86_model(tfms); | |
765 | c->x86_mask = x86_stepping(tfms); | |
0f3fa48a | 766 | |
d4387bd3 | 767 | if (cap0 & (1<<19)) { |
d4387bd3 | 768 | c->x86_clflush_size = ((misc >> 8) & 0xff) * 8; |
9d31d35b | 769 | c->x86_cache_alignment = c->x86_clflush_size; |
d4387bd3 | 770 | } |
1da177e4 | 771 | } |
1da177e4 | 772 | } |
3da99c97 | 773 | |
8bf1ebca AL |
774 | static void apply_forced_caps(struct cpuinfo_x86 *c) |
775 | { | |
776 | int i; | |
777 | ||
778 | for (i = 0; i < NCAPINTS; i++) { | |
779 | c->x86_capability[i] &= ~cpu_caps_cleared[i]; | |
780 | c->x86_capability[i] |= cpu_caps_set[i]; | |
781 | } | |
782 | } | |
783 | ||
148f9bb8 | 784 | void get_cpu_cap(struct cpuinfo_x86 *c) |
093af8d7 | 785 | { |
39c06df4 | 786 | u32 eax, ebx, ecx, edx; |
093af8d7 | 787 | |
3da99c97 YL |
788 | /* Intel-defined flags: level 0x00000001 */ |
789 | if (c->cpuid_level >= 0x00000001) { | |
39c06df4 | 790 | cpuid(0x00000001, &eax, &ebx, &ecx, &edx); |
0f3fa48a | 791 | |
39c06df4 BP |
792 | c->x86_capability[CPUID_1_ECX] = ecx; |
793 | c->x86_capability[CPUID_1_EDX] = edx; | |
3da99c97 | 794 | } |
093af8d7 | 795 | |
3df8d920 AL |
796 | /* Thermal and Power Management Leaf: level 0x00000006 (eax) */ |
797 | if (c->cpuid_level >= 0x00000006) | |
798 | c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006); | |
799 | ||
bdc802dc PA |
800 | /* Additional Intel-defined flags: level 0x00000007 */ |
801 | if (c->cpuid_level >= 0x00000007) { | |
bdc802dc | 802 | cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx); |
39c06df4 | 803 | c->x86_capability[CPUID_7_0_EBX] = ebx; |
dfb4a70f | 804 | c->x86_capability[CPUID_7_ECX] = ecx; |
bdc802dc PA |
805 | } |
806 | ||
6229ad27 FY |
807 | /* Extended state features: level 0x0000000d */ |
808 | if (c->cpuid_level >= 0x0000000d) { | |
6229ad27 FY |
809 | cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx); |
810 | ||
39c06df4 | 811 | c->x86_capability[CPUID_D_1_EAX] = eax; |
6229ad27 FY |
812 | } |
813 | ||
cbc82b17 PWJ |
814 | /* Additional Intel-defined flags: level 0x0000000F */ |
815 | if (c->cpuid_level >= 0x0000000F) { | |
cbc82b17 PWJ |
816 | |
817 | /* QoS sub-leaf, EAX=0Fh, ECX=0 */ | |
818 | cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx); | |
39c06df4 BP |
819 | c->x86_capability[CPUID_F_0_EDX] = edx; |
820 | ||
cbc82b17 PWJ |
821 | if (cpu_has(c, X86_FEATURE_CQM_LLC)) { |
822 | /* will be overridden if occupancy monitoring exists */ | |
823 | c->x86_cache_max_rmid = ebx; | |
824 | ||
825 | /* QoS sub-leaf, EAX=0Fh, ECX=1 */ | |
826 | cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx); | |
39c06df4 BP |
827 | c->x86_capability[CPUID_F_1_EDX] = edx; |
828 | ||
33c3cc7a VS |
829 | if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) || |
830 | ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) || | |
831 | (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) { | |
cbc82b17 PWJ |
832 | c->x86_cache_max_rmid = ecx; |
833 | c->x86_cache_occ_scale = ebx; | |
834 | } | |
835 | } else { | |
836 | c->x86_cache_max_rmid = -1; | |
837 | c->x86_cache_occ_scale = -1; | |
838 | } | |
839 | } | |
840 | ||
3da99c97 | 841 | /* AMD-defined flags: level 0x80000001 */ |
39c06df4 BP |
842 | eax = cpuid_eax(0x80000000); |
843 | c->extended_cpuid_level = eax; | |
844 | ||
845 | if ((eax & 0xffff0000) == 0x80000000) { | |
846 | if (eax >= 0x80000001) { | |
847 | cpuid(0x80000001, &eax, &ebx, &ecx, &edx); | |
0f3fa48a | 848 | |
39c06df4 BP |
849 | c->x86_capability[CPUID_8000_0001_ECX] = ecx; |
850 | c->x86_capability[CPUID_8000_0001_EDX] = edx; | |
093af8d7 | 851 | } |
093af8d7 | 852 | } |
093af8d7 | 853 | |
71faad43 YG |
854 | if (c->extended_cpuid_level >= 0x80000007) { |
855 | cpuid(0x80000007, &eax, &ebx, &ecx, &edx); | |
856 | ||
857 | c->x86_capability[CPUID_8000_0007_EBX] = ebx; | |
858 | c->x86_power = edx; | |
859 | } | |
860 | ||
5122c890 | 861 | if (c->extended_cpuid_level >= 0x80000008) { |
39c06df4 | 862 | cpuid(0x80000008, &eax, &ebx, &ecx, &edx); |
5122c890 YL |
863 | |
864 | c->x86_virt_bits = (eax >> 8) & 0xff; | |
865 | c->x86_phys_bits = eax & 0xff; | |
39c06df4 | 866 | c->x86_capability[CPUID_8000_0008_EBX] = ebx; |
093af8d7 | 867 | } |
13c6c532 JB |
868 | #ifdef CONFIG_X86_32 |
869 | else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36)) | |
870 | c->x86_phys_bits = 36; | |
5122c890 | 871 | #endif |
e3224234 | 872 | |
2ccd71f1 | 873 | if (c->extended_cpuid_level >= 0x8000000a) |
39c06df4 | 874 | c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a); |
093af8d7 | 875 | |
1dedefd1 | 876 | init_scattered_cpuid_features(c); |
60d34501 AL |
877 | |
878 | /* | |
879 | * Clear/Set all flags overridden by options, after probe. | |
880 | * This needs to happen each time we re-probe, which may happen | |
881 | * several times during CPU initialization. | |
882 | */ | |
883 | apply_forced_caps(c); | |
093af8d7 | 884 | } |
1da177e4 | 885 | |
148f9bb8 | 886 | static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c) |
aef93c8b YL |
887 | { |
888 | #ifdef CONFIG_X86_32 | |
889 | int i; | |
890 | ||
891 | /* | |
892 | * First of all, decide if this is a 486 or higher | |
893 | * It's a 486 if we can modify the AC flag | |
894 | */ | |
895 | if (flag_is_changeable_p(X86_EFLAGS_AC)) | |
896 | c->x86 = 4; | |
897 | else | |
898 | c->x86 = 3; | |
899 | ||
900 | for (i = 0; i < X86_VENDOR_NUM; i++) | |
901 | if (cpu_devs[i] && cpu_devs[i]->c_identify) { | |
902 | c->x86_vendor_id[0] = 0; | |
903 | cpu_devs[i]->c_identify(c); | |
904 | if (c->x86_vendor_id[0]) { | |
905 | get_cpu_vendor(c); | |
906 | break; | |
907 | } | |
908 | } | |
909 | #endif | |
910 | } | |
911 | ||
34048c9e PC |
912 | /* |
913 | * Do minimum CPU detection early. | |
914 | * Fields really needed: vendor, cpuid_level, family, model, mask, | |
915 | * cache alignment. | |
916 | * The others are not touched to avoid unwanted side effects. | |
917 | * | |
918 | * WARNING: this function is only called on the BP. Don't add code here | |
919 | * that is supposed to run on all CPUs. | |
920 | */ | |
3da99c97 | 921 | static void __init early_identify_cpu(struct cpuinfo_x86 *c) |
d7cd5611 | 922 | { |
6627d242 YL |
923 | #ifdef CONFIG_X86_64 |
924 | c->x86_clflush_size = 64; | |
13c6c532 JB |
925 | c->x86_phys_bits = 36; |
926 | c->x86_virt_bits = 48; | |
6627d242 | 927 | #else |
d4387bd3 | 928 | c->x86_clflush_size = 32; |
13c6c532 JB |
929 | c->x86_phys_bits = 32; |
930 | c->x86_virt_bits = 32; | |
6627d242 | 931 | #endif |
0a488a53 | 932 | c->x86_cache_alignment = c->x86_clflush_size; |
d7cd5611 | 933 | |
3da99c97 | 934 | memset(&c->x86_capability, 0, sizeof c->x86_capability); |
0a488a53 | 935 | c->extended_cpuid_level = 0; |
d7cd5611 | 936 | |
aef93c8b | 937 | /* cyrix could have cpuid enabled via c_identify()*/ |
05fb3c19 AL |
938 | if (have_cpuid_p()) { |
939 | cpu_detect(c); | |
940 | get_cpu_vendor(c); | |
941 | get_cpu_cap(c); | |
78d1b296 | 942 | setup_force_cpu_cap(X86_FEATURE_CPUID); |
d7cd5611 | 943 | |
05fb3c19 AL |
944 | if (this_cpu->c_early_init) |
945 | this_cpu->c_early_init(c); | |
12cf105c | 946 | |
05fb3c19 AL |
947 | c->cpu_index = 0; |
948 | filter_cpuid_features(c, false); | |
093af8d7 | 949 | |
05fb3c19 AL |
950 | if (this_cpu->c_bsp_init) |
951 | this_cpu->c_bsp_init(c); | |
78d1b296 BP |
952 | } else { |
953 | identify_cpu_without_cpuid(c); | |
954 | setup_clear_cpu_cap(X86_FEATURE_CPUID); | |
05fb3c19 | 955 | } |
c3b83598 BP |
956 | |
957 | setup_force_cpu_cap(X86_FEATURE_ALWAYS); | |
db52ef74 | 958 | fpu__init_system(c); |
d7cd5611 RR |
959 | } |
960 | ||
9d31d35b YL |
961 | void __init early_cpu_init(void) |
962 | { | |
02dde8b4 | 963 | const struct cpu_dev *const *cdev; |
10a434fc YL |
964 | int count = 0; |
965 | ||
ac23f253 | 966 | #ifdef CONFIG_PROCESSOR_SELECT |
1b74dde7 | 967 | pr_info("KERNEL supported cpus:\n"); |
31c997ca IM |
968 | #endif |
969 | ||
10a434fc | 970 | for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) { |
02dde8b4 | 971 | const struct cpu_dev *cpudev = *cdev; |
9d31d35b | 972 | |
10a434fc YL |
973 | if (count >= X86_VENDOR_NUM) |
974 | break; | |
975 | cpu_devs[count] = cpudev; | |
976 | count++; | |
977 | ||
ac23f253 | 978 | #ifdef CONFIG_PROCESSOR_SELECT |
31c997ca IM |
979 | { |
980 | unsigned int j; | |
981 | ||
982 | for (j = 0; j < 2; j++) { | |
983 | if (!cpudev->c_ident[j]) | |
984 | continue; | |
1b74dde7 | 985 | pr_info(" %s %s\n", cpudev->c_vendor, |
31c997ca IM |
986 | cpudev->c_ident[j]); |
987 | } | |
10a434fc | 988 | } |
0388423d | 989 | #endif |
10a434fc | 990 | } |
9d31d35b | 991 | early_identify_cpu(&boot_cpu_data); |
d7cd5611 | 992 | } |
093af8d7 | 993 | |
b6734c35 | 994 | /* |
366d4a43 BP |
995 | * The NOPL instruction is supposed to exist on all CPUs of family >= 6; |
996 | * unfortunately, that's not true in practice because of early VIA | |
997 | * chips and (more importantly) broken virtualizers that are not easy | |
998 | * to detect. In the latter case it doesn't even *fail* reliably, so | |
999 | * probing for it doesn't even work. Disable it completely on 32-bit | |
ba0593bf | 1000 | * unless we can find a reliable way to detect all the broken cases. |
366d4a43 | 1001 | * Enable it explicitly on 64-bit for non-constant inputs of cpu_has(). |
b6734c35 | 1002 | */ |
148f9bb8 | 1003 | static void detect_nopl(struct cpuinfo_x86 *c) |
b6734c35 | 1004 | { |
366d4a43 | 1005 | #ifdef CONFIG_X86_32 |
b6734c35 | 1006 | clear_cpu_cap(c, X86_FEATURE_NOPL); |
366d4a43 BP |
1007 | #else |
1008 | set_cpu_cap(c, X86_FEATURE_NOPL); | |
58a5aac5 | 1009 | #endif |
d7cd5611 | 1010 | } |
58a5aac5 | 1011 | |
7a5d6704 AL |
1012 | static void detect_null_seg_behavior(struct cpuinfo_x86 *c) |
1013 | { | |
1014 | #ifdef CONFIG_X86_64 | |
58a5aac5 | 1015 | /* |
7a5d6704 AL |
1016 | * Empirically, writing zero to a segment selector on AMD does |
1017 | * not clear the base, whereas writing zero to a segment | |
1018 | * selector on Intel does clear the base. Intel's behavior | |
1019 | * allows slightly faster context switches in the common case | |
1020 | * where GS is unused by the prev and next threads. | |
58a5aac5 | 1021 | * |
7a5d6704 AL |
1022 | * Since neither vendor documents this anywhere that I can see, |
1023 | * detect it directly instead of hardcoding the choice by | |
1024 | * vendor. | |
1025 | * | |
1026 | * I've designated AMD's behavior as the "bug" because it's | |
1027 | * counterintuitive and less friendly. | |
58a5aac5 | 1028 | */ |
7a5d6704 AL |
1029 | |
1030 | unsigned long old_base, tmp; | |
1031 | rdmsrl(MSR_FS_BASE, old_base); | |
1032 | wrmsrl(MSR_FS_BASE, 1); | |
1033 | loadsegment(fs, 0); | |
1034 | rdmsrl(MSR_FS_BASE, tmp); | |
1035 | if (tmp != 0) | |
1036 | set_cpu_bug(c, X86_BUG_NULL_SEG); | |
1037 | wrmsrl(MSR_FS_BASE, old_base); | |
366d4a43 | 1038 | #endif |
d7cd5611 RR |
1039 | } |
1040 | ||
148f9bb8 | 1041 | static void generic_identify(struct cpuinfo_x86 *c) |
1da177e4 | 1042 | { |
aef93c8b | 1043 | c->extended_cpuid_level = 0; |
1da177e4 | 1044 | |
3da99c97 | 1045 | if (!have_cpuid_p()) |
aef93c8b | 1046 | identify_cpu_without_cpuid(c); |
1d67953f | 1047 | |
aef93c8b | 1048 | /* cyrix could have cpuid enabled via c_identify()*/ |
a9853dd6 | 1049 | if (!have_cpuid_p()) |
aef93c8b | 1050 | return; |
1da177e4 | 1051 | |
3da99c97 | 1052 | cpu_detect(c); |
1da177e4 | 1053 | |
3da99c97 | 1054 | get_cpu_vendor(c); |
1da177e4 | 1055 | |
3da99c97 | 1056 | get_cpu_cap(c); |
1da177e4 | 1057 | |
3da99c97 YL |
1058 | if (c->cpuid_level >= 0x00000001) { |
1059 | c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF; | |
b89d3b3e | 1060 | #ifdef CONFIG_X86_32 |
c8e56d20 | 1061 | # ifdef CONFIG_SMP |
cb8cc442 | 1062 | c->apicid = apic->phys_pkg_id(c->initial_apicid, 0); |
b89d3b3e | 1063 | # else |
3da99c97 | 1064 | c->apicid = c->initial_apicid; |
b89d3b3e YL |
1065 | # endif |
1066 | #endif | |
b89d3b3e | 1067 | c->phys_proc_id = c->initial_apicid; |
3da99c97 | 1068 | } |
1da177e4 | 1069 | |
1b05d60d | 1070 | get_model_name(c); /* Default name */ |
1da177e4 | 1071 | |
3da99c97 | 1072 | detect_nopl(c); |
7a5d6704 AL |
1073 | |
1074 | detect_null_seg_behavior(c); | |
0230bb03 AL |
1075 | |
1076 | /* | |
1077 | * ESPFIX is a strange bug. All real CPUs have it. Paravirt | |
1078 | * systems that run Linux at CPL > 0 may or may not have the | |
1079 | * issue, but, even if they have the issue, there's absolutely | |
1080 | * nothing we can do about it because we can't use the real IRET | |
1081 | * instruction. | |
1082 | * | |
1083 | * NB: For the time being, only 32-bit kernels support | |
1084 | * X86_BUG_ESPFIX as such. 64-bit kernels directly choose | |
1085 | * whether to apply espfix using paravirt hooks. If any | |
1086 | * non-paravirt system ever shows up that does *not* have the | |
1087 | * ESPFIX issue, we can change this. | |
1088 | */ | |
1089 | #ifdef CONFIG_X86_32 | |
1090 | # ifdef CONFIG_PARAVIRT | |
1091 | do { | |
1092 | extern void native_iret(void); | |
1093 | if (pv_cpu_ops.iret == native_iret) | |
1094 | set_cpu_bug(c, X86_BUG_ESPFIX); | |
1095 | } while (0); | |
1096 | # else | |
1097 | set_cpu_bug(c, X86_BUG_ESPFIX); | |
1098 | # endif | |
1099 | #endif | |
1da177e4 | 1100 | } |
1da177e4 | 1101 | |
cbc82b17 PWJ |
1102 | static void x86_init_cache_qos(struct cpuinfo_x86 *c) |
1103 | { | |
1104 | /* | |
1105 | * The heavy lifting of max_rmid and cache_occ_scale are handled | |
1106 | * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu | |
1107 | * in case CQM bits really aren't there in this CPU. | |
1108 | */ | |
1109 | if (c != &boot_cpu_data) { | |
1110 | boot_cpu_data.x86_cache_max_rmid = | |
1111 | min(boot_cpu_data.x86_cache_max_rmid, | |
1112 | c->x86_cache_max_rmid); | |
1113 | } | |
1114 | } | |
1115 | ||
d49597fd | 1116 | /* |
9d85eb91 TG |
1117 | * Validate that ACPI/mptables have the same information about the |
1118 | * effective APIC id and update the package map. | |
d49597fd | 1119 | */ |
9d85eb91 | 1120 | static void validate_apic_and_package_id(struct cpuinfo_x86 *c) |
d49597fd TG |
1121 | { |
1122 | #ifdef CONFIG_SMP | |
9d85eb91 | 1123 | unsigned int apicid, cpu = smp_processor_id(); |
d49597fd TG |
1124 | |
1125 | apicid = apic->cpu_present_to_apicid(cpu); | |
d49597fd | 1126 | |
9d85eb91 TG |
1127 | if (apicid != c->apicid) { |
1128 | pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n", | |
d49597fd | 1129 | cpu, apicid, c->initial_apicid); |
d49597fd | 1130 | } |
9d85eb91 | 1131 | BUG_ON(topology_update_package_map(c->phys_proc_id, cpu)); |
d49597fd TG |
1132 | #else |
1133 | c->logical_proc_id = 0; | |
1134 | #endif | |
1135 | } | |
1136 | ||
1da177e4 LT |
1137 | /* |
1138 | * This does the hard work of actually picking apart the CPU stuff... | |
1139 | */ | |
148f9bb8 | 1140 | static void identify_cpu(struct cpuinfo_x86 *c) |
1da177e4 LT |
1141 | { |
1142 | int i; | |
1143 | ||
1144 | c->loops_per_jiffy = loops_per_jiffy; | |
1145 | c->x86_cache_size = -1; | |
1146 | c->x86_vendor = X86_VENDOR_UNKNOWN; | |
1da177e4 LT |
1147 | c->x86_model = c->x86_mask = 0; /* So far unknown... */ |
1148 | c->x86_vendor_id[0] = '\0'; /* Unset */ | |
1149 | c->x86_model_id[0] = '\0'; /* Unset */ | |
94605eff | 1150 | c->x86_max_cores = 1; |
102bbe3a | 1151 | c->x86_coreid_bits = 0; |
79a8b9aa | 1152 | c->cu_id = 0xff; |
11fdd252 | 1153 | #ifdef CONFIG_X86_64 |
102bbe3a | 1154 | c->x86_clflush_size = 64; |
13c6c532 JB |
1155 | c->x86_phys_bits = 36; |
1156 | c->x86_virt_bits = 48; | |
102bbe3a YL |
1157 | #else |
1158 | c->cpuid_level = -1; /* CPUID not detected */ | |
770d132f | 1159 | c->x86_clflush_size = 32; |
13c6c532 JB |
1160 | c->x86_phys_bits = 32; |
1161 | c->x86_virt_bits = 32; | |
102bbe3a YL |
1162 | #endif |
1163 | c->x86_cache_alignment = c->x86_clflush_size; | |
1da177e4 LT |
1164 | memset(&c->x86_capability, 0, sizeof c->x86_capability); |
1165 | ||
1da177e4 LT |
1166 | generic_identify(c); |
1167 | ||
3898534d | 1168 | if (this_cpu->c_identify) |
1da177e4 LT |
1169 | this_cpu->c_identify(c); |
1170 | ||
6a6256f9 | 1171 | /* Clear/Set all flags overridden by options, after probe */ |
8bf1ebca | 1172 | apply_forced_caps(c); |
2759c328 | 1173 | |
102bbe3a | 1174 | #ifdef CONFIG_X86_64 |
cb8cc442 | 1175 | c->apicid = apic->phys_pkg_id(c->initial_apicid, 0); |
102bbe3a YL |
1176 | #endif |
1177 | ||
1da177e4 LT |
1178 | /* |
1179 | * Vendor-specific initialization. In this section we | |
1180 | * canonicalize the feature flags, meaning if there are | |
1181 | * features a certain CPU supports which CPUID doesn't | |
1182 | * tell us, CPUID claiming incorrect flags, or other bugs, | |
1183 | * we handle them here. | |
1184 | * | |
1185 | * At the end of this section, c->x86_capability better | |
1186 | * indicate the features this CPU genuinely supports! | |
1187 | */ | |
1188 | if (this_cpu->c_init) | |
1189 | this_cpu->c_init(c); | |
1190 | ||
1191 | /* Disable the PN if appropriate */ | |
1192 | squash_the_stupid_serial_number(c); | |
1193 | ||
b2cc2a07 PA |
1194 | /* Set up SMEP/SMAP */ |
1195 | setup_smep(c); | |
1196 | setup_smap(c); | |
1197 | ||
1da177e4 | 1198 | /* |
0f3fa48a IM |
1199 | * The vendor-specific functions might have changed features. |
1200 | * Now we do "generic changes." | |
1da177e4 LT |
1201 | */ |
1202 | ||
b38b0665 PA |
1203 | /* Filter out anything that depends on CPUID levels we don't have */ |
1204 | filter_cpuid_features(c, true); | |
1205 | ||
1da177e4 | 1206 | /* If the model name is still unset, do table lookup. */ |
34048c9e | 1207 | if (!c->x86_model_id[0]) { |
02dde8b4 | 1208 | const char *p; |
1da177e4 | 1209 | p = table_lookup_model(c); |
34048c9e | 1210 | if (p) |
1da177e4 LT |
1211 | strcpy(c->x86_model_id, p); |
1212 | else | |
1213 | /* Last resort... */ | |
1214 | sprintf(c->x86_model_id, "%02x/%02x", | |
54a20f8c | 1215 | c->x86, c->x86_model); |
1da177e4 LT |
1216 | } |
1217 | ||
102bbe3a YL |
1218 | #ifdef CONFIG_X86_64 |
1219 | detect_ht(c); | |
1220 | #endif | |
1221 | ||
49d859d7 | 1222 | x86_init_rdrand(c); |
cbc82b17 | 1223 | x86_init_cache_qos(c); |
06976945 | 1224 | setup_pku(c); |
3e0c3737 YL |
1225 | |
1226 | /* | |
6a6256f9 | 1227 | * Clear/Set all flags overridden by options, need do it |
3e0c3737 YL |
1228 | * before following smp all cpus cap AND. |
1229 | */ | |
8bf1ebca | 1230 | apply_forced_caps(c); |
3e0c3737 | 1231 | |
1da177e4 LT |
1232 | /* |
1233 | * On SMP, boot_cpu_data holds the common feature set between | |
1234 | * all CPUs; so make sure that we indicate which features are | |
1235 | * common between the CPUs. The first time this routine gets | |
1236 | * executed, c == &boot_cpu_data. | |
1237 | */ | |
34048c9e | 1238 | if (c != &boot_cpu_data) { |
1da177e4 | 1239 | /* AND the already accumulated flags with these */ |
9d31d35b | 1240 | for (i = 0; i < NCAPINTS; i++) |
1da177e4 | 1241 | boot_cpu_data.x86_capability[i] &= c->x86_capability[i]; |
65fc985b BP |
1242 | |
1243 | /* OR, i.e. replicate the bug flags */ | |
1244 | for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++) | |
1245 | c->x86_capability[i] |= boot_cpu_data.x86_capability[i]; | |
1da177e4 LT |
1246 | } |
1247 | ||
1248 | /* Init Machine Check Exception if available. */ | |
5e09954a | 1249 | mcheck_cpu_init(c); |
30d432df AK |
1250 | |
1251 | select_idle_routine(c); | |
102bbe3a | 1252 | |
de2d9445 | 1253 | #ifdef CONFIG_NUMA |
102bbe3a YL |
1254 | numa_add_cpu(smp_processor_id()); |
1255 | #endif | |
a6c4e076 | 1256 | } |
31ab269a | 1257 | |
8b6c0ab1 IM |
1258 | /* |
1259 | * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions | |
1260 | * on 32-bit kernels: | |
1261 | */ | |
cfda7bb9 AL |
1262 | #ifdef CONFIG_X86_32 |
1263 | void enable_sep_cpu(void) | |
1264 | { | |
8b6c0ab1 IM |
1265 | struct tss_struct *tss; |
1266 | int cpu; | |
cfda7bb9 | 1267 | |
b3edfda4 BP |
1268 | if (!boot_cpu_has(X86_FEATURE_SEP)) |
1269 | return; | |
1270 | ||
8b6c0ab1 IM |
1271 | cpu = get_cpu(); |
1272 | tss = &per_cpu(cpu_tss, cpu); | |
1273 | ||
8b6c0ab1 | 1274 | /* |
cf9328cc AL |
1275 | * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field -- |
1276 | * see the big comment in struct x86_hw_tss's definition. | |
8b6c0ab1 | 1277 | */ |
cfda7bb9 AL |
1278 | |
1279 | tss->x86_tss.ss1 = __KERNEL_CS; | |
8b6c0ab1 IM |
1280 | wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0); |
1281 | ||
cf9328cc | 1282 | wrmsr(MSR_IA32_SYSENTER_ESP, |
475b37e7 AL |
1283 | (unsigned long)&get_cpu_entry_area(cpu)->tss + |
1284 | offsetofend(struct tss_struct, SYSENTER_stack), | |
cf9328cc | 1285 | 0); |
8b6c0ab1 | 1286 | |
4c8cd0c5 | 1287 | wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0); |
8b6c0ab1 | 1288 | |
cfda7bb9 AL |
1289 | put_cpu(); |
1290 | } | |
e04d645f GC |
1291 | #endif |
1292 | ||
a6c4e076 JF |
1293 | void __init identify_boot_cpu(void) |
1294 | { | |
1295 | identify_cpu(&boot_cpu_data); | |
102bbe3a | 1296 | #ifdef CONFIG_X86_32 |
a6c4e076 | 1297 | sysenter_setup(); |
6fe940d6 | 1298 | enable_sep_cpu(); |
102bbe3a | 1299 | #endif |
5b556332 | 1300 | cpu_detect_tlb(&boot_cpu_data); |
a6c4e076 | 1301 | } |
3b520b23 | 1302 | |
148f9bb8 | 1303 | void identify_secondary_cpu(struct cpuinfo_x86 *c) |
a6c4e076 JF |
1304 | { |
1305 | BUG_ON(c == &boot_cpu_data); | |
1306 | identify_cpu(c); | |
102bbe3a | 1307 | #ifdef CONFIG_X86_32 |
a6c4e076 | 1308 | enable_sep_cpu(); |
102bbe3a | 1309 | #endif |
a6c4e076 | 1310 | mtrr_ap_init(); |
9d85eb91 | 1311 | validate_apic_and_package_id(c); |
1da177e4 LT |
1312 | } |
1313 | ||
191679fd AK |
1314 | static __init int setup_noclflush(char *arg) |
1315 | { | |
840d2830 | 1316 | setup_clear_cpu_cap(X86_FEATURE_CLFLUSH); |
da4aaa7d | 1317 | setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT); |
191679fd AK |
1318 | return 1; |
1319 | } | |
1320 | __setup("noclflush", setup_noclflush); | |
1321 | ||
148f9bb8 | 1322 | void print_cpu_info(struct cpuinfo_x86 *c) |
1da177e4 | 1323 | { |
02dde8b4 | 1324 | const char *vendor = NULL; |
1da177e4 | 1325 | |
0f3fa48a | 1326 | if (c->x86_vendor < X86_VENDOR_NUM) { |
1da177e4 | 1327 | vendor = this_cpu->c_vendor; |
0f3fa48a IM |
1328 | } else { |
1329 | if (c->cpuid_level >= 0) | |
1330 | vendor = c->x86_vendor_id; | |
1331 | } | |
1da177e4 | 1332 | |
bd32a8cf | 1333 | if (vendor && !strstr(c->x86_model_id, vendor)) |
1b74dde7 | 1334 | pr_cont("%s ", vendor); |
1da177e4 | 1335 | |
9d31d35b | 1336 | if (c->x86_model_id[0]) |
1b74dde7 | 1337 | pr_cont("%s", c->x86_model_id); |
1da177e4 | 1338 | else |
1b74dde7 | 1339 | pr_cont("%d86", c->x86); |
1da177e4 | 1340 | |
1b74dde7 | 1341 | pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model); |
924e101a | 1342 | |
34048c9e | 1343 | if (c->x86_mask || c->cpuid_level >= 0) |
1b74dde7 | 1344 | pr_cont(", stepping: 0x%x)\n", c->x86_mask); |
1da177e4 | 1345 | else |
1b74dde7 | 1346 | pr_cont(")\n"); |
1da177e4 LT |
1347 | } |
1348 | ||
27deb452 AK |
1349 | /* |
1350 | * clearcpuid= was already parsed in fpu__init_parse_early_param. | |
1351 | * But we need to keep a dummy __setup around otherwise it would | |
1352 | * show up as an environment variable for init. | |
1353 | */ | |
1354 | static __init int setup_clearcpuid(char *arg) | |
ac72e788 | 1355 | { |
ac72e788 AK |
1356 | return 1; |
1357 | } | |
27deb452 | 1358 | __setup("clearcpuid=", setup_clearcpuid); |
ac72e788 | 1359 | |
d5494d4f | 1360 | #ifdef CONFIG_X86_64 |
404f6aac KC |
1361 | struct desc_ptr idt_descr __ro_after_init = { |
1362 | .size = NR_VECTORS * 16 - 1, | |
1363 | .address = (unsigned long) idt_table, | |
1364 | }; | |
1365 | const struct desc_ptr debug_idt_descr = { | |
1366 | .size = NR_VECTORS * 16 - 1, | |
1367 | .address = (unsigned long) debug_idt_table, | |
1368 | }; | |
d5494d4f | 1369 | |
947e76cd | 1370 | DEFINE_PER_CPU_FIRST(union irq_stack_union, |
277d5b40 | 1371 | irq_stack_union) __aligned(PAGE_SIZE) __visible; |
0f3fa48a | 1372 | |
bdf977b3 | 1373 | /* |
a7fcf28d AL |
1374 | * The following percpu variables are hot. Align current_task to |
1375 | * cacheline size such that they fall in the same cacheline. | |
bdf977b3 TH |
1376 | */ |
1377 | DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned = | |
1378 | &init_task; | |
1379 | EXPORT_PER_CPU_SYMBOL(current_task); | |
d5494d4f | 1380 | |
bdf977b3 | 1381 | DEFINE_PER_CPU(char *, irq_stack_ptr) = |
4950d6d4 | 1382 | init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE; |
bdf977b3 | 1383 | |
277d5b40 | 1384 | DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1; |
d5494d4f | 1385 | |
c2daa3be PZ |
1386 | DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT; |
1387 | EXPORT_PER_CPU_SYMBOL(__preempt_count); | |
1388 | ||
0f3fa48a IM |
1389 | /* |
1390 | * Special IST stacks which the CPU switches to when it calls | |
1391 | * an IST-marked descriptor entry. Up to 7 stacks (hardware | |
1392 | * limit), all of them are 4K, except the debug stack which | |
1393 | * is 8K. | |
1394 | */ | |
1395 | static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = { | |
1396 | [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ, | |
1397 | [DEBUG_STACK - 1] = DEBUG_STKSZ | |
1398 | }; | |
1399 | ||
92d65b23 | 1400 | static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks |
3e352aa8 | 1401 | [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]); |
d5494d4f | 1402 | |
d5494d4f YL |
1403 | /* May not be marked __init: used by software suspend */ |
1404 | void syscall_init(void) | |
1da177e4 | 1405 | { |
9fec5954 AL |
1406 | extern char _entry_trampoline[]; |
1407 | extern char entry_SYSCALL_64_trampoline[]; | |
1408 | ||
475b37e7 | 1409 | int cpu = smp_processor_id(); |
9fec5954 AL |
1410 | unsigned long SYSCALL64_entry_trampoline = |
1411 | (unsigned long)get_cpu_entry_area(cpu)->entry_trampoline + | |
1412 | (entry_SYSCALL_64_trampoline - _entry_trampoline); | |
475b37e7 | 1413 | |
31ac34ca | 1414 | wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS); |
9fec5954 | 1415 | wrmsrl(MSR_LSTAR, SYSCALL64_entry_trampoline); |
d56fe4bf IM |
1416 | |
1417 | #ifdef CONFIG_IA32_EMULATION | |
47edb651 | 1418 | wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat); |
a76c7f46 | 1419 | /* |
487d1edb DV |
1420 | * This only works on Intel CPUs. |
1421 | * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP. | |
1422 | * This does not cause SYSENTER to jump to the wrong location, because | |
1423 | * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit). | |
a76c7f46 DV |
1424 | */ |
1425 | wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS); | |
8e621515 | 1426 | wrmsrl_safe(MSR_IA32_SYSENTER_ESP, |
475b37e7 | 1427 | (unsigned long)&get_cpu_entry_area(cpu)->tss + |
8e621515 | 1428 | offsetofend(struct tss_struct, SYSENTER_stack)); |
4c8cd0c5 | 1429 | wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat); |
d56fe4bf | 1430 | #else |
47edb651 | 1431 | wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret); |
6b51311c | 1432 | wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG); |
d56fe4bf IM |
1433 | wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL); |
1434 | wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL); | |
d5494d4f | 1435 | #endif |
03ae5768 | 1436 | |
d5494d4f YL |
1437 | /* Flags to clear on syscall */ |
1438 | wrmsrl(MSR_SYSCALL_MASK, | |
63bcff2a | 1439 | X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF| |
8c7aa698 | 1440 | X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT); |
1da177e4 | 1441 | } |
62111195 | 1442 | |
d5494d4f YL |
1443 | /* |
1444 | * Copies of the original ist values from the tss are only accessed during | |
1445 | * debugging, no special alignment required. | |
1446 | */ | |
1447 | DEFINE_PER_CPU(struct orig_ist, orig_ist); | |
1448 | ||
228bdaa9 | 1449 | static DEFINE_PER_CPU(unsigned long, debug_stack_addr); |
42181186 | 1450 | DEFINE_PER_CPU(int, debug_stack_usage); |
228bdaa9 SR |
1451 | |
1452 | int is_debug_stack(unsigned long addr) | |
1453 | { | |
89cbc767 CL |
1454 | return __this_cpu_read(debug_stack_usage) || |
1455 | (addr <= __this_cpu_read(debug_stack_addr) && | |
1456 | addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ)); | |
228bdaa9 | 1457 | } |
0f46efeb | 1458 | NOKPROBE_SYMBOL(is_debug_stack); |
228bdaa9 | 1459 | |
629f4f9d | 1460 | DEFINE_PER_CPU(u32, debug_idt_ctr); |
f8988175 | 1461 | |
228bdaa9 SR |
1462 | void debug_stack_set_zero(void) |
1463 | { | |
629f4f9d SA |
1464 | this_cpu_inc(debug_idt_ctr); |
1465 | load_current_idt(); | |
228bdaa9 | 1466 | } |
0f46efeb | 1467 | NOKPROBE_SYMBOL(debug_stack_set_zero); |
228bdaa9 SR |
1468 | |
1469 | void debug_stack_reset(void) | |
1470 | { | |
629f4f9d | 1471 | if (WARN_ON(!this_cpu_read(debug_idt_ctr))) |
f8988175 | 1472 | return; |
629f4f9d SA |
1473 | if (this_cpu_dec_return(debug_idt_ctr) == 0) |
1474 | load_current_idt(); | |
228bdaa9 | 1475 | } |
0f46efeb | 1476 | NOKPROBE_SYMBOL(debug_stack_reset); |
228bdaa9 | 1477 | |
0f3fa48a | 1478 | #else /* CONFIG_X86_64 */ |
d5494d4f | 1479 | |
bdf977b3 TH |
1480 | DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task; |
1481 | EXPORT_PER_CPU_SYMBOL(current_task); | |
c2daa3be PZ |
1482 | DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT; |
1483 | EXPORT_PER_CPU_SYMBOL(__preempt_count); | |
bdf977b3 | 1484 | |
a7fcf28d AL |
1485 | /* |
1486 | * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find | |
1487 | * the top of the kernel stack. Use an extra percpu variable to track the | |
1488 | * top of the kernel stack directly. | |
1489 | */ | |
1490 | DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) = | |
1491 | (unsigned long)&init_thread_union + THREAD_SIZE; | |
1492 | EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack); | |
1493 | ||
60a5317f | 1494 | #ifdef CONFIG_CC_STACKPROTECTOR |
53f82452 | 1495 | DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary); |
60a5317f | 1496 | #endif |
d5494d4f | 1497 | |
0f3fa48a | 1498 | #endif /* CONFIG_X86_64 */ |
c5413fbe | 1499 | |
9766cdbc JSR |
1500 | /* |
1501 | * Clear all 6 debug registers: | |
1502 | */ | |
1503 | static void clear_all_debug_regs(void) | |
1504 | { | |
1505 | int i; | |
1506 | ||
1507 | for (i = 0; i < 8; i++) { | |
1508 | /* Ignore db4, db5 */ | |
1509 | if ((i == 4) || (i == 5)) | |
1510 | continue; | |
1511 | ||
1512 | set_debugreg(0, i); | |
1513 | } | |
1514 | } | |
c5413fbe | 1515 | |
0bb9fef9 JW |
1516 | #ifdef CONFIG_KGDB |
1517 | /* | |
1518 | * Restore debug regs if using kgdbwait and you have a kernel debugger | |
1519 | * connection established. | |
1520 | */ | |
1521 | static void dbg_restore_debug_regs(void) | |
1522 | { | |
1523 | if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break)) | |
1524 | arch_kgdb_ops.correct_hw_break(); | |
1525 | } | |
1526 | #else /* ! CONFIG_KGDB */ | |
1527 | #define dbg_restore_debug_regs() | |
1528 | #endif /* ! CONFIG_KGDB */ | |
1529 | ||
ce4b1b16 IM |
1530 | static void wait_for_master_cpu(int cpu) |
1531 | { | |
1532 | #ifdef CONFIG_SMP | |
1533 | /* | |
1534 | * wait for ACK from master CPU before continuing | |
1535 | * with AP initialization | |
1536 | */ | |
1537 | WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)); | |
1538 | while (!cpumask_test_cpu(cpu, cpu_callout_mask)) | |
1539 | cpu_relax(); | |
1540 | #endif | |
1541 | } | |
1542 | ||
d2cbcc49 RR |
1543 | /* |
1544 | * cpu_init() initializes state that is per-CPU. Some data is already | |
1545 | * initialized (naturally) in the bootstrap process, such as the GDT | |
1546 | * and IDT. We reload them nevertheless, this function acts as a | |
1547 | * 'CPU state barrier', nothing should get across. | |
1ba76586 | 1548 | * A lot of state is already set up in PDA init for 64 bit |
d2cbcc49 | 1549 | */ |
1ba76586 | 1550 | #ifdef CONFIG_X86_64 |
0f3fa48a | 1551 | |
148f9bb8 | 1552 | void cpu_init(void) |
1ba76586 | 1553 | { |
0fe1e009 | 1554 | struct orig_ist *oist; |
1ba76586 | 1555 | struct task_struct *me; |
0f3fa48a IM |
1556 | struct tss_struct *t; |
1557 | unsigned long v; | |
fb59831b | 1558 | int cpu = raw_smp_processor_id(); |
1ba76586 YL |
1559 | int i; |
1560 | ||
ce4b1b16 IM |
1561 | wait_for_master_cpu(cpu); |
1562 | ||
1e02ce4c AL |
1563 | /* |
1564 | * Initialize the CR4 shadow before doing anything that could | |
1565 | * try to read it. | |
1566 | */ | |
1567 | cr4_init_shadow(); | |
1568 | ||
777284b6 BP |
1569 | if (cpu) |
1570 | load_ucode_ap(); | |
e6ebf5de | 1571 | |
24933b82 | 1572 | t = &per_cpu(cpu_tss, cpu); |
0fe1e009 | 1573 | oist = &per_cpu(orig_ist, cpu); |
0f3fa48a | 1574 | |
e7a22c1e | 1575 | #ifdef CONFIG_NUMA |
27fd185f | 1576 | if (this_cpu_read(numa_node) == 0 && |
e534c7c5 LS |
1577 | early_cpu_to_node(cpu) != NUMA_NO_NODE) |
1578 | set_numa_node(early_cpu_to_node(cpu)); | |
e7a22c1e | 1579 | #endif |
1ba76586 YL |
1580 | |
1581 | me = current; | |
1582 | ||
2eaad1fd | 1583 | pr_debug("Initializing CPU#%d\n", cpu); |
1ba76586 | 1584 | |
375074cc | 1585 | cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); |
1ba76586 YL |
1586 | |
1587 | /* | |
1588 | * Initialize the per-CPU GDT with the boot GDT, | |
1589 | * and set up the GDT descriptor: | |
1590 | */ | |
1591 | ||
552be871 | 1592 | switch_to_new_gdt(cpu); |
2697fbd5 BG |
1593 | loadsegment(fs, 0); |
1594 | ||
cf910e83 | 1595 | load_current_idt(); |
1ba76586 YL |
1596 | |
1597 | memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8); | |
1598 | syscall_init(); | |
1599 | ||
1600 | wrmsrl(MSR_FS_BASE, 0); | |
1601 | wrmsrl(MSR_KERNEL_GS_BASE, 0); | |
1602 | barrier(); | |
1603 | ||
4763ed4d | 1604 | x86_configure_nx(); |
659006bf | 1605 | x2apic_setup(); |
1ba76586 YL |
1606 | |
1607 | /* | |
1608 | * set up and load the per-CPU TSS | |
1609 | */ | |
0fe1e009 | 1610 | if (!oist->ist[0]) { |
92d65b23 | 1611 | char *estacks = per_cpu(exception_stacks, cpu); |
0f3fa48a | 1612 | |
1ba76586 | 1613 | for (v = 0; v < N_EXCEPTION_STACKS; v++) { |
0f3fa48a | 1614 | estacks += exception_stack_sizes[v]; |
0fe1e009 | 1615 | oist->ist[v] = t->x86_tss.ist[v] = |
1ba76586 | 1616 | (unsigned long)estacks; |
228bdaa9 SR |
1617 | if (v == DEBUG_STACK-1) |
1618 | per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks; | |
1ba76586 YL |
1619 | } |
1620 | } | |
1621 | ||
7123a5de | 1622 | t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET; |
0f3fa48a | 1623 | |
1ba76586 YL |
1624 | /* |
1625 | * <= is required because the CPU will access up to | |
1626 | * 8 bits beyond the end of the IO permission bitmap. | |
1627 | */ | |
1628 | for (i = 0; i <= IO_BITMAP_LONGS; i++) | |
1629 | t->io_bitmap[i] = ~0UL; | |
1630 | ||
f1f10076 | 1631 | mmgrab(&init_mm); |
1ba76586 | 1632 | me->active_mm = &init_mm; |
8c5dfd25 | 1633 | BUG_ON(me->mm); |
1ba76586 YL |
1634 | enter_lazy_tlb(&init_mm, me); |
1635 | ||
475b37e7 AL |
1636 | setup_cpu_entry_area(cpu); |
1637 | ||
8c6b12e8 | 1638 | /* |
bfb2d0ed AL |
1639 | * Initialize the TSS. sp0 points to the entry trampoline stack |
1640 | * regardless of what task is running. | |
8c6b12e8 | 1641 | */ |
475b37e7 | 1642 | set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss); |
1ba76586 | 1643 | load_TR_desc(); |
bfb2d0ed AL |
1644 | load_sp0((unsigned long)&get_cpu_entry_area(cpu)->tss + |
1645 | offsetofend(struct tss_struct, SYSENTER_stack)); | |
8c6b12e8 | 1646 | |
37868fe1 | 1647 | load_mm_ldt(&init_mm); |
1ba76586 | 1648 | |
0bb9fef9 JW |
1649 | clear_all_debug_regs(); |
1650 | dbg_restore_debug_regs(); | |
1ba76586 | 1651 | |
21c4cd10 | 1652 | fpu__init_cpu(); |
1ba76586 | 1653 | |
1ba76586 YL |
1654 | if (is_uv_system()) |
1655 | uv_cpu_init(); | |
69218e47 | 1656 | |
69218e47 | 1657 | load_fixmap_gdt(cpu); |
1ba76586 YL |
1658 | } |
1659 | ||
1660 | #else | |
1661 | ||
148f9bb8 | 1662 | void cpu_init(void) |
9ee79a3d | 1663 | { |
d2cbcc49 RR |
1664 | int cpu = smp_processor_id(); |
1665 | struct task_struct *curr = current; | |
24933b82 | 1666 | struct tss_struct *t = &per_cpu(cpu_tss, cpu); |
62111195 | 1667 | |
ce4b1b16 | 1668 | wait_for_master_cpu(cpu); |
e6ebf5de | 1669 | |
5b2bdbc8 SR |
1670 | /* |
1671 | * Initialize the CR4 shadow before doing anything that could | |
1672 | * try to read it. | |
1673 | */ | |
1674 | cr4_init_shadow(); | |
1675 | ||
ce4b1b16 | 1676 | show_ucode_info_early(); |
62111195 | 1677 | |
1b74dde7 | 1678 | pr_info("Initializing CPU#%d\n", cpu); |
62111195 | 1679 | |
362f924b | 1680 | if (cpu_feature_enabled(X86_FEATURE_VME) || |
59e21e3d | 1681 | boot_cpu_has(X86_FEATURE_TSC) || |
362f924b | 1682 | boot_cpu_has(X86_FEATURE_DE)) |
375074cc | 1683 | cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); |
62111195 | 1684 | |
cf910e83 | 1685 | load_current_idt(); |
552be871 | 1686 | switch_to_new_gdt(cpu); |
1da177e4 | 1687 | |
1da177e4 LT |
1688 | /* |
1689 | * Set up and load the per-CPU TSS and LDT | |
1690 | */ | |
f1f10076 | 1691 | mmgrab(&init_mm); |
62111195 | 1692 | curr->active_mm = &init_mm; |
8c5dfd25 | 1693 | BUG_ON(curr->mm); |
62111195 | 1694 | enter_lazy_tlb(&init_mm, curr); |
1da177e4 | 1695 | |
475b37e7 AL |
1696 | setup_cpu_entry_area(cpu); |
1697 | ||
8c6b12e8 AL |
1698 | /* |
1699 | * Initialize the TSS. Don't bother initializing sp0, as the initial | |
1700 | * task never enters user mode. | |
1701 | */ | |
475b37e7 | 1702 | set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss); |
1da177e4 | 1703 | load_TR_desc(); |
8c6b12e8 | 1704 | |
37868fe1 | 1705 | load_mm_ldt(&init_mm); |
1da177e4 | 1706 | |
7123a5de | 1707 | t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET; |
f9a196b8 | 1708 | |
22c4e308 | 1709 | #ifdef CONFIG_DOUBLEFAULT |
1da177e4 LT |
1710 | /* Set up doublefault TSS pointer in the GDT */ |
1711 | __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss); | |
22c4e308 | 1712 | #endif |
1da177e4 | 1713 | |
9766cdbc | 1714 | clear_all_debug_regs(); |
0bb9fef9 | 1715 | dbg_restore_debug_regs(); |
1da177e4 | 1716 | |
21c4cd10 | 1717 | fpu__init_cpu(); |
69218e47 | 1718 | |
69218e47 | 1719 | load_fixmap_gdt(cpu); |
1da177e4 | 1720 | } |
1ba76586 | 1721 | #endif |
5700f743 | 1722 | |
b51ef52d LA |
1723 | static void bsp_resume(void) |
1724 | { | |
1725 | if (this_cpu->c_bsp_resume) | |
1726 | this_cpu->c_bsp_resume(&boot_cpu_data); | |
1727 | } | |
1728 | ||
1729 | static struct syscore_ops cpu_syscore_ops = { | |
1730 | .resume = bsp_resume, | |
1731 | }; | |
1732 | ||
1733 | static int __init init_cpu_syscore(void) | |
1734 | { | |
1735 | register_syscore_ops(&cpu_syscore_ops); | |
1736 | return 0; | |
1737 | } | |
1738 | core_initcall(init_cpu_syscore); |