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