]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/cpu/common.c
x86/xsaves: Add a kernel parameter noxsaves to disable xsaves/xrstors
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / cpu / common.c
CommitLineData
f0fc4aff 1#include <linux/bootmem.h>
9766cdbc 2#include <linux/linkage.h>
f0fc4aff 3#include <linux/bitops.h>
9766cdbc 4#include <linux/kernel.h>
f0fc4aff 5#include <linux/module.h>
9766cdbc
JSR
6#include <linux/percpu.h>
7#include <linux/string.h>
1da177e4 8#include <linux/delay.h>
9766cdbc
JSR
9#include <linux/sched.h>
10#include <linux/init.h>
11#include <linux/kgdb.h>
1da177e4 12#include <linux/smp.h>
9766cdbc
JSR
13#include <linux/io.h>
14
15#include <asm/stackprotector.h>
cdd6c482 16#include <asm/perf_event.h>
1da177e4 17#include <asm/mmu_context.h>
49d859d7 18#include <asm/archrandom.h>
9766cdbc
JSR
19#include <asm/hypervisor.h>
20#include <asm/processor.h>
f649e938 21#include <asm/debugreg.h>
9766cdbc 22#include <asm/sections.h>
8bdbd962
AC
23#include <linux/topology.h>
24#include <linux/cpumask.h>
9766cdbc 25#include <asm/pgtable.h>
60063497 26#include <linux/atomic.h>
9766cdbc
JSR
27#include <asm/proto.h>
28#include <asm/setup.h>
29#include <asm/apic.h>
30#include <asm/desc.h>
31#include <asm/i387.h>
1361b83a 32#include <asm/fpu-internal.h>
27b07da7 33#include <asm/mtrr.h>
8bdbd962 34#include <linux/numa.h>
9766cdbc
JSR
35#include <asm/asm.h>
36#include <asm/cpu.h>
a03a3e28 37#include <asm/mce.h>
9766cdbc 38#include <asm/msr.h>
8d4a4300 39#include <asm/pat.h>
d288e1cf
FY
40#include <asm/microcode.h>
41#include <asm/microcode_intel.h>
e641f5f5
IM
42
43#ifdef CONFIG_X86_LOCAL_APIC
bdbcdd48 44#include <asm/uv/uv.h>
1da177e4
LT
45#endif
46
47#include "cpu.h"
48
c2d1cec1 49/* all of these masks are initialized in setup_cpu_local_masks() */
c2d1cec1 50cpumask_var_t cpu_initialized_mask;
9766cdbc
JSR
51cpumask_var_t cpu_callout_mask;
52cpumask_var_t cpu_callin_mask;
c2d1cec1
MT
53
54/* representing cpus for which sibling maps can be computed */
55cpumask_var_t cpu_sibling_setup_mask;
56
2f2f52ba 57/* correctly size the local cpu masks */
4369f1fb 58void __init setup_cpu_local_masks(void)
2f2f52ba
BG
59{
60 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
61 alloc_bootmem_cpumask_var(&cpu_callin_mask);
62 alloc_bootmem_cpumask_var(&cpu_callout_mask);
63 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
64}
65
148f9bb8 66static void default_init(struct cpuinfo_x86 *c)
e8055139
OZ
67{
68#ifdef CONFIG_X86_64
27c13ece 69 cpu_detect_cache_sizes(c);
e8055139
OZ
70#else
71 /* Not much we can do here... */
72 /* Check if at least it has cpuid */
73 if (c->cpuid_level == -1) {
74 /* No cpuid. It must be an ancient CPU */
75 if (c->x86 == 4)
76 strcpy(c->x86_model_id, "486");
77 else if (c->x86 == 3)
78 strcpy(c->x86_model_id, "386");
79 }
80#endif
81}
82
148f9bb8 83static const struct cpu_dev default_cpu = {
e8055139
OZ
84 .c_init = default_init,
85 .c_vendor = "Unknown",
86 .c_x86_vendor = X86_VENDOR_UNKNOWN,
87};
88
148f9bb8 89static const struct cpu_dev *this_cpu = &default_cpu;
0a488a53 90
06deef89 91DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
950ad7ff 92#ifdef CONFIG_X86_64
06deef89
BG
93 /*
94 * We need valid kernel segments for data and code in long mode too
95 * IRET will check the segment types kkeil 2000/10/28
96 * Also sysret mandates a special GDT layout
97 *
9766cdbc 98 * TLS descriptors are currently at a different place compared to i386.
06deef89
BG
99 * Hopefully nobody expects them at a fixed place (Wine?)
100 */
1e5de182
AM
101 [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
102 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
103 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
104 [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
105 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
106 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
950ad7ff 107#else
1e5de182
AM
108 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
109 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
110 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
111 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
bf504672
RR
112 /*
113 * Segments used for calling PnP BIOS have byte granularity.
114 * They code segments and data segments have fixed 64k limits,
115 * the transfer segment sizes are set at run time.
116 */
6842ef0e 117 /* 32-bit code */
1e5de182 118 [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
6842ef0e 119 /* 16-bit code */
1e5de182 120 [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
6842ef0e 121 /* 16-bit data */
1e5de182 122 [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
6842ef0e 123 /* 16-bit data */
1e5de182 124 [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
6842ef0e 125 /* 16-bit data */
1e5de182 126 [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
bf504672
RR
127 /*
128 * The APM segments have byte granularity and their bases
129 * are set at run time. All have 64k limits.
130 */
6842ef0e 131 /* 32-bit code */
1e5de182 132 [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
bf504672 133 /* 16-bit code */
1e5de182 134 [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
6842ef0e 135 /* data */
72c4d853 136 [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
bf504672 137
1e5de182
AM
138 [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
139 [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
60a5317f 140 GDT_STACK_CANARY_INIT
950ad7ff 141#endif
06deef89 142} };
7a61d35d 143EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
ae1ee11b 144
0c752a93
SS
145static int __init x86_xsave_setup(char *s)
146{
147 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
6bad06b7 148 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
b6f42a4a 149 setup_clear_cpu_cap(X86_FEATURE_XSAVES);
c6fd893d
SS
150 setup_clear_cpu_cap(X86_FEATURE_AVX);
151 setup_clear_cpu_cap(X86_FEATURE_AVX2);
0c752a93
SS
152 return 1;
153}
154__setup("noxsave", x86_xsave_setup);
155
6bad06b7
SS
156static int __init x86_xsaveopt_setup(char *s)
157{
158 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
159 return 1;
160}
161__setup("noxsaveopt", x86_xsaveopt_setup);
162
b6f42a4a
FY
163static int __init x86_xsaves_setup(char *s)
164{
165 setup_clear_cpu_cap(X86_FEATURE_XSAVES);
166 return 1;
167}
168__setup("noxsaves", x86_xsaves_setup);
169
ba51dced 170#ifdef CONFIG_X86_32
148f9bb8
PG
171static int cachesize_override = -1;
172static int disable_x86_serial_nr = 1;
1da177e4 173
0a488a53
YL
174static int __init cachesize_setup(char *str)
175{
176 get_option(&str, &cachesize_override);
177 return 1;
178}
179__setup("cachesize=", cachesize_setup);
180
0a488a53
YL
181static int __init x86_fxsr_setup(char *s)
182{
183 setup_clear_cpu_cap(X86_FEATURE_FXSR);
184 setup_clear_cpu_cap(X86_FEATURE_XMM);
185 return 1;
186}
187__setup("nofxsr", x86_fxsr_setup);
188
189static int __init x86_sep_setup(char *s)
190{
191 setup_clear_cpu_cap(X86_FEATURE_SEP);
192 return 1;
193}
194__setup("nosep", x86_sep_setup);
195
196/* Standard macro to see if a specific flag is changeable */
197static inline int flag_is_changeable_p(u32 flag)
198{
199 u32 f1, f2;
200
94f6bac1
KH
201 /*
202 * Cyrix and IDT cpus allow disabling of CPUID
203 * so the code below may return different results
204 * when it is executed before and after enabling
205 * the CPUID. Add "volatile" to not allow gcc to
206 * optimize the subsequent calls to this function.
207 */
0f3fa48a
IM
208 asm volatile ("pushfl \n\t"
209 "pushfl \n\t"
210 "popl %0 \n\t"
211 "movl %0, %1 \n\t"
212 "xorl %2, %0 \n\t"
213 "pushl %0 \n\t"
214 "popfl \n\t"
215 "pushfl \n\t"
216 "popl %0 \n\t"
217 "popfl \n\t"
218
94f6bac1
KH
219 : "=&r" (f1), "=&r" (f2)
220 : "ir" (flag));
0a488a53
YL
221
222 return ((f1^f2) & flag) != 0;
223}
224
225/* Probe for the CPUID instruction */
148f9bb8 226int have_cpuid_p(void)
0a488a53
YL
227{
228 return flag_is_changeable_p(X86_EFLAGS_ID);
229}
230
148f9bb8 231static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
0a488a53 232{
0f3fa48a
IM
233 unsigned long lo, hi;
234
235 if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
236 return;
237
238 /* Disable processor serial number: */
239
240 rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
241 lo |= 0x200000;
242 wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
243
244 printk(KERN_NOTICE "CPU serial number disabled.\n");
245 clear_cpu_cap(c, X86_FEATURE_PN);
246
247 /* Disabling the serial number may affect the cpuid level */
248 c->cpuid_level = cpuid_eax(0);
0a488a53
YL
249}
250
251static int __init x86_serial_nr_setup(char *s)
252{
253 disable_x86_serial_nr = 0;
254 return 1;
255}
256__setup("serialnumber", x86_serial_nr_setup);
ba51dced 257#else
102bbe3a
YL
258static inline int flag_is_changeable_p(u32 flag)
259{
260 return 1;
261}
102bbe3a
YL
262static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
263{
264}
ba51dced 265#endif
0a488a53 266
de5397ad
FY
267static __init int setup_disable_smep(char *arg)
268{
b2cc2a07 269 setup_clear_cpu_cap(X86_FEATURE_SMEP);
de5397ad
FY
270 return 1;
271}
272__setup("nosmep", setup_disable_smep);
273
b2cc2a07 274static __always_inline void setup_smep(struct cpuinfo_x86 *c)
de5397ad 275{
b2cc2a07
PA
276 if (cpu_has(c, X86_FEATURE_SMEP))
277 set_in_cr4(X86_CR4_SMEP);
de5397ad
FY
278}
279
52b6179a
PA
280static __init int setup_disable_smap(char *arg)
281{
b2cc2a07 282 setup_clear_cpu_cap(X86_FEATURE_SMAP);
52b6179a
PA
283 return 1;
284}
285__setup("nosmap", setup_disable_smap);
286
b2cc2a07
PA
287static __always_inline void setup_smap(struct cpuinfo_x86 *c)
288{
289 unsigned long eflags;
290
291 /* This should have been cleared long ago */
292 raw_local_save_flags(eflags);
293 BUG_ON(eflags & X86_EFLAGS_AC);
294
03bbd596
PA
295 if (cpu_has(c, X86_FEATURE_SMAP)) {
296#ifdef CONFIG_X86_SMAP
b2cc2a07 297 set_in_cr4(X86_CR4_SMAP);
03bbd596
PA
298#else
299 clear_in_cr4(X86_CR4_SMAP);
300#endif
301 }
de5397ad
FY
302}
303
b38b0665
PA
304/*
305 * Some CPU features depend on higher CPUID levels, which may not always
306 * be available due to CPUID level capping or broken virtualization
307 * software. Add those features to this table to auto-disable them.
308 */
309struct cpuid_dependent_feature {
310 u32 feature;
311 u32 level;
312};
0f3fa48a 313
148f9bb8 314static const struct cpuid_dependent_feature
b38b0665
PA
315cpuid_dependent_features[] = {
316 { X86_FEATURE_MWAIT, 0x00000005 },
317 { X86_FEATURE_DCA, 0x00000009 },
318 { X86_FEATURE_XSAVE, 0x0000000d },
319 { 0, 0 }
320};
321
148f9bb8 322static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
b38b0665
PA
323{
324 const struct cpuid_dependent_feature *df;
9766cdbc 325
b38b0665 326 for (df = cpuid_dependent_features; df->feature; df++) {
0f3fa48a
IM
327
328 if (!cpu_has(c, df->feature))
329 continue;
b38b0665
PA
330 /*
331 * Note: cpuid_level is set to -1 if unavailable, but
332 * extended_extended_level is set to 0 if unavailable
333 * and the legitimate extended levels are all negative
334 * when signed; hence the weird messing around with
335 * signs here...
336 */
0f3fa48a 337 if (!((s32)df->level < 0 ?
f6db44df 338 (u32)df->level > (u32)c->extended_cpuid_level :
0f3fa48a
IM
339 (s32)df->level > (s32)c->cpuid_level))
340 continue;
341
342 clear_cpu_cap(c, df->feature);
343 if (!warn)
344 continue;
345
346 printk(KERN_WARNING
347 "CPU: CPU feature %s disabled, no CPUID level 0x%x\n",
348 x86_cap_flags[df->feature], df->level);
b38b0665 349 }
f6db44df 350}
b38b0665 351
102bbe3a
YL
352/*
353 * Naming convention should be: <Name> [(<Codename>)]
354 * This table only is used unless init_<vendor>() below doesn't set it;
0f3fa48a
IM
355 * in particular, if CPUID levels 0x80000002..4 are supported, this
356 * isn't used
102bbe3a
YL
357 */
358
359/* Look up CPU names by table lookup. */
148f9bb8 360static const char *table_lookup_model(struct cpuinfo_x86 *c)
102bbe3a 361{
09dc68d9
JB
362#ifdef CONFIG_X86_32
363 const struct legacy_cpu_model_info *info;
102bbe3a
YL
364
365 if (c->x86_model >= 16)
366 return NULL; /* Range check */
367
368 if (!this_cpu)
369 return NULL;
370
09dc68d9 371 info = this_cpu->legacy_models;
102bbe3a 372
09dc68d9 373 while (info->family) {
102bbe3a
YL
374 if (info->family == c->x86)
375 return info->model_names[c->x86_model];
376 info++;
377 }
09dc68d9 378#endif
102bbe3a
YL
379 return NULL; /* Not found */
380}
381
148f9bb8
PG
382__u32 cpu_caps_cleared[NCAPINTS];
383__u32 cpu_caps_set[NCAPINTS];
7d851c8d 384
11e3a840
JF
385void load_percpu_segment(int cpu)
386{
387#ifdef CONFIG_X86_32
388 loadsegment(fs, __KERNEL_PERCPU);
389#else
390 loadsegment(gs, 0);
391 wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
392#endif
60a5317f 393 load_stack_canary_segment();
11e3a840
JF
394}
395
0f3fa48a
IM
396/*
397 * Current gdt points %fs at the "master" per-cpu area: after this,
398 * it's on the real one.
399 */
552be871 400void switch_to_new_gdt(int cpu)
9d31d35b
YL
401{
402 struct desc_ptr gdt_descr;
403
2697fbd5 404 gdt_descr.address = (long)get_cpu_gdt_table(cpu);
9d31d35b
YL
405 gdt_descr.size = GDT_SIZE - 1;
406 load_gdt(&gdt_descr);
2697fbd5 407 /* Reload the per-cpu base */
11e3a840
JF
408
409 load_percpu_segment(cpu);
9d31d35b
YL
410}
411
148f9bb8 412static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
1da177e4 413
148f9bb8 414static void get_model_name(struct cpuinfo_x86 *c)
1da177e4
LT
415{
416 unsigned int *v;
417 char *p, *q;
418
3da99c97 419 if (c->extended_cpuid_level < 0x80000004)
1b05d60d 420 return;
1da177e4 421
0f3fa48a 422 v = (unsigned int *)c->x86_model_id;
1da177e4
LT
423 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
424 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
425 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
426 c->x86_model_id[48] = 0;
427
0f3fa48a
IM
428 /*
429 * Intel chips right-justify this string for some dumb reason;
430 * undo that brain damage:
431 */
1da177e4 432 p = q = &c->x86_model_id[0];
34048c9e 433 while (*p == ' ')
9766cdbc 434 p++;
34048c9e 435 if (p != q) {
9766cdbc
JSR
436 while (*p)
437 *q++ = *p++;
438 while (q <= &c->x86_model_id[48])
439 *q++ = '\0'; /* Zero-pad the rest */
1da177e4 440 }
1da177e4
LT
441}
442
148f9bb8 443void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
1da177e4 444{
9d31d35b 445 unsigned int n, dummy, ebx, ecx, edx, l2size;
1da177e4 446
3da99c97 447 n = c->extended_cpuid_level;
1da177e4
LT
448
449 if (n >= 0x80000005) {
9d31d35b 450 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
9d31d35b 451 c->x86_cache_size = (ecx>>24) + (edx>>24);
140fc727
YL
452#ifdef CONFIG_X86_64
453 /* On K8 L1 TLB is inclusive, so don't count it */
454 c->x86_tlbsize = 0;
455#endif
1da177e4
LT
456 }
457
458 if (n < 0x80000006) /* Some chips just has a large L1. */
459 return;
460
0a488a53 461 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
1da177e4 462 l2size = ecx >> 16;
34048c9e 463
140fc727
YL
464#ifdef CONFIG_X86_64
465 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
466#else
1da177e4 467 /* do processor-specific cache resizing */
09dc68d9
JB
468 if (this_cpu->legacy_cache_size)
469 l2size = this_cpu->legacy_cache_size(c, l2size);
1da177e4
LT
470
471 /* Allow user to override all this if necessary. */
472 if (cachesize_override != -1)
473 l2size = cachesize_override;
474
34048c9e 475 if (l2size == 0)
1da177e4 476 return; /* Again, no L2 cache is possible */
140fc727 477#endif
1da177e4
LT
478
479 c->x86_cache_size = l2size;
1da177e4
LT
480}
481
e0ba94f1
AS
482u16 __read_mostly tlb_lli_4k[NR_INFO];
483u16 __read_mostly tlb_lli_2m[NR_INFO];
484u16 __read_mostly tlb_lli_4m[NR_INFO];
485u16 __read_mostly tlb_lld_4k[NR_INFO];
486u16 __read_mostly tlb_lld_2m[NR_INFO];
487u16 __read_mostly tlb_lld_4m[NR_INFO];
dd360393 488u16 __read_mostly tlb_lld_1g[NR_INFO];
e0ba94f1 489
c4211f42
AS
490/*
491 * tlb_flushall_shift shows the balance point in replacing cr3 write
492 * with multiple 'invlpg'. It will do this replacement when
493 * flush_tlb_lines <= active_lines/2^tlb_flushall_shift.
494 * If tlb_flushall_shift is -1, means the replacement will be disabled.
495 */
496s8 __read_mostly tlb_flushall_shift = -1;
497
148f9bb8 498void cpu_detect_tlb(struct cpuinfo_x86 *c)
e0ba94f1
AS
499{
500 if (this_cpu->c_detect_tlb)
501 this_cpu->c_detect_tlb(c);
502
dd360393
KS
503 printk(KERN_INFO "Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n"
504 "Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n"
a9ad773e 505 "tlb_flushall_shift: %d\n",
e0ba94f1
AS
506 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
507 tlb_lli_4m[ENTRIES], tlb_lld_4k[ENTRIES],
c4211f42 508 tlb_lld_2m[ENTRIES], tlb_lld_4m[ENTRIES],
dd360393 509 tlb_lld_1g[ENTRIES], tlb_flushall_shift);
e0ba94f1
AS
510}
511
148f9bb8 512void detect_ht(struct cpuinfo_x86 *c)
1da177e4 513{
97e4db7c 514#ifdef CONFIG_X86_HT
0a488a53
YL
515 u32 eax, ebx, ecx, edx;
516 int index_msb, core_bits;
2eaad1fd 517 static bool printed;
1da177e4 518
0a488a53 519 if (!cpu_has(c, X86_FEATURE_HT))
9d31d35b 520 return;
1da177e4 521
0a488a53
YL
522 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
523 goto out;
1da177e4 524
1cd78776
YL
525 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
526 return;
1da177e4 527
0a488a53 528 cpuid(1, &eax, &ebx, &ecx, &edx);
1da177e4 529
9d31d35b
YL
530 smp_num_siblings = (ebx & 0xff0000) >> 16;
531
532 if (smp_num_siblings == 1) {
2eaad1fd 533 printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
0f3fa48a
IM
534 goto out;
535 }
9d31d35b 536
0f3fa48a
IM
537 if (smp_num_siblings <= 1)
538 goto out;
9d31d35b 539
0f3fa48a
IM
540 index_msb = get_count_order(smp_num_siblings);
541 c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
9d31d35b 542
0f3fa48a 543 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
9d31d35b 544
0f3fa48a 545 index_msb = get_count_order(smp_num_siblings);
9d31d35b 546
0f3fa48a 547 core_bits = get_count_order(c->x86_max_cores);
9d31d35b 548
0f3fa48a
IM
549 c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
550 ((1 << core_bits) - 1);
1da177e4 551
0a488a53 552out:
2eaad1fd 553 if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
0a488a53
YL
554 printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
555 c->phys_proc_id);
556 printk(KERN_INFO "CPU: Processor Core ID: %d\n",
557 c->cpu_core_id);
2eaad1fd 558 printed = 1;
9d31d35b 559 }
9d31d35b 560#endif
97e4db7c 561}
1da177e4 562
148f9bb8 563static void get_cpu_vendor(struct cpuinfo_x86 *c)
1da177e4
LT
564{
565 char *v = c->x86_vendor_id;
0f3fa48a 566 int i;
1da177e4
LT
567
568 for (i = 0; i < X86_VENDOR_NUM; i++) {
10a434fc
YL
569 if (!cpu_devs[i])
570 break;
571
572 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
573 (cpu_devs[i]->c_ident[1] &&
574 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
0f3fa48a 575
10a434fc
YL
576 this_cpu = cpu_devs[i];
577 c->x86_vendor = this_cpu->c_x86_vendor;
578 return;
1da177e4
LT
579 }
580 }
10a434fc 581
a9c56953
MK
582 printk_once(KERN_ERR
583 "CPU: vendor_id '%s' unknown, using generic init.\n" \
584 "CPU: Your system may be unstable.\n", v);
10a434fc 585
fe38d855
CE
586 c->x86_vendor = X86_VENDOR_UNKNOWN;
587 this_cpu = &default_cpu;
1da177e4
LT
588}
589
148f9bb8 590void cpu_detect(struct cpuinfo_x86 *c)
1da177e4 591{
1da177e4 592 /* Get vendor name */
4a148513
HH
593 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
594 (unsigned int *)&c->x86_vendor_id[0],
595 (unsigned int *)&c->x86_vendor_id[8],
596 (unsigned int *)&c->x86_vendor_id[4]);
1da177e4 597
1da177e4 598 c->x86 = 4;
9d31d35b 599 /* Intel-defined flags: level 0x00000001 */
1da177e4
LT
600 if (c->cpuid_level >= 0x00000001) {
601 u32 junk, tfms, cap0, misc;
0f3fa48a 602
1da177e4 603 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
9d31d35b
YL
604 c->x86 = (tfms >> 8) & 0xf;
605 c->x86_model = (tfms >> 4) & 0xf;
606 c->x86_mask = tfms & 0xf;
0f3fa48a 607
f5f786d0 608 if (c->x86 == 0xf)
1da177e4 609 c->x86 += (tfms >> 20) & 0xff;
f5f786d0 610 if (c->x86 >= 0x6)
9d31d35b 611 c->x86_model += ((tfms >> 16) & 0xf) << 4;
0f3fa48a 612
d4387bd3 613 if (cap0 & (1<<19)) {
d4387bd3 614 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
9d31d35b 615 c->x86_cache_alignment = c->x86_clflush_size;
d4387bd3 616 }
1da177e4 617 }
1da177e4 618}
3da99c97 619
148f9bb8 620void get_cpu_cap(struct cpuinfo_x86 *c)
093af8d7
YL
621{
622 u32 tfms, xlvl;
3da99c97 623 u32 ebx;
093af8d7 624
3da99c97
YL
625 /* Intel-defined flags: level 0x00000001 */
626 if (c->cpuid_level >= 0x00000001) {
627 u32 capability, excap;
0f3fa48a 628
3da99c97
YL
629 cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
630 c->x86_capability[0] = capability;
631 c->x86_capability[4] = excap;
632 }
093af8d7 633
bdc802dc
PA
634 /* Additional Intel-defined flags: level 0x00000007 */
635 if (c->cpuid_level >= 0x00000007) {
636 u32 eax, ebx, ecx, edx;
637
638 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
639
2494b030 640 c->x86_capability[9] = ebx;
bdc802dc
PA
641 }
642
6229ad27
FY
643 /* Extended state features: level 0x0000000d */
644 if (c->cpuid_level >= 0x0000000d) {
645 u32 eax, ebx, ecx, edx;
646
647 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
648
649 c->x86_capability[10] = eax;
650 }
651
3da99c97
YL
652 /* AMD-defined flags: level 0x80000001 */
653 xlvl = cpuid_eax(0x80000000);
654 c->extended_cpuid_level = xlvl;
0f3fa48a 655
3da99c97
YL
656 if ((xlvl & 0xffff0000) == 0x80000000) {
657 if (xlvl >= 0x80000001) {
658 c->x86_capability[1] = cpuid_edx(0x80000001);
659 c->x86_capability[6] = cpuid_ecx(0x80000001);
093af8d7 660 }
093af8d7 661 }
093af8d7 662
5122c890
YL
663 if (c->extended_cpuid_level >= 0x80000008) {
664 u32 eax = cpuid_eax(0x80000008);
665
666 c->x86_virt_bits = (eax >> 8) & 0xff;
667 c->x86_phys_bits = eax & 0xff;
093af8d7 668 }
13c6c532
JB
669#ifdef CONFIG_X86_32
670 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
671 c->x86_phys_bits = 36;
5122c890 672#endif
e3224234
YL
673
674 if (c->extended_cpuid_level >= 0x80000007)
675 c->x86_power = cpuid_edx(0x80000007);
093af8d7 676
1dedefd1 677 init_scattered_cpuid_features(c);
093af8d7 678}
1da177e4 679
148f9bb8 680static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
aef93c8b
YL
681{
682#ifdef CONFIG_X86_32
683 int i;
684
685 /*
686 * First of all, decide if this is a 486 or higher
687 * It's a 486 if we can modify the AC flag
688 */
689 if (flag_is_changeable_p(X86_EFLAGS_AC))
690 c->x86 = 4;
691 else
692 c->x86 = 3;
693
694 for (i = 0; i < X86_VENDOR_NUM; i++)
695 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
696 c->x86_vendor_id[0] = 0;
697 cpu_devs[i]->c_identify(c);
698 if (c->x86_vendor_id[0]) {
699 get_cpu_vendor(c);
700 break;
701 }
702 }
703#endif
704}
705
34048c9e
PC
706/*
707 * Do minimum CPU detection early.
708 * Fields really needed: vendor, cpuid_level, family, model, mask,
709 * cache alignment.
710 * The others are not touched to avoid unwanted side effects.
711 *
712 * WARNING: this function is only called on the BP. Don't add code here
713 * that is supposed to run on all CPUs.
714 */
3da99c97 715static void __init early_identify_cpu(struct cpuinfo_x86 *c)
d7cd5611 716{
6627d242
YL
717#ifdef CONFIG_X86_64
718 c->x86_clflush_size = 64;
13c6c532
JB
719 c->x86_phys_bits = 36;
720 c->x86_virt_bits = 48;
6627d242 721#else
d4387bd3 722 c->x86_clflush_size = 32;
13c6c532
JB
723 c->x86_phys_bits = 32;
724 c->x86_virt_bits = 32;
6627d242 725#endif
0a488a53 726 c->x86_cache_alignment = c->x86_clflush_size;
d7cd5611 727
3da99c97 728 memset(&c->x86_capability, 0, sizeof c->x86_capability);
0a488a53 729 c->extended_cpuid_level = 0;
d7cd5611 730
aef93c8b
YL
731 if (!have_cpuid_p())
732 identify_cpu_without_cpuid(c);
733
734 /* cyrix could have cpuid enabled via c_identify()*/
d7cd5611
RR
735 if (!have_cpuid_p())
736 return;
737
738 cpu_detect(c);
3da99c97 739 get_cpu_vendor(c);
3da99c97 740 get_cpu_cap(c);
60e019eb 741 fpu_detect(c);
12cf105c 742
10a434fc
YL
743 if (this_cpu->c_early_init)
744 this_cpu->c_early_init(c);
093af8d7 745
f6e9456c 746 c->cpu_index = 0;
b38b0665 747 filter_cpuid_features(c, false);
de5397ad 748
a110b5ec
BP
749 if (this_cpu->c_bsp_init)
750 this_cpu->c_bsp_init(c);
c3b83598
BP
751
752 setup_force_cpu_cap(X86_FEATURE_ALWAYS);
d7cd5611
RR
753}
754
9d31d35b
YL
755void __init early_cpu_init(void)
756{
02dde8b4 757 const struct cpu_dev *const *cdev;
10a434fc
YL
758 int count = 0;
759
ac23f253 760#ifdef CONFIG_PROCESSOR_SELECT
9766cdbc 761 printk(KERN_INFO "KERNEL supported cpus:\n");
31c997ca
IM
762#endif
763
10a434fc 764 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
02dde8b4 765 const struct cpu_dev *cpudev = *cdev;
9d31d35b 766
10a434fc
YL
767 if (count >= X86_VENDOR_NUM)
768 break;
769 cpu_devs[count] = cpudev;
770 count++;
771
ac23f253 772#ifdef CONFIG_PROCESSOR_SELECT
31c997ca
IM
773 {
774 unsigned int j;
775
776 for (j = 0; j < 2; j++) {
777 if (!cpudev->c_ident[j])
778 continue;
779 printk(KERN_INFO " %s %s\n", cpudev->c_vendor,
780 cpudev->c_ident[j]);
781 }
10a434fc 782 }
0388423d 783#endif
10a434fc 784 }
9d31d35b 785 early_identify_cpu(&boot_cpu_data);
d7cd5611 786}
093af8d7 787
b6734c35 788/*
366d4a43
BP
789 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
790 * unfortunately, that's not true in practice because of early VIA
791 * chips and (more importantly) broken virtualizers that are not easy
792 * to detect. In the latter case it doesn't even *fail* reliably, so
793 * probing for it doesn't even work. Disable it completely on 32-bit
ba0593bf 794 * unless we can find a reliable way to detect all the broken cases.
366d4a43 795 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
b6734c35 796 */
148f9bb8 797static void detect_nopl(struct cpuinfo_x86 *c)
b6734c35 798{
366d4a43 799#ifdef CONFIG_X86_32
b6734c35 800 clear_cpu_cap(c, X86_FEATURE_NOPL);
366d4a43
BP
801#else
802 set_cpu_cap(c, X86_FEATURE_NOPL);
803#endif
d7cd5611
RR
804}
805
148f9bb8 806static void generic_identify(struct cpuinfo_x86 *c)
1da177e4 807{
aef93c8b 808 c->extended_cpuid_level = 0;
1da177e4 809
3da99c97 810 if (!have_cpuid_p())
aef93c8b 811 identify_cpu_without_cpuid(c);
1d67953f 812
aef93c8b 813 /* cyrix could have cpuid enabled via c_identify()*/
a9853dd6 814 if (!have_cpuid_p())
aef93c8b 815 return;
1da177e4 816
3da99c97 817 cpu_detect(c);
1da177e4 818
3da99c97 819 get_cpu_vendor(c);
1da177e4 820
3da99c97 821 get_cpu_cap(c);
1da177e4 822
3da99c97
YL
823 if (c->cpuid_level >= 0x00000001) {
824 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
b89d3b3e
YL
825#ifdef CONFIG_X86_32
826# ifdef CONFIG_X86_HT
cb8cc442 827 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
b89d3b3e 828# else
3da99c97 829 c->apicid = c->initial_apicid;
b89d3b3e
YL
830# endif
831#endif
b89d3b3e 832 c->phys_proc_id = c->initial_apicid;
3da99c97 833 }
1da177e4 834
1b05d60d 835 get_model_name(c); /* Default name */
1da177e4 836
3da99c97 837 detect_nopl(c);
1da177e4 838}
1da177e4
LT
839
840/*
841 * This does the hard work of actually picking apart the CPU stuff...
842 */
148f9bb8 843static void identify_cpu(struct cpuinfo_x86 *c)
1da177e4
LT
844{
845 int i;
846
847 c->loops_per_jiffy = loops_per_jiffy;
848 c->x86_cache_size = -1;
849 c->x86_vendor = X86_VENDOR_UNKNOWN;
1da177e4
LT
850 c->x86_model = c->x86_mask = 0; /* So far unknown... */
851 c->x86_vendor_id[0] = '\0'; /* Unset */
852 c->x86_model_id[0] = '\0'; /* Unset */
94605eff 853 c->x86_max_cores = 1;
102bbe3a 854 c->x86_coreid_bits = 0;
11fdd252 855#ifdef CONFIG_X86_64
102bbe3a 856 c->x86_clflush_size = 64;
13c6c532
JB
857 c->x86_phys_bits = 36;
858 c->x86_virt_bits = 48;
102bbe3a
YL
859#else
860 c->cpuid_level = -1; /* CPUID not detected */
770d132f 861 c->x86_clflush_size = 32;
13c6c532
JB
862 c->x86_phys_bits = 32;
863 c->x86_virt_bits = 32;
102bbe3a
YL
864#endif
865 c->x86_cache_alignment = c->x86_clflush_size;
1da177e4
LT
866 memset(&c->x86_capability, 0, sizeof c->x86_capability);
867
1da177e4
LT
868 generic_identify(c);
869
3898534d 870 if (this_cpu->c_identify)
1da177e4
LT
871 this_cpu->c_identify(c);
872
2759c328
YL
873 /* Clear/Set all flags overriden by options, after probe */
874 for (i = 0; i < NCAPINTS; i++) {
875 c->x86_capability[i] &= ~cpu_caps_cleared[i];
876 c->x86_capability[i] |= cpu_caps_set[i];
877 }
878
102bbe3a 879#ifdef CONFIG_X86_64
cb8cc442 880 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
102bbe3a
YL
881#endif
882
1da177e4
LT
883 /*
884 * Vendor-specific initialization. In this section we
885 * canonicalize the feature flags, meaning if there are
886 * features a certain CPU supports which CPUID doesn't
887 * tell us, CPUID claiming incorrect flags, or other bugs,
888 * we handle them here.
889 *
890 * At the end of this section, c->x86_capability better
891 * indicate the features this CPU genuinely supports!
892 */
893 if (this_cpu->c_init)
894 this_cpu->c_init(c);
895
896 /* Disable the PN if appropriate */
897 squash_the_stupid_serial_number(c);
898
b2cc2a07
PA
899 /* Set up SMEP/SMAP */
900 setup_smep(c);
901 setup_smap(c);
902
1da177e4 903 /*
0f3fa48a
IM
904 * The vendor-specific functions might have changed features.
905 * Now we do "generic changes."
1da177e4
LT
906 */
907
b38b0665
PA
908 /* Filter out anything that depends on CPUID levels we don't have */
909 filter_cpuid_features(c, true);
910
1da177e4 911 /* If the model name is still unset, do table lookup. */
34048c9e 912 if (!c->x86_model_id[0]) {
02dde8b4 913 const char *p;
1da177e4 914 p = table_lookup_model(c);
34048c9e 915 if (p)
1da177e4
LT
916 strcpy(c->x86_model_id, p);
917 else
918 /* Last resort... */
919 sprintf(c->x86_model_id, "%02x/%02x",
54a20f8c 920 c->x86, c->x86_model);
1da177e4
LT
921 }
922
102bbe3a
YL
923#ifdef CONFIG_X86_64
924 detect_ht(c);
925#endif
926
88b094fb 927 init_hypervisor(c);
49d859d7 928 x86_init_rdrand(c);
3e0c3737
YL
929
930 /*
931 * Clear/Set all flags overriden by options, need do it
932 * before following smp all cpus cap AND.
933 */
934 for (i = 0; i < NCAPINTS; i++) {
935 c->x86_capability[i] &= ~cpu_caps_cleared[i];
936 c->x86_capability[i] |= cpu_caps_set[i];
937 }
938
1da177e4
LT
939 /*
940 * On SMP, boot_cpu_data holds the common feature set between
941 * all CPUs; so make sure that we indicate which features are
942 * common between the CPUs. The first time this routine gets
943 * executed, c == &boot_cpu_data.
944 */
34048c9e 945 if (c != &boot_cpu_data) {
1da177e4 946 /* AND the already accumulated flags with these */
9d31d35b 947 for (i = 0; i < NCAPINTS; i++)
1da177e4 948 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
65fc985b
BP
949
950 /* OR, i.e. replicate the bug flags */
951 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
952 c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
1da177e4
LT
953 }
954
955 /* Init Machine Check Exception if available. */
5e09954a 956 mcheck_cpu_init(c);
30d432df
AK
957
958 select_idle_routine(c);
102bbe3a 959
de2d9445 960#ifdef CONFIG_NUMA
102bbe3a
YL
961 numa_add_cpu(smp_processor_id());
962#endif
a6c4e076 963}
31ab269a 964
e04d645f
GC
965#ifdef CONFIG_X86_64
966static void vgetcpu_set_mode(void)
967{
968 if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
969 vgetcpu_mode = VGETCPU_RDTSCP;
970 else
971 vgetcpu_mode = VGETCPU_LSL;
972}
973#endif
974
a6c4e076
JF
975void __init identify_boot_cpu(void)
976{
977 identify_cpu(&boot_cpu_data);
02c68a02 978 init_amd_e400_c1e_mask();
102bbe3a 979#ifdef CONFIG_X86_32
a6c4e076 980 sysenter_setup();
6fe940d6 981 enable_sep_cpu();
e04d645f
GC
982#else
983 vgetcpu_set_mode();
102bbe3a 984#endif
5b556332 985 cpu_detect_tlb(&boot_cpu_data);
a6c4e076 986}
3b520b23 987
148f9bb8 988void identify_secondary_cpu(struct cpuinfo_x86 *c)
a6c4e076
JF
989{
990 BUG_ON(c == &boot_cpu_data);
991 identify_cpu(c);
102bbe3a 992#ifdef CONFIG_X86_32
a6c4e076 993 enable_sep_cpu();
102bbe3a 994#endif
a6c4e076 995 mtrr_ap_init();
1da177e4
LT
996}
997
a0854a46 998struct msr_range {
0f3fa48a
IM
999 unsigned min;
1000 unsigned max;
a0854a46 1001};
1da177e4 1002
148f9bb8 1003static const struct msr_range msr_range_array[] = {
a0854a46
YL
1004 { 0x00000000, 0x00000418},
1005 { 0xc0000000, 0xc000040b},
1006 { 0xc0010000, 0xc0010142},
1007 { 0xc0011000, 0xc001103b},
1008};
1da177e4 1009
148f9bb8 1010static void __print_cpu_msr(void)
a0854a46 1011{
0f3fa48a 1012 unsigned index_min, index_max;
a0854a46
YL
1013 unsigned index;
1014 u64 val;
1015 int i;
a0854a46
YL
1016
1017 for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
1018 index_min = msr_range_array[i].min;
1019 index_max = msr_range_array[i].max;
0f3fa48a 1020
a0854a46 1021 for (index = index_min; index < index_max; index++) {
ecd431d9 1022 if (rdmsrl_safe(index, &val))
a0854a46
YL
1023 continue;
1024 printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
1da177e4 1025 }
a0854a46
YL
1026 }
1027}
94605eff 1028
148f9bb8 1029static int show_msr;
0f3fa48a 1030
a0854a46
YL
1031static __init int setup_show_msr(char *arg)
1032{
1033 int num;
3dd9d514 1034
a0854a46 1035 get_option(&arg, &num);
3dd9d514 1036
a0854a46
YL
1037 if (num > 0)
1038 show_msr = num;
1039 return 1;
1da177e4 1040}
a0854a46 1041__setup("show_msr=", setup_show_msr);
1da177e4 1042
191679fd
AK
1043static __init int setup_noclflush(char *arg)
1044{
840d2830 1045 setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
da4aaa7d 1046 setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
191679fd
AK
1047 return 1;
1048}
1049__setup("noclflush", setup_noclflush);
1050
148f9bb8 1051void print_cpu_info(struct cpuinfo_x86 *c)
1da177e4 1052{
02dde8b4 1053 const char *vendor = NULL;
1da177e4 1054
0f3fa48a 1055 if (c->x86_vendor < X86_VENDOR_NUM) {
1da177e4 1056 vendor = this_cpu->c_vendor;
0f3fa48a
IM
1057 } else {
1058 if (c->cpuid_level >= 0)
1059 vendor = c->x86_vendor_id;
1060 }
1da177e4 1061
bd32a8cf 1062 if (vendor && !strstr(c->x86_model_id, vendor))
9d31d35b 1063 printk(KERN_CONT "%s ", vendor);
1da177e4 1064
9d31d35b 1065 if (c->x86_model_id[0])
924e101a 1066 printk(KERN_CONT "%s", strim(c->x86_model_id));
1da177e4 1067 else
9d31d35b 1068 printk(KERN_CONT "%d86", c->x86);
1da177e4 1069
924e101a
BP
1070 printk(KERN_CONT " (fam: %02x, model: %02x", c->x86, c->x86_model);
1071
34048c9e 1072 if (c->x86_mask || c->cpuid_level >= 0)
924e101a 1073 printk(KERN_CONT ", stepping: %02x)\n", c->x86_mask);
1da177e4 1074 else
924e101a 1075 printk(KERN_CONT ")\n");
a0854a46 1076
0b8b8078 1077 print_cpu_msr(c);
21c3fcf3
YL
1078}
1079
148f9bb8 1080void print_cpu_msr(struct cpuinfo_x86 *c)
21c3fcf3 1081{
a0854a46 1082 if (c->cpu_index < show_msr)
21c3fcf3 1083 __print_cpu_msr();
1da177e4
LT
1084}
1085
ac72e788
AK
1086static __init int setup_disablecpuid(char *arg)
1087{
1088 int bit;
0f3fa48a 1089
ac72e788
AK
1090 if (get_option(&arg, &bit) && bit < NCAPINTS*32)
1091 setup_clear_cpu_cap(bit);
1092 else
1093 return 0;
0f3fa48a 1094
ac72e788
AK
1095 return 1;
1096}
1097__setup("clearcpuid=", setup_disablecpuid);
1098
198d208d
SR
1099DEFINE_PER_CPU(unsigned long, kernel_stack) =
1100 (unsigned long)&init_thread_union - KERNEL_STACK_OFFSET + THREAD_SIZE;
1101EXPORT_PER_CPU_SYMBOL(kernel_stack);
1102
d5494d4f 1103#ifdef CONFIG_X86_64
9ff80942 1104struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
629f4f9d
SA
1105struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
1106 (unsigned long) debug_idt_table };
d5494d4f 1107
947e76cd 1108DEFINE_PER_CPU_FIRST(union irq_stack_union,
277d5b40 1109 irq_stack_union) __aligned(PAGE_SIZE) __visible;
0f3fa48a 1110
bdf977b3
TH
1111/*
1112 * The following four percpu variables are hot. Align current_task to
1113 * cacheline size such that all four fall in the same cacheline.
1114 */
1115DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1116 &init_task;
1117EXPORT_PER_CPU_SYMBOL(current_task);
d5494d4f 1118
bdf977b3
TH
1119DEFINE_PER_CPU(char *, irq_stack_ptr) =
1120 init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
1121
277d5b40 1122DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
d5494d4f 1123
c2daa3be
PZ
1124DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1125EXPORT_PER_CPU_SYMBOL(__preempt_count);
1126
7e16838d
LT
1127DEFINE_PER_CPU(struct task_struct *, fpu_owner_task);
1128
0f3fa48a
IM
1129/*
1130 * Special IST stacks which the CPU switches to when it calls
1131 * an IST-marked descriptor entry. Up to 7 stacks (hardware
1132 * limit), all of them are 4K, except the debug stack which
1133 * is 8K.
1134 */
1135static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
1136 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
1137 [DEBUG_STACK - 1] = DEBUG_STKSZ
1138};
1139
92d65b23 1140static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
3e352aa8 1141 [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
d5494d4f 1142
d5494d4f
YL
1143/* May not be marked __init: used by software suspend */
1144void syscall_init(void)
1da177e4 1145{
d5494d4f
YL
1146 /*
1147 * LSTAR and STAR live in a bit strange symbiosis.
1148 * They both write to the same internal register. STAR allows to
1149 * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
1150 */
1151 wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
1152 wrmsrl(MSR_LSTAR, system_call);
1153 wrmsrl(MSR_CSTAR, ignore_sysret);
03ae5768 1154
d5494d4f
YL
1155#ifdef CONFIG_IA32_EMULATION
1156 syscall32_cpu_init();
1157#endif
03ae5768 1158
d5494d4f
YL
1159 /* Flags to clear on syscall */
1160 wrmsrl(MSR_SYSCALL_MASK,
63bcff2a
PA
1161 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
1162 X86_EFLAGS_IOPL|X86_EFLAGS_AC);
1da177e4 1163}
62111195 1164
d5494d4f
YL
1165/*
1166 * Copies of the original ist values from the tss are only accessed during
1167 * debugging, no special alignment required.
1168 */
1169DEFINE_PER_CPU(struct orig_ist, orig_ist);
1170
228bdaa9 1171static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
42181186 1172DEFINE_PER_CPU(int, debug_stack_usage);
228bdaa9
SR
1173
1174int is_debug_stack(unsigned long addr)
1175{
42181186
SR
1176 return __get_cpu_var(debug_stack_usage) ||
1177 (addr <= __get_cpu_var(debug_stack_addr) &&
1178 addr > (__get_cpu_var(debug_stack_addr) - DEBUG_STKSZ));
228bdaa9
SR
1179}
1180
629f4f9d 1181DEFINE_PER_CPU(u32, debug_idt_ctr);
f8988175 1182
228bdaa9
SR
1183void debug_stack_set_zero(void)
1184{
629f4f9d
SA
1185 this_cpu_inc(debug_idt_ctr);
1186 load_current_idt();
228bdaa9
SR
1187}
1188
1189void debug_stack_reset(void)
1190{
629f4f9d 1191 if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
f8988175 1192 return;
629f4f9d
SA
1193 if (this_cpu_dec_return(debug_idt_ctr) == 0)
1194 load_current_idt();
228bdaa9
SR
1195}
1196
0f3fa48a 1197#else /* CONFIG_X86_64 */
d5494d4f 1198
bdf977b3
TH
1199DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1200EXPORT_PER_CPU_SYMBOL(current_task);
c2daa3be
PZ
1201DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1202EXPORT_PER_CPU_SYMBOL(__preempt_count);
27e74da9 1203DEFINE_PER_CPU(struct task_struct *, fpu_owner_task);
bdf977b3 1204
60a5317f 1205#ifdef CONFIG_CC_STACKPROTECTOR
53f82452 1206DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
60a5317f 1207#endif
d5494d4f 1208
0f3fa48a 1209#endif /* CONFIG_X86_64 */
c5413fbe 1210
9766cdbc
JSR
1211/*
1212 * Clear all 6 debug registers:
1213 */
1214static void clear_all_debug_regs(void)
1215{
1216 int i;
1217
1218 for (i = 0; i < 8; i++) {
1219 /* Ignore db4, db5 */
1220 if ((i == 4) || (i == 5))
1221 continue;
1222
1223 set_debugreg(0, i);
1224 }
1225}
c5413fbe 1226
0bb9fef9
JW
1227#ifdef CONFIG_KGDB
1228/*
1229 * Restore debug regs if using kgdbwait and you have a kernel debugger
1230 * connection established.
1231 */
1232static void dbg_restore_debug_regs(void)
1233{
1234 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1235 arch_kgdb_ops.correct_hw_break();
1236}
1237#else /* ! CONFIG_KGDB */
1238#define dbg_restore_debug_regs()
1239#endif /* ! CONFIG_KGDB */
1240
d2cbcc49
RR
1241/*
1242 * cpu_init() initializes state that is per-CPU. Some data is already
1243 * initialized (naturally) in the bootstrap process, such as the GDT
1244 * and IDT. We reload them nevertheless, this function acts as a
1245 * 'CPU state barrier', nothing should get across.
1ba76586 1246 * A lot of state is already set up in PDA init for 64 bit
d2cbcc49 1247 */
1ba76586 1248#ifdef CONFIG_X86_64
0f3fa48a 1249
148f9bb8 1250void cpu_init(void)
1ba76586 1251{
0fe1e009 1252 struct orig_ist *oist;
1ba76586 1253 struct task_struct *me;
0f3fa48a
IM
1254 struct tss_struct *t;
1255 unsigned long v;
1256 int cpu;
1ba76586
YL
1257 int i;
1258
e6ebf5de
FY
1259 /*
1260 * Load microcode on this cpu if a valid microcode is available.
1261 * This is early microcode loading procedure.
1262 */
1263 load_ucode_ap();
1264
0f3fa48a
IM
1265 cpu = stack_smp_processor_id();
1266 t = &per_cpu(init_tss, cpu);
0fe1e009 1267 oist = &per_cpu(orig_ist, cpu);
0f3fa48a 1268
e7a22c1e 1269#ifdef CONFIG_NUMA
27fd185f 1270 if (this_cpu_read(numa_node) == 0 &&
e534c7c5
LS
1271 early_cpu_to_node(cpu) != NUMA_NO_NODE)
1272 set_numa_node(early_cpu_to_node(cpu));
e7a22c1e 1273#endif
1ba76586
YL
1274
1275 me = current;
1276
c2d1cec1 1277 if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask))
1ba76586
YL
1278 panic("CPU#%d already initialized!\n", cpu);
1279
2eaad1fd 1280 pr_debug("Initializing CPU#%d\n", cpu);
1ba76586
YL
1281
1282 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1283
1284 /*
1285 * Initialize the per-CPU GDT with the boot GDT,
1286 * and set up the GDT descriptor:
1287 */
1288
552be871 1289 switch_to_new_gdt(cpu);
2697fbd5
BG
1290 loadsegment(fs, 0);
1291
cf910e83 1292 load_current_idt();
1ba76586
YL
1293
1294 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1295 syscall_init();
1296
1297 wrmsrl(MSR_FS_BASE, 0);
1298 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1299 barrier();
1300
4763ed4d 1301 x86_configure_nx();
27fd185f 1302 enable_x2apic();
1ba76586
YL
1303
1304 /*
1305 * set up and load the per-CPU TSS
1306 */
0fe1e009 1307 if (!oist->ist[0]) {
92d65b23 1308 char *estacks = per_cpu(exception_stacks, cpu);
0f3fa48a 1309
1ba76586 1310 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
0f3fa48a 1311 estacks += exception_stack_sizes[v];
0fe1e009 1312 oist->ist[v] = t->x86_tss.ist[v] =
1ba76586 1313 (unsigned long)estacks;
228bdaa9
SR
1314 if (v == DEBUG_STACK-1)
1315 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
1ba76586
YL
1316 }
1317 }
1318
1319 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
0f3fa48a 1320
1ba76586
YL
1321 /*
1322 * <= is required because the CPU will access up to
1323 * 8 bits beyond the end of the IO permission bitmap.
1324 */
1325 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1326 t->io_bitmap[i] = ~0UL;
1327
1328 atomic_inc(&init_mm.mm_count);
1329 me->active_mm = &init_mm;
8c5dfd25 1330 BUG_ON(me->mm);
1ba76586
YL
1331 enter_lazy_tlb(&init_mm, me);
1332
1333 load_sp0(t, &current->thread);
1334 set_tss_desc(cpu, t);
1335 load_TR_desc();
1336 load_LDT(&init_mm.context);
1337
0bb9fef9
JW
1338 clear_all_debug_regs();
1339 dbg_restore_debug_regs();
1ba76586
YL
1340
1341 fpu_init();
1342
1ba76586
YL
1343 if (is_uv_system())
1344 uv_cpu_init();
1345}
1346
1347#else
1348
148f9bb8 1349void cpu_init(void)
9ee79a3d 1350{
d2cbcc49
RR
1351 int cpu = smp_processor_id();
1352 struct task_struct *curr = current;
34048c9e 1353 struct tss_struct *t = &per_cpu(init_tss, cpu);
9ee79a3d 1354 struct thread_struct *thread = &curr->thread;
62111195 1355
e6ebf5de
FY
1356 show_ucode_info_early();
1357
c2d1cec1 1358 if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)) {
62111195 1359 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
9766cdbc
JSR
1360 for (;;)
1361 local_irq_enable();
62111195
JF
1362 }
1363
1364 printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1365
1366 if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
1367 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
62111195 1368
cf910e83 1369 load_current_idt();
552be871 1370 switch_to_new_gdt(cpu);
1da177e4 1371
1da177e4
LT
1372 /*
1373 * Set up and load the per-CPU TSS and LDT
1374 */
1375 atomic_inc(&init_mm.mm_count);
62111195 1376 curr->active_mm = &init_mm;
8c5dfd25 1377 BUG_ON(curr->mm);
62111195 1378 enter_lazy_tlb(&init_mm, curr);
1da177e4 1379
faca6227 1380 load_sp0(t, thread);
34048c9e 1381 set_tss_desc(cpu, t);
1da177e4
LT
1382 load_TR_desc();
1383 load_LDT(&init_mm.context);
1384
f9a196b8
TG
1385 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1386
22c4e308 1387#ifdef CONFIG_DOUBLEFAULT
1da177e4
LT
1388 /* Set up doublefault TSS pointer in the GDT */
1389 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
22c4e308 1390#endif
1da177e4 1391
9766cdbc 1392 clear_all_debug_regs();
0bb9fef9 1393 dbg_restore_debug_regs();
1da177e4 1394
0e49bf66 1395 fpu_init();
1da177e4 1396}
1ba76586 1397#endif
5700f743
BP
1398
1399#ifdef CONFIG_X86_DEBUG_STATIC_CPU_HAS
1400void warn_pre_alternatives(void)
1401{
1402 WARN(1, "You're using static_cpu_has before alternatives have run!\n");
1403}
1404EXPORT_SYMBOL_GPL(warn_pre_alternatives);
1405#endif
4a90a99c
BP
1406
1407inline bool __static_cpu_has_safe(u16 bit)
1408{
1409 return boot_cpu_has(bit);
1410}
1411EXPORT_SYMBOL_GPL(__static_cpu_has_safe);