OSDN Git Service

Merge "cnss2: Add support for genoa sdio"
[sagit-ice-cold/kernel_xiaomi_msm8998.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/ctype.h>
9 #include <linux/delay.h>
10 #include <linux/sched.h>
11 #include <linux/init.h>
12 #include <linux/kprobes.h>
13 #include <linux/kgdb.h>
14 #include <linux/smp.h>
15 #include <linux/io.h>
16 #include <linux/syscore_ops.h>
17
18 #include <asm/stackprotector.h>
19 #include <asm/perf_event.h>
20 #include <asm/mmu_context.h>
21 #include <asm/archrandom.h>
22 #include <asm/hypervisor.h>
23 #include <asm/processor.h>
24 #include <asm/tlbflush.h>
25 #include <asm/debugreg.h>
26 #include <asm/sections.h>
27 #include <asm/vsyscall.h>
28 #include <linux/topology.h>
29 #include <linux/cpumask.h>
30 #include <asm/pgtable.h>
31 #include <linux/atomic.h>
32 #include <asm/proto.h>
33 #include <asm/setup.h>
34 #include <asm/apic.h>
35 #include <asm/desc.h>
36 #include <asm/fpu/internal.h>
37 #include <asm/mtrr.h>
38 #include <linux/numa.h>
39 #include <asm/asm.h>
40 #include <asm/cpu.h>
41 #include <asm/mce.h>
42 #include <asm/msr.h>
43 #include <asm/pat.h>
44 #include <asm/microcode.h>
45 #include <asm/microcode_intel.h>
46 #include <asm/intel-family.h>
47 #include <asm/cpu_device_id.h>
48
49 #ifdef CONFIG_X86_LOCAL_APIC
50 #include <asm/uv/uv.h>
51 #endif
52
53 #include "cpu.h"
54
55 /* all of these masks are initialized in setup_cpu_local_masks() */
56 cpumask_var_t cpu_initialized_mask;
57 cpumask_var_t cpu_callout_mask;
58 cpumask_var_t cpu_callin_mask;
59
60 /* representing cpus for which sibling maps can be computed */
61 cpumask_var_t cpu_sibling_setup_mask;
62
63 /* correctly size the local cpu masks */
64 void __init setup_cpu_local_masks(void)
65 {
66         alloc_bootmem_cpumask_var(&cpu_initialized_mask);
67         alloc_bootmem_cpumask_var(&cpu_callin_mask);
68         alloc_bootmem_cpumask_var(&cpu_callout_mask);
69         alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
70 }
71
72 static void default_init(struct cpuinfo_x86 *c)
73 {
74 #ifdef CONFIG_X86_64
75         cpu_detect_cache_sizes(c);
76 #else
77         /* Not much we can do here... */
78         /* Check if at least it has cpuid */
79         if (c->cpuid_level == -1) {
80                 /* No cpuid. It must be an ancient CPU */
81                 if (c->x86 == 4)
82                         strcpy(c->x86_model_id, "486");
83                 else if (c->x86 == 3)
84                         strcpy(c->x86_model_id, "386");
85         }
86 #endif
87 }
88
89 static const struct cpu_dev default_cpu = {
90         .c_init         = default_init,
91         .c_vendor       = "Unknown",
92         .c_x86_vendor   = X86_VENDOR_UNKNOWN,
93 };
94
95 static const struct cpu_dev *this_cpu = &default_cpu;
96
97 DEFINE_PER_CPU_PAGE_ALIGNED_USER_MAPPED(struct gdt_page, gdt_page) = { .gdt = {
98 #ifdef CONFIG_X86_64
99         /*
100          * We need valid kernel segments for data and code in long mode too
101          * IRET will check the segment types  kkeil 2000/10/28
102          * Also sysret mandates a special GDT layout
103          *
104          * TLS descriptors are currently at a different place compared to i386.
105          * Hopefully nobody expects them at a fixed place (Wine?)
106          */
107         [GDT_ENTRY_KERNEL32_CS]         = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
108         [GDT_ENTRY_KERNEL_CS]           = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
109         [GDT_ENTRY_KERNEL_DS]           = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
110         [GDT_ENTRY_DEFAULT_USER32_CS]   = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
111         [GDT_ENTRY_DEFAULT_USER_DS]     = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
112         [GDT_ENTRY_DEFAULT_USER_CS]     = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
113 #else
114         [GDT_ENTRY_KERNEL_CS]           = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
115         [GDT_ENTRY_KERNEL_DS]           = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
116         [GDT_ENTRY_DEFAULT_USER_CS]     = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
117         [GDT_ENTRY_DEFAULT_USER_DS]     = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
118         /*
119          * Segments used for calling PnP BIOS have byte granularity.
120          * They code segments and data segments have fixed 64k limits,
121          * the transfer segment sizes are set at run time.
122          */
123         /* 32-bit code */
124         [GDT_ENTRY_PNPBIOS_CS32]        = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
125         /* 16-bit code */
126         [GDT_ENTRY_PNPBIOS_CS16]        = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
127         /* 16-bit data */
128         [GDT_ENTRY_PNPBIOS_DS]          = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
129         /* 16-bit data */
130         [GDT_ENTRY_PNPBIOS_TS1]         = GDT_ENTRY_INIT(0x0092, 0, 0),
131         /* 16-bit data */
132         [GDT_ENTRY_PNPBIOS_TS2]         = GDT_ENTRY_INIT(0x0092, 0, 0),
133         /*
134          * The APM segments have byte granularity and their bases
135          * are set at run time.  All have 64k limits.
136          */
137         /* 32-bit code */
138         [GDT_ENTRY_APMBIOS_BASE]        = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
139         /* 16-bit code */
140         [GDT_ENTRY_APMBIOS_BASE+1]      = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
141         /* data */
142         [GDT_ENTRY_APMBIOS_BASE+2]      = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
143
144         [GDT_ENTRY_ESPFIX_SS]           = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
145         [GDT_ENTRY_PERCPU]              = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
146         GDT_STACK_CANARY_INIT
147 #endif
148 } };
149 EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
150
151 static int __init x86_mpx_setup(char *s)
152 {
153         /* require an exact match without trailing characters */
154         if (strlen(s))
155                 return 0;
156
157         /* do not emit a message if the feature is not present */
158         if (!boot_cpu_has(X86_FEATURE_MPX))
159                 return 1;
160
161         setup_clear_cpu_cap(X86_FEATURE_MPX);
162         pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n");
163         return 1;
164 }
165 __setup("nompx", x86_mpx_setup);
166
167 #ifdef CONFIG_X86_64
168 static int __init x86_pcid_setup(char *s)
169 {
170         /* require an exact match without trailing characters */
171         if (strlen(s))
172                 return 0;
173
174         /* do not emit a message if the feature is not present */
175         if (!boot_cpu_has(X86_FEATURE_PCID))
176                 return 1;
177
178         setup_clear_cpu_cap(X86_FEATURE_PCID);
179         pr_info("nopcid: PCID feature disabled\n");
180         return 1;
181 }
182 __setup("nopcid", x86_pcid_setup);
183 #endif
184
185 static int __init x86_noinvpcid_setup(char *s)
186 {
187         /* noinvpcid doesn't accept parameters */
188         if (s)
189                 return -EINVAL;
190
191         /* do not emit a message if the feature is not present */
192         if (!boot_cpu_has(X86_FEATURE_INVPCID))
193                 return 0;
194
195         setup_clear_cpu_cap(X86_FEATURE_INVPCID);
196         pr_info("noinvpcid: INVPCID feature disabled\n");
197         return 0;
198 }
199 early_param("noinvpcid", x86_noinvpcid_setup);
200
201 #ifdef CONFIG_X86_32
202 static int cachesize_override = -1;
203 static int disable_x86_serial_nr = 1;
204
205 static int __init cachesize_setup(char *str)
206 {
207         get_option(&str, &cachesize_override);
208         return 1;
209 }
210 __setup("cachesize=", cachesize_setup);
211
212 static int __init x86_sep_setup(char *s)
213 {
214         setup_clear_cpu_cap(X86_FEATURE_SEP);
215         return 1;
216 }
217 __setup("nosep", x86_sep_setup);
218
219 /* Standard macro to see if a specific flag is changeable */
220 static inline int flag_is_changeable_p(u32 flag)
221 {
222         u32 f1, f2;
223
224         /*
225          * Cyrix and IDT cpus allow disabling of CPUID
226          * so the code below may return different results
227          * when it is executed before and after enabling
228          * the CPUID. Add "volatile" to not allow gcc to
229          * optimize the subsequent calls to this function.
230          */
231         asm volatile ("pushfl           \n\t"
232                       "pushfl           \n\t"
233                       "popl %0          \n\t"
234                       "movl %0, %1      \n\t"
235                       "xorl %2, %0      \n\t"
236                       "pushl %0         \n\t"
237                       "popfl            \n\t"
238                       "pushfl           \n\t"
239                       "popl %0          \n\t"
240                       "popfl            \n\t"
241
242                       : "=&r" (f1), "=&r" (f2)
243                       : "ir" (flag));
244
245         return ((f1^f2) & flag) != 0;
246 }
247
248 /* Probe for the CPUID instruction */
249 int have_cpuid_p(void)
250 {
251         return flag_is_changeable_p(X86_EFLAGS_ID);
252 }
253
254 static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
255 {
256         unsigned long lo, hi;
257
258         if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
259                 return;
260
261         /* Disable processor serial number: */
262
263         rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
264         lo |= 0x200000;
265         wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
266
267         printk(KERN_NOTICE "CPU serial number disabled.\n");
268         clear_cpu_cap(c, X86_FEATURE_PN);
269
270         /* Disabling the serial number may affect the cpuid level */
271         c->cpuid_level = cpuid_eax(0);
272 }
273
274 static int __init x86_serial_nr_setup(char *s)
275 {
276         disable_x86_serial_nr = 0;
277         return 1;
278 }
279 __setup("serialnumber", x86_serial_nr_setup);
280 #else
281 static inline int flag_is_changeable_p(u32 flag)
282 {
283         return 1;
284 }
285 static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
286 {
287 }
288 #endif
289
290 static __init int setup_disable_smep(char *arg)
291 {
292         setup_clear_cpu_cap(X86_FEATURE_SMEP);
293         return 1;
294 }
295 __setup("nosmep", setup_disable_smep);
296
297 static __always_inline void setup_smep(struct cpuinfo_x86 *c)
298 {
299         if (cpu_has(c, X86_FEATURE_SMEP))
300                 cr4_set_bits(X86_CR4_SMEP);
301 }
302
303 static __init int setup_disable_smap(char *arg)
304 {
305         setup_clear_cpu_cap(X86_FEATURE_SMAP);
306         return 1;
307 }
308 __setup("nosmap", setup_disable_smap);
309
310 static __always_inline void setup_smap(struct cpuinfo_x86 *c)
311 {
312         unsigned long eflags = native_save_fl();
313
314         /* This should have been cleared long ago */
315         BUG_ON(eflags & X86_EFLAGS_AC);
316
317         if (cpu_has(c, X86_FEATURE_SMAP)) {
318 #ifdef CONFIG_X86_SMAP
319                 cr4_set_bits(X86_CR4_SMAP);
320 #else
321                 cr4_clear_bits(X86_CR4_SMAP);
322 #endif
323         }
324 }
325
326 static void setup_pcid(struct cpuinfo_x86 *c)
327 {
328         if (cpu_has(c, X86_FEATURE_PCID)) {
329                 if (cpu_has(c, X86_FEATURE_PGE) || kaiser_enabled) {
330                         cr4_set_bits(X86_CR4_PCIDE);
331                         /*
332                          * INVPCID has two "groups" of types:
333                          * 1/2: Invalidate an individual address
334                          * 3/4: Invalidate all contexts
335                          *
336                          * 1/2 take a PCID, but 3/4 do not.  So, 3/4
337                          * ignore the PCID argument in the descriptor.
338                          * But, we have to be careful not to call 1/2
339                          * with an actual non-zero PCID in them before
340                          * we do the above cr4_set_bits().
341                          */
342                         if (cpu_has(c, X86_FEATURE_INVPCID))
343                                 set_cpu_cap(c, X86_FEATURE_INVPCID_SINGLE);
344                 } else {
345                         /*
346                          * flush_tlb_all(), as currently implemented, won't
347                          * work if PCID is on but PGE is not.  Since that
348                          * combination doesn't exist on real hardware, there's
349                          * no reason to try to fully support it, but it's
350                          * polite to avoid corrupting data if we're on
351                          * an improperly configured VM.
352                          */
353                         clear_cpu_cap(c, X86_FEATURE_PCID);
354                 }
355         }
356         kaiser_setup_pcid();
357 }
358
359 /*
360  * Some CPU features depend on higher CPUID levels, which may not always
361  * be available due to CPUID level capping or broken virtualization
362  * software.  Add those features to this table to auto-disable them.
363  */
364 struct cpuid_dependent_feature {
365         u32 feature;
366         u32 level;
367 };
368
369 static const struct cpuid_dependent_feature
370 cpuid_dependent_features[] = {
371         { X86_FEATURE_MWAIT,            0x00000005 },
372         { X86_FEATURE_DCA,              0x00000009 },
373         { X86_FEATURE_XSAVE,            0x0000000d },
374         { 0, 0 }
375 };
376
377 static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
378 {
379         const struct cpuid_dependent_feature *df;
380
381         for (df = cpuid_dependent_features; df->feature; df++) {
382
383                 if (!cpu_has(c, df->feature))
384                         continue;
385                 /*
386                  * Note: cpuid_level is set to -1 if unavailable, but
387                  * extended_extended_level is set to 0 if unavailable
388                  * and the legitimate extended levels are all negative
389                  * when signed; hence the weird messing around with
390                  * signs here...
391                  */
392                 if (!((s32)df->level < 0 ?
393                      (u32)df->level > (u32)c->extended_cpuid_level :
394                      (s32)df->level > (s32)c->cpuid_level))
395                         continue;
396
397                 clear_cpu_cap(c, df->feature);
398                 if (!warn)
399                         continue;
400
401                 printk(KERN_WARNING
402                        "CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
403                                 x86_cap_flag(df->feature), df->level);
404         }
405 }
406
407 /*
408  * Naming convention should be: <Name> [(<Codename>)]
409  * This table only is used unless init_<vendor>() below doesn't set it;
410  * in particular, if CPUID levels 0x80000002..4 are supported, this
411  * isn't used
412  */
413
414 /* Look up CPU names by table lookup. */
415 static const char *table_lookup_model(struct cpuinfo_x86 *c)
416 {
417 #ifdef CONFIG_X86_32
418         const struct legacy_cpu_model_info *info;
419
420         if (c->x86_model >= 16)
421                 return NULL;    /* Range check */
422
423         if (!this_cpu)
424                 return NULL;
425
426         info = this_cpu->legacy_models;
427
428         while (info->family) {
429                 if (info->family == c->x86)
430                         return info->model_names[c->x86_model];
431                 info++;
432         }
433 #endif
434         return NULL;            /* Not found */
435 }
436
437 __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS];
438 __u32 cpu_caps_set[NCAPINTS + NBUGINTS];
439
440 void load_percpu_segment(int cpu)
441 {
442 #ifdef CONFIG_X86_32
443         loadsegment(fs, __KERNEL_PERCPU);
444 #else
445         loadsegment(gs, 0);
446         wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
447 #endif
448         load_stack_canary_segment();
449 }
450
451 /*
452  * Current gdt points %fs at the "master" per-cpu area: after this,
453  * it's on the real one.
454  */
455 void switch_to_new_gdt(int cpu)
456 {
457         struct desc_ptr gdt_descr;
458
459         gdt_descr.address = (long)get_cpu_gdt_table(cpu);
460         gdt_descr.size = GDT_SIZE - 1;
461         load_gdt(&gdt_descr);
462         /* Reload the per-cpu base */
463
464         load_percpu_segment(cpu);
465 }
466
467 static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
468
469 static void get_model_name(struct cpuinfo_x86 *c)
470 {
471         unsigned int *v;
472         char *p, *q, *s;
473
474         if (c->extended_cpuid_level < 0x80000004)
475                 return;
476
477         v = (unsigned int *)c->x86_model_id;
478         cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
479         cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
480         cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
481         c->x86_model_id[48] = 0;
482
483         /* Trim whitespace */
484         p = q = s = &c->x86_model_id[0];
485
486         while (*p == ' ')
487                 p++;
488
489         while (*p) {
490                 /* Note the last non-whitespace index */
491                 if (!isspace(*p))
492                         s = q;
493
494                 *q++ = *p++;
495         }
496
497         *(s + 1) = '\0';
498 }
499
500 void cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
501 {
502         unsigned int n, dummy, ebx, ecx, edx, l2size;
503
504         n = c->extended_cpuid_level;
505
506         if (n >= 0x80000005) {
507                 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
508                 c->x86_cache_size = (ecx>>24) + (edx>>24);
509 #ifdef CONFIG_X86_64
510                 /* On K8 L1 TLB is inclusive, so don't count it */
511                 c->x86_tlbsize = 0;
512 #endif
513         }
514
515         if (n < 0x80000006)     /* Some chips just has a large L1. */
516                 return;
517
518         cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
519         l2size = ecx >> 16;
520
521 #ifdef CONFIG_X86_64
522         c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
523 #else
524         /* do processor-specific cache resizing */
525         if (this_cpu->legacy_cache_size)
526                 l2size = this_cpu->legacy_cache_size(c, l2size);
527
528         /* Allow user to override all this if necessary. */
529         if (cachesize_override != -1)
530                 l2size = cachesize_override;
531
532         if (l2size == 0)
533                 return;         /* Again, no L2 cache is possible */
534 #endif
535
536         c->x86_cache_size = l2size;
537 }
538
539 u16 __read_mostly tlb_lli_4k[NR_INFO];
540 u16 __read_mostly tlb_lli_2m[NR_INFO];
541 u16 __read_mostly tlb_lli_4m[NR_INFO];
542 u16 __read_mostly tlb_lld_4k[NR_INFO];
543 u16 __read_mostly tlb_lld_2m[NR_INFO];
544 u16 __read_mostly tlb_lld_4m[NR_INFO];
545 u16 __read_mostly tlb_lld_1g[NR_INFO];
546
547 static void cpu_detect_tlb(struct cpuinfo_x86 *c)
548 {
549         if (this_cpu->c_detect_tlb)
550                 this_cpu->c_detect_tlb(c);
551
552         pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
553                 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
554                 tlb_lli_4m[ENTRIES]);
555
556         pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
557                 tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES],
558                 tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]);
559 }
560
561 void detect_ht(struct cpuinfo_x86 *c)
562 {
563 #ifdef CONFIG_SMP
564         u32 eax, ebx, ecx, edx;
565         int index_msb, core_bits;
566         static bool printed;
567
568         if (!cpu_has(c, X86_FEATURE_HT))
569                 return;
570
571         if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
572                 goto out;
573
574         if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
575                 return;
576
577         cpuid(1, &eax, &ebx, &ecx, &edx);
578
579         smp_num_siblings = (ebx & 0xff0000) >> 16;
580
581         if (smp_num_siblings == 1) {
582                 printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
583                 goto out;
584         }
585
586         if (smp_num_siblings <= 1)
587                 goto out;
588
589         index_msb = get_count_order(smp_num_siblings);
590         c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
591
592         smp_num_siblings = smp_num_siblings / c->x86_max_cores;
593
594         index_msb = get_count_order(smp_num_siblings);
595
596         core_bits = get_count_order(c->x86_max_cores);
597
598         c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
599                                        ((1 << core_bits) - 1);
600
601 out:
602         if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
603                 printk(KERN_INFO  "CPU: Physical Processor ID: %d\n",
604                        c->phys_proc_id);
605                 printk(KERN_INFO  "CPU: Processor Core ID: %d\n",
606                        c->cpu_core_id);
607                 printed = 1;
608         }
609 #endif
610 }
611
612 static void get_cpu_vendor(struct cpuinfo_x86 *c)
613 {
614         char *v = c->x86_vendor_id;
615         int i;
616
617         for (i = 0; i < X86_VENDOR_NUM; i++) {
618                 if (!cpu_devs[i])
619                         break;
620
621                 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
622                     (cpu_devs[i]->c_ident[1] &&
623                      !strcmp(v, cpu_devs[i]->c_ident[1]))) {
624
625                         this_cpu = cpu_devs[i];
626                         c->x86_vendor = this_cpu->c_x86_vendor;
627                         return;
628                 }
629         }
630
631         printk_once(KERN_ERR
632                         "CPU: vendor_id '%s' unknown, using generic init.\n" \
633                         "CPU: Your system may be unstable.\n", v);
634
635         c->x86_vendor = X86_VENDOR_UNKNOWN;
636         this_cpu = &default_cpu;
637 }
638
639 void cpu_detect(struct cpuinfo_x86 *c)
640 {
641         /* Get vendor name */
642         cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
643               (unsigned int *)&c->x86_vendor_id[0],
644               (unsigned int *)&c->x86_vendor_id[8],
645               (unsigned int *)&c->x86_vendor_id[4]);
646
647         c->x86 = 4;
648         /* Intel-defined flags: level 0x00000001 */
649         if (c->cpuid_level >= 0x00000001) {
650                 u32 junk, tfms, cap0, misc;
651
652                 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
653                 c->x86 = (tfms >> 8) & 0xf;
654                 c->x86_model = (tfms >> 4) & 0xf;
655                 c->x86_mask = tfms & 0xf;
656
657                 if (c->x86 == 0xf)
658                         c->x86 += (tfms >> 20) & 0xff;
659                 if (c->x86 >= 0x6)
660                         c->x86_model += ((tfms >> 16) & 0xf) << 4;
661
662                 if (cap0 & (1<<19)) {
663                         c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
664                         c->x86_cache_alignment = c->x86_clflush_size;
665                 }
666         }
667 }
668
669 static void apply_forced_caps(struct cpuinfo_x86 *c)
670 {
671         int i;
672
673         for (i = 0; i < NCAPINTS + NBUGINTS; i++) {
674                 c->x86_capability[i] &= ~cpu_caps_cleared[i];
675                 c->x86_capability[i] |= cpu_caps_set[i];
676         }
677 }
678
679 static void init_speculation_control(struct cpuinfo_x86 *c)
680 {
681         /*
682          * The Intel SPEC_CTRL CPUID bit implies IBRS and IBPB support,
683          * and they also have a different bit for STIBP support. Also,
684          * a hypervisor might have set the individual AMD bits even on
685          * Intel CPUs, for finer-grained selection of what's available.
686          */
687         if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
688                 set_cpu_cap(c, X86_FEATURE_IBRS);
689                 set_cpu_cap(c, X86_FEATURE_IBPB);
690                 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
691         }
692
693         if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
694                 set_cpu_cap(c, X86_FEATURE_STIBP);
695
696         if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
697             cpu_has(c, X86_FEATURE_VIRT_SSBD))
698                 set_cpu_cap(c, X86_FEATURE_SSBD);
699
700         if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
701                 set_cpu_cap(c, X86_FEATURE_IBRS);
702                 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
703         }
704
705         if (cpu_has(c, X86_FEATURE_AMD_IBPB))
706                 set_cpu_cap(c, X86_FEATURE_IBPB);
707
708         if (cpu_has(c, X86_FEATURE_AMD_STIBP)) {
709                 set_cpu_cap(c, X86_FEATURE_STIBP);
710                 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
711         }
712
713         if (cpu_has(c, X86_FEATURE_AMD_SSBD)) {
714                 set_cpu_cap(c, X86_FEATURE_SSBD);
715                 set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
716                 clear_cpu_cap(c, X86_FEATURE_VIRT_SSBD);
717         }
718 }
719
720 void get_cpu_cap(struct cpuinfo_x86 *c)
721 {
722         u32 eax, ebx, ecx, edx;
723
724         /* Intel-defined flags: level 0x00000001 */
725         if (c->cpuid_level >= 0x00000001) {
726                 cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
727
728                 c->x86_capability[CPUID_1_ECX] = ecx;
729                 c->x86_capability[CPUID_1_EDX] = edx;
730         }
731
732         /* Thermal and Power Management Leaf: level 0x00000006 (eax) */
733         if (c->cpuid_level >= 0x00000006)
734                 c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
735
736         /* Additional Intel-defined flags: level 0x00000007 */
737         if (c->cpuid_level >= 0x00000007) {
738                 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
739                 c->x86_capability[CPUID_7_0_EBX] = ebx;
740                 c->x86_capability[CPUID_7_ECX] = ecx;
741                 c->x86_capability[CPUID_7_EDX] = edx;
742         }
743
744         /* Extended state features: level 0x0000000d */
745         if (c->cpuid_level >= 0x0000000d) {
746                 cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);
747
748                 c->x86_capability[CPUID_D_1_EAX] = eax;
749         }
750
751         /* Additional Intel-defined flags: level 0x0000000F */
752         if (c->cpuid_level >= 0x0000000F) {
753
754                 /* QoS sub-leaf, EAX=0Fh, ECX=0 */
755                 cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
756                 c->x86_capability[CPUID_F_0_EDX] = edx;
757
758                 if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
759                         /* will be overridden if occupancy monitoring exists */
760                         c->x86_cache_max_rmid = ebx;
761
762                         /* QoS sub-leaf, EAX=0Fh, ECX=1 */
763                         cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
764                         c->x86_capability[CPUID_F_1_EDX] = edx;
765
766                         if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) {
767                                 c->x86_cache_max_rmid = ecx;
768                                 c->x86_cache_occ_scale = ebx;
769                         }
770                 } else {
771                         c->x86_cache_max_rmid = -1;
772                         c->x86_cache_occ_scale = -1;
773                 }
774         }
775
776         /* AMD-defined flags: level 0x80000001 */
777         eax = cpuid_eax(0x80000000);
778         c->extended_cpuid_level = eax;
779
780         if ((eax & 0xffff0000) == 0x80000000) {
781                 if (eax >= 0x80000001) {
782                         cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
783
784                         c->x86_capability[CPUID_8000_0001_ECX] = ecx;
785                         c->x86_capability[CPUID_8000_0001_EDX] = edx;
786                 }
787         }
788
789         if (c->extended_cpuid_level >= 0x80000007) {
790                 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
791
792                 c->x86_capability[CPUID_8000_0007_EBX] = ebx;
793                 c->x86_power = edx;
794         }
795
796         if (c->extended_cpuid_level >= 0x80000008) {
797                 cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
798
799                 c->x86_virt_bits = (eax >> 8) & 0xff;
800                 c->x86_phys_bits = eax & 0xff;
801                 c->x86_capability[CPUID_8000_0008_EBX] = ebx;
802         }
803 #ifdef CONFIG_X86_32
804         else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
805                 c->x86_phys_bits = 36;
806 #endif
807
808         c->x86_cache_bits = c->x86_phys_bits;
809
810         if (c->extended_cpuid_level >= 0x8000000a)
811                 c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a);
812
813         init_scattered_cpuid_features(c);
814         init_speculation_control(c);
815
816         /*
817          * Clear/Set all flags overridden by options, after probe.
818          * This needs to happen each time we re-probe, which may happen
819          * several times during CPU initialization.
820          */
821         apply_forced_caps(c);
822 }
823
824 static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
825 {
826 #ifdef CONFIG_X86_32
827         int i;
828
829         /*
830          * First of all, decide if this is a 486 or higher
831          * It's a 486 if we can modify the AC flag
832          */
833         if (flag_is_changeable_p(X86_EFLAGS_AC))
834                 c->x86 = 4;
835         else
836                 c->x86 = 3;
837
838         for (i = 0; i < X86_VENDOR_NUM; i++)
839                 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
840                         c->x86_vendor_id[0] = 0;
841                         cpu_devs[i]->c_identify(c);
842                         if (c->x86_vendor_id[0]) {
843                                 get_cpu_vendor(c);
844                                 break;
845                         }
846                 }
847 #endif
848 }
849
850 #define NO_SPECULATION  BIT(0)
851 #define NO_MELTDOWN     BIT(1)
852 #define NO_SSB          BIT(2)
853 #define NO_L1TF         BIT(3)
854 #define NO_MDS          BIT(4)
855 #define MSBDS_ONLY      BIT(5)
856 #define NO_SWAPGS       BIT(6)
857
858 #define VULNWL(_vendor, _family, _model, _whitelist)    \
859         { X86_VENDOR_##_vendor, _family, _model, X86_FEATURE_ANY, _whitelist }
860
861 #define VULNWL_INTEL(model, whitelist)          \
862         VULNWL(INTEL, 6, INTEL_FAM6_##model, whitelist)
863
864 #define VULNWL_AMD(family, whitelist)           \
865         VULNWL(AMD, family, X86_MODEL_ANY, whitelist)
866
867 static const __initconst struct x86_cpu_id cpu_vuln_whitelist[] = {
868         VULNWL(ANY,     4, X86_MODEL_ANY,       NO_SPECULATION),
869         VULNWL(CENTAUR, 5, X86_MODEL_ANY,       NO_SPECULATION),
870         VULNWL(INTEL,   5, X86_MODEL_ANY,       NO_SPECULATION),
871         VULNWL(NSC,     5, X86_MODEL_ANY,       NO_SPECULATION),
872
873         /* Intel Family 6 */
874         VULNWL_INTEL(ATOM_SALTWELL,             NO_SPECULATION),
875         VULNWL_INTEL(ATOM_SALTWELL_TABLET,      NO_SPECULATION),
876         VULNWL_INTEL(ATOM_SALTWELL_MID,         NO_SPECULATION),
877         VULNWL_INTEL(ATOM_BONNELL,              NO_SPECULATION),
878         VULNWL_INTEL(ATOM_BONNELL_MID,          NO_SPECULATION),
879
880         VULNWL_INTEL(ATOM_SILVERMONT,           NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS),
881         VULNWL_INTEL(ATOM_SILVERMONT_X,         NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS),
882         VULNWL_INTEL(ATOM_SILVERMONT_MID,       NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS),
883         VULNWL_INTEL(ATOM_AIRMONT,              NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS),
884         VULNWL_INTEL(XEON_PHI_KNL,              NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS),
885         VULNWL_INTEL(XEON_PHI_KNM,              NO_SSB | NO_L1TF | MSBDS_ONLY | NO_SWAPGS),
886
887         VULNWL_INTEL(CORE_YONAH,                NO_SSB),
888
889         VULNWL_INTEL(ATOM_AIRMONT_MID,          NO_L1TF | MSBDS_ONLY | NO_SWAPGS),
890
891         VULNWL_INTEL(ATOM_GOLDMONT,             NO_MDS | NO_L1TF | NO_SWAPGS),
892         VULNWL_INTEL(ATOM_GOLDMONT_X,           NO_MDS | NO_L1TF | NO_SWAPGS),
893         VULNWL_INTEL(ATOM_GOLDMONT_PLUS,        NO_MDS | NO_L1TF | NO_SWAPGS),
894
895         /*
896          * Technically, swapgs isn't serializing on AMD (despite it previously
897          * being documented as such in the APM).  But according to AMD, %gs is
898          * updated non-speculatively, and the issuing of %gs-relative memory
899          * operands will be blocked until the %gs update completes, which is
900          * good enough for our purposes.
901          */
902
903         /* AMD Family 0xf - 0x12 */
904         VULNWL_AMD(0x0f,        NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS),
905         VULNWL_AMD(0x10,        NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS),
906         VULNWL_AMD(0x11,        NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS),
907         VULNWL_AMD(0x12,        NO_MELTDOWN | NO_SSB | NO_L1TF | NO_MDS | NO_SWAPGS),
908
909         /* FAMILY_ANY must be last, otherwise 0x0f - 0x12 matches won't work */
910         VULNWL_AMD(X86_FAMILY_ANY,      NO_MELTDOWN | NO_L1TF | NO_MDS | NO_SWAPGS),
911         {}
912 };
913
914 static bool __init cpu_matches(unsigned long which)
915 {
916         const struct x86_cpu_id *m = x86_match_cpu(cpu_vuln_whitelist);
917
918         return m && !!(m->driver_data & which);
919 }
920
921 static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
922 {
923         u64 ia32_cap = 0;
924
925         if (cpu_matches(NO_SPECULATION))
926                 return;
927
928         setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
929         setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
930
931         if (cpu_has(c, X86_FEATURE_ARCH_CAPABILITIES))
932                 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
933
934         if (!cpu_matches(NO_SSB) && !(ia32_cap & ARCH_CAP_SSB_NO) &&
935            !cpu_has(c, X86_FEATURE_AMD_SSB_NO))
936                 setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
937
938         if (ia32_cap & ARCH_CAP_IBRS_ALL)
939                 setup_force_cpu_cap(X86_FEATURE_IBRS_ENHANCED);
940
941         if (!cpu_matches(NO_MDS) && !(ia32_cap & ARCH_CAP_MDS_NO)) {
942                 setup_force_cpu_bug(X86_BUG_MDS);
943                 if (cpu_matches(MSBDS_ONLY))
944                         setup_force_cpu_bug(X86_BUG_MSBDS_ONLY);
945         }
946
947         if (!cpu_matches(NO_SWAPGS))
948                 setup_force_cpu_bug(X86_BUG_SWAPGS);
949
950         if (cpu_matches(NO_MELTDOWN))
951                 return;
952
953         /* Rogue Data Cache Load? No! */
954         if (ia32_cap & ARCH_CAP_RDCL_NO)
955                 return;
956
957         setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
958
959         if (cpu_matches(NO_L1TF))
960                 return;
961
962         setup_force_cpu_bug(X86_BUG_L1TF);
963 }
964
965 /*
966  * Do minimum CPU detection early.
967  * Fields really needed: vendor, cpuid_level, family, model, mask,
968  * cache alignment.
969  * The others are not touched to avoid unwanted side effects.
970  *
971  * WARNING: this function is only called on the BP.  Don't add code here
972  * that is supposed to run on all CPUs.
973  */
974 static void __init early_identify_cpu(struct cpuinfo_x86 *c)
975 {
976 #ifdef CONFIG_X86_64
977         c->x86_clflush_size = 64;
978         c->x86_phys_bits = 36;
979         c->x86_virt_bits = 48;
980 #else
981         c->x86_clflush_size = 32;
982         c->x86_phys_bits = 32;
983         c->x86_virt_bits = 32;
984 #endif
985         c->x86_cache_alignment = c->x86_clflush_size;
986
987         memset(&c->x86_capability, 0, sizeof c->x86_capability);
988         c->extended_cpuid_level = 0;
989
990         if (!have_cpuid_p())
991                 identify_cpu_without_cpuid(c);
992
993         /* cyrix could have cpuid enabled via c_identify()*/
994         if (have_cpuid_p()) {
995                 cpu_detect(c);
996                 get_cpu_vendor(c);
997                 get_cpu_cap(c);
998
999                 if (this_cpu->c_early_init)
1000                         this_cpu->c_early_init(c);
1001
1002                 c->cpu_index = 0;
1003                 filter_cpuid_features(c, false);
1004
1005                 if (this_cpu->c_bsp_init)
1006                         this_cpu->c_bsp_init(c);
1007         }
1008
1009         setup_force_cpu_cap(X86_FEATURE_ALWAYS);
1010
1011         cpu_set_bug_bits(c);
1012
1013         fpu__init_system(c);
1014
1015 #ifdef CONFIG_X86_32
1016         /*
1017          * Regardless of whether PCID is enumerated, the SDM says
1018          * that it can't be enabled in 32-bit mode.
1019          */
1020         setup_clear_cpu_cap(X86_FEATURE_PCID);
1021 #endif
1022 }
1023
1024 void __init early_cpu_init(void)
1025 {
1026         const struct cpu_dev *const *cdev;
1027         int count = 0;
1028
1029 #ifdef CONFIG_PROCESSOR_SELECT
1030         printk(KERN_INFO "KERNEL supported cpus:\n");
1031 #endif
1032
1033         for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
1034                 const struct cpu_dev *cpudev = *cdev;
1035
1036                 if (count >= X86_VENDOR_NUM)
1037                         break;
1038                 cpu_devs[count] = cpudev;
1039                 count++;
1040
1041 #ifdef CONFIG_PROCESSOR_SELECT
1042                 {
1043                         unsigned int j;
1044
1045                         for (j = 0; j < 2; j++) {
1046                                 if (!cpudev->c_ident[j])
1047                                         continue;
1048                                 printk(KERN_INFO "  %s %s\n", cpudev->c_vendor,
1049                                         cpudev->c_ident[j]);
1050                         }
1051                 }
1052 #endif
1053         }
1054         early_identify_cpu(&boot_cpu_data);
1055 }
1056
1057 /*
1058  * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
1059  * unfortunately, that's not true in practice because of early VIA
1060  * chips and (more importantly) broken virtualizers that are not easy
1061  * to detect. In the latter case it doesn't even *fail* reliably, so
1062  * probing for it doesn't even work. Disable it completely on 32-bit
1063  * unless we can find a reliable way to detect all the broken cases.
1064  * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
1065  */
1066 static void detect_nopl(struct cpuinfo_x86 *c)
1067 {
1068 #ifdef CONFIG_X86_32
1069         clear_cpu_cap(c, X86_FEATURE_NOPL);
1070 #else
1071         set_cpu_cap(c, X86_FEATURE_NOPL);
1072 #endif
1073 }
1074
1075 static void generic_identify(struct cpuinfo_x86 *c)
1076 {
1077         c->extended_cpuid_level = 0;
1078
1079         if (!have_cpuid_p())
1080                 identify_cpu_without_cpuid(c);
1081
1082         /* cyrix could have cpuid enabled via c_identify()*/
1083         if (!have_cpuid_p())
1084                 return;
1085
1086         cpu_detect(c);
1087
1088         get_cpu_vendor(c);
1089
1090         get_cpu_cap(c);
1091
1092         if (c->cpuid_level >= 0x00000001) {
1093                 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
1094 #ifdef CONFIG_X86_32
1095 # ifdef CONFIG_SMP
1096                 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1097 # else
1098                 c->apicid = c->initial_apicid;
1099 # endif
1100 #endif
1101                 c->phys_proc_id = c->initial_apicid;
1102         }
1103
1104         get_model_name(c); /* Default name */
1105
1106         detect_nopl(c);
1107 }
1108
1109 static void x86_init_cache_qos(struct cpuinfo_x86 *c)
1110 {
1111         /*
1112          * The heavy lifting of max_rmid and cache_occ_scale are handled
1113          * in get_cpu_cap().  Here we just set the max_rmid for the boot_cpu
1114          * in case CQM bits really aren't there in this CPU.
1115          */
1116         if (c != &boot_cpu_data) {
1117                 boot_cpu_data.x86_cache_max_rmid =
1118                         min(boot_cpu_data.x86_cache_max_rmid,
1119                             c->x86_cache_max_rmid);
1120         }
1121 }
1122
1123 /*
1124  * This does the hard work of actually picking apart the CPU stuff...
1125  */
1126 static void identify_cpu(struct cpuinfo_x86 *c)
1127 {
1128         int i;
1129
1130         c->loops_per_jiffy = loops_per_jiffy;
1131         c->x86_cache_size = 0;
1132         c->x86_vendor = X86_VENDOR_UNKNOWN;
1133         c->x86_model = c->x86_mask = 0; /* So far unknown... */
1134         c->x86_vendor_id[0] = '\0'; /* Unset */
1135         c->x86_model_id[0] = '\0';  /* Unset */
1136         c->x86_max_cores = 1;
1137         c->x86_coreid_bits = 0;
1138 #ifdef CONFIG_X86_64
1139         c->x86_clflush_size = 64;
1140         c->x86_phys_bits = 36;
1141         c->x86_virt_bits = 48;
1142 #else
1143         c->cpuid_level = -1;    /* CPUID not detected */
1144         c->x86_clflush_size = 32;
1145         c->x86_phys_bits = 32;
1146         c->x86_virt_bits = 32;
1147 #endif
1148         c->x86_cache_alignment = c->x86_clflush_size;
1149         memset(&c->x86_capability, 0, sizeof c->x86_capability);
1150
1151         generic_identify(c);
1152
1153         if (this_cpu->c_identify)
1154                 this_cpu->c_identify(c);
1155
1156         /* Clear/Set all flags overridden by options, after probe */
1157         apply_forced_caps(c);
1158
1159 #ifdef CONFIG_X86_64
1160         c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
1161 #endif
1162
1163         /*
1164          * Vendor-specific initialization.  In this section we
1165          * canonicalize the feature flags, meaning if there are
1166          * features a certain CPU supports which CPUID doesn't
1167          * tell us, CPUID claiming incorrect flags, or other bugs,
1168          * we handle them here.
1169          *
1170          * At the end of this section, c->x86_capability better
1171          * indicate the features this CPU genuinely supports!
1172          */
1173         if (this_cpu->c_init)
1174                 this_cpu->c_init(c);
1175
1176         /* Disable the PN if appropriate */
1177         squash_the_stupid_serial_number(c);
1178
1179         /* Set up SMEP/SMAP */
1180         setup_smep(c);
1181         setup_smap(c);
1182
1183         /* Set up PCID */
1184         setup_pcid(c);
1185
1186         /*
1187          * The vendor-specific functions might have changed features.
1188          * Now we do "generic changes."
1189          */
1190
1191         /* Filter out anything that depends on CPUID levels we don't have */
1192         filter_cpuid_features(c, true);
1193
1194         /* If the model name is still unset, do table lookup. */
1195         if (!c->x86_model_id[0]) {
1196                 const char *p;
1197                 p = table_lookup_model(c);
1198                 if (p)
1199                         strcpy(c->x86_model_id, p);
1200                 else
1201                         /* Last resort... */
1202                         sprintf(c->x86_model_id, "%02x/%02x",
1203                                 c->x86, c->x86_model);
1204         }
1205
1206 #ifdef CONFIG_X86_64
1207         detect_ht(c);
1208 #endif
1209
1210         init_hypervisor(c);
1211         x86_init_rdrand(c);
1212         x86_init_cache_qos(c);
1213
1214         /*
1215          * Clear/Set all flags overriden by options, need do it
1216          * before following smp all cpus cap AND.
1217          */
1218         apply_forced_caps(c);
1219
1220         /*
1221          * On SMP, boot_cpu_data holds the common feature set between
1222          * all CPUs; so make sure that we indicate which features are
1223          * common between the CPUs.  The first time this routine gets
1224          * executed, c == &boot_cpu_data.
1225          */
1226         if (c != &boot_cpu_data) {
1227                 /* AND the already accumulated flags with these */
1228                 for (i = 0; i < NCAPINTS; i++)
1229                         boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
1230
1231                 /* OR, i.e. replicate the bug flags */
1232                 for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++)
1233                         c->x86_capability[i] |= boot_cpu_data.x86_capability[i];
1234         }
1235
1236         /* Init Machine Check Exception if available. */
1237         mcheck_cpu_init(c);
1238
1239         select_idle_routine(c);
1240
1241 #ifdef CONFIG_NUMA
1242         numa_add_cpu(smp_processor_id());
1243 #endif
1244 }
1245
1246 /*
1247  * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions
1248  * on 32-bit kernels:
1249  */
1250 #ifdef CONFIG_X86_32
1251 void enable_sep_cpu(void)
1252 {
1253         struct tss_struct *tss;
1254         int cpu;
1255
1256         cpu = get_cpu();
1257         tss = &per_cpu(cpu_tss, cpu);
1258
1259         if (!boot_cpu_has(X86_FEATURE_SEP))
1260                 goto out;
1261
1262         /*
1263          * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field --
1264          * see the big comment in struct x86_hw_tss's definition.
1265          */
1266
1267         tss->x86_tss.ss1 = __KERNEL_CS;
1268         wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
1269
1270         wrmsr(MSR_IA32_SYSENTER_ESP,
1271               (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack),
1272               0);
1273
1274         wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
1275
1276 out:
1277         put_cpu();
1278 }
1279 #endif
1280
1281 void __init identify_boot_cpu(void)
1282 {
1283         identify_cpu(&boot_cpu_data);
1284         init_amd_e400_c1e_mask();
1285 #ifdef CONFIG_X86_32
1286         sysenter_setup();
1287         enable_sep_cpu();
1288 #endif
1289         cpu_detect_tlb(&boot_cpu_data);
1290 }
1291
1292 void identify_secondary_cpu(struct cpuinfo_x86 *c)
1293 {
1294         BUG_ON(c == &boot_cpu_data);
1295         identify_cpu(c);
1296 #ifdef CONFIG_X86_32
1297         enable_sep_cpu();
1298 #endif
1299         mtrr_ap_init();
1300         x86_spec_ctrl_setup_ap();
1301 }
1302
1303 struct msr_range {
1304         unsigned        min;
1305         unsigned        max;
1306 };
1307
1308 static const struct msr_range msr_range_array[] = {
1309         { 0x00000000, 0x00000418},
1310         { 0xc0000000, 0xc000040b},
1311         { 0xc0010000, 0xc0010142},
1312         { 0xc0011000, 0xc001103b},
1313 };
1314
1315 static void __print_cpu_msr(void)
1316 {
1317         unsigned index_min, index_max;
1318         unsigned index;
1319         u64 val;
1320         int i;
1321
1322         for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
1323                 index_min = msr_range_array[i].min;
1324                 index_max = msr_range_array[i].max;
1325
1326                 for (index = index_min; index < index_max; index++) {
1327                         if (rdmsrl_safe(index, &val))
1328                                 continue;
1329                         printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
1330                 }
1331         }
1332 }
1333
1334 static int show_msr;
1335
1336 static __init int setup_show_msr(char *arg)
1337 {
1338         int num;
1339
1340         get_option(&arg, &num);
1341
1342         if (num > 0)
1343                 show_msr = num;
1344         return 1;
1345 }
1346 __setup("show_msr=", setup_show_msr);
1347
1348 static __init int setup_noclflush(char *arg)
1349 {
1350         setup_clear_cpu_cap(X86_FEATURE_CLFLUSH);
1351         setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT);
1352         return 1;
1353 }
1354 __setup("noclflush", setup_noclflush);
1355
1356 void print_cpu_info(struct cpuinfo_x86 *c)
1357 {
1358         const char *vendor = NULL;
1359
1360         if (c->x86_vendor < X86_VENDOR_NUM) {
1361                 vendor = this_cpu->c_vendor;
1362         } else {
1363                 if (c->cpuid_level >= 0)
1364                         vendor = c->x86_vendor_id;
1365         }
1366
1367         if (vendor && !strstr(c->x86_model_id, vendor))
1368                 printk(KERN_CONT "%s ", vendor);
1369
1370         if (c->x86_model_id[0])
1371                 printk(KERN_CONT "%s", c->x86_model_id);
1372         else
1373                 printk(KERN_CONT "%d86", c->x86);
1374
1375         printk(KERN_CONT " (family: 0x%x, model: 0x%x", c->x86, c->x86_model);
1376
1377         if (c->x86_mask || c->cpuid_level >= 0)
1378                 printk(KERN_CONT ", stepping: 0x%x)\n", c->x86_mask);
1379         else
1380                 printk(KERN_CONT ")\n");
1381
1382         print_cpu_msr(c);
1383 }
1384
1385 void print_cpu_msr(struct cpuinfo_x86 *c)
1386 {
1387         if (c->cpu_index < show_msr)
1388                 __print_cpu_msr();
1389 }
1390
1391 static __init int setup_disablecpuid(char *arg)
1392 {
1393         int bit;
1394
1395         if (get_option(&arg, &bit) && bit >= 0 && bit < NCAPINTS * 32)
1396                 setup_clear_cpu_cap(bit);
1397         else
1398                 return 0;
1399
1400         return 1;
1401 }
1402 __setup("clearcpuid=", setup_disablecpuid);
1403
1404 #ifdef CONFIG_X86_64
1405 struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
1406 struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
1407                                     (unsigned long) debug_idt_table };
1408
1409 DEFINE_PER_CPU_FIRST(union irq_stack_union,
1410                      irq_stack_union) __aligned(PAGE_SIZE) __visible;
1411
1412 /*
1413  * The following percpu variables are hot.  Align current_task to
1414  * cacheline size such that they fall in the same cacheline.
1415  */
1416 DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1417         &init_task;
1418 EXPORT_PER_CPU_SYMBOL(current_task);
1419
1420 DEFINE_PER_CPU(char *, irq_stack_ptr) =
1421         init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
1422
1423 DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1;
1424
1425 DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1426 EXPORT_PER_CPU_SYMBOL(__preempt_count);
1427
1428 /*
1429  * Special IST stacks which the CPU switches to when it calls
1430  * an IST-marked descriptor entry. Up to 7 stacks (hardware
1431  * limit), all of them are 4K, except the debug stack which
1432  * is 8K.
1433  */
1434 static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
1435           [0 ... N_EXCEPTION_STACKS - 1]        = EXCEPTION_STKSZ,
1436           [DEBUG_STACK - 1]                     = DEBUG_STKSZ
1437 };
1438
1439 DEFINE_PER_CPU_PAGE_ALIGNED_USER_MAPPED(char, exception_stacks
1440         [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
1441
1442 /* May not be marked __init: used by software suspend */
1443 void syscall_init(void)
1444 {
1445         /*
1446          * LSTAR and STAR live in a bit strange symbiosis.
1447          * They both write to the same internal register. STAR allows to
1448          * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
1449          */
1450         wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32);
1451         wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
1452
1453 #ifdef CONFIG_IA32_EMULATION
1454         wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
1455         /*
1456          * This only works on Intel CPUs.
1457          * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
1458          * This does not cause SYSENTER to jump to the wrong location, because
1459          * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
1460          */
1461         wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
1462         wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1463         wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
1464 #else
1465         wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
1466         wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
1467         wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
1468         wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
1469 #endif
1470
1471         /* Flags to clear on syscall */
1472         wrmsrl(MSR_SYSCALL_MASK,
1473                X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|
1474                X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT);
1475 }
1476
1477 /*
1478  * Copies of the original ist values from the tss are only accessed during
1479  * debugging, no special alignment required.
1480  */
1481 DEFINE_PER_CPU(struct orig_ist, orig_ist);
1482
1483 static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
1484 DEFINE_PER_CPU(int, debug_stack_usage);
1485
1486 int is_debug_stack(unsigned long addr)
1487 {
1488         return __this_cpu_read(debug_stack_usage) ||
1489                 (addr <= __this_cpu_read(debug_stack_addr) &&
1490                  addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ));
1491 }
1492 NOKPROBE_SYMBOL(is_debug_stack);
1493
1494 DEFINE_PER_CPU(u32, debug_idt_ctr);
1495
1496 void debug_stack_set_zero(void)
1497 {
1498         this_cpu_inc(debug_idt_ctr);
1499         load_current_idt();
1500 }
1501 NOKPROBE_SYMBOL(debug_stack_set_zero);
1502
1503 void debug_stack_reset(void)
1504 {
1505         if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
1506                 return;
1507         if (this_cpu_dec_return(debug_idt_ctr) == 0)
1508                 load_current_idt();
1509 }
1510 NOKPROBE_SYMBOL(debug_stack_reset);
1511
1512 #else   /* CONFIG_X86_64 */
1513
1514 DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1515 EXPORT_PER_CPU_SYMBOL(current_task);
1516 DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT;
1517 EXPORT_PER_CPU_SYMBOL(__preempt_count);
1518
1519 /*
1520  * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find
1521  * the top of the kernel stack.  Use an extra percpu variable to track the
1522  * top of the kernel stack directly.
1523  */
1524 DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =
1525         (unsigned long)&init_thread_union + THREAD_SIZE;
1526 EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack);
1527
1528 #ifdef CONFIG_CC_STACKPROTECTOR
1529 DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
1530 #endif
1531
1532 #endif  /* CONFIG_X86_64 */
1533
1534 /*
1535  * Clear all 6 debug registers:
1536  */
1537 static void clear_all_debug_regs(void)
1538 {
1539         int i;
1540
1541         for (i = 0; i < 8; i++) {
1542                 /* Ignore db4, db5 */
1543                 if ((i == 4) || (i == 5))
1544                         continue;
1545
1546                 set_debugreg(0, i);
1547         }
1548 }
1549
1550 #ifdef CONFIG_KGDB
1551 /*
1552  * Restore debug regs if using kgdbwait and you have a kernel debugger
1553  * connection established.
1554  */
1555 static void dbg_restore_debug_regs(void)
1556 {
1557         if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1558                 arch_kgdb_ops.correct_hw_break();
1559 }
1560 #else /* ! CONFIG_KGDB */
1561 #define dbg_restore_debug_regs()
1562 #endif /* ! CONFIG_KGDB */
1563
1564 static void wait_for_master_cpu(int cpu)
1565 {
1566 #ifdef CONFIG_SMP
1567         /*
1568          * wait for ACK from master CPU before continuing
1569          * with AP initialization
1570          */
1571         WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask));
1572         while (!cpumask_test_cpu(cpu, cpu_callout_mask))
1573                 cpu_relax();
1574 #endif
1575 }
1576
1577 /*
1578  * cpu_init() initializes state that is per-CPU. Some data is already
1579  * initialized (naturally) in the bootstrap process, such as the GDT
1580  * and IDT. We reload them nevertheless, this function acts as a
1581  * 'CPU state barrier', nothing should get across.
1582  * A lot of state is already set up in PDA init for 64 bit
1583  */
1584 #ifdef CONFIG_X86_64
1585
1586 void cpu_init(void)
1587 {
1588         struct orig_ist *oist;
1589         struct task_struct *me;
1590         struct tss_struct *t;
1591         unsigned long v;
1592         int cpu = stack_smp_processor_id();
1593         int i;
1594
1595         wait_for_master_cpu(cpu);
1596
1597         /*
1598          * Initialize the CR4 shadow before doing anything that could
1599          * try to read it.
1600          */
1601         cr4_init_shadow();
1602         if (!kaiser_enabled) {
1603                 /*
1604                  * secondary_startup_64() deferred setting PGE in cr4:
1605                  * probe_page_size_mask() sets it on the boot cpu,
1606                  * but it needs to be set on each secondary cpu.
1607                  */
1608                 cr4_set_bits(X86_CR4_PGE);
1609         }
1610
1611         /*
1612          * Load microcode on this cpu if a valid microcode is available.
1613          * This is early microcode loading procedure.
1614          */
1615         load_ucode_ap();
1616
1617         t = &per_cpu(cpu_tss, cpu);
1618         oist = &per_cpu(orig_ist, cpu);
1619
1620 #ifdef CONFIG_NUMA
1621         if (this_cpu_read(numa_node) == 0 &&
1622             early_cpu_to_node(cpu) != NUMA_NO_NODE)
1623                 set_numa_node(early_cpu_to_node(cpu));
1624 #endif
1625
1626         me = current;
1627
1628         pr_debug("Initializing CPU#%d\n", cpu);
1629
1630         cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1631
1632         /*
1633          * Initialize the per-CPU GDT with the boot GDT,
1634          * and set up the GDT descriptor:
1635          */
1636
1637         switch_to_new_gdt(cpu);
1638         loadsegment(fs, 0);
1639
1640         load_current_idt();
1641
1642         memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1643         syscall_init();
1644
1645         wrmsrl(MSR_FS_BASE, 0);
1646         wrmsrl(MSR_KERNEL_GS_BASE, 0);
1647         barrier();
1648
1649         x86_configure_nx();
1650         x2apic_setup();
1651
1652         /*
1653          * set up and load the per-CPU TSS
1654          */
1655         if (!oist->ist[0]) {
1656                 char *estacks = per_cpu(exception_stacks, cpu);
1657
1658                 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
1659                         estacks += exception_stack_sizes[v];
1660                         oist->ist[v] = t->x86_tss.ist[v] =
1661                                         (unsigned long)estacks;
1662                         if (v == DEBUG_STACK-1)
1663                                 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
1664                 }
1665         }
1666
1667         t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1668
1669         /*
1670          * <= is required because the CPU will access up to
1671          * 8 bits beyond the end of the IO permission bitmap.
1672          */
1673         for (i = 0; i <= IO_BITMAP_LONGS; i++)
1674                 t->io_bitmap[i] = ~0UL;
1675
1676         atomic_inc(&init_mm.mm_count);
1677         me->active_mm = &init_mm;
1678         BUG_ON(me->mm);
1679         enter_lazy_tlb(&init_mm, me);
1680
1681         load_sp0(t, &current->thread);
1682         set_tss_desc(cpu, t);
1683         load_TR_desc();
1684         load_mm_ldt(&init_mm);
1685
1686         clear_all_debug_regs();
1687         dbg_restore_debug_regs();
1688
1689         fpu__init_cpu();
1690
1691         if (is_uv_system())
1692                 uv_cpu_init();
1693 }
1694
1695 #else
1696
1697 void cpu_init(void)
1698 {
1699         int cpu = smp_processor_id();
1700         struct task_struct *curr = current;
1701         struct tss_struct *t = &per_cpu(cpu_tss, cpu);
1702         struct thread_struct *thread = &curr->thread;
1703
1704         wait_for_master_cpu(cpu);
1705
1706         /*
1707          * Initialize the CR4 shadow before doing anything that could
1708          * try to read it.
1709          */
1710         cr4_init_shadow();
1711
1712         show_ucode_info_early();
1713
1714         printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1715
1716         if (cpu_feature_enabled(X86_FEATURE_VME) ||
1717             cpu_has_tsc ||
1718             boot_cpu_has(X86_FEATURE_DE))
1719                 cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1720
1721         load_current_idt();
1722         switch_to_new_gdt(cpu);
1723
1724         /*
1725          * Set up and load the per-CPU TSS and LDT
1726          */
1727         atomic_inc(&init_mm.mm_count);
1728         curr->active_mm = &init_mm;
1729         BUG_ON(curr->mm);
1730         enter_lazy_tlb(&init_mm, curr);
1731
1732         load_sp0(t, thread);
1733         set_tss_desc(cpu, t);
1734         load_TR_desc();
1735         load_mm_ldt(&init_mm);
1736
1737         t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1738
1739 #ifdef CONFIG_DOUBLEFAULT
1740         /* Set up doublefault TSS pointer in the GDT */
1741         __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1742 #endif
1743
1744         clear_all_debug_regs();
1745         dbg_restore_debug_regs();
1746
1747         fpu__init_cpu();
1748 }
1749 #endif
1750
1751 static void bsp_resume(void)
1752 {
1753         if (this_cpu->c_bsp_resume)
1754                 this_cpu->c_bsp_resume(&boot_cpu_data);
1755 }
1756
1757 static struct syscore_ops cpu_syscore_ops = {
1758         .resume         = bsp_resume,
1759 };
1760
1761 static int __init init_cpu_syscore(void)
1762 {
1763         register_syscore_ops(&cpu_syscore_ops);
1764         return 0;
1765 }
1766 core_initcall(init_cpu_syscore);