x86/fpu/init: Move __setup() functions to fpu/init.c
[firefly-linux-kernel-4.4.55.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 #ifdef CONFIG_X86_32
148 static int cachesize_override = -1;
149 static int disable_x86_serial_nr = 1;
150
151 static int __init cachesize_setup(char *str)
152 {
153         get_option(&str, &cachesize_override);
154         return 1;
155 }
156 __setup("cachesize=", cachesize_setup);
157
158 static 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 */
166 static inline int flag_is_changeable_p(u32 flag)
167 {
168         u32 f1, f2;
169
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          */
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
188                       : "=&r" (f1), "=&r" (f2)
189                       : "ir" (flag));
190
191         return ((f1^f2) & flag) != 0;
192 }
193
194 /* Probe for the CPUID instruction */
195 int have_cpuid_p(void)
196 {
197         return flag_is_changeable_p(X86_EFLAGS_ID);
198 }
199
200 static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
201 {
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);
218 }
219
220 static 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);
226 #else
227 static inline int flag_is_changeable_p(u32 flag)
228 {
229         return 1;
230 }
231 static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
232 {
233 }
234 #endif
235
236 static __init int setup_disable_smep(char *arg)
237 {
238         setup_clear_cpu_cap(X86_FEATURE_SMEP);
239         return 1;
240 }
241 __setup("nosmep", setup_disable_smep);
242
243 static __always_inline void setup_smep(struct cpuinfo_x86 *c)
244 {
245         if (cpu_has(c, X86_FEATURE_SMEP))
246                 cr4_set_bits(X86_CR4_SMEP);
247 }
248
249 static __init int setup_disable_smap(char *arg)
250 {
251         setup_clear_cpu_cap(X86_FEATURE_SMAP);
252         return 1;
253 }
254 __setup("nosmap", setup_disable_smap);
255
256 static __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
264         if (cpu_has(c, X86_FEATURE_SMAP)) {
265 #ifdef CONFIG_X86_SMAP
266                 cr4_set_bits(X86_CR4_SMAP);
267 #else
268                 cr4_clear_bits(X86_CR4_SMAP);
269 #endif
270         }
271 }
272
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  */
278 struct cpuid_dependent_feature {
279         u32 feature;
280         u32 level;
281 };
282
283 static const struct cpuid_dependent_feature
284 cpuid_dependent_features[] = {
285         { X86_FEATURE_MWAIT,            0x00000005 },
286         { X86_FEATURE_DCA,              0x00000009 },
287         { X86_FEATURE_XSAVE,            0x0000000d },
288         { 0, 0 }
289 };
290
291 static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
292 {
293         const struct cpuid_dependent_feature *df;
294
295         for (df = cpuid_dependent_features; df->feature; df++) {
296
297                 if (!cpu_has(c, df->feature))
298                         continue;
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                  */
306                 if (!((s32)df->level < 0 ?
307                      (u32)df->level > (u32)c->extended_cpuid_level :
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
316                        "CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
317                                 x86_cap_flag(df->feature), df->level);
318         }
319 }
320
321 /*
322  * Naming convention should be: <Name> [(<Codename>)]
323  * This table only is used unless init_<vendor>() below doesn't set it;
324  * in particular, if CPUID levels 0x80000002..4 are supported, this
325  * isn't used
326  */
327
328 /* Look up CPU names by table lookup. */
329 static const char *table_lookup_model(struct cpuinfo_x86 *c)
330 {
331 #ifdef CONFIG_X86_32
332         const struct legacy_cpu_model_info *info;
333
334         if (c->x86_model >= 16)
335                 return NULL;    /* Range check */
336
337         if (!this_cpu)
338                 return NULL;
339
340         info = this_cpu->legacy_models;
341
342         while (info->family) {
343                 if (info->family == c->x86)
344                         return info->model_names[c->x86_model];
345                 info++;
346         }
347 #endif
348         return NULL;            /* Not found */
349 }
350
351 __u32 cpu_caps_cleared[NCAPINTS];
352 __u32 cpu_caps_set[NCAPINTS];
353
354 void 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
362         load_stack_canary_segment();
363 }
364
365 /*
366  * Current gdt points %fs at the "master" per-cpu area: after this,
367  * it's on the real one.
368  */
369 void switch_to_new_gdt(int cpu)
370 {
371         struct desc_ptr gdt_descr;
372
373         gdt_descr.address = (long)get_cpu_gdt_table(cpu);
374         gdt_descr.size = GDT_SIZE - 1;
375         load_gdt(&gdt_descr);
376         /* Reload the per-cpu base */
377
378         load_percpu_segment(cpu);
379 }
380
381 static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
382
383 static void get_model_name(struct cpuinfo_x86 *c)
384 {
385         unsigned int *v;
386         char *p, *q;
387
388         if (c->extended_cpuid_level < 0x80000004)
389                 return;
390
391         v = (unsigned int *)c->x86_model_id;
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
397         /*
398          * Intel chips right-justify this string for some dumb reason;
399          * undo that brain damage:
400          */
401         p = q = &c->x86_model_id[0];
402         while (*p == ' ')
403                 p++;
404         if (p != q) {
405                 while (*p)
406                         *q++ = *p++;
407                 while (q <= &c->x86_model_id[48])
408                         *q++ = '\0';    /* Zero-pad the rest */
409         }
410 }
411
412 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
413 {
414         unsigned int n, dummy, ebx, ecx, edx, l2size;
415
416         n = c->extended_cpuid_level;
417
418         if (n >= 0x80000005) {
419                 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
420                 c->x86_cache_size = (ecx>>24) + (edx>>24);
421 #ifdef CONFIG_X86_64
422                 /* On K8 L1 TLB is inclusive, so don't count it */
423                 c->x86_tlbsize = 0;
424 #endif
425         }
426
427         if (n < 0x80000006)     /* Some chips just has a large L1. */
428                 return;
429
430         cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
431         l2size = ecx >> 16;
432
433 #ifdef CONFIG_X86_64
434         c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
435 #else
436         /* do processor-specific cache resizing */
437         if (this_cpu->legacy_cache_size)
438                 l2size = this_cpu->legacy_cache_size(c, l2size);
439
440         /* Allow user to override all this if necessary. */
441         if (cachesize_override != -1)
442                 l2size = cachesize_override;
443
444         if (l2size == 0)
445                 return;         /* Again, no L2 cache is possible */
446 #endif
447
448         c->x86_cache_size = l2size;
449 }
450
451 u16 __read_mostly tlb_lli_4k[NR_INFO];
452 u16 __read_mostly tlb_lli_2m[NR_INFO];
453 u16 __read_mostly tlb_lli_4m[NR_INFO];
454 u16 __read_mostly tlb_lld_4k[NR_INFO];
455 u16 __read_mostly tlb_lld_2m[NR_INFO];
456 u16 __read_mostly tlb_lld_4m[NR_INFO];
457 u16 __read_mostly tlb_lld_1g[NR_INFO];
458
459 static void cpu_detect_tlb(struct cpuinfo_x86 *c)
460 {
461         if (this_cpu->c_detect_tlb)
462                 this_cpu->c_detect_tlb(c);
463
464         pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
465                 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
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]);
471 }
472
473 void detect_ht(struct cpuinfo_x86 *c)
474 {
475 #ifdef CONFIG_X86_HT
476         u32 eax, ebx, ecx, edx;
477         int index_msb, core_bits;
478         static bool printed;
479
480         if (!cpu_has(c, X86_FEATURE_HT))
481                 return;
482
483         if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
484                 goto out;
485
486         if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
487                 return;
488
489         cpuid(1, &eax, &ebx, &ecx, &edx);
490
491         smp_num_siblings = (ebx & 0xff0000) >> 16;
492
493         if (smp_num_siblings == 1) {
494                 printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
495                 goto out;
496         }
497
498         if (smp_num_siblings <= 1)
499                 goto out;
500
501         index_msb = get_count_order(smp_num_siblings);
502         c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
503
504         smp_num_siblings = smp_num_siblings / c->x86_max_cores;
505
506         index_msb = get_count_order(smp_num_siblings);
507
508         core_bits = get_count_order(c->x86_max_cores);
509
510         c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
511                                        ((1 << core_bits) - 1);
512
513 out:
514         if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
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);
519                 printed = 1;
520         }
521 #endif
522 }
523
524 static void get_cpu_vendor(struct cpuinfo_x86 *c)
525 {
526         char *v = c->x86_vendor_id;
527         int i;
528
529         for (i = 0; i < X86_VENDOR_NUM; i++) {
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]))) {
536
537                         this_cpu = cpu_devs[i];
538                         c->x86_vendor = this_cpu->c_x86_vendor;
539                         return;
540                 }
541         }
542
543         printk_once(KERN_ERR
544                         "CPU: vendor_id '%s' unknown, using generic init.\n" \
545                         "CPU: Your system may be unstable.\n", v);
546
547         c->x86_vendor = X86_VENDOR_UNKNOWN;
548         this_cpu = &default_cpu;
549 }
550
551 void cpu_detect(struct cpuinfo_x86 *c)
552 {
553         /* Get vendor name */
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]);
558
559         c->x86 = 4;
560         /* Intel-defined flags: level 0x00000001 */
561         if (c->cpuid_level >= 0x00000001) {
562                 u32 junk, tfms, cap0, misc;
563
564                 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
565                 c->x86 = (tfms >> 8) & 0xf;
566                 c->x86_model = (tfms >> 4) & 0xf;
567                 c->x86_mask = tfms & 0xf;
568
569                 if (c->x86 == 0xf)
570                         c->x86 += (tfms >> 20) & 0xff;
571                 if (c->x86 >= 0x6)
572                         c->x86_model += ((tfms >> 16) & 0xf) << 4;
573
574                 if (cap0 & (1<<19)) {
575                         c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
576                         c->x86_cache_alignment = c->x86_clflush_size;
577                 }
578         }
579 }
580
581 void get_cpu_cap(struct cpuinfo_x86 *c)
582 {
583         u32 tfms, xlvl;
584         u32 ebx;
585
586         /* Intel-defined flags: level 0x00000001 */
587         if (c->cpuid_level >= 0x00000001) {
588                 u32 capability, excap;
589
590                 cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
591                 c->x86_capability[0] = capability;
592                 c->x86_capability[4] = excap;
593         }
594
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
601                 c->x86_capability[9] = ebx;
602         }
603
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
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
637         /* AMD-defined flags: level 0x80000001 */
638         xlvl = cpuid_eax(0x80000000);
639         c->extended_cpuid_level = xlvl;
640
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);
645                 }
646         }
647
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;
653         }
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;
657 #endif
658
659         if (c->extended_cpuid_level >= 0x80000007)
660                 c->x86_power = cpuid_edx(0x80000007);
661
662         init_scattered_cpuid_features(c);
663 }
664
665 static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
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
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  */
700 static void __init early_identify_cpu(struct cpuinfo_x86 *c)
701 {
702 #ifdef CONFIG_X86_64
703         c->x86_clflush_size = 64;
704         c->x86_phys_bits = 36;
705         c->x86_virt_bits = 48;
706 #else
707         c->x86_clflush_size = 32;
708         c->x86_phys_bits = 32;
709         c->x86_virt_bits = 32;
710 #endif
711         c->x86_cache_alignment = c->x86_clflush_size;
712
713         memset(&c->x86_capability, 0, sizeof c->x86_capability);
714         c->extended_cpuid_level = 0;
715
716         if (!have_cpuid_p())
717                 identify_cpu_without_cpuid(c);
718
719         /* cyrix could have cpuid enabled via c_identify()*/
720         if (!have_cpuid_p())
721                 return;
722
723         cpu_detect(c);
724         get_cpu_vendor(c);
725         get_cpu_cap(c);
726         fpu__init_system(c);
727
728         if (this_cpu->c_early_init)
729                 this_cpu->c_early_init(c);
730
731         c->cpu_index = 0;
732         filter_cpuid_features(c, false);
733
734         if (this_cpu->c_bsp_init)
735                 this_cpu->c_bsp_init(c);
736
737         setup_force_cpu_cap(X86_FEATURE_ALWAYS);
738 }
739
740 void __init early_cpu_init(void)
741 {
742         const struct cpu_dev *const *cdev;
743         int count = 0;
744
745 #ifdef CONFIG_PROCESSOR_SELECT
746         printk(KERN_INFO "KERNEL supported cpus:\n");
747 #endif
748
749         for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
750                 const struct cpu_dev *cpudev = *cdev;
751
752                 if (count >= X86_VENDOR_NUM)
753                         break;
754                 cpu_devs[count] = cpudev;
755                 count++;
756
757 #ifdef CONFIG_PROCESSOR_SELECT
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                         }
767                 }
768 #endif
769         }
770         early_identify_cpu(&boot_cpu_data);
771 }
772
773 /*
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
779  * unless we can find a reliable way to detect all the broken cases.
780  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
781  */
782 static void detect_nopl(struct cpuinfo_x86 *c)
783 {
784 #ifdef CONFIG_X86_32
785         clear_cpu_cap(c, X86_FEATURE_NOPL);
786 #else
787         set_cpu_cap(c, X86_FEATURE_NOPL);
788 #endif
789 }
790
791 static void generic_identify(struct cpuinfo_x86 *c)
792 {
793         c->extended_cpuid_level = 0;
794
795         if (!have_cpuid_p())
796                 identify_cpu_without_cpuid(c);
797
798         /* cyrix could have cpuid enabled via c_identify()*/
799         if (!have_cpuid_p())
800                 return;
801
802         cpu_detect(c);
803
804         get_cpu_vendor(c);
805
806         get_cpu_cap(c);
807
808         if (c->cpuid_level >= 0x00000001) {
809                 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
810 #ifdef CONFIG_X86_32
811 # ifdef CONFIG_X86_HT
812                 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
813 # else
814                 c->apicid = c->initial_apicid;
815 # endif
816 #endif
817                 c->phys_proc_id = c->initial_apicid;
818         }
819
820         get_model_name(c); /* Default name */
821
822         detect_nopl(c);
823 }
824
825 static 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
839 /*
840  * This does the hard work of actually picking apart the CPU stuff...
841  */
842 static void identify_cpu(struct cpuinfo_x86 *c)
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;
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 */
852         c->x86_max_cores = 1;
853         c->x86_coreid_bits = 0;
854 #ifdef CONFIG_X86_64
855         c->x86_clflush_size = 64;
856         c->x86_phys_bits = 36;
857         c->x86_virt_bits = 48;
858 #else
859         c->cpuid_level = -1;    /* CPUID not detected */
860         c->x86_clflush_size = 32;
861         c->x86_phys_bits = 32;
862         c->x86_virt_bits = 32;
863 #endif
864         c->x86_cache_alignment = c->x86_clflush_size;
865         memset(&c->x86_capability, 0, sizeof c->x86_capability);
866
867         generic_identify(c);
868
869         if (this_cpu->c_identify)
870                 this_cpu->c_identify(c);
871
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
878 #ifdef CONFIG_X86_64
879         c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
880 #endif
881
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
898         /* Set up SMEP/SMAP */
899         setup_smep(c);
900         setup_smap(c);
901
902         /*
903          * The vendor-specific functions might have changed features.
904          * Now we do "generic changes."
905          */
906
907         /* Filter out anything that depends on CPUID levels we don't have */
908         filter_cpuid_features(c, true);
909
910         /* If the model name is still unset, do table lookup. */
911         if (!c->x86_model_id[0]) {
912                 const char *p;
913                 p = table_lookup_model(c);
914                 if (p)
915                         strcpy(c->x86_model_id, p);
916                 else
917                         /* Last resort... */
918                         sprintf(c->x86_model_id, "%02x/%02x",
919                                 c->x86, c->x86_model);
920         }
921
922 #ifdef CONFIG_X86_64
923         detect_ht(c);
924 #endif
925
926         init_hypervisor(c);
927         x86_init_rdrand(c);
928         x86_init_cache_qos(c);
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
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          */
945         if (c != &boot_cpu_data) {
946                 /* AND the already accumulated flags with these */
947                 for (i = 0; i < NCAPINTS; i++)
948                         boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
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];
953         }
954
955         /* Init Machine Check Exception if available. */
956         mcheck_cpu_init(c);
957
958         select_idle_routine(c);
959
960 #ifdef CONFIG_NUMA
961         numa_add_cpu(smp_processor_id());
962 #endif
963 }
964
965 /*
966  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
967  * on 32-bit kernels:
968  */
969 #ifdef CONFIG_X86_32
970 void enable_sep_cpu(void)
971 {
972         struct tss_struct *tss;
973         int cpu;
974
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         /*
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.
984          */
985
986         tss->x86_tss.ss1 = __KERNEL_CS;
987         wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
988
989         wrmsr(MSR_IA32_SYSENTER_ESP,
990               (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
991               0);
992
993         wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)ia32_sysenter_target, 0);
994
995 out:
996         put_cpu();
997 }
998 #endif
999
1000 void __init identify_boot_cpu(void)
1001 {
1002         identify_cpu(&boot_cpu_data);
1003         init_amd_e400_c1e_mask();
1004 #ifdef CONFIG_X86_32
1005         sysenter_setup();
1006         enable_sep_cpu();
1007 #endif
1008         cpu_detect_tlb(&boot_cpu_data);
1009 }
1010
1011 void identify_secondary_cpu(struct cpuinfo_x86 *c)
1012 {
1013         BUG_ON(c == &boot_cpu_data);
1014         identify_cpu(c);
1015 #ifdef CONFIG_X86_32
1016         enable_sep_cpu();
1017 #endif
1018         mtrr_ap_init();
1019 }
1020
1021 struct msr_range {
1022         unsigned        min;
1023         unsigned        max;
1024 };
1025
1026 static const struct msr_range msr_range_array[] = {
1027         { 0x00000000, 0x00000418},
1028         { 0xc0000000, 0xc000040b},
1029         { 0xc0010000, 0xc0010142},
1030         { 0xc0011000, 0xc001103b},
1031 };
1032
1033 static void __print_cpu_msr(void)
1034 {
1035         unsigned index_min, index_max;
1036         unsigned index;
1037         u64 val;
1038         int i;
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;
1043
1044                 for (index = index_min; index < index_max; index++) {
1045                         if (rdmsrl_safe(index, &val))
1046                                 continue;
1047                         printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
1048                 }
1049         }
1050 }
1051
1052 static int show_msr;
1053
1054 static __init int setup_show_msr(char *arg)
1055 {
1056         int num;
1057
1058         get_option(&arg, &num);
1059
1060         if (num > 0)
1061                 show_msr = num;
1062         return 1;
1063 }
1064 __setup("show_msr=", setup_show_msr);
1065
1066 static __init int setup_noclflush(char *arg)
1067 {
1068         setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1069         setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1070         return 1;
1071 }
1072 __setup("noclflush", setup_noclflush);
1073
1074 void print_cpu_info(struct cpuinfo_x86 *c)
1075 {
1076         const char *vendor = NULL;
1077
1078         if (c->x86_vendor < X86_VENDOR_NUM) {
1079                 vendor = this_cpu->c_vendor;
1080         } else {
1081                 if (c->cpuid_level >= 0)
1082                         vendor = c->x86_vendor_id;
1083         }
1084
1085         if (vendor && !strstr(c->x86_model_id, vendor))
1086                 printk(KERN_CONT "%s ", vendor);
1087
1088         if (c->x86_model_id[0])
1089                 printk(KERN_CONT "%s", strim(c->x86_model_id));
1090         else
1091                 printk(KERN_CONT "%d86", c->x86);
1092
1093         printk(KERN_CONT " (fam: %02x, model: %02x", c->x86, c->x86_model);
1094
1095         if (c->x86_mask || c->cpuid_level >= 0)
1096                 printk(KERN_CONT ", stepping: %02x)\n", c->x86_mask);
1097         else
1098                 printk(KERN_CONT ")\n");
1099
1100         print_cpu_msr(c);
1101 }
1102
1103 void print_cpu_msr(struct cpuinfo_x86 *c)
1104 {
1105         if (c->cpu_index < show_msr)
1106                 __print_cpu_msr();
1107 }
1108
1109 static __init int setup_disablecpuid(char *arg)
1110 {
1111         int bit;
1112
1113         if (get_option(&arg, &bit) && bit < NCAPINTS*32)
1114                 setup_clear_cpu_cap(bit);
1115         else
1116                 return 0;
1117
1118         return 1;
1119 }
1120 __setup("clearcpuid=", setup_disablecpuid);
1121
1122 DEFINE_PER_CPU(unsigned long, kernel_stack) =
1123         (unsigned long)&init_thread_union + THREAD_SIZE;
1124 EXPORT_PER_CPU_SYMBOL(kernel_stack);
1125
1126 #ifdef CONFIG_X86_64
1127 struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
1128 struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
1129                                     (unsigned long) debug_idt_table };
1130
1131 DEFINE_PER_CPU_FIRST(union irq_stack_union,
1132                      irq_stack_union) __aligned(PAGE_SIZE) __visible;
1133
1134 /*
1135  * The following percpu variables are hot.  Align current_task to
1136  * cacheline size such that they fall in the same cacheline.
1137  */
1138 DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1139         &init_task;
1140 EXPORT_PER_CPU_SYMBOL(current_task);
1141
1142 DEFINE_PER_CPU(char *, irq_stack_ptr) =
1143         init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
1144
1145 DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
1146
1147 DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1148 EXPORT_PER_CPU_SYMBOL(__preempt_count);
1149
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  */
1156 static 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
1161 static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
1162         [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
1163
1164 /* May not be marked __init: used by software suspend */
1165 void syscall_init(void)
1166 {
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);
1174
1175 #ifdef CONFIG_IA32_EMULATION
1176         wrmsrl(MSR_CSTAR, ia32_cstar_target);
1177         /*
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).
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);
1186 #else
1187         wrmsrl(MSR_CSTAR, ignore_sysret);
1188         wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1189         wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1190         wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1191 #endif
1192
1193         /* Flags to clear on syscall */
1194         wrmsrl(MSR_SYSCALL_MASK,
1195                X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
1196                X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1197 }
1198
1199 /*
1200  * Copies of the original ist values from the tss are only accessed during
1201  * debugging, no special alignment required.
1202  */
1203 DEFINE_PER_CPU(struct orig_ist, orig_ist);
1204
1205 static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
1206 DEFINE_PER_CPU(int, debug_stack_usage);
1207
1208 int is_debug_stack(unsigned long addr)
1209 {
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));
1213 }
1214 NOKPROBE_SYMBOL(is_debug_stack);
1215
1216 DEFINE_PER_CPU(u32, debug_idt_ctr);
1217
1218 void debug_stack_set_zero(void)
1219 {
1220         this_cpu_inc(debug_idt_ctr);
1221         load_current_idt();
1222 }
1223 NOKPROBE_SYMBOL(debug_stack_set_zero);
1224
1225 void debug_stack_reset(void)
1226 {
1227         if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
1228                 return;
1229         if (this_cpu_dec_return(debug_idt_ctr) == 0)
1230                 load_current_idt();
1231 }
1232 NOKPROBE_SYMBOL(debug_stack_reset);
1233
1234 #else   /* CONFIG_X86_64 */
1235
1236 DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1237 EXPORT_PER_CPU_SYMBOL(current_task);
1238 DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1239 EXPORT_PER_CPU_SYMBOL(__preempt_count);
1240
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  */
1246 DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1247         (unsigned long)&init_thread_union + THREAD_SIZE;
1248 EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1249
1250 #ifdef CONFIG_CC_STACKPROTECTOR
1251 DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
1252 #endif
1253
1254 #endif  /* CONFIG_X86_64 */
1255
1256 /*
1257  * Clear all 6 debug registers:
1258  */
1259 static 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 }
1271
1272 #ifdef CONFIG_KGDB
1273 /*
1274  * Restore debug regs if using kgdbwait and you have a kernel debugger
1275  * connection established.
1276  */
1277 static 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
1286 static 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
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.
1304  * A lot of state is already set up in PDA init for 64 bit
1305  */
1306 #ifdef CONFIG_X86_64
1307
1308 void cpu_init(void)
1309 {
1310         struct orig_ist *oist;
1311         struct task_struct *me;
1312         struct tss_struct *t;
1313         unsigned long v;
1314         int cpu = stack_smp_processor_id();
1315         int i;
1316
1317         wait_for_master_cpu(cpu);
1318
1319         /*
1320          * Initialize the CR4 shadow before doing anything that could
1321          * try to read it.
1322          */
1323         cr4_init_shadow();
1324
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
1331         t = &per_cpu(cpu_tss, cpu);
1332         oist = &per_cpu(orig_ist, cpu);
1333
1334 #ifdef CONFIG_NUMA
1335         if (this_cpu_read(numa_node) == 0 &&
1336             early_cpu_to_node(cpu) != NUMA_NO_NODE)
1337                 set_numa_node(early_cpu_to_node(cpu));
1338 #endif
1339
1340         me = current;
1341
1342         pr_debug("Initializing CPU#%d\n", cpu);
1343
1344         cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1345
1346         /*
1347          * Initialize the per-CPU GDT with the boot GDT,
1348          * and set up the GDT descriptor:
1349          */
1350
1351         switch_to_new_gdt(cpu);
1352         loadsegment(fs, 0);
1353
1354         load_current_idt();
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
1363         x86_configure_nx();
1364         x2apic_setup();
1365
1366         /*
1367          * set up and load the per-CPU TSS
1368          */
1369         if (!oist->ist[0]) {
1370                 char *estacks = per_cpu(exception_stacks, cpu);
1371
1372                 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
1373                         estacks += exception_stack_sizes[v];
1374                         oist->ist[v] = t->x86_tss.ist[v] =
1375                                         (unsigned long)estacks;
1376                         if (v == DEBUG_STACK-1)
1377                                 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
1378                 }
1379         }
1380
1381         t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1382
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;
1392         BUG_ON(me->mm);
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
1400         clear_all_debug_regs();
1401         dbg_restore_debug_regs();
1402
1403         fpu__init_cpu();
1404
1405         if (is_uv_system())
1406                 uv_cpu_init();
1407 }
1408
1409 #else
1410
1411 void cpu_init(void)
1412 {
1413         int cpu = smp_processor_id();
1414         struct task_struct *curr = current;
1415         struct tss_struct *t = &per_cpu(cpu_tss, cpu);
1416         struct thread_struct *thread = &curr->thread;
1417
1418         wait_for_master_cpu(cpu);
1419
1420         /*
1421          * Initialize the CR4 shadow before doing anything that could
1422          * try to read it.
1423          */
1424         cr4_init_shadow();
1425
1426         show_ucode_info_early();
1427
1428         printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1429
1430         if (cpu_feature_enabled(X86_FEATURE_VME) || cpu_has_tsc || cpu_has_de)
1431                 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1432
1433         load_current_idt();
1434         switch_to_new_gdt(cpu);
1435
1436         /*
1437          * Set up and load the per-CPU TSS and LDT
1438          */
1439         atomic_inc(&init_mm.mm_count);
1440         curr->active_mm = &init_mm;
1441         BUG_ON(curr->mm);
1442         enter_lazy_tlb(&init_mm, curr);
1443
1444         load_sp0(t, thread);
1445         set_tss_desc(cpu, t);
1446         load_TR_desc();
1447         load_LDT(&init_mm.context);
1448
1449         t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1450
1451 #ifdef CONFIG_DOUBLEFAULT
1452         /* Set up doublefault TSS pointer in the GDT */
1453         __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1454 #endif
1455
1456         clear_all_debug_regs();
1457         dbg_restore_debug_regs();
1458
1459         fpu__init_cpu();
1460 }
1461 #endif
1462
1463 #ifdef CONFIG_X86_DEBUG_STATIC_CPU_HAS
1464 void warn_pre_alternatives(void)
1465 {
1466         WARN(1, "You're using static_cpu_has before alternatives have run!\n");
1467 }
1468 EXPORT_SYMBOL_GPL(warn_pre_alternatives);
1469 #endif
1470
1471 inline bool __static_cpu_has_safe(u16 bit)
1472 {
1473         return boot_cpu_has(bit);
1474 }
1475 EXPORT_SYMBOL_GPL(__static_cpu_has_safe);