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