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